unique.exp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. # Expect script for linker support of STB_GNU_UNIQUE symbols
  2. #
  3. # Copyright (C) 2009-2022 Free Software Foundation, Inc.
  4. # Contributed by Red Hat.
  5. #
  6. # This file is part of the GNU Binutils.
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  21. # MA 02110-1301, USA.
  22. #
  23. # Written by Nick Clifton <nickc@redhat.com>
  24. # Adapted for unique checking by Mark J. Wielaard <mjw@redhat.com>
  25. # Exclude non-ELF targets.
  26. if { ![is_elf_format] } {
  27. return
  28. }
  29. # Require STB_GNU_UNIQUE support with OSABI set to GNU.
  30. if { ![supports_gnu_unique] || [istarget tic6x-*-*] } {
  31. verbose "UNIQUE tests not run - target does not support UNIQUE"
  32. return
  33. }
  34. set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
  35. foreach t $test_list {
  36. # We need to strip the ".d", but can leave the dirname.
  37. verbose [file rootname $t]
  38. run_dump_test [file rootname $t]
  39. }
  40. # We need a working compiler. (Strictly speaking this is
  41. # not true, we could use target specific assembler files).
  42. if { ![check_compiler_available] } {
  43. verbose "UNIQUE compiled tests not run - no compiler available"
  44. return
  45. }
  46. # A procedure to check the OS/ABI field in the ELF header of a binary file.
  47. proc check_osabi { binary_file expected_osabi } {
  48. global READELF
  49. global READELFFLAGS
  50. catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
  51. if ![string match "" $got] then {
  52. verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
  53. return 0
  54. }
  55. if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
  56. [file_contents readelf.out] nil osabi] } {
  57. verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
  58. return 0
  59. }
  60. if { $osabi == $expected_osabi } {
  61. return 1
  62. }
  63. verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
  64. return 0
  65. }
  66. # A procedure to confirm that a file contains the UNIQUE symbol.
  67. # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
  68. proc contains_unique_symbol { binary_file } {
  69. global READELF
  70. global READELFFLAGS
  71. catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
  72. if ![string match "" $got] then {
  73. verbose "proc contains_unique_symbol: Readelf produced unexpected out processing $binary_file: $got"
  74. return -1
  75. }
  76. # Look for a line like this:
  77. # 54: 0000000000400474 4 OBJECT UNIQUE DEFAULT 13 a
  78. if { ![regexp ".*\[ \]*OBJECT\[ \]+UNIQUE\[ \]+DEFAULT\[ \]+\[UND0-9\]+\[ \]+\[ab\]_val\n" [file_contents readelf.out]] } {
  79. return 0
  80. }
  81. return 1
  82. }
  83. set fails 0
  84. # Create object file containing unique symbol.
  85. if ![ld_compile "$CC_FOR_TARGET -c" "$srcdir/$subdir/unique.s" "tmpdir/unique.o"] {
  86. fail "Could not create a unique object"
  87. set fails [expr $fails + 1]
  88. }
  89. # Create object file NOT containing unique symbol.
  90. if ![ld_compile "$CC_FOR_TARGET -c" "$srcdir/$subdir/unique_empty.s" "tmpdir/unique_empty.o"] {
  91. fail "Could not create a non-unique object"
  92. set fails [expr $fails + 1]
  93. }
  94. # When using GCC as the linker driver, we need to specify board cflags when
  95. # linking because cflags may contain linker options. For example when linker
  96. # options are included in GCC spec files then we need the -specs option.
  97. if [board_info [target_info name] exists cflags] {
  98. set board_cflags " [board_info [target_info name] cflags]"
  99. } else {
  100. set board_cflags ""
  101. }
  102. # Create executable containing unique symbol.
  103. if ![ld_link "$CC_FOR_TARGET $NOPIE_LDFLAGS $board_cflags" "tmpdir/unique_prog" "tmpdir/unique.o"] {
  104. fail "Could not link a unique executable"
  105. set fails [expr $fails + 1]
  106. }
  107. if { $fails != 0 } {
  108. return
  109. }
  110. # Check the object file.
  111. if {! [check_osabi tmpdir/unique.o {UNIX - GNU}]} {
  112. fail "Object containing unique does not have an OS/ABI field of GNU"
  113. set fails [expr $fails + 1]
  114. }
  115. if {[contains_unique_symbol tmpdir/unique.o] != 1} {
  116. fail "Object containing unique does not contain an UNIQUE symbol"
  117. set fails [expr $fails + 1]
  118. }
  119. if { $fails == 0 } {
  120. pass "Checking unique object"
  121. }
  122. # Check the executable.
  123. if {! [check_osabi tmpdir/unique_prog {UNIX - GNU}]} {
  124. fail "Executable containing unique does not have an OS/ABI field of GNU"
  125. set fails [expr $fails + 1]
  126. }
  127. if {[contains_unique_symbol tmpdir/unique_prog] != 1} {
  128. fail "Executable containing unique does not contain an UNIQUE symbol"
  129. set fails [expr $fails + 1]
  130. }
  131. if { $fails == 0 } {
  132. pass "Checking unique executable"
  133. }
  134. # Check the empty object file.
  135. switch -glob $target_triplet {
  136. hppa*-*-linux* { set expected_none {UNIX - GNU} }
  137. default { set expected_none {UNIX - System V} }
  138. }
  139. if {! [check_osabi tmpdir/unique_empty.o $expected_none]} {
  140. fail "Object NOT containing unique does not have an OS/ABI field of $expected_none"
  141. set fails [expr $fails + 1]
  142. }
  143. if {[contains_unique_symbol tmpdir/unique_empty.o] == 1} {
  144. fail "Object NOT containing unique does contain an UNIQUE symbol"
  145. set fails [expr $fails + 1]
  146. }
  147. if { $fails == 0 } {
  148. pass "Checking empty unique object"
  149. }
  150. # ------------------------------------------------------------------------------
  151. # Only shared library tests below.
  152. # ------------------------------------------------------------------------------
  153. if { ![check_shared_lib_support] } {
  154. return
  155. }
  156. # Create pic object file containing unique symbol.
  157. if {![ld_compile "$CC_FOR_TARGET -c -fPIC" "$srcdir/$subdir/unique_shared.s" "tmpdir/unique_shared.o"] } {
  158. fail "Could not create a pic unique object"
  159. set fails [expr $fails + 1]
  160. }
  161. # Create shared library containing unique symbol.
  162. if {![ld_link $ld "tmpdir/libunique_shared.so" "-shared tmpdir/unique_shared.o"] } {
  163. fail "Could not create a shared library containing an unique symbol"
  164. set fails [expr $fails + 1]
  165. }
  166. # Create executable NOT containing unique symbol linked against library.
  167. if {![ld_link "$CC_FOR_TARGET $NOPIE_LDFLAGS $board_cflags" "tmpdir/unique_shared_prog" "-Ltmpdir tmpdir/unique_empty.o -Wl,-Bdynamic,-rpath=./tmpdir -lunique_shared"] } {
  168. fail "Could not link a dynamic executable"
  169. set fails [expr $fails + 1]
  170. }
  171. # Create shared library containing unique symbol with reference.
  172. if {![ld_link $ld "tmpdir/libunique_shared_ref.so" "-shared -z notext tmpdir/unique_shared.o tmpdir/unique_empty.o"] } {
  173. fail "Could not create a shared library containing an unique symbol with reference"
  174. set fails [expr $fails + 1]
  175. }
  176. if { $fails != 0 } {
  177. return
  178. }
  179. # Check the unique PIC file.
  180. if {! [check_osabi tmpdir/unique_shared.o {UNIX - GNU}]} {
  181. fail "PIC Object containing unique does not have an OS/ABI field of GNU"
  182. set fails [expr $fails + 1]
  183. }
  184. if {[contains_unique_symbol tmpdir/unique_shared.o] != 1} {
  185. fail "PIC Object containing unique does not contain an UNIQUE symbol"
  186. set fails [expr $fails + 1]
  187. }
  188. if { $fails == 0 } {
  189. pass "Checking unique PIC object 1"
  190. }
  191. # Check the unique shared library.
  192. if {! [check_osabi tmpdir/libunique_shared.so {UNIX - GNU}]} {
  193. fail "Shared library containing unique does not have an OS/ABI field of GNU"
  194. set fails [expr $fails + 1]
  195. }
  196. if {[contains_unique_symbol tmpdir/libunique_shared.so] != 1} {
  197. fail "Shared library containing unique does not contain an UNIQUE symbol"
  198. set fails [expr $fails + 1]
  199. }
  200. # Check the unique shared library with reference.
  201. if {! [check_osabi tmpdir/libunique_shared_ref.so {UNIX - GNU}]} {
  202. fail "Shared library containing unique with reference does not have an OS/ABI field of GNU"
  203. set fails [expr $fails + 1]
  204. }
  205. if {[contains_unique_symbol tmpdir/libunique_shared_ref.so] != 1} {
  206. fail "Shared library containing unique with reference does not contain an UNIQUE symbol"
  207. set fails [expr $fails + 1]
  208. }
  209. if { $fails == 0 } {
  210. pass "Checking unique PIC object 2"
  211. }
  212. # Check the empty executable linked against unique shared library.
  213. if {! [check_osabi tmpdir/unique_shared_prog $expected_none]} {
  214. fail "Executable NOT containing unique does not have an OS/ABI field of $expected_none"
  215. set fails [expr $fails + 1]
  216. }
  217. if {[contains_unique_symbol tmpdir/unique_shared_prog] == 1} {
  218. fail "Executable NOT containing unique does contain an UNIQUE symbol"
  219. set fails [expr $fails + 1]
  220. }
  221. if { $fails == 0 } {
  222. pass "Checking shared empty executable"
  223. }