Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1 | //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===// |
| 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 | // This file implements classes used to handle lowerings specific to common |
| 11 | // object file formats. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/GlobalVariable.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCContext.h" |
| 20 | #include "llvm/MC/MCSection.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachine.h" |
| 22 | #include "llvm/Target/TargetData.h" |
| 23 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Mangler.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
| 26 | using namespace llvm; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // Generic Code |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 32 | TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 33 | TextSection = 0; |
| 34 | DataSection = 0; |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 35 | BSSSection = 0; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 36 | ReadOnlySection = 0; |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 37 | StaticCtorSection = 0; |
| 38 | StaticDtorSection = 0; |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 39 | LSDASection = 0; |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 40 | EHFrameSection = 0; |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 41 | |
| 42 | DwarfAbbrevSection = 0; |
| 43 | DwarfInfoSection = 0; |
| 44 | DwarfLineSection = 0; |
| 45 | DwarfFrameSection = 0; |
| 46 | DwarfPubNamesSection = 0; |
| 47 | DwarfPubTypesSection = 0; |
| 48 | DwarfDebugInlineSection = 0; |
| 49 | DwarfStrSection = 0; |
| 50 | DwarfLocSection = 0; |
| 51 | DwarfARangesSection = 0; |
| 52 | DwarfRangesSection = 0; |
| 53 | DwarfMacroInfoSection = 0; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | TargetLoweringObjectFile::~TargetLoweringObjectFile() { |
| 57 | } |
| 58 | |
| 59 | static bool isSuitableForBSS(const GlobalVariable *GV) { |
| 60 | Constant *C = GV->getInitializer(); |
| 61 | |
| 62 | // Must have zero initializer. |
| 63 | if (!C->isNullValue()) |
| 64 | return false; |
| 65 | |
| 66 | // Leave constant zeros in readonly constant sections, so they can be shared. |
| 67 | if (GV->isConstant()) |
| 68 | return false; |
| 69 | |
| 70 | // If the global has an explicit section specified, don't put it in BSS. |
| 71 | if (!GV->getSection().empty()) |
| 72 | return false; |
| 73 | |
| 74 | // If -nozero-initialized-in-bss is specified, don't ever use BSS. |
| 75 | if (NoZerosInBSS) |
| 76 | return false; |
| 77 | |
| 78 | // Otherwise, put it in BSS! |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | static bool isConstantString(const Constant *C) { |
| 83 | // First check: is we have constant array of i8 terminated with zero |
| 84 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
| 85 | // Check, if initializer is a null-terminated string |
| 86 | if (CVA && CVA->isCString()) |
| 87 | return true; |
| 88 | |
| 89 | // Another possibility: [1 x i8] zeroinitializer |
| 90 | if (isa<ConstantAggregateZero>(C)) |
| 91 | if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType())) |
| 92 | return (Ty->getElementType() == Type::Int8Ty && |
| 93 | Ty->getNumElements() == 1); |
| 94 | |
| 95 | return false; |
| 96 | } |
| 97 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 98 | /// SectionKindForGlobal - This is a top-level target-independent classifier for |
| 99 | /// a global variable. Given an global variable and information from TM, it |
| 100 | /// classifies the global in a variety of ways that make various target |
| 101 | /// implementations simpler. The target implementation is free to ignore this |
| 102 | /// extra info of course. |
| 103 | static SectionKind SectionKindForGlobal(const GlobalValue *GV, |
| 104 | const TargetMachine &TM) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 105 | Reloc::Model ReloModel = TM.getRelocationModel(); |
| 106 | |
| 107 | // Early exit - functions should be always in text sections. |
| 108 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); |
| 109 | if (GVar == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 110 | return SectionKind::getText(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | // Handle thread-local data first. |
| 114 | if (GVar->isThreadLocal()) { |
| 115 | if (isSuitableForBSS(GVar)) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 116 | return SectionKind::getThreadBSS(); |
| 117 | return SectionKind::getThreadData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Variable can be easily put to BSS section. |
| 121 | if (isSuitableForBSS(GVar)) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 122 | return SectionKind::getBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 123 | |
| 124 | Constant *C = GVar->getInitializer(); |
| 125 | |
| 126 | // If the global is marked constant, we can put it into a mergable section, |
| 127 | // a mergable string section, or general .data if it contains relocations. |
| 128 | if (GVar->isConstant()) { |
| 129 | // If the initializer for the global contains something that requires a |
| 130 | // relocation, then we may have to drop this into a wriable data section |
| 131 | // even though it is marked const. |
| 132 | switch (C->getRelocationInfo()) { |
| 133 | default: llvm_unreachable("unknown relocation info kind"); |
| 134 | case Constant::NoRelocation: |
| 135 | // If initializer is a null-terminated string, put it in a "cstring" |
| 136 | // section if the target has it. |
| 137 | if (isConstantString(C)) |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 138 | return SectionKind::getMergeable1ByteCString(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 140 | // FIXME: Detect 2/4 byte strings. |
| 141 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 142 | // Otherwise, just drop it into a mergable constant section. If we have |
| 143 | // a section for this size, use it, otherwise use the arbitrary sized |
| 144 | // mergable section. |
| 145 | switch (TM.getTargetData()->getTypeAllocSize(C->getType())) { |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 146 | case 4: return SectionKind::getMergeableConst4(); |
| 147 | case 8: return SectionKind::getMergeableConst8(); |
| 148 | case 16: return SectionKind::getMergeableConst16(); |
| 149 | default: return SectionKind::getMergeableConst(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | case Constant::LocalRelocation: |
| 153 | // In static relocation model, the linker will resolve all addresses, so |
| 154 | // the relocation entries will actually be constants by the time the app |
| 155 | // starts up. However, we can't put this into a mergable section, because |
| 156 | // the linker doesn't take relocations into consideration when it tries to |
| 157 | // merge entries in the section. |
| 158 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 159 | return SectionKind::getReadOnly(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 160 | |
| 161 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 162 | // writable data.rel.local section. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 163 | return SectionKind::getReadOnlyWithRelLocal(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 164 | |
| 165 | case Constant::GlobalRelocations: |
| 166 | // In static relocation model, the linker will resolve all addresses, so |
| 167 | // the relocation entries will actually be constants by the time the app |
| 168 | // starts up. However, we can't put this into a mergable section, because |
| 169 | // the linker doesn't take relocations into consideration when it tries to |
| 170 | // merge entries in the section. |
| 171 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 172 | return SectionKind::getReadOnly(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 173 | |
| 174 | // Otherwise, the dynamic linker needs to fix it up, put it in the |
| 175 | // writable data.rel section. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 176 | return SectionKind::getReadOnlyWithRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | // Okay, this isn't a constant. If the initializer for the global is going |
| 181 | // to require a runtime relocation by the dynamic linker, put it into a more |
| 182 | // specific section to improve startup time of the app. This coalesces these |
| 183 | // globals together onto fewer pages, improving the locality of the dynamic |
| 184 | // linker. |
| 185 | if (ReloModel == Reloc::Static) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 186 | return SectionKind::getDataNoRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 187 | |
| 188 | switch (C->getRelocationInfo()) { |
| 189 | default: llvm_unreachable("unknown relocation info kind"); |
| 190 | case Constant::NoRelocation: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 191 | return SectionKind::getDataNoRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 192 | case Constant::LocalRelocation: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 193 | return SectionKind::getDataRelLocal(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 194 | case Constant::GlobalRelocations: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 195 | return SectionKind::getDataRel(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | /// SectionForGlobal - This method computes the appropriate section to emit |
| 200 | /// the specified global variable or function definition. This should not |
| 201 | /// be passed external (or available externally) globals. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 202 | const MCSection *TargetLoweringObjectFile:: |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 203 | SectionForGlobal(const GlobalValue *GV, Mangler *Mang, |
| 204 | const TargetMachine &TM) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 205 | assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() && |
| 206 | "Can only be used for global definitions"); |
| 207 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 208 | SectionKind Kind = SectionKindForGlobal(GV, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 209 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 210 | // Select section name. |
| 211 | if (GV->hasSection()) { |
| 212 | // If the target has special section hacks for specifically named globals, |
| 213 | // return them now. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 214 | if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind)) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 215 | return TS; |
| 216 | |
| 217 | // If the target has magic semantics for certain section names, make sure to |
| 218 | // pick up the flags. This allows the user to write things with attribute |
| 219 | // section and still get the appropriate section flags printed. |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 220 | Kind = getKindForNamedSection(GV->getSection().c_str(), Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 222 | return getOrCreateSection(GV->getSection().c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
| 226 | // Use default section depending on the 'type' of global |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 227 | return SelectSectionForGlobal(GV, Kind, Mang, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Lame default implementation. Calculate the section name for global. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 231 | const MCSection * |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 232 | TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV, |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 233 | SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 234 | Mangler *Mang, |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 235 | const TargetMachine &TM) const{ |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 236 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 237 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 238 | if (Kind.isText()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 239 | return getTextSection(); |
| 240 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 241 | if (Kind.isBSS() && BSSSection != 0) |
| 242 | return BSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 243 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 244 | if (Kind.isReadOnly() && ReadOnlySection != 0) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 245 | return ReadOnlySection; |
| 246 | |
| 247 | return getDataSection(); |
| 248 | } |
| 249 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 250 | /// getSectionForConstant - Given a mergable constant with the |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 251 | /// specified size and relocation information, return a section that it |
| 252 | /// should be placed in. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 253 | const MCSection * |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 254 | TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 255 | if (Kind.isReadOnly() && ReadOnlySection != 0) |
| 256 | return ReadOnlySection; |
| 257 | |
| 258 | return DataSection; |
| 259 | } |
| 260 | |
| 261 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 262 | const MCSection *TargetLoweringObjectFile:: |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 263 | getOrCreateSection(const char *Name, bool isDirective, SectionKind Kind) const { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 264 | if (MCSection *S = Ctx->GetSection(Name)) |
| 265 | return S; |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 266 | return MCSection::Create(Name, isDirective, Kind, *Ctx); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
| 270 | |
| 271 | //===----------------------------------------------------------------------===// |
| 272 | // ELF |
| 273 | //===----------------------------------------------------------------------===// |
| 274 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 275 | void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, |
| 276 | const TargetMachine &TM) { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 277 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 278 | if (!HasCrazyBSS) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 279 | BSSSection = getOrCreateSection("\t.bss", true, SectionKind::getBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 280 | else |
| 281 | // PPC/Linux doesn't support the .bss directive, it needs .section .bss. |
| 282 | // FIXME: Does .section .bss work everywhere?? |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 283 | // FIXME2: this should just be handle by the section printer. We should get |
| 284 | // away from syntactic view of the sections and MCSection should just be a |
| 285 | // semantic view. |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 286 | BSSSection = getOrCreateSection("\t.bss", false, SectionKind::getBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 287 | |
| 288 | |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 289 | TextSection = getOrCreateSection("\t.text", true, SectionKind::getText()); |
| 290 | DataSection = getOrCreateSection("\t.data", true, SectionKind::getDataRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 291 | ReadOnlySection = |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 292 | getOrCreateSection("\t.rodata", false, SectionKind::getReadOnly()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 293 | TLSDataSection = |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 294 | getOrCreateSection("\t.tdata", false, SectionKind::getThreadData()); |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 295 | |
| 296 | // FIXME: No reason to make this. |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 297 | CStringSection = getOrCreateSection("\t.rodata.str", true, |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 298 | SectionKind::getMergeable1ByteCString()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 300 | TLSBSSSection = getOrCreateSection("\t.tbss", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 301 | SectionKind::getThreadBSS()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 302 | |
| 303 | DataRelSection = getOrCreateSection("\t.data.rel", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 304 | SectionKind::getDataRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 305 | DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 306 | SectionKind::getDataRelLocal()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 307 | DataRelROSection = getOrCreateSection("\t.data.rel.ro", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 308 | SectionKind::getReadOnlyWithRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 309 | DataRelROLocalSection = |
| 310 | getOrCreateSection("\t.data.rel.ro.local", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 311 | SectionKind::getReadOnlyWithRelLocal()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 312 | |
| 313 | MergeableConst4Section = getOrCreateSection(".rodata.cst4", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 314 | SectionKind::getMergeableConst4()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 315 | MergeableConst8Section = getOrCreateSection(".rodata.cst8", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 316 | SectionKind::getMergeableConst8()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 317 | MergeableConst16Section = getOrCreateSection(".rodata.cst16", false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 318 | SectionKind::getMergeableConst16()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 319 | |
| 320 | StaticCtorSection = |
| 321 | getOrCreateSection(".ctors", false, SectionKind::getDataRel()); |
| 322 | StaticDtorSection = |
| 323 | getOrCreateSection(".dtors", false, SectionKind::getDataRel()); |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 325 | // Exception Handling Sections. |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 326 | |
| 327 | // FIXME: We're emitting LSDA info into a readonly section on ELF, even though |
| 328 | // it contains relocatable pointers. In PIC mode, this is probably a big |
| 329 | // runtime hit for C++ apps. Either the contents of the LSDA need to be |
| 330 | // adjusted or this should be a data section. |
| 331 | LSDASection = |
| 332 | getOrCreateSection(".gcc_except_table", false, SectionKind::getReadOnly()); |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 333 | EHFrameSection = |
| 334 | getOrCreateSection(".eh_frame", false, SectionKind::getDataRel()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 335 | |
| 336 | // Debug Info Sections. |
| 337 | DwarfAbbrevSection = |
| 338 | getOrCreateSection(".debug_abbrev", false, SectionKind::getMetadata()); |
| 339 | DwarfInfoSection = |
| 340 | getOrCreateSection(".debug_info", false, SectionKind::getMetadata()); |
| 341 | DwarfLineSection = |
| 342 | getOrCreateSection(".debug_line", false, SectionKind::getMetadata()); |
| 343 | DwarfFrameSection = |
| 344 | getOrCreateSection(".debug_frame", false, SectionKind::getMetadata()); |
| 345 | DwarfPubNamesSection = |
| 346 | getOrCreateSection(".debug_pubnames", false, SectionKind::getMetadata()); |
| 347 | DwarfPubTypesSection = |
| 348 | getOrCreateSection(".debug_pubtypes", false, SectionKind::getMetadata()); |
| 349 | DwarfStrSection = |
| 350 | getOrCreateSection(".debug_str", false, SectionKind::getMetadata()); |
| 351 | DwarfLocSection = |
| 352 | getOrCreateSection(".debug_loc", false, SectionKind::getMetadata()); |
| 353 | DwarfARangesSection = |
| 354 | getOrCreateSection(".debug_aranges", false, SectionKind::getMetadata()); |
| 355 | DwarfRangesSection = |
| 356 | getOrCreateSection(".debug_ranges", false, SectionKind::getMetadata()); |
| 357 | DwarfMacroInfoSection = |
| 358 | getOrCreateSection(".debug_macinfo", false, SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 362 | SectionKind TargetLoweringObjectFileELF:: |
| 363 | getKindForNamedSection(const char *Name, SectionKind K) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 364 | if (Name[0] != '.') return K; |
| 365 | |
| 366 | // Some lame default implementation based on some magic section names. |
| 367 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
| 368 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0 || |
| 369 | strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 || |
| 370 | strncmp(Name, ".llvm.linkonce.sb.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 371 | return SectionKind::getBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 372 | |
| 373 | if (strcmp(Name, ".tdata") == 0 || |
| 374 | strncmp(Name, ".tdata.", 7) == 0 || |
| 375 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 376 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 377 | return SectionKind::getThreadData(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 378 | |
| 379 | if (strcmp(Name, ".tbss") == 0 || |
| 380 | strncmp(Name, ".tbss.", 6) == 0 || |
| 381 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 382 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 383 | return SectionKind::getThreadBSS(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 384 | |
| 385 | return K; |
| 386 | } |
| 387 | |
| 388 | void TargetLoweringObjectFileELF:: |
| 389 | getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const { |
| 390 | Str.push_back(','); |
| 391 | Str.push_back('"'); |
| 392 | |
| 393 | if (!Kind.isMetadata()) |
| 394 | Str.push_back('a'); |
| 395 | if (Kind.isText()) |
| 396 | Str.push_back('x'); |
| 397 | if (Kind.isWriteable()) |
| 398 | Str.push_back('w'); |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 399 | if (Kind.isMergeable1ByteCString() || |
| 400 | Kind.isMergeable2ByteCString() || |
| 401 | Kind.isMergeable4ByteCString() || |
Chris Lattner | 82987bf | 2009-07-31 16:17:13 +0000 | [diff] [blame] | 402 | Kind.isMergeableConst4() || |
| 403 | Kind.isMergeableConst8() || |
| 404 | Kind.isMergeableConst16()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 405 | Str.push_back('M'); |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 406 | if (Kind.isMergeable1ByteCString() || |
| 407 | Kind.isMergeable2ByteCString() || |
| 408 | Kind.isMergeable4ByteCString()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 409 | Str.push_back('S'); |
| 410 | if (Kind.isThreadLocal()) |
| 411 | Str.push_back('T'); |
| 412 | |
| 413 | Str.push_back('"'); |
| 414 | Str.push_back(','); |
| 415 | |
| 416 | // If comment string is '@', e.g. as on ARM - use '%' instead |
| 417 | if (AtIsCommentChar) |
| 418 | Str.push_back('%'); |
| 419 | else |
| 420 | Str.push_back('@'); |
| 421 | |
| 422 | const char *KindStr; |
Chris Lattner | bf15e43 | 2009-07-28 17:57:51 +0000 | [diff] [blame] | 423 | if (Kind.isBSS() || Kind.isThreadBSS()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 424 | KindStr = "nobits"; |
| 425 | else |
| 426 | KindStr = "progbits"; |
| 427 | |
| 428 | Str.append(KindStr, KindStr+strlen(KindStr)); |
| 429 | |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 430 | if (Kind.isMergeable1ByteCString()) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 431 | Str.push_back(','); |
| 432 | Str.push_back('1'); |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 433 | } else if (Kind.isMergeable2ByteCString()) { |
| 434 | Str.push_back(','); |
| 435 | Str.push_back('2'); |
| 436 | } else if (Kind.isMergeable4ByteCString()) { |
| 437 | Str.push_back(','); |
| 438 | Str.push_back('4'); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 439 | } else if (Kind.isMergeableConst4()) { |
| 440 | Str.push_back(','); |
| 441 | Str.push_back('4'); |
| 442 | } else if (Kind.isMergeableConst8()) { |
| 443 | Str.push_back(','); |
| 444 | Str.push_back('8'); |
| 445 | } else if (Kind.isMergeableConst16()) { |
| 446 | Str.push_back(','); |
| 447 | Str.push_back('1'); |
| 448 | Str.push_back('6'); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 454 | if (Kind.isText()) return ".gnu.linkonce.t."; |
| 455 | if (Kind.isReadOnly()) return ".gnu.linkonce.r."; |
| 456 | |
| 457 | if (Kind.isThreadData()) return ".gnu.linkonce.td."; |
| 458 | if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; |
| 459 | |
| 460 | if (Kind.isBSS()) return ".gnu.linkonce.b."; |
| 461 | if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; |
| 462 | if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; |
| 463 | if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; |
| 464 | if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; |
| 465 | |
| 466 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 467 | return ".gnu.linkonce.d.rel.ro."; |
| 468 | } |
| 469 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 470 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 471 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 472 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 473 | |
| 474 | // If this global is linkonce/weak and the target handles this by emitting it |
| 475 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 476 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 477 | const char *Prefix = getSectionPrefixForUniqueGlobal(Kind); |
Chris Lattner | b8f396b | 2009-07-29 05:20:33 +0000 | [diff] [blame] | 478 | std::string Name = Mang->makeNameProper(GV->getNameStr()); |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 479 | return getOrCreateSection((Prefix+Name).c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 482 | if (Kind.isText()) return TextSection; |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 484 | if (Kind.isMergeable1ByteCString() || |
| 485 | Kind.isMergeable2ByteCString() || |
| 486 | Kind.isMergeable4ByteCString()) { |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 487 | assert(CStringSection && "Should have string section prefix"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 067fe1a | 2009-07-29 04:54:38 +0000 | [diff] [blame] | 489 | // We also need alignment here. |
| 490 | // FIXME: this is getting the alignment of the character, not the |
| 491 | // alignment of the global! |
| 492 | unsigned Align = |
| 493 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 494 | |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 495 | const char *SizeSpec = "1."; |
| 496 | if (Kind.isMergeable2ByteCString()) |
| 497 | SizeSpec = "2."; |
| 498 | else if (Kind.isMergeable4ByteCString()) |
| 499 | SizeSpec = "4."; |
| 500 | else |
| 501 | assert(Kind.isMergeable1ByteCString() && "unknown string width"); |
| 502 | |
| 503 | |
| 504 | std::string Name = CStringSection->getName() + SizeSpec + utostr(Align); |
| 505 | return getOrCreateSection(Name.c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 508 | if (Kind.isMergeableConst()) { |
| 509 | if (Kind.isMergeableConst4()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 510 | return MergeableConst4Section; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 511 | if (Kind.isMergeableConst8()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 512 | return MergeableConst8Section; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 513 | if (Kind.isMergeableConst16()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 514 | return MergeableConst16Section; |
| 515 | return ReadOnlySection; // .const |
| 516 | } |
| 517 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 518 | if (Kind.isReadOnly()) return ReadOnlySection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 519 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 520 | if (Kind.isThreadData()) return TLSDataSection; |
| 521 | if (Kind.isThreadBSS()) return TLSBSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 523 | if (Kind.isBSS()) return BSSSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 524 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 525 | if (Kind.isDataNoRel()) return DataSection; |
| 526 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 527 | if (Kind.isDataRel()) return DataRelSection; |
| 528 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 529 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 530 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 531 | return DataRelROSection; |
| 532 | } |
| 533 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 534 | /// getSectionForConstant - Given a mergeable constant with the |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 535 | /// specified size and relocation information, return a section that it |
| 536 | /// should be placed in. |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 537 | const MCSection *TargetLoweringObjectFileELF:: |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 538 | getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 539 | if (Kind.isMergeableConst4()) |
| 540 | return MergeableConst4Section; |
| 541 | if (Kind.isMergeableConst8()) |
| 542 | return MergeableConst8Section; |
| 543 | if (Kind.isMergeableConst16()) |
| 544 | return MergeableConst16Section; |
| 545 | if (Kind.isReadOnly()) |
| 546 | return ReadOnlySection; |
| 547 | |
| 548 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 549 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 550 | return DataRelROSection; |
| 551 | } |
| 552 | |
| 553 | //===----------------------------------------------------------------------===// |
| 554 | // MachO |
| 555 | //===----------------------------------------------------------------------===// |
| 556 | |
Chris Lattner | 11e9657 | 2009-08-03 21:53:27 +0000 | [diff] [blame] | 557 | const MCSection *TargetLoweringObjectFileMachO:: |
| 558 | getMachOSection(const char *Name, bool isDirective, SectionKind K) { |
| 559 | // FOR NOW, Just forward. |
| 560 | return getOrCreateSection(Name, isDirective, K); |
| 561 | } |
| 562 | |
| 563 | |
| 564 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 565 | void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, |
| 566 | const TargetMachine &TM) { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 567 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 568 | TextSection = getOrCreateSection("\t.text", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 569 | SectionKind::getText()); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 570 | DataSection = getOrCreateSection("\t.data", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 571 | SectionKind::getDataRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 572 | |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 573 | CStringSection = getOrCreateSection("\t.cstring", true, |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 574 | SectionKind::getMergeable1ByteCString()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 575 | FourByteConstantSection = getOrCreateSection("\t.literal4\n", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 576 | SectionKind::getMergeableConst4()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 577 | EightByteConstantSection = getOrCreateSection("\t.literal8\n", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 578 | SectionKind::getMergeableConst8()); |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 579 | |
| 580 | // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back |
| 581 | // to using it in -static mode. |
| 582 | if (TM.getRelocationModel() != Reloc::Static && |
| 583 | TM.getTargetData()->getPointerSize() == 32) |
| 584 | SixteenByteConstantSection = |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 585 | getOrCreateSection("\t.literal16\n", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 586 | SectionKind::getMergeableConst16()); |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 587 | else |
| 588 | SixteenByteConstantSection = 0; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 590 | ReadOnlySection = getOrCreateSection("\t.const", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 591 | SectionKind::getReadOnly()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 592 | |
| 593 | TextCoalSection = |
| 594 | getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions", |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 595 | false, SectionKind::getText()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 596 | ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced", |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 597 | false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 598 | SectionKind::getText()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 599 | ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced", |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 600 | false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 601 | SectionKind::getText()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 602 | ConstDataSection = getOrCreateSection("\t.const_data", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 603 | SectionKind::getReadOnlyWithRel()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 604 | DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced", |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 605 | false, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 606 | SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 607 | |
| 608 | if (TM.getRelocationModel() == Reloc::Static) { |
| 609 | StaticCtorSection = |
| 610 | getOrCreateSection(".constructor", true, SectionKind::getDataRel()); |
| 611 | StaticDtorSection = |
| 612 | getOrCreateSection(".destructor", true, SectionKind::getDataRel()); |
| 613 | } else { |
| 614 | StaticCtorSection = |
| 615 | getOrCreateSection(".mod_init_func", true, SectionKind::getDataRel()); |
| 616 | StaticDtorSection = |
| 617 | getOrCreateSection(".mod_term_func", true, SectionKind::getDataRel()); |
| 618 | } |
| 619 | |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 620 | // Exception Handling. |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 621 | LSDASection = getOrCreateSection("__DATA,__gcc_except_tab", false, |
| 622 | SectionKind::getDataRel()); |
Chris Lattner | 35039ac | 2009-08-02 06:52:36 +0000 | [diff] [blame] | 623 | EHFrameSection = |
| 624 | getOrCreateSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms" |
| 625 | "+live_support", false, SectionKind::getReadOnly()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 626 | |
| 627 | // Debug Information. |
| 628 | // FIXME: Don't use 'directive' syntax: need flags for debug/regular?? |
| 629 | // FIXME: Need __DWARF segment. |
| 630 | DwarfAbbrevSection = |
| 631 | getOrCreateSection(".section __DWARF,__debug_abbrev,regular,debug", true, |
| 632 | SectionKind::getMetadata()); |
| 633 | DwarfInfoSection = |
| 634 | getOrCreateSection(".section __DWARF,__debug_info,regular,debug", true, |
| 635 | SectionKind::getMetadata()); |
| 636 | DwarfLineSection = |
| 637 | getOrCreateSection(".section __DWARF,__debug_line,regular,debug", true, |
| 638 | SectionKind::getMetadata()); |
| 639 | DwarfFrameSection = |
| 640 | getOrCreateSection(".section __DWARF,__debug_frame,regular,debug", true, |
| 641 | SectionKind::getMetadata()); |
| 642 | DwarfPubNamesSection = |
| 643 | getOrCreateSection(".section __DWARF,__debug_pubnames,regular,debug", true, |
| 644 | SectionKind::getMetadata()); |
| 645 | DwarfPubTypesSection = |
| 646 | getOrCreateSection(".section __DWARF,__debug_pubtypes,regular,debug", true, |
| 647 | SectionKind::getMetadata()); |
| 648 | DwarfStrSection = |
| 649 | getOrCreateSection(".section __DWARF,__debug_str,regular,debug", true, |
| 650 | SectionKind::getMetadata()); |
| 651 | DwarfLocSection = |
| 652 | getOrCreateSection(".section __DWARF,__debug_loc,regular,debug", true, |
| 653 | SectionKind::getMetadata()); |
| 654 | DwarfARangesSection = |
| 655 | getOrCreateSection(".section __DWARF,__debug_aranges,regular,debug", true, |
| 656 | SectionKind::getMetadata()); |
| 657 | DwarfRangesSection = |
| 658 | getOrCreateSection(".section __DWARF,__debug_ranges,regular,debug", true, |
| 659 | SectionKind::getMetadata()); |
| 660 | DwarfMacroInfoSection = |
| 661 | getOrCreateSection(".section __DWARF,__debug_macinfo,regular,debug", true, |
| 662 | SectionKind::getMetadata()); |
| 663 | DwarfDebugInlineSection = |
| 664 | getOrCreateSection(".section __DWARF,__debug_inlined,regular,debug", true, |
| 665 | SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 668 | const MCSection *TargetLoweringObjectFileMachO:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 669 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 670 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 671 | assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 672 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 673 | if (Kind.isText()) |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 674 | return GV->isWeakForLinker() ? TextCoalSection : TextSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 675 | |
| 676 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 677 | // or data depending on if it is writable. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 678 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 679 | if (Kind.isReadOnly()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 680 | return ConstTextCoalSection; |
| 681 | return DataCoalSection; |
| 682 | } |
| 683 | |
| 684 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 685 | if (Kind.isMergeable1ByteCString()) { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 686 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
| 687 | const Type *Ty = cast<ArrayType>(C->getType())->getElementType(); |
| 688 | const TargetData &TD = *TM.getTargetData(); |
| 689 | unsigned Size = TD.getTypeAllocSize(Ty); |
| 690 | if (Size) { |
| 691 | unsigned Align = TD.getPreferredAlignment(cast<GlobalVariable>(GV)); |
| 692 | if (Align <= 32) |
Chris Lattner | 8245838 | 2009-08-01 21:56:13 +0000 | [diff] [blame] | 693 | return CStringSection; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | return ReadOnlySection; |
| 697 | } |
| 698 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 699 | if (Kind.isMergeableConst()) { |
| 700 | if (Kind.isMergeableConst4()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 701 | return FourByteConstantSection; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 702 | if (Kind.isMergeableConst8()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 703 | return EightByteConstantSection; |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 704 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 705 | return SixteenByteConstantSection; |
| 706 | return ReadOnlySection; // .const |
| 707 | } |
| 708 | |
| 709 | // FIXME: ROData -> const in -static mode that is relocatable but they happen |
| 710 | // by the static linker. Why not mergeable? |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 711 | if (Kind.isReadOnly()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 712 | return ReadOnlySection; |
| 713 | |
| 714 | // If this is marked const, put it into a const section. But if the dynamic |
| 715 | // linker needs to write to it, put it in the data segment. |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 716 | if (Kind.isReadOnlyWithRel()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 717 | return ConstDataSection; |
| 718 | |
| 719 | // Otherwise, just drop the variable in the normal data section. |
| 720 | return DataSection; |
| 721 | } |
| 722 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 723 | const MCSection * |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 724 | TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 725 | // If this constant requires a relocation, we have to put it in the data |
| 726 | // segment, not in the text segment. |
| 727 | if (Kind.isDataRel()) |
| 728 | return ConstDataSection; |
| 729 | |
| 730 | if (Kind.isMergeableConst4()) |
| 731 | return FourByteConstantSection; |
| 732 | if (Kind.isMergeableConst8()) |
| 733 | return EightByteConstantSection; |
Chris Lattner | 4bb253c | 2009-07-28 17:50:28 +0000 | [diff] [blame] | 734 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 735 | return SixteenByteConstantSection; |
| 736 | return ReadOnlySection; // .const |
| 737 | } |
| 738 | |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 739 | /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide |
| 740 | /// not to emit the UsedDirective for some symbols in llvm.used. |
| 741 | // FIXME: REMOVE this (rdar://7071300) |
| 742 | bool TargetLoweringObjectFileMachO:: |
| 743 | shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { |
| 744 | /// On Darwin, internally linked data beginning with "L" or "l" does not have |
| 745 | /// the directive emitted (this occurs in ObjC metadata). |
| 746 | if (!GV) return false; |
| 747 | |
| 748 | // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. |
| 749 | if (GV->hasLocalLinkage() && !isa<Function>(GV)) { |
| 750 | // FIXME: ObjC metadata is currently emitted as internal symbols that have |
| 751 | // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and |
| 752 | // this horrible hack can go away. |
| 753 | const std::string &Name = Mang->getMangledName(GV); |
| 754 | if (Name[0] == 'L' || Name[0] == 'l') |
| 755 | return false; |
| 756 | } |
| 757 | |
| 758 | return true; |
| 759 | } |
| 760 | |
| 761 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 762 | //===----------------------------------------------------------------------===// |
| 763 | // COFF |
| 764 | //===----------------------------------------------------------------------===// |
| 765 | |
Chris Lattner | 11e9657 | 2009-08-03 21:53:27 +0000 | [diff] [blame] | 766 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 767 | getCOFFSection(const char *Name, bool isDirective, SectionKind K) { |
| 768 | return getOrCreateSection(Name, isDirective, K); |
| 769 | } |
| 770 | |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 771 | void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, |
| 772 | const TargetMachine &TM) { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 773 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 774 | TextSection = getOrCreateSection("\t.text", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 775 | SectionKind::getText()); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 776 | DataSection = getOrCreateSection("\t.data", true, |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 777 | SectionKind::getDataRel()); |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 778 | StaticCtorSection = |
| 779 | getOrCreateSection(".ctors", false, SectionKind::getDataRel()); |
| 780 | StaticDtorSection = |
| 781 | getOrCreateSection(".dtors", false, SectionKind::getDataRel()); |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 782 | |
| 783 | |
| 784 | // Debug info. |
| 785 | // FIXME: Don't use 'directive' mode here. |
| 786 | DwarfAbbrevSection = |
| 787 | getOrCreateSection("\t.section\t.debug_abbrev,\"dr\"", |
| 788 | true, SectionKind::getMetadata()); |
| 789 | DwarfInfoSection = |
| 790 | getOrCreateSection("\t.section\t.debug_info,\"dr\"", |
| 791 | true, SectionKind::getMetadata()); |
| 792 | DwarfLineSection = |
| 793 | getOrCreateSection("\t.section\t.debug_line,\"dr\"", |
| 794 | true, SectionKind::getMetadata()); |
| 795 | DwarfFrameSection = |
| 796 | getOrCreateSection("\t.section\t.debug_frame,\"dr\"", |
| 797 | true, SectionKind::getMetadata()); |
| 798 | DwarfPubNamesSection = |
| 799 | getOrCreateSection("\t.section\t.debug_pubnames,\"dr\"", |
| 800 | true, SectionKind::getMetadata()); |
| 801 | DwarfPubTypesSection = |
| 802 | getOrCreateSection("\t.section\t.debug_pubtypes,\"dr\"", |
| 803 | true, SectionKind::getMetadata()); |
| 804 | DwarfStrSection = |
| 805 | getOrCreateSection("\t.section\t.debug_str,\"dr\"", |
| 806 | true, SectionKind::getMetadata()); |
| 807 | DwarfLocSection = |
| 808 | getOrCreateSection("\t.section\t.debug_loc,\"dr\"", |
| 809 | true, SectionKind::getMetadata()); |
| 810 | DwarfARangesSection = |
| 811 | getOrCreateSection("\t.section\t.debug_aranges,\"dr\"", |
| 812 | true, SectionKind::getMetadata()); |
| 813 | DwarfRangesSection = |
| 814 | getOrCreateSection("\t.section\t.debug_ranges,\"dr\"", |
| 815 | true, SectionKind::getMetadata()); |
| 816 | DwarfMacroInfoSection = |
| 817 | getOrCreateSection("\t.section\t.debug_macinfo,\"dr\"", |
| 818 | true, SectionKind::getMetadata()); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | void TargetLoweringObjectFileCOFF:: |
| 822 | getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const { |
| 823 | // FIXME: Inefficient. |
| 824 | std::string Res = ",\""; |
| 825 | if (Kind.isText()) |
| 826 | Res += 'x'; |
| 827 | if (Kind.isWriteable()) |
| 828 | Res += 'w'; |
| 829 | Res += "\""; |
| 830 | |
| 831 | Str.append(Res.begin(), Res.end()); |
| 832 | } |
| 833 | |
| 834 | static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 835 | if (Kind.isText()) |
| 836 | return ".text$linkonce"; |
| 837 | if (Kind.isWriteable()) |
| 838 | return ".data$linkonce"; |
| 839 | return ".rdata$linkonce"; |
| 840 | } |
| 841 | |
| 842 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 843 | const MCSection *TargetLoweringObjectFileCOFF:: |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 844 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 845 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 846 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 847 | |
| 848 | // If this global is linkonce/weak and the target handles this by emitting it |
| 849 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 27602b8 | 2009-08-01 21:42:58 +0000 | [diff] [blame] | 850 | if (GV->isWeakForLinker()) { |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 851 | const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 852 | std::string Name = Mang->makeNameProper(GV->getNameStr()); |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 853 | return getOrCreateSection((Prefix+Name).c_str(), false, Kind); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Chris Lattner | f9650c0 | 2009-08-01 21:46:23 +0000 | [diff] [blame] | 856 | if (Kind.isText()) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 857 | return getTextSection(); |
| 858 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 859 | return getDataSection(); |
| 860 | } |
| 861 | |