[PATCH] elf: Support DT_RELR relative relocation format [BZ #27924]

Jan Beulich jbeulich@suse.com
Mon Oct 11 07:48:03 GMT 2021


On 08.10.2021 08:57, Fangrui Song via Binutils wrote:
> --- a/elf/dynamic-link.h
> +++ b/elf/dynamic-link.h
> @@ -192,6 +192,33 @@ elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
>  #  define ELF_DYNAMIC_DO_RELA(map, scope, lazy, skip_ifunc) /* Nothing to do.  */
>  # endif
>  
> +# define ELF_DYNAMIC_DO_RELR(map)					      \
> +  do {									      \
> +    ElfW(Addr) l_addr = (map)->l_addr, base = 0, start;			      \
> +    const ElfW(Relr) *r = 0, *end = 0;					      \
> +    if (!(map)->l_info[DT_RELR])					      \
> +      break;								      \
> +    start = D_PTR((map), l_info[DT_RELR]);				      \
> +    r = (const ElfW(Relr) *)start;					      \
> +    end = (const ElfW(Relr) *)(start + (map)->l_info[DT_RELRSZ]->d_un.d_val); \
> +    for (; r < end; ++r) {						      \
> +      ElfW(Relr) entry = *r;						      \
> +      if ((entry & 1) == 0) {						      \
> +	*((ElfW(Addr) *)(l_addr + entry)) += l_addr;			      \
> +	base = entry + sizeof(ElfW(Addr));				      \
> +	continue;							      \
> +      }									      \
> +      ElfW(Addr) offset = base;						      \
> +      do {								      \
> +	entry >>= 1;							      \
> +	if ((entry & 1) != 0)						      \
> +	  *((ElfW(Addr) *)(l_addr + offset)) += l_addr;			      \
> +	offset += sizeof(ElfW(Addr));					      \
> +      } while (entry != 0);						      \
> +      base += (8 * sizeof(ElfW(Relr)) - 1) * sizeof(ElfW(Addr));	      \

While in line with the proposed spec additions I'm afraid the uses of
ElfW(Addr) here aren't universally correct: You assume that ELF
container type (size) expresses an aspect of the ABI. While this is
indeed the case for several arch-es, I think this has been a mistake.
IA-64, while meanwhile mostly dead, is (was) an example where 64-bit
code can validly live in a 32-bit ELF container (at least as far as
the psABI is concerned; I have no idea whether glibc actually
followed the spec). There's a separate ELF header flag indicating the
ABI, and hence the size of a pointer.

> --- /dev/null
> +++ b/elf/tst-relr.c
> @@ -0,0 +1,20 @@
> +static int o, x;
> +void *arr[] = {
> +  &o, &o, &o, &o, &o, &o, &o, &o, &o, &o, &o, &o, &o, &o, &o, &o,
> +  0,
> +  &x, &x, &x, &x, &x, &x, &x, &x, &x, &x, &x, &x, &x, &x, &x, &x,
> +};

Personally I consider this overly simplistic a testcase. The two
sequences are fully identical, have no gaps except for the one in
the middle, and there's also no interleaving / mixing of pointers.
None of this should matter as the specific symbol the relocation
is for is not supposed to be of interest, but in a testcase you
want to make sure none of this has an effect.

I also wonder whether it is a good idea to have a testcase with
"fundamentally" different behavior on 32-bit vs 64-bit: The former
will require two RELR entries afaict, while the latter will get
away with one here.

Jan

> +static int
> +do_test (void)
> +{
> +  for (int i = 0; i < 16; i++)
> +    if (arr[i] != &o)
> +      return 1;
> +  for (int i = 17; i < 33; i++)
> +    if (arr[i] != &x)
> +      return 1;
> +  return 0;
> +}
> +
> +#include <support/test-driver.c>
> 



More information about the Binutils mailing list