libc_stubs.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _ROM_LIBC_STUBS_H_
  14. #define _ROM_LIBC_STUBS_H_
  15. #include <sys/lock.h>
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #include <reent.h>
  21. #include <errno.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /*
  26. ESP32 ROM code contains implementations of some of C library functions.
  27. Whenever a function in ROM needs to use a syscall, it calls a pointer to the corresponding syscall
  28. implementation defined in the following struct.
  29. The table itself, by default, is not allocated in RAM. A global pointer syscall_table_ptr is used to
  30. set the address
  31. So, before using any of the C library functions (except for pure functions and memcpy/memset functions),
  32. application must allocate syscall table structure for each CPU being used, and populate it with pointers
  33. to actual implementations of corresponding syscalls.
  34. */
  35. struct syscall_stub_table
  36. {
  37. struct _reent* (*__getreent)(void);
  38. void* (*_malloc_r)(struct _reent *r, size_t);
  39. void (*_free_r)(struct _reent *r, void*);
  40. void* (*_realloc_r)(struct _reent *r, void*, size_t);
  41. void* (*_calloc_r)(struct _reent *r, size_t, size_t);
  42. void (*_abort)(void);
  43. int (*_system_r)(struct _reent *r, const char*);
  44. int (*_rename_r)(struct _reent *r, const char*, const char*);
  45. clock_t (*_times_r)(struct _reent *r, struct tms *);
  46. int (*_gettimeofday_r) (struct _reent *r, struct timeval *, void *);
  47. void (*_raise_r)(struct _reent *r);
  48. int (*_unlink_r)(struct _reent *r, const char*);
  49. int (*_link_r)(struct _reent *r, const char*, const char*);
  50. int (*_stat_r)(struct _reent *r, const char*, struct stat *);
  51. int (*_fstat_r)(struct _reent *r, int, struct stat *);
  52. void* (*_sbrk_r)(struct _reent *r, ptrdiff_t);
  53. int (*_getpid_r)(struct _reent *r);
  54. int (*_kill_r)(struct _reent *r, int, int);
  55. void (*_exit_r)(struct _reent *r, int);
  56. int (*_close_r)(struct _reent *r, int);
  57. int (*_open_r)(struct _reent *r, const char *, int, int);
  58. int (*_write_r)(struct _reent *r, int, const void *, int);
  59. int (*_lseek_r)(struct _reent *r, int, int, int);
  60. int (*_read_r)(struct _reent *r, int, void *, int);
  61. #ifdef _RETARGETABLE_LOCKING
  62. void (*_retarget_lock_init)(_LOCK_T *lock);
  63. void (*_retarget_lock_init_recursive)(_LOCK_T *lock);
  64. void (*_retarget_lock_close)(_LOCK_T lock);
  65. void (*_retarget_lock_close_recursive)(_LOCK_T lock);
  66. void (*_retarget_lock_acquire)(_LOCK_T lock);
  67. void (*_retarget_lock_acquire_recursive)(_LOCK_T lock);
  68. int (*_retarget_lock_try_acquire)(_LOCK_T lock);
  69. int (*_retarget_lock_try_acquire_recursive)(_LOCK_T lock);
  70. void (*_retarget_lock_release)(_LOCK_T lock);
  71. void (*_retarget_lock_release_recursive)(_LOCK_T lock);
  72. #else
  73. void (*_lock_init)(_lock_t *lock);
  74. void (*_lock_init_recursive)(_lock_t *lock);
  75. void (*_lock_close)(_lock_t *lock);
  76. void (*_lock_close_recursive)(_lock_t *lock);
  77. void (*_lock_acquire)(_lock_t *lock);
  78. void (*_lock_acquire_recursive)(_lock_t *lock);
  79. int (*_lock_try_acquire)(_lock_t *lock);
  80. int (*_lock_try_acquire_recursive)(_lock_t *lock);
  81. void (*_lock_release)(_lock_t *lock);
  82. void (*_lock_release_recursive)(_lock_t *lock);
  83. #endif
  84. int (*_printf_float)(struct _reent *data, void *pdata, FILE * fp, int (*pfunc) (struct _reent *, FILE *, const char *, size_t len), va_list * ap);
  85. int (*_scanf_float) (struct _reent *rptr, void *pdata, FILE *fp, va_list *ap);
  86. void (*__assert_func) (const char *file, int line, const char * func, const char *failedexpr) __attribute__((noreturn));
  87. void (*__sinit) (struct _reent *r);
  88. void (*_cleanup_r) (struct _reent* r);
  89. };
  90. extern struct syscall_stub_table *syscall_table_ptr;
  91. #ifdef __cplusplus
  92. } // extern "C"
  93. #endif
  94. #endif /* _ROM_LIBC_STUBS_H_ */