ELF: Discard a section if any of its linked-to sections has been discarded

Add ldelf_before_place_orphans to call before lang_place_orphans to
discard a section if any of its linked-to sections has been discarded.

	PR ld/25022
	* emultempl/aix.em (ld_${EMULATION_NAME}_emulation): Add
	before_place_orphans_default.
	* emultempl/armcoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/beos.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/generic.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/linux.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/msp430.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/pe.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/pep.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/ticoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/vanilla.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/elf.em (ld_${EMULATION_NAME}_emulation): Use
	ldelf_before_place_orphans.
	* ldelf.c (ldelf_before_place_orphans): New.
	* ldelf.h (ldelf_before_place_orphans): Likewise.
	* ldemul.c (ldemul_before_place_orphans): Likewise.
	(before_place_orphans_default): Likewise.
	* ldemul.h (ldemul_before_place_orphans): Likewise.
	(before_place_orphans_default): Likewise.
	(ld_emulation_xfer_struct): Add before_place_orphans.
	* ldlang.c (lang_process): Call ldemul_before_place_orphans
	before lang_place_orphans.
	* testsuite/ld-elf/pr25022.d: New file.
	* testsuite/ld-elf/pr25022.s: Likewise.
	* testsuite/ld-elf/pr25022.t: Likewise.
This commit is contained in:
H.J. Lu 2020-02-06 19:42:52 -08:00
parent 5242a0a000
commit 9b538ba71f
20 changed files with 111 additions and 0 deletions

View File

@ -1,3 +1,32 @@
2020-02-06 H.J. Lu <hongjiu.lu@intel.com>
PR ld/25022
* emultempl/aix.em (ld_${EMULATION_NAME}_emulation): Add
before_place_orphans_default.
* emultempl/armcoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
* emultempl/beos.em (ld_${EMULATION_NAME}_emulation): Likewise.
* emultempl/generic.em (ld_${EMULATION_NAME}_emulation): Likewise.
* emultempl/linux.em (ld_${EMULATION_NAME}_emulation): Likewise.
* emultempl/msp430.em (ld_${EMULATION_NAME}_emulation): Likewise.
* emultempl/pe.em (ld_${EMULATION_NAME}_emulation): Likewise.
* emultempl/pep.em (ld_${EMULATION_NAME}_emulation): Likewise.
* emultempl/ticoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
* emultempl/vanilla.em (ld_${EMULATION_NAME}_emulation): Likewise.
* emultempl/elf.em (ld_${EMULATION_NAME}_emulation): Use
ldelf_before_place_orphans.
* ldelf.c (ldelf_before_place_orphans): New.
* ldelf.h (ldelf_before_place_orphans): Likewise.
* ldemul.c (ldemul_before_place_orphans): Likewise.
(before_place_orphans_default): Likewise.
* ldemul.h (ldemul_before_place_orphans): Likewise.
(before_place_orphans_default): Likewise.
(ld_emulation_xfer_struct): Add before_place_orphans.
* ldlang.c (lang_process): Call ldemul_before_place_orphans
before lang_place_orphans.
* testsuite/ld-elf/pr25022.d: New file.
* testsuite/ld-elf/pr25022.s: Likewise.
* testsuite/ld-elf/pr25022.t: Likewise.
2020-02-06 H.J. Lu <hongjiu.lu@intel.com>
PR ld/25490

View File

