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
|
|
|
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-30 17:37:43 +00:00
|
|
|
return (C->ContainsRelocations(Reloc::Global) ?
|
2009-03-30 15:27:43 +00:00
|
|
|
(isConstant ?
|
2009-03-30 17:37:43 +00:00
|
|
|
SectionKind::DataRelRO : SectionKind::DataRel) :
|
2009-03-30 15:27:43 +00:00
|
|
|
(isConstant ?
|
2009-03-30 17:37:43 +00:00
|
|
|
SectionKind::DataRelROLocal : SectionKind::DataRelLocal));
|
2009-03-30 15:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Kind;
|
|
|
|
}
|
|
|
|
|
2008-07-19 13:14:11 +00:00
|
|
|
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()) {
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Unknown linkage type!");
|
2009-01-15 20:18:42 +00:00
|
|
|
case Function::PrivateLinkage:
|
2008-07-19 13:14:11 +00:00
|
|
|
case Function::InternalLinkage:
|
|
|
|
case Function::DLLExportLinkage:
|
|
|
|
case Function::ExternalLinkage:
|
2008-09-24 22:16:33 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66339 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-07 15:45:40 +00:00
|
|
|
case Function::WeakAnyLinkage:
|
|
|
|
case Function::WeakODRLinkage:
|
|
|
|
case Function::LinkOnceAnyLinkage:
|
|
|
|
case Function::LinkOnceODRLinkage:
|
2008-07-19 13:14:11 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66339 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-07 15:45:40 +00:00
|
|
|
if (GVar->isWeakForLinker()) {
|
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;
|
2009-03-30 15:27:03 +00:00
|
|
|
case SectionKind::DataRel:
|
|
|
|
return DataRelSection;
|
|
|
|
case SectionKind::DataRelLocal:
|
|
|
|
return DataRelLocalSection;
|
|
|
|
case SectionKind::DataRelRO:
|
|
|
|
return DataRelROSection;
|
|
|
|
case SectionKind::DataRelROLocal:
|
|
|
|
return DataRelROLocalSection;
|
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:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Unsuported section kind for global");
|
2008-07-19 13:14:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("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-05-09 07:06:46 +00:00
|
|
|
unsigned Size = TD->getTypeAllocSize(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();
|
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-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;
|
|
|
|
}
|