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