Subject: another ld and X patches
To: None <current-users@sun-lamp.cs.berkeley.edu>
From: Hung-Chi Chu <f80204@cc.ee.ntu.edu.tw>
List: current-users
Date: 11/12/1993 17:03:11
Hi,
  I have modified ld to automatically link the static part lib*.sa.* of
shared libs. With this patch, it is NOT necessary to modify the source
code of libX*. I adopt the config files for Sun with few modification.
Also, the new ld support LD_OPTIONS env variable like Sun ld so that
you can set static linking via 'setenv LD_OPTIONS -Bstatic'. Wish you
like it. Any opinions ?

--- BEGIN OF LD PATCHES : cd /usr/src/gnu/usr.bin; patch -p -s < thispatch

*** ld/ld.c.orig	Thu Nov 11 18:36:15 1993
--- ld/ld.c	Fri Nov 12 06:30:15 1993
***************
*** 120,125 ****
--- 120,131 ----
   */
  int	specified_data_size;
  
+ /*
+  * token list of argv[0] + environment variable LD_OPTIONS
+  */
+ char	**ld_env_options;
+ int	ld_env_opt_len;
+ 
  long	*set_vectors;
  int	setv_fill_count;
  
***************
*** 195,200 ****
--- 201,216 ----
  					     * sizeof(struct glosym *));
  	*cmdline_references = 0;
  
+ 	/* get LD_OPTIONS environment */
+ 	get_ld_env_options(*argv);
+ 
+ 	/* LD_OPTIONS precede ARGV */
+ 	ld_env_options = (char **) xrealloc(ld_env_options, (ld_env_opt_len + argc)
+ 					    * sizeof(char *));
+ 	bcopy(argv + 1, ld_env_options + ld_env_opt_len, argc * sizeof(char *));
+ 	argc = ld_env_opt_len + argc - 1;
+ 	argv = ld_env_options;
+  
  	/* Completely decode ARGV.  */
  	decode_command(argc, argv);
  
***************
*** 308,313 ****
--- 324,361 ----
  }
  
  /*
+  * Get LD_OPTIONS, setting up token list and size.
+  */
+ 
+ void
+ get_ld_env_options(argv0)
+ 	char *argv0;
+ {
+ 	register char *ld_opt, *tmp, *p = argv0, **pp;
+ 	register int max_len = 1;
+ 
+ 	ld_env_options = (char **) xmalloc(1);
+ 	*ld_env_options = argv0;
+ 	ld_env_opt_len = 1;
+ 	if ((tmp = getenv("LD_OPTIONS")) == NULL)
+ 		return;
+ 	ld_opt = (char *) xmalloc(strlen(tmp) + 1);
+ 	strcpy(ld_opt, tmp);
+ 
+ 	while(p = strtok(ld_opt, " \t")) {
+ 		if (max_len <= ld_env_opt_len) {
+ 			max_len += 10;
+ 			ld_env_options = (char **) xrealloc(ld_env_options,
+ 							    max_len * sizeof(char *));
+ 			pp = ld_env_options + ld_env_opt_len;
+ 		}
+ 		*pp++ = p;
+ 		ld_env_opt_len++;
+ 		ld_opt = NULL;
+ 	}
+ }
+ 
+ /*
   * Process the command arguments, setting up file_table with an entry for
   * each input file, and setting variables according to the options.
   */
***************
*** 338,345 ****
  
  			decode_option(argv[i], argv[i + 1]);
  
! 			if (argv[i][1] == 'l' || argv[i][1] == 'A')
  				number_of_files++;
  
  			i += code - 1;
  		} else
--- 386,395 ----
  
  			decode_option(argv[i], argv[i + 1]);
  
! 			if (argv[i][1] == 'A')
  				number_of_files++;
+ 			if (argv[i][1] == 'l')  /* assume .sa. library exists */
+ 				number_of_files += 2;
  
  			i += code - 1;
  		} else
***************
*** 353,358 ****
--- 403,411 ----
  			xmalloc(number_of_files * sizeof(struct file_entry));
  	bzero(p, number_of_files * sizeof(struct file_entry));
  
