1 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCSectionELF.h"
11 #include "llvm/ADT/Triple.h"
12 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
21 MCSectionELF::~MCSectionELF() = default; // anchor.
23 // Decides whether a '.section' directive
24 // should be printed before the section name.
25 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
26 const MCAsmInfo &MAI) const {
30 return MAI.shouldOmitSectionDirective(Name);
33 static void printName(raw_ostream &OS, StringRef Name) {
34 if (Name.find_first_not_of("0123456789_."
35 "abcdefghijklmnopqrstuvwxyz"
36 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
41 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
42 if (*B == '"') // Unquoted "
44 else if (*B != '\\') // Neither " or backslash
46 else if (B + 1 == E) // Trailing backslash
49 OS << B[0] << B[1]; // Quoted character
56 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
58 const MCExpr *Subsection) const {
59 if (ShouldOmitSectionDirective(SectionName, MAI)) {
60 OS << '\t' << getSectionName();
63 Subsection->print(OS, &MAI);
70 printName(OS, getSectionName());
72 // Handle the weird solaris syntax if desired.
73 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
74 !(Flags & ELF::SHF_MERGE)) {
75 if (Flags & ELF::SHF_ALLOC)
77 if (Flags & ELF::SHF_EXECINSTR)
79 if (Flags & ELF::SHF_WRITE)
81 if (Flags & ELF::SHF_EXCLUDE)
83 if (Flags & ELF::SHF_TLS)
90 if (Flags & ELF::SHF_ALLOC)
92 if (Flags & ELF::SHF_EXCLUDE)
94 if (Flags & ELF::SHF_EXECINSTR)
96 if (Flags & ELF::SHF_GROUP)
98 if (Flags & ELF::SHF_WRITE)
100 if (Flags & ELF::SHF_MERGE)
102 if (Flags & ELF::SHF_STRINGS)
104 if (Flags & ELF::SHF_TLS)
106 if (Flags & ELF::SHF_LINK_ORDER)
109 // If there are target-specific flags, print them.
110 Triple::ArchType Arch = T.getArch();
111 if (Arch == Triple::xcore) {
112 if (Flags & ELF::XCORE_SHF_CP_SECTION)
114 if (Flags & ELF::XCORE_SHF_DP_SECTION)
116 } else if (Arch == Triple::arm || Arch == Triple::armeb ||
117 Arch == Triple::thumb || Arch == Triple::thumbeb) {
118 if (Flags & ELF::SHF_ARM_PURECODE)
126 // If comment string is '@', e.g. as on ARM - use '%' instead
127 if (MAI.getCommentString()[0] == '@')
132 if (Type == ELF::SHT_INIT_ARRAY)
134 else if (Type == ELF::SHT_FINI_ARRAY)
136 else if (Type == ELF::SHT_PREINIT_ARRAY)
137 OS << "preinit_array";
138 else if (Type == ELF::SHT_NOBITS)
140 else if (Type == ELF::SHT_NOTE)
142 else if (Type == ELF::SHT_PROGBITS)
144 else if (Type == ELF::SHT_X86_64_UNWIND)
146 else if (Type == ELF::SHT_MIPS_DWARF)
147 // Print hex value of the flag while we do not have
148 // any standard symbolic representation of the flag.
150 else if (Type == ELF::SHT_LLVM_ODRTAB)
153 report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
154 " for section " + getSectionName());
157 assert(Flags & ELF::SHF_MERGE);
158 OS << "," << EntrySize;
161 if (Flags & ELF::SHF_GROUP) {
163 printName(OS, Group->getName());
167 if (Flags & ELF::SHF_LINK_ORDER) {
168 assert(AssociatedSymbol);
170 printName(OS, AssociatedSymbol->getName());
174 OS << ",unique," << UniqueID;
179 OS << "\t.subsection\t";
180 Subsection->print(OS, &MAI);
185 bool MCSectionELF::UseCodeAlign() const {
186 return getFlags() & ELF::SHF_EXECINSTR;
189 bool MCSectionELF::isVirtualSection() const {
190 return getType() == ELF::SHT_NOBITS;