Rewrite by Zack Weinberg.

1997-04-18 02:18  Ulrich Drepper  <drepper@cygnus.com>

	* csu/initfini.c: Rewrite by Zack Weinberg
	<zack@rabi.phys.columbia.edu>.
	* csu/Makefile: Add rules for rewrite.
	* csu/defs.awk: Helper script to generate derived header.
This commit is contained in:
Ulrich Drepper 1997-04-18 00:20:23 +00:00
parent dd33e89fb2
commit 307bcd7783
4 changed files with 99 additions and 99 deletions

View File

@ -1,3 +1,10 @@
1997-04-18 02:18 Ulrich Drepper <drepper@cygnus.com>
* csu/initfini.c: Rewrite by Zack Weinberg
<zack@rabi.phys.columbia.edu>.
* csu/Makefile: Add rules for rewrite.
* csu/defs.awk: Helper script to generate derived header.
1997-04-17 16:55 Ulrich Drepper <drepper@cygnus.com> 1997-04-17 16:55 Ulrich Drepper <drepper@cygnus.com>
* misc/libgen.h: Change prototype for of basename to XPG variant. * misc/libgen.h: Change prototype for of basename to XPG variant.

View File

@ -1,6 +1,6 @@
# Makefile for csu code for GNU C library. # Makefile for csu code for GNU C library.
# Copyright (C) 1995, 1996 Free Software Foundation, Inc. # Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
# This file is part of the GNU C Library. # This file is part of the GNU C Library.
# The GNU C Library is free software; you can redistribute it and/or # The GNU C Library is free software; you can redistribute it and/or
@ -56,7 +56,7 @@ have-initfini = yes
endif endif
ifeq (yes,$(have-initfini)) ifeq ($(have-initfini),yes)
CPPFLAGS += -DHAVE_INITFINI CPPFLAGS += -DHAVE_INITFINI
@ -67,27 +67,29 @@ crtstuff = crti crtn
install-lib += $(crtstuff:=.o) install-lib += $(crtstuff:=.o)
extra-objs += $(crtstuff:=.o) extra-objs += $(crtstuff:=.o)
generated += $(crtstuff:=.s) generated += $(crtstuff:=.S) initfini.s align.h end.h
omit-deps += $(crtstuff) omit-deps += $(crtstuff)
# Compile initfini.c to assembly code, which contains embedded shell # Special rules for the building of crti.o and crtn.o
# commands that produce crti.s-new and crtn.s-new when run. We need to $(objpfx)crt%.o: $(objpfx)crt%.S $(objpfx)defs.h
# disable emission of .size directives and debugging information, since $(CC) -c -fPIC -g0 -I$(..) -I$(common-objpfx) -DASSEMBLER $< -o $@
# they will get confused by the splitting of the output we do.
$(objpfx)cr%i.s $(objpfx)cr%n.s: initfini.c; $(initfini)
define initfini $(objpfx)initfini.s: initfini.c
-rm -f $(objpfx)crtcommon.tmp $(CC) -S -fPIC -finhibit-size-directive $(no-exceptions) $< -o $@
(echo 'cat > crtcommon.tmp <<\EOF_common'; \
$(CC) $< $(CPPFLAGS) $(CFLAGS) \ $(objpfx)crti.S: $(objpfx)initfini.s
-fPIC -finhibit-size-directive $(no-exceptions) -g0 -S -o -; \ sed -n -e '1,/@HEADER_ENDS/p' \
echo 'EOF_common') | (cd $(@D); $(SHELL)) -e '/@_.*_PROLOG_BEGINS/,/@_.*_PROLOG_ENDS/p' \
cat $(objpfx)crtcommon.tmp >> $(objpfx)crti.s-new -e '/@TRAILER_BEGINS/,$$p' $< > $@
cat $(objpfx)crtcommon.tmp >> $(objpfx)crtn.s-new
rm -f $(objpfx)crtcommon.tmp $(objpfx)crtn.S: $(objpfx)initfini.s
mv -f $(objpfx)crti.s-new $(subst crtn,crti,$@) sed -n -e '1,/@HEADER_ENDS/p' \
mv -f $(objpfx)crtn.s-new $(subst crti,crtn,$@) -e '/@_.*_EPILOG_BEGINS/,/@_.*_EPILOG_ENDS/p' \
endef -e '/@TRAILER_BEGINS/,$$p' $< > $@
$(objpfx)defs.h: $(objpfx)initfini.s
sed -n -e '/@TESTS_BEGIN/,/@TESTS_END/p' $< | \
awk -f defs.awk > $@
endif endif

