authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-10 08:41:13+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-10 08:41:13+02:00
loga455b5692a657d01f07f5228a1abcb38f1abcc38
treede42b16f9c66e82554bd75cf1aade3dd9606646f
parenta6e9163284f75a9b251167eb7efb9b77169c5640

elf: create required linker-defined symbols


4 files changed, 99 insertions(+), 18 deletions(-)

src/link/Elf.zig+87-5
......@@ -36,6 +36,7 @@ phdr_load_rw_index: ?u16 = null,
3636
3737entry_addr: ?u64 = null,
3838page_size: u32,
39default_sym_version: elf.Elf64_Versym,
3940
4041/// .shstrtab buffer
4142shstrtab: StringTable(.strtab) = .{},
......@@ -57,6 +58,23 @@ shstrtab_section_index: ?u16 = null,
5758strtab_section_index: ?u16 = null,
5859symtab_section_index: ?u16 = null,
5960
61// Linker-defined symbols
62dynamic_index: ?Symbol.Index = null,
63ehdr_start_index: ?Symbol.Index = null,
64init_array_start_index: ?Symbol.Index = null,
65init_array_end_index: ?Symbol.Index = null,
66fini_array_start_index: ?Symbol.Index = null,
67fini_array_end_index: ?Symbol.Index = null,
68preinit_array_start_index: ?Symbol.Index = null,
69preinit_array_end_index: ?Symbol.Index = null,
70got_index: ?Symbol.Index = null,
71plt_index: ?Symbol.Index = null,
72end_index: ?Symbol.Index = null,
73gnu_eh_frame_hdr_index: ?Symbol.Index = null,
74dso_handle_index: ?Symbol.Index = null,
75rela_iplt_start_index: ?Symbol.Index = null,
76rela_iplt_end_index: ?Symbol.Index = null,
77
6078symbols: std.ArrayListUnmanaged(Symbol) = .{},
6179symbols_extra: std.ArrayListUnmanaged(u32) = .{},
6280resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{},
......@@ -188,6 +206,10 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Elf {
188206 .sparc64 => 0x2000,
189207 else => 0x1000,
190208 };
209 const default_sym_version: elf.Elf64_Versym = if (options.output_mode == .Lib and options.link_mode == .Dynamic)
210 elf.VER_NDX_GLOBAL
211 else
212 elf.VER_NDX_LOCAL;
191213
192214 var dwarf: ?Dwarf = if (!options.strip and options.module != null)
193215 Dwarf.init(gpa, &self.base, options.target)
......@@ -204,6 +226,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Elf {
204226 .dwarf = dwarf,
205227 .ptr_width = ptr_width,
206228 .page_size = page_size,
229 .default_sym_version = default_sym_version,
207230 };
208231 const use_llvm = options.use_llvm;
209232 if (use_llvm) {
......@@ -987,11 +1010,12 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
9871010 // corresponds to the Zig source code.
9881011 const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented;
9891012
990 self.linker_defined_index = blk: {
991 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));
992 self.files.set(index, .{ .linker_defined = .{ .index = index } });
993 break :blk index;
1013 const compiler_rt_path: ?[]const u8 = blk: {
1014 if (comp.compiler_rt_lib) |x| break :blk x.full_object_path;
1015 if (comp.compiler_rt_obj) |x| break :blk x.full_object_path;
1016 break :blk null;
9941017 };
1018 _ = compiler_rt_path;
9951019
9961020 if (self.lazy_syms.getPtr(.none)) |metadata| {
9971021 // Most lazy symbols can be updated on first use, but
......@@ -1023,6 +1047,16 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10231047 try dw.flushModule(module);
10241048 }
10251049
1050 if (self.linker_defined_index == null) {
1051 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));
1052 self.files.set(index, .{ .linker_defined = .{ .index = index } });
1053 self.linker_defined_index = index;
1054 }
1055
1056 try self.addLinkerDefinedSymbols();
1057
1058 // Beyond this point, everything has been allocated a virtual address and we can resolve
1059 // the relocations.
10261060 {
10271061 var it = self.relocs.iterator();
10281062 while (it.next()) |entry| {
......@@ -2682,6 +2716,54 @@ pub fn deleteDeclExport(
26822716 sym_index.* = 0;
26832717}
26842718
2719fn addLinkerDefinedSymbols(self: *Elf) !void {
2720 const linker_defined_index = self.linker_defined_index orelse return;
2721 const linker_defined = self.file(linker_defined_index).?.linker_defined;
2722 self.dynamic_index = try linker_defined.addGlobal("_DYNAMIC", self);
2723 self.ehdr_start_index = try linker_defined.addGlobal("__ehdr_start", self);
2724 self.init_array_start_index = try linker_defined.addGlobal("__init_array_start", self);
2725 self.init_array_end_index = try linker_defined.addGlobal("__init_array_end", self);
2726 self.fini_array_start_index = try linker_defined.addGlobal("__fini_array_start", self);
2727 self.fini_array_end_index = try linker_defined.addGlobal("__fini_array_end", self);
2728 self.preinit_array_start_index = try linker_defined.addGlobal("__preinit_array_start", self);
2729 self.preinit_array_end_index = try linker_defined.addGlobal("__preinit_array_end", self);
2730 self.got_index = try linker_defined.addGlobal("_GLOBAL_OFFSET_TABLE_", self);
2731 self.plt_index = try linker_defined.addGlobal("_PROCEDURE_LINKAGE_TABLE_", self);
2732 self.end_index = try linker_defined.addGlobal("_end", self);
2733
2734 if (self.base.options.eh_frame_hdr) {
2735 self.gnu_eh_frame_hdr_index = try linker_defined.addGlobal("__GNU_EH_FRAME_HDR", self);
2736 }
2737
2738 if (self.globalByName("__dso_handle")) |index| {
2739 if (self.symbol(index).file(self) == null)
2740 self.dso_handle_index = try linker_defined.addGlobal("__dso_handle", self);
2741 }
2742
2743 self.rela_iplt_start_index = try linker_defined.addGlobal("__rela_iplt_start", self);
2744 self.rela_iplt_end_index = try linker_defined.addGlobal("__rela_iplt_end", self);
2745
2746 // for (self.objects.items) |index| {
2747 // const object = self.getFile(index).?.object;
2748 // for (object.atoms.items) |atom_index| {
2749 // if (self.getStartStopBasename(atom_index)) |name| {
2750 // const gpa = self.base.allocator;
2751 // try self.start_stop_indexes.ensureUnusedCapacity(gpa, 2);
2752
2753 // const start = try std.fmt.allocPrintZ(gpa, "__start_{s}", .{name});
2754 // defer gpa.free(start);
2755 // const stop = try std.fmt.allocPrintZ(gpa, "__stop_{s}", .{name});
2756 // defer gpa.free(stop);
2757
2758 // self.start_stop_indexes.appendAssumeCapacity(try internal.addSyntheticGlobal(start, self));
2759 // self.start_stop_indexes.appendAssumeCapacity(try internal.addSyntheticGlobal(stop, self));
2760 // }
2761 // }
2762 // }
2763
2764 linker_defined.resolveSymbols(self);
2765}
2766
26852767fn updateSymtabSize(self: *Elf) !void {
26862768 var sizes = SymtabSize{};
26872769
......@@ -3193,7 +3275,7 @@ pub fn getOrPutGlobal(self: *Elf, name_off: u32) !GetOrPutGlobalResult {
31933275 };
31943276}
31953277
3196pub fn getGlobalByName(self: *Elf, name: []const u8) ?Symbol.Index {
3278pub fn globalByName(self: *Elf, name: []const u8) ?Symbol.Index {
31973279 const name_off = self.strtab.getOffset(name) orelse return null;
31983280 return self.resolver.get(name_off);
31993281}
src/link/Elf/LinkerDefined.zig+7-9
......@@ -22,7 +22,7 @@ pub fn addGlobal(self: *LinkerDefined, name: [:0]const u8, elf_file: *Elf) !u32
2222 .st_value = 0,
2323 .st_size = 0,
2424 });
25 const off = try elf_file.internString("{s}", .{name});
25 const off = try elf_file.strtab.insert(gpa, name);
2626 const gop = try elf_file.getOrPutGlobal(off);
2727 self.symbols.addOneAssumeCapacity().* = gop.index;
2828 return gop.index;
......@@ -37,14 +37,12 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
3737
3838 const global = elf_file.symbol(index);
3939 if (self.asFile().symbolRank(this_sym, false) < global.symbolRank(elf_file)) {
40 global.* = .{
41 .value = 0,
42 .name = global.name,
43 .atom = 0,
44 .file = self.index,
45 .sym_idx = sym_idx,
46 .ver_idx = elf_file.default_sym_version,
47 };
40 global.value = 0;
41 global.name_offset = global.name_offset;
42 global.atom_index = 0;
43 global.file_index = self.index;
44 global.esym_index = sym_idx;
45 global.version_index = elf_file.default_sym_version;
4846 }
4947 }
5048}
src/link/Elf/Symbol.zig+1-1
......@@ -81,7 +81,7 @@ pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 {
8181 // .object => |x| !x.alive,
8282 else => false,
8383 };
84 return file_ptr.symbolRank(sym, in_archive);
84 return file_ptr.symbolRank(sym.*, in_archive);
8585}
8686
8787pub fn address(symbol: Symbol, opts: struct {
src/link/Elf/file.zig+4-3
......@@ -25,8 +25,8 @@ pub const File = union(enum) {
2525 switch (file) {
2626 .zig_module => try writer.writeAll("(zig module)"),
2727 .linker_defined => try writer.writeAll("(linker defined)"),
28 .object => |x| try writer.print("{}", .{x.fmtPath()}),
29 .shared_object => |x| try writer.writeAll(x.path),
28 // .object => |x| try writer.print("{}", .{x.fmtPath()}),
29 // .shared_object => |x| try writer.writeAll(x.path),
3030 }
3131 }
3232
......@@ -62,7 +62,8 @@ pub const File = union(enum) {
6262 pub fn symbolRank(file: File, sym: elf.Elf64_Sym, in_archive: bool) u32 {
6363 const base: u3 = blk: {
6464 if (sym.st_shndx == elf.SHN_COMMON) break :blk if (in_archive) 6 else 5;
65 if (file == .shared or in_archive) break :blk switch (sym.st_bind()) {
65 // if (file == .shared or in_archive) break :blk switch (sym.st_bind()) {
66 if (in_archive) break :blk switch (sym.st_bind()) {
6667 elf.STB_GLOBAL => 3,
6768 else => 4,
6869 };