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