This is the mail archive of the binutils@sourceware.cygnus.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: GNU ld and -init/-fini


>>>>> "Ian" == Ian Lance Taylor <ian@zembu.com> writes:

    Ian> I think it would be appropriate to add -init and -fini
    Ian> options to work for any ELF target, and permit them to
    Ian> override the default of _init and _fini.

I agree.  Attached is a patch to do just that.  OK to check in?

As an aside, using this patch, I have managed to use the (unsubmitted)
IRIX6 N32 port of GNU ld to fully bootstrap GCC, and there are no
immediately apparent regressions in the testsuites (where GNU ld is
used to link the various tests, naturally.)  I'm still testing, and
there's some code cleanup to do, and a few corner cases to handle, but
I'm nearly ready to make this code available.  Then, I'll just have to
sneak it past your eagle eyes. :-)

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com

1999-06-22  Mark Mitchell  <mark@codesourcery.com>

	* bfdlink.h (struct bfd_link_hash_entry): Add init_function and
	fini_function.

1999-06-22  Mark Mitchell  <mark@codesourcery.com>

	* elflink.h (size_dynamic_sections): Use user-specified init/fini
	functions instead of _init/_fini if requested.

1999-06-22  Mark Mitchell  <mark@codesourcery.com>

	* ldmain.c (main): Initialize link_info.init_function and
	link_info.fini_function.
	* lexsup.c (OPTION_INIT): New macro.
	(OPTION_FINI): Likewise.
	(ld_options): Add descriptions for them.
	(parse_args): Handle them.

Index: include/bfdlink.h
===================================================================
RCS file: /cvs/binutils/binutils/include/bfdlink.h,v
retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 bfdlink.h
--- bfdlink.h	1999/05/03 07:29:01	1.1.1.1
+++ bfdlink.h	1999/06/23 00:16:33
@@ -237,6 +237,13 @@ struct bfd_link_info
   MPC860 C0 (or earlier) should be checked for and modified.  It gives the
   number of bytes that should be checked at the end of each text page. */
   int mpc860c0;
+
+  /* The function to call when the executable or shared object is
+     loaded.  */
+  const char *init_function;
+  /* The function to call when the executable or shared object is
+     unloaded.  */
+  const char *fini_function;
 };
 
 /* This structures holds a set of callback functions.  These are
Index: bfd/elflink.h
===================================================================
RCS file: /cvs/binutils/binutils/bfd/elflink.h,v
retrieving revision 1.6
diff -u -p -r1.6 elflink.h
--- elflink.h	1999/06/22 13:57:15	1.6
+++ elflink.h	1999/06/23 00:16:37
@@ -2589,8 +2589,14 @@ NAME(bfd_elf,size_dynamic_sections) (out
       /* Add some entries to the .dynamic section.  We fill in some of the
 	 values later, in elf_bfd_final_link, but we must add the entries
 	 now so that we know the final size of the .dynamic section.  */
-      h =  elf_link_hash_lookup (elf_hash_table (info), "_init", false,
-				false, false);
+
+      /* If there are initialization and/or finalization functions to
+	 call then add the corresponding DT_INIT/DT_FINI entries.  */
+      h = (info->init_function
+	   ? elf_link_hash_lookup (elf_hash_table (info), 
+				   info->init_function, false,
+				   false, false)
+	   : NULL);
       if (h != NULL
 	  && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
 					| ELF_LINK_HASH_DEF_REGULAR)) != 0)
@@ -2598,8 +2604,11 @@ NAME(bfd_elf,size_dynamic_sections) (out
 	  if (! elf_add_dynamic_entry (info, DT_INIT, 0))
 	    return false;
 	}
-      h =  elf_link_hash_lookup (elf_hash_table (info), "_fini", false,
-				 false, false);
+      h = (info->init_function
+	   ? elf_link_hash_lookup (elf_hash_table (info), 
+				   info->fini_function, false,
+				   false, false)
+	   : NULL);
       if (h != NULL
 	  && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
 					| ELF_LINK_HASH_DEF_REGULAR)) != 0)
@@ -2607,6 +2616,7 @@ NAME(bfd_elf,size_dynamic_sections) (out
 	  if (! elf_add_dynamic_entry (info, DT_FINI, 0))
 	    return false;
 	}
