1 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCSectionELF.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/Support/ELF.h"
16 #include "llvm/Support/raw_ostream.h"
17
18 using namespace llvm;
19
~MCSectionELF()20 MCSectionELF::~MCSectionELF() {} // anchor.
21
22 // Decides whether a '.section' directive
23 // should be printed before the section name.
ShouldOmitSectionDirective(StringRef Name,const MCAsmInfo & MAI) const24 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
25 const MCAsmInfo &MAI) const {
26
27 if (isUnique())
28 return false;
29
30 // FIXME: Does .section .bss/.data/.text work everywhere??
31 if (Name == ".text" || Name == ".data" ||
32 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
33 return true;
34
35 return false;
36 }
37
printName(raw_ostream & OS,StringRef Name)38 static void printName(raw_ostream &OS, StringRef Name) {
39 if (Name.find_first_not_of("0123456789_."
40 "abcdefghijklmnopqrstuvwxyz"
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
42 OS << Name;
43 return;
44 }
45 OS << '"';
46 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
47 if (*B == '"') // Unquoted "
48 OS << "\\\"";
49 else if (*B != '\\') // Neither " or backslash
50 OS << *B;
51 else if (B + 1 == E) // Trailing backslash
52 OS << "\\\\";
53 else {
54 OS << B[0] << B[1]; // Quoted character
55 ++B;
56 }
57 }
58 OS << '"';
59 }
60
PrintSwitchToSection(const MCAsmInfo & MAI,raw_ostream & OS,const MCExpr * Subsection) const61 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
62 raw_ostream &OS,
63 const MCExpr *Subsection) const {
64
65 if (ShouldOmitSectionDirective(SectionName, MAI)) {
66 OS << '\t' << getSectionName();
67 if (Subsection) {
68 OS << '\t';
69 Subsection->print(OS, &MAI);
70 }
71 OS << '\n';
72 return;
73 }
74
75 OS << "\t.section\t";
76 printName(OS, getSectionName());
77
78 // Handle the weird solaris syntax if desired.
79 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
80 !(Flags & ELF::SHF_MERGE)) {
81 if (Flags & ELF::SHF_ALLOC)
82 OS << ",#alloc";
83 if (Flags & ELF::SHF_EXECINSTR)
84 OS << ",#execinstr";
85 if (Flags & ELF::SHF_WRITE)
86 OS << ",#write";
87 if (Flags & ELF::SHF_EXCLUDE)
88 OS << ",#exclude";
89 if (Flags & ELF::SHF_TLS)
90 OS << ",#tls";
91 OS << '\n';
92 return;
93 }
94
95 OS << ",\"";
96 if (Flags & ELF::SHF_ALLOC)
97 OS << 'a';
98 if (Flags & ELF::SHF_EXCLUDE)
99 OS << 'e';
100 if (Flags & ELF::SHF_EXECINSTR)
101 OS << 'x';
102 if (Flags & ELF::SHF_GROUP)
103 OS << 'G';
104 if (Flags & ELF::SHF_WRITE)
105 OS << 'w';
106 if (Flags & ELF::SHF_MERGE)
107 OS << 'M';
108 if (Flags & ELF::SHF_STRINGS)
109 OS << 'S';
110 if (Flags & ELF::SHF_TLS)
111 OS << 'T';
112
113 // If there are target-specific flags, print them.
114 if (Flags & ELF::XCORE_SHF_CP_SECTION)
115 OS << 'c';
116 if (Flags & ELF::XCORE_SHF_DP_SECTION)
117 OS << 'd';
118
119 OS << '"';
120
121 OS << ',';
122
123 // If comment string is '@', e.g. as on ARM - use '%' instead
124 if (MAI.getCommentString()[0] == '@')
125 OS << '%';
126 else
127 OS << '@';
128
129 if (Type == ELF::SHT_INIT_ARRAY)
130 OS << "init_array";
131 else if (Type == ELF::SHT_FINI_ARRAY)
132 OS << "fini_array";
133 else if (Type == ELF::SHT_PREINIT_ARRAY)
134 OS << "preinit_array";
135 else if (Type == ELF::SHT_NOBITS)
136 OS << "nobits";
137 else if (Type == ELF::SHT_NOTE)
138 OS << "note";
139 else if (Type == ELF::SHT_PROGBITS)
140 OS << "progbits";
141
142 if (EntrySize) {
143 assert(Flags & ELF::SHF_MERGE);
144 OS << "," << EntrySize;
145 }
146
147 if (Flags & ELF::SHF_GROUP) {
148 OS << ",";
149 printName(OS, Group->getName());
150 OS << ",comdat";
151 }
152
153 if (isUnique())
154 OS << ",unique," << UniqueID;
155
156 OS << '\n';
157
158 if (Subsection) {
159 OS << "\t.subsection\t";
160 Subsection->print(OS, &MAI);
161 OS << '\n';
162 }
163 }
164
UseCodeAlign() const165 bool MCSectionELF::UseCodeAlign() const {
166 return getFlags() & ELF::SHF_EXECINSTR;
167 }
168
isVirtualSection() const169 bool MCSectionELF::isVirtualSection() const {
170 return getType() == ELF::SHT_NOBITS;
171 }
172