5540b55b1a800795dab9a1a1982aad2ef3d8597b
[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/MC/MCContext.h"
12 #include "llvm/MC/MCSection.h"
13 #include "llvm/MC/MCSectionCOFF.h"
14 #include "llvm/MC/MCSectionELF.h"
15 #include "llvm/MC/MCSectionMachO.h"
16 #include "llvm/ADT/Triple.h"
17 using namespace llvm;
18
19 void MCObjectFileInfo::InitMachOMCObjectFileInfo(Triple T) {
20   // MachO
21   IsFunctionEHFrameSymbolPrivate = false;
22   SupportsWeakOmittedEHFrame = false;
23
24   // .comm doesn't support alignment before Leopard.
25   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
26     CommDirectiveSupportsAlignment = false;
27
28   TextSection // .text
29     = Ctx->getMachOSection("__TEXT", "__text",
30                            MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
31                            SectionKind::getText());
32   DataSection // .data
33     = Ctx->getMachOSection("__DATA", "__data", 0,
34                            SectionKind::getDataRel());
35
36   TLSDataSection // .tdata
37     = Ctx->getMachOSection("__DATA", "__thread_data",
38                            MCSectionMachO::S_THREAD_LOCAL_REGULAR,
39                            SectionKind::getDataRel());
40   TLSBSSSection // .tbss
41     = Ctx->getMachOSection("__DATA", "__thread_bss",
42                            MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
43                            SectionKind::getThreadBSS());
44
45   // TODO: Verify datarel below.
46   TLSTLVSection // .tlv
47     = Ctx->getMachOSection("__DATA", "__thread_vars",
48                            MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
49                            SectionKind::getDataRel());
50
51   TLSThreadInitSection
52     = Ctx->getMachOSection("__DATA", "__thread_init",
53                            MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
54                            SectionKind::getDataRel());
55
56   CStringSection // .cstring
57     = Ctx->getMachOSection("__TEXT", "__cstring",
58                            MCSectionMachO::S_CSTRING_LITERALS,
59                            SectionKind::getMergeable1ByteCString());
60   UStringSection
61     = Ctx->getMachOSection("__TEXT","__ustring", 0,
62                            SectionKind::getMergeable2ByteCString());
63   FourByteConstantSection // .literal4
64     = Ctx->getMachOSection("__TEXT", "__literal4",
65                            MCSectionMachO::S_4BYTE_LITERALS,
66                            SectionKind::getMergeableConst4());
67   EightByteConstantSection // .literal8
68     = Ctx->getMachOSection("__TEXT", "__literal8",
69                            MCSectionMachO::S_8BYTE_LITERALS,
70                            SectionKind::getMergeableConst8());
71
72   // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
73   // to using it in -static mode.
74   SixteenByteConstantSection = 0;
75   if (RelocM != Reloc::Static &&
76       T.getArch() != Triple::x86_64 && T.getArch() != Triple::ppc64)
77     SixteenByteConstantSection =   // .literal16
78       Ctx->getMachOSection("__TEXT", "__literal16",
79                            MCSectionMachO::S_16BYTE_LITERALS,
80                            SectionKind::getMergeableConst16());
81
82   ReadOnlySection  // .const
83     = Ctx->getMachOSection("__TEXT", "__const", 0,
84                            SectionKind::getReadOnly());
85
86   TextCoalSection
87     = Ctx->getMachOSection("__TEXT", "__textcoal_nt",
88                            MCSectionMachO::S_COALESCED |
89                            MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
90                            SectionKind::getText());
91   ConstTextCoalSection
92     = Ctx->getMachOSection("__TEXT", "__const_coal",
93                            MCSectionMachO::S_COALESCED,
94                            SectionKind::getReadOnly());
95   ConstDataSection  // .const_data
96     = Ctx->getMachOSection("__DATA", "__const", 0,
97                            SectionKind::getReadOnlyWithRel());
98   DataCoalSection
99     = Ctx->getMachOSection("__DATA","__datacoal_nt",
100                            MCSectionMachO::S_COALESCED,
101                            SectionKind::getDataRel());
102   DataCommonSection
103     = Ctx->getMachOSection("__DATA","__common",
104                            MCSectionMachO::S_ZEROFILL,
105                            SectionKind::getBSS());
106   DataBSSSection
107     = Ctx->getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
108                            SectionKind::getBSS());
109
110
111   LazySymbolPointerSection
112     = Ctx->getMachOSection("__DATA", "__la_symbol_ptr",
113                            MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
114                            SectionKind::getMetadata());
115   NonLazySymbolPointerSection
116     = Ctx->getMachOSection("__DATA", "__nl_symbol_ptr",
117                            MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
118                            SectionKind::getMetadata());
119
120   if (RelocM == Reloc::Static) {
121     StaticCtorSection
122       = Ctx->getMachOSection("__TEXT", "__constructor", 0,
123                              SectionKind::getDataRel());
124     StaticDtorSection
125       = Ctx->getMachOSection("__TEXT", "__destructor", 0,
126                              SectionKind::getDataRel());
127   } else {
128     StaticCtorSection
129       = Ctx->getMachOSection("__DATA", "__mod_init_func",
130                              MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
131                              SectionKind::getDataRel());
132     StaticDtorSection
133       = Ctx->getMachOSection("__DATA", "__mod_term_func",
134                              MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
135                              SectionKind::getDataRel());
136   }
137
138   // Exception Handling.
139   LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0,
140                                      SectionKind::getReadOnlyWithRel());
141
142   if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6))
143     CompactUnwindSection =
144       Ctx->getMachOSection("__LD", "__compact_unwind",
145                            MCSectionMachO::S_ATTR_DEBUG,
146                            SectionKind::getReadOnly());
147
148   // Debug Information.
149   DwarfAbbrevSection =
150     Ctx->getMachOSection("__DWARF", "__debug_abbrev",
151                          MCSectionMachO::S_ATTR_DEBUG,
152                          SectionKind::getMetadata());
153   DwarfInfoSection =
154     Ctx->getMachOSection("__DWARF", "__debug_info",
155                          MCSectionMachO::S_ATTR_DEBUG,
156                          SectionKind::getMetadata());
157   DwarfLineSection =
158     Ctx->getMachOSection("__DWARF", "__debug_line",
159                          MCSectionMachO::S_ATTR_DEBUG,
160                          SectionKind::getMetadata());
161   DwarfFrameSection =
162     Ctx->getMachOSection("__DWARF", "__debug_frame",
163                          MCSectionMachO::S_ATTR_DEBUG,
164                          SectionKind::getMetadata());
165   DwarfPubNamesSection =
166     Ctx->getMachOSection("__DWARF", "__debug_pubnames",
167                          MCSectionMachO::S_ATTR_DEBUG,
168                          SectionKind::getMetadata());
169   DwarfPubTypesSection =
170     Ctx->getMachOSection("__DWARF", "__debug_pubtypes",
171                          MCSectionMachO::S_ATTR_DEBUG,
172                          SectionKind::getMetadata());
173   DwarfStrSection =
174     Ctx->getMachOSection("__DWARF", "__debug_str",
175                          MCSectionMachO::S_ATTR_DEBUG,
176                          SectionKind::getMetadata());
177   DwarfLocSection =
178     Ctx->getMachOSection("__DWARF", "__debug_loc",
179                          MCSectionMachO::S_ATTR_DEBUG,
180                          SectionKind::getMetadata());
181   DwarfARangesSection =
182     Ctx->getMachOSection("__DWARF", "__debug_aranges",
183                          MCSectionMachO::S_ATTR_DEBUG,
184                          SectionKind::getMetadata());
185   DwarfRangesSection =
186     Ctx->getMachOSection("__DWARF", "__debug_ranges",
187                          MCSectionMachO::S_ATTR_DEBUG,
188                          SectionKind::getMetadata());
189   DwarfMacroInfoSection =
190     Ctx->getMachOSection("__DWARF", "__debug_macinfo",
191                          MCSectionMachO::S_ATTR_DEBUG,
192                          SectionKind::getMetadata());
193   DwarfDebugInlineSection =
194     Ctx->getMachOSection("__DWARF", "__debug_inlined",
195                          MCSectionMachO::S_ATTR_DEBUG,
196                          SectionKind::getMetadata());
197
198   TLSExtraDataSection = TLSTLVSection;
199 }
200
201 void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) {
202   // ELF
203   BSSSection =
204     Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
205                        ELF::SHF_WRITE |ELF::SHF_ALLOC,
206                        SectionKind::getBSS());
207
208   TextSection =
209     Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
210                        ELF::SHF_EXECINSTR |
211                        ELF::SHF_ALLOC,
212                        SectionKind::getText());
213
214   DataSection =
215     Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
216                        ELF::SHF_WRITE |ELF::SHF_ALLOC,
217                        SectionKind::getDataRel());
218
219   ReadOnlySection =
220     Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS,
221                        ELF::SHF_ALLOC,
222                        SectionKind::getReadOnly());
223
224   TLSDataSection =
225     Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
226                        ELF::SHF_ALLOC | ELF::SHF_TLS |
227                        ELF::SHF_WRITE,
228                        SectionKind::getThreadData());
229
230   TLSBSSSection =
231     Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
232                        ELF::SHF_ALLOC | ELF::SHF_TLS |
233                        ELF::SHF_WRITE,
234                        SectionKind::getThreadBSS());
235
236   DataRelSection =
237     Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS,
238                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
239                        SectionKind::getDataRel());
240
241   DataRelLocalSection =
242     Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
243                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
244                        SectionKind::getDataRelLocal());
245
246   DataRelROSection =
247     Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
248                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
249                        SectionKind::getReadOnlyWithRel());
250
251   DataRelROLocalSection =
252     Ctx->getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
253                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
254                        SectionKind::getReadOnlyWithRelLocal());
255
256   MergeableConst4Section =
257     Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
258                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
259                        SectionKind::getMergeableConst4());
260
261   MergeableConst8Section =
262     Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
263                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
264                        SectionKind::getMergeableConst8());
265
266   MergeableConst16Section =
267     Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
268                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
269                        SectionKind::getMergeableConst16());
270
271   StaticCtorSection =
272     Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS,
273                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
274                        SectionKind::getDataRel());
275
276   StaticDtorSection =
277     Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS,
278                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
279                        SectionKind::getDataRel());
280
281   // Exception Handling Sections.
282
283   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
284   // it contains relocatable pointers.  In PIC mode, this is probably a big
285   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
286   // adjusted or this should be a data section.
287   LSDASection =
288     Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
289                        ELF::SHF_ALLOC,
290                        SectionKind::getReadOnly());
291
292   // Debug Info Sections.
293   DwarfAbbrevSection =
294     Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
295                        SectionKind::getMetadata());
296   DwarfInfoSection =
297     Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
298                        SectionKind::getMetadata());
299   DwarfLineSection =
300     Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
301                        SectionKind::getMetadata());
302   DwarfFrameSection =
303     Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
304                        SectionKind::getMetadata());
305   DwarfPubNamesSection =
306     Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
307                        SectionKind::getMetadata());
308   DwarfPubTypesSection =
309     Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
310                        SectionKind::getMetadata());
311   DwarfStrSection =
312     Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS, 0,
313                        SectionKind::getMetadata());
314   DwarfLocSection =
315     Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
316                        SectionKind::getMetadata());
317   DwarfARangesSection =
318     Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
319                        SectionKind::getMetadata());
320   DwarfRangesSection =
321     Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
322                        SectionKind::getMetadata());
323   DwarfMacroInfoSection =
324     Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
325                        SectionKind::getMetadata());
326 }
327
328
329 void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) {
330   // COFF
331   TextSection =
332     Ctx->getCOFFSection(".text",
333                         COFF::IMAGE_SCN_CNT_CODE |
334                         COFF::IMAGE_SCN_MEM_EXECUTE |
335                         COFF::IMAGE_SCN_MEM_READ,
336                         SectionKind::getText());
337   DataSection =
338     Ctx->getCOFFSection(".data",
339                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
340                         COFF::IMAGE_SCN_MEM_READ |
341                         COFF::IMAGE_SCN_MEM_WRITE,
342                         SectionKind::getDataRel());
343   ReadOnlySection =
344     Ctx->getCOFFSection(".rdata",
345                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
346                         COFF::IMAGE_SCN_MEM_READ,
347                         SectionKind::getReadOnly());
348   StaticCtorSection =
349     Ctx->getCOFFSection(".ctors",
350                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
351                         COFF::IMAGE_SCN_MEM_READ |
352                         COFF::IMAGE_SCN_MEM_WRITE,
353                         SectionKind::getDataRel());
354   StaticDtorSection =
355     Ctx->getCOFFSection(".dtors",
356                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
357                         COFF::IMAGE_SCN_MEM_READ |
358                         COFF::IMAGE_SCN_MEM_WRITE,
359                         SectionKind::getDataRel());
360
361   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
362   // though it contains relocatable pointers.  In PIC mode, this is probably a
363   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
364   // adjusted or this should be a data section.
365   LSDASection =
366     Ctx->getCOFFSection(".gcc_except_table",
367                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
368                         COFF::IMAGE_SCN_MEM_READ,
369                         SectionKind::getReadOnly());
370
371   // Debug info.
372   DwarfAbbrevSection =
373     Ctx->getCOFFSection(".debug_abbrev",
374                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
375                         COFF::IMAGE_SCN_MEM_READ,
376                         SectionKind::getMetadata());
377   DwarfInfoSection =
378     Ctx->getCOFFSection(".debug_info",
379                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
380                         COFF::IMAGE_SCN_MEM_READ,
381                         SectionKind::getMetadata());
382   DwarfLineSection =
383     Ctx->getCOFFSection(".debug_line",
384                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
385                         COFF::IMAGE_SCN_MEM_READ,
386                         SectionKind::getMetadata());
387   DwarfFrameSection =
388     Ctx->getCOFFSection(".debug_frame",
389                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
390                         COFF::IMAGE_SCN_MEM_READ,
391                         SectionKind::getMetadata());
392   DwarfPubNamesSection =
393     Ctx->getCOFFSection(".debug_pubnames",
394                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
395                         COFF::IMAGE_SCN_MEM_READ,
396                         SectionKind::getMetadata());
397   DwarfPubTypesSection =
398     Ctx->getCOFFSection(".debug_pubtypes",
399                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
400                         COFF::IMAGE_SCN_MEM_READ,
401                         SectionKind::getMetadata());
402   DwarfStrSection =
403     Ctx->getCOFFSection(".debug_str",
404                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
405                         COFF::IMAGE_SCN_MEM_READ,
406                         SectionKind::getMetadata());
407   DwarfLocSection =
408     Ctx->getCOFFSection(".debug_loc",
409                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
410                         COFF::IMAGE_SCN_MEM_READ,
411                         SectionKind::getMetadata());
412   DwarfARangesSection =
413     Ctx->getCOFFSection(".debug_aranges",
414                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
415                         COFF::IMAGE_SCN_MEM_READ,
416                         SectionKind::getMetadata());
417   DwarfRangesSection =
418     Ctx->getCOFFSection(".debug_ranges",
419                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
420                         COFF::IMAGE_SCN_MEM_READ,
421                         SectionKind::getMetadata());
422   DwarfMacroInfoSection =
423     Ctx->getCOFFSection(".debug_macinfo",
424                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
425                         COFF::IMAGE_SCN_MEM_READ,
426                         SectionKind::getMetadata());
427
428   DrectveSection =
429     Ctx->getCOFFSection(".drectve",
430                         COFF::IMAGE_SCN_LNK_INFO,
431                         SectionKind::getMetadata());
432
433   PDataSection =
434     Ctx->getCOFFSection(".pdata",
435                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
436                         COFF::IMAGE_SCN_MEM_READ |
437                         COFF::IMAGE_SCN_MEM_WRITE,
438                         SectionKind::getDataRel());
439
440   XDataSection =
441     Ctx->getCOFFSection(".xdata",
442                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
443                         COFF::IMAGE_SCN_MEM_READ |
444                         COFF::IMAGE_SCN_MEM_WRITE,
445                         SectionKind::getDataRel());
446 }
447
448 void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model relocm,
449                                             MCContext &ctx) {
450   RelocM = relocm;
451   Ctx = &ctx;
452
453   // Common.
454   CommDirectiveSupportsAlignment = true;
455   SupportsWeakOmittedEHFrame = true;
456   IsFunctionEHFrameSymbolPrivate = true;
457   EHFrameSection = 0;
458
459   Triple T(TT);
460   Triple::ArchType Arch = T.getArch();
461   // FIXME: Checking for Arch here to filter out bogus triples such as
462   // cellspu-apple-darwin. Perhaps we should fix in Triple?
463   if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
464        Arch == Triple::arm || Arch == Triple::thumb ||
465        Arch == Triple::ppc || Arch == Triple::ppc64 ||
466        Arch == Triple::UnknownArch) &&
467       (T.isOSDarwin() || T.getEnvironment() == Triple::MachO)) {
468     Env = IsMachO;
469     InitMachOMCObjectFileInfo(T);
470   } else if (T.getOS() == Triple::MinGW32 || T.getOS() == Triple::Cygwin ||
471              T.getOS() == Triple::Win32) {
472     Env = IsCOFF;
473     InitCOFFMCObjectFileInfo(T);
474   } else {
475     Env = IsELF;
476     InitELFMCObjectFileInfo(T);
477   }
478 }
479
480 void MCObjectFileInfo::InitEHFrameSection() {
481   if (Env == IsMachO)
482     EHFrameSection =
483       Ctx->getMachOSection("__TEXT", "__eh_frame",
484                            MCSectionMachO::S_COALESCED |
485                            MCSectionMachO::S_ATTR_NO_TOC |
486                            MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
487                            MCSectionMachO::S_ATTR_LIVE_SUPPORT,
488                            SectionKind::getReadOnly());
489   else if (Env == IsELF)
490     EHFrameSection =
491       Ctx->getELFSection(".eh_frame", ELF::SHT_PROGBITS,
492                          ELF::SHF_ALLOC,
493                          SectionKind::getDataRel());
494   else
495     EHFrameSection =
496       Ctx->getCOFFSection(".eh_frame",
497                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
498                           COFF::IMAGE_SCN_MEM_READ |
499                           COFF::IMAGE_SCN_MEM_WRITE,
500                           SectionKind::getDataRel());
501 }