check_sys.sh 13 KB

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