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