blob: 72a7fc36a460c9fbdbf8335bf70638d4620b684c [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"
Zachary Turner264b5d92017-06-07 03:48:56 +000011#include "llvm/BinaryFormat/COFF.h"
Chris Lattner87cffa92010-05-07 17:17:41 +000012#include "llvm/MC/MCSymbol.h"
13#include "llvm/Support/raw_ostream.h"
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +000014#include <cassert>
15
Chris Lattner87cffa92010-05-07 17:17:41 +000016using namespace llvm;
17
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +000018MCSectionCOFF::~MCSectionCOFF() = default; // anchor.
Chris Lattner87cffa92010-05-07 17:17:41 +000019
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 {
Nathan Jeffordsda7d0142010-05-09 05:49:00 +000043 // standard sections don't require the '.section'
Chris Lattner87cffa92010-05-07 17:17:41 +000044 if (ShouldOmitSectionDirective(SectionName, MAI)) {
45 OS << '\t' << getSectionName() << '\n';
46 return;
47 }
48
Hans Wennborg7ddcdc82013-10-18 02:14:40 +000049 OS << "\t.section\t" << getSectionName() << ",\"";
David Majnemer5614ea92015-02-07 08:26:40 +000050 if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
51 OS << 'd';
52 if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
53 OS << 'b';
David Majnemer7d0dc3e2014-09-20 20:40:50 +000054 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE)
Chris Lattner87cffa92010-05-07 17:17:41 +000055 OS << 'x';
David Majnemer7d0dc3e2014-09-20 20:40:50 +000056 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE)
Chris Lattner87cffa92010-05-07 17:17:41 +000057 OS << 'w';
David Majnemer7d0dc3e2014-09-20 20:40:50 +000058 else if (getCharacteristics() & COFF::IMAGE_SCN_MEM_READ)
Chris Lattner87cffa92010-05-07 17:17:41 +000059 OS << 'r';
David Majnemer7d0dc3e2014-09-20 20:40:50 +000060 else
61 OS << 'y';
David Majnemer7d0dc3e2014-09-20 20:40:50 +000062 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE)
63 OS << 'n';
64 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED)
65 OS << 's';
Reid Kleckner31263732016-09-14 22:41:50 +000066 if ((getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) &&
67 !isImplicitlyDiscardable(SectionName))
68 OS << 'D';
Rafael Espindola2d30ae22013-11-27 01:18:37 +000069 OS << '"';
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000070
Daniel Dunbar329d2022010-07-01 20:07:24 +000071 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +000072 OS << ",";
Nathan Jeffords76a07582010-05-12 04:26:09 +000073 switch (Selection) {
Daniel Dunbar329d2022010-07-01 20:07:24 +000074 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000075 OS << "one_only,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000076 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000077 case COFF::IMAGE_COMDAT_SELECT_ANY:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000078 OS << "discard,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000079 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000080 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000081 OS << "same_size,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000082 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000083 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000084 OS << "same_contents,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000085 break;
Nico Riecka37acf72013-07-06 12:13:10 +000086 case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
Rafael Espindola0766ae02014-06-06 19:26:12 +000087 OS << "associative,";
Nico Riecka37acf72013-07-06 12:13:10 +000088 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000089 case COFF::IMAGE_COMDAT_SELECT_LARGEST:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000090 OS << "largest,";
Nico Riecka37acf72013-07-06 12:13:10 +000091 break;
92 case COFF::IMAGE_COMDAT_SELECT_NEWEST:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000093 OS << "newest,";
Nico Riecka37acf72013-07-06 12:13:10 +000094 break;
Nathan Jeffordsd2de49d2010-05-12 07:36:03 +000095 default:
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +000096 assert(false && "unsupported COFF selection type");
Nathan Jeffordsd2de49d2010-05-12 07:36:03 +000097 break;
Nathan Jeffords76a07582010-05-12 04:26:09 +000098 }
Rafael Espindola2d30ae22013-11-27 01:18:37 +000099 assert(COMDATSymbol);
Matt Arsenault8b643552015-06-09 00:31:39 +0000100 COMDATSymbol->print(OS, &MAI);
Nathan Jeffords76a07582010-05-12 04:26:09 +0000101 }
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000102 OS << '\n';
Chris Lattner87cffa92010-05-07 17:17:41 +0000103}
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000104
105bool MCSectionCOFF::UseCodeAlign() const {
106 return getKind().isText();
107}
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000108
109bool MCSectionCOFF::isVirtualSection() const {
110 return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
111}