authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-06-05 16:46:50+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-06-05 16:46:50+02:00
log95966f6fd7dc09cb1b03e5c10f1f4feded2537b6
treeb6bce2f74743162e433611b2c9c5d7d62075e3cd
parent33826a6a2e035d2a2be65314ed80a6b7abaf7f12

elf+macho: use explicit alignment on decl is specified


3 files changed, 24 insertions(+), 8 deletions(-)

src/link/Elf.zig+1-1
......@@ -2300,7 +2300,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
23002300 defer self.base.allocator.free(decl_name);
23012301
23022302 log.debug("updateDeclCode {s}{*}", .{ decl_name, decl });
2303 const required_alignment = decl.ty.abiAlignment(self.base.options.target);
2303 const required_alignment = decl.getAlignment(self.base.options.target);
23042304
23052305 const decl_ptr = self.decls.getPtr(decl_index).?;
23062306 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);