1/*
2 Copyright (c) 2005-2019 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17enum FOO_TYPE {
18 FOO_DUMMY,
19 FOO_IMPLEMENTATION
20};
21
22#if _WIN32 || _WIN64
23#define TEST_EXPORT
24#else
25#define TEST_EXPORT extern "C"
26#endif /* _WIN32 || _WIN64 */
27
28// foo "implementations".
29TEST_EXPORT FOO_TYPE foo1() { return FOO_IMPLEMENTATION; }
30TEST_EXPORT FOO_TYPE foo2() { return FOO_IMPLEMENTATION; }
31// foo "dummies".
32FOO_TYPE dummy_foo1() { return FOO_DUMMY; }
33FOO_TYPE dummy_foo2() { return FOO_DUMMY; }
34
35// Handlers.
36static FOO_TYPE (*foo1_handler)() = &dummy_foo1;
37static FOO_TYPE (*foo2_handler)() = &dummy_foo2;
38
39#include "tbb/tbb_config.h"
40// Suppress the weak symbol mechanism to avoid surplus compiler warnings.
41#ifdef __TBB_WEAK_SYMBOLS_PRESENT
42#undef __TBB_WEAK_SYMBOLS_PRESENT
43#endif
44// Use of harness assert to avoid the dependency on TBB
45#include "harness_assert.h"
46#define LIBRARY_ASSERT(p,message) ASSERT(p,message)
47#include "tbb/dynamic_link.h"
48// Table describing how to link the handlers.
49static const tbb::internal::dynamic_link_descriptor LinkTable[] = {
50 { "foo1", (tbb::internal::pointer_to_handler*)(void*)(&foo1_handler) },
51 { "foo2", (tbb::internal::pointer_to_handler*)(void*)(&foo2_handler) }
52};
53
54// The direct include since we want to test internal functionality.
55#include "tbb/dynamic_link.cpp"
56#include "harness_dynamic_libs.h"
57#include "harness.h"
58
59#if !HARNESS_SKIP_TEST
60int TestMain () {
61#if !_WIN32
62 // Check if the executable exports its symbols.
63 ASSERT( Harness::GetAddress( Harness::OpenLibrary(NULL), "foo1" ) && Harness::GetAddress( Harness::OpenLibrary(NULL), "foo2" ),
64 "The executable doesn't export its symbols. Is the -rdynamic switch set during linking?" );
65#endif /* !_WIN32 */
66 // We want to link (or fail to link) to the symbols available from the
67 // executable so it doesn't matter what the library name is specified in
68 // the dynamic_link call - let it be an empty string.
69 // Generally speaking the test has sense only on Linux but on Windows it
70 // checks the dynamic_link graceful behavior with incorrect library name.
71 if ( tbb::internal::dynamic_link( "", LinkTable, sizeof(LinkTable)/sizeof(LinkTable[0]) ) ) {
72 ASSERT( foo1_handler && foo2_handler, "The symbols are corrupted by dynamic_link" );
73 ASSERT( foo1_handler() == FOO_IMPLEMENTATION && foo2_handler() == FOO_IMPLEMENTATION,
74 "dynamic_link returned the successful code but symbol(s) are wrong" );
75 } else {
76 ASSERT( foo1_handler==dummy_foo1 && foo2_handler==dummy_foo2, "The symbols are corrupted by dynamic_link" );
77 }
78 return Harness::Done;
79}
80#endif // HARNESS_SKIP_TEST
81