• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
__attribute__((constructor)) in dylib does not run with DYLD_INSERT_LIBRARIES
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

__attribute__((constructor)) in dylib does not run with DYLD_INSERT_LIBRARIES


  • Subject: __attribute__((constructor)) in dylib does not run with DYLD_INSERT_LIBRARIES
  • From: Brian Dantes <email@hidden>
  • Date: Mon, 28 Feb 2005 03:48:43 -0800

I'm trying to port an LD_PRELOAD trick from Linux to MacOS X. Given the
following two files:

foo.c:
------
#include <stdio.h>

int my_var = 64;

void __attribute__((constructor)) my_init() {
  printf("Constructor called!\n");
}

bar.c:
------
int main() {
  return 0;
}

Things work as expected on RedHat Linux Fedora Core 2:
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
------------------------------------------------------

% gcc -o bar bar.c
% gcc -o foo.so -fPIC -shared foo.c
% LD_PRELOAD="./foo.so" ./bar
Constructor called!
%

But on MacOS X 10.3.8, unhappiness:
gcc version 3.3 20030304 (Apple Computer, Inc. build 1671)
(This is the latest and greatest of Apple's gcc, AFAIK)
----------------------------------------------------------
% gcc -dynamiclib -fno-common -o foo.dylib foo.c
% gcc -o bar bar.c
% DYLD_INSERT_LIBRARIES="foo.dylib" ./bar
%
% DYLD_INSERT_LIBRARIES="foo.dylib" DYLD_PRINT_LIBRARIES=1 ./bar
loading libraries for DYLD_INSERT_LIBRARIES=foo.dylib
loading library: foo.dylib
loading libraries for image: ./bar
loading library: foo.dylib
loading library: /usr/lib/libSystem.B.dylib
loading libraries for image: foo.dylib
loading libraries for image: /usr/lib/libSystem.B.dylib
loading library: /usr/lib/system/libmathCommon.A.dylib
loading libraries for image: /usr/lib/system/libmathCommon.A.dylib
%

As you can see, foo.dylib is, in fact, loaded. If I change the code such
that bar.c references some symbol in foo.c, and link bar directly with the
library, it works as expected.

If anyone reading this has a Tiger seed, would you mind seeing if this is
still broken?

Does anyone know of a workaround in Panther? I can edit foo.c to my heart's
content, but bar.c is not available to me. I've been thinking about
overriding some common symbol I hope will never actually be used (although I
don't know what that might be) as a potential hack.

Thanks,
Brian



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: __attribute__((constructor)) in dylib does not run with DYLD_INSERT_LIBRARIES
      • From: Markus Hitter <email@hidden>
  • Prev by Date: Re: StartupItemContext & Darwin 6.x
  • Next by Date: DSS & JMF
  • Previous by thread: Re: Locale support vs. Carbon -- what the...?
  • Next by thread: Re: __attribute__((constructor)) in dylib does not run with DYLD_INSERT_LIBRARIES
  • Index(es):
    • Date
    • Thread