public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "H. J. Lu" <hjl@lucon.org>
To: binutils@sources.redhat.com
Subject: PATCH: Add _bfd_elf_provide_symbol
Date: Tue, 22 Mar 2005 17:37:00 -0000	[thread overview]
Message-ID: <20050322170758.GA1330@lucon.org> (raw)
In-Reply-To: <20050322050314.GE27445@bubble.modra.org>

On Tue, Mar 22, 2005 at 03:33:14PM +1030, Alan Modra wrote:
> > 
> > We had the same problem with __XXX_array_start/__XXX_array_end. I am
> > running into the same problem for a different issue. Can we can make
> > this a generic ELF function:
> > 
> > void bfd_elf_provide_symbol (struct bfd_link_info *,
> > 			     const char *sym_name,
> > 			     asection *sec,
> > 			     bfd_vma val);
> > 
> > It will define a hidden data symbol NAME defined in SEC with value VAL
> > if it is undefined.
> 
> If you need it, go ahead and steal it.  :)  The function probably ought
> to start with '_' to indicate that it's not part of the public bfd
> interface.
> 

Here is the patch. I call it from gld${EMULATION_NAME}_finish and
it works for me. Is this the best place to do it?


H.J.
----
bfd/

2005-03-22  H.J. Lu  <hongjiu.lu@intel.com>

	* bfd-in.h (_bfd_elf_provide_symbol): New.
	* bfd-in2.h: Regenerated.

	* elf32-ppc.c (set_linker_sym): Moved to elflink.c.
	(ppc_elf_set_sdata_syms): Call _bfd_elf_provide_symbol instead
	of set_linker_sym.

	* elflink.c (_bfd_elf_provide_symbol): New. Moved and renamed
	from elf32-ppc.c.

ld/

2005-03-22  H.J. Lu  <hongjiu.lu@intel.com>

	* emultempl/elf32.em (gld${EMULATION_NAME}_finish): Provide
	__preinit_array_start, __preinit_array_end, __init_array_start,
	__init_array_end, __fini_array_start and __fini_array_end.

	* scripttempl/elf.sc: Don't provide __preinit_array_start,
	__preinit_array_end, __init_array_start, __init_array_end,
	__fini_array_start nor __fini_array_end.

--- binutils/bfd/bfd-in.h.provide	2005-02-21 11:10:29.000000000 -0800
+++ binutils/bfd/bfd-in.h	2005-03-22 08:05:03.000000000 -0800
@@ -694,6 +694,9 @@ extern int bfd_get_sign_extend_vma
 extern struct bfd_section *_bfd_elf_tls_setup
   (bfd *, struct bfd_link_info *);
 
+extern void _bfd_elf_provide_symbol
+  (struct bfd_link_info *, const char *, bfd_vma);
+
 extern bfd_boolean bfd_m68k_elf32_create_embedded_relocs
   (bfd *, struct bfd_link_info *, struct bfd_section *, struct bfd_section *, char **);
 
--- binutils/bfd/elf32-ppc.c.provide	2005-03-22 07:14:43.000000000 -0800
+++ binutils/bfd/elf32-ppc.c	2005-03-22 07:35:13.000000000 -0800
@@ -4545,26 +4545,6 @@ ppc_elf_relax_section (bfd *abfd,
   return FALSE;
 }
 \f
