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