authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-05 21:59:58+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:04+02:00
log4af5caa81f2d1395857d0f64c5a454b3e9240969
treea49b860059facc47c0b569a422d8e7801ef9e034
parent5fa90afb646bfba4ca3b23a354ef171708e002e9

elf: add missing generators for special dynamic sections


2 files changed, 53 insertions(+), 1 deletions(-)

src/link/Elf.zig+52
...@@ -269,6 +269,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option...@@ -269,6 +269,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
269 // Append null byte to string tables269 // Append null byte to string tables
270 try self.shstrtab.buffer.append(allocator, 0);270 try self.shstrtab.buffer.append(allocator, 0);
271 try self.strtab.buffer.append(allocator, 0);271 try self.strtab.buffer.append(allocator, 0);
272 try self.dynstrtab.buffer.append(allocator, 0);
272273
273 if (options.module != null and !options.use_llvm) {274 if (options.module != null and !options.use_llvm) {
274 if (!options.strip) {275 if (!options.strip) {
...@@ -1384,6 +1385,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1384,6 +1385,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1384 try self.file(index).?.object.addAtomsToOutputSections(self);1385 try self.file(index).?.object.addAtomsToOutputSections(self);
1385 }1386 }
1386 try self.sortInitFini();1387 try self.sortInitFini();
1388 try self.setDynamicSection();
1389 self.sortDynamicSymtab();
1390 try self.setHashSections();
1391 try self.setVersionSymtab();
1387 try self.updateSectionSizes();1392 try self.updateSectionSizes();
13881393
1389 try self.allocateSections();1394 try self.allocateSections();
...@@ -3748,6 +3753,7 @@ fn initSections(self: *Elf) !void {...@@ -3748,6 +3753,7 @@ fn initSections(self: *Elf) !void {
3748 .type = elf.SHT_DYNSYM,3753 .type = elf.SHT_DYNSYM,
3749 .addralign = @alignOf(elf.Elf64_Sym),3754 .addralign = @alignOf(elf.Elf64_Sym),
3750 .entsize = @sizeOf(elf.Elf64_Sym),3755 .entsize = @sizeOf(elf.Elf64_Sym),
3756 .info = 1,
3751 });3757 });
3752 self.hash_section_index = try self.addSection(.{3758 self.hash_section_index = try self.addSection(.{
3753 .name = ".hash",3759 .name = ".hash",
...@@ -3873,6 +3879,52 @@ fn sortInitFini(self: *Elf) !void {...@@ -3873,6 +3879,52 @@ fn sortInitFini(self: *Elf) !void {
3873 }3879 }
3874}3880}
38753881
3882fn setDynamicSection(self: *Elf) !void {
3883 if (self.dynamic_section_index == null) return;
3884
3885 for (self.shared_objects.items) |index| {
3886 const shared_object = self.file(index).?.shared_object;
3887 if (!shared_object.alive) continue;
3888 try self.dynamic.addNeeded(shared_object, self);
3889 }
3890
3891 if (self.base.options.soname) |soname| {
3892 try self.dynamic.setSoname(soname, self);
3893 }
3894
3895 try self.dynamic.setRpath(self.base.options.rpath_list, self);
3896}
3897
3898fn sortDynamicSymtab(self: *Elf) void {
3899 if (self.gnu_hash_section_index == null) return;
3900 self.dynsym.sort(self);
3901}
3902
3903fn setVersionSymtab(self: *Elf) !void {
3904 if (self.versym_section_index == null) return;
3905 try self.versym.resize(self.base.allocator, self.dynsym.count());
3906 self.versym.items[0] = elf.VER_NDX_LOCAL;
3907 for (self.dynsym.entries.items, 1..) |entry, i| {
3908 const sym = self.symbol(entry.symbol_index);
3909 self.versym.items[i] = sym.version_index;
3910 }
3911
3912 if (self.verneed_section_index) |shndx| {
3913 try self.verneed.generate(self);
3914 const shdr = &self.shdrs.items[shndx];
3915 shdr.sh_info = @as(u32, @intCast(self.verneed.verneed.items.len));
3916 }
3917}
3918
3919fn setHashSections(self: *Elf) !void {
3920 if (self.hash_section_index != null) {
3921 try self.hash.generate(self);
3922 }
3923 if (self.gnu_hash_section_index != null) {
3924 try self.gnu_hash.calcSize(self);
3925 }
3926}
3927
3876fn sectionRank(self: *Elf, shdr: elf.Elf64_Shdr) u8 {3928fn sectionRank(self: *Elf, shdr: elf.Elf64_Shdr) u8 {
3877 const name = self.shstrtab.getAssumeExists(shdr.sh_name);3929 const name = self.shstrtab.getAssumeExists(shdr.sh_name);
3878 const flags = shdr.sh_flags;3930 const flags = shdr.sh_flags;
src/link/Elf/synthetic_sections.zig+1-1
...@@ -9,7 +9,7 @@ pub const DynamicSection = struct {...@@ -9,7 +9,7 @@ pub const DynamicSection = struct {
99
10 pub fn addNeeded(dt: *DynamicSection, shared: *SharedObject, elf_file: *Elf) !void {10 pub fn addNeeded(dt: *DynamicSection, shared: *SharedObject, elf_file: *Elf) !void {
11 const gpa = elf_file.base.allocator;11 const gpa = elf_file.base.allocator;
12 const off = try elf_file.dynstrtab.insert(gpa, shared.getSoname());12 const off = try elf_file.dynstrtab.insert(gpa, shared.soname());
13 try dt.needed.append(gpa, off);13 try dt.needed.append(gpa, off);
14 }14 }
1515