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