check_sys.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. else
  121. CC_OPT="$CC_OPT -m64 -Wall"
  122. fi
  123. CPUBITS=64
  124. elif [ $is_32 -eq 1 ] ;then
  125. if [ "$os_cpu" == "aarch64" ] ; then
  126. CC=arm-linux-gnueabihf-gcc-4.8
  127. export BINUTILSPREFIX=arm-linux-
  128. fi
  129. if [ "${FPC/ppcarm/}" != "$FPC" ] ; then
  130. CC_OPT="$CC_OPT -march=armv7-a -Wall"
  131. elif [ "${os_cpu/arm/}" != "$os_cpu" ] ; then
  132. CC_OPT="$CC_OPT -march=armv5 -Wall"
  133. else
  134. CC_OPT="$CC_OPT -m32 -Wall"
  135. fi
  136. CPUBITS=32
  137. elif [ $is_16 -eq 1 ] ; then
  138. CPUBITS=16
  139. else
  140. CPUBITS=unknown
  141. fi
  142. # Use gcc with --save-temps option to create .i file
  143. echo "Calling $CC $CC_OPT --save-temps -o test-syscall ./test-syscall.c"
  144. $CC $CC_OPT --save-temps -o ./test-syscall ./test-syscall.c
  145. res=$?
  146. if [ $res -ne 0 ] ; then
  147. echo "Call to $CC failed"
  148. exit
  149. else
  150. rm -f ./test-syscall.c ./test-syscall
  151. fi
  152. # list of errno.h headers listed
  153. syscall_headers=` sed -n "s:.*\"\(.*/.*\.h\)\".*:\1:p" test-syscall.i |sort | uniq`
  154. rm -f test-syscall.*
  155. echo "C syscall headers files found are \"$syscall_headers\""
  156. if [ -n "$syscall_headers" ] ; then
  157. syscall_header="$syscall_headers"
  158. fi
  159. fpc_syscall_prefix=syscall_nr_
  160. if [ "$os" == "Linux" ] ; then
  161. # On Linux system, system call number are defined indirectly
  162. # with #define SYS_XXX __NR_XXX
  163. # We look directly for the __NT_ version
  164. syscall_prefix=__NR_
  165. else
  166. syscall_prefix=SYS_
  167. fi
  168. # You should only need to change the variables above
  169. awkfile=preproc.awk
  170. tmp_fpc_sysnr=tmp-sysnr-${cpu}.inc
  171. c_syscall_source=test-syscall-${cpu}.c
  172. # Test C file to grab all loaded headers
  173. # must be called with -DSYS_MACRO=$sys
  174. cat > $c_syscall_source <<EOF
  175. #include <${c_syscall_header}>
  176. #include <stdio.h>
  177. int
  178. main ()
  179. {
  180. printf ("%d\n", (int) SYS_MACRO);
  181. return 0;
  182. }
  183. EOF
  184. cat > $awkfile <<EOF
  185. BEGIN {IGNORECASE = 1;
  186. enable=1;
  187. macro="";
  188. incfile="";
  189. cpu= "cpu" proc;
  190. cpubits= "cpu" cpubits;
  191. list_defines=macros " " cpu " " cpubits " ";
  192. print "// FPC defined macros used " list_defines;
  193. }
  194. /\{\\\$i / { incfile=\$2;
  195. print "Include file " incfile " found"; }
  196. /\{\\\$ifdef / { macro=gensub("[^A-Za-z_0-9].*","",1,\$2) " ";
  197. if (list_defines ~ macro) { enable=1;
  198. print "// ifdef " macro " found and accepted at line " FNR;
  199. } else {enable=0;
  200. print "// ifdef " macro " found and rejected at line " FNR;
  201. };
  202. }
  203. /\{\\\$ifndef / { macro=gensub("[^A-Za-z_0-9].*","",1,\$2);
  204. if (list_defines ~ macro) { enable=0;
  205. print "// ifndef " macro " found and rejected at line " FNR;
  206. } else {enable=1;
  207. print "// ifndef " macro " found and accepted at line " FNR;
  208. };
  209. }
  210. /\{\\\$else/ { if (enable == 1) {enable=0;} else {enable = 1;}}
  211. /.*/ { if (enable == 1) {
  212. wholeline=\$0;
  213. code=gensub("{.*}","","g",\$0);
  214. code=gensub("[(][*].*[*][)]","","g",code);
  215. comments=gensub(code,"",1,\$0);
  216. comments1=gensub(".*({.*}).*","\1","g",comments);
  217. if (comments == comments1)
  218. comments1="";
  219. comments2=gensub(".*[(][*].*[*][)]).*","\1","g",comments);
  220. if (comments == comments2)
  221. comments2="";
  222. comments3=gensub(".*//","",1,comments);
  223. if (comments == comments3)
  224. comments3="";
  225. all_comments= comments1 comments2 comments3;
  226. if (all_comments != "")
  227. print code "// " comments1 comments2 comments3 ;
  228. else
  229. print code;
  230. }
  231. }
  232. /\{\\\$endif/ {enable=1;}
  233. EOF
  234. if [ -z "$AWK" ] ; then
  235. AWK=`which gawk 2> /dev/null`
  236. fi
  237. if [ -z "$AWK" ] ; then
  238. AWK=`which awk 2> /dev/null`
  239. fi
  240. if [ -n "$AWK" ] ; then
  241. echo "Preprocessing ${fpc_sysnr} to $tmp_fpc_sysnr"
  242. echo "$AWK -v proc=$cpu -v cpubits=$CPUBITS -f $awkfile ${fpc_sysnr} > $tmp_fpc_sysnr"
  243. $AWK -v proc=$cpu -v cpubits=$CPUBITS -v macros="$FPC_MACROS" -f $awkfile ${fpc_sysnr} > $tmp_fpc_sysnr
  244. fpc_sysnr=$tmp_fpc_sysnr
  245. fi
  246. 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
  247. 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
  248. forward_count=0
  249. forward_ok_count=0
  250. forward_failure_count=0
  251. function check_c_syscall_number_from_fpc_rtl ()
  252. {
  253. bare_sys=$1
  254. sys=${syscall_prefix}$bare_sys
  255. arg_2=\"$2\"
  256. if [ "${2:0:1}" == "$" ] ; then
  257. echo "Arg \"$arg_2\" needs Pascal To C hexadecimal conversion"
  258. let "value=0x${arg_2:2}"
  259. else
  260. let "value=$2"
  261. fi
  262. comment="$3"
  263. if [[ ! ( ( -n "$value" ) && ( $value -ge 0 ) ) ]] ; then
  264. echo "Computing $2 value"
  265. let value=$2
  266. fi
  267. obsolete=0
  268. let forward_count++
  269. if [[ "$value" =~ ^[0-9]+$ ]] ; then
  270. eval $sys=\$$value
  271. if [ $verbose -ne 0 ] ; then
  272. echo "$sys is $value"
  273. fi
  274. else
  275. eval $sys=$value
  276. if [ $verbose -ne 0 ] ; then
  277. echo "$sys set to \"${$sys}\" trough \"$value\""
  278. fi
  279. fi
  280. # Remember this value for later
  281. eval $sys=$value
  282. echo -en "Testing $sys value $value \r"
  283. found=`sed -n "/#[[:space:]]*define[[:space:]]*${sys}[^A-Za-z0-9_]/p" ${syscall_header}`
  284. val=`sed -n "s:#[[:space:]]*define[[:space:]]*${sys}[^A-Za-z0-9_][^A-Za-z0-9_]*\([0-9]*\).*:\1:p" ${syscall_header}`
  285. $CC $CC_OPT -DSYS_MACRO=${syscall_prefix}${bare_sys} -o ./test_c_${bare_sys} $c_syscall_source > ./test_${bare_sys}.comp-log 2>&1
  286. C_COMP_RES=$?
  287. if [ $C_COMP_RES -eq 0 ] ; then
  288. CC_value=`./test_c_${bare_sys} `
  289. if [ "$value" != "$CC_value" ] ; then
  290. echo "$CC returns $CC_value, while $value is expected"
  291. let forward_failure_count++
  292. return
  293. else
  294. val=$CC_value
  295. rm -f ./test_c_${bare_sys}
  296. fi
  297. rm -f ./test-${bare_sys}.comp-log
  298. else
  299. echo "$CC failed to compile code containing $sys syscall number $value"
  300. 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"
  301. let forward_failure_count++
  302. return
  303. fi
  304. if [ $verbose -ne 0 ] ; then
  305. echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  306. fi
  307. if [ "${val}" == "${value}" ] ; then
  308. if [ $verbose -ne 0 ] ; then
  309. echo ${sys} value ${val} is correct
  310. fi
  311. let forward_ok_count++
  312. else
  313. if [ -z "${val}" ] ; then
  314. found=`sed -n ".*define[[:space:]].*[^A-Za-z0-9_][[:space:]]${value}$/p" ${syscall_header}`
  315. if [ -z "${found}" ] ; then
  316. found=`sed -n "s:\/\* ${value} is compa:/* ${value} is compa:p" ${syscall_header}`
  317. if [ -n "$found" ] ; then
  318. obsolete=1
  319. fi
  320. fi
  321. fi
  322. if [ -z "$found" ] ; then
  323. found=`grep -n -w $value ${syscall_header}`
  324. fi
  325. if [ $obsolete -eq 1 ] ; then
  326. echo Warning: ${bare_sys} expected ${value}, is obsolete line is \"${found}\"
  327. else
  328. echo Problem: ${bare_sys} expected ${value}, line is \"${found}\", val found is \"${val}\"
  329. fi
  330. let forward_failure_count++
  331. fi
  332. }
  333. reverse_count=0
  334. reverse_ok_count=0
  335. reverse_failure_count=0
  336. add_file=./add_missing_syscalls.inc
  337. suggested_addition_count=0
  338. echo "{ Generated by check_rtl_sys.sh script }" > $add_file
  339. function check_c_syscall_number_in_fpc_rtl ()
  340. {
  341. bare_sys=$1
  342. sys=${fpc_syscall_prefix}${bare_sys}
  343. c_sys=${syscall_prefix}${bare_sys}
  344. value=$2
  345. if [ -z "$value" ] ; then
  346. let "value=$3"
  347. comment="expression $3"
  348. else
  349. comment="$3"
  350. fi
  351. echo -en "Testing $sys value $value \r"
  352. $CC $CC_OPT -DSYS_MACRO=${c_sys} -o ./test_c_${bare_sys} $c_syscall_source > ./test_${bare_sys}.comp-log 2>&1
  353. C_COMP_RES=$?
  354. if [ $C_COMP_RES -eq 0 ] ; then
  355. rm ./test_${bare_sys}.comp-log
  356. CC_value=`./test_c_${bare_sys} `
  357. if [ "$value" != "$CC_value" ] ; then
  358. echo "For sys=$sys, $CC returns $CC_value, while $value is expected"
  359. let reverse_failure_count++
  360. return
  361. else
  362. rm -f ./test_c_$bare_sys
  363. fi
  364. else
  365. # if C syscall is not accepted do nothing
  366. #echo "For sys=$sys, $CC compilation failed"
  367. #cat ./test_${bare_sys}.comp-log
  368. # let reverse_failure_count++
  369. rm -f ./test_c_${bare_sys}
  370. rm ./test_${bare_sys}.comp-log
  371. return
  372. fi
  373. if [ $verbose -ne 0 ] ; then
  374. echo "Full comment is \"$comment \""
  375. fi
  376. if [ "${comment/*\/\*/}" != "$comment" ] ; then
  377. comment="${comment/*\/\*/}"
  378. if [ $verbose -ne 0 ] ; then
  379. echo "comment is \"$comment \""
  380. fi
  381. comment="${comment/\*\/*/}"
  382. if [ $verbose -ne 0 ] ; then
  383. echo "comment is \"$comment \""
  384. fi
  385. comment=`echo $comment | sed 's:^[[:space:]]*\(.*\)[[:space:]]*$:\1' `
  386. if [ $verbose -ne 0 ] ; then
  387. echo "comment is \"$comment \""
  388. fi
  389. fi
  390. if [ $verbose -ne 0 ] ; then
  391. echo Testing syscall header entry $sys value $value
  392. fi
  393. let reverse_count++
  394. found=`sed -n "/.*${sys}/p" ${fpc_sysnr}`
  395. val=`sed -n "s:.*${sys}[ \t]*=[ \t]*\([0-9]*\).*:\1:p" ${fpc_sysnr}`
  396. if [ $verbose -ne 0 ] ; then
  397. echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  398. fi
  399. if [ "${val}" == "${value}" ] ; then
  400. if [ $verbose -ne 0 ] ; then
  401. echo ${sys} value ${val} is correct
  402. fi
  403. let reverse_ok_count++
  404. else
  405. if [ -z "${val}" ] ; then
  406. found=`sed -n "/#[[:space:]]*define.*[^A-Za-z0-9_][[:space:]]*${value}([[:space:]]|$)/p" ${syscall_header}`
  407. if [ -z "${found}" ] ; then
  408. found=`sed -n "s:\/\*.*i[[:space:]]${value} is compa: ${value} is compa:p" ${syscall_header}`
  409. fi
  410. fi
  411. echo "Problem: ${bare_sys} expected ${value}, line is \"${found}\", val found is \"${val}\""
  412. if [ -n "$comment" ] ; then
  413. echo " ${fpc_syscall_prefix}${bare_sys} = ${value}; { $comment }" >> $add_file
  414. echo "Suggest adding: ${fpc_syscall_prefix}${bare_sys} = ${value}; { $comment }"
  415. else
  416. echo " ${fpc_syscall_prefix}${bare_sys} = ${value};" >> $add_file
  417. echo "Suggest adding: ${fpc_syscall_prefix}${bare_sys} = ${value};"
  418. fi
  419. let suggested_addition_count++
  420. let reverse_failure_count++
  421. fi
  422. }
  423. # Sustitution made to pass from fpc syscall number
  424. # to system define
  425. set -f
  426. echo "Checking values from \"${fpc_sysnr}\" in C syscall headers"
  427. source ./check_sys_list.sh
  428. echo "Checking if values in C syscall headers are in \"${fpc_sysnr}\""
  429. source ./check_sys_list_reverse.sh
  430. echo "Forward counts: OK=$forward_ok_count, failures=$forward_failure_count, total=$forward_count"
  431. echo "Reverse counts: OK=$reverse_ok_count, failures=$reverse_failure_count, total=$reverse_count"
  432. if [ $suggested_addition_count -gt 0 ] ; then
  433. echo "Missing $suggested_addition_count syscall numbers in $add_file"
  434. else
  435. rm $add_file
  436. fi
  437. rm ./check_sys_list.sh ./check_sys_list_reverse.sh ./$awkfile
  438. if [ -f "$tmp_fpc_sysnr" ] ; then
  439. echo rm $tmp_fpc_sysnr
  440. fi