blob: 4ac0917bb03d44500145f6dc588445aab7b46b20 [file] [log] [blame]
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001//===- 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
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000012#include "llvm/MC/MCAssembler.h"
13#include "llvm/MC/MCContext.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000014#include "llvm/MC/MCCodeEmitter.h"
15#include "llvm/MC/MCInst.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000016#include "llvm/MC/MCSection.h"
17#include "llvm/MC/MCSymbol.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000018#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000019#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000020using namespace llvm;
21
22namespace {
23
24class MCMachOStreamer : public MCStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000025 /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
26 /// 16 bits of the implementation defined flags.
27 enum SymbolFlags { // See <mach-o/nlist.h>.
28 SF_DescFlagsMask = 0xFFFF,
29
30 // Reference type flags.
31 SF_ReferenceTypeMask = 0x0007,
32 SF_ReferenceTypeUndefinedNonLazy = 0x0000,
33 SF_ReferenceTypeUndefinedLazy = 0x0001,
34 SF_ReferenceTypeDefined = 0x0002,
35 SF_ReferenceTypePrivateDefined = 0x0003,
36 SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
37 SF_ReferenceTypePrivateUndefinedLazy = 0x0005,
38
39 // Other 'desc' flags.
40 SF_NoDeadStrip = 0x0020,
41 SF_WeakReference = 0x0040,
42 SF_WeakDefinition = 0x0080
43 };
44
45private:
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000046 MCAssembler Assembler;
47
Daniel Dunbar4fac7492009-08-27 08:17:51 +000048 MCCodeEmitter *Emitter;
49
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000050 MCSectionData *CurSectionData;
51
52 DenseMap<const MCSection*, MCSectionData*> SectionMap;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000053
54 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
55
56private:
57 MCFragment *getCurrentFragment() const {
58 assert(CurSectionData && "No current section!");
59
60 if (!CurSectionData->empty())
61 return &CurSectionData->getFragmentList().back();
62
63 return 0;
64 }
65
Daniel Dunbar0f5fa692009-08-28 05:48:54 +000066 MCSectionData &getSectionData(const MCSection &Section) {
67 MCSectionData *&Entry = SectionMap[&Section];
68
69 if (!Entry)
70 Entry = new MCSectionData(Section, &Assembler);
71
72 return *Entry;
73 }
74
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000075 MCSymbolData &getSymbolData(MCSymbol &Symbol) {
76 MCSymbolData *&Entry = SymbolMap[&Symbol];
77
78 if (!Entry)
79 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
80
81 return *Entry;
82 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000083
84public:
Daniel Dunbar4fac7492009-08-27 08:17:51 +000085 MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
86 : MCStreamer(Context), Assembler(_OS), Emitter(_Emitter),
87 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000088 ~MCMachOStreamer() {}
89
Daniel Dunbara421de12009-08-26 13:57:37 +000090 const MCValue &AddValueSymbols(const MCValue &Value) {
91 if (Value.getSymA())
92 getSymbolData(*const_cast<MCSymbol*>(Value.getSymA()));
93 if (Value.getSymB())
94 getSymbolData(*const_cast<MCSymbol*>(Value.getSymB()));
95 return Value;
96 }
97
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000098 /// @name MCStreamer Interface
99 /// @{
100
101 virtual void SwitchSection(const MCSection *Section);
102
103 virtual void EmitLabel(MCSymbol *Symbol);
104
105 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
106
107 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
108 bool MakeAbsolute = false);
109
110 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
111
112 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
113
114 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
115
116 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbare6cdbf22009-08-28 05:48:46 +0000117 unsigned Pow2Alignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000118
Daniel Dunbar8751b942009-08-28 05:48:22 +0000119 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000120 unsigned Size = 0, unsigned Pow2Alignment = 0);
121
122 virtual void EmitBytes(const StringRef &Data);
123
124 virtual void EmitValue(const MCValue &Value, unsigned Size);
125
126 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
127 unsigned ValueSize = 1,
128 unsigned MaxBytesToEmit = 0);
129
130 virtual void EmitValueToOffset(const MCValue &Offset,
131 unsigned char Value = 0);
132
133 virtual void EmitInstruction(const MCInst &Inst);
134
135 virtual void Finish();
136
137 /// @}
138};
139
140} // end anonymous namespace.
141
142void MCMachOStreamer::SwitchSection(const MCSection *Section) {
143 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000144
145 // If already in this section, then this is a noop.
146 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000147
Chris Lattnercda7f782009-08-22 19:35:08 +0000148 CurSection = Section;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000149 CurSectionData = &getSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000150}
151
152void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000153 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000154
Daniel Dunbar5e835962009-08-26 02:48:04 +0000155 // FIXME: We should also use offsets into Fill fragments.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000156 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
157 if (!F)
158 F = new MCDataFragment(CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000159
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000160 MCSymbolData &SD = getSymbolData(*Symbol);
161 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
162 SD.setFragment(F);
163 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000164
165 // This causes the reference type and weak reference flags to be cleared.
166 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000167
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000168 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000169}
170
171void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000172 switch (Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000173 case SubsectionsViaSymbols:
174 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000175 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000176 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000177
178 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000179}
180
181void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
182 const MCValue &Value,
183 bool MakeAbsolute) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000184 // Only absolute symbols can be redefined.
185 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
186 "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000187
188 llvm_unreachable("FIXME: Not yet implemented!");
189}
190
191void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
192 SymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000193 // Indirect symbols are handled differently, to match how 'as' handles
194 // them. This makes writing matching .o files easier.
195 if (Attribute == MCStreamer::IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000196 // Note that we intentionally cannot use the symbol data here; this is
197 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000198 IndirectSymbolData ISD;
199 ISD.Symbol = Symbol;
200 ISD.SectionData = CurSectionData;
201 Assembler.getIndirectSymbols().push_back(ISD);
202 return;
203 }
204
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000205 // Adding a symbol attribute always introduces the symbol, note that an
206 // important side effect of calling getSymbolData here is to register the
207 // symbol with the assembler.
208 MCSymbolData &SD = getSymbolData(*Symbol);
209
210 // The implementation of symbol attributes is designed to match 'as', but it
211 // leaves much to desired. It doesn't really make sense to arbitrarily add and
212 // remove flags, but 'as' allows this (in particular, see .desc).
213 //
214 // In the future it might be worth trying to make these operations more well
215 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000216 switch (Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000217 case MCStreamer::IndirectSymbol:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000218 case MCStreamer::Hidden:
219 case MCStreamer::Internal:
220 case MCStreamer::Protected:
221 case MCStreamer::Weak:
222 assert(0 && "Invalid symbol attribute for Mach-O!");
223 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000224
225 case MCStreamer::Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000226 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000227 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000228
229 case MCStreamer::LazyReference:
230 // FIXME: This requires -dynamic.
231 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
232 if (Symbol->isUndefined())
233 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
234 break;
235
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000236 // Since .reference sets the no dead strip bit, it is equivalent to
237 // .no_dead_strip in practice.
238 case MCStreamer::Reference:
239 case MCStreamer::NoDeadStrip:
240 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
241 break;
242
243 case MCStreamer::PrivateExtern:
244 SD.setExternal(true);
245 SD.setPrivateExtern(true);
246 break;
247
248 case MCStreamer::WeakReference:
249 // FIXME: This requires -dynamic.
250 if (Symbol->isUndefined())
251 SD.setFlags(SD.getFlags() | SF_WeakReference);
252 break;
253
254 case MCStreamer::WeakDefinition:
255 // FIXME: 'as' enforces that this is defined and global. The manual claims
256 // it has to be in a coalesced section, but this isn't enforced.
257 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
258 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000259 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000260}
261
262void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000263 // Encode the 'desc' value into the lowest implementation defined bits.
264 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
265 "Invalid .desc value!");
266 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000267}
268
269void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000270 // FIXME: Implement?
271 llvm_report_error("unsupported '.lsym' directive");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000272}
273
274void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbare6cdbf22009-08-28 05:48:46 +0000275 unsigned Pow2Alignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000276 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
277 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
278
279 MCSymbolData &SD = getSymbolData(*Symbol);
280 SD.setExternal(true);
281 SD.setCommon(Size, 1 << Pow2Alignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000282}
283
Daniel Dunbar8751b942009-08-28 05:48:22 +0000284void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000285 unsigned Size, unsigned Pow2Alignment) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000286 unsigned ByteAlignment = 1 << Pow2Alignment;
287 MCSectionData &SectData = getSectionData(*Section);
288
289 // The symbol may not be present, which only creates the section.
290 if (!Symbol)
291 return;
292
293 // FIXME: Assert that this section has the zerofill type.
294
295 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
296
297 MCSymbolData &SD = getSymbolData(*Symbol);
298
299 MCFragment *F = new MCZeroFillFragment(Size, 1 << Pow2Alignment, &SectData);
300 SD.setFragment(F);
301
302 Symbol->setSection(*Section);
303
304 // Update the maximum alignment on the zero fill section if necessary.
305 if (ByteAlignment > SectData.getAlignment())
306 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000307}
308
309void MCMachOStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000310 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
311 if (!DF)
312 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000313 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000314}
315
316void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbara421de12009-08-26 13:57:37 +0000317 new MCFillFragment(AddValueSymbols(Value), Size, 1, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000318}
319
320void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
321 int64_t Value, unsigned ValueSize,
322 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000323 if (MaxBytesToEmit == 0)
324 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000325 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
326 CurSectionData);
327
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000328 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000329 if (ByteAlignment > CurSectionData->getAlignment())
330 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000331}
332
333void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
334 unsigned char Value) {
Daniel Dunbara421de12009-08-26 13:57:37 +0000335 new MCOrgFragment(AddValueSymbols(Offset), Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000336}
337
338void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000339 // Scan for values.
340 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
341 if (Inst.getOperand(i).isMCValue())
342 AddValueSymbols(Inst.getOperand(i).getMCValue());
343
344 if (!Emitter)
345 llvm_unreachable("no code emitter available!");
346
347 // FIXME: Relocations!
348 SmallString<256> Code;
349 raw_svector_ostream VecOS(Code);
350 Emitter->EncodeInstruction(Inst, VecOS);
351 EmitBytes(VecOS.str());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000352}
353
354void MCMachOStreamer::Finish() {
355 Assembler.Finish();
356}
357
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000358MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
359 MCCodeEmitter *CE) {
360 return new MCMachOStreamer(Context, OS, CE);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000361}