authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-02 14:12:08+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-04 09:11:47+01:00
log481ee1b598b02cf5790ed12ed162a9f749a1ac92
treedc5e2c57578775a15f206837b66c96105f28f7a9
parentd2c4597eb5fcaba07f15c22092fc8c42c8b70ee3

elf: enable static-lib flush path


5 files changed, 57 insertions(+), 34 deletions(-)

src/arch/x86_64/Emit.zig+8-4
......@@ -85,15 +85,19 @@ pub fn emitMir(emit: *Emit) Error!void {
8585 @tagName(emit.lower.bin_file.tag),
8686 }),
8787 .linker_reloc => |data| if (emit.lower.bin_file.cast(link.File.Elf)) |elf_file| {
88 const is_obj = emit.lower.bin_file.options.effectiveOutputMode() == .Obj;
88 const is_obj_or_static_lib = switch (emit.lower.bin_file.options.output_mode) {
89 .Exe => false,
90 .Obj => true,
91 .Lib => emit.lower.bin_file.options.link_mode == .Static,
92 };
8993 const atom = elf_file.symbol(data.atom_index).atom(elf_file).?;
9094 const sym_index = elf_file.zigObjectPtr().?.symbol(data.sym_index);
9195 const sym = elf_file.symbol(sym_index);
92 if (sym.flags.needs_zig_got and !is_obj) {
96 if (sym.flags.needs_zig_got and !is_obj_or_static_lib) {
9397 _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file);
9498 }
9599 if (emit.lower.bin_file.options.pic) {
96 const r_type: u32 = if (sym.flags.needs_zig_got and !is_obj)
100 const r_type: u32 = if (sym.flags.needs_zig_got and !is_obj_or_static_lib)
97101 link.File.Elf.R_X86_64_ZIG_GOTPCREL
98102 else if (sym.flags.needs_got)
99103 std.elf.R_X86_64_GOTPCREL
......@@ -105,7 +109,7 @@ pub fn emitMir(emit: *Emit) Error!void {
105109 .r_addend = -4,
106110 });
107111 } else {
108 const r_type: u32 = if (sym.flags.needs_zig_got and !is_obj)
112 const r_type: u32 = if (sym.flags.needs_zig_got and !is_obj_or_static_lib)
109113 link.File.Elf.R_X86_64_ZIG_GOT32
110114 else if (sym.flags.needs_got)
111115 std.elf.R_X86_64_GOT32
src/arch/x86_64/Lower.zig+7-3
......@@ -327,7 +327,11 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
327327 }
328328 }.needsZigGot;
329329
330 const is_obj = lower.bin_file.options.effectiveOutputMode() == .Obj;
330 const is_obj_or_static_lib = switch (lower.bin_file.options.output_mode) {
331 .Exe => false,
332 .Obj => true,
333 .Lib => lower.bin_file.options.link_mode == .Static,
334 };
331335 var emit_prefix = prefix;
332336 var emit_mnemonic = mnemonic;
333337 var emit_ops_storage: [4]Operand = undefined;
......@@ -347,7 +351,7 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
347351 break :op .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) };
348352 },
349353 .mov => {
350 if (is_obj and needsZigGot(sym, lower.bin_file)) emit_mnemonic = .lea;
354 if (is_obj_or_static_lib and needsZigGot(sym, lower.bin_file)) emit_mnemonic = .lea;
351355 break :op .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) };
352356 },
353357 else => unreachable,
......@@ -360,7 +364,7 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
360364 break :op .{ .imm = Immediate.s(0) };
361365 },
362366 .mov => {
363 if (is_obj and needsZigGot(sym, lower.bin_file)) emit_mnemonic = .lea;
367 if (is_obj_or_static_lib and needsZigGot(sym, lower.bin_file)) emit_mnemonic = .lea;
364368 break :op .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{
365369 .base = .{ .reg = .ds },
366370 }) };
src/link/Elf.zig+36-22
......@@ -498,7 +498,7 @@ pub fn initMetadata(self: *Elf) !void {
498498
499499 const fillSection = struct {
500500 fn fillSection(elf_file: *Elf, shdr: *elf.Elf64_Shdr, size: u64, phndx: ?u16) void {
501 if (elf_file.isObject()) {
501 if (elf_file.isRelocatable()) {
502502 const off = elf_file.findFreeSpace(size, shdr.sh_addralign);
503503 shdr.sh_offset = off;
504504 shdr.sh_size = size;
......@@ -513,7 +513,7 @@ pub fn initMetadata(self: *Elf) !void {
513513
514514 comptime assert(number_of_zig_segments == 5);
515515
516 if (!self.isObject()) {
516 if (!self.isRelocatable()) {
517517 if (self.phdr_zig_load_re_index == null) {
518518 const filesz = self.base.options.program_code_size_hint;
519519 const off = self.findFreeSpace(filesz, self.page_size);
......@@ -597,7 +597,7 @@ pub fn initMetadata(self: *Elf) !void {
597597 });
598598 const shdr = &self.shdrs.items[self.zig_text_section_index.?];
599599 fillSection(self, shdr, self.base.options.program_code_size_hint, self.phdr_zig_load_re_index);
600 if (self.isObject()) {
600 if (self.isRelocatable()) {
601601 try zig_object.addSectionSymbol(self.zig_text_section_index.?, self);
602602 self.zig_text_rela_section_index = try self.addRelaShdr(
603603 ".rela.text.zig",
......@@ -613,7 +613,7 @@ pub fn initMetadata(self: *Elf) !void {
613613 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_text_section_index.?, .{});
614614 }
615615
616 if (self.zig_got_section_index == null and !self.isObject()) {
616 if (self.zig_got_section_index == null and !self.isRelocatable()) {
617617 self.zig_got_section_index = try self.addSection(.{
618618 .name = ".got.zig",
619619 .type = elf.SHT_PROGBITS,
......@@ -644,7 +644,7 @@ pub fn initMetadata(self: *Elf) !void {
644644 });
645645 const shdr = &self.shdrs.items[self.zig_data_rel_ro_section_index.?];
646646 fillSection(self, shdr, 1024, self.phdr_zig_load_ro_index);
647 if (self.isObject()) {
647 if (self.isRelocatable()) {
648648 try zig_object.addSectionSymbol(self.zig_data_rel_ro_section_index.?, self);
649649 self.zig_data_rel_ro_rela_section_index = try self.addRelaShdr(
650650 ".rela.data.rel.ro.zig",
......@@ -670,7 +670,7 @@ pub fn initMetadata(self: *Elf) !void {
670670 });
671671 const shdr = &self.shdrs.items[self.zig_data_section_index.?];
672672 fillSection(self, shdr, 1024, self.phdr_zig_load_rw_index);
673 if (self.isObject()) {
673 if (self.isRelocatable()) {
674674 try zig_object.addSectionSymbol(self.zig_data_section_index.?, self);
675675 self.zig_data_rela_section_index = try self.addRelaShdr(
676676 ".rela.data.zig",
......@@ -904,10 +904,6 @@ pub fn flush(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) link
904904 if (use_lld) {
905905 return self.linkWithLLD(comp, prog_node);
906906 }
907 if (self.base.options.output_mode == .Lib and self.isStatic()) {
908 // TODO writing static library files
909 return error.TODOImplementWritingLibFiles;
910 }
911907 try self.flushModule(comp, prog_node);
912908}
913909
......@@ -943,7 +939,12 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
943939 } else null;
944940 const gc_sections = self.base.options.gc_sections orelse false;
945941
946 if (self.isObject() and self.zig_object_index == null) {
942 if (self.isRelocatable() and self.zig_object_index == null) {
943 if (self.isStaticLib()) {
944 var err = try self.addErrorWithNotes(0);
945 try err.addMsg(self, "fatal linker error: emitting static libs unimplemented", .{});
946 return;
947 }
947948 // TODO this will become -r route I guess. For now, just copy the object file.
948949 assert(self.base.file == null); // TODO uncomment once we implement -r
949950 const the_object_path = blk: {
......@@ -1389,6 +1390,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
13891390 }
13901391
13911392 if (self.zigObjectPtr()) |zig_object| try zig_object.flushModule(self);
1393 if (self.isStaticLib()) return self.flushStaticLib(comp);
13921394
13931395 // Dedup shared objects
13941396 {
......@@ -1424,9 +1426,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
14241426 self.resolveSymbols();
14251427 self.markEhFrameAtomsDead();
14261428
1427 if (self.isObject()) {
1428 return self.flushObject(comp);
1429 }
1429 if (self.isObject()) return self.flushObject(comp);
14301430
14311431 try self.convertCommonSymbols();
14321432 self.markImportsExports();
......@@ -1511,7 +1511,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
15111511 try self.writeAtoms();
15121512 try self.writeSyntheticSections();
15131513
1514 if (self.entry_index == null and self.base.options.effectiveOutputMode() == .Exe) {
1514 if (self.entry_index == null and self.isExe()) {
15151515 log.debug("flushing. no_entry_point_found = true", .{});
15161516 self.error_flags.no_entry_point_found = true;
15171517 } else {
......@@ -1521,6 +1521,12 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
15211521 }
15221522}
15231523
1524pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void {
1525 _ = comp;
1526 var err = try self.addErrorWithNotes(0);
1527 try err.addMsg(self, "fatal linker error: emitting static libs unimplemented", .{});
1528}
1529
15241530pub fn flushObject(self: *Elf, comp: *Compilation) link.File.FlushError!void {
15251531 _ = comp;
15261532 self.claimUnresolvedObject();
......@@ -2822,7 +2828,7 @@ fn writeHeader(self: *Elf) !void {
28222828
28232829 assert(index == 16);
28242830
2825 const elf_type: elf.ET = switch (self.base.options.effectiveOutputMode()) {
2831 const elf_type: elf.ET = switch (self.base.options.output_mode) {
28262832 .Exe => if (self.base.options.pie) .DYN else .EXEC,
28272833 .Obj => .REL,
28282834 .Lib => switch (self.base.options.link_mode) {
......@@ -3147,11 +3153,11 @@ fn initSections(self: *Elf) !void {
31473153 };
31483154 const ptr_size = self.ptrWidthBytes();
31493155
3150 for (self.objects.items) |index| {
3156 if (!self.isStaticLib()) for (self.objects.items) |index| {
31513157 try self.file(index).?.object.initOutputSections(self);
3152 }
3158 };
31533159
3154 const needs_eh_frame = for (self.objects.items) |index| {
3160 const needs_eh_frame = if (self.isStaticLib()) false else for (self.objects.items) |index| {
31553161 if (self.file(index).?.object.cies.items.len > 0) break true;
31563162 } else false;
31573163 if (needs_eh_frame) {
......@@ -4954,15 +4960,23 @@ pub fn isStatic(self: Elf) bool {
49544960}
49554961
49564962pub fn isObject(self: Elf) bool {
4957 return self.base.options.effectiveOutputMode() == .Obj;
4963 return self.base.options.output_mode == .Obj;
49584964}
49594965
49604966pub fn isExe(self: Elf) bool {
4961 return self.base.options.effectiveOutputMode() == .Exe;
4967 return self.base.options.output_mode == .Exe;
4968}
4969
4970pub fn isStaticLib(self: Elf) bool {
4971 return self.base.options.output_mode == .Lib and self.isStatic();
4972}
4973
4974pub fn isRelocatable(self: Elf) bool {
4975 return self.isObject() or self.isStaticLib();
49624976}
49634977
49644978pub fn isDynLib(self: Elf) bool {
4965 return self.base.options.effectiveOutputMode() == .Lib and self.base.options.link_mode == .Dynamic;
4979 return self.base.options.output_mode == .Lib and !self.isStatic();
49664980}
49674981
49684982pub fn isZigSection(self: Elf, shndx: u16) bool {
src/link/Elf/Atom.zig+2-1
......@@ -605,7 +605,8 @@ fn dynAbsRelocAction(symbol: *const Symbol, elf_file: *Elf) RelocAction {
605605}
606606
607607fn outputType(elf_file: *Elf) u2 {
608 return switch (elf_file.base.options.effectiveOutputMode()) {
608 assert(!elf_file.isRelocatable());
609 return switch (elf_file.base.options.output_mode) {
609610 .Obj => unreachable,
610611 .Lib => 0,
611612 .Exe => if (elf_file.base.options.pie) 1 else 2,
src/link/Elf/ZigObject.zig+4-4
......@@ -287,7 +287,7 @@ pub fn addAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index {
287287}
288288
289289pub fn addSectionSymbol(self: *ZigObject, shndx: u16, elf_file: *Elf) !void {
290 assert(elf_file.isObject());
290 assert(elf_file.isRelocatable());
291291 const gpa = elf_file.base.allocator;
292292 const symbol_index = try elf_file.addSymbol();
293293 try self.local_symbols.append(gpa, symbol_index);
......@@ -886,7 +886,7 @@ fn updateDeclCode(
886886 sym.value = atom_ptr.value;
887887 esym.st_value = atom_ptr.value;
888888
889 if (!elf_file.isObject()) {
889 if (!elf_file.isRelocatable()) {
890890 log.debug(" (writing new offset table entry)", .{});
891891 assert(sym.flags.has_zig_got);
892892 const extra = sym.extra(elf_file).?;
......@@ -904,7 +904,7 @@ fn updateDeclCode(
904904 sym.flags.needs_zig_got = true;
905905 esym.st_value = atom_ptr.value;
906906
907 if (!elf_file.isObject()) {
907 if (!elf_file.isRelocatable()) {
908908 const gop = try sym.getOrCreateZigGotEntry(sym_index, elf_file);
909909 try elf_file.zig_got.writeOne(elf_file, gop.index);
910910 }
......@@ -1160,7 +1160,7 @@ fn updateLazySymbol(
11601160 local_sym.flags.needs_zig_got = true;
11611161 local_esym.st_value = atom_ptr.value;
11621162
1163 if (!elf_file.isObject()) {
1163 if (!elf_file.isRelocatable()) {
11641164 const gop = try local_sym.getOrCreateZigGotEntry(symbol_index, elf_file);
11651165 try elf_file.zig_got.writeOne(elf_file, gop.index);
11661166 }