1//=== DebugInfoLinker.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 "DebugInfoLinker.h"
10#include "Error.h"
11#include "llvm/ADT/StringSwitch.h"
12#include "llvm/DWARFLinker/Classic/DWARFLinker.h"
13#include "llvm/DWARFLinker/Classic/DWARFStreamer.h"
14#include "llvm/DWARFLinker/Parallel/DWARFLinker.h"
15#include "llvm/DebugInfo/DWARF/DWARFContext.h"
16#include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h"
17#include "llvm/Object/ObjectFile.h"
18#include "llvm/Support/FormatVariadic.h"
19#include "llvm/Support/ThreadPool.h"
20#include "llvm/Support/Threading.h"
21#include <memory>
22#include <optional>
23#include <vector>
24
25namespace llvm {
26using namespace dwarf_linker;
27
28namespace dwarfutil {
29
30// ObjFileAddressMap allows to check whether specified DIE referencing
31// dead addresses. It uses tombstone values to determine dead addresses.
32// The concrete values of tombstone constants were discussed in
33// https://reviews.llvm.org/D81784 and https://reviews.llvm.org/D84825.
34// So we use following values as indicators of dead addresses:
35//
36// bfd: (LowPC == 0) or (LowPC == 1 and HighPC == 1 and DWARF v4 (or less))
37// or ([LowPC, HighPC] is not inside address ranges of .text sections).
38//
39// maxpc: (LowPC == -1) or (LowPC == -2 and DWARF v4 (or less))
40// That value is assumed to be compatible with
41// http://www.dwarfstd.org/ShowIssue.php?issue=200609.1
42//
43// exec: [LowPC, HighPC] is not inside address ranges of .text sections
44//
45// universal: maxpc and bfd
46class ObjFileAddressMap : public AddressesMap {
47public:
48 ObjFileAddressMap(DWARFContext &Context, const Options &Options,
49 object::ObjectFile &ObjFile)
50 : Opts(Options) {
51 // Remember addresses of existing text sections.
52 for (const object::SectionRef &Sect : ObjFile.sections()) {
53 if (!Sect.isText())
54 continue;
55 const uint64_t Size = Sect.getSize();
56 if (Size == 0)
57 continue;
58 const uint64_t StartAddr = Sect.getAddress();
59 TextAddressRanges.insert(Range: {StartAddr, StartAddr + Size});
60 }
61
62 // Check CU address ranges for tombstone value.
63 for (std::unique_ptr<DWARFUnit> &CU : Context.compile_units()) {
64 Expected<llvm::DWARFAddressRangesVector> ARanges =
65 CU->getUnitDIE().getAddressRanges();
66 if (!ARanges) {
67 llvm::consumeError(Err: ARanges.takeError());
68 continue;
69 }
70
71 for (auto &Range : *ARanges) {
72 if (!isDeadAddressRange(LowPC: Range.LowPC, HighPC: Range.HighPC, Version: CU->getVersion(),
73 Tombstone: Options.Tombstone, AddressByteSize: CU->getAddressByteSize())) {
74 HasValidAddressRanges = true;
75 break;
76 }
77 }
78
79 if (HasValidAddressRanges)
80 break;
81 }
82 }
83
84 // should be renamed into has valid address ranges
85 bool hasValidRelocs() override { return HasValidAddressRanges; }
86
87 std::optional<int64_t> getSubprogramRelocAdjustment(const DWARFDie &DIE,
88 bool Verbose) override {
89 assert((DIE.getTag() == dwarf::DW_TAG_subprogram ||
90 DIE.getTag() == dwarf::DW_TAG_label) &&
91 "Wrong type of input die");
92
93 if (std::optional<uint64_t> LowPC =
94 dwarf::toAddress(V: DIE.find(Attr: dwarf::DW_AT_low_pc))) {
95 if (!isDeadAddress(LowPC: *LowPC, Version: DIE.getDwarfUnit()->getVersion(),
96 Tombstone: Opts.Tombstone,
97 AddressByteSize: DIE.getDwarfUnit()->getAddressByteSize()))
98 // Relocation value for the linked binary is 0.
99 return 0;
100 }
101
102 return std::nullopt;
103 }
104
105 std::optional<int64_t>
106 getExprOpAddressRelocAdjustment(DWARFUnit &U,
107 const DWARFExpression::Operation &Op,
108 uint64_t, uint64_t, bool Verbose) override {
109 switch (Op.getCode()) {
110 default: {
111 assert(false && "Specified operation does not have address operand");
112 } break;
113 case dwarf::DW_OP_const2u:
114 case dwarf::DW_OP_const4u:
115 case dwarf::DW_OP_const8u:
116 case dwarf::DW_OP_const2s:
117 case dwarf::DW_OP_const4s:
118 case dwarf::DW_OP_const8s:
119 case dwarf::DW_OP_addr: {
120 if (!isDeadAddress(LowPC: Op.getRawOperand(Idx: 0), Version: U.getVersion(), Tombstone: Opts.Tombstone,
121 AddressByteSize: U.getAddressByteSize()))
122 // Relocation value for the linked binary is 0.
123 return 0;
124 } break;
125 case dwarf::DW_OP_constx:
126 case dwarf::DW_OP_addrx: {
127 if (std::optional<object::SectionedAddress> Address =
128 U.getAddrOffsetSectionItem(Index: Op.getRawOperand(Idx: 0))) {
129 if (!isDeadAddress(LowPC: Address->Address, Version: U.getVersion(), Tombstone: Opts.Tombstone,
130 AddressByteSize: U.getAddressByteSize()))
131 // Relocation value for the linked binary is 0.
132 return 0;
133 }
134 } break;
135 }
136
137 return std::nullopt;
138 }
139
140 std::optional<StringRef> getLibraryInstallName() override {
141 return std::nullopt;
142 }
143
144 bool applyValidRelocs(MutableArrayRef<char>, uint64_t, bool) override {
145 // no need to apply relocations to the linked binary.
146 return false;
147 }
148
149 bool needToSaveValidRelocs() override { return false; }
150
151 bool updateAndSaveValidRelocs(RelocSectionKind, RelocSectionKind, uint64_t,
152 int64_t, uint64_t, uint64_t) override {
153 return false;
154 }
155
156 void clear() override {}
157
158protected:
159 // returns true if specified address range is inside address ranges
160 // of executable sections.
161 bool isInsideExecutableSectionsAddressRange(uint64_t LowPC,
162 std::optional<uint64_t> HighPC) {
163 std::optional<AddressRange> Range =
164 TextAddressRanges.getRangeThatContains(Addr: LowPC);
165
166 if (HighPC)
167 return Range.has_value() && Range->end() >= *HighPC;
168
169 return Range.has_value();
170 }
171
172 uint64_t isBFDDeadAddressRange(uint64_t LowPC, std::optional<uint64_t> HighPC,
173 uint16_t Version) {
174 if (LowPC == 0)
175 return true;
176
177 if ((Version <= 4) && HighPC && (LowPC == 1 && *HighPC == 1))
178 return true;
179
180 return !isInsideExecutableSectionsAddressRange(LowPC, HighPC);
181 }
182
183 uint64_t isMAXPCDeadAddressRange(uint64_t LowPC,
184 std::optional<uint64_t> HighPC,
185 uint16_t Version, uint8_t AddressByteSize) {
186 if (Version <= 4 && HighPC) {
187 if (LowPC == (dwarf::computeTombstoneAddress(AddressByteSize) - 1))
188 return true;
189 } else if (LowPC == dwarf::computeTombstoneAddress(AddressByteSize))
190 return true;
191
192 if (!isInsideExecutableSectionsAddressRange(LowPC, HighPC))
193 warning(Message: "Address referencing invalid text section is not marked with "
194 "tombstone value");
195
196 return false;
197 }
198
199 bool isDeadAddressRange(uint64_t LowPC, std::optional<uint64_t> HighPC,
200 uint16_t Version, TombstoneKind Tombstone,
201 uint8_t AddressByteSize) {
202 switch (Tombstone) {
203 case TombstoneKind::BFD:
204 return isBFDDeadAddressRange(LowPC, HighPC, Version);
205 case TombstoneKind::MaxPC:
206 return isMAXPCDeadAddressRange(LowPC, HighPC, Version, AddressByteSize);
207 case TombstoneKind::Universal:
208 return isBFDDeadAddressRange(LowPC, HighPC, Version) ||
209 isMAXPCDeadAddressRange(LowPC, HighPC, Version, AddressByteSize);
210 case TombstoneKind::Exec:
211 return !isInsideExecutableSectionsAddressRange(LowPC, HighPC);
212 }
213
214 llvm_unreachable("Unknown tombstone value");
215 }
216
217 bool isDeadAddress(uint64_t LowPC, uint16_t Version, TombstoneKind Tombstone,
218 uint8_t AddressByteSize) {
219 return isDeadAddressRange(LowPC, HighPC: std::nullopt, Version, Tombstone,
220 AddressByteSize);
221 }
222
223private:
224 AddressRanges TextAddressRanges;
225 const Options &Opts;
226 bool HasValidAddressRanges = false;
227};
228
229static bool knownByDWARFUtil(StringRef SecName) {
230 return llvm::StringSwitch<bool>(SecName)
231 .Case(S: ".debug_info", Value: true)
232 .Case(S: ".debug_types", Value: true)
233 .Case(S: ".debug_abbrev", Value: true)
234 .Case(S: ".debug_loc", Value: true)
235 .Case(S: ".debug_loclists", Value: true)
236 .Case(S: ".debug_frame", Value: true)
237 .Case(S: ".debug_aranges", Value: true)
238 .Case(S: ".debug_ranges", Value: true)
239 .Case(S: ".debug_rnglists", Value: true)
240 .Case(S: ".debug_line", Value: true)
241 .Case(S: ".debug_line_str", Value: true)
242 .Case(S: ".debug_addr", Value: true)
243 .Case(S: ".debug_macro", Value: true)
244 .Case(S: ".debug_macinfo", Value: true)
245 .Case(S: ".debug_str", Value: true)
246 .Case(S: ".debug_str_offsets", Value: true)
247 .Case(S: ".debug_pubnames", Value: true)
248 .Case(S: ".debug_pubtypes", Value: true)
249 .Case(S: ".debug_names", Value: true)
250 .Default(Value: false);
251}
252
253template <typename AccelTableKind>
254static std::optional<AccelTableKind>
255getAcceleratorTableKind(StringRef SecName) {
256 return llvm::StringSwitch<std::optional<AccelTableKind>>(SecName)
257 .Case(".debug_pubnames", AccelTableKind::Pub)
258 .Case(".debug_pubtypes", AccelTableKind::Pub)
259 .Case(".debug_names", AccelTableKind::DebugNames)
260 .Default(std::nullopt);
261}
262
263static std::string getMessageForReplacedAcceleratorTables(
264 SmallVector<StringRef> &AccelTableNamesToReplace,
265 DwarfUtilAccelKind TargetTable) {
266 std::string Message;
267
268 Message += "'";
269 for (StringRef Name : AccelTableNamesToReplace) {
270 if (Message.size() > 1)
271 Message += ", ";
272 Message += Name;
273 }
274
275 Message += "' will be replaced with requested ";
276
277 switch (TargetTable) {
278 case DwarfUtilAccelKind::DWARF:
279 Message += ".debug_names table";
280 break;
281
282 default:
283 assert(false);
284 }
285
286 return Message;
287}
288
289static std::string getMessageForDeletedAcceleratorTables(
290 SmallVector<StringRef> &AccelTableNamesToReplace) {
291 std::string Message;
292
293 Message += "'";
294 for (StringRef Name : AccelTableNamesToReplace) {
295 if (Message.size() > 1)
296 Message += ", ";
297 Message += Name;
298 }
299
300 Message += "' will be deleted as no accelerator tables are requested";
301
302 return Message;
303}
304
305template <typename Linker>
306Error linkDebugInfoImpl(object::ObjectFile &File, const Options &Options,
307 raw_pwrite_stream &OutStream) {
308 std::mutex ErrorHandlerMutex;
309
310 auto ReportWarn = [&](const Twine &Message, StringRef Context,
311 const DWARFDie *Die) {
312 // FIXME: implement warning logging which does not block other threads.
313 if (!ErrorHandlerMutex.try_lock())
314 return;
315
316 warning(Message, Prefix: Context);
317 if (Options.Verbose && Die) {
318 DIDumpOptions DumpOpts;
319 DumpOpts.ChildRecurseDepth = 0;
320 DumpOpts.Verbose = Options.Verbose;
321
322 WithColor::note() << " in DIE:\n";
323 Die->dump(OS&: errs(), /*Indent=*/indent: 6, DumpOpts);
324 }
325 ErrorHandlerMutex.unlock();
326 };
327 auto ReportErr = [&](const Twine &Message, StringRef Context,
328 const DWARFDie *) {
329 // FIXME: implement error logging which does not block other threads.
330 if (!ErrorHandlerMutex.try_lock())
331 return;
332
333 WithColor::error(OS&: errs(), Prefix: Context) << Message << '\n';
334 ErrorHandlerMutex.unlock();
335 };
336
337 // Create DWARF linker.
338 std::unique_ptr<Linker> DebugInfoLinker =
339 Linker::createLinker(ReportErr, ReportWarn);
340
341 Triple TargetTriple = File.makeTriple();
342 std::unique_ptr<classic::DwarfStreamer> Streamer;
343 if (Expected<std::unique_ptr<classic::DwarfStreamer>> StreamerOrErr =
344 classic::DwarfStreamer::createStreamer(TheTriple: TargetTriple,
345 FileType: Linker::OutputFileType::Object,
346 OutFile&: OutStream, Warning: ReportWarn))
347 Streamer = std::move(*StreamerOrErr);
348 else
349 return StreamerOrErr.takeError();
350
351 // The parallel linker links the object files on this pool; it must outlive
352 // link() below. The classic linker ignores it.
353 DefaultThreadPool ThreadPool(hardware_concurrency(ThreadCount: Options.NumThreads));
354 DebugInfoLinker->setThreadPool(&ThreadPool);
355
356 if constexpr (std::is_same<Linker,
357 dwarf_linker::parallel::DWARFLinker>::value) {
358 DebugInfoLinker->setOutputDWARFHandler(
359 TargetTriple,
360 [&](std::shared_ptr<dwarf_linker::parallel::SectionDescriptorBase>
361 Section) {
362 Streamer->emitSectionContents(SecData: Section->getContents(),
363 SecKind: Section->getKind());
364 });
365 } else {
366 DebugInfoLinker->setOutputDWARFEmitter(Streamer.get());
367 }
368
369 DebugInfoLinker->setEstimatedObjfilesAmount(1);
370 DebugInfoLinker->setNumThreads(Options.NumThreads);
371 DebugInfoLinker->setNoODR(!Options.DoODRDeduplication);
372 DebugInfoLinker->setVerbosity(Options.Verbose);
373 DebugInfoLinker->setUpdateIndexTablesOnly(!Options.DoGarbageCollection);
374
375 std::vector<std::unique_ptr<DWARFFile>> ObjectsForLinking(1);
376
377 // Add object files to the DWARFLinker.
378 std::unique_ptr<DWARFContext> Context = DWARFContext::create(
379 File, DWARFContext::ProcessDebugRelocations::Process, nullptr, "",
380 [&](Error Err) {
381 handleAllErrors(std::move(Err), [&](ErrorInfoBase &Info) {
382 ReportErr(Info.message(), "", nullptr);
383 });
384 },
385 [&](Error Warning) {
386 handleAllErrors(std::move(Warning), [&](ErrorInfoBase &Info) {
387 ReportWarn(Info.message(), "", nullptr);
388 });
389 });
390 std::unique_ptr<ObjFileAddressMap> AddressesMap(
391 std::make_unique<ObjFileAddressMap>(args&: *Context, args: Options, args&: File));
392
393 ObjectsForLinking[0] = std::make_unique<DWARFFile>(
394 args: File.getFileName(), args: std::move(Context), args: std::move(AddressesMap));
395
396 uint16_t MaxDWARFVersion = 0;
397 std::function<void(const DWARFUnit &Unit)> OnCUDieLoaded =
398 [&MaxDWARFVersion](const DWARFUnit &Unit) {
399 MaxDWARFVersion = std::max(a: Unit.getVersion(), b: MaxDWARFVersion);
400 };
401
402 for (size_t I = 0; I < ObjectsForLinking.size(); I++)
403 DebugInfoLinker->addObjectFile(*ObjectsForLinking[I], nullptr,
404 OnCUDieLoaded);
405
406 // If we haven't seen any CUs, pick an arbitrary valid Dwarf version anyway.
407 if (MaxDWARFVersion == 0)
408 MaxDWARFVersion = 3;
409
410 if (Error Err = DebugInfoLinker->setTargetDWARFVersion(MaxDWARFVersion))
411 return Err;
412
413 SmallVector<typename Linker::AccelTableKind> AccelTables;
414
415 switch (Options.AccelTableKind) {
416 case DwarfUtilAccelKind::None:
417 // Nothing to do.
418 break;
419 case DwarfUtilAccelKind::DWARF:
420 // use .debug_names for all DWARF versions.
421 AccelTables.push_back(Linker::AccelTableKind::DebugNames);
422 break;
423 }
424
425 // Add accelerator tables to DWARFLinker.
426 for (typename Linker::AccelTableKind Table : AccelTables)
427 DebugInfoLinker->addAccelTableKind(Table);
428
429 for (std::unique_ptr<DWARFFile> &CurFile : ObjectsForLinking) {
430 SmallVector<StringRef> AccelTableNamesToReplace;
431 SmallVector<StringRef> AccelTableNamesToDelete;
432
433 // Unknown debug sections or non-requested accelerator sections would be
434 // removed. Display warning for such sections.
435 for (SectionName Sec : CurFile->Dwarf->getDWARFObj().getSectionNames()) {
436 if (isDebugSection(SecName: Sec.Name)) {
437 std::optional<typename Linker::AccelTableKind> SrcAccelTableKind =
438 getAcceleratorTableKind<typename Linker::AccelTableKind>(Sec.Name);
439
440 if (SrcAccelTableKind) {
441 assert(knownByDWARFUtil(Sec.Name));
442
443 if (Options.AccelTableKind == DwarfUtilAccelKind::None)
444 AccelTableNamesToDelete.push_back(Elt: Sec.Name);
445 else if (!llvm::is_contained(AccelTables, *SrcAccelTableKind))
446 AccelTableNamesToReplace.push_back(Elt: Sec.Name);
447 } else if (!knownByDWARFUtil(SecName: Sec.Name)) {
448 assert(!SrcAccelTableKind);
449 warning(
450 Message: formatv(
451 Fmt: "'{0}' is not currently supported: section will be skipped",
452 Vals&: Sec.Name),
453 Prefix: Options.InputFileName);
454 }
455 }
456 }
457
458 // Display message for the replaced accelerator tables.
459 if (!AccelTableNamesToReplace.empty())
460 warning(Message: getMessageForReplacedAcceleratorTables(AccelTableNamesToReplace,
461 TargetTable: Options.AccelTableKind),
462 Prefix: Options.InputFileName);
463
464 // Display message for the removed accelerator tables.
465 if (!AccelTableNamesToDelete.empty())
466 warning(Message: getMessageForDeletedAcceleratorTables(AccelTableNamesToReplace&: AccelTableNamesToDelete),
467 Prefix: Options.InputFileName);
468 }
469
470 // Link debug info.
471 if (Error Err = DebugInfoLinker->link())
472 return Err;
473
474 Streamer->finish();
475 return Error::success();
476}
477
478Error linkDebugInfo(object::ObjectFile &File, const Options &Options,
479 raw_pwrite_stream &OutStream) {
480 if (Options.UseDWARFLinkerParallel)
481 return linkDebugInfoImpl<parallel::DWARFLinker>(File, Options, OutStream);
482 else
483 return linkDebugInfoImpl<classic::DWARFLinker>(File, Options, OutStream);
484}
485
486} // end of namespace dwarfutil
487} // end of namespace llvm
488