summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libc/stdio/stdio.c35
-rw-r--r--libc/stdlib/atexit.c3
-rw-r--r--libc/sysdeps/linux/arm/crt0.S26
-rw-r--r--libc/sysdeps/linux/arm/crt0pic.S67
-rw-r--r--libc/sysdeps/linux/i386/crt0.S22
5 files changed, 77 insertions, 76 deletions
diff --git a/libc/stdio/stdio.c b/libc/stdio/stdio.c
index f290260ae..c2486a7a4 100644
--- a/libc/stdio/stdio.c
+++ b/libc/stdio/stdio.c
@@ -121,9 +121,16 @@ void __stdio_close_all(void)
void __init_stdio(void)
{
+ static int stdio_initialized = 0;
#if FIXED_BUFFERS > 2
int i;
+#endif
+
+ if (stdio_initialized!=0)
+ return;
+ stdio_initialized++;
+#if FIXED_BUFFERS > 2
for ( i = 2 ; i < FIXED_BUFFERS ; i++ ) {
_fixed_buffers[i].used = 0;
}
@@ -135,7 +142,7 @@ void __init_stdio(void)
if (isatty(1)) {
stdout->mode |= _IOLBF;
}
-#if 0
+#if 1
/* Taken care of in _start.S and atexit.c now. */
atexit(__stdio_close_all);
#endif
@@ -149,6 +156,8 @@ FILE *fp;
{
register int v;
+ __init_stdio();
+
v = fp->mode;
/* If last op was a read ... */
if ((v & __MODE_READING) && fflush(fp))
@@ -194,6 +203,8 @@ FILE *fp;
{
int ch;
+ __init_stdio();
+
if (fp->mode & __MODE_WRITING)
fflush(fp);
@@ -236,6 +247,8 @@ FILE *fp;
int len, cc, rv = 0;
char *bstart;
+ __init_stdio();
+
if (fp == NULL) { /* On NULL flush the lot. */
if (fflush(stdin))
return EOF;
@@ -314,6 +327,8 @@ FILE *f;
register size_t i;
register int ch;
+ __init_stdio();
+
ret = s;
for (i = count-1; i > 0; i--) {
ch = getc(f);
@@ -343,6 +358,8 @@ char *str;
register char *p = str;
register int c;
+ __init_stdio();
+
while (((c = getc(stdin)) != EOF) && (c != '\n'))
*p++ = c;
*p = '\0';
@@ -357,6 +374,8 @@ FILE *fp;
{
register int n = 0;
+ __init_stdio();
+
while (*str) {
if (putc(*str++, fp) == EOF)
return (EOF);
@@ -372,6 +391,8 @@ const char *str;
{
register int n;
+ __init_stdio();
+
if (((n = fputs(str, stdout)) == EOF)
|| (putc('\n', stdout) == EOF))
return (EOF);
@@ -397,6 +418,8 @@ FILE *fp;
int len, v;
unsigned bytes, got = 0;
+ __init_stdio();
+
v = fp->mode;
/* Want to do this to bring the file pointer up to date */
@@ -453,6 +476,8 @@ FILE *fp;
int len;
unsigned bytes, put;
+ __init_stdio();
+
#ifdef STUB_FWRITE
bytes = size * nelm;
while (bytes > 0) {
@@ -524,6 +549,8 @@ FILE *fp;
void rewind(fp)
FILE *fp;
{
+ __init_stdio();
+
fseek(fp, (long) 0, 0);
clearerr(fp);
}
@@ -600,6 +627,8 @@ const char *mode;
int fopen_mode = 0;
FILE *nfp = 0;
+ __init_stdio();
+
/* If we've got an fp close the old one (freopen) */
if (fp) {
/* Careful, don't de-allocate it */
@@ -710,6 +739,8 @@ FILE *fp;
{
int rv = 0;
+ __init_stdio();
+
if (fp == 0) {
errno = EINVAL;
return EOF;
@@ -817,6 +848,8 @@ int ungetc(c, fp)
int c;
FILE *fp;
{
+ __init_stdio();
+
if (fp->mode & __MODE_WRITING)
fflush(fp);
diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c
index a7ec0fb1f..c720d4e90 100644
--- a/libc/stdlib/atexit.c
+++ b/libc/stdlib/atexit.c
@@ -50,15 +50,12 @@ int atexit(vfuncp ptr)
#endif
#ifdef L_exit
-void __stdio_close_all(void); /* note: see _start.S - could be faked */
-
vfuncp __cleanup = 0;
void exit(int rv)
{
if (__cleanup)
__cleanup();
- __stdio_close_all();
_exit(rv);
}
#endif
diff --git a/libc/sysdeps/linux/arm/crt0.S b/libc/sysdeps/linux/arm/crt0.S
index 72a45a29f..3660f4167 100644
--- a/libc/sysdeps/linux/arm/crt0.S
+++ b/libc/sysdeps/linux/arm/crt0.S
@@ -3,6 +3,7 @@
argv[0] program name (pointer)
argv[1...N] program args (pointers)
argv[argc-1] end of args (integer)
+ NULL
env[0...N] environment variables (pointers)
NULL
@@ -40,18 +41,10 @@ This file now uses the register naming from the ARM Procedure Calling Standard
.global _start
.global exit
.global main
- .global __libc_init
- .global __init_stdio
- .global __stdio_close_all
- .global _void_void_null_func
.type _start,%function
.type exit,%function
.type main,%function
- .type __libc_init,%function
- .type __init_stdio,%function
- .type __stdio_close_all,%function
- .type _void_void_null_func,%function
.text
_start:
@@ -73,27 +66,12 @@ _start:
add a3, a3, #4
str a3, [a4, #0]
- /* Tell libc to initialize whatever it needs */
- bl __libc_init
- bl __init_stdio
bl main
bl exit
-_void_void_null_func:
- mov pc, lr
-
-.weak __libc_init
-__libc_init = _void_void_null_func
-
-.weak __init_stdio
-__init_stdio = _void_void_null_func
-
-.weak __stdio_close_all
-__stdio_close_all = _void_void_null_func
-
.align 2
.L3:
- .word environ
+ .word __environ
.data
diff --git a/libc/sysdeps/linux/arm/crt0pic.S b/libc/sysdeps/linux/arm/crt0pic.S
index a668ac31f..04b1cb755 100644
--- a/libc/sysdeps/linux/arm/crt0pic.S
+++ b/libc/sysdeps/linux/arm/crt0pic.S
@@ -1,21 +1,49 @@
+/* When we enter this piece of code, the program stack looks like this:
+ argc argument counter (integer)
+ argv[0] program name (pointer)
+ argv[1...N] program args (pointers)
+ argv[argc-1] end of args (integer)
+ NULL
+ env[0...N] environment variables (pointers)
+ NULL
+
+ When we are done here, we want
+ a1=argc
+ a2=argv[0]
+ a3=argv[argc+1]
+
+ARM register quick reference:
+
+ Name Number APCS Role
+
+ a1 0 argument 1 / integer result / scratch register / argc
+ a2 1 argument 2 / scratch register / argv
+ a3 2 argument 3 / scratch register / envp
+ a4 3 argument 4 / scratch register
+ v1 4 register variable
+ v2 5 register variable
+ v3 6 register variable
+ v4 7 register variable
+ v5 8 register variable
+ sb/v6 9 static base / register variable
+ sl/v7 10 stack limit / stack chunk handle / reg. variable
+ fp 11 frame pointer
+ ip 12 scratch register / new-sb in inter-link-unit calls
+ sp 13 lower end of current stack frame
+ lr 14 link address / scratch register
+ pc 15 program counter
+*/
+
.text
.align 2
.global __environ
.global _start
.global exit
.global main
- .global __libc_init
- .global __init_stdio
- .global __stdio_close_all
- .global _void_void_null_func
.type _start,%function
.type exit,%function
.type main,%function
- .type __libc_init,%function
- .type __init_stdio,%function
- .type __stdio_close_all,%function
- .type _void_void_null_func,%function
@@ -23,21 +51,18 @@
@ r1 = argv
@ r2 = envp
@ sl = data segment
+#define BASEREG r9
.text
_start:
@ adjust the data segment base pointer
ldr r3,=__data_start
sub sl,sl,r3
- mov r9,sl
+ mov BASEREG,sl
ldr r3, .L3
- str r2,[r9,r3]
+ str r2,[BASEREG,r3]
- /* Tell libc to initialize whatever it needs */
- bl __libc_init
- bl __init_stdio
-
/* pull argc, argv and envp off the stack */
ldr r0,[sp, #0]
ldr r1,[sp, #4]
@@ -47,21 +72,9 @@ _start:
/* ldr r0,=0 */
bl exit
-_void_void_null_func:
- mov pc, lr
-
-.weak __libc_init
-__libc_init = _void_void_null_func
-
-.weak __init_stdio
-__init_stdio = _void_void_null_func
-
-.weak __stdio_close_all
-__stdio_close_all = _void_void_null_func
-
.align 2
.L3:
- .word environ
+ .word __environ
.data
diff --git a/libc/sysdeps/linux/i386/crt0.S b/libc/sysdeps/linux/i386/crt0.S
index 1d01a5dcc..a87287bc3 100644
--- a/libc/sysdeps/linux/i386/crt0.S
+++ b/libc/sysdeps/linux/i386/crt0.S
@@ -25,6 +25,7 @@ Cambridge, MA 02139, USA. */
argv[0] program name (pointer)
argv[1...N] program args (pointers)
argv[argc-1] end of args (integer)
+ NULL
env[0...N] environment variables (pointers)
NULL
*/
@@ -33,8 +34,6 @@ Cambridge, MA 02139, USA. */
.global _start
.global exit
.global main
-.global __stdio_close_all
-.global _void_void_null_func
.global _start_exit
.text
@@ -78,11 +77,6 @@ _start:
movl 8(%esp),%eax
movl %eax,__environ
- /* Tell libc to initialize anything it needs to do */
- call __libc_init
- /* call __malloc_init */
- call __init_stdio
-
/* Ok, now run main() */
call main
pushl %eax
@@ -99,20 +93,6 @@ _start_exit:
_void_void_null_func:
ret
-.weak __libc_init
-__libc_init = _void_void_null_func
-
-/*
-.weak __malloc_init
-__malloc_init = _void_void_null_func
-*/
-
-.weak __init_stdio
-__init_stdio = _void_void_null_func
-
-.weak __stdio_close_all
-__stdio_close_all = _void_void_null_func
-
.data
__environ:
.long 0