simplify __stdio_exit static linking logic
authorRich Felker <dalias@aerifal.cx>
Thu, 17 Jul 2014 00:44:22 +0000 (20:44 -0400)
committerRich Felker <dalias@aerifal.cx>
Thu, 17 Jul 2014 00:44:22 +0000 (20:44 -0400)
the purpose of this logic is to avoid linking __stdio_exit unless any
stdio reads (which might require repositioning the file offset at exit
time) or writes (which might require flushing at exit time) could have
been performed.

previously, exit called two wrapper functions for __stdio_exit named
__flush_on_exit and __seek_on_exit. both of these functions actually
performed both tasks (seek and flushing) by calling the underlying
__stdio_exit. in order to avoid doing this twice, an overridable data
object __towrite_used was used to cause __seek_on_exit to act as a nop
when __towrite was linked.

now, exit only makes one call, directly to __stdio_exit. this is
satisfiable by a weak dummy definition in exit.c, but the real
definition is pulled in by either __toread.c or __towrite.c through
their referencing a symbol which is defined only in __stdio_exit.c.

src/exit/exit.c
src/stdio/__stdio_exit.c
src/stdio/__toread.c
src/stdio/__towrite.c

index 695bdc0..27fb3e2 100644 (file)
@@ -6,10 +6,10 @@ static void dummy()
 {
 }
 
-/* __toread.c, __towrite.c, and atexit.c override these */
+/* atexit.c and __stdio_exit.c override these. the latter is linked
+ * as a consequence of linking either __toread.c or __towrite.c. */
 weak_alias(dummy, __funcs_on_exit);
-weak_alias(dummy, __flush_on_exit);
-weak_alias(dummy, __seek_on_exit);
+weak_alias(dummy, __stdio_exit);
 
 #ifndef SHARED
 weak_alias(dummy, _fini);
@@ -28,8 +28,7 @@ _Noreturn void exit(int code)
        _fini();
 #endif
 
-       __flush_on_exit();
-       __seek_on_exit();
+       __stdio_exit();
 
        _Exit(code);
        for(;;);
index 0fb3323..e4380aa 100644 (file)
@@ -21,3 +21,5 @@ void __stdio_exit(void)
        close_file(__stdin_used);
        close_file(__stdout_used);
 }
+
+weak_alias(__stdio_exit, __stdio_exit_needed);
index 2e804f6..52624f3 100644 (file)
@@ -13,12 +13,9 @@ int __toread(FILE *f)
        return 0;
 }
 
-static const int dummy = 0;
-weak_alias(dummy, __towrite_used);
+void __stdio_exit_needed(void);
 
-void __stdio_exit(void);
-
-void __seek_on_exit()
+void __toread_needs_stdio_exit()
 {
-       if (!__towrite_used) __stdio_exit();
+       __stdio_exit_needed();
 }
index 380ea39..0a69d92 100644 (file)
@@ -17,11 +17,9 @@ int __towrite(FILE *f)
        return 0;
 }
 
-const int __towrite_used = 1;
+void __stdio_exit_needed(void);
 
-void __stdio_exit(void);
-
-void __flush_on_exit()
+void __towrite_needs_stdio_exit()
 {
-       __stdio_exit();
+       __stdio_exit_needed();
 }