blob: 86359ff44d562b8ff5a52e9215e9a529a2c0773f [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- ELF.h - ELF object file implementation -------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the ELFFile template class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_OBJECT_ELF_H
14#define LLVM_OBJECT_ELF_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/BinaryFormat/ELF.h"
20#include "llvm/Object/ELFTypes.h"
21#include "llvm/Object/Error.h"
22#include "llvm/Support/Endian.h"
23#include "llvm/Support/Error.h"
24#include <cassert>
25#include <cstddef>
26#include <cstdint>
27#include <limits>
28#include <utility>
29
30namespace llvm {
31namespace object {
32
33StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
Andrew Walbran16937d02019-10-22 13:54:20 +010034uint32_t getELFRelativeRelocationType(uint32_t Machine);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010035StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type);
36
37// Subclasses of ELFFile may need this for template instantiation
38inline std::pair<unsigned char, unsigned char>
39getElfArchType(StringRef Object) {
40 if (Object.size() < ELF::EI_NIDENT)
41 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,
42 (uint8_t)ELF::ELFDATANONE);
43 return std::make_pair((uint8_t)Object[ELF::EI_CLASS],
44 (uint8_t)Object[ELF::EI_DATA]);
45}
46
Andrew Walbran3d2c1972020-04-07 12:24:26 +010047static inline Error createError(const Twine &Err) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048 return make_error<StringError>(Err, object_error::parse_failed);
49}
50
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020051enum PPCInstrMasks : uint64_t {
52 PADDI_R12_NO_DISP = 0x0610000039800000,
53 PLD_R12_NO_DISP = 0x04100000E5800000,
54 MTCTR_R12 = 0x7D8903A6,
55 BCTR = 0x4E800420,
56};
57
Andrew Walbran3d2c1972020-04-07 12:24:26 +010058template <class ELFT> class ELFFile;
59
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020060template <class T> struct DataRegion {
61 // This constructor is used when we know the start and the size of a data
62 // region. We assume that Arr does not go past the end of the file.
63 DataRegion(ArrayRef<T> Arr) : First(Arr.data()), Size(Arr.size()) {}
64
65 // Sometimes we only know the start of a data region. We still don't want to
66 // read past the end of the file, so we provide the end of a buffer.
67 DataRegion(const T *Data, const uint8_t *BufferEnd)
68 : First(Data), BufEnd(BufferEnd) {}
69
70 Expected<T> operator[](uint64_t N) {
71 assert(Size || BufEnd);
72 if (Size) {
73 if (N >= *Size)
74 return createError(
75 "the index is greater than or equal to the number of entries (" +
76 Twine(*Size) + ")");
77 } else {
78 const uint8_t *EntryStart = (const uint8_t *)First + N * sizeof(T);
79 if (EntryStart + sizeof(T) > BufEnd)
80 return createError("can't read past the end of the file");
81 }
82 return *(First + N);
83 }
84
85 const T *First;
86 Optional<uint64_t> Size = None;
87 const uint8_t *BufEnd = nullptr;
88};
89
Andrew Walbran3d2c1972020-04-07 12:24:26 +010090template <class ELFT>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020091std::string getSecIndexForError(const ELFFile<ELFT> &Obj,
92 const typename ELFT::Shdr &Sec) {
93 auto TableOrErr = Obj.sections();
Andrew Walbran3d2c1972020-04-07 12:24:26 +010094 if (TableOrErr)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020095 return "[index " + std::to_string(&Sec - &TableOrErr->front()) + "]";
Andrew Walbran3d2c1972020-04-07 12:24:26 +010096 // To make this helper be more convenient for error reporting purposes we
97 // drop the error. But really it should never be triggered. Before this point,
98 // our code should have called 'sections()' and reported a proper error on
99 // failure.
100 llvm::consumeError(TableOrErr.takeError());
101 return "[unknown index]";
102}
103
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100104template <class ELFT>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200105std::string getPhdrIndexForError(const ELFFile<ELFT> &Obj,
106 const typename ELFT::Phdr &Phdr) {
107 auto Headers = Obj.program_headers();
108 if (Headers)
109 return ("[index " + Twine(&Phdr - &Headers->front()) + "]").str();
110 // See comment in the getSecIndexForError() above.
111 llvm::consumeError(Headers.takeError());
112 return "[unknown index]";
113}
114
115static inline Error defaultWarningHandler(const Twine &Msg) {
116 return createError(Msg);
117}
118
119template <class ELFT>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120class ELFFile {
121public:
122 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200123
124 // This is a callback that can be passed to a number of functions.
125 // It can be used to ignore non-critical errors (warnings), which is
126 // useful for dumpers, like llvm-readobj.
127 // It accepts a warning message string and returns a success
128 // when the warning should be ignored or an error otherwise.
129 using WarningHandler = llvm::function_ref<Error(const Twine &Msg)>;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100130
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100131 const uint8_t *base() const { return Buf.bytes_begin(); }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200132 const uint8_t *end() const { return base() + getBufSize(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100133
134 size_t getBufSize() const { return Buf.size(); }
135
136private:
137 StringRef Buf;
138
139 ELFFile(StringRef Object);
140
141public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200142 const Elf_Ehdr &getHeader() const {
143 return *reinterpret_cast<const Elf_Ehdr *>(base());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100144 }
145
146 template <typename T>
147 Expected<const T *> getEntry(uint32_t Section, uint32_t Entry) const;
148 template <typename T>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200149 Expected<const T *> getEntry(const Elf_Shdr &Section, uint32_t Entry) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100150
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200151 Expected<StringRef>
152 getStringTable(const Elf_Shdr &Section,
153 WarningHandler WarnHandler = &defaultWarningHandler) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100154 Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
155 Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section,
156 Elf_Shdr_Range Sections) const;
157
158 Expected<ArrayRef<Elf_Word>> getSHNDXTable(const Elf_Shdr &Section) const;
159 Expected<ArrayRef<Elf_Word>> getSHNDXTable(const Elf_Shdr &Section,
160 Elf_Shdr_Range Sections) const;
161
162 StringRef getRelocationTypeName(uint32_t Type) const;
163 void getRelocationTypeName(uint32_t Type,
164 SmallVectorImpl<char> &Result) const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100165 uint32_t getRelativeRelocationType() const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100166
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100167 std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const;
168 std::string getDynamicTagAsString(uint64_t Type) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100169
170 /// Get the symbol for a given relocation.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200171 Expected<const Elf_Sym *> getRelocationSymbol(const Elf_Rel &Rel,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100172 const Elf_Shdr *SymTab) const;
173
174 static Expected<ELFFile> create(StringRef Object);
175
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200176 bool isLE() const {
177 return getHeader().getDataEncoding() == ELF::ELFDATA2LSB;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100178 }
179
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200180 bool isMipsELF64() const {
181 return getHeader().e_machine == ELF::EM_MIPS &&
182 getHeader().getFileClass() == ELF::ELFCLASS64;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100183 }
184
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200185 bool isMips64EL() const { return isMipsELF64() && isLE(); }
186
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100187 Expected<Elf_Shdr_Range> sections() const;
188
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100189 Expected<Elf_Dyn_Range> dynamicEntries() const;
190
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200191 Expected<const uint8_t *>
192 toMappedAddr(uint64_t VAddr,
193 WarningHandler WarnHandler = &defaultWarningHandler) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100194
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100195 Expected<Elf_Sym_Range> symbols(const Elf_Shdr *Sec) const {
196 if (!Sec)
197 return makeArrayRef<Elf_Sym>(nullptr, nullptr);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200198 return getSectionContentsAsArray<Elf_Sym>(*Sec);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100199 }
200
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200201 Expected<Elf_Rela_Range> relas(const Elf_Shdr &Sec) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100202 return getSectionContentsAsArray<Elf_Rela>(Sec);
203 }
204
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200205 Expected<Elf_Rel_Range> rels(const Elf_Shdr &Sec) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100206 return getSectionContentsAsArray<Elf_Rel>(Sec);
207 }
208
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200209 Expected<Elf_Relr_Range> relrs(const Elf_Shdr &Sec) const {
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100210 return getSectionContentsAsArray<Elf_Relr>(Sec);
211 }
212
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200213 std::vector<Elf_Rel> decode_relrs(Elf_Relr_Range relrs) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100214
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200215 Expected<std::vector<Elf_Rela>> android_relas(const Elf_Shdr &Sec) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100216
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100217 /// Iterate over program header table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100218 Expected<Elf_Phdr_Range> program_headers() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200219 if (getHeader().e_phnum && getHeader().e_phentsize != sizeof(Elf_Phdr))
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100220 return createError("invalid e_phentsize: " +
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200221 Twine(getHeader().e_phentsize));
222
223 uint64_t HeadersSize =
224 (uint64_t)getHeader().e_phnum * getHeader().e_phentsize;
225 uint64_t PhOff = getHeader().e_phoff;
226 if (PhOff + HeadersSize < PhOff || PhOff + HeadersSize > getBufSize())
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100227 return createError("program headers are longer than binary of size " +
228 Twine(getBufSize()) + ": e_phoff = 0x" +
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200229 Twine::utohexstr(getHeader().e_phoff) +
230 ", e_phnum = " + Twine(getHeader().e_phnum) +
231 ", e_phentsize = " + Twine(getHeader().e_phentsize));
232
233 auto *Begin = reinterpret_cast<const Elf_Phdr *>(base() + PhOff);
234 return makeArrayRef(Begin, Begin + getHeader().e_phnum);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100235 }
236
237 /// Get an iterator over notes in a program header.
238 ///
239 /// The program header must be of type \c PT_NOTE.
240 ///
241 /// \param Phdr the program header to iterate over.
242 /// \param Err [out] an error to support fallible iteration, which should
243 /// be checked after iteration ends.
244 Elf_Note_Iterator notes_begin(const Elf_Phdr &Phdr, Error &Err) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200245 assert(Phdr.p_type == ELF::PT_NOTE && "Phdr is not of type PT_NOTE");
246 ErrorAsOutParameter ErrAsOutParam(&Err);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100247 if (Phdr.p_offset + Phdr.p_filesz > getBufSize()) {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200248 Err =
249 createError("invalid offset (0x" + Twine::utohexstr(Phdr.p_offset) +
250 ") or size (0x" + Twine::utohexstr(Phdr.p_filesz) + ")");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100251 return Elf_Note_Iterator(Err);
252 }
253 return Elf_Note_Iterator(base() + Phdr.p_offset, Phdr.p_filesz, Err);
254 }
255
256 /// Get an iterator over notes in a section.
257 ///
258 /// The section must be of type \c SHT_NOTE.
259 ///
260 /// \param Shdr the section to iterate over.
261 /// \param Err [out] an error to support fallible iteration, which should
262 /// be checked after iteration ends.
263 Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200264 assert(Shdr.sh_type == ELF::SHT_NOTE && "Shdr is not of type SHT_NOTE");
265 ErrorAsOutParameter ErrAsOutParam(&Err);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100266 if (Shdr.sh_offset + Shdr.sh_size > getBufSize()) {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200267 Err =
268 createError("invalid offset (0x" + Twine::utohexstr(Shdr.sh_offset) +
269 ") or size (0x" + Twine::utohexstr(Shdr.sh_size) + ")");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100270 return Elf_Note_Iterator(Err);
271 }
272 return Elf_Note_Iterator(base() + Shdr.sh_offset, Shdr.sh_size, Err);
273 }
274
275 /// Get the end iterator for notes.
276 Elf_Note_Iterator notes_end() const {
277 return Elf_Note_Iterator();
278 }
279
280 /// Get an iterator range over notes of a program header.
281 ///
282 /// The program header must be of type \c PT_NOTE.
283 ///
284 /// \param Phdr the program header to iterate over.
285 /// \param Err [out] an error to support fallible iteration, which should
286 /// be checked after iteration ends.
287 iterator_range<Elf_Note_Iterator> notes(const Elf_Phdr &Phdr,
288 Error &Err) const {
289 return make_range(notes_begin(Phdr, Err), notes_end());
290 }
291
292 /// Get an iterator range over notes of a section.
293 ///
294 /// The section must be of type \c SHT_NOTE.
295 ///
296 /// \param Shdr the section to iterate over.
297 /// \param Err [out] an error to support fallible iteration, which should
298 /// be checked after iteration ends.
299 iterator_range<Elf_Note_Iterator> notes(const Elf_Shdr &Shdr,
300 Error &Err) const {
301 return make_range(notes_begin(Shdr, Err), notes_end());
302 }
303
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200304 Expected<StringRef> getSectionStringTable(
305 Elf_Shdr_Range Sections,
306 WarningHandler WarnHandler = &defaultWarningHandler) const;
307 Expected<uint32_t> getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
308 DataRegion<Elf_Word> ShndxTable) const;
309 Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100310 const Elf_Shdr *SymTab,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200311 DataRegion<Elf_Word> ShndxTable) const;
312 Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100313 Elf_Sym_Range Symtab,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200314 DataRegion<Elf_Word> ShndxTable) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100315 Expected<const Elf_Shdr *> getSection(uint32_t Index) const;
316
317 Expected<const Elf_Sym *> getSymbol(const Elf_Shdr *Sec,
318 uint32_t Index) const;
319
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200320 Expected<StringRef>
321 getSectionName(const Elf_Shdr &Section,
322 WarningHandler WarnHandler = &defaultWarningHandler) const;
323 Expected<StringRef> getSectionName(const Elf_Shdr &Section,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100324 StringRef DotShstrtab) const;
325 template <typename T>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200326 Expected<ArrayRef<T>> getSectionContentsAsArray(const Elf_Shdr &Sec) const;
327 Expected<ArrayRef<uint8_t>> getSectionContents(const Elf_Shdr &Sec) const;
328 Expected<ArrayRef<uint8_t>> getSegmentContents(const Elf_Phdr &Phdr) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100329};
330
331using ELF32LEFile = ELFFile<ELF32LE>;
332using ELF64LEFile = ELFFile<ELF64LE>;
333using ELF32BEFile = ELFFile<ELF32BE>;
334using ELF64BEFile = ELFFile<ELF64BE>;
335
336template <class ELFT>
337inline Expected<const typename ELFT::Shdr *>
338getSection(typename ELFT::ShdrRange Sections, uint32_t Index) {
339 if (Index >= Sections.size())
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100340 return createError("invalid section index: " + Twine(Index));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100341 return &Sections[Index];
342}
343
344template <class ELFT>
345inline Expected<uint32_t>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200346getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym, unsigned SymIndex,
347 DataRegion<typename ELFT::Word> ShndxTable) {
348 assert(Sym.st_shndx == ELF::SHN_XINDEX);
349 if (!ShndxTable.First)
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100350 return createError(
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200351 "found an extended symbol index (" + Twine(SymIndex) +
352 "), but unable to locate the extended symbol index table");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100353
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200354 Expected<typename ELFT::Word> TableOrErr = ShndxTable[SymIndex];
355 if (!TableOrErr)
356 return createError("unable to read an extended symbol table at index " +
357 Twine(SymIndex) + ": " +
358 toString(TableOrErr.takeError()));
359 return *TableOrErr;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100360}
361
362template <class ELFT>
363Expected<uint32_t>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200364ELFFile<ELFT>::getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
365 DataRegion<Elf_Word> ShndxTable) const {
366 uint32_t Index = Sym.st_shndx;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100367 if (Index == ELF::SHN_XINDEX) {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200368 Expected<uint32_t> ErrorOrIndex =
369 getExtendedSymbolTableIndex<ELFT>(Sym, &Sym - Syms.begin(), ShndxTable);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100370 if (!ErrorOrIndex)
371 return ErrorOrIndex.takeError();
372 return *ErrorOrIndex;
373 }
374 if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
375 return 0;
376 return Index;
377}
378
379template <class ELFT>
380Expected<const typename ELFT::Shdr *>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200381ELFFile<ELFT>::getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab,
382 DataRegion<Elf_Word> ShndxTable) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100383 auto SymsOrErr = symbols(SymTab);
384 if (!SymsOrErr)
385 return SymsOrErr.takeError();
386 return getSection(Sym, *SymsOrErr, ShndxTable);
387}
388
389template <class ELFT>
390Expected<const typename ELFT::Shdr *>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200391ELFFile<ELFT>::getSection(const Elf_Sym &Sym, Elf_Sym_Range Symbols,
392 DataRegion<Elf_Word> ShndxTable) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100393 auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable);
394 if (!IndexOrErr)
395 return IndexOrErr.takeError();
396 uint32_t Index = *IndexOrErr;
397 if (Index == 0)
398 return nullptr;
399 return getSection(Index);
400}
401
402template <class ELFT>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100403Expected<const typename ELFT::Sym *>
404ELFFile<ELFT>::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200405 auto SymsOrErr = symbols(Sec);
406 if (!SymsOrErr)
407 return SymsOrErr.takeError();
408
409 Elf_Sym_Range Symbols = *SymsOrErr;
410 if (Index >= Symbols.size())
411 return createError("unable to get symbol from section " +
412 getSecIndexForError(*this, *Sec) +
413 ": invalid symbol index (" + Twine(Index) + ")");
414 return &Symbols[Index];
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100415}
416
417template <class ELFT>
418template <typename T>
419Expected<ArrayRef<T>>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200420ELFFile<ELFT>::getSectionContentsAsArray(const Elf_Shdr &Sec) const {
421 if (Sec.sh_entsize != sizeof(T) && sizeof(T) != 1)
422 return createError("section " + getSecIndexForError(*this, Sec) +
423 " has invalid sh_entsize: expected " + Twine(sizeof(T)) +
424 ", but got " + Twine(Sec.sh_entsize));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100425
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200426 uintX_t Offset = Sec.sh_offset;
427 uintX_t Size = Sec.sh_size;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100428
429 if (Size % sizeof(T))
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200430 return createError("section " + getSecIndexForError(*this, Sec) +
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100431 " has an invalid sh_size (" + Twine(Size) +
432 ") which is not a multiple of its sh_entsize (" +
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200433 Twine(Sec.sh_entsize) + ")");
434 if (std::numeric_limits<uintX_t>::max() - Offset < Size)
435 return createError("section " + getSecIndexForError(*this, Sec) +
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100436 " has a sh_offset (0x" + Twine::utohexstr(Offset) +
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200437 ") + sh_size (0x" + Twine::utohexstr(Size) +
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100438 ") that cannot be represented");
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200439 if (Offset + Size > Buf.size())
440 return createError("section " + getSecIndexForError(*this, Sec) +
441 " has a sh_offset (0x" + Twine::utohexstr(Offset) +
442 ") + sh_size (0x" + Twine::utohexstr(Size) +
443 ") that is greater than the file size (0x" +
444 Twine::utohexstr(Buf.size()) + ")");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100445
446 if (Offset % alignof(T))
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100447 // TODO: this error is untested.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100448 return createError("unaligned data");
449
450 const T *Start = reinterpret_cast<const T *>(base() + Offset);
451 return makeArrayRef(Start, Size / sizeof(T));
452}
453
454template <class ELFT>
455Expected<ArrayRef<uint8_t>>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200456ELFFile<ELFT>::getSegmentContents(const Elf_Phdr &Phdr) const {
457 uintX_t Offset = Phdr.p_offset;
458 uintX_t Size = Phdr.p_filesz;
459
460 if (std::numeric_limits<uintX_t>::max() - Offset < Size)
461 return createError("program header " + getPhdrIndexForError(*this, Phdr) +
462 " has a p_offset (0x" + Twine::utohexstr(Offset) +
463 ") + p_filesz (0x" + Twine::utohexstr(Size) +
464 ") that cannot be represented");
465 if (Offset + Size > Buf.size())
466 return createError("program header " + getPhdrIndexForError(*this, Phdr) +
467 " has a p_offset (0x" + Twine::utohexstr(Offset) +
468 ") + p_filesz (0x" + Twine::utohexstr(Size) +
469 ") that is greater than the file size (0x" +
470 Twine::utohexstr(Buf.size()) + ")");
471 return makeArrayRef(base() + Offset, Size);
472}
473
474template <class ELFT>
475Expected<ArrayRef<uint8_t>>
476ELFFile<ELFT>::getSectionContents(const Elf_Shdr &Sec) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100477 return getSectionContentsAsArray<uint8_t>(Sec);
478}
479
480template <class ELFT>
481StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200482 return getELFRelocationTypeName(getHeader().e_machine, Type);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100483}
484
485template <class ELFT>
486void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
487 SmallVectorImpl<char> &Result) const {
488 if (!isMipsELF64()) {
489 StringRef Name = getRelocationTypeName(Type);
490 Result.append(Name.begin(), Name.end());
491 } else {
492 // The Mips N64 ABI allows up to three operations to be specified per
493 // relocation record. Unfortunately there's no easy way to test for the
494 // presence of N64 ELFs as they have no special flag that identifies them
495 // as being N64. We can safely assume at the moment that all Mips
496 // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
497 // information to disambiguate between old vs new ABIs.
498 uint8_t Type1 = (Type >> 0) & 0xFF;
499 uint8_t Type2 = (Type >> 8) & 0xFF;
500 uint8_t Type3 = (Type >> 16) & 0xFF;
501
502 // Concat all three relocation type names.
503 StringRef Name = getRelocationTypeName(Type1);
504 Result.append(Name.begin(), Name.end());
505
506 Name = getRelocationTypeName(Type2);
507 Result.append(1, '/');
508 Result.append(Name.begin(), Name.end());
509
510 Name = getRelocationTypeName(Type3);
511 Result.append(1, '/');
512 Result.append(Name.begin(), Name.end());
513 }
514}
515
516template <class ELFT>
Andrew Walbran16937d02019-10-22 13:54:20 +0100517uint32_t ELFFile<ELFT>::getRelativeRelocationType() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200518 return getELFRelativeRelocationType(getHeader().e_machine);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100519}
520
521template <class ELFT>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100522Expected<const typename ELFT::Sym *>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200523ELFFile<ELFT>::getRelocationSymbol(const Elf_Rel &Rel,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100524 const Elf_Shdr *SymTab) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200525 uint32_t Index = Rel.getSymbol(isMips64EL());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100526 if (Index == 0)
527 return nullptr;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200528 return getEntry<Elf_Sym>(*SymTab, Index);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100529}
530
531template <class ELFT>
532Expected<StringRef>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200533ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections,
534 WarningHandler WarnHandler) const {
535 uint32_t Index = getHeader().e_shstrndx;
536 if (Index == ELF::SHN_XINDEX) {
537 // If the section name string table section index is greater than
538 // or equal to SHN_LORESERVE, then the actual index of the section name
539 // string table section is contained in the sh_link field of the section
540 // header at index 0.
541 if (Sections.empty())
542 return createError(
543 "e_shstrndx == SHN_XINDEX, but the section header table is empty");
544
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100545 Index = Sections[0].sh_link;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200546 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100547
548 if (!Index) // no section string table.
549 return "";
550 if (Index >= Sections.size())
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200551 return createError("section header string table index " + Twine(Index) +
552 " does not exist");
553 return getStringTable(Sections[Index], WarnHandler);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100554}
555
556template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
557
558template <class ELFT>
559Expected<ELFFile<ELFT>> ELFFile<ELFT>::create(StringRef Object) {
560 if (sizeof(Elf_Ehdr) > Object.size())
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100561 return createError("invalid buffer: the size (" + Twine(Object.size()) +
562 ") is smaller than an ELF header (" +
563 Twine(sizeof(Elf_Ehdr)) + ")");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100564 return ELFFile(Object);
565}
566
567template <class ELFT>
568Expected<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200569 const uintX_t SectionTableOffset = getHeader().e_shoff;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100570 if (SectionTableOffset == 0)
571 return ArrayRef<Elf_Shdr>();
572
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200573 if (getHeader().e_shentsize != sizeof(Elf_Shdr))
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100574 return createError("invalid e_shentsize in ELF header: " +
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200575 Twine(getHeader().e_shentsize));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100576
577 const uint64_t FileSize = Buf.size();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200578 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize ||
579 SectionTableOffset + (uintX_t)sizeof(Elf_Shdr) < SectionTableOffset)
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100580 return createError(
581 "section header table goes past the end of the file: e_shoff = 0x" +
582 Twine::utohexstr(SectionTableOffset));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100583
584 // Invalid address alignment of section headers
585 if (SectionTableOffset & (alignof(Elf_Shdr) - 1))
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100586 // TODO: this error is untested.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100587 return createError("invalid alignment of section headers");
588
589 const Elf_Shdr *First =
590 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
591
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200592 uintX_t NumSections = getHeader().e_shnum;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100593 if (NumSections == 0)
594 NumSections = First->sh_size;
595
596 if (NumSections > UINT64_MAX / sizeof(Elf_Shdr))
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200597 return createError("invalid number of sections specified in the NULL "
598 "section's sh_size field (" +
599 Twine(NumSections) + ")");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100600
601 const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200602 if (SectionTableOffset + SectionTableSize < SectionTableOffset)
603 return createError(
604 "invalid section header table offset (e_shoff = 0x" +
605 Twine::utohexstr(SectionTableOffset) +
606 ") or invalid number of sections specified in the first section "
607 "header's sh_size field (0x" +
608 Twine::utohexstr(NumSections) + ")");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100609
610 // Section table goes past end of file!
611 if (SectionTableOffset + SectionTableSize > FileSize)
612 return createError("section table goes past the end of file");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100613 return makeArrayRef(First, NumSections);
614}
615
616template <class ELFT>
617template <typename T>
618Expected<const T *> ELFFile<ELFT>::getEntry(uint32_t Section,
619 uint32_t Entry) const {
620 auto SecOrErr = getSection(Section);
621 if (!SecOrErr)
622 return SecOrErr.takeError();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200623 return getEntry<T>(**SecOrErr, Entry);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100624}
625
626template <class ELFT>
627template <typename T>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200628Expected<const T *> ELFFile<ELFT>::getEntry(const Elf_Shdr &Section,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100629 uint32_t Entry) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200630 Expected<ArrayRef<T>> EntriesOrErr = getSectionContentsAsArray<T>(Section);
631 if (!EntriesOrErr)
632 return EntriesOrErr.takeError();
633
634 ArrayRef<T> Arr = *EntriesOrErr;
635 if (Entry >= Arr.size())
636 return createError(
637 "can't read an entry at 0x" +
638 Twine::utohexstr(Entry * static_cast<uint64_t>(sizeof(T))) +
639 ": it goes past the end of the section (0x" +
640 Twine::utohexstr(Section.sh_size) + ")");
641 return &Arr[Entry];
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100642}
643
644template <class ELFT>
645Expected<const typename ELFT::Shdr *>
646ELFFile<ELFT>::getSection(uint32_t Index) const {
647 auto TableOrErr = sections();
648 if (!TableOrErr)
649 return TableOrErr.takeError();
650 return object::getSection<ELFT>(*TableOrErr, Index);
651}
652
653template <class ELFT>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100654Expected<StringRef>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200655ELFFile<ELFT>::getStringTable(const Elf_Shdr &Section,
656 WarningHandler WarnHandler) const {
657 if (Section.sh_type != ELF::SHT_STRTAB)
658 if (Error E = WarnHandler("invalid sh_type for string table section " +
659 getSecIndexForError(*this, Section) +
660 ": expected SHT_STRTAB, but got " +
661 object::getELFSectionTypeName(
662 getHeader().e_machine, Section.sh_type)))
663 return std::move(E);
664
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100665 auto V = getSectionContentsAsArray<char>(Section);
666 if (!V)
667 return V.takeError();
668 ArrayRef<char> Data = *V;
669 if (Data.empty())
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200670 return createError("SHT_STRTAB string table section " +
671 getSecIndexForError(*this, Section) + " is empty");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100672 if (Data.back() != '\0')
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200673 return createError("SHT_STRTAB string table section " +
674 getSecIndexForError(*this, Section) +
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100675 " is non-null terminated");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100676 return StringRef(Data.begin(), Data.size());
677}
678
679template <class ELFT>
680Expected<ArrayRef<typename ELFT::Word>>
681ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section) const {
682 auto SectionsOrErr = sections();
683 if (!SectionsOrErr)
684 return SectionsOrErr.takeError();
685 return getSHNDXTable(Section, *SectionsOrErr);
686}
687
688template <class ELFT>
689Expected<ArrayRef<typename ELFT::Word>>
690ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section,
691 Elf_Shdr_Range Sections) const {
692 assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200693 auto VOrErr = getSectionContentsAsArray<Elf_Word>(Section);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100694 if (!VOrErr)
695 return VOrErr.takeError();
696 ArrayRef<Elf_Word> V = *VOrErr;
697 auto SymTableOrErr = object::getSection<ELFT>(Sections, Section.sh_link);
698 if (!SymTableOrErr)
699 return SymTableOrErr.takeError();
700 const Elf_Shdr &SymTable = **SymTableOrErr;
701 if (SymTable.sh_type != ELF::SHT_SYMTAB &&
702 SymTable.sh_type != ELF::SHT_DYNSYM)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200703 return createError(
704 "SHT_SYMTAB_SHNDX section is linked with " +
705 object::getELFSectionTypeName(getHeader().e_machine, SymTable.sh_type) +
706 " section (expected SHT_SYMTAB/SHT_DYNSYM)");
707
708 uint64_t Syms = SymTable.sh_size / sizeof(Elf_Sym);
709 if (V.size() != Syms)
710 return createError("SHT_SYMTAB_SHNDX has " + Twine(V.size()) +
711 " entries, but the symbol table associated has " +
712 Twine(Syms));
713
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100714 return V;
715}
716
717template <class ELFT>
718Expected<StringRef>
719ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
720 auto SectionsOrErr = sections();
721 if (!SectionsOrErr)
722 return SectionsOrErr.takeError();
723 return getStringTableForSymtab(Sec, *SectionsOrErr);
724}
725
726template <class ELFT>
727Expected<StringRef>
728ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec,
729 Elf_Shdr_Range Sections) const {
730
731 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
732 return createError(
733 "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200734 Expected<const Elf_Shdr *> SectionOrErr =
735 object::getSection<ELFT>(Sections, Sec.sh_link);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100736 if (!SectionOrErr)
737 return SectionOrErr.takeError();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200738 return getStringTable(**SectionOrErr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100739}
740
741template <class ELFT>
742Expected<StringRef>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200743ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section,
744 WarningHandler WarnHandler) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100745 auto SectionsOrErr = sections();
746 if (!SectionsOrErr)
747 return SectionsOrErr.takeError();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200748 auto Table = getSectionStringTable(*SectionsOrErr, WarnHandler);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100749 if (!Table)
750 return Table.takeError();
751 return getSectionName(Section, *Table);
752}
753
754template <class ELFT>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200755Expected<StringRef> ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100756 StringRef DotShstrtab) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200757 uint32_t Offset = Section.sh_name;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100758 if (Offset == 0)
759 return StringRef();
760 if (Offset >= DotShstrtab.size())
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200761 return createError("a section " + getSecIndexForError(*this, Section) +
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100762 " has an invalid sh_name (0x" +
763 Twine::utohexstr(Offset) +
764 ") offset which goes past the end of the "
765 "section name string table");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100766 return StringRef(DotShstrtab.data() + Offset);
767}
768
769/// This function returns the hash value for a symbol in the .dynsym section
770/// Name of the API remains consistent as specified in the libelf
771/// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
772inline unsigned hashSysV(StringRef SymbolName) {
773 unsigned h = 0, g;
774 for (char C : SymbolName) {
775 h = (h << 4) + C;
776 g = h & 0xf0000000L;
777 if (g != 0)
778 h ^= g >> 24;
779 h &= ~g;
780 }
781 return h;
782}
783
784} // end namespace object
785} // end namespace llvm
786
787#endif // LLVM_OBJECT_ELF_H