xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCSectionXCOFF.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- lib/MC/MCSectionXCOFF.cpp - XCOFF Code Section Representation ------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/MC/MCSectionXCOFF.h"
100b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
11fe6060f1SDimitry Andric #include "llvm/Support/Format.h"
120b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
1381ad6265SDimitry Andric namespace llvm {
1481ad6265SDimitry Andric class MCExpr;
1581ad6265SDimitry Andric class Triple;
1681ad6265SDimitry Andric } // namespace llvm
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric using namespace llvm;
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric MCSectionXCOFF::~MCSectionXCOFF() = default;
210b57cec5SDimitry Andric 
printCsectDirective(raw_ostream & OS) const225ffd83dbSDimitry Andric void MCSectionXCOFF::printCsectDirective(raw_ostream &OS) const {
23bdd1243dSDimitry Andric   OS << "\t.csect " << QualName->getName() << "," << Log2(getAlign()) << '\n';
245ffd83dbSDimitry Andric }
258bcb0991SDimitry Andric 
printSwitchToSection(const MCAsmInfo & MAI,const Triple & T,raw_ostream & OS,uint32_t Subsection) const2681ad6265SDimitry Andric void MCSectionXCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
270b57cec5SDimitry Andric                                           raw_ostream &OS,
28*0fca6ea1SDimitry Andric                                           uint32_t Subsection) const {
290b57cec5SDimitry Andric   if (getKind().isText()) {
308bcb0991SDimitry Andric     if (getMappingClass() != XCOFF::XMC_PR)
318bcb0991SDimitry Andric       report_fatal_error("Unhandled storage-mapping class for .text csect");
328bcb0991SDimitry Andric 
335ffd83dbSDimitry Andric     printCsectDirective(OS);
34480093f4SDimitry Andric     return;
35480093f4SDimitry Andric   }
36480093f4SDimitry Andric 
37480093f4SDimitry Andric   if (getKind().isReadOnly()) {
3804eeddc0SDimitry Andric     if (getMappingClass() != XCOFF::XMC_RO &&
3904eeddc0SDimitry Andric         getMappingClass() != XCOFF::XMC_TD)
40480093f4SDimitry Andric       report_fatal_error("Unhandled storage-mapping class for .rodata csect.");
415ffd83dbSDimitry Andric     printCsectDirective(OS);
420b57cec5SDimitry Andric     return;
430b57cec5SDimitry Andric   }
440b57cec5SDimitry Andric 
455f757f3fSDimitry Andric   if (getKind().isReadOnlyWithRel()) {
465f757f3fSDimitry Andric     if (getMappingClass() != XCOFF::XMC_RW &&
475f757f3fSDimitry Andric         getMappingClass() != XCOFF::XMC_RO &&
485f757f3fSDimitry Andric         getMappingClass() != XCOFF::XMC_TD)
495f757f3fSDimitry Andric       report_fatal_error(
505f757f3fSDimitry Andric           "Unexepected storage-mapping class for ReadOnlyWithRel kind");
515f757f3fSDimitry Andric     printCsectDirective(OS);
525f757f3fSDimitry Andric     return;
535f757f3fSDimitry Andric   }
545f757f3fSDimitry Andric 
55fe6060f1SDimitry Andric   // Initialized TLS data.
56fe6060f1SDimitry Andric   if (getKind().isThreadData()) {
57fe6060f1SDimitry Andric     // We only expect XMC_TL here for initialized TLS data.
58fe6060f1SDimitry Andric     if (getMappingClass() != XCOFF::XMC_TL)
59fe6060f1SDimitry Andric       report_fatal_error("Unhandled storage-mapping class for .tdata csect.");
60fe6060f1SDimitry Andric     printCsectDirective(OS);
61fe6060f1SDimitry Andric     return;
62fe6060f1SDimitry Andric   }
63fe6060f1SDimitry Andric 
648bcb0991SDimitry Andric   if (getKind().isData()) {
658bcb0991SDimitry Andric     switch (getMappingClass()) {
668bcb0991SDimitry Andric     case XCOFF::XMC_RW:
678bcb0991SDimitry Andric     case XCOFF::XMC_DS:
68fe6060f1SDimitry Andric     case XCOFF::XMC_TD:
695ffd83dbSDimitry Andric       printCsectDirective(OS);
70480093f4SDimitry Andric       break;
71480093f4SDimitry Andric     case XCOFF::XMC_TC:
72e8d8bef9SDimitry Andric     case XCOFF::XMC_TE:
738bcb0991SDimitry Andric       break;
748bcb0991SDimitry Andric     case XCOFF::XMC_TC0:
758bcb0991SDimitry Andric       OS << "\t.toc\n";
768bcb0991SDimitry Andric       break;
778bcb0991SDimitry Andric     default:
788bcb0991SDimitry Andric       report_fatal_error(
798bcb0991SDimitry Andric           "Unhandled storage-mapping class for .data csect.");
808bcb0991SDimitry Andric     }
818bcb0991SDimitry Andric     return;
828bcb0991SDimitry Andric   }
838bcb0991SDimitry Andric 
84fe6060f1SDimitry Andric   if (isCsect() && getMappingClass() == XCOFF::XMC_TD) {
85*0fca6ea1SDimitry Andric     // Common csect type (uninitialized storage) does not have to print csect
86*0fca6ea1SDimitry Andric     // directive for section switching unless it is local.
87*0fca6ea1SDimitry Andric     if (getKind().isCommon() && !getKind().isBSSLocal())
88*0fca6ea1SDimitry Andric       return;
89*0fca6ea1SDimitry Andric 
90*0fca6ea1SDimitry Andric     assert(getKind().isBSS() && "Unexpected section kind for toc-data");
91fe6060f1SDimitry Andric     printCsectDirective(OS);
92fe6060f1SDimitry Andric     return;
93fe6060f1SDimitry Andric   }
94fe6060f1SDimitry Andric   // Common csect type (uninitialized storage) does not have to print csect
95fe6060f1SDimitry Andric   // directive for section switching.
96fe6060f1SDimitry Andric   if (isCsect() && getCSectType() == XCOFF::XTY_CM) {
978bcb0991SDimitry Andric     assert((getMappingClass() == XCOFF::XMC_RW ||
98fe6060f1SDimitry Andric             getMappingClass() == XCOFF::XMC_BS ||
99fe6060f1SDimitry Andric             getMappingClass() == XCOFF::XMC_UL) &&
100fe6060f1SDimitry Andric            "Generated a storage-mapping class for a common/bss/tbss csect we "
101fe6060f1SDimitry Andric            "don't "
1028bcb0991SDimitry Andric            "understand how to switch to.");
103fe6060f1SDimitry Andric     // Common symbols and local zero-initialized symbols for TLS and Non-TLS are
104fe6060f1SDimitry Andric     // eligible for .bss/.tbss csect, getKind().isThreadBSS() is used to cover
105fe6060f1SDimitry Andric     // TLS common and zero-initialized local symbols since linkage type (in the
106fe6060f1SDimitry Andric     // GlobalVariable) is not accessible in this class.
107fe6060f1SDimitry Andric     assert((getKind().isBSSLocal() || getKind().isCommon() ||
108fe6060f1SDimitry Andric             getKind().isThreadBSS()) &&
109fe6060f1SDimitry Andric            "wrong symbol type for .bss/.tbss csect");
110fe6060f1SDimitry Andric     // Don't have to print a directive for switching to section for commons and
111fe6060f1SDimitry Andric     // zero-initialized TLS data. The '.comm' and '.lcomm' directives of the
112fe6060f1SDimitry Andric     // variable will create the needed csect.
113fe6060f1SDimitry Andric     return;
114fe6060f1SDimitry Andric   }
115fe6060f1SDimitry Andric 
116fe6060f1SDimitry Andric   // Zero-initialized TLS data with weak or external linkage are not eligible to
117fe6060f1SDimitry Andric   // be put into common csect.
118fe6060f1SDimitry Andric   if (getKind().isThreadBSS()) {
119fe6060f1SDimitry Andric     printCsectDirective(OS);
120fe6060f1SDimitry Andric     return;
121fe6060f1SDimitry Andric   }
122fe6060f1SDimitry Andric 
123fe6060f1SDimitry Andric   // XCOFF debug sections.
124fe6060f1SDimitry Andric   if (getKind().isMetadata() && isDwarfSect()) {
125bdd1243dSDimitry Andric     OS << "\n\t.dwsect " << format("0x%" PRIx32, *getDwarfSubtypeFlags())
126753f127fSDimitry Andric        << '\n';
127*0fca6ea1SDimitry Andric     OS << getName() << ':' << '\n';
1288bcb0991SDimitry Andric     return;
1298bcb0991SDimitry Andric   }
1308bcb0991SDimitry Andric 
1310b57cec5SDimitry Andric   report_fatal_error("Printing for this SectionKind is unimplemented.");
1320b57cec5SDimitry Andric }
1330b57cec5SDimitry Andric 
useCodeAlign() const13481ad6265SDimitry Andric bool MCSectionXCOFF::useCodeAlign() const { return getKind().isText(); }
135