33 lines · cpp
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 global5// variable name that the dynamic loader uses to communicate changes in shared6// libraries that get loaded and unloaded. LLDB finds the address of this7// variable by reading the DT_DEBUG entry from the .dynamic section of the main8// executable.9// What will happen is the dynamic loader will use the "_r_debug" symbol from10// itself until the a.out executable gets loaded. At this point the new11// "_r_debug" symbol will take precedence over the orignal "_r_debug" symbol12// from the dynamic loader and the copy below will get updated with shared13// library state changes while the version that LLDB checks in the dynamic14// loader stays the same for ever after this.15//16// When our DYLDRendezvous.cpp tries to check the state in the _r_debug17// structure, it will continue to get the last eAdd as the state before the18// switch in symbol resolution.19//20// Before a fix in LLDB, this would mean that we wouldn't ever load any shared21// libraries since DYLDRendezvous was waiting to see a eAdd state followed by a22// eConsistent state which would trigger the adding of shared libraries, but we23// would never see this change because the local copy below is actually what24// 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 message26// will be printed out if "log enable lldb dyld" is active.27r_debug _r_debug;28 29int main() {30 library_function(); // Break here31 return 0;32}33