1 //===-- DarwinTargetAsmInfo.cpp - Darwin asm properties ---------*- C++ -*-===//
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 defines target asm properties related what form asm statements
11 // should take in general on Darwin-based targets
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Mangler.h"
22 #include "llvm/Target/DarwinTargetAsmInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetData.h"
28 DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine
&TM
)
31 CStringSection_
= getUnnamedSection("\t.cstring",
32 SectionFlags::Mergeable
|SectionFlags::Strings
);
33 FourByteConstantSection
= getUnnamedSection("\t.literal4\n",
34 SectionFlags::Mergeable
);
35 EightByteConstantSection
= getUnnamedSection("\t.literal8\n",
36 SectionFlags::Mergeable
);
38 // Note: 16-byte constant section is subtarget specific and should be provided
40 SixteenByteConstantSection
= 0;
42 ReadOnlySection
= getUnnamedSection("\t.const\n", SectionFlags::None
);
45 getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
47 ConstTextCoalSection
= getNamedSection("\t__TEXT,__const_coal,coalesced",
49 ConstDataCoalSection
= getNamedSection("\t__DATA,__const_coal,coalesced",
51 ConstDataSection
= getUnnamedSection(".const_data", SectionFlags::None
);
52 DataCoalSection
= getNamedSection("\t__DATA,__datacoal_nt,coalesced",
53 SectionFlags::Writeable
);
56 // Common settings for all Darwin targets.
59 PrivateGlobalPrefix
= "L";
60 LinkerPrivateGlobalPrefix
= "l"; // Marker for some ObjC metadata
62 NeedsIndirectEncoding
= true;
63 AllowQuotesInName
= true;
64 HasSingleParameterDotFile
= false;
66 // In non-PIC modes, emit a special label before jump tables so that the
67 // linker can perform more accurate dead code stripping. We do not check the
68 // relocation model here since it can be overridden later.
69 JumpTableSpecialLabelPrefix
= "l";
72 WeakDefDirective
= "\t.weak_definition ";
73 WeakRefDirective
= "\t.weak_reference ";
74 HiddenDirective
= "\t.private_extern ";
77 CStringSection
= "\t.cstring";
78 JumpTableDataSection
= "\t.const\n";
81 if (TM
.getRelocationModel() == Reloc::Static
) {
82 StaticCtorsSection
= ".constructor";
83 StaticDtorsSection
= ".destructor";
85 StaticCtorsSection
= ".mod_init_func";
86 StaticDtorsSection
= ".mod_term_func";
89 // _foo.eh symbols are currently always exported so that the linker knows
90 // about them. This may not strictly be necessary on 10.6 and later, but it
91 // doesn't hurt anything.
92 Is_EHSymbolPrivate
= false;
94 DwarfAbbrevSection
= ".section __DWARF,__debug_abbrev,regular,debug";
95 DwarfInfoSection
= ".section __DWARF,__debug_info,regular,debug";
96 DwarfLineSection
= ".section __DWARF,__debug_line,regular,debug";
97 DwarfFrameSection
= ".section __DWARF,__debug_frame,regular,debug";
98 DwarfPubNamesSection
= ".section __DWARF,__debug_pubnames,regular,debug";
99 DwarfPubTypesSection
= ".section __DWARF,__debug_pubtypes,regular,debug";
100 DwarfStrSection
= ".section __DWARF,__debug_str,regular,debug";
101 DwarfLocSection
= ".section __DWARF,__debug_loc,regular,debug";
102 DwarfARangesSection
= ".section __DWARF,__debug_aranges,regular,debug";
103 DwarfRangesSection
= ".section __DWARF,__debug_ranges,regular,debug";
104 DwarfMacroInfoSection
= ".section __DWARF,__debug_macinfo,regular,debug";
107 /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
108 /// the PrivateGlobalPrefix or the LinkerPrivateGlobalPrefix does not have the
109 /// directive emitted (this occurs in ObjC metadata).
110 bool DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue
* GV
,
111 Mangler
*Mang
) const {
112 if (!GV
) return false;
114 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
115 if (GV
->hasLocalLinkage() && !isa
<Function
>(GV
)) {
116 // FIXME: ObjC metadata is currently emitted as internal symbols that have
117 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
118 // this horrible hack can go away.
119 const std::string
&Name
= Mang
->getMangledName(GV
);
120 if (Name
[0] == 'L' || Name
[0] == 'l')
128 DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue
*GV
,
129 SectionKind::Kind Kind
) const {
130 bool isWeak
= GV
->isWeakForLinker();
131 bool isNonStatic
= TM
.getRelocationModel() != Reloc::Static
;
134 case SectionKind::ThreadData
:
135 case SectionKind::ThreadBSS
:
136 llvm_unreachable("Darwin doesn't support TLS");
137 case SectionKind::Text
:
139 return TextCoalSection
;
141 case SectionKind::Data
:
142 case SectionKind::DataRelLocal
:
143 case SectionKind::DataRel
:
144 case SectionKind::BSS
:
145 if (cast
<GlobalVariable
>(GV
)->isConstant())
146 return isWeak
? ConstDataCoalSection
: ConstDataSection
;
147 return isWeak
? DataCoalSection
: DataSection
;
149 case SectionKind::ROData
:
150 case SectionKind::DataRelRO
:
151 case SectionKind::DataRelROLocal
:
152 return (isWeak
? ConstDataCoalSection
:
153 (isNonStatic
? ConstDataSection
: getReadOnlySection()));
154 case SectionKind::RODataMergeStr
:
156 ConstTextCoalSection
:
157 MergeableStringSection(cast
<GlobalVariable
>(GV
)));
158 case SectionKind::RODataMergeConst
: {
159 if (isWeak
) return ConstDataCoalSection
;
160 const Type
*Ty
= cast
<GlobalVariable
>(GV
)->getInitializer()->getType();
161 const TargetData
*TD
= TM
.getTargetData();
162 return getSectionForMergableConstant(TD
->getTypeAllocSize(Ty
), 0);
165 llvm_unreachable("Unsuported section kind for global");
168 // FIXME: Do we have any extra special weird cases?
173 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable
*GV
) const {
174 const TargetData
*TD
= TM
.getTargetData();
175 Constant
*C
= cast
<GlobalVariable
>(GV
)->getInitializer();
176 const Type
*Ty
= cast
<ArrayType
>(C
->getType())->getElementType();
178 unsigned Size
= TD
->getTypeAllocSize(Ty
);
180 unsigned Align
= TD
->getPreferredAlignment(GV
);
182 return getCStringSection_();
185 return getReadOnlySection();
189 DarwinTargetAsmInfo::getSectionForMergableConstant(uint64_t Size
,
190 unsigned ReloInfo
) const {
191 // If this constant requires a relocation, we have to put it in the data
192 // segment, not in the text segment.
194 return ConstDataSection
;
199 return FourByteConstantSection
;
201 return EightByteConstantSection
;
203 if (SixteenByteConstantSection
)
204 return SixteenByteConstantSection
;
208 return ReadOnlySection
; // .const