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