1 //===-- RelocVisitor.h - Visitor for object file relocations -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef _LLVM_OBJECT_RELOCVISITOR
17 #define _LLVM_OBJECT_RELOCVISITOR
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Object/ELF.h"
23 #include "llvm/ADT/StringRef.h"
29 // The computed value after applying the relevant relocations.
32 // The width of the value; how many bytes to touch when applying the
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) {}
40 /// @brief Base class for object file relocation visitors.
43 explicit RelocVisitor(llvm::StringRef FileFormat)
44 : FileFormat(FileFormat), HasError(false) {}
46 // TODO: Should handle multiple applied relocations via either passing in the
47 // previously computed value or just count paired relocations as a single
49 RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t SecAddr = 0,
51 if (FileFormat == "ELF64-x86-64") {
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);
65 return RelocToApply();
68 return RelocToApply();
71 bool error() { return HasError; }
74 llvm::StringRef FileFormat;
79 // Width is the width in bytes of the extend.
80 RelocToApply zeroExtend(RelocToApply r, char Width) {
83 r.Value &= (1 << ((Width * 8))) - 1;
86 RelocToApply signExtend(RelocToApply r, char Width) {
89 bool SignBit = r.Value & (1 << ((Width * 8) - 1));
91 r.Value |= ~((1 << (Width * 8)) - 1);
93 r.Value &= (1 << (Width * 8)) - 1;
99 RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
100 return RelocToApply(0, 0);
102 RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
104 R.getAdditionalInfo(Addend);
105 return RelocToApply(Value + Addend, 8);
107 RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
110 R.getAdditionalInfo(Addend);
112 R.getAddress(Address);
113 return RelocToApply(Value + Addend - Address, 4);
115 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
117 R.getAdditionalInfo(Addend);
118 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
119 return RelocToApply(Res, 4);
121 RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
123 R.getAdditionalInfo(Addend);
124 int32_t Res = (Value + Addend) & 0xFFFFFFFF;
125 return RelocToApply(Res, 4);