Loading a DLL after a fork is "fixed"

Chris Faylor cgf@cygnus.com
Sun Oct 24 19:22:00 GMT 1999


I've just posted a message to the cygwin mailing list indicating that
I've worked around the long-standing problem with loading DLLs in a
forked process.

Formerly, cygwin was able to load DLLs in a forked process only if the
DLLs were loaded into the DLL's specified base address.  That means if
two DLLs attempted to load to the same base address cygwin would blow up
on a fork.

I'm not exactly happy about the way that I had to fix this.  Here's
what I did:

1) Fork, starts a new process

2) "Forked" process does its usual magic, waits for DLL data segment to
    be filled in.  Then it determines if DLLs need to be loaded.

3) If DLLs need to be loaded, call
   LoadLibraryEx (name, NULL, DONT_RESOLVE_DLL_REFERENCES) for each
   name.  If the load address for the library == the parent's load address,
   everything is fine.  Load the library again and unload the previous handle.
   goto 5

4) If the address is different, mark as "reserved" all memory up to the parent DLL's
   load address.  Then attempt the load again.  The DLL should then load at the correct
   address.

5) Loop until no more DLLs

6) Go on about the fork().

As I said, this seems to work but gawd it's kludgy.

I couldn't find any better way to do this in any of my creative DejaNews or Altavista
searches.

If somebody has a better idea on how to do this, let me know.

-chris


More information about the Cygwin-developers mailing list