Split MCELFStreamer into a header file.
[oota-llvm.git] / lib / MC / MCELFStreamer.cpp
1 //===- lib/MC/MCELFStreamer.cpp - ELF Object Output ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file assembles .s files and emits ELF .o object files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/MC/MCELFStreamer.h"
15
16 #include "MCELF.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"
36
37 using namespace llvm;
38
39
40 inline void MCELFStreamer::SetSection(StringRef Section, unsigned Type,
41                                       unsigned Flags, SectionKind Kind) {
42   SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
43 }
44
45 inline void MCELFStreamer::SetSectionData() {
46   SetSection(".data",
47              ELF::SHT_PROGBITS,
48              ELF::SHF_WRITE | ELF::SHF_ALLOC,
49              SectionKind::getDataRel());
50   EmitCodeAlignment(4, 0);
51 }
52
53 inline void MCELFStreamer::SetSectionText() {
54   SetSection(".text",
55              ELF::SHT_PROGBITS,
56              ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
57              SectionKind::getText());
58   EmitCodeAlignment(4, 0);
59 }
60
61 inline void MCELFStreamer::SetSectionBss() {
62   SetSection(".bss",
63              ELF::SHT_NOBITS,
64              ELF::SHF_WRITE | ELF::SHF_ALLOC,
65              SectionKind::getBSS());
66   EmitCodeAlignment(4, 0);
67 }
68
69 MCELFStreamer::~MCELFStreamer() {
70 }
71
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.
75   SetSectionText();
76   SetSectionData();
77   SetSectionBss();
78   SetSectionText();
79 }
80
81 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
82   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
83
84   MCObjectStreamer::EmitLabel(Symbol);
85
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);
91 }
92
93 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
94   switch (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);
101     return;
102   }
103
104   llvm_unreachable("invalid assembler flag!");
105 }
106
107 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
108   // FIXME: Anything needed here to flag the function as thumb?
109
110   getAssembler().setIsThumbFunc(Func);
111
112   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
113   SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
114 }
115
116 void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
117   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
118   // MCObjectStreamer.
119   // FIXME: Lift context changes into super class.
120   getAssembler().getOrCreateSymbolData(*Symbol);
121   Symbol->setVariableValue(AddValueSymbols(Value));
122 }
123
124 void MCELFStreamer::ChangeSection(const MCSection *Section) {
125   const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
126   if (Grp)
127     getAssembler().getOrCreateSymbolData(*Grp);
128   this->MCObjectStreamer::ChangeSection(Section);
129 }
130
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);
137 }
138
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;
147     ISD.Symbol = Symbol;
148     ISD.SectionData = getCurrentSectionData();
149     getAssembler().getIndirectSymbols().push_back(ISD);
150     return;
151   }
152
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);
157
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).
161   //
162   // In the future it might be worth trying to make these operations more well
163   // defined.
164   switch (Attribute) {
165   case MCSA_LazyReference:
166   case MCSA_Reference:
167   case MCSA_SymbolResolver:
168   case MCSA_PrivateExtern:
169   case MCSA_WeakDefinition:
170   case MCSA_WeakDefAutoPrivate:
171   case MCSA_Invalid:
172   case MCSA_IndirectSymbol:
173     llvm_unreachable("Invalid symbol attribute for ELF!");
174
175   case MCSA_NoDeadStrip:
176   case MCSA_ELF_TypeGnuUniqueObject:
177     // Ignore for now.
178     break;
179
180   case MCSA_Global:
181     MCELF::SetBinding(SD, ELF::STB_GLOBAL);
182     SD.setExternal(true);
183     BindingExplicitlySet.insert(Symbol);
184     break;
185
186   case MCSA_WeakReference:
187   case MCSA_Weak:
188     MCELF::SetBinding(SD, ELF::STB_WEAK);
189     SD.setExternal(true);
190     BindingExplicitlySet.insert(Symbol);
191     break;
192
193   case MCSA_Local:
194     MCELF::SetBinding(SD, ELF::STB_LOCAL);
195     SD.setExternal(false);
196     BindingExplicitlySet.insert(Symbol);
197     break;
198
199   case MCSA_ELF_TypeFunction:
200     MCELF::SetType(SD, ELF::STT_FUNC);
201     break;
202
203   case MCSA_ELF_TypeIndFunction:
204     MCELF::SetType(SD, ELF::STT_GNU_IFUNC);
205     break;
206
207   case MCSA_ELF_TypeObject:
208     MCELF::SetType(SD, ELF::STT_OBJECT);
209     break;
210
211   case MCSA_ELF_TypeTLS:
212     MCELF::SetType(SD, ELF::STT_TLS);
213     break;
214
215   case MCSA_ELF_TypeCommon:
216     MCELF::SetType(SD, ELF::STT_COMMON);
217     break;
218
219   case MCSA_ELF_TypeNoType:
220     MCELF::SetType(SD, ELF::STT_NOTYPE);
221     break;
222
223   case MCSA_Protected:
224     MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
225     break;
226
227   case MCSA_Hidden:
228     MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
229     break;
230
231   case MCSA_Internal:
232     MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
233     break;
234   }
235 }
236
237 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
238                                        unsigned ByteAlignment) {
239   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
240
241   if (!BindingExplicitlySet.count(Symbol)) {
242     MCELF::SetBinding(SD, ELF::STB_GLOBAL);
243     SD.setExternal(true);
244   }
245
246   MCELF::SetType(SD, ELF::STT_OBJECT);
247
248   if (MCELF::GetBinding(SD) == ELF_STB_Local) {
249     const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
250                                                          ELF::SHT_NOBITS,
251                                                          ELF::SHF_WRITE |
252                                                          ELF::SHF_ALLOC,
253                                                          SectionKind::getBSS());
254     Symbol->setSection(*Section);
255
256     struct LocalCommon L = {&SD, Size, ByteAlignment};
257     LocalCommons.push_back(L);
258   } else {
259     SD.setCommon(Size, ByteAlignment);
260   }
261
262   SD.setSize(MCConstantExpr::Create(Size, getContext()));
263 }
264
265 void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
266   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
267   SD.setSize(Value);
268 }
269
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);
278 }
279
280 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
281                                   unsigned AddrSpace) {
282   fixSymbolsInTLSFixups(Value);
283   MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace);
284 }
285
286
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();
293
294   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
295
296   SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
297 }
298
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:
303     break;
304
305   case MCExpr::Binary: {
306     const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
307     fixSymbolsInTLSFixups(be->getLHS());
308     fixSymbolsInTLSFixups(be->getRHS());
309     break;
310   }
311
312   case MCExpr::SymbolRef: {
313     const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
314     switch (symRef.getKind()) {
315     default:
316       return;
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:
333       break;
334     }
335     MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
336     MCELF::SetType(SD, ELF::STT_TLS);
337     break;
338   }
339
340   case MCExpr::Unary:
341     fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
342     break;
343   }
344 }
345
346 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
347   this->MCObjectStreamer::EmitInstToFragment(Inst);
348   MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
349
350   for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
351     fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
352 }
353
354 void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
355   MCDataFragment *DF = getOrCreateDataFragment();
356
357   SmallVector<MCFixup, 4> Fixups;
358   SmallString<256> Code;
359   raw_svector_ostream VecOS(Code);
360   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
361   VecOS.flush();
362
363   for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
364     fixSymbolsInTLSFixups(Fixups[i].getValue());
365
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]);
370   }
371   DF->getContents().append(Code.begin(), Code.end());
372 }
373
374 void MCELFStreamer::FinishImpl() {
375   EmitFrames(true);
376
377   for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
378                                                 e = LocalCommons.end();
379        i != e; ++i) {
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();
385
386     MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
387     new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
388
389     MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
390     SD->setFragment(F);
391
392     // Update the maximum alignment of the section if necessary.
393     if (ByteAlignment > SectData.getAlignment())
394       SectData.setAlignment(ByteAlignment);
395   }
396
397   this->MCObjectStreamer::FinishImpl();
398 }
399
400 void MCELFStreamer::EmitTCEntry(const MCSymbol &S)
401 {
402   // Creates a R_PPC64_TOC relocation
403   MCObjectStreamer::EmitSymbolValue(&S, 8, 0);
404 }
405
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);
410   if (RelaxAll)
411     S->getAssembler().setRelaxAll(true);
412   if (NoExecStack)
413     S->getAssembler().setNoExecStack(true);
414   return S;
415 }
416
417 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
418   llvm_unreachable("ELF doesn't support this directive");
419 }
420
421 void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
422   llvm_unreachable("ELF doesn't support this directive");
423 }
424
425 void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
426   llvm_unreachable("ELF doesn't support this directive");
427 }
428
429 void MCELFStreamer::EmitCOFFSymbolType(int Type) {
430   llvm_unreachable("ELF doesn't support this directive");
431 }
432
433 void MCELFStreamer::EndCOFFSymbolDef() {
434   llvm_unreachable("ELF doesn't support this directive");
435 }
436
437 void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
438                                  uint64_t Size, unsigned ByteAlignment) {
439   llvm_unreachable("ELF doesn't support this directive");
440 }
441
442 void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
443                                    uint64_t Size, unsigned ByteAlignment) {
444   llvm_unreachable("ELF doesn't support this directive");
445 }