Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- ELF.h - ELF object file implementation -------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 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 | |
| 30 | namespace llvm { |
| 31 | namespace object { |
| 32 | |
| 33 | StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 34 | uint32_t getELFRelativeRelocationType(uint32_t Machine); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 35 | StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type); |
| 36 | |
| 37 | // Subclasses of ELFFile may need this for template instantiation |
| 38 | inline std::pair<unsigned char, unsigned char> |
| 39 | getElfArchType(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 Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 47 | static inline Error createError(const Twine &Err) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 48 | return make_error<StringError>(Err, object_error::parse_failed); |
| 49 | } |
| 50 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 51 | enum 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 Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 58 | template <class ELFT> class ELFFile; |
| 59 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 60 | template <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 Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 90 | template <class ELFT> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 91 | std::string getSecIndexForError(const ELFFile<ELFT> &Obj, |
| 92 | const typename ELFT::Shdr &Sec) { |
| 93 | auto TableOrErr = Obj.sections(); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 94 | if (TableOrErr) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 95 | return "[index " + std::to_string(&Sec - &TableOrErr->front()) + "]"; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 96 | // 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 104 | template <class ELFT> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 105 | std::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 | |
| 115 | static inline Error defaultWarningHandler(const Twine &Msg) { |
| 116 | return createError(Msg); |
| 117 | } |
| 118 | |
| 119 | template <class ELFT> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 120 | class ELFFile { |
| 121 | public: |
| 122 | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 123 | |
| 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 130 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 131 | const uint8_t *base() const { return Buf.bytes_begin(); } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 132 | const uint8_t *end() const { return base() + getBufSize(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 133 | |
| 134 | size_t getBufSize() const { return Buf.size(); } |
| 135 | |
| 136 | private: |
| 137 | StringRef Buf; |
| 138 | |
| 139 | ELFFile(StringRef Object); |
| 140 | |
| 141 | public: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 142 | const Elf_Ehdr &getHeader() const { |
| 143 | return *reinterpret_cast<const Elf_Ehdr *>(base()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | template <typename T> |
| 147 | Expected<const T *> getEntry(uint32_t Section, uint32_t Entry) const; |
| 148 | template <typename T> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 149 | Expected<const T *> getEntry(const Elf_Shdr &Section, uint32_t Entry) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 150 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 151 | Expected<StringRef> |
| 152 | getStringTable(const Elf_Shdr &Section, |
| 153 | WarningHandler WarnHandler = &defaultWarningHandler) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 154 | 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 Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 165 | uint32_t getRelativeRelocationType() const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 166 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 167 | std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const; |
| 168 | std::string getDynamicTagAsString(uint64_t Type) const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 169 | |
| 170 | /// Get the symbol for a given relocation. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 171 | Expected<const Elf_Sym *> getRelocationSymbol(const Elf_Rel &Rel, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 172 | const Elf_Shdr *SymTab) const; |
| 173 | |
| 174 | static Expected<ELFFile> create(StringRef Object); |
| 175 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 176 | bool isLE() const { |
| 177 | return getHeader().getDataEncoding() == ELF::ELFDATA2LSB; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 180 | bool isMipsELF64() const { |
| 181 | return getHeader().e_machine == ELF::EM_MIPS && |
| 182 | getHeader().getFileClass() == ELF::ELFCLASS64; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 185 | bool isMips64EL() const { return isMipsELF64() && isLE(); } |
| 186 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 187 | Expected<Elf_Shdr_Range> sections() const; |
| 188 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 189 | Expected<Elf_Dyn_Range> dynamicEntries() const; |
| 190 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 191 | Expected<const uint8_t *> |
| 192 | toMappedAddr(uint64_t VAddr, |
| 193 | WarningHandler WarnHandler = &defaultWarningHandler) const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 194 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 195 | Expected<Elf_Sym_Range> symbols(const Elf_Shdr *Sec) const { |
| 196 | if (!Sec) |
| 197 | return makeArrayRef<Elf_Sym>(nullptr, nullptr); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 198 | return getSectionContentsAsArray<Elf_Sym>(*Sec); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 201 | Expected<Elf_Rela_Range> relas(const Elf_Shdr &Sec) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 202 | return getSectionContentsAsArray<Elf_Rela>(Sec); |
| 203 | } |
| 204 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 205 | Expected<Elf_Rel_Range> rels(const Elf_Shdr &Sec) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 206 | return getSectionContentsAsArray<Elf_Rel>(Sec); |
| 207 | } |
| 208 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 209 | Expected<Elf_Relr_Range> relrs(const Elf_Shdr &Sec) const { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 210 | return getSectionContentsAsArray<Elf_Relr>(Sec); |
| 211 | } |
| 212 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 213 | std::vector<Elf_Rel> decode_relrs(Elf_Relr_Range relrs) const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 214 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 215 | Expected<std::vector<Elf_Rela>> android_relas(const Elf_Shdr &Sec) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 216 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 217 | /// Iterate over program header table. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 218 | Expected<Elf_Phdr_Range> program_headers() const { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 219 | if (getHeader().e_phnum && getHeader().e_phentsize != sizeof(Elf_Phdr)) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 220 | return createError("invalid e_phentsize: " + |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 221 | 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 Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 227 | return createError("program headers are longer than binary of size " + |
| 228 | Twine(getBufSize()) + ": e_phoff = 0x" + |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 229 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 235 | } |
| 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 Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 245 | assert(Phdr.p_type == ELF::PT_NOTE && "Phdr is not of type PT_NOTE"); |
| 246 | ErrorAsOutParameter ErrAsOutParam(&Err); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 247 | if (Phdr.p_offset + Phdr.p_filesz > getBufSize()) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 248 | Err = |
| 249 | createError("invalid offset (0x" + Twine::utohexstr(Phdr.p_offset) + |
| 250 | ") or size (0x" + Twine::utohexstr(Phdr.p_filesz) + ")"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 251 | 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 Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 264 | assert(Shdr.sh_type == ELF::SHT_NOTE && "Shdr is not of type SHT_NOTE"); |
| 265 | ErrorAsOutParameter ErrAsOutParam(&Err); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 266 | if (Shdr.sh_offset + Shdr.sh_size > getBufSize()) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 267 | Err = |
| 268 | createError("invalid offset (0x" + Twine::utohexstr(Shdr.sh_offset) + |
| 269 | ") or size (0x" + Twine::utohexstr(Shdr.sh_size) + ")"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 270 | 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 Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 304 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 310 | const Elf_Shdr *SymTab, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 311 | DataRegion<Elf_Word> ShndxTable) const; |
| 312 | Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 313 | Elf_Sym_Range Symtab, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 314 | DataRegion<Elf_Word> ShndxTable) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 315 | 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 Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 320 | Expected<StringRef> |
| 321 | getSectionName(const Elf_Shdr &Section, |
| 322 | WarningHandler WarnHandler = &defaultWarningHandler) const; |
| 323 | Expected<StringRef> getSectionName(const Elf_Shdr &Section, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 324 | StringRef DotShstrtab) const; |
| 325 | template <typename T> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 326 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | using ELF32LEFile = ELFFile<ELF32LE>; |
| 332 | using ELF64LEFile = ELFFile<ELF64LE>; |
| 333 | using ELF32BEFile = ELFFile<ELF32BE>; |
| 334 | using ELF64BEFile = ELFFile<ELF64BE>; |
| 335 | |
| 336 | template <class ELFT> |
| 337 | inline Expected<const typename ELFT::Shdr *> |
| 338 | getSection(typename ELFT::ShdrRange Sections, uint32_t Index) { |
| 339 | if (Index >= Sections.size()) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 340 | return createError("invalid section index: " + Twine(Index)); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 341 | return &Sections[Index]; |
| 342 | } |
| 343 | |
| 344 | template <class ELFT> |
| 345 | inline Expected<uint32_t> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 346 | getExtendedSymbolTableIndex(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 Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 350 | return createError( |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 351 | "found an extended symbol index (" + Twine(SymIndex) + |
| 352 | "), but unable to locate the extended symbol index table"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 353 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 354 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | template <class ELFT> |
| 363 | Expected<uint32_t> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 364 | ELFFile<ELFT>::getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms, |
| 365 | DataRegion<Elf_Word> ShndxTable) const { |
| 366 | uint32_t Index = Sym.st_shndx; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 367 | if (Index == ELF::SHN_XINDEX) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 368 | Expected<uint32_t> ErrorOrIndex = |
| 369 | getExtendedSymbolTableIndex<ELFT>(Sym, &Sym - Syms.begin(), ShndxTable); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 370 | 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 | |
| 379 | template <class ELFT> |
| 380 | Expected<const typename ELFT::Shdr *> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 381 | ELFFile<ELFT>::getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab, |
| 382 | DataRegion<Elf_Word> ShndxTable) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 383 | auto SymsOrErr = symbols(SymTab); |
| 384 | if (!SymsOrErr) |
| 385 | return SymsOrErr.takeError(); |
| 386 | return getSection(Sym, *SymsOrErr, ShndxTable); |
| 387 | } |
| 388 | |
| 389 | template <class ELFT> |
| 390 | Expected<const typename ELFT::Shdr *> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 391 | ELFFile<ELFT>::getSection(const Elf_Sym &Sym, Elf_Sym_Range Symbols, |
| 392 | DataRegion<Elf_Word> ShndxTable) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 393 | 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 | |
| 402 | template <class ELFT> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 403 | Expected<const typename ELFT::Sym *> |
| 404 | ELFFile<ELFT>::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 405 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | template <class ELFT> |
| 418 | template <typename T> |
| 419 | Expected<ArrayRef<T>> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 420 | ELFFile<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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 425 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 426 | uintX_t Offset = Sec.sh_offset; |
| 427 | uintX_t Size = Sec.sh_size; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 428 | |
| 429 | if (Size % sizeof(T)) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 430 | return createError("section " + getSecIndexForError(*this, Sec) + |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 431 | " has an invalid sh_size (" + Twine(Size) + |
| 432 | ") which is not a multiple of its sh_entsize (" + |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 433 | Twine(Sec.sh_entsize) + ")"); |
| 434 | if (std::numeric_limits<uintX_t>::max() - Offset < Size) |
| 435 | return createError("section " + getSecIndexForError(*this, Sec) + |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 436 | " has a sh_offset (0x" + Twine::utohexstr(Offset) + |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 437 | ") + sh_size (0x" + Twine::utohexstr(Size) + |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 438 | ") that cannot be represented"); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 439 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 445 | |
| 446 | if (Offset % alignof(T)) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 447 | // TODO: this error is untested. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 448 | return createError("unaligned data"); |
| 449 | |
| 450 | const T *Start = reinterpret_cast<const T *>(base() + Offset); |
| 451 | return makeArrayRef(Start, Size / sizeof(T)); |
| 452 | } |
| 453 | |
| 454 | template <class ELFT> |
| 455 | Expected<ArrayRef<uint8_t>> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 456 | ELFFile<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 | |
| 474 | template <class ELFT> |
| 475 | Expected<ArrayRef<uint8_t>> |
| 476 | ELFFile<ELFT>::getSectionContents(const Elf_Shdr &Sec) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 477 | return getSectionContentsAsArray<uint8_t>(Sec); |
| 478 | } |
| 479 | |
| 480 | template <class ELFT> |
| 481 | StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 482 | return getELFRelocationTypeName(getHeader().e_machine, Type); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | template <class ELFT> |
| 486 | void 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 | |
| 516 | template <class ELFT> |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 517 | uint32_t ELFFile<ELFT>::getRelativeRelocationType() const { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 518 | return getELFRelativeRelocationType(getHeader().e_machine); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | template <class ELFT> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 522 | Expected<const typename ELFT::Sym *> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 523 | ELFFile<ELFT>::getRelocationSymbol(const Elf_Rel &Rel, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 524 | const Elf_Shdr *SymTab) const { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 525 | uint32_t Index = Rel.getSymbol(isMips64EL()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 526 | if (Index == 0) |
| 527 | return nullptr; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 528 | return getEntry<Elf_Sym>(*SymTab, Index); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | template <class ELFT> |
| 532 | Expected<StringRef> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 533 | ELFFile<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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 545 | Index = Sections[0].sh_link; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 546 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 547 | |
| 548 | if (!Index) // no section string table. |
| 549 | return ""; |
| 550 | if (Index >= Sections.size()) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 551 | return createError("section header string table index " + Twine(Index) + |
| 552 | " does not exist"); |
| 553 | return getStringTable(Sections[Index], WarnHandler); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {} |
| 557 | |
| 558 | template <class ELFT> |
| 559 | Expected<ELFFile<ELFT>> ELFFile<ELFT>::create(StringRef Object) { |
| 560 | if (sizeof(Elf_Ehdr) > Object.size()) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 561 | return createError("invalid buffer: the size (" + Twine(Object.size()) + |
| 562 | ") is smaller than an ELF header (" + |
| 563 | Twine(sizeof(Elf_Ehdr)) + ")"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 564 | return ELFFile(Object); |
| 565 | } |
| 566 | |
| 567 | template <class ELFT> |
| 568 | Expected<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 569 | const uintX_t SectionTableOffset = getHeader().e_shoff; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 570 | if (SectionTableOffset == 0) |
| 571 | return ArrayRef<Elf_Shdr>(); |
| 572 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 573 | if (getHeader().e_shentsize != sizeof(Elf_Shdr)) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 574 | return createError("invalid e_shentsize in ELF header: " + |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 575 | Twine(getHeader().e_shentsize)); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 576 | |
| 577 | const uint64_t FileSize = Buf.size(); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 578 | if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize || |
| 579 | SectionTableOffset + (uintX_t)sizeof(Elf_Shdr) < SectionTableOffset) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 580 | return createError( |
| 581 | "section header table goes past the end of the file: e_shoff = 0x" + |
| 582 | Twine::utohexstr(SectionTableOffset)); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 583 | |
| 584 | // Invalid address alignment of section headers |
| 585 | if (SectionTableOffset & (alignof(Elf_Shdr) - 1)) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 586 | // TODO: this error is untested. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 587 | return createError("invalid alignment of section headers"); |
| 588 | |
| 589 | const Elf_Shdr *First = |
| 590 | reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset); |
| 591 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 592 | uintX_t NumSections = getHeader().e_shnum; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 593 | if (NumSections == 0) |
| 594 | NumSections = First->sh_size; |
| 595 | |
| 596 | if (NumSections > UINT64_MAX / sizeof(Elf_Shdr)) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 597 | return createError("invalid number of sections specified in the NULL " |
| 598 | "section's sh_size field (" + |
| 599 | Twine(NumSections) + ")"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 600 | |
| 601 | const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 602 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 609 | |
| 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 613 | return makeArrayRef(First, NumSections); |
| 614 | } |
| 615 | |
| 616 | template <class ELFT> |
| 617 | template <typename T> |
| 618 | Expected<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 Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 623 | return getEntry<T>(**SecOrErr, Entry); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | template <class ELFT> |
| 627 | template <typename T> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 628 | Expected<const T *> ELFFile<ELFT>::getEntry(const Elf_Shdr &Section, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 629 | uint32_t Entry) const { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 630 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | template <class ELFT> |
| 645 | Expected<const typename ELFT::Shdr *> |
| 646 | ELFFile<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 | |
| 653 | template <class ELFT> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 654 | Expected<StringRef> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 655 | ELFFile<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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 665 | auto V = getSectionContentsAsArray<char>(Section); |
| 666 | if (!V) |
| 667 | return V.takeError(); |
| 668 | ArrayRef<char> Data = *V; |
| 669 | if (Data.empty()) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 670 | return createError("SHT_STRTAB string table section " + |
| 671 | getSecIndexForError(*this, Section) + " is empty"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 672 | if (Data.back() != '\0') |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 673 | return createError("SHT_STRTAB string table section " + |
| 674 | getSecIndexForError(*this, Section) + |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 675 | " is non-null terminated"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 676 | return StringRef(Data.begin(), Data.size()); |
| 677 | } |
| 678 | |
| 679 | template <class ELFT> |
| 680 | Expected<ArrayRef<typename ELFT::Word>> |
| 681 | ELFFile<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 | |
| 688 | template <class ELFT> |
| 689 | Expected<ArrayRef<typename ELFT::Word>> |
| 690 | ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section, |
| 691 | Elf_Shdr_Range Sections) const { |
| 692 | assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 693 | auto VOrErr = getSectionContentsAsArray<Elf_Word>(Section); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 694 | 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 Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 703 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 714 | return V; |
| 715 | } |
| 716 | |
| 717 | template <class ELFT> |
| 718 | Expected<StringRef> |
| 719 | ELFFile<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 | |
| 726 | template <class ELFT> |
| 727 | Expected<StringRef> |
| 728 | ELFFile<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 Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 734 | Expected<const Elf_Shdr *> SectionOrErr = |
| 735 | object::getSection<ELFT>(Sections, Sec.sh_link); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 736 | if (!SectionOrErr) |
| 737 | return SectionOrErr.takeError(); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 738 | return getStringTable(**SectionOrErr); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | template <class ELFT> |
| 742 | Expected<StringRef> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 743 | ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section, |
| 744 | WarningHandler WarnHandler) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 745 | auto SectionsOrErr = sections(); |
| 746 | if (!SectionsOrErr) |
| 747 | return SectionsOrErr.takeError(); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 748 | auto Table = getSectionStringTable(*SectionsOrErr, WarnHandler); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 749 | if (!Table) |
| 750 | return Table.takeError(); |
| 751 | return getSectionName(Section, *Table); |
| 752 | } |
| 753 | |
| 754 | template <class ELFT> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 755 | Expected<StringRef> ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 756 | StringRef DotShstrtab) const { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 757 | uint32_t Offset = Section.sh_name; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 758 | if (Offset == 0) |
| 759 | return StringRef(); |
| 760 | if (Offset >= DotShstrtab.size()) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 761 | return createError("a section " + getSecIndexForError(*this, Section) + |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 762 | " 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 766 | 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 |
| 772 | inline 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 |