tock / elf2tab

Tool to create Tock Application Bundles from ELF files.
MIT License
15 stars 33 forks source link

Uses program headers to create TBF File #58

Closed bincsh closed 1 year ago

bincsh commented 2 years ago

Replaces the section iterator to create the TBF data in favor of iterating over program headers and using the program segments structure.

Tested this change on all libtock-c examples compiled for Risc-V and ARM. From 1716 ELFs, using elf2tab/master and elf2tab/PR produce all 1716 TBFs with the same file size. 1704 TBF were binary identical. The remaining 12 TBFs, elf2tab/PR adds the missing unwinding information (from .ARM.exidx, present in the program headers), which elf2tab/master doesn't.

Using program headers is the method most (if not all) ELF loaders utilize when loading ELFs in memory. Elf2Tock will look for .crt0_header section, which is the first expected data after the TBF header in memory. This is necessary since some linkers are mapping the entire ELF file into memory (i.e. program header offset = 0). The code verifies this corner case and adjusts the offset within the initial program segment to ensure .crt0_header section content is the first thing after TBF headers.

bradjc commented 2 years ago

This is necessary since some linkers are mapping the entire ELF file into memory (i.e. program header offset = 0). The code verifies this corner case and adjusts the offset within the initial program segment to ensure .crt0_header section content is the first thing after TBF headers.

Can we dig into this more? What do these ELFs look like? This sounds like an issue on the linking side that we need to investigate.

bradjc commented 1 year ago

This is pretty close. I'm currently working on a few updates. We should be able to get rid of all of the hacky stuff we have been doing to cobble together TBF binaries.

bradjc commented 1 year ago

I added a commit which I think gets this PR to the point where it fully removes the kludgy ELF parsing and replaces it with something more principled based on the actual ELF files

One note, @dcz-self, this removes the "check for the first section in the segment" logic when determining addresses. Since we are now including entire segments and not on a section-by-section basis, it doesn't make sense to calculate from the middle of a segment. The TBF will now contain any padding data at the beginning of the segment. I think this is a more principled approach.

bincsh commented 1 year ago

Your commit looks good to me. Is there anything else missing?

bradjc commented 1 year ago

No I think this PR is ready for a final review.

jrvanwhy commented 1 year ago

I gave this a try with libtock-rs's examples on an emulated HiFive and it seems to work fine :-)