blob: 01c8e2825e9d32a8ff2f6d7b3f348acdb96b5ae4 [file] [log] [blame]
Nick Lewyckyc1f96942013-03-09 09:31:44 +00001//===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
Matt Fleming6c1ad482010-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 Chien59ff0702012-12-07 15:50:40 +000014#include "llvm/MC/MCELFStreamer.h"
Peter Collingbourneadac4072013-04-10 16:52:15 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallPtrSet.h"
David Peixotto72667312013-11-25 19:11:13 +000017#include "llvm/MC/MCAsmBackend.h"
Petr Hosek9e0c8902015-04-12 23:42:25 +000018#include "llvm/MC/MCAsmLayout.h"
Rafael Espindola7b61ddf2014-10-15 16:12:52 +000019#include "llvm/MC/MCAsmInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/MC/MCAssembler.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000021#include "llvm/MC/MCCodeEmitter.h"
Rafael Espindola81a62742012-01-07 23:18:39 +000022#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000023#include "llvm/MC/MCELF.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000024#include "llvm/MC/MCELFSymbolFlags.h"
25#include "llvm/MC/MCExpr.h"
26#include "llvm/MC/MCInst.h"
Rafael Espindolae308c0c2014-01-23 22:49:25 +000027#include "llvm/MC/MCObjectFileInfo.h"
Rafael Espindola81a62742012-01-07 23:18:39 +000028#include "llvm/MC/MCObjectStreamer.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000029#include "llvm/MC/MCSection.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/MC/MCSectionELF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000031#include "llvm/MC/MCSymbolELF.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000032#include "llvm/MC/MCSymbol.h"
Rafael Espindola16145972010-11-01 14:28:48 +000033#include "llvm/MC/MCValue.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/ELF.h"
36#include "llvm/Support/ErrorHandling.h"
Rafael Espindolacd584a82015-03-19 01:50:16 +000037#include "llvm/Support/TargetRegistry.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000038#include "llvm/Support/raw_ostream.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000039
40using namespace llvm;
41
Rafael Espindolac1cd9442015-05-25 14:57:35 +000042bool MCELFStreamer::isBundleLocked() const {
Rafael Espindola983bec62015-05-27 21:04:14 +000043 return getCurrentSectionOnly()->isBundleLocked();
Rafael Espindolac1cd9442015-05-25 14:57:35 +000044}
45
Logan Chien59ff0702012-12-07 15:50:40 +000046MCELFStreamer::~MCELFStreamer() {
Rafael Espindola81a62742012-01-07 23:18:39 +000047}
48
Petr Hosek9e0c8902015-04-12 23:42:25 +000049void MCELFStreamer::mergeFragment(MCDataFragment *DF,
50 MCEncodedFragmentWithFixups *EF) {
51 MCAssembler &Assembler = getAssembler();
52
53 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
54 uint64_t FSize = EF->getContents().size();
55
56 if (FSize > Assembler.getBundleAlignSize())
57 report_fatal_error("Fragment can't be larger than a bundle size");
58
59 uint64_t RequiredBundlePadding = computeBundlePadding(
60 Assembler, EF, DF->getContents().size(), FSize);
61
62 if (RequiredBundlePadding > UINT8_MAX)
63 report_fatal_error("Padding cannot exceed 255 bytes");
64
65 if (RequiredBundlePadding > 0) {
66 SmallString<256> Code;
67 raw_svector_ostream VecOS(Code);
68 MCObjectWriter *OW = Assembler.getBackend().createObjectWriter(VecOS);
69
70 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
71
72 Assembler.writeFragmentPadding(*EF, FSize, OW);
73 VecOS.flush();
74 delete OW;
75
76 DF->getContents().append(Code.begin(), Code.end());
77 }
78 }
79
80 flushPendingLabels(DF, DF->getContents().size());
81
82 for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
83 EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
84 DF->getContents().size());
85 DF->getFixups().push_back(EF->getFixups()[i]);
86 }
87 DF->setHasInstructions(true);
88 DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
89}
90
Rafael Espindola7b61ddf2014-10-15 16:12:52 +000091void MCELFStreamer::InitSections(bool NoExecStack) {
Rafael Espindolaf667d922010-09-15 21:48:40 +000092 // This emulates the same behavior of GNU as. This makes it easier
93 // to compare the output as the major sections are in the same order.
Rafael Espindola7b61ddf2014-10-15 16:12:52 +000094 MCContext &Ctx = getContext();
95 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
Rafael Espindola7b514962014-02-04 18:34:04 +000096 EmitCodeAlignment(4);
Rafael Espindola247f9512014-01-24 02:18:40 +000097
Rafael Espindola7b61ddf2014-10-15 16:12:52 +000098 SwitchSection(Ctx.getObjectFileInfo()->getDataSection());
Rafael Espindola7b514962014-02-04 18:34:04 +000099 EmitCodeAlignment(4);
Rafael Espindola247f9512014-01-24 02:18:40 +0000100
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000101 SwitchSection(Ctx.getObjectFileInfo()->getBSSSection());
Rafael Espindola7b514962014-02-04 18:34:04 +0000102 EmitCodeAlignment(4);
Rafael Espindola247f9512014-01-24 02:18:40 +0000103
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000104 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
105
106 if (NoExecStack)
107 SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
Rafael Espindolaf667d922010-09-15 21:48:40 +0000108}
109
Matt Fleming6c1ad482010-08-16 18:57:57 +0000110void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000111 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
112
Rafael Espindolae5b74152010-11-28 17:18:55 +0000113 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola75d65b92010-09-25 05:42:19 +0000114
Rafael Espindola84d03182010-11-11 19:04:55 +0000115 const MCSectionELF &Section =
116 static_cast<const MCSectionELF&>(Symbol->getSection());
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000117 if (Section.getFlags() & ELF::SHF_TLS)
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000118 MCELF::SetType(*Symbol, ELF::STT_TLS);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000119}
120
121void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
David Peixotto72667312013-11-25 19:11:13 +0000122 // Let the target do whatever target specific stuff it needs to do.
123 getAssembler().getBackend().handleAssemblerFlag(Flag);
124 // Do any generic stuff we need to do.
Matt Fleming6c1ad482010-08-16 18:57:57 +0000125 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000126 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000127 case MCAF_Code16: return; // Change parsing mode; no-op here.
128 case MCAF_Code32: return; // Change parsing mode; no-op here.
129 case MCAF_Code64: return; // Change parsing mode; no-op here.
Matt Fleming6c1ad482010-08-16 18:57:57 +0000130 case MCAF_SubsectionsViaSymbols:
131 getAssembler().setSubsectionsViaSymbols(true);
132 return;
133 }
134
Craig Toppera2886c22012-02-07 05:05:23 +0000135 llvm_unreachable("invalid assembler flag!");
Matt Fleming6c1ad482010-08-16 18:57:57 +0000136}
137
Derek Schuff2a1678a2015-04-21 00:14:25 +0000138// If bundle aligment is used and there are any instructions in the section, it
139// needs to be aligned to at least the bundle size.
Rafael Espindolacd625182015-05-25 18:34:26 +0000140static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
Rafael Espindolae6e287d2015-05-26 14:42:52 +0000141 MCSection *Section) {
142 if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
143 Section->getAlignment() < Assembler.getBundleAlignSize())
144 Section->setAlignment(Assembler.getBundleAlignSize());
Derek Schuff2a1678a2015-04-21 00:14:25 +0000145}
146
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000147void MCELFStreamer::ChangeSection(MCSection *Section,
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000148 const MCExpr *Subsection) {
Rafael Espindola983bec62015-05-27 21:04:14 +0000149 MCSection *CurSection = getCurrentSectionOnly();
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000150 if (CurSection && isBundleLocked())
Eli Benderskyf483ff92012-12-20 19:05:53 +0000151 report_fatal_error("Unterminated .bundle_lock when changing a section");
Rafael Espindolab6613022014-10-17 01:48:58 +0000152
153 MCAssembler &Asm = getAssembler();
Derek Schuff2a1678a2015-04-21 00:14:25 +0000154 // Ensure the previous section gets aligned if necessary.
155 setSectionAlignmentForBundling(Asm, CurSection);
Rafael Espindolab6613022014-10-17 01:48:58 +0000156 auto *SectionELF = static_cast<const MCSectionELF *>(Section);
157 const MCSymbol *Grp = SectionELF->getGroup();
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000158 if (Grp)
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000159 Asm.registerSymbol(*Grp);
Rafael Espindolab6613022014-10-17 01:48:58 +0000160
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000161 this->MCObjectStreamer::ChangeSection(Section, Subsection);
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000162 MCContext &Ctx = getContext();
163 MCSymbol *Begin = Section->getBeginSymbol();
164 if (!Begin) {
165 Begin = Ctx.getOrCreateSectionSymbol(*SectionELF);
166 Section->setBeginSymbol(Begin);
Rafael Espindolab6613022014-10-17 01:48:58 +0000167 }
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000168 if (Begin->isUndefined()) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000169 Asm.registerSymbol(*Begin);
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000170 MCELF::SetType(*Begin, ELF::STT_SECTION);
171 }
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000172}
173
Rafael Espindola16145972010-11-01 14:28:48 +0000174void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000175 getAssembler().registerSymbol(*Symbol);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000176 const MCExpr *Value = MCSymbolRefExpr::create(
Rafael Espindola7fadc0e2014-03-20 02:12:01 +0000177 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
Rafael Espindola16145972010-11-01 14:28:48 +0000178 Alias->setVariableValue(Value);
179}
180
Peter Collingbourneadac4072013-04-10 16:52:15 +0000181// When GNU as encounters more than one .type declaration for an object it seems
182// to use a mechanism similar to the one below to decide which type is actually
183// used in the object file. The greater of T1 and T2 is selected based on the
184// following ordering:
185// STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
186// If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
187// provided type).
188static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000189 for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
190 ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
191 if (T1 == Type)
Peter Collingbourneadac4072013-04-10 16:52:15 +0000192 return T2;
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000193 if (T2 == Type)
Peter Collingbourneadac4072013-04-10 16:52:15 +0000194 return T1;
195 }
196
197 return T2;
198}
199
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000200bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
201 MCSymbolAttr Attribute) {
Matt Fleming6c1ad482010-08-16 18:57:57 +0000202 // Indirect symbols are handled differently, to match how 'as' handles
203 // them. This makes writing matching .o files easier.
204 if (Attribute == MCSA_IndirectSymbol) {
205 // Note that we intentionally cannot use the symbol data here; this is
206 // important for matching the string table that 'as' generates.
207 IndirectSymbolData ISD;
208 ISD.Symbol = Symbol;
Rafael Espindola983bec62015-05-27 21:04:14 +0000209 ISD.Section = getCurrentSectionOnly();
Matt Fleming6c1ad482010-08-16 18:57:57 +0000210 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000211 return true;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000212 }
213
214 // Adding a symbol attribute always introduces the symbol, note that an
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000215 // important side effect of calling registerSymbol here is to register
Matt Fleming6c1ad482010-08-16 18:57:57 +0000216 // the symbol with the assembler.
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000217 getAssembler().registerSymbol(*Symbol);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000218
219 // The implementation of symbol attributes is designed to match 'as', but it
220 // leaves much to desired. It doesn't really make sense to arbitrarily add and
221 // remove flags, but 'as' allows this (in particular, see .desc).
222 //
223 // In the future it might be worth trying to make these operations more well
224 // defined.
225 switch (Attribute) {
226 case MCSA_LazyReference:
227 case MCSA_Reference:
Kevin Enderby8be14412010-11-19 18:39:33 +0000228 case MCSA_SymbolResolver:
Matt Fleming6c1ad482010-08-16 18:57:57 +0000229 case MCSA_PrivateExtern:
Matt Fleming6c1ad482010-08-16 18:57:57 +0000230 case MCSA_WeakDefinition:
Eli Friedmanb20b5242010-08-16 19:15:06 +0000231 case MCSA_WeakDefAutoPrivate:
Matt Fleming6c1ad482010-08-16 18:57:57 +0000232 case MCSA_Invalid:
Matt Fleming6c1ad482010-08-16 18:57:57 +0000233 case MCSA_IndirectSymbol:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000234 return false;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000235
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000236 case MCSA_NoDeadStrip:
Rafael Espindolaeba90222010-11-14 01:34:31 +0000237 // Ignore for now.
238 break;
239
Rafael Espindola5fa925e2015-01-23 04:44:35 +0000240 case MCSA_ELF_TypeGnuUniqueObject:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000241 MCELF::SetType(
242 *Symbol, CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_OBJECT));
243 MCELF::SetBinding(*Symbol, ELF::STB_GNU_UNIQUE);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000244 Symbol->setExternal(true);
Rafael Espindola5fa925e2015-01-23 04:44:35 +0000245 BindingExplicitlySet.insert(Symbol);
246 break;
247
Matt Fleming6c1ad482010-08-16 18:57:57 +0000248 case MCSA_Global:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000249 MCELF::SetBinding(*Symbol, ELF::STB_GLOBAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000250 Symbol->setExternal(true);
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000251 BindingExplicitlySet.insert(Symbol);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000252 break;
253
Benjamin Kramer5af02b02010-09-02 17:18:32 +0000254 case MCSA_WeakReference:
Matt Fleming6c1ad482010-08-16 18:57:57 +0000255 case MCSA_Weak:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000256 MCELF::SetBinding(*Symbol, ELF::STB_WEAK);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000257 Symbol->setExternal(true);
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000258 BindingExplicitlySet.insert(Symbol);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000259 break;
260
261 case MCSA_Local:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000262 MCELF::SetBinding(*Symbol, ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000263 Symbol->setExternal(false);
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000264 BindingExplicitlySet.insert(Symbol);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000265 break;
266
267 case MCSA_ELF_TypeFunction:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000268 MCELF::SetType(*Symbol,
269 CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_FUNC));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000270 break;
271
Roman Divacky735cb8b2011-12-12 17:34:04 +0000272 case MCSA_ELF_TypeIndFunction:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000273 MCELF::SetType(*Symbol, CombineSymbolTypes(MCELF::GetType(*Symbol),
274 ELF::STT_GNU_IFUNC));
Roman Divacky735cb8b2011-12-12 17:34:04 +0000275 break;
276
Matt Fleming6c1ad482010-08-16 18:57:57 +0000277 case MCSA_ELF_TypeObject:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000278 MCELF::SetType(
279 *Symbol, CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_OBJECT));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000280 break;
281
282 case MCSA_ELF_TypeTLS:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000283 MCELF::SetType(*Symbol,
284 CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_TLS));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000285 break;
286
287 case MCSA_ELF_TypeCommon:
Peter Collingbourneadac4072013-04-10 16:52:15 +0000288 // TODO: Emit these as a common symbol.
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000289 MCELF::SetType(
290 *Symbol, CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_OBJECT));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000291 break;
292
293 case MCSA_ELF_TypeNoType:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000294 MCELF::SetType(
295 *Symbol, CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_NOTYPE));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000296 break;
297
298 case MCSA_Protected:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000299 MCELF::SetVisibility(*Symbol, ELF::STV_PROTECTED);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000300 break;
301
302 case MCSA_Hidden:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000303 MCELF::SetVisibility(*Symbol, ELF::STV_HIDDEN);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000304 break;
305
306 case MCSA_Internal:
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000307 MCELF::SetVisibility(*Symbol, ELF::STV_INTERNAL);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000308 break;
309 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000310
311 return true;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000312}
313
314void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000315 unsigned ByteAlignment) {
316 getAssembler().registerSymbol(*Symbol);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000317
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000318 if (!BindingExplicitlySet.count(Symbol)) {
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000319 MCELF::SetBinding(*Symbol, ELF::STB_GLOBAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000320 Symbol->setExternal(true);
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000321 }
322
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000323 MCELF::SetType(*Symbol, ELF::STT_OBJECT);
Rafael Espindolaa7d0bed2010-11-14 21:11:16 +0000324
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000325 if (MCELF::GetBinding(*Symbol) == ELF_STB_Local) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000326 MCSection *Section = getAssembler().getContext().getELFSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000327 ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
Richard Mitton21101b32013-09-19 23:21:01 +0000328
329 AssignSection(Symbol, Section);
Rafael Espindola9bac6cb2010-09-22 17:43:04 +0000330
Duncan P. N. Exon Smithfd28abc2015-05-20 18:25:40 +0000331 struct LocalCommon L = {Symbol, Size, ByteAlignment};
Rafael Espindola53f0bf12010-09-29 14:52:01 +0000332 LocalCommons.push_back(L);
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000333 } else {
Rafael Espindola14672502015-05-29 17:48:04 +0000334 Symbol->setCommon(Size, ByteAlignment);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000335 }
336
Rafael Espindolaa8695762015-06-02 00:25:12 +0000337 cast<MCSymbolELF>(Symbol)
338 ->setSize(MCConstantExpr::create(Size, getContext()));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000339}
340
Rafael Espindolaa8695762015-06-02 00:25:12 +0000341void MCELFStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) {
Rafael Espindola7c23cba2015-05-29 17:24:52 +0000342 Symbol->setSize(Value);
Logan Chien59ff0702012-12-07 15:50:40 +0000343}
344
Benjamin Kramer63970512011-09-01 23:04:27 +0000345void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
346 unsigned ByteAlignment) {
Jason W Kim4c1386a2010-12-16 03:12:17 +0000347 // FIXME: Should this be caught and done earlier?
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000348 getAssembler().registerSymbol(*Symbol);
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000349 MCELF::SetBinding(*Symbol, ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000350 Symbol->setExternal(false);
Jason W Kim4c1386a2010-12-16 03:12:17 +0000351 BindingExplicitlySet.insert(Symbol);
Benjamin Kramer63970512011-09-01 23:04:27 +0000352 EmitCommonSymbol(Symbol, Size, ByteAlignment);
Jason W Kim4c1386a2010-12-16 03:12:17 +0000353}
354
Kevin Enderby96918bc2014-04-22 17:27:29 +0000355void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
356 const SMLoc &Loc) {
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000357 if (isBundleLocked())
Eli Benderskyf483ff92012-12-20 19:05:53 +0000358 report_fatal_error("Emitting values inside a locked bundle is forbidden");
David Meyer44ec69e2012-02-15 15:09:06 +0000359 fixSymbolsInTLSFixups(Value);
Kevin Enderby96918bc2014-04-22 17:27:29 +0000360 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
David Meyer44ec69e2012-02-15 15:09:06 +0000361}
362
Eli Benderskyf483ff92012-12-20 19:05:53 +0000363void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
364 int64_t Value,
365 unsigned ValueSize,
366 unsigned MaxBytesToEmit) {
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000367 if (isBundleLocked())
Eli Benderskyf483ff92012-12-20 19:05:53 +0000368 report_fatal_error("Emitting values inside a locked bundle is forbidden");
369 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
370 ValueSize, MaxBytesToEmit);
371}
372
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000373// Add a symbol for the file name of this module. They start after the
374// null symbol and don't count as normal symbol, i.e. a non-STT_FILE symbol
375// with the same name may appear.
Matt Fleming6c1ad482010-08-16 18:57:57 +0000376void MCELFStreamer::EmitFileDirective(StringRef Filename) {
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000377 getAssembler().addFileName(Filename);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000378}
379
Rafael Espindola5645bad2013-10-16 01:05:45 +0000380void MCELFStreamer::EmitIdent(StringRef IdentString) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000381 MCSection *Comment = getAssembler().getContext().getELFSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000382 ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
Rafael Espindola5645bad2013-10-16 01:05:45 +0000383 PushSection();
384 SwitchSection(Comment);
385 if (!SeenIdent) {
386 EmitIntValue(0, 1);
387 SeenIdent = true;
388 }
389 EmitBytes(IdentString);
390 EmitIntValue(0, 1);
391 PopSection();
392}
393
394void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
Rafael Espindola4e70ac72010-11-24 02:19:40 +0000395 switch (expr->getKind()) {
Tim Northovere0e3aef2013-01-31 12:12:40 +0000396 case MCExpr::Target:
397 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
398 break;
Rafael Espindola4e70ac72010-11-24 02:19:40 +0000399 case MCExpr::Constant:
400 break;
401
402 case MCExpr::Binary: {
403 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
404 fixSymbolsInTLSFixups(be->getLHS());
405 fixSymbolsInTLSFixups(be->getRHS());
406 break;
407 }
408
409 case MCExpr::SymbolRef: {
410 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
Rafael Espindolae98d4832010-11-24 18:51:21 +0000411 switch (symRef.getKind()) {
412 default:
Rafael Espindola4e70ac72010-11-24 02:19:40 +0000413 return;
Joerg Sonnenberger07de07e2011-03-17 00:35:10 +0000414 case MCSymbolRefExpr::VK_GOTTPOFF:
415 case MCSymbolRefExpr::VK_INDNTPOFF:
Rafael Espindolae98d4832010-11-24 18:51:21 +0000416 case MCSymbolRefExpr::VK_NTPOFF:
417 case MCSymbolRefExpr::VK_GOTNTPOFF:
418 case MCSymbolRefExpr::VK_TLSGD:
Joerg Sonnenberger07de07e2011-03-17 00:35:10 +0000419 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindolae98d4832010-11-24 18:51:21 +0000420 case MCSymbolRefExpr::VK_TLSLDM:
421 case MCSymbolRefExpr::VK_TPOFF:
422 case MCSymbolRefExpr::VK_DTPOFF:
Bruno Cardoso Lopes82b077e2011-10-25 18:13:20 +0000423 case MCSymbolRefExpr::VK_Mips_TLSGD:
424 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
425 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
426 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
Ulrich Weigandf11efe72013-07-01 23:33:29 +0000427 case MCSymbolRefExpr::VK_PPC_DTPMOD:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000428 case MCSymbolRefExpr::VK_PPC_TPREL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000429 case MCSymbolRefExpr::VK_PPC_TPREL_LO:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000430 case MCSymbolRefExpr::VK_PPC_TPREL_HI:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000431 case MCSymbolRefExpr::VK_PPC_TPREL_HA:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000432 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
433 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
434 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
435 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
436 case MCSymbolRefExpr::VK_PPC_DTPREL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000437 case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000438 case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000439 case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000440 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
441 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
442 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
443 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
444 case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000445 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000446 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000447 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000448 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
449 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
450 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
451 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
Bill Schmidt441907d2013-02-26 16:41:03 +0000452 case MCSymbolRefExpr::VK_PPC_TLS:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000453 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000454 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000455 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000456 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
Ulrich Weigand52cf8e42013-07-09 16:41:09 +0000457 case MCSymbolRefExpr::VK_PPC_TLSGD:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000458 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000459 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000460 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000461 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
Ulrich Weigand52cf8e42013-07-09 16:41:09 +0000462 case MCSymbolRefExpr::VK_PPC_TLSLD:
Rafael Espindolae98d4832010-11-24 18:51:21 +0000463 break;
464 }
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000465 getAssembler().registerSymbol(symRef.getSymbol());
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000466 MCELF::SetType(symRef.getSymbol(), ELF::STT_TLS);
Rafael Espindola4e70ac72010-11-24 02:19:40 +0000467 break;
468 }
469
470 case MCExpr::Unary:
471 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
472 break;
473 }
474}
475
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000476void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
477 const MCSubtargetInfo &STI) {
478 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000479 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
Benjamin Kramer1f601242010-08-27 10:40:51 +0000480
Rafael Espindola3fa6fc12010-12-02 05:44:06 +0000481 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
482 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
Benjamin Kramer1f601242010-08-27 10:40:51 +0000483}
484
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000485void MCELFStreamer::EmitInstToData(const MCInst &Inst,
486 const MCSubtargetInfo &STI) {
Eli Benderskyf483ff92012-12-20 19:05:53 +0000487 MCAssembler &Assembler = getAssembler();
Benjamin Kramer1f601242010-08-27 10:40:51 +0000488 SmallVector<MCFixup, 4> Fixups;
489 SmallString<256> Code;
490 raw_svector_ostream VecOS(Code);
Jim Grosbach91df21f2015-05-15 19:13:16 +0000491 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
Benjamin Kramer1f601242010-08-27 10:40:51 +0000492 VecOS.flush();
493
Rafael Espindola4e70ac72010-11-24 02:19:40 +0000494 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
495 fixSymbolsInTLSFixups(Fixups[i].getValue());
496
Eli Benderskyf483ff92012-12-20 19:05:53 +0000497 // There are several possibilities here:
498 //
499 // If bundling is disabled, append the encoded instruction to the current data
500 // fragment (or create a new such fragment if the current fragment is not a
501 // data fragment).
502 //
503 // If bundling is enabled:
Eli Benderskycf6009b2013-01-15 23:22:09 +0000504 // - If we're not in a bundle-locked group, emit the instruction into a
505 // fragment of its own. If there are no fixups registered for the
506 // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
507 // MCDataFragment.
Eli Benderskyf483ff92012-12-20 19:05:53 +0000508 // - If we're in a bundle-locked group, append the instruction to the current
509 // data fragment because we want all the instructions in a group to get into
510 // the same fragment. Be careful not to do that for the first instruction in
511 // the group, though.
512 MCDataFragment *DF;
513
514 if (Assembler.isBundlingEnabled()) {
Rafael Espindola983bec62015-05-27 21:04:14 +0000515 MCSection &Sec = *getCurrentSectionOnly();
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000516 if (Assembler.getRelaxAll() && isBundleLocked())
Petr Hosek9e0c8902015-04-12 23:42:25 +0000517 // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
518 // the current bundle group.
519 DF = BundleGroups.back();
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000520 else if (Assembler.getRelaxAll() && !isBundleLocked())
Petr Hosek9e0c8902015-04-12 23:42:25 +0000521 // When not in a bundle-locked group and the -mc-relax-all flag is used,
522 // we create a new temporary fragment which will be later merged into
523 // the current fragment.
524 DF = new MCDataFragment();
Rafael Espindolab028cc82015-05-25 15:04:26 +0000525 else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst())
Derek Schuff8878bcc92013-02-15 22:50:52 +0000526 // If we are bundle-locked, we re-use the current fragment.
527 // The bundle-locking directive ensures this is a new data fragment.
528 DF = cast<MCDataFragment>(getCurrentFragment());
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000529 else if (!isBundleLocked() && Fixups.size() == 0) {
Eli Benderskycf6009b2013-01-15 23:22:09 +0000530 // Optimize memory usage by emitting the instruction to a
531 // MCCompactEncodedInstFragment when not in a bundle-locked group and
532 // there are no fixups registered.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000533 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
534 insert(CEIF);
Eli Benderskycf6009b2013-01-15 23:22:09 +0000535 CEIF->getContents().append(Code.begin(), Code.end());
536 return;
Derek Schuff8878bcc92013-02-15 22:50:52 +0000537 } else {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000538 DF = new MCDataFragment();
539 insert(DF);
Derek Schuff05fb7352014-10-15 17:10:04 +0000540 }
Rafael Espindolab028cc82015-05-25 15:04:26 +0000541 if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
Derek Schuff05fb7352014-10-15 17:10:04 +0000542 // If this fragment is for a group marked "align_to_end", set a flag
543 // in the fragment. This can happen after the fragment has already been
544 // created if there are nested bundle_align groups and an inner one
545 // is the one marked align_to_end.
546 DF->setAlignToBundleEnd(true);
Eli Bendersky802b6282013-01-07 21:51:08 +0000547 }
Eli Benderskyf483ff92012-12-20 19:05:53 +0000548
549 // We're now emitting an instruction in a bundle group, so this flag has
550 // to be turned off.
Rafael Espindolab028cc82015-05-25 15:04:26 +0000551 Sec.setBundleGroupBeforeFirstInst(false);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000552 } else {
553 DF = getOrCreateDataFragment();
554 }
555
Benjamin Kramer1f601242010-08-27 10:40:51 +0000556 // Add the fixups and data.
557 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
558 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Benderskya31a8942012-12-07 19:13:57 +0000559 DF->getFixups().push_back(Fixups[i]);
Benjamin Kramer1f601242010-08-27 10:40:51 +0000560 }
Eli Benderskyf483ff92012-12-20 19:05:53 +0000561 DF->setHasInstructions(true);
Benjamin Kramer1f601242010-08-27 10:40:51 +0000562 DF->getContents().append(Code.begin(), Code.end());
Petr Hosek9e0c8902015-04-12 23:42:25 +0000563
564 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000565 if (!isBundleLocked()) {
Petr Hosek9e0c8902015-04-12 23:42:25 +0000566 mergeFragment(getOrCreateDataFragment(), DF);
567 delete DF;
568 }
569 }
Benjamin Kramer1f601242010-08-27 10:40:51 +0000570}
571
Eli Benderskyf483ff92012-12-20 19:05:53 +0000572void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
573 assert(AlignPow2 <= 30 && "Invalid bundle alignment");
574 MCAssembler &Assembler = getAssembler();
Derek Schuff05fb7352014-10-15 17:10:04 +0000575 if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
576 Assembler.getBundleAlignSize() == 1U << AlignPow2))
577 Assembler.setBundleAlignSize(1U << AlignPow2);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000578 else
Derek Schuff05fb7352014-10-15 17:10:04 +0000579 report_fatal_error(".bundle_align_mode cannot be changed once set");
Eli Benderskyf483ff92012-12-20 19:05:53 +0000580}
581
Eli Bendersky802b6282013-01-07 21:51:08 +0000582void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
Rafael Espindola983bec62015-05-27 21:04:14 +0000583 MCSection &Sec = *getCurrentSectionOnly();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000584
585 // Sanity checks
586 //
587 if (!getAssembler().isBundlingEnabled())
588 report_fatal_error(".bundle_lock forbidden when bundling is disabled");
Derek Schuff05fb7352014-10-15 17:10:04 +0000589
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000590 if (!isBundleLocked())
Rafael Espindolab028cc82015-05-25 15:04:26 +0000591 Sec.setBundleGroupBeforeFirstInst(true);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000592
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000593 if (getAssembler().getRelaxAll() && !isBundleLocked()) {
Petr Hosek9e0c8902015-04-12 23:42:25 +0000594 // TODO: drop the lock state and set directly in the fragment
595 MCDataFragment *DF = new MCDataFragment();
596 BundleGroups.push_back(DF);
597 }
598
Rafael Espindolab028cc82015-05-25 15:04:26 +0000599 Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
600 : MCSection::BundleLocked);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000601}
602
603void MCELFStreamer::EmitBundleUnlock() {
Rafael Espindola983bec62015-05-27 21:04:14 +0000604 MCSection &Sec = *getCurrentSectionOnly();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000605
606 // Sanity checks
607 if (!getAssembler().isBundlingEnabled())
608 report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000609 else if (!isBundleLocked())
Eli Benderskyf483ff92012-12-20 19:05:53 +0000610 report_fatal_error(".bundle_unlock without matching lock");
Rafael Espindolab028cc82015-05-25 15:04:26 +0000611 else if (Sec.isBundleGroupBeforeFirstInst())
Eli Benderskyf483ff92012-12-20 19:05:53 +0000612 report_fatal_error("Empty bundle-locked group is forbidden");
613
Petr Hosek9e0c8902015-04-12 23:42:25 +0000614 // When the -mc-relax-all flag is used, we emit instructions to fragments
615 // stored on a stack. When the bundle unlock is emited, we pop a fragment
616 // from the stack a merge it to the one below.
617 if (getAssembler().getRelaxAll()) {
618 assert(!BundleGroups.empty() && "There are no bundle groups");
619 MCDataFragment *DF = BundleGroups.back();
620
621 // FIXME: Use BundleGroups to track the lock state instead.
Rafael Espindolab028cc82015-05-25 15:04:26 +0000622 Sec.setBundleLockState(MCSection::NotBundleLocked);
Petr Hosek9e0c8902015-04-12 23:42:25 +0000623
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000624 // FIXME: Use more separate fragments for nested groups.
625 if (!isBundleLocked()) {
Petr Hosek9e0c8902015-04-12 23:42:25 +0000626 mergeFragment(getOrCreateDataFragment(), DF);
627 BundleGroups.pop_back();
628 delete DF;
629 }
630
Rafael Espindolab028cc82015-05-25 15:04:26 +0000631 if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
Petr Hosek9e0c8902015-04-12 23:42:25 +0000632 getOrCreateDataFragment()->setAlignToBundleEnd(false);
633 } else
Rafael Espindolab028cc82015-05-25 15:04:26 +0000634 Sec.setBundleLockState(MCSection::NotBundleLocked);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000635}
636
Richard Mitton21101b32013-09-19 23:21:01 +0000637void MCELFStreamer::Flush() {
Rafael Espindola53f0bf12010-09-29 14:52:01 +0000638 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
639 e = LocalCommons.end();
640 i != e; ++i) {
Duncan P. N. Exon Smithfd28abc2015-05-20 18:25:40 +0000641 const MCSymbol &Symbol = *i->Symbol;
Rafael Espindola53f0bf12010-09-29 14:52:01 +0000642 uint64_t Size = i->Size;
643 unsigned ByteAlignment = i->ByteAlignment;
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000644 MCSection &Section = Symbol.getSection();
Rafael Espindola53f0bf12010-09-29 14:52:01 +0000645
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000646 getAssembler().registerSection(Section);
647 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &Section);
Rafael Espindola53f0bf12010-09-29 14:52:01 +0000648
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000649 MCFragment *F = new MCFillFragment(0, 0, Size, &Section);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000650 Symbol.setFragment(F);
Rafael Espindola53f0bf12010-09-29 14:52:01 +0000651
652 // Update the maximum alignment of the section if necessary.
Rafael Espindola967d6a62015-05-21 21:02:35 +0000653 if (ByteAlignment > Section.getAlignment())
654 Section.setAlignment(ByteAlignment);
Rafael Espindola53f0bf12010-09-29 14:52:01 +0000655 }
656
Richard Mitton21101b32013-09-19 23:21:01 +0000657 LocalCommons.clear();
658}
659
660void MCELFStreamer::FinishImpl() {
Derek Schuff2a1678a2015-04-21 00:14:25 +0000661 // Ensure the last section gets aligned if necessary.
Rafael Espindola983bec62015-05-27 21:04:14 +0000662 MCSection *CurSection = getCurrentSectionOnly();
Rafael Espindolae6e287d2015-05-26 14:42:52 +0000663 setSectionAlignmentForBundling(getAssembler(), CurSection);
Derek Schuff2a1678a2015-04-21 00:14:25 +0000664
Rafael Espindola7f4ccce2014-05-12 13:30:10 +0000665 EmitFrames(nullptr);
Richard Mitton21101b32013-09-19 23:21:01 +0000666
667 Flush();
668
Rafael Espindola07082092012-01-07 03:13:18 +0000669 this->MCObjectStreamer::FinishImpl();
Matt Fleming6c1ad482010-08-16 18:57:57 +0000670}
Richard Mitton21101b32013-09-19 23:21:01 +0000671
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000672MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000673 raw_pwrite_stream &OS, MCCodeEmitter *CE,
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000674 bool RelaxAll) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000675 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000676 if (RelaxAll)
677 S->getAssembler().setRelaxAll(true);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000678 return S;
679}
Logan Chien59ff0702012-12-07 15:50:40 +0000680
Tim Northover5cc3dc82012-12-07 16:50:23 +0000681void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
682 llvm_unreachable("Generic ELF doesn't support this directive");
683}
684
Logan Chien59ff0702012-12-07 15:50:40 +0000685void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
686 llvm_unreachable("ELF doesn't support this directive");
687}
688
689void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
690 llvm_unreachable("ELF doesn't support this directive");
691}
692
693void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
694 llvm_unreachable("ELF doesn't support this directive");
695}
696
697void MCELFStreamer::EmitCOFFSymbolType(int Type) {
698 llvm_unreachable("ELF doesn't support this directive");
699}
700
701void MCELFStreamer::EndCOFFSymbolDef() {
702 llvm_unreachable("ELF doesn't support this directive");
703}
704
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000705void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
Logan Chien59ff0702012-12-07 15:50:40 +0000706 uint64_t Size, unsigned ByteAlignment) {
707 llvm_unreachable("ELF doesn't support this directive");
708}
709
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000710void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
Logan Chien59ff0702012-12-07 15:50:40 +0000711 uint64_t Size, unsigned ByteAlignment) {
712 llvm_unreachable("ELF doesn't support this directive");
713}