elfcomm.exp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # Expect script for common symbol tests
  2. # Copyright (C) 2003-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 H.J. Lu (hjl@gnu.org)
  22. #
  23. # Make sure that ld correctly handles common symbols in ELF.
  24. # This test can only be run on ELF platforms.
  25. if ![is_elf_format] {
  26. return
  27. }
  28. # hpux assembly is weird
  29. if [istarget "hppa*-*-hpux*"] {
  30. return
  31. }
  32. proc test_sort_common {} {
  33. global exec_output
  34. global objdump
  35. global srcdir
  36. global subdir
  37. global as
  38. global ld
  39. set test "--sort-common (descending)"
  40. verbose "Check to see that --sort-common sorts in descending alignment"
  41. # We do not run the sort common tests for the DLX target because we know that the linker
  42. # will seg-fault. The built-in DLX linker script requires that there be something in the
  43. # .text section and our sort-common.s file does not provide anything.
  44. if [istarget dlx-*-*] {
  45. untested "$test"
  46. return 0
  47. }
  48. if { ![ld_assemble $as $srcdir/$subdir/sort-common.s tmpdir/sort-common.o] } {
  49. unsupported "$test"
  50. return 0
  51. }
  52. if { ![ld_link $ld tmpdir/sort-common.dx "--sort-common=descending tmpdir/sort-common.o"] } {
  53. fail "$test (could not link)"
  54. return 0
  55. }
  56. send_log "$objdump --syms tmpdir/sort-common.dx | grep var | sort\n"
  57. set exec_output [run_host_cmd "$objdump" "--syms tmpdir/sort-common.dx | grep var | sort"]
  58. # Don't know why, but the CR ports fail this test.
  59. setup_xfail "cr16-*-*" "crx-*-*"
  60. # Note: The second regexp is for targets which put small commons in a .sbss
  61. # section and large commons in a .bss section.
  62. if { ![regexp ".*var_16.*var_8.*var_4.*var_2.*var_1.*" $exec_output]
  63. && ![regexp ".*sbss.*var_8.*var_4.*var_2.*var_1.*bss.*var_16.*" $exec_output] } {
  64. fail "$test (variables in wrong order)"
  65. } else {
  66. pass $test
  67. }
  68. set test "--sort-common (ascending)"
  69. verbose "Check to see that --sort-common=ascending sorts in ascending alignment"
  70. if { ![ld_link $ld tmpdir/sort-common.ax "--sort-common=ascending tmpdir/sort-common.o"] } {
  71. fail "$test (could not link)"
  72. return 0
  73. }
  74. send_log "$objdump --syms tmpdir/sort-common.ax | grep var | sort\n"
  75. set exec_output [run_host_cmd "$objdump" "--syms tmpdir/sort-common.ax | grep var | sort"]
  76. if {![regexp ".*var_1.*var_2.*var_4.*var_8.*var_16.*" $exec_output]} {
  77. fail "$test (variables in wrong order)"
  78. return 0
  79. }
  80. pass $test
  81. return 1
  82. }
  83. test_sort_common
  84. set test1 "size/alignment change of common symbols"
  85. set test1w1 "$test1 (warning 1)"
  86. set test1w2 "$test1 (warning 2)"
  87. set test1c1 "$test1 (change 1)"
  88. set test1c2 "$test1 (change 2)"
  89. if { ![check_compiler_available] } {
  90. untested $test1w1
  91. untested $test1w2
  92. untested $test1c1
  93. untested $test1c2
  94. return
  95. }
  96. if { [istarget score-*-*] } {
  97. untested $test1w1
  98. untested $test1w2
  99. untested $test1c1
  100. untested $test1c2
  101. return
  102. }
  103. proc dump_common1 { testname } {
  104. global exec_output
  105. global READELF
  106. send_log "$READELF --syms tmpdir/common1.o | grep foo\n"
  107. set exec_output [run_host_cmd "$READELF" "--syms tmpdir/common1.o | grep foo"]
  108. if { ![regexp "(\[ \]*)(\[0-9\]+):(\[ \]*)(\[0\]*)80(\[ \]+)4(\[ \]+)(COMMON|OBJECT)(\[ \]+)GLOBAL(\[ \]+)DEFAULT(\[ \]+)(PRC\\\[0xff03\\\]|COM|SCOM)(\[ \]+)_?foo2" $exec_output]
  109. || ![regexp "(\[ \]*)(\[0-9\]+):(\[ \]*)(\[0-9\]+)(\[ \]+)21(\[ \]+)OBJECT(\[ \]+)GLOBAL(\[ \]+)DEFAULT(\[ \]+)(\[0-9\]+)(\[ \]+)_?foo1" $exec_output] } {
  110. verbose $exec_output
  111. fail $testname
  112. return 0
  113. }
  114. return 1
  115. }
  116. proc stt_common_test { options testname } {
  117. global exec_output
  118. global READELF
  119. global ld
  120. set options "$options tmpdir/common1c.o"
  121. if { ! [ld_link $ld tmpdir/common.exe $options] } {
  122. fail $testname
  123. return 0
  124. }
  125. send_log "$READELF --syms tmpdir/common.exe | grep foo\n"
  126. set exec_output [run_host_cmd "$READELF" "--syms tmpdir/common.exe | grep foo"]
  127. if {![regexp { +[0-9a-f]+. +[0-9a-f]+ OBJECT + GLOBAL +DEFAULT +[0-9]+ _?foo2} $exec_output] } {
  128. fail $testname
  129. return 0
  130. }
  131. pass $testname
  132. return 1
  133. }
  134. # Explicitly use "-fcommon" so that even if $CFLAGS includes
  135. # "-fno-common", these tests are compiled as expected.
  136. set options "-fcommon $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
  137. if [istarget nios2*-*-*] {
  138. append options " -G0"
  139. }
  140. if { ![ld_compile "$CC_FOR_TARGET $options" $srcdir/$subdir/common1a.c tmpdir/common1a.o]
  141. || ![ld_compile "$CC_FOR_TARGET $options" $srcdir/$subdir/common1b.c tmpdir/common1b.o]
  142. || ![ld_compile "$CC_FOR_TARGET $options -Wa,--elf-stt-common=yes" $srcdir/$subdir/common1b.c tmpdir/common1c.o] } {
  143. unsupported $test1
  144. return
  145. }
  146. set options "-r tmpdir/common1a.o tmpdir/common1b.o"
  147. if { [ld_link $ld tmpdir/common1.o $options] } {
  148. fail $test1w1
  149. return
  150. }
  151. # This test fails on MIPS because the backend sets type_change_ok.
  152. # The size change warning is suppressed. Same on hppa64.
  153. if {[istarget mips*-*-*] || [istarget hppa*64*-*-*]} {
  154. if { ![regexp "warning: alignment (\[0-9\]+) of symbol \`_?foo1\' in tmpdir/common1b.o is smaller than 64 in tmpdir/common1a.o" $link_output] } {
  155. fail $test1w1
  156. } else {
  157. pass $test1w1
  158. }
  159. } else {
  160. if { ![regexp "warning: alignment (\[0-9\]+) of symbol \`_?foo1\' in tmpdir/common1b.o is smaller than 64 in tmpdir/common1a.o" $link_output]
  161. || ![regexp "warning: size of symbol \`_?foo1\' changed from 2 in tmpdir/common1a.o to 21 in tmpdir/common1b.o" $link_output] } {
  162. fail $test1w1
  163. } else {
  164. pass $test1w1
  165. }
  166. }
  167. if { [dump_common1 $test1c1] } {
  168. pass $test1c1
  169. }
  170. set options "-r tmpdir/common1b.o tmpdir/common1a.o"
  171. if { [ld_link $ld tmpdir/common1.o $options] } {
  172. fail $test1w2
  173. return
  174. }
  175. if { ![regexp "warning: alignment (\[0-9\]+) of symbol \`_?foo1\' in tmpdir/common1b.o is smaller than 64 in tmpdir/common1a.o" $link_output] } {
  176. fail $test1w2
  177. } else {
  178. pass $test1w2
  179. }
  180. if { [dump_common1 $test1c2] } {
  181. pass $test1c2
  182. }
  183. #
  184. # The following tests are for when we are generating STT_COMMON symbols only.
  185. #
  186. # Handling of -z common and -z nocommon flags is enabled only if
  187. # $GENERATE_SHLIB_SCRIPT is turned on in emulparams i.e. shared
  188. # libraries are supported.
  189. if ![check_shared_lib_support] {
  190. return
  191. }
  192. stt_common_test "-static -e 0" "static link of common symbols"
  193. stt_common_test "-shared" "shared link of common symbols"
  194. stt_common_test "-pie" "position independent link of common symbols"
  195. run_ld_link_tests [list \
  196. [list \
  197. "Build common-3x.o" \
  198. "-r" "" "--elf-stt-common=no" \
  199. {common-1.s} {} "common-3x.o" \
  200. ] \
  201. [list \
  202. "Build common-3y.o" \
  203. "-r" "" "--elf-stt-common=yes" \
  204. {common-1.s} {} "common-3y.o" \
  205. ] \
  206. [list \
  207. "Build common-3a.o" \
  208. "-r tmpdir/common-3x.o tmpdir/common-3y.o" "" "" \
  209. {dummy.s} {{readelf {-s -W} common-3a.rd}} "common-3a.o" \
  210. ] \
  211. [list \
  212. "Build common-3b.o" \
  213. "-r tmpdir/common-3y.o tmpdir/common-3x.o" "" "" \
  214. {dummy.s} {{readelf {-s -W} common-3b.rd}} "common-3b.o" \
  215. ] \
  216. [list \
  217. "Build common-3c.o" \
  218. "-r -z nocommon tmpdir/common-3x.o tmpdir/common-3y.o" "" "" \
  219. {dummy.s} {{readelf {-s -W} common-3a.rd}} "common-3c.o" \
  220. ] \
  221. [list \
  222. "Build common-3d.o" \
  223. "-r -z common tmpdir/common-3x.o tmpdir/common-3y.o" "" "" \
  224. {dummy.s} {{readelf {-s -W} common-3b.rd}} "common-3b.o" \
  225. ] \
  226. [list \
  227. "Build common-3e.o" \
  228. "-r -z common tmpdir/common-3y.o tmpdir/common-3x.o" "" "" \
  229. {dummy.s} {{readelf {-s -W} common-3b.rd}} "common-3e.o" \
  230. ] \
  231. [list \
  232. "Build common-3f.o" \
  233. "-r -z nocommon tmpdir/common-3y.o tmpdir/common-3x.o" "" "" \
  234. {dummy.s} {{readelf {-s -W} common-3a.rd}} "common-3f.o" \
  235. ] \
  236. ]
  237. set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
  238. foreach t $test_list {
  239. # We need to strip the ".d", but can leave the dirname.
  240. verbose [file rootname $t]
  241. run_dump_test [file rootname $t]
  242. }