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