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 321 // yaml2obj sets the alignment of a segment to 1 by default. 322 // We do not print the default alignment to reduce noise in the output. 323 if (Phdr.p_align != 1) 324 PH.Align = static_cast<llvm::yaml::Hex64>(Phdr.p_align); 325 326 // Here we match sections with segments. 327 // It is not possible to have a non-Section chunk, because 328 // obj2yaml does not create Fill chunks. 329 for (const std::unique_ptr<ELFYAML::Chunk> &C : Chunks) { 330 ELFYAML::Section &S = cast<ELFYAML::Section>(*C.get()); 331 if (isInSegment<ELFT>(S, Sections[S.OriginalSecNdx], Phdr)) 332 PH.Sections.push_back({S.Name}); 333 } 334 335 Ret.push_back(PH); 336 } 337 338 return Ret; 339 } 340 341 template <class ELFT> 342 Expected<ELFYAML::RawContentSection *> 343 ELFDumper<ELFT>::dumpPlaceholderSection(const Elf_Shdr *Shdr) { 344 auto S = std::make_unique<ELFYAML::RawContentSection>(); 345 if (Error E = dumpCommonSection(Shdr, *S.get())) 346 return std::move(E); 347 return S.release(); 348 } 349 350 template <class ELFT> 351 Expected<std::vector<std::unique_ptr<ELFYAML::Chunk>>> 352 ELFDumper<ELFT>::dumpSections() { 353 std::vector<std::unique_ptr<ELFYAML::Chunk>> Ret; 354 auto Add = [&](Expected<ELFYAML::Chunk *> SecOrErr) -> Error { 355 if (!SecOrErr) 356 return SecOrErr.takeError(); 357 Ret.emplace_back(*SecOrErr); 358 return Error::success(); 359 }; 360 361 auto GetDumper = [this](unsigned Type) 362 -> std::function<Expected<ELFYAML::Chunk *>(const Elf_Shdr *)> { 363 switch (Type) { 364 case ELF::SHT_DYNAMIC: 365 return [this](const Elf_Shdr *S) { return dumpDynamicSection(S); }; 366 case ELF::SHT_SYMTAB_SHNDX: 367 return [this](const Elf_Shdr *S) { return dumpSymtabShndxSection(S); }; 368 case ELF::SHT_REL: 369 case ELF::SHT_RELA: 370 return [this](const Elf_Shdr *S) { return dumpRelocSection(S); }; 371 case ELF::SHT_RELR: 372 return [this](const Elf_Shdr *S) { return dumpRelrSection(S); }; 373 case ELF::SHT_GROUP: 374 return [this](const Elf_Shdr *S) { return dumpGroup(S); }; 375 case ELF::SHT_MIPS_ABIFLAGS: 376 return [this](const Elf_Shdr *S) { return dumpMipsABIFlags(S); }; 377 case ELF::SHT_NOBITS: 378 return [this](const Elf_Shdr *S) { return dumpNoBitsSection(S); }; 379 case ELF::SHT_NOTE: 380 return [this](const Elf_Shdr *S) { return dumpNoteSection(S); }; 381 case ELF::SHT_HASH: 382 return [this](const Elf_Shdr *S) { return dumpHashSection(S); }; 383 case ELF::SHT_GNU_HASH: 384 return [this](const Elf_Shdr *S) { return dumpGnuHashSection(S); }; 385 case ELF::SHT_GNU_verdef: 386 return [this](const Elf_Shdr *S) { return dumpVerdefSection(S); }; 387 case ELF::SHT_GNU_versym: 388 return [this](const Elf_Shdr *S) { return dumpSymverSection(S); }; 389 case ELF::SHT_GNU_verneed: 390 return [this](const Elf_Shdr *S) { return dumpVerneedSection(S); }; 391 case ELF::SHT_LLVM_ADDRSIG: 392 return [this](const Elf_Shdr *S) { return dumpAddrsigSection(S); }; 393 case ELF::SHT_LLVM_LINKER_OPTIONS: 394 return [this](const Elf_Shdr *S) { return dumpLinkerOptionsSection(S); }; 395 case ELF::SHT_LLVM_DEPENDENT_LIBRARIES: 396 return [this](const Elf_Shdr *S) { 397 return dumpDependentLibrariesSection(S); 398 }; 399 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE: 400 return 401 [this](const Elf_Shdr *S) { return dumpCallGraphProfileSection(S); }; 402 case ELF::SHT_STRTAB: 403 case ELF::SHT_SYMTAB: 404 case ELF::SHT_DYNSYM: 405 // The contents of these sections are described by other parts of the YAML 406 // file. But we still want to dump them, because their properties can be 407 // important. See comments for 'shouldPrintSection()' for more details. 408 return [this](const Elf_Shdr *S) { return dumpPlaceholderSection(S); }; 409 default: 410 return nullptr; 411 } 412 }; 413 414 for (const Elf_Shdr &Sec : Sections) { 415 // We have dedicated dumping functions for most of the section types. 416 // Try to use one of them first. 417 if (std::function<Expected<ELFYAML::Chunk *>(const Elf_Shdr *)> DumpFn = 418 GetDumper(Sec.sh_type)) { 419 if (Error E = Add(DumpFn(&Sec))) 420 return std::move(E); 421 continue; 422 } 423 424 // Recognize some special SHT_PROGBITS sections by name. 425 if (Sec.sh_type == ELF::SHT_PROGBITS) { 426 auto NameOrErr = getUniquedSectionName(&Sec); 427 if (!NameOrErr) 428 return NameOrErr.takeError(); 429 430 if (ELFYAML::StackSizesSection::nameMatches(*NameOrErr)) { 431 if (Error E = Add(dumpStackSizesSection(&Sec))) 432 return std::move(E); 433 continue; 434 } 435 } 436 437 if (Error E = Add(dumpContentSection(&Sec))) 438 return std::move(E); 439 } 440 441 return std::move(Ret); 442 } 443 444 template <class ELFT> 445 Error ELFDumper<ELFT>::dumpSymbols(const Elf_Shdr *Symtab, 446 std::vector<ELFYAML::Symbol> &Symbols) { 447 if (!Symtab) 448 return Error::success(); 449 450 auto StrTableOrErr = Obj.getStringTableForSymtab(*Symtab); 451 if (!StrTableOrErr) 452 return StrTableOrErr.takeError(); 453 StringRef StrTable = *StrTableOrErr; 454 455 auto SymtabOrErr = Obj.symbols(Symtab); 456 if (!SymtabOrErr) 457 return SymtabOrErr.takeError(); 458 459 if (Symtab->sh_type == ELF::SHT_SYMTAB) { 460 SymTable = *SymtabOrErr; 461 SymbolNames.resize(SymTable.size()); 462 } 463 464 for (const auto &Sym : (*SymtabOrErr).drop_front()) { 465 ELFYAML::Symbol S; 466 if (auto EC = dumpSymbol(&Sym, Symtab, StrTable, S)) 467 return EC; 468 Symbols.push_back(S); 469 } 470 471 return Error::success(); 472 } 473 474 template <class ELFT> 475 Error ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, 476 StringRef StrTable, ELFYAML::Symbol &S) { 477 S.Type = Sym->getType(); 478 S.Value = Sym->st_value; 479 S.Size = Sym->st_size; 480 S.Other = Sym->st_other; 481 S.Binding = Sym->getBinding(); 482 483 Expected<StringRef> SymbolNameOrErr = 484 getUniquedSymbolName(Sym, StrTable, SymTab); 485 if (!SymbolNameOrErr) 486 return SymbolNameOrErr.takeError(); 487 S.Name = SymbolNameOrErr.get(); 488 489 if (Sym->st_shndx >= ELF::SHN_LORESERVE) { 490 S.Index = (ELFYAML::ELF_SHN)Sym->st_shndx; 491 return Error::success(); 492 } 493 494 auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable); 495 if (!ShdrOrErr) 496 return ShdrOrErr.takeError(); 497 const Elf_Shdr *Shdr = *ShdrOrErr; 498 if (!Shdr) 499 return Error::success(); 500 501 auto NameOrErr = getUniquedSectionName(Shdr); 502 if (!NameOrErr) 503 return NameOrErr.takeError(); 504 S.Section = NameOrErr.get(); 505 506 return Error::success(); 507 } 508 509 template <class ELFT> 510 template <class RelT> 511 Error ELFDumper<ELFT>::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab, 512 ELFYAML::Relocation &R) { 513 R.Type = Rel->getType(Obj.isMips64EL()); 514 R.Offset = Rel->r_offset; 515 R.Addend = 0; 516 517 auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab); 518 if (!SymOrErr) 519 return SymOrErr.takeError(); 520 521 // We have might have a relocation with symbol index 0, 522 // e.g. R_X86_64_NONE or R_X86_64_GOTPC32. 523 const Elf_Sym *Sym = *SymOrErr; 524 if (!Sym) 525 return Error::success(); 526 527 auto StrTabSec = Obj.getSection(SymTab->sh_link); 528 if (!StrTabSec) 529 return StrTabSec.takeError(); 530 auto StrTabOrErr = Obj.getStringTable(*StrTabSec); 531 if (!StrTabOrErr) 532 return StrTabOrErr.takeError(); 533 534 Expected<StringRef> NameOrErr = 535 getUniquedSymbolName(Sym, *StrTabOrErr, SymTab); 536 if (!NameOrErr) 537 return NameOrErr.takeError(); 538 R.Symbol = NameOrErr.get(); 539 540 return Error::success(); 541 } 542 543 template <class ELFT> 544 static unsigned getDefaultShEntSize(ELFYAML::ELF_SHT SecType) { 545 switch (SecType) { 546 case ELF::SHT_REL: 547 return sizeof(typename ELFT::Rel); 548 case ELF::SHT_RELA: 549 return sizeof(typename ELFT::Rela); 550 case ELF::SHT_RELR: 551 return sizeof(typename ELFT::Relr); 552 case ELF::SHT_DYNAMIC: 553 return sizeof(typename ELFT::Dyn); 554 default: 555 return 0; 556 } 557 } 558 559 template <class ELFT> 560 Error ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr, 561 ELFYAML::Section &S) { 562 // Dump fields. We do not dump the ShOffset field. When not explicitly 563 // set, the value is set by yaml2obj automatically. 564 S.Type = Shdr->sh_type; 565 if (Shdr->sh_flags) 566 S.Flags = static_cast<ELFYAML::ELF_SHF>(Shdr->sh_flags); 567 if (Shdr->sh_addr) 568 S.Address = static_cast<uint64_t>(Shdr->sh_addr); 569 S.AddressAlign = Shdr->sh_addralign; 570 571 if (Shdr->sh_entsize != getDefaultShEntSize<ELFT>(S.Type)) 572 S.EntSize = static_cast<llvm::yaml::Hex64>(Shdr->sh_entsize); 573 574 S.OriginalSecNdx = Shdr - &Sections[0]; 575 576 auto NameOrErr = getUniquedSectionName(Shdr); 577 if (!NameOrErr) 578 return NameOrErr.takeError(); 579 S.Name = NameOrErr.get(); 580 581 if (Shdr->sh_link != ELF::SHN_UNDEF) { 582 auto LinkSection = Obj.getSection(Shdr->sh_link); 583 if (!LinkSection) 584 return make_error<StringError>( 585 "unable to resolve sh_link reference in section '" + S.Name + 586 "': " + toString(LinkSection.takeError()), 587 inconvertibleErrorCode()); 588 589 NameOrErr = getUniquedSectionName(*LinkSection); 590 if (!NameOrErr) 591 return NameOrErr.takeError(); 592 S.Link = NameOrErr.get(); 593 } 594 595 return Error::success(); 596 } 597 598 template <class ELFT> 599 Error ELFDumper<ELFT>::dumpCommonRelocationSection( 600 const Elf_Shdr *Shdr, ELFYAML::RelocationSection &S) { 601 if (Error E = dumpCommonSection(Shdr, S)) 602 return E; 603 604 // Having a zero sh_info field is normal: .rela.dyn is a dynamic 605 // relocation section that normally has no value in this field. 606 if (!Shdr->sh_info) 607 return Error::success(); 608 609 auto InfoSection = Obj.getSection(Shdr->sh_info); 610 if (!InfoSection) 611 return InfoSection.takeError(); 612 613 auto NameOrErr = getUniquedSectionName(*InfoSection); 614 if (!NameOrErr) 615 return NameOrErr.takeError(); 616 S.RelocatableSec = NameOrErr.get(); 617 618 return Error::success(); 619 } 620 621 template <class ELFT> 622 Expected<ELFYAML::StackSizesSection *> 623 ELFDumper<ELFT>::dumpStackSizesSection(const Elf_Shdr *Shdr) { 624 auto S = std::make_unique<ELFYAML::StackSizesSection>(); 625 if (Error E = dumpCommonSection(Shdr, *S)) 626 return std::move(E); 627 628 auto ContentOrErr = Obj.getSectionContents(Shdr); 629 if (!ContentOrErr) 630 return ContentOrErr.takeError(); 631 632 ArrayRef<uint8_t> Content = *ContentOrErr; 633 DataExtractor Data(Content, Obj.isLE(), ELFT::Is64Bits ? 8 : 4); 634 635 std::vector<ELFYAML::StackSizeEntry> Entries; 636 DataExtractor::Cursor Cur(0); 637 while (Cur && Cur.tell() < Content.size()) { 638 uint64_t Address = Data.getAddress(Cur); 639 uint64_t Size = Data.getULEB128(Cur); 640 Entries.push_back({Address, Size}); 641 } 642 643 if (Content.empty() || !Cur) { 644 // If .stack_sizes cannot be decoded, we dump it as an array of bytes. 645 consumeError(Cur.takeError()); 646 S->Content = yaml::BinaryRef(Content); 647 } else { 648 S->Entries = std::move(Entries); 649 } 650 651 return S.release(); 652 } 653 654 template <class ELFT> 655 Expected<ELFYAML::AddrsigSection *> 656 ELFDumper<ELFT>::dumpAddrsigSection(const Elf_Shdr *Shdr) { 657 auto S = std::make_unique<ELFYAML::AddrsigSection>(); 658 if (Error E = dumpCommonSection(Shdr, *S)) 659 return std::move(E); 660 661 auto ContentOrErr = Obj.getSectionContents(Shdr); 662 if (!ContentOrErr) 663 return ContentOrErr.takeError(); 664 665 ArrayRef<uint8_t> Content = *ContentOrErr; 666 DataExtractor::Cursor Cur(0); 667 DataExtractor Data(Content, Obj.isLE(), /*AddressSize=*/0); 668 std::vector<ELFYAML::YAMLFlowString> Symbols; 669 while (Cur && Cur.tell() < Content.size()) { 670 uint64_t SymNdx = Data.getULEB128(Cur); 671 if (!Cur) 672 break; 673 674 Expected<StringRef> SymbolName = getSymbolName(Shdr->sh_link, SymNdx); 675 if (!SymbolName || SymbolName->empty()) { 676 consumeError(SymbolName.takeError()); 677 Symbols.emplace_back( 678 StringRef(std::to_string(SymNdx)).copy(StringAllocator)); 679 continue; 680 } 681 682 Symbols.emplace_back(*SymbolName); 683 } 684 685 if (Cur) { 686 S->Symbols = std::move(Symbols); 687 return S.release(); 688 } 689 690 consumeError(Cur.takeError()); 691 S->Content = yaml::BinaryRef(Content); 692 return S.release(); 693 } 694 695 template <class ELFT> 696 Expected<ELFYAML::LinkerOptionsSection *> 697 ELFDumper<ELFT>::dumpLinkerOptionsSection(const Elf_Shdr *Shdr) { 698 auto S = std::make_unique<ELFYAML::LinkerOptionsSection>(); 699 if (Error E = dumpCommonSection(Shdr, *S)) 700 return std::move(E); 701 702 auto ContentOrErr = Obj.getSectionContents(Shdr); 703 if (!ContentOrErr) 704 return ContentOrErr.takeError(); 705 706 ArrayRef<uint8_t> Content = *ContentOrErr; 707 if (Content.empty() || Content.back() != 0) { 708 S->Content = Content; 709 return S.release(); 710 } 711 712 SmallVector<StringRef, 16> Strings; 713 toStringRef(Content.drop_back()).split(Strings, '\0'); 714 if (Strings.size() % 2 != 0) { 715 S->Content = Content; 716 return S.release(); 717 } 718 719 S->Options.emplace(); 720 for (size_t I = 0, E = Strings.size(); I != E; I += 2) 721 S->Options->push_back({Strings[I], Strings[I + 1]}); 722 723 return S.release(); 724 } 725 726 template <class ELFT> 727 Expected<ELFYAML::DependentLibrariesSection *> 728 ELFDumper<ELFT>::dumpDependentLibrariesSection(const Elf_Shdr *Shdr) { 729 auto DL = std::make_unique<ELFYAML::DependentLibrariesSection>(); 730 if (Error E = dumpCommonSection(Shdr, *DL)) 731 return std::move(E); 732 733 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr); 734 if (!ContentOrErr) 735 return ContentOrErr.takeError(); 736 737 ArrayRef<uint8_t> Content = *ContentOrErr; 738 if (!Content.empty() && Content.back() != 0) { 739 DL->Content = Content; 740 return DL.release(); 741 } 742 743 DL->Libs.emplace(); 744 for (const uint8_t *I = Content.begin(), *E = Content.end(); I < E;) { 745 StringRef Lib((const char *)I); 746 DL->Libs->emplace_back(Lib); 747 I += Lib.size() + 1; 748 } 749 750 return DL.release(); 751 } 752 753 template <class ELFT> 754 Expected<ELFYAML::CallGraphProfileSection *> 755 ELFDumper<ELFT>::dumpCallGraphProfileSection(const Elf_Shdr *Shdr) { 756 auto S = std::make_unique<ELFYAML::CallGraphProfileSection>(); 757 if (Error E = dumpCommonSection(Shdr, *S)) 758 return std::move(E); 759 760 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr); 761 if (!ContentOrErr) 762 return ContentOrErr.takeError(); 763 ArrayRef<uint8_t> Content = *ContentOrErr; 764 765 // Dump the section by using the Content key when it is truncated. 766 // There is no need to create either "Content" or "Entries" fields when the 767 // section is empty. 768 if (Content.empty() || Content.size() % 16 != 0) { 769 if (!Content.empty()) 770 S->Content = yaml::BinaryRef(Content); 771 return S.release(); 772 } 773 774 std::vector<ELFYAML::CallGraphEntry> Entries(Content.size() / 16); 775 DataExtractor Data(Content, Obj.isLE(), /*AddressSize=*/0); 776 DataExtractor::Cursor Cur(0); 777 auto ReadEntry = [&](ELFYAML::CallGraphEntry &E) { 778 uint32_t FromSymIndex = Data.getU32(Cur); 779 uint32_t ToSymIndex = Data.getU32(Cur); 780 E.Weight = Data.getU64(Cur); 781 if (!Cur) { 782 consumeError(Cur.takeError()); 783 return false; 784 } 785 786 Expected<StringRef> From = getSymbolName(Shdr->sh_link, FromSymIndex); 787 Expected<StringRef> To = getSymbolName(Shdr->sh_link, ToSymIndex); 788 if (From && To) { 789 E.From = *From; 790 E.To = *To; 791 return true; 792 } 793 consumeError(From.takeError()); 794 consumeError(To.takeError()); 795 return false; 796 }; 797 798 for (ELFYAML::CallGraphEntry &E : Entries) { 799 if (ReadEntry(E)) 800 continue; 801 S->Content = yaml::BinaryRef(Content); 802 return S.release(); 803 } 804 805 S->Entries = std::move(Entries); 806 return S.release(); 807 } 808 809 template <class ELFT> 810 Expected<ELFYAML::DynamicSection *> 811 ELFDumper<ELFT>::dumpDynamicSection(const Elf_Shdr *Shdr) { 812 auto S = std::make_unique<ELFYAML::DynamicSection>(); 813 if (Error E = dumpCommonSection(Shdr, *S)) 814 return std::move(E); 815 816 auto DynTagsOrErr = Obj.template getSectionContentsAsArray<Elf_Dyn>(Shdr); 817 if (!DynTagsOrErr) 818 return DynTagsOrErr.takeError(); 819 820 for (const Elf_Dyn &Dyn : *DynTagsOrErr) 821 S->Entries.push_back({(ELFYAML::ELF_DYNTAG)Dyn.getTag(), Dyn.getVal()}); 822 823 return S.release(); 824 } 825 826 template <class ELFT> 827 Expected<ELFYAML::RelocationSection *> 828 ELFDumper<ELFT>::dumpRelocSection(const Elf_Shdr *Shdr) { 829 auto S = std::make_unique<ELFYAML::RelocationSection>(); 830 if (auto E = dumpCommonRelocationSection(Shdr, *S)) 831 return std::move(E); 832 833 auto SymTabOrErr = Obj.getSection(Shdr->sh_link); 834 if (!SymTabOrErr) 835 return SymTabOrErr.takeError(); 836 const Elf_Shdr *SymTab = *SymTabOrErr; 837 838 if (Shdr->sh_type == ELF::SHT_REL) { 839 auto Rels = Obj.rels(Shdr); 840 if (!Rels) 841 return Rels.takeError(); 842 for (const Elf_Rel &Rel : *Rels) { 843 ELFYAML::Relocation R; 844 if (Error E = dumpRelocation(&Rel, SymTab, R)) 845 return std::move(E); 846 S->Relocations.push_back(R); 847 } 848 } else { 849 auto Rels = Obj.relas(Shdr); 850 if (!Rels) 851 return Rels.takeError(); 852 for (const Elf_Rela &Rel : *Rels) { 853 ELFYAML::Relocation R; 854 if (Error E = dumpRelocation(&Rel, SymTab, R)) 855 return std::move(E); 856 R.Addend = Rel.r_addend; 857 S->Relocations.push_back(R); 858 } 859 } 860 861 return S.release(); 862 } 863 864 template <class ELFT> 865 Expected<ELFYAML::RelrSection *> 866 ELFDumper<ELFT>::dumpRelrSection(const Elf_Shdr *Shdr) { 867 auto S = std::make_unique<ELFYAML::RelrSection>(); 868 if (auto E = dumpCommonSection(Shdr, *S)) 869 return std::move(E); 870 871 if (Expected<ArrayRef<Elf_Relr>> Relrs = Obj.relrs(Shdr)) { 872 S->Entries.emplace(); 873 for (Elf_Relr Rel : *Relrs) 874 S->Entries->emplace_back(Rel); 875 return S.release(); 876 } else { 877 // Ignore. We are going to dump the data as raw content below. 878 consumeError(Relrs.takeError()); 879 } 880 881 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr); 882 if (!ContentOrErr) 883 return ContentOrErr.takeError(); 884 S->Content = *ContentOrErr; 885 return S.release(); 886 } 887 888 template <class ELFT> 889 Expected<ELFYAML::RawContentSection *> 890 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) { 891 auto S = std::make_unique<ELFYAML::RawContentSection>(); 892 if (Error E = dumpCommonSection(Shdr, *S)) 893 return std::move(E); 894 895 unsigned SecIndex = Shdr - &Sections[0]; 896 if (SecIndex != 0 || Shdr->sh_type != ELF::SHT_NULL) { 897 auto ContentOrErr = Obj.getSectionContents(Shdr); 898 if (!ContentOrErr) 899 return ContentOrErr.takeError(); 900 ArrayRef<uint8_t> Content = *ContentOrErr; 901 if (!Content.empty()) 902 S->Content = yaml::BinaryRef(Content); 903 } else { 904 S->Size = static_cast<llvm::yaml::Hex64>(Shdr->sh_size); 905 } 906 907 if (Shdr->sh_info) 908 S->Info = static_cast<llvm::yaml::Hex64>(Shdr->sh_info); 909 return S.release(); 910 } 911 912 template <class ELFT> 913 Expected<ELFYAML::SymtabShndxSection *> 914 ELFDumper<ELFT>::dumpSymtabShndxSection(const Elf_Shdr *Shdr) { 915 auto S = std::make_unique<ELFYAML::SymtabShndxSection>(); 916 if (Error E = dumpCommonSection(Shdr, *S)) 917 return std::move(E); 918 919 auto EntriesOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(Shdr); 920 if (!EntriesOrErr) 921 return EntriesOrErr.takeError(); 922 for (const Elf_Word &E : *EntriesOrErr) 923 S->Entries.push_back(E); 924 return S.release(); 925 } 926 927 template <class ELFT> 928 Expected<ELFYAML::NoBitsSection *> 929 ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) { 930 auto S = std::make_unique<ELFYAML::NoBitsSection>(); 931 if (Error E = dumpCommonSection(Shdr, *S)) 932 return std::move(E); 933 S->Size = Shdr->sh_size; 934 935 return S.release(); 936 } 937 938 template <class ELFT> 939 Expected<ELFYAML::NoteSection *> 940 ELFDumper<ELFT>::dumpNoteSection(const Elf_Shdr *Shdr) { 941 auto S = std::make_unique<ELFYAML::NoteSection>(); 942 if (Error E = dumpCommonSection(Shdr, *S)) 943 return std::move(E); 944 945 auto ContentOrErr = Obj.getSectionContents(Shdr); 946 if (!ContentOrErr) 947 return ContentOrErr.takeError(); 948 949 std::vector<ELFYAML::NoteEntry> Entries; 950 ArrayRef<uint8_t> Content = *ContentOrErr; 951 while (!Content.empty()) { 952 if (Content.size() < sizeof(Elf_Nhdr)) { 953 S->Content = yaml::BinaryRef(*ContentOrErr); 954 return S.release(); 955 } 956 957 const Elf_Nhdr *Header = reinterpret_cast<const Elf_Nhdr *>(Content.data()); 958 if (Content.size() < Header->getSize()) { 959 S->Content = yaml::BinaryRef(*ContentOrErr); 960 return S.release(); 961 } 962 963 Elf_Note Note(*Header); 964 Entries.push_back( 965 {Note.getName(), Note.getDesc(), (llvm::yaml::Hex32)Note.getType()}); 966 967 Content = Content.drop_front(Header->getSize()); 968 } 969 970 S->Notes = std::move(Entries); 971 return S.release(); 972 } 973 974 template <class ELFT> 975 Expected<ELFYAML::HashSection *> 976 ELFDumper<ELFT>::dumpHashSection(const Elf_Shdr *Shdr) { 977 auto S = std::make_unique<ELFYAML::HashSection>(); 978 if (Error E = dumpCommonSection(Shdr, *S)) 979 return std::move(E); 980 981 auto ContentOrErr = Obj.getSectionContents(Shdr); 982 if (!ContentOrErr) 983 return ContentOrErr.takeError(); 984 985 ArrayRef<uint8_t> Content = *ContentOrErr; 986 if (Content.size() % 4 != 0 || Content.size() < 8) { 987 S->Content = yaml::BinaryRef(Content); 988 return S.release(); 989 } 990 991 DataExtractor::Cursor Cur(0); 992 DataExtractor Data(Content, Obj.isLE(), /*AddressSize=*/0); 993 uint32_t NBucket = Data.getU32(Cur); 994 uint32_t NChain = Data.getU32(Cur); 995 if (Content.size() != (2 + NBucket + NChain) * 4) { 996 S->Content = yaml::BinaryRef(Content); 997 if (Cur) 998 return S.release(); 999 llvm_unreachable("entries were not read correctly"); 1000 } 1001 1002 S->Bucket.emplace(NBucket); 1003 for (uint32_t &V : *S->Bucket) 1004 V = Data.getU32(Cur); 1005 1006 S->Chain.emplace(NChain); 1007 for (uint32_t &V : *S->Chain) 1008 V = Data.getU32(Cur); 1009 1010 if (Cur) 1011 return S.release(); 1012 llvm_unreachable("entries were not read correctly"); 1013 } 1014 1015 template <class ELFT> 1016 Expected<ELFYAML::GnuHashSection *> 1017 ELFDumper<ELFT>::dumpGnuHashSection(const Elf_Shdr *Shdr) { 1018 auto S = std::make_unique<ELFYAML::GnuHashSection>(); 1019 if (Error E = dumpCommonSection(Shdr, *S)) 1020 return std::move(E); 1021 1022 auto ContentOrErr = Obj.getSectionContents(Shdr); 1023 if (!ContentOrErr) 1024 return ContentOrErr.takeError(); 1025 1026 unsigned AddrSize = ELFT::Is64Bits ? 8 : 4; 1027 ArrayRef<uint8_t> Content = *ContentOrErr; 1028 DataExtractor Data(Content, Obj.isLE(), AddrSize); 1029 1030 ELFYAML::GnuHashHeader Header; 1031 DataExtractor::Cursor Cur(0); 1032 uint32_t NBuckets = Data.getU32(Cur); 1033 Header.SymNdx = Data.getU32(Cur); 1034 uint32_t MaskWords = Data.getU32(Cur); 1035 Header.Shift2 = Data.getU32(Cur); 1036 1037 // Set just the raw binary content if we were unable to read the header 1038 // or when the section data is truncated or malformed. 1039 uint64_t Size = Data.getData().size() - Cur.tell(); 1040 if (!Cur || (Size < MaskWords * AddrSize + NBuckets * 4) || 1041 (Size % 4 != 0)) { 1042 consumeError(Cur.takeError()); 1043 S->Content = yaml::BinaryRef(Content); 1044 return S.release(); 1045 } 1046 1047 S->Header = Header; 1048 1049 S->BloomFilter.emplace(MaskWords); 1050 for (llvm::yaml::Hex64 &Val : *S->BloomFilter) 1051 Val = Data.getAddress(Cur); 1052 1053 S->HashBuckets.emplace(NBuckets); 1054 for (llvm::yaml::Hex32 &Val : *S->HashBuckets) 1055 Val = Data.getU32(Cur); 1056 1057 S->HashValues.emplace((Data.getData().size() - Cur.tell()) / 4); 1058 for (llvm::yaml::Hex32 &Val : *S->HashValues) 1059 Val = Data.getU32(Cur); 1060 1061 if (Cur) 1062 return S.release(); 1063 llvm_unreachable("GnuHashSection was not read correctly"); 1064 } 1065 1066 template <class ELFT> 1067 Expected<ELFYAML::VerdefSection *> 1068 ELFDumper<ELFT>::dumpVerdefSection(const Elf_Shdr *Shdr) { 1069 typedef typename ELFT::Verdef Elf_Verdef; 1070 typedef typename ELFT::Verdaux Elf_Verdaux; 1071 1072 auto S = std::make_unique<ELFYAML::VerdefSection>(); 1073 if (Error E = dumpCommonSection(Shdr, *S)) 1074 return std::move(E); 1075 1076 S->Info = Shdr->sh_info; 1077 1078 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link); 1079 if (!StringTableShdrOrErr) 1080 return StringTableShdrOrErr.takeError(); 1081 1082 auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr); 1083 if (!StringTableOrErr) 1084 return StringTableOrErr.takeError(); 1085 1086 auto Contents = Obj.getSectionContents(Shdr); 1087 if (!Contents) 1088 return Contents.takeError(); 1089 1090 S->Entries.emplace(); 1091 1092 llvm::ArrayRef<uint8_t> Data = *Contents; 1093 const uint8_t *Buf = Data.data(); 1094 while (Buf) { 1095 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(Buf); 1096 ELFYAML::VerdefEntry Entry; 1097 Entry.Version = Verdef->vd_version; 1098 Entry.Flags = Verdef->vd_flags; 1099 Entry.VersionNdx = Verdef->vd_ndx; 1100 Entry.Hash = Verdef->vd_hash; 1101 1102 const uint8_t *BufAux = Buf + Verdef->vd_aux; 1103 while (BufAux) { 1104 const Elf_Verdaux *Verdaux = 1105 reinterpret_cast<const Elf_Verdaux *>(BufAux); 1106 Entry.VerNames.push_back( 1107 StringTableOrErr->drop_front(Verdaux->vda_name).data()); 1108 BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr; 1109 } 1110 1111 S->Entries->push_back(Entry); 1112 Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr; 1113 } 1114 1115 return S.release(); 1116 } 1117 1118 template <class ELFT> 1119 Expected<ELFYAML::SymverSection *> 1120 ELFDumper<ELFT>::dumpSymverSection(const Elf_Shdr *Shdr) { 1121 typedef typename ELFT::Half Elf_Half; 1122 1123 auto S = std::make_unique<ELFYAML::SymverSection>(); 1124 if (Error E = dumpCommonSection(Shdr, *S)) 1125 return std::move(E); 1126 1127 auto VersionsOrErr = Obj.template getSectionContentsAsArray<Elf_Half>(Shdr); 1128 if (!VersionsOrErr) 1129 return VersionsOrErr.takeError(); 1130 for (const Elf_Half &E : *VersionsOrErr) 1131 S->Entries.push_back(E); 1132 1133 return S.release(); 1134 } 1135 1136 template <class ELFT> 1137 Expected<ELFYAML::VerneedSection *> 1138 ELFDumper<ELFT>::dumpVerneedSection(const Elf_Shdr *Shdr) { 1139 typedef typename ELFT::Verneed Elf_Verneed; 1140 typedef typename ELFT::Vernaux Elf_Vernaux; 1141 1142 auto S = std::make_unique<ELFYAML::VerneedSection>(); 1143 if (Error E = dumpCommonSection(Shdr, *S)) 1144 return std::move(E); 1145 1146 S->Info = Shdr->sh_info; 1147 1148 auto Contents = Obj.getSectionContents(Shdr); 1149 if (!Contents) 1150 return Contents.takeError(); 1151 1152 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link); 1153 if (!StringTableShdrOrErr) 1154 return StringTableShdrOrErr.takeError(); 1155 1156 auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr); 1157 if (!StringTableOrErr) 1158 return StringTableOrErr.takeError(); 1159 1160 S->VerneedV.emplace(); 1161 1162 llvm::ArrayRef<uint8_t> Data = *Contents; 1163 const uint8_t *Buf = Data.data(); 1164 while (Buf) { 1165 const Elf_Verneed *Verneed = reinterpret_cast<const Elf_Verneed *>(Buf); 1166 1167 ELFYAML::VerneedEntry Entry; 1168 Entry.Version = Verneed->vn_version; 1169 Entry.File = 1170 StringRef(StringTableOrErr->drop_front(Verneed->vn_file).data()); 1171 1172 const uint8_t *BufAux = Buf + Verneed->vn_aux; 1173 while (BufAux) { 1174 const Elf_Vernaux *Vernaux = 1175 reinterpret_cast<const Elf_Vernaux *>(BufAux); 1176 1177 ELFYAML::VernauxEntry Aux; 1178 Aux.Hash = Vernaux->vna_hash; 1179 Aux.Flags = Vernaux->vna_flags; 1180 Aux.Other = Vernaux->vna_other; 1181 Aux.Name = 1182 StringRef(StringTableOrErr->drop_front(Vernaux->vna_name).data()); 1183 1184 Entry.AuxV.push_back(Aux); 1185 BufAux = Vernaux->vna_next ? BufAux + Vernaux->vna_next : nullptr; 1186 } 1187 1188 S->VerneedV->push_back(Entry); 1189 Buf = Verneed->vn_next ? Buf + Verneed->vn_next : nullptr; 1190 } 1191 1192 return S.release(); 1193 } 1194 1195 template <class ELFT> 1196 Expected<StringRef> ELFDumper<ELFT>::getSymbolName(uint32_t SymtabNdx, 1197 uint32_t SymbolNdx) { 1198 auto SymtabOrErr = Obj.getSection(SymtabNdx); 1199 if (!SymtabOrErr) 1200 return SymtabOrErr.takeError(); 1201 1202 const Elf_Shdr *Symtab = *SymtabOrErr; 1203 auto SymOrErr = Obj.getSymbol(Symtab, SymbolNdx); 1204 if (!SymOrErr) 1205 return SymOrErr.takeError(); 1206 1207 auto StrTabOrErr = Obj.getStringTableForSymtab(*Symtab); 1208 if (!StrTabOrErr) 1209 return StrTabOrErr.takeError(); 1210 return getUniquedSymbolName(*SymOrErr, *StrTabOrErr, Symtab); 1211 } 1212 1213 template <class ELFT> 1214 Expected<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) { 1215 auto S = std::make_unique<ELFYAML::Group>(); 1216 if (Error E = dumpCommonSection(Shdr, *S)) 1217 return std::move(E); 1218 1219 // Get symbol with index sh_info. This symbol's name is the signature of the group. 1220 Expected<StringRef> SymbolName = getSymbolName(Shdr->sh_link, Shdr->sh_info); 1221 if (!SymbolName) 1222 return SymbolName.takeError(); 1223 S->Signature = *SymbolName; 1224 1225 auto MembersOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(Shdr); 1226 if (!MembersOrErr) 1227 return MembersOrErr.takeError(); 1228 1229 for (Elf_Word Member : *MembersOrErr) { 1230 if (Member == llvm::ELF::GRP_COMDAT) { 1231 S->Members.push_back({"GRP_COMDAT"}); 1232 continue; 1233 } 1234 1235 auto SHdrOrErr = Obj.getSection(Member); 1236 if (!SHdrOrErr) 1237 return SHdrOrErr.takeError(); 1238 auto NameOrErr = getUniquedSectionName(*SHdrOrErr); 1239 if (!NameOrErr) 1240 return NameOrErr.takeError(); 1241 S->Members.push_back({*NameOrErr}); 1242 } 1243 return S.release(); 1244 } 1245 1246 template <class ELFT> 1247 Expected<ELFYAML::MipsABIFlags *> 1248 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) { 1249 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS && 1250 "Section type is not SHT_MIPS_ABIFLAGS"); 1251 auto S = std::make_unique<ELFYAML::MipsABIFlags>(); 1252 if (Error E = dumpCommonSection(Shdr, *S)) 1253 return std::move(E); 1254 1255 auto ContentOrErr = Obj.getSectionContents(Shdr); 1256 if (!ContentOrErr) 1257 return ContentOrErr.takeError(); 1258 1259 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>( 1260 ContentOrErr.get().data()); 1261 S->Version = Flags->version; 1262 S->ISALevel = Flags->isa_level; 1263 S->ISARevision = Flags->isa_rev; 1264 S->GPRSize = Flags->gpr_size; 1265 S->CPR1Size = Flags->cpr1_size; 1266 S->CPR2Size = Flags->cpr2_size; 1267 S->FpABI = Flags->fp_abi; 1268 S->ISAExtension = Flags->isa_ext; 1269 S->ASEs = Flags->ases; 1270 S->Flags1 = Flags->flags1; 1271 S->Flags2 = Flags->flags2; 1272 return S.release(); 1273 } 1274 1275 template <class ELFT> 1276 static Error elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj) { 1277 ELFDumper<ELFT> Dumper(Obj); 1278 Expected<ELFYAML::Object *> YAMLOrErr = Dumper.dump(); 1279 if (!YAMLOrErr) 1280 return YAMLOrErr.takeError(); 1281 1282 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get()); 1283 yaml::Output Yout(Out); 1284 Yout << *YAML; 1285 1286 return Error::success(); 1287 } 1288 1289 Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) { 1290 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj)) 1291 return elf2yaml(Out, *ELFObj->getELFFile()); 1292 1293 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj)) 1294 return elf2yaml(Out, *ELFObj->getELFFile()); 1295 1296 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj)) 1297 return elf2yaml(Out, *ELFObj->getELFFile()); 1298 1299 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj)) 1300 return elf2yaml(Out, *ELFObj->getELFFile()); 1301 1302 llvm_unreachable("unknown ELF file format"); 1303 } 1304