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/MC/MCAssembler.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCELFSymbolFlags.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCObjectStreamer.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCValue.h"
28 #include "llvm/MC/MCAsmBackend.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ELF.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/raw_ostream.h"
37 class MCELFStreamer : public MCObjectStreamer {
39 MCELFStreamer(MCContext &Context, MCAsmBackend &TAB,
40 raw_ostream &OS, MCCodeEmitter *Emitter)
41 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
43 MCELFStreamer(MCContext &Context, MCAsmBackend &TAB,
44 raw_ostream &OS, MCCodeEmitter *Emitter,
45 MCAssembler *Assembler)
46 : MCObjectStreamer(Context, TAB, OS, Emitter, Assembler) {}
51 /// @name MCStreamer Interface
54 virtual void InitSections();
55 virtual void ChangeSection(const MCSection *Section);
56 virtual void EmitLabel(MCSymbol *Symbol);
57 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
58 virtual void EmitThumbFunc(MCSymbol *Func);
59 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
60 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
61 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
62 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
63 assert(0 && "ELF doesn't support this directive");
65 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
66 unsigned ByteAlignment);
67 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
68 assert(0 && "ELF doesn't support this directive");
71 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
72 assert(0 && "ELF doesn't support this directive");
75 virtual void EmitCOFFSymbolType(int Type) {
76 assert(0 && "ELF doesn't support this directive");
79 virtual void EndCOFFSymbolDef() {
80 assert(0 && "ELF doesn't support this directive");
83 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
84 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
88 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
89 unsigned ByteAlignment);
91 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
92 unsigned Size = 0, unsigned ByteAlignment = 0) {
93 assert(0 && "ELF doesn't support this directive");
95 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
96 uint64_t Size, unsigned ByteAlignment = 0) {
97 assert(0 && "ELF doesn't support this directive");
99 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
100 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
101 unsigned ValueSize = 1,
102 unsigned MaxBytesToEmit = 0);
103 virtual void EmitCodeAlignment(unsigned ByteAlignment,
104 unsigned MaxBytesToEmit = 0);
106 virtual void EmitFileDirective(StringRef Filename);
108 virtual void FinishImpl();
111 virtual void EmitInstToFragment(const MCInst &Inst);
112 virtual void EmitInstToData(const MCInst &Inst);
114 void fixSymbolsInTLSFixups(const MCExpr *expr);
119 unsigned ByteAlignment;
121 std::vector<LocalCommon> LocalCommons;
123 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
125 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
127 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
130 void SetSectionData() {
131 SetSection(".data", ELF::SHT_PROGBITS,
132 ELF::SHF_WRITE |ELF::SHF_ALLOC,
133 SectionKind::getDataRel());
134 EmitCodeAlignment(4, 0);
136 void SetSectionText() {
137 SetSection(".text", ELF::SHT_PROGBITS,
139 ELF::SHF_ALLOC, SectionKind::getText());
140 EmitCodeAlignment(4, 0);
142 void SetSectionBss() {
143 SetSection(".bss", ELF::SHT_NOBITS,
145 ELF::SHF_ALLOC, SectionKind::getBSS());
146 EmitCodeAlignment(4, 0);
151 void MCELFStreamer::InitSections() {
152 // This emulates the same behavior of GNU as. This makes it easier
153 // to compare the output as the major sections are in the same order.
160 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
161 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
163 MCObjectStreamer::EmitLabel(Symbol);
165 const MCSectionELF &Section =
166 static_cast<const MCSectionELF&>(Symbol->getSection());
167 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
168 if (Section.getFlags() & ELF::SHF_TLS)
169 MCELF::SetType(SD, ELF::STT_TLS);
172 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
174 case MCAF_SyntaxUnified: return; // no-op here.
175 case MCAF_Code16: return; // Change parsing mode; no-op here.
176 case MCAF_Code32: return; // Change parsing mode; no-op here.
177 case MCAF_Code64: return; // Change parsing mode; no-op here.
178 case MCAF_SubsectionsViaSymbols:
179 getAssembler().setSubsectionsViaSymbols(true);
183 assert(0 && "invalid assembler flag!");
186 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
187 // FIXME: Anything needed here to flag the function as thumb?
189 getAssembler().setIsThumbFunc(Func);
191 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
192 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
195 void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
196 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
198 // FIXME: Lift context changes into super class.
199 getAssembler().getOrCreateSymbolData(*Symbol);
200 Symbol->setVariableValue(AddValueSymbols(Value));
203 void MCELFStreamer::ChangeSection(const MCSection *Section) {
204 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
206 getAssembler().getOrCreateSymbolData(*Grp);
207 this->MCObjectStreamer::ChangeSection(Section);
210 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
211 getAssembler().getOrCreateSymbolData(*Symbol);
212 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
213 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
214 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
215 Alias->setVariableValue(Value);
218 void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
219 MCSymbolAttr Attribute) {
220 // Indirect symbols are handled differently, to match how 'as' handles
221 // them. This makes writing matching .o files easier.
222 if (Attribute == MCSA_IndirectSymbol) {
223 // Note that we intentionally cannot use the symbol data here; this is
224 // important for matching the string table that 'as' generates.
225 IndirectSymbolData ISD;
227 ISD.SectionData = getCurrentSectionData();
228 getAssembler().getIndirectSymbols().push_back(ISD);
232 // Adding a symbol attribute always introduces the symbol, note that an
233 // important side effect of calling getOrCreateSymbolData here is to register
234 // the symbol with the assembler.
235 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
237 // The implementation of symbol attributes is designed to match 'as', but it
238 // leaves much to desired. It doesn't really make sense to arbitrarily add and
239 // remove flags, but 'as' allows this (in particular, see .desc).
241 // In the future it might be worth trying to make these operations more well
244 case MCSA_LazyReference:
246 case MCSA_NoDeadStrip:
247 case MCSA_SymbolResolver:
248 case MCSA_PrivateExtern:
249 case MCSA_WeakDefinition:
250 case MCSA_WeakDefAutoPrivate:
252 case MCSA_IndirectSymbol:
253 assert(0 && "Invalid symbol attribute for ELF!");
256 case MCSA_ELF_TypeGnuUniqueObject:
261 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
262 SD.setExternal(true);
263 BindingExplicitlySet.insert(Symbol);
266 case MCSA_WeakReference:
268 MCELF::SetBinding(SD, ELF::STB_WEAK);
269 SD.setExternal(true);
270 BindingExplicitlySet.insert(Symbol);
274 MCELF::SetBinding(SD, ELF::STB_LOCAL);
275 SD.setExternal(false);
276 BindingExplicitlySet.insert(Symbol);
279 case MCSA_ELF_TypeFunction:
280 MCELF::SetType(SD, ELF::STT_FUNC);
283 case MCSA_ELF_TypeIndFunction:
284 MCELF::SetType(SD, ELF::STT_GNU_IFUNC);
287 case MCSA_ELF_TypeObject:
288 MCELF::SetType(SD, ELF::STT_OBJECT);
291 case MCSA_ELF_TypeTLS:
292 MCELF::SetType(SD, ELF::STT_TLS);
295 case MCSA_ELF_TypeCommon:
296 MCELF::SetType(SD, ELF::STT_COMMON);
299 case MCSA_ELF_TypeNoType:
300 MCELF::SetType(SD, ELF::STT_NOTYPE);
304 MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
308 MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
312 MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
317 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
318 unsigned ByteAlignment) {
319 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
321 if (!BindingExplicitlySet.count(Symbol)) {
322 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
323 SD.setExternal(true);
326 MCELF::SetType(SD, ELF::STT_OBJECT);
328 if (MCELF::GetBinding(SD) == ELF_STB_Local) {
329 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
333 SectionKind::getBSS());
334 Symbol->setSection(*Section);
336 struct LocalCommon L = {&SD, Size, ByteAlignment};
337 LocalCommons.push_back(L);
339 SD.setCommon(Size, ByteAlignment);
342 SD.setSize(MCConstantExpr::Create(Size, getContext()));
345 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
346 unsigned ByteAlignment) {
347 // FIXME: Should this be caught and done earlier?
348 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
349 MCELF::SetBinding(SD, ELF::STB_LOCAL);
350 SD.setExternal(false);
351 BindingExplicitlySet.insert(Symbol);
352 EmitCommonSymbol(Symbol, Size, ByteAlignment);
355 void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
356 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
358 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
361 void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
362 int64_t Value, unsigned ValueSize,
363 unsigned MaxBytesToEmit) {
364 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
366 if (MaxBytesToEmit == 0)
367 MaxBytesToEmit = ByteAlignment;
368 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
369 getCurrentSectionData());
371 // Update the maximum alignment on the current section if necessary.
372 if (ByteAlignment > getCurrentSectionData()->getAlignment())
373 getCurrentSectionData()->setAlignment(ByteAlignment);
376 void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
377 unsigned MaxBytesToEmit) {
378 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
380 if (MaxBytesToEmit == 0)
381 MaxBytesToEmit = ByteAlignment;
382 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
383 getCurrentSectionData());
384 F->setEmitNops(true);
386 // Update the maximum alignment on the current section if necessary.
387 if (ByteAlignment > getCurrentSectionData()->getAlignment())
388 getCurrentSectionData()->setAlignment(ByteAlignment);
391 // Add a symbol for the file name of this module. This is the second
392 // entry in the module's symbol table (the first being the null symbol).
393 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
394 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
395 Symbol->setSection(*getCurrentSection());
396 Symbol->setAbsolute();
398 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
400 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
403 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
404 switch (expr->getKind()) {
405 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
406 case MCExpr::Constant:
409 case MCExpr::Binary: {
410 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
411 fixSymbolsInTLSFixups(be->getLHS());
412 fixSymbolsInTLSFixups(be->getRHS());
416 case MCExpr::SymbolRef: {
417 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
418 switch (symRef.getKind()) {
421 case MCSymbolRefExpr::VK_GOTTPOFF:
422 case MCSymbolRefExpr::VK_INDNTPOFF:
423 case MCSymbolRefExpr::VK_NTPOFF:
424 case MCSymbolRefExpr::VK_GOTNTPOFF:
425 case MCSymbolRefExpr::VK_TLSGD:
426 case MCSymbolRefExpr::VK_TLSLD:
427 case MCSymbolRefExpr::VK_TLSLDM:
428 case MCSymbolRefExpr::VK_TPOFF:
429 case MCSymbolRefExpr::VK_DTPOFF:
430 case MCSymbolRefExpr::VK_ARM_TLSGD:
431 case MCSymbolRefExpr::VK_ARM_TPOFF:
432 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
433 case MCSymbolRefExpr::VK_Mips_TLSGD:
434 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
435 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
436 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
439 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
440 MCELF::SetType(SD, ELF::STT_TLS);
445 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
450 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
451 this->MCObjectStreamer::EmitInstToFragment(Inst);
452 MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
454 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
455 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
458 void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
459 MCDataFragment *DF = getOrCreateDataFragment();
461 SmallVector<MCFixup, 4> Fixups;
462 SmallString<256> Code;
463 raw_svector_ostream VecOS(Code);
464 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
467 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
468 fixSymbolsInTLSFixups(Fixups[i].getValue());
470 // Add the fixups and data.
471 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
472 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
473 DF->addFixup(Fixups[i]);
475 DF->getContents().append(Code.begin(), Code.end());
478 void MCELFStreamer::FinishImpl() {
481 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
482 e = LocalCommons.end();
484 MCSymbolData *SD = i->SD;
485 uint64_t Size = i->Size;
486 unsigned ByteAlignment = i->ByteAlignment;
487 const MCSymbol &Symbol = SD->getSymbol();
488 const MCSection &Section = Symbol.getSection();
490 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
491 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
493 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
496 // Update the maximum alignment of the section if necessary.
497 if (ByteAlignment > SectData.getAlignment())
498 SectData.setAlignment(ByteAlignment);
501 this->MCObjectStreamer::FinishImpl();
504 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
505 raw_ostream &OS, MCCodeEmitter *CE,
506 bool RelaxAll, bool NoExecStack) {
507 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
509 S->getAssembler().setRelaxAll(true);
511 S->getAssembler().setNoExecStack(true);