ELFSectionHeaderTable.h revision af521b05a143c96604dbb7488e155c5248e34462
1#ifndef ELF_SECTION_HEADER_TABLE_H
2#define ELF_SECTION_HEADER_TABLE_H
3
4#include <boost/shared_ptr.hpp>
5#include <vector>
6
7#include <assert.h>
8
9template <size_t Bitwidth> class ELFObject;
10template <size_t Bitwidth> class ELFSectionHeader;
11
12template <size_t Bitwidth>
13class ELFSectionHeaderTable {
14private:
15  ELFObject<Bitwidth> *owner;
16  std::vector<boost::shared_ptr<ELFSectionHeader<Bitwidth> > > table;
17
18private:
19  ELFSectionHeaderTable() {
20  }
21
22public:
23  template <typename Archiver>
24  static boost::shared_ptr<ELFSectionHeaderTable<Bitwidth> >
25  read(Archiver &AR, ELFObject<Bitwidth> *owner);
26
27  ELFSectionHeader<Bitwidth> const *operator[](size_t i) const {
28    return table[i].get();
29  }
30
31  ELFSectionHeader<Bitwidth> *operator[](size_t i) {
32    return table[i].get();
33  }
34
35  void print();
36};
37
38
39#include "ELFObject.h"
40#include "ELFHeader.h"
41#include "ELFSectionHeader.h"
42#include "ELFSection.h"
43#include "ELFTypes.h"
44
45
46template <size_t Bitwidth>
47template <typename Archiver>
48inline boost::shared_ptr<ELFSectionHeaderTable<Bitwidth> >
49ELFSectionHeaderTable<Bitwidth>::read(Archiver &AR,
50                                      ELFObject<Bitwidth> *owner) {
51  if (!AR) {
52    // Archiver is in bad state before calling read function.
53    // Return NULL and do nothing.
54    return boost::shared_ptr<ELFSectionHeaderTable>();
55  }
56
57  // Allocate a new section header table and assign the owner.
58  boost::shared_ptr<ELFSectionHeaderTable> tab(new ELFSectionHeaderTable());
59  tab->owner = owner;
60
61  // Get ELF header
62  ELFHeader<Bitwidth> const *header = owner->getHeader();
63
64  assert(header->getSectionHeaderEntrySize() >=
65         TypeTraits<ELFSectionHeader<Bitwidth> >::size);
66
67  size_t pending = TypeTraits<ELFSectionHeader<Bitwidth> >::size -
68                   header->getSectionHeaderEntrySize();
69
70  // Seek to the address of section header
71  AR.seek(header->getSectionHeaderTableOffset(), true);
72
73  for (size_t i = 0; i < header->getSectionHeaderNum(); ++i) {
74    boost::shared_ptr<ELFSectionHeader<Bitwidth> > sh(
75      ELFSectionHeader<Bitwidth>::read(AR, i));
76
77    if (!sh) {
78      // Something wrong while reading the section header.
79      return boost::shared_ptr<ELFSectionHeaderTable>();
80    }
81
82    AR.seek(pending);
83    tab->table.push_back(sh);
84  }
85
86  return tab;
87}
88
89template <size_t Bitwidth>
90inline void ELFSectionHeaderTable<Bitwidth>::print() {
91  using namespace std;
92  using namespace term;
93  using namespace term::color;
94
95  cout << endl << setw(79) << setfill('=') << '=' << endl;
96  cout << light::white()
97       << "ELF Section Header Table" << normal() << endl;
98
99  for (size_t i = 0; i < table.size(); ++i) {
100    table[i]->print();
101  }
102
103  cout << setw(79) << setfill('=') << '=' << endl << endl;
104}
105
106#endif // ELF_SECTION_HEADER_TABLE_H
107