Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 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" |
| 15 | using namespace llvm; |
| 16 | |
| 17 | MCSectionCOFF::~MCSectionCOFF() {} // anchor. |
| 18 | |
| 19 | // ShouldOmitSectionDirective - Decides whether a '.section' directive |
| 20 | // should be printed before the section name |
| 21 | bool MCSectionCOFF::ShouldOmitSectionDirective(StringRef Name, |
| 22 | const MCAsmInfo &MAI) const { |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 23 | |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 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 | |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 31 | void 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 Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 42 | void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 43 | raw_ostream &OS, |
| 44 | const MCExpr *Subsection) const { |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 45 | |
Nathan Jeffords | 72e57f9 | 2010-05-09 05:49:00 +0000 | [diff] [blame] | 46 | // standard sections don't require the '.section' |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 47 | if (ShouldOmitSectionDirective(SectionName, MAI)) { |
| 48 | OS << '\t' << getSectionName() << '\n'; |
| 49 | return; |
| 50 | } |
| 51 | |
Hans Wennborg | ab887bf | 2013-10-18 02:14:40 +0000 | [diff] [blame^] | 52 | OS << "\t.section\t" << getSectionName() << ",\""; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 53 | if (getKind().isText()) |
| 54 | OS << 'x'; |
| 55 | if (getKind().isWriteable()) |
| 56 | OS << 'w'; |
| 57 | else |
| 58 | OS << 'r'; |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 59 | if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 60 | OS << 'n'; |
| 61 | OS << "\"\n"; |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 62 | |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 63 | if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) { |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 64 | switch (Selection) { |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 65 | case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES: |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 66 | OS << "\t.linkonce one_only\n"; |
| 67 | break; |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 68 | case COFF::IMAGE_COMDAT_SELECT_ANY: |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 69 | OS << "\t.linkonce discard\n"; |
| 70 | break; |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 71 | case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE: |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 72 | OS << "\t.linkonce same_size\n"; |
| 73 | break; |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 74 | case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH: |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 75 | OS << "\t.linkonce same_contents\n"; |
| 76 | break; |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 77 | case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE: |
| 78 | OS << "\t.linkonce associative " << Assoc->getSectionName() << "\n"; |
| 79 | break; |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 80 | case COFF::IMAGE_COMDAT_SELECT_LARGEST: |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 81 | OS << "\t.linkonce largest\n"; |
| 82 | break; |
| 83 | case COFF::IMAGE_COMDAT_SELECT_NEWEST: |
| 84 | OS << "\t.linkonce newest\n"; |
| 85 | break; |
Nathan Jeffords | 62d50e8 | 2010-05-12 07:36:03 +0000 | [diff] [blame] | 86 | default: |
| 87 | assert (0 && "unsupported COFF selection type"); |
| 88 | break; |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 91 | } |
Jan Wen Voung | 083cf15 | 2010-10-04 17:32:41 +0000 | [diff] [blame] | 92 | |
| 93 | bool MCSectionCOFF::UseCodeAlign() const { |
| 94 | return getKind().isText(); |
| 95 | } |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 96 | |
| 97 | bool MCSectionCOFF::isVirtualSection() const { |
| 98 | return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; |
| 99 | } |