2008-07-19 13:14:11 +00:00
|
|
|
//===-- ELFTargetAsmInfo.cpp - ELF asm properties ---------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines target asm properties related what form asm statements
|
|
|
|
// should take in general on ELF-based targets
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-08-07 09:50:34 +00:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-07-19 13:14:11 +00:00
|
|
|
#include "llvm/Target/ELFTargetAsmInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2008-11-03 18:22:42 +00:00
|
|
|
ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM)
|
|
|
|
: TargetAsmInfo(TM) {
|
2008-07-19 13:14:11 +00:00
|
|
|
|
|
|
|
BSSSection_ = getUnnamedSection("\t.bss",
|
|
|
|
SectionFlags::Writeable | SectionFlags::BSS);
|
2008-09-24 22:20:27 +00:00
|
|
|
ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None);
|
2008-09-24 22:17:06 +00:00
|
|
|
TLSDataSection = getNamedSection("\t.tdata",
|
|
|
|
SectionFlags::Writeable | SectionFlags::TLS);
|
|
|
|
TLSBSSSection = getNamedSection("\t.tbss",
|
2008-07-19 13:14:11 +00:00
|
|
|
SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
|
|
|
|
|
2009-03-30 15:27:03 +00:00
|
|
|
DataRelSection = getNamedSection("\t.data.rel", SectionFlags::Writeable);
|
|
|
|
DataRelLocalSection = getNamedSection("\t.data.rel.local",
|
|
|
|
SectionFlags::Writeable);
|
|
|
|
DataRelROSection = getNamedSection("\t.data.rel.ro",
|
|
|
|
SectionFlags::Writeable);
|
|
|
|
DataRelROLocalSection = getNamedSection("\t.data.rel.ro.local",
|
|
|
|
SectionFlags::Writeable);
|
2008-07-19 13:14:11 +00:00
|
|
|
}
|
|
|
|
|
2009-03-30 15:27:43 +00:00
|
|
|
|
2008-07-19 13:14:11 +00:00
|
|
|
const Section*
|
2009-07-24 18:42:53 +00:00
|
|
|
ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
|
|
|
|
SectionKind::Kind Kind) const {
|
2008-07-19 13:14:11 +00:00
|
|
|
if (const Function *F = dyn_cast<Function>(GV)) {
|
|
|
|
switch (F->getLinkage()) {
|
2009-07-24 04:52:38 +00:00
|
|
|
default: llvm_unreachable("Unknown linkage type!");
|
|
|
|
case Function::PrivateLinkage:
|
|
|
|
case Function::LinkerPrivateLinkage:
|
|
|
|
case Function::InternalLinkage:
|
|
|
|
case Function::DLLExportLinkage:
|
|
|
|
case Function::ExternalLinkage:
|
|
|
|
return TextSection;
|
2008-07-19 13:14:11 +00:00
|
|
|
}
|
2009-07-24 05:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const GlobalVariable *GVar = cast<GlobalVariable>(GV);
|
|
|
|
switch (Kind) {
|
|
|
|
default: llvm_unreachable("Unsuported section kind for global");
|
2009-07-24 19:15:47 +00:00
|
|
|
case SectionKind::BSS:
|
|
|
|
return getBSSSection_();
|
2009-07-24 05:01:55 +00:00
|
|
|
case SectionKind::Data:
|
|
|
|
case SectionKind::DataRel:
|
|
|
|
return DataRelSection;
|
|
|
|
case SectionKind::DataRelLocal:
|
|
|
|
return DataRelLocalSection;
|
|
|
|
case SectionKind::DataRelRO:
|
|
|
|
return DataRelROSection;
|
|
|
|
case SectionKind::DataRelROLocal:
|
|
|
|
return DataRelROLocalSection;
|
|
|
|
case SectionKind::ROData:
|
|
|
|
return getReadOnlySection();
|
|
|
|
case SectionKind::RODataMergeStr:
|
|
|
|
return MergeableStringSection(GVar);
|
|
|
|
case SectionKind::RODataMergeConst: {
|
|
|
|
const Type *Ty = GVar->getInitializer()->getType();
|
|
|
|
const TargetData *TD = TM.getTargetData();
|
|
|
|
return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
|
|
|
|
}
|
|
|
|
case SectionKind::ThreadData:
|
|
|
|
// ELF targets usually support TLS stuff
|
|
|
|
return TLSDataSection;
|
|
|
|
case SectionKind::ThreadBSS:
|
|
|
|
return TLSBSSSection;
|
2009-07-24 04:52:38 +00:00
|
|
|
}
|
2008-07-19 13:14:11 +00:00
|
|
|
}
|
|
|
|
|
2009-07-22 00:28:43 +00:00
|
|
|
/// getSectionForMergableConstant - Given a mergable constant with the
|
|
|
|
/// specified size and relocation information, return a section that it
|
|
|
|
/// should be placed in.
|
|
|
|
const Section *
|
|
|
|
ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
|
|
|
|
unsigned ReloInfo) const {
|
|
|
|
// FIXME: IF this global requires a relocation, can we really put it in
|
|
|
|
// rodata??? This should check ReloInfo like darwin.
|
|
|
|
|
|
|
|
const char *SecName = 0;
|
|
|
|
switch (Size) {
|
|
|
|
default: break;
|
|
|
|
case 4: SecName = ".rodata.cst4"; break;
|
|
|
|
case 8: SecName = ".rodata.cst8"; break;
|
|
|
|
case 16: SecName = ".rodata.cst16"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SecName)
|
|
|
|
return getNamedSection(SecName,
|
2009-07-22 10:35:05 +00:00
|
|
|
SectionFlags::setEntitySize(SectionFlags::Mergeable,
|
2009-07-22 00:28:43 +00:00
|
|
|
Size));
|
|
|
|
|
|
|
|
return getReadOnlySection(); // .rodata
|
2008-08-07 09:50:34 +00:00
|
|
|
}
|
|
|
|
|
2009-07-24 17:02:17 +00:00
|
|
|
/// getFlagsForNamedSection - If this target wants to be able to infer
|
|
|
|
/// section flags based on the name of the section specified for a global
|
|
|
|
/// variable, it can implement this.
|
|
|
|
unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const {
|
|
|
|
unsigned Flags = 0;
|
|
|
|
if (Name[0] != '.') return 0;
|
|
|
|
|
|
|
|
// Some lame default implementation based on some magic section names.
|
|
|
|
if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
|
|
|
|
strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
|
|
|
|
strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
|
|
|
|
strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
|
|
|
|
Flags |= SectionFlags::BSS;
|
|
|
|
else if (strcmp(Name, ".tdata") == 0 ||
|
|
|
|
strncmp(Name, ".tdata.", 7) == 0 ||
|
|
|
|
strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
|
|
|
|
strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
|
|
|
|
Flags |= SectionFlags::TLS;
|
|
|
|
else if (strcmp(Name, ".tbss") == 0 ||
|
|
|
|
strncmp(Name, ".tbss.", 6) == 0 ||
|
|
|
|
strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
|
|
|
|
strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
|
|
|
|
Flags |= SectionFlags::BSS | SectionFlags::TLS;
|
|
|
|
|
|
|
|
return Flags;
|
|
|
|
}
|
|
|
|
|
2009-07-22 00:28:43 +00:00
|
|
|
|
2008-07-19 13:14:11 +00:00
|
|
|
const Section*
|
|
|
|
ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
|
2008-11-03 18:22:42 +00:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2008-07-19 13:14:11 +00:00
|
|
|
Constant *C = cast<GlobalVariable>(GV)->getInitializer();
|
2009-01-27 22:29:24 +00:00
|
|
|
const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
|
2008-07-19 13:14:11 +00:00
|
|
|
|
2009-05-09 07:06:46 +00:00
|
|
|
unsigned Size = TD->getTypeAllocSize(Ty);
|
2008-07-19 13:14:11 +00:00
|
|
|
if (Size <= 16) {
|
2008-08-07 09:54:40 +00:00
|
|
|
assert(getCStringSection() && "Should have string section prefix");
|
|
|
|
|
2008-07-19 13:14:11 +00:00
|
|
|
// We also need alignment here
|
2008-07-21 18:29:23 +00:00
|
|
|
unsigned Align = TD->getPrefTypeAlignment(Ty);
|
2008-07-19 13:14:11 +00:00
|
|
|
if (Align < Size)
|
|
|
|
Align = Size;
|
|
|
|
|
|
|
|
std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
|
|
|
|
unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
|
|
|
|
SectionFlags::Strings,
|
|
|
|
Size);
|
|
|
|
return getNamedSection(Name.c_str(), Flags);
|
|
|
|
}
|
|
|
|
|
2008-09-24 22:20:27 +00:00
|
|
|
return getReadOnlySection();
|
2008-07-19 13:14:11 +00:00
|
|
|
}
|
|
|
|
|
2008-08-16 12:57:07 +00:00
|
|
|
std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
|
2008-07-19 13:14:11 +00:00
|
|
|
std::string Flags = ",\"";
|
|
|
|
|
|
|
|
if (!(flags & SectionFlags::Debug))
|
|
|
|
Flags += 'a';
|
|
|
|
if (flags & SectionFlags::Code)
|
|
|
|
Flags += 'x';
|
|
|
|
if (flags & SectionFlags::Writeable)
|
|
|
|
Flags += 'w';
|
|
|
|
if (flags & SectionFlags::Mergeable)
|
|
|
|
Flags += 'M';
|
|
|
|
if (flags & SectionFlags::Strings)
|
|
|
|
Flags += 'S';
|
|
|
|
if (flags & SectionFlags::TLS)
|
|
|
|
Flags += 'T';
|
|
|
|
|
2008-08-07 09:55:06 +00:00
|
|
|
Flags += "\",";
|
|
|
|
|
|
|
|
// If comment string is '@', e.g. as on ARM - use '%' instead
|
|
|
|
if (strcmp(CommentString, "@") == 0)
|
|
|
|
Flags += '%';
|
|
|
|
else
|
|
|
|
Flags += '@';
|
2008-07-19 13:14:11 +00:00
|
|
|
|
|
|
|
// FIXME: There can be exceptions here
|
|
|
|
if (flags & SectionFlags::BSS)
|
2008-08-07 09:55:06 +00:00
|
|
|
Flags += "nobits";
|
2008-07-19 13:14:11 +00:00
|
|
|
else
|
2008-08-07 09:55:06 +00:00
|
|
|
Flags += "progbits";
|
2008-07-19 13:14:11 +00:00
|
|
|
|
|
|
|
if (unsigned entitySize = SectionFlags::getEntitySize(flags))
|
|
|
|
Flags += "," + utostr(entitySize);
|
|
|
|
|
|
|
|
return Flags;
|
|
|
|
}
|