22
csu/defs.awk Normal file
View File

@ -0,0 +1,22 @@
/.end/ { need_end = 1 }
/.align/ { if($2 > max) max = $2; }
END {
if(need_end)
{
print "#define END_INIT .end _init";
print "#define END_FINI .end _fini";
}
else
{
print "#define END_INIT";
print "#define END_FINI";
}
if(max)
print "#define ALIGN .align", max;
else
print "#define ALIGN";
print "#include <libc-symbols.h>";
print "weak_extern (__gmon_start__)";
}

View File

@ -1,5 +1,5 @@
/* Special .init and .fini section support. /* Special .init and .fini section support.
Copyright (C) 1995, 1996 Free Software Foundation, Inc. Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it The GNU C Library is free software; you can redistribute it
@ -26,68 +26,40 @@
write to the Free Software Foundation, 59 Temple Place - Suite 330, write to the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */ Boston, MA 02111-1307, USA. */
/* This file is compiled into assembly code which is then surrounded by the /* This file is compiled into assembly code which is then munged by a sed
lines `cat > crtcommon.tmp <<\EOF_common' and `EOF_common' and thus script into two files: crti.s and crtn.s.
becomes a shell script which creates three files of assembly code.
* The first file is crti.s-new; this puts a function prologue at the * crti.s puts a function prologue at the beginning of the
beginning of the .init and .fini sections and defines global symbols for .init and .fini sections and defines global symbols for
those addresses, so they can be called as functions. those addresses, so they can be called as functions.
* The second file is crtn.s-new; this puts the corresponding function * crtn.s puts the corresponding function epilogues
epilogues in the .init and .fini sections. in the .init and .fini sections. */
* The third file is crtcommon.tmp, which is whatever miscellaneous cruft
the compiler generated at the end; it should be appended to both crti.s-new
and crtn.s-new. */
#include <stdlib.h> #include <stdlib.h>
/* We use embedded asm for .section unconditionally, as this makes it
easier to insert the necessary directives into crtn.S. */
#define SECTION(x) asm (".section \"" x "\"");
#ifdef HAVE_ELF /* Embed an #include to pull in the alignment and .end directives. */
/* These declarations make the functions go in the right sections when asm ("\n#include \"defs.h\"");
we define them below. GCC syntax does not allow the attribute
specifications to be in the function definitions themselves. */
void _init (void) __attribute__ ((section (".init")));
void _fini (void) __attribute__ ((section (".fini")));
#define SECTION(x) /* Put nothing extra before the defn. */ /* The initial common code ends here. */
asm ("\n/*@HEADER_ENDS*/");
#else /* To determine whether we need .end and .align: */
/* Some non-ELF systems support .init and .fini sections, asm ("\n/*@TESTS_BEGIN*/");
but the __attribute__ syntax only works for ELF. */
#define SECTION(x) asm (".section " x);
#endif
/* End the here document containing the initial common code.
Then move the output file crtcommon.tmp to crti.s-new and crtn.s-new. */
asm ("\nEOF_common\n\
rm -f crti.s-new crtn.s-new\n\
mv crtcommon.tmp crti.s-new\n\
cp crti.s-new crtn.s-new");
/* Extract a `.end' if one is produced by the compiler. */
asm ("fgrep .end >/dev/null 2>&1 <<\\EOF.end && need_end=yes");
void void
useless_function (void) dummy (void (*foo) (void))
{
return;
}
asm ("\nEOF.end\n");
/* Find out how much alignment is produced by the compiler. */
asm ("align=`awk '$1==\".align\" { if ($2>max) max=$2; } END { print max; }' \
<<\\EOF.align");
void
useless_function2 (void (*foo) (void))
{ {
if (foo) if (foo)
(*foo) (); (*foo) ();
} }
asm ("\nEOF.align\n`\n"); asm ("\n/*@TESTS_END*/");
/* Append the .init prologue to crti.s-new. */ /* The beginning of _init: */
asm ("cat >> crti.s-new <<\\EOF.crti.init"); asm ("\n/*@_init_PROLOG_BEGINS*/");
SECTION (".init") SECTION (".init")
void void
@ -99,38 +71,32 @@ _init (void)
gcrt1.o to reference a symbol which would be defined by some library gcrt1.o to reference a symbol which would be defined by some library
module which has a constructor; but then user code's constructors module which has a constructor; but then user code's constructors
would come first, and not be profiled. */ would come first, and not be profiled. */
extern void __gmon_start__ (void); weak_extern (__gmon_start__) extern void __gmon_start__ (void) __attribute__ ((weak)); /*weak_extern (__gmon_start__);*/
if (__gmon_start__) if (__gmon_start__)
__gmon_start__ (); __gmon_start__ ();
/* End the here document containing the .init prologue code. asm("END_INIT");
Then fetch the .section directive just written and append that /* Now the epilog. */
to crtn.s-new, followed by the function epilogue. */ asm ("\n/*@_init_PROLOG_ENDS*/");
asm ("\n\ asm ("\n/*@_init_EPILOG_BEGINS*/");
EOF.crti.init\n\ SECTION(".init");
test -n \"$align\" && echo .align $align >> crti.s-new\n\ asm ("ALIGN");
test -n \"$need_end\" && echo .end _init >> crti.s-new\n\
fgrep .init crti.s-new >>crtn.s-new\n\
fgrep -v .end >> crtn.s-new <<\\EOF.crtn.init");
} }
asm ("END_INIT");
/* End the here document containing the .init epilogue code. /* End of the _init epilog, beginning of the _fini prolog. */
Then append the .fini prologue to crti.s-new. */ asm ("\n/*@_init_EPILOG_ENDS*/");
asm ("\nEOF.crtn.init\ asm ("\n/*@_fini_PROLOG_BEGINS*/");
\n\
cat >> crti.s-new <<\\EOF.crti.fini");
SECTION (".fini") SECTION (".fini")
void void
_fini (void) _fini (void)
{ {
/* End the here document containing the .fini prologue code.
Then fetch the .section directive just written and append that /* End of the _fini prolog. */
to crtn.s-new, followed by the function epilogue. */ asm ("END_FINI");
asm ("\nEOF.crti.fini\n\ asm ("\n/*@_fini_PROLOG_ENDS*/");
test -n \"$align\" && echo .align $align >> crti.s-new\n\
test -n \"$need_end\" && echo .end _fini >> crti.s-new\n\
cat > /dev/null <<\\EOF.fini.skip");
{ {
/* Let GCC know that _fini is not a leaf function by having a dummy /* Let GCC know that _fini is not a leaf function by having a dummy
@ -140,14 +106,17 @@ cat > /dev/null <<\\EOF.fini.skip");
i_am_not_a_leaf (); i_am_not_a_leaf ();
} }
asm ("\nEOF.fini.skip\ /* Beginning of the _fini epilog. */
\n\ asm ("\n/*@_fini_EPILOG_BEGINS*/");
fgrep .fini crti.s-new >>crtn.s-new\n\ SECTION (".fini");
fgrep -v .end >> crtn.s-new <<\\EOF.crtn.fini"); asm ("ALIGN");
} }
asm ("END_FINI");
/* End the here document containing the .fini epilogue code. /* End of the _fini epilog. Any further generated assembly (e.g. .ident)
Finally, put the remainder of the generated assembly into crtcommon.tmp. */ is shared between both crt files. */
asm ("\nEOF.crtn.fini\ asm ("\n/*@_fini_EPILOG_ENDS*/");
\n\ asm ("\n/*@TRAILER_BEGINS*/");
cat > crtcommon.tmp <<\\EOF_common"); asm ("ALIGN");
/* End of file. */