blob: 660a8c9489f0b115c1ab9a1bbb62ed3ba0fe1813 [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"
11#include "llvm/MC/MCContext.h"
12#include "llvm/Support/raw_ostream.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000013#include "llvm/MC/MCAsmInfo.h"
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000014
15using namespace llvm;
16
17MCSectionELF *MCSectionELF::
18Create(const 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
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000025bool MCSectionELF::ShouldOmitSectionDirective(const char *Name,
Chris Lattner33adcfb2009-08-22 21:43:10 +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??
Dan Gohman86cba322009-08-13 23:56:34 +000029 if (strcmp(Name, ".text") == 0 ||
30 strcmp(Name, ".data") == 0 ||
31 (strcmp(Name, ".bss") == 0 &&
Chris Lattner33adcfb2009-08-22 21:43:10 +000032 !MAI.usesELFSectionDirectiveForBSS()))
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000033 return true;
34
35 return false;
36}
37
38// ShouldPrintSectionType - Only prints the section type if supported
39bool MCSectionELF::ShouldPrintSectionType(unsigned Ty) const {
40
41 if (IsExplicit && !(Ty == SHT_NOBITS || Ty == SHT_PROGBITS))
42 return false;
43
44 return true;
45}
46
Chris Lattner33adcfb2009-08-22 21:43:10 +000047void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000048 raw_ostream &OS) const {
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000049
Chris Lattner33adcfb2009-08-22 21:43:10 +000050 if (ShouldOmitSectionDirective(SectionName.c_str(), MAI)) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000051 OS << '\t' << getSectionName() << '\n';
52 return;
53 }
54
55 OS << "\t.section\t" << getSectionName();
56
57 // Handle the weird solaris syntax if desired.
Chris Lattner33adcfb2009-08-22 21:43:10 +000058 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000059 !(Flags & MCSectionELF::SHF_MERGE)) {
60 if (Flags & MCSectionELF::SHF_ALLOC)
61 OS << ",#alloc";
62 if (Flags & MCSectionELF::SHF_EXECINSTR)
63 OS << ",#execinstr";
64 if (Flags & MCSectionELF::SHF_WRITE)
65 OS << ",#write";
66 if (Flags & MCSectionELF::SHF_TLS)
67 OS << ",#tls";
68 } else {
69 OS << ",\"";
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000070 if (Flags & MCSectionELF::SHF_ALLOC)
71 OS << 'a';
72 if (Flags & MCSectionELF::SHF_EXECINSTR)
73 OS << 'x';
74 if (Flags & MCSectionELF::SHF_WRITE)
75 OS << 'w';
76 if (Flags & MCSectionELF::SHF_MERGE)
77 OS << 'M';
78 if (Flags & MCSectionELF::SHF_STRINGS)
79 OS << 'S';
80 if (Flags & MCSectionELF::SHF_TLS)
81 OS << 'T';
Chris Lattner7d996d92009-08-15 05:56:11 +000082
83 // If there are target-specific flags, print them.
84 if (Flags & ~MCSectionELF::TARGET_INDEP_SHF)
Chris Lattner33adcfb2009-08-22 21:43:10 +000085 PrintTargetSpecificSectionFlags(MAI, OS);
Chris Lattner7d996d92009-08-15 05:56:11 +000086
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000087 OS << '"';
88
89 if (ShouldPrintSectionType(Type)) {
90 OS << ',';
91
92 // If comment string is '@', e.g. as on ARM - use '%' instead
Chris Lattner33adcfb2009-08-22 21:43:10 +000093 if (MAI.getCommentString()[0] == '@')
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000094 OS << '%';
95 else
96 OS << '@';
97
98 if (Type == MCSectionELF::SHT_INIT_ARRAY)
99 OS << "init_array";
100 else if (Type == MCSectionELF::SHT_FINI_ARRAY)
101 OS << "fini_array";
102 else if (Type == MCSectionELF::SHT_PREINIT_ARRAY)
103 OS << "preinit_array";
104 else if (Type == MCSectionELF::SHT_NOBITS)
105 OS << "nobits";
106 else if (Type == MCSectionELF::SHT_PROGBITS)
107 OS << "progbits";
108
109 if (getKind().isMergeable1ByteCString()) {
110 OS << ",1";
111 } else if (getKind().isMergeable2ByteCString()) {
112 OS << ",2";
113 } else if (getKind().isMergeable4ByteCString() ||
114 getKind().isMergeableConst4()) {
115 OS << ",4";
116 } else if (getKind().isMergeableConst8()) {
117 OS << ",8";
118 } else if (getKind().isMergeableConst16()) {
119 OS << ",16";
120 }
121 }
122 }
123
124 OS << '\n';
125}
126
Bruno Cardoso Lopesabd75142009-08-14 19:45:38 +0000127// HasCommonSymbols - True if this section holds common symbols, this is
128// indicated on the ELF object file by a symbol with SHN_COMMON section
129// header index.
130bool MCSectionELF::HasCommonSymbols() const {
Bruno Cardoso Lopes4aa688e2009-08-13 21:08:56 +0000131
132 if (strncmp(SectionName.c_str(), ".gnu.linkonce.", 14) == 0)
133 return true;
134
135 return false;
136}
137
138