Hack your iPhone: toolchains and cross-compilers

The iPhone can run “web apps” very easily. These web apps are really just specially-crafted web pages that take advantage of the iPhone’s built-in web browser. Such web apps can be very cool, but they don’t provide the full functionality that you can get from an actual executable program running on the iPhone. A “native app” that runs on the iPhone can read the iPhone’s built-in accelerometer, for example.

So lots of people want to write independent/third-party native apps that run on the iPhone. The problem is that while Apple hasn’t squashed the development of native apps, they haven’t helped, either. For example, Apple hasn’t provided a toolchain to compile programs for the iPhone.

What is a toolchain?

Yeah, I know, it’s been I while since I had my compiler class too. Here’s a rough diagram of what happens when you compile source code into a program:

Compilation process: compiler to assembler to linker

Here’s the process as I recall it. You write code in high-level computer languages such as C, C++, or Objective-C. That gets piped through a compiler (“gcc” in this diagram), which creates assembly code targeted for a certain type of machine architecture such as Intel x86, PowerPC (PPC), or ARM. Most desktop machines, including newer Apple computers, use Intel or Intel-compatible chips. Older Apple computers use PowerPC chips. ARM chips are embedded in lots of popular consumer electronics devices (e.g. the iPod and iPhone).

Now you’ve got assembly code for a particular type of computer chip. That code is processed through an assembler (“as” in this diagram), which spits out actual machine code (also called “object code”) of zeros and ones. In some cases that code can be executed directly. However, usually you have multiple files of object code to combine, or you need to link in built-in functions from system libraries. A linker (“ld” in this diagram) pulls all the object code into one executable program that a user can run.

So what is a toolchain? Roughly, a toolchain is a set of programs that allow you to compile source code into a program. It can include the programs that I just mentioned, plus a host of other programs such as “make” or a debugger that make compiling programs easier.

What is a cross-compiler?

Look back at the diagram above. Typically someone on (say) a newer Apple computer would have an Intel chip inside. So gcc would output assembly code for an Intel chip, and that assembly code would eventually be transformed into an executable program that runs on a Intel chip. That lets you compile and run programs on your own computer.

But nothing in the diagram above says that you have to output assembly code for the same type of computer chip as the machine that you’re running. For example, an Intel-based Apple computer could translate source code into assembly code for an ARM-based machine architecture. That’s called “cross-compiling,” because you’re compiling your code across for a different type of chip.

Why would you want to cross-compile anything? Well, iPods and iPhones are based on ARM chips. Can you imagine trying to compile source code on one of those little devices? It would be a real pain. Cross-compiling solves that problem: you can compile code on a desktop machine for (say) the iPhone, then figure out how to copy your executable program over to the iPhone, and figure out some way to run your program.

What does this have to do with the iPhone?

It’s possible (but currently difficult) to make a working toolchain for Apple computers that cross-compiles code so that the iPhone can run it. Once you have a cross-compiling toolchain, you can write code that runs on the iPhone.

Unfortunately, Apple hasn’t provided any documentation on the application programming interface (API) or system libraries that would allow people to write native applications for the iPhone. Luckily, dedicated people have helped to produce not only a toolchain but also to fill in documentation on the system APIs that the iPhone supports. In the same way that Apple desktop machines have a framework called AppKit, the iPhone has a user-interface (UI) framework called UIKit.

More reading about toolchains/cross-compiling

If you dig deeper into nearly anything I just said about toolchains or cross-compiling, you’ll discover that it’s more complicated than I just laid out. For example, libraries of built-in system functions don’t have to be statically combined into a program. It’s possible to wait until the program is being loaded to execute before linking in a library. And so-called “binutils” provide lots of functionality other than compilation. For an overview of the compilation process, see the book Running Linux for a good description. To learn about linking vs. loading, I enjoyed this Linux Journal article. You can read more about GCC or binutils on the official GNU site. And of course Wikipedia provides a variety of strong articles about the GNU tool chain, linkers, and crosscompiling.

