1 //===-- ELFDump.cpp - ELF-specific dumper -----------------------*- 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 /// \file 10 /// This file implements the ELF-specific dumper for llvm-objdump. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "ELFDump.h" 15 16 #include "llvm-objdump.h" 17 #include "llvm/Demangle/Demangle.h" 18 #include "llvm/Object/ELFObjectFile.h" 19 #include "llvm/Support/Format.h" 20 #include "llvm/Support/MathExtras.h" 21 #include "llvm/Support/raw_ostream.h" 22 23 using namespace llvm; 24 using namespace llvm::object; 25 using namespace llvm::objdump; 26 27 template <class ELFT> 28 static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> &Elf) { 29 auto DynamicEntriesOrError = Elf.dynamicEntries(); 30 if (!DynamicEntriesOrError) 31 return DynamicEntriesOrError.takeError(); 32 33 for (const typename ELFT::Dyn &Dyn : *DynamicEntriesOrError) { 34 if (Dyn.d_tag == ELF::DT_STRTAB) { 35 auto MappedAddrOrError = Elf.toMappedAddr(Dyn.getPtr()); 36 if (!MappedAddrOrError) 37 consumeError(MappedAddrOrError.takeError()); 38 return StringRef(reinterpret_cast<const char *>(*MappedAddrOrError)); 39 } 40 } 41 42 // If the dynamic segment is not present, we fall back on the sections. 43 auto SectionsOrError = Elf.sections(); 44 if (!SectionsOrError) 45 return SectionsOrError.takeError(); 46 47 for (const typename ELFT::Shdr &Sec : *SectionsOrError) { 48 if (Sec.sh_type == ELF::SHT_DYNSYM) 49 return Elf.getStringTableForSymtab(Sec); 50 } 51 52 return createError("dynamic string table not found"); 53 } 54 55 template <class ELFT> 56 static Error getRelocationValueString(const ELFObjectFile<ELFT> *Obj, 57 const RelocationRef &RelRef, 58 SmallVectorImpl<char> &Result) { 59 const ELFFile<ELFT> &EF = Obj->getELFFile(); 60 DataRefImpl Rel = RelRef.getRawDataRefImpl(); 61 auto SecOrErr = EF.getSection(Rel.d.a); 62 if (!SecOrErr) 63 return SecOrErr.takeError(); 64 65 int64_t Addend = 0; 66 // If there is no Symbol associated with the relocation, we set the undef 67 // boolean value to 'true'. This will prevent us from calling functions that 68 // requires the relocation to be associated with a symbol. 69 // 70 // In SHT_REL case we would need to read the addend from section data. 71 // GNU objdump does not do that and we just follow for simplicity atm. 72 bool Undef = false; 73 if ((*SecOrErr)->sh_type == ELF::SHT_RELA) { 74 const typename ELFT::Rela *ERela = Obj->getRela(Rel); 75 Addend = ERela->r_addend; 76 Undef = ERela->getSymbol(false) == 0; 77 } else if ((*SecOrErr)->sh_type == ELF::SHT_REL) { 78 const typename ELFT::Rel *ERel = Obj->getRel(Rel); 79 Undef = ERel->getSymbol(false) == 0; 80 } else { 81 return make_error<BinaryError>(); 82 } 83 84 // Default scheme is to print Target, as well as "+ <addend>" for nonzero 85 // addend. Should be acceptable for all normal purposes. 86 std::string FmtBuf; 87 raw_string_ostream Fmt(FmtBuf); 88 89 if (!Undef) { 90 symbol_iterator SI = RelRef.getSymbol(); 91 Expected<const typename ELFT::Sym *> SymOrErr = 92 Obj->getSymbol(SI->getRawDataRefImpl()); 93 // TODO: test this error. 94 if (!SymOrErr) 95 return SymOrErr.takeError(); 96 97 if ((*SymOrErr)->getType() == ELF::STT_SECTION) { 98 Expected<section_iterator> SymSI = SI->getSection(); 99 if (!SymSI) 100 return SymSI.takeError(); 101 const typename ELFT::Shdr *SymSec = 102 Obj->getSection((*SymSI)->getRawDataRefImpl()); 103 auto SecName = EF.getSectionName(*SymSec); 104 if (!SecName) 105 return SecName.takeError(); 106 Fmt << *SecName; 107 } else { 108 Expected<StringRef> SymName = SI->getName(); 109 if (!SymName) 110 return SymName.takeError(); 111 if (Demangle) 112 Fmt << demangle(std::string(*SymName)); 113 else 114 Fmt << *SymName; 115 } 116 } else { 117 Fmt << "*ABS*"; 118 } 119 if (Addend != 0) { 120 Fmt << (Addend < 0 121 ? "-" 122 : "+") << format("0x%" PRIx64, 123 (Addend < 0 ? -(uint64_t)Addend : (uint64_t)Addend)); 124 } 125 Fmt.flush(); 126 Result.append(FmtBuf.begin(), FmtBuf.end()); 127 return Error::success(); 128 } 129 130 Error objdump::getELFRelocationValueString(const ELFObjectFileBase *Obj, 131 const RelocationRef &Rel, 132 SmallVectorImpl<char> &Result) { 133 if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj)) 134 return getRelocationValueString(ELF32LE, Rel, Result); 135 if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj)) 136 return getRelocationValueString(ELF64LE, Rel, Result); 137 if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj)) 138 return getRelocationValueString(ELF32BE, Rel, Result); 139 auto *ELF64BE = cast<ELF64BEObjectFile>(Obj); 140 return getRelocationValueString(ELF64BE, Rel, Result); 141 } 142 143 template <class ELFT> 144 static uint64_t getSectionLMA(const ELFFile<ELFT> &Obj, 145 const object::ELFSectionRef &Sec) { 146 auto PhdrRangeOrErr = Obj.program_headers(); 147 if (!PhdrRangeOrErr) 148 report_fatal_error(toString(PhdrRangeOrErr.takeError())); 149 150 // Search for a PT_LOAD segment containing the requested section. Use this 151 // segment's p_addr to calculate the section's LMA. 152 for (const typename ELFT::Phdr &Phdr : *PhdrRangeOrErr) 153 if ((Phdr.p_type == ELF::PT_LOAD) && (Phdr.p_vaddr <= Sec.getAddress()) && 154 (Phdr.p_vaddr + Phdr.p_memsz > Sec.getAddress())) 155 return Sec.getAddress() - Phdr.p_vaddr + Phdr.p_paddr; 156 157 // Return section's VMA if it isn't in a PT_LOAD segment. 158 return Sec.getAddress(); 159 } 160 161 uint64_t objdump::getELFSectionLMA(const object::ELFSectionRef &Sec) { 162 if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Sec.getObject())) 163 return getSectionLMA(ELFObj->getELFFile(), Sec); 164 else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Sec.getObject())) 165 return getSectionLMA(ELFObj->getELFFile(), Sec); 166 else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Sec.getObject())) 167 return getSectionLMA(ELFObj->getELFFile(), Sec); 168 const auto *ELFObj = cast<ELF64BEObjectFile>(Sec.getObject()); 169 return getSectionLMA(ELFObj->getELFFile(), Sec); 170 } 171 172 template <class ELFT> 173 static void printDynamicSection(const ELFFile<ELFT> &Elf, StringRef Filename) { 174 ArrayRef<typename ELFT::Dyn> DynamicEntries = 175 unwrapOrError(Elf.dynamicEntries(), Filename); 176 177 // Find the maximum tag name length to format the value column properly. 178 size_t MaxLen = 0; 179 for (const typename ELFT::Dyn &Dyn : DynamicEntries) 180 MaxLen = std::max(MaxLen, Elf.getDynamicTagAsString(Dyn.d_tag).size()); 181 std::string TagFmt = " %-" + std::to_string(MaxLen) + "s "; 182 183 outs() << "\nDynamic Section:\n"; 184 for (const typename ELFT::Dyn &Dyn : DynamicEntries) { 185 if (Dyn.d_tag == ELF::DT_NULL) 186 continue; 187 188 std::string Str = Elf.getDynamicTagAsString(Dyn.d_tag); 189 outs() << format(TagFmt.c_str(), Str.c_str()); 190 191 const char *Fmt = 192 ELFT::Is64Bits ? "0x%016" PRIx64 "\n" : "0x%08" PRIx64 "\n"; 193 if (Dyn.d_tag == ELF::DT_NEEDED || Dyn.d_tag == ELF::DT_RPATH || 194 Dyn.d_tag == ELF::DT_RUNPATH || Dyn.d_tag == ELF::DT_SONAME || 195 Dyn.d_tag == ELF::DT_AUXILIARY || Dyn.d_tag == ELF::DT_FILTER) { 196 Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf); 197 if (StrTabOrErr) { 198 const char *Data = StrTabOrErr.get().data(); 199 outs() << (Data + Dyn.d_un.d_val) << "\n"; 200 continue; 201 } 202 reportWarning(toString(StrTabOrErr.takeError()), Filename); 203 consumeError(StrTabOrErr.takeError()); 204 } 205 outs() << format(Fmt, (uint64_t)Dyn.d_un.d_val); 206 } 207 } 208 209 template <class ELFT> 210 static void printProgramHeaders(const ELFFile<ELFT> &Obj, StringRef FileName) { 211 outs() << "\nProgram Header:\n"; 212 auto ProgramHeaderOrError = Obj.program_headers(); 213 if (!ProgramHeaderOrError) { 214 reportWarning("unable to read program headers: " + 215 toString(ProgramHeaderOrError.takeError()), 216 FileName); 217 return; 218 } 219 220 for (const typename ELFT::Phdr &Phdr : *ProgramHeaderOrError) { 221 switch (Phdr.p_type) { 222 case ELF::PT_DYNAMIC: 223 outs() << " DYNAMIC "; 224 break; 225 case ELF::PT_GNU_EH_FRAME: 226 outs() << "EH_FRAME "; 227 break; 228 case ELF::PT_GNU_RELRO: 229 outs() << " RELRO "; 230 break; 231 case ELF::PT_GNU_PROPERTY: 232 outs() << " PROPERTY "; 233 break; 234 case ELF::PT_GNU_STACK: 235 outs() << " STACK "; 236 break; 237 case ELF::PT_INTERP: 238 outs() << " INTERP "; 239 break; 240 case ELF::PT_LOAD: 241 outs() << " LOAD "; 242 break; 243 case ELF::PT_NOTE: 244 outs() << " NOTE "; 245 break; 246 case ELF::PT_OPENBSD_BOOTDATA: 247 outs() << " OPENBSD_BOOTDATA "; 248 break; 249 case ELF::PT_OPENBSD_RANDOMIZE: 250 outs() << " OPENBSD_RANDOMIZE "; 251 break; 252 case ELF::PT_OPENBSD_WXNEEDED: 253 outs() << " OPENBSD_WXNEEDED "; 254 break; 255 case ELF::PT_PHDR: 256 outs() << " PHDR "; 257 break; 258 case ELF::PT_TLS: 259 outs() << " TLS "; 260 break; 261 default: 262 outs() << " UNKNOWN "; 263 } 264 265 const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " "; 266 267 outs() << "off " << format(Fmt, (uint64_t)Phdr.p_offset) << "vaddr " 268 << format(Fmt, (uint64_t)Phdr.p_vaddr) << "paddr " 269 << format(Fmt, (uint64_t)Phdr.p_paddr) 270 << format("align 2**%u\n", 271 countTrailingZeros<uint64_t>(Phdr.p_align)) 272 << " filesz " << format(Fmt, (uint64_t)Phdr.p_filesz) 273 << "memsz " << format(Fmt, (uint64_t)Phdr.p_memsz) << "flags " 274 << ((Phdr.p_flags & ELF::PF_R) ? "r" : "-") 275 << ((Phdr.p_flags & ELF::PF_W) ? "w" : "-") 276 << ((Phdr.p_flags & ELF::PF_X) ? "x" : "-") << "\n"; 277 } 278 } 279 280 template <class ELFT> 281 static void printSymbolVersionDependency(ArrayRef<uint8_t> Contents, 282 StringRef StrTab) { 283 outs() << "\nVersion References:\n"; 284 285 const uint8_t *Buf = Contents.data(); 286 while (Buf) { 287 auto *Verneed = reinterpret_cast<const typename ELFT::Verneed *>(Buf); 288 outs() << " required from " 289 << StringRef(StrTab.drop_front(Verneed->vn_file).data()) << ":\n"; 290 291 const uint8_t *BufAux = Buf + Verneed->vn_aux; 292 while (BufAux) { 293 auto *Vernaux = reinterpret_cast<const typename ELFT::Vernaux *>(BufAux); 294 outs() << " " 295 << format("0x%08" PRIx32 " ", (uint32_t)Vernaux->vna_hash) 296 << format("0x%02" PRIx16 " ", (uint16_t)Vernaux->vna_flags) 297 << format("%02" PRIu16 " ", (uint16_t)Vernaux->vna_other) 298 << StringRef(StrTab.drop_front(Vernaux->vna_name).data()) << '\n'; 299 BufAux = Vernaux->vna_next ? BufAux + Vernaux->vna_next : nullptr; 300 } 301 Buf = Verneed->vn_next ? Buf + Verneed->vn_next : nullptr; 302 } 303 } 304 305 template <class ELFT> 306 static void printSymbolVersionDefinition(const typename ELFT::Shdr &Shdr, 307 ArrayRef<uint8_t> Contents, 308 StringRef StrTab) { 309 outs() << "\nVersion definitions:\n"; 310 311 const uint8_t *Buf = Contents.data(); 312 uint32_t VerdefIndex = 1; 313 // sh_info contains the number of entries in the SHT_GNU_verdef section. To 314 // make the index column have consistent width, we should insert blank spaces 315 // according to sh_info. 316 uint16_t VerdefIndexWidth = std::to_string(Shdr.sh_info).size(); 317 while (Buf) { 318 auto *Verdef = reinterpret_cast<const typename ELFT::Verdef *>(Buf); 319 outs() << format_decimal(VerdefIndex++, VerdefIndexWidth) << " " 320 << format("0x%02" PRIx16 " ", (uint16_t)Verdef->vd_flags) 321 << format("0x%08" PRIx32 " ", (uint32_t)Verdef->vd_hash); 322 323 const uint8_t *BufAux = Buf + Verdef->vd_aux; 324 uint16_t VerdauxIndex = 0; 325 while (BufAux) { 326 auto *Verdaux = reinterpret_cast<const typename ELFT::Verdaux *>(BufAux); 327 if (VerdauxIndex) 328 outs() << std::string(VerdefIndexWidth + 17, ' '); 329 outs() << StringRef(StrTab.drop_front(Verdaux->vda_name).data()) << '\n'; 330 BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr; 331 ++VerdauxIndex; 332 } 333 Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr; 334 } 335 } 336 337 template <class ELFT> 338 static void printSymbolVersionInfo(const ELFFile<ELFT> &Elf, 339 StringRef FileName) { 340 ArrayRef<typename ELFT::Shdr> Sections = 341 unwrapOrError(Elf.sections(), FileName); 342 for (const typename ELFT::Shdr &Shdr : Sections) { 343 if (Shdr.sh_type != ELF::SHT_GNU_verneed && 344 Shdr.sh_type != ELF::SHT_GNU_verdef) 345 continue; 346 347 ArrayRef<uint8_t> Contents = 348 unwrapOrError(Elf.getSectionContents(Shdr), FileName); 349 const typename ELFT::Shdr *StrTabSec = 350 unwrapOrError(Elf.getSection(Shdr.sh_link), FileName); 351 StringRef StrTab = unwrapOrError(Elf.getStringTable(*StrTabSec), FileName); 352 353 if (Shdr.sh_type == ELF::SHT_GNU_verneed) 354 printSymbolVersionDependency<ELFT>(Contents, StrTab); 355 else 356 printSymbolVersionDefinition<ELFT>(Shdr, Contents, StrTab); 357 } 358 } 359 360 void objdump::printELFFileHeader(const object::ObjectFile *Obj) { 361 if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj)) 362 printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); 363 else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj)) 364 printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); 365 else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj)) 366 printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); 367 else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj)) 368 printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); 369 } 370 371 void objdump::printELFDynamicSection(const object::ObjectFile *Obj) { 372 if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj)) 373 printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); 374 else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj)) 375 printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); 376 else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj)) 377 printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); 378 else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj)) 379 printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); 380 } 381 382 void objdump::printELFSymbolVersionInfo(const object::ObjectFile *Obj) { 383 if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj)) 384 printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName()); 385 else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj)) 386 printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName()); 387 else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj)) 388 printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName()); 389 else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj)) 390 printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName()); 391 } 392