+ 	/* Append the standard search directories to the user-specified ones. */
+ 	std_search_dirs(getenv("LD_LIBRARY_PATH"));
+ 
  	/* Now scan again and fill in file_table.  */
  	/* All options except -A and -l are ignored here.  */
  
***************
*** 397,402 ****
--- 450,462 ----
  			p->search_dirs_flag = 1;
  			if (link_mode & DYNAMIC && !relocatable_output)
  				p->search_dynamic_flag = 1;
+ 			switch(findlib(p)) {
+ 			case 1:	/* no .sa. library */
+ 				number_of_files--;
+ 				break;
+ 			case 2:	/* DYNAMIC and .sa. library exists */
+ 				p++;
+ 			}
  			p++;
  		}
  		i += code - 1;
***************
*** 407,415 ****
  	if ((magic != OMAGIC)
  	    && (text_start - text_start_alignment) & (page_size - 1))
  		fatal("-T argument not multiple of page size, with sharable output");
- 
- 	/* Append the standard search directories to the user-specified ones. */
- 	std_search_dirs(getenv("LD_LIBRARY_PATH"));
  }
  
  void
--- 467,472 ----
***************
*** 736,743 ****
  
  	if (input_file) file_close ();
  
! 	if (entry->search_dirs_flag) {
! 		desc = findlib(entry);
  	} else
  		desc = open (entry->filename, O_RDONLY, 0);
  
--- 793,800 ----
  
  	if (input_file) file_close ();
  
! 	if (entry->filename == NULL) {
! 		desc = -1;
  	} else
  		desc = open (entry->filename, O_RDONLY, 0);
  
*** ld/ld.h.orig	Thu Nov 11 18:36:15 1993
--- ld/ld.h	Fri Nov 12 06:37:41 1993
***************
*** 814,819 ****
--- 814,820 ----
  
  void	digest_symbols __P((void));
  void	load_symbols __P((void));
+ void	get_ld_env_options __P((char *));
  void	decode_command __P((int, char **));
  void	read_header __P((int, struct file_entry *));
  void	read_entry_symbols __P((int, struct file_entry *));
***************
*** 857,863 ****
  int	findlib __P((struct file_entry *));
  
  /* In shlib.c: */
! char	*findshlib __P((char *, int *, int *));
  void	add_search_dir __P((char *));
  void	std_search_dirs __P((char *));
  
--- 858,864 ----
  int	findlib __P((struct file_entry *));
  
  /* In shlib.c: */
! char	*findshlib __P((char *, int *, int *, char **));
  void	add_search_dir __P((char *));
  void	std_search_dirs __P((char *));
  
*** ld/lib.c.orig	Thu Nov 11 18:36:16 1993
--- ld/lib.c	Fri Nov 12 06:42:14 1993
***************
*** 609,624 ****
  	if (p->search_dynamic_flag == 0)
  		goto dot_a;
  
! 	fname = findshlib(p->filename, &major, &minor);
  
! 	if (fname && (desc = open (fname, O_RDONLY, 0)) > 0) {
  		p->filename = fname;
  		p->lib_major = major;
  		p->lib_minor = minor;
  		p->search_dirs_flag = 0;
! 		return desc;
  	}
- 	free (fname);
  
  dot_a:
  	p->search_dynamic_flag = 0;
--- 609,629 ----
  	if (p->search_dynamic_flag == 0)
  		goto dot_a;
  
! 	fname = findshlib(p->filename, &major, &minor, &((p+1)->filename));
  
! 	if (fname) {
  		p->filename = fname;
  		p->lib_major = major;
  		p->lib_minor = minor;
  		p->search_dirs_flag = 0;
! 		p++;
! 		if ((fname = p->filename) != NULL) {
! 		/* .sa. library exists */
! 			p->local_sym_name = fname;
! 			return (2);
! 		}
! 		return (1);
  	}
  
  dot_a:
  	p->search_dynamic_flag = 0;
