1//===- tools/dsymutil/DwarfLinkerForBinary.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_TOOLS_DSYMUTIL_DWARFLINKER_H
10#define LLVM_TOOLS_DSYMUTIL_DWARFLINKER_H
11
12#include "BinaryHolder.h"
13#include "DebugMap.h"
14#include "LinkUtils.h"
15#include "MachOUtils.h"
16#include "RelocationMap.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/DWARFLinker/DWARFFile.h"
19#include "llvm/DebugInfo/DWARF/DWARFContext.h"
20#include "llvm/Remarks/RemarkFormat.h"
21#include "llvm/Remarks/RemarkLinker.h"
22#include <mutex>
23#include <optional>
24
25namespace llvm {
26class ThreadPoolInterface;
27using namespace dwarf_linker;
28
29namespace dsymutil {
30
31class PseudoProbeLinker;
32
33/// DwarfLinkerForBinaryRelocationMap contains the logic to handle the
34/// relocations and to store them inside an associated RelocationMap.
35class DwarfLinkerForBinaryRelocationMap {
36public:
37 void init(DWARFContext &Context);
38
39 bool isInitialized() { return !Units.empty(); }
40
41 void addValidRelocs(RelocationMap &RM);
42
43 /// Save \p InRelocs, moved by \p LinkedOffset into \p OutSection, for the
44 /// unit at \p UnitOffset in the input.
45 void updateAndSaveValidRelocs(RelocSectionKind OutSection,
46 const std::vector<ValidReloc> &InRelocs,
47 uint64_t UnitOffset, int64_t LinkedOffset);
48
49 DwarfLinkerForBinaryRelocationMap() = default;
50
51private:
52 struct UnitRelocs {
53 std::vector<ValidReloc> DebugInfo;
54 std::vector<ValidReloc> DebugAddr;
55 };
56
57 /// Map compilation unit offset to the valid relocations to store.
58 DenseMap<uint64_t, UnitRelocs> Units;
59};
60
61struct ObjectWithRelocMap {
62 ObjectWithRelocMap(
63 std::unique_ptr<DWARFFile> Object,
64 std::shared_ptr<DwarfLinkerForBinaryRelocationMap> OutRelocs)
65 : Object(std::move(Object)), OutRelocs(OutRelocs) {}
66 std::unique_ptr<DWARFFile> Object;
67 std::shared_ptr<DwarfLinkerForBinaryRelocationMap> OutRelocs;
68};
69
70/// The core of the Dsymutil Dwarf linking logic.
71///
72/// The link of the dwarf information from the object files will be
73/// driven by DWARFLinker. DwarfLinkerForBinary reads DebugMap objects
74/// and pass information to the DWARFLinker. DWARFLinker
75/// optimizes DWARF taking into account valid relocations.
76/// Finally, optimized DWARF is passed to DwarfLinkerForBinary through
77/// DWARFEmitter interface.
78class DwarfLinkerForBinary {
79public:
80 DwarfLinkerForBinary(raw_fd_ostream &OutFile, BinaryHolder &BinHolder,
81 LinkOptions Options, std::mutex &ErrorHandlerMutex,
82 ThreadPoolInterface *ThreadPool = nullptr)
83 : OutFile(OutFile), BinHolder(BinHolder), Options(std::move(Options)),
84 ErrorHandlerMutex(ErrorHandlerMutex), ThreadPool(ThreadPool) {}
85
86 /// Link the contents of the DebugMap.
87 bool link(const DebugMap &);
88
89 void reportWarning(Twine Warning, Twine Context = {},
90 const DWARFDie *DIE = nullptr) const;
91 void reportError(Twine Error, Twine Context = {},
92 const DWARFDie *DIE = nullptr) const;
93
94 /// Returns true if input verification is enabled and verification errors were
95 /// found.
96 bool InputVerificationFailed() const { return HasVerificationErrors; }
97
98 /// Flags passed to DwarfLinker::lookForDIEsToKeep
99 enum TraversalFlags {
100 TF_Keep = 1 << 0, ///< Mark the traversed DIEs as kept.
101 TF_InFunctionScope = 1 << 1, ///< Current scope is a function scope.
102 TF_DependencyWalk = 1 << 2, ///< Walking the dependencies of a kept DIE.
103 TF_ParentWalk = 1 << 3, ///< Walking up the parents of a kept DIE.
104 TF_ODR = 1 << 4, ///< Use the ODR while keeping dependents.
105 TF_SkipPC = 1 << 5, ///< Skip all location attributes.
106 };
107
108private:
109
110 /// Keeps track of relocations.
111 class AddressManager : public dwarf_linker::AddressesMap {
112
113 const DwarfLinkerForBinary &Linker;
114
115 /// The valid relocations for the current DebugMapObject.
116 /// These vectors are sorted by relocation offset.
117 /// {
118 std::vector<ValidReloc> ValidDebugInfoRelocs;
119 std::vector<ValidReloc> ValidDebugAddrRelocs;
120 /// }
121
122 StringRef SrcFileName;
123
124 uint8_t DebugMapObjectType;
125
126 std::shared_ptr<DwarfLinkerForBinaryRelocationMap> DwarfLinkerRelocMap;
127
128 std::optional<std::string> LibInstallName;
129
130 /// Address ranges for symbols with sizes (used for assembly file support).
131 RangesTy AddressRanges;
132
133 /// Sorted linked start addresses of the symbols with a known size.
134 std::vector<uint64_t> LinkedSymbolStarts;
135
136 /// Returns list of valid relocations from \p Relocs,
137 /// between \p StartOffset and \p NextOffset.
138 ///
139 /// \returns true if any relocation is found.
140 std::vector<ValidReloc>
141 getRelocations(const std::vector<ValidReloc> &Relocs, uint64_t StartPos,
142 uint64_t EndPos);
143
144 /// Resolve specified relocation \p Reloc.
145 ///
146 /// \returns resolved value.
147 uint64_t relocate(const ValidReloc &Reloc) const;
148
149 /// \returns value for the specified \p Reloc.
150 int64_t getRelocValue(const ValidReloc &Reloc);
151
152 /// Print contents of debug map entry for the specified \p Reloc.
153 void printReloc(const ValidReloc &Reloc);
154
155 public:
156 AddressManager(DwarfLinkerForBinary &Linker, const object::ObjectFile &Obj,
157 const DebugMapObject &DMO,
158 std::shared_ptr<DwarfLinkerForBinaryRelocationMap> DLBRM)
159 : Linker(Linker), SrcFileName(DMO.getObjectFilename()),
160 DebugMapObjectType(MachO::N_OSO), DwarfLinkerRelocMap(DLBRM) {
161 if (DMO.getRelocationMap().has_value()) {
162 DebugMapObjectType = MachO::N_LIB;
163 LibInstallName.emplace(args: DMO.getInstallName().value());
164 const RelocationMap &RM = DMO.getRelocationMap().value();
165 auto AddRelocs =
166 [&](iterator_range<RelocationMap::const_iterator> Relocs,
167 std::vector<ValidReloc> &ValidRelocs) {
168 for (const auto &Reloc : Relocs) {
169 const auto *DebugMapEntry = DMO.lookupSymbol(SymbolName: Reloc.SymbolName);
170 if (!DebugMapEntry)
171 continue;
172 std::optional<uint64_t> ObjAddress;
173 ObjAddress.emplace(
174 args: DebugMapEntry->getValue().ObjectAddress.value());
175 ValidRelocs.emplace_back(
176 args: Reloc.Offset, args: Reloc.Size, args: Reloc.Addend, args: Reloc.SymbolName,
177 args: SymbolMapping(ObjAddress,
178 DebugMapEntry->getValue().BinaryAddress,
179 DebugMapEntry->getValue().Size));
180 }
181 // The lookups walk the relocations by offset.
182 llvm::sort(C&: ValidRelocs);
183 };
184 AddRelocs(RM.debugInfoRelocations(), ValidDebugInfoRelocs);
185 AddRelocs(RM.debugAddrRelocations(), ValidDebugAddrRelocs);
186 } else {
187 findValidRelocsInDebugSections(Obj, DMO);
188 }
189 // Only a sized symbol has a known extent. The ranges stand in for the
190 // high_pc that assembly files lack.
191 for (const auto &Entry : DMO.symbols()) {
192 const auto &Mapping = Entry.getValue();
193 if (!Mapping.Size)
194 continue;
195 LinkedSymbolStarts.push_back(x: Mapping.BinaryAddress);
196 if (Mapping.ObjectAddress)
197 AddressRanges.insert(
198 Range: {*Mapping.ObjectAddress, *Mapping.ObjectAddress + Mapping.Size},
199 Value: int64_t(Mapping.BinaryAddress) - *Mapping.ObjectAddress);
200 }
201 llvm::sort(C&: LinkedSymbolStarts);
202 LinkedSymbolStarts.erase(first: llvm::unique(R&: LinkedSymbolStarts),
203 last: LinkedSymbolStarts.end());
204 }
205 ~AddressManager() override { clear(); }
206
207 bool hasValidRelocs() override {
208 return !ValidDebugInfoRelocs.empty() || !ValidDebugAddrRelocs.empty();
209 }
210
211 /// \defgroup FindValidRelocations Translate debug map into a list
212 /// of relevant relocations
213 ///
214 /// @{
215 bool findValidRelocsInDebugSections(const object::ObjectFile &Obj,
216 const DebugMapObject &DMO);
217
218 bool findValidRelocs(const object::SectionRef &Section,
219 const object::ObjectFile &Obj,
220 const DebugMapObject &DMO,
221 std::vector<ValidReloc> &ValidRelocs);
222
223 void findValidRelocsMachO(const object::SectionRef &Section,
224 const object::MachOObjectFile &Obj,
225 const DebugMapObject &DMO,
226 std::vector<ValidReloc> &ValidRelocs);
227 /// @}
228
229 /// Checks that there is a relocation in the \p Relocs array against a
230 /// debug map entry between \p StartOffset and \p NextOffset.
231 /// Print debug output if \p Verbose is set.
232 ///
233 /// \returns relocation value if relocation exist, otherwise std::nullopt.
234 std::optional<int64_t>
235 hasValidRelocationAt(const std::vector<ValidReloc> &Relocs,
236 uint64_t StartOffset, uint64_t EndOffset,
237 bool Verbose);
238
239 std::optional<int64_t> getExprOpAddressRelocAdjustment(
240 DWARFUnit &U, const DWARFExpression::Operation &Op,
241 uint64_t StartOffset, uint64_t EndOffset, bool Verbose) override;
242
243 std::optional<int64_t> getSubprogramRelocAdjustment(const DWARFDie &DIE,
244 bool Verbose) override;
245
246 std::optional<StringRef> getLibraryInstallName() override;
247
248 bool applyValidRelocs(MutableArrayRef<char> Data, uint64_t BaseOffset,
249 bool IsLittleEndian) override;
250
251 bool needToSaveValidRelocs() override { return true; }
252
253 bool updateAndSaveValidRelocs(RelocSectionKind InSection,
254 RelocSectionKind OutSection,
255 uint64_t OriginalUnitOffset,
256 int64_t LinkedOffset, uint64_t StartOffset,
257 uint64_t EndOffset) override;
258
259 void clear() override {
260 ValidDebugInfoRelocs.clear();
261 ValidDebugAddrRelocs.clear();
262 AddressRanges.clear();
263 LinkedSymbolStarts.clear();
264 }
265
266 std::optional<SymbolRange>
267 getSymbolRangeForAddress(uint64_t Addr) override {
268 if (auto Range = AddressRanges.getRangeThatContains(Addr))
269 return SymbolRange(Range->Range.start(), Range->Range.end());
270 return std::nullopt;
271 }
272
273 std::optional<uint64_t>
274 getNextLinkedSymbolStart(uint64_t LinkedAddr) override {
275 auto It = llvm::lower_bound(Range&: LinkedSymbolStarts, Value&: LinkedAddr);
276 if (It == LinkedSymbolStarts.end())
277 return std::nullopt;
278 return *It;
279 }
280 };
281
282private:
283 /// \defgroup Helpers Various helper methods.
284 ///
285 /// @{
286 /// Attempt to load a debug object from disk.
287 ErrorOr<const object::ObjectFile &> loadObject(const DebugMapObject &Obj,
288 const Triple &triple);
289 ErrorOr<std::unique_ptr<dwarf_linker::DWARFFile>>
290 loadObject(const DebugMapObject &Obj, const DebugMap &DebugMap,
291 remarks::RemarkLinker &RL, PseudoProbeLinker &PL,
292 std::shared_ptr<DwarfLinkerForBinaryRelocationMap> DLBRM);
293
294 void collectRelocationsToApplyToSwiftReflectionSections(
295 const object::SectionRef &Section, StringRef &Contents,
296 const llvm::object::MachOObjectFile *MO,
297 const std::vector<uint64_t> &SectionToOffsetInDwarf,
298 const llvm::dsymutil::DebugMapObject *Obj,
299 std::vector<MachOUtils::DwarfRelocationApplicationInfo>
300 &RelocationsToApply) const;
301
302 /// Find the compilation-prefix-map.json closest to \p ObjectPath, which
303 /// describes how the compiler remapped paths in the object's debug info.
304 std::shared_ptr<PrefixMapper> findCompilationPrefixMap(StringRef ObjectPath);
305
306 Error copySwiftInterfaces(StringRef Architecture) const;
307
308 Error copyEmbeddedResources() const;
309
310 void copySwiftReflectionMetadata(
311 const llvm::dsymutil::DebugMapObject *Obj,
312 classic::DwarfStreamer *Streamer,
313 std::vector<uint64_t> &SectionToOffsetInDwarf,
314 std::vector<MachOUtils::DwarfRelocationApplicationInfo>
315 &RelocationsToApply);
316
317 template <typename Linker>
318 bool linkImpl(const DebugMap &Map,
319 typename Linker::OutputFileType ObjectType);
320
321 Error emitRelocations(const DebugMap &DM,
322 std::vector<ObjectWithRelocMap> &ObjectsForLinking);
323
324 raw_fd_ostream &OutFile;
325 BinaryHolder &BinHolder;
326 LinkOptions Options;
327 std::mutex &ErrorHandlerMutex;
328 ThreadPoolInterface *ThreadPool;
329
330 /// A list of all .swiftinterface files referenced by the debug
331 /// info, mapping Module name to path on disk. The entries need to
332 /// be uniqued and sorted and there are only few entries expected
333 /// per compile unit, which is why this is a std::map.
334 std::map<std::string, std::string> ParseableSwiftInterfaces;
335
336 /// The compilation prefix map of the objects in each directory, or nullptr
337 /// if there is none.
338 StringMap<std::shared_ptr<PrefixMapper>> CompilationPrefixMaps;
339
340 bool ModuleCacheHintDisplayed = false;
341 bool ArchiveHintDisplayed = false;
342 bool HasVerificationErrors = false;
343};
344
345} // end namespace dsymutil
346} // end namespace llvm
347
348#endif // LLVM_TOOLS_DSYMUTIL_DWARFLINKER_H
349