]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/lib/MC/MCObjectFileInfo.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / lib / MC / MCObjectFileInfo.cpp
1 //===-- MObjectFileInfo.cpp - Object File Information ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCObjectFileInfo.h"
11 #include "llvm/ADT/Triple.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCSection.h"
14 #include "llvm/MC/MCSectionCOFF.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/MC/MCSectionMachO.h"
17 using namespace llvm;
18
19 void MCObjectFileInfo::InitMachOMCObjectFileInfo(Triple T) {
20   // MachO
21   IsFunctionEHFrameSymbolPrivate = false;
22   SupportsWeakOmittedEHFrame = false;
23
24   PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel
25     | dwarf::DW_EH_PE_sdata4;
26   LSDAEncoding = FDEEncoding = FDECFIEncoding = dwarf::DW_EH_PE_pcrel;
27   TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
28     dwarf::DW_EH_PE_sdata4;
29
30   // .comm doesn't support alignment before Leopard.
31   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
32     CommDirectiveSupportsAlignment = false;
33
34   TextSection // .text
35     = Ctx->getMachOSection("__TEXT", "__text",
36                            MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
37                            SectionKind::getText());
38   DataSection // .data
39     = Ctx->getMachOSection("__DATA", "__data", 0,
40                            SectionKind::getDataRel());
41
42   // BSSSection might not be expected initialized on msvc.
43   BSSSection = 0;
44
45   TLSDataSection // .tdata
46     = Ctx->getMachOSection("__DATA", "__thread_data",
47                            MCSectionMachO::S_THREAD_LOCAL_REGULAR,
48                            SectionKind::getDataRel());
49   TLSBSSSection // .tbss
50     = Ctx->getMachOSection("__DATA", "__thread_bss",
51                            MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
52                            SectionKind::getThreadBSS());
53
54   // TODO: Verify datarel below.
55   TLSTLVSection // .tlv
56     = Ctx->getMachOSection("__DATA", "__thread_vars",
57                            MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
58                            SectionKind::getDataRel());
59
60   TLSThreadInitSection
61     = Ctx->getMachOSection("__DATA", "__thread_init",
62                           MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
63                           SectionKind::getDataRel());
64
65   CStringSection // .cstring
66     = Ctx->getMachOSection("__TEXT", "__cstring",
67                            MCSectionMachO::S_CSTRING_LITERALS,
68                            SectionKind::getMergeable1ByteCString());
69   UStringSection
70     = Ctx->getMachOSection("__TEXT","__ustring", 0,
71                            SectionKind::getMergeable2ByteCString());
72   FourByteConstantSection // .literal4
73     = Ctx->getMachOSection("__TEXT", "__literal4",
74                            MCSectionMachO::S_4BYTE_LITERALS,
75                            SectionKind::getMergeableConst4());
76   EightByteConstantSection // .literal8
77     = Ctx->getMachOSection("__TEXT", "__literal8",
78                            MCSectionMachO::S_8BYTE_LITERALS,
79                            SectionKind::getMergeableConst8());
80
81   // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
82   // to using it in -static mode.
83   SixteenByteConstantSection = 0;
84   if (RelocM != Reloc::Static &&
85       T.getArch() != Triple::x86_64 && T.getArch() != Triple::ppc64 &&
86       T.getArch() != Triple::ppc64le)
87     SixteenByteConstantSection =   // .literal16
88       Ctx->getMachOSection("__TEXT", "__literal16",
89                            MCSectionMachO::S_16BYTE_LITERALS,
90                            SectionKind::getMergeableConst16());
91
92   ReadOnlySection  // .const
93     = Ctx->getMachOSection("__TEXT", "__const", 0,
94                            SectionKind::getReadOnly());
95
96   TextCoalSection
97     = Ctx->getMachOSection("__TEXT", "__textcoal_nt",
98                            MCSectionMachO::S_COALESCED |
99                            MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
100                            SectionKind::getText());
101   ConstTextCoalSection
102     = Ctx->getMachOSection("__TEXT", "__const_coal",
103                            MCSectionMachO::S_COALESCED,
104                            SectionKind::getReadOnly());
105   ConstDataSection  // .const_data
106     = Ctx->getMachOSection("__DATA", "__const", 0,
107                            SectionKind::getReadOnlyWithRel());
108   DataCoalSection
109     = Ctx->getMachOSection("__DATA","__datacoal_nt",
110                            MCSectionMachO::S_COALESCED,
111                            SectionKind::getDataRel());
112   DataCommonSection
113     = Ctx->getMachOSection("__DATA","__common",
114                            MCSectionMachO::S_ZEROFILL,
115                            SectionKind::getBSS());
116   DataBSSSection
117     = Ctx->getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
118                            SectionKind::getBSS());
119
120
121   LazySymbolPointerSection
122     = Ctx->getMachOSection("__DATA", "__la_symbol_ptr",
123                            MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
124                            SectionKind::getMetadata());
125   NonLazySymbolPointerSection
126     = Ctx->getMachOSection("__DATA", "__nl_symbol_ptr",
127                            MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
128                            SectionKind::getMetadata());
129
130   if (RelocM == Reloc::Static) {
131     StaticCtorSection
132       = Ctx->getMachOSection("__TEXT", "__constructor", 0,
133                              SectionKind::getDataRel());
134     StaticDtorSection
135       = Ctx->getMachOSection("__TEXT", "__destructor", 0,
136                              SectionKind::getDataRel());
137   } else {
138     StaticCtorSection
139       = Ctx->getMachOSection("__DATA", "__mod_init_func",
140                              MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
141                              SectionKind::getDataRel());
142     StaticDtorSection
143       = Ctx->getMachOSection("__DATA", "__mod_term_func",
144                              MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
145                              SectionKind::getDataRel());
146   }
147
148   // Exception Handling.
149   LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0,
150                                      SectionKind::getReadOnlyWithRel());
151
152   if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6)) {
153     CompactUnwindSection =
154       Ctx->getMachOSection("__LD", "__compact_unwind",
155                            MCSectionMachO::S_ATTR_DEBUG,
156                            SectionKind::getReadOnly());
157
158     if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86)
159       CompactUnwindDwarfEHFrameOnly = 0x04000000;
160   }
161
162   // Debug Information.
163   DwarfAccelNamesSection =
164     Ctx->getMachOSection("__DWARF", "__apple_names",
165                          MCSectionMachO::S_ATTR_DEBUG,
166                          SectionKind::getMetadata());
167   DwarfAccelObjCSection =
168     Ctx->getMachOSection("__DWARF", "__apple_objc",
169                          MCSectionMachO::S_ATTR_DEBUG,
170                          SectionKind::getMetadata());
171   // 16 character section limit...
172   DwarfAccelNamespaceSection =
173     Ctx->getMachOSection("__DWARF", "__apple_namespac",
174                          MCSectionMachO::S_ATTR_DEBUG,
175                          SectionKind::getMetadata());
176   DwarfAccelTypesSection =
177     Ctx->getMachOSection("__DWARF", "__apple_types",
178                          MCSectionMachO::S_ATTR_DEBUG,
179                          SectionKind::getMetadata());
180
181   DwarfAbbrevSection =
182     Ctx->getMachOSection("__DWARF", "__debug_abbrev",
183                          MCSectionMachO::S_ATTR_DEBUG,
184                          SectionKind::getMetadata());
185   DwarfInfoSection =
186     Ctx->getMachOSection("__DWARF", "__debug_info",
187                          MCSectionMachO::S_ATTR_DEBUG,
188                          SectionKind::getMetadata());
189   DwarfLineSection =
190     Ctx->getMachOSection("__DWARF", "__debug_line",
191                          MCSectionMachO::S_ATTR_DEBUG,
192                          SectionKind::getMetadata());
193   DwarfFrameSection =
194     Ctx->getMachOSection("__DWARF", "__debug_frame",
195                          MCSectionMachO::S_ATTR_DEBUG,
196                          SectionKind::getMetadata());
197   DwarfPubNamesSection =
198     Ctx->getMachOSection("__DWARF", "__debug_pubnames",
199                          MCSectionMachO::S_ATTR_DEBUG,
200                          SectionKind::getMetadata());
201   DwarfPubTypesSection =
202     Ctx->getMachOSection("__DWARF", "__debug_pubtypes",
203                          MCSectionMachO::S_ATTR_DEBUG,
204                          SectionKind::getMetadata());
205   DwarfGnuPubNamesSection =
206     Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn",
207                          MCSectionMachO::S_ATTR_DEBUG,
208                          SectionKind::getMetadata());
209   DwarfGnuPubTypesSection =
210     Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt",
211                          MCSectionMachO::S_ATTR_DEBUG,
212                          SectionKind::getMetadata());
213   DwarfStrSection =
214     Ctx->getMachOSection("__DWARF", "__debug_str",
215                          MCSectionMachO::S_ATTR_DEBUG,
216                          SectionKind::getMetadata());
217   DwarfLocSection =
218     Ctx->getMachOSection("__DWARF", "__debug_loc",
219                          MCSectionMachO::S_ATTR_DEBUG,
220                          SectionKind::getMetadata());
221   DwarfARangesSection =
222     Ctx->getMachOSection("__DWARF", "__debug_aranges",
223                          MCSectionMachO::S_ATTR_DEBUG,
224                          SectionKind::getMetadata());
225   DwarfRangesSection =
226     Ctx->getMachOSection("__DWARF", "__debug_ranges",
227                          MCSectionMachO::S_ATTR_DEBUG,
228                          SectionKind::getMetadata());
229   DwarfMacroInfoSection =
230     Ctx->getMachOSection("__DWARF", "__debug_macinfo",
231                          MCSectionMachO::S_ATTR_DEBUG,
232                          SectionKind::getMetadata());
233   DwarfDebugInlineSection =
234     Ctx->getMachOSection("__DWARF", "__debug_inlined",
235                          MCSectionMachO::S_ATTR_DEBUG,
236                          SectionKind::getMetadata());
237   StackMapSection =
238     Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps", 0,
239                          SectionKind::getMetadata());
240
241   TLSExtraDataSection = TLSTLVSection;
242 }
243
244 void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) {
245   if (T.getArch() == Triple::mips ||
246       T.getArch() == Triple::mipsel)
247     FDECFIEncoding = dwarf::DW_EH_PE_sdata4;
248   else if (T.getArch() == Triple::mips64 ||
249            T.getArch() == Triple::mips64el)
250     FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
251   else
252     FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
253
254   if (T.getArch() == Triple::x86) {
255     PersonalityEncoding = (RelocM == Reloc::PIC_)
256      ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
257      : dwarf::DW_EH_PE_absptr;
258     LSDAEncoding = (RelocM == Reloc::PIC_)
259       ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
260       : dwarf::DW_EH_PE_absptr;
261     FDEEncoding = (RelocM == Reloc::PIC_)
262       ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
263       : dwarf::DW_EH_PE_absptr;
264     TTypeEncoding = (RelocM == Reloc::PIC_)
265      ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
266      : dwarf::DW_EH_PE_absptr;
267   } else if (T.getArch() == Triple::x86_64) {
268     if (RelocM == Reloc::PIC_) {
269       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
270         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
271          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
272       LSDAEncoding = dwarf::DW_EH_PE_pcrel |
273         (CMModel == CodeModel::Small
274          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
275       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
276       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
277         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
278          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
279     } else {
280       PersonalityEncoding =
281         (CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
282         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
283       LSDAEncoding = (CMModel == CodeModel::Small)
284         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
285       FDEEncoding = dwarf::DW_EH_PE_udata4;
286       TTypeEncoding = (CMModel == CodeModel::Small)
287         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
288     }
289   }  else if (T.getArch() ==  Triple::aarch64) {
290     // The small model guarantees static code/data size < 4GB, but not where it
291     // will be in memory. Most of these could end up >2GB away so even a signed
292     // pc-relative 32-bit address is insufficient, theoretically.
293     if (RelocM == Reloc::PIC_) {
294       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
295         dwarf::DW_EH_PE_sdata8;
296       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
297       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
298       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
299         dwarf::DW_EH_PE_sdata8;
300     } else {
301       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
302       LSDAEncoding = dwarf::DW_EH_PE_absptr;
303       FDEEncoding = dwarf::DW_EH_PE_udata4;
304       TTypeEncoding = dwarf::DW_EH_PE_absptr;
305     }
306   } else if (T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le) {
307     PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
308       dwarf::DW_EH_PE_udata8;
309     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
310     FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
311     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
312       dwarf::DW_EH_PE_udata8;
313   } else if (T.getArch() == Triple::sparc) {
314     if (RelocM == Reloc::PIC_) {
315       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
316       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
317         dwarf::DW_EH_PE_sdata4;
318       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
319       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
320         dwarf::DW_EH_PE_sdata4;
321     } else {
322       LSDAEncoding = dwarf::DW_EH_PE_absptr;
323       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
324       FDEEncoding = dwarf::DW_EH_PE_udata4;
325       TTypeEncoding = dwarf::DW_EH_PE_absptr;
326     }
327   } else if (T.getArch() == Triple::sparcv9) {
328     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
329     if (RelocM == Reloc::PIC_) {
330       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
331         dwarf::DW_EH_PE_sdata4;
332       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
333       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
334         dwarf::DW_EH_PE_sdata4;
335     } else {
336       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
337       FDEEncoding = dwarf::DW_EH_PE_udata4;
338       TTypeEncoding = dwarf::DW_EH_PE_absptr;
339     }
340   } else if (T.getArch() == Triple::systemz) {
341     // All currently-defined code models guarantee that 4-byte PC-relative
342     // values will be in range.
343     if (RelocM == Reloc::PIC_) {
344       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
345         dwarf::DW_EH_PE_sdata4;
346       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
347       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
348       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
349         dwarf::DW_EH_PE_sdata4;
350     } else {
351       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
352       LSDAEncoding = dwarf::DW_EH_PE_absptr;
353       FDEEncoding = dwarf::DW_EH_PE_absptr;
354       TTypeEncoding = dwarf::DW_EH_PE_absptr;
355     }
356   }
357
358   // Solaris requires different flags for .eh_frame to seemingly every other
359   // platform.
360   EHSectionType = ELF::SHT_PROGBITS;
361   EHSectionFlags = ELF::SHF_ALLOC;
362   if (T.getOS() == Triple::Solaris) {
363     if (T.getArch() == Triple::x86_64)
364       EHSectionType = ELF::SHT_X86_64_UNWIND;
365     else
366       EHSectionFlags |= ELF::SHF_WRITE;
367   }
368
369
370   // ELF
371   BSSSection =
372     Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
373                        ELF::SHF_WRITE | ELF::SHF_ALLOC,
374                        SectionKind::getBSS());
375
376   TextSection =
377     Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
378                        ELF::SHF_EXECINSTR |
379                        ELF::SHF_ALLOC,
380                        SectionKind::getText());
381
382   DataSection =
383     Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
384                        ELF::SHF_WRITE |ELF::SHF_ALLOC,
385                        SectionKind::getDataRel());
386
387   ReadOnlySection =
388     Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS,
389                        ELF::SHF_ALLOC,
390                        SectionKind::getReadOnly());
391
392   TLSDataSection =
393     Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
394                        ELF::SHF_ALLOC | ELF::SHF_TLS |
395                        ELF::SHF_WRITE,
396                        SectionKind::getThreadData());
397
398   TLSBSSSection =
399     Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
400                        ELF::SHF_ALLOC | ELF::SHF_TLS |
401                        ELF::SHF_WRITE,
402                        SectionKind::getThreadBSS());
403
404   DataRelSection =
405     Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS,
406                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
407                        SectionKind::getDataRel());
408
409   DataRelLocalSection =
410     Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
411                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
412                        SectionKind::getDataRelLocal());
413
414   DataRelROSection =
415     Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
416                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
417                        SectionKind::getReadOnlyWithRel());
418
419   DataRelROLocalSection =
420     Ctx->getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
421                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
422                        SectionKind::getReadOnlyWithRelLocal());
423
424   MergeableConst4Section =
425     Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
426                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
427                        SectionKind::getMergeableConst4());
428
429   MergeableConst8Section =
430     Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
431                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
432                        SectionKind::getMergeableConst8());
433
434   MergeableConst16Section =
435     Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
436                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
437                        SectionKind::getMergeableConst16());
438
439   StaticCtorSection =
440     Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS,
441                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
442                        SectionKind::getDataRel());
443
444   StaticDtorSection =
445     Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS,
446                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
447                        SectionKind::getDataRel());
448
449   // Exception Handling Sections.
450
451   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
452   // it contains relocatable pointers.  In PIC mode, this is probably a big
453   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
454   // adjusted or this should be a data section.
455   LSDASection =
456     Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
457                        ELF::SHF_ALLOC,
458                        SectionKind::getReadOnly());
459
460   // Debug Info Sections.
461   DwarfAbbrevSection =
462     Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
463                        SectionKind::getMetadata());
464   DwarfInfoSection =
465     Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
466                        SectionKind::getMetadata());
467   DwarfLineSection =
468     Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
469                        SectionKind::getMetadata());
470   DwarfFrameSection =
471     Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
472                        SectionKind::getMetadata());
473   DwarfPubNamesSection =
474     Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
475                        SectionKind::getMetadata());
476   DwarfPubTypesSection =
477     Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
478                        SectionKind::getMetadata());
479   DwarfGnuPubNamesSection =
480     Ctx->getELFSection(".debug_gnu_pubnames", ELF::SHT_PROGBITS, 0,
481                        SectionKind::getMetadata());
482   DwarfGnuPubTypesSection =
483     Ctx->getELFSection(".debug_gnu_pubtypes", ELF::SHT_PROGBITS, 0,
484                        SectionKind::getMetadata());
485   DwarfStrSection =
486     Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS,
487                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
488                        SectionKind::getMergeable1ByteCString());
489   DwarfLocSection =
490     Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
491                        SectionKind::getMetadata());
492   DwarfARangesSection =
493     Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
494                        SectionKind::getMetadata());
495   DwarfRangesSection =
496     Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
497                        SectionKind::getMetadata());
498   DwarfMacroInfoSection =
499     Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
500                        SectionKind::getMetadata());
501
502   // DWARF5 Experimental Debug Info
503
504   // Accelerator Tables
505   DwarfAccelNamesSection =
506     Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0,
507                        SectionKind::getMetadata());
508   DwarfAccelObjCSection =
509     Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0,
510                        SectionKind::getMetadata());
511   DwarfAccelNamespaceSection =
512     Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0,
513                        SectionKind::getMetadata());
514   DwarfAccelTypesSection =
515     Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0,
516                        SectionKind::getMetadata());
517
518   // Fission Sections
519   DwarfInfoDWOSection =
520     Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0,
521                        SectionKind::getMetadata());
522   DwarfAbbrevDWOSection =
523     Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0,
524                        SectionKind::getMetadata());
525   DwarfStrDWOSection =
526     Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS,
527                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
528                        SectionKind::getMergeable1ByteCString());
529   DwarfLineDWOSection =
530     Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0,
531                        SectionKind::getMetadata());
532   DwarfLocDWOSection =
533     Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0,
534                        SectionKind::getMetadata());
535   DwarfStrOffDWOSection =
536     Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0,
537                        SectionKind::getMetadata());
538   DwarfAddrSection =
539     Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0,
540                        SectionKind::getMetadata());
541 }
542
543
544 void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) {
545   // COFF
546   BSSSection =
547     Ctx->getCOFFSection(".bss",
548                         COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
549                         COFF::IMAGE_SCN_MEM_READ |
550                         COFF::IMAGE_SCN_MEM_WRITE,
551                         SectionKind::getBSS());
552   TextSection =
553     Ctx->getCOFFSection(".text",
554                         COFF::IMAGE_SCN_CNT_CODE |
555                         COFF::IMAGE_SCN_MEM_EXECUTE |
556                         COFF::IMAGE_SCN_MEM_READ,
557                         SectionKind::getText());
558   DataSection =
559     Ctx->getCOFFSection(".data",
560                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
561                         COFF::IMAGE_SCN_MEM_READ |
562                         COFF::IMAGE_SCN_MEM_WRITE,
563                         SectionKind::getDataRel());
564   ReadOnlySection =
565     Ctx->getCOFFSection(".rdata",
566                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
567                         COFF::IMAGE_SCN_MEM_READ,
568                         SectionKind::getReadOnly());
569   if (T.getOS() == Triple::Win32) {
570     StaticCtorSection =
571       Ctx->getCOFFSection(".CRT$XCU",
572                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
573                           COFF::IMAGE_SCN_MEM_READ,
574                           SectionKind::getReadOnly());
575   } else {
576     StaticCtorSection =
577       Ctx->getCOFFSection(".ctors",
578                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
579                           COFF::IMAGE_SCN_MEM_READ |
580                           COFF::IMAGE_SCN_MEM_WRITE,
581                           SectionKind::getDataRel());
582   }
583
584
585   if (T.getOS() == Triple::Win32) {
586     StaticDtorSection =
587       Ctx->getCOFFSection(".CRT$XTX",
588                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
589                           COFF::IMAGE_SCN_MEM_READ,
590                           SectionKind::getReadOnly());
591   } else {
592     StaticDtorSection =
593       Ctx->getCOFFSection(".dtors",
594                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
595                           COFF::IMAGE_SCN_MEM_READ |
596                           COFF::IMAGE_SCN_MEM_WRITE,
597                           SectionKind::getDataRel());
598   }
599
600   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
601   // though it contains relocatable pointers.  In PIC mode, this is probably a
602   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
603   // adjusted or this should be a data section.
604   LSDASection =
605     Ctx->getCOFFSection(".gcc_except_table",
606                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
607                         COFF::IMAGE_SCN_MEM_READ,
608                         SectionKind::getReadOnly());
609
610   // Debug info.
611   DwarfAbbrevSection =
612     Ctx->getCOFFSection(".debug_abbrev",
613                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
614                         COFF::IMAGE_SCN_MEM_READ,
615                         SectionKind::getMetadata());
616   DwarfInfoSection =
617     Ctx->getCOFFSection(".debug_info",
618                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
619                         COFF::IMAGE_SCN_MEM_READ,
620                         SectionKind::getMetadata());
621   DwarfLineSection =
622     Ctx->getCOFFSection(".debug_line",
623                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
624                         COFF::IMAGE_SCN_MEM_READ,
625                         SectionKind::getMetadata());
626   DwarfFrameSection =
627     Ctx->getCOFFSection(".debug_frame",
628                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
629                         COFF::IMAGE_SCN_MEM_READ,
630                         SectionKind::getMetadata());
631   DwarfPubNamesSection =
632     Ctx->getCOFFSection(".debug_pubnames",
633                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
634                         COFF::IMAGE_SCN_MEM_READ,
635                         SectionKind::getMetadata());
636   DwarfPubTypesSection =
637     Ctx->getCOFFSection(".debug_pubtypes",
638                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
639                         COFF::IMAGE_SCN_MEM_READ,
640                         SectionKind::getMetadata());
641   DwarfGnuPubNamesSection =
642     Ctx->getCOFFSection(".debug_gnu_pubnames",
643                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
644                         COFF::IMAGE_SCN_MEM_READ,
645                         SectionKind::getMetadata());
646   DwarfGnuPubTypesSection =
647     Ctx->getCOFFSection(".debug_gnu_pubtypes",
648                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
649                         COFF::IMAGE_SCN_MEM_READ,
650                         SectionKind::getMetadata());
651   DwarfStrSection =
652     Ctx->getCOFFSection(".debug_str",
653                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
654                         COFF::IMAGE_SCN_MEM_READ,
655                         SectionKind::getMetadata());
656   DwarfLocSection =
657     Ctx->getCOFFSection(".debug_loc",
658                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
659                         COFF::IMAGE_SCN_MEM_READ,
660                         SectionKind::getMetadata());
661   DwarfARangesSection =
662     Ctx->getCOFFSection(".debug_aranges",
663                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
664                         COFF::IMAGE_SCN_MEM_READ,
665                         SectionKind::getMetadata());
666   DwarfRangesSection =
667     Ctx->getCOFFSection(".debug_ranges",
668                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
669                         COFF::IMAGE_SCN_MEM_READ,
670                         SectionKind::getMetadata());
671   DwarfMacroInfoSection =
672     Ctx->getCOFFSection(".debug_macinfo",
673                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
674                         COFF::IMAGE_SCN_MEM_READ,
675                         SectionKind::getMetadata());
676
677   DrectveSection =
678     Ctx->getCOFFSection(".drectve",
679                         COFF::IMAGE_SCN_LNK_INFO,
680                         SectionKind::getMetadata());
681
682   PDataSection =
683     Ctx->getCOFFSection(".pdata",
684                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
685                         COFF::IMAGE_SCN_MEM_READ,
686                         SectionKind::getDataRel());
687
688   XDataSection =
689     Ctx->getCOFFSection(".xdata",
690                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
691                         COFF::IMAGE_SCN_MEM_READ,
692                         SectionKind::getDataRel());
693   TLSDataSection =
694     Ctx->getCOFFSection(".tls$",
695                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
696                         COFF::IMAGE_SCN_MEM_READ |
697                         COFF::IMAGE_SCN_MEM_WRITE,
698                         SectionKind::getDataRel());
699 }
700
701 void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model relocm,
702                                             CodeModel::Model cm,
703                                             MCContext &ctx) {
704   RelocM = relocm;
705   CMModel = cm;
706   Ctx = &ctx;
707
708   // Common.
709   CommDirectiveSupportsAlignment = true;
710   SupportsWeakOmittedEHFrame = true;
711   IsFunctionEHFrameSymbolPrivate = true;
712
713   PersonalityEncoding = LSDAEncoding = FDEEncoding = FDECFIEncoding =
714     TTypeEncoding = dwarf::DW_EH_PE_absptr;
715
716   CompactUnwindDwarfEHFrameOnly = 0;
717
718   EHFrameSection = 0;             // Created on demand.
719   CompactUnwindSection = 0;       // Used only by selected targets.
720   DwarfAccelNamesSection = 0;     // Used only by selected targets.
721   DwarfAccelObjCSection = 0;      // Used only by selected targets.
722   DwarfAccelNamespaceSection = 0; // Used only by selected targets.
723   DwarfAccelTypesSection = 0;     // Used only by selected targets.
724
725   Triple T(TT);
726   Triple::ArchType Arch = T.getArch();
727   // FIXME: Checking for Arch here to filter out bogus triples such as
728   // cellspu-apple-darwin. Perhaps we should fix in Triple?
729   if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
730        Arch == Triple::arm || Arch == Triple::thumb ||
731        Arch == Triple::ppc || Arch == Triple::ppc64 ||
732        Arch == Triple::UnknownArch) &&
733       (T.isOSDarwin() || T.getEnvironment() == Triple::MachO)) {
734     Env = IsMachO;
735     InitMachOMCObjectFileInfo(T);
736   } else if ((Arch == Triple::x86 || Arch == Triple::x86_64) &&
737              (T.getEnvironment() != Triple::ELF) &&
738              (T.getOS() == Triple::MinGW32 || T.getOS() == Triple::Cygwin ||
739               T.getOS() == Triple::Win32)) {
740     Env = IsCOFF;
741     InitCOFFMCObjectFileInfo(T);
742   } else {
743     Env = IsELF;
744     InitELFMCObjectFileInfo(T);
745   }
746 }
747
748 void MCObjectFileInfo::InitEHFrameSection() {
749   if (Env == IsMachO)
750     EHFrameSection =
751       Ctx->getMachOSection("__TEXT", "__eh_frame",
752                            MCSectionMachO::S_COALESCED |
753                            MCSectionMachO::S_ATTR_NO_TOC |
754                            MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
755                            MCSectionMachO::S_ATTR_LIVE_SUPPORT,
756                            SectionKind::getReadOnly());
757   else if (Env == IsELF)
758     EHFrameSection =
759       Ctx->getELFSection(".eh_frame", EHSectionType,
760                          EHSectionFlags,
761                          SectionKind::getDataRel());
762   else
763     EHFrameSection =
764       Ctx->getCOFFSection(".eh_frame",
765                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
766                           COFF::IMAGE_SCN_MEM_READ |
767                           COFF::IMAGE_SCN_MEM_WRITE,
768                           SectionKind::getDataRel());
769 }