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_REL_TABLE_HXX
18#define ELF_SECTION_REL_TABLE_HXX
19
20#include "ELF.h"
21#include "ELFHeader.h"
22#include "ELFObject.h"
23#include "ELFReloc.h"
24#include "ELFTypes.h"
25
26#include <set>
27
28template <unsigned Bitwidth>
29ELFSectionRelTable<Bitwidth>::~ELFSectionRelTable() {
30  using namespace std;
31  for (size_t i = 0; i < table.size(); ++i) {
32    delete table[i];
33  }
34}
35
36template <unsigned Bitwidth>
37void ELFSectionRelTable<Bitwidth>::print() const {
38  using namespace llvm;
39
40  out() << '\n' << fillformat('=', 79) << '\n';
41  out().changeColor(raw_ostream::WHITE, true);
42  out() << "Relocation Table" << '\n';
43  out().resetColor();
44
45  for (size_t i = 0; i < this->size(); ++i) {
46    (*this)[i]->print();
47  }
48
49  out() << fillformat('=', 79) << '\n';
50}
51
52template <unsigned Bitwidth>
53template <typename Archiver>
54ELFSectionRelTable<Bitwidth> *
55ELFSectionRelTable<Bitwidth>::read(Archiver &AR,
56                                   ELFSectionHeaderTy const *sh) {
57
58  rsl_assert(sh->getType() == SHT_REL || sh->getType() == SHT_RELA);
59
60  llvm::OwningPtr<ELFSectionRelTable> rt(new ELFSectionRelTable());
61
62  // Seek to the start of the table
63  AR.seek(sh->getOffset(), true);
64
65  // Count the relocation entries
66  size_t size = sh->getSize() / sh->getEntrySize();
67
68  // Read every relocation entries
69  if (sh->getType() == SHT_REL) {
70    rsl_assert(sh->getEntrySize() == TypeTraits<ELFRelocRelTy>::size);
71    for (size_t i = 0; i < size; ++i) {
72      rt->table.push_back(ELFRelocTy::readRel(AR, i));
73    }
74
75  } else {
76    rsl_assert(sh->getEntrySize() == TypeTraits<ELFRelocRelaTy>::size);
77    for (size_t i = 0; i < size; ++i) {
78      rt->table.push_back(ELFRelocTy::readRela(AR, i));
79    }
80  }
81
82  if (!AR) {
83    // Unable to read the table.
84    return 0;
85  }
86
87  return rt.take();
88}
89
90template <unsigned Bitwidth>
91size_t ELFSectionRelTable<Bitwidth>::
92getMaxNumStubs(ELFObjectTy const *obj) const {
93  switch (obj->getHeader()->getMachine()) {
94  case EM_ARM:
95    {
96      std::set<uint32_t> sym_index_set;
97
98      for (size_t i = 0; i < size(); ++i) {
99        ELFRelocTy *rel = table[i];
100
101        if (rel->getType() == R_ARM_CALL ||
102            rel->getType() == R_ARM_THM_CALL) {
103          sym_index_set.insert(rel->getSymTabIndex());
104        }
105      }
106
107      return sym_index_set.size();
108    }
109
110  case EM_MIPS:
111    {
112      std::set<uint32_t> sym_index_set;
113
114      for (size_t i = 0; i < size(); ++i) {
115        ELFRelocTy *rel = table[i];
116
117        if (rel->getType() == R_MIPS_26) {
118          sym_index_set.insert(rel->getSymTabIndex());
119        }
120      }
121
122      return sym_index_set.size();
123    }
124
125  case EM_386:
126  case EM_X86_64:
127    return 0;
128
129  default:
130    rsl_assert(0 && "Only support ARM, MIPS, X86, and X86_64 relocation.");
131    return 0;
132  }
133}
134
135#endif // ELF_SECTION_REL_TABLE_HXX
136