Repository navigation
Conversation
…d their sections Every section of a relocatable ELF object is at address 0, so an address in its DWARF info is an offset into the section its relocation's symbol is in. The ELF view lays those sections out one after another from the image base, but the importer only added the image base, so everything outside the first section landed in the wrong place: with -ffunction-sections every function was at the image base, on Xtensa, where .literal precedes .text, every function was 0x20 too low, and data variables at the start of .data and .bss got the same address, so one of them was lost. When the debug file is a relocatable ELF object, look up where the view put each allocated section and add that to the relocations against symbols in it. Linked files are unaffected.
…tions Loading a RISC-V relocatable object built with -g failed with "Failed to load DWARF info: Unsupported relocation type", so no debug info was imported. object's RelocationMap only accepts absolute relocations, and with linker relaxation GNU as encodes differences between code labels as relocation pairs at the same offset: R_RISCV_ADD*/SUB* (high_pc, line table and FDE lengths), R_RISCV_SET6/SUB6 and SET8/SUB8 (CFA advances) and R_RISCV_SET_ULEB128/SUB_ULEB128 (location and range list offsets). Apply these to the section data before handing it to gimli, in order, as binutils' perform_relocation does, following the psABI formulas. The object isn't relaxed, so this gives the right difference. ULEB128 values keep the length the assembler gave them, and SUB_ULEB128's addend is ignored like binutils does. R_RISCV_NONE and R_RISCV_RELAX are skipped, and R_RISCV_32/64 still go through the relocation map, which moves them to where the view placed their section. The pairs use the symbols' offsets into their sections as S, without the section's address in the view: both labels of a pair are in the same section, so their difference doesn't depend on where it was placed.
This was referenced Nov 19, 2026
[DWARF Import] Fix addresses for PIE and rebased views, relocatable objects and RISC-V objects
#8654
Closed
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8729
Fixes #8730
Supersedes #8654: #8653, its first fix, was fixed separately on
dev(d263624, 2699a0d), and this is the other two commits rebased ontodev.DWARF import puts functions and data variables at the wrong addresses in relocatable objects (
ET_REL), and RISC-V objects built with-gdon't import at all. There are two commits, one per issue; each can be reviewed on its own.1. Place object file addresses where the view put their sections
All sections of a
.ohave address 0, so a DWARF address is an offset into the section of its relocation's symbol. The importer only adds the image base, so anything outside the first section lands at the wrong address. With-ffunction-sections, every function lands on the first one. On Xtensa, where.literalprecedes.text, every function is 0x20 too low. Data variables at the start of.dataand.bssget the same address, so one of them is lost.For a relocatable ELF debug file, each allocated section is looked up in the view by name (sizes must match; the ELF view's
<name>#<index>duplicates resolve too).RelocationMap::addadds that section's address to relocations against its symbols, so relocated addresses, including.debug_frame, land where the view placed the section. The addresses are taken before rebasing, sincepost_processrebases them afterwards like any other file's. Linked files, the.gnu_debugaltlinkfile anddwarfdumpget an empty map and are unchanged.-fcommonsymbols have no section and are not relocated.2. Apply RISC-V label-difference relocations
Any RISC-V object built with
-gfails withFailed to parse DWARF: Failed to load DWARF info: Unsupported relocation type. With linker relaxation, GNU as emitsADD/SUB,SET6/SUB6,SET8/SUB8andSET_ULEB128/SUB_ULEB128pairs at the same offset, whichobjectcan't put in a relocation map. On RISC-V,RelocationMap::addnow applies them in file order to a copy of the section data, following the psABI and binutils (perform_relocation; theSUB_ULEB128addend is ignored, as in binutils PR31179). It errors if a ULEB128 result doesn't fit its encoding or aSET_ULEB128isn't followed by itsSUB_ULEB128. Both labels of a pair are in one section, so the difference doesn't depend on placement.Relation to #8728
#8728 (the
object0.41 update) is independent, and the two touch different files.object0.41 changeself::SectionFlagsinto a newtype, though, so whichever merges second needsobject_section_addressesto useSectionFlags::Elf { sh_flags, .. }andsh_flags.contains(object::elf::SHF_ALLOC). With both, Xtensa objects built with-gimport with every function and data variable on its symbol. Without this PR, #8728 imports them at the wrong addresses described above.Testing
Built
dwarf_importfrom this branch and fromdev, and loaded each headless into Binary Ninja 6.1.11155-dev (same core ABI asdev) in place of the bundled importer (corePlugins.dwarfImport: false). Checked that each imported function and data variable is at the address of the ELF symbol with its name, and compared the output (DWARF functions, data variables and types, and the view's function types and variables).add,twice,bump,counterandzeroedare each at their ELF symbol inobjs.o,objs_fs.oandobjs_rv32.o.-O2 -g, with and without-ffunction-sections -fdata-sections): ondev, with-ffunction-sectionsall 12 functions are at 0x400000. Without it,fplands on top of the functionadd3andsinkis missing. With this branch, all 12 functions and both data variables are on their symbols. The same holds withloader.imageBase0x800000 and 0x1000.-O2 -ggdband-Og -ggdb) fails ondevand imports with this branch: 20 functions, 2 data variables and 4 types, all on their symbols, also at both image bases above. The DWARF function signatures, data variable types and named types are identical to the import of the same object linked withld.add3at 0x10020). The function signatures, types, and the view's function types and local variables are identical to the import of the same object linked withld.loader.imageBase0x800000 and 0x1000) and non-PIE binaries, and linked ESP32 and ESP32-C3 ELFs, give identical output todev.dwarfreader/dwarf_importas ondev.cargo fmt --checkreports two diffs indwarfdebuginfo.rs, which comes fromdevand isn't changed here.