📄 ld-lib.exp
字号:
# Support routines for LD testsuite.# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002# Free Software Foundation, Inc.## This file is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.### default_ld_version# extract and print the version number of ld#proc default_ld_version { ld } { global host_triplet if { [which $ld] == 0 } then { perror "$ld does not exist" exit 1 } catch "exec $ld --version" tmp set tmp [prune_warnings $tmp] regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number if [info exists number] then { clone_output "$ld $number\n" }}## default_ld_relocate# link an object using relocation#proc default_ld_relocate { ld target objects } { global HOSTING_EMU global host_triplet if { [which $ld] == 0 } then { perror "$ld does not exist" return 0 } verbose -log "$ld $HOSTING_EMU -o $target -r $objects" catch "exec $ld $HOSTING_EMU -o $target -r $objects" exec_output set exec_output [prune_warnings $exec_output] if [string match "" $exec_output] then { return 1 } else { verbose -log "$exec_output" return 0 }}# Check to see if ld is being invoked with a non-endian output formatproc is_endian_output_format { object_flags } { if {[string match "*-oformat binary*" $object_flags] || \ [string match "*-oformat ieee*" $object_flags] || \ [string match "*-oformat ihex*" $object_flags] || \ [string match "*-oformat netbsd-core*" $object_flags] || \ [string match "*-oformat srec*" $object_flags] || \ [string match "*-oformat tekhex*" $object_flags] || \ [string match "*-oformat trad-core*" $object_flags] } then { return 0 } else { return 1 }}# Look for big-endian or little-endian switches in the multlib# options and translate these into a -EB or -EL switch. Note# we cannot rely upon proc process_multilib_options to do this# for us because for some targets the compiler does not support# -EB/-EL but it does support -mbig-endian/-mlittle-endian, and# the site.exp file will include the switch "-mbig-endian"# (rather than "big-endian") which is not detected by proc# process_multilib_options.proc big_or_little_endian {} { if [board_info [target_info name] exists multilib_flags] { set tmp_flags " [board_info [target_info name] multilib_flags]"; foreach x $tmp_flags { case $x in { {*big*endian eb EB -eb -EB} { set flags " -EB" return $flags } {*little*endian el EL -el -EL} { set flags " -EL" return $flags } } } } set flags "" return $flags}## default_ld_link# link a program using ld#proc default_ld_link { ld target objects } { global HOSTING_EMU global HOSTING_CRT0 global HOSTING_LIBS global LIBS global host_triplet global link_output set objs "$HOSTING_CRT0 $objects" set libs "$LIBS $HOSTING_LIBS" if { [which $ld] == 0 } then { perror "$ld does not exist" return 0 } if [is_endian_output_format $objects] then { set flags [big_or_little_endian] } else { set flags "" } verbose -log "$ld $HOSTING_EMU $flags -o $target $objs $libs" catch "exec $ld $HOSTING_EMU $flags -o $target $objs $libs" link_output set exec_output [prune_warnings $link_output] if [string match "" $link_output] then { return 1 } else { verbose -log "$link_output" return 0 }}## default_ld_simple_link# link a program using ld, without including any libraries#proc default_ld_simple_link { ld target objects } { global host_triplet global link_output if { [which $ld] == 0 } then { perror "$ld does not exist" return 0 } if [is_endian_output_format $objects] then { set flags [big_or_little_endian] } else { set flags "" } verbose -log "$ld $flags -o $target $objects" catch "exec $ld $flags -o $target $objects" link_output set exec_output [prune_warnings $link_output] # We don't care if we get a warning about a non-existent start # symbol, since the default linker script might use ENTRY. regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output if [string match "" $exec_output] then { return 1 } else { verbose -log "$exec_output" return 0 }}## default_ld_compile# compile an object using cc#proc default_ld_compile { cc source object } { global CFLAGS global srcdir global subdir global host_triplet global gcc_gas_flag set cc_prog $cc if {[llength $cc_prog] > 1} then { set cc_prog [lindex $cc_prog 0] } if {[which $cc_prog] == 0} then { perror "$cc_prog does not exist" return 0 } catch "exec rm -f $object" exec_output set flags "-I$srcdir/$subdir $CFLAGS" # If we are compiling with gcc, we want to add gcc_gas_flag to # flags. Rather than determine this in some complex way, we guess # based on the name of the compiler. if {[string match "*gcc*" $cc] || [string match "*++*" $cc]} then { set flags "$gcc_gas_flag $flags" } if [board_info [target_info name] exists multilib_flags] { append flags " [board_info [target_info name] multilib_flags]"; } verbose -log "$cc $flags -c $source -o $object" catch "exec $cc $flags -c $source -o $object" exec_output set exec_output [prune_warnings $exec_output] if [string match "" $exec_output] then { if {![file exists $object]} then { regexp ".*/(\[^/\]*)$" $source all dobj regsub "\\.c" $dobj ".o" realobj verbose "looking for $realobj" if {[file exists $realobj]} then { verbose -log "mv $realobj $object" catch "exec mv $realobj $object" exec_output set exec_output [prune_warnings $exec_output] if {![string match "" $exec_output]} then { verbose -log "$exec_output" perror "could not move $realobj to $object" return 0 } } else { perror "$object not found after compilation" return 0 } } return 1 } else { verbose -log "$exec_output" perror "$source: compilation failed" return 0 }}## default_ld_assemble# assemble a file#proc default_ld_assemble { as source object } { global ASFLAGS global host_triplet if {[which $as] == 0} then { perror "$as does not exist" return 0 } if ![info exists ASFLAGS] { set ASFLAGS "" } set flags [big_or_little_endian] verbose -log "$as $flags $ASFLAGS -o $object $source" catch "exec $as $flags $ASFLAGS -o $object $source" exec_output set exec_output [prune_warnings $exec_output] if [string match "" $exec_output] then { return 1 } else { verbose -log "$exec_output" perror "$source: assembly failed" return 0 }}## default_ld_nm# run nm on a file, putting the result in the array nm_output#proc default_ld_nm { nm nmflags object } { global NMFLAGS global nm_output global host_triplet if {[which $nm] == 0} then { perror "$nm does not exist" return 0 } if {[info exists nm_output]} { unset nm_output } if ![info exists NMFLAGS] { set NMFLAGS "" } # Ensure consistent sorting of symbols if {[info exists env(LC_ALL)]} { set old_lc_all $env(LC_ALL) } set env(LC_ALL) "C" verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out" catch "exec $nm $NMFLAGS $nmflags $object >tmpdir/nm.out" exec_output if {[info exists old_lc_all]} { set env(LC_ALL) $old_lc_all } else { unset env(LC_ALL) } set exec_output [prune_warnings $exec_output] if [string match "" $exec_output] then { set file [open tmpdir/nm.out r] while { [gets $file line] != -1 } { verbose "$line" 2 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] { set name [string trimleft $name "_"] verbose "Setting nm_output($name) to 0x$value" 2 set nm_output($name) 0x$value } } close $file return 1 } else { verbose -log "$exec_output" perror "$object: nm failed" return 0 }}## is_elf_format# true if the object format is known to be ELF#proc is_elf_format {} { if { ![istarget *-*-sysv4*] \ && ![istarget *-*-unixware*] \ && ![istarget *-*-elf*] \ && ![istarget *-*-eabi*] \ && ![istarget *-*-linux*] \ && ![istarget *-*-irix5*] \ && ![istarget *-*-irix6*] \ && ![istarget *-*-solaris2*] } { return 0 } if { [istarget *-*-linux*aout*] \ || [istarget *-*-linux*oldld*] } { return 0 } return 1}## simple_diff# compares two files line-by-line# returns differences if exist# returns null if file(s) cannot be opened#proc simple_diff { file_1 file_2 } { global target set eof -1 set differences 0 if [file exists $file_1] then { set file_a [open $file_1 r] } else { warning "$file_1 doesn't exist" return } if [file exists $file_2] then { set file_b [open $file_2 r] } else { fail "$file_2 doesn't exist" return } verbose "# Diff'ing: $file_1 $file_2\n" 2 while { [gets $file_a line] != $eof } { if [regexp "^#.*$" $line] then { continue } else { lappend list_a $line } } close $file_a while { [gets $file_b line] != $eof } { if [regexp "^#.*$" $line] then { continue } else { lappend list_b $line } } close $file_b for { set i 0 } { $i < [llength $list_a] } { incr i } { set line_a [lindex $list_a $i] set line_b [lindex $list_b $i] verbose "\t$file_1: $i: $line_a\n" 3 verbose "\t$file_2: $i: $line_b\n" 3 if [string compare $line_a $line_b] then { verbose -log "\t$file_1: $i: $line_a\n" verbose -log "\t$file_2: $i: $line_b\n" fail "Test: $target" return } } if { [llength $list_a] != [llength $list_b] } { fail "Test: $target" return } if $differences<1 then { pass "Test: $target" }}# run_dump_test FILE# Copied from gas testsuite, tweaked and further extended.## Assemble a .s file, then run some utility on it and check the output.## There should be an assembly language file named FILE.s in the test# suite directory, and a pattern file called FILE.d. `run_dump_test'# will assemble FILE.s, run some tool like `objdump', `objcopy', or# `nm' on the .o file to produce textual output, and then analyze that# with regexps. The FILE.d file specifies what program to run, and# what to expect in its output.## The FILE.d file begins with zero or more option lines, which specify# flags to pass to the assembler, the program to run to dump the# assembler's output, and the options it wants. The option lines have# the syntax:## # OPTION: VALUE## OPTION is the name of some option, like "name" or "objdump", and# VALUE is OPTION's value. The valid options are described below.# Whitespace is ignored everywhere, except within VALUE. The option# list ends with the first line that doesn't match the above syntax# (hmm, not great for error detection).## The interesting options are:## name: TEST-NAME# The name of this test, passed to DejaGNU's `pass' and `fail'# commands. If omitted, this defaults to FILE, the root of the# .s and .d files' names.## as: FLAGS# When assembling, pass FLAGS to the assembler.# If assembling several files, you can pass different assembler# options in the "source" directives. See below.## ld: FLAGS# Link assembled files using FLAGS, in the order of the "source"# directives, when using multiple files.## objcopy_linked_file: FLAGS# Run objcopy on the linked file with the specified flags.# This lets you transform the linked file using objcopy, before the# result is analyzed by an analyzer program specified below (which# may in turn *also* be objcopy).## PROG: PROGRAM-NAME# The name of the program to run to analyze the .o file produced# by the assembler or the linker output. This can be omitted;# run_dump_test will guess which program to run by seeing which of# the flags options below is present.## objdump: FLAGS# nm: FLAGS# objcopy: FLAGS# Use the specified program to analyze the assembler or linker# output file, and pass it FLAGS, in addition to the output name.# Note that they are run with LC_ALL=C in the environment to give# consistent sorting of symbols.## source: SOURCE [FLAGS]# Assemble the file SOURCE.s using the flags in the "as" directive# and the (optional) FLAGS. If omitted, the source defaults to# FILE.s.# This is useful if several .d files want to share a .s file.# More than one "source" directive can be given, which is useful# when testing linking.## xfail: TARGET# The test is expected to fail on TARGET. This may occur more than# once.## target: TARGET# Only run the test for TARGET. This may occur more than once; the# target being tested must match at least one.## notarget: TARGET# Do not run the test for TARGET. This may occur more than once;# the target being tested must not match any of them.## error: REGEX# An error with message matching REGEX must be emitted for the test# to pass. The PROG, objdump, nm and objcopy options have no# meaning and need not supplied if this is present.## Each option may occur at most once unless otherwise mentioned.## After the option lines come regexp lines. `run_dump_test' calls# `regexp_diff' to compare the output of the dumping tool against the# regexps in FILE.d. `regexp_diff' is defined later in this file; see# further comments there.proc run_dump_test { name } { global subdir srcdir global OBJDUMP NM AS OBJCOPY READELF LD global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS global host_triplet runtests global env if [string match "*/*" $name] { set file $name set name [file tail $name] } else { set file "$srcdir/$subdir/$name" } if ![runtest_file_p $runtests $name] then { return } set opt_array [slurp_options "${file}.d"] if { $opt_array == -1 } { perror "error reading options from $file.d" unresolved $subdir/$name return } set dumpfile tmpdir/dump.out set run_ld 0 set run_objcopy 0 set opts(as) {} set opts(ld) {} set opts(xfail) {} set opts(target) {} set opts(notarget) {} set opts(objdump) {} set opts(nm) {} set opts(objcopy) {} set opts(readelf) {} set opts(name) {} set opts(PROG) {} set opts(source) {} set opts(error) {} set opts(objcopy_linked_file) {} set asflags(${file}.s) {}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -