0

While tracing a bug in a static library we use in our iOS application I stumbled upon the following question:

The library we use has code that is automatically executed during the launch of our application, before our application's main method is executed. The stack trace while the function is executed looks as follows:

#0  0x000000010050ce04 in _runOnLoad ()
#1  0x000000012008ceb0 in ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) ()
#2  0x000000012008d050 in ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) ()
#3  0x0000000120088808 in ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) ()
#4  0x00000001200879b0 in ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) ()
#5  0x0000000120087a64 in ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) ()
#6  0x000000012007a08c in dyld::initializeMainExecutable() ()
#7  0x000000012007e0fc in dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) ()
#8  0x0000000120079044 in _dyld_start ()

My question is: What is going on here?

  1. How does the image loader know that this method should be executed? When looking at the assembler of the static library, _runOnLoad() looks like a regular method. How is the information that this method should be executed on launch stored in the static library? Where is this information stored when the library is linked to our main application?
  2. How can you specify this when compiling? Can you flag a method to be executed on load? It this something you do in code or is it a compiler argument?
  3. While this method is run, what can I do? I assume other functions and classes are not guaranteed to be loaded yet?

This whole thing is happening inside an iOS application, but this seems to me to be very low level functionality, so I assume this works on every unix based platform?

I would love to learn some more about this.

Michael Ochs
  • 2,846
  • 3
  • 27
  • 33
  • Is this ok for you? http://stackoverflow.com/questions/9759880/automatically-executed-functions-when-loading-shared-libraries – Jean-Baptiste Yunès Nov 02 '15 at 17:10
  • I guess this answers question number 2 but it doesn't explain how this information is known inside the resulting .a file of the library nor what I can and can not do inside this function. – Michael Ochs Nov 03 '15 at 07:47
  • 1
    point 1. this is usually done via section `.init` of the library (an executable point). point 3. much more difficult because this is dependent of loading order... see https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter3-8.html for example. – Jean-Baptiste Yunès Nov 03 '15 at 10:59

0 Answers0