authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 07:19:25+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 13:34:26+02:00
log6ec8b15918dcd692f88875843a51a0514e315d9d
treec63f3721e1a1bf67e02c92833a8bbe8d430eb73c
parent8c76a61ef541cf285a82fcc928face4082606c03

elf: fix emitting static lib when ZigObject is present


2 files changed, 9 insertions(+), 13 deletions(-)

src/link/Elf/relocatable.zig+8-13
...@@ -18,7 +18,7 @@ pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]co...@@ -18,7 +18,7 @@ pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]co
18 }18 }
1919
20 for (positionals.items) |obj| {20 for (positionals.items) |obj| {
21 parsePositional(elf_file, obj.path) catch |err| switch (err) {21 parsePositionalStaticLib(elf_file, obj.path) catch |err| switch (err) {
22 error.MalformedObject,22 error.MalformedObject,
23 error.MalformedArchive,23 error.MalformedArchive,
24 error.InvalidMachineType,24 error.InvalidMachineType,
...@@ -38,17 +38,12 @@ pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]co...@@ -38,17 +38,12 @@ pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]co
38 // First, we flush relocatable object file generated with our backends.38 // First, we flush relocatable object file generated with our backends.
39 if (elf_file.zigObjectPtr()) |zig_object| {39 if (elf_file.zigObjectPtr()) |zig_object| {
40 try zig_object.resolveSymbols(elf_file);40 try zig_object.resolveSymbols(elf_file);
41 elf_file.markEhFrameAtomsDead();
41 try elf_file.addCommentString();42 try elf_file.addCommentString();
42 try elf_file.finalizeMergeSections();43 try elf_file.finalizeMergeSections();
43 zig_object.claimUnresolvedRelocatable(elf_file);44 zig_object.claimUnresolvedRelocatable(elf_file);
4445
45 for (elf_file.merge_sections.items) |*msec| {46 try initSections(elf_file);
46 if (msec.finalized_subsections.items.len == 0) continue;
47 try msec.initOutputSection(elf_file);
48 }
49
50 try elf_file.initSymtab();
51 try elf_file.initShStrtab();
52 try elf_file.sortShdrs();47 try elf_file.sortShdrs();
53 try zig_object.addAtomsToRelaSections(elf_file);48 try zig_object.addAtomsToRelaSections(elf_file);
54 try elf_file.updateMergeSectionSizes();49 try elf_file.updateMergeSectionSizes();
...@@ -229,17 +224,17 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const...@@ -229,17 +224,17 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
229 if (elf_file.base.hasErrors()) return error.FlushFailure;224 if (elf_file.base.hasErrors()) return error.FlushFailure;
230}225}
231226
232fn parsePositional(elf_file: *Elf, path: []const u8) Elf.ParseError!void {227fn parsePositionalStaticLib(elf_file: *Elf, path: []const u8) Elf.ParseError!void {
233 if (try Object.isObject(path)) {228 if (try Object.isObject(path)) {
234 try parseObject(elf_file, path);229 try parseObjectStaticLib(elf_file, path);
235 } else if (try Archive.isArchive(path)) {230 } else if (try Archive.isArchive(path)) {
236 try parseArchive(elf_file, path);231 try parseArchiveStaticLib(elf_file, path);
237 } else return error.UnknownFileType;232 } else return error.UnknownFileType;
238 // TODO: should we check for LD script?233 // TODO: should we check for LD script?
239 // Actually, should we even unpack an archive?234 // Actually, should we even unpack an archive?
240}235}
241236
242fn parseObject(elf_file: *Elf, path: []const u8) Elf.ParseError!void {237fn parseObjectStaticLib(elf_file: *Elf, path: []const u8) Elf.ParseError!void {
243 const gpa = elf_file.base.comp.gpa;238 const gpa = elf_file.base.comp.gpa;
244 const handle = try std.fs.cwd().openFile(path, .{});239 const handle = try std.fs.cwd().openFile(path, .{});
245 const fh = try elf_file.addFileHandle(handle);240 const fh = try elf_file.addFileHandle(handle);
...@@ -256,7 +251,7 @@ fn parseObject(elf_file: *Elf, path: []const u8) Elf.ParseError!void {...@@ -256,7 +251,7 @@ fn parseObject(elf_file: *Elf, path: []const u8) Elf.ParseError!void {
256 try object.parseAr(elf_file);251 try object.parseAr(elf_file);
257}252}
258253
259fn parseArchive(elf_file: *Elf, path: []const u8) Elf.ParseError!void {254fn parseArchiveStaticLib(elf_file: *Elf, path: []const u8) Elf.ParseError!void {
260 const gpa = elf_file.base.comp.gpa;255 const gpa = elf_file.base.comp.gpa;
261 const handle = try std.fs.cwd().openFile(path, .{});256 const handle = try std.fs.cwd().openFile(path, .{});
262 const fh = try elf_file.addFileHandle(handle);257 const fh = try elf_file.addFileHandle(handle);
test/link/elf.zig+1
...@@ -55,6 +55,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {...@@ -55,6 +55,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
5555
56 // Exercise linker in ar mode56 // Exercise linker in ar mode
57 elf_step.dependOn(testEmitStaticLib(b, .{ .target = musl_target }));57 elf_step.dependOn(testEmitStaticLib(b, .{ .target = musl_target }));
58 elf_step.dependOn(testEmitStaticLibZig(b, .{ .target = musl_target }));
5859
59 // Exercise linker with LLVM backend60 // Exercise linker with LLVM backend
60 // musl tests61 // musl tests