-/* Set SYM_NAME to VAL if the symbol exists and is undefined.  */
-
-static void
-set_linker_sym (struct ppc_elf_link_hash_table *htab,
-		const char *sym_name,
-		bfd_vma val)
-{
-  struct elf_link_hash_entry *h;
-  h = elf_link_hash_lookup (&htab->elf, sym_name, FALSE, FALSE, FALSE);
-  if (h != NULL && h->root.type == bfd_link_hash_undefined)
-    {
-      h->root.type = bfd_link_hash_defined;
-      h->root.u.def.section = bfd_abs_section_ptr;
-      h->root.u.def.value = val;
-      h->def_regular = 1;
-      h->type = STT_OBJECT;
-      h->other = STV_HIDDEN;
-    }
-}
-
 /* Set _SDA_BASE_, _SDA2_BASE, and sbss start and end syms.  They are
    set here rather than via PROVIDE in the default linker script,
    because using PROVIDE inside an output section statement results in
@@ -4599,19 +4579,19 @@ ppc_elf_set_sdata_syms (bfd *obfd, struc
 	val = s->vma + 32768;
       lsect->sym_val = val;
 
-      set_linker_sym (htab, lsect->sym_name, val);
+      _bfd_elf_provide_symbol (info, lsect->sym_name, val);
     }
 
   s = bfd_get_section_by_name (obfd, ".sbss");
   val = 0;
   if (s != NULL)
     val = s->vma;
-  set_linker_sym (htab, "__sbss_start", val);
-  set_linker_sym (htab, "___sbss_start", val);
+  _bfd_elf_provide_symbol (info, "__sbss_start", val);
+  _bfd_elf_provide_symbol (info, "___sbss_start", val);
   if (s != NULL)
     val += s->size;
-  set_linker_sym (htab, "__sbss_end", val);
-  set_linker_sym (htab, "___sbss_end", val);
+  _bfd_elf_provide_symbol (info, "__sbss_end", val);
+  _bfd_elf_provide_symbol (info, "___sbss_end", val);
   return TRUE;
 }
 \f
--- binutils/bfd/elflink.c.provide	2005-03-22 07:14:45.000000000 -0800
+++ binutils/bfd/elflink.c	2005-03-22 07:34:13.000000000 -0800
@@ -9754,3 +9754,23 @@ _bfd_elf_section_already_linked (bfd *ab
   /* This is the first section with this name.  Record it.  */
   bfd_section_already_linked_table_insert (already_linked_list, sec);
 }
+
+/* Set NAME to VAL if the symbol exists and is undefined.  */
+
+void
+_bfd_elf_provide_symbol (struct bfd_link_info *info, const char *name,
+			 bfd_vma val)
+{
+  struct elf_link_hash_entry *h;
+  h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE,
+			    FALSE);
+  if (h != NULL && h->root.type == bfd_link_hash_undefined)
+    {
+      h->root.type = bfd_link_hash_defined;
+      h->root.u.def.section = bfd_abs_section_ptr;
+      h->root.u.def.value = val;
+      h->def_regular = 1;
+      h->type = STT_OBJECT;
+      h->other = STV_HIDDEN;
+    }
+}
--- binutils/ld/emultempl/elf32.em.provide	2005-03-22 07:15:14.000000000 -0800
+++ binutils/ld/emultempl/elf32.em	2005-03-22 09:03:36.783703101 -0800
@@ -1430,6 +1430,9 @@ cat >>e${EMULATION_NAME}.c <<EOF
 static void
 gld${EMULATION_NAME}_finish (void)
 {
+  asection *s;
+  bfd_vma start, end;
+
   if (bfd_elf_discard_info (output_bfd, &link_info))
     {
       lang_reset_memory_regions ();
@@ -1454,8 +1457,6 @@ gld${EMULATION_NAME}_finish (void)
 	   os != NULL;
 	   os = os->next)
 	{
-	  asection *s;
-
 	  if (os == abs_output_section || os->constraint == -1)
 	    continue;
 	  s = os->bfd_section;
@@ -1473,6 +1474,63 @@ gld${EMULATION_NAME}_finish (void)
 	    }
 	}
     }
+
+  /* Provide
+
+     __preinit_array_start
+     __preinit_array_end
+     __init_array_start
+     __init_array_end
+     __fini_array_start
+     __fini_array_end
+
+     They are set here rather than via PROVIDE in the linker script,
+     because using PROVIDE inside an output section statement results
+     in unnecessary output sections.  Using PROVIDE outside an output
+     section statement runs the risk of section alignment affecting
+     where the section starts.  */
+
+  s = bfd_get_section_by_name (output_bfd, ".preinit_array");
+  if (s != NULL)
+    {
+      start = s->vma;
+      end = start + s->size;
+    }
+  else
+    {
+      start = 0;
+      end = 0;
+    }
+  _bfd_elf_provide_symbol (&link_info, "__preinit_array_start", start);
+  _bfd_elf_provide_symbol (&link_info, "__preinit_array_end", end);
+
+  s = bfd_get_section_by_name (output_bfd, ".init_array");
+  if (s != NULL)
+    {
+      start = s->vma;
+      end = start + s->size;
+    }
+  else
+    {
+      start = 0;
+      end = 0;
+    }
+  _bfd_elf_provide_symbol (&link_info, "__init_array_start", start);
+  _bfd_elf_provide_symbol (&link_info, "__init_array_end", end);
+
+  s = bfd_get_section_by_name (output_bfd, ".fini_array");
+  if (s != NULL)
+    {
+      start = s->vma;
+      end = start + s->size;
+    }
+  else
+    {
+      start = 0;
+      end = 0;
+    }
+  _bfd_elf_provide_symbol (&link_info, "__fini_array_start", start);
+  _bfd_elf_provide_symbol (&link_info, "__fini_array_end", end);
 }
 EOF
 fi
