1 //===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file assembles .s files and emits ELF .o object files.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/MC/MCELFStreamer.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCFixup.h"
24 #include "llvm/MC/MCFragment.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCObjectWriter.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCSectionELF.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCSymbolELF.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include <cassert>
37 #include <cstdint>
38
39 using namespace llvm;
40
MCELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> TAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter)41 MCELFStreamer::MCELFStreamer(MCContext &Context,
42 std::unique_ptr<MCAsmBackend> TAB,
43 std::unique_ptr<MCObjectWriter> OW,
44 std::unique_ptr<MCCodeEmitter> Emitter)
45 : MCObjectStreamer(Context, std::move(TAB), std::move(OW),
46 std::move(Emitter)) {}
47
isBundleLocked() const48 bool MCELFStreamer::isBundleLocked() const {
49 return getCurrentSectionOnly()->isBundleLocked();
50 }
51
mergeFragment(MCDataFragment * DF,MCDataFragment * EF)52 void MCELFStreamer::mergeFragment(MCDataFragment *DF,
53 MCDataFragment *EF) {
54 MCAssembler &Assembler = getAssembler();
55
56 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
57 uint64_t FSize = EF->getContents().size();
58
59 if (FSize > Assembler.getBundleAlignSize())
60 report_fatal_error("Fragment can't be larger than a bundle size");
61
62 uint64_t RequiredBundlePadding = computeBundlePadding(
63 Assembler, EF, DF->getContents().size(), FSize);
64
65 if (RequiredBundlePadding > UINT8_MAX)
66 report_fatal_error("Padding cannot exceed 255 bytes");
67
68 if (RequiredBundlePadding > 0) {
69 SmallString<256> Code;
70 raw_svector_ostream VecOS(Code);
71 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
72 Assembler.writeFragmentPadding(VecOS, *EF, FSize);
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 if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
86 DF->setHasInstructions(*EF->getSubtargetInfo());
87 DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
88 }
89
InitSections(bool NoExecStack)90 void MCELFStreamer::InitSections(bool NoExecStack) {
91 MCContext &Ctx = getContext();
92 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
93 emitCodeAlignment(4);
94
95 if (NoExecStack)
96 SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
97 }
98
emitLabel(MCSymbol * S,SMLoc Loc)99 void MCELFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) {
100 auto *Symbol = cast<MCSymbolELF>(S);
101 MCObjectStreamer::emitLabel(Symbol, Loc);
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
emitLabelAtPos(MCSymbol * S,SMLoc Loc,MCFragment * F,uint64_t Offset)109 void MCELFStreamer::emitLabelAtPos(MCSymbol *S, SMLoc Loc, MCFragment *F,
110 uint64_t Offset) {
111 auto *Symbol = cast<MCSymbolELF>(S);
112 MCObjectStreamer::emitLabelAtPos(Symbol, Loc, F, Offset);
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
emitAssemblerFlag(MCAssemblerFlag Flag)120 void MCELFStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
121 // 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.
124 switch (Flag) {
125 case MCAF_SyntaxUnified: return; // no-op here.
126 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.
129 case MCAF_SubsectionsViaSymbols:
130 getAssembler().setSubsectionsViaSymbols(true);
131 return;
132 }
133
134 llvm_unreachable("invalid assembler flag!");
135 }
136
137 // If bundle alignment is used and there are any instructions in the section, it
138 // needs to be aligned to at least the bundle size.
setSectionAlignmentForBundling(const MCAssembler & Assembler,MCSection * Section)139 static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
140 MCSection *Section) {
141 if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
142 Section->getAlignment() < Assembler.getBundleAlignSize())
143 Section->setAlignment(Align(Assembler.getBundleAlignSize()));
144 }
145
changeSection(MCSection * Section,const MCExpr * Subsection)146 void MCELFStreamer::changeSection(MCSection *Section,
147 const MCExpr *Subsection) {
148 MCSection *CurSection = getCurrentSectionOnly();
149 if (CurSection && isBundleLocked())
150 report_fatal_error("Unterminated .bundle_lock when changing a section");
151
152 MCAssembler &Asm = getAssembler();
153 // Ensure the previous section gets aligned if necessary.
154 setSectionAlignmentForBundling(Asm, CurSection);
155 auto *SectionELF = static_cast<const MCSectionELF *>(Section);
156 const MCSymbol *Grp = SectionELF->getGroup();
157 if (Grp)
158 Asm.registerSymbol(*Grp);
159 if (SectionELF->getFlags() & ELF::SHF_GNU_RETAIN)
160 Asm.getWriter().markGnuAbi();
161
162 changeSectionImpl(Section, Subsection);
163 Asm.registerSymbol(*Section->getBeginSymbol());
164 }
165
emitWeakReference(MCSymbol * Alias,const MCSymbol * Symbol)166 void MCELFStreamer::emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
167 getAssembler().registerSymbol(*Symbol);
168 const MCExpr *Value = MCSymbolRefExpr::create(
169 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
170 Alias->setVariableValue(Value);
171 }
172
173 // When GNU as encounters more than one .type declaration for an object it seems
174 // to use a mechanism similar to the one below to decide which type is actually
175 // used in the object file. The greater of T1 and T2 is selected based on the
176 // following ordering:
177 // STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
178 // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
179 // provided type).
CombineSymbolTypes(unsigned T1,unsigned T2)180 static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
181 for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
182 ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
183 if (T1 == Type)
184 return T2;
185 if (T2 == Type)
186 return T1;
187 }
188
189 return T2;
190 }
191
emitSymbolAttribute(MCSymbol * S,MCSymbolAttr Attribute)192 bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
193 auto *Symbol = cast<MCSymbolELF>(S);
194
195 // Adding a symbol attribute always introduces the symbol, note that an
196 // important side effect of calling registerSymbol here is to register
197 // the symbol with the assembler.
198 getAssembler().registerSymbol(*Symbol);
199
200 // The implementation of symbol attributes is designed to match 'as', but it
201 // leaves much to desired. It doesn't really make sense to arbitrarily add and
202 // remove flags, but 'as' allows this (in particular, see .desc).
203 //
204 // In the future it might be worth trying to make these operations more well
205 // defined.
206 switch (Attribute) {
207 case MCSA_Cold:
208 case MCSA_Extern:
209 case MCSA_LazyReference:
210 case MCSA_Reference:
211 case MCSA_SymbolResolver:
212 case MCSA_PrivateExtern:
213 case MCSA_WeakDefinition:
214 case MCSA_WeakDefAutoPrivate:
215 case MCSA_Invalid:
216 case MCSA_IndirectSymbol:
217 return false;
218
219 case MCSA_NoDeadStrip:
220 // Ignore for now.
221 break;
222
223 case MCSA_ELF_TypeGnuUniqueObject:
224 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
225 Symbol->setBinding(ELF::STB_GNU_UNIQUE);
226 break;
227
228 case MCSA_Global:
229 // For `.weak x; .global x`, GNU as sets the binding to STB_WEAK while we
230 // traditionally set the binding to STB_GLOBAL. This is error-prone, so we
231 // error on such cases. Note, we also disallow changed binding from .local.
232 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_GLOBAL)
233 getContext().reportError(getStartTokLoc(),
234 Symbol->getName() +
235 " changed binding to STB_GLOBAL");
236 Symbol->setBinding(ELF::STB_GLOBAL);
237 break;
238
239 case MCSA_WeakReference:
240 case MCSA_Weak:
241 // For `.global x; .weak x`, both MC and GNU as set the binding to STB_WEAK.
242 // We emit a warning for now but may switch to an error in the future.
243 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_WEAK)
244 getContext().reportWarning(
245 getStartTokLoc(), Symbol->getName() + " changed binding to STB_WEAK");
246 Symbol->setBinding(ELF::STB_WEAK);
247 break;
248
249 case MCSA_Local:
250 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_LOCAL)
251 getContext().reportError(getStartTokLoc(),
252 Symbol->getName() +
253 " changed binding to STB_LOCAL");
254 Symbol->setBinding(ELF::STB_LOCAL);
255 break;
256
257 case MCSA_ELF_TypeFunction:
258 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
259 break;
260
261 case MCSA_ELF_TypeIndFunction:
262 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
263 break;
264
265 case MCSA_ELF_TypeObject:
266 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
267 break;
268
269 case MCSA_ELF_TypeTLS:
270 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
271 break;
272
273 case MCSA_ELF_TypeCommon:
274 // TODO: Emit these as a common symbol.
275 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
276 break;
277
278 case MCSA_ELF_TypeNoType:
279 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
280 break;
281
282 case MCSA_Protected:
283 Symbol->setVisibility(ELF::STV_PROTECTED);
284 break;
285
286 case MCSA_Hidden:
287 Symbol->setVisibility(ELF::STV_HIDDEN);
288 break;
289
290 case MCSA_Internal:
291 Symbol->setVisibility(ELF::STV_INTERNAL);
292 break;
293
294 case MCSA_AltEntry:
295 llvm_unreachable("ELF doesn't support the .alt_entry attribute");
296
297 case MCSA_LGlobal:
298 llvm_unreachable("ELF doesn't support the .lglobl attribute");
299 }
300
301 return true;
302 }
303
emitCommonSymbol(MCSymbol * S,uint64_t Size,unsigned ByteAlignment)304 void MCELFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,
305 unsigned ByteAlignment) {
306 auto *Symbol = cast<MCSymbolELF>(S);
307 getAssembler().registerSymbol(*Symbol);
308
309 if (!Symbol->isBindingSet())
310 Symbol->setBinding(ELF::STB_GLOBAL);
311
312 Symbol->setType(ELF::STT_OBJECT);
313
314 if (Symbol->getBinding() == ELF::STB_LOCAL) {
315 MCSection &Section = *getAssembler().getContext().getELFSection(
316 ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
317 MCSectionSubPair P = getCurrentSection();
318 SwitchSection(&Section);
319
320 emitValueToAlignment(ByteAlignment, 0, 1, 0);
321 emitLabel(Symbol);
322 emitZeros(Size);
323
324 SwitchSection(P.first, P.second);
325 } else {
326 if(Symbol->declareCommon(Size, ByteAlignment))
327 report_fatal_error("Symbol: " + Symbol->getName() +
328 " redeclared as different type");
329 }
330
331 cast<MCSymbolELF>(Symbol)
332 ->setSize(MCConstantExpr::create(Size, getContext()));
333 }
334
emitELFSize(MCSymbol * Symbol,const MCExpr * Value)335 void MCELFStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
336 cast<MCSymbolELF>(Symbol)->setSize(Value);
337 }
338
emitELFSymverDirective(const MCSymbol * OriginalSym,StringRef Name,bool KeepOriginalSym)339 void MCELFStreamer::emitELFSymverDirective(const MCSymbol *OriginalSym,
340 StringRef Name,
341 bool KeepOriginalSym) {
342 getAssembler().Symvers.push_back(MCAssembler::Symver{
343 getStartTokLoc(), OriginalSym, Name, KeepOriginalSym});
344 }
345
emitLocalCommonSymbol(MCSymbol * S,uint64_t Size,unsigned ByteAlignment)346 void MCELFStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
347 unsigned ByteAlignment) {
348 auto *Symbol = cast<MCSymbolELF>(S);
349 // FIXME: Should this be caught and done earlier?
350 getAssembler().registerSymbol(*Symbol);
351 Symbol->setBinding(ELF::STB_LOCAL);
352 emitCommonSymbol(Symbol, Size, ByteAlignment);
353 }
354
emitValueImpl(const MCExpr * Value,unsigned Size,SMLoc Loc)355 void MCELFStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
356 SMLoc Loc) {
357 if (isBundleLocked())
358 report_fatal_error("Emitting values inside a locked bundle is forbidden");
359 fixSymbolsInTLSFixups(Value);
360 MCObjectStreamer::emitValueImpl(Value, Size, Loc);
361 }
362
emitValueToAlignment(unsigned ByteAlignment,int64_t Value,unsigned ValueSize,unsigned MaxBytesToEmit)363 void MCELFStreamer::emitValueToAlignment(unsigned ByteAlignment,
364 int64_t Value,
365 unsigned ValueSize,
366 unsigned MaxBytesToEmit) {
367 if (isBundleLocked())
368 report_fatal_error("Emitting values inside a locked bundle is forbidden");
369 MCObjectStreamer::emitValueToAlignment(ByteAlignment, Value,
370 ValueSize, MaxBytesToEmit);
371 }
372
emitCGProfileEntry(const MCSymbolRefExpr * From,const MCSymbolRefExpr * To,uint64_t Count)373 void MCELFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From,
374 const MCSymbolRefExpr *To,
375 uint64_t Count) {
376 getAssembler().CGProfile.push_back({From, To, Count});
377 }
378
emitIdent(StringRef IdentString)379 void MCELFStreamer::emitIdent(StringRef IdentString) {
380 MCSection *Comment = getAssembler().getContext().getELFSection(
381 ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1);
382 PushSection();
383 SwitchSection(Comment);
384 if (!SeenIdent) {
385 emitInt8(0);
386 SeenIdent = true;
387 }
388 emitBytes(IdentString);
389 emitInt8(0);
390 PopSection();
391 }
392
fixSymbolsInTLSFixups(const MCExpr * expr)393 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
394 switch (expr->getKind()) {
395 case MCExpr::Target:
396 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
397 break;
398 case MCExpr::Constant:
399 break;
400
401 case MCExpr::Binary: {
402 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
403 fixSymbolsInTLSFixups(be->getLHS());
404 fixSymbolsInTLSFixups(be->getRHS());
405 break;
406 }
407
408 case MCExpr::SymbolRef: {
409 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
410 switch (symRef.getKind()) {
411 default:
412 return;
413 case MCSymbolRefExpr::VK_GOTTPOFF:
414 case MCSymbolRefExpr::VK_INDNTPOFF:
415 case MCSymbolRefExpr::VK_NTPOFF:
416 case MCSymbolRefExpr::VK_GOTNTPOFF:
417 case MCSymbolRefExpr::VK_TLSCALL:
418 case MCSymbolRefExpr::VK_TLSDESC:
419 case MCSymbolRefExpr::VK_TLSGD:
420 case MCSymbolRefExpr::VK_TLSLD:
421 case MCSymbolRefExpr::VK_TLSLDM:
422 case MCSymbolRefExpr::VK_TPOFF:
423 case MCSymbolRefExpr::VK_TPREL:
424 case MCSymbolRefExpr::VK_DTPOFF:
425 case MCSymbolRefExpr::VK_DTPREL:
426 case MCSymbolRefExpr::VK_PPC_DTPMOD:
427 case MCSymbolRefExpr::VK_PPC_TPREL_LO:
428 case MCSymbolRefExpr::VK_PPC_TPREL_HI:
429 case MCSymbolRefExpr::VK_PPC_TPREL_HA:
430 case MCSymbolRefExpr::VK_PPC_TPREL_HIGH:
431 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHA:
432 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
433 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
434 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
435 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
436 case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
437 case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
438 case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
439 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGH:
440 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA:
441 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
442 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
443 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
444 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
445 case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
446 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
447 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
448 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
449 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_PCREL:
450 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
451 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
452 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
453 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
454 case MCSymbolRefExpr::VK_PPC_TLS:
455 case MCSymbolRefExpr::VK_PPC_TLS_PCREL:
456 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
457 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
458 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
459 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
460 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_PCREL:
461 case MCSymbolRefExpr::VK_PPC_TLSGD:
462 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
463 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
464 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
465 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
466 case MCSymbolRefExpr::VK_PPC_TLSLD:
467 break;
468 }
469 getAssembler().registerSymbol(symRef.getSymbol());
470 cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
471 break;
472 }
473
474 case MCExpr::Unary:
475 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
476 break;
477 }
478 }
479
finalizeCGProfileEntry(const MCSymbolRefExpr * & SRE)480 void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
481 const MCSymbol *S = &SRE->getSymbol();
482 if (S->isTemporary()) {
483 if (!S->isInSection()) {
484 getContext().reportError(
485 SRE->getLoc(), Twine("Reference to undefined temporary symbol ") +
486 "`" + S->getName() + "`");
487 return;
488 }
489 S = S->getSection().getBeginSymbol();
490 S->setUsedInReloc();
491 SRE =
492 MCSymbolRefExpr::create(S, SRE->getKind(), getContext(), SRE->getLoc());
493 return;
494 }
495 // Not a temporary, referece it as a weak undefined.
496 bool Created;
497 getAssembler().registerSymbol(*S, &Created);
498 if (Created)
499 cast<MCSymbolELF>(S)->setBinding(ELF::STB_WEAK);
500 }
501
finalizeCGProfile()502 void MCELFStreamer::finalizeCGProfile() {
503 for (MCAssembler::CGProfileEntry &E : getAssembler().CGProfile) {
504 finalizeCGProfileEntry(E.From);
505 finalizeCGProfileEntry(E.To);
506 }
507 }
508
emitInstToFragment(const MCInst & Inst,const MCSubtargetInfo & STI)509 void MCELFStreamer::emitInstToFragment(const MCInst &Inst,
510 const MCSubtargetInfo &STI) {
511 this->MCObjectStreamer::emitInstToFragment(Inst, STI);
512 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
513
514 for (auto &Fixup : F.getFixups())
515 fixSymbolsInTLSFixups(Fixup.getValue());
516 }
517
518 // A fragment can only have one Subtarget, and when bundling is enabled we
519 // sometimes need to use the same fragment. We give an error if there
520 // are conflicting Subtargets.
CheckBundleSubtargets(const MCSubtargetInfo * OldSTI,const MCSubtargetInfo * NewSTI)521 static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI,
522 const MCSubtargetInfo *NewSTI) {
523 if (OldSTI && NewSTI && OldSTI != NewSTI)
524 report_fatal_error("A Bundle can only have one Subtarget.");
525 }
526
emitInstToData(const MCInst & Inst,const MCSubtargetInfo & STI)527 void MCELFStreamer::emitInstToData(const MCInst &Inst,
528 const MCSubtargetInfo &STI) {
529 MCAssembler &Assembler = getAssembler();
530 SmallVector<MCFixup, 4> Fixups;
531 SmallString<256> Code;
532 raw_svector_ostream VecOS(Code);
533 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
534
535 for (auto &Fixup : Fixups)
536 fixSymbolsInTLSFixups(Fixup.getValue());
537
538 // There are several possibilities here:
539 //
540 // If bundling is disabled, append the encoded instruction to the current data
541 // fragment (or create a new such fragment if the current fragment is not a
542 // data fragment, or the Subtarget has changed).
543 //
544 // If bundling is enabled:
545 // - If we're not in a bundle-locked group, emit the instruction into a
546 // fragment of its own. If there are no fixups registered for the
547 // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
548 // MCDataFragment.
549 // - If we're in a bundle-locked group, append the instruction to the current
550 // data fragment because we want all the instructions in a group to get into
551 // the same fragment. Be careful not to do that for the first instruction in
552 // the group, though.
553 MCDataFragment *DF;
554
555 if (Assembler.isBundlingEnabled()) {
556 MCSection &Sec = *getCurrentSectionOnly();
557 if (Assembler.getRelaxAll() && isBundleLocked()) {
558 // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
559 // the current bundle group.
560 DF = BundleGroups.back();
561 CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
562 }
563 else if (Assembler.getRelaxAll() && !isBundleLocked())
564 // When not in a bundle-locked group and the -mc-relax-all flag is used,
565 // we create a new temporary fragment which will be later merged into
566 // the current fragment.
567 DF = new MCDataFragment();
568 else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst()) {
569 // If we are bundle-locked, we re-use the current fragment.
570 // The bundle-locking directive ensures this is a new data fragment.
571 DF = cast<MCDataFragment>(getCurrentFragment());
572 CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
573 }
574 else if (!isBundleLocked() && Fixups.size() == 0) {
575 // Optimize memory usage by emitting the instruction to a
576 // MCCompactEncodedInstFragment when not in a bundle-locked group and
577 // there are no fixups registered.
578 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
579 insert(CEIF);
580 CEIF->getContents().append(Code.begin(), Code.end());
581 CEIF->setHasInstructions(STI);
582 return;
583 } else {
584 DF = new MCDataFragment();
585 insert(DF);
586 }
587 if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
588 // If this fragment is for a group marked "align_to_end", set a flag
589 // in the fragment. This can happen after the fragment has already been
590 // created if there are nested bundle_align groups and an inner one
591 // is the one marked align_to_end.
592 DF->setAlignToBundleEnd(true);
593 }
594
595 // We're now emitting an instruction in a bundle group, so this flag has
596 // to be turned off.
597 Sec.setBundleGroupBeforeFirstInst(false);
598 } else {
599 DF = getOrCreateDataFragment(&STI);
600 }
601
602 // Add the fixups and data.
603 for (auto &Fixup : Fixups) {
604 Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
605 DF->getFixups().push_back(Fixup);
606 }
607
608 DF->setHasInstructions(STI);
609 DF->getContents().append(Code.begin(), Code.end());
610
611 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
612 if (!isBundleLocked()) {
613 mergeFragment(getOrCreateDataFragment(&STI), DF);
614 delete DF;
615 }
616 }
617 }
618
emitBundleAlignMode(unsigned AlignPow2)619 void MCELFStreamer::emitBundleAlignMode(unsigned AlignPow2) {
620 assert(AlignPow2 <= 30 && "Invalid bundle alignment");
621 MCAssembler &Assembler = getAssembler();
622 if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
623 Assembler.getBundleAlignSize() == 1U << AlignPow2))
624 Assembler.setBundleAlignSize(1U << AlignPow2);
625 else
626 report_fatal_error(".bundle_align_mode cannot be changed once set");
627 }
628
emitBundleLock(bool AlignToEnd)629 void MCELFStreamer::emitBundleLock(bool AlignToEnd) {
630 MCSection &Sec = *getCurrentSectionOnly();
631
632 // Sanity checks
633 //
634 if (!getAssembler().isBundlingEnabled())
635 report_fatal_error(".bundle_lock forbidden when bundling is disabled");
636
637 if (!isBundleLocked())
638 Sec.setBundleGroupBeforeFirstInst(true);
639
640 if (getAssembler().getRelaxAll() && !isBundleLocked()) {
641 // TODO: drop the lock state and set directly in the fragment
642 MCDataFragment *DF = new MCDataFragment();
643 BundleGroups.push_back(DF);
644 }
645
646 Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
647 : MCSection::BundleLocked);
648 }
649
emitBundleUnlock()650 void MCELFStreamer::emitBundleUnlock() {
651 MCSection &Sec = *getCurrentSectionOnly();
652
653 // Sanity checks
654 if (!getAssembler().isBundlingEnabled())
655 report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
656 else if (!isBundleLocked())
657 report_fatal_error(".bundle_unlock without matching lock");
658 else if (Sec.isBundleGroupBeforeFirstInst())
659 report_fatal_error("Empty bundle-locked group is forbidden");
660
661 // When the -mc-relax-all flag is used, we emit instructions to fragments
662 // stored on a stack. When the bundle unlock is emitted, we pop a fragment
663 // from the stack a merge it to the one below.
664 if (getAssembler().getRelaxAll()) {
665 assert(!BundleGroups.empty() && "There are no bundle groups");
666 MCDataFragment *DF = BundleGroups.back();
667
668 // FIXME: Use BundleGroups to track the lock state instead.
669 Sec.setBundleLockState(MCSection::NotBundleLocked);
670
671 // FIXME: Use more separate fragments for nested groups.
672 if (!isBundleLocked()) {
673 mergeFragment(getOrCreateDataFragment(DF->getSubtargetInfo()), DF);
674 BundleGroups.pop_back();
675 delete DF;
676 }
677
678 if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
679 getOrCreateDataFragment()->setAlignToBundleEnd(false);
680 } else
681 Sec.setBundleLockState(MCSection::NotBundleLocked);
682 }
683
finishImpl()684 void MCELFStreamer::finishImpl() {
685 // Ensure the last section gets aligned if necessary.
686 MCSection *CurSection = getCurrentSectionOnly();
687 setSectionAlignmentForBundling(getAssembler(), CurSection);
688
689 finalizeCGProfile();
690 emitFrames(nullptr);
691
692 this->MCObjectStreamer::finishImpl();
693 }
694
emitThumbFunc(MCSymbol * Func)695 void MCELFStreamer::emitThumbFunc(MCSymbol *Func) {
696 llvm_unreachable("Generic ELF doesn't support this directive");
697 }
698
emitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)699 void MCELFStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
700 llvm_unreachable("ELF doesn't support this directive");
701 }
702
emitZerofill(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment,SMLoc Loc)703 void MCELFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
704 uint64_t Size, unsigned ByteAlignment,
705 SMLoc Loc) {
706 llvm_unreachable("ELF doesn't support this directive");
707 }
708
emitTBSSSymbol(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)709 void MCELFStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
710 uint64_t Size, unsigned ByteAlignment) {
711 llvm_unreachable("ELF doesn't support this directive");
712 }
713
createELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> && MAB,std::unique_ptr<MCObjectWriter> && OW,std::unique_ptr<MCCodeEmitter> && CE,bool RelaxAll)714 MCStreamer *llvm::createELFStreamer(MCContext &Context,
715 std::unique_ptr<MCAsmBackend> &&MAB,
716 std::unique_ptr<MCObjectWriter> &&OW,
717 std::unique_ptr<MCCodeEmitter> &&CE,
718 bool RelaxAll) {
719 MCELFStreamer *S =
720 new MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
721 if (RelaxAll)
722 S->getAssembler().setRelaxAll(true);
723 return S;
724 }
725