From: Cary Coutant Date: Sun, 22 Mar 2015 02:03:00 +0000 (-0700) Subject: PR gold/18048: Fix INCLUDE directive support for gold X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=410da591ba7814912e11be826b20dfba9abed9f7;p=binutils-gdb.git PR gold/18048: Fix INCLUDE directive support for gold This patch fixes INCLUDE directives in script files, so that when an INCLUDE appears inside a sections block, section commands block, or memory def block, the contents are parsed in the appropriate context. gold/ PR gold/18048 * script-c.h (script_include_directive): Add first_token parameter. * script.cc (script_include_directive): Add first_token parameter, and pass it to read_script_file. * yyscript.y (PARSING_SECTIONS_BLOCK, PARSING_SECTION_CMDS) (PARSING_MEMORY_DEF): New tokens. (top): Add new productions for INCLUDE files. (file_cmd): Replace file_or_sections_cmd with copy of its productions. Pass PARSING_LINKER_SCRIPT to script_include_directive. (section_block_cmd): Likewise; pass PARSING_SECTIONS_BLOCK. (section_cmd): Pass PARSING_SECTION_CMDS. (file_or_sections_cmd): Remove. (memory_def): Pass PARSING_MEMORY_DEF. * testsuite/Makefile.am (memory_test_2): New test. * testsuite/Makefile.in: Regenerate. * testsuite/memory_test_inc.t: New script file. * testsuite/memory_test_inc_1.t.src: New script file. * testsuite/memory_test_inc_2.t.src: New script file. * testsuite/memory_test_inc_3.t.src: New script file. --- diff --git a/gold/ChangeLog b/gold/ChangeLog index 66cb3065de6..d65c555ad5a 100644 --- a/gold/ChangeLog +++ b/gold/ChangeLog @@ -1,3 +1,25 @@ +2015-03-21 Cary Coutant + + PR gold/18048 + * script-c.h (script_include_directive): Add first_token parameter. + * script.cc (script_include_directive): Add first_token parameter, and + pass it to read_script_file. + * yyscript.y (PARSING_SECTIONS_BLOCK, PARSING_SECTION_CMDS) + (PARSING_MEMORY_DEF): New tokens. + (top): Add new productions for INCLUDE files. + (file_cmd): Replace file_or_sections_cmd with copy of its productions. + Pass PARSING_LINKER_SCRIPT to script_include_directive. + (section_block_cmd): Likewise; pass PARSING_SECTIONS_BLOCK. + (section_cmd): Pass PARSING_SECTION_CMDS. + (file_or_sections_cmd): Remove. + (memory_def): Pass PARSING_MEMORY_DEF. + * testsuite/Makefile.am (memory_test_2): New test. + * testsuite/Makefile.in: Regenerate. + * testsuite/memory_test_inc.t: New script file. + * testsuite/memory_test_inc_1.t.src: New script file. + * testsuite/memory_test_inc_2.t.src: New script file. + * testsuite/memory_test_inc_3.t.src: New script file. + 2015-03-21 Cary Coutant * dwp.cc (Sized_relobj_dwo::do_section_contents): Delete. diff --git a/gold/script-c.h b/gold/script-c.h index 772c76cbb65..dc11d290a35 100644 --- a/gold/script-c.h +++ b/gold/script-c.h @@ -434,7 +434,7 @@ extern void script_set_section_region(void*, const char*, size_t, int); extern void -script_include_directive(void *, const char*, size_t); +script_include_directive(int, void *, const char*, size_t); /* Called by the bison parser for expressions. */ diff --git a/gold/script.cc b/gold/script.cc index 5350afc6ad4..56f126c738a 100644 --- a/gold/script.cc +++ b/gold/script.cc @@ -3366,13 +3366,14 @@ script_parse_memory_attr(void* closurev, const char* attrs, size_t attrlen, } extern "C" void -script_include_directive(void* closurev, const char* filename, size_t length) +script_include_directive(int first_token, void* closurev, + const char* filename, size_t length) { Parser_closure* closure = static_cast(closurev); std::string name(filename, length); Command_line* cmdline = closure->command_line(); read_script_file(name.c_str(), cmdline, &cmdline->script_options(), - PARSING_LINKER_SCRIPT, Lex::LINKER_SCRIPT); + first_token, Lex::LINKER_SCRIPT); } // Functions for memory regions. diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am index 43da146c7b3..2bc494032c6 100644 --- a/gold/testsuite/Makefile.am +++ b/gold/testsuite/Makefile.am @@ -2181,6 +2181,19 @@ memory_test: memory_test.o gcctestdir/ld $(srcdir)/memory_test.t memory_test.stdout: memory_test $(TEST_READELF) -lWS $< > $@ +# Test INCLUDE directives in linker scripts. +# The binary isn't runnable, so we just check that we can build it without errors. +check_DATA += memory_test_2 +MOSTLYCLEANFILES += memory_test_inc_1.t memory_test_inc_2.t memory_test_inc_3.t +memory_test_inc_1.t: $(srcdir)/memory_test_inc_1.t.src + cp $< $@ +memory_test_inc_2.t: $(srcdir)/memory_test_inc_2.t.src + cp $< $@ +memory_test_inc_3.t: $(srcdir)/memory_test_inc_3.t.src + cp $< $@ +memory_test_2: memory_test.o gcctestdir/ld $(srcdir)/memory_test.t memory_test_inc_1.t memory_test_inc_2.t memory_test_inc_3.t + $(LINK) -Bgcctestdir/ -nostartfiles -nostdlib -z max-page-size=0x1000 -z common-page-size=0x1000 -Wl,-T,$(srcdir)/memory_test.t -o $@ memory_test.o + if HAVE_PUBNAMES # Test that --gdb-index functions correctly without gcc-generated pubnames. diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in index 51b58ad9516..c48461f02eb 100644 --- a/gold/testsuite/Makefile.in +++ b/gold/testsuite/Makefile.in @@ -424,6 +424,9 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ no_version_test.sh \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ strong_ref_weak_def.sh \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ dyn_weak_ref.sh memory_test.sh + +# Test INCLUDE directives in linker scripts. +# The binary isn't runnable, so we just check that we can build it without errors. @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_46 = exclude_libs_test.syms \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ discard_locals_test.syms \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ discard_locals_relocatable_test1.syms \ @@ -433,7 +436,7 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ no_version_test.stdout \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ strong_ref_weak_def.stdout \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ dyn_weak_ref.stdout \ -@GCC_TRUE@@NATIVE_LINKER_TRUE@ memory_test.stdout +@GCC_TRUE@@NATIVE_LINKER_TRUE@ memory_test.stdout memory_test_2 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_47 = exclude_libs_test.syms \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ libexclude_libs_test_1.a \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ libexclude_libs_test_2.a \ @@ -460,7 +463,10 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ dyn_weak_ref.stdout \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ libstart_lib_test.a \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ memory_test.stdout memory_test \ -@GCC_TRUE@@NATIVE_LINKER_TRUE@ memory_test.o +@GCC_TRUE@@NATIVE_LINKER_TRUE@ memory_test.o \ +@GCC_TRUE@@NATIVE_LINKER_TRUE@ memory_test_inc_1.t \ +@GCC_TRUE@@NATIVE_LINKER_TRUE@ memory_test_inc_2.t \ +@GCC_TRUE@@NATIVE_LINKER_TRUE@ memory_test_inc_3.t @GCC_TRUE@@MCMODEL_MEDIUM_TRUE@@NATIVE_LINKER_TRUE@am__append_48 = large @GCC_FALSE@large_DEPENDENCIES = @MCMODEL_MEDIUM_FALSE@large_DEPENDENCIES = @@ -5664,6 +5670,14 @@ uninstall-am: @GCC_TRUE@@NATIVE_LINKER_TRUE@ $(LINK) -Bgcctestdir/ -nostartfiles -nostdlib -z max-page-size=0x1000 -z common-page-size=0x1000 -Wl,-T,$(srcdir)/memory_test.t -o $@ memory_test.o @GCC_TRUE@@NATIVE_LINKER_TRUE@memory_test.stdout: memory_test @GCC_TRUE@@NATIVE_LINKER_TRUE@ $(TEST_READELF) -lWS $< > $@ +@GCC_TRUE@@NATIVE_LINKER_TRUE@memory_test_inc_1.t: $(srcdir)/memory_test_inc_1.t.src +@GCC_TRUE@@NATIVE_LINKER_TRUE@ cp $< $@ +@GCC_TRUE@@NATIVE_LINKER_TRUE@memory_test_inc_2.t: $(srcdir)/memory_test_inc_2.t.src +@GCC_TRUE@@NATIVE_LINKER_TRUE@ cp $< $@ +@GCC_TRUE@@NATIVE_LINKER_TRUE@memory_test_inc_3.t: $(srcdir)/memory_test_inc_3.t.src +@GCC_TRUE@@NATIVE_LINKER_TRUE@ cp $< $@ +@GCC_TRUE@@NATIVE_LINKER_TRUE@memory_test_2: memory_test.o gcctestdir/ld $(srcdir)/memory_test.t memory_test_inc_1.t memory_test_inc_2.t memory_test_inc_3.t +@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(LINK) -Bgcctestdir/ -nostartfiles -nostdlib -z max-page-size=0x1000 -z common-page-size=0x1000 -Wl,-T,$(srcdir)/memory_test.t -o $@ memory_test.o @GCC_TRUE@@HAVE_PUBNAMES_TRUE@@NATIVE_LINKER_TRUE@gdb_index_test.o: gdb_index_test.cc @GCC_TRUE@@HAVE_PUBNAMES_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -O0 -g -gno-pubnames -c -o $@ $< @GCC_TRUE@@HAVE_PUBNAMES_TRUE@@NATIVE_LINKER_TRUE@gdb_index_test_1: gdb_index_test.o gcctestdir/ld diff --git a/gold/testsuite/memory_test_inc.t b/gold/testsuite/memory_test_inc.t new file mode 100644 index 00000000000..b835ab7b5cd --- /dev/null +++ b/gold/testsuite/memory_test_inc.t @@ -0,0 +1,28 @@ +MEMORY +{ + region1 : ORIGIN = 0x1000, LENGTH = 0x1000 , + INCLUDE memory_test_inc_1.t + region3 (wx) : o = 0x4000, l = 4 + region4 (!r) : o = 0x6000 + 60, len = 0x30 * 0x6 +} + +SECTIONS +{ + .sec0 : { *(*.sec0) } + + .sec1 ORIGIN (region1) : { *(*.sec1) } AT> region2 + + INCLUDE memory_test_inc_2.t + + .sec2 : { *(*.sec2) } > region3 AT> region4 + + .sec3 0x5000 : { + INCLUDE memory_test_inc_3.t + } + + .sec4 : { *(*.sec4) } AT> region2 + + .sec5 : { LONG(0x5555) } > region2 + + /DISCARD/ : { *(*) } +} diff --git a/gold/testsuite/memory_test_inc_1.t.src b/gold/testsuite/memory_test_inc_1.t.src new file mode 100644 index 00000000000..21c08511140 --- /dev/null +++ b/gold/testsuite/memory_test_inc_1.t.src @@ -0,0 +1 @@ + region2 (r) : org = 0x2000, len = 300 diff --git a/gold/testsuite/memory_test_inc_2.t.src b/gold/testsuite/memory_test_inc_2.t.src new file mode 100644 index 00000000000..b0bc4de6b8f --- /dev/null +++ b/gold/testsuite/memory_test_inc_2.t.src @@ -0,0 +1 @@ + fred = ORIGIN (region1) + LENGTH (region1); diff --git a/gold/testsuite/memory_test_inc_3.t.src b/gold/testsuite/memory_test_inc_3.t.src new file mode 100644 index 00000000000..fe58e3b73a2 --- /dev/null +++ b/gold/testsuite/memory_test_inc_3.t.src @@ -0,0 +1 @@ + *(*.sec3) diff --git a/gold/yyscript.y b/gold/yyscript.y index 87aab58257e..b519f5470c9 100644 --- a/gold/yyscript.y +++ b/gold/yyscript.y @@ -201,6 +201,9 @@ %token PARSING_VERSION_SCRIPT %token PARSING_DEFSYM %token PARSING_DYNAMIC_LIST +%token PARSING_SECTIONS_BLOCK +%token PARSING_SECTION_COMMANDS +%token PARSING_MEMORY_DEF /* Non-terminal types, where needed. */ @@ -232,6 +235,9 @@ top: | PARSING_VERSION_SCRIPT version_script | PARSING_DEFSYM defsym_expr | PARSING_DYNAMIC_LIST dynamic_list_expr + | PARSING_SECTIONS_BLOCK sections_block + | PARSING_SECTION_COMMANDS section_cmds + | PARSING_MEMORY_DEF memory_defs ; /* A file contains a list of commands. */ @@ -281,7 +287,14 @@ file_cmd: { script_push_lex_into_version_mode(closure); } version_script '}' { script_pop_lex_mode(closure); } - | file_or_sections_cmd + | ENTRY '(' string ')' + { script_set_entry(closure, $3.value, $3.length); } + | assignment end + | ASSERT_K '(' parse_exp ',' string ')' + { script_add_assertion(closure, $3, $5.value, $5.length); } + | INCLUDE string + { script_include_directive(PARSING_LINKER_SCRIPT, closure, + $2.value, $2.length); } | ignore_cmd | ';' ; @@ -339,7 +352,14 @@ sections_block: /* A command which may appear within a SECTIONS block. */ section_block_cmd: - file_or_sections_cmd + ENTRY '(' string ')' + { script_set_entry(closure, $3.value, $3.length); } + | assignment end + | ASSERT_K '(' parse_exp ',' string ')' + { script_add_assertion(closure, $3, $5.value, $5.length); } + | INCLUDE string + { script_include_directive(PARSING_SECTIONS_BLOCK, closure, + $2.value, $2.length); } | string section_header { script_start_output_section(closure, $1.value, $1.length, &$2); } '{' section_cmds '}' section_trailer @@ -529,7 +549,8 @@ section_cmd: } | SORT_BY_NAME '(' CONSTRUCTORS ')' | INCLUDE string - { script_include_directive(closure, $2.value, $2.length); } + { script_include_directive(PARSING_SECTION_COMMANDS, closure, + $2.value, $2.length); } | ';' ; @@ -683,18 +704,6 @@ wildcard_name: } ; -/* A command which may appear at the top level of a linker script, or - within a SECTIONS block. */ -file_or_sections_cmd: - ENTRY '(' string ')' - { script_set_entry(closure, $3.value, $3.length); } - | assignment end - | ASSERT_K '(' parse_exp ',' string ')' - { script_add_assertion(closure, $3, $5.value, $5.length); } - | INCLUDE string - { script_include_directive(closure, $2.value, $2.length); } - ; - /* A list of MEMORY definitions. */ memory_defs: memory_defs opt_comma memory_def @@ -706,9 +715,9 @@ memory_def: string memory_attr ':' memory_origin '=' parse_exp opt_comma memory_length '=' parse_exp { script_add_memory(closure, $1.value, $1.length, $2, $6, $10); } | - /* LD supports an INCLUDE directive here, currently GOLD does not. */ INCLUDE string - { script_include_directive(closure, $2.value, $2.length); } + { script_include_directive(PARSING_MEMORY_DEF, closure, + $2.value, $2.length); } | ;