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/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"
20 MCSectionELF::~MCSectionELF() {} // anchor.
22 // ShouldOmitSectionDirective - Decides whether a '.section' directive
23 // should be printed before the section name
24 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
25 const MCAsmInfo &MAI) const {
27 // FIXME: Does .section .bss/.data/.text work everywhere??
28 if (Name == ".text" || Name == ".data" ||
29 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
35 static void printName(raw_ostream &OS, StringRef Name) {
36 if (Name.find_first_not_of("0123456789_."
37 "abcdefghijklmnopqrstuvwxyz"
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
43 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
44 if (*B == '"') // Unquoted "
46 else if (*B != '\\') // Neither " or backslash
48 else if (B + 1 == E) // Trailing backslash
51 OS << B[0] << B[1]; // Quoted character
58 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
60 const MCExpr *Subsection) const {
62 if (ShouldOmitSectionDirective(SectionName, MAI)) {
63 OS << '\t' << getSectionName();
65 OS << '\t' << *Subsection;
71 printName(OS, getSectionName());
73 // Handle the weird solaris syntax if desired.
74 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
75 !(Flags & ELF::SHF_MERGE)) {
76 if (Flags & ELF::SHF_ALLOC)
78 if (Flags & ELF::SHF_EXECINSTR)
80 if (Flags & ELF::SHF_WRITE)
82 if (Flags & ELF::SHF_EXCLUDE)
84 if (Flags & ELF::SHF_TLS)
91 if (Flags & ELF::SHF_ALLOC)
93 if (Flags & ELF::SHF_EXCLUDE)
95 if (Flags & ELF::SHF_EXECINSTR)
97 if (Flags & ELF::SHF_GROUP)
99 if (Flags & ELF::SHF_WRITE)
101 if (Flags & ELF::SHF_MERGE)
103 if (Flags & ELF::SHF_STRINGS)
105 if (Flags & ELF::SHF_TLS)
108 // If there are target-specific flags, print them.
109 if (Flags & ELF::XCORE_SHF_CP_SECTION)
111 if (Flags & ELF::XCORE_SHF_DP_SECTION)
118 // If comment string is '@', e.g. as on ARM - use '%' instead
119 if (MAI.getCommentString()[0] == '@')
124 if (Type == ELF::SHT_INIT_ARRAY)
126 else if (Type == ELF::SHT_FINI_ARRAY)
128 else if (Type == ELF::SHT_PREINIT_ARRAY)
129 OS << "preinit_array";
130 else if (Type == ELF::SHT_NOBITS)
132 else if (Type == ELF::SHT_NOTE)
134 else if (Type == ELF::SHT_PROGBITS)
138 assert(Flags & ELF::SHF_MERGE);
139 OS << "," << EntrySize;
142 if (Flags & ELF::SHF_GROUP) {
144 printName(OS, Group->getName());
150 OS << "\t.subsection\t" << *Subsection << '\n';
153 bool MCSectionELF::UseCodeAlign() const {
154 return getFlags() & ELF::SHF_EXECINSTR;
157 bool MCSectionELF::isVirtualSection() const {
158 return getType() == ELF::SHT_NOBITS;
161 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
162 if (Kind.isMergeable1ByteCString()) return 1;
163 if (Kind.isMergeable2ByteCString()) return 2;
164 if (Kind.isMergeable4ByteCString()) return 4;
165 if (Kind.isMergeableConst4()) return 4;
166 if (Kind.isMergeableConst8()) return 8;
167 if (Kind.isMergeableConst16()) return 16;