blob: 0743cfc3ed403ebaead4087ef5eb75fd617b3aa6 [file] [log] [blame]
Frederic Riss31e081e2014-12-12 17:31:24 +00001//===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===//
2//
Jonas Devlieghere928fea22018-06-27 16:13:40 +00003// The LLVM Compiler Infrastructure
Frederic Riss31e081e2014-12-12 17:31:24 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Eugene Zelenko66f724e2017-11-01 21:16:06 +00009
Jonas Devlieghere928fea22018-06-27 16:13:40 +000010#include "DwarfLinker.h"
Frederic Rissb616a182015-01-28 18:27:01 +000011#include "BinaryHolder.h"
12#include "DebugMap.h"
Jonas Devlieghere928fea22018-06-27 16:13:40 +000013#include "DeclContext.h"
14#include "DwarfStreamer.h"
Frederic Rissef8ad012015-09-02 16:49:13 +000015#include "MachOUtils.h"
Frederic Rissfe893832015-08-26 05:09:52 +000016#include "NonRelocatableStringpool.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000017#include "dsymutil.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000018#include "llvm/ADT/ArrayRef.h"
Jonas Devlieghereb9365202018-03-13 14:27:15 +000019#include "llvm/ADT/BitVector.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000020#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/DenseMapInfo.h"
22#include "llvm/ADT/DenseSet.h"
23#include "llvm/ADT/FoldingSet.h"
24#include "llvm/ADT/Hashing.h"
Frederic Riss63a0e362015-03-12 18:45:10 +000025#include "llvm/ADT/IntervalMap.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000026#include "llvm/ADT/None.h"
27#include "llvm/ADT/Optional.h"
28#include "llvm/ADT/PointerIntPair.h"
Frederic Rissfb149692015-03-11 18:46:01 +000029#include "llvm/ADT/STLExtras.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000030#include "llvm/ADT/SmallString.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000031#include "llvm/ADT/StringMap.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000032#include "llvm/ADT/StringRef.h"
33#include "llvm/ADT/Triple.h"
34#include "llvm/ADT/Twine.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000035#include "llvm/BinaryFormat/Dwarf.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000036#include "llvm/BinaryFormat/MachO.h"
Jonas Devliegherea9aab592018-01-29 14:52:50 +000037#include "llvm/CodeGen/AccelTable.h"
Frederic Riss2929a7a2015-02-28 00:29:11 +000038#include "llvm/CodeGen/AsmPrinter.h"
Frederic Rissd0d92e72015-03-04 22:07:44 +000039#include "llvm/CodeGen/DIE.h"
Frederic Riss309fcf82015-07-21 22:41:43 +000040#include "llvm/Config/config.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000041#include "llvm/DebugInfo/DIContext.h"
42#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
Zachary Turner50418a02015-01-30 18:07:45 +000043#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000044#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
45#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
Wolfgang Piebdaecf942018-10-31 01:12:58 +000046#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000047#include "llvm/DebugInfo/DWARF/DWARFDie.h"
Frederic Riss5bdb2cd2015-02-13 23:18:29 +000048#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000049#include "llvm/DebugInfo/DWARF/DWARFSection.h"
50#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
Frederic Riss2929a7a2015-02-28 00:29:11 +000051#include "llvm/MC/MCAsmBackend.h"
52#include "llvm/MC/MCAsmInfo.h"
Frederic Riss2929a7a2015-02-28 00:29:11 +000053#include "llvm/MC/MCCodeEmitter.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000054#include "llvm/MC/MCContext.h"
Frederic Risse6412a62015-03-15 20:45:43 +000055#include "llvm/MC/MCDwarf.h"
Frederic Riss2929a7a2015-02-28 00:29:11 +000056#include "llvm/MC/MCInstrInfo.h"
57#include "llvm/MC/MCObjectFileInfo.h"
Peter Collingbourne17a98142018-05-18 18:26:45 +000058#include "llvm/MC/MCObjectWriter.h"
Frederic Riss2929a7a2015-02-28 00:29:11 +000059#include "llvm/MC/MCRegisterInfo.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000060#include "llvm/MC/MCSection.h"
Frederic Riss2929a7a2015-02-28 00:29:11 +000061#include "llvm/MC/MCStreamer.h"
Pete Cooperfc9bfcd2015-05-15 22:19:42 +000062#include "llvm/MC/MCSubtargetInfo.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000063#include "llvm/MC/MCTargetOptions.h"
Frederic Riss16acc912015-02-13 23:18:22 +000064#include "llvm/Object/MachO.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000065#include "llvm/Object/ObjectFile.h"
66#include "llvm/Object/SymbolicFile.h"
67#include "llvm/Support/Allocator.h"
68#include "llvm/Support/Casting.h"
69#include "llvm/Support/Compiler.h"
Jonas Devliegherea9aab592018-01-29 14:52:50 +000070#include "llvm/Support/DJB.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000071#include "llvm/Support/DataExtractor.h"
72#include "llvm/Support/Error.h"
73#include "llvm/Support/ErrorHandling.h"
74#include "llvm/Support/ErrorOr.h"
75#include "llvm/Support/FileSystem.h"
76#include "llvm/Support/Format.h"
Frederic Riss2e2ed162015-02-13 23:18:34 +000077#include "llvm/Support/LEB128.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000078#include "llvm/Support/MathExtras.h"
79#include "llvm/Support/MemoryBuffer.h"
80#include "llvm/Support/Path.h"
Frederic Riss2929a7a2015-02-28 00:29:11 +000081#include "llvm/Support/TargetRegistry.h"
Jonas Devlieghereb9365202018-03-13 14:27:15 +000082#include "llvm/Support/ThreadPool.h"
Jonas Devlieghereac330ef2017-10-31 13:54:15 +000083#include "llvm/Support/ToolOutputFile.h"
Jonas Devlieghere5baab4c2018-04-14 21:36:42 +000084#include "llvm/Support/WithColor.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000085#include "llvm/Support/raw_ostream.h"
Frederic Riss2929a7a2015-02-28 00:29:11 +000086#include "llvm/Target/TargetMachine.h"
87#include "llvm/Target/TargetOptions.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000088#include <algorithm>
89#include <cassert>
90#include <cinttypes>
91#include <climits>
92#include <cstdint>
93#include <cstdlib>
94#include <cstring>
95#include <limits>
96#include <map>
Greg Claytonfc1fc8b2016-12-01 18:56:29 +000097#include <memory>
Frederic Rissb616a182015-01-28 18:27:01 +000098#include <string>
Eugene Zelenko66f724e2017-11-01 21:16:06 +000099#include <system_error>
Frederic Risse83badb2015-03-13 18:35:57 +0000100#include <tuple>
Eugene Zelenko66f724e2017-11-01 21:16:06 +0000101#include <utility>
102#include <vector>
Frederic Riss31e081e2014-12-12 17:31:24 +0000103
104namespace llvm {
105namespace dsymutil {
106
Adrian Prantld6d5ed02015-09-22 18:50:58 +0000107/// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
108/// CompileUnit object instead.
Jonas Devlieghereea63d872018-06-28 15:01:42 +0000109static CompileUnit *getUnitForOffset(const UnitListTy &Units, unsigned Offset) {
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000110 auto CU = std::upper_bound(
111 Units.begin(), Units.end(), Offset,
112 [](uint32_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
113 return LHS < RHS->getOrigUnit().getNextUnitOffset();
114 });
Greg Claytonfc1fc8b2016-12-01 18:56:29 +0000115 return CU != Units.end() ? CU->get() : nullptr;
Frederic Riss5bdb2cd2015-02-13 23:18:29 +0000116}
117
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000118/// Resolve the DIE attribute reference that has been extracted in \p RefValue.
119/// The resulting DIE might be in another CompileUnit which is stored into \p
120/// ReferencedCU. \returns null if resolving fails for any reason.
Jonas Devlieghereea63d872018-06-28 15:01:42 +0000121static DWARFDie resolveDIEReference(const DwarfLinker &Linker,
122 const DebugMapObject &DMO,
123 const UnitListTy &Units,
124 const DWARFFormValue &RefValue,
125 const DWARFUnit &Unit, const DWARFDie &DIE,
126 CompileUnit *&RefCU) {
Frederic Riss5bdb2cd2015-02-13 23:18:29 +0000127 assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
Greg Clayton1a30f4b2016-10-31 16:46:02 +0000128 uint64_t RefOffset = *RefValue.getAsReference();
Frederic Riss5bdb2cd2015-02-13 23:18:29 +0000129
Adrian Prantld6d5ed02015-09-22 18:50:58 +0000130 if ((RefCU = getUnitForOffset(Units, RefOffset)))
Jonas Devlieghere28b2d0c2017-09-21 10:28:33 +0000131 if (const auto RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) {
132 // In a file with broken references, an attribute might point to a NULL
133 // DIE.
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000134 if (!RefDie.isNULL())
Jonas Devlieghere28b2d0c2017-09-21 10:28:33 +0000135 return RefDie;
136 }
Frederic Riss5bdb2cd2015-02-13 23:18:29 +0000137
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000138 Linker.reportWarning("could not find referenced DIE", DMO, &DIE);
Greg Claytonadb170f2016-12-13 18:25:19 +0000139 return DWARFDie();
Frederic Riss5bdb2cd2015-02-13 23:18:29 +0000140}
141
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000142/// \returns whether the passed \a Attr type might contain a DIE reference
143/// suitable for ODR uniquing.
Frederic Riss309fcf82015-07-21 22:41:43 +0000144static bool isODRAttribute(uint16_t Attr) {
145 switch (Attr) {
146 default:
147 return false;
148 case dwarf::DW_AT_type:
149 case dwarf::DW_AT_containing_type:
150 case dwarf::DW_AT_specification:
151 case dwarf::DW_AT_abstract_origin:
152 case dwarf::DW_AT_import:
153 return true;
154 }
155 llvm_unreachable("Improper attribute.");
156}
157
Adrian Prantlc6bf8fa2018-08-30 21:21:16 +0000158static bool isTypeTag(uint16_t Tag) {
159 switch (Tag) {
160 case dwarf::DW_TAG_array_type:
161 case dwarf::DW_TAG_class_type:
162 case dwarf::DW_TAG_enumeration_type:
163 case dwarf::DW_TAG_pointer_type:
164 case dwarf::DW_TAG_reference_type:
165 case dwarf::DW_TAG_string_type:
166 case dwarf::DW_TAG_structure_type:
167 case dwarf::DW_TAG_subroutine_type:
168 case dwarf::DW_TAG_typedef:
169 case dwarf::DW_TAG_union_type:
170 case dwarf::DW_TAG_ptr_to_member_type:
171 case dwarf::DW_TAG_set_type:
172 case dwarf::DW_TAG_subrange_type:
173 case dwarf::DW_TAG_base_type:
174 case dwarf::DW_TAG_const_type:
175 case dwarf::DW_TAG_constant:
176 case dwarf::DW_TAG_file_type:
177 case dwarf::DW_TAG_namelist:
178 case dwarf::DW_TAG_packed_type:
179 case dwarf::DW_TAG_volatile_type:
180 case dwarf::DW_TAG_restrict_type:
181 case dwarf::DW_TAG_atomic_type:
182 case dwarf::DW_TAG_interface_type:
183 case dwarf::DW_TAG_unspecified_type:
184 case dwarf::DW_TAG_shared_type:
185 return true;
186 default:
187 break;
188 }
189 return false;
190}
191
Greg Claytonadb170f2016-12-13 18:25:19 +0000192bool DwarfLinker::DIECloner::getDIENames(const DWARFDie &Die,
Jonas Devliegherea9aab592018-01-29 14:52:50 +0000193 AttributesInfo &Info,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000194 OffsetsStringPool &StringPool,
Jonas Devliegherea9aab592018-01-29 14:52:50 +0000195 bool StripTemplate) {
196 // This function will be called on DIEs having low_pcs and
197 // ranges. As getting the name might be more expansive, filter out
198 // blocks directly.
199 if (Die.getTag() == dwarf::DW_TAG_lexical_block)
200 return false;
201
Adrian Prantlafca6b42015-09-14 16:46:10 +0000202 // FIXME: a bit wasteful as the first getName might return the
Frederic Riss991f3ea2015-03-16 02:05:10 +0000203 // short name.
Jonas Devliegherea9aab592018-01-29 14:52:50 +0000204 if (!Info.MangledName)
205 if (const char *MangledName = Die.getName(DINameKind::LinkageName))
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000206 Info.MangledName = StringPool.getEntry(MangledName);
Frederic Riss991f3ea2015-03-16 02:05:10 +0000207
Jonas Devliegherea9aab592018-01-29 14:52:50 +0000208 if (!Info.Name)
209 if (const char *Name = Die.getName(DINameKind::ShortName))
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000210 Info.Name = StringPool.getEntry(Name);
Jonas Devliegherea9aab592018-01-29 14:52:50 +0000211
212 if (StripTemplate && Info.Name && Info.MangledName != Info.Name) {
213 // FIXME: dsymutil compatibility. This is wrong for operator<
214 auto Split = Info.Name.getString().split('<');
215 if (!Split.second.empty())
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000216 Info.NameWithoutTemplate = StringPool.getEntry(Split.first);
Jonas Devliegherea9aab592018-01-29 14:52:50 +0000217 }
Frederic Riss991f3ea2015-03-16 02:05:10 +0000218
219 return Info.Name || Info.MangledName;
220}
221
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000222/// Report a warning to the user, optionally including information about a
223/// specific \p DIE related to the warning.
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000224void DwarfLinker::reportWarning(const Twine &Warning, const DebugMapObject &DMO,
Greg Claytonadb170f2016-12-13 18:25:19 +0000225 const DWARFDie *DIE) const {
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000226 StringRef Context = DMO.getObjectFilename();
Frederic Rissa2cd6a52015-02-28 00:29:01 +0000227 warn(Warning, Context);
Frederic Riss5bdb2cd2015-02-13 23:18:29 +0000228
Frederic Rissd9a80bd2015-02-28 00:29:07 +0000229 if (!Options.Verbose || !DIE)
Frederic Riss5bdb2cd2015-02-13 23:18:29 +0000230 return;
231
Jonas Devlieghered2663072017-09-13 09:43:05 +0000232 DIDumpOptions DumpOpts;
Adrian Prantlae599aa2017-09-20 17:44:00 +0000233 DumpOpts.RecurseDepth = 0;
Jonas Devlieghered2663072017-09-13 09:43:05 +0000234 DumpOpts.Verbose = Options.Verbose;
235
Jonas Devlieghere5baab4c2018-04-14 21:36:42 +0000236 WithColor::note() << " in DIE:\n";
Adrian Prantlae599aa2017-09-20 17:44:00 +0000237 DIE->dump(errs(), 6 /* Indent */, DumpOpts);
Frederic Riss5bdb2cd2015-02-13 23:18:29 +0000238}
239
Benjamin Kramer36538ff2016-06-08 19:09:22 +0000240bool DwarfLinker::createStreamer(const Triple &TheTriple,
Rafael Espindola5feaa632017-11-15 20:55:53 +0000241 raw_fd_ostream &OutFile) {
Frederic Riss2929a7a2015-02-28 00:29:11 +0000242 if (Options.NoOutput)
243 return true;
244
Jonas Devliegherea5d860c2018-07-09 16:58:48 +0000245 Streamer = llvm::make_unique<DwarfStreamer>(OutFile, Options);
Rafael Espindola5feaa632017-11-15 20:55:53 +0000246 return Streamer->init(TheTriple);
Frederic Riss2929a7a2015-02-28 00:29:11 +0000247}
248
Adrian Prantl43df5822015-09-23 17:35:52 +0000249/// Recursive helper to build the global DeclContext information and
250/// gather the child->parent relationships in the original compile unit.
251///
252/// \return true when this DIE and all of its children are only
253/// forward declarations to types defined in external clang modules
254/// (i.e., forward declarations that are children of a DW_TAG_module).
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000255static bool analyzeContextInfo(const DWARFDie &DIE, unsigned ParentIdx,
256 CompileUnit &CU, DeclContext *CurrentDeclContext,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000257 UniquingStringPool &StringPool,
Adrian Prantl43df5822015-09-23 17:35:52 +0000258 DeclContextTree &Contexts,
Jonas Devlieghere60c60092018-09-07 10:29:22 +0000259 uint64_t ModulesEndOffset,
Adrian Prantl54a3f632015-09-23 20:44:37 +0000260 bool InImportedModule = false) {
Frederic Riss3ddec312015-01-28 22:15:14 +0000261 unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
Frederic Riss309fcf82015-07-21 22:41:43 +0000262 CompileUnit::DIEInfo &Info = CU.getInfo(MyIdx);
263
Adrian Prantl43df5822015-09-23 17:35:52 +0000264 // Clang imposes an ODR on modules(!) regardless of the language:
265 // "The module-id should consist of only a single identifier,
266 // which provides the name of the module being defined. Each
267 // module shall have a single definition."
268 //
269 // This does not extend to the types inside the modules:
270 // "[I]n C, this implies that if two structs are defined in
271 // different submodules with the same name, those two types are
272 // distinct types (but may be compatible types if their
273 // definitions match)."
274 //
275 // We treat non-C++ modules like namespaces for this reason.
Greg Claytonadb170f2016-12-13 18:25:19 +0000276 if (DIE.getTag() == dwarf::DW_TAG_module && ParentIdx == 0 &&
Adrian Prantl7f359b22017-09-30 00:22:24 +0000277 dwarf::toString(DIE.find(dwarf::DW_AT_name), "") !=
278 CU.getClangModuleName()) {
Adrian Prantl54a3f632015-09-23 20:44:37 +0000279 InImportedModule = true;
280 }
Adrian Prantl43df5822015-09-23 17:35:52 +0000281
Frederic Riss309fcf82015-07-21 22:41:43 +0000282 Info.ParentIdx = ParentIdx;
Adrian Prantlbc010882015-10-02 00:27:08 +0000283 bool InClangModule = CU.isClangModule() || InImportedModule;
284 if (CU.hasODR() || InClangModule) {
Frederic Riss309fcf82015-07-21 22:41:43 +0000285 if (CurrentDeclContext) {
Adrian Prantlbc010882015-10-02 00:27:08 +0000286 auto PtrInvalidPair = Contexts.getChildDeclContext(
287 *CurrentDeclContext, DIE, CU, StringPool, InClangModule);
Frederic Riss309fcf82015-07-21 22:41:43 +0000288 CurrentDeclContext = PtrInvalidPair.getPointer();
289 Info.Ctxt =
290 PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer();
Adrian Prantl64c7aa62017-07-21 02:07:33 +0000291 if (Info.Ctxt)
292 Info.Ctxt->setDefinedInClangModule(InClangModule);
Frederic Riss309fcf82015-07-21 22:41:43 +0000293 } else
294 Info.Ctxt = CurrentDeclContext = nullptr;
295 }
Frederic Riss3ddec312015-01-28 22:15:14 +0000296
Adrian Prantl54a3f632015-09-23 20:44:37 +0000297 Info.Prune = InImportedModule;
Greg Claytonadb170f2016-12-13 18:25:19 +0000298 if (DIE.hasChildren())
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000299 for (auto Child : DIE.children())
Jonas Devlieghere60c60092018-09-07 10:29:22 +0000300 Info.Prune &=
301 analyzeContextInfo(Child, MyIdx, CU, CurrentDeclContext, StringPool,
302 Contexts, ModulesEndOffset, InImportedModule);
Adrian Prantl43df5822015-09-23 17:35:52 +0000303
304 // Prune this DIE if it is either a forward declaration inside a
305 // DW_TAG_module or a DW_TAG_module that contains nothing but
306 // forward declarations.
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000307 Info.Prune &= (DIE.getTag() == dwarf::DW_TAG_module) ||
Adrian Prantlc6bf8fa2018-08-30 21:21:16 +0000308 (isTypeTag(DIE.getTag()) &&
309 dwarf::toUnsigned(DIE.find(dwarf::DW_AT_declaration), 0));
Adrian Prantl43df5822015-09-23 17:35:52 +0000310
Jonas Devlieghere60c60092018-09-07 10:29:22 +0000311 // Only prune forward declarations inside a DW_TAG_module for which a
312 // definition exists elsewhere.
313 if (ModulesEndOffset == 0)
314 Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset();
315 else
316 Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() > 0 &&
317 Info.Ctxt->getCanonicalDIEOffset() <= ModulesEndOffset;
Adrian Prantlfce22192015-10-05 23:11:20 +0000318
Adrian Prantl43df5822015-09-23 17:35:52 +0000319 return Info.Prune;
Jonas Devlieghere60c60092018-09-07 10:29:22 +0000320} // namespace dsymutil
Frederic Riss3ddec312015-01-28 22:15:14 +0000321
Frederic Riss2e2ed162015-02-13 23:18:34 +0000322static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
323 switch (Tag) {
324 default:
325 return false;
326 case dwarf::DW_TAG_subprogram:
327 case dwarf::DW_TAG_lexical_block:
328 case dwarf::DW_TAG_subroutine_type:
329 case dwarf::DW_TAG_structure_type:
330 case dwarf::DW_TAG_class_type:
331 case dwarf::DW_TAG_union_type:
332 return true;
333 }
334 llvm_unreachable("Invalid Tag");
335}
336
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000337void DwarfLinker::startDebugObject(LinkContext &Context) {
Frederic Risse6412a62015-03-15 20:45:43 +0000338 // Iterate over the debug map entries and put all the ones that are
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000339 // functions (because they have a size) into the Ranges map. This map is
340 // very similar to the FunctionRanges that are stored in each unit, with 2
341 // notable differences:
342 //
343 // 1. Obviously this one is global, while the other ones are per-unit.
344 //
345 // 2. This one contains not only the functions described in the DIE
346 // tree, but also the ones that are only in the debug map.
347 //
348 // The latter information is required to reproduce dsymutil's logic while
349 // linking line tables. The cases where this information matters look like
350 // bugs that need to be investigated, but for now we need to reproduce
351 // dsymutil's behavior.
Frederic Risse6412a62015-03-15 20:45:43 +0000352 // FIXME: Once we understood exactly if that information is needed,
353 // maybe totally remove this (or try to use it to do a real
354 // -gline-tables-only on Darwin.
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000355 for (const auto &Entry : Context.DMO.symbols()) {
Frederic Risse6412a62015-03-15 20:45:43 +0000356 const auto &Mapping = Entry.getValue();
Frederic Rissc5413fc2016-01-31 04:29:22 +0000357 if (Mapping.Size && Mapping.ObjectAddress)
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000358 Context.Ranges[*Mapping.ObjectAddress] = DebugMapObjectRange(
Frederic Rissc5413fc2016-01-31 04:29:22 +0000359 *Mapping.ObjectAddress + Mapping.Size,
360 int64_t(Mapping.BinaryAddress) - *Mapping.ObjectAddress);
Frederic Risse6412a62015-03-15 20:45:43 +0000361 }
Frederic Riss3ddec312015-01-28 22:15:14 +0000362}
363
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000364void DwarfLinker::endDebugObject(LinkContext &Context) {
365 Context.Clear();
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +0000366
Aaron Ballman9cd2a382015-06-26 14:51:22 +0000367 for (auto I = DIEBlocks.begin(), E = DIEBlocks.end(); I != E; ++I)
368 (*I)->~DIEBlock();
369 for (auto I = DIELocs.begin(), E = DIELocs.end(); I != E; ++I)
370 (*I)->~DIELoc();
Frederic Rissd0d92e72015-03-04 22:07:44 +0000371
372 DIEBlocks.clear();
373 DIELocs.clear();
374 DIEAlloc.Reset();
Frederic Riss16acc912015-02-13 23:18:22 +0000375}
376
Frederic Riss870f3ff2016-02-01 04:43:14 +0000377static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch) {
378 switch (Arch) {
379 case Triple::x86:
380 return RelocType == MachO::GENERIC_RELOC_SECTDIFF ||
381 RelocType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
382 case Triple::x86_64:
383 return RelocType == MachO::X86_64_RELOC_SUBTRACTOR;
384 case Triple::arm:
385 case Triple::thumb:
386 return RelocType == MachO::ARM_RELOC_SECTDIFF ||
387 RelocType == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
388 RelocType == MachO::ARM_RELOC_HALF ||
389 RelocType == MachO::ARM_RELOC_HALF_SECTDIFF;
390 case Triple::aarch64:
391 return RelocType == MachO::ARM64_RELOC_SUBTRACTOR;
392 default:
393 return false;
394 }
395}
396
Adrian Prantl90d56f02017-07-21 16:51:17 +0000397/// Iterate over the relocations of the given \p Section and
Frederic Riss16acc912015-02-13 23:18:22 +0000398/// store the ones that correspond to debug map entries into the
399/// ValidRelocs array.
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000400void DwarfLinker::RelocationManager::findValidRelocsMachO(
401 const object::SectionRef &Section, const object::MachOObjectFile &Obj,
402 const DebugMapObject &DMO) {
Frederic Riss16acc912015-02-13 23:18:22 +0000403 StringRef Contents;
404 Section.getContents(Contents);
405 DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
Frederic Riss870f3ff2016-02-01 04:43:14 +0000406 bool SkipNext = false;
Frederic Riss16acc912015-02-13 23:18:22 +0000407
408 for (const object::RelocationRef &Reloc : Section.relocations()) {
Frederic Riss870f3ff2016-02-01 04:43:14 +0000409 if (SkipNext) {
410 SkipNext = false;
411 continue;
412 }
413
Frederic Riss16acc912015-02-13 23:18:22 +0000414 object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
415 MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
Frederic Riss870f3ff2016-02-01 04:43:14 +0000416
417 if (isMachOPairedReloc(Obj.getAnyRelocationType(MachOReloc),
418 Obj.getArch())) {
419 SkipNext = true;
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000420 Linker.reportWarning("unsupported relocation in debug_info section.",
421 DMO);
Frederic Riss870f3ff2016-02-01 04:43:14 +0000422 continue;
423 }
424
Frederic Riss16acc912015-02-13 23:18:22 +0000425 unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
Rafael Espindolaff676292015-06-29 23:29:12 +0000426 uint64_t Offset64 = Reloc.getOffset();
427 if ((RelocSize != 4 && RelocSize != 8)) {
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000428 Linker.reportWarning("unsupported relocation in debug_info section.",
429 DMO);
Frederic Riss16acc912015-02-13 23:18:22 +0000430 continue;
431 }
432 uint32_t Offset = Offset64;
433 // Mach-o uses REL relocations, the addend is at the relocation offset.
434 uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
Frederic Rissf768b252016-02-01 03:44:22 +0000435 uint64_t SymAddress;
436 int64_t SymOffset;
437
438 if (Obj.isRelocationScattered(MachOReloc)) {
439 // The address of the base symbol for scattered relocations is
440 // stored in the reloc itself. The actual addend will store the
441 // base address plus the offset.
442 SymAddress = Obj.getScatteredRelocationValue(MachOReloc);
443 SymOffset = int64_t(Addend) - SymAddress;
444 } else {
445 SymAddress = Addend;
446 SymOffset = 0;
447 }
Frederic Riss16acc912015-02-13 23:18:22 +0000448
449 auto Sym = Reloc.getSymbol();
450 if (Sym != Obj.symbol_end()) {
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000451 Expected<StringRef> SymbolName = Sym->getName();
Rafael Espindola8a806412015-07-02 20:55:21 +0000452 if (!SymbolName) {
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000453 consumeError(SymbolName.takeError());
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000454 Linker.reportWarning("error getting relocation symbol name.", DMO);
Frederic Riss16acc912015-02-13 23:18:22 +0000455 continue;
456 }
Rafael Espindola8a806412015-07-02 20:55:21 +0000457 if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
Frederic Riss16acc912015-02-13 23:18:22 +0000458 ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
Frederic Rissf768b252016-02-01 03:44:22 +0000459 } else if (const auto *Mapping = DMO.lookupObjectAddress(SymAddress)) {
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000460 // Do not store the addend. The addend was the address of the symbol in
461 // the object file, the address in the binary that is stored in the debug
462 // map doesn't need to be offset.
Frederic Rissf768b252016-02-01 03:44:22 +0000463 ValidRelocs.emplace_back(Offset64, RelocSize, SymOffset, Mapping);
Frederic Riss16acc912015-02-13 23:18:22 +0000464 }
465 }
466}
467
Adrian Prantl90d56f02017-07-21 16:51:17 +0000468/// Dispatch the valid relocation finding logic to the
Frederic Riss16acc912015-02-13 23:18:22 +0000469/// appropriate handler depending on the object file format.
Adrian Prantl0a0a9602015-09-11 23:45:30 +0000470bool DwarfLinker::RelocationManager::findValidRelocs(
471 const object::SectionRef &Section, const object::ObjectFile &Obj,
472 const DebugMapObject &DMO) {
Frederic Riss16acc912015-02-13 23:18:22 +0000473 // Dispatch to the right handler depending on the file type.
474 if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
475 findValidRelocsMachO(Section, *MachOObj, DMO);
476 else
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000477 Linker.reportWarning(
478 Twine("unsupported object file type: ") + Obj.getFileName(), DMO);
Frederic Riss16acc912015-02-13 23:18:22 +0000479
480 if (ValidRelocs.empty())
481 return false;
482
483 // Sort the relocations by offset. We will walk the DIEs linearly in
484 // the file, this allows us to just keep an index in the relocation
485 // array that we advance during our walk, rather than resorting to
486 // some associative container. See DwarfLinker::NextValidReloc.
Fangrui Song3b35e172018-09-27 02:13:45 +0000487 llvm::sort(ValidRelocs);
Frederic Riss16acc912015-02-13 23:18:22 +0000488 return true;
489}
490
Adrian Prantl90d56f02017-07-21 16:51:17 +0000491/// Look for relocations in the debug_info section that match
Frederic Riss16acc912015-02-13 23:18:22 +0000492/// entries in the debug map. These relocations will drive the Dwarf
493/// link by indicating which DIEs refer to symbols present in the
494/// linked binary.
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000495/// \returns whether there are any valid relocations in the debug info.
496bool DwarfLinker::RelocationManager::findValidRelocsInDebugInfo(
497 const object::ObjectFile &Obj, const DebugMapObject &DMO) {
Frederic Riss16acc912015-02-13 23:18:22 +0000498 // Find the debug_info section.
499 for (const object::SectionRef &Section : Obj.sections()) {
500 StringRef SectionName;
501 Section.getName(SectionName);
502 SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
503 if (SectionName != "debug_info")
504 continue;
505 return findValidRelocs(Section, Obj, DMO);
506 }
507 return false;
508}
Frederic Riss3ddec312015-01-28 22:15:14 +0000509
Adrian Prantl90d56f02017-07-21 16:51:17 +0000510/// Checks that there is a relocation against an actual debug
Frederic Riss2e2ed162015-02-13 23:18:34 +0000511/// map entry between \p StartOffset and \p NextOffset.
512///
513/// This function must be called with offsets in strictly ascending
514/// order because it never looks back at relocations it already 'went past'.
515/// \returns true and sets Info.InDebugMap if it is the case.
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000516bool DwarfLinker::RelocationManager::hasValidRelocation(
517 uint32_t StartOffset, uint32_t EndOffset, CompileUnit::DIEInfo &Info) {
Frederic Riss2e2ed162015-02-13 23:18:34 +0000518 assert(NextValidReloc == 0 ||
519 StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
520 if (NextValidReloc >= ValidRelocs.size())
521 return false;
522
523 uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
524
525 // We might need to skip some relocs that we didn't consider. For
526 // example the high_pc of a discarded DIE might contain a reloc that
527 // is in the list because it actually corresponds to the start of a
528 // function that is in the debug map.
529 while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
530 RelocOffset = ValidRelocs[++NextValidReloc].Offset;
531
532 if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
533 return false;
534
535 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
Frederic Riss786b4712015-06-01 21:12:45 +0000536 const auto &Mapping = ValidReloc.Mapping->getValue();
Eugene Zelenko66f724e2017-11-01 21:16:06 +0000537 uint64_t ObjectAddress = Mapping.ObjectAddress
538 ? uint64_t(*Mapping.ObjectAddress)
539 : std::numeric_limits<uint64_t>::max();
Adrian Prantl0a0a9602015-09-11 23:45:30 +0000540 if (Linker.Options.Verbose)
Frederic Riss2e2ed162015-02-13 23:18:34 +0000541 outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000542 << " "
543 << format("\t%016" PRIx64 " => %016" PRIx64, ObjectAddress,
544 uint64_t(Mapping.BinaryAddress));
Frederic Riss2e2ed162015-02-13 23:18:34 +0000545
Frederic Rissc5413fc2016-01-31 04:29:22 +0000546 Info.AddrAdjust = int64_t(Mapping.BinaryAddress) + ValidReloc.Addend;
547 if (Mapping.ObjectAddress)
548 Info.AddrAdjust -= ObjectAddress;
Frederic Riss2e2ed162015-02-13 23:18:34 +0000549 Info.InDebugMap = true;
550 return true;
551}
552
Adrian Prantl90d56f02017-07-21 16:51:17 +0000553/// Get the starting and ending (exclusive) offset for the
Frederic Riss2e2ed162015-02-13 23:18:34 +0000554/// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
555/// supposed to point to the position of the first attribute described
556/// by \p Abbrev.
557/// \return [StartOffset, EndOffset) as a pair.
558static std::pair<uint32_t, uint32_t>
559getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
560 unsigned Offset, const DWARFUnit &Unit) {
561 DataExtractor Data = Unit.getDebugInfoExtractor();
562
563 for (unsigned i = 0; i < Idx; ++i)
Paul Robinsonfd770ea2017-06-26 18:43:01 +0000564 DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset,
565 Unit.getFormParams());
Frederic Riss2e2ed162015-02-13 23:18:34 +0000566
567 uint32_t End = Offset;
Paul Robinsonfd770ea2017-06-26 18:43:01 +0000568 DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End,
569 Unit.getFormParams());
Frederic Riss2e2ed162015-02-13 23:18:34 +0000570
571 return std::make_pair(Offset, End);
572}
573
Adrian Prantl90d56f02017-07-21 16:51:17 +0000574/// Check if a variable describing DIE should be kept.
Frederic Riss2e2ed162015-02-13 23:18:34 +0000575/// \returns updated TraversalFlags.
Adrian Prantl0a0a9602015-09-11 23:45:30 +0000576unsigned DwarfLinker::shouldKeepVariableDIE(RelocationManager &RelocMgr,
Greg Claytonadb170f2016-12-13 18:25:19 +0000577 const DWARFDie &DIE,
Adrian Prantl0a0a9602015-09-11 23:45:30 +0000578 CompileUnit &Unit,
579 CompileUnit::DIEInfo &MyInfo,
580 unsigned Flags) {
Frederic Riss2e2ed162015-02-13 23:18:34 +0000581 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
582
583 // Global variables with constant value can always be kept.
584 if (!(Flags & TF_InFunctionScope) &&
Greg Claytonb07cdea2016-11-15 01:23:06 +0000585 Abbrev->findAttributeIndex(dwarf::DW_AT_const_value)) {
Frederic Riss2e2ed162015-02-13 23:18:34 +0000586 MyInfo.InDebugMap = true;
587 return Flags | TF_Keep;
588 }
589
Greg Claytonb07cdea2016-11-15 01:23:06 +0000590 Optional<uint32_t> LocationIdx =
591 Abbrev->findAttributeIndex(dwarf::DW_AT_location);
592 if (!LocationIdx)
Frederic Riss2e2ed162015-02-13 23:18:34 +0000593 return Flags;
594
595 uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
596 const DWARFUnit &OrigUnit = Unit.getOrigUnit();
597 uint32_t LocationOffset, LocationEndOffset;
598 std::tie(LocationOffset, LocationEndOffset) =
Greg Claytonb07cdea2016-11-15 01:23:06 +0000599 getAttributeOffsets(Abbrev, *LocationIdx, Offset, OrigUnit);
Frederic Riss2e2ed162015-02-13 23:18:34 +0000600
601 // See if there is a relocation to a valid debug map entry inside
602 // this variable's location. The order is important here. We want to
603 // always check in the variable has a valid relocation, so that the
604 // DIEInfo is filled. However, we don't want a static variable in a
605 // function to force us to keep the enclosing function.
Adrian Prantl0a0a9602015-09-11 23:45:30 +0000606 if (!RelocMgr.hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
Frederic Riss2e2ed162015-02-13 23:18:34 +0000607 (Flags & TF_InFunctionScope))
608 return Flags;
609
Jonas Devlieghered2663072017-09-13 09:43:05 +0000610 if (Options.Verbose) {
611 DIDumpOptions DumpOpts;
Adrian Prantlae599aa2017-09-20 17:44:00 +0000612 DumpOpts.RecurseDepth = 0;
Jonas Devlieghered2663072017-09-13 09:43:05 +0000613 DumpOpts.Verbose = Options.Verbose;
Adrian Prantlae599aa2017-09-20 17:44:00 +0000614 DIE.dump(outs(), 8 /* Indent */, DumpOpts);
Jonas Devlieghered2663072017-09-13 09:43:05 +0000615 }
Frederic Riss2e2ed162015-02-13 23:18:34 +0000616
617 return Flags | TF_Keep;
618}
619
Adrian Prantl90d56f02017-07-21 16:51:17 +0000620/// Check if a function describing DIE should be kept.
Frederic Riss2e2ed162015-02-13 23:18:34 +0000621/// \returns updated TraversalFlags.
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000622unsigned DwarfLinker::shouldKeepSubprogramDIE(
623 RelocationManager &RelocMgr, RangesTy &Ranges, const DWARFDie &DIE,
624 const DebugMapObject &DMO, CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
625 unsigned Flags) {
Frederic Riss2e2ed162015-02-13 23:18:34 +0000626 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
627
628 Flags |= TF_InFunctionScope;
629
Greg Claytonb07cdea2016-11-15 01:23:06 +0000630 Optional<uint32_t> LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
631 if (!LowPcIdx)
Frederic Riss2e2ed162015-02-13 23:18:34 +0000632 return Flags;
633
634 uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
Jonas Devlieghere35c4cd12018-02-20 17:34:29 +0000635 DWARFUnit &OrigUnit = Unit.getOrigUnit();
Frederic Riss2e2ed162015-02-13 23:18:34 +0000636 uint32_t LowPcOffset, LowPcEndOffset;
637 std::tie(LowPcOffset, LowPcEndOffset) =
Greg Claytonb07cdea2016-11-15 01:23:06 +0000638 getAttributeOffsets(Abbrev, *LowPcIdx, Offset, OrigUnit);
Frederic Riss2e2ed162015-02-13 23:18:34 +0000639
Greg Claytonbeabb632017-01-13 21:08:18 +0000640 auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc));
Greg Claytonc64f9192016-12-14 22:38:08 +0000641 assert(LowPc.hasValue() && "low_pc attribute is not an address.");
642 if (!LowPc ||
Adrian Prantl0a0a9602015-09-11 23:45:30 +0000643 !RelocMgr.hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
Frederic Riss2e2ed162015-02-13 23:18:34 +0000644 return Flags;
645
Jonas Devlieghered2663072017-09-13 09:43:05 +0000646 if (Options.Verbose) {
647 DIDumpOptions DumpOpts;
Adrian Prantlae599aa2017-09-20 17:44:00 +0000648 DumpOpts.RecurseDepth = 0;
Jonas Devlieghered2663072017-09-13 09:43:05 +0000649 DumpOpts.Verbose = Options.Verbose;
Adrian Prantlae599aa2017-09-20 17:44:00 +0000650 DIE.dump(outs(), 8 /* Indent */, DumpOpts);
Jonas Devlieghered2663072017-09-13 09:43:05 +0000651 }
Frederic Riss2e2ed162015-02-13 23:18:34 +0000652
Jonas Devlieghere35c4cd12018-02-20 17:34:29 +0000653 if (DIE.getTag() == dwarf::DW_TAG_label) {
654 if (Unit.hasLabelAt(*LowPc))
655 return Flags;
656 // FIXME: dsymutil-classic compat. dsymutil-classic doesn't consider labels
657 // that don't fall into the CU's aranges. This is wrong IMO. Debug info
658 // generation bugs aside, this is really wrong in the case of labels, where
659 // a label marking the end of a function will have a PC == CU's high_pc.
660 if (dwarf::toAddress(OrigUnit.getUnitDIE().find(dwarf::DW_AT_high_pc))
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000661 .getValueOr(UINT64_MAX) <= LowPc)
Jonas Devlieghere35c4cd12018-02-20 17:34:29 +0000662 return Flags;
663 Unit.addLabelLowPc(*LowPc, MyInfo.AddrAdjust);
664 return Flags | TF_Keep;
665 }
666
Frederic Riss63a0e362015-03-12 18:45:10 +0000667 Flags |= TF_Keep;
668
Greg Clayton655737a2016-12-19 20:36:41 +0000669 Optional<uint64_t> HighPc = DIE.getHighPC(*LowPc);
670 if (!HighPc) {
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000671 reportWarning("Function without high_pc. Range will be discarded.\n", DMO,
672 &DIE);
Frederic Riss63a0e362015-03-12 18:45:10 +0000673 return Flags;
674 }
675
Frederic Risse6412a62015-03-15 20:45:43 +0000676 // Replace the debug map range with a more accurate one.
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000677 Ranges[*LowPc] = DebugMapObjectRange(*HighPc, MyInfo.AddrAdjust);
Greg Clayton655737a2016-12-19 20:36:41 +0000678 Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust);
Frederic Riss63a0e362015-03-12 18:45:10 +0000679 return Flags;
Frederic Riss2e2ed162015-02-13 23:18:34 +0000680}
681
Adrian Prantl90d56f02017-07-21 16:51:17 +0000682/// Check if a DIE should be kept.
Frederic Riss2e2ed162015-02-13 23:18:34 +0000683/// \returns updated TraversalFlags.
Adrian Prantl0a0a9602015-09-11 23:45:30 +0000684unsigned DwarfLinker::shouldKeepDIE(RelocationManager &RelocMgr,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000685 RangesTy &Ranges, const DWARFDie &DIE,
686 const DebugMapObject &DMO,
687 CompileUnit &Unit,
Frederic Riss2e2ed162015-02-13 23:18:34 +0000688 CompileUnit::DIEInfo &MyInfo,
689 unsigned Flags) {
690 switch (DIE.getTag()) {
691 case dwarf::DW_TAG_constant:
692 case dwarf::DW_TAG_variable:
Adrian Prantl0a0a9602015-09-11 23:45:30 +0000693 return shouldKeepVariableDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
Frederic Riss2e2ed162015-02-13 23:18:34 +0000694 case dwarf::DW_TAG_subprogram:
Jonas Devlieghere35c4cd12018-02-20 17:34:29 +0000695 case dwarf::DW_TAG_label:
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000696 return shouldKeepSubprogramDIE(RelocMgr, Ranges, DIE, DMO, Unit, MyInfo,
697 Flags);
Frederic Riss2e2ed162015-02-13 23:18:34 +0000698 case dwarf::DW_TAG_imported_module:
699 case dwarf::DW_TAG_imported_declaration:
700 case dwarf::DW_TAG_imported_unit:
701 // We always want to keep these.
702 return Flags | TF_Keep;
Greg Claytonfc1fc8b2016-12-01 18:56:29 +0000703 default:
704 break;
Frederic Riss2e2ed162015-02-13 23:18:34 +0000705 }
706
707 return Flags;
708}
709
Adrian Prantl90d56f02017-07-21 16:51:17 +0000710/// Mark the passed DIE as well as all the ones it depends on
Frederic Riss2e2ed162015-02-13 23:18:34 +0000711/// as kept.
712///
713/// This function is called by lookForDIEsToKeep on DIEs that are
714/// newly discovered to be needed in the link. It recursively calls
715/// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
716/// TraversalFlags to inform it that it's not doing the primary DIE
717/// tree walk.
Jonas Devlieghereea63d872018-06-28 15:01:42 +0000718void DwarfLinker::keepDIEAndDependencies(
719 RelocationManager &RelocMgr, RangesTy &Ranges, const UnitListTy &Units,
720 const DWARFDie &Die, CompileUnit::DIEInfo &MyInfo,
721 const DebugMapObject &DMO, CompileUnit &CU, bool UseODR) {
Greg Claytonadb170f2016-12-13 18:25:19 +0000722 DWARFUnit &Unit = CU.getOrigUnit();
Frederic Riss2e2ed162015-02-13 23:18:34 +0000723 MyInfo.Keep = true;
724
Jonas Devlieghere75475a82017-08-31 20:22:31 +0000725 // We're looking for incomplete types.
726 MyInfo.Incomplete = Die.getTag() != dwarf::DW_TAG_subprogram &&
727 Die.getTag() != dwarf::DW_TAG_member &&
728 dwarf::toUnsigned(Die.find(dwarf::DW_AT_declaration), 0);
729
Frederic Riss2e2ed162015-02-13 23:18:34 +0000730 // First mark all the parent chain as kept.
731 unsigned AncestorIdx = MyInfo.ParentIdx;
732 while (!CU.getInfo(AncestorIdx).Keep) {
Frederic Riss309fcf82015-07-21 22:41:43 +0000733 unsigned ODRFlag = UseODR ? TF_ODR : 0;
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000734 lookForDIEsToKeep(RelocMgr, Ranges, Units, Unit.getDIEAtIndex(AncestorIdx),
735 DMO, CU,
Frederic Riss309fcf82015-07-21 22:41:43 +0000736 TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag);
Frederic Riss2e2ed162015-02-13 23:18:34 +0000737 AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
738 }
739
740 // Then we need to mark all the DIEs referenced by this DIE's
741 // attributes as kept.
Paul Robinson502fd5d2017-06-29 16:52:08 +0000742 DWARFDataExtractor Data = Unit.getDebugInfoExtractor();
Frederic Riss5571e772015-09-11 04:17:25 +0000743 const auto *Abbrev = Die.getAbbreviationDeclarationPtr();
744 uint32_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode());
Frederic Riss2e2ed162015-02-13 23:18:34 +0000745
Jonas Devlieghere75475a82017-08-31 20:22:31 +0000746 // Mark all DIEs referenced through attributes as kept.
Frederic Riss2e2ed162015-02-13 23:18:34 +0000747 for (const auto &AttrSpec : Abbrev->attributes()) {
748 DWARFFormValue Val(AttrSpec.Form);
749
Jonas Devliegheref521abc2018-02-27 19:24:36 +0000750 if (!Val.isFormClass(DWARFFormValue::FC_Reference) ||
751 AttrSpec.Attr == dwarf::DW_AT_sibling) {
Paul Robinsonfd770ea2017-06-26 18:43:01 +0000752 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
753 Unit.getFormParams());
Frederic Riss2e2ed162015-02-13 23:18:34 +0000754 continue;
755 }
756
Paul Robinsone9d757c2017-11-07 19:57:12 +0000757 Val.extractValue(Data, &Offset, Unit.getFormParams(), &Unit);
Frederic Riss2e2ed162015-02-13 23:18:34 +0000758 CompileUnit *ReferencedCU;
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000759 if (auto RefDie = resolveDIEReference(*this, DMO, Units, Val, Unit, Die,
760 ReferencedCU)) {
Adrian Prantl80ce2822017-07-21 16:51:09 +0000761 uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDie);
Frederic Riss309fcf82015-07-21 22:41:43 +0000762 CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx);
Adrian Prantl64c7aa62017-07-21 02:07:33 +0000763 bool IsModuleRef = Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() &&
764 Info.Ctxt->isDefinedInClangModule();
Frederic Riss309fcf82015-07-21 22:41:43 +0000765 // If the referenced DIE has a DeclContext that has already been
766 // emitted, then do not keep the one in this CU. We'll link to
767 // the canonical DIE in cloneDieReferenceAttribute.
768 // FIXME: compatibility with dsymutil-classic. UseODR shouldn't
769 // be necessary and could be advantageously replaced by
770 // ReferencedCU->hasODR() && CU.hasODR().
771 // FIXME: compatibility with dsymutil-classic. There is no
772 // reason not to unique ref_addr references.
Adrian Prantl64c7aa62017-07-21 02:07:33 +0000773 if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && (UseODR || IsModuleRef) &&
774 Info.Ctxt &&
Frederic Riss309fcf82015-07-21 22:41:43 +0000775 Info.Ctxt != ReferencedCU->getInfo(Info.ParentIdx).Ctxt &&
776 Info.Ctxt->getCanonicalDIEOffset() && isODRAttribute(AttrSpec.Attr))
777 continue;
778
Adrian Prantlb29f9e52015-11-10 21:31:05 +0000779 // Keep a module forward declaration if there is no definition.
780 if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
781 Info.Ctxt->getCanonicalDIEOffset()))
782 Info.Prune = false;
783
Frederic Riss309fcf82015-07-21 22:41:43 +0000784 unsigned ODRFlag = UseODR ? TF_ODR : 0;
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000785 lookForDIEsToKeep(RelocMgr, Ranges, Units, RefDie, DMO, *ReferencedCU,
Frederic Riss309fcf82015-07-21 22:41:43 +0000786 TF_Keep | TF_DependencyWalk | ODRFlag);
Jonas Devlieghere75475a82017-08-31 20:22:31 +0000787
788 // The incomplete property is propagated if the current DIE is complete
789 // but references an incomplete DIE.
790 if (Info.Incomplete && !MyInfo.Incomplete &&
791 (Die.getTag() == dwarf::DW_TAG_typedef ||
792 Die.getTag() == dwarf::DW_TAG_member ||
793 Die.getTag() == dwarf::DW_TAG_reference_type ||
794 Die.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
795 Die.getTag() == dwarf::DW_TAG_pointer_type))
796 MyInfo.Incomplete = true;
Frederic Riss309fcf82015-07-21 22:41:43 +0000797 }
Frederic Riss2e2ed162015-02-13 23:18:34 +0000798 }
799}
800
Jonas Devlieghere3289ee62018-08-01 13:24:39 +0000801namespace {
802/// This class represents an item in the work list. In addition to it's obvious
803/// purpose of representing the state associated with a particular run of the
804/// work loop, it also serves as a marker to indicate that we should run the
805/// "continuation" code.
806///
807/// Originally, the latter was lambda which allowed arbitrary code to be run.
808/// Because we always need to run the exact same code, it made more sense to
809/// use a boolean and repurpose the already existing DIE field.
810struct WorklistItem {
811 DWARFDie Die;
812 unsigned Flags;
813 bool IsContinuation;
814 CompileUnit::DIEInfo *ChildInfo = nullptr;
815
816 /// Construct a classic worklist item.
817 WorklistItem(DWARFDie Die, unsigned Flags)
818 : Die(Die), Flags(Flags), IsContinuation(false){};
819
820 /// Creates a continuation marker.
821 WorklistItem(DWARFDie Die) : Die(Die), IsContinuation(true){};
822};
823} // namespace
824
825// Helper that updates the completeness of the current DIE. It depends on the
826// fact that the incompletness of its children is already computed.
827static void updateIncompleteness(const DWARFDie &Die,
828 CompileUnit::DIEInfo &ChildInfo,
829 CompileUnit &CU) {
830 // Only propagate incomplete members.
831 if (Die.getTag() != dwarf::DW_TAG_structure_type &&
832 Die.getTag() != dwarf::DW_TAG_class_type)
833 return;
834
835 unsigned Idx = CU.getOrigUnit().getDIEIndex(Die);
836 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
837
838 if (MyInfo.Incomplete)
839 return;
840
841 if (ChildInfo.Incomplete || ChildInfo.Prune)
842 MyInfo.Incomplete = true;
843}
844
Adrian Prantl90d56f02017-07-21 16:51:17 +0000845/// Recursively walk the \p DIE tree and look for DIEs to
Frederic Riss2e2ed162015-02-13 23:18:34 +0000846/// keep. Store that information in \p CU's DIEInfo.
847///
848/// This function is the entry point of the DIE selection
849/// algorithm. It is expected to walk the DIE tree in file order and
850/// (though the mediation of its helper) call hasValidRelocation() on
851/// each DIE that might be a 'root DIE' (See DwarfLinker class
852/// comment).
853/// While walking the dependencies of root DIEs, this function is
854/// also called, but during these dependency walks the file order is
855/// not respected. The TF_DependencyWalk flag tells us which kind of
856/// traversal we are currently doing.
Jonas Devlieghere75475a82017-08-31 20:22:31 +0000857///
858/// The return value indicates whether the DIE is incomplete.
Jonas Devlieghere3289ee62018-08-01 13:24:39 +0000859void DwarfLinker::lookForDIEsToKeep(RelocationManager &RelocMgr,
Jonas Devlieghereea63d872018-06-28 15:01:42 +0000860 RangesTy &Ranges, const UnitListTy &Units,
Greg Claytonadb170f2016-12-13 18:25:19 +0000861 const DWARFDie &Die,
Frederic Riss2e2ed162015-02-13 23:18:34 +0000862 const DebugMapObject &DMO, CompileUnit &CU,
863 unsigned Flags) {
Jonas Devlieghere3289ee62018-08-01 13:24:39 +0000864 // LIFO work list.
865 SmallVector<WorklistItem, 4> Worklist;
866 Worklist.emplace_back(Die, Flags);
Frederic Riss2e2ed162015-02-13 23:18:34 +0000867
Jonas Devlieghere3289ee62018-08-01 13:24:39 +0000868 while (!Worklist.empty()) {
869 WorklistItem Current = Worklist.back();
870 Worklist.pop_back();
Frederic Riss2e2ed162015-02-13 23:18:34 +0000871
Jonas Devlieghere3289ee62018-08-01 13:24:39 +0000872 if (Current.IsContinuation) {
873 updateIncompleteness(Current.Die, *Current.ChildInfo, CU);
874 continue;
875 }
Frederic Riss2e2ed162015-02-13 23:18:34 +0000876
Jonas Devlieghere3289ee62018-08-01 13:24:39 +0000877 unsigned Idx = CU.getOrigUnit().getDIEIndex(Current.Die);
878 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
879
880 // At this point we are guaranteed to have a continuation marker before us
881 // in the worklist, except for the last DIE.
882 if (!Worklist.empty())
883 Worklist.back().ChildInfo = &MyInfo;
884
885 if (MyInfo.Prune)
886 continue;
887
888 // If the Keep flag is set, we are marking a required DIE's dependencies.
889 // If our target is already marked as kept, we're all set.
890 bool AlreadyKept = MyInfo.Keep;
891 if ((Current.Flags & TF_DependencyWalk) && AlreadyKept)
892 continue;
893
894 // We must not call shouldKeepDIE while called from keepDIEAndDependencies,
895 // because it would screw up the relocation finding logic.
896 if (!(Current.Flags & TF_DependencyWalk))
897 Current.Flags = shouldKeepDIE(RelocMgr, Ranges, Current.Die, DMO, CU,
898 MyInfo, Current.Flags);
899
900 // If it is a newly kept DIE mark it as well as all its dependencies as
901 // kept.
902 if (!AlreadyKept && (Current.Flags & TF_Keep)) {
903 bool UseOdr = (Current.Flags & TF_DependencyWalk)
904 ? (Current.Flags & TF_ODR)
905 : CU.hasODR();
906 keepDIEAndDependencies(RelocMgr, Ranges, Units, Current.Die, MyInfo, DMO,
907 CU, UseOdr);
908 }
909
910 // The TF_ParentWalk flag tells us that we are currently walking up
911 // the parent chain of a required DIE, and we don't want to mark all
912 // the children of the parents as kept (consider for example a
913 // DW_TAG_namespace node in the parent chain). There are however a
914 // set of DIE types for which we want to ignore that directive and still
915 // walk their children.
916 if (dieNeedsChildrenToBeMeaningful(Current.Die.getTag()))
917 Current.Flags &= ~TF_ParentWalk;
918
919 if (!Current.Die.hasChildren() || (Current.Flags & TF_ParentWalk))
920 continue;
921
922 // Add children in reverse order to the worklist to effectively process
923 // them in order.
924 for (auto Child : reverse(Current.Die.children())) {
925 // Add continuation marker before every child to calculate incompleteness
926 // after the last child is processed. We can't store this information in
927 // the same item because we might have to process other continuations
928 // first.
929 Worklist.emplace_back(Current.Die);
930 Worklist.emplace_back(Child, Current.Flags);
931 }
Frederic Riss309fcf82015-07-21 22:41:43 +0000932 }
Frederic Riss2e2ed162015-02-13 23:18:34 +0000933}
934
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000935/// Assign an abbreviation number to \p Abbrev.
Frederic Rissd0d92e72015-03-04 22:07:44 +0000936///
937/// Our DIEs get freed after every DebugMapObject has been processed,
938/// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
939/// the instances hold by the DIEs. When we encounter an abbreviation
940/// that we don't know, we create a permanent copy of it.
941void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) {
942 // Check the set for priors.
943 FoldingSetNodeID ID;
944 Abbrev.Profile(ID);
945 void *InsertToken;
946 DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
947
948 // If it's newly added.
949 if (InSet) {
950 // Assign existing abbreviation number.
951 Abbrev.setNumber(InSet->getNumber());
952 } else {
953 // Add to abbreviation list.
954 Abbreviations.push_back(
David Blaikiebe1cec02015-11-18 00:34:10 +0000955 llvm::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
Frederic Rissd0d92e72015-03-04 22:07:44 +0000956 for (const auto &Attr : Abbrev.getData())
957 Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
David Blaikiebe1cec02015-11-18 00:34:10 +0000958 AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
Frederic Rissd0d92e72015-03-04 22:07:44 +0000959 // Assign the unique abbreviation number.
960 Abbrev.setNumber(Abbreviations.size());
961 Abbreviations.back()->setNumber(Abbreviations.size());
962 }
963}
964
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000965unsigned DwarfLinker::DIECloner::cloneStringAttribute(
966 DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
967 const DWARFUnit &U, OffsetsStringPool &StringPool, AttributesInfo &Info) {
Frederic Rissd0d92e72015-03-04 22:07:44 +0000968 // Switch everything to out of line strings.
Greg Clayton1a30f4b2016-10-31 16:46:02 +0000969 const char *String = *Val.getAsCString();
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000970 auto StringEntry = StringPool.getEntry(String);
971
972 // Update attributes info.
Jonas Devlieghere61693462018-02-08 10:48:54 +0000973 if (AttrSpec.Attr == dwarf::DW_AT_name)
974 Info.Name = StringEntry;
975 else if (AttrSpec.Attr == dwarf::DW_AT_MIPS_linkage_name ||
976 AttrSpec.Attr == dwarf::DW_AT_linkage_name)
977 Info.MangledName = StringEntry;
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000978
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +0000979 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
Jonas Devlieghere61693462018-02-08 10:48:54 +0000980 DIEInteger(StringEntry.getOffset()));
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000981
Frederic Rissd0d92e72015-03-04 22:07:44 +0000982 return 4;
983}
984
Adrian Prantlafca6b42015-09-14 16:46:10 +0000985unsigned DwarfLinker::DIECloner::cloneDieReferenceAttribute(
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000986 DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000987 unsigned AttrSize, const DWARFFormValue &Val, const DebugMapObject &DMO,
988 CompileUnit &Unit) {
Frederic Riss309fcf82015-07-21 22:41:43 +0000989 const DWARFUnit &U = Unit.getOrigUnit();
Greg Clayton1a30f4b2016-10-31 16:46:02 +0000990 uint32_t Ref = *Val.getAsReference();
Frederic Rissfe782872015-03-06 23:22:53 +0000991 DIE *NewRefDie = nullptr;
992 CompileUnit *RefUnit = nullptr;
Frederic Riss309fcf82015-07-21 22:41:43 +0000993 DeclContext *Ctxt = nullptr;
Frederic Rissfe782872015-03-06 23:22:53 +0000994
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000995 DWARFDie RefDie =
Jonas Devliegherea4c60c42018-03-13 10:52:49 +0000996 resolveDIEReference(Linker, DMO, CompileUnits, Val, U, InputDIE, RefUnit);
Frederic Riss309fcf82015-07-21 22:41:43 +0000997
998 // If the referenced DIE is not found, drop the attribute.
Jonas Devliegheref521abc2018-02-27 19:24:36 +0000999 if (!RefDie || AttrSpec.Attr == dwarf::DW_AT_sibling)
Frederic Rissfe782872015-03-06 23:22:53 +00001000 return 0;
Frederic Rissfe782872015-03-06 23:22:53 +00001001
1002 unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
1003 CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
Frederic Riss309fcf82015-07-21 22:41:43 +00001004
1005 // If we already have emitted an equivalent DeclContext, just point
1006 // at it.
1007 if (isODRAttribute(AttrSpec.Attr)) {
1008 Ctxt = RefInfo.Ctxt;
1009 if (Ctxt && Ctxt->getCanonicalDIEOffset()) {
1010 DIEInteger Attr(Ctxt->getCanonicalDIEOffset());
1011 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1012 dwarf::DW_FORM_ref_addr, Attr);
Greg Clayton1a30f4b2016-10-31 16:46:02 +00001013 return U.getRefAddrByteSize();
Frederic Riss309fcf82015-07-21 22:41:43 +00001014 }
1015 }
1016
Frederic Rissfe782872015-03-06 23:22:53 +00001017 if (!RefInfo.Clone) {
1018 assert(Ref > InputDIE.getOffset());
1019 // We haven't cloned this DIE yet. Just create an empty one and
1020 // store it. It'll get really cloned when we process it.
Greg Claytonadb170f2016-12-13 18:25:19 +00001021 RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie.getTag()));
Frederic Rissfe782872015-03-06 23:22:53 +00001022 }
1023 NewRefDie = RefInfo.Clone;
1024
Frederic Riss309fcf82015-07-21 22:41:43 +00001025 if (AttrSpec.Form == dwarf::DW_FORM_ref_addr ||
1026 (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) {
Frederic Rissfe782872015-03-06 23:22:53 +00001027 // We cannot currently rely on a DIEEntry to emit ref_addr
1028 // references, because the implementation calls back to DwarfDebug
1029 // to find the unit offset. (We don't have a DwarfDebug)
1030 // FIXME: we should be able to design DIEEntry reliance on
1031 // DwarfDebug away.
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +00001032 uint64_t Attr;
Frederic Rissfe782872015-03-06 23:22:53 +00001033 if (Ref < InputDIE.getOffset()) {
1034 // We must have already cloned that DIE.
1035 uint32_t NewRefOffset =
1036 RefUnit->getStartOffset() + NewRefDie->getOffset();
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +00001037 Attr = NewRefOffset;
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001038 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1039 dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
Frederic Rissfe782872015-03-06 23:22:53 +00001040 } else {
1041 // A forward reference. Note and fixup later.
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +00001042 Attr = 0xBADDEF;
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001043 Unit.noteForwardReference(
Frederic Riss309fcf82015-07-21 22:41:43 +00001044 NewRefDie, RefUnit, Ctxt,
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001045 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1046 dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
Frederic Rissfe782872015-03-06 23:22:53 +00001047 }
Greg Clayton1a30f4b2016-10-31 16:46:02 +00001048 return U.getRefAddrByteSize();
Frederic Rissfe782872015-03-06 23:22:53 +00001049 }
1050
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001051 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1052 dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
Frederic Rissd0d92e72015-03-04 22:07:44 +00001053 return AttrSize;
1054}
1055
Adrian Prantlafca6b42015-09-14 16:46:10 +00001056unsigned DwarfLinker::DIECloner::cloneBlockAttribute(DIE &Die,
1057 AttributeSpec AttrSpec,
1058 const DWARFFormValue &Val,
1059 unsigned AttrSize) {
Duncan P. N. Exon Smith401c02a2015-08-02 20:48:47 +00001060 DIEValueList *Attr;
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +00001061 DIEValue Value;
Frederic Rissd0d92e72015-03-04 22:07:44 +00001062 DIELoc *Loc = nullptr;
1063 DIEBlock *Block = nullptr;
1064 // Just copy the block data over.
Frederic Riss3a5c1172015-03-13 18:35:39 +00001065 if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +00001066 Loc = new (DIEAlloc) DIELoc;
Adrian Prantlafca6b42015-09-14 16:46:10 +00001067 Linker.DIELocs.push_back(Loc);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001068 } else {
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +00001069 Block = new (DIEAlloc) DIEBlock;
Adrian Prantlafca6b42015-09-14 16:46:10 +00001070 Linker.DIEBlocks.push_back(Block);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001071 }
Duncan P. N. Exon Smith401c02a2015-08-02 20:48:47 +00001072 Attr = Loc ? static_cast<DIEValueList *>(Loc)
1073 : static_cast<DIEValueList *>(Block);
Duncan P. N. Exon Smith611a2f22015-05-27 22:31:41 +00001074
1075 if (Loc)
1076 Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
1077 dwarf::Form(AttrSpec.Form), Loc);
1078 else
1079 Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
1080 dwarf::Form(AttrSpec.Form), Block);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001081 ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
1082 for (auto Byte : Bytes)
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001083 Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
1084 dwarf::DW_FORM_data1, DIEInteger(Byte));
Frederic Rissd0d92e72015-03-04 22:07:44 +00001085 // FIXME: If DIEBlock and DIELoc just reuses the Size field of
1086 // the DIE class, this if could be replaced by
1087 // Attr->setSize(Bytes.size()).
Adrian Prantlafca6b42015-09-14 16:46:10 +00001088 if (Linker.Streamer) {
1089 auto *AsmPrinter = &Linker.Streamer->getAsmPrinter();
Frederic Rissd0d92e72015-03-04 22:07:44 +00001090 if (Loc)
Adrian Prantlafca6b42015-09-14 16:46:10 +00001091 Loc->ComputeSize(AsmPrinter);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001092 else
Adrian Prantlafca6b42015-09-14 16:46:10 +00001093 Block->ComputeSize(AsmPrinter);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001094 }
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001095 Die.addValue(DIEAlloc, Value);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001096 return AttrSize;
1097}
1098
Adrian Prantlafca6b42015-09-14 16:46:10 +00001099unsigned DwarfLinker::DIECloner::cloneAddressAttribute(
1100 DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1101 const CompileUnit &Unit, AttributesInfo &Info) {
Greg Clayton1a30f4b2016-10-31 16:46:02 +00001102 uint64_t Addr = *Val.getAsAddress();
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001103
Jonas Devlieghere61693462018-02-08 10:48:54 +00001104 if (LLVM_UNLIKELY(Linker.Options.Update)) {
1105 if (AttrSpec.Attr == dwarf::DW_AT_low_pc)
1106 Info.HasLowPc = true;
1107 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1108 dwarf::Form(AttrSpec.Form), DIEInteger(Addr));
1109 return Unit.getOrigUnit().getAddressByteSize();
1110 }
1111
Frederic Riss89393bb2015-03-11 18:45:52 +00001112 if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
1113 if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
1114 Die.getTag() == dwarf::DW_TAG_lexical_block)
Frederic Rissefe30162015-08-31 01:43:14 +00001115 // The low_pc of a block or inline subroutine might get
1116 // relocated because it happens to match the low_pc of the
1117 // enclosing subprogram. To prevent issues with that, always use
1118 // the low_pc from the input DIE if relocations have been applied.
Eugene Zelenko66f724e2017-11-01 21:16:06 +00001119 Addr = (Info.OrigLowPc != std::numeric_limits<uint64_t>::max()
1120 ? Info.OrigLowPc
1121 : Addr) +
Frederic Rissefe30162015-08-31 01:43:14 +00001122 Info.PCOffset;
Frederic Riss27db5192015-03-13 18:35:54 +00001123 else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1124 Addr = Unit.getLowPc();
Eugene Zelenko66f724e2017-11-01 21:16:06 +00001125 if (Addr == std::numeric_limits<uint64_t>::max())
Frederic Riss27db5192015-03-13 18:35:54 +00001126 return 0;
1127 }
Frederic Riss991f3ea2015-03-16 02:05:10 +00001128 Info.HasLowPc = true;
Frederic Riss89393bb2015-03-11 18:45:52 +00001129 } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
Frederic Riss27db5192015-03-13 18:35:54 +00001130 if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1131 if (uint64_t HighPc = Unit.getHighPc())
1132 Addr = HighPc;
1133 else
1134 return 0;
1135 } else
1136 // If we have a high_pc recorded for the input DIE, use
1137 // it. Otherwise (when no relocations where applied) just use the
1138 // one we just decoded.
1139 Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
Frederic Riss89393bb2015-03-11 18:45:52 +00001140 }
1141
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001142 Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +00001143 static_cast<dwarf::Form>(AttrSpec.Form), DIEInteger(Addr));
Frederic Riss89393bb2015-03-11 18:45:52 +00001144 return Unit.getOrigUnit().getAddressByteSize();
1145}
1146
Adrian Prantlafca6b42015-09-14 16:46:10 +00001147unsigned DwarfLinker::DIECloner::cloneScalarAttribute(
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001148 DIE &Die, const DWARFDie &InputDIE, const DebugMapObject &DMO,
1149 CompileUnit &Unit, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1150 unsigned AttrSize, AttributesInfo &Info) {
Frederic Rissd0d92e72015-03-04 22:07:44 +00001151 uint64_t Value;
Jonas Devlieghere61693462018-02-08 10:48:54 +00001152
1153 if (LLVM_UNLIKELY(Linker.Options.Update)) {
1154 if (auto OptionalValue = Val.getAsUnsignedConstant())
1155 Value = *OptionalValue;
1156 else if (auto OptionalValue = Val.getAsSignedConstant())
1157 Value = *OptionalValue;
1158 else if (auto OptionalValue = Val.getAsSectionOffset())
1159 Value = *OptionalValue;
1160 else {
1161 Linker.reportWarning(
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001162 "Unsupported scalar attribute form. Dropping attribute.", DMO,
1163 &InputDIE);
Jonas Devlieghere61693462018-02-08 10:48:54 +00001164 return 0;
1165 }
1166 if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1167 Info.IsDeclaration = true;
1168 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1169 dwarf::Form(AttrSpec.Form), DIEInteger(Value));
1170 return AttrSize;
1171 }
1172
Frederic Riss27db5192015-03-13 18:35:54 +00001173 if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
1174 Die.getTag() == dwarf::DW_TAG_compile_unit) {
1175 if (Unit.getLowPc() == -1ULL)
1176 return 0;
1177 // Dwarf >= 4 high_pc is an size, not an address.
1178 Value = Unit.getHighPc() - Unit.getLowPc();
1179 } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
Frederic Rissd0d92e72015-03-04 22:07:44 +00001180 Value = *Val.getAsSectionOffset();
1181 else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
1182 Value = *Val.getAsSignedConstant();
Frederic Rissd0d92e72015-03-04 22:07:44 +00001183 else if (auto OptionalValue = Val.getAsUnsignedConstant())
1184 Value = *OptionalValue;
1185 else {
Adrian Prantlafca6b42015-09-14 16:46:10 +00001186 Linker.reportWarning(
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001187 "Unsupported scalar attribute form. Dropping attribute.", DMO,
1188 &InputDIE);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001189 return 0;
1190 }
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001191 PatchLocation Patch =
1192 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1193 dwarf::Form(AttrSpec.Form), DIEInteger(Value));
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001194 if (AttrSpec.Attr == dwarf::DW_AT_ranges) {
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001195 Unit.noteRangeAttribute(Die, Patch);
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001196 Info.HasRanges = true;
1197 }
Frederic Riss5f69f9f2015-09-11 04:17:30 +00001198
Frederic Riss5712ee12015-03-14 15:49:07 +00001199 // A more generic way to check for location attributes would be
1200 // nice, but it's very unlikely that any other attribute needs a
1201 // location list.
1202 else if (AttrSpec.Attr == dwarf::DW_AT_location ||
1203 AttrSpec.Attr == dwarf::DW_AT_frame_base)
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001204 Unit.noteLocationAttribute(Patch, Info.PCOffset);
Frederic Riss991f3ea2015-03-16 02:05:10 +00001205 else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1206 Info.IsDeclaration = true;
Frederic Riss5712ee12015-03-14 15:49:07 +00001207
Frederic Rissd0d92e72015-03-04 22:07:44 +00001208 return AttrSize;
1209}
1210
Adrian Prantl90d56f02017-07-21 16:51:17 +00001211/// Clone \p InputDIE's attribute described by \p AttrSpec with
Frederic Rissd0d92e72015-03-04 22:07:44 +00001212/// value \p Val, and add it to \p Die.
1213/// \returns the size of the cloned attribute.
Adrian Prantlafca6b42015-09-14 16:46:10 +00001214unsigned DwarfLinker::DIECloner::cloneAttribute(
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001215 DIE &Die, const DWARFDie &InputDIE, const DebugMapObject &DMO,
1216 CompileUnit &Unit, OffsetsStringPool &StringPool, const DWARFFormValue &Val,
1217 const AttributeSpec AttrSpec, unsigned AttrSize, AttributesInfo &Info) {
Frederic Rissd0d92e72015-03-04 22:07:44 +00001218 const DWARFUnit &U = Unit.getOrigUnit();
1219
1220 switch (AttrSpec.Form) {
1221 case dwarf::DW_FORM_strp:
1222 case dwarf::DW_FORM_string:
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001223 return cloneStringAttribute(Die, AttrSpec, Val, U, StringPool, Info);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001224 case dwarf::DW_FORM_ref_addr:
1225 case dwarf::DW_FORM_ref1:
1226 case dwarf::DW_FORM_ref2:
1227 case dwarf::DW_FORM_ref4:
1228 case dwarf::DW_FORM_ref8:
Frederic Rissfe782872015-03-06 23:22:53 +00001229 return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001230 DMO, Unit);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001231 case dwarf::DW_FORM_block:
1232 case dwarf::DW_FORM_block1:
1233 case dwarf::DW_FORM_block2:
1234 case dwarf::DW_FORM_block4:
1235 case dwarf::DW_FORM_exprloc:
1236 return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize);
1237 case dwarf::DW_FORM_addr:
Frederic Riss89393bb2015-03-11 18:45:52 +00001238 return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001239 case dwarf::DW_FORM_data1:
1240 case dwarf::DW_FORM_data2:
1241 case dwarf::DW_FORM_data4:
1242 case dwarf::DW_FORM_data8:
1243 case dwarf::DW_FORM_udata:
1244 case dwarf::DW_FORM_sdata:
1245 case dwarf::DW_FORM_sec_offset:
1246 case dwarf::DW_FORM_flag:
1247 case dwarf::DW_FORM_flag_present:
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001248 return cloneScalarAttribute(Die, InputDIE, DMO, Unit, AttrSpec, Val,
1249 AttrSize, Info);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001250 default:
Adrian Prantlafca6b42015-09-14 16:46:10 +00001251 Linker.reportWarning(
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001252 "Unsupported attribute form in cloneAttribute. Dropping.", DMO,
1253 &InputDIE);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001254 }
1255
1256 return 0;
1257}
1258
Adrian Prantl90d56f02017-07-21 16:51:17 +00001259/// Apply the valid relocations found by findValidRelocs() to
Frederic Riss3c8e6192015-03-07 01:25:09 +00001260/// the buffer \p Data, taking into account that Data is at \p BaseOffset
1261/// in the debug_info section.
1262///
1263/// Like for findValidRelocs(), this function must be called with
1264/// monotonic \p BaseOffset values.
1265///
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001266/// \returns whether any reloc has been applied.
1267bool DwarfLinker::RelocationManager::applyValidRelocs(
1268 MutableArrayRef<char> Data, uint32_t BaseOffset, bool isLittleEndian) {
Aaron Ballmanb3650642015-03-07 15:16:27 +00001269 assert((NextValidReloc == 0 ||
Frederic Riss7944cd12015-03-11 18:45:57 +00001270 BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) &&
1271 "BaseOffset should only be increasing.");
Frederic Riss3c8e6192015-03-07 01:25:09 +00001272 if (NextValidReloc >= ValidRelocs.size())
1273 return false;
1274
1275 // Skip relocs that haven't been applied.
1276 while (NextValidReloc < ValidRelocs.size() &&
1277 ValidRelocs[NextValidReloc].Offset < BaseOffset)
1278 ++NextValidReloc;
1279
1280 bool Applied = false;
1281 uint64_t EndOffset = BaseOffset + Data.size();
1282 while (NextValidReloc < ValidRelocs.size() &&
1283 ValidRelocs[NextValidReloc].Offset >= BaseOffset &&
1284 ValidRelocs[NextValidReloc].Offset < EndOffset) {
1285 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
1286 assert(ValidReloc.Offset - BaseOffset < Data.size());
1287 assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size());
1288 char Buf[8];
1289 uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress;
1290 Value += ValidReloc.Addend;
1291 for (unsigned i = 0; i != ValidReloc.Size; ++i) {
1292 unsigned Index = isLittleEndian ? i : (ValidReloc.Size - i - 1);
1293 Buf[i] = uint8_t(Value >> (Index * 8));
1294 }
1295 assert(ValidReloc.Size <= sizeof(Buf));
1296 memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size);
1297 Applied = true;
1298 }
1299
1300 return Applied;
1301}
1302
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001303static bool isObjCSelector(StringRef Name) {
1304 return Name.size() > 2 && (Name[0] == '-' || Name[0] == '+') &&
1305 (Name[1] == '[');
1306}
1307
1308void DwarfLinker::DIECloner::addObjCAccelerator(CompileUnit &Unit,
1309 const DIE *Die,
1310 DwarfStringPoolEntryRef Name,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001311 OffsetsStringPool &StringPool,
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001312 bool SkipPubSection) {
1313 assert(isObjCSelector(Name.getString()) && "not an objc selector");
1314 // Objective C method or class function.
1315 // "- [Class(Category) selector :withArg ...]"
1316 StringRef ClassNameStart(Name.getString().drop_front(2));
1317 size_t FirstSpace = ClassNameStart.find(' ');
1318 if (FirstSpace == StringRef::npos)
1319 return;
1320
1321 StringRef SelectorStart(ClassNameStart.data() + FirstSpace + 1);
1322 if (!SelectorStart.size())
1323 return;
1324
1325 StringRef Selector(SelectorStart.data(), SelectorStart.size() - 1);
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001326 Unit.addNameAccelerator(Die, StringPool.getEntry(Selector), SkipPubSection);
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001327
1328 // Add an entry for the class name that points to this
1329 // method/class function.
1330 StringRef ClassName(ClassNameStart.data(), FirstSpace);
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001331 Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassName), SkipPubSection);
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001332
1333 if (ClassName[ClassName.size() - 1] == ')') {
1334 size_t OpenParens = ClassName.find('(');
1335 if (OpenParens != StringRef::npos) {
1336 StringRef ClassNameNoCategory(ClassName.data(), OpenParens);
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001337 Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassNameNoCategory),
1338 SkipPubSection);
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001339
1340 std::string MethodNameNoCategory(Name.getString().data(), OpenParens + 2);
1341 // FIXME: The missing space here may be a bug, but
1342 // dsymutil-classic also does it this way.
1343 MethodNameNoCategory.append(SelectorStart);
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001344 Unit.addNameAccelerator(Die, StringPool.getEntry(MethodNameNoCategory),
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001345 SkipPubSection);
1346 }
1347 }
1348}
1349
Frederic Riss5f69f9f2015-09-11 04:17:30 +00001350static bool
1351shouldSkipAttribute(DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
1352 uint16_t Tag, bool InDebugMap, bool SkipPC,
1353 bool InFunctionScope) {
1354 switch (AttrSpec.Attr) {
1355 default:
1356 return false;
1357 case dwarf::DW_AT_low_pc:
1358 case dwarf::DW_AT_high_pc:
1359 case dwarf::DW_AT_ranges:
1360 return SkipPC;
1361 case dwarf::DW_AT_location:
1362 case dwarf::DW_AT_frame_base:
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001363 // FIXME: for some reason dsymutil-classic keeps the location attributes
1364 // when they are of block type (i.e. not location lists). This is totally
1365 // wrong for globals where we will keep a wrong address. It is mostly
1366 // harmless for locals, but there is no point in keeping these anyway when
1367 // the function wasn't linked.
Frederic Riss5f69f9f2015-09-11 04:17:30 +00001368 return (SkipPC || (!InFunctionScope && Tag == dwarf::DW_TAG_variable &&
1369 !InDebugMap)) &&
1370 !DWARFFormValue(AttrSpec.Form).isFormClass(DWARFFormValue::FC_Block);
1371 }
1372}
1373
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001374DIE *DwarfLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001375 const DebugMapObject &DMO,
1376 CompileUnit &Unit,
1377 OffsetsStringPool &StringPool,
1378 int64_t PCOffset, uint32_t OutOffset,
1379 unsigned Flags, DIE *Die) {
Frederic Rissd0d92e72015-03-04 22:07:44 +00001380 DWARFUnit &U = Unit.getOrigUnit();
Greg Claytonadb170f2016-12-13 18:25:19 +00001381 unsigned Idx = U.getDIEIndex(InputDIE);
Frederic Rissfe782872015-03-06 23:22:53 +00001382 CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001383
1384 // Should the DIE appear in the output?
1385 if (!Unit.getInfo(Idx).Keep)
1386 return nullptr;
1387
1388 uint32_t Offset = InputDIE.getOffset();
Greg Claytonfc1fc8b2016-12-01 18:56:29 +00001389 assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE");
1390 if (!Die) {
1391 // The DIE might have been already created by a forward reference
1392 // (see cloneDieReferenceAttribute()).
David Blaikie2af4e8b2016-12-01 22:04:16 +00001393 if (!Info.Clone)
1394 Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
1395 Die = Info.Clone;
Greg Claytonfc1fc8b2016-12-01 18:56:29 +00001396 }
1397
Frederic Rissfe782872015-03-06 23:22:53 +00001398 assert(Die->getTag() == InputDIE.getTag());
Frederic Rissd0d92e72015-03-04 22:07:44 +00001399 Die->setOffset(OutOffset);
Jonas Devlieghere75475a82017-08-31 20:22:31 +00001400 if ((Unit.hasODR() || Unit.isClangModule()) && !Info.Incomplete &&
Adrian Prantl43df5822015-09-23 17:35:52 +00001401 Die->getTag() != dwarf::DW_TAG_namespace && Info.Ctxt &&
Frederic Riss309fcf82015-07-21 22:41:43 +00001402 Info.Ctxt != Unit.getInfo(Info.ParentIdx).Ctxt &&
1403 !Info.Ctxt->getCanonicalDIEOffset()) {
1404 // We are about to emit a DIE that is the root of its own valid
1405 // DeclContext tree. Make the current offset the canonical offset
1406 // for this context.
1407 Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset());
1408 }
Frederic Rissd0d92e72015-03-04 22:07:44 +00001409
1410 // Extract and clone every attribute.
Paul Robinson502fd5d2017-06-29 16:52:08 +00001411 DWARFDataExtractor Data = U.getDebugInfoExtractor();
Adrian Prantld2136b32015-09-22 22:20:50 +00001412 // Point to the next DIE (generally there is always at least a NULL
1413 // entry after the current one). If this is a lone
1414 // DW_TAG_compile_unit without any children, point to the next unit.
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001415 uint32_t NextOffset = (Idx + 1 < U.getNumDIEs())
1416 ? U.getDIEAtIndex(Idx + 1).getOffset()
1417 : U.getNextUnitOffset();
Frederic Riss89393bb2015-03-11 18:45:52 +00001418 AttributesInfo AttrInfo;
Frederic Riss3c8e6192015-03-07 01:25:09 +00001419
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001420 // We could copy the data only if we need to apply a relocation to it. After
1421 // testing, it seems there is no performance downside to doing the copy
1422 // unconditionally, and it makes the code simpler.
Frederic Riss3c8e6192015-03-07 01:25:09 +00001423 SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
Paul Robinson502fd5d2017-06-29 16:52:08 +00001424 Data =
1425 DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
Frederic Riss3c8e6192015-03-07 01:25:09 +00001426 // Modify the copy with relocated addresses.
Adrian Prantl0a0a9602015-09-11 23:45:30 +00001427 if (RelocMgr.applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) {
Frederic Riss89393bb2015-03-11 18:45:52 +00001428 // If we applied relocations, we store the value of high_pc that was
1429 // potentially stored in the input DIE. If high_pc is an address
1430 // (Dwarf version == 2), then it might have been relocated to a
1431 // totally unrelated value (because the end address in the object
1432 // file might be start address of another function which got moved
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001433 // independently by the linker). The computation of the actual
Frederic Riss89393bb2015-03-11 18:45:52 +00001434 // high_pc value is done in cloneAddressAttribute().
Adrian Prantl7f359b22017-09-30 00:22:24 +00001435 AttrInfo.OrigHighPc =
1436 dwarf::toAddress(InputDIE.find(dwarf::DW_AT_high_pc), 0);
Frederic Rissefe30162015-08-31 01:43:14 +00001437 // Also store the low_pc. It might get relocated in an
1438 // inline_subprogram that happens at the beginning of its
1439 // inlining function.
Eugene Zelenko66f724e2017-11-01 21:16:06 +00001440 AttrInfo.OrigLowPc = dwarf::toAddress(InputDIE.find(dwarf::DW_AT_low_pc),
1441 std::numeric_limits<uint64_t>::max());
Frederic Riss89393bb2015-03-11 18:45:52 +00001442 }
Frederic Riss3c8e6192015-03-07 01:25:09 +00001443
1444 // Reset the Offset to 0 as we will be working on the local copy of
1445 // the data.
1446 Offset = 0;
1447
Frederic Rissd0d92e72015-03-04 22:07:44 +00001448 const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
1449 Offset += getULEB128Size(Abbrev->getCode());
1450
Frederic Riss89393bb2015-03-11 18:45:52 +00001451 // We are entering a subprogram. Get and propagate the PCOffset.
1452 if (Die->getTag() == dwarf::DW_TAG_subprogram)
1453 PCOffset = Info.AddrAdjust;
1454 AttrInfo.PCOffset = PCOffset;
1455
Frederic Riss5f69f9f2015-09-11 04:17:30 +00001456 if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) {
1457 Flags |= TF_InFunctionScope;
Jonas Devlieghere61693462018-02-08 10:48:54 +00001458 if (!Info.InDebugMap && LLVM_LIKELY(!Options.Update))
Frederic Riss5f69f9f2015-09-11 04:17:30 +00001459 Flags |= TF_SkipPC;
1460 }
1461
1462 bool Copied = false;
Frederic Rissd0d92e72015-03-04 22:07:44 +00001463 for (const auto &AttrSpec : Abbrev->attributes()) {
Jonas Devlieghere61693462018-02-08 10:48:54 +00001464 if (LLVM_LIKELY(!Options.Update) &&
1465 shouldSkipAttribute(AttrSpec, Die->getTag(), Info.InDebugMap,
Frederic Riss5f69f9f2015-09-11 04:17:30 +00001466 Flags & TF_SkipPC, Flags & TF_InFunctionScope)) {
Paul Robinsonfd770ea2017-06-26 18:43:01 +00001467 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
1468 U.getFormParams());
Frederic Riss5f69f9f2015-09-11 04:17:30 +00001469 // FIXME: dsymutil-classic keeps the old abbreviation around
1470 // even if it's not used. We can remove this (and the copyAbbrev
1471 // helper) as soon as bit-for-bit compatibility is not a goal anymore.
1472 if (!Copied) {
1473 copyAbbrev(*InputDIE.getAbbreviationDeclarationPtr(), Unit.hasODR());
1474 Copied = true;
1475 }
1476 continue;
1477 }
1478
Frederic Rissd0d92e72015-03-04 22:07:44 +00001479 DWARFFormValue Val(AttrSpec.Form);
1480 uint32_t AttrSize = Offset;
Paul Robinsone9d757c2017-11-07 19:57:12 +00001481 Val.extractValue(Data, &Offset, U.getFormParams(), &U);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001482 AttrSize = Offset - AttrSize;
1483
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001484 OutOffset += cloneAttribute(*Die, InputDIE, DMO, Unit, StringPool, Val,
1485 AttrSpec, AttrSize, AttrInfo);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001486 }
1487
Frederic Riss991f3ea2015-03-16 02:05:10 +00001488 // Look for accelerator entries.
1489 uint16_t Tag = InputDIE.getTag();
1490 // FIXME: This is slightly wrong. An inline_subroutine without a
1491 // low_pc, but with AT_ranges might be interesting to get into the
1492 // accelerator tables too. For now stick with dsymutil's behavior.
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001493 if ((Info.InDebugMap || AttrInfo.HasLowPc || AttrInfo.HasRanges) &&
Frederic Riss991f3ea2015-03-16 02:05:10 +00001494 Tag != dwarf::DW_TAG_compile_unit &&
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001495 getDIENames(InputDIE, AttrInfo, StringPool,
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001496 Tag != dwarf::DW_TAG_inlined_subroutine)) {
Frederic Riss991f3ea2015-03-16 02:05:10 +00001497 if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
1498 Unit.addNameAccelerator(Die, AttrInfo.MangledName,
Frederic Riss991f3ea2015-03-16 02:05:10 +00001499 Tag == dwarf::DW_TAG_inlined_subroutine);
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001500 if (AttrInfo.Name) {
1501 if (AttrInfo.NameWithoutTemplate)
1502 Unit.addNameAccelerator(Die, AttrInfo.NameWithoutTemplate,
1503 /* SkipPubSection */ true);
1504 Unit.addNameAccelerator(Die, AttrInfo.Name,
Frederic Riss991f3ea2015-03-16 02:05:10 +00001505 Tag == dwarf::DW_TAG_inlined_subroutine);
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001506 }
1507 if (AttrInfo.Name && isObjCSelector(AttrInfo.Name.getString()))
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001508 addObjCAccelerator(Unit, Die, AttrInfo.Name, StringPool,
1509 /* SkipPubSection =*/true);
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001510
1511 } else if (Tag == dwarf::DW_TAG_namespace) {
1512 if (!AttrInfo.Name)
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001513 AttrInfo.Name = StringPool.getEntry("(anonymous namespace)");
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001514 Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
Frederic Riss991f3ea2015-03-16 02:05:10 +00001515 } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001516 getDIENames(InputDIE, AttrInfo, StringPool) && AttrInfo.Name &&
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001517 AttrInfo.Name.getString()[0]) {
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001518 uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, DMO);
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001519 uint64_t RuntimeLang =
1520 dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class))
1521 .getValueOr(0);
1522 bool ObjCClassIsImplementation =
1523 (RuntimeLang == dwarf::DW_LANG_ObjC ||
1524 RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
1525 dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type))
1526 .getValueOr(0);
1527 Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
1528 Hash);
Frederic Riss991f3ea2015-03-16 02:05:10 +00001529 }
1530
Adrian Prantlb29f9e52015-11-10 21:31:05 +00001531 // Determine whether there are any children that we want to keep.
1532 bool HasChildren = false;
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001533 for (auto Child : InputDIE.children()) {
Adrian Prantlb29f9e52015-11-10 21:31:05 +00001534 unsigned Idx = U.getDIEIndex(Child);
1535 if (Unit.getInfo(Idx).Keep) {
1536 HasChildren = true;
1537 break;
1538 }
1539 }
1540
Duncan P. N. Exon Smith611a2f22015-05-27 22:31:41 +00001541 DIEAbbrev NewAbbrev = Die->generateAbbrev();
Adrian Prantlb29f9e52015-11-10 21:31:05 +00001542 if (HasChildren)
Frederic Rissd0d92e72015-03-04 22:07:44 +00001543 NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
1544 // Assign a permanent abbrev number
Adrian Prantlafca6b42015-09-14 16:46:10 +00001545 Linker.AssignAbbrev(NewAbbrev);
Duncan P. N. Exon Smith611a2f22015-05-27 22:31:41 +00001546 Die->setAbbrevNumber(NewAbbrev.getNumber());
Frederic Rissd0d92e72015-03-04 22:07:44 +00001547
1548 // Add the size of the abbreviation number to the output offset.
1549 OutOffset += getULEB128Size(Die->getAbbrevNumber());
1550
Adrian Prantlb29f9e52015-11-10 21:31:05 +00001551 if (!HasChildren) {
Frederic Rissd0d92e72015-03-04 22:07:44 +00001552 // Update our size.
1553 Die->setSize(OutOffset - Die->getOffset());
1554 return Die;
1555 }
1556
1557 // Recursively clone children.
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001558 for (auto Child : InputDIE.children()) {
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001559 if (DIE *Clone = cloneDIE(Child, DMO, Unit, StringPool, PCOffset, OutOffset,
1560 Flags)) {
Duncan P. N. Exon Smith2da14842015-06-25 23:52:10 +00001561 Die->addChild(Clone);
Frederic Rissd0d92e72015-03-04 22:07:44 +00001562 OutOffset = Clone->getOffset() + Clone->getSize();
1563 }
1564 }
1565
1566 // Account for the end of children marker.
1567 OutOffset += sizeof(int8_t);
1568 // Update our size.
1569 Die->setSize(OutOffset - Die->getOffset());
1570 return Die;
1571}
1572
Adrian Prantl90d56f02017-07-21 16:51:17 +00001573/// Patch the input object file relevant debug_ranges entries
Frederic Risse0315012015-03-13 23:30:31 +00001574/// and emit them in the output file. Update the relevant attributes
1575/// to point at the new entries.
1576void DwarfLinker::patchRangesForUnit(const CompileUnit &Unit,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001577 DWARFContext &OrigDwarf,
1578 const DebugMapObject &DMO) const {
Wolfgang Piebdaecf942018-10-31 01:12:58 +00001579 DWARFDebugRangeList RangeList;
Frederic Risse0315012015-03-13 23:30:31 +00001580 const auto &FunctionRanges = Unit.getFunctionRanges();
1581 unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
Rafael Espindolaee809ac2017-07-19 22:27:28 +00001582 DWARFDataExtractor RangeExtractor(OrigDwarf.getDWARFObj(),
1583 OrigDwarf.getDWARFObj().getRangeSection(),
Paul Robinson502fd5d2017-06-29 16:52:08 +00001584 OrigDwarf.isLittleEndian(), AddressSize);
Frederic Risse0315012015-03-13 23:30:31 +00001585 auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
1586 DWARFUnit &OrigUnit = Unit.getOrigUnit();
Greg Claytonadb170f2016-12-13 18:25:19 +00001587 auto OrigUnitDie = OrigUnit.getUnitDIE(false);
Adrian Prantl7f359b22017-09-30 00:22:24 +00001588 uint64_t OrigLowPc =
1589 dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc), -1ULL);
Frederic Risse0315012015-03-13 23:30:31 +00001590 // Ranges addresses are based on the unit's low_pc. Compute the
Sanjay Patel322ee9e2015-12-07 19:21:39 +00001591 // offset we need to apply to adapt to the new unit's low_pc.
Frederic Risse0315012015-03-13 23:30:31 +00001592 int64_t UnitPcOffset = 0;
1593 if (OrigLowPc != -1ULL)
1594 UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
1595
1596 for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +00001597 uint32_t Offset = RangeAttribute.get();
1598 RangeAttribute.set(Streamer->getRangesSectionSize());
Wolfgang Piebdaecf942018-10-31 01:12:58 +00001599 if (Error E = RangeList.extract(RangeExtractor, &Offset)) {
Wolfgang Pieb5a4174c2018-06-20 22:56:37 +00001600 llvm::consumeError(std::move(E));
1601 reportWarning("invalid range list ignored.", DMO);
1602 RangeList.clear();
1603 }
Frederic Risse0315012015-03-13 23:30:31 +00001604 const auto &Entries = RangeList.getEntries();
Wolfgang Piebdaecf942018-10-31 01:12:58 +00001605 if (!Entries.empty()) {
1606 const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
1607
Frederic Risse0315012015-03-13 23:30:31 +00001608 if (CurrRange == InvalidRange ||
Wolfgang Piebdaecf942018-10-31 01:12:58 +00001609 First.StartAddress + OrigLowPc < CurrRange.start() ||
1610 First.StartAddress + OrigLowPc >= CurrRange.stop()) {
1611 CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
Frederic Riss9be58042015-08-31 05:09:32 +00001612 if (CurrRange == InvalidRange ||
Wolfgang Piebdaecf942018-10-31 01:12:58 +00001613 CurrRange.start() > First.StartAddress + OrigLowPc) {
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001614 reportWarning("no mapping for range.", DMO);
Frederic Riss9be58042015-08-31 05:09:32 +00001615 continue;
1616 }
Frederic Risse0315012015-03-13 23:30:31 +00001617 }
1618 }
1619
Wolfgang Piebdaecf942018-10-31 01:12:58 +00001620 Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries,
Frederic Risse0315012015-03-13 23:30:31 +00001621 AddressSize);
1622 }
1623}
1624
Adrian Prantl90d56f02017-07-21 16:51:17 +00001625/// Generate the debug_aranges entries for \p Unit and if the
Frederic Riss9786d3d2015-03-14 03:46:51 +00001626/// unit has a DW_AT_ranges attribute, also emit the debug_ranges
1627/// contribution for this attribute.
Frederic Risse0315012015-03-13 23:30:31 +00001628/// FIXME: this could actually be done right in patchRangesForUnit,
1629/// but for the sake of initial bit-for-bit compatibility with legacy
1630/// dsymutil, we have to do it in a delayed pass.
1631void DwarfLinker::generateUnitRanges(CompileUnit &Unit) const {
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +00001632 auto Attr = Unit.getUnitRangesAttribute();
Frederic Riss9786d3d2015-03-14 03:46:51 +00001633 if (Attr)
Duncan P. N. Exon Smith09fe4bf2015-05-27 22:14:58 +00001634 Attr->set(Streamer->getRangesSectionSize());
1635 Streamer->emitUnitRangesEntries(Unit, static_cast<bool>(Attr));
Frederic Risse0315012015-03-13 23:30:31 +00001636}
1637
Adrian Prantl90d56f02017-07-21 16:51:17 +00001638/// Insert the new line info sequence \p Seq into the current
Frederic Risse6412a62015-03-15 20:45:43 +00001639/// set of already linked line info \p Rows.
1640static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
1641 std::vector<DWARFDebugLine::Row> &Rows) {
1642 if (Seq.empty())
1643 return;
1644
1645 if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
1646 Rows.insert(Rows.end(), Seq.begin(), Seq.end());
1647 Seq.clear();
1648 return;
1649 }
1650
1651 auto InsertPoint = std::lower_bound(
1652 Rows.begin(), Rows.end(), Seq.front(),
1653 [](const DWARFDebugLine::Row &LHS, const DWARFDebugLine::Row &RHS) {
1654 return LHS.Address < RHS.Address;
1655 });
1656
1657 // FIXME: this only removes the unneeded end_sequence if the
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001658 // sequences have been inserted in order. Using a global sort like
Frederic Risse6412a62015-03-15 20:45:43 +00001659 // described in patchLineTableForUnit() and delaying the end_sequene
1660 // elimination to emitLineTableForUnit() we can get rid of all of them.
1661 if (InsertPoint != Rows.end() &&
1662 InsertPoint->Address == Seq.front().Address && InsertPoint->EndSequence) {
1663 *InsertPoint = Seq.front();
1664 Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
1665 } else {
1666 Rows.insert(InsertPoint, Seq.begin(), Seq.end());
1667 }
1668
1669 Seq.clear();
1670}
1671
Duncan P. N. Exon Smithee46b792015-06-25 21:42:46 +00001672static void patchStmtList(DIE &Die, DIEInteger Offset) {
1673 for (auto &V : Die.values())
1674 if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
Duncan P. N. Exon Smith73e3fb62015-06-25 23:46:41 +00001675 V = DIEValue(V.getAttribute(), V.getForm(), Offset);
Duncan P. N. Exon Smithee46b792015-06-25 21:42:46 +00001676 return;
1677 }
1678
1679 llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
1680}
1681
Adrian Prantl90d56f02017-07-21 16:51:17 +00001682/// Extract the line table for \p Unit from \p OrigDwarf, and
Frederic Risse6412a62015-03-15 20:45:43 +00001683/// recreate a relocated version of these for the address ranges that
1684/// are present in the binary.
1685void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001686 DWARFContext &OrigDwarf,
1687 RangesTy &Ranges,
1688 const DebugMapObject &DMO) {
Greg Claytonadb170f2016-12-13 18:25:19 +00001689 DWARFDie CUDie = Unit.getOrigUnit().getUnitDIE();
Greg Claytonbeabb632017-01-13 21:08:18 +00001690 auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
Greg Claytonc64f9192016-12-14 22:38:08 +00001691 if (!StmtList)
Frederic Risse6412a62015-03-15 20:45:43 +00001692 return;
1693
1694 // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
Duncan P. N. Exon Smithee46b792015-06-25 21:42:46 +00001695 if (auto *OutputDIE = Unit.getOutputUnitDIE())
1696 patchStmtList(*OutputDIE, DIEInteger(Streamer->getLineSectionSize()));
Frederic Risse6412a62015-03-15 20:45:43 +00001697
1698 // Parse the original line info for the unit.
1699 DWARFDebugLine::LineTable LineTable;
Greg Claytonc64f9192016-12-14 22:38:08 +00001700 uint32_t StmtOffset = *StmtList;
Rafael Espindolaee809ac2017-07-19 22:27:28 +00001701 DWARFDataExtractor LineExtractor(
1702 OrigDwarf.getDWARFObj(), OrigDwarf.getDWARFObj().getLineSection(),
1703 OrigDwarf.isLittleEndian(), Unit.getOrigUnit().getAddressByteSize());
Jonas Devlieghere20767d12019-01-07 23:27:25 +00001704 if (Options.Translator)
1705 return Streamer->translateLineTable(LineExtractor, StmtOffset, Options);
James Hendersonb8266312018-05-10 10:51:33 +00001706
1707 Error Err = LineTable.parse(LineExtractor, &StmtOffset, OrigDwarf,
Victor Leschuk271b93a2018-08-23 12:43:33 +00001708 &Unit.getOrigUnit(), DWARFContext::dumpWarning);
1709 DWARFContext::dumpWarning(std::move(Err));
Frederic Risse6412a62015-03-15 20:45:43 +00001710
1711 // This vector is the output line table.
1712 std::vector<DWARFDebugLine::Row> NewRows;
1713 NewRows.reserve(LineTable.Rows.size());
1714
1715 // Current sequence of rows being extracted, before being inserted
1716 // in NewRows.
1717 std::vector<DWARFDebugLine::Row> Seq;
1718 const auto &FunctionRanges = Unit.getFunctionRanges();
1719 auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
1720
1721 // FIXME: This logic is meant to generate exactly the same output as
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001722 // Darwin's classic dsymutil. There is a nicer way to implement this
Frederic Risse6412a62015-03-15 20:45:43 +00001723 // by simply putting all the relocated line info in NewRows and simply
1724 // sorting NewRows before passing it to emitLineTableForUnit. This
1725 // should be correct as sequences for a function should stay
1726 // together in the sorted output. There are a few corner cases that
1727 // look suspicious though, and that required to implement the logic
1728 // this way. Revisit that once initial validation is finished.
1729
1730 // Iterate over the object file line info and extract the sequences
1731 // that correspond to linked functions.
1732 for (auto &Row : LineTable.Rows) {
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001733 // Check whether we stepped out of the range. The range is
Frederic Risse6412a62015-03-15 20:45:43 +00001734 // half-open, but consider accept the end address of the range if
1735 // it is marked as end_sequence in the input (because in that
1736 // case, the relocation offset is accurate and that entry won't
1737 // serve as the start of another function).
1738 if (CurrRange == InvalidRange || Row.Address < CurrRange.start() ||
1739 Row.Address > CurrRange.stop() ||
1740 (Row.Address == CurrRange.stop() && !Row.EndSequence)) {
1741 // We just stepped out of a known range. Insert a end_sequence
1742 // corresponding to the end of the range.
1743 uint64_t StopAddress = CurrRange != InvalidRange
1744 ? CurrRange.stop() + CurrRange.value()
1745 : -1ULL;
1746 CurrRange = FunctionRanges.find(Row.Address);
1747 bool CurrRangeValid =
1748 CurrRange != InvalidRange && CurrRange.start() <= Row.Address;
1749 if (!CurrRangeValid) {
1750 CurrRange = InvalidRange;
1751 if (StopAddress != -1ULL) {
1752 // Try harder by looking in the DebugMapObject function
1753 // ranges map. There are corner cases where this finds a
1754 // valid entry. It's unclear if this is right or wrong, but
1755 // for now do as dsymutil.
1756 // FIXME: Understand exactly what cases this addresses and
1757 // potentially remove it along with the Ranges map.
1758 auto Range = Ranges.lower_bound(Row.Address);
1759 if (Range != Ranges.begin() && Range != Ranges.end())
1760 --Range;
1761
1762 if (Range != Ranges.end() && Range->first <= Row.Address &&
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001763 Range->second.HighPC >= Row.Address) {
1764 StopAddress = Row.Address + Range->second.Offset;
Frederic Risse6412a62015-03-15 20:45:43 +00001765 }
1766 }
1767 }
1768 if (StopAddress != -1ULL && !Seq.empty()) {
1769 // Insert end sequence row with the computed end address, but
1770 // the same line as the previous one.
Yaron Kerenccc37052015-08-10 16:15:51 +00001771 auto NextLine = Seq.back();
Yaron Keren99b2ac62015-08-10 18:27:51 +00001772 NextLine.Address = StopAddress;
1773 NextLine.EndSequence = 1;
1774 NextLine.PrologueEnd = 0;
1775 NextLine.BasicBlock = 0;
1776 NextLine.EpilogueBegin = 0;
Yaron Kerenaf1c9b32015-08-10 18:03:35 +00001777 Seq.push_back(NextLine);
Frederic Risse6412a62015-03-15 20:45:43 +00001778 insertLineSequence(Seq, NewRows);
1779 }
1780
1781 if (!CurrRangeValid)
1782 continue;
1783 }
1784
1785 // Ignore empty sequences.
1786 if (Row.EndSequence && Seq.empty())
1787 continue;
1788
1789 // Relocate row address and add it to the current sequence.
1790 Row.Address += CurrRange.value();
1791 Seq.emplace_back(Row);
1792
1793 if (Row.EndSequence)
1794 insertLineSequence(Seq, NewRows);
1795 }
1796
1797 // Finished extracting, now emit the line tables.
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00001798 // FIXME: LLVM hard-codes its prologue values. We just copy the
Frederic Risse6412a62015-03-15 20:45:43 +00001799 // prologue over and that works because we act as both producer and
1800 // consumer. It would be nicer to have a real configurable line
1801 // table emitter.
Jonas Devliegheree75cd382017-12-12 11:32:21 +00001802 if (LineTable.Prologue.getVersion() < 2 ||
1803 LineTable.Prologue.getVersion() > 5 ||
Frederic Risse6412a62015-03-15 20:45:43 +00001804 LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
Frederic Riss54c40cf2015-08-07 15:14:13 +00001805 LineTable.Prologue.OpcodeBase > 13)
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001806 reportWarning("line table parameters mismatch. Cannot emit.", DMO);
Frederic Riss54c40cf2015-08-07 15:14:13 +00001807 else {
Jonas Devliegheree75cd382017-12-12 11:32:21 +00001808 uint32_t PrologueEnd = *StmtList + 10 + LineTable.Prologue.PrologueLength;
Jonas Devlieghere217ad732018-02-01 10:19:56 +00001809 // DWARF v5 has an extra 2 bytes of information before the header_length
Jonas Devliegheree75cd382017-12-12 11:32:21 +00001810 // field.
1811 if (LineTable.Prologue.getVersion() == 5)
1812 PrologueEnd += 2;
Rafael Espindolaee809ac2017-07-19 22:27:28 +00001813 StringRef LineData = OrigDwarf.getDWARFObj().getLineSection().Data;
Frederic Riss54c40cf2015-08-07 15:14:13 +00001814 MCDwarfLineTableParams Params;
1815 Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
1816 Params.DWARF2LineBase = LineTable.Prologue.LineBase;
1817 Params.DWARF2LineRange = LineTable.Prologue.LineRange;
1818 Streamer->emitLineTableForUnit(Params,
Greg Claytonc64f9192016-12-14 22:38:08 +00001819 LineData.slice(*StmtList + 4, PrologueEnd),
Frederic Risse6412a62015-03-15 20:45:43 +00001820 LineTable.Prologue.MinInstLength, NewRows,
1821 Unit.getOrigUnit().getAddressByteSize());
Frederic Riss54c40cf2015-08-07 15:14:13 +00001822 }
Frederic Risse6412a62015-03-15 20:45:43 +00001823}
1824
Frederic Riss991f3ea2015-03-16 02:05:10 +00001825void DwarfLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00001826 switch (Options.TheAccelTableKind) {
1827 case AccelTableKind::Apple:
1828 emitAppleAcceleratorEntriesForUnit(Unit);
1829 break;
1830 case AccelTableKind::Dwarf:
1831 emitDwarfAcceleratorEntriesForUnit(Unit);
1832 break;
1833 case AccelTableKind::Default:
1834 llvm_unreachable("The default must be updated to a concrete value.");
1835 break;
1836 }
1837}
1838
1839void DwarfLinker::emitAppleAcceleratorEntriesForUnit(CompileUnit &Unit) {
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001840 // Add namespaces.
1841 for (const auto &Namespace : Unit.getNamespaces())
1842 AppleNamespaces.addName(Namespace.Name,
1843 Namespace.Die->getOffset() + Unit.getStartOffset());
1844
1845 /// Add names.
1846 if (!Options.Minimize)
1847 Streamer->emitPubNamesForUnit(Unit);
1848 for (const auto &Pubname : Unit.getPubnames())
1849 AppleNames.addName(Pubname.Name,
1850 Pubname.Die->getOffset() + Unit.getStartOffset());
1851
1852 /// Add types.
1853 if (!Options.Minimize)
1854 Streamer->emitPubTypesForUnit(Unit);
1855 for (const auto &Pubtype : Unit.getPubtypes())
1856 AppleTypes.addName(
1857 Pubtype.Name, Pubtype.Die->getOffset() + Unit.getStartOffset(),
1858 Pubtype.Die->getTag(),
1859 Pubtype.ObjcClassImplementation ? dwarf::DW_FLAG_type_implementation
1860 : 0,
1861 Pubtype.QualifiedNameHash);
1862
1863 /// Add ObjC names.
1864 for (const auto &ObjC : Unit.getObjC())
1865 AppleObjc.addName(ObjC.Name, ObjC.Die->getOffset() + Unit.getStartOffset());
Frederic Riss991f3ea2015-03-16 02:05:10 +00001866}
1867
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00001868void DwarfLinker::emitDwarfAcceleratorEntriesForUnit(CompileUnit &Unit) {
1869 for (const auto &Namespace : Unit.getNamespaces())
1870 DebugNames.addName(Namespace.Name, Namespace.Die->getOffset(),
1871 Namespace.Die->getTag(), Unit.getUniqueID());
1872 for (const auto &Pubname : Unit.getPubnames())
1873 DebugNames.addName(Pubname.Name, Pubname.Die->getOffset(),
1874 Pubname.Die->getTag(), Unit.getUniqueID());
1875 for (const auto &Pubtype : Unit.getPubtypes())
1876 DebugNames.addName(Pubtype.Name, Pubtype.Die->getOffset(),
1877 Pubtype.Die->getTag(), Unit.getUniqueID());
1878}
1879
Adrian Prantl90d56f02017-07-21 16:51:17 +00001880/// Read the frame info stored in the object, and emit the
Frederic Riss9ccf07f2015-06-05 23:06:11 +00001881/// patched frame descriptions for the linked binary.
1882///
1883/// This is actually pretty easy as the data of the CIEs and FDEs can
1884/// be considered as black boxes and moved as is. The only thing to do
1885/// is to patch the addresses in the headers.
1886void DwarfLinker::patchFrameInfoForObject(const DebugMapObject &DMO,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001887 RangesTy &Ranges,
Frederic Riss9ccf07f2015-06-05 23:06:11 +00001888 DWARFContext &OrigDwarf,
1889 unsigned AddrSize) {
Rafael Espindolaee809ac2017-07-19 22:27:28 +00001890 StringRef FrameData = OrigDwarf.getDWARFObj().getDebugFrameSection();
Frederic Riss9ccf07f2015-06-05 23:06:11 +00001891 if (FrameData.empty())
1892 return;
1893
1894 DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
1895 uint32_t InputOffset = 0;
1896
1897 // Store the data of the CIEs defined in this object, keyed by their
1898 // offsets.
1899 DenseMap<uint32_t, StringRef> LocalCIES;
1900
1901 while (Data.isValidOffset(InputOffset)) {
1902 uint32_t EntryOffset = InputOffset;
1903 uint32_t InitialLength = Data.getU32(&InputOffset);
1904 if (InitialLength == 0xFFFFFFFF)
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001905 return reportWarning("Dwarf64 bits no supported", DMO);
Frederic Riss9ccf07f2015-06-05 23:06:11 +00001906
1907 uint32_t CIEId = Data.getU32(&InputOffset);
1908 if (CIEId == 0xFFFFFFFF) {
1909 // This is a CIE, store it.
1910 StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
1911 LocalCIES[EntryOffset] = CIEData;
1912 // The -4 is to account for the CIEId we just read.
1913 InputOffset += InitialLength - 4;
1914 continue;
1915 }
1916
1917 uint32_t Loc = Data.getUnsigned(&InputOffset, AddrSize);
1918
1919 // Some compilers seem to emit frame info that doesn't start at
1920 // the function entry point, thus we can't just lookup the address
1921 // in the debug map. Use the linker's range map to see if the FDE
1922 // describes something that we can relocate.
1923 auto Range = Ranges.upper_bound(Loc);
1924 if (Range != Ranges.begin())
1925 --Range;
1926 if (Range == Ranges.end() || Range->first > Loc ||
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001927 Range->second.HighPC <= Loc) {
Frederic Riss9ccf07f2015-06-05 23:06:11 +00001928 // The +4 is to account for the size of the InitialLength field itself.
1929 InputOffset = EntryOffset + InitialLength + 4;
1930 continue;
1931 }
1932
1933 // This is an FDE, and we have a mapping.
1934 // Have we already emitted a corresponding CIE?
1935 StringRef CIEData = LocalCIES[CIEId];
1936 if (CIEData.empty())
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001937 return reportWarning("Inconsistent debug_frame content. Dropping.", DMO);
Frederic Riss9ccf07f2015-06-05 23:06:11 +00001938
1939 // Look if we already emitted a CIE that corresponds to the
1940 // referenced one (the CIE data is the key of that lookup).
1941 auto IteratorInserted = EmittedCIEs.insert(
1942 std::make_pair(CIEData, Streamer->getFrameSectionSize()));
1943 // If there is no CIE yet for this ID, emit it.
1944 if (IteratorInserted.second ||
1945 // FIXME: dsymutil-classic only caches the last used CIE for
1946 // reuse. Mimic that behavior for now. Just removing that
1947 // second half of the condition and the LastCIEOffset variable
1948 // makes the code DTRT.
1949 LastCIEOffset != IteratorInserted.first->getValue()) {
1950 LastCIEOffset = Streamer->getFrameSectionSize();
1951 IteratorInserted.first->getValue() = LastCIEOffset;
1952 Streamer->emitCIE(CIEData);
1953 }
1954
1955 // Emit the FDE with updated address and CIE pointer.
1956 // (4 + AddrSize) is the size of the CIEId + initial_location
1957 // fields that will get reconstructed by emitFDE().
1958 unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
1959 Streamer->emitFDE(IteratorInserted.first->getValue(), AddrSize,
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001960 Loc + Range->second.Offset,
Frederic Riss9ccf07f2015-06-05 23:06:11 +00001961 FrameData.substr(InputOffset, FDERemainingBytes));
1962 InputOffset += FDERemainingBytes;
1963 }
1964}
1965
Adrian Prantlafca6b42015-09-14 16:46:10 +00001966void DwarfLinker::DIECloner::copyAbbrev(
1967 const DWARFAbbreviationDeclaration &Abbrev, bool hasODR) {
Frederic Riss5f69f9f2015-09-11 04:17:30 +00001968 DIEAbbrev Copy(dwarf::Tag(Abbrev.getTag()),
1969 dwarf::Form(Abbrev.hasChildren()));
1970
1971 for (const auto &Attr : Abbrev.attributes()) {
1972 uint16_t Form = Attr.Form;
1973 if (hasODR && isODRAttribute(Attr.Attr))
1974 Form = dwarf::DW_FORM_ref_addr;
1975 Copy.AddAttribute(dwarf::Attribute(Attr.Attr), dwarf::Form(Form));
1976 }
1977
Adrian Prantlafca6b42015-09-14 16:46:10 +00001978 Linker.AssignAbbrev(Copy);
Frederic Riss5f69f9f2015-09-11 04:17:30 +00001979}
1980
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00001981uint32_t DwarfLinker::DIECloner::hashFullyQualifiedName(
1982 DWARFDie DIE, CompileUnit &U, const DebugMapObject &DMO, int RecurseDepth) {
Jonas Devliegherea9aab592018-01-29 14:52:50 +00001983 const char *Name = nullptr;
1984 DWARFUnit *OrigUnit = &U.getOrigUnit();
1985 CompileUnit *CU = &U;
1986 Optional<DWARFFormValue> Ref;
1987
1988 while (1) {
1989 if (const char *CurrentName = DIE.getName(DINameKind::ShortName))
1990 Name = CurrentName;
1991
1992 if (!(Ref = DIE.find(dwarf::DW_AT_specification)) &&
1993 !(Ref = DIE.find(dwarf::DW_AT_abstract_origin)))
1994 break;
1995
1996 if (!Ref->isFormClass(DWARFFormValue::FC_Reference))
1997 break;
1998
1999 CompileUnit *RefCU;
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002000 if (auto RefDIE = resolveDIEReference(Linker, DMO, CompileUnits, *Ref,
Jonas Devliegherea9aab592018-01-29 14:52:50 +00002001 U.getOrigUnit(), DIE, RefCU)) {
2002 CU = RefCU;
2003 OrigUnit = &RefCU->getOrigUnit();
2004 DIE = RefDIE;
2005 }
2006 }
2007
2008 unsigned Idx = OrigUnit->getDIEIndex(DIE);
2009 if (!Name && DIE.getTag() == dwarf::DW_TAG_namespace)
2010 Name = "(anonymous namespace)";
2011
2012 if (CU->getInfo(Idx).ParentIdx == 0 ||
2013 // FIXME: dsymutil-classic compatibility. Ignore modules.
2014 CU->getOrigUnit().getDIEAtIndex(CU->getInfo(Idx).ParentIdx).getTag() ==
2015 dwarf::DW_TAG_module)
2016 return djbHash(Name ? Name : "", djbHash(RecurseDepth ? "" : "::"));
2017
2018 DWARFDie Die = OrigUnit->getDIEAtIndex(CU->getInfo(Idx).ParentIdx);
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002019 return djbHash(
2020 (Name ? Name : ""),
2021 djbHash((Name ? "::" : ""),
2022 hashFullyQualifiedName(Die, *CU, DMO, ++RecurseDepth)));
Jonas Devliegherea9aab592018-01-29 14:52:50 +00002023}
2024
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00002025static uint64_t getDwoId(const DWARFDie &CUDie, const DWARFUnit &Unit) {
2026 auto DwoId = dwarf::toUnsigned(
2027 CUDie.find({dwarf::DW_AT_dwo_id, dwarf::DW_AT_GNU_dwo_id}));
Greg Claytonc64f9192016-12-14 22:38:08 +00002028 if (DwoId)
2029 return *DwoId;
2030 return 0;
Adrian Prantlf4ad7592015-09-23 17:11:10 +00002031}
2032
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002033bool DwarfLinker::registerModuleReference(
2034 const DWARFDie &CUDie, const DWARFUnit &Unit, DebugMap &ModuleMap,
2035 const DebugMapObject &DMO, RangesTy &Ranges, OffsetsStringPool &StringPool,
2036 UniquingStringPool &UniquingStringPool, DeclContextTree &ODRContexts,
Jonas Devlieghere60c60092018-09-07 10:29:22 +00002037 uint64_t ModulesEndOffset, unsigned &UnitID, unsigned Indent, bool Quiet) {
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00002038 std::string PCMfile = dwarf::toString(
2039 CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
Adrian Prantld2136b32015-09-22 22:20:50 +00002040 if (PCMfile.empty())
2041 return false;
2042
2043 // Clang module DWARF skeleton CUs abuse this for the path to the module.
Greg Claytonbeabb632017-01-13 21:08:18 +00002044 std::string PCMpath = dwarf::toString(CUDie.find(dwarf::DW_AT_comp_dir), "");
Adrian Prantlf4ad7592015-09-23 17:11:10 +00002045 uint64_t DwoId = getDwoId(CUDie, Unit);
Adrian Prantld2136b32015-09-22 22:20:50 +00002046
Greg Claytonbeabb632017-01-13 21:08:18 +00002047 std::string Name = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
Adrian Prantl43df5822015-09-23 17:35:52 +00002048 if (Name.empty()) {
Adrian Prantl5309f162018-08-24 20:41:08 +00002049 if (!Quiet)
2050 reportWarning("Anonymous module skeleton CU for " + PCMfile, DMO);
Adrian Prantl43df5822015-09-23 17:35:52 +00002051 return true;
2052 }
2053
Adrian Prantl5309f162018-08-24 20:41:08 +00002054 if (!Quiet && Options.Verbose) {
Adrian Prantld2136b32015-09-22 22:20:50 +00002055 outs().indent(Indent);
2056 outs() << "Found clang module reference " << PCMfile;
2057 }
2058
Adrian Prantlf4ad7592015-09-23 17:11:10 +00002059 auto Cached = ClangModules.find(PCMfile);
2060 if (Cached != ClangModules.end()) {
Adrian Prantl07368be2016-05-13 00:17:58 +00002061 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2062 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2063 // ASTFileSignatures will change randomly when a module is rebuilt.
Adrian Prantl5309f162018-08-24 20:41:08 +00002064 if (!Quiet && Options.Verbose && (Cached->second != DwoId))
Adrian Prantlf4ad7592015-09-23 17:11:10 +00002065 reportWarning(Twine("hash mismatch: this object file was built against a "
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00002066 "different version of the module ") +
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002067 PCMfile,
2068 DMO);
Adrian Prantl5309f162018-08-24 20:41:08 +00002069 if (!Quiet && Options.Verbose)
Adrian Prantld2136b32015-09-22 22:20:50 +00002070 outs() << " [cached].\n";
2071 return true;
2072 }
Adrian Prantl5309f162018-08-24 20:41:08 +00002073 if (!Quiet && Options.Verbose)
Adrian Prantld2136b32015-09-22 22:20:50 +00002074 outs() << " ...\n";
2075
2076 // Cyclic dependencies are disallowed by Clang, but we still
2077 // shouldn't run into an infinite loop, so mark it as processed now.
Adrian Prantlf4ad7592015-09-23 17:11:10 +00002078 ClangModules.insert({PCMfile, DwoId});
Jonas Devlieghere60c60092018-09-07 10:29:22 +00002079 if (Error E =
2080 loadClangModule(PCMfile, PCMpath, Name, DwoId, ModuleMap, DMO, Ranges,
2081 StringPool, UniquingStringPool, ODRContexts,
2082 ModulesEndOffset, UnitID, Indent + 2, Quiet)) {
Rafael Espindola75fe73f2017-11-16 17:46:43 +00002083 consumeError(std::move(E));
2084 return false;
2085 }
Adrian Prantld2136b32015-09-22 22:20:50 +00002086 return true;
2087}
2088
Frederic Rissfa7f7bd2015-07-24 06:41:11 +00002089ErrorOr<const object::ObjectFile &>
Jonas Devlieghere4d3e6c12018-06-29 16:50:41 +00002090DwarfLinker::loadObject(const DebugMapObject &Obj, const DebugMap &Map) {
2091 auto ObjectEntry =
2092 BinHolder.getObjectEntry(Obj.getObjectFilename(), Obj.getTimestamp());
2093 if (!ObjectEntry) {
2094 auto Err = ObjectEntry.takeError();
2095 reportWarning(
2096 Twine(Obj.getObjectFilename()) + ": " + toString(std::move(Err)), Obj);
2097 return errorToErrorCode(std::move(Err));
Frederic Riss42829a52015-08-31 05:16:35 +00002098 }
Jonas Devlieghere4d3e6c12018-06-29 16:50:41 +00002099
2100 auto Object = ObjectEntry->getObject(Map.getTriple());
2101 if (!Object) {
2102 auto Err = Object.takeError();
2103 reportWarning(
2104 Twine(Obj.getObjectFilename()) + ": " + toString(std::move(Err)), Obj);
2105 return errorToErrorCode(std::move(Err));
2106 }
2107
2108 return *Object;
Frederic Rissfa7f7bd2015-07-24 06:41:11 +00002109}
2110
Adrian Prantl5309f162018-08-24 20:41:08 +00002111Error DwarfLinker::loadClangModule(
2112 StringRef Filename, StringRef ModulePath, StringRef ModuleName,
2113 uint64_t DwoId, DebugMap &ModuleMap, const DebugMapObject &DMO,
2114 RangesTy &Ranges, OffsetsStringPool &StringPool,
2115 UniquingStringPool &UniquingStringPool, DeclContextTree &ODRContexts,
Jonas Devlieghere60c60092018-09-07 10:29:22 +00002116 uint64_t ModulesEndOffset, unsigned &UnitID, unsigned Indent, bool Quiet) {
Adrian Prantld2136b32015-09-22 22:20:50 +00002117 SmallString<80> Path(Options.PrependPath);
2118 if (sys::path::is_relative(Filename))
2119 sys::path::append(Path, ModulePath, Filename);
2120 else
2121 sys::path::append(Path, Filename);
Jonas Devlieghere4d3e6c12018-06-29 16:50:41 +00002122 // Don't use the cached binary holder because we have no thread-safety
2123 // guarantee and the lifetime is limited.
Francis Ricci6f200df2017-10-06 14:49:20 +00002124 auto &Obj = ModuleMap.addDebugMapObject(
2125 Path, sys::TimePoint<std::chrono::seconds>(), MachO::N_OSO);
Jonas Devlieghere4d3e6c12018-06-29 16:50:41 +00002126 auto ErrOrObj = loadObject(Obj, ModuleMap);
Adrian Prantlb363d332016-01-14 18:31:07 +00002127 if (!ErrOrObj) {
2128 // Try and emit more helpful warnings by applying some heuristics.
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002129 StringRef ObjFile = DMO.getObjectFilename();
Adrian Prantlb363d332016-01-14 18:31:07 +00002130 bool isClangModule = sys::path::extension(Filename).equals(".pcm");
2131 bool isArchive = ObjFile.endswith(")");
2132 if (isClangModule) {
Adrian Prantlb363d332016-01-14 18:31:07 +00002133 StringRef ModuleCacheDir = sys::path::parent_path(Path);
2134 if (sys::fs::exists(ModuleCacheDir)) {
2135 // If the module's parent directory exists, we assume that the module
2136 // cache has expired and was pruned by clang. A more adventurous
2137 // dsymutil would invoke clang to rebuild the module now.
2138 if (!ModuleCacheHintDisplayed) {
Jonas Devlieghere5baab4c2018-04-14 21:36:42 +00002139 WithColor::note() << "The clang module cache may have expired since "
2140 "this object file was built. Rebuilding the "
2141 "object file will rebuild the module cache.\n";
Adrian Prantlb363d332016-01-14 18:31:07 +00002142 ModuleCacheHintDisplayed = true;
2143 }
2144 } else if (isArchive) {
2145 // If the module cache directory doesn't exist at all and the object
2146 // file is inside a static library, we assume that the static library
2147 // was built on a different machine. We don't want to discourage module
2148 // debugging for convenience libraries within a project though.
2149 if (!ArchiveHintDisplayed) {
Jonas Devlieghere5baab4c2018-04-14 21:36:42 +00002150 WithColor::note()
2151 << "Linking a static library that was built with "
2152 "-gmodules, but the module cache was not found. "
2153 "Redistributable static libraries should never be "
2154 "built with module debugging enabled. The debug "
2155 "experience will be degraded due to incomplete "
2156 "debug information.\n";
Adrian Prantlb363d332016-01-14 18:31:07 +00002157 ArchiveHintDisplayed = true;
2158 }
2159 }
2160 }
Rafael Espindola75fe73f2017-11-16 17:46:43 +00002161 return Error::success();
Adrian Prantlb363d332016-01-14 18:31:07 +00002162 }
Adrian Prantld2136b32015-09-22 22:20:50 +00002163
Benjamin Kramer39a9cdb2015-09-23 10:38:59 +00002164 std::unique_ptr<CompileUnit> Unit;
Adrian Prantld2136b32015-09-22 22:20:50 +00002165
2166 // Setup access to the debug info.
Rafael Espindolaee809ac2017-07-19 22:27:28 +00002167 auto DwarfContext = DWARFContext::create(*ErrOrObj);
Adrian Prantld2136b32015-09-22 22:20:50 +00002168 RelocationManager RelocMgr(*this);
Adrian Prantlf3855902017-08-08 18:26:12 +00002169
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00002170 for (const auto &CU : DwarfContext->compile_units()) {
2171 updateDwarfVersion(CU->getVersion());
Adrian Prantld2136b32015-09-22 22:20:50 +00002172 // Recursively get all modules imported by this one.
Adrian Prantlf3855902017-08-08 18:26:12 +00002173 auto CUDie = CU->getUnitDIE(false);
Jonas Devlieghere760af162018-04-08 17:35:17 +00002174 if (!CUDie)
2175 continue;
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002176 if (!registerModuleReference(CUDie, *CU, ModuleMap, DMO, Ranges, StringPool,
Jonas Devlieghere60c60092018-09-07 10:29:22 +00002177 UniquingStringPool, ODRContexts,
2178 ModulesEndOffset, UnitID, Indent, Quiet)) {
Adrian Prantld2136b32015-09-22 22:20:50 +00002179 if (Unit) {
Rafael Espindola75fe73f2017-11-16 17:46:43 +00002180 std::string Err =
2181 (Filename +
2182 ": Clang modules are expected to have exactly 1 compile unit.\n")
2183 .str();
Jonas Devliegherebeee6ae2018-03-09 15:22:42 +00002184 error(Err);
Rafael Espindola75fe73f2017-11-16 17:46:43 +00002185 return make_error<StringError>(Err, inconvertibleErrorCode());
Adrian Prantld2136b32015-09-22 22:20:50 +00002186 }
Adrian Prantlcbea3362016-04-25 17:04:32 +00002187 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2188 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2189 // ASTFileSignatures will change randomly when a module is rebuilt.
Greg Claytonadb170f2016-12-13 18:25:19 +00002190 uint64_t PCMDwoId = getDwoId(CUDie, *CU);
Adrian Prantl07368be2016-05-13 00:17:58 +00002191 if (PCMDwoId != DwoId) {
Adrian Prantl5309f162018-08-24 20:41:08 +00002192 if (!Quiet && Options.Verbose)
Adrian Prantl07368be2016-05-13 00:17:58 +00002193 reportWarning(
2194 Twine("hash mismatch: this object file was built against a "
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +00002195 "different version of the module ") +
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002196 Filename,
2197 DMO);
Adrian Prantl07368be2016-05-13 00:17:58 +00002198 // Update the cache entry with the DwoId of the module loaded from disk.
2199 ClangModules[Filename] = PCMDwoId;
2200 }
Adrian Prantlf4ad7592015-09-23 17:11:10 +00002201
2202 // Add this module.
Adrian Prantl43df5822015-09-23 17:35:52 +00002203 Unit = llvm::make_unique<CompileUnit>(*CU, UnitID++, !Options.NoODR,
2204 ModuleName);
Adrian Prantld2136b32015-09-22 22:20:50 +00002205 Unit->setHasInterestingContent();
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002206 analyzeContextInfo(CUDie, 0, *Unit, &ODRContexts.getRoot(),
Jonas Devlieghere60c60092018-09-07 10:29:22 +00002207 UniquingStringPool, ODRContexts, ModulesEndOffset);
Adrian Prantld2136b32015-09-22 22:20:50 +00002208 // Keep everything.
2209 Unit->markEverythingAsKept();
2210 }
2211 }
Adrian Prantl88b828e2017-08-22 01:10:48 +00002212 if (!Unit->getOrigUnit().getUnitDIE().hasChildren())
Rafael Espindola75fe73f2017-11-16 17:46:43 +00002213 return Error::success();
Adrian Prantl5309f162018-08-24 20:41:08 +00002214 if (!Quiet && Options.Verbose) {
Adrian Prantld2136b32015-09-22 22:20:50 +00002215 outs().indent(Indent);
2216 outs() << "cloning .debug_info from " << Filename << "\n";
2217 }
2218
Jonas Devlieghereea63d872018-06-28 15:01:42 +00002219 UnitListTy CompileUnits;
Greg Claytonfc1fc8b2016-12-01 18:56:29 +00002220 CompileUnits.push_back(std::move(Unit));
2221 DIECloner(*this, RelocMgr, DIEAlloc, CompileUnits, Options)
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002222 .cloneAllCompileUnits(*DwarfContext, DMO, Ranges, StringPool);
Rafael Espindola75fe73f2017-11-16 17:46:43 +00002223 return Error::success();
Adrian Prantld2136b32015-09-22 22:20:50 +00002224}
2225
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002226void DwarfLinker::DIECloner::cloneAllCompileUnits(
2227 DWARFContext &DwarfContext, const DebugMapObject &DMO, RangesTy &Ranges,
2228 OffsetsStringPool &StringPool) {
Adrian Prantlafca6b42015-09-14 16:46:10 +00002229 if (!Linker.Streamer)
Adrian Prantl0a0a9602015-09-11 23:45:30 +00002230 return;
2231
2232 for (auto &CurrentUnit : CompileUnits) {
Greg Claytonadb170f2016-12-13 18:25:19 +00002233 auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
Greg Claytonfc1fc8b2016-12-01 18:56:29 +00002234 CurrentUnit->setStartOffset(Linker.OutputDebugInfoSize);
Jonas Devlieghere760af162018-04-08 17:35:17 +00002235 if (!InputDIE) {
2236 Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
2237 continue;
2238 }
Frederic Riss909a3212017-02-09 19:41:55 +00002239 if (CurrentUnit->getInfo(0).Keep) {
2240 // Clone the InputDIE into your Unit DIE in our compile unit since it
2241 // already has a DIE inside of it.
2242 CurrentUnit->createOutputDIE();
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002243 cloneDIE(InputDIE, DMO, *CurrentUnit, StringPool, 0 /* PC offset */,
Frederic Riss909a3212017-02-09 19:41:55 +00002244 11 /* Unit Header size */, 0, CurrentUnit->getOutputUnitDIE());
2245 }
Greg Claytonfc1fc8b2016-12-01 18:56:29 +00002246 Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
Adrian Prantlafca6b42015-09-14 16:46:10 +00002247 if (Linker.Options.NoOutput)
Adrian Prantl0a0a9602015-09-11 23:45:30 +00002248 continue;
Jonas Devlieghere61693462018-02-08 10:48:54 +00002249
Jonas Devlieghere20767d12019-01-07 23:27:25 +00002250 // FIXME: for compatibility with the classic dsymutil, we emit
2251 // an empty line table for the unit, even if the unit doesn't
2252 // actually exist in the DIE tree.
2253 if (LLVM_LIKELY(!Linker.Options.Update) || Linker.Options.Translator)
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002254 Linker.patchLineTableForUnit(*CurrentUnit, DwarfContext, Ranges, DMO);
Jonas Devlieghere20767d12019-01-07 23:27:25 +00002255 Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
2256 if (Linker.Options.Update)
2257 continue;
2258 Linker.patchRangesForUnit(*CurrentUnit, DwarfContext, DMO);
2259 Linker.Streamer->emitLocationsForUnit(*CurrentUnit, DwarfContext);
Adrian Prantl0a0a9602015-09-11 23:45:30 +00002260 }
2261
Adrian Prantlafca6b42015-09-14 16:46:10 +00002262 if (Linker.Options.NoOutput)
Adrian Prantl0a0a9602015-09-11 23:45:30 +00002263 return;
2264
2265 // Emit all the compile unit's debug information.
2266 for (auto &CurrentUnit : CompileUnits) {
Jonas Devlieghere61693462018-02-08 10:48:54 +00002267 if (LLVM_LIKELY(!Linker.Options.Update))
2268 Linker.generateUnitRanges(*CurrentUnit);
Greg Claytonfc1fc8b2016-12-01 18:56:29 +00002269 CurrentUnit->fixupForwardReferences();
2270 Linker.Streamer->emitCompileUnitHeader(*CurrentUnit);
2271 if (!CurrentUnit->getOutputUnitDIE())
Adrian Prantl0a0a9602015-09-11 23:45:30 +00002272 continue;
Greg Claytonfc1fc8b2016-12-01 18:56:29 +00002273 Linker.Streamer->emitDIE(*CurrentUnit->getOutputUnitDIE());
Adrian Prantl0a0a9602015-09-11 23:45:30 +00002274 }
2275}
2276
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00002277void DwarfLinker::updateAccelKind(DWARFContext &Dwarf) {
2278 if (Options.TheAccelTableKind != AccelTableKind::Default)
2279 return;
2280
2281 auto &DwarfObj = Dwarf.getDWARFObj();
2282
2283 if (!AtLeastOneDwarfAccelTable &&
2284 (!DwarfObj.getAppleNamesSection().Data.empty() ||
2285 !DwarfObj.getAppleTypesSection().Data.empty() ||
2286 !DwarfObj.getAppleNamespacesSection().Data.empty() ||
2287 !DwarfObj.getAppleObjCSection().Data.empty())) {
2288 AtLeastOneAppleAccelTable = true;
2289 }
2290
2291 if (!AtLeastOneDwarfAccelTable &&
2292 !DwarfObj.getDebugNamesSection().Data.empty()) {
2293 AtLeastOneDwarfAccelTable = true;
2294 }
2295}
2296
Jonas Devlieghere67eb8fd2018-04-02 10:40:43 +00002297bool DwarfLinker::emitPaperTrailWarnings(const DebugMapObject &DMO,
2298 const DebugMap &Map,
2299 OffsetsStringPool &StringPool) {
2300 if (DMO.getWarnings().empty() || !DMO.empty())
2301 return false;
2302
2303 Streamer->switchToDebugInfoSection(/* Version */ 2);
2304 DIE *CUDie = DIE::get(DIEAlloc, dwarf::DW_TAG_compile_unit);
2305 CUDie->setOffset(11);
2306 StringRef Producer = StringPool.internString("dsymutil");
2307 StringRef File = StringPool.internString(DMO.getObjectFilename());
2308 CUDie->addValue(DIEAlloc, dwarf::DW_AT_producer, dwarf::DW_FORM_strp,
2309 DIEInteger(StringPool.getStringOffset(Producer)));
2310 DIEBlock *String = new (DIEAlloc) DIEBlock();
2311 DIEBlocks.push_back(String);
2312 for (auto &C : File)
2313 String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
2314 DIEInteger(C));
2315 String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
2316 DIEInteger(0));
2317
2318 CUDie->addValue(DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_string, String);
2319 for (const auto &Warning : DMO.getWarnings()) {
2320 DIE &ConstDie = CUDie->addChild(DIE::get(DIEAlloc, dwarf::DW_TAG_constant));
2321 ConstDie.addValue(
2322 DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp,
2323 DIEInteger(StringPool.getStringOffset("dsymutil_warning")));
2324 ConstDie.addValue(DIEAlloc, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag,
2325 DIEInteger(1));
2326 ConstDie.addValue(DIEAlloc, dwarf::DW_AT_const_value, dwarf::DW_FORM_strp,
2327 DIEInteger(StringPool.getStringOffset(Warning)));
2328 }
2329 unsigned Size = 4 /* FORM_strp */ + File.size() + 1 +
2330 DMO.getWarnings().size() * (4 + 1 + 4) +
2331 1 /* End of children */;
2332 DIEAbbrev Abbrev = CUDie->generateAbbrev();
2333 AssignAbbrev(Abbrev);
2334 CUDie->setAbbrevNumber(Abbrev.getNumber());
2335 Size += getULEB128Size(Abbrev.getNumber());
2336 // Abbreviation ordering needed for classic compatibility.
2337 for (auto &Child : CUDie->children()) {
2338 Abbrev = Child.generateAbbrev();
2339 AssignAbbrev(Abbrev);
2340 Child.setAbbrevNumber(Abbrev.getNumber());
2341 Size += getULEB128Size(Abbrev.getNumber());
2342 }
2343 CUDie->setSize(Size);
2344 auto &Asm = Streamer->getAsmPrinter();
2345 Asm.emitInt32(11 + CUDie->getSize() - 4);
2346 Asm.emitInt16(2);
2347 Asm.emitInt32(0);
2348 Asm.emitInt8(Map.getTriple().isArch64Bit() ? 8 : 4);
2349 Streamer->emitDIE(*CUDie);
2350 OutputDebugInfoSize += 11 /* Header */ + Size;
2351
2352 return true;
2353}
2354
Frederic Rissb616a182015-01-28 18:27:01 +00002355bool DwarfLinker::link(const DebugMap &Map) {
Rafael Espindola5feaa632017-11-15 20:55:53 +00002356 if (!createStreamer(Map.getTriple(), OutFile))
Frederic Riss2929a7a2015-02-28 00:29:11 +00002357 return false;
2358
Frederic Rissd0d92e72015-03-04 22:07:44 +00002359 // Size of the DIEs (and headers) generated for the linked output.
Adrian Prantl0a0a9602015-09-11 23:45:30 +00002360 OutputDebugInfoSize = 0;
Frederic Riss1d6cc762015-03-14 03:46:40 +00002361 // A unique ID that identifies each compile unit.
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002362 unsigned UnitID = 0;
Adrian Prantld2136b32015-09-22 22:20:50 +00002363 DebugMap ModuleMap(Map.getTriple(), Map.getBinaryPath());
2364
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002365 // First populate the data structure we need for each iteration of the
2366 // parallel loop.
2367 unsigned NumObjects = Map.getNumberOfObjects();
2368 std::vector<LinkContext> ObjectContexts;
2369 ObjectContexts.reserve(NumObjects);
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00002370 for (const auto &Obj : Map.objects()) {
Jonas Devlieghere4d3e6c12018-06-29 16:50:41 +00002371 ObjectContexts.emplace_back(Map, *this, *Obj.get());
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00002372 LinkContext &LC = ObjectContexts.back();
2373 if (LC.ObjectFile)
2374 updateAccelKind(*LC.DwarfContext);
2375 }
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002376
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002377 // This Dwarf string pool which is only used for uniquing. This one should
2378 // never be used for offsets as its not thread-safe or predictable.
2379 UniquingStringPool UniquingStringPool;
2380
2381 // This Dwarf string pool which is used for emission. It must be used
2382 // serially as the order of calling getStringOffset matters for
2383 // reproducibility.
Jonas Devlieghere20767d12019-01-07 23:27:25 +00002384 OffsetsStringPool OffsetsStringPool(Options.Translator);
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002385
2386 // ODR Contexts for the link.
2387 DeclContextTree ODRContexts;
2388
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00002389 // If we haven't decided on an accelerator table kind yet, we base ourselves
2390 // on the DWARF we have seen so far. At this point we haven't pulled in debug
2391 // information from modules yet, so it is technically possible that they
2392 // would affect the decision. However, as they're built with the same
2393 // compiler and flags, it is safe to assume that they will follow the
2394 // decision made here.
2395 if (Options.TheAccelTableKind == AccelTableKind::Default) {
2396 if (AtLeastOneDwarfAccelTable && !AtLeastOneAppleAccelTable)
2397 Options.TheAccelTableKind = AccelTableKind::Dwarf;
2398 else
2399 Options.TheAccelTableKind = AccelTableKind::Apple;
2400 }
2401
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002402 for (LinkContext &LinkContext : ObjectContexts) {
Frederic Rissd9a80bd2015-02-28 00:29:07 +00002403 if (Options.Verbose)
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002404 outs() << "DEBUG MAP OBJECT: " << LinkContext.DMO.getObjectFilename()
2405 << "\n";
Francis Ricci6f200df2017-10-06 14:49:20 +00002406
2407 // N_AST objects (swiftmodule files) should get dumped directly into the
2408 // appropriate DWARF section.
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002409 if (LinkContext.DMO.getType() == MachO::N_AST) {
2410 StringRef File = LinkContext.DMO.getObjectFilename();
Jonas Devliegheref16734c2017-10-13 14:41:23 +00002411 auto ErrorOrMem = MemoryBuffer::getFile(File);
2412 if (!ErrorOrMem) {
Jonas Devliegherecc46e392018-03-13 15:47:38 +00002413 warn("Could not open '" + File + "'\n");
Francis Ricci6f200df2017-10-06 14:49:20 +00002414 continue;
Jonas Devliegheref16734c2017-10-13 14:41:23 +00002415 }
2416 sys::fs::file_status Stat;
Jonas Devliegherebeee6ae2018-03-09 15:22:42 +00002417 if (auto Err = sys::fs::status(File, Stat)) {
2418 warn(Err.message());
Jonas Devliegheref16734c2017-10-13 14:41:23 +00002419 continue;
2420 }
Jonas Devlieghereaefeecf2018-12-04 17:15:23 +00002421 if (!Options.NoTimestamp) {
2422 // The modification can have sub-second precision so we need to cast
2423 // away the extra precision that's not present in the debug map.
2424 auto ModificationTime =
2425 std::chrono::time_point_cast<std::chrono::seconds>(
2426 Stat.getLastModificationTime());
2427 if (ModificationTime != LinkContext.DMO.getTimestamp()) {
2428 // Not using the helper here as we can easily stream TimePoint<>.
2429 WithColor::warning()
2430 << "Timestamp mismatch for " << File << ": "
2431 << Stat.getLastModificationTime() << " and "
2432 << sys::TimePoint<>(LinkContext.DMO.getTimestamp()) << "\n";
2433 continue;
2434 }
Jonas Devliegheref16734c2017-10-13 14:41:23 +00002435 }
2436
2437 // Copy the module into the .swift_ast section.
Francis Ricci6f200df2017-10-06 14:49:20 +00002438 if (!Options.NoOutput)
Jonas Devliegheref16734c2017-10-13 14:41:23 +00002439 Streamer->emitSwiftAST((*ErrorOrMem)->getBuffer());
Francis Ricci6f200df2017-10-06 14:49:20 +00002440 continue;
2441 }
2442
Jonas Devlieghere67eb8fd2018-04-02 10:40:43 +00002443 if (emitPaperTrailWarnings(LinkContext.DMO, Map, OffsetsStringPool))
2444 continue;
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002445
Jonas Devlieghere67eb8fd2018-04-02 10:40:43 +00002446 if (!LinkContext.ObjectFile)
Frederic Rissb616a182015-01-28 18:27:01 +00002447 continue;
Frederic Rissb616a182015-01-28 18:27:01 +00002448
Frederic Riss16acc912015-02-13 23:18:22 +00002449 // Look for relocations that correspond to debug map entries.
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002450
Jonas Devlieghere61693462018-02-08 10:48:54 +00002451 if (LLVM_LIKELY(!Options.Update) &&
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002452 !LinkContext.RelocMgr.findValidRelocsInDebugInfo(
2453 *LinkContext.ObjectFile, LinkContext.DMO)) {
Frederic Rissd9a80bd2015-02-28 00:29:07 +00002454 if (Options.Verbose)
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002455 outs() << "No valid relocations found. Skipping.\n";
2456
2457 // Clear this ObjFile entry as a signal to other loops that we should not
2458 // process this iteration.
2459 LinkContext.ObjectFile = nullptr;
Frederic Riss16acc912015-02-13 23:18:22 +00002460 continue;
2461 }
2462
Frederic Riss3ddec312015-01-28 22:15:14 +00002463 // Setup access to the debug info.
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002464 if (!LinkContext.DwarfContext)
2465 continue;
Frederic Rissb616a182015-01-28 18:27:01 +00002466
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002467 startDebugObject(LinkContext);
2468
Adrian Prantlfce22192015-10-05 23:11:20 +00002469 // In a first phase, just read in the debug info and load all clang modules.
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002470 LinkContext.CompileUnits.reserve(
2471 LinkContext.DwarfContext->getNumCompileUnits());
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00002472
Jonas Devliegherea4c60c42018-03-13 10:52:49 +00002473 for (const auto &CU : LinkContext.DwarfContext->compile_units()) {
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00002474 updateDwarfVersion(CU->getVersion());
Greg Claytonadb170f2016-12-13 18:25:19 +00002475 auto CUDie = CU->getUnitDIE(false);
Frederic Rissd9a80bd2015-02-28 00:29:07 +00002476 if (Options.Verbose) {
Frederic Rissb616a182015-01-28 18:27:01 +00002477 outs() << "Input compilation unit:";
Jonas Devlieghered2663072017-09-13 09:43:05 +00002478 DIDumpOptions DumpOpts;
Adrian Prantlae599aa2017-09-20 17:44:00 +00002479 DumpOpts.RecurseDepth = 0;
Jonas Devlieghered2663072017-09-13 09:43:05 +00002480 DumpOpts.Verbose = Options.Verbose;
Adrian Prantlae599aa2017-09-20 17:44:00 +00002481 CUDie.dump(outs(), 0, DumpOpts);
Frederic Rissb616a182015-01-28 18:27:01 +00002482 }
Adrian Prantl5309f162018-08-24 20:41:08 +00002483 if (CUDie && !LLVM_UNLIKELY(Options.Update))
2484 registerModuleReference(CUDie, *CU, ModuleMap, LinkContext.DMO,
2485 LinkContext.Ranges, OffsetsStringPool,
Jonas Devlieghere60c60092018-09-07 10:29:22 +00002486 UniquingStringPool, ODRContexts, 0, UnitID);
Frederic Rissb616a182015-01-28 18:27:01 +00002487 }
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002488 }
Frederic Riss3ddec312015-01-28 22:15:14 +00002489
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00002490 // If we haven't seen any CUs, pick an arbitrary valid Dwarf version anyway.
Jonas Devlieghere67eb8fd2018-04-02 10:40:43 +00002491 if (MaxDwarfVersion == 0)
2492 MaxDwarfVersion = 3;
2493
Jonas Devlieghere60c60092018-09-07 10:29:22 +00002494 // At this point we know how much data we have emitted. We use this value to
2495 // compare canonical DIE offsets in analyzeContextInfo to see if a definition
2496 // is already emitted, without being affected by canonical die offsets set
2497 // later. This prevents undeterminism when analyze and clone execute
2498 // concurrently, as clone set the canonical DIE offset and analyze reads it.
2499 const uint64_t ModulesEndOffset = OutputDebugInfoSize;
2500
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002501 // These variables manage the list of processed object files.
2502 // The mutex and condition variable are to ensure that this is thread safe.
2503 std::mutex ProcessedFilesMutex;
2504 std::condition_variable ProcessedFilesConditionVariable;
2505 BitVector ProcessedFiles(NumObjects, false);
2506
Jonas Devlieghere9840d7c2018-09-06 17:31:59 +00002507 // Analyzing the context info is particularly expensive so it is executed in
2508 // parallel with emitting the previous compile unit.
2509 auto AnalyzeLambda = [&](size_t i) {
2510 auto &LinkContext = ObjectContexts[i];
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002511
Jonas Devlieghere9840d7c2018-09-06 17:31:59 +00002512 if (!LinkContext.ObjectFile || !LinkContext.DwarfContext)
2513 return;
2514
2515 for (const auto &CU : LinkContext.DwarfContext->compile_units()) {
2516 updateDwarfVersion(CU->getVersion());
2517 // The !registerModuleReference() condition effectively skips
2518 // over fully resolved skeleton units. This second pass of
2519 // registerModuleReferences doesn't do any new work, but it
2520 // will collect top-level errors, which are suppressed. Module
2521 // warnings were already displayed in the first iteration.
2522 bool Quiet = true;
2523 auto CUDie = CU->getUnitDIE(false);
2524 if (!CUDie || LLVM_UNLIKELY(Options.Update) ||
2525 !registerModuleReference(CUDie, *CU, ModuleMap, LinkContext.DMO,
2526 LinkContext.Ranges, OffsetsStringPool,
Jonas Devlieghere60c60092018-09-07 10:29:22 +00002527 UniquingStringPool, ODRContexts,
2528 ModulesEndOffset, UnitID, Quiet)) {
Jonas Devlieghere9840d7c2018-09-06 17:31:59 +00002529 LinkContext.CompileUnits.push_back(llvm::make_unique<CompileUnit>(
2530 *CU, UnitID++, !Options.NoODR && !Options.Update, ""));
2531 }
2532 }
2533
2534 // Now build the DIE parent links that we will use during the next phase.
2535 for (auto &CurrentUnit : LinkContext.CompileUnits) {
2536 auto CUDie = CurrentUnit->getOrigUnit().getUnitDIE();
2537 if (!CUDie)
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002538 continue;
Jonas Devlieghere9840d7c2018-09-06 17:31:59 +00002539 analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0,
2540 *CurrentUnit, &ODRContexts.getRoot(),
Jonas Devlieghere60c60092018-09-07 10:29:22 +00002541 UniquingStringPool, ODRContexts, ModulesEndOffset);
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002542 }
Jonas Devlieghere2f039a02018-04-03 18:01:18 +00002543 };
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002544
2545 // And then the remaining work in serial again.
2546 // Note, although this loop runs in serial, it can run in parallel with
2547 // the analyzeContextInfo loop so long as we process files with indices >=
2548 // than those processed by analyzeContextInfo.
Jonas Devlieghere9840d7c2018-09-06 17:31:59 +00002549 auto CloneLambda = [&](size_t i) {
2550 auto &LinkContext = ObjectContexts[i];
2551 if (!LinkContext.ObjectFile)
2552 return;
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002553
Jonas Devlieghere9840d7c2018-09-06 17:31:59 +00002554 // Then mark all the DIEs that need to be present in the linked output
2555 // and collect some information about them.
2556 // Note that this loop can not be merged with the previous one because
2557 // cross-cu references require the ParentIdx to be setup for every CU in
2558 // the object file before calling this.
2559 if (LLVM_UNLIKELY(Options.Update)) {
2560 for (auto &CurrentUnit : LinkContext.CompileUnits)
2561 CurrentUnit->markEverythingAsKept();
2562 Streamer->copyInvariantDebugSection(*LinkContext.ObjectFile);
2563 } else {
2564 for (auto &CurrentUnit : LinkContext.CompileUnits)
2565 lookForDIEsToKeep(LinkContext.RelocMgr, LinkContext.Ranges,
2566 LinkContext.CompileUnits,
2567 CurrentUnit->getOrigUnit().getUnitDIE(),
2568 LinkContext.DMO, *CurrentUnit, 0);
Jonas Devlieghere61693462018-02-08 10:48:54 +00002569 }
Frederic Riss2e2ed162015-02-13 23:18:34 +00002570
Jonas Devlieghere9840d7c2018-09-06 17:31:59 +00002571 // The calls to applyValidRelocs inside cloneDIE will walk the reloc
2572 // array again (in the same way findValidRelocsInDebugInfo() did). We
2573 // need to reset the NextValidReloc index to the beginning.
2574 LinkContext.RelocMgr.resetValidRelocs();
2575 if (LinkContext.RelocMgr.hasValidRelocs() || LLVM_UNLIKELY(Options.Update))
2576 DIECloner(*this, LinkContext.RelocMgr, DIEAlloc, LinkContext.CompileUnits,
2577 Options)
2578 .cloneAllCompileUnits(*LinkContext.DwarfContext, LinkContext.DMO,
2579 LinkContext.Ranges, OffsetsStringPool);
2580 if (!Options.NoOutput && !LinkContext.CompileUnits.empty() &&
2581 LLVM_LIKELY(!Options.Update))
2582 patchFrameInfoForObject(
2583 LinkContext.DMO, LinkContext.Ranges, *LinkContext.DwarfContext,
2584 LinkContext.CompileUnits[0]->getOrigUnit().getAddressByteSize());
2585
2586 // Clean-up before starting working on the next object.
2587 endDebugObject(LinkContext);
2588 };
2589
2590 auto EmitLambda = [&]() {
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002591 // Emit everything that's global.
2592 if (!Options.NoOutput) {
2593 Streamer->emitAbbrevs(Abbreviations, MaxDwarfVersion);
2594 Streamer->emitStrings(OffsetsStringPool);
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00002595 switch (Options.TheAccelTableKind) {
2596 case AccelTableKind::Apple:
2597 Streamer->emitAppleNames(AppleNames);
2598 Streamer->emitAppleNamespaces(AppleNamespaces);
2599 Streamer->emitAppleTypes(AppleTypes);
2600 Streamer->emitAppleObjc(AppleObjc);
2601 break;
2602 case AccelTableKind::Dwarf:
2603 Streamer->emitDebugNames(DebugNames);
2604 break;
2605 case AccelTableKind::Default:
2606 llvm_unreachable("Default should have already been resolved.");
2607 break;
2608 }
Jonas Devlieghereb9365202018-03-13 14:27:15 +00002609 }
Jonas Devlieghere2f039a02018-04-03 18:01:18 +00002610 };
Frederic Riss9ccf07f2015-06-05 23:06:11 +00002611
Jonas Devlieghere9840d7c2018-09-06 17:31:59 +00002612 auto AnalyzeAll = [&]() {
2613 for (unsigned i = 0, e = NumObjects; i != e; ++i) {
2614 AnalyzeLambda(i);
2615
2616 std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
2617 ProcessedFiles.set(i);
2618 ProcessedFilesConditionVariable.notify_one();
2619 }
2620 };
2621
2622 auto CloneAll = [&]() {
2623 for (unsigned i = 0, e = NumObjects; i != e; ++i) {
2624 {
2625 std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
2626 if (!ProcessedFiles[i]) {
2627 ProcessedFilesConditionVariable.wait(
2628 LockGuard, [&]() { return ProcessedFiles[i]; });
2629 }
2630 }
2631
2632 CloneLambda(i);
2633 }
2634 EmitLambda();
2635 };
2636
2637 // To limit memory usage in the single threaded case, analyze and clone are
2638 // run sequentially so the LinkContext is freed after processing each object
2639 // in endDebugObject.
Jonas Devlieghere2f039a02018-04-03 18:01:18 +00002640 if (Options.Threads == 1) {
Jonas Devlieghere9840d7c2018-09-06 17:31:59 +00002641 for (unsigned i = 0, e = NumObjects; i != e; ++i) {
2642 AnalyzeLambda(i);
2643 CloneLambda(i);
2644 }
2645 EmitLambda();
Jonas Devlieghere2f039a02018-04-03 18:01:18 +00002646 } else {
2647 ThreadPool pool(2);
Jonas Devlieghere9840d7c2018-09-06 17:31:59 +00002648 pool.async(AnalyzeAll);
2649 pool.async(CloneAll);
Jonas Devlieghere2f039a02018-04-03 18:01:18 +00002650 pool.wait();
2651 }
Frederic Rissd0d92e72015-03-04 22:07:44 +00002652
Jonas Devlieghere20767d12019-01-07 23:27:25 +00002653 return Options.NoOutput ? true : Streamer->finish(Map, Options.Translator);
Jonas Devlieghere7c767bd2018-07-25 23:01:38 +00002654} // namespace dsymutil
Frederic Rissb616a182015-01-28 18:27:01 +00002655
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +00002656bool linkDwarf(raw_fd_ostream &OutFile, BinaryHolder &BinHolder,
Jonas Devlieghere4d3e6c12018-06-29 16:50:41 +00002657 const DebugMap &DM, const LinkOptions &Options) {
2658 DwarfLinker Linker(OutFile, BinHolder, Options);
Frederic Rissb616a182015-01-28 18:27:01 +00002659 return Linker.link(DM);
2660}
Eugene Zelenko66f724e2017-11-01 21:16:06 +00002661
Jonas Devliegherecc46e392018-03-13 15:47:38 +00002662} // namespace dsymutil
2663} // namespace llvm