linking issues when compiling kernel headers into arm binary

Questions and Answers about all things *OS (macOS, iOS, tvOS, watchOS)

linking issues when compiling kernel headers into arm binary

Postby backendbilly » Sun Dec 27, 2015 7:13 am

Hey J,

I'm compiling an arm binary that makes use of kernel headers such as libkern.h. I can compile fine but when it comes time to link, I'm getting the following:

Code: Select all
Undefined symbols for architecture armv7:
  "_find_bytes", referenced from:
      _main in kp-c41f02.o
  "_get_kernel_base", referenced from:
      _main in kp-c41f02.o
  "_write_kernel", referenced from:
      _main in kp-c41f02.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)


my initial thought is that APPL strips kernel symbols when building for arm architectures to avoid having kernel based tools running on iOS. Would love to hear your thoughts.

Thanks

Billy
backendbilly
Site Admin
 
Posts: 132
Joined: Fri May 29, 2015 5:58 pm

Re: linking issues when compiling kernel headers into arm bi

Postby morpheus » Thu Jan 07, 2016 9:41 pm

Need more info here, please? Specifically, show me your #includes and a snippet, please
morpheus
Site Admin
 
Posts: 530
Joined: Thu Apr 11, 2013 6:24 pm

Re: linking issues when compiling kernel headers into arm bi

Postby backendbilly » Thu Jan 14, 2016 4:14 pm

Code: Select all
#include <stdio.h>
#include <string.h>

#include <sys/sysctl.h>     // sysctlbyname

#include "../lib/kernel/libkern.h"


int main(int argc, char** argv)
{
    vm_address_t kbase;

    if (argc < 2) {
        printf("Usage: kpatch new-uuid\n");
        return -1;
    }

    char uuid[0x50];
    size_t size = 0x50;
    memset(uuid, 0, size);
    int ret = sysctlbyname("kern.uuid", uuid, &size, NULL, 0);
    printf("[*] uuid: %s\n", uuid);

    if((kbase = get_kernel_base()) == 0) {
        printf("[!] failed to get the kernel base address");
        return -1;
    }

    vm_address_t uuid_addr = find_bytes(kbase, kbase + 0x1000000, (unsigned char*)uuid, strlen(uuid));
    if (uuid_addr == 0) {
        printf("[!] failed to find the uuid in kernel memory\n");
        return -1;
    }
    printf("[*] found uuid at 0x" ADDR "\n", uuid_addr);

    write_kernel(uuid_addr, (unsigned char*)argv[1], strlen(argv[1])+1);

    printf("[*] done, check \"sysctl kern.uuid\"\n");

    return 0;
}
backendbilly
Site Admin
 
Posts: 132
Joined: Fri May 29, 2015 5:58 pm

Re: linking issues when compiling kernel headers into arm bi

Postby backendbilly » Mon Feb 01, 2016 11:52 am

To make things clearer, this is what I'm trying to do:

I'm looking at the iOS kernel utilities found on:

https://github.com/saelo/ios-kern-utils


trying to build the tool, this is the output I'm getting:

Determining if the C compiler works failed with the following output:
Change Dir: /Users/backendbilly/Documents/other/ios-kern-utils/build/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/make" "cmTC_2a59a/fast"
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/cmTC_2a59a.dir/build.make CMakeFiles/cmTC_2a59a.dir/build
Building C object CMakeFiles/cmTC_2a59a.dir/testCCompiler.c.o
/usr/bin/clang -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.2.sdk -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -o CMakeFiles/cmTC_2a59a.dir/testCCompiler.c.o -c /Users/backendbilly/Documents/other/ios-kern-utils/build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_2a59a
/usr/local/Cellar/cmake/3.4.3/bin/cmake -E cmake_link_script CMakeFiles/cmTC_2a59a.dir/link.txt --verbose=1
/usr/bin/clang -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.2.sdk -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/cmTC_2a59a.dir/testCCompiler.c.o -o cmTC_2a59a
ld: warning: ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/lib/libSystem.tbd, missing required architecture arm64 in file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/lib/libSystem.tbdignoring file /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a, missing required architecture arm64 in file /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a (3 slices)

ld: dynamic main executables must link with libSystem.dylib for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [cmTC_2a59a] Error 1
make: *** [cmTC_2a59a/fast] Error 2


Billy
backendbilly
Site Admin
 
Posts: 132
Joined: Fri May 29, 2015 5:58 pm

Re: linking issues when compiling kernel headers into arm bi

Postby Siguza » Thu Feb 04, 2016 11:59 pm

The original error you got is due to not including the *.c files in lib.

FWIW, I forked my own copy and ported it (back) to make because I don't like cmake.
Mac support only for now, but you appear to be on Mac, so that should not be a problem.
User avatar
Siguza
Unicorn
 
Posts: 158
Joined: Thu Jan 28, 2016 10:38 am

Re: linking issues when compiling kernel headers into arm bi

Postby backendbilly » Fri Feb 05, 2016 3:18 am

Thanks Siguza. I also don't like cmake. I'll give it a try. The errors threw me off when I started getting link errors thinking I'm missing libraries to link with.
backendbilly
Site Admin
 
Posts: 132
Joined: Fri May 29, 2015 5:58 pm

Re: linking issues when compiling kernel headers into arm bi

Postby backendbilly » Fri Feb 05, 2016 4:02 am

Thanks Siguza. Worked like a charm.
backendbilly
Site Admin
 
Posts: 132
Joined: Fri May 29, 2015 5:58 pm

Re: linking issues when compiling kernel headers into arm bi

Postby Siguza » Fri Feb 05, 2016 2:54 pm

You're welcome.

My first instinct when encountering unresolved symbols is to grep around, and this time the missing link turned up quite soon in the lib folder.
User avatar
Siguza
Unicorn
 
Posts: 158
Joined: Thu Jan 28, 2016 10:38 am


Return to Questions and Answers

Who is online

Users browsing this forum: No registered users and 2 guests

cron