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/ELF.h"
15 #include "llvm/Support/raw_ostream.h"
19 MCSectionELF::~MCSectionELF() {} // anchor.
21 // ShouldOmitSectionDirective - Decides whether a '.section' directive
22 // should be printed before the section name
23 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name
,
24 const MCAsmInfo
&MAI
) const {
26 // FIXME: Does .section .bss/.data/.text work everywhere??
27 if (Name
== ".text" || Name
== ".data" ||
28 (Name
== ".bss" && !MAI
.usesELFSectionDirectiveForBSS()))
34 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo
&MAI
,
35 raw_ostream
&OS
) const {
37 if (ShouldOmitSectionDirective(SectionName
, MAI
)) {
38 OS
<< '\t' << getSectionName() << '\n';
42 OS
<< "\t.section\t" << getSectionName();
44 // Handle the weird solaris syntax if desired.
45 if (MAI
.usesSunStyleELFSectionSwitchSyntax() &&
46 !(Flags
& ELF::SHF_MERGE
)) {
47 if (Flags
& ELF::SHF_ALLOC
)
49 if (Flags
& ELF::SHF_EXECINSTR
)
51 if (Flags
& ELF::SHF_WRITE
)
53 if (Flags
& ELF::SHF_TLS
)
60 if (Flags
& ELF::SHF_ALLOC
)
62 if (Flags
& ELF::SHF_EXECINSTR
)
64 if (Flags
& ELF::SHF_WRITE
)
66 if (Flags
& ELF::SHF_MERGE
)
68 if (Flags
& ELF::SHF_STRINGS
)
70 if (Flags
& ELF::SHF_TLS
)
73 // If there are target-specific flags, print them.
74 if (Flags
& ELF::XCORE_SHF_CP_SECTION
)
76 if (Flags
& ELF::XCORE_SHF_DP_SECTION
)
83 // If comment string is '@', e.g. as on ARM - use '%' instead
84 if (MAI
.getCommentString()[0] == '@')
89 if (Type
== ELF::SHT_INIT_ARRAY
)
91 else if (Type
== ELF::SHT_FINI_ARRAY
)
93 else if (Type
== ELF::SHT_PREINIT_ARRAY
)
94 OS
<< "preinit_array";
95 else if (Type
== ELF::SHT_NOBITS
)
97 else if (Type
== ELF::SHT_NOTE
)
99 else if (Type
== ELF::SHT_PROGBITS
)
103 assert(Flags
& ELF::SHF_MERGE
);
104 OS
<< "," << EntrySize
;
110 bool MCSectionELF::UseCodeAlign() const {
111 return getFlags() & ELF::SHF_EXECINSTR
;
114 bool MCSectionELF::isVirtualSection() const {
115 return getType() == ELF::SHT_NOBITS
;
118 // HasCommonSymbols - True if this section holds common symbols, this is
119 // indicated on the ELF object file by a symbol with SHN_COMMON section
121 bool MCSectionELF::HasCommonSymbols() const {
123 if (StringRef(SectionName
).startswith(".gnu.linkonce."))
129 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind
) {
130 if (Kind
.isMergeable1ByteCString()) return 1;
131 if (Kind
.isMergeable2ByteCString()) return 2;
132 if (Kind
.isMergeable4ByteCString()) return 4;
133 if (Kind
.isMergeableConst4()) return 4;
134 if (Kind
.isMergeableConst8()) return 8;
135 if (Kind
.isMergeableConst16()) return 16;