TargetLoweringObjectFileImpl.cpp revision 1875ce4ad95b8dc7aa7a0ab50a9bf93fb84d50bf
134c445b5c64ca70096f190f1abea9b4788ee6414Dmitry V. Levin//===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===//
2166b0bc71c5ec7c4f9825f3ef9b181ffdec27e98Dmitry V. Levin//
3140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova//                     The LLVM Compiler Infrastructure
4140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova//
5140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova// This file is distributed under the University of Illinois Open Source
6140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova// License. See LICENSE.TXT for details.
7140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova//
8140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova//===----------------------------------------------------------------------===//
9140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova//
10140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova// This file implements classes used to handle lowerings specific to common
11140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova// object file formats.
12140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova//
13140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova//===----------------------------------------------------------------------===//
14140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
15140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
16140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/Constants.h"
17140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/DerivedTypes.h"
18140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/Function.h"
19140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/GlobalVariable.h"
20140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/CodeGen/MachineModuleInfoImpls.h"
21140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/MC/MCContext.h"
22140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/MC/MCExpr.h"
23aef5e14ba29976949961084ddaabb703df688dd7Dmitry V. Levin#include "llvm/MC/MCSectionMachO.h"
24140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/MC/MCSectionELF.h"
25140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/MC/MCSectionCOFF.h"
26140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/MC/MCSymbol.h"
27140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/Target/Mangler.h"
28140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/Target/TargetData.h"
29140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/Target/TargetMachine.h"
30140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/Target/TargetOptions.h"
31140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/Support/Dwarf.h"
32140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/Support/ErrorHandling.h"
33140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/Support/raw_ostream.h"
34140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/ADT/SmallString.h"
35140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova#include "llvm/ADT/StringExtras.h"
36140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirovausing namespace llvm;
37140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirovausing namespace dwarf;
38140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
39140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova//===----------------------------------------------------------------------===//
40140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova//                                  ELF
41140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova//===----------------------------------------------------------------------===//
42140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
43140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirovavoid TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
44140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                                             const TargetMachine &TM) {
45140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  TargetLoweringObjectFile::Initialize(Ctx, TM);
46140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
47140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  BSSSection =
48140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".bss", MCSectionELF::SHT_NOBITS,
49140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
50140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getBSS());
51140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
52140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  TextSection =
53140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".text", MCSectionELF::SHT_PROGBITS,
54140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_EXECINSTR |
55140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC,
56140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getText());
57140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
58140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DataSection =
59140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".data", MCSectionELF::SHT_PROGBITS,
60140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
61140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getDataRel());
62140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
63dd039b74efd2b7ecb499a122e05465783716d577Dmitry V. Levin  ReadOnlySection =
64140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
65140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC,
66140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getReadOnly());
67aef5e14ba29976949961084ddaabb703df688dd7Dmitry V. Levin
68140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  TLSDataSection =
69140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
70140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
71140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_WRITE,
72140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getThreadData());
73140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
74140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  TLSBSSSection =
75140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
76140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
77140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_WRITE,
78140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getThreadBSS());
79140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
80140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DataRelSection =
81140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
82140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
83140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getDataRel());
84140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
85140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DataRelLocalSection =
86140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
87140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
88140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getDataRelLocal());
89140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
90140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DataRelROSection =
91140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
92140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
93140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getReadOnlyWithRel());
94140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
95140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DataRelROLocalSection =
96140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
97140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
98140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getReadOnlyWithRelLocal());
99140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
100140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  MergeableConst4Section =
101140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
102140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
103140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMergeableConst4());
104140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
105140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  MergeableConst8Section =
106140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
107140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
108140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMergeableConst8());
109140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
110140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  MergeableConst16Section =
111140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
112140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
113140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMergeableConst16());
114140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
115140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  StaticCtorSection =
116140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
117140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
118140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getDataRel());
119140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
120140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  StaticDtorSection =
121140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
122140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
123140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getDataRel());
124140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
125140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // Exception Handling Sections.
126140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
127140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
128140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // it contains relocatable pointers.  In PIC mode, this is probably a big
129140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // runtime hit for C++ apps.  Either the contents of the LSDA need to be
130140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // adjusted or this should be a data section.
131140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  LSDASection =
132140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
133140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC,
134140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getReadOnly());
135140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  EHFrameSection =
136140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
137140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
138140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getDataRel());
139ffb6c55d7d52b22a526e58815770b09a2994d856Dmitry V. Levin
140140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // Debug Info Sections.
141140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfAbbrevSection =
142140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
143140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
144140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfInfoSection =
14569b7307225874edc9902ad2409bc435158112ffbDmitry V. Levin    getContext().getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
146140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
147140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfLineSection =
148140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
149140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
150140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfFrameSection =
151140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
152140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
153140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfPubNamesSection =
154140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
155140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
156140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfPubTypesSection =
157140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
158140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
159140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfStrSection =
160140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
161140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
162140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfLocSection =
163140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
164140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
165140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfARangesSection =
166140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
167140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
168140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfRangesSection =
169140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
170140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
171140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  DwarfMacroInfoSection =
172140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    getContext().getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
173140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                               SectionKind::getMetadata());
174140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova}
175140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
176140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
177140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirovastatic SectionKind
178140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira KhabirovagetELFKindForNamedSection(StringRef Name, SectionKind K) {
179140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Name.empty() || Name[0] != '.') return K;
180140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
181140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // Some lame default implementation based on some magic section names.
182140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Name == ".bss" ||
183140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".bss.") ||
184140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".gnu.linkonce.b.") ||
185140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".llvm.linkonce.b.") ||
186140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name == ".sbss" ||
187140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".sbss.") ||
188140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".gnu.linkonce.sb.") ||
189140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".llvm.linkonce.sb."))
190140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    return SectionKind::getBSS();
191140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
192140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Name == ".tdata" ||
193140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".tdata.") ||
194140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".gnu.linkonce.td.") ||
195140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".llvm.linkonce.td."))
196140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    return SectionKind::getThreadData();
197140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
198140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Name == ".tbss" ||
199140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".tbss.") ||
200140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".gnu.linkonce.tb.") ||
201140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Name.startswith(".llvm.linkonce.tb."))
202140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    return SectionKind::getThreadBSS();
203140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
204140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  return K;
205140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova}
206140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
207140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
208140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirovastatic unsigned getELFSectionType(StringRef Name, SectionKind K) {
209140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
210140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Name == ".init_array")
211329288b6f9cf8544735c209dd3691137a162d402Dmitry V. Levin    return MCSectionELF::SHT_INIT_ARRAY;
212329288b6f9cf8544735c209dd3691137a162d402Dmitry V. Levin
213140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Name == ".fini_array")
214140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    return MCSectionELF::SHT_FINI_ARRAY;
215140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
216140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Name == ".preinit_array")
217140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    return MCSectionELF::SHT_PREINIT_ARRAY;
218140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
219140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (K.isBSS() || K.isThreadBSS())
220140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    return MCSectionELF::SHT_NOBITS;
221140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
222140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  return MCSectionELF::SHT_PROGBITS;
223211d6ef4b673673fe24060b18ca582b9e68fffbbElvira Khabirova}
224166b0bc71c5ec7c4f9825f3ef9b181ffdec27e98Dmitry V. Levin
225aef5e14ba29976949961084ddaabb703df688dd7Dmitry V. Levin
226140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirovastatic unsigned
227140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira KhabirovagetELFSectionFlags(SectionKind K) {
228140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  unsigned Flags = 0;
229140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
230140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (!K.isMetadata())
231140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    Flags |= MCSectionELF::SHF_ALLOC;
232140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
233140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (K.isText())
234140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    Flags |= MCSectionELF::SHF_EXECINSTR;
235140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
236140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (K.isWriteable())
237140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    Flags |= MCSectionELF::SHF_WRITE;
238140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
239140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (K.isThreadLocal())
240140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    Flags |= MCSectionELF::SHF_TLS;
241140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
242140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // K.isMergeableConst() is left out to honour PR4650
243140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (K.isMergeableCString() || K.isMergeableConst4() ||
244140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      K.isMergeableConst8() || K.isMergeableConst16())
245140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    Flags |= MCSectionELF::SHF_MERGE;
246140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
247140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (K.isMergeableCString())
248140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    Flags |= MCSectionELF::SHF_STRINGS;
249140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
250140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  return Flags;
251140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova}
252140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
253140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
254140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirovaconst MCSection *TargetLoweringObjectFileELF::
255140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira KhabirovagetExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
256140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                         Mangler *Mang, const TargetMachine &TM) const {
257e3a2fe007b8b6f29c4c3c46d540a483406eb43beDmitry V. Levin  StringRef SectionName = GV->getSection();
258140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
259140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // Infer section flags from the section name if we can.
260140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  Kind = getELFKindForNamedSection(SectionName, Kind);
261140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
262140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  return getContext().getELFSection(SectionName,
263140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                                    getELFSectionType(SectionName, Kind),
264140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                                    getELFSectionFlags(Kind), Kind, true);
265140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova}
266140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
267140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirovastatic const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
268140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isText())                 return ".gnu.linkonce.t.";
269140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isReadOnly())             return ".gnu.linkonce.r.";
270140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
271140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isThreadData())           return ".gnu.linkonce.td.";
272140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isThreadBSS())            return ".gnu.linkonce.tb.";
273140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
274140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isDataNoRel())            return ".gnu.linkonce.d.";
275140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isDataRelLocal())         return ".gnu.linkonce.d.rel.local.";
276140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isDataRel())              return ".gnu.linkonce.d.rel.";
277140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
278140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
279140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
280140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  return ".gnu.linkonce.d.rel.ro.";
281140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova}
282166b0bc71c5ec7c4f9825f3ef9b181ffdec27e98Dmitry V. Levin
283140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova/// getSectionPrefixForGlobal - Return the section prefix name used by options
284140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova/// FunctionsSections and DataSections.
285140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirovastatic const char *getSectionPrefixForGlobal(SectionKind Kind) {
286140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isText())                 return ".text.";
287140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isReadOnly())             return ".rodata.";
288140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
289140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isThreadData())           return ".tdata.";
290140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isThreadBSS())            return ".tbss.";
291140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
292140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isDataNoRel())            return ".data.";
293140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isDataRelLocal())         return ".data.rel.local.";
294140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isDataRel())              return ".data.rel.";
295140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
296140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
297140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
298140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  return ".data.rel.ro.";
299140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova}
300140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
301140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
302140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirovaconst MCSection *TargetLoweringObjectFileELF::
303140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira KhabirovaSelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
304140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                       Mangler *Mang, const TargetMachine &TM) const {
305140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // If we have -ffunction-section or -fdata-section then we should emit the
306140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // global value to a uniqued section specifically for it.
307140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  bool EmitUniquedSection;
308140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isText())
309140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    EmitUniquedSection = TM.getFunctionSections();
310140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  else
311140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    EmitUniquedSection = TM.getDataSections();
312140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
313140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // If this global is linkonce/weak and the target handles this by emitting it
314140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  // into a 'uniqued' section name, create and return the section now.
315140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if ((GV->isWeakForLinker() || EmitUniquedSection) &&
316140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      !Kind.isCommon() && !Kind.isBSS()) {
317140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    const char *Prefix;
318140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    if (GV->isWeakForLinker())
319ce93bbdef166ea6fb242ac89b9d28125234d25d5Dmitry V. Levin      Prefix = getSectionPrefixForUniqueGlobal(Kind);
320140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    else {
321140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      assert(EmitUniquedSection);
322140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Prefix = getSectionPrefixForGlobal(Kind);
323140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    }
324140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
325140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
326140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    MCSymbol *Sym = Mang->getSymbol(GV);
327140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    Name.append(Sym->getName().begin(), Sym->getName().end());
328140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    return getContext().getELFSection(Name.str(),
329140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                                      getELFSectionType(Name.str(), Kind),
330140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                                      getELFSectionFlags(Kind), Kind);
331140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  }
332140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
333140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isText()) return TextSection;
334140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
335140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova  if (Kind.isMergeable1ByteCString() ||
336140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Kind.isMergeable2ByteCString() ||
337140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      Kind.isMergeable4ByteCString()) {
338140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
339140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    // We also need alignment here.
340140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    // FIXME: this is getting the alignment of the character, not the
341140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    // alignment of the global!
342140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    unsigned Align =
343140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
344140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
345140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    const char *SizeSpec = ".rodata.str1.";
346140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    if (Kind.isMergeable2ByteCString())
347140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      SizeSpec = ".rodata.str2.";
348140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    else if (Kind.isMergeable4ByteCString())
349140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      SizeSpec = ".rodata.str4.";
350140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    else
351140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova      assert(Kind.isMergeable1ByteCString() && "unknown string width");
352140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
353140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova
354140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    std::string Name = SizeSpec + utostr(Align);
355140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova    return getContext().getELFSection(Name, MCSectionELF::SHT_PROGBITS,
356140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                                      MCSectionELF::SHF_ALLOC |
357140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                                      MCSectionELF::SHF_MERGE |
358140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                                      MCSectionELF::SHF_STRINGS,
359140ecf876686d49085c6eb3a2306e2ea6ea641bcElvira Khabirova                                      Kind);
360401d4bd6015253f4d4eaabe8a97bcef6deb57b76Dmitry V. Levin  }
361401d4bd6015253f4d4eaabe8a97bcef6deb57b76Dmitry V. Levin
362401d4bd6015253f4d4eaabe8a97bcef6deb57b76Dmitry V. Levin  if (Kind.isMergeableConst()) {
363401d4bd6015253f4d4eaabe8a97bcef6deb57b76Dmitry V. Levin    if (Kind.isMergeableConst4() && MergeableConst4Section)
364b396bf63a5fc92a0b6d5a8a384ea548876e1b59eDmitry V. Levin      return MergeableConst4Section;
365b396bf63a5fc92a0b6d5a8a384ea548876e1b59eDmitry V. Levin    if (Kind.isMergeableConst8() && MergeableConst8Section)
366b396bf63a5fc92a0b6d5a8a384ea548876e1b59eDmitry V. Levin      return MergeableConst8Section;
367b396bf63a5fc92a0b6d5a8a384ea548876e1b59eDmitry V. Levin    if (Kind.isMergeableConst16() && MergeableConst16Section)
368b396bf63a5fc92a0b6d5a8a384ea548876e1b59eDmitry V. Levin      return MergeableConst16Section;
369166b0bc71c5ec7c4f9825f3ef9b181ffdec27e98Dmitry V. Levin    return ReadOnlySection;  // .const
37034c445b5c64ca70096f190f1abea9b4788ee6414Dmitry V. Levin  }
371166b0bc71c5ec7c4f9825f3ef9b181ffdec27e98Dmitry V. Levin
372b396bf63a5fc92a0b6d5a8a384ea548876e1b59eDmitry V. Levin  if (Kind.isReadOnly())             return ReadOnlySection;
373b396bf63a5fc92a0b6d5a8a384ea548876e1b59eDmitry V. Levin
374166b0bc71c5ec7c4f9825f3ef9b181ffdec27e98Dmitry V. Levin  if (Kind.isThreadData())           return TLSDataSection;
37534c445b5c64ca70096f190f1abea9b4788ee6414Dmitry V. Levin  if (Kind.isThreadBSS())            return TLSBSSSection;
376
377  // Note: we claim that common symbols are put in BSSSection, but they are
378  // really emitted with the magic .comm directive, which creates a symbol table
379  // entry but not a section.
380  if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
381
382  if (Kind.isDataNoRel())            return DataSection;
383  if (Kind.isDataRelLocal())         return DataRelLocalSection;
384  if (Kind.isDataRel())              return DataRelSection;
385  if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
386
387  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
388  return DataRelROSection;
389}
390
391/// getSectionForConstant - Given a mergeable constant with the
392/// specified size and relocation information, return a section that it
393/// should be placed in.
394const MCSection *TargetLoweringObjectFileELF::
395getSectionForConstant(SectionKind Kind) const {
396  if (Kind.isMergeableConst4() && MergeableConst4Section)
397    return MergeableConst4Section;
398  if (Kind.isMergeableConst8() && MergeableConst8Section)
399    return MergeableConst8Section;
400  if (Kind.isMergeableConst16() && MergeableConst16Section)
401    return MergeableConst16Section;
402  if (Kind.isReadOnly())
403    return ReadOnlySection;
404
405  if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
406  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
407  return DataRelROSection;
408}
409
410const MCExpr *TargetLoweringObjectFileELF::
411getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
412                               MachineModuleInfo *MMI,
413                               unsigned Encoding, MCStreamer &Streamer) const {
414
415  if (Encoding & dwarf::DW_EH_PE_indirect) {
416    MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
417
418    SmallString<128> Name;
419    Mang->getNameWithPrefix(Name, GV, true);
420    Name += ".DW.stub";
421
422    // Add information about the stub reference to ELFMMI so that the stub
423    // gets emitted by the asmprinter.
424    MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
425    MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
426    if (StubSym.getPointer() == 0) {
427      MCSymbol *Sym = Mang->getSymbol(GV);
428      StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
429    }
430
431    return TargetLoweringObjectFile::
432      getExprForDwarfReference(SSym, Mang, MMI,
433                               Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
434  }
435
436  return TargetLoweringObjectFile::
437    getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
438}
439
440//===----------------------------------------------------------------------===//
441//                                 MachO
442//===----------------------------------------------------------------------===//
443
444void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
445                                               const TargetMachine &TM) {
446  // _foo.eh symbols are currently always exported so that the linker knows
447  // about them.  This is not necessary on 10.6 and later, but it
448  // doesn't hurt anything.
449  // FIXME: I need to get this from Triple.
450  IsFunctionEHSymbolGlobal = true;
451  IsFunctionEHFrameSymbolPrivate = false;
452  SupportsWeakOmittedEHFrame = false;
453
454  TargetLoweringObjectFile::Initialize(Ctx, TM);
455
456  TextSection // .text
457    = getContext().getMachOSection("__TEXT", "__text",
458                                   MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
459                                   SectionKind::getText());
460  DataSection // .data
461    = getContext().getMachOSection("__DATA", "__data", 0,
462                                   SectionKind::getDataRel());
463
464  TLSDataSection // .tdata
465    = getContext().getMachOSection("__DATA", "__thread_data",
466                                   MCSectionMachO::S_THREAD_LOCAL_REGULAR,
467                                   SectionKind::getDataRel());
468  TLSBSSSection // .tbss
469    = getContext().getMachOSection("__DATA", "__thread_bss",
470                                   MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
471                                   SectionKind::getThreadBSS());
472
473  // TODO: Verify datarel below.
474  TLSTLVSection // .tlv
475    = getContext().getMachOSection("__DATA", "__thread_vars",
476                                   MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
477                                   SectionKind::getDataRel());
478
479  TLSThreadInitSection
480    = getContext().getMachOSection("__DATA", "__thread_init",
481                          MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
482                          SectionKind::getDataRel());
483
484  CStringSection // .cstring
485    = getContext().getMachOSection("__TEXT", "__cstring",
486                                   MCSectionMachO::S_CSTRING_LITERALS,
487                                   SectionKind::getMergeable1ByteCString());
488  UStringSection
489    = getContext().getMachOSection("__TEXT","__ustring", 0,
490                                   SectionKind::getMergeable2ByteCString());
491  FourByteConstantSection // .literal4
492    = getContext().getMachOSection("__TEXT", "__literal4",
493                                   MCSectionMachO::S_4BYTE_LITERALS,
494                                   SectionKind::getMergeableConst4());
495  EightByteConstantSection // .literal8
496    = getContext().getMachOSection("__TEXT", "__literal8",
497                                   MCSectionMachO::S_8BYTE_LITERALS,
498                                   SectionKind::getMergeableConst8());
499
500  // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
501  // to using it in -static mode.
502  SixteenByteConstantSection = 0;
503  if (TM.getRelocationModel() != Reloc::Static &&
504      TM.getTargetData()->getPointerSize() == 32)
505    SixteenByteConstantSection =   // .literal16
506      getContext().getMachOSection("__TEXT", "__literal16",
507                                   MCSectionMachO::S_16BYTE_LITERALS,
508                                   SectionKind::getMergeableConst16());
509
510  ReadOnlySection  // .const
511    = getContext().getMachOSection("__TEXT", "__const", 0,
512                                   SectionKind::getReadOnly());
513
514  TextCoalSection
515    = getContext().getMachOSection("__TEXT", "__textcoal_nt",
516                                   MCSectionMachO::S_COALESCED |
517                                   MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
518                                   SectionKind::getText());
519  ConstTextCoalSection
520    = getContext().getMachOSection("__TEXT", "__const_coal",
521                                   MCSectionMachO::S_COALESCED,
522                                   SectionKind::getReadOnly());
523  ConstDataSection  // .const_data
524    = getContext().getMachOSection("__DATA", "__const", 0,
525                                   SectionKind::getReadOnlyWithRel());
526  DataCoalSection
527    = getContext().getMachOSection("__DATA","__datacoal_nt",
528                                   MCSectionMachO::S_COALESCED,
529                                   SectionKind::getDataRel());
530  DataCommonSection
531    = getContext().getMachOSection("__DATA","__common",
532                                   MCSectionMachO::S_ZEROFILL,
533                                   SectionKind::getBSS());
534  DataBSSSection
535    = getContext().getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
536                                   SectionKind::getBSS());
537
538
539  LazySymbolPointerSection
540    = getContext().getMachOSection("__DATA", "__la_symbol_ptr",
541                                   MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
542                                   SectionKind::getMetadata());
543  NonLazySymbolPointerSection
544    = getContext().getMachOSection("__DATA", "__nl_symbol_ptr",
545                                   MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
546                                   SectionKind::getMetadata());
547
548  if (TM.getRelocationModel() == Reloc::Static) {
549    StaticCtorSection
550      = getContext().getMachOSection("__TEXT", "__constructor", 0,
551                                     SectionKind::getDataRel());
552    StaticDtorSection
553      = getContext().getMachOSection("__TEXT", "__destructor", 0,
554                                     SectionKind::getDataRel());
555  } else {
556    StaticCtorSection
557      = getContext().getMachOSection("__DATA", "__mod_init_func",
558                                     MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
559                                     SectionKind::getDataRel());
560    StaticDtorSection
561      = getContext().getMachOSection("__DATA", "__mod_term_func",
562                                     MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
563                                     SectionKind::getDataRel());
564  }
565
566  // Exception Handling.
567  LSDASection = getContext().getMachOSection("__TEXT", "__gcc_except_tab", 0,
568                                             SectionKind::getReadOnlyWithRel());
569  EHFrameSection =
570    getContext().getMachOSection("__TEXT", "__eh_frame",
571                                 MCSectionMachO::S_COALESCED |
572                                 MCSectionMachO::S_ATTR_NO_TOC |
573                                 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
574                                 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
575                                 SectionKind::getReadOnly());
576
577  // Debug Information.
578  DwarfAbbrevSection =
579    getContext().getMachOSection("__DWARF", "__debug_abbrev",
580                                 MCSectionMachO::S_ATTR_DEBUG,
581                                 SectionKind::getMetadata());
582  DwarfInfoSection =
583    getContext().getMachOSection("__DWARF", "__debug_info",
584                                 MCSectionMachO::S_ATTR_DEBUG,
585                                 SectionKind::getMetadata());
586  DwarfLineSection =
587    getContext().getMachOSection("__DWARF", "__debug_line",
588                                 MCSectionMachO::S_ATTR_DEBUG,
589                                 SectionKind::getMetadata());
590  DwarfFrameSection =
591    getContext().getMachOSection("__DWARF", "__debug_frame",
592                                 MCSectionMachO::S_ATTR_DEBUG,
593                                 SectionKind::getMetadata());
594  DwarfPubNamesSection =
595    getContext().getMachOSection("__DWARF", "__debug_pubnames",
596                                 MCSectionMachO::S_ATTR_DEBUG,
597                                 SectionKind::getMetadata());
598  DwarfPubTypesSection =
599    getContext().getMachOSection("__DWARF", "__debug_pubtypes",
600                                 MCSectionMachO::S_ATTR_DEBUG,
601                                 SectionKind::getMetadata());
602  DwarfStrSection =
603    getContext().getMachOSection("__DWARF", "__debug_str",
604                                 MCSectionMachO::S_ATTR_DEBUG,
605                                 SectionKind::getMetadata());
606  DwarfLocSection =
607    getContext().getMachOSection("__DWARF", "__debug_loc",
608                                 MCSectionMachO::S_ATTR_DEBUG,
609                                 SectionKind::getMetadata());
610  DwarfARangesSection =
611    getContext().getMachOSection("__DWARF", "__debug_aranges",
612                                 MCSectionMachO::S_ATTR_DEBUG,
613                                 SectionKind::getMetadata());
614  DwarfRangesSection =
615    getContext().getMachOSection("__DWARF", "__debug_ranges",
616                                 MCSectionMachO::S_ATTR_DEBUG,
617                                 SectionKind::getMetadata());
618  DwarfMacroInfoSection =
619    getContext().getMachOSection("__DWARF", "__debug_macinfo",
620                                 MCSectionMachO::S_ATTR_DEBUG,
621                                 SectionKind::getMetadata());
622  DwarfDebugInlineSection =
623    getContext().getMachOSection("__DWARF", "__debug_inlined",
624                                 MCSectionMachO::S_ATTR_DEBUG,
625                                 SectionKind::getMetadata());
626
627  TLSExtraDataSection = TLSTLVSection;
628}
629
630const MCSection *TargetLoweringObjectFileMachO::
631getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
632                         Mangler *Mang, const TargetMachine &TM) const {
633  // Parse the section specifier and create it if valid.
634  StringRef Segment, Section;
635  unsigned TAA, StubSize;
636  std::string ErrorCode =
637    MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
638                                          TAA, StubSize);
639  if (!ErrorCode.empty()) {
640    // If invalid, report the error with report_fatal_error.
641    report_fatal_error("Global variable '" + GV->getNameStr() +
642                      "' has an invalid section specifier '" + GV->getSection()+
643                      "': " + ErrorCode + ".");
644    // Fall back to dropping it into the data section.
645    return DataSection;
646  }
647
648  // Get the section.
649  const MCSectionMachO *S =
650    getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
651
652  // Okay, now that we got the section, verify that the TAA & StubSize agree.
653  // If the user declared multiple globals with different section flags, we need
654  // to reject it here.
655  if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
656    // If invalid, report the error with report_fatal_error.
657    report_fatal_error("Global variable '" + GV->getNameStr() +
658                      "' section type or attributes does not match previous"
659                      " section specifier");
660  }
661
662  return S;
663}
664
665const MCSection *TargetLoweringObjectFileMachO::
666SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
667                       Mangler *Mang, const TargetMachine &TM) const {
668
669  // Handle thread local data.
670  if (Kind.isThreadBSS()) return TLSBSSSection;
671  if (Kind.isThreadData()) return TLSDataSection;
672
673  if (Kind.isText())
674    return GV->isWeakForLinker() ? TextCoalSection : TextSection;
675
676  // If this is weak/linkonce, put this in a coalescable section, either in text
677  // or data depending on if it is writable.
678  if (GV->isWeakForLinker()) {
679    if (Kind.isReadOnly())
680      return ConstTextCoalSection;
681    return DataCoalSection;
682  }
683
684  // FIXME: Alignment check should be handled by section classifier.
685  if (Kind.isMergeable1ByteCString() &&
686      TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
687    return CStringSection;
688
689  // Do not put 16-bit arrays in the UString section if they have an
690  // externally visible label, this runs into issues with certain linker
691  // versions.
692  if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
693      TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
694    return UStringSection;
695
696  if (Kind.isMergeableConst()) {
697    if (Kind.isMergeableConst4())
698      return FourByteConstantSection;
699    if (Kind.isMergeableConst8())
700      return EightByteConstantSection;
701    if (Kind.isMergeableConst16() && SixteenByteConstantSection)
702      return SixteenByteConstantSection;
703  }
704
705  // Otherwise, if it is readonly, but not something we can specially optimize,
706  // just drop it in .const.
707  if (Kind.isReadOnly())
708    return ReadOnlySection;
709
710  // If this is marked const, put it into a const section.  But if the dynamic
711  // linker needs to write to it, put it in the data segment.
712  if (Kind.isReadOnlyWithRel())
713    return ConstDataSection;
714
715  // Put zero initialized globals with strong external linkage in the
716  // DATA, __common section with the .zerofill directive.
717  if (Kind.isBSSExtern())
718    return DataCommonSection;
719
720  // Put zero initialized globals with local linkage in __DATA,__bss directive
721  // with the .zerofill directive (aka .lcomm).
722  if (Kind.isBSSLocal())
723    return DataBSSSection;
724
725  // Otherwise, just drop the variable in the normal data section.
726  return DataSection;
727}
728
729const MCSection *
730TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
731  // If this constant requires a relocation, we have to put it in the data
732  // segment, not in the text segment.
733  if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
734    return ConstDataSection;
735
736  if (Kind.isMergeableConst4())
737    return FourByteConstantSection;
738  if (Kind.isMergeableConst8())
739    return EightByteConstantSection;
740  if (Kind.isMergeableConst16() && SixteenByteConstantSection)
741    return SixteenByteConstantSection;
742  return ReadOnlySection;  // .const
743}
744
745/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
746/// not to emit the UsedDirective for some symbols in llvm.used.
747// FIXME: REMOVE this (rdar://7071300)
748bool TargetLoweringObjectFileMachO::
749shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
750  /// On Darwin, internally linked data beginning with "L" or "l" does not have
751  /// the directive emitted (this occurs in ObjC metadata).
752  if (!GV) return false;
753
754  // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
755  if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
756    // FIXME: ObjC metadata is currently emitted as internal symbols that have
757    // \1L and \0l prefixes on them.  Fix them to be Private/LinkerPrivate and
758    // this horrible hack can go away.
759    MCSymbol *Sym = Mang->getSymbol(GV);
760    if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l')
761      return false;
762  }
763
764  return true;
765}
766
767const MCExpr *TargetLoweringObjectFileMachO::
768getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
769                               MachineModuleInfo *MMI, unsigned Encoding,
770                               MCStreamer &Streamer) const {
771  // The mach-o version of this method defaults to returning a stub reference.
772
773  if (Encoding & DW_EH_PE_indirect) {
774    MachineModuleInfoMachO &MachOMMI =
775      MMI->getObjFileInfo<MachineModuleInfoMachO>();
776
777    SmallString<128> Name;
778    Mang->getNameWithPrefix(Name, GV, true);
779    Name += "$non_lazy_ptr";
780
781    // Add information about the stub reference to MachOMMI so that the stub
782    // gets emitted by the asmprinter.
783    MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str());
784    MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
785    if (StubSym.getPointer() == 0) {
786      MCSymbol *Sym = Mang->getSymbol(GV);
787      StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
788    }
789
790    return TargetLoweringObjectFile::
791      getExprForDwarfReference(SSym, Mang, MMI,
792                               Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
793  }
794
795  return TargetLoweringObjectFile::
796    getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer);
797}
798
799unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const {
800  return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
801}
802
803unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const {
804  return DW_EH_PE_pcrel;
805}
806
807unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const {
808  return DW_EH_PE_pcrel;
809}
810
811unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const {
812  return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
813}
814
815//===----------------------------------------------------------------------===//
816//                                  COFF
817//===----------------------------------------------------------------------===//
818
819void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
820                                              const TargetMachine &TM) {
821  TargetLoweringObjectFile::Initialize(Ctx, TM);
822  TextSection =
823    getContext().getCOFFSection(".text",
824                                COFF::IMAGE_SCN_CNT_CODE |
825                                COFF::IMAGE_SCN_MEM_EXECUTE |
826                                COFF::IMAGE_SCN_MEM_READ,
827                                SectionKind::getText());
828  DataSection =
829    getContext().getCOFFSection(".data",
830                                COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
831                                COFF::IMAGE_SCN_MEM_READ |
832                                COFF::IMAGE_SCN_MEM_WRITE,
833                                SectionKind::getDataRel());
834  ReadOnlySection =
835    getContext().getCOFFSection(".rdata",
836                                COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
837                                COFF::IMAGE_SCN_MEM_READ,
838                                SectionKind::getReadOnly());
839  StaticCtorSection =
840    getContext().getCOFFSection(".ctors",
841                                COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
842                                COFF::IMAGE_SCN_MEM_READ |
843                                COFF::IMAGE_SCN_MEM_WRITE,
844                                SectionKind::getDataRel());
845  StaticDtorSection =
846    getContext().getCOFFSection(".dtors",
847                                COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
848                                COFF::IMAGE_SCN_MEM_READ |
849                                COFF::IMAGE_SCN_MEM_WRITE,
850                                SectionKind::getDataRel());
851
852  // FIXME: We're emitting LSDA info into a readonly section on COFF, even
853  // though it contains relocatable pointers.  In PIC mode, this is probably a
854  // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
855  // adjusted or this should be a data section.
856  LSDASection =
857    getContext().getCOFFSection(".gcc_except_table",
858                                COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
859                                COFF::IMAGE_SCN_MEM_READ,
860                                SectionKind::getReadOnly());
861  EHFrameSection =
862    getContext().getCOFFSection(".eh_frame",
863                                COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
864                                COFF::IMAGE_SCN_MEM_READ |
865                                COFF::IMAGE_SCN_MEM_WRITE,
866                                SectionKind::getDataRel());
867
868  // Debug info.
869  DwarfAbbrevSection =
870    getContext().getCOFFSection(".debug_abbrev",
871                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
872                                COFF::IMAGE_SCN_MEM_READ,
873                                SectionKind::getMetadata());
874  DwarfInfoSection =
875    getContext().getCOFFSection(".debug_info",
876                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
877                                COFF::IMAGE_SCN_MEM_READ,
878                                SectionKind::getMetadata());
879  DwarfLineSection =
880    getContext().getCOFFSection(".debug_line",
881                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
882                                COFF::IMAGE_SCN_MEM_READ,
883                                SectionKind::getMetadata());
884  DwarfFrameSection =
885    getContext().getCOFFSection(".debug_frame",
886                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
887                                COFF::IMAGE_SCN_MEM_READ,
888                                SectionKind::getMetadata());
889  DwarfPubNamesSection =
890    getContext().getCOFFSection(".debug_pubnames",
891                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
892                                COFF::IMAGE_SCN_MEM_READ,
893                                SectionKind::getMetadata());
894  DwarfPubTypesSection =
895    getContext().getCOFFSection(".debug_pubtypes",
896                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
897                                COFF::IMAGE_SCN_MEM_READ,
898                                SectionKind::getMetadata());
899  DwarfStrSection =
900    getContext().getCOFFSection(".debug_str",
901                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
902                                COFF::IMAGE_SCN_MEM_READ,
903                                SectionKind::getMetadata());
904  DwarfLocSection =
905    getContext().getCOFFSection(".debug_loc",
906                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
907                                COFF::IMAGE_SCN_MEM_READ,
908                                SectionKind::getMetadata());
909  DwarfARangesSection =
910    getContext().getCOFFSection(".debug_aranges",
911                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
912                                COFF::IMAGE_SCN_MEM_READ,
913                                SectionKind::getMetadata());
914  DwarfRangesSection =
915    getContext().getCOFFSection(".debug_ranges",
916                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
917                                COFF::IMAGE_SCN_MEM_READ,
918                                SectionKind::getMetadata());
919  DwarfMacroInfoSection =
920    getContext().getCOFFSection(".debug_macinfo",
921                                COFF::IMAGE_SCN_MEM_DISCARDABLE |
922                                COFF::IMAGE_SCN_MEM_READ,
923                                SectionKind::getMetadata());
924
925  DrectveSection =
926    getContext().getCOFFSection(".drectve",
927                                COFF::IMAGE_SCN_LNK_INFO,
928                                SectionKind::getMetadata());
929}
930
931static unsigned
932getCOFFSectionFlags(SectionKind K) {
933  unsigned Flags = 0;
934
935  if (K.isMetadata())
936    Flags |=
937      COFF::IMAGE_SCN_MEM_DISCARDABLE;
938  else if (K.isText())
939    Flags |=
940      COFF::IMAGE_SCN_MEM_EXECUTE |
941      COFF::IMAGE_SCN_CNT_CODE;
942  else if (K.isBSS ())
943    Flags |=
944      COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
945      COFF::IMAGE_SCN_MEM_READ |
946      COFF::IMAGE_SCN_MEM_WRITE;
947  else if (K.isReadOnly())
948    Flags |=
949      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
950      COFF::IMAGE_SCN_MEM_READ;
951  else if (K.isWriteable())
952    Flags |=
953      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
954      COFF::IMAGE_SCN_MEM_READ |
955      COFF::IMAGE_SCN_MEM_WRITE;
956
957  return Flags;
958}
959
960const MCSection *TargetLoweringObjectFileCOFF::
961getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
962                         Mangler *Mang, const TargetMachine &TM) const {
963  return getContext().getCOFFSection(GV->getSection(),
964                                     getCOFFSectionFlags(Kind),
965                                     Kind);
966}
967
968static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
969  if (Kind.isText())
970    return ".text$linkonce";
971  if (Kind.isBSS ())
972    return ".bss$linkonce";
973  if (Kind.isWriteable())
974    return ".data$linkonce";
975  return ".rdata$linkonce";
976}
977
978
979const MCSection *TargetLoweringObjectFileCOFF::
980SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
981                       Mangler *Mang, const TargetMachine &TM) const {
982  assert(!Kind.isThreadLocal() && "Doesn't support TLS");
983
984  // If this global is linkonce/weak and the target handles this by emitting it
985  // into a 'uniqued' section name, create and return the section now.
986  if (GV->isWeakForLinker()) {
987    const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
988    SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
989    MCSymbol *Sym = Mang->getSymbol(GV);
990    Name.append(Sym->getName().begin(), Sym->getName().end());
991
992    unsigned Characteristics = getCOFFSectionFlags(Kind);
993
994    Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
995
996    return getContext().getCOFFSection(Name.str(), Characteristics,
997                          COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH, Kind);
998  }
999
1000  if (Kind.isText())
1001    return getTextSection();
1002
1003  return getDataSection();
1004}
1005
1006