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);
|
2009-07-22 00:05:44 +00:00
|
|
|
if (GVar->hasInitializer() && TM.getRelocationModel() != Reloc::Static) {
|
2009-03-30 15:27:43 +00:00
|
|
|
Constant *C = GVar->getInitializer();
|
|
|
|
bool isConstant = GVar->isConstant();
|
2009-07-21 23:47:11 +00:00
|
|
|
|
|
|
|
// By default - all relocations in PIC mode would force symbol to be
|
|
|
|
// placed in r/w section.
|
2009-07-22 00:05:44 +00:00
|
|
|
switch (C->getRelocationInfo()) {
|
|
|
|
default: break;
|
|
|
|
case 1:
|
|
|
|
return isConstant ? SectionKind::DataRelROLocal :
|
|
|
|
SectionKind::DataRelLocal;
|
|
|
|
case 2:
|
|
|
|
return isConstant ? SectionKind::DataRelRO : SectionKind::DataRel;
|
|
|
|
}
|
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:
|
2009-07-20 01:03:30 +00:00
|
|
|
case Function::LinkerPrivateLinkage:
|
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) {
|
2009-07-21 21:29:08 +00:00
|
|
|
case SectionKind::Data:
|
|
|
|
case SectionKind::SmallData:
|
2008-09-24 22:16:33 +00:00
|
|
|
return DataSection;
|
2009-07-21 21:29:08 +00:00
|
|
|
case SectionKind::DataRel:
|
2009-03-30 15:27:03 +00:00
|
|
|
return DataRelSection;
|
2009-07-21 21:29:08 +00:00
|
|
|
case SectionKind::DataRelLocal:
|
2009-03-30 15:27:03 +00:00
|
|
|
return DataRelLocalSection;
|
2009-07-21 21:29:08 +00:00
|
|
|
case SectionKind::DataRelRO:
|
2009-03-30 15:27:03 +00:00
|
|
|
return DataRelROSection;
|
2009-07-21 21:29:08 +00:00
|
|
|
case SectionKind::DataRelROLocal:
|
2009-03-30 15:27:03 +00:00
|
|
|
return DataRelROLocalSection;
|
2009-07-21 21:29:08 +00:00
|
|
|
case SectionKind::BSS:
|
|
|
|
case SectionKind::SmallBSS:
|
2008-07-19 13:14:11 +00:00
|
|
|
// ELF targets usually have BSS sections
|
|
|
|
return getBSSSection_();
|
2009-07-21 21:29:08 +00:00
|
|
|
case SectionKind::ROData:
|
|
|
|
case SectionKind::SmallROData:
|
2008-09-24 22:20:27 +00:00
|
|
|
return getReadOnlySection();
|
2009-07-21 21:29:08 +00:00
|
|
|
case SectionKind::RODataMergeStr:
|
2008-07-19 13:14:11 +00:00
|
|
|
return MergeableStringSection(GVar);
|
2009-07-22 00:47:11 +00:00
|
|
|
case SectionKind::RODataMergeConst: {
|
|
|
|
const Type *Ty = GVar->getInitializer()->getType();
|
|
|
|
const TargetData *TD = TM.getTargetData();
|
|
|
|
return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
|
|
|
|
}
|
2009-07-21 21:29:08 +00:00
|
|
|
case SectionKind::ThreadData:
|
2008-07-19 13:14:11 +00:00
|
|
|
// ELF targets usually support TLS stuff
|
2008-09-24 22:17:06 +00:00
|
|
|
return TLSDataSection;
|
2009-07-21 21:29:08 +00:00
|
|
|
case SectionKind::ThreadBSS:
|
2008-09-24 22:17:06 +00:00
|
|
|
return TLSBSSSection;
|
2009-07-21 21:29:08 +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
|
|
|
}
|
|
|
|
|
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-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;
|
|
|
|
}
|