[comp.bugs.sys5] nlist

ok@quintus (11/24/88)

If you give nlist(3) a name-list vector which contains duplicate
copies of symbols, only the first is correctly set.  I wanted to
generate a table of addresses, and gave nlist() the symbols I
wanted the addresses of in the order that I wanted them.  There
is nothing in the manual to suggest that it shouldn't work, but
it doesn't.

Tested on
	SunOS 3.2, Sequent Dynix (4.2), UTek (4.2), Intel 386 (V.3)

#!/bin/sh
cat >nlistbug.c <<'EOF'
/*  This program illustrates a bug in nlist(3).

    The manual page says
	Each name is looked up in the name list of the  file.
	If the name is found, the type and value of the name are
	inserted in [[n_type and n_value]].
	If the name is not  found, both  entries  are set to 0.

    If a symbol appears twice in the name-list, you would expect,
    from this description, that both copies would be filled in.
    But only the first occurrence is filled in, and later ones are
    treated as if the symbol could not be found.
    Nothing in the manual page says a symbol should appear only once
    in the name-list argument!

    To demonstrate this bug on 4.xBSD systems:
	% cc nlistbug.c
	% a.out
    To demonstrate this bug on System V systems:
	$ cc -DSYS5 nlistbug.c
	$ a.out
    If the bug is present, the first entry will be ok and the second
    entry will have zeros.  If both have zeros, something else is wrong.
*/

#include <stdio.h>
#include <nlist.h>

main()
    {
	int i;
	struct nlist NL[3];
	static char myname[] =
#ifdef	SYS5
		"main";
#else /*BSD4*/
		"_main";
#endif/*SYS5*/
	static char myfile[] = "a.out";

	NL[0].n_name = myname;
	NL[1].n_name = myname;
	NL[2].n_name = NULL;

	i = nlist(myfile, NL);
	if (-1 == i) {
	    printf("nlist(\"%s\", ...) failed\n", myfile);
	} else {
	    printf("nlist(\"%s\", ...) returned %d\n", myfile, i);
	    for (i = 0; i < (sizeof NL)/(sizeof NL[0])-1; i++)
		printf("NL[%d] = {%s, %x, %lu)\n",
		    i, NL[i].n_name, NL[i].n_type, NL[i].n_value);
	}
	exit(0);
    }
EOF
exit 0
--