+
       strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
       if (! elf_add_dynamic_entry (info, DT_HASH, 0)
 	  || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
@@ -4159,15 +4169,11 @@ elf_bfd_final_link (abfd, info)
 	    {
 	    default:
 	      break;
-
-	      /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
-                 magic _init and _fini symbols.  This is pretty ugly,
-                 but we are compatible.  */
 	    case DT_INIT:
-	      name = "_init";
+	      name = info->init_function;
 	      goto get_sym;
 	    case DT_FINI:
-	      name = "_fini";
+	      name = info->fini_function;
 	    get_sym:
 	      {
 		struct elf_link_hash_entry *h;
Index: ld/ld.texinfo
===================================================================
RCS file: /cvs/binutils/binutils/ld/ld.texinfo,v
retrieving revision 1.3
diff -u -p -r1.3 ld.texinfo
--- ld.texinfo	1999/06/14 01:40:24	1.3
+++ ld.texinfo	1999/06/23 00:16:45
@@ -404,6 +404,14 @@ purpose: the @code{-b}, @code{--format},
 environment variable.  The @sc{gnu} linker will ignore the @code{-F}
 option when not creating an ELF shared object.
 
+@cindex finalization function
+@kindex -fini
+@item -fini @var{name}
+When creating an ELF executable or shared object, call NAME when the
+executable or shared object is unloaded, by setting DT_FINI to the
+address of the function.  By default, the linker uses @code{_fini} as
+the function to call.
+
 @kindex -g
 @item -g
 Ignored.  Provided for compatibility with other tools.
@@ -433,6 +441,14 @@ field rather than the using the file nam
 @cindex incremental link
 @item -i
 Perform an incremental link (same as option @samp{-r}).
+
+@cindex initialization function
+@kindex -init
+@item -init @var{name}
+When creating an ELF executable or shared object, call NAME when the
+executable or shared object is loaded, by setting DT_INIT to the address
+of the function.  By default, the linker uses @code{_init} as the
+function to call.
 
 @cindex archive files, from cmd line
 @kindex -l@var{archive}
Index: ld/ldmain.c
===================================================================
RCS file: /cvs/binutils/binutils/ld/ldmain.c,v
retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 ldmain.c
--- ldmain.c	1999/05/03 07:29:07	1.1.1.1
+++ ldmain.c	1999/06/23 00:16:46
@@ -231,7 +231,11 @@ main (argc, argv)
   link_info.notice_hash = NULL;
   link_info.wrap_hash = NULL;
   link_info.mpc860c0 = 0;
-  
+  /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
+     and _fini symbols.  We are compatible.  */
+  link_info.init_function = "_init";
+  link_info.fini_function = "_fini";
+
   ldfile_add_arch ("");
 
   config.make_executable = true;
Index: ld/lexsup.c
===================================================================
RCS file: /cvs/binutils/binutils/ld/lexsup.c,v
retrieving revision 1.3
diff -u -p -r1.3 lexsup.c
--- lexsup.c	1999/06/09 05:35:55	1.3
+++ lexsup.c	1999/06/23 00:16:47
@@ -120,6 +120,8 @@ int parsing_defsym = 0;
 #define OPTION_NO_CHECK_SECTIONS	(OPTION_CHECK_SECTIONS + 1)
 #define OPTION_MPC860C0                 (OPTION_NO_CHECK_SECTIONS + 1)
 #define OPTION_NO_UNDEFINED		(OPTION_MPC860C0 + 1)
+#define OPTION_INIT                     (OPTION_NO_UNDEFINED + 1)
+#define OPTION_FINI                     (OPTION_INIT + 1)
 
 /* The long options.  This structure is used for both the option
    parsing and the help text.  */
@@ -360,7 +362,11 @@ static const struct ld_option ld_options
   { {"wrap", required_argument, NULL, OPTION_WRAP},
       '\0', N_("SYMBOL"), N_("Use wrapper functions for SYMBOL"), TWO_DASHES },
   { {"mpc860c0", optional_argument, NULL, OPTION_MPC860C0},
-      '\0', N_("[=WORDS]"), N_("Modify problematic branches in last WORDS (1-10,\n\t\t\t\tdefault 5) words of a page"), TWO_DASHES }
+      '\0', N_("[=WORDS]"), N_("Modify problematic branches in last WORDS (1-10,\n\t\t\t\tdefault 5) words of a page"), TWO_DASHES },
+  { {"init", required_argument, NULL, OPTION_INIT},
+     '\0', N_("SYMBOL"), N_("Call SYMBOL at load-time"), ONE_DASH },
+  { {"fini", required_argument, NULL, OPTION_FINI},
+     '\0', N_("SYMBOL"), N_("Call SYMBOL at unload-time"), ONE_DASH },
 };
 
 #define OPTION_COUNT ((int) (sizeof ld_options / sizeof ld_options[0]))
@@ -988,6 +994,14 @@ the GNU General Public License.  This pr
             }
           command_line.relax = true;
           break;
+
+	case OPTION_INIT:
+	  link_info.init_function = optarg;
+	  break;
+	  
+	case OPTION_FINI:
+	  link_info.fini_function = optarg;
+	  break;
 	}
     }
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]