Skip to content

Commit

Permalink
[ELF][ARM64] Fix IFUNC relocations in -static-pie executables
Browse files Browse the repository at this point in the history
For a static position-independent executable, a libc's intitialization
routine reads the dynamic relocation table and apply them. It looks like
on ARM64, they simply apply relocations from the begining of the table
to end. Therefore, if an IFUNC relocation appears before RELATIVE relocs,
the IFUNC resolver would see an unrelocated value. So, we need to place
RELATIVE relocs before IFUNCs.

Fixes #382
  • Loading branch information
rui314 committed Mar 9, 2022
1 parent c98d276 commit 3d68824
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ void RelDynSection<E>::sort(Context<E> &ctx) {

auto get_rank = [](u32 r_type) {
switch (r_type) {
case E::R_IRELATIVE: return 0;
case E::R_RELATIVE: return 1;
default: return 2;
case E::R_RELATIVE: return 0;
case E::R_IRELATIVE: return 2;
default: return 1;
}
};

Expand Down
6 changes: 3 additions & 3 deletions elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1287,10 +1287,10 @@ void fix_synthetic_symbols(Context<E> &ctx) {
// the second attempt with wrong function addresses, causing a
// segmentation fault.
if (ctx.reldyn && ctx.arg.is_static && !ctx.arg.pie) {
start(ctx.__rel_iplt_start, ctx.reldyn);
stop(ctx.__rel_iplt_start, ctx.reldyn);
stop(ctx.__rel_iplt_end, ctx.reldyn);

ctx.__rel_iplt_end->shndx = -ctx.reldyn->shndx;
ctx.__rel_iplt_end->value = ctx.reldyn->shdr.sh_addr +
ctx.__rel_iplt_start->value -=
get_num_irelative_relocs(ctx) * sizeof(ElfRel<E>);
}

Expand Down

0 comments on commit 3d68824

Please sign in to comment.