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/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCSymbol.h"
14 #include "llvm/Support/raw_ostream.h"
17 MCSectionELF::~MCSectionELF() {} // anchor.
19 // ShouldOmitSectionDirective - Decides whether a '.section' directive
20 // should be printed before the section name
21 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name
,
22 const MCAsmInfo
&MAI
) const {
24 // FIXME: Does .section .bss/.data/.text work everywhere??
25 if (Name
== ".text" || Name
== ".data" ||
26 (Name
== ".bss" && !MAI
.usesELFSectionDirectiveForBSS()))
32 // ShouldPrintSectionType - Only prints the section type if supported
33 bool MCSectionELF::ShouldPrintSectionType(unsigned Ty
) const {
34 if (IsExplicit
&& !(Ty
== SHT_NOBITS
|| Ty
== SHT_PROGBITS
))
40 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo
&MAI
,
41 raw_ostream
&OS
) const {
43 if (ShouldOmitSectionDirective(SectionName
, MAI
)) {
44 OS
<< '\t' << getSectionName() << '\n';
48 OS
<< "\t.section\t" << getSectionName();
50 // Handle the weird solaris syntax if desired.
51 if (MAI
.usesSunStyleELFSectionSwitchSyntax() &&
52 !(Flags
& MCSectionELF::SHF_MERGE
)) {
53 if (Flags
& MCSectionELF::SHF_ALLOC
)
55 if (Flags
& MCSectionELF::SHF_EXECINSTR
)
57 if (Flags
& MCSectionELF::SHF_WRITE
)
59 if (Flags
& MCSectionELF::SHF_TLS
)
66 if (Flags
& MCSectionELF::SHF_ALLOC
)
68 if (Flags
& MCSectionELF::SHF_EXECINSTR
)
70 if (Flags
& MCSectionELF::SHF_WRITE
)
72 if (Flags
& MCSectionELF::SHF_MERGE
)
74 if (Flags
& MCSectionELF::SHF_STRINGS
)
76 if (Flags
& MCSectionELF::SHF_TLS
)
79 // If there are target-specific flags, print them.
80 if (Flags
& MCSectionELF::XCORE_SHF_CP_SECTION
)
82 if (Flags
& MCSectionELF::XCORE_SHF_DP_SECTION
)
87 if (ShouldPrintSectionType(Type
)) {
90 // If comment string is '@', e.g. as on ARM - use '%' instead
91 if (MAI
.getCommentString()[0] == '@')
96 if (Type
== MCSectionELF::SHT_INIT_ARRAY
)
98 else if (Type
== MCSectionELF::SHT_FINI_ARRAY
)
100 else if (Type
== MCSectionELF::SHT_PREINIT_ARRAY
)
101 OS
<< "preinit_array";
102 else if (Type
== MCSectionELF::SHT_NOBITS
)
104 else if (Type
== MCSectionELF::SHT_PROGBITS
)
108 OS
<< "," << EntrySize
;
115 bool MCSectionELF::UseCodeAlign() const {
116 return getFlags() & MCSectionELF::SHF_EXECINSTR
;
119 // HasCommonSymbols - True if this section holds common symbols, this is
120 // indicated on the ELF object file by a symbol with SHN_COMMON section
122 bool MCSectionELF::HasCommonSymbols() const {
124 if (StringRef(SectionName
).startswith(".gnu.linkonce."))
130 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind
) {
131 if (Kind
.isMergeable1ByteCString()) return 1;
132 if (Kind
.isMergeable2ByteCString()) return 2;
133 if (Kind
.isMergeable4ByteCString()) return 4;
134 if (Kind
.isMergeableConst4()) return 4;
135 if (Kind
.isMergeableConst8()) return 8;
136 if (Kind
.isMergeableConst16()) return 16;