1//=== DWARFLinkerImpl.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DWARFLinkerImpl.h"
10#include "DependencyTracker.h"
11#include "llvm/DWARFLinker/Utils.h"
12#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
13#include "llvm/Support/FormatVariadic.h"
14#include "llvm/Support/Parallel.h"
15#include "llvm/Support/ThreadPool.h"
16
17using namespace llvm;
18using namespace dwarf_linker;
19using namespace dwarf_linker::parallel;
20
21DWARFLinkerImpl::DWARFLinkerImpl(MessageHandlerTy ErrorHandler,
22 MessageHandlerTy WarningHandler)
23 : UniqueUnitID(0), DebugStrStrings(GlobalData),
24 DebugLineStrStrings(GlobalData), CommonSections(GlobalData) {
25 GlobalData.setErrorHandler(ErrorHandler);
26 GlobalData.setWarningHandler(WarningHandler);
27}
28
29DWARFLinkerImpl::LinkContext::LinkContext(LinkingGlobalData &GlobalData,
30 DWARFFile &File, uint64_t ObjFileIdx,
31 StringMap<uint64_t> &ClangModules,
32 uint64_t &ModuleUnitIdx,
33 std::atomic<size_t> &UniqueUnitID)
34 : OutputSections(GlobalData), InputDWARFFile(File),
35 ObjectFileIdx(ObjFileIdx), ClangModules(ClangModules),
36 ModuleUnitIdx(ModuleUnitIdx), UniqueUnitID(UniqueUnitID) {
37
38 if (File.Dwarf) {
39 if (!File.Dwarf->compile_units().empty())
40 CompileUnits.reserve(N: File.Dwarf->getNumCompileUnits());
41
42 // Set context format&endianness based on the input file.
43 Format.Version = File.Dwarf->getMaxVersion();
44 Format.AddrSize = File.Dwarf->getCUAddrSize();
45 Endianness = File.Dwarf->isLittleEndian() ? llvm::endianness::little
46 : llvm::endianness::big;
47 }
48}
49
50void DWARFLinkerImpl::addObjectFile(DWARFFile &File, ObjFileLoaderTy Loader,
51 CompileUnitHandlerTy OnCUDieLoaded) {
52 ObjectContexts.emplace_back(Args: std::make_unique<LinkContext>(
53 args&: GlobalData, args&: File, args: FirstObjFileIdx + ObjectContexts.size(), args&: ClangModules,
54 args&: ModuleUnitIdx, args&: UniqueUnitID));
55
56 if (ObjectContexts.back()->InputDWARFFile.Dwarf) {
57 for (const std::unique_ptr<DWARFUnit> &CU :
58 ObjectContexts.back()->InputDWARFFile.Dwarf->compile_units()) {
59 DWARFDie CUDie = CU->getUnitDIE();
60
61 if (!CUDie)
62 continue;
63
64 OnCUDieLoaded(*CU);
65
66 // Register mofule reference.
67 if (!GlobalData.getOptions().UpdateIndexTablesOnly)
68 ObjectContexts.back()->registerModuleReference(CUDie, Loader,
69 OnCUDieLoaded);
70 }
71 }
72}
73
74void DWARFLinkerImpl::setEstimatedObjfilesAmount(unsigned ObjFilesNum) {
75 ObjectContexts.reserve(N: ObjFilesNum);
76}
77
78Error DWARFLinkerImpl::link() {
79 // UniqueUnitID is initialized by the constructor and must not be reset
80 // here. addObjectFile() may have already handed out IDs to clang module
81 // CUs loaded from .pcm files, and the IDs handed out below must stay
82 // disjoint from those.
83
84 if (Error Err = validateAndUpdateOptions())
85 return Err;
86
87 dwarf::FormParams GlobalFormat = {.Version: GlobalData.getOptions().TargetDWARFVersion,
88 .AddrSize: 0, .Format: dwarf::DwarfFormat::DWARF32};
89 llvm::endianness GlobalEndianness = llvm::endianness::native;
90
91 if (std::optional<std::reference_wrapper<const Triple>> CurTriple =
92 GlobalData.getTargetTriple()) {
93 GlobalEndianness = (*CurTriple).get().isLittleEndian()
94 ? llvm::endianness::little
95 : llvm::endianness::big;
96 }
97 std::optional<uint16_t> Language;
98
99 for (std::unique_ptr<LinkContext> &Context : ObjectContexts) {
100 if (Context->InputDWARFFile.Dwarf == nullptr) {
101 Context->setOutputFormat(Format: Context->getFormParams(), Endianness: GlobalEndianness);
102 continue;
103 }
104
105 if (GlobalData.getOptions().Verbose) {
106 outs() << "DEBUG MAP OBJECT: " << Context->InputDWARFFile.FileName
107 << "\n";
108
109 for (const std::unique_ptr<DWARFUnit> &OrigCU :
110 Context->InputDWARFFile.Dwarf->compile_units()) {
111 outs() << "Input compilation unit:";
112 DIDumpOptions DumpOpts;
113 DumpOpts.ChildRecurseDepth = 0;
114 DumpOpts.Verbose = GlobalData.getOptions().Verbose;
115 OrigCU->getUnitDIE().dump(OS&: outs(), indent: 0, DumpOpts);
116 }
117 }
118
119 // Verify input DWARF if requested.
120 if (GlobalData.getOptions().VerifyInputDWARF)
121 verifyInput(File: Context->InputDWARFFile);
122
123 if (!GlobalData.getTargetTriple())
124 GlobalEndianness = Context->getEndianness();
125 GlobalFormat.AddrSize =
126 std::max(a: GlobalFormat.AddrSize, b: Context->getFormParams().AddrSize);
127
128 Context->setOutputFormat(Format: Context->getFormParams(), Endianness: GlobalEndianness);
129
130 // FIXME: move creation of CompileUnits into the addObjectFile.
131 // This would allow to not scan for context Language and Modules state
132 // twice. And then following handling might be removed.
133 for (const std::unique_ptr<DWARFUnit> &OrigCU :
134 Context->InputDWARFFile.Dwarf->compile_units()) {
135 DWARFDie UnitDie = OrigCU->getUnitDIE();
136
137 if (!Language) {
138 if (std::optional<uint64_t> LangVal = UnitDie.getLanguage())
139 if (isODRLanguage(Language: *LangVal))
140 Language = static_cast<uint16_t>(*LangVal);
141 }
142 }
143
144 // Clang module units decide their ODR availability from their own
145 // language, so they have to be part of this scan as well. A module unit
146 // can be the only ODR unit of a link, and any unit which deduplicates
147 // types requires the artificial type unit to exist.
148 for (const std::unique_ptr<CompileUnit> &Module :
149 Context->ModulesCompileUnits) {
150 if (!Language) {
151 if (std::optional<uint16_t> LangVal = Module->getLanguage())
152 if (isODRLanguage(Language: *LangVal))
153 Language = *LangVal;
154 }
155 }
156 }
157
158 if (GlobalFormat.AddrSize == 0) {
159 if (std::optional<std::reference_wrapper<const Triple>> TargetTriple =
160 GlobalData.getTargetTriple())
161 GlobalFormat.AddrSize = (*TargetTriple).get().isArch32Bit() ? 4 : 8;
162 else
163 GlobalFormat.AddrSize = 8;
164 }
165
166 CommonSections.setOutputFormat(Format: GlobalFormat, Endianness: GlobalEndianness);
167
168 if (!GlobalData.Options.NoODR && Language.has_value()) {
169 llvm::parallel::TaskGroup TGroup;
170 TGroup.spawn(f: [&]() {
171 ArtificialTypeUnit = std::make_unique<TypeUnit>(
172 args&: GlobalData, args: UniqueUnitID++, args&: Language, args&: GlobalFormat, args&: GlobalEndianness);
173 });
174 }
175
176 // Set this process-global once. link() runs per architecture and dsymutil
177 // may run those links concurrently, so assigning it from each would be a
178 // data race; the thread count is the same for every architecture, so the
179 // first assignment suffices. Size the executor from that thread count rather
180 // than the per-architecture CU count, which is moot once it is shared.
181 static llvm::once_flag ParallelStrategyFlag;
182 llvm::call_once(flag&: ParallelStrategyFlag, F: [&] {
183 llvm::parallel::strategy =
184 hardware_concurrency(ThreadCount: GlobalData.getOptions().Threads);
185 });
186
187 // Link object files.
188 if (GlobalData.getOptions().Threads == 1) {
189 for (std::unique_ptr<LinkContext> &Context : ObjectContexts) {
190 // Link object file.
191 if (Error Err = Context->link(ArtificialTypeUnit: ArtificialTypeUnit.get()))
192 GlobalData.error(Err: std::move(Err), Context: Context->InputDWARFFile.FileName);
193 if (Error Err = Context->unloadInput())
194 GlobalData.error(Err: std::move(Err), Context: Context->InputDWARFFile.FileName);
195 }
196 } else {
197 assert(ThreadPool && "setThreadPool() must be called before link()");
198 ThreadPoolTaskGroup Group(*ThreadPool);
199 for (std::unique_ptr<LinkContext> &Context : ObjectContexts)
200 Group.async(F: [&]() {
201 // Link object file.
202 if (Error Err = Context->link(ArtificialTypeUnit: ArtificialTypeUnit.get()))
203 GlobalData.error(Err: std::move(Err), Context: Context->InputDWARFFile.FileName);
204 if (Error Err = Context->unloadInput())
205 GlobalData.error(Err: std::move(Err), Context: Context->InputDWARFFile.FileName);
206 });
207 }
208
209 // Merge staged parseable Swift interface entries into the shared map. Done
210 // serially so that the final map contents and any conflict warnings are
211 // deterministic.
212 if (DWARFLinkerBase::SwiftInterfacesMapTy *SwiftInterfaces =
213 GlobalData.Options.ParseableSwiftInterfaces) {
214 for (std::unique_ptr<LinkContext> &Context : ObjectContexts) {
215 for (std::unique_ptr<CompileUnit> &ModuleUnit :
216 Context->ModulesCompileUnits)
217 ModuleUnit->mergeSwiftInterfaces(Map&: *SwiftInterfaces);
218 for (std::unique_ptr<CompileUnit> &CU : Context->CompileUnits)
219 CU->mergeSwiftInterfaces(Map&: *SwiftInterfaces);
220 }
221 }
222
223 // Build the linker-wide CIE registry, then emit each context's
224 // .debug_frame in parallel. See CIERegistry for the ownership rules.
225 if (!GlobalData.getOptions().UpdateIndexTablesOnly) {
226 LinkContext::CIERegistry CIEs;
227 for (std::unique_ptr<LinkContext> &Context : ObjectContexts)
228 if (Context->FrameScan)
229 Context->registerCIEs(CIEs);
230
231 llvm::parallel::TaskGroup TGroup;
232 for (std::unique_ptr<LinkContext> &Context : ObjectContexts) {
233 if (!Context->FrameScan)
234 continue;
235 TGroup.spawn(f: [&]() {
236 if (Error Err = Context->emitDebugFrame(CIEs))
237 GlobalData.error(Err: std::move(Err), Context: Context->InputDWARFFile.FileName);
238 });
239 }
240 }
241
242 if (ArtificialTypeUnit != nullptr && !ArtificialTypeUnit->getTypePool()
243 .getRoot()
244 ->getValue()
245 .load()
246 ->Children.empty()) {
247 if (GlobalData.getTargetTriple().has_value())
248 if (Error Err = ArtificialTypeUnit->finishCloningAndEmit(
249 TargetTriple: (*GlobalData.getTargetTriple()).get()))
250 return Err;
251 }
252
253 // At this stage each compile units are cloned to their own set of debug
254 // sections. Now, update patches, assign offsets and assemble final file
255 // glueing debug tables from each compile unit.
256 glueCompileUnitsAndWriteToTheOutput();
257
258 return Error::success();
259}
260
261void DWARFLinkerImpl::verifyInput(const DWARFFile &File) {
262 assert(File.Dwarf);
263
264 std::string Buffer;
265 raw_string_ostream OS(Buffer);
266 DIDumpOptions DumpOpts;
267 if (!File.Dwarf->verify(OS, DumpOpts: DumpOpts.noImplicitRecursion())) {
268 if (GlobalData.getOptions().InputVerificationHandler)
269 GlobalData.getOptions().InputVerificationHandler(File, OS.str());
270 }
271}
272
273Error DWARFLinkerImpl::validateAndUpdateOptions() {
274 if (GlobalData.getOptions().TargetDWARFVersion == 0)
275 return createStringError(EC: std::errc::invalid_argument,
276 Fmt: "target DWARF version is not set");
277
278 if (GlobalData.getOptions().Verbose && GlobalData.getOptions().Threads != 1) {
279 GlobalData.Options.Threads = 1;
280 GlobalData.warn(
281 Warning: "set number of threads to 1 to make --verbose to work properly.", Context: "");
282 }
283
284 // Do not do types deduplication in case --update.
285 if (GlobalData.getOptions().UpdateIndexTablesOnly &&
286 !GlobalData.Options.NoODR)
287 GlobalData.Options.NoODR = true;
288
289 return Error::success();
290}
291
292/// Resolve the relative path to a build artifact referenced by DWARF of \p File
293/// by applying DW_AT_comp_dir.
294static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU,
295 const DWARFFile &File) {
296 sys::path::append(
297 path&: Buf, a: File.remapPath(Path: dwarf::toStringRef(V: CU.find(Attr: dwarf::DW_AT_comp_dir))));
298}
299
300static uint64_t getDwoId(const DWARFDie &CUDie) {
301 auto DwoId = dwarf::toUnsigned(
302 V: CUDie.find(Attrs: {dwarf::DW_AT_dwo_id, dwarf::DW_AT_GNU_dwo_id}));
303 if (DwoId)
304 return *DwoId;
305 return 0;
306}
307
308static std::string
309remapPath(StringRef Path,
310 const DWARFLinker::ObjectPrefixMapTy &ObjectPrefixMap) {
311 if (ObjectPrefixMap.empty())
312 return Path.str();
313
314 SmallString<256> p = Path;
315 for (const auto &Entry : ObjectPrefixMap)
316 if (llvm::sys::path::replace_path_prefix(Path&: p, OldPrefix: Entry.first, NewPrefix: Entry.second))
317 break;
318 return p.str().str();
319}
320
321static std::string getPCMFile(const DWARFDie &CUDie,
322 DWARFLinker::ObjectPrefixMapTy *ObjectPrefixMap,
323 const DWARFFile &File) {
324 std::string PCMFile = dwarf::toString(
325 V: CUDie.find(Attrs: {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), Default: "");
326
327 if (PCMFile.empty())
328 return PCMFile;
329
330 if (ObjectPrefixMap)
331 PCMFile = remapPath(Path: PCMFile, ObjectPrefixMap: *ObjectPrefixMap);
332
333 return File.remapPath(Path: PCMFile);
334}
335
336std::pair<bool, bool> DWARFLinkerImpl::LinkContext::isClangModuleRef(
337 const DWARFDie &CUDie, std::string &PCMFile, unsigned Indent, bool Quiet) {
338 if (PCMFile.empty())
339 return std::make_pair(x: false, y: false);
340
341 // Clang module DWARF skeleton CUs abuse this for the path to the module.
342 uint64_t DwoId = getDwoId(CUDie);
343
344 std::string Name = dwarf::toString(V: CUDie.find(Attr: dwarf::DW_AT_name), Default: "");
345 if (Name.empty()) {
346 if (!Quiet)
347 GlobalData.warn(Warning: "anonymous module skeleton CU for " + PCMFile + ".",
348 Context: InputDWARFFile.FileName);
349 return std::make_pair(x: true, y: true);
350 }
351
352 if (!Quiet && GlobalData.getOptions().Verbose) {
353 outs().indent(NumSpaces: Indent);
354 outs() << "Found clang module reference " << PCMFile;
355 }
356
357 auto Cached = ClangModules.find(Key: PCMFile);
358 if (Cached != ClangModules.end()) {
359 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
360 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
361 // ASTFileSignatures will change randomly when a module is rebuilt.
362 if (!Quiet && GlobalData.getOptions().Verbose && (Cached->second != DwoId))
363 GlobalData.warn(
364 Warning: Twine("hash mismatch: this object file was built against a "
365 "different version of the module ") +
366 PCMFile + ".",
367 Context: InputDWARFFile.FileName);
368 if (!Quiet && GlobalData.getOptions().Verbose)
369 outs() << " [cached].\n";
370 return std::make_pair(x: true, y: true);
371 }
372
373 return std::make_pair(x: true, y: false);
374}
375
376/// If this compile unit is really a skeleton CU that points to a
377/// clang module, register it in ClangModules and return true.
378///
379/// A skeleton CU is a CU without children, a DW_AT_gnu_dwo_name
380/// pointing to the module, and a DW_AT_gnu_dwo_id with the module
381/// hash.
382bool DWARFLinkerImpl::LinkContext::registerModuleReference(
383 const DWARFDie &CUDie, ObjFileLoaderTy Loader,
384 CompileUnitHandlerTy OnCUDieLoaded, unsigned Indent) {
385 std::string PCMFile = getPCMFile(
386 CUDie, ObjectPrefixMap: GlobalData.getOptions().ObjectPrefixMap, File: InputDWARFFile);
387 std::pair<bool, bool> IsClangModuleRef =
388 isClangModuleRef(CUDie, PCMFile, Indent, Quiet: false);
389
390 if (!IsClangModuleRef.first)
391 return false;
392
393 if (IsClangModuleRef.second)
394 return true;
395
396 if (GlobalData.getOptions().Verbose)
397 outs() << " ...\n";
398
399 // Cyclic dependencies are disallowed by Clang, but we still
400 // shouldn't run into an infinite loop, so mark it as processed now.
401 ClangModules.insert(KV: {PCMFile, getDwoId(CUDie)});
402
403 if (Error E =
404 loadClangModule(Loader, CUDie, PCMFile, OnCUDieLoaded, Indent: Indent + 2)) {
405 consumeError(Err: std::move(E));
406 return false;
407 }
408 return true;
409}
410
411Error DWARFLinkerImpl::LinkContext::loadClangModule(
412 ObjFileLoaderTy Loader, const DWARFDie &CUDie, const std::string &PCMFile,
413 CompileUnitHandlerTy OnCUDieLoaded, unsigned Indent) {
414
415 uint64_t DwoId = getDwoId(CUDie);
416 std::string ModuleName = dwarf::toString(V: CUDie.find(Attr: dwarf::DW_AT_name), Default: "");
417
418 /// Using a SmallString<0> because loadClangModule() is recursive.
419 SmallString<0> Path(GlobalData.getOptions().PrependPath);
420 if (sys::path::is_relative(path: PCMFile))
421 resolveRelativeObjectPath(Buf&: Path, CU: CUDie, File: InputDWARFFile);
422 sys::path::append(path&: Path, a: PCMFile);
423 // Don't use the cached binary holder because we have no thread-safety
424 // guarantee and the lifetime is limited.
425
426 if (Loader == nullptr) {
427 GlobalData.error(Err: "cann't load clang module: loader is not specified.",
428 Context: InputDWARFFile.FileName);
429 return Error::success();
430 }
431
432 auto ErrOrObj = Loader(InputDWARFFile.FileName, Path);
433 if (!ErrOrObj)
434 return Error::success();
435
436 // The module was built by the same build as the object file referencing it,
437 // so the same prefix map applies.
438 if (!ErrOrObj->PrefixMap)
439 ErrOrObj->PrefixMap = InputDWARFFile.PrefixMap;
440
441 std::unique_ptr<CompileUnit> Unit;
442 for (const auto &CU : ErrOrObj->Dwarf->compile_units()) {
443 OnCUDieLoaded(*CU);
444 // Recursively get all modules imported by this one.
445 auto ChildCUDie = CU->getUnitDIE();
446 if (!ChildCUDie)
447 continue;
448 if (!registerModuleReference(CUDie: ChildCUDie, Loader, OnCUDieLoaded, Indent)) {
449 if (Unit) {
450 std::string Err =
451 (PCMFile +
452 ": Clang modules are expected to have exactly 1 compile unit.\n");
453 GlobalData.error(Err, Context: InputDWARFFile.FileName);
454 return make_error<StringError>(Args&: Err, Args: inconvertibleErrorCode());
455 }
456 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
457 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
458 // ASTFileSignatures will change randomly when a module is rebuilt.
459 uint64_t PCMDwoId = getDwoId(CUDie: ChildCUDie);
460 if (PCMDwoId != DwoId) {
461 if (GlobalData.getOptions().Verbose)
462 GlobalData.warn(
463 Warning: Twine("hash mismatch: this object file was built against a "
464 "different version of the module ") +
465 PCMFile + ".",
466 Context: InputDWARFFile.FileName);
467 // Update the cache entry with the DwoId of the module loaded from disk.
468 ClangModules[PCMFile] = PCMDwoId;
469 }
470
471 // Empty modules units should not be cloned.
472 if (!ChildCUDie.hasChildren())
473 continue;
474
475 // Add this module.
476 Unit = std::make_unique<CompileUnit>(
477 args&: GlobalData, args&: *CU, args: UniqueUnitID.fetch_add(i: 1), args&: ModuleName, args&: *ErrOrObj,
478 args&: getUnitForOffset, args: CU->getFormParams(), args: getEndianness());
479 }
480 }
481
482 if (Unit) {
483 // Incrementing the shared counter needs no synchronization: reaching this
484 // point requires a loader, and only the serial pass over the object files
485 // supplies one.
486 if (Error E = Unit->setPriority(ObjFileIdx: ModuleUnitObjFileIdx, LocalIdx: ModuleUnitIdx++))
487 return E;
488
489 ModulesCompileUnits.emplace_back(Args: std::move(Unit));
490 // Preload line table, as it can't be loaded asynchronously.
491 ModulesCompileUnits.back()->loadLineTable();
492 }
493
494 return Error::success();
495}
496
497Error DWARFLinkerImpl::LinkContext::link(TypeUnit *ArtificialTypeUnit) {
498 InterCUProcessingStarted = false;
499 if (!InputDWARFFile.Dwarf)
500 return Error::success();
501
502 // Preload macro tables, as they can't be loaded asynchronously.
503 InputDWARFFile.Dwarf->getDebugMacinfo();
504 InputDWARFFile.Dwarf->getDebugMacro();
505
506 // Link modules compile units first.
507 parallelForEach(R&: ModulesCompileUnits, Fn: [&](std::unique_ptr<CompileUnit> &Mod) {
508 // A module unit describes DIEs which no address reaches, so nothing marks
509 // it inter-connected and the inter-connected loops below, which iterate
510 // CompileUnits alone, would never advance it.
511 assert(!Mod->isInterconnectedCU() && "module unit is inter-connected");
512 linkSingleCompileUnit(CU&: *Mod, ArtificialTypeUnit);
513 });
514
515 // Check for live relocations. If there is no any live relocation then we
516 // can skip entire object file.
517 if (!GlobalData.getOptions().UpdateIndexTablesOnly &&
518 !InputDWARFFile.Addresses->hasValidRelocs()) {
519 if (GlobalData.getOptions().Verbose)
520 outs() << "No valid relocations found. Skipping.\n";
521 return Error::success();
522 }
523
524 OriginalDebugInfoSize = getInputDebugInfoSize();
525
526 // Create CompileUnit structures to keep information about source
527 // DWARFUnit`s, load line tables.
528 uint64_t LocalCUIdx = 0;
529 for (const auto &OrigCU : InputDWARFFile.Dwarf->compile_units()) {
530 // Load only unit DIE at this stage.
531 auto CUDie = OrigCU->getUnitDIE();
532 std::string PCMFile = getPCMFile(
533 CUDie, ObjectPrefixMap: GlobalData.getOptions().ObjectPrefixMap, File: InputDWARFFile);
534
535 // The !isClangModuleRef condition effectively skips over fully resolved
536 // skeleton units.
537 if (!CUDie || GlobalData.getOptions().UpdateIndexTablesOnly ||
538 !isClangModuleRef(CUDie, PCMFile, Indent: 0, Quiet: true).first) {
539 CompileUnits.emplace_back(Args: std::make_unique<CompileUnit>(
540 args&: GlobalData, args&: *OrigCU, args: UniqueUnitID.fetch_add(i: 1), args: "", args&: InputDWARFFile,
541 args&: getUnitForOffset, args: OrigCU->getFormParams(), args: getEndianness()));
542 if (llvm::Error E =
543 CompileUnits.back()->setPriority(ObjFileIdx: ObjectFileIdx, LocalIdx: LocalCUIdx++))
544 return E;
545
546 // Preload line table, as it can't be loaded asynchronously.
547 CompileUnits.back()->loadLineTable();
548 }
549 };
550
551 HasNewInterconnectedCUs = false;
552
553 // Link self-sufficient compile units and discover inter-connected compile
554 // units.
555 parallelForEach(R&: CompileUnits, Fn: [&](std::unique_ptr<CompileUnit> &CU) {
556 linkSingleCompileUnit(CU&: *CU, ArtificialTypeUnit);
557 });
558
559 // Link all inter-connected units.
560 if (HasNewInterconnectedCUs) {
561 InterCUProcessingStarted = true;
562
563 if (Error Err = finiteLoop(Iteration: [&]() -> Expected<bool> {
564 HasNewInterconnectedCUs = false;
565
566 // Load inter-connected units and find their live roots.
567 parallelForEach(R&: CompileUnits, Fn: [&](std::unique_ptr<CompileUnit> &CU) {
568 if (CU->isInterconnectedCU()) {
569 CU->maybeResetToLoadedStage();
570 linkSingleCompileUnit(CU&: *CU, ArtificialTypeUnit,
571 DoUntilStage: CompileUnit::Stage::LiveRootsCollected);
572 }
573 });
574
575 // Do liveness analysis for inter-connected units.
576 parallelForEach(R&: CompileUnits, Fn: [&](std::unique_ptr<CompileUnit> &CU) {
577 linkSingleCompileUnit(CU&: *CU, ArtificialTypeUnit,
578 DoUntilStage: CompileUnit::Stage::LivenessAnalysisDone);
579 });
580
581 return HasNewInterconnectedCUs.load();
582 }))
583 return Err;
584
585 // Update dependencies.
586 if (Error Err = finiteLoop(Iteration: [&]() -> Expected<bool> {
587 HasNewGlobalDependency = false;
588 parallelForEach(R&: CompileUnits, Fn: [&](std::unique_ptr<CompileUnit> &CU) {
589 linkSingleCompileUnit(
590 CU&: *CU, ArtificialTypeUnit,
591 DoUntilStage: CompileUnit::Stage::UpdateDependenciesCompleteness);
592 });
593 return HasNewGlobalDependency.load();
594 }))
595 return Err;
596 parallelForEach(R&: CompileUnits, Fn: [&](std::unique_ptr<CompileUnit> &CU) {
597 if (CU->isInterconnectedCU() &&
598 CU->getStage() == CompileUnit::Stage::LivenessAnalysisDone)
599 CU->setStage(CompileUnit::Stage::UpdateDependenciesCompleteness);
600 });
601
602 // Assign type names.
603 parallelForEach(R&: CompileUnits, Fn: [&](std::unique_ptr<CompileUnit> &CU) {
604 linkSingleCompileUnit(CU&: *CU, ArtificialTypeUnit,
605 DoUntilStage: CompileUnit::Stage::TypeNamesAssigned);
606 });
607
608 // Clone inter-connected units.
609 parallelForEach(R&: CompileUnits, Fn: [&](std::unique_ptr<CompileUnit> &CU) {
610 linkSingleCompileUnit(CU&: *CU, ArtificialTypeUnit,
611 DoUntilStage: CompileUnit::Stage::Cloned);
612 });
613
614 // Update patches for inter-connected units.
615 parallelForEach(R&: CompileUnits, Fn: [&](std::unique_ptr<CompileUnit> &CU) {
616 linkSingleCompileUnit(CU&: *CU, ArtificialTypeUnit,
617 DoUntilStage: CompileUnit::Stage::PatchesUpdated);
618 });
619
620 // Release data.
621 parallelForEach(R&: CompileUnits, Fn: [&](std::unique_ptr<CompileUnit> &CU) {
622 linkSingleCompileUnit(CU&: *CU, ArtificialTypeUnit,
623 DoUntilStage: CompileUnit::Stage::Cleaned);
624 });
625 }
626
627 if (GlobalData.getOptions().UpdateIndexTablesOnly) {
628 // Emit Invariant sections.
629
630 if (Error Err = emitInvariantSections())
631 return Err;
632 }
633
634 return Error::success();
635}
636
637void DWARFLinkerImpl::LinkContext::linkSingleCompileUnit(
638 CompileUnit &CU, TypeUnit *ArtificialTypeUnit,
639 enum CompileUnit::Stage DoUntilStage) {
640 if (InterCUProcessingStarted != CU.isInterconnectedCU())
641 return;
642
643 if (Error Err = finiteLoop(Iteration: [&]() -> Expected<bool> {
644 if (CU.getStage() >= DoUntilStage)
645 return false;
646
647 switch (CU.getStage()) {
648 case CompileUnit::Stage::CreatedNotLoaded: {
649 // Load input compilation unit DIEs.
650 // Analyze properties of DIEs.
651 if (!CU.loadInputDIEs()) {
652 // We do not need to do liveness analysis for invalid compilation
653 // unit.
654 CU.setStage(CompileUnit::Stage::Skipped);
655 } else {
656 CU.analyzeDWARFStructure();
657
658 // The registerModuleReference() condition effectively skips
659 // over fully resolved skeleton units. This second pass of
660 // registerModuleReferences doesn't do any new work, but it
661 // will collect top-level errors, which are suppressed. Module
662 // warnings were already displayed in the first iteration.
663 //
664 // Runs concurrently, so it must stay a no-op: it may only be
665 // entered when the serial pass in addObjectFile() has already
666 // populated the module map.
667 if (!GlobalData.getOptions().UpdateIndexTablesOnly &&
668 registerModuleReference(
669 CUDie: CU.getOrigUnit().getUnitDIE(), Loader: nullptr,
670 OnCUDieLoaded: [](const DWARFUnit &) {}, Indent: 0))
671 CU.setStage(CompileUnit::Stage::PatchesUpdated);
672 else
673 CU.setStage(CompileUnit::Stage::Loaded);
674 }
675 } break;
676
677 case CompileUnit::Stage::Loaded:
678 CU.collectLiveRoots();
679 CU.setStage(CompileUnit::Stage::LiveRootsCollected);
680 break;
681
682 case CompileUnit::Stage::LiveRootsCollected: {
683 // Mark all the DIEs that need to be present in the generated output.
684 // If ODR requested, build type names.
685 if (!CU.resolveDependenciesAndMarkLiveness(InterCUProcessingStarted,
686 HasNewInterconnectedCUs)) {
687 assert(HasNewInterconnectedCUs &&
688 "Flag indicating new inter-connections is not set");
689 return false;
690 }
691
692 CU.setStage(CompileUnit::Stage::LivenessAnalysisDone);
693 } break;
694
695 case CompileUnit::Stage::LivenessAnalysisDone: {
696 if (InterCUProcessingStarted) {
697 if (CU.updateDependenciesCompleteness())
698 HasNewGlobalDependency = true;
699 return false;
700 } else {
701 if (Error Err = finiteLoop(Iteration: [&]() -> Expected<bool> {
702 return CU.updateDependenciesCompleteness();
703 }))
704 return std::move(Err);
705
706 CU.setStage(CompileUnit::Stage::UpdateDependenciesCompleteness);
707 }
708 } break;
709
710 case CompileUnit::Stage::UpdateDependenciesCompleteness:
711#ifndef NDEBUG
712 CU.verifyDependencies();
713#endif
714
715 if (ArtificialTypeUnit) {
716 if (Error Err =
717 CU.assignTypeNames(TypePoolRef&: ArtificialTypeUnit->getTypePool()))
718 return std::move(Err);
719 }
720 CU.setStage(CompileUnit::Stage::TypeNamesAssigned);
721 break;
722
723 case CompileUnit::Stage::TypeNamesAssigned:
724 // Clone input compile unit.
725 if (CU.isClangModule() ||
726 GlobalData.getOptions().UpdateIndexTablesOnly ||
727 CU.getContainingFile().Addresses->hasValidRelocs()) {
728 if (Error Err = CU.cloneAndEmit(TargetTriple: GlobalData.getTargetTriple(),
729 ArtificialTypeUnit))
730 return std::move(Err);
731 }
732
733 CU.setStage(CompileUnit::Stage::Cloned);
734 break;
735
736 case CompileUnit::Stage::Cloned:
737 // Update DIEs referencies.
738 CU.updateDieRefPatchesWithClonedOffsets();
739
740 // Later than cloning, so that the offsets are final, and no later,
741 // because a unit which got this far can no longer be skipped and have
742 // its section dropped from the output.
743 if (CU.isClangModule())
744 CU.noteModuleAnchors();
745
746 CU.setStage(CompileUnit::Stage::PatchesUpdated);
747 break;
748
749 case CompileUnit::Stage::PatchesUpdated:
750 // Cleanup resources.
751 CU.cleanupDataAfterClonning();
752 CU.setStage(CompileUnit::Stage::Cleaned);
753 break;
754
755 case CompileUnit::Stage::Cleaned:
756 assert(false);
757 break;
758
759 case CompileUnit::Stage::Skipped:
760 // Nothing to do.
761 break;
762 }
763
764 return true;
765 })) {
766 CU.error(Err: std::move(Err));
767 CU.cleanupDataAfterClonning();
768 CU.setStage(CompileUnit::Stage::Skipped);
769 }
770}
771
772Error DWARFLinkerImpl::LinkContext::emitInvariantSections() {
773 if (!GlobalData.getTargetTriple().has_value())
774 return Error::success();
775
776 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugLoc).OS
777 << InputDWARFFile.Dwarf->getDWARFObj().getLocSection().Data;
778 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugLocLists).OS
779 << InputDWARFFile.Dwarf->getDWARFObj().getLoclistsSection().Data;
780 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugRange).OS
781 << InputDWARFFile.Dwarf->getDWARFObj().getRangesSection().Data;
782 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugRngLists).OS
783 << InputDWARFFile.Dwarf->getDWARFObj().getRnglistsSection().Data;
784 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugARanges).OS
785 << InputDWARFFile.Dwarf->getDWARFObj().getArangesSection();
786 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugFrame).OS
787 << InputDWARFFile.Dwarf->getDWARFObj().getFrameSection().Data;
788 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugAddr).OS
789 << InputDWARFFile.Dwarf->getDWARFObj().getAddrSection().Data;
790
791 return Error::success();
792}
793
794Error DWARFLinkerImpl::LinkContext::scanFrameData() {
795 if (GlobalData.getOptions().UpdateIndexTablesOnly)
796 return Error::success();
797 if (!GlobalData.getTargetTriple().has_value())
798 return Error::success();
799
800 if (InputDWARFFile.Dwarf == nullptr)
801 return Error::success();
802 if (CompileUnits.empty())
803 return Error::success();
804
805 const DWARFObject &InputDWARFObj = InputDWARFFile.Dwarf->getDWARFObj();
806
807 StringRef OrigFrameData = InputDWARFObj.getFrameSection().Data;
808 if (OrigFrameData.empty())
809 return Error::success();
810
811 auto Scan = std::make_unique<FrameScanResult>();
812 Scan->FrameData = OrigFrameData;
813 Scan->AddressSize = InputDWARFObj.getAddressSize();
814
815 RangesTy AllUnitsRanges;
816 for (std::unique_ptr<CompileUnit> &Unit : CompileUnits) {
817 for (auto CurRange : Unit->getFunctionRanges())
818 AllUnitsRanges.insert(Range: CurRange.Range, Value: CurRange.Value);
819 }
820
821 StringRef FrameBytes = Scan->FrameData;
822 DataExtractor Data(FrameBytes, InputDWARFObj.isLittleEndian());
823 uint64_t InputOffset = 0;
824 const unsigned SrcAddrSize = Scan->AddressSize;
825 // Width of the CIE_pointer field at the start of every FDE (and of the
826 // CIE_id sentinel at the start of every CIE) in DWARF32 .debug_frame.
827 constexpr unsigned CIEPointerSize = 4;
828
829 // CIEs defined in this input, keyed by their input offsets.
830 DenseMap<uint64_t, StringRef> LocalCIEs;
831 DenseSet<uint64_t> AddedCIEs;
832
833 while (Data.isValidOffset(offset: InputOffset)) {
834 uint64_t EntryOffset = InputOffset;
835 uint32_t InitialLength = Data.getU32(offset_ptr: &InputOffset);
836 if (InitialLength == 0xFFFFFFFF)
837 return createFileError(F: InputDWARFFile.FileName,
838 E: createStringError(EC: std::errc::invalid_argument,
839 Fmt: "Dwarf64 bits not supported"));
840
841 // Reject lengths that don't fit in the input section. substr() saturates
842 // silently, which would otherwise let a malformed length poison the
843 // CIE bytes used as the registry key.
844 if (InitialLength > FrameBytes.size() - InputOffset)
845 return createFileError(
846 F: InputDWARFFile.FileName,
847 E: createStringError(EC: std::errc::invalid_argument,
848 Fmt: "Truncated .debug_frame entry."));
849
850 uint32_t CIEId = Data.getU32(offset_ptr: &InputOffset);
851 if (CIEId == 0xFFFFFFFF) {
852 // This is a CIE, store it.
853 StringRef CIEData = FrameBytes.substr(Start: EntryOffset, N: InitialLength + 4);
854 LocalCIEs[EntryOffset] = CIEData;
855 // The -4 is to account for the CIEId we just read.
856 InputOffset += InitialLength - 4;
857 continue;
858 }
859
860 uint64_t Loc = Data.getUnsigned(offset_ptr: &InputOffset, byte_size: SrcAddrSize);
861
862 // Some compilers seem to emit frame info that doesn't start at
863 // the function entry point, thus we can't just lookup the address
864 // in the debug map. Use the AddressInfo's range map to see if the FDE
865 // describes something that we can relocate.
866 std::optional<AddressRangeValuePair> Range =
867 AllUnitsRanges.getRangeThatContains(Addr: Loc);
868 if (!Range) {
869 // The +4 is to account for the size of the InitialLength field itself.
870 InputOffset = EntryOffset + InitialLength + 4;
871 continue;
872 }
873
874 // This is an FDE, and we have a mapping.
875 StringRef CIEData = LocalCIEs.lookup(Val: CIEId);
876 if (CIEData.empty())
877 return createFileError(
878 F: InputDWARFFile.FileName,
879 E: createStringError(EC: std::errc::invalid_argument,
880 Fmt: "Inconsistent debug_frame content. Dropping."));
881
882 // Reject FDEs whose length doesn't even cover the CIE_pointer and
883 // initial_location fields; otherwise the unsigned subtraction below
884 // would wrap and substr() would saturate to a giant garbage blob.
885 if (InitialLength < CIEPointerSize + SrcAddrSize)
886 return createFileError(F: InputDWARFFile.FileName,
887 E: createStringError(EC: std::errc::invalid_argument,
888 Fmt: "Truncated .debug_frame FDE."));
889
890 // Promote each CIE on first reference; CIEs no FDE references are
891 // dropped from the output.
892 if (AddedCIEs.insert(V: CIEId).second)
893 Scan->CIEs.push_back(Elt: CIEData);
894
895 unsigned FDERemainingBytes = InitialLength - (CIEPointerSize + SrcAddrSize);
896 Scan->FDEs.push_back(Elt: {.CIEBytes: CIEData, .Address: Loc + Range->Value,
897 .Instructions: FrameBytes.substr(Start: InputOffset, N: FDERemainingBytes)});
898 InputOffset += FDERemainingBytes;
899 }
900
901 FrameScan = std::move(Scan);
902 return Error::success();
903}
904
905void DWARFLinkerImpl::LinkContext::registerCIEs(CIERegistry &CIEs) {
906 assert(FrameScan && "registerCIEs called without FrameScan");
907 SectionDescriptor &OutSection =
908 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugFrame);
909
910 uint32_t NextLocalOffset = 0;
911 for (StringRef CIEBytes : FrameScan->CIEs) {
912 auto [It, Inserted] =
913 CIEs.try_emplace(Key: CIEBytes, Args: CIELocation{.OwnerSection: &OutSection, .LocalOffset: NextLocalOffset});
914 if (Inserted) {
915 FrameScan->OwnedCIEs.push_back(Elt: CIEBytes);
916 NextLocalOffset += static_cast<uint32_t>(CIEBytes.size());
917 }
918 }
919}
920
921Error DWARFLinkerImpl::LinkContext::emitDebugFrame(const CIERegistry &CIEs) {
922 assert(FrameScan && "emitDebugFrame called without FrameScan");
923 SectionDescriptor &OutSection =
924 getSectionDescriptor(SectionKind: DebugSectionKind::DebugFrame);
925
926 // Emit owned CIEs at the offsets registerCIEs reserved for them.
927 for (StringRef CIEBytes : FrameScan->OwnedCIEs)
928 OutSection.OS << CIEBytes;
929
930 const dwarf::FormParams FP = OutSection.getFormParams();
931 const unsigned SrcAddrSize = FrameScan->AddressSize;
932
933 for (const FrameScanResult::FDE &FDE : FrameScan->FDEs) {
934 auto It = CIEs.find(Key: FDE.CIEBytes);
935 assert(It != CIEs.end() && "CIE missing from registry");
936 SectionDescriptor *CIEOwnerSection = It->second.OwnerSection;
937 const uint32_t CIELocalOffset = It->second.LocalOffset;
938
939 const uint64_t FDEPos = OutSection.OS.tell();
940 // Note: this guards against a single context's section exceeding the
941 // DWARF32 limit. It does NOT catch the post-glue overflow that would
942 // happen if the concatenated .debug_frame across all contexts pushes
943 // past 4 GB; that case slips through silently because StartOffset is
944 // not yet assigned. A post-glue check would belong in the patch
945 // resolver in OutputSections.cpp.
946 if (FDEPos > FP.getDwarfMaxOffset())
947 return createFileError(
948 F: InputDWARFFile.FileName,
949 E: createStringError(S: ".debug_frame section offset "
950 "0x" +
951 Twine::utohexstr(Val: FDEPos) + " exceeds the " +
952 dwarf::FormatString(Format: FP.Format) + " limit"));
953
954 // CIE_pointer field follows the 4-byte initial_length.
955 OutSection.notePatch(Patch: DebugOffsetPatch{FDEPos + 4, CIEOwnerSection, true});
956
957 emitFDE(CIEOffset: CIELocalOffset, AddrSize: SrcAddrSize, Address: FDE.Address, FDEBytes: FDE.Instructions,
958 Section&: OutSection);
959 }
960
961 FrameScan.reset();
962 return Error::success();
963}
964
965Error DWARFLinkerImpl::LinkContext::unloadInput() {
966 // Scan the input's .debug_frame now, while the DWARFContext is still
967 // loaded, so the later (post-pool) emission pass can run against the
968 // scan result alone.
969 Error ScanErr = scanFrameData();
970 InputDWARFFile.unload();
971 return ScanErr;
972}
973
974/// Emit a FDE into the debug_frame section. \p FDEBytes
975/// contains the FDE data without the length, CIE offset and address
976/// which will be replaced with the parameter values.
977void DWARFLinkerImpl::LinkContext::emitFDE(uint32_t CIEOffset,
978 uint32_t AddrSize, uint64_t Address,
979 StringRef FDEBytes,
980 SectionDescriptor &Section) {
981 Section.emitIntVal(Val: FDEBytes.size() + 4 + AddrSize, Size: 4);
982 Section.emitIntVal(Val: CIEOffset, Size: 4);
983 Section.emitIntVal(Val: Address, Size: AddrSize);
984 Section.OS.write(Ptr: FDEBytes.data(), Size: FDEBytes.size());
985}
986
987void DWARFLinkerImpl::glueCompileUnitsAndWriteToTheOutput() {
988 if (!GlobalData.getTargetTriple().has_value())
989 return;
990 assert(SectionHandler);
991
992 // Go through all object files, all compile units and assign
993 // offsets to them.
994 assignOffsets();
995
996 // Patch size/offsets fields according to the assigned CU offsets.
997 patchOffsetsAndSizes();
998
999 // Emit common sections and write debug tables from all object files/compile
1000 // units into the resulting file.
1001 emitCommonSectionsAndWriteCompileUnitsToTheOutput();
1002
1003 if (ArtificialTypeUnit != nullptr)
1004 ArtificialTypeUnit.reset();
1005
1006 // Write common debug sections into the resulting file.
1007 writeCommonSectionsToTheOutput();
1008
1009 // Cleanup data.
1010 cleanupDataAfterDWARFOutputIsWritten();
1011
1012 if (GlobalData.getOptions().Statistics)
1013 printStatistic();
1014}
1015
1016void DWARFLinkerImpl::printStatistic() {
1017
1018 // For each object file map how many bytes were emitted.
1019 StringMap<DebugInfoSize> SizeByObject;
1020
1021 for (const std::unique_ptr<LinkContext> &Context : ObjectContexts) {
1022 uint64_t AllDebugInfoSectionsSize = 0;
1023
1024 for (std::unique_ptr<CompileUnit> &CU : Context->CompileUnits)
1025 if (std::optional<SectionDescriptor *> DebugInfo =
1026 CU->tryGetSectionDescriptor(SectionKind: DebugSectionKind::DebugInfo))
1027 AllDebugInfoSectionsSize += (*DebugInfo)->getContents().size();
1028
1029 auto &Size = SizeByObject[Context->InputDWARFFile.FileName];
1030 Size.Input = Context->OriginalDebugInfoSize;
1031 Size.Output = AllDebugInfoSectionsSize;
1032 }
1033
1034 // Create a vector sorted in descending order by output size.
1035 std::vector<std::pair<StringRef, DebugInfoSize>> Sorted;
1036 for (auto &E : SizeByObject)
1037 Sorted.emplace_back(args: E.first(), args&: E.second);
1038 llvm::sort(C&: Sorted, Comp: [](auto &LHS, auto &RHS) {
1039 return LHS.second.Output > RHS.second.Output;
1040 });
1041
1042 auto ComputePercentange = [](int64_t Input, int64_t Output) -> float {
1043 const float Difference = Output - Input;
1044 const float Sum = Input + Output;
1045 if (Sum == 0)
1046 return 0;
1047 return (Difference / (Sum / 2));
1048 };
1049
1050 int64_t InputTotal = 0;
1051 int64_t OutputTotal = 0;
1052 const char *FormatStr = "{0,-45} {1,10}b {2,10}b {3,8:P}\n";
1053
1054 // Print header.
1055 outs() << ".debug_info section size (in bytes)\n";
1056 outs() << "----------------------------------------------------------------"
1057 "---------------\n";
1058 outs() << "Filename Object "
1059 " dSYM Change\n";
1060 outs() << "----------------------------------------------------------------"
1061 "---------------\n";
1062
1063 // Print body.
1064 for (auto &E : Sorted) {
1065 InputTotal += E.second.Input;
1066 OutputTotal += E.second.Output;
1067 llvm::outs() << formatv(
1068 Fmt: FormatStr, Vals: sys::path::filename(path: E.first).take_back(N: 45), Vals&: E.second.Input,
1069 Vals&: E.second.Output, Vals: ComputePercentange(E.second.Input, E.second.Output));
1070 }
1071 // Print total and footer.
1072 outs() << "----------------------------------------------------------------"
1073 "---------------\n";
1074 llvm::outs() << formatv(Fmt: FormatStr, Vals: "Total", Vals&: InputTotal, Vals&: OutputTotal,
1075 Vals: ComputePercentange(InputTotal, OutputTotal));
1076 outs() << "----------------------------------------------------------------"
1077 "---------------\n\n";
1078}
1079
1080void DWARFLinkerImpl::assignOffsets() {
1081 llvm::parallel::TaskGroup TGroup;
1082 TGroup.spawn(f: [&]() { assignOffsetsToStrings(); });
1083 TGroup.spawn(f: [&]() { assignOffsetsToSections(); });
1084}
1085
1086void DWARFLinkerImpl::assignOffsetsToStrings() {
1087 size_t CurDebugStrIndex = 1; // start from 1 to take into account zero entry.
1088 uint64_t CurDebugStrOffset =
1089 1; // start from 1 to take into account zero entry.
1090 size_t CurDebugLineStrIndex = 0;
1091 uint64_t CurDebugLineStrOffset = 0;
1092
1093 // Enumerates all strings, add them into the DwarfStringPoolEntry map,
1094 // assign offset and index to the string if it is not indexed yet.
1095 forEachOutputString(StringHandler: [&](StringDestinationKind Kind,
1096 const StringEntry *String) {
1097 switch (Kind) {
1098 case StringDestinationKind::DebugStr: {
1099 DwarfStringPoolEntryWithExtString *Entry = DebugStrStrings.add(String);
1100 assert(Entry != nullptr);
1101
1102 if (!Entry->isIndexed()) {
1103 Entry->Offset = CurDebugStrOffset;
1104 CurDebugStrOffset += Entry->String.size() + 1;
1105 Entry->Index = CurDebugStrIndex++;
1106 }
1107 } break;
1108 case StringDestinationKind::DebugLineStr: {
1109 DwarfStringPoolEntryWithExtString *Entry =
1110 DebugLineStrStrings.add(String);
1111 assert(Entry != nullptr);
1112
1113 if (!Entry->isIndexed()) {
1114 Entry->Offset = CurDebugLineStrOffset;
1115 CurDebugLineStrOffset += Entry->String.size() + 1;
1116 Entry->Index = CurDebugLineStrIndex++;
1117 }
1118 } break;
1119 }
1120 });
1121}
1122
1123void DWARFLinkerImpl::assignOffsetsToSections() {
1124 std::array<uint64_t, SectionKindsNum> SectionSizesAccumulator = {0};
1125
1126 forEachObjectSectionsSet(SectionsSetHandler: [&](OutputSections &UnitSections) {
1127 UnitSections.assignSectionsOffsetAndAccumulateSize(SectionSizesAccumulator);
1128 });
1129}
1130
1131void DWARFLinkerImpl::forEachOutputString(
1132 function_ref<void(StringDestinationKind Kind, const StringEntry *String)>
1133 StringHandler) {
1134 // To save space we do not create any separate string table.
1135 // We use already allocated string patches and accelerator entries:
1136 // enumerate them in natural order and assign offsets.
1137 // ASSUMPTION: strings should be stored into .debug_str/.debug_line_str
1138 // sections in the same order as they were assigned offsets.
1139 forEachCompileUnit(UnitHandler: [&](CompileUnit *CU) {
1140 CU->forEach(Handler: [&](SectionDescriptor &OutSection) {
1141 OutSection.ListDebugStrPatch.forEach(Handler: [&](DebugStrPatch &Patch) {
1142 StringHandler(StringDestinationKind::DebugStr, Patch.String);
1143 });
1144
1145 OutSection.ListDebugLineStrPatch.forEach(Handler: [&](DebugLineStrPatch &Patch) {
1146 StringHandler(StringDestinationKind::DebugLineStr, Patch.String);
1147 });
1148 });
1149
1150 CU->forEachAcceleratorRecord(Handler: [&](DwarfUnit::AccelInfo &Info) {
1151 StringHandler(DebugStr, Info.String);
1152 });
1153 });
1154
1155 if (ArtificialTypeUnit != nullptr) {
1156 ArtificialTypeUnit->forEach(Handler: [&](SectionDescriptor &OutSection) {
1157 OutSection.ListDebugStrPatch.forEach(Handler: [&](DebugStrPatch &Patch) {
1158 StringHandler(StringDestinationKind::DebugStr, Patch.String);
1159 });
1160
1161 OutSection.ListDebugLineStrPatch.forEach(Handler: [&](DebugLineStrPatch &Patch) {
1162 StringHandler(StringDestinationKind::DebugLineStr, Patch.String);
1163 });
1164
1165 OutSection.ListDebugTypeStrPatch.forEach(Handler: [&](DebugTypeStrPatch &Patch) {
1166 if (Patch.Die == nullptr)
1167 return;
1168
1169 TypeEntryBody *TypeEntry = Patch.TypeName->getValue().load();
1170 if (&TypeEntry->getFinalDie() != Patch.Die)
1171 return;
1172
1173 StringHandler(StringDestinationKind::DebugStr, Patch.String);
1174 });
1175
1176 OutSection.ListDebugTypeLineStrPatch.forEach(
1177 Handler: [&](DebugTypeLineStrPatch &Patch) {
1178 if (Patch.Die == nullptr)
1179 return;
1180
1181 TypeEntryBody *TypeEntry = Patch.TypeName->getValue().load();
1182 if (&TypeEntry->getFinalDie() != Patch.Die)
1183 return;
1184
1185 StringHandler(StringDestinationKind::DebugStr, Patch.String);
1186 });
1187 });
1188 }
1189}
1190
1191void DWARFLinkerImpl::forEachObjectSectionsSet(
1192 function_ref<void(OutputSections &)> SectionsSetHandler) {
1193 // Handle artificial type unit first.
1194 if (ArtificialTypeUnit != nullptr)
1195 SectionsSetHandler(*ArtificialTypeUnit);
1196
1197 // Then all modules(before regular compilation units).
1198 for (const std::unique_ptr<LinkContext> &Context : ObjectContexts)
1199 for (std::unique_ptr<CompileUnit> &ModuleUnit :
1200 Context->ModulesCompileUnits)
1201 if (ModuleUnit->getStage() != CompileUnit::Stage::Skipped)
1202 SectionsSetHandler(*ModuleUnit);
1203
1204 // Finally all compilation units.
1205 for (const std::unique_ptr<LinkContext> &Context : ObjectContexts) {
1206 // Handle object file common sections.
1207 SectionsSetHandler(*Context);
1208
1209 // Handle compilation units.
1210 for (std::unique_ptr<CompileUnit> &CU : Context->CompileUnits)
1211 if (CU->getStage() != CompileUnit::Stage::Skipped)
1212 SectionsSetHandler(*CU);
1213 }
1214}
1215
1216void DWARFLinkerImpl::forEachCompileAndTypeUnit(
1217 function_ref<void(DwarfUnit *CU)> UnitHandler) {
1218 if (ArtificialTypeUnit != nullptr)
1219 UnitHandler(ArtificialTypeUnit.get());
1220
1221 // Enumerate module units.
1222 for (const std::unique_ptr<LinkContext> &Context : ObjectContexts)
1223 for (std::unique_ptr<CompileUnit> &ModuleUnit :
1224 Context->ModulesCompileUnits)
1225 if (ModuleUnit->getStage() != CompileUnit::Stage::Skipped)
1226 UnitHandler(ModuleUnit.get());
1227
1228 // Enumerate compile units.
1229 for (const std::unique_ptr<LinkContext> &Context : ObjectContexts)
1230 for (std::unique_ptr<CompileUnit> &CU : Context->CompileUnits)
1231 if (CU->getStage() != CompileUnit::Stage::Skipped)
1232 UnitHandler(CU.get());
1233}
1234
1235void DWARFLinkerImpl::forEachCompileUnit(
1236 function_ref<void(CompileUnit *CU)> UnitHandler) {
1237 // Enumerate module units.
1238 for (const std::unique_ptr<LinkContext> &Context : ObjectContexts)
1239 for (std::unique_ptr<CompileUnit> &ModuleUnit :
1240 Context->ModulesCompileUnits)
1241 if (ModuleUnit->getStage() != CompileUnit::Stage::Skipped)
1242 UnitHandler(ModuleUnit.get());
1243
1244 // Enumerate compile units.
1245 for (const std::unique_ptr<LinkContext> &Context : ObjectContexts)
1246 for (std::unique_ptr<CompileUnit> &CU : Context->CompileUnits)
1247 if (CU->getStage() != CompileUnit::Stage::Skipped)
1248 UnitHandler(CU.get());
1249}
1250
1251void DWARFLinkerImpl::patchOffsetsAndSizes() {
1252 forEachObjectSectionsSet(SectionsSetHandler: [&](OutputSections &SectionsSet) {
1253 SectionsSet.forEach(Handler: [&](SectionDescriptor &OutSection) {
1254 SectionsSet.applyPatches(Section&: OutSection, DebugStrStrings, DebugLineStrStrings,
1255 TypeUnitPtr: ArtificialTypeUnit.get());
1256 });
1257 });
1258}
1259
1260void DWARFLinkerImpl::emitCommonSectionsAndWriteCompileUnitsToTheOutput() {
1261 llvm::parallel::TaskGroup TG;
1262
1263 // Create section descriptors ahead if they are not exist at the moment.
1264 // SectionDescriptors container is not thread safe. Thus we should be sure
1265 // that descriptors would not be created in following parallel tasks.
1266
1267 CommonSections.getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugStr);
1268 CommonSections.getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugLineStr);
1269
1270 if (llvm::is_contained(Range&: GlobalData.Options.AccelTables,
1271 Element: AccelTableKind::Apple)) {
1272 CommonSections.getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::AppleNames);
1273 CommonSections.getOrCreateSectionDescriptor(
1274 SectionKind: DebugSectionKind::AppleNamespaces);
1275 CommonSections.getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::AppleObjC);
1276 CommonSections.getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::AppleTypes);
1277 }
1278
1279 if (llvm::is_contained(Range&: GlobalData.Options.AccelTables,
1280 Element: AccelTableKind::DebugNames))
1281 CommonSections.getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugNames);
1282
1283 // Emit .debug_str and .debug_line_str sections.
1284 TG.spawn(f: [&]() { emitStringSections(); });
1285
1286 if (llvm::is_contained(Range&: GlobalData.Options.AccelTables,
1287 Element: AccelTableKind::Apple)) {
1288 // Emit apple accelerator sections.
1289 TG.spawn(f: [&]() {
1290 emitAppleAcceleratorSections(TargetTriple: (*GlobalData.getTargetTriple()).get());
1291 });
1292 }
1293
1294 if (llvm::is_contained(Range&: GlobalData.Options.AccelTables,
1295 Element: AccelTableKind::DebugNames)) {
1296 // Emit .debug_names section.
1297 TG.spawn(f: [&]() {
1298 emitDWARFv5DebugNamesSection(TargetTriple: (*GlobalData.getTargetTriple()).get());
1299 });
1300 }
1301
1302 // Write compile units to the output file.
1303 TG.spawn(f: [&]() { writeCompileUnitsToTheOutput(); });
1304}
1305
1306void DWARFLinkerImpl::emitStringSections() {
1307 uint64_t DebugStrNextOffset = 0;
1308 uint64_t DebugLineStrNextOffset = 0;
1309
1310 // Emit zero length string. Accelerator tables does not work correctly
1311 // if the first string is not zero length string.
1312 CommonSections.getSectionDescriptor(SectionKind: DebugSectionKind::DebugStr)
1313 .emitInplaceString(String: "");
1314 DebugStrNextOffset++;
1315
1316 forEachOutputString(
1317 StringHandler: [&](StringDestinationKind Kind, const StringEntry *String) {
1318 switch (Kind) {
1319 case StringDestinationKind::DebugStr: {
1320 DwarfStringPoolEntryWithExtString *StringToEmit =
1321 DebugStrStrings.getExistingEntry(String);
1322 assert(StringToEmit->isIndexed());
1323
1324 // Strings may be repeated. Use accumulated DebugStrNextOffset
1325 // to understand whether corresponding string is already emitted.
1326 // Skip string if its offset less than accumulated offset.
1327 if (StringToEmit->Offset >= DebugStrNextOffset) {
1328 DebugStrNextOffset =
1329 StringToEmit->Offset + StringToEmit->String.size() + 1;
1330 // Emit the string itself.
1331 CommonSections.getSectionDescriptor(SectionKind: DebugSectionKind::DebugStr)
1332 .emitInplaceString(String: StringToEmit->String);
1333 }
1334 } break;
1335 case StringDestinationKind::DebugLineStr: {
1336 DwarfStringPoolEntryWithExtString *StringToEmit =
1337 DebugLineStrStrings.getExistingEntry(String);
1338 assert(StringToEmit->isIndexed());
1339
1340 // Strings may be repeated. Use accumulated DebugLineStrStrings
1341 // to understand whether corresponding string is already emitted.
1342 // Skip string if its offset less than accumulated offset.
1343 if (StringToEmit->Offset >= DebugLineStrNextOffset) {
1344 DebugLineStrNextOffset =
1345 StringToEmit->Offset + StringToEmit->String.size() + 1;
1346 // Emit the string itself.
1347 CommonSections.getSectionDescriptor(SectionKind: DebugSectionKind::DebugLineStr)
1348 .emitInplaceString(String: StringToEmit->String);
1349 }
1350 } break;
1351 }
1352 });
1353}
1354
1355void DWARFLinkerImpl::emitAppleAcceleratorSections(const Triple &TargetTriple) {
1356 AccelTable<AppleAccelTableStaticOffsetData> AppleNamespaces;
1357 AccelTable<AppleAccelTableStaticOffsetData> AppleNames;
1358 AccelTable<AppleAccelTableStaticOffsetData> AppleObjC;
1359 AccelTable<AppleAccelTableStaticTypeData> AppleTypes;
1360
1361 forEachCompileAndTypeUnit(UnitHandler: [&](DwarfUnit *CU) {
1362 CU->forEachAcceleratorRecord(Handler: [&](const DwarfUnit::AccelInfo &Info) {
1363 uint64_t OutOffset = Info.OutOffset;
1364 switch (Info.Type) {
1365 case DwarfUnit::AccelType::None: {
1366 llvm_unreachable("Unknown accelerator record");
1367 } break;
1368 case DwarfUnit::AccelType::Namespace: {
1369 AppleNamespaces.addName(
1370 Name: *DebugStrStrings.getExistingEntry(String: Info.String),
1371 Args: CU->getSectionDescriptor(SectionKind: DebugSectionKind::DebugInfo).StartOffset +
1372 OutOffset);
1373 } break;
1374 case DwarfUnit::AccelType::Name: {
1375 AppleNames.addName(
1376 Name: *DebugStrStrings.getExistingEntry(String: Info.String),
1377 Args: CU->getSectionDescriptor(SectionKind: DebugSectionKind::DebugInfo).StartOffset +
1378 OutOffset);
1379 } break;
1380 case DwarfUnit::AccelType::ObjC: {
1381 AppleObjC.addName(
1382 Name: *DebugStrStrings.getExistingEntry(String: Info.String),
1383 Args: CU->getSectionDescriptor(SectionKind: DebugSectionKind::DebugInfo).StartOffset +
1384 OutOffset);
1385 } break;
1386 case DwarfUnit::AccelType::Type: {
1387 AppleTypes.addName(
1388 Name: *DebugStrStrings.getExistingEntry(String: Info.String),
1389 Args: CU->getSectionDescriptor(SectionKind: DebugSectionKind::DebugInfo).StartOffset +
1390 OutOffset,
1391 Args: Info.Tag,
1392 Args: Info.ObjcClassImplementation ? dwarf::DW_FLAG_type_implementation
1393 : 0,
1394 Args: Info.QualifiedNameHash);
1395 } break;
1396 }
1397 });
1398 });
1399
1400 {
1401 // FIXME: we use AsmPrinter to emit accelerator sections.
1402 // It might be beneficial to directly emit accelerator data
1403 // to the raw_svector_ostream.
1404 SectionDescriptor &OutSection =
1405 CommonSections.getSectionDescriptor(SectionKind: DebugSectionKind::AppleNamespaces);
1406 DwarfEmitterImpl Emitter(DWARFLinker::OutputFileType::Object,
1407 OutSection.OS);
1408 if (Error Err = Emitter.init(TheTriple: TargetTriple, Swift5ReflectionSegmentName: "__DWARF")) {
1409 consumeError(Err: std::move(Err));
1410 return;
1411 }
1412
1413 // Emit table.
1414 Emitter.emitAppleNamespaces(Table&: AppleNamespaces);
1415 Emitter.finish();
1416
1417 // Set start offset and size for output section.
1418 OutSection.setSizesForSectionCreatedByAsmPrinter();
1419 }
1420
1421 {
1422 // FIXME: we use AsmPrinter to emit accelerator sections.
1423 // It might be beneficial to directly emit accelerator data
1424 // to the raw_svector_ostream.
1425 SectionDescriptor &OutSection =
1426 CommonSections.getSectionDescriptor(SectionKind: DebugSectionKind::AppleNames);
1427 DwarfEmitterImpl Emitter(DWARFLinker::OutputFileType::Object,
1428 OutSection.OS);
1429 if (Error Err = Emitter.init(TheTriple: TargetTriple, Swift5ReflectionSegmentName: "__DWARF")) {
1430 consumeError(Err: std::move(Err));
1431 return;
1432 }
1433
1434 // Emit table.
1435 Emitter.emitAppleNames(Table&: AppleNames);
1436 Emitter.finish();
1437
1438 // Set start offset ans size for output section.
1439 OutSection.setSizesForSectionCreatedByAsmPrinter();
1440 }
1441
1442 {
1443 // FIXME: we use AsmPrinter to emit accelerator sections.
1444 // It might be beneficial to directly emit accelerator data
1445 // to the raw_svector_ostream.
1446 SectionDescriptor &OutSection =
1447 CommonSections.getSectionDescriptor(SectionKind: DebugSectionKind::AppleObjC);
1448 DwarfEmitterImpl Emitter(DWARFLinker::OutputFileType::Object,
1449 OutSection.OS);
1450 if (Error Err = Emitter.init(TheTriple: TargetTriple, Swift5ReflectionSegmentName: "__DWARF")) {
1451 consumeError(Err: std::move(Err));
1452 return;
1453 }
1454
1455 // Emit table.
1456 Emitter.emitAppleObjc(Table&: AppleObjC);
1457 Emitter.finish();
1458
1459 // Set start offset ans size for output section.
1460 OutSection.setSizesForSectionCreatedByAsmPrinter();
1461 }
1462
1463 {
1464 // FIXME: we use AsmPrinter to emit accelerator sections.
1465 // It might be beneficial to directly emit accelerator data
1466 // to the raw_svector_ostream.
1467 SectionDescriptor &OutSection =
1468 CommonSections.getSectionDescriptor(SectionKind: DebugSectionKind::AppleTypes);
1469 DwarfEmitterImpl Emitter(DWARFLinker::OutputFileType::Object,
1470 OutSection.OS);
1471 if (Error Err = Emitter.init(TheTriple: TargetTriple, Swift5ReflectionSegmentName: "__DWARF")) {
1472 consumeError(Err: std::move(Err));
1473 return;
1474 }
1475
1476 // Emit table.
1477 Emitter.emitAppleTypes(Table&: AppleTypes);
1478 Emitter.finish();
1479
1480 // Set start offset ans size for output section.
1481 OutSection.setSizesForSectionCreatedByAsmPrinter();
1482 }
1483}
1484
1485void DWARFLinkerImpl::emitDWARFv5DebugNamesSection(const Triple &TargetTriple) {
1486 std::unique_ptr<DWARF5AccelTable> DebugNames;
1487
1488 DebugNamesUnitsOffsets CompUnits;
1489 CompUnitIDToIdx CUidToIdx;
1490
1491 unsigned Id = 0;
1492
1493 forEachCompileAndTypeUnit(UnitHandler: [&](DwarfUnit *CU) {
1494 bool HasRecords = false;
1495 CU->forEachAcceleratorRecord(Handler: [&](const DwarfUnit::AccelInfo &Info) {
1496 if (DebugNames == nullptr)
1497 DebugNames = std::make_unique<DWARF5AccelTable>();
1498
1499 HasRecords = true;
1500 switch (Info.Type) {
1501 case DwarfUnit::AccelType::Name:
1502 case DwarfUnit::AccelType::Namespace:
1503 case DwarfUnit::AccelType::Type: {
1504 DebugNames->addName(Name: *DebugStrStrings.getExistingEntry(String: Info.String),
1505 Args: Info.OutOffset, Args: Info.ParentOffset, Args: Info.Tag,
1506 Args: CU->getUniqueID(),
1507 Args: CU->getTag() == dwarf::DW_TAG_type_unit);
1508 } break;
1509
1510 default:
1511 break; // Nothing to do.
1512 };
1513 });
1514
1515 if (HasRecords) {
1516 CompUnits.push_back(
1517 x: CU->getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugInfo)
1518 .StartOffset);
1519 CUidToIdx[CU->getUniqueID()] = Id++;
1520 }
1521 });
1522
1523 if (DebugNames != nullptr) {
1524 // FIXME: we use AsmPrinter to emit accelerator sections.
1525 // It might be beneficial to directly emit accelerator data
1526 // to the raw_svector_ostream.
1527 SectionDescriptor &OutSection =
1528 CommonSections.getSectionDescriptor(SectionKind: DebugSectionKind::DebugNames);
1529 DwarfEmitterImpl Emitter(DWARFLinker::OutputFileType::Object,
1530 OutSection.OS);
1531 if (Error Err = Emitter.init(TheTriple: TargetTriple, Swift5ReflectionSegmentName: "__DWARF")) {
1532 consumeError(Err: std::move(Err));
1533 return;
1534 }
1535
1536 // Emit table.
1537 Emitter.emitDebugNames(Table&: *DebugNames, CUOffsets&: CompUnits, UnitIDToIdxMap&: CUidToIdx);
1538 Emitter.finish();
1539
1540 // Set start offset ans size for output section.
1541 OutSection.setSizesForSectionCreatedByAsmPrinter();
1542 }
1543}
1544
1545void DWARFLinkerImpl::cleanupDataAfterDWARFOutputIsWritten() {
1546 GlobalData.getStringPool().clear();
1547 DebugStrStrings.clear();
1548 DebugLineStrStrings.clear();
1549}
1550
1551void DWARFLinkerImpl::writeCompileUnitsToTheOutput() {
1552 // Enumerate all sections and store them into the final emitter.
1553 forEachObjectSectionsSet(SectionsSetHandler: [&](OutputSections &Sections) {
1554 Sections.forEach(Handler: [&](std::shared_ptr<SectionDescriptor> OutSection) {
1555 // Emit section content.
1556 SectionHandler(OutSection);
1557 });
1558 });
1559}
1560
1561void DWARFLinkerImpl::writeCommonSectionsToTheOutput() {
1562 CommonSections.forEach(Handler: [&](std::shared_ptr<SectionDescriptor> OutSection) {
1563 SectionHandler(OutSection);
1564 });
1565}
1566