authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-15 21:50:45+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-20 23:36:41+02:00
logd5fdb7315f2f5742c58af547909437401651ffd5
treebd9f8e9f3947e83b0c20230d00f79000c2f8fb19
parent700644d35dd674c7381c4794d566e0e64f45c174

link/elf: port macho symbol extras handling


6 files changed, 42 insertions(+), 65 deletions(-)

src/link/Elf/LinkerDefined.zig+2-2
......@@ -60,10 +60,10 @@ pub fn updateSymtabSize(self: *LinkerDefined, elf_file: *Elf) !void {
6060 if (file_ptr.index() != self.index) continue;
6161 global.flags.output_symtab = true;
6262 if (global.isLocal(elf_file)) {
63 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
63 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
6464 self.output_symtab_ctx.nlocals += 1;
6565 } else {
66 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
66 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nglobals }, elf_file);
6767 self.output_symtab_ctx.nglobals += 1;
6868 }
6969 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
src/link/Elf/Object.zig+3-3
......@@ -853,7 +853,7 @@ pub fn updateSymtabSize(self: *Object, elf_file: *Elf) !void {
853853 else => {},
854854 }
855855 local.flags.output_symtab = true;
856 try local.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
856 try local.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
857857 self.output_symtab_ctx.nlocals += 1;
858858 self.output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;
859859 }
......@@ -865,10 +865,10 @@ pub fn updateSymtabSize(self: *Object, elf_file: *Elf) !void {
865865 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
866866 global.flags.output_symtab = true;
867867 if (global.isLocal(elf_file)) {
868 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
868 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
869869 self.output_symtab_ctx.nlocals += 1;
870870 } else {
871 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
871 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nglobals }, elf_file);
872872 self.output_symtab_ctx.nglobals += 1;
873873 }
874874 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
src/link/Elf/SharedObject.zig+1-1
......@@ -269,7 +269,7 @@ pub fn updateSymtabSize(self: *SharedObject, elf_file: *Elf) !void {
269269 if (file_ptr.index() != self.index) continue;
270270 if (global.isLocal(elf_file)) continue;
271271 global.flags.output_symtab = true;
272 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
272 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nglobals }, elf_file);
273273 self.output_symtab_ctx.nglobals += 1;
274274 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
275275 }
src/link/Elf/Symbol.zig+24-10
......@@ -143,14 +143,6 @@ pub fn outputSymtabIndex(symbol: Symbol, elf_file: *Elf) ?u32 {
143143 return if (symbol.isLocal(elf_file)) idx + symtab_ctx.ilocal else idx + symtab_ctx.iglobal;
144144}
145145
146pub fn setOutputSymtabIndex(symbol: *Symbol, index: u32, elf_file: *Elf) !void {
147 if (symbol.extra(elf_file)) |extras| {
148 var new_extras = extras;
149 new_extras.symtab = index;
150 symbol.setExtra(new_extras, elf_file);
151 } else try symbol.addExtra(.{ .symtab = index }, elf_file);
152}
153
154146pub fn gotAddress(symbol: Symbol, elf_file: *Elf) u64 {
155147 if (!symbol.flags.has_got) return 0;
156148 const extras = symbol.extra(elf_file).?;
......@@ -240,8 +232,30 @@ pub fn dsoAlignment(symbol: Symbol, elf_file: *Elf) !u64 {
240232 @min(alignment, try std.math.powi(u64, 2, @ctz(esym.st_value)));
241233}
242234
243pub fn addExtra(symbol: *Symbol, extras: Extra, elf_file: *Elf) !void {
244 symbol.extra_index = try elf_file.addSymbolExtra(extras);
235const AddExtraOpts = struct {
236 got: ?u32 = null,
237 plt: ?u32 = null,
238 plt_got: ?u32 = null,
239 dynamic: ?u32 = null,
240 symtab: ?u32 = null,
241 copy_rel: ?u32 = null,
242 tlsgd: ?u32 = null,
243 gottp: ?u32 = null,
244 tlsdesc: ?u32 = null,
245 zig_got: ?u32 = null,
246};
247
248pub fn addExtra(symbol: *Symbol, opts: AddExtraOpts, elf_file: *Elf) !void {
249 if (symbol.extra(elf_file) == null) {
250 symbol.extra_index = try elf_file.addSymbolExtra(.{});
251 }
252 var extras = symbol.extra(elf_file).?;
253 inline for (@typeInfo(@TypeOf(opts)).Struct.fields) |field| {
254 if (@field(opts, field.name)) |x| {
255 @field(extras, field.name) = x;
256 }
257 }
258 symbol.setExtra(extras, elf_file);
245259}
246260
247261pub fn extra(symbol: Symbol, elf_file: *Elf) ?Extra {
src/link/Elf/ZigObject.zig+3-3
......@@ -566,7 +566,7 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
566566 else => {},
567567 }
568568 local.flags.output_symtab = true;
569 try local.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
569 try local.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
570570 self.output_symtab_ctx.nlocals += 1;
571571 self.output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;
572572 }
......@@ -578,10 +578,10 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
578578 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
579579 global.flags.output_symtab = true;
580580 if (global.isLocal(elf_file)) {
581 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
581 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
582582 self.output_symtab_ctx.nlocals += 1;
583583 } else {
584 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
584 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nglobals }, elf_file);
585585 self.output_symtab_ctx.nglobals += 1;
586586 }
587587 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
src/link/Elf/synthetic_sections.zig+9-46
......@@ -259,11 +259,7 @@ pub const ZigGotSection = struct {
259259 if (elf_file.isEffectivelyDynLib() or (elf_file.base.isExe() and comp.config.pie)) {
260260 zig_got.flags.needs_rela = true;
261261 }
262 if (symbol.extra(elf_file)) |extra| {
263 var new_extra = extra;
264 new_extra.zig_got = index;
265 symbol.setExtra(new_extra, elf_file);
266 } else try symbol.addExtra(.{ .zig_got = index }, elf_file);
262 try symbol.addExtra(.{ .zig_got = index }, elf_file);
267263 return index;
268264 }
269265
......@@ -499,11 +495,7 @@ pub const GotSection = struct {
499495 {
500496 got.flags.needs_rela = true;
501497 }
502 if (symbol.extra(elf_file)) |extra| {
503 var new_extra = extra;
504 new_extra.got = index;
505 symbol.setExtra(new_extra, elf_file);
506 } else try symbol.addExtra(.{ .got = index }, elf_file);
498 try symbol.addExtra(.{ .got = index }, elf_file);
507499 return index;
508500 }
509501
......@@ -529,11 +521,7 @@ pub const GotSection = struct {
529521 const symbol = elf_file.symbol(sym_index);
530522 symbol.flags.has_tlsgd = true;
531523 if (symbol.flags.import or elf_file.isEffectivelyDynLib()) got.flags.needs_rela = true;
532 if (symbol.extra(elf_file)) |extra| {
533 var new_extra = extra;
534 new_extra.tlsgd = index;
535 symbol.setExtra(new_extra, elf_file);
536 } else try symbol.addExtra(.{ .tlsgd = index }, elf_file);
524 try symbol.addExtra(.{ .tlsgd = index }, elf_file);
537525 }
538526
539527 pub fn addGotTpSymbol(got: *GotSection, sym_index: Symbol.Index, elf_file: *Elf) !void {
......@@ -546,11 +534,7 @@ pub const GotSection = struct {
546534 const symbol = elf_file.symbol(sym_index);
547535 symbol.flags.has_gottp = true;
548536 if (symbol.flags.import or elf_file.isEffectivelyDynLib()) got.flags.needs_rela = true;
549 if (symbol.extra(elf_file)) |extra| {
550 var new_extra = extra;
551 new_extra.gottp = index;
552 symbol.setExtra(new_extra, elf_file);
553 } else try symbol.addExtra(.{ .gottp = index }, elf_file);
537 try symbol.addExtra(.{ .gottp = index }, elf_file);
554538 }
555539
556540 pub fn addTlsDescSymbol(got: *GotSection, sym_index: Symbol.Index, elf_file: *Elf) !void {
......@@ -563,11 +547,7 @@ pub const GotSection = struct {
563547 const symbol = elf_file.symbol(sym_index);
564548 symbol.flags.has_tlsdesc = true;
565549 got.flags.needs_rela = true;
566 if (symbol.extra(elf_file)) |extra| {
567 var new_extra = extra;
568 new_extra.tlsdesc = index;
569 symbol.setExtra(new_extra, elf_file);
570 } else try symbol.addExtra(.{ .tlsdesc = index }, elf_file);
550 try symbol.addExtra(.{ .tlsdesc = index }, elf_file);
571551 }
572552
573553 pub fn size(got: GotSection, elf_file: *Elf) usize {
......@@ -877,11 +857,7 @@ pub const PltSection = struct {
877857 const index = @as(u32, @intCast(plt.symbols.items.len));
878858 const symbol = elf_file.symbol(sym_index);
879859 symbol.flags.has_plt = true;
880 if (symbol.extra(elf_file)) |extra| {
881 var new_extra = extra;
882 new_extra.plt = index;
883 symbol.setExtra(new_extra, elf_file);
884 } else try symbol.addExtra(.{ .plt = index }, elf_file);
860 try symbol.addExtra(.{ .plt = index }, elf_file);
885861 try plt.symbols.append(gpa, sym_index);
886862 }
887863
......@@ -1132,11 +1108,7 @@ pub const PltGotSection = struct {
11321108 const symbol = elf_file.symbol(sym_index);
11331109 symbol.flags.has_plt = true;
11341110 symbol.flags.has_got = true;
1135 if (symbol.extra(elf_file)) |extra| {
1136 var new_extra = extra;
1137 new_extra.plt_got = index;
1138 symbol.setExtra(new_extra, elf_file);
1139 } else try symbol.addExtra(.{ .plt_got = index }, elf_file);
1111 try symbol.addExtra(.{ .plt_got = index }, elf_file);
11401112 try plt_got.symbols.append(gpa, sym_index);
11411113 }
11421114
......@@ -1248,12 +1220,7 @@ pub const CopyRelSection = struct {
12481220 symbol.flags.@"export" = true;
12491221 symbol.flags.has_copy_rel = true;
12501222 symbol.flags.weak = false;
1251
1252 if (symbol.extra(elf_file)) |extra| {
1253 var new_extra = extra;
1254 new_extra.copy_rel = index;
1255 symbol.setExtra(new_extra, elf_file);
1256 } else try symbol.addExtra(.{ .copy_rel = index }, elf_file);
1223 try symbol.addExtra(.{ .copy_rel = index }, elf_file);
12571224 try copy_rel.symbols.append(gpa, sym_index);
12581225
12591226 const shared_object = symbol.file(elf_file).?.shared_object;
......@@ -1335,11 +1302,7 @@ pub const DynsymSection = struct {
13351302 const index = @as(u32, @intCast(dynsym.entries.items.len + 1));
13361303 const sym = elf_file.symbol(sym_index);
13371304 sym.flags.has_dynamic = true;
1338 if (sym.extra(elf_file)) |extra| {
1339 var new_extra = extra;
1340 new_extra.dynamic = index;
1341 sym.setExtra(new_extra, elf_file);
1342 } else try sym.addExtra(.{ .dynamic = index }, elf_file);
1305 try sym.addExtra(.{ .dynamic = index }, elf_file);
13431306 const off = try elf_file.insertDynString(sym.name(elf_file));
13441307 try dynsym.entries.append(gpa, .{ .symbol_index = sym_index, .off = off });
13451308 }