blob: ef5a99fb37acaa13ad5806310cc6303a27b455bf [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"
14#include "llvm/MC/MCSection.h"
15#include "llvm/MC/MCSymbol.h"
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000016#include "llvm/Support/ErrorHandling.h"
17using namespace llvm;
18
19namespace {
20
21class MCMachOStreamer : public MCStreamer {
Daniel Dunbar30b09422009-08-24 08:40:12 +000022 /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
23 /// 16 bits of the implementation defined flags.
24 enum SymbolFlags { // See <mach-o/nlist.h>.
25 SF_DescFlagsMask = 0xFFFF,
26
27 // Reference type flags.
28 SF_ReferenceTypeMask = 0x0007,
29 SF_ReferenceTypeUndefinedNonLazy = 0x0000,
30 SF_ReferenceTypeUndefinedLazy = 0x0001,
31 SF_ReferenceTypeDefined = 0x0002,
32 SF_ReferenceTypePrivateDefined = 0x0003,
33 SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
34 SF_ReferenceTypePrivateUndefinedLazy = 0x0005,
35
36 // Other 'desc' flags.
37 SF_NoDeadStrip = 0x0020,
38 SF_WeakReference = 0x0040,
39 SF_WeakDefinition = 0x0080
40 };
41
42private:
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000043 MCAssembler Assembler;
44
45 MCSectionData *CurSectionData;
46
47 DenseMap<const MCSection*, MCSectionData*> SectionMap;
Daniel Dunbar197f5632009-08-22 10:13:24 +000048
49 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
50
51private:
52 MCFragment *getCurrentFragment() const {
53 assert(CurSectionData && "No current section!");
54
55 if (!CurSectionData->empty())
56 return &CurSectionData->getFragmentList().back();
57
58 return 0;
59 }
60
61 MCSymbolData &getSymbolData(MCSymbol &Symbol) {
62 MCSymbolData *&Entry = SymbolMap[&Symbol];
63
64 if (!Entry)
65 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
66
67 return *Entry;
68 }
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000069
70public:
71 MCMachOStreamer(MCContext &Context, raw_ostream &_OS)
72 : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {}
73 ~MCMachOStreamer() {}
74
Daniel Dunbar4486ac12009-08-26 13:57:37 +000075 const MCValue &AddValueSymbols(const MCValue &Value) {
76 if (Value.getSymA())
77 getSymbolData(*const_cast<MCSymbol*>(Value.getSymA()));
78 if (Value.getSymB())
79 getSymbolData(*const_cast<MCSymbol*>(Value.getSymB()));
80 return Value;
81 }
82
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000083 /// @name MCStreamer Interface
84 /// @{
85
86 virtual void SwitchSection(const MCSection *Section);
87
88 virtual void EmitLabel(MCSymbol *Symbol);
89
90 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
91
92 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
93 bool MakeAbsolute = false);
94
95 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
96
97 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
98
99 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
100
101 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
102 unsigned Pow2Alignment, bool IsLocal);
103
104 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
105 unsigned Size = 0, unsigned Pow2Alignment = 0);
106
107 virtual void EmitBytes(const StringRef &Data);
108
109 virtual void EmitValue(const MCValue &Value, unsigned Size);
110
111 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
112 unsigned ValueSize = 1,
113 unsigned MaxBytesToEmit = 0);
114
115 virtual void EmitValueToOffset(const MCValue &Offset,
116 unsigned char Value = 0);
117
118 virtual void EmitInstruction(const MCInst &Inst);
119
120 virtual void Finish();
121
122 /// @}
123};
124
125} // end anonymous namespace.
126
127void MCMachOStreamer::SwitchSection(const MCSection *Section) {
128 assert(Section && "Cannot switch to a null section!");
Chris Lattnerbf7a11f2009-08-22 19:35:08 +0000129
130 // If already in this section, then this is a noop.
131 if (Section == CurSection) return;
132
133 CurSection = Section;
134 MCSectionData *&Entry = SectionMap[Section];
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000135
Chris Lattnerbf7a11f2009-08-22 19:35:08 +0000136 if (!Entry)
137 Entry = new MCSectionData(*Section, &Assembler);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000138
Chris Lattnerbf7a11f2009-08-22 19:35:08 +0000139 CurSectionData = Entry;
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000140}
141
142void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000143 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000144
Daniel Dunbarb3730b12009-08-26 02:48:04 +0000145 // FIXME: We should also use offsets into Fill fragments.
Daniel Dunbar197f5632009-08-22 10:13:24 +0000146 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
147 if (!F)
148 F = new MCDataFragment(CurSectionData);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000149
Daniel Dunbar197f5632009-08-22 10:13:24 +0000150 MCSymbolData &SD = getSymbolData(*Symbol);
151 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
152 SD.setFragment(F);
153 SD.setOffset(F->getContents().size());
Daniel Dunbar30b09422009-08-24 08:40:12 +0000154
155 // This causes the reference type and weak reference flags to be cleared.
156 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbar197f5632009-08-22 10:13:24 +0000157
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000158 Symbol->setSection(*CurSection);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000159}
160
161void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
Daniel Dunbarf8c4bb72009-08-26 21:22:22 +0000162 switch (Flag) {
163 default:
164 llvm_unreachable("FIXME: Not yet implemented!");
165
166 case SubsectionsViaSymbols:
167 Assembler.setSubsectionsViaSymbols(true);
168 break;
169 }
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000170}
171
172void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
173 const MCValue &Value,
174 bool MakeAbsolute) {
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000175 // Only absolute symbols can be redefined.
176 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
177 "Cannot define a symbol twice!");
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000178
179 llvm_unreachable("FIXME: Not yet implemented!");
180}
181
182void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
183 SymbolAttr Attribute) {
Daniel Dunbara0151d02009-08-24 11:56:58 +0000184 // Indirect symbols are handled differently, to match how 'as' handles
185 // them. This makes writing matching .o files easier.
186 if (Attribute == MCStreamer::IndirectSymbol) {
Daniel Dunbar6d294da2009-08-26 00:18:21 +0000187 // Note that we intentionally cannot use the symbol data here; this is
188 // important for matching the string table that 'as' generates.
Daniel Dunbara0151d02009-08-24 11:56:58 +0000189 IndirectSymbolData ISD;
190 ISD.Symbol = Symbol;
191 ISD.SectionData = CurSectionData;
192 Assembler.getIndirectSymbols().push_back(ISD);
193 return;
194 }
195
Daniel Dunbar30b09422009-08-24 08:40:12 +0000196 // Adding a symbol attribute always introduces the symbol, note that an
197 // important side effect of calling getSymbolData here is to register the
198 // symbol with the assembler.
199 MCSymbolData &SD = getSymbolData(*Symbol);
200
201 // The implementation of symbol attributes is designed to match 'as', but it
202 // leaves much to desired. It doesn't really make sense to arbitrarily add and
203 // remove flags, but 'as' allows this (in particular, see .desc).
204 //
205 // In the future it might be worth trying to make these operations more well
206 // defined.
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000207 switch (Attribute) {
Daniel Dunbara0151d02009-08-24 11:56:58 +0000208 case MCStreamer::IndirectSymbol:
Daniel Dunbar30b09422009-08-24 08:40:12 +0000209 case MCStreamer::Hidden:
210 case MCStreamer::Internal:
211 case MCStreamer::Protected:
212 case MCStreamer::Weak:
213 assert(0 && "Invalid symbol attribute for Mach-O!");
214 break;
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000215
216 case MCStreamer::Global:
217 getSymbolData(*Symbol).setExternal(true);
218 break;
Daniel Dunbar30b09422009-08-24 08:40:12 +0000219
220 case MCStreamer::LazyReference:
221 // FIXME: This requires -dynamic.
222 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
223 if (Symbol->isUndefined())
224 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
225 break;
226
Daniel Dunbar30b09422009-08-24 08:40:12 +0000227 // Since .reference sets the no dead strip bit, it is equivalent to
228 // .no_dead_strip in practice.
229 case MCStreamer::Reference:
230 case MCStreamer::NoDeadStrip:
231 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
232 break;
233
234 case MCStreamer::PrivateExtern:
235 SD.setExternal(true);
236 SD.setPrivateExtern(true);
237 break;
238
239 case MCStreamer::WeakReference:
240 // FIXME: This requires -dynamic.
241 if (Symbol->isUndefined())
242 SD.setFlags(SD.getFlags() | SF_WeakReference);
243 break;
244
245 case MCStreamer::WeakDefinition:
246 // FIXME: 'as' enforces that this is defined and global. The manual claims
247 // it has to be in a coalesced section, but this isn't enforced.
248 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
249 break;
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000250 }
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000251}
252
253void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar30b09422009-08-24 08:40:12 +0000254 // Encode the 'desc' value into the lowest implementation defined bits.
255 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
256 "Invalid .desc value!");
257 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000258}
259
260void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
261 llvm_unreachable("FIXME: Not yet implemented!");
262}
263
264void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
265 unsigned Pow2Alignment,
266 bool IsLocal) {
267 llvm_unreachable("FIXME: Not yet implemented!");
268}
269
270void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
271 unsigned Size, unsigned Pow2Alignment) {
272 llvm_unreachable("FIXME: Not yet implemented!");
273}
274
275void MCMachOStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar197f5632009-08-22 10:13:24 +0000276 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
277 if (!DF)
278 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000279 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000280}
281
282void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar4486ac12009-08-26 13:57:37 +0000283 new MCFillFragment(AddValueSymbols(Value), Size, 1, CurSectionData);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000284}
285
286void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
287 int64_t Value, unsigned ValueSize,
288 unsigned MaxBytesToEmit) {
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000289 if (MaxBytesToEmit == 0)
290 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000291 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
292 CurSectionData);
293
Daniel Dunbar197f5632009-08-22 10:13:24 +0000294 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000295 if (ByteAlignment > CurSectionData->getAlignment())
296 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000297}
298
299void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
300 unsigned char Value) {
Daniel Dunbar4486ac12009-08-26 13:57:37 +0000301 new MCOrgFragment(AddValueSymbols(Offset), Value, CurSectionData);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000302}
303
304void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
305 llvm_unreachable("FIXME: Not yet implemented!");
306}
307
308void MCMachOStreamer::Finish() {
309 Assembler.Finish();
310}
311
312MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
313 return new MCMachOStreamer(Context, OS);
314}