Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macho: backport fixes from upstreaming MachO to Zig #102

Merged
merged 2 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

log.debug("parsing input object file {}", .{self.fmtPath()});

const gpa = macho_file.base.allocator;
var stream = std.io.fixedBufferStream(self.data);
const reader = stream.reader();
Expand Down
33 changes: 26 additions & 7 deletions src/MachO/UnwindInfo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ pub fn generate(info: *UnwindInfo, macho_file: *MachO) !void {
const rec = macho_file.getUnwindRecord(index);
if (rec.getFde(macho_file)) |fde| {
rec.enc.setDwarfSectionOffset(@intCast(fde.out_offset));
if (fde.getLsdaAtom(macho_file)) |lsda| {
rec.lsda = lsda.atom_index;
rec.lsda_offset = fde.lsda_offset;
rec.enc.setHasLsda(true);
}
const cie = fde.getCie(macho_file);
if (cie.getPersonality(macho_file)) |_| {
const personality_index = try info.getOrPutPersonalityFunction(cie.personality.?.index); // TODO handle error
rec.enc.setPersonalityIndex(personality_index + 1);
}
} else if (rec.getPersonality(macho_file)) |_| {
const personality_index = try info.getOrPutPersonalityFunction(rec.personality.?); // TODO handle error
rec.enc.setPersonalityIndex(personality_index + 1);
Expand Down Expand Up @@ -232,11 +242,13 @@ pub fn generate(info: *UnwindInfo, macho_file: *MachO) !void {
}

// Save records having an LSDA pointer
log.debug("LSDA pointers:", .{});
try info.lsdas_lookup.ensureTotalCapacityPrecise(gpa, info.records.items.len);
for (info.records.items, 0..) |index, i| {
const rec = macho_file.getUnwindRecord(index);
info.lsdas_lookup.appendAssumeCapacity(@intCast(info.lsdas.items.len));
if (rec.getLsdaAtom(macho_file)) |_| {
if (rec.getLsdaAtom(macho_file)) |lsda| {
log.debug(" @{x} => lsda({d})", .{ rec.getAtomAddress(macho_file), lsda.atom_index });
try info.lsdas.append(gpa, @intCast(i));
}
}
Expand Down Expand Up @@ -367,7 +379,8 @@ pub const Encoding = extern struct {

pub fn getMode(enc: Encoding) u4 {
comptime assert(macho.UNWIND_ARM64_MODE_MASK == macho.UNWIND_X86_64_MODE_MASK);
return @as(u4, @truncate((enc.enc & macho.UNWIND_ARM64_MODE_MASK) >> 24));
const shift = comptime @ctz(macho.UNWIND_ARM64_MODE_MASK);
return @as(u4, @truncate((enc.enc & macho.UNWIND_ARM64_MODE_MASK) >> shift));
}

pub fn isDwarf(enc: Encoding, macho_file: *MachO) bool {
Expand All @@ -380,26 +393,32 @@ pub const Encoding = extern struct {
}

pub fn setMode(enc: *Encoding, mode: anytype) void {
enc.enc |= @as(u32, @intCast(@intFromEnum(mode))) << 24;
comptime assert(macho.UNWIND_ARM64_MODE_MASK == macho.UNWIND_X86_64_MODE_MASK);
const shift = comptime @ctz(macho.UNWIND_ARM64_MODE_MASK);
enc.enc |= @as(u32, @intCast(@intFromEnum(mode))) << shift;
}

pub fn hasLsda(enc: Encoding) bool {
const has_lsda = @as(u1, @truncate((enc.enc & macho.UNWIND_HAS_LSDA) >> 31));
const shift = comptime @ctz(macho.UNWIND_HAS_LSDA);
const has_lsda = @as(u1, @truncate((enc.enc & macho.UNWIND_HAS_LSDA) >> shift));
return has_lsda == 1;
}

pub fn setHasLsda(enc: *Encoding, has_lsda: bool) void {
const mask = @as(u32, @intCast(@intFromBool(has_lsda))) << 31;
const shift = comptime @ctz(macho.UNWIND_HAS_LSDA);
const mask = @as(u32, @intCast(@intFromBool(has_lsda))) << shift;
enc.enc |= mask;
}

pub fn getPersonalityIndex(enc: Encoding) u2 {
const index = @as(u2, @truncate((enc.enc & macho.UNWIND_PERSONALITY_MASK) >> 28));
const shift = comptime @ctz(macho.UNWIND_PERSONALITY_MASK);
const index = @as(u2, @truncate((enc.enc & macho.UNWIND_PERSONALITY_MASK) >> shift));
return index;
}

pub fn setPersonalityIndex(enc: *Encoding, index: u2) void {
const mask = @as(u32, @intCast(index)) << 28;
const shift = comptime @ctz(macho.UNWIND_PERSONALITY_MASK);
const mask = @as(u32, @intCast(index)) << shift;
enc.enc |= mask;
}

Expand Down
7 changes: 6 additions & 1 deletion src/MachO/dead_strip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ fn mark(roots: []*Atom, objects: []const File.Index, macho_file: *MachO) void {
for (macho_file.getFile(index).?.getAtoms()) |atom_index| {
const atom = macho_file.getAtom(atom_index).?;
const isec = atom.getInputSection(macho_file);
if (isec.isDontDeadStripIfReferencesLive() and !atom.flags.alive and refersLive(atom, macho_file)) {
if (isec.isDontDeadStripIfReferencesLive() and
!(mem.eql(u8, isec.sectName(), "__eh_frame") or
mem.eql(u8, isec.sectName(), "__compact_unwind") or
isec.attrs() & macho.S_ATTR_DEBUG != 0) and
!atom.flags.alive and refersLive(atom, macho_file))
{
markLive(atom, macho_file);
loop = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/MachO/eh_frame.zig
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,9 @@ pub fn write(macho_file: *MachO, buffer: []u8) void {
}

if (fde.getLsdaAtom(macho_file)) |atom| {
const offset = fde.out_offset + fde.lsda_offset;
const offset = fde.out_offset + fde.lsda_ptr_offset;
const saddr = sect.addr + offset;
const taddr = atom.value;
const taddr = atom.value + fde.lsda_offset;
switch (fde.getCie(macho_file).lsda_size.?) {
.p32 => std.mem.writeInt(
i32,
Expand Down
102 changes: 70 additions & 32 deletions test/macho.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2416,40 +2416,78 @@ fn testSymbolStabs(b: *Build, opts: Options) *Step {
else => unreachable,
}

const exe = cc(b, "a.out", opts);
exe.addCSource(
\\#include <stdio.h>
\\int get_x();
\\void incr_x();
\\int bar();
\\void print_x() {
\\ printf("x=%d\n", get_x());
\\}
\\void print_bar() {
\\ printf("bar=%d\n", bar());
\\}
\\int main() {
\\ print_x();
\\ incr_x();
\\ print_x();
\\ print_bar();
\\ return 0;
\\}
);
exe.addFileSource(a_o.getFile());
exe.addFileSource(b_o.getFile());
exe.addArg("-g");
{
const exe = cc(b, "a.out", opts);
exe.addCSource(
\\#include <stdio.h>
\\int get_x();
\\void incr_x();
\\int bar();
\\void print_x() {
\\ printf("x=%d\n", get_x());
\\}
\\void print_bar() {
\\ printf("bar=%d\n", bar());
\\}
\\int main() {
\\ print_x();
\\ incr_x();
\\ print_x();
\\ print_bar();
\\ return 0;
\\}
);
exe.addFileSource(a_o.getFile());
exe.addFileSource(b_o.getFile());
exe.addArg("-g");

const run = exe.run();
run.expectStdOutEqual(
\\x=0
\\x=1
\\bar=42
\\
);
test_step.dependOn(run.step());
const run = exe.run();
run.expectStdOutEqual(
\\x=0
\\x=1
\\bar=42
\\
);
test_step.dependOn(run.step());

// TODO check for _foo and _bar having set sizes in stabs

}

{
const exe = cc(b, "a.out", opts);
exe.addCSource(
\\#include <stdio.h>
\\int get_x();
\\void incr_x();
\\int bar();
\\void print_x() {
\\ printf("x=%d\n", get_x());
\\}
\\void print_bar() {
\\ printf("bar=%d\n", bar());
\\}
\\int main() {
\\ print_x();
\\ incr_x();
\\ print_x();
\\ print_bar();
\\ return 0;
\\}
);
exe.addFileSource(a_o.getFile());
exe.addFileSource(b_o.getFile());
exe.addArgs(&.{ "-g", "-Wl,-dead_strip" });

// TODO check for _foo and _bar having set sizes in stabs
const run = exe.run();
run.expectStdOutEqual(
\\x=0
\\x=1
\\bar=42
\\
);
test_step.dependOn(run.step());
}

return test_step;
}
Expand Down