File: DwarfLinkerForBinary.h

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (293 lines) | stat: -rw-r--r-- 11,259 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//===- tools/dsymutil/DwarfLinkerForBinary.h --------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TOOLS_DSYMUTIL_DWARFLINKER_H
#define LLVM_TOOLS_DSYMUTIL_DWARFLINKER_H

#include "BinaryHolder.h"
#include "DebugMap.h"
#include "LinkUtils.h"
#include "MachOUtils.h"
#include "RelocationMap.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/Remarks/RemarkFormat.h"
#include "llvm/Remarks/RemarkLinker.h"
#include <mutex>
#include <optional>

namespace llvm {
using namespace dwarf_linker;

namespace dsymutil {

/// DwarfLinkerForBinaryRelocationMap contains the logic to handle the
/// relocations and to store them inside an associated RelocationMap.
class DwarfLinkerForBinaryRelocationMap {
public:
  void init(DWARFContext &Context);

  bool isInitialized() {
    return StoredValidDebugInfoRelocsMap.getMemorySize() != 0;
  }

  void addValidRelocs(RelocationMap &RM);

  void updateAndSaveValidRelocs(bool IsDWARF5,
                                std::vector<ValidReloc> &InRelocs,
                                uint64_t UnitOffset, int64_t LinkedOffset);

  void updateRelocationsWithUnitOffset(uint64_t OriginalUnitOffset,
                                       uint64_t OutputUnitOffset);

  /// Map compilation unit offset to the valid relocations to store
  /// @{
  DenseMap<uint64_t, std::vector<ValidReloc>> StoredValidDebugInfoRelocsMap;
  DenseMap<uint64_t, std::vector<ValidReloc>> StoredValidDebugAddrRelocsMap;
  /// @}

  DwarfLinkerForBinaryRelocationMap() = default;
};

struct ObjectWithRelocMap {
  ObjectWithRelocMap(
      std::unique_ptr<DWARFFile> Object,
      std::shared_ptr<DwarfLinkerForBinaryRelocationMap> OutRelocs)
      : Object(std::move(Object)), OutRelocs(OutRelocs) {}
  std::unique_ptr<DWARFFile> Object;
  std::shared_ptr<DwarfLinkerForBinaryRelocationMap> OutRelocs;
};

/// The core of the Dsymutil Dwarf linking logic.
///
/// The link of the dwarf information from the object files will be
/// driven by DWARFLinker. DwarfLinkerForBinary reads DebugMap objects
/// and pass information to the DWARFLinker. DWARFLinker
/// optimizes DWARF taking into account valid relocations.
/// Finally, optimized DWARF is passed to DwarfLinkerForBinary through
/// DWARFEmitter interface.
class DwarfLinkerForBinary {
public:
  DwarfLinkerForBinary(raw_fd_ostream &OutFile, BinaryHolder &BinHolder,
                       LinkOptions Options, std::mutex &ErrorHandlerMutex)
      : OutFile(OutFile), BinHolder(BinHolder), Options(std::move(Options)),
        ErrorHandlerMutex(ErrorHandlerMutex) {}

  /// Link the contents of the DebugMap.
  bool link(const DebugMap &);

  void reportWarning(Twine Warning, Twine Context = {},
                     const DWARFDie *DIE = nullptr) const;
  void reportError(Twine Error, Twine Context = {},
                   const DWARFDie *DIE = nullptr) const;

  /// Returns true if input verification is enabled and verification errors were
  /// found.
  bool InputVerificationFailed() const { return HasVerificationErrors; }

  /// Flags passed to DwarfLinker::lookForDIEsToKeep
  enum TraversalFlags {
    TF_Keep = 1 << 0,            ///< Mark the traversed DIEs as kept.
    TF_InFunctionScope = 1 << 1, ///< Current scope is a function scope.
    TF_DependencyWalk = 1 << 2,  ///< Walking the dependencies of a kept DIE.
    TF_ParentWalk = 1 << 3,      ///< Walking up the parents of a kept DIE.
    TF_ODR = 1 << 4,             ///< Use the ODR while keeping dependents.
    TF_SkipPC = 1 << 5,          ///< Skip all location attributes.
  };

private:

