12
12
// ===----------------------------------------------------------------------===//
13
13
14
14
#include " llvm/MC/MCAsmInfoELF.h"
15
+ #include " llvm/ADT/Twine.h"
15
16
#include " llvm/BinaryFormat/ELF.h"
17
+ #include " llvm/MC/MCAsmInfo.h"
16
18
#include " llvm/MC/MCContext.h"
19
+ #include " llvm/MC/MCExpr.h"
17
20
#include " llvm/MC/MCSectionELF.h"
21
+ #include " llvm/Support/ErrorHandling.h"
22
+ #include " llvm/Support/raw_ostream.h"
23
+ #include " llvm/TargetParser/Triple.h"
24
+ #include < cassert>
18
25
19
26
using namespace llvm ;
20
27
@@ -38,3 +45,196 @@ MCAsmInfoELF::MCAsmInfoELF() {
38
45
PrivateGlobalPrefix = " .L" ;
39
46
PrivateLabelPrefix = " .L" ;
40
47
}
48
+
49
+ // Decides whether a '.section' directive
50
+ // should be printed before the section name.
51
+ bool MCSectionELF::shouldOmitSectionDirective (StringRef Name,
52
+ const MCAsmInfo &MAI) const {
53
+ if (isUnique ())
54
+ return false ;
55
+
56
+ return MAI.shouldOmitSectionDirective (Name);
57
+ }
58
+
59
+ static void printName (raw_ostream &OS, StringRef Name) {
60
+ if (Name.find_first_not_of (" 0123456789_."
61
+ " abcdefghijklmnopqrstuvwxyz"
62
+ " ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) == Name.npos ) {
63
+ OS << Name;
64
+ return ;
65
+ }
66
+ OS << ' "' ;
67
+ for (const char *B = Name.begin (), *E = Name.end (); B < E; ++B) {
68
+ if (*B == ' "' ) // Unquoted "
69
+ OS << " \\\" " ;
70
+ else if (*B != ' \\ ' ) // Neither " or backslash
71
+ OS << *B;
72
+ else if (B + 1 == E) // Trailing backslash
73
+ OS << " \\\\ " ;
74
+ else {
75
+ OS << B[0 ] << B[1 ]; // Quoted character
76
+ ++B;
77
+ }
78
+ }
79
+ OS << ' "' ;
80
+ }
81
+
82
+ void MCSectionELF::printSwitchToSection (const MCAsmInfo &MAI, const Triple &T,
83
+ raw_ostream &OS,
84
+ uint32_t Subsection) const {
85
+ if (shouldOmitSectionDirective (getName (), MAI)) {
86
+ OS << ' \t ' << getName ();
87
+ if (Subsection)
88
+ OS << ' \t ' << Subsection;
89
+ OS << ' \n ' ;
90
+ return ;
91
+ }
92
+
93
+ OS << " \t .section\t " ;
94
+ printName (OS, getName ());
95
+
96
+ // Handle the weird solaris syntax if desired.
97
+ if (MAI.usesSunStyleELFSectionSwitchSyntax () && !(Flags & ELF::SHF_MERGE)) {
98
+ if (Flags & ELF::SHF_ALLOC)
99
+ OS << " ,#alloc" ;
100
+ if (Flags & ELF::SHF_EXECINSTR)
101
+ OS << " ,#execinstr" ;
102
+ if (Flags & ELF::SHF_WRITE)
103
+ OS << " ,#write" ;
104
+ if (Flags & ELF::SHF_EXCLUDE)
105
+ OS << " ,#exclude" ;
106
+ if (Flags & ELF::SHF_TLS)
107
+ OS << " ,#tls" ;
108
+ OS << ' \n ' ;
109
+ return ;
110
+ }
111
+
112
+ OS << " ,\" " ;
113
+ if (Flags & ELF::SHF_ALLOC)
114
+ OS << ' a' ;
115
+ if (Flags & ELF::SHF_EXCLUDE)
116
+ OS << ' e' ;
117
+ if (Flags & ELF::SHF_EXECINSTR)
118
+ OS << ' x' ;
119
+ if (Flags & ELF::SHF_WRITE)
120
+ OS << ' w' ;
121
+ if (Flags & ELF::SHF_MERGE)
122
+ OS << ' M' ;
123
+ if (Flags & ELF::SHF_STRINGS)
124
+ OS << ' S' ;
125
+ if (Flags & ELF::SHF_TLS)
126
+ OS << ' T' ;
127
+ if (Flags & ELF::SHF_LINK_ORDER)
128
+ OS << ' o' ;
129
+ if (Flags & ELF::SHF_GROUP)
130
+ OS << ' G' ;
131
+ if (Flags & ELF::SHF_GNU_RETAIN)
132
+ OS << ' R' ;
133
+
134
+ // If there are os-specific flags, print them.
135
+ if (T.isOSSolaris ())
136
+ if (Flags & ELF::SHF_SUNW_NODISCARD)
137
+ OS << ' R' ;
138
+
139
+ // If there are target-specific flags, print them.
140
+ Triple::ArchType Arch = T.getArch ();
141
+ if (Arch == Triple::xcore) {
142
+ if (Flags & ELF::XCORE_SHF_CP_SECTION)
143
+ OS << ' c' ;
144
+ if (Flags & ELF::XCORE_SHF_DP_SECTION)
145
+ OS << ' d' ;
146
+ } else if (T.isARM () || T.isThumb ()) {
147
+ if (Flags & ELF::SHF_ARM_PURECODE)
148
+ OS << ' y' ;
149
+ } else if (T.isAArch64 ()) {
150
+ if (Flags & ELF::SHF_AARCH64_PURECODE)
151
+ OS << ' y' ;
152
+ } else if (Arch == Triple::hexagon) {
153
+ if (Flags & ELF::SHF_HEX_GPREL)
154
+ OS << ' s' ;
155
+ } else if (Arch == Triple::x86_64) {
156
+ if (Flags & ELF::SHF_X86_64_LARGE)
157
+ OS << ' l' ;
158
+ }
159
+
160
+ OS << ' "' ;
161
+
162
+ OS << ' ,' ;
163
+
164
+ // If comment string is '@', e.g. as on ARM - use '%' instead
165
+ if (MAI.getCommentString ()[0 ] == ' @' )
166
+ OS << ' %' ;
167
+ else
168
+ OS << ' @' ;
169
+
170
+ if (Type == ELF::SHT_INIT_ARRAY)
171
+ OS << " init_array" ;
172
+ else if (Type == ELF::SHT_FINI_ARRAY)
173
+ OS << " fini_array" ;
174
+ else if (Type == ELF::SHT_PREINIT_ARRAY)
175
+ OS << " preinit_array" ;
176
+ else if (Type == ELF::SHT_NOBITS)
177
+ OS << " nobits" ;
178
+ else if (Type == ELF::SHT_NOTE)
179
+ OS << " note" ;
180
+ else if (Type == ELF::SHT_PROGBITS)
181
+ OS << " progbits" ;
182
+ else if (Type == ELF::SHT_X86_64_UNWIND)
183
+ OS << " unwind" ;
184
+ else if (Type == ELF::SHT_MIPS_DWARF)
185
+ // Print hex value of the flag while we do not have
186
+ // any standard symbolic representation of the flag.
187
+ OS << " 0x7000001e" ;
188
+ else if (Type == ELF::SHT_LLVM_ODRTAB)
189
+ OS << " llvm_odrtab" ;
190
+ else if (Type == ELF::SHT_LLVM_LINKER_OPTIONS)
191
+ OS << " llvm_linker_options" ;
192
+ else if (Type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
193
+ OS << " llvm_call_graph_profile" ;
194
+ else if (Type == ELF::SHT_LLVM_DEPENDENT_LIBRARIES)
195
+ OS << " llvm_dependent_libraries" ;
196
+ else if (Type == ELF::SHT_LLVM_SYMPART)
197
+ OS << " llvm_sympart" ;
198
+ else if (Type == ELF::SHT_LLVM_BB_ADDR_MAP)
199
+ OS << " llvm_bb_addr_map" ;
200
+ else if (Type == ELF::SHT_LLVM_OFFLOADING)
201
+ OS << " llvm_offloading" ;
202
+ else if (Type == ELF::SHT_LLVM_LTO)
203
+ OS << " llvm_lto" ;
204
+ else if (Type == ELF::SHT_LLVM_JT_SIZES)
205
+ OS << " llvm_jt_sizes" ;
206
+ else if (Type == ELF::SHT_LLVM_CFI_JUMP_TABLE)
207
+ OS << " llvm_cfi_jump_table" ;
208
+ else
209
+ OS << " 0x" << Twine::utohexstr (Type);
210
+
211
+ if (EntrySize) {
212
+ assert ((Flags & ELF::SHF_MERGE) || Type == ELF::SHT_LLVM_CFI_JUMP_TABLE);
213
+ OS << " ," << EntrySize;
214
+ }
215
+
216
+ if (Flags & ELF::SHF_LINK_ORDER) {
217
+ OS << " ," ;
218
+ if (LinkedToSym)
219
+ printName (OS, LinkedToSym->getName ());
220
+ else
221
+ OS << ' 0' ;
222
+ }
223
+
224
+ if (Flags & ELF::SHF_GROUP) {
225
+ OS << " ," ;
226
+ printName (OS, Group.getPointer ()->getName ());
227
+ if (isComdat ())
228
+ OS << " ,comdat" ;
229
+ }
230
+
231
+ if (isUnique ())
232
+ OS << " ,unique," << UniqueID;
233
+
234
+ OS << ' \n ' ;
235
+
236
+ if (Subsection) {
237
+ OS << " \t .subsection\t " << Subsection;
238
+ OS << ' \n ' ;
239
+ }
240
+ }
0 commit comments