blob: de1c6ed712246a59fcced0585c0e9c43a2082202 [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
75 /// @name MCStreamer Interface
76 /// @{
77
78 virtual void SwitchSection(const MCSection *Section);
79
80 virtual void EmitLabel(MCSymbol *Symbol);
81
82 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
83
84 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
85 bool MakeAbsolute = false);
86
87 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
88
89 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
90
91 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
92
93 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
94 unsigned Pow2Alignment, bool IsLocal);
95
96 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
97 unsigned Size = 0, unsigned Pow2Alignment = 0);
98
99 virtual void EmitBytes(const StringRef &Data);
100
101 virtual void EmitValue(const MCValue &Value, unsigned Size);
102
103 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
104 unsigned ValueSize = 1,
105 unsigned MaxBytesToEmit = 0);
106
107 virtual void EmitValueToOffset(const MCValue &Offset,
108 unsigned char Value = 0);
109
110 virtual void EmitInstruction(const MCInst &Inst);
111
112 virtual void Finish();
113
114 /// @}
115};
116
117} // end anonymous namespace.
118
119void MCMachOStreamer::SwitchSection(const MCSection *Section) {
120 assert(Section && "Cannot switch to a null section!");
Chris Lattnerbf7a11f2009-08-22 19:35:08 +0000121
122 // If already in this section, then this is a noop.
123 if (Section == CurSection) return;
124
125 CurSection = Section;
126 MCSectionData *&Entry = SectionMap[Section];
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000127
Chris Lattnerbf7a11f2009-08-22 19:35:08 +0000128 if (!Entry)
129 Entry = new MCSectionData(*Section, &Assembler);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000130
Chris Lattnerbf7a11f2009-08-22 19:35:08 +0000131 CurSectionData = Entry;
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000132}
133
134void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000135 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000136
Daniel Dunbar197f5632009-08-22 10:13:24 +0000137 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
138 if (!F)
139 F = new MCDataFragment(CurSectionData);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000140
Daniel Dunbar197f5632009-08-22 10:13:24 +0000141 MCSymbolData &SD = getSymbolData(*Symbol);
142 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
143 SD.setFragment(F);
144 SD.setOffset(F->getContents().size());
Daniel Dunbar30b09422009-08-24 08:40:12 +0000145
146 // This causes the reference type and weak reference flags to be cleared.
147 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbar197f5632009-08-22 10:13:24 +0000148
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000149 Symbol->setSection(*CurSection);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000150}
151
152void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
153 llvm_unreachable("FIXME: Not yet implemented!");
154}
155
156void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
157 const MCValue &Value,
158 bool MakeAbsolute) {
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000159 // Only absolute symbols can be redefined.
160 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
161 "Cannot define a symbol twice!");
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000162
163 llvm_unreachable("FIXME: Not yet implemented!");
164}
165
166void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
167 SymbolAttr Attribute) {
Daniel Dunbara0151d02009-08-24 11:56:58 +0000168 // Indirect symbols are handled differently, to match how 'as' handles
169 // them. This makes writing matching .o files easier.
170 if (Attribute == MCStreamer::IndirectSymbol) {
Daniel Dunbar6d294da2009-08-26 00:18:21 +0000171 // Note that we intentionally cannot use the symbol data here; this is
172 // important for matching the string table that 'as' generates.
Daniel Dunbara0151d02009-08-24 11:56:58 +0000173 IndirectSymbolData ISD;
174 ISD.Symbol = Symbol;
175 ISD.SectionData = CurSectionData;
176 Assembler.getIndirectSymbols().push_back(ISD);
177 return;
178 }
179
Daniel Dunbar30b09422009-08-24 08:40:12 +0000180 // Adding a symbol attribute always introduces the symbol, note that an
181 // important side effect of calling getSymbolData here is to register the
182 // symbol with the assembler.
183 MCSymbolData &SD = getSymbolData(*Symbol);
184
185 // The implementation of symbol attributes is designed to match 'as', but it
186 // leaves much to desired. It doesn't really make sense to arbitrarily add and
187 // remove flags, but 'as' allows this (in particular, see .desc).
188 //
189 // In the future it might be worth trying to make these operations more well
190 // defined.
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000191 switch (Attribute) {
Daniel Dunbara0151d02009-08-24 11:56:58 +0000192 case MCStreamer::IndirectSymbol:
Daniel Dunbar30b09422009-08-24 08:40:12 +0000193 case MCStreamer::Hidden:
194 case MCStreamer::Internal:
195 case MCStreamer::Protected:
196 case MCStreamer::Weak:
197 assert(0 && "Invalid symbol attribute for Mach-O!");
198 break;
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000199
200 case MCStreamer::Global:
201 getSymbolData(*Symbol).setExternal(true);
202 break;
Daniel Dunbar30b09422009-08-24 08:40:12 +0000203
204 case MCStreamer::LazyReference:
205 // FIXME: This requires -dynamic.
206 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
207 if (Symbol->isUndefined())
208 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
209 break;
210
Daniel Dunbar30b09422009-08-24 08:40:12 +0000211 // Since .reference sets the no dead strip bit, it is equivalent to
212 // .no_dead_strip in practice.
213 case MCStreamer::Reference:
214 case MCStreamer::NoDeadStrip:
215 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
216 break;
217
218 case MCStreamer::PrivateExtern:
219 SD.setExternal(true);
220 SD.setPrivateExtern(true);
221 break;
222
223 case MCStreamer::WeakReference:
224 // FIXME: This requires -dynamic.
225 if (Symbol->isUndefined())
226 SD.setFlags(SD.getFlags() | SF_WeakReference);
227 break;
228
229 case MCStreamer::WeakDefinition:
230 // FIXME: 'as' enforces that this is defined and global. The manual claims
231 // it has to be in a coalesced section, but this isn't enforced.
232 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
233 break;
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000234 }
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000235}
236
237void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar30b09422009-08-24 08:40:12 +0000238 // Encode the 'desc' value into the lowest implementation defined bits.
239 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
240 "Invalid .desc value!");
241 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000242}
243
244void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
245 llvm_unreachable("FIXME: Not yet implemented!");
246}
247
248void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
249 unsigned Pow2Alignment,
250 bool IsLocal) {
251 llvm_unreachable("FIXME: Not yet implemented!");
252}
253
254void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
255 unsigned Size, unsigned Pow2Alignment) {
256 llvm_unreachable("FIXME: Not yet implemented!");
257}
258
259void MCMachOStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar197f5632009-08-22 10:13:24 +0000260 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
261 if (!DF)
262 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000263 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000264}
265
266void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000267 new MCFillFragment(Value, Size, 1, CurSectionData);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000268}
269
270void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
271 int64_t Value, unsigned ValueSize,
272 unsigned MaxBytesToEmit) {
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000273 if (MaxBytesToEmit == 0)
274 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000275 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
276 CurSectionData);
277
Daniel Dunbar197f5632009-08-22 10:13:24 +0000278 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000279 if (ByteAlignment > CurSectionData->getAlignment())
280 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000281}
282
283void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
284 unsigned char Value) {
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000285 new MCOrgFragment(Offset, Value, CurSectionData);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000286}
287
288void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
289 llvm_unreachable("FIXME: Not yet implemented!");
290}
291
292void MCMachOStreamer::Finish() {
293 Assembler.Finish();
294}
295
296MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
297 return new MCMachOStreamer(Context, OS);
298}