  /// Keeps track of relocations.
  class AddressManager : public dwarf_linker::AddressesMap {

    const DwarfLinkerForBinary &Linker;

    /// The valid relocations for the current DebugMapObject.
    /// These vectors are sorted by relocation offset.
    /// {
    std::vector<ValidReloc> ValidDebugInfoRelocs;
    std::vector<ValidReloc> ValidDebugAddrRelocs;
    /// }

    StringRef SrcFileName;

    uint8_t DebugMapObjectType;

    std::shared_ptr<DwarfLinkerForBinaryRelocationMap> DwarfLinkerRelocMap;

    std::optional<std::string> LibInstallName;

    /// Returns list of valid relocations from \p Relocs,
    /// between \p StartOffset and \p NextOffset.
    ///
    /// \returns true if any relocation is found.
    std::vector<ValidReloc>
    getRelocations(const std::vector<ValidReloc> &Relocs, uint64_t StartPos,
                   uint64_t EndPos);

    /// Resolve specified relocation \p Reloc.
    ///
    /// \returns resolved value.
    uint64_t relocate(const ValidReloc &Reloc) const;

    /// \returns value for the specified \p Reloc.
    int64_t getRelocValue(const ValidReloc &Reloc);

    /// Print contents of debug map entry for the specified \p Reloc.
    void printReloc(const ValidReloc &Reloc);

  public:
    AddressManager(DwarfLinkerForBinary &Linker, const object::ObjectFile &Obj,
                   const DebugMapObject &DMO,
                   std::shared_ptr<DwarfLinkerForBinaryRelocationMap> DLBRM)
        : Linker(Linker), SrcFileName(DMO.getObjectFilename()),
          DebugMapObjectType(MachO::N_OSO), DwarfLinkerRelocMap(DLBRM) {
      if (DMO.getRelocationMap().has_value()) {
        DebugMapObjectType = MachO::N_LIB;
        LibInstallName.emplace(DMO.getInstallName().value());
        const RelocationMap &RM = DMO.getRelocationMap().value();
        for (const auto &Reloc : RM.relocations()) {
          const auto *DebugMapEntry = DMO.lookupSymbol(Reloc.SymbolName);
          if (!DebugMapEntry)
            continue;
          std::optional<uint64_t> ObjAddress;
          ObjAddress.emplace(DebugMapEntry->getValue().ObjectAddress.value());
          ValidDebugInfoRelocs.emplace_back(
              Reloc.Offset, Reloc.Size, Reloc.Addend, Reloc.SymbolName,
              SymbolMapping(ObjAddress, DebugMapEntry->getValue().BinaryAddress,
                            DebugMapEntry->getValue().Size));
          // FIXME: Support relocations debug_addr.
        }
      } else {
        findValidRelocsInDebugSections(Obj, DMO);
      }
    }
    ~AddressManager() override { clear(); }

    bool hasValidRelocs() override {
      return !ValidDebugInfoRelocs.empty() || !ValidDebugAddrRelocs.empty();
    }

    /// \defgroup FindValidRelocations Translate debug map into a list
    /// of relevant relocations
    ///
    /// @{
    bool findValidRelocsInDebugSections(const object::ObjectFile &Obj,
                                        const DebugMapObject &DMO);

    bool findValidRelocs(const object::SectionRef &Section,
                         const object::ObjectFile &Obj,
                         const DebugMapObject &DMO,
                         std::vector<ValidReloc> &ValidRelocs);

    void findValidRelocsMachO(const object::SectionRef &Section,
                              const object::MachOObjectFile &Obj,
                              const DebugMapObject &DMO,
                              std::vector<ValidReloc> &ValidRelocs);
    /// @}

