blob: 64aa2c5c49ea9d3bc9549b6fbd4ef864850bb477 [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 {
Jim Grosbach2684d9e2012-05-11 01:41:30 +000023
Chris Lattnereb40a0f2010-05-07 17:17:41 +000024 // FIXME: Does .section .bss/.data/.text work everywhere??
25 if (Name == ".text" || Name == ".data" || Name == ".bss")
26 return true;
27
28 return false;
29}
30
Nico Rieck80646282013-07-06 12:13:10 +000031void MCSectionCOFF::setSelection(int Selection,
32 const MCSectionCOFF *Assoc) const {
33 assert(Selection != 0 && "invalid COMDAT selection type");
34 assert((Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) ==
35 (Assoc != 0) &&
36 "associative COMDAT section must have an associated section");
37 this->Selection = Selection;
38 this->Assoc = Assoc;
39 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
40}
41
Chris Lattnereb40a0f2010-05-07 17:17:41 +000042void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
Peter Collingbournedf39be62013-04-17 21:18:16 +000043 raw_ostream &OS,
44 const MCExpr *Subsection) const {
Jim Grosbach2684d9e2012-05-11 01:41:30 +000045
Nathan Jeffords72e57f92010-05-09 05:49:00 +000046 // standard sections don't require the '.section'
Chris Lattnereb40a0f2010-05-07 17:17:41 +000047 if (ShouldOmitSectionDirective(SectionName, MAI)) {
48 OS << '\t' << getSectionName() << '\n';
49 return;
50 }
51
52 OS << "\t.section\t" << getSectionName() << ",\"";
53 if (getKind().isText())
54 OS << 'x';
55 if (getKind().isWriteable())
56 OS << 'w';
57 else
58 OS << 'r';
Daniel Dunbar94610582010-07-01 20:07:24 +000059 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE)
Chris Lattnereb40a0f2010-05-07 17:17:41 +000060 OS << 'n';
61 OS << "\"\n";
Jim Grosbach2684d9e2012-05-11 01:41:30 +000062
Daniel Dunbar94610582010-07-01 20:07:24 +000063 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
Nathan Jeffords871bb942010-05-12 04:26:09 +000064 switch (Selection) {
Daniel Dunbar94610582010-07-01 20:07:24 +000065 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
Nathan Jeffords871bb942010-05-12 04:26:09 +000066 OS << "\t.linkonce one_only\n";
67 break;
Daniel Dunbar94610582010-07-01 20:07:24 +000068 case COFF::IMAGE_COMDAT_SELECT_ANY:
Nathan Jeffords871bb942010-05-12 04:26:09 +000069 OS << "\t.linkonce discard\n";
70 break;
Daniel Dunbar94610582010-07-01 20:07:24 +000071 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
Nathan Jeffords871bb942010-05-12 04:26:09 +000072 OS << "\t.linkonce same_size\n";
73 break;
Daniel Dunbar94610582010-07-01 20:07:24 +000074 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
Nathan Jeffords871bb942010-05-12 04:26:09 +000075 OS << "\t.linkonce same_contents\n";
76 break;
Nico Rieck80646282013-07-06 12:13:10 +000077 case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
78 OS << "\t.linkonce associative " << Assoc->getSectionName() << "\n";
79 break;
Daniel Dunbar94610582010-07-01 20:07:24 +000080 case COFF::IMAGE_COMDAT_SELECT_LARGEST:
Nico Rieck80646282013-07-06 12:13:10 +000081 OS << "\t.linkonce largest\n";
82 break;
83 case COFF::IMAGE_COMDAT_SELECT_NEWEST:
84 OS << "\t.linkonce newest\n";
85 break;
Nathan Jeffords62d50e82010-05-12 07:36:03 +000086 default:
87 assert (0 && "unsupported COFF selection type");
88 break;
Nathan Jeffords871bb942010-05-12 04:26:09 +000089 }
90 }
Chris Lattnereb40a0f2010-05-07 17:17:41 +000091}
Jan Wen Voung083cf152010-10-04 17:32:41 +000092
93bool MCSectionCOFF::UseCodeAlign() const {
94 return getKind().isText();
95}
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000096
97bool MCSectionCOFF::isVirtualSection() const {
98 return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
99}