blob: ebfe2691719ca0d5cac02a362e1068b2b90d6c35 [file] [log] [blame]
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +00001//===- 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 Lattneraf76e592009-08-22 20:48:53 +000011#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2419d2010-01-13 21:21:29 +000012#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCSymbol.h"
14#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000015using namespace llvm;
16
17MCSectionELF *MCSectionELF::
Daniel Dunbar2928c832009-11-06 10:58:06 +000018Create(StringRef Section, unsigned Type, unsigned Flags,
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000019 SectionKind K, bool isExplicit, MCContext &Ctx) {
Chris Lattner7d996d92009-08-15 05:56:11 +000020 return new (Ctx) MCSectionELF(Section, Type, Flags, K, isExplicit);
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000021}
22
23// ShouldOmitSectionDirective - Decides whether a '.section' directive
24// should be printed before the section name
Benjamin Kramera46918d2010-01-22 18:21:23 +000025bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
Chris Lattner4d2419d2010-01-13 21:21:29 +000026 const MCAsmInfo &MAI) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000027
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000028 // FIXME: Does .section .bss/.data/.text work everywhere??
Benjamin Kramera46918d2010-01-22 18:21:23 +000029 if (Name == ".text" || Name == ".data" ||
30 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000031 return true;
32
33 return false;
34}
35
36// ShouldPrintSectionType - Only prints the section type if supported
37bool MCSectionELF::ShouldPrintSectionType(unsigned Ty) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000038 if (IsExplicit && !(Ty == SHT_NOBITS || Ty == SHT_PROGBITS))
39 return false;
40
41 return true;
42}
43
Chris Lattner33adcfb2009-08-22 21:43:10 +000044void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000045 raw_ostream &OS) const {
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000046
Benjamin Kramera46918d2010-01-22 18:21:23 +000047 if (ShouldOmitSectionDirective(SectionName, MAI)) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000048 OS << '\t' << getSectionName() << '\n';
49 return;
50 }
51
52 OS << "\t.section\t" << getSectionName();
53
54 // Handle the weird solaris syntax if desired.
Chris Lattner33adcfb2009-08-22 21:43:10 +000055 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000056 !(Flags & MCSectionELF::SHF_MERGE)) {
57 if (Flags & MCSectionELF::SHF_ALLOC)
58 OS << ",#alloc";
59 if (Flags & MCSectionELF::SHF_EXECINSTR)
60 OS << ",#execinstr";
61 if (Flags & MCSectionELF::SHF_WRITE)
62 OS << ",#write";
63 if (Flags & MCSectionELF::SHF_TLS)
64 OS << ",#tls";
65 } else {
66 OS << ",\"";
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000067 if (Flags & MCSectionELF::SHF_ALLOC)
68 OS << 'a';
69 if (Flags & MCSectionELF::SHF_EXECINSTR)
70 OS << 'x';
71 if (Flags & MCSectionELF::SHF_WRITE)
72 OS << 'w';
73 if (Flags & MCSectionELF::SHF_MERGE)
74 OS << 'M';
75 if (Flags & MCSectionELF::SHF_STRINGS)
76 OS << 'S';
77 if (Flags & MCSectionELF::SHF_TLS)
78 OS << 'T';
Chris Lattner7d996d92009-08-15 05:56:11 +000079
80 // If there are target-specific flags, print them.
81 if (Flags & ~MCSectionELF::TARGET_INDEP_SHF)
Chris Lattner33adcfb2009-08-22 21:43:10 +000082 PrintTargetSpecificSectionFlags(MAI, OS);
Chris Lattner7d996d92009-08-15 05:56:11 +000083
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000084 OS << '"';
85
86 if (ShouldPrintSectionType(Type)) {
87 OS << ',';
88
89 // If comment string is '@', e.g. as on ARM - use '%' instead
Chris Lattner33adcfb2009-08-22 21:43:10 +000090 if (MAI.getCommentString()[0] == '@')
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000091 OS << '%';
92 else
93 OS << '@';
94
95 if (Type == MCSectionELF::SHT_INIT_ARRAY)
96 OS << "init_array";
97 else if (Type == MCSectionELF::SHT_FINI_ARRAY)
98 OS << "fini_array";
99 else if (Type == MCSectionELF::SHT_PREINIT_ARRAY)
100 OS << "preinit_array";
101 else if (Type == MCSectionELF::SHT_NOBITS)
102 OS << "nobits";
103 else if (Type == MCSectionELF::SHT_PROGBITS)
104 OS << "progbits";
105
106 if (getKind().isMergeable1ByteCString()) {
107 OS << ",1";
108 } else if (getKind().isMergeable2ByteCString()) {
109 OS << ",2";
110 } else if (getKind().isMergeable4ByteCString() ||
111 getKind().isMergeableConst4()) {
112 OS << ",4";
113 } else if (getKind().isMergeableConst8()) {
114 OS << ",8";
115 } else if (getKind().isMergeableConst16()) {
116 OS << ",16";
117 }
118 }
119 }
120
121 OS << '\n';
122}
123
Bruno Cardoso Lopesabd75142009-08-14 19:45:38 +0000124// HasCommonSymbols - True if this section holds common symbols, this is
125// indicated on the ELF object file by a symbol with SHN_COMMON section
126// header index.
127bool MCSectionELF::HasCommonSymbols() const {
Bruno Cardoso Lopes4aa688e2009-08-13 21:08:56 +0000128
Benjamin Kramera46918d2010-01-22 18:21:23 +0000129 if (StringRef(SectionName).startswith(".gnu.linkonce."))
Bruno Cardoso Lopes4aa688e2009-08-13 21:08:56 +0000130 return true;
131
132 return false;
133}
134
135