gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Multiple _OBJC_Module in the same shared library/framework, not so good


From: Nat!
Subject: Multiple _OBJC_Module in the same shared library/framework, not so good
Date: Tue, 3 Jan 2012 21:38:47 +0100

Hi

there is a problem I ran into with static NSStrings in frameworks +load 
methods, which I wanted to share. 

If I read the gnu runtime correctly, then for every struct objc_module 
(_OBJC_Module), there is eventually a call to __objc_exec_class.

__objc_exec_class does in succession a couple of initialization steps 

o) on the first run, one big time objc init

o) add classes/prototypes
o) hack static strings 
o) call +load

I suppose there is one _OBJC_Module per compile unit (i.e. file), but even if 
not, irregardless, currently multiple _OBJC_Module can appear in a .so file and 
then this otherwise harmless looking code will crash:

a.m:    
#import <Foundation/Foundation.h>

NSString  *A = @"A";


@implementation ATest

+ (void) load
{
  extern NSString  *Z;

  printf( "+[ATest load]\n");
  printf( "Z=%s\n", [Z cString]);
}

@end


z.m:
#import <Foundation/Foundation.h>

NSString  *Z = @"Z";


@implementation ZTest

+ (void) load
{
  extern NSString  *A;

  printf( "+[ZTest load]\n");   
  printf( "%s\n", [A cString]);
}

@end


It will crash, because one of the +load is happening before the external 
NXConstantString is fixed up.

This is not optimal. Optimally, the linker should combine multiple 
_OBJC_Modules into one for each shared library.

The workaround is to not use static NSStrings that are defined outside the 
current file in the same framework/library/tool during +load. static NSStrings 
in dependent linkage should be fine. (e.g. gui linking with base or so)

Ciao
  Nat!
---------------------------------------------------------
Any one who thinks about the future must live in fear and
terror. This is due to the fact that the intelligence and
character of the masses are incomparably lower than the 
intelligence and character of the few who produce some 
thing valuable for the community.  -- A. Einstein




reply via email to

[Prev in Thread] Current Thread [Next in Thread]