check_sys.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #!/usr/bin/env bash
  2. # Script to test fpc to system syscall numbers
  3. # Location of syscall header in system
  4. syscall_header=/usr/include/syscall.h
  5. fpc_sysnr=./sysnr.inc
  6. i=1
  7. while [ $i -le "$#" ] ; do
  8. arg="${!i}"
  9. echo "Handling arg $i, \"$arg\""
  10. if [ "${arg//=}" != "$arg" ] ; then
  11. echo "Evaluating \"$arg\""
  12. arg2="${arg/=*/}=\"${arg/*=/}\""
  13. eval "$arg2"
  14. elif [ "$arg" == "-v" ] ; then
  15. verbose=1
  16. else
  17. echo "arg not handled!"
  18. fi
  19. let i++
  20. done
  21. start_pwd=`pwd`
  22. start_dir=`basename $start_pwd`
  23. if [ -d "rtl" ] ; then
  24. echo "Entering rtl directory"
  25. cd rtl
  26. fi
  27. os=`uname -s | tr [:upper:] [:lower:] `
  28. os_cpu=`uname -m | tr [:upper:] [:lower:] `
  29. now_pwd=`pwd`
  30. now_dir=`basename $now_pwd`
  31. if [ -d "$os" ] ; then
  32. echo "Entering $os directory"
  33. cd $os
  34. fi
  35. case "$os" in
  36. freebsd|openbsd|netbsd) c_syscall_header=sys/syscall.h;;
  37. *) c_syscall_header=syscall.h;;
  38. esac
  39. if [ -z "$FPC" ] ; then
  40. FPC=fpc
  41. fi
  42. if [ ! -f $fpc_sysnr ] ; then
  43. cpu=`$FPC -iTP`
  44. if [ "${cpu//sparc/}" != "$cpu" ] ; then
  45. cpu=sparcgen
  46. fi
  47. fpc_sysnr=./$cpu/sysnr.inc
  48. if [ ! -f "$fpc_sysnr" ] ; then
  49. fpc_sysnr=`ls -1 ./${cpu}*/sysnr.inc| head -1`
  50. fi
  51. if [ ! -f "$fpc_sysnr" ] ; then
  52. if [ "${cpu//sparc/}" != "$cpu" ] ; then
  53. cpu=sparcgen
  54. fi
  55. fpc_sysnr=./$cpu/sysnr.inc
  56. if [ ! -f "$fpc_sysnr" ] ; then
  57. echo "sysnr.inc file not found, try again in rtl/$os directory"
  58. exit
  59. fi
  60. fi
  61. fi
  62. if [ -f "$fpc_sysnr" ] ; then
  63. echo "Checking $fpc_sysnr content for Free Pascal syscall numbers"
  64. fpc_sysnr_dir=`dirname $fpc_sysnr `
  65. sysnr_includes=`grep -o '{\$i *[a-z_A-Z0-9/.-]*' $fpc_sysnr | sed 's:.*{\$i *:'$fpc_sysnr_dir/: `
  66. if [ -n "$sysnr_includes" ] ; then
  67. echo "Found $sysnr_includes include files"
  68. fpc_sysnr="$fpc_sysnr $sysnr_includes"
  69. fi
  70. fi
  71. if [ -z "$verbose" ] ; then
  72. verbose=0
  73. fi
  74. os=`uname -s`
  75. # Test C file to grab all loaded headers
  76. cat > test-syscall.c <<EOF
  77. #include <${c_syscall_header}>
  78. int
  79. main ()
  80. {
  81. return 0;
  82. }
  83. EOF
  84. # Default C compiler is gcc
  85. # Can be overwritten by setting CC variable
  86. # But I don't know if other compilers also generate
  87. # .i files with --save-temps option
  88. if [ -z "$CC" ] ; then
  89. CC=gcc
  90. fi
  91. cpu=`$FPC -iTP`
  92. cpu_source=`$FPC -iSP`
  93. is_16=0
  94. is_32=0
  95. is_64=0
  96. case $cpu in
  97. aarch64) is_64=1;;
  98. alpha) is_32=1;;
  99. arm) is_32=1;;
  100. avr) is_16=1;;
  101. i386) is_32=1;;
  102. i8086) is_16=1;;
  103. ia64) is_64=1;;
  104. jvm) is_32=1;;
  105. m68k) is_32=1;;
  106. mips) is_32=1;;
  107. mipsel) is_32=1;;
  108. powerpc) is_32=1;;
  109. powerpc64) is_64=1;;
  110. riscv32) is_32=1;;
  111. riscv64) is_64=1;;
  112. sparc) is_32=1;;
  113. sparc64) is_64=1;;
  114. vis) is_32=1;;
  115. x86_64) is_64=1;;
  116. esac
  117. if [ $is_64 -eq 1 ] ; then
  118. if [ "$os_cpu" == "aarch64" ] ; then
  119. CC_OPT="$CC_OPT -Wall"
  120. elif [ "$os_cpu" == "riscv64" ] ; then
  121. CC_OPT="$CC_OPT -mabi=lp64d"
  122. else
  123. CC_OPT="$CC_OPT -m64 -Wall"
  124. fi
  125. CPUBITS=64
  126. elif [ $is_32 -eq 1 ] ;then
  127. if [ "$os_cpu" == "aarch64" ] ; then
  128. CC=arm-linux-gnueabihf-gcc-4.8
  129. export BINUTILSPREFIX=arm-linux-
  130. fi
  131. if [ "${FPC/ppcarm/}" != "$FPC" ] ; then
  132. CC_OPT="$CC_OPT -march=armv7-a -Wall"
  133. elif [ "${os_cpu/arm/}" != "$os_cpu" ] ; then
  134. CC_OPT="$CC_OPT -march=armv5 -Wall"
  135. elif [ "${os_cpu/riscv/}" != "$os_cpu" ] ; then
  136. CC_OPT="$CC_OPT -mabi=ilp32 -Wall"
  137. else
  138. CC_OPT="$CC_OPT -m32 -Wall"
  139. fi
  140. CPUBITS=32
  141. elif [ $is_16 -eq 1 ] ; then
  142. CPUBITS=16
  143. else
  144. CPUBITS=unknown
  145. fi
  146. # Use gcc with --save-temps option to create .i file
  147. echo "Calling $CC $CC_OPT --save-temps -o test-syscall ./test-syscall.c"
  148. $CC $CC_OPT --save-temps -o ./test-syscall ./test-syscall.c
  149. res=$?
  150. if [ $res -ne 0 ] ; then
  151. echo "Call to $CC failed"
  152. exit
  153. else
  154. rm -f ./test-syscall.c ./test-syscall
  155. fi
  156. # list of errno.h headers listed
  157. syscall_headers=` sed -n "s:.*\"\(.*/.*\.h\)\".*:\1:p" test-syscall.i |sort | uniq`
  158. rm -f test-syscall.*
  159. echo "C syscall headers files found are \"$syscall_headers\""
  160. if [ -n "$syscall_headers" ] ; then
  161. syscall_header="$syscall_headers"
  162. fi
  163. fpc_syscall_prefix=syscall_nr_
  164. if [ "$os" == "Linux" ] ; then
  165. # On Linux system, system call number are defined indirectly
  166. # with #define SYS_XXX __NR_XXX
  167. # We look directly for the __NT_ version
  168. syscall_prefix=__NR_
  169. else
  170. syscall_prefix=SYS_
  171. fi
  172. # You should only need to change the variables above
  173. awkfile=preproc.awk
  174. tmp_fpc_sysnr=tmp-sysnr-${cpu}.inc
  175. c_syscall_source=test-syscall-${cpu}.c
  176. # Test C file to grab all loaded headers
  177. # must be called with -DSYS_MACRO=$sys
  178. cat > $c_syscall_source <<EOF
  179. #include <${c_syscall_header}>
  180. #include <stdio.h>
  181. int
  182. main ()
  183. {
  184. printf ("%d\n", (int) SYS_MACRO);
  185. return 0;
  186. }
  187. EOF
  188. cat > $awkfile <<EOF
  189. BEGIN {IGNORECASE = 1;
  190. enable=1;
  191. macro="";
  192. incfile="";
  193. cpu= "cpu" proc;
  194. cpubits= "cpu" cpubits;
  195. list_defines=macros " " cpu " " cpubits " ";
  196. print "// FPC defined macros used " list_defines;
  197. }
  198. /\{\\\$i / { incfile=\$2;
  199. print "Include file " incfile " found"; }
  200. /\{\\\$ifdef / { macro=gensub("[^A-Za-z_0-9].*","",1,\$2) " ";
  201. if (list_defines ~ macro) { enable=1;
  202. print "// ifdef " macro " found and accepted at line " FNR;
  203. } else {enable=0;
  204. print "// ifdef " macro " found and rejected at line " FNR;
  205. };
  206. }
  207. /\{\\\$ifndef / { macro=gensub("[^A-Za-z_0-9].*","",1,\$2);
  208. if (list_defines ~ macro) { enable=0;
  209. print "// ifndef " macro " found and rejected at line " FNR;
  210. } else {enable=1;
  211. print "// ifndef " macro " found and accepted at line " FNR;
  212. };
  213. }
  214. /\{\\\$else/ { if (enable == 1) {enable=0;} else {enable = 1;}}
  215. /.*/ { if (enable == 1) {
  216. wholeline=\$0;
  217. code=gensub("{.*}","","g",\$0);
  218. code=gensub("[(][*].*[*][)]","","g",code);
  219. comments=gensub(code,"",1,\$0);
  220. comments1=gensub(".*({.*}).*","\1","g",comments);
  221. if (comments == comments1)
  222. comments1="";
  223. comments2=gensub(".*[(][*].*[*][)]).*","\1","g",comments);
  224. if (comments == comments2)
  225. comments2="";
  226. comments3=gensub(".*//","",1,comments);
  227. if (comments == comments3)
  228. comments3="";
  229. all_comments= comments1 comments2 comments3;
  230. if (all_comments != "")
  231. print code "// " comments1 comments2 comments3 ;
  232. else
  233. print code;
  234. }
  235. }
  236. /\{\\\$endif/ {enable=1;}
  237. EOF
  238. if [ -z "$AWK" ] ; then
  239. AWK=`which gawk 2> /dev/null`
  240. fi
  241. if [ -z "$AWK" ] ; then
  242. AWK=`which awk 2> /dev/null`
  243. fi
  244. if [ -n "$AWK" ] ; then
  245. echo "Preprocessing ${fpc_sysnr} to $tmp_fpc_sysnr"
  246. echo "$AWK -v proc=$cpu -v cpubits=$CPUBITS -f $awkfile ${fpc_sysnr} > $tmp_fpc_sysnr"
  247. $AWK -v proc=$cpu -v cpubits=$CPUBITS -v macros="$FPC_MACROS" -f $awkfile ${fpc_sysnr} > $tmp_fpc_sysnr
  248. fpc_sysnr=$tmp_fpc_sysnr
  249. fi
  250. sed -n "s:^\(.*\)*[ \t]*${fpc_syscall_prefix}\\([_a-zA-Z0-9]*\\)[ \t]*=[ \t]*\\(.*\\);\\(.*\\)$:check_c_syscall_number_from_fpc_rtl \2 \"\3\" \"\1 \4\":p" $fpc_sysnr > check_sys_list.sh
  251. sed -n "s:^.*#[[:space:]]*define[[:space:]]*${syscall_prefix}\\([_a-zA-Z0-9]*\\)[[:space:]]*\\([0-9]*\\)\\(.*\\)$:check_c_syscall_number_in_fpc_rtl \1 \"\2\" \"\3\":p" ${syscall_header} > check_sys_list_reverse.sh
  252. forward_count=0
  253. forward_ok_count=0
  254. forward_failure_count=0
  255. function check_c_syscall_number_from_fpc_rtl ()
  256. {
  257. bare_sys=$1
  258. sys=${syscall_prefix}$bare_sys
  259. arg_2=\"$2\"
  260. if [ "${2:0:1}" == "$" ] ; then
  261. echo "Arg \"$arg_2\" needs Pascal To C hexadecimal conversion"
  262. let "value=0x${arg_2:2}"
  263. else
  264. let "value=$2"
  265. fi
  266. comment="$3"
  267. if [[ ! ( ( -n "$value" ) && ( $value -ge 0 ) ) ]] ; then
  268. echo "Computing $2 value"
  269. let value=$2
  270. fi
  271. obsolete=0
  272. let forward_count++
  273. if [[ "$value" =~ ^[0-9]+$ ]] ; then
  274. eval $sys=\$$value
  275. if [ $verbose -ne 0 ] ; then
  276. echo "$sys is $value"
  277. fi
  278. else
  279. eval $sys=$value
  280. if [ $verbose -ne 0 ] ; then
  281. echo "$sys set to \"${$sys}\" trough \"$value\""
  282. fi
  283. fi
  284. # Remember this value for later
  285. eval $sys=$value
  286. echo -en "Testing $sys value $value \r"
  287. found=`sed -n "/#[[:space:]]*define[[:space:]]*${sys}[^A-Za-z0-9_]/p" ${syscall_header}`
  288. val=`sed -n "s:#[[:space:]]*define[[:space:]]*${sys}[^A-Za-z0-9_][^A-Za-z0-9_]*\([0-9]*\).*:\1:p" ${syscall_header}`
  289. $CC $CC_OPT -DSYS_MACRO=${syscall_prefix}${bare_sys} -o ./test_c_${bare_sys} $c_syscall_source > ./test_${bare_sys}.comp-log 2>&1
  290. C_COMP_RES=$?
  291. if [ $C_COMP_RES -eq 0 ] ; then
  292. CC_value=`./test_c_${bare_sys} `
  293. if [ "$value" != "$CC_value" ] ; then
  294. echo "$CC returns $CC_value, while $value is expected"
  295. let forward_failure_count++
  296. return
  297. else
  298. val=$CC_value
  299. rm -f ./test_c_${bare_sys}
  300. fi
  301. rm -f ./test-${bare_sys}.comp-log
  302. else
  303. echo "$CC failed to compile code containing $sys syscall number $value"
  304. echo "$CC $CC_OPT -DSYS_MACRO=${syscall_prefix}${bare_sys} -o ./test_c_${bare_sys} $c_syscall_source > ./test_${bare_sys}.comp-log 2>&1"
  305. let forward_failure_count++
  306. return
  307. fi
  308. if [ $verbose -ne 0 ] ; then
  309. echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  310. fi
  311. if [ "${val}" == "${value}" ] ; then
  312. if [ $verbose -ne 0 ] ; then
  313. echo ${sys} value ${val} is correct
  314. fi
  315. let forward_ok_count++
  316. else
  317. if [ -z "${val}" ] ; then
  318. found=`sed -n ".*define[[:space:]].*[^A-Za-z0-9_][[:space:]]${value}$/p" ${syscall_header}`
  319. if [ -z "${found}" ] ; then
  320. found=`sed -n "s:\/\* ${value} is compa:/* ${value} is compa:p" ${syscall_header}`
  321. if [ -n "$found" ] ; then
  322. obsolete=1
  323. fi
  324. fi
  325. fi
  326. if [ -z "$found" ] ; then
  327. found=`grep -n -w $value ${syscall_header}`
  328. fi
  329. if [ $obsolete -eq 1 ] ; then
  330. echo Warning: ${bare_sys} expected ${value}, is obsolete line is \"${found}\"
  331. else
  332. echo Problem: ${bare_sys} expected ${value}, line is \"${found}\", val found is \"${val}\"
  333. fi
  334. let forward_failure_count++
  335. fi
  336. }
  337. reverse_count=0
  338. reverse_ok_count=0
  339. reverse_failure_count=0
  340. add_file=./add_missing_syscalls.inc
  341. suggested_addition_count=0
  342. echo "{ Generated by check_rtl_sys.sh script }" > $add_file
  343. function check_c_syscall_number_in_fpc_rtl ()
  344. {
  345. bare_sys=$1
  346. sys=${fpc_syscall_prefix}${bare_sys}
  347. c_sys=${syscall_prefix}${bare_sys}
  348. value=$2
  349. if [ -z "$value" ] ; then
  350. let "value=$3"
  351. comment="expression $3"
  352. else
  353. comment="$3"
  354. fi
  355. echo -en "Testing $sys value $value \r"
  356. $CC $CC_OPT -DSYS_MACRO=${c_sys} -o ./test_c_${bare_sys} $c_syscall_source > ./test_${bare_sys}.comp-log 2>&1
  357. C_COMP_RES=$?
  358. if [ $C_COMP_RES -eq 0 ] ; then
  359. rm ./test_${bare_sys}.comp-log
  360. CC_value=`./test_c_${bare_sys} `
  361. if [ "$value" != "$CC_value" ] ; then
  362. echo "For sys=$sys, $CC returns $CC_value, while $value is expected"
  363. let reverse_failure_count++
  364. return
  365. else
  366. rm -f ./test_c_$bare_sys
  367. fi
  368. else
  369. # if C syscall is not accepted do nothing
  370. #echo "For sys=$sys, $CC compilation failed"
  371. #cat ./test_${bare_sys}.comp-log
  372. # let reverse_failure_count++
  373. rm -f ./test_c_${bare_sys}
  374. rm ./test_${bare_sys}.comp-log
  375. return
  376. fi
  377. if [ $verbose -ne 0 ] ; then
  378. echo "Full comment is \"$comment \""
  379. fi
  380. if [ "${comment/*\/\*/}" != "$comment" ] ; then
  381. comment="${comment/*\/\*/}"
  382. if [ $verbose -ne 0 ] ; then
  383. echo "comment is \"$comment \""
  384. fi
  385. comment="${comment/\*\/*/}"
  386. if [ $verbose -ne 0 ] ; then
  387. echo "comment is \"$comment \""
  388. fi
  389. comment=`echo $comment | sed 's:^[[:space:]]*\(.*\)[[:space:]]*$:\1' `
  390. if [ $verbose -ne 0 ] ; then
  391. echo "comment is \"$comment \""
  392. fi
  393. fi
  394. if [ $verbose -ne 0 ] ; then
  395. echo Testing syscall header entry $sys value $value
  396. fi
  397. let reverse_count++
  398. found=`sed -n "/.*${sys}/p" ${fpc_sysnr}`
  399. val=`sed -n "s:.*${sys}[ \t]*=[ \t]*\([0-9]*\).*:\1:p" ${fpc_sysnr}`
  400. if [ $verbose -ne 0 ] ; then
  401. echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  402. fi
  403. if [ "${val}" == "${value}" ] ; then
  404. if [ $verbose -ne 0 ] ; then
  405. echo ${sys} value ${val} is correct
  406. fi
  407. let reverse_ok_count++
  408. else
  409. if [ -z "${val}" ] ; then
  410. found=`sed -n "/#[[:space:]]*define.*[^A-Za-z0-9_][[:space:]]*${value}([[:space:]]|$)/p" ${syscall_header}`
  411. if [ -z "${found}" ] ; then
  412. found=`sed -n "s:\/\*.*i[[:space:]]${value} is compa: ${value} is compa:p" ${syscall_header}`
  413. fi
  414. fi
  415. echo "Problem: ${bare_sys} expected ${value}, line is \"${found}\", val found is \"${val}\""
  416. if [ -n "$comment" ] ; then
  417. echo " ${fpc_syscall_prefix}${bare_sys} = ${value}; { $comment }" >> $add_file
  418. echo "Suggest adding: ${fpc_syscall_prefix}${bare_sys} = ${value}; { $comment }"
  419. else
  420. echo " ${fpc_syscall_prefix}${bare_sys} = ${value};" >> $add_file
  421. echo "Suggest adding: ${fpc_syscall_prefix}${bare_sys} = ${value};"
  422. fi
  423. let suggested_addition_count++
  424. let reverse_failure_count++
  425. fi
  426. }
  427. # Sustitution made to pass from fpc syscall number
  428. # to system define
  429. set -f
  430. echo "Checking values from \"${fpc_sysnr}\" in C syscall headers"
  431. source ./check_sys_list.sh
  432. echo "Checking if values in C syscall headers are in \"${fpc_sysnr}\""
  433. source ./check_sys_list_reverse.sh
  434. echo "Forward counts: OK=$forward_ok_count, failures=$forward_failure_count, total=$forward_count"
  435. echo "Reverse counts: OK=$reverse_ok_count, failures=$reverse_failure_count, total=$reverse_count"
  436. if [ $suggested_addition_count -gt 0 ] ; then
  437. echo "Missing $suggested_addition_count syscall numbers in $add_file"
  438. else
  439. rm $add_file
  440. fi
  441. rm ./check_sys_list.sh ./check_sys_list_reverse.sh ./$awkfile
  442. if [ -f "$tmp_fpc_sysnr" ] ; then
  443. echo rm $tmp_fpc_sysnr
  444. fi