TargetLoweringObjectFileImpl.cpp revision 4a99f59aef358fb93eac180e49f6dcef03822046
1362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===//
2362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//
3362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//                     The LLVM Compiler Infrastructure
4362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//
5362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov// This file is distributed under the University of Illinois Open Source
6362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov// License. See LICENSE.TXT for details.
7362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//
8362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//===----------------------------------------------------------------------===//
9362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//
10362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov// This file implements classes used to handle lowerings specific to common
11362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov// object file formats.
12362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//
13362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//===----------------------------------------------------------------------===//
14362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
15362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
16362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/Constants.h"
17362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/DerivedTypes.h"
18362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/Function.h"
19362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/GlobalVariable.h"
20362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/CodeGen/MachineModuleInfoImpls.h"
21362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/MC/MCContext.h"
22362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/MC/MCExpr.h"
23362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/MC/MCSectionMachO.h"
24362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/MC/MCSectionELF.h"
25eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner#include "llvm/MC/MCSectionCOFF.h"
2630deafc84adf88f643cdc39dc97a37537155347fRafael Espindola#include "llvm/MC/MCStreamer.h"
27362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/MC/MCSymbol.h"
28362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/Target/Mangler.h"
29362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/Target/TargetData.h"
30362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/Target/TargetMachine.h"
31362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/Target/TargetOptions.h"
32362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/Support/Dwarf.h"
33c85dca66e68c9fa6ffa8471c64113b12d8d94fb1Rafael Espindola#include "llvm/Support/ELF.h"
34362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/Support/ErrorHandling.h"
35362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/Support/raw_ostream.h"
36362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/ADT/SmallString.h"
37362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov#include "llvm/ADT/StringExtras.h"
388048ebe91d76f5ee58f5c2c7535151d782af4b29Chris Lattner#include "llvm/ADT/Triple.h"
39362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovusing namespace llvm;
40293d592bf9cff6c3679998dde32f3cfd3cd53bc4Anton Korobeynikovusing namespace dwarf;
41362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
42362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//===----------------------------------------------------------------------===//
43362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//                                  ELF
44362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//===----------------------------------------------------------------------===//
45362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
4630deafc84adf88f643cdc39dc97a37537155347fRafael EspindolaMCSymbol *
477afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael EspindolaTargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV,
487afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola                                                     Mangler *Mang,
497afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola                                                MachineModuleInfo *MMI) const {
5060246a96224c8b790177253bf25433b93b335d2bRafael Espindola  unsigned Encoding = getPersonalityEncoding();
517afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  switch (Encoding & 0x70) {
527afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  default:
537afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola    report_fatal_error("We do not support this DWARF encoding yet!");
547afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  case dwarf::DW_EH_PE_absptr:
557afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola    return  Mang->getSymbol(GV);
567afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  case dwarf::DW_EH_PE_pcrel: {
57fb66f477a1c1cdde2670163bd60e42139a5009d8Rafael Espindola    return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
58fb66f477a1c1cdde2670163bd60e42139a5009d8Rafael Espindola                                          Mang->getSymbol(GV)->getName());
597afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  }
607afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  }
6130deafc84adf88f643cdc39dc97a37537155347fRafael Espindola}
6230deafc84adf88f643cdc39dc97a37537155347fRafael Espindola
6330deafc84adf88f643cdc39dc97a37537155347fRafael Espindolavoid TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
6430deafc84adf88f643cdc39dc97a37537155347fRafael Espindola                                                       const TargetMachine &TM,
657afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola                                                       const MCSymbol *Sym) const {
66fb66f477a1c1cdde2670163bd60e42139a5009d8Rafael Espindola  SmallString<64> NameData("DW.ref.");
67fb66f477a1c1cdde2670163bd60e42139a5009d8Rafael Espindola  NameData += Sym->getName();
68fb66f477a1c1cdde2670163bd60e42139a5009d8Rafael Espindola  MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
69018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
70018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
71fb66f477a1c1cdde2670163bd60e42139a5009d8Rafael Espindola  StringRef Prefix = ".data.";
72fb66f477a1c1cdde2670163bd60e42139a5009d8Rafael Espindola  NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
73018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
74018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  const MCSection *Sec = getContext().getELFSection(NameData,
75018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola                                                    ELF::SHT_PROGBITS,
76018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola                                                    Flags,
77018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola                                                    SectionKind::getDataRel(),
78018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola                                                    0, Label->getName());
79018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  Streamer.SwitchSection(Sec);
80018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  Streamer.EmitValueToAlignment(8);
81018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
82018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  const MCExpr *E = MCConstantExpr::Create(8, getContext());
83018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  Streamer.EmitELFSize(Label, E);
84018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  Streamer.EmitLabel(Label);
85018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola
86018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  unsigned Size = TM.getTargetData()->getPointerSize();
87018e38c6dd8794c6c58be518ddaae99033f6d080Rafael Espindola  Streamer.EmitSymbolValue(Sym, Size);
8830deafc84adf88f643cdc39dc97a37537155347fRafael Espindola}
8930deafc84adf88f643cdc39dc97a37537155347fRafael Espindola
90362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovstatic SectionKind
91362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovgetELFKindForNamedSection(StringRef Name, SectionKind K) {
92e6657980977ed63df8bbd2604c9fefe513ac4126Rafael Espindola  // N.B.: The defaults used in here are no the same ones used in MC.
93e6657980977ed63df8bbd2604c9fefe513ac4126Rafael Espindola  // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
94e6657980977ed63df8bbd2604c9fefe513ac4126Rafael Espindola  // both gas and MC will produce a section with no flags. Given
95e6657980977ed63df8bbd2604c9fefe513ac4126Rafael Espindola  // section(".eh_frame") gcc will produce
96e6657980977ed63df8bbd2604c9fefe513ac4126Rafael Espindola  // .section	.eh_frame,"a",@progbits
97362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Name.empty() || Name[0] != '.') return K;
98362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
99362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // Some lame default implementation based on some magic section names.
100362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Name == ".bss" ||
101362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".bss.") ||
102362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".gnu.linkonce.b.") ||
103362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".llvm.linkonce.b.") ||
104362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name == ".sbss" ||
105362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".sbss.") ||
106362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".gnu.linkonce.sb.") ||
107362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".llvm.linkonce.sb."))
108362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return SectionKind::getBSS();
109362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
110362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Name == ".tdata" ||
111362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".tdata.") ||
112362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".gnu.linkonce.td.") ||
113362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".llvm.linkonce.td."))
114362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return SectionKind::getThreadData();
115362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
116362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Name == ".tbss" ||
117362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".tbss.") ||
118362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".gnu.linkonce.tb.") ||
119362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Name.startswith(".llvm.linkonce.tb."))
120362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return SectionKind::getThreadBSS();
121362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
122362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return K;
123362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
124362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
125362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
126362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovstatic unsigned getELFSectionType(StringRef Name, SectionKind K) {
127362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
128362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Name == ".init_array")
129c85dca66e68c9fa6ffa8471c64113b12d8d94fb1Rafael Espindola    return ELF::SHT_INIT_ARRAY;
130362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
131362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Name == ".fini_array")
132c85dca66e68c9fa6ffa8471c64113b12d8d94fb1Rafael Espindola    return ELF::SHT_FINI_ARRAY;
133362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
134362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Name == ".preinit_array")
135c85dca66e68c9fa6ffa8471c64113b12d8d94fb1Rafael Espindola    return ELF::SHT_PREINIT_ARRAY;
136362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
137362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (K.isBSS() || K.isThreadBSS())
138c85dca66e68c9fa6ffa8471c64113b12d8d94fb1Rafael Espindola    return ELF::SHT_NOBITS;
139362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
140c85dca66e68c9fa6ffa8471c64113b12d8d94fb1Rafael Espindola  return ELF::SHT_PROGBITS;
141362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
142362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
143362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
144362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovstatic unsigned
145362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovgetELFSectionFlags(SectionKind K) {
146362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  unsigned Flags = 0;
147362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
148362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (!K.isMetadata())
1491c13026e8f94bd332c0979baa9c777da99d48736Rafael Espindola    Flags |= ELF::SHF_ALLOC;
150362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
151362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (K.isText())
1521c13026e8f94bd332c0979baa9c777da99d48736Rafael Espindola    Flags |= ELF::SHF_EXECINSTR;
153362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
154d846e3fb5a36c625df1bea3d4ef4e337b7b6e917Rafael Espindola  if (K.isWriteable())
1551c13026e8f94bd332c0979baa9c777da99d48736Rafael Espindola    Flags |= ELF::SHF_WRITE;
156362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
157362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (K.isThreadLocal())
1581c13026e8f94bd332c0979baa9c777da99d48736Rafael Espindola    Flags |= ELF::SHF_TLS;
159362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
160362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // K.isMergeableConst() is left out to honour PR4650
161362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (K.isMergeableCString() || K.isMergeableConst4() ||
162362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      K.isMergeableConst8() || K.isMergeableConst16())
1631c13026e8f94bd332c0979baa9c777da99d48736Rafael Espindola    Flags |= ELF::SHF_MERGE;
164362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
165362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (K.isMergeableCString())
1661c13026e8f94bd332c0979baa9c777da99d48736Rafael Espindola    Flags |= ELF::SHF_STRINGS;
167362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
168362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return Flags;
169362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
170362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
171362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
172362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovconst MCSection *TargetLoweringObjectFileELF::
173362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovgetExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
174362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov                         Mangler *Mang, const TargetMachine &TM) const {
175362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  StringRef SectionName = GV->getSection();
176362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
177362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // Infer section flags from the section name if we can.
178362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  Kind = getELFKindForNamedSection(SectionName, Kind);
179362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
180287df1bc0309962770b6c176f2d143795dd3cc2fChris Lattner  return getContext().getELFSection(SectionName,
181287df1bc0309962770b6c176f2d143795dd3cc2fChris Lattner                                    getELFSectionType(SectionName, Kind),
18234be396a12b00a95a1353c356d64868798ea3098Rafael Espindola                                    getELFSectionFlags(Kind), Kind);
183362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
184362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
18543ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner/// getSectionPrefixForGlobal - Return the section prefix name used by options
18643ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner/// FunctionsSections and DataSections.
18743ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattnerstatic const char *getSectionPrefixForGlobal(SectionKind Kind) {
18843ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  if (Kind.isText())                 return ".text.";
18943ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  if (Kind.isReadOnly())             return ".rodata.";
19043ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner
19143ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  if (Kind.isThreadData())           return ".tdata.";
19243ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  if (Kind.isThreadBSS())            return ".tbss.";
19343ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner
19443ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  if (Kind.isDataNoRel())            return ".data.";
19543ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  if (Kind.isDataRelLocal())         return ".data.rel.local.";
19643ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  if (Kind.isDataRel())              return ".data.rel.";
19743ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
19843ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner
19943ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
20043ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  return ".data.rel.ro.";
20143ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner}
20243ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner
20343ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner
204362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovconst MCSection *TargetLoweringObjectFileELF::
205362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovSelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
206362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov                       Mangler *Mang, const TargetMachine &TM) const {
20743ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  // If we have -ffunction-section or -fdata-section then we should emit the
20843ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  // global value to a uniqued section specifically for it.
20943ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  bool EmitUniquedSection;
21043ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  if (Kind.isText())
21143ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner    EmitUniquedSection = TM.getFunctionSections();
212579d7a3dccaf3c2e91ee81c026c47a0850803820Michael J. Spencer  else
21343ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner    EmitUniquedSection = TM.getDataSections();
214362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
215362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // If this global is linkonce/weak and the target handles this by emitting it
216362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // into a 'uniqued' section name, create and return the section now.
21743ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner  if ((GV->isWeakForLinker() || EmitUniquedSection) &&
21843ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner      !Kind.isCommon() && !Kind.isBSS()) {
21943ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner    const char *Prefix;
2205d618ef7f1ad005fc6912f3e9a08c044b082e47fRafael Espindola    Prefix = getSectionPrefixForGlobal(Kind);
22143ac721e3b35341e7cf59d5982ad702ca8d27433Chris Lattner
2224c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner    SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
2234c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner    MCSymbol *Sym = Mang->getSymbol(GV);
2244c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner    Name.append(Sym->getName().begin(), Sym->getName().end());
2255d618ef7f1ad005fc6912f3e9a08c044b082e47fRafael Espindola    StringRef Group = "";
2265d618ef7f1ad005fc6912f3e9a08c044b082e47fRafael Espindola    unsigned Flags = getELFSectionFlags(Kind);
2275d618ef7f1ad005fc6912f3e9a08c044b082e47fRafael Espindola    if (GV->isWeakForLinker()) {
2285d618ef7f1ad005fc6912f3e9a08c044b082e47fRafael Espindola      Group = Sym->getName();
2295d618ef7f1ad005fc6912f3e9a08c044b082e47fRafael Espindola      Flags |= ELF::SHF_GROUP;
2305d618ef7f1ad005fc6912f3e9a08c044b082e47fRafael Espindola    }
2315d618ef7f1ad005fc6912f3e9a08c044b082e47fRafael Espindola
232287df1bc0309962770b6c176f2d143795dd3cc2fChris Lattner    return getContext().getELFSection(Name.str(),
233287df1bc0309962770b6c176f2d143795dd3cc2fChris Lattner                                      getELFSectionType(Name.str(), Kind),
2345d618ef7f1ad005fc6912f3e9a08c044b082e47fRafael Espindola                                      Flags, Kind, 0, Group);
235362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
236362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
237362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isText()) return TextSection;
238362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
239362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isMergeable1ByteCString() ||
240362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Kind.isMergeable2ByteCString() ||
241362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      Kind.isMergeable4ByteCString()) {
242362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
243362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    // We also need alignment here.
244362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    // FIXME: this is getting the alignment of the character, not the
245362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    // alignment of the global!
246362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    unsigned Align =
247362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
248362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
249362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    const char *SizeSpec = ".rodata.str1.";
250362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    if (Kind.isMergeable2ByteCString())
251362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      SizeSpec = ".rodata.str2.";
252362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    else if (Kind.isMergeable4ByteCString())
253362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      SizeSpec = ".rodata.str4.";
254362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    else
255362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      assert(Kind.isMergeable1ByteCString() && "unknown string width");
256362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
257362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
258362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    std::string Name = SizeSpec + utostr(Align);
259c85dca66e68c9fa6ffa8471c64113b12d8d94fb1Rafael Espindola    return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
2601c13026e8f94bd332c0979baa9c777da99d48736Rafael Espindola                                      ELF::SHF_ALLOC |
2611c13026e8f94bd332c0979baa9c777da99d48736Rafael Espindola                                      ELF::SHF_MERGE |
2621c13026e8f94bd332c0979baa9c777da99d48736Rafael Espindola                                      ELF::SHF_STRINGS,
263287df1bc0309962770b6c176f2d143795dd3cc2fChris Lattner                                      Kind);
264362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
265362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
266362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isMergeableConst()) {
267362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    if (Kind.isMergeableConst4() && MergeableConst4Section)
268362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      return MergeableConst4Section;
269362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    if (Kind.isMergeableConst8() && MergeableConst8Section)
270362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      return MergeableConst8Section;
271362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    if (Kind.isMergeableConst16() && MergeableConst16Section)
272362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      return MergeableConst16Section;
273362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return ReadOnlySection;  // .const
274362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
275362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
276362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isReadOnly())             return ReadOnlySection;
277362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
278362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isThreadData())           return TLSDataSection;
279362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isThreadBSS())            return TLSBSSSection;
280362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
281362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // Note: we claim that common symbols are put in BSSSection, but they are
282362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // really emitted with the magic .comm directive, which creates a symbol table
283362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // entry but not a section.
284362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
285362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
286362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isDataNoRel())            return DataSection;
287362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isDataRelLocal())         return DataRelLocalSection;
288362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isDataRel())              return DataRelSection;
289362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
290362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
291362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
292362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return DataRelROSection;
293362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
294362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
295362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov/// getSectionForConstant - Given a mergeable constant with the
296362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov/// specified size and relocation information, return a section that it
297362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov/// should be placed in.
298362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovconst MCSection *TargetLoweringObjectFileELF::
299362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovgetSectionForConstant(SectionKind Kind) const {
300362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isMergeableConst4() && MergeableConst4Section)
301362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return MergeableConst4Section;
302362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isMergeableConst8() && MergeableConst8Section)
303362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return MergeableConst8Section;
304362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isMergeableConst16() && MergeableConst16Section)
305362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return MergeableConst16Section;
306362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isReadOnly())
307362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return ReadOnlySection;
308362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
309362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
310362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
311362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return DataRelROSection;
312362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
313362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
314362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovconst MCExpr *TargetLoweringObjectFileELF::
3153192d14076dbe5724ce85b9d48644bb3c081f0e5Chris LattnergetExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
3163192d14076dbe5724ce85b9d48644bb3c081f0e5Chris Lattner                               MachineModuleInfo *MMI,
3173192d14076dbe5724ce85b9d48644bb3c081f0e5Chris Lattner                               unsigned Encoding, MCStreamer &Streamer) const {
318362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
319362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Encoding & dwarf::DW_EH_PE_indirect) {
320362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
321362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
322362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    SmallString<128> Name;
323362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    Mang->getNameWithPrefix(Name, GV, true);
324362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    Name += ".DW.stub";
325362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
326362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    // Add information about the stub reference to ELFMMI so that the stub
327362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    // gets emitted by the asmprinter.
3289b97a73dedf736e14b04a3d1a153f10d25b2507bChris Lattner    MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
3294c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner    MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
330cebae36f57456fe6b0e13726acd1e0250654f02dBill Wendling    if (StubSym.getPointer() == 0) {
3314c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner      MCSymbol *Sym = Mang->getSymbol(GV);
3324c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner      StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
333362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    }
334362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
335362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return TargetLoweringObjectFile::
3364788c3e839203dc75ba9fe2026f315095677715cRafael Espindola      getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
337362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
338362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
339362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return TargetLoweringObjectFile::
3403192d14076dbe5724ce85b9d48644bb3c081f0e5Chris Lattner    getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
341362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
342362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
3434a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikovconst MCSection *
3444a99f59aef358fb93eac180e49f6dcef03822046Anton KorobeynikovTargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const {
3454a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov  // The default scheme is .ctor / .dtor, so we have to invert the priority
3464a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov  // numbering.
3474a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov  if (Priority == 65535)
3484a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov    return StaticCtorSection;
3494a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov
3504a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov  std::string Name = std::string(".ctors.") + utostr(65535 - Priority);
3514a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov  return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
3524a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov                                    ELF::SHF_ALLOC |ELF::SHF_WRITE,
3534a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov                                    SectionKind::getDataRel());
3544a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov}
3554a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov
3564a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikovconst MCSection *
3574a99f59aef358fb93eac180e49f6dcef03822046Anton KorobeynikovTargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const {
3584a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov  // The default scheme is .ctor / .dtor, so we have to invert the priority
3594a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov  // numbering.
3604a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov  if (Priority == 65535)
3614a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov    return StaticDtorSection;
3624a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov
3634a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov  std::string Name = std::string(".dtors.") + utostr(65535 - Priority);
3644a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov  return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
3654a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov                                    ELF::SHF_ALLOC |ELF::SHF_WRITE,
3664a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov                                    SectionKind::getDataRel());
3674a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov}
3684a99f59aef358fb93eac180e49f6dcef03822046Anton Korobeynikov
369362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//===----------------------------------------------------------------------===//
370362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//                                 MachO
371362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//===----------------------------------------------------------------------===//
372362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
373362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovconst MCSection *TargetLoweringObjectFileMachO::
374362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovgetExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
375362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov                         Mangler *Mang, const TargetMachine &TM) const {
376362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // Parse the section specifier and create it if valid.
377362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  StringRef Segment, Section;
37865c8bca78854712ab2bf135c2008ed455ef0c9b7Stuart Hastings  unsigned TAA = 0, StubSize = 0;
37965c8bca78854712ab2bf135c2008ed455ef0c9b7Stuart Hastings  bool TAAParsed;
380362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  std::string ErrorCode =
381362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
38265c8bca78854712ab2bf135c2008ed455ef0c9b7Stuart Hastings                                          TAA, TAAParsed, StubSize);
383362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (!ErrorCode.empty()) {
38475361b69f3f327842b9dad69fa7f28ae3b688412Chris Lattner    // If invalid, report the error with report_fatal_error.
385a7b0cb759433c715065440ee2a963a04db7f2b0bBenjamin Kramer    report_fatal_error("Global variable '" + GV->getName() +
386a7b0cb759433c715065440ee2a963a04db7f2b0bBenjamin Kramer                       "' has an invalid section specifier '" +
387a7b0cb759433c715065440ee2a963a04db7f2b0bBenjamin Kramer                       GV->getSection() + "': " + ErrorCode + ".");
388362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
389362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
390362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // Get the section.
391362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  const MCSectionMachO *S =
39222772214de79aa1c5ca38c4fb1da137d8fb30a05Chris Lattner    getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
393362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
3946ad82d81cc3061a000799ad8751f018c1301906bStuart Hastings  // If TAA wasn't set by ParseSectionSpecifier() above,
3956ad82d81cc3061a000799ad8751f018c1301906bStuart Hastings  // use the value returned by getMachOSection() as a default.
39665c8bca78854712ab2bf135c2008ed455ef0c9b7Stuart Hastings  if (!TAAParsed)
3976ad82d81cc3061a000799ad8751f018c1301906bStuart Hastings    TAA = S->getTypeAndAttributes();
3986ad82d81cc3061a000799ad8751f018c1301906bStuart Hastings
399362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // Okay, now that we got the section, verify that the TAA & StubSize agree.
400362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // If the user declared multiple globals with different section flags, we need
401362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // to reject it here.
402362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
40375361b69f3f327842b9dad69fa7f28ae3b688412Chris Lattner    // If invalid, report the error with report_fatal_error.
404a7b0cb759433c715065440ee2a963a04db7f2b0bBenjamin Kramer    report_fatal_error("Global variable '" + GV->getName() +
405a7b0cb759433c715065440ee2a963a04db7f2b0bBenjamin Kramer                       "' section type or attributes does not match previous"
406a7b0cb759433c715065440ee2a963a04db7f2b0bBenjamin Kramer                       " section specifier");
407362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
408362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
409362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return S;
410362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
411362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
412362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovconst MCSection *TargetLoweringObjectFileMachO::
413362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovSelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
4148116ca5134b355b897450f9a537c9c77e1f08723Eric Christopher                       Mangler *Mang, const TargetMachine &TM) const {
415579d7a3dccaf3c2e91ee81c026c47a0850803820Michael J. Spencer
41602b46bc9426925b90137d264216a54aa413335fdEric Christopher  // Handle thread local data.
4178116ca5134b355b897450f9a537c9c77e1f08723Eric Christopher  if (Kind.isThreadBSS()) return TLSBSSSection;
41802b46bc9426925b90137d264216a54aa413335fdEric Christopher  if (Kind.isThreadData()) return TLSDataSection;
419362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
420362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isText())
421362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return GV->isWeakForLinker() ? TextCoalSection : TextSection;
422362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
423362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // If this is weak/linkonce, put this in a coalescable section, either in text
424362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // or data depending on if it is writable.
425362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (GV->isWeakForLinker()) {
426362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    if (Kind.isReadOnly())
427362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      return ConstTextCoalSection;
428362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return DataCoalSection;
429362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
430362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
431362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // FIXME: Alignment check should be handled by section classifier.
43298f15d27cda62ca2f973da0244b6b5e15f18acb7Chris Lattner  if (Kind.isMergeable1ByteCString() &&
43398f15d27cda62ca2f973da0244b6b5e15f18acb7Chris Lattner      TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
43498f15d27cda62ca2f973da0244b6b5e15f18acb7Chris Lattner    return CStringSection;
435579d7a3dccaf3c2e91ee81c026c47a0850803820Michael J. Spencer
43698f15d27cda62ca2f973da0244b6b5e15f18acb7Chris Lattner  // Do not put 16-bit arrays in the UString section if they have an
43798f15d27cda62ca2f973da0244b6b5e15f18acb7Chris Lattner  // externally visible label, this runs into issues with certain linker
43898f15d27cda62ca2f973da0244b6b5e15f18acb7Chris Lattner  // versions.
43998f15d27cda62ca2f973da0244b6b5e15f18acb7Chris Lattner  if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
44098f15d27cda62ca2f973da0244b6b5e15f18acb7Chris Lattner      TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
44198f15d27cda62ca2f973da0244b6b5e15f18acb7Chris Lattner    return UStringSection;
442362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
443362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isMergeableConst()) {
444362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    if (Kind.isMergeableConst4())
445362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      return FourByteConstantSection;
446362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    if (Kind.isMergeableConst8())
447362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      return EightByteConstantSection;
448362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    if (Kind.isMergeableConst16() && SixteenByteConstantSection)
449362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      return SixteenByteConstantSection;
450362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
451362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
452362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // Otherwise, if it is readonly, but not something we can specially optimize,
453362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // just drop it in .const.
454362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isReadOnly())
455362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return ReadOnlySection;
456362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
457362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // If this is marked const, put it into a const section.  But if the dynamic
458362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // linker needs to write to it, put it in the data segment.
459362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isReadOnlyWithRel())
460362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return ConstDataSection;
461362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
462362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // Put zero initialized globals with strong external linkage in the
463362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // DATA, __common section with the .zerofill directive.
464362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isBSSExtern())
465362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return DataCommonSection;
466362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
467362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // Put zero initialized globals with local linkage in __DATA,__bss directive
468362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // with the .zerofill directive (aka .lcomm).
469362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isBSSLocal())
470362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return DataBSSSection;
471579d7a3dccaf3c2e91ee81c026c47a0850803820Michael J. Spencer
472362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // Otherwise, just drop the variable in the normal data section.
473362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return DataSection;
474362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
475362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
476362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovconst MCSection *
477362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovTargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
478362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // If this constant requires a relocation, we have to put it in the data
479362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // segment, not in the text segment.
480362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
481362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return ConstDataSection;
482362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
483362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isMergeableConst4())
484362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return FourByteConstantSection;
485362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isMergeableConst8())
486362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return EightByteConstantSection;
487362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isMergeableConst16() && SixteenByteConstantSection)
488362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return SixteenByteConstantSection;
489362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return ReadOnlySection;  // .const
490362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
491362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
492362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
493362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov/// not to emit the UsedDirective for some symbols in llvm.used.
494362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov// FIXME: REMOVE this (rdar://7071300)
495362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovbool TargetLoweringObjectFileMachO::
496362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovshouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
497362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  /// On Darwin, internally linked data beginning with "L" or "l" does not have
498362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  /// the directive emitted (this occurs in ObjC metadata).
499362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (!GV) return false;
500362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
50107d317711781d8c9268f7d6afcf1ba7eadf1d127Bill Wendling  // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
502362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
503362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    // FIXME: ObjC metadata is currently emitted as internal symbols that have
50407d317711781d8c9268f7d6afcf1ba7eadf1d127Bill Wendling    // \1L and \0l prefixes on them.  Fix them to be Private/LinkerPrivate and
50507d317711781d8c9268f7d6afcf1ba7eadf1d127Bill Wendling    // this horrible hack can go away.
5064c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner    MCSymbol *Sym = Mang->getSymbol(GV);
5074c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner    if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
508362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov      return false;
509362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
510362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
511362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return true;
512362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
513362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
514362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovconst MCExpr *TargetLoweringObjectFileMachO::
5153192d14076dbe5724ce85b9d48644bb3c081f0e5Chris LattnergetExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
5163192d14076dbe5724ce85b9d48644bb3c081f0e5Chris Lattner                               MachineModuleInfo *MMI, unsigned Encoding,
5173192d14076dbe5724ce85b9d48644bb3c081f0e5Chris Lattner                               MCStreamer &Streamer) const {
518362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // The mach-o version of this method defaults to returning a stub reference.
519362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
520293d592bf9cff6c3679998dde32f3cfd3cd53bc4Anton Korobeynikov  if (Encoding & DW_EH_PE_indirect) {
521293d592bf9cff6c3679998dde32f3cfd3cd53bc4Anton Korobeynikov    MachineModuleInfoMachO &MachOMMI =
522293d592bf9cff6c3679998dde32f3cfd3cd53bc4Anton Korobeynikov      MMI->getObjFileInfo<MachineModuleInfoMachO>();
523293d592bf9cff6c3679998dde32f3cfd3cd53bc4Anton Korobeynikov
524362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    SmallString<128> Name;
525362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    Mang->getNameWithPrefix(Name, GV, true);
526362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    Name += "$non_lazy_ptr";
527293d592bf9cff6c3679998dde32f3cfd3cd53bc4Anton Korobeynikov
528293d592bf9cff6c3679998dde32f3cfd3cd53bc4Anton Korobeynikov    // Add information about the stub reference to MachOMMI so that the stub
529293d592bf9cff6c3679998dde32f3cfd3cd53bc4Anton Korobeynikov    // gets emitted by the asmprinter.
5309b97a73dedf736e14b04a3d1a153f10d25b2507bChris Lattner    MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
531671215464c8335e383fa2db90df2eab73ef83b10Bill Wendling    MachineModuleInfoImpl::StubValueTy &StubSym =
532671215464c8335e383fa2db90df2eab73ef83b10Bill Wendling      GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
533671215464c8335e383fa2db90df2eab73ef83b10Bill Wendling                                  MachOMMI.getGVStubEntry(SSym);
534cebae36f57456fe6b0e13726acd1e0250654f02dBill Wendling    if (StubSym.getPointer() == 0) {
5354c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner      MCSymbol *Sym = Mang->getSymbol(GV);
5364c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner      StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
537293d592bf9cff6c3679998dde32f3cfd3cd53bc4Anton Korobeynikov    }
538362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
539362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return TargetLoweringObjectFile::
5404788c3e839203dc75ba9fe2026f315095677715cRafael Espindola      getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
541362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
542362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
543362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return TargetLoweringObjectFile::
5443192d14076dbe5724ce85b9d48644bb3c081f0e5Chris Lattner    getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
545362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
546362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
5477afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael EspindolaMCSymbol *TargetLoweringObjectFileMachO::
54860246a96224c8b790177253bf25433b93b335d2bRafael EspindolagetCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
5497afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola                        MachineModuleInfo *MMI) const {
5507afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  // The mach-o version of this method defaults to returning a stub reference.
5517afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  MachineModuleInfoMachO &MachOMMI =
5527afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola    MMI->getObjFileInfo<MachineModuleInfoMachO>();
5537afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola
5547afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  SmallString<128> Name;
5557afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  Mang->getNameWithPrefix(Name, GV, true);
5567afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  Name += "$non_lazy_ptr";
5577afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola
5587afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  // Add information about the stub reference to MachOMMI so that the stub
5597afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  // gets emitted by the asmprinter.
5607afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
561d7c2494b3cba8dce914a7d16cfca11ffcd223d4eBill Wendling  MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
5627afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  if (StubSym.getPointer() == 0) {
5637afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola    MCSymbol *Sym = Mang->getSymbol(GV);
5647afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola    StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
5657afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  }
5667afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola
5677afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola  return SSym;
5687afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola}
5697afec9cc0ff1654619d30b6f30e2a4d13369c8bfRafael Espindola
570362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//===----------------------------------------------------------------------===//
571362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//                                  COFF
572362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov//===----------------------------------------------------------------------===//
573362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
574eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattnerstatic unsigned
575eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris LattnergetCOFFSectionFlags(SectionKind K) {
576eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner  unsigned Flags = 0;
577eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner
57836335be3b95f856b7eb7e0766e01ba423315e1abAnton Korobeynikov  if (K.isMetadata())
5796e5ce287b0e53c264af0ba37169ad964e19b5bb7Chris Lattner    Flags |=
58094610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_MEM_DISCARDABLE;
581eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner  else if (K.isText())
582eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner    Flags |=
58394610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_MEM_EXECUTE |
5843931b54a5f274183f850b912e0003f9aa1600584Michael J. Spencer      COFF::IMAGE_SCN_MEM_READ |
58594610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_CNT_CODE;
5866e5ce287b0e53c264af0ba37169ad964e19b5bb7Chris Lattner  else if (K.isBSS ())
5876e5ce287b0e53c264af0ba37169ad964e19b5bb7Chris Lattner    Flags |=
58894610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
58994610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_MEM_READ |
59094610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_MEM_WRITE;
591eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner  else if (K.isReadOnly())
592eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner    Flags |=
59394610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
59494610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_MEM_READ;
595eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner  else if (K.isWriteable())
596eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner    Flags |=
59794610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
59894610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_MEM_READ |
59994610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar      COFF::IMAGE_SCN_MEM_WRITE;
600eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner
601eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner  return Flags;
602362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
603362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
604362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovconst MCSection *TargetLoweringObjectFileCOFF::
605362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovgetExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
606362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov                         Mangler *Mang, const TargetMachine &TM) const {
607eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner  return getContext().getCOFFSection(GV->getSection(),
608eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner                                     getCOFFSectionFlags(Kind),
609eb40a0fd98c44ecc6360e7fab33cf9e9911bed4fChris Lattner                                     Kind);
610362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
611362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
612362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovstatic const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
613362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isText())
6143b3b0ebd2ec9cd1de709007f31f40790c9243511NAKAMURA Takumi    return ".text$";
6156e5ce287b0e53c264af0ba37169ad964e19b5bb7Chris Lattner  if (Kind.isBSS ())
6163b3b0ebd2ec9cd1de709007f31f40790c9243511NAKAMURA Takumi    return ".bss$";
617362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isWriteable())
6183b3b0ebd2ec9cd1de709007f31f40790c9243511NAKAMURA Takumi    return ".data$";
6193b3b0ebd2ec9cd1de709007f31f40790c9243511NAKAMURA Takumi  return ".rdata$";
620362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
621362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
622362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
623362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikovconst MCSection *TargetLoweringObjectFileCOFF::
624362dd0bef5437f85586c046bc53287b6fbe9c099Anton KorobeynikovSelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
625362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov                       Mangler *Mang, const TargetMachine &TM) const {
626362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  assert(!Kind.isThreadLocal() && "Doesn't support TLS");
627362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
628362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // If this global is linkonce/weak and the target handles this by emitting it
629362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  // into a 'uniqued' section name, create and return the section now.
630362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (GV->isWeakForLinker()) {
631362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
632362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
6334c6741f7078f3a9f655e67bf56bd4d5d427fc6bcChris Lattner    MCSymbol *Sym = Mang->getSymbol(GV);
6343b3b0ebd2ec9cd1de709007f31f40790c9243511NAKAMURA Takumi    Name.append(Sym->getName().begin() + 1, Sym->getName().end());
6356e5ce287b0e53c264af0ba37169ad964e19b5bb7Chris Lattner
6366e5ce287b0e53c264af0ba37169ad964e19b5bb7Chris Lattner    unsigned Characteristics = getCOFFSectionFlags(Kind);
6376e5ce287b0e53c264af0ba37169ad964e19b5bb7Chris Lattner
63894610588af55ae7d16ba7d72d1e68324631ec249Daniel Dunbar    Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
6396e5ce287b0e53c264af0ba37169ad964e19b5bb7Chris Lattner
6406e5ce287b0e53c264af0ba37169ad964e19b5bb7Chris Lattner    return getContext().getCOFFSection(Name.str(), Characteristics,
641657985eb8b8cbbde2391befafccfafbddf04242aAnton Korobeynikov                          COFF::IMAGE_COMDAT_SELECT_ANY, Kind);
642362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  }
643362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
644362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  if (Kind.isText())
645362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov    return getTextSection();
646362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
647362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov  return getDataSection();
648362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov}
649362dd0bef5437f85586c046bc53287b6fbe9c099Anton Korobeynikov
650