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