Use simple section names for COMDAT sections on COFF.
[oota-llvm.git] / lib / MC / MCSectionCOFF.cpp
1 //===- lib/MC/MCSectionCOFF.cpp - COFF 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/MCSectionCOFF.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 MCSectionCOFF::~MCSectionCOFF() {} // anchor.
18
19 // ShouldOmitSectionDirective - Decides whether a '.section' directive
20 // should be printed before the section name
21 bool MCSectionCOFF::ShouldOmitSectionDirective(StringRef Name,
22                                                const MCAsmInfo &MAI) const {
23   if (COMDATSymbol)
24     return false;
25
26   // FIXME: Does .section .bss/.data/.text work everywhere??
27   if (Name == ".text" || Name == ".data" || Name == ".bss")
28     return true;
29
30   return false;
31 }
32
33 void MCSectionCOFF::setSelection(int Selection,
34                                  const MCSectionCOFF *Assoc) const {
35   assert(Selection != 0 && "invalid COMDAT selection type");
36   assert((Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) ==
37          (Assoc != 0) &&
38     "associative COMDAT section must have an associated section");
39   this->Selection = Selection;
40   this->Assoc = Assoc;
41   Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
42 }
43
44 void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
45                                          raw_ostream &OS,
46                                          const MCExpr *Subsection) const {
47
48   // standard sections don't require the '.section'
49   if (ShouldOmitSectionDirective(SectionName, MAI)) {
50     OS << '\t' << getSectionName() << '\n';
51     return;
52   }
53
54   OS << "\t.section\t" << getSectionName() << ",\"";
55   if (getKind().isText())
56     OS << 'x';
57   if (getKind().isWriteable())
58     OS << 'w';
59   else
60     OS << 'r';
61   if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE)
62     OS << 'n';
63
64   OS << '"';
65
66   if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
67     OS << ",";
68     switch (Selection) {
69       case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
70         OS << "one_only,";
71         break;
72       case COFF::IMAGE_COMDAT_SELECT_ANY:
73         OS << "discard,";
74         break;
75       case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
76         OS << "same_size,";
77         break;
78       case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
79         OS << "same_contents,";
80         break;
81       case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
82         OS << "associative " << Assoc->getSectionName() << ",";
83         break;
84       case COFF::IMAGE_COMDAT_SELECT_LARGEST:
85         OS << "largest,";
86         break;
87       case COFF::IMAGE_COMDAT_SELECT_NEWEST:
88         OS << "newest,";
89         break;
90       default:
91         assert (0 && "unsupported COFF selection type");
92         break;
93     }
94     assert(COMDATSymbol);
95     OS << *COMDATSymbol;
96   }
97   OS << '\n';
98 }
99
100 bool MCSectionCOFF::UseCodeAlign() const {
101   return getKind().isText();
102 }
103
104 bool MCSectionCOFF::isVirtualSection() const {
105   return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
106 }