xref: /llvm-project-15.0.7/lld/ELF/Arch/PPC.cpp (revision 7fe441b2)
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_HI:
48     write16be(Loc, Val >> 16);
49     break;
50   case R_PPC_ADDR16_LO:
51     write16be(Loc, Val);
52     break;
53   case R_PPC_ADDR32:
54   case R_PPC_REL32:
55     write32be(Loc, Val);
56     break;
57   case R_PPC_REL24:
58     write32be(Loc, read32be(Loc) | (Val & 0x3FFFFFC));
59     break;
60   default:
61     error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
62   }
63 }
64 
65 TargetInfo *elf::getPPCTargetInfo() {
66   static PPC Target;
67   return &Target;
68 }
69