2008-07-19 21:14:11 +08: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 17:50:34 +08:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2008-07-19 21:14:11 +08:00
|
|
|
#include "llvm/Target/ELFTargetAsmInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2008-11-04 02:22:42 +08:00
|
|
|
ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM)
|
|
|
|
: TargetAsmInfo(TM) {
|
2008-07-19 21:14:11 +08:00
|
|
|
|
|
|
|
BSSSection_ = getUnnamedSection("\t.bss",
|
|
|
|
SectionFlags::Writeable | SectionFlags::BSS);
|
2008-09-25 06:20:27 +08:00
|
|
|
ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None);
|
2008-09-25 06:17:06 +08:00
|
|
|
TLSDataSection = getNamedSection("\t.tdata",
|
|
|
|
SectionFlags::Writeable | SectionFlags::TLS);
|
|
|
|
TLSBSSSection = getNamedSection("\t.tbss",
|
2008-07-19 21:14:11 +08:00
|
|
|
SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
|
|
|
|
|
2009-03-30 23:27:03 +08: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 21:14:11 +08:00
|
|
|
}
|
|
|
|
|
2009-03-30 23:27:43 +08:00
|
|
|
SectionKind::Kind
|
|
|
|
ELFTargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
|
|
|
|
SectionKind::Kind Kind = TargetAsmInfo::SectionKindForGlobal(GV);
|
|
|
|
|
|
|
|
if (Kind != SectionKind::Data)
|
|
|
|
return Kind;
|
|
|
|
|
|
|
|
// Decide, whether we need data.rel stuff
|
|
|
|
const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
|
|
|
|
if (GVar->hasInitializer()) {
|
|
|
|
Constant *C = GVar->getInitializer();
|
|
|
|
bool isConstant = GVar->isConstant();
|
|
|
|
unsigned Reloc = RelocBehaviour();
|
|
|
|
if (Reloc != Reloc::None && C->ContainsRelocations(Reloc))
|
2009-03-31 01:37:43 +08:00
|
|
|
return (C->ContainsRelocations(Reloc::Global) ?
|
2009-03-30 23:27:43 +08:00
|
|
|
(isConstant ?
|
2009-03-31 01:37:43 +08:00
|
|
|
SectionKind::DataRelRO : SectionKind::DataRel) :
|
2009-03-30 23:27:43 +08:00
|
|
|
(isConstant ?
|
2009-03-31 01:37:43 +08:00
|
|
|
SectionKind::DataRelROLocal : SectionKind::DataRelLocal));
|
2009-03-30 23:27:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Kind;
|
|
|
|
}
|
|
|
|
|
2008-07-19 21:14:11 +08:00
|
|
|
const Section*
|
2008-08-09 01:56:50 +08:00
|
|
|
ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
|
2008-07-19 21:14:11 +08:00
|
|
|
SectionKind::Kind Kind = SectionKindForGlobal(GV);
|
|
|
|
|
|
|
|
if (const Function *F = dyn_cast<Function>(GV)) {
|
|
|
|
switch (F->getLinkage()) {
|
|
|
|
default: assert(0 && "Unknown linkage type!");
|
2009-01-16 04:18:42 +08:00
|
|
|
case Function::PrivateLinkage:
|
2008-07-19 21:14:11 +08:00
|
|
|
case Function::InternalLinkage:
|
|
|
|
case Function::DLLExportLinkage:
|
|
|
|
case Function::ExternalLinkage:
|
2008-09-25 06:16:33 +08:00
|
|
|
return TextSection;
|
Introduce new linkage types linkonce_odr, weak_odr, common_odr
and extern_weak_odr. These are the same as the non-odr versions,
except that they indicate that the global will only be overridden
by an *equivalent* global. In C, a function with weak linkage can
be overridden by a function which behaves completely differently.
This means that IP passes have to skip weak functions, since any
deductions made from the function definition might be wrong, since
the definition could be replaced by something completely different
at link time. This is not allowed in C++, thanks to the ODR
(One-Definition-Rule): if a function is replaced by another at
link-time, then the new function must be the same as the original
function. If a language knows that a function or other global can
only be overridden by an equivalent global, it can give it the
weak_odr linkage type, and the optimizers will understand that it
is alright to make deductions based on the function body. The
code generators on the other hand map weak and weak_odr linkage
to the same thing.
llvm-svn: 66339
2009-03-07 23:45:40 +08:00
|
|
|
case Function::WeakAnyLinkage:
|
|
|
|
case Function::WeakODRLinkage:
|
|
|
|
case Function::LinkOnceAnyLinkage:
|
|
|
|
case Function::LinkOnceODRLinkage:
|
2008-07-19 21:14:11 +08:00
|
|
|
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)) {
|
Introduce new linkage types linkonce_odr, weak_odr, common_odr
and extern_weak_odr. These are the same as the non-odr versions,
except that they indicate that the global will only be overridden
by an *equivalent* global. In C, a function with weak linkage can
be overridden by a function which behaves completely differently.
This means that IP passes have to skip weak functions, since any
deductions made from the function definition might be wrong, since
the definition could be replaced by something completely different
at link time. This is not allowed in C++, thanks to the ODR
(One-Definition-Rule): if a function is replaced by another at
link-time, then the new function must be the same as the original
function. If a language knows that a function or other global can
only be overridden by an equivalent global, it can give it the
weak_odr linkage type, and the optimizers will understand that it
is alright to make deductions based on the function body. The
code generators on the other hand map weak and weak_odr linkage
to the same thing.
llvm-svn: 66339
2009-03-07 23:45:40 +08:00
|
|
|
if (GVar->isWeakForLinker()) {
|
2008-07-19 21:14:11 +08: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-23 01:09:41 +08:00
|
|
|
case SectionKind::SmallData:
|
2008-09-25 06:16:33 +08:00
|
|
|
return DataSection;
|
2009-03-30 23:27:03 +08:00
|
|
|
case SectionKind::DataRel:
|
|
|
|
return DataRelSection;
|
|
|
|
case SectionKind::DataRelLocal:
|
|
|
|
return DataRelLocalSection;
|
|
|
|
case SectionKind::DataRelRO:
|
|
|
|
return DataRelROSection;
|
|
|
|
case SectionKind::DataRelROLocal:
|
|
|
|
return DataRelROLocalSection;
|
2008-07-19 21:14:11 +08:00
|
|
|
case SectionKind::BSS:
|
2008-07-23 01:09:41 +08:00
|
|
|
case SectionKind::SmallBSS:
|
2008-07-19 21:14:11 +08:00
|
|
|
// ELF targets usually have BSS sections
|
|
|
|
return getBSSSection_();
|
|
|
|
case SectionKind::ROData:
|
2008-07-23 01:09:41 +08:00
|
|
|
case SectionKind::SmallROData:
|
2008-09-25 06:20:27 +08:00
|
|
|
return getReadOnlySection();
|
2008-07-19 21:14:11 +08:00
|
|
|
case SectionKind::RODataMergeStr:
|
|
|
|
return MergeableStringSection(GVar);
|
|
|
|
case SectionKind::RODataMergeConst:
|
|
|
|
return MergeableConstSection(GVar);
|
|
|
|
case SectionKind::ThreadData:
|
|
|
|
// ELF targets usually support TLS stuff
|
2008-09-25 06:17:06 +08:00
|
|
|
return TLSDataSection;
|
2008-07-19 21:14:11 +08:00
|
|
|
case SectionKind::ThreadBSS:
|
2008-09-25 06:17:06 +08:00
|
|
|
return TLSBSSSection;
|
2008-07-19 21:14:11 +08:00
|
|
|
default:
|
|
|
|
assert(0 && "Unsuported section kind for global");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
assert(0 && "Unsupported global");
|
2009-01-06 01:31:22 +08:00
|
|
|
|
|
|
|
return NULL;
|
2008-07-19 21:14:11 +08:00
|
|
|
}
|
|
|
|
|
2008-08-07 17:50:34 +08:00
|
|
|
const Section*
|
|
|
|
ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
|
|
|
|
// FIXME: Support data.rel stuff someday
|
|
|
|
return MergeableConstSection(Ty);
|
|
|
|
}
|
|
|
|
|
2008-07-19 21:14:11 +08:00
|
|
|
const Section*
|
|
|
|
ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
|
2008-09-25 06:17:27 +08:00
|
|
|
Constant *C = GV->getInitializer();
|
2008-08-07 17:51:02 +08:00
|
|
|
return MergeableConstSection(C->getType());
|
2008-08-07 17:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const Section*
|
|
|
|
ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
|
2008-11-04 02:22:42 +08:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2008-07-19 21:14:11 +08: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-13 04:38:59 +08:00
|
|
|
unsigned Size = TD->getTypePaddedSize(Ty);
|
2008-07-19 21:14:11 +08: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-25 06:20:27 +08:00
|
|
|
return getReadOnlySection();
|
2008-07-19 21:14:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Section*
|
|
|
|
ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
|
2008-11-04 02:22:42 +08:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2008-07-19 21:14:11 +08:00
|
|
|
Constant *C = cast<GlobalVariable>(GV)->getInitializer();
|
2009-01-28 06:29:24 +08:00
|
|
|
const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
|
2008-07-19 21:14:11 +08:00
|
|
|
|
2009-01-13 04:38:59 +08:00
|
|
|
unsigned Size = TD->getTypePaddedSize(Ty);
|
2008-07-19 21:14:11 +08:00
|
|
|
if (Size <= 16) {
|
2008-08-07 17:54:40 +08:00
|
|
|
assert(getCStringSection() && "Should have string section prefix");
|
|
|
|
|
2008-07-19 21:14:11 +08:00
|
|
|
// We also need alignment here
|
2008-07-22 02:29:23 +08:00
|
|
|
unsigned Align = TD->getPrefTypeAlignment(Ty);
|
2008-07-19 21:14:11 +08: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-25 06:20:27 +08:00
|
|
|
return getReadOnlySection();
|
2008-07-19 21:14:11 +08:00
|
|
|
}
|
|
|
|
|
2008-08-16 20:57:07 +08:00
|
|
|
std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
|
2008-07-19 21:14:11 +08: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-23 01:09:41 +08:00
|
|
|
if (flags & SectionFlags::Small)
|
|
|
|
Flags += 's';
|
2008-07-19 21:14:11 +08:00
|
|
|
|
2008-08-07 17:55:06 +08:00
|
|
|
Flags += "\",";
|
|
|
|
|
|
|
|
// If comment string is '@', e.g. as on ARM - use '%' instead
|
|
|
|
if (strcmp(CommentString, "@") == 0)
|
|
|
|
Flags += '%';
|
|
|
|
else
|
|
|
|
Flags += '@';
|
2008-07-19 21:14:11 +08:00
|
|
|
|
|
|
|
// FIXME: There can be exceptions here
|
|
|
|
if (flags & SectionFlags::BSS)
|
2008-08-07 17:55:06 +08:00
|
|
|
Flags += "nobits";
|
2008-07-19 21:14:11 +08:00
|
|
|
else
|
2008-08-07 17:55:06 +08:00
|
|
|
Flags += "progbits";
|
2008-07-19 21:14:11 +08:00
|
|
|
|
|
|
|
if (unsigned entitySize = SectionFlags::getEntitySize(flags))
|
|
|
|
Flags += "," + utostr(entitySize);
|
|
|
|
|
|
|
|
return Flags;
|
|
|
|
}
|