gold --emit-relocs

Cary Coutant ccoutant@gmail.com
Wed Aug 19 07:39:00 GMT 2015


> A symbol value in an ELF final linked binary is absolute, in contrast
> to a relocatable object file where the value is section relative.  For
> --emit-relocs it is therefore incorrect to use the value of a section
> symbol as the addend when adjusting relocs against input section
> symbols to output section symbols.
>
> I believe it is correct to always subtract os->address() from the
> addend.  Relocatable linking will set output section addresses to
> zero, but if for some reason the addresses changed to be non-zero then
> you'd need to subtract os->address() for ld -r too.

Yes, I agree with your analysis. Thanks!

> diff --git a/gold/target-reloc.h b/gold/target-reloc.h
> index c135459..3688e4e 100644
> --- a/gold/target-reloc.h
> +++ b/gold/target-reloc.h
> @@ -702,6 +702,18 @@ relocate_relocs(
>                 gold_assert(os != NULL);
>                 gold_assert(os->needs_symtab_index());
>                 new_symndx = os->symtab_index();
> +               if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
> +                 {
> +                   typename elfcpp::Elf_types<size>::Elf_Swxword addend;
> +                   const Symbol_value<size>* psymval;
> +                   psymval = object->local_symbol(r_sym);
> +                   addend = Reloc_types<sh_type, size, big_endian>::
> +                     get_reloc_addend(&reloc);
> +                   addend = psymval->value(object, addend);
> +                   addend -= os->address();
> +                   Reloc_types<sh_type, size, big_endian>::
> +                     set_reloc_addend(&reloc_write, addend);
> +                 }

Rather than move this code to here, I'd prefer to leave it where it
is, but promote "os" to the scope of the loop and adjust the addend
using os->address():

diff --git a/gold/target-reloc.h b/gold/target-reloc.h
index c135459..89906af 100644
--- a/gold/target-reloc.h
+++ b/gold/target-reloc.h
@@ -666,6 +666,7 @@ relocate_relocs(

       // Get the new symbol index.

+      Output_section* os = NULL;
       unsigned int new_symndx;
       if (r_sym < local_count)
        {
@@ -698,7 +699,7 @@ relocate_relocs(
                unsigned int shndx =
                  object->local_symbol_input_shndx(r_sym, &is_ordinary);
                gold_assert(is_ordinary);
-               Output_section* os = object->output_section(shndx);
+               os = object->output_section(shndx);
                gold_assert(os != NULL);
                gold_assert(os->needs_symtab_index());
                new_symndx = os->symtab_index();
@@ -780,7 +781,8 @@ relocate_relocs(
                typename elfcpp::Elf_types<size>::Elf_Swxword addend;
                addend = Reloc_types<sh_type, size, big_endian>::
                           get_reloc_addend(&reloc);
-               addend = psymval->value(object, addend);
+               gold_assert(os != NULL);
+               addend = psymval->value(object, addend) - os->address();
                Reloc_types<sh_type, size, big_endian>::
                  set_reloc_addend(&reloc_write, addend);
              }

... And a similar change in powerpc.cc.

-cary



More information about the Binutils mailing list