| author | |
| committer | |
| log | 669f28594393e90e4d1aacd0d28f67ebe015b922 |
| tree | 08f6d4e19a0b8dfc431171214fffdb6e6d956883 |
| parent | f1fedb3a51d4d9a37e4568ce1a80149bb5a610f7 |
15 files changed, 464 insertions(+), 353 deletions(-)
src/link/Elf.zig+39-99| ... | ... | @@ -54,7 +54,7 @@ phdr_to_shdr_table: std.AutoHashMapUnmanaged(u32, u32) = .{}, |
| 54 | 54 | shdr_table_offset: ?u64 = null, |
| 55 | 55 | /// Table of lists of atoms per output section. |
| 56 | 56 | /// This table is not used to track incrementally generated atoms. |
| 57 | output_sections: std.AutoArrayHashMapUnmanaged(u32, std.ArrayListUnmanaged(Atom.Index)) = .{}, | |
| 57 | output_sections: std.AutoArrayHashMapUnmanaged(u32, std.ArrayListUnmanaged(Ref)) = .{}, | |
| 58 | 58 | output_rela_sections: std.AutoArrayHashMapUnmanaged(u32, RelaSection) = .{}, |
| 59 | 59 | |
| 60 | 60 | /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. |
| ... | ... | @@ -203,10 +203,6 @@ resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{}, |
| 203 | 203 | has_text_reloc: bool = false, |
| 204 | 204 | num_ifunc_dynrelocs: usize = 0, |
| 205 | 205 | |
| 206 | /// List of atoms that are owned directly by the linker. | |
| 207 | atoms: std.ArrayListUnmanaged(Atom) = .{}, | |
| 208 | atoms_extra: std.ArrayListUnmanaged(u32) = .{}, | |
| 209 | ||
| 210 | 206 | /// List of range extension thunks. |
| 211 | 207 | thunks: std.ArrayListUnmanaged(Thunk) = .{}, |
| 212 | 208 | |
| ... | ... | @@ -375,9 +371,6 @@ pub fn createEmpty( |
| 375 | 371 | try self.symbols.append(gpa, .{}); |
| 376 | 372 | // Index 0 is always a null symbol. |
| 377 | 373 | try self.symbols_extra.append(gpa, 0); |
| 378 | // Allocate atom index 0 to null atom | |
| 379 | try self.atoms.append(gpa, .{}); | |
| 380 | try self.atoms_extra.append(gpa, 0); | |
| 381 | 374 | // Append null file at index 0 |
| 382 | 375 | try self.files.append(gpa, .null); |
| 383 | 376 | // Append null byte to string tables |
| ... | ... | @@ -499,8 +492,6 @@ pub fn deinit(self: *Elf) void { |
| 499 | 492 | self.resolver.deinit(gpa); |
| 500 | 493 | self.start_stop_indexes.deinit(gpa); |
| 501 | 494 | |
| 502 | self.atoms.deinit(gpa); | |
| 503 | self.atoms_extra.deinit(gpa); | |
| 504 | 495 | for (self.thunks.items) |*th| { |
| 505 | 496 | th.deinit(gpa); |
| 506 | 497 | } |
| ... | ... | @@ -1305,6 +1296,8 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod |
| 1305 | 1296 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); |
| 1306 | 1297 | self.files.set(index, .{ .linker_defined = .{ .index = index } }); |
| 1307 | 1298 | self.linker_defined_index = index; |
| 1299 | const object = self.file(index).?.linker_defined; | |
| 1300 | try object.init(gpa); | |
| 1308 | 1301 | } |
| 1309 | 1302 | |
| 1310 | 1303 | // Now, we are ready to resolve the symbols across all input files. |
| ... | ... | @@ -1379,15 +1372,15 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod |
| 1379 | 1372 | |
| 1380 | 1373 | // Beyond this point, everything has been allocated a virtual address and we can resolve |
| 1381 | 1374 | // the relocations, and commit objects to file. |
| 1382 | if (self.zigObjectPtr()) |zig_object| { | |
| 1375 | if (self.zigObjectPtr()) |zo| { | |
| 1383 | 1376 | var has_reloc_errors = false; |
| 1384 | for (zig_object.atoms.items) |atom_index| { | |
| 1385 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 1377 | for (zo.atoms_indexes.items) |atom_index| { | |
| 1378 | const atom_ptr = zo.atom(atom_index) orelse continue; | |
| 1386 | 1379 | if (!atom_ptr.flags.alive) continue; |
| 1387 | 1380 | const out_shndx = atom_ptr.outputShndx() orelse continue; |
| 1388 | 1381 | const shdr = &self.shdrs.items[out_shndx]; |
| 1389 | 1382 | if (shdr.sh_type == elf.SHT_NOBITS) continue; |
| 1390 | const code = try zig_object.codeAlloc(self, atom_index); | |
| 1383 | const code = try zo.codeAlloc(self, atom_index); | |
| 1391 | 1384 | defer gpa.free(code); |
| 1392 | 1385 | const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value)); |
| 1393 | 1386 | atom_ptr.resolveRelocsAlloc(self, code) catch |err| switch (err) { |
| ... | ... | @@ -2012,8 +2005,8 @@ pub fn resolveSymbols(self: *Elf) void { |
| 2012 | 2005 | const cg_owner = self.comdatGroupOwner(cg.owner); |
| 2013 | 2006 | if (cg_owner.file != index) { |
| 2014 | 2007 | for (cg.comdatGroupMembers(self)) |shndx| { |
| 2015 | const atom_index = object.atoms.items[shndx]; | |
| 2016 | if (self.atom(atom_index)) |atom_ptr| { | |
| 2008 | const atom_index = object.atoms_indexes.items[shndx]; | |
| 2009 | if (object.atom(atom_index)) |atom_ptr| { | |
| 2017 | 2010 | atom_ptr.flags.alive = false; |
| 2018 | 2011 | atom_ptr.markFdesDead(self); |
| 2019 | 2012 | } |
| ... | ... | @@ -2117,7 +2110,7 @@ fn claimUnresolved(self: *Elf) void { |
| 2117 | 2110 | fn scanRelocs(self: *Elf) !void { |
| 2118 | 2111 | const gpa = self.base.comp.gpa; |
| 2119 | 2112 | |
| 2120 | var undefs = std.AutoHashMap(Symbol.Index, std.ArrayList(Atom.Index)).init(gpa); | |
| 2113 | var undefs = std.AutoHashMap(Symbol.Index, std.ArrayList(Ref)).init(gpa); | |
| 2121 | 2114 | defer { |
| 2122 | 2115 | var it = undefs.iterator(); |
| 2123 | 2116 | while (it.next()) |entry| { |
| ... | ... | @@ -3721,11 +3714,11 @@ fn sortInitFini(self: *Elf) !void { |
| 3721 | 3714 | |
| 3722 | 3715 | const Entry = struct { |
| 3723 | 3716 | priority: i32, |
| 3724 | atom_index: Atom.Index, | |
| 3717 | atom_ref: Ref, | |
| 3725 | 3718 | |
| 3726 | 3719 | pub fn lessThan(ctx: *Elf, lhs: @This(), rhs: @This()) bool { |
| 3727 | 3720 | if (lhs.priority == rhs.priority) { |
| 3728 | return ctx.atom(lhs.atom_index).?.priority(ctx) < ctx.atom(rhs.atom_index).?.priority(ctx); | |
| 3721 | return ctx.atom(lhs.atom_ref).?.priority(ctx) < ctx.atom(rhs.atom_ref).?.priority(ctx); | |
| 3729 | 3722 | } |
| 3730 | 3723 | return lhs.priority < rhs.priority; |
| 3731 | 3724 | } |
| ... | ... | @@ -3756,8 +3749,8 @@ fn sortInitFini(self: *Elf) !void { |
| 3756 | 3749 | try entries.ensureTotalCapacityPrecise(atom_list.items.len); |
| 3757 | 3750 | defer entries.deinit(); |
| 3758 | 3751 | |
| 3759 | for (atom_list.items) |atom_index| { | |
| 3760 | const atom_ptr = self.atom(atom_index).?; | |
| 3752 | for (atom_list.items) |ref| { | |
| 3753 | const atom_ptr = self.atom(ref).?; | |
| 3761 | 3754 | const object = atom_ptr.file(self).?.object; |
| 3762 | 3755 | const priority = blk: { |
| 3763 | 3756 | if (is_ctor_dtor) { |
| ... | ... | @@ -3770,14 +3763,14 @@ fn sortInitFini(self: *Elf) !void { |
| 3770 | 3763 | const priority = std.fmt.parseUnsigned(u16, it.first(), 10) catch default; |
| 3771 | 3764 | break :blk priority; |
| 3772 | 3765 | }; |
| 3773 | entries.appendAssumeCapacity(.{ .priority = priority, .atom_index = atom_index }); | |
| 3766 | entries.appendAssumeCapacity(.{ .priority = priority, .atom_ref = ref }); | |
| 3774 | 3767 | } |
| 3775 | 3768 | |
| 3776 | 3769 | mem.sort(Entry, entries.items, self, Entry.lessThan); |
| 3777 | 3770 | |
| 3778 | 3771 | atom_list.clearRetainingCapacity(); |
| 3779 | 3772 | for (entries.items) |entry| { |
| 3780 | atom_list.appendAssumeCapacity(entry.atom_index); | |
| 3773 | atom_list.appendAssumeCapacity(entry.atom_ref); | |
| 3781 | 3774 | } |
| 3782 | 3775 | } |
| 3783 | 3776 | } |
| ... | ... | @@ -4143,23 +4136,23 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) !void { |
| 4143 | 4136 | } |
| 4144 | 4137 | } |
| 4145 | 4138 | |
| 4146 | if (self.zigObjectPtr()) |zig_object| { | |
| 4147 | for (zig_object.atoms.items) |atom_index| { | |
| 4148 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 4139 | if (self.zigObjectPtr()) |zo| { | |
| 4140 | for (zo.atoms_indexes.items) |atom_index| { | |
| 4141 | const atom_ptr = zo.atom(atom_index) orelse continue; | |
| 4149 | 4142 | atom_ptr.output_section_index = backlinks[atom_ptr.output_section_index]; |
| 4150 | 4143 | } |
| 4151 | 4144 | |
| 4152 | for (zig_object.locals()) |local_index| { | |
| 4145 | for (zo.locals()) |local_index| { | |
| 4153 | 4146 | const local = self.symbol(local_index); |
| 4154 | 4147 | local.output_section_index = backlinks[local.output_section_index]; |
| 4155 | 4148 | } |
| 4156 | 4149 | |
| 4157 | for (zig_object.globals()) |global_index| { | |
| 4150 | for (zo.globals()) |global_index| { | |
| 4158 | 4151 | const global = self.symbol(global_index); |
| 4159 | 4152 | const atom_ptr = global.atom(self) orelse continue; |
| 4160 | 4153 | if (!atom_ptr.flags.alive) continue; |
| 4161 | 4154 | // TODO claim unresolved for objects |
| 4162 | if (global.file(self).?.index() != zig_object.index) continue; | |
| 4155 | if (global.file(self).?.index() != zo.index) continue; | |
| 4163 | 4156 | const out_shndx = global.outputShndx() orelse continue; |
| 4164 | 4157 | global.output_section_index = backlinks[out_shndx]; |
| 4165 | 4158 | } |
| ... | ... | @@ -4182,8 +4175,8 @@ fn updateSectionSizes(self: *Elf) !void { |
| 4182 | 4175 | const shdr = &self.shdrs.items[shndx]; |
| 4183 | 4176 | if (atom_list.items.len == 0) continue; |
| 4184 | 4177 | if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue; |
| 4185 | for (atom_list.items) |atom_index| { | |
| 4186 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 4178 | for (atom_list.items) |ref| { | |
| 4179 | const atom_ptr = self.atom(ref) orelse continue; | |
| 4187 | 4180 | if (!atom_ptr.flags.alive) continue; |
| 4188 | 4181 | const offset = atom_ptr.alignment.forward(shdr.sh_size); |
| 4189 | 4182 | const padding = offset - shdr.sh_size; |
| ... | ... | @@ -4618,7 +4611,7 @@ fn allocateSpecialPhdrs(self: *Elf) void { |
| 4618 | 4611 | fn writeAtoms(self: *Elf) !void { |
| 4619 | 4612 | const gpa = self.base.comp.gpa; |
| 4620 | 4613 | |
| 4621 | var undefs = std.AutoHashMap(Symbol.Index, std.ArrayList(Atom.Index)).init(gpa); | |
| 4614 | var undefs = std.AutoHashMap(Symbol.Index, std.ArrayList(Ref)).init(gpa); | |
| 4622 | 4615 | defer { |
| 4623 | 4616 | var it = undefs.iterator(); |
| 4624 | 4617 | while (it.next()) |entry| { |
| ... | ... | @@ -4666,21 +4659,21 @@ fn writeAtoms(self: *Elf) !void { |
| 4666 | 4659 | 0; |
| 4667 | 4660 | @memset(buffer, padding_byte); |
| 4668 | 4661 | |
| 4669 | for (atom_list.items) |atom_index| { | |
| 4670 | const atom_ptr = self.atom(atom_index).?; | |
| 4662 | for (atom_list.items) |ref| { | |
| 4663 | const atom_ptr = self.atom(ref).?; | |
| 4671 | 4664 | assert(atom_ptr.flags.alive); |
| 4672 | 4665 | |
| 4673 | 4666 | const offset = math.cast(usize, atom_ptr.value - @as(i64, @intCast(base_offset))) orelse |
| 4674 | 4667 | return error.Overflow; |
| 4675 | 4668 | const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow; |
| 4676 | 4669 | |
| 4677 | log.debug("writing atom({d}) at 0x{x}", .{ atom_index, sh_offset + offset }); | |
| 4670 | log.debug("writing atom({}) at 0x{x}", .{ ref, sh_offset + offset }); | |
| 4678 | 4671 | |
| 4679 | 4672 | // TODO decompress directly into provided buffer |
| 4680 | 4673 | const out_code = buffer[offset..][0..size]; |
| 4681 | 4674 | const in_code = switch (atom_ptr.file(self).?) { |
| 4682 | .object => |x| try x.codeDecompressAlloc(self, atom_index), | |
| 4683 | .zig_object => |x| try x.codeAlloc(self, atom_index), | |
| 4675 | .object => |x| try x.codeDecompressAlloc(self, ref.index), | |
| 4676 | .zig_object => |x| try x.codeAlloc(self, ref.index), | |
| 4684 | 4677 | else => unreachable, |
| 4685 | 4678 | }; |
| 4686 | 4679 | defer gpa.free(in_code); |
| ... | ... | @@ -5598,64 +5591,6 @@ fn getStartStopBasename(self: *Elf, shdr: elf.Elf64_Shdr) ?[]const u8 { |
| 5598 | 5591 | return null; |
| 5599 | 5592 | } |
| 5600 | 5593 | |
| 5601 | pub fn atom(self: *Elf, atom_index: Atom.Index) ?*Atom { | |
| 5602 | if (atom_index == 0) return null; | |
| 5603 | assert(atom_index < self.atoms.items.len); | |
| 5604 | return &self.atoms.items[atom_index]; | |
| 5605 | } | |
| 5606 | ||
| 5607 | pub fn addAtom(self: *Elf) !Atom.Index { | |
| 5608 | const gpa = self.base.comp.gpa; | |
| 5609 | const index = @as(Atom.Index, @intCast(self.atoms.items.len)); | |
| 5610 | const atom_ptr = try self.atoms.addOne(gpa); | |
| 5611 | atom_ptr.* = .{ .atom_index = index }; | |
| 5612 | return index; | |
| 5613 | } | |
| 5614 | ||
| 5615 | pub fn addAtomExtra(self: *Elf, extra: Atom.Extra) !u32 { | |
| 5616 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 5617 | try self.atoms_extra.ensureUnusedCapacity(self.base.comp.gpa, fields.len); | |
| 5618 | return self.addAtomExtraAssumeCapacity(extra); | |
| 5619 | } | |
| 5620 | ||
| 5621 | pub fn addAtomExtraAssumeCapacity(self: *Elf, extra: Atom.Extra) u32 { | |
| 5622 | const index = @as(u32, @intCast(self.atoms_extra.items.len)); | |
| 5623 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 5624 | inline for (fields) |field| { | |
| 5625 | self.atoms_extra.appendAssumeCapacity(switch (field.type) { | |
| 5626 | u32 => @field(extra, field.name), | |
| 5627 | else => @compileError("bad field type"), | |
| 5628 | }); | |
| 5629 | } | |
| 5630 | return index; | |
| 5631 | } | |
| 5632 | ||
| 5633 | pub fn atomExtra(self: *Elf, index: u32) ?Atom.Extra { | |
| 5634 | if (index == 0) return null; | |
| 5635 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 5636 | var i: usize = index; | |
| 5637 | var result: Atom.Extra = undefined; | |
| 5638 | inline for (fields) |field| { | |
| 5639 | @field(result, field.name) = switch (field.type) { | |
| 5640 | u32 => self.atoms_extra.items[i], | |
| 5641 | else => @compileError("bad field type"), | |
| 5642 | }; | |
| 5643 | i += 1; | |
| 5644 | } | |
| 5645 | return result; | |
| 5646 | } | |
| 5647 | ||
| 5648 | pub fn setAtomExtra(self: *Elf, index: u32, extra: Atom.Extra) void { | |
| 5649 | assert(index > 0); | |
| 5650 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 5651 | inline for (fields, 0..) |field, i| { | |
| 5652 | self.atoms_extra.items[index + i] = switch (field.type) { | |
| 5653 | u32 => @field(extra, field.name), | |
| 5654 | else => @compileError("bad field type"), | |
| 5655 | }; | |
| 5656 | } | |
| 5657 | } | |
| 5658 | ||
| 5659 | 5594 | pub fn addThunk(self: *Elf) !Thunk.Index { |
| 5660 | 5595 | const index = @as(Thunk.Index, @intCast(self.thunks.items.len)); |
| 5661 | 5596 | const th = try self.thunks.addOne(self.base.comp.gpa); |
| ... | ... | @@ -5692,6 +5627,11 @@ pub fn fileHandle(self: Elf, index: File.HandleIndex) File.Handle { |
| 5692 | 5627 | return self.file_handles.items[index]; |
| 5693 | 5628 | } |
| 5694 | 5629 | |
| 5630 | pub fn atom(self: *Elf, ref: Ref) ?*Atom { | |
| 5631 | const file_ptr = self.file(ref.file) orelse return null; | |
| 5632 | return file_ptr.atom(ref.index); | |
| 5633 | } | |
| 5634 | ||
| 5695 | 5635 | /// Returns pointer-to-symbol described at sym_index. |
| 5696 | 5636 | pub fn symbol(self: *Elf, sym_index: Symbol.Index) *Symbol { |
| 5697 | 5637 | return &self.symbols.items[sym_index]; |
| ... | ... | @@ -5938,9 +5878,9 @@ fn reportUndefinedSymbols(self: *Elf, undefs: anytype) !void { |
| 5938 | 5878 | var err = try self.base.addErrorWithNotesAssumeCapacity(nnotes); |
| 5939 | 5879 | try err.addMsg("undefined symbol: {s}", .{self.symbol(undef_index).name(self)}); |
| 5940 | 5880 | |
| 5941 | for (atoms[0..natoms]) |atom_index| { | |
| 5942 | const atom_ptr = self.atom(atom_index).?; | |
| 5943 | const file_ptr = self.file(atom_ptr.file_index).?; | |
| 5881 | for (atoms[0..natoms]) |ref| { | |
| 5882 | const atom_ptr = self.atom(ref).?; | |
| 5883 | const file_ptr = self.file(ref.file).?; | |
| 5944 | 5884 | try err.addNote("referenced by {s}:{s}", .{ file_ptr.fmtPath(), atom_ptr.name(self) }); |
| 5945 | 5885 | } |
| 5946 | 5886 | |
| ... | ... | @@ -6401,7 +6341,7 @@ const LastAtomAndFreeListTable = std.AutoArrayHashMapUnmanaged(u32, LastAtomAndF |
| 6401 | 6341 | |
| 6402 | 6342 | const RelaSection = struct { |
| 6403 | 6343 | shndx: u32, |
| 6404 | atom_list: std.ArrayListUnmanaged(Atom.Index) = .{}, | |
| 6344 | atom_list: std.ArrayListUnmanaged(Ref) = .{}, | |
| 6405 | 6345 | }; |
| 6406 | 6346 | const RelaSectionTable = std.AutoArrayHashMapUnmanaged(u32, RelaSection); |
| 6407 | 6347 |
src/link/Elf/Atom.zig+29-27| ... | ... | @@ -68,7 +68,7 @@ pub fn file(self: Atom, elf_file: *Elf) ?File { |
| 68 | 68 | |
| 69 | 69 | pub fn thunk(self: Atom, elf_file: *Elf) *Thunk { |
| 70 | 70 | assert(self.flags.thunk); |
| 71 | const extras = self.extra(elf_file).?; | |
| 71 | const extras = self.extra(elf_file); | |
| 72 | 72 | return elf_file.thunk(extras.thunk); |
| 73 | 73 | } |
| 74 | 74 | |
| ... | ... | @@ -99,7 +99,8 @@ pub fn priority(self: Atom, elf_file: *Elf) u64 { |
| 99 | 99 | /// File offset relocation happens transparently, so it is not included in |
| 100 | 100 | /// this calculation. |
| 101 | 101 | pub fn capacity(self: Atom, elf_file: *Elf) u64 { |
| 102 | const next_addr = if (elf_file.atom(self.next_index)) |next| | |
| 102 | const zo = elf_file.zigObjectPtr().?; | |
| 103 | const next_addr = if (zo.atom(self.next_index)) |next| | |
| 103 | 104 | next.address(elf_file) |
| 104 | 105 | else |
| 105 | 106 | std.math.maxInt(u32); |
| ... | ... | @@ -107,8 +108,9 @@ pub fn capacity(self: Atom, elf_file: *Elf) u64 { |
| 107 | 108 | } |
| 108 | 109 | |
| 109 | 110 | pub fn freeListEligible(self: Atom, elf_file: *Elf) bool { |
| 111 | const zo = elf_file.zigObjectPtr().?; | |
| 110 | 112 | // No need to keep a free list node for the last block. |
| 111 | const next = elf_file.atom(self.next_index) orelse return false; | |
| 113 | const next = zo.atom(self.next_index) orelse return false; | |
| 112 | 114 | const cap: u64 = @intCast(next.address(elf_file) - self.address(elf_file)); |
| 113 | 115 | const ideal_cap = Elf.padToIdeal(self.size); |
| 114 | 116 | if (cap <= ideal_cap) return false; |
| ... | ... | @@ -117,6 +119,7 @@ pub fn freeListEligible(self: Atom, elf_file: *Elf) bool { |
| 117 | 119 | } |
| 118 | 120 | |
| 119 | 121 | pub fn allocate(self: *Atom, elf_file: *Elf) !void { |
| 122 | const zo = elf_file.zigObjectPtr().?; | |
| 120 | 123 | const shdr = &elf_file.shdrs.items[self.outputShndx().?]; |
| 121 | 124 | const meta = elf_file.last_atom_and_free_list_table.getPtr(self.outputShndx().?).?; |
| 122 | 125 | const free_list = &meta.free_list; |
| ... | ... | @@ -137,7 +140,7 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void { |
| 137 | 140 | var i: usize = if (elf_file.base.child_pid == null) 0 else free_list.items.len; |
| 138 | 141 | while (i < free_list.items.len) { |
| 139 | 142 | const big_atom_index = free_list.items[i]; |
| 140 | const big_atom = elf_file.atom(big_atom_index).?; | |
| 143 | const big_atom = zo.atom(big_atom_index).?; | |
| 141 | 144 | // We now have a pointer to a live atom that has too much capacity. |
| 142 | 145 | // Is it enough that we could fit this new atom? |
| 143 | 146 | const cap = big_atom.capacity(elf_file); |
| ... | ... | @@ -169,7 +172,7 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void { |
| 169 | 172 | free_list_removal = i; |
| 170 | 173 | } |
| 171 | 174 | break :blk @intCast(new_start_vaddr); |
| 172 | } else if (elf_file.atom(last_atom_index.*)) |last| { | |
| 175 | } else if (zo.atom(last_atom_index.*)) |last| { | |
| 173 | 176 | const ideal_capacity = Elf.padToIdeal(last.size); |
| 174 | 177 | const ideal_capacity_end_vaddr = @as(u64, @intCast(last.value)) + ideal_capacity; |
| 175 | 178 | const new_start_vaddr = self.alignment.forward(ideal_capacity_end_vaddr); |
| ... | ... | @@ -189,7 +192,7 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void { |
| 189 | 192 | }); |
| 190 | 193 | |
| 191 | 194 | const expand_section = if (atom_placement) |placement_index| |
| 192 | elf_file.atom(placement_index).?.next_index == 0 | |
| 195 | zo.atom(placement_index).?.next_index == 0 | |
| 193 | 196 | else |
| 194 | 197 | true; |
| 195 | 198 | if (expand_section) { |
| ... | ... | @@ -214,15 +217,15 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void { |
| 214 | 217 | // This function can also reallocate an atom. |
| 215 | 218 | // In this case we need to "unplug" it from its previous location before |
| 216 | 219 | // plugging it in to its new location. |
| 217 | if (elf_file.atom(self.prev_index)) |prev| { | |
| 220 | if (zo.atom(self.prev_index)) |prev| { | |
| 218 | 221 | prev.next_index = self.next_index; |
| 219 | 222 | } |
| 220 | if (elf_file.atom(self.next_index)) |next| { | |
| 223 | if (zo.atom(self.next_index)) |next| { | |
| 221 | 224 | next.prev_index = self.prev_index; |
| 222 | 225 | } |
| 223 | 226 | |
| 224 | 227 | if (atom_placement) |big_atom_index| { |
| 225 | const big_atom = elf_file.atom(big_atom_index).?; | |
| 228 | const big_atom = zo.atom(big_atom_index).?; | |
| 226 | 229 | self.prev_index = big_atom_index; |
| 227 | 230 | self.next_index = big_atom.next_index; |
| 228 | 231 | big_atom.next_index = self.atom_index; |
| ... | ... | @@ -250,6 +253,7 @@ pub fn grow(self: *Atom, elf_file: *Elf) !void { |
| 250 | 253 | pub fn free(self: *Atom, elf_file: *Elf) void { |
| 251 | 254 | log.debug("freeAtom {d} ({s})", .{ self.atom_index, self.name(elf_file) }); |
| 252 | 255 | |
| 256 | const zo = elf_file.zigObjectPtr().?; | |
| 253 | 257 | const comp = elf_file.base.comp; |
| 254 | 258 | const gpa = comp.gpa; |
| 255 | 259 | const shndx = self.outputShndx().?; |
| ... | ... | @@ -272,9 +276,9 @@ pub fn free(self: *Atom, elf_file: *Elf) void { |
| 272 | 276 | } |
| 273 | 277 | } |
| 274 | 278 | |
| 275 | if (elf_file.atom(last_atom_index.*)) |last_atom| { | |
| 279 | if (zo.atom(last_atom_index.*)) |last_atom| { | |
| 276 | 280 | if (last_atom.atom_index == self.atom_index) { |
| 277 | if (elf_file.atom(self.prev_index)) |_| { | |
| 281 | if (zo.atom(self.prev_index)) |_| { | |
| 278 | 282 | // TODO shrink the section size here |
| 279 | 283 | last_atom_index.* = self.prev_index; |
| 280 | 284 | } else { |
| ... | ... | @@ -283,7 +287,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void { |
| 283 | 287 | } |
| 284 | 288 | } |
| 285 | 289 | |
| 286 | if (elf_file.atom(self.prev_index)) |prev| { | |
| 290 | if (zo.atom(self.prev_index)) |prev| { | |
| 287 | 291 | prev.next_index = self.next_index; |
| 288 | 292 | if (!already_have_free_list_node and prev.*.freeListEligible(elf_file)) { |
| 289 | 293 | // The free list is heuristics, it doesn't have to be perfect, so we can |
| ... | ... | @@ -294,7 +298,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void { |
| 294 | 298 | self.prev_index = 0; |
| 295 | 299 | } |
| 296 | 300 | |
| 297 | if (elf_file.atom(self.next_index)) |next| { | |
| 301 | if (zo.atom(self.next_index)) |next| { | |
| 298 | 302 | next.prev_index = self.prev_index; |
| 299 | 303 | } else { |
| 300 | 304 | self.next_index = 0; |
| ... | ... | @@ -313,7 +317,7 @@ pub fn relocs(self: Atom, elf_file: *Elf) []const elf.Elf64_Rela { |
| 313 | 317 | switch (self.file(elf_file).?) { |
| 314 | 318 | .zig_object => |x| return x.relocs.items[shndx].items, |
| 315 | 319 | .object => |x| { |
| 316 | const extras = self.extra(elf_file).?; | |
| 320 | const extras = self.extra(elf_file); | |
| 317 | 321 | return x.relocs.items[extras.rel_index..][0..extras.rel_count]; |
| 318 | 322 | }, |
| 319 | 323 | else => unreachable, |
| ... | ... | @@ -367,7 +371,7 @@ pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.El |
| 367 | 371 | |
| 368 | 372 | pub fn fdes(self: Atom, elf_file: *Elf) []Fde { |
| 369 | 373 | if (!self.flags.fde) return &[0]Fde{}; |
| 370 | const extras = self.extra(elf_file).?; | |
| 374 | const extras = self.extra(elf_file); | |
| 371 | 375 | const object = self.file(elf_file).?.object; |
| 372 | 376 | return object.fdes.items[extras.fde_start..][0..extras.fde_count]; |
| 373 | 377 | } |
| ... | ... | @@ -712,9 +716,9 @@ fn reportUndefined( |
| 712 | 716 | { |
| 713 | 717 | const gop = try undefs.getOrPut(sym_index); |
| 714 | 718 | if (!gop.found_existing) { |
| 715 | gop.value_ptr.* = std.ArrayList(Atom.Index).init(gpa); | |
| 719 | gop.value_ptr.* = std.ArrayList(Elf.Ref).init(gpa); | |
| 716 | 720 | } |
| 717 | try gop.value_ptr.append(self.atom_index); | |
| 721 | try gop.value_ptr.append(.{ .index = self.atom_index, .file = self.file_index }); | |
| 718 | 722 | return true; |
| 719 | 723 | } |
| 720 | 724 | |
| ... | ... | @@ -1001,25 +1005,23 @@ const AddExtraOpts = struct { |
| 1001 | 1005 | rel_count: ?u32 = null, |
| 1002 | 1006 | }; |
| 1003 | 1007 | |
| 1004 | pub fn addExtra(atom: *Atom, opts: AddExtraOpts, elf_file: *Elf) !void { | |
| 1005 | if (atom.extra(elf_file) == null) { | |
| 1006 | atom.extra_index = try elf_file.addAtomExtra(.{}); | |
| 1007 | } | |
| 1008 | var extras = atom.extra(elf_file).?; | |
| 1008 | pub fn addExtra(atom: *Atom, opts: AddExtraOpts, elf_file: *Elf) void { | |
| 1009 | const file_ptr = atom.file(elf_file).?; | |
| 1010 | var extras = file_ptr.atomExtra(atom.extra_index); | |
| 1009 | 1011 | inline for (@typeInfo(@TypeOf(opts)).Struct.fields) |field| { |
| 1010 | 1012 | if (@field(opts, field.name)) |x| { |
| 1011 | 1013 | @field(extras, field.name) = x; |
| 1012 | 1014 | } |
| 1013 | 1015 | } |
| 1014 | atom.setExtra(extras, elf_file); | |
| 1016 | file_ptr.setAtomExtra(atom.extra_index, extras); | |
| 1015 | 1017 | } |
| 1016 | 1018 | |
| 1017 | pub inline fn extra(atom: Atom, elf_file: *Elf) ?Extra { | |
| 1018 | return elf_file.atomExtra(atom.extra_index); | |
| 1019 | pub inline fn extra(atom: Atom, elf_file: *Elf) Extra { | |
| 1020 | return atom.file(elf_file).?.atomExtra(atom.extra_index); | |
| 1019 | 1021 | } |
| 1020 | 1022 | |
| 1021 | 1023 | pub inline fn setExtra(atom: Atom, extras: Extra, elf_file: *Elf) void { |
| 1022 | elf_file.setAtomExtra(atom.extra_index, extras); | |
| 1024 | atom.file(elf_file).?.setAtomExtra(atom.extra_index, extras); | |
| 1023 | 1025 | } |
| 1024 | 1026 | |
| 1025 | 1027 | pub fn format( |
| ... | ... | @@ -1063,7 +1065,7 @@ fn format2( |
| 1063 | 1065 | }); |
| 1064 | 1066 | if (atom.flags.fde) { |
| 1065 | 1067 | try writer.writeAll(" : fdes{ "); |
| 1066 | const extras = atom.extra(elf_file).?; | |
| 1068 | const extras = atom.extra(elf_file); | |
| 1067 | 1069 | for (atom.fdes(elf_file), extras.fde_start..) |fde, i| { |
| 1068 | 1070 | try writer.print("{d}", .{i}); |
| 1069 | 1071 | if (!fde.alive) try writer.writeAll("([*])"); |
src/link/Elf/LinkerDefined.zig+8-1| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | index: File.Index, |
| 2 | ||
| 2 | 3 | symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{}, |
| 3 | 4 | strtab: std.ArrayListUnmanaged(u8) = .{}, |
| 4 | 5 | symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| ... | ... | @@ -11,6 +12,11 @@ pub fn deinit(self: *LinkerDefined, allocator: Allocator) void { |
| 11 | 12 | self.symbols.deinit(allocator); |
| 12 | 13 | } |
| 13 | 14 | |
| 15 | pub fn init(self: *LinkerDefined, allocator: Allocator) !void { | |
| 16 | // Null byte in strtab | |
| 17 | try self.strtab.append(allocator, 0); | |
| 18 | } | |
| 19 | ||
| 14 | 20 | pub fn addGlobal(self: *LinkerDefined, name: [:0]const u8, elf_file: *Elf) !u32 { |
| 15 | 21 | const comp = elf_file.base.comp; |
| 16 | 22 | const gpa = comp.gpa; |
| ... | ... | @@ -41,7 +47,7 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void { |
| 41 | 47 | const global = elf_file.symbol(index); |
| 42 | 48 | if (self.asFile().symbolRank(this_sym, false) < global.symbolRank(elf_file)) { |
| 43 | 49 | global.value = 0; |
| 44 | global.atom_index = 0; | |
| 50 | global.atom_ref = .{ .index = 0, .file = 0 }; | |
| 45 | 51 | global.file_index = self.index; |
| 46 | 52 | global.esym_index = sym_idx; |
| 47 | 53 | global.version_index = elf_file.default_sym_version; |
| ... | ... | @@ -127,6 +133,7 @@ const mem = std.mem; |
| 127 | 133 | const std = @import("std"); |
| 128 | 134 | |
| 129 | 135 | const Allocator = mem.Allocator; |
| 136 | const Atom = @import("Atom.zig"); | |
| 130 | 137 | const Elf = @import("../Elf.zig"); |
| 131 | 138 | const File = @import("file.zig").File; |
| 132 | 139 | const LinkerDefined = @This(); |
src/link/Elf/Object.zig+199-123| ... | ... | @@ -10,9 +10,12 @@ symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{}, |
| 10 | 10 | strtab: std.ArrayListUnmanaged(u8) = .{}, |
| 11 | 11 | first_global: ?Symbol.Index = null, |
| 12 | 12 | symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 13 | atoms: std.ArrayListUnmanaged(Atom.Index) = .{}, | |
| 14 | 13 | relocs: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{}, |
| 15 | 14 | |
| 15 | atoms: std.ArrayListUnmanaged(Atom) = .{}, | |
| 16 | atoms_indexes: std.ArrayListUnmanaged(Atom.Index) = .{}, | |
| 17 | atoms_extra: std.ArrayListUnmanaged(u32) = .{}, | |
| 18 | ||
| 16 | 19 | comdat_groups: std.ArrayListUnmanaged(Elf.ComdatGroup) = .{}, |
| 17 | 20 | comdat_group_data: std.ArrayListUnmanaged(u32) = .{}, |
| 18 | 21 | |
| ... | ... | @@ -49,6 +52,8 @@ pub fn deinit(self: *Object, allocator: Allocator) void { |
| 49 | 52 | self.strtab.deinit(allocator); |
| 50 | 53 | self.symbols.deinit(allocator); |
| 51 | 54 | self.atoms.deinit(allocator); |
| 55 | self.atoms_indexes.deinit(allocator); | |
| 56 | self.atoms_extra.deinit(allocator); | |
| 52 | 57 | self.comdat_groups.deinit(allocator); |
| 53 | 58 | self.comdat_group_data.deinit(allocator); |
| 54 | 59 | self.relocs.deinit(allocator); |
| ... | ... | @@ -71,15 +76,17 @@ pub fn parse(self: *Object, elf_file: *Elf) !void { |
| 71 | 76 | |
| 72 | 77 | // Append null input merge section |
| 73 | 78 | try self.input_merge_sections.append(gpa, .{}); |
| 79 | // Allocate atom index 0 to null atom | |
| 80 | try self.atoms.append(gpa, .{ .extra_index = try self.addAtomExtra(gpa, .{}) }); | |
| 74 | 81 | |
| 75 | 82 | try self.initAtoms(gpa, handle, elf_file); |
| 76 | 83 | try self.initSymtab(gpa, elf_file); |
| 77 | 84 | |
| 78 | 85 | for (self.shdrs.items, 0..) |shdr, i| { |
| 79 | const atom = elf_file.atom(self.atoms.items[i]) orelse continue; | |
| 80 | if (!atom.flags.alive) continue; | |
| 86 | const atom_ptr = self.atom(self.atoms_indexes.items[i]) orelse continue; | |
| 87 | if (!atom_ptr.flags.alive) continue; | |
| 81 | 88 | if ((cpu_arch == .x86_64 and shdr.sh_type == elf.SHT_X86_64_UNWIND) or |
| 82 | mem.eql(u8, atom.name(elf_file), ".eh_frame")) | |
| 89 | mem.eql(u8, atom_ptr.name(elf_file), ".eh_frame")) | |
| 83 | 90 | { |
| 84 | 91 | try self.parseEhFrame(gpa, handle, @as(u32, @intCast(i)), elf_file); |
| 85 | 92 | } |
| ... | ... | @@ -179,8 +186,11 @@ fn parseCommon(self: *Object, allocator: Allocator, handle: std.fs.File, elf_fil |
| 179 | 186 | |
| 180 | 187 | fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file: *Elf) !void { |
| 181 | 188 | const shdrs = self.shdrs.items; |
| 182 | try self.atoms.resize(allocator, shdrs.len); | |
| 183 | @memset(self.atoms.items, 0); | |
| 189 | try self.atoms.ensureTotalCapacityPrecise(allocator, shdrs.len); | |
| 190 | try self.atoms_extra.ensureTotalCapacityPrecise(allocator, shdrs.len * @sizeOf(Atom.Extra)); | |
| 191 | try self.atoms_indexes.ensureTotalCapacityPrecise(allocator, shdrs.len); | |
| 192 | try self.atoms_indexes.resize(allocator, shdrs.len); | |
| 193 | @memset(self.atoms_indexes.items, 0); | |
| 184 | 194 | |
| 185 | 195 | for (shdrs, 0..) |shdr, i| { |
| 186 | 196 | if (shdr.sh_flags & elf.SHF_EXCLUDE != 0 and |
| ... | ... | @@ -242,7 +252,19 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file: |
| 242 | 252 | else => { |
| 243 | 253 | const shndx = @as(u32, @intCast(i)); |
| 244 | 254 | if (self.skipShdr(shndx, elf_file)) continue; |
| 245 | try self.addAtom(allocator, handle, shdr, shndx, elf_file); | |
| 255 | const size, const alignment = if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) blk: { | |
| 256 | const data = try self.preadShdrContentsAlloc(allocator, handle, shndx); | |
| 257 | defer allocator.free(data); | |
| 258 | const chdr = @as(*align(1) const elf.Elf64_Chdr, @ptrCast(data.ptr)).*; | |
| 259 | break :blk .{ chdr.ch_size, Alignment.fromNonzeroByteUnits(chdr.ch_addralign) }; | |
| 260 | } else .{ shdr.sh_size, Alignment.fromNonzeroByteUnits(shdr.sh_addralign) }; | |
| 261 | const atom_index = self.addAtomAssumeCapacity(.{ | |
| 262 | .name = shdr.sh_name, | |
| 263 | .shndx = shndx, | |
| 264 | .size = size, | |
| 265 | .alignment = alignment, | |
| 266 | }); | |
| 267 | self.atoms_indexes.items[shndx] = atom_index; | |
| 246 | 268 | }, |
| 247 | 269 | } |
| 248 | 270 | } |
| ... | ... | @@ -250,14 +272,14 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file: |
| 250 | 272 | // Parse relocs sections if any. |
| 251 | 273 | for (shdrs, 0..) |shdr, i| switch (shdr.sh_type) { |
| 252 | 274 | elf.SHT_REL, elf.SHT_RELA => { |
| 253 | const atom_index = self.atoms.items[shdr.sh_info]; | |
| 254 | if (elf_file.atom(atom_index)) |atom| { | |
| 275 | const atom_index = self.atoms_indexes.items[shdr.sh_info]; | |
| 276 | if (self.atom(atom_index)) |atom_ptr| { | |
| 255 | 277 | const relocs = try self.preadRelocsAlloc(allocator, handle, @intCast(i)); |
| 256 | 278 | defer allocator.free(relocs); |
| 257 | atom.relocs_section_index = @intCast(i); | |
| 279 | atom_ptr.relocs_section_index = @intCast(i); | |
| 258 | 280 | const rel_index: u32 = @intCast(self.relocs.items.len); |
| 259 | 281 | const rel_count: u32 = @intCast(relocs.len); |
| 260 | try atom.addExtra(.{ .rel_index = rel_index, .rel_count = rel_count }, elf_file); | |
| 282 | atom_ptr.addExtra(.{ .rel_index = rel_index, .rel_count = rel_count }, elf_file); | |
| 261 | 283 | try self.relocs.appendUnalignedSlice(allocator, relocs); |
| 262 | 284 | if (elf_file.getTarget().cpu.arch == .riscv64) { |
| 263 | 285 | sortRelocs(self.relocs.items[rel_index..][0..rel_count]); |
| ... | ... | @@ -268,27 +290,6 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file: |
| 268 | 290 | }; |
| 269 | 291 | } |
| 270 | 292 | |
| 271 | fn addAtom(self: *Object, allocator: Allocator, handle: std.fs.File, shdr: elf.Elf64_Shdr, shndx: u32, elf_file: *Elf) !void { | |
| 272 | const atom_index = try elf_file.addAtom(); | |
| 273 | const atom = elf_file.atom(atom_index).?; | |
| 274 | atom.atom_index = atom_index; | |
| 275 | atom.name_offset = shdr.sh_name; | |
| 276 | atom.file_index = self.index; | |
| 277 | atom.input_section_index = shndx; | |
| 278 | self.atoms.items[shndx] = atom_index; | |
| 279 | ||
| 280 | if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) { | |
| 281 | const data = try self.preadShdrContentsAlloc(allocator, handle, shndx); | |
| 282 | defer allocator.free(data); | |
| 283 | const chdr = @as(*align(1) const elf.Elf64_Chdr, @ptrCast(data.ptr)).*; | |
| 284 | atom.size = chdr.ch_size; | |
| 285 | atom.alignment = Alignment.fromNonzeroByteUnits(chdr.ch_addralign); | |
| 286 | } else { | |
| 287 | atom.size = shdr.sh_size; | |
| 288 | atom.alignment = Alignment.fromNonzeroByteUnits(shdr.sh_addralign); | |
| 289 | } | |
| 290 | } | |
| 291 | ||
| 292 | 293 | fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) error{OutOfMemory}!u32 { |
| 293 | 294 | const name = blk: { |
| 294 | 295 | const name = self.getString(shdr.sh_name); |
| ... | ... | @@ -368,8 +369,10 @@ fn initSymtab(self: *Object, allocator: Allocator, elf_file: *Elf) !void { |
| 368 | 369 | sym_ptr.value = @intCast(sym.st_value); |
| 369 | 370 | sym_ptr.name_offset = sym.st_name; |
| 370 | 371 | sym_ptr.esym_index = @as(u32, @intCast(i)); |
| 371 | sym_ptr.atom_index = if (sym.st_shndx == elf.SHN_ABS) 0 else self.atoms.items[sym.st_shndx]; | |
| 372 | 372 | sym_ptr.file_index = self.index; |
| 373 | if (sym.st_shndx != elf.SHN_ABS) { | |
| 374 | sym_ptr.atom_ref = .{ .index = self.atoms_indexes.items[sym.st_shndx], .file = self.index }; | |
| 375 | } | |
| 373 | 376 | } |
| 374 | 377 | |
| 375 | 378 | for (self.symtab.items[first_global..]) |sym| { |
| ... | ... | @@ -456,15 +459,15 @@ fn parseEhFrame(self: *Object, allocator: Allocator, handle: std.fs.File, shndx: |
| 456 | 459 | var i: u32 = @as(u32, @intCast(fdes_start)); |
| 457 | 460 | while (i < self.fdes.items.len) { |
| 458 | 461 | const fde = self.fdes.items[i]; |
| 459 | const atom = fde.atom(elf_file); | |
| 462 | const atom_ptr = fde.atom(elf_file); | |
| 460 | 463 | const start = i; |
| 461 | 464 | i += 1; |
| 462 | 465 | while (i < self.fdes.items.len) : (i += 1) { |
| 463 | 466 | const next_fde = self.fdes.items[i]; |
| 464 | if (atom.atom_index != next_fde.atom(elf_file).atom_index) break; | |
| 467 | if (atom_ptr.atom_index != next_fde.atom(elf_file).atom_index) break; | |
| 465 | 468 | } |
| 466 | try atom.addExtra(.{ .fde_start = start, .fde_count = i - start }, elf_file); | |
| 467 | atom.flags.fde = true; | |
| 469 | atom_ptr.addExtra(.{ .fde_start = start, .fde_count = i - start }, elf_file); | |
| 470 | atom_ptr.flags.fde = true; | |
| 468 | 471 | } |
| 469 | 472 | } |
| 470 | 473 | |
| ... | ... | @@ -507,19 +510,19 @@ fn filterRelocs( |
| 507 | 510 | pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void { |
| 508 | 511 | const comp = elf_file.base.comp; |
| 509 | 512 | const gpa = comp.gpa; |
| 510 | for (self.atoms.items) |atom_index| { | |
| 511 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 512 | if (!atom.flags.alive) continue; | |
| 513 | const shdr = atom.inputShdr(elf_file); | |
| 513 | for (self.atoms_indexes.items) |atom_index| { | |
| 514 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 515 | if (!atom_ptr.flags.alive) continue; | |
| 516 | const shdr = atom_ptr.inputShdr(elf_file); | |
| 514 | 517 | if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue; |
| 515 | 518 | if (shdr.sh_type == elf.SHT_NOBITS) continue; |
| 516 | if (atom.scanRelocsRequiresCode(elf_file)) { | |
| 519 | if (atom_ptr.scanRelocsRequiresCode(elf_file)) { | |
| 517 | 520 | // TODO ideally, we don't have to decompress at this stage (should already be done) |
| 518 | 521 | // and we just fetch the code slice. |
| 519 | 522 | const code = try self.codeDecompressAlloc(elf_file, atom_index); |
| 520 | 523 | defer gpa.free(code); |
| 521 | try atom.scanRelocs(elf_file, code, undefs); | |
| 522 | } else try atom.scanRelocs(elf_file, null, undefs); | |
| 524 | try atom_ptr.scanRelocs(elf_file, code, undefs); | |
| 525 | } else try atom_ptr.scanRelocs(elf_file, null, undefs); | |
| 523 | 526 | } |
| 524 | 527 | |
| 525 | 528 | for (self.cies.items) |cie| { |
| ... | ... | @@ -547,19 +550,21 @@ pub fn resolveSymbols(self: *Object, elf_file: *Elf) void { |
| 547 | 550 | if (esym.st_shndx == elf.SHN_UNDEF) continue; |
| 548 | 551 | |
| 549 | 552 | if (esym.st_shndx != elf.SHN_ABS and esym.st_shndx != elf.SHN_COMMON) { |
| 550 | const atom_index = self.atoms.items[esym.st_shndx]; | |
| 551 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 552 | if (!atom.flags.alive) continue; | |
| 553 | const atom_index = self.atoms_indexes.items[esym.st_shndx]; | |
| 554 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 555 | if (!atom_ptr.flags.alive) continue; | |
| 553 | 556 | } |
| 554 | 557 | |
| 555 | 558 | const global = elf_file.symbol(index); |
| 556 | 559 | if (self.asFile().symbolRank(esym, !self.alive) < global.symbolRank(elf_file)) { |
| 557 | const atom_index = switch (esym.st_shndx) { | |
| 558 | elf.SHN_ABS, elf.SHN_COMMON => 0, | |
| 559 | else => self.atoms.items[esym.st_shndx], | |
| 560 | }; | |
| 560 | switch (esym.st_shndx) { | |
| 561 | elf.SHN_ABS, elf.SHN_COMMON => {}, | |
| 562 | else => global.atom_ref = .{ | |
| 563 | .index = self.atoms_indexes.items[esym.st_shndx], | |
| 564 | .file = self.index, | |
| 565 | }, | |
| 566 | } | |
| 561 | 567 | global.value = @intCast(esym.st_value); |
| 562 | global.atom_index = atom_index; | |
| 563 | 568 | global.esym_index = esym_index; |
| 564 | 569 | global.file_index = self.index; |
| 565 | 570 | global.version_index = elf_file.default_sym_version; |
| ... | ... | @@ -588,7 +593,7 @@ pub fn claimUnresolved(self: *Object, elf_file: *Elf) void { |
| 588 | 593 | }; |
| 589 | 594 | |
| 590 | 595 | global.value = 0; |
| 591 | global.atom_index = 0; | |
| 596 | global.atom_ref = .{ .index = 0, .file = 0 }; | |
| 592 | 597 | global.esym_index = esym_index; |
| 593 | 598 | global.file_index = self.index; |
| 594 | 599 | global.version_index = if (is_import) elf.VER_NDX_LOCAL else elf_file.default_sym_version; |
| ... | ... | @@ -609,7 +614,7 @@ pub fn claimUnresolvedObject(self: *Object, elf_file: *Elf) void { |
| 609 | 614 | } |
| 610 | 615 | |
| 611 | 616 | global.value = 0; |
| 612 | global.atom_index = 0; | |
| 617 | global.atom_ref = .{ .index = 0, .file = 0 }; | |
| 613 | 618 | global.esym_index = esym_index; |
| 614 | 619 | global.file_index = self.index; |
| 615 | 620 | } |
| ... | ... | @@ -633,13 +638,13 @@ pub fn markLive(self: *Object, elf_file: *Elf) void { |
| 633 | 638 | } |
| 634 | 639 | } |
| 635 | 640 | |
| 636 | pub fn markEhFrameAtomsDead(self: Object, elf_file: *Elf) void { | |
| 641 | pub fn markEhFrameAtomsDead(self: *Object, elf_file: *Elf) void { | |
| 637 | 642 | const cpu_arch = elf_file.getTarget().cpu.arch; |
| 638 | for (self.atoms.items) |atom_index| { | |
| 639 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 640 | const is_eh_frame = (cpu_arch == .x86_64 and atom.inputShdr(elf_file).sh_type == elf.SHT_X86_64_UNWIND) or | |
| 641 | mem.eql(u8, atom.name(elf_file), ".eh_frame"); | |
| 642 | if (atom.flags.alive and is_eh_frame) atom.flags.alive = false; | |
| 643 | for (self.atoms_indexes.items) |atom_index| { | |
| 644 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 645 | const is_eh_frame = (cpu_arch == .x86_64 and atom_ptr.inputShdr(elf_file).sh_type == elf.SHT_X86_64_UNWIND) or | |
| 646 | mem.eql(u8, atom_ptr.name(elf_file), ".eh_frame"); | |
| 647 | if (atom_ptr.flags.alive and is_eh_frame) atom_ptr.flags.alive = false; | |
| 643 | 648 | } |
| 644 | 649 | } |
| 645 | 650 | |
| ... | ... | @@ -657,9 +662,9 @@ pub fn checkDuplicates(self: *Object, dupes: anytype, elf_file: *Elf) error{OutO |
| 657 | 662 | sym.st_shndx == elf.SHN_COMMON) continue; |
| 658 | 663 | |
| 659 | 664 | if (sym.st_shndx != elf.SHN_ABS) { |
| 660 | const atom_index = self.atoms.items[sym.st_shndx]; | |
| 661 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 662 | if (!atom.flags.alive) continue; | |
| 665 | const atom_index = self.atoms_indexes.items[sym.st_shndx]; | |
| 666 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 667 | if (!atom_ptr.flags.alive) continue; | |
| 663 | 668 | } |
| 664 | 669 | |
| 665 | 670 | const gop = try dupes.getOrPut(index); |
| ... | ... | @@ -680,8 +685,8 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void { |
| 680 | 685 | for (self.shdrs.items, 0..) |shdr, shndx| { |
| 681 | 686 | if (shdr.sh_flags & elf.SHF_MERGE == 0) continue; |
| 682 | 687 | |
| 683 | const atom_index = self.atoms.items[shndx]; | |
| 684 | const atom_ptr = elf_file.atom(atom_index) orelse continue; | |
| 688 | const atom_index = self.atoms_indexes.items[shndx]; | |
| 689 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 685 | 690 | if (!atom_ptr.flags.alive) continue; |
| 686 | 691 | if (atom_ptr.relocs(elf_file).len > 0) continue; |
| 687 | 692 | |
| ... | ... | @@ -755,7 +760,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void { |
| 755 | 760 | const imsec = self.inputMergeSection(index) orelse continue; |
| 756 | 761 | if (imsec.offsets.items.len == 0) continue; |
| 757 | 762 | const msec = elf_file.mergeSection(imsec.merge_section_index); |
| 758 | const atom_ptr = elf_file.atom(imsec.atom_index).?; | |
| 763 | const atom_ptr = self.atom(imsec.atom_index).?; | |
| 759 | 764 | const isec = atom_ptr.inputShdr(elf_file); |
| 760 | 765 | |
| 761 | 766 | try imsec.subsections.resize(gpa, imsec.strings.items.len); |
| ... | ... | @@ -802,10 +807,10 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void { |
| 802 | 807 | sym.value = offset; |
| 803 | 808 | } |
| 804 | 809 | |
| 805 | for (self.atoms.items) |atom_index| { | |
| 806 | const atom_ptr = elf_file.atom(atom_index) orelse continue; | |
| 810 | for (self.atoms_indexes.items) |atom_index| { | |
| 811 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 807 | 812 | if (!atom_ptr.flags.alive) continue; |
| 808 | const extras = atom_ptr.extra(elf_file) orelse continue; | |
| 813 | const extras = atom_ptr.extra(elf_file); | |
| 809 | 814 | const relocs = self.relocs.items[extras.rel_index..][0..extras.rel_count]; |
| 810 | 815 | for (relocs) |*rel| { |
| 811 | 816 | const esym = self.symtab.items[rel.r_sym()]; |
| ... | ... | @@ -867,21 +872,10 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void { |
| 867 | 872 | const comp = elf_file.base.comp; |
| 868 | 873 | const gpa = comp.gpa; |
| 869 | 874 | |
| 870 | const atom_index = try elf_file.addAtom(); | |
| 871 | try self.atoms.append(gpa, atom_index); | |
| 872 | ||
| 873 | 875 | const is_tls = global.type(elf_file) == elf.STT_TLS; |
| 874 | 876 | const name = if (is_tls) ".tls_common" else ".common"; |
| 875 | ||
| 876 | const atom = elf_file.atom(atom_index).?; | |
| 877 | 877 | const name_offset = @as(u32, @intCast(self.strtab.items.len)); |
| 878 | 878 | try self.strtab.writer(gpa).print("{s}\x00", .{name}); |
| 879 | atom.atom_index = atom_index; | |
| 880 | atom.name_offset = name_offset; | |
| 881 | atom.file_index = self.index; | |
| 882 | atom.size = this_sym.st_size; | |
| 883 | const alignment = this_sym.st_value; | |
| 884 | atom.alignment = Alignment.fromNonzeroByteUnits(alignment); | |
| 885 | 879 | |
| 886 | 880 | var sh_flags: u32 = elf.SHF_ALLOC | elf.SHF_WRITE; |
| 887 | 881 | if (is_tls) sh_flags |= elf.SHF_TLS; |
| ... | ... | @@ -897,38 +891,45 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void { |
| 897 | 891 | .sh_size = sh_size, |
| 898 | 892 | .sh_link = 0, |
| 899 | 893 | .sh_info = 0, |
| 900 | .sh_addralign = alignment, | |
| 894 | .sh_addralign = this_sym.st_value, | |
| 901 | 895 | .sh_entsize = 0, |
| 902 | 896 | }; |
| 903 | atom.input_section_index = shndx; | |
| 897 | ||
| 898 | const atom_index = try self.addAtom(gpa, .{ | |
| 899 | .name = name_offset, | |
| 900 | .shndx = shndx, | |
| 901 | .size = this_sym.st_size, | |
| 902 | .alignment = Alignment.fromNonzeroByteUnits(this_sym.st_value), | |
| 903 | }); | |
| 904 | try self.atoms_indexes.append(gpa, atom_index); | |
| 904 | 905 | |
| 905 | 906 | global.value = 0; |
| 906 | global.atom_index = atom_index; | |
| 907 | global.atom_ref = .{ .index = atom_index, .file = self.index }; | |
| 907 | 908 | global.flags.weak = false; |
| 908 | 909 | } |
| 909 | 910 | } |
| 910 | 911 | |
| 911 | pub fn initOutputSections(self: Object, elf_file: *Elf) !void { | |
| 912 | for (self.atoms.items) |atom_index| { | |
| 913 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 914 | if (!atom.flags.alive) continue; | |
| 915 | const shdr = atom.inputShdr(elf_file); | |
| 912 | pub fn initOutputSections(self: *Object, elf_file: *Elf) !void { | |
| 913 | for (self.atoms_indexes.items) |atom_index| { | |
| 914 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 915 | if (!atom_ptr.flags.alive) continue; | |
| 916 | const shdr = atom_ptr.inputShdr(elf_file); | |
| 916 | 917 | _ = try self.initOutputSection(elf_file, shdr); |
| 917 | 918 | } |
| 918 | 919 | } |
| 919 | 920 | |
| 920 | 921 | pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void { |
| 921 | for (self.atoms.items) |atom_index| { | |
| 922 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 923 | if (!atom.flags.alive) continue; | |
| 924 | const shdr = atom.inputShdr(elf_file); | |
| 925 | atom.output_section_index = self.initOutputSection(elf_file, shdr) catch unreachable; | |
| 922 | for (self.atoms_indexes.items) |atom_index| { | |
| 923 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 924 | if (!atom_ptr.flags.alive) continue; | |
| 925 | const shdr = atom_ptr.inputShdr(elf_file); | |
| 926 | atom_ptr.output_section_index = self.initOutputSection(elf_file, shdr) catch unreachable; | |
| 926 | 927 | |
| 927 | 928 | const comp = elf_file.base.comp; |
| 928 | 929 | const gpa = comp.gpa; |
| 929 | const gop = try elf_file.output_sections.getOrPut(gpa, atom.output_section_index); | |
| 930 | const gop = try elf_file.output_sections.getOrPut(gpa, atom_ptr.output_section_index); | |
| 930 | 931 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 931 | try gop.value_ptr.append(gpa, atom_index); | |
| 932 | try gop.value_ptr.append(gpa, .{ .index = atom_index, .file = self.index }); | |
| 932 | 933 | } |
| 933 | 934 | |
| 934 | 935 | for (self.locals()) |local_index| { |
| ... | ... | @@ -938,9 +939,9 @@ pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void { |
| 938 | 939 | local.output_section_index = msub.mergeSection(elf_file).output_section_index; |
| 939 | 940 | continue; |
| 940 | 941 | } |
| 941 | const atom = local.atom(elf_file) orelse continue; | |
| 942 | if (!atom.flags.alive) continue; | |
| 943 | local.output_section_index = atom.output_section_index; | |
| 942 | const atom_ptr = local.atom(elf_file) orelse continue; | |
| 943 | if (!atom_ptr.flags.alive) continue; | |
| 944 | local.output_section_index = atom_ptr.output_section_index; | |
| 944 | 945 | } |
| 945 | 946 | |
| 946 | 947 | for (self.globals()) |global_index| { |
| ... | ... | @@ -951,9 +952,9 @@ pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void { |
| 951 | 952 | global.output_section_index = msub.mergeSection(elf_file).output_section_index; |
| 952 | 953 | continue; |
| 953 | 954 | } |
| 954 | const atom = global.atom(elf_file) orelse continue; | |
| 955 | if (!atom.flags.alive) continue; | |
| 956 | global.output_section_index = atom.output_section_index; | |
| 955 | const atom_ptr = global.atom(elf_file) orelse continue; | |
| 956 | if (!atom_ptr.flags.alive) continue; | |
| 957 | global.output_section_index = atom_ptr.output_section_index; | |
| 957 | 958 | } |
| 958 | 959 | |
| 959 | 960 | for (self.symbols.items[self.symtab.items.len..]) |local_index| { |
| ... | ... | @@ -964,11 +965,11 @@ pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void { |
| 964 | 965 | } |
| 965 | 966 | } |
| 966 | 967 | |
| 967 | pub fn initRelaSections(self: Object, elf_file: *Elf) !void { | |
| 968 | for (self.atoms.items) |atom_index| { | |
| 969 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 970 | if (!atom.flags.alive) continue; | |
| 971 | const shndx = atom.relocsShndx() orelse continue; | |
| 968 | pub fn initRelaSections(self: *Object, elf_file: *Elf) !void { | |
| 969 | for (self.atoms_indexes.items) |atom_index| { | |
| 970 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 971 | if (!atom_ptr.flags.alive) continue; | |
| 972 | const shndx = atom_ptr.relocsShndx() orelse continue; | |
| 972 | 973 | const shdr = self.shdrs.items[shndx]; |
| 973 | 974 | const out_shndx = try self.initOutputSection(elf_file, shdr); |
| 974 | 975 | const out_shdr = &elf_file.shdrs.items[out_shndx]; |
| ... | ... | @@ -978,24 +979,24 @@ pub fn initRelaSections(self: Object, elf_file: *Elf) !void { |
| 978 | 979 | } |
| 979 | 980 | } |
| 980 | 981 | |
| 981 | pub fn addAtomsToRelaSections(self: Object, elf_file: *Elf) !void { | |
| 982 | for (self.atoms.items) |atom_index| { | |
| 983 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 984 | if (!atom.flags.alive) continue; | |
| 982 | pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void { | |
| 983 | for (self.atoms_indexes.items) |atom_index| { | |
| 984 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 985 | if (!atom_ptr.flags.alive) continue; | |
| 985 | 986 | const shndx = blk: { |
| 986 | const shndx = atom.relocsShndx() orelse continue; | |
| 987 | const shndx = atom_ptr.relocsShndx() orelse continue; | |
| 987 | 988 | const shdr = self.shdrs.items[shndx]; |
| 988 | 989 | break :blk self.initOutputSection(elf_file, shdr) catch unreachable; |
| 989 | 990 | }; |
| 990 | 991 | const shdr = &elf_file.shdrs.items[shndx]; |
| 991 | shdr.sh_info = atom.outputShndx().?; | |
| 992 | shdr.sh_info = atom_ptr.outputShndx().?; | |
| 992 | 993 | shdr.sh_link = elf_file.symtab_section_index.?; |
| 993 | 994 | |
| 994 | 995 | const comp = elf_file.base.comp; |
| 995 | 996 | const gpa = comp.gpa; |
| 996 | const gop = try elf_file.output_rela_sections.getOrPut(gpa, atom.outputShndx().?); | |
| 997 | const gop = try elf_file.output_rela_sections.getOrPut(gpa, atom_ptr.outputShndx().?); | |
| 997 | 998 | if (!gop.found_existing) gop.value_ptr.* = .{ .shndx = shndx }; |
| 998 | try gop.value_ptr.atom_list.append(gpa, atom_index); | |
| 999 | try gop.value_ptr.atom_list.append(gpa, .{ .index = atom_index, .file = self.index }); | |
| 999 | 1000 | } |
| 1000 | 1001 | } |
| 1001 | 1002 | |
| ... | ... | @@ -1129,11 +1130,10 @@ pub fn globals(self: Object) []const Symbol.Index { |
| 1129 | 1130 | |
| 1130 | 1131 | /// Returns atom's code and optionally uncompresses data if required (for compressed sections). |
| 1131 | 1132 | /// Caller owns the memory. |
| 1132 | pub fn codeDecompressAlloc(self: Object, elf_file: *Elf, atom_index: Atom.Index) ![]u8 { | |
| 1133 | pub fn codeDecompressAlloc(self: *Object, elf_file: *Elf, atom_index: Atom.Index) ![]u8 { | |
| 1133 | 1134 | const comp = elf_file.base.comp; |
| 1134 | 1135 | const gpa = comp.gpa; |
| 1135 | const atom_ptr = elf_file.atom(atom_index).?; | |
| 1136 | assert(atom_ptr.file_index == self.index); | |
| 1136 | const atom_ptr = self.atom(atom_index).?; | |
| 1137 | 1137 | const shdr = atom_ptr.inputShdr(elf_file); |
| 1138 | 1138 | const handle = elf_file.fileHandle(self.file_handle); |
| 1139 | 1139 | const data = try self.preadShdrContentsAlloc(gpa, handle, atom_ptr.input_section_index); |
| ... | ... | @@ -1194,6 +1194,82 @@ fn preadRelocsAlloc(self: Object, allocator: Allocator, handle: std.fs.File, shn |
| 1194 | 1194 | return @as([*]align(1) const elf.Elf64_Rela, @ptrCast(raw.ptr))[0..num]; |
| 1195 | 1195 | } |
| 1196 | 1196 | |
| 1197 | const AddAtomArgs = struct { | |
| 1198 | name: u32, | |
| 1199 | shndx: u32, | |
| 1200 | size: u64, | |
| 1201 | alignment: Alignment, | |
| 1202 | }; | |
| 1203 | ||
| 1204 | fn addAtom(self: *Object, allocator: Allocator, args: AddAtomArgs) !Atom.Index { | |
| 1205 | try self.atoms.ensureUnusedCapacity(allocator, 1); | |
| 1206 | try self.atoms_extra.ensureUnusedCapacity(allocator, @sizeOf(Atom.Extra)); | |
| 1207 | return self.addAtomAssumeCapacity(args); | |
| 1208 | } | |
| 1209 | ||
| 1210 | fn addAtomAssumeCapacity(self: *Object, args: AddAtomArgs) Atom.Index { | |
| 1211 | const atom_index: Atom.Index = @intCast(self.atoms.items.len); | |
| 1212 | const atom_ptr = self.atoms.addOneAssumeCapacity(); | |
| 1213 | atom_ptr.* = .{ | |
| 1214 | .atom_index = atom_index, | |
| 1215 | .name_offset = args.name, | |
| 1216 | .file_index = self.index, | |
| 1217 | .input_section_index = args.shndx, | |
| 1218 | .extra_index = self.addAtomExtraAssumeCapacity(.{}), | |
| 1219 | .size = args.size, | |
| 1220 | .alignment = args.alignment, | |
| 1221 | }; | |
| 1222 | return atom_index; | |
| 1223 | } | |
| 1224 | ||
| 1225 | pub fn atom(self: *Object, atom_index: Atom.Index) ?*Atom { | |
| 1226 | if (atom_index == 0) return null; | |
| 1227 | assert(atom_index < self.atoms.items.len); | |
| 1228 | return &self.atoms.items[atom_index]; | |
| 1229 | } | |
| 1230 | ||
| 1231 | pub fn addAtomExtra(self: *Object, allocator: Allocator, extra: Atom.Extra) !u32 { | |
| 1232 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 1233 | try self.atoms_extra.ensureUnusedCapacity(allocator, fields.len); | |
| 1234 | return self.addAtomExtraAssumeCapacity(extra); | |
| 1235 | } | |
| 1236 | ||
| 1237 | pub fn addAtomExtraAssumeCapacity(self: *Object, extra: Atom.Extra) u32 { | |
| 1238 | const index = @as(u32, @intCast(self.atoms_extra.items.len)); | |
| 1239 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 1240 | inline for (fields) |field| { | |
| 1241 | self.atoms_extra.appendAssumeCapacity(switch (field.type) { | |
| 1242 | u32 => @field(extra, field.name), | |
| 1243 | else => @compileError("bad field type"), | |
| 1244 | }); | |
| 1245 | } | |
| 1246 | return index; | |
| 1247 | } | |
| 1248 | ||
| 1249 | pub fn atomExtra(self: *Object, index: u32) Atom.Extra { | |
| 1250 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 1251 | var i: usize = index; | |
| 1252 | var result: Atom.Extra = undefined; | |
| 1253 | inline for (fields) |field| { | |
| 1254 | @field(result, field.name) = switch (field.type) { | |
| 1255 | u32 => self.atoms_extra.items[i], | |
| 1256 | else => @compileError("bad field type"), | |
| 1257 | }; | |
| 1258 | i += 1; | |
| 1259 | } | |
| 1260 | return result; | |
| 1261 | } | |
| 1262 | ||
| 1263 | pub fn setAtomExtra(self: *Object, index: u32, extra: Atom.Extra) void { | |
| 1264 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 1265 | inline for (fields, 0..) |field, i| { | |
| 1266 | self.atoms_extra.items[index + i] = switch (field.type) { | |
| 1267 | u32 => @field(extra, field.name), | |
| 1268 | else => @compileError("bad field type"), | |
| 1269 | }; | |
| 1270 | } | |
| 1271 | } | |
| 1272 | ||
| 1197 | 1273 | fn addInputMergeSection(self: *Object, allocator: Allocator) !InputMergeSection.Index { |
| 1198 | 1274 | const index: InputMergeSection.Index = @intCast(self.input_merge_sections.items.len); |
| 1199 | 1275 | const msec = try self.input_merge_sections.addOne(allocator); |
| ... | ... | @@ -1280,9 +1356,9 @@ fn formatAtoms( |
| 1280 | 1356 | _ = options; |
| 1281 | 1357 | const object = ctx.object; |
| 1282 | 1358 | try writer.writeAll(" atoms\n"); |
| 1283 | for (object.atoms.items) |atom_index| { | |
| 1284 | const atom = ctx.elf_file.atom(atom_index) orelse continue; | |
| 1285 | try writer.print(" {}\n", .{atom.fmt(ctx.elf_file)}); | |
| 1359 | for (object.atoms_indexes.items) |atom_index| { | |
| 1360 | const atom_ptr = object.atom(atom_index) orelse continue; | |
| 1361 | try writer.print(" {}\n", .{atom_ptr.fmt(ctx.elf_file)}); | |
| 1286 | 1362 | } |
| 1287 | 1363 | } |
| 1288 | 1364 | |
| ... | ... | @@ -1354,9 +1430,9 @@ fn formatComdatGroups( |
| 1354 | 1430 | try writer.print(" COMDAT({d})\n", .{cg_index}); |
| 1355 | 1431 | const cg_members = cg.comdatGroupMembers(elf_file); |
| 1356 | 1432 | for (cg_members) |shndx| { |
| 1357 | const atom_index = object.atoms.items[shndx]; | |
| 1358 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 1359 | try writer.print(" atom({d}) : {s}\n", .{ atom_index, atom.name(elf_file) }); | |
| 1433 | const atom_index = object.atoms_indexes.items[shndx]; | |
| 1434 | const atom_ptr = object.atom(atom_index) orelse continue; | |
| 1435 | try writer.print(" atom({d}) : {s}\n", .{ atom_index, atom_ptr.name(elf_file) }); | |
| 1360 | 1436 | } |
| 1361 | 1437 | } |
| 1362 | 1438 | } |
src/link/Elf/SharedObject.zig+1-1| ... | ... | @@ -232,7 +232,7 @@ pub fn resolveSymbols(self: *SharedObject, elf_file: *Elf) void { |
| 232 | 232 | const global = elf_file.symbol(index); |
| 233 | 233 | if (self.asFile().symbolRank(this_sym, false) < global.symbolRank(elf_file)) { |
| 234 | 234 | global.value = @intCast(this_sym.st_value); |
| 235 | global.atom_index = 0; | |
| 235 | global.atom_ref = .{ .index = 0, .file = 0 }; | |
| 236 | 236 | global.esym_index = esym_index; |
| 237 | 237 | global.version_index = self.versyms.items[esym_index]; |
| 238 | 238 | global.file_index = self.index; |
src/link/Elf/Symbol.zig+4-4| ... | ... | @@ -9,10 +9,9 @@ name_offset: u32 = 0, |
| 9 | 9 | /// Index of file where this symbol is defined. |
| 10 | 10 | file_index: File.Index = 0, |
| 11 | 11 | |
| 12 | /// Index of atom containing this symbol. | |
| 13 | /// Index of 0 means there is no associated atom with this symbol. | |
| 12 | /// Reference to Atom containing this symbol if any. | |
| 14 | 13 | /// Use `atom` to get the pointer to the atom. |
| 15 | atom_index: Atom.Index = 0, | |
| 14 | atom_ref: Elf.Ref = .{ .index = 0, .file = 0 }, | |
| 16 | 15 | |
| 17 | 16 | /// Assigned output section index for this symbol. |
| 18 | 17 | output_section_index: u32 = 0, |
| ... | ... | @@ -68,7 +67,8 @@ pub fn name(symbol: Symbol, elf_file: *Elf) [:0]const u8 { |
| 68 | 67 | } |
| 69 | 68 | |
| 70 | 69 | pub fn atom(symbol: Symbol, elf_file: *Elf) ?*Atom { |
| 71 | return elf_file.atom(symbol.atom_index); | |
| 70 | const file_ptr = elf_file.file(symbol.atom_ref.file) orelse return null; | |
| 71 | return file_ptr.atom(symbol.atom_ref.index); | |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | pub fn mergeSubsection(symbol: Symbol, elf_file: *Elf) ?*MergeSubsection { |
src/link/Elf/ZigObject.zig+124-61| ... | ... | @@ -15,7 +15,9 @@ local_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 15 | 15 | global_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 16 | 16 | globals_lookup: std.AutoHashMapUnmanaged(u32, Symbol.Index) = .{}, |
| 17 | 17 | |
| 18 | atoms: std.ArrayListUnmanaged(Atom.Index) = .{}, | |
| 18 | atoms: std.ArrayListUnmanaged(Atom) = .{}, | |
| 19 | atoms_indexes: std.ArrayListUnmanaged(Atom.Index) = .{}, | |
| 20 | atoms_extra: std.ArrayListUnmanaged(u32) = .{}, | |
| 19 | 21 | relocs: std.ArrayListUnmanaged(std.ArrayListUnmanaged(elf.Elf64_Rela)) = .{}, |
| 20 | 22 | |
| 21 | 23 | num_dynrelocs: u32 = 0, |
| ... | ... | @@ -80,7 +82,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf) !void { |
| 80 | 82 | const comp = elf_file.base.comp; |
| 81 | 83 | const gpa = comp.gpa; |
| 82 | 84 | |
| 83 | try self.atoms.append(gpa, 0); // null input section | |
| 85 | try self.atoms.append(gpa, .{ .extra_index = try self.addAtomExtra(gpa, .{}) }); // null input section | |
| 84 | 86 | try self.relocs.append(gpa, .{}); // null relocs section |
| 85 | 87 | try self.strtab.buffer.append(gpa, 0); |
| 86 | 88 | |
| ... | ... | @@ -117,6 +119,8 @@ pub fn deinit(self: *ZigObject, allocator: Allocator) void { |
| 117 | 119 | self.global_symbols.deinit(allocator); |
| 118 | 120 | self.globals_lookup.deinit(allocator); |
| 119 | 121 | self.atoms.deinit(allocator); |
| 122 | self.atoms_indexes.deinit(allocator); | |
| 123 | self.atoms_extra.deinit(allocator); | |
| 120 | 124 | for (self.relocs.items) |*list| { |
| 121 | 125 | list.deinit(allocator); |
| 122 | 126 | } |
| ... | ... | @@ -276,24 +280,20 @@ pub fn addGlobalEsym(self: *ZigObject, allocator: Allocator) !Symbol.Index { |
| 276 | 280 | return index | global_symbol_bit; |
| 277 | 281 | } |
| 278 | 282 | |
| 279 | pub fn addAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index { | |
| 283 | pub fn newAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index { | |
| 280 | 284 | const gpa = elf_file.base.comp.gpa; |
| 281 | const atom_index = try elf_file.addAtom(); | |
| 285 | const atom_index = try self.addAtom(gpa); | |
| 282 | 286 | const symbol_index = try elf_file.addSymbol(); |
| 283 | 287 | const esym_index = try self.addLocalEsym(gpa); |
| 284 | 288 | |
| 285 | const shndx = @as(u32, @intCast(self.atoms.items.len)); | |
| 286 | try self.atoms.append(gpa, atom_index); | |
| 289 | try self.atoms_indexes.append(gpa, atom_index); | |
| 287 | 290 | try self.local_symbols.append(gpa, symbol_index); |
| 288 | 291 | |
| 289 | const atom_ptr = elf_file.atom(atom_index).?; | |
| 290 | atom_ptr.file_index = self.index; | |
| 291 | ||
| 292 | 292 | const symbol_ptr = elf_file.symbol(symbol_index); |
| 293 | 293 | symbol_ptr.file_index = self.index; |
| 294 | symbol_ptr.atom_index = atom_index; | |
| 294 | symbol_ptr.atom_ref = .{ .index = atom_index, .file = self.index }; | |
| 295 | 295 | |
| 296 | self.local_esyms.items(.shndx)[esym_index] = shndx; | |
| 296 | self.local_esyms.items(.shndx)[esym_index] = atom_index; | |
| 297 | 297 | self.local_esyms.items(.elf_sym)[esym_index].st_shndx = SHN_ATOM; |
| 298 | 298 | symbol_ptr.esym_index = esym_index; |
| 299 | 299 | |
| ... | ... | @@ -301,21 +301,22 @@ pub fn addAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index { |
| 301 | 301 | const relocs_index = @as(u32, @intCast(self.relocs.items.len)); |
| 302 | 302 | const relocs = try self.relocs.addOne(gpa); |
| 303 | 303 | relocs.* = .{}; |
| 304 | ||
| 305 | const atom_ptr = self.atom(atom_index).?; | |
| 304 | 306 | atom_ptr.relocs_section_index = relocs_index; |
| 305 | 307 | |
| 306 | 308 | return symbol_index; |
| 307 | 309 | } |
| 308 | 310 | |
| 309 | 311 | /// TODO actually create fake input shdrs and return that instead. |
| 310 | pub fn inputShdr(self: ZigObject, atom_index: Atom.Index, elf_file: *Elf) elf.Elf64_Shdr { | |
| 311 | _ = self; | |
| 312 | const atom = elf_file.atom(atom_index) orelse return Elf.null_shdr; | |
| 313 | const shndx = atom.outputShndx() orelse return Elf.null_shdr; | |
| 312 | pub fn inputShdr(self: *ZigObject, atom_index: Atom.Index, elf_file: *Elf) elf.Elf64_Shdr { | |
| 313 | const atom_ptr = self.atom(atom_index) orelse return Elf.null_shdr; | |
| 314 | const shndx = atom_ptr.outputShndx() orelse return Elf.null_shdr; | |
| 314 | 315 | var shdr = elf_file.shdrs.items[shndx]; |
| 315 | 316 | shdr.sh_addr = 0; |
| 316 | 317 | shdr.sh_offset = 0; |
| 317 | shdr.sh_size = atom.size; | |
| 318 | shdr.sh_addralign = atom.alignment.toByteUnits() orelse 1; | |
| 318 | shdr.sh_size = atom_ptr.size; | |
| 319 | shdr.sh_addralign = atom_ptr.alignment.toByteUnits() orelse 1; | |
| 319 | 320 | return shdr; |
| 320 | 321 | } |
| 321 | 322 | |
| ... | ... | @@ -329,24 +330,23 @@ pub fn resolveSymbols(self: *ZigObject, elf_file: *Elf) void { |
| 329 | 330 | |
| 330 | 331 | if (esym.st_shndx != elf.SHN_ABS and esym.st_shndx != elf.SHN_COMMON) { |
| 331 | 332 | assert(esym.st_shndx == SHN_ATOM); |
| 332 | const atom_index = self.atoms.items[shndx]; | |
| 333 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 334 | if (!atom.flags.alive) continue; | |
| 333 | const atom_ptr = self.atom(shndx) orelse continue; | |
| 334 | if (!atom_ptr.flags.alive) continue; | |
| 335 | 335 | } |
| 336 | 336 | |
| 337 | 337 | const global = elf_file.symbol(index); |
| 338 | 338 | if (self.asFile().symbolRank(esym, false) < global.symbolRank(elf_file)) { |
| 339 | 339 | const atom_index = switch (esym.st_shndx) { |
| 340 | 340 | elf.SHN_ABS, elf.SHN_COMMON => 0, |
| 341 | SHN_ATOM => self.atoms.items[shndx], | |
| 341 | SHN_ATOM => shndx, | |
| 342 | 342 | else => unreachable, |
| 343 | 343 | }; |
| 344 | const output_section_index = if (elf_file.atom(atom_index)) |atom| | |
| 345 | atom.outputShndx().? | |
| 344 | const output_section_index = if (self.atom(atom_index)) |atom_ptr| | |
| 345 | atom_ptr.outputShndx().? | |
| 346 | 346 | else |
| 347 | 347 | elf.SHN_UNDEF; |
| 348 | 348 | global.value = @intCast(esym.st_value); |
| 349 | global.atom_index = atom_index; | |
| 349 | global.atom_ref = .{ .index = atom_index, .file = self.index }; | |
| 350 | 350 | global.esym_index = esym_index; |
| 351 | 351 | global.file_index = self.index; |
| 352 | 352 | global.output_section_index = output_section_index; |
| ... | ... | @@ -376,7 +376,7 @@ pub fn claimUnresolved(self: ZigObject, elf_file: *Elf) void { |
| 376 | 376 | }; |
| 377 | 377 | |
| 378 | 378 | global.value = 0; |
| 379 | global.atom_index = 0; | |
| 379 | global.atom_ref = .{ .index = 0, .file = 0 }; | |
| 380 | 380 | global.esym_index = esym_index; |
| 381 | 381 | global.file_index = self.index; |
| 382 | 382 | global.version_index = if (is_import) elf.VER_NDX_LOCAL else elf_file.default_sym_version; |
| ... | ... | @@ -397,7 +397,7 @@ pub fn claimUnresolvedObject(self: ZigObject, elf_file: *Elf) void { |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | global.value = 0; |
| 400 | global.atom_index = 0; | |
| 400 | global.atom_ref = .{ .index = 0, .file = 0 }; | |
| 401 | 401 | global.esym_index = esym_index; |
| 402 | 402 | global.file_index = self.index; |
| 403 | 403 | } |
| ... | ... | @@ -405,19 +405,19 @@ pub fn claimUnresolvedObject(self: ZigObject, elf_file: *Elf) void { |
| 405 | 405 | |
| 406 | 406 | pub fn scanRelocs(self: *ZigObject, elf_file: *Elf, undefs: anytype) !void { |
| 407 | 407 | const gpa = elf_file.base.comp.gpa; |
| 408 | for (self.atoms.items) |atom_index| { | |
| 409 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 410 | if (!atom.flags.alive) continue; | |
| 411 | const shdr = atom.inputShdr(elf_file); | |
| 408 | for (self.atoms_indexes.items) |atom_index| { | |
| 409 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 410 | if (!atom_ptr.flags.alive) continue; | |
| 411 | const shdr = atom_ptr.inputShdr(elf_file); | |
| 412 | 412 | if (shdr.sh_type == elf.SHT_NOBITS) continue; |
| 413 | if (atom.scanRelocsRequiresCode(elf_file)) { | |
| 413 | if (atom_ptr.scanRelocsRequiresCode(elf_file)) { | |
| 414 | 414 | // TODO ideally we don't have to fetch the code here. |
| 415 | 415 | // Perhaps it would make sense to save the code until flushModule where we |
| 416 | 416 | // would free all of generated code? |
| 417 | 417 | const code = try self.codeAlloc(elf_file, atom_index); |
| 418 | 418 | defer gpa.free(code); |
| 419 | try atom.scanRelocs(elf_file, code, undefs); | |
| 420 | } else try atom.scanRelocs(elf_file, null, undefs); | |
| 419 | try atom_ptr.scanRelocs(elf_file, code, undefs); | |
| 420 | } else try atom_ptr.scanRelocs(elf_file, null, undefs); | |
| 421 | 421 | } |
| 422 | 422 | } |
| 423 | 423 | |
| ... | ... | @@ -450,9 +450,8 @@ pub fn checkDuplicates(self: *ZigObject, dupes: anytype, elf_file: *Elf) error{O |
| 450 | 450 | esym.st_shndx == elf.SHN_COMMON) continue; |
| 451 | 451 | |
| 452 | 452 | if (esym.st_shndx == SHN_ATOM) { |
| 453 | const atom_index = self.atoms.items[shndx]; | |
| 454 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 455 | if (!atom.flags.alive) continue; | |
| 453 | const atom_ptr = self.atom(shndx) orelse continue; | |
| 454 | if (!atom_ptr.flags.alive) continue; | |
| 456 | 455 | } |
| 457 | 456 | |
| 458 | 457 | const gop = try dupes.getOrPut(index); |
| ... | ... | @@ -517,20 +516,20 @@ pub fn writeAr(self: ZigObject, writer: anytype) !void { |
| 517 | 516 | try writer.writeAll(self.data.items); |
| 518 | 517 | } |
| 519 | 518 | |
| 520 | pub fn addAtomsToRelaSections(self: ZigObject, elf_file: *Elf) !void { | |
| 521 | for (self.atoms.items) |atom_index| { | |
| 522 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 523 | if (!atom.flags.alive) continue; | |
| 524 | const rela_shndx = atom.relocsShndx() orelse continue; | |
| 519 | pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void { | |
| 520 | for (self.atoms_indexes.items) |atom_index| { | |
| 521 | const atom_ptr = self.atom(atom_index) orelse continue; | |
| 522 | if (!atom_ptr.flags.alive) continue; | |
| 523 | const rela_shndx = atom_ptr.relocsShndx() orelse continue; | |
| 525 | 524 | // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level |
| 526 | 525 | if (self.relocs.items[rela_shndx].items.len == 0) continue; |
| 527 | const out_shndx = atom.outputShndx().?; | |
| 526 | const out_shndx = atom_ptr.outputShndx().?; | |
| 528 | 527 | const out_shdr = elf_file.shdrs.items[out_shndx]; |
| 529 | 528 | if (out_shdr.sh_type == elf.SHT_NOBITS) continue; |
| 530 | 529 | |
| 531 | 530 | const gpa = elf_file.base.comp.gpa; |
| 532 | 531 | const sec = elf_file.output_rela_sections.getPtr(out_shndx).?; |
| 533 | try sec.atom_list.append(gpa, atom_index); | |
| 532 | try sec.atom_list.append(gpa, .{ .index = atom_index, .file = self.index }); | |
| 534 | 533 | } |
| 535 | 534 | } |
| 536 | 535 | |
| ... | ... | @@ -561,7 +560,7 @@ pub fn globals(self: ZigObject) []const Symbol.Index { |
| 561 | 560 | pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void { |
| 562 | 561 | for (self.locals()) |local_index| { |
| 563 | 562 | const local = elf_file.symbol(local_index); |
| 564 | if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue; | |
| 563 | if (local.atom(elf_file)) |atom_ptr| if (!atom_ptr.flags.alive) continue; | |
| 565 | 564 | const esym = local.elfSym(elf_file); |
| 566 | 565 | switch (esym.st_type()) { |
| 567 | 566 | elf.STT_SECTION, elf.STT_NOTYPE => continue, |
| ... | ... | @@ -577,7 +576,7 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void { |
| 577 | 576 | const global = elf_file.symbol(global_index); |
| 578 | 577 | const file_ptr = global.file(elf_file) orelse continue; |
| 579 | 578 | if (file_ptr.index() != self.index) continue; |
| 580 | if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue; | |
| 579 | if (global.atom(elf_file)) |atom_ptr| if (!atom_ptr.flags.alive) continue; | |
| 581 | 580 | global.flags.output_symtab = true; |
| 582 | 581 | if (global.isLocal(elf_file)) { |
| 583 | 582 | try global.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file); |
| ... | ... | @@ -621,11 +620,10 @@ pub fn asFile(self: *ZigObject) File { |
| 621 | 620 | |
| 622 | 621 | /// Returns atom's code. |
| 623 | 622 | /// Caller owns the memory. |
| 624 | pub fn codeAlloc(self: ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8 { | |
| 623 | pub fn codeAlloc(self: *ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8 { | |
| 625 | 624 | const gpa = elf_file.base.comp.gpa; |
| 626 | const atom = elf_file.atom(atom_index).?; | |
| 627 | assert(atom.file_index == self.index); | |
| 628 | const shdr = &elf_file.shdrs.items[atom.outputShndx().?]; | |
| 625 | const atom_ptr = self.atom(atom_index).?; | |
| 626 | const shdr = &elf_file.shdrs.items[atom_ptr.outputShndx().?]; | |
| 629 | 627 | |
| 630 | 628 | if (shdr.sh_flags & elf.SHF_TLS != 0) { |
| 631 | 629 | const tlv = self.tls_variables.get(atom_index).?; |
| ... | ... | @@ -633,13 +631,13 @@ pub fn codeAlloc(self: ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8 |
| 633 | 631 | return code; |
| 634 | 632 | } |
| 635 | 633 | |
| 636 | const file_offset = shdr.sh_offset + @as(u64, @intCast(atom.value)); | |
| 637 | const size = std.math.cast(usize, atom.size) orelse return error.Overflow; | |
| 634 | const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value)); | |
| 635 | const size = std.math.cast(usize, atom_ptr.size) orelse return error.Overflow; | |
| 638 | 636 | const code = try gpa.alloc(u8, size); |
| 639 | 637 | errdefer gpa.free(code); |
| 640 | 638 | const amt = try elf_file.base.file.?.preadAll(code, file_offset); |
| 641 | 639 | if (amt != code.len) { |
| 642 | log.err("fetching code for {s} failed", .{atom.name(elf_file)}); | |
| 640 | log.err("fetching code for {s} failed", .{atom_ptr.name(elf_file)}); | |
| 643 | 641 | return error.InputOutput; |
| 644 | 642 | } |
| 645 | 643 | return code; |
| ... | ... | @@ -760,7 +758,7 @@ pub fn getOrCreateMetadataForLazySymbol( |
| 760 | 758 | }; |
| 761 | 759 | switch (metadata.state.*) { |
| 762 | 760 | .unused => { |
| 763 | const symbol_index = try self.addAtom(elf_file); | |
| 761 | const symbol_index = try self.newAtom(elf_file); | |
| 764 | 762 | const sym = elf_file.symbol(symbol_index); |
| 765 | 763 | sym.flags.needs_zig_got = true; |
| 766 | 764 | metadata.symbol_index.* = symbol_index; |
| ... | ... | @@ -824,7 +822,7 @@ pub fn getOrCreateMetadataForDecl( |
| 824 | 822 | const gop = try self.decls.getOrPut(gpa, decl_index); |
| 825 | 823 | if (!gop.found_existing) { |
| 826 | 824 | const any_non_single_threaded = elf_file.base.comp.config.any_non_single_threaded; |
| 827 | const symbol_index = try self.addAtom(elf_file); | |
| 825 | const symbol_index = try self.newAtom(elf_file); | |
| 828 | 826 | const mod = elf_file.base.comp.module.?; |
| 829 | 827 | const decl = mod.declPtr(decl_index); |
| 830 | 828 | const sym = elf_file.symbol(symbol_index); |
| ... | ... | @@ -1048,7 +1046,7 @@ fn updateTlv( |
| 1048 | 1046 | { |
| 1049 | 1047 | const gop = try elf_file.output_sections.getOrPut(gpa, atom_ptr.output_section_index); |
| 1050 | 1048 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 1051 | try gop.value_ptr.append(gpa, atom_ptr.atom_index); | |
| 1049 | try gop.value_ptr.append(gpa, .{ .index = atom_ptr.atom_index, .file = self.index }); | |
| 1052 | 1050 | } |
| 1053 | 1051 | } |
| 1054 | 1052 | |
| ... | ... | @@ -1307,8 +1305,7 @@ pub fn lowerUnnamedConst( |
| 1307 | 1305 | return error.CodegenFail; |
| 1308 | 1306 | }, |
| 1309 | 1307 | }; |
| 1310 | const sym = elf_file.symbol(sym_index); | |
| 1311 | try unnamed_consts.append(gpa, sym.atom_index); | |
| 1308 | try unnamed_consts.append(gpa, sym_index); | |
| 1312 | 1309 | return sym_index; |
| 1313 | 1310 | } |
| 1314 | 1311 | |
| ... | ... | @@ -1332,7 +1329,7 @@ fn lowerConst( |
| 1332 | 1329 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1333 | 1330 | defer code_buffer.deinit(); |
| 1334 | 1331 | |
| 1335 | const sym_index = try self.addAtom(elf_file); | |
| 1332 | const sym_index = try self.newAtom(elf_file); | |
| 1336 | 1333 | |
| 1337 | 1334 | const res = try codegen.generateSymbol( |
| 1338 | 1335 | &elf_file.base, |
| ... | ... | @@ -1530,6 +1527,72 @@ pub fn getString(self: ZigObject, off: u32) [:0]const u8 { |
| 1530 | 1527 | return self.strtab.getAssumeExists(off); |
| 1531 | 1528 | } |
| 1532 | 1529 | |
| 1530 | fn addAtom(self: *ZigObject, allocator: Allocator) !Atom.Index { | |
| 1531 | try self.atoms.ensureUnusedCapacity(allocator, 1); | |
| 1532 | try self.atoms_extra.ensureUnusedCapacity(allocator, @sizeOf(Atom.Extra)); | |
| 1533 | return self.addAtomAssumeCapacity(); | |
| 1534 | } | |
| 1535 | ||
| 1536 | fn addAtomAssumeCapacity(self: *ZigObject) Atom.Index { | |
| 1537 | const atom_index: Atom.Index = @intCast(self.atoms.items.len); | |
| 1538 | const atom_ptr = self.atoms.addOneAssumeCapacity(); | |
| 1539 | atom_ptr.* = .{ | |
| 1540 | .file_index = self.index, | |
| 1541 | .atom_index = atom_index, | |
| 1542 | .extra_index = self.addAtomExtraAssumeCapacity(.{}), | |
| 1543 | }; | |
| 1544 | return atom_index; | |
| 1545 | } | |
| 1546 | ||
| 1547 | pub fn atom(self: *ZigObject, atom_index: Atom.Index) ?*Atom { | |
| 1548 | if (atom_index == 0) return null; | |
| 1549 | assert(atom_index < self.atoms.items.len); | |
| 1550 | return &self.atoms.items[atom_index]; | |
| 1551 | } | |
| 1552 | ||
| 1553 | fn addAtomExtra(self: *ZigObject, allocator: Allocator, extra: Atom.Extra) !u32 { | |
| 1554 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 1555 | try self.atoms_extra.ensureUnusedCapacity(allocator, fields.len); | |
| 1556 | return self.addAtomExtraAssumeCapacity(extra); | |
| 1557 | } | |
| 1558 | ||
| 1559 | fn addAtomExtraAssumeCapacity(self: *ZigObject, extra: Atom.Extra) u32 { | |
| 1560 | const index = @as(u32, @intCast(self.atoms_extra.items.len)); | |
| 1561 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 1562 | inline for (fields) |field| { | |
| 1563 | self.atoms_extra.appendAssumeCapacity(switch (field.type) { | |
| 1564 | u32 => @field(extra, field.name), | |
| 1565 | else => @compileError("bad field type"), | |
| 1566 | }); | |
| 1567 | } | |
| 1568 | return index; | |
| 1569 | } | |
| 1570 | ||
| 1571 | pub fn atomExtra(self: ZigObject, index: u32) Atom.Extra { | |
| 1572 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 1573 | var i: usize = index; | |
| 1574 | var result: Atom.Extra = undefined; | |
| 1575 | inline for (fields) |field| { | |
| 1576 | @field(result, field.name) = switch (field.type) { | |
| 1577 | u32 => self.atoms_extra.items[i], | |
| 1578 | else => @compileError("bad field type"), | |
| 1579 | }; | |
| 1580 | i += 1; | |
| 1581 | } | |
| 1582 | return result; | |
| 1583 | } | |
| 1584 | ||
| 1585 | pub fn setAtomExtra(self: *ZigObject, index: u32, extra: Atom.Extra) void { | |
| 1586 | assert(index > 0); | |
| 1587 | const fields = @typeInfo(Atom.Extra).Struct.fields; | |
| 1588 | inline for (fields, 0..) |field, i| { | |
| 1589 | self.atoms_extra.items[index + i] = switch (field.type) { | |
| 1590 | u32 => @field(extra, field.name), | |
| 1591 | else => @compileError("bad field type"), | |
| 1592 | }; | |
| 1593 | } | |
| 1594 | } | |
| 1595 | ||
| 1533 | 1596 | pub fn fmtSymtab(self: *ZigObject, elf_file: *Elf) std.fmt.Formatter(formatSymtab) { |
| 1534 | 1597 | return .{ .data = .{ |
| 1535 | 1598 | .self = self, |
| ... | ... | @@ -1578,9 +1641,9 @@ fn formatAtoms( |
| 1578 | 1641 | _ = unused_fmt_string; |
| 1579 | 1642 | _ = options; |
| 1580 | 1643 | try writer.writeAll(" atoms\n"); |
| 1581 | for (ctx.self.atoms.items) |atom_index| { | |
| 1582 | const atom = ctx.elf_file.atom(atom_index) orelse continue; | |
| 1583 | try writer.print(" {}\n", .{atom.fmt(ctx.elf_file)}); | |
| 1644 | for (ctx.self.atoms_indexes.items) |atom_index| { | |
| 1645 | const atom_ptr = ctx.self.atom(atom_index) orelse continue; | |
| 1646 | try writer.print(" {}\n", .{atom_ptr.fmt(ctx.elf_file)}); | |
| 1584 | 1647 | } |
| 1585 | 1648 | } |
| 1586 | 1649 |
src/link/Elf/eh_frame.zig+2-2| ... | ... | @@ -42,8 +42,8 @@ pub const Fde = struct { |
| 42 | 42 | const object = elf_file.file(fde.file_index).?.object; |
| 43 | 43 | const rel = fde.relocs(elf_file)[0]; |
| 44 | 44 | const sym = object.symtab.items[rel.r_sym()]; |
| 45 | const atom_index = object.atoms.items[sym.st_shndx]; | |
| 46 | return elf_file.atom(atom_index).?; | |
| 45 | const atom_index = object.atoms_indexes.items[sym.st_shndx]; | |
| 46 | return object.atom(atom_index).?; | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | pub fn relocs(fde: Fde, elf_file: *Elf) []align(1) const elf.Elf64_Rela { |
src/link/Elf/file.zig+26-3| ... | ... | @@ -98,11 +98,34 @@ pub const File = union(enum) { |
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | pub fn atom(file: File, atom_index: Atom.Index) ?*Atom { | |
| 102 | return switch (file) { | |
| 103 | .shared_object => unreachable, | |
| 104 | .linker_defined => null, | |
| 105 | inline else => |x| x.atom(atom_index), | |
| 106 | }; | |
| 107 | } | |
| 108 | ||
| 101 | 109 | pub fn atoms(file: File) []const Atom.Index { |
| 102 | 110 | return switch (file) { |
| 103 | .linker_defined, .shared_object => &[0]Atom.Index{}, | |
| 104 | .zig_object => |x| x.atoms.items, | |
| 105 | .object => |x| x.atoms.items, | |
| 111 | .shared_object => unreachable, | |
| 112 | .linker_defined => &[0]Atom.Index{}, | |
| 113 | .zig_object => |x| x.atoms_indexes.items, | |
| 114 | .object => |x| x.atoms_indexes.items, | |
| 115 | }; | |
| 116 | } | |
| 117 | ||
| 118 | pub fn atomExtra(file: File, extra_index: u32) Atom.Extra { | |
| 119 | return switch (file) { | |
| 120 | .shared_object, .linker_defined => unreachable, | |
| 121 | inline else => |x| x.atomExtra(extra_index), | |
| 122 | }; | |
| 123 | } | |
| 124 | ||
| 125 | pub fn setAtomExtra(file: File, extra_index: u32, extra: Atom.Extra) void { | |
| 126 | return switch (file) { | |
| 127 | .shared_object, .linker_defined => unreachable, | |
| 128 | inline else => |x| x.setAtomExtra(extra_index, extra), | |
| 106 | 129 | }; |
| 107 | 130 | } |
| 108 | 131 |
src/link/Elf/gc.zig+7-5| ... | ... | @@ -35,7 +35,7 @@ fn collectRoots(roots: *std.ArrayList(*Atom), files: []const File.Index, elf_fil |
| 35 | 35 | const file = elf_file.file(index).?; |
| 36 | 36 | |
| 37 | 37 | for (file.atoms()) |atom_index| { |
| 38 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 38 | const atom = file.atom(atom_index) orelse continue; | |
| 39 | 39 | if (!atom.flags.alive) continue; |
| 40 | 40 | |
| 41 | 41 | const shdr = atom.inputShdr(elf_file); |
| ... | ... | @@ -120,8 +120,9 @@ fn mark(roots: std.ArrayList(*Atom), elf_file: *Elf) void { |
| 120 | 120 | |
| 121 | 121 | fn prune(files: []const File.Index, elf_file: *Elf) void { |
| 122 | 122 | for (files) |index| { |
| 123 | for (elf_file.file(index).?.atoms()) |atom_index| { | |
| 124 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 123 | const file = elf_file.file(index).?; | |
| 124 | for (file.atoms()) |atom_index| { | |
| 125 | const atom = file.atom(atom_index) orelse continue; | |
| 125 | 126 | if (atom.flags.alive and !atom.flags.visited) { |
| 126 | 127 | atom.flags.alive = false; |
| 127 | 128 | atom.markFdesDead(elf_file); |
| ... | ... | @@ -133,8 +134,9 @@ fn prune(files: []const File.Index, elf_file: *Elf) void { |
| 133 | 134 | pub fn dumpPrunedAtoms(elf_file: *Elf) !void { |
| 134 | 135 | const stderr = std.io.getStdErr().writer(); |
| 135 | 136 | for (elf_file.objects.items) |index| { |
| 136 | for (elf_file.file(index).?.object.atoms.items) |atom_index| { | |
| 137 | const atom = elf_file.atom(atom_index) orelse continue; | |
| 137 | const file = elf_file.file(index).?; | |
| 138 | for (file.atoms()) |atom_index| { | |
| 139 | const atom = file.atom(atom_index) orelse continue; | |
| 138 | 140 | if (!atom.flags.alive) |
| 139 | 141 | // TODO should we simply print to stderr? |
| 140 | 142 | try stderr.print("link: removing unused section '{s}' in file '{}'\n", .{ |
src/link/Elf/relocatable.zig+12-12| ... | ... | @@ -341,8 +341,8 @@ fn initComdatGroups(elf_file: *Elf) !void { |
| 341 | 341 | fn updateSectionSizes(elf_file: *Elf) !void { |
| 342 | 342 | for (elf_file.output_sections.keys(), elf_file.output_sections.values()) |shndx, atom_list| { |
| 343 | 343 | const shdr = &elf_file.shdrs.items[shndx]; |
| 344 | for (atom_list.items) |atom_index| { | |
| 345 | const atom_ptr = elf_file.atom(atom_index) orelse continue; | |
| 344 | for (atom_list.items) |ref| { | |
| 345 | const atom_ptr = elf_file.atom(ref) orelse continue; | |
| 346 | 346 | if (!atom_ptr.flags.alive) continue; |
| 347 | 347 | const offset = atom_ptr.alignment.forward(shdr.sh_size); |
| 348 | 348 | const padding = offset - shdr.sh_size; |
| ... | ... | @@ -354,8 +354,8 @@ fn updateSectionSizes(elf_file: *Elf) !void { |
| 354 | 354 | |
| 355 | 355 | for (elf_file.output_rela_sections.values()) |sec| { |
| 356 | 356 | const shdr = &elf_file.shdrs.items[sec.shndx]; |
| 357 | for (sec.atom_list.items) |atom_index| { | |
| 358 | const atom_ptr = elf_file.atom(atom_index) orelse continue; | |
| 357 | for (sec.atom_list.items) |ref| { | |
| 358 | const atom_ptr = elf_file.atom(ref) orelse continue; | |
| 359 | 359 | if (!atom_ptr.flags.alive) continue; |
| 360 | 360 | const relocs = atom_ptr.relocs(elf_file); |
| 361 | 361 | shdr.sh_size += shdr.sh_entsize * relocs.len; |
| ... | ... | @@ -448,16 +448,16 @@ fn writeAtoms(elf_file: *Elf) !void { |
| 448 | 448 | 0; |
| 449 | 449 | @memset(buffer, padding_byte); |
| 450 | 450 | |
| 451 | for (atom_list.items) |atom_index| { | |
| 452 | const atom_ptr = elf_file.atom(atom_index).?; | |
| 451 | for (atom_list.items) |ref| { | |
| 452 | const atom_ptr = elf_file.atom(ref).?; | |
| 453 | 453 | assert(atom_ptr.flags.alive); |
| 454 | 454 | |
| 455 | 455 | const offset = math.cast(usize, atom_ptr.value - @as(i64, @intCast(shdr.sh_addr - base_offset))) orelse |
| 456 | 456 | return error.Overflow; |
| 457 | 457 | const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow; |
| 458 | 458 | |
| 459 | log.debug("writing atom({d}) from 0x{x} to 0x{x}", .{ | |
| 460 | atom_index, | |
| 459 | log.debug("writing atom({}) from 0x{x} to 0x{x}", .{ | |
| 460 | ref, | |
| 461 | 461 | sh_offset + offset, |
| 462 | 462 | sh_offset + offset + size, |
| 463 | 463 | }); |
| ... | ... | @@ -465,8 +465,8 @@ fn writeAtoms(elf_file: *Elf) !void { |
| 465 | 465 | // TODO decompress directly into provided buffer |
| 466 | 466 | const out_code = buffer[offset..][0..size]; |
| 467 | 467 | const in_code = switch (atom_ptr.file(elf_file).?) { |
| 468 | .object => |x| try x.codeDecompressAlloc(elf_file, atom_index), | |
| 469 | .zig_object => |x| try x.codeAlloc(elf_file, atom_index), | |
| 468 | .object => |x| try x.codeDecompressAlloc(elf_file, ref.index), | |
| 469 | .zig_object => |x| try x.codeAlloc(elf_file, ref.index), | |
| 470 | 470 | else => unreachable, |
| 471 | 471 | }; |
| 472 | 472 | defer gpa.free(in_code); |
| ... | ... | @@ -490,8 +490,8 @@ fn writeSyntheticSections(elf_file: *Elf) !void { |
| 490 | 490 | var relocs = try std.ArrayList(elf.Elf64_Rela).initCapacity(gpa, num_relocs); |
| 491 | 491 | defer relocs.deinit(); |
| 492 | 492 | |
| 493 | for (sec.atom_list.items) |atom_index| { | |
| 494 | const atom_ptr = elf_file.atom(atom_index) orelse continue; | |
| 493 | for (sec.atom_list.items) |ref| { | |
| 494 | const atom_ptr = elf_file.atom(ref) orelse continue; | |
| 495 | 495 | if (!atom_ptr.flags.alive) continue; |
| 496 | 496 | try atom_ptr.writeRelocs(elf_file, &relocs); |
| 497 | 497 | } |
src/link/Elf/synthetic_sections.zig+4-4| ... | ... | @@ -1705,14 +1705,14 @@ pub const ComdatGroupSection = struct { |
| 1705 | 1705 | const shdr = object.shdrs.items[shndx]; |
| 1706 | 1706 | switch (shdr.sh_type) { |
| 1707 | 1707 | elf.SHT_RELA => { |
| 1708 | const atom_index = object.atoms.items[shdr.sh_info]; | |
| 1709 | const atom = elf_file.atom(atom_index).?; | |
| 1708 | const atom_index = object.atoms_indexes.items[shdr.sh_info]; | |
| 1709 | const atom = object.atom(atom_index).?; | |
| 1710 | 1710 | const rela = elf_file.output_rela_sections.get(atom.outputShndx().?).?; |
| 1711 | 1711 | try writer.writeInt(u32, rela.shndx, .little); |
| 1712 | 1712 | }, |
| 1713 | 1713 | else => { |
| 1714 | const atom_index = object.atoms.items[shndx]; | |
| 1715 | const atom = elf_file.atom(atom_index).?; | |
| 1714 | const atom_index = object.atoms_indexes.items[shndx]; | |
| 1715 | const atom = object.atom(atom_index).?; | |
| 1716 | 1716 | try writer.writeInt(u32, atom.outputShndx().?, .little); |
| 1717 | 1717 | }, |
| 1718 | 1718 | } |
src/link/Elf/thunks.zig+7-8| ... | ... | @@ -6,8 +6,8 @@ pub fn createThunks(shndx: u32, elf_file: *Elf) !void { |
| 6 | 6 | const atoms = elf_file.output_sections.get(shndx).?.items; |
| 7 | 7 | assert(atoms.len > 0); |
| 8 | 8 | |
| 9 | for (atoms) |atom_index| { | |
| 10 | elf_file.atom(atom_index).?.value = -1; | |
| 9 | for (atoms) |ref| { | |
| 10 | elf_file.atom(ref).?.value = -1; | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | var i: usize = 0; |
| ... | ... | @@ -19,8 +19,7 @@ pub fn createThunks(shndx: u32, elf_file: *Elf) !void { |
| 19 | 19 | i += 1; |
| 20 | 20 | |
| 21 | 21 | while (i < atoms.len) : (i += 1) { |
| 22 | const atom_index = atoms[i]; | |
| 23 | const atom = elf_file.atom(atom_index).?; | |
| 22 | const atom = elf_file.atom(atoms[i]).?; | |
| 24 | 23 | assert(atom.flags.alive); |
| 25 | 24 | if (@as(i64, @intCast(atom.alignment.forward(shdr.sh_size))) - start_atom.value >= max_distance) |
| 26 | 25 | break; |
| ... | ... | @@ -33,10 +32,10 @@ pub fn createThunks(shndx: u32, elf_file: *Elf) !void { |
| 33 | 32 | thunk.output_section_index = shndx; |
| 34 | 33 | |
| 35 | 34 | // Scan relocs in the group and create trampolines for any unreachable callsite |
| 36 | for (atoms[start..i]) |atom_index| { | |
| 37 | const atom = elf_file.atom(atom_index).?; | |
| 35 | for (atoms[start..i]) |ref| { | |
| 36 | const atom = elf_file.atom(ref).?; | |
| 38 | 37 | const file = atom.file(elf_file).?; |
| 39 | log.debug("atom({d}) {s}", .{ atom_index, atom.name(elf_file) }); | |
| 38 | log.debug("atom({}) {s}", .{ ref, atom.name(elf_file) }); | |
| 40 | 39 | for (atom.relocs(elf_file)) |rel| { |
| 41 | 40 | const is_reachable = switch (cpu_arch) { |
| 42 | 41 | .aarch64 => aarch64.isReachable(atom, rel, elf_file), |
| ... | ... | @@ -51,7 +50,7 @@ pub fn createThunks(shndx: u32, elf_file: *Elf) !void { |
| 51 | 50 | }; |
| 52 | 51 | try thunk.symbols.put(gpa, target, {}); |
| 53 | 52 | } |
| 54 | try atom.addExtra(.{ .thunk = thunk_index }, elf_file); | |
| 53 | atom.addExtra(.{ .thunk = thunk_index }, elf_file); | |
| 55 | 54 | atom.flags.thunk = true; |
| 56 | 55 | } |
| 57 | 56 |
src/link/MachO/InternalObject.zig+1-2| ... | ... | @@ -45,8 +45,7 @@ pub fn deinit(self: *InternalObject, allocator: Allocator) void { |
| 45 | 45 | |
| 46 | 46 | pub fn init(self: *InternalObject, allocator: Allocator) !void { |
| 47 | 47 | // Atom at index 0 is reserved as null atom. |
| 48 | try self.atoms.append(allocator, .{}); | |
| 49 | try self.atoms_extra.append(allocator, 0); | |
| 48 | try self.atoms.append(allocator, .{ .extra = try self.addAtomExtra(allocator, .{}) }); | |
| 50 | 49 | // Null byte in strtab |
| 51 | 50 | try self.strtab.append(allocator, 0); |
| 52 | 51 | } |
src/link/MachO/ZigObject.zig+1-1| ... | ... | @@ -1634,7 +1634,7 @@ fn isThreadlocal(macho_file: *MachO, decl_index: InternPool.DeclIndex) bool { |
| 1634 | 1634 | |
| 1635 | 1635 | fn addAtom(self: *ZigObject, allocator: Allocator) !Atom.Index { |
| 1636 | 1636 | try self.atoms.ensureUnusedCapacity(allocator, 1); |
| 1637 | try self.atoms_extra.ensureUnusedCapacity(allocator, 1); | |
| 1637 | try self.atoms_extra.ensureUnusedCapacity(allocator, @sizeOf(Atom.Extra)); | |
| 1638 | 1638 | return self.addAtomAssumeCapacity(); |
| 1639 | 1639 | } |
| 1640 | 1640 |