Bug 24008 - Wrong value of ternary expression in map file
Summary: Wrong value of ternary expression in map file
Status: VERIFIED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.32
: P2 normal
Target Milestone: 2.33
Assignee: Alan Modra
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-12-19 15:14 UTC by Alexander Fedotov
Modified: 2019-02-06 07:04 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed: 2019-01-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Fedotov 2018-12-19 15:14:54 UTC
If we have such expression in linker script:

__TESTVAL1__ = 1;
__TESTVAL2__ = 2;
__TESTVAL__ = DEFINED ( __TESTVAL1__ ) ? (__TESTVAL1__) : (__TESTVAL2__);


in final Map file output it always shows "else" branch:
0x00000001  __TESTVAL1__ = 0x1
0x00000002  __TESTVAL2__ = 0x2
0x00000002  __TESTVAL__ = DEFINED (__TESTVAL1__)?__TESTVAL1__:__TESTVAL2__

While disassembling final ELF shows:

00000001 g       *ABS*	00000000 __TESTVAL__
00000001 g       *ABS*	00000000 __TESTVAL1__
00000002 g       *ABS*	00000000 __TESTVAL2__
Comment 1 Alan Modra 2019-01-26 03:08:12 UTC
The problem is that the defined state isn't tracked during printing of the map,
due to not passing the entire assignment expression to exp_fold_tree.
Comment 2 Sourceware Commits 2019-01-27 23:31:22 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=6a84624340dd19ccd927d91d79e0ece135c6d846

commit 6a84624340dd19ccd927d91d79e0ece135c6d846
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Jan 28 09:59:29 2019 +1030

    PR24008, Wrong value of ternary expression in map file
    
    	PR 24008
    	* ldexp.h (lang_phase_type): Add lang_fixed_phase_enum.
    	* ldexp.c (fold_name): Move expld.assign_name check later to
    	avoid an extra lookup.
    	(exp_fold_tree_1): When lang_fixed_phase_enum, don't change symbol
    	values, and don't clear expld.assign_name.
    	* ldlang.c (lang_map): Set expld.phase to lang_fixed_phase_enum.
    	(print_assignment): Resolve entire assignment expression.
    	Don't access symbol u.def unless symbol is defined.
Comment 3 Alan Modra 2019-01-28 06:57:00 UTC
Fixed
Comment 4 Sourceware Commits 2019-01-29 11:21:55 UTC
The binutils-2_32-branch branch has been updated by Nick Clifton <nickc@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=12542b2a6da22f5ab2fd4043e8bded1444ed42ad

commit 12542b2a6da22f5ab2fd4043e8bded1444ed42ad
Author: Nick Clifton <nickc@redhat.com>
Date:   Tue Jan 29 11:20:02 2019 +0000

    Fix error in expression evalutation in linker output maps.
    
    	PR 24008
    	* ldexp.h (lang_phase_type): Add lang_fixed_phase_enum.
    	* ldexp.c (fold_name): Move expld.assign_name check later to
    	avoid an extra lookup.
    	(exp_fold_tree_1): When lang_fixed_phase_enum, don't change symbol
    	values, and don't clear expld.assign_name.
    	* ldlang.c (lang_map): Set expld.phase to lang_fixed_phase_enum.
    	(print_assignment): Resolve entire assignment expression.
    	Don't access symbol u.def unless symbol is defined.
Comment 5 Sourceware Commits 2019-01-29 14:09:04 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ad0f979c9df2cc3fba1f120c5e7f39e35591ed07

commit ad0f979c9df2cc3fba1f120c5e7f39e35591ed07
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Jan 29 06:07:42 2019 -0800

    Add a testcase for PR ld/24008
    
    	PR ld/24008
    	* testsuite/ld-scripts/defined.exp: Run pr24008.
    	* testsuite/ld-scripts/pr24008.d: New file.
    	* testsuite/ld-scripts/pr24008.map: Likewise.
    	* testsuite/ld-scripts/pr24008.s: Likewise.
    	* testsuite/ld-scripts/pr24008.t: Likewise.
Comment 6 Alexander Fedotov 2019-01-29 15:34:18 UTC
Verified using binutils-2_32-branch
Comment 7 Sourceware Commits 2019-02-06 07:04:48 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=4249a53cce8651061092d666e5df06492cb91cf1

commit 4249a53cce8651061092d666e5df06492cb91cf1
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Feb 6 17:26:23 2019 +1030

    Fix some ldscripts/pr24008 fails
    
    These targets were all failing due to extra symbols.
    pdp11-dec-aout  +FAIL: ld-scripts/pr24008
    powerpc-aix5.1  +FAIL: ld-scripts/pr24008
    powerpc-aix5.2  +FAIL: ld-scripts/pr24008
    rs6000-aix4.3.3  +FAIL: ld-scripts/pr24008
    rs6000-aix5.1  +FAIL: ld-scripts/pr24008
    rs6000-aix5.2  +FAIL: ld-scripts/pr24008
    
    Some fails remain, those I saw were segfaults or assertion fails that
    indicate target bugs.
    
    	PR ld/24008
    	* testsuite/ld-scripts/pr24008.d: Pass with extra target
    	defined symbols.