1 //===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file assembles .s files and emits ELF .o object files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/MC/MCELFStreamer.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCObjectStreamer.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCSectionELF.h"
29 #include "llvm/MC/MCSymbolELF.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCValue.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ELF.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/raw_ostream.h"
37
38 using namespace llvm;
39
isBundleLocked() const40 bool MCELFStreamer::isBundleLocked() const {
41 return getCurrentSectionOnly()->isBundleLocked();
42 }
43
~MCELFStreamer()44 MCELFStreamer::~MCELFStreamer() {
45 }
46
mergeFragment(MCDataFragment * DF,MCDataFragment * EF)47 void MCELFStreamer::mergeFragment(MCDataFragment *DF,
48 MCDataFragment *EF) {
49 MCAssembler &Assembler = getAssembler();
50
51 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
52 uint64_t FSize = EF->getContents().size();
53
54 if (FSize > Assembler.getBundleAlignSize())
55 report_fatal_error("Fragment can't be larger than a bundle size");
56
57 uint64_t RequiredBundlePadding = computeBundlePadding(
58 Assembler, EF, DF->getContents().size(), FSize);
59
60 if (RequiredBundlePadding > UINT8_MAX)
61 report_fatal_error("Padding cannot exceed 255 bytes");
62
63 if (RequiredBundlePadding > 0) {
64 SmallString<256> Code;
65 raw_svector_ostream VecOS(Code);
66 MCObjectWriter *OW = Assembler.getBackend().createObjectWriter(VecOS);
67
68 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
69
70 Assembler.writeFragmentPadding(*EF, FSize, OW);
71 delete OW;
72
73 DF->getContents().append(Code.begin(), Code.end());
74 }
75 }
76
77 flushPendingLabels(DF, DF->getContents().size());
78
79 for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
80 EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
81 DF->getContents().size());
82 DF->getFixups().push_back(EF->getFixups()[i]);
83 }
84 DF->setHasInstructions(true);
85 DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
86 }
87
InitSections(bool NoExecStack)88 void MCELFStreamer::InitSections(bool NoExecStack) {
89 MCContext &Ctx = getContext();
90 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
91 EmitCodeAlignment(4);
92
93 if (NoExecStack)
94 SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
95 }
96
EmitLabel(MCSymbol * S)97 void MCELFStreamer::EmitLabel(MCSymbol *S) {
98 auto *Symbol = cast<MCSymbolELF>(S);
99 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
100
101 MCObjectStreamer::EmitLabel(Symbol);
102
103 const MCSectionELF &Section =
104 static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
105 if (Section.getFlags() & ELF::SHF_TLS)
106 Symbol->setType(ELF::STT_TLS);
107 }
108
EmitAssemblerFlag(MCAssemblerFlag Flag)109 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
110 // Let the target do whatever target specific stuff it needs to do.
111 getAssembler().getBackend().handleAssemblerFlag(Flag);
112 // Do any generic stuff we need to do.
113 switch (Flag) {
114 case MCAF_SyntaxUnified: return; // no-op here.
115 case MCAF_Code16: return; // Change parsing mode; no-op here.
116 case MCAF_Code32: return; // Change parsing mode; no-op here.
117 case MCAF_Code64: return; // Change parsing mode; no-op here.
118 case MCAF_SubsectionsViaSymbols:
119 getAssembler().setSubsectionsViaSymbols(true);
120 return;
121 }
122
123 llvm_unreachable("invalid assembler flag!");
124 }
125
126 // If bundle alignment is used and there are any instructions in the section, it
127 // needs to be aligned to at least the bundle size.
setSectionAlignmentForBundling(const MCAssembler & Assembler,MCSection * Section)128 static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
129 MCSection *Section) {
130 if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
131 Section->getAlignment() < Assembler.getBundleAlignSize())
132 Section->setAlignment(Assembler.getBundleAlignSize());
133 }
134
ChangeSection(MCSection * Section,const MCExpr * Subsection)135 void MCELFStreamer::ChangeSection(MCSection *Section,
136 const MCExpr *Subsection) {
137 MCSection *CurSection = getCurrentSectionOnly();
138 if (CurSection && isBundleLocked())
139 report_fatal_error("Unterminated .bundle_lock when changing a section");
140
141 MCAssembler &Asm = getAssembler();
142 // Ensure the previous section gets aligned if necessary.
143 setSectionAlignmentForBundling(Asm, CurSection);
144 auto *SectionELF = static_cast<const MCSectionELF *>(Section);
145 const MCSymbol *Grp = SectionELF->getGroup();
146 if (Grp)
147 Asm.registerSymbol(*Grp);
148
149 this->MCObjectStreamer::ChangeSection(Section, Subsection);
150 MCContext &Ctx = getContext();
151 auto *Begin = cast_or_null<MCSymbolELF>(Section->getBeginSymbol());
152 if (!Begin) {
153 Begin = Ctx.getOrCreateSectionSymbol(*SectionELF);
154 Section->setBeginSymbol(Begin);
155 }
156 if (Begin->isUndefined()) {
157 Asm.registerSymbol(*Begin);
158 Begin->setType(ELF::STT_SECTION);
159 }
160 }
161
EmitWeakReference(MCSymbol * Alias,const MCSymbol * Symbol)162 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
163 getAssembler().registerSymbol(*Symbol);
164 const MCExpr *Value = MCSymbolRefExpr::create(
165 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
166 Alias->setVariableValue(Value);
167 }
168
169 // When GNU as encounters more than one .type declaration for an object it seems
170 // to use a mechanism similar to the one below to decide which type is actually
171 // used in the object file. The greater of T1 and T2 is selected based on the
172 // following ordering:
173 // STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
174 // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
175 // provided type).
CombineSymbolTypes(unsigned T1,unsigned T2)176 static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
177 for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
178 ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
179 if (T1 == Type)
180 return T2;
181 if (T2 == Type)
182 return T1;
183 }
184
185 return T2;
186 }
187
EmitSymbolAttribute(MCSymbol * S,MCSymbolAttr Attribute)188 bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
189 auto *Symbol = cast<MCSymbolELF>(S);
190 // Indirect symbols are handled differently, to match how 'as' handles
191 // them. This makes writing matching .o files easier.
192 if (Attribute == MCSA_IndirectSymbol) {
193 // Note that we intentionally cannot use the symbol data here; this is
194 // important for matching the string table that 'as' generates.
195 IndirectSymbolData ISD;
196 ISD.Symbol = Symbol;
197 ISD.Section = getCurrentSectionOnly();
198 getAssembler().getIndirectSymbols().push_back(ISD);
199 return true;
200 }
201
202 // Adding a symbol attribute always introduces the symbol, note that an
203 // important side effect of calling registerSymbol here is to register
204 // the symbol with the assembler.
205 getAssembler().registerSymbol(*Symbol);
206
207 // The implementation of symbol attributes is designed to match 'as', but it
208 // leaves much to desired. It doesn't really make sense to arbitrarily add and
209 // remove flags, but 'as' allows this (in particular, see .desc).
210 //
211 // In the future it might be worth trying to make these operations more well
212 // defined.
213 switch (Attribute) {
214 case MCSA_LazyReference:
215 case MCSA_Reference:
216 case MCSA_SymbolResolver:
217 case MCSA_PrivateExtern:
218 case MCSA_WeakDefinition:
219 case MCSA_WeakDefAutoPrivate:
220 case MCSA_Invalid:
221 case MCSA_IndirectSymbol:
222 return false;
223
224 case MCSA_NoDeadStrip:
225 // Ignore for now.
226 break;
227
228 case MCSA_ELF_TypeGnuUniqueObject:
229 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
230 Symbol->setBinding(ELF::STB_GNU_UNIQUE);
231 Symbol->setExternal(true);
232 break;
233
234 case MCSA_Global:
235 Symbol->setBinding(ELF::STB_GLOBAL);
236 Symbol->setExternal(true);
237 break;
238
239 case MCSA_WeakReference:
240 case MCSA_Weak:
241 Symbol->setBinding(ELF::STB_WEAK);
242 Symbol->setExternal(true);
243 break;
244
245 case MCSA_Local:
246 Symbol->setBinding(ELF::STB_LOCAL);
247 Symbol->setExternal(false);
248 break;
249
250 case MCSA_ELF_TypeFunction:
251 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
252 break;
253
254 case MCSA_ELF_TypeIndFunction:
255 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
256 break;
257
258 case MCSA_ELF_TypeObject:
259 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
260 break;
261
262 case MCSA_ELF_TypeTLS:
263 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
264 break;
265
266 case MCSA_ELF_TypeCommon:
267 // TODO: Emit these as a common symbol.
268 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
269 break;
270
271 case MCSA_ELF_TypeNoType:
272 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
273 break;
274
275 case MCSA_Protected:
276 Symbol->setVisibility(ELF::STV_PROTECTED);
277 break;
278
279 case MCSA_Hidden:
280 Symbol->setVisibility(ELF::STV_HIDDEN);
281 break;
282
283 case MCSA_Internal:
284 Symbol->setVisibility(ELF::STV_INTERNAL);
285 break;
286 }
287
288 return true;
289 }
290
EmitCommonSymbol(MCSymbol * S,uint64_t Size,unsigned ByteAlignment)291 void MCELFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
292 unsigned ByteAlignment) {
293 auto *Symbol = cast<MCSymbolELF>(S);
294 getAssembler().registerSymbol(*Symbol);
295
296 if (!Symbol->isBindingSet()) {
297 Symbol->setBinding(ELF::STB_GLOBAL);
298 Symbol->setExternal(true);
299 }
300
301 Symbol->setType(ELF::STT_OBJECT);
302
303 if (Symbol->getBinding() == ELF::STB_LOCAL) {
304 MCSection &Section = *getAssembler().getContext().getELFSection(
305 ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
306 MCSectionSubPair P = getCurrentSection();
307 SwitchSection(&Section);
308
309 EmitValueToAlignment(ByteAlignment, 0, 1, 0);
310 EmitLabel(Symbol);
311 EmitZeros(Size);
312
313 // Update the maximum alignment of the section if necessary.
314 if (ByteAlignment > Section.getAlignment())
315 Section.setAlignment(ByteAlignment);
316
317 SwitchSection(P.first, P.second);
318 } else {
319 if(Symbol->declareCommon(Size, ByteAlignment))
320 report_fatal_error("Symbol: " + Symbol->getName() +
321 " redeclared as different type");
322 }
323
324 cast<MCSymbolELF>(Symbol)
325 ->setSize(MCConstantExpr::create(Size, getContext()));
326 }
327
emitELFSize(MCSymbolELF * Symbol,const MCExpr * Value)328 void MCELFStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) {
329 Symbol->setSize(Value);
330 }
331
EmitLocalCommonSymbol(MCSymbol * S,uint64_t Size,unsigned ByteAlignment)332 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
333 unsigned ByteAlignment) {
334 auto *Symbol = cast<MCSymbolELF>(S);
335 // FIXME: Should this be caught and done earlier?
336 getAssembler().registerSymbol(*Symbol);
337 Symbol->setBinding(ELF::STB_LOCAL);
338 Symbol->setExternal(false);
339 EmitCommonSymbol(Symbol, Size, ByteAlignment);
340 }
341
EmitValueImpl(const MCExpr * Value,unsigned Size,SMLoc Loc)342 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
343 SMLoc Loc) {
344 if (isBundleLocked())
345 report_fatal_error("Emitting values inside a locked bundle is forbidden");
346 fixSymbolsInTLSFixups(Value);
347 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
348 }
349
EmitValueToAlignment(unsigned ByteAlignment,int64_t Value,unsigned ValueSize,unsigned MaxBytesToEmit)350 void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
351 int64_t Value,
352 unsigned ValueSize,
353 unsigned MaxBytesToEmit) {
354 if (isBundleLocked())
355 report_fatal_error("Emitting values inside a locked bundle is forbidden");
356 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
357 ValueSize, MaxBytesToEmit);
358 }
359
360 // Add a symbol for the file name of this module. They start after the
361 // null symbol and don't count as normal symbol, i.e. a non-STT_FILE symbol
362 // with the same name may appear.
EmitFileDirective(StringRef Filename)363 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
364 getAssembler().addFileName(Filename);
365 }
366
EmitIdent(StringRef IdentString)367 void MCELFStreamer::EmitIdent(StringRef IdentString) {
368 MCSection *Comment = getAssembler().getContext().getELFSection(
369 ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
370 PushSection();
371 SwitchSection(Comment);
372 if (!SeenIdent) {
373 EmitIntValue(0, 1);
374 SeenIdent = true;
375 }
376 EmitBytes(IdentString);
377 EmitIntValue(0, 1);
378 PopSection();
379 }
380
fixSymbolsInTLSFixups(const MCExpr * expr)381 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
382 switch (expr->getKind()) {
383 case MCExpr::Target:
384 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
385 break;
386 case MCExpr::Constant:
387 break;
388
389 case MCExpr::Binary: {
390 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
391 fixSymbolsInTLSFixups(be->getLHS());
392 fixSymbolsInTLSFixups(be->getRHS());
393 break;
394 }
395
396 case MCExpr::SymbolRef: {
397 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
398 switch (symRef.getKind()) {
399 default:
400 return;
401 case MCSymbolRefExpr::VK_GOTTPOFF:
402 case MCSymbolRefExpr::VK_INDNTPOFF:
403 case MCSymbolRefExpr::VK_NTPOFF:
404 case MCSymbolRefExpr::VK_GOTNTPOFF:
405 case MCSymbolRefExpr::VK_TLSGD:
406 case MCSymbolRefExpr::VK_TLSLD:
407 case MCSymbolRefExpr::VK_TLSLDM:
408 case MCSymbolRefExpr::VK_TPOFF:
409 case MCSymbolRefExpr::VK_DTPOFF:
410 case MCSymbolRefExpr::VK_Mips_TLSGD:
411 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
412 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
413 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
414 case MCSymbolRefExpr::VK_PPC_DTPMOD:
415 case MCSymbolRefExpr::VK_PPC_TPREL:
416 case MCSymbolRefExpr::VK_PPC_TPREL_LO:
417 case MCSymbolRefExpr::VK_PPC_TPREL_HI:
418 case MCSymbolRefExpr::VK_PPC_TPREL_HA:
419 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
420 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
421 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
422 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
423 case MCSymbolRefExpr::VK_PPC_DTPREL:
424 case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
425 case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
426 case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
427 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:
432 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
433 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
434 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
435 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:
439 case MCSymbolRefExpr::VK_PPC_TLS:
440 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
441 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
442 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
443 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
444 case MCSymbolRefExpr::VK_PPC_TLSGD:
445 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
446 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
447 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
448 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
449 case MCSymbolRefExpr::VK_PPC_TLSLD:
450 break;
451 }
452 getAssembler().registerSymbol(symRef.getSymbol());
453 cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
454 break;
455 }
456
457 case MCExpr::Unary:
458 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
459 break;
460 }
461 }
462
EmitInstToFragment(const MCInst & Inst,const MCSubtargetInfo & STI)463 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
464 const MCSubtargetInfo &STI) {
465 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
466 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
467
468 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
469 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
470 }
471
EmitInstToData(const MCInst & Inst,const MCSubtargetInfo & STI)472 void MCELFStreamer::EmitInstToData(const MCInst &Inst,
473 const MCSubtargetInfo &STI) {
474 MCAssembler &Assembler = getAssembler();
475 SmallVector<MCFixup, 4> Fixups;
476 SmallString<256> Code;
477 raw_svector_ostream VecOS(Code);
478 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
479
480 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
481 fixSymbolsInTLSFixups(Fixups[i].getValue());
482
483 // 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:
490 // - 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.
494 // - 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()) {
501 MCSection &Sec = *getCurrentSectionOnly();
502 if (Assembler.getRelaxAll() && isBundleLocked())
503 // 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();
506 else if (Assembler.getRelaxAll() && !isBundleLocked())
507 // 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();
511 else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst())
512 // 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());
515 else if (!isBundleLocked() && Fixups.size() == 0) {
516 // 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.
519 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
520 insert(CEIF);
521 CEIF->getContents().append(Code.begin(), Code.end());
522 return;
523 } else {
524 DF = new MCDataFragment();
525 insert(DF);
526 }
527 if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
528 // 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);
533 }
534
535 // We're now emitting an instruction in a bundle group, so this flag has
536 // to be turned off.
537 Sec.setBundleGroupBeforeFirstInst(false);
538 } else {
539 DF = getOrCreateDataFragment();
540 }
541
542 // 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());
545 DF->getFixups().push_back(Fixups[i]);
546 }
547 DF->setHasInstructions(true);
548 DF->getContents().append(Code.begin(), Code.end());
549
550 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
551 if (!isBundleLocked()) {
552 mergeFragment(getOrCreateDataFragment(), DF);
553 delete DF;
554 }
555 }
556 }
557
EmitBundleAlignMode(unsigned AlignPow2)558 void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
559 assert(AlignPow2 <= 30 && "Invalid bundle alignment");
560 MCAssembler &Assembler = getAssembler();
561 if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
562 Assembler.getBundleAlignSize() == 1U << AlignPow2))
563 Assembler.setBundleAlignSize(1U << AlignPow2);
564 else
565 report_fatal_error(".bundle_align_mode cannot be changed once set");
566 }
567
EmitBundleLock(bool AlignToEnd)568 void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
569 MCSection &Sec = *getCurrentSectionOnly();
570
571 // Sanity checks
572 //
573 if (!getAssembler().isBundlingEnabled())
574 report_fatal_error(".bundle_lock forbidden when bundling is disabled");
575
576 if (!isBundleLocked())
577 Sec.setBundleGroupBeforeFirstInst(true);
578
579 if (getAssembler().getRelaxAll() && !isBundleLocked()) {
580 // TODO: drop the lock state and set directly in the fragment
581 MCDataFragment *DF = new MCDataFragment();
582 BundleGroups.push_back(DF);
583 }
584
585 Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
586 : MCSection::BundleLocked);
587 }
588
EmitBundleUnlock()589 void MCELFStreamer::EmitBundleUnlock() {
590 MCSection &Sec = *getCurrentSectionOnly();
591
592 // Sanity checks
593 if (!getAssembler().isBundlingEnabled())
594 report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
595 else if (!isBundleLocked())
596 report_fatal_error(".bundle_unlock without matching lock");
597 else if (Sec.isBundleGroupBeforeFirstInst())
598 report_fatal_error("Empty bundle-locked group is forbidden");
599
600 // When the -mc-relax-all flag is used, we emit instructions to fragments
601 // stored on a stack. When the bundle unlock is emitted, we pop a fragment
602 // 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.
608 Sec.setBundleLockState(MCSection::NotBundleLocked);
609
610 // FIXME: Use more separate fragments for nested groups.
611 if (!isBundleLocked()) {
612 mergeFragment(getOrCreateDataFragment(), DF);
613 BundleGroups.pop_back();
614 delete DF;
615 }
616
617 if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
618 getOrCreateDataFragment()->setAlignToBundleEnd(false);
619 } else
620 Sec.setBundleLockState(MCSection::NotBundleLocked);
621 }
622
FinishImpl()623 void MCELFStreamer::FinishImpl() {
624 // Ensure the last section gets aligned if necessary.
625 MCSection *CurSection = getCurrentSectionOnly();
626 setSectionAlignmentForBundling(getAssembler(), CurSection);
627
628 EmitFrames(nullptr);
629
630 this->MCObjectStreamer::FinishImpl();
631 }
632
createELFStreamer(MCContext & Context,MCAsmBackend & MAB,raw_pwrite_stream & OS,MCCodeEmitter * CE,bool RelaxAll)633 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
634 raw_pwrite_stream &OS, MCCodeEmitter *CE,
635 bool RelaxAll) {
636 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
637 if (RelaxAll)
638 S->getAssembler().setRelaxAll(true);
639 return S;
640 }
641
EmitThumbFunc(MCSymbol * Func)642 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
643 llvm_unreachable("Generic ELF doesn't support this directive");
644 }
645
EmitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)646 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
647 llvm_unreachable("ELF doesn't support this directive");
648 }
649
BeginCOFFSymbolDef(const MCSymbol * Symbol)650 void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
651 llvm_unreachable("ELF doesn't support this directive");
652 }
653
EmitCOFFSymbolStorageClass(int StorageClass)654 void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
655 llvm_unreachable("ELF doesn't support this directive");
656 }
657
EmitCOFFSymbolType(int Type)658 void MCELFStreamer::EmitCOFFSymbolType(int Type) {
659 llvm_unreachable("ELF doesn't support this directive");
660 }
661
EndCOFFSymbolDef()662 void MCELFStreamer::EndCOFFSymbolDef() {
663 llvm_unreachable("ELF doesn't support this directive");
664 }
665
EmitZerofill(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)666 void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
667 uint64_t Size, unsigned ByteAlignment) {
668 llvm_unreachable("ELF doesn't support this directive");
669 }
670
EmitTBSSSymbol(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)671 void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
672 uint64_t Size, unsigned ByteAlignment) {
673 llvm_unreachable("ELF doesn't support this directive");
674 }
675