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