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(
59115911 }
59125912
59135913 new_decl.val = try Value.Tag.function.create(new_decl_arena_allocator, new_func);
5914 new_decl.@"align" = 0;
59145915 new_decl.has_tv = true;
59155916 new_decl.owns_tv = true;
59165917 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
23042304 defer self.base.allocator.free(decl_name);
23052305
23062306 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
23092309 const decl_ptr = self.decls.getPtr(decl_index).?;
23102310 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
37863786 atom.code.clearRetainingCapacity();
37873787 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 );
37903796 const addr = try self.allocateAtom(atom, code.len, required_alignment, match);
37913797
37923798 log.debug("allocated atom for {s} at 0x{x}", .{ name, addr });
......@@ -3949,11 +3955,16 @@ fn needsPointerRebase(ty: Type, val: Value, mod: *Module) bool {
39493955 }
39503956}
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 {
39533966 const code = atom.code.items;
3954 const target = self.base.options.target;
39553967 const mod = self.base.options.module.?;
3956 const alignment = ty.abiAlignment(target);
39573968 const align_log_2 = math.log2(alignment);
39583969 const zig_ty = ty.zigTypeTag();
39593970 const mode = self.base.options.optimize_mode;
......@@ -4039,7 +4050,7 @@ fn getMatchingSectionAtom(self: *MachO, atom: *Atom, name: []const u8, ty: Type,
40394050fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*macho.nlist_64 {
40404051 const module = self.base.options.module.?;
40414052 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);
40434054 assert(decl.link.macho.local_sym_index != 0); // Caller forgot to call allocateDeclIndexes()
40444055 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
40484059
40494060 const decl_ptr = self.decls.getPtr(decl_index).?;
40504061 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 );
40524069 }
40534070 const match = decl_ptr.*.?;
40544071
test/behavior/bugs/1741.zig-1
......@@ -5,7 +5,6 @@ test "fixed" {
55 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
66 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
77 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
8 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
98
109 const x: f32 align(128) = 12.34;
1110 try std.testing.expect(@ptrToInt(&x) % 128 == 0);