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