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