Bug#9033: For the ELF assembler output, always quote the section name.
[oota-llvm.git] / lib / MC / MCSectionELF.cpp
1 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
16
17 using namespace llvm;
18
19 MCSectionELF::~MCSectionELF() {} // anchor.
20
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 {
25   
26   // FIXME: Does .section .bss/.data/.text work everywhere??
27   if (Name == ".text" || Name == ".data" ||
28       (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
29     return true;
30
31   return false;
32 }
33
34 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
35                                         raw_ostream &OS) const {
36    
37   if (ShouldOmitSectionDirective(SectionName, MAI)) {
38     OS << '\t' << getSectionName() << '\n';
39     return;
40   }
41
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 "
46       OS << "\\\"";
47     else if (*b != '\\') // Neither " or backslash
48       OS << *b;
49     else if (b + 1 == e) // Trailing backslash
50       OS << "\\\\";
51     else {
52       OS << b[0] << b[1]; // Quoted character
53       ++b;
54     }
55   }
56   OS << '"';
57
58   // Handle the weird solaris syntax if desired.
59   if (MAI.usesSunStyleELFSectionSwitchSyntax() && 
60       !(Flags & ELF::SHF_MERGE)) {
61     if (Flags & ELF::SHF_ALLOC)
62       OS << ",#alloc";
63     if (Flags & ELF::SHF_EXECINSTR)
64       OS << ",#execinstr";
65     if (Flags & ELF::SHF_WRITE)
66       OS << ",#write";
67     if (Flags & ELF::SHF_TLS)
68       OS << ",#tls";
69     OS << '\n';
70     return;
71   }
72   
73   OS << ",\"";
74   if (Flags & ELF::SHF_ALLOC)
75     OS << 'a';
76   if (Flags & ELF::SHF_EXECINSTR)
77     OS << 'x';
78   if (Flags & ELF::SHF_GROUP)
79     OS << 'G';
80   if (Flags & ELF::SHF_WRITE)
81     OS << 'w';
82   if (Flags & ELF::SHF_MERGE)
83     OS << 'M';
84   if (Flags & ELF::SHF_STRINGS)
85     OS << 'S';
86   if (Flags & ELF::SHF_TLS)
87     OS << 'T';
88   
89   // If there are target-specific flags, print them.
90   if (Flags & ELF::XCORE_SHF_CP_SECTION)
91     OS << 'c';
92   if (Flags & ELF::XCORE_SHF_DP_SECTION)
93     OS << 'd';
94   
95   OS << '"';
96
97   OS << ',';
98
99   // If comment string is '@', e.g. as on ARM - use '%' instead
100   if (MAI.getCommentString()[0] == '@')
101     OS << '%';
102   else
103     OS << '@';
104
105   if (Type == ELF::SHT_INIT_ARRAY)
106     OS << "init_array";
107   else if (Type == ELF::SHT_FINI_ARRAY)
108     OS << "fini_array";
109   else if (Type == ELF::SHT_PREINIT_ARRAY)
110     OS << "preinit_array";
111   else if (Type == ELF::SHT_NOBITS)
112     OS << "nobits";
113   else if (Type == ELF::SHT_NOTE)
114     OS << "note";
115   else if (Type == ELF::SHT_PROGBITS)
116     OS << "progbits";
117
118   if (EntrySize) {
119     assert(Flags & ELF::SHF_MERGE);
120     OS << "," << EntrySize;
121   }
122
123   if (Flags & ELF::SHF_GROUP)
124     OS << "," << Group->getName() << ",comdat";
125   OS << '\n';
126 }
127
128 bool MCSectionELF::UseCodeAlign() const {
129   return getFlags() & ELF::SHF_EXECINSTR;
130 }
131
132 bool MCSectionELF::isVirtualSection() const {
133   return getType() == ELF::SHT_NOBITS;
134 }
135
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;
143   return 0;
144 }