1 //===------ utils/elf2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===// 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 "obj2yaml.h" 10 #include "llvm/ADT/DenseSet.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/Twine.h" 13 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 14 #include "llvm/Object/ELFObjectFile.h" 15 #include "llvm/ObjectYAML/DWARFYAML.h" 16 #include "llvm/ObjectYAML/ELFYAML.h" 17 #include "llvm/Support/DataExtractor.h" 18 #include "llvm/Support/ErrorHandling.h" 19 #include "llvm/Support/YAMLTraits.h" 20 21 using namespace llvm; 22 23 namespace { 24 25 template <class ELFT> 26 class ELFDumper { 27 typedef object::Elf_Sym_Impl<ELFT> Elf_Sym; 28 typedef typename ELFT::Dyn Elf_Dyn; 29 typedef typename ELFT::Shdr Elf_Shdr; 30 typedef typename ELFT::Word Elf_Word; 31 typedef typename ELFT::Rel Elf_Rel; 32 typedef typename ELFT::Rela Elf_Rela; 33 using Elf_Relr = typename ELFT::Relr; 34 using Elf_Nhdr = typename ELFT::Nhdr; 35 using Elf_Note = typename ELFT::Note; 36 37 ArrayRef<Elf_Shdr> Sections; 38 ArrayRef<Elf_Sym> SymTable; 39 40 DenseMap<StringRef, uint32_t> UsedSectionNames; 41 std::vector<std::string> SectionNames; 42 43 DenseMap<StringRef, uint32_t> UsedSymbolNames; 44 std::vector<std::string> SymbolNames; 45 46 BumpPtrAllocator StringAllocator; 47 48 Expected<StringRef> getUniquedSectionName(const Elf_Shdr *Sec); 49 Expected<StringRef> getUniquedSymbolName(const Elf_Sym *Sym, 50 StringRef StrTable, 51 const Elf_Shdr *SymTab); 52 Expected<StringRef> getSymbolName(uint32_t SymtabNdx, uint32_t SymbolNdx); 53 54 const object::ELFFile<ELFT> &Obj; 55 std::unique_ptr<DWARFContext> DWARFCtx; 56 ArrayRef<Elf_Word> ShndxTable; 57 58 Expected<std::vector<ELFYAML::ProgramHeader>> 59 dumpProgramHeaders(ArrayRef<std::unique_ptr<ELFYAML::Chunk>> Sections); 60 61 Optional<DWARFYAML::Data> 62 dumpDWARFSections(std::vector<std::unique_ptr<ELFYAML::Chunk>> &Sections); 63 64 Error dumpSymbols(const Elf_Shdr *Symtab, 65 std::vector<ELFYAML::Symbol> &Symbols); 66 Error dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, 67 StringRef StrTable, ELFYAML::Symbol &S); 68 Expected<std::vector<std::unique_ptr<ELFYAML::Chunk>>> dumpSections(); 69 Error dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S); 70 Error dumpCommonRelocationSection(const Elf_Shdr *Shdr, 71 ELFYAML::RelocationSection &S); 72 template <class RelT> 73 Error dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab, 74 ELFYAML::Relocation &R); 75 76 Expected<ELFYAML::AddrsigSection *> dumpAddrsigSection(const Elf_Shdr *Shdr); 77 Expected<ELFYAML::LinkerOptionsSection *> 78 dumpLinkerOptionsSection(const Elf_Shdr *Shdr); 79 Expected<ELFYAML::DependentLibrariesSection *> 80 dumpDependentLibrariesSection(const Elf_Shdr *Shdr); 81 Expected<ELFYAML::CallGraphProfileSection *> 82 dumpCallGraphProfileSection(const Elf_Shdr *Shdr); 83 Expected<ELFYAML::DynamicSection *> dumpDynamicSection(const Elf_Shdr *Shdr); 84 Expected<ELFYAML::RelocationSection *> dumpRelocSection(const Elf_Shdr *Shdr); 85 Expected<ELFYAML::RelrSection *> dumpRelrSection(const Elf_Shdr *Shdr); 86 Expected<ELFYAML::RawContentSection *> 87 dumpContentSection(const Elf_Shdr *Shdr); 88 Expected<ELFYAML::SymtabShndxSection *> 89 dumpSymtabShndxSection(const Elf_Shdr *Shdr); 90 Expected<ELFYAML::NoBitsSection *> dumpNoBitsSection(const Elf_Shdr *Shdr); 91 Expected<ELFYAML::HashSection *> dumpHashSection(const Elf_Shdr *Shdr); 92 Expected<ELFYAML::NoteSection *> dumpNoteSection(const Elf_Shdr *Shdr); 93 Expected<ELFYAML::GnuHashSection *> dumpGnuHashSection(const Elf_Shdr *Shdr); 94 Expected<ELFYAML::VerdefSection *> dumpVerdefSection(const Elf_Shdr *Shdr); 95 Expected<ELFYAML::SymverSection *> dumpSymverSection(const Elf_Shdr *Shdr); 96 Expected<ELFYAML::VerneedSection *> dumpVerneedSection(const Elf_Shdr *Shdr); 97 Expected<ELFYAML::GroupSection *> dumpGroupSection(const Elf_Shdr *Shdr); 98 Expected<ELFYAML::ARMIndexTableSection *> 99 dumpARMIndexTableSection(const Elf_Shdr *Shdr); 100 Expected<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr); 101 Expected<ELFYAML::StackSizesSection *> 102 dumpStackSizesSection(const Elf_Shdr *Shdr); 103 Expected<ELFYAML::RawContentSection *> 104 dumpPlaceholderSection(const Elf_Shdr *Shdr); 105 106 bool shouldPrintSection(const ELFYAML::Section &S, const Elf_Shdr &SHdr, 107 Optional<DWARFYAML::Data> DWARF); 108 109 public: 110 ELFDumper(const object::ELFFile<ELFT> &O, std::unique_ptr<DWARFContext> DCtx); 111 Expected<ELFYAML::Object *> dump(); 112 }; 113 114 } 115 116 template <class ELFT> 117 ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O, 118 std::unique_ptr<DWARFContext> DCtx) 119 : Obj(O), DWARFCtx(std::move(DCtx)) {} 120 121 template <class ELFT> 122 Expected<StringRef> 123 ELFDumper<ELFT>::getUniquedSectionName(const Elf_Shdr *Sec) { 124 unsigned SecIndex = Sec - &Sections[0]; 125 assert(&Sections[SecIndex] == Sec); 126 if (!SectionNames[SecIndex].empty()) 127 return SectionNames[SecIndex]; 128 129 auto NameOrErr = Obj.getSectionName(*Sec); 130 if (!NameOrErr) 131 return NameOrErr; 132 StringRef Name = *NameOrErr; 133 // In some specific cases we might have more than one section without a 134 // name (sh_name == 0). It normally doesn't happen, but when we have this case 135 // it doesn't make sense to uniquify their names and add noise to the output. 136 if (Name.empty()) 137 return ""; 138 139 std::string &Ret = SectionNames[SecIndex]; 140 141 auto It = UsedSectionNames.insert({Name, 0}); 142 if (!It.second) 143 Ret = ELFYAML::appendUniqueSuffix(Name, Twine(++It.first->second)); 144 else 145 Ret = std::string(Name); 146 return Ret; 147 } 148 149 template <class ELFT> 150 Expected<StringRef> 151 ELFDumper<ELFT>::getUniquedSymbolName(const Elf_Sym *Sym, StringRef StrTable, 152 const Elf_Shdr *SymTab) { 153 Expected<StringRef> SymbolNameOrErr = Sym->getName(StrTable); 154 if (!SymbolNameOrErr) 155 return SymbolNameOrErr; 156 StringRef Name = *SymbolNameOrErr; 157 if (Name.empty() && Sym->getType() == ELF::STT_SECTION) { 158 auto ShdrOrErr = Obj.getSection(*Sym, SymTab, ShndxTable); 159 if (!ShdrOrErr) 160 return ShdrOrErr.takeError(); 161 return getUniquedSectionName(*ShdrOrErr); 162 } 163 164 // Symbols in .symtab can have duplicate names. For example, it is a common 165 // situation for local symbols in a relocatable object. Here we assign unique 166 // suffixes for such symbols so that we can differentiate them. 167 if (SymTab->sh_type == ELF::SHT_SYMTAB) { 168 unsigned Index = Sym - SymTable.data(); 169 if (!SymbolNames[Index].empty()) 170 return SymbolNames[Index]; 171 172 auto It = UsedSymbolNames.insert({Name, 0}); 173 if (!It.second) 174 SymbolNames[Index] = 175 ELFYAML::appendUniqueSuffix(Name, Twine(++It.first->second)); 176 else 177 SymbolNames[Index] = std::string(Name); 178 return SymbolNames[Index]; 179 } 180 181 return Name; 182 } 183 184 template <class ELFT> 185 bool ELFDumper<ELFT>::shouldPrintSection(const ELFYAML::Section &S, 186 const Elf_Shdr &SHdr, 187 Optional<DWARFYAML::Data> DWARF) { 188 // We only print the SHT_NULL section at index 0 when it 189 // has at least one non-null field, because yaml2obj 190 // normally creates the zero section at index 0 implicitly. 191 if (S.Type == ELF::SHT_NULL && (&SHdr == &Sections[0])) { 192 const uint8_t *Begin = reinterpret_cast<const uint8_t *>(&SHdr); 193 const uint8_t *End = Begin + sizeof(Elf_Shdr); 194 return std::find_if(Begin, End, [](uint8_t V) { return V != 0; }) != End; 195 } 196 197 // Normally we use "DWARF:" to describe contents of DWARF sections. Sometimes 198 // the content of DWARF sections can be successfully parsed into the "DWARF:" 199 // entry but their section headers may have special flags, entry size, address 200 // alignment, etc. We will preserve the header for them under such 201 // circumstances. 202 StringRef SecName = S.Name.substr(1); 203 if (DWARF && DWARF->getNonEmptySectionNames().count(SecName)) { 204 if (const ELFYAML::RawContentSection *RawSec = 205 dyn_cast<const ELFYAML::RawContentSection>(&S)) { 206 if (RawSec->Type != ELF::SHT_PROGBITS || !RawSec->Link.empty() || 207 RawSec->Info || RawSec->AddressAlign != 1 || RawSec->Address || 208 RawSec->EntSize) 209 return true; 210 211 ELFYAML::ELF_SHF ShFlags = RawSec->Flags.getValueOr(ELFYAML::ELF_SHF(0)); 212 213 if (SecName == "debug_str") 214 return ShFlags != ELFYAML::ELF_SHF(ELF::SHF_MERGE | ELF::SHF_STRINGS); 215 216 return ShFlags != 0; 217 } 218 } 219 220 // Normally we use "Symbols:" and "DynamicSymbols:" to describe contents of 221 // symbol tables. We also build and emit corresponding string tables 222 // implicitly. But sometimes it is important to preserve positions and virtual 223 // addresses of allocatable sections, e.g. for creating program headers. 224 // Generally we are trying to reduce noise in the YAML output. Because 225 // of that we do not print non-allocatable versions of such sections and 226 // assume they are placed at the end. 227 if (S.Type == ELF::SHT_STRTAB || S.Type == ELF::SHT_SYMTAB || 228 S.Type == ELF::SHT_DYNSYM) 229 return S.Flags.getValueOr(ELFYAML::ELF_SHF(0)) & ELF::SHF_ALLOC; 230 231 return true; 232 } 233 234 template <class ELFT> Expected<ELFYAML::Object *> ELFDumper<ELFT>::dump() { 235 auto Y = std::make_unique<ELFYAML::Object>(); 236 237 // Dump header. We do not dump EPh* and ESh* fields. When not explicitly set, 238 // the values are set by yaml2obj automatically and there is no need to dump 239 // them here. 240 Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader().getFileClass()); 241 Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader().getDataEncoding()); 242 Y->Header.OSABI = Obj.getHeader().e_ident[ELF::EI_OSABI]; 243 Y->Header.ABIVersion = Obj.getHeader().e_ident[ELF::EI_ABIVERSION]; 244 Y->Header.Type = Obj.getHeader().e_type; 245 if (Obj.getHeader().e_machine != 0) 246 Y->Header.Machine = ELFYAML::ELF_EM(Obj.getHeader().e_machine); 247 Y->Header.Flags = Obj.getHeader().e_flags; 248 Y->Header.Entry = Obj.getHeader().e_entry; 249 250 // Dump sections 251 auto SectionsOrErr = Obj.sections(); 252 if (!SectionsOrErr) 253 return SectionsOrErr.takeError(); 254 Sections = *SectionsOrErr; 255 SectionNames.resize(Sections.size()); 256 257 // Dump symbols. We need to do this early because other sections might want 258 // to access the deduplicated symbol names that we also create here. 259 const Elf_Shdr *SymTab = nullptr; 260 const Elf_Shdr *SymTabShndx = nullptr; 261 const Elf_Shdr *DynSymTab = nullptr; 262 263 for (const Elf_Shdr &Sec : Sections) { 264 if (Sec.sh_type == ELF::SHT_SYMTAB) { 265 SymTab = &Sec; 266 } else if (Sec.sh_type == ELF::SHT_DYNSYM) { 267 DynSymTab = &Sec; 268 } else if (Sec.sh_type == ELF::SHT_SYMTAB_SHNDX) { 269 // ABI allows us to have one SHT_SYMTAB_SHNDX for each symbol table. 270 // We only support having the SHT_SYMTAB_SHNDX for SHT_SYMTAB now. 271 if (SymTabShndx) 272 return createStringError( 273 errc::not_supported, 274 "multiple SHT_SYMTAB_SHNDX sections are not supported"); 275 SymTabShndx = &Sec; 276 } 277 } 278 279 // We need to locate the SHT_SYMTAB_SHNDX section early, because it might be 280 // needed for dumping symbols. 281 if (SymTabShndx) { 282 if (!SymTab || 283 SymTabShndx->sh_link != (unsigned)(SymTab - Sections.begin())) 284 return createStringError( 285 errc::not_supported, 286 "only SHT_SYMTAB_SHNDX associated with SHT_SYMTAB are supported"); 287 288 auto TableOrErr = Obj.getSHNDXTable(*SymTabShndx); 289 if (!TableOrErr) 290 return TableOrErr.takeError(); 291 ShndxTable = *TableOrErr; 292 } 293 294 if (SymTab) { 295 Y->Symbols.emplace(); 296 if (Error E = dumpSymbols(SymTab, *Y->Symbols)) 297 return std::move(E); 298 } 299 300 if (DynSymTab) { 301 Y->DynamicSymbols.emplace(); 302 if (Error E = dumpSymbols(DynSymTab, *Y->DynamicSymbols)) 303 return std::move(E); 304 } 305 306 // We dump all sections first. It is simple and allows us to verify that all 307 // sections are valid and also to generalize the code. But we are not going to 308 // keep all of them in the final output (see comments for 309 // 'shouldPrintSection()'). Undesired chunks will be removed later. 310 Expected<std::vector<std::unique_ptr<ELFYAML::Chunk>>> ChunksOrErr = 311 dumpSections(); 312 if (!ChunksOrErr) 313 return ChunksOrErr.takeError(); 314 std::vector<std::unique_ptr<ELFYAML::Chunk>> Chunks = std::move(*ChunksOrErr); 315 316 // Dump program headers. 317 Expected<std::vector<ELFYAML::ProgramHeader>> PhdrsOrErr = 318 dumpProgramHeaders(Chunks); 319 if (!PhdrsOrErr) 320 return PhdrsOrErr.takeError(); 321 Y->ProgramHeaders = std::move(*PhdrsOrErr); 322 323 // Dump DWARF sections. 324 Y->DWARF = dumpDWARFSections(Chunks); 325 326 llvm::erase_if(Chunks, [this, &Y](const std::unique_ptr<ELFYAML::Chunk> &C) { 327 const ELFYAML::Section &S = cast<ELFYAML::Section>(*C.get()); 328 return !shouldPrintSection(S, Sections[S.OriginalSecNdx], Y->DWARF); 329 }); 330 331 Y->Chunks = std::move(Chunks); 332 return Y.release(); 333 } 334 335 template <class ELFT> 336 static bool isInSegment(const ELFYAML::Section &Sec, 337 const typename ELFT::Shdr &SHdr, 338 const typename ELFT::Phdr &Phdr) { 339 if (Sec.Type == ELF::SHT_NULL) 340 return false; 341 342 // A section is within a segment when its location in a file is within the 343 // [p_offset, p_offset + p_filesz] region. 344 bool FileOffsetsMatch = 345 SHdr.sh_offset >= Phdr.p_offset && 346 (SHdr.sh_offset + SHdr.sh_size <= Phdr.p_offset + Phdr.p_filesz); 347 348 bool VirtualAddressesMatch = SHdr.sh_addr >= Phdr.p_vaddr && 349 SHdr.sh_addr <= Phdr.p_vaddr + Phdr.p_memsz; 350 351 if (FileOffsetsMatch) { 352 // An empty section on the edges of a program header can be outside of the 353 // virtual address space of the segment. This means it is not included in 354 // the segment and we should ignore it. 355 if (SHdr.sh_size == 0 && (SHdr.sh_offset == Phdr.p_offset || 356 SHdr.sh_offset == Phdr.p_offset + Phdr.p_filesz)) 357 return VirtualAddressesMatch; 358 return true; 359 } 360 361 // SHT_NOBITS sections usually occupy no physical space in a file. Such 362 // sections belong to a segment when they reside in the segment's virtual 363 // address space. 364 if (Sec.Type != ELF::SHT_NOBITS) 365 return false; 366 return VirtualAddressesMatch; 367 } 368 369 template <class ELFT> 370 Expected<std::vector<ELFYAML::ProgramHeader>> 371 ELFDumper<ELFT>::dumpProgramHeaders( 372 ArrayRef<std::unique_ptr<ELFYAML::Chunk>> Chunks) { 373 std::vector<ELFYAML::ProgramHeader> Ret; 374 Expected<typename ELFT::PhdrRange> PhdrsOrErr = Obj.program_headers(); 375 if (!PhdrsOrErr) 376 return PhdrsOrErr.takeError(); 377 378 for (const typename ELFT::Phdr &Phdr : *PhdrsOrErr) { 379 ELFYAML::ProgramHeader PH; 380 PH.Type = Phdr.p_type; 381 PH.Flags = Phdr.p_flags; 382 PH.VAddr = Phdr.p_vaddr; 383 PH.PAddr = Phdr.p_paddr; 384 385 // yaml2obj sets the alignment of a segment to 1 by default. 386 // We do not print the default alignment to reduce noise in the output. 387 if (Phdr.p_align != 1) 388 PH.Align = static_cast<llvm::yaml::Hex64>(Phdr.p_align); 389 390 // Here we match sections with segments. 391 // It is not possible to have a non-Section chunk, because 392 // obj2yaml does not create Fill chunks. 393 for (const std::unique_ptr<ELFYAML::Chunk> &C : Chunks) { 394 ELFYAML::Section &S = cast<ELFYAML::Section>(*C.get()); 395 if (isInSegment<ELFT>(S, Sections[S.OriginalSecNdx], Phdr)) 396 PH.Sections.push_back({S.Name}); 397 } 398 399 Ret.push_back(PH); 400 } 401 402 return Ret; 403 } 404 405 template <class ELFT> 406 Optional<DWARFYAML::Data> ELFDumper<ELFT>::dumpDWARFSections( 407 std::vector<std::unique_ptr<ELFYAML::Chunk>> &Sections) { 408 DWARFYAML::Data DWARF; 409 for (std::unique_ptr<ELFYAML::Chunk> &C : Sections) { 410 if (!C->Name.startswith(".debug_")) 411 continue; 412 413 if (ELFYAML::RawContentSection *RawSec = 414 dyn_cast<ELFYAML::RawContentSection>(C.get())) { 415 Error Err = Error::success(); 416 cantFail(std::move(Err)); 417 418 if (RawSec->Name == ".debug_aranges") 419 Err = dumpDebugARanges(*DWARFCtx.get(), DWARF); 420 else if (RawSec->Name == ".debug_str") 421 Err = dumpDebugStrings(*DWARFCtx.get(), DWARF); 422 else if (RawSec->Name == ".debug_ranges") 423 Err = dumpDebugRanges(*DWARFCtx.get(), DWARF); 424 else if (RawSec->Name == ".debug_addr") 425 Err = dumpDebugAddr(*DWARFCtx.get(), DWARF); 426 else 427 continue; 428 429 // If the DWARF section cannot be successfully parsed, emit raw content 430 // instead of an entry in the DWARF section of the YAML. 431 if (Err) 432 consumeError(std::move(Err)); 433 else 434 RawSec->Content.reset(); 435 } 436 } 437 438 if (DWARF.getNonEmptySectionNames().empty()) 439 return None; 440 return DWARF; 441 } 442 443 template <class ELFT> 444 Expected<ELFYAML::RawContentSection *> 445 ELFDumper<ELFT>::dumpPlaceholderSection(const Elf_Shdr *Shdr) { 446 auto S = std::make_unique<ELFYAML::RawContentSection>(); 447 if (Error E = dumpCommonSection(Shdr, *S.get())) 448 return std::move(E); 449 return S.release(); 450 } 451 452 template <class ELFT> 453 Expected<std::vector<std::unique_ptr<ELFYAML::Chunk>>> 454 ELFDumper<ELFT>::dumpSections() { 455 std::vector<std::unique_ptr<ELFYAML::Chunk>> Ret; 456 auto Add = [&](Expected<ELFYAML::Chunk *> SecOrErr) -> Error { 457 if (!SecOrErr) 458 return SecOrErr.takeError(); 459 Ret.emplace_back(*SecOrErr); 460 return Error::success(); 461 }; 462 463 auto GetDumper = [this](unsigned Type) 464 -> std::function<Expected<ELFYAML::Chunk *>(const Elf_Shdr *)> { 465 if (Obj.getHeader().e_machine == ELF::EM_ARM && Type == ELF::SHT_ARM_EXIDX) 466 return [this](const Elf_Shdr *S) { return dumpARMIndexTableSection(S); }; 467 468 if (Obj.getHeader().e_machine == ELF::EM_MIPS && 469 Type == ELF::SHT_MIPS_ABIFLAGS) 470 return [this](const Elf_Shdr *S) { return dumpMipsABIFlags(S); }; 471 472 switch (Type) { 473 case ELF::SHT_DYNAMIC: 474 return [this](const Elf_Shdr *S) { return dumpDynamicSection(S); }; 475 case ELF::SHT_SYMTAB_SHNDX: 476 return [this](const Elf_Shdr *S) { return dumpSymtabShndxSection(S); }; 477 case ELF::SHT_REL: 478 case ELF::SHT_RELA: 479 return [this](const Elf_Shdr *S) { return dumpRelocSection(S); }; 480 case ELF::SHT_RELR: 481 return [this](const Elf_Shdr *S) { return dumpRelrSection(S); }; 482 case ELF::SHT_GROUP: 483 return [this](const Elf_Shdr *S) { return dumpGroupSection(S); }; 484 case ELF::SHT_NOBITS: 485 return [this](const Elf_Shdr *S) { return dumpNoBitsSection(S); }; 486 case ELF::SHT_NOTE: 487 return [this](const Elf_Shdr *S) { return dumpNoteSection(S); }; 488 case ELF::SHT_HASH: 489 return [this](const Elf_Shdr *S) { return dumpHashSection(S); }; 490 case ELF::SHT_GNU_HASH: 491 return [this](const Elf_Shdr *S) { return dumpGnuHashSection(S); }; 492 case ELF::SHT_GNU_verdef: 493 return [this](const Elf_Shdr *S) { return dumpVerdefSection(S); }; 494 case ELF::SHT_GNU_versym: 495 return [this](const Elf_Shdr *S) { return dumpSymverSection(S); }; 496 case ELF::SHT_GNU_verneed: 497 return [this](const Elf_Shdr *S) { return dumpVerneedSection(S); }; 498 case ELF::SHT_LLVM_ADDRSIG: 499 return [this](const Elf_Shdr *S) { return dumpAddrsigSection(S); }; 500 case ELF::SHT_LLVM_LINKER_OPTIONS: 501 return [this](const Elf_Shdr *S) { return dumpLinkerOptionsSection(S); }; 502 case ELF::SHT_LLVM_DEPENDENT_LIBRARIES: 503 return [this](const Elf_Shdr *S) { 504 return dumpDependentLibrariesSection(S); 505 }; 506 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE: 507 return 508 [this](const Elf_Shdr *S) { return dumpCallGraphProfileSection(S); }; 509 case ELF::SHT_STRTAB: 510 case ELF::SHT_SYMTAB: 511 case ELF::SHT_DYNSYM: 512 // The contents of these sections are described by other parts of the YAML 513 // file. But we still want to dump them, because their properties can be 514 // important. See comments for 'shouldPrintSection()' for more details. 515 return [this](const Elf_Shdr *S) { return dumpPlaceholderSection(S); }; 516 default: 517 return nullptr; 518 } 519 }; 520 521 for (const Elf_Shdr &Sec : Sections) { 522 // We have dedicated dumping functions for most of the section types. 523 // Try to use one of them first. 524 if (std::function<Expected<ELFYAML::Chunk *>(const Elf_Shdr *)> DumpFn = 525 GetDumper(Sec.sh_type)) { 526 if (Error E = Add(DumpFn(&Sec))) 527 return std::move(E); 528 continue; 529 } 530 531 // Recognize some special SHT_PROGBITS sections by name. 532 if (Sec.sh_type == ELF::SHT_PROGBITS) { 533 auto NameOrErr = Obj.getSectionName(Sec); 534 if (!NameOrErr) 535 return NameOrErr.takeError(); 536 537 if (ELFYAML::StackSizesSection::nameMatches(*NameOrErr)) { 538 if (Error E = Add(dumpStackSizesSection(&Sec))) 539 return std::move(E); 540 continue; 541 } 542 } 543 544 if (Error E = Add(dumpContentSection(&Sec))) 545 return std::move(E); 546 } 547 548 return std::move(Ret); 549 } 550 551 template <class ELFT> 552 Error ELFDumper<ELFT>::dumpSymbols(const Elf_Shdr *Symtab, 553 std::vector<ELFYAML::Symbol> &Symbols) { 554 if (!Symtab) 555 return Error::success(); 556 557 auto StrTableOrErr = Obj.getStringTableForSymtab(*Symtab); 558 if (!StrTableOrErr) 559 return StrTableOrErr.takeError(); 560 StringRef StrTable = *StrTableOrErr; 561 562 auto SymtabOrErr = Obj.symbols(Symtab); 563 if (!SymtabOrErr) 564 return SymtabOrErr.takeError(); 565 566 if (Symtab->sh_type == ELF::SHT_SYMTAB) { 567 SymTable = *SymtabOrErr; 568 SymbolNames.resize(SymTable.size()); 569 } 570 571 for (const auto &Sym : (*SymtabOrErr).drop_front()) { 572 ELFYAML::Symbol S; 573 if (auto EC = dumpSymbol(&Sym, Symtab, StrTable, S)) 574 return EC; 575 Symbols.push_back(S); 576 } 577 578 return Error::success(); 579 } 580 581 template <class ELFT> 582 Error ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, 583 StringRef StrTable, ELFYAML::Symbol &S) { 584 S.Type = Sym->getType(); 585 S.Value = Sym->st_value; 586 S.Size = Sym->st_size; 587 S.Other = Sym->st_other; 588 S.Binding = Sym->getBinding(); 589 590 Expected<StringRef> SymbolNameOrErr = 591 getUniquedSymbolName(Sym, StrTable, SymTab); 592 if (!SymbolNameOrErr) 593 return SymbolNameOrErr.takeError(); 594 S.Name = SymbolNameOrErr.get(); 595 596 if (Sym->st_shndx >= ELF::SHN_LORESERVE) { 597 S.Index = (ELFYAML::ELF_SHN)Sym->st_shndx; 598 return Error::success(); 599 } 600 601 auto ShdrOrErr = Obj.getSection(*Sym, SymTab, ShndxTable); 602 if (!ShdrOrErr) 603 return ShdrOrErr.takeError(); 604 const Elf_Shdr *Shdr = *ShdrOrErr; 605 if (!Shdr) 606 return Error::success(); 607 608 auto NameOrErr = getUniquedSectionName(Shdr); 609 if (!NameOrErr) 610 return NameOrErr.takeError(); 611 S.Section = NameOrErr.get(); 612 613 return Error::success(); 614 } 615 616 template <class ELFT> 617 template <class RelT> 618 Error ELFDumper<ELFT>::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab, 619 ELFYAML::Relocation &R) { 620 R.Type = Rel->getType(Obj.isMips64EL()); 621 R.Offset = Rel->r_offset; 622 R.Addend = 0; 623 624 auto SymOrErr = Obj.getRelocationSymbol(*Rel, SymTab); 625 if (!SymOrErr) 626 return SymOrErr.takeError(); 627 628 // We have might have a relocation with symbol index 0, 629 // e.g. R_X86_64_NONE or R_X86_64_GOTPC32. 630 const Elf_Sym *Sym = *SymOrErr; 631 if (!Sym) 632 return Error::success(); 633 634 auto StrTabSec = Obj.getSection(SymTab->sh_link); 635 if (!StrTabSec) 636 return StrTabSec.takeError(); 637 auto StrTabOrErr = Obj.getStringTable(**StrTabSec); 638 if (!StrTabOrErr) 639 return StrTabOrErr.takeError(); 640 641 Expected<StringRef> NameOrErr = 642 getUniquedSymbolName(Sym, *StrTabOrErr, SymTab); 643 if (!NameOrErr) 644 return NameOrErr.takeError(); 645 R.Symbol = NameOrErr.get(); 646 647 return Error::success(); 648 } 649 650 template <class ELFT> 651 static unsigned getDefaultShEntSize(ELFYAML::ELF_SHT SecType, 652 StringRef SecName) { 653 switch (SecType) { 654 case ELF::SHT_REL: 655 return sizeof(typename ELFT::Rel); 656 case ELF::SHT_RELA: 657 return sizeof(typename ELFT::Rela); 658 case ELF::SHT_RELR: 659 return sizeof(typename ELFT::Relr); 660 case ELF::SHT_DYNAMIC: 661 return sizeof(typename ELFT::Dyn); 662 case ELF::SHT_HASH: 663 return sizeof(typename ELFT::Word); 664 default: 665 if (SecName == ".debug_str") 666 return 1; 667 return 0; 668 } 669 } 670 671 template <class ELFT> 672 Error ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr, 673 ELFYAML::Section &S) { 674 // Dump fields. We do not dump the ShOffset field. When not explicitly 675 // set, the value is set by yaml2obj automatically. 676 S.Type = Shdr->sh_type; 677 if (Shdr->sh_flags) 678 S.Flags = static_cast<ELFYAML::ELF_SHF>(Shdr->sh_flags); 679 if (Shdr->sh_addr) 680 S.Address = static_cast<uint64_t>(Shdr->sh_addr); 681 S.AddressAlign = Shdr->sh_addralign; 682 683 S.OriginalSecNdx = Shdr - &Sections[0]; 684 685 auto NameOrErr = getUniquedSectionName(Shdr); 686 if (!NameOrErr) 687 return NameOrErr.takeError(); 688 S.Name = NameOrErr.get(); 689 690 if (Shdr->sh_entsize != getDefaultShEntSize<ELFT>(S.Type, S.Name)) 691 S.EntSize = static_cast<llvm::yaml::Hex64>(Shdr->sh_entsize); 692 693 if (Shdr->sh_link != ELF::SHN_UNDEF) { 694 auto LinkSection = Obj.getSection(Shdr->sh_link); 695 if (!LinkSection) 696 return make_error<StringError>( 697 "unable to resolve sh_link reference in section '" + S.Name + 698 "': " + toString(LinkSection.takeError()), 699 inconvertibleErrorCode()); 700 701 NameOrErr = getUniquedSectionName(*LinkSection); 702 if (!NameOrErr) 703 return NameOrErr.takeError(); 704 S.Link = NameOrErr.get(); 705 } 706 707 return Error::success(); 708 } 709 710 template <class ELFT> 711 Error ELFDumper<ELFT>::dumpCommonRelocationSection( 712 const Elf_Shdr *Shdr, ELFYAML::RelocationSection &S) { 713 if (Error E = dumpCommonSection(Shdr, S)) 714 return E; 715 716 // Having a zero sh_info field is normal: .rela.dyn is a dynamic 717 // relocation section that normally has no value in this field. 718 if (!Shdr->sh_info) 719 return Error::success(); 720 721 auto InfoSection = Obj.getSection(Shdr->sh_info); 722 if (!InfoSection) 723 return InfoSection.takeError(); 724 725 auto NameOrErr = getUniquedSectionName(*InfoSection); 726 if (!NameOrErr) 727 return NameOrErr.takeError(); 728 S.RelocatableSec = NameOrErr.get(); 729 730 return Error::success(); 731 } 732 733 template <class ELFT> 734 Expected<ELFYAML::StackSizesSection *> 735 ELFDumper<ELFT>::dumpStackSizesSection(const Elf_Shdr *Shdr) { 736 auto S = std::make_unique<ELFYAML::StackSizesSection>(); 737 if (Error E = dumpCommonSection(Shdr, *S)) 738 return std::move(E); 739 740 auto ContentOrErr = Obj.getSectionContents(*Shdr); 741 if (!ContentOrErr) 742 return ContentOrErr.takeError(); 743 744 ArrayRef<uint8_t> Content = *ContentOrErr; 745 DataExtractor Data(Content, Obj.isLE(), ELFT::Is64Bits ? 8 : 4); 746 747 std::vector<ELFYAML::StackSizeEntry> Entries; 748 DataExtractor::Cursor Cur(0); 749 while (Cur && Cur.tell() < Content.size()) { 750 uint64_t Address = Data.getAddress(Cur); 751 uint64_t Size = Data.getULEB128(Cur); 752 Entries.push_back({Address, Size}); 753 } 754 755 if (Content.empty() || !Cur) { 756 // If .stack_sizes cannot be decoded, we dump it as an array of bytes. 757 consumeError(Cur.takeError()); 758 S->Content = yaml::BinaryRef(Content); 759 } else { 760 S->Entries = std::move(Entries); 761 } 762 763 return S.release(); 764 } 765 766 template <class ELFT> 767 Expected<ELFYAML::AddrsigSection *> 768 ELFDumper<ELFT>::dumpAddrsigSection(const Elf_Shdr *Shdr) { 769 auto S = std::make_unique<ELFYAML::AddrsigSection>(); 770 if (Error E = dumpCommonSection(Shdr, *S)) 771 return std::move(E); 772 773 auto ContentOrErr = Obj.getSectionContents(*Shdr); 774 if (!ContentOrErr) 775 return ContentOrErr.takeError(); 776 777 ArrayRef<uint8_t> Content = *ContentOrErr; 778 DataExtractor::Cursor Cur(0); 779 DataExtractor Data(Content, Obj.isLE(), /*AddressSize=*/0); 780 std::vector<ELFYAML::YAMLFlowString> Symbols; 781 while (Cur && Cur.tell() < Content.size()) { 782 uint64_t SymNdx = Data.getULEB128(Cur); 783 if (!Cur) 784 break; 785 786 Expected<StringRef> SymbolName = getSymbolName(Shdr->sh_link, SymNdx); 787 if (!SymbolName || SymbolName->empty()) { 788 consumeError(SymbolName.takeError()); 789 Symbols.emplace_back( 790 StringRef(std::to_string(SymNdx)).copy(StringAllocator)); 791 continue; 792 } 793 794 Symbols.emplace_back(*SymbolName); 795 } 796 797 if (Cur) { 798 S->Symbols = std::move(Symbols); 799 return S.release(); 800 } 801 802 consumeError(Cur.takeError()); 803 S->Content = yaml::BinaryRef(Content); 804 return S.release(); 805 } 806 807 template <class ELFT> 808 Expected<ELFYAML::LinkerOptionsSection *> 809 ELFDumper<ELFT>::dumpLinkerOptionsSection(const Elf_Shdr *Shdr) { 810 auto S = std::make_unique<ELFYAML::LinkerOptionsSection>(); 811 if (Error E = dumpCommonSection(Shdr, *S)) 812 return std::move(E); 813 814 auto ContentOrErr = Obj.getSectionContents(*Shdr); 815 if (!ContentOrErr) 816 return ContentOrErr.takeError(); 817 818 ArrayRef<uint8_t> Content = *ContentOrErr; 819 if (Content.empty() || Content.back() != 0) { 820 S->Content = Content; 821 return S.release(); 822 } 823 824 SmallVector<StringRef, 16> Strings; 825 toStringRef(Content.drop_back()).split(Strings, '\0'); 826 if (Strings.size() % 2 != 0) { 827 S->Content = Content; 828 return S.release(); 829 } 830 831 S->Options.emplace(); 832 for (size_t I = 0, E = Strings.size(); I != E; I += 2) 833 S->Options->push_back({Strings[I], Strings[I + 1]}); 834 835 return S.release(); 836 } 837 838 template <class ELFT> 839 Expected<ELFYAML::DependentLibrariesSection *> 840 ELFDumper<ELFT>::dumpDependentLibrariesSection(const Elf_Shdr *Shdr) { 841 auto DL = std::make_unique<ELFYAML::DependentLibrariesSection>(); 842 if (Error E = dumpCommonSection(Shdr, *DL)) 843 return std::move(E); 844 845 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr); 846 if (!ContentOrErr) 847 return ContentOrErr.takeError(); 848 849 ArrayRef<uint8_t> Content = *ContentOrErr; 850 if (!Content.empty() && Content.back() != 0) { 851 DL->Content = Content; 852 return DL.release(); 853 } 854 855 DL->Libs.emplace(); 856 for (const uint8_t *I = Content.begin(), *E = Content.end(); I < E;) { 857 StringRef Lib((const char *)I); 858 DL->Libs->emplace_back(Lib); 859 I += Lib.size() + 1; 860 } 861 862 return DL.release(); 863 } 864 865 template <class ELFT> 866 Expected<ELFYAML::CallGraphProfileSection *> 867 ELFDumper<ELFT>::dumpCallGraphProfileSection(const Elf_Shdr *Shdr) { 868 auto S = std::make_unique<ELFYAML::CallGraphProfileSection>(); 869 if (Error E = dumpCommonSection(Shdr, *S)) 870 return std::move(E); 871 872 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr); 873 if (!ContentOrErr) 874 return ContentOrErr.takeError(); 875 ArrayRef<uint8_t> Content = *ContentOrErr; 876 877 // Dump the section by using the Content key when it is truncated. 878 // There is no need to create either "Content" or "Entries" fields when the 879 // section is empty. 880 if (Content.empty() || Content.size() % 16 != 0) { 881 if (!Content.empty()) 882 S->Content = yaml::BinaryRef(Content); 883 return S.release(); 884 } 885 886 std::vector<ELFYAML::CallGraphEntry> Entries(Content.size() / 16); 887 DataExtractor Data(Content, Obj.isLE(), /*AddressSize=*/0); 888 DataExtractor::Cursor Cur(0); 889 auto ReadEntry = [&](ELFYAML::CallGraphEntry &E) { 890 uint32_t FromSymIndex = Data.getU32(Cur); 891 uint32_t ToSymIndex = Data.getU32(Cur); 892 E.Weight = Data.getU64(Cur); 893 if (!Cur) { 894 consumeError(Cur.takeError()); 895 return false; 896 } 897 898 Expected<StringRef> From = getSymbolName(Shdr->sh_link, FromSymIndex); 899 Expected<StringRef> To = getSymbolName(Shdr->sh_link, ToSymIndex); 900 if (From && To) { 901 E.From = *From; 902 E.To = *To; 903 return true; 904 } 905 consumeError(From.takeError()); 906 consumeError(To.takeError()); 907 return false; 908 }; 909 910 for (ELFYAML::CallGraphEntry &E : Entries) { 911 if (ReadEntry(E)) 912 continue; 913 S->Content = yaml::BinaryRef(Content); 914 return S.release(); 915 } 916 917 S->Entries = std::move(Entries); 918 return S.release(); 919 } 920 921 template <class ELFT> 922 Expected<ELFYAML::DynamicSection *> 923 ELFDumper<ELFT>::dumpDynamicSection(const Elf_Shdr *Shdr) { 924 auto S = std::make_unique<ELFYAML::DynamicSection>(); 925 if (Error E = dumpCommonSection(Shdr, *S)) 926 return std::move(E); 927 928 auto DynTagsOrErr = Obj.template getSectionContentsAsArray<Elf_Dyn>(*Shdr); 929 if (!DynTagsOrErr) 930 return DynTagsOrErr.takeError(); 931 932 S->Entries.emplace(); 933 for (const Elf_Dyn &Dyn : *DynTagsOrErr) 934 S->Entries->push_back({(ELFYAML::ELF_DYNTAG)Dyn.getTag(), Dyn.getVal()}); 935 936 return S.release(); 937 } 938 939 template <class ELFT> 940 Expected<ELFYAML::RelocationSection *> 941 ELFDumper<ELFT>::dumpRelocSection(const Elf_Shdr *Shdr) { 942 auto S = std::make_unique<ELFYAML::RelocationSection>(); 943 if (auto E = dumpCommonRelocationSection(Shdr, *S)) 944 return std::move(E); 945 946 auto SymTabOrErr = Obj.getSection(Shdr->sh_link); 947 if (!SymTabOrErr) 948 return SymTabOrErr.takeError(); 949 950 if (Shdr->sh_size != 0) 951 S->Relocations.emplace(); 952 953 if (Shdr->sh_type == ELF::SHT_REL) { 954 auto Rels = Obj.rels(*Shdr); 955 if (!Rels) 956 return Rels.takeError(); 957 for (const Elf_Rel &Rel : *Rels) { 958 ELFYAML::Relocation R; 959 if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R)) 960 return std::move(E); 961 S->Relocations->push_back(R); 962 } 963 } else { 964 auto Rels = Obj.relas(*Shdr); 965 if (!Rels) 966 return Rels.takeError(); 967 for (const Elf_Rela &Rel : *Rels) { 968 ELFYAML::Relocation R; 969 if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R)) 970 return std::move(E); 971 R.Addend = Rel.r_addend; 972 S->Relocations->push_back(R); 973 } 974 } 975 976 return S.release(); 977 } 978 979 template <class ELFT> 980 Expected<ELFYAML::RelrSection *> 981 ELFDumper<ELFT>::dumpRelrSection(const Elf_Shdr *Shdr) { 982 auto S = std::make_unique<ELFYAML::RelrSection>(); 983 if (auto E = dumpCommonSection(Shdr, *S)) 984 return std::move(E); 985 986 if (Expected<ArrayRef<Elf_Relr>> Relrs = Obj.relrs(*Shdr)) { 987 S->Entries.emplace(); 988 for (Elf_Relr Rel : *Relrs) 989 S->Entries->emplace_back(Rel); 990 return S.release(); 991 } else { 992 // Ignore. We are going to dump the data as raw content below. 993 consumeError(Relrs.takeError()); 994 } 995 996 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr); 997 if (!ContentOrErr) 998 return ContentOrErr.takeError(); 999 S->Content = *ContentOrErr; 1000 return S.release(); 1001 } 1002 1003 template <class ELFT> 1004 Expected<ELFYAML::RawContentSection *> 1005 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) { 1006 auto S = std::make_unique<ELFYAML::RawContentSection>(); 1007 if (Error E = dumpCommonSection(Shdr, *S)) 1008 return std::move(E); 1009 1010 unsigned SecIndex = Shdr - &Sections[0]; 1011 if (SecIndex != 0 || Shdr->sh_type != ELF::SHT_NULL) { 1012 auto ContentOrErr = Obj.getSectionContents(*Shdr); 1013 if (!ContentOrErr) 1014 return ContentOrErr.takeError(); 1015 ArrayRef<uint8_t> Content = *ContentOrErr; 1016 if (!Content.empty()) 1017 S->Content = yaml::BinaryRef(Content); 1018 } else { 1019 S->Size = static_cast<llvm::yaml::Hex64>(Shdr->sh_size); 1020 } 1021 1022 if (Shdr->sh_info) 1023 S->Info = static_cast<llvm::yaml::Hex64>(Shdr->sh_info); 1024 return S.release(); 1025 } 1026 1027 template <class ELFT> 1028 Expected<ELFYAML::SymtabShndxSection *> 1029 ELFDumper<ELFT>::dumpSymtabShndxSection(const Elf_Shdr *Shdr) { 1030 auto S = std::make_unique<ELFYAML::SymtabShndxSection>(); 1031 if (Error E = dumpCommonSection(Shdr, *S)) 1032 return std::move(E); 1033 1034 auto EntriesOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(*Shdr); 1035 if (!EntriesOrErr) 1036 return EntriesOrErr.takeError(); 1037 1038 S->Entries.emplace(); 1039 for (const Elf_Word &E : *EntriesOrErr) 1040 S->Entries->push_back(E); 1041 return S.release(); 1042 } 1043 1044 template <class ELFT> 1045 Expected<ELFYAML::NoBitsSection *> 1046 ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) { 1047 auto S = std::make_unique<ELFYAML::NoBitsSection>(); 1048 if (Error E = dumpCommonSection(Shdr, *S)) 1049 return std::move(E); 1050 if (Shdr->sh_size) 1051 S->Size = static_cast<llvm::yaml::Hex64>(Shdr->sh_size); 1052 return S.release(); 1053 } 1054 1055 template <class ELFT> 1056 Expected<ELFYAML::NoteSection *> 1057 ELFDumper<ELFT>::dumpNoteSection(const Elf_Shdr *Shdr) { 1058 auto S = std::make_unique<ELFYAML::NoteSection>(); 1059 if (Error E = dumpCommonSection(Shdr, *S)) 1060 return std::move(E); 1061 1062 auto ContentOrErr = Obj.getSectionContents(*Shdr); 1063 if (!ContentOrErr) 1064 return ContentOrErr.takeError(); 1065 1066 std::vector<ELFYAML::NoteEntry> Entries; 1067 ArrayRef<uint8_t> Content = *ContentOrErr; 1068 while (!Content.empty()) { 1069 if (Content.size() < sizeof(Elf_Nhdr)) { 1070 S->Content = yaml::BinaryRef(*ContentOrErr); 1071 return S.release(); 1072 } 1073 1074 const Elf_Nhdr *Header = reinterpret_cast<const Elf_Nhdr *>(Content.data()); 1075 if (Content.size() < Header->getSize()) { 1076 S->Content = yaml::BinaryRef(*ContentOrErr); 1077 return S.release(); 1078 } 1079 1080 Elf_Note Note(*Header); 1081 Entries.push_back( 1082 {Note.getName(), Note.getDesc(), (llvm::yaml::Hex32)Note.getType()}); 1083 1084 Content = Content.drop_front(Header->getSize()); 1085 } 1086 1087 S->Notes = std::move(Entries); 1088 return S.release(); 1089 } 1090 1091 template <class ELFT> 1092 Expected<ELFYAML::HashSection *> 1093 ELFDumper<ELFT>::dumpHashSection(const Elf_Shdr *Shdr) { 1094 auto S = std::make_unique<ELFYAML::HashSection>(); 1095 if (Error E = dumpCommonSection(Shdr, *S)) 1096 return std::move(E); 1097 1098 auto ContentOrErr = Obj.getSectionContents(*Shdr); 1099 if (!ContentOrErr) 1100 return ContentOrErr.takeError(); 1101 1102 ArrayRef<uint8_t> Content = *ContentOrErr; 1103 if (Content.size() % 4 != 0 || Content.size() < 8) { 1104 S->Content = yaml::BinaryRef(Content); 1105 return S.release(); 1106 } 1107 1108 DataExtractor::Cursor Cur(0); 1109 DataExtractor Data(Content, Obj.isLE(), /*AddressSize=*/0); 1110 uint32_t NBucket = Data.getU32(Cur); 1111 uint32_t NChain = Data.getU32(Cur); 1112 if (Content.size() != (2 + NBucket + NChain) * 4) { 1113 S->Content = yaml::BinaryRef(Content); 1114 if (Cur) 1115 return S.release(); 1116 llvm_unreachable("entries were not read correctly"); 1117 } 1118 1119 S->Bucket.emplace(NBucket); 1120 for (uint32_t &V : *S->Bucket) 1121 V = Data.getU32(Cur); 1122 1123 S->Chain.emplace(NChain); 1124 for (uint32_t &V : *S->Chain) 1125 V = Data.getU32(Cur); 1126 1127 if (Cur) 1128 return S.release(); 1129 llvm_unreachable("entries were not read correctly"); 1130 } 1131 1132 template <class ELFT> 1133 Expected<ELFYAML::GnuHashSection *> 1134 ELFDumper<ELFT>::dumpGnuHashSection(const Elf_Shdr *Shdr) { 1135 auto S = std::make_unique<ELFYAML::GnuHashSection>(); 1136 if (Error E = dumpCommonSection(Shdr, *S)) 1137 return std::move(E); 1138 1139 auto ContentOrErr = Obj.getSectionContents(*Shdr); 1140 if (!ContentOrErr) 1141 return ContentOrErr.takeError(); 1142 1143 unsigned AddrSize = ELFT::Is64Bits ? 8 : 4; 1144 ArrayRef<uint8_t> Content = *ContentOrErr; 1145 DataExtractor Data(Content, Obj.isLE(), AddrSize); 1146 1147 ELFYAML::GnuHashHeader Header; 1148 DataExtractor::Cursor Cur(0); 1149 uint32_t NBuckets = Data.getU32(Cur); 1150 Header.SymNdx = Data.getU32(Cur); 1151 uint32_t MaskWords = Data.getU32(Cur); 1152 Header.Shift2 = Data.getU32(Cur); 1153 1154 // Set just the raw binary content if we were unable to read the header 1155 // or when the section data is truncated or malformed. 1156 uint64_t Size = Data.getData().size() - Cur.tell(); 1157 if (!Cur || (Size < MaskWords * AddrSize + NBuckets * 4) || 1158 (Size % 4 != 0)) { 1159 consumeError(Cur.takeError()); 1160 S->Content = yaml::BinaryRef(Content); 1161 return S.release(); 1162 } 1163 1164 S->Header = Header; 1165 1166 S->BloomFilter.emplace(MaskWords); 1167 for (llvm::yaml::Hex64 &Val : *S->BloomFilter) 1168 Val = Data.getAddress(Cur); 1169 1170 S->HashBuckets.emplace(NBuckets); 1171 for (llvm::yaml::Hex32 &Val : *S->HashBuckets) 1172 Val = Data.getU32(Cur); 1173 1174 S->HashValues.emplace((Data.getData().size() - Cur.tell()) / 4); 1175 for (llvm::yaml::Hex32 &Val : *S->HashValues) 1176 Val = Data.getU32(Cur); 1177 1178 if (Cur) 1179 return S.release(); 1180 llvm_unreachable("GnuHashSection was not read correctly"); 1181 } 1182 1183 template <class ELFT> 1184 Expected<ELFYAML::VerdefSection *> 1185 ELFDumper<ELFT>::dumpVerdefSection(const Elf_Shdr *Shdr) { 1186 typedef typename ELFT::Verdef Elf_Verdef; 1187 typedef typename ELFT::Verdaux Elf_Verdaux; 1188 1189 auto S = std::make_unique<ELFYAML::VerdefSection>(); 1190 if (Error E = dumpCommonSection(Shdr, *S)) 1191 return std::move(E); 1192 1193 S->Info = Shdr->sh_info; 1194 1195 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link); 1196 if (!StringTableShdrOrErr) 1197 return StringTableShdrOrErr.takeError(); 1198 1199 auto StringTableOrErr = Obj.getStringTable(**StringTableShdrOrErr); 1200 if (!StringTableOrErr) 1201 return StringTableOrErr.takeError(); 1202 1203 auto Contents = Obj.getSectionContents(*Shdr); 1204 if (!Contents) 1205 return Contents.takeError(); 1206 1207 S->Entries.emplace(); 1208 1209 llvm::ArrayRef<uint8_t> Data = *Contents; 1210 const uint8_t *Buf = Data.data(); 1211 while (Buf) { 1212 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(Buf); 1213 ELFYAML::VerdefEntry Entry; 1214 Entry.Version = Verdef->vd_version; 1215 Entry.Flags = Verdef->vd_flags; 1216 Entry.VersionNdx = Verdef->vd_ndx; 1217 Entry.Hash = Verdef->vd_hash; 1218 1219 const uint8_t *BufAux = Buf + Verdef->vd_aux; 1220 while (BufAux) { 1221 const Elf_Verdaux *Verdaux = 1222 reinterpret_cast<const Elf_Verdaux *>(BufAux); 1223 Entry.VerNames.push_back( 1224 StringTableOrErr->drop_front(Verdaux->vda_name).data()); 1225 BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr; 1226 } 1227 1228 S->Entries->push_back(Entry); 1229 Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr; 1230 } 1231 1232 return S.release(); 1233 } 1234 1235 template <class ELFT> 1236 Expected<ELFYAML::SymverSection *> 1237 ELFDumper<ELFT>::dumpSymverSection(const Elf_Shdr *Shdr) { 1238 typedef typename ELFT::Half Elf_Half; 1239 1240 auto S = std::make_unique<ELFYAML::SymverSection>(); 1241 if (Error E = dumpCommonSection(Shdr, *S)) 1242 return std::move(E); 1243 1244 auto VersionsOrErr = Obj.template getSectionContentsAsArray<Elf_Half>(*Shdr); 1245 if (!VersionsOrErr) 1246 return VersionsOrErr.takeError(); 1247 1248 S->Entries.emplace(); 1249 for (const Elf_Half &E : *VersionsOrErr) 1250 S->Entries->push_back(E); 1251 1252 return S.release(); 1253 } 1254 1255 template <class ELFT> 1256 Expected<ELFYAML::VerneedSection *> 1257 ELFDumper<ELFT>::dumpVerneedSection(const Elf_Shdr *Shdr) { 1258 typedef typename ELFT::Verneed Elf_Verneed; 1259 typedef typename ELFT::Vernaux Elf_Vernaux; 1260 1261 auto S = std::make_unique<ELFYAML::VerneedSection>(); 1262 if (Error E = dumpCommonSection(Shdr, *S)) 1263 return std::move(E); 1264 1265 S->Info = Shdr->sh_info; 1266 1267 auto Contents = Obj.getSectionContents(*Shdr); 1268 if (!Contents) 1269 return Contents.takeError(); 1270 1271 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link); 1272 if (!StringTableShdrOrErr) 1273 return StringTableShdrOrErr.takeError(); 1274 1275 auto StringTableOrErr = Obj.getStringTable(**StringTableShdrOrErr); 1276 if (!StringTableOrErr) 1277 return StringTableOrErr.takeError(); 1278 1279 S->VerneedV.emplace(); 1280 1281 llvm::ArrayRef<uint8_t> Data = *Contents; 1282 const uint8_t *Buf = Data.data(); 1283 while (Buf) { 1284 const Elf_Verneed *Verneed = reinterpret_cast<const Elf_Verneed *>(Buf); 1285 1286 ELFYAML::VerneedEntry Entry; 1287 Entry.Version = Verneed->vn_version; 1288 Entry.File = 1289 StringRef(StringTableOrErr->drop_front(Verneed->vn_file).data()); 1290 1291 const uint8_t *BufAux = Buf + Verneed->vn_aux; 1292 while (BufAux) { 1293 const Elf_Vernaux *Vernaux = 1294 reinterpret_cast<const Elf_Vernaux *>(BufAux); 1295 1296 ELFYAML::VernauxEntry Aux; 1297 Aux.Hash = Vernaux->vna_hash; 1298 Aux.Flags = Vernaux->vna_flags; 1299 Aux.Other = Vernaux->vna_other; 1300 Aux.Name = 1301 StringRef(StringTableOrErr->drop_front(Vernaux->vna_name).data()); 1302 1303 Entry.AuxV.push_back(Aux); 1304 BufAux = Vernaux->vna_next ? BufAux + Vernaux->vna_next : nullptr; 1305 } 1306 1307 S->VerneedV->push_back(Entry); 1308 Buf = Verneed->vn_next ? Buf + Verneed->vn_next : nullptr; 1309 } 1310 1311 return S.release(); 1312 } 1313 1314 template <class ELFT> 1315 Expected<StringRef> ELFDumper<ELFT>::getSymbolName(uint32_t SymtabNdx, 1316 uint32_t SymbolNdx) { 1317 auto SymtabOrErr = Obj.getSection(SymtabNdx); 1318 if (!SymtabOrErr) 1319 return SymtabOrErr.takeError(); 1320 1321 const Elf_Shdr *Symtab = *SymtabOrErr; 1322 auto SymOrErr = Obj.getSymbol(Symtab, SymbolNdx); 1323 if (!SymOrErr) 1324 return SymOrErr.takeError(); 1325 1326 auto StrTabOrErr = Obj.getStringTableForSymtab(*Symtab); 1327 if (!StrTabOrErr) 1328 return StrTabOrErr.takeError(); 1329 return getUniquedSymbolName(*SymOrErr, *StrTabOrErr, Symtab); 1330 } 1331 1332 template <class ELFT> 1333 Expected<ELFYAML::GroupSection *> 1334 ELFDumper<ELFT>::dumpGroupSection(const Elf_Shdr *Shdr) { 1335 auto S = std::make_unique<ELFYAML::GroupSection>(); 1336 if (Error E = dumpCommonSection(Shdr, *S)) 1337 return std::move(E); 1338 1339 // Get symbol with index sh_info. This symbol's name is the signature of the group. 1340 Expected<StringRef> SymbolName = getSymbolName(Shdr->sh_link, Shdr->sh_info); 1341 if (!SymbolName) 1342 return SymbolName.takeError(); 1343 S->Signature = *SymbolName; 1344 1345 auto MembersOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(*Shdr); 1346 if (!MembersOrErr) 1347 return MembersOrErr.takeError(); 1348 1349 S->Members.emplace(); 1350 for (Elf_Word Member : *MembersOrErr) { 1351 if (Member == llvm::ELF::GRP_COMDAT) { 1352 S->Members->push_back({"GRP_COMDAT"}); 1353 continue; 1354 } 1355 1356 auto SHdrOrErr = Obj.getSection(Member); 1357 if (!SHdrOrErr) 1358 return SHdrOrErr.takeError(); 1359 auto NameOrErr = getUniquedSectionName(*SHdrOrErr); 1360 if (!NameOrErr) 1361 return NameOrErr.takeError(); 1362 S->Members->push_back({*NameOrErr}); 1363 } 1364 return S.release(); 1365 } 1366 1367 template <class ELFT> 1368 Expected<ELFYAML::ARMIndexTableSection *> 1369 ELFDumper<ELFT>::dumpARMIndexTableSection(const Elf_Shdr *Shdr) { 1370 auto S = std::make_unique<ELFYAML::ARMIndexTableSection>(); 1371 if (Error E = dumpCommonSection(Shdr, *S)) 1372 return std::move(E); 1373 1374 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr); 1375 if (!ContentOrErr) 1376 return ContentOrErr.takeError(); 1377 1378 if (ContentOrErr->size() % (sizeof(Elf_Word) * 2) != 0) { 1379 S->Content = yaml::BinaryRef(*ContentOrErr); 1380 return S.release(); 1381 } 1382 1383 ArrayRef<Elf_Word> Words( 1384 reinterpret_cast<const Elf_Word *>(ContentOrErr->data()), 1385 ContentOrErr->size() / sizeof(Elf_Word)); 1386 1387 S->Entries.emplace(); 1388 for (size_t I = 0, E = Words.size(); I != E; I += 2) 1389 S->Entries->push_back({(yaml::Hex32)Words[I], (yaml::Hex32)Words[I + 1]}); 1390 1391 return S.release(); 1392 } 1393 1394 template <class ELFT> 1395 Expected<ELFYAML::MipsABIFlags *> 1396 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) { 1397 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS && 1398 "Section type is not SHT_MIPS_ABIFLAGS"); 1399 auto S = std::make_unique<ELFYAML::MipsABIFlags>(); 1400 if (Error E = dumpCommonSection(Shdr, *S)) 1401 return std::move(E); 1402 1403 auto ContentOrErr = Obj.getSectionContents(*Shdr); 1404 if (!ContentOrErr) 1405 return ContentOrErr.takeError(); 1406 1407 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>( 1408 ContentOrErr.get().data()); 1409 S->Version = Flags->version; 1410 S->ISALevel = Flags->isa_level; 1411 S->ISARevision = Flags->isa_rev; 1412 S->GPRSize = Flags->gpr_size; 1413 S->CPR1Size = Flags->cpr1_size; 1414 S->CPR2Size = Flags->cpr2_size; 1415 S->FpABI = Flags->fp_abi; 1416 S->ISAExtension = Flags->isa_ext; 1417 S->ASEs = Flags->ases; 1418 S->Flags1 = Flags->flags1; 1419 S->Flags2 = Flags->flags2; 1420 return S.release(); 1421 } 1422 1423 template <class ELFT> 1424 static Error elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj, 1425 std::unique_ptr<DWARFContext> DWARFCtx) { 1426 ELFDumper<ELFT> Dumper(Obj, std::move(DWARFCtx)); 1427 Expected<ELFYAML::Object *> YAMLOrErr = Dumper.dump(); 1428 if (!YAMLOrErr) 1429 return YAMLOrErr.takeError(); 1430 1431 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get()); 1432 yaml::Output Yout(Out); 1433 Yout << *YAML; 1434 1435 return Error::success(); 1436 } 1437 1438 Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) { 1439 std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj); 1440 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj)) 1441 return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx)); 1442 1443 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj)) 1444 return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx)); 1445 1446 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj)) 1447 return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx)); 1448 1449 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj)) 1450 return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx)); 1451 1452 llvm_unreachable("unknown ELF file format"); 1453 } 1454