authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-15 20:34:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:20-07:00
log46297087871dec88c2b632d057f1e55b662126df
treef21e26828e7eb230ba46baf08bb7476f2388d745
parent48d5861f9256dfd1c5357dd6f2a023e700de16e4

linker: fix some allocator references


8 files changed, 65 insertions(+), 37 deletions(-)

src/link/Elf/Archive.zig+4-2
......@@ -20,7 +20,8 @@ pub fn deinit(self: *Archive, allocator: Allocator) void {
2020}
2121
2222pub fn parse(self: *Archive, elf_file: *Elf) !void {
23 const gpa = elf_file.base.allocator;
23 const comp = elf_file.base.comp;
24 const gpa = comp.gpa;
2425
2526 var stream = std.io.fixedBufferStream(self.data);
2627 const reader = stream.reader();
......@@ -150,7 +151,8 @@ pub const ArSymtab = struct {
150151 const hdr = setArHdr(.{ .name = .symtab, .size = @intCast(ar.size(.p64)) });
151152 try writer.writeAll(mem.asBytes(&hdr));
152153
153 const gpa = elf_file.base.allocator;
154 const comp = elf_file.base.comp;
155 const gpa = comp.gpa;
154156 var offsets = std.AutoHashMap(File.Index, u64).init(gpa);
155157 defer offsets.deinit();
156158 try offsets.ensureUnusedCapacity(@intCast(elf_file.objects.items.len + 1));
src/link/Elf/LdScript.zig+2-1
......@@ -14,7 +14,8 @@ pub const Error = error{
1414};
1515
1616pub fn parse(scr: *LdScript, data: []const u8, elf_file: *Elf) Error!void {
17 const gpa = elf_file.base.allocator;
17 const comp = elf_file.base.comp;
18 const gpa = comp.gpa;
1819 var tokenizer = Tokenizer{ .source = data };
1920 var tokens = std.ArrayList(Token).init(gpa);
2021 defer tokens.deinit();
src/link/Elf/LinkerDefined.zig+2-1
......@@ -12,7 +12,8 @@ pub fn deinit(self: *LinkerDefined, allocator: Allocator) void {
1212}
1313
1414pub fn addGlobal(self: *LinkerDefined, name: [:0]const u8, elf_file: *Elf) !u32 {
15 const gpa = elf_file.base.allocator;
15 const comp = elf_file.base.comp;
16 const gpa = comp.gpa;
1617 try self.symtab.ensureUnusedCapacity(gpa, 1);
1718 try self.symbols.ensureUnusedCapacity(gpa, 1);
1819 const name_off = @as(u32, @intCast(self.strtab.items.len));
src/link/Elf/Object.zig+22-11
......@@ -66,7 +66,8 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
6666
6767 if (self.header.?.e_shnum == 0) return;
6868
69 const gpa = elf_file.base.allocator;
69 const comp = elf_file.base.comp;
70 const gpa = comp.gpa;
7071
7172 if (self.data.len < self.header.?.e_shoff or
7273 self.data.len < self.header.?.e_shoff + @as(u64, @intCast(self.header.?.e_shnum)) * @sizeOf(elf.Elf64_Shdr))
......@@ -149,8 +150,10 @@ pub fn init(self: *Object, elf_file: *Elf) !void {
149150}
150151
151152fn initAtoms(self: *Object, elf_file: *Elf) !void {
153 const comp = elf_file.base.comp;
154 const gpa = comp.gpa;
152155 const shdrs = self.shdrs.items;
153 try self.atoms.resize(elf_file.base.allocator, shdrs.len);
156 try self.atoms.resize(gpa, shdrs.len);
154157 @memset(self.atoms.items, 0);
155158
156159 for (shdrs, 0..) |shdr, i| {
......@@ -185,7 +188,6 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void {
185188 continue;
186189 }
187190
188 const gpa = elf_file.base.allocator;
189191 const gop = try elf_file.getOrCreateComdatGroupOwner(group_signature);
190192 const comdat_group_index = try elf_file.addComdatGroup();
191193 const comdat_group = elf_file.comdatGroup(comdat_group_index);
......@@ -308,7 +310,8 @@ fn skipShdr(self: *Object, index: u16, elf_file: *Elf) bool {
308310}
309311
310312fn initSymtab(self: *Object, elf_file: *Elf) !void {
311 const gpa = elf_file.base.allocator;
313 const comp = elf_file.base.comp;
314 const gpa = comp.gpa;
312315 const first_global = self.first_global orelse self.symtab.items.len;
313316
314317 try self.symbols.ensureTotalCapacityPrecise(gpa, self.symtab.items.len);
......@@ -340,7 +343,8 @@ fn parseEhFrame(self: *Object, shndx: u16, elf_file: *Elf) !void {
340343 return;
341344 };
342345
343 const gpa = elf_file.base.allocator;
346 const comp = elf_file.base.comp;
347 const gpa = comp.gpa;
344348 const raw = self.shdrContents(shndx);
345349 const relocs = self.getRelocs(relocs_shndx);
346350 const fdes_start = self.fdes.items.len;
......@@ -440,6 +444,8 @@ fn filterRelocs(
440444}
441445
442446pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void {
447 const comp = elf_file.base.comp;
448 const gpa = comp.gpa;
443449 for (self.atoms.items) |atom_index| {
444450 const atom = elf_file.atom(atom_index) orelse continue;
445451 if (!atom.flags.alive) continue;
......@@ -450,7 +456,7 @@ pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void {
450456 // TODO ideally, we don't have to decompress at this stage (should already be done)
451457 // and we just fetch the code slice.
452458 const code = try self.codeDecompressAlloc(elf_file, atom_index);
453 defer elf_file.base.allocator.free(code);
459 defer gpa.free(code);
454460 try atom.scanRelocs(elf_file, code, undefs);
455461 } else try atom.scanRelocs(elf_file, null, undefs);
456462 }
......@@ -623,7 +629,8 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {
623629 continue;
624630 }
625631
626 const gpa = elf_file.base.allocator;
632 const comp = elf_file.base.comp;
633 const gpa = comp.gpa;
627634
628635 const atom_index = try elf_file.addAtom();
629636 try self.atoms.append(gpa, atom_index);
......@@ -682,7 +689,8 @@ pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void {
682689 const shdr = atom.inputShdr(elf_file);
683690 atom.output_section_index = self.initOutputSection(elf_file, shdr) catch unreachable;
684691
685 const gpa = elf_file.base.allocator;
692 const comp = elf_file.base.comp;
693 const gpa = comp.gpa;
686694 const gop = try elf_file.output_sections.getOrPut(gpa, atom.output_section_index);
687695 if (!gop.found_existing) gop.value_ptr.* = .{};
688696 try gop.value_ptr.append(gpa, atom_index);
......@@ -742,7 +750,8 @@ pub fn addAtomsToRelaSections(self: Object, elf_file: *Elf) !void {
742750 shdr.sh_info = atom.outputShndx().?;
743751 shdr.sh_link = elf_file.symtab_section_index.?;
744752
745 const gpa = elf_file.base.allocator;
753 const comp = elf_file.base.comp;
754 const gpa = comp.gpa;
746755 const gop = try elf_file.output_rela_sections.getOrPut(gpa, atom.outputShndx().?);
747756 if (!gop.found_existing) gop.value_ptr.* = .{ .shndx = shndx };
748757 try gop.value_ptr.atom_list.append(gpa, atom_index);
......@@ -750,7 +759,8 @@ pub fn addAtomsToRelaSections(self: Object, elf_file: *Elf) !void {
750759}
751760
752761pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, elf_file: *Elf) !void {
753 const gpa = elf_file.base.allocator;
762 const comp = elf_file.base.comp;
763 const gpa = comp.gpa;
754764 const start = self.first_global orelse self.symtab.items.len;
755765
756766 try ar_symtab.symtab.ensureUnusedCapacity(gpa, self.symtab.items.len - start);
......@@ -857,7 +867,8 @@ pub fn shdrContents(self: Object, index: u32) []const u8 {
857867/// Returns atom's code and optionally uncompresses data if required (for compressed sections).
858868/// Caller owns the memory.
859869pub fn codeDecompressAlloc(self: Object, elf_file: *Elf, atom_index: Atom.Index) ![]u8 {
860 const gpa = elf_file.base.allocator;
870 const comp = elf_file.base.comp;
871 const gpa = comp.gpa;
861872 const atom_ptr = elf_file.atom(atom_index).?;
862873 assert(atom_ptr.file_index == self.index);
863874 const data = self.shdrContents(atom_ptr.input_section_index);
src/link/Elf/SharedObject.zig+8-4
......@@ -47,7 +47,8 @@ pub fn deinit(self: *SharedObject, allocator: Allocator) void {
4747}
4848
4949pub fn parse(self: *SharedObject, elf_file: *Elf) !void {
50 const gpa = elf_file.base.allocator;
50 const comp = elf_file.base.comp;
51 const gpa = comp.gpa;
5152 var stream = std.io.fixedBufferStream(self.data);
5253 const reader = stream.reader();
5354
......@@ -101,7 +102,8 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void {
101102}
102103
103104fn parseVersions(self: *SharedObject, elf_file: *Elf) !void {
104 const gpa = elf_file.base.allocator;
105 const comp = elf_file.base.comp;
106 const gpa = comp.gpa;
105107 const symtab = self.getSymtabRaw();
106108
107109 try self.verstrings.resize(gpa, 2);
......@@ -146,7 +148,8 @@ fn parseVersions(self: *SharedObject, elf_file: *Elf) !void {
146148}
147149
148150pub fn init(self: *SharedObject, elf_file: *Elf) !void {
149 const gpa = elf_file.base.allocator;
151 const comp = elf_file.base.comp;
152 const gpa = comp.gpa;
150153 const symtab = self.getSymtabRaw();
151154 const strtab = self.getStrtabRaw();
152155
......@@ -295,7 +298,8 @@ pub fn initSymbolAliases(self: *SharedObject, elf_file: *Elf) !void {
295298 }
296299 };
297300
298 const gpa = elf_file.base.allocator;
301 const comp = elf_file.base.comp;
302 const gpa = comp.gpa;
299303 var aliases = std.ArrayList(Symbol.Index).init(gpa);
300304 defer aliases.deinit();
301305 try aliases.ensureTotalCapacityPrecise(self.globals().len);
src/link/Elf/eh_frame.zig+12-4
......@@ -233,9 +233,12 @@ pub const Iterator = struct {
233233};
234234
235235pub fn calcEhFrameSize(elf_file: *Elf) !usize {
236 const comp = elf_file.base.comp;
237 const gpa = comp.gpa;
238
236239 var offset: usize = 0;
237240
238 var cies = std.ArrayList(Cie).init(elf_file.base.allocator);
241 var cies = std.ArrayList(Cie).init(gpa);
239242 defer cies.deinit();
240243
241244 for (elf_file.objects.items) |index| {
......@@ -327,7 +330,8 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file:
327330}
328331
329332pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
330 const gpa = elf_file.base.allocator;
333 const comp = elf_file.base.comp;
334 const gpa = comp.gpa;
331335
332336 relocs_log.debug("{x}: .eh_frame", .{elf_file.shdrs.items[elf_file.eh_frame_section_index.?].sh_addr});
333337
......@@ -378,7 +382,8 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
378382}
379383
380384pub fn writeEhFrameObject(elf_file: *Elf, writer: anytype) !void {
381 const gpa = elf_file.base.allocator;
385 const comp = elf_file.base.comp;
386 const gpa = comp.gpa;
382387
383388 for (elf_file.objects.items) |index| {
384389 const object = elf_file.file(index).?.object;
......@@ -467,6 +472,9 @@ pub fn writeEhFrameRelocs(elf_file: *Elf, writer: anytype) !void {
467472}
468473
469474pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
475 const comp = elf_file.base.comp;
476 const gpa = comp.gpa;
477
470478 try writer.writeByte(1); // version
471479 try writer.writeByte(EH_PE.pcrel | EH_PE.sdata4);
472480 try writer.writeByte(EH_PE.udata4);
......@@ -495,7 +503,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
495503 }
496504 };
497505
498 var entries = std.ArrayList(Entry).init(elf_file.base.allocator);
506 var entries = std.ArrayList(Entry).init(gpa);
499507 defer entries.deinit();
500508 try entries.ensureTotalCapacityPrecise(num_fdes);
501509
src/link/Elf/gc.zig+2-1
......@@ -1,5 +1,6 @@
11pub fn gcAtoms(elf_file: *Elf) !void {
2 const gpa = elf_file.base.allocator;
2 const comp = elf_file.base.comp;
3 const gpa = comp.gpa;
34 const num_files = elf_file.objects.items.len + @intFromBool(elf_file.zig_object_index != null);
45 var files = try std.ArrayList(File.Index).initCapacity(gpa, num_files);
56 defer files.deinit();
src/link/Elf/synthetic_sections.zig+13-13
......@@ -57,7 +57,7 @@ pub const DynamicSection = struct {
5757 if (elf_file.z_now) {
5858 flags_1 |= elf.DF_1_NOW;
5959 }
60 if (elf_file.isExe() and comp.config.pie) {
60 if (elf_file.base.isExe() and comp.config.pie) {
6161 flags_1 |= elf.DF_1_PIE;
6262 }
6363 // if (elf_file.z_nodlopen) {
......@@ -89,7 +89,7 @@ pub const DynamicSection = struct {
8989 if (elf_file.verneed_section_index != null) nentries += 2; // VERNEED
9090 if (dt.getFlags(elf_file) != null) nentries += 1; // FLAGS
9191 if (dt.getFlags1(elf_file) != null) nentries += 1; // FLAGS_1
92 if (!elf_file.isDynLib()) nentries += 1; // DEBUG
92 if (!elf_file.base.isDynLib()) nentries += 1; // DEBUG
9393 nentries += 1; // NULL
9494 return nentries * @sizeOf(elf.Elf64_Dyn);
9595 }
......@@ -216,7 +216,7 @@ pub const DynamicSection = struct {
216216 }
217217
218218 // DEBUG
219 if (!elf_file.isDynLib()) try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_DEBUG, .d_val = 0 });
219 if (!elf_file.base.isDynLib()) try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_DEBUG, .d_val = 0 });
220220
221221 // NULL
222222 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_NULL, .d_val = 0 });
......@@ -256,7 +256,7 @@ pub const ZigGotSection = struct {
256256 entry.* = sym_index;
257257 const symbol = elf_file.symbol(sym_index);
258258 symbol.flags.has_zig_got = true;
259 if (elf_file.isDynLib() or (elf_file.isExe() and comp.config.pie)) {
259 if (elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) {
260260 zig_got.flags.needs_rela = true;
261261 }
262262 if (symbol.extra(elf_file)) |extra| {
......@@ -494,7 +494,7 @@ pub const GotSection = struct {
494494 const symbol = elf_file.symbol(sym_index);
495495 symbol.flags.has_got = true;
496496 if (symbol.flags.import or symbol.isIFunc(elf_file) or
497 ((elf_file.isDynLib() or (elf_file.isExe() and comp.config.pie)) and !symbol.isAbs(elf_file)))
497 ((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and !symbol.isAbs(elf_file)))
498498 {
499499 got.flags.needs_rela = true;
500500 }
......@@ -527,7 +527,7 @@ pub const GotSection = struct {
527527 entry.symbol_index = sym_index;
528528 const symbol = elf_file.symbol(sym_index);
529529 symbol.flags.has_tlsgd = true;
530 if (symbol.flags.import or elf_file.isDynLib()) got.flags.needs_rela = true;
530 if (symbol.flags.import or elf_file.base.isDynLib()) got.flags.needs_rela = true;
531531 if (symbol.extra(elf_file)) |extra| {
532532 var new_extra = extra;
533533 new_extra.tlsgd = index;
......@@ -544,7 +544,7 @@ pub const GotSection = struct {
544544 entry.symbol_index = sym_index;
545545 const symbol = elf_file.symbol(sym_index);
546546 symbol.flags.has_gottp = true;
547 if (symbol.flags.import or elf_file.isDynLib()) got.flags.needs_rela = true;
547 if (symbol.flags.import or elf_file.base.isDynLib()) got.flags.needs_rela = true;
548548 if (symbol.extra(elf_file)) |extra| {
549549 var new_extra = extra;
550550 new_extra.gottp = index;
......@@ -579,7 +579,7 @@ pub const GotSection = struct {
579579
580580 pub fn write(got: GotSection, elf_file: *Elf, writer: anytype) !void {
581581 const comp = elf_file.base.comp;
582 const is_dyn_lib = elf_file.isDynLib();
582 const is_dyn_lib = elf_file.base.isDynLib();
583583 const apply_relocs = true; // TODO add user option for this
584584
585585 for (got.entries.items) |entry| {
......@@ -594,7 +594,7 @@ pub const GotSection = struct {
594594 if (symbol.?.flags.import) break :blk 0;
595595 if (symbol.?.isIFunc(elf_file))
596596 break :blk if (apply_relocs) value else 0;
597 if ((elf_file.isDynLib() or (elf_file.isExe() and comp.config.pie)) and
597 if ((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
598598 !symbol.?.isAbs(elf_file))
599599 {
600600 break :blk if (apply_relocs) value else 0;
......@@ -643,7 +643,7 @@ pub const GotSection = struct {
643643 pub fn addRela(got: GotSection, elf_file: *Elf) !void {
644644 const comp = elf_file.base.comp;
645645 const gpa = comp.gpa;
646 const is_dyn_lib = elf_file.isDynLib();
646 const is_dyn_lib = elf_file.base.isDynLib();
647647 try elf_file.rela_dyn.ensureUnusedCapacity(gpa, got.numRela(elf_file));
648648
649649 for (got.entries.items) |entry| {
......@@ -672,7 +672,7 @@ pub const GotSection = struct {
672672 });
673673 continue;
674674 }
675 if ((elf_file.isDynLib() or (elf_file.isExe() and comp.config.pie)) and
675 if ((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
676676 !symbol.?.isAbs(elf_file))
677677 {
678678 elf_file.addRelaDynAssumeCapacity(.{
......@@ -746,7 +746,7 @@ pub const GotSection = struct {
746746
747747 pub fn numRela(got: GotSection, elf_file: *Elf) usize {
748748 const comp = elf_file.base.comp;
749 const is_dyn_lib = elf_file.isDynLib();
749 const is_dyn_lib = elf_file.base.isDynLib();
750750 var num: usize = 0;
751751 for (got.entries.items) |entry| {
752752 const symbol = switch (entry.tag) {
......@@ -755,7 +755,7 @@ pub const GotSection = struct {
755755 };
756756 switch (entry.tag) {
757757 .got => if (symbol.?.flags.import or symbol.?.isIFunc(elf_file) or
758 ((elf_file.isDynLib() or (elf_file.isExe() and comp.config.pie)) and
758 ((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
759759 !symbol.?.isAbs(elf_file)))
760760 {
761761 num += 1;