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"
17using namespace llvm;
18
19void 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  DwarfPubNamesSection =
190    Ctx->getMachOSection("__DWARF", "__debug_pubnames",
191                         MCSectionMachO::S_ATTR_DEBUG,
192                         SectionKind::getMetadata());
193  DwarfPubTypesSection =
194    Ctx->getMachOSection("__DWARF", "__debug_pubtypes",
195                         MCSectionMachO::S_ATTR_DEBUG,
196                         SectionKind::getMetadata());
197  DwarfStrSection =
198    Ctx->getMachOSection("__DWARF", "__debug_str",
199                         MCSectionMachO::S_ATTR_DEBUG,
200                         SectionKind::getMetadata());
201  DwarfLocSection =
202    Ctx->getMachOSection("__DWARF", "__debug_loc",
203                         MCSectionMachO::S_ATTR_DEBUG,
204                         SectionKind::getMetadata());
205  DwarfARangesSection =
206    Ctx->getMachOSection("__DWARF", "__debug_aranges",
207                         MCSectionMachO::S_ATTR_DEBUG,
208                         SectionKind::getMetadata());
209  DwarfRangesSection =
210    Ctx->getMachOSection("__DWARF", "__debug_ranges",
211                         MCSectionMachO::S_ATTR_DEBUG,
212                         SectionKind::getMetadata());
213  DwarfMacroInfoSection =
214    Ctx->getMachOSection("__DWARF", "__debug_macinfo",
215                         MCSectionMachO::S_ATTR_DEBUG,
216                         SectionKind::getMetadata());
217  DwarfDebugInlineSection =
218    Ctx->getMachOSection("__DWARF", "__debug_inlined",
219                         MCSectionMachO::S_ATTR_DEBUG,
220                         SectionKind::getMetadata());
221
222  TLSExtraDataSection = TLSTLVSection;
223}
224
225void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) {
226  // FIXME: Check this. Mips64el is using the base values, which is most likely
227  // incorrect.
228  if (T.getArch() != Triple::mips64el)
229    FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
230
231  if (T.getArch() == Triple::x86) {
232    PersonalityEncoding = (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    LSDAEncoding = (RelocM == Reloc::PIC_)
236      ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
237      : dwarf::DW_EH_PE_absptr;
238    FDEEncoding = (RelocM == Reloc::PIC_)
239      ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
240      : dwarf::DW_EH_PE_absptr;
241    TTypeEncoding = (RelocM == Reloc::PIC_)
242     ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
243     : dwarf::DW_EH_PE_absptr;
244  } else if (T.getArch() == Triple::x86_64) {
245    if (RelocM == Reloc::PIC_) {
246      PersonalityEncoding = 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      LSDAEncoding = dwarf::DW_EH_PE_pcrel |
250        (CMModel == CodeModel::Small
251         ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
252      FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
253      TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
254        ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
255         ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
256    } else {
257      PersonalityEncoding =
258        (CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
259        ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
260      LSDAEncoding = (CMModel == CodeModel::Small)
261        ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
262      FDEEncoding = dwarf::DW_EH_PE_udata4;
263      TTypeEncoding = (CMModel == CodeModel::Small)
264        ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
265    }
266  }  else if (T.getArch() ==  Triple::aarch64) {
267    // The small model guarantees static code/data size < 4GB, but not where it
268    // will be in memory. Most of these could end up >2GB away so even a signed
269    // pc-relative 32-bit address is insufficient, theoretically.
270    if (RelocM == Reloc::PIC_) {
271      PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
272        dwarf::DW_EH_PE_sdata8;
273      LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
274      FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
275      TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
276        dwarf::DW_EH_PE_sdata8;
277    } else {
278      PersonalityEncoding = dwarf::DW_EH_PE_absptr;
279      LSDAEncoding = dwarf::DW_EH_PE_absptr;
280      FDEEncoding = dwarf::DW_EH_PE_udata4;
281      TTypeEncoding = dwarf::DW_EH_PE_absptr;
282    }
283  } else if (T.getArch() == Triple::ppc64) {
284    PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
285      dwarf::DW_EH_PE_udata8;
286    LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
287    FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
288    TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
289      dwarf::DW_EH_PE_udata8;
290  }
291
292  // Solaris requires different flags for .eh_frame to seemingly every other
293  // platform.
294  EHSectionType = ELF::SHT_PROGBITS;
295  EHSectionFlags = ELF::SHF_ALLOC;
296  if (T.getOS() == Triple::Solaris) {
297    if (T.getArch() == Triple::x86_64)
298      EHSectionType = ELF::SHT_X86_64_UNWIND;
299    else
300      EHSectionFlags |= ELF::SHF_WRITE;
301  }
302
303
304  // ELF
305  BSSSection =
306    Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
307                       ELF::SHF_WRITE | ELF::SHF_ALLOC,
308                       SectionKind::getBSS());
309
310  TextSection =
311    Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
312                       ELF::SHF_EXECINSTR |
313                       ELF::SHF_ALLOC,
314                       SectionKind::getText());
315
316  DataSection =
317    Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
318                       ELF::SHF_WRITE |ELF::SHF_ALLOC,
319                       SectionKind::getDataRel());
320
321  ReadOnlySection =
322    Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS,
323                       ELF::SHF_ALLOC,
324                       SectionKind::getReadOnly());
325
326  TLSDataSection =
327    Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
328                       ELF::SHF_ALLOC | ELF::SHF_TLS |
329                       ELF::SHF_WRITE,
330                       SectionKind::getThreadData());
331
332  TLSBSSSection =
333    Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
334                       ELF::SHF_ALLOC | ELF::SHF_TLS |
335                       ELF::SHF_WRITE,
336                       SectionKind::getThreadBSS());
337
338  DataRelSection =
339    Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS,
340                       ELF::SHF_ALLOC |ELF::SHF_WRITE,
341                       SectionKind::getDataRel());
342
343  DataRelLocalSection =
344    Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
345                       ELF::SHF_ALLOC |ELF::SHF_WRITE,
346                       SectionKind::getDataRelLocal());
347
348  DataRelROSection =
349    Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
350                       ELF::SHF_ALLOC |ELF::SHF_WRITE,
351                       SectionKind::getReadOnlyWithRel());
352
353  DataRelROLocalSection =
354    Ctx->getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
355                       ELF::SHF_ALLOC |ELF::SHF_WRITE,
356                       SectionKind::getReadOnlyWithRelLocal());
357
358  MergeableConst4Section =
359    Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
360                       ELF::SHF_ALLOC |ELF::SHF_MERGE,
361                       SectionKind::getMergeableConst4());
362
363  MergeableConst8Section =
364    Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
365                       ELF::SHF_ALLOC |ELF::SHF_MERGE,
366                       SectionKind::getMergeableConst8());
367
368  MergeableConst16Section =
369    Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
370                       ELF::SHF_ALLOC |ELF::SHF_MERGE,
371                       SectionKind::getMergeableConst16());
372
373  StaticCtorSection =
374    Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS,
375                       ELF::SHF_ALLOC |ELF::SHF_WRITE,
376                       SectionKind::getDataRel());
377
378  StaticDtorSection =
379    Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS,
380                       ELF::SHF_ALLOC |ELF::SHF_WRITE,
381                       SectionKind::getDataRel());
382
383  // Exception Handling Sections.
384
385  // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
386  // it contains relocatable pointers.  In PIC mode, this is probably a big
387  // runtime hit for C++ apps.  Either the contents of the LSDA need to be
388  // adjusted or this should be a data section.
389  LSDASection =
390    Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
391                       ELF::SHF_ALLOC,
392                       SectionKind::getReadOnly());
393
394  // Debug Info Sections.
395  DwarfAbbrevSection =
396    Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
397                       SectionKind::getMetadata());
398  DwarfInfoSection =
399    Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
400                       SectionKind::getMetadata());
401  DwarfLineSection =
402    Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
403                       SectionKind::getMetadata());
404  DwarfFrameSection =
405    Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
406                       SectionKind::getMetadata());
407  DwarfPubNamesSection =
408    Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
409                       SectionKind::getMetadata());
410  DwarfPubTypesSection =
411    Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
412                       SectionKind::getMetadata());
413  DwarfStrSection =
414    Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS,
415                       ELF::SHF_MERGE | ELF::SHF_STRINGS,
416                       SectionKind::getMergeable1ByteCString());
417  DwarfLocSection =
418    Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
419                       SectionKind::getMetadata());
420  DwarfARangesSection =
421    Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
422                       SectionKind::getMetadata());
423  DwarfRangesSection =
424    Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
425                       SectionKind::getMetadata());
426  DwarfMacroInfoSection =
427    Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
428                       SectionKind::getMetadata());
429
430  // DWARF5 Experimental Debug Info
431
432  // Accelerator Tables
433  DwarfAccelNamesSection =
434    Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0,
435                       SectionKind::getMetadata());
436  DwarfAccelObjCSection =
437    Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0,
438                       SectionKind::getMetadata());
439  DwarfAccelNamespaceSection =
440    Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0,
441                       SectionKind::getMetadata());
442  DwarfAccelTypesSection =
443    Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0,
444                       SectionKind::getMetadata());
445
446  // Fission Sections
447  DwarfInfoDWOSection =
448    Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0,
449                       SectionKind::getMetadata());
450  DwarfAbbrevDWOSection =
451    Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0,
452                       SectionKind::getMetadata());
453  DwarfStrDWOSection =
454    Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS,
455                       ELF::SHF_MERGE | ELF::SHF_STRINGS,
456                       SectionKind::getMergeable1ByteCString());
457  DwarfLineDWOSection =
458    Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0,
459                       SectionKind::getMetadata());
460  DwarfLocDWOSection =
461    Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0,
462                       SectionKind::getMetadata());
463  DwarfStrOffDWOSection =
464    Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0,
465                       SectionKind::getMetadata());
466  DwarfAddrSection =
467    Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0,
468                       SectionKind::getMetadata());
469}
470
471
472void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) {
473  // COFF
474  TextSection =
475    Ctx->getCOFFSection(".text",
476                        COFF::IMAGE_SCN_CNT_CODE |
477                        COFF::IMAGE_SCN_MEM_EXECUTE |
478                        COFF::IMAGE_SCN_MEM_READ,
479                        SectionKind::getText());
480  DataSection =
481    Ctx->getCOFFSection(".data",
482                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
483                        COFF::IMAGE_SCN_MEM_READ |
484                        COFF::IMAGE_SCN_MEM_WRITE,
485                        SectionKind::getDataRel());
486  ReadOnlySection =
487    Ctx->getCOFFSection(".rdata",
488                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
489                        COFF::IMAGE_SCN_MEM_READ,
490                        SectionKind::getReadOnly());
491  if (T.getOS() == Triple::Win32) {
492    StaticCtorSection =
493      Ctx->getCOFFSection(".CRT$XCU",
494                          COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
495                          COFF::IMAGE_SCN_MEM_READ,
496                          SectionKind::getReadOnly());
497  } else {
498    StaticCtorSection =
499      Ctx->getCOFFSection(".ctors",
500                          COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
501                          COFF::IMAGE_SCN_MEM_READ |
502                          COFF::IMAGE_SCN_MEM_WRITE,
503                          SectionKind::getDataRel());
504  }
505
506
507  if (T.getOS() == Triple::Win32) {
508    StaticDtorSection =
509      Ctx->getCOFFSection(".CRT$XTX",
510                          COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
511                          COFF::IMAGE_SCN_MEM_READ,
512                          SectionKind::getReadOnly());
513  } else {
514    StaticDtorSection =
515      Ctx->getCOFFSection(".dtors",
516                          COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
517                          COFF::IMAGE_SCN_MEM_READ |
518                          COFF::IMAGE_SCN_MEM_WRITE,
519                          SectionKind::getDataRel());
520  }
521
522  // FIXME: We're emitting LSDA info into a readonly section on COFF, even
523  // though it contains relocatable pointers.  In PIC mode, this is probably a
524  // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
525  // adjusted or this should be a data section.
526  LSDASection =
527    Ctx->getCOFFSection(".gcc_except_table",
528                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
529                        COFF::IMAGE_SCN_MEM_READ,
530                        SectionKind::getReadOnly());
531
532  // Debug info.
533  DwarfAbbrevSection =
534    Ctx->getCOFFSection(".debug_abbrev",
535                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
536                        COFF::IMAGE_SCN_MEM_READ,
537                        SectionKind::getMetadata());
538  DwarfInfoSection =
539    Ctx->getCOFFSection(".debug_info",
540                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
541                        COFF::IMAGE_SCN_MEM_READ,
542                        SectionKind::getMetadata());
543  DwarfLineSection =
544    Ctx->getCOFFSection(".debug_line",
545                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
546                        COFF::IMAGE_SCN_MEM_READ,
547                        SectionKind::getMetadata());
548  DwarfFrameSection =
549    Ctx->getCOFFSection(".debug_frame",
550                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
551                        COFF::IMAGE_SCN_MEM_READ,
552                        SectionKind::getMetadata());
553  DwarfPubNamesSection =
554    Ctx->getCOFFSection(".debug_pubnames",
555                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
556                        COFF::IMAGE_SCN_MEM_READ,
557                        SectionKind::getMetadata());
558  DwarfPubTypesSection =
559    Ctx->getCOFFSection(".debug_pubtypes",
560                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
561                        COFF::IMAGE_SCN_MEM_READ,
562                        SectionKind::getMetadata());
563  DwarfStrSection =
564    Ctx->getCOFFSection(".debug_str",
565                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
566                        COFF::IMAGE_SCN_MEM_READ,
567                        SectionKind::getMetadata());
568  DwarfLocSection =
569    Ctx->getCOFFSection(".debug_loc",
570                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
571                        COFF::IMAGE_SCN_MEM_READ,
572                        SectionKind::getMetadata());
573  DwarfARangesSection =
574    Ctx->getCOFFSection(".debug_aranges",
575                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
576                        COFF::IMAGE_SCN_MEM_READ,
577                        SectionKind::getMetadata());
578  DwarfRangesSection =
579    Ctx->getCOFFSection(".debug_ranges",
580                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
581                        COFF::IMAGE_SCN_MEM_READ,
582                        SectionKind::getMetadata());
583  DwarfMacroInfoSection =
584    Ctx->getCOFFSection(".debug_macinfo",
585                        COFF::IMAGE_SCN_MEM_DISCARDABLE |
586                        COFF::IMAGE_SCN_MEM_READ,
587                        SectionKind::getMetadata());
588
589  DrectveSection =
590    Ctx->getCOFFSection(".drectve",
591                        COFF::IMAGE_SCN_LNK_INFO,
592                        SectionKind::getMetadata());
593
594  PDataSection =
595    Ctx->getCOFFSection(".pdata",
596                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
597                        COFF::IMAGE_SCN_MEM_READ,
598                        SectionKind::getDataRel());
599
600  XDataSection =
601    Ctx->getCOFFSection(".xdata",
602                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
603                        COFF::IMAGE_SCN_MEM_READ,
604                        SectionKind::getDataRel());
605  TLSDataSection =
606    Ctx->getCOFFSection(".tls$",
607                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
608                        COFF::IMAGE_SCN_MEM_READ |
609                        COFF::IMAGE_SCN_MEM_WRITE,
610                        SectionKind::getDataRel());
611}
612
613void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model relocm,
614                                            CodeModel::Model cm,
615                                            MCContext &ctx) {
616  RelocM = relocm;
617  CMModel = cm;
618  Ctx = &ctx;
619
620  // Common.
621  CommDirectiveSupportsAlignment = true;
622  SupportsWeakOmittedEHFrame = true;
623  IsFunctionEHFrameSymbolPrivate = true;
624
625  PersonalityEncoding = LSDAEncoding = FDEEncoding = FDECFIEncoding =
626    TTypeEncoding = dwarf::DW_EH_PE_absptr;
627
628  EHFrameSection = 0;             // Created on demand.
629  CompactUnwindSection = 0;       // Used only by selected targets.
630  DwarfAccelNamesSection = 0;     // Used only by selected targets.
631  DwarfAccelObjCSection = 0;      // Used only by selected targets.
632  DwarfAccelNamespaceSection = 0; // Used only by selected targets.
633  DwarfAccelTypesSection = 0;     // Used only by selected targets.
634
635  Triple T(TT);
636  Triple::ArchType Arch = T.getArch();
637  // FIXME: Checking for Arch here to filter out bogus triples such as
638  // cellspu-apple-darwin. Perhaps we should fix in Triple?
639  if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
640       Arch == Triple::arm || Arch == Triple::thumb ||
641       Arch == Triple::ppc || Arch == Triple::ppc64 ||
642       Arch == Triple::UnknownArch) &&
643      (T.isOSDarwin() || T.getEnvironment() == Triple::MachO)) {
644    Env = IsMachO;
645    InitMachOMCObjectFileInfo(T);
646  } else if ((Arch == Triple::x86 || Arch == Triple::x86_64) &&
647             (T.getEnvironment() != Triple::ELF) &&
648             (T.getOS() == Triple::MinGW32 || T.getOS() == Triple::Cygwin ||
649              T.getOS() == Triple::Win32)) {
650    Env = IsCOFF;
651    InitCOFFMCObjectFileInfo(T);
652  } else {
653    Env = IsELF;
654    InitELFMCObjectFileInfo(T);
655  }
656}
657
658void MCObjectFileInfo::InitEHFrameSection() {
659  if (Env == IsMachO)
660    EHFrameSection =
661      Ctx->getMachOSection("__TEXT", "__eh_frame",
662                           MCSectionMachO::S_COALESCED |
663                           MCSectionMachO::S_ATTR_NO_TOC |
664                           MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
665                           MCSectionMachO::S_ATTR_LIVE_SUPPORT,
666                           SectionKind::getReadOnly());
667  else if (Env == IsELF)
668    EHFrameSection =
669      Ctx->getELFSection(".eh_frame", EHSectionType,
670                         EHSectionFlags,
671                         SectionKind::getDataRel());
672  else
673    EHFrameSection =
674      Ctx->getCOFFSection(".eh_frame",
675                          COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
676                          COFF::IMAGE_SCN_MEM_READ |
677                          COFF::IMAGE_SCN_MEM_WRITE,
678                          SectionKind::getDataRel());
679}
680