Skip to content

DWARF import: functions and data variables of relocatable objects are placed at section offsets, not addresses #8729

Open

Description

@jrozner

Version and Platform (required):

  • Binary Ninja Version: 6.1.11155-dev
  • Edition: Ultimate
  • OS: Ubuntu Linux
  • OS Version: 26.04
  • CPU Architecture: x64

Bug Description:
The DWARF importer places functions and data variables from a relocatable object (ET_REL, a .o file) at the wrong addresses whenever they aren't in the object's first section.

Every section of a .o is at address 0, so a DWARF address in it is an offset into the section its relocation's symbol is in. The ELF view lays the sections out one after another from the image base, but the importer only adds the image base, as if everything were in the first section. As a result:

  • With -ffunction-sections, every function is placed at the image base, on top of the first function.
  • Data variables in .data and .bss are both placed at the image base (on top of code), and since both are at offset 0 of their sections, they get the same address and one of them is lost.
  • On architectures whose objects put another section before .text, such as Xtensa (.literal), every function is placed too low.

Steps To Reproduce:

  1. Save this as objs.c:

    int counter = 42;
    int zeroed;
    
    int add(int a, int b) { return a + b; }
    int twice(int x) { return add(x, x); }
    int bump(void) { return ++counter + zeroed; }
  2. Build two objects (gcc 15.2.0):

    gcc -g -O2 -c objs.c -o objs.o
    gcc -g -O2 -ffunction-sections -fdata-sections -c objs.c -o objs_fs.o
  3. Load them with the stock DWARF importer and compare each DWARF function and data variable with the ELF symbol of the same name:

    import binaryninja as bn
    for path in ["objs.o", "objs_fs.o"]:
        with bn.load(path) as bv:
            print(path, bn.core_version(), hex(bv.start))
            for f in bv.debug_info.functions:
                if f.short_name in ("add", "twice", "bump"):
                    sym = bv.get_symbol_at(f.address)
                    print(f"  function {f.short_name:6} dwarf={f.address:#x} symbol_there={sym.name if sym else None}")
            for v in bv.debug_info.data_variables:
                if v.name in ("counter", "zeroed"):
                    print(f"  data     {v.name:7} dwarf={v.address:#x} elf_symbol={[hex(s.address) for s in bv.get_symbols_by_name(v.name)]}")

    Output:

    objs.o 6.1.11155-dev 0x400000
      function bump   dwarf=0x400020 symbol_there=bump
      function twice  dwarf=0x400010 symbol_there=twice
      function add    dwarf=0x400000 symbol_there=add
      data     zeroed  dwarf=0x400000 elf_symbol=['0x400000', '0x400050']
    objs_fs.o 6.1.11155-dev 0x400000
      function bump   dwarf=0x400000 symbol_there=add
      function twice  dwarf=0x400000 symbol_there=add
      function add    dwarf=0x400000 symbol_there=add
      data     zeroed  dwarf=0x400000 elf_symbol=['0x400000', '0x400040']
    

    counter (ELF symbol at 0x400040 in objs.o, 0x400050 in objs_fs.o) is missing from both. zeroed is placed on top of add, and the importer adds a second zeroed symbol there. In objs_fs.o, all three functions are at 0x400000.

Expected Behavior:
Each DWARF function and data variable is at the address where the view placed it, the address of its ELF symbol: in objs_fs.o, twice at 0x400010, bump at 0x400020, zeroed at 0x400040 and counter at 0x400050.

Binary:
Built from the source above.

Additional Information:
Linked binaries are unaffected. This is separate from #8653 (data variables not rebased in PIE and rebased views, fixed in 6.0.11067-dev): the rebase is correct here, but it's applied to section offsets instead of addresses.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions