blob: 4e2df01c89c272aec147ddfb2385025e943c0fcb [file] [log] [blame]
Chris Lattner87cffa92010-05-07 17:17:41 +00001//===- 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"
Reid Kleckner1f13d472015-09-03 16:41:50 +000014#include "llvm/Support/COFF.h"
Chris Lattner87cffa92010-05-07 17:17:41 +000015#include "llvm/Support/raw_ostream.h"
16using namespace llvm;
17
18MCSectionCOFF::~MCSectionCOFF() {} // anchor.
19
20// ShouldOmitSectionDirective - Decides whether a '.section' directive
21// should be printed before the section name
22bool MCSectionCOFF::ShouldOmitSectionDirective(StringRef Name,
23 const MCAsmInfo &MAI) const {
Rafael Espindola2d30ae22013-11-27 01:18:37 +000024 if (COMDATSymbol)
25 return false;
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000026
Chris Lattner87cffa92010-05-07 17:17:41 +000027 // 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
Rafael Espindola0766ae02014-06-06 19:26:12 +000034void MCSectionCOFF::setSelection(int Selection) const {
Nico Riecka37acf72013-07-06 12:13:10 +000035 assert(Selection != 0 && "invalid COMDAT selection type");
Nico Riecka37acf72013-07-06 12:13:10 +000036 this->Selection = Selection;
Nico Riecka37acf72013-07-06 12:13:10 +000037 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
38}
39
Rafael Espindolae0eba3c2017-01-30 15:38:43 +000040void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
Peter Collingbourne2f495b92013-04-17 21:18:16 +000041 raw_ostream &OS,
42 const MCExpr *Subsection) const {
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000043
Nathan Jeffordsda7d0142010-05-09 05:49:00 +000044 // standard sections don't require the '.section'
Chris Lattner87cffa92010-05-07 17:17:41 +000045 if (ShouldOmitSectionDirective(SectionName, MAI)) {
46 OS << '\t' << getSectionName() << '\n';
47 return;
48 }
49
Hans Wennborg7ddcdc82013-10-18 02:14:40 +000050 OS << "\t.section\t" << getSectionName() << ",\"";
David Majnemer5614ea92015-02-07 08:26:40 +000051 if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
52 OS << 'd';
53 if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
54 OS << 'b';
David Majnemer7d0dc3e2014-09-20 20:40:50 +000055 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE)
Chris Lattner87cffa92010-05-07 17:17:41 +000056 OS << 'x';
David Majnemer7d0dc3e2014-09-20 20:40:50 +000057 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE)
Chris Lattner87cffa92010-05-07 17:17:41 +000058 OS << 'w';
David Majnemer7d0dc3e2014-09-20 20:40:50 +000059 else if (getCharacteristics() & COFF::IMAGE_SCN_MEM_READ)
Chris Lattner87cffa92010-05-07 17:17:41 +000060 OS << 'r';
David Majnemer7d0dc3e2014-09-20 20:40:50 +000061 else
62 OS << 'y';
David Majnemer7d0dc3e2014-09-20 20:40:50 +000063 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE)
64 OS << 'n';
65 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED)
66 OS << 's';
Reid Kleckner31263732016-09-14 22:41:50 +000067 if ((getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) &&
68 !isImplicitlyDiscardable(SectionName))
69 OS << 'D';
Rafael Espindola2d30ae22013-11-27 01:18:37 +000070 OS << '"';
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000071
Daniel Dunbar329d2022010-07-01 20:07:24 +000072 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +000073 OS << ",";
Nathan Jeffords76a07582010-05-12 04:26:09 +000074 switch (Selection) {
Daniel Dunbar329d2022010-07-01 20:07:24 +000075 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000076 OS << "one_only,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000077 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000078 case COFF::IMAGE_COMDAT_SELECT_ANY:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000079 OS << "discard,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000080 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000081 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000082 OS << "same_size,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000083 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000084 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000085 OS << "same_contents,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000086 break;
Nico Riecka37acf72013-07-06 12:13:10 +000087 case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
Rafael Espindola0766ae02014-06-06 19:26:12 +000088 OS << "associative,";
Nico Riecka37acf72013-07-06 12:13:10 +000089 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000090 case COFF::IMAGE_COMDAT_SELECT_LARGEST:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000091 OS << "largest,";
Nico Riecka37acf72013-07-06 12:13:10 +000092 break;
93 case COFF::IMAGE_COMDAT_SELECT_NEWEST:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000094 OS << "newest,";
Nico Riecka37acf72013-07-06 12:13:10 +000095 break;
Nathan Jeffordsd2de49d2010-05-12 07:36:03 +000096 default:
97 assert (0 && "unsupported COFF selection type");
98 break;
Nathan Jeffords76a07582010-05-12 04:26:09 +000099 }
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000100 assert(COMDATSymbol);
Matt Arsenault8b643552015-06-09 00:31:39 +0000101 COMDATSymbol->print(OS, &MAI);
Nathan Jeffords76a07582010-05-12 04:26:09 +0000102 }
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000103 OS << '\n';
Chris Lattner87cffa92010-05-07 17:17:41 +0000104}
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000105
106bool MCSectionCOFF::UseCodeAlign() const {
107 return getKind().isText();
108}
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000109
110bool MCSectionCOFF::isVirtualSection() const {
111 return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
112}