forked from M-Labs/nac3
Compare commits
2 Commits
65fa85815a
...
b0526ba29f
Author | SHA1 | Date | |
---|---|---|---|
b0526ba29f | |||
0a481ec880 |
@ -178,6 +178,7 @@ pub struct Linker<'a> {
|
||||
section_map: HashMap<usize, usize>,
|
||||
image: Vec<u8>,
|
||||
load_offset: u32,
|
||||
image_offset: u32,
|
||||
rela_dyn_relas: Vec<Elf32_Rela>,
|
||||
}
|
||||
|
||||
@ -196,15 +197,26 @@ impl<'a> Linker<'a> {
|
||||
|
||||
// Maintain alignment requirement specified in sh_addralign
|
||||
let align = shdr.sh_addralign;
|
||||
let padding = (align - (self.load_offset % align)) % align;
|
||||
self.load_offset += padding;
|
||||
let load_padding = (align - (self.load_offset % align)) % align;
|
||||
let image_padding = (align - (self.image_offset % align)) % align;
|
||||
|
||||
elf_shdr.sh_addr =
|
||||
if (shdr.sh_flags as usize & SHF_ALLOC) == SHF_ALLOC { self.load_offset } else { 0 };
|
||||
elf_shdr.sh_offset = self.load_offset;
|
||||
let section_load_offset = if (shdr.sh_flags as usize & SHF_ALLOC) == SHF_ALLOC {
|
||||
self.load_offset + load_padding
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let section_image_offset = self.image_offset + image_padding;
|
||||
|
||||
elf_shdr.sh_addr = section_load_offset;
|
||||
elf_shdr.sh_offset = section_image_offset;
|
||||
self.elf_shdrs.push(SectionRecord { shdr: elf_shdr, name: sh_name_str, data });
|
||||
|
||||
self.load_offset += shdr.sh_size;
|
||||
if (shdr.sh_flags as usize & SHF_ALLOC) == SHF_ALLOC {
|
||||
self.load_offset = section_load_offset + shdr.sh_size;
|
||||
}
|
||||
if shdr.sh_type as usize != SHT_NOBITS {
|
||||
self.image_offset = section_image_offset + shdr.sh_size;
|
||||
}
|
||||
|
||||
self.elf_shdrs.len() - 1
|
||||
}
|
||||
@ -754,6 +766,7 @@ impl<'a> Linker<'a> {
|
||||
section_map,
|
||||
image,
|
||||
load_offset: elf_sh_data_off as u32,
|
||||
image_offset: elf_sh_data_off as u32,
|
||||
rela_dyn_relas,
|
||||
};
|
||||
|
||||
@ -1302,8 +1315,7 @@ impl<'a> Linker<'a> {
|
||||
let bss_elf_index = linker.load_section(
|
||||
shdr,
|
||||
section_name,
|
||||
data[shdr.sh_offset as usize..(shdr.sh_offset + shdr.sh_size) as usize]
|
||||
.to_vec(),
|
||||
vec![0; 0], // NOBITS section has no data
|
||||
);
|
||||
linker.section_map.insert(bss_section_index, bss_elf_index);
|
||||
|
||||
@ -1401,11 +1413,13 @@ impl<'a> Linker<'a> {
|
||||
linker.implement_eh_frame_hdr()?;
|
||||
}
|
||||
|
||||
// Load all section data into the image
|
||||
// Load all non-NOBITS section data into the image
|
||||
for rec in &linker.elf_shdrs[1..] {
|
||||
if rec.shdr.sh_type as usize != SHT_NOBITS {
|
||||
linker.image.extend(vec![0; (rec.shdr.sh_offset as usize) - linker.image.len()]);
|
||||
linker.image.extend(&rec.data);
|
||||
}
|
||||
}
|
||||
|
||||
// Load all section headers to the image
|
||||
let alignment = (4 - (linker.image.len() % 4)) % 4;
|
||||
|
Loading…
x
Reference in New Issue
Block a user