Debian Codegraph @

tests/manual/test_linkage.c

raw symbols SCIP
1 /*
2 SPDX-FileCopyrightText: 2018 Ivan Middleton <TODO>
3 SPDX-FileCopyrightText: 2018 Milian Wolff <[email protected]>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7
8 /*
9 By default, the linker uses lazy binding (function calls aren't
10 resolved until the first time the function is called).
11
12 Relevant sections of the executable for lazy binding:
13 .plt (trampoline code)
14 .got.plt (function addresses cached here)
15 .rela.plt (relocation entries associating each function name
16 with its storage location in .got.plt)
17
18 But symbols can also be bound right away when the executable or
19 shared library is started or loaded.
20
21 Relevant sections for immediate binding:
22 .plt.got (trampoline code)
23 .got (function addresses stored here)
24 .rela.dyn (relocation entries)
25
26 Immediate binding can be triggered in a couple different ways:
27
28 (1) The linker option "-z now" makes all symbols use immediate
29 binding. Compile this file as follows to see this in action:
30
31 gcc -Wl,-z,now -o testbind testbind.c
32
33 Why might this linker option be used? See:
34
35 https://wiki.debian.org/Hardening#DEB_BUILD_HARDENING_BINDNOW_.28ld_-z_now.29
36
37 Note that this seems to be platform dependant and is not always reproducible.
38
39 (2) If a particular function has a pointer to it passed around,
40 then it must be bound immediately. Define TAKE_ADDR, i.e. compile with
41
42 gcc -g -O0 -fuse-ld=bfd -DTAKE_ADDR -o testbind testbind.c
43
44 to see this behavior. Note that ld.gold does not show this behavior.
45
46 The heaptrack_inject function needs to look in both .rela.plt
47 (DT_JMPREL) and .rela.dyn (DT_RELA) in order to find all
48 malloc/free function pointers, lazily-bound or no.
49
50 There is also another option which is currently not handled by heaptrack:
51 When do not rewrite data segments, which would be required to catch accessing
52 a given symbol through a function pointer (-DUSE_FREEPTR).
53
54 Use the run_linkage_tests.sh bash script to check the behavior in an automated fashion.
55 */
56
57 #include <stdlib.h>
58 #include <unistd.h>
59
60 #include <benchutil.h>
61
62 int main()
63 {
64 // NOTE: if we'd read the free ptr here, then we would not catch/override the value by heaptrack
65 int i = 0;
66
67 sleep(1);
68
69 for (i = 0; i < 10; ++i) {
70 void* foo = malloc(256);
71 escape(foo);
72
73 #if defined(TAKE_ADDR) || defined(USE_FREEPTR)
74 // but when we read it here, heaptrack may have rewritten the value properly
75 void (*freePtr)(void*) = &free;
76
77 escape(freePtr);
78 #endif
79
80 usleep(200);
81 #if defined(USE_FREEPTR)
82 freePtr(foo);
83 #else
84 free(foo);
85 #endif
86 usleep(200);
87 }
88 return 0;
89 }