summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/ELF/PPCReference.cpp
blob: ec7df8f3a4c1a9cfa41ef3e0524811ef98f540f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//===- lib/ReaderWriter/ELF/PPCReference.cpp ----------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//


#include "ReferenceKinds.h"

#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"

#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ELF.h"

namespace lld {
namespace elf {

//===----------------------------------------------------------------------===//
//  PPCKindHandler
//  TODO: more to do here
//===----------------------------------------------------------------------===//

PPCKindHandler::~PPCKindHandler() {
}

/// \brief The following relocation routines are derived from the
///  SYSTEM V APPLICATION BINARY INTERFACE: PowerPC Processor Supplement
/// Symbols used:
///  A: Added used to compute the value, r_addend
///  P: Place address of the field being relocated, r_offset
///  S: Value of the symbol whose index resides in the relocation entry.

namespace ppc {
int relocNONE(uint8_t *location, uint64_t P, uint64_t S, uint64_t A) {
  return PPCKindHandler::NoError;
}

/// \brief low24 (S + A - P) >> 2 : Verify
int relocB24PCREL(uint8_t *location, uint64_t P, uint64_t S, uint64_t A) {
  int32_t result = (uint32_t)(((S + A) - P));
  if ((result < 0x1000000) && (result > -0x1000000)) {
    result &= ~-(0x1000000);
    *reinterpret_cast<llvm::support::ubig32_t *>(location) = result |
               (uint32_t)*reinterpret_cast<llvm::support::ubig32_t *>(location);
    return PPCKindHandler::NoError;
  }
  return PPCKindHandler::Overflow;
}
} // namespace ppc

PPCKindHandler::PPCKindHandler(llvm::support::endianness endian){
  _fixupHandler[llvm::ELF::R_PPC_REL24] = ppc::relocB24PCREL;
}

Reference::Kind PPCKindHandler::stringToKind(StringRef str) {
  return llvm::StringSwitch<Reference::Kind>(str)
    .Case("none",                  none)
    .Case("R_PPC_REL24", llvm::ELF::R_PPC_REL24)
    .Default(invalid);
}

StringRef PPCKindHandler::kindToString(Reference::Kind kind) {
  switch ((int32_t)kind) {
  case llvm::ELF::R_PPC_REL24:
    return "R_PPC_REL24";
  default:
    return "none";
  }
}

bool PPCKindHandler::isCallSite(Kind kind) {
  llvm_unreachable("Unimplemented: PPCKindHandler::isCallSite");
  return false;
}

bool PPCKindHandler::isPointer(Kind kind) {
  llvm_unreachable("Unimplemented: PPCKindHandler::isPointer");
  return false;
}

bool PPCKindHandler::isLazyImmediate(Kind kind) {
  llvm_unreachable("Unimplemented: PPCKindHandler::isLazyImmediate");
  return false;
}

bool PPCKindHandler::isLazyTarget(Kind kind) {
  llvm_unreachable("Unimplemented: PPCKindHandler::isLazyTarget");
  return false;
}

void PPCKindHandler::applyFixup(int32_t reloc, uint64_t addend,
                                 uint8_t *location, uint64_t fixupAddress,
                                 uint64_t targetAddress) {
  int error;
  if (_fixupHandler[reloc])
  {
    error = (_fixupHandler[reloc])(location,
                                   fixupAddress, targetAddress, addend);

    switch ((RelocationError)error) {
    case NoError:
      return;
    case Overflow:
      llvm::report_fatal_error("applyFixup relocation overflow");
      return;
    }
  }
}

} // namespace elf
} // namespace lld
OpenPOWER on IntegriCloud