public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Relocations against STN_UNDEF
@ 2010-09-23 15:37 Thomas Schwinge
  2010-09-24  0:12 ` Alan Modra
  0 siblings, 1 reply; 12+ messages in thread
From: Thomas Schwinge @ 2010-09-23 15:37 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 5740 bytes --]

Hello!

The ELF standard says that STN_UNDEF (symbol index zero, the undefined
symbol index) is to be marked as SHN_UNDEF, undefined symbol.  There is
one exception however: during relocations processing, a relocation
against STN_UNDEF shall be treated as a symbol value of zero.

The BFD code makes use of this, for example when it is writing out all
relocations in bfd/elfcode.h:elf_write_relocs:

    [...]
      1000    for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
      1001      {
      1002        Elf_Internal_Rela src_rela;
      1003        arelent *ptr;
      1004        asymbol *sym;
      1005        int n;
      1006  
      1007        ptr = sec->orelocation[idx];
      1008        sym = *ptr->sym_ptr_ptr;
      1009        if (sym == last_sym)
      1010          n = last_sym_idx;
      1011        else if (bfd_is_abs_section (sym->section) && sym->value == 0)
      1012          n = STN_UNDEF;
      1013        else
      1014          {
      1015            last_sym = sym;
      1016            n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
    [...]

That is, if the symbol is (internally) listed in the absolute sections
and it has a value of zero, simply a relocation against STN_UNDEF is
emitted.


For example on ARM, the bl instruction does a PC-relative jump, so to
jump to an absolute value of 0x10000, ``bl 0x10000'' has to keep its
relocation until the final linking is done, to be converted to the proper
PC-relative offset.

    $ cat < ~/sgxx/issue8612/bl_ABS.s
            bl 0x10000
    $ "$PWD"_install/bin/*-as -o bl_ABS.o ~/sgxx/issue8612/bl_ABS.s
    $ "$PWD"_install/bin/*-readelf -r bl_ABS.o 
    
    Relocation section '.rel.text' at offset 0x25c contains 1 entries:
     Offset     Info    Type            Sym.Value  Sym. Name
    00000000  0000001c R_ARM_CALL       

This info value translates to relocation type 28 (R_ARM_CALL) against
symbol index zero (STN_UNDEF).

objdump even translates this that it's an ABSolut value, and no longer
STN_UNDEF:

    $ "$PWD"_install/bin/*-objdump -dr bl_ABS.o
    
    bl_ABS.o:     file format elf32-littlearm
    
    
    Disassembly of section .text:
    
    00000000 <.text>:
       0:   eb003ffe        bl      10000 <.text+0x10000>
                            0: R_ARM_CALL   *ABS*

This STN_UNDEF -> *ABS* conversion is done when reading in the
relocations, in bfd/elfcode.h:elf_slurp_reloc_table_from_section:

      1469        if (ELF_R_SYM (rela.r_info) == 0)
      1470          relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;

That is, if the index is zero -- should use STN_UNDEF here instead of the
zero constant, I'd say? -- then this relocation is marked as being in
BFD's absolute section, instead of undefined, as it'd be from the symbol
table.  This is important, as relocations against undefined symbols will
be processed differently from those against absolute values, etc.

However, this function elf_slurp_reloc_table_from_section for reading in
relocations is not being used in case of LD linking an executable -- the
STN_UNDEF symbol remains in BFD's internal undefined section, and linking
thus fails:

    $ "$PWD"_install/bin/*-ld -o bl_ABS bl_ABS.o
    /scratch/thomas/binutils/HEAD_build_arm-none-eabi_install/bin/arm-none-eabi-ld: warning: cannot find entry symbol _start; defaulting to 0000000000008000
    bl_ABS.o:(.text+0x0): undefined reference to `no symbol'

I wonder why we need this code duplicated in (at least) two places; why
do objdump and ld use different code paths for reading in relocations?

Here is a patch to fix this issue for the ld code path.  There are no
regressions with a --target=arm-none-eabi config.  What other testing
should be done, as this is against a generic (not target-dependent) file?

Index: bfd/elflink.c
===================================================================
RCS file: /cvs/src/src/bfd/elflink.c,v
retrieving revision 1.378
diff -u -p -r1.378 elflink.c
--- bfd/elflink.c	9 Sep 2010 09:55:03 -0000	1.378
+++ bfd/elflink.c	11 Sep 2010 07:32:44 -0000
@@ -9135,6 +9135,13 @@ elf_link_input_bfd (struct elf_final_lin
 	return FALSE;
     }
 
+  /* STN_UNDEF is symbol index zero.  During relocations processing it shall
+     resolve to a symbol value of zero.
+
+     For that to happen, we have to make sure that it will be registered with
+     the "absolute" section.  */
+  set_symbol_value (input_bfd, isymbuf, locsymcount, STN_UNDEF, 0);
+
   /* Find local symbol sections and adjust values of symbols in
      SEC_MERGE sections.  Write out those local symbols we know are
      going into the output file.  */

