ELFSectionSymTab.hxx revision ee6cdb95525abc8c7766798148302306a100b774
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_SYM_TAB_HXX
18#define ELF_SECTION_SYM_TAB_HXX
19
20#include "ELFSectionHeader.h"
21#include "ELFSymbol.h"
22#include "utils/rsl_assert.h"
23
24template <unsigned Bitwidth>
25ELFSectionSymTab<Bitwidth>::~ELFSectionSymTab() {
26  for (size_t i = 0; i < table.size(); ++i) {
27    delete table[i];
28  }
29}
30
31template <unsigned Bitwidth>
32size_t ELFSectionSymTab<Bitwidth>::getExternFuncCount() const {
33  size_t result = 0;
34  for (size_t i = 0; i < table.size(); ++i) {
35    if (table[i] && table[i]->isExternFunc()) {
36      result++;
37    }
38  }
39  return result;
40}
41
42template <unsigned Bitwidth>
43template <typename Archiver>
44ELFSectionSymTab<Bitwidth> *
45ELFSectionSymTab<Bitwidth>::read(Archiver &AR,
46                                 ELFObjectTy *owner,
47                                 ELFSectionHeaderTy const *sh) {
48
49  llvm::OwningPtr<ELFSectionSymTabTy> st(new ELFSectionSymTabTy());
50
51  // Assert that entry size will be the same as standard.
52  rsl_assert(sh->getEntrySize() == TypeTraits<ELFSymbolTy>::size);
53
54  // Seek to the start of symbol table
55  AR.seek(sh->getOffset(), true);
56
57  // Read all symbol table entry
58  size_t size = sh->getSize() / sh->getEntrySize();
59  for (size_t i = 0; i < size; ++i) {
60    st->table.push_back(ELFSymbolTy::read(AR, owner, i));
61  }
62
63  if (!AR) {
64    // Unable to read the table.
65    return 0;
66  }
67
68  return st.take();
69}
70
71template <unsigned Bitwidth>
72void ELFSectionSymTab<Bitwidth>::print() const {
73  using namespace llvm;
74
75  out() << '\n' << fillformat('=', 79) << '\n';
76  out().changeColor(raw_ostream::WHITE, true);
77  out() << "Symbol Table" << '\n';
78  out().resetColor();
79
80  for (size_t i = 0; i < table.size(); ++i) {
81    table[i]->print();
82  }
83
84  out() << fillformat('=', 79) << '\n';
85}
86
87#endif // ELF_SECTION_SYM_TAB_HXX
88