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