[ELF][MIPS] MIPS .reginfo sections handling

MIPS .reginfo section provides information on the registers used by
the code in the object file. Linker should collect this information and
write .reginfo section in the output file. This section contains a union
of used registers masks taken from input .reginfo sections and final
value of the `_gp` symbol.

For details see the "Register Information" section in Chapter 4 in the
following document:
ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf

The patch implements .reginfo sections handling with a couple missed
features: a) it does not put output .reginfo section into the separate
REGINFO segment; b) it does not merge `ri_cprmask` masks from input
section. These features will be implemented later.

Differential Revision: http://reviews.llvm.org/D15669

llvm-svn: 256119
This commit is contained in:
Simon Atanasyan 2015-12-20 10:57:34 +00:00
parent e85abf7269
commit 1d7df40711
8 changed files with 125 additions and 2 deletions

View File

@ -232,6 +232,9 @@ void elf2::ObjectFile<ELFT>::initializeSections(DenseSet<StringRef> &Comdats) {
Sections[I] = &InputSection<ELFT>::Discarded;
else if (Name == ".eh_frame")
Sections[I] = new (this->Alloc) EHInputSection<ELFT>(this, &Sec);
else if (Name == ".reginfo")
Sections[I] =
new (this->Alloc) MipsReginfoInputSection<ELFT>(this, &Sec);
else if (shouldMerge<ELFT>(Sec))
Sections[I] = new (this->Alloc) MergeInputSection<ELFT>(this, &Sec);
else

View File

@ -51,6 +51,8 @@ InputSectionBase<ELFT>::getOffset(uintX_t Offset) {
return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
case Merge:
return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
case MipsReginfo:
return cast<MipsReginfoInputSection<ELFT>>(this)->getOffset(Offset);
}
llvm_unreachable("Invalid section kind");
}
@ -320,6 +322,24 @@ MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
return Base + Addend;
}
template <class ELFT>
MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(ObjectFile<ELFT> *F,
const Elf_Shdr *Header)
: InputSectionBase<ELFT>(F, Header, InputSectionBase<ELFT>::MipsReginfo) {}
template <class ELFT>
uint32_t MipsReginfoInputSection<ELFT>::getGeneralMask() const {
ArrayRef<uint8_t> D = this->getSectionData();
if (D.size() != sizeof(Elf_Mips_RegInfo))
error("Invalid size of .reginfo section");
return reinterpret_cast<const Elf_Mips_RegInfo *>(D.data())->ri_gprmask;
}
template <class ELFT>
bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
}
namespace lld {
namespace elf2 {
template class InputSectionBase<object::ELF32LE>;
@ -341,5 +361,10 @@ template class MergeInputSection<object::ELF32LE>;
template class MergeInputSection<object::ELF32BE>;
template class MergeInputSection<object::ELF64LE>;
template class MergeInputSection<object::ELF64BE>;
template class MipsReginfoInputSection<object::ELF32LE>;
template class MipsReginfoInputSection<object::ELF32BE>;
template class MipsReginfoInputSection<object::ELF64LE>;
template class MipsReginfoInputSection<object::ELF64BE>;
}
}

View File

@ -35,7 +35,7 @@ protected:
ObjectFile<ELFT> *File;
public:
enum Kind { Regular, EHFrame, Merge };
enum Kind { Regular, EHFrame, Merge, MipsReginfo };
Kind SectionKind;
InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
@ -161,6 +161,25 @@ public:
static bool classof(const InputSectionBase<ELFT> *S);
};
// MIPS .reginfo section provides information on the registers used by the code
// in the object file. Linker should collect this information and write a single
// .reginfo section in the output file. The output section contains a union of
// used registers masks taken from input .reginfo sections and final value
// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
template <class ELFT>
class MipsReginfoInputSection : public InputSectionBase<ELFT> {
typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
public:
MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
uint32_t getGeneralMask() const;
static bool classof(const InputSectionBase<ELFT> *S);
};
} // namespace elf2
} // namespace lld

View File

