Skip to content

Commit

Permalink
write/elf: fix reserving empty section (#514)
Browse files Browse the repository at this point in the history
`reserve` must always return an aligned address, even if the section is empty. This matches the behaviour of `write_align`.

Also both `reserve` and `write_align` must handle the possibility of `sh_addralign` being 0.

Signed-off-by: lzwycc <lzw32321226@163.com>
  • Loading branch information
lzw3232 authored Feb 17, 2023
1 parent d9c2291 commit 9aab18f
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/write/elf/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,19 @@ 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
}

/// 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.
Expand Down

0 comments on commit 9aab18f

Please sign in to comment.