1 //===-- AVRELFObjectWriter.cpp - AVR ELF Writer ---------------------------===//
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 #include "MCTargetDesc/AVRFixupKinds.h"
10 #include "MCTargetDesc/AVRMCExpr.h"
11 #include "MCTargetDesc/AVRMCTargetDesc.h"
12
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Support/ErrorHandling.h"
20
21 namespace llvm {
22
23 /// Writes AVR machine code into an ELF32 object file.
24 class AVRELFObjectWriter : public MCELFObjectTargetWriter {
25 public:
26 AVRELFObjectWriter(uint8_t OSABI);
27
~AVRELFObjectWriter()28 virtual ~AVRELFObjectWriter() {}
29
30 unsigned getRelocType(MCContext &Ctx,
31 const MCValue &Target,
32 const MCFixup &Fixup,
33 bool IsPCRel) const override;
34 };
35
AVRELFObjectWriter(uint8_t OSABI)36 AVRELFObjectWriter::AVRELFObjectWriter(uint8_t OSABI)
37 : MCELFObjectTargetWriter(false, OSABI, ELF::EM_AVR, true) {}
38
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const39 unsigned AVRELFObjectWriter::getRelocType(MCContext &Ctx,
40 const MCValue &Target,
41 const MCFixup &Fixup,
42 bool IsPCRel) const {
43 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
44 switch ((unsigned) Fixup.getKind()) {
45 case FK_Data_1:
46 switch (Modifier) {
47 default:
48 llvm_unreachable("Unsupported Modifier");
49 case MCSymbolRefExpr::VK_None:
50 return ELF::R_AVR_8;
51 case MCSymbolRefExpr::VK_AVR_DIFF8:
52 return ELF::R_AVR_DIFF8;
53 case MCSymbolRefExpr::VK_AVR_LO8:
54 return ELF::R_AVR_8_LO8;
55 case MCSymbolRefExpr::VK_AVR_HI8:
56 return ELF::R_AVR_8_HI8;
57 case MCSymbolRefExpr::VK_AVR_HLO8:
58 return ELF::R_AVR_8_HLO8;
59 }
60 case FK_Data_4:
61 switch (Modifier) {
62 default:
63 llvm_unreachable("Unsupported Modifier");
64 case MCSymbolRefExpr::VK_None:
65 return ELF::R_AVR_32;
66 case MCSymbolRefExpr::VK_AVR_DIFF32:
67 return ELF::R_AVR_DIFF32;
68 }
69 case FK_Data_2:
70 switch (Modifier) {
71 default:
72 llvm_unreachable("Unsupported Modifier");
73 case MCSymbolRefExpr::VK_None:
74 return ELF::R_AVR_16;
75 case MCSymbolRefExpr::VK_AVR_NONE:
76 case MCSymbolRefExpr::VK_AVR_PM:
77 return ELF::R_AVR_16_PM;
78 case MCSymbolRefExpr::VK_AVR_DIFF16:
79 return ELF::R_AVR_DIFF16;
80 }
81 case AVR::fixup_32:
82 return ELF::R_AVR_32;
83 case AVR::fixup_7_pcrel:
84 return ELF::R_AVR_7_PCREL;
85 case AVR::fixup_13_pcrel:
86 return ELF::R_AVR_13_PCREL;
87 case AVR::fixup_16:
88 return ELF::R_AVR_16;
89 case AVR::fixup_16_pm:
90 return ELF::R_AVR_16_PM;
91 case AVR::fixup_lo8_ldi:
92 return ELF::R_AVR_LO8_LDI;
93 case AVR::fixup_hi8_ldi:
94 return ELF::R_AVR_HI8_LDI;
95 case AVR::fixup_hh8_ldi:
96 return ELF::R_AVR_HH8_LDI;
97 case AVR::fixup_lo8_ldi_neg:
98 return ELF::R_AVR_LO8_LDI_NEG;
99 case AVR::fixup_hi8_ldi_neg:
100 return ELF::R_AVR_HI8_LDI_NEG;
101 case AVR::fixup_hh8_ldi_neg:
102 return ELF::R_AVR_HH8_LDI_NEG;
103 case AVR::fixup_lo8_ldi_pm:
104 return ELF::R_AVR_LO8_LDI_PM;
105 case AVR::fixup_hi8_ldi_pm:
106 return ELF::R_AVR_HI8_LDI_PM;
107 case AVR::fixup_hh8_ldi_pm:
108 return ELF::R_AVR_HH8_LDI_PM;
109 case AVR::fixup_lo8_ldi_pm_neg:
110 return ELF::R_AVR_LO8_LDI_PM_NEG;
111 case AVR::fixup_hi8_ldi_pm_neg:
112 return ELF::R_AVR_HI8_LDI_PM_NEG;
113 case AVR::fixup_hh8_ldi_pm_neg:
114 return ELF::R_AVR_HH8_LDI_PM_NEG;
115 case AVR::fixup_call:
116 return ELF::R_AVR_CALL;
117 case AVR::fixup_ldi:
118 return ELF::R_AVR_LDI;
119 case AVR::fixup_6:
120 return ELF::R_AVR_6;
121 case AVR::fixup_6_adiw:
122 return ELF::R_AVR_6_ADIW;
123 case AVR::fixup_ms8_ldi:
124 return ELF::R_AVR_MS8_LDI;
125 case AVR::fixup_ms8_ldi_neg:
126 return ELF::R_AVR_MS8_LDI_NEG;
127 case AVR::fixup_lo8_ldi_gs:
128 return ELF::R_AVR_LO8_LDI_GS;
129 case AVR::fixup_hi8_ldi_gs:
130 return ELF::R_AVR_HI8_LDI_GS;
131 case AVR::fixup_8:
132 return ELF::R_AVR_8;
133 case AVR::fixup_8_lo8:
134 return ELF::R_AVR_8_LO8;
135 case AVR::fixup_8_hi8:
136 return ELF::R_AVR_8_HI8;
137 case AVR::fixup_8_hlo8:
138 return ELF::R_AVR_8_HLO8;
139 case AVR::fixup_diff8:
140 return ELF::R_AVR_DIFF8;
141 case AVR::fixup_diff16:
142 return ELF::R_AVR_DIFF16;
143 case AVR::fixup_diff32:
144 return ELF::R_AVR_DIFF32;
145 case AVR::fixup_lds_sts_16:
146 return ELF::R_AVR_LDS_STS_16;
147 case AVR::fixup_port6:
148 return ELF::R_AVR_PORT6;
149 case AVR::fixup_port5:
150 return ELF::R_AVR_PORT5;
151 default:
152 llvm_unreachable("invalid fixup kind!");
153 }
154 }
155
createAVRELFObjectWriter(uint8_t OSABI)156 std::unique_ptr<MCObjectTargetWriter> createAVRELFObjectWriter(uint8_t OSABI) {
157 return std::make_unique<AVRELFObjectWriter>(OSABI);
158 }
159
160 } // end of namespace llvm
161
162