LLVM  8.0.1
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/ADT/Triple.h"
12 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCExpr.h"
17 #include <cassert>
18 
19 using namespace llvm;
20 
21 MCSectionELF::~MCSectionELF() = default; // anchor.
22 
23 // Decides whether a '.section' directive
24 // should be printed before the section name.
26  const MCAsmInfo &MAI) const {
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 {
60  OS << '\t' << getSectionName();
61  if (Subsection) {
62  OS << '\t';
63  Subsection->print(OS, &MAI);
64  }
65  OS << '\n';
66  return;
67  }
68 
69  OS << "\t.section\t";
71 
72  // Handle the weird solaris syntax if desired.
74  !(Flags & ELF::SHF_MERGE)) {
75  if (Flags & ELF::SHF_ALLOC)
76  OS << ",#alloc";
77  if (Flags & ELF::SHF_EXECINSTR)
78  OS << ",#execinstr";
79  if (Flags & ELF::SHF_WRITE)
80  OS << ",#write";
81  if (Flags & ELF::SHF_EXCLUDE)
82  OS << ",#exclude";
83  if (Flags & ELF::SHF_TLS)
84  OS << ",#tls";
85  OS << '\n';
86  return;
87  }
88 
89  OS << ",\"";
90  if (Flags & ELF::SHF_ALLOC)
91  OS << 'a';
92  if (Flags & ELF::SHF_EXCLUDE)
93  OS << 'e';
94  if (Flags & ELF::SHF_EXECINSTR)
95  OS << 'x';
96  if (Flags & ELF::SHF_GROUP)
97  OS << 'G';
98  if (Flags & ELF::SHF_WRITE)
99  OS << 'w';
100  if (Flags & ELF::SHF_MERGE)
101  OS << 'M';
102  if (Flags & ELF::SHF_STRINGS)
103  OS << 'S';
104  if (Flags & ELF::SHF_TLS)
105  OS << 'T';
106  if (Flags & ELF::SHF_LINK_ORDER)
107  OS << 'o';
108 
109  // If there are target-specific flags, print them.
110  Triple::ArchType Arch = T.getArch();
111  if (Arch == Triple::xcore) {
112  if (Flags & ELF::XCORE_SHF_CP_SECTION)
113  OS << 'c';
114  if (Flags & ELF::XCORE_SHF_DP_SECTION)
115  OS << 'd';
116  } else if (T.isARM() || T.isThumb()) {
117  if (Flags & ELF::SHF_ARM_PURECODE)
118  OS << 'y';
119  } else if (Arch == Triple::hexagon) {
120  if (Flags & ELF::SHF_HEX_GPREL)
121  OS << 's';
122  }
123 
124  OS << '"';
125 
126  OS << ',';
127 
128  // If comment string is '@', e.g. as on ARM - use '%' instead
129  if (MAI.getCommentString()[0] == '@')
130  OS << '%';
131  else
132  OS << '@';
133 
134  if (Type == ELF::SHT_INIT_ARRAY)
135  OS << "init_array";
136  else if (Type == ELF::SHT_FINI_ARRAY)
137  OS << "fini_array";
138  else if (Type == ELF::SHT_PREINIT_ARRAY)
139  OS << "preinit_array";
140  else if (Type == ELF::SHT_NOBITS)
141  OS << "nobits";
142  else if (Type == ELF::SHT_NOTE)
143  OS << "note";
144  else if (Type == ELF::SHT_PROGBITS)
145  OS << "progbits";
146  else if (Type == ELF::SHT_X86_64_UNWIND)
147  OS << "unwind";
148  else if (Type == ELF::SHT_MIPS_DWARF)
149  // Print hex value of the flag while we do not have
150  // any standard symbolic representation of the flag.
151  OS << "0x7000001e";
152  else if (Type == ELF::SHT_LLVM_ODRTAB)
153  OS << "llvm_odrtab";
155  OS << "llvm_linker_options";
157  OS << "llvm_call_graph_profile";
158  else
159  report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
160  " for section " + getSectionName());
161 
162  if (EntrySize) {
163  assert(Flags & ELF::SHF_MERGE);
164  OS << "," << EntrySize;
165  }
166 
167  if (Flags & ELF::SHF_GROUP) {
168  OS << ",";
169  printName(OS, Group->getName());
170  OS << ",comdat";
171  }
172 
173  if (Flags & ELF::SHF_LINK_ORDER) {
174  assert(AssociatedSymbol);
175  OS << ",";
176  printName(OS, AssociatedSymbol->getName());
177  }
178 
179  if (isUnique())
180  OS << ",unique," << UniqueID;
181 
182  OS << '\n';
183 
184  if (Subsection) {
185  OS << "\t.subsection\t";
186  Subsection->print(OS, &MAI);
187  OS << '\n';
188  }
189 }
190 
192  return getFlags() & ELF::SHF_EXECINSTR;
193 }
194 
196  return getType() == ELF::SHT_NOBITS;
197 }
bool UseCodeAlign() const override
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
Definition: ELF.h:926
static void printName(raw_ostream &OS, StringRef Name)
All sections with the "c" flag are grouped together by the linker to form the constant pool and the c...
Definition: ELF.h:931
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const
Decides whether a &#39;.section&#39; directive should be printed before the section name. ...
virtual bool shouldOmitSectionDirective(StringRef SectionName) const
Return true if the .section directive should be omitted when emitting SectionName.
Definition: MCAsmInfo.cpp:119
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:290
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool isVirtualSection() const override
Check whether this section is "virtual", that is has no actual object file contents.
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:250
StringRef getCommentString() const
Definition: MCAsmInfo.h:486
bool isUnique() const
Definition: MCSectionELF.h:86
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:385
unsigned getType() const
Definition: MCSectionELF.h:74
bool usesSunStyleELFSectionSwitchSyntax() const
Definition: MCAsmInfo.h:459
iterator begin() const
Definition: StringRef.h:106
static const size_t npos
Definition: StringRef.h:51
bool isThumb() const
Tests whether the target is Thumb (little and big endian).
Definition: Triple.h:665
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, raw_ostream &OS, const MCExpr *Subsection) const override
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
unsigned getFlags() const
Definition: MCSectionELF.h:75
iterator end() const
Definition: StringRef.h:108
bool isARM() const
Tests whether the target is ARM (little and big endian).
Definition: Triple.h:670
StringRef getSectionName() const
Definition: MCSectionELF.h:73