From 5cb45b25a37390afab4a62220ce148a2c135e595 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Fri, 20 Nov 2015 01:43:12 +0000 Subject: [PATCH] mpers: skip xlat struct definitions in mpers mode Avoid duplicate definitions of xlat structures in files compiled in mpers mode. Each xlat file defines the corresponding xlat struct with either global or local visibility using the following rules: - if xlat struct declaration is available in defs.h, a global definition is provided in regular mode, and nothing is provided in mpers mode; - otherwise, if xlat file is included by a mpers source file, a global definition is provided in regular mode (unless no mpers mode is supported on this architecture, in that case, a local definition is provided instead), and a declaration is provided in mpers mode; - otherwise, a local definition is provided in regular mode, and an error message is printed in mpers mode. Fallback definitions of constants provided by xlat files remain available in all modes. * bootstrap: Invoke generate_mpers_am.sh before xlat/gen.sh. * defs.h (adjtimex_modes, adjtimex_status, sigev_value): Remove. * generate_mpers_am.sh: Generate mpers_xlat.h. * Makefile.am (EXTRA_DIST): Add mpers_xlat.h. * print_sigevent.c: Include "xlat/sigev_value.h" unconditionally. * print_timex.c: Include "xlat/adjtimex_modes.h" and "xlat/adjtimex_status.h" unconditionally. * xlat/gen.sh (cond_xlat): Move printing of fallback definitions ... (cond_def): ... here. (gen_header): Check also mpers_xlat.h for global declarations of xlat structures. Process input file twice, first time print directives only, second time print everything. Wrap struct xlat into #ifdef IN_MPERS ... #else ... #endif. * xlat/getrandom_flags.in: Cleanup. --- Makefile.am | 1 + bootstrap | 2 +- defs.h | 3 -- generate_mpers_am.sh | 16 +++--- print_sigevent.c | 4 +- print_timex.c | 6 +-- xlat/gen.sh | 112 +++++++++++++++++++++++++++++++++------- xlat/getrandom_flags.in | 14 +---- 8 files changed, 109 insertions(+), 49 deletions(-) diff --git a/Makefile.am b/Makefile.am index a79d50c2..b74bf455 100644 --- a/Makefile.am +++ b/Makefile.am @@ -595,6 +595,7 @@ EXTRA_DIST = \ mpers.awk \ mpers.sh \ mpers_test.sh \ + mpers_xlat.h \ signalent.sh \ strace-graph \ strace-log-merge \ diff --git a/bootstrap b/bootstrap index 59d2480d..9b432d41 100755 --- a/bootstrap +++ b/bootstrap @@ -14,7 +14,7 @@ for m in -m32 -mx32; do done done -./xlat/gen.sh ./generate_mpers_am.sh +./xlat/gen.sh exec autoreconf -f -i "$@" diff --git a/defs.h b/defs.h index 5e40f4db..4aab7bbf 100644 --- a/defs.h +++ b/defs.h @@ -410,14 +410,11 @@ struct xlat { #define XLAT_END { 0, NULL } extern const struct xlat addrfams[]; -extern const struct xlat adjtimex_modes[]; -extern const struct xlat adjtimex_status[]; extern const struct xlat at_flags[]; extern const struct xlat dirent_types[]; extern const struct xlat open_access_modes[]; extern const struct xlat open_mode_flags[]; extern const struct xlat resource_flags[]; -extern const struct xlat sigev_value[]; extern const struct xlat whence_codes[]; /* Format of syscall return values */ diff --git a/generate_mpers_am.sh b/generate_mpers_am.sh index 5469c946..1ee4791b 100755 --- a/generate_mpers_am.sh +++ b/generate_mpers_am.sh @@ -1,11 +1,13 @@ #!/bin/sh -e -exec > mpers.am +list="$(sed -n '/^strace_SOURCES[[:space:]]*=/,/^[[:space:]]*# end of strace_SOURCES/ s/^[[:space:]]*\([[:alnum:]][^.]*\.c\)[[:space:]]*\\$/\1/p' Makefile.am | + xargs -r grep -lx '#[[:space:]]*include[[:space:]]\+MPERS_DEFS' | + tr '\n' ' ')" -echo "# Generated by $0; do not edit." -printf 'mpers_source_files = ' +cat > mpers.am < mpers_xlat.h diff --git a/print_sigevent.c b/print_sigevent.c index 5454486b..9ef655ae 100644 --- a/print_sigevent.c +++ b/print_sigevent.c @@ -35,9 +35,7 @@ typedef struct sigevent struct_sigevent; #include MPERS_DEFS -#ifndef IN_MPERS -# include "xlat/sigev_value.h" -#endif +#include "xlat/sigev_value.h" MPERS_PRINTER_DECL(void, print_sigevent)(struct tcb *tcp, const long addr) { diff --git a/print_timex.c b/print_timex.c index f961ecf2..4eac54df 100644 --- a/print_timex.c +++ b/print_timex.c @@ -37,10 +37,8 @@ typedef struct timex struct_timex; #include MPERS_DEFS -#ifndef IN_MPERS -# include "xlat/adjtimex_modes.h" -# include "xlat/adjtimex_status.h" -#endif +#include "xlat/adjtimex_modes.h" +#include "xlat/adjtimex_status.h" MPERS_PRINTER_DECL(int, print_timex)(struct tcb *tcp, const long addr) { diff --git a/xlat/gen.sh b/xlat/gen.sh index 8754a032..0f1adadc 100755 --- a/xlat/gen.sh +++ b/xlat/gen.sh @@ -11,6 +11,28 @@ EOF exit 1 } +cond_def() +{ + local line + line="$1"; shift + + local val + val="$(printf %s "$line" | + sed -n 's/^\([^[:space:]]\+\).*$/\1/p')" + + local def + def="$(printf %s "${line}" | + sed -n 's/^[^[:space:]]\+[[:space:]]\+\([^[:space:]].*\)$/\1/p')" + + if [ -n "$def" ]; then + cat <<-EOF + #if !(defined($val) || (defined(HAVE_DECL_$val) && HAVE_DECL_$val)) + # define $val $def + #endif + EOF + fi +} + cond_xlat() { local line val m def xlat @@ -22,7 +44,7 @@ cond_xlat() sed -n 's/^[^[:space:]]\+[[:space:]]\+\([^[:space:]].*\)$/\1/p')" if [ "${m}" = "${m#1<<}" ]; then - xlat="XLAT(${val})," + xlat=" XLAT(${val})," else m="${m#1<<}" xlat=" { ${val}, \"${m}\" }," @@ -35,12 +57,7 @@ cond_xlat() #endif EOF else - cat <<-EOF - #if !(defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m})) - # define ${m} ${def} - #endif - ${xlat} - EOF + echo "$xlat" fi } @@ -50,28 +67,81 @@ gen_header() echo "generating ${output}" ( local defs="${0%/*}/../defs.h" - local prefix - if grep -x "extern const struct xlat ${name}\\[\\];" "${defs}" > /dev/null; then - prefix= - else - prefix='static ' + local mpers="${0%/*}/../mpers_xlat.h" + local decl="extern const struct xlat ${name}[];" + local in_defs= in_mpers= + + if grep -F -x "$decl" "$defs" > /dev/null; then + in_defs=1 + elif grep -F -x "$decl" "$mpers" > /dev/null; then + in_mpers=1 fi - cat <<-EOF - /* Generated by $0 from $1; do not edit. */ + echo "/* Generated by $0 from $1; do not edit. */" - ${prefix}const struct xlat ${name}[] = { - EOF local unconditional= unterminated= line + # 1st pass: output directives. while read line; do LC_COLLATE=C - case ${line} in + case $line in '#unconditional') unconditional=1 ;; '#unterminated') unterminated=1 ;; + '#'*) + echo "${line}" + ;; + [A-Z_]*) + [ -n "$unconditional" ] || + cond_def "$line" + ;; + esac + done < "$input" + + echo + if [ -n "$in_defs" ]; then + cat <<-EOF + #ifndef IN_MPERS + + EOF + elif [ -n "$in_mpers" ]; then + cat <<-EOF + #ifdef IN_MPERS + + ${decl} + + #else + + # if !(defined HAVE_M32_MPERS || defined HAVE_MX32_MPERS) + static + # endif + EOF + else + cat <<-EOF + #ifdef IN_MPERS + + # error static const struct xlat ${name} in mpers mode + + #else + + static + EOF + fi + echo "const struct xlat ${name}[] = {" + + unconditional= + # 2nd pass: output everything. + while read line; do + LC_COLLATE=C + case ${line} in + '#unconditional') + unconditional=1 + ;; + '#unterminated') + # processed during 1st pass + ;; [A-Z_]*) # symbolic constants if [ -n "${unconditional}" ]; then echo " XLAT(${line})," @@ -80,7 +150,6 @@ gen_header() fi ;; '1<<'[A-Z_]*) # symbolic constants with shift - m="${line%% *}" if [ -n "${unconditional}" ]; then echo " { ${line}, \"${line#1<<}\" }," else @@ -100,7 +169,12 @@ gen_header() else echo " XLAT_END" fi - echo "};" + + cat <<-EOF + }; + + #endif /* !IN_MPERS */ + EOF ) >"${output}" } diff --git a/xlat/getrandom_flags.in b/xlat/getrandom_flags.in index 7b77c5ab..ad1a7580 100644 --- a/xlat/getrandom_flags.in +++ b/xlat/getrandom_flags.in @@ -1,12 +1,2 @@ -#unconditional - -#ifndef GRND_NONBLOCK -# define GRND_NONBLOCK 1 -#endif - -#ifndef GRND_RANDOM -# define GRND_RANDOM 2 -#endif - -GRND_NONBLOCK -GRND_RANDOM +GRND_NONBLOCK 1 +GRND_RANDOM 2 -- 2.40.0