8b9bdb14a073b2d332a6439eb3beaba352d32c0f
[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 #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"
32
33 using namespace llvm;
34
35
36 inline void MCELFStreamer::SetSection(StringRef Section, unsigned Type,
37                                       unsigned Flags, SectionKind Kind) {
38   SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
39 }
40
41 inline void MCELFStreamer::SetSectionData() {
42   SetSection(".data",
43              ELF::SHT_PROGBITS,
44              ELF::SHF_WRITE | ELF::SHF_ALLOC,
45              SectionKind::getDataRel());
46   EmitCodeAlignment(4, 0);
47 }
48
49 inline void MCELFStreamer::SetSectionText() {
50   SetSection(".text",
51              ELF::SHT_PROGBITS,
52              ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
53              SectionKind::getText());
54   EmitCodeAlignment(4, 0);
55 }
56
57 inline void MCELFStreamer::SetSectionBss() {
58   SetSection(".bss",
59              ELF::SHT_NOBITS,
60              ELF::SHF_WRITE | ELF::SHF_ALLOC,
61              SectionKind::getBSS());
62   EmitCodeAlignment(4, 0);
63 }
64
65 MCELFStreamer::~MCELFStreamer() {
66 }
67
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.
71   SetSectionText();
72   SetSectionData();
73   SetSectionBss();
74   SetSectionText();
75 }
76
77 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
78   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
79
80   MCObjectStreamer::EmitLabel(Symbol);
81
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);
87 }
88
89 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
90   switch (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);
97     return;
98   }
99
100   llvm_unreachable("invalid assembler flag!");
101 }
102
103 void MCELFStreamer::ChangeSection(const MCSection *Section) {
104   const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
105   if (Grp)
106     getAssembler().getOrCreateSymbolData(*Grp);
107   this->MCObjectStreamer::ChangeSection(Section);
108 }
109
110 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
111   getAssembler().getOrCreateSymbolData(*Symbol);
112   MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
113   AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
114   const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
115   Alias->setVariableValue(Value);
116 }
117
118 void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
119                                           MCSymbolAttr Attribute) {
120   // Indirect symbols are handled differently, to match how 'as' handles
121   // them. This makes writing matching .o files easier.
122   if (Attribute == MCSA_IndirectSymbol) {
123     // Note that we intentionally cannot use the symbol data here; this is
124     // important for matching the string table that 'as' generates.
125     IndirectSymbolData ISD;
126     ISD.Symbol = Symbol;
127     ISD.SectionData = getCurrentSectionData();
128     getAssembler().getIndirectSymbols().push_back(ISD);
129     return;
130   }
131
132   // Adding a symbol attribute always introduces the symbol, note that an
133   // important side effect of calling getOrCreateSymbolData here is to register
134   // the symbol with the assembler.
135   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
136
137   // The implementation of symbol attributes is designed to match 'as', but it
138   // leaves much to desired. It doesn't really make sense to arbitrarily add and
139   // remove flags, but 'as' allows this (in particular, see .desc).
140   //
141   // In the future it might be worth trying to make these operations more well
142   // defined.
143   switch (Attribute) {
144   case MCSA_LazyReference:
145   case MCSA_Reference:
146   case MCSA_SymbolResolver:
147   case MCSA_PrivateExtern:
148   case MCSA_WeakDefinition:
149   case MCSA_WeakDefAutoPrivate:
150   case MCSA_Invalid:
151   case MCSA_IndirectSymbol:
152     llvm_unreachable("Invalid symbol attribute for ELF!");
153
154   case MCSA_NoDeadStrip:
155   case MCSA_ELF_TypeGnuUniqueObject:
156     // Ignore for now.
157     break;
158
159   case MCSA_Global:
160     MCELF::SetBinding(SD, ELF::STB_GLOBAL);
161     SD.setExternal(true);
162     BindingExplicitlySet.insert(Symbol);
163     break;
164
165   case MCSA_WeakReference:
166   case MCSA_Weak:
167     MCELF::SetBinding(SD, ELF::STB_WEAK);
168     SD.setExternal(true);
169     BindingExplicitlySet.insert(Symbol);
170     break;
171
172   case MCSA_Local:
173     MCELF::SetBinding(SD, ELF::STB_LOCAL);
174     SD.setExternal(false);
175     BindingExplicitlySet.insert(Symbol);
176     break;
177
178   case MCSA_ELF_TypeFunction:
179     MCELF::SetType(SD, ELF::STT_FUNC);
180     break;
181
182   case MCSA_ELF_TypeIndFunction:
183     MCELF::SetType(SD, ELF::STT_GNU_IFUNC);
184     break;
185
186   case MCSA_ELF_TypeObject:
187     MCELF::SetType(SD, ELF::STT_OBJECT);
188     break;
189
190   case MCSA_ELF_TypeTLS:
191     MCELF::SetType(SD, ELF::STT_TLS);
192     break;
193
194   case MCSA_ELF_TypeCommon:
195     MCELF::SetType(SD, ELF::STT_COMMON);
196     break;
197
198   case MCSA_ELF_TypeNoType:
199     MCELF::SetType(SD, ELF::STT_NOTYPE);
200     break;
201
202   case MCSA_Protected:
203     MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
204     break;
205
206   case MCSA_Hidden:
207     MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
208     break;
209
210   case MCSA_Internal:
211     MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
212     break;
213   }
214 }
215
216 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
217                                        unsigned ByteAlignment) {
218   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
219
220   if (!BindingExplicitlySet.count(Symbol)) {
221     MCELF::SetBinding(SD, ELF::STB_GLOBAL);
222     SD.setExternal(true);
223   }
224
225   MCELF::SetType(SD, ELF::STT_OBJECT);
226
227   if (MCELF::GetBinding(SD) == ELF_STB_Local) {
228     const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
229                                                          ELF::SHT_NOBITS,
230                                                          ELF::SHF_WRITE |
231                                                          ELF::SHF_ALLOC,
232                                                          SectionKind::getBSS());
233     Symbol->setSection(*Section);
234
235     struct LocalCommon L = {&SD, Size, ByteAlignment};
236     LocalCommons.push_back(L);
237   } else {
238     SD.setCommon(Size, ByteAlignment);
239   }
240
241   SD.setSize(MCConstantExpr::Create(Size, getContext()));
242 }
243
244 void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
245   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
246   SD.setSize(Value);
247 }
248
249 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
250                                           unsigned ByteAlignment) {
251   // FIXME: Should this be caught and done earlier?
252   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
253   MCELF::SetBinding(SD, ELF::STB_LOCAL);
254   SD.setExternal(false);
255   BindingExplicitlySet.insert(Symbol);
256   EmitCommonSymbol(Symbol, Size, ByteAlignment);
257 }
258
259 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
260                                   unsigned AddrSpace) {
261   fixSymbolsInTLSFixups(Value);
262   MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace);
263 }
264
265
266 // Add a symbol for the file name of this module. This is the second
267 // entry in the module's symbol table (the first being the null symbol).
268 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
269   MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
270   Symbol->setSection(*getCurrentSection());
271   Symbol->setAbsolute();
272
273   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
274
275   SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
276 }
277
278 void  MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
279   switch (expr->getKind()) {
280   case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
281   case MCExpr::Constant:
282     break;
283
284   case MCExpr::Binary: {
285     const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
286     fixSymbolsInTLSFixups(be->getLHS());
287     fixSymbolsInTLSFixups(be->getRHS());
288     break;
289   }
290
291   case MCExpr::SymbolRef: {
292     const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
293     switch (symRef.getKind()) {
294     default:
295       return;
296     case MCSymbolRefExpr::VK_GOTTPOFF:
297     case MCSymbolRefExpr::VK_INDNTPOFF:
298     case MCSymbolRefExpr::VK_NTPOFF:
299     case MCSymbolRefExpr::VK_GOTNTPOFF:
300     case MCSymbolRefExpr::VK_TLSGD:
301     case MCSymbolRefExpr::VK_TLSLD:
302     case MCSymbolRefExpr::VK_TLSLDM:
303     case MCSymbolRefExpr::VK_TPOFF:
304     case MCSymbolRefExpr::VK_DTPOFF:
305     case MCSymbolRefExpr::VK_ARM_TLSGD:
306     case MCSymbolRefExpr::VK_ARM_TPOFF:
307     case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
308     case MCSymbolRefExpr::VK_Mips_TLSGD:
309     case MCSymbolRefExpr::VK_Mips_GOTTPREL:
310     case MCSymbolRefExpr::VK_Mips_TPREL_HI:
311     case MCSymbolRefExpr::VK_Mips_TPREL_LO:
312       break;
313     }
314     MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
315     MCELF::SetType(SD, ELF::STT_TLS);
316     break;
317   }
318
319   case MCExpr::Unary:
320     fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
321     break;
322   }
323 }
324
325 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
326   this->MCObjectStreamer::EmitInstToFragment(Inst);
327   MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
328
329   for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
330     fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
331 }
332
333 void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
334   MCDataFragment *DF = getOrCreateDataFragment();
335
336   SmallVector<MCFixup, 4> Fixups;
337   SmallString<256> Code;
338   raw_svector_ostream VecOS(Code);
339   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
340   VecOS.flush();
341
342   for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
343     fixSymbolsInTLSFixups(Fixups[i].getValue());
344
345   // Add the fixups and data.
346   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
347     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
348     DF->getFixups().push_back(Fixups[i]);
349   }
350   DF->getContents().append(Code.begin(), Code.end());
351 }
352
353 void MCELFStreamer::FinishImpl() {
354   EmitFrames(true);
355
356   for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
357                                                 e = LocalCommons.end();
358        i != e; ++i) {
359     MCSymbolData *SD = i->SD;
360     uint64_t Size = i->Size;
361     unsigned ByteAlignment = i->ByteAlignment;
362     const MCSymbol &Symbol = SD->getSymbol();
363     const MCSection &Section = Symbol.getSection();
364
365     MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
366     new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
367
368     MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
369     SD->setFragment(F);
370
371     // Update the maximum alignment of the section if necessary.
372     if (ByteAlignment > SectData.getAlignment())
373       SectData.setAlignment(ByteAlignment);
374   }
375
376   this->MCObjectStreamer::FinishImpl();
377 }
378 void MCELFStreamer::EmitTCEntry(const MCSymbol &S) {
379   // Creates a R_PPC64_TOC relocation
380   MCObjectStreamer::EmitSymbolValue(&S, 8, 0);
381 }
382
383 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
384                                     raw_ostream &OS, MCCodeEmitter *CE,
385                                     bool RelaxAll, bool NoExecStack) {
386   MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
387   if (RelaxAll)
388     S->getAssembler().setRelaxAll(true);
389   if (NoExecStack)
390     S->getAssembler().setNoExecStack(true);
391   return S;
392 }
393
394 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
395   llvm_unreachable("Generic ELF doesn't support this directive");
396 }
397
398 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
399   llvm_unreachable("ELF doesn't support this directive");
400 }
401
402 void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
403   llvm_unreachable("ELF doesn't support this directive");
404 }
405
406 void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
407   llvm_unreachable("ELF doesn't support this directive");
408 }
409
410 void MCELFStreamer::EmitCOFFSymbolType(int Type) {
411   llvm_unreachable("ELF doesn't support this directive");
412 }
413
414 void MCELFStreamer::EndCOFFSymbolDef() {
415   llvm_unreachable("ELF doesn't support this directive");
416 }
417
418 void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
419                                  uint64_t Size, unsigned ByteAlignment) {
420   llvm_unreachable("ELF doesn't support this directive");
421 }
422
423 void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
424                                    uint64_t Size, unsigned ByteAlignment) {
425   llvm_unreachable("ELF doesn't support this directive");
426 }