blob: 754a200979084ceac3b053c8b0dd4695f04f51e6 [file] [log] [blame]
Daniel Dunbar12783d12010-02-21 21:54:14 +00001//===-- X86AsmBackend.cpp - X86 Assembler Backend -------------------------===//
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 "llvm/Target/TargetAsmBackend.h"
11#include "X86.h"
Daniel Dunbar87190c42010-03-19 09:28:12 +000012#include "X86FixupKinds.h"
13#include "llvm/MC/MCAssembler.h"
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +000014#include "llvm/MC/MCSectionELF.h"
Daniel Dunbard6e59082010-03-15 21:56:50 +000015#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar1a9158c2010-03-19 10:43:26 +000016#include "llvm/MC/MachObjectWriter.h"
Daniel Dunbar12783d12010-02-21 21:54:14 +000017#include "llvm/Target/TargetRegistry.h"
18#include "llvm/Target/TargetAsmBackend.h"
19using namespace llvm;
20
21namespace {
22
Daniel Dunbar87190c42010-03-19 09:28:12 +000023static unsigned getFixupKindLog2Size(unsigned Kind) {
24 switch (Kind) {
25 default: assert(0 && "invalid fixup kind!");
26 case X86::reloc_pcrel_1byte:
27 case FK_Data_1: return 0;
28 case FK_Data_2: return 1;
29 case X86::reloc_pcrel_4byte:
30 case X86::reloc_riprel_4byte:
31 case X86::reloc_riprel_4byte_movq_load:
32 case FK_Data_4: return 2;
33 case FK_Data_8: return 3;
34 }
35}
36
Daniel Dunbar12783d12010-02-21 21:54:14 +000037class X86AsmBackend : public TargetAsmBackend {
38public:
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +000039 X86AsmBackend(const Target &T)
Daniel Dunbar12783d12010-02-21 21:54:14 +000040 : TargetAsmBackend(T) {}
Daniel Dunbar87190c42010-03-19 09:28:12 +000041
42 void ApplyFixup(const MCAsmFixup &Fixup, MCDataFragment &DF,
43 uint64_t Value) const {
44 unsigned Size = 1 << getFixupKindLog2Size(Fixup.Kind);
45
46 assert(Fixup.Offset + Size <= DF.getContents().size() &&
47 "Invalid fixup offset!");
48 for (unsigned i = 0; i != Size; ++i)
49 DF.getContents()[Fixup.Offset + i] = uint8_t(Value >> (i * 8));
50 }
Daniel Dunbar12783d12010-02-21 21:54:14 +000051};
52
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +000053class ELFX86AsmBackend : public X86AsmBackend {
54public:
55 ELFX86AsmBackend(const Target &T)
56 : X86AsmBackend(T) {
57 HasAbsolutizedSet = true;
58 HasScatteredSymbols = true;
59 }
60
Daniel Dunbar1a9158c2010-03-19 10:43:26 +000061 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
62 return 0;
63 }
64
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +000065 bool isVirtualSection(const MCSection &Section) const {
66 const MCSectionELF &SE = static_cast<const MCSectionELF&>(Section);
67 return SE.getType() == MCSectionELF::SHT_NOBITS;;
68 }
69};
70
Daniel Dunbar23ac7c72010-03-11 01:34:21 +000071class DarwinX86AsmBackend : public X86AsmBackend {
72public:
73 DarwinX86AsmBackend(const Target &T)
Daniel Dunbar06829512010-03-18 00:58:53 +000074 : X86AsmBackend(T) {
75 HasAbsolutizedSet = true;
76 HasScatteredSymbols = true;
77 }
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +000078
79 bool isVirtualSection(const MCSection &Section) const {
80 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
81 return (SMO.getType() == MCSectionMachO::S_ZEROFILL ||
82 SMO.getType() == MCSectionMachO::S_GB_ZEROFILL);
83 }
Daniel Dunbar23ac7c72010-03-11 01:34:21 +000084};
85
Daniel Dunbard6e59082010-03-15 21:56:50 +000086class DarwinX86_32AsmBackend : public DarwinX86AsmBackend {
87public:
88 DarwinX86_32AsmBackend(const Target &T)
89 : DarwinX86AsmBackend(T) {}
Daniel Dunbar1a9158c2010-03-19 10:43:26 +000090
91 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
92 return new MachObjectWriter(OS, /*Is64Bit=*/false);
93 }
Daniel Dunbard6e59082010-03-15 21:56:50 +000094};
95
96class DarwinX86_64AsmBackend : public DarwinX86AsmBackend {
97public:
98 DarwinX86_64AsmBackend(const Target &T)
Daniel Dunbar06829512010-03-18 00:58:53 +000099 : DarwinX86AsmBackend(T) {
100 HasReliableSymbolDifference = true;
101 }
Daniel Dunbard6e59082010-03-15 21:56:50 +0000102
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000103 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
104 return new MachObjectWriter(OS, /*Is64Bit=*/true);
105 }
106
Daniel Dunbard6e59082010-03-15 21:56:50 +0000107 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
108 // Temporary labels in the string literals sections require symbols. The
109 // issue is that the x86_64 relocation format does not allow symbol +
110 // offset, and so the linker does not have enough information to resolve the
111 // access to the appropriate atom unless an external relocation is used. For
112 // non-cstring sections, we expect the compiler to use a non-temporary label
113 // for anything that could have an addend pointing outside the symbol.
114 //
115 // See <rdar://problem/4765733>.
116 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
117 return SMO.getType() == MCSectionMachO::S_CSTRING_LITERALS;
118 }
119};
120
Daniel Dunbar12783d12010-02-21 21:54:14 +0000121}
122
123TargetAsmBackend *llvm::createX86_32AsmBackend(const Target &T,
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +0000124 const std::string &TT) {
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000125 switch (Triple(TT).getOS()) {
126 case Triple::Darwin:
Daniel Dunbard6e59082010-03-15 21:56:50 +0000127 return new DarwinX86_32AsmBackend(T);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000128 default:
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000129 return new ELFX86AsmBackend(T);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000130 }
Daniel Dunbar12783d12010-02-21 21:54:14 +0000131}
132
133TargetAsmBackend *llvm::createX86_64AsmBackend(const Target &T,
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +0000134 const std::string &TT) {
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000135 switch (Triple(TT).getOS()) {
136 case Triple::Darwin:
Daniel Dunbard6e59082010-03-15 21:56:50 +0000137 return new DarwinX86_64AsmBackend(T);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000138 default:
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000139 return new ELFX86AsmBackend(T);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000140 }
Daniel Dunbar12783d12010-02-21 21:54:14 +0000141}