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_PROGBITS_HXX
     18 #define ELF_SECTION_PROGBITS_HXX
     19 
     20 #include "ELFTypes.h"
     21 #include "StubLayout.h"
     22 
     23 #include <llvm/Support/Format.h>
     24 #include <llvm/Support/raw_ostream.h>
     25 
     26 #include "utils/raw_ostream.h"
     27 
     28 #include <string.h>
     29 
     30 template <unsigned Bitwidth>
     31 template <typename Archiver>
     32 ELFSectionProgBits<Bitwidth> *
     33 ELFSectionProgBits<Bitwidth>::read(Archiver &AR,
     34                                    ELFObjectTy *owner,
     35                                    ELFSectionHeaderTy const *sh) {
     36   int machine = owner->getHeader()->getMachine();
     37   ELFSectionProgBits *secp = new ELFSectionProgBits(machine);
     38   llvm::OwningPtr<ELFSectionProgBits> result(secp);
     39   size_t max_num_stubs = 0;
     40   // Align section boundary to 4 bytes.
     41   size_t section_size = (sh->getSize() + 3) / 4 * 4;
     42   size_t alloc_size = section_size;
     43   StubLayout *stubs = result->getStubLayout();
     44   if (stubs) {
     45     // Compute the maximal possible numbers of stubs
     46     std::string reltab_name(".rel" + std::string(sh->getName()));
     47 
     48     ELFSectionRelTableTy const *reltab =
     49       static_cast<ELFSectionRelTableTy *>(
     50         owner->getSectionByName(reltab_name.c_str()));
     51 
     52     if (reltab) {
     53       // If we have relocation table, then get the approximation of
     54       // maximum numbers of stubs.
     55       max_num_stubs = reltab->getMaxNumStubs(owner);
     56     }
     57 
     58     // Compute the stub table size
     59     size_t stub_table_size = stubs->calcStubTableSize(max_num_stubs);
     60 
     61     // Allocate PROGBITS section with stubs table
     62     alloc_size += stub_table_size;
     63   }
     64 
     65   // Allocate text section
     66   if (!result->chunk.allocate(alloc_size)) {
     67     return NULL;
     68   }
     69 
     70   if (stubs) {
     71     stubs->initStubTable(result->chunk.getBuffer() + section_size,
     72                          max_num_stubs);
     73   }
     74 
     75   result->sh = sh;
     76 
     77   if (!result->serialize(AR)) {
     78     // Unable to read the progbits section.
     79     return NULL;
     80   }
     81 
     82   return result.take();
     83 }
     84 
     85 #endif // ELF_SECTION_PROGBITS_HXX
     86