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