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