blob: 828b92a74478980d439010b2efbcb69b59c9f7cb [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 Dunbar821e3332009-08-31 08:09:28 +000019#include "llvm/MC/MCValue.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000020#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000021#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000022using namespace llvm;
23
24namespace {
25
26class MCMachOStreamer : public MCStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000027 /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
28 /// 16 bits of the implementation defined flags.
29 enum SymbolFlags { // See <mach-o/nlist.h>.
30 SF_DescFlagsMask = 0xFFFF,
31
32 // Reference type flags.
33 SF_ReferenceTypeMask = 0x0007,
34 SF_ReferenceTypeUndefinedNonLazy = 0x0000,
35 SF_ReferenceTypeUndefinedLazy = 0x0001,
36 SF_ReferenceTypeDefined = 0x0002,
37 SF_ReferenceTypePrivateDefined = 0x0003,
38 SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
39 SF_ReferenceTypePrivateUndefinedLazy = 0x0005,
40
41 // Other 'desc' flags.
42 SF_NoDeadStrip = 0x0020,
43 SF_WeakReference = 0x0040,
44 SF_WeakDefinition = 0x0080
45 };
46
47private:
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000048 MCAssembler Assembler;
49
Daniel Dunbar4fac7492009-08-27 08:17:51 +000050 MCCodeEmitter *Emitter;
51
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000052 MCSectionData *CurSectionData;
53
54 DenseMap<const MCSection*, MCSectionData*> SectionMap;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000055
56 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
57
58private:
59 MCFragment *getCurrentFragment() const {
60 assert(CurSectionData && "No current section!");
61
62 if (!CurSectionData->empty())
63 return &CurSectionData->getFragmentList().back();
64
65 return 0;
66 }
67
Daniel Dunbar0f5fa692009-08-28 05:48:54 +000068 MCSectionData &getSectionData(const MCSection &Section) {
69 MCSectionData *&Entry = SectionMap[&Section];
70
71 if (!Entry)
72 Entry = new MCSectionData(Section, &Assembler);
73
74 return *Entry;
75 }
76
Daniel Dunbarcb579b32009-08-31 08:08:06 +000077 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000078 MCSymbolData *&Entry = SymbolMap[&Symbol];
79
80 if (!Entry)
81 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
82
83 return *Entry;
84 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000085
86public:
Daniel Dunbar4fac7492009-08-27 08:17:51 +000087 MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbara03a3682009-08-31 08:07:55 +000088 : MCStreamer(Context), Assembler(Context, _OS), Emitter(_Emitter),
Daniel Dunbar4fac7492009-08-27 08:17:51 +000089 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000090 ~MCMachOStreamer() {}
91
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000092 const MCExpr *AddValueSymbols(const MCExpr *Value) {
93 switch (Value->getKind()) {
94 case MCExpr::Constant:
95 break;
96
97 case MCExpr::Binary: {
98 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
99 AddValueSymbols(BE->getLHS());
100 AddValueSymbols(BE->getRHS());
101 break;
102 }
103
104 case MCExpr::SymbolRef:
105 getSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
106 break;
107
108 case MCExpr::Unary:
109 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
110 break;
111 }
112
113 return Value;
114 }
115
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000116 /// @name MCStreamer Interface
117 /// @{
118
119 virtual void SwitchSection(const MCSection *Section);
120
121 virtual void EmitLabel(MCSymbol *Symbol);
122
123 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
124
Daniel Dunbar821e3332009-08-31 08:09:28 +0000125 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000126
127 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
128
129 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
130
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000131 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000132 unsigned ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000133
Daniel Dunbar8751b942009-08-28 05:48:22 +0000134 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000135 unsigned Size = 0, unsigned ByteAlignment = 0);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000136
Daniel Dunbar2928c832009-11-06 10:58:06 +0000137 virtual void EmitBytes(StringRef Data);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000138
Daniel Dunbar821e3332009-08-31 08:09:28 +0000139 virtual void EmitValue(const MCExpr *Value, unsigned Size);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000140
141 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
142 unsigned ValueSize = 1,
143 unsigned MaxBytesToEmit = 0);
144
Daniel Dunbar821e3332009-08-31 08:09:28 +0000145 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000146 unsigned char Value = 0);
147
148 virtual void EmitInstruction(const MCInst &Inst);
149
150 virtual void Finish();
151
152 /// @}
153};
154
155} // end anonymous namespace.
156
157void MCMachOStreamer::SwitchSection(const MCSection *Section) {
158 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000159
160 // If already in this section, then this is a noop.
161 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000162
Chris Lattnercda7f782009-08-22 19:35:08 +0000163 CurSection = Section;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000164 CurSectionData = &getSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000165}
166
167void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000168 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000169
Daniel Dunbar5e835962009-08-26 02:48:04 +0000170 // FIXME: We should also use offsets into Fill fragments.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000171 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
172 if (!F)
173 F = new MCDataFragment(CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000174
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000175 MCSymbolData &SD = getSymbolData(*Symbol);
176 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
177 SD.setFragment(F);
178 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000179
180 // This causes the reference type and weak reference flags to be cleared.
181 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000182
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000183 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000184}
185
186void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000187 switch (Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000188 case SubsectionsViaSymbols:
189 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000190 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000191 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000192
193 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000194}
195
Daniel Dunbar821e3332009-08-31 08:09:28 +0000196void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000197 // Only absolute symbols can be redefined.
198 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
199 "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000200
Daniel Dunbara4d86672009-10-16 01:58:23 +0000201 // FIXME: Lift context changes into super class.
202 // FIXME: Set associated section.
203 Symbol->setValue(Value);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000204}
205
206void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
207 SymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000208 // Indirect symbols are handled differently, to match how 'as' handles
209 // them. This makes writing matching .o files easier.
210 if (Attribute == MCStreamer::IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000211 // Note that we intentionally cannot use the symbol data here; this is
212 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000213 IndirectSymbolData ISD;
214 ISD.Symbol = Symbol;
215 ISD.SectionData = CurSectionData;
216 Assembler.getIndirectSymbols().push_back(ISD);
217 return;
218 }
219
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000220 // Adding a symbol attribute always introduces the symbol, note that an
221 // important side effect of calling getSymbolData here is to register the
222 // symbol with the assembler.
223 MCSymbolData &SD = getSymbolData(*Symbol);
224
225 // The implementation of symbol attributes is designed to match 'as', but it
226 // leaves much to desired. It doesn't really make sense to arbitrarily add and
227 // remove flags, but 'as' allows this (in particular, see .desc).
228 //
229 // In the future it might be worth trying to make these operations more well
230 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000231 switch (Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000232 case MCStreamer::IndirectSymbol:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000233 case MCStreamer::Hidden:
234 case MCStreamer::Internal:
235 case MCStreamer::Protected:
236 case MCStreamer::Weak:
237 assert(0 && "Invalid symbol attribute for Mach-O!");
238 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000239
240 case MCStreamer::Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000241 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000242 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000243
244 case MCStreamer::LazyReference:
245 // FIXME: This requires -dynamic.
246 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
247 if (Symbol->isUndefined())
248 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
249 break;
250
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000251 // Since .reference sets the no dead strip bit, it is equivalent to
252 // .no_dead_strip in practice.
253 case MCStreamer::Reference:
254 case MCStreamer::NoDeadStrip:
255 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
256 break;
257
258 case MCStreamer::PrivateExtern:
259 SD.setExternal(true);
260 SD.setPrivateExtern(true);
261 break;
262
263 case MCStreamer::WeakReference:
264 // FIXME: This requires -dynamic.
265 if (Symbol->isUndefined())
266 SD.setFlags(SD.getFlags() | SF_WeakReference);
267 break;
268
269 case MCStreamer::WeakDefinition:
270 // FIXME: 'as' enforces that this is defined and global. The manual claims
271 // it has to be in a coalesced section, but this isn't enforced.
272 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
273 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000274 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000275}
276
277void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000278 // Encode the 'desc' value into the lowest implementation defined bits.
279 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
280 "Invalid .desc value!");
281 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000282}
283
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000284void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000285 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000286 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
287 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
288
289 MCSymbolData &SD = getSymbolData(*Symbol);
290 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000291 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000292}
293
Daniel Dunbar8751b942009-08-28 05:48:22 +0000294void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000295 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000296 MCSectionData &SectData = getSectionData(*Section);
297
298 // The symbol may not be present, which only creates the section.
299 if (!Symbol)
300 return;
301
302 // FIXME: Assert that this section has the zerofill type.
303
304 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
305
306 MCSymbolData &SD = getSymbolData(*Symbol);
307
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000308 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000309 SD.setFragment(F);
310
311 Symbol->setSection(*Section);
312
313 // Update the maximum alignment on the zero fill section if necessary.
314 if (ByteAlignment > SectData.getAlignment())
315 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000316}
317
Daniel Dunbar2928c832009-11-06 10:58:06 +0000318void MCMachOStreamer::EmitBytes(StringRef Data) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000319 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
320 if (!DF)
321 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000322 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000323}
324
Daniel Dunbar821e3332009-08-31 08:09:28 +0000325void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000326 new MCFillFragment(*AddValueSymbols(Value), Size, 1, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000327}
328
329void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
330 int64_t Value, unsigned ValueSize,
331 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000332 if (MaxBytesToEmit == 0)
333 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000334 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
335 CurSectionData);
336
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000337 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000338 if (ByteAlignment > CurSectionData->getAlignment())
339 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000340}
341
Daniel Dunbar821e3332009-08-31 08:09:28 +0000342void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000343 unsigned char Value) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000344 new MCOrgFragment(*Offset, Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000345}
346
347void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000348 // Scan for values.
349 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000350 if (Inst.getOperand(i).isExpr())
351 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000352
353 if (!Emitter)
354 llvm_unreachable("no code emitter available!");
355
356 // FIXME: Relocations!
357 SmallString<256> Code;
358 raw_svector_ostream VecOS(Code);
359 Emitter->EncodeInstruction(Inst, VecOS);
360 EmitBytes(VecOS.str());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000361}
362
363void MCMachOStreamer::Finish() {
364 Assembler.Finish();
365}
366
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000367MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
368 MCCodeEmitter *CE) {
369 return new MCMachOStreamer(Context, OS, CE);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000370}