Refactor the RelocVisitor::visit method
[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::hexagon:
159         llvm_unreachable("Unimplemented");
160       case Triple::mipsel:
161       case Triple::mips:
162         switch (RelocType) {
163         case llvm::ELF::R_MIPS_32:
164           return visitELF_MIPS_32(R, Value);
165         default:
166           HasError = true;
167           return RelocToApply();
168         }
169       case Triple::sparc:
170         switch (RelocType) {
171         case llvm::ELF::R_SPARC_32:
172         case llvm::ELF::R_SPARC_UA32:
173           return visitELF_SPARC_32(R, Value);
174         default:
175           HasError = true;
176           return RelocToApply();
177         }
178       default:
179         HasError = true;
180         return RelocToApply();
181       }
182     } else {
183       report_fatal_error("Invalid word size in object file");
184     }
185   }
186
187   bool error() { return HasError; }
188
189 private:
190   ObjectFile &ObjToVisit;
191   bool HasError;
192
193   int64_t getAddend32LE(RelocationRef R) {
194     const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
195     DataRefImpl DRI = R.getRawDataRefImpl();
196     int64_t Addend;
197     Obj->getRelocationAddend(DRI, Addend);
198     return Addend;
199   }
200
201   int64_t getAddend64LE(RelocationRef R) {
202     const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
203     DataRefImpl DRI = R.getRawDataRefImpl();
204     int64_t Addend;
205     Obj->getRelocationAddend(DRI, Addend);
206     return Addend;
207   }
208
209   int64_t getAddend32BE(RelocationRef R) {
210     const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
211     DataRefImpl DRI = R.getRawDataRefImpl();
212     int64_t Addend;
213     Obj->getRelocationAddend(DRI, Addend);
214     return Addend;
215   }
216
217   int64_t getAddend64BE(RelocationRef R) {
218     const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
219     DataRefImpl DRI = R.getRawDataRefImpl();
220     int64_t Addend;
221     Obj->getRelocationAddend(DRI, Addend);
222     return Addend;
223   }
224   /// Operations
225
226   /// 386-ELF
227   RelocToApply visitELF_386_NONE(RelocationRef R) {
228     return RelocToApply(0, 0);
229   }
230
231   // Ideally the Addend here will be the addend in the data for
232   // the relocation. It's not actually the case for Rel relocations.
233   RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
234     int64_t Addend = getAddend32LE(R);
235     return RelocToApply(Value + Addend, 4);
236   }
237
238   RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
239                                  uint64_t SecAddr) {
240     int64_t Addend = getAddend32LE(R);
241     uint64_t Address;
242     R.getOffset(Address);
243     return RelocToApply(Value + Addend - Address, 4);
244   }
245
246   /// X86-64 ELF
247   RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
248     return RelocToApply(0, 0);
249   }
250   RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
251     int64_t Addend = getAddend64LE(R);
252     return RelocToApply(Value + Addend, 8);
253   }
254   RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
255                                     uint64_t SecAddr) {
256     int64_t Addend = getAddend64LE(R);
257     uint64_t Address;
258     R.getOffset(Address);
259     return RelocToApply(Value + Addend - Address, 4);
260   }
261   RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
262     int64_t Addend = getAddend64LE(R);
263     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
264     return RelocToApply(Res, 4);
265   }
266   RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
267     int64_t Addend = getAddend64LE(R);
268     int32_t Res = (Value + Addend) & 0xFFFFFFFF;
269     return RelocToApply(Res, 4);
270   }
271
272   /// PPC64 ELF
273   RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
274     int64_t Addend;
275     getELFRelocationAddend(R, Addend);
276     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
277     return RelocToApply(Res, 4);
278   }
279   RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
280     int64_t Addend;
281     getELFRelocationAddend(R, Addend);
282     return RelocToApply(Value + Addend, 8);
283   }
284
285   /// PPC32 ELF
286   RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
287     int64_t Addend = getAddend32BE(R);
288     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
289     return RelocToApply(Res, 4);
290   }
291
292   /// MIPS ELF
293   RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
294     int64_t Addend;
295     getELFRelocationAddend(R, Addend);
296     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
297     return RelocToApply(Res, 4);
298   }
299
300   RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
301     int64_t Addend;
302     getELFRelocationAddend(R, Addend);
303     uint64_t Res = (Value + Addend);
304     return RelocToApply(Res, 8);
305   }
306
307   // AArch64 ELF
308   RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
309     int64_t Addend = getAddend64LE(R);
310     int64_t Res =  Value + Addend;
311
312     // Overflow check allows for both signed and unsigned interpretation.
313     if (Res < INT32_MIN || Res > UINT32_MAX)
314       HasError = true;
315
316     return RelocToApply(static_cast<uint32_t>(Res), 4);
317   }
318
319   RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
320     int64_t Addend = getAddend64LE(R);
321     return RelocToApply(Value + Addend, 8);
322   }
323
324   // SystemZ ELF
325   RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
326     int64_t Addend = getAddend64BE(R);
327     int64_t Res = Value + Addend;
328
329     // Overflow check allows for both signed and unsigned interpretation.
330     if (Res < INT32_MIN || Res > UINT32_MAX)
331       HasError = true;
332
333     return RelocToApply(static_cast<uint32_t>(Res), 4);
334   }
335
336   RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
337     int64_t Addend = getAddend64BE(R);
338     return RelocToApply(Value + Addend, 8);
339   }
340
341   RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
342     int32_t Addend = getAddend32BE(R);
343     return RelocToApply(Value + Addend, 4);
344   }
345
346   RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
347     int32_t Addend = getAddend64BE(R);
348     return RelocToApply(Value + Addend, 4);
349   }
350
351   RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
352     int64_t Addend = getAddend64BE(R);
353     return RelocToApply(Value + Addend, 8);
354   }
355
356   RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) {
357     int64_t Addend = getAddend32LE(R);
358     return RelocToApply(Value + Addend, 4);
359   }
360
361 };
362
363 }
364 }
365 #endif