How can I check the built-in function implementation in Objective-C? - ios

I would like to ask if there is a possibility to check a built-in function implementation in Xcode IDE. I mean - is there something like CTRL + [click on function name] in IntelliJ? I need to check arc4random() implementation.

In Xcode, you can hold command + click on function name to jump to file which function is defined (.h file).
If it's a built-in function, you can only see in .h file (Header file). You can't open built-in function in .m or .ccp file.
With arc4random() you can open stdlib.h but you can't check how it's implemented from XCode. Luckily you can check it here https://opensource.apple.com/source/Libc/Libc-997.90.3/gen/FreeBSD/arc4random.c.
opensource.apple.com is provided by Apple
EDIT:
To open stdlib.h from Xcode
Step 1: command + click to arc4random()
Step 2: Look at navigation bar
Step 3: Right click on stdlib.h
Step 4: Click on include folder and you will see what you want.

You can't access this specific implementation because it is Operating System's specific. But I think it's pretty likely that it's the same as BSD's implementation, since the Apple documentation describes it as "BSD Library Functions".
Edit: Being more specific, this function is part of the C standard library, and it's common practice to simply link it with the binary version distributed as part of the OS. So only the headers are normally available. Apple makes their libc source code available in the http://opensource.apple.com site though.
More broadly speaking though, you won't find the implementation of most Apple-related frameworks, except maybe Foundation, which is currently open source, and the Swift standard library.

you can't check implementation because it is defined in stdlib.h and you don't have its implementation file access.

Related

How can I modify the contents of an public m file in a dynamic library?

I made a framework, The files I chose to make public were .h and .m files. I found that if I modify the contents of the .m file directly, it won't take effect. So what should I do to take effect?
Maybe I'm misunderstanding your question, but in the abscence of other answers let's see if I can help:
I made a framework
So you wrote some text into some files; then you used a tool, probably Xcode, to invoke the compiler, which interpreted that text as Objective-C and produced machine code in another file, and then constructed a framework bundle for you.
The files I chose to make public were .h and .m files. I found that if I modify the contents of the .m file directly, it won't take effect.
So now you edit your text file, and what do you expect to happen - not sure. Do you expect the framework code to change? If so aren't you missing a step compared to the above?
So what should I do to take effect?
Well that depends on what your goal is here. If you want your users to be able to customise your framework in some way then you need to design a method to do that using whatever tools you can when iOS is your target (Apple has rules).
This answer isn't much, but hope it helps.

Using C++ library in iOS application

I've a task in which I need to use a .a c++ library (don't have any idea what is the code in that library) with a header file having some methods declared in it. Now what I need to do is to call these methods from my objective c class. for this I've done following things:
changed the extension of my view controller class from .m to .mm
set my 'other C++ flags' in build settings to objective-c++
all other steps which were suggested at stack overflow and any where else.still I'm not able to use that library. An example will give you all the better understanding on my problem:
suppose We've a library mylibrary.a,We also have a header file named myHeaderFile.h. Now we need to call a method named 'int Login(unsigned long *LoginInfo)' which is declared in myHeaderFile.h header file. What should be done? The screen shot is the crash report when I try to call C++ method
What can be mistake here,please suggest. A quick help will be appreciated...
Attach to the app with any debugger or turn on Error Reporting to see what exactly crashes.
UPD: Apple's LLDB tutorial. You need these steps:
Specifying the Program to Debug
Launching the Program with LLDB (r, then Enter)
Finally, when it crashes, type bt, then Enter.

Rewriting symbols in static iOS libraries

