xref: /freebsd/contrib/llvm-project/lld/ELF/Arch/AVR.cpp (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 //===- AVR.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 // AVR is a Harvard-architecture 8-bit micrcontroller designed for small
10 // baremetal programs. All AVR-family processors have 32 8-bit registers.
11 // The tiniest AVR has 32 byte RAM and 1 KiB program memory, and the largest
12 // one supports up to 2^24 data address space and 2^22 code address space.
13 //
14 // Since it is a baremetal programming, there's usually no loader to load
15 // ELF files on AVRs. You are expected to link your program against address
16 // 0 and pull out a .text section from the result using objcopy, so that you
17 // can write the linked code to on-chip flush memory. You can do that with
18 // the following commands:
19 //
20 //   ld.lld -Ttext=0 -o foo foo.o
21 //   objcopy -O binary --only-section=.text foo output.bin
22 //
23 // Note that the current AVR support is very preliminary so you can't
24 // link any useful program yet, though.
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #include "InputFiles.h"
29 #include "Symbols.h"
30 #include "Target.h"
31 #include "lld/Common/ErrorHandler.h"
32 #include "llvm/Object/ELF.h"
33 #include "llvm/Support/Endian.h"
34 
35 using namespace llvm;
36 using namespace llvm::object;
37 using namespace llvm::support::endian;
38 using namespace llvm::ELF;
39 using namespace lld;
40 using namespace lld::elf;
41 
42 namespace {
43 class AVR final : public TargetInfo {
44 public:
45   AVR();
46   RelExpr getRelExpr(RelType type, const Symbol &s,
47                      const uint8_t *loc) const override;
48   void relocate(uint8_t *loc, const Relocation &rel,
49                 uint64_t val) const override;
50 };
51 } // namespace
52 
53 AVR::AVR() { noneRel = R_AVR_NONE; }
54 
55 RelExpr AVR::getRelExpr(RelType type, const Symbol &s,
56                         const uint8_t *loc) const {
57   switch (type) {
58   case R_AVR_7_PCREL:
59   case R_AVR_13_PCREL:
60     return R_PC;
61   default:
62     return R_ABS;
63   }
64 }
65 
66 static void writeLDI(uint8_t *loc, uint64_t val) {
67   write16le(loc, (read16le(loc) & 0xf0f0) | (val & 0xf0) << 4 | (val & 0x0f));
68 }
69 
70 void AVR::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
71   switch (rel.type) {
72   case R_AVR_8:
73     checkUInt(loc, val, 8, rel);
74     *loc = val;
75     break;
76   case R_AVR_16:
77     // Note: this relocation is often used between code and data space, which
78     // are 0x800000 apart in the output ELF file. The bitmask cuts off the high
79     // bit.
80     write16le(loc, val & 0xffff);
81     break;
82   case R_AVR_16_PM:
83     checkAlignment(loc, val, 2, rel);
84     checkUInt(loc, val >> 1, 16, rel);
85     write16le(loc, val >> 1);
86     break;
87   case R_AVR_32:
88     checkUInt(loc, val, 32, rel);
89     write32le(loc, val);
90     break;
91 
92   case R_AVR_LDI:
93     checkUInt(loc, val, 8, rel);
94     writeLDI(loc, val & 0xff);
95     break;
96 
97   case R_AVR_LO8_LDI_NEG:
98     writeLDI(loc, -val & 0xff);
99     break;
100   case R_AVR_LO8_LDI:
101     writeLDI(loc, val & 0xff);
102     break;
103   case R_AVR_HI8_LDI_NEG:
104     writeLDI(loc, (-val >> 8) & 0xff);
105     break;
106   case R_AVR_HI8_LDI:
107     writeLDI(loc, (val >> 8) & 0xff);
108     break;
109   case R_AVR_HH8_LDI_NEG:
110     writeLDI(loc, (-val >> 16) & 0xff);
111     break;
112   case R_AVR_HH8_LDI:
113     writeLDI(loc, (val >> 16) & 0xff);
114     break;
115   case R_AVR_MS8_LDI_NEG:
116     writeLDI(loc, (-val >> 24) & 0xff);
117     break;
118   case R_AVR_MS8_LDI:
119     writeLDI(loc, (val >> 24) & 0xff);
120     break;
121 
122   case R_AVR_LO8_LDI_PM:
123     checkAlignment(loc, val, 2, rel);
124     writeLDI(loc, (val >> 1) & 0xff);
125     break;
126   case R_AVR_HI8_LDI_PM:
127     checkAlignment(loc, val, 2, rel);
128     writeLDI(loc, (val >> 9) & 0xff);
129     break;
130   case R_AVR_HH8_LDI_PM:
131     checkAlignment(loc, val, 2, rel);
132     writeLDI(loc, (val >> 17) & 0xff);
133     break;
134 
135   case R_AVR_LO8_LDI_PM_NEG:
136     checkAlignment(loc, val, 2, rel);
137     writeLDI(loc, (-val >> 1) & 0xff);
138     break;
139   case R_AVR_HI8_LDI_PM_NEG:
140     checkAlignment(loc, val, 2, rel);
141     writeLDI(loc, (-val >> 9) & 0xff);
142     break;
143   case R_AVR_HH8_LDI_PM_NEG:
144     checkAlignment(loc, val, 2, rel);
145     writeLDI(loc, (-val >> 17) & 0xff);
146     break;
147 
148   case R_AVR_PORT5:
149     checkUInt(loc, val, 5, rel);
150     write16le(loc, (read16le(loc) & 0xff07) | (val << 3));
151     break;
152   case R_AVR_PORT6:
153     checkUInt(loc, val, 6, rel);
154     write16le(loc, (read16le(loc) & 0xf9f0) | (val & 0x30) << 5 | (val & 0x0f));
155     break;
156 
157   // Since every jump destination is word aligned we gain an extra bit
158   case R_AVR_7_PCREL: {
159     checkInt(loc, val, 7, rel);
160     checkAlignment(loc, val, 2, rel);
161     const uint16_t target = (val - 2) >> 1;
162     write16le(loc, (read16le(loc) & 0xfc07) | ((target & 0x7f) << 3));
163     break;
164   }
165   case R_AVR_13_PCREL: {
166     checkAlignment(loc, val, 2, rel);
167     const uint16_t target = (val - 2) >> 1;
168     write16le(loc, (read16le(loc) & 0xf000) | (target & 0xfff));
169     break;
170   }
171 
172   case R_AVR_6:
173     checkInt(loc, val, 6, rel);
174     write16le(loc, (read16le(loc) & 0xd3f8) | (val & 0x20) << 8 |
175                        (val & 0x18) << 7 | (val & 0x07));
176     break;
177   case R_AVR_6_ADIW:
178     checkInt(loc, val, 6, rel);
179     write16le(loc, (read16le(loc) & 0xff30) | (val & 0x30) << 2 | (val & 0x0F));
180     break;
181 
182   case R_AVR_CALL: {
183     uint16_t hi = val >> 17;
184     uint16_t lo = val >> 1;
185     write16le(loc, read16le(loc) | ((hi >> 1) << 4) | (hi & 1));
186     write16le(loc + 2, lo);
187     break;
188   }
189   default:
190     error(getErrorLocation(loc) + "unrecognized relocation " +
191           toString(rel.type));
192   }
193 }
194 
195 TargetInfo *elf::getAVRTargetInfo() {
196   static AVR target;
197   return &target;
198 }
199