ELFSectionHeaderTable.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_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 llvm::OwningPtr<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 llvm::OwningPtr<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.take()); 65 } 66 67 return tab.take(); 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 ELFSectionHeader<Bitwidth> const * 88ELFSectionHeaderTable<Bitwidth>::getByName(const std::string &str) const { 89 // TODO: Use map 90 for (size_t i = 0; i < table.size(); ++i) { 91 if (str == std::string(table[i]->getName())) { 92 return table[i]; 93 } 94 } 95 // Return SHN_UNDEF section header; 96 return table[0]; 97} 98 99template <unsigned Bitwidth> 100inline ELFSectionHeader<Bitwidth> * 101ELFSectionHeaderTable<Bitwidth>::getByName(const std::string &str) { 102 ELFSectionHeaderTableTy const *const_this = this; 103 ELFSectionHeaderTy const *shptr = const_this->getByName(str); 104 // Const cast for the same API's const and non-const versions. 105 return const_cast<ELFSectionHeaderTy *>(shptr); 106} 107 108#endif // ELF_SECTION_HEADER_TABLE_HXX 109