public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Problem with static linking
@ 2002-01-18 10:44 Hendrik Muhs
  0 siblings, 0 replies; 11+ messages in thread
From: Hendrik Muhs @ 2002-01-18 10:44 UTC (permalink / raw)
  To: gcc-help

Hi,

I have a problem with static linking. Please look at the following example.

The main programm foo "dlopens" the shared library bar and calls the function 
in bar.

If I compile at least bar.c or foo.c not static:

gcc -o foo foo.c -ldl
gcc -shared -o bar.so bar.c

or 

gcc -static -o foo foo.c -ldl
gcc -shared -o bar.so bar.c

or 

gcc -o foo foo.c -ldl
gcc -shared -o bar.so bar.c -Wl,-static

it works. But if compile both static:

gcc -static -o foo foo.c -ldl
gcc -shared -o bar.so bar.c -Wl,-static

I get a segfault(in malloc):

hendrik@tux:~> ./foo
we are in bar
Segmentation fault

I tried it with different versions of gcc: 2.95.3, 3.0.3(both SuSE Linux 7.3) 
and 2.96(Redhat Linux 7.2)

the code:

//foo.c
#include <stdio.h>
#include <dlfcn.h>
int main (void)
{
  void *lib;
  int (*sym)(void);
  lib = dlopen("./bar.so", RTLD_NOW);
  if (!lib) {
        fputs (dlerror(), stderr);
        exit(1);
  }
  sym = dlsym(lib, "bar");
  (*sym)();
  dlclose(lib);

  return 0;
}

//bar.c
#include <stdlib.h>
#include <malloc.h>
int bar(void)
{
 char *buf;
 printf("we are in bar\n");
 buf = malloc(5);
 printf("return to foo\n");
 return 0;
}

Why does it segfaults?

Thanks in advance,

Hendrik

^ permalink raw reply	[flat|nested] 11+ messages in thread
* Problem with static linking
@ 2002-01-18 10:44 Hendrik Muhs
  0 siblings, 0 replies; 11+ messages in thread
From: Hendrik Muhs @ 2002-01-18 10:44 UTC (permalink / raw)
  To: gcc-help

Hi,

I have a problem with static linking. Please look at the following example.

The main programm foo "dlopens" the shared library bar and calls the function 
in bar.

If I compile at least bar.c or foo.c not static:

gcc -o foo foo.c -ldl
gcc -shared -o bar.so bar.c

or 

gcc -static -o foo foo.c -ldl
gcc -shared -o bar.so bar.c

or 

gcc -o foo foo.c -ldl
gcc -shared -o bar.so bar.c -Wl,-static

it works. But if compile both static:

gcc -static -o foo foo.c -ldl
gcc -shared -o bar.so bar.c -Wl,-static

I get a segfault(in malloc):

hendrik@tux:~> ./foo
we are in bar
Segmentation fault

I tried it with different versions of gcc: 2.95.3, 3.0.3(both SuSE Linux 7.3) 
and 2.96(Redhat Linux 7.2)

the code:

//foo.c
#include <stdio.h>
#include <dlfcn.h>
int main (void)
{
  void *lib;
  int (*sym)(void);
  lib = dlopen("./bar.so", RTLD_NOW);
  if (!lib) {
        fputs (dlerror(), stderr);
        exit(1);
  }
  sym = dlsym(lib, "bar");
  (*sym)();
  dlclose(lib);

  return 0;
}

//bar.c
#include <stdlib.h>
#include <malloc.h>
int bar(void)
{
 char *buf;
 printf("we are in bar\n");
 buf = malloc(5);
 printf("return to foo\n");
 return 0;
}

Why does it segfaults?

Thanks in advance,

Hendrik

^ permalink raw reply	[flat|nested] 11+ messages in thread
[parent not found: <200201181844.SAA08954@crotus.sc.intel.com>]
[parent not found: <478231340907151937x4f1059f8l4545bb071441c9f9@mail.gmail.com>]

end of thread, other threads:[~2009-07-16  8:20 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-18 10:44 Problem with static linking Hendrik Muhs
2002-01-18 10:44 Hendrik Muhs
     [not found] <200201181844.SAA08954@crotus.sc.intel.com>
2002-01-18 15:51 ` Robert A Nesius
2002-01-19  0:54   ` Hendrik Muhs
2002-01-19  0:54   ` Hendrik Muhs
     [not found] <478231340907151937x4f1059f8l4545bb071441c9f9@mail.gmail.com>
     [not found] ` <m3fxcx70qi.fsf@google.com>
2009-07-16  4:30   ` Zachary Turner
2009-07-16  5:46     ` Ian Lance Taylor
2009-07-16  6:28       ` Zachary Turner
2009-07-16  7:07         ` Ian Lance Taylor
2009-07-16  7:52           ` Zachary Turner
2009-07-16  8:20             ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).