Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 1 Apr 1995 03:35:38 +1000
From:      Bruce Evans <bde@zeta.org.au>
To:        jkh@freefall.cdrom.com, wollman@halloran-eldar.lcs.mit.edu
Cc:        current@freefall.cdrom.com
Subject:   Re: dlopen() and Garrett's comments on fn pointers!
Message-ID:  <199503311735.DAA07088@godzilla.zeta.org.au>

next in thread | raw e-mail | index | archive | help
>> In case no one has noticed, the dlsym() routine returns a void* for
>> both function and data pointers! :-)

>Yup.  You have to take an intermediate step through a large-enough
>integer (like unsigned long) in order to do it without diagnostics.
>(The result is still implementation-defined.)

Interesting.  dlsym() does the following:

	long addr;		/* WRONG large-enough integral type
				 * (subject to unwanted overflow traps
				 * and 1's complement conversions).
				 */
	...
	addr = np->nz_value;	/* nz_value is RIGHT large-enough type
				 * (unsigned long).
				 */
	...			/* sometimes adjust `addr' */
	return (void *)addr;	/* match the losing interface */

The rules for unportable casts are:

1. pointer -> integral: this may be done for some implementation defined
   integral type.  The result is implementation defined.  Casting to an
   integral type smaller than this type is undefined.  Where do we define
   this type? :-)

2. integral -> pointer: this may always be done.  The result is
   implementation defined.

[others omitted].

n. object pointer <-> function  pointer: never allowed (but rules 1-2
   allow pointer -> big integral -> pointer).

dlsym() should return the large-enough integral type and let the caller
cast it.  Rule 2 should not be used to mask errors.

The intermediate step actually makes the cast implementation defined
instead of undefined.  gcc on i*86's apparently prints more diagnostics
for the undefined case but you can't depend on this - it can do whatever
for the undefined case, including silently making it work, and it should
warn for implementation defined cases iff a cast loses information.

None of this should apply to the forms library unless you have dynamic
forms and want to write pointers to the forms objects and functions to
disk.

Bruce



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199503311735.DAA07088>