Fixed/added namespace ending comments using clang-tidy. NFC
[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/COFF.h"
21 #include "llvm/Object/ELFObjectFile.h"
22 #include "llvm/Object/MachO.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/Support/MachO.h"
27 #include "llvm/Support/raw_ostream.h"
28
29 namespace llvm {
30 namespace object {
31
32 struct RelocToApply {
33   // The computed value after applying the relevant relocations.
34   int64_t Value;
35
36   // The width of the value; how many bytes to touch when applying the
37   // relocation.
38   char Width;
39   RelocToApply(int64_t Value, char Width) : Value(Value), Width(Width) {}
40   RelocToApply() : Value(0), Width(0) {}
41 };
42
43 /// @brief Base class for object file relocation visitors.
44 class RelocVisitor {
45 public:
46   explicit RelocVisitor(const ObjectFile &Obj)
47     : ObjToVisit(Obj), HasError(false) {}
48
49   // TODO: Should handle multiple applied relocations via either passing in the
50   // previously computed value or just count paired relocations as a single
51   // visit.
52   RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t Value = 0) {
53     if (isa<ELFObjectFileBase>(ObjToVisit))
54       return visitELF(RelocType, R, Value);
55     if (isa<COFFObjectFile>(ObjToVisit))
56       return visitCOFF(RelocType, R, Value);
57     if (isa<MachOObjectFile>(ObjToVisit))
58       return visitMachO(RelocType, R, Value);
59
60     HasError = true;
61     return RelocToApply();
62   }
63
64   bool error() { return HasError; }
65
66 private:
67   const ObjectFile &ObjToVisit;
68   bool HasError;
69
70   RelocToApply visitELF(uint32_t RelocType, RelocationRef R, uint64_t Value) {
71     if (ObjToVisit.getBytesInAddress() == 8) { // 64-bit object file
72       switch (ObjToVisit.getArch()) {
73       case Triple::x86_64:
74         switch (RelocType) {
75         case llvm::ELF::R_X86_64_NONE:
76           return visitELF_X86_64_NONE(R);
77         case llvm::ELF::R_X86_64_64:
78           return visitELF_X86_64_64(R, Value);
79         case llvm::ELF::R_X86_64_PC32:
80           return visitELF_X86_64_PC32(R, Value);
81         case llvm::ELF::R_X86_64_32:
82           return visitELF_X86_64_32(R, Value);
83         case llvm::ELF::R_X86_64_32S:
84           return visitELF_X86_64_32S(R, Value);
85         default:
86           HasError = true;
87           return RelocToApply();
88         }
89       case Triple::aarch64:
90         switch (RelocType) {
91         case llvm::ELF::R_AARCH64_ABS32:
92           return visitELF_AARCH64_ABS32(R, Value);
93         case llvm::ELF::R_AARCH64_ABS64:
94           return visitELF_AARCH64_ABS64(R, Value);
95         default:
96           HasError = true;
97           return RelocToApply();
98         }
99       case Triple::mips64el:
100       case Triple::mips64:
101         switch (RelocType) {
102         case llvm::ELF::R_MIPS_32:
103           return visitELF_MIPS_32(R, Value);
104         case llvm::ELF::R_MIPS_64:
105           return visitELF_MIPS_64(R, Value);
106         default:
107           HasError = true;
108           return RelocToApply();
109         }
110       case Triple::ppc64le:
111       case Triple::ppc64:
112         switch (RelocType) {
113         case llvm::ELF::R_PPC64_ADDR32:
114           return visitELF_PPC64_ADDR32(R, Value);
115         case llvm::ELF::R_PPC64_ADDR64:
116           return visitELF_PPC64_ADDR64(R, Value);
117         default:
118           HasError = true;
119           return RelocToApply();
120         }
121       case Triple::systemz:
122         switch (RelocType) {
123         case llvm::ELF::R_390_32:
124           return visitELF_390_32(R, Value);
125         case llvm::ELF::R_390_64:
126           return visitELF_390_64(R, Value);
127         default:
128           HasError = true;
129           return RelocToApply();
130         }
131       case Triple::sparcv9:
132         switch (RelocType) {
133         case llvm::ELF::R_SPARC_32:
134         case llvm::ELF::R_SPARC_UA32:
135           return visitELF_SPARCV9_32(R, Value);
136         case llvm::ELF::R_SPARC_64:
137         case llvm::ELF::R_SPARC_UA64:
138           return visitELF_SPARCV9_64(R, Value);
139         default:
140           HasError = true;
141           return RelocToApply();
142         }
143       default:
144         HasError = true;
145         return RelocToApply();
146       }
147     } else if (ObjToVisit.getBytesInAddress() == 4) { // 32-bit object file
148       switch (ObjToVisit.getArch()) {
149       case Triple::x86:
150         switch (RelocType) {
151         case llvm::ELF::R_386_NONE:
152           return visitELF_386_NONE(R);
153         case llvm::ELF::R_386_32:
154           return visitELF_386_32(R, Value);
155         case llvm::ELF::R_386_PC32:
156           return visitELF_386_PC32(R, Value);
157         default:
158           HasError = true;
159           return RelocToApply();
160         }
161       case Triple::ppc:
162         switch (RelocType) {
163         case llvm::ELF::R_PPC_ADDR32:
164           return visitELF_PPC_ADDR32(R, Value);
165         default:
166           HasError = true;
167           return RelocToApply();
168         }
169       case Triple::arm:
170       case Triple::armeb:
171         switch (RelocType) {
172         default:
173           HasError = true;
174           return RelocToApply();
175         case llvm::ELF::R_ARM_ABS32:
176           return visitELF_ARM_ABS32(R, Value);
177         }
178       case Triple::mipsel:
179       case Triple::mips:
180         switch (RelocType) {
181         case llvm::ELF::R_MIPS_32:
182           return visitELF_MIPS_32(R, Value);
183         default:
184           HasError = true;
185           return RelocToApply();
186         }
187       case Triple::sparc:
188         switch (RelocType) {
189         case llvm::ELF::R_SPARC_32:
190         case llvm::ELF::R_SPARC_UA32:
191           return visitELF_SPARC_32(R, Value);
192         default:
193           HasError = true;
194           return RelocToApply();
195         }
196       default:
197         HasError = true;
198         return RelocToApply();
199       }
200     } else {
201       report_fatal_error("Invalid word size in object file");
202     }
203   }
204
205   RelocToApply visitCOFF(uint32_t RelocType, RelocationRef R, uint64_t Value) {
206     switch (ObjToVisit.getArch()) {
207     case Triple::x86:
208       switch (RelocType) {
209       case COFF::IMAGE_REL_I386_SECREL:
210         return visitCOFF_I386_SECREL(R, Value);
211       case COFF::IMAGE_REL_I386_DIR32:
212         return visitCOFF_I386_DIR32(R, Value);
213       }
214       break;
215     case Triple::x86_64:
216       switch (RelocType) {
217       case COFF::IMAGE_REL_AMD64_SECREL:
218         return visitCOFF_AMD64_SECREL(R, Value);
219       case COFF::IMAGE_REL_AMD64_ADDR64:
220         return visitCOFF_AMD64_ADDR64(R, Value);
221       }
222       break;
223     }
224     HasError = true;
225     return RelocToApply();
226   }
227
228   RelocToApply visitMachO(uint32_t RelocType, RelocationRef R, uint64_t Value) {
229     switch (ObjToVisit.getArch()) {
230     default: break;
231     case Triple::x86_64:
232       switch (RelocType) {
233         default: break;
234         case MachO::X86_64_RELOC_UNSIGNED:
235           return visitMACHO_X86_64_UNSIGNED(R, Value);
236       }
237     }
238     HasError = true;
239     return RelocToApply();
240   }
241
242   int64_t getELFAddend32LE(RelocationRef R) {
243     const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
244     DataRefImpl DRI = R.getRawDataRefImpl();
245     int64_t Addend;
246     Obj->getRelocationAddend(DRI, Addend);
247     return Addend;
248   }
249
250   int64_t getELFAddend64LE(RelocationRef R) {
251     const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
252     DataRefImpl DRI = R.getRawDataRefImpl();
253     int64_t Addend;
254     Obj->getRelocationAddend(DRI, Addend);
255     return Addend;
256   }
257
258   int64_t getELFAddend32BE(RelocationRef R) {
259     const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
260     DataRefImpl DRI = R.getRawDataRefImpl();
261     int64_t Addend;
262     Obj->getRelocationAddend(DRI, Addend);
263     return Addend;
264   }
265
266   int64_t getELFAddend64BE(RelocationRef R) {
267     const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
268     DataRefImpl DRI = R.getRawDataRefImpl();
269     int64_t Addend;
270     Obj->getRelocationAddend(DRI, Addend);
271     return Addend;
272   }
273
274   uint8_t getLengthMachO64(RelocationRef R) {
275     const MachOObjectFile *Obj = cast<MachOObjectFile>(R.getObjectFile());
276     return Obj->getRelocationLength(R.getRawDataRefImpl());
277   }
278
279   /// Operations
280
281   /// 386-ELF
282   RelocToApply visitELF_386_NONE(RelocationRef R) {
283     return RelocToApply(0, 0);
284   }
285
286   // Ideally the Addend here will be the addend in the data for
287   // the relocation. It's not actually the case for Rel relocations.
288   RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
289     int64_t Addend = getELFAddend32LE(R);
290     return RelocToApply(Value + Addend, 4);
291   }
292
293   RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value) {
294     int64_t Addend = getELFAddend32LE(R);
295     uint64_t Address;
296     R.getOffset(Address);
297     return RelocToApply(Value + Addend - Address, 4);
298   }
299
300   /// X86-64 ELF
301   RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
302     return RelocToApply(0, 0);
303   }
304   RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
305     int64_t Addend = getELFAddend64LE(R);
306     return RelocToApply(Value + Addend, 8);
307   }
308   RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value) {
309     int64_t Addend = getELFAddend64LE(R);
310     uint64_t Address;
311     R.getOffset(Address);
312     return RelocToApply(Value + Addend - Address, 4);
313   }
314   RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
315     int64_t Addend = getELFAddend64LE(R);
316     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
317     return RelocToApply(Res, 4);
318   }
319   RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
320     int64_t Addend = getELFAddend64LE(R);
321     int32_t Res = (Value + Addend) & 0xFFFFFFFF;
322     return RelocToApply(Res, 4);
323   }
324
325   /// PPC64 ELF
326   RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
327     int64_t Addend;
328     getELFRelocationAddend(R, Addend);
329     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
330     return RelocToApply(Res, 4);
331   }
332   RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
333     int64_t Addend;
334     getELFRelocationAddend(R, Addend);
335     return RelocToApply(Value + Addend, 8);
336   }
337
338   /// PPC32 ELF
339   RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
340     int64_t Addend = getELFAddend32BE(R);
341     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
342     return RelocToApply(Res, 4);
343   }
344
345   /// MIPS ELF
346   RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
347     int64_t Addend;
348     getELFRelocationAddend(R, Addend);
349     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
350     return RelocToApply(Res, 4);
351   }
352
353   RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
354     int64_t Addend;
355     getELFRelocationAddend(R, Addend);
356     uint64_t Res = (Value + Addend);
357     return RelocToApply(Res, 8);
358   }
359
360   // AArch64 ELF
361   RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
362     int64_t Addend;
363     getELFRelocationAddend(R, Addend);
364     int64_t Res =  Value + Addend;
365
366     // Overflow check allows for both signed and unsigned interpretation.
367     if (Res < INT32_MIN || Res > UINT32_MAX)
368       HasError = true;
369
370     return RelocToApply(static_cast<uint32_t>(Res), 4);
371   }
372
373   RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
374     int64_t Addend;
375     getELFRelocationAddend(R, Addend);
376     return RelocToApply(Value + Addend, 8);
377   }
378
379   // SystemZ ELF
380   RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
381     int64_t Addend = getELFAddend64BE(R);
382     int64_t Res = Value + Addend;
383
384     // Overflow check allows for both signed and unsigned interpretation.
385     if (Res < INT32_MIN || Res > UINT32_MAX)
386       HasError = true;
387
388     return RelocToApply(static_cast<uint32_t>(Res), 4);
389   }
390
391   RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
392     int64_t Addend = getELFAddend64BE(R);
393     return RelocToApply(Value + Addend, 8);
394   }
395
396   RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
397     int32_t Addend = getELFAddend32BE(R);
398     return RelocToApply(Value + Addend, 4);
399   }
400
401   RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
402     int32_t Addend = getELFAddend64BE(R);
403     return RelocToApply(Value + Addend, 4);
404   }
405
406   RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
407     int64_t Addend = getELFAddend64BE(R);
408     return RelocToApply(Value + Addend, 8);
409   }
410
411   RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) {
412     int64_t Addend;
413     getELFRelocationAddend(R, Addend);
414     int64_t Res = Value + Addend;
415
416     // Overflow check allows for both signed and unsigned interpretation.
417     if (Res < INT32_MIN || Res > UINT32_MAX)
418       HasError = true;
419
420     return RelocToApply(static_cast<uint32_t>(Res), 4);
421   }
422
423   /// I386 COFF
424   RelocToApply visitCOFF_I386_SECREL(RelocationRef R, uint64_t Value) {
425     return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
426   }
427
428   RelocToApply visitCOFF_I386_DIR32(RelocationRef R, uint64_t Value) {
429     return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
430   }
431
432   /// AMD64 COFF
433   RelocToApply visitCOFF_AMD64_SECREL(RelocationRef R, uint64_t Value) {
434     return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
435   }
436
437   RelocToApply visitCOFF_AMD64_ADDR64(RelocationRef R, uint64_t Value) {
438     return RelocToApply(Value, /*Width=*/8);
439   }
440
441   // X86_64 MachO
442   RelocToApply visitMACHO_X86_64_UNSIGNED(RelocationRef R, uint64_t Value) {
443     uint8_t Length = getLengthMachO64(R);
444     Length = 1<<Length;
445     return RelocToApply(Value, Length);
446   }
447 };
448
449 } // namespace object
450 } // namespace llvm
451 #endif