authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-30 14:59:59+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-07 10:21:02+02:00
log518c7908f01cafac9acfbbb708fc49d2c3b5d460
tree1091aff77fc5b0011d72dc257dbdf868b6785261
parentf9f894200891c8af6ce3a3ad222cd0bf1ee15587

elf: always create symbol extra


7 files changed, 31 insertions(+), 44 deletions(-)

src/link/Elf.zig+4-18
......@@ -176,7 +176,6 @@ symtab_section_index: ?u32 = null,
176176/// An array of symbols parsed across all input files.
177177symbols: std.ArrayListUnmanaged(Symbol) = .{},
178178symbols_extra: std.ArrayListUnmanaged(u32) = .{},
179symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{},
180179
181180resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{},
182181
......@@ -343,8 +342,6 @@ pub fn createEmpty(
343342
344343 // Index 0 is always a null symbol.
345344 try self.symbols.append(gpa, .{});
346 // Index 0 is always a null symbol.
347 try self.symbols_extra.append(gpa, 0);
348345 // Append null file at index 0
349346 try self.files.append(gpa, .null);
350347 // Append null byte to string tables
......@@ -462,7 +459,6 @@ pub fn deinit(self: *Elf) void {
462459 self.strtab.deinit(gpa);
463460 self.symbols.deinit(gpa);
464461 self.symbols_extra.deinit(gpa);
465 self.symbols_free_list.deinit(gpa);
466462 self.resolver.deinit(gpa);
467463
468464 for (self.thunks.items) |*th| {
......@@ -5389,17 +5385,8 @@ pub fn symbol(self: *Elf, sym_index: Symbol.Index) *Symbol {
53895385pub fn addSymbol(self: *Elf) !Symbol.Index {
53905386 const gpa = self.base.comp.gpa;
53915387 try self.symbols.ensureUnusedCapacity(gpa, 1);
5392 const index = blk: {
5393 if (self.symbols_free_list.popOrNull()) |index| {
5394 log.debug(" (reusing symbol index {d})", .{index});
5395 break :blk index;
5396 } else {
5397 log.debug(" (allocating symbol index {d})", .{self.symbols.items.len});
5398 const index: Symbol.Index = @intCast(self.symbols.items.len);
5399 _ = self.symbols.addOneAssumeCapacity();
5400 break :blk index;
5401 }
5402 };
5388 const index: Symbol.Index = @intCast(self.symbols.items.len);
5389 _ = self.symbols.addOneAssumeCapacity();
54035390 self.symbols.items[index] = .{};
54045391 return index;
54055392}
......@@ -5423,8 +5410,7 @@ pub fn addSymbolExtraAssumeCapacity(self: *Elf, extra: Symbol.Extra) u32 {
54235410 return index;
54245411}
54255412
5426pub fn symbolExtra(self: *Elf, index: u32) ?Symbol.Extra {
5427 if (index == 0) return null;
5413pub fn symbolExtra(self: *Elf, index: u32) Symbol.Extra {
54285414 const fields = @typeInfo(Symbol.Extra).Struct.fields;
54295415 var i: usize = index;
54305416 var result: Symbol.Extra = undefined;
......@@ -5439,7 +5425,6 @@ pub fn symbolExtra(self: *Elf, index: u32) ?Symbol.Extra {
54395425}
54405426
54415427pub fn setSymbolExtra(self: *Elf, index: u32, extra: Symbol.Extra) void {
5442 assert(index > 0);
54435428 const fields = @typeInfo(Symbol.Extra).Struct.fields;
54445429 inline for (fields, 0..) |field, i| {
54455430 self.symbols_extra.items[index + i] = switch (field.type) {
......@@ -5464,6 +5449,7 @@ pub fn getOrPutGlobal(self: *Elf, name: []const u8) !GetOrPutGlobalResult {
54645449 const global = self.symbol(index);
54655450 global.name_offset = name_off;
54665451 global.flags.global = true;
5452 global.extra_index = try self.addSymbolExtra(.{});
54675453 gop.value_ptr.* = index;
54685454 }
54695455 return .{
src/link/Elf/Atom.zig+3-3
......@@ -845,7 +845,7 @@ fn resolveDynAbsReloc(
845845 if (is_writeable or elf_file.z_nocopyreloc) {
846846 elf_file.addRelaDynAssumeCapacity(.{
847847 .offset = P,
848 .sym = target.extra(elf_file).?.dynamic,
848 .sym = target.extra(elf_file).dynamic,
849849 .type = relocation.encode(.abs, cpu_arch),
850850 .addend = A,
851851 });
......@@ -859,7 +859,7 @@ fn resolveDynAbsReloc(
859859 if (is_writeable) {
860860 elf_file.addRelaDynAssumeCapacity(.{
861861 .offset = P,
862 .sym = target.extra(elf_file).?.dynamic,
862 .sym = target.extra(elf_file).dynamic,
863863 .type = relocation.encode(.abs, cpu_arch),
864864 .addend = A,
865865 });
......@@ -872,7 +872,7 @@ fn resolveDynAbsReloc(
872872 .dynrel => {
873873 elf_file.addRelaDynAssumeCapacity(.{
874874 .offset = P,
875 .sym = target.extra(elf_file).?.dynamic,
875 .sym = target.extra(elf_file).dynamic,
876876 .type = relocation.encode(.abs, cpu_arch),
877877 .addend = A,
878878 });
src/link/Elf/Object.zig+2
......@@ -387,6 +387,7 @@ fn initSymtab(self: *Object, allocator: Allocator, elf_file: *Elf) !void {
387387 sym_ptr.name_offset = sym.st_name;
388388 sym_ptr.esym_index = @as(u32, @intCast(i));
389389 sym_ptr.file_index = self.index;
390 sym_ptr.extra_index = try elf_file.addSymbolExtra(.{});
390391 if (sym.st_shndx != elf.SHN_ABS) {
391392 sym_ptr.ref = .{ .index = self.atoms_indexes.items[sym.st_shndx], .file = self.index };
392393 }
......@@ -865,6 +866,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
865866 .name_offset = try self.addString(gpa, name),
866867 .esym_index = rel.r_sym(),
867868 .file_index = self.index,
869 .extra_index = try elf_file.addSymbolExtra(.{}),
868870 };
869871 sym.ref = .{ .index = res.msub_index, .file = imsec.merge_section_index };
870872 sym.flags.merge_subsection = true;
src/link/Elf/Symbol.zig+12-15
......@@ -159,20 +159,20 @@ pub fn outputSymtabIndex(symbol: Symbol, elf_file: *Elf) ?u32 {
159159 const symtab_ctx = switch (file_ptr) {
160160 inline else => |x| x.output_symtab_ctx,
161161 };
162 const idx = symbol.extra(elf_file).?.symtab;
162 const idx = symbol.extra(elf_file).symtab;
163163 return if (symbol.isLocal(elf_file)) idx + symtab_ctx.ilocal else idx + symtab_ctx.iglobal;
164164}
165165
166166pub fn gotAddress(symbol: Symbol, elf_file: *Elf) i64 {
167167 if (!symbol.flags.has_got) return 0;
168 const extras = symbol.extra(elf_file).?;
168 const extras = symbol.extra(elf_file);
169169 const entry = elf_file.got.entries.items[extras.got];
170170 return entry.address(elf_file);
171171}
172172
173173pub fn pltGotAddress(symbol: Symbol, elf_file: *Elf) i64 {
174174 if (!(symbol.flags.has_plt and symbol.flags.has_got)) return 0;
175 const extras = symbol.extra(elf_file).?;
175 const extras = symbol.extra(elf_file);
176176 const shdr = elf_file.shdrs.items[elf_file.plt_got_section_index.?];
177177 const cpu_arch = elf_file.getTarget().cpu.arch;
178178 return @intCast(shdr.sh_addr + extras.plt_got * PltGotSection.entrySize(cpu_arch));
......@@ -180,7 +180,7 @@ pub fn pltGotAddress(symbol: Symbol, elf_file: *Elf) i64 {
180180
181181pub fn pltAddress(symbol: Symbol, elf_file: *Elf) i64 {
182182 if (!symbol.flags.has_plt) return 0;
183 const extras = symbol.extra(elf_file).?;
183 const extras = symbol.extra(elf_file);
184184 const shdr = elf_file.shdrs.items[elf_file.plt_section_index.?];
185185 const cpu_arch = elf_file.getTarget().cpu.arch;
186186 return @intCast(shdr.sh_addr + extras.plt * PltSection.entrySize(cpu_arch) + PltSection.preambleSize(cpu_arch));
......@@ -188,7 +188,7 @@ pub fn pltAddress(symbol: Symbol, elf_file: *Elf) i64 {
188188
189189pub fn gotPltAddress(symbol: Symbol, elf_file: *Elf) i64 {
190190 if (!symbol.flags.has_plt) return 0;
191 const extras = symbol.extra(elf_file).?;
191 const extras = symbol.extra(elf_file);
192192 const shdr = elf_file.shdrs.items[elf_file.got_plt_section_index.?];
193193 return @intCast(shdr.sh_addr + extras.plt * 8 + GotPltSection.preamble_size);
194194}
......@@ -201,21 +201,21 @@ pub fn copyRelAddress(symbol: Symbol, elf_file: *Elf) i64 {
201201
202202pub fn tlsGdAddress(symbol: Symbol, elf_file: *Elf) i64 {
203203 if (!symbol.flags.has_tlsgd) return 0;
204 const extras = symbol.extra(elf_file).?;
204 const extras = symbol.extra(elf_file);
205205 const entry = elf_file.got.entries.items[extras.tlsgd];
206206 return entry.address(elf_file);
207207}
208208
209209pub fn gotTpAddress(symbol: Symbol, elf_file: *Elf) i64 {
210210 if (!symbol.flags.has_gottp) return 0;
211 const extras = symbol.extra(elf_file).?;
211 const extras = symbol.extra(elf_file);
212212 const entry = elf_file.got.entries.items[extras.gottp];
213213 return entry.address(elf_file);
214214}
215215
216216pub fn tlsDescAddress(symbol: Symbol, elf_file: *Elf) i64 {
217217 if (!symbol.flags.has_tlsdesc) return 0;
218 const extras = symbol.extra(elf_file).?;
218 const extras = symbol.extra(elf_file);
219219 const entry = elf_file.got.entries.items[extras.tlsdesc];
220220 return entry.address(elf_file);
221221}
......@@ -228,14 +228,14 @@ const GetOrCreateZigGotEntryResult = struct {
228228pub fn getOrCreateZigGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *Elf) !GetOrCreateZigGotEntryResult {
229229 assert(!elf_file.base.isRelocatable());
230230 assert(symbol.flags.needs_zig_got);
231 if (symbol.flags.has_zig_got) return .{ .found_existing = true, .index = symbol.extra(elf_file).?.zig_got };
231 if (symbol.flags.has_zig_got) return .{ .found_existing = true, .index = symbol.extra(elf_file).zig_got };
232232 const index = try elf_file.zig_got.addSymbol(symbol_index, elf_file);
233233 return .{ .found_existing = false, .index = index };
234234}
235235
236236pub fn zigGotAddress(symbol: Symbol, elf_file: *Elf) i64 {
237237 if (!symbol.flags.has_zig_got) return 0;
238 const extras = symbol.extra(elf_file).?;
238 const extras = symbol.extra(elf_file);
239239 return elf_file.zig_got.entryAddress(extras.zig_got, elf_file);
240240}
241241
......@@ -266,10 +266,7 @@ const AddExtraOpts = struct {
266266};
267267
268268pub fn addExtra(symbol: *Symbol, opts: AddExtraOpts, elf_file: *Elf) !void {
269 if (symbol.extra(elf_file) == null) {
270 symbol.extra_index = try elf_file.addSymbolExtra(.{});
271 }
272 var extras = symbol.extra(elf_file).?;
269 var extras = symbol.extra(elf_file);
273270 inline for (@typeInfo(@TypeOf(opts)).Struct.fields) |field| {
274271 if (@field(opts, field.name)) |x| {
275272 @field(extras, field.name) = x;
......@@ -278,7 +275,7 @@ pub fn addExtra(symbol: *Symbol, opts: AddExtraOpts, elf_file: *Elf) !void {
278275 symbol.setExtra(extras, elf_file);
279276}
280277
281pub fn extra(symbol: Symbol, elf_file: *Elf) ?Extra {
278pub fn extra(symbol: Symbol, elf_file: *Elf) Extra {
282279 return elf_file.symbolExtra(symbol.extra_index);
283280}
284281
src/link/Elf/ZigObject.zig+3-3
......@@ -92,6 +92,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf) !void {
9292 const symbol_ptr = elf_file.symbol(symbol_index);
9393 symbol_ptr.file_index = self.index;
9494 symbol_ptr.name_offset = name_off;
95 symbol_ptr.extra_index = try elf_file.addSymbolExtra(.{});
9596
9697 const esym_index = try self.addLocalEsym(gpa);
9798 const esym = &self.local_esyms.items(.elf_sym)[esym_index];
......@@ -292,6 +293,7 @@ pub fn newAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index {
292293 const symbol_ptr = elf_file.symbol(symbol_index);
293294 symbol_ptr.file_index = self.index;
294295 symbol_ptr.ref = .{ .index = atom_index, .file = self.index };
296 symbol_ptr.extra_index = try elf_file.addSymbolExtra(.{});
295297
296298 self.local_esyms.items(.shndx)[esym_index] = atom_index;
297299 self.local_esyms.items(.elf_sym)[esym_index].st_shndx = SHN_ATOM;
......@@ -779,11 +781,9 @@ fn freeUnnamedConsts(self: *ZigObject, elf_file: *Elf, decl_index: InternPool.De
779781
780782fn freeDeclMetadata(self: *ZigObject, elf_file: *Elf, sym_index: Symbol.Index) void {
781783 _ = self;
782 const gpa = elf_file.base.comp.gpa;
783784 const sym = elf_file.symbol(sym_index);
784785 sym.atom(elf_file).?.free(elf_file);
785786 log.debug("adding %{d} to local symbols free list", .{sym_index});
786 elf_file.symbols_free_list.append(gpa, sym_index) catch {};
787787 elf_file.symbols.items[sym_index] = .{};
788788 // TODO free GOT entry here
789789}
......@@ -940,7 +940,7 @@ fn updateDeclCode(
940940 if (!elf_file.base.isRelocatable()) {
941941 log.debug(" (writing new offset table entry)", .{});
942942 assert(sym.flags.has_zig_got);
943 const extra = sym.extra(elf_file).?;
943 const extra = sym.extra(elf_file);
944944 try elf_file.zig_got.writeOne(elf_file, extra.zig_got);
945945 }
946946 }
src/link/Elf/file.zig+2
......@@ -71,9 +71,11 @@ pub const File = union(enum) {
7171 for (file.globals()) |global_index| {
7272 const global = elf_file.symbol(global_index);
7373 const name_offset = global.name_offset;
74 const extra_index = global.extra_index;
7475 global.* = .{};
7576 global.name_offset = name_offset;
7677 global.flags.global = true;
78 global.extra_index = extra_index;
7779 }
7880 }
7981
src/link/Elf/synthetic_sections.zig+5-5
......@@ -641,7 +641,7 @@ pub const GotSection = struct {
641641 .tlsld => null,
642642 inline else => elf_file.symbol(entry.symbol_index),
643643 };
644 const extra = if (symbol) |s| s.extra(elf_file).? else null;
644 const extra = if (symbol) |s| s.extra(elf_file) else null;
645645
646646 switch (entry.tag) {
647647 .got => {
......@@ -898,7 +898,7 @@ pub const PltSection = struct {
898898 for (plt.symbols.items) |sym_index| {
899899 const sym = elf_file.symbol(sym_index);
900900 assert(sym.flags.import);
901 const extra = sym.extra(elf_file).?;
901 const extra = sym.extra(elf_file);
902902 const r_offset: u64 = @intCast(sym.gotPltAddress(elf_file));
903903 const r_sym: u64 = extra.dynamic;
904904 const r_type = relocation.encode(.jump_slot, cpu_arch);
......@@ -1267,7 +1267,7 @@ pub const CopyRelSection = struct {
12671267 for (copy_rel.symbols.items) |sym_index| {
12681268 const sym = elf_file.symbol(sym_index);
12691269 assert(sym.flags.import and sym.flags.has_copy_rel);
1270 const extra = sym.extra(elf_file).?;
1270 const extra = sym.extra(elf_file);
12711271 elf_file.addRelaDynAssumeCapacity(.{
12721272 .offset = @intCast(sym.address(.{}, elf_file)),
12731273 .sym = extra.dynamic,
......@@ -1322,7 +1322,7 @@ pub const DynsymSection = struct {
13221322 const rhs_hash = GnuHashSection.hasher(rhs_sym.name(ctx)) % nbuckets;
13231323
13241324 if (lhs_hash == rhs_hash)
1325 return lhs_sym.extra(ctx).?.dynamic < rhs_sym.extra(ctx).?.dynamic;
1325 return lhs_sym.extra(ctx).dynamic < rhs_sym.extra(ctx).dynamic;
13261326 return lhs_hash < rhs_hash;
13271327 }
13281328 };
......@@ -1339,7 +1339,7 @@ pub const DynsymSection = struct {
13391339
13401340 for (dynsym.entries.items, 1..) |entry, index| {
13411341 const sym = elf_file.symbol(entry.symbol_index);
1342 var extra = sym.extra(elf_file).?;
1342 var extra = sym.extra(elf_file);
13431343 extra.dynamic = @as(u32, @intCast(index));
13441344 sym.setExtra(extra, elf_file);
13451345 }