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 StringRef name
= getSectionName();
43 if (name
.find_first_not_of("0123456789_."
44 "abcdefghijklmnopqrstuvwxyz"
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == name
.npos
) {
46 OS
<< "\t.section\t" << name
;
48 OS
<< "\t.section\t\"";
49 for (const char *b
= name
.begin(), *e
= name
.end(); b
< e
; ++b
) {
50 if (*b
== '"') // Unquoted "
52 else if (*b
!= '\\') // Neither " or backslash
54 else if (b
+ 1 == e
) // Trailing backslash
57 OS
<< b
[0] << b
[1]; // Quoted character
64 // Handle the weird solaris syntax if desired.
65 if (MAI
.usesSunStyleELFSectionSwitchSyntax() &&
66 !(Flags
& ELF::SHF_MERGE
)) {
67 if (Flags
& ELF::SHF_ALLOC
)
69 if (Flags
& ELF::SHF_EXECINSTR
)
71 if (Flags
& ELF::SHF_WRITE
)
73 if (Flags
& ELF::SHF_TLS
)
80 if (Flags
& ELF::SHF_ALLOC
)
82 if (Flags
& ELF::SHF_EXECINSTR
)
84 if (Flags
& ELF::SHF_GROUP
)
86 if (Flags
& ELF::SHF_WRITE
)
88 if (Flags
& ELF::SHF_MERGE
)
90 if (Flags
& ELF::SHF_STRINGS
)
92 if (Flags
& ELF::SHF_TLS
)
95 // If there are target-specific flags, print them.
96 if (Flags
& ELF::XCORE_SHF_CP_SECTION
)
98 if (Flags
& ELF::XCORE_SHF_DP_SECTION
)
105 // If comment string is '@', e.g. as on ARM - use '%' instead
106 if (MAI
.getCommentString()[0] == '@')
111 if (Type
== ELF::SHT_INIT_ARRAY
)
113 else if (Type
== ELF::SHT_FINI_ARRAY
)
115 else if (Type
== ELF::SHT_PREINIT_ARRAY
)
116 OS
<< "preinit_array";
117 else if (Type
== ELF::SHT_NOBITS
)
119 else if (Type
== ELF::SHT_NOTE
)
121 else if (Type
== ELF::SHT_PROGBITS
)
125 assert(Flags
& ELF::SHF_MERGE
);
126 OS
<< "," << EntrySize
;
129 if (Flags
& ELF::SHF_GROUP
)
130 OS
<< "," << Group
->getName() << ",comdat";
134 bool MCSectionELF::UseCodeAlign() const {
135 return getFlags() & ELF::SHF_EXECINSTR
;
138 bool MCSectionELF::isVirtualSection() const {
139 return getType() == ELF::SHT_NOBITS
;
142 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind
) {
143 if (Kind
.isMergeable1ByteCString()) return 1;
144 if (Kind
.isMergeable2ByteCString()) return 2;
145 if (Kind
.isMergeable4ByteCString()) return 4;
146 if (Kind
.isMergeableConst4()) return 4;
147 if (Kind
.isMergeableConst8()) return 8;
148 if (Kind
.isMergeableConst16()) return 16;