Bug 5652 - genscripts.sh fails with BASH_LINENO
Summary: genscripts.sh fails with BASH_LINENO
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.19
: P2 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-01-21 18:33 UTC by Vincent Rivière
Modified: 2008-01-28 11:07 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
genscripts fix (423 bytes, patch)
2008-01-21 18:39 UTC, Vincent Rivière
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent Rivière 2008-01-21 18:33:09 UTC
When compiling the binutils on some systems, the ld link fails. The reason is 
that the generated emulation C file is empty.
By looking at the make output, we can see
.../genscripts.sh: line 403: ${BASH_LINENO[0]} + 1: bad substitution
However, no error is detected, and the build process of ld continues until the 
final link, which fails.

This happens when $(SHELL) is actually bash compiled with the --enable-minimal-
config option. In that case, the variable BASH is set, but the array 
BASH_LINENO is not supported.

The problem is at the bottom of ld/genscripts.sh
If it is supported by the shell, line number information is inserted into the 
generated emulation source, by using BASH_LINENO. However the detection method 
is wrong. It assumes that if the BASH variable is set, the BASH_LINENO array 
will be available, which is not always true.
Comment 1 Vincent Rivière 2008-01-21 18:39:02 UTC
Created attachment 2208 [details]
genscripts fix

This patch fixes the bug. The availability of the BASH_LINENO variable is now
checked in a reliable way.
Comment 2 Nick Clifton 2008-01-25 16:56:05 UTC
Hi Vincent,

  Thanks for reporting this problem.

  I am not sure if your proposed patch will work for all versions of /bin/sh
however.  Correct me if I am wrong, but aren't shell functions a feature of bash
rather than all shells ?  So wouldn't it be better to just test BASH_LINENO
directly ?  eg:

  if test $BASH_LINENO" != "x"; then
    source_em()

Cheers
  Nick
Comment 3 Andreas Schwab 2008-01-25 20:04:07 UTC
http://www.faqs.org/faqs/unix-faq/shell/shell-differences/

                                     sh   csh  ksh  bash tcsh zsh  rc   es
Shell functions                      Y(1) N    Y    Y    N    Y    Y    Y

    1. This feature was not in the orginal version, but has since become
       almost standard.
Comment 4 Vincent Rivière 2008-01-25 22:55:36 UTC
Furthermore:
- SVR2 (1984):
           - shell functions (sh)
http://www.faqs.org/faqs/unix-faq/faq/part6/

I didn't find any documentation about POSIX. But bash --posix knows about 
functions. And dash (ash) knows it, too (without the function keyword).

I really think this patch is good and harmless.
I put the test in a function because the BASH_LINENO (a bash builtin) exists 
only in functions ! And as the original code uses functions, it doesn't matter 
to add one more.

The original code uses the BASH_LINENO trick in order to produce #line 
statements about the original em file into the generated C file. If BASH_LINENO 
is available, the script use it. If not, it does not produce #line. The 
BASH_LINENO trick is absolutely not mandatory. It just helps to get better 
error report when there is a compilation problem in the em file.

Regards,

Vincent
Comment 5 Vincent Rivière 2008-01-26 19:28:15 UTC
Sorry, there is a typo in the comments:
accruate instead of accurate
Comment 6 Nick Clifton 2008-01-28 11:07:49 UTC
Hi Vincent,

  Ok, I am convinved.  I have checked your patch in along with this changelog entry.

Cheers
  Nick

ld/ChangeLog
2008-01-28  Vincent Riviere  <vincent.riviere@freesbee.fr>

	PR ld/5652
	* genscripts.sh: Check for the existence of BASH_LINENO not just
	the BASH shell before generating line numbers in the emulation
	file.