Switch llvm to using comdats. For now always use groups with a single
[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   OS << "\t.section\t" << getSectionName();
43   
44   // Handle the weird solaris syntax if desired.
45   if (MAI.usesSunStyleELFSectionSwitchSyntax() && 
46       !(Flags & ELF::SHF_MERGE)) {
47     if (Flags & ELF::SHF_ALLOC)
48       OS << ",#alloc";
49     if (Flags & ELF::SHF_EXECINSTR)
50       OS << ",#execinstr";
51     if (Flags & ELF::SHF_WRITE)
52       OS << ",#write";
53     if (Flags & ELF::SHF_TLS)
54       OS << ",#tls";
55     OS << '\n';
56     return;
57   }
58   
59   OS << ",\"";
60   if (Flags & ELF::SHF_ALLOC)
61     OS << 'a';
62   if (Flags & ELF::SHF_EXECINSTR)
63     OS << 'x';
64   if (Flags & ELF::SHF_GROUP)
65     OS << 'G';
66   if (Flags & ELF::SHF_WRITE)
67     OS << 'w';
68   if (Flags & ELF::SHF_MERGE)
69     OS << 'M';
70   if (Flags & ELF::SHF_STRINGS)
71     OS << 'S';
72   if (Flags & ELF::SHF_TLS)
73     OS << 'T';
74   
75   // If there are target-specific flags, print them.
76   if (Flags & ELF::XCORE_SHF_CP_SECTION)
77     OS << 'c';
78   if (Flags & ELF::XCORE_SHF_DP_SECTION)
79     OS << 'd';
80   
81   OS << '"';
82
83   OS << ',';
84
85   // If comment string is '@', e.g. as on ARM - use '%' instead
86   if (MAI.getCommentString()[0] == '@')
87     OS << '%';
88   else
89     OS << '@';
90
91   if (Type == ELF::SHT_INIT_ARRAY)
92     OS << "init_array";
93   else if (Type == ELF::SHT_FINI_ARRAY)
94     OS << "fini_array";
95   else if (Type == ELF::SHT_PREINIT_ARRAY)
96     OS << "preinit_array";
97   else if (Type == ELF::SHT_NOBITS)
98     OS << "nobits";
99   else if (Type == ELF::SHT_NOTE)
100     OS << "note";
101   else if (Type == ELF::SHT_PROGBITS)
102     OS << "progbits";
103
104   if (EntrySize) {
105     assert(Flags & ELF::SHF_MERGE);
106     OS << "," << EntrySize;
107   }
108
109   if (Flags & ELF::SHF_GROUP)
110     OS << "," << Group->getName() << ",comdat";
111   OS << '\n';
112 }
113
114 bool MCSectionELF::UseCodeAlign() const {
115   return getFlags() & ELF::SHF_EXECINSTR;
116 }
117
118 bool MCSectionELF::isVirtualSection() const {
119   return getType() == ELF::SHT_NOBITS;
120 }
121
122 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
123   if (Kind.isMergeable1ByteCString()) return 1;
124   if (Kind.isMergeable2ByteCString()) return 2;
125   if (Kind.isMergeable4ByteCString()) return 4;
126   if (Kind.isMergeableConst4())       return 4;
127   if (Kind.isMergeableConst8())       return 8;
128   if (Kind.isMergeableConst16())      return 16;
129   return 0;
130 }