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