Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BOLT] Fix order of R_*_IRELATIVE in .rela.plt #106515

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bolt/include/bolt/Core/Relocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ struct Relocation {
/// Return code for a RELATIVE relocation
static uint64_t getRelative();

/// Return code for a IRELATIVE relocation
static uint64_t getIRelative();

/// Return true if this relocation is PC-relative. Return false otherwise.
bool isPCRelative() const { return isPCRelative(Type); }

Expand Down
15 changes: 14 additions & 1 deletion bolt/lib/Core/Relocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ bool Relocation::isIRelative(uint64_t Type) {
case Triple::aarch64:
return Type == ELF::R_AARCH64_IRELATIVE;
case Triple::riscv64:
llvm_unreachable("not implemented");
return Type == ELF::R_RISCV_IRELATIVE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace the whole function body with return Type == getIRelative();?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace the whole function body with return Type == getIRelative();?

Some extensions may introduce new variants(R_AARCH64_AUTH_[I]RELATIVE), but currently, BOLT has no plans to support them, so I think we can make this chang. Do you have any suggestions?

case Triple::x86_64:
return Type == ELF::R_X86_64_IRELATIVE;
}
Expand Down Expand Up @@ -1009,6 +1009,19 @@ uint64_t Relocation::getRelative() {
}
}

uint64_t Relocation::getIRelative() {
switch (Arch) {
default:
llvm_unreachable("Unsupported architecture");
case Triple::aarch64:
return ELF::R_AARCH64_IRELATIVE;
case Triple::riscv64:
return ELF::R_RISCV_IRELATIVE;
case Triple::x86_64:
return ELF::R_X86_64_IRELATIVE;
}
}

size_t Relocation::emit(MCStreamer *Streamer) const {
const size_t Size = getSizeForType(Type);
const auto *Value = createExpr(Streamer);
Expand Down
19 changes: 16 additions & 3 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5185,12 +5185,15 @@ RewriteInstance::patchELFAllocatableRelaSections(ELFObjectFile<ELFT> *File) {

DynamicRelativeRelocationsCount = 0;

const bool HasIRelativeInPLT =
IsJmpRelocation.contains(Relocation::getIRelative());

auto writeRela = [&OS](const Elf_Rela *RelA, uint64_t &Offset) {
OS.pwrite(reinterpret_cast<const char *>(RelA), sizeof(*RelA), Offset);
Offset += sizeof(*RelA);
};

auto writeRelocations = [&](bool PatchRelative) {
auto writeRelocations = [&](bool PatchRelative, bool PatchIRelativeInPLT) {
for (BinarySection &Section : BC->allocatableSections()) {
const uint64_t SectionInputAddress = Section.getAddress();
uint64_t SectionAddress = Section.getOutputAddress();
Expand All @@ -5199,9 +5202,14 @@ RewriteInstance::patchELFAllocatableRelaSections(ELFObjectFile<ELFT> *File) {

for (const Relocation &Rel : Section.dynamicRelocations()) {
const bool IsRelative = Rel.isRelative();
const bool IsIRelativeInPLT = HasIRelativeInPLT && Rel.isIRelative();

if (PatchRelative != IsRelative)
continue;

if (PatchIRelativeInPLT != IsIRelativeInPLT)
continue;

if (IsRelative)
++DynamicRelativeRelocationsCount;

Expand Down Expand Up @@ -5249,8 +5257,13 @@ RewriteInstance::patchELFAllocatableRelaSections(ELFObjectFile<ELFT> *File) {
// The dynamic linker expects all R_*_RELATIVE relocations in RELA
// to be emitted first.
if (!DynamicRelrAddress)
writeRelocations(/* PatchRelative */ true);
writeRelocations(/* PatchRelative */ false);
writeRelocations(/* PatchRelative */ true, /* PatchIRelativeInPLT */ false);
writeRelocations(/* PatchRelative */ false, /* PatchIRelativeInPLT */ false);

// Place R_*_IRELATIVE after all R_*_JUMP_SLOT relocations emitted if
// R_*_IRELATIVE is presented at .rela.plt
if (HasIRelativeInPLT)
writeRelocations(/* PatchRelative */ false, /* PatchIRelativeInPLT */ true);

auto fillNone = [&](uint64_t &Offset, uint64_t EndOffset) {
if (!Offset)
Expand Down
Loading
Loading