1 //===- lib/MC/MCMachOStreamer.cpp - Mach-O Object Output ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCStreamer.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCCodeEmitter.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCSection.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
24 class MCMachOStreamer : public MCStreamer {
25 /// 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,
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,
39 // Other 'desc' flags.
40 SF_NoDeadStrip = 0x0020,
41 SF_WeakReference = 0x0040,
42 SF_WeakDefinition = 0x0080
46 MCAssembler Assembler;
48 MCCodeEmitter *Emitter;
50 MCSectionData *CurSectionData;
52 DenseMap<const MCSection*, MCSectionData*> SectionMap;
54 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
57 MCFragment *getCurrentFragment() const {
58 assert(CurSectionData && "No current section!");
60 if (!CurSectionData->empty())
61 return &CurSectionData->getFragmentList().back();
66 MCSymbolData &getSymbolData(MCSymbol &Symbol) {
67 MCSymbolData *&Entry = SymbolMap[&Symbol];
70 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
76 MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
77 : MCStreamer(Context), Assembler(_OS), Emitter(_Emitter),
81 const MCValue &AddValueSymbols(const MCValue &Value) {
83 getSymbolData(*const_cast<MCSymbol*>(Value.getSymA()));
85 getSymbolData(*const_cast<MCSymbol*>(Value.getSymB()));
89 /// @name MCStreamer Interface
92 virtual void SwitchSection(const MCSection *Section);
94 virtual void EmitLabel(MCSymbol *Symbol);
96 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
98 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
99 bool MakeAbsolute = false);
101 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
103 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
105 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
107 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
108 unsigned Pow2Alignment);
110 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
111 unsigned Size = 0, unsigned Pow2Alignment = 0);
113 virtual void EmitBytes(const StringRef &Data);
115 virtual void EmitValue(const MCValue &Value, unsigned Size);
117 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
118 unsigned ValueSize = 1,
119 unsigned MaxBytesToEmit = 0);
121 virtual void EmitValueToOffset(const MCValue &Offset,
122 unsigned char Value = 0);
124 virtual void EmitInstruction(const MCInst &Inst);
126 virtual void Finish();
131 } // end anonymous namespace.
133 void MCMachOStreamer::SwitchSection(const MCSection *Section) {
134 assert(Section && "Cannot switch to a null section!");
136 // If already in this section, then this is a noop.
137 if (Section == CurSection) return;
139 CurSection = Section;
140 MCSectionData *&Entry = SectionMap[Section];
143 Entry = new MCSectionData(*Section, &Assembler);
145 CurSectionData = Entry;
148 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
149 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
151 // FIXME: We should also use offsets into Fill fragments.
152 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
154 F = new MCDataFragment(CurSectionData);
156 MCSymbolData &SD = getSymbolData(*Symbol);
157 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
159 SD.setOffset(F->getContents().size());
161 // This causes the reference type and weak reference flags to be cleared.
162 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
164 Symbol->setSection(*CurSection);
167 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
170 llvm_unreachable("FIXME: Not yet implemented!");
172 case SubsectionsViaSymbols:
173 Assembler.setSubsectionsViaSymbols(true);
178 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
179 const MCValue &Value,
181 // Only absolute symbols can be redefined.
182 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
183 "Cannot define a symbol twice!");
185 llvm_unreachable("FIXME: Not yet implemented!");
188 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
189 SymbolAttr Attribute) {
190 // Indirect symbols are handled differently, to match how 'as' handles
191 // them. This makes writing matching .o files easier.
192 if (Attribute == MCStreamer::IndirectSymbol) {
193 // Note that we intentionally cannot use the symbol data here; this is
194 // important for matching the string table that 'as' generates.
195 IndirectSymbolData ISD;
197 ISD.SectionData = CurSectionData;
198 Assembler.getIndirectSymbols().push_back(ISD);
202 // Adding a symbol attribute always introduces the symbol, note that an
203 // important side effect of calling getSymbolData here is to register the
204 // symbol with the assembler.
205 MCSymbolData &SD = getSymbolData(*Symbol);
207 // The implementation of symbol attributes is designed to match 'as', but it
208 // leaves much to desired. It doesn't really make sense to arbitrarily add and
209 // remove flags, but 'as' allows this (in particular, see .desc).
211 // In the future it might be worth trying to make these operations more well
214 case MCStreamer::IndirectSymbol:
215 case MCStreamer::Hidden:
216 case MCStreamer::Internal:
217 case MCStreamer::Protected:
218 case MCStreamer::Weak:
219 assert(0 && "Invalid symbol attribute for Mach-O!");
222 case MCStreamer::Global:
223 getSymbolData(*Symbol).setExternal(true);
226 case MCStreamer::LazyReference:
227 // FIXME: This requires -dynamic.
228 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
229 if (Symbol->isUndefined())
230 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
233 // Since .reference sets the no dead strip bit, it is equivalent to
234 // .no_dead_strip in practice.
235 case MCStreamer::Reference:
236 case MCStreamer::NoDeadStrip:
237 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
240 case MCStreamer::PrivateExtern:
241 SD.setExternal(true);
242 SD.setPrivateExtern(true);
245 case MCStreamer::WeakReference:
246 // FIXME: This requires -dynamic.
247 if (Symbol->isUndefined())
248 SD.setFlags(SD.getFlags() | SF_WeakReference);
251 case MCStreamer::WeakDefinition:
252 // FIXME: 'as' enforces that this is defined and global. The manual claims
253 // it has to be in a coalesced section, but this isn't enforced.
254 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
259 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
260 // Encode the 'desc' value into the lowest implementation defined bits.
261 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
262 "Invalid .desc value!");
263 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
266 void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
267 llvm_unreachable("FIXME: Not yet implemented!");
270 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
271 unsigned Pow2Alignment) {
272 llvm_unreachable("FIXME: Not yet implemented!");
275 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
276 unsigned Size, unsigned Pow2Alignment) {
277 llvm_unreachable("FIXME: Not yet implemented!");
280 void MCMachOStreamer::EmitBytes(const StringRef &Data) {
281 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
283 DF = new MCDataFragment(CurSectionData);
284 DF->getContents().append(Data.begin(), Data.end());
287 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
288 new MCFillFragment(AddValueSymbols(Value), Size, 1, CurSectionData);
291 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
292 int64_t Value, unsigned ValueSize,
293 unsigned MaxBytesToEmit) {
294 if (MaxBytesToEmit == 0)
295 MaxBytesToEmit = ByteAlignment;
296 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
299 // Update the maximum alignment on the current section if necessary.
300 if (ByteAlignment > CurSectionData->getAlignment())
301 CurSectionData->setAlignment(ByteAlignment);
304 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
305 unsigned char Value) {
306 new MCOrgFragment(AddValueSymbols(Offset), Value, CurSectionData);
309 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
311 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
312 if (Inst.getOperand(i).isMCValue())
313 AddValueSymbols(Inst.getOperand(i).getMCValue());
316 llvm_unreachable("no code emitter available!");
318 // FIXME: Relocations!
319 SmallString<256> Code;
320 raw_svector_ostream VecOS(Code);
321 Emitter->EncodeInstruction(Inst, VecOS);
322 EmitBytes(VecOS.str());
325 void MCMachOStreamer::Finish() {
329 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
331 return new MCMachOStreamer(Context, OS, CE);