delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/07/05/18:49:31

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <42CB0F4E.A01AC6A3@dessent.net>
Date: Tue, 05 Jul 2005 15:53:02 -0700
From: Brian Dessent <brian AT dessent DOT net>
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: Load a shared library using gcc/Cygwin
References: <200507051911 DOT j65JBrn4005874 AT citheronia DOT ucdavis DOT edu>
X-Spam-Report: -5.9/5.0 ---- Start SpamAssassin results * -3.3 ALL_TRUSTED Did not pass through any untrusted hosts * -2.6 BAYES_00 BODY: Bayesian spam probability is 0 to 1% * [score: 0.0000] * 0.0 AWL AWL: From: address is in the auto white-list ---- End SpamAssassin results
X-IsSubscribed: yes
Reply-To: cygwin AT cygwin DOT com

Yu-Cheng Chou wrote:

> int main(){
>    void *handle;
>    int (*fp)();
>    char *modname = "./module.dll";
>    HMODULE h;
>    void (*init)();
> printf("hello1\n");
>    h = LoadLibrary("cygwin1.dll");
> printf("hello1 h  = %p\n", h);
>    init = ( void (*)())GetProcAddress(h, "cygwin_dll_init");
> printf("init = %p\n", init);
>    init(); // CRASH HERE.......!!!

Of course it crashes, you aren't doing anything to address the fact that
the bottom CYGTLS_PADSIZE bytes of the stack will be clobbered by
Cygwin's tls structure.  This is mentioned in the FAQ and explained
further in how-cygtls-works.txt and the cygload sample.  In a Cygwin
program, this is handled for you automatically by the startup code, but
not so if your program is compiled with VC++ and uses its runtime.

This means you have to add the necessary scratch space to the stack, as
low on the stack as possible after the entry to main().  But the
arguments to main() as well as its return location will already be on
the stack (as well as whatever arbitrary values the MSVCRT chooses to
put there) which means they will be clobbered as well.  You can work
around this by making copies of the arguments and not returning from
main().  Or you can save the state of the stack pre-clobbering and then
restore it before returning from main(), as cygload does.

> I followed the instructions from FAQ to load a shared library.
> But the program main.exe crashed at the line init() highlighted in the
> main.c
> 
> How can I fix the problem?

Go back and read the FAQ again and make sure you understand it all.

Brian

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019