binutils.exp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # Expect script for binutils tests
  2. # Copyright (C) 2006-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 (hongjiu.lu@intel.com)
  22. #
  23. # Make sure that binutils can correctly handle ld output in ELF.
  24. if { !([istarget *-*-linux*]
  25. || [istarget arm*-*-uclinuxfdpiceabi]
  26. || [istarget *-*-nacl*]
  27. || [istarget *-*-gnu*]) || [istarget *ecoff] } then {
  28. return
  29. }
  30. # The optional test_name argument provides a mechanism for the caller
  31. # to hardwire the test name. This is important if ld_options contains
  32. # absolute path names because the default test name is constructed
  33. # from the prog_name and ld_options and we do not want absolute paths
  34. # to appear in the test_name. The optional readelf_options can be
  35. # used to specify different options for readelf.
  36. proc binutils_test { prog_name ld_options test {test_name ""} {readelf_options "-l"}} {
  37. global as
  38. global ld
  39. global READELF
  40. global objcopy
  41. global strip
  42. global srcdir
  43. global subdir
  44. global link_output
  45. eval set prog \$$prog_name
  46. if { "$test_name" == "" } {
  47. set test_name "$prog_name $ld_options ($test)"
  48. }
  49. if { ![ld_assemble $as $srcdir/$subdir/$test.s tmpdir/$test.o ] } {
  50. unsupported "$test_name"
  51. return
  52. }
  53. if { ![ld_link $ld tmpdir/$test "$ld_options -z stack-size=0 tmpdir/$test.o"] } {
  54. if { [string match "*not supported*" $link_output]
  55. || [string match "*unrecognized option*" $link_output]
  56. || [string match "*-z * ignored*" $link_output] } {
  57. unsupported "$ld_options is not supported by this target"
  58. } else {
  59. fail "$test_name"
  60. }
  61. return
  62. }
  63. send_log "$READELF $readelf_options --wide tmpdir/$test > tmpdir/$test.exp\n"
  64. set got [remote_exec host "$READELF $readelf_options --wide tmpdir/$test" "" "/dev/null" "tmpdir/$test.exp"]
  65. if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
  66. send_log "$got\n"
  67. fail "$test_name"
  68. return
  69. }
  70. if [string match "*-z relro*" $ld_options] {
  71. # These targets set TEXT_DYNAMIC and therefore have `.dynamic'
  72. # in the (read-only) text segment rather than data segment.
  73. set relro_dynamic [expr !([istarget "hppa64*-*-*"] \
  74. || ([istarget "mips*-*-*"] \
  75. && ![istarget "*-*-vxworks*"]))]
  76. # These targets have `.got' in regular data even though they do
  77. # not have `.got' there.
  78. set relro_gotplt [expr [istarget "mips*-*-*"]]
  79. # These targets do not set NO_SMALL_DATA or DATA_GOT and therefore
  80. # have `.got' in (read-write) small data rather than regular data.
  81. set relro_got [expr !([istarget "alpha*-*-*"] \
  82. || [istarget "hppa*-*-linux-*"] \
  83. || [istarget "hppa*-*-netbsd*"] \
  84. || [istarget "hppa*-*-openbsd*"] \
  85. || [istarget "metag-*-*"] \
  86. || [istarget "mips*-*-*"] \
  87. || [istarget "nios2*-*-*"] \
  88. || [istarget "or1k-*-*"] \
  89. || [istarget "riscv*-*-*"] \
  90. || [istarget "sh*-*-*"] \
  91. || [istarget "x86_64-*-rdos*"])]
  92. # Check if GNU_RELRO segment is generated.
  93. set got [remote_exec host "grep GNU_RELRO tmpdir/$test.exp"]
  94. if { ![string match "*GNU_RELRO*" $got] } then {
  95. set got [remote_exec host "cat tmpdir/$test.exp"]
  96. if { [string match "*.data.rel.ro*" $got]
  97. || ($relro_dynamic && [string match "*.dynamic*" $got])
  98. || ($relro_gotplt && [string match "*.got.plt*" $got])
  99. || ($relro_got && [string match "*.got*" $got])
  100. || [string match "*.eh_frame*" $got]
  101. || [string match "*.gcc_except_table*" $got]
  102. || [string match "*.exception_ranges*" $got]
  103. || [string match "*.ctors*" $got]
  104. || [string match "*.dtors*" $got]
  105. || [string match "*.tdata*" $got]
  106. || [string match "*.preinit_array*" $got]
  107. || [string match "*.init_array*" $got]
  108. || [string match "*.fini_array*" $got] } then {
  109. # Fail if GNU_RELRO segment isn't generated, but should.
  110. fail "$test_name"
  111. return
  112. }
  113. }
  114. }
  115. send_log "$prog tmpdir/$test\n"
  116. set got [remote_exec host "$prog tmpdir/$test"]
  117. if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
  118. send_log "$got\n"
  119. fail "$test_name"
  120. return
  121. }
  122. send_log "$READELF $readelf_options --wide tmpdir/$test > tmpdir/$test.out\n"
  123. set got [remote_exec host "$READELF $readelf_options --wide tmpdir/$test" "" "/dev/null" "tmpdir/$test.out"]
  124. if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
  125. send_log "$got\n"
  126. fail "$test_name"
  127. return
  128. }
  129. if { [catch {exec cmp tmpdir/$test.exp tmpdir/$test.out}] } then {
  130. send_log "tmpdir/$test.exp tmpdir/$test.out differ.\n"
  131. fail "$test_name"
  132. return
  133. }
  134. pass "$test_name"
  135. }
  136. binutils_test strip "-z max-page-size=0x200000" maxpage1
  137. binutils_test strip "-z max-page-size=0x200000 -z common-page-size=0x100000" maxpage1
  138. binutils_test strip "-z max-page-size=0x100000" maxpage1
  139. binutils_test strip "-z max-page-size=0x100000 -z common-page-size=0x1000" maxpage1
  140. binutils_test strip "" maxpage1
  141. binutils_test strip "-shared" maxpage1
  142. binutils_test objcopy "" maxpage1
  143. binutils_test objcopy "-shared" maxpage1
  144. binutils_test strip "-z relro" relro1
  145. binutils_test strip "-z relro -shared" relro1
  146. binutils_test objcopy "-z relro" relro1
  147. binutils_test objcopy "-z relro -shared" relro1
  148. if { ([istarget "i?86-*-elf*"]
  149. || [istarget "i?86-*-linux*"]
  150. || [istarget "i?86-*-gnu*"]
  151. || [istarget "x86_64-*-linux*"]
  152. || [istarget "amd64-*-linux*"]) } {
  153. binutils_test strip "-z relro -shared" relro2
  154. binutils_test objcopy "-z relro -shared" relro2
  155. }
  156. binutils_test strip "-T ${srcdir}/${subdir}/lma.lnk" lma "strip -T lma.lnk"
  157. set tls_tests { "tdata1" "tdata2" }
  158. # hppa64 has its own .tbss section, with different flags.
  159. if { ![istarget "hppa64-*-*"] } {
  160. lappend tls_tests "tdata3" "tbss1" "tbss2" "tbss3"
  161. }
  162. set tls_opts {
  163. "-z noseparate-code"
  164. "-z separate-code"
  165. "-z relro"
  166. "-shared -z noseparate-code"
  167. "-shared -z separate-code"
  168. "-shared -z relro"
  169. "-z max-page-size=0x100000"
  170. "-z max-page-size=0x100000 -z common-page-size=0x1000"
  171. }
  172. foreach testitem $tls_tests {
  173. foreach testopt $tls_opts {
  174. binutils_test objcopy $testopt $testitem
  175. }
  176. }
  177. binutils_test strip "-s" gap "" "-lS"