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/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCSymbol.h"
14 #include "llvm/Support/raw_ostream.h"
17 MCSectionELF *MCSectionELF::
18 Create(StringRef Section, unsigned Type, unsigned Flags,
19 SectionKind K, bool isExplicit, MCContext &Ctx) {
20 return new (Ctx) MCSectionELF(Section, Type, Flags, K, isExplicit);
23 // ShouldOmitSectionDirective - Decides whether a '.section' directive
24 // should be printed before the section name
25 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
26 const MCAsmInfo &MAI) const {
28 // FIXME: Does .section .bss/.data/.text work everywhere??
29 if (Name == ".text" || Name == ".data" ||
30 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
36 // ShouldPrintSectionType - Only prints the section type if supported
37 bool MCSectionELF::ShouldPrintSectionType(unsigned Ty) const {
38 if (IsExplicit && !(Ty == SHT_NOBITS || Ty == SHT_PROGBITS))
44 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
45 raw_ostream &OS) const {
47 if (ShouldOmitSectionDirective(SectionName, MAI)) {
48 OS << '\t' << getSectionName() << '\n';
52 OS << "\t.section\t" << getSectionName();
54 // Handle the weird solaris syntax if desired.
55 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
56 !(Flags & MCSectionELF::SHF_MERGE)) {
57 if (Flags & MCSectionELF::SHF_ALLOC)
59 if (Flags & MCSectionELF::SHF_EXECINSTR)
61 if (Flags & MCSectionELF::SHF_WRITE)
63 if (Flags & MCSectionELF::SHF_TLS)
67 if (Flags & MCSectionELF::SHF_ALLOC)
69 if (Flags & MCSectionELF::SHF_EXECINSTR)
71 if (Flags & MCSectionELF::SHF_WRITE)
73 if (Flags & MCSectionELF::SHF_MERGE)
75 if (Flags & MCSectionELF::SHF_STRINGS)
77 if (Flags & MCSectionELF::SHF_TLS)
80 // If there are target-specific flags, print them.
81 if (Flags & ~MCSectionELF::TARGET_INDEP_SHF)
82 PrintTargetSpecificSectionFlags(MAI, OS);
86 if (ShouldPrintSectionType(Type)) {
89 // If comment string is '@', e.g. as on ARM - use '%' instead
90 if (MAI.getCommentString()[0] == '@')
95 if (Type == MCSectionELF::SHT_INIT_ARRAY)
97 else if (Type == MCSectionELF::SHT_FINI_ARRAY)
99 else if (Type == MCSectionELF::SHT_PREINIT_ARRAY)
100 OS << "preinit_array";
101 else if (Type == MCSectionELF::SHT_NOBITS)
103 else if (Type == MCSectionELF::SHT_PROGBITS)
106 if (getKind().isMergeable1ByteCString()) {
108 } else if (getKind().isMergeable2ByteCString()) {
110 } else if (getKind().isMergeable4ByteCString() ||
111 getKind().isMergeableConst4()) {
113 } else if (getKind().isMergeableConst8()) {
115 } else if (getKind().isMergeableConst16()) {
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
127 bool MCSectionELF::HasCommonSymbols() const {
129 if (StringRef(SectionName).startswith(".gnu.linkonce."))