lorder(1) bug + fix (all UNIX systems)

Sam Kendall sam at delftcc.UUCP
Tue Nov 19 14:54:07 AEST 1985


Index:	usr.bin/lorder.sh 4.2BSD +FIX

Description:
        Lorder(1) on (almost) all UNIX systems may produce incorrect
        ordering for object files containing uninitialized external
        (common) symbols.  Lorder tries to classify all symbols as
        "defs" or "refs", but a common symbol can be either, depending
        on the other instances of the symbol in other object files.

        This is most important on V7 systems, where this bug can lead
        (and has led, at this site) to undefined symbol errors from the
        loader, due to incorrect archive ordering.  This can also happen
        on 4.2BSD systems if ranlib(1) is not used.

        Lorder is less widely used on 4.2BSD and System 5.2 systems, but
        its use is necessary to ensure source portability to V7 and
        other systems (MS-DOS?) which lack System V's archive table of
        contents or 4.2BSD's ranlib.  That's probably why it is still
	around.

        Note: this bug will not occur on System 5.0 and other systems
        whose loaders do not support common storage.

Repeat-By:
	Write the following 3 files:
		main.c:		main(){
					f();
				}
		extern.c:	f(){
					extern int common;
					common = 5;
				}
		common.c:	int common;

	Compile them like this:
		cc -c main.c common.c extern.c
		ar cr lib.a `lorder common.o extern.o | tsort`
		cc main.o lib.a

        Ld will give a message about "_common" (or "common") undefined,
        because common.o was incorrectly placed before extern.o in the
        archive.

Fix:
        Lorder is almost identical on all UNIX systems.  The fix here is
        for the V7/4.2BSD version.  For System V: (1) don't change the
        options to nm; (2) add to the sed script to make the added part
        parallel to the other 2 parts, rather than exactly like what I
        have below.

	The temporary files should be put in /tmp--otherwise lorder will
	fail if the current directory is not writable--but I haven't
	bothered.

*** /usr/bin/lorder	Tue Sep 27 12:54:25 1983
--- /usr/lbin/lorder	Tue Aug 13 10:57:33 1985
***************
*** 1,4
  trap "rm -f $$sym?ef; exit" 0 1 2 13 15
  case $# in
  0)	echo usage: lorder file ...
  	exit ;;

--- 1,10 -----
  trap "rm -f $$sym?ef; exit" 0 1 2 13 15
+ 
+ # modified 8/13/85 by sam at delftcc.UUCP.  
+ # (1)	3rd tmp file $$symcef added for common blocks, which can sometimes
+ #	be refs, sometimes defs.
+ # (2)	-p option speeds up nm.
+ 
  case $# in
  0)	echo usage: lorder file ...
  	exit ;;
***************
*** 6,12
  	*.o)	set $1 $1
  	esac
  esac
! nm -g $* | sed '
  	/^$/d
  	/:$/{
  		/\.o:/!d

--- 12,18 -----
  	*.o)	set $1 $1
  	esac
  esac
! nm -pg $* | sed '
  	/^$/d
  	/:$/{
  		/\.o:/!d
***************
*** 23,28
  		w '$$symdef'
  		d
  	}
  	s/.* //
  	G
  	s/\n/ /

--- 29,41 -----
  		w '$$symdef'
  		d
  	}
+ 	/C /{
+ 		s/.* //
+ 		G
+ 		s/\n/ /
+ 		w '$$symcef'
+ 		d
+ 	}
  	s/.* //
  	G
  	s/\n/ /
***************
*** 30,34
  	d
  '
  sort $$symdef -o $$symdef
  sort $$symref -o $$symref
  join $$symref $$symdef | sed 's/[^ ]* *//'

--- 43,49 -----
  	d
  '
  sort $$symdef -o $$symdef
+ sort $$symcef -o $$symcef
  sort $$symref -o $$symref
  {
  	join $$symref $$symdef
***************
*** 31,34
  '
  sort $$symdef -o $$symdef
  sort $$symref -o $$symref
! join $$symref $$symdef | sed 's/[^ ]* *//'

--- 45,52 -----
  sort $$symdef -o $$symdef
  sort $$symcef -o $$symcef
  sort $$symref -o $$symref
! {
! 	join $$symref $$symdef
! 	join $$symref $$symcef
! 	join $$symcef $$symdef
! } | sed 's/[^ ]* *//'

----
Sam Kendall
Delft Consulting Corp.
(212) 243-8700
{allegra, seismo!cmcl2, ihnp4}!delftcc!sam



More information about the Net.bugs.usg mailing list