Changeset ed8b809


Ignore:
Timestamp:
06/02/15 18:53:15 (9 years ago)
Author:
Hal Finkel <hfinkel@…>
Branches:
master
Children:
4c51931
Parents:
6444e03
git-author:
Hal Finkel <hfinkel@…> (06/02/15 18:53:15)
git-committer:
Hal Finkel <hfinkel@…> (06/02/15 18:53:15)
Message:

fixup the wrapping of mmap, etc.

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • README

    r192a260 red8b809  
    1818For statically-linked applications, add the following to your linker flags: 
    1919 
    20   -Wl,--wrap,malloc,--wrap,free,--wrap,realloc,--wrap,calloc,--wrap,memalign \ 
     20  -Wl,--wrap,malloc,--wrap,valloc,--wrap,realloc,--wrap,calloc,--wrap,memalign,--wrap,free \ 
     21    -Wl,--wrap,posix_memalign,--wrap,mmap,--wrap,mmap64,--wrap,munmap \ 
    2122    /path/to/memlog/memlog_s.o -lpthread -ldl 
    2223 
  • memlog.cpp

    r6444e03 red8b809  
    5454void *initial_brk = 0; 
    5555 
    56 #ifdef __PIC__ 
    57 typedef int (*__real_posix_memalign_t)(void **memptr, size_t alignment, 
    58                                        size_t size); 
    59 __real_posix_memalign_t __real_posix_memalign = 0; 
    60 #else 
    61 extern "C" { 
    62 extern int __real_posix_memalign(void **memptr, size_t alignment, size_t size); 
    63 } 
    64 #endif 
    65  
    6656__attribute__((__constructor__)) 
    6757static void record_init() { 
     
    255245} 
    256246 
     247#ifdef __PIC__ 
     248int (*__real_posix_memalign)(void **memptr, size_t alignment, size_t size) = 0; 
     249 
     250void *(*__real_mmap)(void *addr, size_t length, int prot, int flags, 
     251                     int fd, off_t offset) = 0; 
     252void *(*__real_mmap64)(void *addr, size_t length, int prot, int flags, 
     253                       int fd, off64_t offset) = 0; 
     254int (*__real_munmap)(void *addr, size_t length) = 0; 
     255#else 
     256extern "C" { 
     257extern int __real_posix_memalign(void **memptr, size_t alignment, size_t size); 
     258 
     259extern void *__real_mmap(void *addr, size_t length, int prot, int flags, 
     260                         int fd, off_t offset); 
     261extern void *__real_mmap64(void *addr, size_t length, int prot, int flags, 
     262                           int fd, off64_t offset); 
     263extern int __real_munmap(void *addr, size_t length); 
     264} 
     265#endif 
     266 
    257267// glibc exports its underlying malloc implementation under the name 
    258268// __libc_malloc so that hooks like this can use it. 
     
    265275extern void __libc_free(void *ptr); 
    266276 
    267 extern void *__mmap(void *addr, size_t length, int prot, int flags, 
    268                     int fd, off_t offset); 
    269 extern void *__mmap64(void *addr, size_t length, int prot, int flags, 
    270                       int fd, off64_t offset); 
    271 extern int __munmap(void *addr, size_t length); 
    272  
    273277#ifdef __PIC__ 
    274278#define FUNC(x) x 
     
    392396#ifdef __PIC__ 
    393397  if (!__real_posix_memalign) 
    394     if (!(__real_posix_memalign = 
    395         (__real_posix_memalign_t) dlsym(RTLD_NEXT, "posix_memalign"))) 
    396       return -1; 
     398    if (!(*(void **) (&__real_posix_memalign) = 
     399        dlsym(RTLD_NEXT, "posix_memalign"))) { 
     400      return ELIBACC; 
     401    } 
    397402#endif 
    398403 
     
    417422    __builtin_extract_return_addr(__builtin_return_address(0)); 
    418423 
    419   if (in_malloc) 
    420     return __mmap(addr, length, prot, flags, fd, offset); 
    421  
    422   in_malloc = 1; 
    423  
    424   void *ptr = __mmap(addr, length, prot, flags, fd, offset); 
     424#ifdef __PIC__ 
     425  if (!__real_mmap) 
     426    if (!(*(void **) (&__real_mmap) = dlsym(RTLD_NEXT, "mmap"))) { 
     427      errno = ELIBACC; 
     428      return MAP_FAILED; 
     429    } 
     430#endif 
     431 
     432  if (in_malloc) 
     433    return __real_mmap(addr, length, prot, flags, fd, offset); 
     434 
     435  in_malloc = 1; 
     436 
     437  void *ptr = __real_mmap(addr, length, prot, flags, fd, offset); 
    425438 
    426439  if (ptr != MAP_FAILED) 
     
    437450    __builtin_extract_return_addr(__builtin_return_address(0)); 
    438451 
    439   if (in_malloc) 
    440     return __mmap64(addr, length, prot, flags, fd, offset); 
    441  
    442   in_malloc = 1; 
    443  
    444   void *ptr = __mmap64(addr, length, prot, flags, fd, offset); 
     452#ifdef __PIC__ 
     453  if (!__real_mmap64) 
     454    if (!(*(void **) (&__real_mmap64) = dlsym(RTLD_NEXT, "mmap64"))) { 
     455      errno = ELIBACC; 
     456      return MAP_FAILED; 
     457    } 
     458#endif 
     459 
     460  if (in_malloc) 
     461    return __real_mmap64(addr, length, prot, flags, fd, offset); 
     462 
     463  in_malloc = 1; 
     464 
     465  void *ptr = __real_mmap64(addr, length, prot, flags, fd, offset); 
    445466 
    446467  if (ptr != MAP_FAILED) 
     
    456477    __builtin_extract_return_addr(__builtin_return_address(0)); 
    457478 
    458   if (in_malloc) 
    459     return __munmap(addr, length); 
     479#ifdef __PIC__ 
     480  if (!__real_munmap) 
     481    if (!(*(void **) (&__real_munmap) = dlsym(RTLD_NEXT, "munmap"))) { 
     482      errno = ELIBACC; 
     483      return -1; 
     484    } 
     485#endif 
     486 
     487  if (in_malloc) 
     488    return __real_munmap(addr, length); 
    460489 
    461490  in_malloc = 1; 
     
    463492  record_free(addr, caller); 
    464493 
    465   int r = __munmap(addr, length); 
     494  int r = __real_munmap(addr, length); 
    466495 
    467496  in_malloc = 0; 
Note: See TracChangeset for help on using the changeset viewer.