Success with patched BFD:

    $ "$PWD"_install/bin/*-ld -o bl_ABS bl_ABS.o
    /scratch/thomas/binutils/HEAD_build_arm-none-eabi_install/bin/arm-none-eabi-ld: warning: cannot find entry symbol _start; defaulting to 0000000000008000
    $ "$PWD"_install/bin/*-readelf -r bl_ABS
    
    There are no relocations in this file.
    $ "$PWD"_install/bin/*-objdump -dr bl_ABS
    
    bl_ABS:     file format elf32-littlearm
    
    
    Disassembly of section .text:
    
    00008000 <__data_start-0x8004>:
        8000:       eb001ffe        bl      10000 <__bss_end__-0x4>


This was my first encounter with BFD code, and apart from the time I have
now spent on it, I don't know much about how / where it originated, so
I'd like your input on this matter: is my patch the correct one, or
should this issue be solved differently?  Should the two code paths of
reading in relocations be unified, or is there perhaps a reason they're
different?


Regards,
 Thomas

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Relocations against STN_UNDEF
  2010-09-23 15:37 Relocations against STN_UNDEF Thomas Schwinge
@ 2010-09-24  0:12 ` Alan Modra
  2010-09-24  7:54   ` Thomas Schwinge
                     ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Alan Modra @ 2010-09-24  0:12 UTC (permalink / raw)
  To: Thomas Schwinge; +Cc: binutils

On Thu, Sep 23, 2010 at 05:37:01PM +0200, Thomas Schwinge wrote:
>       1469        if (ELF_R_SYM (rela.r_info) == 0)
>       1470          relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
> 
> That is, if the index is zero -- should use STN_UNDEF here instead of the
> zero constant, I'd say?

Yes.

>     $ "$PWD"_install/bin/*-ld -o bl_ABS bl_ABS.o
>     /scratch/thomas/binutils/HEAD_build_arm-none-eabi_install/bin/arm-none-eabi-ld: warning: cannot find entry symbol _start; defaulting to 0000000000008000
>     bl_ABS.o:(.text+0x0): undefined reference to `no symbol'

ARM fails because the arm backend specifically checks for undefined
local symbols.  I'd say the arm backend check needs fixing (or
removing) rather than changing the common elflink code.

> I wonder why we need this code duplicated in (at least) two places; why
> do objdump and ld use different code paths for reading in relocations?

Efficiency.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Relocations against STN_UNDEF
  2010-09-24  0:12 ` Alan Modra
@ 2010-09-24  7:54   ` Thomas Schwinge
  2010-09-24  9:17     ` Alan Modra
  2010-09-24  9:46   ` Relocations against STN_UNDEF Thomas Schwinge
  2010-09-24 11:26   ` [ARM] " Thomas Schwinge
  2 siblings, 1 reply; 12+ messages in thread
From: Thomas Schwinge @ 2010-09-24  7:54 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 1222 bytes --]

Hello!

On 2010-09-24 00:12, Alan Modra wrote:
> On Thu, Sep 23, 2010 at 05:37:01PM +0200, Thomas Schwinge wrote:
>>     $ "$PWD"_install/bin/*-ld -o bl_ABS bl_ABS.o
>>     /scratch/thomas/binutils/HEAD_build_arm-none-eabi_install/bin/arm-none-eabi-ld: warning: cannot find entry symbol _start; defaulting to 0000000000008000
>>     bl_ABS.o:(.text+0x0): undefined reference to `no symbol'
>
> ARM fails because the arm backend specifically checks for undefined
> local symbols.  I'd say the arm backend check needs fixing (or
> removing) rather than changing the common elflink code.

Surely enough we could just change the ARM backend, but I'm not yet
really convinced that this is the most appropriate approach: if we agree
that BFD internally shall register STN_UNDEF with bfd_abs_section
instead of bfd_und_section (as demonstrated in other parts of the code
that I quoted), then why not make it like this during relocations
processing, too?  Or is it that you think this change may be disruptive
for other backends?


>> I wonder why we need this code duplicated in (at least) two places; why
>> do objdump and ld use different code paths for reading in relocations?
>
> Efficiency.

Uhm, okay...


Regards,
 Thomas

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Relocations against STN_UNDEF
  2010-09-24  7:54   ` Thomas Schwinge
@ 2010-09-24  9:17     ` Alan Modra
  2010-09-24 10:37       ` BFD, GAS, LD internal manuals (was: Relocations against STN_UNDEF) Thomas Schwinge
  0 siblings, 1 reply; 12+ messages in thread
From: Alan Modra @ 2010-09-24  9:17 UTC (permalink / raw)
  To: Thomas Schwinge; +Cc: binutils

On Fri, Sep 24, 2010 at 09:54:25AM +0200, Thomas Schwinge wrote:
> On 2010-09-24 00:12, Alan Modra wrote:
> > ARM fails because the arm backend specifically checks for undefined
> > local symbols.  I'd say the arm backend check needs fixing (or
> > removing) rather than changing the common elflink code.
> 
> Surely enough we could just change the ARM backend, but I'm not yet
> really convinced that this is the most appropriate approach: if we agree
> that BFD internally shall register STN_UNDEF with bfd_abs_section
> instead of bfd_und_section (as demonstrated in other parts of the code
> that I quoted),

What is done elsewhere to make ELF relocs fit into the BFD framework
isn't particularly relevant to the ELF linker.

> then why not make it like this during relocations
> processing, too?

Why do extra work just for ARM?

>  Or is it that you think this change may be disruptive
> for other backends?

The thought had crossed my mind.

> >> I wonder why we need this code duplicated in (at least) two places; why
> >> do objdump and ld use different code paths for reading in relocations?
> >
> > Efficiency.
> 
> Uhm, okay...

To expand a little, the general BFD code for reading relocs wants to
return them as an arelent array, using symbols from an asymbol *
array.  By processing the relocs directly the ELF linker saves time
and memory;  No need to convert ELF symbols to BFD asymbols, and no
need to set up an arelent array.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Relocations against STN_UNDEF
  2010-09-24  0:12 ` Alan Modra
  2010-09-24  7:54   ` Thomas Schwinge
@ 2010-09-24  9:46   ` Thomas Schwinge
  2010-09-24 11:33     ` Alan Modra
  2010-09-24 11:26   ` [ARM] " Thomas Schwinge
  2 siblings, 1 reply; 12+ messages in thread
From: Thomas Schwinge @ 2010-09-24  9:46 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 30687 bytes --]

Hello!

On 2010-09-24 00:12, Alan Modra wrote:
> On Thu, Sep 23, 2010 at 05:37:01PM +0200, Thomas Schwinge wrote:
>>       1469        if (ELF_R_SYM (rela.r_info) == 0)
>>       1470          relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
>> 
>> That is, if the index is zero -- should use STN_UNDEF here instead of the
>> zero constant, I'd say?
>
> Yes.

There are quite some more of these (for most, grep was my friend).


I caught one bug (harmless, as both are defined to zero):

bfd/
2010-09-24  Thomas Schwinge  <thomas@codesourcery.com>

	* elflink.c (bfd_elf_reloc_symbol_deleted_p): Compare the symbol index
	to STN_UNDEF, not SHN_UNDEF.

Index: bfd/elflink.c
===================================================================
RCS file: /cvs/src/src/bfd/elflink.c,v
retrieving revision 1.379
diff -u -p -r1.379 elflink.c
--- bfd/elflink.c	16 Sep 2010 00:06:11 -0000	1.379
+++ bfd/elflink.c	24 Sep 2010 09:28:12 -0000
@@ -12196,7 +12203,7 @@ bfd_elf_reloc_symbol_deleted_p (bfd_vma 
 	continue;
 
       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
-      if (r_symndx == SHN_UNDEF)
+      if (r_symndx == STN_UNDEF)
 	return TRUE;
 
       if (r_symndx >= rcookie->locsymcount


The following ones may seem a bit excessive, but such extra verbosity
(using STN_UNDEF instead of 0) does help when reading the code for the
first time, like I did.  In case this is accepted, is the following
ChangeLog sufficient?  I have not done any testing; it's a purely
mechanical change.

2010-09-24  Thomas Schwinge  <thomas@codesourcery.com>

	Use STN_UNDEF when referring to the undefined symbol index, instead of
	a blank zero constant.

Index: bfd/elf32-arm.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-arm.c,v
retrieving revision 1.241
diff -u -p -r1.241 elf32-arm.c
--- bfd/elf32-arm.c	26 Aug 2010 10:32:26 -0000	1.241
+++ bfd/elf32-arm.c	24 Sep 2010 09:28:11 -0000
@@ -9063,7 +9063,7 @@ elf32_arm_relocate_section (bfd *       
 	    name = bfd_section_name (input_bfd, sec);
 	}
 
-      if (r_symndx != 0
+      if (r_symndx != STN_UNDEF
 	  && r_type != R_ARM_NONE
 	  && (h == NULL
 	      || h->root.type == bfd_link_hash_defined
@@ -10902,7 +10902,7 @@ elf32_arm_check_relocs (bfd *abfd, struc
 	  /* PR 9934: It is possible to have relocations that do not
 	     refer to symbols, thus it is also possible to have an
 	     object file containing relocations but no symbol table.  */
