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