make isVirtualSection a virtual method on MCSection. Chris' suggestion.
[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/raw_ostream.h"
15 using namespace llvm;
16
17 MCSectionELF::~MCSectionELF() {} // anchor.
18
19 // ShouldOmitSectionDirective - Decides whether a '.section' directive
20 // should be printed before the section name
21 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
22                                               const MCAsmInfo &MAI) const {
23   
24   // FIXME: Does .section .bss/.data/.text work everywhere??
25   if (Name == ".text" || Name == ".data" ||
26       (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
27     return true;
28
29   return false;
30 }
31
32 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
33                                         raw_ostream &OS) const {
34    
35   if (ShouldOmitSectionDirective(SectionName, MAI)) {
36     OS << '\t' << getSectionName() << '\n';
37     return;
38   }
39
40   OS << "\t.section\t" << getSectionName();
41   
42   // Handle the weird solaris syntax if desired.
43   if (MAI.usesSunStyleELFSectionSwitchSyntax() && 
44       !(Flags & MCSectionELF::SHF_MERGE)) {
45     if (Flags & MCSectionELF::SHF_ALLOC)
46       OS << ",#alloc";
47     if (Flags & MCSectionELF::SHF_EXECINSTR)
48       OS << ",#execinstr";
49     if (Flags & MCSectionELF::SHF_WRITE)
50       OS << ",#write";
51     if (Flags & MCSectionELF::SHF_TLS)
52       OS << ",#tls";
53     OS << '\n';
54     return;
55   }
56   
57   OS << ",\"";
58   if (Flags & MCSectionELF::SHF_ALLOC)
59     OS << 'a';
60   if (Flags & MCSectionELF::SHF_EXECINSTR)
61     OS << 'x';
62   if (Flags & MCSectionELF::SHF_WRITE)
63     OS << 'w';
64   if (Flags & MCSectionELF::SHF_MERGE)
65     OS << 'M';
66   if (Flags & MCSectionELF::SHF_STRINGS)
67     OS << 'S';
68   if (Flags & MCSectionELF::SHF_TLS)
69     OS << 'T';
70   
71   // If there are target-specific flags, print them.
72   if (Flags & MCSectionELF::XCORE_SHF_CP_SECTION)
73     OS << 'c';
74   if (Flags & MCSectionELF::XCORE_SHF_DP_SECTION)
75     OS << 'd';
76   
77   OS << '"';
78
79   OS << ',';
80
81   // If comment string is '@', e.g. as on ARM - use '%' instead
82   if (MAI.getCommentString()[0] == '@')
83     OS << '%';
84   else
85     OS << '@';
86
87   if (Type == MCSectionELF::SHT_INIT_ARRAY)
88     OS << "init_array";
89   else if (Type == MCSectionELF::SHT_FINI_ARRAY)
90     OS << "fini_array";
91   else if (Type == MCSectionELF::SHT_PREINIT_ARRAY)
92     OS << "preinit_array";
93   else if (Type == MCSectionELF::SHT_NOBITS)
94     OS << "nobits";
95   else if (Type == MCSectionELF::SHT_PROGBITS)
96     OS << "progbits";
97
98   if (EntrySize) {
99     assert(Flags & MCSectionELF::SHF_MERGE);
100     OS << "," << EntrySize;
101   }
102
103   OS << '\n';
104 }
105
106 bool MCSectionELF::UseCodeAlign() const {
107   return getFlags() & MCSectionELF::SHF_EXECINSTR;
108 }
109
110 bool MCSectionELF::isVirtualSection() const {
111   return getType() == MCSectionELF::SHT_NOBITS;
112 }
113
114 // HasCommonSymbols - True if this section holds common symbols, this is
115 // indicated on the ELF object file by a symbol with SHN_COMMON section 
116 // header index.
117 bool MCSectionELF::HasCommonSymbols() const {
118   
119   if (StringRef(SectionName).startswith(".gnu.linkonce."))
120     return true;
121
122   return false;
123 }
124
125 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
126   if (Kind.isMergeable1ByteCString()) return 1;
127   if (Kind.isMergeable2ByteCString()) return 2;
128   if (Kind.isMergeable4ByteCString()) return 4;
129   if (Kind.isMergeableConst4())       return 4;
130   if (Kind.isMergeableConst8())       return 8;
131   if (Kind.isMergeableConst16())      return 16;
132   return 0;
133 }