1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===//
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 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to X86 machine code.
13 //===----------------------------------------------------------------------===//
15 #include "X86AsmPrinter.h"
16 #include "InstPrinter/X86ATTInstPrinter.h"
17 #include "MCTargetDesc/X86BaseInfo.h"
18 #include "X86InstrInfo.h"
19 #include "X86MachineFunctionInfo.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
23 #include "llvm/CodeGen/MachineValueType.h"
24 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
25 #include "llvm/IR/DebugInfo.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Mangler.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/Type.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCContext.h"
32 #include "llvm/MC/MCExpr.h"
33 #include "llvm/MC/MCSectionCOFF.h"
34 #include "llvm/MC/MCSectionMachO.h"
35 #include "llvm/MC/MCStreamer.h"
36 #include "llvm/MC/MCSymbol.h"
37 #include "llvm/Support/COFF.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/TargetRegistry.h"
43 //===----------------------------------------------------------------------===//
44 // Primitive Helper Functions.
45 //===----------------------------------------------------------------------===//
47 /// runOnMachineFunction - Emit the function body.
49 bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
50 SetupMachineFunction(MF);
52 if (Subtarget->isTargetCOFF()) {
53 bool Intrn = MF.getFunction()->hasInternalLinkage();
54 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
55 OutStreamer.EmitCOFFSymbolStorageClass(Intrn ? COFF::IMAGE_SYM_CLASS_STATIC
56 : COFF::IMAGE_SYM_CLASS_EXTERNAL);
57 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
58 << COFF::SCT_COMPLEX_TYPE_SHIFT);
59 OutStreamer.EndCOFFSymbolDef();
62 // Have common code print out the function header with linkage info etc.
65 // Emit the rest of the function body.
68 // We didn't modify anything.
72 /// printSymbolOperand - Print a raw symbol reference operand. This handles
73 /// jump tables, constant pools, global address and external symbols, all of
74 /// which print to a label with various suffixes for relocation types etc.
75 static void printSymbolOperand(X86AsmPrinter &P, const MachineOperand &MO,
77 switch (MO.getType()) {
78 default: llvm_unreachable("unknown symbol type!");
79 case MachineOperand::MO_ConstantPoolIndex:
80 O << *P.GetCPISymbol(MO.getIndex());
81 P.printOffset(MO.getOffset(), O);
83 case MachineOperand::MO_GlobalAddress: {
84 const GlobalValue *GV = MO.getGlobal();
87 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
88 GVSym = P.getSymbolWithGlobalValueBase(GV, "$stub");
89 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
90 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
91 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
92 GVSym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
94 GVSym = P.getSymbol(GV);
96 // Handle dllimport linkage.
97 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
99 P.OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
101 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
102 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
103 MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
104 MachineModuleInfoImpl::StubValueTy &StubSym =
105 P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
106 if (!StubSym.getPointer())
107 StubSym = MachineModuleInfoImpl::
108 StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
109 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
110 MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
111 MachineModuleInfoImpl::StubValueTy &StubSym =
112 P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(
114 if (!StubSym.getPointer())
115 StubSym = MachineModuleInfoImpl::
116 StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
117 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
118 MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$stub");
119 MachineModuleInfoImpl::StubValueTy &StubSym =
120 P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
121 if (!StubSym.getPointer())
122 StubSym = MachineModuleInfoImpl::
123 StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
126 // If the name begins with a dollar-sign, enclose it in parens. We do this
127 // to avoid having it look like an integer immediate to the assembler.
128 if (GVSym->getName()[0] != '$')
131 O << '(' << *GVSym << ')';
132 P.printOffset(MO.getOffset(), O);
137 switch (MO.getTargetFlags()) {
139 llvm_unreachable("Unknown target flag on GV operand");
140 case X86II::MO_NO_FLAG: // No flag.
142 case X86II::MO_DARWIN_NONLAZY:
143 case X86II::MO_DLLIMPORT:
144 case X86II::MO_DARWIN_STUB:
145 // These affect the name of the symbol, not any suffix.
147 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
148 O << " + [.-" << *P.MF->getPICBaseSymbol() << ']';
150 case X86II::MO_PIC_BASE_OFFSET:
151 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
152 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
153 O << '-' << *P.MF->getPICBaseSymbol();
155 case X86II::MO_TLSGD: O << "@TLSGD"; break;
156 case X86II::MO_TLSLD: O << "@TLSLD"; break;
157 case X86II::MO_TLSLDM: O << "@TLSLDM"; break;
158 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
159 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
160 case X86II::MO_TPOFF: O << "@TPOFF"; break;
161 case X86II::MO_DTPOFF: O << "@DTPOFF"; break;
162 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
163 case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break;
164 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
165 case X86II::MO_GOT: O << "@GOT"; break;
166 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
167 case X86II::MO_PLT: O << "@PLT"; break;
168 case X86II::MO_TLVP: O << "@TLVP"; break;
169 case X86II::MO_TLVP_PIC_BASE:
170 O << "@TLVP" << '-' << *P.MF->getPICBaseSymbol();
172 case X86II::MO_SECREL: O << "@SECREL32"; break;
176 static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
177 unsigned OpNo, raw_ostream &O,
178 const char *Modifier = nullptr, unsigned AsmVariant = 0);
180 /// printPCRelImm - This is used to print an immediate value that ends up
181 /// being encoded as a pc-relative value. These print slightly differently, for
182 /// example, a $ is not emitted.
183 static void printPCRelImm(X86AsmPrinter &P, const MachineInstr *MI,
184 unsigned OpNo, raw_ostream &O) {
185 const MachineOperand &MO = MI->getOperand(OpNo);
186 switch (MO.getType()) {
187 default: llvm_unreachable("Unknown pcrel immediate operand");
188 case MachineOperand::MO_Register:
189 // pc-relativeness was handled when computing the value in the reg.
190 printOperand(P, MI, OpNo, O);
192 case MachineOperand::MO_Immediate:
195 case MachineOperand::MO_GlobalAddress:
196 printSymbolOperand(P, MO, O);
201 static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
202 unsigned OpNo, raw_ostream &O, const char *Modifier,
203 unsigned AsmVariant) {
204 const MachineOperand &MO = MI->getOperand(OpNo);
205 switch (MO.getType()) {
206 default: llvm_unreachable("unknown operand type!");
207 case MachineOperand::MO_Register: {
208 // FIXME: Enumerating AsmVariant, so we can remove magic number.
209 if (AsmVariant == 0) O << '%';
210 unsigned Reg = MO.getReg();
211 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
212 MVT::SimpleValueType VT = (strcmp(Modifier+6,"64") == 0) ?
213 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
214 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
215 Reg = getX86SubSuperRegister(Reg, VT);
217 O << X86ATTInstPrinter::getRegisterName(Reg);
221 case MachineOperand::MO_Immediate:
222 if (AsmVariant == 0) O << '$';
226 case MachineOperand::MO_GlobalAddress: {
227 if (AsmVariant == 0) O << '$';
228 printSymbolOperand(P, MO, O);
234 static void printLeaMemReference(X86AsmPrinter &P, const MachineInstr *MI,
235 unsigned Op, raw_ostream &O,
236 const char *Modifier = nullptr) {
237 const MachineOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg);
238 const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
239 const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
241 // If we really don't want to print out (rip), don't.
242 bool HasBaseReg = BaseReg.getReg() != 0;
243 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
244 BaseReg.getReg() == X86::RIP)
247 // HasParenPart - True if we will print out the () part of the mem ref.
248 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
250 switch (DispSpec.getType()) {
252 llvm_unreachable("unknown operand type!");
253 case MachineOperand::MO_Immediate: {
254 int DispVal = DispSpec.getImm();
255 if (DispVal || !HasParenPart)
259 case MachineOperand::MO_GlobalAddress:
260 case MachineOperand::MO_ConstantPoolIndex:
261 printSymbolOperand(P, DispSpec, O);
264 if (Modifier && strcmp(Modifier, "H") == 0)
268 assert(IndexReg.getReg() != X86::ESP &&
269 "X86 doesn't allow scaling by ESP");
273 printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier);
275 if (IndexReg.getReg()) {
277 printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier);
278 unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
280 O << ',' << ScaleVal;
286 static void printMemReference(X86AsmPrinter &P, const MachineInstr *MI,
287 unsigned Op, raw_ostream &O,
288 const char *Modifier = nullptr) {
289 assert(isMem(MI, Op) && "Invalid memory reference!");
290 const MachineOperand &Segment = MI->getOperand(Op+X86::AddrSegmentReg);
291 if (Segment.getReg()) {
292 printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier);
295 printLeaMemReference(P, MI, Op, O, Modifier);
298 static void printIntelMemReference(X86AsmPrinter &P, const MachineInstr *MI,
299 unsigned Op, raw_ostream &O,
300 const char *Modifier = nullptr,
301 unsigned AsmVariant = 1) {
302 const MachineOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg);
303 unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
304 const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
305 const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
306 const MachineOperand &SegReg = MI->getOperand(Op+X86::AddrSegmentReg);
308 // If this has a segment register, print it.
309 if (SegReg.getReg()) {
310 printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier, AsmVariant);
316 bool NeedPlus = false;
317 if (BaseReg.getReg()) {
318 printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier, AsmVariant);
322 if (IndexReg.getReg()) {
323 if (NeedPlus) O << " + ";
325 O << ScaleVal << '*';
326 printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier, AsmVariant);
330 if (!DispSpec.isImm()) {
331 if (NeedPlus) O << " + ";
332 printOperand(P, MI, Op+X86::AddrDisp, O, Modifier, AsmVariant);
334 int64_t DispVal = DispSpec.getImm();
335 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
350 static bool printAsmMRegister(X86AsmPrinter &P, const MachineOperand &MO,
351 char Mode, raw_ostream &O) {
352 unsigned Reg = MO.getReg();
354 default: return true; // Unknown mode.
355 case 'b': // Print QImode register
356 Reg = getX86SubSuperRegister(Reg, MVT::i8);
358 case 'h': // Print QImode high register
359 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
361 case 'w': // Print HImode register
362 Reg = getX86SubSuperRegister(Reg, MVT::i16);
364 case 'k': // Print SImode register
365 Reg = getX86SubSuperRegister(Reg, MVT::i32);
368 // Print 64-bit register names if 64-bit integer registers are available.
369 // Otherwise, print 32-bit register names.
370 MVT::SimpleValueType Ty = P.getSubtarget().is64Bit() ? MVT::i64 : MVT::i32;
371 Reg = getX86SubSuperRegister(Reg, Ty);
375 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
379 /// PrintAsmOperand - Print out an operand for an inline asm expression.
381 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
383 const char *ExtraCode, raw_ostream &O) {
384 // Does this asm operand have a single letter operand modifier?
385 if (ExtraCode && ExtraCode[0]) {
386 if (ExtraCode[1] != 0) return true; // Unknown modifier.
388 const MachineOperand &MO = MI->getOperand(OpNo);
390 switch (ExtraCode[0]) {
392 // See if this is a generic print operand
393 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
394 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
395 switch (MO.getType()) {
398 case MachineOperand::MO_Immediate:
401 case MachineOperand::MO_ConstantPoolIndex:
402 case MachineOperand::MO_JumpTableIndex:
403 case MachineOperand::MO_ExternalSymbol:
404 llvm_unreachable("unexpected operand type!");
405 case MachineOperand::MO_GlobalAddress:
406 printSymbolOperand(*this, MO, O);
407 if (Subtarget->isPICStyleRIPRel())
410 case MachineOperand::MO_Register:
412 printOperand(*this, MI, OpNo, O);
417 case 'c': // Don't print "$" before a global var name or constant.
418 switch (MO.getType()) {
420 printOperand(*this, MI, OpNo, O);
422 case MachineOperand::MO_Immediate:
425 case MachineOperand::MO_ConstantPoolIndex:
426 case MachineOperand::MO_JumpTableIndex:
427 case MachineOperand::MO_ExternalSymbol:
428 llvm_unreachable("unexpected operand type!");
429 case MachineOperand::MO_GlobalAddress:
430 printSymbolOperand(*this, MO, O);
435 case 'A': // Print '*' before a register (it must be a register)
438 printOperand(*this, MI, OpNo, O);
443 case 'b': // Print QImode register
444 case 'h': // Print QImode high register
445 case 'w': // Print HImode register
446 case 'k': // Print SImode register
447 case 'q': // Print DImode register
449 return printAsmMRegister(*this, MO, ExtraCode[0], O);
450 printOperand(*this, MI, OpNo, O);
453 case 'P': // This is the operand of a call, treat specially.
454 printPCRelImm(*this, MI, OpNo, O);
457 case 'n': // Negate the immediate or print a '-' before the operand.
458 // Note: this is a temporary solution. It should be handled target
459 // independently as part of the 'MC' work.
468 printOperand(*this, MI, OpNo, O, /*Modifier*/ nullptr, AsmVariant);
472 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
473 unsigned OpNo, unsigned AsmVariant,
474 const char *ExtraCode,
477 printIntelMemReference(*this, MI, OpNo, O);
481 if (ExtraCode && ExtraCode[0]) {
482 if (ExtraCode[1] != 0) return true; // Unknown modifier.
484 switch (ExtraCode[0]) {
485 default: return true; // Unknown modifier.
486 case 'b': // Print QImode register
487 case 'h': // Print QImode high register
488 case 'w': // Print HImode register
489 case 'k': // Print SImode register
490 case 'q': // Print SImode register
491 // These only apply to registers, ignore on mem.
494 printMemReference(*this, MI, OpNo, O, "H");
496 case 'P': // Don't print @PLT, but do print as memory.
497 printMemReference(*this, MI, OpNo, O, "no-rip");
501 printMemReference(*this, MI, OpNo, O);
505 void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
506 if (Subtarget->isTargetMacho())
507 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
509 if (Subtarget->isTargetCOFF()) {
510 // Emit an absolute @feat.00 symbol. This appears to be some kind of
511 // compiler features bitfield read by link.exe.
512 if (!Subtarget->is64Bit()) {
513 MCSymbol *S = MMI->getContext().GetOrCreateSymbol(StringRef("@feat.00"));
514 OutStreamer.BeginCOFFSymbolDef(S);
515 OutStreamer.EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
516 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL);
517 OutStreamer.EndCOFFSymbolDef();
518 // According to the PE-COFF spec, the LSB of this value marks the object
519 // for "registered SEH". This means that all SEH handler entry points
520 // must be registered in .sxdata. Use of any unregistered handlers will
521 // cause the process to terminate immediately. LLVM does not know how to
522 // register any SEH handlers, so its object files should be safe.
524 OutStreamer.EmitSymbolAttribute(S, MCSA_Global);
525 OutStreamer.EmitAssignment(
526 S, MCConstantExpr::Create(int64_t(1), MMI->getContext()));
532 emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel,
533 MachineModuleInfoImpl::StubValueTy &MCSym) {
535 OutStreamer.EmitLabel(StubLabel);
536 // .indirect_symbol _foo
537 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
540 // External to current translation unit.
541 OutStreamer.EmitIntValue(0, 4/*size*/);
543 // Internal to current translation unit.
545 // When we place the LSDA into the TEXT section, the type info
546 // pointers need to be indirect and pc-rel. We accomplish this by
547 // using NLPs; however, sometimes the types are local to the file.
548 // We need to fill in the value for the NLP in those cases.
549 OutStreamer.EmitValue(
550 MCSymbolRefExpr::Create(MCSym.getPointer(), OutStreamer.getContext()),
554 MCSymbol *X86AsmPrinter::GetCPISymbol(unsigned CPID) const {
555 if (Subtarget->isTargetKnownWindowsMSVC()) {
556 const MachineConstantPoolEntry &CPE =
557 MF->getConstantPool()->getConstants()[CPID];
558 if (!CPE.isMachineConstantPoolEntry()) {
559 SectionKind Kind = CPE.getSectionKind(TM.getDataLayout());
560 const Constant *C = CPE.Val.ConstVal;
561 const MCSectionCOFF *S = cast<MCSectionCOFF>(
562 getObjFileLowering().getSectionForConstant(Kind, C));
563 if (MCSymbol *Sym = S->getCOMDATSymbol()) {
564 if (Sym->isUndefined())
565 OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global);
571 return AsmPrinter::GetCPISymbol(CPID);
574 void X86AsmPrinter::GenerateExportDirective(const MCSymbol *Sym, bool IsData) {
575 SmallString<128> Directive;
576 raw_svector_ostream OS(Directive);
577 StringRef Name = Sym->getName();
579 if (Subtarget->isTargetKnownWindowsMSVC())
584 if ((Subtarget->isTargetWindowsGNU() || Subtarget->isTargetWindowsCygwin()) &&
585 (Name[0] == getDataLayout().getGlobalPrefix()))
586 Name = Name.drop_front();
591 if (Subtarget->isTargetKnownWindowsMSVC())
598 OutStreamer.EmitBytes(Directive);
601 void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
602 if (Subtarget->isTargetMacho()) {
603 // All darwin targets use mach-o.
604 MachineModuleInfoMachO &MMIMacho =
605 MMI->getObjFileInfo<MachineModuleInfoMachO>();
607 // Output stubs for dynamically-linked functions.
608 MachineModuleInfoMachO::SymbolListTy Stubs;
610 Stubs = MMIMacho.GetFnStubList();
611 if (!Stubs.empty()) {
612 const MCSection *TheSection =
613 OutContext.getMachOSection("__IMPORT", "__jump_table",
614 MachO::S_SYMBOL_STUBS |
615 MachO::S_ATTR_SELF_MODIFYING_CODE |
616 MachO::S_ATTR_PURE_INSTRUCTIONS,
617 5, SectionKind::getMetadata());
618 OutStreamer.SwitchSection(TheSection);
620 for (const auto &Stub : Stubs) {
622 OutStreamer.EmitLabel(Stub.first);
623 // .indirect_symbol _foo
624 OutStreamer.EmitSymbolAttribute(Stub.second.getPointer(),
625 MCSA_IndirectSymbol);
626 // hlt; hlt; hlt; hlt; hlt hlt = 0xf4.
627 const char HltInsts[] = "\xf4\xf4\xf4\xf4\xf4";
628 OutStreamer.EmitBytes(StringRef(HltInsts, 5));
632 OutStreamer.AddBlankLine();
635 // Output stubs for external and common global variables.
636 Stubs = MMIMacho.GetGVStubList();
637 if (!Stubs.empty()) {
638 const MCSection *TheSection =
639 OutContext.getMachOSection("__IMPORT", "__pointers",
640 MachO::S_NON_LAZY_SYMBOL_POINTERS,
641 SectionKind::getMetadata());
642 OutStreamer.SwitchSection(TheSection);
644 for (auto &Stub : Stubs)
645 emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second);
648 OutStreamer.AddBlankLine();
651 Stubs = MMIMacho.GetHiddenGVStubList();
652 if (!Stubs.empty()) {
653 const MCSection *TheSection =
654 OutContext.getMachOSection("__IMPORT", "__pointers",
655 MachO::S_NON_LAZY_SYMBOL_POINTERS,
656 SectionKind::getMetadata());
657 OutStreamer.SwitchSection(TheSection);
659 for (auto &Stub : Stubs)
660 emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second);
663 OutStreamer.AddBlankLine();
666 SM.serializeToStackMapSection();
668 // Funny Darwin hack: This flag tells the linker that no global symbols
669 // contain code that falls through to other global symbols (e.g. the obvious
670 // implementation of multiple entry points). If this doesn't occur, the
671 // linker can safely perform dead code stripping. Since LLVM never
672 // generates code that does this, it is always safe to set.
673 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
676 if (Subtarget->isTargetKnownWindowsMSVC() && MMI->usesVAFloatArgument()) {
677 StringRef SymbolName = Subtarget->is64Bit() ? "_fltused" : "__fltused";
678 MCSymbol *S = MMI->getContext().GetOrCreateSymbol(SymbolName);
679 OutStreamer.EmitSymbolAttribute(S, MCSA_Global);
682 if (Subtarget->isTargetCOFF()) {
683 // Necessary for dllexport support
684 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
686 for (const auto &Function : M)
687 if (Function.hasDLLExportStorageClass())
688 DLLExportedFns.push_back(getSymbol(&Function));
690 for (const auto &Global : M.globals())
691 if (Global.hasDLLExportStorageClass())
692 DLLExportedGlobals.push_back(getSymbol(&Global));
694 for (const auto &Alias : M.aliases()) {
695 if (!Alias.hasDLLExportStorageClass())
698 if (Alias.getType()->getElementType()->isFunctionTy())
699 DLLExportedFns.push_back(getSymbol(&Alias));
701 DLLExportedGlobals.push_back(getSymbol(&Alias));
704 // Output linker support code for dllexported globals on windows.
705 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
706 const TargetLoweringObjectFileCOFF &TLOFCOFF =
707 static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering());
709 OutStreamer.SwitchSection(TLOFCOFF.getDrectveSection());
711 for (auto & Symbol : DLLExportedGlobals)
712 GenerateExportDirective(Symbol, /*IsData=*/true);
713 for (auto & Symbol : DLLExportedFns)
714 GenerateExportDirective(Symbol, /*IsData=*/false);
718 if (Subtarget->isTargetELF()) {
719 const TargetLoweringObjectFileELF &TLOFELF =
720 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
722 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
724 // Output stubs for external and common global variables.
725 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
726 if (!Stubs.empty()) {
727 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
728 const DataLayout *TD = TM.getDataLayout();
730 for (const auto &Stub : Stubs) {
731 OutStreamer.EmitLabel(Stub.first);
732 OutStreamer.EmitSymbolValue(Stub.second.getPointer(),
733 TD->getPointerSize());
740 //===----------------------------------------------------------------------===//
741 // Target Registry Stuff
742 //===----------------------------------------------------------------------===//
744 // Force static initialization.
745 extern "C" void LLVMInitializeX86AsmPrinter() {
746 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
747 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);