Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/TargetLoweringObjectFileImpl.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/CodeGen/TargetLoweringObjectFileImpl.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/GlobalVariable.h" |
| 20 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
| 21 | #include "llvm/MC/MCContext.h" |
| 22 | #include "llvm/MC/MCExpr.h" |
| 23 | #include "llvm/MC/MCSectionMachO.h" |
| 24 | #include "llvm/MC/MCSectionELF.h" |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSectionCOFF.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSymbol.h" |
| 27 | #include "llvm/Target/Mangler.h" |
| 28 | #include "llvm/Target/TargetData.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
| 30 | #include "llvm/Target/TargetOptions.h" |
| 31 | #include "llvm/Support/Dwarf.h" |
| 32 | #include "llvm/Support/ErrorHandling.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | #include "llvm/ADT/SmallString.h" |
| 35 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 8048ebe | 2010-09-27 06:44:54 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Triple.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 38 | using namespace dwarf; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // ELF |
| 42 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 43 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 44 | void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, |
| 45 | const TargetMachine &TM) { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 46 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
| 47 | |
| 48 | BSSSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 49 | getContext().getELFSection(".bss", MCSectionELF::SHT_NOBITS, |
| 50 | MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC, |
| 51 | SectionKind::getBSS()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 52 | |
| 53 | TextSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 54 | getContext().getELFSection(".text", MCSectionELF::SHT_PROGBITS, |
| 55 | MCSectionELF::SHF_EXECINSTR | |
| 56 | MCSectionELF::SHF_ALLOC, |
| 57 | SectionKind::getText()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 58 | |
| 59 | DataSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 60 | getContext().getELFSection(".data", MCSectionELF::SHT_PROGBITS, |
| 61 | MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC, |
| 62 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 63 | |
| 64 | ReadOnlySection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 65 | getContext().getELFSection(".rodata", MCSectionELF::SHT_PROGBITS, |
| 66 | MCSectionELF::SHF_ALLOC, |
| 67 | SectionKind::getReadOnly()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 68 | |
| 69 | TLSDataSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 70 | getContext().getELFSection(".tdata", MCSectionELF::SHT_PROGBITS, |
| 71 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | |
| 72 | MCSectionELF::SHF_WRITE, |
| 73 | SectionKind::getThreadData()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 74 | |
| 75 | TLSBSSSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 76 | getContext().getELFSection(".tbss", MCSectionELF::SHT_NOBITS, |
| 77 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | |
| 78 | MCSectionELF::SHF_WRITE, |
| 79 | SectionKind::getThreadBSS()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 80 | |
| 81 | DataRelSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 82 | getContext().getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS, |
| 83 | MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, |
| 84 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 85 | |
| 86 | DataRelLocalSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 87 | getContext().getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS, |
| 88 | MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, |
| 89 | SectionKind::getDataRelLocal()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 90 | |
| 91 | DataRelROSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 92 | getContext().getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS, |
| 93 | MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, |
| 94 | SectionKind::getReadOnlyWithRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 95 | |
| 96 | DataRelROLocalSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 97 | getContext().getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS, |
| 98 | MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, |
| 99 | SectionKind::getReadOnlyWithRelLocal()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 100 | |
| 101 | MergeableConst4Section = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 102 | getContext().getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS, |
| 103 | MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE, |
| 104 | SectionKind::getMergeableConst4()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 105 | |
| 106 | MergeableConst8Section = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 107 | getContext().getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS, |
| 108 | MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE, |
| 109 | SectionKind::getMergeableConst8()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 110 | |
| 111 | MergeableConst16Section = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 112 | getContext().getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS, |
| 113 | MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE, |
| 114 | SectionKind::getMergeableConst16()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 115 | |
| 116 | StaticCtorSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 117 | getContext().getELFSection(".ctors", MCSectionELF::SHT_PROGBITS, |
| 118 | MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, |
| 119 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 120 | |
| 121 | StaticDtorSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 122 | getContext().getELFSection(".dtors", MCSectionELF::SHT_PROGBITS, |
| 123 | MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, |
| 124 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 125 | |
| 126 | // Exception Handling Sections. |
| 127 | |
| 128 | // FIXME: We're emitting LSDA info into a readonly section on ELF, even though |
| 129 | // it contains relocatable pointers. In PIC mode, this is probably a big |
| 130 | // runtime hit for C++ apps. Either the contents of the LSDA need to be |
| 131 | // adjusted or this should be a data section. |
| 132 | LSDASection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 133 | getContext().getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS, |
| 134 | MCSectionELF::SHF_ALLOC, |
| 135 | SectionKind::getReadOnly()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 136 | EHFrameSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 137 | getContext().getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS, |
| 138 | MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE, |
| 139 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 140 | |
| 141 | // Debug Info Sections. |
| 142 | DwarfAbbrevSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 143 | getContext().getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0, |
| 144 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 145 | DwarfInfoSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 146 | getContext().getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0, |
| 147 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 148 | DwarfLineSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 149 | getContext().getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0, |
| 150 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 151 | DwarfFrameSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 152 | getContext().getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0, |
| 153 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 154 | DwarfPubNamesSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 155 | getContext().getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0, |
| 156 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 157 | DwarfPubTypesSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 158 | getContext().getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0, |
| 159 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 160 | DwarfStrSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 161 | getContext().getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0, |
| 162 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 163 | DwarfLocSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 164 | getContext().getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0, |
| 165 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 166 | DwarfARangesSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 167 | getContext().getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0, |
| 168 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 169 | DwarfRangesSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 170 | getContext().getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0, |
| 171 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 172 | DwarfMacroInfoSection = |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 173 | getContext().getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0, |
| 174 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
| 178 | static SectionKind |
| 179 | getELFKindForNamedSection(StringRef Name, SectionKind K) { |
| 180 | if (Name.empty() || Name[0] != '.') return K; |
| 181 | |
| 182 | // Some lame default implementation based on some magic section names. |
| 183 | if (Name == ".bss" || |
| 184 | Name.startswith(".bss.") || |
| 185 | Name.startswith(".gnu.linkonce.b.") || |
| 186 | Name.startswith(".llvm.linkonce.b.") || |
| 187 | Name == ".sbss" || |
| 188 | Name.startswith(".sbss.") || |
| 189 | Name.startswith(".gnu.linkonce.sb.") || |
| 190 | Name.startswith(".llvm.linkonce.sb.")) |
| 191 | return SectionKind::getBSS(); |
| 192 | |
| 193 | if (Name == ".tdata" || |
| 194 | Name.startswith(".tdata.") || |
| 195 | Name.startswith(".gnu.linkonce.td.") || |
| 196 | Name.startswith(".llvm.linkonce.td.")) |
| 197 | return SectionKind::getThreadData(); |
| 198 | |
| 199 | if (Name == ".tbss" || |
| 200 | Name.startswith(".tbss.") || |
| 201 | Name.startswith(".gnu.linkonce.tb.") || |
| 202 | Name.startswith(".llvm.linkonce.tb.")) |
| 203 | return SectionKind::getThreadBSS(); |
| 204 | |
| 205 | return K; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | static unsigned getELFSectionType(StringRef Name, SectionKind K) { |
| 210 | |
| 211 | if (Name == ".init_array") |
| 212 | return MCSectionELF::SHT_INIT_ARRAY; |
| 213 | |
| 214 | if (Name == ".fini_array") |
| 215 | return MCSectionELF::SHT_FINI_ARRAY; |
| 216 | |
| 217 | if (Name == ".preinit_array") |
| 218 | return MCSectionELF::SHT_PREINIT_ARRAY; |
| 219 | |
| 220 | if (K.isBSS() || K.isThreadBSS()) |
| 221 | return MCSectionELF::SHT_NOBITS; |
| 222 | |
| 223 | return MCSectionELF::SHT_PROGBITS; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | static unsigned |
| 228 | getELFSectionFlags(SectionKind K) { |
| 229 | unsigned Flags = 0; |
| 230 | |
| 231 | if (!K.isMetadata()) |
| 232 | Flags |= MCSectionELF::SHF_ALLOC; |
| 233 | |
| 234 | if (K.isText()) |
| 235 | Flags |= MCSectionELF::SHF_EXECINSTR; |
| 236 | |
| 237 | if (K.isWriteable()) |
| 238 | Flags |= MCSectionELF::SHF_WRITE; |
| 239 | |
| 240 | if (K.isThreadLocal()) |
| 241 | Flags |= MCSectionELF::SHF_TLS; |
| 242 | |
| 243 | // K.isMergeableConst() is left out to honour PR4650 |
| 244 | if (K.isMergeableCString() || K.isMergeableConst4() || |
| 245 | K.isMergeableConst8() || K.isMergeableConst16()) |
| 246 | Flags |= MCSectionELF::SHF_MERGE; |
| 247 | |
| 248 | if (K.isMergeableCString()) |
| 249 | Flags |= MCSectionELF::SHF_STRINGS; |
| 250 | |
| 251 | return Flags; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | const MCSection *TargetLoweringObjectFileELF:: |
| 256 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 257 | Mangler *Mang, const TargetMachine &TM) const { |
| 258 | StringRef SectionName = GV->getSection(); |
| 259 | |
| 260 | // Infer section flags from the section name if we can. |
| 261 | Kind = getELFKindForNamedSection(SectionName, Kind); |
| 262 | |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 263 | return getContext().getELFSection(SectionName, |
| 264 | getELFSectionType(SectionName, Kind), |
| 265 | getELFSectionFlags(Kind), Kind, true); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 269 | if (Kind.isText()) return ".gnu.linkonce.t."; |
| 270 | if (Kind.isReadOnly()) return ".gnu.linkonce.r."; |
| 271 | |
| 272 | if (Kind.isThreadData()) return ".gnu.linkonce.td."; |
| 273 | if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; |
| 274 | |
| 275 | if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; |
| 276 | if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; |
| 277 | if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; |
| 278 | if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; |
| 279 | |
| 280 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 281 | return ".gnu.linkonce.d.rel.ro."; |
| 282 | } |
| 283 | |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 284 | /// getSectionPrefixForGlobal - Return the section prefix name used by options |
| 285 | /// FunctionsSections and DataSections. |
| 286 | static const char *getSectionPrefixForGlobal(SectionKind Kind) { |
| 287 | if (Kind.isText()) return ".text."; |
| 288 | if (Kind.isReadOnly()) return ".rodata."; |
| 289 | |
| 290 | if (Kind.isThreadData()) return ".tdata."; |
| 291 | if (Kind.isThreadBSS()) return ".tbss."; |
| 292 | |
| 293 | if (Kind.isDataNoRel()) return ".data."; |
| 294 | if (Kind.isDataRelLocal()) return ".data.rel.local."; |
| 295 | if (Kind.isDataRel()) return ".data.rel."; |
| 296 | if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local."; |
| 297 | |
| 298 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 299 | return ".data.rel.ro."; |
| 300 | } |
| 301 | |
| 302 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 303 | const MCSection *TargetLoweringObjectFileELF:: |
| 304 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
| 305 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 306 | // If we have -ffunction-section or -fdata-section then we should emit the |
| 307 | // global value to a uniqued section specifically for it. |
| 308 | bool EmitUniquedSection; |
| 309 | if (Kind.isText()) |
| 310 | EmitUniquedSection = TM.getFunctionSections(); |
| 311 | else |
| 312 | EmitUniquedSection = TM.getDataSections(); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 313 | |
| 314 | // If this global is linkonce/weak and the target handles this by emitting it |
| 315 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 316 | if ((GV->isWeakForLinker() || EmitUniquedSection) && |
| 317 | !Kind.isCommon() && !Kind.isBSS()) { |
| 318 | const char *Prefix; |
| 319 | if (GV->isWeakForLinker()) |
| 320 | Prefix = getSectionPrefixForUniqueGlobal(Kind); |
| 321 | else { |
| 322 | assert(EmitUniquedSection); |
| 323 | Prefix = getSectionPrefixForGlobal(Kind); |
| 324 | } |
| 325 | |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 326 | SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); |
| 327 | MCSymbol *Sym = Mang->getSymbol(GV); |
| 328 | Name.append(Sym->getName().begin(), Sym->getName().end()); |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 329 | return getContext().getELFSection(Name.str(), |
| 330 | getELFSectionType(Name.str(), Kind), |
| 331 | getELFSectionFlags(Kind), Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | if (Kind.isText()) return TextSection; |
| 335 | |
| 336 | if (Kind.isMergeable1ByteCString() || |
| 337 | Kind.isMergeable2ByteCString() || |
| 338 | Kind.isMergeable4ByteCString()) { |
| 339 | |
| 340 | // We also need alignment here. |
| 341 | // FIXME: this is getting the alignment of the character, not the |
| 342 | // alignment of the global! |
| 343 | unsigned Align = |
| 344 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); |
| 345 | |
| 346 | const char *SizeSpec = ".rodata.str1."; |
| 347 | if (Kind.isMergeable2ByteCString()) |
| 348 | SizeSpec = ".rodata.str2."; |
| 349 | else if (Kind.isMergeable4ByteCString()) |
| 350 | SizeSpec = ".rodata.str4."; |
| 351 | else |
| 352 | assert(Kind.isMergeable1ByteCString() && "unknown string width"); |
| 353 | |
| 354 | |
| 355 | std::string Name = SizeSpec + utostr(Align); |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 356 | return getContext().getELFSection(Name, MCSectionELF::SHT_PROGBITS, |
| 357 | MCSectionELF::SHF_ALLOC | |
| 358 | MCSectionELF::SHF_MERGE | |
| 359 | MCSectionELF::SHF_STRINGS, |
| 360 | Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | if (Kind.isMergeableConst()) { |
| 364 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 365 | return MergeableConst4Section; |
| 366 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 367 | return MergeableConst8Section; |
| 368 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 369 | return MergeableConst16Section; |
| 370 | return ReadOnlySection; // .const |
| 371 | } |
| 372 | |
| 373 | if (Kind.isReadOnly()) return ReadOnlySection; |
| 374 | |
| 375 | if (Kind.isThreadData()) return TLSDataSection; |
| 376 | if (Kind.isThreadBSS()) return TLSBSSSection; |
| 377 | |
| 378 | // Note: we claim that common symbols are put in BSSSection, but they are |
| 379 | // really emitted with the magic .comm directive, which creates a symbol table |
| 380 | // entry but not a section. |
| 381 | if (Kind.isBSS() || Kind.isCommon()) return BSSSection; |
| 382 | |
| 383 | if (Kind.isDataNoRel()) return DataSection; |
| 384 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 385 | if (Kind.isDataRel()) return DataRelSection; |
| 386 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 387 | |
| 388 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 389 | return DataRelROSection; |
| 390 | } |
| 391 | |
| 392 | /// getSectionForConstant - Given a mergeable constant with the |
| 393 | /// specified size and relocation information, return a section that it |
| 394 | /// should be placed in. |
| 395 | const MCSection *TargetLoweringObjectFileELF:: |
| 396 | getSectionForConstant(SectionKind Kind) const { |
| 397 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 398 | return MergeableConst4Section; |
| 399 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 400 | return MergeableConst8Section; |
| 401 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 402 | return MergeableConst16Section; |
| 403 | if (Kind.isReadOnly()) |
| 404 | return ReadOnlySection; |
| 405 | |
| 406 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 407 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 408 | return DataRelROSection; |
| 409 | } |
| 410 | |
| 411 | const MCExpr *TargetLoweringObjectFileELF:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 412 | getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, |
| 413 | MachineModuleInfo *MMI, |
| 414 | unsigned Encoding, MCStreamer &Streamer) const { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 415 | |
| 416 | if (Encoding & dwarf::DW_EH_PE_indirect) { |
| 417 | MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); |
| 418 | |
| 419 | SmallString<128> Name; |
| 420 | Mang->getNameWithPrefix(Name, GV, true); |
| 421 | Name += ".DW.stub"; |
| 422 | |
| 423 | // Add information about the stub reference to ELFMMI so that the stub |
| 424 | // gets emitted by the asmprinter. |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 425 | MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str()); |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 426 | MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 427 | if (StubSym.getPointer() == 0) { |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 428 | MCSymbol *Sym = Mang->getSymbol(GV); |
| 429 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | return TargetLoweringObjectFile:: |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 433 | getExprForDwarfReference(SSym, Mang, MMI, |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 434 | Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | return TargetLoweringObjectFile:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 438 | getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | //===----------------------------------------------------------------------===// |
| 442 | // MachO |
| 443 | //===----------------------------------------------------------------------===// |
| 444 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 445 | void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, |
| 446 | const TargetMachine &TM) { |
Bill Wendling | 2ff6e1e | 2010-09-28 23:11:55 +0000 | [diff] [blame] | 447 | // _foo.eh symbols are currently always exported so that the linker knows |
| 448 | // about them. This is not necessary on 10.6 and later, but it |
| 449 | // doesn't hurt anything. |
| 450 | // FIXME: I need to get this from Triple. |
Chris Lattner | 09d53fe | 2010-03-10 07:20:42 +0000 | [diff] [blame] | 451 | IsFunctionEHSymbolGlobal = true; |
| 452 | IsFunctionEHFrameSymbolPrivate = false; |
| 453 | SupportsWeakOmittedEHFrame = false; |
Chris Lattner | 8048ebe | 2010-09-27 06:44:54 +0000 | [diff] [blame] | 454 | |
| 455 | Triple T(((LLVMTargetMachine&)TM).getTargetTriple()); |
| 456 | if (T.getOS() == Triple::Darwin) { |
Bill Wendling | 2ff6e1e | 2010-09-28 23:11:55 +0000 | [diff] [blame] | 457 | switch (T.getDarwinMajorNumber()) { |
| 458 | case 7: // 10.3 Panther. |
| 459 | case 8: // 10.4 Tiger. |
Chris Lattner | 8048ebe | 2010-09-27 06:44:54 +0000 | [diff] [blame] | 460 | CommDirectiveSupportsAlignment = false; |
Bill Wendling | 2ff6e1e | 2010-09-28 23:11:55 +0000 | [diff] [blame] | 461 | break; |
| 462 | case 9: // 10.5 Leopard. |
| 463 | case 10: // 10.6 SnowLeopard. |
| 464 | break; |
| 465 | } |
Chris Lattner | 8048ebe | 2010-09-27 06:44:54 +0000 | [diff] [blame] | 466 | } |
Bill Wendling | 2ff6e1e | 2010-09-28 23:11:55 +0000 | [diff] [blame] | 467 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 468 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
| 469 | |
| 470 | TextSection // .text |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 471 | = getContext().getMachOSection("__TEXT", "__text", |
| 472 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 473 | SectionKind::getText()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 474 | DataSection // .data |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 475 | = getContext().getMachOSection("__DATA", "__data", 0, |
| 476 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 477 | |
Eric Christopher | 423c9e3 | 2010-05-17 21:02:07 +0000 | [diff] [blame] | 478 | TLSDataSection // .tdata |
| 479 | = getContext().getMachOSection("__DATA", "__thread_data", |
| 480 | MCSectionMachO::S_THREAD_LOCAL_REGULAR, |
| 481 | SectionKind::getDataRel()); |
| 482 | TLSBSSSection // .tbss |
| 483 | = getContext().getMachOSection("__DATA", "__thread_bss", |
| 484 | MCSectionMachO::S_THREAD_LOCAL_ZEROFILL, |
| 485 | SectionKind::getThreadBSS()); |
| 486 | |
| 487 | // TODO: Verify datarel below. |
| 488 | TLSTLVSection // .tlv |
| 489 | = getContext().getMachOSection("__DATA", "__thread_vars", |
| 490 | MCSectionMachO::S_THREAD_LOCAL_VARIABLES, |
| 491 | SectionKind::getDataRel()); |
Eric Christopher | c6177a4 | 2010-05-17 22:53:55 +0000 | [diff] [blame] | 492 | |
| 493 | TLSThreadInitSection |
| 494 | = getContext().getMachOSection("__DATA", "__thread_init", |
| 495 | MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS, |
| 496 | SectionKind::getDataRel()); |
| 497 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 498 | CStringSection // .cstring |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 499 | = getContext().getMachOSection("__TEXT", "__cstring", |
| 500 | MCSectionMachO::S_CSTRING_LITERALS, |
| 501 | SectionKind::getMergeable1ByteCString()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 502 | UStringSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 503 | = getContext().getMachOSection("__TEXT","__ustring", 0, |
| 504 | SectionKind::getMergeable2ByteCString()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 505 | FourByteConstantSection // .literal4 |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 506 | = getContext().getMachOSection("__TEXT", "__literal4", |
| 507 | MCSectionMachO::S_4BYTE_LITERALS, |
| 508 | SectionKind::getMergeableConst4()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 509 | EightByteConstantSection // .literal8 |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 510 | = getContext().getMachOSection("__TEXT", "__literal8", |
| 511 | MCSectionMachO::S_8BYTE_LITERALS, |
| 512 | SectionKind::getMergeableConst8()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 513 | |
| 514 | // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back |
| 515 | // to using it in -static mode. |
| 516 | SixteenByteConstantSection = 0; |
| 517 | if (TM.getRelocationModel() != Reloc::Static && |
| 518 | TM.getTargetData()->getPointerSize() == 32) |
| 519 | SixteenByteConstantSection = // .literal16 |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 520 | getContext().getMachOSection("__TEXT", "__literal16", |
| 521 | MCSectionMachO::S_16BYTE_LITERALS, |
| 522 | SectionKind::getMergeableConst16()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 523 | |
| 524 | ReadOnlySection // .const |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 525 | = getContext().getMachOSection("__TEXT", "__const", 0, |
| 526 | SectionKind::getReadOnly()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 527 | |
| 528 | TextCoalSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 529 | = getContext().getMachOSection("__TEXT", "__textcoal_nt", |
| 530 | MCSectionMachO::S_COALESCED | |
| 531 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 532 | SectionKind::getText()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 533 | ConstTextCoalSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 534 | = getContext().getMachOSection("__TEXT", "__const_coal", |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 535 | MCSectionMachO::S_COALESCED, |
Chris Lattner | 6a624a6 | 2010-07-15 21:22:00 +0000 | [diff] [blame] | 536 | SectionKind::getReadOnly()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 537 | ConstDataSection // .const_data |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 538 | = getContext().getMachOSection("__DATA", "__const", 0, |
| 539 | SectionKind::getReadOnlyWithRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 540 | DataCoalSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 541 | = getContext().getMachOSection("__DATA","__datacoal_nt", |
| 542 | MCSectionMachO::S_COALESCED, |
| 543 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 544 | DataCommonSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 545 | = getContext().getMachOSection("__DATA","__common", |
| 546 | MCSectionMachO::S_ZEROFILL, |
| 547 | SectionKind::getBSS()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 548 | DataBSSSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 549 | = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL, |
| 550 | SectionKind::getBSS()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 551 | |
| 552 | |
| 553 | LazySymbolPointerSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 554 | = getContext().getMachOSection("__DATA", "__la_symbol_ptr", |
| 555 | MCSectionMachO::S_LAZY_SYMBOL_POINTERS, |
| 556 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 557 | NonLazySymbolPointerSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 558 | = getContext().getMachOSection("__DATA", "__nl_symbol_ptr", |
| 559 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, |
| 560 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 561 | |
| 562 | if (TM.getRelocationModel() == Reloc::Static) { |
| 563 | StaticCtorSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 564 | = getContext().getMachOSection("__TEXT", "__constructor", 0, |
| 565 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 566 | StaticDtorSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 567 | = getContext().getMachOSection("__TEXT", "__destructor", 0, |
| 568 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 569 | } else { |
| 570 | StaticCtorSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 571 | = getContext().getMachOSection("__DATA", "__mod_init_func", |
| 572 | MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, |
| 573 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 574 | StaticDtorSection |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 575 | = getContext().getMachOSection("__DATA", "__mod_term_func", |
| 576 | MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, |
| 577 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // Exception Handling. |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 581 | LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0, |
| 582 | SectionKind::getReadOnlyWithRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 583 | EHFrameSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 584 | getContext().getMachOSection("__TEXT", "__eh_frame", |
| 585 | MCSectionMachO::S_COALESCED | |
| 586 | MCSectionMachO::S_ATTR_NO_TOC | |
| 587 | MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS | |
| 588 | MCSectionMachO::S_ATTR_LIVE_SUPPORT, |
| 589 | SectionKind::getReadOnly()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 590 | |
| 591 | // Debug Information. |
| 592 | DwarfAbbrevSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 593 | getContext().getMachOSection("__DWARF", "__debug_abbrev", |
| 594 | MCSectionMachO::S_ATTR_DEBUG, |
| 595 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 596 | DwarfInfoSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 597 | getContext().getMachOSection("__DWARF", "__debug_info", |
| 598 | MCSectionMachO::S_ATTR_DEBUG, |
| 599 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 600 | DwarfLineSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 601 | getContext().getMachOSection("__DWARF", "__debug_line", |
| 602 | MCSectionMachO::S_ATTR_DEBUG, |
| 603 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 604 | DwarfFrameSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 605 | getContext().getMachOSection("__DWARF", "__debug_frame", |
| 606 | MCSectionMachO::S_ATTR_DEBUG, |
| 607 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 608 | DwarfPubNamesSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 609 | getContext().getMachOSection("__DWARF", "__debug_pubnames", |
| 610 | MCSectionMachO::S_ATTR_DEBUG, |
| 611 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 612 | DwarfPubTypesSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 613 | getContext().getMachOSection("__DWARF", "__debug_pubtypes", |
| 614 | MCSectionMachO::S_ATTR_DEBUG, |
| 615 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 616 | DwarfStrSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 617 | getContext().getMachOSection("__DWARF", "__debug_str", |
| 618 | MCSectionMachO::S_ATTR_DEBUG, |
| 619 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 620 | DwarfLocSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 621 | getContext().getMachOSection("__DWARF", "__debug_loc", |
| 622 | MCSectionMachO::S_ATTR_DEBUG, |
| 623 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 624 | DwarfARangesSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 625 | getContext().getMachOSection("__DWARF", "__debug_aranges", |
| 626 | MCSectionMachO::S_ATTR_DEBUG, |
| 627 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 628 | DwarfRangesSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 629 | getContext().getMachOSection("__DWARF", "__debug_ranges", |
| 630 | MCSectionMachO::S_ATTR_DEBUG, |
| 631 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 632 | DwarfMacroInfoSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 633 | getContext().getMachOSection("__DWARF", "__debug_macinfo", |
| 634 | MCSectionMachO::S_ATTR_DEBUG, |
| 635 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 636 | DwarfDebugInlineSection = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 637 | getContext().getMachOSection("__DWARF", "__debug_inlined", |
| 638 | MCSectionMachO::S_ATTR_DEBUG, |
| 639 | SectionKind::getMetadata()); |
Eric Christopher | 8116ca5 | 2010-05-22 00:10:22 +0000 | [diff] [blame] | 640 | |
| 641 | TLSExtraDataSection = TLSTLVSection; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | const MCSection *TargetLoweringObjectFileMachO:: |
| 645 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 646 | Mangler *Mang, const TargetMachine &TM) const { |
| 647 | // Parse the section specifier and create it if valid. |
| 648 | StringRef Segment, Section; |
| 649 | unsigned TAA, StubSize; |
| 650 | std::string ErrorCode = |
| 651 | MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, |
| 652 | TAA, StubSize); |
| 653 | if (!ErrorCode.empty()) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 654 | // If invalid, report the error with report_fatal_error. |
| 655 | report_fatal_error("Global variable '" + GV->getNameStr() + |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 656 | "' has an invalid section specifier '" + GV->getSection()+ |
| 657 | "': " + ErrorCode + "."); |
| 658 | // Fall back to dropping it into the data section. |
| 659 | return DataSection; |
| 660 | } |
| 661 | |
| 662 | // Get the section. |
| 663 | const MCSectionMachO *S = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 664 | getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 665 | |
| 666 | // Okay, now that we got the section, verify that the TAA & StubSize agree. |
| 667 | // If the user declared multiple globals with different section flags, we need |
| 668 | // to reject it here. |
| 669 | if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 670 | // If invalid, report the error with report_fatal_error. |
| 671 | report_fatal_error("Global variable '" + GV->getNameStr() + |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 672 | "' section type or attributes does not match previous" |
| 673 | " section specifier"); |
| 674 | } |
| 675 | |
| 676 | return S; |
| 677 | } |
| 678 | |
| 679 | const MCSection *TargetLoweringObjectFileMachO:: |
| 680 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Eric Christopher | 8116ca5 | 2010-05-22 00:10:22 +0000 | [diff] [blame] | 681 | Mangler *Mang, const TargetMachine &TM) const { |
| 682 | |
Eric Christopher | 02b46bc | 2010-05-25 21:28:50 +0000 | [diff] [blame] | 683 | // Handle thread local data. |
Eric Christopher | 8116ca5 | 2010-05-22 00:10:22 +0000 | [diff] [blame] | 684 | if (Kind.isThreadBSS()) return TLSBSSSection; |
Eric Christopher | 02b46bc | 2010-05-25 21:28:50 +0000 | [diff] [blame] | 685 | if (Kind.isThreadData()) return TLSDataSection; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 686 | |
| 687 | if (Kind.isText()) |
| 688 | return GV->isWeakForLinker() ? TextCoalSection : TextSection; |
| 689 | |
| 690 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 691 | // or data depending on if it is writable. |
| 692 | if (GV->isWeakForLinker()) { |
| 693 | if (Kind.isReadOnly()) |
| 694 | return ConstTextCoalSection; |
| 695 | return DataCoalSection; |
| 696 | } |
| 697 | |
| 698 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | 98f15d2 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 699 | if (Kind.isMergeable1ByteCString() && |
| 700 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) |
| 701 | return CStringSection; |
| 702 | |
| 703 | // Do not put 16-bit arrays in the UString section if they have an |
| 704 | // externally visible label, this runs into issues with certain linker |
| 705 | // versions. |
| 706 | if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && |
| 707 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) |
| 708 | return UStringSection; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 709 | |
| 710 | if (Kind.isMergeableConst()) { |
| 711 | if (Kind.isMergeableConst4()) |
| 712 | return FourByteConstantSection; |
| 713 | if (Kind.isMergeableConst8()) |
| 714 | return EightByteConstantSection; |
| 715 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
| 716 | return SixteenByteConstantSection; |
| 717 | } |
| 718 | |
| 719 | // Otherwise, if it is readonly, but not something we can specially optimize, |
| 720 | // just drop it in .const. |
| 721 | if (Kind.isReadOnly()) |
| 722 | return ReadOnlySection; |
| 723 | |
| 724 | // If this is marked const, put it into a const section. But if the dynamic |
| 725 | // linker needs to write to it, put it in the data segment. |
| 726 | if (Kind.isReadOnlyWithRel()) |
| 727 | return ConstDataSection; |
| 728 | |
| 729 | // Put zero initialized globals with strong external linkage in the |
| 730 | // DATA, __common section with the .zerofill directive. |
| 731 | if (Kind.isBSSExtern()) |
| 732 | return DataCommonSection; |
| 733 | |
| 734 | // Put zero initialized globals with local linkage in __DATA,__bss directive |
| 735 | // with the .zerofill directive (aka .lcomm). |
| 736 | if (Kind.isBSSLocal()) |
| 737 | return DataBSSSection; |
| 738 | |
| 739 | // Otherwise, just drop the variable in the normal data section. |
| 740 | return DataSection; |
| 741 | } |
| 742 | |
| 743 | const MCSection * |
| 744 | TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { |
| 745 | // If this constant requires a relocation, we have to put it in the data |
| 746 | // segment, not in the text segment. |
| 747 | if (Kind.isDataRel() || Kind.isReadOnlyWithRel()) |
| 748 | return ConstDataSection; |
| 749 | |
| 750 | if (Kind.isMergeableConst4()) |
| 751 | return FourByteConstantSection; |
| 752 | if (Kind.isMergeableConst8()) |
| 753 | return EightByteConstantSection; |
| 754 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
| 755 | return SixteenByteConstantSection; |
| 756 | return ReadOnlySection; // .const |
| 757 | } |
| 758 | |
| 759 | /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide |
| 760 | /// not to emit the UsedDirective for some symbols in llvm.used. |
| 761 | // FIXME: REMOVE this (rdar://7071300) |
| 762 | bool TargetLoweringObjectFileMachO:: |
| 763 | shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { |
| 764 | /// On Darwin, internally linked data beginning with "L" or "l" does not have |
| 765 | /// the directive emitted (this occurs in ObjC metadata). |
| 766 | if (!GV) return false; |
| 767 | |
Bill Wendling | 07d3177 | 2010-06-29 22:34:52 +0000 | [diff] [blame] | 768 | // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 769 | if (GV->hasLocalLinkage() && !isa<Function>(GV)) { |
| 770 | // FIXME: ObjC metadata is currently emitted as internal symbols that have |
Bill Wendling | 07d3177 | 2010-06-29 22:34:52 +0000 | [diff] [blame] | 771 | // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and |
| 772 | // this horrible hack can go away. |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 773 | MCSymbol *Sym = Mang->getSymbol(GV); |
| 774 | if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l') |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 775 | return false; |
| 776 | } |
| 777 | |
| 778 | return true; |
| 779 | } |
| 780 | |
| 781 | const MCExpr *TargetLoweringObjectFileMachO:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 782 | getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, |
| 783 | MachineModuleInfo *MMI, unsigned Encoding, |
| 784 | MCStreamer &Streamer) const { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 785 | // The mach-o version of this method defaults to returning a stub reference. |
| 786 | |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 787 | if (Encoding & DW_EH_PE_indirect) { |
| 788 | MachineModuleInfoMachO &MachOMMI = |
| 789 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 790 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 791 | SmallString<128> Name; |
| 792 | Mang->getNameWithPrefix(Name, GV, true); |
| 793 | Name += "$non_lazy_ptr"; |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 794 | |
| 795 | // Add information about the stub reference to MachOMMI so that the stub |
| 796 | // gets emitted by the asmprinter. |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 797 | MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str()); |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 798 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 799 | if (StubSym.getPointer() == 0) { |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 800 | MCSymbol *Sym = Mang->getSymbol(GV); |
| 801 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 802 | } |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 803 | |
| 804 | return TargetLoweringObjectFile:: |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 805 | getExprForDwarfReference(SSym, Mang, MMI, |
Chris Lattner | 42263e2 | 2010-03-11 21:55:20 +0000 | [diff] [blame] | 806 | Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | return TargetLoweringObjectFile:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 810 | getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 813 | unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const { |
| 814 | return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4; |
| 815 | } |
| 816 | |
| 817 | unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const { |
| 818 | return DW_EH_PE_pcrel; |
| 819 | } |
| 820 | |
| 821 | unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const { |
| 822 | return DW_EH_PE_pcrel; |
| 823 | } |
| 824 | |
| 825 | unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const { |
Bill Wendling | 505ad8b | 2010-03-15 21:09:38 +0000 | [diff] [blame] | 826 | return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4; |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 827 | } |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 828 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 829 | //===----------------------------------------------------------------------===// |
| 830 | // COFF |
| 831 | //===----------------------------------------------------------------------===// |
| 832 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 833 | void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, |
| 834 | const TargetMachine &TM) { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 835 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 836 | TextSection = |
| 837 | getContext().getCOFFSection(".text", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 838 | COFF::IMAGE_SCN_CNT_CODE | |
| 839 | COFF::IMAGE_SCN_MEM_EXECUTE | |
| 840 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 841 | SectionKind::getText()); |
| 842 | DataSection = |
| 843 | getContext().getCOFFSection(".data", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 844 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 845 | COFF::IMAGE_SCN_MEM_READ | |
| 846 | COFF::IMAGE_SCN_MEM_WRITE, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 847 | SectionKind::getDataRel()); |
| 848 | ReadOnlySection = |
| 849 | getContext().getCOFFSection(".rdata", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 850 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 851 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 852 | SectionKind::getReadOnly()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 853 | StaticCtorSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 854 | getContext().getCOFFSection(".ctors", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 855 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 856 | COFF::IMAGE_SCN_MEM_READ | |
| 857 | COFF::IMAGE_SCN_MEM_WRITE, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 858 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 859 | StaticDtorSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 860 | getContext().getCOFFSection(".dtors", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 861 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 862 | COFF::IMAGE_SCN_MEM_READ | |
| 863 | COFF::IMAGE_SCN_MEM_WRITE, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 864 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 865 | |
| 866 | // FIXME: We're emitting LSDA info into a readonly section on COFF, even |
| 867 | // though it contains relocatable pointers. In PIC mode, this is probably a |
| 868 | // big runtime hit for C++ apps. Either the contents of the LSDA need to be |
| 869 | // adjusted or this should be a data section. |
| 870 | LSDASection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 871 | getContext().getCOFFSection(".gcc_except_table", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 872 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 873 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 874 | SectionKind::getReadOnly()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 875 | EHFrameSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 876 | getContext().getCOFFSection(".eh_frame", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 877 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 878 | COFF::IMAGE_SCN_MEM_READ | |
| 879 | COFF::IMAGE_SCN_MEM_WRITE, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 880 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 881 | |
| 882 | // Debug info. |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 883 | DwarfAbbrevSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 884 | getContext().getCOFFSection(".debug_abbrev", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 885 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 886 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 887 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 888 | DwarfInfoSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 889 | getContext().getCOFFSection(".debug_info", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 890 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 891 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 892 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 893 | DwarfLineSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 894 | getContext().getCOFFSection(".debug_line", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 895 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 896 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 897 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 898 | DwarfFrameSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 899 | getContext().getCOFFSection(".debug_frame", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 900 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 901 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 902 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 903 | DwarfPubNamesSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 904 | getContext().getCOFFSection(".debug_pubnames", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 905 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 906 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 907 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 908 | DwarfPubTypesSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 909 | getContext().getCOFFSection(".debug_pubtypes", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 910 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 911 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 912 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 913 | DwarfStrSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 914 | getContext().getCOFFSection(".debug_str", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 915 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 916 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 917 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 918 | DwarfLocSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 919 | getContext().getCOFFSection(".debug_loc", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 920 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 921 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 922 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 923 | DwarfARangesSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 924 | getContext().getCOFFSection(".debug_aranges", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 925 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 926 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 927 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 928 | DwarfRangesSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 929 | getContext().getCOFFSection(".debug_ranges", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 930 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 931 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 932 | SectionKind::getMetadata()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 933 | DwarfMacroInfoSection = |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 934 | getContext().getCOFFSection(".debug_macinfo", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 935 | COFF::IMAGE_SCN_MEM_DISCARDABLE | |
| 936 | COFF::IMAGE_SCN_MEM_READ, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 937 | SectionKind::getMetadata()); |
| 938 | |
| 939 | DrectveSection = |
| 940 | getContext().getCOFFSection(".drectve", |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 941 | COFF::IMAGE_SCN_LNK_INFO, |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 942 | SectionKind::getMetadata()); |
| 943 | } |
| 944 | |
| 945 | static unsigned |
| 946 | getCOFFSectionFlags(SectionKind K) { |
| 947 | unsigned Flags = 0; |
| 948 | |
Anton Korobeynikov | 36335be | 2010-07-06 15:24:56 +0000 | [diff] [blame] | 949 | if (K.isMetadata()) |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 950 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 951 | COFF::IMAGE_SCN_MEM_DISCARDABLE; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 952 | else if (K.isText()) |
| 953 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 954 | COFF::IMAGE_SCN_MEM_EXECUTE | |
| 955 | COFF::IMAGE_SCN_CNT_CODE; |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 956 | else if (K.isBSS ()) |
| 957 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 958 | COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | |
| 959 | COFF::IMAGE_SCN_MEM_READ | |
| 960 | COFF::IMAGE_SCN_MEM_WRITE; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 961 | else if (K.isReadOnly()) |
| 962 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 963 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 964 | COFF::IMAGE_SCN_MEM_READ; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 965 | else if (K.isWriteable()) |
| 966 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 967 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 968 | COFF::IMAGE_SCN_MEM_READ | |
| 969 | COFF::IMAGE_SCN_MEM_WRITE; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 970 | |
| 971 | return Flags; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 975 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 976 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 977 | return getContext().getCOFFSection(GV->getSection(), |
| 978 | getCOFFSectionFlags(Kind), |
| 979 | Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 983 | if (Kind.isText()) |
NAKAMURA Takumi | 3b3b0eb | 2010-10-19 03:24:42 +0000 | [diff] [blame^] | 984 | return ".text$"; |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 985 | if (Kind.isBSS ()) |
NAKAMURA Takumi | 3b3b0eb | 2010-10-19 03:24:42 +0000 | [diff] [blame^] | 986 | return ".bss$"; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 987 | if (Kind.isWriteable()) |
NAKAMURA Takumi | 3b3b0eb | 2010-10-19 03:24:42 +0000 | [diff] [blame^] | 988 | return ".data$"; |
| 989 | return ".rdata$"; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | |
| 993 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 994 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
| 995 | Mangler *Mang, const TargetMachine &TM) const { |
| 996 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
| 997 | |
| 998 | // If this global is linkonce/weak and the target handles this by emitting it |
| 999 | // into a 'uniqued' section name, create and return the section now. |
| 1000 | if (GV->isWeakForLinker()) { |
| 1001 | const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); |
| 1002 | SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 1003 | MCSymbol *Sym = Mang->getSymbol(GV); |
NAKAMURA Takumi | 3b3b0eb | 2010-10-19 03:24:42 +0000 | [diff] [blame^] | 1004 | Name.append(Sym->getName().begin() + 1, Sym->getName().end()); |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 1005 | |
| 1006 | unsigned Characteristics = getCOFFSectionFlags(Kind); |
| 1007 | |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 1008 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 1009 | |
| 1010 | return getContext().getCOFFSection(Name.str(), Characteristics, |
Anton Korobeynikov | 657985e | 2010-10-08 21:50:04 +0000 | [diff] [blame] | 1011 | COFF::IMAGE_COMDAT_SELECT_ANY, Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | if (Kind.isText()) |
| 1015 | return getTextSection(); |
| 1016 | |
| 1017 | return getDataSection(); |
| 1018 | } |
| 1019 | |