xref: /llvm-project/lldb/test/API/functionalities/dyld-multiple-rdebug/main.cpp (revision 07c215e8a8af54d0084af7291ac29fef3672fcd8)
1 #include "library_file.h"
2 #include <link.h>
3 #include <stdio.h>
4 // Make a duplicate "_r_debug" symbol that is visible. This is the global
5 // variable name that the dynamic loader uses to communicate changes in shared
6 // libraries that get loaded and unloaded. LLDB finds the address of this
7 // variable by reading the DT_DEBUG entry from the .dynamic section of the main
8 // executable.
9 // What will happen is the dynamic loader will use the "_r_debug" symbol from
10 // itself until the a.out executable gets loaded. At this point the new
11 // "_r_debug" symbol will take precedence over the orignal "_r_debug" symbol
12 // from the dynamic loader and the copy below will get updated with shared
13 // library state changes while the version that LLDB checks in the dynamic
14 // loader stays the same for ever after this.
15 //
16 // When our DYLDRendezvous.cpp tries to check the state in the _r_debug
17 // structure, it will continue to get the last eAdd as the state before the
18 // switch in symbol resolution.
19 //
20 // Before a fix in LLDB, this would mean that we wouldn't ever load any shared
21 // libraries since DYLDRendezvous was waiting to see a eAdd state followed by a
22 // eConsistent state which would trigger the adding of shared libraries, but we
23 // would never see this change because the local copy below is actually what
24 // would get updated. Now if DYLDRendezvous detects two eAdd states in a row,
25 // it will load the shared libraries instead of doing nothing and a log message
26 // will be printed out if "log enable lldb dyld" is active.
27 r_debug _r_debug;
28 
main()29 int main() {
30   library_function(); // Break here
31   return 0;
32 }
33