]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/llvm/include/llvm/Object/RelocVisitor.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / llvm / include / llvm / Object / RelocVisitor.h
1 //===-- RelocVisitor.h - Visitor for object file relocations -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides a wrapper around all the different types of relocations
11 // in different file formats, such that a client can handle them in a unified
12 // manner by only implementing a minimal number of functions.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_OBJECT_RELOCVISITOR_H
17 #define LLVM_OBJECT_RELOCVISITOR_H
18
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ELF.h"
23 #include "llvm/Support/raw_ostream.h"
24
25 namespace llvm {
26 namespace object {
27
28 struct RelocToApply {
29   // The computed value after applying the relevant relocations.
30   int64_t Value;
31
32   // The width of the value; how many bytes to touch when applying the
33   // relocation.
34   char Width;
35   RelocToApply(const RelocToApply &In) : Value(In.Value), Width(In.Width) {}
36   RelocToApply(int64_t Value, char Width) : Value(Value), Width(Width) {}
37   RelocToApply() : Value(0), Width(0) {}
38 };
39
40 /// @brief Base class for object file relocation visitors.
41 class RelocVisitor {
42 public:
43   explicit RelocVisitor(StringRef FileFormat)
44     : FileFormat(FileFormat), HasError(false) {}
45
46   // TODO: Should handle multiple applied relocations via either passing in the
47   // previously computed value or just count paired relocations as a single
48   // visit.
49   RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t SecAddr = 0,
50                      uint64_t Value = 0) {
51     if (FileFormat == "ELF64-x86-64") {
52       switch (RelocType) {
53         case llvm::ELF::R_X86_64_NONE:
54           return visitELF_X86_64_NONE(R);
55         case llvm::ELF::R_X86_64_64:
56           return visitELF_X86_64_64(R, Value);
57         case llvm::ELF::R_X86_64_PC32:
58           return visitELF_X86_64_PC32(R, Value, SecAddr);
59         case llvm::ELF::R_X86_64_32:
60           return visitELF_X86_64_32(R, Value);
61         case llvm::ELF::R_X86_64_32S:
62           return visitELF_X86_64_32S(R, Value);
63         default:
64           HasError = true;
65           return RelocToApply();
66       }
67     } else if (FileFormat == "ELF32-i386") {
68       switch (RelocType) {
69       case llvm::ELF::R_386_NONE:
70         return visitELF_386_NONE(R);
71       case llvm::ELF::R_386_32:
72         return visitELF_386_32(R, Value);
73       case llvm::ELF::R_386_PC32:
74         return visitELF_386_PC32(R, Value, SecAddr);
75       default:
76         HasError = true;
77         return RelocToApply();
78       }
79     } else if (FileFormat == "ELF64-ppc64") {
80       switch (RelocType) {
81       case llvm::ELF::R_PPC64_ADDR32:
82         return visitELF_PPC64_ADDR32(R, Value);
83       default:
84         HasError = true;
85         return RelocToApply();
86       }
87     } else if (FileFormat == "ELF32-mips") {
88       switch (RelocType) {
89       case llvm::ELF::R_MIPS_32:
90         return visitELF_MIPS_32(R, Value);
91       default:
92         HasError = true;
93         return RelocToApply();
94       }
95     } else if (FileFormat == "ELF64-aarch64") {
96       switch (RelocType) {
97       case llvm::ELF::R_AARCH64_ABS32:
98         return visitELF_AARCH64_ABS32(R, Value);
99       case llvm::ELF::R_AARCH64_ABS64:
100         return visitELF_AARCH64_ABS64(R, Value);
101       default:
102         HasError = true;
103         return RelocToApply();
104       }
105     } else if (FileFormat == "ELF64-s390") {
106       switch (RelocType) {
107       case llvm::ELF::R_390_32:
108         return visitELF_390_32(R, Value);
109       case llvm::ELF::R_390_64:
110         return visitELF_390_64(R, Value);
111       default:
112         HasError = true;
113         return RelocToApply();
114       }
115     }
116     HasError = true;
117     return RelocToApply();
118   }
119
120   bool error() { return HasError; }
121
122 private:
123   StringRef FileFormat;
124   bool HasError;
125
126   /// Operations
127
128   /// 386-ELF
129   RelocToApply visitELF_386_NONE(RelocationRef R) {
130     return RelocToApply(0, 0);
131   }
132
133   // Ideally the Addend here will be the addend in the data for
134   // the relocation. It's not actually the case for Rel relocations.
135   RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
136     int64_t Addend;
137     R.getAdditionalInfo(Addend);
138     return RelocToApply(Value + Addend, 4);
139   }
140
141   RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
142                                  uint64_t SecAddr) {
143     int64_t Addend;
144     R.getAdditionalInfo(Addend);
145     uint64_t Address;
146     R.getOffset(Address);
147     return RelocToApply(Value + Addend - Address, 4);
148   }
149
150   /// X86-64 ELF
151   RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
152     return RelocToApply(0, 0);
153   }
154   RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
155     int64_t Addend;
156     R.getAdditionalInfo(Addend);
157     return RelocToApply(Value + Addend, 8);
158   }
159   RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
160                                     uint64_t SecAddr) {
161     int64_t Addend;
162     R.getAdditionalInfo(Addend);
163     uint64_t Address;
164     R.getOffset(Address);
165     return RelocToApply(Value + Addend - Address, 4);
166   }
167   RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
168     int64_t Addend;
169     R.getAdditionalInfo(Addend);
170     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
171     return RelocToApply(Res, 4);
172   }
173   RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
174     int64_t Addend;
175     R.getAdditionalInfo(Addend);
176     int32_t Res = (Value + Addend) & 0xFFFFFFFF;
177     return RelocToApply(Res, 4);
178   }
179
180   /// PPC64 ELF
181   RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
182     int64_t Addend;
183     R.getAdditionalInfo(Addend);
184     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
185     return RelocToApply(Res, 4);
186   }
187
188   /// MIPS ELF
189   RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
190     int64_t Addend;
191     R.getAdditionalInfo(Addend);
192     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
193     return RelocToApply(Res, 4);
194   }
195
196   // AArch64 ELF
197   RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
198     int64_t Addend;
199     R.getAdditionalInfo(Addend);
200     int64_t Res =  Value + Addend;
201
202     // Overflow check allows for both signed and unsigned interpretation.
203     if (Res < INT32_MIN || Res > UINT32_MAX)
204       HasError = true;
205
206     return RelocToApply(static_cast<uint32_t>(Res), 4);
207   }
208
209   RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
210     int64_t Addend;
211     R.getAdditionalInfo(Addend);
212     return RelocToApply(Value + Addend, 8);
213   }
214
215   // SystemZ ELF
216   RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
217     int64_t Addend;
218     R.getAdditionalInfo(Addend);
219     int64_t Res = Value + Addend;
220
221     // Overflow check allows for both signed and unsigned interpretation.
222     if (Res < INT32_MIN || Res > UINT32_MAX)
223       HasError = true;
224
225     return RelocToApply(static_cast<uint32_t>(Res), 4);
226   }
227
228   RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
229     int64_t Addend;
230     R.getAdditionalInfo(Addend);
231     return RelocToApply(Value + Addend, 8);
232   }
233 };
234
235 }
236 }
237 #endif