***************
*** 636,645 ****
  		if (desc > 0) {
  			p->filename = string;
  			p->search_dirs_flag = 0;
! 			break;
  		}
  		free (string);
  	}
! 	return desc;
  }
  
--- 641,653 ----
  		if (desc > 0) {
  			p->filename = string;
  			p->search_dirs_flag = 0;
! 			close(desc);
! 			return (1);
  		}
  		free (string);
  	}
! 	perror_file(p);
! 	p->filename = NULL;
! 	return 0;
  }
  
*** ld/shlib.c.orig	Tue Nov  9 18:40:13 1993
--- ld/shlib.c	Fri Nov 12 06:48:49 1993
***************
*** 13,18 ****
--- 13,19 ----
  #include <string.h>
  #include <dirent.h>
  #include <a.out.h>
+ #include <errno.h>
  
  #include "ld.h"
  
***************
*** 132,139 ****
  #undef minor
  
  char *
! findshlib(name, majorp, minorp)
! char	*name;
  int	*majorp, *minorp;
  {
  	int		dewey[MAXDEWEY];
--- 133,140 ----
  #undef minor
  
  char *
! findshlib(name, majorp, minorp, p_saname)
! char	*name, **p_saname;	/* p_sname: pointer to .sa. library name */
  int	*majorp, *minorp;
  {
  	int		dewey[MAXDEWEY];
***************
*** 141,147 ****
  	int		tmp[MAXDEWEY];
  	int		i;
  	int		len;
! 	char		*lname, *path = NULL;
  	int		major = *majorp, minor = *minorp;
  
  	len = strlen(name);
--- 142,148 ----
  	int		tmp[MAXDEWEY];
  	int		i;
  	int		len;
! 	char		*lname, *path = NULL, *saname;
  	int		major = *majorp, minor = *minorp;
  
  	len = strlen(name);
***************
*** 200,204 ****
--- 201,215 ----
  		closedir(dd);
  	}
  
+ 	*p_saname = NULL;
+ 	if (path) {
+ 		*p_saname = saname = (char *) xmalloc (strlen(path) + 1);
+ 		strcpy(saname, path);
+ 		strncpy(strstr(saname, ".so."), ".sa.", 4);
+ 		if ((access(saname, R_OK) < 0) && (errno == ENOENT)) {
+ 			free(saname);
+ 			*p_saname = NULL;
+ 		}
+ 	}
  	return path;
  }
--- END OF LD PATCHES ---

--- BEGIN OF X PATCHES ---
*** mit/config/x386.cf.orig	Wed Nov  3 22:43:32 1993
--- mit/config/x386.cf	Thu Nov  4 00:22:49 1993
***************
*** 1040,1051 ****
          $(RM) $$i.0; \                                                  @@\
  	$(LN) file.0 $$i.0); \                                          @@\
  	done
! #endif
  
  #define InstallGenManPageAliases(file,destdir,suffix,aliases)		@@\
  	InstallManPageAliases(file,destdir,aliases)
  
! #endif /* FormattedManPages */
  
  
  #if defined(i386MachArchitecture)
--- 1040,1055 ----
          $(RM) $$i.0; \                                                  @@\
  	$(LN) file.0 $$i.0); \                                          @@\
  	done
! #endif /* FormattedManPages */
  
  #define InstallGenManPageAliases(file,destdir,suffix,aliases)		@@\
  	InstallManPageAliases(file,destdir,aliases)
  
! #ifdef i386NetBsd
! #include <netbsdLib.rules>
! #endif
! 
! #endif
  
  
  #if defined(i386MachArchitecture)
