aboutsummaryrefslogtreecommitdiff
path: root/ld/testsuite/ld-plugin/libdep.exp
blob: e0afcfc342a855a9f5b28014b7552f5139b4ee24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Expect script for ld-plugin LIBDEP tests
#   Copyright (C) 2024 Free Software Foundation, Inc.
#
# This file is part of the GNU Binutils.
#
# This program 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
# MA 02110-1301, USA.

# These tests require the plugin API.
if { ![check_plugin_api_available] } {
    return
}

# Check to see if the C compiler works
# FIXME: This is being lazy, we could create assembler equivalents instead.
if { ![check_compiler_available] } {
    return
}

proc run_test { } {
    global CC_FOR_TARGET
    global srcdir
    global subdir
    global ar
    global ld
    global libdep
    global base_dir
    
    set testname "libdep test"
    
    # Create temporary directories if they do not already exist.
    file mkdir -p libdep-a libdep-b

    # Delete old versions of the files we are about to build, just in case.
    file delete libdep-a/a.o libdep-a/liba.a libdep-b/b.o libdep-b/libc.a libdep-main.o

    # Build object files.
    if {![ld_compile $CC_FOR_TARGET $srcdir/$subdir/libdep-a.c libdep-a/a.o]} {
	fail "$testname: could not compile source file 1"
	return
    }
    if {![ld_compile $CC_FOR_TARGET $srcdir/$subdir/libdep-b.c libdep-b/b.o]} {
	fail "$testname: could not compile source file 2"
	return
    }
    if {![ld_compile $CC_FOR_TARGET $srcdir/$subdir/libdep-main.c libdep-main.o]} {
	fail "$testname: could not compile source file 3"
	return
    }

    # Create static archives from the objects.

    # For the first archive we add a libdep element that loads the second library.
    if {![ar_simple_create $ar {--record-libdeps "-Llibdep-b -lc"} libdep-a/liba.a libdep-a/a.o]} {
	fail "$testname: could not create archive 1"
	return
    }

    # For the second archive we choose a name - libc.a - that is likely to clash
    # with a system library.  This will help to check that the library loaded by
    # following the libdep element in the first library is the one that we expect.
    if {![ar_simple_create $ar {} libdep-b/libc.a libdep-b/b.o]} {
	fail "$testname: could not create archive 2"
	return
    }

    # Find the libdep plugin.
    # Use libtool to find the path to the plugin rather
    # than worrying about run paths or anything like that.
    catch "exec $base_dir/libtool --config" lt_config
    verbose "Full lt config: $lt_config" 2
    # Look for "objdir=.libs"
    regexp -line "^objdir=.*$" "$lt_config" lt_objdir
    verbose "lt_objdir line is '$lt_objdir'" 2
    set lt_objdir [regsub "objdir=" "$lt_objdir" ""]

    if { [ file exists "$base_dir/$lt_objdir/libdep.so" ] } {
	set libdep "$base_dir/$lt_objdir/libdep.so"
    } else {
	# FIXME: Check in the system bfd-plugin directory ?
	fail "$testname - could not locate libdep plugin"
    }

    verbose "Full plugin path: $libdep" 1
    
    # Link the main object file with the liba.a library.
    # Use the libdep plugin to read the __.LIBDEP element in the liba.a library
    # and so bring in the libdep-b.o object file from the libc.a library.
    # Failure to locate the libc.a library, or loading the wrong libc.a library
    # will result in an unresolved reference error.
    set exec_output [run_host_cmd "$ld" "-plugin $libdep -o libdep.exe libdep-main.o -L libdep-a -la -e 0"]
    set exec_output [prune_warnings $exec_output]

    set expected_output "got deps for library libdep-a/liba.a: -Llibdep-b -lc"
    
    if ![string match $expected_output $exec_output] then {
	fail "$testname: did not get expected output from the linker"
	return
    }

    regsub -all "$expected_output" $exec_output "\\1" exec_output

    if {![string match "" $exec_output]} {
	fail "$testname: unexpected output from linker: $exec_output"
	return
    }

    pass "$testname"
}

run_test