Home | History | Annotate | Download | only in impl
      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_HXX
     18 #define ELF_SECTION_HEADER_HXX
     19 
     20 #include "utils/raw_ostream.h"
     21 #include <llvm/Support/Format.h>
     22 #include <llvm/Support/raw_ostream.h>
     23 
     24 #include "ELFObject.h"
     25 
     26 template <unsigned Bitwidth>
     27 char const *ELFSectionHeader_CRTP<Bitwidth>::getName() const {
     28   return owner->getSectionName(getNameIndex());
     29 }
     30 
     31 template <unsigned Bitwidth>
     32 template <typename Archiver>
     33 typename ELFSectionHeader_CRTP<Bitwidth>::ELFSectionHeaderTy *
     34 ELFSectionHeader_CRTP<Bitwidth>::read(Archiver &AR,
     35                                       ELFObjectTy const *owner,
     36                                       size_t index) {
     37 
     38   if (!AR) {
     39     // Archiver is in bad state before calling read function.
     40     // Return NULL and do nothing.
     41     return 0;
     42   }
     43 
     44   llvm::OwningPtr<ELFSectionHeaderTy> sh(new ELFSectionHeaderTy());
     45 
     46   if (!sh->serialize(AR)) {
     47     // Unable to read the structure.  Return NULL.
     48     return 0;
     49   }
     50 
     51   if (!sh->isValid()) {
     52     // Header read from archiver is not valid.  Return NULL.
     53     return 0;
     54   }
     55 
     56   // Set the section header index
     57   sh->index = index;
     58 
     59   // Set the owner elf object
     60   sh->owner = owner;
     61 
     62   return sh.take();
     63 }
     64 
     65 template <unsigned Bitwidth>
     66 void ELFSectionHeader_CRTP<Bitwidth>::print(bool shouldPrintHeader) const {
     67   using namespace llvm;
     68 
     69   if (shouldPrintHeader) {
     70     out() << '\n' << fillformat('=', 79) << '\n';
     71     out().changeColor(raw_ostream::WHITE, true);
     72     out() << "ELF Section Header "
     73       << this->getIndex() << '\n';
     74     out().resetColor();
     75     out() << fillformat('-', 79) << '\n';
     76   } else {
     77     out() << fillformat('-', 79) << '\n';
     78     out().changeColor(raw_ostream::YELLOW, true);
     79     out() << "ELF Section Header "
     80       << this->getIndex() << " : " << '\n';
     81     out().resetColor();
     82   }
     83 
     84 #define PRINT_LINT(title, value) \
     85   out() << format("  %-13s : ", (char const *)(title)) << (value) << '\n'
     86   PRINT_LINT("Name",          getName() );
     87   PRINT_LINT("Type",          getSectionTypeStr(getType()));
     88   PRINT_LINT("Flags",         concrete()->getFlags());
     89   PRINT_LINT("Address",       getAddress());
     90   PRINT_LINT("Offset",        getOffset());
     91   PRINT_LINT("Size",          concrete()->getSize());
     92   PRINT_LINT("Link",          getLink());
     93   PRINT_LINT("Extra Info",    getExtraInfo());
     94   PRINT_LINT("Address Align", concrete()->getAddressAlign());
     95   PRINT_LINT("Entry Size",    concrete()->getEntrySize());
     96 #undef PRINT_LINT
     97 
     98   if (shouldPrintHeader) {
     99     out() << fillformat('=', 79) << '\n';
    100   }
    101 }
    102 
    103 #endif // ELF_SECTION_HEADER_HXX
    104