blob: ad9ca8840f4f4169fdb693a96a20fcc3d0357229 [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
Nico Riecka37acf72013-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 Lattner87cffa92010-05-07 17:17:41 +000044void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
Peter Collingbourne2f495b92013-04-17 21:18:16 +000045 raw_ostream &OS,
46 const MCExpr *Subsection) const {
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000047
Nathan Jeffordsda7d0142010-05-09 05:49:00 +000048 // standard sections don't require the '.section'
Chris Lattner87cffa92010-05-07 17:17:41 +000049 if (ShouldOmitSectionDirective(SectionName, MAI)) {
50 OS << '\t' << getSectionName() << '\n';
51 return;
52 }
53
Hans Wennborg7ddcdc82013-10-18 02:14:40 +000054 OS << "\t.section\t" << getSectionName() << ",\"";
Chris Lattner87cffa92010-05-07 17:17:41 +000055 if (getKind().isText())
56 OS << 'x';
Reid Klecknerd4e53f52013-12-17 22:12:40 +000057 else if (getKind().isBSS())
58 OS << 'b';
Chris Lattner87cffa92010-05-07 17:17:41 +000059 if (getKind().isWriteable())
60 OS << 'w';
61 else
62 OS << 'r';
Daniel Dunbar329d2022010-07-01 20:07:24 +000063 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE)
Chris Lattner87cffa92010-05-07 17:17:41 +000064 OS << 'n';
Rafael Espindola2d30ae22013-11-27 01:18:37 +000065
66 OS << '"';
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000067
Daniel Dunbar329d2022010-07-01 20:07:24 +000068 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +000069 OS << ",";
Nathan Jeffords76a07582010-05-12 04:26:09 +000070 switch (Selection) {
Daniel Dunbar329d2022010-07-01 20:07:24 +000071 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000072 OS << "one_only,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000073 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000074 case COFF::IMAGE_COMDAT_SELECT_ANY:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000075 OS << "discard,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000076 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000077 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000078 OS << "same_size,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000079 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000080 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000081 OS << "same_contents,";
Nathan Jeffords76a07582010-05-12 04:26:09 +000082 break;
Nico Riecka37acf72013-07-06 12:13:10 +000083 case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000084 OS << "associative " << Assoc->getSectionName() << ",";
Nico Riecka37acf72013-07-06 12:13:10 +000085 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000086 case COFF::IMAGE_COMDAT_SELECT_LARGEST:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000087 OS << "largest,";
Nico Riecka37acf72013-07-06 12:13:10 +000088 break;
89 case COFF::IMAGE_COMDAT_SELECT_NEWEST:
Rafael Espindola2d30ae22013-11-27 01:18:37 +000090 OS << "newest,";
Nico Riecka37acf72013-07-06 12:13:10 +000091 break;
Nathan Jeffordsd2de49d2010-05-12 07:36:03 +000092 default:
93 assert (0 && "unsupported COFF selection type");
94 break;
Nathan Jeffords76a07582010-05-12 04:26:09 +000095 }
Rafael Espindola2d30ae22013-11-27 01:18:37 +000096 assert(COMDATSymbol);
97 OS << *COMDATSymbol;
Nathan Jeffords76a07582010-05-12 04:26:09 +000098 }
Rafael Espindola2d30ae22013-11-27 01:18:37 +000099 OS << '\n';
Chris Lattner87cffa92010-05-07 17:17:41 +0000100}
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000101
102bool MCSectionCOFF::UseCodeAlign() const {
103 return getKind().isText();
104}
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000105
106bool MCSectionCOFF::isVirtualSection() const {
107 return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
108}