2009-10-14 17:57:32 +00: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
2010-01-15 15:37:28 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2009-10-14 17:57:32 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2013-06-10 20:36:52 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2010-01-15 15:37:28 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2011-02-20 12:57:14 +00:00
|
|
|
#include "llvm/Support/ELF.h"
|
2009-10-14 17:57:32 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-02-20 12:57:14 +00:00
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
MCSectionELF::~MCSectionELF() {} // anchor.
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2015-01-18 16:17:27 +00:00
|
|
|
// Decides whether a '.section' directive
|
|
|
|
// should be printed before the section name.
|
2010-01-23 11:09:33 +00:00
|
|
|
bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
|
2010-01-15 15:37:28 +00:00
|
|
|
const MCAsmInfo &MAI) const {
|
2012-08-15 19:34:23 +00:00
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
if (isUnique())
|
|
|
|
return false;
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
// FIXME: Does .section .bss/.data/.text work everywhere??
|
2010-01-23 11:09:33 +00:00
|
|
|
if (Name == ".text" || Name == ".data" ||
|
|
|
|
(Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
|
2009-10-14 17:57:32 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-22 00:04:03 +00: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 << '"';
|
|
|
|
}
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
|
2013-06-10 20:36:52 +00:00
|
|
|
raw_ostream &OS,
|
|
|
|
const MCExpr *Subsection) const {
|
2012-08-15 19:34:23 +00:00
|
|
|
|
2010-01-23 11:09:33 +00:00
|
|
|
if (ShouldOmitSectionDirective(SectionName, MAI)) {
|
2013-06-10 20:36:52 +00:00
|
|
|
OS << '\t' << getSectionName();
|
2015-06-09 19:06:30 +00:00
|
|
|
if (Subsection) {
|
|
|
|
OS << '\t';
|
|
|
|
Subsection->print(OS, &MAI);
|
|
|
|
}
|
2013-06-10 20:36:52 +00:00
|
|
|
OS << '\n';
|
2009-10-14 17:57:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
OS << "\t.section\t";
|
|
|
|
printName(OS, getSectionName());
|
2011-05-02 19:34:44 +00:00
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
// Handle the weird solaris syntax if desired.
|
2012-08-15 19:34:23 +00:00
|
|
|
if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
|
2011-02-20 12:57:14 +00:00
|
|
|
!(Flags & ELF::SHF_MERGE)) {
|
|
|
|
if (Flags & ELF::SHF_ALLOC)
|
2009-10-14 17:57:32 +00:00
|
|
|
OS << ",#alloc";
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::SHF_EXECINSTR)
|
2009-10-14 17:57:32 +00:00
|
|
|
OS << ",#execinstr";
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::SHF_WRITE)
|
2009-10-14 17:57:32 +00:00
|
|
|
OS << ",#write";
|
2013-12-22 00:04:03 +00:00
|
|
|
if (Flags & ELF::SHF_EXCLUDE)
|
|
|
|
OS << ",#exclude";
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::SHF_TLS)
|
2009-10-14 17:57:32 +00:00
|
|
|
OS << ",#tls";
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << '\n';
|
|
|
|
return;
|
|
|
|
}
|
2012-08-15 19:34:23 +00:00
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << ",\"";
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::SHF_ALLOC)
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << 'a';
|
2013-12-22 00:04:03 +00:00
|
|
|
if (Flags & ELF::SHF_EXCLUDE)
|
|
|
|
OS << 'e';
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::SHF_EXECINSTR)
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << 'x';
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::SHF_GROUP)
|
|
|
|
OS << 'G';
|
|
|
|
if (Flags & ELF::SHF_WRITE)
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << 'w';
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::SHF_MERGE)
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << 'M';
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::SHF_STRINGS)
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << 'S';
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::SHF_TLS)
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << 'T';
|
2012-08-15 19:34:23 +00:00
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
// If there are target-specific flags, print them.
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::XCORE_SHF_CP_SECTION)
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << 'c';
|
2011-02-20 12:57:14 +00:00
|
|
|
if (Flags & ELF::XCORE_SHF_DP_SECTION)
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << 'd';
|
2012-08-15 19:34:23 +00:00
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
OS << '"';
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2011-02-20 12:57:14 +00:00
|
|
|
OS << ',';
|
|
|
|
|
|
|
|
// If comment string is '@', e.g. as on ARM - use '%' instead
|
|
|
|
if (MAI.getCommentString()[0] == '@')
|
|
|
|
OS << '%';
|
|
|
|
else
|
|
|
|
OS << '@';
|
|
|
|
|
|
|
|
if (Type == ELF::SHT_INIT_ARRAY)
|
|
|
|
OS << "init_array";
|
|
|
|
else if (Type == ELF::SHT_FINI_ARRAY)
|
|
|
|
OS << "fini_array";
|
|
|
|
else if (Type == ELF::SHT_PREINIT_ARRAY)
|
|
|
|
OS << "preinit_array";
|
|
|
|
else if (Type == ELF::SHT_NOBITS)
|
|
|
|
OS << "nobits";
|
|
|
|
else if (Type == ELF::SHT_NOTE)
|
|
|
|
OS << "note";
|
|
|
|
else if (Type == ELF::SHT_PROGBITS)
|
|
|
|
OS << "progbits";
|
|
|
|
|
|
|
|
if (EntrySize) {
|
|
|
|
assert(Flags & ELF::SHF_MERGE);
|
|
|
|
OS << "," << EntrySize;
|
2009-10-14 17:57:32 +00:00
|
|
|
}
|
2011-02-20 12:57:14 +00:00
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
if (Flags & ELF::SHF_GROUP) {
|
|
|
|
OS << ",";
|
|
|
|
printName(OS, Group->getName());
|
|
|
|
OS << ",comdat";
|
|
|
|
}
|
2015-05-27 18:44:32 +00:00
|
|
|
|
|
|
|
if (isUnique())
|
|
|
|
OS << ",unique," << UniqueID;
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
OS << '\n';
|
2013-06-10 20:36:52 +00:00
|
|
|
|
2015-06-09 19:06:30 +00:00
|
|
|
if (Subsection) {
|
|
|
|
OS << "\t.subsection\t";
|
|
|
|
Subsection->print(OS, &MAI);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
2009-10-14 17:57:32 +00:00
|
|
|
}
|
|
|
|
|
2011-02-20 12:57:14 +00:00
|
|
|
bool MCSectionELF::UseCodeAlign() const {
|
|
|
|
return getFlags() & ELF::SHF_EXECINSTR;
|
2009-10-14 17:57:32 +00:00
|
|
|
}
|
|
|
|
|
2011-02-20 12:57:14 +00:00
|
|
|
bool MCSectionELF::isVirtualSection() const {
|
|
|
|
return getType() == ELF::SHT_NOBITS;
|
|
|
|
}
|