ELFSectionHeaderTable.hxx revision bafb650811e8bcf6fbc014face6e127502869e68
1#ifndef ELF_SECTION_HEADER_TABLE_HXX
2#define ELF_SECTION_HEADER_TABLE_HXX
3
4#include "ELFHeader.h"
5#include "ELFObject.h"
6#include "ELFSectionHeader.h"
7
8#include <assert.h>
9
10template <unsigned Bitwidth>
11ELFSectionHeaderTable<Bitwidth>::~ELFSectionHeaderTable() {
12  for (size_t i = 0; i < table.size(); ++i) {
13    delete table[i];
14  }
15}
16
17template <unsigned Bitwidth>
18template <typename Archiver>
19inline ELFSectionHeaderTable<Bitwidth> *
20ELFSectionHeaderTable<Bitwidth>::read(Archiver &AR,
21                                      ELFObject<Bitwidth> *owner) {
22  if (!AR) {
23    // Archiver is in bad state before calling read function.
24    // Return NULL and do nothing.
25    return 0;
26  }
27
28  // Allocate a new section header table and assign the owner.
29  llvm::OwningPtr<ELFSectionHeaderTable> tab(new ELFSectionHeaderTable());
30
31  // Get ELF header
32  ELFHeader<Bitwidth> const *header = owner->getHeader();
33
34  assert(header->getSectionHeaderEntrySize() >=
35         TypeTraits<ELFSectionHeader<Bitwidth> >::size);
36
37  size_t pending = TypeTraits<ELFSectionHeader<Bitwidth> >::size -
38                   header->getSectionHeaderEntrySize();
39
40  // Seek to the address of section header
41  AR.seek(header->getSectionHeaderTableOffset(), true);
42
43  for (size_t i = 0; i < header->getSectionHeaderNum(); ++i) {
44    llvm::OwningPtr<ELFSectionHeader<Bitwidth> > sh(
45      ELFSectionHeader<Bitwidth>::read(AR, owner, i));
46
47    if (!sh) {
48      // Something wrong while reading the section header.
49      return 0;
50    }
51
52    AR.seek(pending);
53    tab->table.push_back(sh.take());
54  }
55
56  return tab.take();
57}
58
59template <unsigned Bitwidth>
60inline void ELFSectionHeaderTable<Bitwidth>::print() const {
61  using namespace llvm;
62
63  out() << '\n' << fillformat('=', 79) << '\n';
64  out().changeColor(raw_ostream::WHITE, true);
65  out() << "ELF Section Header Table" << '\n';
66  out().resetColor();
67
68  for (size_t i = 0; i < table.size(); ++i) {
69    (*this)[i]->print();
70  }
71
72  out() << fillformat('=', 79) << '\n';
73}
74
75template <unsigned Bitwidth>
76inline ELFSectionHeader<Bitwidth> const *
77ELFSectionHeaderTable<Bitwidth>::getByName(const std::string &str) const {
78  // TODO: Use map
79  for (size_t i = 0; i < table.size(); ++i) {
80    if (str == std::string(table[i]->getName())) {
81      return table[i];
82    }
83  }
84  // Return SHN_UNDEF section header;
85  return table[0];
86}
87
88template <unsigned Bitwidth>
89inline ELFSectionHeader<Bitwidth> *
90ELFSectionHeaderTable<Bitwidth>::getByName(const std::string &str) {
91  ELFSectionHeaderTable<Bitwidth> const *const_this = this;
92  ELFSectionHeader<Bitwidth> const *shptr = const_this->getByName(str);
93  // Const cast for the same API's const and non-const versions.
94  return const_cast<ELFSectionHeader<Bitwidth> *>(shptr);
95}
96
97#endif // ELF_SECTION_HEADER_TABLE_HXX
98