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> *
read(Archiver & AR,ELFObjectTy * owner,ELFSectionHeaderTy const * sh)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   std::unique_ptr<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     max_num_stubs = 0;
47     for (const char* prefix : {".rel", ".rela"}) {
48       std::string reltab_name(prefix + std::string(sh->getName()));
49 
50       ELFSectionRelTableTy const *reltab =
51         static_cast<ELFSectionRelTableTy *>(
52           owner->getSectionByName(reltab_name.c_str()));
53 
54       if (reltab) {
55         // If we have relocation table, then get the approximation of
56         // maximum numbers of stubs.
57         max_num_stubs += reltab->getMaxNumStubs(owner);
58       }
59     }
60 
61     // Compute the stub table size
62     size_t stub_table_size = stubs->calcStubTableSize(max_num_stubs);
63 
64     // Allocate PROGBITS section with stubs table
65     alloc_size += stub_table_size;
66   }
67 
68   // Allocate text section
69   if (!result->chunk.allocate(alloc_size)) {
70     return NULL;
71   }
72 
73   if (stubs) {
74     stubs->initStubTable(result->chunk.getBuffer() + section_size,
75                          max_num_stubs);
76   }
77 
78   result->sh = sh;
79 
80   if (!result->serialize(AR)) {
81     // Unable to read the progbits section.
82     return NULL;
83   }
84 
85   return result.release();
86 }
87 
88 #endif // ELF_SECTION_PROGBITS_HXX
89