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