1 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCSectionELF.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "llvm/Target/TargetAsmInfo.h"
17 MCSectionELF *MCSectionELF::
18 Create(const StringRef &Section, unsigned Type, unsigned Flags,
19 SectionKind K, bool isExplicit, MCContext &Ctx) {
21 (Ctx) MCSectionELF(Section, Type, Flags, K, isExplicit);
24 // ShouldOmitSectionDirective - Decides whether a '.section' directive
25 // should be printed before the section name
26 bool MCSectionELF::ShouldOmitSectionDirective(const char *Name,
27 const TargetAsmInfo &TAI) const {
29 // FIXME: Does .section .bss/.data/.text work everywhere??
30 if (strcmp(Name, ".text") == 0 ||
31 strcmp(Name, ".data") == 0 ||
32 (strcmp(Name, ".bss") == 0 &&
33 !TAI.usesELFSectionDirectiveForBSS()))
39 // ShouldPrintSectionType - Only prints the section type if supported
40 bool MCSectionELF::ShouldPrintSectionType(unsigned Ty) const {
42 if (IsExplicit && !(Ty == SHT_NOBITS || Ty == SHT_PROGBITS))
48 void MCSectionELF::PrintSwitchToSection(const TargetAsmInfo &TAI,
49 raw_ostream &OS) const {
51 if (ShouldOmitSectionDirective(SectionName.c_str(), TAI)) {
52 OS << '\t' << getSectionName() << '\n';
56 OS << "\t.section\t" << getSectionName();
58 // Handle the weird solaris syntax if desired.
59 if (TAI.usesSunStyleELFSectionSwitchSyntax() &&
60 !(Flags & MCSectionELF::SHF_MERGE)) {
61 if (Flags & MCSectionELF::SHF_ALLOC)
63 if (Flags & MCSectionELF::SHF_EXECINSTR)
65 if (Flags & MCSectionELF::SHF_WRITE)
67 if (Flags & MCSectionELF::SHF_TLS)
72 if (Flags & MCSectionELF::SHF_ALLOC)
74 if (Flags & MCSectionELF::SHF_EXECINSTR)
76 if (Flags & MCSectionELF::SHF_WRITE)
78 if (Flags & MCSectionELF::SHF_MERGE)
80 if (Flags & MCSectionELF::SHF_STRINGS)
82 if (Flags & MCSectionELF::SHF_TLS)
87 if (ShouldPrintSectionType(Type)) {
90 // If comment string is '@', e.g. as on ARM - use '%' instead
91 if (TAI.getCommentString()[0] == '@')
96 if (Type == MCSectionELF::SHT_INIT_ARRAY)
98 else if (Type == MCSectionELF::SHT_FINI_ARRAY)
100 else if (Type == MCSectionELF::SHT_PREINIT_ARRAY)
101 OS << "preinit_array";
102 else if (Type == MCSectionELF::SHT_NOBITS)
104 else if (Type == MCSectionELF::SHT_PROGBITS)
107 if (getKind().isMergeable1ByteCString()) {
109 } else if (getKind().isMergeable2ByteCString()) {
111 } else if (getKind().isMergeable4ByteCString() ||
112 getKind().isMergeableConst4()) {
114 } else if (getKind().isMergeableConst8()) {
116 } else if (getKind().isMergeableConst16()) {
125 // HasCommonSymbols - True if this section holds common symbols, this is
126 // indicated on the ELF object file by a symbol with SHN_COMMON section
128 bool MCSectionELF::HasCommonSymbols() const {
130 if (strncmp(SectionName.c_str(), ".gnu.linkonce.", 14) == 0)