Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



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:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.