Page MenuHomePhabricator

Use the section sizes to determine symbols sizes in the Symtab instead of just using the following symbol's address
ClosedPublic

Authored by jasonmolenda on Apr 11 2016, 9:37 PM.

Details

Summary

The Symtab has an array of symbol file addresses and sizes for address-to-symbol lookups, created in Symtab::InitAddressIndexes. The Symtab has a vector of symbol (m_symbols) and a parallel vector of Range entries (m_file_addr_to_index - the index being an index into m_symbols). Once the m_symbols have been filled in, it makes a pass over them and creates the m_file_addr_to_index entries. Then it does additional passes to fill in the sizes of the Range entries based on the other symbol start addresses.

I'm working on a problem in an environment where the sections of a file are noncontiguous in memory - so we can have a gap of hundreds of megabytes between the sections, and the last symbol in the first section gets a 400MB size. If the unwinder (or one of the fallback unwind methods) happens to come up with an address in that 400MB gap, lldb will try to disassemble that entire section, thinking it is a function.

Before this patch, InitAddressIndexes would create the m_file_addr_to_index array, then set sizes based on the next symbol, then use the last section's end address to set the last symbol(s) in the array.

In this patch, I'm using the smaller of the containing section end OR the next symbol's address to determine the symbol's size. Looking up the section for each symbol would be expensive, so I make a local RangeVector with all of the section address/sizes before I loop over the entries. RangeVector::Sort sorts by start address and then by size so we'll get the smallest containing section for a given lookup.

Diff Detail

Repository
rL LLVM

Event Timeline

jasonmolenda retitled this revision from to Use the section sizes to determine symbols sizes in the Symtab instead of just using the following symbol's address.
jasonmolenda updated this object.
jasonmolenda added a reviewer: clayborg.
jasonmolenda set the repository for this revision to rL LLVM.
jasonmolenda added a subscriber: lldb-commits.
clayborg requested changes to this revision.Apr 12 2016, 9:49 AM
clayborg edited edge metadata.

Only add sections that don't have children in AddSectionsToRangeMap() and this will be good to go.

source/Symbol/Symtab.cpp
952–975

You want to only add sections that don't have children. On MacOSX we have:

__TEXT [0x1000-0x2000)

__text [0x1000-0x1100)
__textcoal_nt [0x1100-0x1200)

If you have all of these in the mix you will have a non deterministic sort since TEXT and text will compare to the same thing. Object files are also tricky because they have only one LC_SEGMENT with a bunch of sections and the ObjectFileMachO will actually make segments from the segname and sectname and they will have overlapping address ranges. Checking out a .o file from a recent build:

% dwarfdump -R main.o
Sections
Section Name     Segment Name     addr             size             offset   align    reloff   nreloc   flags    reserv1  reserv2  reserv3  size     size %
---------------- ---------------- ---------------- ---------------- -------- -------- -------- -------- -------- -------- -------- -------- ======== ======
__text           __TEXT           0000000000000000 0000000000000165 00000590 00000004 00000b60 00000015 80000400 00000000 00000000 00000000  357       nan%
__debug_info     __DWARF          0000000000000165 00000000000000da 000006f5 00000000 00000c08 00000004 02000000 00000000 00000000 00000000  218       nan%
__debug_abbrev   __DWARF          000000000000023f 000000000000007d 000007cf 00000000 00000000 00000000 02000000 00000000 00000000 00000000  125       nan%
__debug_line     __DWARF          00000000000002bc 0000000000000075 0000084c 00000000 00000c28 00000001 02000000 00000000 00000000 00000000  117       nan%
__debug_str      __DWARF          0000000000000331 000000000000008f 000008c1 00000000 00000000 00000000 02000000 00000000 00000000 00000000  143       nan%
__debug_loc      __DWARF          00000000000003c0 0000000000000000 00000950 00000000 00000000 00000000 02000000 00000000 00000000 00000000    0       nan%
__debug_ranges   __DWARF          00000000000003c0 0000000000000000 00000950 00000000 00000000 00000000 02000000 00000000 00000000 00000000    0       nan%
__cstring        __TEXT           00000000000003c0 000000000000009c 00000950 00000000 00000000 00000000 00000002 00000000 00000000 00000000  156       nan%
__apple_names    __DWARF          000000000000045c 000000000000003c 000009ec 00000000 00000000 00000000 02000000 00000000 00000000 00000000   60       nan%
__apple_objc     __DWARF          0000000000000498 0000000000000024 00000a28 00000000 00000000 00000000 02000000 00000000 00000000 00000000   36       nan%
__apple_namespac __DWARF          00000000000004bc 0000000000000024 00000a4c 00000000 00000000 00000000 02000000 00000000 00000000 00000000   36       nan%
__apple_types    __DWARF          00000000000004e0 0000000000000066 00000a70 00000000 00000000 00000000 02000000 00000000 00000000 00000000  102       nan%
__apple_exttypes __DWARF          0000000000000546 0000000000000024 00000ad6 00000000 00000000 00000000 02000000 00000000 00000000 00000000   36       nan%
__compact_unwind __LD             0000000000000570 0000000000000020 00000b00 00000003 00000c30 00000001 02000000 00000000 00000000 00000000   32       nan%
__eh_frame       __TEXT           0000000000000590 0000000000000040 00000b20 00000003 00000000 00000000 6800000b 00000000 00000000 00000000   64       nan%

