LLVM API Documentation
00001 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/MC/MCSectionELF.h" 00011 #include "llvm/MC/MCContext.h" 00012 #include "llvm/Support/raw_ostream.h" 00013 #include "llvm/Target/TargetAsmInfo.h" 00014 00015 using namespace llvm; 00016 00017 MCSectionELF *MCSectionELF:: 00018 Create(const StringRef &Section, unsigned Type, unsigned Flags, 00019 SectionKind K, bool isExplicit, MCContext &Ctx) { 00020 return new (Ctx) MCSectionELF(Section, Type, Flags, K, isExplicit); 00021 } 00022 00023 // ShouldOmitSectionDirective - Decides whether a '.section' directive 00024 // should be printed before the section name 00025 bool MCSectionELF::ShouldOmitSectionDirective(const char *Name, 00026 const TargetAsmInfo &TAI) const { 00027 00028 // FIXME: Does .section .bss/.data/.text work everywhere?? 00029 if (strcmp(Name, ".text") == 0 || 00030 strcmp(Name, ".data") == 0 || 00031 (strcmp(Name, ".bss") == 0 && 00032 !TAI.usesELFSectionDirectiveForBSS())) 00033 return true; 00034 00035 return false; 00036 } 00037 00038 // ShouldPrintSectionType - Only prints the section type if supported 00039 bool MCSectionELF::ShouldPrintSectionType(unsigned Ty) const { 00040 00041 if (IsExplicit && !(Ty == SHT_NOBITS || Ty == SHT_PROGBITS)) 00042 return false; 00043 00044 return true; 00045 } 00046 00047 void MCSectionELF::PrintSwitchToSection(const TargetAsmInfo &TAI, 00048 raw_ostream &OS) const { 00049 00050 if (ShouldOmitSectionDirective(SectionName.c_str(), TAI)) { 00051 OS << '\t' << getSectionName() << '\n'; 00052 return; 00053 } 00054 00055 OS << "\t.section\t" << getSectionName(); 00056 00057 // Handle the weird solaris syntax if desired. 00058 if (TAI.usesSunStyleELFSectionSwitchSyntax() && 00059 !(Flags & MCSectionELF::SHF_MERGE)) { 00060 if (Flags & MCSectionELF::SHF_ALLOC) 00061 OS << ",#alloc"; 00062 if (Flags & MCSectionELF::SHF_EXECINSTR) 00063 OS << ",#execinstr"; 00064 if (Flags & MCSectionELF::SHF_WRITE) 00065 OS << ",#write"; 00066 if (Flags & MCSectionELF::SHF_TLS) 00067 OS << ",#tls"; 00068 } else { 00069 OS << ",\""; 00070 if (Flags & MCSectionELF::SHF_ALLOC) 00071 OS << 'a'; 00072 if (Flags & MCSectionELF::SHF_EXECINSTR) 00073 OS << 'x'; 00074 if (Flags & MCSectionELF::SHF_WRITE) 00075 OS << 'w'; 00076 if (Flags & MCSectionELF::SHF_MERGE) 00077 OS << 'M'; 00078 if (Flags & MCSectionELF::SHF_STRINGS) 00079 OS << 'S'; 00080 if (Flags & MCSectionELF::SHF_TLS) 00081 OS << 'T'; 00082 00083 // If there are target-specific flags, print them. 00084 if (Flags & ~MCSectionELF::TARGET_INDEP_SHF) 00085 PrintTargetSpecificSectionFlags(TAI, OS); 00086 00087 OS << '"'; 00088 00089 if (ShouldPrintSectionType(Type)) { 00090 OS << ','; 00091 00092 // If comment string is '@', e.g. as on ARM - use '%' instead 00093 if (TAI.getCommentString()[0] == '@') 00094 OS << '%'; 00095 else 00096 OS << '@'; 00097 00098 if (Type == MCSectionELF::SHT_INIT_ARRAY) 00099 OS << "init_array"; 00100 else if (Type == MCSectionELF::SHT_FINI_ARRAY) 00101 OS << "fini_array"; 00102 else if (Type == MCSectionELF::SHT_PREINIT_ARRAY) 00103 OS << "preinit_array"; 00104 else if (Type == MCSectionELF::SHT_NOBITS) 00105 OS << "nobits"; 00106 else if (Type == MCSectionELF::SHT_PROGBITS) 00107 OS << "progbits"; 00108 00109 if (getKind().isMergeable1ByteCString()) { 00110 OS << ",1"; 00111 } else if (getKind().isMergeable2ByteCString()) { 00112 OS << ",2"; 00113 } else if (getKind().isMergeable4ByteCString() || 00114 getKind().isMergeableConst4()) { 00115 OS << ",4"; 00116 } else if (getKind().isMergeableConst8()) { 00117 OS << ",8"; 00118 } else if (getKind().isMergeableConst16()) { 00119 OS << ",16"; 00120 } 00121 } 00122 } 00123 00124 OS << '\n'; 00125 } 00126 00127 // HasCommonSymbols - True if this section holds common symbols, this is 00128 // indicated on the ELF object file by a symbol with SHN_COMMON section 00129 // header index. 00130 bool MCSectionELF::HasCommonSymbols() const { 00131 00132 if (strncmp(SectionName.c_str(), ".gnu.linkonce.", 14) == 0) 00133 return true; 00134 00135 return false; 00136 } 00137 00138