google / bloaty

Bloaty: a size profiler for binaries

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Non-native endianness handling?

awilke opened this issue · comments

I'm not able to load ELFs for an embedded PowerPC platform that I'm using:

$ bloaty ppc_hello_world.elf
bloaty: ELF region out-of-bounds

I'm running on Linux x64, and here is the sample file I'm trying to load.

The exception is being thrown here:

Breakpoint 1, bloaty::(anonymous namespace)::StrictSubstr (data=..., off=off@entry=0, n=134217728)
    at /home/awilke/code/bloaty/src/elf.cc:109
109         THROW("ELF region out-of-bounds");
(gdb) bt
#0  bloaty::(anonymous namespace)::StrictSubstr (data=..., off=off@entry=0, n=134217728)
    at /home/awilke/code/bloaty/src/elf.cc:109
#1  0x00000000004bca0f in Next (this=0x7ffff6aa2930) at /home/awilke/code/bloaty/src/elf.cc:221
#2  NoteIter (section=..., this=0x7ffff6aa2930) at /home/awilke/code/bloaty/src/elf.cc:205
#3  bloaty::(anonymous namespace)::ElfObjectFile::GetBuildId (this=<optimized out>)
    at /home/awilke/code/bloaty/src/elf.cc:1278
#4  0x00000000004a0f4f in bloaty::Bloaty::ScanAndRollupFile (this=0x7fffffffda30, file=0xd0cb20,
    rollup=rollup@entry=0xd0acf0, out_build_id=out_build_id@entry=0xd0ad48)
    at /home/awilke/code/bloaty/src/bloaty.cc:1580
#5  0x00000000004a17e7 in operator() (data=0xd0acf0, __closure=0xd0d718)
    at /home/awilke/code/bloaty/src/bloaty.cc:1673
<snip>

I'm assuming that bloaty is crashing when reading the section header for .PPC.EMB.apuinfo, since that's the only note-type section in this ELF:

$ readelf ppc_hello_world.elf -S | grep NOTE
  [15] .PPC.EMB.apuinfo  NOTE            00000000 0300c8 000018 00      0   0  1

The header values parsed by bloaty don't make sense:

(gdb) frame 1
#1  0x00000000004bca0f in Next (this=0x7ffff6aa2930) at /home/awilke/code/bloaty/src/elf.cc:221
221           name_ = StrictSubstr(remaining_, 0, ptr->n_namesz);
(gdb) p/x ptr->n_namesz
$19 = 0x8000000
(gdb) p/x ptr->n_descsz
$20 = 0x4000000
(gdb) p/x ptr->n_type
$21 = 0x2000000
(gdb)

However, these appear to be byte-reversed interpretations of the expected values in the header. See this ABI reference, section 2.2.1. The expected values are:

  • name size = 8 bytes
  • type = 2

This is really pushing my knowledge of the ELF format, but it appears that this is an endianness issue when parsing the section header? PowerPC is a big-endian platform, and my host platform is little-endian.

$ readelf ppc_hello_world.elf -h | grep endian
  Data:                              2's complement, big endian

Thanks for any help you can provide!

I have also found this issue when trying to read powerpc64 (big endian). Here is a gist of how to recreate it:

It does appear to be related to the way that the NOTE sections are encoded. If I strip out that section bloaty will work.

Thanks for the repro! I have a fix, see attached PR.

I tested #182 for powerpc64 and it works as expected now

I pulled 1b5f8fc, but I'm still getting that same error message with ppc_hello_world.elf. If I strip the .PPC.EMB.apuinfo section from that ELF (like @MattCatz), it does load as expected.

Hmm, is there any chance you are using a stale build? The fix works for both @MattCatz and me, so it if it's failing for you it seems that either the bloaty binary or the input must be different.

Do you have a link or repro instructions for how to create your failing binary?

Ah yup, just PEBKAC. I was running a stale binary, works now!

Thanks for the fix!