    /// Checks that there is a relocation in the \p Relocs array against a
    /// debug map entry between \p StartOffset and \p NextOffset.
    /// Print debug output if \p Verbose is set.
    ///
    /// \returns relocation value if relocation exist, otherwise std::nullopt.
    std::optional<int64_t>
    hasValidRelocationAt(const std::vector<ValidReloc> &Relocs,
                         uint64_t StartOffset, uint64_t EndOffset,
                         bool Verbose);

    std::optional<int64_t> getExprOpAddressRelocAdjustment(
        DWARFUnit &U, const DWARFExpression::Operation &Op,
        uint64_t StartOffset, uint64_t EndOffset, bool Verbose) override;

    std::optional<int64_t> getSubprogramRelocAdjustment(const DWARFDie &DIE,
                                                        bool Verbose) override;

    std::optional<StringRef> getLibraryInstallName() override;

    bool applyValidRelocs(MutableArrayRef<char> Data, uint64_t BaseOffset,
                          bool IsLittleEndian) override;

    bool needToSaveValidRelocs() override { return true; }

    void updateAndSaveValidRelocs(bool IsDWARF5, uint64_t OriginalUnitOffset,
                                  int64_t LinkedOffset, uint64_t StartOffset,
                                  uint64_t EndOffset) override;

    void updateRelocationsWithUnitOffset(uint64_t OriginalUnitOffset,
                                         uint64_t OutputUnitOffset) override;

    void clear() override {
      ValidDebugInfoRelocs.clear();
      ValidDebugAddrRelocs.clear();
    }
  };

private:
  /// \defgroup Helpers Various helper methods.
  ///
  /// @{
  template <typename OutStreamer>
  bool createStreamer(const Triple &TheTriple,
                      typename OutStreamer::OutputFileType FileType,
                      std::unique_ptr<OutStreamer> &Streamer,
                      raw_fd_ostream &OutFile);

  /// Attempt to load a debug object from disk.
  ErrorOr<const object::ObjectFile &> loadObject(const DebugMapObject &Obj,
                                                 const Triple &triple);
  ErrorOr<std::unique_ptr<dwarf_linker::DWARFFile>>
  loadObject(const DebugMapObject &Obj, const DebugMap &DebugMap,
             remarks::RemarkLinker &RL,
             std::shared_ptr<DwarfLinkerForBinaryRelocationMap> DLBRM);

  void collectRelocationsToApplyToSwiftReflectionSections(
      const object::SectionRef &Section, StringRef &Contents,
      const llvm::object::MachOObjectFile *MO,
      const std::vector<uint64_t> &SectionToOffsetInDwarf,
      const llvm::dsymutil::DebugMapObject *Obj,
      std::vector<MachOUtils::DwarfRelocationApplicationInfo>
          &RelocationsToApply) const;

  Error copySwiftInterfaces(StringRef Architecture) const;

  void copySwiftReflectionMetadata(
      const llvm::dsymutil::DebugMapObject *Obj,
      classic::DwarfStreamer *Streamer,
      std::vector<uint64_t> &SectionToOffsetInDwarf,
      std::vector<MachOUtils::DwarfRelocationApplicationInfo>
          &RelocationsToApply);

  template <typename Linker>
  bool linkImpl(const DebugMap &Map,
                typename Linker::OutputFileType ObjectType);

  Error emitRelocations(const DebugMap &DM,
                        std::vector<ObjectWithRelocMap> &ObjectsForLinking);

  raw_fd_ostream &OutFile;
  BinaryHolder &BinHolder;
  LinkOptions Options;
  std::mutex &ErrorHandlerMutex;

  std::vector<std::string> EmptyWarnings;

  /// A list of all .swiftinterface files referenced by the debug
  /// info, mapping Module name to path on disk. The entries need to
  /// be uniqued and sorted and there are only few entries expected
  /// per compile unit, which is why this is a std::map.
  std::map<std::string, std::string> ParseableSwiftInterfaces;

  bool ModuleCacheHintDisplayed = false;
  bool ArchiveHintDisplayed = false;
  bool HasVerificationErrors = false;
};

} // end namespace dsymutil
} // end namespace llvm

#endif // LLVM_TOOLS_DSYMUTIL_DWARFLINKER_H