Add a method to return if the ELF section contains only common symbols!
[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/MCContext.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "llvm/Target/TargetAsmInfo.h"
14
15 using namespace llvm;
16
17 MCSectionELF *MCSectionELF::
18 Create(const StringRef &Section, unsigned Type, unsigned Flags,
19        SectionKind K, bool hasCrazyBSS, bool isExplicit, MCContext &Ctx) {
20   return new 
21     (Ctx) MCSectionELF(Section, Type, Flags, K, hasCrazyBSS, isExplicit);
22 }
23
24 // ShouldOmitSectionDirective - Decides whether a '.section' directive
25 // should be printed before the section name
26 bool MCSectionELF::ShouldOmitSectionDirective(const char *Name) const {
27   
28   // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
29   // FIXME: Does .section .bss/.data/.text work everywhere??
30   if ((!HasCrazyBSS && strncmp(Name, ".bss", 4) == 0) || 
31       strncmp(Name, ".text", 5) == 0 || 
32       strncmp(Name, ".data", 5) == 0)
33     return true;
34
35   return false;
36 }
37
38 // ShouldPrintSectionType - Only prints the section type if supported
39 bool MCSectionELF::ShouldPrintSectionType(unsigned Ty) const {
40   
41   if (IsExplicit && !(Ty == SHT_NOBITS || Ty == SHT_PROGBITS))
42     return false;
43
44   return true;
45 }
46
47 void MCSectionELF::PrintSwitchToSection(const TargetAsmInfo &TAI,
48                                         raw_ostream &OS) const {
49   
50   if (ShouldOmitSectionDirective(SectionName.c_str())) {
51     OS << '\t' << getSectionName() << '\n';
52     return;
53   }
54
55   OS << "\t.section\t" << getSectionName();
56   
57   // Handle the weird solaris syntax if desired.
58   if (TAI.usesSunStyleELFSectionSwitchSyntax() && 
59       !(Flags & MCSectionELF::SHF_MERGE)) {
60     if (Flags & MCSectionELF::SHF_ALLOC)
61       OS << ",#alloc";
62     if (Flags & MCSectionELF::SHF_EXECINSTR)
63       OS << ",#execinstr";
64     if (Flags & MCSectionELF::SHF_WRITE)
65       OS << ",#write";
66     if (Flags & MCSectionELF::SHF_TLS)
67       OS << ",#tls";
68   } else {
69     OS << ",\"";
70   
71     if (Flags & MCSectionELF::SHF_ALLOC)
72       OS << 'a';
73     if (Flags & MCSectionELF::SHF_EXECINSTR)
74       OS << 'x';
75     if (Flags & MCSectionELF::SHF_WRITE)
76       OS << 'w';
77     if (Flags & MCSectionELF::SHF_MERGE)
78       OS << 'M';
79     if (Flags & MCSectionELF::SHF_STRINGS)
80       OS << 'S';
81     if (Flags & MCSectionELF::SHF_TLS)
82       OS << 'T';
83    
84     OS << '"';
85
86     if (ShouldPrintSectionType(Type)) {
87       OS << ',';
88    
89       // If comment string is '@', e.g. as on ARM - use '%' instead
90       if (TAI.getCommentString()[0] == '@')
91         OS << '%';
92       else
93         OS << '@';
94     
95       if (Type == MCSectionELF::SHT_INIT_ARRAY)
96         OS << "init_array";
97       else if (Type == MCSectionELF::SHT_FINI_ARRAY)
98         OS << "fini_array";
99       else if (Type == MCSectionELF::SHT_PREINIT_ARRAY)
100         OS << "preinit_array";
101       else if (Type == MCSectionELF::SHT_NOBITS)
102         OS << "nobits";
103       else if (Type == MCSectionELF::SHT_PROGBITS)
104         OS << "progbits";
105     
106       if (getKind().isMergeable1ByteCString()) {
107         OS << ",1";
108       } else if (getKind().isMergeable2ByteCString()) {
109         OS << ",2";
110       } else if (getKind().isMergeable4ByteCString() || 
111                  getKind().isMergeableConst4()) {
112         OS << ",4";
113       } else if (getKind().isMergeableConst8()) {
114         OS << ",8";
115       } else if (getKind().isMergeableConst16()) {
116         OS << ",16";
117       }
118     }
119   }
120   
121   OS << '\n';
122 }
123
124 // IsCommon - True if this section contains only common symbols
125 bool MCSectionELF::IsCommon() const {
126   
127   if (strncmp(SectionName.c_str(), ".gnu.linkonce.", 14) == 0)
128     return true;
129
130   return false;
131 }
132
133