OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // A crazy linker test to: |
| 6 // - Load a library (libbar.so) with the linker, which depends on |
| 7 // another library (libfoo.so) |
| 8 // - Find the address of the "Bar" function in libbar.so. |
| 9 // - Call the Bar() function, which ends up calling Foo() in libfoo.so |
| 10 // - Close the library. |
| 11 |
| 12 #include <crazy_linker.h> |
| 13 |
| 14 #include <stdarg.h> |
| 15 #include <stdio.h> |
| 16 #include <stdlib.h> |
| 17 |
| 18 typedef void (*FunctionPtr)(); |
| 19 |
| 20 static void Panic(const char* fmt, ...) { |
| 21 va_list args; |
| 22 fprintf(stderr, "PANIC: "); |
| 23 va_start(args, fmt); |
| 24 vfprintf(stderr, fmt, args); |
| 25 va_end(args); |
| 26 exit(1); |
| 27 } |
| 28 |
| 29 int main() { |
| 30 crazy_context_t* context = crazy_context_create(); |
| 31 crazy_library_t* library; |
| 32 |
| 33 // DEBUG |
| 34 crazy_context_set_load_address(context, 0x20000000); |
| 35 |
| 36 // Load libbar.so |
| 37 if (!crazy_library_open(&library, |
| 38 "libbar.so", |
| 39 context)) { |
| 40 Panic("Could not open library: %s\n", crazy_context_get_error(context)); |
| 41 } |
| 42 |
| 43 // Find the "Bar" symbol. |
| 44 FunctionPtr bar_func; |
| 45 if (!crazy_library_find_symbol(library, |
| 46 "Bar", |
| 47 reinterpret_cast<void**>(&bar_func))) { |
| 48 Panic("Could not find 'Bar' in libbar.so\n"); |
| 49 } |
| 50 |
| 51 // Call it. |
| 52 (*bar_func)(); |
| 53 |
| 54 // Find the "Foo" symbol from libbar.so |
| 55 FunctionPtr foo_func; |
| 56 if (!crazy_library_find_symbol(library, |
| 57 "Foo", |
| 58 reinterpret_cast<void**>(&foo_func))) { |
| 59 Panic("Could not find 'Foo' from libbar.so\n"); |
| 60 } |
| 61 |
| 62 // Close the library. |
| 63 printf("Closing libbar.so\n"); |
| 64 crazy_library_close(library); |
| 65 |
| 66 crazy_context_destroy(context); |
| 67 |
| 68 return 0; |
| 69 } |
OLD | NEW |