objcopy.exp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. # Copyright (C) 1994-2015 Free Software Foundation, Inc.
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  15. # Please email any bugs, comments, and/or additions to this file to:
  16. # bug-dejagnu@prep.ai.mit.edu
  17. # Written by Ian Lance Taylor <ian@cygnus.com>
  18. if ![is_remote host] {
  19. if {[which $OBJCOPY] == 0} then {
  20. perror "$OBJCOPY does not exist"
  21. return
  22. }
  23. }
  24. send_user "Version [binutil_version $OBJCOPY]"
  25. if ![is_remote host] {
  26. set tempfile tmpdir/bintest.o
  27. set copyfile tmpdir/copy
  28. } else {
  29. set tempfile [remote_download host tmpdir/bintest.o]
  30. set copyfile copy
  31. }
  32. # Test that objcopy does not modify a file when copying it.
  33. proc objcopy_test {testname srcfile} {
  34. global OBJCOPY
  35. global OBJCOPYFLAGS
  36. global srcdir
  37. global subdir
  38. global tempfile
  39. global copyfile
  40. if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
  41. perror "unresolved $testname"
  42. unresolved "objcopy ($testname)"
  43. return
  44. }
  45. set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
  46. if ![string equal "" $got] then {
  47. fail "objcopy ($testname)"
  48. } else {
  49. send_log "cmp $tempfile ${copyfile}.o\n"
  50. verbose "cmp $tempfile ${copyfile}.o"
  51. if [is_remote host] {
  52. set src1 tmpdir/bintest.o
  53. set src2 tmpdir/copy.o
  54. remote_upload host $tempfile $src1
  55. remote_upload host ${copyfile}.o $src2
  56. } else {
  57. set src1 ${tempfile}
  58. set src2 ${copyfile}.o
  59. }
  60. set status [remote_exec build cmp "${src1} ${src2}"]
  61. set exec_output [lindex $status 1]
  62. set exec_output [prune_warnings $exec_output]
  63. # On some systems the result of objcopy will not be identical.
  64. # Usually this is just because gas isn't using bfd to write the
  65. # files in the first place, and may order things a little
  66. # differently. Those systems should use setup_xfail here.
  67. setup_xfail "h8300-*-coff"
  68. setup_xfail "h8500-*-rtems*" "h8500-*-coff"
  69. setup_xfail "hppa*-*-*"
  70. setup_xfail "i960-*"
  71. setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
  72. setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
  73. setup_xfail "m8*-*"
  74. setup_xfail "sh-*-coff*"
  75. setup_xfail "tic80-*-*" "w65-*"
  76. clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
  77. clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
  78. clear_xfail "m68*-*-sysv4*"
  79. if [string equal "" $exec_output] then {
  80. pass "objcopy ($testname)"
  81. } else {
  82. send_log "$exec_output\n"
  83. verbose "$exec_output" 1
  84. # On OSF/1, this succeeds with gas and fails with /bin/as.
  85. setup_xfail "alpha*-*-osf*"
  86. fail "objcopy ($testname)"
  87. }
  88. }
  89. }
  90. objcopy_test "simple copy" bintest.s
  91. # Test reversing bytes in a section.
  92. set reversed ${tempfile}-reversed
  93. set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
  94. if ![string equal "" $got] then {
  95. fail "objcopy --reverse-bytes"
  96. } else {
  97. if [is_remote host] {
  98. remote_upload host ${reversed} tmpdir/copy-reversed.o
  99. set reversed tmpdir/copy-reversed.o
  100. }
  101. set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
  102. set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
  103. set want "^ \[0-9\]+ (\[0-9\]+)"
  104. set found_orig [regexp -lineanchor $want $origdata -> origdata]
  105. set found_rev [regexp -lineanchor $want $revdata -> revdata]
  106. if {$found_orig == 0 || $found_rev == 0} then {
  107. # som doesn't have a .data section
  108. setup_xfail "hppa*-*-hpux*"
  109. clear_xfail "hppa*64*-*-hpux*"
  110. fail "objcopy --reverse-bytes"
  111. } else {
  112. scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
  113. scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
  114. if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
  115. pass "objcopy --reverse-bytes"
  116. } else {
  117. fail "objcopy --reverse-bytes"
  118. }
  119. }
  120. }
  121. # Test interleaved copy of multiple byte width
  122. set sequence_file sequence_file
  123. set file [open ${sequence_file} w]
  124. puts ${file} "12345678"
  125. close ${file}
  126. if [is_remote host] {
  127. remote_upload host ${sequence_file} tmpdir/sequence_file
  128. set sequence_file tmpdir/sequence_file
  129. }
  130. set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
  131. if ![string equal "" $got] then {
  132. fail "objcopy -i --interleave-width"
  133. } else {
  134. if [is_remote host] {
  135. remote_upload host ${copyfile} tmpdir/interleave_output
  136. set interleave_output tmpdir/interleave_output
  137. } else {
  138. set interleave_output ${copyfile}
  139. }
  140. set file [open ${interleave_output} r]
  141. gets $file line
  142. send_log "$line\n"
  143. verbose $line
  144. if ![string equal "1256" $line] then {
  145. fail "objcopy -i --interleave-width"
  146. }
  147. pass "objcopy -i --interleave-width"
  148. close $file
  149. }
  150. # Test generating S records.
  151. # We make the srec filename 8.3 compatible. Note that the header string
  152. # matched against depends on the name of the file. Ugh.
  153. if [is_remote host] {
  154. set srecfile copy.sre
  155. set header_string S00B0000636F70792E737265C1
  156. } else {
  157. set srecfile ${copyfile}.srec
  158. set header_string S0130000746D706469722F636F70792E7372656397
  159. }
  160. set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
  161. if ![string equal "" $got] then {
  162. fail "objcopy -O srec"
  163. } else {
  164. if [is_remote host] {
  165. remote_upload host ${srecfile} tmpdir/copy.srec
  166. set srecfile tmpdir/copy.srec
  167. }
  168. set file [open ${srecfile} r]
  169. # The first S record is fixed by the file name we are using.
  170. gets $file line
  171. send_log "$line\n"
  172. verbose $line
  173. if ![regexp "$header_string.*" $line] {
  174. send_log "bad header\n"
  175. fail "objcopy -O srec"
  176. } else {
  177. while {[gets $file line] != -1 \
  178. && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
  179. send_log "$line\n"
  180. verbose $line
  181. set line "**EOF**"
  182. }
  183. send_log "$line\n"
  184. verbose $line
  185. if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
  186. send_log "bad trailer\n"
  187. fail "objcopy -O srec"
  188. } else {
  189. if {[gets $file line] != -1} then {
  190. send_log "garbage at end\n"
  191. send_log "$line\n"
  192. verbose $line
  193. fail "objcopy -O srec"
  194. } else {
  195. set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
  196. if ![regexp "file format srec" $got] then {
  197. send_log "objdump failed\n"
  198. fail "objcopy -O srec"
  199. } else {
  200. pass "objcopy -O srec"
  201. }
  202. }
  203. }
  204. }
  205. close $file
  206. }
  207. # Test setting and adjusting the start address. We only test this
  208. # while generating S records, because we may not be able to set the
  209. # start address for other object file formats, and the S record case
  210. # is the only useful one anyhow.
  211. set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
  212. if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
  213. perror "objdump can not recognize bintest.o"
  214. set origstart ""
  215. } else {
  216. set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
  217. if ![string equal "" $got] then {
  218. fail "objcopy --set-start"
  219. } else {
  220. set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
  221. if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
  222. fail "objcopy --set-start"
  223. } else {
  224. if {$srecstart != 0x7654} then {
  225. send_log "$srecstart != 0x7654\n"
  226. fail "objcopy --set-start"
  227. } else {
  228. pass "objcopy --set-start"
  229. }
  230. }
  231. }
  232. set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
  233. if ![string equal "" $got] then {
  234. fail "objcopy --adjust-start"
  235. } else {
  236. set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
  237. if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
  238. fail "objcopy --adjust-start"
  239. } else {
  240. if {$srecstart != $origstart + 0x123} then {
  241. send_log "$srecstart != $origstart + 0x123\n"
  242. fail "objcopy --adjust-start"
  243. } else {
  244. pass "objcopy --adjust-start"
  245. }
  246. }
  247. }
  248. }
  249. # Test adjusting the overall VMA, and adjusting the VMA of a
  250. # particular section. We again only test this when generating S
  251. # records.
  252. set low ""
  253. set lowname ""
  254. set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
  255. set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
  256. set got $headers
  257. while {[regexp $headers_regexp $got all name size vma rest]} {
  258. set vma 0x$vma
  259. set size 0x$size
  260. if {$size != 0} {
  261. if {$low == "" || $vma < $low} {
  262. set low $vma
  263. set lowname $name
  264. }
  265. }
  266. set got $rest
  267. }
  268. if {$low == "" || $origstart == ""} then {
  269. perror "objdump can not recognize bintest.o"
  270. } else {
  271. set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
  272. if ![string equal "" $got] then {
  273. fail "objcopy --adjust-vma"
  274. } else {
  275. set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
  276. set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
  277. if ![regexp $want $got all start vma] then {
  278. fail "objcopy --adjust-vma"
  279. } else {
  280. set vma 0x$vma
  281. if {$vma != $low + 0x123} then {
  282. send_log "$vma != $low + 0x123\n"
  283. fail "objcopy --adjust-vma"
  284. } else {
  285. if {$start != $origstart + 0x123} then {
  286. send_log "$start != $origstart + 0x123\n"
  287. fail "objcopy --adjust-vma"
  288. } else {
  289. pass "objcopy --adjust-vma"
  290. }
  291. }
  292. }
  293. }
  294. set arg ""
  295. set got $headers
  296. while {[regexp $headers_regexp $got all name size vma rest]} {
  297. set vma 0x$vma
  298. if {$vma == $low} then {
  299. set arg "$arg --adjust-section-vma $name+4"
  300. }
  301. set got $rest
  302. }
  303. set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
  304. if ![string equal "" $got] then {
  305. fail "objcopy --adjust-section-vma +"
  306. } else {
  307. set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
  308. set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
  309. if ![regexp $want $got all vma] then {
  310. fail "objcopy --adjust-section-vma +"
  311. } else {
  312. set vma 0x$vma
  313. if {$vma != $low + 4} then {
  314. send_log "$vma != $low + 4\n"
  315. fail "objcopy --adjust-section-vma +"
  316. } else {
  317. pass "objcopy --adjust-section-vma +"
  318. }
  319. }
  320. }
  321. regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
  322. set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
  323. if ![string equal "" $got] then {
  324. fail "objcopy --adjust-section-vma ="
  325. } else {
  326. set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
  327. set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
  328. if ![regexp $want $got all vma] then {
  329. fail "objcopy --adjust-section-vma ="
  330. } else {
  331. set vma 0x$vma
  332. if {$vma != $low + 4} then {
  333. send_log "$vma != $low + 4\n"
  334. fail "objcopy --adjust-section-vma ="
  335. } else {
  336. pass "objcopy --adjust-section-vma ="
  337. }
  338. }
  339. }
  340. }
  341. # Test stripping an object.
  342. proc strip_test { } {
  343. global AR
  344. global CC
  345. global STRIP
  346. global STRIPFLAGS
  347. global NM
  348. global NMFLAGS
  349. global srcdir
  350. global subdir
  351. global READELF
  352. set test "strip"
  353. if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
  354. untested $test
  355. return
  356. }
  357. set osabi_fail "false"
  358. if [is_elf_format] {
  359. verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
  360. set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
  361. if { [lindex $exec_output 0] != 0 } then {
  362. unresolved "$test preserving OS/ABI"
  363. set osabi_fail "true"
  364. } else {
  365. verbose -log "grep OS/ABI tmpdir/osabi.in"
  366. catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
  367. }
  368. }
  369. if [is_remote host] {
  370. set archive libstrip.a
  371. set objfile [remote_download host tmpdir/testprog.o]
  372. remote_file host delete $archive
  373. } else {
  374. set archive tmpdir/libstrip.a
  375. set objfile tmpdir/testprog.o
  376. }
  377. remote_file build delete tmpdir/libstrip.a
  378. set exec_output [binutils_run $AR "rc $archive ${objfile}"]
  379. if ![string equal "" $exec_output] {
  380. fail $test
  381. unresolved "$test preserving OS/ABI"
  382. return
  383. }
  384. set exec_output [binutils_run $STRIP "-g $archive"]
  385. if ![string equal "" $exec_output] {
  386. fail $test
  387. unresolved "$test preserving OS/ABI"
  388. return
  389. }
  390. set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
  391. if ![string equal "" $exec_output] {
  392. fail $test
  393. unresolved "$test preserving OS/ABI"
  394. return
  395. }
  396. if { $osabi_fail != "true" && [is_elf_format] } {
  397. verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
  398. set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
  399. if { [lindex $exec_output 0] != 0 } then {
  400. unresolved "$test preserving OS/ABI"
  401. } else {
  402. verbose -log "grep OS/ABI tmpdir/osabi.out"
  403. catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
  404. if { "$osabi_in" == "$osabi_out" } {
  405. pass "$test preserving OS/ABI"
  406. } else {
  407. fail "$test preserving OS/ABI"
  408. }
  409. }
  410. }
  411. if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
  412. untested $test
  413. return
  414. }
  415. if [is_remote host] {
  416. set objfile [remote_download host tmpdir/testprog.o]
  417. } else {
  418. set objfile tmpdir/testprog.o
  419. }
  420. set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
  421. if ![string equal "" $exec_output] {
  422. fail $test
  423. return
  424. }
  425. set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
  426. if ![string match "*: no symbols*" $exec_output] {
  427. fail $test
  428. return
  429. }
  430. pass $test
  431. }
  432. strip_test
  433. # Test stripping an object file with saving a symbol
  434. proc strip_test_with_saving_a_symbol { } {
  435. global CC
  436. global STRIP
  437. global STRIPFLAGS
  438. global NM
  439. global NMFLAGS
  440. global srcdir
  441. global subdir
  442. set test "strip with saving a symbol"
  443. if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
  444. untested $test
  445. return
  446. }
  447. if [is_remote host] {
  448. set objfile [remote_download host tmpdir/testprog.o]
  449. } else {
  450. set objfile tmpdir/testprog.o
  451. }
  452. set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
  453. if ![string equal "" $exec_output] {
  454. fail $test
  455. return
  456. }
  457. set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
  458. if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
  459. && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
  460. fail $test
  461. return
  462. }
  463. pass $test
  464. }
  465. strip_test_with_saving_a_symbol
  466. # Build a final executable.
  467. if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
  468. set test_prog "testprog.exe"
  469. } else {
  470. set test_prog "testprog"
  471. }
  472. proc copy_setup { } {
  473. global srcdir
  474. global subdir
  475. global gcc_gas_flag
  476. global test_prog
  477. global host_triplet
  478. set res [build_wrapper testglue.o]
  479. set flags { debug }
  480. if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] } {
  481. return 1
  482. }
  483. if { $res != "" } {
  484. lappend flags "additional_flags=[lindex $res 1]"
  485. set add_libs "testglue.o"
  486. } else {
  487. set add_libs ""
  488. }
  489. if { [istarget *-*-linux*]
  490. || [istarget *-*-gnu*] } {
  491. foreach i $gcc_gas_flag {
  492. set flags "additional_flags=$i $flags"
  493. }
  494. }
  495. if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
  496. return 2
  497. }
  498. set result [remote_load target tmpdir/$test_prog]
  499. set status [lindex $result 0]
  500. if { $status != "pass" } {
  501. send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
  502. return 3
  503. }
  504. return 0
  505. }
  506. # Test copying an executable.
  507. proc copy_executable { prog flags test1 test2 } {
  508. global test_prog
  509. if [is_remote host] {
  510. set testfile [remote_download host tmpdir/$test_prog]
  511. set testcopy copyprog
  512. } else {
  513. set testfile tmpdir/$test_prog
  514. set testcopy tmpdir/copyprog
  515. }
  516. remote_file host delete $testcopy
  517. set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
  518. if ![string equal "" $exec_output] {
  519. fail $test1
  520. if [string equal "" $test2] {
  521. return
  522. }
  523. fail $test2
  524. return
  525. }
  526. if [is_remote host] {
  527. remote_upload host $testcopy tmpdir/copyprog
  528. }
  529. set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
  530. set exec_output [lindex $status 1]
  531. if [string equal "" $exec_output] then {
  532. pass $test1
  533. } else {
  534. send_log "$exec_output\n"
  535. verbose "$exec_output"
  536. # This will fail for many reasons. For example, it will most
  537. # likely fail if a non-GNU linker is used. Therefore, we do
  538. # not insist that it pass. If you are using an assembler and
  539. # linker based on the same BFD as objcopy, it is worth
  540. # investigating to see why this failure occurs. If we are
  541. # cross compiling, we assume that a GNU linker is being used,
  542. # and expect it to succeed.
  543. if {[isnative]} then {
  544. setup_xfail "*-*-*"
  545. }
  546. # This also fails for mips*-*-elf targets. See elf32-mips.c
  547. # mips_elf_sym_is_global.
  548. setup_xfail "mips*-*-elf"
  549. setup_xfail "arm*-*-coff"
  550. setup_xfail "arm*-*-pe"
  551. setup_xfail "*-*-mingw*"
  552. setup_xfail "*-*-cygwin*"
  553. fail $test1
  554. }
  555. if [string equal "" $test2] {
  556. return
  557. }
  558. set output [remote_load target tmpdir/copyprog]
  559. set status [lindex $output 0]
  560. if { $status != "pass" } {
  561. fail $test2
  562. } else {
  563. pass $test2
  564. }
  565. }
  566. # Test stripping an executable
  567. proc strip_executable { prog flags test1 test2 } {
  568. global NM
  569. global NMFLAGS
  570. global READELF
  571. remote_file build delete tmpdir/striprog
  572. remote_download build tmpdir/copyprog tmpdir/striprog
  573. if [is_remote host] {
  574. set copyfile [remote_download host tmpdir/striprog]
  575. } else {
  576. set copyfile tmpdir/striprog
  577. }
  578. set osabi_fail "false"
  579. if [is_elf_format] {
  580. verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
  581. set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
  582. if { [lindex $exec_output 0] != 0 } then {
  583. unresolved "$test1 preserving OS/ABI"
  584. set osabi_fail "true"
  585. } else {
  586. verbose -log "grep OS/ABI tmpdir/osabi.in"
  587. catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
  588. }
  589. }
  590. set exec_output [binutils_run $prog "$flags ${copyfile}"]
  591. if ![string equal "" $exec_output] {
  592. fail $test1
  593. if [string equal "" $test2] {
  594. return
  595. }
  596. fail $test2
  597. return
  598. }
  599. if [is_remote host] {
  600. remote_upload host ${copyfile} tmpdir/striprog
  601. }
  602. if { $osabi_fail != "true" && [is_elf_format] } {
  603. verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
  604. set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
  605. if { [lindex $exec_output 0] != 0 } then {
  606. unresolved "$test1 preserving OS/ABI"
  607. } else {
  608. verbose -log "grep OS/ABI tmpdir/osabi.out"
  609. catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
  610. if { "$osabi_in" == "$osabi_out" } {
  611. pass "$test1 preserving OS/ABI"
  612. } else {
  613. fail "$test1 preserving OS/ABI"
  614. }
  615. }
  616. }
  617. set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
  618. if ![string match "*: no symbols*" $exec_output] {
  619. fail $test1
  620. return
  621. }
  622. if [string equal "" $test2] {
  623. return
  624. }
  625. set result [remote_load target tmpdir/striprog]
  626. set status [lindex $result 0]
  627. if { $status != "pass" } {
  628. fail $test2
  629. return
  630. }
  631. pass $test2
  632. }
  633. # Test stripping an executable with saving a symbol
  634. proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
  635. global NM
  636. global NMFLAGS
  637. remote_file build delete tmpdir/striprog
  638. remote_download build tmpdir/copyprog tmpdir/striprog
  639. if [is_remote host] {
  640. set copyfile [remote_download host tmpdir/striprog]
  641. } else {
  642. set copyfile tmpdir/striprog
  643. }
  644. set exec_output [binutils_run $prog "$flags ${copyfile}"]
  645. if ![string equal "" $exec_output] {
  646. fail $test1
  647. if [string equal "" $test2] {
  648. return
  649. }
  650. fail $test2
  651. return
  652. }
  653. set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
  654. if { [istarget mmix-knuth-mmixware] } {
  655. # Whenever there's a symbol in the mmo format, there's the symbol
  656. # Main, so remove it manually from the expected output for sake of
  657. # this test.
  658. # Using "" not {} to get the \n and \r translated.
  659. regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
  660. }
  661. if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
  662. && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
  663. fail $test1
  664. return
  665. }
  666. if [string equal "" $test2] {
  667. return
  668. }
  669. if [is_remote host] {
  670. remote_upload host ${copyfile} tmpdir/striprog
  671. }
  672. set result [remote_load target tmpdir/striprog]
  673. set status [lindex $result 0]
  674. if { $status != "pass" } {
  675. fail $test2
  676. return
  677. }
  678. pass $test2
  679. }
  680. # Test keeping only debug symbols of an executable
  681. proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
  682. remote_file build delete tmpdir/striprog
  683. remote_download build tmpdir/copyprog tmpdir/striprog
  684. if [is_remote host] {
  685. set copyfile [remote_download host tmpdir/striprog]
  686. } else {
  687. set copyfile tmpdir/striprog
  688. }
  689. set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
  690. if ![string equal "" $exec_output] {
  691. fail $test1
  692. return
  693. }
  694. pass $test1
  695. set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
  696. if ![string equal "" $exec_output] {
  697. fail $test2
  698. return
  699. }
  700. pass $test2
  701. }
  702. # Tests that in a debug only copy of a file the sections
  703. # headers whoes types have been changed to NOBITS still
  704. # retain their sh_link fields.
  705. proc keep_debug_symbols_and_check_links { prog flags test } {
  706. global READELF
  707. remote_file build delete tmpdir/striprog
  708. remote_download build tmpdir/copyprog tmpdir/striprog
  709. if [is_remote host] {
  710. set copyfile [remote_download host tmpdir/striprog]
  711. } else {
  712. set copyfile tmpdir/striprog
  713. }
  714. set exec_output [binutils_run $prog "$flags ${copyfile}"]
  715. if ![string equal "" $exec_output] {
  716. fail $test
  717. return
  718. }
  719. set got [binutils_run $READELF "-S --wide ${copyfile}"]
  720. set fails 0
  721. # Regexp to match a section with NOBITS type and extract its name and sh_link fields
  722. while {[regexp \
  723. {[^a-zA-Z]+([a-zA-Z0-9_\.]+)[ ]+NOBITS[ ]+[0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9]+[ A]+([0-9]+)(.*)} \
  724. $got all name link rest]} {
  725. set sh_link 0x$link
  726. if {$sh_link == 0} {
  727. # Only some NOBITS sections should have a non-zero sh_link field.
  728. # Look for them by name.
  729. verbose "NOBITS section .$name has a 0 sh_link field\n"
  730. switch $name {
  731. "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
  732. "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
  733. "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
  734. "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
  735. "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
  736. "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
  737. "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
  738. "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
  739. }
  740. }
  741. set got $rest
  742. }
  743. if {$fails == 0} {
  744. pass $test
  745. } else {
  746. fail $test
  747. }
  748. }
  749. set test1 "simple objcopy of executable"
  750. set test1r "run objcopy of executable"
  751. set test2 "strip executable"
  752. set test2r "run stripped executable"
  753. set test3 "strip executable with saving a symbol"
  754. set test3r "run stripped executable with saving a symbol"
  755. set test4 "keep only debug data"
  756. set test5 "simple objcopy of debug data"
  757. if [is_elf_format] {
  758. set test6 "NOBITS sections retain sh_link field"
  759. }
  760. switch [copy_setup] {
  761. "1" {
  762. # do nothing
  763. }
  764. "2" {
  765. untested $test1
  766. untested $test1r
  767. untested $test2
  768. untested $test2r
  769. untested $test3
  770. untested $test3r
  771. untested $test4
  772. untested $test5
  773. if [is_elf_format] {
  774. untested $test6
  775. }
  776. }
  777. "3" {
  778. copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
  779. unsupported $test1r
  780. strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
  781. unsupported $test2r
  782. strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
  783. unsupported $test3r
  784. keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
  785. "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
  786. if [is_elf_format] {
  787. keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
  788. }
  789. }
  790. "0" {
  791. copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
  792. strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
  793. strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
  794. keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
  795. "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
  796. if [is_elf_format] {
  797. keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
  798. }
  799. }
  800. }
  801. proc objcopy_test_readelf {testname srcfile} {
  802. global OBJCOPY
  803. global OBJCOPYFLAGS
  804. global READELF
  805. global srcdir
  806. global subdir
  807. if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
  808. unresolved "objcopy ($testname)"
  809. return
  810. }
  811. verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
  812. set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
  813. if { [lindex $exec_output 0] != 0
  814. || ![string equal "" [lindex $exec_output 1]] } then {
  815. fail "objcopy ($testname)"
  816. return
  817. }
  818. verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
  819. set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
  820. if { [lindex $exec_output 0] != 0 } then {
  821. unresolved "objcopy ($testname)"
  822. return
  823. }
  824. set exec_output [prune_warnings [lindex $exec_output 1]]
  825. if ![string equal "" $exec_output] then {
  826. unresolved "objcopy ($testname)"
  827. return
  828. }
  829. verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
  830. set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
  831. if { [lindex $exec_output 0] != 0 } then {
  832. unresolved "objcopy ($testname)"
  833. return
  834. }
  835. set exec_output [prune_warnings [lindex $exec_output 1]]
  836. if ![string equal "" $exec_output] then {
  837. unresolved "objcopy ($testname)"
  838. return
  839. }
  840. verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
  841. catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
  842. set exec_output [prune_warnings $exec_output]
  843. if [string equal "" $exec_output] then {
  844. pass "objcopy ($testname)"
  845. } else {
  846. fail "objcopy ($testname)"
  847. }
  848. }
  849. proc objcopy_test_symbol_manipulation {} {
  850. global srcdir
  851. global subdir
  852. set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
  853. foreach t $test_list {
  854. # We need to strip the ".d", but can leave the dirname.
  855. verbose [file rootname $t]
  856. run_dump_test [file rootname $t]
  857. }
  858. }
  859. # ia64 specific tests
  860. if { ([istarget "ia64-*-elf*"]
  861. || [istarget "ia64-*-linux*"]) } {
  862. objcopy_test "ia64 link order" link-order.s
  863. }
  864. # ELF specific tests
  865. if [is_elf_format] {
  866. objcopy_test_symbol_manipulation
  867. objcopy_test "ELF unknown section type" unknown.s
  868. objcopy_test_readelf "ELF group" group.s
  869. objcopy_test_readelf "ELF group" group-2.s
  870. objcopy_test_readelf "ELF group" group-3.s
  871. objcopy_test_readelf "ELF group" group-4.s
  872. run_dump_test "group-5"
  873. run_dump_test "group-6"
  874. run_dump_test "copy-1"
  875. run_dump_test "note-1"
  876. }
  877. run_dump_test "copy-2"
  878. run_dump_test "copy-3"
  879. run_dump_test "copy-4"
  880. run_dump_test "pr19020a"
  881. run_dump_test "pr19020b"
  882. if [is_elf_format] {
  883. run_dump_test "strip-1"
  884. run_dump_test "strip-2"
  885. run_dump_test "strip-3"
  886. run_dump_test "strip-4"
  887. run_dump_test "strip-5"
  888. run_dump_test "strip-6"
  889. run_dump_test "strip-7"
  890. run_dump_test "strip-8"
  891. run_dump_test "strip-9"
  892. run_dump_test "strip-12"
  893. # Non-EABI ARM targets will set OSABI to ARM
  894. # The V850 sets the OSABI to UNIX - System V
  895. if { ![istarget "*-*-hpux*"]
  896. && ![istarget "msp*-*-*"]
  897. && ![istarget "visium-*-*"]
  898. && ![istarget "v850*-*-*"]
  899. && !([istarget "arm*-*-*"] && ![istarget "arm-*-*eabi*"])} {
  900. run_dump_test "strip-10"
  901. }
  902. set extra_strip11 ""
  903. if { [istarget "sh64*-*"] } {
  904. # pr17755 testcase
  905. set extra_strip11 { { "as" "--isa=SHmedia --abi=64" } }
  906. }
  907. run_dump_test "strip-11" $extra_strip11
  908. if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
  909. # Check to make sure we don't strip a symbol named in relocations.
  910. set test "objcopy keeps symbols needed by relocs"
  911. set srcfile $srcdir/$subdir/needed-by-reloc.s
  912. if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
  913. unresolved $test
  914. } else {
  915. set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
  916. if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
  917. pass $test
  918. } else {
  919. fail $test
  920. }
  921. }
  922. }
  923. # The symbol table for some MIPS targets is sorted differently than
  924. # the ELF canonical order, so the regexps in localize-hidden-1.d fail
  925. # to match. These tests must be matched to targets for which
  926. # targ_defvec=mips_elf32_be_vec,
  927. # targ_defvec=mips_elf32_le_vec,
  928. # targ_defvec=mips_elf32_n_be_vec or
  929. # targ_defvec=mips_elf32_n_le_vec in config.bfd. When syncing,
  930. # don't forget that earlier case-matches trump later ones.
  931. if { ![istarget "mips*-sde-elf*"] && ![istarget "mips*-mti-elf*"]
  932. && ![istarget "mips*-img-elf*"]
  933. && ![istarget "mips64*-*-openbsd*"] } {
  934. setup_xfail "mips*-*-irix5*" "mips*-*-irix6*" "mips*-*-elf*" \
  935. "mips*-*-rtems*" "mips*-*-windiss" "mips*-*-none" \
  936. "mips*-*-openbsd*" "mips*-*-chorus*"
  937. }
  938. run_dump_test "localize-hidden-1"
  939. run_dump_test "testranges"
  940. run_dump_test "testranges-ia64"
  941. run_dump_test "add-section"
  942. run_dump_test "add-symbol"
  943. run_dump_test "add-empty-section"
  944. run_dump_test "exclude-1a"
  945. run_dump_test "exclude-1b"
  946. }
  947. run_dump_test "localize-hidden-2"