GithubHelp home page GithubHelp logo

Take advantage of weak linking about logos HOT 1 OPEN

theos avatar theos commented on June 16, 2024
Take advantage of weak linking

from logos.

Comments (1)

NSExceptional avatar NSExceptional commented on June 16, 2024

I think I originally misunderstood the entire purpose of weak_import in the first place. From some old Apple Support forum:

iOS and OS X use a "two level namespace" for symbol binding. That means the static linker records from which dylib each undefined symbol was found. At launch time, dyld uses that info to search for that symbol only in that dylib. The use of weak_import means the symbol might be missing at runtime—but still, it must be found at build time (to record the dylib it might be in at runtime). This is different than ELF system were "weak" means the linker won't complain if no definition is found.

There are compiler flags to get around this, but they either require flags for specific symbols in question (yuck) or opting into dynamic_lookup behavior for all undefined symbols, unfortunately:

  1. -U <symbol> where <symbol> is something like _OBJC_CLASS_$_SomeClass
  2. -undefined dynamic_lookup which means all undefined symbols are looked up by dyld.

Because of 2., we know the functionality we want is possible; that is, dyld will happily search all images for a symbol. I've tested it. Sample code:

#include <stdio.h>
#import <Foundation/NSObject.h>

__attribute__((weak_import))
@interface NSArray : NSObject
+ (instancetype)new;
@end

int main() {
    id obj = [NSArray new];
    if (obj) {
        printf("Success\n");
    } else {
        printf("Failure\n");
    }

    return 0;
}

Note that the weak_import attribute is required; all it does is tell dyld to ignore runtime linking errors. Without it, the code will still compile, but it will crash. Anyway, compiled with clang main.m -lobjc -undefined dynamic_lookup -o foo, tested like so:

$ ./foo
Failure
$ DYLD_INSERT_LIBRARIES=/System/Library/Frameworks/Cocoa.framework/Cocoa ./foo
Success

As expected, compiling without -undefined dynamic_lookup yields

Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_NSArray", referenced from:
      objc-class-ref in main-97d90b.o
ld: symbol(s) not found for architecture x86_64

It is simply unfortunate that this isn't a language feature yet :/

from logos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.