Search code examples
objective-cmach-o

What makes a symbol be lazy or non-lazy one?


I'm learning some knowledge about Mach-O file recently. There comes a question when I learn lazy-symbol & non-lazy symbol: who determinates a symbol to be lazy or non-lazy? and why?

I guess it might be the compiler do that...

UPDATE: I found the a answer about my question, maybe it's the truth:

Lazy binding is controlled by the -z option to the linker, ld. This option takes keywords as an argument; the keywords include (among others):

lazy When generating an executable or shared library, mark it to tell the dynamic linker to defer function-call resolution to the point when the function is called (lazy binding), rather than at load time.

now When generating an executable or shared library, mark it to tell the dynamic linker to resolve all symbols when the program is started, or when the shared library is linked to using dlopen(), instead of deferring function-call resolution to the point when the function is first called.

Lazy binding is the default. If you're using qcc (as we recommend), use the -W option to pass the -z option to ld. For example, specify -Wl,-zlazy or -Wl,-znow.

Reference:Optimizing the runtime linker

Related Question: Does Clang/GCC really support a delay loading feature?


Solution

  • Sounds like you've got the what understood.

    As for the why part:

    Doing things as late as possible: (a) reduces the work incurred to start up, and (b) avoids that work altogether if that execution path is never taken. This is true not just for the linker, but any execution that you can delay until needed.