104eeddc0SDimitry Andric //===-- M68kELFObjectWriter.cpp - M68k ELF Writer ---------------*- C++ -*-===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric ///
9fe6060f1SDimitry Andric /// \file
10fe6060f1SDimitry Andric /// This file contains definitions for M68k ELF Writers
11fe6060f1SDimitry Andric ///
12fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
13fe6060f1SDimitry Andric
14fe6060f1SDimitry Andric #include "MCTargetDesc/M68kFixupKinds.h"
15fe6060f1SDimitry Andric #include "MCTargetDesc/M68kMCTargetDesc.h"
16fe6060f1SDimitry Andric
17fe6060f1SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
18fe6060f1SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
19fe6060f1SDimitry Andric #include "llvm/MC/MCContext.h"
20fe6060f1SDimitry Andric #include "llvm/MC/MCELFObjectWriter.h"
21fe6060f1SDimitry Andric #include "llvm/MC/MCExpr.h"
22fe6060f1SDimitry Andric #include "llvm/MC/MCValue.h"
23fe6060f1SDimitry Andric #include "llvm/Support/ErrorHandling.h"
24fe6060f1SDimitry Andric
25fe6060f1SDimitry Andric using namespace llvm;
26fe6060f1SDimitry Andric
27fe6060f1SDimitry Andric namespace {
28fe6060f1SDimitry Andric class M68kELFObjectWriter : public MCELFObjectTargetWriter {
29fe6060f1SDimitry Andric public:
30fe6060f1SDimitry Andric M68kELFObjectWriter(uint8_t OSABI);
31fe6060f1SDimitry Andric
32fe6060f1SDimitry Andric ~M68kELFObjectWriter() override;
33fe6060f1SDimitry Andric
34fe6060f1SDimitry Andric protected:
35fe6060f1SDimitry Andric unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
36fe6060f1SDimitry Andric const MCFixup &Fixup, bool IsPCRel) const override;
37fe6060f1SDimitry Andric };
38fe6060f1SDimitry Andric } // namespace
39fe6060f1SDimitry Andric
M68kELFObjectWriter(uint8_t OSABI)40fe6060f1SDimitry Andric M68kELFObjectWriter::M68kELFObjectWriter(uint8_t OSABI)
41fe6060f1SDimitry Andric : MCELFObjectTargetWriter(false, OSABI, ELF::EM_68K, /* RELA */ true) {}
42fe6060f1SDimitry Andric
~M68kELFObjectWriter()43fe6060f1SDimitry Andric M68kELFObjectWriter::~M68kELFObjectWriter() {}
44fe6060f1SDimitry Andric
45fe6060f1SDimitry Andric enum M68kRelType { RT_32, RT_16, RT_8 };
46fe6060f1SDimitry Andric
47fe6060f1SDimitry Andric static M68kRelType
getType(unsigned Kind,MCSymbolRefExpr::VariantKind & Modifier,bool & IsPCRel)48fe6060f1SDimitry Andric getType(unsigned Kind, MCSymbolRefExpr::VariantKind &Modifier, bool &IsPCRel) {
49fe6060f1SDimitry Andric switch (Kind) {
50fe6060f1SDimitry Andric case FK_Data_4:
51fe6060f1SDimitry Andric case FK_PCRel_4:
52fe6060f1SDimitry Andric return RT_32;
53fe6060f1SDimitry Andric case FK_PCRel_2:
54fe6060f1SDimitry Andric case FK_Data_2:
55fe6060f1SDimitry Andric return RT_16;
56fe6060f1SDimitry Andric case FK_PCRel_1:
57fe6060f1SDimitry Andric case FK_Data_1:
58fe6060f1SDimitry Andric return RT_8;
59fe6060f1SDimitry Andric }
60fe6060f1SDimitry Andric llvm_unreachable("Unimplemented");
61fe6060f1SDimitry Andric }
62fe6060f1SDimitry Andric
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const63fe6060f1SDimitry Andric unsigned M68kELFObjectWriter::getRelocType(MCContext &Ctx,
64fe6060f1SDimitry Andric const MCValue &Target,
65fe6060f1SDimitry Andric const MCFixup &Fixup,
66fe6060f1SDimitry Andric bool IsPCRel) const {
67fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
68fe6060f1SDimitry Andric unsigned Kind = Fixup.getKind();
69fe6060f1SDimitry Andric M68kRelType Type = getType(Kind, Modifier, IsPCRel);
70fe6060f1SDimitry Andric switch (Modifier) {
71fe6060f1SDimitry Andric default:
72fe6060f1SDimitry Andric llvm_unreachable("Unimplemented");
73*06c3fb27SDimitry Andric
74*06c3fb27SDimitry Andric case MCSymbolRefExpr::VK_TLSGD:
75*06c3fb27SDimitry Andric switch (Type) {
76*06c3fb27SDimitry Andric case RT_32:
77*06c3fb27SDimitry Andric return ELF::R_68K_TLS_GD32;
78*06c3fb27SDimitry Andric case RT_16:
79*06c3fb27SDimitry Andric return ELF::R_68K_TLS_GD16;
80*06c3fb27SDimitry Andric case RT_8:
81*06c3fb27SDimitry Andric return ELF::R_68K_TLS_GD8;
82*06c3fb27SDimitry Andric }
83*06c3fb27SDimitry Andric llvm_unreachable("Unrecognized size");
84*06c3fb27SDimitry Andric case MCSymbolRefExpr::VK_TLSLDM:
85*06c3fb27SDimitry Andric switch (Type) {
86*06c3fb27SDimitry Andric case RT_32:
87*06c3fb27SDimitry Andric return ELF::R_68K_TLS_LDM32;
88*06c3fb27SDimitry Andric case RT_16:
89*06c3fb27SDimitry Andric return ELF::R_68K_TLS_LDM16;
90*06c3fb27SDimitry Andric case RT_8:
91*06c3fb27SDimitry Andric return ELF::R_68K_TLS_LDM8;
92*06c3fb27SDimitry Andric }
93*06c3fb27SDimitry Andric llvm_unreachable("Unrecognized size");
94*06c3fb27SDimitry Andric case MCSymbolRefExpr::VK_TLSLD:
95*06c3fb27SDimitry Andric switch (Type) {
96*06c3fb27SDimitry Andric case RT_32:
97*06c3fb27SDimitry Andric return ELF::R_68K_TLS_LDO32;
98*06c3fb27SDimitry Andric case RT_16:
99*06c3fb27SDimitry Andric return ELF::R_68K_TLS_LDO16;
100*06c3fb27SDimitry Andric case RT_8:
101*06c3fb27SDimitry Andric return ELF::R_68K_TLS_LDO8;
102*06c3fb27SDimitry Andric }
103*06c3fb27SDimitry Andric llvm_unreachable("Unrecognized size");
104*06c3fb27SDimitry Andric case MCSymbolRefExpr::VK_GOTTPOFF:
105*06c3fb27SDimitry Andric switch (Type) {
106*06c3fb27SDimitry Andric case RT_32:
107*06c3fb27SDimitry Andric return ELF::R_68K_TLS_IE32;
108*06c3fb27SDimitry Andric case RT_16:
109*06c3fb27SDimitry Andric return ELF::R_68K_TLS_IE16;
110*06c3fb27SDimitry Andric case RT_8:
111*06c3fb27SDimitry Andric return ELF::R_68K_TLS_IE8;
112*06c3fb27SDimitry Andric }
113*06c3fb27SDimitry Andric llvm_unreachable("Unrecognized size");
114*06c3fb27SDimitry Andric case MCSymbolRefExpr::VK_TPOFF:
115*06c3fb27SDimitry Andric switch (Type) {
116*06c3fb27SDimitry Andric case RT_32:
117*06c3fb27SDimitry Andric return ELF::R_68K_TLS_LE32;
118*06c3fb27SDimitry Andric case RT_16:
119*06c3fb27SDimitry Andric return ELF::R_68K_TLS_LE16;
120*06c3fb27SDimitry Andric case RT_8:
121*06c3fb27SDimitry Andric return ELF::R_68K_TLS_LE8;
122*06c3fb27SDimitry Andric }
123*06c3fb27SDimitry Andric llvm_unreachable("Unrecognized size");
124fe6060f1SDimitry Andric case MCSymbolRefExpr::VK_None:
125fe6060f1SDimitry Andric switch (Type) {
126fe6060f1SDimitry Andric case RT_32:
127fe6060f1SDimitry Andric return IsPCRel ? ELF::R_68K_PC32 : ELF::R_68K_32;
128fe6060f1SDimitry Andric case RT_16:
129fe6060f1SDimitry Andric return IsPCRel ? ELF::R_68K_PC16 : ELF::R_68K_16;
130fe6060f1SDimitry Andric case RT_8:
131fe6060f1SDimitry Andric return IsPCRel ? ELF::R_68K_PC8 : ELF::R_68K_8;
132fe6060f1SDimitry Andric }
133fe6060f1SDimitry Andric llvm_unreachable("Unrecognized size");
134fe6060f1SDimitry Andric case MCSymbolRefExpr::VK_GOTPCREL:
135fe6060f1SDimitry Andric switch (Type) {
136fe6060f1SDimitry Andric case RT_32:
137fe6060f1SDimitry Andric return ELF::R_68K_GOTPCREL32;
138fe6060f1SDimitry Andric case RT_16:
139fe6060f1SDimitry Andric return ELF::R_68K_GOTPCREL16;
140fe6060f1SDimitry Andric case RT_8:
141fe6060f1SDimitry Andric return ELF::R_68K_GOTPCREL8;
142fe6060f1SDimitry Andric }
143fe6060f1SDimitry Andric llvm_unreachable("Unrecognized size");
144fe6060f1SDimitry Andric case MCSymbolRefExpr::VK_GOTOFF:
145fe6060f1SDimitry Andric assert(!IsPCRel);
146fe6060f1SDimitry Andric switch (Type) {
147fe6060f1SDimitry Andric case RT_32:
148fe6060f1SDimitry Andric return ELF::R_68K_GOTOFF32;
149fe6060f1SDimitry Andric case RT_16:
150fe6060f1SDimitry Andric return ELF::R_68K_GOTOFF16;
151fe6060f1SDimitry Andric case RT_8:
152fe6060f1SDimitry Andric return ELF::R_68K_GOTOFF8;
153fe6060f1SDimitry Andric }
154fe6060f1SDimitry Andric llvm_unreachable("Unrecognized size");
155fe6060f1SDimitry Andric case MCSymbolRefExpr::VK_PLT:
156fe6060f1SDimitry Andric switch (Type) {
157fe6060f1SDimitry Andric case RT_32:
158fe6060f1SDimitry Andric return ELF::R_68K_PLT32;
159fe6060f1SDimitry Andric case RT_16:
160fe6060f1SDimitry Andric return ELF::R_68K_PLT16;
161fe6060f1SDimitry Andric case RT_8:
162fe6060f1SDimitry Andric return ELF::R_68K_PLT8;
163fe6060f1SDimitry Andric }
164fe6060f1SDimitry Andric llvm_unreachable("Unrecognized size");
165fe6060f1SDimitry Andric }
166fe6060f1SDimitry Andric }
167fe6060f1SDimitry Andric
168fe6060f1SDimitry Andric std::unique_ptr<MCObjectTargetWriter>
createM68kELFObjectWriter(uint8_t OSABI)169fe6060f1SDimitry Andric llvm::createM68kELFObjectWriter(uint8_t OSABI) {
170fe6060f1SDimitry Andric return std::make_unique<M68kELFObjectWriter>(OSABI);
171fe6060f1SDimitry Andric }
172