Refactor RelocVisitor to take an object. This removes some
[oota-llvm.git] / 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/ELFObjectFile.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ELF.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 namespace llvm {
27 namespace object {
28
29 struct RelocToApply {
30   // The computed value after applying the relevant relocations.
31   int64_t Value;
32
33   // The width of the value; how many bytes to touch when applying the
34   // relocation.
35   char 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(ObjectFile &Obj)
44     : ObjToVisit(Obj), 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 (ObjToVisit.getBytesInAddress() == 8) { // 64-bit object file
52       switch (ObjToVisit.getArch()) {
53       case Triple::x86_64:
54         switch (RelocType) {
55         case llvm::ELF::R_X86_64_NONE:
56           return visitELF_X86_64_NONE(R);
57         case llvm::ELF::R_X86_64_64:
58           return visitELF_X86_64_64(R, Value);
59         case llvm::ELF::R_X86_64_PC32:
60           return visitELF_X86_64_PC32(R, Value, SecAddr);
61         case llvm::ELF::R_X86_64_32:
62           return visitELF_X86_64_32(R, Value);
63         case llvm::ELF::R_X86_64_32S:
64           return visitELF_X86_64_32S(R, Value);
65         default:
66           HasError = true;
67           return RelocToApply();
68         }
69       case Triple::aarch64:
70         switch (RelocType) {
71         case llvm::ELF::R_AARCH64_ABS32:
72           return visitELF_AARCH64_ABS32(R, Value);
73         case llvm::ELF::R_AARCH64_ABS64:
74           return visitELF_AARCH64_ABS64(R, Value);
75         default:
76           HasError = true;
77           return RelocToApply();
78         }
79       case Triple::mips64el:
80       case Triple::mips64:
81         switch (RelocType) {
82         case llvm::ELF::R_MIPS_32:
83           return visitELF_MIPS_32(R, Value);
84         case llvm::ELF::R_MIPS_64:
85           return visitELF_MIPS_64(R, Value);
86         default:
87           HasError = true;
88           return RelocToApply();
89         }
90       case Triple::ppc64le:
91       case Triple::ppc64:
92         switch (RelocType) {
93         case llvm::ELF::R_PPC64_ADDR32:
94           return visitELF_PPC64_ADDR32(R, Value);
95         case llvm::ELF::R_PPC64_ADDR64:
96           return visitELF_PPC64_ADDR64(R, Value);
97         default:
98           HasError = true;
99           return RelocToApply();
100         }
101       case Triple::systemz:
102         switch (RelocType) {
103         case llvm::ELF::R_390_32:
104           return visitELF_390_32(R, Value);
105         case llvm::ELF::R_390_64:
106           return visitELF_390_64(R, Value);
107         default:
108           HasError = true;
109           return RelocToApply();
110         }
111       case Triple::sparcv9:
112         switch (RelocType) {
113         case llvm::ELF::R_SPARC_32:
114         case llvm::ELF::R_SPARC_UA32:
115           return visitELF_SPARCV9_32(R, Value);
116         case llvm::ELF::R_SPARC_64:
117         case llvm::ELF::R_SPARC_UA64:
118           return visitELF_SPARCV9_64(R, Value);
119         default:
120           HasError = true;
121           return RelocToApply();
122         }
123       default:
124         HasError = true;
125         return RelocToApply();
126       }
127     } else if (ObjToVisit.getBytesInAddress() == 4) { // 32-bit object file
128       switch (ObjToVisit.getArch()) {
129       case Triple::x86:
130         switch (RelocType) {
131         case llvm::ELF::R_386_NONE:
132           return visitELF_386_NONE(R);
133         case llvm::ELF::R_386_32:
134           return visitELF_386_32(R, Value);
135         case llvm::ELF::R_386_PC32:
136           return visitELF_386_PC32(R, Value, SecAddr);
137         default:
138           HasError = true;
139           return RelocToApply();
140         }
141       case Triple::ppc:
142         switch (RelocType) {
143         case llvm::ELF::R_PPC_ADDR32:
144           return visitELF_PPC_ADDR32(R, Value);
145         default:
146           HasError = true;
147           return RelocToApply();
148         }
149       case Triple::arm:
150       case Triple::armeb:
151         switch (RelocType) {
152         default:
153           HasError = true;
154           return RelocToApply();
155         case llvm::ELF::R_ARM_ABS32:
156           return visitELF_ARM_ABS32(R, Value);
157         }
158       case Triple::mipsel:
159       case Triple::mips:
160         switch (RelocType) {
161         case llvm::ELF::R_MIPS_32:
162           return visitELF_MIPS_32(R, Value);
163         default:
164           HasError = true;
165           return RelocToApply();
166         }
167       case Triple::sparc:
168         switch (RelocType) {
169         case llvm::ELF::R_SPARC_32:
170         case llvm::ELF::R_SPARC_UA32:
171           return visitELF_SPARC_32(R, Value);
172         default:
173           HasError = true;
174           return RelocToApply();
175         }
176       default:
177         HasError = true;
178         return RelocToApply();
179       }
180     } else {
181       report_fatal_error("Invalid word size in object file");
182     }
183   }
184
185   bool error() { return HasError; }
186
187 private:
188   ObjectFile &ObjToVisit;
189   bool HasError;
190
191   int64_t getAddend32LE(RelocationRef R) {
192     const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
193     DataRefImpl DRI = R.getRawDataRefImpl();
194     int64_t Addend;
195     Obj->getRelocationAddend(DRI, Addend);
196     return Addend;
197   }
198
199   int64_t getAddend64LE(RelocationRef R) {
200     const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
201     DataRefImpl DRI = R.getRawDataRefImpl();
202     int64_t Addend;
203     Obj->getRelocationAddend(DRI, Addend);
204     return Addend;
205   }
206
207   int64_t getAddend32BE(RelocationRef R) {
208     const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
209     DataRefImpl DRI = R.getRawDataRefImpl();
210     int64_t Addend;
211     Obj->getRelocationAddend(DRI, Addend);
212     return Addend;
213   }
214
215   int64_t getAddend64BE(RelocationRef R) {
216     const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
217     DataRefImpl DRI = R.getRawDataRefImpl();
218     int64_t Addend;
219     Obj->getRelocationAddend(DRI, Addend);
220     return Addend;
221   }
222   /// Operations
223
224   /// 386-ELF
225   RelocToApply visitELF_386_NONE(RelocationRef R) {
226     return RelocToApply(0, 0);
227   }
228
229   // Ideally the Addend here will be the addend in the data for
230   // the relocation. It's not actually the case for Rel relocations.
231   RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
232     int64_t Addend = getAddend32LE(R);
233     return RelocToApply(Value + Addend, 4);
234   }
235
236   RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
237                                  uint64_t SecAddr) {
238     int64_t Addend = getAddend32LE(R);
239     uint64_t Address;
240     R.getOffset(Address);
241     return RelocToApply(Value + Addend - Address, 4);
242   }
243
244   /// X86-64 ELF
245   RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
246     return RelocToApply(0, 0);
247   }
248   RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
249     int64_t Addend = getAddend64LE(R);
250     return RelocToApply(Value + Addend, 8);
251   }
252   RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
253                                     uint64_t SecAddr) {
254     int64_t Addend = getAddend64LE(R);
255     uint64_t Address;
256     R.getOffset(Address);
257     return RelocToApply(Value + Addend - Address, 4);
258   }
259   RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
260     int64_t Addend = getAddend64LE(R);
261     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
262     return RelocToApply(Res, 4);
263   }
264   RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
265     int64_t Addend = getAddend64LE(R);
266     int32_t Res = (Value + Addend) & 0xFFFFFFFF;
267     return RelocToApply(Res, 4);
268   }
269
270   /// PPC64 ELF
271   RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
272     int64_t Addend;
273     getELFRelocationAddend(R, Addend);
274     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
275     return RelocToApply(Res, 4);
276   }
277   RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
278     int64_t Addend;
279     getELFRelocationAddend(R, Addend);
280     return RelocToApply(Value + Addend, 8);
281   }
282
283   /// PPC32 ELF
284   RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
285     int64_t Addend = getAddend32BE(R);
286     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
287     return RelocToApply(Res, 4);
288   }
289
290   /// MIPS ELF
291   RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
292     int64_t Addend;
293     getELFRelocationAddend(R, Addend);
294     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
295     return RelocToApply(Res, 4);
296   }
297
298   RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
299     int64_t Addend;
300     getELFRelocationAddend(R, Addend);
301     uint64_t Res = (Value + Addend);
302     return RelocToApply(Res, 8);
303   }
304
305   // AArch64 ELF
306   RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
307     int64_t Addend = getAddend64LE(R);
308     int64_t Res =  Value + Addend;
309
310     // Overflow check allows for both signed and unsigned interpretation.
311     if (Res < INT32_MIN || Res > UINT32_MAX)
312       HasError = true;
313
314     return RelocToApply(static_cast<uint32_t>(Res), 4);
315   }
316
317   RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
318     int64_t Addend = getAddend64LE(R);
319     return RelocToApply(Value + Addend, 8);
320   }
321
322   // SystemZ ELF
323   RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
324     int64_t Addend = getAddend64BE(R);
325     int64_t Res = Value + Addend;
326
327     // Overflow check allows for both signed and unsigned interpretation.
328     if (Res < INT32_MIN || Res > UINT32_MAX)
329       HasError = true;
330
331     return RelocToApply(static_cast<uint32_t>(Res), 4);
332   }
333
334   RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
335     int64_t Addend = getAddend64BE(R);
336     return RelocToApply(Value + Addend, 8);
337   }
338
339   RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
340     int32_t Addend = getAddend32BE(R);
341     return RelocToApply(Value + Addend, 4);
342   }
343
344   RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
345     int32_t Addend = getAddend64BE(R);
346     return RelocToApply(Value + Addend, 4);
347   }
348
349   RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
350     int64_t Addend = getAddend64BE(R);
351     return RelocToApply(Value + Addend, 8);
352   }
353
354   RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) {
355     int64_t Addend = getAddend32LE(R);
356     return RelocToApply(Value + Addend, 4);
357   }
358
359 };
360
361 }
362 }
363 #endif