vers.exp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. # Expect script for ld-version tests
  2. # Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. #
  4. # This file is part of the GNU Binutils.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19. # MA 02110-1301, USA.
  20. #
  21. # Written by Eric Youngdale (eric@andante.jic.com)
  22. #
  23. # Check to see if the C compiler works
  24. if { ![check_compiler_available] } {
  25. return
  26. }
  27. # This test can only be run on a couple of ELF platforms.
  28. # Square bracket expressions seem to confuse istarget.
  29. # This is similar to the test that is used in ld-shared, BTW.
  30. if { ![istarget hppa*64*-*-hpux*]
  31. && ![istarget hppa*-*-linux*]
  32. && ![istarget i?86-*-sysv4*]
  33. && ![istarget i?86-*-unixware]
  34. && ![istarget i?86-*-elf*]
  35. && ![istarget i?86-*-linux*]
  36. && ![istarget i?86-*-gnu*]
  37. && ![istarget ia64-*-elf*]
  38. && ![istarget ia64-*-linux*]
  39. && ![istarget m68k-*-linux*]
  40. && ![istarget mips*-*-irix5*]
  41. && ![istarget powerpc*-*-elf*]
  42. && ![istarget powerpc*-*-linux*]
  43. && ![istarget powerpc*-*-sysv4*]
  44. && ![istarget sparc*-*-elf]
  45. && ![istarget sparc*-*-solaris2*]
  46. && ![istarget sparc*-*-linux*]
  47. && ![istarget aarch64*-*-linux*]
  48. && ![istarget arm*-*-linux*]
  49. && ![istarget mips*-*-linux*]
  50. && ![istarget alpha*-*-linux*]
  51. && ![istarget s390*-*-linux*]
  52. && ![istarget sh\[34\]*-*-linux*]
  53. && ![istarget x86_64-*-linux*] } {
  54. return
  55. }
  56. set diff diff
  57. set tmpdir tmpdir
  58. set VOBJDUMP_FLAGS --private-headers
  59. set DOBJDUMP_FLAGS --dynamic-syms
  60. set SOBJDUMP_FLAGS --syms
  61. set shared "--shared --no-undefined-version"
  62. set script --version-script
  63. # Old version of GCC for MIPS default to enabling -fpic
  64. # and get confused if it is used on the command line.
  65. if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
  66. set picflag ""
  67. } else {
  68. # Unfortunately, the gcc argument is -fpic and the cc argument is
  69. # -KPIC. We have to try both.
  70. set picflag "-fpic"
  71. send_log "$CC_FOR_TARGET $picflag\n"
  72. verbose "$CC_FOR_TARGET $picflag"
  73. catch "exec $CC_FOR_TARGET $picflag" exec_output
  74. send_log "$exec_output\n"
  75. verbose "--" "$exec_output"
  76. if { [string match "*illegal option*" $exec_output]
  77. || [string match "*option ignored*" $exec_output]
  78. || [string match "*unrecognized option*" $exec_output]
  79. || [string match "*passed to ld*" $exec_output] } {
  80. set picflag "-KPIC"
  81. }
  82. }
  83. switch -glob $target_triplet {
  84. ia64-*-* { set as_options "-x" }
  85. sparc-*-* { set as_options "-Av9a" }
  86. default { set as_options "" }
  87. }
  88. proc test_ar { test lib object expect } {
  89. global ar
  90. global nm
  91. global tmpdir
  92. global srcdir
  93. global subdir
  94. global diff
  95. verbose -log "$ar -cr $tmpdir/$lib $tmpdir/$object"
  96. catch "exec $ar -cr $tmpdir/$lib $tmpdir/$object" exec_output
  97. set exec_output [prune_warnings $exec_output]
  98. if ![string match "" $exec_output] {
  99. verbose -log "$exec_output"
  100. fail "$test"
  101. return
  102. }
  103. set cmd "$nm --print-armap $tmpdir/$lib | grep \\\ in\\\ | egrep VERS\\\|bar\\\|foo | grep -v ^\\\\. | sort > $tmpdir/nm.out"
  104. verbose -log $cmd
  105. catch "exec $cmd" exec_output
  106. if [string match "" $exec_output] then {
  107. catch "exec sort $srcdir/$subdir/$expect | $diff $tmpdir/nm.out -" exec_output
  108. set exec_output [prune_warnings $exec_output]
  109. if [string match "" $exec_output] then {
  110. pass $test
  111. return
  112. } else {
  113. verbose -log "$exec_output"
  114. fail "$test"
  115. return
  116. }
  117. } else {
  118. verbose -log "$exec_output"
  119. fail "$test"
  120. }
  121. }
  122. #
  123. # objdump_emptysymstuff
  124. # Check non-dynamic symbols and make sure there are none with '@'.
  125. #
  126. proc objdump_emptysymstuff { objdump object } {
  127. global SOBJDUMP_FLAGS
  128. global version_output
  129. global diff
  130. if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
  131. verbose -log "$objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p"
  132. catch "exec $objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p" exec_output
  133. set exec_output [prune_warnings $exec_output]
  134. if [string match "" $exec_output] then {
  135. # We shouldn't get anything here.
  136. return 1
  137. } else {
  138. # it is not normal to come here - we have no output to compare.
  139. verbose -log "$exec_output"
  140. verbose -log "objdump_emptysymstuff: did not expect any output from objdump"
  141. return 0
  142. }
  143. }
  144. #
  145. # objdump_emptydynsymstuff
  146. # Check dynamic symbols and make sure there are none with '@'.
  147. #
  148. proc objdump_emptydynsymstuff { objdump object } {
  149. global DOBJDUMP_FLAGS
  150. global version_output
  151. global diff
  152. if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
  153. verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p"
  154. catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p" exec_output
  155. set exec_output [prune_warnings $exec_output]
  156. if [string match "" $exec_output] then {
  157. # We shouldn't get anything here.
  158. return 1
  159. } else { if [string match "*objdump: *: not a dynamic object" $exec_output] then {
  160. return 1
  161. } else {
  162. # it is not normal to come here - we have no output to compare.
  163. verbose -log "$exec_output"
  164. verbose -log "objdump_emptydynsymstuff: did not expect any output from objdump"
  165. return 0
  166. } }
  167. }
  168. #
  169. # objdump_emptyverstuff
  170. # Make sure there is no version information
  171. #
  172. proc objdump_emptyverstuff { objdump object } {
  173. global VOBJDUMP_FLAGS
  174. global version_output
  175. global diff
  176. global tmpdir
  177. if {[which $objdump] == 0} then {
  178. perror "$objdump does not exist"
  179. return 0
  180. }
  181. if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
  182. verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
  183. catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p" exec_output
  184. set exec_output [prune_warnings $exec_output]
  185. if [string match "" $exec_output] then {
  186. # it is normal to fail here - we have no output to compare.
  187. return 1
  188. } else { if { [string match "*libc*" $exec_output] } then {
  189. # this probably means that there is version information in libc, so we
  190. # can't really perform this test.
  191. return 1
  192. } else {
  193. verbose -log "$exec_output"
  194. verbose -log "objdump_emptyverstuff: did not expect any output from objdump"
  195. return 0
  196. } }
  197. }
  198. #
  199. # objdump_symstuff
  200. # Dump non-dynamic symbol stuff and make sure that it is sane.
  201. #
  202. proc objdump_symstuff { objdump object expectfile } {
  203. global SOBJDUMP_FLAGS
  204. global version_output
  205. global diff
  206. global tmpdir
  207. if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
  208. set cmd "$objdump $SOBJDUMP_FLAGS $object | sed -n {s/^\\(\[0-9a-f\]* *\\)\\(\[gw\]\\)\\( *\\)\\(\[FO\]\\)/\\1\\2\\4\\3/;/\@/p} | sort -k 5 > $tmpdir/objdump.out"
  209. verbose -log $cmd
  210. catch "exec $cmd" exec_output
  211. set exec_output [prune_warnings $exec_output]
  212. if [string match "" $exec_output] then {
  213. # Now do a line-by-line comparison to effectively diff the darned things
  214. # The stuff coming from the expectfile is actually a regex, so we can
  215. # skip over the actual addresses and so forth. This is currently very
  216. # simpleminded - it expects a one-to-one correspondence in terms of line
  217. # numbers.
  218. if [file exists $expectfile] then {
  219. set file_a [open $expectfile r]
  220. } else {
  221. perror "$expectfile doesn't exist"
  222. return 0
  223. }
  224. if [file exists $tmpdir/objdump.out] then {
  225. set file_b [open $tmpdir/objdump.out r]
  226. } else {
  227. perror "$tmpdir/objdump.out doesn't exist"
  228. return 0
  229. }
  230. verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
  231. set eof -1
  232. set list_a {}
  233. set list_b {}
  234. while { [gets $file_a line] != $eof } {
  235. if [regexp "^#.*$" $line] then {
  236. continue
  237. } else {
  238. lappend list_a $line
  239. }
  240. }
  241. close $file_a
  242. while { [gets $file_b line] != $eof } {
  243. if [regexp {\.text.* \.[^ ]*$} $line] then {
  244. # Discard defined powerpc64 dot-symbols
  245. continue
  246. } else {
  247. lappend list_b $line
  248. }
  249. }
  250. close $file_b
  251. for { set i 0 } { $i < [llength $list_a] } { incr i } {
  252. set line_a [lindex $list_a $i]
  253. set line_b [lindex $list_b $i]
  254. verbose "\t$expectfile: $i: $line_a" 3
  255. verbose "\t/tmp/objdump.out: $i: $line_b" 3
  256. if [regexp $line_a $line_b] then {
  257. continue
  258. } else {
  259. verbose -log "\t$expectfile: $i: $line_a"
  260. verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
  261. return 0
  262. }
  263. }
  264. if { [llength $list_a] != [llength $list_b] } {
  265. verbose -log "Line count"
  266. return 0
  267. }
  268. return 1
  269. } else {
  270. verbose -log "$exec_output"
  271. return 0
  272. }
  273. }
  274. #
  275. # objdump_dymsymstuff
  276. # Dump dynamic symbol stuff and make sure that it is sane.
  277. #
  278. proc objdump_dynsymstuff { objdump object expectfile } {
  279. global DOBJDUMP_FLAGS
  280. global version_output
  281. global diff
  282. global tmpdir
  283. if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
  284. set cmd "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out"
  285. verbose -log $cmd
  286. catch "exec $cmd" exec_output
  287. set exec_output [prune_warnings $exec_output]
  288. if [string match "" $exec_output] then {
  289. # Now do a line-by-line comparison to effectively diff the darned things
  290. # The stuff coming from the expectfile is actually a regex, so we can
  291. # skip over the actual addresses and so forth. This is currently very
  292. # simpleminded - it expects a one-to-one correspondence in terms of line
  293. # numbers.
  294. if [file exists $expectfile] then {
  295. set file_a [open $expectfile r]
  296. } else {
  297. warning "$expectfile doesn't exist"
  298. return 0
  299. }
  300. if [file exists $tmpdir/objdump.out] then {
  301. set file_b [open $tmpdir/objdump.out r]
  302. } else {
  303. fail "$tmpdir/objdump.out doesn't exist"
  304. return 0
  305. }
  306. verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
  307. set eof -1
  308. set list_a {}
  309. set list_b {}
  310. while { [gets $file_a line] != $eof } {
  311. if [regexp "^#.*$" $line] then {
  312. continue
  313. } else {
  314. lappend list_a $line
  315. }
  316. }
  317. close $file_a
  318. while { [gets $file_b line] != $eof } {
  319. if [regexp {\.text.* \.[^ ]*$} $line] then {
  320. # Discard defined powerpc64 dot-symbols
  321. continue
  322. } else {
  323. lappend list_b $line
  324. }
  325. }
  326. close $file_b
  327. for { set i 0 } { $i < [llength $list_b] } { incr i } {
  328. set line_b [lindex $list_b $i]
  329. # The tests are rigged so that we should never export a symbol with the
  330. # word 'hide' in it. Thus we just search for it, and bail if we find it.
  331. if [regexp "hide" $line_b] then {
  332. verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
  333. return 0
  334. }
  335. verbose "\t$expectfile: $i: $line_b" 3
  336. # We can't assume that the sort is consistent across
  337. # systems, so we must check each regexp. When we find a
  338. # regexp, we null it out, so we don't match it twice.
  339. for { set j 0 } { $j < [llength $list_a] } { incr j } {
  340. set line_a [lindex $list_a $j]
  341. if [regexp $line_a $line_b] then {
  342. lreplace $list_a $j $j "CAN NOT MATCH"
  343. break
  344. }
  345. }
  346. if { $j >= [llength $list_a] } {
  347. verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
  348. return 0
  349. }
  350. }
  351. if { [llength $list_a] != [llength $list_b] } {
  352. verbose -log "Line count"
  353. return 0
  354. }
  355. return 1
  356. } else {
  357. verbose -log "$exec_output"
  358. return 0
  359. }
  360. }
  361. #
  362. # objdump_versionstuff
  363. # Dump version definitions/references and make sure that it is sane.
  364. #
  365. proc objdump_versionstuff { objdump object expectfile } {
  366. global VOBJDUMP_FLAGS
  367. global version_output
  368. global diff
  369. global tmpdir
  370. if {[which $objdump] == 0} then {
  371. perror "$objdump does not exist"
  372. return 0
  373. }
  374. if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
  375. verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
  376. catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out" exec_output
  377. set exec_output [prune_warnings $exec_output]
  378. if [string match "" $exec_output] then {
  379. # It's OK if there are extra lines in the actual output; they
  380. # may come from version information in libc. We require that
  381. # every line in EXPECTFILE appear in the output in any order.
  382. set f2 [open $expectfile r]
  383. while { [gets $f2 l2] != -1 } {
  384. if { ![regexp "^#.*$" $l2] } then {
  385. set f1 [open $tmpdir/objdump.out r]
  386. while { [gets $f1 l1] != -1 } {
  387. if { [string match $l2 $l1] } then {
  388. break
  389. }
  390. }
  391. close $f1
  392. if { ![string match $l2 $l1] } then {
  393. verbose -log "Did not find \"$l2\""
  394. set f1 [open $tmpdir/objdump.out r]
  395. while { [gets $f1 l1] != -1 } {
  396. verbose -log $l1
  397. }
  398. close $f1
  399. close $f2
  400. return 0
  401. }
  402. }
  403. }
  404. close $f2
  405. return 1
  406. } else {
  407. verbose -log "$exec_output"
  408. return 0
  409. }
  410. }
  411. proc build_binary { shared pic test source libname other mapfile verexp versymexp symexp ldargs } {
  412. global ld
  413. global srcdir
  414. global subdir
  415. global exec_output
  416. global host_triplet
  417. global tmpdir
  418. global as
  419. global as_options
  420. global objdump
  421. global CC_FOR_TARGET
  422. global script
  423. global NOSANITIZE_CFLAGS
  424. global NOLTO_CFLAGS
  425. if ![ld_compile "$CC_FOR_TARGET -S $pic $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s] {
  426. unsupported "$test"
  427. return
  428. }
  429. if ![ld_assemble $as "$as_options $tmpdir/$libname.s" $tmpdir/$libname.o ] {
  430. unsupported "$test"
  431. return
  432. }
  433. set other_lib ""
  434. if ![string match "" $other] then {
  435. foreach o $other {
  436. set other_lib "$other_lib $tmpdir/$o"
  437. }
  438. }
  439. if [string match "" $mapfile] then {
  440. set script_arg ""
  441. } else {
  442. set script_arg "$script $srcdir/$subdir/$mapfile"
  443. }
  444. if {![ld_link $ld $tmpdir/$libname.so "$shared $tmpdir/$libname.o $other_lib $script_arg $ldargs"]} {
  445. fail "$test"
  446. return
  447. }
  448. if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
  449. fail "$test"
  450. return
  451. }
  452. if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
  453. fail "$test"
  454. return
  455. }
  456. if [string match "" $symexp] then {
  457. if {![objdump_emptysymstuff $objdump $tmpdir/$libname.o ]} {
  458. fail "$test"
  459. return
  460. }
  461. } else {
  462. if {![objdump_symstuff $objdump $tmpdir/$libname.o $srcdir/$subdir/$symexp ]} {
  463. fail "$test"
  464. return
  465. }
  466. }
  467. pass $test
  468. }
  469. proc build_executable { test source libname other mapfile verexp versymexp symexp } {
  470. build_binary "" "" $test $source $libname $other $mapfile $verexp $versymexp $symexp ""
  471. }
  472. proc build_vers_lib_no_pic { test source libname other mapfile verexp versymexp symexp } {
  473. global shared
  474. # Make sure that PLT is used since PLT is expected.
  475. global PLT_CFLAGS
  476. build_binary "$shared -z notext" $PLT_CFLAGS $test $source $libname $other $mapfile $verexp $versymexp $symexp ""
  477. }
  478. proc build_vers_lib_pic { test source libname other mapfile verexp versymexp symexp } {
  479. global picflag
  480. global shared
  481. build_binary $shared $picflag $test $source $libname $other $mapfile $verexp $versymexp $symexp ""
  482. }
  483. proc build_vers_lib_pic_flags { test source libname other mapfile verexp versymexp symexp ldargs } {
  484. global picflag
  485. global shared
  486. build_binary $shared $picflag $test $source $libname $other $mapfile $verexp $versymexp $symexp $ldargs
  487. }
  488. proc test_ldfail { test flag source execname other mapfile whyfail } {
  489. global srcdir
  490. global subdir
  491. global exec_output
  492. global host_triplet
  493. global tmpdir
  494. global as
  495. global as_options
  496. global objdump
  497. global CC_FOR_TARGET
  498. global script
  499. if [string match "" $other] then {
  500. set other_lib ""
  501. } else {
  502. set other_lib $tmpdir/$other
  503. }
  504. if ![ld_compile "$CC_FOR_TARGET -S $flag" $srcdir/$subdir/$source $tmpdir/$execname.s] {
  505. unsupported "$test"
  506. return
  507. }
  508. if ![ld_assemble $as "$as_options $tmpdir/$execname.s" $tmpdir/$execname.o ] {
  509. unsupported "$test"
  510. return
  511. }
  512. verbose -log "This link should fail because of $whyfail"
  513. if [string match "" $mapfile] then {
  514. set script_arg ""
  515. } else {
  516. set script_arg "-Wl,$script $srcdir/$subdir/$mapfile"
  517. }
  518. if {![ld_link $CC_FOR_TARGET $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]} {
  519. pass "$test"
  520. return
  521. }
  522. fail "$test"
  523. }
  524. proc test_asfail { test flag source execname whyfail } {
  525. global srcdir
  526. global subdir
  527. global tmpdir
  528. global as
  529. global CC_FOR_TARGET
  530. global NOLTO_CFLAGS
  531. if ![ld_compile "$CC_FOR_TARGET -S $flag $NOLTO_CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
  532. unsupported "$test"
  533. return
  534. }
  535. verbose -log "This assemble should fail because of $whyfail"
  536. catch "exec $as -o $tmpdir/$execname.o $tmpdir/$execname.s" exec_output
  537. set exec_output [prune_warnings $exec_output]
  538. if [string match "" $exec_output] then {
  539. fail "$test"
  540. return
  541. }
  542. verbose -log "$exec_output"
  543. pass "$test"
  544. }
  545. proc test_strip_vers_lib { test srclib libname verexp versymexp } {
  546. global strip
  547. global srcdir
  548. global subdir
  549. global exec_output
  550. global host_triplet
  551. global tmpdir
  552. global objdump
  553. if {! [file exists $tmpdir/$srclib] } then {
  554. fail "$test ($tmpdir/$srclib does not exist)"
  555. return
  556. }
  557. verbose -log "cp $tmpdir/$srclib $tmpdir/$libname.so"
  558. exec cp $tmpdir/$srclib $tmpdir/$libname.so
  559. verbose -log "$strip $tmpdir/$libname.so"
  560. catch "exec $strip $tmpdir/$libname.so" exec_output
  561. if [string match "" $exec_output] then {
  562. # If strip went OK, then run the usual tests on the thing to make sure that
  563. # it is sane.
  564. if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
  565. fail "$test"
  566. return
  567. }
  568. if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
  569. fail "$test"
  570. return
  571. }
  572. } else {
  573. verbose -log "$exec_output"
  574. fail "$test"
  575. return
  576. }
  577. pass $test
  578. }
  579. proc build_exec { test source execname flags solibname verexp versymexp symexp } {
  580. global srcdir
  581. global subdir
  582. global exec_output
  583. global host_triplet
  584. global tmpdir
  585. global as
  586. global as_options
  587. global objdump
  588. global CC_FOR_TARGET
  589. global NOSANITIZE_CFLAGS
  590. global NOLTO_CFLAGS
  591. set shared "--shared --no-undefined-version"
  592. set script --version-script
  593. # Disable LTO for these tests.
  594. set cc_cmd "$CC_FOR_TARGET -S"
  595. if {[check_lto_available]} {
  596. append cc_cmd " -fno-lto"
  597. }
  598. # Disable all sanitizers and LTO.
  599. append cc_cmd " $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
  600. if ![ld_compile $cc_cmd $srcdir/$subdir/$source $tmpdir/$execname.s] {
  601. unsupported "$test"
  602. return
  603. }
  604. if ![ld_assemble $as "$as_options $tmpdir/$execname.s" $tmpdir/$execname.o ] {
  605. unsupported "$test"
  606. return
  607. }
  608. if [string match "" $solibname] then {
  609. set solibname_lib ""
  610. } else {
  611. set solibname_lib $tmpdir/$solibname
  612. }
  613. if {![ld_link $CC_FOR_TARGET $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]} {
  614. fail "$test"
  615. return
  616. }
  617. if [string match "" $verexp] then {
  618. #
  619. # Make sure we get nothing back.
  620. #
  621. if {![objdump_emptyverstuff $objdump $tmpdir/$execname ]} {
  622. fail "$test"
  623. return
  624. }
  625. } else {
  626. if {![objdump_versionstuff $objdump $tmpdir/$execname $srcdir/$subdir/$verexp ]} {
  627. fail "$test"
  628. return
  629. }
  630. }
  631. if [string match "" $versymexp] then {
  632. if {![objdump_emptydynsymstuff $objdump $tmpdir/$execname ]} {
  633. fail "$test"
  634. return
  635. }
  636. } else {
  637. if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$versymexp ]} {
  638. fail "$test"
  639. return
  640. }
  641. }
  642. if [string match "" $symexp] then {
  643. if {![objdump_emptysymstuff $objdump $tmpdir/$execname.o ]} {
  644. fail "$test"
  645. return
  646. }
  647. } else {
  648. if {![objdump_symstuff $objdump $tmpdir/$execname.o $srcdir/$subdir/$symexp ]} {
  649. fail "$test"
  650. return
  651. }
  652. }
  653. pass $test
  654. }
  655. if { [istarget x86_64-*-linux*] \
  656. || [istarget arm*-*-*] \
  657. || ( [istarget mips*-*-linux*] && [at_least_gcc_version 4 3] ) } {
  658. # x86_64, ARM and newer MIPS toolchains do not like non-pic shared libraries
  659. set pic "yes"
  660. } else {
  661. set pic "no"
  662. }
  663. #
  664. # Basic test - build a library with versioned symbols.
  665. #
  666. build_vers_lib_pic "vers1" vers1.c vers1 "" vers1.map vers1.ver vers1.dsym vers1.sym
  667. #
  668. # Test #2 - build a library, and link it against the library we built in step
  669. # 1.
  670. #
  671. build_vers_lib_pic "vers2" vers2.c vers2 vers1.so vers2.map vers2.ver vers2.dsym ""
  672. #
  673. # Test #3 - build an executable, and link it against vers1.so.
  674. #
  675. build_exec "vers3" vers3.c vers3 "-Wl,--no-as-needed" vers1.so vers3.ver vers3.dsym ""
  676. #
  677. # Test #4 - Make sure a version implicitly defined in an executable
  678. # causes a version node to be created. Verify this both with and without
  679. # --export-dynamic.
  680. #
  681. # This test fails on MIPS. On the MIPS we must put foo in the dynamic
  682. # symbol table, which the test does not expect.
  683. setup_xfail "mips*-*-*"
  684. build_exec "vers4" vers4.c vers4 "" "" "" "" vers4.sym
  685. build_exec "vers4a" vers4.c vers4a "-Wl,-export-dynamic" "" vers4a.ver vers4a.dsym vers4a.sym
  686. # Verify that --no-export-dynamic undoes the effect of --export-dynamic.
  687. setup_xfail "mips*-*-*"
  688. build_exec "vers4b" vers4.c vers4b "-Wl,-export-dynamic -Wl,--no-export-dynamic" "" "" "" vers4.sym
  689. #
  690. # Try multiple definitions foo@BAR and foo@@BAR and make sure the linker
  691. # complains.
  692. #
  693. test_ldfail "vers5" "" vers5.c vers5 "" "" "multiple definition of foo@VERS_1.2"
  694. #
  695. #
  696. # Now build a test that should reference a bunch of versioned symbols.
  697. # All of them should be correctly referenced.
  698. #
  699. build_exec "vers6" vers6.c vers6 "-Wl,--no-as-needed" vers1.so vers6.ver vers6.dsym vers6.sym
  700. #
  701. # Another test to verify that something made local via 'local' is truly not
  702. # accessible.
  703. #
  704. if [string match "yes" $pic] then {
  705. xfail "vers7a"
  706. xfail "vers7"
  707. } else {
  708. build_vers_lib_no_pic "vers7a" vers7a.c vers7a "" vers7.map vers7a.ver vers7a.dsym vers7a.sym
  709. test_ldfail "vers7" "" vers7.c vers7 vers7a.so "" "undefined reference to hide_a"
  710. }
  711. #
  712. # This test is designed to verify that we can pass a linker script on the
  713. # command line as if it were a normal .o file.
  714. #
  715. catch "exec cp $srcdir/$subdir/vers8.map $tmpdir/" ignore_output
  716. build_vers_lib_pic "vers8" vers1.c vers8 vers8.map "" vers8.ver vers1.dsym vers1.sym
  717. #
  718. # This test tries to make sure that version references to versioned symbols
  719. # don't collide with default definitions with the same symbol.
  720. #
  721. build_exec "vers9" vers9.c vers9 "-Wl,-export-dynamic" "" vers9.ver vers9.dsym vers9.sym
  722. #
  723. # Try and use a non-existant version node. The linker should fail with
  724. # an error message.
  725. #
  726. test_ldfail "vers10" "-DDO_TEST10" vers1.c vers10 "" "vers1.map --shared" "invalid version"
  727. #
  728. # Try and some things the assembler should complain about.
  729. #
  730. test_asfail "vers11" "-DDO_TEST11" vers1.c vers11 "no @ in symver"
  731. test_asfail "vers12" "-DDO_TEST12" vers1.c vers12 "extern version definition"
  732. #
  733. # Put a shared library in an archive library, and make sure the global
  734. # archive symbol table is sane.
  735. #
  736. test_ar "ar with versioned solib" vers13.a vers1.so vers13.asym
  737. #
  738. # Strip a shared library, and make sure we didn't screw something up in there.
  739. #
  740. test_strip_vers_lib "vers14" vers1.so vers14 vers1.ver vers1.dsym
  741. #
  742. # Build another test with some versioned symbols. Here we are going to
  743. # try and override something from the library, and we shouldn't get
  744. # any errors.
  745. #
  746. build_exec "vers15" vers15.c vers15 "-Wl,--no-as-needed" vers1.so vers15.ver vers15.dsym vers15.sym
  747. #
  748. # Test that when we override a versioned symbol from the library this
  749. # symbol appears in the dynamic symbol table of the executable.
  750. #
  751. build_vers_lib_pic "vers16a" vers16a.c vers16a "" vers16.map vers16a.ver vers16a.dsym ""
  752. build_exec "vers16" vers16.c vers16 "-Wl,--no-as-needed" vers16a.so "" vers16.dsym ""
  753. # Test a weak versioned symbol.
  754. build_vers_lib_pic "vers17" vers17.c vers17 "" vers17.map vers17.ver vers17.dsym ""
  755. build_vers_lib_pic "vers18" vers18.c vers18 vers17.so vers18.map vers18.ver vers18.dsym vers18.sym
  756. build_exec "vers19" vers19.c vers19 "-Wl,--no-as-needed,-rpath,.,-rpath-link,." vers18.so vers19.ver vers19.dsym ""
  757. build_vers_lib_no_pic "vers20a" vers20.c vers20a "" vers20.map vers20a.ver vers20.dsym ""
  758. if {! [file exists $tmpdir/vers20a.so] } then {
  759. fail "Building vers20a.so"
  760. } else {
  761. exec cp $tmpdir/vers20a.so $tmpdir/vers20b.so
  762. build_vers_lib_no_pic "vers20" vers20.c vers20 "vers20a.so vers20b.so" vers20.map vers20.ver vers20.dsym ""
  763. }
  764. # Test .symver override.
  765. build_vers_lib_pic "vers21" vers21.c vers21 "" vers21.map vers21.ver vers21.dsym vers21.sym
  766. # Test moving default definition from one DSO to another.
  767. build_vers_lib_pic "vers22a" vers22a.c vers22a "" vers22.map vers22a.ver vers22a.dsym vers22a.sym
  768. build_vers_lib_pic "vers22b" vers22b.c vers22b "" vers22.map vers22b.ver vers22b.dsym ""
  769. build_vers_lib_pic "vers22" vers22.c vers22 "vers22a.so vers22b.so" "" vers22.ver vers22.dsym ""
  770. # Test versioned definitions in different files.
  771. if [string match "yes" $pic] then {
  772. xfail "vers23a"
  773. xfail "vers23b"
  774. xfail "vers23c"
  775. xfail "vers23d"
  776. xfail "vers23"
  777. } else {
  778. build_vers_lib_no_pic "vers23a" vers23a.c vers23a "" vers23a.map vers23a.ver vers23a.dsym vers23a.sym
  779. build_vers_lib_no_pic "vers23b" vers23b.c vers23b "" vers23b.map vers23b.ver vers23b.dsym ""
  780. build_vers_lib_no_pic "vers23c" vers23b.c vers23c "vers23a.so" vers23b.map vers23c.ver vers23b.dsym ""
  781. build_exec "vers23d" vers23.c vers23d "-Wl,--no-as-needed tmpdir/vers23a.so tmpdir/vers23c.so" "" vers23.ver vers23d.dsym ""
  782. build_exec "vers23" vers23.c vers23 "-Wl,--no-as-needed tmpdir/vers23a.so tmpdir/vers23b.o tmpdir/vers23b.so" "" vers23.ver vers23.dsym ""
  783. }
  784. # Test .symver x,x@VERS.0
  785. set as_pic_flags ""
  786. if [istarget sparc*-*-*] {
  787. set as_pic_flags "-K PIC"
  788. }
  789. run_ld_link_tests [list "\"vers24a\"
  790. \"-shared --version-script $srcdir/$subdir/vers24.map\" \"\"
  791. \"$as_pic_flags $as_options\" {vers24a.c vers24b.c} { { readelf -Wrs vers24.rd } }
  792. \"libvers24a.so\" \"-fpic\""]
  793. run_ld_link_tests [list "\"vers24b\"
  794. \"-shared --version-script $srcdir/$subdir/vers24.map\" \"\"
  795. \"$as_pic_flags $as_options\" {vers24b.c vers24a.c} { { readelf -Wrs vers24.rd } }
  796. \"libvers24b.so\" \"-fpic\""]
  797. run_ld_link_tests [list "\"vers24c\"
  798. \"-shared --version-script $srcdir/$subdir/vers24.map\" \"\"
  799. \"$as_pic_flags $as_options\" {vers24c.c} { { readelf -Wrs vers24.rd } }
  800. \"libvers24c.so\" \"-fpic\""]
  801. # Test versioned definition vs. normal definition in different files.
  802. if [string match "yes" $pic] then {
  803. xfail "vers25a"
  804. xfail "vers25b1"
  805. xfail "vers25b2"
  806. } else {
  807. build_vers_lib_no_pic "vers25a" vers25a.c vers25a "" vers25a.map vers25a.ver vers25a.dsym ""
  808. build_vers_lib_no_pic "vers25b1" vers25b.c vers25b1 "vers25a.o vers25a.so" "" vers25b.ver vers25b.dsym ""
  809. build_vers_lib_no_pic "vers25b2" vers25b.c vers25b2 "vers25a.so vers25a.o" "" vers25b.ver vers25b.dsym ""
  810. }
  811. build_vers_lib_pic "vers26a" vers26a.c vers26a "" vers26a.map vers26a.ver vers26a.dsym ""
  812. build_vers_lib_pic "vers26b1" vers26b.c vers26b1 "" "" vers26b.ver vers26b.dsym ""
  813. build_vers_lib_pic "vers26b2" vers26b.c vers26b2 "vers26a.so vers26b1.so vers26a.o" "" vers26b.ver vers26b.dsym ""
  814. if [string match "yes" $pic] then {
  815. xfail "vers26b3"
  816. } else {
  817. build_vers_lib_no_pic "vers26b3" vers26b.c vers26b3 "vers26a.so vers26b1.so vers26a.o" "" vers26b.ver vers26b.dsym ""
  818. }
  819. # Test versioned definition vs. hidden definition in different files.
  820. if [string match "yes" $pic] then {
  821. xfail "vers27a"
  822. xfail "vers27b"
  823. xfail "vers27c1"
  824. xfail "vers27c2"
  825. xfail "vers27d1"
  826. xfail "vers27d2"
  827. xfail "vers27d3"
  828. xfail "vers27d4"
  829. xfail "vers27d5"
  830. } else {
  831. build_vers_lib_no_pic "vers27a" vers27a.c vers27a "" vers27a.map vers27a.ver vers27a.dsym ""
  832. build_vers_lib_no_pic "vers27b" vers27b.c vers27b "" "" vers27b.ver vers27b.dsym ""
  833. build_vers_lib_no_pic "vers27c1" vers27c.c vers27c1 "vers27b.o vers27a.so" "" vers27c.ver vers27c.dsym ""
  834. build_vers_lib_no_pic "vers27c2" vers27c.c vers27c2 "vers27a.so vers27b.o" "" vers27c.ver vers27c.dsym ""
  835. build_vers_lib_pic "vers27d1" vers27d1.c vers27d1 "" vers27a.map vers27d.ver vers27d.dsym vers27d.sym
  836. build_vers_lib_pic "vers27d2" vers27d2.c vers27d2 "" "" vers27b.ver vers27b.dsym ""
  837. build_executable "vers27d3" vers27d3.c vers27d3 "vers27b.o vers27d2.so vers27d1.so" "" vers27b.ver vers27b.dsym ""
  838. build_vers_lib_pic "vers27d4" vers27d2.c vers27d4 "vers27a.so" "" vers27d4.ver vers27d4.dsym ""
  839. build_executable "vers27d5" vers27d3.c vers27d5 "vers27d4.so vers27b.o vers27a.so" "" vers27b.ver vers27b.dsym ""
  840. }
  841. # Test weak versioned definition vs. strong definition in different
  842. # files.
  843. build_vers_lib_pic "vers28a" vers28a.c vers28a "" "" vers28a.ver vers28a.dsym ""
  844. build_vers_lib_pic "vers28b" vers28b.c vers28b "" vers28b.map vers28b.ver vers28b.dsym ""
  845. build_vers_lib_pic "vers28c" vers28c.c vers28c "vers28b.so vers28a.so" "" vers28c.ver vers28c.dsym ""
  846. build_vers_lib_pic_flags "vers29" vers29.c vers29 "" "" vers29.ver vers29.dsym "" "--default-symver"
  847. # Test #30 - test handling of symbol names global, local and extern in the
  848. # version script.
  849. build_vers_lib_pic "vers30" vers30.c vers30 "" vers30.map vers30.ver vers30.dsym ""
  850. # Test #31 -- quoted strings in version sections.
  851. build_vers_lib_pic "vers31" vers31.c vers31 "" vers31.map vers31.ver vers31.dsym ""
  852. # Test #32 -- linker --defsym
  853. build_vers_lib_pic "vers32a" vers32a.c vers32a "" vers32.map vers32a.ver vers32a.dsym ""
  854. build_vers_lib_pic_flags "vers32b" vers32b.c vers32b "vers32a.so" vers32.map vers32b.ver vers32b.dsym "" "--defsym foo=0"