Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fixed flash address calculation (alternative version) #49

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,33 @@ pub fn elf_to_tbf<W: Write>(
false
}

/// Helper function to find the start section is inside a
/// given segment.
/// Helper function to find the address of the first section inside a given
/// segment.
///
/// This is necessary because the flash segment is not guaranteed
/// to start at the same address as the start section.
fn find_start_in_segment<'a>(input: &'a elf::File, segment: &elf::types::ProgramHeader) -> Option<&'a elf::Section> {
/// This is necessary because the flash segment is not guaranteed
/// to start at the same address as the first section.
fn find_first_section_address_in_segment<'a>(
input: &'a elf::File,
segment: &elf::types::ProgramHeader,
) -> Option<u32> {
let segment_start = segment.offset as u32;
let segment_size = segment.filesz as u32;
let segment_end = segment_start + segment_size;

let mut first_section_address: Option<u32> = None;
for section in input.sections.iter() {
let section_start = section.shdr.offset as u32;
let section_size = section.shdr.size as u32;
let section_end = section_start + section_size;

if section_start >= segment_start
&& section_end <= segment_end
&& section_size > 0
&& section.shdr.name == ".start"
{
return Some(section);
if section_start >= segment_start && section_end <= segment_end && section_size > 0 {
first_section_address = match first_section_address {
Some(first_address) => Some(cmp::min(first_address, section.shdr.addr as u32)),
None => Some(section.shdr.addr as u32),
};
}
}
None
first_section_address
}

// Do flash address.
Expand All @@ -187,16 +190,36 @@ pub fn elf_to_tbf<W: Write>(
// If this is standard Tock PIC, then this virtual address
// will be at 0x80000000. Otherwise, we interpret this to
// mean that the binary was compiled for a fixed address in
// flash.
if segment.vaddr == 0x80000000 {
// flash. Once we confirm this we do not need to keep
// checking.
if segment.vaddr == 0x80000000 || fixed_address_flash_pic {
fixed_address_flash_pic = true;
} else {
let start = find_start_in_segment(input, segment)
.map(|section| section.shdr.addr as u32);
fixed_address_flash = match (fixed_address_flash, start) {
(Some(prev_addr), Some(start)) =>
Some(cmp::min(prev_addr, start as u32)),
(prev, start) => prev.or(start),
// We need to see if this segment represents the lowest
// address in flash that we are going to specify this
// app needs to be loaded at. To do this we compare this
// segment to any previous and keep track of the lowest
// address. However, we need to use the address of the
// first _section_ in the segment, not just the address
// of the segment, because a linker may insert padding.
let segment_start = find_first_section_address_in_segment(input, segment);

fixed_address_flash = match (fixed_address_flash, segment_start) {
(Some(prev_addr), Some(segment_start)) => {
// We already found a candidate, and we found a
// new candidate. Keep looking for the lowest
// address.
Some(cmp::min(prev_addr, segment_start))
}
(None, Some(segment_start)) => {
// We found our first valid segment and haven't set our
// lowest address yet, so we do that now.
Some(segment_start)
}
(prev_addr, None) => {
// We can't use this segment, so skip.
prev_addr
}
};
}
}
Expand Down