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 //===----------------------------------------------------------------------===//
14 #include "llvm/MC/MCELFStreamer.h"
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/MCELF.h"
20 #include "llvm/MC/MCELFSymbolFlags.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCObjectStreamer.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCSectionELF.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCValue.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ELF.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
36 inline void MCELFStreamer::SetSection(StringRef Section, unsigned Type,
37 unsigned Flags, SectionKind Kind) {
38 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
41 inline void MCELFStreamer::SetSectionData() {
44 ELF::SHF_WRITE | ELF::SHF_ALLOC,
45 SectionKind::getDataRel());
46 EmitCodeAlignment(4, 0);
49 inline void MCELFStreamer::SetSectionText() {
52 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
53 SectionKind::getText());
54 EmitCodeAlignment(4, 0);
57 inline void MCELFStreamer::SetSectionBss() {
60 ELF::SHF_WRITE | ELF::SHF_ALLOC,
61 SectionKind::getBSS());
62 EmitCodeAlignment(4, 0);
65 MCELFStreamer::~MCELFStreamer() {
68 void MCELFStreamer::InitSections() {
69 // This emulates the same behavior of GNU as. This makes it easier
70 // to compare the output as the major sections are in the same order.
77 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
78 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
80 MCObjectStreamer::EmitLabel(Symbol);
82 const MCSectionELF &Section =
83 static_cast<const MCSectionELF&>(Symbol->getSection());
84 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
85 if (Section.getFlags() & ELF::SHF_TLS)
86 MCELF::SetType(SD, ELF::STT_TLS);
89 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
91 case MCAF_SyntaxUnified: return; // no-op here.
92 case MCAF_Code16: return; // Change parsing mode; no-op here.
93 case MCAF_Code32: return; // Change parsing mode; no-op here.
94 case MCAF_Code64: return; // Change parsing mode; no-op here.
95 case MCAF_SubsectionsViaSymbols:
96 getAssembler().setSubsectionsViaSymbols(true);
100 llvm_unreachable("invalid assembler flag!");
103 void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
104 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
106 // FIXME: Lift context changes into super class.
107 getAssembler().getOrCreateSymbolData(*Symbol);
108 Symbol->setVariableValue(AddValueSymbols(Value));
111 void MCELFStreamer::ChangeSection(const MCSection *Section) {
112 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
114 getAssembler().getOrCreateSymbolData(*Grp);
115 this->MCObjectStreamer::ChangeSection(Section);
118 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
119 getAssembler().getOrCreateSymbolData(*Symbol);
120 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
121 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
122 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
123 Alias->setVariableValue(Value);
126 void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
127 MCSymbolAttr Attribute) {
128 // Indirect symbols are handled differently, to match how 'as' handles
129 // them. This makes writing matching .o files easier.
130 if (Attribute == MCSA_IndirectSymbol) {
131 // Note that we intentionally cannot use the symbol data here; this is
132 // important for matching the string table that 'as' generates.
133 IndirectSymbolData ISD;
135 ISD.SectionData = getCurrentSectionData();
136 getAssembler().getIndirectSymbols().push_back(ISD);
140 // Adding a symbol attribute always introduces the symbol, note that an
141 // important side effect of calling getOrCreateSymbolData here is to register
142 // the symbol with the assembler.
143 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
145 // The implementation of symbol attributes is designed to match 'as', but it
146 // leaves much to desired. It doesn't really make sense to arbitrarily add and
147 // remove flags, but 'as' allows this (in particular, see .desc).
149 // In the future it might be worth trying to make these operations more well
152 case MCSA_LazyReference:
154 case MCSA_SymbolResolver:
155 case MCSA_PrivateExtern:
156 case MCSA_WeakDefinition:
157 case MCSA_WeakDefAutoPrivate:
159 case MCSA_IndirectSymbol:
160 llvm_unreachable("Invalid symbol attribute for ELF!");
162 case MCSA_NoDeadStrip:
163 case MCSA_ELF_TypeGnuUniqueObject:
168 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
169 SD.setExternal(true);
170 BindingExplicitlySet.insert(Symbol);
173 case MCSA_WeakReference:
175 MCELF::SetBinding(SD, ELF::STB_WEAK);
176 SD.setExternal(true);
177 BindingExplicitlySet.insert(Symbol);
181 MCELF::SetBinding(SD, ELF::STB_LOCAL);
182 SD.setExternal(false);
183 BindingExplicitlySet.insert(Symbol);
186 case MCSA_ELF_TypeFunction:
187 MCELF::SetType(SD, ELF::STT_FUNC);
190 case MCSA_ELF_TypeIndFunction:
191 MCELF::SetType(SD, ELF::STT_GNU_IFUNC);
194 case MCSA_ELF_TypeObject:
195 MCELF::SetType(SD, ELF::STT_OBJECT);
198 case MCSA_ELF_TypeTLS:
199 MCELF::SetType(SD, ELF::STT_TLS);
202 case MCSA_ELF_TypeCommon:
203 MCELF::SetType(SD, ELF::STT_COMMON);
206 case MCSA_ELF_TypeNoType:
207 MCELF::SetType(SD, ELF::STT_NOTYPE);
211 MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
215 MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
219 MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
224 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
225 unsigned ByteAlignment) {
226 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
228 if (!BindingExplicitlySet.count(Symbol)) {
229 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
230 SD.setExternal(true);
233 MCELF::SetType(SD, ELF::STT_OBJECT);
235 if (MCELF::GetBinding(SD) == ELF_STB_Local) {
236 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
240 SectionKind::getBSS());
241 Symbol->setSection(*Section);
243 struct LocalCommon L = {&SD, Size, ByteAlignment};
244 LocalCommons.push_back(L);
246 SD.setCommon(Size, ByteAlignment);
249 SD.setSize(MCConstantExpr::Create(Size, getContext()));
252 void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
253 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
257 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
258 unsigned ByteAlignment) {
259 // FIXME: Should this be caught and done earlier?
260 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
261 MCELF::SetBinding(SD, ELF::STB_LOCAL);
262 SD.setExternal(false);
263 BindingExplicitlySet.insert(Symbol);
264 EmitCommonSymbol(Symbol, Size, ByteAlignment);
267 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
268 unsigned AddrSpace) {
269 fixSymbolsInTLSFixups(Value);
270 MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace);
274 // Add a symbol for the file name of this module. This is the second
275 // entry in the module's symbol table (the first being the null symbol).
276 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
277 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
278 Symbol->setSection(*getCurrentSection());
279 Symbol->setAbsolute();
281 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
283 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
286 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
287 switch (expr->getKind()) {
288 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
289 case MCExpr::Constant:
292 case MCExpr::Binary: {
293 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
294 fixSymbolsInTLSFixups(be->getLHS());
295 fixSymbolsInTLSFixups(be->getRHS());
299 case MCExpr::SymbolRef: {
300 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
301 switch (symRef.getKind()) {
304 case MCSymbolRefExpr::VK_GOTTPOFF:
305 case MCSymbolRefExpr::VK_INDNTPOFF:
306 case MCSymbolRefExpr::VK_NTPOFF:
307 case MCSymbolRefExpr::VK_GOTNTPOFF:
308 case MCSymbolRefExpr::VK_TLSGD:
309 case MCSymbolRefExpr::VK_TLSLD:
310 case MCSymbolRefExpr::VK_TLSLDM:
311 case MCSymbolRefExpr::VK_TPOFF:
312 case MCSymbolRefExpr::VK_DTPOFF:
313 case MCSymbolRefExpr::VK_ARM_TLSGD:
314 case MCSymbolRefExpr::VK_ARM_TPOFF:
315 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
316 case MCSymbolRefExpr::VK_Mips_TLSGD:
317 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
318 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
319 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
322 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
323 MCELF::SetType(SD, ELF::STT_TLS);
328 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
333 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
334 this->MCObjectStreamer::EmitInstToFragment(Inst);
335 MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
337 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
338 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
341 void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
342 MCDataFragment *DF = getOrCreateDataFragment();
344 SmallVector<MCFixup, 4> Fixups;
345 SmallString<256> Code;
346 raw_svector_ostream VecOS(Code);
347 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
350 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
351 fixSymbolsInTLSFixups(Fixups[i].getValue());
353 // Add the fixups and data.
354 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
355 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
356 DF->addFixup(Fixups[i]);
358 DF->getContents().append(Code.begin(), Code.end());
361 void MCELFStreamer::FinishImpl() {
364 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
365 e = LocalCommons.end();
367 MCSymbolData *SD = i->SD;
368 uint64_t Size = i->Size;
369 unsigned ByteAlignment = i->ByteAlignment;
370 const MCSymbol &Symbol = SD->getSymbol();
371 const MCSection &Section = Symbol.getSection();
373 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
374 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
376 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
379 // Update the maximum alignment of the section if necessary.
380 if (ByteAlignment > SectData.getAlignment())
381 SectData.setAlignment(ByteAlignment);
384 this->MCObjectStreamer::FinishImpl();
386 void MCELFStreamer::EmitTCEntry(const MCSymbol &S) {
387 // Creates a R_PPC64_TOC relocation
388 MCObjectStreamer::EmitSymbolValue(&S, 8, 0);
391 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
392 raw_ostream &OS, MCCodeEmitter *CE,
393 bool RelaxAll, bool NoExecStack) {
394 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
396 S->getAssembler().setRelaxAll(true);
398 S->getAssembler().setNoExecStack(true);
402 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
403 llvm_unreachable("Generic ELF doesn't support this directive");
406 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
407 llvm_unreachable("ELF doesn't support this directive");
410 void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
411 llvm_unreachable("ELF doesn't support this directive");
414 void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
415 llvm_unreachable("ELF doesn't support this directive");
418 void MCELFStreamer::EmitCOFFSymbolType(int Type) {
419 llvm_unreachable("ELF doesn't support this directive");
422 void MCELFStreamer::EndCOFFSymbolDef() {
423 llvm_unreachable("ELF doesn't support this directive");
426 void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
427 uint64_t Size, unsigned ByteAlignment) {
428 llvm_unreachable("ELF doesn't support this directive");
431 void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
432 uint64_t Size, unsigned ByteAlignment) {
433 llvm_unreachable("ELF doesn't support this directive");