MCSectionCOFF.cpp revision 251662
1//===- 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 {
23
24  // 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
31void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
32                                         raw_ostream &OS,
33                                         const MCExpr *Subsection) const {
34
35  // standard sections don't require the '.section'
36  if (ShouldOmitSectionDirective(SectionName, MAI)) {
37    OS << '\t' << getSectionName() << '\n';
38    return;
39  }
40
41  OS << "\t.section\t" << getSectionName() << ",\"";
42  if (getKind().isText())
43    OS << 'x';
44  if (getKind().isWriteable())
45    OS << 'w';
46  else
47    OS << 'r';
48  if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE)
49    OS << 'n';
50  OS << "\"\n";
51
52  if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
53    switch (Selection) {
54      case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
55        OS << "\t.linkonce one_only\n";
56        break;
57      case COFF::IMAGE_COMDAT_SELECT_ANY:
58        OS << "\t.linkonce discard\n";
59        break;
60      case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
61        OS << "\t.linkonce same_size\n";
62        break;
63      case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
64        OS << "\t.linkonce same_contents\n";
65        break;
66    //NOTE: as of binutils 2.20, there is no way to specifiy select largest
67    //      with the .linkonce directive. For now, we treat it as an invalid
68    //      comdat selection value.
69      case COFF::IMAGE_COMDAT_SELECT_LARGEST:
70    //  OS << "\t.linkonce largest\n";
71    //  break;
72      default:
73        assert (0 && "unsupported COFF selection type");
74        break;
75    }
76  }
77}
78
79bool MCSectionCOFF::UseCodeAlign() const {
80  return getKind().isText();
81}
82
83bool MCSectionCOFF::isVirtualSection() const {
84  return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
85}
86