blob: 95b48e6abc740f8022ee96566f84a03a0d789d6d [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
Chandler Carruthe3e43d92017-06-06 11:49:48 +000014#include "llvm/MC/MCELFStreamer.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/SmallVector.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000017#include "llvm/BinaryFormat/ELF.h"
David Peixotto1edc33b2013-11-25 19:11:13 +000018#include "llvm/MC/MCAsmBackend.h"
Rafael Espindola90ce9f72014-10-15 16:12:52 +000019#include "llvm/MC/MCAsmInfo.h"
Chandler Carruth974a4452014-01-07 11:48:04 +000020#include "llvm/MC/MCAssembler.h"
Matt Fleming3565a062010-08-16 18:57:57 +000021#include "llvm/MC/MCCodeEmitter.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000022#include "llvm/MC/MCContext.h"
Matt Fleming3565a062010-08-16 18:57:57 +000023#include "llvm/MC/MCExpr.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000024#include "llvm/MC/MCFixup.h"
25#include "llvm/MC/MCFragment.h"
Rafael Espindolae0fd5672014-01-23 22:49:25 +000026#include "llvm/MC/MCObjectFileInfo.h"
Reid Klecknerd0175612016-06-22 23:25:26 +000027#include "llvm/MC/MCObjectWriter.h"
Matt Fleming3565a062010-08-16 18:57:57 +000028#include "llvm/MC/MCSection.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/MC/MCSectionELF.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000030#include "llvm/MC/MCStreamer.h"
Matt Fleming3565a062010-08-16 18:57:57 +000031#include "llvm/MC/MCSymbol.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000032#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000033#include "llvm/Support/Casting.h"
Matt Fleming3565a062010-08-16 18:57:57 +000034#include "llvm/Support/ErrorHandling.h"
Rafael Espindolac7c4c362015-03-19 01:50:16 +000035#include "llvm/Support/TargetRegistry.h"
Matt Fleming3565a062010-08-16 18:57:57 +000036#include "llvm/Support/raw_ostream.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000037#include <cassert>
38#include <cstdint>
Matt Fleming3565a062010-08-16 18:57:57 +000039
40using namespace llvm;
41
Lang Hames445025a2017-10-11 01:57:21 +000042MCELFStreamer::MCELFStreamer(MCContext &Context,
43 std::unique_ptr<MCAsmBackend> TAB,
Peter Collingbourne17a98142018-05-18 18:26:45 +000044 std::unique_ptr<MCObjectWriter> OW,
Lang Hames806f68b2017-10-11 23:34:47 +000045 std::unique_ptr<MCCodeEmitter> Emitter)
Peter Collingbourne17a98142018-05-18 18:26:45 +000046 : MCObjectStreamer(Context, std::move(TAB), std::move(OW),
47 std::move(Emitter)) {}
Lang Hames445025a2017-10-11 01:57:21 +000048
Rafael Espindola63b75632015-05-25 14:57:35 +000049bool MCELFStreamer::isBundleLocked() const {
Rafael Espindola792e1582015-05-27 21:04:14 +000050 return getCurrentSectionOnly()->isBundleLocked();
Rafael Espindola63b75632015-05-25 14:57:35 +000051}
52
Petr Hosek054db7d2015-04-12 23:42:25 +000053void MCELFStreamer::mergeFragment(MCDataFragment *DF,
Pete Cooperc821cef2015-06-17 22:01:28 +000054 MCDataFragment *EF) {
Petr Hosek054db7d2015-04-12 23:42:25 +000055 MCAssembler &Assembler = getAssembler();
56
57 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
58 uint64_t FSize = EF->getContents().size();
59
60 if (FSize > Assembler.getBundleAlignSize())
61 report_fatal_error("Fragment can't be larger than a bundle size");
62
63 uint64_t RequiredBundlePadding = computeBundlePadding(
64 Assembler, EF, DF->getContents().size(), FSize);
65
66 if (RequiredBundlePadding > UINT8_MAX)
67 report_fatal_error("Padding cannot exceed 255 bytes");
68
69 if (RequiredBundlePadding > 0) {
70 SmallString<256> Code;
71 raw_svector_ostream VecOS(Code);
Peter Collingbourne74bda0b2018-05-21 18:11:35 +000072 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
73 Assembler.writeFragmentPadding(VecOS, *EF, FSize);
Petr Hosek054db7d2015-04-12 23:42:25 +000074
75 DF->getContents().append(Code.begin(), Code.end());
76 }
77 }
78
79 flushPendingLabels(DF, DF->getContents().size());
80
81 for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
82 EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
83 DF->getContents().size());
84 DF->getFixups().push_back(EF->getFixups()[i]);
85 }
Peter Smithe2b2a912018-06-06 09:40:06 +000086 if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
87 DF->setHasInstructions(*EF->getSubtargetInfo());
Petr Hosek054db7d2015-04-12 23:42:25 +000088 DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
89}
90
Rafael Espindola90ce9f72014-10-15 16:12:52 +000091void MCELFStreamer::InitSections(bool NoExecStack) {
Rafael Espindola90ce9f72014-10-15 16:12:52 +000092 MCContext &Ctx = getContext();
93 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
Rafael Espindola506eb6d2014-02-04 18:34:04 +000094 EmitCodeAlignment(4);
Rafael Espindola5950c6f2014-01-24 02:18:40 +000095
Rafael Espindola90ce9f72014-10-15 16:12:52 +000096 if (NoExecStack)
97 SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
Rafael Espindolad80781b2010-09-15 21:48:40 +000098}
99
Rafael Espindola940b0c02017-02-10 15:13:12 +0000100void MCELFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc) {
Rafael Espindola569f3822015-06-02 20:38:46 +0000101 auto *Symbol = cast<MCSymbolELF>(S);
Rafael Espindola940b0c02017-02-10 15:13:12 +0000102 MCObjectStreamer::EmitLabel(Symbol, Loc);
Rafael Espindola73ffea42010-09-25 05:42:19 +0000103
Rafael Espindolae1a25872010-11-11 19:04:55 +0000104 const MCSectionELF &Section =
Rafael Espindola0eba49c2015-10-05 12:07:05 +0000105 static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
Rafael Espindola1c130262011-01-23 04:43:11 +0000106 if (Section.getFlags() & ELF::SHF_TLS)
Rafael Espindola569f3822015-06-02 20:38:46 +0000107 Symbol->setType(ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000108}
109
Weiming Zhaoc4d38012017-04-03 21:50:04 +0000110void MCELFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc, MCFragment *F) {
111 auto *Symbol = cast<MCSymbolELF>(S);
112 MCObjectStreamer::EmitLabel(Symbol, Loc, F);
113
114 const MCSectionELF &Section =
115 static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
116 if (Section.getFlags() & ELF::SHF_TLS)
117 Symbol->setType(ELF::STT_TLS);
118}
119
Matt Fleming3565a062010-08-16 18:57:57 +0000120void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
David Peixotto1edc33b2013-11-25 19:11:13 +0000121 // Let the target do whatever target specific stuff it needs to do.
122 getAssembler().getBackend().handleAssemblerFlag(Flag);
123 // Do any generic stuff we need to do.
Matt Fleming3565a062010-08-16 18:57:57 +0000124 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000125 case MCAF_SyntaxUnified: return; // no-op here.
Evan Chengbd27f5a2011-07-27 00:38:12 +0000126 case MCAF_Code16: return; // Change parsing mode; no-op here.
127 case MCAF_Code32: return; // Change parsing mode; no-op here.
128 case MCAF_Code64: return; // Change parsing mode; no-op here.
Matt Fleming3565a062010-08-16 18:57:57 +0000129 case MCAF_SubsectionsViaSymbols:
130 getAssembler().setSubsectionsViaSymbols(true);
131 return;
132 }
133
Craig Topper85814382012-02-07 05:05:23 +0000134 llvm_unreachable("invalid assembler flag!");
Matt Fleming3565a062010-08-16 18:57:57 +0000135}
136
Benjamin Kramerd3c712e2015-08-08 18:27:36 +0000137// If bundle alignment is used and there are any instructions in the section, it
Derek Schuffa49508c2015-04-21 00:14:25 +0000138// needs to be aligned to at least the bundle size.
Rafael Espindola712c27f2015-05-25 18:34:26 +0000139static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
Rafael Espindolada68b182015-05-26 14:42:52 +0000140 MCSection *Section) {
141 if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
142 Section->getAlignment() < Assembler.getBundleAlignSize())
143 Section->setAlignment(Assembler.getBundleAlignSize());
Derek Schuffa49508c2015-04-21 00:14:25 +0000144}
145
Rafael Espindola75219642015-05-21 19:20:38 +0000146void MCELFStreamer::ChangeSection(MCSection *Section,
Peter Collingbournedf39be62013-04-17 21:18:16 +0000147 const MCExpr *Subsection) {
Rafael Espindola792e1582015-05-27 21:04:14 +0000148 MCSection *CurSection = getCurrentSectionOnly();
Rafael Espindola63b75632015-05-25 14:57:35 +0000149 if (CurSection && isBundleLocked())
Eli Bendersky4766ef42012-12-20 19:05:53 +0000150 report_fatal_error("Unterminated .bundle_lock when changing a section");
Rafael Espindolad8ee23f2014-10-17 01:48:58 +0000151
152 MCAssembler &Asm = getAssembler();
Derek Schuffa49508c2015-04-21 00:14:25 +0000153 // Ensure the previous section gets aligned if necessary.
154 setSectionAlignmentForBundling(Asm, CurSection);
Rafael Espindolad8ee23f2014-10-17 01:48:58 +0000155 auto *SectionELF = static_cast<const MCSectionELF *>(Section);
156 const MCSymbol *Grp = SectionELF->getGroup();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000157 if (Grp)
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000158 Asm.registerSymbol(*Grp);
Rafael Espindolad8ee23f2014-10-17 01:48:58 +0000159
David Blaikie1e54c562017-03-16 00:43:19 +0000160 changeSectionImpl(Section, Subsection);
Rafael Espindola7931a612017-02-02 21:26:06 +0000161 Asm.registerSymbol(*Section->getBeginSymbol());
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000162}
163
Rafael Espindola484291c2010-11-01 14:28:48 +0000164void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000165 getAssembler().registerSymbol(*Symbol);
Jim Grosbach586c0042015-05-30 01:25:56 +0000166 const MCExpr *Value = MCSymbolRefExpr::create(
Rafael Espindola0a70f9b2014-03-20 02:12:01 +0000167 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
Rafael Espindola484291c2010-11-01 14:28:48 +0000168 Alias->setVariableValue(Value);
169}
170
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000171// When GNU as encounters more than one .type declaration for an object it seems
172// to use a mechanism similar to the one below to decide which type is actually
173// used in the object file. The greater of T1 and T2 is selected based on the
174// following ordering:
175// STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
176// If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
177// provided type).
178static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
Benjamin Kramered0266d2015-03-07 17:41:00 +0000179 for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
180 ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
181 if (T1 == Type)
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000182 return T2;
Benjamin Kramered0266d2015-03-07 17:41:00 +0000183 if (T2 == Type)
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000184 return T1;
185 }
186
187 return T2;
188}
189
Rafael Espindola569f3822015-06-02 20:38:46 +0000190bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
191 auto *Symbol = cast<MCSymbolELF>(S);
Matt Fleming3565a062010-08-16 18:57:57 +0000192
193 // Adding a symbol attribute always introduces the symbol, note that an
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000194 // important side effect of calling registerSymbol here is to register
Matt Fleming3565a062010-08-16 18:57:57 +0000195 // the symbol with the assembler.
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000196 getAssembler().registerSymbol(*Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000197
198 // The implementation of symbol attributes is designed to match 'as', but it
199 // leaves much to desired. It doesn't really make sense to arbitrarily add and
200 // remove flags, but 'as' allows this (in particular, see .desc).
201 //
202 // In the future it might be worth trying to make these operations more well
203 // defined.
204 switch (Attribute) {
205 case MCSA_LazyReference:
206 case MCSA_Reference:
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000207 case MCSA_SymbolResolver:
Matt Fleming3565a062010-08-16 18:57:57 +0000208 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000209 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000210 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000211 case MCSA_Invalid:
Matt Fleming3565a062010-08-16 18:57:57 +0000212 case MCSA_IndirectSymbol:
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000213 return false;
Matt Fleming3565a062010-08-16 18:57:57 +0000214
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000215 case MCSA_NoDeadStrip:
Rafael Espindola7e528a12010-11-14 01:34:31 +0000216 // Ignore for now.
217 break;
218
Rafael Espindolaeb3eb882015-01-23 04:44:35 +0000219 case MCSA_ELF_TypeGnuUniqueObject:
Rafael Espindola569f3822015-06-02 20:38:46 +0000220 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
221 Symbol->setBinding(ELF::STB_GNU_UNIQUE);
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000222 Symbol->setExternal(true);
Rafael Espindolaeb3eb882015-01-23 04:44:35 +0000223 break;
224
Matt Fleming3565a062010-08-16 18:57:57 +0000225 case MCSA_Global:
Rafael Espindola569f3822015-06-02 20:38:46 +0000226 Symbol->setBinding(ELF::STB_GLOBAL);
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000227 Symbol->setExternal(true);
Matt Fleming3565a062010-08-16 18:57:57 +0000228 break;
229
Benjamin Kramer230c2742010-09-02 17:18:32 +0000230 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000231 case MCSA_Weak:
Rafael Espindola569f3822015-06-02 20:38:46 +0000232 Symbol->setBinding(ELF::STB_WEAK);
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000233 Symbol->setExternal(true);
Matt Fleming3565a062010-08-16 18:57:57 +0000234 break;
235
236 case MCSA_Local:
Rafael Espindola569f3822015-06-02 20:38:46 +0000237 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000238 Symbol->setExternal(false);
Matt Fleming3565a062010-08-16 18:57:57 +0000239 break;
240
241 case MCSA_ELF_TypeFunction:
Rafael Espindola569f3822015-06-02 20:38:46 +0000242 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
Matt Fleming3565a062010-08-16 18:57:57 +0000243 break;
244
Roman Divackya0c17a42011-12-12 17:34:04 +0000245 case MCSA_ELF_TypeIndFunction:
Rafael Espindola569f3822015-06-02 20:38:46 +0000246 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
Roman Divackya0c17a42011-12-12 17:34:04 +0000247 break;
248
Matt Fleming3565a062010-08-16 18:57:57 +0000249 case MCSA_ELF_TypeObject:
Rafael Espindola569f3822015-06-02 20:38:46 +0000250 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
Matt Fleming3565a062010-08-16 18:57:57 +0000251 break;
252
253 case MCSA_ELF_TypeTLS:
Rafael Espindola569f3822015-06-02 20:38:46 +0000254 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
Matt Fleming3565a062010-08-16 18:57:57 +0000255 break;
256
257 case MCSA_ELF_TypeCommon:
Peter Collingbourne01a7b5c2013-04-10 16:52:15 +0000258 // TODO: Emit these as a common symbol.
Rafael Espindola569f3822015-06-02 20:38:46 +0000259 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
Matt Fleming3565a062010-08-16 18:57:57 +0000260 break;
261
262 case MCSA_ELF_TypeNoType:
Rafael Espindola569f3822015-06-02 20:38:46 +0000263 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
Matt Fleming3565a062010-08-16 18:57:57 +0000264 break;
265
266 case MCSA_Protected:
Rafael Espindola569f3822015-06-02 20:38:46 +0000267 Symbol->setVisibility(ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000268 break;
269
270 case MCSA_Hidden:
Rafael Espindola569f3822015-06-02 20:38:46 +0000271 Symbol->setVisibility(ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000272 break;
273
274 case MCSA_Internal:
Rafael Espindola569f3822015-06-02 20:38:46 +0000275 Symbol->setVisibility(ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000276 break;
Lang Hames875b7562016-03-15 01:43:05 +0000277
278 case MCSA_AltEntry:
Lang Hames90ae2142016-04-11 18:33:45 +0000279 llvm_unreachable("ELF doesn't support the .alt_entry attribute");
Matt Fleming3565a062010-08-16 18:57:57 +0000280 }
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000281
282 return true;
Matt Fleming3565a062010-08-16 18:57:57 +0000283}
284
Rafael Espindola569f3822015-06-02 20:38:46 +0000285void MCELFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000286 unsigned ByteAlignment) {
Rafael Espindola569f3822015-06-02 20:38:46 +0000287 auto *Symbol = cast<MCSymbolELF>(S);
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000288 getAssembler().registerSymbol(*Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000289
Rafael Espindola96fdaa42015-06-03 21:18:03 +0000290 if (!Symbol->isBindingSet()) {
Rafael Espindola569f3822015-06-02 20:38:46 +0000291 Symbol->setBinding(ELF::STB_GLOBAL);
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000292 Symbol->setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000293 }
294
Rafael Espindola569f3822015-06-02 20:38:46 +0000295 Symbol->setType(ELF::STT_OBJECT);
Rafael Espindola55d02f32010-11-14 21:11:16 +0000296
Rafael Espindolac2128562015-06-04 00:47:43 +0000297 if (Symbol->getBinding() == ELF::STB_LOCAL) {
Rafael Espindolaca792d82015-11-03 18:50:51 +0000298 MCSection &Section = *getAssembler().getContext().getELFSection(
299 ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
300 MCSectionSubPair P = getCurrentSection();
301 SwitchSection(&Section);
302
303 EmitValueToAlignment(ByteAlignment, 0, 1, 0);
304 EmitLabel(Symbol);
305 EmitZeros(Size);
306
307 // Update the maximum alignment of the section if necessary.
308 if (ByteAlignment > Section.getAlignment())
309 Section.setAlignment(ByteAlignment);
310
311 SwitchSection(P.first, P.second);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000312 } else {
Colin LeMahieu2ca8f0f2015-06-06 20:12:40 +0000313 if(Symbol->declareCommon(Size, ByteAlignment))
314 report_fatal_error("Symbol: " + Symbol->getName() +
315 " redeclared as different type");
Matt Fleming3565a062010-08-16 18:57:57 +0000316 }
317
Rafael Espindolaf7e06852015-06-02 00:25:12 +0000318 cast<MCSymbolELF>(Symbol)
319 ->setSize(MCConstantExpr::create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000320}
321
Dan Gohman14b34de2016-12-01 23:39:08 +0000322void MCELFStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
323 cast<MCSymbolELF>(Symbol)->setSize(Value);
Logan Chien64501652012-12-07 15:50:40 +0000324}
325
Rafael Espindola6f0333a2018-03-09 18:42:25 +0000326void MCELFStreamer::emitELFSymverDirective(StringRef AliasName,
Rafael Espindola9e6c29a2018-03-09 03:13:37 +0000327 const MCSymbol *Aliasee) {
Rafael Espindola6f0333a2018-03-09 18:42:25 +0000328 getAssembler().Symvers.push_back({AliasName, Aliasee});
Rafael Espindola9e6c29a2018-03-09 03:13:37 +0000329}
330
Rafael Espindola569f3822015-06-02 20:38:46 +0000331void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
Benjamin Kramer36a16012011-09-01 23:04:27 +0000332 unsigned ByteAlignment) {
Rafael Espindola569f3822015-06-02 20:38:46 +0000333 auto *Symbol = cast<MCSymbolELF>(S);
Jason W Kimf13743b2010-12-16 03:12:17 +0000334 // FIXME: Should this be caught and done earlier?
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000335 getAssembler().registerSymbol(*Symbol);
Rafael Espindola569f3822015-06-02 20:38:46 +0000336 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000337 Symbol->setExternal(false);
Benjamin Kramer36a16012011-09-01 23:04:27 +0000338 EmitCommonSymbol(Symbol, Size, ByteAlignment);
Jason W Kimf13743b2010-12-16 03:12:17 +0000339}
340
Kevin Enderby29c96f12014-04-22 17:27:29 +0000341void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Craig Topper634763c2015-09-20 23:35:59 +0000342 SMLoc Loc) {
Rafael Espindola63b75632015-05-25 14:57:35 +0000343 if (isBundleLocked())
Eli Bendersky4766ef42012-12-20 19:05:53 +0000344 report_fatal_error("Emitting values inside a locked bundle is forbidden");
David Meyer5f769262012-02-15 15:09:06 +0000345 fixSymbolsInTLSFixups(Value);
Kevin Enderby29c96f12014-04-22 17:27:29 +0000346 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
David Meyer5f769262012-02-15 15:09:06 +0000347}
348
Eli Bendersky4766ef42012-12-20 19:05:53 +0000349void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
350 int64_t Value,
351 unsigned ValueSize,
352 unsigned MaxBytesToEmit) {
Rafael Espindola63b75632015-05-25 14:57:35 +0000353 if (isBundleLocked())
Eli Bendersky4766ef42012-12-20 19:05:53 +0000354 report_fatal_error("Emitting values inside a locked bundle is forbidden");
355 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
356 ValueSize, MaxBytesToEmit);
357}
358
Michael J. Spencer56500c72018-06-02 16:33:01 +0000359void MCELFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From,
360 const MCSymbolRefExpr *To,
361 uint64_t Count) {
362 getAssembler().CGProfile.push_back({From, To, Count});
363}
364
Rafael Espindolac7ce3e42013-10-16 01:05:45 +0000365void MCELFStreamer::EmitIdent(StringRef IdentString) {
Rafael Espindola75219642015-05-21 19:20:38 +0000366 MCSection *Comment = getAssembler().getContext().getELFSection(
Rafael Espindola9936b802015-01-29 17:33:21 +0000367 ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
Rafael Espindolac7ce3e42013-10-16 01:05:45 +0000368 PushSection();
369 SwitchSection(Comment);
370 if (!SeenIdent) {
371 EmitIntValue(0, 1);
372 SeenIdent = true;
373 }
374 EmitBytes(IdentString);
375 EmitIntValue(0, 1);
376 PopSection();
377}
378
379void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
Rafael Espindola97551272010-11-24 02:19:40 +0000380 switch (expr->getKind()) {
Tim Northover72062f52013-01-31 12:12:40 +0000381 case MCExpr::Target:
382 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
383 break;
Rafael Espindola97551272010-11-24 02:19:40 +0000384 case MCExpr::Constant:
385 break;
386
387 case MCExpr::Binary: {
388 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
389 fixSymbolsInTLSFixups(be->getLHS());
390 fixSymbolsInTLSFixups(be->getRHS());
391 break;
392 }
393
394 case MCExpr::SymbolRef: {
395 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000396 switch (symRef.getKind()) {
397 default:
Rafael Espindola97551272010-11-24 02:19:40 +0000398 return;
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000399 case MCSymbolRefExpr::VK_GOTTPOFF:
400 case MCSymbolRefExpr::VK_INDNTPOFF:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000401 case MCSymbolRefExpr::VK_NTPOFF:
402 case MCSymbolRefExpr::VK_GOTNTPOFF:
403 case MCSymbolRefExpr::VK_TLSGD:
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000404 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000405 case MCSymbolRefExpr::VK_TLSLDM:
406 case MCSymbolRefExpr::VK_TPOFF:
Colin LeMahieu89dcdff2016-02-10 18:32:01 +0000407 case MCSymbolRefExpr::VK_TPREL:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000408 case MCSymbolRefExpr::VK_DTPOFF:
Colin LeMahieu89dcdff2016-02-10 18:32:01 +0000409 case MCSymbolRefExpr::VK_DTPREL:
Ulrich Weigand228e0af2013-07-01 23:33:29 +0000410 case MCSymbolRefExpr::VK_PPC_DTPMOD:
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000411 case MCSymbolRefExpr::VK_PPC_TPREL_LO:
Ulrich Weigandcab0a192013-06-21 14:44:15 +0000412 case MCSymbolRefExpr::VK_PPC_TPREL_HI:
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000413 case MCSymbolRefExpr::VK_PPC_TPREL_HA:
Sean Fertilea2448532018-06-15 19:47:16 +0000414 case MCSymbolRefExpr::VK_PPC_TPREL_HIGH:
415 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHA:
Ulrich Weigandcab0a192013-06-21 14:44:15 +0000416 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
417 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
418 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
419 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000420 case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
Ulrich Weigandcab0a192013-06-21 14:44:15 +0000421 case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000422 case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
Sean Fertilea2448532018-06-15 19:47:16 +0000423 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGH:
424 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA:
Ulrich Weigandcab0a192013-06-21 14:44:15 +0000425 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
426 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
427 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
428 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
429 case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000430 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
Ulrich Weigandcab0a192013-06-21 14:44:15 +0000431 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000432 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
Ulrich Weigandcab0a192013-06-21 14:44:15 +0000433 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
434 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
435 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
436 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
Bill Schmidt3a429892013-02-26 16:41:03 +0000437 case MCSymbolRefExpr::VK_PPC_TLS:
Ulrich Weigandcab0a192013-06-21 14:44:15 +0000438 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000439 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
Ulrich Weigandcab0a192013-06-21 14:44:15 +0000440 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000441 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
Ulrich Weigand7a345992013-07-09 16:41:09 +0000442 case MCSymbolRefExpr::VK_PPC_TLSGD:
Ulrich Weigandcab0a192013-06-21 14:44:15 +0000443 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000444 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
Ulrich Weigandcab0a192013-06-21 14:44:15 +0000445 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000446 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
Ulrich Weigand7a345992013-07-09 16:41:09 +0000447 case MCSymbolRefExpr::VK_PPC_TLSLD:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000448 break;
449 }
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000450 getAssembler().registerSymbol(symRef.getSymbol());
Rafael Espindola569f3822015-06-02 20:38:46 +0000451 cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
Rafael Espindola97551272010-11-24 02:19:40 +0000452 break;
453 }
454
455 case MCExpr::Unary:
456 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
457 break;
458 }
459}
460
Michael J. Spencer56500c72018-06-02 16:33:01 +0000461void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
462 const MCSymbol *S = &SRE->getSymbol();
463 if (S->isTemporary()) {
464 if (!S->isInSection()) {
465 getContext().reportError(
466 SRE->getLoc(), Twine("Reference to undefined temporary symbol ") +
467 "`" + S->getName() + "`");
468 return;
469 }
470 S = S->getSection().getBeginSymbol();
471 S->setUsedInReloc();
472 SRE =
473 MCSymbolRefExpr::create(S, SRE->getKind(), getContext(), SRE->getLoc());
474 return;
475 }
476 // Not a temporary, referece it as a weak undefined.
477 bool Created;
478 getAssembler().registerSymbol(*S, &Created);
479 if (Created) {
480 cast<MCSymbolELF>(S)->setBinding(ELF::STB_WEAK);
481 cast<MCSymbolELF>(S)->setExternal(true);
482 }
483}
484
485void MCELFStreamer::finalizeCGProfile() {
486 for (MCAssembler::CGProfileEntry &E : getAssembler().CGProfile) {
487 finalizeCGProfileEntry(E.From);
488 finalizeCGProfileEntry(E.To);
489 }
490}
491
David Woodhoused5d381b2014-01-28 23:12:49 +0000492void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
493 const MCSubtargetInfo &STI) {
494 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
Eli Bendersky251040b2013-01-08 00:22:56 +0000495 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000496
Rafael Espindoladedb0452010-12-02 05:44:06 +0000497 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
498 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000499}
500
Peter Smithe2b2a912018-06-06 09:40:06 +0000501// A fragment can only have one Subtarget, and when bundling is enabled we
502// sometimes need to use the same fragment. We give an error if there
503// are conflicting Subtargets.
504static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI,
505 const MCSubtargetInfo *NewSTI) {
506 if (OldSTI && NewSTI && OldSTI != NewSTI)
507 report_fatal_error("A Bundle can only have one Subtarget.");
508}
509
David Woodhoused5d381b2014-01-28 23:12:49 +0000510void MCELFStreamer::EmitInstToData(const MCInst &Inst,
511 const MCSubtargetInfo &STI) {
Eli Bendersky4766ef42012-12-20 19:05:53 +0000512 MCAssembler &Assembler = getAssembler();
Benjamin Kramer93ded732010-08-27 10:40:51 +0000513 SmallVector<MCFixup, 4> Fixups;
514 SmallString<256> Code;
515 raw_svector_ostream VecOS(Code);
Jim Grosbach251a66e2015-05-15 19:13:16 +0000516 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
Benjamin Kramer93ded732010-08-27 10:40:51 +0000517
Rafael Espindola97551272010-11-24 02:19:40 +0000518 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
519 fixSymbolsInTLSFixups(Fixups[i].getValue());
520
Eli Bendersky4766ef42012-12-20 19:05:53 +0000521 // There are several possibilities here:
522 //
523 // If bundling is disabled, append the encoded instruction to the current data
524 // fragment (or create a new such fragment if the current fragment is not a
Peter Smithe2b2a912018-06-06 09:40:06 +0000525 // data fragment, or the Subtarget has changed).
Eli Bendersky4766ef42012-12-20 19:05:53 +0000526 //
527 // If bundling is enabled:
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000528 // - If we're not in a bundle-locked group, emit the instruction into a
529 // fragment of its own. If there are no fixups registered for the
530 // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
531 // MCDataFragment.
Eli Bendersky4766ef42012-12-20 19:05:53 +0000532 // - If we're in a bundle-locked group, append the instruction to the current
533 // data fragment because we want all the instructions in a group to get into
534 // the same fragment. Be careful not to do that for the first instruction in
535 // the group, though.
536 MCDataFragment *DF;
537
538 if (Assembler.isBundlingEnabled()) {
Rafael Espindola792e1582015-05-27 21:04:14 +0000539 MCSection &Sec = *getCurrentSectionOnly();
Peter Smithe2b2a912018-06-06 09:40:06 +0000540 if (Assembler.getRelaxAll() && isBundleLocked()) {
Petr Hosek054db7d2015-04-12 23:42:25 +0000541 // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
542 // the current bundle group.
543 DF = BundleGroups.back();
Peter Smithe2b2a912018-06-06 09:40:06 +0000544 CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
545 }
Rafael Espindola63b75632015-05-25 14:57:35 +0000546 else if (Assembler.getRelaxAll() && !isBundleLocked())
Petr Hosek054db7d2015-04-12 23:42:25 +0000547 // When not in a bundle-locked group and the -mc-relax-all flag is used,
548 // we create a new temporary fragment which will be later merged into
549 // the current fragment.
550 DF = new MCDataFragment();
Peter Smithe2b2a912018-06-06 09:40:06 +0000551 else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst()) {
Derek Schuff67144e32013-02-15 22:50:52 +0000552 // If we are bundle-locked, we re-use the current fragment.
553 // The bundle-locking directive ensures this is a new data fragment.
554 DF = cast<MCDataFragment>(getCurrentFragment());
Peter Smithe2b2a912018-06-06 09:40:06 +0000555 CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
556 }
Rafael Espindola63b75632015-05-25 14:57:35 +0000557 else if (!isBundleLocked() && Fixups.size() == 0) {
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000558 // Optimize memory usage by emitting the instruction to a
559 // MCCompactEncodedInstFragment when not in a bundle-locked group and
560 // there are no fixups registered.
Peter Collingbournedf39be62013-04-17 21:18:16 +0000561 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
562 insert(CEIF);
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000563 CEIF->getContents().append(Code.begin(), Code.end());
Peter Smith565bf542018-06-15 09:48:18 +0000564 CEIF->setHasInstructions(STI);
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000565 return;
Derek Schuff67144e32013-02-15 22:50:52 +0000566 } else {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000567 DF = new MCDataFragment();
568 insert(DF);
Derek Schuff279b5502014-10-15 17:10:04 +0000569 }
Rafael Espindola68c5b832015-05-25 15:04:26 +0000570 if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
Derek Schuff279b5502014-10-15 17:10:04 +0000571 // If this fragment is for a group marked "align_to_end", set a flag
572 // in the fragment. This can happen after the fragment has already been
573 // created if there are nested bundle_align groups and an inner one
574 // is the one marked align_to_end.
575 DF->setAlignToBundleEnd(true);
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000576 }
Eli Bendersky4766ef42012-12-20 19:05:53 +0000577
578 // We're now emitting an instruction in a bundle group, so this flag has
579 // to be turned off.
Rafael Espindola68c5b832015-05-25 15:04:26 +0000580 Sec.setBundleGroupBeforeFirstInst(false);
Eli Bendersky4766ef42012-12-20 19:05:53 +0000581 } else {
Peter Smithe2b2a912018-06-06 09:40:06 +0000582 DF = getOrCreateDataFragment(&STI);
Eli Bendersky4766ef42012-12-20 19:05:53 +0000583 }
584
Benjamin Kramer93ded732010-08-27 10:40:51 +0000585 // Add the fixups and data.
586 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
587 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000588 DF->getFixups().push_back(Fixups[i]);
Benjamin Kramer93ded732010-08-27 10:40:51 +0000589 }
Peter Smithe2b2a912018-06-06 09:40:06 +0000590 DF->setHasInstructions(STI);
Benjamin Kramer93ded732010-08-27 10:40:51 +0000591 DF->getContents().append(Code.begin(), Code.end());
Petr Hosek054db7d2015-04-12 23:42:25 +0000592
593 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
Rafael Espindola63b75632015-05-25 14:57:35 +0000594 if (!isBundleLocked()) {
Peter Smithe2b2a912018-06-06 09:40:06 +0000595 mergeFragment(getOrCreateDataFragment(&STI), DF);
Petr Hosek054db7d2015-04-12 23:42:25 +0000596 delete DF;
597 }
598 }
Benjamin Kramer93ded732010-08-27 10:40:51 +0000599}
600
Eli Bendersky4766ef42012-12-20 19:05:53 +0000601void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
602 assert(AlignPow2 <= 30 && "Invalid bundle alignment");
603 MCAssembler &Assembler = getAssembler();
Derek Schuff279b5502014-10-15 17:10:04 +0000604 if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
605 Assembler.getBundleAlignSize() == 1U << AlignPow2))
606 Assembler.setBundleAlignSize(1U << AlignPow2);
Eli Bendersky4766ef42012-12-20 19:05:53 +0000607 else
Derek Schuff279b5502014-10-15 17:10:04 +0000608 report_fatal_error(".bundle_align_mode cannot be changed once set");
Eli Bendersky4766ef42012-12-20 19:05:53 +0000609}
610
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000611void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
Rafael Espindola792e1582015-05-27 21:04:14 +0000612 MCSection &Sec = *getCurrentSectionOnly();
Eli Bendersky4766ef42012-12-20 19:05:53 +0000613
614 // Sanity checks
615 //
616 if (!getAssembler().isBundlingEnabled())
617 report_fatal_error(".bundle_lock forbidden when bundling is disabled");
Derek Schuff279b5502014-10-15 17:10:04 +0000618
Rafael Espindola63b75632015-05-25 14:57:35 +0000619 if (!isBundleLocked())
Rafael Espindola68c5b832015-05-25 15:04:26 +0000620 Sec.setBundleGroupBeforeFirstInst(true);
Eli Bendersky4766ef42012-12-20 19:05:53 +0000621
Rafael Espindola63b75632015-05-25 14:57:35 +0000622 if (getAssembler().getRelaxAll() && !isBundleLocked()) {
Petr Hosek054db7d2015-04-12 23:42:25 +0000623 // TODO: drop the lock state and set directly in the fragment
624 MCDataFragment *DF = new MCDataFragment();
625 BundleGroups.push_back(DF);
626 }
627
Rafael Espindola68c5b832015-05-25 15:04:26 +0000628 Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
629 : MCSection::BundleLocked);
Eli Bendersky4766ef42012-12-20 19:05:53 +0000630}
631
632void MCELFStreamer::EmitBundleUnlock() {
Rafael Espindola792e1582015-05-27 21:04:14 +0000633 MCSection &Sec = *getCurrentSectionOnly();
Eli Bendersky4766ef42012-12-20 19:05:53 +0000634
635 // Sanity checks
636 if (!getAssembler().isBundlingEnabled())
637 report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
Rafael Espindola63b75632015-05-25 14:57:35 +0000638 else if (!isBundleLocked())
Eli Bendersky4766ef42012-12-20 19:05:53 +0000639 report_fatal_error(".bundle_unlock without matching lock");
Rafael Espindola68c5b832015-05-25 15:04:26 +0000640 else if (Sec.isBundleGroupBeforeFirstInst())
Eli Bendersky4766ef42012-12-20 19:05:53 +0000641 report_fatal_error("Empty bundle-locked group is forbidden");
642
Petr Hosek054db7d2015-04-12 23:42:25 +0000643 // When the -mc-relax-all flag is used, we emit instructions to fragments
Benjamin Kramerd3c712e2015-08-08 18:27:36 +0000644 // stored on a stack. When the bundle unlock is emitted, we pop a fragment
Petr Hosek054db7d2015-04-12 23:42:25 +0000645 // from the stack a merge it to the one below.
646 if (getAssembler().getRelaxAll()) {
647 assert(!BundleGroups.empty() && "There are no bundle groups");
648 MCDataFragment *DF = BundleGroups.back();
649
650 // FIXME: Use BundleGroups to track the lock state instead.
Rafael Espindola68c5b832015-05-25 15:04:26 +0000651 Sec.setBundleLockState(MCSection::NotBundleLocked);
Petr Hosek054db7d2015-04-12 23:42:25 +0000652
Rafael Espindola63b75632015-05-25 14:57:35 +0000653 // FIXME: Use more separate fragments for nested groups.
654 if (!isBundleLocked()) {
Peter Smithe2b2a912018-06-06 09:40:06 +0000655 mergeFragment(getOrCreateDataFragment(DF->getSubtargetInfo()), DF);
Petr Hosek054db7d2015-04-12 23:42:25 +0000656 BundleGroups.pop_back();
657 delete DF;
658 }
659
Rafael Espindola68c5b832015-05-25 15:04:26 +0000660 if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
Petr Hosek054db7d2015-04-12 23:42:25 +0000661 getOrCreateDataFragment()->setAlignToBundleEnd(false);
662 } else
Rafael Espindola68c5b832015-05-25 15:04:26 +0000663 Sec.setBundleLockState(MCSection::NotBundleLocked);
Eli Bendersky4766ef42012-12-20 19:05:53 +0000664}
665
Richard Mitton5cc319a2013-09-19 23:21:01 +0000666void MCELFStreamer::FinishImpl() {
Derek Schuffa49508c2015-04-21 00:14:25 +0000667 // Ensure the last section gets aligned if necessary.
Rafael Espindola792e1582015-05-27 21:04:14 +0000668 MCSection *CurSection = getCurrentSectionOnly();
Rafael Espindolada68b182015-05-26 14:42:52 +0000669 setSectionAlignmentForBundling(getAssembler(), CurSection);
Derek Schuffa49508c2015-04-21 00:14:25 +0000670
Michael J. Spencer56500c72018-06-02 16:33:01 +0000671 finalizeCGProfile();
Rafael Espindola044302d2014-05-12 13:30:10 +0000672 EmitFrames(nullptr);
Richard Mitton5cc319a2013-09-19 23:21:01 +0000673
Rafael Espindola99b42372012-01-07 03:13:18 +0000674 this->MCObjectStreamer::FinishImpl();
Matt Fleming3565a062010-08-16 18:57:57 +0000675}
Richard Mitton5cc319a2013-09-19 23:21:01 +0000676
Tim Northover6eb3e872012-12-07 16:50:23 +0000677void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
678 llvm_unreachable("Generic ELF doesn't support this directive");
679}
680
Logan Chien64501652012-12-07 15:50:40 +0000681void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
682 llvm_unreachable("ELF doesn't support this directive");
683}
684
Rafael Espindola75219642015-05-21 19:20:38 +0000685void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
Francis Visoiu Mistriha5d70d82018-07-02 17:29:43 +0000686 uint64_t Size, unsigned ByteAlignment,
687 SMLoc Loc) {
Logan Chien64501652012-12-07 15:50:40 +0000688 llvm_unreachable("ELF doesn't support this directive");
689}
690
Rafael Espindola75219642015-05-21 19:20:38 +0000691void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
Logan Chien64501652012-12-07 15:50:40 +0000692 uint64_t Size, unsigned ByteAlignment) {
693 llvm_unreachable("ELF doesn't support this directive");
694}
Eugene Zelenkof31871c2017-02-07 23:02:00 +0000695
Lang Hames445025a2017-10-11 01:57:21 +0000696MCStreamer *llvm::createELFStreamer(MCContext &Context,
697 std::unique_ptr<MCAsmBackend> &&MAB,
Peter Collingbourne17a98142018-05-18 18:26:45 +0000698 std::unique_ptr<MCObjectWriter> &&OW,
Lang Hames806f68b2017-10-11 23:34:47 +0000699 std::unique_ptr<MCCodeEmitter> &&CE,
Eugene Zelenkof31871c2017-02-07 23:02:00 +0000700 bool RelaxAll) {
Lang Hames806f68b2017-10-11 23:34:47 +0000701 MCELFStreamer *S =
Peter Collingbourne17a98142018-05-18 18:26:45 +0000702 new MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
Eugene Zelenkof31871c2017-02-07 23:02:00 +0000703 if (RelaxAll)
704 S->getAssembler().setRelaxAll(true);
705 return S;
706}