1 //===- lib/MC/MCSectionXCOFF.cpp - XCOFF 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/MCSectionXCOFF.h"
10 #include "llvm/MC/MCAsmInfo.h"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
15 
16 using namespace llvm;
17 
18 MCSectionXCOFF::~MCSectionXCOFF() = default;
19 
20 void MCSectionXCOFF::printCsectDirective(raw_ostream &OS) const {
21   OS << "\t.csect " << QualName->getName() << "," << Log2_32(getAlignment())
22      << '\n';
23 }
24 
25 void MCSectionXCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
26                                           raw_ostream &OS,
27                                           const MCExpr *Subsection) const {
28   if (getKind().isText()) {
29     if (getMappingClass() != XCOFF::XMC_PR)
30       report_fatal_error("Unhandled storage-mapping class for .text csect");
31 
32     printCsectDirective(OS);
33     return;
34   }
35 
36   if (getKind().isReadOnly()) {
37     if (getMappingClass() != XCOFF::XMC_RO)
38       report_fatal_error("Unhandled storage-mapping class for .rodata csect.");
39     printCsectDirective(OS);
40     return;
41   }
42 
43   // Initialized TLS data.
44   if (getKind().isThreadData()) {
45     // We only expect XMC_TL here for initialized TLS data.
46     if (getMappingClass() != XCOFF::XMC_TL)
47       report_fatal_error("Unhandled storage-mapping class for .tdata csect.");
48     printCsectDirective(OS);
49     return;
50   }
51 
52   if (getKind().isData()) {
53     switch (getMappingClass()) {
54     case XCOFF::XMC_RW:
55     case XCOFF::XMC_DS:
56     case XCOFF::XMC_TD:
57       printCsectDirective(OS);
58       break;
59     case XCOFF::XMC_TC:
60     case XCOFF::XMC_TE:
61       break;
62     case XCOFF::XMC_TC0:
63       OS << "\t.toc\n";
64       break;
65     default:
66       report_fatal_error(
67           "Unhandled storage-mapping class for .data csect.");
68     }
69     return;
70   }
71 
72   if (isCsect() && getMappingClass() == XCOFF::XMC_TD) {
73     assert((getKind().isBSSExtern() || getKind().isBSSLocal()) &&
74            "Unexepected section kind for toc-data");
75     printCsectDirective(OS);
76     return;
77   }
78   // Common csect type (uninitialized storage) does not have to print csect
79   // directive for section switching.
80   if (isCsect() && getCSectType() == XCOFF::XTY_CM) {
81     assert((getMappingClass() == XCOFF::XMC_RW ||
82             getMappingClass() == XCOFF::XMC_BS ||
83             getMappingClass() == XCOFF::XMC_UL) &&
84            "Generated a storage-mapping class for a common/bss/tbss csect we "
85            "don't "
86            "understand how to switch to.");
87     // Common symbols and local zero-initialized symbols for TLS and Non-TLS are
88     // eligible for .bss/.tbss csect, getKind().isThreadBSS() is used to cover
89     // TLS common and zero-initialized local symbols since linkage type (in the
90     // GlobalVariable) is not accessible in this class.
91     assert((getKind().isBSSLocal() || getKind().isCommon() ||
92             getKind().isThreadBSS()) &&
93            "wrong symbol type for .bss/.tbss csect");
94     // Don't have to print a directive for switching to section for commons and
95     // zero-initialized TLS data. The '.comm' and '.lcomm' directives of the
96     // variable will create the needed csect.
97     return;
98   }
99 
100   // Zero-initialized TLS data with weak or external linkage are not eligible to
101   // be put into common csect.
102   if (getKind().isThreadBSS()) {
103     printCsectDirective(OS);
104     return;
105   }
106 
107   // XCOFF debug sections.
108   if (getKind().isMetadata() && isDwarfSect()) {
109     OS << "\n\t.dwsect "
110        << format("0x%" PRIx32, getDwarfSubtypeFlags().getValue()) << '\n';
111     OS << MAI.getPrivateLabelPrefix() << getName() << ':' << '\n';
112     return;
113   }
114 
115   report_fatal_error("Printing for this SectionKind is unimplemented.");
116 }
117 
118 bool MCSectionXCOFF::UseCodeAlign() const { return getKind().isText(); }
119 
120 bool MCSectionXCOFF::isVirtualSection() const {
121   assert(isCsect() && "Only csect section can be virtual!");
122   return XCOFF::XTY_CM == CsectProp->Type;
123 }
124