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 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
37 const MCExpr *Subsection) const {
39 if (ShouldOmitSectionDirective(SectionName, MAI)) {
40 OS << '\t' << getSectionName();
42 OS << '\t' << *Subsection;
47 StringRef name = getSectionName();
48 if (name.find_first_not_of("0123456789_."
49 "abcdefghijklmnopqrstuvwxyz"
50 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == name.npos) {
51 OS << "\t.section\t" << name;
53 OS << "\t.section\t\"";
54 for (const char *b = name.begin(), *e = name.end(); b < e; ++b) {
55 if (*b == '"') // Unquoted "
57 else if (*b != '\\') // Neither " or backslash
59 else if (b + 1 == e) // Trailing backslash
62 OS << b[0] << b[1]; // Quoted character
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_TLS)
85 if (Flags & ELF::SHF_ALLOC)
87 if (Flags & ELF::SHF_EXECINSTR)
89 if (Flags & ELF::SHF_GROUP)
91 if (Flags & ELF::SHF_WRITE)
93 if (Flags & ELF::SHF_MERGE)
95 if (Flags & ELF::SHF_STRINGS)
97 if (Flags & ELF::SHF_TLS)
100 // If there are target-specific flags, print them.
101 if (Flags & ELF::XCORE_SHF_CP_SECTION)
103 if (Flags & ELF::XCORE_SHF_DP_SECTION)
110 // If comment string is '@', e.g. as on ARM - use '%' instead
111 if (MAI.getCommentString()[0] == '@')
116 if (Type == ELF::SHT_INIT_ARRAY)
118 else if (Type == ELF::SHT_FINI_ARRAY)
120 else if (Type == ELF::SHT_PREINIT_ARRAY)
121 OS << "preinit_array";
122 else if (Type == ELF::SHT_NOBITS)
124 else if (Type == ELF::SHT_NOTE)
126 else if (Type == ELF::SHT_PROGBITS)
130 assert(Flags & ELF::SHF_MERGE);
131 OS << "," << EntrySize;
134 if (Flags & ELF::SHF_GROUP)
135 OS << "," << Group->getName() << ",comdat";
139 OS << "\t.subsection\t" << *Subsection << '\n';
142 bool MCSectionELF::UseCodeAlign() const {
143 return getFlags() & ELF::SHF_EXECINSTR;
146 bool MCSectionELF::isVirtualSection() const {
147 return getType() == ELF::SHT_NOBITS;
150 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
151 if (Kind.isMergeable1ByteCString()) return 1;
152 if (Kind.isMergeable2ByteCString()) return 2;
153 if (Kind.isMergeable4ByteCString()) return 4;
154 if (Kind.isMergeableConst4()) return 4;
155 if (Kind.isMergeableConst8()) return 8;
156 if (Kind.isMergeableConst16()) return 16;