1 //===- lib/MC/MCSectionCOFF.cpp - COFF Code Section Representation --------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCSectionCOFF.h"
10 #include "llvm/BinaryFormat/COFF.h"
11 #include "llvm/MC/MCSymbol.h"
12 #include "llvm/Support/raw_ostream.h"
17 // ShouldOmitSectionDirective - Decides whether a '.section' directive
18 // should be printed before the section name
19 bool MCSectionCOFF::ShouldOmitSectionDirective(StringRef Name
,
20 const MCAsmInfo
&MAI
) const {
24 // FIXME: Does .section .bss/.data/.text work everywhere??
25 if (Name
== ".text" || Name
== ".data" || Name
== ".bss")
31 void MCSectionCOFF::setSelection(int Selection
) const {
32 assert(Selection
!= 0 && "invalid COMDAT selection type");
33 this->Selection
= Selection
;
34 Characteristics
|= COFF::IMAGE_SCN_LNK_COMDAT
;
37 void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo
&MAI
, const Triple
&T
,
39 const MCExpr
*Subsection
) const {
40 // standard sections don't require the '.section'
41 if (ShouldOmitSectionDirective(SectionName
, MAI
)) {
42 OS
<< '\t' << getSectionName() << '\n';
46 OS
<< "\t.section\t" << getSectionName() << ",\"";
47 if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
)
49 if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
51 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE
)
53 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE
)
55 else if (getCharacteristics() & COFF::IMAGE_SCN_MEM_READ
)
59 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE
)
61 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED
)
63 if ((getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE
) &&
64 !isImplicitlyDiscardable(SectionName
))
68 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT
) {
72 OS
<< "\n\t.linkonce\t";
74 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES
:
77 case COFF::IMAGE_COMDAT_SELECT_ANY
:
80 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE
:
83 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH
:
84 OS
<< "same_contents";
86 case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
:
89 case COFF::IMAGE_COMDAT_SELECT_LARGEST
:
92 case COFF::IMAGE_COMDAT_SELECT_NEWEST
:
96 assert(false && "unsupported COFF selection type");
101 COMDATSymbol
->print(OS
, &MAI
);
107 bool MCSectionCOFF::UseCodeAlign() const {
108 return getKind().isText();
111 bool MCSectionCOFF::isVirtualSection() const {
112 return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
;