Add basic support for ELF32-ppc relocations to llvm-dwarfdump.
[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/ObjectFile.h"
21 #include "llvm/Object/ELF.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       default:
85         HasError = true;
86         return RelocToApply();
87       }
88     } else if (FileFormat == "ELF32-ppc") {
89       switch (RelocType) {
90       case llvm::ELF::R_PPC_ADDR32:
91         return visitELF_PPC_ADDR32(R, Value);
92       default:
93         HasError = true;
94         return RelocToApply();
95       }
96     } else if (FileFormat == "ELF32-mips") {
97       switch (RelocType) {
98       case llvm::ELF::R_MIPS_32:
99         return visitELF_MIPS_32(R, Value);
100       default:
101         HasError = true;
102         return RelocToApply();
103       }
104     } else if (FileFormat == "ELF64-aarch64") {
105       switch (RelocType) {
106       case llvm::ELF::R_AARCH64_ABS32:
107         return visitELF_AARCH64_ABS32(R, Value);
108       case llvm::ELF::R_AARCH64_ABS64:
109         return visitELF_AARCH64_ABS64(R, Value);
110       default:
111         HasError = true;
112         return RelocToApply();
113       }
114     } else if (FileFormat == "ELF64-s390") {
115       switch (RelocType) {
116       case llvm::ELF::R_390_32:
117         return visitELF_390_32(R, Value);
118       case llvm::ELF::R_390_64:
119         return visitELF_390_64(R, Value);
120       default:
121         HasError = true;
122         return RelocToApply();
123       }
124     }
125     HasError = true;
126     return RelocToApply();
127   }
128
129   bool error() { return HasError; }
130
131 private:
132   StringRef FileFormat;
133   bool HasError;
134
135   int64_t getAddend32LE(RelocationRef R) {
136     const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
137     DataRefImpl DRI = R.getRawDataRefImpl();
138     int64_t Addend;
139     Obj->getRelocationAddend(DRI, Addend);
140     return Addend;
141   }
142
143   int64_t getAddend64LE(RelocationRef R) {
144     const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
145     DataRefImpl DRI = R.getRawDataRefImpl();
146     int64_t Addend;
147     Obj->getRelocationAddend(DRI, Addend);
148     return Addend;
149   }
150
151   int64_t getAddend32BE(RelocationRef R) {
152     const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
153     DataRefImpl DRI = R.getRawDataRefImpl();
154     int64_t Addend;
155     Obj->getRelocationAddend(DRI, Addend);
156     return Addend;
157   }
158
159   int64_t getAddend64BE(RelocationRef R) {
160     const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
161     DataRefImpl DRI = R.getRawDataRefImpl();
162     int64_t Addend;
163     Obj->getRelocationAddend(DRI, Addend);
164     return Addend;
165   }
166   /// Operations
167
168   /// 386-ELF
169   RelocToApply visitELF_386_NONE(RelocationRef R) {
170     return RelocToApply(0, 0);
171   }
172
173   // Ideally the Addend here will be the addend in the data for
174   // the relocation. It's not actually the case for Rel relocations.
175   RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
176     int64_t Addend = getAddend32LE(R);
177     return RelocToApply(Value + Addend, 4);
178   }
179
180   RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
181                                  uint64_t SecAddr) {
182     int64_t Addend = getAddend32LE(R);
183     uint64_t Address;
184     R.getOffset(Address);
185     return RelocToApply(Value + Addend - Address, 4);
186   }
187
188   /// X86-64 ELF
189   RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
190     return RelocToApply(0, 0);
191   }
192   RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
193     int64_t Addend = getAddend64LE(R);
194     return RelocToApply(Value + Addend, 8);
195   }
196   RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
197                                     uint64_t SecAddr) {
198     int64_t Addend = getAddend64LE(R);
199     uint64_t Address;
200     R.getOffset(Address);
201     return RelocToApply(Value + Addend - Address, 4);
202   }
203   RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
204     int64_t Addend = getAddend64LE(R);
205     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
206     return RelocToApply(Res, 4);
207   }
208   RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
209     int64_t Addend = getAddend64LE(R);
210     int32_t Res = (Value + Addend) & 0xFFFFFFFF;
211     return RelocToApply(Res, 4);
212   }
213
214   /// PPC64 ELF
215   RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
216     int64_t Addend = getAddend64BE(R);
217     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
218     return RelocToApply(Res, 4);
219   }
220
221   /// PPC32 ELF
222   RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
223     int64_t Addend = getAddend32BE(R);
224     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
225     return RelocToApply(Res, 4);
226   }
227
228   /// MIPS ELF
229   RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
230     int64_t Addend;
231     getELFRelocationAddend(R, Addend);
232     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
233     return RelocToApply(Res, 4);
234   }
235
236   // AArch64 ELF
237   RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
238     int64_t Addend = getAddend64LE(R);
239     int64_t Res =  Value + Addend;
240
241     // Overflow check allows for both signed and unsigned interpretation.
242     if (Res < INT32_MIN || Res > UINT32_MAX)
243       HasError = true;
244
245     return RelocToApply(static_cast<uint32_t>(Res), 4);
246   }
247
248   RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
249     int64_t Addend = getAddend64LE(R);
250     return RelocToApply(Value + Addend, 8);
251   }
252
253   // SystemZ ELF
254   RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
255     int64_t Addend = getAddend64BE(R);
256     int64_t Res = Value + Addend;
257
258     // Overflow check allows for both signed and unsigned interpretation.
259     if (Res < INT32_MIN || Res > UINT32_MAX)
260       HasError = true;
261
262     return RelocToApply(static_cast<uint32_t>(Res), 4);
263   }
264
265   RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
266     int64_t Addend = getAddend64BE(R);
267     return RelocToApply(Value + Addend, 8);
268   }
269 };
270
271 }
272 }
273 #endif