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.
This commit is contained in:
Дмитрий Левин 2015-11-20 01:43:12 +00:00
parent 7528a0b2e1
commit 5cb45b25a3
8 changed files with 110 additions and 50 deletions

View File

@ -595,6 +595,7 @@ EXTRA_DIST = \
mpers.awk \
mpers.sh \
mpers_test.sh \
mpers_xlat.h \
signalent.sh \
strace-graph \
strace-log-merge \

View File

@ -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 "$@"

3
defs.h
View File

@ -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 */

View File

@ -1,11 +1,13 @@
#!/bin/sh -e
exec > mpers.am
echo "# Generated by $0; do not edit."
printf 'mpers_source_files = '
sed -n '/^strace_SOURCES[[:space:]]*=/,/^[[:space:]]*# end of strace_SOURCES/ s/^[[:space:]]*\([[:alnum:]][^.]*\.c\)[[:space:]]*\\$/\1/p' Makefile.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
tr '\n' ' ')"
cat > mpers.am <<EOF
# Generated by $0; do not edit.
mpers_source_files = $list
EOF
sed -n 's/^#[[:space:]]*include[[:space:]]*"xlat\/\([^."]\+\)\.h".*/extern const struct xlat \1[];/p' \
$list > mpers_xlat.h

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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,19 +67,72 @@ 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
'#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
@ -70,7 +140,7 @@ gen_header()
unconditional=1
;;
'#unterminated')
unterminated=1
# processed during 1st pass
;;
[A-Z_]*) # symbolic constants
if [ -n "${unconditional}" ]; then
@ -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}"
}

View File

@ -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