LibCSymTable.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #define LOAD_SYM(sym) \
  20. sym=(sym##_sig)dlsym(handle,#sym); \
  21. assert("Unable to load "#sym" from libc"&&sym)
  22. LibCSymTable& LibCSymTable::instance(){
  23. static LibCSymTable tbl;
  24. return tbl;
  25. }
  26. //******************************************************************************
  27. // preload original libc symbols
  28. LibCSymTable::LibCSymTable()
  29. {
  30. void* handle=getHandle();
  31. LOAD_SYM(gethostbyname);
  32. LOAD_SYM(calloc);
  33. LOAD_SYM(realloc);
  34. LOAD_SYM(free);
  35. LOAD_SYM(random);
  36. LOAD_SYM(srandom);
  37. LOAD_SYM(printf);
  38. LOAD_SYM(socket);
  39. LOAD_SYM(close);
  40. LOAD_SYM(getsockopt);
  41. LOAD_SYM(setsockopt);
  42. LOAD_SYM(fcntl);
  43. LOAD_SYM(connect);
  44. LOAD_SYM(send);
  45. LOAD_SYM(recv);
  46. LOAD_SYM(select);
  47. LOAD_SYM(poll);
  48. LOAD_SYM(gettimeofday);
  49. #ifdef THREADED
  50. LOAD_SYM(pthread_create);
  51. LOAD_SYM(pthread_detach);
  52. LOAD_SYM(pthread_cond_broadcast);
  53. LOAD_SYM(pthread_cond_destroy);
  54. LOAD_SYM(pthread_cond_init);
  55. LOAD_SYM(pthread_cond_signal);
  56. LOAD_SYM(pthread_cond_timedwait);
  57. LOAD_SYM(pthread_cond_wait);
  58. LOAD_SYM(pthread_join);
  59. LOAD_SYM(pthread_mutex_destroy);
  60. LOAD_SYM(pthread_mutex_init);
  61. LOAD_SYM(pthread_mutex_lock);
  62. LOAD_SYM(pthread_mutex_trylock);
  63. LOAD_SYM(pthread_mutex_unlock);
  64. #endif
  65. }
  66. void* LibCSymTable::getHandle(){
  67. static void* handle=0;
  68. if(!handle){
  69. #ifdef __CYGWIN__
  70. handle=dlopen("cygwin1.dll",RTLD_LAZY);
  71. assert("Unable to dlopen global sym table"&&handle);
  72. #else
  73. handle=RTLD_NEXT;
  74. #endif
  75. }
  76. return handle;
  77. }