authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-06-07 01:03:15+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-06-07 01:03:15+02:00
logd9b0c984aaf4f5e738ca4d06f160a9110f9167ec
treef4e60b5265c7450c170408192ce99156ee5a82eb
parentd1bfc83774ffaeeb7646f3003038bc4e4e94f143
parent5dade176d85bc2944f7f40760ae3105e951d5e65
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11794 from ziglang/elf-macho-alignment

elf+macho: use explicit alignment on Decl if specified

4 files changed, 25 insertions(+), 8 deletions(-)

src/Sema.zig+1
...@@ -5911,6 +5911,7 @@ fn instantiateGenericCall(...@@ -5911,6 +5911,7 @@ fn instantiateGenericCall(
5911 }5911 }
59125912
5913 new_decl.val = try Value.Tag.function.create(new_decl_arena_allocator, new_func);5913 new_decl.val = try Value.Tag.function.create(new_decl_arena_allocator, new_func);
5914 new_decl.@"align" = 0;
5914 new_decl.has_tv = true;5915 new_decl.has_tv = true;
5915 new_decl.owns_tv = true;5916 new_decl.owns_tv = true;
5916 new_decl.analysis = .complete;5917 new_decl.analysis = .complete;
src/link/Elf.zig+1-1
...@@ -2304,7 +2304,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s...@@ -2304,7 +2304,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
2304 defer self.base.allocator.free(decl_name);2304 defer self.base.allocator.free(decl_name);
23052305
2306 log.debug("updateDeclCode {s}{*}", .{ decl_name, decl });2306 log.debug("updateDeclCode {s}{*}", .{ decl_name, decl });
2307 const required_alignment = decl.ty.abiAlignment(self.base.options.target);2307 const required_alignment = decl.getAlignment(self.base.options.target);
23082308
2309 const decl_ptr = self.decls.getPtr(decl_index).?;2309 const decl_ptr = self.decls.getPtr(decl_index).?;
2310 if (decl_ptr.* == null) {2310 if (decl_ptr.* == null) {
src/link/MachO.zig+23-6
...@@ -3786,7 +3786,13 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu...@@ -3786,7 +3786,13 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
3786 atom.code.clearRetainingCapacity();3786 atom.code.clearRetainingCapacity();
3787 try atom.code.appendSlice(self.base.allocator, code);3787 try atom.code.appendSlice(self.base.allocator, code);
37883788
3789 const match = try self.getMatchingSectionAtom(atom, decl_name, typed_value.ty, typed_value.val);3789 const match = try self.getMatchingSectionAtom(
3790 atom,
3791 decl_name,
3792 typed_value.ty,
3793 typed_value.val,
3794 required_alignment,
3795 );
3790 const addr = try self.allocateAtom(atom, code.len, required_alignment, match);3796 const addr = try self.allocateAtom(atom, code.len, required_alignment, match);
37913797
3792 log.debug("allocated atom for {s} at 0x{x}", .{ name, addr });3798 log.debug("allocated atom for {s} at 0x{x}", .{ name, addr });
...@@ -3949,11 +3955,16 @@ fn needsPointerRebase(ty: Type, val: Value, mod: *Module) bool {...@@ -3949,11 +3955,16 @@ fn needsPointerRebase(ty: Type, val: Value, mod: *Module) bool {
3949 }3955 }
3950}3956}
39513957
3952fn getMatchingSectionAtom(self: *MachO, atom: *Atom, name: []const u8, ty: Type, val: Value) !MatchingSection {3958fn getMatchingSectionAtom(
3959 self: *MachO,
3960 atom: *Atom,
3961 name: []const u8,
3962 ty: Type,
3963 val: Value,
3964 alignment: u32,
3965) !MatchingSection {
3953 const code = atom.code.items;3966 const code = atom.code.items;
3954 const target = self.base.options.target;
3955 const mod = self.base.options.module.?;3967 const mod = self.base.options.module.?;
3956 const alignment = ty.abiAlignment(target);
3957 const align_log_2 = math.log2(alignment);3968 const align_log_2 = math.log2(alignment);
3958 const zig_ty = ty.zigTypeTag();3969 const zig_ty = ty.zigTypeTag();
3959 const mode = self.base.options.optimize_mode;3970 const mode = self.base.options.optimize_mode;
...@@ -4039,7 +4050,7 @@ fn getMatchingSectionAtom(self: *MachO, atom: *Atom, name: []const u8, ty: Type,...@@ -4039,7 +4050,7 @@ fn getMatchingSectionAtom(self: *MachO, atom: *Atom, name: []const u8, ty: Type,
4039fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*macho.nlist_64 {4050fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*macho.nlist_64 {
4040 const module = self.base.options.module.?;4051 const module = self.base.options.module.?;
4041 const decl = module.declPtr(decl_index);4052 const decl = module.declPtr(decl_index);
4042 const required_alignment = decl.ty.abiAlignment(self.base.options.target);4053 const required_alignment = decl.getAlignment(self.base.options.target);
4043 assert(decl.link.macho.local_sym_index != 0); // Caller forgot to call allocateDeclIndexes()4054 assert(decl.link.macho.local_sym_index != 0); // Caller forgot to call allocateDeclIndexes()
4044 const symbol = &self.locals.items[decl.link.macho.local_sym_index];4055 const symbol = &self.locals.items[decl.link.macho.local_sym_index];
40454056
...@@ -4048,7 +4059,13 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*mac...@@ -4048,7 +4059,13 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*mac
40484059
4049 const decl_ptr = self.decls.getPtr(decl_index).?;4060 const decl_ptr = self.decls.getPtr(decl_index).?;
4050 if (decl_ptr.* == null) {4061 if (decl_ptr.* == null) {
4051 decl_ptr.* = try self.getMatchingSectionAtom(&decl.link.macho, sym_name, decl.ty, decl.val);4062 decl_ptr.* = try self.getMatchingSectionAtom(
4063 &decl.link.macho,
4064 sym_name,
4065 decl.ty,
4066 decl.val,
4067 required_alignment,
4068 );
4052 }4069 }
4053 const match = decl_ptr.*.?;4070 const match = decl_ptr.*.?;
40544071
test/behavior/bugs/1741.zig-1
...@@ -5,7 +5,6 @@ test "fixed" {...@@ -5,7 +5,6 @@ test "fixed" {
5 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO5 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
6 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO6 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
7 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO7 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
8 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
98
10 const x: f32 align(128) = 12.34;9 const x: f32 align(128) = 12.34;
11 try std.testing.expect(@ptrToInt(&x) % 128 == 0);10 try std.testing.expect(@ptrToInt(&x) % 128 == 0);