blob: 46504e74bc26c4d65217bf249c584b1e7146e760 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- ELF.h - ELF object file implementation -------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the ELFFile template class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_OBJECT_ELF_H
15#define LLVM_OBJECT_ELF_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/BinaryFormat/ELF.h"
21#include "llvm/Object/ELFTypes.h"
22#include "llvm/Object/Error.h"
23#include "llvm/Support/Endian.h"
24#include "llvm/Support/Error.h"
25#include <cassert>
26#include <cstddef>
27#include <cstdint>
28#include <limits>
29#include <utility>
30
31namespace llvm {
32namespace object {
33
34StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
35StringRef 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
47static inline Error createError(StringRef Err) {
48 return make_error<StringError>(Err, object_error::parse_failed);
49}
50
51template <class ELFT>
52class ELFFile {
53public:
54 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
55 using uintX_t = typename ELFT::uint;
56 using Elf_Ehdr = typename ELFT::Ehdr;
57 using Elf_Shdr = typename ELFT::Shdr;
58 using Elf_Sym = typename ELFT::Sym;
59 using Elf_Dyn = typename ELFT::Dyn;
60 using Elf_Phdr = typename ELFT::Phdr;
61 using Elf_Rel = typename ELFT::Rel;
62 using Elf_Rela = typename ELFT::Rela;
63 using Elf_Verdef = typename ELFT::Verdef;
64 using Elf_Verdaux = typename ELFT::Verdaux;
65 using Elf_Verneed = typename ELFT::Verneed;
66 using Elf_Vernaux = typename ELFT::Vernaux;
67 using Elf_Versym = typename ELFT::Versym;
68 using Elf_Hash = typename ELFT::Hash;
69 using Elf_GnuHash = typename ELFT::GnuHash;
70 using Elf_Nhdr = typename ELFT::Nhdr;
71 using Elf_Note = typename ELFT::Note;
72 using Elf_Note_Iterator = typename ELFT::NoteIterator;
73 using Elf_Dyn_Range = typename ELFT::DynRange;
74 using Elf_Shdr_Range = typename ELFT::ShdrRange;
75 using Elf_Sym_Range = typename ELFT::SymRange;
76 using Elf_Rel_Range = typename ELFT::RelRange;
77 using Elf_Rela_Range = typename ELFT::RelaRange;
78 using Elf_Phdr_Range = typename ELFT::PhdrRange;
79
80 const uint8_t *base() const {
81 return reinterpret_cast<const uint8_t *>(Buf.data());
82 }
83
84 size_t getBufSize() const { return Buf.size(); }
85
86private:
87 StringRef Buf;
88
89 ELFFile(StringRef Object);
90
91public:
92 const Elf_Ehdr *getHeader() const {
93 return reinterpret_cast<const Elf_Ehdr *>(base());
94 }
95
96 template <typename T>
97 Expected<const T *> getEntry(uint32_t Section, uint32_t Entry) const;
98 template <typename T>
99 Expected<const T *> getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
100
101 Expected<StringRef> getStringTable(const Elf_Shdr *Section) const;
102 Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
103 Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section,
104 Elf_Shdr_Range Sections) const;
105
106 Expected<ArrayRef<Elf_Word>> getSHNDXTable(const Elf_Shdr &Section) const;
107 Expected<ArrayRef<Elf_Word>> getSHNDXTable(const Elf_Shdr &Section,
108 Elf_Shdr_Range Sections) const;
109
110 StringRef getRelocationTypeName(uint32_t Type) const;
111 void getRelocationTypeName(uint32_t Type,
112 SmallVectorImpl<char> &Result) const;
113
114 /// \brief Get the symbol for a given relocation.
115 Expected<const Elf_Sym *> getRelocationSymbol(const Elf_Rel *Rel,
116 const Elf_Shdr *SymTab) const;
117
118 static Expected<ELFFile> create(StringRef Object);
119
120 bool isMipsELF64() const {
121 return getHeader()->e_machine == ELF::EM_MIPS &&
122 getHeader()->getFileClass() == ELF::ELFCLASS64;
123 }
124
125 bool isMips64EL() const {
126 return isMipsELF64() &&
127 getHeader()->getDataEncoding() == ELF::ELFDATA2LSB;
128 }
129
130 Expected<Elf_Shdr_Range> sections() const;
131
132 Expected<Elf_Sym_Range> symbols(const Elf_Shdr *Sec) const {
133 if (!Sec)
134 return makeArrayRef<Elf_Sym>(nullptr, nullptr);
135 return getSectionContentsAsArray<Elf_Sym>(Sec);
136 }
137
138 Expected<Elf_Rela_Range> relas(const Elf_Shdr *Sec) const {
139 return getSectionContentsAsArray<Elf_Rela>(Sec);
140 }
141
142 Expected<Elf_Rel_Range> rels(const Elf_Shdr *Sec) const {
143 return getSectionContentsAsArray<Elf_Rel>(Sec);
144 }
145
146 Expected<std::vector<Elf_Rela>> android_relas(const Elf_Shdr *Sec) const;
147
148 /// \brief Iterate over program header table.
149 Expected<Elf_Phdr_Range> program_headers() const {
150 if (getHeader()->e_phnum && getHeader()->e_phentsize != sizeof(Elf_Phdr))
151 return createError("invalid e_phentsize");
152 if (getHeader()->e_phoff +
153 (getHeader()->e_phnum * getHeader()->e_phentsize) >
154 getBufSize())
155 return createError("program headers longer than binary");
156 auto *Begin =
157 reinterpret_cast<const Elf_Phdr *>(base() + getHeader()->e_phoff);
158 return makeArrayRef(Begin, Begin + getHeader()->e_phnum);
159 }
160
161 /// Get an iterator over notes in a program header.
162 ///
163 /// The program header must be of type \c PT_NOTE.
164 ///
165 /// \param Phdr the program header to iterate over.
166 /// \param Err [out] an error to support fallible iteration, which should
167 /// be checked after iteration ends.
168 Elf_Note_Iterator notes_begin(const Elf_Phdr &Phdr, Error &Err) const {
169 if (Phdr.p_type != ELF::PT_NOTE) {
170 Err = createError("attempt to iterate notes of non-note program header");
171 return Elf_Note_Iterator(Err);
172 }
173 if (Phdr.p_offset + Phdr.p_filesz > getBufSize()) {
174 Err = createError("invalid program header offset/size");
175 return Elf_Note_Iterator(Err);
176 }
177 return Elf_Note_Iterator(base() + Phdr.p_offset, Phdr.p_filesz, Err);
178 }
179
180 /// Get an iterator over notes in a section.
181 ///
182 /// The section must be of type \c SHT_NOTE.
183 ///
184 /// \param Shdr the section to iterate over.
185 /// \param Err [out] an error to support fallible iteration, which should
186 /// be checked after iteration ends.
187 Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const {
188 if (Shdr.sh_type != ELF::SHT_NOTE) {
189 Err = createError("attempt to iterate notes of non-note section");
190 return Elf_Note_Iterator(Err);
191 }
192 if (Shdr.sh_offset + Shdr.sh_size > getBufSize()) {
193 Err = createError("invalid section offset/size");
194 return Elf_Note_Iterator(Err);
195 }
196 return Elf_Note_Iterator(base() + Shdr.sh_offset, Shdr.sh_size, Err);
197 }
198
199 /// Get the end iterator for notes.
200 Elf_Note_Iterator notes_end() const {
201 return Elf_Note_Iterator();
202 }
203
204 /// Get an iterator range over notes of a program header.
205 ///
206 /// The program header must be of type \c PT_NOTE.
207 ///
208 /// \param Phdr the program header to iterate over.
209 /// \param Err [out] an error to support fallible iteration, which should
210 /// be checked after iteration ends.
211 iterator_range<Elf_Note_Iterator> notes(const Elf_Phdr &Phdr,
212 Error &Err) const {
213 return make_range(notes_begin(Phdr, Err), notes_end());
214 }
215
216 /// Get an iterator range over notes of a section.
217 ///
218 /// The section must be of type \c SHT_NOTE.
219 ///
220 /// \param Shdr the section to iterate over.
221 /// \param Err [out] an error to support fallible iteration, which should
222 /// be checked after iteration ends.
223 iterator_range<Elf_Note_Iterator> notes(const Elf_Shdr &Shdr,
224 Error &Err) const {
225 return make_range(notes_begin(Shdr, Err), notes_end());
226 }
227
228 Expected<StringRef> getSectionStringTable(Elf_Shdr_Range Sections) const;
229 Expected<uint32_t> getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms,
230 ArrayRef<Elf_Word> ShndxTable) const;
231 Expected<const Elf_Shdr *> getSection(const Elf_Sym *Sym,
232 const Elf_Shdr *SymTab,
233 ArrayRef<Elf_Word> ShndxTable) const;
234 Expected<const Elf_Shdr *> getSection(const Elf_Sym *Sym,
235 Elf_Sym_Range Symtab,
236 ArrayRef<Elf_Word> ShndxTable) const;
237 Expected<const Elf_Shdr *> getSection(uint32_t Index) const;
238
239 Expected<const Elf_Sym *> getSymbol(const Elf_Shdr *Sec,
240 uint32_t Index) const;
241
242 Expected<StringRef> getSectionName(const Elf_Shdr *Section) const;
243 Expected<StringRef> getSectionName(const Elf_Shdr *Section,
244 StringRef DotShstrtab) const;
245 template <typename T>
246 Expected<ArrayRef<T>> getSectionContentsAsArray(const Elf_Shdr *Sec) const;
247 Expected<ArrayRef<uint8_t>> getSectionContents(const Elf_Shdr *Sec) const;
248};
249
250using ELF32LEFile = ELFFile<ELF32LE>;
251using ELF64LEFile = ELFFile<ELF64LE>;
252using ELF32BEFile = ELFFile<ELF32BE>;
253using ELF64BEFile = ELFFile<ELF64BE>;
254
255template <class ELFT>
256inline Expected<const typename ELFT::Shdr *>
257getSection(typename ELFT::ShdrRange Sections, uint32_t Index) {
258 if (Index >= Sections.size())
259 return createError("invalid section index");
260 return &Sections[Index];
261}
262
263template <class ELFT>
264inline Expected<uint32_t>
265getExtendedSymbolTableIndex(const typename ELFT::Sym *Sym,
266 const typename ELFT::Sym *FirstSym,
267 ArrayRef<typename ELFT::Word> ShndxTable) {
268 assert(Sym->st_shndx == ELF::SHN_XINDEX);
269 unsigned Index = Sym - FirstSym;
270 if (Index >= ShndxTable.size())
271 return createError("index past the end of the symbol table");
272
273 // The size of the table was checked in getSHNDXTable.
274 return ShndxTable[Index];
275}
276
277template <class ELFT>
278Expected<uint32_t>
279ELFFile<ELFT>::getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms,
280 ArrayRef<Elf_Word> ShndxTable) const {
281 uint32_t Index = Sym->st_shndx;
282 if (Index == ELF::SHN_XINDEX) {
283 auto ErrorOrIndex = getExtendedSymbolTableIndex<ELFT>(
284 Sym, Syms.begin(), ShndxTable);
285 if (!ErrorOrIndex)
286 return ErrorOrIndex.takeError();
287 return *ErrorOrIndex;
288 }
289 if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
290 return 0;
291 return Index;
292}
293
294template <class ELFT>
295Expected<const typename ELFT::Shdr *>
296ELFFile<ELFT>::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
297 ArrayRef<Elf_Word> ShndxTable) const {
298 auto SymsOrErr = symbols(SymTab);
299 if (!SymsOrErr)
300 return SymsOrErr.takeError();
301 return getSection(Sym, *SymsOrErr, ShndxTable);
302}
303
304template <class ELFT>
305Expected<const typename ELFT::Shdr *>
306ELFFile<ELFT>::getSection(const Elf_Sym *Sym, Elf_Sym_Range Symbols,
307 ArrayRef<Elf_Word> ShndxTable) const {
308 auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable);
309 if (!IndexOrErr)
310 return IndexOrErr.takeError();
311 uint32_t Index = *IndexOrErr;
312 if (Index == 0)
313 return nullptr;
314 return getSection(Index);
315}
316
317template <class ELFT>
318inline Expected<const typename ELFT::Sym *>
319getSymbol(typename ELFT::SymRange Symbols, uint32_t Index) {
320 if (Index >= Symbols.size())
321 return createError("invalid symbol index");
322 return &Symbols[Index];
323}
324
325template <class ELFT>
326Expected<const typename ELFT::Sym *>
327ELFFile<ELFT>::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
328 auto SymtabOrErr = symbols(Sec);
329 if (!SymtabOrErr)
330 return SymtabOrErr.takeError();
331 return object::getSymbol<ELFT>(*SymtabOrErr, Index);
332}
333
334template <class ELFT>
335template <typename T>
336Expected<ArrayRef<T>>
337ELFFile<ELFT>::getSectionContentsAsArray(const Elf_Shdr *Sec) const {
338 if (Sec->sh_entsize != sizeof(T) && sizeof(T) != 1)
339 return createError("invalid sh_entsize");
340
341 uintX_t Offset = Sec->sh_offset;
342 uintX_t Size = Sec->sh_size;
343
344 if (Size % sizeof(T))
345 return createError("size is not a multiple of sh_entsize");
346 if ((std::numeric_limits<uintX_t>::max() - Offset < Size) ||
347 Offset + Size > Buf.size())
348 return createError("invalid section offset");
349
350 if (Offset % alignof(T))
351 return createError("unaligned data");
352
353 const T *Start = reinterpret_cast<const T *>(base() + Offset);
354 return makeArrayRef(Start, Size / sizeof(T));
355}
356
357template <class ELFT>
358Expected<ArrayRef<uint8_t>>
359ELFFile<ELFT>::getSectionContents(const Elf_Shdr *Sec) const {
360 return getSectionContentsAsArray<uint8_t>(Sec);
361}
362
363template <class ELFT>
364StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
365 return getELFRelocationTypeName(getHeader()->e_machine, Type);
366}
367
368template <class ELFT>
369void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
370 SmallVectorImpl<char> &Result) const {
371 if (!isMipsELF64()) {
372 StringRef Name = getRelocationTypeName(Type);
373 Result.append(Name.begin(), Name.end());
374 } else {
375 // The Mips N64 ABI allows up to three operations to be specified per
376 // relocation record. Unfortunately there's no easy way to test for the
377 // presence of N64 ELFs as they have no special flag that identifies them
378 // as being N64. We can safely assume at the moment that all Mips
379 // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
380 // information to disambiguate between old vs new ABIs.
381 uint8_t Type1 = (Type >> 0) & 0xFF;
382 uint8_t Type2 = (Type >> 8) & 0xFF;
383 uint8_t Type3 = (Type >> 16) & 0xFF;
384
385 // Concat all three relocation type names.
386 StringRef Name = getRelocationTypeName(Type1);
387 Result.append(Name.begin(), Name.end());
388
389 Name = getRelocationTypeName(Type2);
390 Result.append(1, '/');
391 Result.append(Name.begin(), Name.end());
392
393 Name = getRelocationTypeName(Type3);
394 Result.append(1, '/');
395 Result.append(Name.begin(), Name.end());
396 }
397}
398
399template <class ELFT>
400Expected<const typename ELFT::Sym *>
401ELFFile<ELFT>::getRelocationSymbol(const Elf_Rel *Rel,
402 const Elf_Shdr *SymTab) const {
403 uint32_t Index = Rel->getSymbol(isMips64EL());
404 if (Index == 0)
405 return nullptr;
406 return getEntry<Elf_Sym>(SymTab, Index);
407}
408
409template <class ELFT>
410Expected<StringRef>
411ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections) const {
412 uint32_t Index = getHeader()->e_shstrndx;
413 if (Index == ELF::SHN_XINDEX)
414 Index = Sections[0].sh_link;
415
416 if (!Index) // no section string table.
417 return "";
418 if (Index >= Sections.size())
419 return createError("invalid section index");
420 return getStringTable(&Sections[Index]);
421}
422
423template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
424
425template <class ELFT>
426Expected<ELFFile<ELFT>> ELFFile<ELFT>::create(StringRef Object) {
427 if (sizeof(Elf_Ehdr) > Object.size())
428 return createError("Invalid buffer");
429 return ELFFile(Object);
430}
431
432template <class ELFT>
433Expected<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const {
434 const uintX_t SectionTableOffset = getHeader()->e_shoff;
435 if (SectionTableOffset == 0)
436 return ArrayRef<Elf_Shdr>();
437
438 if (getHeader()->e_shentsize != sizeof(Elf_Shdr))
439 return createError(
440 "invalid section header entry size (e_shentsize) in ELF header");
441
442 const uint64_t FileSize = Buf.size();
443
444 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
445 return createError("section header table goes past the end of the file");
446
447 // Invalid address alignment of section headers
448 if (SectionTableOffset & (alignof(Elf_Shdr) - 1))
449 return createError("invalid alignment of section headers");
450
451 const Elf_Shdr *First =
452 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
453
454 uintX_t NumSections = getHeader()->e_shnum;
455 if (NumSections == 0)
456 NumSections = First->sh_size;
457
458 if (NumSections > UINT64_MAX / sizeof(Elf_Shdr))
459 return createError("section table goes past the end of file");
460
461 const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr);
462
463 // Section table goes past end of file!
464 if (SectionTableOffset + SectionTableSize > FileSize)
465 return createError("section table goes past the end of file");
466
467 return makeArrayRef(First, NumSections);
468}
469
470template <class ELFT>
471template <typename T>
472Expected<const T *> ELFFile<ELFT>::getEntry(uint32_t Section,
473 uint32_t Entry) const {
474 auto SecOrErr = getSection(Section);
475 if (!SecOrErr)
476 return SecOrErr.takeError();
477 return getEntry<T>(*SecOrErr, Entry);
478}
479
480template <class ELFT>
481template <typename T>
482Expected<const T *> ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
483 uint32_t Entry) const {
484 if (sizeof(T) != Section->sh_entsize)
485 return createError("invalid sh_entsize");
486 size_t Pos = Section->sh_offset + Entry * sizeof(T);
487 if (Pos + sizeof(T) > Buf.size())
488 return createError("invalid section offset");
489 return reinterpret_cast<const T *>(base() + Pos);
490}
491
492template <class ELFT>
493Expected<const typename ELFT::Shdr *>
494ELFFile<ELFT>::getSection(uint32_t Index) const {
495 auto TableOrErr = sections();
496 if (!TableOrErr)
497 return TableOrErr.takeError();
498 return object::getSection<ELFT>(*TableOrErr, Index);
499}
500
501template <class ELFT>
502Expected<StringRef>
503ELFFile<ELFT>::getStringTable(const Elf_Shdr *Section) const {
504 if (Section->sh_type != ELF::SHT_STRTAB)
505 return createError("invalid sh_type for string table, expected SHT_STRTAB");
506 auto V = getSectionContentsAsArray<char>(Section);
507 if (!V)
508 return V.takeError();
509 ArrayRef<char> Data = *V;
510 if (Data.empty())
511 return createError("empty string table");
512 if (Data.back() != '\0')
513 return createError("string table non-null terminated");
514 return StringRef(Data.begin(), Data.size());
515}
516
517template <class ELFT>
518Expected<ArrayRef<typename ELFT::Word>>
519ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section) const {
520 auto SectionsOrErr = sections();
521 if (!SectionsOrErr)
522 return SectionsOrErr.takeError();
523 return getSHNDXTable(Section, *SectionsOrErr);
524}
525
526template <class ELFT>
527Expected<ArrayRef<typename ELFT::Word>>
528ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section,
529 Elf_Shdr_Range Sections) const {
530 assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
531 auto VOrErr = getSectionContentsAsArray<Elf_Word>(&Section);
532 if (!VOrErr)
533 return VOrErr.takeError();
534 ArrayRef<Elf_Word> V = *VOrErr;
535 auto SymTableOrErr = object::getSection<ELFT>(Sections, Section.sh_link);
536 if (!SymTableOrErr)
537 return SymTableOrErr.takeError();
538 const Elf_Shdr &SymTable = **SymTableOrErr;
539 if (SymTable.sh_type != ELF::SHT_SYMTAB &&
540 SymTable.sh_type != ELF::SHT_DYNSYM)
541 return createError("invalid sh_type");
542 if (V.size() != (SymTable.sh_size / sizeof(Elf_Sym)))
543 return createError("invalid section contents size");
544 return V;
545}
546
547template <class ELFT>
548Expected<StringRef>
549ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
550 auto SectionsOrErr = sections();
551 if (!SectionsOrErr)
552 return SectionsOrErr.takeError();
553 return getStringTableForSymtab(Sec, *SectionsOrErr);
554}
555
556template <class ELFT>
557Expected<StringRef>
558ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec,
559 Elf_Shdr_Range Sections) const {
560
561 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
562 return createError(
563 "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
564 auto SectionOrErr = object::getSection<ELFT>(Sections, Sec.sh_link);
565 if (!SectionOrErr)
566 return SectionOrErr.takeError();
567 return getStringTable(*SectionOrErr);
568}
569
570template <class ELFT>
571Expected<StringRef>
572ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
573 auto SectionsOrErr = sections();
574 if (!SectionsOrErr)
575 return SectionsOrErr.takeError();
576 auto Table = getSectionStringTable(*SectionsOrErr);
577 if (!Table)
578 return Table.takeError();
579 return getSectionName(Section, *Table);
580}
581
582template <class ELFT>
583Expected<StringRef> ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section,
584 StringRef DotShstrtab) const {
585 uint32_t Offset = Section->sh_name;
586 if (Offset == 0)
587 return StringRef();
588 if (Offset >= DotShstrtab.size())
589 return createError("invalid string offset");
590 return StringRef(DotShstrtab.data() + Offset);
591}
592
593/// This function returns the hash value for a symbol in the .dynsym section
594/// Name of the API remains consistent as specified in the libelf
595/// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
596inline unsigned hashSysV(StringRef SymbolName) {
597 unsigned h = 0, g;
598 for (char C : SymbolName) {
599 h = (h << 4) + C;
600 g = h & 0xf0000000L;
601 if (g != 0)
602 h ^= g >> 24;
603 h &= ~g;
604 }
605 return h;
606}
607
608} // end namespace object
609} // end namespace llvm
610
611#endif // LLVM_OBJECT_ELF_H