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"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/MC/MCAsmBackend.h"
21 #include "llvm/MC/MCAssembler.h"
22 #include "llvm/MC/MCCodeEmitter.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCELFSymbolFlags.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCObjectStreamer.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCSectionELF.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/raw_ostream.h"
40 inline void MCELFStreamer::SetSection(StringRef Section, unsigned Type,
41 unsigned Flags, SectionKind Kind) {
42 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
45 inline void MCELFStreamer::SetSectionData() {
48 ELF::SHF_WRITE | ELF::SHF_ALLOC,
49 SectionKind::getDataRel());
50 EmitCodeAlignment(4, 0);
53 inline void MCELFStreamer::SetSectionText() {
56 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
57 SectionKind::getText());
58 EmitCodeAlignment(4, 0);
61 inline void MCELFStreamer::SetSectionBss() {
64 ELF::SHF_WRITE | ELF::SHF_ALLOC,
65 SectionKind::getBSS());
66 EmitCodeAlignment(4, 0);
69 MCELFStreamer::~MCELFStreamer() {
72 void MCELFStreamer::InitSections() {
73 // This emulates the same behavior of GNU as. This makes it easier
74 // to compare the output as the major sections are in the same order.
81 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
82 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
84 MCObjectStreamer::EmitLabel(Symbol);
86 const MCSectionELF &Section =
87 static_cast<const MCSectionELF&>(Symbol->getSection());
88 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
89 if (Section.getFlags() & ELF::SHF_TLS)
90 MCELF::SetType(SD, ELF::STT_TLS);
93 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
95 case MCAF_SyntaxUnified: return; // no-op here.
96 case MCAF_Code16: return; // Change parsing mode; no-op here.
97 case MCAF_Code32: return; // Change parsing mode; no-op here.
98 case MCAF_Code64: return; // Change parsing mode; no-op here.
99 case MCAF_SubsectionsViaSymbols:
100 getAssembler().setSubsectionsViaSymbols(true);
104 llvm_unreachable("invalid assembler flag!");
107 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
108 // FIXME: Anything needed here to flag the function as thumb?
110 getAssembler().setIsThumbFunc(Func);
112 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
113 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
116 void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
117 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
119 // FIXME: Lift context changes into super class.
120 getAssembler().getOrCreateSymbolData(*Symbol);
121 Symbol->setVariableValue(AddValueSymbols(Value));
124 void MCELFStreamer::ChangeSection(const MCSection *Section) {
125 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
127 getAssembler().getOrCreateSymbolData(*Grp);
128 this->MCObjectStreamer::ChangeSection(Section);
131 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
132 getAssembler().getOrCreateSymbolData(*Symbol);
133 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
134 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
135 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
136 Alias->setVariableValue(Value);
139 void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
140 MCSymbolAttr Attribute) {
141 // Indirect symbols are handled differently, to match how 'as' handles
142 // them. This makes writing matching .o files easier.
143 if (Attribute == MCSA_IndirectSymbol) {
144 // Note that we intentionally cannot use the symbol data here; this is
145 // important for matching the string table that 'as' generates.
146 IndirectSymbolData ISD;
148 ISD.SectionData = getCurrentSectionData();
149 getAssembler().getIndirectSymbols().push_back(ISD);
153 // Adding a symbol attribute always introduces the symbol, note that an
154 // important side effect of calling getOrCreateSymbolData here is to register
155 // the symbol with the assembler.
156 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
158 // The implementation of symbol attributes is designed to match 'as', but it
159 // leaves much to desired. It doesn't really make sense to arbitrarily add and
160 // remove flags, but 'as' allows this (in particular, see .desc).
162 // In the future it might be worth trying to make these operations more well
165 case MCSA_LazyReference:
167 case MCSA_SymbolResolver:
168 case MCSA_PrivateExtern:
169 case MCSA_WeakDefinition:
170 case MCSA_WeakDefAutoPrivate:
172 case MCSA_IndirectSymbol:
173 llvm_unreachable("Invalid symbol attribute for ELF!");
175 case MCSA_NoDeadStrip:
176 case MCSA_ELF_TypeGnuUniqueObject:
181 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
182 SD.setExternal(true);
183 BindingExplicitlySet.insert(Symbol);
186 case MCSA_WeakReference:
188 MCELF::SetBinding(SD, ELF::STB_WEAK);
189 SD.setExternal(true);
190 BindingExplicitlySet.insert(Symbol);
194 MCELF::SetBinding(SD, ELF::STB_LOCAL);
195 SD.setExternal(false);
196 BindingExplicitlySet.insert(Symbol);
199 case MCSA_ELF_TypeFunction:
200 MCELF::SetType(SD, ELF::STT_FUNC);
203 case MCSA_ELF_TypeIndFunction:
204 MCELF::SetType(SD, ELF::STT_GNU_IFUNC);
207 case MCSA_ELF_TypeObject:
208 MCELF::SetType(SD, ELF::STT_OBJECT);
211 case MCSA_ELF_TypeTLS:
212 MCELF::SetType(SD, ELF::STT_TLS);
215 case MCSA_ELF_TypeCommon:
216 MCELF::SetType(SD, ELF::STT_COMMON);
219 case MCSA_ELF_TypeNoType:
220 MCELF::SetType(SD, ELF::STT_NOTYPE);
224 MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
228 MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
232 MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
237 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
238 unsigned ByteAlignment) {
239 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
241 if (!BindingExplicitlySet.count(Symbol)) {
242 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
243 SD.setExternal(true);
246 MCELF::SetType(SD, ELF::STT_OBJECT);
248 if (MCELF::GetBinding(SD) == ELF_STB_Local) {
249 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
253 SectionKind::getBSS());
254 Symbol->setSection(*Section);
256 struct LocalCommon L = {&SD, Size, ByteAlignment};
257 LocalCommons.push_back(L);
259 SD.setCommon(Size, ByteAlignment);
262 SD.setSize(MCConstantExpr::Create(Size, getContext()));
265 void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
266 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
270 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
271 unsigned ByteAlignment) {
272 // FIXME: Should this be caught and done earlier?
273 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
274 MCELF::SetBinding(SD, ELF::STB_LOCAL);
275 SD.setExternal(false);
276 BindingExplicitlySet.insert(Symbol);
277 EmitCommonSymbol(Symbol, Size, ByteAlignment);
280 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
281 unsigned AddrSpace) {
282 fixSymbolsInTLSFixups(Value);
283 MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace);
287 // Add a symbol for the file name of this module. This is the second
288 // entry in the module's symbol table (the first being the null symbol).
289 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
290 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
291 Symbol->setSection(*getCurrentSection());
292 Symbol->setAbsolute();
294 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
296 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
299 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
300 switch (expr->getKind()) {
301 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
302 case MCExpr::Constant:
305 case MCExpr::Binary: {
306 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
307 fixSymbolsInTLSFixups(be->getLHS());
308 fixSymbolsInTLSFixups(be->getRHS());
312 case MCExpr::SymbolRef: {
313 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
314 switch (symRef.getKind()) {
317 case MCSymbolRefExpr::VK_GOTTPOFF:
318 case MCSymbolRefExpr::VK_INDNTPOFF:
319 case MCSymbolRefExpr::VK_NTPOFF:
320 case MCSymbolRefExpr::VK_GOTNTPOFF:
321 case MCSymbolRefExpr::VK_TLSGD:
322 case MCSymbolRefExpr::VK_TLSLD:
323 case MCSymbolRefExpr::VK_TLSLDM:
324 case MCSymbolRefExpr::VK_TPOFF:
325 case MCSymbolRefExpr::VK_DTPOFF:
326 case MCSymbolRefExpr::VK_ARM_TLSGD:
327 case MCSymbolRefExpr::VK_ARM_TPOFF:
328 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
329 case MCSymbolRefExpr::VK_Mips_TLSGD:
330 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
331 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
332 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
335 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
336 MCELF::SetType(SD, ELF::STT_TLS);
341 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
346 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
347 this->MCObjectStreamer::EmitInstToFragment(Inst);
348 MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
350 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
351 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
354 void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
355 MCDataFragment *DF = getOrCreateDataFragment();
357 SmallVector<MCFixup, 4> Fixups;
358 SmallString<256> Code;
359 raw_svector_ostream VecOS(Code);
360 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
363 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
364 fixSymbolsInTLSFixups(Fixups[i].getValue());
366 // Add the fixups and data.
367 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
368 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
369 DF->addFixup(Fixups[i]);
371 DF->getContents().append(Code.begin(), Code.end());
374 void MCELFStreamer::FinishImpl() {
377 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
378 e = LocalCommons.end();
380 MCSymbolData *SD = i->SD;
381 uint64_t Size = i->Size;
382 unsigned ByteAlignment = i->ByteAlignment;
383 const MCSymbol &Symbol = SD->getSymbol();
384 const MCSection &Section = Symbol.getSection();
386 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
387 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
389 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
392 // Update the maximum alignment of the section if necessary.
393 if (ByteAlignment > SectData.getAlignment())
394 SectData.setAlignment(ByteAlignment);
397 this->MCObjectStreamer::FinishImpl();
400 void MCELFStreamer::EmitTCEntry(const MCSymbol &S)
402 // Creates a R_PPC64_TOC relocation
403 MCObjectStreamer::EmitSymbolValue(&S, 8, 0);
406 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
407 raw_ostream &OS, MCCodeEmitter *CE,
408 bool RelaxAll, bool NoExecStack) {
409 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
411 S->getAssembler().setRelaxAll(true);
413 S->getAssembler().setNoExecStack(true);
417 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
418 llvm_unreachable("ELF doesn't support this directive");
421 void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
422 llvm_unreachable("ELF doesn't support this directive");
425 void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
426 llvm_unreachable("ELF doesn't support this directive");
429 void MCELFStreamer::EmitCOFFSymbolType(int Type) {
430 llvm_unreachable("ELF doesn't support this directive");
433 void MCELFStreamer::EndCOFFSymbolDef() {
434 llvm_unreachable("ELF doesn't support this directive");
437 void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
438 uint64_t Size, unsigned ByteAlignment) {
439 llvm_unreachable("ELF doesn't support this directive");
442 void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
443 uint64_t Size, unsigned ByteAlignment) {
444 llvm_unreachable("ELF doesn't support this directive");