public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Hendrik Muhs <Hendrik.Muhs@student.uni-magdeburg.de>
To: gcc-help@gcc.gnu.org
Subject: Problem with static linking
Date: Fri, 18 Jan 2002 10:44:00 -0000	[thread overview]
Message-ID: <20020118104400.06PjispuQfqIQy_So7PMOCFP4RzSn_zyo6y4pLmh1u0@z> (raw)

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

             reply	other threads:[~2002-01-18 18:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-18 10:44 Hendrik Muhs [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020118104400.06PjispuQfqIQy_So7PMOCFP4RzSn_zyo6y4pLmh1u0@z \
    --to=hendrik.muhs@student.uni-magdeburg.de \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).