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/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
25 class MCMachOStreamer : public MCStreamer {
26 /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
27 /// 16 bits of the implementation defined flags.
28 enum SymbolFlags { // See <mach-o/nlist.h>.
29 SF_DescFlagsMask = 0xFFFF,
31 // Reference type flags.
32 SF_ReferenceTypeMask = 0x0007,
33 SF_ReferenceTypeUndefinedNonLazy = 0x0000,
34 SF_ReferenceTypeUndefinedLazy = 0x0001,
35 SF_ReferenceTypeDefined = 0x0002,
36 SF_ReferenceTypePrivateDefined = 0x0003,
37 SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
38 SF_ReferenceTypePrivateUndefinedLazy = 0x0005,
40 // Other 'desc' flags.
41 SF_NoDeadStrip = 0x0020,
42 SF_WeakReference = 0x0040,
43 SF_WeakDefinition = 0x0080
47 MCAssembler Assembler;
49 MCCodeEmitter *Emitter;
51 MCSectionData *CurSectionData;
53 DenseMap<const MCSection*, MCSectionData*> SectionMap;
55 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
58 MCFragment *getCurrentFragment() const {
59 assert(CurSectionData && "No current section!");
61 if (!CurSectionData->empty())
62 return &CurSectionData->getFragmentList().back();
67 MCSectionData &getSectionData(const MCSection &Section) {
68 MCSectionData *&Entry = SectionMap[&Section];
71 Entry = new MCSectionData(Section, &Assembler);
76 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
77 MCSymbolData *&Entry = SymbolMap[&Symbol];
80 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
86 MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
87 : MCStreamer(Context), Assembler(Context, _OS), Emitter(_Emitter),
91 const MCValue &AddValueSymbols(const MCValue &Value) {
93 getSymbolData(*const_cast<MCSymbol*>(Value.getSymA()));
95 getSymbolData(*const_cast<MCSymbol*>(Value.getSymB()));
99 const MCExpr *AddValueSymbols(const MCExpr *Value) {
100 switch (Value->getKind()) {
101 case MCExpr::Constant:
104 case MCExpr::Binary: {
105 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
106 AddValueSymbols(BE->getLHS());
107 AddValueSymbols(BE->getRHS());
111 case MCExpr::SymbolRef:
112 getSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
116 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
123 /// @name MCStreamer Interface
126 virtual void SwitchSection(const MCSection *Section);
128 virtual void EmitLabel(MCSymbol *Symbol);
130 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
132 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
133 bool MakeAbsolute = false);
135 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
137 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
139 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
140 unsigned ByteAlignment);
142 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
143 unsigned Size = 0, unsigned ByteAlignment = 0);
145 virtual void EmitBytes(const StringRef &Data);
147 virtual void EmitValue(const MCValue &Value, unsigned Size);
149 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
150 unsigned ValueSize = 1,
151 unsigned MaxBytesToEmit = 0);
153 virtual void EmitValueToOffset(const MCValue &Offset,
154 unsigned char Value = 0);
156 virtual void EmitInstruction(const MCInst &Inst);
158 virtual void Finish();
163 } // end anonymous namespace.
165 void MCMachOStreamer::SwitchSection(const MCSection *Section) {
166 assert(Section && "Cannot switch to a null section!");
168 // If already in this section, then this is a noop.
169 if (Section == CurSection) return;
171 CurSection = Section;
172 CurSectionData = &getSectionData(*Section);
175 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
176 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
178 // FIXME: We should also use offsets into Fill fragments.
179 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
181 F = new MCDataFragment(CurSectionData);
183 MCSymbolData &SD = getSymbolData(*Symbol);
184 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
186 SD.setOffset(F->getContents().size());
188 // This causes the reference type and weak reference flags to be cleared.
189 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
191 Symbol->setSection(*CurSection);
194 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
196 case SubsectionsViaSymbols:
197 Assembler.setSubsectionsViaSymbols(true);
201 assert(0 && "invalid assembler flag!");
204 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
205 const MCValue &Value,
207 // Only absolute symbols can be redefined.
208 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
209 "Cannot define a symbol twice!");
211 llvm_unreachable("FIXME: Not yet implemented!");
214 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
215 SymbolAttr Attribute) {
216 // Indirect symbols are handled differently, to match how 'as' handles
217 // them. This makes writing matching .o files easier.
218 if (Attribute == MCStreamer::IndirectSymbol) {
219 // Note that we intentionally cannot use the symbol data here; this is
220 // important for matching the string table that 'as' generates.
221 IndirectSymbolData ISD;
223 ISD.SectionData = CurSectionData;
224 Assembler.getIndirectSymbols().push_back(ISD);
228 // Adding a symbol attribute always introduces the symbol, note that an
229 // important side effect of calling getSymbolData here is to register the
230 // symbol with the assembler.
231 MCSymbolData &SD = getSymbolData(*Symbol);
233 // The implementation of symbol attributes is designed to match 'as', but it
234 // leaves much to desired. It doesn't really make sense to arbitrarily add and
235 // remove flags, but 'as' allows this (in particular, see .desc).
237 // In the future it might be worth trying to make these operations more well
240 case MCStreamer::IndirectSymbol:
241 case MCStreamer::Hidden:
242 case MCStreamer::Internal:
243 case MCStreamer::Protected:
244 case MCStreamer::Weak:
245 assert(0 && "Invalid symbol attribute for Mach-O!");
248 case MCStreamer::Global:
249 SD.setExternal(true);
252 case MCStreamer::LazyReference:
253 // FIXME: This requires -dynamic.
254 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
255 if (Symbol->isUndefined())
256 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
259 // Since .reference sets the no dead strip bit, it is equivalent to
260 // .no_dead_strip in practice.
261 case MCStreamer::Reference:
262 case MCStreamer::NoDeadStrip:
263 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
266 case MCStreamer::PrivateExtern:
267 SD.setExternal(true);
268 SD.setPrivateExtern(true);
271 case MCStreamer::WeakReference:
272 // FIXME: This requires -dynamic.
273 if (Symbol->isUndefined())
274 SD.setFlags(SD.getFlags() | SF_WeakReference);
277 case MCStreamer::WeakDefinition:
278 // FIXME: 'as' enforces that this is defined and global. The manual claims
279 // it has to be in a coalesced section, but this isn't enforced.
280 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
285 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
286 // Encode the 'desc' value into the lowest implementation defined bits.
287 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
288 "Invalid .desc value!");
289 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
292 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
293 unsigned ByteAlignment) {
294 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
295 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
297 MCSymbolData &SD = getSymbolData(*Symbol);
298 SD.setExternal(true);
299 SD.setCommon(Size, ByteAlignment);
302 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
303 unsigned Size, unsigned ByteAlignment) {
304 MCSectionData &SectData = getSectionData(*Section);
306 // The symbol may not be present, which only creates the section.
310 // FIXME: Assert that this section has the zerofill type.
312 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
314 MCSymbolData &SD = getSymbolData(*Symbol);
316 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
319 Symbol->setSection(*Section);
321 // Update the maximum alignment on the zero fill section if necessary.
322 if (ByteAlignment > SectData.getAlignment())
323 SectData.setAlignment(ByteAlignment);
326 void MCMachOStreamer::EmitBytes(const StringRef &Data) {
327 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
329 DF = new MCDataFragment(CurSectionData);
330 DF->getContents().append(Data.begin(), Data.end());
333 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
334 new MCFillFragment(AddValueSymbols(Value), Size, 1, CurSectionData);
337 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
338 int64_t Value, unsigned ValueSize,
339 unsigned MaxBytesToEmit) {
340 if (MaxBytesToEmit == 0)
341 MaxBytesToEmit = ByteAlignment;
342 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
345 // Update the maximum alignment on the current section if necessary.
346 if (ByteAlignment > CurSectionData->getAlignment())
347 CurSectionData->setAlignment(ByteAlignment);
350 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
351 unsigned char Value) {
352 new MCOrgFragment(AddValueSymbols(Offset), Value, CurSectionData);
355 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
357 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
358 if (Inst.getOperand(i).isExpr())
359 AddValueSymbols(Inst.getOperand(i).getExpr());
362 llvm_unreachable("no code emitter available!");
364 // FIXME: Relocations!
365 SmallString<256> Code;
366 raw_svector_ostream VecOS(Code);
367 Emitter->EncodeInstruction(Inst, VecOS);
368 EmitBytes(VecOS.str());
371 void MCMachOStreamer::Finish() {
375 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
377 return new MCMachOStreamer(Context, OS, CE);