I am working on an iOS app which links several static libraries. The challenge is, those linked libraries define same method names with different implementations. Oddly, I don't get any duplicate symbol definition errors; but, to no surprise, I end up with access to only one implementation of the method.
To be more clear, say I have libA and libB and they both define a global C method called func1()
When I link both libA and libB, and make a call to func1(), it resolves to either libA's or libB's implementation without any compilation warning. I, however, need to be able to access both libA's func1() and libB's func1() separately.
There's a similar SO post that explains how it can be done in C (via symbol renaming) but unfortunately, as I found out, objcopy tool doesn't work for ARM architecture (hence iPhone).
(I will submit it to the App Store, hence, dynamic linking is not an option)
It appears that you are in luck - you can still rename symbols with the ARM binary format, it's just a bit more hacky than the objcopy method...
NOTE: This has only been tested minimally, and I would strongly advise you to make a backup of all libraries in question before trying this!
Also note that this only works for files not compiled with the C++ compiler! This will fail if the C++ compiler was used on these files.
First, you will need a decent hex editor, for this example, I will be using Hex Fiend.
Next, you will open up a copy of your of of your libraries, let's call it lib1-renamed.a, and do the following with it:
Find the name of the symbol you wish to re-name. It can be found using the nm tool, or, if you know the header name, you should be set.
Next, you will use hex fiend, and to a textual replace of the old name (in this case foo), and give it a new name (in this case, bar). These names must have the same length, or it will corrupt the binary's offsets!
Note: if there is more than one function that contain's foo's name in it, you may have problems.
Now, you must edit the headers of the library you changed, to use the new function name (bar) instead of the old one.
If you have done the three simple† steps above properly, you should now be able to compile & link the two files successfully, and call both implementations.
If you are trying to do this with a universal binary (e.g. one the works on the simulator as well), you'd be best off using lipo to separate the two binaries, using objcopy on the i386/x64 binary, and then using my method on the ARM binary, and lipo it back together.
†: Simplicity is not guaranteed, nor is it covered by the Richard J. Ross III super warranty. For more information about the super warranty, call 1-800-FREE-WARRANTY now. That's 1-800-FREE-WARRANTY now!

Why won't this simple LiveCode iOS external compile?

I created a simple external using the LiveCode iOS externals SDK. The test.lcidl file is as follows:
external test
function testMyExternal
return boolean
The test.mm file is as follows:
bool testMyExternal(void) {
return true;
}
The test.ios file is the default Foundation framework.
This is about as simple as it gets but it won't compile... why not?
This question was asked on a LiveCode listserve and I'm asking and answering here because the answer will be useful for others.
There are a few problems here:
First is the ios file which specifies frameworks and libraries to compile the external against includes Foundation framework yet the use objc-objects clause is not specified in the .lcidl file. If you don't want to use objective c objects then remove the foundation framework from the .ios file.
Second is the file is a .mm which is Objective-C++ and the use c++-naming clause is not specified. If you don't want C++ you can change the .mm to .c for C or .m for Objective-C.
More detail can be found in section 6.3 of the documentation
Monte managed to answer his own question but in this case the external is a .mm file which means its obj-c++. This means you need to add use c++-naming in the lcidl file
otherwise the glue code that's generated will look for C-style (unmangled) names
(C++ 'mangles' names of functions to include the typing information so that they can be overloaded)

How are headers and functions obtained from iOS private api binaries ? How to reverse engineer an app?

I have read that class-dump utility is used to dump headers from iphone private api's. However, it does work only for objective-c frameworks. I wanted to know how it works for frameworks written in C, example - IOSurface, IOMobileFramebuffer etc.
The second part of the question is very generic. I have an app with me downloaded from cydia or istore. How do i go about reverse engineering the app on a jailbroken deivce (if that is actually needed). To be specifc, I am able to locate the executable binary and the dylibs. I am able to see the frameworks being used. But, how do I make out what functions inside the frameworks are being called by the app ?
Thanks.
I would recommend to break down this question to two questions:
1) "I wanted to know how it works for frameworks written in C, example - IOSurface, IOMobileFramebuffer etc."
I had exactly the same question:
Getting signatures of private API methods for iOS
The answer is
a) Try to google C method to see whether somebody else has disassembled it and found method signature.
b) If nobody did this before, you can be the first who will do it
2) "how do I make out what functions inside the frameworks are being called by the app ?"
There are two types of references to functions in frameworks/libraries:
Compile time references
You use some disassembler, it will list all compile time references to frameworks/dylibs.
Runtime references
These are references when somebody does dlopen, dlsym or NSBundle to use some functions.
You will have to disassemble and look/grep through disassembled code to find where they are used. There will be strings with the names of methods which are used.

Resources