1 //===-- AVRELFObjectWriter.cpp - AVR ELF Writer ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
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 "MCTargetDesc/AVRFixupKinds.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 
28   virtual ~AVRELFObjectWriter() {}
29 
30   unsigned getRelocType(MCContext &Ctx,
31                         const MCValue &Target,
32                         const MCFixup &Fixup,
33                         bool IsPCRel) const override;
34 };
35 
36 AVRELFObjectWriter::AVRELFObjectWriter(uint8_t OSABI)
37     : MCELFObjectTargetWriter(false, OSABI, ELF::EM_AVR, true) {}
38 
39 unsigned AVRELFObjectWriter::getRelocType(MCContext &Ctx,
40                                           const MCValue &Target,
41                                           const MCFixup &Fixup,
42                                           bool IsPCRel) const {
43   switch ((unsigned) Fixup.getKind()) {
44   case FK_Data_1:
45   case FK_Data_4:
46     llvm_unreachable("unsupported relocation type");
47   case FK_Data_2:
48     return ELF::R_AVR_16_PM;
49   case AVR::fixup_32:
50     return ELF::R_AVR_32;
51   case AVR::fixup_7_pcrel:
52     return ELF::R_AVR_7_PCREL;
53   case AVR::fixup_13_pcrel:
54     return ELF::R_AVR_13_PCREL;
55   case AVR::fixup_16:
56     return ELF::R_AVR_16;
57   case AVR::fixup_16_pm:
58     return ELF::R_AVR_16_PM;
59   case AVR::fixup_lo8_ldi:
60     return ELF::R_AVR_LO8_LDI;
61   case AVR::fixup_hi8_ldi:
62     return ELF::R_AVR_HI8_LDI;
63   case AVR::fixup_hh8_ldi:
64     return ELF::R_AVR_HH8_LDI;
65   case AVR::fixup_lo8_ldi_neg:
66     return ELF::R_AVR_LO8_LDI_NEG;
67   case AVR::fixup_hi8_ldi_neg:
68     return ELF::R_AVR_HI8_LDI_NEG;
69   case AVR::fixup_hh8_ldi_neg:
70     return ELF::R_AVR_HH8_LDI_NEG;
71   case AVR::fixup_lo8_ldi_pm:
72     return ELF::R_AVR_LO8_LDI_PM;
73   case AVR::fixup_hi8_ldi_pm:
74     return ELF::R_AVR_HI8_LDI_PM;
75   case AVR::fixup_hh8_ldi_pm:
76     return ELF::R_AVR_HH8_LDI_PM;
77   case AVR::fixup_lo8_ldi_pm_neg:
78     return ELF::R_AVR_LO8_LDI_PM_NEG;
79   case AVR::fixup_hi8_ldi_pm_neg:
80     return ELF::R_AVR_HI8_LDI_PM_NEG;
81   case AVR::fixup_hh8_ldi_pm_neg:
82     return ELF::R_AVR_HH8_LDI_PM_NEG;
83   case AVR::fixup_call:
84     return ELF::R_AVR_CALL;
85   case AVR::fixup_ldi:
86     return ELF::R_AVR_LDI;
87   case AVR::fixup_6:
88     return ELF::R_AVR_6;
89   case AVR::fixup_6_adiw:
90     return ELF::R_AVR_6_ADIW;
91   case AVR::fixup_ms8_ldi:
92     return ELF::R_AVR_MS8_LDI;
93   case AVR::fixup_ms8_ldi_neg:
94     return ELF::R_AVR_MS8_LDI_NEG;
95   case AVR::fixup_lo8_ldi_gs:
96     return ELF::R_AVR_LO8_LDI_GS;
97   case AVR::fixup_hi8_ldi_gs:
98     return ELF::R_AVR_HI8_LDI_GS;
99   case AVR::fixup_8:
100     return ELF::R_AVR_8;
101   case AVR::fixup_8_lo8:
102     return ELF::R_AVR_8_LO8;
103   case AVR::fixup_8_hi8:
104     return ELF::R_AVR_8_HI8;
105   case AVR::fixup_8_hlo8:
106     return ELF::R_AVR_8_HLO8;
107   case AVR::fixup_sym_diff:
108     return ELF::R_AVR_SYM_DIFF;
109   case AVR::fixup_16_ldst:
110     return ELF::R_AVR_16_LDST;
111   case AVR::fixup_lds_sts_16:
112     return ELF::R_AVR_LDS_STS_16;
113   case AVR::fixup_port6:
114     return ELF::R_AVR_PORT6;
115   case AVR::fixup_port5:
116     return ELF::R_AVR_PORT5;
117   default:
118     llvm_unreachable("invalid fixup kind!");
119   }
120 }
121 
122 std::unique_ptr<MCObjectWriter>
123 createAVRELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI) {
124   std::unique_ptr<MCELFObjectTargetWriter> MOTW(new AVRELFObjectWriter(OSABI));
125   return createELFObjectWriter(std::move(MOTW), OS, true);
126 }
127 
128 } // end of namespace llvm
129 
130