1 //===- lib/MC/MCELFStreamer.cpp - ELF Object Output ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file assembles .s files and emits ELF .o object files.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCELFSymbolFlags.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCObjectStreamer.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/MC/MCValue.h"
30 #include "llvm/MC/MCAsmBackend.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ELF.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
39 class MCELFStreamer : public MCObjectStreamer {
41 MCELFStreamer(MCContext &Context, MCAsmBackend &TAB,
42 raw_ostream &OS, MCCodeEmitter *Emitter)
43 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
45 MCELFStreamer(MCContext &Context, MCAsmBackend &TAB,
46 raw_ostream &OS, MCCodeEmitter *Emitter,
47 MCAssembler *Assembler)
48 : MCObjectStreamer(Context, TAB, OS, Emitter, Assembler) {}
53 /// @name MCStreamer Interface
56 virtual void InitSections();
57 virtual void ChangeSection(const MCSection *Section);
58 virtual void EmitLabel(MCSymbol *Symbol);
59 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
60 virtual void EmitThumbFunc(MCSymbol *Func);
61 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
62 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
63 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
64 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
65 llvm_unreachable("ELF doesn't support this directive");
67 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68 unsigned ByteAlignment);
69 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
70 llvm_unreachable("ELF doesn't support this directive");
73 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
74 llvm_unreachable("ELF doesn't support this directive");
77 virtual void EmitCOFFSymbolType(int Type) {
78 llvm_unreachable("ELF doesn't support this directive");
81 virtual void EndCOFFSymbolDef() {
82 llvm_unreachable("ELF doesn't support this directive");
85 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
86 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
90 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
91 unsigned ByteAlignment);
93 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
94 uint64_t Size = 0, unsigned ByteAlignment = 0) {
95 llvm_unreachable("ELF doesn't support this directive");
97 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
98 uint64_t Size, unsigned ByteAlignment = 0) {
99 llvm_unreachable("ELF doesn't support this directive");
101 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
104 virtual void EmitFileDirective(StringRef Filename);
106 virtual void FinishImpl();
109 virtual void EmitInstToFragment(const MCInst &Inst);
110 virtual void EmitInstToData(const MCInst &Inst);
112 void fixSymbolsInTLSFixups(const MCExpr *expr);
117 unsigned ByteAlignment;
119 std::vector<LocalCommon> LocalCommons;
121 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
123 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
125 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
128 void SetSectionData() {
129 SetSection(".data", ELF::SHT_PROGBITS,
130 ELF::SHF_WRITE |ELF::SHF_ALLOC,
131 SectionKind::getDataRel());
132 EmitCodeAlignment(4, 0);
134 void SetSectionText() {
135 SetSection(".text", ELF::SHT_PROGBITS,
137 ELF::SHF_ALLOC, SectionKind::getText());
138 EmitCodeAlignment(4, 0);
140 void SetSectionBss() {
141 SetSection(".bss", ELF::SHT_NOBITS,
143 ELF::SHF_ALLOC, SectionKind::getBSS());
144 EmitCodeAlignment(4, 0);
149 void MCELFStreamer::InitSections() {
150 // This emulates the same behavior of GNU as. This makes it easier
151 // to compare the output as the major sections are in the same order.
158 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
159 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
161 MCObjectStreamer::EmitLabel(Symbol);
163 const MCSectionELF &Section =
164 static_cast<const MCSectionELF&>(Symbol->getSection());
165 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
166 if (Section.getFlags() & ELF::SHF_TLS)
167 MCELF::SetType(SD, ELF::STT_TLS);
170 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
172 case MCAF_SyntaxUnified: return; // no-op here.
173 case MCAF_Code16: return; // Change parsing mode; no-op here.
174 case MCAF_Code32: return; // Change parsing mode; no-op here.
175 case MCAF_Code64: return; // Change parsing mode; no-op here.
176 case MCAF_SubsectionsViaSymbols:
177 getAssembler().setSubsectionsViaSymbols(true);
181 llvm_unreachable("invalid assembler flag!");
184 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
185 // FIXME: Anything needed here to flag the function as thumb?
187 getAssembler().setIsThumbFunc(Func);
189 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
190 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
193 void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
194 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
196 // FIXME: Lift context changes into super class.
197 getAssembler().getOrCreateSymbolData(*Symbol);
198 Symbol->setVariableValue(AddValueSymbols(Value));
201 void MCELFStreamer::ChangeSection(const MCSection *Section) {
202 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
204 getAssembler().getOrCreateSymbolData(*Grp);
205 this->MCObjectStreamer::ChangeSection(Section);
208 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
209 getAssembler().getOrCreateSymbolData(*Symbol);
210 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
211 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
212 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
213 Alias->setVariableValue(Value);
216 void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
217 MCSymbolAttr Attribute) {
218 // Indirect symbols are handled differently, to match how 'as' handles
219 // them. This makes writing matching .o files easier.
220 if (Attribute == MCSA_IndirectSymbol) {
221 // Note that we intentionally cannot use the symbol data here; this is
222 // important for matching the string table that 'as' generates.
223 IndirectSymbolData ISD;
225 ISD.SectionData = getCurrentSectionData();
226 getAssembler().getIndirectSymbols().push_back(ISD);
230 // Adding a symbol attribute always introduces the symbol, note that an
231 // important side effect of calling getOrCreateSymbolData here is to register
232 // the symbol with the assembler.
233 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
235 // The implementation of symbol attributes is designed to match 'as', but it
236 // leaves much to desired. It doesn't really make sense to arbitrarily add and
237 // remove flags, but 'as' allows this (in particular, see .desc).
239 // In the future it might be worth trying to make these operations more well
242 case MCSA_LazyReference:
244 case MCSA_SymbolResolver:
245 case MCSA_PrivateExtern:
246 case MCSA_WeakDefinition:
247 case MCSA_WeakDefAutoPrivate:
249 case MCSA_IndirectSymbol:
250 llvm_unreachable("Invalid symbol attribute for ELF!");
252 case MCSA_NoDeadStrip:
253 case MCSA_ELF_TypeGnuUniqueObject:
258 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
259 SD.setExternal(true);
260 BindingExplicitlySet.insert(Symbol);
263 case MCSA_WeakReference:
265 MCELF::SetBinding(SD, ELF::STB_WEAK);
266 SD.setExternal(true);
267 BindingExplicitlySet.insert(Symbol);
271 MCELF::SetBinding(SD, ELF::STB_LOCAL);
272 SD.setExternal(false);
273 BindingExplicitlySet.insert(Symbol);
276 case MCSA_ELF_TypeFunction:
277 MCELF::SetType(SD, ELF::STT_FUNC);
280 case MCSA_ELF_TypeIndFunction:
281 MCELF::SetType(SD, ELF::STT_GNU_IFUNC);
284 case MCSA_ELF_TypeObject:
285 MCELF::SetType(SD, ELF::STT_OBJECT);
288 case MCSA_ELF_TypeTLS:
289 MCELF::SetType(SD, ELF::STT_TLS);
292 case MCSA_ELF_TypeCommon:
293 MCELF::SetType(SD, ELF::STT_COMMON);
296 case MCSA_ELF_TypeNoType:
297 MCELF::SetType(SD, ELF::STT_NOTYPE);
301 MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
305 MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
309 MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
314 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
315 unsigned ByteAlignment) {
316 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
318 if (!BindingExplicitlySet.count(Symbol)) {
319 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
320 SD.setExternal(true);
323 MCELF::SetType(SD, ELF::STT_OBJECT);
325 if (MCELF::GetBinding(SD) == ELF_STB_Local) {
326 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
330 SectionKind::getBSS());
331 Symbol->setSection(*Section);
333 struct LocalCommon L = {&SD, Size, ByteAlignment};
334 LocalCommons.push_back(L);
336 SD.setCommon(Size, ByteAlignment);
339 SD.setSize(MCConstantExpr::Create(Size, getContext()));
342 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
343 unsigned ByteAlignment) {
344 // FIXME: Should this be caught and done earlier?
345 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
346 MCELF::SetBinding(SD, ELF::STB_LOCAL);
347 SD.setExternal(false);
348 BindingExplicitlySet.insert(Symbol);
349 EmitCommonSymbol(Symbol, Size, ByteAlignment);
352 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
353 unsigned AddrSpace) {
354 fixSymbolsInTLSFixups(Value);
355 MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace);
359 // Add a symbol for the file name of this module. This is the second
360 // entry in the module's symbol table (the first being the null symbol).
361 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
362 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
363 Symbol->setSection(*getCurrentSection());
364 Symbol->setAbsolute();
366 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
368 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
371 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
372 switch (expr->getKind()) {
373 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
374 case MCExpr::Constant:
377 case MCExpr::Binary: {
378 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
379 fixSymbolsInTLSFixups(be->getLHS());
380 fixSymbolsInTLSFixups(be->getRHS());
384 case MCExpr::SymbolRef: {
385 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
386 switch (symRef.getKind()) {
389 case MCSymbolRefExpr::VK_GOTTPOFF:
390 case MCSymbolRefExpr::VK_INDNTPOFF:
391 case MCSymbolRefExpr::VK_NTPOFF:
392 case MCSymbolRefExpr::VK_GOTNTPOFF:
393 case MCSymbolRefExpr::VK_TLSGD:
394 case MCSymbolRefExpr::VK_TLSLD:
395 case MCSymbolRefExpr::VK_TLSLDM:
396 case MCSymbolRefExpr::VK_TPOFF:
397 case MCSymbolRefExpr::VK_DTPOFF:
398 case MCSymbolRefExpr::VK_ARM_TLSGD:
399 case MCSymbolRefExpr::VK_ARM_TPOFF:
400 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
401 case MCSymbolRefExpr::VK_Mips_TLSGD:
402 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
403 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
404 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
407 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
408 MCELF::SetType(SD, ELF::STT_TLS);
413 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
418 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
419 this->MCObjectStreamer::EmitInstToFragment(Inst);
420 MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
422 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
423 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
426 void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
427 MCDataFragment *DF = getOrCreateDataFragment();
429 SmallVector<MCFixup, 4> Fixups;
430 SmallString<256> Code;
431 raw_svector_ostream VecOS(Code);
432 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
435 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
436 fixSymbolsInTLSFixups(Fixups[i].getValue());
438 // Add the fixups and data.
439 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
440 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
441 DF->addFixup(Fixups[i]);
443 DF->getContents().append(Code.begin(), Code.end());
446 void MCELFStreamer::FinishImpl() {
449 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
450 e = LocalCommons.end();
452 MCSymbolData *SD = i->SD;
453 uint64_t Size = i->Size;
454 unsigned ByteAlignment = i->ByteAlignment;
455 const MCSymbol &Symbol = SD->getSymbol();
456 const MCSection &Section = Symbol.getSection();
458 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
459 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
461 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
464 // Update the maximum alignment of the section if necessary.
465 if (ByteAlignment > SectData.getAlignment())
466 SectData.setAlignment(ByteAlignment);
469 this->MCObjectStreamer::FinishImpl();
472 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
473 raw_ostream &OS, MCCodeEmitter *CE,
474 bool RelaxAll, bool NoExecStack) {
475 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
477 S->getAssembler().setRelaxAll(true);
479 S->getAssembler().setNoExecStack(true);