Now if we look at what ObjectFileMachO does:

% xcrun lldb main.o
(lldb) target create "main.o"
Current executable set to 'main.o' (x86_64).
(lldb) image dump sections
Dumping sections for 1 modules.
Sections for '/Volumes/work/gclayton/Documents/src/args/main.o' (x86_64):
  SectID     Type             File Address                             File Off.  File Size  Flags      Section Name
  ---------- ---------------- ---------------------------------------  ---------- ---------- ---------- ----------------------------
  0x00000100 container        [0x0000000000000000-0x00000000000005d0)  0x00000590 0x000005d0 0x00000000 main.o.__TEXT
  0x00000001 code             [0x0000000000000000-0x0000000000000165)  0x00000590 0x00000165 0x80000400 main.o.__TEXT.__text
  0x00000008 data-cstr        [0x00000000000003c0-0x000000000000045c)  0x00000950 0x0000009c 0x00000002 main.o.__TEXT.__cstring
  0x0000000f eh-frame         [0x0000000000000590-0x00000000000005d0)  0x00000b20 0x00000040 0x6800000b main.o.__TEXT.__eh_frame
  0x00000200 container        [0x0000000000000165-0x000000000000056a)  0x000006f5 0x00000405 0x00000000 main.o.__DWARF
  0x00000002 dwarf-info       [0x0000000000000165-0x000000000000023f)  0x000006f5 0x000000da 0x02000000 main.o.__DWARF.__debug_info
  0x00000003 dwarf-abbrev     [0x000000000000023f-0x00000000000002bc)  0x000007cf 0x0000007d 0x02000000 main.o.__DWARF.__debug_abbrev
  0x00000004 dwarf-line       [0x00000000000002bc-0x0000000000000331)  0x0000084c 0x00000075 0x02000000 main.o.__DWARF.__debug_line
  0x00000005 dwarf-str        [0x0000000000000331-0x00000000000003c0)  0x000008c1 0x0000008f 0x02000000 main.o.__DWARF.__debug_str
  0x00000006 dwarf-loc                                                 0x00000950 0x00000000 0x02000000 main.o.__DWARF.__debug_loc
  0x00000007 dwarf-ranges                                              0x00000950 0x00000000 0x02000000 main.o.__DWARF.__debug_ranges
  0x00000009 apple-names      [0x000000000000045c-0x0000000000000498)  0x000009ec 0x0000003c 0x02000000 main.o.__DWARF.__apple_names
  0x0000000a apple-objc       [0x0000000000000498-0x00000000000004bc)  0x00000a28 0x00000024 0x02000000 main.o.__DWARF.__apple_objc
  0x0000000b apple-namespaces [0x00000000000004bc-0x00000000000004e0)  0x00000a4c 0x00000024 0x02000000 main.o.__DWARF.__apple_namespac
  0x0000000c apple-types      [0x00000000000004e0-0x0000000000000546)  0x00000a70 0x00000066 0x02000000 main.o.__DWARF.__apple_types
  0x0000000d apple-external-types [0x0000000000000546-0x000000000000056a)  0x00000ad6 0x00000024 0x02000000 main.o.__DWARF.__apple_exttypes
  0x00000300 container        [0x0000000000000570-0x0000000000000590)  0x00000b00 0x00000020 0x00000000 main.o.__LD
  0x0000000e regular          [0x0000000000000570-0x0000000000000590)  0x00000b00 0x00000020 0x02000000 main.o.__LD.__compact_unwind

Note that TEXT and DWARF overlap. That will probably cause problems. So the fix is to only add sections that don't have children.

This revision now requires changes to proceed.Apr 12 2016, 9:49 AM
jasonmolenda edited edge metadata.

Updated the patch to address Greg's feedback.

jasonmolenda accepted this revision.Apr 12 2016, 9:41 PM
jasonmolenda added a reviewer: jasonmolenda.
clayborg accepted this revision.Apr 13 2016, 10:00 AM
clayborg edited edge metadata.

Looks good.

This revision is now accepted and ready to land.Apr 13 2016, 10:00 AM
jasonmolenda closed this revision.Apr 13 2016, 4:34 PM

Committed in r266165.