Skip to content

Commit

Permalink
Properly handle sections which end in a call
Browse files Browse the repository at this point in the history
  • Loading branch information
koute committed Jan 5, 2024
1 parent 3c39779 commit 05113de
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions crates/polkavm-linker/src/program_from_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5452,13 +5452,27 @@ pub fn program_from_elf(config: Config, data: &[u8]) -> Result<ProgramBlob, Prog

for &section_index in &sections_code {
let section = elf.section_by_index(section_index);
let initial_instruction_count = instructions.len();
parse_code_section(
section,
&import_by_location,
&relocations,
&mut instruction_overrides,
&mut instructions,
)?;

if instructions.len() > initial_instruction_count {
// Sometimes a section ends with a `call`, which (considering sections can be reordered) would put
// the return address out of bounds of the section, so let's inject an `unimp` here to make sure this doesn't happen.
//
// If it ends up being unnecessary the optimizer will remove it anyway.
let last_source = instructions.last().unwrap().0;
let source = Source {
section_index: last_source.section_index,
offset_range: (last_source.offset_range.end..last_source.offset_range.end + 4).into(),
};
instructions.push((source, InstExt::Control(ControlInst::Unimplemented)));
}
}

if !instruction_overrides.is_empty() {
Expand Down

0 comments on commit 05113de

Please sign in to comment.