[llvm] [BOLT] Update DW_AT_GNU_locviews when rewriting .debug_loclists (PR #206765)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 3 07:42:47 PDT 2026
not4juu wrote:
# Its short summary of proposed solution in the pull request draft attached in this topic:
## Summary
When `llvm-bolt --update-debug-sections` processes a binary built by GCC with `-gdwarf-5` and optimizations, it rewrites `.debug_loclists` (updating `DW_AT_location`, `loclistx`, `loclists_base`, and `.debug_addr`) but leaves the GNU Location Views attribute (`DW_AT_GNU_locviews`) untouched. The view-pair bytes that GCC emits immediately before each location list are dropped from the rebuilt section, while the stale `DW_AT_GNU_locviews` offsets remain in `.debug_info`.
Result: the location-view offsets point into the wrong place in the rewritten `.debug_loclists`. `readelf --debug-dump=loc` reports invalid view offsets and broken adjacency, and debuggers that rely on location views (LVU) can pick the wrong variable value at a given PC after `step`.
This patch teaches BOLT to parse the input location-view pairs, attach each pair to the location list entry it describes so it survives BOLT's range translation/merge, re-emit one view pair per emitted list entry in front of each rewritten DWARF 5 location list, and patch `DW_AT_GNU_locviews` to the new absolute offset in the merged section.
## Background: DW_AT_GNU_locviews
`DW_AT_GNU_locviews` is a GCC extension (not part of the DWARF standard. For `-gdwarf-5`, GCC encodes, per location list:
| Element | Meaning |
|---------|---------|
| `DW_AT_GNU_locviews` (`DW_FORM_sec_offset`) | Absolute offset in `.debug_loclists` to a list of view pairs |
| `DW_AT_location` (`DW_FORM_loclistx`) | Index into the offset table → start of the loclist (immediately after the view pairs) |
| View list | Sequence of ULEB128 `(view_begin, view_end)` pairs, one per loclist entry |
The view pairs sit in the section body directly before the loclist they belong to. Any rewrite of `.debug_loclists` must keep that adjacency and update both offsets consistently.
## Proposed Fix
The implementation is generic with respect to byte offsets — it does not assume any fixed gap size between the view list and the loclist. View pairs are whatever ULEB128 bytes sit between `DW_AT_GNU_locviews` and the start of the loclist in the input. The key idea is that a view pair belongs to a *specific* location list entry, so it is stored on the entry and follows it through translation, merging and sorting; exactly one pair is then emitted per surviving entry.
1. **Parse input view pairs** (`DWARFRewriter.cpp`)
- `resolveInputLoclistSectionOffset` resolves the input loclist start for `loclistx`, `sec_offset`, and constant forms.
- `parseLocViewList` reads all ULEB128 `(begin,end)` pairs in `[ViewOff, ListOff)` from `.debug_loclists` (any count, any size).
- `parseInputLocViews` ties them together (DWARF 5 only).
2. **Bind each view to its entry** (`DebugData.h`, `DWARFRewriter.cpp`)
- `DebugLocationEntry` gains a `DebugLocationViewPair View` member, so a view travels together with the range it describes.
- After parsing, each `InputViews[I]` is copied onto `InputLL[I].View`.
- `translateInputToOutputLocationList` propagates `View` when it emits translated ranges and, when it merges two contiguous ranges with the same expression, keeps the merged entry's `ViewBegin` and advances its `ViewEnd` to the absorbed range — so the pair still brackets the merged range. This is what keeps the view list and the loclist the same length.
3. **Re-emit view pairs 1:1** (`DebugData.cpp`, `writeDWARF5LocList`)
- Before writing the rewritten list, emit exactly one `(ViewBegin, ViewEnd)` pair per `DebugLocationEntry` in the (possibly merged) output list, and record a `LocViewPatch{DIE*, LocalViewOffset}`. No separate view array is passed around any more.
- If `DW_AT_GNU_locviews` is present but the list is empty, the attribute is deleted to avoid a dangling offset.
4. **Patch the attribute** (`DebugData.cpp`, `applyViewOffsets`)
- `AbsOffset = GlobalCUOffset + HeaderSize + NumberOfEntries*sizeof(uint32_t) + LocalViewOffset`
- The `sizeof(uint32_t)` (i.e. `4`) is the size of a single slot in the DWARF 5 loclist offset table (`offset_entry_count` × one DWARF32 offset each), so `NumberOfEntries * sizeof(uint32_t)` is the whole offset table that sits between the header and the body. `LocalViewOffset` is measured from the start of the body.
- This mirrors the math used for the loclist offset table (see `SizeOfArraySection` in `finalizeDWARF5`), so it works for any CU position in the merged section (including the first CU at offset 0).
5. **Plumbing**
- `DebugLocWriter::addList` / `DebugLoclistWriter::addList` / `writeDWARF5LocList` gain a `bool HasGNULocViews` flag; the view data itself rides along inside `DebugLocationEntry`, so no `ArrayRef` parameter is needed.
- `mergePerBucketLocs` calls `applyViewOffsets(PartDIEBlder, GlobalCUOffset)` for each CU after the global loclists base is known.
## Files changed
- `bolt/include/bolt/Core/DebugData.h`
- New `struct DebugLocationViewPair`.
- New `DebugLocationEntry::View` member (view pair bound to the entry).
- Extended `addList` / `writeDWARF5LocList` signatures with `HasGNULocViews`.
- New `applyViewOffsets`, `writeDWARF5LocList` member, `LocViewPatch` + `LocViewPatches`.
- `bolt/lib/Core/DebugData.cpp`
- `writeDWARF5LocList` becomes a member that emits one view pair per output entry from `DebugLocationEntry::View`; new `applyViewOffsets`.
- `bolt/lib/Rewrite/DWARFRewriter.cpp`
- `getLoclistsSectionData`, `resolveInputLoclistSectionOffset`, `parseLocViewList`, `parseInputLocViews`; bind parsed views onto the input entries; carry/merge `View` in `translateInputToOutputLocationList`; call `applyViewOffsets` in `mergePerBucketLocs`.
https://github.com/llvm/llvm-project/pull/206765
More information about the llvm-commits
mailing list