summaryrefslogtreecommitdiff
path: root/components/libc/compilers/newlib
diff options
context:
space:
mode:
authorlizhirui <[email protected]>2021-05-18 09:54:11 +0800
committerlizhirui <[email protected]>2021-05-18 09:54:11 +0800
commit9a4e270f9770f778128ffdabe94f101416ce8c61 (patch)
tree9df43fbcc25c3f288ed6fdecd65909d4094d849f /components/libc/compilers/newlib
parente7fc0c89d2367533dcd1b77e46acaeea7bc3f876 (diff)
parent4e9e3f79a565bb4fdc1d96deda18f9c771aeb0c9 (diff)
Merge branch 'RT-Thread:master' into master
Diffstat (limited to 'components/libc/compilers/newlib')
-rw-r--r--components/libc/compilers/newlib/SConscript30
-rw-r--r--components/libc/compilers/newlib/libc.c4
-rw-r--r--components/libc/compilers/newlib/libc.h2
-rw-r--r--components/libc/compilers/newlib/libc_syms.c6
-rw-r--r--components/libc/compilers/newlib/machine/time.h2
-rw-r--r--components/libc/compilers/newlib/minilib.c68
-rw-r--r--components/libc/compilers/newlib/stdio.c6
-rw-r--r--components/libc/compilers/newlib/sys/dirent.h65
-rw-r--r--components/libc/compilers/newlib/sys/mman.h58
-rw-r--r--components/libc/compilers/newlib/sys/statfs.h21
-rw-r--r--components/libc/compilers/newlib/sys/termios.h8
-rw-r--r--components/libc/compilers/newlib/syscalls.c108
12 files changed, 156 insertions, 222 deletions
diff --git a/components/libc/compilers/newlib/SConscript b/components/libc/compilers/newlib/SConscript
index d0868bbdea..542cdecd40 100644
--- a/components/libc/compilers/newlib/SConscript
+++ b/components/libc/compilers/newlib/SConscript
@@ -1,21 +1,29 @@
from building import *
Import('rtconfig')
-src = Glob('*.c')
+src = []
cwd = GetCurrentDir()
group = []
-
+LIBS = []
+CPPDEFINES = []
CPPPATH = [cwd]
-CPPDEFINES = ['RT_USING_NEWLIB']
-
-# link with libc and libm:
-# libm is a frequently used lib. Newlib is compiled with -ffunction-sections in
-# recent GCC tool chains. The linker would just link in the functions that have
-# been referenced. So setting this won't result in bigger text size.
-LIBS = ['c', 'm']
if rtconfig.PLATFORM == 'gcc':
- group = DefineGroup('libc', src, depend = ['RT_USING_LIBC'],
- CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES, LIBS = LIBS)
+ if GetDepend('RT_USING_LIBC'):
+ CPPDEFINES += ['RT_USING_NEWLIB']
+ # link with libc and libm:
+ # libm is a frequently used lib. Newlib is compiled with -ffunction-sections in
+ # recent GCC tool chains. The linker would just link in the functions that have
+ # been referenced. So setting this won't result in bigger text size.
+ LIBS += ['c', 'm']
+
+ src += Glob('*.c')
+ SrcRemove(src, ['minilib.c'])
+ if GetDepend('RT_USING_MODULE') == False:
+ SrcRemove(src, ['libc_syms.c'])
+ else:
+ src += ['minilib.c']
+
+ group = DefineGroup('libc', src, depend = [], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES, LIBS = LIBS)
Return('group')
diff --git a/components/libc/compilers/newlib/libc.c b/components/libc/compilers/newlib/libc.c
index 2234c36f98..9834a9a015 100644
--- a/components/libc/compilers/newlib/libc.c
+++ b/components/libc/compilers/newlib/libc.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -19,7 +19,7 @@
#include <pthread.h>
#endif
-int _EXFUN(putenv,(char *__string));
+int _EXFUN(putenv,(char *__string));
int libc_system_init(void)
{
diff --git a/components/libc/compilers/newlib/libc.h b/components/libc/compilers/newlib/libc.h
index 9a76ffb79f..c1f76d7605 100644
--- a/components/libc/compilers/newlib/libc.h
+++ b/components/libc/compilers/newlib/libc.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
diff --git a/components/libc/compilers/newlib/libc_syms.c b/components/libc/compilers/newlib/libc_syms.c
index b150c10fd6..aaf6980d6b 100644
--- a/components/libc/compilers/newlib/libc_syms.c
+++ b/components/libc/compilers/newlib/libc_syms.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -39,10 +39,6 @@ RTM_EXPORT(snprintf);
RTM_EXPORT(fwrite);
-#include <time.h>
-RTM_EXPORT(localtime);
-RTM_EXPORT(time);
-
#include <setjmp.h>
RTM_EXPORT(longjmp);
RTM_EXPORT(setjmp);
diff --git a/components/libc/compilers/newlib/machine/time.h b/components/libc/compilers/newlib/machine/time.h
index d46533eb39..ff754d2473 100644
--- a/components/libc/compilers/newlib/machine/time.h
+++ b/components/libc/compilers/newlib/machine/time.h
@@ -9,4 +9,4 @@
int nanosleep (const struct timespec *, struct timespec *);
#endif
-#endif /* _MACHTIME_H_ */
+#endif /* _MACHTIME_H_ */
diff --git a/components/libc/compilers/newlib/minilib.c b/components/libc/compilers/newlib/minilib.c
new file mode 100644
index 0000000000..f6df38f67b
--- /dev/null
+++ b/components/libc/compilers/newlib/minilib.c
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2021-02-23 Meco Man first version
+ */
+
+#include <reent.h>
+#include <rtthread.h>
+
+#ifdef RT_USING_HEAP /* Memory routine */
+void *
+_malloc_r (struct _reent *ptr, size_t size)
+{
+ void* result;
+
+ result = (void*)rt_malloc (size);
+ if (result == RT_NULL)
+ {
+ ptr->_errno = ENOMEM;
+ }
+
+ return result;
+}
+
+void *
+_realloc_r (struct _reent *ptr, void *old, size_t newlen)
+{
+ void* result;
+
+ result = (void*)rt_realloc (old, newlen);
+ if (result == RT_NULL)
+ {
+ ptr->_errno = ENOMEM;
+ }
+
+ return result;
+}
+
+void *_calloc_r (struct _reent *ptr, size_t size, size_t len)
+{
+ void* result;
+
+ result = (void*)rt_calloc (size, len);
+ if (result == RT_NULL)
+ {
+ ptr->_errno = ENOMEM;
+ }
+
+ return result;
+}
+
+void
+_free_r (struct _reent *ptr, void *addr)
+{
+ rt_free (addr);
+}
+
+#else
+void *
+_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
+{
+ return RT_NULL;
+}
+#endif /*RT_USING_HEAP*/
diff --git a/components/libc/compilers/newlib/stdio.c b/components/libc/compilers/newlib/stdio.c
index 233a447396..ce5bdb75cb 100644
--- a/components/libc/compilers/newlib/stdio.c
+++ b/components/libc/compilers/newlib/stdio.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -15,7 +15,7 @@
#define STDIO_DEVICE_NAME_MAX 32
-int _EXFUN(fileno, (FILE *));
+int _EXFUN(fileno, (FILE *));
static FILE* std_console = NULL;
@@ -48,7 +48,7 @@ int libc_stdio_set_console(const char* device_name, int mode)
{
_GLOBAL_REENT->_stdin = std_console;
}
- else
+ else
{
_GLOBAL_REENT->_stdin = NULL;
}
diff --git a/components/libc/compilers/newlib/sys/dirent.h b/components/libc/compilers/newlib/sys/dirent.h
deleted file mode 100644
index db8c0655a1..0000000000
--- a/components/libc/compilers/newlib/sys/dirent.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- */
-#ifndef __RTT_DIRENT_H__
-#define __RTT_DIRENT_H__
-
-#include <rtthread.h>
-
-/*
-* dirent.h - format of directory entries
- * Ref: http://www.opengroup.org/onlinepubs/009695399/basedefs/dirent.h.html
- */
-
-/* File types */
-#define FT_REGULAR 0 /* regular file */
-#define FT_SOCKET 1 /* socket file */
-#define FT_DIRECTORY 2 /* directory */
-#define FT_USER 3 /* user defined */
-
-#define DT_UNKNOWN 0x00
-#define DT_REG 0x01
-#define DT_DIR 0x02
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#ifndef HAVE_DIR_STRUCTURE
-typedef struct
-{
- int fd; /* directory file */
- char buf[512];
- int num;
- int cur;
-} DIR;
-#endif
-
-#ifndef HAVE_DIRENT_STRUCTURE
-struct dirent
-{
- rt_uint8_t d_type; /* The type of the file */
- rt_uint8_t d_namlen; /* The length of the not including the terminating null file name */
- rt_uint16_t d_reclen; /* length of this record */
- char d_name[256]; /* The null-terminated file name */
-};
-#endif
-
-int closedir(DIR *);
-DIR *opendir(const char *);
-struct dirent *readdir(DIR *);
-int readdir_r(DIR *, struct dirent *, struct dirent **);
-void rewinddir(DIR *);
-void seekdir(DIR *, long int);
-long telldir(DIR *);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/components/libc/compilers/newlib/sys/mman.h b/components/libc/compilers/newlib/sys/mman.h
deleted file mode 100644
index 8c61b32b7c..0000000000
--- a/components/libc/compilers/newlib/sys/mman.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2017/11/30 Bernard The first version.
- */
-
-#ifndef _SYS_MMAN_H
-#define _SYS_MMAN_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#define MAP_FAILED ((void *) -1)
-
-#define MAP_SHARED 0x01
-#define MAP_PRIVATE 0x02
-#define MAP_TYPE 0x0f
-#define MAP_FIXED 0x10
-#define MAP_ANON 0x20
-#define MAP_ANONYMOUS MAP_ANON
-#define MAP_NORESERVE 0x4000
-#define MAP_GROWSDOWN 0x0100
-#define MAP_DENYWRITE 0x0800
-#define MAP_EXECUTABLE 0x1000
-#define MAP_LOCKED 0x2000
-#define MAP_POPULATE 0x8000
-#define MAP_NONBLOCK 0x10000
-#define MAP_STACK 0x20000
-#define MAP_HUGETLB 0x40000
-#define MAP_FILE 0
-
-#define PROT_NONE 0
-#define PROT_READ 1
-#define PROT_WRITE 2
-#define PROT_EXEC 4
-#define PROT_GROWSDOWN 0x01000000
-#define PROT_GROWSUP 0x02000000
-
-#define MS_ASYNC 1
-#define MS_INVALIDATE 2
-#define MS_SYNC 4
-
-#define MCL_CURRENT 1
-#define MCL_FUTURE 2
-#define MCL_ONFAULT 4
-
-void *mmap (void *start, size_t len, int prot, int flags, int fd, off_t off);
-int munmap (void *start, size_t len);
-
-#ifdef __cplusplus
-}
-#endif
-#endif
diff --git a/components/libc/compilers/newlib/sys/statfs.h b/components/libc/compilers/newlib/sys/statfs.h
deleted file mode 100644
index 3c923a21f9..0000000000
--- a/components/libc/compilers/newlib/sys/statfs.h
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- */
-#ifndef __RTT_STATFS_H__
-#define __RTT_STATFS_H__
-
-#include <rtthread.h>
-
-struct statfs
-{
- rt_size_t f_bsize; /* block size */
- rt_size_t f_blocks; /* total data blocks in file system */
- rt_size_t f_bfree; /* free blocks in file system */
-};
-
-#endif
diff --git a/components/libc/compilers/newlib/sys/termios.h b/components/libc/compilers/newlib/sys/termios.h
deleted file mode 100644
index ce95230e88..0000000000
--- a/components/libc/compilers/newlib/sys/termios.h
+++ /dev/null
@@ -1,8 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- */
diff --git a/components/libc/compilers/newlib/syscalls.c b/components/libc/compilers/newlib/syscalls.c
index 76300f76b2..7c5d51eca6 100644
--- a/components/libc/compilers/newlib/syscalls.c
+++ b/components/libc/compilers/newlib/syscalls.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -7,12 +7,14 @@
* Date Author Notes
* 2021-02-11 Meco Man remove _gettimeofday_r() and _times_r()
* 2020-02-13 Meco Man re-implement exit() and abort()
+ * 2020-02-21 Meco Man improve and beautify syscalls
+ * 2020-02-24 Meco Man fix bug of _isatty_r()
*/
#include <reent.h>
-#include <sys/errno.h>
-#include <sys/time.h>
+#include <errno.h>
#include <stdio.h>
+#include <sys/time.h>
#include <rtthread.h>
@@ -35,10 +37,18 @@ __errno ()
#endif
int
+_getpid_r(struct _reent *ptr)
+{
+ return 0;
+}
+
+int
_close_r(struct _reent *ptr, int fd)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
return close(fd);
#endif
@@ -77,19 +87,16 @@ _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
}
int
-_getpid_r(struct _reent *ptr)
-{
- return 0;
-}
-
-int
_isatty_r(struct _reent *ptr, int fd)
{
- if (fd >=0 && fd < 3) return 1;
-
- /* return "not supported" */
- ptr->_errno = ENOTSUP;
- return -1;
+ if (fd >=0 && fd < 3)
+ {
+ return 1;
+ }
+ else
+ {
+ return 0;
+ }
}
int
@@ -112,7 +119,9 @@ _off_t
_lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
_off_t rc;
@@ -125,7 +134,9 @@ int
_mkdir_r(struct _reent *ptr, const char *name, int mode)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
int rc;
@@ -138,7 +149,9 @@ int
_open_r(struct _reent *ptr, const char *file, int flags, int mode)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
int rc;
@@ -151,7 +164,9 @@ _ssize_t
_read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
_ssize_t rc;
@@ -164,7 +179,9 @@ int
_rename_r(struct _reent *ptr, const char *old, const char *new)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
int rc;
@@ -173,18 +190,13 @@ _rename_r(struct _reent *ptr, const char *old, const char *new)
#endif
}
-void *
-_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
-{
- /* no use this routine to get memory */
- return RT_NULL;
-}
-
int
_stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
int rc;
@@ -197,6 +209,8 @@ int
_unlink_r(struct _reent *ptr, const char *file)
{
#ifndef RT_USING_DFS
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
return -1;
#else
return unlink(file);
@@ -211,11 +225,11 @@ _wait_r(struct _reent *ptr, int *status)
return -1;
}
-#ifdef RT_USING_DEVICE
_ssize_t
_write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
{
#ifndef RT_USING_DFS
+#ifdef RT_USING_DEVICE
if (fileno(stdout) == fd)
{
rt_device_t console;
@@ -225,7 +239,11 @@ _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
}
return 0;
-
+#else
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
+#endif /*RT_USING_DEVICE*/
#else
_ssize_t rc;
@@ -233,9 +251,8 @@ _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
return rc;
#endif
}
-#endif
-/* Memory routine */
+#ifdef RT_USING_HEAP /* Memory routine */
void *
_malloc_r (struct _reent *ptr, size_t size)
{
@@ -283,8 +300,17 @@ _free_r (struct _reent *ptr, void *addr)
rt_free (addr);
}
-void
-exit (int status)
+#else
+void *
+_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
+{
+ return RT_NULL;
+}
+#endif /*RT_USING_HEAP*/
+
+/* for exit() and abort() */
+__attribute__ ((noreturn)) void
+_exit (int status)
{
extern void __rt_libc_exit(int status);
__rt_libc_exit(status);
@@ -303,18 +329,6 @@ void __libc_init_array(void)
/* we not use __libc init_aray to initialize C++ objects */
}
-void abort(void)
-{
- extern void __rt_libc_abort(void);
- __rt_libc_abort();
- while(1);
-}
-
-uid_t getuid(void)
-{
- return 0;
-}
-
mode_t umask(mode_t mask)
{
return 022;
@@ -326,7 +340,7 @@ int flock(int fd, int operation)
}
/*
-These functions will be implemented and replaced by the 'common/time.c' file
+These functions are implemented and replaced by the 'common/time.c' file
int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
_CLOCK_T_ _times_r(struct _reent *ptr, struct tms *ptms);
*/