authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-09 23:45:50+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-10 08:12:02+01:00
log57357c43e3b56fd636cd08af591c50a08223b654
tree5bb2099de0368f3ef2a67cc8ee0226d6e2d2218f
parent65c812842dfb0db9a4c6f5af04719ebf18f2d169

elf: pad out file to the required size when init data

We need to pad out the file to the required maximum size equal the final section's offset plus the section's size. We only need to this when populating initial metadata and only when section header was updated.

1 files changed, 15 insertions(+), 0 deletions(-)

src/link/Elf.zig+15
......@@ -922,6 +922,21 @@ pub fn populateMissingMetadata(self: *Elf) !void {
922922 }
923923 // We are starting with an empty file. The default values are correct, null and empty list.
924924 }
925
926 if (self.shdr_table_dirty) {
927 // We need to find out what the max file offset is according to section headers.
928 // Otherwise, we may end up with an ELF binary with file size not matching the final section's
929 // offset + it's filesize.
930 var max_file_offset: u64 = 0;
931
932 for (self.sections.items) |shdr| {
933 if (shdr.sh_offset + shdr.sh_size > max_file_offset) {
934 max_file_offset = shdr.sh_offset + shdr.sh_size;
935 }
936 }
937
938 try self.base.file.?.pwriteAll(&[_]u8{0}, max_file_offset);
939 }
925940}
926941
927942pub const abbrev_compile_unit = 1;