1 //===- ELF.h - ELF object file implementation -------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares the ELFFile template class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_OBJECT_ELF_H
14 #define LLVM_OBJECT_ELF_H
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"
33 StringRef
getELFRelocationTypeName(uint32_t Machine
, uint32_t Type
);
34 uint32_t getELFRelativeRelocationType(uint32_t Machine
);
35 StringRef
getELFSectionTypeName(uint32_t Machine
, uint32_t Type
);
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
]);
47 static inline Error
createError(const Twine
&Err
) {
48 return make_error
<StringError
>(Err
, object_error::parse_failed
);
51 template <class ELFT
> class ELFFile
;
54 std::string
getSecIndexForError(const ELFFile
<ELFT
> *Obj
,
55 const typename
ELFT::Shdr
*Sec
) {
56 auto TableOrErr
= Obj
->sections();
58 return "[index " + std::to_string(Sec
- &TableOrErr
->front()) + "]";
59 // To make this helper be more convenient for error reporting purposes we
60 // drop the error. But really it should never be triggered. Before this point,
61 // our code should have called 'sections()' and reported a proper error on
63 llvm::consumeError(TableOrErr
.takeError());
64 return "[unknown index]";
70 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT
)
71 using uintX_t
= typename
ELFT::uint
;
72 using Elf_Ehdr
= typename
ELFT::Ehdr
;
73 using Elf_Shdr
= typename
ELFT::Shdr
;
74 using Elf_Sym
= typename
ELFT::Sym
;
75 using Elf_Dyn
= typename
ELFT::Dyn
;
76 using Elf_Phdr
= typename
ELFT::Phdr
;
77 using Elf_Rel
= typename
ELFT::Rel
;
78 using Elf_Rela
= typename
ELFT::Rela
;
79 using Elf_Relr
= typename
ELFT::Relr
;
80 using Elf_Verdef
= typename
ELFT::Verdef
;
81 using Elf_Verdaux
= typename
ELFT::Verdaux
;
82 using Elf_Verneed
= typename
ELFT::Verneed
;
83 using Elf_Vernaux
= typename
ELFT::Vernaux
;
84 using Elf_Versym
= typename
ELFT::Versym
;
85 using Elf_Hash
= typename
ELFT::Hash
;
86 using Elf_GnuHash
= typename
ELFT::GnuHash
;
87 using Elf_Nhdr
= typename
ELFT::Nhdr
;
88 using Elf_Note
= typename
ELFT::Note
;
89 using Elf_Note_Iterator
= typename
ELFT::NoteIterator
;
90 using Elf_Dyn_Range
= typename
ELFT::DynRange
;
91 using Elf_Shdr_Range
= typename
ELFT::ShdrRange
;
92 using Elf_Sym_Range
= typename
ELFT::SymRange
;
93 using Elf_Rel_Range
= typename
ELFT::RelRange
;
94 using Elf_Rela_Range
= typename
ELFT::RelaRange
;
95 using Elf_Relr_Range
= typename
ELFT::RelrRange
;
96 using Elf_Phdr_Range
= typename
ELFT::PhdrRange
;
98 const uint8_t *base() const { return Buf
.bytes_begin(); }
100 size_t getBufSize() const { return Buf
.size(); }
105 ELFFile(StringRef Object
);
108 const Elf_Ehdr
*getHeader() const {
109 return reinterpret_cast<const Elf_Ehdr
*>(base());
112 template <typename T
>
113 Expected
<const T
*> getEntry(uint32_t Section
, uint32_t Entry
) const;
114 template <typename T
>
115 Expected
<const T
*> getEntry(const Elf_Shdr
*Section
, uint32_t Entry
) const;
117 Expected
<StringRef
> getStringTable(const Elf_Shdr
*Section
) const;
118 Expected
<StringRef
> getStringTableForSymtab(const Elf_Shdr
&Section
) const;
119 Expected
<StringRef
> getStringTableForSymtab(const Elf_Shdr
&Section
,
120 Elf_Shdr_Range Sections
) const;
122 Expected
<ArrayRef
<Elf_Word
>> getSHNDXTable(const Elf_Shdr
&Section
) const;
123 Expected
<ArrayRef
<Elf_Word
>> getSHNDXTable(const Elf_Shdr
&Section
,
124 Elf_Shdr_Range Sections
) const;
126 StringRef
getRelocationTypeName(uint32_t Type
) const;
127 void getRelocationTypeName(uint32_t Type
,
128 SmallVectorImpl
<char> &Result
) const;
129 uint32_t getRelativeRelocationType() const;
131 std::string
getDynamicTagAsString(unsigned Arch
, uint64_t Type
) const;
132 std::string
getDynamicTagAsString(uint64_t Type
) const;
134 /// Get the symbol for a given relocation.
135 Expected
<const Elf_Sym
*> getRelocationSymbol(const Elf_Rel
*Rel
,
136 const Elf_Shdr
*SymTab
) const;
138 static Expected
<ELFFile
> create(StringRef Object
);
140 bool isMipsELF64() const {
141 return getHeader()->e_machine
== ELF::EM_MIPS
&&
142 getHeader()->getFileClass() == ELF::ELFCLASS64
;
145 bool isMips64EL() const {
146 return isMipsELF64() &&
147 getHeader()->getDataEncoding() == ELF::ELFDATA2LSB
;
150 Expected
<Elf_Shdr_Range
> sections() const;
152 Expected
<Elf_Dyn_Range
> dynamicEntries() const;
154 Expected
<const uint8_t *> toMappedAddr(uint64_t VAddr
) const;
156 Expected
<Elf_Sym_Range
> symbols(const Elf_Shdr
*Sec
) const {
158 return makeArrayRef
<Elf_Sym
>(nullptr, nullptr);
159 return getSectionContentsAsArray
<Elf_Sym
>(Sec
);
162 Expected
<Elf_Rela_Range
> relas(const Elf_Shdr
*Sec
) const {
163 return getSectionContentsAsArray
<Elf_Rela
>(Sec
);
166 Expected
<Elf_Rel_Range
> rels(const Elf_Shdr
*Sec
) const {
167 return getSectionContentsAsArray
<Elf_Rel
>(Sec
);
170 Expected
<Elf_Relr_Range
> relrs(const Elf_Shdr
*Sec
) const {
171 return getSectionContentsAsArray
<Elf_Relr
>(Sec
);
174 Expected
<std::vector
<Elf_Rela
>> decode_relrs(Elf_Relr_Range relrs
) const;
176 Expected
<std::vector
<Elf_Rela
>> android_relas(const Elf_Shdr
*Sec
) const;
178 /// Iterate over program header table.
179 Expected
<Elf_Phdr_Range
> program_headers() const {
180 if (getHeader()->e_phnum
&& getHeader()->e_phentsize
!= sizeof(Elf_Phdr
))
181 return createError("invalid e_phentsize: " +
182 Twine(getHeader()->e_phentsize
));
183 if (getHeader()->e_phoff
+
184 (getHeader()->e_phnum
* getHeader()->e_phentsize
) >
186 return createError("program headers are longer than binary of size " +
187 Twine(getBufSize()) + ": e_phoff = 0x" +
188 Twine::utohexstr(getHeader()->e_phoff
) +
189 ", e_phnum = " + Twine(getHeader()->e_phnum
) +
190 ", e_phentsize = " + Twine(getHeader()->e_phentsize
));
192 reinterpret_cast<const Elf_Phdr
*>(base() + getHeader()->e_phoff
);
193 return makeArrayRef(Begin
, Begin
+ getHeader()->e_phnum
);
196 /// Get an iterator over notes in a program header.
198 /// The program header must be of type \c PT_NOTE.
200 /// \param Phdr the program header to iterate over.
201 /// \param Err [out] an error to support fallible iteration, which should
202 /// be checked after iteration ends.
203 Elf_Note_Iterator
notes_begin(const Elf_Phdr
&Phdr
, Error
&Err
) const {
204 assert(Phdr
.p_type
== ELF::PT_NOTE
&& "Phdr is not of type PT_NOTE");
205 ErrorAsOutParameter
ErrAsOutParam(&Err
);
206 if (Phdr
.p_offset
+ Phdr
.p_filesz
> getBufSize()) {
207 Err
= createError("PT_NOTE header has invalid offset (0x" +
208 Twine::utohexstr(Phdr
.p_offset
) + ") or size (0x" +
209 Twine::utohexstr(Phdr
.p_filesz
) + ")");
210 return Elf_Note_Iterator(Err
);
212 return Elf_Note_Iterator(base() + Phdr
.p_offset
, Phdr
.p_filesz
, Err
);
215 /// Get an iterator over notes in a section.
217 /// The section must be of type \c SHT_NOTE.
219 /// \param Shdr the section to iterate over.
220 /// \param Err [out] an error to support fallible iteration, which should
221 /// be checked after iteration ends.
222 Elf_Note_Iterator
notes_begin(const Elf_Shdr
&Shdr
, Error
&Err
) const {
223 assert(Shdr
.sh_type
== ELF::SHT_NOTE
&& "Shdr is not of type SHT_NOTE");
224 ErrorAsOutParameter
ErrAsOutParam(&Err
);
225 if (Shdr
.sh_offset
+ Shdr
.sh_size
> getBufSize()) {
226 Err
= createError("SHT_NOTE section " + getSecIndexForError(this, &Shdr
) +
227 " has invalid offset (0x" +
228 Twine::utohexstr(Shdr
.sh_offset
) + ") or size (0x" +
229 Twine::utohexstr(Shdr
.sh_size
) + ")");
230 return Elf_Note_Iterator(Err
);
232 return Elf_Note_Iterator(base() + Shdr
.sh_offset
, Shdr
.sh_size
, Err
);
235 /// Get the end iterator for notes.
236 Elf_Note_Iterator
notes_end() const {
237 return Elf_Note_Iterator();
240 /// Get an iterator range over notes of a program header.
242 /// The program header must be of type \c PT_NOTE.
244 /// \param Phdr the program header to iterate over.
245 /// \param Err [out] an error to support fallible iteration, which should
246 /// be checked after iteration ends.
247 iterator_range
<Elf_Note_Iterator
> notes(const Elf_Phdr
&Phdr
,
249 return make_range(notes_begin(Phdr
, Err
), notes_end());
252 /// Get an iterator range over notes of a section.
254 /// The section must be of type \c SHT_NOTE.
256 /// \param Shdr the section to iterate over.
257 /// \param Err [out] an error to support fallible iteration, which should
258 /// be checked after iteration ends.
259 iterator_range
<Elf_Note_Iterator
> notes(const Elf_Shdr
&Shdr
,
261 return make_range(notes_begin(Shdr
, Err
), notes_end());
264 Expected
<StringRef
> getSectionStringTable(Elf_Shdr_Range Sections
) const;
265 Expected
<uint32_t> getSectionIndex(const Elf_Sym
*Sym
, Elf_Sym_Range Syms
,
266 ArrayRef
<Elf_Word
> ShndxTable
) const;
267 Expected
<const Elf_Shdr
*> getSection(const Elf_Sym
*Sym
,
268 const Elf_Shdr
*SymTab
,
269 ArrayRef
<Elf_Word
> ShndxTable
) const;
270 Expected
<const Elf_Shdr
*> getSection(const Elf_Sym
*Sym
,
271 Elf_Sym_Range Symtab
,
272 ArrayRef
<Elf_Word
> ShndxTable
) const;
273 Expected
<const Elf_Shdr
*> getSection(uint32_t Index
) const;
275 Expected
<const Elf_Sym
*> getSymbol(const Elf_Shdr
*Sec
,
276 uint32_t Index
) const;
278 Expected
<StringRef
> getSectionName(const Elf_Shdr
*Section
) const;
279 Expected
<StringRef
> getSectionName(const Elf_Shdr
*Section
,
280 StringRef DotShstrtab
) const;
281 template <typename T
>
282 Expected
<ArrayRef
<T
>> getSectionContentsAsArray(const Elf_Shdr
*Sec
) const;
283 Expected
<ArrayRef
<uint8_t>> getSectionContents(const Elf_Shdr
*Sec
) const;
286 using ELF32LEFile
= ELFFile
<ELF32LE
>;
287 using ELF64LEFile
= ELFFile
<ELF64LE
>;
288 using ELF32BEFile
= ELFFile
<ELF32BE
>;
289 using ELF64BEFile
= ELFFile
<ELF64BE
>;
291 template <class ELFT
>
292 inline Expected
<const typename
ELFT::Shdr
*>
293 getSection(typename
ELFT::ShdrRange Sections
, uint32_t Index
) {
294 if (Index
>= Sections
.size())
295 return createError("invalid section index: " + Twine(Index
));
296 return &Sections
[Index
];
299 template <class ELFT
>
300 inline Expected
<uint32_t>
301 getExtendedSymbolTableIndex(const typename
ELFT::Sym
*Sym
,
302 const typename
ELFT::Sym
*FirstSym
,
303 ArrayRef
<typename
ELFT::Word
> ShndxTable
) {
304 assert(Sym
->st_shndx
== ELF::SHN_XINDEX
);
305 unsigned Index
= Sym
- FirstSym
;
306 if (Index
>= ShndxTable
.size())
308 "extended symbol index (" + Twine(Index
) +
309 ") is past the end of the SHT_SYMTAB_SHNDX section of size " +
310 Twine(ShndxTable
.size()));
312 // The size of the table was checked in getSHNDXTable.
313 return ShndxTable
[Index
];
316 template <class ELFT
>
318 ELFFile
<ELFT
>::getSectionIndex(const Elf_Sym
*Sym
, Elf_Sym_Range Syms
,
319 ArrayRef
<Elf_Word
> ShndxTable
) const {
320 uint32_t Index
= Sym
->st_shndx
;
321 if (Index
== ELF::SHN_XINDEX
) {
322 auto ErrorOrIndex
= getExtendedSymbolTableIndex
<ELFT
>(
323 Sym
, Syms
.begin(), ShndxTable
);
325 return ErrorOrIndex
.takeError();
326 return *ErrorOrIndex
;
328 if (Index
== ELF::SHN_UNDEF
|| Index
>= ELF::SHN_LORESERVE
)
333 template <class ELFT
>
334 Expected
<const typename
ELFT::Shdr
*>
335 ELFFile
<ELFT
>::getSection(const Elf_Sym
*Sym
, const Elf_Shdr
*SymTab
,
336 ArrayRef
<Elf_Word
> ShndxTable
) const {
337 auto SymsOrErr
= symbols(SymTab
);
339 return SymsOrErr
.takeError();
340 return getSection(Sym
, *SymsOrErr
, ShndxTable
);
343 template <class ELFT
>
344 Expected
<const typename
ELFT::Shdr
*>
345 ELFFile
<ELFT
>::getSection(const Elf_Sym
*Sym
, Elf_Sym_Range Symbols
,
346 ArrayRef
<Elf_Word
> ShndxTable
) const {
347 auto IndexOrErr
= getSectionIndex(Sym
, Symbols
, ShndxTable
);
349 return IndexOrErr
.takeError();
350 uint32_t Index
= *IndexOrErr
;
353 return getSection(Index
);
356 template <class ELFT
>
357 Expected
<const typename
ELFT::Sym
*>
358 ELFFile
<ELFT
>::getSymbol(const Elf_Shdr
*Sec
, uint32_t Index
) const {
359 auto SymsOrErr
= symbols(Sec
);
361 return SymsOrErr
.takeError();
363 Elf_Sym_Range Symbols
= *SymsOrErr
;
364 if (Index
>= Symbols
.size())
365 return createError("unable to get symbol from section " +
366 getSecIndexForError(this, Sec
) +
367 ": invalid symbol index (" + Twine(Index
) + ")");
368 return &Symbols
[Index
];
371 template <class ELFT
>
372 template <typename T
>
373 Expected
<ArrayRef
<T
>>
374 ELFFile
<ELFT
>::getSectionContentsAsArray(const Elf_Shdr
*Sec
) const {
375 if (Sec
->sh_entsize
!= sizeof(T
) && sizeof(T
) != 1)
376 return createError("section " + getSecIndexForError(this, Sec
) +
377 " has an invalid sh_entsize: " + Twine(Sec
->sh_entsize
));
379 uintX_t Offset
= Sec
->sh_offset
;
380 uintX_t Size
= Sec
->sh_size
;
382 if (Size
% sizeof(T
))
383 return createError("section " + getSecIndexForError(this, Sec
) +
384 " has an invalid sh_size (" + Twine(Size
) +
385 ") which is not a multiple of its sh_entsize (" +
386 Twine(Sec
->sh_entsize
) + ")");
387 if ((std::numeric_limits
<uintX_t
>::max() - Offset
< Size
) ||
388 Offset
+ Size
> Buf
.size())
389 return createError("section " + getSecIndexForError(this, Sec
) +
390 " has a sh_offset (0x" + Twine::utohexstr(Offset
) +
391 ") + sh_size (0x" + Twine(Size
) +
392 ") that cannot be represented");
394 if (Offset
% alignof(T
))
395 // TODO: this error is untested.
396 return createError("unaligned data");
398 const T
*Start
= reinterpret_cast<const T
*>(base() + Offset
);
399 return makeArrayRef(Start
, Size
/ sizeof(T
));
402 template <class ELFT
>
403 Expected
<ArrayRef
<uint8_t>>
404 ELFFile
<ELFT
>::getSectionContents(const Elf_Shdr
*Sec
) const {
405 return getSectionContentsAsArray
<uint8_t>(Sec
);
408 template <class ELFT
>
409 StringRef ELFFile
<ELFT
>::getRelocationTypeName(uint32_t Type
) const {
410 return getELFRelocationTypeName(getHeader()->e_machine
, Type
);
413 template <class ELFT
>
414 void ELFFile
<ELFT
>::getRelocationTypeName(uint32_t Type
,
415 SmallVectorImpl
<char> &Result
) const {
416 if (!isMipsELF64()) {
417 StringRef Name
= getRelocationTypeName(Type
);
418 Result
.append(Name
.begin(), Name
.end());
420 // The Mips N64 ABI allows up to three operations to be specified per
421 // relocation record. Unfortunately there's no easy way to test for the
422 // presence of N64 ELFs as they have no special flag that identifies them
423 // as being N64. We can safely assume at the moment that all Mips
424 // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
425 // information to disambiguate between old vs new ABIs.
426 uint8_t Type1
= (Type
>> 0) & 0xFF;
427 uint8_t Type2
= (Type
>> 8) & 0xFF;
428 uint8_t Type3
= (Type
>> 16) & 0xFF;
430 // Concat all three relocation type names.
431 StringRef Name
= getRelocationTypeName(Type1
);
432 Result
.append(Name
.begin(), Name
.end());
434 Name
= getRelocationTypeName(Type2
);
435 Result
.append(1, '/');
436 Result
.append(Name
.begin(), Name
.end());
438 Name
= getRelocationTypeName(Type3
);
439 Result
.append(1, '/');
440 Result
.append(Name
.begin(), Name
.end());
444 template <class ELFT
>
445 uint32_t ELFFile
<ELFT
>::getRelativeRelocationType() const {
446 return getELFRelativeRelocationType(getHeader()->e_machine
);
449 template <class ELFT
>
450 Expected
<const typename
ELFT::Sym
*>
451 ELFFile
<ELFT
>::getRelocationSymbol(const Elf_Rel
*Rel
,
452 const Elf_Shdr
*SymTab
) const {
453 uint32_t Index
= Rel
->getSymbol(isMips64EL());
456 return getEntry
<Elf_Sym
>(SymTab
, Index
);
459 template <class ELFT
>
461 ELFFile
<ELFT
>::getSectionStringTable(Elf_Shdr_Range Sections
) const {
462 uint32_t Index
= getHeader()->e_shstrndx
;
463 if (Index
== ELF::SHN_XINDEX
)
464 Index
= Sections
[0].sh_link
;
466 if (!Index
) // no section string table.
468 // TODO: Test a case when the sh_link of the section with index 0 is broken.
469 if (Index
>= Sections
.size())
470 return createError("section header string table index " + Twine(Index
) +
472 return getStringTable(&Sections
[Index
]);
475 template <class ELFT
> ELFFile
<ELFT
>::ELFFile(StringRef Object
) : Buf(Object
) {}
477 template <class ELFT
>
478 Expected
<ELFFile
<ELFT
>> ELFFile
<ELFT
>::create(StringRef Object
) {
479 if (sizeof(Elf_Ehdr
) > Object
.size())
480 return createError("invalid buffer: the size (" + Twine(Object
.size()) +
481 ") is smaller than an ELF header (" +
482 Twine(sizeof(Elf_Ehdr
)) + ")");
483 return ELFFile(Object
);
486 template <class ELFT
>
487 Expected
<typename
ELFT::ShdrRange
> ELFFile
<ELFT
>::sections() const {
488 const uintX_t SectionTableOffset
= getHeader()->e_shoff
;
489 if (SectionTableOffset
== 0)
490 return ArrayRef
<Elf_Shdr
>();
492 if (getHeader()->e_shentsize
!= sizeof(Elf_Shdr
))
493 return createError("invalid e_shentsize in ELF header: " +
494 Twine(getHeader()->e_shentsize
));
496 const uint64_t FileSize
= Buf
.size();
497 if (SectionTableOffset
+ sizeof(Elf_Shdr
) > FileSize
||
498 SectionTableOffset
+ (uintX_t
)sizeof(Elf_Shdr
) < SectionTableOffset
)
500 "section header table goes past the end of the file: e_shoff = 0x" +
501 Twine::utohexstr(SectionTableOffset
));
503 // Invalid address alignment of section headers
504 if (SectionTableOffset
& (alignof(Elf_Shdr
) - 1))
505 // TODO: this error is untested.
506 return createError("invalid alignment of section headers");
508 const Elf_Shdr
*First
=
509 reinterpret_cast<const Elf_Shdr
*>(base() + SectionTableOffset
);
511 uintX_t NumSections
= getHeader()->e_shnum
;
512 if (NumSections
== 0)
513 NumSections
= First
->sh_size
;
515 if (NumSections
> UINT64_MAX
/ sizeof(Elf_Shdr
))
516 return createError("invalid number of sections specified in the NULL "
517 "section's sh_size field (" +
518 Twine(NumSections
) + ")");
520 const uint64_t SectionTableSize
= NumSections
* sizeof(Elf_Shdr
);
521 if (SectionTableOffset
+ SectionTableSize
< SectionTableOffset
)
523 "invalid section header table offset (e_shoff = 0x" +
524 Twine::utohexstr(SectionTableOffset
) +
525 ") or invalid number of sections specified in the first section "
526 "header's sh_size field (0x" +
527 Twine::utohexstr(NumSections
) + ")");
529 // Section table goes past end of file!
530 if (SectionTableOffset
+ SectionTableSize
> FileSize
)
531 return createError("section table goes past the end of file");
532 return makeArrayRef(First
, NumSections
);
535 template <class ELFT
>
536 template <typename T
>
537 Expected
<const T
*> ELFFile
<ELFT
>::getEntry(uint32_t Section
,
538 uint32_t Entry
) const {
539 auto SecOrErr
= getSection(Section
);
541 return SecOrErr
.takeError();
542 return getEntry
<T
>(*SecOrErr
, Entry
);
545 template <class ELFT
>
546 template <typename T
>
547 Expected
<const T
*> ELFFile
<ELFT
>::getEntry(const Elf_Shdr
*Section
,
548 uint32_t Entry
) const {
549 if (sizeof(T
) != Section
->sh_entsize
)
550 // TODO: this error is untested.
551 return createError("invalid sh_entsize");
552 size_t Pos
= Section
->sh_offset
+ Entry
* sizeof(T
);
553 if (Pos
+ sizeof(T
) > Buf
.size())
554 return createError("unable to access section " +
555 getSecIndexForError(this, Section
) + " data at 0x" +
556 Twine::utohexstr(Pos
) +
557 ": offset goes past the end of file");
558 return reinterpret_cast<const T
*>(base() + Pos
);
561 template <class ELFT
>
562 Expected
<const typename
ELFT::Shdr
*>
563 ELFFile
<ELFT
>::getSection(uint32_t Index
) const {
564 auto TableOrErr
= sections();
566 return TableOrErr
.takeError();
567 return object::getSection
<ELFT
>(*TableOrErr
, Index
);
570 template <class ELFT
>
572 ELFFile
<ELFT
>::getStringTable(const Elf_Shdr
*Section
) const {
573 if (Section
->sh_type
!= ELF::SHT_STRTAB
)
574 return createError("invalid sh_type for string table section " +
575 getSecIndexForError(this, Section
) +
576 ": expected SHT_STRTAB, but got " +
577 object::getELFSectionTypeName(getHeader()->e_machine
,
579 auto V
= getSectionContentsAsArray
<char>(Section
);
581 return V
.takeError();
582 ArrayRef
<char> Data
= *V
;
584 return createError("SHT_STRTAB string table section " +
585 getSecIndexForError(this, Section
) + " is empty");
586 if (Data
.back() != '\0')
587 return createError("SHT_STRTAB string table section " +
588 getSecIndexForError(this, Section
) +
589 " is non-null terminated");
590 return StringRef(Data
.begin(), Data
.size());
593 template <class ELFT
>
594 Expected
<ArrayRef
<typename
ELFT::Word
>>
595 ELFFile
<ELFT
>::getSHNDXTable(const Elf_Shdr
&Section
) const {
596 auto SectionsOrErr
= sections();
598 return SectionsOrErr
.takeError();
599 return getSHNDXTable(Section
, *SectionsOrErr
);
602 template <class ELFT
>
603 Expected
<ArrayRef
<typename
ELFT::Word
>>
604 ELFFile
<ELFT
>::getSHNDXTable(const Elf_Shdr
&Section
,
605 Elf_Shdr_Range Sections
) const {
606 assert(Section
.sh_type
== ELF::SHT_SYMTAB_SHNDX
);
607 auto VOrErr
= getSectionContentsAsArray
<Elf_Word
>(&Section
);
609 return VOrErr
.takeError();
610 ArrayRef
<Elf_Word
> V
= *VOrErr
;
611 auto SymTableOrErr
= object::getSection
<ELFT
>(Sections
, Section
.sh_link
);
613 return SymTableOrErr
.takeError();
614 const Elf_Shdr
&SymTable
= **SymTableOrErr
;
615 if (SymTable
.sh_type
!= ELF::SHT_SYMTAB
&&
616 SymTable
.sh_type
!= ELF::SHT_DYNSYM
)
617 // TODO: this error is untested.
618 return createError("invalid sh_type");
619 if (V
.size() != (SymTable
.sh_size
/ sizeof(Elf_Sym
)))
620 return createError("SHT_SYMTAB_SHNDX section has sh_size (" +
621 Twine(SymTable
.sh_size
) +
622 ") which is not equal to the number of symbols (" +
623 Twine(V
.size()) + ")");
627 template <class ELFT
>
629 ELFFile
<ELFT
>::getStringTableForSymtab(const Elf_Shdr
&Sec
) const {
630 auto SectionsOrErr
= sections();
632 return SectionsOrErr
.takeError();
633 return getStringTableForSymtab(Sec
, *SectionsOrErr
);
636 template <class ELFT
>
638 ELFFile
<ELFT
>::getStringTableForSymtab(const Elf_Shdr
&Sec
,
639 Elf_Shdr_Range Sections
) const {
641 if (Sec
.sh_type
!= ELF::SHT_SYMTAB
&& Sec
.sh_type
!= ELF::SHT_DYNSYM
)
642 // TODO: this error is untested.
644 "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
645 auto SectionOrErr
= object::getSection
<ELFT
>(Sections
, Sec
.sh_link
);
647 return SectionOrErr
.takeError();
648 return getStringTable(*SectionOrErr
);
651 template <class ELFT
>
653 ELFFile
<ELFT
>::getSectionName(const Elf_Shdr
*Section
) const {
654 auto SectionsOrErr
= sections();
656 return SectionsOrErr
.takeError();
657 auto Table
= getSectionStringTable(*SectionsOrErr
);
659 return Table
.takeError();
660 return getSectionName(Section
, *Table
);
663 template <class ELFT
>
664 Expected
<StringRef
> ELFFile
<ELFT
>::getSectionName(const Elf_Shdr
*Section
,
665 StringRef DotShstrtab
) const {
666 uint32_t Offset
= Section
->sh_name
;
669 if (Offset
>= DotShstrtab
.size())
670 return createError("a section " + getSecIndexForError(this, Section
) +
671 " has an invalid sh_name (0x" +
672 Twine::utohexstr(Offset
) +
673 ") offset which goes past the end of the "
674 "section name string table");
675 return StringRef(DotShstrtab
.data() + Offset
);
678 /// This function returns the hash value for a symbol in the .dynsym section
679 /// Name of the API remains consistent as specified in the libelf
680 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
681 inline unsigned hashSysV(StringRef SymbolName
) {
683 for (char C
: SymbolName
) {
693 } // end namespace object
694 } // end namespace llvm
696 #endif // LLVM_OBJECT_ELF_H