Fix a few issues with comdat handling 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) const {
34   assert(Selection != 0 && "invalid COMDAT selection type");
35   this->Selection = Selection;
36   Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
37 }
38
39 void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
40                                          raw_ostream &OS,
41                                          const MCExpr *Subsection) const {
42
43   // standard sections don't require the '.section'
44   if (ShouldOmitSectionDirective(SectionName, MAI)) {
45     OS << '\t' << getSectionName() << '\n';
46     return;
47   }
48
49   OS << "\t.section\t" << getSectionName() << ",\"";
50   if (getKind().isText())
51     OS << 'x';
52   else if (getKind().isBSS())
53     OS << 'b';
54   if (getKind().isWriteable())
55     OS << 'w';
56   else
57     OS << 'r';
58   if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE)
59     OS << 'n';
60   if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
61     OS << 'd';
62   OS << '"';
63
64   if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
65     OS << ",";
66     switch (Selection) {
67       case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
68         OS << "one_only,";
69         break;
70       case COFF::IMAGE_COMDAT_SELECT_ANY:
71         OS << "discard,";
72         break;
73       case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
74         OS << "same_size,";
75         break;
76       case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
77         OS << "same_contents,";
78         break;
79       case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
80         OS << "associative,";
81         break;
82       case COFF::IMAGE_COMDAT_SELECT_LARGEST:
83         OS << "largest,";
84         break;
85       case COFF::IMAGE_COMDAT_SELECT_NEWEST:
86         OS << "newest,";
87         break;
88       default:
89         assert (0 && "unsupported COFF selection type");
90         break;
91     }
92     assert(COMDATSymbol);
93     OS << *COMDATSymbol;
94   }
95   OS << '\n';
96 }
97
98 bool MCSectionCOFF::UseCodeAlign() const {
99   return getKind().isText();
100 }
101
102 bool MCSectionCOFF::isVirtualSection() const {
103   return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
104 }