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"
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const Section*
|
2008-08-08 17:56:50 +00:00
|
|
|
ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
|
2008-07-19 13:14:11 +00:00
|
|
|
SectionKind::Kind Kind = SectionKindForGlobal(GV);
|
|
|
|
|
|
|
|
if (const Function *F = dyn_cast<Function>(GV)) {
|
|
|
|
switch (F->getLinkage()) {
|
|
|
|
default: assert(0 && "Unknown linkage type!");
|
|
|
|
case Function::InternalLinkage:
|
|
|
|
case Function::DLLExportLinkage:
|
|
|
|
case Function::ExternalLinkage:
|
2008-09-24 22:16:33 +00:00
|
|
|
return TextSection;
|
2008-07-19 13:14:11 +00:00
|
|
|
case Function::WeakLinkage:
|
|
|
|
case Function::LinkOnceLinkage:
|
|
|
|
std::string Name = UniqueSectionForGlobal(GV, Kind);
|
|
|
|
unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
|
|
|
|
return getNamedSection(Name.c_str(), Flags);
|
|
|
|
}
|
|
|
|
} else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
|
2008-09-29 11:25:42 +00:00
|
|
|
if (GVar->mayBeOverridden()) {
|
2008-07-19 13:14:11 +00:00
|
|
|
std::string Name = UniqueSectionForGlobal(GVar, Kind);
|
|
|
|
unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
|
|
|
|
return getNamedSection(Name.c_str(), Flags);
|
|
|
|
} else {
|
|
|
|
switch (Kind) {
|
|
|
|
case SectionKind::Data:
|
2008-07-22 17:09:41 +00:00
|
|
|
case SectionKind::SmallData:
|
2008-09-24 22:16:33 +00:00
|
|
|
return DataSection;
|
2008-07-19 13:14:11 +00:00
|
|
|
case SectionKind::BSS:
|
2008-07-22 17:09:41 +00:00
|
|
|
case SectionKind::SmallBSS:
|
2008-07-19 13:14:11 +00:00
|
|
|
// ELF targets usually have BSS sections
|
|
|
|
return getBSSSection_();
|
|
|
|
case SectionKind::ROData:
|
2008-07-22 17:09:41 +00:00
|
|
|
case SectionKind::SmallROData:
|
2008-09-24 22:20:27 +00:00
|
|
|
return getReadOnlySection();
|
2008-07-19 13:14:11 +00:00
|
|
|
case SectionKind::RODataMergeStr:
|
|
|
|
return MergeableStringSection(GVar);
|
|
|
|
case SectionKind::RODataMergeConst:
|
|
|
|
return MergeableConstSection(GVar);
|
|
|
|
case SectionKind::ThreadData:
|
|
|
|
// ELF targets usually support TLS stuff
|
2008-09-24 22:17:06 +00:00
|
|
|
return TLSDataSection;
|
2008-07-19 13:14:11 +00:00
|
|
|
case SectionKind::ThreadBSS:
|
2008-09-24 22:17:06 +00:00
|
|
|
return TLSBSSSection;
|
2008-07-19 13:14:11 +00:00
|
|
|
default:
|
|
|
|
assert(0 && "Unsuported section kind for global");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
assert(0 && "Unsupported global");
|
2009-01-05 17:31:22 +00:00
|
|
|
|
|
|
|
return NULL;
|
2008-07-19 13:14:11 +00:00
|
|
|
}
|
|
|
|
|
2008-08-07 09:50:34 +00:00
|
|
|
const Section*
|
|
|
|
ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
|
|
|
|
// FIXME: Support data.rel stuff someday
|
|
|
|
return MergeableConstSection(Ty);
|
|
|
|
}
|
|
|
|
|
2008-07-19 13:14:11 +00:00
|
|
|
const Section*
|
|
|
|
ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
|
2008-09-24 22:17:27 +00:00
|
|
|
Constant *C = GV->getInitializer();
|
2008-08-07 09:51:02 +00:00
|
|
|
return MergeableConstSection(C->getType());
|
2008-08-07 09:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const Section*
|
|
|
|
ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
|
2008-11-03 18:22:42 +00:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2008-07-19 13:14:11 +00:00
|
|
|
|
|
|
|
// FIXME: string here is temporary, until stuff will fully land in.
|
|
|
|
// We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
|
|
|
|
// currently directly used by asmprinter.
|
2009-01-12 20:38:59 +00:00
|
|
|
unsigned Size = TD->getTypePaddedSize(Ty);
|
2008-07-19 13:14:11 +00:00
|
|
|
if (Size == 4 || Size == 8 || Size == 16) {
|
|
|
|
std::string Name = ".rodata.cst" + utostr(Size);
|
|
|
|
|
|
|
|
return getNamedSection(Name.c_str(),
|
|
|
|
SectionFlags::setEntitySize(SectionFlags::Mergeable,
|
|
|
|
Size));
|
|
|
|
}
|
|
|
|
|
2008-09-24 22:20:27 +00:00
|
|
|
return getReadOnlySection();
|
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();
|
|
|
|
const ConstantArray *CVA = cast<ConstantArray>(C);
|
2008-07-21 18:29:23 +00:00
|
|
|
const Type *Ty = CVA->getType()->getElementType();
|
2008-07-19 13:14:11 +00:00
|
|
|
|
2009-01-12 20:38:59 +00:00
|
|
|
unsigned Size = TD->getTypePaddedSize(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-07-22 17:09:41 +00:00
|
|
|
if (flags & SectionFlags::Small)
|
|
|
|
Flags += 's';
|
2008-07-19 13:14:11 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|