Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uintptr_t LocateDataNullZone( const char* moduleName, const unsigned int numVMs )
- {
- static std::vector<dl_phdr_info> libraries;
- if ( libraries.empty() ) {
- dl_iterate_phdr( []( struct dl_phdr_info* info, size_t, void* ) {
- libraries.push_back( *info );
- return 0;
- }, nullptr );
- }
- size_t page = sysconf(_SC_PAGESIZE); // ~4096
- /* Locate Data section/s for this module */
- for ( const dl_phdr_info& current : libraries ) {
- if ( !current.dlpi_name )
- continue;
- if ( !strcasestr( current.dlpi_name, moduleName ) )
- continue;
- for( int i = 0; i < current.dlpi_phnum; i++ ){
- if( !( current.dlpi_phdr[i].p_flags & ( PF_R | PF_W ) ) ) // not Readable and writable? Keep going...
- continue;
- uintptr_t address = current.dlpi_addr + current.dlpi_phdr[i].p_vaddr;
- size_t len = current.dlpi_phdr[i].p_memsz;
- /* Start at beginning of page */
- if( address % page ){
- len += address % page;
- address -= address %page;
- }
- /* Make sure to get the extra bits at the end. */
- if( len % page ){
- len += page - (len % page);
- }
- if( !current.dlpi_phdr[i].p_vaddr )
- continue;
- uintptr_t endAddress = address + len - sizeof(uintptr_t);
- LogLocal("Found a .data segment at: %p-%p\n", (void*)address, (void*)endAddress);
- for( uintptr_t currentAddr = address; currentAddr < endAddress; currentAddr += sizeof(uintptr_t) ) {
- if( *reinterpret_cast<uintptr_t*>( currentAddr ) == 0 ){
- bool isNullZone = true;
- size_t ptrCount = 0;
- for( ; ptrCount < numVMs && isNullZone; ptrCount += sizeof(uintptr_t) ){
- if( *reinterpret_cast<uintptr_t*>( currentAddr + ptrCount ) != 0 )
- isNullZone = false;
- }
- if( isNullZone && ptrCount >= numVMs )
- return currentAddr;
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement