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