@ -1389,6 +1389,27 @@ uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
}
template <class ELFT>
MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
: OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
this->Header.sh_addralign = 4;
this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
}
template <class ELFT>
void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
R->ri_gp_value = getMipsGpAddr<ELFT>();
R->ri_gprmask = GeneralMask;
}
template <class ELFT>
void MipsReginfoOutputSection<ELFT>::addSection(
MipsReginfoInputSection<ELFT> *S) {
GeneralMask |= S->getGeneralMask();
}
namespace lld {
namespace elf2 {
template class OutputSectionBase<ELF32LE>;
@ -1446,6 +1467,11 @@ template class EHOutputSection<ELF32BE>;
template class EHOutputSection<ELF64LE>;
template class EHOutputSection<ELF64BE>;
template class MipsReginfoOutputSection<ELF32LE>;
template class MipsReginfoOutputSection<ELF32BE>;
template class MipsReginfoOutputSection<ELF64LE>;
template class MipsReginfoOutputSection<ELF64BE>;
template class MergeOutputSection<ELF32LE>;
template class MergeOutputSection<ELF32BE>;
template class MergeOutputSection<ELF64LE>;

View File

@ -31,6 +31,7 @@ template <class ELFT> class EHInputSection;
template <class ELFT> class InputSection;
template <class ELFT> class InputSectionBase;
template <class ELFT> class MergeInputSection;
template <class ELFT> class MipsReginfoInputSection;
template <class ELFT> class OutputSection;
template <class ELFT> class ObjectFile;
template <class ELFT> class DefinedRegular;
@ -412,6 +413,20 @@ private:
uint32_t DtFlags1 = 0;
};
template <class ELFT>
class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
public:
MipsReginfoOutputSection();
void writeTo(uint8_t *Buf) override;
void addSection(MipsReginfoInputSection<ELFT> *S);
private:
uint32_t GeneralMask = 0;
};
// All output sections that are hadnled by the linker specially are
// globally accessible. Writer initializes them, so don't use them
// until Writer is initialized.

View File

@ -75,6 +75,7 @@ private:
SpecificBumpPtrAllocator<OutputSection<ELFT>> SecAlloc;
SpecificBumpPtrAllocator<MergeOutputSection<ELFT>> MSecAlloc;
SpecificBumpPtrAllocator<EHOutputSection<ELFT>> EHSecAlloc;
SpecificBumpPtrAllocator<MipsReginfoOutputSection<ELFT>> MReginfoSecAlloc;
BumpPtrAllocator Alloc;
std::vector<OutputSectionBase<ELFT> *> OutputSections;
unsigned getNumSections() const { return OutputSections.size() + 1; }
@ -618,6 +619,10 @@ template <class ELFT> void Writer<ELFT>::createSections() {
Sec = new (MSecAlloc.Allocate())
MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
break;
case InputSectionBase<ELFT>::MipsReginfo:
Sec = new (MReginfoSecAlloc.Allocate())
MipsReginfoOutputSection<ELFT>();
break;
}
OutputSections.push_back(Sec);
RegularSections.push_back(Sec);
@ -635,6 +640,10 @@ template <class ELFT> void Writer<ELFT>::createSections() {
static_cast<MergeOutputSection<ELFT> *>(Sec)
->addSection(cast<MergeInputSection<ELFT>>(C));
break;
case InputSectionBase<ELFT>::MipsReginfo:
static_cast<MipsReginfoOutputSection<ELFT> *>(Sec)
->addSection(cast<MipsReginfoInputSection<ELFT>>(C));
break;
}
}
}

View File

@ -68,7 +68,7 @@ __start:
# CHECK-NEXT: Link: 0
# CHECK-NEXT: Info: 0
# CHECK-NEXT: AddressAlignment: 4
# CHECK-NEXT: EntrySize: 0
# CHECK-NEXT: EntrySize: 24
# CHECK-NEXT: }
# CHECK-NEXT: Section {
# CHECK-NEXT: Index: 2

View File

@ -0,0 +1,26 @@
# Check MIPS .reginfo section generation.
# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t1.o
# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
# RUN: %S/Inputs/mips-dynamic.s -o %t2.o
# RUN: ld.lld %t1.o %t2.o -shared -o %t.so
# RUN: llvm-readobj -symbols -mips-reginfo %t.so | FileCheck %s
# REQUIRES: mips
.text
.globl __start
__start:
lw $t0,%call16(g1)($gp)
# CHECK: Name: _gp
# CHECK-NEXT: Value: 0x[[GP:[0-9A-F]+]]
# CHECK: MIPS RegInfo {
# CHECK-NEXT: GP: 0x[[GP]]
# CHECK-NEXT: General Mask: 0x10000101
# CHECK-NEXT: Co-Proc Mask0: 0x0
# CHECK-NEXT: Co-Proc Mask1: 0x0
# CHECK-NEXT: Co-Proc Mask2: 0x0
# CHECK-NEXT: Co-Proc Mask3: 0x0
# CHECK-NEXT: }