ELFSectionHeaderTable.h revision 61dfca9e892597db79889addb7a3abf54cd50588
161dfca9e892597db79889addb7a3abf54cd50588Logan Chien#ifndef ELF_SECTION_HEADER_TABLE_H
261dfca9e892597db79889addb7a3abf54cd50588Logan Chien#define ELF_SECTION_HEADER_TABLE_H
361dfca9e892597db79889addb7a3abf54cd50588Logan Chien
461dfca9e892597db79889addb7a3abf54cd50588Logan Chien#include <boost/shared_ptr.hpp>
561dfca9e892597db79889addb7a3abf54cd50588Logan Chien#include <vector>
661dfca9e892597db79889addb7a3abf54cd50588Logan Chien
761dfca9e892597db79889addb7a3abf54cd50588Logan Chien#include <assert.h>
861dfca9e892597db79889addb7a3abf54cd50588Logan Chien
961dfca9e892597db79889addb7a3abf54cd50588Logan Chientemplate <size_t Bitwidth> class ELFObject;
1061dfca9e892597db79889addb7a3abf54cd50588Logan Chientemplate <size_t Bitwidth> class ELFSectionHeader;
1161dfca9e892597db79889addb7a3abf54cd50588Logan Chien
1261dfca9e892597db79889addb7a3abf54cd50588Logan Chientemplate <size_t Bitwidth>
1361dfca9e892597db79889addb7a3abf54cd50588Logan Chienclass ELFSectionHeaderTable {
1461dfca9e892597db79889addb7a3abf54cd50588Logan Chienprivate:
1561dfca9e892597db79889addb7a3abf54cd50588Logan Chien  ELFObject<Bitwidth> *owner;
1661dfca9e892597db79889addb7a3abf54cd50588Logan Chien  std::vector<boost::shared_ptr<ELFSectionHeader<Bitwidth> > > table;
1761dfca9e892597db79889addb7a3abf54cd50588Logan Chien
1861dfca9e892597db79889addb7a3abf54cd50588Logan Chienprivate:
1961dfca9e892597db79889addb7a3abf54cd50588Logan Chien  ELFSectionHeaderTable() {
2061dfca9e892597db79889addb7a3abf54cd50588Logan Chien  }
2161dfca9e892597db79889addb7a3abf54cd50588Logan Chien
2261dfca9e892597db79889addb7a3abf54cd50588Logan Chienpublic:
2361dfca9e892597db79889addb7a3abf54cd50588Logan Chien  template <typename Archiver>
2461dfca9e892597db79889addb7a3abf54cd50588Logan Chien  static boost::shared_ptr<ELFSectionHeaderTable<Bitwidth> >
2561dfca9e892597db79889addb7a3abf54cd50588Logan Chien  read(Archiver &AR, ELFObject<Bitwidth> *owner);
2661dfca9e892597db79889addb7a3abf54cd50588Logan Chien
2761dfca9e892597db79889addb7a3abf54cd50588Logan Chien  void print();
2861dfca9e892597db79889addb7a3abf54cd50588Logan Chien};
2961dfca9e892597db79889addb7a3abf54cd50588Logan Chien
3061dfca9e892597db79889addb7a3abf54cd50588Logan Chien#include "ELFObject.h"
3161dfca9e892597db79889addb7a3abf54cd50588Logan Chien#include "ELFHeader.h"
3261dfca9e892597db79889addb7a3abf54cd50588Logan Chien#include "ELFSectionHeader.h"
3361dfca9e892597db79889addb7a3abf54cd50588Logan Chien#include "ELFTypes.h"
3461dfca9e892597db79889addb7a3abf54cd50588Logan Chien
3561dfca9e892597db79889addb7a3abf54cd50588Logan Chien
3661dfca9e892597db79889addb7a3abf54cd50588Logan Chientemplate <size_t Bitwidth>
3761dfca9e892597db79889addb7a3abf54cd50588Logan Chientemplate <typename Archiver>
3861dfca9e892597db79889addb7a3abf54cd50588Logan Chieninline boost::shared_ptr<ELFSectionHeaderTable<Bitwidth> >
3961dfca9e892597db79889addb7a3abf54cd50588Logan ChienELFSectionHeaderTable<Bitwidth>::read(Archiver &AR,
4061dfca9e892597db79889addb7a3abf54cd50588Logan Chien                                      ELFObject<Bitwidth> *owner) {
4161dfca9e892597db79889addb7a3abf54cd50588Logan Chien  if (!AR) {
4261dfca9e892597db79889addb7a3abf54cd50588Logan Chien    // Archiver is in bad state before calling read function.
4361dfca9e892597db79889addb7a3abf54cd50588Logan Chien    // Return NULL and do nothing.
4461dfca9e892597db79889addb7a3abf54cd50588Logan Chien    return boost::shared_ptr<ELFSectionHeaderTable>();
4561dfca9e892597db79889addb7a3abf54cd50588Logan Chien  }
4661dfca9e892597db79889addb7a3abf54cd50588Logan Chien
4761dfca9e892597db79889addb7a3abf54cd50588Logan Chien  // Allocate a new section header table and assign the owner.
4861dfca9e892597db79889addb7a3abf54cd50588Logan Chien  boost::shared_ptr<ELFSectionHeaderTable> tab(new ELFSectionHeaderTable());
4961dfca9e892597db79889addb7a3abf54cd50588Logan Chien  tab->owner = owner;
5061dfca9e892597db79889addb7a3abf54cd50588Logan Chien
5161dfca9e892597db79889addb7a3abf54cd50588Logan Chien  // Get ELF header
5261dfca9e892597db79889addb7a3abf54cd50588Logan Chien  ELFHeader<Bitwidth> const *header = owner->getHeader();
5361dfca9e892597db79889addb7a3abf54cd50588Logan Chien
5461dfca9e892597db79889addb7a3abf54cd50588Logan Chien  assert(header->getSectionHeaderEntrySize() >=
5561dfca9e892597db79889addb7a3abf54cd50588Logan Chien         TypeTraits<ELFSectionHeader<Bitwidth> >::size);
5661dfca9e892597db79889addb7a3abf54cd50588Logan Chien
5761dfca9e892597db79889addb7a3abf54cd50588Logan Chien  size_t pending = TypeTraits<ELFSectionHeader<Bitwidth> >::size -
5861dfca9e892597db79889addb7a3abf54cd50588Logan Chien                   header->getSectionHeaderEntrySize();
5961dfca9e892597db79889addb7a3abf54cd50588Logan Chien
6061dfca9e892597db79889addb7a3abf54cd50588Logan Chien  // Seek to the address of section header
6161dfca9e892597db79889addb7a3abf54cd50588Logan Chien  AR.seek(header->getSectionHeaderTableOffset(), true);
6261dfca9e892597db79889addb7a3abf54cd50588Logan Chien
6361dfca9e892597db79889addb7a3abf54cd50588Logan Chien  for (size_t i = 0; i < header->getSectionHeaderNum(); ++i) {
6461dfca9e892597db79889addb7a3abf54cd50588Logan Chien    boost::shared_ptr<ELFSectionHeader<Bitwidth> > sh(
6561dfca9e892597db79889addb7a3abf54cd50588Logan Chien      ELFSectionHeader<Bitwidth>::read(AR, i));
6661dfca9e892597db79889addb7a3abf54cd50588Logan Chien
6761dfca9e892597db79889addb7a3abf54cd50588Logan Chien    if (!sh) {
6861dfca9e892597db79889addb7a3abf54cd50588Logan Chien      // Something wrong while reading the section header.
6961dfca9e892597db79889addb7a3abf54cd50588Logan Chien      return boost::shared_ptr<ELFSectionHeaderTable>();
7061dfca9e892597db79889addb7a3abf54cd50588Logan Chien    }
7161dfca9e892597db79889addb7a3abf54cd50588Logan Chien
7261dfca9e892597db79889addb7a3abf54cd50588Logan Chien    AR.seek(pending);
7361dfca9e892597db79889addb7a3abf54cd50588Logan Chien    tab->table.push_back(sh);
7461dfca9e892597db79889addb7a3abf54cd50588Logan Chien  }
7561dfca9e892597db79889addb7a3abf54cd50588Logan Chien
7661dfca9e892597db79889addb7a3abf54cd50588Logan Chien  return tab;
7761dfca9e892597db79889addb7a3abf54cd50588Logan Chien}
7861dfca9e892597db79889addb7a3abf54cd50588Logan Chien
7961dfca9e892597db79889addb7a3abf54cd50588Logan Chientemplate <size_t Bitwidth>
8061dfca9e892597db79889addb7a3abf54cd50588Logan Chieninline void ELFSectionHeaderTable<Bitwidth>::print() {
8161dfca9e892597db79889addb7a3abf54cd50588Logan Chien  using namespace std;
8261dfca9e892597db79889addb7a3abf54cd50588Logan Chien  using namespace term;
8361dfca9e892597db79889addb7a3abf54cd50588Logan Chien  using namespace term::color;
8461dfca9e892597db79889addb7a3abf54cd50588Logan Chien
8561dfca9e892597db79889addb7a3abf54cd50588Logan Chien  cout << endl << setw(79) << setfill('=') << '=' << endl;
8661dfca9e892597db79889addb7a3abf54cd50588Logan Chien  cout << light::white()
8761dfca9e892597db79889addb7a3abf54cd50588Logan Chien       << "ELF Section Header Table" << normal() << endl;
8861dfca9e892597db79889addb7a3abf54cd50588Logan Chien
8961dfca9e892597db79889addb7a3abf54cd50588Logan Chien  for (size_t i = 0; i < table.size(); ++i) {
9061dfca9e892597db79889addb7a3abf54cd50588Logan Chien    table[i]->print();
9161dfca9e892597db79889addb7a3abf54cd50588Logan Chien  }
9261dfca9e892597db79889addb7a3abf54cd50588Logan Chien
9361dfca9e892597db79889addb7a3abf54cd50588Logan Chien  cout << setw(79) << setfill('=') << '=' << endl;
9461dfca9e892597db79889addb7a3abf54cd50588Logan Chien}
9561dfca9e892597db79889addb7a3abf54cd50588Logan Chien
9661dfca9e892597db79889addb7a3abf54cd50588Logan Chien#endif // ELF_SECTION_HEADER_TABLE_H
97