Home | History | Annotate | Download | only in include
      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_H
     18 #define ELF_SECTION_HEADER_H
     19 
     20 #include "ELFTypes.h"
     21 
     22 #include <llvm/ADT/OwningPtr.h>
     23 #include <stdint.h>
     24 
     25 class ELFSectionHeaderHelperMixin {
     26 protected:
     27   static char const *getSectionTypeStr(uint32_t type);
     28 };
     29 
     30 template <unsigned Bitwidth>
     31 class ELFSectionHeader_CRTP : private ELFSectionHeaderHelperMixin {
     32 public:
     33   ELF_TYPE_INTRO_TO_TEMPLATE_SCOPE(Bitwidth);
     34 
     35 protected:
     36   ELFObjectTy const *owner;
     37 
     38   size_t index;
     39 
     40   word_t sh_name;
     41   word_t sh_type;
     42   addr_t sh_addr;
     43   offset_t sh_offset;
     44   word_t sh_link;
     45   word_t sh_info;
     46 
     47 protected:
     48   ELFSectionHeader_CRTP() { }
     49   ~ELFSectionHeader_CRTP() { }
     50 
     51 public:
     52   size_t getIndex() const {
     53     return index;
     54   }
     55 
     56   word_t getNameIndex() const {
     57     return sh_name;
     58   }
     59 
     60   char const *getName() const;
     61 
     62   word_t getType() const {
     63     return sh_type;
     64   }
     65 
     66   addr_t getAddress() const {
     67     return sh_addr;
     68   }
     69 
     70   offset_t getOffset() const {
     71     return sh_offset;
     72   }
     73 
     74   word_t getLink() const {
     75     return sh_link;
     76   }
     77 
     78   word_t getExtraInfo() const {
     79     return sh_info;
     80   }
     81 
     82   bool isValid() const {
     83     // FIXME: Should check the correctness of the section header.
     84     return true;
     85   }
     86 
     87   template <typename Archiver>
     88   static ELFSectionHeaderTy *
     89   read(Archiver &AR, ELFObjectTy const *owner, size_t index = 0);
     90 
     91   void print(bool shouldPrintHeader = false) const;
     92 
     93 private:
     94   ELFSectionHeaderTy *concrete() {
     95     return static_cast<ELFSectionHeaderTy *>(this);
     96   }
     97 
     98   ELFSectionHeaderTy const *concrete() const {
     99     return static_cast<ELFSectionHeaderTy const *>(this);
    100   }
    101 };
    102 
    103 
    104 #include "impl/ELFSectionHeader.hxx"
    105 
    106 template <>
    107 class ELFSectionHeader<32> : public ELFSectionHeader_CRTP<32> {
    108   friend class ELFSectionHeader_CRTP<32>;
    109 
    110 private:
    111   word_t sh_flags;
    112   word_t sh_size;
    113   word_t sh_addralign;
    114   word_t sh_entsize;
    115 
    116 private:
    117   ELFSectionHeader() {
    118   }
    119 
    120   template <typename Archiver>
    121   bool serialize(Archiver &AR) {
    122     AR.prologue(TypeTraits<ELFSectionHeader>::size);
    123 
    124     AR & sh_name;
    125     AR & sh_type;
    126     AR & sh_flags;
    127     AR & sh_addr;
    128     AR & sh_offset;
    129     AR & sh_size;
    130     AR & sh_link;
    131     AR & sh_info;
    132     AR & sh_addralign;
    133     AR & sh_entsize;
    134 
    135     AR.epilogue(TypeTraits<ELFSectionHeader>::size);
    136     return AR;
    137   }
    138 
    139 public:
    140   word_t getFlags() const {
    141     return sh_flags;
    142   }
    143 
    144   word_t getSize() const {
    145     return sh_size;
    146   }
    147 
    148   word_t getAddressAlign() const {
    149     return sh_addralign;
    150   }
    151 
    152   word_t getEntrySize() const {
    153     return sh_entsize;
    154   }
    155 };
    156 
    157 template <>
    158 class ELFSectionHeader<64> : public ELFSectionHeader_CRTP<64> {
    159   friend class ELFSectionHeader_CRTP<64>;
    160 
    161 private:
    162   xword_t sh_flags;
    163   xword_t sh_size;
    164   xword_t sh_addralign;
    165   xword_t sh_entsize;
    166 
    167 private:
    168   ELFSectionHeader() {
    169   }
    170 
    171   template <typename Archiver>
    172   bool serialize(Archiver &AR) {
    173     AR.prologue(TypeTraits<ELFSectionHeader>::size);
    174 
    175     AR & sh_name;
    176     AR & sh_type;
    177     AR & sh_flags;
    178     AR & sh_addr;
    179     AR & sh_offset;
    180     AR & sh_size;
    181     AR & sh_link;
    182     AR & sh_info;
    183     AR & sh_addralign;
    184     AR & sh_entsize;
    185 
    186     AR.epilogue(TypeTraits<ELFSectionHeader>::size);
    187     return AR;
    188   }
    189 
    190 public:
    191   xword_t getFlags() const {
    192     return sh_flags;
    193   }
    194 
    195   xword_t getSize() const {
    196     return sh_size;
    197   }
    198 
    199   xword_t getAddressAlign() const {
    200     return sh_addralign;
    201   }
    202 
    203   xword_t getEntrySize() const {
    204     return sh_entsize;
    205   }
    206 };
    207 
    208 #endif // ELF_SECTION_HEADER_H
    209