xref: /llvm-project-15.0.7/lld/ELF/Arch/PPC.cpp (revision ad38fbff)
1 //===- PPC.cpp ------------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Error.h"
11 #include "Symbols.h"
12 #include "Target.h"
13 #include "llvm/Support/Endian.h"
14 
15 using namespace llvm;
16 using namespace llvm::support::endian;
17 using namespace llvm::ELF;
18 using namespace lld;
19 using namespace lld::elf;
20 
21 namespace {
22 class PPC final : public TargetInfo {
23 public:
24   PPC() { GotBaseSymOff = 0x8000; }
25   void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
26   RelExpr getRelExpr(RelType Type, const SymbolBody &S,
27                      const uint8_t *Loc) const override;
28 };
29 } // namespace
30 
31 RelExpr PPC::getRelExpr(RelType Type, const SymbolBody &S,
32                         const uint8_t *Loc) const {
33   switch (Type) {
34   case R_PPC_REL24:
35   case R_PPC_REL32:
36     return R_PC;
37   default:
38     return R_ABS;
39   }
40 }
41 
42 void PPC::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
43   switch (Type) {
44   case R_PPC_ADDR16_HA:
45     write16be(Loc, (Val + 0x8000) >> 16);
46     break;
47   case R_PPC_ADDR16_LO:
48     write16be(Loc, Val);
49     break;
50   case R_PPC_ADDR32:
51   case R_PPC_REL32:
52     write32be(Loc, Val);
53     break;
54   case R_PPC_REL24:
55     write32be(Loc, read32be(Loc) | (Val & 0x3FFFFFC));
56     break;
57   default:
58     error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
59   }
60 }
61 
62 TargetInfo *elf::getPPCTargetInfo() {
63   static PPC Target;
64   return &Target;
65 }
66