blob: cf110345df3de13df73480094c331fc02ae84046 [file] [log] [blame]
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +00001//===- MCAsmBackend.cpp - Target MC Assembly Backend ----------------------===//
Daniel Dunbar3bea9bf2010-02-21 21:53:53 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Daniel Dunbar3bea9bf2010-02-21 21:53:53 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth6bda14b2017-06-06 11:49:48 +00009#include "llvm/MC/MCAsmBackend.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000010#include "llvm/ADT/None.h"
Craig Topper58713212013-07-15 04:27:47 +000011#include "llvm/ADT/STLExtras.h"
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000012#include "llvm/MC/MCELFObjectWriter.h"
Craig Topper6e80c282012-03-26 06:58:25 +000013#include "llvm/MC/MCFixupKindInfo.h"
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000014#include "llvm/MC/MCMachObjectWriter.h"
15#include "llvm/MC/MCObjectWriter.h"
16#include "llvm/MC/MCWasmObjectWriter.h"
17#include "llvm/MC/MCWinCOFFObjectWriter.h"
Sean Fertilef09d54e2019-07-09 19:21:01 +000018#include "llvm/MC/MCXCOFFObjectWriter.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000019#include <cassert>
20#include <cstddef>
21#include <cstdint>
22
Daniel Dunbar3bea9bf2010-02-21 21:53:53 +000023using namespace llvm;
24
Fangrui Song95747572019-12-05 18:18:55 -080025MCAsmBackend::MCAsmBackend(support::endianness Endian) : Endian(Endian) {}
Daniel Dunbar3bea9bf2010-02-21 21:53:53 +000026
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000027MCAsmBackend::~MCAsmBackend() = default;
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000028
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000029std::unique_ptr<MCObjectWriter>
30MCAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
31 auto TW = createObjectTargetWriter();
32 switch (TW->getFormat()) {
33 case Triple::ELF:
34 return createELFObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)), OS,
35 Endian == support::little);
36 case Triple::MachO:
37 return createMachObjectWriter(cast<MCMachObjectTargetWriter>(std::move(TW)),
38 OS, Endian == support::little);
39 case Triple::COFF:
40 return createWinCOFFObjectWriter(
41 cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS);
42 case Triple::Wasm:
43 return createWasmObjectWriter(cast<MCWasmObjectTargetWriter>(std::move(TW)),
44 OS);
Sean Fertilef09d54e2019-07-09 19:21:01 +000045 case Triple::XCOFF:
46 return createXCOFFObjectWriter(
47 cast<MCXCOFFObjectTargetWriter>(std::move(TW)), OS);
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000048 default:
49 llvm_unreachable("unexpected object format");
50 }
51}
52
Peter Collingbourne63062d92018-05-21 19:44:54 +000053std::unique_ptr<MCObjectWriter>
54MCAsmBackend::createDwoObjectWriter(raw_pwrite_stream &OS,
55 raw_pwrite_stream &DwoOS) const {
56 auto TW = createObjectTargetWriter();
57 if (TW->getFormat() != Triple::ELF)
58 report_fatal_error("dwo only supported with ELF");
59 return createELFDwoObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)),
60 OS, DwoOS, Endian == support::little);
61}
62
David Majnemerce108422016-01-19 23:05:27 +000063Optional<MCFixupKind> MCAsmBackend::getFixupKind(StringRef Name) const {
64 return None;
Daniel Sanders9f6ad492015-11-12 13:33:00 +000065}
66
Colin LeMahieua01780f2015-05-30 18:42:22 +000067const MCFixupKindInfo &MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000068 static const MCFixupKindInfo Builtins[] = {
Fangrui Song43ca0e92019-05-17 02:51:54 +000069 {"FK_NONE", 0, 0, 0},
Colin LeMahieua01780f2015-05-30 18:42:22 +000070 {"FK_Data_1", 0, 8, 0},
71 {"FK_Data_2", 0, 16, 0},
72 {"FK_Data_4", 0, 32, 0},
73 {"FK_Data_8", 0, 64, 0},
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +000074 {"FK_Data_6b", 0, 6, 0},
Colin LeMahieua01780f2015-05-30 18:42:22 +000075 {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel},
76 {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
77 {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
78 {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel},
79 {"FK_GPRel_1", 0, 8, 0},
80 {"FK_GPRel_2", 0, 16, 0},
81 {"FK_GPRel_4", 0, 32, 0},
82 {"FK_GPRel_8", 0, 64, 0},
Simon Atanasyaneb9ed612016-08-22 16:18:42 +000083 {"FK_DTPRel_4", 0, 32, 0},
84 {"FK_DTPRel_8", 0, 64, 0},
85 {"FK_TPRel_4", 0, 32, 0},
86 {"FK_TPRel_8", 0, 64, 0},
Colin LeMahieua01780f2015-05-30 18:42:22 +000087 {"FK_SecRel_1", 0, 8, 0},
88 {"FK_SecRel_2", 0, 16, 0},
89 {"FK_SecRel_4", 0, 32, 0},
Alex Bradbury257d5b52018-05-23 12:36:18 +000090 {"FK_SecRel_8", 0, 64, 0},
91 {"FK_Data_Add_1", 0, 8, 0},
92 {"FK_Data_Add_2", 0, 16, 0},
93 {"FK_Data_Add_4", 0, 32, 0},
94 {"FK_Data_Add_8", 0, 64, 0},
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +000095 {"FK_Data_Add_6b", 0, 6, 0},
Alex Bradbury257d5b52018-05-23 12:36:18 +000096 {"FK_Data_Sub_1", 0, 8, 0},
97 {"FK_Data_Sub_2", 0, 16, 0},
98 {"FK_Data_Sub_4", 0, 32, 0},
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +000099 {"FK_Data_Sub_8", 0, 64, 0},
100 {"FK_Data_Sub_6b", 0, 6, 0}};
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000101
Craig Topper58713212013-07-15 04:27:47 +0000102 assert((size_t)Kind <= array_lengthof(Builtins) && "Unknown fixup kind");
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +0000103 return Builtins[Kind];
104}
Colin LeMahieua01780f2015-05-30 18:42:22 +0000105
106bool MCAsmBackend::fixupNeedsRelaxationAdvanced(
107 const MCFixup &Fixup, bool Resolved, uint64_t Value,
Shiva Chen6e07dfb2018-05-18 06:42:21 +0000108 const MCRelaxableFragment *DF, const MCAsmLayout &Layout,
109 const bool WasForced) const {
Colin LeMahieua01780f2015-05-30 18:42:22 +0000110 if (!Resolved)
111 return true;
112 return fixupNeedsRelaxation(Fixup, Value, DF, Layout);
113}