This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [linker] Allow asserts in sections


Nathan Sidwell wrote:
This patch does 2 things

1) allow assert statements inside section scripts. This means one doesn't have to write the awful
foo : {
. = . + (ASSERT (foo, bar) & 0);
}
any more.


I also changed the lexer to not allow ',' in NAMES when in expression mode. I kept getting tripped up by
ASSERT (FOO, "FOO zero");
which parses the first token as "FOO,". Also I recently got bitten by EXTERN which allows both comma and whitespace to separate the symbol names. Thus
EXTERN (FOO, BAR, BAZ)
parses ok, but does not pull in the symbols you think it might.

there was a mistake in the patch, which I didn't notice due to lack of a testcase. I've fixed the grammar problem and added an EXTERN testcase to check it. ok?


nathan

--
Nathan Sidwell    ::   http://www.codesourcery.com   ::         CodeSourcery
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk

2007-05-24  Nathan Sidwell  <nathan@codesourcery.com>

	* ldlex.l: ASSERT is recognized in SCRIPT env.  NAMES cannot
	contain commas in EXP env.
	* ldgram.y (extern_name_list): Push to EXP env, move body to ...
	(extern_name_list_body): ... here.
	(script_file, ifile_list): Reformat.
	(statement): Add ASSERT.

	testsuite/
	* ld/ld-scripts/assert.t: Add additional cases.

Index: ldgram.y
===================================================================
RCS file: /cvs/src/src/ld/ldgram.y,v
retrieving revision 1.53
diff -c -3 -p -r1.53 ldgram.y
*** ldgram.y	26 Apr 2007 14:46:59 -0000	1.53
--- ldgram.y	24 May 2007 06:42:16 -0000
*************** casesymlist:
*** 280,312 ****
  	| casesymlist ',' NAME
  	;
  
  extern_name_list:
  	  NAME
  			{ ldlang_add_undef ($1); }
! 	| extern_name_list NAME
  			{ ldlang_add_undef ($2); }
! 	| extern_name_list ',' NAME
  			{ ldlang_add_undef ($3); }
  	;
  
  script_file:
! 	{
! 	 ldlex_both();
! 	}
!        ifile_list
! 	{
! 	ldlex_popstate();
! 	}
          ;
  
- 
  ifile_list:
!        ifile_list ifile_p1
          |
  	;
  
  
- 
  ifile_p1:
  		memory
  	|	sections
--- 280,312 ----
  	| casesymlist ',' NAME
  	;
  
+ /* Parsed as expressions so that commas separate entries */
  extern_name_list:
+ 	{ ldlex_expression (); }
+ 	extern_name_list_body
+ 	{ ldlex_popstate (); }
+ 
+ extern_name_list_body:
  	  NAME
  			{ ldlang_add_undef ($1); }
! 	| extern_name_list_body NAME
  			{ ldlang_add_undef ($2); }
! 	| extern_name_list_body ',' NAME
  			{ ldlang_add_undef ($3); }
  	;
  
  script_file:
! 	{ ldlex_both(); }
! 	ifile_list
! 	{ ldlex_popstate(); }
          ;
  
  ifile_list:
! 	ifile_list ifile_p1
          |
  	;
  
  
  ifile_p1:
  		memory
  	|	sections
*************** statement:
*** 573,578 ****
--- 573,581 ----
  			{
  			  lang_add_fill ($3);
  			}
+ 	| ASSERT_K  {ldlex_expression ();} '(' exp ',' NAME ')' end
+ 			{ ldlex_popstate ();
+ 			  lang_add_assignment (exp_assert ($4, $6)); }
  	;
  
  statement_list:
