blob: 7d858c306d2ed6a8838fa1ae9aa86e07cb8e0a93 [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"
Matt Fleming6c1ad482010-08-16 18:57:57 +000023#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCInst.h"
Rafael Espindolae308c0c2014-01-23 22:49:25 +000025#include "llvm/MC/MCObjectFileInfo.h"
Rafael Espindola81a62742012-01-07 23:18:39 +000026#include "llvm/MC/MCObjectStreamer.h"
Reid Kleckner0801d122016-06-22 23:25:26 +000027#include "llvm/MC/MCObjectWriter.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000028#include "llvm/MC/MCSection.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/MC/MCSectionELF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000030#include "llvm/MC/MCSymbolELF.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000031#include "llvm/MC/MCSymbol.h"
Rafael Espindola16145972010-11-01 14:28:48 +000032#include "llvm/MC/MCValue.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000033#include "llvm/Support/Debug.h"
34#include "llvm/Support/ELF.h"
35#include "llvm/Support/ErrorHandling.h"
Rafael Espindolacd584a82015-03-19 01:50:16 +000036#include "llvm/Support/TargetRegistry.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000037#include "llvm/Support/raw_ostream.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000038
39using namespace llvm;
40
Rafael Espindolac1cd9442015-05-25 14:57:35 +000041bool MCELFStreamer::isBundleLocked() const {
Rafael Espindola983bec62015-05-27 21:04:14 +000042 return getCurrentSectionOnly()->isBundleLocked();
Rafael Espindolac1cd9442015-05-25 14:57:35 +000043}
44
Logan Chien59ff0702012-12-07 15:50:40 +000045MCELFStreamer::~MCELFStreamer() {
Rafael Espindola81a62742012-01-07 23:18:39 +000046}
47
Petr Hosek9e0c8902015-04-12 23:42:25 +000048void MCELFStreamer::mergeFragment(MCDataFragment *DF,
Pete Coopere0d40372015-06-17 22:01:28 +000049 MCDataFragment *EF) {
Petr Hosek9e0c8902015-04-12 23:42:25 +000050 MCAssembler &Assembler = getAssembler();
51
52 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
53 uint64_t FSize = EF->getContents().size();
54
55 if (FSize > Assembler.getBundleAlignSize())
56 report_fatal_error("Fragment can't be larger than a bundle size");
57
58 uint64_t RequiredBundlePadding = computeBundlePadding(
59 Assembler, EF, DF->getContents().size(), FSize);
60
61 if (RequiredBundlePadding > UINT8_MAX)
62 report_fatal_error("Padding cannot exceed 255 bytes");
63
64 if (RequiredBundlePadding > 0) {
65 SmallString<256> Code;
66 raw_svector_ostream VecOS(Code);
67 MCObjectWriter *OW = Assembler.getBackend().createObjectWriter(VecOS);
68
69 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
70
71 Assembler.writeFragmentPadding(*EF, FSize, OW);
Petr Hosek9e0c8902015-04-12 23:42:25 +000072 delete OW;
73
74 DF->getContents().append(Code.begin(), Code.end());
75 }
76 }
77
78 flushPendingLabels(DF, DF->getContents().size());
79
80 for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
81 EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
82 DF->getContents().size());
83 DF->getFixups().push_back(EF->getFixups()[i]);
84 }
85 DF->setHasInstructions(true);
86 DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
87}
88
Rafael Espindola7b61ddf2014-10-15 16:12:52 +000089void MCELFStreamer::InitSections(bool NoExecStack) {
Rafael Espindola7b61ddf2014-10-15 16:12:52 +000090 MCContext &Ctx = getContext();
91 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
Rafael Espindola7b514962014-02-04 18:34:04 +000092 EmitCodeAlignment(4);
Rafael Espindola247f9512014-01-24 02:18:40 +000093
Rafael Espindola7b61ddf2014-10-15 16:12:52 +000094 if (NoExecStack)
95 SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
Rafael Espindolaf667d922010-09-15 21:48:40 +000096}
97
Rafael Espindola95fb9b92015-06-02 20:38:46 +000098void MCELFStreamer::EmitLabel(MCSymbol *S) {
99 auto *Symbol = cast<MCSymbolELF>(S);
Rafael Espindola16795802010-11-28 16:22:59 +0000100 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
101
Rafael Espindolae5b74152010-11-28 17:18:55 +0000102 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola75d65b92010-09-25 05:42:19 +0000103
Rafael Espindola84d03182010-11-11 19:04:55 +0000104 const MCSectionELF &Section =
Rafael Espindolae3a20f52015-10-05 12:07:05 +0000105 static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000106 if (Section.getFlags() & ELF::SHF_TLS)
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000107 Symbol->setType(ELF::STT_TLS);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000108}
109
110void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
David Peixotto72667312013-11-25 19:11:13 +0000111 // Let the target do whatever target specific stuff it needs to do.
112 getAssembler().getBackend().handleAssemblerFlag(Flag);
113 // Do any generic stuff we need to do.
Matt Fleming6c1ad482010-08-16 18:57:57 +0000114 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000115 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000116 case MCAF_Code16: return; // Change parsing mode; no-op here.
117 case MCAF_Code32: return; // Change parsing mode; no-op here.
118 case MCAF_Code64: return; // Change parsing mode; no-op here.
Matt Fleming6c1ad482010-08-16 18:57:57 +0000119 case MCAF_SubsectionsViaSymbols:
120 getAssembler().setSubsectionsViaSymbols(true);
121 return;
122 }
123
Craig Toppera2886c22012-02-07 05:05:23 +0000124 llvm_unreachable("invalid assembler flag!");
Matt Fleming6c1ad482010-08-16 18:57:57 +0000125}
126
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000127// If bundle alignment is used and there are any instructions in the section, it
Derek Schuff2a1678a2015-04-21 00:14:25 +0000128// needs to be aligned to at least the bundle size.
Rafael Espindolacd625182015-05-25 18:34:26 +0000129static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
Rafael Espindolae6e287d2015-05-26 14:42:52 +0000130 MCSection *Section) {
131 if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
132 Section->getAlignment() < Assembler.getBundleAlignSize())
133 Section->setAlignment(Assembler.getBundleAlignSize());
Derek Schuff2a1678a2015-04-21 00:14:25 +0000134}
135
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000136void MCELFStreamer::ChangeSection(MCSection *Section,
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000137 const MCExpr *Subsection) {
Rafael Espindola983bec62015-05-27 21:04:14 +0000138 MCSection *CurSection = getCurrentSectionOnly();
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000139 if (CurSection && isBundleLocked())
Eli Benderskyf483ff92012-12-20 19:05:53 +0000140 report_fatal_error("Unterminated .bundle_lock when changing a section");
Rafael Espindolab6613022014-10-17 01:48:58 +0000141
142 MCAssembler &Asm = getAssembler();
Derek Schuff2a1678a2015-04-21 00:14:25 +0000143 // Ensure the previous section gets aligned if necessary.
144 setSectionAlignmentForBundling(Asm, CurSection);
Rafael Espindolab6613022014-10-17 01:48:58 +0000145 auto *SectionELF = static_cast<const MCSectionELF *>(Section);
146 const MCSymbol *Grp = SectionELF->getGroup();
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000147 if (Grp)
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000148 Asm.registerSymbol(*Grp);
Rafael Espindolab6613022014-10-17 01:48:58 +0000149
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000150 this->MCObjectStreamer::ChangeSection(Section, Subsection);
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000151 MCContext &Ctx = getContext();
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000152 auto *Begin = cast_or_null<MCSymbolELF>(Section->getBeginSymbol());
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000153 if (!Begin) {
154 Begin = Ctx.getOrCreateSectionSymbol(*SectionELF);
155 Section->setBeginSymbol(Begin);
Rafael Espindolab6613022014-10-17 01:48:58 +0000156 }
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000157 if (Begin->isUndefined()) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000158 Asm.registerSymbol(*Begin);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000159 Begin->setType(ELF::STT_SECTION);
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000160 }
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000161}
162
Rafael Espindola16145972010-11-01 14:28:48 +0000163void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000164 getAssembler().registerSymbol(*Symbol);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000165 const MCExpr *Value = MCSymbolRefExpr::create(
Rafael Espindola7fadc0e2014-03-20 02:12:01 +0000166 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
Rafael Espindola16145972010-11-01 14:28:48 +0000167 Alias->setVariableValue(Value);
168}
169
Peter Collingbourneadac4072013-04-10 16:52:15 +0000170// When GNU as encounters more than one .type declaration for an object it seems
171// to use a mechanism similar to the one below to decide which type is actually
172// used in the object file. The greater of T1 and T2 is selected based on the
173// following ordering:
174// STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
175// If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
176// provided type).
177static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000178 for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
179 ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
180 if (T1 == Type)
Peter Collingbourneadac4072013-04-10 16:52:15 +0000181 return T2;
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000182 if (T2 == Type)
Peter Collingbourneadac4072013-04-10 16:52:15 +0000183 return T1;
184 }
185
186 return T2;
187}
188
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000189bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
190 auto *Symbol = cast<MCSymbolELF>(S);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000191 // Indirect symbols are handled differently, to match how 'as' handles
192 // them. This makes writing matching .o files easier.
193 if (Attribute == MCSA_IndirectSymbol) {
194 // Note that we intentionally cannot use the symbol data here; this is
195 // important for matching the string table that 'as' generates.
196 IndirectSymbolData ISD;
197 ISD.Symbol = Symbol;
Rafael Espindola983bec62015-05-27 21:04:14 +0000198 ISD.Section = getCurrentSectionOnly();
Matt Fleming6c1ad482010-08-16 18:57:57 +0000199 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000200 return true;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000201 }
202
203 // Adding a symbol attribute always introduces the symbol, note that an
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000204 // important side effect of calling registerSymbol here is to register
Matt Fleming6c1ad482010-08-16 18:57:57 +0000205 // the symbol with the assembler.
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000206 getAssembler().registerSymbol(*Symbol);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000207
208 // The implementation of symbol attributes is designed to match 'as', but it
209 // leaves much to desired. It doesn't really make sense to arbitrarily add and
210 // remove flags, but 'as' allows this (in particular, see .desc).
211 //
212 // In the future it might be worth trying to make these operations more well
213 // defined.
214 switch (Attribute) {
215 case MCSA_LazyReference:
216 case MCSA_Reference:
Kevin Enderby8be14412010-11-19 18:39:33 +0000217 case MCSA_SymbolResolver:
Matt Fleming6c1ad482010-08-16 18:57:57 +0000218 case MCSA_PrivateExtern:
Matt Fleming6c1ad482010-08-16 18:57:57 +0000219 case MCSA_WeakDefinition:
Eli Friedmanb20b5242010-08-16 19:15:06 +0000220 case MCSA_WeakDefAutoPrivate:
Matt Fleming6c1ad482010-08-16 18:57:57 +0000221 case MCSA_Invalid:
Matt Fleming6c1ad482010-08-16 18:57:57 +0000222 case MCSA_IndirectSymbol:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000223 return false;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000224
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000225 case MCSA_NoDeadStrip:
Rafael Espindolaeba90222010-11-14 01:34:31 +0000226 // Ignore for now.
227 break;
228
Rafael Espindola5fa925e2015-01-23 04:44:35 +0000229 case MCSA_ELF_TypeGnuUniqueObject:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000230 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
231 Symbol->setBinding(ELF::STB_GNU_UNIQUE);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000232 Symbol->setExternal(true);
Rafael Espindola5fa925e2015-01-23 04:44:35 +0000233 break;
234
Matt Fleming6c1ad482010-08-16 18:57:57 +0000235 case MCSA_Global:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000236 Symbol->setBinding(ELF::STB_GLOBAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000237 Symbol->setExternal(true);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000238 break;
239
Benjamin Kramer5af02b02010-09-02 17:18:32 +0000240 case MCSA_WeakReference:
Matt Fleming6c1ad482010-08-16 18:57:57 +0000241 case MCSA_Weak:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000242 Symbol->setBinding(ELF::STB_WEAK);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000243 Symbol->setExternal(true);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000244 break;
245
246 case MCSA_Local:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000247 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000248 Symbol->setExternal(false);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000249 break;
250
251 case MCSA_ELF_TypeFunction:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000252 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000253 break;
254
Roman Divacky735cb8b2011-12-12 17:34:04 +0000255 case MCSA_ELF_TypeIndFunction:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000256 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
Roman Divacky735cb8b2011-12-12 17:34:04 +0000257 break;
258
Matt Fleming6c1ad482010-08-16 18:57:57 +0000259 case MCSA_ELF_TypeObject:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000260 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000261 break;
262
263 case MCSA_ELF_TypeTLS:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000264 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000265 break;
266
267 case MCSA_ELF_TypeCommon:
Peter Collingbourneadac4072013-04-10 16:52:15 +0000268 // TODO: Emit these as a common symbol.
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000269 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000270 break;
271
272 case MCSA_ELF_TypeNoType:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000273 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000274 break;
275
276 case MCSA_Protected:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000277 Symbol->setVisibility(ELF::STV_PROTECTED);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000278 break;
279
280 case MCSA_Hidden:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000281 Symbol->setVisibility(ELF::STV_HIDDEN);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000282 break;
283
284 case MCSA_Internal:
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000285 Symbol->setVisibility(ELF::STV_INTERNAL);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000286 break;
Lang Hames1b640e02016-03-15 01:43:05 +0000287
288 case MCSA_AltEntry:
Lang Hamesf9033bb2016-04-11 18:33:45 +0000289 llvm_unreachable("ELF doesn't support the .alt_entry attribute");
Matt Fleming6c1ad482010-08-16 18:57:57 +0000290 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000291
292 return true;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000293}
294
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000295void MCELFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000296 unsigned ByteAlignment) {
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000297 auto *Symbol = cast<MCSymbolELF>(S);
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000298 getAssembler().registerSymbol(*Symbol);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000299
Rafael Espindolaf6dcd2a2015-06-03 21:18:03 +0000300 if (!Symbol->isBindingSet()) {
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000301 Symbol->setBinding(ELF::STB_GLOBAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000302 Symbol->setExternal(true);
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000303 }
304
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000305 Symbol->setType(ELF::STT_OBJECT);
Rafael Espindolaa7d0bed2010-11-14 21:11:16 +0000306
Rafael Espindolaf8794ff2015-06-04 00:47:43 +0000307 if (Symbol->getBinding() == ELF::STB_LOCAL) {
Rafael Espindolae7fe1a42015-11-03 18:50:51 +0000308 MCSection &Section = *getAssembler().getContext().getELFSection(
309 ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
310 MCSectionSubPair P = getCurrentSection();
311 SwitchSection(&Section);
312
313 EmitValueToAlignment(ByteAlignment, 0, 1, 0);
314 EmitLabel(Symbol);
315 EmitZeros(Size);
316
317 // Update the maximum alignment of the section if necessary.
318 if (ByteAlignment > Section.getAlignment())
319 Section.setAlignment(ByteAlignment);
320
321 SwitchSection(P.first, P.second);
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000322 } else {
Colin LeMahieu1c8c2132015-06-06 20:12:40 +0000323 if(Symbol->declareCommon(Size, ByteAlignment))
324 report_fatal_error("Symbol: " + Symbol->getName() +
325 " redeclared as different type");
Matt Fleming6c1ad482010-08-16 18:57:57 +0000326 }
327
Rafael Espindolaa8695762015-06-02 00:25:12 +0000328 cast<MCSymbolELF>(Symbol)
329 ->setSize(MCConstantExpr::create(Size, getContext()));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000330}
331
Rafael Espindolaa8695762015-06-02 00:25:12 +0000332void MCELFStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) {
Rafael Espindola7c23cba2015-05-29 17:24:52 +0000333 Symbol->setSize(Value);
Logan Chien59ff0702012-12-07 15:50:40 +0000334}
335
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000336void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
Benjamin Kramer63970512011-09-01 23:04:27 +0000337 unsigned ByteAlignment) {
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000338 auto *Symbol = cast<MCSymbolELF>(S);
Jason W Kim4c1386a2010-12-16 03:12:17 +0000339 // FIXME: Should this be caught and done earlier?
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000340 getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000341 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000342 Symbol->setExternal(false);
Benjamin Kramer63970512011-09-01 23:04:27 +0000343 EmitCommonSymbol(Symbol, Size, ByteAlignment);
Jason W Kim4c1386a2010-12-16 03:12:17 +0000344}
345
Kevin Enderby96918bc2014-04-22 17:27:29 +0000346void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Craig Topper3c76c522015-09-20 23:35:59 +0000347 SMLoc Loc) {
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000348 if (isBundleLocked())
Eli Benderskyf483ff92012-12-20 19:05:53 +0000349 report_fatal_error("Emitting values inside a locked bundle is forbidden");
David Meyer44ec69e2012-02-15 15:09:06 +0000350 fixSymbolsInTLSFixups(Value);
Kevin Enderby96918bc2014-04-22 17:27:29 +0000351 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
David Meyer44ec69e2012-02-15 15:09:06 +0000352}
353
Eli Benderskyf483ff92012-12-20 19:05:53 +0000354void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
355 int64_t Value,
356 unsigned ValueSize,
357 unsigned MaxBytesToEmit) {
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000358 if (isBundleLocked())
Eli Benderskyf483ff92012-12-20 19:05:53 +0000359 report_fatal_error("Emitting values inside a locked bundle is forbidden");
360 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
361 ValueSize, MaxBytesToEmit);
362}
363
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000364// Add a symbol for the file name of this module. They start after the
365// null symbol and don't count as normal symbol, i.e. a non-STT_FILE symbol
366// with the same name may appear.
Matt Fleming6c1ad482010-08-16 18:57:57 +0000367void MCELFStreamer::EmitFileDirective(StringRef Filename) {
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000368 getAssembler().addFileName(Filename);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000369}
370
Rafael Espindola5645bad2013-10-16 01:05:45 +0000371void MCELFStreamer::EmitIdent(StringRef IdentString) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000372 MCSection *Comment = getAssembler().getContext().getELFSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000373 ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
Rafael Espindola5645bad2013-10-16 01:05:45 +0000374 PushSection();
375 SwitchSection(Comment);
376 if (!SeenIdent) {
377 EmitIntValue(0, 1);
378 SeenIdent = true;
379 }
380 EmitBytes(IdentString);
381 EmitIntValue(0, 1);
382 PopSection();
383}
384
385void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
Rafael Espindola4e70ac72010-11-24 02:19:40 +0000386 switch (expr->getKind()) {
Tim Northovere0e3aef2013-01-31 12:12:40 +0000387 case MCExpr::Target:
388 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
389 break;
Rafael Espindola4e70ac72010-11-24 02:19:40 +0000390 case MCExpr::Constant:
391 break;
392
393 case MCExpr::Binary: {
394 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
395 fixSymbolsInTLSFixups(be->getLHS());
396 fixSymbolsInTLSFixups(be->getRHS());
397 break;
398 }
399
400 case MCExpr::SymbolRef: {
401 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
Rafael Espindolae98d4832010-11-24 18:51:21 +0000402 switch (symRef.getKind()) {
403 default:
Rafael Espindola4e70ac72010-11-24 02:19:40 +0000404 return;
Joerg Sonnenberger07de07e2011-03-17 00:35:10 +0000405 case MCSymbolRefExpr::VK_GOTTPOFF:
406 case MCSymbolRefExpr::VK_INDNTPOFF:
Rafael Espindolae98d4832010-11-24 18:51:21 +0000407 case MCSymbolRefExpr::VK_NTPOFF:
408 case MCSymbolRefExpr::VK_GOTNTPOFF:
409 case MCSymbolRefExpr::VK_TLSGD:
Joerg Sonnenberger07de07e2011-03-17 00:35:10 +0000410 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindolae98d4832010-11-24 18:51:21 +0000411 case MCSymbolRefExpr::VK_TLSLDM:
412 case MCSymbolRefExpr::VK_TPOFF:
Colin LeMahieu0e051922016-02-10 18:32:01 +0000413 case MCSymbolRefExpr::VK_TPREL:
Rafael Espindolae98d4832010-11-24 18:51:21 +0000414 case MCSymbolRefExpr::VK_DTPOFF:
Colin LeMahieu0e051922016-02-10 18:32:01 +0000415 case MCSymbolRefExpr::VK_DTPREL:
Ulrich Weigandf11efe72013-07-01 23:33:29 +0000416 case MCSymbolRefExpr::VK_PPC_DTPMOD:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000417 case MCSymbolRefExpr::VK_PPC_TPREL_LO:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000418 case MCSymbolRefExpr::VK_PPC_TPREL_HI:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000419 case MCSymbolRefExpr::VK_PPC_TPREL_HA:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000420 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
421 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
422 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
423 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000424 case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000425 case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000426 case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000427 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
428 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
429 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
430 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
431 case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000432 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000433 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000434 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000435 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
436 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
437 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
438 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
Bill Schmidt441907d2013-02-26 16:41:03 +0000439 case MCSymbolRefExpr::VK_PPC_TLS:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000440 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000441 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000442 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000443 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
Ulrich Weigand52cf8e42013-07-09 16:41:09 +0000444 case MCSymbolRefExpr::VK_PPC_TLSGD:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000445 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000446 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
Ulrich Weigand876a0d02013-06-21 14:44:15 +0000447 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000448 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
Ulrich Weigand52cf8e42013-07-09 16:41:09 +0000449 case MCSymbolRefExpr::VK_PPC_TLSLD:
Rafael Espindolae98d4832010-11-24 18:51:21 +0000450 break;
451 }
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000452 getAssembler().registerSymbol(symRef.getSymbol());
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000453 cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
Rafael Espindola4e70ac72010-11-24 02:19:40 +0000454 break;
455 }
456
457 case MCExpr::Unary:
458 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
459 break;
460 }
461}
462
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000463void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
464 const MCSubtargetInfo &STI) {
465 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000466 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
Benjamin Kramer1f601242010-08-27 10:40:51 +0000467
Rafael Espindola3fa6fc12010-12-02 05:44:06 +0000468 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
469 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
Benjamin Kramer1f601242010-08-27 10:40:51 +0000470}
471
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000472void MCELFStreamer::EmitInstToData(const MCInst &Inst,
473 const MCSubtargetInfo &STI) {
Eli Benderskyf483ff92012-12-20 19:05:53 +0000474 MCAssembler &Assembler = getAssembler();
Benjamin Kramer1f601242010-08-27 10:40:51 +0000475 SmallVector<MCFixup, 4> Fixups;
476 SmallString<256> Code;
477 raw_svector_ostream VecOS(Code);
Jim Grosbach91df21f2015-05-15 19:13:16 +0000478 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
Benjamin Kramer1f601242010-08-27 10:40:51 +0000479
Rafael Espindola4e70ac72010-11-24 02:19:40 +0000480 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
481 fixSymbolsInTLSFixups(Fixups[i].getValue());
482
Eli Benderskyf483ff92012-12-20 19:05:53 +0000483 // There are several possibilities here:
484 //
485 // If bundling is disabled, append the encoded instruction to the current data
486 // fragment (or create a new such fragment if the current fragment is not a
487 // data fragment).
488 //
489 // If bundling is enabled:
Eli Benderskycf6009b2013-01-15 23:22:09 +0000490 // - If we're not in a bundle-locked group, emit the instruction into a
491 // fragment of its own. If there are no fixups registered for the
492 // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
493 // MCDataFragment.
Eli Benderskyf483ff92012-12-20 19:05:53 +0000494 // - If we're in a bundle-locked group, append the instruction to the current
495 // data fragment because we want all the instructions in a group to get into
496 // the same fragment. Be careful not to do that for the first instruction in
497 // the group, though.
498 MCDataFragment *DF;
499
500 if (Assembler.isBundlingEnabled()) {
Rafael Espindola983bec62015-05-27 21:04:14 +0000501 MCSection &Sec = *getCurrentSectionOnly();
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000502 if (Assembler.getRelaxAll() && isBundleLocked())
Petr Hosek9e0c8902015-04-12 23:42:25 +0000503 // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
504 // the current bundle group.
505 DF = BundleGroups.back();
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000506 else if (Assembler.getRelaxAll() && !isBundleLocked())
Petr Hosek9e0c8902015-04-12 23:42:25 +0000507 // When not in a bundle-locked group and the -mc-relax-all flag is used,
508 // we create a new temporary fragment which will be later merged into
509 // the current fragment.
510 DF = new MCDataFragment();
Rafael Espindolab028cc82015-05-25 15:04:26 +0000511 else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst())
Derek Schuff8878bcc92013-02-15 22:50:52 +0000512 // If we are bundle-locked, we re-use the current fragment.
513 // The bundle-locking directive ensures this is a new data fragment.
514 DF = cast<MCDataFragment>(getCurrentFragment());
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000515 else if (!isBundleLocked() && Fixups.size() == 0) {
Eli Benderskycf6009b2013-01-15 23:22:09 +0000516 // Optimize memory usage by emitting the instruction to a
517 // MCCompactEncodedInstFragment when not in a bundle-locked group and
518 // there are no fixups registered.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000519 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
520 insert(CEIF);
Eli Benderskycf6009b2013-01-15 23:22:09 +0000521 CEIF->getContents().append(Code.begin(), Code.end());
522 return;
Derek Schuff8878bcc92013-02-15 22:50:52 +0000523 } else {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000524 DF = new MCDataFragment();
525 insert(DF);
Derek Schuff05fb7352014-10-15 17:10:04 +0000526 }
Rafael Espindolab028cc82015-05-25 15:04:26 +0000527 if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
Derek Schuff05fb7352014-10-15 17:10:04 +0000528 // If this fragment is for a group marked "align_to_end", set a flag
529 // in the fragment. This can happen after the fragment has already been
530 // created if there are nested bundle_align groups and an inner one
531 // is the one marked align_to_end.
532 DF->setAlignToBundleEnd(true);
Eli Bendersky802b6282013-01-07 21:51:08 +0000533 }
Eli Benderskyf483ff92012-12-20 19:05:53 +0000534
535 // We're now emitting an instruction in a bundle group, so this flag has
536 // to be turned off.
Rafael Espindolab028cc82015-05-25 15:04:26 +0000537 Sec.setBundleGroupBeforeFirstInst(false);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000538 } else {
539 DF = getOrCreateDataFragment();
540 }
541
Benjamin Kramer1f601242010-08-27 10:40:51 +0000542 // Add the fixups and data.
543 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
544 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Benderskya31a8942012-12-07 19:13:57 +0000545 DF->getFixups().push_back(Fixups[i]);
Benjamin Kramer1f601242010-08-27 10:40:51 +0000546 }
Eli Benderskyf483ff92012-12-20 19:05:53 +0000547 DF->setHasInstructions(true);
Benjamin Kramer1f601242010-08-27 10:40:51 +0000548 DF->getContents().append(Code.begin(), Code.end());
Petr Hosek9e0c8902015-04-12 23:42:25 +0000549
550 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000551 if (!isBundleLocked()) {
Petr Hosek9e0c8902015-04-12 23:42:25 +0000552 mergeFragment(getOrCreateDataFragment(), DF);
553 delete DF;
554 }
555 }
Benjamin Kramer1f601242010-08-27 10:40:51 +0000556}
557
Eli Benderskyf483ff92012-12-20 19:05:53 +0000558void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
559 assert(AlignPow2 <= 30 && "Invalid bundle alignment");
560 MCAssembler &Assembler = getAssembler();
Derek Schuff05fb7352014-10-15 17:10:04 +0000561 if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
562 Assembler.getBundleAlignSize() == 1U << AlignPow2))
563 Assembler.setBundleAlignSize(1U << AlignPow2);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000564 else
Derek Schuff05fb7352014-10-15 17:10:04 +0000565 report_fatal_error(".bundle_align_mode cannot be changed once set");
Eli Benderskyf483ff92012-12-20 19:05:53 +0000566}
567
Eli Bendersky802b6282013-01-07 21:51:08 +0000568void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
Rafael Espindola983bec62015-05-27 21:04:14 +0000569 MCSection &Sec = *getCurrentSectionOnly();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000570
571 // Sanity checks
572 //
573 if (!getAssembler().isBundlingEnabled())
574 report_fatal_error(".bundle_lock forbidden when bundling is disabled");
Derek Schuff05fb7352014-10-15 17:10:04 +0000575
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000576 if (!isBundleLocked())
Rafael Espindolab028cc82015-05-25 15:04:26 +0000577 Sec.setBundleGroupBeforeFirstInst(true);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000578
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000579 if (getAssembler().getRelaxAll() && !isBundleLocked()) {
Petr Hosek9e0c8902015-04-12 23:42:25 +0000580 // TODO: drop the lock state and set directly in the fragment
581 MCDataFragment *DF = new MCDataFragment();
582 BundleGroups.push_back(DF);
583 }
584
Rafael Espindolab028cc82015-05-25 15:04:26 +0000585 Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
586 : MCSection::BundleLocked);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000587}
588
589void MCELFStreamer::EmitBundleUnlock() {
Rafael Espindola983bec62015-05-27 21:04:14 +0000590 MCSection &Sec = *getCurrentSectionOnly();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000591
592 // Sanity checks
593 if (!getAssembler().isBundlingEnabled())
594 report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000595 else if (!isBundleLocked())
Eli Benderskyf483ff92012-12-20 19:05:53 +0000596 report_fatal_error(".bundle_unlock without matching lock");
Rafael Espindolab028cc82015-05-25 15:04:26 +0000597 else if (Sec.isBundleGroupBeforeFirstInst())
Eli Benderskyf483ff92012-12-20 19:05:53 +0000598 report_fatal_error("Empty bundle-locked group is forbidden");
599
Petr Hosek9e0c8902015-04-12 23:42:25 +0000600 // When the -mc-relax-all flag is used, we emit instructions to fragments
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000601 // stored on a stack. When the bundle unlock is emitted, we pop a fragment
Petr Hosek9e0c8902015-04-12 23:42:25 +0000602 // from the stack a merge it to the one below.
603 if (getAssembler().getRelaxAll()) {
604 assert(!BundleGroups.empty() && "There are no bundle groups");
605 MCDataFragment *DF = BundleGroups.back();
606
607 // FIXME: Use BundleGroups to track the lock state instead.
Rafael Espindolab028cc82015-05-25 15:04:26 +0000608 Sec.setBundleLockState(MCSection::NotBundleLocked);
Petr Hosek9e0c8902015-04-12 23:42:25 +0000609
Rafael Espindolac1cd9442015-05-25 14:57:35 +0000610 // FIXME: Use more separate fragments for nested groups.
611 if (!isBundleLocked()) {
Petr Hosek9e0c8902015-04-12 23:42:25 +0000612 mergeFragment(getOrCreateDataFragment(), DF);
613 BundleGroups.pop_back();
614 delete DF;
615 }
616
Rafael Espindolab028cc82015-05-25 15:04:26 +0000617 if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
Petr Hosek9e0c8902015-04-12 23:42:25 +0000618 getOrCreateDataFragment()->setAlignToBundleEnd(false);
619 } else
Rafael Espindolab028cc82015-05-25 15:04:26 +0000620 Sec.setBundleLockState(MCSection::NotBundleLocked);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000621}
622
Richard Mitton21101b32013-09-19 23:21:01 +0000623void MCELFStreamer::FinishImpl() {
Derek Schuff2a1678a2015-04-21 00:14:25 +0000624 // Ensure the last section gets aligned if necessary.
Rafael Espindola983bec62015-05-27 21:04:14 +0000625 MCSection *CurSection = getCurrentSectionOnly();
Rafael Espindolae6e287d2015-05-26 14:42:52 +0000626 setSectionAlignmentForBundling(getAssembler(), CurSection);
Derek Schuff2a1678a2015-04-21 00:14:25 +0000627
Rafael Espindola7f4ccce2014-05-12 13:30:10 +0000628 EmitFrames(nullptr);
Richard Mitton21101b32013-09-19 23:21:01 +0000629
Rafael Espindola07082092012-01-07 03:13:18 +0000630 this->MCObjectStreamer::FinishImpl();
Matt Fleming6c1ad482010-08-16 18:57:57 +0000631}
Richard Mitton21101b32013-09-19 23:21:01 +0000632
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000633MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000634 raw_pwrite_stream &OS, MCCodeEmitter *CE,
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000635 bool RelaxAll) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000636 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000637 if (RelaxAll)
638 S->getAssembler().setRelaxAll(true);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000639 return S;
640}
Logan Chien59ff0702012-12-07 15:50:40 +0000641
Tim Northover5cc3dc82012-12-07 16:50:23 +0000642void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
643 llvm_unreachable("Generic ELF doesn't support this directive");
644}
645
Logan Chien59ff0702012-12-07 15:50:40 +0000646void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
647 llvm_unreachable("ELF doesn't support this directive");
648}
649
650void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
651 llvm_unreachable("ELF doesn't support this directive");
652}
653
654void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
655 llvm_unreachable("ELF doesn't support this directive");
656}
657
658void MCELFStreamer::EmitCOFFSymbolType(int Type) {
659 llvm_unreachable("ELF doesn't support this directive");
660}
661
662void MCELFStreamer::EndCOFFSymbolDef() {
663 llvm_unreachable("ELF doesn't support this directive");
664}
665
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000666void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
Logan Chien59ff0702012-12-07 15:50:40 +0000667 uint64_t Size, unsigned ByteAlignment) {
668 llvm_unreachable("ELF doesn't support this directive");
669}
670
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000671void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
Logan Chien59ff0702012-12-07 15:50:40 +0000672 uint64_t Size, unsigned ByteAlignment) {
673 llvm_unreachable("ELF doesn't support this directive");
674}