···1+From 36088682f447540fd8666a2c437fa232064044a7 Mon Sep 17 00:00:00 2001
2+From: Alan Modra <amodra@gmail.com>
3+Date: Thu, 3 Aug 2017 14:01:34 +0930
4+Subject: [PATCH] ELF checks for orphan placement
5+6+The loop checking for previous orphan placement should run even when
7+the output is non-ELF.
8+9+ PR ld/21884
10+ * emultempl/elf32.em (gld${EMULATION_NAME}_place_orphan): Revert
11+ last change. Rename iself to elfinput. Expand comments. Condition
12+ ELF checks on having both input and output ELF files. Extract..
13+ (elf_orphan_compatible): ..this new function.
14+---
15+ ld/ChangeLog | 8 ++++++
16+ ld/emultempl/elf32.em | 76 +++++++++++++++++++++++++++++++--------------------
17+ 2 files changed, 55 insertions(+), 29 deletions(-)
18+19+diff --git a/ld/emultempl/elf32.em b/ld/emultempl/elf32.em
20+index 75ded12..9ac1840 100644
21+--- a/ld/emultempl/elf32.em
22++++ b/ld/emultempl/elf32.em
23+@@ -2008,6 +2008,29 @@ output_rel_find (asection *sec, int isdyn)
24+ return last;
25+ }
26+27++/* Return whether IN is suitable to be part of OUT. */
28++
29++static bfd_boolean
30++elf_orphan_compatible (asection *in, asection *out)
31++{
32++ /* Non-zero sh_info implies a section with SHF_INFO_LINK with
33++ unknown semantics for the generic linker, or a SHT_REL/SHT_RELA
34++ section where sh_info specifies a symbol table. (We won't see
35++ SHT_GROUP, SHT_SYMTAB or SHT_DYNSYM sections here.) We clearly
36++ can't merge SHT_REL/SHT_RELA using differing symbol tables, and
37++ shouldn't merge sections with differing unknown semantics. */
38++ if (elf_section_data (out)->this_hdr.sh_info
39++ != elf_section_data (in)->this_hdr.sh_info)
40++ return FALSE;
41++ /* We can't merge two sections with differing SHF_EXCLUDE when doing
42++ a relocatable link. */
43++ if (bfd_link_relocatable (&link_info)
44++ && ((elf_section_flags (out) ^ elf_section_flags (in)) & SHF_EXCLUDE) != 0)
45++ return FALSE;
46++ return _bfd_elf_match_sections_by_type (link_info.output_bfd, out,
47++ in->owner, in);
48++}
49++
50+ /* Place an orphan section. We use this to put random SHF_ALLOC
51+ sections in the right segment. */
52+53+@@ -2064,8 +2087,9 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
54+ lang_output_section_statement_type *os;
55+ lang_output_section_statement_type *match_by_name = NULL;
56+ int isdyn = 0;
57+- int iself = s->owner->xvec->flavour == bfd_target_elf_flavour;
58+- unsigned int sh_type = iself ? elf_section_type (s) : SHT_NULL;
59++ int elfinput = s->owner->xvec->flavour == bfd_target_elf_flavour;
60++ int elfoutput = link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour;
61++ unsigned int sh_type = elfinput ? elf_section_type (s) : SHT_NULL;
62+ flagword flags;
63+ asection *nexts;
64+65+@@ -2073,7 +2097,7 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
66+ && link_info.combreloc
67+ && (s->flags & SEC_ALLOC))
68+ {
69+- if (iself)
70++ if (elfinput)
71+ switch (sh_type)
72+ {
73+ case SHT_RELA:
74+@@ -2095,6 +2119,8 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
75+ }
76+77+ if (!bfd_link_relocatable (&link_info)
78++ && elfinput
79++ && elfoutput
80+ && (s->flags & SEC_ALLOC) != 0
81+ && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
82+ {
83+@@ -2135,9 +2161,11 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
84+ secname = ".mbind.text";
85+ }
86+87+- /* Look through the script to see where to place this section. */
88+- if (constraint == 0
89+- && link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour)
90++ /* Look through the script to see where to place this section. The
91++ script includes entries added by previous lang_insert_orphan
92++ calls, so this loop puts multiple compatible orphans of the same
93++ name into a single output section. */
94++ if (constraint == 0)
95+ for (os = lang_output_section_find (secname);
96+ os != NULL;
97+ os = next_matching_output_section_statement (os, 0))
98+@@ -2146,29 +2174,19 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
99+ lang_insert_orphan to create a new output section. */
100+ constraint = SPECIAL;
101+102+- /* SEC_EXCLUDE is cleared when doing a relocatable link. But
103+- we can't merge 2 input sections with the same name when only
104+- one of them has SHF_EXCLUDE. Don't merge 2 sections with
105+- different sh_info. */
106++ /* Check to see if we already have an output section statement
107++ with this name, and its bfd section has compatible flags.
108++ If the section already exists but does not have any flags
109++ set, then it has been created by the linker, possibly as a
110++ result of a --section-start command line switch. */
111+ if (os->bfd_section != NULL
112+- && (elf_section_data (os->bfd_section)->this_hdr.sh_info
113+- == elf_section_data (s)->this_hdr.sh_info)
114+ && (os->bfd_section->flags == 0
115+- || ((!bfd_link_relocatable (&link_info)
116+- || (iself && (((elf_section_flags (s)
117+- ^ elf_section_flags (os->bfd_section))
118+- & SHF_EXCLUDE) == 0)))
119+- && ((s->flags ^ os->bfd_section->flags)
120++ || (((s->flags ^ os->bfd_section->flags)
121+ & (SEC_LOAD | SEC_ALLOC)) == 0
122+- && _bfd_elf_match_sections_by_type (link_info.output_bfd,
123+- os->bfd_section,
124+- s->owner, s))))
125++ && (!elfinput
126++ || !elfoutput
127++ || elf_orphan_compatible (s, os->bfd_section)))))
128+ {
129+- /* We already have an output section statement with this
130+- name, and its bfd section has compatible flags.
131+- If the section already exists but does not have any flags
132+- set, then it has been created by the linker, probably as a
133+- result of a --section-start command line switch. */
134+ lang_add_section (&os->children, s, NULL, os);
135+ return os;
136+ }
137+@@ -2244,8 +2262,8 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
138+ else if ((flags & SEC_ALLOC) == 0)
139+ ;
140+ else if ((flags & SEC_LOAD) != 0
141+- && ((iself && sh_type == SHT_NOTE)
142+- || (!iself && CONST_STRNEQ (secname, ".note"))))
143++ && ((elfinput && sh_type == SHT_NOTE)
144++ || (!elfinput && CONST_STRNEQ (secname, ".note"))))
145+ place = &hold[orphan_interp];
146+ else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0)
147+ place = &hold[orphan_bss];
148+@@ -2255,8 +2273,8 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
149+ place = &hold[orphan_tdata];
150+ else if ((flags & SEC_READONLY) == 0)
151+ place = &hold[orphan_data];
152+- else if (((iself && (sh_type == SHT_RELA || sh_type == SHT_REL))
153+- || (!iself && CONST_STRNEQ (secname, ".rel")))
154++ else if (((elfinput && (sh_type == SHT_RELA || sh_type == SHT_REL))
155++ || (!elfinput && CONST_STRNEQ (secname, ".rel")))
156+ && (flags & SEC_LOAD) != 0)
157+ place = &hold[orphan_rel];
158+ else if ((flags & SEC_CODE) == 0)
159+--
160+2.9.3
161+