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/ADT/Triple.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/raw_ostream.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 const MCExpr
*Subsection
) const {
56 if (ShouldOmitSectionDirective(SectionName
, MAI
)) {
57 OS
<< '\t' << getSectionName();
60 Subsection
->print(OS
, &MAI
);
67 printName(OS
, getSectionName());
69 // Handle the weird solaris syntax if desired.
70 if (MAI
.usesSunStyleELFSectionSwitchSyntax() &&
71 !(Flags
& ELF::SHF_MERGE
)) {
72 if (Flags
& ELF::SHF_ALLOC
)
74 if (Flags
& ELF::SHF_EXECINSTR
)
76 if (Flags
& ELF::SHF_WRITE
)
78 if (Flags
& ELF::SHF_EXCLUDE
)
80 if (Flags
& ELF::SHF_TLS
)
87 if (Flags
& ELF::SHF_ALLOC
)
89 if (Flags
& ELF::SHF_EXCLUDE
)
91 if (Flags
& ELF::SHF_EXECINSTR
)
93 if (Flags
& ELF::SHF_GROUP
)
95 if (Flags
& ELF::SHF_WRITE
)
97 if (Flags
& ELF::SHF_MERGE
)
99 if (Flags
& ELF::SHF_STRINGS
)
101 if (Flags
& ELF::SHF_TLS
)
103 if (Flags
& ELF::SHF_LINK_ORDER
)
106 // If there are target-specific flags, print them.
107 Triple::ArchType Arch
= T
.getArch();
108 if (Arch
== Triple::xcore
) {
109 if (Flags
& ELF::XCORE_SHF_CP_SECTION
)
111 if (Flags
& ELF::XCORE_SHF_DP_SECTION
)
113 } else if (T
.isARM() || T
.isThumb()) {
114 if (Flags
& ELF::SHF_ARM_PURECODE
)
116 } else if (Arch
== Triple::hexagon
) {
117 if (Flags
& ELF::SHF_HEX_GPREL
)
125 // If comment string is '@', e.g. as on ARM - use '%' instead
126 if (MAI
.getCommentString()[0] == '@')
131 if (Type
== ELF::SHT_INIT_ARRAY
)
133 else if (Type
== ELF::SHT_FINI_ARRAY
)
135 else if (Type
== ELF::SHT_PREINIT_ARRAY
)
136 OS
<< "preinit_array";
137 else if (Type
== ELF::SHT_NOBITS
)
139 else if (Type
== ELF::SHT_NOTE
)
141 else if (Type
== ELF::SHT_PROGBITS
)
143 else if (Type
== ELF::SHT_X86_64_UNWIND
)
145 else if (Type
== ELF::SHT_MIPS_DWARF
)
146 // Print hex value of the flag while we do not have
147 // any standard symbolic representation of the flag.
149 else if (Type
== ELF::SHT_LLVM_ODRTAB
)
151 else if (Type
== ELF::SHT_LLVM_LINKER_OPTIONS
)
152 OS
<< "llvm_linker_options";
153 else if (Type
== ELF::SHT_LLVM_CALL_GRAPH_PROFILE
)
154 OS
<< "llvm_call_graph_profile";
155 else if (Type
== ELF::SHT_LLVM_DEPENDENT_LIBRARIES
)
156 OS
<< "llvm_dependent_libraries";
157 else if (Type
== ELF::SHT_LLVM_SYMPART
)
158 OS
<< "llvm_sympart";
160 report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type
) +
161 " for section " + getSectionName());
164 assert(Flags
& ELF::SHF_MERGE
);
165 OS
<< "," << EntrySize
;
168 if (Flags
& ELF::SHF_GROUP
) {
170 printName(OS
, Group
->getName());
174 if (Flags
& ELF::SHF_LINK_ORDER
) {
175 assert(AssociatedSymbol
);
177 printName(OS
, AssociatedSymbol
->getName());
181 OS
<< ",unique," << UniqueID
;
186 OS
<< "\t.subsection\t";
187 Subsection
->print(OS
, &MAI
);
192 bool MCSectionELF::UseCodeAlign() const {
193 return getFlags() & ELF::SHF_EXECINSTR
;
196 bool MCSectionELF::isVirtualSection() const {
197 return getType() == ELF::SHT_NOBITS
;