iPhone Honeypot Project

June 17, 2010

GCC on iPhone – Progress?

Filed under: Uncategorized — abnev @ 3:14 am

I’ve been trying to get gcc working on the iphone and I’ve taken several approaches but all to no avail.

  • install gcc on iPhone to set up developer environment on the device
  • install iPhone SDK (3.1.3) on Mac OSX and compile (and statically link) binary and move to phone

I have made progress with both of these approaches but still no cigar. In relation to setting up the environment, I was directed to http://iphonesdkdev.blogspot.com/2009/10/how-to-compile-mobilesubstrate.html. I managed to download the related files but ran into ld errors when attempting to make the example.

abnev-ip1:/var/toolchain/ExampleHook root# make
dyld: Library not loaded: /usr/lib/libgcc_s_v6.1.dylib
Referenced from: /usr/local/bin/make
Reason: image not found
Trace/BPT trap
abnev-ip1:/var/toolchain/ExampleHook root#

I then attempted to compile it manually by inspecting the Makefile:

abnev-ip1:/var/toolchain/ExampleHook root# arm-apple-darwin9-g++ -lobjc -framework Foundation -framework UIKit -framework CoreFoundation -multiply_defined suppress -L/var/toolchain/sys30/usr/lib -F/var/toolchain/sys30/System/Library/Frameworks -F/var/toolchain/sys30/System/Library/PrivateFrameworks -dynamiclib -init _ExampleHookInitialize -Wall -Werror -lsubstrate -lobjc -ObjC++ -fobjc-exceptions -march=armv6 -mcpu=arm1176jzf-s -fobjc-call-cxx-cdtors -o $@ $^    collect2: ld terminated with signal 5 [Trace/BPT trap]

dyld: Library not loaded: /usr/lib/libgcc_s_v6.1.dylib

Referenced from: /usr/local/bin/ld

Reason: image not found

In relation to attempting to compile on OSX the following errors occured:

I have managed to get this bit of it working (sort of) by installing the iPhone 3.1.3 SDK on my Mac and setting the architecture to the iPhone in the gcc line as follows (as per http://www.theiphonewiki.com/wiki/index.php?title=Toolchain_2.0);

/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk -o test test.c

I have managed to successfully compile a “Hello World” C program and executed it on the iPhone with success. e.g.

int main() {

printf("Hello, World!\n");
return(0);

}

This worked a charm. I am also using ‘ldid’ to sign the application (but only if necessary).

Anyways, I have been trying to get a program to compile which uses libpcap (http://www.tcpdump.org/) but with little success. I have downloaded and installed libpcap-1.1.1 on my mac and set the configure –prefix to /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk/usr/local and build the application. I then saw that the includes actually reside in /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk/usr/includes and so moved the /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk/usr/local/include files (which contained only the pcap stuff) to the correct location. I then attempted to compile the test program using;

/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc -I /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.3.sdk/usr/include/ -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk -o pcap pcap.c -lpcap

This worked a lot better than other tests but produces an error; i.e.

ld: library not found for -lcrt1.o collect2: ld returned 1 exit status

I have emailed a developer friend of mine and contacted the #iphone community on FreeNode (irc.linux.org) including reaching out to iphone developers via stackoverflow and twitter. I await for responses.

About these ads

2 Comments »

  1. I was just wondering how you even got a little bit of GCC on your iPhone. I have a fresh jailbreak and this is one of the first things I wanted to do (purity and all that). I’m actually running iOS4 on my phone, but got “Killed”. I then signed the command but get

    dyld: Library not loaded: /usr/lib/libgcc_s_v6.1.dylib
    Referenced from: /usr/local/bin/gcc
    Reason: image not found
    Trace/BPT trap

    I’m also wondering in the end what ./configure opts you used on your computer when compiling gcc.

    Thanks

    Comment by Wally Nelson — August 22, 2010 @ 12:00 pm | Reply

    • Hi Wally,

      Originally I had gcc on my phone that I installed from cydia but it seems to have been removed from their default repositories. I can’t seem to find it anywhere these days. Instead, I installed the iPhone SDK on my Mac. I then wrote a quick and dirty shell script called ‘gcc2′ and put it into ~/bin and updated my PATH environment variable to include this directory i.e. export PATH=$PATH:/Users/abnev/bin. (You can add this line to your ~/.profile so it’s executed everytime you start up Terminal.app. The script I wrote encompasses all standard command line arguments for GCC:

      #!/bin/sh

      GCC=”/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc”
      ISYSROOT=”/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.3.sdk” #3.1.3
      EXPECTED_ARGS=3

      # check num of args supplied
      if [ $# -ne $EXPECTED_ARGS ]
      then
      #print usage
      echo “Usage: `basename $0`: no input files”
      exit -1 #exit on error
      fi

      # e.g. gcc -arch armv6 -isysroot $ISYSROOT -o test test.c
      $GCC -arch armv6 -isysroot $ISYSROOT $1 $2 $3

      Chmoding this to 755 (executable), I could now build standard C code applications for use on the iPhone. For example, here is a basic helloworld.c example:


      abnev-lpt2:bin abnev$ cat helloworld.c
      #include

      int main() {

      printf("Hello, World!\n");
      return(0);

      }
      abnev-lpt2:bin abnev$ gcc --version
      i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659)
      Copyright (C) 2007 Free Software Foundation, Inc.
      This is free software; see the source for copying conditions. There is NO
      warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

      abnev-lpt2:bin abnev$ gcc2 --version 0 0
      arm-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659)
      Copyright (C) 2007 Free Software Foundation, Inc.
      This is free software; see the source for copying conditions. There is NO
      warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

      abnev-lpt2:bin abnev$ gcc -o helloworld-osx helloworld.c
      abnev-lpt2:bin abnev$ gcc2 -o helloworld-iphone helloworld.c
      abnev-lpt2:bin abnev$ file helloworld-*
      helloworld-iphone: Mach-O executable acorn
      helloworld-osx: Mach-O 64-bit executable
      abnev-lpt2:bin abnev$

      I installed OpenSSH from Cydia and then sftp’ed the helloworld-iphone binary to the phone and I could execute fine. I have had problems thoguh trying to link this set up with other libs for cross-compiling. i.e. compiling libpcap using the gcc shipped with the iPhone SDK.

      Just to note, the gcc line (in the shell script above) evaluates to:


      /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.3.sdk -o helloworld helloworld.c

      I hope this is of some help to you.

      Comment by anev — August 22, 2010 @ 12:11 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: