blob: fc2bd365e16d56373e462a33fbd2c605860d841f [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"
14#include "llvm/Support/raw_ostream.h"
15using namespace llvm;
16
17MCSectionCOFF::~MCSectionCOFF() {} // anchor.
18
19// ShouldOmitSectionDirective - Decides whether a '.section' directive
20// should be printed before the section name
21bool MCSectionCOFF::ShouldOmitSectionDirective(StringRef Name,
22 const MCAsmInfo &MAI) const {
Rafael Espindola2d30ae22013-11-27 01:18:37 +000023 if (COMDATSymbol)
24 return false;
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000025
Chris Lattner87cffa92010-05-07 17:17:41 +000026 // FIXME: Does .section .bss/.data/.text work everywhere??
27 if (Name == ".text" || Name == ".data" || Name == ".bss")
28 return true;
29
30 return false;
31}
32
Rafael Espindola0766ae02014-06-06 19:26:12 +000033void MCSectionCOFF::setSelection(int Selection) const {
Nico Riecka37acf72013-07-06 12:13:10 +000034 assert(Selection != 0 && "invalid COMDAT selection type");
Nico Riecka37acf72013-07-06 12:13:10 +000035 this->Selection = Selection;
Nico Riecka37acf72013-07-06 12:13:10 +000036 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
37}
38
Chris Lattner87cffa92010-05-07 17:17:41 +000039void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
Peter Collingbourne2f495b92013-04-17 21:18:16 +000040 raw_ostream &OS,
41 const MCExpr *Subsection) const {
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000042
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() << ",\"";
Chris Lattner87cffa92010-05-07 17:17:41 +000050 if (getKind().isText())
51 OS << 'x';
Reid Klecknerd4e53f52013-12-17 22:12:40 +000052 else if (getKind().isBSS())
53 OS << 'b';
Chris Lattner87cffa92010-05-07 17:17:41 +000054 if (getKind().isWriteable())
55 OS << 'w';
56 else
57 OS << 'r';
Daniel Dunbar329d2022010-07-01 20:07:24 +000058 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE)
Chris Lattner87cffa92010-05-07 17:17:41 +000059 OS << 'n';
Saleem Abdulrasool11049a02014-04-23 21:29:34 +000060 if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
61 OS << 'd';
Rafael Espindola2d30ae22013-11-27 01:18:37 +000062 OS << '"';
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000063
Daniel Dunbar329d2022010-07-01 20:07:24 +000064 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +000065 OS << ",";
Nathan Jeffords76a07582010-05-12 04:26:09 +000066 switch (Selection) {
Daniel Dunbar329d2022010-07-01 20:07:24 +000067 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000068 OS << "one_only,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000069 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000070 case COFF::IMAGE_COMDAT_SELECT_ANY:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000071 OS << "discard,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000072 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000073 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000074 OS << "same_size,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000075 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000076 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000077 OS << "same_contents,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000078 break;
Nico Riecka37acf72013-07-06 12:13:10 +000079 case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
Rafael Espindola0766ae02014-06-06 19:26:12 +000080 OS << "associative,";
Nico Riecka37acf72013-07-06 12:13:10 +000081 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000082 case COFF::IMAGE_COMDAT_SELECT_LARGEST:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000083 OS << "largest,";
Nico Riecka37acf72013-07-06 12:13:10 +000084 break;
85 case COFF::IMAGE_COMDAT_SELECT_NEWEST:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000086 OS << "newest,";
Nico Riecka37acf72013-07-06 12:13:10 +000087 break;
Nathan Jeffordsd2de49d2010-05-12 07:36:03 +000088 default:
89 assert (0 && "unsupported COFF selection type");
90 break;
Nathan Jeffords76a07582010-05-12 04:26:09 +000091 }
Rafael Espindola2d30ae22013-11-27 01:18:37 +000092 assert(COMDATSymbol);
93 OS << *COMDATSymbol;
Nathan Jeffords76a07582010-05-12 04:26:09 +000094 }
Rafael Espindola2d30ae22013-11-27 01:18:37 +000095 OS << '\n';
Chris Lattner87cffa92010-05-07 17:17:41 +000096}
Jan Wen Voung87f77b52010-10-04 17:32:41 +000097
98bool MCSectionCOFF::UseCodeAlign() const {
99 return getKind().isText();
100}
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000101
102bool MCSectionCOFF::isVirtualSection() const {
103 return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
104}