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/MCSymbol.h"
14 #include "llvm/Support/ELF.h"
15 #include "llvm/Support/raw_ostream.h"
19 MCSectionELF::~MCSectionELF() {} // anchor.
21 // ShouldOmitSectionDirective - Decides whether a '.section' directive
22 // should be printed before the section name
23 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
24 const MCAsmInfo &MAI) const {
26 // FIXME: Does .section .bss/.data/.text work everywhere??
27 if (Name == ".text" || Name == ".data" ||
28 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
34 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
35 raw_ostream &OS) const {
37 if (ShouldOmitSectionDirective(SectionName, MAI)) {
38 OS << '\t' << getSectionName() << '\n';
42 StringRef name = getSectionName();
43 OS << "\t.section\t\"";
44 for (const char *b = name.begin(), *e = name.end(); b < e; ++b) {
45 if (*b == '"') // Unquoted "
47 else if (*b != '\\') // Neither " or backslash
49 else if (b + 1 == e) // Trailing backslash
52 OS << b[0] << b[1]; // Quoted character
58 // Handle the weird solaris syntax if desired.
59 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
60 !(Flags & ELF::SHF_MERGE)) {
61 if (Flags & ELF::SHF_ALLOC)
63 if (Flags & ELF::SHF_EXECINSTR)
65 if (Flags & ELF::SHF_WRITE)
67 if (Flags & ELF::SHF_TLS)
74 if (Flags & ELF::SHF_ALLOC)
76 if (Flags & ELF::SHF_EXECINSTR)
78 if (Flags & ELF::SHF_GROUP)
80 if (Flags & ELF::SHF_WRITE)
82 if (Flags & ELF::SHF_MERGE)
84 if (Flags & ELF::SHF_STRINGS)
86 if (Flags & ELF::SHF_TLS)
89 // If there are target-specific flags, print them.
90 if (Flags & ELF::XCORE_SHF_CP_SECTION)
92 if (Flags & ELF::XCORE_SHF_DP_SECTION)
99 // If comment string is '@', e.g. as on ARM - use '%' instead
100 if (MAI.getCommentString()[0] == '@')
105 if (Type == ELF::SHT_INIT_ARRAY)
107 else if (Type == ELF::SHT_FINI_ARRAY)
109 else if (Type == ELF::SHT_PREINIT_ARRAY)
110 OS << "preinit_array";
111 else if (Type == ELF::SHT_NOBITS)
113 else if (Type == ELF::SHT_NOTE)
115 else if (Type == ELF::SHT_PROGBITS)
119 assert(Flags & ELF::SHF_MERGE);
120 OS << "," << EntrySize;
123 if (Flags & ELF::SHF_GROUP)
124 OS << "," << Group->getName() << ",comdat";
128 bool MCSectionELF::UseCodeAlign() const {
129 return getFlags() & ELF::SHF_EXECINSTR;
132 bool MCSectionELF::isVirtualSection() const {
133 return getType() == ELF::SHT_NOBITS;
136 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
137 if (Kind.isMergeable1ByteCString()) return 1;
138 if (Kind.isMergeable2ByteCString()) return 2;
139 if (Kind.isMergeable4ByteCString()) return 4;
140 if (Kind.isMergeableConst4()) return 4;
141 if (Kind.isMergeableConst8()) return 8;
142 if (Kind.isMergeableConst16()) return 16;