Index: ldlex.l
===================================================================
RCS file: /cvs/src/src/ld/ldlex.l,v
retrieving revision 1.36
diff -c -3 -p -r1.36 ldlex.l
*** ldlex.l	26 Apr 2007 14:46:59 -0000	1.36
--- ldlex.l	24 May 2007 06:42:16 -0000
*************** V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([
*** 256,262 ****
  <EXPRESSION,BOTH,SCRIPT>"LOADADDR"	{ RTOKEN(LOADADDR);}
  <EXPRESSION,BOTH>"MAX"			{ RTOKEN(MAX_K); }
  <EXPRESSION,BOTH>"MIN"			{ RTOKEN(MIN_K); }
! <EXPRESSION,BOTH>"ASSERT"		{ RTOKEN(ASSERT_K); }
  <BOTH,SCRIPT>"ENTRY"			{ RTOKEN(ENTRY);}
  <BOTH,SCRIPT,MRI>"EXTERN"		{ RTOKEN(EXTERN);}
  <EXPRESSION,BOTH,SCRIPT>"NEXT"		{ RTOKEN(NEXT);}
--- 256,262 ----
  <EXPRESSION,BOTH,SCRIPT>"LOADADDR"	{ RTOKEN(LOADADDR);}
  <EXPRESSION,BOTH>"MAX"			{ RTOKEN(MAX_K); }
  <EXPRESSION,BOTH>"MIN"			{ RTOKEN(MIN_K); }
! <EXPRESSION,BOTH,SCRIPT>"ASSERT"	{ RTOKEN(ASSERT_K); }
  <BOTH,SCRIPT>"ENTRY"			{ RTOKEN(ENTRY);}
  <BOTH,SCRIPT,MRI>"EXTERN"		{ RTOKEN(EXTERN);}
  <EXPRESSION,BOTH,SCRIPT>"NEXT"		{ RTOKEN(NEXT);}
*************** V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([
*** 363,373 ****
  				}
  
  
! <BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}*	{
  				 yylval.name = xstrdup (yytext);
  				  return NAME;
  				}
! <BOTH,EXPRESSION>"-l"{FILENAMECHAR}+ {
  				  yylval.name = xstrdup (yytext + 2);
  				  return LNAME;
  				}
--- 363,381 ----
  				}
  
  
! <BOTH>{FILENAMECHAR1}{FILENAMECHAR}*	{
  				 yylval.name = xstrdup (yytext);
  				  return NAME;
  				}
! <BOTH>"-l"{FILENAMECHAR}+ {
! 				  yylval.name = xstrdup (yytext + 2);
! 				  return LNAME;
! 				}
! <EXPRESSION>{FILENAMECHAR1}{NOCFILENAMECHAR}*	{
! 				 yylval.name = xstrdup (yytext);
! 				  return NAME;
! 				}
! <EXPRESSION>"-l"{NOCFILENAMECHAR}+ {
  				  yylval.name = xstrdup (yytext + 2);
  				  return LNAME;
  				}
Index: testsuite/ld-scripts/assert.t
===================================================================
RCS file: /cvs/src/src/ld/testsuite/ld-scripts/assert.t,v
retrieving revision 1.1
diff -c -3 -p -r1.1 assert.t
*** testsuite/ld-scripts/assert.t	18 Feb 2004 16:37:21 -0000	1.1
--- testsuite/ld-scripts/assert.t	24 May 2007 06:42:18 -0000
***************
*** 1,5 ****
  SECTIONS
  {
!   .empty : {}
    ASSERT (!SIZEOF(.empty), "Empty is not empty")
  }
--- 1,9 ----
  SECTIONS
  {
!   .empty : {
!   here = !.;
!   ASSERT (!., "dot is not zero");
!   ASSERT (here, "here is zero");
!   }
    ASSERT (!SIZEOF(.empty), "Empty is not empty")
  }
Index: testsuite/ld-scripts/extern.exp
===================================================================
RCS file: testsuite/ld-scripts/extern.exp
diff -N testsuite/ld-scripts/extern.exp
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/ld-scripts/extern.exp	24 May 2007 06:42:19 -0000
***************
*** 0 ****
--- 1,68 ----
+ # Test EXTERN in a linker script.
+ # By Nathan Sidwell, CodeSourcery LLC
+ #   Copyright 2007
+ #   Free Software Foundation, Inc.
+ #
+ # This file 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 2 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.
+ 
+ set testname "EXTERN"
+ 
+ if ![ld_assemble $as $srcdir/$subdir/extern.s tmpdir/extern.o] {
+     unresolved $testname
+     return
+ }
+ 
+ if ![ld_simple_link $ld tmpdir/extern "-T $srcdir/$subdir/extern.t tmpdir/extern.o"] {
+     fail $testname
+ }
+ 
+ if ![ld_nm $nm "" tmpdir/extern] {
+     unresolved $testname
+     return
+ }
+ 
+ if {![info exists nm_output(sym1)] || $nm_output(sym1) != 1} {
+     send_log "sym1 wrong\n"
+     verbose "sym1 wrong"
+     fail $testname
+     return
+ }
+     
+ if {![info exists nm_output(sym2)] || $nm_output(sym2) != 2} {
+     send_log "sym1 wrong\n"
+     verbose "sym1 wrong"
+     fail $testname
+     return
+ }
+ if {![info exists nm_output(sym3)] || $nm_output(sym3) != 3} {
+     send_log "sym1 wrong\n"
+     verbose "sym1 wrong"
+     fail $testname
+     return
+ }
+ if {![info exists nm_output(sym4)] || $nm_output(sym4) != 4} {
+     send_log "sym1 wrong\n"
+     verbose "sym1 wrong"
+     fail $testname
+     return
+ }
+ if {![info exists nm_output(sym5)] || $nm_output(sym5) != 5} {
+     send_log "sym1 wrong\n"
+     verbose "sym1 wrong"
+     fail $testname
+     return
+ }
+ 
+ pass $testname
Index: testsuite/ld-scripts/extern.s
===================================================================
RCS file: testsuite/ld-scripts/extern.s
diff -N testsuite/ld-scripts/extern.s
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/ld-scripts/extern.s	24 May 2007 06:42:19 -0000
***************
*** 0 ****
--- 1 ----
+ 	.text
Index: testsuite/ld-scripts/extern.t
===================================================================
RCS file: testsuite/ld-scripts/extern.t
diff -N testsuite/ld-scripts/extern.t
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/ld-scripts/extern.t	24 May 2007 06:42:19 -0000
***************
*** 0 ****
--- 1,14 ----
+ 
+ EXTERN(sym1)
+ EXTERN(sym2, sym3)
+ EXTERN(sym4 sym5)
+ 
+ PROVIDE(sym1 = 1);
+ PROVIDE(sym2 = 2);
+ PROVIDE(sym3 = 3);
+ PROVIDE(sym4 = 4);
+ PROVIDE(sym5 = 5);
+ 
+ SECTIONS
+ {
+ }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]