···11+From 36088682f447540fd8666a2c437fa232064044a7 Mon Sep 17 00:00:00 2001
22+From: Alan Modra <amodra@gmail.com>
33+Date: Thu, 3 Aug 2017 14:01:34 +0930
44+Subject: [PATCH] ELF checks for orphan placement
55+66+The loop checking for previous orphan placement should run even when
77+the output is non-ELF.
88+99+ PR ld/21884
1010+ * emultempl/elf32.em (gld${EMULATION_NAME}_place_orphan): Revert
1111+ last change. Rename iself to elfinput. Expand comments. Condition
1212+ ELF checks on having both input and output ELF files. Extract..
1313+ (elf_orphan_compatible): ..this new function.
1414+---
1515+ ld/ChangeLog | 8 ++++++
1616+ ld/emultempl/elf32.em | 76 +++++++++++++++++++++++++++++++--------------------
1717+ 2 files changed, 55 insertions(+), 29 deletions(-)
1818+1919+diff --git a/ld/emultempl/elf32.em b/ld/emultempl/elf32.em
2020+index 75ded12..9ac1840 100644
2121+--- a/ld/emultempl/elf32.em
2222++++ b/ld/emultempl/elf32.em
2323+@@ -2008,6 +2008,29 @@ output_rel_find (asection *sec, int isdyn)
2424+ return last;
2525+ }
2626+2727++/* Return whether IN is suitable to be part of OUT. */
2828++
2929++static bfd_boolean
3030++elf_orphan_compatible (asection *in, asection *out)
3131++{
3232++ /* Non-zero sh_info implies a section with SHF_INFO_LINK with
3333++ unknown semantics for the generic linker, or a SHT_REL/SHT_RELA
3434++ section where sh_info specifies a symbol table. (We won't see
3535++ SHT_GROUP, SHT_SYMTAB or SHT_DYNSYM sections here.) We clearly
3636++ can't merge SHT_REL/SHT_RELA using differing symbol tables, and
3737++ shouldn't merge sections with differing unknown semantics. */
3838++ if (elf_section_data (out)->this_hdr.sh_info
3939++ != elf_section_data (in)->this_hdr.sh_info)
4040++ return FALSE;
4141++ /* We can't merge two sections with differing SHF_EXCLUDE when doing
4242++ a relocatable link. */
4343++ if (bfd_link_relocatable (&link_info)
4444++ && ((elf_section_flags (out) ^ elf_section_flags (in)) & SHF_EXCLUDE) != 0)
4545++ return FALSE;
4646++ return _bfd_elf_match_sections_by_type (link_info.output_bfd, out,
4747++ in->owner, in);
4848++}
4949++
5050+ /* Place an orphan section. We use this to put random SHF_ALLOC
5151+ sections in the right segment. */
5252+5353+@@ -2064,8 +2087,9 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
5454+ lang_output_section_statement_type *os;
5555+ lang_output_section_statement_type *match_by_name = NULL;
5656+ int isdyn = 0;
5757+- int iself = s->owner->xvec->flavour == bfd_target_elf_flavour;
5858+- unsigned int sh_type = iself ? elf_section_type (s) : SHT_NULL;
5959++ int elfinput = s->owner->xvec->flavour == bfd_target_elf_flavour;
6060++ int elfoutput = link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour;
6161++ unsigned int sh_type = elfinput ? elf_section_type (s) : SHT_NULL;
6262+ flagword flags;
6363+ asection *nexts;
6464+6565+@@ -2073,7 +2097,7 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
6666+ && link_info.combreloc
6767+ && (s->flags & SEC_ALLOC))
6868+ {
6969+- if (iself)
7070++ if (elfinput)
7171+ switch (sh_type)
7272+ {
7373+ case SHT_RELA:
7474+@@ -2095,6 +2119,8 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
7575+ }
7676+7777+ if (!bfd_link_relocatable (&link_info)
7878++ && elfinput
7979++ && elfoutput
8080+ && (s->flags & SEC_ALLOC) != 0
8181+ && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
8282+ {
8383+@@ -2135,9 +2161,11 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
8484+ secname = ".mbind.text";
8585+ }
8686+8787+- /* Look through the script to see where to place this section. */
8888+- if (constraint == 0
8989+- && link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour)
9090++ /* Look through the script to see where to place this section. The
9191++ script includes entries added by previous lang_insert_orphan
9292++ calls, so this loop puts multiple compatible orphans of the same
9393++ name into a single output section. */
9494++ if (constraint == 0)
9595+ for (os = lang_output_section_find (secname);
9696+ os != NULL;
9797+ os = next_matching_output_section_statement (os, 0))
9898+@@ -2146,29 +2174,19 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
9999+ lang_insert_orphan to create a new output section. */
100100+ constraint = SPECIAL;
101101+102102+- /* SEC_EXCLUDE is cleared when doing a relocatable link. But
103103+- we can't merge 2 input sections with the same name when only
104104+- one of them has SHF_EXCLUDE. Don't merge 2 sections with
105105+- different sh_info. */
106106++ /* Check to see if we already have an output section statement
107107++ with this name, and its bfd section has compatible flags.
108108++ If the section already exists but does not have any flags
109109++ set, then it has been created by the linker, possibly as a
110110++ result of a --section-start command line switch. */
111111+ if (os->bfd_section != NULL
112112+- && (elf_section_data (os->bfd_section)->this_hdr.sh_info
113113+- == elf_section_data (s)->this_hdr.sh_info)
114114+ && (os->bfd_section->flags == 0
115115+- || ((!bfd_link_relocatable (&link_info)
116116+- || (iself && (((elf_section_flags (s)
117117+- ^ elf_section_flags (os->bfd_section))
118118+- & SHF_EXCLUDE) == 0)))
119119+- && ((s->flags ^ os->bfd_section->flags)
120120++ || (((s->flags ^ os->bfd_section->flags)
121121+ & (SEC_LOAD | SEC_ALLOC)) == 0
122122+- && _bfd_elf_match_sections_by_type (link_info.output_bfd,
123123+- os->bfd_section,
124124+- s->owner, s))))
125125++ && (!elfinput
126126++ || !elfoutput
127127++ || elf_orphan_compatible (s, os->bfd_section)))))
128128+ {
129129+- /* We already have an output section statement with this
130130+- name, and its bfd section has compatible flags.
131131+- If the section already exists but does not have any flags
132132+- set, then it has been created by the linker, probably as a
133133+- result of a --section-start command line switch. */
134134+ lang_add_section (&os->children, s, NULL, os);
135135+ return os;
136136+ }
137137+@@ -2244,8 +2262,8 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
138138+ else if ((flags & SEC_ALLOC) == 0)
139139+ ;
140140+ else if ((flags & SEC_LOAD) != 0
141141+- && ((iself && sh_type == SHT_NOTE)
142142+- || (!iself && CONST_STRNEQ (secname, ".note"))))
143143++ && ((elfinput && sh_type == SHT_NOTE)
144144++ || (!elfinput && CONST_STRNEQ (secname, ".note"))))
145145+ place = &hold[orphan_interp];
146146+ else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0)
147147+ place = &hold[orphan_bss];
148148+@@ -2255,8 +2273,8 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
149149+ place = &hold[orphan_tdata];
150150+ else if ((flags & SEC_READONLY) == 0)
151151+ place = &hold[orphan_data];
152152+- else if (((iself && (sh_type == SHT_RELA || sh_type == SHT_REL))
153153+- || (!iself && CONST_STRNEQ (secname, ".rel")))
154154++ else if (((elfinput && (sh_type == SHT_RELA || sh_type == SHT_REL))
155155++ || (!elfinput && CONST_STRNEQ (secname, ".rel")))
156156+ && (flags & SEC_LOAD) != 0)
157157+ place = &hold[orphan_rel];
158158+ else if ((flags & SEC_CODE) == 0)
159159+--
160160+2.9.3
161161+