LibCSymTable.cc 2.5 KB

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