[mips] Add support for ELF64-mips and the R_MIPS_32/R_MIPS_64 relocs for it.
[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(const RelocToApply &In) : Value(In.Value), Width(In.Width) {}
37   RelocToApply(int64_t Value, char Width) : Value(Value), Width(Width) {}
38   RelocToApply() : Value(0), Width(0) {}
39 };
40
41 /// @brief Base class for object file relocation visitors.
42 class RelocVisitor {
43 public:
44   explicit RelocVisitor(StringRef FileFormat)
45     : FileFormat(FileFormat), HasError(false) {}
46
47   // TODO: Should handle multiple applied relocations via either passing in the
48   // previously computed value or just count paired relocations as a single
49   // visit.
50   RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t SecAddr = 0,
51                      uint64_t Value = 0) {
52     if (FileFormat == "ELF64-x86-64") {
53       switch (RelocType) {
54         case llvm::ELF::R_X86_64_NONE:
55           return visitELF_X86_64_NONE(R);
56         case llvm::ELF::R_X86_64_64:
57           return visitELF_X86_64_64(R, Value);
58         case llvm::ELF::R_X86_64_PC32:
59           return visitELF_X86_64_PC32(R, Value, SecAddr);
60         case llvm::ELF::R_X86_64_32:
61           return visitELF_X86_64_32(R, Value);
62         case llvm::ELF::R_X86_64_32S:
63           return visitELF_X86_64_32S(R, Value);
64         default:
65           HasError = true;
66           return RelocToApply();
67       }
68     } else if (FileFormat == "ELF32-i386") {
69       switch (RelocType) {
70       case llvm::ELF::R_386_NONE:
71         return visitELF_386_NONE(R);
72       case llvm::ELF::R_386_32:
73         return visitELF_386_32(R, Value);
74       case llvm::ELF::R_386_PC32:
75         return visitELF_386_PC32(R, Value, SecAddr);
76       default:
77         HasError = true;
78         return RelocToApply();
79       }
80     } else if (FileFormat == "ELF64-ppc64") {
81       switch (RelocType) {
82       case llvm::ELF::R_PPC64_ADDR32:
83         return visitELF_PPC64_ADDR32(R, Value);
84       case llvm::ELF::R_PPC64_ADDR64:
85         return visitELF_PPC64_ADDR64(R, Value);
86       default:
87         HasError = true;
88         return RelocToApply();
89       }
90     } else if (FileFormat == "ELF32-ppc") {
91       switch (RelocType) {
92       case llvm::ELF::R_PPC_ADDR32:
93         return visitELF_PPC_ADDR32(R, Value);
94       default:
95         HasError = true;
96         return RelocToApply();
97       }
98     } else if (FileFormat == "ELF32-mips") {
99       switch (RelocType) {
100       case llvm::ELF::R_MIPS_32:
101         return visitELF_MIPS_32(R, Value);
102       default:
103         HasError = true;
104         return RelocToApply();
105       }
106     } else if (FileFormat == "ELF64-mips") {
107       switch (RelocType) {
108       case llvm::ELF::R_MIPS_32:
109         return visitELF_MIPS_32(R, Value);
110       case llvm::ELF::R_MIPS_64:
111         return visitELF_MIPS_64(R, Value);
112       default:
113         HasError = true;
114         return RelocToApply();
115       }
116     } else if (FileFormat == "ELF64-aarch64") {
117       switch (RelocType) {
118       case llvm::ELF::R_AARCH64_ABS32:
119         return visitELF_AARCH64_ABS32(R, Value);
120       case llvm::ELF::R_AARCH64_ABS64:
121         return visitELF_AARCH64_ABS64(R, Value);
122       default:
123         HasError = true;
124         return RelocToApply();
125       }
126     } else if (FileFormat == "ELF64-s390") {
127       switch (RelocType) {
128       case llvm::ELF::R_390_32:
129         return visitELF_390_32(R, Value);
130       case llvm::ELF::R_390_64:
131         return visitELF_390_64(R, Value);
132       default:
133         HasError = true;
134         return RelocToApply();
135       }
136     } else if (FileFormat == "ELF32-sparc") {
137       switch (RelocType) {
138       case llvm::ELF::R_SPARC_32:
139       case llvm::ELF::R_SPARC_UA32:
140         return visitELF_SPARC_32(R, Value);
141       default:
142         HasError = true;
143         return RelocToApply();
144       }
145     } else if (FileFormat == "ELF64-sparc") {
146       switch (RelocType) {
147       case llvm::ELF::R_SPARC_32:
148       case llvm::ELF::R_SPARC_UA32:
149         return visitELF_SPARCV9_32(R, Value);
150       case llvm::ELF::R_SPARC_64:
151       case llvm::ELF::R_SPARC_UA64:
152         return visitELF_SPARCV9_64(R, Value);
153       default:
154         HasError = true;
155         return RelocToApply();
156       }
157     }
158     HasError = true;
159     return RelocToApply();
160   }
161
162   bool error() { return HasError; }
163
164 private:
165   StringRef FileFormat;
166   bool HasError;
167
168   int64_t getAddend32LE(RelocationRef R) {
169     const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
170     DataRefImpl DRI = R.getRawDataRefImpl();
171     int64_t Addend;
172     Obj->getRelocationAddend(DRI, Addend);
173     return Addend;
174   }
175
176   int64_t getAddend64LE(RelocationRef R) {
177     const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
178     DataRefImpl DRI = R.getRawDataRefImpl();
179     int64_t Addend;
180     Obj->getRelocationAddend(DRI, Addend);
181     return Addend;
182   }
183
184   int64_t getAddend32BE(RelocationRef R) {
185     const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
186     DataRefImpl DRI = R.getRawDataRefImpl();
187     int64_t Addend;
188     Obj->getRelocationAddend(DRI, Addend);
189     return Addend;
190   }
191
192   int64_t getAddend64BE(RelocationRef R) {
193     const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
194     DataRefImpl DRI = R.getRawDataRefImpl();
195     int64_t Addend;
196     Obj->getRelocationAddend(DRI, Addend);
197     return Addend;
198   }
199   /// Operations
200
201   /// 386-ELF
202   RelocToApply visitELF_386_NONE(RelocationRef R) {
203     return RelocToApply(0, 0);
204   }
205
206   // Ideally the Addend here will be the addend in the data for
207   // the relocation. It's not actually the case for Rel relocations.
208   RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
209     int64_t Addend = getAddend32LE(R);
210     return RelocToApply(Value + Addend, 4);
211   }
212
213   RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
214                                  uint64_t SecAddr) {
215     int64_t Addend = getAddend32LE(R);
216     uint64_t Address;
217     R.getOffset(Address);
218     return RelocToApply(Value + Addend - Address, 4);
219   }
220
221   /// X86-64 ELF
222   RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
223     return RelocToApply(0, 0);
224   }
225   RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
226     int64_t Addend = getAddend64LE(R);
227     return RelocToApply(Value + Addend, 8);
228   }
229   RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
230                                     uint64_t SecAddr) {
231     int64_t Addend = getAddend64LE(R);
232     uint64_t Address;
233     R.getOffset(Address);
234     return RelocToApply(Value + Addend - Address, 4);
235   }
236   RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
237     int64_t Addend = getAddend64LE(R);
238     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
239     return RelocToApply(Res, 4);
240   }
241   RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
242     int64_t Addend = getAddend64LE(R);
243     int32_t Res = (Value + Addend) & 0xFFFFFFFF;
244     return RelocToApply(Res, 4);
245   }
246
247   /// PPC64 ELF
248   RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
249     int64_t Addend = getAddend64BE(R);
250     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
251     return RelocToApply(Res, 4);
252   }
253   RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
254     int64_t Addend = getAddend64BE(R);
255     return RelocToApply(Value + Addend, 8);
256   }
257
258   /// PPC32 ELF
259   RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
260     int64_t Addend = getAddend32BE(R);
261     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
262     return RelocToApply(Res, 4);
263   }
264
265   /// MIPS ELF
266   RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
267     int64_t Addend;
268     getELFRelocationAddend(R, Addend);
269     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
270     return RelocToApply(Res, 4);
271   }
272
273   RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
274     int64_t Addend;
275     getELFRelocationAddend(R, Addend);
276     uint64_t Res = (Value + Addend);
277     return RelocToApply(Res, 8);
278   }
279
280   // AArch64 ELF
281   RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
282     int64_t Addend = getAddend64LE(R);
283     int64_t Res =  Value + Addend;
284
285     // Overflow check allows for both signed and unsigned interpretation.
286     if (Res < INT32_MIN || Res > UINT32_MAX)
287       HasError = true;
288
289     return RelocToApply(static_cast<uint32_t>(Res), 4);
290   }
291
292   RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
293     int64_t Addend = getAddend64LE(R);
294     return RelocToApply(Value + Addend, 8);
295   }
296
297   // SystemZ ELF
298   RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
299     int64_t Addend = getAddend64BE(R);
300     int64_t Res = Value + Addend;
301
302     // Overflow check allows for both signed and unsigned interpretation.
303     if (Res < INT32_MIN || Res > UINT32_MAX)
304       HasError = true;
305
306     return RelocToApply(static_cast<uint32_t>(Res), 4);
307   }
308
309   RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
310     int64_t Addend = getAddend64BE(R);
311     return RelocToApply(Value + Addend, 8);
312   }
313
314   RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
315     int32_t Addend = getAddend32BE(R);
316     return RelocToApply(Value + Addend, 4);
317   }
318
319   RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
320     int32_t Addend = getAddend64BE(R);
321     return RelocToApply(Value + Addend, 4);
322   }
323
324   RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
325     int64_t Addend = getAddend64BE(R);
326     return RelocToApply(Value + Addend, 8);
327   }
328
329 };
330
331 }
332 }
333 #endif