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