blob: 14301824ba8b2e3c31dfa1707dde65ba37a6c317 [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);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000120 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000121 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000122 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000123 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000124 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000125 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000126 unsigned ByteAlignment);
Daniel Dunbar8751b942009-08-28 05:48:22 +0000127 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000128 unsigned Size = 0, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +0000129 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +0000130 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000131 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
132 unsigned ValueSize = 1,
133 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000134 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000135 unsigned char Value = 0);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000136 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000137 virtual void Finish();
138
139 /// @}
140};
141
142} // end anonymous namespace.
143
144void MCMachOStreamer::SwitchSection(const MCSection *Section) {
145 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000146
147 // If already in this section, then this is a noop.
148 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000149
Chris Lattnercda7f782009-08-22 19:35:08 +0000150 CurSection = Section;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000151 CurSectionData = &getSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000152}
153
154void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000155 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000156
Daniel Dunbar5e835962009-08-26 02:48:04 +0000157 // FIXME: We should also use offsets into Fill fragments.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000158 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
159 if (!F)
160 F = new MCDataFragment(CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000161
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000162 MCSymbolData &SD = getSymbolData(*Symbol);
163 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
164 SD.setFragment(F);
165 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000166
167 // This causes the reference type and weak reference flags to be cleared.
168 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000169
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000170 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000171}
172
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000173void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000174 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000175 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar6009db42009-08-26 21:22:22 +0000176 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000177 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000178 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000179
180 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000181}
182
Daniel Dunbar821e3332009-08-31 08:09:28 +0000183void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
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
Daniel Dunbara4d86672009-10-16 01:58:23 +0000188 // FIXME: Lift context changes into super class.
189 // FIXME: Set associated section.
190 Symbol->setValue(Value);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000191}
192
193void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000194 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000195 // Indirect symbols are handled differently, to match how 'as' handles
196 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000197 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000198 // Note that we intentionally cannot use the symbol data here; this is
199 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000200 IndirectSymbolData ISD;
201 ISD.Symbol = Symbol;
202 ISD.SectionData = CurSectionData;
203 Assembler.getIndirectSymbols().push_back(ISD);
204 return;
205 }
206
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000207 // Adding a symbol attribute always introduces the symbol, note that an
208 // important side effect of calling getSymbolData here is to register the
209 // symbol with the assembler.
210 MCSymbolData &SD = getSymbolData(*Symbol);
211
212 // The implementation of symbol attributes is designed to match 'as', but it
213 // leaves much to desired. It doesn't really make sense to arbitrarily add and
214 // remove flags, but 'as' allows this (in particular, see .desc).
215 //
216 // In the future it might be worth trying to make these operations more well
217 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000218 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000219 case MCSA_Invalid:
220 case MCSA_IndirectSymbol:
221 case MCSA_Hidden:
222 case MCSA_Internal:
223 case MCSA_Protected:
224 case MCSA_Weak:
225 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000226 assert(0 && "Invalid symbol attribute for Mach-O!");
227 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000228
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000229 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000230 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000231 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000232
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000233 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000234 // FIXME: This requires -dynamic.
235 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
236 if (Symbol->isUndefined())
237 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
238 break;
239
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000240 // Since .reference sets the no dead strip bit, it is equivalent to
241 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000242 case MCSA_Reference:
243 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000244 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
245 break;
246
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000247 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000248 SD.setExternal(true);
249 SD.setPrivateExtern(true);
250 break;
251
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000252 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000253 // FIXME: This requires -dynamic.
254 if (Symbol->isUndefined())
255 SD.setFlags(SD.getFlags() | SF_WeakReference);
256 break;
257
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000258 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000259 // FIXME: 'as' enforces that this is defined and global. The manual claims
260 // it has to be in a coalesced section, but this isn't enforced.
261 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
262 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000263 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000264}
265
266void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000267 // Encode the 'desc' value into the lowest implementation defined bits.
268 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
269 "Invalid .desc value!");
270 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000271}
272
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000273void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000274 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000275 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
276 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
277
278 MCSymbolData &SD = getSymbolData(*Symbol);
279 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000280 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000281}
282
Daniel Dunbar8751b942009-08-28 05:48:22 +0000283void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000284 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000285 MCSectionData &SectData = getSectionData(*Section);
286
287 // The symbol may not be present, which only creates the section.
288 if (!Symbol)
289 return;
290
291 // FIXME: Assert that this section has the zerofill type.
292
293 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
294
295 MCSymbolData &SD = getSymbolData(*Symbol);
296
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000297 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000298 SD.setFragment(F);
299
300 Symbol->setSection(*Section);
301
302 // Update the maximum alignment on the zero fill section if necessary.
303 if (ByteAlignment > SectData.getAlignment())
304 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000305}
306
Chris Lattneraaec2052010-01-19 19:46:13 +0000307void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000308 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
309 if (!DF)
310 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000311 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000312}
313
Chris Lattneraaec2052010-01-19 19:46:13 +0000314void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
315 unsigned AddrSpace) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000316 new MCFillFragment(*AddValueSymbols(Value), Size, 1, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000317}
318
319void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
320 int64_t Value, unsigned ValueSize,
321 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000322 if (MaxBytesToEmit == 0)
323 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000324 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
325 CurSectionData);
326
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000327 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000328 if (ByteAlignment > CurSectionData->getAlignment())
329 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000330}
331
Daniel Dunbar821e3332009-08-31 08:09:28 +0000332void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000333 unsigned char Value) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000334 new MCOrgFragment(*Offset, Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000335}
336
337void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000338 // Scan for values.
339 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000340 if (Inst.getOperand(i).isExpr())
341 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000342
343 if (!Emitter)
344 llvm_unreachable("no code emitter available!");
345
Nate Begeman6f24a0a2010-01-17 03:49:01 +0000346 // FIXME: Emitting an instruction should cause S_ATTR_SOME_INSTRUCTIONS to
347 // be set for the current section.
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000348 // FIXME: Relocations!
349 SmallString<256> Code;
350 raw_svector_ostream VecOS(Code);
351 Emitter->EncodeInstruction(Inst, VecOS);
Chris Lattneraaec2052010-01-19 19:46:13 +0000352 EmitBytes(VecOS.str(), 0);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000353}
354
355void MCMachOStreamer::Finish() {
356 Assembler.Finish();
357}
358
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000359MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
360 MCCodeEmitter *CE) {
361 return new MCMachOStreamer(Context, OS, CE);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000362}