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" |
Zachary Turner | 19ca2b0 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 11 | #include "llvm/BinaryFormat/COFF.h" |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCSymbol.h" |
| 13 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | c51124b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 14 | #include <cassert> |
| 15 | |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
Eugene Zelenko | c51124b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 18 | MCSectionCOFF::~MCSectionCOFF() = default; // anchor. |
Chris Lattner | eb40a0f | 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 | 823c9c7 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 24 | if (COMDATSymbol) |
| 25 | return false; |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 26 | |
Chris Lattner | eb40a0f | 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 | 013321a | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 34 | void MCSectionCOFF::setSelection(int Selection) const { |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 35 | assert(Selection != 0 && "invalid COMDAT selection type"); |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 36 | this->Selection = Selection; |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 37 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
| 38 | } |
| 39 | |
Rafael Espindola | d5702b7 | 2017-01-30 15:38:43 +0000 | [diff] [blame] | 40 | void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 41 | raw_ostream &OS, |
| 42 | const MCExpr *Subsection) const { |
Nathan Jeffords | 72e57f9 | 2010-05-09 05:49:00 +0000 | [diff] [blame] | 43 | // standard sections don't require the '.section' |
Chris Lattner | eb40a0f | 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 | ab887bf | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 49 | OS << "\t.section\t" << getSectionName() << ",\""; |
David Majnemer | fdac306 | 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 | 1c1bde6 | 2014-09-20 20:40:50 +0000 | [diff] [blame] | 54 | if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE) |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 55 | OS << 'x'; |
David Majnemer | 1c1bde6 | 2014-09-20 20:40:50 +0000 | [diff] [blame] | 56 | if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE) |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 57 | OS << 'w'; |
David Majnemer | 1c1bde6 | 2014-09-20 20:40:50 +0000 | [diff] [blame] | 58 | else if (getCharacteristics() & COFF::IMAGE_SCN_MEM_READ) |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 59 | OS << 'r'; |
David Majnemer | 1c1bde6 | 2014-09-20 20:40:50 +0000 | [diff] [blame] | 60 | else |
| 61 | OS << 'y'; |
David Majnemer | 1c1bde6 | 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 | a984fc7 | 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 | 823c9c7 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 69 | OS << '"'; |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 70 | |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 71 | if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) { |
Martin Storsjo | c819c64 | 2018-07-23 22:15:19 +0000 | [diff] [blame] | 72 | if (COMDATSymbol) |
| 73 | OS << ","; |
| 74 | else |
| 75 | OS << "\n\t.linkonce\t"; |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 76 | switch (Selection) { |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 77 | case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES: |
Martin Storsjo | c819c64 | 2018-07-23 22:15:19 +0000 | [diff] [blame] | 78 | OS << "one_only"; |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 79 | break; |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 80 | case COFF::IMAGE_COMDAT_SELECT_ANY: |
Martin Storsjo | c819c64 | 2018-07-23 22:15:19 +0000 | [diff] [blame] | 81 | OS << "discard"; |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 82 | break; |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 83 | case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE: |
Martin Storsjo | c819c64 | 2018-07-23 22:15:19 +0000 | [diff] [blame] | 84 | OS << "same_size"; |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 85 | break; |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 86 | case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH: |
Martin Storsjo | c819c64 | 2018-07-23 22:15:19 +0000 | [diff] [blame] | 87 | OS << "same_contents"; |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 88 | break; |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 89 | case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE: |
Martin Storsjo | c819c64 | 2018-07-23 22:15:19 +0000 | [diff] [blame] | 90 | OS << "associative"; |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 91 | break; |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 92 | case COFF::IMAGE_COMDAT_SELECT_LARGEST: |
Martin Storsjo | c819c64 | 2018-07-23 22:15:19 +0000 | [diff] [blame] | 93 | OS << "largest"; |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 94 | break; |
| 95 | case COFF::IMAGE_COMDAT_SELECT_NEWEST: |
Martin Storsjo | c819c64 | 2018-07-23 22:15:19 +0000 | [diff] [blame] | 96 | OS << "newest"; |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 97 | break; |
Nathan Jeffords | 62d50e8 | 2010-05-12 07:36:03 +0000 | [diff] [blame] | 98 | default: |
Eugene Zelenko | c51124b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 99 | assert(false && "unsupported COFF selection type"); |
Nathan Jeffords | 62d50e8 | 2010-05-12 07:36:03 +0000 | [diff] [blame] | 100 | break; |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 101 | } |
Martin Storsjo | c819c64 | 2018-07-23 22:15:19 +0000 | [diff] [blame] | 102 | if (COMDATSymbol) { |
| 103 | OS << ","; |
| 104 | COMDATSymbol->print(OS, &MAI); |
| 105 | } |
Nathan Jeffords | 871bb94 | 2010-05-12 04:26:09 +0000 | [diff] [blame] | 106 | } |
Rafael Espindola | 823c9c7 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 107 | OS << '\n'; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 108 | } |
Jan Wen Voung | 083cf15 | 2010-10-04 17:32:41 +0000 | [diff] [blame] | 109 | |
| 110 | bool MCSectionCOFF::UseCodeAlign() const { |
| 111 | return getKind().isText(); |
| 112 | } |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 113 | |
| 114 | bool MCSectionCOFF::isVirtualSection() const { |
| 115 | return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; |
| 116 | } |