Changed: Detect bitness from .elf header.

This commit is contained in:
sewer56 2023-12-07 17:59:46 +00:00
parent 3e14b2f287
commit 24c8dce0a8

View File

@ -50,9 +50,6 @@ pub struct DumpArgs {
#[argp(switch)] #[argp(switch)]
/// Disable color output. /// Disable color output.
no_color: bool, no_color: bool,
/// Whether to use Little Endian for DWARF parsing.
#[argp(switch)]
little_endian: bool,
} }
pub fn run(args: Args) -> Result<()> { pub fn run(args: Args) -> Result<()> {
@ -72,10 +69,6 @@ fn dump(args: DumpArgs) -> Result<()> {
let syntax = syntax_set.find_syntax_by_name("C++").context("Failed to find syntax")?.clone(); let syntax = syntax_set.find_syntax_by_name("C++").context("Failed to find syntax")?.clone();
// Set Endian // Set Endian
if args.little_endian {
unsafe { ENDIAN = Endian::Little };
}
let file = map_file(&args.in_file)?; let file = map_file(&args.in_file)?;
let buf = file.as_slice(); let buf = file.as_slice();
if buf.starts_with(b"!<arch>\n") { if buf.starts_with(b"!<arch>\n") {
@ -117,6 +110,12 @@ fn dump(args: DumpArgs) -> Result<()> {
} }
} }
} else { } else {
// [.elf] e_ident.ei_data == ELFDATA2LSB
// This offset is constant for ELF32.
if buf[5] == 1 {
unsafe { ENDIAN = Endian::Little };
};
let obj_file = object::read::File::parse(buf)?; let obj_file = object::read::File::parse(buf)?;
let debug_section = obj_file let debug_section = obj_file
.section_by_name(".debug") .section_by_name(".debug")
@ -140,8 +139,8 @@ fn dump_debug_section<W>(
obj_file: &object::File<'_>, obj_file: &object::File<'_>,
debug_section: Section, debug_section: Section,
) -> Result<()> ) -> Result<()>
where where
W: Write + ?Sized, W: Write + ?Sized,
{ {
let mut data = debug_section.uncompressed_data()?.into_owned(); let mut data = debug_section.uncompressed_data()?.into_owned();