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