blob: 5a7b01b4764b7ae07200528db28a6cfe7cdc8d1a [file] [log] [blame]
Daniel Dunbar7760fbe2009-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 Dunbar7760fbe2009-08-21 09:11:24 +000012#include "llvm/MC/MCAssembler.h"
13#include "llvm/MC/MCContext.h"
Daniel Dunbar006e46e2009-08-27 08:17:51 +000014#include "llvm/MC/MCCodeEmitter.h"
15#include "llvm/MC/MCInst.h"
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000016#include "llvm/MC/MCSection.h"
17#include "llvm/MC/MCSymbol.h"
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000018#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar006e46e2009-08-27 08:17:51 +000019#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000020using namespace llvm;
21
22namespace {
23
24class MCMachOStreamer : public MCStreamer {
Daniel Dunbar30b09422009-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 Dunbar7760fbe2009-08-21 09:11:24 +000046 MCAssembler Assembler;
47
Daniel Dunbar006e46e2009-08-27 08:17:51 +000048 MCCodeEmitter *Emitter;
49
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000050 MCSectionData *CurSectionData;
51
52 DenseMap<const MCSection*, MCSectionData*> SectionMap;
Daniel Dunbar197f5632009-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 Dunbar91e7ee62009-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 Dunbar22ad5f82009-08-31 08:08:06 +000075 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
Daniel Dunbar197f5632009-08-22 10:13:24 +000076 MCSymbolData *&Entry = SymbolMap[&Symbol];
77
78 if (!Entry)
79 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
80
81 return *Entry;
82 }
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000083
84public:
Daniel Dunbar006e46e2009-08-27 08:17:51 +000085 MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbar29efe7e2009-08-31 08:07:55 +000086 : MCStreamer(Context), Assembler(Context, _OS), Emitter(_Emitter),
Daniel Dunbar006e46e2009-08-27 08:17:51 +000087 CurSectionData(0) {}
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000088 ~MCMachOStreamer() {}
89
Daniel Dunbar4486ac12009-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 Dunbar7760fbe2009-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
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000114 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000115 unsigned ByteAlignment);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000116
Daniel Dunbar15f1a5c2009-08-28 05:48:22 +0000117 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000118 unsigned Size = 0, unsigned ByteAlignment = 0);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000119
120 virtual void EmitBytes(const StringRef &Data);
121
122 virtual void EmitValue(const MCValue &Value, unsigned Size);
123
124 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
125 unsigned ValueSize = 1,
126 unsigned MaxBytesToEmit = 0);
127
128 virtual void EmitValueToOffset(const MCValue &Offset,
129 unsigned char Value = 0);
130
131 virtual void EmitInstruction(const MCInst &Inst);
132
133 virtual void Finish();
134
135 /// @}
136};
137
138} // end anonymous namespace.
139
140void MCMachOStreamer::SwitchSection(const MCSection *Section) {
141 assert(Section && "Cannot switch to a null section!");
Chris Lattnerbf7a11f2009-08-22 19:35:08 +0000142
143 // If already in this section, then this is a noop.
144 if (Section == CurSection) return;
Daniel Dunbar91e7ee62009-08-28 05:48:54 +0000145
Chris Lattnerbf7a11f2009-08-22 19:35:08 +0000146 CurSection = Section;
Daniel Dunbar91e7ee62009-08-28 05:48:54 +0000147 CurSectionData = &getSectionData(*Section);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000148}
149
150void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000151 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000152
Daniel Dunbarb3730b12009-08-26 02:48:04 +0000153 // FIXME: We should also use offsets into Fill fragments.
Daniel Dunbar197f5632009-08-22 10:13:24 +0000154 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
155 if (!F)
156 F = new MCDataFragment(CurSectionData);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000157
Daniel Dunbar197f5632009-08-22 10:13:24 +0000158 MCSymbolData &SD = getSymbolData(*Symbol);
159 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
160 SD.setFragment(F);
161 SD.setOffset(F->getContents().size());
Daniel Dunbar30b09422009-08-24 08:40:12 +0000162
163 // This causes the reference type and weak reference flags to be cleared.
164 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbar197f5632009-08-22 10:13:24 +0000165
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000166 Symbol->setSection(*CurSection);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000167}
168
169void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
Daniel Dunbarf8c4bb72009-08-26 21:22:22 +0000170 switch (Flag) {
Daniel Dunbarf8c4bb72009-08-26 21:22:22 +0000171 case SubsectionsViaSymbols:
172 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar796a6392009-08-28 07:08:47 +0000173 return;
Daniel Dunbarf8c4bb72009-08-26 21:22:22 +0000174 }
Daniel Dunbar796a6392009-08-28 07:08:47 +0000175
176 assert(0 && "invalid assembler flag!");
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000177}
178
179void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
180 const MCValue &Value,
181 bool MakeAbsolute) {
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000182 // Only absolute symbols can be redefined.
183 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
184 "Cannot define a symbol twice!");
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000185
186 llvm_unreachable("FIXME: Not yet implemented!");
187}
188
189void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
190 SymbolAttr Attribute) {
Daniel Dunbara0151d02009-08-24 11:56:58 +0000191 // Indirect symbols are handled differently, to match how 'as' handles
192 // them. This makes writing matching .o files easier.
193 if (Attribute == MCStreamer::IndirectSymbol) {
Daniel Dunbar6d294da2009-08-26 00:18:21 +0000194 // Note that we intentionally cannot use the symbol data here; this is
195 // important for matching the string table that 'as' generates.
Daniel Dunbara0151d02009-08-24 11:56:58 +0000196 IndirectSymbolData ISD;
197 ISD.Symbol = Symbol;
198 ISD.SectionData = CurSectionData;
199 Assembler.getIndirectSymbols().push_back(ISD);
200 return;
201 }
202
Daniel Dunbar30b09422009-08-24 08:40:12 +0000203 // Adding a symbol attribute always introduces the symbol, note that an
204 // important side effect of calling getSymbolData here is to register the
205 // symbol with the assembler.
206 MCSymbolData &SD = getSymbolData(*Symbol);
207
208 // The implementation of symbol attributes is designed to match 'as', but it
209 // leaves much to desired. It doesn't really make sense to arbitrarily add and
210 // remove flags, but 'as' allows this (in particular, see .desc).
211 //
212 // In the future it might be worth trying to make these operations more well
213 // defined.
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000214 switch (Attribute) {
Daniel Dunbara0151d02009-08-24 11:56:58 +0000215 case MCStreamer::IndirectSymbol:
Daniel Dunbar30b09422009-08-24 08:40:12 +0000216 case MCStreamer::Hidden:
217 case MCStreamer::Internal:
218 case MCStreamer::Protected:
219 case MCStreamer::Weak:
220 assert(0 && "Invalid symbol attribute for Mach-O!");
221 break;
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000222
223 case MCStreamer::Global:
Daniel Dunbar8388a8f2009-08-28 07:08:35 +0000224 SD.setExternal(true);
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000225 break;
Daniel Dunbar30b09422009-08-24 08:40:12 +0000226
227 case MCStreamer::LazyReference:
228 // FIXME: This requires -dynamic.
229 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
230 if (Symbol->isUndefined())
231 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
232 break;
233
Daniel Dunbar30b09422009-08-24 08:40:12 +0000234 // Since .reference sets the no dead strip bit, it is equivalent to
235 // .no_dead_strip in practice.
236 case MCStreamer::Reference:
237 case MCStreamer::NoDeadStrip:
238 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
239 break;
240
241 case MCStreamer::PrivateExtern:
242 SD.setExternal(true);
243 SD.setPrivateExtern(true);
244 break;
245
246 case MCStreamer::WeakReference:
247 // FIXME: This requires -dynamic.
248 if (Symbol->isUndefined())
249 SD.setFlags(SD.getFlags() | SF_WeakReference);
250 break;
251
252 case MCStreamer::WeakDefinition:
253 // FIXME: 'as' enforces that this is defined and global. The manual claims
254 // it has to be in a coalesced section, but this isn't enforced.
255 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
256 break;
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000257 }
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000258}
259
260void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar30b09422009-08-24 08:40:12 +0000261 // Encode the 'desc' value into the lowest implementation defined bits.
262 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
263 "Invalid .desc value!");
264 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000265}
266
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000267void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000268 unsigned ByteAlignment) {
Daniel Dunbar8388a8f2009-08-28 07:08:35 +0000269 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
270 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
271
272 MCSymbolData &SD = getSymbolData(*Symbol);
273 SD.setExternal(true);
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000274 SD.setCommon(Size, ByteAlignment);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000275}
276
Daniel Dunbar15f1a5c2009-08-28 05:48:22 +0000277void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000278 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbardf98a142009-08-28 05:49:21 +0000279 MCSectionData &SectData = getSectionData(*Section);
280
281 // The symbol may not be present, which only creates the section.
282 if (!Symbol)
283 return;
284
285 // FIXME: Assert that this section has the zerofill type.
286
287 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
288
289 MCSymbolData &SD = getSymbolData(*Symbol);
290
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000291 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
Daniel Dunbardf98a142009-08-28 05:49:21 +0000292 SD.setFragment(F);
293
294 Symbol->setSection(*Section);
295
296 // Update the maximum alignment on the zero fill section if necessary.
297 if (ByteAlignment > SectData.getAlignment())
298 SectData.setAlignment(ByteAlignment);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000299}
300
301void MCMachOStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar197f5632009-08-22 10:13:24 +0000302 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
303 if (!DF)
304 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000305 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000306}
307
308void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar4486ac12009-08-26 13:57:37 +0000309 new MCFillFragment(AddValueSymbols(Value), Size, 1, CurSectionData);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000310}
311
312void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
313 int64_t Value, unsigned ValueSize,
314 unsigned MaxBytesToEmit) {
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000315 if (MaxBytesToEmit == 0)
316 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000317 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
318 CurSectionData);
319
Daniel Dunbar197f5632009-08-22 10:13:24 +0000320 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000321 if (ByteAlignment > CurSectionData->getAlignment())
322 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000323}
324
325void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
326 unsigned char Value) {
Daniel Dunbar4486ac12009-08-26 13:57:37 +0000327 new MCOrgFragment(AddValueSymbols(Offset), Value, CurSectionData);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000328}
329
330void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar006e46e2009-08-27 08:17:51 +0000331 // Scan for values.
332 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
333 if (Inst.getOperand(i).isMCValue())
334 AddValueSymbols(Inst.getOperand(i).getMCValue());
335
336 if (!Emitter)
337 llvm_unreachable("no code emitter available!");
338
339 // FIXME: Relocations!
340 SmallString<256> Code;
341 raw_svector_ostream VecOS(Code);
342 Emitter->EncodeInstruction(Inst, VecOS);
343 EmitBytes(VecOS.str());
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000344}
345
346void MCMachOStreamer::Finish() {
347 Assembler.Finish();
348}
349
Daniel Dunbar006e46e2009-08-27 08:17:51 +0000350MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
351 MCCodeEmitter *CE) {
352 return new MCMachOStreamer(Context, OS, CE);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000353}