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/COFF.h"
15 #include "llvm/Support/raw_ostream.h"
16 using namespace llvm;
17 
~MCSectionCOFF()18 MCSectionCOFF::~MCSectionCOFF() {} // anchor.
19 
20 // ShouldOmitSectionDirective - Decides whether a '.section' directive
21 // should be printed before the section name
ShouldOmitSectionDirective(StringRef Name,const MCAsmInfo & MAI) const22 bool MCSectionCOFF::ShouldOmitSectionDirective(StringRef Name,
23                                                const MCAsmInfo &MAI) const {
24   if (COMDATSymbol)
25     return false;
26 
27   // FIXME: Does .section .bss/.data/.text work everywhere??
28   if (Name == ".text" || Name == ".data" || Name == ".bss")
29     return true;
30 
31   return false;
32 }
33 
setSelection(int Selection) const34 void MCSectionCOFF::setSelection(int Selection) const {
35   assert(Selection != 0 && "invalid COMDAT selection type");
36   this->Selection = Selection;
37   Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
38 }
39 
PrintSwitchToSection(const MCAsmInfo & MAI,raw_ostream & OS,const MCExpr * Subsection) const40 void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
41                                          raw_ostream &OS,
42                                          const MCExpr *Subsection) const {
43 
44   // standard sections don't require the '.section'
45   if (ShouldOmitSectionDirective(SectionName, MAI)) {
46     OS << '\t' << getSectionName() << '\n';
47     return;
48   }
49 
50   OS << "\t.section\t" << getSectionName() << ",\"";
51   if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
52     OS << 'd';
53   if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
54     OS << 'b';
55   if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE)
56     OS << 'x';
57   if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE)
58     OS << 'w';
59   else if (getCharacteristics() & COFF::IMAGE_SCN_MEM_READ)
60     OS << 'r';
61   else
62     OS << 'y';
63   if (getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE)
64     OS << 'n';
65   if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED)
66     OS << 's';
67   OS << '"';
68 
69   if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
70     OS << ",";
71     switch (Selection) {
72       case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
73         OS << "one_only,";
74         break;
75       case COFF::IMAGE_COMDAT_SELECT_ANY:
76         OS << "discard,";
77         break;
78       case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
79         OS << "same_size,";
80         break;
81       case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
82         OS << "same_contents,";
83         break;
84       case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
85         OS << "associative,";
86         break;
87       case COFF::IMAGE_COMDAT_SELECT_LARGEST:
88         OS << "largest,";
89         break;
90       case COFF::IMAGE_COMDAT_SELECT_NEWEST:
91         OS << "newest,";
92         break;
93       default:
94         assert (0 && "unsupported COFF selection type");
95         break;
96     }
97     assert(COMDATSymbol);
98     COMDATSymbol->print(OS, &MAI);
99   }
100   OS << '\n';
101 }
102 
UseCodeAlign() const103 bool MCSectionCOFF::UseCodeAlign() const {
104   return getKind().isText();
105 }
106 
isVirtualSection() const107 bool MCSectionCOFF::isVirtualSection() const {
108   return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
109 }
110