blob: 1d66b841523207d96997de124175bdae0eda8217 [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 Dunbar12783d12010-02-21 21:54:14 +000016#include "llvm/Target/TargetRegistry.h"
17#include "llvm/Target/TargetAsmBackend.h"
18using namespace llvm;
19
20namespace {
21
Daniel Dunbar87190c42010-03-19 09:28:12 +000022static unsigned getFixupKindLog2Size(unsigned Kind) {
23 switch (Kind) {
24 default: assert(0 && "invalid fixup kind!");
25 case X86::reloc_pcrel_1byte:
26 case FK_Data_1: return 0;
27 case FK_Data_2: return 1;
28 case X86::reloc_pcrel_4byte:
29 case X86::reloc_riprel_4byte:
30 case X86::reloc_riprel_4byte_movq_load:
31 case FK_Data_4: return 2;
32 case FK_Data_8: return 3;
33 }
34}
35
Daniel Dunbar12783d12010-02-21 21:54:14 +000036class X86AsmBackend : public TargetAsmBackend {
37public:
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +000038 X86AsmBackend(const Target &T)
Daniel Dunbar12783d12010-02-21 21:54:14 +000039 : TargetAsmBackend(T) {}
Daniel Dunbar87190c42010-03-19 09:28:12 +000040
41 void ApplyFixup(const MCAsmFixup &Fixup, MCDataFragment &DF,
42 uint64_t Value) const {
43 unsigned Size = 1 << getFixupKindLog2Size(Fixup.Kind);
44
45 assert(Fixup.Offset + Size <= DF.getContents().size() &&
46 "Invalid fixup offset!");
47 for (unsigned i = 0; i != Size; ++i)
48 DF.getContents()[Fixup.Offset + i] = uint8_t(Value >> (i * 8));
49 }
Daniel Dunbar12783d12010-02-21 21:54:14 +000050};
51
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +000052class ELFX86AsmBackend : public X86AsmBackend {
53public:
54 ELFX86AsmBackend(const Target &T)
55 : X86AsmBackend(T) {
56 HasAbsolutizedSet = true;
57 HasScatteredSymbols = true;
58 }
59
60 bool isVirtualSection(const MCSection &Section) const {
61 const MCSectionELF &SE = static_cast<const MCSectionELF&>(Section);
62 return SE.getType() == MCSectionELF::SHT_NOBITS;;
63 }
64};
65
Daniel Dunbar23ac7c72010-03-11 01:34:21 +000066class DarwinX86AsmBackend : public X86AsmBackend {
67public:
68 DarwinX86AsmBackend(const Target &T)
Daniel Dunbar06829512010-03-18 00:58:53 +000069 : X86AsmBackend(T) {
70 HasAbsolutizedSet = true;
71 HasScatteredSymbols = true;
72 }
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +000073
74 bool isVirtualSection(const MCSection &Section) const {
75 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
76 return (SMO.getType() == MCSectionMachO::S_ZEROFILL ||
77 SMO.getType() == MCSectionMachO::S_GB_ZEROFILL);
78 }
Daniel Dunbar23ac7c72010-03-11 01:34:21 +000079};
80
Daniel Dunbard6e59082010-03-15 21:56:50 +000081class DarwinX86_32AsmBackend : public DarwinX86AsmBackend {
82public:
83 DarwinX86_32AsmBackend(const Target &T)
84 : DarwinX86AsmBackend(T) {}
85};
86
87class DarwinX86_64AsmBackend : public DarwinX86AsmBackend {
88public:
89 DarwinX86_64AsmBackend(const Target &T)
Daniel Dunbar06829512010-03-18 00:58:53 +000090 : DarwinX86AsmBackend(T) {
91 HasReliableSymbolDifference = true;
92 }
Daniel Dunbard6e59082010-03-15 21:56:50 +000093
94 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
95 // Temporary labels in the string literals sections require symbols. The
96 // issue is that the x86_64 relocation format does not allow symbol +
97 // offset, and so the linker does not have enough information to resolve the
98 // access to the appropriate atom unless an external relocation is used. For
99 // non-cstring sections, we expect the compiler to use a non-temporary label
100 // for anything that could have an addend pointing outside the symbol.
101 //
102 // See <rdar://problem/4765733>.
103 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
104 return SMO.getType() == MCSectionMachO::S_CSTRING_LITERALS;
105 }
106};
107
Daniel Dunbar12783d12010-02-21 21:54:14 +0000108}
109
110TargetAsmBackend *llvm::createX86_32AsmBackend(const Target &T,
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +0000111 const std::string &TT) {
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000112 switch (Triple(TT).getOS()) {
113 case Triple::Darwin:
Daniel Dunbard6e59082010-03-15 21:56:50 +0000114 return new DarwinX86_32AsmBackend(T);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000115 default:
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000116 return new ELFX86AsmBackend(T);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000117 }
Daniel Dunbar12783d12010-02-21 21:54:14 +0000118}
119
120TargetAsmBackend *llvm::createX86_64AsmBackend(const Target &T,
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +0000121 const std::string &TT) {
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000122 switch (Triple(TT).getOS()) {
123 case Triple::Darwin:
Daniel Dunbard6e59082010-03-15 21:56:50 +0000124 return new DarwinX86_64AsmBackend(T);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000125 default:
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000126 return new ELFX86AsmBackend(T);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000127 }
Daniel Dunbar12783d12010-02-21 21:54:14 +0000128}