1/*
2 * Copyright 2011, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ELF_SECTION_HEADER_TABLE_HXX
18#define ELF_SECTION_HEADER_TABLE_HXX
19
20#include "ELFHeader.h"
21#include "ELFObject.h"
22#include "ELFSectionHeader.h"
23
24#include "utils/rsl_assert.h"
25
26template <unsigned Bitwidth>
27ELFSectionHeaderTable<Bitwidth>::~ELFSectionHeaderTable() {
28  for (size_t i = 0; i < table.size(); ++i) {
29    delete table[i];
30  }
31}
32
33template <unsigned Bitwidth>
34template <typename Archiver>
35inline ELFSectionHeaderTable<Bitwidth> *
36ELFSectionHeaderTable<Bitwidth>::read(Archiver &AR, ELFObjectTy *owner) {
37  if (!AR) {
38    // Archiver is in bad state before calling read function.
39    // Return NULL and do nothing.
40    return 0;
41  }
42
43  // Allocate a new section header table and assign the owner.
44  std::unique_ptr<ELFSectionHeaderTable> tab(new ELFSectionHeaderTable());
45
46  // Get ELF header
47  ELFHeaderTy const *header = owner->getHeader();
48
49  rsl_assert(header->getSectionHeaderEntrySize() ==
50         TypeTraits<ELFSectionHeaderTy>::size);
51
52  // Seek to the address of section header
53  AR.seek(header->getSectionHeaderTableOffset(), true);
54
55  for (size_t i = 0; i < header->getSectionHeaderNum(); ++i) {
56    std::unique_ptr<ELFSectionHeaderTy> sh(
57      ELFSectionHeaderTy::read(AR, owner, i));
58
59    if (!sh) {
60      // Something wrong while reading the section header.
61      return 0;
62    }
63
64    tab->table.push_back(sh.release());
65  }
66
67  return tab.release();
68}
69
70template <unsigned Bitwidth>
71inline void ELFSectionHeaderTable<Bitwidth>::print() const {
72  using namespace llvm;
73
74  out() << '\n' << fillformat('=', 79) << '\n';
75  out().changeColor(raw_ostream::WHITE, true);
76  out() << "ELF Section Header Table" << '\n';
77  out().resetColor();
78
79  for (size_t i = 0; i < table.size(); ++i) {
80    (*this)[i]->print();
81  }
82
83  out() << fillformat('=', 79) << '\n';
84}
85
86template <unsigned Bitwidth>
87inline void ELFSectionHeaderTable<Bitwidth>::buildNameMap() {
88  for (size_t i = 0; i < table.size(); ++i) {
89    ELFSectionHeaderTy *sh = table[i];
90    if ( sh ) {
91      name_map[sh->getName()] = sh;
92    }
93  }
94}
95
96template <unsigned Bitwidth>
97inline ELFSectionHeader<Bitwidth> const *
98ELFSectionHeaderTable<Bitwidth>::getByName(const std::string &name) const {
99  typename llvm::StringMap<ELFSectionHeaderTy *>::const_iterator sh =
100    name_map.find(name);
101  if (sh == name_map.end()) {
102    // Return SHN_UNDEF section header;
103    return table[0];
104  }
105  return sh->getValue();
106}
107
108template <unsigned Bitwidth>
109inline ELFSectionHeader<Bitwidth> *
110ELFSectionHeaderTable<Bitwidth>::getByName(const std::string &name) {
111  ELFSectionHeaderTableTy const *const_this = this;
112  ELFSectionHeaderTy const *shptr = const_this->getByName(name);
113  // Const cast for the same API's const and non-const versions.
114  return const_cast<ELFSectionHeaderTy *>(shptr);
115}
116
117#endif // ELF_SECTION_HEADER_TABLE_HXX
118