Fixed another assert exposed by fuzzing. The utility function getRegisterEnum()
[oota-llvm.git] / lib / MC / MCMachOStreamer.cpp
1 //===- lib/MC/MCMachOStreamer.cpp - Mach-O 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 #include "llvm/MC/MCStreamer.h"
11
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCCodeEmitter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Target/TargetAsmBackend.h"
22
23 using namespace llvm;
24
25 namespace {
26
27 class MCMachOStreamer : public MCStreamer {
28   /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
29   /// 16 bits of the implementation defined flags.
30   enum SymbolFlags { // See <mach-o/nlist.h>.
31     SF_DescFlagsMask                        = 0xFFFF,
32
33     // Reference type flags.
34     SF_ReferenceTypeMask                    = 0x0007,
35     SF_ReferenceTypeUndefinedNonLazy        = 0x0000,
36     SF_ReferenceTypeUndefinedLazy           = 0x0001,
37     SF_ReferenceTypeDefined                 = 0x0002,
38     SF_ReferenceTypePrivateDefined          = 0x0003,
39     SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
40     SF_ReferenceTypePrivateUndefinedLazy    = 0x0005,
41
42     // Other 'desc' flags.
43     SF_NoDeadStrip                          = 0x0020,
44     SF_WeakReference                        = 0x0040,
45     SF_WeakDefinition                       = 0x0080
46   };
47
48 private:
49   MCAssembler Assembler;
50   MCSectionData *CurSectionData;
51
52 private:
53   MCFragment *getCurrentFragment() const {
54     assert(CurSectionData && "No current section!");
55
56     if (!CurSectionData->empty())
57       return &CurSectionData->getFragmentList().back();
58
59     return 0;
60   }
61
62   /// Get a data fragment to write into, creating a new one if the current
63   /// fragment is not a data fragment.
64   MCDataFragment *getOrCreateDataFragment() const {
65     MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
66     if (!F)
67       F = new MCDataFragment(CurSectionData);
68     return F;
69   }
70
71 public:
72   MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
73                   raw_ostream &_OS, MCCodeEmitter *_Emitter)
74     : MCStreamer(Context), Assembler(Context, TAB, *_Emitter, _OS),
75       CurSectionData(0) {}
76   ~MCMachOStreamer() {}
77
78   MCAssembler &getAssembler() { return Assembler; }
79
80   const MCExpr *AddValueSymbols(const MCExpr *Value) {
81     switch (Value->getKind()) {
82     case MCExpr::Target: assert(0 && "Can't handle target exprs yet!");
83     case MCExpr::Constant:
84       break;
85
86     case MCExpr::Binary: {
87       const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
88       AddValueSymbols(BE->getLHS());
89       AddValueSymbols(BE->getRHS());
90       break;
91     }
92
93     case MCExpr::SymbolRef:
94       Assembler.getOrCreateSymbolData(
95         cast<MCSymbolRefExpr>(Value)->getSymbol());
96       break;
97
98     case MCExpr::Unary:
99       AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
100       break;
101     }
102
103     return Value;
104   }
105
106   /// @name MCStreamer Interface
107   /// @{
108
109   virtual void SwitchSection(const MCSection *Section);
110   virtual void EmitLabel(MCSymbol *Symbol);
111   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
112   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
113   virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
114   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
115   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
116                                 unsigned ByteAlignment);
117   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
118     assert(0 && "macho doesn't support this directive");
119   }
120   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
121     assert(0 && "macho doesn't support this directive");
122   }
123   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
124                             unsigned Size = 0, unsigned ByteAlignment = 0);
125   virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
126   virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
127   virtual void EmitGPRel32Value(const MCExpr *Value) {
128     assert(0 && "macho doesn't support this directive");
129   }
130   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
131                                     unsigned ValueSize = 1,
132                                     unsigned MaxBytesToEmit = 0);
133   virtual void EmitCodeAlignment(unsigned ByteAlignment,
134                                  unsigned MaxBytesToEmit = 0);
135   virtual void EmitValueToOffset(const MCExpr *Offset,
136                                  unsigned char Value = 0);
137   
138   virtual void EmitFileDirective(StringRef Filename) {
139     errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n";
140   }
141   virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
142     errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n";
143   }
144   
145   virtual void EmitInstruction(const MCInst &Inst);
146   virtual void Finish();
147
148   /// @}
149 };
150
151 } // end anonymous namespace.
152
153 void MCMachOStreamer::SwitchSection(const MCSection *Section) {
154   assert(Section && "Cannot switch to a null section!");
155   
156   // If already in this section, then this is a noop.
157   if (Section == CurSection) return;
158
159   CurSection = Section;
160   CurSectionData = &Assembler.getOrCreateSectionData(*Section);
161 }
162
163 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
164   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
165
166   // FIXME: This is wasteful, we don't necessarily need to create a data
167   // fragment. Instead, we should mark the symbol as pointing into the data
168   // fragment if it exists, otherwise we should just queue the label and set its
169   // fragment pointer when we emit the next fragment.
170   MCDataFragment *F = getOrCreateDataFragment();
171   MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
172   assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
173   SD.setFragment(F);
174   SD.setOffset(F->getContents().size());
175
176   // This causes the reference type and weak reference flags to be cleared.
177   SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
178   
179   Symbol->setSection(*CurSection);
180 }
181
182 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
183   switch (Flag) {
184   case MCAF_SubsectionsViaSymbols:
185     Assembler.setSubsectionsViaSymbols(true);
186     return;
187   }
188
189   assert(0 && "invalid assembler flag!");
190 }
191
192 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
193   // Only absolute symbols can be redefined.
194   assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
195          "Cannot define a symbol twice!");
196
197   // FIXME: Lift context changes into super class.
198   // FIXME: Set associated section.
199   Symbol->setValue(AddValueSymbols(Value));
200 }
201
202 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
203                                           MCSymbolAttr Attribute) {
204   // Indirect symbols are handled differently, to match how 'as' handles
205   // them. This makes writing matching .o files easier.
206   if (Attribute == MCSA_IndirectSymbol) {
207     // Note that we intentionally cannot use the symbol data here; this is
208     // important for matching the string table that 'as' generates.
209     IndirectSymbolData ISD;
210     ISD.Symbol = Symbol;
211     ISD.SectionData = CurSectionData;
212     Assembler.getIndirectSymbols().push_back(ISD);
213     return;
214   }
215
216   // Adding a symbol attribute always introduces the symbol, note that an
217   // important side effect of calling getOrCreateSymbolData here is to register
218   // the symbol with the assembler.
219   MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
220
221   // The implementation of symbol attributes is designed to match 'as', but it
222   // leaves much to desired. It doesn't really make sense to arbitrarily add and
223   // remove flags, but 'as' allows this (in particular, see .desc).
224   //
225   // In the future it might be worth trying to make these operations more well
226   // defined.
227   switch (Attribute) {
228   case MCSA_Invalid:
229   case MCSA_ELF_TypeFunction:
230   case MCSA_ELF_TypeIndFunction:
231   case MCSA_ELF_TypeObject:
232   case MCSA_ELF_TypeTLS:
233   case MCSA_ELF_TypeCommon:
234   case MCSA_ELF_TypeNoType:
235   case MCSA_IndirectSymbol:
236   case MCSA_Hidden:
237   case MCSA_Internal:
238   case MCSA_Protected:
239   case MCSA_Weak:
240   case MCSA_Local:
241     assert(0 && "Invalid symbol attribute for Mach-O!");
242     break;
243
244   case MCSA_Global:
245     SD.setExternal(true);
246     break;
247
248   case MCSA_LazyReference:
249     // FIXME: This requires -dynamic.
250     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
251     if (Symbol->isUndefined())
252       SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
253     break;
254
255     // Since .reference sets the no dead strip bit, it is equivalent to
256     // .no_dead_strip in practice.
257   case MCSA_Reference:
258   case MCSA_NoDeadStrip:
259     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
260     break;
261
262   case MCSA_PrivateExtern:
263     SD.setExternal(true);
264     SD.setPrivateExtern(true);
265     break;
266
267   case MCSA_WeakReference:
268     // FIXME: This requires -dynamic.
269     if (Symbol->isUndefined())
270       SD.setFlags(SD.getFlags() | SF_WeakReference);
271     break;
272
273   case MCSA_WeakDefinition:
274     // FIXME: 'as' enforces that this is defined and global. The manual claims
275     // it has to be in a coalesced section, but this isn't enforced.
276     SD.setFlags(SD.getFlags() | SF_WeakDefinition);
277     break;
278   }
279 }
280
281 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
282   // Encode the 'desc' value into the lowest implementation defined bits.
283   assert(DescValue == (DescValue & SF_DescFlagsMask) && 
284          "Invalid .desc value!");
285   Assembler.getOrCreateSymbolData(*Symbol).setFlags(DescValue&SF_DescFlagsMask);
286 }
287
288 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
289                                        unsigned ByteAlignment) {
290   // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
291   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
292
293   MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
294   SD.setExternal(true);
295   SD.setCommon(Size, ByteAlignment);
296 }
297
298 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
299                                    unsigned Size, unsigned ByteAlignment) {
300   MCSectionData &SectData = Assembler.getOrCreateSectionData(*Section);
301
302   // The symbol may not be present, which only creates the section.
303   if (!Symbol)
304     return;
305
306   // FIXME: Assert that this section has the zerofill type.
307
308   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
309
310   MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
311
312   MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
313   SD.setFragment(F);
314
315   Symbol->setSection(*Section);
316
317   // Update the maximum alignment on the zero fill section if necessary.
318   if (ByteAlignment > SectData.getAlignment())
319     SectData.setAlignment(ByteAlignment);
320 }
321
322 void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
323   getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
324 }
325
326 void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
327                                 unsigned AddrSpace) {
328   MCDataFragment *DF = getOrCreateDataFragment();
329
330   // Avoid fixups when possible.
331   int64_t AbsValue;
332   if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
333     // FIXME: Endianness assumption.
334     for (unsigned i = 0; i != Size; ++i)
335       DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
336   } else {
337     DF->addFixup(MCAsmFixup(DF->getContents().size(), *AddValueSymbols(Value),
338                             MCFixup::getKindForSize(Size)));
339     DF->getContents().resize(DF->getContents().size() + Size, 0);
340   }
341 }
342
343 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
344                                            int64_t Value, unsigned ValueSize,
345                                            unsigned MaxBytesToEmit) {
346   if (MaxBytesToEmit == 0)
347     MaxBytesToEmit = ByteAlignment;
348   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
349                       false /* EmitNops */, CurSectionData);
350
351   // Update the maximum alignment on the current section if necessary.
352   if (ByteAlignment > CurSectionData->getAlignment())
353     CurSectionData->setAlignment(ByteAlignment);
354 }
355
356 void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
357                                         unsigned MaxBytesToEmit) {
358   if (MaxBytesToEmit == 0)
359     MaxBytesToEmit = ByteAlignment;
360   new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
361                       true /* EmitNops */, CurSectionData);
362
363   // Update the maximum alignment on the current section if necessary.
364   if (ByteAlignment > CurSectionData->getAlignment())
365     CurSectionData->setAlignment(ByteAlignment);
366 }
367
368 void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
369                                         unsigned char Value) {
370   new MCOrgFragment(*Offset, Value, CurSectionData);
371 }
372
373 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
374   // Scan for values.
375   for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
376     if (Inst.getOperand(i).isExpr())
377       AddValueSymbols(Inst.getOperand(i).getExpr());
378
379   CurSectionData->setHasInstructions(true);
380
381   // FIXME-PERF: Common case is that we don't need to relax, encode directly
382   // onto the data fragments buffers.
383
384   SmallVector<MCFixup, 4> Fixups;
385   SmallString<256> Code;
386   raw_svector_ostream VecOS(Code);
387   Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
388   VecOS.flush();
389
390   // FIXME: Eliminate this copy.
391   SmallVector<MCAsmFixup, 4> AsmFixups;
392   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
393     MCFixup &F = Fixups[i];
394     AsmFixups.push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
395                                    F.getKind()));
396   }
397
398   // See if we might need to relax this instruction, if so it needs its own
399   // fragment.
400   //
401   // FIXME-PERF: Support target hook to do a fast path that avoids the encoder,
402   // when we can immediately tell that we will get something which might need
403   // relaxation (and compute its size).
404   //
405   // FIXME-PERF: We should also be smart about immediately relaxing instructions
406   // which we can already show will never possibly fit (we can also do a very
407   // good job of this before we do the first relaxation pass, because we have
408   // total knowledge about undefined symbols at that point). Even now, though,
409   // we can do a decent job, especially on Darwin where scattering means that we
410   // are going to often know that we can never fully resolve a fixup.
411   if (Assembler.getBackend().MayNeedRelaxation(Inst, AsmFixups)) {
412     MCInstFragment *IF = new MCInstFragment(Inst, CurSectionData);
413
414     // Add the fixups and data.
415     //
416     // FIXME: Revisit this design decision when relaxation is done, we may be
417     // able to get away with not storing any extra data in the MCInst.
418     IF->getCode() = Code;
419     IF->getFixups() = AsmFixups;
420
421     return;
422   }
423
424   // Add the fixups and data.
425   MCDataFragment *DF = getOrCreateDataFragment();
426   for (unsigned i = 0, e = AsmFixups.size(); i != e; ++i) {
427     AsmFixups[i].Offset += DF->getContents().size();
428     DF->addFixup(AsmFixups[i]);
429   }
430   DF->getContents().append(Code.begin(), Code.end());
431 }
432
433 void MCMachOStreamer::Finish() {
434   Assembler.Finish();
435 }
436
437 MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
438                                       raw_ostream &OS, MCCodeEmitter *CE,
439                                       bool RelaxAll) {
440   MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
441   if (RelaxAll)
442     S->getAssembler().setRelaxAll(true);
443   return S;
444 }