| author | |
| committer | |
| log | 46297087871dec88c2b632d057f1e55b662126df |
| tree | f21e26828e7eb230ba46baf08bb7476f2388d745 |
| parent | 48d5861f9256dfd1c5357dd6f2a023e700de16e4 |
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 { | ... | @@ -20,7 +20,8 @@ pub fn deinit(self: *Archive, allocator: Allocator) void { |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | pub fn parse(self: *Archive, elf_file: *Elf) !void { | 22 | pub 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; | ||
| 24 | 25 | ||
| 25 | var stream = std.io.fixedBufferStream(self.data); | 26 | var stream = std.io.fixedBufferStream(self.data); |
| 26 | const reader = stream.reader(); | 27 | const reader = stream.reader(); |
| ... | @@ -150,7 +151,8 @@ pub const ArSymtab = struct { | ... | @@ -150,7 +151,8 @@ pub const ArSymtab = struct { |
| 150 | const hdr = setArHdr(.{ .name = .symtab, .size = @intCast(ar.size(.p64)) }); | 151 | const hdr = setArHdr(.{ .name = .symtab, .size = @intCast(ar.size(.p64)) }); |
| 151 | try writer.writeAll(mem.asBytes(&hdr)); | 152 | try writer.writeAll(mem.asBytes(&hdr)); |
| 152 | 153 | ||
| 153 | const gpa = elf_file.base.allocator; | 154 | const comp = elf_file.base.comp; |
| 155 | const gpa = comp.gpa; | ||
| 154 | var offsets = std.AutoHashMap(File.Index, u64).init(gpa); | 156 | var offsets = std.AutoHashMap(File.Index, u64).init(gpa); |
| 155 | defer offsets.deinit(); | 157 | defer offsets.deinit(); |
| 156 | try offsets.ensureUnusedCapacity(@intCast(elf_file.objects.items.len + 1)); | 158 | 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{ | ... | @@ -14,7 +14,8 @@ pub const Error = error{ |
| 14 | }; | 14 | }; |
| 15 | 15 | ||
| 16 | pub fn parse(scr: *LdScript, data: []const u8, elf_file: *Elf) Error!void { | 16 | pub 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; | ||
| 18 | var tokenizer = Tokenizer{ .source = data }; | 19 | var tokenizer = Tokenizer{ .source = data }; |
| 19 | var tokens = std.ArrayList(Token).init(gpa); | 20 | var tokens = std.ArrayList(Token).init(gpa); |
| 20 | defer tokens.deinit(); | 21 | defer tokens.deinit(); |
src/link/Elf/LinkerDefined.zig+2-1| ... | @@ -12,7 +12,8 @@ pub fn deinit(self: *LinkerDefined, allocator: Allocator) void { | ... | @@ -12,7 +12,8 @@ pub fn deinit(self: *LinkerDefined, allocator: Allocator) void { |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | pub fn addGlobal(self: *LinkerDefined, name: [:0]const u8, elf_file: *Elf) !u32 { | 14 | pub 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; | ||
| 16 | try self.symtab.ensureUnusedCapacity(gpa, 1); | 17 | try self.symtab.ensureUnusedCapacity(gpa, 1); |
| 17 | try self.symbols.ensureUnusedCapacity(gpa, 1); | 18 | try self.symbols.ensureUnusedCapacity(gpa, 1); |
| 18 | const name_off = @as(u32, @intCast(self.strtab.items.len)); | 19 | 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 { | ... | @@ -66,7 +66,8 @@ pub fn parse(self: *Object, elf_file: *Elf) !void { |
| 66 | 66 | ||
| 67 | if (self.header.?.e_shnum == 0) return; | 67 | if (self.header.?.e_shnum == 0) return; |
| 68 | 68 | ||
| 69 | const gpa = elf_file.base.allocator; | 69 | const comp = elf_file.base.comp; |
| 70 | const gpa = comp.gpa; | ||
| 70 | 71 | ||
| 71 | if (self.data.len < self.header.?.e_shoff or | 72 | if (self.data.len < self.header.?.e_shoff or |
| 72 | self.data.len < self.header.?.e_shoff + @as(u64, @intCast(self.header.?.e_shnum)) * @sizeOf(elf.Elf64_Shdr)) | 73 | 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 { | ... | @@ -149,8 +150,10 @@ pub fn init(self: *Object, elf_file: *Elf) !void { |
| 149 | } | 150 | } |
| 150 | 151 | ||
| 151 | fn initAtoms(self: *Object, elf_file: *Elf) !void { | 152 | fn initAtoms(self: *Object, elf_file: *Elf) !void { |
| 153 | const comp = elf_file.base.comp; | ||
| 154 | const gpa = comp.gpa; | ||
| 152 | const shdrs = self.shdrs.items; | 155 | const shdrs = self.shdrs.items; |
| 153 | try self.atoms.resize(elf_file.base.allocator, shdrs.len); | 156 | try self.atoms.resize(gpa, shdrs.len); |
| 154 | @memset(self.atoms.items, 0); | 157 | @memset(self.atoms.items, 0); |
| 155 | 158 | ||
| 156 | for (shdrs, 0..) |shdr, i| { | 159 | for (shdrs, 0..) |shdr, i| { |
| ... | @@ -185,7 +188,6 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void { | ... | @@ -185,7 +188,6 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void { |
| 185 | continue; | 188 | continue; |
| 186 | } | 189 | } |
| 187 | 190 | ||
| 188 | const gpa = elf_file.base.allocator; | ||
| 189 | const gop = try elf_file.getOrCreateComdatGroupOwner(group_signature); | 191 | const gop = try elf_file.getOrCreateComdatGroupOwner(group_signature); |
| 190 | const comdat_group_index = try elf_file.addComdatGroup(); | 192 | const comdat_group_index = try elf_file.addComdatGroup(); |
| 191 | const comdat_group = elf_file.comdatGroup(comdat_group_index); | 193 | const comdat_group = elf_file.comdatGroup(comdat_group_index); |
| ... | @@ -308,7 +310,8 @@ fn skipShdr(self: *Object, index: u16, elf_file: *Elf) bool { | ... | @@ -308,7 +310,8 @@ fn skipShdr(self: *Object, index: u16, elf_file: *Elf) bool { |
| 308 | } | 310 | } |
| 309 | 311 | ||
| 310 | fn initSymtab(self: *Object, elf_file: *Elf) !void { | 312 | fn 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; | ||
| 312 | const first_global = self.first_global orelse self.symtab.items.len; | 315 | const first_global = self.first_global orelse self.symtab.items.len; |
| 313 | 316 | ||
| 314 | try self.symbols.ensureTotalCapacityPrecise(gpa, self.symtab.items.len); | 317 | try self.symbols.ensureTotalCapacityPrecise(gpa, self.symtab.items.len); |
| ... | @@ -340,7 +343,8 @@ fn parseEhFrame(self: *Object, shndx: u16, elf_file: *Elf) !void { | ... | @@ -340,7 +343,8 @@ fn parseEhFrame(self: *Object, shndx: u16, elf_file: *Elf) !void { |
| 340 | return; | 343 | return; |
| 341 | }; | 344 | }; |
| 342 | 345 | ||
| 343 | const gpa = elf_file.base.allocator; | 346 | const comp = elf_file.base.comp; |
| 347 | const gpa = comp.gpa; | ||
| 344 | const raw = self.shdrContents(shndx); | 348 | const raw = self.shdrContents(shndx); |
| 345 | const relocs = self.getRelocs(relocs_shndx); | 349 | const relocs = self.getRelocs(relocs_shndx); |
| 346 | const fdes_start = self.fdes.items.len; | 350 | const fdes_start = self.fdes.items.len; |
| ... | @@ -440,6 +444,8 @@ fn filterRelocs( | ... | @@ -440,6 +444,8 @@ fn filterRelocs( |
| 440 | } | 444 | } |
| 441 | 445 | ||
| 442 | pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void { | 446 | pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void { |
| 447 | const comp = elf_file.base.comp; | ||
| 448 | const gpa = comp.gpa; | ||
| 443 | for (self.atoms.items) |atom_index| { | 449 | for (self.atoms.items) |atom_index| { |
| 444 | const atom = elf_file.atom(atom_index) orelse continue; | 450 | const atom = elf_file.atom(atom_index) orelse continue; |
| 445 | if (!atom.flags.alive) continue; | 451 | if (!atom.flags.alive) continue; |
| ... | @@ -450,7 +456,7 @@ pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void { | ... | @@ -450,7 +456,7 @@ pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void { |
| 450 | // TODO ideally, we don't have to decompress at this stage (should already be done) | 456 | // TODO ideally, we don't have to decompress at this stage (should already be done) |
| 451 | // and we just fetch the code slice. | 457 | // and we just fetch the code slice. |
| 452 | const code = try self.codeDecompressAlloc(elf_file, atom_index); | 458 | const code = try self.codeDecompressAlloc(elf_file, atom_index); |
| 453 | defer elf_file.base.allocator.free(code); | 459 | defer gpa.free(code); |
| 454 | try atom.scanRelocs(elf_file, code, undefs); | 460 | try atom.scanRelocs(elf_file, code, undefs); |
| 455 | } else try atom.scanRelocs(elf_file, null, undefs); | 461 | } else try atom.scanRelocs(elf_file, null, undefs); |
| 456 | } | 462 | } |
| ... | @@ -623,7 +629,8 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void { | ... | @@ -623,7 +629,8 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void { |
| 623 | continue; | 629 | continue; |
| 624 | } | 630 | } |
| 625 | 631 | ||
| 626 | const gpa = elf_file.base.allocator; | 632 | const comp = elf_file.base.comp; |
| 633 | const gpa = comp.gpa; | ||
| 627 | 634 | ||
| 628 | const atom_index = try elf_file.addAtom(); | 635 | const atom_index = try elf_file.addAtom(); |
| 629 | try self.atoms.append(gpa, atom_index); | 636 | try self.atoms.append(gpa, atom_index); |
| ... | @@ -682,7 +689,8 @@ pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void { | ... | @@ -682,7 +689,8 @@ pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void { |
| 682 | const shdr = atom.inputShdr(elf_file); | 689 | const shdr = atom.inputShdr(elf_file); |
| 683 | atom.output_section_index = self.initOutputSection(elf_file, shdr) catch unreachable; | 690 | atom.output_section_index = self.initOutputSection(elf_file, shdr) catch unreachable; |
| 684 | 691 | ||
| 685 | const gpa = elf_file.base.allocator; | 692 | const comp = elf_file.base.comp; |
| 693 | const gpa = comp.gpa; | ||
| 686 | const gop = try elf_file.output_sections.getOrPut(gpa, atom.output_section_index); | 694 | const gop = try elf_file.output_sections.getOrPut(gpa, atom.output_section_index); |
| 687 | if (!gop.found_existing) gop.value_ptr.* = .{}; | 695 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 688 | try gop.value_ptr.append(gpa, atom_index); | 696 | try gop.value_ptr.append(gpa, atom_index); |
| ... | @@ -742,7 +750,8 @@ pub fn addAtomsToRelaSections(self: Object, elf_file: *Elf) !void { | ... | @@ -742,7 +750,8 @@ pub fn addAtomsToRelaSections(self: Object, elf_file: *Elf) !void { |
| 742 | shdr.sh_info = atom.outputShndx().?; | 750 | shdr.sh_info = atom.outputShndx().?; |
| 743 | shdr.sh_link = elf_file.symtab_section_index.?; | 751 | shdr.sh_link = elf_file.symtab_section_index.?; |
| 744 | 752 | ||
| 745 | const gpa = elf_file.base.allocator; | 753 | const comp = elf_file.base.comp; |
| 754 | const gpa = comp.gpa; | ||
| 746 | const gop = try elf_file.output_rela_sections.getOrPut(gpa, atom.outputShndx().?); | 755 | const gop = try elf_file.output_rela_sections.getOrPut(gpa, atom.outputShndx().?); |
| 747 | if (!gop.found_existing) gop.value_ptr.* = .{ .shndx = shndx }; | 756 | if (!gop.found_existing) gop.value_ptr.* = .{ .shndx = shndx }; |
| 748 | try gop.value_ptr.atom_list.append(gpa, atom_index); | 757 | try gop.value_ptr.atom_list.append(gpa, atom_index); |
| ... | @@ -750,7 +759,8 @@ pub fn addAtomsToRelaSections(self: Object, elf_file: *Elf) !void { | ... | @@ -750,7 +759,8 @@ pub fn addAtomsToRelaSections(self: Object, elf_file: *Elf) !void { |
| 750 | } | 759 | } |
| 751 | 760 | ||
| 752 | pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, elf_file: *Elf) !void { | 761 | pub 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; | ||
| 754 | const start = self.first_global orelse self.symtab.items.len; | 764 | const start = self.first_global orelse self.symtab.items.len; |
| 755 | 765 | ||
| 756 | try ar_symtab.symtab.ensureUnusedCapacity(gpa, self.symtab.items.len - start); | 766 | try ar_symtab.symtab.ensureUnusedCapacity(gpa, self.symtab.items.len - start); |
| ... | @@ -857,7 +867,8 @@ pub fn shdrContents(self: Object, index: u32) []const u8 { | ... | @@ -857,7 +867,8 @@ pub fn shdrContents(self: Object, index: u32) []const u8 { |
| 857 | /// Returns atom's code and optionally uncompresses data if required (for compressed sections). | 867 | /// Returns atom's code and optionally uncompresses data if required (for compressed sections). |
| 858 | /// Caller owns the memory. | 868 | /// Caller owns the memory. |
| 859 | pub fn codeDecompressAlloc(self: Object, elf_file: *Elf, atom_index: Atom.Index) ![]u8 { | 869 | pub 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; | ||
| 861 | const atom_ptr = elf_file.atom(atom_index).?; | 872 | const atom_ptr = elf_file.atom(atom_index).?; |
| 862 | assert(atom_ptr.file_index == self.index); | 873 | assert(atom_ptr.file_index == self.index); |
| 863 | const data = self.shdrContents(atom_ptr.input_section_index); | 874 | 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 { | ... | @@ -47,7 +47,8 @@ pub fn deinit(self: *SharedObject, allocator: Allocator) void { |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | pub fn parse(self: *SharedObject, elf_file: *Elf) !void { | 49 | pub 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; | ||
| 51 | var stream = std.io.fixedBufferStream(self.data); | 52 | var stream = std.io.fixedBufferStream(self.data); |
| 52 | const reader = stream.reader(); | 53 | const reader = stream.reader(); |
| 53 | 54 | ||
| ... | @@ -101,7 +102,8 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void { | ... | @@ -101,7 +102,8 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void { |
| 101 | } | 102 | } |
| 102 | 103 | ||
| 103 | fn parseVersions(self: *SharedObject, elf_file: *Elf) !void { | 104 | fn 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; | ||
| 105 | const symtab = self.getSymtabRaw(); | 107 | const symtab = self.getSymtabRaw(); |
| 106 | 108 | ||
| 107 | try self.verstrings.resize(gpa, 2); | 109 | try self.verstrings.resize(gpa, 2); |
| ... | @@ -146,7 +148,8 @@ fn parseVersions(self: *SharedObject, elf_file: *Elf) !void { | ... | @@ -146,7 +148,8 @@ fn parseVersions(self: *SharedObject, elf_file: *Elf) !void { |
| 146 | } | 148 | } |
| 147 | 149 | ||
| 148 | pub fn init(self: *SharedObject, elf_file: *Elf) !void { | 150 | pub 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; | ||
| 150 | const symtab = self.getSymtabRaw(); | 153 | const symtab = self.getSymtabRaw(); |
| 151 | const strtab = self.getStrtabRaw(); | 154 | const strtab = self.getStrtabRaw(); |
| 152 | 155 | ||
| ... | @@ -295,7 +298,8 @@ pub fn initSymbolAliases(self: *SharedObject, elf_file: *Elf) !void { | ... | @@ -295,7 +298,8 @@ pub fn initSymbolAliases(self: *SharedObject, elf_file: *Elf) !void { |
| 295 | } | 298 | } |
| 296 | }; | 299 | }; |
| 297 | 300 | ||
| 298 | const gpa = elf_file.base.allocator; | 301 | const comp = elf_file.base.comp; |
| 302 | const gpa = comp.gpa; | ||
| 299 | var aliases = std.ArrayList(Symbol.Index).init(gpa); | 303 | var aliases = std.ArrayList(Symbol.Index).init(gpa); |
| 300 | defer aliases.deinit(); | 304 | defer aliases.deinit(); |
| 301 | try aliases.ensureTotalCapacityPrecise(self.globals().len); | 305 | try aliases.ensureTotalCapacityPrecise(self.globals().len); |
src/link/Elf/eh_frame.zig+12-4| ... | @@ -233,9 +233,12 @@ pub const Iterator = struct { | ... | @@ -233,9 +233,12 @@ pub const Iterator = struct { |
| 233 | }; | 233 | }; |
| 234 | 234 | ||
| 235 | pub fn calcEhFrameSize(elf_file: *Elf) !usize { | 235 | pub fn calcEhFrameSize(elf_file: *Elf) !usize { |
| 236 | const comp = elf_file.base.comp; | ||
| 237 | const gpa = comp.gpa; | ||
| 238 | |||
| 236 | var offset: usize = 0; | 239 | var offset: usize = 0; |
| 237 | 240 | ||
| 238 | var cies = std.ArrayList(Cie).init(elf_file.base.allocator); | 241 | var cies = std.ArrayList(Cie).init(gpa); |
| 239 | defer cies.deinit(); | 242 | defer cies.deinit(); |
| 240 | 243 | ||
| 241 | for (elf_file.objects.items) |index| { | 244 | for (elf_file.objects.items) |index| { |
| ... | @@ -327,7 +330,8 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file: | ... | @@ -327,7 +330,8 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file: |
| 327 | } | 330 | } |
| 328 | 331 | ||
| 329 | pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { | 332 | pub 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; | ||
| 331 | 335 | ||
| 332 | relocs_log.debug("{x}: .eh_frame", .{elf_file.shdrs.items[elf_file.eh_frame_section_index.?].sh_addr}); | 336 | relocs_log.debug("{x}: .eh_frame", .{elf_file.shdrs.items[elf_file.eh_frame_section_index.?].sh_addr}); |
| 333 | 337 | ||
| ... | @@ -378,7 +382,8 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { | ... | @@ -378,7 +382,8 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { |
| 378 | } | 382 | } |
| 379 | 383 | ||
| 380 | pub fn writeEhFrameObject(elf_file: *Elf, writer: anytype) !void { | 384 | pub 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; | ||
| 382 | 387 | ||
| 383 | for (elf_file.objects.items) |index| { | 388 | for (elf_file.objects.items) |index| { |
| 384 | const object = elf_file.file(index).?.object; | 389 | const object = elf_file.file(index).?.object; |
| ... | @@ -467,6 +472,9 @@ pub fn writeEhFrameRelocs(elf_file: *Elf, writer: anytype) !void { | ... | @@ -467,6 +472,9 @@ pub fn writeEhFrameRelocs(elf_file: *Elf, writer: anytype) !void { |
| 467 | } | 472 | } |
| 468 | 473 | ||
| 469 | pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { | 474 | pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { |
| 475 | const comp = elf_file.base.comp; | ||
| 476 | const gpa = comp.gpa; | ||
| 477 | |||
| 470 | try writer.writeByte(1); // version | 478 | try writer.writeByte(1); // version |
| 471 | try writer.writeByte(EH_PE.pcrel | EH_PE.sdata4); | 479 | try writer.writeByte(EH_PE.pcrel | EH_PE.sdata4); |
| 472 | try writer.writeByte(EH_PE.udata4); | 480 | try writer.writeByte(EH_PE.udata4); |
| ... | @@ -495,7 +503,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { | ... | @@ -495,7 +503,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { |
| 495 | } | 503 | } |
| 496 | }; | 504 | }; |
| 497 | 505 | ||
| 498 | var entries = std.ArrayList(Entry).init(elf_file.base.allocator); | 506 | var entries = std.ArrayList(Entry).init(gpa); |
| 499 | defer entries.deinit(); | 507 | defer entries.deinit(); |
| 500 | try entries.ensureTotalCapacityPrecise(num_fdes); | 508 | try entries.ensureTotalCapacityPrecise(num_fdes); |
| 501 | 509 |
src/link/Elf/gc.zig+2-1| ... | @@ -1,5 +1,6 @@ | ... | @@ -1,5 +1,6 @@ |
| 1 | pub fn gcAtoms(elf_file: *Elf) !void { | 1 | pub 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; | ||
| 3 | const num_files = elf_file.objects.items.len + @intFromBool(elf_file.zig_object_index != null); | 4 | const num_files = elf_file.objects.items.len + @intFromBool(elf_file.zig_object_index != null); |
| 4 | var files = try std.ArrayList(File.Index).initCapacity(gpa, num_files); | 5 | var files = try std.ArrayList(File.Index).initCapacity(gpa, num_files); |
| 5 | defer files.deinit(); | 6 | defer files.deinit(); |
src/link/Elf/synthetic_sections.zig+13-13| ... | @@ -57,7 +57,7 @@ pub const DynamicSection = struct { | ... | @@ -57,7 +57,7 @@ pub const DynamicSection = struct { |
| 57 | if (elf_file.z_now) { | 57 | if (elf_file.z_now) { |
| 58 | flags_1 |= elf.DF_1_NOW; | 58 | flags_1 |= elf.DF_1_NOW; |
| 59 | } | 59 | } |
| 60 | if (elf_file.isExe() and comp.config.pie) { | 60 | if (elf_file.base.isExe() and comp.config.pie) { |
| 61 | flags_1 |= elf.DF_1_PIE; | 61 | flags_1 |= elf.DF_1_PIE; |
| 62 | } | 62 | } |
| 63 | // if (elf_file.z_nodlopen) { | 63 | // if (elf_file.z_nodlopen) { |
| ... | @@ -89,7 +89,7 @@ pub const DynamicSection = struct { | ... | @@ -89,7 +89,7 @@ pub const DynamicSection = struct { |
| 89 | if (elf_file.verneed_section_index != null) nentries += 2; // VERNEED | 89 | if (elf_file.verneed_section_index != null) nentries += 2; // VERNEED |
| 90 | if (dt.getFlags(elf_file) != null) nentries += 1; // FLAGS | 90 | if (dt.getFlags(elf_file) != null) nentries += 1; // FLAGS |
| 91 | if (dt.getFlags1(elf_file) != null) nentries += 1; // FLAGS_1 | 91 | 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 |
| 93 | nentries += 1; // NULL | 93 | nentries += 1; // NULL |
| 94 | return nentries * @sizeOf(elf.Elf64_Dyn); | 94 | return nentries * @sizeOf(elf.Elf64_Dyn); |
| 95 | } | 95 | } |
| ... | @@ -216,7 +216,7 @@ pub const DynamicSection = struct { | ... | @@ -216,7 +216,7 @@ pub const DynamicSection = struct { |
| 216 | } | 216 | } |
| 217 | 217 | ||
| 218 | // DEBUG | 218 | // 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 }); |
| 220 | 220 | ||
| 221 | // NULL | 221 | // NULL |
| 222 | try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_NULL, .d_val = 0 }); | 222 | try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_NULL, .d_val = 0 }); |
| ... | @@ -256,7 +256,7 @@ pub const ZigGotSection = struct { | ... | @@ -256,7 +256,7 @@ pub const ZigGotSection = struct { |
| 256 | entry.* = sym_index; | 256 | entry.* = sym_index; |
| 257 | const symbol = elf_file.symbol(sym_index); | 257 | const symbol = elf_file.symbol(sym_index); |
| 258 | symbol.flags.has_zig_got = true; | 258 | 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)) { |
| 260 | zig_got.flags.needs_rela = true; | 260 | zig_got.flags.needs_rela = true; |
| 261 | } | 261 | } |
| 262 | if (symbol.extra(elf_file)) |extra| { | 262 | if (symbol.extra(elf_file)) |extra| { |
| ... | @@ -494,7 +494,7 @@ pub const GotSection = struct { | ... | @@ -494,7 +494,7 @@ pub const GotSection = struct { |
| 494 | const symbol = elf_file.symbol(sym_index); | 494 | const symbol = elf_file.symbol(sym_index); |
| 495 | symbol.flags.has_got = true; | 495 | symbol.flags.has_got = true; |
| 496 | if (symbol.flags.import or symbol.isIFunc(elf_file) or | 496 | 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))) |
| 498 | { | 498 | { |
| 499 | got.flags.needs_rela = true; | 499 | got.flags.needs_rela = true; |
| 500 | } | 500 | } |
| ... | @@ -527,7 +527,7 @@ pub const GotSection = struct { | ... | @@ -527,7 +527,7 @@ pub const GotSection = struct { |
| 527 | entry.symbol_index = sym_index; | 527 | entry.symbol_index = sym_index; |
| 528 | const symbol = elf_file.symbol(sym_index); | 528 | const symbol = elf_file.symbol(sym_index); |
| 529 | symbol.flags.has_tlsgd = true; | 529 | 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; |
| 531 | if (symbol.extra(elf_file)) |extra| { | 531 | if (symbol.extra(elf_file)) |extra| { |
| 532 | var new_extra = extra; | 532 | var new_extra = extra; |
| 533 | new_extra.tlsgd = index; | 533 | new_extra.tlsgd = index; |
| ... | @@ -544,7 +544,7 @@ pub const GotSection = struct { | ... | @@ -544,7 +544,7 @@ pub const GotSection = struct { |
| 544 | entry.symbol_index = sym_index; | 544 | entry.symbol_index = sym_index; |
| 545 | const symbol = elf_file.symbol(sym_index); | 545 | const symbol = elf_file.symbol(sym_index); |
| 546 | symbol.flags.has_gottp = true; | 546 | 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; |
| 548 | if (symbol.extra(elf_file)) |extra| { | 548 | if (symbol.extra(elf_file)) |extra| { |
| 549 | var new_extra = extra; | 549 | var new_extra = extra; |
| 550 | new_extra.gottp = index; | 550 | new_extra.gottp = index; |
| ... | @@ -579,7 +579,7 @@ pub const GotSection = struct { | ... | @@ -579,7 +579,7 @@ pub const GotSection = struct { |
| 579 | 579 | ||
| 580 | pub fn write(got: GotSection, elf_file: *Elf, writer: anytype) !void { | 580 | pub fn write(got: GotSection, elf_file: *Elf, writer: anytype) !void { |
| 581 | const comp = elf_file.base.comp; | 581 | const comp = elf_file.base.comp; |
| 582 | const is_dyn_lib = elf_file.isDynLib(); | 582 | const is_dyn_lib = elf_file.base.isDynLib(); |
| 583 | const apply_relocs = true; // TODO add user option for this | 583 | const apply_relocs = true; // TODO add user option for this |
| 584 | 584 | ||
| 585 | for (got.entries.items) |entry| { | 585 | for (got.entries.items) |entry| { |
| ... | @@ -594,7 +594,7 @@ pub const GotSection = struct { | ... | @@ -594,7 +594,7 @@ pub const GotSection = struct { |
| 594 | if (symbol.?.flags.import) break :blk 0; | 594 | if (symbol.?.flags.import) break :blk 0; |
| 595 | if (symbol.?.isIFunc(elf_file)) | 595 | if (symbol.?.isIFunc(elf_file)) |
| 596 | break :blk if (apply_relocs) value else 0; | 596 | 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 |
| 598 | !symbol.?.isAbs(elf_file)) | 598 | !symbol.?.isAbs(elf_file)) |
| 599 | { | 599 | { |
| 600 | break :blk if (apply_relocs) value else 0; | 600 | break :blk if (apply_relocs) value else 0; |
| ... | @@ -643,7 +643,7 @@ pub const GotSection = struct { | ... | @@ -643,7 +643,7 @@ pub const GotSection = struct { |
| 643 | pub fn addRela(got: GotSection, elf_file: *Elf) !void { | 643 | pub fn addRela(got: GotSection, elf_file: *Elf) !void { |
| 644 | const comp = elf_file.base.comp; | 644 | const comp = elf_file.base.comp; |
| 645 | const gpa = comp.gpa; | 645 | const gpa = comp.gpa; |
| 646 | const is_dyn_lib = elf_file.isDynLib(); | 646 | const is_dyn_lib = elf_file.base.isDynLib(); |
| 647 | try elf_file.rela_dyn.ensureUnusedCapacity(gpa, got.numRela(elf_file)); | 647 | try elf_file.rela_dyn.ensureUnusedCapacity(gpa, got.numRela(elf_file)); |
| 648 | 648 | ||
| 649 | for (got.entries.items) |entry| { | 649 | for (got.entries.items) |entry| { |
| ... | @@ -672,7 +672,7 @@ pub const GotSection = struct { | ... | @@ -672,7 +672,7 @@ pub const GotSection = struct { |
| 672 | }); | 672 | }); |
| 673 | continue; | 673 | continue; |
| 674 | } | 674 | } |
| 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 |
| 676 | !symbol.?.isAbs(elf_file)) | 676 | !symbol.?.isAbs(elf_file)) |
| 677 | { | 677 | { |
| 678 | elf_file.addRelaDynAssumeCapacity(.{ | 678 | elf_file.addRelaDynAssumeCapacity(.{ |
| ... | @@ -746,7 +746,7 @@ pub const GotSection = struct { | ... | @@ -746,7 +746,7 @@ pub const GotSection = struct { |
| 746 | 746 | ||
| 747 | pub fn numRela(got: GotSection, elf_file: *Elf) usize { | 747 | pub fn numRela(got: GotSection, elf_file: *Elf) usize { |
| 748 | const comp = elf_file.base.comp; | 748 | const comp = elf_file.base.comp; |
| 749 | const is_dyn_lib = elf_file.isDynLib(); | 749 | const is_dyn_lib = elf_file.base.isDynLib(); |
| 750 | var num: usize = 0; | 750 | var num: usize = 0; |
| 751 | for (got.entries.items) |entry| { | 751 | for (got.entries.items) |entry| { |
| 752 | const symbol = switch (entry.tag) { | 752 | const symbol = switch (entry.tag) { |
| ... | @@ -755,7 +755,7 @@ pub const GotSection = struct { | ... | @@ -755,7 +755,7 @@ pub const GotSection = struct { |
| 755 | }; | 755 | }; |
| 756 | switch (entry.tag) { | 756 | switch (entry.tag) { |
| 757 | .got => if (symbol.?.flags.import or symbol.?.isIFunc(elf_file) or | 757 | .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 |
| 759 | !symbol.?.isAbs(elf_file))) | 759 | !symbol.?.isAbs(elf_file))) |
| 760 | { | 760 | { |
| 761 | num += 1; | 761 | num += 1; |