1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) International Business Machines Corp., 2000
4# Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
5# Author: Robbie Williamson <robbiew@us.ibm.com>
6#
7# Test basic functionality of the `ld` command.
8
9CC=${CC:=gcc}
10LD=${LD:=ld}
11
12TST_CNT=5
13TST_TESTFUNC=test
14TST_SETUP=setup
15TST_NEEDS_TMPDIR=1
16TST_NEEDS_CMDS="$CC $LD"
17. tst_test.sh
18
19setup()
20{
21	for i in rf1 f1 rd1 d1 main; do
22		ROD $CC -fPIC -c -o ${i}.o $TST_DATAROOT/${i}.c
23	done
24}
25
26test1()
27{
28	EXPECT_FAIL $LD x.o y.o 2\> ld.out
29
30	if grep -q "$LD:.*[xy]\.o.*No such file or directory" ld.out; then
31		tst_res TPASS "Missing files were reported"
32	else
33		tst_res TFAIL "Missing files were not reported"
34		cat ld.out
35	fi
36}
37
38test2()
39{
40	EXPECT_FAIL $CC x.o y.o 2\> cc.out
41
42	if grep -q "$CC:.*[xy]\.o.*No such file or directory" cc.out; then
43		tst_res TPASS "Missing files were reported"
44	else
45		tst_res TFAIL "Missing files were not reported"
46		cat cc.out
47	fi
48}
49
50test3()
51{
52	EXPECT_PASS $LD -shared f1.o d1.o -o test.so
53
54	if file test.so |grep -q -e 'pie executable' -e 'shared object'; then
55		tst_res TPASS "Shared library could be build"
56	else
57		tst_res TFAIL "Failed to build shared library"
58	fi
59}
60
61test4()
62{
63	EXPECT_PASS $LD -Bdynamic -shared f1.o d1.o -o test.so
64	EXPECT_FAIL $LD -Bstatic -L. main.o rd1.o test.so -o a.out
65}
66
67test5()
68{
69	EXPECT_PASS $LD -Bdynamic -shared main.o f1.o rf1.o -o test.so -L/usr/lib/
70	EXPECT_FAIL $LD -Bstatic -r main.o f1.o rf1.o test.so -L/usr/lib/ 2\> ld.out
71	cat ld.out
72
73	if grep -q "$LD: attempted static link of dynamic object" ld.out; then
74		tst_res TPASS "Got expected error message"
75	else
76		tst_res TFAIL "Unexpected error message"
77		cat ld.out
78	fi
79}
80
81tst_run
82