Skip to content

[StaticMirror] Add support for GLOB_DAT relocations. #75905

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

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
37 changes: 37 additions & 0 deletions lib/StaticMirror/ObjectFileContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ void Image::scanMachO(const llvm::object::MachOObjectFile *O) {
}
}

// We only support these for AArch64, ARM and x86-64 at present
static uint32_t getELFGlobDatRelocationType(uint32_t machine) {
switch (machine) {
case llvm::ELF::EM_AARCH64:
return llvm::ELF::R_AARCH64_GLOB_DAT;
case llvm::ELF::EM_ARM:
return llvm::ELF::R_ARM_GLOB_DAT;
case llvm::ELF::EM_X86_64:
return llvm::ELF::R_X86_64_GLOB_DAT;
default:
return 0;
}
}

template <typename ELFT>
void Image::scanELFType(const llvm::object::ELFObjectFile<ELFT> *O) {
using namespace llvm::ELF;
Expand Down Expand Up @@ -147,6 +161,7 @@ void Image::scanELFType(const llvm::object::ELFObjectFile<ELFT> *O) {

auto machine = O->getELFFile().getHeader().e_machine;
auto relativeRelocType = llvm::object::getELFRelativeRelocationType(machine);
auto globDatRelocType = getELFGlobDatRelocationType(machine);

for (auto &S : static_cast<const llvm::object::ELFObjectFileBase *>(O)
->dynamic_relocation_sections()) {
Expand All @@ -163,6 +178,28 @@ void Image::scanELFType(const llvm::object::ELFObjectFile<ELFT> *O) {
continue;
}

// `getRelocationResolver` doesn't handle GLOB_DAT relocations, so we
// also have to do that ourselves.
if (globDatRelocType && R.getType() == globDatRelocType) {
auto symbol = R.getSymbol();
auto name = symbol->getName();
if (!name) {
llvm::consumeError(name.takeError());
continue;
}

// On x86-64, this is just S, but on other architectures it is
// usually S + A.
uint64_t addend = 0;
if (isRela && machine != llvm::ELF::EM_X86_64) {
auto rela = O->getRela(R.getRawDataRefImpl());
addend = rela->r_addend;
}

DynamicRelocations.insert({R.getOffset(), {*name, addend}});
continue;
}

if (!resolverSupports(R.getType()))
continue;
auto symbol = R.getSymbol();
Expand Down