*** mit/config/netbsdLib.rules.orig	Thu Nov  4 00:29:12 1993
--- mit/config/netbsdLib.rules	Fri Nov 12 07:03:46 1993
***************
*** 0 ****
--- 1,85 ----
+ /*
+  * NetBSD shared library rules, added by H.C.Chu 93/11/12
+  */
+ 
+ #ifndef HasSharedLibraries
+ #define HasSharedLibraries YES
+ #endif
+ #ifndef SharedDataSeparation
+ #define SharedDataSeparation YES
+ #endif
+ #ifndef SharedCodeDef
+ #define SharedCodeDef -DSHAREDCODE
+ #endif
+ #ifndef SharedLibraryDef
+ #define SharedLibraryDef -DSUNSHLIB
+ #endif
+ #ifndef ShLibIncludeFile
+ #define ShLibIncludeFile <netbsdLib.tmpl>
+ #endif
+ #ifndef SharedLibraryLoadFlags
+ #define SharedLibraryLoadFlags -Bshareable
+ #endif
+ #ifndef PositionIndependentCFlags
+ #define PositionIndependentCFlags -fpic
+ #endif
+ 
+ /*
+  * InstallSharedLibrary - generate rules to install the shared library.
+  */
+ #ifndef InstallSharedLibrary
+ #define	InstallSharedLibrary(libname,rev,dest)				@@\
+ install:: Concat(lib,libname.so.rev) 					@@\
+ 	MakeDir($(DESTDIR)dest)						@@\
+ 	$(INSTALL) -c $(INSTLIBFLAGS) Concat(lib,libname.so.rev) $(DESTDIR)dest @@\
+ 
+ #endif /* InstallSharedLibrary */
+ 
+ /*
+  * InstallSharedLibraryData - generate rules to install the shared library data
+  */
+ #ifndef InstallSharedLibraryData
+ #define	InstallSharedLibraryData(libname,rev,dest)			@@\
+ install:: Concat(lib,libname.sa.rev)					@@\
+ 	MakeDir($(DESTDIR)dest)						@@\
+ 	$(INSTALL) -c $(INSTLIBFLAGS) Concat(lib,libname.sa.rev) $(DESTDIR)dest @@\
+ 	RanLibrary($(RANLIBINSTFLAGS) Concat($(DESTDIR)dest/lib,libname.sa.rev))
+ 
+ #endif /* InstallSharedLibraryData */
+ 
+ /*
+  * NormalSharedLibraryTarget - generate rules to create a shared library;
+  * build it into a different name so that the we do not hose people by having
+  * the library gone for long periods.
+  */
+ #ifndef SharedLibraryTarget
+ #define SharedLibraryTarget(libname,rev,solist,down,up)			@@\
+ AllTarget(Concat(lib,libname.so.rev))					@@\
+ 									@@\
+ Concat(lib,libname.so.rev):  solist					@@\
+ 	$(RM) $@~							@@\
+ 	(cd down; $(LD) -o up/$@~ $(SHLIBLDFLAGS) solist $(REQUIREDLIBS)) @@\
+ 	$(RM) $@ 							@@\
+ 	$(MV) $@~ $@							@@\
+ 									@@\
+ clean::									@@\
+ 	$(RM) Concat(lib,libname.so.rev)
+ 
+ #endif /* SharedLibraryTarget */
+ 
+ /*
+  * SharedLibraryDataTarget - generate rules to create shlib data file;
+  */
+ #ifndef SharedLibraryDataTarget
+ #define SharedLibraryDataTarget(libname,rev,salist)			@@\
+ AllTarget(Concat(lib,libname.sa.rev))					@@\
+ 									@@\
+ Concat(lib,libname.sa.rev):  salist					@@\
+ 	$(RM) $@							@@\
+ 	$(AR) $@ salist							@@\
+ 	RanLibrary($@)							@@\
+ 									@@\
+ clean::									@@\
+ 	$(RM) Concat(lib,libname.sa.rev)
+ 
+ #endif /* SharedLibraryDataTarget */
*** mit/config/netbsdLib.tmpl.orig	Thu Nov  4 00:29:12 1993
--- mit/config/netbsdLib.tmpl	Fri Nov 12 07:05:02 1993
***************
*** 0 ****
--- 1,75 ----
+ /*
+  * NetBSD shared library template, added by H.C.Chu 93/11/12
+  */
+ 
+ #ifndef SharedXlibRev
+ #define SharedXlibRev 0.0
+ #endif
+ #ifndef SharedOldXRev
+ #define SharedOldXRev 0.0
+ #endif
+ #ifndef SharedXtRev
+ #define SharedXtRev 0.0
+ #endif
+ #ifndef SharedXawRev
+ #define SharedXawRev 0.0
+ #endif
+ #ifndef SharedXmuRev
+ #define SharedXmuRev 0.0
+ #endif
+ #ifndef SharedXextRev
+ #define SharedXextRev 0.0
+ #endif
+ #ifndef SharedXinputRev
+ #define SharedXinputRev 0.0
+ #endif
+ #ifndef SharedXTrapRev
+ #define SharedXTrapRev 0.0
+ #endif
+ #ifndef SharedPexRev
+ #define SharedPexRev 0.0
+ #endif
+ 
+ SHLIBLDFLAGS = SharedLibraryLoadFlags
+ PICFLAGS = PositionIndependentCFlags
+ 
+ /*
+  * and now a little bit of magic for using imake without source tree; if we
+  * are using shared libraries, we really do not need to depend on anything
+  */
+ #if SharedLibXext
+   DEPEXTENSIONLIB = /* _UseCat($(USRLIBDIR),$(EXTENSIONSRC)/lib,/libXext.sa.$(SOXEXTREV)) */
+      EXTENSIONLIB = _Use(-lXext,-L$(EXTENSIONSRC)/lib -lXext)
+ #endif
+ #if SharedLibX
+           DEPXLIB = $(DEPEXTENSIONLIB) /* _UseCat($(USRLIBDIR),$(XLIBSRC),/libX11.sa.$(SOXLIBREV)) */
+              XLIB = $(EXTENSIONLIB) _Use(-lX11,-L$(XLIBSRC) -lX11)
+ #endif
+ #if SharedLibXmu
+         DEPXMULIB = _UseCat($(USRLIBDIR),$(XMUSRC),/libXmu.sa.$(SOXMUREV))
+        XMULIBONLY = _Use(-lXmu,-L$(XMUSRC) -lXmu)
+            XMULIB = _Use(-lXmu,-L$(XMUSRC) -lXmu -L$(TOOLKITSRC) -L$(EXTENSIONSRC)/lib -L$(XLIBSRC))
+ #if !defined(UseInstalled) && !defined(XawClientLibs)
+ #define XawClientLibs $(XAWLIB) $(XMULIBONLY) $(XTOOLLIB) $(XLIB)
+ #endif
+ #endif
+ #if SharedOldLibX
+        DEPOLDXLIB = /* _UseCat($(USRLIBDIR),$(OLDXLIBSRC),/liboldX.sa.$(SOOLDXREV)) */
+           OLDXLIB = _Use(-loldX,-L$(OLDXLIBSRC) -loldX)
+ #endif
+ #if SharedLibXt
+       DEPXTOOLLIB = _UseCat($(USRLIBDIR),$(TOOLKITSRC),/libXt.sa.$(SOXTREV))
+          XTOOLLIB = _Use(-lXt,-L$(TOOLKITSRC) -lXt)
+ #endif
+ #if SharedLibXaw
+         DEPXAWLIB = _UseCat($(USRLIBDIR),$(AWIDGETSRC),/libXaw.sa.$(SOXAWREV))
+            XAWLIB = _Use(-lXaw,-L$(AWIDGETSRC) -lXaw)
+ #endif
+ #if SharedLibXinput
+         DEPXILIB = /* _UseCat($(USRLIBDIR),$(XILIBSRC),/libXi.sa.$(SOXINPUTREV)) */
+            XILIB = _Use(-lXi,-L$(XILIBSRC) -lXi)
+ #endif
+ #if SharedLibPex
+         DEPPEXLIB = /* _UseCat($(USRLIBDIR),$(PEXLIBSRC),/libPEX5.sa.$(SOPEXREV)) */
+            PEXLIB = _Use(-lPEX5,-L$(PEXLIBSRC) -lPEX5)
+ #endif
--- END OF X PATCHES ---

---
Hung-Chi Chu	Email: f80204@cc.ee.ntu.edu.tw

------------------------------------------------------------------------------