7cfbaa6153a41d9e8a49548da4520078073c1f86
[openwrt/staging/stintel.git] / toolchain / binutils / patches / 2.28 / 002-PR-ld-21334-Always-call-_bfd_elf_link_renumber_dynsy.patch
1 From 51ffcfc2c67b4c4aa1620fb4c9834c38d150e1c5 Mon Sep 17 00:00:00 2001
2 From: "Maciej W. Rozycki" <macro@imgtec.com>
3 Date: Wed, 26 Apr 2017 22:23:44 +0100
4 Subject: PR ld/21334: Always call `_bfd_elf_link_renumber_dynsyms' if required
5
6 Complement commit e17b0c351f0b ("MIPS/BFD: Respect the ELF gABI dynamic
7 symbol table sort requirement") and correct an inconsistency in dynamic
8 symbol accounting data causing an assertion failure in the MIPS backend:
9
10 ld: BFD (GNU Binutils) 2.28.51.20170330 assertion fail
11 ../../binutils-gdb/bfd/elfxx-mips.c:3860
12
13 in the course of making a GOT entry in a static binary to satisfy a GOT
14 relocation present in input, due to the local dynamic symbol count not
15 having been established.
16
17 To do so let backends request `_bfd_elf_link_renumber_dynsyms' to be
18 always called, rather than where a dynamic binary is linked only, and
19 then make this request in the MIPS backend.
20
21 bfd/
22 PR ld/21334
23 * elf-bfd.h (elf_backend_data): Add `always_renumber_dynsyms'
24 member.
25 * elfxx-target.h [!elf_backend_always_renumber_dynsyms]
26 (elf_backend_always_renumber_dynsyms): Define.
27 (elfNN_bed): Initialize `always_renumber_dynsyms' member.
28 * elfxx-mips.h (elf_backend_always_renumber_dynsyms): Define.
29 * elflink.c (bfd_elf_size_dynsym_hash_dynstr): Also call
30 `_bfd_elf_link_renumber_dynsyms' if the backend has requested
31 it.
32 (elf_gc_sweep): Likewise.
33
34 (backported from commit 23ec1e32b1ab714649a7c25e49b5d721fe3bd3db)
35 ---
36 bfd/ChangeLog | 14 ++++++++++++++
37 bfd/elf-bfd.h | 4 ++++
38 bfd/elflink.c | 34 +++++++++++++++++++++++-----------
39 bfd/elfxx-mips.h | 1 +
40 bfd/elfxx-target.h | 6 +++++-
41 5 files changed, 47 insertions(+), 12 deletions(-)
42
43 diff --git a/bfd/ChangeLog b/bfd/ChangeLog
44 index 537ebb5e2c..3f10bc55f2 100644
45 --- a/bfd/ChangeLog
46 +++ b/bfd/ChangeLog
47 @@ -1,5 +1,19 @@
48 2017-04-26 Maciej W. Rozycki <macro@imgtec.com>
49
50 + PR ld/21334
51 + * elf-bfd.h (elf_backend_data): Add `always_renumber_dynsyms'
52 + member.
53 + * elfxx-target.h [!elf_backend_always_renumber_dynsyms]
54 + (elf_backend_always_renumber_dynsyms): Define.
55 + (elfNN_bed): Initialize `always_renumber_dynsyms' member.
56 + * elfxx-mips.h (elf_backend_always_renumber_dynsyms): Define.
57 + * elflink.c (bfd_elf_size_dynsym_hash_dynstr): Also call
58 + `_bfd_elf_link_renumber_dynsyms' if the backend has requested
59 + it.
60 + (elf_gc_sweep): Likewise.
61 +
62 +2017-04-26 Maciej W. Rozycki <macro@imgtec.com>
63 +
64 * elflink.c (elf_gc_sweep): Only call
65 `_bfd_elf_link_renumber_dynsyms' if dynamic sections have been
66 created.
67 diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
68 index 5de9ab6ca6..dc4bd87f89 100644
69 --- a/bfd/elf-bfd.h
70 +++ b/bfd/elf-bfd.h
71 @@ -1478,6 +1478,10 @@ struct elf_backend_data
72 /* Address of protected data defined in the shared library may be
73 external, i.e., due to copy relocation. */
74 unsigned extern_protected_data : 1;
75 +
76 + /* True if `_bfd_elf_link_renumber_dynsyms' must be called even for
77 + static binaries. */
78 + unsigned always_renumber_dynsyms : 1;
79 };
80
81 /* Information about reloc sections associated with a bfd_elf_section_data
82 diff --git a/bfd/elflink.c b/bfd/elflink.c
83 index 75d6543dcc..507f2b6259 100644
84 --- a/bfd/elflink.c
85 +++ b/bfd/elflink.c
86 @@ -6710,6 +6710,8 @@ bfd_boolean
87 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
88 {
89 const struct elf_backend_data *bed;
90 + unsigned long section_sym_count;
91 + bfd_size_type dynsymcount;
92
93 if (!is_elf_hash_table (info->hash))
94 return TRUE;
95 @@ -6717,24 +6719,30 @@ bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
96 bed = get_elf_backend_data (output_bfd);
97 (*bed->elf_backend_init_index_section) (output_bfd, info);
98
99 + /* Assign dynsym indices. In a shared library we generate a section
100 + symbol for each output section, which come first. Next come all
101 + of the back-end allocated local dynamic syms, followed by the rest
102 + of the global symbols.
103 +
104 + This is usually not needed for static binaries, however backends
105 + can request to always do it, e.g. the MIPS backend uses dynamic
106 + symbol counts to lay out GOT, which will be produced in the
107 + presence of GOT relocations even in static binaries (holding fixed
108 + data in that case, to satisfy those relocations). */
109 +
110 + if (elf_hash_table (info)->dynamic_sections_created
111 + || bed->always_renumber_dynsyms)
112 + dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
113 + &section_sym_count);
114 +
115 if (elf_hash_table (info)->dynamic_sections_created)
116 {
117 bfd *dynobj;
118 asection *s;
119 - bfd_size_type dynsymcount;
120 - unsigned long section_sym_count;
121 unsigned int dtagcount;
122
123 dynobj = elf_hash_table (info)->dynobj;
124
125 - /* Assign dynsym indicies. In a shared library we generate a
126 - section symbol for each output section, which come first.
127 - Next come all of the back-end allocated local dynamic syms,
128 - followed by the rest of the global symbols. */
129 -
130 - dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
131 - &section_sym_count);
132 -
133 /* Work out the size of the symbol version section. */
134 s = bfd_get_linker_section (dynobj, ".gnu.version");
135 BFD_ASSERT (s != NULL);
136 @@ -12986,7 +12994,11 @@ elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
137 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
138 &sweep_info);
139
140 - if (elf_hash_table (info)->dynamic_sections_created)
141 + /* We need to reassign dynsym indices now that symbols may have
142 + been removed. See the call in `bfd_elf_size_dynsym_hash_dynstr'
143 + for the details of the conditions used here. */
144 + if (elf_hash_table (info)->dynamic_sections_created
145 + || bed->always_renumber_dynsyms)
146 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
147 return TRUE;
148 }
149 diff --git a/bfd/elfxx-mips.h b/bfd/elfxx-mips.h
150 index fa5b5d2de9..274129b2e5 100644
151 --- a/bfd/elfxx-mips.h
152 +++ b/bfd/elfxx-mips.h
153 @@ -196,3 +196,4 @@ literal_reloc_p (int r_type)
154 #define elf_backend_post_process_headers _bfd_mips_post_process_headers
155 #define elf_backend_compact_eh_encoding _bfd_mips_elf_compact_eh_encoding
156 #define elf_backend_cant_unwind_opcode _bfd_mips_elf_cant_unwind_opcode
157 +#define elf_backend_always_renumber_dynsyms TRUE
158 diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h
159 index d063fb7f1b..d07600c15d 100644
160 --- a/bfd/elfxx-target.h
161 +++ b/bfd/elfxx-target.h
162 @@ -126,6 +126,9 @@
163 #ifndef elf_backend_extern_protected_data
164 #define elf_backend_extern_protected_data 0
165 #endif
166 +#ifndef elf_backend_always_renumber_dynsyms
167 +#define elf_backend_always_renumber_dynsyms FALSE
168 +#endif
169 #ifndef elf_backend_stack_align
170 #define elf_backend_stack_align 16
171 #endif
172 @@ -866,7 +869,8 @@ static struct elf_backend_data elfNN_bed =
173 elf_backend_no_page_alias,
174 elf_backend_default_execstack,
175 elf_backend_caches_rawsize,
176 - elf_backend_extern_protected_data
177 + elf_backend_extern_protected_data,
178 + elf_backend_always_renumber_dynsyms
179 };
180
181 /* Forward declaration for use when initialising alternative_target field. */
182 --
183 2.11.0
184