blob: a8f5db095b187af7f06018c7cb54f85fa2644d33 [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 {
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000023
Chris Lattner87cffa92010-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 Riecka37acf72013-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
Hans Wennborg69918bc2013-10-17 01:13:02 +000042static bool isAcceptableSectionNameChar(char C) {
43 return (C >= 'a' && C <= 'z') ||
44 (C >= 'A' && C <= 'Z') ||
45 (C >= '0' && C <= '9') ||
46 C == '_' || C == '$' || C == '.';
47}
48
49/// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
50/// syntactically correct.
51static bool sectionNameNeedsQuoting(StringRef Name) {
52 for (unsigned i = 0, e = Name.size(); i != e; ++i)
53 if (!isAcceptableSectionNameChar(Name[i]))
54 return true;
55 return false;
56}
57
Chris Lattner87cffa92010-05-07 17:17:41 +000058void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
Peter Collingbourne2f495b92013-04-17 21:18:16 +000059 raw_ostream &OS,
60 const MCExpr *Subsection) const {
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000061
Nathan Jeffordsda7d0142010-05-09 05:49:00 +000062 // standard sections don't require the '.section'
Chris Lattner87cffa92010-05-07 17:17:41 +000063 if (ShouldOmitSectionDirective(SectionName, MAI)) {
64 OS << '\t' << getSectionName() << '\n';
65 return;
66 }
67
Hans Wennborg69918bc2013-10-17 01:13:02 +000068 if (sectionNameNeedsQuoting(getSectionName()))
69 OS << "\t.section\t" << '"' << getSectionName() << '"' << ",\"";
70 else
71 OS << "\t.section\t" << getSectionName() << ",\"";
Chris Lattner87cffa92010-05-07 17:17:41 +000072 if (getKind().isText())
73 OS << 'x';
74 if (getKind().isWriteable())
75 OS << 'w';
76 else
77 OS << 'r';
Daniel Dunbar329d2022010-07-01 20:07:24 +000078 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE)
Chris Lattner87cffa92010-05-07 17:17:41 +000079 OS << 'n';
80 OS << "\"\n";
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000081
Daniel Dunbar329d2022010-07-01 20:07:24 +000082 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
Nathan Jeffords76a07582010-05-12 04:26:09 +000083 switch (Selection) {
Daniel Dunbar329d2022010-07-01 20:07:24 +000084 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
Nathan Jeffords76a07582010-05-12 04:26:09 +000085 OS << "\t.linkonce one_only\n";
86 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000087 case COFF::IMAGE_COMDAT_SELECT_ANY:
Nathan Jeffords76a07582010-05-12 04:26:09 +000088 OS << "\t.linkonce discard\n";
89 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000090 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
Nathan Jeffords76a07582010-05-12 04:26:09 +000091 OS << "\t.linkonce same_size\n";
92 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000093 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
Nathan Jeffords76a07582010-05-12 04:26:09 +000094 OS << "\t.linkonce same_contents\n";
95 break;
Nico Riecka37acf72013-07-06 12:13:10 +000096 case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
97 OS << "\t.linkonce associative " << Assoc->getSectionName() << "\n";
98 break;
Daniel Dunbar329d2022010-07-01 20:07:24 +000099 case COFF::IMAGE_COMDAT_SELECT_LARGEST:
Nico Riecka37acf72013-07-06 12:13:10 +0000100 OS << "\t.linkonce largest\n";
101 break;
102 case COFF::IMAGE_COMDAT_SELECT_NEWEST:
103 OS << "\t.linkonce newest\n";
104 break;
Nathan Jeffordsd2de49d2010-05-12 07:36:03 +0000105 default:
106 assert (0 && "unsupported COFF selection type");
107 break;
Nathan Jeffords76a07582010-05-12 04:26:09 +0000108 }
109 }
Chris Lattner87cffa92010-05-07 17:17:41 +0000110}
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000111
112bool MCSectionCOFF::UseCodeAlign() const {
113 return getKind().isText();
114}
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000115
116bool MCSectionCOFF::isVirtualSection() const {
117 return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
118}