1 //===-- MObjectFileInfo.cpp - Object File Information ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
19 void MCObjectFileInfo::InitMachOMCObjectFileInfo(Triple T) {
21 IsFunctionEHFrameSymbolPrivate = false;
22 SupportsWeakOmittedEHFrame = false;
24 // .comm doesn't support alignment before Leopard.
25 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
26 CommDirectiveSupportsAlignment = false;
29 = Ctx->getMachOSection("__TEXT", "__text",
30 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
31 SectionKind::getText());
33 = Ctx->getMachOSection("__DATA", "__data", 0,
34 SectionKind::getDataRel());
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());
45 // TODO: Verify datarel below.
47 = Ctx->getMachOSection("__DATA", "__thread_vars",
48 MCSectionMachO::S_THREAD_LOCAL_VARIABLES,
49 SectionKind::getDataRel());
52 = Ctx->getMachOSection("__DATA", "__thread_init",
53 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
54 SectionKind::getDataRel());
56 CStringSection // .cstring
57 = Ctx->getMachOSection("__TEXT", "__cstring",
58 MCSectionMachO::S_CSTRING_LITERALS,
59 SectionKind::getMergeable1ByteCString());
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());
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());
82 ReadOnlySection // .const
83 = Ctx->getMachOSection("__TEXT", "__const", 0,
84 SectionKind::getReadOnly());
87 = Ctx->getMachOSection("__TEXT", "__textcoal_nt",
88 MCSectionMachO::S_COALESCED |
89 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
90 SectionKind::getText());
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());
99 = Ctx->getMachOSection("__DATA","__datacoal_nt",
100 MCSectionMachO::S_COALESCED,
101 SectionKind::getDataRel());
103 = Ctx->getMachOSection("__DATA","__common",
104 MCSectionMachO::S_ZEROFILL,
105 SectionKind::getBSS());
107 = Ctx->getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
108 SectionKind::getBSS());
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());
120 if (RelocM == Reloc::Static) {
122 = Ctx->getMachOSection("__TEXT", "__constructor", 0,
123 SectionKind::getDataRel());
125 = Ctx->getMachOSection("__TEXT", "__destructor", 0,
126 SectionKind::getDataRel());
129 = Ctx->getMachOSection("__DATA", "__mod_init_func",
130 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
131 SectionKind::getDataRel());
133 = Ctx->getMachOSection("__DATA", "__mod_term_func",
134 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
135 SectionKind::getDataRel());
138 // Exception Handling.
139 LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0,
140 SectionKind::getReadOnlyWithRel());
142 if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6))
143 CompactUnwindSection =
144 Ctx->getMachOSection("__LD", "__compact_unwind",
145 MCSectionMachO::S_ATTR_DEBUG,
146 SectionKind::getReadOnly());
148 // Debug Information.
150 Ctx->getMachOSection("__DWARF", "__debug_abbrev",
151 MCSectionMachO::S_ATTR_DEBUG,
152 SectionKind::getMetadata());
154 Ctx->getMachOSection("__DWARF", "__debug_info",
155 MCSectionMachO::S_ATTR_DEBUG,
156 SectionKind::getMetadata());
158 Ctx->getMachOSection("__DWARF", "__debug_line",
159 MCSectionMachO::S_ATTR_DEBUG,
160 SectionKind::getMetadata());
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());
174 Ctx->getMachOSection("__DWARF", "__debug_str",
175 MCSectionMachO::S_ATTR_DEBUG,
176 SectionKind::getMetadata());
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());
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());
198 TLSExtraDataSection = TLSTLVSection;
201 void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) {
204 Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
205 ELF::SHF_WRITE |ELF::SHF_ALLOC,
206 SectionKind::getBSS());
209 Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
212 SectionKind::getText());
215 Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
216 ELF::SHF_WRITE |ELF::SHF_ALLOC,
217 SectionKind::getDataRel());
220 Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS,
222 SectionKind::getReadOnly());
225 Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
226 ELF::SHF_ALLOC | ELF::SHF_TLS |
228 SectionKind::getThreadData());
231 Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
232 ELF::SHF_ALLOC | ELF::SHF_TLS |
234 SectionKind::getThreadBSS());
237 Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS,
238 ELF::SHF_ALLOC |ELF::SHF_WRITE,
239 SectionKind::getDataRel());
241 DataRelLocalSection =
242 Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
243 ELF::SHF_ALLOC |ELF::SHF_WRITE,
244 SectionKind::getDataRelLocal());
247 Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
248 ELF::SHF_ALLOC |ELF::SHF_WRITE,
249 SectionKind::getReadOnlyWithRel());
251 DataRelROLocalSection =
252 Ctx->getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
253 ELF::SHF_ALLOC |ELF::SHF_WRITE,
254 SectionKind::getReadOnlyWithRelLocal());
256 MergeableConst4Section =
257 Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
258 ELF::SHF_ALLOC |ELF::SHF_MERGE,
259 SectionKind::getMergeableConst4());
261 MergeableConst8Section =
262 Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
263 ELF::SHF_ALLOC |ELF::SHF_MERGE,
264 SectionKind::getMergeableConst8());
266 MergeableConst16Section =
267 Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
268 ELF::SHF_ALLOC |ELF::SHF_MERGE,
269 SectionKind::getMergeableConst16());
272 Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS,
273 ELF::SHF_ALLOC |ELF::SHF_WRITE,
274 SectionKind::getDataRel());
277 Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS,
278 ELF::SHF_ALLOC |ELF::SHF_WRITE,
279 SectionKind::getDataRel());
281 // Exception Handling Sections.
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.
288 Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
290 SectionKind::getReadOnly());
292 // Debug Info Sections.
294 Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
295 SectionKind::getMetadata());
297 Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
298 SectionKind::getMetadata());
300 Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
301 SectionKind::getMetadata());
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());
312 Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS, 0,
313 SectionKind::getMetadata());
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());
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());
329 void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) {
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());
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());
344 Ctx->getCOFFSection(".rdata",
345 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
346 COFF::IMAGE_SCN_MEM_READ,
347 SectionKind::getReadOnly());
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());
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());
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.
366 Ctx->getCOFFSection(".gcc_except_table",
367 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
368 COFF::IMAGE_SCN_MEM_READ,
369 SectionKind::getReadOnly());
373 Ctx->getCOFFSection(".debug_abbrev",
374 COFF::IMAGE_SCN_MEM_DISCARDABLE |
375 COFF::IMAGE_SCN_MEM_READ,
376 SectionKind::getMetadata());
378 Ctx->getCOFFSection(".debug_info",
379 COFF::IMAGE_SCN_MEM_DISCARDABLE |
380 COFF::IMAGE_SCN_MEM_READ,
381 SectionKind::getMetadata());
383 Ctx->getCOFFSection(".debug_line",
384 COFF::IMAGE_SCN_MEM_DISCARDABLE |
385 COFF::IMAGE_SCN_MEM_READ,
386 SectionKind::getMetadata());
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());
403 Ctx->getCOFFSection(".debug_str",
404 COFF::IMAGE_SCN_MEM_DISCARDABLE |
405 COFF::IMAGE_SCN_MEM_READ,
406 SectionKind::getMetadata());
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());
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());
429 Ctx->getCOFFSection(".drectve",
430 COFF::IMAGE_SCN_LNK_INFO,
431 SectionKind::getMetadata());
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());
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());
448 void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model relocm,
454 CommDirectiveSupportsAlignment = true;
455 SupportsWeakOmittedEHFrame = true;
456 IsFunctionEHFrameSymbolPrivate = true;
459 Triple::ArchType Arch = T.getArch();
460 // FIXME: Checking for Arch here to filter out bogus triples such as
461 // cellspu-apple-darwin. Perhaps we should fix in Triple?
462 if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
463 Arch == Triple::arm || Arch == Triple::thumb ||
464 Arch == Triple::ppc || Arch == Triple::ppc64 ||
465 Arch == Triple::UnknownArch) &&
466 (T.isOSDarwin() || T.getEnvironment() == Triple::MachO)) {
468 InitMachOMCObjectFileInfo(T);
469 } else if (T.getOS() == Triple::MinGW32 || T.getOS() == Triple::Cygwin ||
470 T.getOS() == Triple::Win32) {
472 InitCOFFMCObjectFileInfo(T);
475 InitELFMCObjectFileInfo(T);
479 void MCObjectFileInfo::InitEHFrameSection() {
482 Ctx->getMachOSection("__TEXT", "__eh_frame",
483 MCSectionMachO::S_COALESCED |
484 MCSectionMachO::S_ATTR_NO_TOC |
485 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
486 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
487 SectionKind::getReadOnly());
488 else if (Env == IsELF)
490 Ctx->getELFSection(".eh_frame", ELF::SHT_PROGBITS,
492 SectionKind::getDataRel());
495 Ctx->getCOFFSection(".eh_frame",
496 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
497 COFF::IMAGE_SCN_MEM_READ |
498 COFF::IMAGE_SCN_MEM_WRITE,
499 SectionKind::getDataRel());