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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
2010-01-13 22:21:29 +01:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2009-08-13 07:07:35 +02:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2010-01-13 22:21:29 +01:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2011-01-23 05:28:49 +01:00
|
|
|
#include "llvm/Support/ELF.h"
|
2009-08-13 07:07:35 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-01-23 05:28:49 +01:00
|
|
|
|
2009-08-13 07:07:35 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-04-08 23:26:26 +02:00
|
|
|
MCSectionELF::~MCSectionELF() {} // anchor.
|
2009-08-13 07:07:35 +02:00
|
|
|
|
|
|
|
// ShouldOmitSectionDirective - 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 {
|
2009-08-13 07:07:35 +02:00
|
|
|
|
|
|
|
// FIXME: Does .section .bss/.data/.text work everywhere??
|
2010-01-22 19:21:23 +01:00
|
|
|
if (Name == ".text" || Name == ".data" ||
|
|
|
|
(Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
|
2009-08-13 07:07:35 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-22 23:43:10 +02:00
|
|
|
void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
|
2009-08-13 07:07:35 +02:00
|
|
|
raw_ostream &OS) const {
|
2009-08-14 01:30:21 +02:00
|
|
|
|
2010-01-22 19:21:23 +01:00
|
|
|
if (ShouldOmitSectionDirective(SectionName, MAI)) {
|
2009-08-13 07:07:35 +02:00
|
|
|
OS << '\t' << getSectionName() << '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-03 23:31:08 +01:00
|
|
|
StringRef name = getSectionName();
|
2011-03-04 21:03:14 +01:00
|
|
|
if (name.find_first_not_of("0123456789_."
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ") == name.npos) {
|
|
|
|
OS << "\t.section\t" << name;
|
|
|
|
} else {
|
|
|
|
OS << "\t.section\t\"";
|
|
|
|
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;
|
|
|
|
}
|
2011-03-03 23:31:08 +01:00
|
|
|
}
|
2011-03-04 21:03:14 +01:00
|
|
|
OS << '"';
|
2011-03-03 23:31:08 +01:00
|
|
|
}
|
|
|
|
|
2009-08-13 07:07:35 +02:00
|
|
|
// Handle the weird solaris syntax if desired.
|
2009-08-22 23:43:10 +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";
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << ",\"";
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::SHF_ALLOC)
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << 'a';
|
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';
|
|
|
|
|
|
|
|
// If there are target-specific flags, print them.
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::XCORE_SHF_CP_SECTION)
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << 'c';
|
2011-01-23 05:43:11 +01:00
|
|
|
if (Flags & ELF::XCORE_SHF_DP_SECTION)
|
2010-04-08 23:26:26 +02:00
|
|
|
OS << 'd';
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-02-14 23:23:49 +01:00
|
|
|
if (Flags & ELF::SHF_GROUP)
|
|
|
|
OS << "," << Group->getName() << ",comdat";
|
2009-08-13 07:07:35 +02:00
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-09-30 07:59:22 +02:00
|
|
|
unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
|
|
|
|
if (Kind.isMergeable1ByteCString()) return 1;
|
|
|
|
if (Kind.isMergeable2ByteCString()) return 2;
|
|
|
|
if (Kind.isMergeable4ByteCString()) return 4;
|
|
|
|
if (Kind.isMergeableConst4()) return 4;
|
|
|
|
if (Kind.isMergeableConst8()) return 8;
|
|
|
|
if (Kind.isMergeableConst16()) return 16;
|
|
|
|
return 0;
|
|
|
|
}
|