LLVM 24.0.0git
RelocationFixups.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/STLExtras.h"
11
12using namespace llvm;
13using namespace dwarf_linker;
14
16 uint64_t InputStart, uint64_t InputEnd,
17 uint64_t OutputOffset,
18 const AttributeSource &Source) {
19 if (!Source.Expression) {
20 DIEFixups.push_back({RelocSectionKind::DebugInfo, InputStart, InputEnd,
21 int64_t(OutputOffset) - int64_t(InputStart)});
22 } else {
23 uint64_t ExpressionStart = InputEnd - Source.Expression->InputSize;
25 Source.Expression->Operations) {
26 uint64_t Start = Op.InputStart;
27 if (Op.InputSection == RelocSectionKind::DebugInfo)
28 Start += ExpressionStart;
29 DIEFixups.push_back(
30 {Op.InputSection, Start, Start + Op.Size,
31 int64_t(OutputOffset + Op.OutputOffset) - int64_t(Start)});
32 }
33 }
34
35 // A compile unit's low_pc is computed rather than taken from its entry. A
36 // high_pc can end where the next symbol starts, in the entry of that symbol's
37 // low_pc, which the relocation of the high_pc would misdescribe.
38 bool IsLowPC = Attr == dwarf::DW_AT_low_pc;
39 if (Source.Entry && Attr != dwarf::DW_AT_high_pc &&
40 !(IsLowPC && Tag == dwarf::DW_TAG_compile_unit))
41 DebugAddrFixups.push_back({*Source.Entry, IsLowPC});
42}
43
44void RelocationFixups::finishDIE(uint64_t AbbrevNumberSize) {
45 for (DebugInfoFixup &Fixup : DIEFixups) {
46 Fixup.LinkedOffset += AbbrevNumberSize;
47 DebugInfoFixups.push_back(Fixup);
48 }
49 DIEFixups.clear();
50}
51
52void RelocationFixups::save(AddressesMap &Addresses, uint64_t UnitOffset,
53 uint8_t AddrSize) const {
54 for (const DebugInfoFixup &Fixup : DebugInfoFixups)
56 Fixup.InputSection, RelocSectionKind::DebugInfo, UnitOffset,
57 Fixup.LinkedOffset, Fixup.InputStart, Fixup.InputEnd);
58
59 if (!DebugAddrTableOffset)
60 return;
61
62 // Entries holding the same address are merged, so symbols can share one: a
63 // DW_AT_call_return_pc ending a function holds the low_pc of the next. Only
64 // one relocation can describe an entry. The low_pc's names the right symbol.
65 SmallVector<DebugAddrFixup> Sorted(DebugAddrFixups);
67 Sorted, [](const DebugAddrFixup &LHS, const DebugAddrFixup &RHS) {
68 return std::make_pair(LHS.Entry.OutputIndex, !LHS.IsLowPC) <
69 std::make_pair(RHS.Entry.OutputIndex, !RHS.IsLowPC);
70 });
71
72 std::optional<uint64_t> SavedIndex;
73 for (const DebugAddrFixup &Fixup : Sorted) {
74 if (SavedIndex == Fixup.Entry.OutputIndex)
75 continue;
76 uint64_t InputOffset = Fixup.Entry.InputOffset;
77 uint64_t OutputOffset =
78 *DebugAddrTableOffset + Fixup.Entry.OutputIndex * AddrSize;
79 if (Addresses.updateAndSaveValidRelocs(
81 UnitOffset, int64_t(OutputOffset) - int64_t(InputOffset),
82 InputOffset, InputOffset + AddrSize))
83 SavedIndex = Fixup.Entry.OutputIndex;
84 }
85}
86
88 DIEFixups.clear();
89 DebugInfoFixups.clear();
90 DebugAddrFixups.clear();
91 DebugAddrTableOffset.reset();
92}
This file contains some templates that are useful if you are working with the STL at all.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
AddressesMap represents information about valid addresses used by debug information.
virtual bool updateAndSaveValidRelocs(RelocSectionKind InSection, RelocSectionKind OutSection, uint64_t OriginalUnitOffset, int64_t LinkedOffset, uint64_t StartOffset, uint64_t EndOffset)=0
Save the valid relocations in [StartOffset, EndOffset) of InSection, moved by LinkedOffset into OutSe...
LLVM_ABI void noteAttribute(dwarf::Tag Tag, dwarf::Attribute Attr, uint64_t InputStart, uint64_t InputEnd, uint64_t OutputOffset, const AttributeSource &Source)
Notes that the attribute Attr of a DIE tagged Tag was cloned from [InputStart, InputEnd) of ....
LLVM_ABI void save(AddressesMap &Addresses, uint64_t UnitOffset, uint8_t AddrSize) const
Saves the relocations, for the unit at UnitOffset in the input.
LLVM_ABI void finishDIE(uint64_t AbbrevNumberSize)
Completes the attributes noted since the previous call, once the DIE has an abbreviation number.
Attribute
Attributes.
Definition Dwarf.h:125
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
Definition STLExtras.h:2132
DWARFExpression::Operation Op
What an attribute being cloned was cloned from.