1 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCSectionELF.h"
10 #include "llvm/BinaryFormat/ELF.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCExpr.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/TargetParser/Triple.h"
20 // Decides whether a '.section' directive
21 // should be printed before the section name.
22 bool MCSectionELF::shouldOmitSectionDirective(StringRef Name
,
23 const MCAsmInfo
&MAI
) const {
27 return MAI
.shouldOmitSectionDirective(Name
);
30 static void printName(raw_ostream
&OS
, StringRef Name
) {
31 if (Name
.find_first_not_of("0123456789_."
32 "abcdefghijklmnopqrstuvwxyz"
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name
.npos
) {
38 for (const char *B
= Name
.begin(), *E
= Name
.end(); B
< E
; ++B
) {
39 if (*B
== '"') // Unquoted "
41 else if (*B
!= '\\') // Neither " or backslash
43 else if (B
+ 1 == E
) // Trailing backslash
46 OS
<< B
[0] << B
[1]; // Quoted character
53 void MCSectionELF::printSwitchToSection(const MCAsmInfo
&MAI
, const Triple
&T
,
55 uint32_t Subsection
) const {
56 if (shouldOmitSectionDirective(getName(), MAI
)) {
57 OS
<< '\t' << getName();
59 OS
<< '\t' << Subsection
;
65 printName(OS
, getName());
67 // Handle the weird solaris syntax if desired.
68 if (MAI
.usesSunStyleELFSectionSwitchSyntax() &&
69 !(Flags
& ELF::SHF_MERGE
)) {
70 if (Flags
& ELF::SHF_ALLOC
)
72 if (Flags
& ELF::SHF_EXECINSTR
)
74 if (Flags
& ELF::SHF_WRITE
)
76 if (Flags
& ELF::SHF_EXCLUDE
)
78 if (Flags
& ELF::SHF_TLS
)
85 if (Flags
& ELF::SHF_ALLOC
)
87 if (Flags
& ELF::SHF_EXCLUDE
)
89 if (Flags
& ELF::SHF_EXECINSTR
)
91 if (Flags
& ELF::SHF_WRITE
)
93 if (Flags
& ELF::SHF_MERGE
)
95 if (Flags
& ELF::SHF_STRINGS
)
97 if (Flags
& ELF::SHF_TLS
)
99 if (Flags
& ELF::SHF_LINK_ORDER
)
101 if (Flags
& ELF::SHF_GROUP
)
103 if (Flags
& ELF::SHF_GNU_RETAIN
)
106 // If there are os-specific flags, print them.
108 if (Flags
& ELF::SHF_SUNW_NODISCARD
)
111 // If there are target-specific flags, print them.
112 Triple::ArchType Arch
= T
.getArch();
113 if (Arch
== Triple::xcore
) {
114 if (Flags
& ELF::XCORE_SHF_CP_SECTION
)
116 if (Flags
& ELF::XCORE_SHF_DP_SECTION
)
118 } else if (T
.isARM() || T
.isThumb()) {
119 if (Flags
& ELF::SHF_ARM_PURECODE
)
121 } else if (Arch
== Triple::hexagon
) {
122 if (Flags
& ELF::SHF_HEX_GPREL
)
124 } else if (Arch
== Triple::x86_64
) {
125 if (Flags
& ELF::SHF_X86_64_LARGE
)
133 // If comment string is '@', e.g. as on ARM - use '%' instead
134 if (MAI
.getCommentString()[0] == '@')
139 if (Type
== ELF::SHT_INIT_ARRAY
)
141 else if (Type
== ELF::SHT_FINI_ARRAY
)
143 else if (Type
== ELF::SHT_PREINIT_ARRAY
)
144 OS
<< "preinit_array";
145 else if (Type
== ELF::SHT_NOBITS
)
147 else if (Type
== ELF::SHT_NOTE
)
149 else if (Type
== ELF::SHT_PROGBITS
)
151 else if (Type
== ELF::SHT_X86_64_UNWIND
)
153 else if (Type
== ELF::SHT_MIPS_DWARF
)
154 // Print hex value of the flag while we do not have
155 // any standard symbolic representation of the flag.
157 else if (Type
== ELF::SHT_LLVM_ODRTAB
)
159 else if (Type
== ELF::SHT_LLVM_LINKER_OPTIONS
)
160 OS
<< "llvm_linker_options";
161 else if (Type
== ELF::SHT_LLVM_CALL_GRAPH_PROFILE
)
162 OS
<< "llvm_call_graph_profile";
163 else if (Type
== ELF::SHT_LLVM_DEPENDENT_LIBRARIES
)
164 OS
<< "llvm_dependent_libraries";
165 else if (Type
== ELF::SHT_LLVM_SYMPART
)
166 OS
<< "llvm_sympart";
167 else if (Type
== ELF::SHT_LLVM_BB_ADDR_MAP
)
168 OS
<< "llvm_bb_addr_map";
169 else if (Type
== ELF::SHT_LLVM_BB_ADDR_MAP_V0
)
170 OS
<< "llvm_bb_addr_map_v0";
171 else if (Type
== ELF::SHT_LLVM_OFFLOADING
)
172 OS
<< "llvm_offloading";
173 else if (Type
== ELF::SHT_LLVM_LTO
)
176 OS
<< "0x" << Twine::utohexstr(Type
);
179 assert(Flags
& ELF::SHF_MERGE
);
180 OS
<< "," << EntrySize
;
183 if (Flags
& ELF::SHF_LINK_ORDER
) {
186 printName(OS
, LinkedToSym
->getName());
191 if (Flags
& ELF::SHF_GROUP
) {
193 printName(OS
, Group
.getPointer()->getName());
199 OS
<< ",unique," << UniqueID
;
204 OS
<< "\t.subsection\t" << Subsection
;
209 bool MCSectionELF::useCodeAlign() const {
210 return getFlags() & ELF::SHF_EXECINSTR
;
213 StringRef
MCSectionELF::getVirtualSectionKind() const { return "SHT_NOBITS"; }