LLVM  3.7.0
MCSectionELF.cpp
Go to the documentation of this file.
1 //===- 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/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/Support/ELF.h"
17 
18 using namespace llvm;
19 
20 MCSectionELF::~MCSectionELF() {} // anchor.
21 
22 // Decides whether a '.section' directive
23 // should be printed before the section name.
25  const MCAsmInfo &MAI) const {
26 
27  if (isUnique())
28  return false;
29 
30  // FIXME: Does .section .bss/.data/.text work everywhere??
31  if (Name == ".text" || Name == ".data" ||
32  (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
33  return true;
34 
35  return false;
36 }
37 
38 static void printName(raw_ostream &OS, StringRef Name) {
39  if (Name.find_first_not_of("0123456789_."
40  "abcdefghijklmnopqrstuvwxyz"
41  "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
42  OS << Name;
43  return;
44  }
45  OS << '"';
46  for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
47  if (*B == '"') // Unquoted "
48  OS << "\\\"";
49  else if (*B != '\\') // Neither " or backslash
50  OS << *B;
51  else if (B + 1 == E) // Trailing backslash
52  OS << "\\\\";
53  else {
54  OS << B[0] << B[1]; // Quoted character
55  ++B;
56  }
57  }
58  OS << '"';
59 }
60 
62  raw_ostream &OS,
63  const MCExpr *Subsection) const {
64 
65  if (ShouldOmitSectionDirective(SectionName, MAI)) {
66  OS << '\t' << getSectionName();
67  if (Subsection) {
68  OS << '\t';
69  Subsection->print(OS, &MAI);
70  }
71  OS << '\n';
72  return;
73  }
74 
75  OS << "\t.section\t";
77 
78  // Handle the weird solaris syntax if desired.
80  !(Flags & ELF::SHF_MERGE)) {
81  if (Flags & ELF::SHF_ALLOC)
82  OS << ",#alloc";
84  OS << ",#execinstr";
85  if (Flags & ELF::SHF_WRITE)
86  OS << ",#write";
87  if (Flags & ELF::SHF_EXCLUDE)
88  OS << ",#exclude";
89  if (Flags & ELF::SHF_TLS)
90  OS << ",#tls";
91  OS << '\n';
92  return;
93  }
94 
95  OS << ",\"";
96  if (Flags & ELF::SHF_ALLOC)
97  OS << 'a';
98  if (Flags & ELF::SHF_EXCLUDE)
99  OS << 'e';
101  OS << 'x';
102  if (Flags & ELF::SHF_GROUP)
103  OS << 'G';
104  if (Flags & ELF::SHF_WRITE)
105  OS << 'w';
106  if (Flags & ELF::SHF_MERGE)
107  OS << 'M';
108  if (Flags & ELF::SHF_STRINGS)
109  OS << 'S';
110  if (Flags & ELF::SHF_TLS)
111  OS << 'T';
112 
113  // If there are target-specific flags, print them.
115  OS << 'c';
117  OS << 'd';
118 
119  OS << '"';
120 
121  OS << ',';
122 
123  // If comment string is '@', e.g. as on ARM - use '%' instead
124  if (MAI.getCommentString()[0] == '@')
125  OS << '%';
126  else
127  OS << '@';
128 
129  if (Type == ELF::SHT_INIT_ARRAY)
130  OS << "init_array";
131  else if (Type == ELF::SHT_FINI_ARRAY)
132  OS << "fini_array";
133  else if (Type == ELF::SHT_PREINIT_ARRAY)
134  OS << "preinit_array";
135  else if (Type == ELF::SHT_NOBITS)
136  OS << "nobits";
137  else if (Type == ELF::SHT_NOTE)
138  OS << "note";
139  else if (Type == ELF::SHT_PROGBITS)
140  OS << "progbits";
141 
142  if (EntrySize) {
143  assert(Flags & ELF::SHF_MERGE);
144  OS << "," << EntrySize;
145  }
146 
147  if (Flags & ELF::SHF_GROUP) {
148  OS << ",";
149  printName(OS, Group->getName());
150  OS << ",comdat";
151  }
152 
153  if (isUnique())
154  OS << ",unique," << UniqueID;
155 
156  OS << '\n';
157 
158  if (Subsection) {
159  OS << "\t.subsection\t";
160  Subsection->print(OS, &MAI);
161  OS << '\n';
162  }
163 }
164 
166  return getFlags() & ELF::SHF_EXECINSTR;
167 }
168 
170  return getType() == ELF::SHT_NOBITS;
171 }
bool isUnique() const
Definition: MCSectionELF.h:86
StringRef getSectionName() const
Definition: MCSectionELF.h:75
bool UseCodeAlign() const override
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
static void printName(raw_ostream &OS, StringRef Name)
unsigned getFlags() const
Definition: MCSectionELF.h:77
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
iterator begin() const
Definition: StringRef.h:90
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:58
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
Definition: MCExpr.cpp:33
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
bool isVirtualSection() const override
Check whether this section is "virtual", that is has no actual object file contents.
bool usesSunStyleELFSectionSwitchSyntax() const
Definition: MCAsmInfo.h:417
size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: StringRef.cpp:212
bool usesELFSectionDirectiveForBSS() const
Definition: MCAsmInfo.h:421
bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const
ShouldOmitSectionDirective - Decides whether a '.section' directive should be printed before the sect...
void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS, const MCExpr *Subsection) const override
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
static const size_t npos
Definition: StringRef.h:44
XCORE_SHF_CP_SECTION - All sections with the "c" flag are grouped together by the linker to form the ...
Definition: Support/ELF.h:702
iterator end() const
Definition: StringRef.h:92
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
unsigned getType() const
Definition: MCSectionELF.h:76
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
const char * getCommentString() const
Definition: MCAsmInfo.h:445
XCORE_SHF_DP_SECTION - All sections with the "d" flag are grouped together by the linker to form the ...
Definition: Support/ELF.h:707