Move broken HasCommonSymbols to ELFWriter.cpp.
[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_WRITE)
65     OS << 'w';
66   if (Flags & ELF::SHF_MERGE)
67     OS << 'M';
68   if (Flags & ELF::SHF_STRINGS)
69     OS << 'S';
70   if (Flags & ELF::SHF_TLS)
71     OS << 'T';
72   
73   // If there are target-specific flags, print them.
74   if (Flags & ELF::XCORE_SHF_CP_SECTION)
75     OS << 'c';
76   if (Flags & ELF::XCORE_SHF_DP_SECTION)
77     OS << 'd';
78   
79   OS << '"';
80
81   OS << ',';
82
83   // If comment string is '@', e.g. as on ARM - use '%' instead
84   if (MAI.getCommentString()[0] == '@')
85     OS << '%';
86   else
87     OS << '@';
88
89   if (Type == ELF::SHT_INIT_ARRAY)
90     OS << "init_array";
91   else if (Type == ELF::SHT_FINI_ARRAY)
92     OS << "fini_array";
93   else if (Type == ELF::SHT_PREINIT_ARRAY)
94     OS << "preinit_array";
95   else if (Type == ELF::SHT_NOBITS)
96     OS << "nobits";
97   else if (Type == ELF::SHT_NOTE)
98     OS << "note";
99   else if (Type == ELF::SHT_PROGBITS)
100     OS << "progbits";
101
102   if (EntrySize) {
103     assert(Flags & ELF::SHF_MERGE);
104     OS << "," << EntrySize;
105   }
106
107   OS << '\n';
108 }
109
110 bool MCSectionELF::UseCodeAlign() const {
111   return getFlags() & ELF::SHF_EXECINSTR;
112 }
113
114 bool MCSectionELF::isVirtualSection() const {
115   return getType() == ELF::SHT_NOBITS;
116 }
117
118 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
119   if (Kind.isMergeable1ByteCString()) return 1;
120   if (Kind.isMergeable2ByteCString()) return 2;
121   if (Kind.isMergeable4ByteCString()) return 4;
122   if (Kind.isMergeableConst4())       return 4;
123   if (Kind.isMergeableConst8())       return 8;
124   if (Kind.isMergeableConst16())      return 16;
125   return 0;
126 }