LLVM 24.0.0git
AddressesMap.h
Go to the documentation of this file.
1//===- AddressesMap.h -------------------------------------------*- C++ -*-===//
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
9#ifndef LLVM_DWARFLINKER_ADDRESSESMAP_H
10#define LLVM_DWARFLINKER_ADDRESSESMAP_H
11
17#include <cstdint>
18
19namespace llvm {
20namespace dwarf_linker {
21
22/// Mapped value in the address map is the offset to apply to the
23/// linked address.
25
26/// The debug sections holding relocations which the linker can save.
28
29/// AddressesMap represents information about valid addresses used
30/// by debug information. Valid addresses are those which points to
31/// live code sections. i.e. relocations for these addresses point
32/// into sections which would be/are placed into resulting binary.
34public:
35 virtual ~AddressesMap() = default;
36
37 /// Checks that there are valid relocations in the .debug_info
38 /// section.
39 virtual bool hasValidRelocs() = 0;
40
41 /// Checks that the specified DWARF expression operand \p Op references live
42 /// code section and returns the relocation adjustment value (to get the
43 /// linked address this value might be added to the source expression operand
44 /// address). Print debug output if \p Verbose is true.
45 /// \returns relocation adjustment value or std::nullopt if there is no
46 /// corresponding live address.
47 virtual std::optional<int64_t> getExprOpAddressRelocAdjustment(
48 DWARFUnit &U, const DWARFExpression::Operation &Op, uint64_t StartOffset,
49 uint64_t EndOffset, bool Verbose) = 0;
50
51 /// Returns the relocation adjustment value for the .debug_addr entry that the
52 /// DW_OP_addrx or DW_OP_constx operation \p Op refers to, or std::nullopt if
53 /// there is no corresponding live address. The entry may name a different
54 /// symbol than whatever owns the expression, such as a variable in a location
55 /// list covering a function's code, and that symbol may have moved by a
56 /// different amount.
57 std::optional<int64_t> getAddrIndexRelocAdjustment(
59 std::optional<uint64_t> AddrOffset =
60 U.getIndexedAddressOffset(Op.getRawOperand(0));
61 if (!AddrOffset)
62 return std::nullopt;
64 U, Op, *AddrOffset, *AddrOffset + U.getAddressByteSize(), Verbose);
65 }
66
67 /// Checks that the specified subprogram \p DIE references the live code
68 /// section and returns the relocation adjustment value (to get the linked
69 /// address this value might be added to the source subprogram address).
70 /// Allowed kinds of input DIE: DW_TAG_subprogram, DW_TAG_label.
71 /// Print debug output if \p Verbose is true.
72 /// \returns relocation adjustment value or std::nullopt if there is no
73 /// corresponding live address.
74 virtual std::optional<int64_t>
76
77 // Returns the library install name associated to the AddessesMap.
78 virtual std::optional<StringRef> getLibraryInstallName() = 0;
79
80 /// Apply the valid relocations to the buffer \p Data, taking into
81 /// account that Data is at \p BaseOffset in the .debug_info section.
82 ///
83 /// \returns true whether any reloc has been applied.
85 bool IsLittleEndian) = 0;
86
87 /// Check if the linker needs to gather and save relocation info.
88 virtual bool needToSaveValidRelocs() = 0;
89
90 /// Save the valid relocations in [\p StartOffset, \p EndOffset) of
91 /// \p InSection, moved by \p LinkedOffset into \p OutSection, for the unit at
92 /// \p OriginalUnitOffset in the input.
93 ///
94 /// \returns true if any relocation was saved.
96 RelocSectionKind OutSection,
97 uint64_t OriginalUnitOffset,
98 int64_t LinkedOffset,
99 uint64_t StartOffset,
100 uint64_t EndOffset) = 0;
101
102 /// Erases all data.
103 virtual void clear() = 0;
104
105 /// The extent the linker gave a symbol, in source address space.
112
113 /// Returns the symbol range [LowPC, HighPC) containing \p Addr, if known.
114 virtual std::optional<SymbolRange> getSymbolRangeForAddress(uint64_t Addr) {
115 return std::nullopt;
116 }
117
118 /// Returns the linked address of the first symbol placed at or after
119 /// \p LinkedAddr, if one is known.
120 virtual std::optional<uint64_t>
122 return std::nullopt;
123 }
124
125 /// Constrains the end of the code range starting at \p LowPC, whose addresses
126 /// shift by \p Adjustment in the output. \p HighPC is an address, not a
127 /// length.
128 ///
129 /// The linker places symbols independently, so a range overrunning the symbol
130 /// it starts in can cover a different one once linked. Only that overlap is
131 /// repaired.
133 int64_t Adjustment) {
134 std::optional<SymbolRange> Symbol = getSymbolRangeForAddress(LowPC);
135 if (!Symbol)
136 return HighPC;
137 assert(Symbol->LowPC <= LowPC && LowPC < Symbol->HighPC &&
138 "Symbol range must contain the address it was looked up for");
139 uint64_t LinkedSymbolHighPC = Symbol->HighPC + Adjustment;
140 assert(LinkedSymbolHighPC > Symbol->LowPC + Adjustment &&
141 "Adjusting a symbol range must preserve its order");
142 std::optional<uint64_t> NextStart =
143 getNextLinkedSymbolStart(LinkedSymbolHighPC);
144 if (!NextStart)
145 return HighPC;
146 assert(*NextStart >= LinkedSymbolHighPC &&
147 "A range must never be cut short of its own symbol");
148 return std::min(HighPC, *NextStart - Adjustment);
149 }
150
151 /// This function checks whether variable has DWARF expression containing
152 /// operation referencing live address(f.e. DW_OP_addr, DW_OP_addrx...).
153 /// \returns first is true if the expression has an operation referencing an
154 /// address.
155 /// second is the relocation adjustment value if the live address is
156 /// referenced.
157 std::pair<bool, std::optional<int64_t>>
159 assert((DIE.getTag() == dwarf::DW_TAG_variable ||
160 DIE.getTag() == dwarf::DW_TAG_constant) &&
161 "Wrong type of input die");
162
163 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
164
165 // Check if DIE has DW_AT_location attribute.
166 DWARFUnit *U = DIE.getDwarfUnit();
167 std::optional<uint32_t> LocationIdx =
168 Abbrev->findAttributeIndex(dwarf::DW_AT_location);
169 if (!LocationIdx)
170 return std::make_pair(false, std::nullopt);
171
172 // Get offset to the DW_AT_location attribute.
173 uint64_t AttrOffset =
174 Abbrev->getAttributeOffsetFromIndex(*LocationIdx, DIE.getOffset(), *U);
175
176 // Get value of the DW_AT_location attribute.
177 std::optional<DWARFFormValue> LocationValue =
178 Abbrev->getAttributeValueFromOffset(*LocationIdx, AttrOffset, *U);
179 if (!LocationValue)
180 return std::make_pair(false, std::nullopt);
181
182 // Check that DW_AT_location attribute is of 'exprloc' class.
183 // Handling value of location expressions for attributes of 'loclist'
184 // class is not implemented yet.
185 std::optional<ArrayRef<uint8_t>> Expr = LocationValue->getAsBlock();
186 if (!Expr)
187 return std::make_pair(false, std::nullopt);
188
189 // Parse 'exprloc' expression.
190 DataExtractor Data(*Expr, U->getContext().isLittleEndian());
191 DWARFExpression Expression(Data, U->getAddressByteSize(),
192 U->getFormParams().Format);
193
194 bool HasLocationAddress = false;
195 uint64_t CurExprOffset = 0;
196 for (DWARFExpression::iterator It = Expression.begin();
197 It != Expression.end(); ++It) {
198 DWARFExpression::iterator NextIt = It;
199 ++NextIt;
200
201 const DWARFExpression::Operation &Op = *It;
202 switch (Op.getCode()) {
203 case dwarf::DW_OP_const2u:
204 case dwarf::DW_OP_const4u:
205 case dwarf::DW_OP_const8u:
206 case dwarf::DW_OP_const2s:
207 case dwarf::DW_OP_const4s:
208 case dwarf::DW_OP_const8s:
209 if (NextIt == Expression.end() ||
210 !dwarf::isTlsAddressOp(NextIt->getCode()))
211 break;
212 [[fallthrough]];
213 case dwarf::DW_OP_addr: {
214 HasLocationAddress = true;
215 // Check relocation for the address.
216 if (std::optional<int64_t> RelocAdjustment =
218 *U, Op, AttrOffset + CurExprOffset,
219 AttrOffset + Op.getEndOffset(), Verbose))
220 return std::make_pair(HasLocationAddress, *RelocAdjustment);
221 } break;
222 case dwarf::DW_OP_constx:
223 case dwarf::DW_OP_addrx: {
224 HasLocationAddress = true;
225 if (std::optional<uint64_t> AddressOffset =
226 DIE.getDwarfUnit()->getIndexedAddressOffset(
227 Op.getRawOperand(0))) {
228 // Check relocation for the address.
229 if (std::optional<int64_t> RelocAdjustment =
231 *U, Op, *AddressOffset,
232 *AddressOffset + DIE.getDwarfUnit()->getAddressByteSize(),
233 Verbose))
234 return std::make_pair(HasLocationAddress, *RelocAdjustment);
235 }
236 } break;
237 default: {
238 // Nothing to do.
239 } break;
240 }
241 CurExprOffset = Op.getEndOffset();
242 }
243
244 return std::make_pair(HasLocationAddress, std::nullopt);
245 }
246};
247
248} // namespace dwarf_linker
249} // end namespace llvm
250
251#endif // LLVM_DWARFLINKER_ADDRESSESMAP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
AddressRangesMap class maps values to the address ranges.
A structured debug information entry.
Definition DIE.h:835
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition DIE.h:882
dwarf::Tag getTag() const
Definition DIE.h:880
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition DWARFDie.h:43
This class represents an Operation in the Expression.
An iterator to go through the expression operations.
Class representing an expression and its matching format.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
AddressesMap represents information about valid addresses used by debug information.
virtual bool hasValidRelocs()=0
Checks that there are valid relocations in the .debug_info section.
virtual void clear()=0
Erases all data.
virtual std::optional< uint64_t > getNextLinkedSymbolStart(uint64_t LinkedAddr)
Returns the linked address of the first symbol placed at or after LinkedAddr, if one is known.
virtual bool needToSaveValidRelocs()=0
Check if the linker needs to gather and save relocation info.
virtual std::optional< StringRef > getLibraryInstallName()=0
std::optional< int64_t > getAddrIndexRelocAdjustment(DWARFUnit &U, const DWARFExpression::Operation &Op, bool Verbose)
Returns the relocation adjustment value for the .debug_addr entry that the DW_OP_addrx or DW_OP_const...
uint64_t constrainCodeRangeHighPC(uint64_t LowPC, uint64_t HighPC, int64_t Adjustment)
Constrains the end of the code range starting at LowPC, whose addresses shift by Adjustment in the ou...
virtual std::optional< SymbolRange > getSymbolRangeForAddress(uint64_t Addr)
Returns the symbol range [LowPC, HighPC) containing Addr, if known.
virtual bool applyValidRelocs(MutableArrayRef< char > Data, uint64_t BaseOffset, bool IsLittleEndian)=0
Apply the valid relocations to the buffer Data, taking into account that Data is at BaseOffset in the...
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...
virtual std::optional< int64_t > getExprOpAddressRelocAdjustment(DWARFUnit &U, const DWARFExpression::Operation &Op, uint64_t StartOffset, uint64_t EndOffset, bool Verbose)=0
Checks that the specified DWARF expression operand Op references live code section and returns the re...
std::pair< bool, std::optional< int64_t > > getVariableRelocAdjustment(const DWARFDie &DIE, bool Verbose)
This function checks whether variable has DWARF expression containing operation referencing live addr...
virtual std::optional< int64_t > getSubprogramRelocAdjustment(const DWARFDie &DIE, bool Verbose)=0
Checks that the specified subprogram DIE references the live code section and returns the relocation ...
AddressRangesMap RangesTy
Mapped value in the address map is the offset to apply to the linked address.
RelocSectionKind
The debug sections holding relocations which the linker can save.
bool isTlsAddressOp(uint8_t O)
Definition Dwarf.h:1201
This is an optimization pass for GlobalISel generic memory operations.
DWARFExpression::Operation Op
SymbolRange(uint64_t LowPC, uint64_t HighPC)