1//===- lib/MC/MCSectionCOFF.cpp - COFF Code Section Representation --------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
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"
13#include <cassert>
14
15using namespace llvm;
16
17// shouldOmitSectionDirective - Decides whether a '.section' directive
18// should be printed before the section name
19bool MCSectionCOFF::shouldOmitSectionDirective(StringRef Name,
20 const MCAsmInfo &MAI) const {
21 if (COMDATSymbol || isUnique())
22 return false;
23
24 // FIXME: Does .section .bss/.data/.text work everywhere??
25 if (Name == ".text" || Name == ".data" || Name == ".bss")
26 return true;
27
28 return false;
29}
30
31void MCSectionCOFF::setSelection(int Selection) const {
32 assert(Selection != 0 && "invalid COMDAT selection type");
33 this->Selection = Selection;
34 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
35}
36
37void MCSectionCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
38 raw_ostream &OS,
39 uint32_t Subsection) const {
40 // standard sections don't require the '.section'
41 if (shouldOmitSectionDirective(Name: getName(), MAI)) {
42 OS << '\t' << getName() << '\n';
43 return;
44 }
45
46 OS << "\t.section\t" << getName() << ",\"";
47 if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
48 OS << 'd';
49 if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
50 OS << 'b';
51 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE)
52 OS << 'x';
53 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE)
54 OS << 'w';
55 else if (getCharacteristics() & COFF::IMAGE_SCN_MEM_READ)
56 OS << 'r';
57 else
58 OS << 'y';
59 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE)
60 OS << 'n';
61 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED)
62 OS << 's';
63 if ((getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) &&
64 !isImplicitlyDiscardable(Name: getName()))
65 OS << 'D';
66 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_INFO)
67 OS << 'i';
68 OS << '"';
69
70 // unique should be tail of .section directive.
71 if (isUnique() && !COMDATSymbol)
72 OS << ",unique," << UniqueID;
73
74 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
75 if (COMDATSymbol)
76 OS << ",";
77 else
78 OS << "\n\t.linkonce\t";
79 switch (Selection) {
80 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
81 OS << "one_only";
82 break;
83 case COFF::IMAGE_COMDAT_SELECT_ANY:
84 OS << "discard";
85 break;
86 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
87 OS << "same_size";
88 break;
89 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
90 OS << "same_contents";
91 break;
92 case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
93 OS << "associative";
94 break;
95 case COFF::IMAGE_COMDAT_SELECT_LARGEST:
96 OS << "largest";
97 break;
98 case COFF::IMAGE_COMDAT_SELECT_NEWEST:
99 OS << "newest";
100 break;
101 default:
102 assert(false && "unsupported COFF selection type");
103 break;
104 }
105 if (COMDATSymbol) {
106 OS << ",";
107 COMDATSymbol->print(OS, MAI: &MAI);
108 }
109 }
110
111 if (isUnique() && COMDATSymbol)
112 OS << ",unique," << UniqueID;
113
114 OS << '\n';
115}
116
117bool MCSectionCOFF::useCodeAlign() const { return isText(); }
118
119StringRef MCSectionCOFF::getVirtualSectionKind() const {
120 return "IMAGE_SCN_CNT_UNINITIALIZED_DATA";
121}
122