LLVM  9.0.0svn
MCSectionELF.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCSectionELF.h"
10 #include "llvm/ADT/Triple.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCExpr.h"
16 #include <cassert>
17 
18 using namespace llvm;
19 
20 // Decides whether a '.section' directive
21 // should be printed before the section name.
23  const MCAsmInfo &MAI) const {
24  if (isUnique())
25  return false;
26 
27  return MAI.shouldOmitSectionDirective(Name);
28 }
29 
30 static void printName(raw_ostream &OS, StringRef Name) {
31  if (Name.find_first_not_of("0123456789_."
32  "abcdefghijklmnopqrstuvwxyz"
33  "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
34  OS << Name;
35  return;
36  }
37  OS << '"';
38  for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
39  if (*B == '"') // Unquoted "
40  OS << "\\\"";
41  else if (*B != '\\') // Neither " or backslash
42  OS << *B;
43  else if (B + 1 == E) // Trailing backslash
44  OS << "\\\\";
45  else {
46  OS << B[0] << B[1]; // Quoted character
47  ++B;
48  }
49  }
50  OS << '"';
51 }
52 
54  raw_ostream &OS,
55  const MCExpr *Subsection) const {
57  OS << '\t' << getSectionName();
58  if (Subsection) {
59  OS << '\t';
60  Subsection->print(OS, &MAI);
61  }
62  OS << '\n';
63  return;
64  }
65 
66  OS << "\t.section\t";
68 
69  // Handle the weird solaris syntax if desired.
71  !(Flags & ELF::SHF_MERGE)) {
72  if (Flags & ELF::SHF_ALLOC)
73  OS << ",#alloc";
74  if (Flags & ELF::SHF_EXECINSTR)
75  OS << ",#execinstr";
76  if (Flags & ELF::SHF_WRITE)
77  OS << ",#write";
78  if (Flags & ELF::SHF_EXCLUDE)
79  OS << ",#exclude";
80  if (Flags & ELF::SHF_TLS)
81  OS << ",#tls";
82  OS << '\n';
83  return;
84  }
85 
86  OS << ",\"";
87  if (Flags & ELF::SHF_ALLOC)
88  OS << 'a';
89  if (Flags & ELF::SHF_EXCLUDE)
90  OS << 'e';
91  if (Flags & ELF::SHF_EXECINSTR)
92  OS << 'x';
93  if (Flags & ELF::SHF_GROUP)
94  OS << 'G';
95  if (Flags & ELF::SHF_WRITE)
96  OS << 'w';
97  if (Flags & ELF::SHF_MERGE)
98  OS << 'M';
99  if (Flags & ELF::SHF_STRINGS)
100  OS << 'S';
101  if (Flags & ELF::SHF_TLS)
102  OS << 'T';
103  if (Flags & ELF::SHF_LINK_ORDER)
104  OS << 'o';
105 
106  // If there are target-specific flags, print them.
107  Triple::ArchType Arch = T.getArch();
108  if (Arch == Triple::xcore) {
109  if (Flags & ELF::XCORE_SHF_CP_SECTION)
110  OS << 'c';
111  if (Flags & ELF::XCORE_SHF_DP_SECTION)
112  OS << 'd';
113  } else if (T.isARM() || T.isThumb()) {
114  if (Flags & ELF::SHF_ARM_PURECODE)
115  OS << 'y';
116  } else if (Arch == Triple::hexagon) {
117  if (Flags & ELF::SHF_HEX_GPREL)
118  OS << 's';
119  }
120 
121  OS << '"';
122 
123  OS << ',';
124 
125  // If comment string is '@', e.g. as on ARM - use '%' instead
126  if (MAI.getCommentString()[0] == '@')
127  OS << '%';
128  else
129  OS << '@';
130 
131  if (Type == ELF::SHT_INIT_ARRAY)
132  OS << "init_array";
133  else if (Type == ELF::SHT_FINI_ARRAY)
134  OS << "fini_array";
135  else if (Type == ELF::SHT_PREINIT_ARRAY)
136  OS << "preinit_array";
137  else if (Type == ELF::SHT_NOBITS)
138  OS << "nobits";
139  else if (Type == ELF::SHT_NOTE)
140  OS << "note";
141  else if (Type == ELF::SHT_PROGBITS)
142  OS << "progbits";
143  else if (Type == ELF::SHT_X86_64_UNWIND)
144  OS << "unwind";
145  else if (Type == ELF::SHT_MIPS_DWARF)
146  // Print hex value of the flag while we do not have
147  // any standard symbolic representation of the flag.
148  OS << "0x7000001e";
149  else if (Type == ELF::SHT_LLVM_ODRTAB)
150  OS << "llvm_odrtab";
152  OS << "llvm_linker_options";
154  OS << "llvm_call_graph_profile";
155  else
156  report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
157  " for section " + getSectionName());
158 
159  if (EntrySize) {
160  assert(Flags & ELF::SHF_MERGE);
161  OS << "," << EntrySize;
162  }
163 
164  if (Flags & ELF::SHF_GROUP) {
165  OS << ",";
166  printName(OS, Group->getName());
167  OS << ",comdat";
168  }
169 
170  if (Flags & ELF::SHF_LINK_ORDER) {
171  assert(AssociatedSymbol);
172  OS << ",";
173  printName(OS, AssociatedSymbol->getName());
174  }
175 
176  if (isUnique())
177  OS << ",unique," << UniqueID;
178 
179  OS << '\n';
180 
181  if (Subsection) {
182  OS << "\t.subsection\t";
183  Subsection->print(OS, &MAI);
184  OS << '\n';
185  }
186 }
187 
189  return getFlags() & ELF::SHF_EXECINSTR;
190 }
191 
193  return getType() == ELF::SHT_NOBITS;
194 }
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:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
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:930
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
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:118
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:55
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:41
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
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:249
StringRef getCommentString() const
Definition: MCAsmInfo.h:485
bool isUnique() const
Definition: MCSectionELF.h:83
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
Definition: ELF.h:925
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:387
unsigned getType() const
Definition: MCSectionELF.h:71
bool usesSunStyleELFSectionSwitchSyntax() const
Definition: MCAsmInfo.h:458
iterator begin() const
Definition: StringRef.h:101
static const size_t npos
Definition: StringRef.h:50
bool isThumb() const
Tests whether the target is Thumb (little and big endian).
Definition: Triple.h:674
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:202
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:45
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:48
unsigned getFlags() const
Definition: MCSectionELF.h:72
iterator end() const
Definition: StringRef.h:103
bool isARM() const
Tests whether the target is ARM (little and big endian).
Definition: Triple.h:679
StringRef getSectionName() const
Definition: MCSectionELF.h:70