@ -1541,6 +1541,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = {
after_parse_default,
gld${EMULATION_NAME}_after_open,
after_check_relocs_default,
before_place_orphans_default,
after_allocation_default,
gld${EMULATION_NAME}_set_output_arch,
gld${EMULATION_NAME}_choose_target,

View File

@ -263,6 +263,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
after_parse_default,
gld${EMULATION_NAME}_after_open,
after_check_relocs_default,
before_place_orphans_default,
after_allocation_default,
set_output_arch_default,
ldemul_default_target,

View File

@ -763,6 +763,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
after_parse_default,
gld_${EMULATION_NAME}_after_open,
after_check_relocs_default,
before_place_orphans_default,
after_allocation_default,
set_output_arch_default,
ldemul_default_target,

View File

@ -880,6 +880,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
${LDEMUL_AFTER_PARSE-ldelf_after_parse},
${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
${LDEMUL_AFTER_CHECK_RELOCS-after_check_relocs_default},
${LDEMUL_BEFORE_PLACE_ORPHANS-ldelf_before_place_orphans},
${LDEMUL_AFTER_ALLOCATION-gld${EMULATION_NAME}_after_allocation},
${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
${LDEMUL_CHOOSE_TARGET-ldemul_default_target},

View File

@ -138,6 +138,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
${LDEMUL_AFTER_PARSE-after_parse_default},
${LDEMUL_AFTER_OPEN-after_open_default},
${LDEMUL_AFTER_CHECK_RELOCS-after_check_relocs_default},
${LDEMUL_BEFORE_PLACE_ORPHANS-before_place_orphans_default},
${LDEMUL_AFTER_ALLOCATION-after_allocation_default},
${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
${LDEMUL_CHOOSE_TARGET-ldemul_default_target},

View File

@ -190,6 +190,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
after_parse_default,
after_open_default,
after_check_relocs_default,
before_place_orphans_default,
after_allocation_default,
set_output_arch_default,
ldemul_default_target,

View File

@ -825,6 +825,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
${LDEMUL_AFTER_PARSE-after_parse_default},
msp430_elf_after_open,
after_check_relocs_default,
before_place_orphans_default,
msp430_elf_after_allocation,
${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
${LDEMUL_CHOOSE_TARGET-ldemul_default_target},

View File

@ -2354,6 +2354,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
gld_${EMULATION_NAME}_after_parse,
gld_${EMULATION_NAME}_after_open,
after_check_relocs_default,
before_place_orphans_default,
after_allocation_default,
set_output_arch_default,
ldemul_default_target,

View File

@ -2153,6 +2153,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
gld_${EMULATION_NAME}_after_parse,
gld_${EMULATION_NAME}_after_open,
after_check_relocs_default,
before_place_orphans_default,
after_allocation_default,
set_output_arch_default,
ldemul_default_target,

View File

@ -163,6 +163,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
after_parse_default,
after_open_default,
after_check_relocs_default,
before_place_orphans_default,
after_allocation_default,
set_output_arch_default,
ldemul_default_target,

View File

@ -64,6 +64,7 @@ struct ld_emulation_xfer_struct ld_vanilla_emulation =
after_parse_default,
after_open_default,
after_check_relocs_default,
before_place_orphans_default,
after_allocation_default,
vanilla_set_output_arch,
ldemul_default_target,

View File

@ -2134,3 +2134,32 @@ ldelf_place_orphan (asection *s, const char *secname, int constraint)
return lang_insert_orphan (s, secname, constraint, after, place, NULL, NULL);
}
void
ldelf_before_place_orphans (void)
{
bfd *abfd;
for (abfd = link_info.input_bfds;
abfd != (bfd *) NULL; abfd = abfd->link.next)
if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
&& bfd_count_sections (abfd) != 0
&& !bfd_input_just_syms (abfd))
{
asection *isec;
for (isec = abfd->sections; isec != NULL; isec = isec->next)
{
/* Discard a section if any of its linked-to section has
been discarded. */
asection *linked_to_sec;
for (linked_to_sec = elf_linked_to_section (isec);
linked_to_sec != NULL;
linked_to_sec = elf_linked_to_section (linked_to_sec))
if (discarded_section (linked_to_sec))
{
isec->output_section = bfd_abs_section_ptr;
break;
}
}
}
}

View File

@ -30,3 +30,4 @@ extern bfd_boolean ldelf_open_dynamic_archive
(const char *, search_dirs_type *, lang_input_statement_type *);
extern lang_output_section_statement_type *ldelf_place_orphan
(asection *, const char *, int);
extern void ldelf_before_place_orphans (void);

View File

@ -71,6 +71,12 @@ ldemul_after_check_relocs (void)
ld_emulation->after_check_relocs ();
}
void
ldemul_before_place_orphans (void)
{
ld_emulation->before_place_orphans ();
}
void
ldemul_after_allocation (void)
{
@ -266,6 +272,11 @@ after_check_relocs_default (void)
{
}
void
before_place_orphans_default (void)
{
}
void
after_allocation_default (void)
{

View File

@ -36,6 +36,8 @@ extern void ldemul_after_open
(void);
extern void ldemul_after_check_relocs
(void);
extern void ldemul_before_place_orphans
(void);
extern void ldemul_after_allocation
(void);
extern void ldemul_before_allocation
@ -80,6 +82,8 @@ extern void after_open_default
(void);
extern void after_check_relocs_default
(void);
extern void before_place_orphans_default
(void);
extern void after_allocation_default
(void);
extern void before_allocation_default
@ -129,6 +133,9 @@ typedef struct ld_emulation_xfer_struct {
/* Run after checking relocations. */
void (*after_check_relocs) (void);
/* Run before placing orphans. */
void (*before_place_orphans) (void);
/* Run after allocating output sections. */
void (*after_allocation) (void);

View File

@ -7837,6 +7837,8 @@ lang_process (void)
output statement, so that it isn't reordered. */
process_insert_statements (&lang_os_list.head->header.next);
ldemul_before_place_orphans ();
/* Find any sections not attached explicitly and handle them. */
lang_place_orphans ();

View File

@ -0,0 +1,9 @@
#ld: -T pr25022.t
#readelf: -SW
#xfail: msp*-*
# msp* doesn't use ldelf_before_place_orphans.
#failif
#...
+\[ *[0-9]+\] \.(bar|moo|zed) +.*
#...

View File

@ -0,0 +1,11 @@
.section .foo,"a"
.dc.a 0
.section .moo,"ao",%progbits,.zed
.dc.a 0
.section .bar,"ao",%progbits,.foo
.dc.a 0
.section .zed,"ao",%progbits,.foo
.dc.a 0

View File

@ -0,0 +1 @@
SECTIONS { /DISCARD/ : { *(.foo) } }