xref: /llvm-project-15.0.7/lld/ELF/Arch/MSP430.cpp (revision 90e4ebdc)
1 //===- MSP430.cpp ---------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The MSP430 is a 16-bit microcontroller RISC architecture. The instruction set
10 // has only 27 core instructions orthogonally augmented with a variety
11 // of addressing modes for source and destination operands. Entire address space
12 // of MSP430 is 64KB (the extended MSP430X architecture is not considered here).
13 // A typical MSP430 MCU has several kilobytes of RAM and ROM, plenty
14 // of peripherals and is generally optimized for a low power consumption.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "InputFiles.h"
19 #include "Symbols.h"
20 #include "Target.h"
21 #include "lld/Common/ErrorHandler.h"
22 #include "llvm/Object/ELF.h"
23 #include "llvm/Support/Endian.h"
24 
25 using namespace llvm;
26 using namespace llvm::object;
27 using namespace llvm::support::endian;
28 using namespace llvm::ELF;
29 
30 namespace lld {
31 namespace elf {
32 
33 namespace {
34 class MSP430 final : public TargetInfo {
35 public:
36   MSP430();
37   RelExpr getRelExpr(RelType type, const Symbol &s,
38                      const uint8_t *loc) const override;
39   void relocate(uint8_t *loc, const Relocation &rel,
40                 uint64_t val) const override;
41 };
42 } // namespace
43 
44 MSP430::MSP430() {
45   // mov.b #0, r3
46   trapInstr = {0x43, 0x43, 0x43, 0x43};
47 }
48 
49 RelExpr MSP430::getRelExpr(RelType type, const Symbol &s,
50                            const uint8_t *loc) const {
51   switch (type) {
52   case R_MSP430_10_PCREL:
53   case R_MSP430_16_PCREL:
54   case R_MSP430_16_PCREL_BYTE:
55   case R_MSP430_2X_PCREL:
56   case R_MSP430_RL_PCREL:
57   case R_MSP430_SYM_DIFF:
58     return R_PC;
59   default:
60     return R_ABS;
61   }
62 }
63 
64 void MSP430::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
65   switch (rel.type) {
66   case R_MSP430_8:
67     checkIntUInt(loc, val, 8, rel);
68     *loc = val;
69     break;
70   case R_MSP430_16:
71   case R_MSP430_16_PCREL:
72   case R_MSP430_16_BYTE:
73   case R_MSP430_16_PCREL_BYTE:
74     checkIntUInt(loc, val, 16, rel);
75     write16le(loc, val);
76     break;
77   case R_MSP430_32:
78     checkIntUInt(loc, val, 32, rel);
79     write32le(loc, val);
80     break;
81   case R_MSP430_10_PCREL: {
82     int16_t offset = ((int16_t)val >> 1) - 1;
83     checkInt(loc, offset, 10, rel);
84     write16le(loc, (read16le(loc) & 0xFC00) | (offset & 0x3FF));
85     break;
86   }
87   default:
88     error(getErrorLocation(loc) + "unrecognized relocation " +
89           toString(rel.type));
90   }
91 }
92 
93 TargetInfo *getMSP430TargetInfo() {
94   static MSP430 target;
95   return ⌖
96 }
97 
98 } // namespace elf
99 } // namespace lld
100