26 Responses to Hack your iPhone: toolchains and cross-compilers (Leave a comment)

  1. Matt,
    are you almost done with this iPhone dribble? Can you send a message to the 95% of us who read your blog looking for insight on Google when you’re done sucking hard on Apple’s teet ? Enough already with the iPhone… this blog is really beginning to bite pretty bad. We all knew you were a total nerd because you work for G! and do the nerd conventions but this is puke.

    have you seen the film “Yesterday” ? the one about the African woman who gets HIV from her husband who is very promiscuous and he makes a fool of her in the village yet she takes care of him until his death because all she wants is for her daughter to go to school which she had never the opportunity..

    It’s the first international film in native Zulu language with English subtitles, the soundtrack is haunting. See what some fellow blogger person has to say…

    http://kwailawai.blogspot.com/2004/06/yesterday.html

    If and when you’re not too busy fiddling with your “gadget”, you might take 2 hours of your life to enlighten yourself to how indigenous people live. Otherwise, carry on with your dribbling…

  2. What I find interesting is how Apple and AT&T make “iphone hacking” so difficult and litigious when Steve and Woz Built apple with the money they made from “hacking” phreaking tools to use on AT&T in the first place

    http://9to5mac.com/steve-jobs-hacks-phones-234556455

    oh the irony

  3. They just don’t want to deal with a bunch of apps that will crash until they get a good market share. Besides, if they made it easy, it wouldn’t half as much fun for the people doing the hacking. Unravelling the puzzle is most of the fun for some people. In a year or two there will probably be a full slew of apps for the iPhone.

  4. “However, usually you have multiple files of object code to combine, or you need to link in built-in functions from system libraries. A linker (”ld” in this diagram) pulls all the object code into one executable program that a user can run.”

    Minor nit: These days, many programs are dynamically linked … so just references to external libraries are in the final executable. Some advantages of this are smaller .exe’s, shared code in memory (how many programs use libc?), and “instant upgradability” when those shared libraries are patched.

    Mechanisms are in place via versioning (often minor/major numbers) to handle cases where the interface/behavior should be identical and/or allowed to change.

  5. I think you’ve entered a time warp. We are back in the day where the best selling computer book was “Undocumented DOS” and the evil giant was shifting from IBM to MS with the current possibility of the cycle moving to include Apple.

  6. Hehehehe,

    Thanks for the recap on CS 101, I think we called it as the ‘seperate compilation process’.

    Btw, what is this Objective C. I don’t really remember anything about it.

    Maybe Gosling can convince Apple to put a jvm in their iphone for third party vendors to write code for it.

  7. Hi Matt,
    although discussing Iphone might seem interesting to a lot of people, i think currently webmasters are shaking in their pants thinking about Pr update, or rather lack of it. Could you write a post about what is going on at Google? Why id the update so late, blah blah, etc.?
    πŸ™‚

  8. Dave (The Other One), I did a post on Sunday reminding people how to get my Google/search-only posts:
    http://www.mattcutts.com/blog/attack-of-the-gadget-posts/

    I’m mainly doing this series of posts because I was digging into this subject over the Labor day weekend and found that the whole subject wasn’t that well-documented. So I’m trying to get some of what I needed to figure out documented so that other people can save time and don’t have to figure it out for themselves. Thanks for the link though.

    alek, absolutely; I just wanted to get the simple version documented.

    Adnan, Objective C started on the NeXTstep operating system. Apple bought NeXT, so that programming environment (Interface Builder, Objective C, etc.) became the core of Mac OS X. When you see “NS” at the beginning of an Apple system call, I believe that stands for NeXTstep.

  9. Matt,

    I am very happy to see you blogging about hacking the I-phone. It obviously is interesting and exciting for you. That is one of the great things about your blog, it is YOUR blog, and you get to do with it whatever you want.

    If Oprah can talk about hunger in Africa, and George Clooney can talk about the war in Iraq, then you definitely can talk about the I-phone!

    And for my fellow readers, remember, this is not the Google Press Release and Secrets released so we can make more money SEO’ing our site better site.

    It is Matt’s I will write on whatever I damn well feel like even if it doesn’t interest you site. Tee hee!

    Unfortunately you are only coming up around #40 for
    iphone+toolchain
    http://www.google.com/search?hl=en&q=iphone+toolchain&btnG=Google+Search

    dk

  10. I suspect that the dead hand of the mobile phone companies might somthing to do with the locked down nature of the iPhone.

    Mobile seems to be where the dinosuars go when they cant hack life in a dereulated moden telco – the sort of people who are more backward looking than the SWP activists.

    The post took me back to the days when i did proper (FORTRAN and PL/1) coding on PR1MES ah the days of a compete compile/load sequence that would fix bugs.

    The new iphone style I pods look cool if you want to play with coding for the platform and arn’t to botherd about the phone access.

    geting kismet to run on a iphone would be a cool war driving /network security tool.

  11. If you are interested in cross compiling for ARM then I would recommend looking at the MokoMakefile which builds a complete system to run on the neo.

    http://wiki.openmoko.org/wiki/MokoMakefile

    Even if you decide that you want to hack the iPhone, this may help you learn the concepts.

    OpenMoko uses bitbake which is like a portage system for cross compiling. I suppose it would be possible to use bitbake to compile for the iPhone, but I am not sure.

    http://bitbake.berlios.de/manual/

  12. Now I just have to get an iPhone and I’ll be set πŸ™‚
    At least Apple dropped the price 200$

  13. When are advertisers paying for our Iphones? Just every time we start up our phone some advertiser sends us a message (only 1 a day). In this way Apple can make more money via advertising! What do you reckon? Like google adwords!?

  14. I know it’s technically correct, but does anyone else think it’s odd calling C a “high level language” these days? The number of people with any experience at all of assembler seems to be dropping – there are plenty of comp sci courses where it’s not even taught – so I increasingly think of C as “low level”, as opposed to PHP or Python or whatever.

  15. Whoa..Iphone dropped $200 ? when ? huhu..guess i’m buying it too lol

  16. It’s the first international film in native Zulu language with English subtitles, the soundtrack is haunting. See what some fellow blogger person has to say…

  17. Just got my Iphone! I could surf the web with Iphone, and even chat with folks over Iphone MSN, but a bit concern about online securities as it doesn’t look like having anti-virus software installed.

  18. Hi All,

    May I know from anyone out there… When the Apple iPhone is releasing in India. I would highlight it on my website’s homepage http://www.sendindiagifts.com; Just to make Indians (living outside India)elligible to send iPhones across India to their loved ones.

    Please do let me know if anyone knows when its launching in India (GSM)

    Jamila Delhiwala

  19. Hi Matt ,

    in this case i hav to tell you something..
    Why hack phones ?
    There is a little source to get flikr fotos videos and more on mobile phone without hacking programms.

    Do you know about aral Balkan ?
    If you hav 65 Minutes watch this

    http://213.200.64.229/pema07/flashmedia/aralbalkan.html

    SWX its a new way to get native data into iphone or other mobile platforms..

    thx for reading..

  20. Hi Matt,
    although discussing Iphone might seem interesting to a lot of people, i think currently webmasters are shaking in their pants thinking about Pr update, or rather lack of it. Could you write a post about what is going on at Google? Why id the update so late, blah blah, etc.?

  21. Matt, I cannot believe, I got educated a lot about my iPhone on your blog. Great tips you wrote, I try almost everything that you wrote. Hope that will be more of this. πŸ™‚

  22. Matt, Got a lot of info on Hacks. Will be trying one by one and will post the results πŸ™‚

  23. Hi, Matt,

    Thank your for sharing so much! You just make complex things in simple words. πŸ˜‰

  24. Technical information like Toolchain, Cross compiler, the connection between cross compiler & iPhone is really helpful to know the basic principle of iPhone operation.

  25. Hello Matt,

    Even though this post is quite old, it has be a valuable resource for my research. I have learned so much about compilers and tool chains. This blog post was the starting point of all of it, so thank you for this post. I have a question that I have been unable to answer. Perhaps you can help me out:

    1. Do traces of tool chains and compilers remain in the executable file? For example, if I make my software on Windows for OS-X, can I find out if it was made on Windows just by looking at the executable file? Or even using MacRuby to write an OS-X application, is it possible to know it was made with MacRuby by looking at the executable file?

    Thanks again for the wonderful wealth of information. I will continue to refer back to your blog for future research as well. Looking forward to your response.

    Sincerely,
    Aaron Franco

  26. Dear Matt Cutts,

    I read your artical of cross-compiler in your web site.
    I am a yeyoul Lee.
    I am currently working for samsung electronics.
    My major interest is cross-compiler for iphone.
    Because I want to transfer iphone application to other phone.
    How can I try to it?

    Also I am working for development to transfer software which from iphone contract and music, video, photo to other phone.
    If you have a good knowledge, please comment to me.

    Recently I have a interest of cloud phone which I imagine.
    Also unified and shared mobile application store or market.

    I think that you have a good knowledge for that.
    Please I want to discuss with you.

    Thanks

    Best Regard
    Yeyoul Lee

    P.S. please refer to bellow cross-platform approach example

    http://www.phonegap.com/about/

    Since winning the Web 2.0 Expo LaunchPad competition in April 2009, PhoneGap has been widely recognized as a game-changer for mobile app development. The open source code has been downloaded more than 200,000 times and hundreds of apps built using PhoneGap are available in mobile app stores and directories. In 2009, PhoneGap was named by InfoWorld as a top emerging enterprise technology in the category “cross-platform mobile app development”.

    The PhoneGap mission is to Web-enable native device functionality with open standards like HTML, CSS and JavaScript so that you can focus on the app you’re building, not on authoring complex platform compatibility layers.

css.php