99#include < fcntl.h>
1010#include < unistd.h>
1111#include < inttypes.h>
12+ #include < stdlib.h>
1213#include < stdio.h>
1314#include < string.h>
1415#include < assert.h>
2526#include " minipal.h"
2627#include " minipal/cpufeatures.h"
2728
29+ #ifndef TARGET_APPLE
30+ #include < link.h>
31+ #include < dlfcn.h>
32+ #endif // TARGET_APPLE
33+
2834#ifdef TARGET_APPLE
2935
3036#include < mach/mach.h>
@@ -275,26 +281,32 @@ struct InitializeTemplateThunkLocals
275281
276282static TemplateThunkMappingData *s_pThunkData = NULL ;
277283
284+ static Elf32_Word Elf32_WordMin (Elf32_Word left, Elf32_Word right)
285+ {
286+ return left < right ? left : right;
287+ }
288+
278289static int InitializeTemplateThunkMappingDataPhdrCallback (struct dl_phdr_info *info, size_t size, void *dataPtr)
279290{
280291 InitializeTemplateThunkLocals *locals = (InitializeTemplateThunkLocals*)dataPtr;
281292
282- if (info->dlpi_addr == locals->info .dli_fbase )
293+ if (( void *) info->dlpi_addr == locals->info .dli_fbase )
283294 {
284295 for (size_t j = 0 ; j < info->dlpi_phnum ; j++)
285296 {
286- if (locals.pTemplate < info->dlpi_phdr [j].p_vaddr )
297+ uint8_t * baseSectionAddr = (uint8_t *)locals->info .dli_fbase + info->dlpi_phdr [j].p_vaddr ;
298+ if (locals->pTemplate < baseSectionAddr)
287299 {
288300 // Address is before the virtual address of this section begins
289301 continue ;
290302 }
291303
292304 // Since this is all in support of mapping code from the file, we need to ensure that the region we find
293305 // is actually present in the file.
294- Elf32_Word sizeOfSectionWhichCanBeMapped = min (info->dlpi_phdr [j].p_filesz , info->dlpi_phdr [j].p_memsz ;
306+ Elf32_Word sizeOfSectionWhichCanBeMapped = Elf32_WordMin (info->dlpi_phdr [j].p_filesz , info->dlpi_phdr [j].p_memsz ) ;
295307
296- Elf32_Addr endAddressAllowedForTemplate = info-> dlpi_phdr [j]. p_vaddr + sizeOfSectionWhichCanBeMapped) ;
297- if (locals. pTemplate >= endAddressAllowedForTemplate)
308+ uint8_t * endAddressAllowedForTemplate = baseSectionAddr + sizeOfSectionWhichCanBeMapped;
309+ if (locals-> pTemplate >= endAddressAllowedForTemplate)
298310 {
299311 // Template is after the virtual address of this section ends (or the mappable region of the file)
300312 continue ;
@@ -316,8 +328,8 @@ static int InitializeTemplateThunkMappingDataPhdrCallback(struct dl_phdr_info *i
316328
317329 locals->data .fdImage = fdImage;
318330 locals->data .offsetInFileOfStartOfSection = info->dlpi_phdr [j].p_offset ;
319- locals->data .addrOfStartOfSection = info-> dlpi_phdr [j]. p_vaddr ;
320- locals->data .addrOfEndOfSection = info-> dlpi_phdr [j]. p_vaddr + sizeOfSectionWhichCanBeMapped;
331+ locals->data .addrOfStartOfSection = baseSectionAddr ;
332+ locals->data .addrOfEndOfSection = baseSectionAddr + sizeOfSectionWhichCanBeMapped;
321333 locals->data .imageTemplates = true ;
322334 return 1 ; // We have found the result. Abort further processing.
323335 }
@@ -343,7 +355,7 @@ TemplateThunkMappingData *InitializeTemplateThunkMappingData(void* pTemplate)
343355 dl_iterate_phdr (InitializeTemplateThunkMappingDataPhdrCallback, &locals);
344356 }
345357
346- if (locals.data -> addrOfStartOfSection == NULL )
358+ if (locals.data . addrOfStartOfSection == NULL )
347359 {
348360 // This is the detail of thunk data which indicates if we were able to compute the template mapping data from the image.
349361
@@ -378,10 +390,10 @@ TemplateThunkMappingData *InitializeTemplateThunkMappingData(void* pTemplate)
378390 else
379391 {
380392 locals.data .fdImage = fd;
381- locals-> data .offsetInFileOfStartOfSection = 0 ;
382- locals-> data .addrOfStartOfSection = (void *)0x10000 ;
383- locals-> data .addrOfEndOfSection = ((uint8_t *)locals-> data .addrOfStartOfSection ) + maxFileSize;
384- locals-> data .imageTemplates = false ;
393+ locals. data .offsetInFileOfStartOfSection = 0 ;
394+ locals. data .addrOfStartOfSection = (void *)0x10000 ;
395+ locals. data .addrOfEndOfSection = ((uint8_t *)locals. data .addrOfStartOfSection ) + maxFileSize;
396+ locals. data .imageTemplates = false ;
385397 }
386398 }
387399 }
@@ -440,8 +452,8 @@ void* VMToOSInterface::CreateTemplate(void* pImageTemplate, size_t templateSize,
440452 void * mappedMemory = mmap (NULL , templateSize, PROT_READ | PROT_WRITE, MAP_SHARED, pThunkData->fdImage , locationInFileToStoreGeneratedCode);
441453 if (mappedMemory != MAP_FAILED)
442454 {
443- codePageGenerator (mappedMemory, mappedMemory, templateSize);
444- munmap (mappedMemory);
455+ codePageGenerator (( uint8_t *) mappedMemory, ( uint8_t *) mappedMemory, templateSize);
456+ munmap (mappedMemory, templateSize );
445457 return ((uint8_t *)pThunkData->addrOfStartOfSection ) + locationInFileToStoreGeneratedCode;
446458 }
447459 else
@@ -511,13 +523,13 @@ void* VMToOSInterface::AllocateThunksFromTemplate(void* pTemplate, size_t templa
511523 }
512524
513525 uint8_t * endOfTemplate = ((uint8_t *)pTemplate + templateSize);
514- if (endOfTemplate > addrOfEndOfSection)
526+ if (endOfTemplate > pThunkData-> addrOfEndOfSection )
515527 return NULL ;
516528
517- size_t sectionOffset = fileOffset = (uint8_t *)pTemplate - (uint8_t *)pThunkData->addrOfStartOfSection ;
529+ size_t sectionOffset = (uint8_t *)pTemplate - (uint8_t *)pThunkData->addrOfStartOfSection ;
518530 off_t fileOffset = pThunkData->offsetInFileOfStartOfSection + sectionOffset;
519531
520- void *pStart = mmap (pStartHint , templateSize * 2 , PROT_READ | PROT_WRITE, MAP_PRIVATE | (pStartSpecification != NULL ? MAP_FIXED : 0 ), 0 , 0 );
532+ void *pStart = mmap (pStartSpecification , templateSize * 2 , PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | (pStartSpecification != NULL ? MAP_FIXED : 0 ), - 1 , 0 );
521533 if (pStart == MAP_FAILED)
522534 {
523535 return NULL ;
0 commit comments