libc glossy 嵌入式底层移植
Newlib的构成
Newlib由三部分构成:libgloss、libc、libm,三者在Newlib原代码中的存储位置如下。
- newlib-x.y.z
- libgloss
- newlib
- libc
- libm
libc是标准C库,libm是标准数学库,那libgloss是干啥的?
C库的部分函数需要引用系统调用,裸机系统没有这些系统调用,那么就由C库中的libgloss来提
供,gloss这个单词的本意是"假象"。
#include <errno.h> #include <sys/types.h> #include <sys/time.h> #include <sys/times.h> #include <unistd.h> #include <serial.h> #include <timer.h> /* * file operations */ /* Read from a file. */ ssize_t _read(int file, char *ptr, size_t len) { size_t id; int ch; for (id = 0; id < len; id++) { while (serial_getc(&ch) != 0); *ptr = ch; ptr++; serial_putc(ch); if ((ch == '\r')) { break; } } return id + 1; } /* Write to a file. */ ssize_t _write(int file, const void *ptr, size_t len) { const char *bptr = ptr; for (size_t i = 0; i < len; ++i) { serial_putc(bptr[i]); } return len; } /* Close a file. */ int _close(int file) { return 0; } /* Status of an open file. The sys/stat.h header file required is distributed in the include subdirectory for this C library. */ int _fstat(__attribute__((unused)) int file) { return 0; } /* Set position in a file. */ off_t _lseek(int file, off_t ptr, int dir) { return 0; } /* Query whether output stream is a terminal. For consistency with the other minimal implementations, which only support output to stdout, this minimal implementation is suggested by the newlib docs. */ int _isatty(int file) { return (file == STDOUT_FILENO); } #include <sys/types.h> /* brk is handled entirely within the C library. This limits METAL programs * that use the C library to be disallowed from dynamically allocating memory * without talking to the C library, but that sounds like a sane way to go * about it. Note that there is no error checking anywhere in this file, users * will simply get the relevant error when actually trying to use the memory * that's been allocated. */ extern char metal_segment_heap_target_start; extern char metal_segment_heap_target_end; static char *__brk = &metal_segment_heap_target_start; char *_sbrk(ptrdiff_t incr) { char *old = __brk; /* If __heap_size == 0, we can't allocate memory on the heap */ if (&metal_segment_heap_target_start == &metal_segment_heap_target_end) { return (void *) -1; } /* Don't move the break past the end of the heap */ if ((__brk + incr) < &metal_segment_heap_target_end) { __brk += incr; } else { __brk = &metal_segment_heap_target_end; return (void *) -1; } return old; } /* Get process id. This is sometimes used to generate strings unlikely to conflict with other processes. Minimal implementation for a system without processes just returns 1. */ int _getpid() { return 1; } /* Send a signal. Minimal implementation for a system without processes just causes an error. */ int _kill(int pid, int sig) { errno = EINVAL; return -1; } /* Exit a program without cleaning up files. */ void _exit(int exit_status) { while (1); } clock_t _times(struct tms *buf) { uint64_t ticks; ticks = get_timestamp_us(); buf->tms_stime = 0; buf->tms_cutime = 0; buf->tms_cstime = 0; return buf->tms_utime = ticks; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!