xref: /llvm-project-15.0.7/lld/ELF/Arch/PPC.cpp (revision 22bdb331)
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 "Symbols.h"
11 #include "Target.h"
12 #include "lld/Common/ErrorHandler.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();
25   void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
26   RelExpr getRelExpr(RelType Type, const Symbol &S,
27                      const uint8_t *Loc) const override;
28 };
29 } // namespace
30 
31 PPC::PPC() {
32   NoneRel = R_PPC_NONE;
33   GotBaseSymOff = 0x8000;
34   GotBaseSymInGotPlt = false;
35 }
36 
37 RelExpr PPC::getRelExpr(RelType Type, const Symbol &S,
38                         const uint8_t *Loc) const {
39   switch (Type) {
40   case R_PPC_REL24:
41   case R_PPC_REL32:
42     return R_PC;
43   case R_PPC_PLTREL24:
44     return R_PLT_PC;
45   default:
46     return R_ABS;
47   }
48 }
49 
50 void PPC::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
51   switch (Type) {
52   case R_PPC_ADDR16_HA:
53     write16be(Loc, (Val + 0x8000) >> 16);
54     break;
55   case R_PPC_ADDR16_HI:
56     write16be(Loc, Val >> 16);
57     break;
58   case R_PPC_ADDR16_LO:
59     write16be(Loc, Val);
60     break;
61   case R_PPC_ADDR32:
62   case R_PPC_REL32:
63     write32be(Loc, Val);
64     break;
65   case R_PPC_PLTREL24:
66   case R_PPC_REL24:
67     write32be(Loc, read32be(Loc) | (Val & 0x3FFFFFC));
68     break;
69   default:
70     error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
71   }
72 }
73 
74 TargetInfo *elf::getPPCTargetInfo() {
75   static PPC Target;
76   return &Target;
77 }
78