1// Copyright 2014 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 (libzoo.so) with the linker.
7// - Find the address of the "Zoo" function in libzoo.so.
8// - Call the Zoo() function, which will use dlopen() / dlsym() to
9//   find libbar.so (which depends on libfoo.so).
10// - Close the library.
11
12// This tests the dlopen/dlsym/dlclose wrappers provided by the crazy
13// linker to loaded libraries.
14
15#include <stdio.h>
16#include <crazy_linker.h>
17
18#include "test_util.h"
19
20typedef bool (*FunctionPtr)();
21
22int main() {
23  crazy_context_t* context = crazy_context_create();
24  crazy_library_t* library;
25
26  // Load libzoo.so
27  if (!crazy_library_open(&library, "libzoo.so", context)) {
28    Panic("Could not open library: %s\n", crazy_context_get_error(context));
29  }
30
31  // Find the "Zoo" symbol.
32  FunctionPtr zoo_func;
33  if (!crazy_library_find_symbol(
34           library, "Zoo", reinterpret_cast<void**>(&zoo_func))) {
35    Panic("Could not find 'Zoo' in libzoo.so\n");
36  }
37
38  // Call it.
39  bool ret = (*zoo_func)();
40  if (!ret)
41    Panic("'Zoo' function failed!");
42
43  // Close the library.
44  printf("Closing libzoo.so\n");
45  crazy_library_close(library);
46
47  crazy_context_destroy(context);
48
49  return 0;
50}