Fix include guards so they exactly match file names.
[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/ELF.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
24
25 namespace llvm {
26 namespace object {
27
28 struct RelocToApply {
29   // The computed value after applying the relevant relocations.
30   int64_t Value;
31
32   // The width of the value; how many bytes to touch when applying the
33   // relocation.
34   char Width;
35   RelocToApply(const RelocToApply &In) : Value(In.Value), Width(In.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(llvm::StringRef FileFormat)
44     : FileFormat(FileFormat), 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 (FileFormat == "ELF64-x86-64") {
52       switch (RelocType) {
53         case llvm::ELF::R_X86_64_NONE:
54           return visitELF_X86_64_NONE(R);
55         case llvm::ELF::R_X86_64_64:
56           return visitELF_X86_64_64(R, Value);
57         case llvm::ELF::R_X86_64_PC32:
58           return visitELF_X86_64_PC32(R, Value, SecAddr);
59         case llvm::ELF::R_X86_64_32:
60           return visitELF_X86_64_32(R, Value);
61         case llvm::ELF::R_X86_64_32S:
62           return visitELF_X86_64_32S(R, Value);
63         default:
64           HasError = true;
65           return RelocToApply();
66       }
67     } else if (FileFormat == "ELF32-i386") {
68       switch (RelocType) {
69       case llvm::ELF::R_386_NONE:
70         return visitELF_386_NONE(R);
71       case llvm::ELF::R_386_32:
72         return visitELF_386_32(R, Value);
73       case llvm::ELF::R_386_PC32:
74         return visitELF_386_PC32(R, Value, SecAddr);
75       default:
76         HasError = true;
77         return RelocToApply();
78       }
79     }
80     return RelocToApply();
81   }
82
83   bool error() { return HasError; }
84
85 private:
86   llvm::StringRef FileFormat;
87   bool HasError;
88
89   /// Operations
90
91   /// 386-ELF
92   RelocToApply visitELF_386_NONE(RelocationRef R) {
93     return RelocToApply(0, 0);
94   }
95
96   // Ideally the Addend here will be the addend in the data for
97   // the relocation. It's not actually the case for Rel relocations.
98   RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
99     int64_t Addend;
100     R.getAdditionalInfo(Addend);
101     return RelocToApply(Value + Addend, 4);
102   }
103
104   RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
105                                  uint64_t SecAddr) {
106     int64_t Addend;
107     R.getAdditionalInfo(Addend);
108     uint64_t Address;
109     R.getAddress(Address);
110     return RelocToApply(Value + Addend - Address, 4);
111   }
112
113   /// X86-64 ELF
114   RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
115     return RelocToApply(0, 0);
116   }
117   RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
118     int64_t Addend;
119     R.getAdditionalInfo(Addend);
120     return RelocToApply(Value + Addend, 8);
121   }
122   RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
123                                     uint64_t SecAddr) {
124     int64_t Addend;
125     R.getAdditionalInfo(Addend);
126     uint64_t Address;
127     R.getAddress(Address);
128     return RelocToApply(Value + Addend - Address, 4);
129   }
130   RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
131     int64_t Addend;
132     R.getAdditionalInfo(Addend);
133     uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
134     return RelocToApply(Res, 4);
135   }
136   RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
137     int64_t Addend;
138     R.getAdditionalInfo(Addend);
139     int32_t Res = (Value + Addend) & 0xFFFFFFFF;
140     return RelocToApply(Res, 4);
141   }
142 };
143
144 }
145 }
146 #endif