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