MCMachOStreamer.cpp revision 212904
1//===- 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 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/MCObjectStreamer.h" 18#include "llvm/MC/MCSection.h" 19#include "llvm/MC/MCSymbol.h" 20#include "llvm/MC/MCMachOSymbolFlags.h" 21#include "llvm/MC/MCSectionMachO.h" 22#include "llvm/MC/MCDwarf.h" 23#include "llvm/Support/ErrorHandling.h" 24#include "llvm/Support/raw_ostream.h" 25#include "llvm/Target/TargetAsmBackend.h" 26 27using namespace llvm; 28 29namespace { 30 31class MCMachOStreamer : public MCObjectStreamer { 32private: 33 void EmitInstToFragment(const MCInst &Inst); 34 void EmitInstToData(const MCInst &Inst); 35 // FIXME: These will likely moved to a better place. 36 void MakeLineEntryForSection(const MCSection *Section); 37 const MCExpr * MakeStartMinusEndExpr(MCSymbol *Start, MCSymbol *End, 38 int IntVal); 39 void EmitDwarfFileTable(void); 40 41public: 42 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, 43 raw_ostream &OS, MCCodeEmitter *Emitter) 44 : MCObjectStreamer(Context, TAB, OS, Emitter) {} 45 46 /// @name MCStreamer Interface 47 /// @{ 48 49 virtual void EmitLabel(MCSymbol *Symbol); 50 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); 51 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); 52 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); 53 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); 54 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 55 unsigned ByteAlignment); 56 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { 57 assert(0 && "macho doesn't support this directive"); 58 } 59 virtual void EmitCOFFSymbolStorageClass(int StorageClass) { 60 assert(0 && "macho doesn't support this directive"); 61 } 62 virtual void EmitCOFFSymbolType(int Type) { 63 assert(0 && "macho doesn't support this directive"); 64 } 65 virtual void EndCOFFSymbolDef() { 66 assert(0 && "macho doesn't support this directive"); 67 } 68 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { 69 assert(0 && "macho doesn't support this directive"); 70 } 71 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { 72 assert(0 && "macho doesn't support this directive"); 73 } 74 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, 75 unsigned Size = 0, unsigned ByteAlignment = 0); 76 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, 77 uint64_t Size, unsigned ByteAlignment = 0); 78 virtual void EmitBytes(StringRef Data, unsigned AddrSpace); 79 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); 80 virtual void EmitGPRel32Value(const MCExpr *Value) { 81 assert(0 && "macho doesn't support this directive"); 82 } 83 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 84 unsigned ValueSize = 1, 85 unsigned MaxBytesToEmit = 0); 86 virtual void EmitCodeAlignment(unsigned ByteAlignment, 87 unsigned MaxBytesToEmit = 0); 88 virtual void EmitValueToOffset(const MCExpr *Offset, 89 unsigned char Value = 0); 90 91 virtual void EmitFileDirective(StringRef Filename) { 92 // FIXME: Just ignore the .file; it isn't important enough to fail the 93 // entire assembly. 94 95 //report_fatal_error("unsupported directive: '.file'"); 96 } 97 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) { 98 // FIXME: Just ignore the .file; it isn't important enough to fail the 99 // entire assembly. 100 101 //report_fatal_error("unsupported directive: '.file'"); 102 } 103 104 virtual void EmitInstruction(const MCInst &Inst); 105 106 virtual void Finish(); 107 108 /// @} 109}; 110 111} // end anonymous namespace. 112 113void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { 114 // TODO: This is almost exactly the same as WinCOFFStreamer. Consider merging 115 // into MCObjectStreamer. 116 assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); 117 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); 118 assert(CurSection && "Cannot emit before setting section!"); 119 120 Symbol->setSection(*CurSection); 121 122 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 123 124 // We have to create a new fragment if this is an atom defining symbol, 125 // fragments cannot span atoms. 126 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol())) 127 new MCDataFragment(getCurrentSectionData()); 128 129 // FIXME: This is wasteful, we don't necessarily need to create a data 130 // fragment. Instead, we should mark the symbol as pointing into the data 131 // fragment if it exists, otherwise we should just queue the label and set its 132 // fragment pointer when we emit the next fragment. 133 MCDataFragment *F = getOrCreateDataFragment(); 134 assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); 135 SD.setFragment(F); 136 SD.setOffset(F->getContents().size()); 137 138 // This causes the reference type flag to be cleared. Darwin 'as' was "trying" 139 // to clear the weak reference and weak definition bits too, but the 140 // implementation was buggy. For now we just try to match 'as', for 141 // diffability. 142 // 143 // FIXME: Cleanup this code, these bits should be emitted based on semantic 144 // properties, not on the order of definition, etc. 145 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask); 146} 147 148void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { 149 switch (Flag) { 150 case MCAF_SubsectionsViaSymbols: 151 getAssembler().setSubsectionsViaSymbols(true); 152 return; 153 } 154 155 assert(0 && "invalid assembler flag!"); 156} 157 158void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { 159 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 160 // MCObjectStreamer. 161 // FIXME: Lift context changes into super class. 162 getAssembler().getOrCreateSymbolData(*Symbol); 163 Symbol->setVariableValue(AddValueSymbols(Value)); 164} 165 166void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 167 MCSymbolAttr Attribute) { 168 // Indirect symbols are handled differently, to match how 'as' handles 169 // them. This makes writing matching .o files easier. 170 if (Attribute == MCSA_IndirectSymbol) { 171 // Note that we intentionally cannot use the symbol data here; this is 172 // important for matching the string table that 'as' generates. 173 IndirectSymbolData ISD; 174 ISD.Symbol = Symbol; 175 ISD.SectionData = getCurrentSectionData(); 176 getAssembler().getIndirectSymbols().push_back(ISD); 177 return; 178 } 179 180 // Adding a symbol attribute always introduces the symbol, note that an 181 // important side effect of calling getOrCreateSymbolData here is to register 182 // the symbol with the assembler. 183 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*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. 191 switch (Attribute) { 192 case MCSA_Invalid: 193 case MCSA_ELF_TypeFunction: 194 case MCSA_ELF_TypeIndFunction: 195 case MCSA_ELF_TypeObject: 196 case MCSA_ELF_TypeTLS: 197 case MCSA_ELF_TypeCommon: 198 case MCSA_ELF_TypeNoType: 199 case MCSA_IndirectSymbol: 200 case MCSA_Hidden: 201 case MCSA_Internal: 202 case MCSA_Protected: 203 case MCSA_Weak: 204 case MCSA_Local: 205 assert(0 && "Invalid symbol attribute for Mach-O!"); 206 break; 207 208 case MCSA_Global: 209 SD.setExternal(true); 210 // This effectively clears the undefined lazy bit, in Darwin 'as', although 211 // it isn't very consistent because it implements this as part of symbol 212 // lookup. 213 // 214 // FIXME: Cleanup this code, these bits should be emitted based on semantic 215 // properties, not on the order of definition, etc. 216 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy); 217 break; 218 219 case MCSA_LazyReference: 220 // FIXME: This requires -dynamic. 221 SD.setFlags(SD.getFlags() | SF_NoDeadStrip); 222 if (Symbol->isUndefined()) 223 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy); 224 break; 225 226 // Since .reference sets the no dead strip bit, it is equivalent to 227 // .no_dead_strip in practice. 228 case MCSA_Reference: 229 case MCSA_NoDeadStrip: 230 SD.setFlags(SD.getFlags() | SF_NoDeadStrip); 231 break; 232 233 case MCSA_PrivateExtern: 234 SD.setExternal(true); 235 SD.setPrivateExtern(true); 236 break; 237 238 case MCSA_WeakReference: 239 // FIXME: This requires -dynamic. 240 if (Symbol->isUndefined()) 241 SD.setFlags(SD.getFlags() | SF_WeakReference); 242 break; 243 244 case MCSA_WeakDefinition: 245 // FIXME: 'as' enforces that this is defined and global. The manual claims 246 // it has to be in a coalesced section, but this isn't enforced. 247 SD.setFlags(SD.getFlags() | SF_WeakDefinition); 248 break; 249 250 case MCSA_WeakDefAutoPrivate: 251 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference); 252 break; 253 } 254} 255 256void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { 257 // Encode the 'desc' value into the lowest implementation defined bits. 258 assert(DescValue == (DescValue & SF_DescFlagsMask) && 259 "Invalid .desc value!"); 260 getAssembler().getOrCreateSymbolData(*Symbol).setFlags( 261 DescValue & SF_DescFlagsMask); 262} 263 264void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 265 unsigned ByteAlignment) { 266 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. 267 assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); 268 269 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 270 SD.setExternal(true); 271 SD.setCommon(Size, ByteAlignment); 272} 273 274void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, 275 unsigned Size, unsigned ByteAlignment) { 276 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section); 277 278 // The symbol may not be present, which only creates the section. 279 if (!Symbol) 280 return; 281 282 // FIXME: Assert that this section has the zerofill type. 283 284 assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); 285 286 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 287 288 // Emit an align fragment if necessary. 289 if (ByteAlignment != 1) 290 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData); 291 292 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); 293 SD.setFragment(F); 294 295 Symbol->setSection(*Section); 296 297 // Update the maximum alignment on the zero fill section if necessary. 298 if (ByteAlignment > SectData.getAlignment()) 299 SectData.setAlignment(ByteAlignment); 300} 301 302// This should always be called with the thread local bss section. Like the 303// .zerofill directive this doesn't actually switch sections on us. 304void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, 305 uint64_t Size, unsigned ByteAlignment) { 306 EmitZerofill(Section, Symbol, Size, ByteAlignment); 307 return; 308} 309 310void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { 311 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 312 // MCObjectStreamer. 313 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); 314} 315 316void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size, 317 unsigned AddrSpace) { 318 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 319 // MCObjectStreamer. 320 MCDataFragment *DF = getOrCreateDataFragment(); 321 322 // Avoid fixups when possible. 323 int64_t AbsValue; 324 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) { 325 // FIXME: Endianness assumption. 326 for (unsigned i = 0; i != Size; ++i) 327 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8))); 328 } else { 329 DF->addFixup(MCFixup::Create(DF->getContents().size(), 330 AddValueSymbols(Value), 331 MCFixup::getKindForSize(Size))); 332 DF->getContents().resize(DF->getContents().size() + Size, 0); 333 } 334} 335 336void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, 337 int64_t Value, unsigned ValueSize, 338 unsigned MaxBytesToEmit) { 339 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 340 // MCObjectStreamer. 341 if (MaxBytesToEmit == 0) 342 MaxBytesToEmit = ByteAlignment; 343 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, 344 getCurrentSectionData()); 345 346 // Update the maximum alignment on the current section if necessary. 347 if (ByteAlignment > getCurrentSectionData()->getAlignment()) 348 getCurrentSectionData()->setAlignment(ByteAlignment); 349} 350 351void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment, 352 unsigned MaxBytesToEmit) { 353 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 354 // MCObjectStreamer. 355 if (MaxBytesToEmit == 0) 356 MaxBytesToEmit = ByteAlignment; 357 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, 358 getCurrentSectionData()); 359 F->setEmitNops(true); 360 361 // Update the maximum alignment on the current section if necessary. 362 if (ByteAlignment > getCurrentSectionData()->getAlignment()) 363 getCurrentSectionData()->setAlignment(ByteAlignment); 364} 365 366void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset, 367 unsigned char Value) { 368 new MCOrgFragment(*Offset, Value, getCurrentSectionData()); 369} 370 371void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) { 372 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData()); 373 374 // Add the fixups and data. 375 // 376 // FIXME: Revisit this design decision when relaxation is done, we may be 377 // able to get away with not storing any extra data in the MCInst. 378 SmallVector<MCFixup, 4> Fixups; 379 SmallString<256> Code; 380 raw_svector_ostream VecOS(Code); 381 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); 382 VecOS.flush(); 383 384 IF->getCode() = Code; 385 IF->getFixups() = Fixups; 386} 387 388void MCMachOStreamer::EmitInstToData(const MCInst &Inst) { 389 MCDataFragment *DF = getOrCreateDataFragment(); 390 391 SmallVector<MCFixup, 4> Fixups; 392 SmallString<256> Code; 393 raw_svector_ostream VecOS(Code); 394 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); 395 VecOS.flush(); 396 397 // Add the fixups and data. 398 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { 399 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); 400 DF->addFixup(Fixups[i]); 401 } 402 DF->getContents().append(Code.begin(), Code.end()); 403} 404 405void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { 406 // Scan for values. 407 for (unsigned i = Inst.getNumOperands(); i--; ) 408 if (Inst.getOperand(i).isExpr()) 409 AddValueSymbols(Inst.getOperand(i).getExpr()); 410 411 getCurrentSectionData()->setHasInstructions(true); 412 413 // Now that a machine instruction has been assembled into this section, make 414 // a line entry for any .loc directive that has been seen. 415 MakeLineEntryForSection(getCurrentSection()); 416 417 // If this instruction doesn't need relaxation, just emit it as data. 418 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) { 419 EmitInstToData(Inst); 420 return; 421 } 422 423 // Otherwise, if we are relaxing everything, relax the instruction as much as 424 // possible and emit it as data. 425 if (getAssembler().getRelaxAll()) { 426 MCInst Relaxed; 427 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed); 428 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed)) 429 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed); 430 EmitInstToData(Relaxed); 431 return; 432 } 433 434 // Otherwise emit to a separate fragment. 435 EmitInstToFragment(Inst); 436} 437 438// 439// This is called when an instruction is assembled into the specified section 440// and if there is information from the last .loc directive that has yet to have 441// a line entry made for it is made. 442// 443void MCMachOStreamer::MakeLineEntryForSection(const MCSection *Section) { 444 if (!getContext().getDwarfLocSeen()) 445 return; 446 447 // Create a symbol at in the current section for use in the line entry. 448 MCSymbol *LineSym = getContext().CreateTempSymbol(); 449 // Set the value of the symbol to use for the MCLineEntry. 450 EmitLabel(LineSym); 451 452 // Get the current .loc info saved in the context. 453 const MCDwarfLoc &DwarfLoc = getContext().getCurrentDwarfLoc(); 454 455 // Create a (local) line entry with the symbol and the current .loc info. 456 MCLineEntry LineEntry(LineSym, DwarfLoc); 457 458 // clear DwarfLocSeen saying the current .loc info is now used. 459 getContext().clearDwarfLocSeen(); 460 461 // Get the MCLineSection for this section, if one does not exist for this 462 // section create it. 463 DenseMap<const MCSection *, MCLineSection *> &MCLineSections = 464 getContext().getMCLineSections(); 465 MCLineSection *LineSection = MCLineSections[Section]; 466 if (!LineSection) { 467 // Create a new MCLineSection. This will be deleted after the dwarf line 468 // table is created using it by iterating through the MCLineSections 469 // DenseMap. 470 LineSection = new MCLineSection; 471 // Save a pointer to the new LineSection into the MCLineSections DenseMap. 472 MCLineSections[Section] = LineSection; 473 } 474 475 // Add the line entry to this section's entries. 476 LineSection->addLineEntry(LineEntry); 477} 478 479// 480// This helper routine returns an expression of End - Start + IntVal for use 481// by EmitDwarfFileTable() below. 482// 483const MCExpr * MCMachOStreamer::MakeStartMinusEndExpr(MCSymbol *Start, 484 MCSymbol *End, 485 int IntVal) { 486 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 487 const MCExpr *Res = 488 MCSymbolRefExpr::Create(End, Variant, getContext()); 489 const MCExpr *RHS = 490 MCSymbolRefExpr::Create(Start, Variant, getContext()); 491 const MCExpr *Res1 = 492 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS,getContext()); 493 const MCExpr *Res2 = 494 MCConstantExpr::Create(IntVal, getContext()); 495 const MCExpr *Res3 = 496 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, getContext()); 497 return Res3; 498} 499 500// 501// This emits the Dwarf file (and eventually the line) table. 502// 503void MCMachOStreamer::EmitDwarfFileTable(void) { 504 // For now make sure we don't put out the Dwarf file table if no .file 505 // directives were seen. 506 const std::vector<MCDwarfFile *> &MCDwarfFiles = 507 getContext().getMCDwarfFiles(); 508 if (MCDwarfFiles.size() == 0) 509 return; 510 511 // This is the Mach-O section, for ELF it is the .debug_line section. 512 SwitchSection(getContext().getMachOSection("__DWARF", "__debug_line", 513 MCSectionMachO::S_ATTR_DEBUG, 514 0, SectionKind::getDataRelLocal())); 515 516 // Create a symbol at the beginning of this section. 517 MCSymbol *LineStartSym = getContext().CreateTempSymbol(); 518 // Set the value of the symbol, as we are at the start of the section. 519 EmitLabel(LineStartSym); 520 521 // Create a symbol for the end of the section (to be set when we get there). 522 MCSymbol *LineEndSym = getContext().CreateTempSymbol(); 523 524 // The first 4 bytes is the total length of the information for this 525 // compilation unit (not including these 4 bytes for the length). 526 EmitValue(MakeStartMinusEndExpr(LineStartSym, LineEndSym, 4), 4, 0); 527 528 // Next 2 bytes is the Version, which is Dwarf 2. 529 EmitIntValue(2, 2); 530 531 // Create a symbol for the end of the prologue (to be set when we get there). 532 MCSymbol *ProEndSym = getContext().CreateTempSymbol(); // Lprologue_end 533 534 // Length of the prologue, is the next 4 bytes. Which is the start of the 535 // section to the end of the prologue. Not including the 4 bytes for the 536 // total length, the 2 bytes for the version, and these 4 bytes for the 537 // length of the prologue. 538 EmitValue(MakeStartMinusEndExpr(LineStartSym, ProEndSym, (4 + 2 + 4)), 4, 0); 539 540 // Parameters of the state machine, are next. 541 // Define the architecture-dependent minimum instruction length (in 542 // bytes). This value should be rather too small than too big. */ 543 // DWARF2_LINE_MIN_INSN_LENGTH 544 EmitIntValue(1, 1); 545 // Flag that indicates the initial value of the is_stmt_start flag. 546 // DWARF2_LINE_DEFAULT_IS_STMT 547 EmitIntValue(1, 1); 548 // Minimum line offset in a special line info. opcode. This value 549 // was chosen to give a reasonable range of values. */ 550 // DWARF2_LINE_BASE 551 EmitIntValue(uint64_t(-5), 1); 552 // Range of line offsets in a special line info. opcode. 553 // DWARF2_LINE_RANGE 554 EmitIntValue(14, 1); 555 // First special line opcode - leave room for the standard opcodes. 556 // DWARF2_LINE_OPCODE_BASE 557 EmitIntValue(13, 1); 558 559 // Standard opcode lengths 560 EmitIntValue(0, 1); // length of DW_LNS_copy 561 EmitIntValue(1, 1); // length of DW_LNS_advance_pc 562 EmitIntValue(1, 1); // length of DW_LNS_advance_line 563 EmitIntValue(1, 1); // length of DW_LNS_set_file 564 EmitIntValue(1, 1); // length of DW_LNS_set_column 565 EmitIntValue(0, 1); // length of DW_LNS_negate_stmt 566 EmitIntValue(0, 1); // length of DW_LNS_set_basic_block 567 EmitIntValue(0, 1); // length of DW_LNS_const_add_pc 568 EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc 569 EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end 570 EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin 571 EmitIntValue(1, 1); // DW_LNS_set_isa 572 573 // Put out the directory and file tables. 574 575 // First the directory table. 576 const std::vector<StringRef> &MCDwarfDirs = 577 getContext().getMCDwarfDirs(); 578 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) { 579 EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName 580 EmitBytes(StringRef("\0", 1), 0); // the null termination of the string 581 } 582 EmitIntValue(0, 1); // Terminate the directory list 583 584 // Second the file table. 585 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { 586 EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName 587 EmitBytes(StringRef("\0", 1), 0); // the null termination of the string 588 // FIXME the Directory number should be a .uleb128 not a .byte 589 EmitIntValue(MCDwarfFiles[i]->getDirIndex(), 1); 590 EmitIntValue(0, 1); // last modification timestamp (always 0) 591 EmitIntValue(0, 1); // filesize (always 0) 592 } 593 EmitIntValue(0, 1); // Terminate the file list 594 595 // This is the end of the prologue, so set the value of the symbol at the 596 // end of the prologue (that was used in a previous expression). 597 EmitLabel(ProEndSym); 598 599 // TODO: This is the point where the line tables would be emitted. 600 601 // Delete the MCLineSections that were created in 602 // MCMachOStreamer::MakeLineEntryForSection() and used to emit the line 603 // tables. 604 DenseMap<const MCSection *, MCLineSection *> &MCLineSections = 605 getContext().getMCLineSections(); 606 for (DenseMap<const MCSection *, MCLineSection *>::iterator it = 607 MCLineSections.begin(), ie = MCLineSections.end(); it != ie; ++it) { 608 delete it->second; 609 } 610 611 // If there are no line tables emited then we emit: 612 // The following DW_LNE_set_address sequence to set the address to zero 613 // TODO test for 32-bit or 64-bit output 614 // This is the sequence for 32-bit code 615 EmitIntValue(0, 1); 616 EmitIntValue(5, 1); 617 EmitIntValue(2, 1); 618 EmitIntValue(0, 1); 619 EmitIntValue(0, 1); 620 EmitIntValue(0, 1); 621 EmitIntValue(0, 1); 622 623 // Lastly emit the DW_LNE_end_sequence which consists of 3 bytes '00 01 01' 624 // (00 is the code for extended opcodes, followed by a ULEB128 length of the 625 // extended opcode (01), and the DW_LNE_end_sequence (01). 626 EmitIntValue(0, 1); // DW_LNS_extended_op 627 EmitIntValue(1, 1); // ULEB128 length of the extended opcode 628 EmitIntValue(1, 1); // DW_LNE_end_sequence 629 630 // This is the end of the section, so set the value of the symbol at the end 631 // of this section (that was used in a previous expression). 632 EmitLabel(LineEndSym); 633} 634 635void MCMachOStreamer::Finish() { 636 // Dump out the dwarf file and directory tables (soon to include line table) 637 EmitDwarfFileTable(); 638 639 // We have to set the fragment atom associations so we can relax properly for 640 // Mach-O. 641 642 // First, scan the symbol table to build a lookup table from fragments to 643 // defining symbols. 644 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap; 645 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(), 646 ie = getAssembler().symbol_end(); it != ie; ++it) { 647 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) && 648 it->getFragment()) { 649 // An atom defining symbol should never be internal to a fragment. 650 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!"); 651 DefiningSymbolMap[it->getFragment()] = it; 652 } 653 } 654 655 // Set the fragment atom associations by tracking the last seen atom defining 656 // symbol. 657 for (MCAssembler::iterator it = getAssembler().begin(), 658 ie = getAssembler().end(); it != ie; ++it) { 659 MCSymbolData *CurrentAtom = 0; 660 for (MCSectionData::iterator it2 = it->begin(), 661 ie2 = it->end(); it2 != ie2; ++it2) { 662 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2)) 663 CurrentAtom = SD; 664 it2->setAtom(CurrentAtom); 665 } 666 } 667 668 this->MCObjectStreamer::Finish(); 669} 670 671MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, 672 raw_ostream &OS, MCCodeEmitter *CE, 673 bool RelaxAll) { 674 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE); 675 if (RelaxAll) 676 S->getAssembler().setRelaxAll(true); 677 return S; 678} 679