From 5b5f4e6f8cd250e07ec98278f7223e57b3d3bb0c Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Thu, 17 Aug 2017 11:29:04 +0100 Subject: [PATCH] ld: Early detection of orphans we know will be discarded When processing an orphan section we first call lang_place_orphans, this function handles a few sections for which the behaviour is known COMMON sections, or sections marked as SEC_EXCLUDE. Any orphans that are not handled in lang_place_orphans are passed on to ldlang_place_orphan, this is where we decide where to put the orphan, and then call lang_add_section to perform the actual orphan placement. We previously had a larger set of checks at the start of the function lang_add_section to discard some sections that we _knew_ should not be added into the output file, this was where .group sections (in a final link) and .debug* sections (with --strip-debug) were dropped. The problem with dropping these sections at the lang_add_section stage is that a user might also be using --orphan-handling=error to prevent orphans. If they are then they should not be get errors about sections that we know will be discarded, and which are not mentioned in the linker script. The solution proposed in this patch is to move the "will this section be discarded" check into a separate function, and use this in lang_place_orphans to have the early discard phase discard sections that we know should not be included in the output file. ld/ChangeLog: PR 21961 * ldlang.c (lang_discard_section_p): New function. (lang_add_section): Checks moved out into new function, which is now called. (lang_place_orphans): Call lang_discard_section_p instead of duplicating some of the checks from lang_add_section. * testsuite/ld-elf/orphan-11.d: New file. * testsuite/ld-elf/orphan-11.ld: New file. * testsuite/ld-elf/orphan-11.s: New file. * testsuite/ld-elf/orphan-12.d: New file. * testsuite/ld-elf/orphan-12.s: New file. --- ld/ChangeLog | 14 ++++++++++ ld/ldlang.c | 46 +++++++++++++++++++++----------- ld/testsuite/ld-elf/orphan-11.d | 9 +++++++ ld/testsuite/ld-elf/orphan-11.ld | 16 +++++++++++ ld/testsuite/ld-elf/orphan-11.s | 11 ++++++++ ld/testsuite/ld-elf/orphan-12.d | 9 +++++++ ld/testsuite/ld-elf/orphan-12.s | 8 ++++++ 7 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 ld/testsuite/ld-elf/orphan-11.d create mode 100644 ld/testsuite/ld-elf/orphan-11.ld create mode 100644 ld/testsuite/ld-elf/orphan-11.s create mode 100644 ld/testsuite/ld-elf/orphan-12.d create mode 100644 ld/testsuite/ld-elf/orphan-12.s diff --git a/ld/ChangeLog b/ld/ChangeLog index bf303708671..e3faa792f5d 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,17 @@ +2017-08-17 Andrew Burgess + + PR 21961 + * ldlang.c (lang_discard_section_p): New function. + (lang_add_section): Checks moved out into new function, which is + now called. + (lang_place_orphans): Call lang_discard_section_p instead of + duplicating some of the checks from lang_add_section. + * testsuite/ld-elf/orphan-11.d: New file. + * testsuite/ld-elf/orphan-11.ld: New file. + * testsuite/ld-elf/orphan-11.s: New file. + * testsuite/ld-elf/orphan-12.d: New file. + * testsuite/ld-elf/orphan-12.s: New file. + 2017-08-24 Alan Modra * testsuite/ld-undefined/fundef.s: New test. diff --git a/ld/ldlang.c b/ld/ldlang.c index 196b2ccc729..96de2e26590 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -2273,6 +2273,34 @@ section_already_linked (bfd *abfd, asection *sec, void *data) bfd_section_already_linked (abfd, sec, &link_info); } + +/* Returns true if SECTION is one we know will be discarded based on its + section flags, otherwise returns false. */ + +static bfd_boolean +lang_discard_section_p (asection *section) +{ + bfd_boolean discard; + flagword flags = section->flags; + + /* Discard sections marked with SEC_EXCLUDE. */ + discard = (flags & SEC_EXCLUDE) != 0; + + /* Discard the group descriptor sections when we're finally placing the + sections from within the group. */ + if ((flags & SEC_GROUP) != 0 + && link_info.resolve_section_groups) + discard = TRUE; + + /* Discard debugging sections if we are stripping debugging + information. */ + if ((link_info.strip == strip_debugger || link_info.strip == strip_all) + && (flags & SEC_DEBUGGING) != 0) + discard = TRUE; + + return discard; +} + /* The wild routines. These expand statements like *(.text) and foo.o to a list of @@ -2294,26 +2322,14 @@ lang_add_section (lang_statement_list_type *ptr, lang_input_section_type *new_section; bfd *abfd = link_info.output_bfd; - /* Discard sections marked with SEC_EXCLUDE. */ - discard = (flags & SEC_EXCLUDE) != 0; + /* Is this section one we know should be discarded? */ + discard = lang_discard_section_p (section); /* Discard input sections which are assigned to a section named DISCARD_SECTION_NAME. */ if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) discard = TRUE; - /* Discard the group descriptor sections when we're finally placing the - sections from within the group. */ - if ((section->flags & SEC_GROUP) == SEC_GROUP - && link_info.resolve_section_groups) - discard = TRUE; - - /* Discard debugging sections if we are stripping debugging - information. */ - if ((link_info.strip == strip_debugger || link_info.strip == strip_all) - && (flags & SEC_DEBUGGING) != 0) - discard = TRUE; - if (discard) { if (section->output_section == NULL) @@ -6446,7 +6462,7 @@ lang_place_orphans (void) if (file->flags.just_syms) bfd_link_just_syms (file->the_bfd, s, &link_info); - else if ((s->flags & SEC_EXCLUDE) != 0) + else if (lang_discard_section_p (s)) s->output_section = bfd_abs_section_ptr; else if (strcmp (s->name, "COMMON") == 0) { diff --git a/ld/testsuite/ld-elf/orphan-11.d b/ld/testsuite/ld-elf/orphan-11.d new file mode 100644 index 00000000000..3daefba5d42 --- /dev/null +++ b/ld/testsuite/ld-elf/orphan-11.d @@ -0,0 +1,9 @@ +#source: orphan-11.s +#ld: -T orphan-11.ld --orphan-handling=error +#objdump: -wh +#notarget: d30v-* dlx-* fr30-* frv-* ft32-* i860-* i960-* iq2000-* mn10200-* moxie-* ms1-* msp430-* mt-* pj-* + +#... + . \.text .* + . \.data .* +#pass diff --git a/ld/testsuite/ld-elf/orphan-11.ld b/ld/testsuite/ld-elf/orphan-11.ld new file mode 100644 index 00000000000..74c77894570 --- /dev/null +++ b/ld/testsuite/ld-elf/orphan-11.ld @@ -0,0 +1,16 @@ +SECTIONS +{ + . = SIZEOF_HEADERS; + .text : { *(.text .text.*) } + .data : { *(.data .data.*) } + .bss : { *(.bss .bss.*) *(COMMON) } + .sbss : { *(.sbss .sbss.*) } + .note : { *(.note .note.*) } + .rela : { *(.rela .rela.*) } + .rel : { *(.rel .rel.*) } + + /DISCARD/ : { + *(.reginfo) *(.MIPS.abiflags) *(.trampolines) *(.iplt*) + *(.note*) *(.got*) *(.igot*) *(.*.attributes) *(.*.info) + *(.pdr) "linker stubs*"(*) } +} diff --git a/ld/testsuite/ld-elf/orphan-11.s b/ld/testsuite/ld-elf/orphan-11.s new file mode 100644 index 00000000000..3d7961bb2c7 --- /dev/null +++ b/ld/testsuite/ld-elf/orphan-11.s @@ -0,0 +1,11 @@ + .section .text.foo,"axG",%progbits,foo_group + .word 0 + + .section .data.foo,"waG",%progbits,foo_group + .word 1 + + .section .text, "ax" + .word 0 + + .section .data, "wa" + .word 1 diff --git a/ld/testsuite/ld-elf/orphan-12.d b/ld/testsuite/ld-elf/orphan-12.d new file mode 100644 index 00000000000..71a8c93e618 --- /dev/null +++ b/ld/testsuite/ld-elf/orphan-12.d @@ -0,0 +1,9 @@ +#source: orphan-12.s +#ld: -T orphan-11.ld --strip-debug --orphan-handling=error +#objdump: -wh +#notarget: d30v-* dlx-* fr30-* frv-* ft32-* i860-* i960-* iq2000-* mn10200-* moxie-* ms1-* msp430-* mt-* pj-* + +#... + . \.text .* + . \.data .* +#pass diff --git a/ld/testsuite/ld-elf/orphan-12.s b/ld/testsuite/ld-elf/orphan-12.s new file mode 100644 index 00000000000..f9cbcf7e157 --- /dev/null +++ b/ld/testsuite/ld-elf/orphan-12.s @@ -0,0 +1,8 @@ + .section .debug_info, "",%progbits + .word 0 + + .section .text, "ax" + .word 0 + + .section .data, "wa" + .word 1 -- 2.30.2