freebsd-skq/contrib/llvm/lib/MC/MCSectionELF.cpp

169 lines
4.1 KiB
C++
Raw Normal View History

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"
#include "llvm/MC/MCExpr.h"
2010-01-15 15:37:28 +00:00
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/ELF.h"
2009-10-14 17:57:32 +00:00
#include "llvm/Support/raw_ostream.h"
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
// 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 {
if (isUnique())
return false;
return MAI.shouldOmitSectionDirective(Name);
2009-10-14 17:57:32 +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,
raw_ostream &OS,
const MCExpr *Subsection) const {
2010-01-23 11:09:33 +00:00
if (ShouldOmitSectionDirective(SectionName, MAI)) {
OS << '\t' << getSectionName();
if (Subsection) {
OS << '\t';
Subsection->print(OS, &MAI);
}
OS << '\n';
2009-10-14 17:57:32 +00:00
return;
}
OS << "\t.section\t";
printName(OS, getSectionName());
2009-10-14 17:57:32 +00:00
// Handle the weird solaris syntax if desired.
if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
!(Flags & ELF::SHF_MERGE)) {
if (Flags & ELF::SHF_ALLOC)
2009-10-14 17:57:32 +00:00
OS << ",#alloc";
if (Flags & ELF::SHF_EXECINSTR)
2009-10-14 17:57:32 +00:00
OS << ",#execinstr";
if (Flags & ELF::SHF_WRITE)
2009-10-14 17:57:32 +00:00
OS << ",#write";
if (Flags & ELF::SHF_EXCLUDE)
OS << ",#exclude";
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;
}
2010-05-04 16:11:02 +00:00
OS << ",\"";
if (Flags & ELF::SHF_ALLOC)
2010-05-04 16:11:02 +00:00
OS << 'a';
if (Flags & ELF::SHF_EXCLUDE)
OS << 'e';
if (Flags & ELF::SHF_EXECINSTR)
2010-05-04 16:11:02 +00:00
OS << 'x';
if (Flags & ELF::SHF_GROUP)
OS << 'G';
if (Flags & ELF::SHF_WRITE)
2010-05-04 16:11:02 +00:00
OS << 'w';
if (Flags & ELF::SHF_MERGE)
2010-05-04 16:11:02 +00:00
OS << 'M';
if (Flags & ELF::SHF_STRINGS)
2010-05-04 16:11:02 +00:00
OS << 'S';
if (Flags & ELF::SHF_TLS)
2010-05-04 16:11:02 +00:00
OS << 'T';
2010-05-04 16:11:02 +00:00
// If there are target-specific flags, print them.
if (Flags & ELF::XCORE_SHF_CP_SECTION)
2010-05-04 16:11:02 +00:00
OS << 'c';
if (Flags & ELF::XCORE_SHF_DP_SECTION)
2010-05-04 16:11:02 +00:00
OS << 'd';
2010-05-04 16:11:02 +00:00
OS << '"';
2009-10-14 17:57:32 +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";
else if (Type == ELF::SHT_X86_64_UNWIND)
OS << "unwind";
if (EntrySize) {
assert(Flags & ELF::SHF_MERGE);
OS << "," << EntrySize;
2009-10-14 17:57:32 +00:00
}
if (Flags & ELF::SHF_GROUP) {
OS << ",";
printName(OS, Group->getName());
OS << ",comdat";
}
if (isUnique())
OS << ",unique," << UniqueID;
2009-10-14 17:57:32 +00:00
OS << '\n';
if (Subsection) {
OS << "\t.subsection\t";
Subsection->print(OS, &MAI);
OS << '\n';
}
2009-10-14 17:57:32 +00:00
}
bool MCSectionELF::UseCodeAlign() const {
return getFlags() & ELF::SHF_EXECINSTR;
2009-10-14 17:57:32 +00:00
}
bool MCSectionELF::isVirtualSection() const {
return getType() == ELF::SHT_NOBITS;
}