blob: bb35027464021972184982fe0b205a25f5899fbb [file] [log] [blame]
Chris Lattnereb40a0f2010-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 {
Bill Wendling38af0672013-11-27 06:44:45 +000023 if (COMDATSymbol)
24 return false;
Jim Grosbach2684d9e2012-05-11 01:41:30 +000025
Chris Lattnereb40a0f2010-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
Nico Rieck80646282013-07-06 12:13:10 +000033void MCSectionCOFF::setSelection(int Selection,
34 const MCSectionCOFF *Assoc) const {
35 assert(Selection != 0 && "invalid COMDAT selection type");
36 assert((Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) ==
37 (Assoc != 0) &&
38 "associative COMDAT section must have an associated section");
39 this->Selection = Selection;
40 this->Assoc = Assoc;
41 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
42}
43
Chris Lattnereb40a0f2010-05-07 17:17:41 +000044void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
Peter Collingbournedf39be62013-04-17 21:18:16 +000045 raw_ostream &OS,
46 const MCExpr *Subsection) const {
Jim Grosbach2684d9e2012-05-11 01:41:30 +000047
Nathan Jeffords72e57f92010-05-09 05:49:00 +000048 // standard sections don't require the '.section'
Chris Lattnereb40a0f2010-05-07 17:17:41 +000049 if (ShouldOmitSectionDirective(SectionName, MAI)) {
50 OS << '\t' << getSectionName() << '\n';
51 return;
52 }
53
Hans Wennborgab887bf2013-10-18 02:14:40 +000054 OS << "\t.section\t" << getSectionName() << ",\"";
Chris Lattnereb40a0f2010-05-07 17:17:41 +000055 if (getKind().isText())
56 OS << 'x';
57 if (getKind().isWriteable())
58 OS << 'w';
59 else
60 OS << 'r';
Daniel Dunbar94610582010-07-01 20:07:24 +000061 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE)
Chris Lattnereb40a0f2010-05-07 17:17:41 +000062 OS << 'n';
Bill Wendling38af0672013-11-27 06:44:45 +000063
64 OS << '"';
Jim Grosbach2684d9e2012-05-11 01:41:30 +000065
Daniel Dunbar94610582010-07-01 20:07:24 +000066 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
Bill Wendling38af0672013-11-27 06:44:45 +000067 OS << ",";
Nathan Jeffords871bb942010-05-12 04:26:09 +000068 switch (Selection) {
Daniel Dunbar94610582010-07-01 20:07:24 +000069 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
Bill Wendling38af0672013-11-27 06:44:45 +000070 OS << "one_only,";
Nathan Jeffords871bb942010-05-12 04:26:09 +000071 break;
Daniel Dunbar94610582010-07-01 20:07:24 +000072 case COFF::IMAGE_COMDAT_SELECT_ANY:
Bill Wendling38af0672013-11-27 06:44:45 +000073 OS << "discard,";
Nathan Jeffords871bb942010-05-12 04:26:09 +000074 break;
Daniel Dunbar94610582010-07-01 20:07:24 +000075 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
Bill Wendling38af0672013-11-27 06:44:45 +000076 OS << "same_size,";
Nathan Jeffords871bb942010-05-12 04:26:09 +000077 break;
Daniel Dunbar94610582010-07-01 20:07:24 +000078 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
Bill Wendling38af0672013-11-27 06:44:45 +000079 OS << "same_contents,";
Nathan Jeffords871bb942010-05-12 04:26:09 +000080 break;
Nico Rieck80646282013-07-06 12:13:10 +000081 case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
Bill Wendling38af0672013-11-27 06:44:45 +000082 OS << "associative " << Assoc->getSectionName() << ",";
Nico Rieck80646282013-07-06 12:13:10 +000083 break;
Daniel Dunbar94610582010-07-01 20:07:24 +000084 case COFF::IMAGE_COMDAT_SELECT_LARGEST:
Bill Wendling38af0672013-11-27 06:44:45 +000085 OS << "largest,";
Nico Rieck80646282013-07-06 12:13:10 +000086 break;
87 case COFF::IMAGE_COMDAT_SELECT_NEWEST:
Bill Wendling38af0672013-11-27 06:44:45 +000088 OS << "newest,";
Nico Rieck80646282013-07-06 12:13:10 +000089 break;
Nathan Jeffords62d50e82010-05-12 07:36:03 +000090 default:
91 assert (0 && "unsupported COFF selection type");
92 break;
Nathan Jeffords871bb942010-05-12 04:26:09 +000093 }
Bill Wendling38af0672013-11-27 06:44:45 +000094 assert(COMDATSymbol);
95 OS << *COMDATSymbol;
Nathan Jeffords871bb942010-05-12 04:26:09 +000096 }
Bill Wendling38af0672013-11-27 06:44:45 +000097 OS << '\n';
Chris Lattnereb40a0f2010-05-07 17:17:41 +000098}
Jan Wen Voung083cf152010-10-04 17:32:41 +000099
100bool MCSectionCOFF::UseCodeAlign() const {
101 return getKind().isText();
102}
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000103
104bool MCSectionCOFF::isVirtualSection() const {
105 return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
106}