LLVM  14.0.0git
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 {
56  if (ShouldOmitSectionDirective(getName(), MAI)) {
57  OS << '\t' << getName();
58  if (Subsection) {
59  OS << '\t';
60  Subsection->print(OS, &MAI);
61  }
62  OS << '\n';
63  return;
64  }
65 
66  OS << "\t.section\t";
67  printName(OS, getName());
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  if (Flags & ELF::SHF_GNU_RETAIN)
106  OS << 'R';
107 
108  // If there are target-specific flags, print them.
109  Triple::ArchType Arch = T.getArch();
110  if (Arch == Triple::xcore) {
111  if (Flags & ELF::XCORE_SHF_CP_SECTION)
112  OS << 'c';
113  if (Flags & ELF::XCORE_SHF_DP_SECTION)
114  OS << 'd';
115  } else if (T.isARM() || T.isThumb()) {
116  if (Flags & ELF::SHF_ARM_PURECODE)
117  OS << 'y';
118  } else if (Arch == Triple::hexagon) {
119  if (Flags & ELF::SHF_HEX_GPREL)
120  OS << 's';
121  }
122 
123  OS << '"';
124 
125  OS << ',';
126 
127  // If comment string is '@', e.g. as on ARM - use '%' instead
128  if (MAI.getCommentString()[0] == '@')
129  OS << '%';
130  else
131  OS << '@';
132 
133  if (Type == ELF::SHT_INIT_ARRAY)
134  OS << "init_array";
135  else if (Type == ELF::SHT_FINI_ARRAY)
136  OS << "fini_array";
137  else if (Type == ELF::SHT_PREINIT_ARRAY)
138  OS << "preinit_array";
139  else if (Type == ELF::SHT_NOBITS)
140  OS << "nobits";
141  else if (Type == ELF::SHT_NOTE)
142  OS << "note";
143  else if (Type == ELF::SHT_PROGBITS)
144  OS << "progbits";
145  else if (Type == ELF::SHT_X86_64_UNWIND)
146  OS << "unwind";
147  else if (Type == ELF::SHT_MIPS_DWARF)
148  // Print hex value of the flag while we do not have
149  // any standard symbolic representation of the flag.
150  OS << "0x7000001e";
151  else if (Type == ELF::SHT_LLVM_ODRTAB)
152  OS << "llvm_odrtab";
154  OS << "llvm_linker_options";
156  OS << "llvm_call_graph_profile";
158  OS << "llvm_dependent_libraries";
159  else if (Type == ELF::SHT_LLVM_SYMPART)
160  OS << "llvm_sympart";
161  else if (Type == ELF::SHT_LLVM_BB_ADDR_MAP)
162  OS << "llvm_bb_addr_map";
163  else
164  report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
165  " for section " + getName());
166 
167  if (EntrySize) {
168  assert(Flags & ELF::SHF_MERGE);
169  OS << "," << EntrySize;
170  }
171 
172  if (Flags & ELF::SHF_GROUP) {
173  OS << ",";
174  printName(OS, Group.getPointer()->getName());
175  if (isComdat())
176  OS << ",comdat";
177  }
178 
179  if (Flags & ELF::SHF_LINK_ORDER) {
180  OS << ",";
181  if (LinkedToSym)
182  printName(OS, LinkedToSym->getName());
183  else
184  OS << '0';
185  }
186 
187  if (isUnique())
188  OS << ",unique," << UniqueID;
189 
190  OS << '\n';
191 
192  if (Subsection) {
193  OS << "\t.subsection\t";
194  Subsection->print(OS, &MAI);
195  OS << '\n';
196  }
197 }
198 
200  return getFlags() & ELF::SHF_EXECINSTR;
201 }
202 
204  return getType() == ELF::SHT_NOBITS;
205 }
206 
207 StringRef MCSectionELF::getVirtualSectionKind() const { return "SHT_NOBITS"; }
llvm::MCSection::Name
StringRef Name
Definition: MCSection.h:111
llvm::MCAsmInfo::usesSunStyleELFSectionSwitchSyntax
bool usesSunStyleELFSectionSwitchSyntax() const
Definition: MCAsmInfo.h:603
llvm::ELF::SHT_LLVM_SYMPART
@ SHT_LLVM_SYMPART
Definition: ELF.h:940
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MCAsmInfo::getCommentString
StringRef getCommentString() const
Definition: MCAsmInfo.h:641
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
MCSectionELF.h
ErrorHandling.h
llvm::ELF::XCORE_SHF_DP_SECTION
@ XCORE_SHF_DP_SECTION
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
Definition: ELF.h:1034
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::ELF::SHT_INIT_ARRAY
@ SHT_INIT_ARRAY
Definition: ELF.h:921
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::ELF::SHF_EXECINSTR
@ SHF_EXECINSTR
Definition: ELF.h:991
llvm::ELF::SHT_X86_64_UNWIND
@ SHT_X86_64_UNWIND
Definition: ELF.h:966
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::Triple::hexagon
@ hexagon
Definition: Triple.h:60
llvm::ELF::SHT_NOTE
@ SHT_NOTE
Definition: ELF.h:916
llvm::ELF::SHF_LINK_ORDER
@ SHF_LINK_ORDER
Definition: ELF.h:1003
llvm::ELF::SHT_PROGBITS
@ SHT_PROGBITS
Definition: ELF.h:910
llvm::MCAsmInfo::shouldOmitSectionDirective
virtual bool shouldOmitSectionDirective(StringRef SectionName) const
Return true if the .section directive should be omitted when emitting SectionName.
Definition: MCAsmInfo.cpp:134
ELF.h
llvm::ELF::SHF_TLS
@ SHF_TLS
Definition: ELF.h:1013
llvm::Triple::ArchType
ArchType
Definition: Triple.h:47
llvm::ELF::SHF_GNU_RETAIN
@ SHF_GNU_RETAIN
Definition: ELF.h:1019
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
printName
static void printName(raw_ostream &OS, StringRef Name)
Definition: MCSectionELF.cpp:30
llvm::MCSectionELF::isUnique
bool isUnique() const
Definition: MCSectionELF.h:88
llvm::MCSectionELF::UseCodeAlign
bool UseCodeAlign() const override
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
Definition: MCSectionELF.cpp:199
llvm::ELF::SHT_LLVM_LINKER_OPTIONS
@ SHT_LLVM_LINKER_OPTIONS
Definition: ELF.h:935
llvm::MCSectionELF::PrintSwitchToSection
void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, raw_ostream &OS, const MCExpr *Subsection) const override
Definition: MCSectionELF.cpp:53
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::MCSectionELF::isVirtualSection
bool isVirtualSection() const override
Check whether this section is "virtual", that is has no actual object file contents.
Definition: MCSectionELF.cpp:203
llvm::Triple::xcore
@ xcore
Definition: Triple.h:85
llvm::MCSectionELF::ShouldOmitSectionDirective
bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const
Decides whether a '.section' directive should be printed before the section name.
Definition: MCSectionELF.cpp:22
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCSymbol::getName
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:198
llvm::MCSectionELF::isComdat
bool isComdat() const
Definition: MCSectionELF.h:79
llvm::Twine::utohexstr
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:408
llvm::ELF::SHT_PREINIT_ARRAY
@ SHT_PREINIT_ARRAY
Definition: ELF.h:923
llvm::ELF::SHF_WRITE
@ SHF_WRITE
Definition: ELF.h:985
llvm::ELF::SHT_MIPS_DWARF
@ SHT_MIPS_DWARF
Definition: ELF.h:970
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::ELF::SHF_ARM_PURECODE
@ SHF_ARM_PURECODE
Definition: ELF.h:1080
llvm::ELF::SHT_LLVM_BB_ADDR_MAP
@ SHT_LLVM_BB_ADDR_MAP
Definition: ELF.h:943
llvm::ELF::SHF_HEX_GPREL
@ SHF_HEX_GPREL
Definition: ELF.h:1052
Triple.h
llvm::ELF::SHF_ALLOC
@ SHF_ALLOC
Definition: ELF.h:988
llvm::MCSectionELF::getFlags
unsigned getFlags() const
Definition: MCSectionELF.h:75
MCAsmInfo.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MCSectionELF::getVirtualSectionKind
StringRef getVirtualSectionKind() const override
Definition: MCSectionELF.cpp:207
llvm::ELF::SHF_STRINGS
@ SHF_STRINGS
Definition: ELF.h:997
llvm::ELF::SHF_MERGE
@ SHF_MERGE
Definition: ELF.h:994
llvm::MCSection::getName
StringRef getName() const
Definition: MCSection.h:122
llvm::ELF::XCORE_SHF_CP_SECTION
@ XCORE_SHF_CP_SECTION
All sections with the "c" flag are grouped together by the linker to form the constant pool and the c...
Definition: ELF.h:1039
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::ELF::SHT_LLVM_DEPENDENT_LIBRARIES
@ SHT_LLVM_DEPENDENT_LIBRARIES
Definition: ELF.h:938
llvm::ELF::SHT_FINI_ARRAY
@ SHT_FINI_ARRAY
Definition: ELF.h:922
llvm::MCExpr::print
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
llvm::ELF::SHT_LLVM_ODRTAB
@ SHT_LLVM_ODRTAB
Definition: ELF.h:934
llvm::ELF::SHF_GROUP
@ SHF_GROUP
Definition: ELF.h:1010
llvm::ELF::SHT_LLVM_CALL_GRAPH_PROFILE
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition: ELF.h:944
raw_ostream.h
llvm::ELF::SHT_NOBITS
@ SHT_NOBITS
Definition: ELF.h:917
MCExpr.h
llvm::ELF::SHF_EXCLUDE
@ SHF_EXCLUDE
Definition: ELF.h:1022
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::MCSectionELF::getType
unsigned getType() const
Definition: MCSectionELF.h:74