xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AVR/MCTargetDesc/AVRELFObjectWriter.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===-- AVRELFObjectWriter.cpp - AVR ELF Writer ---------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "MCTargetDesc/AVRFixupKinds.h"
10fe6060f1SDimitry Andric #include "MCTargetDesc/AVRMCExpr.h"
110b57cec5SDimitry Andric #include "MCTargetDesc/AVRMCTargetDesc.h"
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/MC/MCAssembler.h"
140b57cec5SDimitry Andric #include "llvm/MC/MCELFObjectWriter.h"
150b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCObjectWriter.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCSection.h"
180b57cec5SDimitry Andric #include "llvm/MC/MCValue.h"
190b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric namespace llvm {
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric /// Writes AVR machine code into an ELF32 object file.
240b57cec5SDimitry Andric class AVRELFObjectWriter : public MCELFObjectTargetWriter {
250b57cec5SDimitry Andric public:
260b57cec5SDimitry Andric   AVRELFObjectWriter(uint8_t OSABI);
270b57cec5SDimitry Andric 
2881ad6265SDimitry Andric   virtual ~AVRELFObjectWriter() = default;
290b57cec5SDimitry Andric 
30349cc55cSDimitry Andric   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
31349cc55cSDimitry Andric                         const MCFixup &Fixup, bool IsPCRel) const override;
320b57cec5SDimitry Andric };
330b57cec5SDimitry Andric 
AVRELFObjectWriter(uint8_t OSABI)340b57cec5SDimitry Andric AVRELFObjectWriter::AVRELFObjectWriter(uint8_t OSABI)
350b57cec5SDimitry Andric     : MCELFObjectTargetWriter(false, OSABI, ELF::EM_AVR, true) {}
360b57cec5SDimitry Andric 
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const37349cc55cSDimitry Andric unsigned AVRELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
380b57cec5SDimitry Andric                                           const MCFixup &Fixup,
390b57cec5SDimitry Andric                                           bool IsPCRel) const {
40*bdd1243dSDimitry Andric   const unsigned Kind = Fixup.getTargetKind();
41*bdd1243dSDimitry Andric   if (Kind >= FirstLiteralRelocationKind)
42*bdd1243dSDimitry Andric     return Kind - FirstLiteralRelocationKind;
430b57cec5SDimitry Andric   MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
440b57cec5SDimitry Andric   switch ((unsigned)Fixup.getKind()) {
450b57cec5SDimitry Andric   case FK_Data_1:
460b57cec5SDimitry Andric     switch (Modifier) {
470b57cec5SDimitry Andric     default:
480b57cec5SDimitry Andric       llvm_unreachable("Unsupported Modifier");
490b57cec5SDimitry Andric     case MCSymbolRefExpr::VK_None:
500b57cec5SDimitry Andric       return ELF::R_AVR_8;
510b57cec5SDimitry Andric     case MCSymbolRefExpr::VK_AVR_DIFF8:
520b57cec5SDimitry Andric       return ELF::R_AVR_DIFF8;
530b57cec5SDimitry Andric     case MCSymbolRefExpr::VK_AVR_LO8:
540b57cec5SDimitry Andric       return ELF::R_AVR_8_LO8;
550b57cec5SDimitry Andric     case MCSymbolRefExpr::VK_AVR_HI8:
560b57cec5SDimitry Andric       return ELF::R_AVR_8_HI8;
570b57cec5SDimitry Andric     case MCSymbolRefExpr::VK_AVR_HLO8:
580b57cec5SDimitry Andric       return ELF::R_AVR_8_HLO8;
590b57cec5SDimitry Andric     }
600b57cec5SDimitry Andric   case FK_Data_4:
610b57cec5SDimitry Andric     switch (Modifier) {
620b57cec5SDimitry Andric     default:
630b57cec5SDimitry Andric       llvm_unreachable("Unsupported Modifier");
640b57cec5SDimitry Andric     case MCSymbolRefExpr::VK_None:
650b57cec5SDimitry Andric       return ELF::R_AVR_32;
660b57cec5SDimitry Andric     case MCSymbolRefExpr::VK_AVR_DIFF32:
670b57cec5SDimitry Andric       return ELF::R_AVR_DIFF32;
680b57cec5SDimitry Andric     }
690b57cec5SDimitry Andric   case FK_Data_2:
700b57cec5SDimitry Andric     switch (Modifier) {
710b57cec5SDimitry Andric     default:
720b57cec5SDimitry Andric       llvm_unreachable("Unsupported Modifier");
730b57cec5SDimitry Andric     case MCSymbolRefExpr::VK_None:
740b57cec5SDimitry Andric       return ELF::R_AVR_16;
750b57cec5SDimitry Andric     case MCSymbolRefExpr::VK_AVR_NONE:
76fe6060f1SDimitry Andric     case MCSymbolRefExpr::VK_AVR_PM:
770b57cec5SDimitry Andric       return ELF::R_AVR_16_PM;
780b57cec5SDimitry Andric     case MCSymbolRefExpr::VK_AVR_DIFF16:
790b57cec5SDimitry Andric       return ELF::R_AVR_DIFF16;
800b57cec5SDimitry Andric     }
810b57cec5SDimitry Andric   case AVR::fixup_32:
820b57cec5SDimitry Andric     return ELF::R_AVR_32;
830b57cec5SDimitry Andric   case AVR::fixup_7_pcrel:
840b57cec5SDimitry Andric     return ELF::R_AVR_7_PCREL;
850b57cec5SDimitry Andric   case AVR::fixup_13_pcrel:
860b57cec5SDimitry Andric     return ELF::R_AVR_13_PCREL;
870b57cec5SDimitry Andric   case AVR::fixup_16:
880b57cec5SDimitry Andric     return ELF::R_AVR_16;
890b57cec5SDimitry Andric   case AVR::fixup_16_pm:
900b57cec5SDimitry Andric     return ELF::R_AVR_16_PM;
910b57cec5SDimitry Andric   case AVR::fixup_lo8_ldi:
920b57cec5SDimitry Andric     return ELF::R_AVR_LO8_LDI;
930b57cec5SDimitry Andric   case AVR::fixup_hi8_ldi:
940b57cec5SDimitry Andric     return ELF::R_AVR_HI8_LDI;
950b57cec5SDimitry Andric   case AVR::fixup_hh8_ldi:
960b57cec5SDimitry Andric     return ELF::R_AVR_HH8_LDI;
970b57cec5SDimitry Andric   case AVR::fixup_lo8_ldi_neg:
980b57cec5SDimitry Andric     return ELF::R_AVR_LO8_LDI_NEG;
990b57cec5SDimitry Andric   case AVR::fixup_hi8_ldi_neg:
1000b57cec5SDimitry Andric     return ELF::R_AVR_HI8_LDI_NEG;
1010b57cec5SDimitry Andric   case AVR::fixup_hh8_ldi_neg:
1020b57cec5SDimitry Andric     return ELF::R_AVR_HH8_LDI_NEG;
1030b57cec5SDimitry Andric   case AVR::fixup_lo8_ldi_pm:
1040b57cec5SDimitry Andric     return ELF::R_AVR_LO8_LDI_PM;
1050b57cec5SDimitry Andric   case AVR::fixup_hi8_ldi_pm:
1060b57cec5SDimitry Andric     return ELF::R_AVR_HI8_LDI_PM;
1070b57cec5SDimitry Andric   case AVR::fixup_hh8_ldi_pm:
1080b57cec5SDimitry Andric     return ELF::R_AVR_HH8_LDI_PM;
1090b57cec5SDimitry Andric   case AVR::fixup_lo8_ldi_pm_neg:
1100b57cec5SDimitry Andric     return ELF::R_AVR_LO8_LDI_PM_NEG;
1110b57cec5SDimitry Andric   case AVR::fixup_hi8_ldi_pm_neg:
1120b57cec5SDimitry Andric     return ELF::R_AVR_HI8_LDI_PM_NEG;
1130b57cec5SDimitry Andric   case AVR::fixup_hh8_ldi_pm_neg:
1140b57cec5SDimitry Andric     return ELF::R_AVR_HH8_LDI_PM_NEG;
1150b57cec5SDimitry Andric   case AVR::fixup_call:
1160b57cec5SDimitry Andric     return ELF::R_AVR_CALL;
1170b57cec5SDimitry Andric   case AVR::fixup_ldi:
1180b57cec5SDimitry Andric     return ELF::R_AVR_LDI;
1190b57cec5SDimitry Andric   case AVR::fixup_6:
1200b57cec5SDimitry Andric     return ELF::R_AVR_6;
1210b57cec5SDimitry Andric   case AVR::fixup_6_adiw:
1220b57cec5SDimitry Andric     return ELF::R_AVR_6_ADIW;
1230b57cec5SDimitry Andric   case AVR::fixup_ms8_ldi:
1240b57cec5SDimitry Andric     return ELF::R_AVR_MS8_LDI;
1250b57cec5SDimitry Andric   case AVR::fixup_ms8_ldi_neg:
1260b57cec5SDimitry Andric     return ELF::R_AVR_MS8_LDI_NEG;
1270b57cec5SDimitry Andric   case AVR::fixup_lo8_ldi_gs:
1280b57cec5SDimitry Andric     return ELF::R_AVR_LO8_LDI_GS;
1290b57cec5SDimitry Andric   case AVR::fixup_hi8_ldi_gs:
1300b57cec5SDimitry Andric     return ELF::R_AVR_HI8_LDI_GS;
1310b57cec5SDimitry Andric   case AVR::fixup_8:
1320b57cec5SDimitry Andric     return ELF::R_AVR_8;
1330b57cec5SDimitry Andric   case AVR::fixup_8_lo8:
1340b57cec5SDimitry Andric     return ELF::R_AVR_8_LO8;
1350b57cec5SDimitry Andric   case AVR::fixup_8_hi8:
1360b57cec5SDimitry Andric     return ELF::R_AVR_8_HI8;
1370b57cec5SDimitry Andric   case AVR::fixup_8_hlo8:
1380b57cec5SDimitry Andric     return ELF::R_AVR_8_HLO8;
1390b57cec5SDimitry Andric   case AVR::fixup_diff8:
1400b57cec5SDimitry Andric     return ELF::R_AVR_DIFF8;
1410b57cec5SDimitry Andric   case AVR::fixup_diff16:
1420b57cec5SDimitry Andric     return ELF::R_AVR_DIFF16;
1430b57cec5SDimitry Andric   case AVR::fixup_diff32:
1440b57cec5SDimitry Andric     return ELF::R_AVR_DIFF32;
1450b57cec5SDimitry Andric   case AVR::fixup_lds_sts_16:
1460b57cec5SDimitry Andric     return ELF::R_AVR_LDS_STS_16;
1470b57cec5SDimitry Andric   case AVR::fixup_port6:
1480b57cec5SDimitry Andric     return ELF::R_AVR_PORT6;
1490b57cec5SDimitry Andric   case AVR::fixup_port5:
1500b57cec5SDimitry Andric     return ELF::R_AVR_PORT5;
1510b57cec5SDimitry Andric   default:
1520b57cec5SDimitry Andric     llvm_unreachable("invalid fixup kind!");
1530b57cec5SDimitry Andric   }
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
createAVRELFObjectWriter(uint8_t OSABI)1560b57cec5SDimitry Andric std::unique_ptr<MCObjectTargetWriter> createAVRELFObjectWriter(uint8_t OSABI) {
1578bcb0991SDimitry Andric   return std::make_unique<AVRELFObjectWriter>(OSABI);
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric } // end of namespace llvm
161