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