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