blob: 3d8774ceeb27604b0d7ccc9e4f784c515544d71e [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"
13#include "llvm/Target/TargetAsmInfo.h"
14
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) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000020 return new
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000021 (Ctx) MCSectionELF(Section, Type, Flags, K, isExplicit);
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000022}
23
24// ShouldOmitSectionDirective - Decides whether a '.section' directive
25// should be printed before the section name
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000026bool MCSectionELF::ShouldOmitSectionDirective(const char *Name,
27 const TargetAsmInfo &TAI) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000028
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000029 // FIXME: Does .section .bss/.data/.text work everywhere??
Dan Gohman86cba322009-08-13 23:56:34 +000030 if (strcmp(Name, ".text") == 0 ||
31 strcmp(Name, ".data") == 0 ||
32 (strcmp(Name, ".bss") == 0 &&
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000033 !TAI.usesELFSectionDirectiveForBSS()))
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000034 return true;
35
36 return false;
37}
38
39// ShouldPrintSectionType - Only prints the section type if supported
40bool MCSectionELF::ShouldPrintSectionType(unsigned Ty) const {
41
42 if (IsExplicit && !(Ty == SHT_NOBITS || Ty == SHT_PROGBITS))
43 return false;
44
45 return true;
46}
47
48void MCSectionELF::PrintSwitchToSection(const TargetAsmInfo &TAI,
49 raw_ostream &OS) const {
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000050
51 if (ShouldOmitSectionDirective(SectionName.c_str(), TAI)) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000052 OS << '\t' << getSectionName() << '\n';
53 return;
54 }
55
56 OS << "\t.section\t" << getSectionName();
57
58 // Handle the weird solaris syntax if desired.
59 if (TAI.usesSunStyleELFSectionSwitchSyntax() &&
60 !(Flags & MCSectionELF::SHF_MERGE)) {
61 if (Flags & MCSectionELF::SHF_ALLOC)
62 OS << ",#alloc";
63 if (Flags & MCSectionELF::SHF_EXECINSTR)
64 OS << ",#execinstr";
65 if (Flags & MCSectionELF::SHF_WRITE)
66 OS << ",#write";
67 if (Flags & MCSectionELF::SHF_TLS)
68 OS << ",#tls";
69 } else {
70 OS << ",\"";
71
72 if (Flags & MCSectionELF::SHF_ALLOC)
73 OS << 'a';
74 if (Flags & MCSectionELF::SHF_EXECINSTR)
75 OS << 'x';
76 if (Flags & MCSectionELF::SHF_WRITE)
77 OS << 'w';
78 if (Flags & MCSectionELF::SHF_MERGE)
79 OS << 'M';
80 if (Flags & MCSectionELF::SHF_STRINGS)
81 OS << 'S';
82 if (Flags & MCSectionELF::SHF_TLS)
83 OS << 'T';
84
85 OS << '"';
86
87 if (ShouldPrintSectionType(Type)) {
88 OS << ',';
89
90 // If comment string is '@', e.g. as on ARM - use '%' instead
91 if (TAI.getCommentString()[0] == '@')
92 OS << '%';
93 else
94 OS << '@';
95
96 if (Type == MCSectionELF::SHT_INIT_ARRAY)
97 OS << "init_array";
98 else if (Type == MCSectionELF::SHT_FINI_ARRAY)
99 OS << "fini_array";
100 else if (Type == MCSectionELF::SHT_PREINIT_ARRAY)
101 OS << "preinit_array";
102 else if (Type == MCSectionELF::SHT_NOBITS)
103 OS << "nobits";
104 else if (Type == MCSectionELF::SHT_PROGBITS)
105 OS << "progbits";
106
107 if (getKind().isMergeable1ByteCString()) {
108 OS << ",1";
109 } else if (getKind().isMergeable2ByteCString()) {
110 OS << ",2";
111 } else if (getKind().isMergeable4ByteCString() ||
112 getKind().isMergeableConst4()) {
113 OS << ",4";
114 } else if (getKind().isMergeableConst8()) {
115 OS << ",8";
116 } else if (getKind().isMergeableConst16()) {
117 OS << ",16";
118 }
119 }
120 }
121
122 OS << '\n';
123}
124
Bruno Cardoso Lopesabd75142009-08-14 19:45:38 +0000125// HasCommonSymbols - True if this section holds common symbols, this is
126// indicated on the ELF object file by a symbol with SHN_COMMON section
127// header index.
128bool MCSectionELF::HasCommonSymbols() const {
Bruno Cardoso Lopes4aa688e2009-08-13 21:08:56 +0000129
130 if (strncmp(SectionName.c_str(), ".gnu.linkonce.", 14) == 0)
131 return true;
132
133 return false;
134}
135
136