From 8694eaa004ac946d4267a8781e0cd25207f966eb Mon Sep 17 00:00:00 2001 From: eupn <36292692+eupn@users.noreply.github.com> Date: Mon, 27 Mar 2023 18:41:45 +0400 Subject: [PATCH] elf.rs: fix alignment edge case (take 2) --- tools/src/elf.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tools/src/elf.rs b/tools/src/elf.rs index 36e2d92bf..46da8ca44 100644 --- a/tools/src/elf.rs +++ b/tools/src/elf.rs @@ -372,11 +372,12 @@ pub fn process_minielf(b: &[u8]) -> Result { if s.get_type() != Ok(ShType::NoBits) { let section_data = s.raw_data(&elf); let pad_amount = if let Some(next_section) = section_iter.peek() { - if next_section.get_type() != Ok(ShType::NoBits) { - if section_data.len() % next_section.align() as usize != 0 { - next_section.align() as usize - (section_data.len() % next_section.align() as usize) + if section_data.len() % next_section.align() as usize != 0 { + let pad_amount = next_section.align() as usize - (section_data.len() % next_section.align() as usize); + if s.address() + size + pad_amount as u64 > next_section.address() { + (next_section.address() - (s.address() + size)) as usize } else { - 0 + pad_amount } } else { 0