1From b31e9b1bff6832063816b972395179859d1d4619 Mon Sep 17 00:00:00 2001
2From: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
3Date: Sun, 13 Aug 2017 16:03:20 +0200
4Subject: [PATCH] ld-elf2flt: behave properly when called with a name different
5 from TARGET_ALIAS
6
7ld-elf2flt currently handles two cases:
8
9 1 It is called as the wrapper for <TARGET_ALIAS>-ld, generally
10   installed in the bin/ directory of a toolchain.
11
12 2 It is called as the wrapper for "ld", generally installed in the
13   TARGET_ALIAS/bin/ directory of a toolchain.
14
15Unfortunately, if for some reason it gets called using a FOOBAR-ld
16name that is different from <TARGET_ALIAS>-ld, it assumes it is in
17case (2), while it really is in case (1). Due to this, the path
18mangling logic doesn't work, and it doesn't find ld.real.
19
20This happens for example when the binary program in bin/ is named
21arm-buildroot-uclinux-uclibcgnueabi-ld, but also has a simpler symlink
22named arm-linux-ld. In this case,
23arm-buildroot-uclinux-uclibcgnueabi-ld is recognized by ld-elf2flt as
24containing TARGET_ALIAS, and therefore the proper logic to find
25ld.real is applied. However, when arm-linux-ld is used, ld-elf2flt
26doesn't find TARGET_ALIAS, and therefore assumes we're being called as
27TARGET_ALIAS/bin/ld.. and searches for a program called ld.real in
28bin/, which doesn't exist.
29
30See:
31
32$ ./output/host/bin/arm-buildroot-uclinux-uclibcgnueabi-ld
33/home/thomas/buildroot/buildroot/output/host/bin/../arm-buildroot-uclinux-uclibcgnueabi/bin/ld.real: no input files
34
35$ ./output/host/bin/arm-linux-ld
36arm-linux-ld (ld-elf2flt): error trying to exec '/home/thomas/buildroot/buildroot/output/host/bin/ld.real': execvp: No such file or directory
37
38$ ./output/host/arm-buildroot-uclinux-uclibcgnueabi/bin/ld
39/home/thomas/buildroot/buildroot/output/host/arm-buildroot-uclinux-uclibcgnueabi/bin/ld.real: no input files
40
41This commit fixes that by inverting the logic: if we're being called
42as just "ld", then we assume it's the program in
43TARGET_ALIAS/bin/. Otherwise, we're called through some variant of
44TARGET-ld.
45
46Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
47Submitted-upstream: https://github.com/uclinux-dev/elf2flt/pull/8
48---
49 ld-elf2flt.c | 10 +++++-----
50 1 file changed, 5 insertions(+), 5 deletions(-)
51
52diff --git a/ld-elf2flt.c b/ld-elf2flt.c
53index de39fe0..c187c2e 100644
54--- a/ld-elf2flt.c
55+++ b/ld-elf2flt.c
56@@ -506,15 +506,15 @@ int main(int argc, char *argv[])
57 	   the host while those in <TARGET_ALIAS>/lib are for the target.
58 	   Make bindir point to the bin dir for bin/<TARGET_ALIAS>-foo.
59 	   Make tooldir point to the bin dir for <TARGET_ALIAS>/bin/foo.  */
60-	if (streqn(elf2flt_progname, TARGET_ALIAS)) {
61-		tmp = concat(argv0_dir, "../" TARGET_ALIAS "/bin", NULL);
62+	if (streqn(elf2flt_progname, "ld")) {
63+		tmp = concat(argv0_dir, "../../bin", NULL);
64 		if (stat(tmp, &buf) == 0 && S_ISDIR(buf.st_mode)) {
65-			tooldir = concat(tmp, "/", NULL);
66+			bindir = concat(tmp, "/", NULL);
67 		}
68 	} else {
69-		tmp = concat(argv0_dir, "../../bin", NULL);
70+		tmp = concat(argv0_dir, "../" TARGET_ALIAS "/bin", NULL);
71 		if (stat(tmp, &buf) == 0 && S_ISDIR(buf.st_mode)) {
72-			bindir = concat(tmp, "/", NULL);
73+			tooldir = concat(tmp, "/", NULL);
74 		}
75 	}
76
77--
782.9.4
79
80