1 //===------ utils/elf2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "Error.h" 11 #include "obj2yaml.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/Object/ELFObjectFile.h" 14 #include "llvm/Object/ELFYAML.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 object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; 26 typedef typename object::ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter; 27 typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word; 28 29 const object::ELFFile<ELFT> &Obj; 30 31 std::error_code dumpSymbol(Elf_Sym_Iter Sym, ELFYAML::Symbol &S); 32 std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S); 33 std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr, 34 ELFYAML::RelocationSection &S); 35 template <class RelT> 36 std::error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel, 37 ELFYAML::Relocation &R); 38 39 ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr); 40 ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr); 41 ErrorOr<ELFYAML::RawContentSection *> 42 dumpContentSection(const Elf_Shdr *Shdr); 43 ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr); 44 ErrorOr<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr); 45 46 public: 47 ELFDumper(const object::ELFFile<ELFT> &O); 48 ErrorOr<ELFYAML::Object *> dump(); 49 }; 50 51 } 52 53 template <class ELFT> 54 ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O) 55 : Obj(O) {} 56 57 template <class ELFT> 58 ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() { 59 auto Y = make_unique<ELFYAML::Object>(); 60 61 // Dump header 62 Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass()); 63 Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding()); 64 Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI]; 65 Y->Header.Type = Obj.getHeader()->e_type; 66 Y->Header.Machine = Obj.getHeader()->e_machine; 67 Y->Header.Flags = Obj.getHeader()->e_flags; 68 Y->Header.Entry = Obj.getHeader()->e_entry; 69 70 // Dump sections 71 for (const Elf_Shdr &Sec : Obj.sections()) { 72 switch (Sec.sh_type) { 73 case ELF::SHT_NULL: 74 case ELF::SHT_SYMTAB: 75 case ELF::SHT_DYNSYM: 76 case ELF::SHT_STRTAB: 77 // Do not dump these sections. 78 break; 79 case ELF::SHT_RELA: { 80 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec); 81 if (std::error_code EC = S.getError()) 82 return EC; 83 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get())); 84 break; 85 } 86 case ELF::SHT_REL: { 87 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec); 88 if (std::error_code EC = S.getError()) 89 return EC; 90 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get())); 91 break; 92 } 93 case ELF::SHT_GROUP: { 94 ErrorOr<ELFYAML::Group *> G = dumpGroup(&Sec); 95 if (std::error_code EC = G.getError()) 96 return EC; 97 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get())); 98 break; 99 } 100 case ELF::SHT_MIPS_ABIFLAGS: { 101 ErrorOr<ELFYAML::MipsABIFlags *> G = dumpMipsABIFlags(&Sec); 102 if (std::error_code EC = G.getError()) 103 return EC; 104 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get())); 105 break; 106 } 107 default: { 108 ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec); 109 if (std::error_code EC = S.getError()) 110 return EC; 111 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get())); 112 } 113 } 114 } 115 116 // Dump symbols 117 bool IsFirstSym = true; 118 for (auto SI = Obj.begin_symbols(), SE = Obj.end_symbols(); SI != SE; ++SI) { 119 if (IsFirstSym) { 120 IsFirstSym = false; 121 continue; 122 } 123 124 ELFYAML::Symbol S; 125 if (std::error_code EC = ELFDumper<ELFT>::dumpSymbol(SI, S)) 126 return EC; 127 128 switch (SI->getBinding()) 129 { 130 case ELF::STB_LOCAL: 131 Y->Symbols.Local.push_back(S); 132 break; 133 case ELF::STB_GLOBAL: 134 Y->Symbols.Global.push_back(S); 135 break; 136 case ELF::STB_WEAK: 137 Y->Symbols.Weak.push_back(S); 138 break; 139 default: 140 llvm_unreachable("Unknown ELF symbol binding"); 141 } 142 } 143 144 return Y.release(); 145 } 146 147 template <class ELFT> 148 std::error_code ELFDumper<ELFT>::dumpSymbol(Elf_Sym_Iter Sym, 149 ELFYAML::Symbol &S) { 150 S.Type = Sym->getType(); 151 S.Value = Sym->st_value; 152 S.Size = Sym->st_size; 153 S.Other = Sym->st_other; 154 155 ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym); 156 if (std::error_code EC = NameOrErr.getError()) 157 return EC; 158 S.Name = NameOrErr.get(); 159 160 const Elf_Shdr *Shdr = Obj.getSection(&*Sym); 161 if (!Shdr) 162 return obj2yaml_error::success; 163 164 NameOrErr = Obj.getSectionName(Shdr); 165 if (std::error_code EC = NameOrErr.getError()) 166 return EC; 167 S.Section = NameOrErr.get(); 168 169 return obj2yaml_error::success; 170 } 171 172 template <class ELFT> 173 template <class RelT> 174 std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr, 175 const RelT *Rel, 176 ELFYAML::Relocation &R) { 177 R.Type = Rel->getType(Obj.isMips64EL()); 178 R.Offset = Rel->r_offset; 179 R.Addend = 0; 180 181 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel); 182 if (!NamePair.first) 183 return obj2yaml_error::success; 184 185 ErrorOr<StringRef> NameOrErr = 186 Obj.getSymbolName(NamePair.first, NamePair.second); 187 if (std::error_code EC = NameOrErr.getError()) 188 return EC; 189 R.Symbol = NameOrErr.get(); 190 191 return obj2yaml_error::success; 192 } 193 194 template <class ELFT> 195 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr, 196 ELFYAML::Section &S) { 197 S.Type = Shdr->sh_type; 198 S.Flags = Shdr->sh_flags; 199 S.Address = Shdr->sh_addr; 200 S.AddressAlign = Shdr->sh_addralign; 201 202 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr); 203 if (std::error_code EC = NameOrErr.getError()) 204 return EC; 205 S.Name = NameOrErr.get(); 206 207 if (Shdr->sh_link != ELF::SHN_UNDEF) { 208 if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) { 209 NameOrErr = Obj.getSectionName(LinkSection); 210 if (std::error_code EC = NameOrErr.getError()) 211 return EC; 212 S.Link = NameOrErr.get(); 213 } 214 } 215 216 return obj2yaml_error::success; 217 } 218 219 template <class ELFT> 220 std::error_code 221 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr, 222 ELFYAML::RelocationSection &S) { 223 if (std::error_code EC = dumpCommonSection(Shdr, S)) 224 return EC; 225 226 if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) { 227 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(InfoSection); 228 if (std::error_code EC = NameOrErr.getError()) 229 return EC; 230 S.Info = NameOrErr.get(); 231 } 232 233 return obj2yaml_error::success; 234 } 235 236 template <class ELFT> 237 ErrorOr<ELFYAML::RelocationSection *> 238 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) { 239 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL"); 240 auto S = make_unique<ELFYAML::RelocationSection>(); 241 242 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) 243 return EC; 244 245 for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE; 246 ++RI) { 247 ELFYAML::Relocation R; 248 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R)) 249 return EC; 250 S->Relocations.push_back(R); 251 } 252 253 return S.release(); 254 } 255 256 template <class ELFT> 257 ErrorOr<ELFYAML::RelocationSection *> 258 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) { 259 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA"); 260 auto S = make_unique<ELFYAML::RelocationSection>(); 261 262 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) 263 return EC; 264 265 for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE; 266 ++RI) { 267 ELFYAML::Relocation R; 268 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R)) 269 return EC; 270 R.Addend = RI->r_addend; 271 S->Relocations.push_back(R); 272 } 273 274 return S.release(); 275 } 276 277 template <class ELFT> 278 ErrorOr<ELFYAML::RawContentSection *> 279 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) { 280 auto S = make_unique<ELFYAML::RawContentSection>(); 281 282 if (std::error_code EC = dumpCommonSection(Shdr, *S)) 283 return EC; 284 285 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr); 286 if (std::error_code EC = ContentOrErr.getError()) 287 return EC; 288 S->Content = yaml::BinaryRef(ContentOrErr.get()); 289 S->Size = S->Content.binary_size(); 290 291 return S.release(); 292 } 293 294 template <class ELFT> 295 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) { 296 auto S = make_unique<ELFYAML::Group>(); 297 298 if (std::error_code EC = dumpCommonSection(Shdr, *S)) 299 return EC; 300 // Get sh_info which is the signature. 301 const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info); 302 const Elf_Shdr *symtab = Obj.getSection(Shdr->sh_link); 303 auto sectionContents = Obj.getSectionContents(Shdr); 304 if (std::error_code ec = sectionContents.getError()) 305 return ec; 306 ErrorOr<StringRef> symbolName = Obj.getSymbolName(symtab, symbol); 307 if (std::error_code EC = symbolName.getError()) 308 return EC; 309 S->Info = *symbolName; 310 const Elf_Word *groupMembers = 311 reinterpret_cast<const Elf_Word *>(sectionContents->data()); 312 const long count = (Shdr->sh_size) / sizeof(Elf_Word); 313 ELFYAML::SectionOrType s; 314 for (int i = 0; i < count; i++) { 315 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) { 316 s.sectionNameOrType = "GRP_COMDAT"; 317 } else { 318 const Elf_Shdr *sHdr = Obj.getSection(groupMembers[i]); 319 ErrorOr<StringRef> sectionName = Obj.getSectionName(sHdr); 320 if (std::error_code ec = sectionName.getError()) 321 return ec; 322 s.sectionNameOrType = *sectionName; 323 } 324 S->Members.push_back(s); 325 } 326 return S.release(); 327 } 328 329 template <class ELFT> 330 ErrorOr<ELFYAML::MipsABIFlags *> 331 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) { 332 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS && 333 "Section type is not SHT_MIPS_ABIFLAGS"); 334 auto S = make_unique<ELFYAML::MipsABIFlags>(); 335 if (std::error_code EC = dumpCommonSection(Shdr, *S)) 336 return EC; 337 338 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr); 339 if (std::error_code EC = ContentOrErr.getError()) 340 return EC; 341 342 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>( 343 ContentOrErr.get().data()); 344 S->Version = Flags->version; 345 S->ISALevel = Flags->isa_level; 346 S->ISARevision = Flags->isa_rev; 347 S->GPRSize = Flags->gpr_size; 348 S->CPR1Size = Flags->cpr1_size; 349 S->CPR2Size = Flags->cpr2_size; 350 S->FpABI = Flags->fp_abi; 351 S->ISAExtension = Flags->isa_ext; 352 S->ASEs = Flags->ases; 353 S->Flags1 = Flags->flags1; 354 S->Flags2 = Flags->flags2; 355 return S.release(); 356 } 357 358 template <class ELFT> 359 static std::error_code elf2yaml(raw_ostream &Out, 360 const object::ELFFile<ELFT> &Obj) { 361 ELFDumper<ELFT> Dumper(Obj); 362 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump(); 363 if (std::error_code EC = YAMLOrErr.getError()) 364 return EC; 365 366 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get()); 367 yaml::Output Yout(Out); 368 Yout << *YAML; 369 370 return std::error_code(); 371 } 372 373 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) { 374 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj)) 375 return elf2yaml(Out, *ELFObj->getELFFile()); 376 377 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj)) 378 return elf2yaml(Out, *ELFObj->getELFFile()); 379 380 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj)) 381 return elf2yaml(Out, *ELFObj->getELFFile()); 382 383 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj)) 384 return elf2yaml(Out, *ELFObj->getELFFile()); 385 386 return obj2yaml_error::unsupported_obj_file_format; 387 } 388