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() {} // anchor.
19 // ShouldOmitSectionDirective - Decides whether a '.section' directive
20 // should be printed before the section name
21 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
22 const MCAsmInfo &MAI) const {
24 // FIXME: Does .section .bss/.data/.text work everywhere??
25 if (Name == ".text" || Name == ".data" ||
26 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
32 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
33 raw_ostream &OS) const {
35 if (ShouldOmitSectionDirective(SectionName, MAI)) {
36 OS << '\t' << getSectionName() << '\n';
40 OS << "\t.section\t" << getSectionName();
42 // Handle the weird solaris syntax if desired.
43 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
44 !(Flags & MCSectionELF::SHF_MERGE)) {
45 if (Flags & MCSectionELF::SHF_ALLOC)
47 if (Flags & MCSectionELF::SHF_EXECINSTR)
49 if (Flags & MCSectionELF::SHF_WRITE)
51 if (Flags & MCSectionELF::SHF_TLS)
58 if (Flags & MCSectionELF::SHF_ALLOC)
60 if (Flags & MCSectionELF::SHF_EXECINSTR)
62 if (Flags & MCSectionELF::SHF_WRITE)
64 if (Flags & MCSectionELF::SHF_MERGE)
66 if (Flags & MCSectionELF::SHF_STRINGS)
68 if (Flags & MCSectionELF::SHF_TLS)
71 // If there are target-specific flags, print them.
72 if (Flags & MCSectionELF::XCORE_SHF_CP_SECTION)
74 if (Flags & MCSectionELF::XCORE_SHF_DP_SECTION)
81 // If comment string is '@', e.g. as on ARM - use '%' instead
82 if (MAI.getCommentString()[0] == '@')
87 if (Type == MCSectionELF::SHT_INIT_ARRAY)
89 else if (Type == MCSectionELF::SHT_FINI_ARRAY)
91 else if (Type == MCSectionELF::SHT_PREINIT_ARRAY)
92 OS << "preinit_array";
93 else if (Type == MCSectionELF::SHT_NOBITS)
95 else if (Type == MCSectionELF::SHT_PROGBITS)
99 assert(Flags & MCSectionELF::SHF_MERGE);
100 OS << "," << EntrySize;
106 bool MCSectionELF::UseCodeAlign() const {
107 return getFlags() & MCSectionELF::SHF_EXECINSTR;
110 bool MCSectionELF::isVirtualSection() const {
111 return getType() == MCSectionELF::SHT_NOBITS;
114 // HasCommonSymbols - True if this section holds common symbols, this is
115 // indicated on the ELF object file by a symbol with SHN_COMMON section
117 bool MCSectionELF::HasCommonSymbols() const {
119 if (StringRef(SectionName).startswith(".gnu.linkonce."))
125 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
126 if (Kind.isMergeable1ByteCString()) return 1;
127 if (Kind.isMergeable2ByteCString()) return 2;
128 if (Kind.isMergeable4ByteCString()) return 4;
129 if (Kind.isMergeableConst4()) return 4;
130 if (Kind.isMergeableConst8()) return 8;
131 if (Kind.isMergeableConst16()) return 16;