[ms-cxxabi] Add a new calling convention that swaps 'this' and 'sret'
[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-aarch64") {
107       switch (RelocType) {
108       case llvm::ELF::R_AARCH64_ABS32:
109         return visitELF_AARCH64_ABS32(R, Value);
110       case llvm::ELF::R_AARCH64_ABS64:
111         return visitELF_AARCH64_ABS64(R, Value);
112       default:
113         HasError = true;
114         return RelocToApply();
115       }
116     } else if (FileFormat == "ELF64-s390") {
117       switch (RelocType) {
118       case llvm::ELF::R_390_32:
119         return visitELF_390_32(R, Value);
120       case llvm::ELF::R_390_64:
121         return visitELF_390_64(R, Value);
122       default:
123         HasError = true;
124         return RelocToApply();
125       }
126     } else if (FileFormat == "ELF32-sparc") {
127       switch (RelocType) {
128       case llvm::ELF::R_SPARC_32:
129       case llvm::ELF::R_SPARC_UA32:
130         return visitELF_SPARC_32(R, Value);
131       default:
132         HasError = true;
133         return RelocToApply();
134       }
135     } else if (FileFormat == "ELF64-sparc") {
136       switch (RelocType) {
137       case llvm::ELF::R_SPARC_32:
138       case llvm::ELF::R_SPARC_UA32:
139         return visitELF_SPARCV9_32(R, Value);
140       case llvm::ELF::R_SPARC_64:
141       case llvm::ELF::R_SPARC_UA64:
142         return visitELF_SPARCV9_64(R, Value);
143       default:
144         HasError = true;
145         return RelocToApply();
146       }
147     }
148     HasError = true;
149     return RelocToApply();
150   }
151
152   bool error() { return HasError; }
153
154 private:
155   StringRef FileFormat;
156   bool HasError;
157
158   int64_t getAddend32LE(RelocationRef R) {
159     const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
160     DataRefImpl DRI = R.getRawDataRefImpl();
161     int64_t Addend;
162     Obj->getRelocationAddend(DRI, Addend);
163     return Addend;
164   }
165
166   int64_t getAddend64LE(RelocationRef R) {
167     const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
168     DataRefImpl DRI = R.getRawDataRefImpl();
169     int64_t Addend;
170     Obj->getRelocationAddend(DRI, Addend);
171     return Addend;
172   }
173
174   int64_t getAddend32BE(RelocationRef R) {
175     const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
176     DataRefImpl DRI = R.getRawDataRefImpl();
177     int64_t Addend;
178     Obj->getRelocationAddend(DRI, Addend);
179     return Addend;
180   }
181
182   int64_t getAddend64BE(RelocationRef R) {
183     const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
184     DataRefImpl DRI = R.getRawDataRefImpl();
185     int64_t Addend;
186     Obj->getRelocationAddend(DRI, Addend);
187     return Addend;
188   }
189   /// Operations
190
191   /// 386-ELF
192   RelocToApply visitELF_386_NONE(RelocationRef R) {
193     return RelocToApply(0, 0);
194   }
195
196   // Ideally the Addend here will be the addend in the data for
197   // the relocation. It's not actually the case for Rel relocations.
198   RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
199     int64_t Addend = getAddend32LE(R);
200     return RelocToApply(Value + Addend, 4);
201   }
202
203   RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
204                                  uint64_t SecAddr) {
205     int64_t Addend = getAddend32LE(R);
206     uint64_t Address;
207     R.getOffset(Address);
208     return RelocToApply(Value + Addend - Address, 4);
209   }
210
211   /// X86-64 ELF
212   RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
213     return RelocToApply(0, 0);
214   }
215   RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
216     int64_t Addend = getAddend64LE(R);
217     return RelocToApply(Value + Addend, 8);
218   }
219   RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
220                                     uint64_t SecAddr) {
221     int64_t Addend = getAddend64LE(R);
222     uint64_t Address;
223     R.getOffset(Address);
224     return RelocToApply(Value + Addend - Address, 4);
225   }
226   RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
227     int64_t Addend = getAddend64LE(R);
228     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
229     return RelocToApply(Res, 4);
230   }
231   RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
232     int64_t Addend = getAddend64LE(R);
233     int32_t Res = (Value + Addend) & 0xFFFFFFFF;
234     return RelocToApply(Res, 4);
235   }
236
237   /// PPC64 ELF
238   RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
239     int64_t Addend = getAddend64BE(R);
240     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
241     return RelocToApply(Res, 4);
242   }
243   RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
244     int64_t Addend = getAddend64BE(R);
245     return RelocToApply(Value + Addend, 8);
246   }
247
248   /// PPC32 ELF
249   RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
250     int64_t Addend = getAddend32BE(R);
251     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
252     return RelocToApply(Res, 4);
253   }
254
255   /// MIPS ELF
256   RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
257     int64_t Addend;
258     getELFRelocationAddend(R, Addend);
259     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
260     return RelocToApply(Res, 4);
261   }
262
263   // AArch64 ELF
264   RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
265     int64_t Addend = getAddend64LE(R);
266     int64_t Res =  Value + Addend;
267
268     // Overflow check allows for both signed and unsigned interpretation.
269     if (Res < INT32_MIN || Res > UINT32_MAX)
270       HasError = true;
271
272     return RelocToApply(static_cast<uint32_t>(Res), 4);
273   }
274
275   RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
276     int64_t Addend = getAddend64LE(R);
277     return RelocToApply(Value + Addend, 8);
278   }
279
280   // SystemZ ELF
281   RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
282     int64_t Addend = getAddend64BE(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_390_64(RelocationRef R, uint64_t Value) {
293     int64_t Addend = getAddend64BE(R);
294     return RelocToApply(Value + Addend, 8);
295   }
296
297   RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
298     int32_t Addend = getAddend32BE(R);
299     return RelocToApply(Value + Addend, 4);
300   }
301
302   RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
303     int32_t Addend = getAddend64BE(R);
304     return RelocToApply(Value + Addend, 4);
305   }
306
307   RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
308     int64_t Addend = getAddend64BE(R);
309     return RelocToApply(Value + Addend, 8);
310   }
311
312 };
313
314 }
315 }
316 #endif