/* * Copyright 2011, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ELF_SECTION_HEADER_TABLE_HXX #define ELF_SECTION_HEADER_TABLE_HXX #include "ELFHeader.h" #include "ELFObject.h" #include "ELFSectionHeader.h" #include "utils/rsl_assert.h" template ELFSectionHeaderTable::~ELFSectionHeaderTable() { for (size_t i = 0; i < table.size(); ++i) { delete table[i]; } } template template inline ELFSectionHeaderTable * ELFSectionHeaderTable::read(Archiver &AR, ELFObjectTy *owner) { if (!AR) { // Archiver is in bad state before calling read function. // Return NULL and do nothing. return 0; } // Allocate a new section header table and assign the owner. llvm::OwningPtr tab(new ELFSectionHeaderTable()); // Get ELF header ELFHeaderTy const *header = owner->getHeader(); rsl_assert(header->getSectionHeaderEntrySize() == TypeTraits::size); // Seek to the address of section header AR.seek(header->getSectionHeaderTableOffset(), true); for (size_t i = 0; i < header->getSectionHeaderNum(); ++i) { llvm::OwningPtr sh( ELFSectionHeaderTy::read(AR, owner, i)); if (!sh) { // Something wrong while reading the section header. return 0; } tab->table.push_back(sh.take()); } return tab.take(); } template inline void ELFSectionHeaderTable::print() const { using namespace llvm; out() << '\n' << fillformat('=', 79) << '\n'; out().changeColor(raw_ostream::WHITE, true); out() << "ELF Section Header Table" << '\n'; out().resetColor(); for (size_t i = 0; i < table.size(); ++i) { (*this)[i]->print(); } out() << fillformat('=', 79) << '\n'; } template inline void ELFSectionHeaderTable::buildNameMap() { for (size_t i = 0; i < table.size(); ++i) { ELFSectionHeaderTy *sh = table[i]; if ( sh ) { name_map[sh->getName()] = sh; } } } template inline ELFSectionHeader const * ELFSectionHeaderTable::getByName(const std::string &name) const { typename llvm::StringMap::const_iterator sh = name_map.find(name); if (sh == name_map.end()) { // Return SHN_UNDEF section header; return table[0]; } return sh->getValue(); } template inline ELFSectionHeader * ELFSectionHeaderTable::getByName(const std::string &name) { ELFSectionHeaderTableTy const *const_this = this; ELFSectionHeaderTy const *shptr = const_this->getByName(name); // Const cast for the same API's const and non-const versions. return const_cast(shptr); } #endif // ELF_SECTION_HEADER_TABLE_HXX