elf2dol: Support section name denylist (#64)

This commit is contained in:
riidefi 2024-07-17 20:02:34 -06:00 committed by GitHub
parent c3c7c2b062
commit cfeacd2c3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 10 deletions

View File

@ -321,6 +321,8 @@ Creates a DOL file from the provided ELF file.
```shell ```shell
$ dtk elf2dol input.elf output.dol $ dtk elf2dol input.elf output.dol
# or, to ignore certain sections
$ dtk elf2dol input.elf output.dol --ignore debug_section1 --ignore debug_section2
``` ```
### map ### map

View File

@ -19,6 +19,9 @@ pub struct Args {
#[argp(positional)] #[argp(positional)]
/// path to output DOL /// path to output DOL
dol_file: PathBuf, dol_file: PathBuf,
/// sections (by name) to ignore
#[argp(option, long = "ignore")]
deny_sections: Vec<String>,
} }
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
@ -61,9 +64,11 @@ pub fn run(args: Args) -> Result<()> {
out.seek(SeekFrom::Start(offset as u64))?; out.seek(SeekFrom::Start(offset as u64))?;
// Text sections // Text sections
for section in for section in obj_file.sections().filter(|s| {
obj_file.sections().filter(|s| section_kind(s) == SectionKind::Text && is_alloc(s.flags())) section_kind(s) == SectionKind::Text
{ && is_alloc(s.flags())
&& is_name_allowed(s, &args.deny_sections)
}) {
log::debug!("Processing text section '{}'", section.name().unwrap_or("[error]")); log::debug!("Processing text section '{}'", section.name().unwrap_or("[error]"));
let address = section.address() as u32; let address = section.address() as u32;
let size = align32(section.size() as u32); let size = align32(section.size() as u32);
@ -79,9 +84,11 @@ pub fn run(args: Args) -> Result<()> {
} }
// Data sections // Data sections
for section in for section in obj_file.sections().filter(|s| {
obj_file.sections().filter(|s| section_kind(s) == SectionKind::Data && is_alloc(s.flags())) section_kind(s) == SectionKind::Data
{ && is_alloc(s.flags())
&& is_name_allowed(s, &args.deny_sections)
}) {
log::debug!("Processing data section '{}'", section.name().unwrap_or("[error]")); log::debug!("Processing data section '{}'", section.name().unwrap_or("[error]"));
let address = section.address() as u32; let address = section.address() as u32;
let size = align32(section.size() as u32); let size = align32(section.size() as u32);
@ -97,10 +104,11 @@ pub fn run(args: Args) -> Result<()> {
} }
// BSS sections // BSS sections
for section in obj_file for section in obj_file.sections().filter(|s| {
.sections() section_kind(s) == SectionKind::UninitializedData
.filter(|s| section_kind(s) == SectionKind::UninitializedData && is_alloc(s.flags())) && is_alloc(s.flags())
{ && is_name_allowed(s, &args.deny_sections)
}) {
let address = section.address() as u32; let address = section.address() as u32;
let size = section.size() as u32; let size = section.size() as u32;
if header.bss_address == 0 { if header.bss_address == 0 {
@ -184,3 +192,8 @@ fn section_kind(section: &object::Section) -> SectionKind {
fn is_alloc(flags: object::SectionFlags) -> bool { fn is_alloc(flags: object::SectionFlags) -> bool {
matches!(flags, object::SectionFlags::Elf { sh_flags } if sh_flags & object::elf::SHF_ALLOC as u64 != 0) matches!(flags, object::SectionFlags::Elf { sh_flags } if sh_flags & object::elf::SHF_ALLOC as u64 != 0)
} }
#[inline]
fn is_name_allowed(s: &object::Section, denied: &[String]) -> bool {
!denied.contains(&s.name().unwrap_or("[error]").to_string())
}