Skip to content

Commit

Permalink
elf+macho: use explicit alignment on decl is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Jun 5, 2022
1 parent 33826a6 commit 95966f6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/link/Elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2300,7 +2300,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
defer self.base.allocator.free(decl_name);

log.debug("updateDeclCode {s}{*}", .{ decl_name, decl });
const required_alignment = decl.ty.abiAlignment(self.base.options.target);
const required_alignment = decl.getAlignment(self.base.options.target);

const decl_ptr = self.decls.getPtr(decl_index).?;
if (decl_ptr.* == null) {
Expand Down
29 changes: 23 additions & 6 deletions src/link/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3786,7 +3786,13 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
atom.code.clearRetainingCapacity();
try atom.code.appendSlice(self.base.allocator, code);

const match = try self.getMatchingSectionAtom(atom, decl_name, typed_value.ty, typed_value.val);
const match = try self.getMatchingSectionAtom(
atom,
decl_name,
typed_value.ty,
typed_value.val,
required_alignment,
);
const addr = try self.allocateAtom(atom, code.len, required_alignment, match);

log.debug("allocated atom for {s} at 0x{x}", .{ name, addr });
Expand Down Expand Up @@ -3949,11 +3955,16 @@ fn needsPointerRebase(ty: Type, val: Value, mod: *Module) bool {
}
}

fn getMatchingSectionAtom(self: *MachO, atom: *Atom, name: []const u8, ty: Type, val: Value) !MatchingSection {
fn getMatchingSectionAtom(
self: *MachO,
atom: *Atom,
name: []const u8,
ty: Type,
val: Value,
alignment: u32,
) !MatchingSection {
const code = atom.code.items;
const target = self.base.options.target;
const mod = self.base.options.module.?;
const alignment = ty.abiAlignment(target);
const align_log_2 = math.log2(alignment);
const zig_ty = ty.zigTypeTag();
const mode = self.base.options.optimize_mode;
Expand Down Expand Up @@ -4039,7 +4050,7 @@ fn getMatchingSectionAtom(self: *MachO, atom: *Atom, name: []const u8, ty: Type,
fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*macho.nlist_64 {
const module = self.base.options.module.?;
const decl = module.declPtr(decl_index);
const required_alignment = decl.ty.abiAlignment(self.base.options.target);
const required_alignment = decl.getAlignment(self.base.options.target);
assert(decl.link.macho.local_sym_index != 0); // Caller forgot to call allocateDeclIndexes()
const symbol = &self.locals.items[decl.link.macho.local_sym_index];

Expand All @@ -4048,7 +4059,13 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*mac

const decl_ptr = self.decls.getPtr(decl_index).?;
if (decl_ptr.* == null) {
decl_ptr.* = try self.getMatchingSectionAtom(&decl.link.macho, sym_name, decl.ty, decl.val);
decl_ptr.* = try self.getMatchingSectionAtom(
&decl.link.macho,
sym_name,
decl.ty,
decl.val,
required_alignment,
);
}
const match = decl_ptr.*.?;

Expand Down
1 change: 0 additions & 1 deletion test/behavior/bugs/1741.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ test "fixed" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO

const x: f32 align(128) = 12.34;
try std.testing.expect(@ptrToInt(&x) % 128 == 0);
Expand Down

0 comments on commit 95966f6

Please sign in to comment.