LLVM  4.0.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 
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  return MAI.shouldOmitSectionDirective(Name);
31 }
32 
33 static void printName(raw_ostream &OS, StringRef Name) {
34  if (Name.find_first_not_of("0123456789_."
35  "abcdefghijklmnopqrstuvwxyz"
36  "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
37  OS << Name;
38  return;
39  }
40  OS << '"';
41  for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
42  if (*B == '"') // Unquoted "
43  OS << "\\\"";
44  else if (*B != '\\') // Neither " or backslash
45  OS << *B;
46  else if (B + 1 == E) // Trailing backslash
47  OS << "\\\\";
48  else {
49  OS << B[0] << B[1]; // Quoted character
50  ++B;
51  }
52  }
53  OS << '"';
54 }
55 
57  raw_ostream &OS,
58  const MCExpr *Subsection) const {
59 
61  OS << '\t' << getSectionName();
62  if (Subsection) {
63  OS << '\t';
64  Subsection->print(OS, &MAI);
65  }
66  OS << '\n';
67  return;
68  }
69 
70  OS << "\t.section\t";
72 
73  // Handle the weird solaris syntax if desired.
75  !(Flags & ELF::SHF_MERGE)) {
76  if (Flags & ELF::SHF_ALLOC)
77  OS << ",#alloc";
79  OS << ",#execinstr";
80  if (Flags & ELF::SHF_WRITE)
81  OS << ",#write";
82  if (Flags & ELF::SHF_EXCLUDE)
83  OS << ",#exclude";
84  if (Flags & ELF::SHF_TLS)
85  OS << ",#tls";
86  OS << '\n';
87  return;
88  }
89 
90  OS << ",\"";
91  if (Flags & ELF::SHF_ALLOC)
92  OS << 'a';
93  if (Flags & ELF::SHF_EXCLUDE)
94  OS << 'e';
96  OS << 'x';
97  if (Flags & ELF::SHF_GROUP)
98  OS << 'G';
99  if (Flags & ELF::SHF_WRITE)
100  OS << 'w';
101  if (Flags & ELF::SHF_MERGE)
102  OS << 'M';
103  if (Flags & ELF::SHF_STRINGS)
104  OS << 'S';
105  if (Flags & ELF::SHF_TLS)
106  OS << 'T';
107 
108  // If there are target-specific flags, print them.
110  OS << 'c';
112  OS << 'd';
114  OS << 'y';
115 
116  OS << '"';
117 
118  OS << ',';
119 
120  // If comment string is '@', e.g. as on ARM - use '%' instead
121  if (MAI.getCommentString()[0] == '@')
122  OS << '%';
123  else
124  OS << '@';
125 
126  if (Type == ELF::SHT_INIT_ARRAY)
127  OS << "init_array";
128  else if (Type == ELF::SHT_FINI_ARRAY)
129  OS << "fini_array";
130  else if (Type == ELF::SHT_PREINIT_ARRAY)
131  OS << "preinit_array";
132  else if (Type == ELF::SHT_NOBITS)
133  OS << "nobits";
134  else if (Type == ELF::SHT_NOTE)
135  OS << "note";
136  else if (Type == ELF::SHT_PROGBITS)
137  OS << "progbits";
138  else if (Type == ELF::SHT_X86_64_UNWIND)
139  OS << "unwind";
140 
141  if (EntrySize) {
142  assert(Flags & ELF::SHF_MERGE);
143  OS << "," << EntrySize;
144  }
145 
146  if (Flags & ELF::SHF_GROUP) {
147  OS << ",";
148  printName(OS, Group->getName());
149  OS << ",comdat";
150  }
151 
152  if (isUnique())
153  OS << ",unique," << UniqueID;
154 
155  OS << '\n';
156 
157  if (Subsection) {
158  OS << "\t.subsection\t";
159  Subsection->print(OS, &MAI);
160  OS << '\n';
161  }
162 }
163 
165  return getFlags() & ELF::SHF_EXECINSTR;
166 }
167 
169  return getType() == ELF::SHT_NOBITS;
170 }
bool isUnique() const
Definition: MCSectionELF.h:86
StringRef getSectionName() const
Definition: MCSectionELF.h:74
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:76
XCORE_SHF_CP_SECTION - All sections with the "c" flag are grouped together by the linker to form the ...
Definition: Support/ELF.h:757
StringRef getCommentString() const
Definition: MCAsmInfo.h:471
struct fuzzer::@269 Flags
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
virtual bool shouldOmitSectionDirective(StringRef SectionName) const
Return true if the .section directive should be omitted when emitting SectionName.
Definition: MCAsmInfo.cpp:162
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
iterator begin() const
Definition: StringRef.h:103
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:57
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:446
XCORE_SHF_DP_SECTION - All sections with the "d" flag are grouped together by the linker to form the ...
Definition: Support/ELF.h:762
LLVM_NODISCARD 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:264
bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const
Decides whether a '.section' directive should be printed before the section name. ...
void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS, const MCExpr *Subsection) const override
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:199
static const size_t npos
Definition: StringRef.h:51
const char SectionName[]
Definition: AMDGPUPTNote.h:24
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:33
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator end() const
Definition: StringRef.h:105
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
unsigned getType() const
Definition: MCSectionELF.h:75
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47