1 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
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/MCSectionELF.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "llvm/MC/MCAsmInfo.h"
17 MCSectionELF
*MCSectionELF::
18 Create(const StringRef
&Section
, unsigned Type
, unsigned Flags
,
19 SectionKind K
, bool isExplicit
, MCContext
&Ctx
) {
20 return new (Ctx
) MCSectionELF(Section
, Type
, Flags
, K
, isExplicit
);
23 // ShouldOmitSectionDirective - Decides whether a '.section' directive
24 // should be printed before the section name
25 bool MCSectionELF::ShouldOmitSectionDirective(const char *Name
,
26 const MCAsmInfo
&MAI
) const {
28 // FIXME: Does .section .bss/.data/.text work everywhere??
29 if (strcmp(Name
, ".text") == 0 ||
30 strcmp(Name
, ".data") == 0 ||
31 (strcmp(Name
, ".bss") == 0 &&
32 !MAI
.usesELFSectionDirectiveForBSS()))
38 // ShouldPrintSectionType - Only prints the section type if supported
39 bool MCSectionELF::ShouldPrintSectionType(unsigned Ty
) const {
41 if (IsExplicit
&& !(Ty
== SHT_NOBITS
|| Ty
== SHT_PROGBITS
))
47 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo
&MAI
,
48 raw_ostream
&OS
) const {
50 if (ShouldOmitSectionDirective(SectionName
.c_str(), MAI
)) {
51 OS
<< '\t' << getSectionName() << '\n';
55 OS
<< "\t.section\t" << getSectionName();
57 // Handle the weird solaris syntax if desired.
58 if (MAI
.usesSunStyleELFSectionSwitchSyntax() &&
59 !(Flags
& MCSectionELF::SHF_MERGE
)) {
60 if (Flags
& MCSectionELF::SHF_ALLOC
)
62 if (Flags
& MCSectionELF::SHF_EXECINSTR
)
64 if (Flags
& MCSectionELF::SHF_WRITE
)
66 if (Flags
& MCSectionELF::SHF_TLS
)
70 if (Flags
& MCSectionELF::SHF_ALLOC
)
72 if (Flags
& MCSectionELF::SHF_EXECINSTR
)
74 if (Flags
& MCSectionELF::SHF_WRITE
)
76 if (Flags
& MCSectionELF::SHF_MERGE
)
78 if (Flags
& MCSectionELF::SHF_STRINGS
)
80 if (Flags
& MCSectionELF::SHF_TLS
)
83 // If there are target-specific flags, print them.
84 if (Flags
& ~MCSectionELF::TARGET_INDEP_SHF
)
85 PrintTargetSpecificSectionFlags(MAI
, OS
);
89 if (ShouldPrintSectionType(Type
)) {
92 // If comment string is '@', e.g. as on ARM - use '%' instead
93 if (MAI
.getCommentString()[0] == '@')
98 if (Type
== MCSectionELF::SHT_INIT_ARRAY
)
100 else if (Type
== MCSectionELF::SHT_FINI_ARRAY
)
102 else if (Type
== MCSectionELF::SHT_PREINIT_ARRAY
)
103 OS
<< "preinit_array";
104 else if (Type
== MCSectionELF::SHT_NOBITS
)
106 else if (Type
== MCSectionELF::SHT_PROGBITS
)
109 if (getKind().isMergeable1ByteCString()) {
111 } else if (getKind().isMergeable2ByteCString()) {
113 } else if (getKind().isMergeable4ByteCString() ||
114 getKind().isMergeableConst4()) {
116 } else if (getKind().isMergeableConst8()) {
118 } else if (getKind().isMergeableConst16()) {
127 // HasCommonSymbols - True if this section holds common symbols, this is
128 // indicated on the ELF object file by a symbol with SHN_COMMON section
130 bool MCSectionELF::HasCommonSymbols() const {
132 if (strncmp(SectionName
.c_str(), ".gnu.linkonce.", 14) == 0)