authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-26 11:12:31+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-27 09:59:59+02:00
log1200a5a2401cd4a13d818499f9d534c0051c3724
treeaac835a704b309e6dd8fef3956c3c354834d22f5
parentde4d1ea250f37c03cf2eec6c6bdf7436c8816893

elf: allow expanding segments in virtual memory


6 files changed, 177 insertions(+), 79 deletions(-)

src/link/Elf.zig+96-52
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1base: link.File,1base: link.File,
2
2dwarf: ?Dwarf = null,3dwarf: ?Dwarf = null,
34
4ptr_width: PtrWidth,5ptr_width: PtrWidth,
...@@ -103,6 +104,7 @@ phdr_table_dirty: bool = false,...@@ -103,6 +104,7 @@ phdr_table_dirty: bool = false,
103shdr_table_dirty: bool = false,104shdr_table_dirty: bool = false,
104shstrtab_dirty: bool = false,105shstrtab_dirty: bool = false,
105strtab_dirty: bool = false,106strtab_dirty: bool = false,
107got_dirty: bool = false,
106108
107debug_strtab_dirty: bool = false,109debug_strtab_dirty: bool = false,
108debug_abbrev_section_dirty: bool = false,110debug_abbrev_section_dirty: bool = false,
...@@ -411,7 +413,6 @@ fn findFreeSpace(self: *Elf, object_size: u64, min_alignment: u64) u64 {...@@ -411,7 +413,6 @@ fn findFreeSpace(self: *Elf, object_size: u64, min_alignment: u64) u64 {
411const AllocateSegmentOpts = struct {413const AllocateSegmentOpts = struct {
412 size: u64,414 size: u64,
413 alignment: u64,415 alignment: u64,
414 addr: ?u64 = null, // TODO find free VM space
415 flags: u32 = elf.PF_R,416 flags: u32 = elf.PF_R,
416};417};
417418
...@@ -422,7 +423,7 @@ pub fn allocateSegment(self: *Elf, opts: AllocateSegmentOpts) error{OutOfMemory}...@@ -422,7 +423,7 @@ pub fn allocateSegment(self: *Elf, opts: AllocateSegmentOpts) error{OutOfMemory}
422 // Memory is always allocated in sequence.423 // Memory is always allocated in sequence.
423 // TODO is this correct? Or should we implement something similar to `findFreeSpace`?424 // TODO is this correct? Or should we implement something similar to `findFreeSpace`?
424 // How would that impact HCS?425 // How would that impact HCS?
425 const addr = opts.addr orelse blk: {426 const addr = blk: {
426 assert(self.phdr_table_load_index != null);427 assert(self.phdr_table_load_index != null);
427 const phdr = &self.phdrs.items[index - 1];428 const phdr = &self.phdrs.items[index - 1];
428 break :blk mem.alignForward(u64, phdr.p_vaddr + phdr.p_memsz, opts.alignment);429 break :blk mem.alignForward(u64, phdr.p_vaddr + phdr.p_memsz, opts.alignment);
...@@ -568,7 +569,6 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -568,7 +569,6 @@ pub fn populateMissingMetadata(self: *Elf) !void {
568569
569 if (self.phdr_load_re_index == null) {570 if (self.phdr_load_re_index == null) {
570 self.phdr_load_re_index = try self.allocateSegment(.{571 self.phdr_load_re_index = try self.allocateSegment(.{
571 .addr = self.defaultEntryAddress(),
572 .size = self.base.options.program_code_size_hint,572 .size = self.base.options.program_code_size_hint,
573 .alignment = self.page_size,573 .alignment = self.page_size,
574 .flags = elf.PF_X | elf.PF_R | elf.PF_W,574 .flags = elf.PF_X | elf.PF_R | elf.PF_W,
...@@ -577,15 +577,10 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -577,15 +577,10 @@ pub fn populateMissingMetadata(self: *Elf) !void {
577 }577 }
578578
579 if (self.phdr_got_index == null) {579 if (self.phdr_got_index == null) {
580 // TODO instead of hard coding the vaddr, make a function to find a vaddr to put things at.
581 // we'll need to re-use that function anyway, in case the GOT grows and overlaps something
582 // else in virtual memory.
583 const addr: u64 = if (self.base.options.target.ptrBitWidth() >= 32) 0x4000000 else 0x8000;
584 // We really only need ptr alignment but since we are using PROGBITS, linux requires580 // We really only need ptr alignment but since we are using PROGBITS, linux requires
585 // page align.581 // page align.
586 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);582 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);
587 self.phdr_got_index = try self.allocateSegment(.{583 self.phdr_got_index = try self.allocateSegment(.{
588 .addr = addr,
589 .size = @as(u64, ptr_size) * self.base.options.symbol_count_hint,584 .size = @as(u64, ptr_size) * self.base.options.symbol_count_hint,
590 .alignment = alignment,585 .alignment = alignment,
591 .flags = elf.PF_R | elf.PF_W,586 .flags = elf.PF_R | elf.PF_W,
...@@ -593,12 +588,9 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -593,12 +588,9 @@ pub fn populateMissingMetadata(self: *Elf) !void {
593 }588 }
594589
595 if (self.phdr_load_ro_index == null) {590 if (self.phdr_load_ro_index == null) {
596 // TODO Same as for GOT
597 const addr: u64 = if (self.base.options.target.ptrBitWidth() >= 32) 0xc000000 else 0xa000;
598 // Same reason as for GOT591 // Same reason as for GOT
599 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);592 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);
600 self.phdr_load_ro_index = try self.allocateSegment(.{593 self.phdr_load_ro_index = try self.allocateSegment(.{
601 .addr = addr,
602 .size = 1024,594 .size = 1024,
603 .alignment = alignment,595 .alignment = alignment,
604 .flags = elf.PF_R | elf.PF_W,596 .flags = elf.PF_R | elf.PF_W,
...@@ -606,12 +598,9 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -606,12 +598,9 @@ pub fn populateMissingMetadata(self: *Elf) !void {
606 }598 }
607599
608 if (self.phdr_load_rw_index == null) {600 if (self.phdr_load_rw_index == null) {
609 // TODO Same as for GOT
610 const addr: u64 = if (self.base.options.target.ptrBitWidth() >= 32) 0x10000000 else 0xc000;
611 // Same reason as for GOT601 // Same reason as for GOT
612 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);602 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);
613 self.phdr_load_rw_index = try self.allocateSegment(.{603 self.phdr_load_rw_index = try self.allocateSegment(.{
614 .addr = addr,
615 .size = 1024,604 .size = 1024,
616 .alignment = alignment,605 .alignment = alignment,
617 .flags = elf.PF_R | elf.PF_W,606 .flags = elf.PF_R | elf.PF_W,
...@@ -619,11 +608,8 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -619,11 +608,8 @@ pub fn populateMissingMetadata(self: *Elf) !void {
619 }608 }
620609
621 if (self.phdr_load_zerofill_index == null) {610 if (self.phdr_load_zerofill_index == null) {
622 // TODO Same as for GOT
623 const addr: u64 = if (self.base.options.target.ptrBitWidth() >= 32) 0x14000000 else 0xf000;
624 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);611 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);
625 self.phdr_load_zerofill_index = try self.allocateSegment(.{612 self.phdr_load_zerofill_index = try self.allocateSegment(.{
626 .addr = addr,
627 .size = 0,613 .size = 0,
628 .alignment = alignment,614 .alignment = alignment,
629 .flags = elf.PF_R | elf.PF_W,615 .flags = elf.PF_R | elf.PF_W,
...@@ -858,7 +844,19 @@ pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {...@@ -858,7 +844,19 @@ pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
858 }844 }
859845
860 const mem_capacity = self.allocatedVirtualSize(phdr.p_vaddr);846 const mem_capacity = self.allocatedVirtualSize(phdr.p_vaddr);
861 assert(needed_size <= mem_capacity); // TODO grow section in virtual memory847 if (needed_size > mem_capacity) {
848 // We are exceeding our allocated VM capacity so we need to shift everything in memory
849 // and grow.
850 {
851 const dirty_addr = phdr.p_vaddr + phdr.p_memsz;
852 self.got_dirty = for (self.got.entries.items) |entry| {
853 if (self.symbol(entry.symbol_index).value >= dirty_addr) break true;
854 } else false;
855
856 // TODO mark relocs dirty
857 }
858 try self.growSegment(shdr_index, needed_size);
859 }
862860
863 shdr.sh_size = needed_size;861 shdr.sh_size = needed_size;
864 phdr.p_memsz = needed_size;862 phdr.p_memsz = needed_size;
...@@ -870,6 +868,61 @@ pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {...@@ -870,6 +868,61 @@ pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
870 self.markDirty(shdr_index, phdr_index);868 self.markDirty(shdr_index, phdr_index);
871}869}
872870
871fn growSegment(self: *Elf, shndx: u16, needed_size: u64) !void {
872 const phdr_index = self.phdr_to_shdr_table.get(shndx).?;
873 const phdr = &self.phdrs.items[phdr_index];
874 const increased_size = padToIdeal(needed_size);
875 const end_addr = phdr.p_vaddr + phdr.p_memsz;
876 const old_aligned_end = phdr.p_vaddr + mem.alignForward(u64, phdr.p_memsz, phdr.p_align);
877 const new_aligned_end = phdr.p_vaddr + mem.alignForward(u64, increased_size, phdr.p_align);
878 const diff = new_aligned_end - old_aligned_end;
879 log.debug("growing phdr({d}) in memory by {x}", .{ phdr_index, diff });
880
881 // Update symbols and atoms.
882 var files = std.ArrayList(File.Index).init(self.base.allocator);
883 defer files.deinit();
884 try files.ensureTotalCapacityPrecise(self.objects.items.len + 1);
885
886 if (self.zig_module_index) |index| files.appendAssumeCapacity(index);
887 files.appendSliceAssumeCapacity(self.objects.items);
888
889 for (files.items) |index| {
890 const file_ptr = self.file(index).?;
891
892 for (file_ptr.locals()) |sym_index| {
893 const sym = self.symbol(sym_index);
894 const atom_ptr = sym.atom(self) orelse continue;
895 if (!atom_ptr.flags.alive or !atom_ptr.flags.allocated) continue;
896 if (atom_ptr.value >= end_addr) sym.value += diff;
897 }
898
899 for (file_ptr.globals()) |sym_index| {
900 const sym = self.symbol(sym_index);
901 const atom_ptr = sym.atom(self) orelse continue;
902 if (!atom_ptr.flags.alive or !atom_ptr.flags.allocated) continue;
903 if (atom_ptr.value >= end_addr) sym.value += diff;
904 }
905
906 for (file_ptr.atoms()) |atom_index| {
907 const atom_ptr = self.atom(atom_index) orelse continue;
908 if (!atom_ptr.flags.alive or !atom_ptr.flags.allocated) continue;
909 if (atom_ptr.value >= end_addr) atom_ptr.value += diff;
910 }
911 }
912
913 // Finally, update section headers.
914 for (self.shdrs.items, 0..) |*other_shdr, other_shndx| {
915 if (other_shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
916 if (other_shndx == shndx) continue;
917 const other_phdr_index = self.phdr_to_shdr_table.get(@intCast(other_shndx)) orelse continue;
918 const other_phdr = &self.phdrs.items[other_phdr_index];
919 if (other_phdr.p_vaddr < end_addr) continue;
920 other_shdr.sh_addr += diff;
921 other_phdr.p_vaddr += diff;
922 other_phdr.p_paddr += diff;
923 }
924}
925
873pub fn growNonAllocSection(926pub fn growNonAllocSection(
874 self: *Elf,927 self: *Elf,
875 shdr_index: u16,928 shdr_index: u16,
...@@ -1133,6 +1186,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1133,6 +1186,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1133 // Scan and create missing synthetic entries such as GOT indirection.1186 // Scan and create missing synthetic entries such as GOT indirection.
1134 try self.scanRelocs();1187 try self.scanRelocs();
11351188
1189 if (build_options.enable_logging) {
1190 state_log.debug("{}", .{self.dumpState()});
1191 }
1192
1136 // Allocate atoms parsed from input object files, followed by allocating1193 // Allocate atoms parsed from input object files, followed by allocating
1137 // linker-defined synthetic symbols.1194 // linker-defined synthetic symbols.
1138 try self.allocateObjects();1195 try self.allocateObjects();
...@@ -1143,7 +1200,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1143,7 +1200,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1143 if (self.zig_module_index) |index| {1200 if (self.zig_module_index) |index| {
1144 for (self.file(index).?.zig_module.atoms.keys()) |atom_index| {1201 for (self.file(index).?.zig_module.atoms.keys()) |atom_index| {
1145 const atom_ptr = self.atom(atom_index).?;1202 const atom_ptr = self.atom(atom_index).?;
1146 if (!atom_ptr.alive) continue;1203 if (!atom_ptr.flags.alive) continue;
1147 const shdr = &self.shdrs.items[atom_ptr.outputShndx().?];1204 const shdr = &self.shdrs.items[atom_ptr.outputShndx().?];
1148 const file_offset = shdr.sh_offset + atom_ptr.value - shdr.sh_addr;1205 const file_offset = shdr.sh_offset + atom_ptr.value - shdr.sh_addr;
1149 const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow;1206 const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow;
...@@ -1157,6 +1214,15 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1157,6 +1214,15 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1157 }1214 }
1158 try self.writeObjects();1215 try self.writeObjects();
11591216
1217 if (self.got_dirty) {
1218 const shdr = &self.shdrs.items[self.got_section_index.?];
1219 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.got.size(self));
1220 defer buffer.deinit();
1221 try self.got.writeAllEntries(self, buffer.writer());
1222 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
1223 self.got_dirty = false;
1224 }
1225
1160 // Look for entry address in objects if not set by the incremental compiler.1226 // Look for entry address in objects if not set by the incremental compiler.
1161 if (self.entry_addr == null) {1227 if (self.entry_addr == null) {
1162 const entry: ?[]const u8 = entry: {1228 const entry: ?[]const u8 = entry: {
...@@ -1537,7 +1603,7 @@ fn resolveSymbols(self: *Elf) error{Overflow}!void {...@@ -1537,7 +1603,7 @@ fn resolveSymbols(self: *Elf) error{Overflow}!void {
1537 for (try object.comdatGroupMembers(cg.shndx)) |shndx| {1603 for (try object.comdatGroupMembers(cg.shndx)) |shndx| {
1538 const atom_index = object.atoms.items[shndx];1604 const atom_index = object.atoms.items[shndx];
1539 if (self.atom(atom_index)) |atom_ptr| {1605 if (self.atom(atom_index)) |atom_ptr| {
1540 atom_ptr.alive = false;1606 atom_ptr.flags.alive = false;
1541 // atom_ptr.markFdesDead(self);1607 // atom_ptr.markFdesDead(self);
1542 }1608 }
1543 }1609 }
...@@ -1645,25 +1711,26 @@ fn scanRelocs(self: *Elf) !void {...@@ -1645,25 +1711,26 @@ fn scanRelocs(self: *Elf) !void {
1645fn allocateObjects(self: *Elf) !void {1711fn allocateObjects(self: *Elf) !void {
1646 for (self.objects.items) |index| {1712 for (self.objects.items) |index| {
1647 const object = self.file(index).?.object;1713 const object = self.file(index).?.object;
1714
1648 for (object.atoms.items) |atom_index| {1715 for (object.atoms.items) |atom_index| {
1649 const atom_ptr = self.atom(atom_index) orelse continue;1716 const atom_ptr = self.atom(atom_index) orelse continue;
1650 if (!atom_ptr.alive) continue;1717 if (!atom_ptr.flags.alive or atom_ptr.flags.allocated) continue;
1651 try atom_ptr.allocate(self);1718 try atom_ptr.allocate(self);
1652 }1719 }
16531720
1654 for (object.locals()) |local_index| {1721 for (object.locals()) |local_index| {
1655 const local = self.symbol(local_index);1722 const local = self.symbol(local_index);
1656 const atom_ptr = local.atom(self) orelse continue;1723 const atom_ptr = local.atom(self) orelse continue;
1657 if (!atom_ptr.alive) continue;1724 if (!atom_ptr.flags.alive) continue;
1658 local.value += atom_ptr.value;1725 local.value = local.elfSym(self).st_value + atom_ptr.value;
1659 }1726 }
16601727
1661 for (object.globals()) |global_index| {1728 for (object.globals()) |global_index| {
1662 const global = self.symbol(global_index);1729 const global = self.symbol(global_index);
1663 const atom_ptr = global.atom(self) orelse continue;1730 const atom_ptr = global.atom(self) orelse continue;
1664 if (!atom_ptr.alive) continue;1731 if (!atom_ptr.flags.alive) continue;
1665 if (global.file_index == index) {1732 if (global.file_index == index) {
1666 global.value += atom_ptr.value;1733 global.value = global.elfSym(self).st_value + atom_ptr.value;
1667 }1734 }
1668 }1735 }
1669 }1736 }
...@@ -1676,7 +1743,7 @@ fn writeObjects(self: *Elf) !void {...@@ -1676,7 +1743,7 @@ fn writeObjects(self: *Elf) !void {
1676 const object = self.file(index).?.object;1743 const object = self.file(index).?.object;
1677 for (object.atoms.items) |atom_index| {1744 for (object.atoms.items) |atom_index| {
1678 const atom_ptr = self.atom(atom_index) orelse continue;1745 const atom_ptr = self.atom(atom_index) orelse continue;
1679 if (!atom_ptr.alive) continue;1746 if (!atom_ptr.flags.alive) continue;
16801747
1681 const shdr = &self.shdrs.items[atom_ptr.outputShndx().?];1748 const shdr = &self.shdrs.items[atom_ptr.outputShndx().?];
1682 if (shdr.sh_type == elf.SHT_NOBITS) continue;1749 if (shdr.sh_type == elf.SHT_NOBITS) continue;
...@@ -2650,7 +2717,7 @@ fn updateDeclCode(...@@ -2650,7 +2717,7 @@ fn updateDeclCode(
2650 atom_ptr.output_section_index = shdr_index;2717 atom_ptr.output_section_index = shdr_index;
26512718
2652 sym.name_offset = try self.strtab.insert(gpa, decl_name);2719 sym.name_offset = try self.strtab.insert(gpa, decl_name);
2653 atom_ptr.alive = true;2720 atom_ptr.flags.alive = true;
2654 atom_ptr.name_offset = sym.name_offset;2721 atom_ptr.name_offset = sym.name_offset;
2655 esym.st_name = sym.name_offset;2722 esym.st_name = sym.name_offset;
2656 esym.st_info |= stt_bits;2723 esym.st_info |= stt_bits;
...@@ -2681,11 +2748,6 @@ fn updateDeclCode(...@@ -2681,11 +2748,6 @@ fn updateDeclCode(
2681 } else {2748 } else {
2682 try atom_ptr.allocate(self);2749 try atom_ptr.allocate(self);
2683 errdefer self.freeDeclMetadata(sym_index);2750 errdefer self.freeDeclMetadata(sym_index);
2684 log.debug("allocated atom for {s} at 0x{x} to 0x{x}", .{
2685 decl_name,
2686 atom_ptr.value,
2687 atom_ptr.value + atom_ptr.size,
2688 });
26892751
2690 sym.value = atom_ptr.value;2752 sym.value = atom_ptr.value;
2691 esym.st_value = atom_ptr.value;2753 esym.st_value = atom_ptr.value;
...@@ -2873,7 +2935,6 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol....@@ -2873,7 +2935,6 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.
2873 defer gpa.free(name);2935 defer gpa.free(name);
2874 break :blk try self.strtab.insert(gpa, name);2936 break :blk try self.strtab.insert(gpa, name);
2875 };2937 };
2876 const name = self.strtab.get(name_str_index).?;
28772938
2878 const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|2939 const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|
2879 mod.declPtr(owner_decl).srcLoc(mod)2940 mod.declPtr(owner_decl).srcLoc(mod)
...@@ -2913,7 +2974,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol....@@ -2913,7 +2974,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.
2913 local_esym.st_info |= elf.STT_OBJECT;2974 local_esym.st_info |= elf.STT_OBJECT;
2914 local_esym.st_size = code.len;2975 local_esym.st_size = code.len;
2915 const atom_ptr = local_sym.atom(self).?;2976 const atom_ptr = local_sym.atom(self).?;
2916 atom_ptr.alive = true;2977 atom_ptr.flags.alive = true;
2917 atom_ptr.name_offset = name_str_index;2978 atom_ptr.name_offset = name_str_index;
2918 atom_ptr.alignment = required_alignment;2979 atom_ptr.alignment = required_alignment;
2919 atom_ptr.size = code.len;2980 atom_ptr.size = code.len;
...@@ -2922,12 +2983,6 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol....@@ -2922,12 +2983,6 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.
2922 try atom_ptr.allocate(self);2983 try atom_ptr.allocate(self);
2923 errdefer self.freeDeclMetadata(symbol_index);2984 errdefer self.freeDeclMetadata(symbol_index);
29242985
2925 log.debug("allocated atom for {s} at 0x{x} to 0x{x}", .{
2926 name,
2927 atom_ptr.value,
2928 atom_ptr.value + atom_ptr.size,
2929 });
2930
2931 local_sym.value = atom_ptr.value;2986 local_sym.value = atom_ptr.value;
2932 local_esym.st_value = atom_ptr.value;2987 local_esym.st_value = atom_ptr.value;
29332988
...@@ -2961,7 +3016,6 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2961,7 +3016,6 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2961 defer gpa.free(name);3016 defer gpa.free(name);
2962 break :blk try self.strtab.insert(gpa, name);3017 break :blk try self.strtab.insert(gpa, name);
2963 };3018 };
2964 const name = self.strtab.get(name_str_index).?;
29653019
2966 const zig_module = self.file(self.zig_module_index.?).?.zig_module;3020 const zig_module = self.file(self.zig_module_index.?).?.zig_module;
2967 const sym_index = try zig_module.addAtom(self);3021 const sym_index = try zig_module.addAtom(self);
...@@ -2992,7 +3046,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2992,7 +3046,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2992 local_esym.st_info |= elf.STT_OBJECT;3046 local_esym.st_info |= elf.STT_OBJECT;
2993 local_esym.st_size = code.len;3047 local_esym.st_size = code.len;
2994 const atom_ptr = local_sym.atom(self).?;3048 const atom_ptr = local_sym.atom(self).?;
2995 atom_ptr.alive = true;3049 atom_ptr.flags.alive = true;
2996 atom_ptr.name_offset = name_str_index;3050 atom_ptr.name_offset = name_str_index;
2997 atom_ptr.alignment = required_alignment;3051 atom_ptr.alignment = required_alignment;
2998 atom_ptr.size = code.len;3052 atom_ptr.size = code.len;
...@@ -3001,8 +3055,6 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -3001,8 +3055,6 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
3001 try atom_ptr.allocate(self);3055 try atom_ptr.allocate(self);
3002 errdefer self.freeDeclMetadata(sym_index);3056 errdefer self.freeDeclMetadata(sym_index);
30033057
3004 log.debug("allocated atom for {s} at 0x{x} to 0x{x}", .{ name, atom_ptr.value, atom_ptr.value + atom_ptr.size });
3005
3006 local_sym.value = atom_ptr.value;3058 local_sym.value = atom_ptr.value;
3007 local_esym.st_value = atom_ptr.value;3059 local_esym.st_value = atom_ptr.value;
30083060
...@@ -3740,14 +3792,6 @@ pub fn calcImageBase(self: Elf) u64 {...@@ -3740,14 +3792,6 @@ pub fn calcImageBase(self: Elf) u64 {
3740 };3792 };
3741}3793}
37423794
3743pub fn defaultEntryAddress(self: Elf) u64 {
3744 if (self.entry_addr) |addr| return addr;
3745 return switch (self.base.options.target.cpu.arch) {
3746 .spu_2 => 0,
3747 else => default_entry_addr,
3748 };
3749}
3750
3751pub fn isDynLib(self: Elf) bool {3795pub fn isDynLib(self: Elf) bool {
3752 return self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic;3796 return self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic;
3753}3797}
src/link/Elf/Atom.zig+37-16
...@@ -25,11 +25,8 @@ relocs_section_index: Index = 0,...@@ -25,11 +25,8 @@ relocs_section_index: Index = 0,
25/// Index of this atom in the linker's atoms table.25/// Index of this atom in the linker's atoms table.
26atom_index: Index = 0,26atom_index: Index = 0,
2727
28/// Specifies whether this atom is alive or has been garbage collected.28/// Flags we use for state tracking.
29alive: bool = false,29flags: Flags = .{},
30
31/// Specifies if the atom has been visited during garbage collection.
32visited: bool = false,
3330
34/// Start index of FDEs referencing this atom.31/// Start index of FDEs referencing this atom.
35fde_start: u32 = 0,32fde_start: u32 = 0,
...@@ -48,8 +45,12 @@ pub fn name(self: Atom, elf_file: *Elf) []const u8 {...@@ -48,8 +45,12 @@ pub fn name(self: Atom, elf_file: *Elf) []const u8 {
48 return elf_file.strtab.getAssumeExists(self.name_offset);45 return elf_file.strtab.getAssumeExists(self.name_offset);
49}46}
5047
48pub fn file(self: Atom, elf_file: *Elf) ?File {
49 return elf_file.file(self.file_index);
50}
51
51pub fn inputShdr(self: Atom, elf_file: *Elf) elf.Elf64_Shdr {52pub fn inputShdr(self: Atom, elf_file: *Elf) elf.Elf64_Shdr {
52 const object = elf_file.file(self.file_index).?.object;53 const object = self.file(elf_file).?.object;
53 return object.shdrs.items[self.input_section_index];54 return object.shdrs.items[self.input_section_index];
54}55}
5556
...@@ -59,7 +60,7 @@ pub fn outputShndx(self: Atom) ?u16 {...@@ -59,7 +60,7 @@ pub fn outputShndx(self: Atom) ?u16 {
59}60}
6061
61pub fn codeInObject(self: Atom, elf_file: *Elf) error{Overflow}![]const u8 {62pub fn codeInObject(self: Atom, elf_file: *Elf) error{Overflow}![]const u8 {
62 const object = elf_file.file(self.file_index).?.object;63 const object = self.file(elf_file).?.object;
63 return object.shdrContents(self.input_section_index);64 return object.shdrContents(self.input_section_index);
64}65}
6566
...@@ -91,7 +92,7 @@ pub fn codeInObjectUncompressAlloc(self: Atom, elf_file: *Elf) ![]u8 {...@@ -91,7 +92,7 @@ pub fn codeInObjectUncompressAlloc(self: Atom, elf_file: *Elf) ![]u8 {
91}92}
9293
93pub fn priority(self: Atom, elf_file: *Elf) u64 {94pub fn priority(self: Atom, elf_file: *Elf) u64 {
94 const index = elf_file.file(self.file_index).?.index();95 const index = self.file(elf_file).?.index();
95 return (@as(u64, @intCast(index)) << 32) | @as(u64, @intCast(self.input_section_index));96 return (@as(u64, @intCast(index)) << 32) | @as(u64, @intCast(self.input_section_index));
96}97}
9798
...@@ -178,6 +179,13 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {...@@ -178,6 +179,13 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {
178 }179 }
179 };180 };
180181
182 log.debug("allocated atom({d}) : '{s}' at 0x{x} to 0x{x}", .{
183 self.atom_index,
184 self.name(elf_file),
185 self.value,
186 self.value + self.size,
187 });
188
181 const expand_section = if (atom_placement) |placement_index|189 const expand_section = if (atom_placement) |placement_index|
182 elf_file.atom(placement_index).?.next_index == 0190 elf_file.atom(placement_index).?.next_index == 0
183 else191 else
...@@ -222,6 +230,8 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {...@@ -222,6 +230,8 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {
222 if (free_list_removal) |i| {230 if (free_list_removal) |i| {
223 _ = free_list.swapRemove(i);231 _ = free_list.swapRemove(i);
224 }232 }
233
234 self.flags.allocated = true;
225}235}
226236
227pub fn shrink(self: *Atom, elf_file: *Elf) void {237pub fn shrink(self: *Atom, elf_file: *Elf) void {
...@@ -238,7 +248,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void {...@@ -238,7 +248,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
238 log.debug("freeAtom {d} ({s})", .{ self.atom_index, self.name(elf_file) });248 log.debug("freeAtom {d} ({s})", .{ self.atom_index, self.name(elf_file) });
239249
240 const gpa = elf_file.base.allocator;250 const gpa = elf_file.base.allocator;
241 const zig_module = elf_file.file(self.file_index).?.zig_module;251 const zig_module = self.file(elf_file).?.zig_module;
242 const shndx = self.outputShndx().?;252 const shndx = self.outputShndx().?;
243 const meta = elf_file.last_atom_and_free_list_table.getPtr(shndx).?;253 const meta = elf_file.last_atom_and_free_list_table.getPtr(shndx).?;
244 const free_list = &meta.free_list;254 const free_list = &meta.free_list;
...@@ -294,7 +304,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void {...@@ -294,7 +304,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
294}304}
295305
296pub fn relocs(self: Atom, elf_file: *Elf) error{Overflow}![]align(1) const elf.Elf64_Rela {306pub fn relocs(self: Atom, elf_file: *Elf) error{Overflow}![]align(1) const elf.Elf64_Rela {
297 return switch (elf_file.file(self.file_index).?) {307 return switch (self.file(elf_file).?) {
298 .zig_module => |x| x.relocs.items[self.relocs_section_index].items,308 .zig_module => |x| x.relocs.items[self.relocs_section_index].items,
299 .object => |x| x.getRelocs(self.relocs_section_index),309 .object => |x| x.getRelocs(self.relocs_section_index),
300 else => unreachable,310 else => unreachable,
...@@ -303,7 +313,7 @@ pub fn relocs(self: Atom, elf_file: *Elf) error{Overflow}![]align(1) const elf.E...@@ -303,7 +313,7 @@ pub fn relocs(self: Atom, elf_file: *Elf) error{Overflow}![]align(1) const elf.E
303313
304pub fn addReloc(self: Atom, elf_file: *Elf, reloc: elf.Elf64_Rela) !void {314pub fn addReloc(self: Atom, elf_file: *Elf, reloc: elf.Elf64_Rela) !void {
305 const gpa = elf_file.base.allocator;315 const gpa = elf_file.base.allocator;
306 const file_ptr = elf_file.file(self.file_index).?;316 const file_ptr = self.file(elf_file).?;
307 assert(file_ptr == .zig_module);317 assert(file_ptr == .zig_module);
308 const zig_module = file_ptr.zig_module;318 const zig_module = file_ptr.zig_module;
309 const rels = &zig_module.relocs.items[self.relocs_section_index];319 const rels = &zig_module.relocs.items[self.relocs_section_index];
...@@ -311,14 +321,14 @@ pub fn addReloc(self: Atom, elf_file: *Elf, reloc: elf.Elf64_Rela) !void {...@@ -311,14 +321,14 @@ pub fn addReloc(self: Atom, elf_file: *Elf, reloc: elf.Elf64_Rela) !void {
311}321}
312322
313pub fn freeRelocs(self: Atom, elf_file: *Elf) void {323pub fn freeRelocs(self: Atom, elf_file: *Elf) void {
314 const file_ptr = elf_file.file(self.file_index).?;324 const file_ptr = self.file(elf_file).?;
315 assert(file_ptr == .zig_module);325 assert(file_ptr == .zig_module);
316 const zig_module = file_ptr.zig_module;326 const zig_module = file_ptr.zig_module;
317 zig_module.relocs.items[self.relocs_section_index].clearRetainingCapacity();327 zig_module.relocs.items[self.relocs_section_index].clearRetainingCapacity();
318}328}
319329
320pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {330pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {
321 const file_ptr = elf_file.file(self.file_index).?;331 const file_ptr = self.file(elf_file).?;
322 const rels = try self.relocs(elf_file);332 const rels = try self.relocs(elf_file);
323 var i: usize = 0;333 var i: usize = 0;
324 while (i < rels.len) : (i += 1) {334 while (i < rels.len) : (i += 1) {
...@@ -392,7 +402,7 @@ fn reportUndefined(...@@ -392,7 +402,7 @@ fn reportUndefined(
392 rel: elf.Elf64_Rela,402 rel: elf.Elf64_Rela,
393 undefs: anytype,403 undefs: anytype,
394) !void {404) !void {
395 const rel_esym = switch (elf_file.file(self.file_index).?) {405 const rel_esym = switch (self.file(elf_file).?) {
396 .zig_module => |x| x.elfSym(rel.r_sym()).*,406 .zig_module => |x| x.elfSym(rel.r_sym()).*,
397 .object => |x| x.symtab[rel.r_sym()],407 .object => |x| x.symtab[rel.r_sym()],
398 else => unreachable,408 else => unreachable,
...@@ -416,7 +426,7 @@ fn reportUndefined(...@@ -416,7 +426,7 @@ fn reportUndefined(
416pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void {426pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void {
417 relocs_log.debug("0x{x}: {s}", .{ self.value, self.name(elf_file) });427 relocs_log.debug("0x{x}: {s}", .{ self.value, self.name(elf_file) });
418428
419 const file_ptr = elf_file.file(self.file_index).?;429 const file_ptr = self.file(elf_file).?;
420 var stream = std.io.fixedBufferStream(code);430 var stream = std.io.fixedBufferStream(code);
421 const cwriter = stream.writer();431 const cwriter = stream.writer();
422432
...@@ -619,7 +629,7 @@ fn format2(...@@ -619,7 +629,7 @@ fn format2(
619 // try writer.writeAll(" }");629 // try writer.writeAll(" }");
620 // }630 // }
621 const gc_sections = if (elf_file.base.options.gc_sections) |gc_sections| gc_sections else false;631 const gc_sections = if (elf_file.base.options.gc_sections) |gc_sections| gc_sections else false;
622 if (gc_sections and !atom.alive) {632 if (gc_sections and !atom.flags.alive) {
623 try writer.writeAll(" : [*]");633 try writer.writeAll(" : [*]");
624 }634 }
625}635}
...@@ -629,6 +639,17 @@ fn format2(...@@ -629,6 +639,17 @@ fn format2(
629// future.639// future.
630pub const Index = u16;640pub const Index = u16;
631641
642pub const Flags = packed struct {
643 /// Specifies whether this atom is alive or has been garbage collected.
644 alive: bool = false,
645
646 /// Specifies if the atom has been visited during garbage collection.
647 visited: bool = false,
648
649 /// Specifies whether this atom has been allocated in the output section.
650 allocated: bool = false,
651};
652
632const x86_64 = struct {653const x86_64 = struct {
633 pub fn relaxGotpcrelx(code: []u8) !void {654 pub fn relaxGotpcrelx(code: []u8) !void {
634 const old_inst = disassemble(code) orelse return error.RelaxFail;655 const old_inst = disassemble(code) orelse return error.RelaxFail;
src/link/Elf/Object.zig+6-6
...@@ -180,7 +180,7 @@ fn addAtom(...@@ -180,7 +180,7 @@ fn addAtom(
180 atom.file_index = self.index;180 atom.file_index = self.index;
181 atom.input_section_index = shndx;181 atom.input_section_index = shndx;
182 atom.output_section_index = try self.getOutputSectionIndex(elf_file, shdr);182 atom.output_section_index = try self.getOutputSectionIndex(elf_file, shdr);
183 atom.alive = true;183 atom.flags.alive = true;
184 self.atoms.items[shndx] = atom_index;184 self.atoms.items[shndx] = atom_index;
185185
186 if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) {186 if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) {
...@@ -424,7 +424,7 @@ fn filterRelocs(...@@ -424,7 +424,7 @@ fn filterRelocs(
424pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void {424pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void {
425 for (self.atoms.items) |atom_index| {425 for (self.atoms.items) |atom_index| {
426 const atom = elf_file.atom(atom_index) orelse continue;426 const atom = elf_file.atom(atom_index) orelse continue;
427 if (!atom.alive) continue;427 if (!atom.flags.alive) continue;
428 const shdr = atom.inputShdr(elf_file);428 const shdr = atom.inputShdr(elf_file);
429 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;429 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
430 if (shdr.sh_type == elf.SHT_NOBITS) continue;430 if (shdr.sh_type == elf.SHT_NOBITS) continue;
...@@ -458,7 +458,7 @@ pub fn resolveSymbols(self: *Object, elf_file: *Elf) void {...@@ -458,7 +458,7 @@ pub fn resolveSymbols(self: *Object, elf_file: *Elf) void {
458 if (esym.st_shndx != elf.SHN_ABS and esym.st_shndx != elf.SHN_COMMON) {458 if (esym.st_shndx != elf.SHN_ABS and esym.st_shndx != elf.SHN_COMMON) {
459 const atom_index = self.atoms.items[esym.st_shndx];459 const atom_index = self.atoms.items[esym.st_shndx];
460 const atom = elf_file.atom(atom_index) orelse continue;460 const atom = elf_file.atom(atom_index) orelse continue;
461 if (!atom.alive) continue;461 if (!atom.flags.alive) continue;
462 }462 }
463463
464 const global = elf_file.symbol(index);464 const global = elf_file.symbol(index);
...@@ -553,7 +553,7 @@ pub fn checkDuplicates(self: *Object, elf_file: *Elf) void {...@@ -553,7 +553,7 @@ pub fn checkDuplicates(self: *Object, elf_file: *Elf) void {
553 if (this_sym.st_shndx != elf.SHN_ABS) {553 if (this_sym.st_shndx != elf.SHN_ABS) {
554 const atom_index = self.atoms.items[this_sym.st_shndx];554 const atom_index = self.atoms.items[this_sym.st_shndx];
555 const atom = elf_file.atom(atom_index) orelse continue;555 const atom = elf_file.atom(atom_index) orelse continue;
556 if (!atom.alive) continue;556 if (!atom.flags.alive) continue;
557 }557 }
558558
559 elf_file.base.fatal("multiple definition: {}: {}: {s}", .{559 elf_file.base.fatal("multiple definition: {}: {}: {s}", .{
...@@ -628,7 +628,7 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {...@@ -628,7 +628,7 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {
628pub fn updateSymtabSize(self: *Object, elf_file: *Elf) void {628pub fn updateSymtabSize(self: *Object, elf_file: *Elf) void {
629 for (self.locals()) |local_index| {629 for (self.locals()) |local_index| {
630 const local = elf_file.symbol(local_index);630 const local = elf_file.symbol(local_index);
631 if (local.atom(elf_file)) |atom| if (!atom.alive) continue;631 if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
632 const esym = local.elfSym(elf_file);632 const esym = local.elfSym(elf_file);
633 switch (esym.st_type()) {633 switch (esym.st_type()) {
634 elf.STT_SECTION, elf.STT_NOTYPE => continue,634 elf.STT_SECTION, elf.STT_NOTYPE => continue,
...@@ -641,7 +641,7 @@ pub fn updateSymtabSize(self: *Object, elf_file: *Elf) void {...@@ -641,7 +641,7 @@ pub fn updateSymtabSize(self: *Object, elf_file: *Elf) void {
641 for (self.globals()) |global_index| {641 for (self.globals()) |global_index| {
642 const global = elf_file.symbol(global_index);642 const global = elf_file.symbol(global_index);
643 if (global.file(elf_file)) |file| if (file.index() != self.index) continue;643 if (global.file(elf_file)) |file| if (file.index() != self.index) continue;
644 if (global.atom(elf_file)) |atom| if (!atom.alive) continue;644 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
645 global.flags.output_symtab = true;645 global.flags.output_symtab = true;
646 if (global.isLocal()) {646 if (global.isLocal()) {
647 self.output_symtab_size.nlocals += 1;647 self.output_symtab_size.nlocals += 1;
src/link/Elf/ZigModule.zig+7-5
...@@ -53,17 +53,19 @@ pub fn addAtom(self: *ZigModule, elf_file: *Elf) !Symbol.Index {...@@ -53,17 +53,19 @@ pub fn addAtom(self: *ZigModule, elf_file: *Elf) !Symbol.Index {
53 const gpa = elf_file.base.allocator;53 const gpa = elf_file.base.allocator;
5454
55 const atom_index = try elf_file.addAtom();55 const atom_index = try elf_file.addAtom();
56 const symbol_index = try elf_file.addSymbol();
57 const esym_index = try self.addLocalEsym(gpa);
58
56 try self.atoms.putNoClobber(gpa, atom_index, {});59 try self.atoms.putNoClobber(gpa, atom_index, {});
60 try self.local_symbols.append(gpa, symbol_index);
61
57 const atom_ptr = elf_file.atom(atom_index).?;62 const atom_ptr = elf_file.atom(atom_index).?;
58 atom_ptr.file_index = self.index;63 atom_ptr.file_index = self.index;
5964
60 const symbol_index = try elf_file.addSymbol();
61 try self.local_symbols.append(gpa, symbol_index);
62 const symbol_ptr = elf_file.symbol(symbol_index);65 const symbol_ptr = elf_file.symbol(symbol_index);
63 symbol_ptr.file_index = self.index;66 symbol_ptr.file_index = self.index;
64 symbol_ptr.atom_index = atom_index;67 symbol_ptr.atom_index = atom_index;
6568
66 const esym_index = try self.addLocalEsym(gpa);
67 const esym = &self.local_esyms.items[esym_index];69 const esym = &self.local_esyms.items[esym_index];
68 esym.st_shndx = atom_index;70 esym.st_shndx = atom_index;
69 symbol_ptr.esym_index = esym_index;71 symbol_ptr.esym_index = esym_index;
...@@ -86,7 +88,7 @@ pub fn resolveSymbols(self: *ZigModule, elf_file: *Elf) void {...@@ -86,7 +88,7 @@ pub fn resolveSymbols(self: *ZigModule, elf_file: *Elf) void {
86 if (esym.st_shndx != elf.SHN_ABS and esym.st_shndx != elf.SHN_COMMON) {88 if (esym.st_shndx != elf.SHN_ABS and esym.st_shndx != elf.SHN_COMMON) {
87 const atom_index = esym.st_shndx;89 const atom_index = esym.st_shndx;
88 const atom = elf_file.atom(atom_index) orelse continue;90 const atom = elf_file.atom(atom_index) orelse continue;
89 if (!atom.alive) continue;91 if (!atom.flags.alive) continue;
90 }92 }
9193
92 const global = elf_file.symbol(index);94 const global = elf_file.symbol(index);
...@@ -141,7 +143,7 @@ pub fn claimUnresolved(self: *ZigModule, elf_file: *Elf) void {...@@ -141,7 +143,7 @@ pub fn claimUnresolved(self: *ZigModule, elf_file: *Elf) void {
141pub fn scanRelocs(self: *ZigModule, elf_file: *Elf, undefs: anytype) !void {143pub fn scanRelocs(self: *ZigModule, elf_file: *Elf, undefs: anytype) !void {
142 for (self.atoms.keys()) |atom_index| {144 for (self.atoms.keys()) |atom_index| {
143 const atom = elf_file.atom(atom_index) orelse continue;145 const atom = elf_file.atom(atom_index) orelse continue;
144 if (!atom.alive) continue;146 if (!atom.flags.alive) continue;
145 try atom.scanRelocs(elf_file, undefs);147 try atom.scanRelocs(elf_file, undefs);
146 }148 }
147}149}
src/link/Elf/file.zig+16
...@@ -89,6 +89,21 @@ pub const File = union(enum) {...@@ -89,6 +89,21 @@ pub const File = union(enum) {
89 }89 }
90 }90 }
9191
92 pub fn atoms(file: File) []const Atom.Index {
93 return switch (file) {
94 .linker_defined => unreachable,
95 .zig_module => |x| x.atoms.keys(),
96 .object => |x| x.atoms.items,
97 };
98 }
99
100 pub fn locals(file: File) []const Symbol.Index {
101 return switch (file) {
102 .linker_defined => unreachable,
103 inline else => |x| x.locals(),
104 };
105 }
106
92 pub fn globals(file: File) []const Symbol.Index {107 pub fn globals(file: File) []const Symbol.Index {
93 return switch (file) {108 return switch (file) {
94 inline else => |x| x.globals(),109 inline else => |x| x.globals(),
...@@ -110,6 +125,7 @@ const std = @import("std");...@@ -110,6 +125,7 @@ const std = @import("std");
110const elf = std.elf;125const elf = std.elf;
111126
112const Allocator = std.mem.Allocator;127const Allocator = std.mem.Allocator;
128const Atom = @import("Atom.zig");
113const Elf = @import("../Elf.zig");129const Elf = @import("../Elf.zig");
114const LinkerDefined = @import("LinkerDefined.zig");130const LinkerDefined = @import("LinkerDefined.zig");
115const Object = @import("Object.zig");131const Object = @import("Object.zig");
src/link/Elf/synthetic_sections.zig+15
...@@ -169,6 +169,21 @@ pub const GotSection = struct {...@@ -169,6 +169,21 @@ pub const GotSection = struct {
169 }169 }
170 }170 }
171171
172 pub fn writeAllEntries(got: GotSection, elf_file: *Elf, writer: anytype) !void {
173 assert(!got.dirty);
174 const entry_size: u16 = elf_file.archPtrWidthBytes();
175 const endian = elf_file.base.options.target.cpu.arch.endian();
176 for (got.entries.items) |entry| {
177 const value = elf_file.symbol(entry.symbol_index).value;
178 switch (entry_size) {
179 2 => try writer.writeInt(u16, @intCast(value), endian),
180 4 => try writer.writeInt(u32, @intCast(value), endian),
181 8 => try writer.writeInt(u64, @intCast(value), endian),
182 else => unreachable,
183 }
184 }
185 }
186
172 // pub fn write(got: GotSection, elf_file: *Elf, writer: anytype) !void {187 // pub fn write(got: GotSection, elf_file: *Elf, writer: anytype) !void {
173 // const is_shared = elf_file.options.output_mode == .lib;188 // const is_shared = elf_file.options.output_mode == .lib;
174 // const apply_relocs = elf_file.options.apply_dynamic_relocs;189 // const apply_relocs = elf_file.options.apply_dynamic_relocs;