Chris Lattner | 87cffa9 | 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" |
Reid Kleckner | 1f13d47 | 2015-09-03 16:41:50 +0000 | [diff] [blame] | 14 | #include "llvm/Support/COFF.h" |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | using namespace llvm; |
| 17 | |
| 18 | MCSectionCOFF::~MCSectionCOFF() {} // anchor. |
| 19 | |
| 20 | // ShouldOmitSectionDirective - Decides whether a '.section' directive |
| 21 | // should be printed before the section name |
| 22 | bool MCSectionCOFF::ShouldOmitSectionDirective(StringRef Name, |
| 23 | const MCAsmInfo &MAI) const { |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 24 | if (COMDATSymbol) |
| 25 | return false; |
Jim Grosbach | dc1e36e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 27 | // FIXME: Does .section .bss/.data/.text work everywhere?? |
| 28 | if (Name == ".text" || Name == ".data" || Name == ".bss") |
| 29 | return true; |
| 30 | |
| 31 | return false; |
| 32 | } |
| 33 | |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 34 | void MCSectionCOFF::setSelection(int Selection) const { |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 35 | assert(Selection != 0 && "invalid COMDAT selection type"); |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 36 | this->Selection = Selection; |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 37 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
| 38 | } |
| 39 | |
Rafael Espindola | e0eba3c | 2017-01-30 15:38:43 +0000 | [diff] [blame] | 40 | void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 41 | raw_ostream &OS, |
| 42 | const MCExpr *Subsection) const { |
Jim Grosbach | dc1e36e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 43 | |
Nathan Jeffords | da7d014 | 2010-05-09 05:49:00 +0000 | [diff] [blame] | 44 | // standard sections don't require the '.section' |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 45 | if (ShouldOmitSectionDirective(SectionName, MAI)) { |
| 46 | OS << '\t' << getSectionName() << '\n'; |
| 47 | return; |
| 48 | } |
| 49 | |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 50 | OS << "\t.section\t" << getSectionName() << ",\""; |
David Majnemer | 5614ea9 | 2015-02-07 08:26:40 +0000 | [diff] [blame] | 51 | if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) |
| 52 | OS << 'd'; |
| 53 | if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) |
| 54 | OS << 'b'; |
David Majnemer | 7d0dc3e | 2014-09-20 20:40:50 +0000 | [diff] [blame] | 55 | if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE) |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 56 | OS << 'x'; |
David Majnemer | 7d0dc3e | 2014-09-20 20:40:50 +0000 | [diff] [blame] | 57 | if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE) |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 58 | OS << 'w'; |
David Majnemer | 7d0dc3e | 2014-09-20 20:40:50 +0000 | [diff] [blame] | 59 | else if (getCharacteristics() & COFF::IMAGE_SCN_MEM_READ) |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 60 | OS << 'r'; |
David Majnemer | 7d0dc3e | 2014-09-20 20:40:50 +0000 | [diff] [blame] | 61 | else |
| 62 | OS << 'y'; |
David Majnemer | 7d0dc3e | 2014-09-20 20:40:50 +0000 | [diff] [blame] | 63 | if (getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE) |
| 64 | OS << 'n'; |
| 65 | if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED) |
| 66 | OS << 's'; |
Reid Kleckner | 3126373 | 2016-09-14 22:41:50 +0000 | [diff] [blame] | 67 | if ((getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) && |
| 68 | !isImplicitlyDiscardable(SectionName)) |
| 69 | OS << 'D'; |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 70 | OS << '"'; |
Jim Grosbach | dc1e36e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 71 | |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 72 | if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) { |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 73 | OS << ","; |
Nathan Jeffords | 76a0758 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 74 | switch (Selection) { |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 75 | case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES: |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 76 | OS << "one_only,"; |
Nathan Jeffords | 76a0758 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 77 | break; |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 78 | case COFF::IMAGE_COMDAT_SELECT_ANY: |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 79 | OS << "discard,"; |
Nathan Jeffords | 76a0758 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 80 | break; |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 81 | case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE: |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 82 | OS << "same_size,"; |
Nathan Jeffords | 76a0758 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 83 | break; |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 84 | case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH: |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 85 | OS << "same_contents,"; |
Nathan Jeffords | 76a0758 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 86 | break; |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 87 | case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE: |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 88 | OS << "associative,"; |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 89 | break; |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 90 | case COFF::IMAGE_COMDAT_SELECT_LARGEST: |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 91 | OS << "largest,"; |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 92 | break; |
| 93 | case COFF::IMAGE_COMDAT_SELECT_NEWEST: |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 94 | OS << "newest,"; |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 95 | break; |
Nathan Jeffords | d2de49d | 2010-05-12 07:36:03 +0000 | [diff] [blame] | 96 | default: |
| 97 | assert (0 && "unsupported COFF selection type"); |
| 98 | break; |
Nathan Jeffords | 76a0758 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 99 | } |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 100 | assert(COMDATSymbol); |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 101 | COMDATSymbol->print(OS, &MAI); |
Nathan Jeffords | 76a0758 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 102 | } |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 103 | OS << '\n'; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 104 | } |
Jan Wen Voung | 87f77b5 | 2010-10-04 17:32:41 +0000 | [diff] [blame] | 105 | |
| 106 | bool MCSectionCOFF::UseCodeAlign() const { |
| 107 | return getKind().isText(); |
| 108 | } |
Rafael Espindola | 7a2cd8b | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 109 | |
| 110 | bool MCSectionCOFF::isVirtualSection() const { |
| 111 | return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; |
| 112 | } |