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_RELOC_HXX
18 #define ELF_RELOC_HXX
19
20 #include "utils/raw_ostream.h"
21
22 #include <llvm/Support/Format.h>
23 #include <llvm/Support/raw_ostream.h>
24
25 template <unsigned Bitwidth>
26 template <typename Archiver>
27 inline ELFReloc<Bitwidth> *
readRela(Archiver & AR,size_t index)28 ELFReloc_CRTP<Bitwidth>::readRela(Archiver &AR, size_t index) {
29 if (!AR) {
30 // Archiver is in bad state before calling read function.
31 // Return NULL and do nothing.
32 return 0;
33 }
34
35 llvm::OwningPtr<ELFRelocTy> sh(new ELFRelocTy());
36
37 if (!sh->serializeRela(AR)) {
38 // Unable to read the structure. Return NULL.
39 return 0;
40 }
41
42 if (!sh->isValid()) {
43 // Rel read from archiver is not valid. Return NULL.
44 return 0;
45 }
46
47 // Set the section header index
48 sh->index = index;
49
50 return sh.take();
51 }
52
53 template <unsigned Bitwidth>
54 template <typename Archiver>
55 inline ELFReloc<Bitwidth> *
readRel(Archiver & AR,size_t index)56 ELFReloc_CRTP<Bitwidth>::readRel(Archiver &AR, size_t index) {
57 if (!AR) {
58 // Archiver is in bad state before calling read function.
59 // Return NULL and do nothing.
60 return 0;
61 }
62
63 llvm::OwningPtr<ELFRelocTy> sh(new ELFRelocTy());
64
65 sh->r_addend = 0;
66 if (!sh->serializeRel(AR)) {
67 return 0;
68 }
69
70 if (!sh->isValid()) {
71 // Rel read from archiver is not valid. Return NULL.
72 return 0;
73 }
74
75 // Set the section header index
76 sh->index = index;
77
78 return sh.take();
79 }
80
81 template <unsigned Bitwidth>
print(bool shouldPrintHeader) const82 inline void ELFReloc_CRTP<Bitwidth>::print(bool shouldPrintHeader) const {
83 using namespace llvm;
84 if (shouldPrintHeader) {
85 out() << '\n' << fillformat('=', 79) << '\n';
86 out().changeColor(raw_ostream::WHITE, true);
87 out() << "ELF Relaocation Table Entry "
88 << this->getIndex() << '\n';
89 out().resetColor();
90 out() << fillformat('-', 79) << '\n';
91 } else {
92 out() << fillformat('-', 79) << '\n';
93 out().changeColor(raw_ostream::YELLOW, true);
94 out() << "ELF Relaocation Table Entry "
95 << this->getIndex() << " : " << '\n';
96 out().resetColor();
97 }
98
99 #define PRINT_LINT(title, value) \
100 out() << format(" %-13s : ", (char const *)(title)) << (value) << '\n'
101 PRINT_LINT("Offset", concrete()->getOffset() );
102 PRINT_LINT("SymTab Index", concrete()->getSymTabIndex() );
103 PRINT_LINT("Type", concrete()->getType() );
104 PRINT_LINT("Addend", concrete()->getAddend() );
105 #undef PRINT_LINT
106 }
107
108 #endif // ELF_RELOC_HXX
109