blob: c92d8a85ea7f48b2a5a07d19d71a063254f85d52 [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"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000016#include "llvm/MC/MCInst.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000017#include "llvm/MC/MCSection.h"
18#include "llvm/MC/MCSymbol.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000019#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000020#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000021using namespace llvm;
22
23namespace {
24
25class MCMachOStreamer : public MCStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000026 /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
27 /// 16 bits of the implementation defined flags.
28 enum SymbolFlags { // See <mach-o/nlist.h>.
29 SF_DescFlagsMask = 0xFFFF,
30
31 // Reference type flags.
32 SF_ReferenceTypeMask = 0x0007,
33 SF_ReferenceTypeUndefinedNonLazy = 0x0000,
34 SF_ReferenceTypeUndefinedLazy = 0x0001,
35 SF_ReferenceTypeDefined = 0x0002,
36 SF_ReferenceTypePrivateDefined = 0x0003,
37 SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
38 SF_ReferenceTypePrivateUndefinedLazy = 0x0005,
39
40 // Other 'desc' flags.
41 SF_NoDeadStrip = 0x0020,
42 SF_WeakReference = 0x0040,
43 SF_WeakDefinition = 0x0080
44 };
45
46private:
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000047 MCAssembler Assembler;
48
Daniel Dunbar4fac7492009-08-27 08:17:51 +000049 MCCodeEmitter *Emitter;
50
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000051 MCSectionData *CurSectionData;
52
53 DenseMap<const MCSection*, MCSectionData*> SectionMap;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000054
55 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
56
57private:
58 MCFragment *getCurrentFragment() const {
59 assert(CurSectionData && "No current section!");
60
61 if (!CurSectionData->empty())
62 return &CurSectionData->getFragmentList().back();
63
64 return 0;
65 }
66
Daniel Dunbar0f5fa692009-08-28 05:48:54 +000067 MCSectionData &getSectionData(const MCSection &Section) {
68 MCSectionData *&Entry = SectionMap[&Section];
69
70 if (!Entry)
71 Entry = new MCSectionData(Section, &Assembler);
72
73 return *Entry;
74 }
75
Daniel Dunbarcb579b32009-08-31 08:08:06 +000076 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000077 MCSymbolData *&Entry = SymbolMap[&Symbol];
78
79 if (!Entry)
80 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
81
82 return *Entry;
83 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000084
85public:
Daniel Dunbar4fac7492009-08-27 08:17:51 +000086 MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbara03a3682009-08-31 08:07:55 +000087 : MCStreamer(Context), Assembler(Context, _OS), Emitter(_Emitter),
Daniel Dunbar4fac7492009-08-27 08:17:51 +000088 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000089 ~MCMachOStreamer() {}
90
Daniel Dunbara421de12009-08-26 13:57:37 +000091 const MCValue &AddValueSymbols(const MCValue &Value) {
92 if (Value.getSymA())
93 getSymbolData(*const_cast<MCSymbol*>(Value.getSymA()));
94 if (Value.getSymB())
95 getSymbolData(*const_cast<MCSymbol*>(Value.getSymB()));
96 return Value;
97 }
98
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000099 const MCExpr *AddValueSymbols(const MCExpr *Value) {
100 switch (Value->getKind()) {
101 case MCExpr::Constant:
102 break;
103
104 case MCExpr::Binary: {
105 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
106 AddValueSymbols(BE->getLHS());
107 AddValueSymbols(BE->getRHS());
108 break;
109 }
110
111 case MCExpr::SymbolRef:
112 getSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
113 break;
114
115 case MCExpr::Unary:
116 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
117 break;
118 }
119
120 return Value;
121 }
122
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000123 /// @name MCStreamer Interface
124 /// @{
125
126 virtual void SwitchSection(const MCSection *Section);
127
128 virtual void EmitLabel(MCSymbol *Symbol);
129
130 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
131
Daniel Dunbare2ace502009-08-31 08:09:09 +0000132 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000133
134 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
135
136 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
137
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000138 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000139 unsigned ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000140
Daniel Dunbar8751b942009-08-28 05:48:22 +0000141 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000142 unsigned Size = 0, unsigned ByteAlignment = 0);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000143
144 virtual void EmitBytes(const StringRef &Data);
145
146 virtual void EmitValue(const MCValue &Value, unsigned Size);
147
148 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
149 unsigned ValueSize = 1,
150 unsigned MaxBytesToEmit = 0);
151
152 virtual void EmitValueToOffset(const MCValue &Offset,
153 unsigned char Value = 0);
154
155 virtual void EmitInstruction(const MCInst &Inst);
156
157 virtual void Finish();
158
159 /// @}
160};
161
162} // end anonymous namespace.
163
164void MCMachOStreamer::SwitchSection(const MCSection *Section) {
165 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000166
167 // If already in this section, then this is a noop.
168 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000169
Chris Lattnercda7f782009-08-22 19:35:08 +0000170 CurSection = Section;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000171 CurSectionData = &getSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000172}
173
174void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000175 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000176
Daniel Dunbar5e835962009-08-26 02:48:04 +0000177 // FIXME: We should also use offsets into Fill fragments.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000178 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
179 if (!F)
180 F = new MCDataFragment(CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000181
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000182 MCSymbolData &SD = getSymbolData(*Symbol);
183 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
184 SD.setFragment(F);
185 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000186
187 // This causes the reference type and weak reference flags to be cleared.
188 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000189
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000190 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000191}
192
193void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000194 switch (Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000195 case SubsectionsViaSymbols:
196 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000197 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000198 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000199
200 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000201}
202
Daniel Dunbare2ace502009-08-31 08:09:09 +0000203void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000204 // Only absolute symbols can be redefined.
205 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
206 "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000207
208 llvm_unreachable("FIXME: Not yet implemented!");
209}
210
211void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
212 SymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000213 // Indirect symbols are handled differently, to match how 'as' handles
214 // them. This makes writing matching .o files easier.
215 if (Attribute == MCStreamer::IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000216 // Note that we intentionally cannot use the symbol data here; this is
217 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000218 IndirectSymbolData ISD;
219 ISD.Symbol = Symbol;
220 ISD.SectionData = CurSectionData;
221 Assembler.getIndirectSymbols().push_back(ISD);
222 return;
223 }
224
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000225 // Adding a symbol attribute always introduces the symbol, note that an
226 // important side effect of calling getSymbolData here is to register the
227 // symbol with the assembler.
228 MCSymbolData &SD = getSymbolData(*Symbol);
229
230 // The implementation of symbol attributes is designed to match 'as', but it
231 // leaves much to desired. It doesn't really make sense to arbitrarily add and
232 // remove flags, but 'as' allows this (in particular, see .desc).
233 //
234 // In the future it might be worth trying to make these operations more well
235 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000236 switch (Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000237 case MCStreamer::IndirectSymbol:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000238 case MCStreamer::Hidden:
239 case MCStreamer::Internal:
240 case MCStreamer::Protected:
241 case MCStreamer::Weak:
242 assert(0 && "Invalid symbol attribute for Mach-O!");
243 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000244
245 case MCStreamer::Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000246 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000247 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000248
249 case MCStreamer::LazyReference:
250 // FIXME: This requires -dynamic.
251 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
252 if (Symbol->isUndefined())
253 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
254 break;
255
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000256 // Since .reference sets the no dead strip bit, it is equivalent to
257 // .no_dead_strip in practice.
258 case MCStreamer::Reference:
259 case MCStreamer::NoDeadStrip:
260 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
261 break;
262
263 case MCStreamer::PrivateExtern:
264 SD.setExternal(true);
265 SD.setPrivateExtern(true);
266 break;
267
268 case MCStreamer::WeakReference:
269 // FIXME: This requires -dynamic.
270 if (Symbol->isUndefined())
271 SD.setFlags(SD.getFlags() | SF_WeakReference);
272 break;
273
274 case MCStreamer::WeakDefinition:
275 // FIXME: 'as' enforces that this is defined and global. The manual claims
276 // it has to be in a coalesced section, but this isn't enforced.
277 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
278 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000279 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000280}
281
282void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000283 // Encode the 'desc' value into the lowest implementation defined bits.
284 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
285 "Invalid .desc value!");
286 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000287}
288
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000289void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000290 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000291 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
292 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
293
294 MCSymbolData &SD = getSymbolData(*Symbol);
295 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000296 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000297}
298
Daniel Dunbar8751b942009-08-28 05:48:22 +0000299void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000300 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000301 MCSectionData &SectData = getSectionData(*Section);
302
303 // The symbol may not be present, which only creates the section.
304 if (!Symbol)
305 return;
306
307 // FIXME: Assert that this section has the zerofill type.
308
309 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
310
311 MCSymbolData &SD = getSymbolData(*Symbol);
312
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000313 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000314 SD.setFragment(F);
315
316 Symbol->setSection(*Section);
317
318 // Update the maximum alignment on the zero fill section if necessary.
319 if (ByteAlignment > SectData.getAlignment())
320 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000321}
322
323void MCMachOStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000324 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
325 if (!DF)
326 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000327 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000328}
329
330void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbara421de12009-08-26 13:57:37 +0000331 new MCFillFragment(AddValueSymbols(Value), Size, 1, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000332}
333
334void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
335 int64_t Value, unsigned ValueSize,
336 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000337 if (MaxBytesToEmit == 0)
338 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000339 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
340 CurSectionData);
341
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000342 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000343 if (ByteAlignment > CurSectionData->getAlignment())
344 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000345}
346
347void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
348 unsigned char Value) {
Daniel Dunbara421de12009-08-26 13:57:37 +0000349 new MCOrgFragment(AddValueSymbols(Offset), Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000350}
351
352void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000353 // Scan for values.
354 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000355 if (Inst.getOperand(i).isExpr())
356 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000357
358 if (!Emitter)
359 llvm_unreachable("no code emitter available!");
360
361 // FIXME: Relocations!
362 SmallString<256> Code;
363 raw_svector_ostream VecOS(Code);
364 Emitter->EncodeInstruction(Inst, VecOS);
365 EmitBytes(VecOS.str());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000366}
367
368void MCMachOStreamer::Finish() {
369 Assembler.Finish();
370}
371
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000372MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
373 MCCodeEmitter *CE) {
374 return new MCMachOStreamer(Context, OS, CE);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000375}