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