summaryrefslogtreecommitdiff
path: root/components/libc/compilers/newlib
diff options
context:
space:
mode:
authorguo <[email protected]>2021-09-28 10:36:54 +0800
committerGitHub <[email protected]>2021-09-28 10:36:54 +0800
commit55a302cd092758a44b589d65470a69d9e8ae4ced (patch)
treee60fc53994c426007e9eb4877e5bd123f124ef39 /components/libc/compilers/newlib
parent00727d6b6db92dc1cf98f028f26fd6168594b905 (diff)
parent00d42ef73cff61ecda7ff006b077a73e1a4ff669 (diff)
Merge branch 'master' into usbh_construct
Diffstat (limited to 'components/libc/compilers/newlib')
-rw-r--r--components/libc/compilers/newlib/SConscript29
-rw-r--r--components/libc/compilers/newlib/libc.c4
-rw-r--r--components/libc/compilers/newlib/libc.h4
-rw-r--r--components/libc/compilers/newlib/libc_syms.c2
-rw-r--r--components/libc/compilers/newlib/machine/time.h2
-rw-r--r--components/libc/compilers/newlib/stdio.c8
-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.c237
11 files changed, 148 insertions, 290 deletions
diff --git a/components/libc/compilers/newlib/SConscript b/components/libc/compilers/newlib/SConscript
index d0868bbdea..cef546e17c 100644
--- a/components/libc/compilers/newlib/SConscript
+++ b/components/libc/compilers/newlib/SConscript
@@ -1,21 +1,28 @@
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')
+ if GetDepend('RT_USING_MODULE') == False:
+ SrcRemove(src, ['libc_syms.c'])
+ else:
+ src += ['syscalls.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..c470698aa6 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
*
@@ -18,8 +18,8 @@
extern "C" {
#endif
int libc_system_init(void);
-int libc_stdio_set_console(const char* device_name, int mode);
int libc_stdio_get_console(void);
+int libc_stdio_set_console(const char* device_name, int mode);
#ifdef __cplusplus
}
diff --git a/components/libc/compilers/newlib/libc_syms.c b/components/libc/compilers/newlib/libc_syms.c
index 4b0ee355f0..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
*
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/stdio.c b/components/libc/compilers/newlib/stdio.c
index 233a447396..baccafad87 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
*
@@ -9,13 +9,13 @@
*/
#include <stdio.h>
#include <stdlib.h>
-
+#include <fcntl.h>
#include <rtthread.h>
#include "libc.h"
#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 45aa4a34de..4c1dc82ad3 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
*
@@ -8,41 +8,109 @@
* 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 <errno.h>
-#include <stdio.h>
-#include <sys/time.h>
-
#include <rtthread.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <sys/errno.h>
+
+#define DBG_TAG "newlib.syscalls"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.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)
+{
+ LOG_E("Please enable RT_USING_HEAP or RT_USING_LIBC");
+ RT_ASSERT(0);
+ return RT_NULL;
+}
+#endif /*RT_USING_HEAP*/
+
+void __libc_init_array(void)
+{
+ /* we not use __libc init_aray to initialize C++ objects */
+ /* __libc_init_array is ARM code, not Thumb; it will cause a hardfault. */
+}
+
+#ifdef RT_USING_LIBC
+#include <reent.h>
+#include <stdio.h>
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif
-
#ifdef RT_USING_MODULE
#include <dlmodule.h>
#endif
+#define DBG_TAG "newlib.syscalls"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
/* Reentrant versions of system calls. */
#ifndef _REENT_ONLY
-int *
-__errno ()
+int *__errno ()
{
return _rt_errno();
}
#endif
-int
-_getpid_r(struct _reent *ptr)
+int _getpid_r(struct _reent *ptr)
{
return 0;
}
-int
-_close_r(struct _reent *ptr, int fd)
+int _close_r(struct _reent *ptr, int fd)
{
#ifndef RT_USING_DFS
/* return "not supported" */
@@ -53,66 +121,61 @@ _close_r(struct _reent *ptr, int fd)
#endif
}
-int
-_execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
+int _execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
{
/* return "not supported" */
ptr->_errno = ENOTSUP;
return -1;
}
-int
-_fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
+int _fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
{
/* return "not supported" */
ptr->_errno = ENOTSUP;
return -1;
}
-int
-_fork_r(struct _reent *ptr)
+int _fork_r(struct _reent *ptr)
{
/* return "not supported" */
ptr->_errno = ENOTSUP;
return -1;
}
-int
-_fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
+int _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
{
/* return "not supported" */
ptr->_errno = ENOTSUP;
return -1;
}
-int
-_isatty_r(struct _reent *ptr, int fd)
+int _isatty_r(struct _reent *ptr, int fd)
{
if (fd >=0 && fd < 3)
+ {
return 1;
-
- ptr->_errno = ENOTTY ;
- return 0;
+ }
+ else
+ {
+ return 0;
+ }
}
-int
-_kill_r(struct _reent *ptr, int pid, int sig)
+int _kill_r(struct _reent *ptr, int pid, int sig)
{
/* return "not supported" */
ptr->_errno = ENOTSUP;
return -1;
}
-int
-_link_r(struct _reent *ptr, const char *old, const char *new)
+int _link_r(struct _reent *ptr, const char *old, const char *new)
{
/* return "not supported" */
ptr->_errno = ENOTSUP;
return -1;
}
-_off_t
-_lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
+_off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
{
#ifndef RT_USING_DFS
/* return "not supported" */
@@ -126,8 +189,7 @@ _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
#endif
}
-int
-_mkdir_r(struct _reent *ptr, const char *name, int mode)
+int _mkdir_r(struct _reent *ptr, const char *name, int mode)
{
#ifndef RT_USING_DFS
/* return "not supported" */
@@ -141,8 +203,7 @@ _mkdir_r(struct _reent *ptr, const char *name, int mode)
#endif
}
-int
-_open_r(struct _reent *ptr, const char *file, int flags, int mode)
+int _open_r(struct _reent *ptr, const char *file, int flags, int mode)
{
#ifndef RT_USING_DFS
/* return "not supported" */
@@ -156,8 +217,7 @@ _open_r(struct _reent *ptr, const char *file, int flags, int mode)
#endif
}
-_ssize_t
-_read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
+_ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
{
#ifndef RT_USING_DFS
/* return "not supported" */
@@ -165,14 +225,17 @@ _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
return -1;
#else
_ssize_t rc;
-
+ if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO)
+ {
+ LOG_W("Do not invoke standard input before initializing libc");
+ return 0;
+ }
rc = read(fd, buf, nbytes);
return rc;
#endif
}
-int
-_rename_r(struct _reent *ptr, const char *old, const char *new)
+int _rename_r(struct _reent *ptr, const char *old, const char *new)
{
#ifndef RT_USING_DFS
/* return "not supported" */
@@ -186,15 +249,7 @@ _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)
+int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
{
#ifndef RT_USING_DFS
/* return "not supported" */
@@ -208,8 +263,7 @@ _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
#endif
}
-int
-_unlink_r(struct _reent *ptr, const char *file)
+int _unlink_r(struct _reent *ptr, const char *file)
{
#ifndef RT_USING_DFS
/* return "not supported" */
@@ -220,20 +274,18 @@ _unlink_r(struct _reent *ptr, const char *file)
#endif
}
-int
-_wait_r(struct _reent *ptr, int *status)
+int _wait_r(struct _reent *ptr, int *status)
{
/* return "not supported" */
ptr->_errno = ENOTSUP;
return -1;
}
-_ssize_t
-_write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
+_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)
+#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
+ if (STDOUT_FILENO == fd)
{
rt_device_t console;
@@ -249,81 +301,30 @@ _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
#endif /*RT_USING_DEVICE*/
#else
_ssize_t rc;
-
+ if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO)
+ {
+ LOG_W("Do not invoke standard output before initializing libc");
+ return 0;
+ }
rc = write(fd, buf, nbytes);
return rc;
#endif
}
-/* Memory routine */
-void *
-_malloc_r (struct _reent *ptr, size_t size)
+void _system(const char *s)
{
- 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);
+ extern int __rt_libc_system(const char *string);
+ __rt_libc_system(s);
}
/* for exit() and abort() */
-__attribute__ ((noreturn)) void
-_exit (int status)
+__attribute__ ((noreturn)) void _exit (int status)
{
extern void __rt_libc_exit(int status);
__rt_libc_exit(status);
while(1);
}
-void
-_system(const char *s)
-{
- extern int __rt_libc_system(const char *string);
- __rt_libc_system(s);
-}
-
-void __libc_init_array(void)
-{
- /* we not use __libc init_aray to initialize C++ objects */
-}
-
mode_t umask(mode_t mask)
{
return 022;
@@ -339,3 +340,5 @@ 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);
*/
+
+#endif /* RT_USING_LIBC */