Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCSectionELF.cpp - ELF 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/MCSectionELF.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | 4d2419d | 2010-01-13 21:21:29 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCContext.h" |
| 13 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame^] | 14 | #include "llvm/Support/ELF.h" |
Chris Lattner | 4d2419d | 2010-01-13 21:21:29 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame^] | 16 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 19 | MCSectionELF::~MCSectionELF() {} // anchor. |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 20 | |
| 21 | // ShouldOmitSectionDirective - Decides whether a '.section' directive |
| 22 | // should be printed before the section name |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame] | 23 | bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name, |
Chris Lattner | 4d2419d | 2010-01-13 21:21:29 +0000 | [diff] [blame] | 24 | const MCAsmInfo &MAI) const { |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 25 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 26 | // FIXME: Does .section .bss/.data/.text work everywhere?? |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame] | 27 | if (Name == ".text" || Name == ".data" || |
| 28 | (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS())) |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 29 | return true; |
| 30 | |
| 31 | return false; |
| 32 | } |
| 33 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 34 | void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 35 | raw_ostream &OS) const { |
Bruno Cardoso Lopes | fdf229e | 2009-08-13 23:30:21 +0000 | [diff] [blame] | 36 | |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame] | 37 | if (ShouldOmitSectionDirective(SectionName, MAI)) { |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 38 | OS << '\t' << getSectionName() << '\n'; |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | OS << "\t.section\t" << getSectionName(); |
| 43 | |
| 44 | // Handle the weird solaris syntax if desired. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 45 | if (MAI.usesSunStyleELFSectionSwitchSyntax() && |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 46 | !(Flags & MCSectionELF::SHF_MERGE)) { |
| 47 | if (Flags & MCSectionELF::SHF_ALLOC) |
| 48 | OS << ",#alloc"; |
| 49 | if (Flags & MCSectionELF::SHF_EXECINSTR) |
| 50 | OS << ",#execinstr"; |
| 51 | if (Flags & MCSectionELF::SHF_WRITE) |
| 52 | OS << ",#write"; |
| 53 | if (Flags & MCSectionELF::SHF_TLS) |
| 54 | OS << ",#tls"; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 55 | OS << '\n'; |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | OS << ",\""; |
| 60 | if (Flags & MCSectionELF::SHF_ALLOC) |
| 61 | OS << 'a'; |
| 62 | if (Flags & MCSectionELF::SHF_EXECINSTR) |
| 63 | OS << 'x'; |
| 64 | if (Flags & MCSectionELF::SHF_WRITE) |
| 65 | OS << 'w'; |
| 66 | if (Flags & MCSectionELF::SHF_MERGE) |
| 67 | OS << 'M'; |
| 68 | if (Flags & MCSectionELF::SHF_STRINGS) |
| 69 | OS << 'S'; |
| 70 | if (Flags & MCSectionELF::SHF_TLS) |
| 71 | OS << 'T'; |
| 72 | |
| 73 | // If there are target-specific flags, print them. |
| 74 | if (Flags & MCSectionELF::XCORE_SHF_CP_SECTION) |
| 75 | OS << 'c'; |
| 76 | if (Flags & MCSectionELF::XCORE_SHF_DP_SECTION) |
| 77 | OS << 'd'; |
| 78 | |
| 79 | OS << '"'; |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 80 | |
Rafael Espindola | 34be396 | 2010-11-09 23:42:07 +0000 | [diff] [blame] | 81 | OS << ','; |
| 82 | |
| 83 | // If comment string is '@', e.g. as on ARM - use '%' instead |
| 84 | if (MAI.getCommentString()[0] == '@') |
| 85 | OS << '%'; |
| 86 | else |
| 87 | OS << '@'; |
| 88 | |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame^] | 89 | if (Type == ELF::SHT_INIT_ARRAY) |
Rafael Espindola | 34be396 | 2010-11-09 23:42:07 +0000 | [diff] [blame] | 90 | OS << "init_array"; |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame^] | 91 | else if (Type == ELF::SHT_FINI_ARRAY) |
Rafael Espindola | 34be396 | 2010-11-09 23:42:07 +0000 | [diff] [blame] | 92 | OS << "fini_array"; |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame^] | 93 | else if (Type == ELF::SHT_PREINIT_ARRAY) |
Rafael Espindola | 34be396 | 2010-11-09 23:42:07 +0000 | [diff] [blame] | 94 | OS << "preinit_array"; |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame^] | 95 | else if (Type == ELF::SHT_NOBITS) |
Rafael Espindola | 34be396 | 2010-11-09 23:42:07 +0000 | [diff] [blame] | 96 | OS << "nobits"; |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame^] | 97 | else if (Type == ELF::SHT_NOTE) |
Rafael Espindola | 9897661 | 2010-12-26 21:30:59 +0000 | [diff] [blame] | 98 | OS << "note"; |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame^] | 99 | else if (Type == ELF::SHT_PROGBITS) |
Rafael Espindola | 34be396 | 2010-11-09 23:42:07 +0000 | [diff] [blame] | 100 | OS << "progbits"; |
| 101 | |
| 102 | if (EntrySize) { |
| 103 | assert(Flags & MCSectionELF::SHF_MERGE); |
| 104 | OS << "," << EntrySize; |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 105 | } |
Rafael Espindola | 34be396 | 2010-11-09 23:42:07 +0000 | [diff] [blame] | 106 | |
Bruno Cardoso Lopes | b808588 | 2009-08-13 05:07:35 +0000 | [diff] [blame] | 107 | OS << '\n'; |
| 108 | } |
| 109 | |
Jan Wen Voung | 083cf15 | 2010-10-04 17:32:41 +0000 | [diff] [blame] | 110 | bool MCSectionELF::UseCodeAlign() const { |
| 111 | return getFlags() & MCSectionELF::SHF_EXECINSTR; |
| 112 | } |
| 113 | |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 114 | bool MCSectionELF::isVirtualSection() const { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame^] | 115 | return getType() == ELF::SHT_NOBITS; |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Bruno Cardoso Lopes | abd7514 | 2009-08-14 19:45:38 +0000 | [diff] [blame] | 118 | // HasCommonSymbols - True if this section holds common symbols, this is |
| 119 | // indicated on the ELF object file by a symbol with SHN_COMMON section |
| 120 | // header index. |
| 121 | bool MCSectionELF::HasCommonSymbols() const { |
Bruno Cardoso Lopes | 4aa688e | 2009-08-13 21:08:56 +0000 | [diff] [blame] | 122 | |
Benjamin Kramer | a46918d | 2010-01-22 18:21:23 +0000 | [diff] [blame] | 123 | if (StringRef(SectionName).startswith(".gnu.linkonce.")) |
Bruno Cardoso Lopes | 4aa688e | 2009-08-13 21:08:56 +0000 | [diff] [blame] | 124 | return true; |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | |
Jan Wen Voung | 186e7a0 | 2010-09-30 05:59:22 +0000 | [diff] [blame] | 129 | unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) { |
| 130 | if (Kind.isMergeable1ByteCString()) return 1; |
| 131 | if (Kind.isMergeable2ByteCString()) return 2; |
| 132 | if (Kind.isMergeable4ByteCString()) return 4; |
| 133 | if (Kind.isMergeableConst4()) return 4; |
| 134 | if (Kind.isMergeableConst8()) return 8; |
| 135 | if (Kind.isMergeableConst16()) return 16; |
| 136 | return 0; |
| 137 | } |