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: handle more flags; ld: fix a data race #164

Merged
merged 2 commits into from
Oct 25, 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/Coff.zig
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ fn reportUndefs(self: *Coff) !void {
const nnotes = @min(notes.items.len, max_notes) + @intFromBool(notes.items.len > max_notes);

const err = try self.base.addErrorWithNotes(nnotes);
defer err.unlock();
try err.addMsg("undefined symbol: {s}", .{undef_sym.getName(self)});
has_undefs = true;

Expand All @@ -681,6 +682,7 @@ fn reportUndefs(self: *Coff) !void {
if (sym.getFile(self)) |_| continue;
has_undefs = true;
const err = try self.base.addErrorWithNotes(1);
defer err.unlock();
try err.addMsg("undefined symbol: {s}", .{sym.getName(self)});
try err.addNote("/force command line option", .{});
}
Expand Down
7 changes: 5 additions & 2 deletions src/Elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ pub fn flush(self: *Elf) !void {

if (has_parse_error) {
const err = try self.base.addErrorWithNotes(search_dirs.items.len);
defer err.unlock();
try err.addMsg("library search paths", .{});
for (search_dirs.items) |dir| {
try err.addNote(" {s}", .{dir});
Expand Down Expand Up @@ -1901,7 +1902,8 @@ fn reportDuplicates(self: *Elf) error{ HasDuplicates, OutOfMemory }!void {
const sym = self.resolver.keys.items[key - 1];
const nnotes = @min(notes.items.len, max_notes) + @intFromBool(notes.items.len > max_notes);

var err = try self.base.addErrorWithNotes(nnotes + 1);
const err = try self.base.addErrorWithNotes(nnotes + 1);
defer err.unlock();
try err.addMsg("duplicate symbol definition: {s}", .{sym.getName(self)});
try err.addNote("defined by {}", .{sym.getFile(self).?.fmtPath()});

Expand Down Expand Up @@ -1930,7 +1932,8 @@ fn reportUndefs(self: *Elf) !void {
const nrefs = @min(refs.items.len, max_notes);
const nnotes = nrefs + @intFromBool(refs.items.len > max_notes);

var err = try self.base.addErrorWithNotes(nnotes);
const err = try self.base.addErrorWithNotes(nnotes);
defer err.unlock();
try err.addMsg("undefined symbol: {s}", .{undef_sym.getName(self)});

for (refs.items[0..nrefs]) |ref| {
Expand Down
25 changes: 21 additions & 4 deletions src/Ld.zig
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,21 @@ pub fn flush(base: *Ld) !void {

pub fn warn(base: *Ld, comptime format: []const u8, args: anytype) void {
const warning = base.addWarningWithNotes(0) catch return;
defer warning.unlock();
warning.addMsg(format, args) catch return;
}

pub fn fatal(base: *Ld, comptime format: []const u8, args: anytype) void {
const err = base.addErrorWithNotes(0) catch return;
defer err.unlock();
err.addMsg(format, args) catch return;
}

pub const ErrorWithNotes = struct {
err_index: usize,
allocator: Allocator,
errors: []ErrorMsg,
lock: *std.Thread.Mutex,

pub fn addMsg(err: ErrorWithNotes, comptime format: []const u8, args: anytype) !void {
const err_msg = err.getErrorMsg();
Expand All @@ -283,26 +286,40 @@ pub const ErrorWithNotes = struct {
assert(err.err_index < err.errors.len);
return &err.errors[err.err_index];
}

pub fn unlock(err: ErrorWithNotes) void {
err.lock.unlock();
}
};

pub fn addErrorWithNotes(base: *Ld, note_count: usize) !ErrorWithNotes {
base.errors_mutex.lock();
defer base.errors_mutex.unlock();
errdefer base.errors_mutex.unlock();
const err_index = base.errors.items.len;
const err_msg = try base.errors.addOne(base.allocator);
err_msg.* = .{ .msg = undefined };
try err_msg.notes.ensureTotalCapacityPrecise(base.allocator, note_count);
return .{ .err_index = err_index, .allocator = base.allocator, .errors = base.errors.items };
return .{
.err_index = err_index,
.allocator = base.allocator,
.errors = base.errors.items,
.lock = &base.errors_mutex,
};
}

pub fn addWarningWithNotes(base: *Ld, note_count: usize) !ErrorWithNotes {
base.warnings_mutex.lock();
defer base.warnings_mutex.unlock();
errdefer base.warnings_mutex.unlock();
const err_index = base.warnings.items.len;
const err_msg = try base.warnings.addOne(base.allocator);
err_msg.* = .{ .msg = undefined };
try err_msg.notes.ensureTotalCapacityPrecise(base.allocator, note_count);
return .{ .err_index = err_index, .allocator = base.allocator, .errors = base.warnings.items };
return .{
.err_index = err_index,
.allocator = base.allocator,
.errors = base.warnings.items,
.lock = &base.warnings_mutex,
};
}

pub fn getAllWarningsAlloc(base: *Ld) !ErrorBundle {
Expand Down
7 changes: 6 additions & 1 deletion src/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ fn resolvePaths(
.lib => {
const full_path = (try self.resolveLib(arena, lib_dirs, obj.path)) orelse {
const err = try self.base.addErrorWithNotes(lib_dirs.len);
defer err.unlock();
try err.addMsg("library not found for {}", .{obj});
for (lib_dirs) |dir| try err.addNote("tried {s}", .{dir});
has_resolve_error = true;
Expand All @@ -478,6 +479,7 @@ fn resolvePaths(
.framework => {
const full_path = (try self.resolveFramework(arena, framework_dirs, obj.path)) orelse {
const err = try self.base.addErrorWithNotes(framework_dirs.len);
defer err.unlock();
try err.addMsg("framework not found for {}", .{obj});
for (framework_dirs) |dir| try err.addNote("tried {s}", .{dir});
has_resolve_error = true;
Expand Down Expand Up @@ -642,6 +644,7 @@ fn parseFatFile(self: *MachO, obj: LinkObject, file: std.fs.File) !?fat.Arch {
return error.MissingArch;
} else {
const err = try self.base.addErrorWithNotes(1 + fat_archs.len);
defer err.unlock();
try err.addMsg("{s}: ignoring universal file as no architecture specified", .{obj.path});
for (fat_archs) |arch| {
try err.addNote("universal file built for {s}", .{@tagName(arch.tag)});
Expand Down Expand Up @@ -1207,7 +1210,8 @@ fn reportDuplicates(self: *MachO) error{ HasDuplicates, OutOfMemory }!void {
const notes = self.dupes.get(key).?;
const nnotes = @min(notes.items.len, max_notes) + @intFromBool(notes.items.len > max_notes);

var err = try self.base.addErrorWithNotes(nnotes + 1);
const err = try self.base.addErrorWithNotes(nnotes + 1);
defer err.unlock();
try err.addMsg("duplicate symbol definition: {s}", .{sym.getName(self)});
try err.addNote("defined by {}", .{sym.getFile(self).?.fmtPath()});

Expand Down Expand Up @@ -1329,6 +1333,7 @@ fn reportUndefs(self: *MachO) !void {
};

const err = try addFn(&self.base, nnotes);
defer err.unlock();
try err.addMsg("undefined symbol: {s}", .{undef_sym.getName(self)});

switch (notes) {
Expand Down
1 change: 1 addition & 0 deletions src/MachO/Dylib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ fn parseBinary(self: *Dylib, macho_file: *MachO) !void {
}
} else {
const err = try macho_file.base.addErrorWithNotes(1 + platforms.items.len);
defer err.unlock();
try err.addMsg("{s}: object file was built for different platforms than required {s}", .{
self.path,
@tagName(plat.platform),
Expand Down
1 change: 1 addition & 0 deletions src/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
}
} else {
const err = try macho_file.base.addErrorWithNotes(1 + platforms.items.len);
defer err.unlock();
try err.addMsg("{}: object file was built for different platforms than required {s}", .{
self.fmtPath(),
@tagName(plat.platform),
Expand Down
8 changes: 8 additions & 0 deletions src/MachO/Options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const usage =
\\-dylib Create dynamic library
\\-dynamic Perform dynamic linking
\\-e [name] Specifies the entry point of main executable
\\-execute Create an executable (default)
\\-exported_symbol [name] Marks symbol [name] as global
\\-exported_symbols_list [filename] Specifies which symbols are to be marked as global
\\--entitlements Add path to entitlements file for embedding in code signature
Expand All @@ -53,6 +54,7 @@ const usage =
\\ -dylib_install_name
\\-l[name] Link against library
\\-L[path] Add search path for libraries
\\-macos_version_min [version] Set oldest macOS version that the output can be used on.
\\-needed_framework [name] Link against framework (even if unused)
\\-needed-l[name] Link against library (even if unused)
\\ -needed_library [name]
Expand Down Expand Up @@ -240,6 +242,8 @@ pub fn parse(arena: Allocator, args: []const []const u8, ctx: anytype) !Options
error.FileNotFound => ctx.fatal("Specified file in -exported_symbols_list {s} does not exist\n", .{filename}),
else => |e| return e,
};
} else if (p.flag1("execute")) {
opts.dylib = false;
} else if (p.arg1("e")) |name| {
opts.entry = name;
} else if (p.arg1("undefined")) |treatment| {
Expand Down Expand Up @@ -276,6 +280,10 @@ pub fn parse(arena: Allocator, args: []const []const u8, ctx: anytype) !Options
} else {
ctx.fatal("Could not parse CPU architecture from '{s}'\n", .{value});
}
} else if (p.arg1("macos_version_min")) |ver| {
const min_ver = Version.parse(ver) orelse
ctx.fatal("Unable to parse version from '{s}'\n", .{version});
opts.platform = .{ .platform = .MACOS, .version = min_ver };
} else if (p.arg1("platform_version")) |platform_s| {
// TODO clunky!
const min_v = it.next() orelse
Expand Down
41 changes: 21 additions & 20 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,26 @@ pub fn main() !void {

const ld = try Ld.openPath(gpa, tag, opts, &thread_pool);
defer ld.deinit();
ld.flush() catch |err| switch (err) {
error.FlushFailed,
error.InferCpuFailed,
error.ParseFailed,
error.HasDuplicates,
error.UndefinedSymbols,
error.RelocError,
error.ResolveFailed,
error.Unimplemented,
=> {
ld.reportWarnings();
ld.reportErrors();
std.process.exit(1);
},
else => |e| {
ld.reportErrors();
print("unexpected linker error: {s}\n", .{@errorName(e)});
return e;
},
};
const res = ld.flush();
ld.reportWarnings();
ld.reportErrors();
res catch |err| {
switch (err) {
error.FlushFailed,
error.InferCpuFailed,
error.ParseFailed,
error.HasDuplicates,
error.UndefinedSymbols,
error.RelocError,
error.ResolveFailed,
error.Unimplemented,
=> {
std.process.exit(1);
},
else => |e| {
print("unexpected linker error: {s}\n", .{@errorName(e)});
return e;
},
}
};
}
Loading