From 476adb1853131f36c8151bcde8b1fc527c6b03fa Mon Sep 17 00:00:00 2001 From: lzwycc Date: Thu, 16 Feb 2023 18:19:32 +0800 Subject: [PATCH] write: fix reserving empty section error if we copy a elf with a empty section and the sh_addralign is not 0, it will return a unaligned offset. Signed-off-by: lzwycc --- src/write/elf/writer.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/write/elf/writer.rs b/src/write/elf/writer.rs index 3c9d85a1..2caddf1e 100644 --- a/src/write/elf/writer.rs +++ b/src/write/elf/writer.rs @@ -216,10 +216,9 @@ impl<'a> Writer<'a> { /// /// Returns the aligned offset of the start of the range. pub fn reserve(&mut self, len: usize, align_start: usize) -> usize { - if len == 0 { - return self.len; + if align_start > 1 { + self.len = util::align(self.len, align_start); } - self.len = util::align(self.len, align_start); let offset = self.len; self.len += len; offset @@ -227,7 +226,9 @@ impl<'a> Writer<'a> { /// Write alignment padding bytes. pub fn write_align(&mut self, align_start: usize) { - util::write_align(self.buffer, align_start); + if align_start > 1 { + util::write_align(self.buffer, align_start); + } } /// Write data.