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_OBJECT_H
     18 #define ELF_OBJECT_H
     19 
     20 #include "ELFTypes.h"
     21 #include "MemChunk.h"
     22 
     23 #include "utils/rsl_assert.h"
     24 
     25 #include <llvm/ADT/OwningPtr.h>
     26 
     27 #include <string>
     28 #include <vector>
     29 
     30 template <unsigned Bitwidth>
     31 class ELFObject {
     32 public:
     33   ELF_TYPE_INTRO_TO_TEMPLATE_SCOPE(Bitwidth);
     34 
     35 private:
     36   llvm::OwningPtr<ELFHeaderTy> header;
     37   llvm::OwningPtr<ELFSectionHeaderTableTy> shtab;
     38   std::vector<ELFSectionTy *> stab;
     39 
     40   MemChunk SHNCommonData;
     41   unsigned char *SHNCommonDataPtr;
     42   size_t SHNCommonDataFreeSize;
     43 
     44   // TODO: Need refactor!
     45   bool initSHNCommonDataSize(size_t SHNCommonDataSize) {
     46     rsl_assert(!SHNCommonDataPtr && "Can't init twice.");
     47     if (!SHNCommonData.allocate(SHNCommonDataSize)) {
     48       return false;
     49     }
     50 
     51     SHNCommonDataPtr = SHNCommonData.getBuffer();
     52     SHNCommonDataFreeSize = SHNCommonDataSize;
     53     return true;
     54   }
     55 
     56 private:
     57   ELFObject() : SHNCommonDataPtr(NULL) { }
     58 
     59 public:
     60   template <typename Archiver>
     61   static ELFObject *read(Archiver &AR);
     62 
     63   ELFHeaderTy const *getHeader() const {
     64     return header.get();
     65   }
     66 
     67   ELFSectionHeaderTableTy const *getSectionHeaderTable() const {
     68     return shtab.get();
     69   }
     70 
     71   char const *getSectionName(size_t i) const;
     72   ELFSectionTy const *getSectionByIndex(size_t i) const;
     73   ELFSectionTy *getSectionByIndex(size_t i);
     74   ELFSectionTy const *getSectionByName(std::string const &str) const;
     75   ELFSectionTy *getSectionByName(std::string const &str);
     76 
     77   void *allocateSHNCommonData(size_t size, size_t align = 1) {
     78     rsl_assert(size > 0 && align != 0);
     79 
     80     rsl_assert(SHNCommonDataPtr && "Must init common data size before use!");
     81 
     82     // Ensure alignment
     83     size_t rem = ((uintptr_t)SHNCommonDataPtr) % align;
     84     if (rem != 0) {
     85       SHNCommonDataPtr += align - rem;
     86       SHNCommonDataFreeSize -= align - rem;
     87     }
     88 
     89     // Ensure the free size is sufficient
     90     if (SHNCommonDataFreeSize < size) {
     91       return NULL;
     92     }
     93 
     94     // Allcoate
     95     void *result = SHNCommonDataPtr;
     96     SHNCommonDataPtr += size;
     97     SHNCommonDataFreeSize -= size;
     98 
     99     return result;
    100   }
    101 
    102   void relocate(void *(*find_sym)(void *context, char const *name),
    103                 void *context);
    104 
    105   void print() const;
    106 
    107   ~ELFObject() {
    108     for (size_t i = 0; i < stab.size(); ++i) {
    109       // Delete will check the pointer is nullptr or not by himself.
    110       delete stab[i];
    111     }
    112   }
    113 
    114 private:
    115   void relocateARM(void *(*find_sym)(void *context, char const *name),
    116                    void *context,
    117                    ELFSectionRelTableTy *reltab,
    118                    ELFSectionProgBitsTy *text);
    119 
    120   void relocateX86_32(void *(*find_sym)(void *context, char const *name),
    121                       void *context,
    122                       ELFSectionRelTableTy *reltab,
    123                       ELFSectionProgBitsTy *text);
    124 
    125   void relocateX86_64(void *(*find_sym)(void *context, char const *name),
    126                       void *context,
    127                       ELFSectionRelTableTy *reltab,
    128                       ELFSectionProgBitsTy *text);
    129 
    130   void relocateMIPS(void *(*find_sym)(void *context, char const *name),
    131                     void *context,
    132                     ELFSectionRelTableTy *reltab,
    133                     ELFSectionProgBitsTy *text);
    134 };
    135 
    136 #include "impl/ELFObject.hxx"
    137 
    138 #endif // ELF_OBJECT_H
    139