ELFSectionRelTable.hxx revision c40d8a8b26547ab9c51792d9d9b3aca13fb5cdf9
1#ifndef ELF_SECTION_REL_TABLE_HXX
2#define ELF_SECTION_REL_TABLE_HXX
3
4#include "ELFReloc.h"
5
6template <unsigned Bitwidth>
7ELFSectionRelTable<Bitwidth>::~ELFSectionRelTable() {
8  using namespace std;
9  for (size_t i = 0; i < table.size(); ++i) {
10    delete table[i];
11  }
12}
13
14template <unsigned Bitwidth>
15void ELFSectionRelTable<Bitwidth>::print() const {
16  using namespace llvm;
17
18  out() << '\n' << fillformat('=', 79) << '\n';
19  out().changeColor(raw_ostream::WHITE, true);
20  out() << "Relocation Table" << '\n';
21  out().resetColor();
22
23  for (size_t i = 0; i < this->size(); ++i) {
24    (*this)[i]->print();
25  }
26
27  out() << fillformat('=', 79) << '\n';
28}
29
30template <unsigned Bitwidth>
31template <typename Archiver>
32ELFSectionRelTable<Bitwidth> *
33ELFSectionRelTable<Bitwidth>::read(Archiver &AR,
34                                   ELFSectionHeaderTy const *sh) {
35
36  rsl_assert(sh->getType() == SHT_REL || sh->getType() == SHT_RELA);
37
38  llvm::OwningPtr<ELFSectionRelTable> rt(new ELFSectionRelTable());
39
40  // Seek to the start of the table
41  AR.seek(sh->getOffset(), true);
42
43  // Count the relocation entries
44  size_t size = sh->getSize() / sh->getEntrySize();
45
46  // Read every relocation entries
47  if (sh->getType() == SHT_REL) {
48    rsl_assert(sh->getEntrySize() == TypeTraits<ELFRelocRelTy>::size);
49    for (size_t i = 0; i < size; ++i) {
50      rt->table.push_back(ELFRelocTy::readRel(AR, i));
51    }
52
53  } else {
54    rsl_assert(sh->getEntrySize() == TypeTraits<ELFRelocRelaTy>::size);
55    for (size_t i = 0; i < size; ++i) {
56      rt->table.push_back(ELFRelocTy::readRela(AR, i));
57    }
58  }
59
60  if (!AR) {
61    // Unable to read the table.
62    return 0;
63  }
64
65  return rt.take();
66}
67
68#endif // ELF_SECTION_REL_TABLE_HXX
69