*try* to use a better name to describe how common symbols are marked on the elf objec...
[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 isExplicit, MCContext &Ctx) {
20   return new 
21     (Ctx) MCSectionELF(Section, Type, Flags, K, 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,
27                                         const TargetAsmInfo &TAI) const {
28   
29   // FIXME: Does .section .bss/.data/.text work everywhere??
30   if (strcmp(Name, ".text") == 0 ||
31       strcmp(Name, ".data") == 0 ||
32       (strcmp(Name, ".bss") == 0 &&
33        !TAI.usesELFSectionDirectiveForBSS())) 
34     return true;
35
36   return false;
37 }
38
39 // ShouldPrintSectionType - Only prints the section type if supported
40 bool MCSectionELF::ShouldPrintSectionType(unsigned Ty) const {
41   
42   if (IsExplicit && !(Ty == SHT_NOBITS || Ty == SHT_PROGBITS))
43     return false;
44
45   return true;
46 }
47
48 void MCSectionELF::PrintSwitchToSection(const TargetAsmInfo &TAI,
49                                         raw_ostream &OS) const {
50    
51   if (ShouldOmitSectionDirective(SectionName.c_str(), TAI)) {
52     OS << '\t' << getSectionName() << '\n';
53     return;
54   }
55
56   OS << "\t.section\t" << getSectionName();
57   
58   // Handle the weird solaris syntax if desired.
59   if (TAI.usesSunStyleELFSectionSwitchSyntax() && 
60       !(Flags & MCSectionELF::SHF_MERGE)) {
61     if (Flags & MCSectionELF::SHF_ALLOC)
62       OS << ",#alloc";
63     if (Flags & MCSectionELF::SHF_EXECINSTR)
64       OS << ",#execinstr";
65     if (Flags & MCSectionELF::SHF_WRITE)
66       OS << ",#write";
67     if (Flags & MCSectionELF::SHF_TLS)
68       OS << ",#tls";
69   } else {
70     OS << ",\"";
71   
72     if (Flags & MCSectionELF::SHF_ALLOC)
73       OS << 'a';
74     if (Flags & MCSectionELF::SHF_EXECINSTR)
75       OS << 'x';
76     if (Flags & MCSectionELF::SHF_WRITE)
77       OS << 'w';
78     if (Flags & MCSectionELF::SHF_MERGE)
79       OS << 'M';
80     if (Flags & MCSectionELF::SHF_STRINGS)
81       OS << 'S';
82     if (Flags & MCSectionELF::SHF_TLS)
83       OS << 'T';
84    
85     OS << '"';
86
87     if (ShouldPrintSectionType(Type)) {
88       OS << ',';
89    
90       // If comment string is '@', e.g. as on ARM - use '%' instead
91       if (TAI.getCommentString()[0] == '@')
92         OS << '%';
93       else
94         OS << '@';
95     
96       if (Type == MCSectionELF::SHT_INIT_ARRAY)
97         OS << "init_array";
98       else if (Type == MCSectionELF::SHT_FINI_ARRAY)
99         OS << "fini_array";
100       else if (Type == MCSectionELF::SHT_PREINIT_ARRAY)
101         OS << "preinit_array";
102       else if (Type == MCSectionELF::SHT_NOBITS)
103         OS << "nobits";
104       else if (Type == MCSectionELF::SHT_PROGBITS)
105         OS << "progbits";
106     
107       if (getKind().isMergeable1ByteCString()) {
108         OS << ",1";
109       } else if (getKind().isMergeable2ByteCString()) {
110         OS << ",2";
111       } else if (getKind().isMergeable4ByteCString() || 
112                  getKind().isMergeableConst4()) {
113         OS << ",4";
114       } else if (getKind().isMergeableConst8()) {
115         OS << ",8";
116       } else if (getKind().isMergeableConst16()) {
117         OS << ",16";
118       }
119     }
120   }
121   
122   OS << '\n';
123 }
124
125 // HasCommonSymbols - True if this section holds common symbols, this is
126 // indicated on the ELF object file by a symbol with SHN_COMMON section 
127 // header index.
128 bool MCSectionELF::HasCommonSymbols() const {
129   
130   if (strncmp(SectionName.c_str(), ".gnu.linkonce.", 14) == 0)
131     return true;
132
133   return false;
134 }
135
136