1 //===- DWARFLinker.h --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_DWARFLINKER_DWARFLINKER_H
10 #define LLVM_DWARFLINKER_DWARFLINKER_H
12 #include "llvm/CodeGen/AccelTable.h"
13 #include "llvm/CodeGen/NonRelocatableStringpool.h"
14 #include "llvm/DWARFLinker/DWARFLinkerDeclContext.h"
15 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
16 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
17 #include "llvm/MC/MCDwarf.h"
22 enum class DwarfLinkerClient { Dsymutil, LLD, General };
24 /// Partial address range. Besides an offset, only the
25 /// HighPC is stored. The structure is stored in a map where the LowPC is the
27 struct ObjFileAddressRange {
30 /// Offset to apply to the linked address.
31 /// should be 0 for not-linked object file.
34 ObjFileAddressRange(uint64_t EndPC, int64_t Offset)
35 : HighPC(EndPC), Offset(Offset) {}
37 ObjFileAddressRange() : HighPC(0), Offset(0) {}
40 /// Map LowPC to ObjFileAddressRange.
41 using RangesTy = std::map<uint64_t, ObjFileAddressRange>;
43 /// AddressesMap represents information about valid addresses used
44 /// by debug information. Valid addresses are those which points to
45 /// live code sections. i.e. relocations for these addresses point
46 /// into sections which would be/are placed into resulting binary.
49 virtual ~AddressesMap();
51 /// Returns true if represented addresses are from linked file.
52 /// Returns false if represented addresses are from not-linked
54 virtual bool areRelocationsResolved() const = 0;
56 /// Checks that there are valid relocations against a .debug_info
57 /// section. Reset current relocation pointer if neccessary.
58 virtual bool hasValidRelocs(bool ResetRelocsPtr = true) = 0;
60 /// Checks that there is a relocation against .debug_info
61 /// table between \p StartOffset and \p NextOffset.
63 /// This function must be called with offsets in strictly ascending
64 /// order because it never looks back at relocations it already 'went past'.
65 /// \returns true and sets Info.InDebugMap if it is the case.
66 virtual bool hasValidRelocationAt(uint64_t StartOffset, uint64_t EndOffset,
67 CompileUnit::DIEInfo &Info) = 0;
69 /// Apply the valid relocations to the buffer \p Data, taking into
70 /// account that Data is at \p BaseOffset in the debug_info section.
72 /// This function must be called with monotonic \p BaseOffset values.
74 /// \returns true whether any reloc has been applied.
75 virtual bool applyValidRelocs(MutableArrayRef<char> Data, uint64_t BaseOffset,
76 bool IsLittleEndian) = 0;
78 /// Returns all valid functions address ranges(i.e., those ranges
79 /// which points to sections with code).
80 virtual RangesTy &getValidAddressRanges() = 0;
83 virtual void clear() = 0;
86 /// DwarfEmitter presents interface to generate all debug info tables.
89 virtual ~DwarfEmitter();
91 /// Emit DIE containing warnings.
92 virtual void emitPaperTrailWarningsDie(const Triple &Triple, DIE &Die) = 0;
94 /// Emit section named SecName with content equals to
95 /// corresponding section in Obj.
96 virtual void emitSectionContents(const object::ObjectFile &Obj,
97 StringRef SecName) = 0;
99 /// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
101 emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
102 unsigned DwarfVersion) = 0;
104 /// Emit the string table described by \p Pool.
105 virtual void emitStrings(const NonRelocatableStringpool &Pool) = 0;
107 /// Emit DWARF debug names.
109 emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table) = 0;
111 /// Emit Apple namespaces accelerator table.
113 emitAppleNamespaces(AccelTable<AppleAccelTableStaticOffsetData> &Table) = 0;
115 /// Emit Apple names accelerator table.
117 emitAppleNames(AccelTable<AppleAccelTableStaticOffsetData> &Table) = 0;
119 /// Emit Apple Objective-C accelerator table.
121 emitAppleObjc(AccelTable<AppleAccelTableStaticOffsetData> &Table) = 0;
123 /// Emit Apple type accelerator table.
125 emitAppleTypes(AccelTable<AppleAccelTableStaticTypeData> &Table) = 0;
127 /// Emit debug_ranges for \p FuncRange by translating the
128 /// original \p Entries.
129 virtual void emitRangesEntries(
130 int64_t UnitPcOffset, uint64_t OrigLowPc,
131 const FunctionIntervals::const_iterator &FuncRange,
132 const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
133 unsigned AddressSize) = 0;
135 /// Emit debug_aranges entries for \p Unit and if \p DoRangesSection is true,
136 /// also emit the debug_ranges entries for the DW_TAG_compile_unit's
137 /// DW_AT_ranges attribute.
138 virtual void emitUnitRangesEntries(CompileUnit &Unit,
139 bool DoRangesSection) = 0;
141 /// Copy the debug_line over to the updated binary while unobfuscating the
142 /// file names and directories.
143 virtual void translateLineTable(DataExtractor LineData, uint64_t Offset) = 0;
145 /// Emit the line table described in \p Rows into the debug_line section.
146 virtual void emitLineTableForUnit(MCDwarfLineTableParams Params,
147 StringRef PrologueBytes,
148 unsigned MinInstLength,
149 std::vector<DWARFDebugLine::Row> &Rows,
150 unsigned AdddressSize) = 0;
152 /// Emit the .debug_pubnames contribution for \p Unit.
153 virtual void emitPubNamesForUnit(const CompileUnit &Unit) = 0;
155 /// Emit the .debug_pubtypes contribution for \p Unit.
156 virtual void emitPubTypesForUnit(const CompileUnit &Unit) = 0;
159 virtual void emitCIE(StringRef CIEBytes) = 0;
161 /// Emit an FDE with data \p Bytes.
162 virtual void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address,
163 StringRef Bytes) = 0;
165 /// Emit the debug_loc contribution for \p Unit by copying the entries from
166 /// \p Dwarf and offsetting them. Update the location attributes to point to
168 virtual void emitLocationsForUnit(
169 const CompileUnit &Unit, DWARFContext &Dwarf,
170 std::function<void(StringRef, SmallVectorImpl<uint8_t> &)>
173 /// Emit the compilation unit header for \p Unit in the
174 /// debug_info section.
176 /// As a side effect, this also switches the current Dwarf version
177 /// of the MC layer to the one of U.getOrigUnit().
178 virtual void emitCompileUnitHeader(CompileUnit &Unit) = 0;
180 /// Recursively emit the DIE tree rooted at \p Die.
181 virtual void emitDIE(DIE &Die) = 0;
183 /// Returns size of generated .debug_line section.
184 virtual uint64_t getLineSectionSize() const = 0;
186 /// Returns size of generated .debug_frame section.
187 virtual uint64_t getFrameSectionSize() const = 0;
189 /// Returns size of generated .debug_ranges section.
190 virtual uint64_t getRangesSectionSize() const = 0;
192 /// Returns size of generated .debug_info section.
193 virtual uint64_t getDebugInfoSectionSize() const = 0;
196 } // end namespace llvm
198 #endif // LLVM_DWARFLINKER_DWARFLINKER_H