-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathcheck_addr_of_weak_undefined_funcs.cpp
118 lines (94 loc) · 3.01 KB
/
check_addr_of_weak_undefined_funcs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
Gabriel Staples
9 Apr. 2021
check_addr_of_weak_undefined_funcs.cpp
- Prove that gcc `weak` functions have a nil (zero) address if they are declared but not defined!
This is how Arduino implements the weak and strong `serialEvent()` functions and calls to that
function! Very ingenious!
- See my answer here for details and links to the Arduino source code of interest:
https://stackoverflow.com/a/67016917/4561887
To compile and run:
mkdir -p bin && g++ -Wall -Werror -Wextra -ggdb -std=c++17 -o bin/check_addr_of_weak_undefined_funcs \
check_addr_of_weak_undefined_funcs.cpp && bin/check_addr_of_weak_undefined_funcs
References:
1. My answer: https://stackoverflow.com/a/67016917/4561887
*/
// C includes
#include <stdio.h>
// C++ includes
#include <iostream>
// using namespace std;
int get2()
{
return 2;
}
int get3()
{
return 3;
}
// Func definition
void doNothing1() {};
// Func declarations ONLY, NOT definitions
void doNothing2() __attribute__((weak));
void doNothing3() __attribute__((weak));
void doNothing4() __attribute__((weak));
// Strong func definition for the weak prototype (declaration) above
void doNothing4() {};
/// Check to see if a function address is zero
void checkForNilFuncAddress(const char* func_string, uint64_t func_addr)
{
if (func_addr)
{
printf("%s = 0x%lx\n", func_string, func_addr);
}
else
{
printf("%s = 0x%lx (nil, or zero)\n", func_string, func_addr);
}
}
int main()
{
// C-style print
printf("Hello World1\n");
// C++-style print
std::cout << "Hello world2" << std::endl;
std::cout << "\n";
printf("&get2 = %p\n"
"&get3 = %p\n"
"&doNothing1 = %p\n"
"&doNothing2 = %p\n"
"&doNothing3 = %p\n"
"&doNothing4 = %p\n\n",
&get2,
&get3,
&doNothing1,
&doNothing2,
&doNothing3,
&doNothing4);
checkForNilFuncAddress("&get2", (uint64_t)&get2);
checkForNilFuncAddress("&get3", (uint64_t)&get3);
checkForNilFuncAddress("&doNothing1", (uint64_t)&doNothing1);
checkForNilFuncAddress("&doNothing2", (uint64_t)&doNothing2);
checkForNilFuncAddress("&doNothing3", (uint64_t)&doNothing3);
checkForNilFuncAddress("&doNothing4", (uint64_t)&doNothing4);
return 0;
}
/*
Sample Output:
eRCaGuy_hello_world/cpp$ mkdir -p bin && g++ -Wall -Werror -Wextra -ggdb -std=c++17 -o bin/check_addr_of_weak_undefined_funcs check_addr_of_weak_undefined_funcs.cpp && bin/check_addr_of_weak_undefined_funcs
Hello World1
Hello world2
&get2 = 0x5653b20819da
&get3 = 0x5653b20819e5
&doNothing1 = 0x5653b20819f0
&doNothing2 = (nil)
&doNothing3 = (nil)
&doNothing4 = 0x5653b20819f7
&get2 = 0x5653b20819da
&get3 = 0x5653b20819e5
&doNothing1 = 0x5653b20819f0
&doNothing2 = 0x0 (nil, or zero)
&doNothing3 = 0x0 (nil, or zero)
&doNothing4 = 0x5653b20819f7
*/