2009-08-13 07:07:35 +02:00
|
|
|
//===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
2017-02-14 01:33:36 +01:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2017-06-07 05:48:56 +02:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2010-01-13 22:21:29 +01:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2013-04-17 23:18:16 +02:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2017-03-10 09:22:13 +01:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-13 07:07:35 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-14 01:33:36 +01:00
|
|
|
#include <cassert>
|
2011-01-23 05:28:49 +01:00
|
|
|
|
2009-08-13 07:07:35 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-02-14 01:33:36 +01:00
|
|
|
MCSectionELF::~MCSectionELF() = default; // anchor.
|
2009-08-13 07:07:35 +02:00
|
|
|
|
2014-10-09 23:23:39 +02:00
|
|
|
// Decides whether a '.section' directive
|
|
|
|
// should be printed before the section name.
|
2010-01-22 19:21:23 +01:00
|
|
|
bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
|
2010-01-13 22:21:29 +01:00
|
|
|
const MCAsmInfo &MAI) const {
|
2015-04-04 20:02:01 +02:00
|
|
|
if (isUnique())
|
2015-02-17 21:48:01 +01:00
|
|
|
return false;
|
|
|
|
|
MCAsmInfo: Allow targets to specify when the .section directive should be omitted
Summary:
The default behavior is to omit the .section directive for .text, .data,
and sometimes .bss, but some targets may want to omit this directive for
other sections too.
The AMDGPU backend will uses this to emit a simplified syntax for section
switches. For example if the section directive is not omitted (current
behavior), section switches to .hsatext will be printed like this:
.section .hsatext,#alloc,#execinstr,#write
This is actually wrong, because .hsatext has some custom STT_* flags,
which MC doesn't know how to print or parse.
If the section directive is omitted (made possible by this commit),
section switches will be printed like this:
.hsatext
The motivation for this patch is to make it possible to emit sections
with custom STT_* flags without having to teach MC about all the target
specific STT_* flags.
Reviewers: rafael, grosbach
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D12423
llvm-svn: 248618
2015-09-25 23:41:14 +02:00
|
|
|
return MAI.shouldOmitSectionDirective(Name);
|
2009-08-13 07:07:35 +02:00
|
|
|
}
|
|
|
|
|
2013-11-13 15:01:59 +01:00
|
|
|
static void printName(raw_ostream &OS, StringRef Name) {
|
|
|
|
if (Name.find_first_not_of("0123456789_."
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
|
|
|
|
OS << Name;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
OS << '"';
|
|
|
|
for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
|
|
|
|
if (*B == '"') // Unquoted "
|
|
|
|
OS << "\\\"";
|
|
|
|
else if (*B != '\\') // Neither " or backslash
|
|
|
|
OS << *B;
|
|
|
|
else if (B + 1 == E) // Trailing backslash
|
|
|
|
OS << "\\\\";
|
|
|
|
else {
|
|
|
|
OS << B[0] << B[1]; // Quoted character
|
|
|
|
++B;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OS << '"';
|
|
|
|
}
|
|
|
|
|
2017-01-30 16:38:43 +01:00
|
|
|
void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
|
2013-04-17 23:18:16 +02:00
|
|
|
raw_ostream &OS,
|
|
|
|
const MCExpr *Subsection) const {
|
2010-01-22 19:21:23 +01:00
|
|
|
if (ShouldOmitSectionDirective(SectionName, MAI)) {
|
2013-04-17 23:18:16 +02:00
|
|
|
OS << '\t' << getSectionName();
|
2015-06-09 02:31:39 +02:00
|
|
|
if (Subsection) {
|
|
|
|
OS << '\t';
|
|
|
|
Subsection->print(OS, &MAI);
|
|
|
|
}
|
2013-04-17 23:18:16 +02:00
|
|
|
OS << '\n';
|
2009-08-13 07:07:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-13 15:01:59 +01:00
|
|
|
OS << "\t.section\t";
|
|
|
|
printName(OS, getSectionName());
|
2011-03-03 23:31:08 +01:00
|
|
|
|
2009-08-13 07:07:35 +02:00
|
|
|
// Handle the weird solaris syntax if desired.
|
2012-05-11 03:41:30 +02:00
|
|
|
if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
|
2011-01-23 05:43:11 +01:00
|
|
|
!(Flags & ELF::SHF_MERGE)) {
|
|
|
|
if (Flags & ELF::SHF_ALLOC)
|
2009-08-13 07:07:35 +02:00
|
|
|
OS << ",#alloc";
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::SHF_EXECINSTR)
|
2009-08-13 07:07:35 +02:00
|
|
|
OS << ",#execinstr";
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::SHF_WRITE)
|
2009-08-13 07:07:35 +02:00
|
|
|
OS << ",#write";
|
2013-09-15 21:53:20 +02:00
|
|
|
if (Flags & ELF::SHF_EXCLUDE)
|
|
|
|
OS << ",#exclude";
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::SHF_TLS)
|
2009-08-13 07:07:35 +02:00
|
|
|
OS << ",#tls";
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << '\n';
|
|
|
|
return;
|
|
|
|
}
|
2012-05-11 03:41:30 +02:00
|
|
|
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << ",\"";
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::SHF_ALLOC)
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << 'a';
|
2013-09-15 21:53:20 +02:00
|
|
|
if (Flags & ELF::SHF_EXCLUDE)
|
|
|
|
OS << 'e';
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::SHF_EXECINSTR)
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << 'x';
|
2011-02-14 23:23:49 +01:00
|
|
|
if (Flags & ELF::SHF_GROUP)
|
|
|
|
OS << 'G';
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::SHF_WRITE)
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << 'w';
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::SHF_MERGE)
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << 'M';
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::SHF_STRINGS)
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << 'S';
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::SHF_TLS)
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << 'T';
|
2017-03-14 20:28:51 +01:00
|
|
|
if (Flags & ELF::SHF_LINK_ORDER)
|
2017-04-05 00:35:08 +02:00
|
|
|
OS << 'o';
|
2012-05-11 03:41:30 +02:00
|
|
|
|
2010-04-08 23:26:26 +02:00
|
|
|
// If there are target-specific flags, print them.
|
2017-01-30 16:38:43 +01:00
|
|
|
Triple::ArchType Arch = T.getArch();
|
|
|
|
if (Arch == Triple::xcore) {
|
|
|
|
if (Flags & ELF::XCORE_SHF_CP_SECTION)
|
|
|
|
OS << 'c';
|
|
|
|
if (Flags & ELF::XCORE_SHF_DP_SECTION)
|
|
|
|
OS << 'd';
|
|
|
|
} else if (Arch == Triple::arm || Arch == Triple::armeb ||
|
|
|
|
Arch == Triple::thumb || Arch == Triple::thumbeb) {
|
|
|
|
if (Flags & ELF::SHF_ARM_PURECODE)
|
|
|
|
OS << 'y';
|
|
|
|
}
|
2012-05-11 03:41:30 +02:00
|
|
|
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << '"';
|
2009-08-13 07:07:35 +02:00
|
|
|
|
2010-11-10 00:42:07 +01:00
|
|
|
OS << ',';
|
|
|
|
|
|
|
|
// If comment string is '@', e.g. as on ARM - use '%' instead
|
|
|
|
if (MAI.getCommentString()[0] == '@')
|
|
|
|
OS << '%';
|
|
|
|
else
|
|
|
|
OS << '@';
|
|
|
|
|
2011-01-23 05:28:49 +01:00
|
|
|
if (Type == ELF::SHT_INIT_ARRAY)
|
2010-11-10 00:42:07 +01:00
|
|
|
OS << "init_array";
|
2011-01-23 05:28:49 +01:00
|
|
|
else if (Type == ELF::SHT_FINI_ARRAY)
|
2010-11-10 00:42:07 +01:00
|
|
|
OS << "fini_array";
|
2011-01-23 05:28:49 +01:00
|
|
|
else if (Type == ELF::SHT_PREINIT_ARRAY)
|
2010-11-10 00:42:07 +01:00
|
|
|
OS << "preinit_array";
|
2011-01-23 05:28:49 +01:00
|
|
|
else if (Type == ELF::SHT_NOBITS)
|
2010-11-10 00:42:07 +01:00
|
|
|
OS << "nobits";
|
2011-01-23 05:28:49 +01:00
|
|
|
else if (Type == ELF::SHT_NOTE)
|
2010-12-26 22:30:59 +01:00
|
|
|
OS << "note";
|
2011-01-23 05:28:49 +01:00
|
|
|
else if (Type == ELF::SHT_PROGBITS)
|
2010-11-10 00:42:07 +01:00
|
|
|
OS << "progbits";
|
2015-11-06 16:30:45 +01:00
|
|
|
else if (Type == ELF::SHT_X86_64_UNWIND)
|
|
|
|
OS << "unwind";
|
2017-03-10 09:22:20 +01:00
|
|
|
else if (Type == ELF::SHT_MIPS_DWARF)
|
|
|
|
// Print hex value of the flag while we do not have
|
|
|
|
// any standard symbolic representation of the flag.
|
|
|
|
OS << "0x7000001e";
|
2017-06-14 20:52:12 +02:00
|
|
|
else if (Type == ELF::SHT_LLVM_ODRTAB)
|
|
|
|
OS << "llvm_odrtab";
|
2017-03-10 09:22:13 +01:00
|
|
|
else
|
|
|
|
report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
|
|
|
|
" for section " + getSectionName());
|
2010-11-10 00:42:07 +01:00
|
|
|
|
|
|
|
if (EntrySize) {
|
2011-01-23 05:43:11 +01:00
|
|
|
assert(Flags & ELF::SHF_MERGE);
|
2010-11-10 00:42:07 +01:00
|
|
|
OS << "," << EntrySize;
|
2009-08-13 07:07:35 +02:00
|
|
|
}
|
2010-11-10 00:42:07 +01:00
|
|
|
|
2013-11-13 15:01:59 +01:00
|
|
|
if (Flags & ELF::SHF_GROUP) {
|
|
|
|
OS << ",";
|
|
|
|
printName(OS, Group->getName());
|
|
|
|
OS << ",comdat";
|
|
|
|
}
|
2015-02-17 21:48:01 +01:00
|
|
|
|
2017-03-14 20:28:51 +01:00
|
|
|
if (Flags & ELF::SHF_LINK_ORDER) {
|
|
|
|
assert(AssociatedSymbol);
|
|
|
|
OS << ",";
|
|
|
|
printName(OS, AssociatedSymbol->getName());
|
|
|
|
}
|
|
|
|
|
2015-04-04 20:02:01 +02:00
|
|
|
if (isUnique())
|
2015-04-06 18:34:41 +02:00
|
|
|
OS << ",unique," << UniqueID;
|
2015-02-17 21:48:01 +01:00
|
|
|
|
2009-08-13 07:07:35 +02:00
|
|
|
OS << '\n';
|
2013-04-17 23:18:16 +02:00
|
|
|
|
2015-06-09 02:31:39 +02:00
|
|
|
if (Subsection) {
|
|
|
|
OS << "\t.subsection\t";
|
|
|
|
Subsection->print(OS, &MAI);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
2009-08-13 07:07:35 +02:00
|
|
|
}
|
|
|
|
|
2010-10-04 19:32:41 +02:00
|
|
|
bool MCSectionELF::UseCodeAlign() const {
|
2011-01-23 05:43:11 +01:00
|
|
|
return getFlags() & ELF::SHF_EXECINSTR;
|
2010-10-04 19:32:41 +02:00
|
|
|
}
|
|
|
|
|
2010-11-17 21:03:54 +01:00
|
|
|
bool MCSectionELF::isVirtualSection() const {
|
2011-01-23 05:28:49 +01:00
|
|
|
return getType() == ELF::SHT_NOBITS;
|
2010-11-17 21:03:54 +01:00
|
|
|
}
|