-	  && (r_symndx > 0 || nsyms > 0))
+	  && (r_symndx > STN_UNDEF || nsyms > 0))
 	{
 	  (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
 				   r_symndx);
Index: bfd/elf32-cris.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-cris.c,v
retrieving revision 1.112
diff -u -p -r1.112 elf32-cris.c
--- bfd/elf32-cris.c	25 Aug 2010 14:53:41 -0000	1.112
+++ bfd/elf32-cris.c	24 Sep 2010 09:28:11 -0000
@@ -1493,7 +1493,7 @@ cris_elf_relocate_section (bfd *output_b
 	case R_CRIS_16:
 	case R_CRIS_32:
 	  if (info->shared
-	      && r_symndx != 0
+	      && r_symndx != STN_UNDEF
 	      && (input_section->flags & SEC_ALLOC) != 0
 	      && ((r_type != R_CRIS_8_PCREL
 		   && r_type != R_CRIS_16_PCREL
Index: bfd/elf32-hppa.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-hppa.c,v
retrieving revision 1.174
diff -u -p -r1.174 elf32-hppa.c
--- bfd/elf32-hppa.c	25 Aug 2010 14:53:41 -0000	1.174
+++ bfd/elf32-hppa.c	24 Sep 2010 09:28:11 -0000
@@ -4472,7 +4472,7 @@ static enum elf_reloc_type_class
 elf32_hppa_reloc_type_class (const Elf_Internal_Rela *rela)
 {
   /* Handle TLS relocs first; we don't want them to be marked
-     relative by the "if (ELF32_R_SYM (rela->r_info) == 0)"
+     relative by the "if (ELF32_R_SYM (rela->r_info) == STN_UNDEF)"
      check below.  */
   switch ((int) ELF32_R_TYPE (rela->r_info))
     {
@@ -4482,7 +4482,7 @@ elf32_hppa_reloc_type_class (const Elf_I
         return reloc_class_normal;
     }
 
-  if (ELF32_R_SYM (rela->r_info) == 0)
+  if (ELF32_R_SYM (rela->r_info) == STN_UNDEF)
     return reloc_class_relative;
 
   switch ((int) ELF32_R_TYPE (rela->r_info))
Index: bfd/elf32-i370.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-i370.c,v
retrieving revision 1.65
diff -u -p -r1.65 elf32-i370.c
--- bfd/elf32-i370.c	27 Jun 2010 04:07:51 -0000	1.65
+++ bfd/elf32-i370.c	24 Sep 2010 09:28:11 -0000
@@ -1183,7 +1183,7 @@ i370_elf_relocate_section (bfd *output_b
 	case (int) R_I370_ADDR31:
 	case (int) R_I370_ADDR16:
 	  if (info->shared
-	      && r_symndx != 0)
+	      && r_symndx != STN_UNDEF)
 	    {
 	      Elf_Internal_Rela outrel;
 	      bfd_byte *loc;
Index: bfd/elf32-m32r.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-m32r.c,v
retrieving revision 1.98
diff -u -p -r1.98 elf32-m32r.c
--- bfd/elf32-m32r.c	25 Aug 2010 14:53:42 -0000	1.98
+++ bfd/elf32-m32r.c	24 Sep 2010 09:28:11 -0000
@@ -2897,7 +2897,7 @@ m32r_elf_relocate_section (bfd *output_b
             case R_M32R_HI16_ULO_RELA:
             case R_M32R_LO16_RELA:
               if (info->shared
-                  && r_symndx != 0
+                  && r_symndx != STN_UNDEF
                   && (input_section->flags & SEC_ALLOC) != 0
                   && ((   r_type != R_M32R_10_PCREL_RELA
                        && r_type != R_M32R_18_PCREL_RELA
Index: bfd/elf32-m68k.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-m68k.c,v
retrieving revision 1.122
diff -u -p -r1.122 elf32-m68k.c
--- bfd/elf32-m68k.c	25 Aug 2010 14:53:42 -0000	1.122
+++ bfd/elf32-m68k.c	24 Sep 2010 09:28:11 -0000
@@ -4034,7 +4034,7 @@ elf_m68k_relocate_section (output_bfd, i
 	case R_68K_PC16:
 	case R_68K_PC32:
 	  if (info->shared
-	      && r_symndx != 0
+	      && r_symndx != STN_UNDEF
 	      && (input_section->flags & SEC_ALLOC) != 0
 	      && (h == NULL
 		  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
@@ -4169,7 +4169,7 @@ elf_m68k_relocate_section (output_bfd, i
 	  return FALSE;
 	}
 
-      if (r_symndx != 0
+      if (r_symndx != STN_UNDEF
 	  && r_type != R_68K_NONE
 	  && (h == NULL
 	      || h->root.type == bfd_link_hash_defined
Index: bfd/elf32-microblaze.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-microblaze.c,v
retrieving revision 1.7
diff -u -p -r1.7 elf32-microblaze.c
--- bfd/elf32-microblaze.c	25 Aug 2010 14:53:42 -0000	1.7
+++ bfd/elf32-microblaze.c	24 Sep 2010 09:28:11 -0000
@@ -1067,10 +1067,10 @@ microblaze_elf_relocate_section (bfd *ou
 	    case (int) R_MICROBLAZE_64:
 	    case (int) R_MICROBLAZE_32:
 	      {
-		/* r_symndx will be zero only for relocs against symbols
+		/* r_symndx will be STN_UNDEF (zero) only for relocs against symbols
 		   from removed linkonce sections, or sections discarded by
 		   a linker script.  */
-		if (r_symndx == 0 || (input_section->flags & SEC_ALLOC) == 0)
+		if (r_symndx == STN_UNDEF || (input_section->flags & SEC_ALLOC) == 0)
 		  {
 		    relocation += addend;
 		    if (r_type == R_MICROBLAZE_32)
Index: bfd/elf32-ppc.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-ppc.c,v
retrieving revision 1.287
diff -u -p -r1.287 elf32-ppc.c
--- bfd/elf32-ppc.c	25 Aug 2010 14:53:43 -0000	1.287
+++ bfd/elf32-ppc.c	24 Sep 2010 09:28:11 -0000
@@ -6979,15 +6979,15 @@ ppc_elf_relocate_section (bfd *output_bf
 		  if (tls_gd == 0)
 		    {
 		      /* Was an LD reloc.  */
-		      for (r_symndx = 0;
+		      for (r_symndx = STN_UNDEF;
 			   r_symndx < symtab_hdr->sh_info;
 			   r_symndx++)
 			if (local_sections[r_symndx] == sec)
 			  break;
 		      if (r_symndx >= symtab_hdr->sh_info)
-			r_symndx = 0;
+			r_symndx = STN_UNDEF;
 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
-		      if (r_symndx != 0)
+		      if (r_symndx != STN_UNDEF)
 			rel->r_addend -= (local_syms[r_symndx].st_value
 					  + sec->output_offset
 					  + sec->output_section->vma);
@@ -7047,15 +7047,15 @@ ppc_elf_relocate_section (bfd *output_bf
 	    {
 	      unsigned int insn2;
 
-	      for (r_symndx = 0;
+	      for (r_symndx = STN_UNDEF;
 		   r_symndx < symtab_hdr->sh_info;
 		   r_symndx++)
 		if (local_sections[r_symndx] == sec)
 		  break;
 	      if (r_symndx >= symtab_hdr->sh_info)
-		r_symndx = 0;
+		r_symndx = STN_UNDEF;
 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
-	      if (r_symndx != 0)
+	      if (r_symndx != STN_UNDEF)
 		rel->r_addend -= (local_syms[r_symndx].st_value
 				  + sec->output_offset
 				  + sec->output_section->vma);
@@ -7658,7 +7658,7 @@ ppc_elf_relocate_section (bfd *output_bf
 			     sym_name);
 			  ret = FALSE;
 			}
-		      else if (r_symndx == 0 || bfd_is_abs_section (sec))
+		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
 			;
 		      else if (sec == NULL || sec->owner == NULL)
 			{
Index: bfd/elf32-score.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-score.c,v
retrieving revision 1.19
diff -u -p -r1.19 elf32-score.c
--- bfd/elf32-score.c	18 Aug 2010 12:24:06 -0000	1.19
+++ bfd/elf32-score.c	24 Sep 2010 09:28:11 -0000
@@ -2126,7 +2126,7 @@ score_elf_final_link_relocate (reloc_how
                && h != NULL
                && h->root.def_dynamic
                && !h->root.def_regular))
-           && r_symndx != 0
+           && r_symndx != STN_UNDEF
            && (input_section->flags & SEC_ALLOC) != 0)
         {
           /* If we're creating a shared library, or this relocation is against a symbol
@@ -2139,8 +2139,8 @@ score_elf_final_link_relocate (reloc_how
                                                     input_section))
             return bfd_reloc_undefined;
         }
-      else if (r_symndx == 0)
-        /* r_symndx will be zero only for relocs against symbols
+      else if (r_symndx == STN_UNDEF)
+        /* r_symndx will be STN_UNDEF (zero) only for relocs against symbols
            from removed linkonce sections, or sections discarded by
            a linker script.  */
         value = 0;
Index: bfd/elf32-score7.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-score7.c,v
retrieving revision 1.6
diff -u -p -r1.6 elf32-score7.c
--- bfd/elf32-score7.c	18 Aug 2010 12:24:06 -0000	1.6
+++ bfd/elf32-score7.c	24 Sep 2010 09:28:11 -0000
@@ -2025,7 +2025,7 @@ score_elf_final_link_relocate (reloc_how
                && h != NULL
                && h->root.def_dynamic
                && !h->root.def_regular))
-           && r_symndx != 0
+           && r_symndx != STN_UNDEF
            && (input_section->flags & SEC_ALLOC) != 0)
         {
           /* If we're creating a shared library, or this relocation is against a symbol
@@ -2038,8 +2038,8 @@ score_elf_final_link_relocate (reloc_how
                                                     input_section))
             return bfd_reloc_undefined;
         }
-      else if (r_symndx == 0)
-        /* r_symndx will be zero only for relocs against symbols
+      else if (r_symndx == STN_UNDEF)
+        /* r_symndx will be STN_UNDEF (zero) only for relocs against symbols
            from removed linkonce sections, or sections discarded by
            a linker script.  */
         value = 0;
Index: bfd/elf32-sh.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-sh.c,v
retrieving revision 1.168
diff -u -p -r1.168 elf32-sh.c
--- bfd/elf32-sh.c	25 Aug 2010 14:53:43 -0000	1.168
+++ bfd/elf32-sh.c	24 Sep 2010 09:28:11 -0000
@@ -4405,7 +4405,7 @@ sh_elf_relocate_section (bfd *output_bfd
 	      && (h == NULL
 		  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
 		  || h->root.type != bfd_link_hash_undefweak)
-	      && r_symndx != 0
+	      && r_symndx != STN_UNDEF
 	      && (input_section->flags & SEC_ALLOC) != 0
 	      && !is_vxworks_tls
 	      && (r_type == R_SH_DIR32
Index: bfd/elf32-vax.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-vax.c,v
retrieving revision 1.61
diff -u -p -r1.61 elf32-vax.c
--- bfd/elf32-vax.c	27 Jun 2010 04:07:53 -0000	1.61
+++ bfd/elf32-vax.c	24 Sep 2010 09:28:11 -0000
@@ -1611,7 +1611,7 @@ elf_vax_relocate_section (bfd *output_bf
 	case R_VAX_16:
 	case R_VAX_32:
 	  if (info->shared
-	      && r_symndx != 0
+	      && r_symndx != STN_UNDEF
 	      && (input_section->flags & SEC_ALLOC) != 0
 	      && ((r_type != R_VAX_PC8
 		   && r_type != R_VAX_PC16
Index: bfd/elf32-xtensa.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-xtensa.c,v
retrieving revision 1.125
diff -u -p -r1.125 elf32-xtensa.c
--- bfd/elf32-xtensa.c	25 Aug 2010 14:53:44 -0000	1.125
+++ bfd/elf32-xtensa.c	24 Sep 2010 09:28:11 -0000
@@ -2800,7 +2800,7 @@ elf_xtensa_relocate_section (bfd *output
 	    name = bfd_section_name (input_bfd, sec);
 	}
 
-      if (r_symndx != 0
+      if (r_symndx != STN_UNDEF
 	  && r_type != R_XTENSA_NONE
 	  && (h == NULL
 	      || h->root.type == bfd_link_hash_defined
Index: bfd/elf64-alpha.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-alpha.c,v
retrieving revision 1.171
diff -u -p -r1.171 elf64-alpha.c
--- bfd/elf64-alpha.c	20 Sep 2010 16:09:02 -0000	1.171
+++ bfd/elf64-alpha.c	24 Sep 2010 09:28:11 -0000
@@ -1866,8 +1866,8 @@ elf64_alpha_check_relocs (bfd *abfd, str
 
 	case R_ALPHA_TLSLDM:
 	  /* The symbol for a TLSLDM reloc is ignored.  Collapse the
-	     reloc to the 0 symbol so that they all match.  */
-	  r_symndx = 0;
+	     reloc to the STN_UNDEF (0) symbol so that they all match.  */
+	  r_symndx = STN_UNDEF;
 	  h = 0;
 	  maybe_dynamic = FALSE;
 	  /* FALLTHRU */
@@ -3491,7 +3491,7 @@ elf64_alpha_relax_tls_get_addr (struct a
      as appropriate.  */
 
   use_gottprel = FALSE;
-  new_symndx = is_gd ? ELF64_R_SYM (irel->r_info) : 0;
+  new_symndx = is_gd ? ELF64_R_SYM (irel->r_info) : STN_UNDEF;
 
   /* Beware of the compiler hoisting part of the sequence out a loop
      and adjusting the destination register for the TLSGD insn.  If this
@@ -3724,8 +3724,8 @@ elf64_alpha_relax_section (bfd *abfd, as
 
 	case R_ALPHA_TLSLDM:
 	  /* The symbol for a TLSLDM reloc is ignored.  Collapse the
-             reloc to the 0 symbol so that they all match.  */
-	  r_symndx = 0;
+             reloc to the STN_UNDEF (0) symbol so that they all match.  */
+	  r_symndx = STN_UNDEF;
 	  break;
 
 	default:
@@ -4154,9 +4154,9 @@ elf64_alpha_relocate_section (bfd *outpu
       r_symndx = ELF64_R_SYM(rel->r_info);
 
       /* The symbol for a TLSLDM reloc is ignored.  Collapse the
-	 reloc to the 0 symbol so that they all match.  */
+	 reloc to the STN_UNDEF (0) symbol so that they all match.  */
       if (r_type == R_ALPHA_TLSLDM)
-	r_symndx = 0;
+	r_symndx = STN_UNDEF;
 
       if (r_symndx < symtab_hdr->sh_info)
 	{
@@ -4166,10 +4166,10 @@ elf64_alpha_relocate_section (bfd *outpu
 	  msec = sec;
 	  value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
 
-	  /* If this is a tp-relative relocation against sym 0,
+	  /* If this is a tp-relative relocation against sym STN_UNDEF (0),
 	     this is hackery from relax_section.  Force the value to
 	     be the tls module base.  */
-	  if (r_symndx == 0
+	  if (r_symndx == STN_UNDEF
 	      && (r_type == R_ALPHA_TLSLDM
 		  || r_type == R_ALPHA_GOTTPREL
 		  || r_type == R_ALPHA_TPREL64
@@ -4455,7 +4455,7 @@ elf64_alpha_relocate_section (bfd *outpu
 		dynaddend = value - dtp_base;
 	      }
 	    else if (info->shared
-		     && r_symndx != 0
+		     && r_symndx != STN_UNDEF
 		     && (input_section->flags & SEC_ALLOC)
 		     && !undef_weak_ref)
 	      {
@@ -4503,7 +4503,7 @@ elf64_alpha_relocate_section (bfd *outpu
 	  /* ??? .eh_frame references to discarded sections will be smashed
 	     to relocations against SHN_UNDEF.  The .eh_frame format allows
 	     NULL to be encoded as 0 in any format, so this works here.  */
-	  if (r_symndx == 0)
+	  if (r_symndx == STN_UNDEF)
 	    howto = (elf64_alpha_howto_table
 		     + (r_type - R_ALPHA_SREL32 + R_ALPHA_REFLONG));
 	  goto default_reloc;
Index: bfd/elf64-hppa.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-hppa.c,v
retrieving revision 1.98
diff -u -p -r1.98 elf64-hppa.c
--- bfd/elf64-hppa.c	25 Aug 2010 14:53:44 -0000	1.98
+++ bfd/elf64-hppa.c	24 Sep 2010 09:28:11 -0000
@@ -2465,7 +2465,7 @@ elf64_hppa_finalize_dynreloc (struct elf
 static enum elf_reloc_type_class
 elf64_hppa_reloc_type_class (const Elf_Internal_Rela *rela)
 {
-  if (ELF64_R_SYM (rela->r_info) == 0)
+  if (ELF64_R_SYM (rela->r_info) == STN_UNDEF)
     return reloc_class_relative;
 
   switch ((int) ELF64_R_TYPE (rela->r_info))
Index: bfd/elf64-mips.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-mips.c,v
retrieving revision 1.96
diff -u -p -r1.96 elf64-mips.c
--- bfd/elf64-mips.c	25 Aug 2010 14:53:44 -0000	1.96
+++ bfd/elf64-mips.c	24 Sep 2010 09:28:12 -0000
@@ -2619,7 +2619,7 @@ mips_elf64_slurp_one_reloc_table (bfd *a
 	    default:
 	      if (! used_sym)
 		{
-		  if (rela.r_sym == 0)
+		  if (rela.r_sym == STN_UNDEF)
 		    relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
 		  else
 		    {
Index: bfd/elf64-ppc.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-ppc.c,v
retrieving revision 1.336
diff -u -p -r1.336 elf64-ppc.c
--- bfd/elf64-ppc.c	25 Aug 2010 14:53:44 -0000	1.336
+++ bfd/elf64-ppc.c	24 Sep 2010 09:28:12 -0000
@@ -11668,7 +11668,7 @@ ppc64_elf_relocate_section (bfd *output_
 
       /* Check that tls relocs are used with tls syms, and non-tls
 	 relocs are used with non-tls syms.  */
-      if (r_symndx != 0
+      if (r_symndx != STN_UNDEF
 	  && r_type != R_PPC64_NONE
 	  && (h == NULL
 	      || h->elf.root.type == bfd_link_hash_defined
@@ -11911,15 +11911,15 @@ ppc64_elf_relocate_section (bfd *output_
 		      /* Was an LD reloc.  */
 		      if (toc_symndx)
 			sec = local_sections[toc_symndx];
-		      for (r_symndx = 0;
+		      for (r_symndx = STN_UNDEF;
 			   r_symndx < symtab_hdr->sh_info;
 			   r_symndx++)
 			if (local_sections[r_symndx] == sec)
 			  break;
 		      if (r_symndx >= symtab_hdr->sh_info)
-			r_symndx = 0;
+			r_symndx = STN_UNDEF;
 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
-		      if (r_symndx != 0)
+		      if (r_symndx != STN_UNDEF)
 			rel->r_addend -= (local_syms[r_symndx].st_value
 					  + sec->output_offset
 					  + sec->output_section->vma);
@@ -12019,15 +12019,15 @@ ppc64_elf_relocate_section (bfd *output_
 
 	      if (toc_symndx)
 		sec = local_sections[toc_symndx];
-	      for (r_symndx = 0;
+	      for (r_symndx = STN_UNDEF;
 		   r_symndx < symtab_hdr->sh_info;
 		   r_symndx++)
 		if (local_sections[r_symndx] == sec)
 		  break;
 	      if (r_symndx >= symtab_hdr->sh_info)
-		r_symndx = 0;
+		r_symndx = STN_UNDEF;
 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
-	      if (r_symndx != 0)
+	      if (r_symndx != STN_UNDEF)
 		rel->r_addend -= (local_syms[r_symndx].st_value
 				  + sec->output_offset
 				  + sec->output_section->vma);
@@ -12569,7 +12569,7 @@ ppc64_elf_relocate_section (bfd *output_
 	case R_PPC64_TOC:
 	  /* Relocation value is TOC base.  */
 	  relocation = TOCstart;
-	  if (r_symndx == 0)
+	  if (r_symndx == STN_UNDEF)
 	    relocation += htab->stub_group[input_section->id].toc_off;
 	  else if (unresolved_reloc)
 	    ;
@@ -12819,7 +12819,7 @@ ppc64_elf_relocate_section (bfd *output_
 			     sym_name);
 			  ret = FALSE;
 			}
-		      else if (r_symndx == 0 || bfd_is_abs_section (sec))
+		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
 			;
 		      else if (sec == NULL || sec->owner == NULL)
 			{
Index: bfd/elf64-sparc.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-sparc.c,v
retrieving revision 1.121
diff -u -p -r1.121 elf64-sparc.c
--- bfd/elf64-sparc.c	19 Feb 2010 05:07:49 -0000	1.121
+++ bfd/elf64-sparc.c	24 Sep 2010 09:28:12 -0000
@@ -98,7 +98,7 @@ elf64_sparc_slurp_one_reloc_table (bfd *
       else
 	relent->address = rela.r_offset - asect->vma;
 
-      if (ELF64_R_SYM (rela.r_info) == 0)
+      if (ELF64_R_SYM (rela.r_info) == STN_UNDEF)
 	relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
       else
 	{
Index: bfd/elfcode.h
===================================================================
RCS file: /cvs/src/src/bfd/elfcode.h,v
retrieving revision 1.101
diff -u -p -r1.101 elfcode.h
--- bfd/elfcode.h	18 Aug 2010 12:24:07 -0000	1.101
+++ bfd/elfcode.h	24 Sep 2010 09:28:12 -0000
@@ -1466,7 +1466,7 @@ elf_slurp_reloc_table_from_section (bfd 
       else
 	relent->address = rela.r_offset - asect->vma;
 
-      if (ELF_R_SYM (rela.r_info) == 0)
+      if (ELF_R_SYM (rela.r_info) == STN_UNDEF)
 	relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
       else if (ELF_R_SYM (rela.r_info) > symcount)
 	{
Index: bfd/elflink.c
===================================================================
RCS file: /cvs/src/src/bfd/elflink.c,v
retrieving revision 1.379
diff -u -p -r1.379 elflink.c
--- bfd/elflink.c	16 Sep 2010 00:06:11 -0000	1.379
+++ bfd/elflink.c	24 Sep 2010 09:28:12 -0000
@@ -2170,7 +2170,7 @@ elf_link_read_relocs_from_section (bfd *
 	      return FALSE;
 	    }
 	}
-      else if (r_symndx != 0)
+      else if (r_symndx != STN_UNDEF)
 	{
 	  (*_bfd_error_handler)
 	    (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
@@ -9500,7 +9507,7 @@ elf_link_input_bfd (struct elf_final_lin
 		     discarded section.  */
 		  if ((sec = *ps) != NULL && elf_discarded_section (sec))
 		    {
-		      BFD_ASSERT (r_symndx != 0);
+		      BFD_ASSERT (r_symndx != STN_UNDEF);
 		      if (action_discarded & COMPLAIN)
 			(*finfo->info->callbacks->einfo)
 			  (_("%X`%s' referenced in section `%A' of %B: "
@@ -9667,7 +9674,7 @@ elf_link_input_bfd (struct elf_final_lin
 		      /* I suppose the backend ought to fill in the
 			 section of any STT_SECTION symbol against a
 			 processor specific section.  */
-		      r_symndx = 0;
+		      r_symndx = STN_UNDEF;
 		      if (bfd_is_abs_section (sec))
 			;
 		      else if (sec == NULL || sec->owner == NULL)
@@ -9696,7 +9703,7 @@ elf_link_input_bfd (struct elf_final_lin
 			  if (!bfd_is_abs_section (osec))
 			    {
 			      r_symndx = osec->target_index;
-			      if (r_symndx == 0)
+			      if (r_symndx == STN_UNDEF)
 				{
 				  struct elf_link_hash_table *htab;
 				  asection *oi;
@@ -9714,7 +9721,7 @@ elf_link_input_bfd (struct elf_final_lin
 				    }
 				}
 
-			      BFD_ASSERT (r_symndx != 0);
+			      BFD_ASSERT (r_symndx != STN_UNDEF);
 			    }
 			}
 
@@ -11471,7 +11478,7 @@ _bfd_elf_gc_mark_rsec (struct bfd_link_i
   struct elf_link_hash_entry *h;
 
   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
-  if (r_symndx == 0)
+  if (r_symndx == STN_UNDEF)
     return NULL;
 
   if (r_symndx >= cookie->locsymcount
Index: bfd/elfxx-ia64.c
===================================================================
RCS file: /cvs/src/src/bfd/elfxx-ia64.c,v
retrieving revision 1.225
diff -u -p -r1.225 elfxx-ia64.c
--- bfd/elfxx-ia64.c	25 Aug 2010 14:53:45 -0000	1.225
+++ bfd/elfxx-ia64.c	24 Sep 2010 09:28:12 -0000
@@ -4695,7 +4695,7 @@ elfNN_ia64_relocate_section (bfd *output
 	case R_IA64_DIR64LSB:
 	  /* Install a dynamic relocation for this reloc.  */
 	  if ((dynamic_symbol_p || info->shared)
-	      && r_symndx != 0
+	      && r_symndx != STN_UNDEF
 	      && (input_section->flags & SEC_ALLOC) != 0)
 	    {
 	      unsigned int dyn_r_type;
@@ -4924,7 +4924,7 @@ elfNN_ia64_relocate_section (bfd *output
 	case R_IA64_PCREL64MSB:
 	case R_IA64_PCREL64LSB:
 	  /* Install a dynamic relocation for this reloc.  */
-	  if (dynamic_symbol_p && r_symndx != 0)
+	  if (dynamic_symbol_p && r_symndx != STN_UNDEF)
 	    {
 	      BFD_ASSERT (srel != NULL);
 
Index: bfd/elfxx-mips.c
===================================================================
RCS file: /cvs/src/src/bfd/elfxx-mips.c,v
retrieving revision 1.274
diff -u -p -r1.274 elfxx-mips.c
--- bfd/elfxx-mips.c	19 Sep 2010 10:52:17 -0000	1.274
+++ bfd/elfxx-mips.c	24 Sep 2010 09:28:12 -0000
@@ -52,7 +52,7 @@
       (1) absolute addresses
 	    (abfd == NULL)
       (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
-	    (abfd != NULL, symndx >= 0)
+	    (abfd != NULL, symndx >= STN_UNDEF (0))
       (3) SYMBOL addresses, where SYMBOL is not local to an input bfd
 	    (abfd != NULL, symndx == -1)
 
@@ -104,7 +104,7 @@ struct mips_got_entry
      IE).  The GD and IE flags can be added as we encounter new
      relocations.  LDM can also be set; it will always be alone, not
      combined with any GD or IE flags.  An LDM GOT entry will be
-     a local symbol entry with r_symndx == 0.  */
+     a local symbol entry with r_symndx == STN_UNDEF.  */
   unsigned char tls_type;
 
   /* The offset from the beginning of the .got section to the entry
@@ -1446,7 +1446,7 @@ section_allows_mips16_refs_p (asection *
 
 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
    stub section of some kind.  Return the R_SYMNDX of the target
-   function, or 0 if we can't decide which function that is.  */
+   function, or STN_UNDEF if we can't decide which function that is.  */
 
 static unsigned long
 mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
@@ -1465,7 +1465,7 @@ mips16_stub_symndx (asection *sec ATTRIB
   if (relocs < relend)
     return ELF_R_SYM (sec->owner, relocs->r_info);
 
-  return 0;
+  return STN_UNDEF;
 }
 
 /* Check the mips16 stubs for a particular symbol, and see if we can
@@ -2624,7 +2624,7 @@ mips_elf_got_entry_hash (const void *ent
     + ((entry->tls_type & GOT_TLS_LDM) << 17)
     + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
        : entry->abfd->id
-         + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
+         + (entry->symndx >= STN_UNDEF ? mips_elf_hash_bfd_vma (entry->d.addend)
 	    : entry->d.h->root.root.root.hash));
 }
 
@@ -2640,7 +2640,7 @@ mips_elf_got_entry_eq (const void *entry
 
   return e1->abfd == e2->abfd && e1->symndx == e2->symndx
     && (! e1->abfd ? e1->d.address == e2->d.address
-	: e1->symndx >= 0 ? e1->d.addend == e2->d.addend
+	: e1->symndx >= STN_UNDEF ? e1->d.addend == e2->d.addend
 	: e1->d.h == e2->d.h);
 }
 
@@ -2657,7 +2657,7 @@ mips_elf_multi_got_entry_hash (const voi
   return entry->symndx
     + (! entry->abfd
        ? mips_elf_hash_bfd_vma (entry->d.address)
-       : entry->symndx >= 0
+       : entry->symndx >= STN_UNDEF
        ? ((entry->tls_type & GOT_TLS_LDM)
 	  ? (GOT_TLS_LDM << 17)
 	  : (entry->abfd->id
@@ -2680,7 +2680,8 @@ mips_elf_multi_got_entry_eq (const void 
     return 0;
 
   return e1->symndx == e2->symndx
-    && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
+    && (e1->symndx >= STN_UNDEF
+	? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
 	: e1->abfd == NULL || e2->abfd == NULL
 	? e1->abfd == e2->abfd && e1->d.address == e2->d.address
 	: e1->d.h == e2->d.h);
@@ -3299,7 +3300,7 @@ mips_elf_create_local_got_entry (bfd *ab
       if (r_type == R_MIPS_TLS_LDM)
 	{
 	  entry.tls_type = GOT_TLS_LDM;
-	  entry.symndx = 0;
+	  entry.symndx = STN_UNDEF;
 	  entry.d.addend = 0;
 	}
       else if (h == NULL)
@@ -4043,7 +4044,8 @@ mips_elf_make_got_per_bfd (void **entryp
       if (entry->tls_type & GOT_TLS_IE)
 	g->tls_gotno += 1;
     }
-  else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
+  else if (entry->symndx >= STN_UNDEF
+	   || entry->d.h->global_got_area == GGA_NONE)
     ++g->local_gotno;
   else
     ++g->global_gotno;
@@ -5282,7 +5284,7 @@ mips_elf_calculate_relocation (bfd *abfd
 	       && h->root.def_dynamic
 	       && !h->root.def_regular
 	       && !h->has_static_relocs))
-	  && r_symndx != 0
+	  && r_symndx != STN_UNDEF
 	  && (h == NULL
 	      || h->root.root.type != bfd_link_hash_undefweak
 	      || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
@@ -7265,7 +7267,7 @@ _bfd_mips_elf_check_relocs (bfd *abfd, s
          this is for.  */
 
       r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
-      if (r_symndx == 0)
+      if (r_symndx == STN_UNDEF)
 	{
 	  (*_bfd_error_handler)
 	    (_("%B: Warning: cannot determine the target function for"
@@ -7390,7 +7392,7 @@ _bfd_mips_elf_check_relocs (bfd *abfd, s
          this is for.  */
 
       r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
-      if (r_symndx == 0)
+      if (r_symndx == STN_UNDEF)
 	{
 	  (*_bfd_error_handler)
 	    (_("%B: Warning: cannot determine the target function for"
@@ -7752,7 +7754,7 @@ _bfd_mips_elf_check_relocs (bfd *abfd, s
 	case R_MIPS_TLS_LDM:
 	  if (r_type == R_MIPS_TLS_LDM)
 	    {
-	      r_symndx = 0;
+	      r_symndx = STN_UNDEF;
 	      h = NULL;
 	    }
 	  /* Fall through */
@@ -7778,7 +7780,7 @@ _bfd_mips_elf_check_relocs (bfd *abfd, s
 	      }
 	    else
 	      {
-		BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
+		BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != STN_UNDEF);
 
 		if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
 						       rel->r_addend,
@@ -7918,7 +7920,7 @@ _bfd_mips_elf_check_relocs (bfd *abfd, s
 	    case R_MIPS_HIGHEST:
 	      /* Don't refuse a high part relocation if it's against
 		 no symbol (e.g. part of a compound relocation).  */
-	      if (r_symndx == 0)
+	      if (r_symndx == STN_UNDEF)
 		break;
 
 	      /* R_MIPS_HI16 against _gp_disp is used for $gp setup,


Regards,
 Thomas

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* BFD, GAS, LD internal manuals (was: Relocations against STN_UNDEF)
  2010-09-24  9:17     ` Alan Modra
@ 2010-09-24 10:37       ` Thomas Schwinge
  0 siblings, 0 replies; 12+ messages in thread
From: Thomas Schwinge @ 2010-09-24 10:37 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 1374 bytes --]

Hello!

On 2010-09-24 09:16, Alan Modra wrote:
> On Fri, Sep 24, 2010 at 09:54:25AM +0200, Thomas Schwinge wrote:
>> On 2010-09-24 00:12, Alan Modra wrote:
>> > ARM fails because the arm backend specifically checks for undefined
>> > local symbols.  I'd say the arm backend check needs fixing (or
>> > removing) rather than changing the common elflink code.
>> 
>> Surely enough we could just change the ARM backend, but I'm not yet
>> really convinced that this is the most appropriate approach: if we agree
>> that BFD internally shall register STN_UNDEF with bfd_abs_section
>> instead of bfd_und_section (as demonstrated in other parts of the code
>> that I quoted),
>
> What is done elsewhere to make ELF relocs fit into the BFD framework
> isn't particularly relevant to the ELF linker.

>> >> I wonder why we need this code duplicated in (at least) two places; why
>> >> do objdump and ld use different code paths for reading in relocations?
>> >
>> > Efficiency.
>> 
>> Uhm, okay...
>
> To expand a little, [...]

Thanks!, that's exactly the information I needed.  It does make sense
now.

Oh, I *just now* found exactly this information in bfd/doc/bfdint.texi --
my bad that I missed it before.


Is it intentional that the BFD, GAS, LD internal manuals don't have
Makefile rules for easily building them?


Regards,
 Thomas

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ARM] Relocations against STN_UNDEF
  2010-09-24  0:12 ` Alan Modra
  2010-09-24  7:54   ` Thomas Schwinge
  2010-09-24  9:46   ` Relocations against STN_UNDEF Thomas Schwinge
@ 2010-09-24 11:26   ` Thomas Schwinge
  2010-09-24 14:53     ` Richard Henderson
  2010-10-08 10:33     ` Alan Modra
  2 siblings, 2 replies; 12+ messages in thread
From: Thomas Schwinge @ 2010-09-24 11:26 UTC (permalink / raw)
  To: binutils; +Cc: paul, dan

[-- Attachment #1: Type: text/plain, Size: 4158 bytes --]

Hello!

The ELF standard says that STN_UNDEF (symbol index zero, the undefined
symbol index) is to be marked as SHN_UNDEF, undefined symbol.  There is
one exception however: during relocations processing, a relocation
against STN_UNDEF shall be treated as a symbol value of zero.

For example on ARM, the bl instruction does a PC-relative jump, so to
jump to an absolute value of 0x10000, ``bl 0x10000'' has to keep its
relocation until the final linking is done, to be converted to the proper
PC-relative offset.

    $ cat < ~/sgxx/issue8612/bl_ABS.s
            bl 0x10000
    $ "$PWD"_install/bin/*-as -o bl_ABS.o ~/sgxx/issue8612/bl_ABS.s
    $ "$PWD"_install/bin/*-readelf -r bl_ABS.o 
    
    Relocation section '.rel.text' at offset 0x25c contains 1 entries:
     Offset     Info    Type            Sym.Value  Sym. Name
    00000000  0000001c R_ARM_CALL       

This info value translates to relocation type 28 (R_ARM_CALL) against
symbol index zero (STN_UNDEF).

On 2010-09-24 00:12, Alan Modra wrote:
> On Thu, Sep 23, 2010 at 05:37:01PM +0200, Thomas Schwinge wrote:
>>     $ "$PWD"_install/bin/*-ld -o bl_ABS bl_ABS.o
>>     /scratch/thomas/binutils/HEAD_build_arm-none-eabi_install/bin/arm-none-eabi-ld: warning: cannot find entry symbol _start; defaulting to 0000000000008000
>>     bl_ABS.o:(.text+0x0): undefined reference to `no symbol'
>
> ARM fails because the arm backend specifically checks for undefined
> local symbols.  I'd say the arm backend check needs fixing (or
> removing) rather than changing the common elflink code.

Here is a patch to fix this issue in the ARM backend.  There are no
regressions with a --target=arm-none-eabi config.

bfd/
2010-09-24  Thomas Schwinge  <thomas@codesourcery.com>

	* elf32-arm.c (elf32_arm_final_link_relocate)
	(elf32_arm_relocate_section): Handle relocations against STN_UNDEF.

Index: bfd/elf32-arm.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-arm.c,v
retrieving revision 1.241
diff -u -p -r1.241 elf32-arm.c
--- bfd/elf32-arm.c	26 Aug 2010 10:32:26 -0000	1.241
+++ bfd/elf32-arm.c	24 Sep 2010 11:16:45 -0000
@@ -7224,12 +7224,12 @@ elf32_arm_final_link_relocate (reloc_how
 
 	  /* A branch to an undefined weak symbol is turned into a jump to
 	     the next instruction unless a PLT entry will be created.
-	     Do the same for local undefined symbols.
+	     Do the same for local undefined symbols (but not for STN_UNDEF).
 	     The jump to the next instruction is optimized as a NOP depending
 	     on the architecture.  */
 	  if (h ? (h->root.type == bfd_link_hash_undefweak
 		   && !(splt != NULL && h->plt.offset != (bfd_vma) -1))
-	      : bfd_is_und_section (sym_sec))
+	      : r_symndx != STN_UNDEF && bfd_is_und_section (sym_sec))
 	    {
 	      value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000);
 
@@ -8903,9 +8903,11 @@ elf32_arm_relocate_section (bfd *       
 	     undefined symbol.  This is a daft object file, but we
 	     should at least do something about it.  V4BX & NONE
 	     relocations do not use the symbol and are explicitly
-	     allowed to use the undefined symbol, so allow those.  */
+	     allowed to use the undefined symbol, so allow those.
+	     Likewise for relocations against STN_UNDEF.  */
 	  if (r_type != R_ARM_V4BX
 	      && r_type != R_ARM_NONE
+	      && r_symndx != STN_UNDEF
 	      && bfd_is_und_section (sec)
 	      && ELF_ST_BIND (sym->st_info) != STB_WEAK)
 	    {

Success with patched BFD:

    $ "$PWD"_install/bin/*-ld -o bl_ABS bl_ABS.o
    /scratch/thomas/binutils/HEAD_build_arm-none-eabi_install/bin/arm-none-eabi-ld: warning: cannot find entry symbol _start; defaulting to 0000000000008000
    $ "$PWD"_install/bin/*-readelf -r bl_ABS
    
    There are no relocations in this file.
    $ "$PWD"_install/bin/*-objdump -dr bl_ABS
    
    bl_ABS:     file format elf32-littlearm
    
    
    Disassembly of section .text:
    
    00008000 <__data_start-0x8004>:
        8000:       eb001ffe        bl      10000 <__bss_end__-0x4>


Regards,
 Thomas

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Relocations against STN_UNDEF
  2010-09-24  9:46   ` Relocations against STN_UNDEF Thomas Schwinge
@ 2010-09-24 11:33     ` Alan Modra
  2010-09-24 12:16       ` Thomas Schwinge
  0 siblings, 1 reply; 12+ messages in thread
From: Alan Modra @ 2010-09-24 11:33 UTC (permalink / raw)
  To: Thomas Schwinge; +Cc: binutils

On Fri, Sep 24, 2010 at 11:45:42AM +0200, Thomas Schwinge wrote:
> 	* elflink.c (bfd_elf_reloc_symbol_deleted_p): Compare the symbol index
> 	to STN_UNDEF, not SHN_UNDEF.

OK.

The other changelog is probably best written as

	* elf32-arm.c, * elf32-cris.c, * elf32-hppa.c, * elf32-i370.c,
	* elf32-m32r.c, * elf32-m68k.c, * elf32-microblaze.c, * elf32-ppc.c,
	* elf32-score.c, * elf32-score7.c, * elf32-sh.c, * elf32-vax.c,
	* elf32-xtensa.c, * elf64-alpha.c, * elf64-hppa.c, * elf64-mips.c,
	* elf64-ppc.c, * elf64-sparc.c, * elfcode.h, * elflink.c,
	* elfxx-ia64.c, * elfxx-mips.c: Use STN_UNDEF when referring to the
	zero symbol index.


> -		      for (r_symndx = 0;
> +		      for (r_symndx = STN_UNDEF;
>  			   r_symndx < symtab_hdr->sh_info;
>  			   r_symndx++)

Please leave this, and the other 3 instances like it in elf32-ppc.c and
elf64-ppc.c as zeros.

I think it's also best to leave the struct mips_got_entry symndx
valuse as zeros here and in later comparisons, because the comparisons
probably ought to be "!= -1" rather than ">= 0".  Similaryly
mips16_stub_symndx is using 0 as an error return so I think that
should be left alone too.

So of the elfxx-mipx.c changes I'll OK the following.  The rest you'll
need to argue with a mips maintainer if you want to push your patch.  ;-)

> @@ -5282,7 +5284,7 @@ mips_elf_calculate_relocation (bfd *abfd
>  	       && h->root.def_dynamic
>  	       && !h->root.def_regular
>  	       && !h->has_static_relocs))
> -	  && r_symndx != 0
> +	  && r_symndx != STN_UNDEF
>  	  && (h == NULL
>  	      || h->root.root.type != bfd_link_hash_undefweak
>  	      || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)

> @@ -7752,7 +7754,7 @@ _bfd_mips_elf_check_relocs (bfd *abfd, s
>  	case R_MIPS_TLS_LDM:
>  	  if (r_type == R_MIPS_TLS_LDM)
>  	    {
> -	      r_symndx = 0;
> +	      r_symndx = STN_UNDEF;
>  	      h = NULL;
>  	    }
>  	  /* Fall through */
> @@ -7778,7 +7780,7 @@ _bfd_mips_elf_check_relocs (bfd *abfd, s
>  	      }
>  	    else
>  	      {
> -		BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
> +		BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != STN_UNDEF);
>  
>  		if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
>  						       rel->r_addend,
> @@ -7918,7 +7920,7 @@ _bfd_mips_elf_check_relocs (bfd *abfd, s
>  	    case R_MIPS_HIGHEST:
>  	      /* Don't refuse a high part relocation if it's against
>  		 no symbol (e.g. part of a compound relocation).  */
> -	      if (r_symndx == 0)
> +	      if (r_symndx == STN_UNDEF)
>  		break;
>  
>  	      /* R_MIPS_HI16 against _gp_disp is used for $gp setup,

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Relocations against STN_UNDEF
  2010-09-24 11:33     ` Alan Modra
@ 2010-09-24 12:16       ` Thomas Schwinge
  0 siblings, 0 replies; 12+ messages in thread
From: Thomas Schwinge @ 2010-09-24 12:16 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 1025 bytes --]

Hello!

On 2010-09-24 11:33, Alan Modra wrote:
> On Fri, Sep 24, 2010 at 11:45:42AM +0200, Thomas Schwinge wrote:
>> 	* elflink.c (bfd_elf_reloc_symbol_deleted_p): Compare the symbol index
>> 	to STN_UNDEF, not SHN_UNDEF.
>
> OK.

Committed.


>> -		      for (r_symndx = 0;
>> +		      for (r_symndx = STN_UNDEF;
>>  			   r_symndx < symtab_hdr->sh_info;
>>  			   r_symndx++)
>
> Please leave this, and the other 3 instances like it in elf32-ppc.c and
> elf64-ppc.c as zeros.

Yes, I admit: that looked a bit strange.  :-)

> I think it's also best to leave the struct mips_got_entry symndx
> valuse as zeros here and in later comparisons, because the comparisons
> probably ought to be "!= -1" rather than ">= 0".  Similaryly
> mips16_stub_symndx is using 0 as an error return so I think that
> should be left alone too.

Ack.

> So of the elfxx-mipx.c changes I'll OK the following.

Committed.

> The rest you'll
> need to argue with a mips maintainer if you want to push your patch.  ;-)

Nope.  :-)


Regards,
 Thomas

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [ARM] Relocations against STN_UNDEF
  2010-09-24 11:26   ` [ARM] " Thomas Schwinge
@ 2010-09-24 14:53     ` Richard Henderson
  2010-09-27  4:31       ` Thomas Schwinge
  2010-10-08 10:33     ` Alan Modra
  1 sibling, 1 reply; 12+ messages in thread
From: Richard Henderson @ 2010-09-24 14:53 UTC (permalink / raw)
  To: Thomas Schwinge; +Cc: binutils, paul, dan

On 09/24/2010 04:25 AM, Thomas Schwinge wrote:
> Hello!
> 
> The ELF standard says that STN_UNDEF (symbol index zero, the undefined
> symbol index) is to be marked as SHN_UNDEF, undefined symbol.  There is
> one exception however: during relocations processing, a relocation
> against STN_UNDEF shall be treated as a symbol value of zero.
> 
> For example on ARM, the bl instruction does a PC-relative jump, so to
> jump to an absolute value of 0x10000, ``bl 0x10000'' has to keep its
> relocation until the final linking is done, to be converted to the proper
> PC-relative offset.
> 
>     $ cat < ~/sgxx/issue8612/bl_ABS.s
>             bl 0x10000
>     $ "$PWD"_install/bin/*-as -o bl_ABS.o ~/sgxx/issue8612/bl_ABS.s
>     $ "$PWD"_install/bin/*-readelf -r bl_ABS.o 
>     
>     Relocation section '.rel.text' at offset 0x25c contains 1 entries:
>      Offset     Info    Type            Sym.Value  Sym. Name
>     00000000  0000001c R_ARM_CALL       

Err, why isn't this relocation against (the section symbol for) SHN_ABS
plus the 0x10000 offset?


r~

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [ARM] Relocations against STN_UNDEF
  2010-09-24 14:53     ` Richard Henderson
@ 2010-09-27  4:31       ` Thomas Schwinge
  0 siblings, 0 replies; 12+ messages in thread
From: Thomas Schwinge @ 2010-09-27  4:31 UTC (permalink / raw)
  To: Richard Henderson; +Cc: binutils, paul, dan

[-- Attachment #1: Type: text/plain, Size: 1206 bytes --]

Hello!

On 2010-09-24 14:52, Richard Henderson wrote:
> On 09/24/2010 04:25 AM, Thomas Schwinge wrote:
>>     $ cat < ~/sgxx/issue8612/bl_ABS.s
>>             bl 0x10000
>>     $ "$PWD"_install/bin/*-as -o bl_ABS.o ~/sgxx/issue8612/bl_ABS.s
>>     $ "$PWD"_install/bin/*-readelf -r bl_ABS.o 
>>     
>>     Relocation section '.rel.text' at offset 0x25c contains 1 entries:
>>      Offset     Info    Type            Sym.Value  Sym. Name
>>     00000000  0000001c R_ARM_CALL       
>
> Err, why isn't this relocation against (the section symbol for) SHN_ABS
> plus the 0x10000 offset?

Uhm, as I quoted the standard in my message:

>> The ELF standard says that STN_UNDEF (symbol index zero, the undefined
>> symbol index) is to be marked as SHN_UNDEF, undefined symbol.  There is
>> one exception however: during relocations processing, a relocation
>> against STN_UNDEF shall be treated as a symbol value of zero.

Doesn't this mandate that during relocations processing, a relocation
against STN_UNDEF is equivalent to a new symbol as proposed by you, that
is: for STN_UNDEF, SHN_UNDEF is turned into SHN_ABS (value zero) during
relocations processing?


Regards,
 Thomas

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [ARM] Relocations against STN_UNDEF
  2010-09-24 11:26   ` [ARM] " Thomas Schwinge
  2010-09-24 14:53     ` Richard Henderson
@ 2010-10-08 10:33     ` Alan Modra
  1 sibling, 0 replies; 12+ messages in thread
From: Alan Modra @ 2010-10-08 10:33 UTC (permalink / raw)
  To: Thomas Schwinge; +Cc: binutils, paul, dan

On Fri, Sep 24, 2010 at 01:25:09PM +0200, Thomas Schwinge wrote:
> 	* elf32-arm.c (elf32_arm_final_link_relocate)
> 	(elf32_arm_relocate_section): Handle relocations against STN_UNDEF.

OK.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-10-08 10:33 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-23 15:37 Relocations against STN_UNDEF Thomas Schwinge
2010-09-24  0:12 ` Alan Modra
2010-09-24  7:54   ` Thomas Schwinge
2010-09-24  9:17     ` Alan Modra
2010-09-24 10:37       ` BFD, GAS, LD internal manuals (was: Relocations against STN_UNDEF) Thomas Schwinge
2010-09-24  9:46   ` Relocations against STN_UNDEF Thomas Schwinge
2010-09-24 11:33     ` Alan Modra
2010-09-24 12:16       ` Thomas Schwinge
2010-09-24 11:26   ` [ARM] " Thomas Schwinge
2010-09-24 14:53     ` Richard Henderson
2010-09-27  4:31       ` Thomas Schwinge
2010-10-08 10:33     ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).