1 //===- MSP430.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 // The MSP430 is a 16-bit microcontroller RISC architecture. The instruction set
11 // has only 27 core instructions orthogonally augmented with a variety
12 // of addressing modes for source and destination operands. Entire address space
13 // of MSP430 is 64KB (the extended MSP430X architecture is not considered here).
14 // A typical MSP430 MCU has several kilobytes of RAM and ROM, plenty
15 // of peripherals and is generally optimized for a low power consumption.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "InputFiles.h"
20 #include "Symbols.h"
21 #include "Target.h"
22 #include "lld/Common/ErrorHandler.h"
23 #include "llvm/Object/ELF.h"
24 #include "llvm/Support/Endian.h"
25
26 using namespace llvm;
27 using namespace llvm::object;
28 using namespace llvm::support::endian;
29 using namespace llvm::ELF;
30 using namespace lld;
31 using namespace lld::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 relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
40 };
41 } // namespace
42
MSP430()43 MSP430::MSP430() {
44 // mov.b #0, r3
45 TrapInstr = {0x43, 0x43, 0x43, 0x43};
46 }
47
getRelExpr(RelType Type,const Symbol & S,const uint8_t * Loc) const48 RelExpr MSP430::getRelExpr(RelType Type, const Symbol &S,
49 const uint8_t *Loc) const {
50 switch (Type) {
51 case R_MSP430_10_PCREL:
52 case R_MSP430_16_PCREL:
53 case R_MSP430_16_PCREL_BYTE:
54 case R_MSP430_2X_PCREL:
55 case R_MSP430_RL_PCREL:
56 case R_MSP430_SYM_DIFF:
57 return R_PC;
58 default:
59 return R_ABS;
60 }
61 }
62
relocateOne(uint8_t * Loc,RelType Type,uint64_t Val) const63 void MSP430::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
64 switch (Type) {
65 case R_MSP430_8:
66 checkIntUInt(Loc, Val, 8, Type);
67 *Loc = Val;
68 break;
69 case R_MSP430_16:
70 case R_MSP430_16_PCREL:
71 case R_MSP430_16_BYTE:
72 case R_MSP430_16_PCREL_BYTE:
73 checkIntUInt(Loc, Val, 16, Type);
74 write16le(Loc, Val);
75 break;
76 case R_MSP430_32:
77 checkIntUInt(Loc, Val, 32, Type);
78 write32le(Loc, Val);
79 break;
80 case R_MSP430_10_PCREL: {
81 int16_t Offset = ((int16_t)Val >> 1) - 1;
82 checkInt(Loc, Offset, 10, Type);
83 write16le(Loc, (read16le(Loc) & 0xFC00) | (Offset & 0x3FF));
84 break;
85 }
86 default:
87 error(getErrorLocation(Loc) + "unrecognized reloc " + toString(Type));
88 }
89 }
90
getMSP430TargetInfo()91 TargetInfo *elf::getMSP430TargetInfo() {
92 static MSP430 Target;
93 return &Target;
94 }
95