Don't use a DenseMap to handle SHT_SYMTAB_SHNDX.
[oota-llvm.git] / include / llvm / Object / ELF.h
1 //===- 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/PointerIntPair.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Object/ELFTypes.h"
23 #include "llvm/Object/Error.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/ErrorOr.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <algorithm>
32 #include <limits>
33 #include <utility>
34
35 namespace llvm {
36 namespace object {
37
38 StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
39
40 // Subclasses of ELFFile may need this for template instantiation
41 inline std::pair<unsigned char, unsigned char>
42 getElfArchType(StringRef Object) {
43   if (Object.size() < ELF::EI_NIDENT)
44     return std::make_pair((uint8_t)ELF::ELFCLASSNONE,
45                           (uint8_t)ELF::ELFDATANONE);
46   return std::make_pair((uint8_t)Object[ELF::EI_CLASS],
47                         (uint8_t)Object[ELF::EI_DATA]);
48 }
49
50 template <class ELFT>
51 class ELFFile {
52 public:
53   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
54   typedef typename std::conditional<ELFT::Is64Bits,
55                                     uint64_t, uint32_t>::type uintX_t;
56
57   typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
58   typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
59   typedef Elf_Sym_Impl<ELFT> Elf_Sym;
60   typedef Elf_Dyn_Impl<ELFT> Elf_Dyn;
61   typedef Elf_Phdr_Impl<ELFT> Elf_Phdr;
62   typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
63   typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
64   typedef Elf_Verdef_Impl<ELFT> Elf_Verdef;
65   typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
66   typedef Elf_Verneed_Impl<ELFT> Elf_Verneed;
67   typedef Elf_Vernaux_Impl<ELFT> Elf_Vernaux;
68   typedef Elf_Versym_Impl<ELFT> Elf_Versym;
69   typedef Elf_Hash_Impl<ELFT> Elf_Hash;
70   typedef iterator_range<const Elf_Dyn *> Elf_Dyn_Range;
71   typedef iterator_range<const Elf_Shdr *> Elf_Shdr_Range;
72
73   /// \brief Archive files are 2 byte aligned, so we need this for
74   ///     PointerIntPair to work.
75   template <typename T>
76   class ArchivePointerTypeTraits {
77   public:
78     static inline const void *getAsVoidPointer(T *P) { return P; }
79     static inline T *getFromVoidPointer(const void *P) {
80       return static_cast<T *>(P);
81     }
82     enum { NumLowBitsAvailable = 1 };
83   };
84
85   typedef iterator_range<const Elf_Sym *> Elf_Sym_Range;
86
87   const uint8_t *base() const {
88     return reinterpret_cast<const uint8_t *>(Buf.data());
89   }
90
91 private:
92   typedef SmallVector<const Elf_Shdr *, 2> Sections_t;
93
94   StringRef Buf;
95
96   const Elf_Ehdr *Header;
97   const Elf_Shdr *SectionHeaderTable = nullptr;
98   StringRef DotShstrtab;                    // Section header string table.
99   StringRef DotStrtab;                      // Symbol header string table.
100   const Elf_Shdr *dot_symtab_sec = nullptr; // Symbol table section.
101   const Elf_Shdr *DotDynSymSec = nullptr;   // Dynamic symbol table section.
102
103   const Elf_Shdr *SymbolTableSectionHeaderIndex = nullptr;
104
105 public:
106   template<typename T>
107   const T        *getEntry(uint32_t Section, uint32_t Entry) const;
108   template <typename T>
109   const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
110
111   const Elf_Shdr *getDotSymtabSec() const { return dot_symtab_sec; }
112   const Elf_Shdr *getDotDynSymSec() const { return DotDynSymSec; }
113
114   ErrorOr<StringRef> getStringTable(const Elf_Shdr *Section) const;
115   ErrorOr<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
116
117   void VerifyStrTab(const Elf_Shdr *sh) const;
118
119   StringRef getRelocationTypeName(uint32_t Type) const;
120   void getRelocationTypeName(uint32_t Type,
121                              SmallVectorImpl<char> &Result) const;
122
123   /// \brief Get the symbol table section and symbol for a given relocation.
124   template <class RelT>
125   std::pair<const Elf_Shdr *, const Elf_Sym *>
126   getRelocationSymbol(const Elf_Shdr *RelSec, const RelT *Rel) const;
127
128   ELFFile(StringRef Object, std::error_code &EC);
129
130   bool isMipsELF64() const {
131     return Header->e_machine == ELF::EM_MIPS &&
132       Header->getFileClass() == ELF::ELFCLASS64;
133   }
134
135   bool isMips64EL() const {
136     return Header->e_machine == ELF::EM_MIPS &&
137       Header->getFileClass() == ELF::ELFCLASS64 &&
138       Header->getDataEncoding() == ELF::ELFDATA2LSB;
139   }
140
141   ErrorOr<const Elf_Dyn *> dynamic_table_begin(const Elf_Phdr *Phdr) const;
142   ErrorOr<const Elf_Dyn *> dynamic_table_end(const Elf_Phdr *Phdr) const;
143   ErrorOr<Elf_Dyn_Range> dynamic_table(const Elf_Phdr *Phdr) const {
144     ErrorOr<const Elf_Dyn *> Begin = dynamic_table_begin(Phdr);
145     if (std::error_code EC = Begin.getError())
146       return EC;
147     ErrorOr<const Elf_Dyn *> End = dynamic_table_end(Phdr);
148     if (std::error_code EC = End.getError())
149       return EC;
150     return make_range(*Begin, *End);
151   }
152
153   const Elf_Shdr *section_begin() const;
154   const Elf_Shdr *section_end() const;
155   Elf_Shdr_Range sections() const {
156     return make_range(section_begin(), section_end());
157   }
158
159   const Elf_Sym *symbol_begin(const Elf_Shdr *Sec) const {
160     if (!Sec)
161       return nullptr;
162     if (Sec->sh_entsize != sizeof(Elf_Sym))
163       report_fatal_error("Invalid symbol size");
164     return reinterpret_cast<const Elf_Sym *>(base() + Sec->sh_offset);
165   }
166   const Elf_Sym *symbol_end(const Elf_Shdr *Sec) const {
167     if (!Sec)
168       return nullptr;
169     uint64_t Size = Sec->sh_size;
170     if (Size % sizeof(Elf_Sym))
171       report_fatal_error("Invalid symbol table size");
172     return symbol_begin(Sec) + Size / sizeof(Elf_Sym);
173   }
174   Elf_Sym_Range symbols(const Elf_Shdr *Sec) const {
175     return make_range(symbol_begin(Sec), symbol_end(Sec));
176   }
177
178   const Elf_Sym *symbol_begin() const { return symbol_begin(dot_symtab_sec); }
179   const Elf_Sym *symbol_end() const { return symbol_end(dot_symtab_sec); }
180   Elf_Sym_Range symbols() const { return symbols(dot_symtab_sec); }
181
182   const Elf_Sym *dynamic_symbol_begin() const {
183     return symbol_begin(DotDynSymSec);
184   }
185   const Elf_Sym *dynamic_symbol_end() const { return symbol_end(DotDynSymSec); }
186   Elf_Sym_Range dynamic_symbols() const { return symbols(DotDynSymSec); }
187
188   typedef iterator_range<const Elf_Rela *> Elf_Rela_Range;
189
190   const Elf_Rela *rela_begin(const Elf_Shdr *sec) const {
191     if (sec->sh_entsize != sizeof(Elf_Rela))
192       report_fatal_error("Invalid relocation entry size");
193     return reinterpret_cast<const Elf_Rela *>(base() + sec->sh_offset);
194   }
195
196   const Elf_Rela *rela_end(const Elf_Shdr *sec) const {
197     uint64_t Size = sec->sh_size;
198     if (Size % sizeof(Elf_Rela))
199       report_fatal_error("Invalid relocation table size");
200     return rela_begin(sec) + Size / sizeof(Elf_Rela);
201   }
202
203   Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
204     return make_range(rela_begin(Sec), rela_end(Sec));
205   }
206
207   const Elf_Rel *rel_begin(const Elf_Shdr *sec) const {
208     if (sec->sh_entsize != sizeof(Elf_Rel))
209       report_fatal_error("Invalid relocation entry size");
210     return reinterpret_cast<const Elf_Rel *>(base() + sec->sh_offset);
211   }
212
213   const Elf_Rel *rel_end(const Elf_Shdr *sec) const {
214     uint64_t Size = sec->sh_size;
215     if (Size % sizeof(Elf_Rel))
216       report_fatal_error("Invalid relocation table size");
217     return rel_begin(sec) + Size / sizeof(Elf_Rel);
218   }
219
220   typedef iterator_range<const Elf_Rel *> Elf_Rel_Range;
221   Elf_Rel_Range rels(const Elf_Shdr *Sec) const {
222     return make_range(rel_begin(Sec), rel_end(Sec));
223   }
224
225   /// \brief Iterate over program header table.
226   const Elf_Phdr *program_header_begin() const {
227     if (Header->e_phnum && Header->e_phentsize != sizeof(Elf_Phdr))
228       report_fatal_error("Invalid program header size");
229     return reinterpret_cast<const Elf_Phdr *>(base() + Header->e_phoff);
230   }
231
232   const Elf_Phdr *program_header_end() const {
233     return program_header_begin() + Header->e_phnum;
234   }
235
236   typedef iterator_range<const Elf_Phdr *> Elf_Phdr_Range;
237
238   const Elf_Phdr_Range program_headers() const {
239     return make_range(program_header_begin(), program_header_end());
240   }
241
242   uint64_t getNumSections() const;
243   uintX_t getStringTableIndex() const;
244   ELF::Elf64_Word getExtendedSymbolTableIndex(const Elf_Sym *symb) const;
245   const Elf_Ehdr *getHeader() const { return Header; }
246   ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *symb) const;
247   ErrorOr<const Elf_Shdr *> getSection(uint32_t Index) const;
248
249   const Elf_Sym *getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
250     return &*(symbol_begin(Sec) + Index);
251   }
252
253   ErrorOr<StringRef> getSectionName(const Elf_Shdr *Section) const;
254   ErrorOr<ArrayRef<uint8_t> > getSectionContents(const Elf_Shdr *Sec) const;
255 };
256
257 typedef ELFFile<ELFType<support::little, false>> ELF32LEFile;
258 typedef ELFFile<ELFType<support::little, true>> ELF64LEFile;
259 typedef ELFFile<ELFType<support::big, false>> ELF32BEFile;
260 typedef ELFFile<ELFType<support::big, true>> ELF64BEFile;
261
262 template <class ELFT>
263 ELF::Elf64_Word
264 ELFFile<ELFT>::getExtendedSymbolTableIndex(const Elf_Sym *Sym) const {
265   assert(Sym->st_shndx == ELF::SHN_XINDEX);
266   unsigned Index = Sym - symbol_begin();
267
268   // FIXME: error checking
269   const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word *>(
270       base() + SymbolTableSectionHeaderIndex->sh_offset);
271   return ShndxTable[Index];
272 }
273
274 template <class ELFT>
275 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
276 ELFFile<ELFT>::getSection(const Elf_Sym *symb) const {
277   uint32_t Index = symb->st_shndx;
278   if (Index == ELF::SHN_XINDEX)
279     return getSection(getExtendedSymbolTableIndex(symb));
280   if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
281     return nullptr;
282   return getSection(symb->st_shndx);
283 }
284
285 template <class ELFT>
286 ErrorOr<ArrayRef<uint8_t> >
287 ELFFile<ELFT>::getSectionContents(const Elf_Shdr *Sec) const {
288   if (Sec->sh_offset + Sec->sh_size > Buf.size())
289     return object_error::parse_failed;
290   const uint8_t *Start = base() + Sec->sh_offset;
291   return makeArrayRef(Start, Sec->sh_size);
292 }
293
294 template <class ELFT>
295 StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
296   return getELFRelocationTypeName(Header->e_machine, Type);
297 }
298
299 template <class ELFT>
300 void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
301                                           SmallVectorImpl<char> &Result) const {
302   if (!isMipsELF64()) {
303     StringRef Name = getRelocationTypeName(Type);
304     Result.append(Name.begin(), Name.end());
305   } else {
306     // The Mips N64 ABI allows up to three operations to be specified per
307     // relocation record. Unfortunately there's no easy way to test for the
308     // presence of N64 ELFs as they have no special flag that identifies them
309     // as being N64. We can safely assume at the moment that all Mips
310     // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough
311     // information to disambiguate between old vs new ABIs.
312     uint8_t Type1 = (Type >> 0) & 0xFF;
313     uint8_t Type2 = (Type >> 8) & 0xFF;
314     uint8_t Type3 = (Type >> 16) & 0xFF;
315
316     // Concat all three relocation type names.
317     StringRef Name = getRelocationTypeName(Type1);
318     Result.append(Name.begin(), Name.end());
319
320     Name = getRelocationTypeName(Type2);
321     Result.append(1, '/');
322     Result.append(Name.begin(), Name.end());
323
324     Name = getRelocationTypeName(Type3);
325     Result.append(1, '/');
326     Result.append(Name.begin(), Name.end());
327   }
328 }
329
330 template <class ELFT>
331 template <class RelT>
332 std::pair<const typename ELFFile<ELFT>::Elf_Shdr *,
333           const typename ELFFile<ELFT>::Elf_Sym *>
334 ELFFile<ELFT>::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
335   if (!Sec->sh_link)
336     return std::make_pair(nullptr, nullptr);
337   ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Sec->sh_link);
338   if (std::error_code EC = SymTableOrErr.getError())
339     report_fatal_error(EC.message());
340   const Elf_Shdr *SymTable = *SymTableOrErr;
341   return std::make_pair(
342       SymTable, getEntry<Elf_Sym>(SymTable, Rel->getSymbol(isMips64EL())));
343 }
344
345 template <class ELFT>
346 uint64_t ELFFile<ELFT>::getNumSections() const {
347   assert(Header && "Header not initialized!");
348   if (Header->e_shnum == ELF::SHN_UNDEF && Header->e_shoff > 0) {
349     assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
350     return SectionHeaderTable->sh_size;
351   }
352   return Header->e_shnum;
353 }
354
355 template <class ELFT>
356 typename ELFFile<ELFT>::uintX_t ELFFile<ELFT>::getStringTableIndex() const {
357   if (Header->e_shnum == ELF::SHN_UNDEF) {
358     if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
359       return SectionHeaderTable->sh_link;
360     if (Header->e_shstrndx >= getNumSections())
361       return 0;
362   }
363   return Header->e_shstrndx;
364 }
365
366 template <class ELFT>
367 ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
368     : Buf(Object) {
369   const uint64_t FileSize = Buf.size();
370
371   if (sizeof(Elf_Ehdr) > FileSize) {
372     // File too short!
373     EC = object_error::parse_failed;
374     return;
375   }
376
377   Header = reinterpret_cast<const Elf_Ehdr *>(base());
378
379   if (Header->e_shoff == 0)
380     return;
381
382   const uint64_t SectionTableOffset = Header->e_shoff;
383
384   if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) {
385     // Section header table goes past end of file!
386     EC = object_error::parse_failed;
387     return;
388   }
389
390   // The getNumSections() call below depends on SectionHeaderTable being set.
391   SectionHeaderTable =
392     reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
393   const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
394
395   if (SectionTableOffset + SectionTableSize > FileSize) {
396     // Section table goes past end of file!
397     EC = object_error::parse_failed;
398     return;
399   }
400
401   // Scan sections for special sections.
402
403   for (const Elf_Shdr &Sec : sections()) {
404     switch (Sec.sh_type) {
405     case ELF::SHT_SYMTAB_SHNDX:
406       if (SymbolTableSectionHeaderIndex) {
407         // More than one .symtab_shndx!
408         EC = object_error::parse_failed;
409         return;
410       }
411       SymbolTableSectionHeaderIndex = &Sec;
412       break;
413     case ELF::SHT_SYMTAB: {
414       if (dot_symtab_sec) {
415         // More than one .symtab!
416         EC = object_error::parse_failed;
417         return;
418       }
419       dot_symtab_sec = &Sec;
420       ErrorOr<StringRef> SymtabOrErr = getStringTableForSymtab(Sec);
421       if ((EC = SymtabOrErr.getError()))
422         return;
423       DotStrtab = *SymtabOrErr;
424     } break;
425     case ELF::SHT_DYNSYM: {
426       if (DotDynSymSec) {
427         // More than one .dynsym!
428         EC = object_error::parse_failed;
429         return;
430       }
431       DotDynSymSec = &Sec;
432       break;
433     }
434     }
435   }
436
437   // Get string table sections.
438   uintX_t StringTableIndex = getStringTableIndex();
439   if (StringTableIndex) {
440     ErrorOr<const Elf_Shdr *> StrTabSecOrErr = getSection(StringTableIndex);
441     if ((EC = StrTabSecOrErr.getError()))
442       return;
443
444     ErrorOr<StringRef> SymtabOrErr = getStringTable(*StrTabSecOrErr);
445     if ((EC = SymtabOrErr.getError()))
446       return;
447     DotShstrtab = *SymtabOrErr;
448   }
449
450   EC = std::error_code();
451 }
452
453 template <class ELFT>
454 static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
455   return VAddr < Phdr->p_vaddr;
456 }
457
458 template <class ELFT>
459 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
460   if (Header->e_shentsize != sizeof(Elf_Shdr))
461     report_fatal_error(
462         "Invalid section header entry size (e_shentsize) in ELF header");
463   return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
464 }
465
466 template <class ELFT>
467 const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_end() const {
468   return section_begin() + getNumSections();
469 }
470
471 template <class ELFT>
472 ErrorOr<const typename ELFFile<ELFT>::Elf_Dyn *>
473 ELFFile<ELFT>::dynamic_table_begin(const Elf_Phdr *Phdr) const {
474   if (!Phdr)
475     return nullptr;
476   assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
477   uintX_t Offset = Phdr->p_offset;
478   if (Offset > Buf.size())
479     return object_error::parse_failed;
480   return reinterpret_cast<const Elf_Dyn *>(base() + Offset);
481 }
482
483 template <class ELFT>
484 ErrorOr<const typename ELFFile<ELFT>::Elf_Dyn *>
485 ELFFile<ELFT>::dynamic_table_end(const Elf_Phdr *Phdr) const {
486   if (!Phdr)
487     return nullptr;
488   assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
489   uintX_t Size = Phdr->p_filesz;
490   if (Size % sizeof(Elf_Dyn))
491     return object_error::elf_invalid_dynamic_table_size;
492   // FIKME: Check for overflow?
493   uintX_t End = Phdr->p_offset + Size;
494   if (End > Buf.size())
495     return object_error::parse_failed;
496   return reinterpret_cast<const Elf_Dyn *>(base() + End);
497 }
498
499 template <class ELFT>
500 template <typename T>
501 const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
502   ErrorOr<const Elf_Shdr *> Sec = getSection(Section);
503   if (std::error_code EC = Sec.getError())
504     report_fatal_error(EC.message());
505   return getEntry<T>(*Sec, Entry);
506 }
507
508 template <class ELFT>
509 template <typename T>
510 const T *ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
511                                  uint32_t Entry) const {
512   return reinterpret_cast<const T *>(base() + Section->sh_offset +
513                                      (Entry * Section->sh_entsize));
514 }
515
516 template <class ELFT>
517 ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
518 ELFFile<ELFT>::getSection(uint32_t Index) const {
519   assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
520   if (Index >= getNumSections())
521     return object_error::invalid_section_index;
522
523   return reinterpret_cast<const Elf_Shdr *>(
524       reinterpret_cast<const char *>(SectionHeaderTable) +
525       (Index * Header->e_shentsize));
526 }
527
528 template <class ELFT>
529 ErrorOr<StringRef>
530 ELFFile<ELFT>::getStringTable(const Elf_Shdr *Section) const {
531   if (Section->sh_type != ELF::SHT_STRTAB)
532     return object_error::parse_failed;
533   uint64_t Offset = Section->sh_offset;
534   uint64_t Size = Section->sh_size;
535   if (Offset + Size > Buf.size())
536     return object_error::parse_failed;
537   StringRef Data((const char *)base() + Section->sh_offset, Size);
538   if (Data[Size - 1] != '\0')
539     return object_error::string_table_non_null_end;
540   return Data;
541 }
542
543 template <class ELFT>
544 ErrorOr<StringRef>
545 ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
546   if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
547     return object_error::parse_failed;
548   ErrorOr<const Elf_Shdr *> SectionOrErr = getSection(Sec.sh_link);
549   if (std::error_code EC = SectionOrErr.getError())
550     return EC;
551   return getStringTable(*SectionOrErr);
552 }
553
554 template <class ELFT>
555 ErrorOr<StringRef>
556 ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
557   uint32_t Offset = Section->sh_name;
558   if (Offset == 0)
559     return StringRef();
560   if (Offset >= DotShstrtab.size())
561     return object_error::parse_failed;
562   return StringRef(DotShstrtab.data() + Offset);
563 }
564
565 /// This function returns the hash value for a symbol in the .dynsym section
566 /// Name of the API remains consistent as specified in the libelf
567 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
568 static inline unsigned elf_hash(StringRef &symbolName) {
569   unsigned h = 0, g;
570   for (unsigned i = 0, j = symbolName.size(); i < j; i++) {
571     h = (h << 4) + symbolName[i];
572     g = h & 0xf0000000L;
573     if (g != 0)
574       h ^= g >> 24;
575     h &= ~g;
576   }
577   return h;
578 }
579 } // end namespace object
580 } // end namespace llvm
581
582 #endif