MC: Emit COFF section flags in the "proper" order
[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 (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
51     OS << 'd';
52   if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
53     OS << 'b';
54   if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE)
55     OS << 'x';
56   if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE)
57     OS << 'w';
58   else if (getCharacteristics() & COFF::IMAGE_SCN_MEM_READ)
59     OS << 'r';
60   else
61     OS << 'y';
62   if (getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE)
63     OS << 'n';
64   if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED)
65     OS << 's';
66   OS << '"';
67
68   if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
69     OS << ",";
70     switch (Selection) {
71       case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
72         OS << "one_only,";
73         break;
74       case COFF::IMAGE_COMDAT_SELECT_ANY:
75         OS << "discard,";
76         break;
77       case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
78         OS << "same_size,";
79         break;
80       case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
81         OS << "same_contents,";
82         break;
83       case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
84         OS << "associative,";
85         break;
86       case COFF::IMAGE_COMDAT_SELECT_LARGEST:
87         OS << "largest,";
88         break;
89       case COFF::IMAGE_COMDAT_SELECT_NEWEST:
90         OS << "newest,";
91         break;
92       default:
93         assert (0 && "unsupported COFF selection type");
94         break;
95     }
96     assert(COMDATSymbol);
97     OS << *COMDATSymbol;
98   }
99   OS << '\n';
100 }
101
102 bool MCSectionCOFF::UseCodeAlign() const {
103   return getKind().isText();
104 }
105
106 bool MCSectionCOFF::isVirtualSection() const {
107   return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
108 }