blob: 7faa6f411b3813af580088f51a8b1e39b96d282b [file] [log] [blame]
Nick Lewycky462bba32013-03-09 09:31:44 +00001//===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
Matt Fleming3565a062010-08-16 18:57:57 +00002//
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// This file assembles .s files and emits ELF .o object files.
11//
12//===----------------------------------------------------------------------===//
13
Logan Chien64501652012-12-07 15:50:40 +000014#include "llvm/MC/MCELFStreamer.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000015#include "llvm/ADT/SmallPtrSet.h"
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +000016#include "llvm/ADT/STLExtras.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000017#include "llvm/MC/MCAssembler.h"
Matt Fleming3565a062010-08-16 18:57:57 +000018#include "llvm/MC/MCCodeEmitter.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000019#include "llvm/MC/MCContext.h"
Tim Northover6eb3e872012-12-07 16:50:23 +000020#include "llvm/MC/MCELF.h"
Matt Fleming3565a062010-08-16 18:57:57 +000021#include "llvm/MC/MCELFSymbolFlags.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000024#include "llvm/MC/MCObjectStreamer.h"
Matt Fleming3565a062010-08-16 18:57:57 +000025#include "llvm/MC/MCSection.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/MC/MCSectionELF.h"
Matt Fleming3565a062010-08-16 18:57:57 +000027#include "llvm/MC/MCSymbol.h"
Rafael Espindola484291c2010-11-01 14:28:48 +000028#include "llvm/MC/MCValue.h"
Matt Fleming3565a062010-08-16 18:57:57 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/ELF.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
Matt Fleming3565a062010-08-16 18:57:57 +000033
34using namespace llvm;
35
Rafael Espindolaf340a292012-01-07 23:18:39 +000036
Logan Chien64501652012-12-07 15:50:40 +000037inline void MCELFStreamer::SetSection(StringRef Section, unsigned Type,
38 unsigned Flags, SectionKind Kind) {
39 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
40}
Rafael Espindolaf340a292012-01-07 23:18:39 +000041
Logan Chien64501652012-12-07 15:50:40 +000042inline void MCELFStreamer::SetSectionData() {
43 SetSection(".data",
44 ELF::SHT_PROGBITS,
45 ELF::SHF_WRITE | ELF::SHF_ALLOC,
46 SectionKind::getDataRel());
47 EmitCodeAlignment(4, 0);
48}
Rafael Espindolaf340a292012-01-07 23:18:39 +000049
Logan Chien64501652012-12-07 15:50:40 +000050inline void MCELFStreamer::SetSectionText() {
51 SetSection(".text",
52 ELF::SHT_PROGBITS,
53 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
54 SectionKind::getText());
55 EmitCodeAlignment(4, 0);
56}
Rafael Espindolaf340a292012-01-07 23:18:39 +000057
Logan Chien64501652012-12-07 15:50:40 +000058inline void MCELFStreamer::SetSectionBss() {
59 SetSection(".bss",
60 ELF::SHT_NOBITS,
61 ELF::SHF_WRITE | ELF::SHF_ALLOC,
62 SectionKind::getBSS());
63 EmitCodeAlignment(4, 0);
64}
Rafael Espindolaf340a292012-01-07 23:18:39 +000065
Logan Chien64501652012-12-07 15:50:40 +000066MCELFStreamer::~MCELFStreamer() {
Rafael Espindolaf340a292012-01-07 23:18:39 +000067}
68
Eli Bendersky030f63a2013-01-14 19:04:57 +000069void MCELFStreamer::InitToTextSection() {
70 SetSectionText();
71}
72
Rafael Espindolad80781b2010-09-15 21:48:40 +000073void MCELFStreamer::InitSections() {
74 // This emulates the same behavior of GNU as. This makes it easier
75 // to compare the output as the major sections are in the same order.
76 SetSectionText();
77 SetSectionData();
78 SetSectionBss();
79 SetSectionText();
80}
81
Matt Fleming3565a062010-08-16 18:57:57 +000082void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +000083 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
84
Rafael Espindolaea4afa92010-11-28 17:18:55 +000085 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola73ffea42010-09-25 05:42:19 +000086
Rafael Espindolae1a25872010-11-11 19:04:55 +000087 const MCSectionELF &Section =
88 static_cast<const MCSectionELF&>(Symbol->getSection());
Rafael Espindolaea4afa92010-11-28 17:18:55 +000089 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Rafael Espindola1c130262011-01-23 04:43:11 +000090 if (Section.getFlags() & ELF::SHF_TLS)
Jan Sjödin2ddfd952011-02-28 21:45:04 +000091 MCELF::SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +000092}
93
Reed Kotler2c3a4642012-12-16 04:00:45 +000094void MCELFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
95 EmitLabel(Symbol);
96}
97
Matt Fleming3565a062010-08-16 18:57:57 +000098void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
99 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000100 case MCAF_SyntaxUnified: return; // no-op here.
Evan Chengbd27f5a2011-07-27 00:38:12 +0000101 case MCAF_Code16: return; // Change parsing mode; no-op here.
102 case MCAF_Code32: return; // Change parsing mode; no-op here.
103 case MCAF_Code64: return; // Change parsing mode; no-op here.
Matt Fleming3565a062010-08-16 18:57:57 +0000104 case MCAF_SubsectionsViaSymbols:
105 getAssembler().setSubsectionsViaSymbols(true);
106 return;
107 }
108
Craig Topper85814382012-02-07 05:05:23 +0000109 llvm_unreachable("invalid assembler flag!");
Matt Fleming3565a062010-08-16 18:57:57 +0000110}
111
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000112void MCELFStreamer::ChangeSection(const MCSection *Section) {
Eli Bendersky4766ef42012-12-20 19:05:53 +0000113 MCSectionData *CurSection = getCurrentSectionData();
114 if (CurSection && CurSection->isBundleLocked())
115 report_fatal_error("Unterminated .bundle_lock when changing a section");
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000116 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
117 if (Grp)
118 getAssembler().getOrCreateSymbolData(*Grp);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000119 this->MCObjectStreamer::ChangeSection(Section);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000120}
121
Rafael Espindola484291c2010-11-01 14:28:48 +0000122void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
123 getAssembler().getOrCreateSymbolData(*Symbol);
124 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
125 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000126 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
Rafael Espindola484291c2010-11-01 14:28:48 +0000127 Alias->setVariableValue(Value);
128}
129
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000130// When GNU as encounters more than one .type declaration for an object it seems
131// to use a mechanism similar to the one below to decide which type is actually
132// used in the object file. The greater of T1 and T2 is selected based on the
133// following ordering:
134// STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
135// If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
136// provided type).
137static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
138 unsigned TypeOrdering[] = {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
139 ELF::STT_GNU_IFUNC, ELF::STT_TLS};
140 for (unsigned i = 0; i != array_lengthof(TypeOrdering); ++i) {
141 if (T1 == TypeOrdering[i])
142 return T2;
143 if (T2 == TypeOrdering[i])
144 return T1;
145 }
146
147 return T2;
148}
149
Matt Fleming3565a062010-08-16 18:57:57 +0000150void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
151 MCSymbolAttr Attribute) {
152 // Indirect symbols are handled differently, to match how 'as' handles
153 // them. This makes writing matching .o files easier.
154 if (Attribute == MCSA_IndirectSymbol) {
155 // Note that we intentionally cannot use the symbol data here; this is
156 // important for matching the string table that 'as' generates.
157 IndirectSymbolData ISD;
158 ISD.Symbol = Symbol;
159 ISD.SectionData = getCurrentSectionData();
160 getAssembler().getIndirectSymbols().push_back(ISD);
161 return;
162 }
163
164 // Adding a symbol attribute always introduces the symbol, note that an
165 // important side effect of calling getOrCreateSymbolData here is to register
166 // the symbol with the assembler.
167 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
168
169 // The implementation of symbol attributes is designed to match 'as', but it
170 // leaves much to desired. It doesn't really make sense to arbitrarily add and
171 // remove flags, but 'as' allows this (in particular, see .desc).
172 //
173 // In the future it might be worth trying to make these operations more well
174 // defined.
175 switch (Attribute) {
176 case MCSA_LazyReference:
177 case MCSA_Reference:
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000178 case MCSA_SymbolResolver:
Matt Fleming3565a062010-08-16 18:57:57 +0000179 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000180 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000181 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000182 case MCSA_Invalid:
Matt Fleming3565a062010-08-16 18:57:57 +0000183 case MCSA_IndirectSymbol:
Craig Topper85814382012-02-07 05:05:23 +0000184 llvm_unreachable("Invalid symbol attribute for ELF!");
Matt Fleming3565a062010-08-16 18:57:57 +0000185
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000186 case MCSA_NoDeadStrip:
Rafael Espindola7e528a12010-11-14 01:34:31 +0000187 case MCSA_ELF_TypeGnuUniqueObject:
188 // Ignore for now.
189 break;
190
Matt Fleming3565a062010-08-16 18:57:57 +0000191 case MCSA_Global:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000192 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000193 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000194 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000195 break;
196
Benjamin Kramer230c2742010-09-02 17:18:32 +0000197 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000198 case MCSA_Weak:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000199 MCELF::SetBinding(SD, ELF::STB_WEAK);
Rafael Espindola3223f192010-10-06 16:47:31 +0000200 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000201 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000202 break;
203
204 case MCSA_Local:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000205 MCELF::SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000206 SD.setExternal(false);
207 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000208 break;
209
210 case MCSA_ELF_TypeFunction:
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000211 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
212 ELF::STT_FUNC));
Matt Fleming3565a062010-08-16 18:57:57 +0000213 break;
214
Roman Divackya0c17a42011-12-12 17:34:04 +0000215 case MCSA_ELF_TypeIndFunction:
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000216 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
217 ELF::STT_GNU_IFUNC));
Roman Divackya0c17a42011-12-12 17:34:04 +0000218 break;
219
Matt Fleming3565a062010-08-16 18:57:57 +0000220 case MCSA_ELF_TypeObject:
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000221 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
222 ELF::STT_OBJECT));
Matt Fleming3565a062010-08-16 18:57:57 +0000223 break;
224
225 case MCSA_ELF_TypeTLS:
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000226 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
227 ELF::STT_TLS));
Matt Fleming3565a062010-08-16 18:57:57 +0000228 break;
229
230 case MCSA_ELF_TypeCommon:
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000231 // TODO: Emit these as a common symbol.
232 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
233 ELF::STT_OBJECT));
Matt Fleming3565a062010-08-16 18:57:57 +0000234 break;
235
236 case MCSA_ELF_TypeNoType:
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000237 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
238 ELF::STT_NOTYPE));
Matt Fleming3565a062010-08-16 18:57:57 +0000239 break;
240
241 case MCSA_Protected:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000242 MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000243 break;
244
245 case MCSA_Hidden:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000246 MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000247 break;
248
249 case MCSA_Internal:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000250 MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000251 break;
252 }
253}
254
255void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
256 unsigned ByteAlignment) {
257 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
258
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000259 if (!BindingExplicitlySet.count(Symbol)) {
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000260 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000261 SD.setExternal(true);
262 }
263
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000264 MCELF::SetType(SD, ELF::STT_OBJECT);
Rafael Espindola55d02f32010-11-14 21:11:16 +0000265
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000266 if (MCELF::GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000267 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
Jim Grosbach946227d2011-11-15 16:46:22 +0000268 ELF::SHT_NOBITS,
269 ELF::SHF_WRITE |
270 ELF::SHF_ALLOC,
271 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000272 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000273
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000274 struct LocalCommon L = {&SD, Size, ByteAlignment};
275 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000276 } else {
277 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000278 }
279
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000280 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000281}
282
Logan Chien64501652012-12-07 15:50:40 +0000283void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
284 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
285 SD.setSize(Value);
286}
287
Benjamin Kramer36a16012011-09-01 23:04:27 +0000288void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
289 unsigned ByteAlignment) {
Jason W Kimf13743b2010-12-16 03:12:17 +0000290 // FIXME: Should this be caught and done earlier?
291 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000292 MCELF::SetBinding(SD, ELF::STB_LOCAL);
Jason W Kimf13743b2010-12-16 03:12:17 +0000293 SD.setExternal(false);
294 BindingExplicitlySet.insert(Symbol);
Benjamin Kramer36a16012011-09-01 23:04:27 +0000295 EmitCommonSymbol(Symbol, Size, ByteAlignment);
Jason W Kimf13743b2010-12-16 03:12:17 +0000296}
297
David Meyer5f769262012-02-15 15:09:06 +0000298void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
299 unsigned AddrSpace) {
Eli Bendersky4766ef42012-12-20 19:05:53 +0000300 if (getCurrentSectionData()->isBundleLocked())
301 report_fatal_error("Emitting values inside a locked bundle is forbidden");
David Meyer5f769262012-02-15 15:09:06 +0000302 fixSymbolsInTLSFixups(Value);
303 MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace);
304}
305
Eli Bendersky4766ef42012-12-20 19:05:53 +0000306void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
307 int64_t Value,
308 unsigned ValueSize,
309 unsigned MaxBytesToEmit) {
310 if (getCurrentSectionData()->isBundleLocked())
311 report_fatal_error("Emitting values inside a locked bundle is forbidden");
312 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
313 ValueSize, MaxBytesToEmit);
314}
315
David Meyer5f769262012-02-15 15:09:06 +0000316
Matt Fleming3565a062010-08-16 18:57:57 +0000317// Add a symbol for the file name of this module. This is the second
318// entry in the module's symbol table (the first being the null symbol).
319void MCELFStreamer::EmitFileDirective(StringRef Filename) {
320 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000321 Symbol->setSection(*getCurrentSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000322 Symbol->setAbsolute();
323
324 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
325
326 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
327}
328
Rafael Espindola97551272010-11-24 02:19:40 +0000329void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
330 switch (expr->getKind()) {
Tim Northover72062f52013-01-31 12:12:40 +0000331 case MCExpr::Target:
332 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
333 break;
Rafael Espindola97551272010-11-24 02:19:40 +0000334 case MCExpr::Constant:
335 break;
336
337 case MCExpr::Binary: {
338 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
339 fixSymbolsInTLSFixups(be->getLHS());
340 fixSymbolsInTLSFixups(be->getRHS());
341 break;
342 }
343
344 case MCExpr::SymbolRef: {
345 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000346 switch (symRef.getKind()) {
347 default:
Rafael Espindola97551272010-11-24 02:19:40 +0000348 return;
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000349 case MCSymbolRefExpr::VK_GOTTPOFF:
350 case MCSymbolRefExpr::VK_INDNTPOFF:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000351 case MCSymbolRefExpr::VK_NTPOFF:
352 case MCSymbolRefExpr::VK_GOTNTPOFF:
353 case MCSymbolRefExpr::VK_TLSGD:
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000354 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000355 case MCSymbolRefExpr::VK_TLSLDM:
356 case MCSymbolRefExpr::VK_TPOFF:
357 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000358 case MCSymbolRefExpr::VK_ARM_TLSGD:
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000359 case MCSymbolRefExpr::VK_ARM_TPOFF:
360 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Bruno Cardoso Lopes3507d242011-10-25 18:13:20 +0000361 case MCSymbolRefExpr::VK_Mips_TLSGD:
362 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
363 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
364 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
Bill Schmidt3a429892013-02-26 16:41:03 +0000365 case MCSymbolRefExpr::VK_PPC_TPREL16_HA:
366 case MCSymbolRefExpr::VK_PPC_TPREL16_LO:
367 case MCSymbolRefExpr::VK_PPC_DTPREL16_HA:
368 case MCSymbolRefExpr::VK_PPC_DTPREL16_LO:
369 case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_HA:
370 case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO:
371 case MCSymbolRefExpr::VK_PPC_TLS:
372 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_HA:
373 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_LO:
374 case MCSymbolRefExpr::VK_PPC_TLSGD:
375 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_HA:
376 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO:
377 case MCSymbolRefExpr::VK_PPC_TLSLD:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000378 break;
379 }
Rafael Espindola97551272010-11-24 02:19:40 +0000380 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000381 MCELF::SetType(SD, ELF::STT_TLS);
Rafael Espindola97551272010-11-24 02:19:40 +0000382 break;
383 }
384
385 case MCExpr::Unary:
386 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
387 break;
388 }
389}
390
Benjamin Kramer93ded732010-08-27 10:40:51 +0000391void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
Rafael Espindoladedb0452010-12-02 05:44:06 +0000392 this->MCObjectStreamer::EmitInstToFragment(Inst);
Eli Bendersky251040b2013-01-08 00:22:56 +0000393 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000394
Rafael Espindoladedb0452010-12-02 05:44:06 +0000395 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
396 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000397}
398
399void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
Eli Bendersky4766ef42012-12-20 19:05:53 +0000400 MCAssembler &Assembler = getAssembler();
Benjamin Kramer93ded732010-08-27 10:40:51 +0000401 SmallVector<MCFixup, 4> Fixups;
402 SmallString<256> Code;
403 raw_svector_ostream VecOS(Code);
Eli Bendersky4766ef42012-12-20 19:05:53 +0000404 Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Benjamin Kramer93ded732010-08-27 10:40:51 +0000405 VecOS.flush();
406
Rafael Espindola97551272010-11-24 02:19:40 +0000407 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
408 fixSymbolsInTLSFixups(Fixups[i].getValue());
409
Eli Bendersky4766ef42012-12-20 19:05:53 +0000410 // There are several possibilities here:
411 //
412 // If bundling is disabled, append the encoded instruction to the current data
413 // fragment (or create a new such fragment if the current fragment is not a
414 // data fragment).
415 //
416 // If bundling is enabled:
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000417 // - If we're not in a bundle-locked group, emit the instruction into a
418 // fragment of its own. If there are no fixups registered for the
419 // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
420 // MCDataFragment.
Eli Bendersky4766ef42012-12-20 19:05:53 +0000421 // - If we're in a bundle-locked group, append the instruction to the current
422 // data fragment because we want all the instructions in a group to get into
423 // the same fragment. Be careful not to do that for the first instruction in
424 // the group, though.
425 MCDataFragment *DF;
426
427 if (Assembler.isBundlingEnabled()) {
428 MCSectionData *SD = getCurrentSectionData();
429 if (SD->isBundleLocked() && !SD->isBundleGroupBeforeFirstInst())
Derek Schuff67144e32013-02-15 22:50:52 +0000430 // If we are bundle-locked, we re-use the current fragment.
431 // The bundle-locking directive ensures this is a new data fragment.
432 DF = cast<MCDataFragment>(getCurrentFragment());
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000433 else if (!SD->isBundleLocked() && Fixups.size() == 0) {
434 // Optimize memory usage by emitting the instruction to a
435 // MCCompactEncodedInstFragment when not in a bundle-locked group and
436 // there are no fixups registered.
437 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment(SD);
438 CEIF->getContents().append(Code.begin(), Code.end());
439 return;
Derek Schuff67144e32013-02-15 22:50:52 +0000440 } else {
Eli Bendersky4766ef42012-12-20 19:05:53 +0000441 DF = new MCDataFragment(SD);
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000442 if (SD->getBundleLockState() == MCSectionData::BundleLockedAlignToEnd) {
443 // If this is a new fragment created for a bundle-locked group, and the
444 // group was marked as "align_to_end", set a flag in the fragment.
445 DF->setAlignToBundleEnd(true);
446 }
447 }
Eli Bendersky4766ef42012-12-20 19:05:53 +0000448
449 // We're now emitting an instruction in a bundle group, so this flag has
450 // to be turned off.
451 SD->setBundleGroupBeforeFirstInst(false);
452 } else {
453 DF = getOrCreateDataFragment();
454 }
455
Benjamin Kramer93ded732010-08-27 10:40:51 +0000456 // Add the fixups and data.
457 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
458 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000459 DF->getFixups().push_back(Fixups[i]);
Benjamin Kramer93ded732010-08-27 10:40:51 +0000460 }
Eli Bendersky4766ef42012-12-20 19:05:53 +0000461 DF->setHasInstructions(true);
Benjamin Kramer93ded732010-08-27 10:40:51 +0000462 DF->getContents().append(Code.begin(), Code.end());
463}
464
Eli Bendersky4766ef42012-12-20 19:05:53 +0000465void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
466 assert(AlignPow2 <= 30 && "Invalid bundle alignment");
467 MCAssembler &Assembler = getAssembler();
468 if (Assembler.getBundleAlignSize() == 0 && AlignPow2 > 0)
469 Assembler.setBundleAlignSize(1 << AlignPow2);
470 else
471 report_fatal_error(".bundle_align_mode should be only set once per file");
472}
473
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000474void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
Eli Bendersky4766ef42012-12-20 19:05:53 +0000475 MCSectionData *SD = getCurrentSectionData();
476
477 // Sanity checks
478 //
479 if (!getAssembler().isBundlingEnabled())
480 report_fatal_error(".bundle_lock forbidden when bundling is disabled");
481 else if (SD->isBundleLocked())
482 report_fatal_error("Nesting of .bundle_lock is forbidden");
483
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000484 SD->setBundleLockState(AlignToEnd ? MCSectionData::BundleLockedAlignToEnd :
485 MCSectionData::BundleLocked);
Eli Bendersky4766ef42012-12-20 19:05:53 +0000486 SD->setBundleGroupBeforeFirstInst(true);
487}
488
489void MCELFStreamer::EmitBundleUnlock() {
490 MCSectionData *SD = getCurrentSectionData();
491
492 // Sanity checks
493 if (!getAssembler().isBundlingEnabled())
494 report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
495 else if (!SD->isBundleLocked())
496 report_fatal_error(".bundle_unlock without matching lock");
497 else if (SD->isBundleGroupBeforeFirstInst())
498 report_fatal_error("Empty bundle-locked group is forbidden");
499
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000500 SD->setBundleLockState(MCSectionData::NotBundleLocked);
Eli Bendersky4766ef42012-12-20 19:05:53 +0000501}
502
Rafael Espindola99b42372012-01-07 03:13:18 +0000503void MCELFStreamer::FinishImpl() {
Rafael Espindolac25dad82011-05-10 03:14:15 +0000504 EmitFrames(true);
Rafael Espindolac70a1d92010-11-01 17:07:14 +0000505
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000506 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
507 e = LocalCommons.end();
508 i != e; ++i) {
509 MCSymbolData *SD = i->SD;
510 uint64_t Size = i->Size;
511 unsigned ByteAlignment = i->ByteAlignment;
512 const MCSymbol &Symbol = SD->getSymbol();
513 const MCSection &Section = Symbol.getSection();
514
515 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
516 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
517
518 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
519 SD->setFragment(F);
520
521 // Update the maximum alignment of the section if necessary.
522 if (ByteAlignment > SectData.getAlignment())
523 SectData.setAlignment(ByteAlignment);
524 }
525
Rafael Espindola99b42372012-01-07 03:13:18 +0000526 this->MCObjectStreamer::FinishImpl();
Matt Fleming3565a062010-08-16 18:57:57 +0000527}
Tim Northover6eb3e872012-12-07 16:50:23 +0000528void MCELFStreamer::EmitTCEntry(const MCSymbol &S) {
Adhemerval Zanellaf35c62b2012-10-15 15:43:14 +0000529 // Creates a R_PPC64_TOC relocation
Eric Christopher1ced2082013-01-09 03:52:05 +0000530 MCObjectStreamer::EmitSymbolValue(&S, 8);
Adhemerval Zanellaf35c62b2012-10-15 15:43:14 +0000531}
532
Evan Cheng78c10ee2011-07-25 23:24:55 +0000533MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000534 raw_ostream &OS, MCCodeEmitter *CE,
535 bool RelaxAll, bool NoExecStack) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000536 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
Matt Fleming3565a062010-08-16 18:57:57 +0000537 if (RelaxAll)
538 S->getAssembler().setRelaxAll(true);
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000539 if (NoExecStack)
540 S->getAssembler().setNoExecStack(true);
Matt Fleming3565a062010-08-16 18:57:57 +0000541 return S;
542}
Logan Chien64501652012-12-07 15:50:40 +0000543
Tim Northover6eb3e872012-12-07 16:50:23 +0000544void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
545 llvm_unreachable("Generic ELF doesn't support this directive");
546}
547
Jack Carter77afbdc2013-02-19 21:57:35 +0000548MCSymbolData &MCELFStreamer::getOrCreateSymbolData(MCSymbol *Symbol) {
549 return getAssembler().getOrCreateSymbolData(*Symbol);
550}
551
Logan Chien64501652012-12-07 15:50:40 +0000552void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
553 llvm_unreachable("ELF doesn't support this directive");
554}
555
556void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
557 llvm_unreachable("ELF doesn't support this directive");
558}
559
560void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
561 llvm_unreachable("ELF doesn't support this directive");
562}
563
564void MCELFStreamer::EmitCOFFSymbolType(int Type) {
565 llvm_unreachable("ELF doesn't support this directive");
566}
567
568void MCELFStreamer::EndCOFFSymbolDef() {
569 llvm_unreachable("ELF doesn't support this directive");
570}
571
572void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
573 uint64_t Size, unsigned ByteAlignment) {
574 llvm_unreachable("ELF doesn't support this directive");
575}
576
577void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
578 uint64_t Size, unsigned ByteAlignment) {
579 llvm_unreachable("ELF doesn't support this directive");
580}