authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-02 17:43:45+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:04+02:00
logf0f19e18c77c435795b3454bd6c34517874f02cb
tree6b5d97f6610909ad6c10a5831e11aa61ff27b388
parent87602092fa21029c872c217a6544a2c0ced2f34f

elf: write shdr_table in flush


1 files changed, 4 insertions(+), 38 deletions(-)

src/link/Elf.zig+4-38
...@@ -842,42 +842,6 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -842,42 +842,6 @@ pub fn populateMissingMetadata(self: *Elf) !void {
842 }842 }
843 }843 }
844844
845 const shsize: u64 = switch (self.ptr_width) {
846 .p32 => @sizeOf(elf.Elf32_Shdr),
847 .p64 => @sizeOf(elf.Elf64_Shdr),
848 };
849 const shalign: u16 = switch (self.ptr_width) {
850 .p32 => @alignOf(elf.Elf32_Shdr),
851 .p64 => @alignOf(elf.Elf64_Shdr),
852 };
853 if (self.shdr_table_offset == null) {
854 self.shdr_table_offset = self.findFreeSpace(self.shdrs.items.len * shsize, shalign);
855 self.shdr_table_dirty = true;
856 }
857
858 {
859 // Iterate over symbols, populating free_list and last_text_block.
860 if (self.symbols.items.len != 1) {
861 @panic("TODO implement setting up free_list and last_text_block from existing ELF file");
862 }
863 // We are starting with an empty file. The default values are correct, null and empty list.
864 }
865
866 if (self.shdr_table_dirty) {
867 // We need to find out what the max file offset is according to section headers.
868 // Otherwise, we may end up with an ELF binary with file size not matching the final section's
869 // offset + it's filesize.
870 var max_file_offset: u64 = 0;
871
872 for (self.shdrs.items) |shdr| {
873 if (shdr.sh_offset + shdr.sh_size > max_file_offset) {
874 max_file_offset = shdr.sh_offset + shdr.sh_size;
875 }
876 }
877
878 try self.base.file.?.pwriteAll(&[_]u8{0}, max_file_offset);
879 }
880
881 if (self.base.options.module) |module| {845 if (self.base.options.module) |module| {
882 if (self.zig_module_index == null and !self.base.options.use_llvm) {846 if (self.zig_module_index == null and !self.base.options.use_llvm) {
883 const index: File.Index = @intCast(try self.files.addOne(gpa));847 const index: File.Index = @intCast(try self.files.addOne(gpa));
...@@ -1469,9 +1433,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1469,9 +1433,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1469 .p32 => @alignOf(elf.Elf32_Shdr),1433 .p32 => @alignOf(elf.Elf32_Shdr),
1470 .p64 => @alignOf(elf.Elf64_Shdr),1434 .p64 => @alignOf(elf.Elf64_Shdr),
1471 };1435 };
1472 const allocated_size = self.allocatedSize(self.shdr_table_offset.?);
1473 const needed_size = self.shdrs.items.len * shsize;1436 const needed_size = self.shdrs.items.len * shsize;
14741437 const allocated_size = if (self.shdr_table_offset) |off|
1438 self.allocatedSize(off)
1439 else
1440 0;
1475 if (needed_size > allocated_size) {1441 if (needed_size > allocated_size) {
1476 self.shdr_table_offset = null; // free the space1442 self.shdr_table_offset = null; // free the space
1477 self.shdr_table_offset = self.findFreeSpace(needed_size, shalign);1443 self.shdr_table_offset = self.findFreeSpace(needed_size, shalign);