--- binutils/ld/scripttempl/elf.sc.provide	2005-03-21 13:13:41.000000000 -0800
+++ binutils/ld/scripttempl/elf.sc	2005-03-22 07:36:29.000000000 -0800
@@ -333,22 +333,9 @@ cat <<EOF
   .tdata	${RELOCATING-0} : { *(.tdata${RELOCATING+ .tdata.* .gnu.linkonce.td.*}) }
   .tbss		${RELOCATING-0} : { *(.tbss${RELOCATING+ .tbss.* .gnu.linkonce.tb.*})${RELOCATING+ *(.tcommon)} }
 
-  /* Ensure the __preinit_array_start label is properly aligned.  We
-     could instead move the label definition inside the section, but
-     the linker would then create the section even if it turns out to
-     be empty, which isn't pretty.  */
-  ${RELOCATING+. = ALIGN(${ALIGNMENT});}
-  ${RELOCATING+${CREATE_SHLIB-PROVIDE (__preinit_array_start = .);}}
   .preinit_array   ${RELOCATING-0} : { KEEP (*(.preinit_array)) }
-  ${RELOCATING+${CREATE_SHLIB-PROVIDE (__preinit_array_end = .);}}
-
-  ${RELOCATING+${CREATE_SHLIB-PROVIDE (__init_array_start = .);}}
   .init_array   ${RELOCATING-0} : { KEEP (*(.init_array)) }
-  ${RELOCATING+${CREATE_SHLIB-PROVIDE (__init_array_end = .);}}
-
-  ${RELOCATING+${CREATE_SHLIB-PROVIDE (__fini_array_start = .);}}
   .fini_array   ${RELOCATING-0} : { KEEP (*(.fini_array)) }
-  ${RELOCATING+${CREATE_SHLIB-PROVIDE (__fini_array_end = .);}}
 
   ${SMALL_DATA_CTOR-${RELOCATING+${CTOR}}}
   ${SMALL_DATA_DTOR-${RELOCATING+${DTOR}}}

  reply	other threads:[~2005-03-22 17:08 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-21 15:42 Yet another unnecessary powerpc section Alan Modra
2005-03-22  8:16 ` Alan Modra
2005-03-22 11:48   ` H. J. Lu
2005-03-22 12:06     ` Alan Modra
2005-03-22 17:37       ` H. J. Lu [this message]
2005-03-23  4:19         ` PATCH: Add _bfd_elf_provide_symbol H. J. Lu
2005-03-23  5:37           ` H. J. Lu
2005-03-23  9:28             ` Alan Modra
2005-03-23  9:31               ` Daniel Jacobowitz
2005-03-23  9:33                 ` H. J. Lu
2005-04-25  4:53                   ` Michael Matz
2005-04-25 14:55                     ` H. J. Lu
2005-04-25 15:17                       ` Michael Matz
2005-04-25 17:31                         ` PATCH: Fix alpha relocation overflow (Re: PATCH: Add _bfd_elf_provide_symbol) H. J. Lu
2005-04-25 17:46                           ` H. J. Lu
2005-04-26 12:01                             ` Michael Matz
2005-04-26 13:47                               ` H. J. Lu
2005-04-27 19:35                                 ` H. J. Lu
2005-05-03  1:27                                   ` Alan Modra
2005-05-03  1:35                                     ` H. J. Lu
2005-05-03  5:31                                       ` Alan Modra
2005-05-03 21:40                                         ` H. J. Lu
2005-05-04  6:18                                           ` Alan Modra
2005-05-04  6:50                                             ` H. J. Lu
2005-05-04 10:17                                               ` Alan Modra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050322170758.GA1330@lucon.org \
    --to=hjl@lucon.org \
    --cc=binutils@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).