LibCSymTable.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2008, Yahoo! Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "LibCSymTable.h"
  17. #define LOAD_SYM(sym) \
  18. sym=(sym##_sig)dlsym(handle,#sym); \
  19. assert("Unable to load "#sym" from libc"&&sym)
  20. LibCSymTable& LibCSymTable::instance(){
  21. static LibCSymTable tbl;
  22. return tbl;
  23. }
  24. //******************************************************************************
  25. // preload original libc symbols
  26. LibCSymTable::LibCSymTable()
  27. {
  28. void* handle=getHandle();
  29. LOAD_SYM(gethostbyname);
  30. LOAD_SYM(calloc);
  31. LOAD_SYM(realloc);
  32. LOAD_SYM(free);
  33. LOAD_SYM(random);
  34. LOAD_SYM(srandom);
  35. LOAD_SYM(printf);
  36. LOAD_SYM(socket);
  37. LOAD_SYM(close);
  38. LOAD_SYM(getsockopt);
  39. LOAD_SYM(setsockopt);
  40. LOAD_SYM(fcntl);
  41. LOAD_SYM(connect);
  42. LOAD_SYM(send);
  43. LOAD_SYM(recv);
  44. LOAD_SYM(select);
  45. LOAD_SYM(poll);
  46. LOAD_SYM(gettimeofday);
  47. #ifdef THREADED
  48. LOAD_SYM(pthread_create);
  49. LOAD_SYM(pthread_detach);
  50. LOAD_SYM(pthread_cond_broadcast);
  51. LOAD_SYM(pthread_cond_destroy);
  52. LOAD_SYM(pthread_cond_init);
  53. LOAD_SYM(pthread_cond_signal);
  54. LOAD_SYM(pthread_cond_timedwait);
  55. LOAD_SYM(pthread_cond_wait);
  56. LOAD_SYM(pthread_join);
  57. LOAD_SYM(pthread_mutex_destroy);
  58. LOAD_SYM(pthread_mutex_init);
  59. LOAD_SYM(pthread_mutex_lock);
  60. LOAD_SYM(pthread_mutex_trylock);
  61. LOAD_SYM(pthread_mutex_unlock);
  62. #endif
  63. }
  64. void* LibCSymTable::getHandle(){
  65. static void* handle=0;
  66. if(!handle){
  67. #ifdef __CYGWIN__
  68. handle=dlopen("cygwin1.dll",RTLD_LAZY);
  69. assert("Unable to dlopen global sym table"&&handle);
  70. #else
  71. handle=RTLD_NEXT;
  72. #endif
  73. }
  74. return handle;
  75. }