Revert part "Set dynamic tag VMA and size from dynamic section when possible"
[binutils-gdb.git] / bfd / elf32-xtensa.c
1 /* Xtensa-specific support for 32-bit ELF.
2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 3 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23
24 #include <stdarg.h>
25 #include <strings.h>
26
27 #include "bfdlink.h"
28 #include "libbfd.h"
29 #include "elf-bfd.h"
30 #include "elf/xtensa.h"
31 #include "splay-tree.h"
32 #include "xtensa-isa.h"
33 #include "xtensa-config.h"
34
35 #define XTENSA_NO_NOP_REMOVAL 0
36
37 /* Local helper functions. */
38
39 static bfd_boolean add_extra_plt_sections (struct bfd_link_info *, int);
40 static char *vsprint_msg (const char *, const char *, int, ...) ATTRIBUTE_PRINTF(2,4);
41 static bfd_reloc_status_type bfd_elf_xtensa_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_boolean do_fix_for_relocatable_link
44 (Elf_Internal_Rela *, bfd *, asection *, bfd_byte *);
45 static void do_fix_for_final_link
46 (Elf_Internal_Rela *, bfd *, asection *, bfd_byte *, bfd_vma *);
47
48 /* Local functions to handle Xtensa configurability. */
49
50 static bfd_boolean is_indirect_call_opcode (xtensa_opcode);
51 static bfd_boolean is_direct_call_opcode (xtensa_opcode);
52 static bfd_boolean is_windowed_call_opcode (xtensa_opcode);
53 static xtensa_opcode get_const16_opcode (void);
54 static xtensa_opcode get_l32r_opcode (void);
55 static bfd_vma l32r_offset (bfd_vma, bfd_vma);
56 static int get_relocation_opnd (xtensa_opcode, int);
57 static int get_relocation_slot (int);
58 static xtensa_opcode get_relocation_opcode
59 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *);
60 static bfd_boolean is_l32r_relocation
61 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *);
62 static bfd_boolean is_alt_relocation (int);
63 static bfd_boolean is_operand_relocation (int);
64 static bfd_size_type insn_decode_len
65 (bfd_byte *, bfd_size_type, bfd_size_type);
66 static xtensa_opcode insn_decode_opcode
67 (bfd_byte *, bfd_size_type, bfd_size_type, int);
68 static bfd_boolean check_branch_target_aligned
69 (bfd_byte *, bfd_size_type, bfd_vma, bfd_vma);
70 static bfd_boolean check_loop_aligned
71 (bfd_byte *, bfd_size_type, bfd_vma, bfd_vma);
72 static bfd_boolean check_branch_target_aligned_address (bfd_vma, int);
73 static bfd_size_type get_asm_simplify_size
74 (bfd_byte *, bfd_size_type, bfd_size_type);
75
76 /* Functions for link-time code simplifications. */
77
78 static bfd_reloc_status_type elf_xtensa_do_asm_simplify
79 (bfd_byte *, bfd_vma, bfd_vma, char **);
80 static bfd_reloc_status_type contract_asm_expansion
81 (bfd_byte *, bfd_vma, Elf_Internal_Rela *, char **);
82 static xtensa_opcode swap_callx_for_call_opcode (xtensa_opcode);
83 static xtensa_opcode get_expanded_call_opcode (bfd_byte *, int, bfd_boolean *);
84
85 /* Access to internal relocations, section contents and symbols. */
86
87 static Elf_Internal_Rela *retrieve_internal_relocs
88 (bfd *, asection *, bfd_boolean);
89 static void pin_internal_relocs (asection *, Elf_Internal_Rela *);
90 static void release_internal_relocs (asection *, Elf_Internal_Rela *);
91 static bfd_byte *retrieve_contents (bfd *, asection *, bfd_boolean);
92 static void pin_contents (asection *, bfd_byte *);
93 static void release_contents (asection *, bfd_byte *);
94 static Elf_Internal_Sym *retrieve_local_syms (bfd *);
95
96 /* Miscellaneous utility functions. */
97
98 static asection *elf_xtensa_get_plt_section (struct bfd_link_info *, int);
99 static asection *elf_xtensa_get_gotplt_section (struct bfd_link_info *, int);
100 static asection *get_elf_r_symndx_section (bfd *, unsigned long);
101 static struct elf_link_hash_entry *get_elf_r_symndx_hash_entry
102 (bfd *, unsigned long);
103 static bfd_vma get_elf_r_symndx_offset (bfd *, unsigned long);
104 static bfd_boolean is_reloc_sym_weak (bfd *, Elf_Internal_Rela *);
105 static bfd_boolean pcrel_reloc_fits (xtensa_opcode, int, bfd_vma, bfd_vma);
106 static bfd_boolean xtensa_is_property_section (asection *);
107 static bfd_boolean xtensa_is_insntable_section (asection *);
108 static bfd_boolean xtensa_is_littable_section (asection *);
109 static bfd_boolean xtensa_is_proptable_section (asection *);
110 static int internal_reloc_compare (const void *, const void *);
111 static int internal_reloc_matches (const void *, const void *);
112 static asection *xtensa_get_property_section (asection *, const char *);
113 static flagword xtensa_get_property_predef_flags (asection *);
114
115 /* Other functions called directly by the linker. */
116
117 typedef void (*deps_callback_t)
118 (asection *, bfd_vma, asection *, bfd_vma, void *);
119 extern bfd_boolean xtensa_callback_required_dependence
120 (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
121
122
123 /* Globally visible flag for choosing size optimization of NOP removal
124 instead of branch-target-aware minimization for NOP removal.
125 When nonzero, narrow all instructions and remove all NOPs possible
126 around longcall expansions. */
127
128 int elf32xtensa_size_opt;
129
130
131 /* The "new_section_hook" is used to set up a per-section
132 "xtensa_relax_info" data structure with additional information used
133 during relaxation. */
134
135 typedef struct xtensa_relax_info_struct xtensa_relax_info;
136
137
138 /* The GNU tools do not easily allow extending interfaces to pass around
139 the pointer to the Xtensa ISA information, so instead we add a global
140 variable here (in BFD) that can be used by any of the tools that need
141 this information. */
142
143 xtensa_isa xtensa_default_isa;
144
145
146 /* When this is true, relocations may have been modified to refer to
147 symbols from other input files. The per-section list of "fix"
148 records needs to be checked when resolving relocations. */
149
150 static bfd_boolean relaxing_section = FALSE;
151
152 /* When this is true, during final links, literals that cannot be
153 coalesced and their relocations may be moved to other sections. */
154
155 int elf32xtensa_no_literal_movement = 1;
156
157 /* Rename one of the generic section flags to better document how it
158 is used here. */
159 /* Whether relocations have been processed. */
160 #define reloc_done sec_flg0
161 \f
162 static reloc_howto_type elf_howto_table[] =
163 {
164 HOWTO (R_XTENSA_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
165 bfd_elf_xtensa_reloc, "R_XTENSA_NONE",
166 FALSE, 0, 0, FALSE),
167 HOWTO (R_XTENSA_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
168 bfd_elf_xtensa_reloc, "R_XTENSA_32",
169 TRUE, 0xffffffff, 0xffffffff, FALSE),
170
171 /* Replace a 32-bit value with a value from the runtime linker (only
172 used by linker-generated stub functions). The r_addend value is
173 special: 1 means to substitute a pointer to the runtime linker's
174 dynamic resolver function; 2 means to substitute the link map for
175 the shared object. */
176 HOWTO (R_XTENSA_RTLD, 0, 2, 32, FALSE, 0, complain_overflow_dont,
177 NULL, "R_XTENSA_RTLD", FALSE, 0, 0, FALSE),
178
179 HOWTO (R_XTENSA_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
180 bfd_elf_generic_reloc, "R_XTENSA_GLOB_DAT",
181 FALSE, 0, 0xffffffff, FALSE),
182 HOWTO (R_XTENSA_JMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
183 bfd_elf_generic_reloc, "R_XTENSA_JMP_SLOT",
184 FALSE, 0, 0xffffffff, FALSE),
185 HOWTO (R_XTENSA_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
186 bfd_elf_generic_reloc, "R_XTENSA_RELATIVE",
187 FALSE, 0, 0xffffffff, FALSE),
188 HOWTO (R_XTENSA_PLT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
189 bfd_elf_xtensa_reloc, "R_XTENSA_PLT",
190 FALSE, 0, 0xffffffff, FALSE),
191
192 EMPTY_HOWTO (7),
193
194 /* Old relocations for backward compatibility. */
195 HOWTO (R_XTENSA_OP0, 0, 0, 0, TRUE, 0, complain_overflow_dont,
196 bfd_elf_xtensa_reloc, "R_XTENSA_OP0", FALSE, 0, 0, TRUE),
197 HOWTO (R_XTENSA_OP1, 0, 0, 0, TRUE, 0, complain_overflow_dont,
198 bfd_elf_xtensa_reloc, "R_XTENSA_OP1", FALSE, 0, 0, TRUE),
199 HOWTO (R_XTENSA_OP2, 0, 0, 0, TRUE, 0, complain_overflow_dont,
200 bfd_elf_xtensa_reloc, "R_XTENSA_OP2", FALSE, 0, 0, TRUE),
201
202 /* Assembly auto-expansion. */
203 HOWTO (R_XTENSA_ASM_EXPAND, 0, 0, 0, TRUE, 0, complain_overflow_dont,
204 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_EXPAND", FALSE, 0, 0, TRUE),
205 /* Relax assembly auto-expansion. */
206 HOWTO (R_XTENSA_ASM_SIMPLIFY, 0, 0, 0, TRUE, 0, complain_overflow_dont,
207 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_SIMPLIFY", FALSE, 0, 0, TRUE),
208
209 EMPTY_HOWTO (13),
210
211 HOWTO (R_XTENSA_32_PCREL, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
212 bfd_elf_xtensa_reloc, "R_XTENSA_32_PCREL",
213 FALSE, 0, 0xffffffff, TRUE),
214
215 /* GNU extension to record C++ vtable hierarchy. */
216 HOWTO (R_XTENSA_GNU_VTINHERIT, 0, 2, 0, FALSE, 0, complain_overflow_dont,
217 NULL, "R_XTENSA_GNU_VTINHERIT",
218 FALSE, 0, 0, FALSE),
219 /* GNU extension to record C++ vtable member usage. */
220 HOWTO (R_XTENSA_GNU_VTENTRY, 0, 2, 0, FALSE, 0, complain_overflow_dont,
221 _bfd_elf_rel_vtable_reloc_fn, "R_XTENSA_GNU_VTENTRY",
222 FALSE, 0, 0, FALSE),
223
224 /* Relocations for supporting difference of symbols. */
225 HOWTO (R_XTENSA_DIFF8, 0, 0, 8, FALSE, 0, complain_overflow_signed,
226 bfd_elf_xtensa_reloc, "R_XTENSA_DIFF8", FALSE, 0, 0xff, FALSE),
227 HOWTO (R_XTENSA_DIFF16, 0, 1, 16, FALSE, 0, complain_overflow_signed,
228 bfd_elf_xtensa_reloc, "R_XTENSA_DIFF16", FALSE, 0, 0xffff, FALSE),
229 HOWTO (R_XTENSA_DIFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
230 bfd_elf_xtensa_reloc, "R_XTENSA_DIFF32", FALSE, 0, 0xffffffff, FALSE),
231
232 /* General immediate operand relocations. */
233 HOWTO (R_XTENSA_SLOT0_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
234 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_OP", FALSE, 0, 0, TRUE),
235 HOWTO (R_XTENSA_SLOT1_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
236 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_OP", FALSE, 0, 0, TRUE),
237 HOWTO (R_XTENSA_SLOT2_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
238 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_OP", FALSE, 0, 0, TRUE),
239 HOWTO (R_XTENSA_SLOT3_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
240 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_OP", FALSE, 0, 0, TRUE),
241 HOWTO (R_XTENSA_SLOT4_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
242 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_OP", FALSE, 0, 0, TRUE),
243 HOWTO (R_XTENSA_SLOT5_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
244 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_OP", FALSE, 0, 0, TRUE),
245 HOWTO (R_XTENSA_SLOT6_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
246 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_OP", FALSE, 0, 0, TRUE),
247 HOWTO (R_XTENSA_SLOT7_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
248 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_OP", FALSE, 0, 0, TRUE),
249 HOWTO (R_XTENSA_SLOT8_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
250 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_OP", FALSE, 0, 0, TRUE),
251 HOWTO (R_XTENSA_SLOT9_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
252 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_OP", FALSE, 0, 0, TRUE),
253 HOWTO (R_XTENSA_SLOT10_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
254 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_OP", FALSE, 0, 0, TRUE),
255 HOWTO (R_XTENSA_SLOT11_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
256 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_OP", FALSE, 0, 0, TRUE),
257 HOWTO (R_XTENSA_SLOT12_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
258 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_OP", FALSE, 0, 0, TRUE),
259 HOWTO (R_XTENSA_SLOT13_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
260 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_OP", FALSE, 0, 0, TRUE),
261 HOWTO (R_XTENSA_SLOT14_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
262 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_OP", FALSE, 0, 0, TRUE),
263
264 /* "Alternate" relocations. The meaning of these is opcode-specific. */
265 HOWTO (R_XTENSA_SLOT0_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
266 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_ALT", FALSE, 0, 0, TRUE),
267 HOWTO (R_XTENSA_SLOT1_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
268 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_ALT", FALSE, 0, 0, TRUE),
269 HOWTO (R_XTENSA_SLOT2_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
270 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_ALT", FALSE, 0, 0, TRUE),
271 HOWTO (R_XTENSA_SLOT3_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
272 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_ALT", FALSE, 0, 0, TRUE),
273 HOWTO (R_XTENSA_SLOT4_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
274 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_ALT", FALSE, 0, 0, TRUE),
275 HOWTO (R_XTENSA_SLOT5_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
276 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_ALT", FALSE, 0, 0, TRUE),
277 HOWTO (R_XTENSA_SLOT6_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
278 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_ALT", FALSE, 0, 0, TRUE),
279 HOWTO (R_XTENSA_SLOT7_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
280 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_ALT", FALSE, 0, 0, TRUE),
281 HOWTO (R_XTENSA_SLOT8_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
282 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_ALT", FALSE, 0, 0, TRUE),
283 HOWTO (R_XTENSA_SLOT9_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
284 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_ALT", FALSE, 0, 0, TRUE),
285 HOWTO (R_XTENSA_SLOT10_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
286 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_ALT", FALSE, 0, 0, TRUE),
287 HOWTO (R_XTENSA_SLOT11_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
288 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_ALT", FALSE, 0, 0, TRUE),
289 HOWTO (R_XTENSA_SLOT12_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
290 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_ALT", FALSE, 0, 0, TRUE),
291 HOWTO (R_XTENSA_SLOT13_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
292 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_ALT", FALSE, 0, 0, TRUE),
293 HOWTO (R_XTENSA_SLOT14_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
294 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_ALT", FALSE, 0, 0, TRUE),
295
296 /* TLS relocations. */
297 HOWTO (R_XTENSA_TLSDESC_FN, 0, 2, 32, FALSE, 0, complain_overflow_dont,
298 bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_FN",
299 FALSE, 0, 0xffffffff, FALSE),
300 HOWTO (R_XTENSA_TLSDESC_ARG, 0, 2, 32, FALSE, 0, complain_overflow_dont,
301 bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_ARG",
302 FALSE, 0, 0xffffffff, FALSE),
303 HOWTO (R_XTENSA_TLS_DTPOFF, 0, 2, 32, FALSE, 0, complain_overflow_dont,
304 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_DTPOFF",
305 FALSE, 0, 0xffffffff, FALSE),
306 HOWTO (R_XTENSA_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_dont,
307 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_TPOFF",
308 FALSE, 0, 0xffffffff, FALSE),
309 HOWTO (R_XTENSA_TLS_FUNC, 0, 0, 0, FALSE, 0, complain_overflow_dont,
310 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_FUNC",
311 FALSE, 0, 0, FALSE),
312 HOWTO (R_XTENSA_TLS_ARG, 0, 0, 0, FALSE, 0, complain_overflow_dont,
313 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_ARG",
314 FALSE, 0, 0, FALSE),
315 HOWTO (R_XTENSA_TLS_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
316 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_CALL",
317 FALSE, 0, 0, FALSE),
318 };
319
320 #if DEBUG_GEN_RELOC
321 #define TRACE(str) \
322 fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
323 #else
324 #define TRACE(str)
325 #endif
326
327 static reloc_howto_type *
328 elf_xtensa_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
329 bfd_reloc_code_real_type code)
330 {
331 switch (code)
332 {
333 case BFD_RELOC_NONE:
334 TRACE ("BFD_RELOC_NONE");
335 return &elf_howto_table[(unsigned) R_XTENSA_NONE ];
336
337 case BFD_RELOC_32:
338 TRACE ("BFD_RELOC_32");
339 return &elf_howto_table[(unsigned) R_XTENSA_32 ];
340
341 case BFD_RELOC_32_PCREL:
342 TRACE ("BFD_RELOC_32_PCREL");
343 return &elf_howto_table[(unsigned) R_XTENSA_32_PCREL ];
344
345 case BFD_RELOC_XTENSA_DIFF8:
346 TRACE ("BFD_RELOC_XTENSA_DIFF8");
347 return &elf_howto_table[(unsigned) R_XTENSA_DIFF8 ];
348
349 case BFD_RELOC_XTENSA_DIFF16:
350 TRACE ("BFD_RELOC_XTENSA_DIFF16");
351 return &elf_howto_table[(unsigned) R_XTENSA_DIFF16 ];
352
353 case BFD_RELOC_XTENSA_DIFF32:
354 TRACE ("BFD_RELOC_XTENSA_DIFF32");
355 return &elf_howto_table[(unsigned) R_XTENSA_DIFF32 ];
356
357 case BFD_RELOC_XTENSA_RTLD:
358 TRACE ("BFD_RELOC_XTENSA_RTLD");
359 return &elf_howto_table[(unsigned) R_XTENSA_RTLD ];
360
361 case BFD_RELOC_XTENSA_GLOB_DAT:
362 TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
363 return &elf_howto_table[(unsigned) R_XTENSA_GLOB_DAT ];
364
365 case BFD_RELOC_XTENSA_JMP_SLOT:
366 TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
367 return &elf_howto_table[(unsigned) R_XTENSA_JMP_SLOT ];
368
369 case BFD_RELOC_XTENSA_RELATIVE:
370 TRACE ("BFD_RELOC_XTENSA_RELATIVE");
371 return &elf_howto_table[(unsigned) R_XTENSA_RELATIVE ];
372
373 case BFD_RELOC_XTENSA_PLT:
374 TRACE ("BFD_RELOC_XTENSA_PLT");
375 return &elf_howto_table[(unsigned) R_XTENSA_PLT ];
376
377 case BFD_RELOC_XTENSA_OP0:
378 TRACE ("BFD_RELOC_XTENSA_OP0");
379 return &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
380
381 case BFD_RELOC_XTENSA_OP1:
382 TRACE ("BFD_RELOC_XTENSA_OP1");
383 return &elf_howto_table[(unsigned) R_XTENSA_OP1 ];
384
385 case BFD_RELOC_XTENSA_OP2:
386 TRACE ("BFD_RELOC_XTENSA_OP2");
387 return &elf_howto_table[(unsigned) R_XTENSA_OP2 ];
388
389 case BFD_RELOC_XTENSA_ASM_EXPAND:
390 TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
391 return &elf_howto_table[(unsigned) R_XTENSA_ASM_EXPAND ];
392
393 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
394 TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
395 return &elf_howto_table[(unsigned) R_XTENSA_ASM_SIMPLIFY ];
396
397 case BFD_RELOC_VTABLE_INHERIT:
398 TRACE ("BFD_RELOC_VTABLE_INHERIT");
399 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTINHERIT ];
400
401 case BFD_RELOC_VTABLE_ENTRY:
402 TRACE ("BFD_RELOC_VTABLE_ENTRY");
403 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTENTRY ];
404
405 case BFD_RELOC_XTENSA_TLSDESC_FN:
406 TRACE ("BFD_RELOC_XTENSA_TLSDESC_FN");
407 return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_FN ];
408
409 case BFD_RELOC_XTENSA_TLSDESC_ARG:
410 TRACE ("BFD_RELOC_XTENSA_TLSDESC_ARG");
411 return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_ARG ];
412
413 case BFD_RELOC_XTENSA_TLS_DTPOFF:
414 TRACE ("BFD_RELOC_XTENSA_TLS_DTPOFF");
415 return &elf_howto_table[(unsigned) R_XTENSA_TLS_DTPOFF ];
416
417 case BFD_RELOC_XTENSA_TLS_TPOFF:
418 TRACE ("BFD_RELOC_XTENSA_TLS_TPOFF");
419 return &elf_howto_table[(unsigned) R_XTENSA_TLS_TPOFF ];
420
421 case BFD_RELOC_XTENSA_TLS_FUNC:
422 TRACE ("BFD_RELOC_XTENSA_TLS_FUNC");
423 return &elf_howto_table[(unsigned) R_XTENSA_TLS_FUNC ];
424
425 case BFD_RELOC_XTENSA_TLS_ARG:
426 TRACE ("BFD_RELOC_XTENSA_TLS_ARG");
427 return &elf_howto_table[(unsigned) R_XTENSA_TLS_ARG ];
428
429 case BFD_RELOC_XTENSA_TLS_CALL:
430 TRACE ("BFD_RELOC_XTENSA_TLS_CALL");
431 return &elf_howto_table[(unsigned) R_XTENSA_TLS_CALL ];
432
433 default:
434 if (code >= BFD_RELOC_XTENSA_SLOT0_OP
435 && code <= BFD_RELOC_XTENSA_SLOT14_OP)
436 {
437 unsigned n = (R_XTENSA_SLOT0_OP +
438 (code - BFD_RELOC_XTENSA_SLOT0_OP));
439 return &elf_howto_table[n];
440 }
441
442 if (code >= BFD_RELOC_XTENSA_SLOT0_ALT
443 && code <= BFD_RELOC_XTENSA_SLOT14_ALT)
444 {
445 unsigned n = (R_XTENSA_SLOT0_ALT +
446 (code - BFD_RELOC_XTENSA_SLOT0_ALT));
447 return &elf_howto_table[n];
448 }
449
450 break;
451 }
452
453 TRACE ("Unknown");
454 return NULL;
455 }
456
457 static reloc_howto_type *
458 elf_xtensa_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
459 const char *r_name)
460 {
461 unsigned int i;
462
463 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
464 if (elf_howto_table[i].name != NULL
465 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
466 return &elf_howto_table[i];
467
468 return NULL;
469 }
470
471
472 /* Given an ELF "rela" relocation, find the corresponding howto and record
473 it in the BFD internal arelent representation of the relocation. */
474
475 static void
476 elf_xtensa_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
477 arelent *cache_ptr,
478 Elf_Internal_Rela *dst)
479 {
480 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
481
482 if (r_type >= (unsigned int) R_XTENSA_max)
483 {
484 /* xgettext:c-format */
485 _bfd_error_handler (_("%B: invalid XTENSA reloc number: %d"), abfd, r_type);
486 r_type = 0;
487 }
488 cache_ptr->howto = &elf_howto_table[r_type];
489 }
490
491 \f
492 /* Functions for the Xtensa ELF linker. */
493
494 /* The name of the dynamic interpreter. This is put in the .interp
495 section. */
496
497 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
498
499 /* The size in bytes of an entry in the procedure linkage table.
500 (This does _not_ include the space for the literals associated with
501 the PLT entry.) */
502
503 #define PLT_ENTRY_SIZE 16
504
505 /* For _really_ large PLTs, we may need to alternate between literals
506 and code to keep the literals within the 256K range of the L32R
507 instructions in the code. It's unlikely that anyone would ever need
508 such a big PLT, but an arbitrary limit on the PLT size would be bad.
509 Thus, we split the PLT into chunks. Since there's very little
510 overhead (2 extra literals) for each chunk, the chunk size is kept
511 small so that the code for handling multiple chunks get used and
512 tested regularly. With 254 entries, there are 1K of literals for
513 each chunk, and that seems like a nice round number. */
514
515 #define PLT_ENTRIES_PER_CHUNK 254
516
517 /* PLT entries are actually used as stub functions for lazy symbol
518 resolution. Once the symbol is resolved, the stub function is never
519 invoked. Note: the 32-byte frame size used here cannot be changed
520 without a corresponding change in the runtime linker. */
521
522 static const bfd_byte elf_xtensa_be_plt_entry[PLT_ENTRY_SIZE] =
523 {
524 #if XSHAL_ABI == XTHAL_ABI_WINDOWED
525 0x6c, 0x10, 0x04, /* entry sp, 32 */
526 #endif
527 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
528 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
529 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
530 0x0a, 0x80, 0x00, /* jx a8 */
531 0 /* unused */
532 };
533
534 static const bfd_byte elf_xtensa_le_plt_entry[PLT_ENTRY_SIZE] =
535 {
536 #if XSHAL_ABI == XTHAL_ABI_WINDOWED
537 0x36, 0x41, 0x00, /* entry sp, 32 */
538 #endif
539 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
540 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
541 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
542 0xa0, 0x08, 0x00, /* jx a8 */
543 0 /* unused */
544 };
545
546 /* The size of the thread control block. */
547 #define TCB_SIZE 8
548
549 struct elf_xtensa_link_hash_entry
550 {
551 struct elf_link_hash_entry elf;
552
553 bfd_signed_vma tlsfunc_refcount;
554
555 #define GOT_UNKNOWN 0
556 #define GOT_NORMAL 1
557 #define GOT_TLS_GD 2 /* global or local dynamic */
558 #define GOT_TLS_IE 4 /* initial or local exec */
559 #define GOT_TLS_ANY (GOT_TLS_GD | GOT_TLS_IE)
560 unsigned char tls_type;
561 };
562
563 #define elf_xtensa_hash_entry(ent) ((struct elf_xtensa_link_hash_entry *)(ent))
564
565 struct elf_xtensa_obj_tdata
566 {
567 struct elf_obj_tdata root;
568
569 /* tls_type for each local got entry. */
570 char *local_got_tls_type;
571
572 bfd_signed_vma *local_tlsfunc_refcounts;
573 };
574
575 #define elf_xtensa_tdata(abfd) \
576 ((struct elf_xtensa_obj_tdata *) (abfd)->tdata.any)
577
578 #define elf_xtensa_local_got_tls_type(abfd) \
579 (elf_xtensa_tdata (abfd)->local_got_tls_type)
580
581 #define elf_xtensa_local_tlsfunc_refcounts(abfd) \
582 (elf_xtensa_tdata (abfd)->local_tlsfunc_refcounts)
583
584 #define is_xtensa_elf(bfd) \
585 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
586 && elf_tdata (bfd) != NULL \
587 && elf_object_id (bfd) == XTENSA_ELF_DATA)
588
589 static bfd_boolean
590 elf_xtensa_mkobject (bfd *abfd)
591 {
592 return bfd_elf_allocate_object (abfd, sizeof (struct elf_xtensa_obj_tdata),
593 XTENSA_ELF_DATA);
594 }
595
596 /* Xtensa ELF linker hash table. */
597
598 struct elf_xtensa_link_hash_table
599 {
600 struct elf_link_hash_table elf;
601
602 /* Short-cuts to get to dynamic linker sections. */
603 asection *sgot;
604 asection *sgotplt;
605 asection *srelgot;
606 asection *splt;
607 asection *srelplt;
608 asection *sgotloc;
609 asection *spltlittbl;
610
611 /* Total count of PLT relocations seen during check_relocs.
612 The actual PLT code must be split into multiple sections and all
613 the sections have to be created before size_dynamic_sections,
614 where we figure out the exact number of PLT entries that will be
615 needed. It is OK if this count is an overestimate, e.g., some
616 relocations may be removed by GC. */
617 int plt_reloc_count;
618
619 struct elf_xtensa_link_hash_entry *tlsbase;
620 };
621
622 /* Get the Xtensa ELF linker hash table from a link_info structure. */
623
624 #define elf_xtensa_hash_table(p) \
625 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
626 == XTENSA_ELF_DATA ? ((struct elf_xtensa_link_hash_table *) ((p)->hash)) : NULL)
627
628 /* Create an entry in an Xtensa ELF linker hash table. */
629
630 static struct bfd_hash_entry *
631 elf_xtensa_link_hash_newfunc (struct bfd_hash_entry *entry,
632 struct bfd_hash_table *table,
633 const char *string)
634 {
635 /* Allocate the structure if it has not already been allocated by a
636 subclass. */
637 if (entry == NULL)
638 {
639 entry = bfd_hash_allocate (table,
640 sizeof (struct elf_xtensa_link_hash_entry));
641 if (entry == NULL)
642 return entry;
643 }
644
645 /* Call the allocation method of the superclass. */
646 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
647 if (entry != NULL)
648 {
649 struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (entry);
650 eh->tlsfunc_refcount = 0;
651 eh->tls_type = GOT_UNKNOWN;
652 }
653
654 return entry;
655 }
656
657 /* Create an Xtensa ELF linker hash table. */
658
659 static struct bfd_link_hash_table *
660 elf_xtensa_link_hash_table_create (bfd *abfd)
661 {
662 struct elf_link_hash_entry *tlsbase;
663 struct elf_xtensa_link_hash_table *ret;
664 bfd_size_type amt = sizeof (struct elf_xtensa_link_hash_table);
665
666 ret = bfd_zmalloc (amt);
667 if (ret == NULL)
668 return NULL;
669
670 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
671 elf_xtensa_link_hash_newfunc,
672 sizeof (struct elf_xtensa_link_hash_entry),
673 XTENSA_ELF_DATA))
674 {
675 free (ret);
676 return NULL;
677 }
678
679 /* Create a hash entry for "_TLS_MODULE_BASE_" to speed up checking
680 for it later. */
681 tlsbase = elf_link_hash_lookup (&ret->elf, "_TLS_MODULE_BASE_",
682 TRUE, FALSE, FALSE);
683 tlsbase->root.type = bfd_link_hash_new;
684 tlsbase->root.u.undef.abfd = NULL;
685 tlsbase->non_elf = 0;
686 ret->tlsbase = elf_xtensa_hash_entry (tlsbase);
687 ret->tlsbase->tls_type = GOT_UNKNOWN;
688
689 return &ret->elf.root;
690 }
691
692 /* Copy the extra info we tack onto an elf_link_hash_entry. */
693
694 static void
695 elf_xtensa_copy_indirect_symbol (struct bfd_link_info *info,
696 struct elf_link_hash_entry *dir,
697 struct elf_link_hash_entry *ind)
698 {
699 struct elf_xtensa_link_hash_entry *edir, *eind;
700
701 edir = elf_xtensa_hash_entry (dir);
702 eind = elf_xtensa_hash_entry (ind);
703
704 if (ind->root.type == bfd_link_hash_indirect)
705 {
706 edir->tlsfunc_refcount += eind->tlsfunc_refcount;
707 eind->tlsfunc_refcount = 0;
708
709 if (dir->got.refcount <= 0)
710 {
711 edir->tls_type = eind->tls_type;
712 eind->tls_type = GOT_UNKNOWN;
713 }
714 }
715
716 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
717 }
718
719 static inline bfd_boolean
720 elf_xtensa_dynamic_symbol_p (struct elf_link_hash_entry *h,
721 struct bfd_link_info *info)
722 {
723 /* Check if we should do dynamic things to this symbol. The
724 "ignore_protected" argument need not be set, because Xtensa code
725 does not require special handling of STV_PROTECTED to make function
726 pointer comparisons work properly. The PLT addresses are never
727 used for function pointers. */
728
729 return _bfd_elf_dynamic_symbol_p (h, info, 0);
730 }
731
732 \f
733 static int
734 property_table_compare (const void *ap, const void *bp)
735 {
736 const property_table_entry *a = (const property_table_entry *) ap;
737 const property_table_entry *b = (const property_table_entry *) bp;
738
739 if (a->address == b->address)
740 {
741 if (a->size != b->size)
742 return (a->size - b->size);
743
744 if ((a->flags & XTENSA_PROP_ALIGN) != (b->flags & XTENSA_PROP_ALIGN))
745 return ((b->flags & XTENSA_PROP_ALIGN)
746 - (a->flags & XTENSA_PROP_ALIGN));
747
748 if ((a->flags & XTENSA_PROP_ALIGN)
749 && (GET_XTENSA_PROP_ALIGNMENT (a->flags)
750 != GET_XTENSA_PROP_ALIGNMENT (b->flags)))
751 return (GET_XTENSA_PROP_ALIGNMENT (a->flags)
752 - GET_XTENSA_PROP_ALIGNMENT (b->flags));
753
754 if ((a->flags & XTENSA_PROP_UNREACHABLE)
755 != (b->flags & XTENSA_PROP_UNREACHABLE))
756 return ((b->flags & XTENSA_PROP_UNREACHABLE)
757 - (a->flags & XTENSA_PROP_UNREACHABLE));
758
759 return (a->flags - b->flags);
760 }
761
762 return (a->address - b->address);
763 }
764
765
766 static int
767 property_table_matches (const void *ap, const void *bp)
768 {
769 const property_table_entry *a = (const property_table_entry *) ap;
770 const property_table_entry *b = (const property_table_entry *) bp;
771
772 /* Check if one entry overlaps with the other. */
773 if ((b->address >= a->address && b->address < (a->address + a->size))
774 || (a->address >= b->address && a->address < (b->address + b->size)))
775 return 0;
776
777 return (a->address - b->address);
778 }
779
780
781 /* Get the literal table or property table entries for the given
782 section. Sets TABLE_P and returns the number of entries. On
783 error, returns a negative value. */
784
785 static int
786 xtensa_read_table_entries (bfd *abfd,
787 asection *section,
788 property_table_entry **table_p,
789 const char *sec_name,
790 bfd_boolean output_addr)
791 {
792 asection *table_section;
793 bfd_size_type table_size = 0;
794 bfd_byte *table_data;
795 property_table_entry *blocks;
796 int blk, block_count;
797 bfd_size_type num_records;
798 Elf_Internal_Rela *internal_relocs, *irel, *rel_end;
799 bfd_vma section_addr, off;
800 flagword predef_flags;
801 bfd_size_type table_entry_size, section_limit;
802
803 if (!section
804 || !(section->flags & SEC_ALLOC)
805 || (section->flags & SEC_DEBUGGING))
806 {
807 *table_p = NULL;
808 return 0;
809 }
810
811 table_section = xtensa_get_property_section (section, sec_name);
812 if (table_section)
813 table_size = table_section->size;
814
815 if (table_size == 0)
816 {
817 *table_p = NULL;
818 return 0;
819 }
820
821 predef_flags = xtensa_get_property_predef_flags (table_section);
822 table_entry_size = 12;
823 if (predef_flags)
824 table_entry_size -= 4;
825
826 num_records = table_size / table_entry_size;
827 table_data = retrieve_contents (abfd, table_section, TRUE);
828 blocks = (property_table_entry *)
829 bfd_malloc (num_records * sizeof (property_table_entry));
830 block_count = 0;
831
832 if (output_addr)
833 section_addr = section->output_section->vma + section->output_offset;
834 else
835 section_addr = section->vma;
836
837 internal_relocs = retrieve_internal_relocs (abfd, table_section, TRUE);
838 if (internal_relocs && !table_section->reloc_done)
839 {
840 qsort (internal_relocs, table_section->reloc_count,
841 sizeof (Elf_Internal_Rela), internal_reloc_compare);
842 irel = internal_relocs;
843 }
844 else
845 irel = NULL;
846
847 section_limit = bfd_get_section_limit (abfd, section);
848 rel_end = internal_relocs + table_section->reloc_count;
849
850 for (off = 0; off < table_size; off += table_entry_size)
851 {
852 bfd_vma address = bfd_get_32 (abfd, table_data + off);
853
854 /* Skip any relocations before the current offset. This should help
855 avoid confusion caused by unexpected relocations for the preceding
856 table entry. */
857 while (irel &&
858 (irel->r_offset < off
859 || (irel->r_offset == off
860 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_NONE)))
861 {
862 irel += 1;
863 if (irel >= rel_end)
864 irel = 0;
865 }
866
867 if (irel && irel->r_offset == off)
868 {
869 bfd_vma sym_off;
870 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
871 BFD_ASSERT (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32);
872
873 if (get_elf_r_symndx_section (abfd, r_symndx) != section)
874 continue;
875
876 sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
877 BFD_ASSERT (sym_off == 0);
878 address += (section_addr + sym_off + irel->r_addend);
879 }
880 else
881 {
882 if (address < section_addr
883 || address >= section_addr + section_limit)
884 continue;
885 }
886
887 blocks[block_count].address = address;
888 blocks[block_count].size = bfd_get_32 (abfd, table_data + off + 4);
889 if (predef_flags)
890 blocks[block_count].flags = predef_flags;
891 else
892 blocks[block_count].flags = bfd_get_32 (abfd, table_data + off + 8);
893 block_count++;
894 }
895
896 release_contents (table_section, table_data);
897 release_internal_relocs (table_section, internal_relocs);
898
899 if (block_count > 0)
900 {
901 /* Now sort them into address order for easy reference. */
902 qsort (blocks, block_count, sizeof (property_table_entry),
903 property_table_compare);
904
905 /* Check that the table contents are valid. Problems may occur,
906 for example, if an unrelocated object file is stripped. */
907 for (blk = 1; blk < block_count; blk++)
908 {
909 /* The only circumstance where two entries may legitimately
910 have the same address is when one of them is a zero-size
911 placeholder to mark a place where fill can be inserted.
912 The zero-size entry should come first. */
913 if (blocks[blk - 1].address == blocks[blk].address &&
914 blocks[blk - 1].size != 0)
915 {
916 /* xgettext:c-format */
917 _bfd_error_handler (_("%B(%A): invalid property table"),
918 abfd, section);
919 bfd_set_error (bfd_error_bad_value);
920 free (blocks);
921 return -1;
922 }
923 }
924 }
925
926 *table_p = blocks;
927 return block_count;
928 }
929
930
931 static property_table_entry *
932 elf_xtensa_find_property_entry (property_table_entry *property_table,
933 int property_table_size,
934 bfd_vma addr)
935 {
936 property_table_entry entry;
937 property_table_entry *rv;
938
939 if (property_table_size == 0)
940 return NULL;
941
942 entry.address = addr;
943 entry.size = 1;
944 entry.flags = 0;
945
946 rv = bsearch (&entry, property_table, property_table_size,
947 sizeof (property_table_entry), property_table_matches);
948 return rv;
949 }
950
951
952 static bfd_boolean
953 elf_xtensa_in_literal_pool (property_table_entry *lit_table,
954 int lit_table_size,
955 bfd_vma addr)
956 {
957 if (elf_xtensa_find_property_entry (lit_table, lit_table_size, addr))
958 return TRUE;
959
960 return FALSE;
961 }
962
963 \f
964 /* Look through the relocs for a section during the first phase, and
965 calculate needed space in the dynamic reloc sections. */
966
967 static bfd_boolean
968 elf_xtensa_check_relocs (bfd *abfd,
969 struct bfd_link_info *info,
970 asection *sec,
971 const Elf_Internal_Rela *relocs)
972 {
973 struct elf_xtensa_link_hash_table *htab;
974 Elf_Internal_Shdr *symtab_hdr;
975 struct elf_link_hash_entry **sym_hashes;
976 const Elf_Internal_Rela *rel;
977 const Elf_Internal_Rela *rel_end;
978
979 if (bfd_link_relocatable (info) || (sec->flags & SEC_ALLOC) == 0)
980 return TRUE;
981
982 BFD_ASSERT (is_xtensa_elf (abfd));
983
984 htab = elf_xtensa_hash_table (info);
985 if (htab == NULL)
986 return FALSE;
987
988 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
989 sym_hashes = elf_sym_hashes (abfd);
990
991 rel_end = relocs + sec->reloc_count;
992 for (rel = relocs; rel < rel_end; rel++)
993 {
994 unsigned int r_type;
995 unsigned long r_symndx;
996 struct elf_link_hash_entry *h = NULL;
997 struct elf_xtensa_link_hash_entry *eh;
998 int tls_type, old_tls_type;
999 bfd_boolean is_got = FALSE;
1000 bfd_boolean is_plt = FALSE;
1001 bfd_boolean is_tlsfunc = FALSE;
1002
1003 r_symndx = ELF32_R_SYM (rel->r_info);
1004 r_type = ELF32_R_TYPE (rel->r_info);
1005
1006 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1007 {
1008 /* xgettext:c-format */
1009 _bfd_error_handler (_("%B: bad symbol index: %d"),
1010 abfd, r_symndx);
1011 return FALSE;
1012 }
1013
1014 if (r_symndx >= symtab_hdr->sh_info)
1015 {
1016 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1017 while (h->root.type == bfd_link_hash_indirect
1018 || h->root.type == bfd_link_hash_warning)
1019 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1020
1021 /* PR15323, ref flags aren't set for references in the same
1022 object. */
1023 h->root.non_ir_ref = 1;
1024 }
1025 eh = elf_xtensa_hash_entry (h);
1026
1027 switch (r_type)
1028 {
1029 case R_XTENSA_TLSDESC_FN:
1030 if (bfd_link_pic (info))
1031 {
1032 tls_type = GOT_TLS_GD;
1033 is_got = TRUE;
1034 is_tlsfunc = TRUE;
1035 }
1036 else
1037 tls_type = GOT_TLS_IE;
1038 break;
1039
1040 case R_XTENSA_TLSDESC_ARG:
1041 if (bfd_link_pic (info))
1042 {
1043 tls_type = GOT_TLS_GD;
1044 is_got = TRUE;
1045 }
1046 else
1047 {
1048 tls_type = GOT_TLS_IE;
1049 if (h && elf_xtensa_hash_entry (h) != htab->tlsbase)
1050 is_got = TRUE;
1051 }
1052 break;
1053
1054 case R_XTENSA_TLS_DTPOFF:
1055 if (bfd_link_pic (info))
1056 tls_type = GOT_TLS_GD;
1057 else
1058 tls_type = GOT_TLS_IE;
1059 break;
1060
1061 case R_XTENSA_TLS_TPOFF:
1062 tls_type = GOT_TLS_IE;
1063 if (bfd_link_pic (info))
1064 info->flags |= DF_STATIC_TLS;
1065 if (bfd_link_pic (info) || h)
1066 is_got = TRUE;
1067 break;
1068
1069 case R_XTENSA_32:
1070 tls_type = GOT_NORMAL;
1071 is_got = TRUE;
1072 break;
1073
1074 case R_XTENSA_PLT:
1075 tls_type = GOT_NORMAL;
1076 is_plt = TRUE;
1077 break;
1078
1079 case R_XTENSA_GNU_VTINHERIT:
1080 /* This relocation describes the C++ object vtable hierarchy.
1081 Reconstruct it for later use during GC. */
1082 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1083 return FALSE;
1084 continue;
1085
1086 case R_XTENSA_GNU_VTENTRY:
1087 /* This relocation describes which C++ vtable entries are actually
1088 used. Record for later use during GC. */
1089 BFD_ASSERT (h != NULL);
1090 if (h != NULL
1091 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1092 return FALSE;
1093 continue;
1094
1095 default:
1096 /* Nothing to do for any other relocations. */
1097 continue;
1098 }
1099
1100 if (h)
1101 {
1102 if (is_plt)
1103 {
1104 if (h->plt.refcount <= 0)
1105 {
1106 h->needs_plt = 1;
1107 h->plt.refcount = 1;
1108 }
1109 else
1110 h->plt.refcount += 1;
1111
1112 /* Keep track of the total PLT relocation count even if we
1113 don't yet know whether the dynamic sections will be
1114 created. */
1115 htab->plt_reloc_count += 1;
1116
1117 if (elf_hash_table (info)->dynamic_sections_created)
1118 {
1119 if (! add_extra_plt_sections (info, htab->plt_reloc_count))
1120 return FALSE;
1121 }
1122 }
1123 else if (is_got)
1124 {
1125 if (h->got.refcount <= 0)
1126 h->got.refcount = 1;
1127 else
1128 h->got.refcount += 1;
1129 }
1130
1131 if (is_tlsfunc)
1132 eh->tlsfunc_refcount += 1;
1133
1134 old_tls_type = eh->tls_type;
1135 }
1136 else
1137 {
1138 /* Allocate storage the first time. */
1139 if (elf_local_got_refcounts (abfd) == NULL)
1140 {
1141 bfd_size_type size = symtab_hdr->sh_info;
1142 void *mem;
1143
1144 mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
1145 if (mem == NULL)
1146 return FALSE;
1147 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) mem;
1148
1149 mem = bfd_zalloc (abfd, size);
1150 if (mem == NULL)
1151 return FALSE;
1152 elf_xtensa_local_got_tls_type (abfd) = (char *) mem;
1153
1154 mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
1155 if (mem == NULL)
1156 return FALSE;
1157 elf_xtensa_local_tlsfunc_refcounts (abfd)
1158 = (bfd_signed_vma *) mem;
1159 }
1160
1161 /* This is a global offset table entry for a local symbol. */
1162 if (is_got || is_plt)
1163 elf_local_got_refcounts (abfd) [r_symndx] += 1;
1164
1165 if (is_tlsfunc)
1166 elf_xtensa_local_tlsfunc_refcounts (abfd) [r_symndx] += 1;
1167
1168 old_tls_type = elf_xtensa_local_got_tls_type (abfd) [r_symndx];
1169 }
1170
1171 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1172 tls_type |= old_tls_type;
1173 /* If a TLS symbol is accessed using IE at least once,
1174 there is no point to use a dynamic model for it. */
1175 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1176 && ((old_tls_type & GOT_TLS_GD) == 0
1177 || (tls_type & GOT_TLS_IE) == 0))
1178 {
1179 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GD))
1180 tls_type = old_tls_type;
1181 else if ((old_tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_GD))
1182 tls_type |= old_tls_type;
1183 else
1184 {
1185 _bfd_error_handler
1186 /* xgettext:c-format */
1187 (_("%B: `%s' accessed both as normal and thread local symbol"),
1188 abfd,
1189 h ? h->root.root.string : "<local>");
1190 return FALSE;
1191 }
1192 }
1193
1194 if (old_tls_type != tls_type)
1195 {
1196 if (eh)
1197 eh->tls_type = tls_type;
1198 else
1199 elf_xtensa_local_got_tls_type (abfd) [r_symndx] = tls_type;
1200 }
1201 }
1202
1203 return TRUE;
1204 }
1205
1206
1207 static void
1208 elf_xtensa_make_sym_local (struct bfd_link_info *info,
1209 struct elf_link_hash_entry *h)
1210 {
1211 if (bfd_link_pic (info))
1212 {
1213 if (h->plt.refcount > 0)
1214 {
1215 /* For shared objects, there's no need for PLT entries for local
1216 symbols (use RELATIVE relocs instead of JMP_SLOT relocs). */
1217 if (h->got.refcount < 0)
1218 h->got.refcount = 0;
1219 h->got.refcount += h->plt.refcount;
1220 h->plt.refcount = 0;
1221 }
1222 }
1223 else
1224 {
1225 /* Don't need any dynamic relocations at all. */
1226 h->plt.refcount = 0;
1227 h->got.refcount = 0;
1228 }
1229 }
1230
1231
1232 static void
1233 elf_xtensa_hide_symbol (struct bfd_link_info *info,
1234 struct elf_link_hash_entry *h,
1235 bfd_boolean force_local)
1236 {
1237 /* For a shared link, move the plt refcount to the got refcount to leave
1238 space for RELATIVE relocs. */
1239 elf_xtensa_make_sym_local (info, h);
1240
1241 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
1242 }
1243
1244
1245 /* Return the section that should be marked against GC for a given
1246 relocation. */
1247
1248 static asection *
1249 elf_xtensa_gc_mark_hook (asection *sec,
1250 struct bfd_link_info *info,
1251 Elf_Internal_Rela *rel,
1252 struct elf_link_hash_entry *h,
1253 Elf_Internal_Sym *sym)
1254 {
1255 /* Property sections are marked "KEEP" in the linker scripts, but they
1256 should not cause other sections to be marked. (This approach relies
1257 on elf_xtensa_discard_info to remove property table entries that
1258 describe discarded sections. Alternatively, it might be more
1259 efficient to avoid using "KEEP" in the linker scripts and instead use
1260 the gc_mark_extra_sections hook to mark only the property sections
1261 that describe marked sections. That alternative does not work well
1262 with the current property table sections, which do not correspond
1263 one-to-one with the sections they describe, but that should be fixed
1264 someday.) */
1265 if (xtensa_is_property_section (sec))
1266 return NULL;
1267
1268 if (h != NULL)
1269 switch (ELF32_R_TYPE (rel->r_info))
1270 {
1271 case R_XTENSA_GNU_VTINHERIT:
1272 case R_XTENSA_GNU_VTENTRY:
1273 return NULL;
1274 }
1275
1276 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1277 }
1278
1279
1280 /* Update the GOT & PLT entry reference counts
1281 for the section being removed. */
1282
1283 static bfd_boolean
1284 elf_xtensa_gc_sweep_hook (bfd *abfd,
1285 struct bfd_link_info *info,
1286 asection *sec,
1287 const Elf_Internal_Rela *relocs)
1288 {
1289 Elf_Internal_Shdr *symtab_hdr;
1290 struct elf_link_hash_entry **sym_hashes;
1291 const Elf_Internal_Rela *rel, *relend;
1292 struct elf_xtensa_link_hash_table *htab;
1293
1294 htab = elf_xtensa_hash_table (info);
1295 if (htab == NULL)
1296 return FALSE;
1297
1298 if (bfd_link_relocatable (info))
1299 return TRUE;
1300
1301 if ((sec->flags & SEC_ALLOC) == 0)
1302 return TRUE;
1303
1304 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1305 sym_hashes = elf_sym_hashes (abfd);
1306
1307 relend = relocs + sec->reloc_count;
1308 for (rel = relocs; rel < relend; rel++)
1309 {
1310 unsigned long r_symndx;
1311 unsigned int r_type;
1312 struct elf_link_hash_entry *h = NULL;
1313 struct elf_xtensa_link_hash_entry *eh;
1314 bfd_boolean is_got = FALSE;
1315 bfd_boolean is_plt = FALSE;
1316 bfd_boolean is_tlsfunc = FALSE;
1317
1318 r_symndx = ELF32_R_SYM (rel->r_info);
1319 if (r_symndx >= symtab_hdr->sh_info)
1320 {
1321 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1322 while (h->root.type == bfd_link_hash_indirect
1323 || h->root.type == bfd_link_hash_warning)
1324 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1325 }
1326 eh = elf_xtensa_hash_entry (h);
1327
1328 r_type = ELF32_R_TYPE (rel->r_info);
1329 switch (r_type)
1330 {
1331 case R_XTENSA_TLSDESC_FN:
1332 if (bfd_link_pic (info))
1333 {
1334 is_got = TRUE;
1335 is_tlsfunc = TRUE;
1336 }
1337 break;
1338
1339 case R_XTENSA_TLSDESC_ARG:
1340 if (bfd_link_pic (info))
1341 is_got = TRUE;
1342 else
1343 {
1344 if (h && elf_xtensa_hash_entry (h) != htab->tlsbase)
1345 is_got = TRUE;
1346 }
1347 break;
1348
1349 case R_XTENSA_TLS_TPOFF:
1350 if (bfd_link_pic (info) || h)
1351 is_got = TRUE;
1352 break;
1353
1354 case R_XTENSA_32:
1355 is_got = TRUE;
1356 break;
1357
1358 case R_XTENSA_PLT:
1359 is_plt = TRUE;
1360 break;
1361
1362 default:
1363 continue;
1364 }
1365
1366 if (h)
1367 {
1368 if (is_plt)
1369 {
1370 /* If the symbol has been localized its plt.refcount got moved
1371 to got.refcount. Handle it as GOT. */
1372 if (h->plt.refcount > 0)
1373 h->plt.refcount--;
1374 else
1375 is_got = TRUE;
1376 }
1377 if (is_got)
1378 {
1379 if (h->got.refcount > 0)
1380 h->got.refcount--;
1381 }
1382 if (is_tlsfunc)
1383 {
1384 if (eh->tlsfunc_refcount > 0)
1385 eh->tlsfunc_refcount--;
1386 }
1387 }
1388 else
1389 {
1390 if (is_got || is_plt)
1391 {
1392 bfd_signed_vma *got_refcount
1393 = &elf_local_got_refcounts (abfd) [r_symndx];
1394 if (*got_refcount > 0)
1395 *got_refcount -= 1;
1396 }
1397 if (is_tlsfunc)
1398 {
1399 bfd_signed_vma *tlsfunc_refcount
1400 = &elf_xtensa_local_tlsfunc_refcounts (abfd) [r_symndx];
1401 if (*tlsfunc_refcount > 0)
1402 *tlsfunc_refcount -= 1;
1403 }
1404 }
1405 }
1406
1407 return TRUE;
1408 }
1409
1410
1411 /* Create all the dynamic sections. */
1412
1413 static bfd_boolean
1414 elf_xtensa_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1415 {
1416 struct elf_xtensa_link_hash_table *htab;
1417 flagword flags, noalloc_flags;
1418
1419 htab = elf_xtensa_hash_table (info);
1420 if (htab == NULL)
1421 return FALSE;
1422
1423 /* First do all the standard stuff. */
1424 if (! _bfd_elf_create_dynamic_sections (dynobj, info))
1425 return FALSE;
1426 htab->splt = bfd_get_linker_section (dynobj, ".plt");
1427 htab->srelplt = bfd_get_linker_section (dynobj, ".rela.plt");
1428 htab->sgot = bfd_get_linker_section (dynobj, ".got");
1429 htab->sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
1430 htab->srelgot = bfd_get_linker_section (dynobj, ".rela.got");
1431
1432 /* Create any extra PLT sections in case check_relocs has already
1433 been called on all the non-dynamic input files. */
1434 if (! add_extra_plt_sections (info, htab->plt_reloc_count))
1435 return FALSE;
1436
1437 noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
1438 | SEC_LINKER_CREATED | SEC_READONLY);
1439 flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
1440
1441 /* Mark the ".got.plt" section READONLY. */
1442 if (htab->sgotplt == NULL
1443 || ! bfd_set_section_flags (dynobj, htab->sgotplt, flags))
1444 return FALSE;
1445
1446 /* Create ".got.loc" (literal tables for use by dynamic linker). */
1447 htab->sgotloc = bfd_make_section_anyway_with_flags (dynobj, ".got.loc",
1448 flags);
1449 if (htab->sgotloc == NULL
1450 || ! bfd_set_section_alignment (dynobj, htab->sgotloc, 2))
1451 return FALSE;
1452
1453 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
1454 htab->spltlittbl = bfd_make_section_anyway_with_flags (dynobj, ".xt.lit.plt",
1455 noalloc_flags);
1456 if (htab->spltlittbl == NULL
1457 || ! bfd_set_section_alignment (dynobj, htab->spltlittbl, 2))
1458 return FALSE;
1459
1460 return TRUE;
1461 }
1462
1463
1464 static bfd_boolean
1465 add_extra_plt_sections (struct bfd_link_info *info, int count)
1466 {
1467 bfd *dynobj = elf_hash_table (info)->dynobj;
1468 int chunk;
1469
1470 /* Iterate over all chunks except 0 which uses the standard ".plt" and
1471 ".got.plt" sections. */
1472 for (chunk = count / PLT_ENTRIES_PER_CHUNK; chunk > 0; chunk--)
1473 {
1474 char *sname;
1475 flagword flags;
1476 asection *s;
1477
1478 /* Stop when we find a section has already been created. */
1479 if (elf_xtensa_get_plt_section (info, chunk))
1480 break;
1481
1482 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1483 | SEC_LINKER_CREATED | SEC_READONLY);
1484
1485 sname = (char *) bfd_malloc (10);
1486 sprintf (sname, ".plt.%u", chunk);
1487 s = bfd_make_section_anyway_with_flags (dynobj, sname, flags | SEC_CODE);
1488 if (s == NULL
1489 || ! bfd_set_section_alignment (dynobj, s, 2))
1490 return FALSE;
1491
1492 sname = (char *) bfd_malloc (14);
1493 sprintf (sname, ".got.plt.%u", chunk);
1494 s = bfd_make_section_anyway_with_flags (dynobj, sname, flags);
1495 if (s == NULL
1496 || ! bfd_set_section_alignment (dynobj, s, 2))
1497 return FALSE;
1498 }
1499
1500 return TRUE;
1501 }
1502
1503
1504 /* Adjust a symbol defined by a dynamic object and referenced by a
1505 regular object. The current definition is in some section of the
1506 dynamic object, but we're not including those sections. We have to
1507 change the definition to something the rest of the link can
1508 understand. */
1509
1510 static bfd_boolean
1511 elf_xtensa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1512 struct elf_link_hash_entry *h)
1513 {
1514 /* If this is a weak symbol, and there is a real definition, the
1515 processor independent code will have arranged for us to see the
1516 real definition first, and we can just use the same value. */
1517 if (h->u.weakdef)
1518 {
1519 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1520 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1521 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1522 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1523 return TRUE;
1524 }
1525
1526 /* This is a reference to a symbol defined by a dynamic object. The
1527 reference must go through the GOT, so there's no need for COPY relocs,
1528 .dynbss, etc. */
1529
1530 return TRUE;
1531 }
1532
1533
1534 static bfd_boolean
1535 elf_xtensa_allocate_dynrelocs (struct elf_link_hash_entry *h, void *arg)
1536 {
1537 struct bfd_link_info *info;
1538 struct elf_xtensa_link_hash_table *htab;
1539 struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (h);
1540
1541 if (h->root.type == bfd_link_hash_indirect)
1542 return TRUE;
1543
1544 info = (struct bfd_link_info *) arg;
1545 htab = elf_xtensa_hash_table (info);
1546 if (htab == NULL)
1547 return FALSE;
1548
1549 /* If we saw any use of an IE model for this symbol, we can then optimize
1550 away GOT entries for any TLSDESC_FN relocs. */
1551 if ((eh->tls_type & GOT_TLS_IE) != 0)
1552 {
1553 BFD_ASSERT (h->got.refcount >= eh->tlsfunc_refcount);
1554 h->got.refcount -= eh->tlsfunc_refcount;
1555 }
1556
1557 if (! elf_xtensa_dynamic_symbol_p (h, info))
1558 elf_xtensa_make_sym_local (info, h);
1559
1560 if (h->plt.refcount > 0)
1561 htab->srelplt->size += (h->plt.refcount * sizeof (Elf32_External_Rela));
1562
1563 if (h->got.refcount > 0)
1564 htab->srelgot->size += (h->got.refcount * sizeof (Elf32_External_Rela));
1565
1566 return TRUE;
1567 }
1568
1569
1570 static void
1571 elf_xtensa_allocate_local_got_size (struct bfd_link_info *info)
1572 {
1573 struct elf_xtensa_link_hash_table *htab;
1574 bfd *i;
1575
1576 htab = elf_xtensa_hash_table (info);
1577 if (htab == NULL)
1578 return;
1579
1580 for (i = info->input_bfds; i; i = i->link.next)
1581 {
1582 bfd_signed_vma *local_got_refcounts;
1583 bfd_size_type j, cnt;
1584 Elf_Internal_Shdr *symtab_hdr;
1585
1586 local_got_refcounts = elf_local_got_refcounts (i);
1587 if (!local_got_refcounts)
1588 continue;
1589
1590 symtab_hdr = &elf_tdata (i)->symtab_hdr;
1591 cnt = symtab_hdr->sh_info;
1592
1593 for (j = 0; j < cnt; ++j)
1594 {
1595 /* If we saw any use of an IE model for this symbol, we can
1596 then optimize away GOT entries for any TLSDESC_FN relocs. */
1597 if ((elf_xtensa_local_got_tls_type (i) [j] & GOT_TLS_IE) != 0)
1598 {
1599 bfd_signed_vma *tlsfunc_refcount
1600 = &elf_xtensa_local_tlsfunc_refcounts (i) [j];
1601 BFD_ASSERT (local_got_refcounts[j] >= *tlsfunc_refcount);
1602 local_got_refcounts[j] -= *tlsfunc_refcount;
1603 }
1604
1605 if (local_got_refcounts[j] > 0)
1606 htab->srelgot->size += (local_got_refcounts[j]
1607 * sizeof (Elf32_External_Rela));
1608 }
1609 }
1610 }
1611
1612
1613 /* Set the sizes of the dynamic sections. */
1614
1615 static bfd_boolean
1616 elf_xtensa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1617 struct bfd_link_info *info)
1618 {
1619 struct elf_xtensa_link_hash_table *htab;
1620 bfd *dynobj, *abfd;
1621 asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
1622 bfd_boolean relplt, relgot;
1623 int plt_entries, plt_chunks, chunk;
1624
1625 plt_entries = 0;
1626 plt_chunks = 0;
1627
1628 htab = elf_xtensa_hash_table (info);
1629 if (htab == NULL)
1630 return FALSE;
1631
1632 dynobj = elf_hash_table (info)->dynobj;
1633 if (dynobj == NULL)
1634 abort ();
1635 srelgot = htab->srelgot;
1636 srelplt = htab->srelplt;
1637
1638 if (elf_hash_table (info)->dynamic_sections_created)
1639 {
1640 BFD_ASSERT (htab->srelgot != NULL
1641 && htab->srelplt != NULL
1642 && htab->sgot != NULL
1643 && htab->spltlittbl != NULL
1644 && htab->sgotloc != NULL);
1645
1646 /* Set the contents of the .interp section to the interpreter. */
1647 if (bfd_link_executable (info) && !info->nointerp)
1648 {
1649 s = bfd_get_linker_section (dynobj, ".interp");
1650 if (s == NULL)
1651 abort ();
1652 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1653 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1654 }
1655
1656 /* Allocate room for one word in ".got". */
1657 htab->sgot->size = 4;
1658
1659 /* Allocate space in ".rela.got" for literals that reference global
1660 symbols and space in ".rela.plt" for literals that have PLT
1661 entries. */
1662 elf_link_hash_traverse (elf_hash_table (info),
1663 elf_xtensa_allocate_dynrelocs,
1664 (void *) info);
1665
1666 /* If we are generating a shared object, we also need space in
1667 ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1668 reference local symbols. */
1669 if (bfd_link_pic (info))
1670 elf_xtensa_allocate_local_got_size (info);
1671
1672 /* Allocate space in ".plt" to match the size of ".rela.plt". For
1673 each PLT entry, we need the PLT code plus a 4-byte literal.
1674 For each chunk of ".plt", we also need two more 4-byte
1675 literals, two corresponding entries in ".rela.got", and an
1676 8-byte entry in ".xt.lit.plt". */
1677 spltlittbl = htab->spltlittbl;
1678 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
1679 plt_chunks =
1680 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
1681
1682 /* Iterate over all the PLT chunks, including any extra sections
1683 created earlier because the initial count of PLT relocations
1684 was an overestimate. */
1685 for (chunk = 0;
1686 (splt = elf_xtensa_get_plt_section (info, chunk)) != NULL;
1687 chunk++)
1688 {
1689 int chunk_entries;
1690
1691 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
1692 BFD_ASSERT (sgotplt != NULL);
1693
1694 if (chunk < plt_chunks - 1)
1695 chunk_entries = PLT_ENTRIES_PER_CHUNK;
1696 else if (chunk == plt_chunks - 1)
1697 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
1698 else
1699 chunk_entries = 0;
1700
1701 if (chunk_entries != 0)
1702 {
1703 sgotplt->size = 4 * (chunk_entries + 2);
1704 splt->size = PLT_ENTRY_SIZE * chunk_entries;
1705 srelgot->size += 2 * sizeof (Elf32_External_Rela);
1706 spltlittbl->size += 8;
1707 }
1708 else
1709 {
1710 sgotplt->size = 0;
1711 splt->size = 0;
1712 }
1713 }
1714
1715 /* Allocate space in ".got.loc" to match the total size of all the
1716 literal tables. */
1717 sgotloc = htab->sgotloc;
1718 sgotloc->size = spltlittbl->size;
1719 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
1720 {
1721 if (abfd->flags & DYNAMIC)
1722 continue;
1723 for (s = abfd->sections; s != NULL; s = s->next)
1724 {
1725 if (! discarded_section (s)
1726 && xtensa_is_littable_section (s)
1727 && s != spltlittbl)
1728 sgotloc->size += s->size;
1729 }
1730 }
1731 }
1732
1733 /* Allocate memory for dynamic sections. */
1734 relplt = FALSE;
1735 relgot = FALSE;
1736 for (s = dynobj->sections; s != NULL; s = s->next)
1737 {
1738 const char *name;
1739
1740 if ((s->flags & SEC_LINKER_CREATED) == 0)
1741 continue;
1742
1743 /* It's OK to base decisions on the section name, because none
1744 of the dynobj section names depend upon the input files. */
1745 name = bfd_get_section_name (dynobj, s);
1746
1747 if (CONST_STRNEQ (name, ".rela"))
1748 {
1749 if (s->size != 0)
1750 {
1751 if (strcmp (name, ".rela.plt") == 0)
1752 relplt = TRUE;
1753 else if (strcmp (name, ".rela.got") == 0)
1754 relgot = TRUE;
1755
1756 /* We use the reloc_count field as a counter if we need
1757 to copy relocs into the output file. */
1758 s->reloc_count = 0;
1759 }
1760 }
1761 else if (! CONST_STRNEQ (name, ".plt.")
1762 && ! CONST_STRNEQ (name, ".got.plt.")
1763 && strcmp (name, ".got") != 0
1764 && strcmp (name, ".plt") != 0
1765 && strcmp (name, ".got.plt") != 0
1766 && strcmp (name, ".xt.lit.plt") != 0
1767 && strcmp (name, ".got.loc") != 0)
1768 {
1769 /* It's not one of our sections, so don't allocate space. */
1770 continue;
1771 }
1772
1773 if (s->size == 0)
1774 {
1775 /* If we don't need this section, strip it from the output
1776 file. We must create the ".plt*" and ".got.plt*"
1777 sections in create_dynamic_sections and/or check_relocs
1778 based on a conservative estimate of the PLT relocation
1779 count, because the sections must be created before the
1780 linker maps input sections to output sections. The
1781 linker does that before size_dynamic_sections, where we
1782 compute the exact size of the PLT, so there may be more
1783 of these sections than are actually needed. */
1784 s->flags |= SEC_EXCLUDE;
1785 }
1786 else if ((s->flags & SEC_HAS_CONTENTS) != 0)
1787 {
1788 /* Allocate memory for the section contents. */
1789 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1790 if (s->contents == NULL)
1791 return FALSE;
1792 }
1793 }
1794
1795 if (elf_hash_table (info)->dynamic_sections_created)
1796 {
1797 /* Add the special XTENSA_RTLD relocations now. The offsets won't be
1798 known until finish_dynamic_sections, but we need to get the relocs
1799 in place before they are sorted. */
1800 for (chunk = 0; chunk < plt_chunks; chunk++)
1801 {
1802 Elf_Internal_Rela irela;
1803 bfd_byte *loc;
1804
1805 irela.r_offset = 0;
1806 irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
1807 irela.r_addend = 0;
1808
1809 loc = (srelgot->contents
1810 + srelgot->reloc_count * sizeof (Elf32_External_Rela));
1811 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
1812 bfd_elf32_swap_reloca_out (output_bfd, &irela,
1813 loc + sizeof (Elf32_External_Rela));
1814 srelgot->reloc_count += 2;
1815 }
1816
1817 /* Add some entries to the .dynamic section. We fill in the
1818 values later, in elf_xtensa_finish_dynamic_sections, but we
1819 must add the entries now so that we get the correct size for
1820 the .dynamic section. The DT_DEBUG entry is filled in by the
1821 dynamic linker and used by the debugger. */
1822 #define add_dynamic_entry(TAG, VAL) \
1823 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1824
1825 if (bfd_link_executable (info))
1826 {
1827 if (!add_dynamic_entry (DT_DEBUG, 0))
1828 return FALSE;
1829 }
1830
1831 if (relplt)
1832 {
1833 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1834 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1835 || !add_dynamic_entry (DT_JMPREL, 0))
1836 return FALSE;
1837 }
1838
1839 if (relgot)
1840 {
1841 if (!add_dynamic_entry (DT_RELA, 0)
1842 || !add_dynamic_entry (DT_RELASZ, 0)
1843 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1844 return FALSE;
1845 }
1846
1847 if (!add_dynamic_entry (DT_PLTGOT, 0)
1848 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
1849 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
1850 return FALSE;
1851 }
1852 #undef add_dynamic_entry
1853
1854 return TRUE;
1855 }
1856
1857 static bfd_boolean
1858 elf_xtensa_always_size_sections (bfd *output_bfd,
1859 struct bfd_link_info *info)
1860 {
1861 struct elf_xtensa_link_hash_table *htab;
1862 asection *tls_sec;
1863
1864 htab = elf_xtensa_hash_table (info);
1865 if (htab == NULL)
1866 return FALSE;
1867
1868 tls_sec = htab->elf.tls_sec;
1869
1870 if (tls_sec && (htab->tlsbase->tls_type & GOT_TLS_ANY) != 0)
1871 {
1872 struct elf_link_hash_entry *tlsbase = &htab->tlsbase->elf;
1873 struct bfd_link_hash_entry *bh = &tlsbase->root;
1874 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
1875
1876 tlsbase->type = STT_TLS;
1877 if (!(_bfd_generic_link_add_one_symbol
1878 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1879 tls_sec, 0, NULL, FALSE,
1880 bed->collect, &bh)))
1881 return FALSE;
1882 tlsbase->def_regular = 1;
1883 tlsbase->other = STV_HIDDEN;
1884 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
1885 }
1886
1887 return TRUE;
1888 }
1889
1890 \f
1891 /* Return the base VMA address which should be subtracted from real addresses
1892 when resolving @dtpoff relocation.
1893 This is PT_TLS segment p_vaddr. */
1894
1895 static bfd_vma
1896 dtpoff_base (struct bfd_link_info *info)
1897 {
1898 /* If tls_sec is NULL, we should have signalled an error already. */
1899 if (elf_hash_table (info)->tls_sec == NULL)
1900 return 0;
1901 return elf_hash_table (info)->tls_sec->vma;
1902 }
1903
1904 /* Return the relocation value for @tpoff relocation
1905 if STT_TLS virtual address is ADDRESS. */
1906
1907 static bfd_vma
1908 tpoff (struct bfd_link_info *info, bfd_vma address)
1909 {
1910 struct elf_link_hash_table *htab = elf_hash_table (info);
1911 bfd_vma base;
1912
1913 /* If tls_sec is NULL, we should have signalled an error already. */
1914 if (htab->tls_sec == NULL)
1915 return 0;
1916 base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
1917 return address - htab->tls_sec->vma + base;
1918 }
1919
1920 /* Perform the specified relocation. The instruction at (contents + address)
1921 is modified to set one operand to represent the value in "relocation". The
1922 operand position is determined by the relocation type recorded in the
1923 howto. */
1924
1925 #define CALL_SEGMENT_BITS (30)
1926 #define CALL_SEGMENT_SIZE (1 << CALL_SEGMENT_BITS)
1927
1928 static bfd_reloc_status_type
1929 elf_xtensa_do_reloc (reloc_howto_type *howto,
1930 bfd *abfd,
1931 asection *input_section,
1932 bfd_vma relocation,
1933 bfd_byte *contents,
1934 bfd_vma address,
1935 bfd_boolean is_weak_undef,
1936 char **error_message)
1937 {
1938 xtensa_format fmt;
1939 xtensa_opcode opcode;
1940 xtensa_isa isa = xtensa_default_isa;
1941 static xtensa_insnbuf ibuff = NULL;
1942 static xtensa_insnbuf sbuff = NULL;
1943 bfd_vma self_address;
1944 bfd_size_type input_size;
1945 int opnd, slot;
1946 uint32 newval;
1947
1948 if (!ibuff)
1949 {
1950 ibuff = xtensa_insnbuf_alloc (isa);
1951 sbuff = xtensa_insnbuf_alloc (isa);
1952 }
1953
1954 input_size = bfd_get_section_limit (abfd, input_section);
1955
1956 /* Calculate the PC address for this instruction. */
1957 self_address = (input_section->output_section->vma
1958 + input_section->output_offset
1959 + address);
1960
1961 switch (howto->type)
1962 {
1963 case R_XTENSA_NONE:
1964 case R_XTENSA_DIFF8:
1965 case R_XTENSA_DIFF16:
1966 case R_XTENSA_DIFF32:
1967 case R_XTENSA_TLS_FUNC:
1968 case R_XTENSA_TLS_ARG:
1969 case R_XTENSA_TLS_CALL:
1970 return bfd_reloc_ok;
1971
1972 case R_XTENSA_ASM_EXPAND:
1973 if (!is_weak_undef)
1974 {
1975 /* Check for windowed CALL across a 1GB boundary. */
1976 opcode = get_expanded_call_opcode (contents + address,
1977 input_size - address, 0);
1978 if (is_windowed_call_opcode (opcode))
1979 {
1980 if ((self_address >> CALL_SEGMENT_BITS)
1981 != (relocation >> CALL_SEGMENT_BITS))
1982 {
1983 *error_message = "windowed longcall crosses 1GB boundary; "
1984 "return may fail";
1985 return bfd_reloc_dangerous;
1986 }
1987 }
1988 }
1989 return bfd_reloc_ok;
1990
1991 case R_XTENSA_ASM_SIMPLIFY:
1992 {
1993 /* Convert the L32R/CALLX to CALL. */
1994 bfd_reloc_status_type retval =
1995 elf_xtensa_do_asm_simplify (contents, address, input_size,
1996 error_message);
1997 if (retval != bfd_reloc_ok)
1998 return bfd_reloc_dangerous;
1999
2000 /* The CALL needs to be relocated. Continue below for that part. */
2001 address += 3;
2002 self_address += 3;
2003 howto = &elf_howto_table[(unsigned) R_XTENSA_SLOT0_OP ];
2004 }
2005 break;
2006
2007 case R_XTENSA_32:
2008 {
2009 bfd_vma x;
2010 x = bfd_get_32 (abfd, contents + address);
2011 x = x + relocation;
2012 bfd_put_32 (abfd, x, contents + address);
2013 }
2014 return bfd_reloc_ok;
2015
2016 case R_XTENSA_32_PCREL:
2017 bfd_put_32 (abfd, relocation - self_address, contents + address);
2018 return bfd_reloc_ok;
2019
2020 case R_XTENSA_PLT:
2021 case R_XTENSA_TLSDESC_FN:
2022 case R_XTENSA_TLSDESC_ARG:
2023 case R_XTENSA_TLS_DTPOFF:
2024 case R_XTENSA_TLS_TPOFF:
2025 bfd_put_32 (abfd, relocation, contents + address);
2026 return bfd_reloc_ok;
2027 }
2028
2029 /* Only instruction slot-specific relocations handled below.... */
2030 slot = get_relocation_slot (howto->type);
2031 if (slot == XTENSA_UNDEFINED)
2032 {
2033 *error_message = "unexpected relocation";
2034 return bfd_reloc_dangerous;
2035 }
2036
2037 /* Read the instruction into a buffer and decode the opcode. */
2038 xtensa_insnbuf_from_chars (isa, ibuff, contents + address,
2039 input_size - address);
2040 fmt = xtensa_format_decode (isa, ibuff);
2041 if (fmt == XTENSA_UNDEFINED)
2042 {
2043 *error_message = "cannot decode instruction format";
2044 return bfd_reloc_dangerous;
2045 }
2046
2047 xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
2048
2049 opcode = xtensa_opcode_decode (isa, fmt, slot, sbuff);
2050 if (opcode == XTENSA_UNDEFINED)
2051 {
2052 *error_message = "cannot decode instruction opcode";
2053 return bfd_reloc_dangerous;
2054 }
2055
2056 /* Check for opcode-specific "alternate" relocations. */
2057 if (is_alt_relocation (howto->type))
2058 {
2059 if (opcode == get_l32r_opcode ())
2060 {
2061 /* Handle the special-case of non-PC-relative L32R instructions. */
2062 bfd *output_bfd = input_section->output_section->owner;
2063 asection *lit4_sec = bfd_get_section_by_name (output_bfd, ".lit4");
2064 if (!lit4_sec)
2065 {
2066 *error_message = "relocation references missing .lit4 section";
2067 return bfd_reloc_dangerous;
2068 }
2069 self_address = ((lit4_sec->vma & ~0xfff)
2070 + 0x40000 - 3); /* -3 to compensate for do_reloc */
2071 newval = relocation;
2072 opnd = 1;
2073 }
2074 else if (opcode == get_const16_opcode ())
2075 {
2076 /* ALT used for high 16 bits. */
2077 newval = relocation >> 16;
2078 opnd = 1;
2079 }
2080 else
2081 {
2082 /* No other "alternate" relocations currently defined. */
2083 *error_message = "unexpected relocation";
2084 return bfd_reloc_dangerous;
2085 }
2086 }
2087 else /* Not an "alternate" relocation.... */
2088 {
2089 if (opcode == get_const16_opcode ())
2090 {
2091 newval = relocation & 0xffff;
2092 opnd = 1;
2093 }
2094 else
2095 {
2096 /* ...normal PC-relative relocation.... */
2097
2098 /* Determine which operand is being relocated. */
2099 opnd = get_relocation_opnd (opcode, howto->type);
2100 if (opnd == XTENSA_UNDEFINED)
2101 {
2102 *error_message = "unexpected relocation";
2103 return bfd_reloc_dangerous;
2104 }
2105
2106 if (!howto->pc_relative)
2107 {
2108 *error_message = "expected PC-relative relocation";
2109 return bfd_reloc_dangerous;
2110 }
2111
2112 newval = relocation;
2113 }
2114 }
2115
2116 /* Apply the relocation. */
2117 if (xtensa_operand_do_reloc (isa, opcode, opnd, &newval, self_address)
2118 || xtensa_operand_encode (isa, opcode, opnd, &newval)
2119 || xtensa_operand_set_field (isa, opcode, opnd, fmt, slot,
2120 sbuff, newval))
2121 {
2122 const char *opname = xtensa_opcode_name (isa, opcode);
2123 const char *msg;
2124
2125 msg = "cannot encode";
2126 if (is_direct_call_opcode (opcode))
2127 {
2128 if ((relocation & 0x3) != 0)
2129 msg = "misaligned call target";
2130 else
2131 msg = "call target out of range";
2132 }
2133 else if (opcode == get_l32r_opcode ())
2134 {
2135 if ((relocation & 0x3) != 0)
2136 msg = "misaligned literal target";
2137 else if (is_alt_relocation (howto->type))
2138 msg = "literal target out of range (too many literals)";
2139 else if (self_address > relocation)
2140 msg = "literal target out of range (try using text-section-literals)";
2141 else
2142 msg = "literal placed after use";
2143 }
2144
2145 *error_message = vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
2146 return bfd_reloc_dangerous;
2147 }
2148
2149 /* Check for calls across 1GB boundaries. */
2150 if (is_direct_call_opcode (opcode)
2151 && is_windowed_call_opcode (opcode))
2152 {
2153 if ((self_address >> CALL_SEGMENT_BITS)
2154 != (relocation >> CALL_SEGMENT_BITS))
2155 {
2156 *error_message =
2157 "windowed call crosses 1GB boundary; return may fail";
2158 return bfd_reloc_dangerous;
2159 }
2160 }
2161
2162 /* Write the modified instruction back out of the buffer. */
2163 xtensa_format_set_slot (isa, fmt, slot, ibuff, sbuff);
2164 xtensa_insnbuf_to_chars (isa, ibuff, contents + address,
2165 input_size - address);
2166 return bfd_reloc_ok;
2167 }
2168
2169
2170 static char *
2171 vsprint_msg (const char *origmsg, const char *fmt, int arglen, ...)
2172 {
2173 /* To reduce the size of the memory leak,
2174 we only use a single message buffer. */
2175 static bfd_size_type alloc_size = 0;
2176 static char *message = NULL;
2177 bfd_size_type orig_len, len = 0;
2178 bfd_boolean is_append;
2179 va_list ap;
2180
2181 va_start (ap, arglen);
2182
2183 is_append = (origmsg == message);
2184
2185 orig_len = strlen (origmsg);
2186 len = orig_len + strlen (fmt) + arglen + 20;
2187 if (len > alloc_size)
2188 {
2189 message = (char *) bfd_realloc_or_free (message, len);
2190 alloc_size = len;
2191 }
2192 if (message != NULL)
2193 {
2194 if (!is_append)
2195 memcpy (message, origmsg, orig_len);
2196 vsprintf (message + orig_len, fmt, ap);
2197 }
2198 va_end (ap);
2199 return message;
2200 }
2201
2202
2203 /* This function is registered as the "special_function" in the
2204 Xtensa howto for handling simplify operations.
2205 bfd_perform_relocation / bfd_install_relocation use it to
2206 perform (install) the specified relocation. Since this replaces the code
2207 in bfd_perform_relocation, it is basically an Xtensa-specific,
2208 stripped-down version of bfd_perform_relocation. */
2209
2210 static bfd_reloc_status_type
2211 bfd_elf_xtensa_reloc (bfd *abfd,
2212 arelent *reloc_entry,
2213 asymbol *symbol,
2214 void *data,
2215 asection *input_section,
2216 bfd *output_bfd,
2217 char **error_message)
2218 {
2219 bfd_vma relocation;
2220 bfd_reloc_status_type flag;
2221 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2222 bfd_vma output_base = 0;
2223 reloc_howto_type *howto = reloc_entry->howto;
2224 asection *reloc_target_output_section;
2225 bfd_boolean is_weak_undef;
2226
2227 if (!xtensa_default_isa)
2228 xtensa_default_isa = xtensa_isa_init (0, 0);
2229
2230 /* ELF relocs are against symbols. If we are producing relocatable
2231 output, and the reloc is against an external symbol, the resulting
2232 reloc will also be against the same symbol. In such a case, we
2233 don't want to change anything about the way the reloc is handled,
2234 since it will all be done at final link time. This test is similar
2235 to what bfd_elf_generic_reloc does except that it lets relocs with
2236 howto->partial_inplace go through even if the addend is non-zero.
2237 (The real problem is that partial_inplace is set for XTENSA_32
2238 relocs to begin with, but that's a long story and there's little we
2239 can do about it now....) */
2240
2241 if (output_bfd && (symbol->flags & BSF_SECTION_SYM) == 0)
2242 {
2243 reloc_entry->address += input_section->output_offset;
2244 return bfd_reloc_ok;
2245 }
2246
2247 /* Is the address of the relocation really within the section? */
2248 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2249 return bfd_reloc_outofrange;
2250
2251 /* Work out which section the relocation is targeted at and the
2252 initial relocation command value. */
2253
2254 /* Get symbol value. (Common symbols are special.) */
2255 if (bfd_is_com_section (symbol->section))
2256 relocation = 0;
2257 else
2258 relocation = symbol->value;
2259
2260 reloc_target_output_section = symbol->section->output_section;
2261
2262 /* Convert input-section-relative symbol value to absolute. */
2263 if ((output_bfd && !howto->partial_inplace)
2264 || reloc_target_output_section == NULL)
2265 output_base = 0;
2266 else
2267 output_base = reloc_target_output_section->vma;
2268
2269 relocation += output_base + symbol->section->output_offset;
2270
2271 /* Add in supplied addend. */
2272 relocation += reloc_entry->addend;
2273
2274 /* Here the variable relocation holds the final address of the
2275 symbol we are relocating against, plus any addend. */
2276 if (output_bfd)
2277 {
2278 if (!howto->partial_inplace)
2279 {
2280 /* This is a partial relocation, and we want to apply the relocation
2281 to the reloc entry rather than the raw data. Everything except
2282 relocations against section symbols has already been handled
2283 above. */
2284
2285 BFD_ASSERT (symbol->flags & BSF_SECTION_SYM);
2286 reloc_entry->addend = relocation;
2287 reloc_entry->address += input_section->output_offset;
2288 return bfd_reloc_ok;
2289 }
2290 else
2291 {
2292 reloc_entry->address += input_section->output_offset;
2293 reloc_entry->addend = 0;
2294 }
2295 }
2296
2297 is_weak_undef = (bfd_is_und_section (symbol->section)
2298 && (symbol->flags & BSF_WEAK) != 0);
2299 flag = elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
2300 (bfd_byte *) data, (bfd_vma) octets,
2301 is_weak_undef, error_message);
2302
2303 if (flag == bfd_reloc_dangerous)
2304 {
2305 /* Add the symbol name to the error message. */
2306 if (! *error_message)
2307 *error_message = "";
2308 *error_message = vsprint_msg (*error_message, ": (%s + 0x%lx)",
2309 strlen (symbol->name) + 17,
2310 symbol->name,
2311 (unsigned long) reloc_entry->addend);
2312 }
2313
2314 return flag;
2315 }
2316
2317
2318 /* Set up an entry in the procedure linkage table. */
2319
2320 static bfd_vma
2321 elf_xtensa_create_plt_entry (struct bfd_link_info *info,
2322 bfd *output_bfd,
2323 unsigned reloc_index)
2324 {
2325 asection *splt, *sgotplt;
2326 bfd_vma plt_base, got_base;
2327 bfd_vma code_offset, lit_offset, abi_offset;
2328 int chunk;
2329
2330 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
2331 splt = elf_xtensa_get_plt_section (info, chunk);
2332 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
2333 BFD_ASSERT (splt != NULL && sgotplt != NULL);
2334
2335 plt_base = splt->output_section->vma + splt->output_offset;
2336 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
2337
2338 lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
2339 code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
2340
2341 /* Fill in the literal entry. This is the offset of the dynamic
2342 relocation entry. */
2343 bfd_put_32 (output_bfd, reloc_index * sizeof (Elf32_External_Rela),
2344 sgotplt->contents + lit_offset);
2345
2346 /* Fill in the entry in the procedure linkage table. */
2347 memcpy (splt->contents + code_offset,
2348 (bfd_big_endian (output_bfd)
2349 ? elf_xtensa_be_plt_entry
2350 : elf_xtensa_le_plt_entry),
2351 PLT_ENTRY_SIZE);
2352 abi_offset = XSHAL_ABI == XTHAL_ABI_WINDOWED ? 3 : 0;
2353 bfd_put_16 (output_bfd, l32r_offset (got_base + 0,
2354 plt_base + code_offset + abi_offset),
2355 splt->contents + code_offset + abi_offset + 1);
2356 bfd_put_16 (output_bfd, l32r_offset (got_base + 4,
2357 plt_base + code_offset + abi_offset + 3),
2358 splt->contents + code_offset + abi_offset + 4);
2359 bfd_put_16 (output_bfd, l32r_offset (got_base + lit_offset,
2360 plt_base + code_offset + abi_offset + 6),
2361 splt->contents + code_offset + abi_offset + 7);
2362
2363 return plt_base + code_offset;
2364 }
2365
2366
2367 static bfd_boolean get_indirect_call_dest_reg (xtensa_opcode, unsigned *);
2368
2369 static bfd_boolean
2370 replace_tls_insn (Elf_Internal_Rela *rel,
2371 bfd *abfd,
2372 asection *input_section,
2373 bfd_byte *contents,
2374 bfd_boolean is_ld_model,
2375 char **error_message)
2376 {
2377 static xtensa_insnbuf ibuff = NULL;
2378 static xtensa_insnbuf sbuff = NULL;
2379 xtensa_isa isa = xtensa_default_isa;
2380 xtensa_format fmt;
2381 xtensa_opcode old_op, new_op;
2382 bfd_size_type input_size;
2383 int r_type;
2384 unsigned dest_reg, src_reg;
2385
2386 if (ibuff == NULL)
2387 {
2388 ibuff = xtensa_insnbuf_alloc (isa);
2389 sbuff = xtensa_insnbuf_alloc (isa);
2390 }
2391
2392 input_size = bfd_get_section_limit (abfd, input_section);
2393
2394 /* Read the instruction into a buffer and decode the opcode. */
2395 xtensa_insnbuf_from_chars (isa, ibuff, contents + rel->r_offset,
2396 input_size - rel->r_offset);
2397 fmt = xtensa_format_decode (isa, ibuff);
2398 if (fmt == XTENSA_UNDEFINED)
2399 {
2400 *error_message = "cannot decode instruction format";
2401 return FALSE;
2402 }
2403
2404 BFD_ASSERT (xtensa_format_num_slots (isa, fmt) == 1);
2405 xtensa_format_get_slot (isa, fmt, 0, ibuff, sbuff);
2406
2407 old_op = xtensa_opcode_decode (isa, fmt, 0, sbuff);
2408 if (old_op == XTENSA_UNDEFINED)
2409 {
2410 *error_message = "cannot decode instruction opcode";
2411 return FALSE;
2412 }
2413
2414 r_type = ELF32_R_TYPE (rel->r_info);
2415 switch (r_type)
2416 {
2417 case R_XTENSA_TLS_FUNC:
2418 case R_XTENSA_TLS_ARG:
2419 if (old_op != get_l32r_opcode ()
2420 || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
2421 sbuff, &dest_reg) != 0)
2422 {
2423 *error_message = "cannot extract L32R destination for TLS access";
2424 return FALSE;
2425 }
2426 break;
2427
2428 case R_XTENSA_TLS_CALL:
2429 if (! get_indirect_call_dest_reg (old_op, &dest_reg)
2430 || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
2431 sbuff, &src_reg) != 0)
2432 {
2433 *error_message = "cannot extract CALLXn operands for TLS access";
2434 return FALSE;
2435 }
2436 break;
2437
2438 default:
2439 abort ();
2440 }
2441
2442 if (is_ld_model)
2443 {
2444 switch (r_type)
2445 {
2446 case R_XTENSA_TLS_FUNC:
2447 case R_XTENSA_TLS_ARG:
2448 /* Change the instruction to a NOP (or "OR a1, a1, a1" for older
2449 versions of Xtensa). */
2450 new_op = xtensa_opcode_lookup (isa, "nop");
2451 if (new_op == XTENSA_UNDEFINED)
2452 {
2453 new_op = xtensa_opcode_lookup (isa, "or");
2454 if (new_op == XTENSA_UNDEFINED
2455 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2456 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2457 sbuff, 1) != 0
2458 || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
2459 sbuff, 1) != 0
2460 || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
2461 sbuff, 1) != 0)
2462 {
2463 *error_message = "cannot encode OR for TLS access";
2464 return FALSE;
2465 }
2466 }
2467 else
2468 {
2469 if (xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0)
2470 {
2471 *error_message = "cannot encode NOP for TLS access";
2472 return FALSE;
2473 }
2474 }
2475 break;
2476
2477 case R_XTENSA_TLS_CALL:
2478 /* Read THREADPTR into the CALLX's return value register. */
2479 new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
2480 if (new_op == XTENSA_UNDEFINED
2481 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2482 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2483 sbuff, dest_reg + 2) != 0)
2484 {
2485 *error_message = "cannot encode RUR.THREADPTR for TLS access";
2486 return FALSE;
2487 }
2488 break;
2489 }
2490 }
2491 else
2492 {
2493 switch (r_type)
2494 {
2495 case R_XTENSA_TLS_FUNC:
2496 new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
2497 if (new_op == XTENSA_UNDEFINED
2498 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2499 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2500 sbuff, dest_reg) != 0)
2501 {
2502 *error_message = "cannot encode RUR.THREADPTR for TLS access";
2503 return FALSE;
2504 }
2505 break;
2506
2507 case R_XTENSA_TLS_ARG:
2508 /* Nothing to do. Keep the original L32R instruction. */
2509 return TRUE;
2510
2511 case R_XTENSA_TLS_CALL:
2512 /* Add the CALLX's src register (holding the THREADPTR value)
2513 to the first argument register (holding the offset) and put
2514 the result in the CALLX's return value register. */
2515 new_op = xtensa_opcode_lookup (isa, "add");
2516 if (new_op == XTENSA_UNDEFINED
2517 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2518 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2519 sbuff, dest_reg + 2) != 0
2520 || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
2521 sbuff, dest_reg + 2) != 0
2522 || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
2523 sbuff, src_reg) != 0)
2524 {
2525 *error_message = "cannot encode ADD for TLS access";
2526 return FALSE;
2527 }
2528 break;
2529 }
2530 }
2531
2532 xtensa_format_set_slot (isa, fmt, 0, ibuff, sbuff);
2533 xtensa_insnbuf_to_chars (isa, ibuff, contents + rel->r_offset,
2534 input_size - rel->r_offset);
2535
2536 return TRUE;
2537 }
2538
2539
2540 #define IS_XTENSA_TLS_RELOC(R_TYPE) \
2541 ((R_TYPE) == R_XTENSA_TLSDESC_FN \
2542 || (R_TYPE) == R_XTENSA_TLSDESC_ARG \
2543 || (R_TYPE) == R_XTENSA_TLS_DTPOFF \
2544 || (R_TYPE) == R_XTENSA_TLS_TPOFF \
2545 || (R_TYPE) == R_XTENSA_TLS_FUNC \
2546 || (R_TYPE) == R_XTENSA_TLS_ARG \
2547 || (R_TYPE) == R_XTENSA_TLS_CALL)
2548
2549 /* Relocate an Xtensa ELF section. This is invoked by the linker for
2550 both relocatable and final links. */
2551
2552 static bfd_boolean
2553 elf_xtensa_relocate_section (bfd *output_bfd,
2554 struct bfd_link_info *info,
2555 bfd *input_bfd,
2556 asection *input_section,
2557 bfd_byte *contents,
2558 Elf_Internal_Rela *relocs,
2559 Elf_Internal_Sym *local_syms,
2560 asection **local_sections)
2561 {
2562 struct elf_xtensa_link_hash_table *htab;
2563 Elf_Internal_Shdr *symtab_hdr;
2564 Elf_Internal_Rela *rel;
2565 Elf_Internal_Rela *relend;
2566 struct elf_link_hash_entry **sym_hashes;
2567 property_table_entry *lit_table = 0;
2568 int ltblsize = 0;
2569 char *local_got_tls_types;
2570 char *error_message = NULL;
2571 bfd_size_type input_size;
2572 int tls_type;
2573
2574 if (!xtensa_default_isa)
2575 xtensa_default_isa = xtensa_isa_init (0, 0);
2576
2577 BFD_ASSERT (is_xtensa_elf (input_bfd));
2578
2579 htab = elf_xtensa_hash_table (info);
2580 if (htab == NULL)
2581 return FALSE;
2582
2583 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2584 sym_hashes = elf_sym_hashes (input_bfd);
2585 local_got_tls_types = elf_xtensa_local_got_tls_type (input_bfd);
2586
2587 if (elf_hash_table (info)->dynamic_sections_created)
2588 {
2589 ltblsize = xtensa_read_table_entries (input_bfd, input_section,
2590 &lit_table, XTENSA_LIT_SEC_NAME,
2591 TRUE);
2592 if (ltblsize < 0)
2593 return FALSE;
2594 }
2595
2596 input_size = bfd_get_section_limit (input_bfd, input_section);
2597
2598 rel = relocs;
2599 relend = relocs + input_section->reloc_count;
2600 for (; rel < relend; rel++)
2601 {
2602 int r_type;
2603 reloc_howto_type *howto;
2604 unsigned long r_symndx;
2605 struct elf_link_hash_entry *h;
2606 Elf_Internal_Sym *sym;
2607 char sym_type;
2608 const char *name;
2609 asection *sec;
2610 bfd_vma relocation;
2611 bfd_reloc_status_type r;
2612 bfd_boolean is_weak_undef;
2613 bfd_boolean unresolved_reloc;
2614 bfd_boolean warned;
2615 bfd_boolean dynamic_symbol;
2616
2617 r_type = ELF32_R_TYPE (rel->r_info);
2618 if (r_type == (int) R_XTENSA_GNU_VTINHERIT
2619 || r_type == (int) R_XTENSA_GNU_VTENTRY)
2620 continue;
2621
2622 if (r_type < 0 || r_type >= (int) R_XTENSA_max)
2623 {
2624 bfd_set_error (bfd_error_bad_value);
2625 return FALSE;
2626 }
2627 howto = &elf_howto_table[r_type];
2628
2629 r_symndx = ELF32_R_SYM (rel->r_info);
2630
2631 h = NULL;
2632 sym = NULL;
2633 sec = NULL;
2634 is_weak_undef = FALSE;
2635 unresolved_reloc = FALSE;
2636 warned = FALSE;
2637
2638 if (howto->partial_inplace && !bfd_link_relocatable (info))
2639 {
2640 /* Because R_XTENSA_32 was made partial_inplace to fix some
2641 problems with DWARF info in partial links, there may be
2642 an addend stored in the contents. Take it out of there
2643 and move it back into the addend field of the reloc. */
2644 rel->r_addend += bfd_get_32 (input_bfd, contents + rel->r_offset);
2645 bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
2646 }
2647
2648 if (r_symndx < symtab_hdr->sh_info)
2649 {
2650 sym = local_syms + r_symndx;
2651 sym_type = ELF32_ST_TYPE (sym->st_info);
2652 sec = local_sections[r_symndx];
2653 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2654 }
2655 else
2656 {
2657 bfd_boolean ignored;
2658
2659 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2660 r_symndx, symtab_hdr, sym_hashes,
2661 h, sec, relocation,
2662 unresolved_reloc, warned, ignored);
2663
2664 if (relocation == 0
2665 && !unresolved_reloc
2666 && h->root.type == bfd_link_hash_undefweak)
2667 is_weak_undef = TRUE;
2668
2669 sym_type = h->type;
2670 }
2671
2672 if (sec != NULL && discarded_section (sec))
2673 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2674 rel, 1, relend, howto, 0, contents);
2675
2676 if (bfd_link_relocatable (info))
2677 {
2678 bfd_vma dest_addr;
2679 asection * sym_sec = get_elf_r_symndx_section (input_bfd, r_symndx);
2680
2681 /* This is a relocatable link.
2682 1) If the reloc is against a section symbol, adjust
2683 according to the output section.
2684 2) If there is a new target for this relocation,
2685 the new target will be in the same output section.
2686 We adjust the relocation by the output section
2687 difference. */
2688
2689 if (relaxing_section)
2690 {
2691 /* Check if this references a section in another input file. */
2692 if (!do_fix_for_relocatable_link (rel, input_bfd, input_section,
2693 contents))
2694 return FALSE;
2695 }
2696
2697 dest_addr = sym_sec->output_section->vma + sym_sec->output_offset
2698 + get_elf_r_symndx_offset (input_bfd, r_symndx) + rel->r_addend;
2699
2700 if (r_type == R_XTENSA_ASM_SIMPLIFY)
2701 {
2702 error_message = NULL;
2703 /* Convert ASM_SIMPLIFY into the simpler relocation
2704 so that they never escape a relaxing link. */
2705 r = contract_asm_expansion (contents, input_size, rel,
2706 &error_message);
2707 if (r != bfd_reloc_ok)
2708 (*info->callbacks->reloc_dangerous)
2709 (info, error_message,
2710 input_bfd, input_section, rel->r_offset);
2711
2712 r_type = ELF32_R_TYPE (rel->r_info);
2713 }
2714
2715 /* This is a relocatable link, so we don't have to change
2716 anything unless the reloc is against a section symbol,
2717 in which case we have to adjust according to where the
2718 section symbol winds up in the output section. */
2719 if (r_symndx < symtab_hdr->sh_info)
2720 {
2721 sym = local_syms + r_symndx;
2722 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2723 {
2724 sec = local_sections[r_symndx];
2725 rel->r_addend += sec->output_offset + sym->st_value;
2726 }
2727 }
2728
2729 /* If there is an addend with a partial_inplace howto,
2730 then move the addend to the contents. This is a hack
2731 to work around problems with DWARF in relocatable links
2732 with some previous version of BFD. Now we can't easily get
2733 rid of the hack without breaking backward compatibility.... */
2734 r = bfd_reloc_ok;
2735 howto = &elf_howto_table[r_type];
2736 if (howto->partial_inplace && rel->r_addend)
2737 {
2738 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2739 rel->r_addend, contents,
2740 rel->r_offset, FALSE,
2741 &error_message);
2742 rel->r_addend = 0;
2743 }
2744 else
2745 {
2746 /* Put the correct bits in the target instruction, even
2747 though the relocation will still be present in the output
2748 file. This makes disassembly clearer, as well as
2749 allowing loadable kernel modules to work without needing
2750 relocations on anything other than calls and l32r's. */
2751
2752 /* If it is not in the same section, there is nothing we can do. */
2753 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP &&
2754 sym_sec->output_section == input_section->output_section)
2755 {
2756 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2757 dest_addr, contents,
2758 rel->r_offset, FALSE,
2759 &error_message);
2760 }
2761 }
2762 if (r != bfd_reloc_ok)
2763 (*info->callbacks->reloc_dangerous)
2764 (info, error_message,
2765 input_bfd, input_section, rel->r_offset);
2766
2767 /* Done with work for relocatable link; continue with next reloc. */
2768 continue;
2769 }
2770
2771 /* This is a final link. */
2772
2773 if (relaxing_section)
2774 {
2775 /* Check if this references a section in another input file. */
2776 do_fix_for_final_link (rel, input_bfd, input_section, contents,
2777 &relocation);
2778 }
2779
2780 /* Sanity check the address. */
2781 if (rel->r_offset >= input_size
2782 && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
2783 {
2784 _bfd_error_handler
2785 /* xgettext:c-format */
2786 (_("%B(%A+0x%lx): relocation offset out of range (size=0x%x)"),
2787 input_bfd, input_section, rel->r_offset, input_size);
2788 bfd_set_error (bfd_error_bad_value);
2789 return FALSE;
2790 }
2791
2792 if (h != NULL)
2793 name = h->root.root.string;
2794 else
2795 {
2796 name = (bfd_elf_string_from_elf_section
2797 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2798 if (name == NULL || *name == '\0')
2799 name = bfd_section_name (input_bfd, sec);
2800 }
2801
2802 if (r_symndx != STN_UNDEF
2803 && r_type != R_XTENSA_NONE
2804 && (h == NULL
2805 || h->root.type == bfd_link_hash_defined
2806 || h->root.type == bfd_link_hash_defweak)
2807 && IS_XTENSA_TLS_RELOC (r_type) != (sym_type == STT_TLS))
2808 {
2809 _bfd_error_handler
2810 ((sym_type == STT_TLS
2811 /* xgettext:c-format */
2812 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
2813 /* xgettext:c-format */
2814 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
2815 input_bfd,
2816 input_section,
2817 (long) rel->r_offset,
2818 howto->name,
2819 name);
2820 }
2821
2822 dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
2823
2824 tls_type = GOT_UNKNOWN;
2825 if (h)
2826 tls_type = elf_xtensa_hash_entry (h)->tls_type;
2827 else if (local_got_tls_types)
2828 tls_type = local_got_tls_types [r_symndx];
2829
2830 switch (r_type)
2831 {
2832 case R_XTENSA_32:
2833 case R_XTENSA_PLT:
2834 if (elf_hash_table (info)->dynamic_sections_created
2835 && (input_section->flags & SEC_ALLOC) != 0
2836 && (dynamic_symbol || bfd_link_pic (info)))
2837 {
2838 Elf_Internal_Rela outrel;
2839 bfd_byte *loc;
2840 asection *srel;
2841
2842 if (dynamic_symbol && r_type == R_XTENSA_PLT)
2843 srel = htab->srelplt;
2844 else
2845 srel = htab->srelgot;
2846
2847 BFD_ASSERT (srel != NULL);
2848
2849 outrel.r_offset =
2850 _bfd_elf_section_offset (output_bfd, info,
2851 input_section, rel->r_offset);
2852
2853 if ((outrel.r_offset | 1) == (bfd_vma) -1)
2854 memset (&outrel, 0, sizeof outrel);
2855 else
2856 {
2857 outrel.r_offset += (input_section->output_section->vma
2858 + input_section->output_offset);
2859
2860 /* Complain if the relocation is in a read-only section
2861 and not in a literal pool. */
2862 if ((input_section->flags & SEC_READONLY) != 0
2863 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
2864 outrel.r_offset))
2865 {
2866 error_message =
2867 _("dynamic relocation in read-only section");
2868 (*info->callbacks->reloc_dangerous)
2869 (info, error_message,
2870 input_bfd, input_section, rel->r_offset);
2871 }
2872
2873 if (dynamic_symbol)
2874 {
2875 outrel.r_addend = rel->r_addend;
2876 rel->r_addend = 0;
2877
2878 if (r_type == R_XTENSA_32)
2879 {
2880 outrel.r_info =
2881 ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
2882 relocation = 0;
2883 }
2884 else /* r_type == R_XTENSA_PLT */
2885 {
2886 outrel.r_info =
2887 ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
2888
2889 /* Create the PLT entry and set the initial
2890 contents of the literal entry to the address of
2891 the PLT entry. */
2892 relocation =
2893 elf_xtensa_create_plt_entry (info, output_bfd,
2894 srel->reloc_count);
2895 }
2896 unresolved_reloc = FALSE;
2897 }
2898 else
2899 {
2900 /* Generate a RELATIVE relocation. */
2901 outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
2902 outrel.r_addend = 0;
2903 }
2904 }
2905
2906 loc = (srel->contents
2907 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2908 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2909 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
2910 <= srel->size);
2911 }
2912 else if (r_type == R_XTENSA_ASM_EXPAND && dynamic_symbol)
2913 {
2914 /* This should only happen for non-PIC code, which is not
2915 supposed to be used on systems with dynamic linking.
2916 Just ignore these relocations. */
2917 continue;
2918 }
2919 break;
2920
2921 case R_XTENSA_TLS_TPOFF:
2922 /* Switch to LE model for local symbols in an executable. */
2923 if (! bfd_link_pic (info) && ! dynamic_symbol)
2924 {
2925 relocation = tpoff (info, relocation);
2926 break;
2927 }
2928 /* fall through */
2929
2930 case R_XTENSA_TLSDESC_FN:
2931 case R_XTENSA_TLSDESC_ARG:
2932 {
2933 if (r_type == R_XTENSA_TLSDESC_FN)
2934 {
2935 if (! bfd_link_pic (info) || (tls_type & GOT_TLS_IE) != 0)
2936 r_type = R_XTENSA_NONE;
2937 }
2938 else if (r_type == R_XTENSA_TLSDESC_ARG)
2939 {
2940 if (bfd_link_pic (info))
2941 {
2942 if ((tls_type & GOT_TLS_IE) != 0)
2943 r_type = R_XTENSA_TLS_TPOFF;
2944 }
2945 else
2946 {
2947 r_type = R_XTENSA_TLS_TPOFF;
2948 if (! dynamic_symbol)
2949 {
2950 relocation = tpoff (info, relocation);
2951 break;
2952 }
2953 }
2954 }
2955
2956 if (r_type == R_XTENSA_NONE)
2957 /* Nothing to do here; skip to the next reloc. */
2958 continue;
2959
2960 if (! elf_hash_table (info)->dynamic_sections_created)
2961 {
2962 error_message =
2963 _("TLS relocation invalid without dynamic sections");
2964 (*info->callbacks->reloc_dangerous)
2965 (info, error_message,
2966 input_bfd, input_section, rel->r_offset);
2967 }
2968 else
2969 {
2970 Elf_Internal_Rela outrel;
2971 bfd_byte *loc;
2972 asection *srel = htab->srelgot;
2973 int indx;
2974
2975 outrel.r_offset = (input_section->output_section->vma
2976 + input_section->output_offset
2977 + rel->r_offset);
2978
2979 /* Complain if the relocation is in a read-only section
2980 and not in a literal pool. */
2981 if ((input_section->flags & SEC_READONLY) != 0
2982 && ! elf_xtensa_in_literal_pool (lit_table, ltblsize,
2983 outrel.r_offset))
2984 {
2985 error_message =
2986 _("dynamic relocation in read-only section");
2987 (*info->callbacks->reloc_dangerous)
2988 (info, error_message,
2989 input_bfd, input_section, rel->r_offset);
2990 }
2991
2992 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2993 if (indx == 0)
2994 outrel.r_addend = relocation - dtpoff_base (info);
2995 else
2996 outrel.r_addend = 0;
2997 rel->r_addend = 0;
2998
2999 outrel.r_info = ELF32_R_INFO (indx, r_type);
3000 relocation = 0;
3001 unresolved_reloc = FALSE;
3002
3003 BFD_ASSERT (srel);
3004 loc = (srel->contents
3005 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
3006 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3007 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
3008 <= srel->size);
3009 }
3010 }
3011 break;
3012
3013 case R_XTENSA_TLS_DTPOFF:
3014 if (! bfd_link_pic (info))
3015 /* Switch from LD model to LE model. */
3016 relocation = tpoff (info, relocation);
3017 else
3018 relocation -= dtpoff_base (info);
3019 break;
3020
3021 case R_XTENSA_TLS_FUNC:
3022 case R_XTENSA_TLS_ARG:
3023 case R_XTENSA_TLS_CALL:
3024 /* Check if optimizing to IE or LE model. */
3025 if ((tls_type & GOT_TLS_IE) != 0)
3026 {
3027 bfd_boolean is_ld_model =
3028 (h && elf_xtensa_hash_entry (h) == htab->tlsbase);
3029 if (! replace_tls_insn (rel, input_bfd, input_section, contents,
3030 is_ld_model, &error_message))
3031 (*info->callbacks->reloc_dangerous)
3032 (info, error_message,
3033 input_bfd, input_section, rel->r_offset);
3034
3035 if (r_type != R_XTENSA_TLS_ARG || is_ld_model)
3036 {
3037 /* Skip subsequent relocations on the same instruction. */
3038 while (rel + 1 < relend && rel[1].r_offset == rel->r_offset)
3039 rel++;
3040 }
3041 }
3042 continue;
3043
3044 default:
3045 if (elf_hash_table (info)->dynamic_sections_created
3046 && dynamic_symbol && (is_operand_relocation (r_type)
3047 || r_type == R_XTENSA_32_PCREL))
3048 {
3049 error_message =
3050 vsprint_msg ("invalid relocation for dynamic symbol", ": %s",
3051 strlen (name) + 2, name);
3052 (*info->callbacks->reloc_dangerous)
3053 (info, error_message, input_bfd, input_section, rel->r_offset);
3054 continue;
3055 }
3056 break;
3057 }
3058
3059 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3060 because such sections are not SEC_ALLOC and thus ld.so will
3061 not process them. */
3062 if (unresolved_reloc
3063 && !((input_section->flags & SEC_DEBUGGING) != 0
3064 && h->def_dynamic)
3065 && _bfd_elf_section_offset (output_bfd, info, input_section,
3066 rel->r_offset) != (bfd_vma) -1)
3067 {
3068 _bfd_error_handler
3069 /* xgettext:c-format */
3070 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3071 input_bfd,
3072 input_section,
3073 (long) rel->r_offset,
3074 howto->name,
3075 name);
3076 return FALSE;
3077 }
3078
3079 /* TLS optimizations may have changed r_type; update "howto". */
3080 howto = &elf_howto_table[r_type];
3081
3082 /* There's no point in calling bfd_perform_relocation here.
3083 Just go directly to our "special function". */
3084 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
3085 relocation + rel->r_addend,
3086 contents, rel->r_offset, is_weak_undef,
3087 &error_message);
3088
3089 if (r != bfd_reloc_ok && !warned)
3090 {
3091 BFD_ASSERT (r == bfd_reloc_dangerous || r == bfd_reloc_other);
3092 BFD_ASSERT (error_message != NULL);
3093
3094 if (rel->r_addend == 0)
3095 error_message = vsprint_msg (error_message, ": %s",
3096 strlen (name) + 2, name);
3097 else
3098 error_message = vsprint_msg (error_message, ": (%s+0x%x)",
3099 strlen (name) + 22,
3100 name, (int) rel->r_addend);
3101
3102 (*info->callbacks->reloc_dangerous)
3103 (info, error_message, input_bfd, input_section, rel->r_offset);
3104 }
3105 }
3106
3107 if (lit_table)
3108 free (lit_table);
3109
3110 input_section->reloc_done = TRUE;
3111
3112 return TRUE;
3113 }
3114
3115
3116 /* Finish up dynamic symbol handling. There's not much to do here since
3117 the PLT and GOT entries are all set up by relocate_section. */
3118
3119 static bfd_boolean
3120 elf_xtensa_finish_dynamic_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
3121 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3122 struct elf_link_hash_entry *h,
3123 Elf_Internal_Sym *sym)
3124 {
3125 if (h->needs_plt && !h->def_regular)
3126 {
3127 /* Mark the symbol as undefined, rather than as defined in
3128 the .plt section. Leave the value alone. */
3129 sym->st_shndx = SHN_UNDEF;
3130 /* If the symbol is weak, we do need to clear the value.
3131 Otherwise, the PLT entry would provide a definition for
3132 the symbol even if the symbol wasn't defined anywhere,
3133 and so the symbol would never be NULL. */
3134 if (!h->ref_regular_nonweak)
3135 sym->st_value = 0;
3136 }
3137
3138 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3139 if (h == elf_hash_table (info)->hdynamic
3140 || h == elf_hash_table (info)->hgot)
3141 sym->st_shndx = SHN_ABS;
3142
3143 return TRUE;
3144 }
3145
3146
3147 /* Combine adjacent literal table entries in the output. Adjacent
3148 entries within each input section may have been removed during
3149 relaxation, but we repeat the process here, even though it's too late
3150 to shrink the output section, because it's important to minimize the
3151 number of literal table entries to reduce the start-up work for the
3152 runtime linker. Returns the number of remaining table entries or -1
3153 on error. */
3154
3155 static int
3156 elf_xtensa_combine_prop_entries (bfd *output_bfd,
3157 asection *sxtlit,
3158 asection *sgotloc)
3159 {
3160 bfd_byte *contents;
3161 property_table_entry *table;
3162 bfd_size_type section_size, sgotloc_size;
3163 bfd_vma offset;
3164 int n, m, num;
3165
3166 section_size = sxtlit->size;
3167 BFD_ASSERT (section_size % 8 == 0);
3168 num = section_size / 8;
3169
3170 sgotloc_size = sgotloc->size;
3171 if (sgotloc_size != section_size)
3172 {
3173 _bfd_error_handler
3174 (_("internal inconsistency in size of .got.loc section"));
3175 return -1;
3176 }
3177
3178 table = bfd_malloc (num * sizeof (property_table_entry));
3179 if (table == 0)
3180 return -1;
3181
3182 /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
3183 propagates to the output section, where it doesn't really apply and
3184 where it breaks the following call to bfd_malloc_and_get_section. */
3185 sxtlit->flags &= ~SEC_IN_MEMORY;
3186
3187 if (!bfd_malloc_and_get_section (output_bfd, sxtlit, &contents))
3188 {
3189 if (contents != 0)
3190 free (contents);
3191 free (table);
3192 return -1;
3193 }
3194
3195 /* There should never be any relocations left at this point, so this
3196 is quite a bit easier than what is done during relaxation. */
3197
3198 /* Copy the raw contents into a property table array and sort it. */
3199 offset = 0;
3200 for (n = 0; n < num; n++)
3201 {
3202 table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
3203 table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
3204 offset += 8;
3205 }
3206 qsort (table, num, sizeof (property_table_entry), property_table_compare);
3207
3208 for (n = 0; n < num; n++)
3209 {
3210 bfd_boolean remove_entry = FALSE;
3211
3212 if (table[n].size == 0)
3213 remove_entry = TRUE;
3214 else if (n > 0
3215 && (table[n-1].address + table[n-1].size == table[n].address))
3216 {
3217 table[n-1].size += table[n].size;
3218 remove_entry = TRUE;
3219 }
3220
3221 if (remove_entry)
3222 {
3223 for (m = n; m < num - 1; m++)
3224 {
3225 table[m].address = table[m+1].address;
3226 table[m].size = table[m+1].size;
3227 }
3228
3229 n--;
3230 num--;
3231 }
3232 }
3233
3234 /* Copy the data back to the raw contents. */
3235 offset = 0;
3236 for (n = 0; n < num; n++)
3237 {
3238 bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
3239 bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
3240 offset += 8;
3241 }
3242
3243 /* Clear the removed bytes. */
3244 if ((bfd_size_type) (num * 8) < section_size)
3245 memset (&contents[num * 8], 0, section_size - num * 8);
3246
3247 if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
3248 section_size))
3249 return -1;
3250
3251 /* Copy the contents to ".got.loc". */
3252 memcpy (sgotloc->contents, contents, section_size);
3253
3254 free (contents);
3255 free (table);
3256 return num;
3257 }
3258
3259
3260 /* Finish up the dynamic sections. */
3261
3262 static bfd_boolean
3263 elf_xtensa_finish_dynamic_sections (bfd *output_bfd,
3264 struct bfd_link_info *info)
3265 {
3266 struct elf_xtensa_link_hash_table *htab;
3267 bfd *dynobj;
3268 asection *sdyn, *srelplt, *sgot, *sxtlit, *sgotloc;
3269 Elf32_External_Dyn *dyncon, *dynconend;
3270 int num_xtlit_entries = 0;
3271
3272 if (! elf_hash_table (info)->dynamic_sections_created)
3273 return TRUE;
3274
3275 htab = elf_xtensa_hash_table (info);
3276 if (htab == NULL)
3277 return FALSE;
3278
3279 dynobj = elf_hash_table (info)->dynobj;
3280 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3281 BFD_ASSERT (sdyn != NULL);
3282
3283 /* Set the first entry in the global offset table to the address of
3284 the dynamic section. */
3285 sgot = htab->sgot;
3286 if (sgot)
3287 {
3288 BFD_ASSERT (sgot->size == 4);
3289 if (sdyn == NULL)
3290 bfd_put_32 (output_bfd, 0, sgot->contents);
3291 else
3292 bfd_put_32 (output_bfd,
3293 sdyn->output_section->vma + sdyn->output_offset,
3294 sgot->contents);
3295 }
3296
3297 srelplt = htab->srelplt;
3298 if (srelplt && srelplt->size != 0)
3299 {
3300 asection *sgotplt, *srelgot, *spltlittbl;
3301 int chunk, plt_chunks, plt_entries;
3302 Elf_Internal_Rela irela;
3303 bfd_byte *loc;
3304 unsigned rtld_reloc;
3305
3306 srelgot = htab->srelgot;
3307 spltlittbl = htab->spltlittbl;
3308 BFD_ASSERT (srelgot != NULL && spltlittbl != NULL);
3309
3310 /* Find the first XTENSA_RTLD relocation. Presumably the rest
3311 of them follow immediately after.... */
3312 for (rtld_reloc = 0; rtld_reloc < srelgot->reloc_count; rtld_reloc++)
3313 {
3314 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
3315 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3316 if (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD)
3317 break;
3318 }
3319 BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
3320
3321 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
3322 plt_chunks =
3323 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
3324
3325 for (chunk = 0; chunk < plt_chunks; chunk++)
3326 {
3327 int chunk_entries = 0;
3328
3329 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
3330 BFD_ASSERT (sgotplt != NULL);
3331
3332 /* Emit special RTLD relocations for the first two entries in
3333 each chunk of the .got.plt section. */
3334
3335 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
3336 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3337 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
3338 irela.r_offset = (sgotplt->output_section->vma
3339 + sgotplt->output_offset);
3340 irela.r_addend = 1; /* tell rtld to set value to resolver function */
3341 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
3342 rtld_reloc += 1;
3343 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
3344
3345 /* Next literal immediately follows the first. */
3346 loc += sizeof (Elf32_External_Rela);
3347 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3348 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
3349 irela.r_offset = (sgotplt->output_section->vma
3350 + sgotplt->output_offset + 4);
3351 /* Tell rtld to set value to object's link map. */
3352 irela.r_addend = 2;
3353 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
3354 rtld_reloc += 1;
3355 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
3356
3357 /* Fill in the literal table. */
3358 if (chunk < plt_chunks - 1)
3359 chunk_entries = PLT_ENTRIES_PER_CHUNK;
3360 else
3361 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
3362
3363 BFD_ASSERT ((unsigned) (chunk + 1) * 8 <= spltlittbl->size);
3364 bfd_put_32 (output_bfd,
3365 sgotplt->output_section->vma + sgotplt->output_offset,
3366 spltlittbl->contents + (chunk * 8) + 0);
3367 bfd_put_32 (output_bfd,
3368 8 + (chunk_entries * 4),
3369 spltlittbl->contents + (chunk * 8) + 4);
3370 }
3371
3372 /* All the dynamic relocations have been emitted at this point.
3373 Make sure the relocation sections are the correct size. */
3374 if (srelgot->size != (sizeof (Elf32_External_Rela)
3375 * srelgot->reloc_count)
3376 || srelplt->size != (sizeof (Elf32_External_Rela)
3377 * srelplt->reloc_count))
3378 abort ();
3379
3380 /* The .xt.lit.plt section has just been modified. This must
3381 happen before the code below which combines adjacent literal
3382 table entries, and the .xt.lit.plt contents have to be forced to
3383 the output here. */
3384 if (! bfd_set_section_contents (output_bfd,
3385 spltlittbl->output_section,
3386 spltlittbl->contents,
3387 spltlittbl->output_offset,
3388 spltlittbl->size))
3389 return FALSE;
3390 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
3391 spltlittbl->flags &= ~SEC_HAS_CONTENTS;
3392 }
3393
3394 /* Combine adjacent literal table entries. */
3395 BFD_ASSERT (! bfd_link_relocatable (info));
3396 sxtlit = bfd_get_section_by_name (output_bfd, ".xt.lit");
3397 sgotloc = htab->sgotloc;
3398 BFD_ASSERT (sgotloc);
3399 if (sxtlit)
3400 {
3401 num_xtlit_entries =
3402 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc);
3403 if (num_xtlit_entries < 0)
3404 return FALSE;
3405 }
3406
3407 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3408 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3409 for (; dyncon < dynconend; dyncon++)
3410 {
3411 Elf_Internal_Dyn dyn;
3412
3413 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3414
3415 switch (dyn.d_tag)
3416 {
3417 default:
3418 break;
3419
3420 case DT_XTENSA_GOT_LOC_SZ:
3421 dyn.d_un.d_val = num_xtlit_entries;
3422 break;
3423
3424 case DT_XTENSA_GOT_LOC_OFF:
3425 dyn.d_un.d_ptr = (htab->sgotloc->output_section->vma
3426 + htab->sgotloc->output_offset);
3427 break;
3428
3429 case DT_PLTGOT:
3430 dyn.d_un.d_ptr = (htab->sgot->output_section->vma
3431 + htab->sgot->output_offset);
3432 break;
3433
3434 case DT_JMPREL:
3435 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
3436 + htab->srelplt->output_offset);
3437 break;
3438
3439 case DT_PLTRELSZ:
3440 dyn.d_un.d_val = htab->srelplt->size;
3441 break;
3442
3443 case DT_RELASZ:
3444 /* Adjust RELASZ to not include JMPREL. This matches what
3445 glibc expects and what is done for several other ELF
3446 targets (e.g., i386, alpha), but the "correct" behavior
3447 seems to be unresolved. Since the linker script arranges
3448 for .rela.plt to follow all other relocation sections, we
3449 don't have to worry about changing the DT_RELA entry. */
3450 if (htab->srelplt)
3451 dyn.d_un.d_val -= htab->srelplt->size;
3452 break;
3453 }
3454
3455 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3456 }
3457
3458 return TRUE;
3459 }
3460
3461 \f
3462 /* Functions for dealing with the e_flags field. */
3463
3464 /* Merge backend specific data from an object file to the output
3465 object file when linking. */
3466
3467 static bfd_boolean
3468 elf_xtensa_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3469 {
3470 bfd *obfd = info->output_bfd;
3471 unsigned out_mach, in_mach;
3472 flagword out_flag, in_flag;
3473
3474 /* Check if we have the same endianness. */
3475 if (!_bfd_generic_verify_endian_match (ibfd, info))
3476 return FALSE;
3477
3478 /* Don't even pretend to support mixed-format linking. */
3479 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3480 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3481 return FALSE;
3482
3483 out_flag = elf_elfheader (obfd)->e_flags;
3484 in_flag = elf_elfheader (ibfd)->e_flags;
3485
3486 out_mach = out_flag & EF_XTENSA_MACH;
3487 in_mach = in_flag & EF_XTENSA_MACH;
3488 if (out_mach != in_mach)
3489 {
3490 _bfd_error_handler
3491 /* xgettext:c-format */
3492 (_("%B: incompatible machine type. Output is 0x%x. Input is 0x%x"),
3493 ibfd, out_mach, in_mach);
3494 bfd_set_error (bfd_error_wrong_format);
3495 return FALSE;
3496 }
3497
3498 if (! elf_flags_init (obfd))
3499 {
3500 elf_flags_init (obfd) = TRUE;
3501 elf_elfheader (obfd)->e_flags = in_flag;
3502
3503 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3504 && bfd_get_arch_info (obfd)->the_default)
3505 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3506 bfd_get_mach (ibfd));
3507
3508 return TRUE;
3509 }
3510
3511 if ((out_flag & EF_XTENSA_XT_INSN) != (in_flag & EF_XTENSA_XT_INSN))
3512 elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
3513
3514 if ((out_flag & EF_XTENSA_XT_LIT) != (in_flag & EF_XTENSA_XT_LIT))
3515 elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
3516
3517 return TRUE;
3518 }
3519
3520
3521 static bfd_boolean
3522 elf_xtensa_set_private_flags (bfd *abfd, flagword flags)
3523 {
3524 BFD_ASSERT (!elf_flags_init (abfd)
3525 || elf_elfheader (abfd)->e_flags == flags);
3526
3527 elf_elfheader (abfd)->e_flags |= flags;
3528 elf_flags_init (abfd) = TRUE;
3529
3530 return TRUE;
3531 }
3532
3533
3534 static bfd_boolean
3535 elf_xtensa_print_private_bfd_data (bfd *abfd, void *farg)
3536 {
3537 FILE *f = (FILE *) farg;
3538 flagword e_flags = elf_elfheader (abfd)->e_flags;
3539
3540 fprintf (f, "\nXtensa header:\n");
3541 if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
3542 fprintf (f, "\nMachine = Base\n");
3543 else
3544 fprintf (f, "\nMachine Id = 0x%x\n", e_flags & EF_XTENSA_MACH);
3545
3546 fprintf (f, "Insn tables = %s\n",
3547 (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
3548
3549 fprintf (f, "Literal tables = %s\n",
3550 (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
3551
3552 return _bfd_elf_print_private_bfd_data (abfd, farg);
3553 }
3554
3555
3556 /* Set the right machine number for an Xtensa ELF file. */
3557
3558 static bfd_boolean
3559 elf_xtensa_object_p (bfd *abfd)
3560 {
3561 int mach;
3562 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
3563
3564 switch (arch)
3565 {
3566 case E_XTENSA_MACH:
3567 mach = bfd_mach_xtensa;
3568 break;
3569 default:
3570 return FALSE;
3571 }
3572
3573 (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
3574 return TRUE;
3575 }
3576
3577
3578 /* The final processing done just before writing out an Xtensa ELF object
3579 file. This gets the Xtensa architecture right based on the machine
3580 number. */
3581
3582 static void
3583 elf_xtensa_final_write_processing (bfd *abfd,
3584 bfd_boolean linker ATTRIBUTE_UNUSED)
3585 {
3586 int mach;
3587 unsigned long val;
3588
3589 switch (mach = bfd_get_mach (abfd))
3590 {
3591 case bfd_mach_xtensa:
3592 val = E_XTENSA_MACH;
3593 break;
3594 default:
3595 return;
3596 }
3597
3598 elf_elfheader (abfd)->e_flags &= (~ EF_XTENSA_MACH);
3599 elf_elfheader (abfd)->e_flags |= val;
3600 }
3601
3602
3603 static enum elf_reloc_type_class
3604 elf_xtensa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3605 const asection *rel_sec ATTRIBUTE_UNUSED,
3606 const Elf_Internal_Rela *rela)
3607 {
3608 switch ((int) ELF32_R_TYPE (rela->r_info))
3609 {
3610 case R_XTENSA_RELATIVE:
3611 return reloc_class_relative;
3612 case R_XTENSA_JMP_SLOT:
3613 return reloc_class_plt;
3614 default:
3615 return reloc_class_normal;
3616 }
3617 }
3618
3619 \f
3620 static bfd_boolean
3621 elf_xtensa_discard_info_for_section (bfd *abfd,
3622 struct elf_reloc_cookie *cookie,
3623 struct bfd_link_info *info,
3624 asection *sec)
3625 {
3626 bfd_byte *contents;
3627 bfd_vma offset, actual_offset;
3628 bfd_size_type removed_bytes = 0;
3629 bfd_size_type entry_size;
3630
3631 if (sec->output_section
3632 && bfd_is_abs_section (sec->output_section))
3633 return FALSE;
3634
3635 if (xtensa_is_proptable_section (sec))
3636 entry_size = 12;
3637 else
3638 entry_size = 8;
3639
3640 if (sec->size == 0 || sec->size % entry_size != 0)
3641 return FALSE;
3642
3643 contents = retrieve_contents (abfd, sec, info->keep_memory);
3644 if (!contents)
3645 return FALSE;
3646
3647 cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
3648 if (!cookie->rels)
3649 {
3650 release_contents (sec, contents);
3651 return FALSE;
3652 }
3653
3654 /* Sort the relocations. They should already be in order when
3655 relaxation is enabled, but it might not be. */
3656 qsort (cookie->rels, sec->reloc_count, sizeof (Elf_Internal_Rela),
3657 internal_reloc_compare);
3658
3659 cookie->rel = cookie->rels;
3660 cookie->relend = cookie->rels + sec->reloc_count;
3661
3662 for (offset = 0; offset < sec->size; offset += entry_size)
3663 {
3664 actual_offset = offset - removed_bytes;
3665
3666 /* The ...symbol_deleted_p function will skip over relocs but it
3667 won't adjust their offsets, so do that here. */
3668 while (cookie->rel < cookie->relend
3669 && cookie->rel->r_offset < offset)
3670 {
3671 cookie->rel->r_offset -= removed_bytes;
3672 cookie->rel++;
3673 }
3674
3675 while (cookie->rel < cookie->relend
3676 && cookie->rel->r_offset == offset)
3677 {
3678 if (bfd_elf_reloc_symbol_deleted_p (offset, cookie))
3679 {
3680 /* Remove the table entry. (If the reloc type is NONE, then
3681 the entry has already been merged with another and deleted
3682 during relaxation.) */
3683 if (ELF32_R_TYPE (cookie->rel->r_info) != R_XTENSA_NONE)
3684 {
3685 /* Shift the contents up. */
3686 if (offset + entry_size < sec->size)
3687 memmove (&contents[actual_offset],
3688 &contents[actual_offset + entry_size],
3689 sec->size - offset - entry_size);
3690 removed_bytes += entry_size;
3691 }
3692
3693 /* Remove this relocation. */
3694 cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
3695 }
3696
3697 /* Adjust the relocation offset for previous removals. This
3698 should not be done before calling ...symbol_deleted_p
3699 because it might mess up the offset comparisons there.
3700 Make sure the offset doesn't underflow in the case where
3701 the first entry is removed. */
3702 if (cookie->rel->r_offset >= removed_bytes)
3703 cookie->rel->r_offset -= removed_bytes;
3704 else
3705 cookie->rel->r_offset = 0;
3706
3707 cookie->rel++;
3708 }
3709 }
3710
3711 if (removed_bytes != 0)
3712 {
3713 /* Adjust any remaining relocs (shouldn't be any). */
3714 for (; cookie->rel < cookie->relend; cookie->rel++)
3715 {
3716 if (cookie->rel->r_offset >= removed_bytes)
3717 cookie->rel->r_offset -= removed_bytes;
3718 else
3719 cookie->rel->r_offset = 0;
3720 }
3721
3722 /* Clear the removed bytes. */
3723 memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
3724
3725 pin_contents (sec, contents);
3726 pin_internal_relocs (sec, cookie->rels);
3727
3728 /* Shrink size. */
3729 if (sec->rawsize == 0)
3730 sec->rawsize = sec->size;
3731 sec->size -= removed_bytes;
3732
3733 if (xtensa_is_littable_section (sec))
3734 {
3735 asection *sgotloc = elf_xtensa_hash_table (info)->sgotloc;
3736 if (sgotloc)
3737 sgotloc->size -= removed_bytes;
3738 }
3739 }
3740 else
3741 {
3742 release_contents (sec, contents);
3743 release_internal_relocs (sec, cookie->rels);
3744 }
3745
3746 return (removed_bytes != 0);
3747 }
3748
3749
3750 static bfd_boolean
3751 elf_xtensa_discard_info (bfd *abfd,
3752 struct elf_reloc_cookie *cookie,
3753 struct bfd_link_info *info)
3754 {
3755 asection *sec;
3756 bfd_boolean changed = FALSE;
3757
3758 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3759 {
3760 if (xtensa_is_property_section (sec))
3761 {
3762 if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
3763 changed = TRUE;
3764 }
3765 }
3766
3767 return changed;
3768 }
3769
3770
3771 static bfd_boolean
3772 elf_xtensa_ignore_discarded_relocs (asection *sec)
3773 {
3774 return xtensa_is_property_section (sec);
3775 }
3776
3777
3778 static unsigned int
3779 elf_xtensa_action_discarded (asection *sec)
3780 {
3781 if (strcmp (".xt_except_table", sec->name) == 0)
3782 return 0;
3783
3784 if (strcmp (".xt_except_desc", sec->name) == 0)
3785 return 0;
3786
3787 return _bfd_elf_default_action_discarded (sec);
3788 }
3789
3790 \f
3791 /* Support for core dump NOTE sections. */
3792
3793 static bfd_boolean
3794 elf_xtensa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3795 {
3796 int offset;
3797 unsigned int size;
3798
3799 /* The size for Xtensa is variable, so don't try to recognize the format
3800 based on the size. Just assume this is GNU/Linux. */
3801
3802 /* pr_cursig */
3803 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3804
3805 /* pr_pid */
3806 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
3807
3808 /* pr_reg */
3809 offset = 72;
3810 size = note->descsz - offset - 4;
3811
3812 /* Make a ".reg/999" section. */
3813 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3814 size, note->descpos + offset);
3815 }
3816
3817
3818 static bfd_boolean
3819 elf_xtensa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3820 {
3821 switch (note->descsz)
3822 {
3823 default:
3824 return FALSE;
3825
3826 case 128: /* GNU/Linux elf_prpsinfo */
3827 elf_tdata (abfd)->core->program
3828 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
3829 elf_tdata (abfd)->core->command
3830 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
3831 }
3832
3833 /* Note that for some reason, a spurious space is tacked
3834 onto the end of the args in some (at least one anyway)
3835 implementations, so strip it off if it exists. */
3836
3837 {
3838 char *command = elf_tdata (abfd)->core->command;
3839 int n = strlen (command);
3840
3841 if (0 < n && command[n - 1] == ' ')
3842 command[n - 1] = '\0';
3843 }
3844
3845 return TRUE;
3846 }
3847
3848 \f
3849 /* Generic Xtensa configurability stuff. */
3850
3851 static xtensa_opcode callx0_op = XTENSA_UNDEFINED;
3852 static xtensa_opcode callx4_op = XTENSA_UNDEFINED;
3853 static xtensa_opcode callx8_op = XTENSA_UNDEFINED;
3854 static xtensa_opcode callx12_op = XTENSA_UNDEFINED;
3855 static xtensa_opcode call0_op = XTENSA_UNDEFINED;
3856 static xtensa_opcode call4_op = XTENSA_UNDEFINED;
3857 static xtensa_opcode call8_op = XTENSA_UNDEFINED;
3858 static xtensa_opcode call12_op = XTENSA_UNDEFINED;
3859
3860 static void
3861 init_call_opcodes (void)
3862 {
3863 if (callx0_op == XTENSA_UNDEFINED)
3864 {
3865 callx0_op = xtensa_opcode_lookup (xtensa_default_isa, "callx0");
3866 callx4_op = xtensa_opcode_lookup (xtensa_default_isa, "callx4");
3867 callx8_op = xtensa_opcode_lookup (xtensa_default_isa, "callx8");
3868 callx12_op = xtensa_opcode_lookup (xtensa_default_isa, "callx12");
3869 call0_op = xtensa_opcode_lookup (xtensa_default_isa, "call0");
3870 call4_op = xtensa_opcode_lookup (xtensa_default_isa, "call4");
3871 call8_op = xtensa_opcode_lookup (xtensa_default_isa, "call8");
3872 call12_op = xtensa_opcode_lookup (xtensa_default_isa, "call12");
3873 }
3874 }
3875
3876
3877 static bfd_boolean
3878 is_indirect_call_opcode (xtensa_opcode opcode)
3879 {
3880 init_call_opcodes ();
3881 return (opcode == callx0_op
3882 || opcode == callx4_op
3883 || opcode == callx8_op
3884 || opcode == callx12_op);
3885 }
3886
3887
3888 static bfd_boolean
3889 is_direct_call_opcode (xtensa_opcode opcode)
3890 {
3891 init_call_opcodes ();
3892 return (opcode == call0_op
3893 || opcode == call4_op
3894 || opcode == call8_op
3895 || opcode == call12_op);
3896 }
3897
3898
3899 static bfd_boolean
3900 is_windowed_call_opcode (xtensa_opcode opcode)
3901 {
3902 init_call_opcodes ();
3903 return (opcode == call4_op
3904 || opcode == call8_op
3905 || opcode == call12_op
3906 || opcode == callx4_op
3907 || opcode == callx8_op
3908 || opcode == callx12_op);
3909 }
3910
3911
3912 static bfd_boolean
3913 get_indirect_call_dest_reg (xtensa_opcode opcode, unsigned *pdst)
3914 {
3915 unsigned dst = (unsigned) -1;
3916
3917 init_call_opcodes ();
3918 if (opcode == callx0_op)
3919 dst = 0;
3920 else if (opcode == callx4_op)
3921 dst = 4;
3922 else if (opcode == callx8_op)
3923 dst = 8;
3924 else if (opcode == callx12_op)
3925 dst = 12;
3926
3927 if (dst == (unsigned) -1)
3928 return FALSE;
3929
3930 *pdst = dst;
3931 return TRUE;
3932 }
3933
3934
3935 static xtensa_opcode
3936 get_const16_opcode (void)
3937 {
3938 static bfd_boolean done_lookup = FALSE;
3939 static xtensa_opcode const16_opcode = XTENSA_UNDEFINED;
3940 if (!done_lookup)
3941 {
3942 const16_opcode = xtensa_opcode_lookup (xtensa_default_isa, "const16");
3943 done_lookup = TRUE;
3944 }
3945 return const16_opcode;
3946 }
3947
3948
3949 static xtensa_opcode
3950 get_l32r_opcode (void)
3951 {
3952 static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
3953 static bfd_boolean done_lookup = FALSE;
3954
3955 if (!done_lookup)
3956 {
3957 l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
3958 done_lookup = TRUE;
3959 }
3960 return l32r_opcode;
3961 }
3962
3963
3964 static bfd_vma
3965 l32r_offset (bfd_vma addr, bfd_vma pc)
3966 {
3967 bfd_vma offset;
3968
3969 offset = addr - ((pc+3) & -4);
3970 BFD_ASSERT ((offset & ((1 << 2) - 1)) == 0);
3971 offset = (signed int) offset >> 2;
3972 BFD_ASSERT ((signed int) offset >> 16 == -1);
3973 return offset;
3974 }
3975
3976
3977 static int
3978 get_relocation_opnd (xtensa_opcode opcode, int r_type)
3979 {
3980 xtensa_isa isa = xtensa_default_isa;
3981 int last_immed, last_opnd, opi;
3982
3983 if (opcode == XTENSA_UNDEFINED)
3984 return XTENSA_UNDEFINED;
3985
3986 /* Find the last visible PC-relative immediate operand for the opcode.
3987 If there are no PC-relative immediates, then choose the last visible
3988 immediate; otherwise, fail and return XTENSA_UNDEFINED. */
3989 last_immed = XTENSA_UNDEFINED;
3990 last_opnd = xtensa_opcode_num_operands (isa, opcode);
3991 for (opi = last_opnd - 1; opi >= 0; opi--)
3992 {
3993 if (xtensa_operand_is_visible (isa, opcode, opi) == 0)
3994 continue;
3995 if (xtensa_operand_is_PCrelative (isa, opcode, opi) == 1)
3996 {
3997 last_immed = opi;
3998 break;
3999 }
4000 if (last_immed == XTENSA_UNDEFINED
4001 && xtensa_operand_is_register (isa, opcode, opi) == 0)
4002 last_immed = opi;
4003 }
4004 if (last_immed < 0)
4005 return XTENSA_UNDEFINED;
4006
4007 /* If the operand number was specified in an old-style relocation,
4008 check for consistency with the operand computed above. */
4009 if (r_type >= R_XTENSA_OP0 && r_type <= R_XTENSA_OP2)
4010 {
4011 int reloc_opnd = r_type - R_XTENSA_OP0;
4012 if (reloc_opnd != last_immed)
4013 return XTENSA_UNDEFINED;
4014 }
4015
4016 return last_immed;
4017 }
4018
4019
4020 int
4021 get_relocation_slot (int r_type)
4022 {
4023 switch (r_type)
4024 {
4025 case R_XTENSA_OP0:
4026 case R_XTENSA_OP1:
4027 case R_XTENSA_OP2:
4028 return 0;
4029
4030 default:
4031 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
4032 return r_type - R_XTENSA_SLOT0_OP;
4033 if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
4034 return r_type - R_XTENSA_SLOT0_ALT;
4035 break;
4036 }
4037
4038 return XTENSA_UNDEFINED;
4039 }
4040
4041
4042 /* Get the opcode for a relocation. */
4043
4044 static xtensa_opcode
4045 get_relocation_opcode (bfd *abfd,
4046 asection *sec,
4047 bfd_byte *contents,
4048 Elf_Internal_Rela *irel)
4049 {
4050 static xtensa_insnbuf ibuff = NULL;
4051 static xtensa_insnbuf sbuff = NULL;
4052 xtensa_isa isa = xtensa_default_isa;
4053 xtensa_format fmt;
4054 int slot;
4055
4056 if (contents == NULL)
4057 return XTENSA_UNDEFINED;
4058
4059 if (bfd_get_section_limit (abfd, sec) <= irel->r_offset)
4060 return XTENSA_UNDEFINED;
4061
4062 if (ibuff == NULL)
4063 {
4064 ibuff = xtensa_insnbuf_alloc (isa);
4065 sbuff = xtensa_insnbuf_alloc (isa);
4066 }
4067
4068 /* Decode the instruction. */
4069 xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset],
4070 sec->size - irel->r_offset);
4071 fmt = xtensa_format_decode (isa, ibuff);
4072 slot = get_relocation_slot (ELF32_R_TYPE (irel->r_info));
4073 if (slot == XTENSA_UNDEFINED)
4074 return XTENSA_UNDEFINED;
4075 xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
4076 return xtensa_opcode_decode (isa, fmt, slot, sbuff);
4077 }
4078
4079
4080 bfd_boolean
4081 is_l32r_relocation (bfd *abfd,
4082 asection *sec,
4083 bfd_byte *contents,
4084 Elf_Internal_Rela *irel)
4085 {
4086 xtensa_opcode opcode;
4087 if (!is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
4088 return FALSE;
4089 opcode = get_relocation_opcode (abfd, sec, contents, irel);
4090 return (opcode == get_l32r_opcode ());
4091 }
4092
4093
4094 static bfd_size_type
4095 get_asm_simplify_size (bfd_byte *contents,
4096 bfd_size_type content_len,
4097 bfd_size_type offset)
4098 {
4099 bfd_size_type insnlen, size = 0;
4100
4101 /* Decode the size of the next two instructions. */
4102 insnlen = insn_decode_len (contents, content_len, offset);
4103 if (insnlen == 0)
4104 return 0;
4105
4106 size += insnlen;
4107
4108 insnlen = insn_decode_len (contents, content_len, offset + size);
4109 if (insnlen == 0)
4110 return 0;
4111
4112 size += insnlen;
4113 return size;
4114 }
4115
4116
4117 bfd_boolean
4118 is_alt_relocation (int r_type)
4119 {
4120 return (r_type >= R_XTENSA_SLOT0_ALT
4121 && r_type <= R_XTENSA_SLOT14_ALT);
4122 }
4123
4124
4125 bfd_boolean
4126 is_operand_relocation (int r_type)
4127 {
4128 switch (r_type)
4129 {
4130 case R_XTENSA_OP0:
4131 case R_XTENSA_OP1:
4132 case R_XTENSA_OP2:
4133 return TRUE;
4134
4135 default:
4136 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
4137 return TRUE;
4138 if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
4139 return TRUE;
4140 break;
4141 }
4142
4143 return FALSE;
4144 }
4145
4146
4147 #define MIN_INSN_LENGTH 2
4148
4149 /* Return 0 if it fails to decode. */
4150
4151 bfd_size_type
4152 insn_decode_len (bfd_byte *contents,
4153 bfd_size_type content_len,
4154 bfd_size_type offset)
4155 {
4156 int insn_len;
4157 xtensa_isa isa = xtensa_default_isa;
4158 xtensa_format fmt;
4159 static xtensa_insnbuf ibuff = NULL;
4160
4161 if (offset + MIN_INSN_LENGTH > content_len)
4162 return 0;
4163
4164 if (ibuff == NULL)
4165 ibuff = xtensa_insnbuf_alloc (isa);
4166 xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
4167 content_len - offset);
4168 fmt = xtensa_format_decode (isa, ibuff);
4169 if (fmt == XTENSA_UNDEFINED)
4170 return 0;
4171 insn_len = xtensa_format_length (isa, fmt);
4172 if (insn_len == XTENSA_UNDEFINED)
4173 return 0;
4174 return insn_len;
4175 }
4176
4177
4178 /* Decode the opcode for a single slot instruction.
4179 Return 0 if it fails to decode or the instruction is multi-slot. */
4180
4181 xtensa_opcode
4182 insn_decode_opcode (bfd_byte *contents,
4183 bfd_size_type content_len,
4184 bfd_size_type offset,
4185 int slot)
4186 {
4187 xtensa_isa isa = xtensa_default_isa;
4188 xtensa_format fmt;
4189 static xtensa_insnbuf insnbuf = NULL;
4190 static xtensa_insnbuf slotbuf = NULL;
4191
4192 if (offset + MIN_INSN_LENGTH > content_len)
4193 return XTENSA_UNDEFINED;
4194
4195 if (insnbuf == NULL)
4196 {
4197 insnbuf = xtensa_insnbuf_alloc (isa);
4198 slotbuf = xtensa_insnbuf_alloc (isa);
4199 }
4200
4201 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4202 content_len - offset);
4203 fmt = xtensa_format_decode (isa, insnbuf);
4204 if (fmt == XTENSA_UNDEFINED)
4205 return XTENSA_UNDEFINED;
4206
4207 if (slot >= xtensa_format_num_slots (isa, fmt))
4208 return XTENSA_UNDEFINED;
4209
4210 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
4211 return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
4212 }
4213
4214
4215 /* The offset is the offset in the contents.
4216 The address is the address of that offset. */
4217
4218 static bfd_boolean
4219 check_branch_target_aligned (bfd_byte *contents,
4220 bfd_size_type content_length,
4221 bfd_vma offset,
4222 bfd_vma address)
4223 {
4224 bfd_size_type insn_len = insn_decode_len (contents, content_length, offset);
4225 if (insn_len == 0)
4226 return FALSE;
4227 return check_branch_target_aligned_address (address, insn_len);
4228 }
4229
4230
4231 static bfd_boolean
4232 check_loop_aligned (bfd_byte *contents,
4233 bfd_size_type content_length,
4234 bfd_vma offset,
4235 bfd_vma address)
4236 {
4237 bfd_size_type loop_len, insn_len;
4238 xtensa_opcode opcode;
4239
4240 opcode = insn_decode_opcode (contents, content_length, offset, 0);
4241 if (opcode == XTENSA_UNDEFINED
4242 || xtensa_opcode_is_loop (xtensa_default_isa, opcode) != 1)
4243 {
4244 BFD_ASSERT (FALSE);
4245 return FALSE;
4246 }
4247
4248 loop_len = insn_decode_len (contents, content_length, offset);
4249 insn_len = insn_decode_len (contents, content_length, offset + loop_len);
4250 if (loop_len == 0 || insn_len == 0)
4251 {
4252 BFD_ASSERT (FALSE);
4253 return FALSE;
4254 }
4255
4256 return check_branch_target_aligned_address (address + loop_len, insn_len);
4257 }
4258
4259
4260 static bfd_boolean
4261 check_branch_target_aligned_address (bfd_vma addr, int len)
4262 {
4263 if (len == 8)
4264 return (addr % 8 == 0);
4265 return ((addr >> 2) == ((addr + len - 1) >> 2));
4266 }
4267
4268 \f
4269 /* Instruction widening and narrowing. */
4270
4271 /* When FLIX is available we need to access certain instructions only
4272 when they are 16-bit or 24-bit instructions. This table caches
4273 information about such instructions by walking through all the
4274 opcodes and finding the smallest single-slot format into which each
4275 can be encoded. */
4276
4277 static xtensa_format *op_single_fmt_table = NULL;
4278
4279
4280 static void
4281 init_op_single_format_table (void)
4282 {
4283 xtensa_isa isa = xtensa_default_isa;
4284 xtensa_insnbuf ibuf;
4285 xtensa_opcode opcode;
4286 xtensa_format fmt;
4287 int num_opcodes;
4288
4289 if (op_single_fmt_table)
4290 return;
4291
4292 ibuf = xtensa_insnbuf_alloc (isa);
4293 num_opcodes = xtensa_isa_num_opcodes (isa);
4294
4295 op_single_fmt_table = (xtensa_format *)
4296 bfd_malloc (sizeof (xtensa_format) * num_opcodes);
4297 for (opcode = 0; opcode < num_opcodes; opcode++)
4298 {
4299 op_single_fmt_table[opcode] = XTENSA_UNDEFINED;
4300 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
4301 {
4302 if (xtensa_format_num_slots (isa, fmt) == 1
4303 && xtensa_opcode_encode (isa, fmt, 0, ibuf, opcode) == 0)
4304 {
4305 xtensa_opcode old_fmt = op_single_fmt_table[opcode];
4306 int fmt_length = xtensa_format_length (isa, fmt);
4307 if (old_fmt == XTENSA_UNDEFINED
4308 || fmt_length < xtensa_format_length (isa, old_fmt))
4309 op_single_fmt_table[opcode] = fmt;
4310 }
4311 }
4312 }
4313 xtensa_insnbuf_free (isa, ibuf);
4314 }
4315
4316
4317 static xtensa_format
4318 get_single_format (xtensa_opcode opcode)
4319 {
4320 init_op_single_format_table ();
4321 return op_single_fmt_table[opcode];
4322 }
4323
4324
4325 /* For the set of narrowable instructions we do NOT include the
4326 narrowings beqz -> beqz.n or bnez -> bnez.n because of complexities
4327 involved during linker relaxation that may require these to
4328 re-expand in some conditions. Also, the narrowing "or" -> mov.n
4329 requires special case code to ensure it only works when op1 == op2. */
4330
4331 struct string_pair
4332 {
4333 const char *wide;
4334 const char *narrow;
4335 };
4336
4337 struct string_pair narrowable[] =
4338 {
4339 { "add", "add.n" },
4340 { "addi", "addi.n" },
4341 { "addmi", "addi.n" },
4342 { "l32i", "l32i.n" },
4343 { "movi", "movi.n" },
4344 { "ret", "ret.n" },
4345 { "retw", "retw.n" },
4346 { "s32i", "s32i.n" },
4347 { "or", "mov.n" } /* special case only when op1 == op2 */
4348 };
4349
4350 struct string_pair widenable[] =
4351 {
4352 { "add", "add.n" },
4353 { "addi", "addi.n" },
4354 { "addmi", "addi.n" },
4355 { "beqz", "beqz.n" },
4356 { "bnez", "bnez.n" },
4357 { "l32i", "l32i.n" },
4358 { "movi", "movi.n" },
4359 { "ret", "ret.n" },
4360 { "retw", "retw.n" },
4361 { "s32i", "s32i.n" },
4362 { "or", "mov.n" } /* special case only when op1 == op2 */
4363 };
4364
4365
4366 /* Check if an instruction can be "narrowed", i.e., changed from a standard
4367 3-byte instruction to a 2-byte "density" instruction. If it is valid,
4368 return the instruction buffer holding the narrow instruction. Otherwise,
4369 return 0. The set of valid narrowing are specified by a string table
4370 but require some special case operand checks in some cases. */
4371
4372 static xtensa_insnbuf
4373 can_narrow_instruction (xtensa_insnbuf slotbuf,
4374 xtensa_format fmt,
4375 xtensa_opcode opcode)
4376 {
4377 xtensa_isa isa = xtensa_default_isa;
4378 xtensa_format o_fmt;
4379 unsigned opi;
4380
4381 static xtensa_insnbuf o_insnbuf = NULL;
4382 static xtensa_insnbuf o_slotbuf = NULL;
4383
4384 if (o_insnbuf == NULL)
4385 {
4386 o_insnbuf = xtensa_insnbuf_alloc (isa);
4387 o_slotbuf = xtensa_insnbuf_alloc (isa);
4388 }
4389
4390 for (opi = 0; opi < (sizeof (narrowable)/sizeof (struct string_pair)); opi++)
4391 {
4392 bfd_boolean is_or = (strcmp ("or", narrowable[opi].wide) == 0);
4393
4394 if (opcode == xtensa_opcode_lookup (isa, narrowable[opi].wide))
4395 {
4396 uint32 value, newval;
4397 int i, operand_count, o_operand_count;
4398 xtensa_opcode o_opcode;
4399
4400 /* Address does not matter in this case. We might need to
4401 fix it to handle branches/jumps. */
4402 bfd_vma self_address = 0;
4403
4404 o_opcode = xtensa_opcode_lookup (isa, narrowable[opi].narrow);
4405 if (o_opcode == XTENSA_UNDEFINED)
4406 return 0;
4407 o_fmt = get_single_format (o_opcode);
4408 if (o_fmt == XTENSA_UNDEFINED)
4409 return 0;
4410
4411 if (xtensa_format_length (isa, fmt) != 3
4412 || xtensa_format_length (isa, o_fmt) != 2)
4413 return 0;
4414
4415 xtensa_format_encode (isa, o_fmt, o_insnbuf);
4416 operand_count = xtensa_opcode_num_operands (isa, opcode);
4417 o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
4418
4419 if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
4420 return 0;
4421
4422 if (!is_or)
4423 {
4424 if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
4425 return 0;
4426 }
4427 else
4428 {
4429 uint32 rawval0, rawval1, rawval2;
4430
4431 if (o_operand_count + 1 != operand_count
4432 || xtensa_operand_get_field (isa, opcode, 0,
4433 fmt, 0, slotbuf, &rawval0) != 0
4434 || xtensa_operand_get_field (isa, opcode, 1,
4435 fmt, 0, slotbuf, &rawval1) != 0
4436 || xtensa_operand_get_field (isa, opcode, 2,
4437 fmt, 0, slotbuf, &rawval2) != 0
4438 || rawval1 != rawval2
4439 || rawval0 == rawval1 /* it is a nop */)
4440 return 0;
4441 }
4442
4443 for (i = 0; i < o_operand_count; ++i)
4444 {
4445 if (xtensa_operand_get_field (isa, opcode, i, fmt, 0,
4446 slotbuf, &value)
4447 || xtensa_operand_decode (isa, opcode, i, &value))
4448 return 0;
4449
4450 /* PC-relative branches need adjustment, but
4451 the PC-rel operand will always have a relocation. */
4452 newval = value;
4453 if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4454 self_address)
4455 || xtensa_operand_encode (isa, o_opcode, i, &newval)
4456 || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4457 o_slotbuf, newval))
4458 return 0;
4459 }
4460
4461 if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4462 return 0;
4463
4464 return o_insnbuf;
4465 }
4466 }
4467 return 0;
4468 }
4469
4470
4471 /* Attempt to narrow an instruction. If the narrowing is valid, perform
4472 the action in-place directly into the contents and return TRUE. Otherwise,
4473 the return value is FALSE and the contents are not modified. */
4474
4475 static bfd_boolean
4476 narrow_instruction (bfd_byte *contents,
4477 bfd_size_type content_length,
4478 bfd_size_type offset)
4479 {
4480 xtensa_opcode opcode;
4481 bfd_size_type insn_len;
4482 xtensa_isa isa = xtensa_default_isa;
4483 xtensa_format fmt;
4484 xtensa_insnbuf o_insnbuf;
4485
4486 static xtensa_insnbuf insnbuf = NULL;
4487 static xtensa_insnbuf slotbuf = NULL;
4488
4489 if (insnbuf == NULL)
4490 {
4491 insnbuf = xtensa_insnbuf_alloc (isa);
4492 slotbuf = xtensa_insnbuf_alloc (isa);
4493 }
4494
4495 BFD_ASSERT (offset < content_length);
4496
4497 if (content_length < 2)
4498 return FALSE;
4499
4500 /* We will hand-code a few of these for a little while.
4501 These have all been specified in the assembler aleady. */
4502 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4503 content_length - offset);
4504 fmt = xtensa_format_decode (isa, insnbuf);
4505 if (xtensa_format_num_slots (isa, fmt) != 1)
4506 return FALSE;
4507
4508 if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
4509 return FALSE;
4510
4511 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4512 if (opcode == XTENSA_UNDEFINED)
4513 return FALSE;
4514 insn_len = xtensa_format_length (isa, fmt);
4515 if (insn_len > content_length)
4516 return FALSE;
4517
4518 o_insnbuf = can_narrow_instruction (slotbuf, fmt, opcode);
4519 if (o_insnbuf)
4520 {
4521 xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4522 content_length - offset);
4523 return TRUE;
4524 }
4525
4526 return FALSE;
4527 }
4528
4529
4530 /* Check if an instruction can be "widened", i.e., changed from a 2-byte
4531 "density" instruction to a standard 3-byte instruction. If it is valid,
4532 return the instruction buffer holding the wide instruction. Otherwise,
4533 return 0. The set of valid widenings are specified by a string table
4534 but require some special case operand checks in some cases. */
4535
4536 static xtensa_insnbuf
4537 can_widen_instruction (xtensa_insnbuf slotbuf,
4538 xtensa_format fmt,
4539 xtensa_opcode opcode)
4540 {
4541 xtensa_isa isa = xtensa_default_isa;
4542 xtensa_format o_fmt;
4543 unsigned opi;
4544
4545 static xtensa_insnbuf o_insnbuf = NULL;
4546 static xtensa_insnbuf o_slotbuf = NULL;
4547
4548 if (o_insnbuf == NULL)
4549 {
4550 o_insnbuf = xtensa_insnbuf_alloc (isa);
4551 o_slotbuf = xtensa_insnbuf_alloc (isa);
4552 }
4553
4554 for (opi = 0; opi < (sizeof (widenable)/sizeof (struct string_pair)); opi++)
4555 {
4556 bfd_boolean is_or = (strcmp ("or", widenable[opi].wide) == 0);
4557 bfd_boolean is_branch = (strcmp ("beqz", widenable[opi].wide) == 0
4558 || strcmp ("bnez", widenable[opi].wide) == 0);
4559
4560 if (opcode == xtensa_opcode_lookup (isa, widenable[opi].narrow))
4561 {
4562 uint32 value, newval;
4563 int i, operand_count, o_operand_count, check_operand_count;
4564 xtensa_opcode o_opcode;
4565
4566 /* Address does not matter in this case. We might need to fix it
4567 to handle branches/jumps. */
4568 bfd_vma self_address = 0;
4569
4570 o_opcode = xtensa_opcode_lookup (isa, widenable[opi].wide);
4571 if (o_opcode == XTENSA_UNDEFINED)
4572 return 0;
4573 o_fmt = get_single_format (o_opcode);
4574 if (o_fmt == XTENSA_UNDEFINED)
4575 return 0;
4576
4577 if (xtensa_format_length (isa, fmt) != 2
4578 || xtensa_format_length (isa, o_fmt) != 3)
4579 return 0;
4580
4581 xtensa_format_encode (isa, o_fmt, o_insnbuf);
4582 operand_count = xtensa_opcode_num_operands (isa, opcode);
4583 o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
4584 check_operand_count = o_operand_count;
4585
4586 if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
4587 return 0;
4588
4589 if (!is_or)
4590 {
4591 if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
4592 return 0;
4593 }
4594 else
4595 {
4596 uint32 rawval0, rawval1;
4597
4598 if (o_operand_count != operand_count + 1
4599 || xtensa_operand_get_field (isa, opcode, 0,
4600 fmt, 0, slotbuf, &rawval0) != 0
4601 || xtensa_operand_get_field (isa, opcode, 1,
4602 fmt, 0, slotbuf, &rawval1) != 0
4603 || rawval0 == rawval1 /* it is a nop */)
4604 return 0;
4605 }
4606 if (is_branch)
4607 check_operand_count--;
4608
4609 for (i = 0; i < check_operand_count; i++)
4610 {
4611 int new_i = i;
4612 if (is_or && i == o_operand_count - 1)
4613 new_i = i - 1;
4614 if (xtensa_operand_get_field (isa, opcode, new_i, fmt, 0,
4615 slotbuf, &value)
4616 || xtensa_operand_decode (isa, opcode, new_i, &value))
4617 return 0;
4618
4619 /* PC-relative branches need adjustment, but
4620 the PC-rel operand will always have a relocation. */
4621 newval = value;
4622 if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4623 self_address)
4624 || xtensa_operand_encode (isa, o_opcode, i, &newval)
4625 || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4626 o_slotbuf, newval))
4627 return 0;
4628 }
4629
4630 if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4631 return 0;
4632
4633 return o_insnbuf;
4634 }
4635 }
4636 return 0;
4637 }
4638
4639
4640 /* Attempt to widen an instruction. If the widening is valid, perform
4641 the action in-place directly into the contents and return TRUE. Otherwise,
4642 the return value is FALSE and the contents are not modified. */
4643
4644 static bfd_boolean
4645 widen_instruction (bfd_byte *contents,
4646 bfd_size_type content_length,
4647 bfd_size_type offset)
4648 {
4649 xtensa_opcode opcode;
4650 bfd_size_type insn_len;
4651 xtensa_isa isa = xtensa_default_isa;
4652 xtensa_format fmt;
4653 xtensa_insnbuf o_insnbuf;
4654
4655 static xtensa_insnbuf insnbuf = NULL;
4656 static xtensa_insnbuf slotbuf = NULL;
4657
4658 if (insnbuf == NULL)
4659 {
4660 insnbuf = xtensa_insnbuf_alloc (isa);
4661 slotbuf = xtensa_insnbuf_alloc (isa);
4662 }
4663
4664 BFD_ASSERT (offset < content_length);
4665
4666 if (content_length < 2)
4667 return FALSE;
4668
4669 /* We will hand-code a few of these for a little while.
4670 These have all been specified in the assembler aleady. */
4671 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4672 content_length - offset);
4673 fmt = xtensa_format_decode (isa, insnbuf);
4674 if (xtensa_format_num_slots (isa, fmt) != 1)
4675 return FALSE;
4676
4677 if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
4678 return FALSE;
4679
4680 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4681 if (opcode == XTENSA_UNDEFINED)
4682 return FALSE;
4683 insn_len = xtensa_format_length (isa, fmt);
4684 if (insn_len > content_length)
4685 return FALSE;
4686
4687 o_insnbuf = can_widen_instruction (slotbuf, fmt, opcode);
4688 if (o_insnbuf)
4689 {
4690 xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4691 content_length - offset);
4692 return TRUE;
4693 }
4694 return FALSE;
4695 }
4696
4697 \f
4698 /* Code for transforming CALLs at link-time. */
4699
4700 static bfd_reloc_status_type
4701 elf_xtensa_do_asm_simplify (bfd_byte *contents,
4702 bfd_vma address,
4703 bfd_vma content_length,
4704 char **error_message)
4705 {
4706 static xtensa_insnbuf insnbuf = NULL;
4707 static xtensa_insnbuf slotbuf = NULL;
4708 xtensa_format core_format = XTENSA_UNDEFINED;
4709 xtensa_opcode opcode;
4710 xtensa_opcode direct_call_opcode;
4711 xtensa_isa isa = xtensa_default_isa;
4712 bfd_byte *chbuf = contents + address;
4713 int opn;
4714
4715 if (insnbuf == NULL)
4716 {
4717 insnbuf = xtensa_insnbuf_alloc (isa);
4718 slotbuf = xtensa_insnbuf_alloc (isa);
4719 }
4720
4721 if (content_length < address)
4722 {
4723 *error_message = _("Attempt to convert L32R/CALLX to CALL failed");
4724 return bfd_reloc_other;
4725 }
4726
4727 opcode = get_expanded_call_opcode (chbuf, content_length - address, 0);
4728 direct_call_opcode = swap_callx_for_call_opcode (opcode);
4729 if (direct_call_opcode == XTENSA_UNDEFINED)
4730 {
4731 *error_message = _("Attempt to convert L32R/CALLX to CALL failed");
4732 return bfd_reloc_other;
4733 }
4734
4735 /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
4736 core_format = xtensa_format_lookup (isa, "x24");
4737 opcode = xtensa_opcode_lookup (isa, "or");
4738 xtensa_opcode_encode (isa, core_format, 0, slotbuf, opcode);
4739 for (opn = 0; opn < 3; opn++)
4740 {
4741 uint32 regno = 1;
4742 xtensa_operand_encode (isa, opcode, opn, &regno);
4743 xtensa_operand_set_field (isa, opcode, opn, core_format, 0,
4744 slotbuf, regno);
4745 }
4746 xtensa_format_encode (isa, core_format, insnbuf);
4747 xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
4748 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf, content_length - address);
4749
4750 /* Assemble a CALL ("callN 0") into the 3 byte offset. */
4751 xtensa_opcode_encode (isa, core_format, 0, slotbuf, direct_call_opcode);
4752 xtensa_operand_set_field (isa, opcode, 0, core_format, 0, slotbuf, 0);
4753
4754 xtensa_format_encode (isa, core_format, insnbuf);
4755 xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
4756 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf + 3,
4757 content_length - address - 3);
4758
4759 return bfd_reloc_ok;
4760 }
4761
4762
4763 static bfd_reloc_status_type
4764 contract_asm_expansion (bfd_byte *contents,
4765 bfd_vma content_length,
4766 Elf_Internal_Rela *irel,
4767 char **error_message)
4768 {
4769 bfd_reloc_status_type retval =
4770 elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length,
4771 error_message);
4772
4773 if (retval != bfd_reloc_ok)
4774 return bfd_reloc_dangerous;
4775
4776 /* Update the irel->r_offset field so that the right immediate and
4777 the right instruction are modified during the relocation. */
4778 irel->r_offset += 3;
4779 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_XTENSA_SLOT0_OP);
4780 return bfd_reloc_ok;
4781 }
4782
4783
4784 static xtensa_opcode
4785 swap_callx_for_call_opcode (xtensa_opcode opcode)
4786 {
4787 init_call_opcodes ();
4788
4789 if (opcode == callx0_op) return call0_op;
4790 if (opcode == callx4_op) return call4_op;
4791 if (opcode == callx8_op) return call8_op;
4792 if (opcode == callx12_op) return call12_op;
4793
4794 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
4795 return XTENSA_UNDEFINED;
4796 }
4797
4798
4799 /* Check if "buf" is pointing to a "L32R aN; CALLX aN" or "CONST16 aN;
4800 CONST16 aN; CALLX aN" sequence, and if so, return the CALLX opcode.
4801 If not, return XTENSA_UNDEFINED. */
4802
4803 #define L32R_TARGET_REG_OPERAND 0
4804 #define CONST16_TARGET_REG_OPERAND 0
4805 #define CALLN_SOURCE_OPERAND 0
4806
4807 static xtensa_opcode
4808 get_expanded_call_opcode (bfd_byte *buf, int bufsize, bfd_boolean *p_uses_l32r)
4809 {
4810 static xtensa_insnbuf insnbuf = NULL;
4811 static xtensa_insnbuf slotbuf = NULL;
4812 xtensa_format fmt;
4813 xtensa_opcode opcode;
4814 xtensa_isa isa = xtensa_default_isa;
4815 uint32 regno, const16_regno, call_regno;
4816 int offset = 0;
4817
4818 if (insnbuf == NULL)
4819 {
4820 insnbuf = xtensa_insnbuf_alloc (isa);
4821 slotbuf = xtensa_insnbuf_alloc (isa);
4822 }
4823
4824 xtensa_insnbuf_from_chars (isa, insnbuf, buf, bufsize);
4825 fmt = xtensa_format_decode (isa, insnbuf);
4826 if (fmt == XTENSA_UNDEFINED
4827 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4828 return XTENSA_UNDEFINED;
4829
4830 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4831 if (opcode == XTENSA_UNDEFINED)
4832 return XTENSA_UNDEFINED;
4833
4834 if (opcode == get_l32r_opcode ())
4835 {
4836 if (p_uses_l32r)
4837 *p_uses_l32r = TRUE;
4838 if (xtensa_operand_get_field (isa, opcode, L32R_TARGET_REG_OPERAND,
4839 fmt, 0, slotbuf, &regno)
4840 || xtensa_operand_decode (isa, opcode, L32R_TARGET_REG_OPERAND,
4841 &regno))
4842 return XTENSA_UNDEFINED;
4843 }
4844 else if (opcode == get_const16_opcode ())
4845 {
4846 if (p_uses_l32r)
4847 *p_uses_l32r = FALSE;
4848 if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4849 fmt, 0, slotbuf, &regno)
4850 || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4851 &regno))
4852 return XTENSA_UNDEFINED;
4853
4854 /* Check that the next instruction is also CONST16. */
4855 offset += xtensa_format_length (isa, fmt);
4856 xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
4857 fmt = xtensa_format_decode (isa, insnbuf);
4858 if (fmt == XTENSA_UNDEFINED
4859 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4860 return XTENSA_UNDEFINED;
4861 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4862 if (opcode != get_const16_opcode ())
4863 return XTENSA_UNDEFINED;
4864
4865 if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4866 fmt, 0, slotbuf, &const16_regno)
4867 || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4868 &const16_regno)
4869 || const16_regno != regno)
4870 return XTENSA_UNDEFINED;
4871 }
4872 else
4873 return XTENSA_UNDEFINED;
4874
4875 /* Next instruction should be an CALLXn with operand 0 == regno. */
4876 offset += xtensa_format_length (isa, fmt);
4877 xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
4878 fmt = xtensa_format_decode (isa, insnbuf);
4879 if (fmt == XTENSA_UNDEFINED
4880 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4881 return XTENSA_UNDEFINED;
4882 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4883 if (opcode == XTENSA_UNDEFINED
4884 || !is_indirect_call_opcode (opcode))
4885 return XTENSA_UNDEFINED;
4886
4887 if (xtensa_operand_get_field (isa, opcode, CALLN_SOURCE_OPERAND,
4888 fmt, 0, slotbuf, &call_regno)
4889 || xtensa_operand_decode (isa, opcode, CALLN_SOURCE_OPERAND,
4890 &call_regno))
4891 return XTENSA_UNDEFINED;
4892
4893 if (call_regno != regno)
4894 return XTENSA_UNDEFINED;
4895
4896 return opcode;
4897 }
4898
4899 \f
4900 /* Data structures used during relaxation. */
4901
4902 /* r_reloc: relocation values. */
4903
4904 /* Through the relaxation process, we need to keep track of the values
4905 that will result from evaluating relocations. The standard ELF
4906 relocation structure is not sufficient for this purpose because we're
4907 operating on multiple input files at once, so we need to know which
4908 input file a relocation refers to. The r_reloc structure thus
4909 records both the input file (bfd) and ELF relocation.
4910
4911 For efficiency, an r_reloc also contains a "target_offset" field to
4912 cache the target-section-relative offset value that is represented by
4913 the relocation.
4914
4915 The r_reloc also contains a virtual offset that allows multiple
4916 inserted literals to be placed at the same "address" with
4917 different offsets. */
4918
4919 typedef struct r_reloc_struct r_reloc;
4920
4921 struct r_reloc_struct
4922 {
4923 bfd *abfd;
4924 Elf_Internal_Rela rela;
4925 bfd_vma target_offset;
4926 bfd_vma virtual_offset;
4927 };
4928
4929
4930 /* The r_reloc structure is included by value in literal_value, but not
4931 every literal_value has an associated relocation -- some are simple
4932 constants. In such cases, we set all the fields in the r_reloc
4933 struct to zero. The r_reloc_is_const function should be used to
4934 detect this case. */
4935
4936 static bfd_boolean
4937 r_reloc_is_const (const r_reloc *r_rel)
4938 {
4939 return (r_rel->abfd == NULL);
4940 }
4941
4942
4943 static bfd_vma
4944 r_reloc_get_target_offset (const r_reloc *r_rel)
4945 {
4946 bfd_vma target_offset;
4947 unsigned long r_symndx;
4948
4949 BFD_ASSERT (!r_reloc_is_const (r_rel));
4950 r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4951 target_offset = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
4952 return (target_offset + r_rel->rela.r_addend);
4953 }
4954
4955
4956 static struct elf_link_hash_entry *
4957 r_reloc_get_hash_entry (const r_reloc *r_rel)
4958 {
4959 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4960 return get_elf_r_symndx_hash_entry (r_rel->abfd, r_symndx);
4961 }
4962
4963
4964 static asection *
4965 r_reloc_get_section (const r_reloc *r_rel)
4966 {
4967 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4968 return get_elf_r_symndx_section (r_rel->abfd, r_symndx);
4969 }
4970
4971
4972 static bfd_boolean
4973 r_reloc_is_defined (const r_reloc *r_rel)
4974 {
4975 asection *sec;
4976 if (r_rel == NULL)
4977 return FALSE;
4978
4979 sec = r_reloc_get_section (r_rel);
4980 if (sec == bfd_abs_section_ptr
4981 || sec == bfd_com_section_ptr
4982 || sec == bfd_und_section_ptr)
4983 return FALSE;
4984 return TRUE;
4985 }
4986
4987
4988 static void
4989 r_reloc_init (r_reloc *r_rel,
4990 bfd *abfd,
4991 Elf_Internal_Rela *irel,
4992 bfd_byte *contents,
4993 bfd_size_type content_length)
4994 {
4995 int r_type;
4996 reloc_howto_type *howto;
4997
4998 if (irel)
4999 {
5000 r_rel->rela = *irel;
5001 r_rel->abfd = abfd;
5002 r_rel->target_offset = r_reloc_get_target_offset (r_rel);
5003 r_rel->virtual_offset = 0;
5004 r_type = ELF32_R_TYPE (r_rel->rela.r_info);
5005 howto = &elf_howto_table[r_type];
5006 if (howto->partial_inplace)
5007 {
5008 bfd_vma inplace_val;
5009 BFD_ASSERT (r_rel->rela.r_offset < content_length);
5010
5011 inplace_val = bfd_get_32 (abfd, &contents[r_rel->rela.r_offset]);
5012 r_rel->target_offset += inplace_val;
5013 }
5014 }
5015 else
5016 memset (r_rel, 0, sizeof (r_reloc));
5017 }
5018
5019
5020 #if DEBUG
5021
5022 static void
5023 print_r_reloc (FILE *fp, const r_reloc *r_rel)
5024 {
5025 if (r_reloc_is_defined (r_rel))
5026 {
5027 asection *sec = r_reloc_get_section (r_rel);
5028 fprintf (fp, " %s(%s + ", sec->owner->filename, sec->name);
5029 }
5030 else if (r_reloc_get_hash_entry (r_rel))
5031 fprintf (fp, " %s + ", r_reloc_get_hash_entry (r_rel)->root.root.string);
5032 else
5033 fprintf (fp, " ?? + ");
5034
5035 fprintf_vma (fp, r_rel->target_offset);
5036 if (r_rel->virtual_offset)
5037 {
5038 fprintf (fp, " + ");
5039 fprintf_vma (fp, r_rel->virtual_offset);
5040 }
5041
5042 fprintf (fp, ")");
5043 }
5044
5045 #endif /* DEBUG */
5046
5047 \f
5048 /* source_reloc: relocations that reference literals. */
5049
5050 /* To determine whether literals can be coalesced, we need to first
5051 record all the relocations that reference the literals. The
5052 source_reloc structure below is used for this purpose. The
5053 source_reloc entries are kept in a per-literal-section array, sorted
5054 by offset within the literal section (i.e., target offset).
5055
5056 The source_sec and r_rel.rela.r_offset fields identify the source of
5057 the relocation. The r_rel field records the relocation value, i.e.,
5058 the offset of the literal being referenced. The opnd field is needed
5059 to determine the range of the immediate field to which the relocation
5060 applies, so we can determine whether another literal with the same
5061 value is within range. The is_null field is true when the relocation
5062 is being removed (e.g., when an L32R is being removed due to a CALLX
5063 that is converted to a direct CALL). */
5064
5065 typedef struct source_reloc_struct source_reloc;
5066
5067 struct source_reloc_struct
5068 {
5069 asection *source_sec;
5070 r_reloc r_rel;
5071 xtensa_opcode opcode;
5072 int opnd;
5073 bfd_boolean is_null;
5074 bfd_boolean is_abs_literal;
5075 };
5076
5077
5078 static void
5079 init_source_reloc (source_reloc *reloc,
5080 asection *source_sec,
5081 const r_reloc *r_rel,
5082 xtensa_opcode opcode,
5083 int opnd,
5084 bfd_boolean is_abs_literal)
5085 {
5086 reloc->source_sec = source_sec;
5087 reloc->r_rel = *r_rel;
5088 reloc->opcode = opcode;
5089 reloc->opnd = opnd;
5090 reloc->is_null = FALSE;
5091 reloc->is_abs_literal = is_abs_literal;
5092 }
5093
5094
5095 /* Find the source_reloc for a particular source offset and relocation
5096 type. Note that the array is sorted by _target_ offset, so this is
5097 just a linear search. */
5098
5099 static source_reloc *
5100 find_source_reloc (source_reloc *src_relocs,
5101 int src_count,
5102 asection *sec,
5103 Elf_Internal_Rela *irel)
5104 {
5105 int i;
5106
5107 for (i = 0; i < src_count; i++)
5108 {
5109 if (src_relocs[i].source_sec == sec
5110 && src_relocs[i].r_rel.rela.r_offset == irel->r_offset
5111 && (ELF32_R_TYPE (src_relocs[i].r_rel.rela.r_info)
5112 == ELF32_R_TYPE (irel->r_info)))
5113 return &src_relocs[i];
5114 }
5115
5116 return NULL;
5117 }
5118
5119
5120 static int
5121 source_reloc_compare (const void *ap, const void *bp)
5122 {
5123 const source_reloc *a = (const source_reloc *) ap;
5124 const source_reloc *b = (const source_reloc *) bp;
5125
5126 if (a->r_rel.target_offset != b->r_rel.target_offset)
5127 return (a->r_rel.target_offset - b->r_rel.target_offset);
5128
5129 /* We don't need to sort on these criteria for correctness,
5130 but enforcing a more strict ordering prevents unstable qsort
5131 from behaving differently with different implementations.
5132 Without the code below we get correct but different results
5133 on Solaris 2.7 and 2.8. We would like to always produce the
5134 same results no matter the host. */
5135
5136 if ((!a->is_null) - (!b->is_null))
5137 return ((!a->is_null) - (!b->is_null));
5138 return internal_reloc_compare (&a->r_rel.rela, &b->r_rel.rela);
5139 }
5140
5141 \f
5142 /* Literal values and value hash tables. */
5143
5144 /* Literals with the same value can be coalesced. The literal_value
5145 structure records the value of a literal: the "r_rel" field holds the
5146 information from the relocation on the literal (if there is one) and
5147 the "value" field holds the contents of the literal word itself.
5148
5149 The value_map structure records a literal value along with the
5150 location of a literal holding that value. The value_map hash table
5151 is indexed by the literal value, so that we can quickly check if a
5152 particular literal value has been seen before and is thus a candidate
5153 for coalescing. */
5154
5155 typedef struct literal_value_struct literal_value;
5156 typedef struct value_map_struct value_map;
5157 typedef struct value_map_hash_table_struct value_map_hash_table;
5158
5159 struct literal_value_struct
5160 {
5161 r_reloc r_rel;
5162 unsigned long value;
5163 bfd_boolean is_abs_literal;
5164 };
5165
5166 struct value_map_struct
5167 {
5168 literal_value val; /* The literal value. */
5169 r_reloc loc; /* Location of the literal. */
5170 value_map *next;
5171 };
5172
5173 struct value_map_hash_table_struct
5174 {
5175 unsigned bucket_count;
5176 value_map **buckets;
5177 unsigned count;
5178 bfd_boolean has_last_loc;
5179 r_reloc last_loc;
5180 };
5181
5182
5183 static void
5184 init_literal_value (literal_value *lit,
5185 const r_reloc *r_rel,
5186 unsigned long value,
5187 bfd_boolean is_abs_literal)
5188 {
5189 lit->r_rel = *r_rel;
5190 lit->value = value;
5191 lit->is_abs_literal = is_abs_literal;
5192 }
5193
5194
5195 static bfd_boolean
5196 literal_value_equal (const literal_value *src1,
5197 const literal_value *src2,
5198 bfd_boolean final_static_link)
5199 {
5200 struct elf_link_hash_entry *h1, *h2;
5201
5202 if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
5203 return FALSE;
5204
5205 if (r_reloc_is_const (&src1->r_rel))
5206 return (src1->value == src2->value);
5207
5208 if (ELF32_R_TYPE (src1->r_rel.rela.r_info)
5209 != ELF32_R_TYPE (src2->r_rel.rela.r_info))
5210 return FALSE;
5211
5212 if (src1->r_rel.target_offset != src2->r_rel.target_offset)
5213 return FALSE;
5214
5215 if (src1->r_rel.virtual_offset != src2->r_rel.virtual_offset)
5216 return FALSE;
5217
5218 if (src1->value != src2->value)
5219 return FALSE;
5220
5221 /* Now check for the same section (if defined) or the same elf_hash
5222 (if undefined or weak). */
5223 h1 = r_reloc_get_hash_entry (&src1->r_rel);
5224 h2 = r_reloc_get_hash_entry (&src2->r_rel);
5225 if (r_reloc_is_defined (&src1->r_rel)
5226 && (final_static_link
5227 || ((!h1 || h1->root.type != bfd_link_hash_defweak)
5228 && (!h2 || h2->root.type != bfd_link_hash_defweak))))
5229 {
5230 if (r_reloc_get_section (&src1->r_rel)
5231 != r_reloc_get_section (&src2->r_rel))
5232 return FALSE;
5233 }
5234 else
5235 {
5236 /* Require that the hash entries (i.e., symbols) be identical. */
5237 if (h1 != h2 || h1 == 0)
5238 return FALSE;
5239 }
5240
5241 if (src1->is_abs_literal != src2->is_abs_literal)
5242 return FALSE;
5243
5244 return TRUE;
5245 }
5246
5247
5248 /* Must be power of 2. */
5249 #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
5250
5251 static value_map_hash_table *
5252 value_map_hash_table_init (void)
5253 {
5254 value_map_hash_table *values;
5255
5256 values = (value_map_hash_table *)
5257 bfd_zmalloc (sizeof (value_map_hash_table));
5258 values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
5259 values->count = 0;
5260 values->buckets = (value_map **)
5261 bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
5262 if (values->buckets == NULL)
5263 {
5264 free (values);
5265 return NULL;
5266 }
5267 values->has_last_loc = FALSE;
5268
5269 return values;
5270 }
5271
5272
5273 static void
5274 value_map_hash_table_delete (value_map_hash_table *table)
5275 {
5276 free (table->buckets);
5277 free (table);
5278 }
5279
5280
5281 static unsigned
5282 hash_bfd_vma (bfd_vma val)
5283 {
5284 return (val >> 2) + (val >> 10);
5285 }
5286
5287
5288 static unsigned
5289 literal_value_hash (const literal_value *src)
5290 {
5291 unsigned hash_val;
5292
5293 hash_val = hash_bfd_vma (src->value);
5294 if (!r_reloc_is_const (&src->r_rel))
5295 {
5296 void *sec_or_hash;
5297
5298 hash_val += hash_bfd_vma (src->is_abs_literal * 1000);
5299 hash_val += hash_bfd_vma (src->r_rel.target_offset);
5300 hash_val += hash_bfd_vma (src->r_rel.virtual_offset);
5301
5302 /* Now check for the same section and the same elf_hash. */
5303 if (r_reloc_is_defined (&src->r_rel))
5304 sec_or_hash = r_reloc_get_section (&src->r_rel);
5305 else
5306 sec_or_hash = r_reloc_get_hash_entry (&src->r_rel);
5307 hash_val += hash_bfd_vma ((bfd_vma) (size_t) sec_or_hash);
5308 }
5309 return hash_val;
5310 }
5311
5312
5313 /* Check if the specified literal_value has been seen before. */
5314
5315 static value_map *
5316 value_map_get_cached_value (value_map_hash_table *map,
5317 const literal_value *val,
5318 bfd_boolean final_static_link)
5319 {
5320 value_map *map_e;
5321 value_map *bucket;
5322 unsigned idx;
5323
5324 idx = literal_value_hash (val);
5325 idx = idx & (map->bucket_count - 1);
5326 bucket = map->buckets[idx];
5327 for (map_e = bucket; map_e; map_e = map_e->next)
5328 {
5329 if (literal_value_equal (&map_e->val, val, final_static_link))
5330 return map_e;
5331 }
5332 return NULL;
5333 }
5334
5335
5336 /* Record a new literal value. It is illegal to call this if VALUE
5337 already has an entry here. */
5338
5339 static value_map *
5340 add_value_map (value_map_hash_table *map,
5341 const literal_value *val,
5342 const r_reloc *loc,
5343 bfd_boolean final_static_link)
5344 {
5345 value_map **bucket_p;
5346 unsigned idx;
5347
5348 value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
5349 if (val_e == NULL)
5350 {
5351 bfd_set_error (bfd_error_no_memory);
5352 return NULL;
5353 }
5354
5355 BFD_ASSERT (!value_map_get_cached_value (map, val, final_static_link));
5356 val_e->val = *val;
5357 val_e->loc = *loc;
5358
5359 idx = literal_value_hash (val);
5360 idx = idx & (map->bucket_count - 1);
5361 bucket_p = &map->buckets[idx];
5362
5363 val_e->next = *bucket_p;
5364 *bucket_p = val_e;
5365 map->count++;
5366 /* FIXME: Consider resizing the hash table if we get too many entries. */
5367
5368 return val_e;
5369 }
5370
5371 \f
5372 /* Lists of text actions (ta_) for narrowing, widening, longcall
5373 conversion, space fill, code & literal removal, etc. */
5374
5375 /* The following text actions are generated:
5376
5377 "ta_remove_insn" remove an instruction or instructions
5378 "ta_remove_longcall" convert longcall to call
5379 "ta_convert_longcall" convert longcall to nop/call
5380 "ta_narrow_insn" narrow a wide instruction
5381 "ta_widen" widen a narrow instruction
5382 "ta_fill" add fill or remove fill
5383 removed < 0 is a fill; branches to the fill address will be
5384 changed to address + fill size (e.g., address - removed)
5385 removed >= 0 branches to the fill address will stay unchanged
5386 "ta_remove_literal" remove a literal; this action is
5387 indicated when a literal is removed
5388 or replaced.
5389 "ta_add_literal" insert a new literal; this action is
5390 indicated when a literal has been moved.
5391 It may use a virtual_offset because
5392 multiple literals can be placed at the
5393 same location.
5394
5395 For each of these text actions, we also record the number of bytes
5396 removed by performing the text action. In the case of a "ta_widen"
5397 or a "ta_fill" that adds space, the removed_bytes will be negative. */
5398
5399 typedef struct text_action_struct text_action;
5400 typedef struct text_action_list_struct text_action_list;
5401 typedef enum text_action_enum_t text_action_t;
5402
5403 enum text_action_enum_t
5404 {
5405 ta_none,
5406 ta_remove_insn, /* removed = -size */
5407 ta_remove_longcall, /* removed = -size */
5408 ta_convert_longcall, /* removed = 0 */
5409 ta_narrow_insn, /* removed = -1 */
5410 ta_widen_insn, /* removed = +1 */
5411 ta_fill, /* removed = +size */
5412 ta_remove_literal,
5413 ta_add_literal
5414 };
5415
5416
5417 /* Structure for a text action record. */
5418 struct text_action_struct
5419 {
5420 text_action_t action;
5421 asection *sec; /* Optional */
5422 bfd_vma offset;
5423 bfd_vma virtual_offset; /* Zero except for adding literals. */
5424 int removed_bytes;
5425 literal_value value; /* Only valid when adding literals. */
5426 };
5427
5428 struct removal_by_action_entry_struct
5429 {
5430 bfd_vma offset;
5431 int removed;
5432 int eq_removed;
5433 int eq_removed_before_fill;
5434 };
5435 typedef struct removal_by_action_entry_struct removal_by_action_entry;
5436
5437 struct removal_by_action_map_struct
5438 {
5439 unsigned n_entries;
5440 removal_by_action_entry *entry;
5441 };
5442 typedef struct removal_by_action_map_struct removal_by_action_map;
5443
5444
5445 /* List of all of the actions taken on a text section. */
5446 struct text_action_list_struct
5447 {
5448 unsigned count;
5449 splay_tree tree;
5450 removal_by_action_map map;
5451 };
5452
5453
5454 static text_action *
5455 find_fill_action (text_action_list *l, asection *sec, bfd_vma offset)
5456 {
5457 text_action a;
5458
5459 /* It is not necessary to fill at the end of a section. */
5460 if (sec->size == offset)
5461 return NULL;
5462
5463 a.offset = offset;
5464 a.action = ta_fill;
5465
5466 splay_tree_node node = splay_tree_lookup (l->tree, (splay_tree_key)&a);
5467 if (node)
5468 return (text_action *)node->value;
5469 return NULL;
5470 }
5471
5472
5473 static int
5474 compute_removed_action_diff (const text_action *ta,
5475 asection *sec,
5476 bfd_vma offset,
5477 int removed,
5478 int removable_space)
5479 {
5480 int new_removed;
5481 int current_removed = 0;
5482
5483 if (ta)
5484 current_removed = ta->removed_bytes;
5485
5486 BFD_ASSERT (ta == NULL || ta->offset == offset);
5487 BFD_ASSERT (ta == NULL || ta->action == ta_fill);
5488
5489 /* It is not necessary to fill at the end of a section. Clean this up. */
5490 if (sec->size == offset)
5491 new_removed = removable_space - 0;
5492 else
5493 {
5494 int space;
5495 int added = -removed - current_removed;
5496 /* Ignore multiples of the section alignment. */
5497 added = ((1 << sec->alignment_power) - 1) & added;
5498 new_removed = (-added);
5499
5500 /* Modify for removable. */
5501 space = removable_space - new_removed;
5502 new_removed = (removable_space
5503 - (((1 << sec->alignment_power) - 1) & space));
5504 }
5505 return (new_removed - current_removed);
5506 }
5507
5508
5509 static void
5510 adjust_fill_action (text_action *ta, int fill_diff)
5511 {
5512 ta->removed_bytes += fill_diff;
5513 }
5514
5515
5516 static int
5517 text_action_compare (splay_tree_key a, splay_tree_key b)
5518 {
5519 text_action *pa = (text_action *)a;
5520 text_action *pb = (text_action *)b;
5521 static const int action_priority[] =
5522 {
5523 [ta_fill] = 0,
5524 [ta_none] = 1,
5525 [ta_convert_longcall] = 2,
5526 [ta_narrow_insn] = 3,
5527 [ta_remove_insn] = 4,
5528 [ta_remove_longcall] = 5,
5529 [ta_remove_literal] = 6,
5530 [ta_widen_insn] = 7,
5531 [ta_add_literal] = 8,
5532 };
5533
5534 if (pa->offset == pb->offset)
5535 {
5536 if (pa->action == pb->action)
5537 return 0;
5538 return action_priority[pa->action] - action_priority[pb->action];
5539 }
5540 else
5541 return pa->offset < pb->offset ? -1 : 1;
5542 }
5543
5544 static text_action *
5545 action_first (text_action_list *action_list)
5546 {
5547 splay_tree_node node = splay_tree_min (action_list->tree);
5548 return node ? (text_action *)node->value : NULL;
5549 }
5550
5551 static text_action *
5552 action_next (text_action_list *action_list, text_action *action)
5553 {
5554 splay_tree_node node = splay_tree_successor (action_list->tree,
5555 (splay_tree_key)action);
5556 return node ? (text_action *)node->value : NULL;
5557 }
5558
5559 /* Add a modification action to the text. For the case of adding or
5560 removing space, modify any current fill and assume that
5561 "unreachable_space" bytes can be freely contracted. Note that a
5562 negative removed value is a fill. */
5563
5564 static void
5565 text_action_add (text_action_list *l,
5566 text_action_t action,
5567 asection *sec,
5568 bfd_vma offset,
5569 int removed)
5570 {
5571 text_action *ta;
5572 text_action a;
5573
5574 /* It is not necessary to fill at the end of a section. */
5575 if (action == ta_fill && sec->size == offset)
5576 return;
5577
5578 /* It is not necessary to fill 0 bytes. */
5579 if (action == ta_fill && removed == 0)
5580 return;
5581
5582 a.action = action;
5583 a.offset = offset;
5584
5585 if (action == ta_fill)
5586 {
5587 splay_tree_node node = splay_tree_lookup (l->tree, (splay_tree_key)&a);
5588
5589 if (node)
5590 {
5591 ta = (text_action *)node->value;
5592 ta->removed_bytes += removed;
5593 return;
5594 }
5595 }
5596 else
5597 BFD_ASSERT (splay_tree_lookup (l->tree, (splay_tree_key)&a) == NULL);
5598
5599 ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5600 ta->action = action;
5601 ta->sec = sec;
5602 ta->offset = offset;
5603 ta->removed_bytes = removed;
5604 splay_tree_insert (l->tree, (splay_tree_key)ta, (splay_tree_value)ta);
5605 ++l->count;
5606 }
5607
5608
5609 static void
5610 text_action_add_literal (text_action_list *l,
5611 text_action_t action,
5612 const r_reloc *loc,
5613 const literal_value *value,
5614 int removed)
5615 {
5616 text_action *ta;
5617 asection *sec = r_reloc_get_section (loc);
5618 bfd_vma offset = loc->target_offset;
5619 bfd_vma virtual_offset = loc->virtual_offset;
5620
5621 BFD_ASSERT (action == ta_add_literal);
5622
5623 /* Create a new record and fill it up. */
5624 ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5625 ta->action = action;
5626 ta->sec = sec;
5627 ta->offset = offset;
5628 ta->virtual_offset = virtual_offset;
5629 ta->value = *value;
5630 ta->removed_bytes = removed;
5631
5632 BFD_ASSERT (splay_tree_lookup (l->tree, (splay_tree_key)ta) == NULL);
5633 splay_tree_insert (l->tree, (splay_tree_key)ta, (splay_tree_value)ta);
5634 ++l->count;
5635 }
5636
5637
5638 /* Find the total offset adjustment for the relaxations specified by
5639 text_actions, beginning from a particular starting action. This is
5640 typically used from offset_with_removed_text to search an entire list of
5641 actions, but it may also be called directly when adjusting adjacent offsets
5642 so that each search may begin where the previous one left off. */
5643
5644 static int
5645 removed_by_actions (text_action_list *action_list,
5646 text_action **p_start_action,
5647 bfd_vma offset,
5648 bfd_boolean before_fill)
5649 {
5650 text_action *r;
5651 int removed = 0;
5652
5653 r = *p_start_action;
5654 if (r)
5655 {
5656 splay_tree_node node = splay_tree_lookup (action_list->tree,
5657 (splay_tree_key)r);
5658 BFD_ASSERT (node != NULL && r == (text_action *)node->value);
5659 }
5660
5661 while (r)
5662 {
5663 if (r->offset > offset)
5664 break;
5665
5666 if (r->offset == offset
5667 && (before_fill || r->action != ta_fill || r->removed_bytes >= 0))
5668 break;
5669
5670 removed += r->removed_bytes;
5671
5672 r = action_next (action_list, r);
5673 }
5674
5675 *p_start_action = r;
5676 return removed;
5677 }
5678
5679
5680 static bfd_vma
5681 offset_with_removed_text (text_action_list *action_list, bfd_vma offset)
5682 {
5683 text_action *r = action_first (action_list);
5684
5685 return offset - removed_by_actions (action_list, &r, offset, FALSE);
5686 }
5687
5688
5689 static unsigned
5690 action_list_count (text_action_list *action_list)
5691 {
5692 return action_list->count;
5693 }
5694
5695 typedef struct map_action_fn_context_struct map_action_fn_context;
5696 struct map_action_fn_context_struct
5697 {
5698 int removed;
5699 removal_by_action_map map;
5700 bfd_boolean eq_complete;
5701 };
5702
5703 static int
5704 map_action_fn (splay_tree_node node, void *p)
5705 {
5706 map_action_fn_context *ctx = p;
5707 text_action *r = (text_action *)node->value;
5708 removal_by_action_entry *ientry = ctx->map.entry + ctx->map.n_entries;
5709
5710 if (ctx->map.n_entries && (ientry - 1)->offset == r->offset)
5711 {
5712 --ientry;
5713 }
5714 else
5715 {
5716 ++ctx->map.n_entries;
5717 ctx->eq_complete = FALSE;
5718 ientry->offset = r->offset;
5719 ientry->eq_removed_before_fill = ctx->removed;
5720 }
5721
5722 if (!ctx->eq_complete)
5723 {
5724 if (r->action != ta_fill || r->removed_bytes >= 0)
5725 {
5726 ientry->eq_removed = ctx->removed;
5727 ctx->eq_complete = TRUE;
5728 }
5729 else
5730 ientry->eq_removed = ctx->removed + r->removed_bytes;
5731 }
5732
5733 ctx->removed += r->removed_bytes;
5734 ientry->removed = ctx->removed;
5735 return 0;
5736 }
5737
5738 static void
5739 map_removal_by_action (text_action_list *action_list)
5740 {
5741 map_action_fn_context ctx;
5742
5743 ctx.removed = 0;
5744 ctx.map.n_entries = 0;
5745 ctx.map.entry = bfd_malloc (action_list_count (action_list) *
5746 sizeof (removal_by_action_entry));
5747 ctx.eq_complete = FALSE;
5748
5749 splay_tree_foreach (action_list->tree, map_action_fn, &ctx);
5750 action_list->map = ctx.map;
5751 }
5752
5753 static int
5754 removed_by_actions_map (text_action_list *action_list, bfd_vma offset,
5755 bfd_boolean before_fill)
5756 {
5757 unsigned a, b;
5758
5759 if (!action_list->map.entry)
5760 map_removal_by_action (action_list);
5761
5762 if (!action_list->map.n_entries)
5763 return 0;
5764
5765 a = 0;
5766 b = action_list->map.n_entries;
5767
5768 while (b - a > 1)
5769 {
5770 unsigned c = (a + b) / 2;
5771
5772 if (action_list->map.entry[c].offset <= offset)
5773 a = c;
5774 else
5775 b = c;
5776 }
5777
5778 if (action_list->map.entry[a].offset < offset)
5779 {
5780 return action_list->map.entry[a].removed;
5781 }
5782 else if (action_list->map.entry[a].offset == offset)
5783 {
5784 return before_fill ?
5785 action_list->map.entry[a].eq_removed_before_fill :
5786 action_list->map.entry[a].eq_removed;
5787 }
5788 else
5789 {
5790 return 0;
5791 }
5792 }
5793
5794 static bfd_vma
5795 offset_with_removed_text_map (text_action_list *action_list, bfd_vma offset)
5796 {
5797 int removed = removed_by_actions_map (action_list, offset, FALSE);
5798 return offset - removed;
5799 }
5800
5801
5802 /* The find_insn_action routine will only find non-fill actions. */
5803
5804 static text_action *
5805 find_insn_action (text_action_list *action_list, bfd_vma offset)
5806 {
5807 static const text_action_t action[] =
5808 {
5809 ta_convert_longcall,
5810 ta_remove_longcall,
5811 ta_widen_insn,
5812 ta_narrow_insn,
5813 ta_remove_insn,
5814 };
5815 text_action a;
5816 unsigned i;
5817
5818 a.offset = offset;
5819 for (i = 0; i < sizeof (action) / sizeof (*action); ++i)
5820 {
5821 splay_tree_node node;
5822
5823 a.action = action[i];
5824 node = splay_tree_lookup (action_list->tree, (splay_tree_key)&a);
5825 if (node)
5826 return (text_action *)node->value;
5827 }
5828 return NULL;
5829 }
5830
5831
5832 #if DEBUG
5833
5834 static void
5835 print_action (FILE *fp, text_action *r)
5836 {
5837 const char *t = "unknown";
5838 switch (r->action)
5839 {
5840 case ta_remove_insn:
5841 t = "remove_insn"; break;
5842 case ta_remove_longcall:
5843 t = "remove_longcall"; break;
5844 case ta_convert_longcall:
5845 t = "convert_longcall"; break;
5846 case ta_narrow_insn:
5847 t = "narrow_insn"; break;
5848 case ta_widen_insn:
5849 t = "widen_insn"; break;
5850 case ta_fill:
5851 t = "fill"; break;
5852 case ta_none:
5853 t = "none"; break;
5854 case ta_remove_literal:
5855 t = "remove_literal"; break;
5856 case ta_add_literal:
5857 t = "add_literal"; break;
5858 }
5859
5860 fprintf (fp, "%s: %s[0x%lx] \"%s\" %d\n",
5861 r->sec->owner->filename,
5862 r->sec->name, (unsigned long) r->offset, t, r->removed_bytes);
5863 }
5864
5865 static int
5866 print_action_list_fn (splay_tree_node node, void *p)
5867 {
5868 text_action *r = (text_action *)node->value;
5869
5870 print_action (p, r);
5871 return 0;
5872 }
5873
5874 static void
5875 print_action_list (FILE *fp, text_action_list *action_list)
5876 {
5877 fprintf (fp, "Text Action\n");
5878 splay_tree_foreach (action_list->tree, print_action_list_fn, fp);
5879 }
5880
5881 #endif /* DEBUG */
5882
5883 \f
5884 /* Lists of literals being coalesced or removed. */
5885
5886 /* In the usual case, the literal identified by "from" is being
5887 coalesced with another literal identified by "to". If the literal is
5888 unused and is being removed altogether, "to.abfd" will be NULL.
5889 The removed_literal entries are kept on a per-section list, sorted
5890 by the "from" offset field. */
5891
5892 typedef struct removed_literal_struct removed_literal;
5893 typedef struct removed_literal_map_entry_struct removed_literal_map_entry;
5894 typedef struct removed_literal_list_struct removed_literal_list;
5895
5896 struct removed_literal_struct
5897 {
5898 r_reloc from;
5899 r_reloc to;
5900 removed_literal *next;
5901 };
5902
5903 struct removed_literal_map_entry_struct
5904 {
5905 bfd_vma addr;
5906 removed_literal *literal;
5907 };
5908
5909 struct removed_literal_list_struct
5910 {
5911 removed_literal *head;
5912 removed_literal *tail;
5913
5914 unsigned n_map;
5915 removed_literal_map_entry *map;
5916 };
5917
5918
5919 /* Record that the literal at "from" is being removed. If "to" is not
5920 NULL, the "from" literal is being coalesced with the "to" literal. */
5921
5922 static void
5923 add_removed_literal (removed_literal_list *removed_list,
5924 const r_reloc *from,
5925 const r_reloc *to)
5926 {
5927 removed_literal *r, *new_r, *next_r;
5928
5929 new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
5930
5931 new_r->from = *from;
5932 if (to)
5933 new_r->to = *to;
5934 else
5935 new_r->to.abfd = NULL;
5936 new_r->next = NULL;
5937
5938 r = removed_list->head;
5939 if (r == NULL)
5940 {
5941 removed_list->head = new_r;
5942 removed_list->tail = new_r;
5943 }
5944 /* Special check for common case of append. */
5945 else if (removed_list->tail->from.target_offset < from->target_offset)
5946 {
5947 removed_list->tail->next = new_r;
5948 removed_list->tail = new_r;
5949 }
5950 else
5951 {
5952 while (r->from.target_offset < from->target_offset && r->next)
5953 {
5954 r = r->next;
5955 }
5956 next_r = r->next;
5957 r->next = new_r;
5958 new_r->next = next_r;
5959 if (next_r == NULL)
5960 removed_list->tail = new_r;
5961 }
5962 }
5963
5964 static void
5965 map_removed_literal (removed_literal_list *removed_list)
5966 {
5967 unsigned n_map = 0;
5968 unsigned i;
5969 removed_literal_map_entry *map = NULL;
5970 removed_literal *r = removed_list->head;
5971
5972 for (i = 0; r; ++i, r = r->next)
5973 {
5974 if (i == n_map)
5975 {
5976 n_map = (n_map * 2) + 2;
5977 map = bfd_realloc (map, n_map * sizeof (*map));
5978 }
5979 map[i].addr = r->from.target_offset;
5980 map[i].literal = r;
5981 }
5982 removed_list->map = map;
5983 removed_list->n_map = i;
5984 }
5985
5986 static int
5987 removed_literal_compare (const void *a, const void *b)
5988 {
5989 const removed_literal_map_entry *pa = a;
5990 const removed_literal_map_entry *pb = b;
5991
5992 if (pa->addr == pb->addr)
5993 return 0;
5994 else
5995 return pa->addr < pb->addr ? -1 : 1;
5996 }
5997
5998 /* Check if the list of removed literals contains an entry for the
5999 given address. Return the entry if found. */
6000
6001 static removed_literal *
6002 find_removed_literal (removed_literal_list *removed_list, bfd_vma addr)
6003 {
6004 removed_literal_map_entry *p;
6005 removed_literal *r = NULL;
6006
6007 if (removed_list->map == NULL)
6008 map_removed_literal (removed_list);
6009
6010 p = bsearch (&addr, removed_list->map, removed_list->n_map,
6011 sizeof (*removed_list->map), removed_literal_compare);
6012 if (p)
6013 {
6014 while (p != removed_list->map && (p - 1)->addr == addr)
6015 --p;
6016 r = p->literal;
6017 }
6018 return r;
6019 }
6020
6021
6022 #if DEBUG
6023
6024 static void
6025 print_removed_literals (FILE *fp, removed_literal_list *removed_list)
6026 {
6027 removed_literal *r;
6028 r = removed_list->head;
6029 if (r)
6030 fprintf (fp, "Removed Literals\n");
6031 for (; r != NULL; r = r->next)
6032 {
6033 print_r_reloc (fp, &r->from);
6034 fprintf (fp, " => ");
6035 if (r->to.abfd == NULL)
6036 fprintf (fp, "REMOVED");
6037 else
6038 print_r_reloc (fp, &r->to);
6039 fprintf (fp, "\n");
6040 }
6041 }
6042
6043 #endif /* DEBUG */
6044
6045 \f
6046 /* Per-section data for relaxation. */
6047
6048 typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
6049
6050 struct xtensa_relax_info_struct
6051 {
6052 bfd_boolean is_relaxable_literal_section;
6053 bfd_boolean is_relaxable_asm_section;
6054 int visited; /* Number of times visited. */
6055
6056 source_reloc *src_relocs; /* Array[src_count]. */
6057 int src_count;
6058 int src_next; /* Next src_relocs entry to assign. */
6059
6060 removed_literal_list removed_list;
6061 text_action_list action_list;
6062
6063 reloc_bfd_fix *fix_list;
6064 reloc_bfd_fix *fix_array;
6065 unsigned fix_array_count;
6066
6067 /* Support for expanding the reloc array that is stored
6068 in the section structure. If the relocations have been
6069 reallocated, the newly allocated relocations will be referenced
6070 here along with the actual size allocated. The relocation
6071 count will always be found in the section structure. */
6072 Elf_Internal_Rela *allocated_relocs;
6073 unsigned relocs_count;
6074 unsigned allocated_relocs_count;
6075 };
6076
6077 struct elf_xtensa_section_data
6078 {
6079 struct bfd_elf_section_data elf;
6080 xtensa_relax_info relax_info;
6081 };
6082
6083
6084 static bfd_boolean
6085 elf_xtensa_new_section_hook (bfd *abfd, asection *sec)
6086 {
6087 if (!sec->used_by_bfd)
6088 {
6089 struct elf_xtensa_section_data *sdata;
6090 bfd_size_type amt = sizeof (*sdata);
6091
6092 sdata = bfd_zalloc (abfd, amt);
6093 if (sdata == NULL)
6094 return FALSE;
6095 sec->used_by_bfd = sdata;
6096 }
6097
6098 return _bfd_elf_new_section_hook (abfd, sec);
6099 }
6100
6101
6102 static xtensa_relax_info *
6103 get_xtensa_relax_info (asection *sec)
6104 {
6105 struct elf_xtensa_section_data *section_data;
6106
6107 /* No info available if no section or if it is an output section. */
6108 if (!sec || sec == sec->output_section)
6109 return NULL;
6110
6111 section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
6112 return &section_data->relax_info;
6113 }
6114
6115
6116 static void
6117 init_xtensa_relax_info (asection *sec)
6118 {
6119 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6120
6121 relax_info->is_relaxable_literal_section = FALSE;
6122 relax_info->is_relaxable_asm_section = FALSE;
6123 relax_info->visited = 0;
6124
6125 relax_info->src_relocs = NULL;
6126 relax_info->src_count = 0;
6127 relax_info->src_next = 0;
6128
6129 relax_info->removed_list.head = NULL;
6130 relax_info->removed_list.tail = NULL;
6131
6132 relax_info->action_list.tree = splay_tree_new (text_action_compare,
6133 NULL, NULL);
6134 relax_info->action_list.map.n_entries = 0;
6135 relax_info->action_list.map.entry = NULL;
6136
6137 relax_info->fix_list = NULL;
6138 relax_info->fix_array = NULL;
6139 relax_info->fix_array_count = 0;
6140
6141 relax_info->allocated_relocs = NULL;
6142 relax_info->relocs_count = 0;
6143 relax_info->allocated_relocs_count = 0;
6144 }
6145
6146 \f
6147 /* Coalescing literals may require a relocation to refer to a section in
6148 a different input file, but the standard relocation information
6149 cannot express that. Instead, the reloc_bfd_fix structures are used
6150 to "fix" the relocations that refer to sections in other input files.
6151 These structures are kept on per-section lists. The "src_type" field
6152 records the relocation type in case there are multiple relocations on
6153 the same location. FIXME: This is ugly; an alternative might be to
6154 add new symbols with the "owner" field to some other input file. */
6155
6156 struct reloc_bfd_fix_struct
6157 {
6158 asection *src_sec;
6159 bfd_vma src_offset;
6160 unsigned src_type; /* Relocation type. */
6161
6162 asection *target_sec;
6163 bfd_vma target_offset;
6164 bfd_boolean translated;
6165
6166 reloc_bfd_fix *next;
6167 };
6168
6169
6170 static reloc_bfd_fix *
6171 reloc_bfd_fix_init (asection *src_sec,
6172 bfd_vma src_offset,
6173 unsigned src_type,
6174 asection *target_sec,
6175 bfd_vma target_offset,
6176 bfd_boolean translated)
6177 {
6178 reloc_bfd_fix *fix;
6179
6180 fix = (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix));
6181 fix->src_sec = src_sec;
6182 fix->src_offset = src_offset;
6183 fix->src_type = src_type;
6184 fix->target_sec = target_sec;
6185 fix->target_offset = target_offset;
6186 fix->translated = translated;
6187
6188 return fix;
6189 }
6190
6191
6192 static void
6193 add_fix (asection *src_sec, reloc_bfd_fix *fix)
6194 {
6195 xtensa_relax_info *relax_info;
6196
6197 relax_info = get_xtensa_relax_info (src_sec);
6198 fix->next = relax_info->fix_list;
6199 relax_info->fix_list = fix;
6200 }
6201
6202
6203 static int
6204 fix_compare (const void *ap, const void *bp)
6205 {
6206 const reloc_bfd_fix *a = (const reloc_bfd_fix *) ap;
6207 const reloc_bfd_fix *b = (const reloc_bfd_fix *) bp;
6208
6209 if (a->src_offset != b->src_offset)
6210 return (a->src_offset - b->src_offset);
6211 return (a->src_type - b->src_type);
6212 }
6213
6214
6215 static void
6216 cache_fix_array (asection *sec)
6217 {
6218 unsigned i, count = 0;
6219 reloc_bfd_fix *r;
6220 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6221
6222 if (relax_info == NULL)
6223 return;
6224 if (relax_info->fix_list == NULL)
6225 return;
6226
6227 for (r = relax_info->fix_list; r != NULL; r = r->next)
6228 count++;
6229
6230 relax_info->fix_array =
6231 (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix) * count);
6232 relax_info->fix_array_count = count;
6233
6234 r = relax_info->fix_list;
6235 for (i = 0; i < count; i++, r = r->next)
6236 {
6237 relax_info->fix_array[count - 1 - i] = *r;
6238 relax_info->fix_array[count - 1 - i].next = NULL;
6239 }
6240
6241 qsort (relax_info->fix_array, relax_info->fix_array_count,
6242 sizeof (reloc_bfd_fix), fix_compare);
6243 }
6244
6245
6246 static reloc_bfd_fix *
6247 get_bfd_fix (asection *sec, bfd_vma offset, unsigned type)
6248 {
6249 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6250 reloc_bfd_fix *rv;
6251 reloc_bfd_fix key;
6252
6253 if (relax_info == NULL)
6254 return NULL;
6255 if (relax_info->fix_list == NULL)
6256 return NULL;
6257
6258 if (relax_info->fix_array == NULL)
6259 cache_fix_array (sec);
6260
6261 key.src_offset = offset;
6262 key.src_type = type;
6263 rv = bsearch (&key, relax_info->fix_array, relax_info->fix_array_count,
6264 sizeof (reloc_bfd_fix), fix_compare);
6265 return rv;
6266 }
6267
6268 \f
6269 /* Section caching. */
6270
6271 typedef struct section_cache_struct section_cache_t;
6272
6273 struct section_cache_struct
6274 {
6275 asection *sec;
6276
6277 bfd_byte *contents; /* Cache of the section contents. */
6278 bfd_size_type content_length;
6279
6280 property_table_entry *ptbl; /* Cache of the section property table. */
6281 unsigned pte_count;
6282
6283 Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
6284 unsigned reloc_count;
6285 };
6286
6287
6288 static void
6289 init_section_cache (section_cache_t *sec_cache)
6290 {
6291 memset (sec_cache, 0, sizeof (*sec_cache));
6292 }
6293
6294
6295 static void
6296 free_section_cache (section_cache_t *sec_cache)
6297 {
6298 if (sec_cache->sec)
6299 {
6300 release_contents (sec_cache->sec, sec_cache->contents);
6301 release_internal_relocs (sec_cache->sec, sec_cache->relocs);
6302 if (sec_cache->ptbl)
6303 free (sec_cache->ptbl);
6304 }
6305 }
6306
6307
6308 static bfd_boolean
6309 section_cache_section (section_cache_t *sec_cache,
6310 asection *sec,
6311 struct bfd_link_info *link_info)
6312 {
6313 bfd *abfd;
6314 property_table_entry *prop_table = NULL;
6315 int ptblsize = 0;
6316 bfd_byte *contents = NULL;
6317 Elf_Internal_Rela *internal_relocs = NULL;
6318 bfd_size_type sec_size;
6319
6320 if (sec == NULL)
6321 return FALSE;
6322 if (sec == sec_cache->sec)
6323 return TRUE;
6324
6325 abfd = sec->owner;
6326 sec_size = bfd_get_section_limit (abfd, sec);
6327
6328 /* Get the contents. */
6329 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
6330 if (contents == NULL && sec_size != 0)
6331 goto err;
6332
6333 /* Get the relocations. */
6334 internal_relocs = retrieve_internal_relocs (abfd, sec,
6335 link_info->keep_memory);
6336
6337 /* Get the entry table. */
6338 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
6339 XTENSA_PROP_SEC_NAME, FALSE);
6340 if (ptblsize < 0)
6341 goto err;
6342
6343 /* Fill in the new section cache. */
6344 free_section_cache (sec_cache);
6345 init_section_cache (sec_cache);
6346
6347 sec_cache->sec = sec;
6348 sec_cache->contents = contents;
6349 sec_cache->content_length = sec_size;
6350 sec_cache->relocs = internal_relocs;
6351 sec_cache->reloc_count = sec->reloc_count;
6352 sec_cache->pte_count = ptblsize;
6353 sec_cache->ptbl = prop_table;
6354
6355 return TRUE;
6356
6357 err:
6358 release_contents (sec, contents);
6359 release_internal_relocs (sec, internal_relocs);
6360 if (prop_table)
6361 free (prop_table);
6362 return FALSE;
6363 }
6364
6365 \f
6366 /* Extended basic blocks. */
6367
6368 /* An ebb_struct represents an Extended Basic Block. Within this
6369 range, we guarantee that all instructions are decodable, the
6370 property table entries are contiguous, and no property table
6371 specifies a segment that cannot have instructions moved. This
6372 structure contains caches of the contents, property table and
6373 relocations for the specified section for easy use. The range is
6374 specified by ranges of indices for the byte offset, property table
6375 offsets and relocation offsets. These must be consistent. */
6376
6377 typedef struct ebb_struct ebb_t;
6378
6379 struct ebb_struct
6380 {
6381 asection *sec;
6382
6383 bfd_byte *contents; /* Cache of the section contents. */
6384 bfd_size_type content_length;
6385
6386 property_table_entry *ptbl; /* Cache of the section property table. */
6387 unsigned pte_count;
6388
6389 Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
6390 unsigned reloc_count;
6391
6392 bfd_vma start_offset; /* Offset in section. */
6393 unsigned start_ptbl_idx; /* Offset in the property table. */
6394 unsigned start_reloc_idx; /* Offset in the relocations. */
6395
6396 bfd_vma end_offset;
6397 unsigned end_ptbl_idx;
6398 unsigned end_reloc_idx;
6399
6400 bfd_boolean ends_section; /* Is this the last ebb in a section? */
6401
6402 /* The unreachable property table at the end of this set of blocks;
6403 NULL if the end is not an unreachable block. */
6404 property_table_entry *ends_unreachable;
6405 };
6406
6407
6408 enum ebb_target_enum
6409 {
6410 EBB_NO_ALIGN = 0,
6411 EBB_DESIRE_TGT_ALIGN,
6412 EBB_REQUIRE_TGT_ALIGN,
6413 EBB_REQUIRE_LOOP_ALIGN,
6414 EBB_REQUIRE_ALIGN
6415 };
6416
6417
6418 /* proposed_action_struct is similar to the text_action_struct except
6419 that is represents a potential transformation, not one that will
6420 occur. We build a list of these for an extended basic block
6421 and use them to compute the actual actions desired. We must be
6422 careful that the entire set of actual actions we perform do not
6423 break any relocations that would fit if the actions were not
6424 performed. */
6425
6426 typedef struct proposed_action_struct proposed_action;
6427
6428 struct proposed_action_struct
6429 {
6430 enum ebb_target_enum align_type; /* for the target alignment */
6431 bfd_vma alignment_pow;
6432 text_action_t action;
6433 bfd_vma offset;
6434 int removed_bytes;
6435 bfd_boolean do_action; /* If false, then we will not perform the action. */
6436 };
6437
6438
6439 /* The ebb_constraint_struct keeps a set of proposed actions for an
6440 extended basic block. */
6441
6442 typedef struct ebb_constraint_struct ebb_constraint;
6443
6444 struct ebb_constraint_struct
6445 {
6446 ebb_t ebb;
6447 bfd_boolean start_movable;
6448
6449 /* Bytes of extra space at the beginning if movable. */
6450 int start_extra_space;
6451
6452 enum ebb_target_enum start_align;
6453
6454 bfd_boolean end_movable;
6455
6456 /* Bytes of extra space at the end if movable. */
6457 int end_extra_space;
6458
6459 unsigned action_count;
6460 unsigned action_allocated;
6461
6462 /* Array of proposed actions. */
6463 proposed_action *actions;
6464
6465 /* Action alignments -- one for each proposed action. */
6466 enum ebb_target_enum *action_aligns;
6467 };
6468
6469
6470 static void
6471 init_ebb_constraint (ebb_constraint *c)
6472 {
6473 memset (c, 0, sizeof (ebb_constraint));
6474 }
6475
6476
6477 static void
6478 free_ebb_constraint (ebb_constraint *c)
6479 {
6480 if (c->actions)
6481 free (c->actions);
6482 }
6483
6484
6485 static void
6486 init_ebb (ebb_t *ebb,
6487 asection *sec,
6488 bfd_byte *contents,
6489 bfd_size_type content_length,
6490 property_table_entry *prop_table,
6491 unsigned ptblsize,
6492 Elf_Internal_Rela *internal_relocs,
6493 unsigned reloc_count)
6494 {
6495 memset (ebb, 0, sizeof (ebb_t));
6496 ebb->sec = sec;
6497 ebb->contents = contents;
6498 ebb->content_length = content_length;
6499 ebb->ptbl = prop_table;
6500 ebb->pte_count = ptblsize;
6501 ebb->relocs = internal_relocs;
6502 ebb->reloc_count = reloc_count;
6503 ebb->start_offset = 0;
6504 ebb->end_offset = ebb->content_length - 1;
6505 ebb->start_ptbl_idx = 0;
6506 ebb->end_ptbl_idx = ptblsize;
6507 ebb->start_reloc_idx = 0;
6508 ebb->end_reloc_idx = reloc_count;
6509 }
6510
6511
6512 /* Extend the ebb to all decodable contiguous sections. The algorithm
6513 for building a basic block around an instruction is to push it
6514 forward until we hit the end of a section, an unreachable block or
6515 a block that cannot be transformed. Then we push it backwards
6516 searching for similar conditions. */
6517
6518 static bfd_boolean extend_ebb_bounds_forward (ebb_t *);
6519 static bfd_boolean extend_ebb_bounds_backward (ebb_t *);
6520 static bfd_size_type insn_block_decodable_len
6521 (bfd_byte *, bfd_size_type, bfd_vma, bfd_size_type);
6522
6523 static bfd_boolean
6524 extend_ebb_bounds (ebb_t *ebb)
6525 {
6526 if (!extend_ebb_bounds_forward (ebb))
6527 return FALSE;
6528 if (!extend_ebb_bounds_backward (ebb))
6529 return FALSE;
6530 return TRUE;
6531 }
6532
6533
6534 static bfd_boolean
6535 extend_ebb_bounds_forward (ebb_t *ebb)
6536 {
6537 property_table_entry *the_entry, *new_entry;
6538
6539 the_entry = &ebb->ptbl[ebb->end_ptbl_idx];
6540
6541 /* Stop when (1) we cannot decode an instruction, (2) we are at
6542 the end of the property tables, (3) we hit a non-contiguous property
6543 table entry, (4) we hit a NO_TRANSFORM region. */
6544
6545 while (1)
6546 {
6547 bfd_vma entry_end;
6548 bfd_size_type insn_block_len;
6549
6550 entry_end = the_entry->address - ebb->sec->vma + the_entry->size;
6551 insn_block_len =
6552 insn_block_decodable_len (ebb->contents, ebb->content_length,
6553 ebb->end_offset,
6554 entry_end - ebb->end_offset);
6555 if (insn_block_len != (entry_end - ebb->end_offset))
6556 {
6557 _bfd_error_handler
6558 /* xgettext:c-format */
6559 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6560 ebb->sec->owner, ebb->sec, ebb->end_offset + insn_block_len);
6561 return FALSE;
6562 }
6563 ebb->end_offset += insn_block_len;
6564
6565 if (ebb->end_offset == ebb->sec->size)
6566 ebb->ends_section = TRUE;
6567
6568 /* Update the reloc counter. */
6569 while (ebb->end_reloc_idx + 1 < ebb->reloc_count
6570 && (ebb->relocs[ebb->end_reloc_idx + 1].r_offset
6571 < ebb->end_offset))
6572 {
6573 ebb->end_reloc_idx++;
6574 }
6575
6576 if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
6577 return TRUE;
6578
6579 new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
6580 if (((new_entry->flags & XTENSA_PROP_INSN) == 0)
6581 || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
6582 || ((the_entry->flags & XTENSA_PROP_ALIGN) != 0))
6583 break;
6584
6585 if (the_entry->address + the_entry->size != new_entry->address)
6586 break;
6587
6588 the_entry = new_entry;
6589 ebb->end_ptbl_idx++;
6590 }
6591
6592 /* Quick check for an unreachable or end of file just at the end. */
6593 if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
6594 {
6595 if (ebb->end_offset == ebb->content_length)
6596 ebb->ends_section = TRUE;
6597 }
6598 else
6599 {
6600 new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
6601 if ((new_entry->flags & XTENSA_PROP_UNREACHABLE) != 0
6602 && the_entry->address + the_entry->size == new_entry->address)
6603 ebb->ends_unreachable = new_entry;
6604 }
6605
6606 /* Any other ending requires exact alignment. */
6607 return TRUE;
6608 }
6609
6610
6611 static bfd_boolean
6612 extend_ebb_bounds_backward (ebb_t *ebb)
6613 {
6614 property_table_entry *the_entry, *new_entry;
6615
6616 the_entry = &ebb->ptbl[ebb->start_ptbl_idx];
6617
6618 /* Stop when (1) we cannot decode the instructions in the current entry.
6619 (2) we are at the beginning of the property tables, (3) we hit a
6620 non-contiguous property table entry, (4) we hit a NO_TRANSFORM region. */
6621
6622 while (1)
6623 {
6624 bfd_vma block_begin;
6625 bfd_size_type insn_block_len;
6626
6627 block_begin = the_entry->address - ebb->sec->vma;
6628 insn_block_len =
6629 insn_block_decodable_len (ebb->contents, ebb->content_length,
6630 block_begin,
6631 ebb->start_offset - block_begin);
6632 if (insn_block_len != ebb->start_offset - block_begin)
6633 {
6634 _bfd_error_handler
6635 /* xgettext:c-format */
6636 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6637 ebb->sec->owner, ebb->sec, ebb->end_offset + insn_block_len);
6638 return FALSE;
6639 }
6640 ebb->start_offset -= insn_block_len;
6641
6642 /* Update the reloc counter. */
6643 while (ebb->start_reloc_idx > 0
6644 && (ebb->relocs[ebb->start_reloc_idx - 1].r_offset
6645 >= ebb->start_offset))
6646 {
6647 ebb->start_reloc_idx--;
6648 }
6649
6650 if (ebb->start_ptbl_idx == 0)
6651 return TRUE;
6652
6653 new_entry = &ebb->ptbl[ebb->start_ptbl_idx - 1];
6654 if ((new_entry->flags & XTENSA_PROP_INSN) == 0
6655 || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
6656 || ((new_entry->flags & XTENSA_PROP_ALIGN) != 0))
6657 return TRUE;
6658 if (new_entry->address + new_entry->size != the_entry->address)
6659 return TRUE;
6660
6661 the_entry = new_entry;
6662 ebb->start_ptbl_idx--;
6663 }
6664 return TRUE;
6665 }
6666
6667
6668 static bfd_size_type
6669 insn_block_decodable_len (bfd_byte *contents,
6670 bfd_size_type content_len,
6671 bfd_vma block_offset,
6672 bfd_size_type block_len)
6673 {
6674 bfd_vma offset = block_offset;
6675
6676 while (offset < block_offset + block_len)
6677 {
6678 bfd_size_type insn_len = 0;
6679
6680 insn_len = insn_decode_len (contents, content_len, offset);
6681 if (insn_len == 0)
6682 return (offset - block_offset);
6683 offset += insn_len;
6684 }
6685 return (offset - block_offset);
6686 }
6687
6688
6689 static void
6690 ebb_propose_action (ebb_constraint *c,
6691 enum ebb_target_enum align_type,
6692 bfd_vma alignment_pow,
6693 text_action_t action,
6694 bfd_vma offset,
6695 int removed_bytes,
6696 bfd_boolean do_action)
6697 {
6698 proposed_action *act;
6699
6700 if (c->action_allocated <= c->action_count)
6701 {
6702 unsigned new_allocated, i;
6703 proposed_action *new_actions;
6704
6705 new_allocated = (c->action_count + 2) * 2;
6706 new_actions = (proposed_action *)
6707 bfd_zmalloc (sizeof (proposed_action) * new_allocated);
6708
6709 for (i = 0; i < c->action_count; i++)
6710 new_actions[i] = c->actions[i];
6711 if (c->actions)
6712 free (c->actions);
6713 c->actions = new_actions;
6714 c->action_allocated = new_allocated;
6715 }
6716
6717 act = &c->actions[c->action_count];
6718 act->align_type = align_type;
6719 act->alignment_pow = alignment_pow;
6720 act->action = action;
6721 act->offset = offset;
6722 act->removed_bytes = removed_bytes;
6723 act->do_action = do_action;
6724
6725 c->action_count++;
6726 }
6727
6728 \f
6729 /* Access to internal relocations, section contents and symbols. */
6730
6731 /* During relaxation, we need to modify relocations, section contents,
6732 and symbol definitions, and we need to keep the original values from
6733 being reloaded from the input files, i.e., we need to "pin" the
6734 modified values in memory. We also want to continue to observe the
6735 setting of the "keep-memory" flag. The following functions wrap the
6736 standard BFD functions to take care of this for us. */
6737
6738 static Elf_Internal_Rela *
6739 retrieve_internal_relocs (bfd *abfd, asection *sec, bfd_boolean keep_memory)
6740 {
6741 Elf_Internal_Rela *internal_relocs;
6742
6743 if ((sec->flags & SEC_LINKER_CREATED) != 0)
6744 return NULL;
6745
6746 internal_relocs = elf_section_data (sec)->relocs;
6747 if (internal_relocs == NULL)
6748 internal_relocs = (_bfd_elf_link_read_relocs
6749 (abfd, sec, NULL, NULL, keep_memory));
6750 return internal_relocs;
6751 }
6752
6753
6754 static void
6755 pin_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
6756 {
6757 elf_section_data (sec)->relocs = internal_relocs;
6758 }
6759
6760
6761 static void
6762 release_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
6763 {
6764 if (internal_relocs
6765 && elf_section_data (sec)->relocs != internal_relocs)
6766 free (internal_relocs);
6767 }
6768
6769
6770 static bfd_byte *
6771 retrieve_contents (bfd *abfd, asection *sec, bfd_boolean keep_memory)
6772 {
6773 bfd_byte *contents;
6774 bfd_size_type sec_size;
6775
6776 sec_size = bfd_get_section_limit (abfd, sec);
6777 contents = elf_section_data (sec)->this_hdr.contents;
6778
6779 if (contents == NULL && sec_size != 0)
6780 {
6781 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
6782 {
6783 if (contents)
6784 free (contents);
6785 return NULL;
6786 }
6787 if (keep_memory)
6788 elf_section_data (sec)->this_hdr.contents = contents;
6789 }
6790 return contents;
6791 }
6792
6793
6794 static void
6795 pin_contents (asection *sec, bfd_byte *contents)
6796 {
6797 elf_section_data (sec)->this_hdr.contents = contents;
6798 }
6799
6800
6801 static void
6802 release_contents (asection *sec, bfd_byte *contents)
6803 {
6804 if (contents && elf_section_data (sec)->this_hdr.contents != contents)
6805 free (contents);
6806 }
6807
6808
6809 static Elf_Internal_Sym *
6810 retrieve_local_syms (bfd *input_bfd)
6811 {
6812 Elf_Internal_Shdr *symtab_hdr;
6813 Elf_Internal_Sym *isymbuf;
6814 size_t locsymcount;
6815
6816 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6817 locsymcount = symtab_hdr->sh_info;
6818
6819 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6820 if (isymbuf == NULL && locsymcount != 0)
6821 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6822 NULL, NULL, NULL);
6823
6824 /* Save the symbols for this input file so they won't be read again. */
6825 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
6826 symtab_hdr->contents = (unsigned char *) isymbuf;
6827
6828 return isymbuf;
6829 }
6830
6831 \f
6832 /* Code for link-time relaxation. */
6833
6834 /* Initialization for relaxation: */
6835 static bfd_boolean analyze_relocations (struct bfd_link_info *);
6836 static bfd_boolean find_relaxable_sections
6837 (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
6838 static bfd_boolean collect_source_relocs
6839 (bfd *, asection *, struct bfd_link_info *);
6840 static bfd_boolean is_resolvable_asm_expansion
6841 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, struct bfd_link_info *,
6842 bfd_boolean *);
6843 static Elf_Internal_Rela *find_associated_l32r_irel
6844 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Rela *);
6845 static bfd_boolean compute_text_actions
6846 (bfd *, asection *, struct bfd_link_info *);
6847 static bfd_boolean compute_ebb_proposed_actions (ebb_constraint *);
6848 static bfd_boolean compute_ebb_actions (ebb_constraint *);
6849 typedef struct reloc_range_list_struct reloc_range_list;
6850 static bfd_boolean check_section_ebb_pcrels_fit
6851 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *,
6852 reloc_range_list *, const ebb_constraint *,
6853 const xtensa_opcode *);
6854 static bfd_boolean check_section_ebb_reduces (const ebb_constraint *);
6855 static void text_action_add_proposed
6856 (text_action_list *, const ebb_constraint *, asection *);
6857 static int compute_fill_extra_space (property_table_entry *);
6858
6859 /* First pass: */
6860 static bfd_boolean compute_removed_literals
6861 (bfd *, asection *, struct bfd_link_info *, value_map_hash_table *);
6862 static Elf_Internal_Rela *get_irel_at_offset
6863 (asection *, Elf_Internal_Rela *, bfd_vma);
6864 static bfd_boolean is_removable_literal
6865 (const source_reloc *, int, const source_reloc *, int, asection *,
6866 property_table_entry *, int);
6867 static bfd_boolean remove_dead_literal
6868 (bfd *, asection *, struct bfd_link_info *, Elf_Internal_Rela *,
6869 Elf_Internal_Rela *, source_reloc *, property_table_entry *, int);
6870 static bfd_boolean identify_literal_placement
6871 (bfd *, asection *, bfd_byte *, struct bfd_link_info *,
6872 value_map_hash_table *, bfd_boolean *, Elf_Internal_Rela *, int,
6873 source_reloc *, property_table_entry *, int, section_cache_t *,
6874 bfd_boolean);
6875 static bfd_boolean relocations_reach (source_reloc *, int, const r_reloc *);
6876 static bfd_boolean coalesce_shared_literal
6877 (asection *, source_reloc *, property_table_entry *, int, value_map *);
6878 static bfd_boolean move_shared_literal
6879 (asection *, struct bfd_link_info *, source_reloc *, property_table_entry *,
6880 int, const r_reloc *, const literal_value *, section_cache_t *);
6881
6882 /* Second pass: */
6883 static bfd_boolean relax_section (bfd *, asection *, struct bfd_link_info *);
6884 static bfd_boolean translate_section_fixes (asection *);
6885 static bfd_boolean translate_reloc_bfd_fix (reloc_bfd_fix *);
6886 static asection *translate_reloc (const r_reloc *, r_reloc *, asection *);
6887 static void shrink_dynamic_reloc_sections
6888 (struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *);
6889 static bfd_boolean move_literal
6890 (bfd *, struct bfd_link_info *, asection *, bfd_vma, bfd_byte *,
6891 xtensa_relax_info *, Elf_Internal_Rela **, const literal_value *);
6892 static bfd_boolean relax_property_section
6893 (bfd *, asection *, struct bfd_link_info *);
6894
6895 /* Third pass: */
6896 static bfd_boolean relax_section_symbols (bfd *, asection *);
6897
6898
6899 static bfd_boolean
6900 elf_xtensa_relax_section (bfd *abfd,
6901 asection *sec,
6902 struct bfd_link_info *link_info,
6903 bfd_boolean *again)
6904 {
6905 static value_map_hash_table *values = NULL;
6906 static bfd_boolean relocations_analyzed = FALSE;
6907 xtensa_relax_info *relax_info;
6908
6909 if (!relocations_analyzed)
6910 {
6911 /* Do some overall initialization for relaxation. */
6912 values = value_map_hash_table_init ();
6913 if (values == NULL)
6914 return FALSE;
6915 relaxing_section = TRUE;
6916 if (!analyze_relocations (link_info))
6917 return FALSE;
6918 relocations_analyzed = TRUE;
6919 }
6920 *again = FALSE;
6921
6922 /* Don't mess with linker-created sections. */
6923 if ((sec->flags & SEC_LINKER_CREATED) != 0)
6924 return TRUE;
6925
6926 relax_info = get_xtensa_relax_info (sec);
6927 BFD_ASSERT (relax_info != NULL);
6928
6929 switch (relax_info->visited)
6930 {
6931 case 0:
6932 /* Note: It would be nice to fold this pass into
6933 analyze_relocations, but it is important for this step that the
6934 sections be examined in link order. */
6935 if (!compute_removed_literals (abfd, sec, link_info, values))
6936 return FALSE;
6937 *again = TRUE;
6938 break;
6939
6940 case 1:
6941 if (values)
6942 value_map_hash_table_delete (values);
6943 values = NULL;
6944 if (!relax_section (abfd, sec, link_info))
6945 return FALSE;
6946 *again = TRUE;
6947 break;
6948
6949 case 2:
6950 if (!relax_section_symbols (abfd, sec))
6951 return FALSE;
6952 break;
6953 }
6954
6955 relax_info->visited++;
6956 return TRUE;
6957 }
6958
6959 \f
6960 /* Initialization for relaxation. */
6961
6962 /* This function is called once at the start of relaxation. It scans
6963 all the input sections and marks the ones that are relaxable (i.e.,
6964 literal sections with L32R relocations against them), and then
6965 collects source_reloc information for all the relocations against
6966 those relaxable sections. During this process, it also detects
6967 longcalls, i.e., calls relaxed by the assembler into indirect
6968 calls, that can be optimized back into direct calls. Within each
6969 extended basic block (ebb) containing an optimized longcall, it
6970 computes a set of "text actions" that can be performed to remove
6971 the L32R associated with the longcall while optionally preserving
6972 branch target alignments. */
6973
6974 static bfd_boolean
6975 analyze_relocations (struct bfd_link_info *link_info)
6976 {
6977 bfd *abfd;
6978 asection *sec;
6979 bfd_boolean is_relaxable = FALSE;
6980
6981 /* Initialize the per-section relaxation info. */
6982 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
6983 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6984 {
6985 init_xtensa_relax_info (sec);
6986 }
6987
6988 /* Mark relaxable sections (and count relocations against each one). */
6989 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
6990 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6991 {
6992 if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
6993 return FALSE;
6994 }
6995
6996 /* Bail out if there are no relaxable sections. */
6997 if (!is_relaxable)
6998 return TRUE;
6999
7000 /* Allocate space for source_relocs. */
7001 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7002 for (sec = abfd->sections; sec != NULL; sec = sec->next)
7003 {
7004 xtensa_relax_info *relax_info;
7005
7006 relax_info = get_xtensa_relax_info (sec);
7007 if (relax_info->is_relaxable_literal_section
7008 || relax_info->is_relaxable_asm_section)
7009 {
7010 relax_info->src_relocs = (source_reloc *)
7011 bfd_malloc (relax_info->src_count * sizeof (source_reloc));
7012 }
7013 else
7014 relax_info->src_count = 0;
7015 }
7016
7017 /* Collect info on relocations against each relaxable section. */
7018 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7019 for (sec = abfd->sections; sec != NULL; sec = sec->next)
7020 {
7021 if (!collect_source_relocs (abfd, sec, link_info))
7022 return FALSE;
7023 }
7024
7025 /* Compute the text actions. */
7026 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7027 for (sec = abfd->sections; sec != NULL; sec = sec->next)
7028 {
7029 if (!compute_text_actions (abfd, sec, link_info))
7030 return FALSE;
7031 }
7032
7033 return TRUE;
7034 }
7035
7036
7037 /* Find all the sections that might be relaxed. The motivation for
7038 this pass is that collect_source_relocs() needs to record _all_ the
7039 relocations that target each relaxable section. That is expensive
7040 and unnecessary unless the target section is actually going to be
7041 relaxed. This pass identifies all such sections by checking if
7042 they have L32Rs pointing to them. In the process, the total number
7043 of relocations targeting each section is also counted so that we
7044 know how much space to allocate for source_relocs against each
7045 relaxable literal section. */
7046
7047 static bfd_boolean
7048 find_relaxable_sections (bfd *abfd,
7049 asection *sec,
7050 struct bfd_link_info *link_info,
7051 bfd_boolean *is_relaxable_p)
7052 {
7053 Elf_Internal_Rela *internal_relocs;
7054 bfd_byte *contents;
7055 bfd_boolean ok = TRUE;
7056 unsigned i;
7057 xtensa_relax_info *source_relax_info;
7058 bfd_boolean is_l32r_reloc;
7059
7060 internal_relocs = retrieve_internal_relocs (abfd, sec,
7061 link_info->keep_memory);
7062 if (internal_relocs == NULL)
7063 return ok;
7064
7065 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7066 if (contents == NULL && sec->size != 0)
7067 {
7068 ok = FALSE;
7069 goto error_return;
7070 }
7071
7072 source_relax_info = get_xtensa_relax_info (sec);
7073 for (i = 0; i < sec->reloc_count; i++)
7074 {
7075 Elf_Internal_Rela *irel = &internal_relocs[i];
7076 r_reloc r_rel;
7077 asection *target_sec;
7078 xtensa_relax_info *target_relax_info;
7079
7080 /* If this section has not already been marked as "relaxable", and
7081 if it contains any ASM_EXPAND relocations (marking expanded
7082 longcalls) that can be optimized into direct calls, then mark
7083 the section as "relaxable". */
7084 if (source_relax_info
7085 && !source_relax_info->is_relaxable_asm_section
7086 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_EXPAND)
7087 {
7088 bfd_boolean is_reachable = FALSE;
7089 if (is_resolvable_asm_expansion (abfd, sec, contents, irel,
7090 link_info, &is_reachable)
7091 && is_reachable)
7092 {
7093 source_relax_info->is_relaxable_asm_section = TRUE;
7094 *is_relaxable_p = TRUE;
7095 }
7096 }
7097
7098 r_reloc_init (&r_rel, abfd, irel, contents,
7099 bfd_get_section_limit (abfd, sec));
7100
7101 target_sec = r_reloc_get_section (&r_rel);
7102 target_relax_info = get_xtensa_relax_info (target_sec);
7103 if (!target_relax_info)
7104 continue;
7105
7106 /* Count PC-relative operand relocations against the target section.
7107 Note: The conditions tested here must match the conditions under
7108 which init_source_reloc is called in collect_source_relocs(). */
7109 is_l32r_reloc = FALSE;
7110 if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
7111 {
7112 xtensa_opcode opcode =
7113 get_relocation_opcode (abfd, sec, contents, irel);
7114 if (opcode != XTENSA_UNDEFINED)
7115 {
7116 is_l32r_reloc = (opcode == get_l32r_opcode ());
7117 if (!is_alt_relocation (ELF32_R_TYPE (irel->r_info))
7118 || is_l32r_reloc)
7119 target_relax_info->src_count++;
7120 }
7121 }
7122
7123 if (is_l32r_reloc && r_reloc_is_defined (&r_rel))
7124 {
7125 /* Mark the target section as relaxable. */
7126 target_relax_info->is_relaxable_literal_section = TRUE;
7127 *is_relaxable_p = TRUE;
7128 }
7129 }
7130
7131 error_return:
7132 release_contents (sec, contents);
7133 release_internal_relocs (sec, internal_relocs);
7134 return ok;
7135 }
7136
7137
7138 /* Record _all_ the relocations that point to relaxable sections, and
7139 get rid of ASM_EXPAND relocs by either converting them to
7140 ASM_SIMPLIFY or by removing them. */
7141
7142 static bfd_boolean
7143 collect_source_relocs (bfd *abfd,
7144 asection *sec,
7145 struct bfd_link_info *link_info)
7146 {
7147 Elf_Internal_Rela *internal_relocs;
7148 bfd_byte *contents;
7149 bfd_boolean ok = TRUE;
7150 unsigned i;
7151 bfd_size_type sec_size;
7152
7153 internal_relocs = retrieve_internal_relocs (abfd, sec,
7154 link_info->keep_memory);
7155 if (internal_relocs == NULL)
7156 return ok;
7157
7158 sec_size = bfd_get_section_limit (abfd, sec);
7159 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7160 if (contents == NULL && sec_size != 0)
7161 {
7162 ok = FALSE;
7163 goto error_return;
7164 }
7165
7166 /* Record relocations against relaxable literal sections. */
7167 for (i = 0; i < sec->reloc_count; i++)
7168 {
7169 Elf_Internal_Rela *irel = &internal_relocs[i];
7170 r_reloc r_rel;
7171 asection *target_sec;
7172 xtensa_relax_info *target_relax_info;
7173
7174 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
7175
7176 target_sec = r_reloc_get_section (&r_rel);
7177 target_relax_info = get_xtensa_relax_info (target_sec);
7178
7179 if (target_relax_info
7180 && (target_relax_info->is_relaxable_literal_section
7181 || target_relax_info->is_relaxable_asm_section))
7182 {
7183 xtensa_opcode opcode = XTENSA_UNDEFINED;
7184 int opnd = -1;
7185 bfd_boolean is_abs_literal = FALSE;
7186
7187 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
7188 {
7189 /* None of the current alternate relocs are PC-relative,
7190 and only PC-relative relocs matter here. However, we
7191 still need to record the opcode for literal
7192 coalescing. */
7193 opcode = get_relocation_opcode (abfd, sec, contents, irel);
7194 if (opcode == get_l32r_opcode ())
7195 {
7196 is_abs_literal = TRUE;
7197 opnd = 1;
7198 }
7199 else
7200 opcode = XTENSA_UNDEFINED;
7201 }
7202 else if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
7203 {
7204 opcode = get_relocation_opcode (abfd, sec, contents, irel);
7205 opnd = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
7206 }
7207
7208 if (opcode != XTENSA_UNDEFINED)
7209 {
7210 int src_next = target_relax_info->src_next++;
7211 source_reloc *s_reloc = &target_relax_info->src_relocs[src_next];
7212
7213 init_source_reloc (s_reloc, sec, &r_rel, opcode, opnd,
7214 is_abs_literal);
7215 }
7216 }
7217 }
7218
7219 /* Now get rid of ASM_EXPAND relocations. At this point, the
7220 src_relocs array for the target literal section may still be
7221 incomplete, but it must at least contain the entries for the L32R
7222 relocations associated with ASM_EXPANDs because they were just
7223 added in the preceding loop over the relocations. */
7224
7225 for (i = 0; i < sec->reloc_count; i++)
7226 {
7227 Elf_Internal_Rela *irel = &internal_relocs[i];
7228 bfd_boolean is_reachable;
7229
7230 if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
7231 &is_reachable))
7232 continue;
7233
7234 if (is_reachable)
7235 {
7236 Elf_Internal_Rela *l32r_irel;
7237 r_reloc r_rel;
7238 asection *target_sec;
7239 xtensa_relax_info *target_relax_info;
7240
7241 /* Mark the source_reloc for the L32R so that it will be
7242 removed in compute_removed_literals(), along with the
7243 associated literal. */
7244 l32r_irel = find_associated_l32r_irel (abfd, sec, contents,
7245 irel, internal_relocs);
7246 if (l32r_irel == NULL)
7247 continue;
7248
7249 r_reloc_init (&r_rel, abfd, l32r_irel, contents, sec_size);
7250
7251 target_sec = r_reloc_get_section (&r_rel);
7252 target_relax_info = get_xtensa_relax_info (target_sec);
7253
7254 if (target_relax_info
7255 && (target_relax_info->is_relaxable_literal_section
7256 || target_relax_info->is_relaxable_asm_section))
7257 {
7258 source_reloc *s_reloc;
7259
7260 /* Search the source_relocs for the entry corresponding to
7261 the l32r_irel. Note: The src_relocs array is not yet
7262 sorted, but it wouldn't matter anyway because we're
7263 searching by source offset instead of target offset. */
7264 s_reloc = find_source_reloc (target_relax_info->src_relocs,
7265 target_relax_info->src_next,
7266 sec, l32r_irel);
7267 BFD_ASSERT (s_reloc);
7268 s_reloc->is_null = TRUE;
7269 }
7270
7271 /* Convert this reloc to ASM_SIMPLIFY. */
7272 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
7273 R_XTENSA_ASM_SIMPLIFY);
7274 l32r_irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7275
7276 pin_internal_relocs (sec, internal_relocs);
7277 }
7278 else
7279 {
7280 /* It is resolvable but doesn't reach. We resolve now
7281 by eliminating the relocation -- the call will remain
7282 expanded into L32R/CALLX. */
7283 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7284 pin_internal_relocs (sec, internal_relocs);
7285 }
7286 }
7287
7288 error_return:
7289 release_contents (sec, contents);
7290 release_internal_relocs (sec, internal_relocs);
7291 return ok;
7292 }
7293
7294
7295 /* Return TRUE if the asm expansion can be resolved. Generally it can
7296 be resolved on a final link or when a partial link locates it in the
7297 same section as the target. Set "is_reachable" flag if the target of
7298 the call is within the range of a direct call, given the current VMA
7299 for this section and the target section. */
7300
7301 bfd_boolean
7302 is_resolvable_asm_expansion (bfd *abfd,
7303 asection *sec,
7304 bfd_byte *contents,
7305 Elf_Internal_Rela *irel,
7306 struct bfd_link_info *link_info,
7307 bfd_boolean *is_reachable_p)
7308 {
7309 asection *target_sec;
7310 bfd_vma target_offset;
7311 r_reloc r_rel;
7312 xtensa_opcode opcode, direct_call_opcode;
7313 bfd_vma self_address;
7314 bfd_vma dest_address;
7315 bfd_boolean uses_l32r;
7316 bfd_size_type sec_size;
7317
7318 *is_reachable_p = FALSE;
7319
7320 if (contents == NULL)
7321 return FALSE;
7322
7323 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
7324 return FALSE;
7325
7326 sec_size = bfd_get_section_limit (abfd, sec);
7327 opcode = get_expanded_call_opcode (contents + irel->r_offset,
7328 sec_size - irel->r_offset, &uses_l32r);
7329 /* Optimization of longcalls that use CONST16 is not yet implemented. */
7330 if (!uses_l32r)
7331 return FALSE;
7332
7333 direct_call_opcode = swap_callx_for_call_opcode (opcode);
7334 if (direct_call_opcode == XTENSA_UNDEFINED)
7335 return FALSE;
7336
7337 /* Check and see that the target resolves. */
7338 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
7339 if (!r_reloc_is_defined (&r_rel))
7340 return FALSE;
7341
7342 target_sec = r_reloc_get_section (&r_rel);
7343 target_offset = r_rel.target_offset;
7344
7345 /* If the target is in a shared library, then it doesn't reach. This
7346 isn't supposed to come up because the compiler should never generate
7347 non-PIC calls on systems that use shared libraries, but the linker
7348 shouldn't crash regardless. */
7349 if (!target_sec->output_section)
7350 return FALSE;
7351
7352 /* For relocatable sections, we can only simplify when the output
7353 section of the target is the same as the output section of the
7354 source. */
7355 if (bfd_link_relocatable (link_info)
7356 && (target_sec->output_section != sec->output_section
7357 || is_reloc_sym_weak (abfd, irel)))
7358 return FALSE;
7359
7360 if (target_sec->output_section != sec->output_section)
7361 {
7362 /* If the two sections are sufficiently far away that relaxation
7363 might take the call out of range, we can't simplify. For
7364 example, a positive displacement call into another memory
7365 could get moved to a lower address due to literal removal,
7366 but the destination won't move, and so the displacment might
7367 get larger.
7368
7369 If the displacement is negative, assume the destination could
7370 move as far back as the start of the output section. The
7371 self_address will be at least as far into the output section
7372 as it is prior to relaxation.
7373
7374 If the displacement is postive, assume the destination will be in
7375 it's pre-relaxed location (because relaxation only makes sections
7376 smaller). The self_address could go all the way to the beginning
7377 of the output section. */
7378
7379 dest_address = target_sec->output_section->vma;
7380 self_address = sec->output_section->vma;
7381
7382 if (sec->output_section->vma > target_sec->output_section->vma)
7383 self_address += sec->output_offset + irel->r_offset + 3;
7384 else
7385 dest_address += bfd_get_section_limit (abfd, target_sec->output_section);
7386 /* Call targets should be four-byte aligned. */
7387 dest_address = (dest_address + 3) & ~3;
7388 }
7389 else
7390 {
7391
7392 self_address = (sec->output_section->vma
7393 + sec->output_offset + irel->r_offset + 3);
7394 dest_address = (target_sec->output_section->vma
7395 + target_sec->output_offset + target_offset);
7396 }
7397
7398 *is_reachable_p = pcrel_reloc_fits (direct_call_opcode, 0,
7399 self_address, dest_address);
7400
7401 if ((self_address >> CALL_SEGMENT_BITS) !=
7402 (dest_address >> CALL_SEGMENT_BITS))
7403 return FALSE;
7404
7405 return TRUE;
7406 }
7407
7408
7409 static Elf_Internal_Rela *
7410 find_associated_l32r_irel (bfd *abfd,
7411 asection *sec,
7412 bfd_byte *contents,
7413 Elf_Internal_Rela *other_irel,
7414 Elf_Internal_Rela *internal_relocs)
7415 {
7416 unsigned i;
7417
7418 for (i = 0; i < sec->reloc_count; i++)
7419 {
7420 Elf_Internal_Rela *irel = &internal_relocs[i];
7421
7422 if (irel == other_irel)
7423 continue;
7424 if (irel->r_offset != other_irel->r_offset)
7425 continue;
7426 if (is_l32r_relocation (abfd, sec, contents, irel))
7427 return irel;
7428 }
7429
7430 return NULL;
7431 }
7432
7433
7434 static xtensa_opcode *
7435 build_reloc_opcodes (bfd *abfd,
7436 asection *sec,
7437 bfd_byte *contents,
7438 Elf_Internal_Rela *internal_relocs)
7439 {
7440 unsigned i;
7441 xtensa_opcode *reloc_opcodes =
7442 (xtensa_opcode *) bfd_malloc (sizeof (xtensa_opcode) * sec->reloc_count);
7443 for (i = 0; i < sec->reloc_count; i++)
7444 {
7445 Elf_Internal_Rela *irel = &internal_relocs[i];
7446 reloc_opcodes[i] = get_relocation_opcode (abfd, sec, contents, irel);
7447 }
7448 return reloc_opcodes;
7449 }
7450
7451 struct reloc_range_struct
7452 {
7453 bfd_vma addr;
7454 bfd_boolean add; /* TRUE if start of a range, FALSE otherwise. */
7455 /* Original irel index in the array of relocations for a section. */
7456 unsigned irel_index;
7457 };
7458 typedef struct reloc_range_struct reloc_range;
7459
7460 typedef struct reloc_range_list_entry_struct reloc_range_list_entry;
7461 struct reloc_range_list_entry_struct
7462 {
7463 reloc_range_list_entry *next;
7464 reloc_range_list_entry *prev;
7465 Elf_Internal_Rela *irel;
7466 xtensa_opcode opcode;
7467 int opnum;
7468 };
7469
7470 struct reloc_range_list_struct
7471 {
7472 /* The rest of the structure is only meaningful when ok is TRUE. */
7473 bfd_boolean ok;
7474
7475 unsigned n_range; /* Number of range markers. */
7476 reloc_range *range; /* Sorted range markers. */
7477
7478 unsigned first; /* Index of a first range element in the list. */
7479 unsigned last; /* One past index of a last range element in the list. */
7480
7481 unsigned n_list; /* Number of list elements. */
7482 reloc_range_list_entry *reloc; /* */
7483 reloc_range_list_entry list_root;
7484 };
7485
7486 static int
7487 reloc_range_compare (const void *a, const void *b)
7488 {
7489 const reloc_range *ra = a;
7490 const reloc_range *rb = b;
7491
7492 if (ra->addr != rb->addr)
7493 return ra->addr < rb->addr ? -1 : 1;
7494 if (ra->add != rb->add)
7495 return ra->add ? -1 : 1;
7496 return 0;
7497 }
7498
7499 static void
7500 build_reloc_ranges (bfd *abfd, asection *sec,
7501 bfd_byte *contents,
7502 Elf_Internal_Rela *internal_relocs,
7503 xtensa_opcode *reloc_opcodes,
7504 reloc_range_list *list)
7505 {
7506 unsigned i;
7507 size_t n = 0;
7508 size_t max_n = 0;
7509 reloc_range *ranges = NULL;
7510 reloc_range_list_entry *reloc =
7511 bfd_malloc (sec->reloc_count * sizeof (*reloc));
7512
7513 memset (list, 0, sizeof (*list));
7514 list->ok = TRUE;
7515
7516 for (i = 0; i < sec->reloc_count; i++)
7517 {
7518 Elf_Internal_Rela *irel = &internal_relocs[i];
7519 int r_type = ELF32_R_TYPE (irel->r_info);
7520 reloc_howto_type *howto = &elf_howto_table[r_type];
7521 r_reloc r_rel;
7522
7523 if (r_type == R_XTENSA_ASM_SIMPLIFY
7524 || r_type == R_XTENSA_32_PCREL
7525 || !howto->pc_relative)
7526 continue;
7527
7528 r_reloc_init (&r_rel, abfd, irel, contents,
7529 bfd_get_section_limit (abfd, sec));
7530
7531 if (r_reloc_get_section (&r_rel) != sec)
7532 continue;
7533
7534 if (n + 2 > max_n)
7535 {
7536 max_n = (max_n + 2) * 2;
7537 ranges = bfd_realloc (ranges, max_n * sizeof (*ranges));
7538 }
7539
7540 ranges[n].addr = irel->r_offset;
7541 ranges[n + 1].addr = r_rel.target_offset;
7542
7543 ranges[n].add = ranges[n].addr < ranges[n + 1].addr;
7544 ranges[n + 1].add = !ranges[n].add;
7545
7546 ranges[n].irel_index = i;
7547 ranges[n + 1].irel_index = i;
7548
7549 n += 2;
7550
7551 reloc[i].irel = irel;
7552
7553 /* Every relocation won't possibly be checked in the optimized version of
7554 check_section_ebb_pcrels_fit, so this needs to be done here. */
7555 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
7556 {
7557 /* None of the current alternate relocs are PC-relative,
7558 and only PC-relative relocs matter here. */
7559 }
7560 else
7561 {
7562 xtensa_opcode opcode;
7563 int opnum;
7564
7565 if (reloc_opcodes)
7566 opcode = reloc_opcodes[i];
7567 else
7568 opcode = get_relocation_opcode (abfd, sec, contents, irel);
7569
7570 if (opcode == XTENSA_UNDEFINED)
7571 {
7572 list->ok = FALSE;
7573 break;
7574 }
7575
7576 opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
7577 if (opnum == XTENSA_UNDEFINED)
7578 {
7579 list->ok = FALSE;
7580 break;
7581 }
7582
7583 /* Record relocation opcode and opnum as we've calculated them
7584 anyway and they won't change. */
7585 reloc[i].opcode = opcode;
7586 reloc[i].opnum = opnum;
7587 }
7588 }
7589
7590 if (list->ok)
7591 {
7592 ranges = bfd_realloc (ranges, n * sizeof (*ranges));
7593 qsort (ranges, n, sizeof (*ranges), reloc_range_compare);
7594
7595 list->n_range = n;
7596 list->range = ranges;
7597 list->reloc = reloc;
7598 list->list_root.prev = &list->list_root;
7599 list->list_root.next = &list->list_root;
7600 }
7601 else
7602 {
7603 free (ranges);
7604 free (reloc);
7605 }
7606 }
7607
7608 static void reloc_range_list_append (reloc_range_list *list,
7609 unsigned irel_index)
7610 {
7611 reloc_range_list_entry *entry = list->reloc + irel_index;
7612
7613 entry->prev = list->list_root.prev;
7614 entry->next = &list->list_root;
7615 entry->prev->next = entry;
7616 entry->next->prev = entry;
7617 ++list->n_list;
7618 }
7619
7620 static void reloc_range_list_remove (reloc_range_list *list,
7621 unsigned irel_index)
7622 {
7623 reloc_range_list_entry *entry = list->reloc + irel_index;
7624
7625 entry->next->prev = entry->prev;
7626 entry->prev->next = entry->next;
7627 --list->n_list;
7628 }
7629
7630 /* Update relocation list object so that it lists all relocations that cross
7631 [first; last] range. Range bounds should not decrease with successive
7632 invocations. */
7633 static void reloc_range_list_update_range (reloc_range_list *list,
7634 bfd_vma first, bfd_vma last)
7635 {
7636 /* This should not happen: EBBs are iterated from lower addresses to higher.
7637 But even if that happens there's no need to break: just flush current list
7638 and start from scratch. */
7639 if ((list->last > 0 && list->range[list->last - 1].addr > last) ||
7640 (list->first > 0 && list->range[list->first - 1].addr >= first))
7641 {
7642 list->first = 0;
7643 list->last = 0;
7644 list->n_list = 0;
7645 list->list_root.next = &list->list_root;
7646 list->list_root.prev = &list->list_root;
7647 fprintf (stderr, "%s: move backwards requested\n", __func__);
7648 }
7649
7650 for (; list->last < list->n_range &&
7651 list->range[list->last].addr <= last; ++list->last)
7652 if (list->range[list->last].add)
7653 reloc_range_list_append (list, list->range[list->last].irel_index);
7654
7655 for (; list->first < list->n_range &&
7656 list->range[list->first].addr < first; ++list->first)
7657 if (!list->range[list->first].add)
7658 reloc_range_list_remove (list, list->range[list->first].irel_index);
7659 }
7660
7661 static void free_reloc_range_list (reloc_range_list *list)
7662 {
7663 free (list->range);
7664 free (list->reloc);
7665 }
7666
7667 /* The compute_text_actions function will build a list of potential
7668 transformation actions for code in the extended basic block of each
7669 longcall that is optimized to a direct call. From this list we
7670 generate a set of actions to actually perform that optimizes for
7671 space and, if not using size_opt, maintains branch target
7672 alignments.
7673
7674 These actions to be performed are placed on a per-section list.
7675 The actual changes are performed by relax_section() in the second
7676 pass. */
7677
7678 bfd_boolean
7679 compute_text_actions (bfd *abfd,
7680 asection *sec,
7681 struct bfd_link_info *link_info)
7682 {
7683 xtensa_opcode *reloc_opcodes = NULL;
7684 xtensa_relax_info *relax_info;
7685 bfd_byte *contents;
7686 Elf_Internal_Rela *internal_relocs;
7687 bfd_boolean ok = TRUE;
7688 unsigned i;
7689 property_table_entry *prop_table = 0;
7690 int ptblsize = 0;
7691 bfd_size_type sec_size;
7692 reloc_range_list relevant_relocs;
7693
7694 relax_info = get_xtensa_relax_info (sec);
7695 BFD_ASSERT (relax_info);
7696 BFD_ASSERT (relax_info->src_next == relax_info->src_count);
7697
7698 /* Do nothing if the section contains no optimized longcalls. */
7699 if (!relax_info->is_relaxable_asm_section)
7700 return ok;
7701
7702 internal_relocs = retrieve_internal_relocs (abfd, sec,
7703 link_info->keep_memory);
7704
7705 if (internal_relocs)
7706 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
7707 internal_reloc_compare);
7708
7709 sec_size = bfd_get_section_limit (abfd, sec);
7710 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7711 if (contents == NULL && sec_size != 0)
7712 {
7713 ok = FALSE;
7714 goto error_return;
7715 }
7716
7717 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
7718 XTENSA_PROP_SEC_NAME, FALSE);
7719 if (ptblsize < 0)
7720 {
7721 ok = FALSE;
7722 goto error_return;
7723 }
7724
7725 /* Precompute the opcode for each relocation. */
7726 reloc_opcodes = build_reloc_opcodes (abfd, sec, contents, internal_relocs);
7727
7728 build_reloc_ranges (abfd, sec, contents, internal_relocs, reloc_opcodes,
7729 &relevant_relocs);
7730
7731 for (i = 0; i < sec->reloc_count; i++)
7732 {
7733 Elf_Internal_Rela *irel = &internal_relocs[i];
7734 bfd_vma r_offset;
7735 property_table_entry *the_entry;
7736 int ptbl_idx;
7737 ebb_t *ebb;
7738 ebb_constraint ebb_table;
7739 bfd_size_type simplify_size;
7740
7741 if (irel && ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_SIMPLIFY)
7742 continue;
7743 r_offset = irel->r_offset;
7744
7745 simplify_size = get_asm_simplify_size (contents, sec_size, r_offset);
7746 if (simplify_size == 0)
7747 {
7748 _bfd_error_handler
7749 /* xgettext:c-format */
7750 (_("%B(%A+0x%lx): could not decode instruction for XTENSA_ASM_SIMPLIFY relocation; possible configuration mismatch"),
7751 sec->owner, sec, r_offset);
7752 continue;
7753 }
7754
7755 /* If the instruction table is not around, then don't do this
7756 relaxation. */
7757 the_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
7758 sec->vma + irel->r_offset);
7759 if (the_entry == NULL || XTENSA_NO_NOP_REMOVAL)
7760 {
7761 text_action_add (&relax_info->action_list,
7762 ta_convert_longcall, sec, r_offset,
7763 0);
7764 continue;
7765 }
7766
7767 /* If the next longcall happens to be at the same address as an
7768 unreachable section of size 0, then skip forward. */
7769 ptbl_idx = the_entry - prop_table;
7770 while ((the_entry->flags & XTENSA_PROP_UNREACHABLE)
7771 && the_entry->size == 0
7772 && ptbl_idx + 1 < ptblsize
7773 && (prop_table[ptbl_idx + 1].address
7774 == prop_table[ptbl_idx].address))
7775 {
7776 ptbl_idx++;
7777 the_entry++;
7778 }
7779
7780 if (the_entry->flags & XTENSA_PROP_NO_TRANSFORM)
7781 /* NO_REORDER is OK */
7782 continue;
7783
7784 init_ebb_constraint (&ebb_table);
7785 ebb = &ebb_table.ebb;
7786 init_ebb (ebb, sec, contents, sec_size, prop_table, ptblsize,
7787 internal_relocs, sec->reloc_count);
7788 ebb->start_offset = r_offset + simplify_size;
7789 ebb->end_offset = r_offset + simplify_size;
7790 ebb->start_ptbl_idx = ptbl_idx;
7791 ebb->end_ptbl_idx = ptbl_idx;
7792 ebb->start_reloc_idx = i;
7793 ebb->end_reloc_idx = i;
7794
7795 if (!extend_ebb_bounds (ebb)
7796 || !compute_ebb_proposed_actions (&ebb_table)
7797 || !compute_ebb_actions (&ebb_table)
7798 || !check_section_ebb_pcrels_fit (abfd, sec, contents,
7799 internal_relocs,
7800 &relevant_relocs,
7801 &ebb_table, reloc_opcodes)
7802 || !check_section_ebb_reduces (&ebb_table))
7803 {
7804 /* If anything goes wrong or we get unlucky and something does
7805 not fit, with our plan because of expansion between
7806 critical branches, just convert to a NOP. */
7807
7808 text_action_add (&relax_info->action_list,
7809 ta_convert_longcall, sec, r_offset, 0);
7810 i = ebb_table.ebb.end_reloc_idx;
7811 free_ebb_constraint (&ebb_table);
7812 continue;
7813 }
7814
7815 text_action_add_proposed (&relax_info->action_list, &ebb_table, sec);
7816
7817 /* Update the index so we do not go looking at the relocations
7818 we have already processed. */
7819 i = ebb_table.ebb.end_reloc_idx;
7820 free_ebb_constraint (&ebb_table);
7821 }
7822
7823 free_reloc_range_list (&relevant_relocs);
7824
7825 #if DEBUG
7826 if (action_list_count (&relax_info->action_list))
7827 print_action_list (stderr, &relax_info->action_list);
7828 #endif
7829
7830 error_return:
7831 release_contents (sec, contents);
7832 release_internal_relocs (sec, internal_relocs);
7833 if (prop_table)
7834 free (prop_table);
7835 if (reloc_opcodes)
7836 free (reloc_opcodes);
7837
7838 return ok;
7839 }
7840
7841
7842 /* Do not widen an instruction if it is preceeded by a
7843 loop opcode. It might cause misalignment. */
7844
7845 static bfd_boolean
7846 prev_instr_is_a_loop (bfd_byte *contents,
7847 bfd_size_type content_length,
7848 bfd_size_type offset)
7849 {
7850 xtensa_opcode prev_opcode;
7851
7852 if (offset < 3)
7853 return FALSE;
7854 prev_opcode = insn_decode_opcode (contents, content_length, offset-3, 0);
7855 return (xtensa_opcode_is_loop (xtensa_default_isa, prev_opcode) == 1);
7856 }
7857
7858
7859 /* Find all of the possible actions for an extended basic block. */
7860
7861 bfd_boolean
7862 compute_ebb_proposed_actions (ebb_constraint *ebb_table)
7863 {
7864 const ebb_t *ebb = &ebb_table->ebb;
7865 unsigned rel_idx = ebb->start_reloc_idx;
7866 property_table_entry *entry, *start_entry, *end_entry;
7867 bfd_vma offset = 0;
7868 xtensa_isa isa = xtensa_default_isa;
7869 xtensa_format fmt;
7870 static xtensa_insnbuf insnbuf = NULL;
7871 static xtensa_insnbuf slotbuf = NULL;
7872
7873 if (insnbuf == NULL)
7874 {
7875 insnbuf = xtensa_insnbuf_alloc (isa);
7876 slotbuf = xtensa_insnbuf_alloc (isa);
7877 }
7878
7879 start_entry = &ebb->ptbl[ebb->start_ptbl_idx];
7880 end_entry = &ebb->ptbl[ebb->end_ptbl_idx];
7881
7882 for (entry = start_entry; entry <= end_entry; entry++)
7883 {
7884 bfd_vma start_offset, end_offset;
7885 bfd_size_type insn_len;
7886
7887 start_offset = entry->address - ebb->sec->vma;
7888 end_offset = entry->address + entry->size - ebb->sec->vma;
7889
7890 if (entry == start_entry)
7891 start_offset = ebb->start_offset;
7892 if (entry == end_entry)
7893 end_offset = ebb->end_offset;
7894 offset = start_offset;
7895
7896 if (offset == entry->address - ebb->sec->vma
7897 && (entry->flags & XTENSA_PROP_INSN_BRANCH_TARGET) != 0)
7898 {
7899 enum ebb_target_enum align_type = EBB_DESIRE_TGT_ALIGN;
7900 BFD_ASSERT (offset != end_offset);
7901 if (offset == end_offset)
7902 return FALSE;
7903
7904 insn_len = insn_decode_len (ebb->contents, ebb->content_length,
7905 offset);
7906 if (insn_len == 0)
7907 goto decode_error;
7908
7909 if (check_branch_target_aligned_address (offset, insn_len))
7910 align_type = EBB_REQUIRE_TGT_ALIGN;
7911
7912 ebb_propose_action (ebb_table, align_type, 0,
7913 ta_none, offset, 0, TRUE);
7914 }
7915
7916 while (offset != end_offset)
7917 {
7918 Elf_Internal_Rela *irel;
7919 xtensa_opcode opcode;
7920
7921 while (rel_idx < ebb->end_reloc_idx
7922 && (ebb->relocs[rel_idx].r_offset < offset
7923 || (ebb->relocs[rel_idx].r_offset == offset
7924 && (ELF32_R_TYPE (ebb->relocs[rel_idx].r_info)
7925 != R_XTENSA_ASM_SIMPLIFY))))
7926 rel_idx++;
7927
7928 /* Check for longcall. */
7929 irel = &ebb->relocs[rel_idx];
7930 if (irel->r_offset == offset
7931 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_SIMPLIFY)
7932 {
7933 bfd_size_type simplify_size;
7934
7935 simplify_size = get_asm_simplify_size (ebb->contents,
7936 ebb->content_length,
7937 irel->r_offset);
7938 if (simplify_size == 0)
7939 goto decode_error;
7940
7941 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7942 ta_convert_longcall, offset, 0, TRUE);
7943
7944 offset += simplify_size;
7945 continue;
7946 }
7947
7948 if (offset + MIN_INSN_LENGTH > ebb->content_length)
7949 goto decode_error;
7950 xtensa_insnbuf_from_chars (isa, insnbuf, &ebb->contents[offset],
7951 ebb->content_length - offset);
7952 fmt = xtensa_format_decode (isa, insnbuf);
7953 if (fmt == XTENSA_UNDEFINED)
7954 goto decode_error;
7955 insn_len = xtensa_format_length (isa, fmt);
7956 if (insn_len == (bfd_size_type) XTENSA_UNDEFINED)
7957 goto decode_error;
7958
7959 if (xtensa_format_num_slots (isa, fmt) != 1)
7960 {
7961 offset += insn_len;
7962 continue;
7963 }
7964
7965 xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
7966 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
7967 if (opcode == XTENSA_UNDEFINED)
7968 goto decode_error;
7969
7970 if ((entry->flags & XTENSA_PROP_INSN_NO_DENSITY) == 0
7971 && (entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
7972 && can_narrow_instruction (slotbuf, fmt, opcode) != 0)
7973 {
7974 /* Add an instruction narrow action. */
7975 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7976 ta_narrow_insn, offset, 0, FALSE);
7977 }
7978 else if ((entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
7979 && can_widen_instruction (slotbuf, fmt, opcode) != 0
7980 && ! prev_instr_is_a_loop (ebb->contents,
7981 ebb->content_length, offset))
7982 {
7983 /* Add an instruction widen action. */
7984 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7985 ta_widen_insn, offset, 0, FALSE);
7986 }
7987 else if (xtensa_opcode_is_loop (xtensa_default_isa, opcode) == 1)
7988 {
7989 /* Check for branch targets. */
7990 ebb_propose_action (ebb_table, EBB_REQUIRE_LOOP_ALIGN, 0,
7991 ta_none, offset, 0, TRUE);
7992 }
7993
7994 offset += insn_len;
7995 }
7996 }
7997
7998 if (ebb->ends_unreachable)
7999 {
8000 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
8001 ta_fill, ebb->end_offset, 0, TRUE);
8002 }
8003
8004 return TRUE;
8005
8006 decode_error:
8007 _bfd_error_handler
8008 /* xgettext:c-format */
8009 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
8010 ebb->sec->owner, ebb->sec, offset);
8011 return FALSE;
8012 }
8013
8014
8015 /* After all of the information has collected about the
8016 transformations possible in an EBB, compute the appropriate actions
8017 here in compute_ebb_actions. We still must check later to make
8018 sure that the actions do not break any relocations. The algorithm
8019 used here is pretty greedy. Basically, it removes as many no-ops
8020 as possible so that the end of the EBB has the same alignment
8021 characteristics as the original. First, it uses narrowing, then
8022 fill space at the end of the EBB, and finally widenings. If that
8023 does not work, it tries again with one fewer no-op removed. The
8024 optimization will only be performed if all of the branch targets
8025 that were aligned before transformation are also aligned after the
8026 transformation.
8027
8028 When the size_opt flag is set, ignore the branch target alignments,
8029 narrow all wide instructions, and remove all no-ops unless the end
8030 of the EBB prevents it. */
8031
8032 bfd_boolean
8033 compute_ebb_actions (ebb_constraint *ebb_table)
8034 {
8035 unsigned i = 0;
8036 unsigned j;
8037 int removed_bytes = 0;
8038 ebb_t *ebb = &ebb_table->ebb;
8039 unsigned seg_idx_start = 0;
8040 unsigned seg_idx_end = 0;
8041
8042 /* We perform this like the assembler relaxation algorithm: Start by
8043 assuming all instructions are narrow and all no-ops removed; then
8044 walk through.... */
8045
8046 /* For each segment of this that has a solid constraint, check to
8047 see if there are any combinations that will keep the constraint.
8048 If so, use it. */
8049 for (seg_idx_end = 0; seg_idx_end < ebb_table->action_count; seg_idx_end++)
8050 {
8051 bfd_boolean requires_text_end_align = FALSE;
8052 unsigned longcall_count = 0;
8053 unsigned longcall_convert_count = 0;
8054 unsigned narrowable_count = 0;
8055 unsigned narrowable_convert_count = 0;
8056 unsigned widenable_count = 0;
8057 unsigned widenable_convert_count = 0;
8058
8059 proposed_action *action = NULL;
8060 int align = (1 << ebb_table->ebb.sec->alignment_power);
8061
8062 seg_idx_start = seg_idx_end;
8063
8064 for (i = seg_idx_start; i < ebb_table->action_count; i++)
8065 {
8066 action = &ebb_table->actions[i];
8067 if (action->action == ta_convert_longcall)
8068 longcall_count++;
8069 if (action->action == ta_narrow_insn)
8070 narrowable_count++;
8071 if (action->action == ta_widen_insn)
8072 widenable_count++;
8073 if (action->action == ta_fill)
8074 break;
8075 if (action->align_type == EBB_REQUIRE_LOOP_ALIGN)
8076 break;
8077 if (action->align_type == EBB_REQUIRE_TGT_ALIGN
8078 && !elf32xtensa_size_opt)
8079 break;
8080 }
8081 seg_idx_end = i;
8082
8083 if (seg_idx_end == ebb_table->action_count && !ebb->ends_unreachable)
8084 requires_text_end_align = TRUE;
8085
8086 if (elf32xtensa_size_opt && !requires_text_end_align
8087 && action->align_type != EBB_REQUIRE_LOOP_ALIGN
8088 && action->align_type != EBB_REQUIRE_TGT_ALIGN)
8089 {
8090 longcall_convert_count = longcall_count;
8091 narrowable_convert_count = narrowable_count;
8092 widenable_convert_count = 0;
8093 }
8094 else
8095 {
8096 /* There is a constraint. Convert the max number of longcalls. */
8097 narrowable_convert_count = 0;
8098 longcall_convert_count = 0;
8099 widenable_convert_count = 0;
8100
8101 for (j = 0; j < longcall_count; j++)
8102 {
8103 int removed = (longcall_count - j) * 3 & (align - 1);
8104 unsigned desire_narrow = (align - removed) & (align - 1);
8105 unsigned desire_widen = removed;
8106 if (desire_narrow <= narrowable_count)
8107 {
8108 narrowable_convert_count = desire_narrow;
8109 narrowable_convert_count +=
8110 (align * ((narrowable_count - narrowable_convert_count)
8111 / align));
8112 longcall_convert_count = (longcall_count - j);
8113 widenable_convert_count = 0;
8114 break;
8115 }
8116 if (desire_widen <= widenable_count && !elf32xtensa_size_opt)
8117 {
8118 narrowable_convert_count = 0;
8119 longcall_convert_count = longcall_count - j;
8120 widenable_convert_count = desire_widen;
8121 break;
8122 }
8123 }
8124 }
8125
8126 /* Now the number of conversions are saved. Do them. */
8127 for (i = seg_idx_start; i < seg_idx_end; i++)
8128 {
8129 action = &ebb_table->actions[i];
8130 switch (action->action)
8131 {
8132 case ta_convert_longcall:
8133 if (longcall_convert_count != 0)
8134 {
8135 action->action = ta_remove_longcall;
8136 action->do_action = TRUE;
8137 action->removed_bytes += 3;
8138 longcall_convert_count--;
8139 }
8140 break;
8141 case ta_narrow_insn:
8142 if (narrowable_convert_count != 0)
8143 {
8144 action->do_action = TRUE;
8145 action->removed_bytes += 1;
8146 narrowable_convert_count--;
8147 }
8148 break;
8149 case ta_widen_insn:
8150 if (widenable_convert_count != 0)
8151 {
8152 action->do_action = TRUE;
8153 action->removed_bytes -= 1;
8154 widenable_convert_count--;
8155 }
8156 break;
8157 default:
8158 break;
8159 }
8160 }
8161 }
8162
8163 /* Now we move on to some local opts. Try to remove each of the
8164 remaining longcalls. */
8165
8166 if (ebb_table->ebb.ends_section || ebb_table->ebb.ends_unreachable)
8167 {
8168 removed_bytes = 0;
8169 for (i = 0; i < ebb_table->action_count; i++)
8170 {
8171 int old_removed_bytes = removed_bytes;
8172 proposed_action *action = &ebb_table->actions[i];
8173
8174 if (action->do_action && action->action == ta_convert_longcall)
8175 {
8176 bfd_boolean bad_alignment = FALSE;
8177 removed_bytes += 3;
8178 for (j = i + 1; j < ebb_table->action_count; j++)
8179 {
8180 proposed_action *new_action = &ebb_table->actions[j];
8181 bfd_vma offset = new_action->offset;
8182 if (new_action->align_type == EBB_REQUIRE_TGT_ALIGN)
8183 {
8184 if (!check_branch_target_aligned
8185 (ebb_table->ebb.contents,
8186 ebb_table->ebb.content_length,
8187 offset, offset - removed_bytes))
8188 {
8189 bad_alignment = TRUE;
8190 break;
8191 }
8192 }
8193 if (new_action->align_type == EBB_REQUIRE_LOOP_ALIGN)
8194 {
8195 if (!check_loop_aligned (ebb_table->ebb.contents,
8196 ebb_table->ebb.content_length,
8197 offset,
8198 offset - removed_bytes))
8199 {
8200 bad_alignment = TRUE;
8201 break;
8202 }
8203 }
8204 if (new_action->action == ta_narrow_insn
8205 && !new_action->do_action
8206 && ebb_table->ebb.sec->alignment_power == 2)
8207 {
8208 /* Narrow an instruction and we are done. */
8209 new_action->do_action = TRUE;
8210 new_action->removed_bytes += 1;
8211 bad_alignment = FALSE;
8212 break;
8213 }
8214 if (new_action->action == ta_widen_insn
8215 && new_action->do_action
8216 && ebb_table->ebb.sec->alignment_power == 2)
8217 {
8218 /* Narrow an instruction and we are done. */
8219 new_action->do_action = FALSE;
8220 new_action->removed_bytes += 1;
8221 bad_alignment = FALSE;
8222 break;
8223 }
8224 if (new_action->do_action)
8225 removed_bytes += new_action->removed_bytes;
8226 }
8227 if (!bad_alignment)
8228 {
8229 action->removed_bytes += 3;
8230 action->action = ta_remove_longcall;
8231 action->do_action = TRUE;
8232 }
8233 }
8234 removed_bytes = old_removed_bytes;
8235 if (action->do_action)
8236 removed_bytes += action->removed_bytes;
8237 }
8238 }
8239
8240 removed_bytes = 0;
8241 for (i = 0; i < ebb_table->action_count; ++i)
8242 {
8243 proposed_action *action = &ebb_table->actions[i];
8244 if (action->do_action)
8245 removed_bytes += action->removed_bytes;
8246 }
8247
8248 if ((removed_bytes % (1 << ebb_table->ebb.sec->alignment_power)) != 0
8249 && ebb->ends_unreachable)
8250 {
8251 proposed_action *action;
8252 int br;
8253 int extra_space;
8254
8255 BFD_ASSERT (ebb_table->action_count != 0);
8256 action = &ebb_table->actions[ebb_table->action_count - 1];
8257 BFD_ASSERT (action->action == ta_fill);
8258 BFD_ASSERT (ebb->ends_unreachable->flags & XTENSA_PROP_UNREACHABLE);
8259
8260 extra_space = compute_fill_extra_space (ebb->ends_unreachable);
8261 br = action->removed_bytes + removed_bytes + extra_space;
8262 br = br & ((1 << ebb->sec->alignment_power ) - 1);
8263
8264 action->removed_bytes = extra_space - br;
8265 }
8266 return TRUE;
8267 }
8268
8269
8270 /* The xlate_map is a sorted array of address mappings designed to
8271 answer the offset_with_removed_text() query with a binary search instead
8272 of a linear search through the section's action_list. */
8273
8274 typedef struct xlate_map_entry xlate_map_entry_t;
8275 typedef struct xlate_map xlate_map_t;
8276
8277 struct xlate_map_entry
8278 {
8279 unsigned orig_address;
8280 unsigned new_address;
8281 unsigned size;
8282 };
8283
8284 struct xlate_map
8285 {
8286 unsigned entry_count;
8287 xlate_map_entry_t *entry;
8288 };
8289
8290
8291 static int
8292 xlate_compare (const void *a_v, const void *b_v)
8293 {
8294 const xlate_map_entry_t *a = (const xlate_map_entry_t *) a_v;
8295 const xlate_map_entry_t *b = (const xlate_map_entry_t *) b_v;
8296 if (a->orig_address < b->orig_address)
8297 return -1;
8298 if (a->orig_address > (b->orig_address + b->size - 1))
8299 return 1;
8300 return 0;
8301 }
8302
8303
8304 static bfd_vma
8305 xlate_offset_with_removed_text (const xlate_map_t *map,
8306 text_action_list *action_list,
8307 bfd_vma offset)
8308 {
8309 void *r;
8310 xlate_map_entry_t *e;
8311
8312 if (map == NULL)
8313 return offset_with_removed_text (action_list, offset);
8314
8315 if (map->entry_count == 0)
8316 return offset;
8317
8318 r = bsearch (&offset, map->entry, map->entry_count,
8319 sizeof (xlate_map_entry_t), &xlate_compare);
8320 e = (xlate_map_entry_t *) r;
8321
8322 BFD_ASSERT (e != NULL);
8323 if (e == NULL)
8324 return offset;
8325 return e->new_address - e->orig_address + offset;
8326 }
8327
8328 typedef struct xlate_map_context_struct xlate_map_context;
8329 struct xlate_map_context_struct
8330 {
8331 xlate_map_t *map;
8332 xlate_map_entry_t *current_entry;
8333 int removed;
8334 };
8335
8336 static int
8337 xlate_map_fn (splay_tree_node node, void *p)
8338 {
8339 text_action *r = (text_action *)node->value;
8340 xlate_map_context *ctx = p;
8341 unsigned orig_size = 0;
8342
8343 switch (r->action)
8344 {
8345 case ta_none:
8346 case ta_remove_insn:
8347 case ta_convert_longcall:
8348 case ta_remove_literal:
8349 case ta_add_literal:
8350 break;
8351 case ta_remove_longcall:
8352 orig_size = 6;
8353 break;
8354 case ta_narrow_insn:
8355 orig_size = 3;
8356 break;
8357 case ta_widen_insn:
8358 orig_size = 2;
8359 break;
8360 case ta_fill:
8361 break;
8362 }
8363 ctx->current_entry->size =
8364 r->offset + orig_size - ctx->current_entry->orig_address;
8365 if (ctx->current_entry->size != 0)
8366 {
8367 ctx->current_entry++;
8368 ctx->map->entry_count++;
8369 }
8370 ctx->current_entry->orig_address = r->offset + orig_size;
8371 ctx->removed += r->removed_bytes;
8372 ctx->current_entry->new_address = r->offset + orig_size - ctx->removed;
8373 ctx->current_entry->size = 0;
8374 return 0;
8375 }
8376
8377 /* Build a binary searchable offset translation map from a section's
8378 action list. */
8379
8380 static xlate_map_t *
8381 build_xlate_map (asection *sec, xtensa_relax_info *relax_info)
8382 {
8383 text_action_list *action_list = &relax_info->action_list;
8384 unsigned num_actions = 0;
8385 xlate_map_context ctx;
8386
8387 ctx.map = (xlate_map_t *) bfd_malloc (sizeof (xlate_map_t));
8388
8389 if (ctx.map == NULL)
8390 return NULL;
8391
8392 num_actions = action_list_count (action_list);
8393 ctx.map->entry = (xlate_map_entry_t *)
8394 bfd_malloc (sizeof (xlate_map_entry_t) * (num_actions + 1));
8395 if (ctx.map->entry == NULL)
8396 {
8397 free (ctx.map);
8398 return NULL;
8399 }
8400 ctx.map->entry_count = 0;
8401
8402 ctx.removed = 0;
8403 ctx.current_entry = &ctx.map->entry[0];
8404
8405 ctx.current_entry->orig_address = 0;
8406 ctx.current_entry->new_address = 0;
8407 ctx.current_entry->size = 0;
8408
8409 splay_tree_foreach (action_list->tree, xlate_map_fn, &ctx);
8410
8411 ctx.current_entry->size = (bfd_get_section_limit (sec->owner, sec)
8412 - ctx.current_entry->orig_address);
8413 if (ctx.current_entry->size != 0)
8414 ctx.map->entry_count++;
8415
8416 return ctx.map;
8417 }
8418
8419
8420 /* Free an offset translation map. */
8421
8422 static void
8423 free_xlate_map (xlate_map_t *map)
8424 {
8425 if (map && map->entry)
8426 free (map->entry);
8427 if (map)
8428 free (map);
8429 }
8430
8431
8432 /* Use check_section_ebb_pcrels_fit to make sure that all of the
8433 relocations in a section will fit if a proposed set of actions
8434 are performed. */
8435
8436 static bfd_boolean
8437 check_section_ebb_pcrels_fit (bfd *abfd,
8438 asection *sec,
8439 bfd_byte *contents,
8440 Elf_Internal_Rela *internal_relocs,
8441 reloc_range_list *relevant_relocs,
8442 const ebb_constraint *constraint,
8443 const xtensa_opcode *reloc_opcodes)
8444 {
8445 unsigned i, j;
8446 unsigned n = sec->reloc_count;
8447 Elf_Internal_Rela *irel;
8448 xlate_map_t *xmap = NULL;
8449 bfd_boolean ok = TRUE;
8450 xtensa_relax_info *relax_info;
8451 reloc_range_list_entry *entry = NULL;
8452
8453 relax_info = get_xtensa_relax_info (sec);
8454
8455 if (relax_info && sec->reloc_count > 100)
8456 {
8457 xmap = build_xlate_map (sec, relax_info);
8458 /* NULL indicates out of memory, but the slow version
8459 can still be used. */
8460 }
8461
8462 if (relevant_relocs && constraint->action_count)
8463 {
8464 if (!relevant_relocs->ok)
8465 {
8466 ok = FALSE;
8467 n = 0;
8468 }
8469 else
8470 {
8471 bfd_vma min_offset, max_offset;
8472 min_offset = max_offset = constraint->actions[0].offset;
8473
8474 for (i = 1; i < constraint->action_count; ++i)
8475 {
8476 proposed_action *action = &constraint->actions[i];
8477 bfd_vma offset = action->offset;
8478
8479 if (offset < min_offset)
8480 min_offset = offset;
8481 if (offset > max_offset)
8482 max_offset = offset;
8483 }
8484 reloc_range_list_update_range (relevant_relocs, min_offset,
8485 max_offset);
8486 n = relevant_relocs->n_list;
8487 entry = &relevant_relocs->list_root;
8488 }
8489 }
8490 else
8491 {
8492 relevant_relocs = NULL;
8493 }
8494
8495 for (i = 0; i < n; i++)
8496 {
8497 r_reloc r_rel;
8498 bfd_vma orig_self_offset, orig_target_offset;
8499 bfd_vma self_offset, target_offset;
8500 int r_type;
8501 reloc_howto_type *howto;
8502 int self_removed_bytes, target_removed_bytes;
8503
8504 if (relevant_relocs)
8505 {
8506 entry = entry->next;
8507 irel = entry->irel;
8508 }
8509 else
8510 {
8511 irel = internal_relocs + i;
8512 }
8513 r_type = ELF32_R_TYPE (irel->r_info);
8514
8515 howto = &elf_howto_table[r_type];
8516 /* We maintain the required invariant: PC-relative relocations
8517 that fit before linking must fit after linking. Thus we only
8518 need to deal with relocations to the same section that are
8519 PC-relative. */
8520 if (r_type == R_XTENSA_ASM_SIMPLIFY
8521 || r_type == R_XTENSA_32_PCREL
8522 || !howto->pc_relative)
8523 continue;
8524
8525 r_reloc_init (&r_rel, abfd, irel, contents,
8526 bfd_get_section_limit (abfd, sec));
8527
8528 if (r_reloc_get_section (&r_rel) != sec)
8529 continue;
8530
8531 orig_self_offset = irel->r_offset;
8532 orig_target_offset = r_rel.target_offset;
8533
8534 self_offset = orig_self_offset;
8535 target_offset = orig_target_offset;
8536
8537 if (relax_info)
8538 {
8539 self_offset =
8540 xlate_offset_with_removed_text (xmap, &relax_info->action_list,
8541 orig_self_offset);
8542 target_offset =
8543 xlate_offset_with_removed_text (xmap, &relax_info->action_list,
8544 orig_target_offset);
8545 }
8546
8547 self_removed_bytes = 0;
8548 target_removed_bytes = 0;
8549
8550 for (j = 0; j < constraint->action_count; ++j)
8551 {
8552 proposed_action *action = &constraint->actions[j];
8553 bfd_vma offset = action->offset;
8554 int removed_bytes = action->removed_bytes;
8555 if (offset < orig_self_offset
8556 || (offset == orig_self_offset && action->action == ta_fill
8557 && action->removed_bytes < 0))
8558 self_removed_bytes += removed_bytes;
8559 if (offset < orig_target_offset
8560 || (offset == orig_target_offset && action->action == ta_fill
8561 && action->removed_bytes < 0))
8562 target_removed_bytes += removed_bytes;
8563 }
8564 self_offset -= self_removed_bytes;
8565 target_offset -= target_removed_bytes;
8566
8567 /* Try to encode it. Get the operand and check. */
8568 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
8569 {
8570 /* None of the current alternate relocs are PC-relative,
8571 and only PC-relative relocs matter here. */
8572 }
8573 else
8574 {
8575 xtensa_opcode opcode;
8576 int opnum;
8577
8578 if (relevant_relocs)
8579 {
8580 opcode = entry->opcode;
8581 opnum = entry->opnum;
8582 }
8583 else
8584 {
8585 if (reloc_opcodes)
8586 opcode = reloc_opcodes[relevant_relocs ?
8587 (unsigned)(entry - relevant_relocs->reloc) : i];
8588 else
8589 opcode = get_relocation_opcode (abfd, sec, contents, irel);
8590 if (opcode == XTENSA_UNDEFINED)
8591 {
8592 ok = FALSE;
8593 break;
8594 }
8595
8596 opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
8597 if (opnum == XTENSA_UNDEFINED)
8598 {
8599 ok = FALSE;
8600 break;
8601 }
8602 }
8603
8604 if (!pcrel_reloc_fits (opcode, opnum, self_offset, target_offset))
8605 {
8606 ok = FALSE;
8607 break;
8608 }
8609 }
8610 }
8611
8612 if (xmap)
8613 free_xlate_map (xmap);
8614
8615 return ok;
8616 }
8617
8618
8619 static bfd_boolean
8620 check_section_ebb_reduces (const ebb_constraint *constraint)
8621 {
8622 int removed = 0;
8623 unsigned i;
8624
8625 for (i = 0; i < constraint->action_count; i++)
8626 {
8627 const proposed_action *action = &constraint->actions[i];
8628 if (action->do_action)
8629 removed += action->removed_bytes;
8630 }
8631 if (removed < 0)
8632 return FALSE;
8633
8634 return TRUE;
8635 }
8636
8637
8638 void
8639 text_action_add_proposed (text_action_list *l,
8640 const ebb_constraint *ebb_table,
8641 asection *sec)
8642 {
8643 unsigned i;
8644
8645 for (i = 0; i < ebb_table->action_count; i++)
8646 {
8647 proposed_action *action = &ebb_table->actions[i];
8648
8649 if (!action->do_action)
8650 continue;
8651 switch (action->action)
8652 {
8653 case ta_remove_insn:
8654 case ta_remove_longcall:
8655 case ta_convert_longcall:
8656 case ta_narrow_insn:
8657 case ta_widen_insn:
8658 case ta_fill:
8659 case ta_remove_literal:
8660 text_action_add (l, action->action, sec, action->offset,
8661 action->removed_bytes);
8662 break;
8663 case ta_none:
8664 break;
8665 default:
8666 BFD_ASSERT (0);
8667 break;
8668 }
8669 }
8670 }
8671
8672
8673 int
8674 compute_fill_extra_space (property_table_entry *entry)
8675 {
8676 int fill_extra_space;
8677
8678 if (!entry)
8679 return 0;
8680
8681 if ((entry->flags & XTENSA_PROP_UNREACHABLE) == 0)
8682 return 0;
8683
8684 fill_extra_space = entry->size;
8685 if ((entry->flags & XTENSA_PROP_ALIGN) != 0)
8686 {
8687 /* Fill bytes for alignment:
8688 (2**n)-1 - (addr + (2**n)-1) & (2**n -1) */
8689 int pow = GET_XTENSA_PROP_ALIGNMENT (entry->flags);
8690 int nsm = (1 << pow) - 1;
8691 bfd_vma addr = entry->address + entry->size;
8692 bfd_vma align_fill = nsm - ((addr + nsm) & nsm);
8693 fill_extra_space += align_fill;
8694 }
8695 return fill_extra_space;
8696 }
8697
8698 \f
8699 /* First relaxation pass. */
8700
8701 /* If the section contains relaxable literals, check each literal to
8702 see if it has the same value as another literal that has already
8703 been seen, either in the current section or a previous one. If so,
8704 add an entry to the per-section list of removed literals. The
8705 actual changes are deferred until the next pass. */
8706
8707 static bfd_boolean
8708 compute_removed_literals (bfd *abfd,
8709 asection *sec,
8710 struct bfd_link_info *link_info,
8711 value_map_hash_table *values)
8712 {
8713 xtensa_relax_info *relax_info;
8714 bfd_byte *contents;
8715 Elf_Internal_Rela *internal_relocs;
8716 source_reloc *src_relocs, *rel;
8717 bfd_boolean ok = TRUE;
8718 property_table_entry *prop_table = NULL;
8719 int ptblsize;
8720 int i, prev_i;
8721 bfd_boolean last_loc_is_prev = FALSE;
8722 bfd_vma last_target_offset = 0;
8723 section_cache_t target_sec_cache;
8724 bfd_size_type sec_size;
8725
8726 init_section_cache (&target_sec_cache);
8727
8728 /* Do nothing if it is not a relaxable literal section. */
8729 relax_info = get_xtensa_relax_info (sec);
8730 BFD_ASSERT (relax_info);
8731 if (!relax_info->is_relaxable_literal_section)
8732 return ok;
8733
8734 internal_relocs = retrieve_internal_relocs (abfd, sec,
8735 link_info->keep_memory);
8736
8737 sec_size = bfd_get_section_limit (abfd, sec);
8738 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
8739 if (contents == NULL && sec_size != 0)
8740 {
8741 ok = FALSE;
8742 goto error_return;
8743 }
8744
8745 /* Sort the source_relocs by target offset. */
8746 src_relocs = relax_info->src_relocs;
8747 qsort (src_relocs, relax_info->src_count,
8748 sizeof (source_reloc), source_reloc_compare);
8749 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
8750 internal_reloc_compare);
8751
8752 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
8753 XTENSA_PROP_SEC_NAME, FALSE);
8754 if (ptblsize < 0)
8755 {
8756 ok = FALSE;
8757 goto error_return;
8758 }
8759
8760 prev_i = -1;
8761 for (i = 0; i < relax_info->src_count; i++)
8762 {
8763 Elf_Internal_Rela *irel = NULL;
8764
8765 rel = &src_relocs[i];
8766 if (get_l32r_opcode () != rel->opcode)
8767 continue;
8768 irel = get_irel_at_offset (sec, internal_relocs,
8769 rel->r_rel.target_offset);
8770
8771 /* If the relocation on this is not a simple R_XTENSA_32 or
8772 R_XTENSA_PLT then do not consider it. This may happen when
8773 the difference of two symbols is used in a literal. */
8774 if (irel && (ELF32_R_TYPE (irel->r_info) != R_XTENSA_32
8775 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_PLT))
8776 continue;
8777
8778 /* If the target_offset for this relocation is the same as the
8779 previous relocation, then we've already considered whether the
8780 literal can be coalesced. Skip to the next one.... */
8781 if (i != 0 && prev_i != -1
8782 && src_relocs[i-1].r_rel.target_offset == rel->r_rel.target_offset)
8783 continue;
8784 prev_i = i;
8785
8786 if (last_loc_is_prev &&
8787 last_target_offset + 4 != rel->r_rel.target_offset)
8788 last_loc_is_prev = FALSE;
8789
8790 /* Check if the relocation was from an L32R that is being removed
8791 because a CALLX was converted to a direct CALL, and check if
8792 there are no other relocations to the literal. */
8793 if (is_removable_literal (rel, i, src_relocs, relax_info->src_count,
8794 sec, prop_table, ptblsize))
8795 {
8796 if (!remove_dead_literal (abfd, sec, link_info, internal_relocs,
8797 irel, rel, prop_table, ptblsize))
8798 {
8799 ok = FALSE;
8800 goto error_return;
8801 }
8802 last_target_offset = rel->r_rel.target_offset;
8803 continue;
8804 }
8805
8806 if (!identify_literal_placement (abfd, sec, contents, link_info,
8807 values,
8808 &last_loc_is_prev, irel,
8809 relax_info->src_count - i, rel,
8810 prop_table, ptblsize,
8811 &target_sec_cache, rel->is_abs_literal))
8812 {
8813 ok = FALSE;
8814 goto error_return;
8815 }
8816 last_target_offset = rel->r_rel.target_offset;
8817 }
8818
8819 #if DEBUG
8820 print_removed_literals (stderr, &relax_info->removed_list);
8821 print_action_list (stderr, &relax_info->action_list);
8822 #endif /* DEBUG */
8823
8824 error_return:
8825 if (prop_table)
8826 free (prop_table);
8827 free_section_cache (&target_sec_cache);
8828
8829 release_contents (sec, contents);
8830 release_internal_relocs (sec, internal_relocs);
8831 return ok;
8832 }
8833
8834
8835 static Elf_Internal_Rela *
8836 get_irel_at_offset (asection *sec,
8837 Elf_Internal_Rela *internal_relocs,
8838 bfd_vma offset)
8839 {
8840 unsigned i;
8841 Elf_Internal_Rela *irel;
8842 unsigned r_type;
8843 Elf_Internal_Rela key;
8844
8845 if (!internal_relocs)
8846 return NULL;
8847
8848 key.r_offset = offset;
8849 irel = bsearch (&key, internal_relocs, sec->reloc_count,
8850 sizeof (Elf_Internal_Rela), internal_reloc_matches);
8851 if (!irel)
8852 return NULL;
8853
8854 /* bsearch does not guarantee which will be returned if there are
8855 multiple matches. We need the first that is not an alignment. */
8856 i = irel - internal_relocs;
8857 while (i > 0)
8858 {
8859 if (internal_relocs[i-1].r_offset != offset)
8860 break;
8861 i--;
8862 }
8863 for ( ; i < sec->reloc_count; i++)
8864 {
8865 irel = &internal_relocs[i];
8866 r_type = ELF32_R_TYPE (irel->r_info);
8867 if (irel->r_offset == offset && r_type != R_XTENSA_NONE)
8868 return irel;
8869 }
8870
8871 return NULL;
8872 }
8873
8874
8875 bfd_boolean
8876 is_removable_literal (const source_reloc *rel,
8877 int i,
8878 const source_reloc *src_relocs,
8879 int src_count,
8880 asection *sec,
8881 property_table_entry *prop_table,
8882 int ptblsize)
8883 {
8884 const source_reloc *curr_rel;
8885 property_table_entry *entry;
8886
8887 if (!rel->is_null)
8888 return FALSE;
8889
8890 entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8891 sec->vma + rel->r_rel.target_offset);
8892 if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
8893 return FALSE;
8894
8895 for (++i; i < src_count; ++i)
8896 {
8897 curr_rel = &src_relocs[i];
8898 /* If all others have the same target offset.... */
8899 if (curr_rel->r_rel.target_offset != rel->r_rel.target_offset)
8900 return TRUE;
8901
8902 if (!curr_rel->is_null
8903 && !xtensa_is_property_section (curr_rel->source_sec)
8904 && !(curr_rel->source_sec->flags & SEC_DEBUGGING))
8905 return FALSE;
8906 }
8907 return TRUE;
8908 }
8909
8910
8911 bfd_boolean
8912 remove_dead_literal (bfd *abfd,
8913 asection *sec,
8914 struct bfd_link_info *link_info,
8915 Elf_Internal_Rela *internal_relocs,
8916 Elf_Internal_Rela *irel,
8917 source_reloc *rel,
8918 property_table_entry *prop_table,
8919 int ptblsize)
8920 {
8921 property_table_entry *entry;
8922 xtensa_relax_info *relax_info;
8923
8924 relax_info = get_xtensa_relax_info (sec);
8925 if (!relax_info)
8926 return FALSE;
8927
8928 entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8929 sec->vma + rel->r_rel.target_offset);
8930
8931 /* Mark the unused literal so that it will be removed. */
8932 add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
8933
8934 text_action_add (&relax_info->action_list,
8935 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
8936
8937 /* If the section is 4-byte aligned, do not add fill. */
8938 if (sec->alignment_power > 2)
8939 {
8940 int fill_extra_space;
8941 bfd_vma entry_sec_offset;
8942 text_action *fa;
8943 property_table_entry *the_add_entry;
8944 int removed_diff;
8945
8946 if (entry)
8947 entry_sec_offset = entry->address - sec->vma + entry->size;
8948 else
8949 entry_sec_offset = rel->r_rel.target_offset + 4;
8950
8951 /* If the literal range is at the end of the section,
8952 do not add fill. */
8953 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8954 entry_sec_offset);
8955 fill_extra_space = compute_fill_extra_space (the_add_entry);
8956
8957 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
8958 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
8959 -4, fill_extra_space);
8960 if (fa)
8961 adjust_fill_action (fa, removed_diff);
8962 else
8963 text_action_add (&relax_info->action_list,
8964 ta_fill, sec, entry_sec_offset, removed_diff);
8965 }
8966
8967 /* Zero out the relocation on this literal location. */
8968 if (irel)
8969 {
8970 if (elf_hash_table (link_info)->dynamic_sections_created)
8971 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
8972
8973 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
8974 pin_internal_relocs (sec, internal_relocs);
8975 }
8976
8977 /* Do not modify "last_loc_is_prev". */
8978 return TRUE;
8979 }
8980
8981
8982 bfd_boolean
8983 identify_literal_placement (bfd *abfd,
8984 asection *sec,
8985 bfd_byte *contents,
8986 struct bfd_link_info *link_info,
8987 value_map_hash_table *values,
8988 bfd_boolean *last_loc_is_prev_p,
8989 Elf_Internal_Rela *irel,
8990 int remaining_src_rels,
8991 source_reloc *rel,
8992 property_table_entry *prop_table,
8993 int ptblsize,
8994 section_cache_t *target_sec_cache,
8995 bfd_boolean is_abs_literal)
8996 {
8997 literal_value val;
8998 value_map *val_map;
8999 xtensa_relax_info *relax_info;
9000 bfd_boolean literal_placed = FALSE;
9001 r_reloc r_rel;
9002 unsigned long value;
9003 bfd_boolean final_static_link;
9004 bfd_size_type sec_size;
9005
9006 relax_info = get_xtensa_relax_info (sec);
9007 if (!relax_info)
9008 return FALSE;
9009
9010 sec_size = bfd_get_section_limit (abfd, sec);
9011
9012 final_static_link =
9013 (!bfd_link_relocatable (link_info)
9014 && !elf_hash_table (link_info)->dynamic_sections_created);
9015
9016 /* The placement algorithm first checks to see if the literal is
9017 already in the value map. If so and the value map is reachable
9018 from all uses, then the literal is moved to that location. If
9019 not, then we identify the last location where a fresh literal was
9020 placed. If the literal can be safely moved there, then we do so.
9021 If not, then we assume that the literal is not to move and leave
9022 the literal where it is, marking it as the last literal
9023 location. */
9024
9025 /* Find the literal value. */
9026 value = 0;
9027 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
9028 if (!irel)
9029 {
9030 BFD_ASSERT (rel->r_rel.target_offset < sec_size);
9031 value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
9032 }
9033 init_literal_value (&val, &r_rel, value, is_abs_literal);
9034
9035 /* Check if we've seen another literal with the same value that
9036 is in the same output section. */
9037 val_map = value_map_get_cached_value (values, &val, final_static_link);
9038
9039 if (val_map
9040 && (r_reloc_get_section (&val_map->loc)->output_section
9041 == sec->output_section)
9042 && relocations_reach (rel, remaining_src_rels, &val_map->loc)
9043 && coalesce_shared_literal (sec, rel, prop_table, ptblsize, val_map))
9044 {
9045 /* No change to last_loc_is_prev. */
9046 literal_placed = TRUE;
9047 }
9048
9049 /* For relocatable links, do not try to move literals. To do it
9050 correctly might increase the number of relocations in an input
9051 section making the default relocatable linking fail. */
9052 if (!bfd_link_relocatable (link_info) && !literal_placed
9053 && values->has_last_loc && !(*last_loc_is_prev_p))
9054 {
9055 asection *target_sec = r_reloc_get_section (&values->last_loc);
9056 if (target_sec && target_sec->output_section == sec->output_section)
9057 {
9058 /* Increment the virtual offset. */
9059 r_reloc try_loc = values->last_loc;
9060 try_loc.virtual_offset += 4;
9061
9062 /* There is a last loc that was in the same output section. */
9063 if (relocations_reach (rel, remaining_src_rels, &try_loc)
9064 && move_shared_literal (sec, link_info, rel,
9065 prop_table, ptblsize,
9066 &try_loc, &val, target_sec_cache))
9067 {
9068 values->last_loc.virtual_offset += 4;
9069 literal_placed = TRUE;
9070 if (!val_map)
9071 val_map = add_value_map (values, &val, &try_loc,
9072 final_static_link);
9073 else
9074 val_map->loc = try_loc;
9075 }
9076 }
9077 }
9078
9079 if (!literal_placed)
9080 {
9081 /* Nothing worked, leave the literal alone but update the last loc. */
9082 values->has_last_loc = TRUE;
9083 values->last_loc = rel->r_rel;
9084 if (!val_map)
9085 val_map = add_value_map (values, &val, &rel->r_rel, final_static_link);
9086 else
9087 val_map->loc = rel->r_rel;
9088 *last_loc_is_prev_p = TRUE;
9089 }
9090
9091 return TRUE;
9092 }
9093
9094
9095 /* Check if the original relocations (presumably on L32R instructions)
9096 identified by reloc[0..N] can be changed to reference the literal
9097 identified by r_rel. If r_rel is out of range for any of the
9098 original relocations, then we don't want to coalesce the original
9099 literal with the one at r_rel. We only check reloc[0..N], where the
9100 offsets are all the same as for reloc[0] (i.e., they're all
9101 referencing the same literal) and where N is also bounded by the
9102 number of remaining entries in the "reloc" array. The "reloc" array
9103 is sorted by target offset so we know all the entries for the same
9104 literal will be contiguous. */
9105
9106 static bfd_boolean
9107 relocations_reach (source_reloc *reloc,
9108 int remaining_relocs,
9109 const r_reloc *r_rel)
9110 {
9111 bfd_vma from_offset, source_address, dest_address;
9112 asection *sec;
9113 int i;
9114
9115 if (!r_reloc_is_defined (r_rel))
9116 return FALSE;
9117
9118 sec = r_reloc_get_section (r_rel);
9119 from_offset = reloc[0].r_rel.target_offset;
9120
9121 for (i = 0; i < remaining_relocs; i++)
9122 {
9123 if (reloc[i].r_rel.target_offset != from_offset)
9124 break;
9125
9126 /* Ignore relocations that have been removed. */
9127 if (reloc[i].is_null)
9128 continue;
9129
9130 /* The original and new output section for these must be the same
9131 in order to coalesce. */
9132 if (r_reloc_get_section (&reloc[i].r_rel)->output_section
9133 != sec->output_section)
9134 return FALSE;
9135
9136 /* Absolute literals in the same output section can always be
9137 combined. */
9138 if (reloc[i].is_abs_literal)
9139 continue;
9140
9141 /* A literal with no PC-relative relocations can be moved anywhere. */
9142 if (reloc[i].opnd != -1)
9143 {
9144 /* Otherwise, check to see that it fits. */
9145 source_address = (reloc[i].source_sec->output_section->vma
9146 + reloc[i].source_sec->output_offset
9147 + reloc[i].r_rel.rela.r_offset);
9148 dest_address = (sec->output_section->vma
9149 + sec->output_offset
9150 + r_rel->target_offset);
9151
9152 if (!pcrel_reloc_fits (reloc[i].opcode, reloc[i].opnd,
9153 source_address, dest_address))
9154 return FALSE;
9155 }
9156 }
9157
9158 return TRUE;
9159 }
9160
9161
9162 /* Move a literal to another literal location because it is
9163 the same as the other literal value. */
9164
9165 static bfd_boolean
9166 coalesce_shared_literal (asection *sec,
9167 source_reloc *rel,
9168 property_table_entry *prop_table,
9169 int ptblsize,
9170 value_map *val_map)
9171 {
9172 property_table_entry *entry;
9173 text_action *fa;
9174 property_table_entry *the_add_entry;
9175 int removed_diff;
9176 xtensa_relax_info *relax_info;
9177
9178 relax_info = get_xtensa_relax_info (sec);
9179 if (!relax_info)
9180 return FALSE;
9181
9182 entry = elf_xtensa_find_property_entry
9183 (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
9184 if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
9185 return TRUE;
9186
9187 /* Mark that the literal will be coalesced. */
9188 add_removed_literal (&relax_info->removed_list, &rel->r_rel, &val_map->loc);
9189
9190 text_action_add (&relax_info->action_list,
9191 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
9192
9193 /* If the section is 4-byte aligned, do not add fill. */
9194 if (sec->alignment_power > 2)
9195 {
9196 int fill_extra_space;
9197 bfd_vma entry_sec_offset;
9198
9199 if (entry)
9200 entry_sec_offset = entry->address - sec->vma + entry->size;
9201 else
9202 entry_sec_offset = rel->r_rel.target_offset + 4;
9203
9204 /* If the literal range is at the end of the section,
9205 do not add fill. */
9206 fill_extra_space = 0;
9207 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9208 entry_sec_offset);
9209 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9210 fill_extra_space = the_add_entry->size;
9211
9212 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
9213 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
9214 -4, fill_extra_space);
9215 if (fa)
9216 adjust_fill_action (fa, removed_diff);
9217 else
9218 text_action_add (&relax_info->action_list,
9219 ta_fill, sec, entry_sec_offset, removed_diff);
9220 }
9221
9222 return TRUE;
9223 }
9224
9225
9226 /* Move a literal to another location. This may actually increase the
9227 total amount of space used because of alignments so we need to do
9228 this carefully. Also, it may make a branch go out of range. */
9229
9230 static bfd_boolean
9231 move_shared_literal (asection *sec,
9232 struct bfd_link_info *link_info,
9233 source_reloc *rel,
9234 property_table_entry *prop_table,
9235 int ptblsize,
9236 const r_reloc *target_loc,
9237 const literal_value *lit_value,
9238 section_cache_t *target_sec_cache)
9239 {
9240 property_table_entry *the_add_entry, *src_entry, *target_entry = NULL;
9241 text_action *fa, *target_fa;
9242 int removed_diff;
9243 xtensa_relax_info *relax_info, *target_relax_info;
9244 asection *target_sec;
9245 ebb_t *ebb;
9246 ebb_constraint ebb_table;
9247 bfd_boolean relocs_fit;
9248
9249 /* If this routine always returns FALSE, the literals that cannot be
9250 coalesced will not be moved. */
9251 if (elf32xtensa_no_literal_movement)
9252 return FALSE;
9253
9254 relax_info = get_xtensa_relax_info (sec);
9255 if (!relax_info)
9256 return FALSE;
9257
9258 target_sec = r_reloc_get_section (target_loc);
9259 target_relax_info = get_xtensa_relax_info (target_sec);
9260
9261 /* Literals to undefined sections may not be moved because they
9262 must report an error. */
9263 if (bfd_is_und_section (target_sec))
9264 return FALSE;
9265
9266 src_entry = elf_xtensa_find_property_entry
9267 (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
9268
9269 if (!section_cache_section (target_sec_cache, target_sec, link_info))
9270 return FALSE;
9271
9272 target_entry = elf_xtensa_find_property_entry
9273 (target_sec_cache->ptbl, target_sec_cache->pte_count,
9274 target_sec->vma + target_loc->target_offset);
9275
9276 if (!target_entry)
9277 return FALSE;
9278
9279 /* Make sure that we have not broken any branches. */
9280 relocs_fit = FALSE;
9281
9282 init_ebb_constraint (&ebb_table);
9283 ebb = &ebb_table.ebb;
9284 init_ebb (ebb, target_sec_cache->sec, target_sec_cache->contents,
9285 target_sec_cache->content_length,
9286 target_sec_cache->ptbl, target_sec_cache->pte_count,
9287 target_sec_cache->relocs, target_sec_cache->reloc_count);
9288
9289 /* Propose to add 4 bytes + worst-case alignment size increase to
9290 destination. */
9291 ebb_propose_action (&ebb_table, EBB_NO_ALIGN, 0,
9292 ta_fill, target_loc->target_offset,
9293 -4 - (1 << target_sec->alignment_power), TRUE);
9294
9295 /* Check all of the PC-relative relocations to make sure they still fit. */
9296 relocs_fit = check_section_ebb_pcrels_fit (target_sec->owner, target_sec,
9297 target_sec_cache->contents,
9298 target_sec_cache->relocs, NULL,
9299 &ebb_table, NULL);
9300
9301 if (!relocs_fit)
9302 return FALSE;
9303
9304 text_action_add_literal (&target_relax_info->action_list,
9305 ta_add_literal, target_loc, lit_value, -4);
9306
9307 if (target_sec->alignment_power > 2 && target_entry != src_entry)
9308 {
9309 /* May need to add or remove some fill to maintain alignment. */
9310 int fill_extra_space;
9311 bfd_vma entry_sec_offset;
9312
9313 entry_sec_offset =
9314 target_entry->address - target_sec->vma + target_entry->size;
9315
9316 /* If the literal range is at the end of the section,
9317 do not add fill. */
9318 fill_extra_space = 0;
9319 the_add_entry =
9320 elf_xtensa_find_property_entry (target_sec_cache->ptbl,
9321 target_sec_cache->pte_count,
9322 entry_sec_offset);
9323 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9324 fill_extra_space = the_add_entry->size;
9325
9326 target_fa = find_fill_action (&target_relax_info->action_list,
9327 target_sec, entry_sec_offset);
9328 removed_diff = compute_removed_action_diff (target_fa, target_sec,
9329 entry_sec_offset, 4,
9330 fill_extra_space);
9331 if (target_fa)
9332 adjust_fill_action (target_fa, removed_diff);
9333 else
9334 text_action_add (&target_relax_info->action_list,
9335 ta_fill, target_sec, entry_sec_offset, removed_diff);
9336 }
9337
9338 /* Mark that the literal will be moved to the new location. */
9339 add_removed_literal (&relax_info->removed_list, &rel->r_rel, target_loc);
9340
9341 /* Remove the literal. */
9342 text_action_add (&relax_info->action_list,
9343 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
9344
9345 /* If the section is 4-byte aligned, do not add fill. */
9346 if (sec->alignment_power > 2 && target_entry != src_entry)
9347 {
9348 int fill_extra_space;
9349 bfd_vma entry_sec_offset;
9350
9351 if (src_entry)
9352 entry_sec_offset = src_entry->address - sec->vma + src_entry->size;
9353 else
9354 entry_sec_offset = rel->r_rel.target_offset+4;
9355
9356 /* If the literal range is at the end of the section,
9357 do not add fill. */
9358 fill_extra_space = 0;
9359 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9360 entry_sec_offset);
9361 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9362 fill_extra_space = the_add_entry->size;
9363
9364 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
9365 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
9366 -4, fill_extra_space);
9367 if (fa)
9368 adjust_fill_action (fa, removed_diff);
9369 else
9370 text_action_add (&relax_info->action_list,
9371 ta_fill, sec, entry_sec_offset, removed_diff);
9372 }
9373
9374 return TRUE;
9375 }
9376
9377 \f
9378 /* Second relaxation pass. */
9379
9380 static int
9381 action_remove_bytes_fn (splay_tree_node node, void *p)
9382 {
9383 bfd_size_type *final_size = p;
9384 text_action *action = (text_action *)node->value;
9385
9386 *final_size -= action->removed_bytes;
9387 return 0;
9388 }
9389
9390 /* Modify all of the relocations to point to the right spot, and if this
9391 is a relaxable section, delete the unwanted literals and fix the
9392 section size. */
9393
9394 bfd_boolean
9395 relax_section (bfd *abfd, asection *sec, struct bfd_link_info *link_info)
9396 {
9397 Elf_Internal_Rela *internal_relocs;
9398 xtensa_relax_info *relax_info;
9399 bfd_byte *contents;
9400 bfd_boolean ok = TRUE;
9401 unsigned i;
9402 bfd_boolean rv = FALSE;
9403 bfd_boolean virtual_action;
9404 bfd_size_type sec_size;
9405
9406 sec_size = bfd_get_section_limit (abfd, sec);
9407 relax_info = get_xtensa_relax_info (sec);
9408 BFD_ASSERT (relax_info);
9409
9410 /* First translate any of the fixes that have been added already. */
9411 translate_section_fixes (sec);
9412
9413 /* Handle property sections (e.g., literal tables) specially. */
9414 if (xtensa_is_property_section (sec))
9415 {
9416 BFD_ASSERT (!relax_info->is_relaxable_literal_section);
9417 return relax_property_section (abfd, sec, link_info);
9418 }
9419
9420 internal_relocs = retrieve_internal_relocs (abfd, sec,
9421 link_info->keep_memory);
9422 if (!internal_relocs && !action_list_count (&relax_info->action_list))
9423 return TRUE;
9424
9425 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
9426 if (contents == NULL && sec_size != 0)
9427 {
9428 ok = FALSE;
9429 goto error_return;
9430 }
9431
9432 if (internal_relocs)
9433 {
9434 for (i = 0; i < sec->reloc_count; i++)
9435 {
9436 Elf_Internal_Rela *irel;
9437 xtensa_relax_info *target_relax_info;
9438 bfd_vma source_offset, old_source_offset;
9439 r_reloc r_rel;
9440 unsigned r_type;
9441 asection *target_sec;
9442
9443 /* Locally change the source address.
9444 Translate the target to the new target address.
9445 If it points to this section and has been removed,
9446 NULLify it.
9447 Write it back. */
9448
9449 irel = &internal_relocs[i];
9450 source_offset = irel->r_offset;
9451 old_source_offset = source_offset;
9452
9453 r_type = ELF32_R_TYPE (irel->r_info);
9454 r_reloc_init (&r_rel, abfd, irel, contents,
9455 bfd_get_section_limit (abfd, sec));
9456
9457 /* If this section could have changed then we may need to
9458 change the relocation's offset. */
9459
9460 if (relax_info->is_relaxable_literal_section
9461 || relax_info->is_relaxable_asm_section)
9462 {
9463 pin_internal_relocs (sec, internal_relocs);
9464
9465 if (r_type != R_XTENSA_NONE
9466 && find_removed_literal (&relax_info->removed_list,
9467 irel->r_offset))
9468 {
9469 /* Remove this relocation. */
9470 if (elf_hash_table (link_info)->dynamic_sections_created)
9471 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
9472 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
9473 irel->r_offset = offset_with_removed_text_map
9474 (&relax_info->action_list, irel->r_offset);
9475 continue;
9476 }
9477
9478 if (r_type == R_XTENSA_ASM_SIMPLIFY)
9479 {
9480 text_action *action =
9481 find_insn_action (&relax_info->action_list,
9482 irel->r_offset);
9483 if (action && (action->action == ta_convert_longcall
9484 || action->action == ta_remove_longcall))
9485 {
9486 bfd_reloc_status_type retval;
9487 char *error_message = NULL;
9488
9489 retval = contract_asm_expansion (contents, sec_size,
9490 irel, &error_message);
9491 if (retval != bfd_reloc_ok)
9492 {
9493 (*link_info->callbacks->reloc_dangerous)
9494 (link_info, error_message, abfd, sec,
9495 irel->r_offset);
9496 goto error_return;
9497 }
9498 /* Update the action so that the code that moves
9499 the contents will do the right thing. */
9500 /* ta_remove_longcall and ta_remove_insn actions are
9501 grouped together in the tree as well as
9502 ta_convert_longcall and ta_none, so that changes below
9503 can be done w/o removing and reinserting action into
9504 the tree. */
9505
9506 if (action->action == ta_remove_longcall)
9507 action->action = ta_remove_insn;
9508 else
9509 action->action = ta_none;
9510 /* Refresh the info in the r_rel. */
9511 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
9512 r_type = ELF32_R_TYPE (irel->r_info);
9513 }
9514 }
9515
9516 source_offset = offset_with_removed_text_map
9517 (&relax_info->action_list, irel->r_offset);
9518 irel->r_offset = source_offset;
9519 }
9520
9521 /* If the target section could have changed then
9522 we may need to change the relocation's target offset. */
9523
9524 target_sec = r_reloc_get_section (&r_rel);
9525
9526 /* For a reference to a discarded section from a DWARF section,
9527 i.e., where action_discarded is PRETEND, the symbol will
9528 eventually be modified to refer to the kept section (at least if
9529 the kept and discarded sections are the same size). Anticipate
9530 that here and adjust things accordingly. */
9531 if (! elf_xtensa_ignore_discarded_relocs (sec)
9532 && elf_xtensa_action_discarded (sec) == PRETEND
9533 && sec->sec_info_type != SEC_INFO_TYPE_STABS
9534 && target_sec != NULL
9535 && discarded_section (target_sec))
9536 {
9537 /* It would be natural to call _bfd_elf_check_kept_section
9538 here, but it's not exported from elflink.c. It's also a
9539 fairly expensive check. Adjusting the relocations to the
9540 discarded section is fairly harmless; it will only adjust
9541 some addends and difference values. If it turns out that
9542 _bfd_elf_check_kept_section fails later, it won't matter,
9543 so just compare the section names to find the right group
9544 member. */
9545 asection *kept = target_sec->kept_section;
9546 if (kept != NULL)
9547 {
9548 if ((kept->flags & SEC_GROUP) != 0)
9549 {
9550 asection *first = elf_next_in_group (kept);
9551 asection *s = first;
9552
9553 kept = NULL;
9554 while (s != NULL)
9555 {
9556 if (strcmp (s->name, target_sec->name) == 0)
9557 {
9558 kept = s;
9559 break;
9560 }
9561 s = elf_next_in_group (s);
9562 if (s == first)
9563 break;
9564 }
9565 }
9566 }
9567 if (kept != NULL
9568 && ((target_sec->rawsize != 0
9569 ? target_sec->rawsize : target_sec->size)
9570 == (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9571 target_sec = kept;
9572 }
9573
9574 target_relax_info = get_xtensa_relax_info (target_sec);
9575 if (target_relax_info
9576 && (target_relax_info->is_relaxable_literal_section
9577 || target_relax_info->is_relaxable_asm_section))
9578 {
9579 r_reloc new_reloc;
9580 target_sec = translate_reloc (&r_rel, &new_reloc, target_sec);
9581
9582 if (r_type == R_XTENSA_DIFF8
9583 || r_type == R_XTENSA_DIFF16
9584 || r_type == R_XTENSA_DIFF32)
9585 {
9586 bfd_signed_vma diff_value = 0;
9587 bfd_vma new_end_offset, diff_mask = 0;
9588
9589 if (bfd_get_section_limit (abfd, sec) < old_source_offset)
9590 {
9591 (*link_info->callbacks->reloc_dangerous)
9592 (link_info, _("invalid relocation address"),
9593 abfd, sec, old_source_offset);
9594 goto error_return;
9595 }
9596
9597 switch (r_type)
9598 {
9599 case R_XTENSA_DIFF8:
9600 diff_value =
9601 bfd_get_signed_8 (abfd, &contents[old_source_offset]);
9602 break;
9603 case R_XTENSA_DIFF16:
9604 diff_value =
9605 bfd_get_signed_16 (abfd, &contents[old_source_offset]);
9606 break;
9607 case R_XTENSA_DIFF32:
9608 diff_value =
9609 bfd_get_signed_32 (abfd, &contents[old_source_offset]);
9610 break;
9611 }
9612
9613 new_end_offset = offset_with_removed_text_map
9614 (&target_relax_info->action_list,
9615 r_rel.target_offset + diff_value);
9616 diff_value = new_end_offset - new_reloc.target_offset;
9617
9618 switch (r_type)
9619 {
9620 case R_XTENSA_DIFF8:
9621 diff_mask = 0x7f;
9622 bfd_put_signed_8 (abfd, diff_value,
9623 &contents[old_source_offset]);
9624 break;
9625 case R_XTENSA_DIFF16:
9626 diff_mask = 0x7fff;
9627 bfd_put_signed_16 (abfd, diff_value,
9628 &contents[old_source_offset]);
9629 break;
9630 case R_XTENSA_DIFF32:
9631 diff_mask = 0x7fffffff;
9632 bfd_put_signed_32 (abfd, diff_value,
9633 &contents[old_source_offset]);
9634 break;
9635 }
9636
9637 /* Check for overflow. Sign bits must be all zeroes or all ones */
9638 if ((diff_value & ~diff_mask) != 0 &&
9639 (diff_value & ~diff_mask) != (-1 & ~diff_mask))
9640 {
9641 (*link_info->callbacks->reloc_dangerous)
9642 (link_info, _("overflow after relaxation"),
9643 abfd, sec, old_source_offset);
9644 goto error_return;
9645 }
9646
9647 pin_contents (sec, contents);
9648 }
9649
9650 /* If the relocation still references a section in the same
9651 input file, modify the relocation directly instead of
9652 adding a "fix" record. */
9653 if (target_sec->owner == abfd)
9654 {
9655 unsigned r_symndx = ELF32_R_SYM (new_reloc.rela.r_info);
9656 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
9657 irel->r_addend = new_reloc.rela.r_addend;
9658 pin_internal_relocs (sec, internal_relocs);
9659 }
9660 else
9661 {
9662 bfd_vma addend_displacement;
9663 reloc_bfd_fix *fix;
9664
9665 addend_displacement =
9666 new_reloc.target_offset + new_reloc.virtual_offset;
9667 fix = reloc_bfd_fix_init (sec, source_offset, r_type,
9668 target_sec,
9669 addend_displacement, TRUE);
9670 add_fix (sec, fix);
9671 }
9672 }
9673 }
9674 }
9675
9676 if ((relax_info->is_relaxable_literal_section
9677 || relax_info->is_relaxable_asm_section)
9678 && action_list_count (&relax_info->action_list))
9679 {
9680 /* Walk through the planned actions and build up a table
9681 of move, copy and fill records. Use the move, copy and
9682 fill records to perform the actions once. */
9683
9684 bfd_size_type final_size, copy_size, orig_insn_size;
9685 bfd_byte *scratch = NULL;
9686 bfd_byte *dup_contents = NULL;
9687 bfd_size_type orig_size = sec->size;
9688 bfd_vma orig_dot = 0;
9689 bfd_vma orig_dot_copied = 0; /* Byte copied already from
9690 orig dot in physical memory. */
9691 bfd_vma orig_dot_vo = 0; /* Virtual offset from orig_dot. */
9692 bfd_vma dup_dot = 0;
9693
9694 text_action *action;
9695
9696 final_size = sec->size;
9697
9698 splay_tree_foreach (relax_info->action_list.tree,
9699 action_remove_bytes_fn, &final_size);
9700 scratch = (bfd_byte *) bfd_zmalloc (final_size);
9701 dup_contents = (bfd_byte *) bfd_zmalloc (final_size);
9702
9703 /* The dot is the current fill location. */
9704 #if DEBUG
9705 print_action_list (stderr, &relax_info->action_list);
9706 #endif
9707
9708 for (action = action_first (&relax_info->action_list); action;
9709 action = action_next (&relax_info->action_list, action))
9710 {
9711 virtual_action = FALSE;
9712 if (action->offset > orig_dot)
9713 {
9714 orig_dot += orig_dot_copied;
9715 orig_dot_copied = 0;
9716 orig_dot_vo = 0;
9717 /* Out of the virtual world. */
9718 }
9719
9720 if (action->offset > orig_dot)
9721 {
9722 copy_size = action->offset - orig_dot;
9723 memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
9724 orig_dot += copy_size;
9725 dup_dot += copy_size;
9726 BFD_ASSERT (action->offset == orig_dot);
9727 }
9728 else if (action->offset < orig_dot)
9729 {
9730 if (action->action == ta_fill
9731 && action->offset - action->removed_bytes == orig_dot)
9732 {
9733 /* This is OK because the fill only effects the dup_dot. */
9734 }
9735 else if (action->action == ta_add_literal)
9736 {
9737 /* TBD. Might need to handle this. */
9738 }
9739 }
9740 if (action->offset == orig_dot)
9741 {
9742 if (action->virtual_offset > orig_dot_vo)
9743 {
9744 if (orig_dot_vo == 0)
9745 {
9746 /* Need to copy virtual_offset bytes. Probably four. */
9747 copy_size = action->virtual_offset - orig_dot_vo;
9748 memmove (&dup_contents[dup_dot],
9749 &contents[orig_dot], copy_size);
9750 orig_dot_copied = copy_size;
9751 dup_dot += copy_size;
9752 }
9753 virtual_action = TRUE;
9754 }
9755 else
9756 BFD_ASSERT (action->virtual_offset <= orig_dot_vo);
9757 }
9758 switch (action->action)
9759 {
9760 case ta_remove_literal:
9761 case ta_remove_insn:
9762 BFD_ASSERT (action->removed_bytes >= 0);
9763 orig_dot += action->removed_bytes;
9764 break;
9765
9766 case ta_narrow_insn:
9767 orig_insn_size = 3;
9768 copy_size = 2;
9769 memmove (scratch, &contents[orig_dot], orig_insn_size);
9770 BFD_ASSERT (action->removed_bytes == 1);
9771 rv = narrow_instruction (scratch, final_size, 0);
9772 BFD_ASSERT (rv);
9773 memmove (&dup_contents[dup_dot], scratch, copy_size);
9774 orig_dot += orig_insn_size;
9775 dup_dot += copy_size;
9776 break;
9777
9778 case ta_fill:
9779 if (action->removed_bytes >= 0)
9780 orig_dot += action->removed_bytes;
9781 else
9782 {
9783 /* Already zeroed in dup_contents. Just bump the
9784 counters. */
9785 dup_dot += (-action->removed_bytes);
9786 }
9787 break;
9788
9789 case ta_none:
9790 BFD_ASSERT (action->removed_bytes == 0);
9791 break;
9792
9793 case ta_convert_longcall:
9794 case ta_remove_longcall:
9795 /* These will be removed or converted before we get here. */
9796 BFD_ASSERT (0);
9797 break;
9798
9799 case ta_widen_insn:
9800 orig_insn_size = 2;
9801 copy_size = 3;
9802 memmove (scratch, &contents[orig_dot], orig_insn_size);
9803 BFD_ASSERT (action->removed_bytes == -1);
9804 rv = widen_instruction (scratch, final_size, 0);
9805 BFD_ASSERT (rv);
9806 memmove (&dup_contents[dup_dot], scratch, copy_size);
9807 orig_dot += orig_insn_size;
9808 dup_dot += copy_size;
9809 break;
9810
9811 case ta_add_literal:
9812 orig_insn_size = 0;
9813 copy_size = 4;
9814 BFD_ASSERT (action->removed_bytes == -4);
9815 /* TBD -- place the literal value here and insert
9816 into the table. */
9817 memset (&dup_contents[dup_dot], 0, 4);
9818 pin_internal_relocs (sec, internal_relocs);
9819 pin_contents (sec, contents);
9820
9821 if (!move_literal (abfd, link_info, sec, dup_dot, dup_contents,
9822 relax_info, &internal_relocs, &action->value))
9823 goto error_return;
9824
9825 if (virtual_action)
9826 orig_dot_vo += copy_size;
9827
9828 orig_dot += orig_insn_size;
9829 dup_dot += copy_size;
9830 break;
9831
9832 default:
9833 /* Not implemented yet. */
9834 BFD_ASSERT (0);
9835 break;
9836 }
9837
9838 BFD_ASSERT (dup_dot <= final_size);
9839 BFD_ASSERT (orig_dot <= orig_size);
9840 }
9841
9842 orig_dot += orig_dot_copied;
9843 orig_dot_copied = 0;
9844
9845 if (orig_dot != orig_size)
9846 {
9847 copy_size = orig_size - orig_dot;
9848 BFD_ASSERT (orig_size > orig_dot);
9849 BFD_ASSERT (dup_dot + copy_size == final_size);
9850 memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
9851 orig_dot += copy_size;
9852 dup_dot += copy_size;
9853 }
9854 BFD_ASSERT (orig_size == orig_dot);
9855 BFD_ASSERT (final_size == dup_dot);
9856
9857 /* Move the dup_contents back. */
9858 if (final_size > orig_size)
9859 {
9860 /* Contents need to be reallocated. Swap the dup_contents into
9861 contents. */
9862 sec->contents = dup_contents;
9863 free (contents);
9864 contents = dup_contents;
9865 pin_contents (sec, contents);
9866 }
9867 else
9868 {
9869 BFD_ASSERT (final_size <= orig_size);
9870 memset (contents, 0, orig_size);
9871 memcpy (contents, dup_contents, final_size);
9872 free (dup_contents);
9873 }
9874 free (scratch);
9875 pin_contents (sec, contents);
9876
9877 if (sec->rawsize == 0)
9878 sec->rawsize = sec->size;
9879 sec->size = final_size;
9880 }
9881
9882 error_return:
9883 release_internal_relocs (sec, internal_relocs);
9884 release_contents (sec, contents);
9885 return ok;
9886 }
9887
9888
9889 static bfd_boolean
9890 translate_section_fixes (asection *sec)
9891 {
9892 xtensa_relax_info *relax_info;
9893 reloc_bfd_fix *r;
9894
9895 relax_info = get_xtensa_relax_info (sec);
9896 if (!relax_info)
9897 return TRUE;
9898
9899 for (r = relax_info->fix_list; r != NULL; r = r->next)
9900 if (!translate_reloc_bfd_fix (r))
9901 return FALSE;
9902
9903 return TRUE;
9904 }
9905
9906
9907 /* Translate a fix given the mapping in the relax info for the target
9908 section. If it has already been translated, no work is required. */
9909
9910 static bfd_boolean
9911 translate_reloc_bfd_fix (reloc_bfd_fix *fix)
9912 {
9913 reloc_bfd_fix new_fix;
9914 asection *sec;
9915 xtensa_relax_info *relax_info;
9916 removed_literal *removed;
9917 bfd_vma new_offset, target_offset;
9918
9919 if (fix->translated)
9920 return TRUE;
9921
9922 sec = fix->target_sec;
9923 target_offset = fix->target_offset;
9924
9925 relax_info = get_xtensa_relax_info (sec);
9926 if (!relax_info)
9927 {
9928 fix->translated = TRUE;
9929 return TRUE;
9930 }
9931
9932 new_fix = *fix;
9933
9934 /* The fix does not need to be translated if the section cannot change. */
9935 if (!relax_info->is_relaxable_literal_section
9936 && !relax_info->is_relaxable_asm_section)
9937 {
9938 fix->translated = TRUE;
9939 return TRUE;
9940 }
9941
9942 /* If the literal has been moved and this relocation was on an
9943 opcode, then the relocation should move to the new literal
9944 location. Otherwise, the relocation should move within the
9945 section. */
9946
9947 removed = FALSE;
9948 if (is_operand_relocation (fix->src_type))
9949 {
9950 /* Check if the original relocation is against a literal being
9951 removed. */
9952 removed = find_removed_literal (&relax_info->removed_list,
9953 target_offset);
9954 }
9955
9956 if (removed)
9957 {
9958 asection *new_sec;
9959
9960 /* The fact that there is still a relocation to this literal indicates
9961 that the literal is being coalesced, not simply removed. */
9962 BFD_ASSERT (removed->to.abfd != NULL);
9963
9964 /* This was moved to some other address (possibly another section). */
9965 new_sec = r_reloc_get_section (&removed->to);
9966 if (new_sec != sec)
9967 {
9968 sec = new_sec;
9969 relax_info = get_xtensa_relax_info (sec);
9970 if (!relax_info ||
9971 (!relax_info->is_relaxable_literal_section
9972 && !relax_info->is_relaxable_asm_section))
9973 {
9974 target_offset = removed->to.target_offset;
9975 new_fix.target_sec = new_sec;
9976 new_fix.target_offset = target_offset;
9977 new_fix.translated = TRUE;
9978 *fix = new_fix;
9979 return TRUE;
9980 }
9981 }
9982 target_offset = removed->to.target_offset;
9983 new_fix.target_sec = new_sec;
9984 }
9985
9986 /* The target address may have been moved within its section. */
9987 new_offset = offset_with_removed_text (&relax_info->action_list,
9988 target_offset);
9989
9990 new_fix.target_offset = new_offset;
9991 new_fix.target_offset = new_offset;
9992 new_fix.translated = TRUE;
9993 *fix = new_fix;
9994 return TRUE;
9995 }
9996
9997
9998 /* Fix up a relocation to take account of removed literals. */
9999
10000 static asection *
10001 translate_reloc (const r_reloc *orig_rel, r_reloc *new_rel, asection *sec)
10002 {
10003 xtensa_relax_info *relax_info;
10004 removed_literal *removed;
10005 bfd_vma target_offset, base_offset;
10006
10007 *new_rel = *orig_rel;
10008
10009 if (!r_reloc_is_defined (orig_rel))
10010 return sec ;
10011
10012 relax_info = get_xtensa_relax_info (sec);
10013 BFD_ASSERT (relax_info && (relax_info->is_relaxable_literal_section
10014 || relax_info->is_relaxable_asm_section));
10015
10016 target_offset = orig_rel->target_offset;
10017
10018 removed = FALSE;
10019 if (is_operand_relocation (ELF32_R_TYPE (orig_rel->rela.r_info)))
10020 {
10021 /* Check if the original relocation is against a literal being
10022 removed. */
10023 removed = find_removed_literal (&relax_info->removed_list,
10024 target_offset);
10025 }
10026 if (removed && removed->to.abfd)
10027 {
10028 asection *new_sec;
10029
10030 /* The fact that there is still a relocation to this literal indicates
10031 that the literal is being coalesced, not simply removed. */
10032 BFD_ASSERT (removed->to.abfd != NULL);
10033
10034 /* This was moved to some other address
10035 (possibly in another section). */
10036 *new_rel = removed->to;
10037 new_sec = r_reloc_get_section (new_rel);
10038 if (new_sec != sec)
10039 {
10040 sec = new_sec;
10041 relax_info = get_xtensa_relax_info (sec);
10042 if (!relax_info
10043 || (!relax_info->is_relaxable_literal_section
10044 && !relax_info->is_relaxable_asm_section))
10045 return sec;
10046 }
10047 target_offset = new_rel->target_offset;
10048 }
10049
10050 /* Find the base offset of the reloc symbol, excluding any addend from the
10051 reloc or from the section contents (for a partial_inplace reloc). Then
10052 find the adjusted values of the offsets due to relaxation. The base
10053 offset is needed to determine the change to the reloc's addend; the reloc
10054 addend should not be adjusted due to relaxations located before the base
10055 offset. */
10056
10057 base_offset = r_reloc_get_target_offset (new_rel) - new_rel->rela.r_addend;
10058 if (base_offset <= target_offset)
10059 {
10060 int base_removed = removed_by_actions_map (&relax_info->action_list,
10061 base_offset, FALSE);
10062 int addend_removed = removed_by_actions_map (&relax_info->action_list,
10063 target_offset, FALSE) -
10064 base_removed;
10065
10066 new_rel->target_offset = target_offset - base_removed - addend_removed;
10067 new_rel->rela.r_addend -= addend_removed;
10068 }
10069 else
10070 {
10071 /* Handle a negative addend. The base offset comes first. */
10072 int tgt_removed = removed_by_actions_map (&relax_info->action_list,
10073 target_offset, FALSE);
10074 int addend_removed = removed_by_actions_map (&relax_info->action_list,
10075 base_offset, FALSE) -
10076 tgt_removed;
10077
10078 new_rel->target_offset = target_offset - tgt_removed;
10079 new_rel->rela.r_addend += addend_removed;
10080 }
10081
10082 return sec;
10083 }
10084
10085
10086 /* For dynamic links, there may be a dynamic relocation for each
10087 literal. The number of dynamic relocations must be computed in
10088 size_dynamic_sections, which occurs before relaxation. When a
10089 literal is removed, this function checks if there is a corresponding
10090 dynamic relocation and shrinks the size of the appropriate dynamic
10091 relocation section accordingly. At this point, the contents of the
10092 dynamic relocation sections have not yet been filled in, so there's
10093 nothing else that needs to be done. */
10094
10095 static void
10096 shrink_dynamic_reloc_sections (struct bfd_link_info *info,
10097 bfd *abfd,
10098 asection *input_section,
10099 Elf_Internal_Rela *rel)
10100 {
10101 struct elf_xtensa_link_hash_table *htab;
10102 Elf_Internal_Shdr *symtab_hdr;
10103 struct elf_link_hash_entry **sym_hashes;
10104 unsigned long r_symndx;
10105 int r_type;
10106 struct elf_link_hash_entry *h;
10107 bfd_boolean dynamic_symbol;
10108
10109 htab = elf_xtensa_hash_table (info);
10110 if (htab == NULL)
10111 return;
10112
10113 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10114 sym_hashes = elf_sym_hashes (abfd);
10115
10116 r_type = ELF32_R_TYPE (rel->r_info);
10117 r_symndx = ELF32_R_SYM (rel->r_info);
10118
10119 if (r_symndx < symtab_hdr->sh_info)
10120 h = NULL;
10121 else
10122 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
10123
10124 dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
10125
10126 if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
10127 && (input_section->flags & SEC_ALLOC) != 0
10128 && (dynamic_symbol || bfd_link_pic (info)))
10129 {
10130 asection *srel;
10131 bfd_boolean is_plt = FALSE;
10132
10133 if (dynamic_symbol && r_type == R_XTENSA_PLT)
10134 {
10135 srel = htab->srelplt;
10136 is_plt = TRUE;
10137 }
10138 else
10139 srel = htab->srelgot;
10140
10141 /* Reduce size of the .rela.* section by one reloc. */
10142 BFD_ASSERT (srel != NULL);
10143 BFD_ASSERT (srel->size >= sizeof (Elf32_External_Rela));
10144 srel->size -= sizeof (Elf32_External_Rela);
10145
10146 if (is_plt)
10147 {
10148 asection *splt, *sgotplt, *srelgot;
10149 int reloc_index, chunk;
10150
10151 /* Find the PLT reloc index of the entry being removed. This
10152 is computed from the size of ".rela.plt". It is needed to
10153 figure out which PLT chunk to resize. Usually "last index
10154 = size - 1" since the index starts at zero, but in this
10155 context, the size has just been decremented so there's no
10156 need to subtract one. */
10157 reloc_index = srel->size / sizeof (Elf32_External_Rela);
10158
10159 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
10160 splt = elf_xtensa_get_plt_section (info, chunk);
10161 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
10162 BFD_ASSERT (splt != NULL && sgotplt != NULL);
10163
10164 /* Check if an entire PLT chunk has just been eliminated. */
10165 if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
10166 {
10167 /* The two magic GOT entries for that chunk can go away. */
10168 srelgot = htab->srelgot;
10169 BFD_ASSERT (srelgot != NULL);
10170 srelgot->reloc_count -= 2;
10171 srelgot->size -= 2 * sizeof (Elf32_External_Rela);
10172 sgotplt->size -= 8;
10173
10174 /* There should be only one entry left (and it will be
10175 removed below). */
10176 BFD_ASSERT (sgotplt->size == 4);
10177 BFD_ASSERT (splt->size == PLT_ENTRY_SIZE);
10178 }
10179
10180 BFD_ASSERT (sgotplt->size >= 4);
10181 BFD_ASSERT (splt->size >= PLT_ENTRY_SIZE);
10182
10183 sgotplt->size -= 4;
10184 splt->size -= PLT_ENTRY_SIZE;
10185 }
10186 }
10187 }
10188
10189
10190 /* Take an r_rel and move it to another section. This usually
10191 requires extending the interal_relocation array and pinning it. If
10192 the original r_rel is from the same BFD, we can complete this here.
10193 Otherwise, we add a fix record to let the final link fix the
10194 appropriate address. Contents and internal relocations for the
10195 section must be pinned after calling this routine. */
10196
10197 static bfd_boolean
10198 move_literal (bfd *abfd,
10199 struct bfd_link_info *link_info,
10200 asection *sec,
10201 bfd_vma offset,
10202 bfd_byte *contents,
10203 xtensa_relax_info *relax_info,
10204 Elf_Internal_Rela **internal_relocs_p,
10205 const literal_value *lit)
10206 {
10207 Elf_Internal_Rela *new_relocs = NULL;
10208 size_t new_relocs_count = 0;
10209 Elf_Internal_Rela this_rela;
10210 const r_reloc *r_rel;
10211
10212 r_rel = &lit->r_rel;
10213 BFD_ASSERT (elf_section_data (sec)->relocs == *internal_relocs_p);
10214
10215 if (r_reloc_is_const (r_rel))
10216 bfd_put_32 (abfd, lit->value, contents + offset);
10217 else
10218 {
10219 int r_type;
10220 unsigned i;
10221 reloc_bfd_fix *fix;
10222 unsigned insert_at;
10223
10224 r_type = ELF32_R_TYPE (r_rel->rela.r_info);
10225
10226 /* This is the difficult case. We have to create a fix up. */
10227 this_rela.r_offset = offset;
10228 this_rela.r_info = ELF32_R_INFO (0, r_type);
10229 this_rela.r_addend =
10230 r_rel->target_offset - r_reloc_get_target_offset (r_rel);
10231 bfd_put_32 (abfd, lit->value, contents + offset);
10232
10233 /* Currently, we cannot move relocations during a relocatable link. */
10234 BFD_ASSERT (!bfd_link_relocatable (link_info));
10235 fix = reloc_bfd_fix_init (sec, offset, r_type,
10236 r_reloc_get_section (r_rel),
10237 r_rel->target_offset + r_rel->virtual_offset,
10238 FALSE);
10239 /* We also need to mark that relocations are needed here. */
10240 sec->flags |= SEC_RELOC;
10241
10242 translate_reloc_bfd_fix (fix);
10243 /* This fix has not yet been translated. */
10244 add_fix (sec, fix);
10245
10246 /* Add the relocation. If we have already allocated our own
10247 space for the relocations and we have room for more, then use
10248 it. Otherwise, allocate new space and move the literals. */
10249 insert_at = sec->reloc_count;
10250 for (i = 0; i < sec->reloc_count; ++i)
10251 {
10252 if (this_rela.r_offset < (*internal_relocs_p)[i].r_offset)
10253 {
10254 insert_at = i;
10255 break;
10256 }
10257 }
10258
10259 if (*internal_relocs_p != relax_info->allocated_relocs
10260 || sec->reloc_count + 1 > relax_info->allocated_relocs_count)
10261 {
10262 BFD_ASSERT (relax_info->allocated_relocs == NULL
10263 || sec->reloc_count == relax_info->relocs_count);
10264
10265 if (relax_info->allocated_relocs_count == 0)
10266 new_relocs_count = (sec->reloc_count + 2) * 2;
10267 else
10268 new_relocs_count = (relax_info->allocated_relocs_count + 2) * 2;
10269
10270 new_relocs = (Elf_Internal_Rela *)
10271 bfd_zmalloc (sizeof (Elf_Internal_Rela) * (new_relocs_count));
10272 if (!new_relocs)
10273 return FALSE;
10274
10275 /* We could handle this more quickly by finding the split point. */
10276 if (insert_at != 0)
10277 memcpy (new_relocs, *internal_relocs_p,
10278 insert_at * sizeof (Elf_Internal_Rela));
10279
10280 new_relocs[insert_at] = this_rela;
10281
10282 if (insert_at != sec->reloc_count)
10283 memcpy (new_relocs + insert_at + 1,
10284 (*internal_relocs_p) + insert_at,
10285 (sec->reloc_count - insert_at)
10286 * sizeof (Elf_Internal_Rela));
10287
10288 if (*internal_relocs_p != relax_info->allocated_relocs)
10289 {
10290 /* The first time we re-allocate, we can only free the
10291 old relocs if they were allocated with bfd_malloc.
10292 This is not true when keep_memory is in effect. */
10293 if (!link_info->keep_memory)
10294 free (*internal_relocs_p);
10295 }
10296 else
10297 free (*internal_relocs_p);
10298 relax_info->allocated_relocs = new_relocs;
10299 relax_info->allocated_relocs_count = new_relocs_count;
10300 elf_section_data (sec)->relocs = new_relocs;
10301 sec->reloc_count++;
10302 relax_info->relocs_count = sec->reloc_count;
10303 *internal_relocs_p = new_relocs;
10304 }
10305 else
10306 {
10307 if (insert_at != sec->reloc_count)
10308 {
10309 unsigned idx;
10310 for (idx = sec->reloc_count; idx > insert_at; idx--)
10311 (*internal_relocs_p)[idx] = (*internal_relocs_p)[idx-1];
10312 }
10313 (*internal_relocs_p)[insert_at] = this_rela;
10314 sec->reloc_count++;
10315 if (relax_info->allocated_relocs)
10316 relax_info->relocs_count = sec->reloc_count;
10317 }
10318 }
10319 return TRUE;
10320 }
10321
10322
10323 /* This is similar to relax_section except that when a target is moved,
10324 we shift addresses up. We also need to modify the size. This
10325 algorithm does NOT allow for relocations into the middle of the
10326 property sections. */
10327
10328 static bfd_boolean
10329 relax_property_section (bfd *abfd,
10330 asection *sec,
10331 struct bfd_link_info *link_info)
10332 {
10333 Elf_Internal_Rela *internal_relocs;
10334 bfd_byte *contents;
10335 unsigned i;
10336 bfd_boolean ok = TRUE;
10337 bfd_boolean is_full_prop_section;
10338 size_t last_zfill_target_offset = 0;
10339 asection *last_zfill_target_sec = NULL;
10340 bfd_size_type sec_size;
10341 bfd_size_type entry_size;
10342
10343 sec_size = bfd_get_section_limit (abfd, sec);
10344 internal_relocs = retrieve_internal_relocs (abfd, sec,
10345 link_info->keep_memory);
10346 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
10347 if (contents == NULL && sec_size != 0)
10348 {
10349 ok = FALSE;
10350 goto error_return;
10351 }
10352
10353 is_full_prop_section = xtensa_is_proptable_section (sec);
10354 if (is_full_prop_section)
10355 entry_size = 12;
10356 else
10357 entry_size = 8;
10358
10359 if (internal_relocs)
10360 {
10361 for (i = 0; i < sec->reloc_count; i++)
10362 {
10363 Elf_Internal_Rela *irel;
10364 xtensa_relax_info *target_relax_info;
10365 unsigned r_type;
10366 asection *target_sec;
10367 literal_value val;
10368 bfd_byte *size_p, *flags_p;
10369
10370 /* Locally change the source address.
10371 Translate the target to the new target address.
10372 If it points to this section and has been removed, MOVE IT.
10373 Also, don't forget to modify the associated SIZE at
10374 (offset + 4). */
10375
10376 irel = &internal_relocs[i];
10377 r_type = ELF32_R_TYPE (irel->r_info);
10378 if (r_type == R_XTENSA_NONE)
10379 continue;
10380
10381 /* Find the literal value. */
10382 r_reloc_init (&val.r_rel, abfd, irel, contents, sec_size);
10383 size_p = &contents[irel->r_offset + 4];
10384 flags_p = NULL;
10385 if (is_full_prop_section)
10386 flags_p = &contents[irel->r_offset + 8];
10387 BFD_ASSERT (irel->r_offset + entry_size <= sec_size);
10388
10389 target_sec = r_reloc_get_section (&val.r_rel);
10390 target_relax_info = get_xtensa_relax_info (target_sec);
10391
10392 if (target_relax_info
10393 && (target_relax_info->is_relaxable_literal_section
10394 || target_relax_info->is_relaxable_asm_section ))
10395 {
10396 /* Translate the relocation's destination. */
10397 bfd_vma old_offset = val.r_rel.target_offset;
10398 bfd_vma new_offset;
10399 long old_size, new_size;
10400 int removed_by_old_offset =
10401 removed_by_actions_map (&target_relax_info->action_list,
10402 old_offset, FALSE);
10403 new_offset = old_offset - removed_by_old_offset;
10404
10405 /* Assert that we are not out of bounds. */
10406 old_size = bfd_get_32 (abfd, size_p);
10407 new_size = old_size;
10408
10409 if (old_size == 0)
10410 {
10411 /* Only the first zero-sized unreachable entry is
10412 allowed to expand. In this case the new offset
10413 should be the offset before the fill and the new
10414 size is the expansion size. For other zero-sized
10415 entries the resulting size should be zero with an
10416 offset before or after the fill address depending
10417 on whether the expanding unreachable entry
10418 preceeds it. */
10419 if (last_zfill_target_sec == 0
10420 || last_zfill_target_sec != target_sec
10421 || last_zfill_target_offset != old_offset)
10422 {
10423 bfd_vma new_end_offset = new_offset;
10424
10425 /* Recompute the new_offset, but this time don't
10426 include any fill inserted by relaxation. */
10427 removed_by_old_offset =
10428 removed_by_actions_map (&target_relax_info->action_list,
10429 old_offset, TRUE);
10430 new_offset = old_offset - removed_by_old_offset;
10431
10432 /* If it is not unreachable and we have not yet
10433 seen an unreachable at this address, place it
10434 before the fill address. */
10435 if (flags_p && (bfd_get_32 (abfd, flags_p)
10436 & XTENSA_PROP_UNREACHABLE) != 0)
10437 {
10438 new_size = new_end_offset - new_offset;
10439
10440 last_zfill_target_sec = target_sec;
10441 last_zfill_target_offset = old_offset;
10442 }
10443 }
10444 }
10445 else
10446 {
10447 int removed_by_old_offset_size =
10448 removed_by_actions_map (&target_relax_info->action_list,
10449 old_offset + old_size, TRUE);
10450 new_size -= removed_by_old_offset_size - removed_by_old_offset;
10451 }
10452
10453 if (new_size != old_size)
10454 {
10455 bfd_put_32 (abfd, new_size, size_p);
10456 pin_contents (sec, contents);
10457 }
10458
10459 if (new_offset != old_offset)
10460 {
10461 bfd_vma diff = new_offset - old_offset;
10462 irel->r_addend += diff;
10463 pin_internal_relocs (sec, internal_relocs);
10464 }
10465 }
10466 }
10467 }
10468
10469 /* Combine adjacent property table entries. This is also done in
10470 finish_dynamic_sections() but at that point it's too late to
10471 reclaim the space in the output section, so we do this twice. */
10472
10473 if (internal_relocs && (!bfd_link_relocatable (link_info)
10474 || xtensa_is_littable_section (sec)))
10475 {
10476 Elf_Internal_Rela *last_irel = NULL;
10477 Elf_Internal_Rela *irel, *next_rel, *rel_end;
10478 int removed_bytes = 0;
10479 bfd_vma offset;
10480 flagword predef_flags;
10481
10482 predef_flags = xtensa_get_property_predef_flags (sec);
10483
10484 /* Walk over memory and relocations at the same time.
10485 This REQUIRES that the internal_relocs be sorted by offset. */
10486 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
10487 internal_reloc_compare);
10488
10489 pin_internal_relocs (sec, internal_relocs);
10490 pin_contents (sec, contents);
10491
10492 next_rel = internal_relocs;
10493 rel_end = internal_relocs + sec->reloc_count;
10494
10495 BFD_ASSERT (sec->size % entry_size == 0);
10496
10497 for (offset = 0; offset < sec->size; offset += entry_size)
10498 {
10499 Elf_Internal_Rela *offset_rel, *extra_rel;
10500 bfd_vma bytes_to_remove, size, actual_offset;
10501 bfd_boolean remove_this_rel;
10502 flagword flags;
10503
10504 /* Find the first relocation for the entry at the current offset.
10505 Adjust the offsets of any extra relocations for the previous
10506 entry. */
10507 offset_rel = NULL;
10508 if (next_rel)
10509 {
10510 for (irel = next_rel; irel < rel_end; irel++)
10511 {
10512 if ((irel->r_offset == offset
10513 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
10514 || irel->r_offset > offset)
10515 {
10516 offset_rel = irel;
10517 break;
10518 }
10519 irel->r_offset -= removed_bytes;
10520 }
10521 }
10522
10523 /* Find the next relocation (if there are any left). */
10524 extra_rel = NULL;
10525 if (offset_rel)
10526 {
10527 for (irel = offset_rel + 1; irel < rel_end; irel++)
10528 {
10529 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
10530 {
10531 extra_rel = irel;
10532 break;
10533 }
10534 }
10535 }
10536
10537 /* Check if there are relocations on the current entry. There
10538 should usually be a relocation on the offset field. If there
10539 are relocations on the size or flags, then we can't optimize
10540 this entry. Also, find the next relocation to examine on the
10541 next iteration. */
10542 if (offset_rel)
10543 {
10544 if (offset_rel->r_offset >= offset + entry_size)
10545 {
10546 next_rel = offset_rel;
10547 /* There are no relocations on the current entry, but we
10548 might still be able to remove it if the size is zero. */
10549 offset_rel = NULL;
10550 }
10551 else if (offset_rel->r_offset > offset
10552 || (extra_rel
10553 && extra_rel->r_offset < offset + entry_size))
10554 {
10555 /* There is a relocation on the size or flags, so we can't
10556 do anything with this entry. Continue with the next. */
10557 next_rel = offset_rel;
10558 continue;
10559 }
10560 else
10561 {
10562 BFD_ASSERT (offset_rel->r_offset == offset);
10563 offset_rel->r_offset -= removed_bytes;
10564 next_rel = offset_rel + 1;
10565 }
10566 }
10567 else
10568 next_rel = NULL;
10569
10570 remove_this_rel = FALSE;
10571 bytes_to_remove = 0;
10572 actual_offset = offset - removed_bytes;
10573 size = bfd_get_32 (abfd, &contents[actual_offset + 4]);
10574
10575 if (is_full_prop_section)
10576 flags = bfd_get_32 (abfd, &contents[actual_offset + 8]);
10577 else
10578 flags = predef_flags;
10579
10580 if (size == 0
10581 && (flags & XTENSA_PROP_ALIGN) == 0
10582 && (flags & XTENSA_PROP_UNREACHABLE) == 0)
10583 {
10584 /* Always remove entries with zero size and no alignment. */
10585 bytes_to_remove = entry_size;
10586 if (offset_rel)
10587 remove_this_rel = TRUE;
10588 }
10589 else if (offset_rel
10590 && ELF32_R_TYPE (offset_rel->r_info) == R_XTENSA_32)
10591 {
10592 if (last_irel)
10593 {
10594 flagword old_flags;
10595 bfd_vma old_size =
10596 bfd_get_32 (abfd, &contents[last_irel->r_offset + 4]);
10597 bfd_vma old_address =
10598 (last_irel->r_addend
10599 + bfd_get_32 (abfd, &contents[last_irel->r_offset]));
10600 bfd_vma new_address =
10601 (offset_rel->r_addend
10602 + bfd_get_32 (abfd, &contents[actual_offset]));
10603 if (is_full_prop_section)
10604 old_flags = bfd_get_32
10605 (abfd, &contents[last_irel->r_offset + 8]);
10606 else
10607 old_flags = predef_flags;
10608
10609 if ((ELF32_R_SYM (offset_rel->r_info)
10610 == ELF32_R_SYM (last_irel->r_info))
10611 && old_address + old_size == new_address
10612 && old_flags == flags
10613 && (old_flags & XTENSA_PROP_INSN_BRANCH_TARGET) == 0
10614 && (old_flags & XTENSA_PROP_INSN_LOOP_TARGET) == 0)
10615 {
10616 /* Fix the old size. */
10617 bfd_put_32 (abfd, old_size + size,
10618 &contents[last_irel->r_offset + 4]);
10619 bytes_to_remove = entry_size;
10620 remove_this_rel = TRUE;
10621 }
10622 else
10623 last_irel = offset_rel;
10624 }
10625 else
10626 last_irel = offset_rel;
10627 }
10628
10629 if (remove_this_rel)
10630 {
10631 offset_rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
10632 offset_rel->r_offset = 0;
10633 }
10634
10635 if (bytes_to_remove != 0)
10636 {
10637 removed_bytes += bytes_to_remove;
10638 if (offset + bytes_to_remove < sec->size)
10639 memmove (&contents[actual_offset],
10640 &contents[actual_offset + bytes_to_remove],
10641 sec->size - offset - bytes_to_remove);
10642 }
10643 }
10644
10645 if (removed_bytes)
10646 {
10647 /* Fix up any extra relocations on the last entry. */
10648 for (irel = next_rel; irel < rel_end; irel++)
10649 irel->r_offset -= removed_bytes;
10650
10651 /* Clear the removed bytes. */
10652 memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
10653
10654 if (sec->rawsize == 0)
10655 sec->rawsize = sec->size;
10656 sec->size -= removed_bytes;
10657
10658 if (xtensa_is_littable_section (sec))
10659 {
10660 asection *sgotloc = elf_xtensa_hash_table (link_info)->sgotloc;
10661 if (sgotloc)
10662 sgotloc->size -= removed_bytes;
10663 }
10664 }
10665 }
10666
10667 error_return:
10668 release_internal_relocs (sec, internal_relocs);
10669 release_contents (sec, contents);
10670 return ok;
10671 }
10672
10673 \f
10674 /* Third relaxation pass. */
10675
10676 /* Change symbol values to account for removed literals. */
10677
10678 bfd_boolean
10679 relax_section_symbols (bfd *abfd, asection *sec)
10680 {
10681 xtensa_relax_info *relax_info;
10682 unsigned int sec_shndx;
10683 Elf_Internal_Shdr *symtab_hdr;
10684 Elf_Internal_Sym *isymbuf;
10685 unsigned i, num_syms, num_locals;
10686
10687 relax_info = get_xtensa_relax_info (sec);
10688 BFD_ASSERT (relax_info);
10689
10690 if (!relax_info->is_relaxable_literal_section
10691 && !relax_info->is_relaxable_asm_section)
10692 return TRUE;
10693
10694 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
10695
10696 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10697 isymbuf = retrieve_local_syms (abfd);
10698
10699 num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
10700 num_locals = symtab_hdr->sh_info;
10701
10702 /* Adjust the local symbols defined in this section. */
10703 for (i = 0; i < num_locals; i++)
10704 {
10705 Elf_Internal_Sym *isym = &isymbuf[i];
10706
10707 if (isym->st_shndx == sec_shndx)
10708 {
10709 bfd_vma orig_addr = isym->st_value;
10710 int removed = removed_by_actions_map (&relax_info->action_list,
10711 orig_addr, FALSE);
10712
10713 isym->st_value -= removed;
10714 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC)
10715 isym->st_size -=
10716 removed_by_actions_map (&relax_info->action_list,
10717 orig_addr + isym->st_size, FALSE) -
10718 removed;
10719 }
10720 }
10721
10722 /* Now adjust the global symbols defined in this section. */
10723 for (i = 0; i < (num_syms - num_locals); i++)
10724 {
10725 struct elf_link_hash_entry *sym_hash;
10726
10727 sym_hash = elf_sym_hashes (abfd)[i];
10728
10729 if (sym_hash->root.type == bfd_link_hash_warning)
10730 sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
10731
10732 if ((sym_hash->root.type == bfd_link_hash_defined
10733 || sym_hash->root.type == bfd_link_hash_defweak)
10734 && sym_hash->root.u.def.section == sec)
10735 {
10736 bfd_vma orig_addr = sym_hash->root.u.def.value;
10737 int removed = removed_by_actions_map (&relax_info->action_list,
10738 orig_addr, FALSE);
10739
10740 sym_hash->root.u.def.value -= removed;
10741
10742 if (sym_hash->type == STT_FUNC)
10743 sym_hash->size -=
10744 removed_by_actions_map (&relax_info->action_list,
10745 orig_addr + sym_hash->size, FALSE) -
10746 removed;
10747 }
10748 }
10749
10750 return TRUE;
10751 }
10752
10753 \f
10754 /* "Fix" handling functions, called while performing relocations. */
10755
10756 static bfd_boolean
10757 do_fix_for_relocatable_link (Elf_Internal_Rela *rel,
10758 bfd *input_bfd,
10759 asection *input_section,
10760 bfd_byte *contents)
10761 {
10762 r_reloc r_rel;
10763 asection *sec, *old_sec;
10764 bfd_vma old_offset;
10765 int r_type = ELF32_R_TYPE (rel->r_info);
10766 reloc_bfd_fix *fix;
10767
10768 if (r_type == R_XTENSA_NONE)
10769 return TRUE;
10770
10771 fix = get_bfd_fix (input_section, rel->r_offset, r_type);
10772 if (!fix)
10773 return TRUE;
10774
10775 r_reloc_init (&r_rel, input_bfd, rel, contents,
10776 bfd_get_section_limit (input_bfd, input_section));
10777 old_sec = r_reloc_get_section (&r_rel);
10778 old_offset = r_rel.target_offset;
10779
10780 if (!old_sec || !r_reloc_is_defined (&r_rel))
10781 {
10782 if (r_type != R_XTENSA_ASM_EXPAND)
10783 {
10784 _bfd_error_handler
10785 /* xgettext:c-format */
10786 (_("%B(%A+0x%lx): unexpected fix for %s relocation"),
10787 input_bfd, input_section, rel->r_offset,
10788 elf_howto_table[r_type].name);
10789 return FALSE;
10790 }
10791 /* Leave it be. Resolution will happen in a later stage. */
10792 }
10793 else
10794 {
10795 sec = fix->target_sec;
10796 rel->r_addend += ((sec->output_offset + fix->target_offset)
10797 - (old_sec->output_offset + old_offset));
10798 }
10799 return TRUE;
10800 }
10801
10802
10803 static void
10804 do_fix_for_final_link (Elf_Internal_Rela *rel,
10805 bfd *input_bfd,
10806 asection *input_section,
10807 bfd_byte *contents,
10808 bfd_vma *relocationp)
10809 {
10810 asection *sec;
10811 int r_type = ELF32_R_TYPE (rel->r_info);
10812 reloc_bfd_fix *fix;
10813 bfd_vma fixup_diff;
10814
10815 if (r_type == R_XTENSA_NONE)
10816 return;
10817
10818 fix = get_bfd_fix (input_section, rel->r_offset, r_type);
10819 if (!fix)
10820 return;
10821
10822 sec = fix->target_sec;
10823
10824 fixup_diff = rel->r_addend;
10825 if (elf_howto_table[fix->src_type].partial_inplace)
10826 {
10827 bfd_vma inplace_val;
10828 BFD_ASSERT (fix->src_offset
10829 < bfd_get_section_limit (input_bfd, input_section));
10830 inplace_val = bfd_get_32 (input_bfd, &contents[fix->src_offset]);
10831 fixup_diff += inplace_val;
10832 }
10833
10834 *relocationp = (sec->output_section->vma
10835 + sec->output_offset
10836 + fix->target_offset - fixup_diff);
10837 }
10838
10839 \f
10840 /* Miscellaneous utility functions.... */
10841
10842 static asection *
10843 elf_xtensa_get_plt_section (struct bfd_link_info *info, int chunk)
10844 {
10845 struct elf_xtensa_link_hash_table *htab;
10846 bfd *dynobj;
10847 char plt_name[10];
10848
10849 if (chunk == 0)
10850 {
10851 htab = elf_xtensa_hash_table (info);
10852 if (htab == NULL)
10853 return NULL;
10854
10855 return htab->splt;
10856 }
10857
10858 dynobj = elf_hash_table (info)->dynobj;
10859 sprintf (plt_name, ".plt.%u", chunk);
10860 return bfd_get_linker_section (dynobj, plt_name);
10861 }
10862
10863
10864 static asection *
10865 elf_xtensa_get_gotplt_section (struct bfd_link_info *info, int chunk)
10866 {
10867 struct elf_xtensa_link_hash_table *htab;
10868 bfd *dynobj;
10869 char got_name[14];
10870
10871 if (chunk == 0)
10872 {
10873 htab = elf_xtensa_hash_table (info);
10874 if (htab == NULL)
10875 return NULL;
10876 return htab->sgotplt;
10877 }
10878
10879 dynobj = elf_hash_table (info)->dynobj;
10880 sprintf (got_name, ".got.plt.%u", chunk);
10881 return bfd_get_linker_section (dynobj, got_name);
10882 }
10883
10884
10885 /* Get the input section for a given symbol index.
10886 If the symbol is:
10887 . a section symbol, return the section;
10888 . a common symbol, return the common section;
10889 . an undefined symbol, return the undefined section;
10890 . an indirect symbol, follow the links;
10891 . an absolute value, return the absolute section. */
10892
10893 static asection *
10894 get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
10895 {
10896 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10897 asection *target_sec = NULL;
10898 if (r_symndx < symtab_hdr->sh_info)
10899 {
10900 Elf_Internal_Sym *isymbuf;
10901 unsigned int section_index;
10902
10903 isymbuf = retrieve_local_syms (abfd);
10904 section_index = isymbuf[r_symndx].st_shndx;
10905
10906 if (section_index == SHN_UNDEF)
10907 target_sec = bfd_und_section_ptr;
10908 else if (section_index == SHN_ABS)
10909 target_sec = bfd_abs_section_ptr;
10910 else if (section_index == SHN_COMMON)
10911 target_sec = bfd_com_section_ptr;
10912 else
10913 target_sec = bfd_section_from_elf_index (abfd, section_index);
10914 }
10915 else
10916 {
10917 unsigned long indx = r_symndx - symtab_hdr->sh_info;
10918 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
10919
10920 while (h->root.type == bfd_link_hash_indirect
10921 || h->root.type == bfd_link_hash_warning)
10922 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10923
10924 switch (h->root.type)
10925 {
10926 case bfd_link_hash_defined:
10927 case bfd_link_hash_defweak:
10928 target_sec = h->root.u.def.section;
10929 break;
10930 case bfd_link_hash_common:
10931 target_sec = bfd_com_section_ptr;
10932 break;
10933 case bfd_link_hash_undefined:
10934 case bfd_link_hash_undefweak:
10935 target_sec = bfd_und_section_ptr;
10936 break;
10937 default: /* New indirect warning. */
10938 target_sec = bfd_und_section_ptr;
10939 break;
10940 }
10941 }
10942 return target_sec;
10943 }
10944
10945
10946 static struct elf_link_hash_entry *
10947 get_elf_r_symndx_hash_entry (bfd *abfd, unsigned long r_symndx)
10948 {
10949 unsigned long indx;
10950 struct elf_link_hash_entry *h;
10951 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10952
10953 if (r_symndx < symtab_hdr->sh_info)
10954 return NULL;
10955
10956 indx = r_symndx - symtab_hdr->sh_info;
10957 h = elf_sym_hashes (abfd)[indx];
10958 while (h->root.type == bfd_link_hash_indirect
10959 || h->root.type == bfd_link_hash_warning)
10960 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10961 return h;
10962 }
10963
10964
10965 /* Get the section-relative offset for a symbol number. */
10966
10967 static bfd_vma
10968 get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
10969 {
10970 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10971 bfd_vma offset = 0;
10972
10973 if (r_symndx < symtab_hdr->sh_info)
10974 {
10975 Elf_Internal_Sym *isymbuf;
10976 isymbuf = retrieve_local_syms (abfd);
10977 offset = isymbuf[r_symndx].st_value;
10978 }
10979 else
10980 {
10981 unsigned long indx = r_symndx - symtab_hdr->sh_info;
10982 struct elf_link_hash_entry *h =
10983 elf_sym_hashes (abfd)[indx];
10984
10985 while (h->root.type == bfd_link_hash_indirect
10986 || h->root.type == bfd_link_hash_warning)
10987 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10988 if (h->root.type == bfd_link_hash_defined
10989 || h->root.type == bfd_link_hash_defweak)
10990 offset = h->root.u.def.value;
10991 }
10992 return offset;
10993 }
10994
10995
10996 static bfd_boolean
10997 is_reloc_sym_weak (bfd *abfd, Elf_Internal_Rela *rel)
10998 {
10999 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
11000 struct elf_link_hash_entry *h;
11001
11002 h = get_elf_r_symndx_hash_entry (abfd, r_symndx);
11003 if (h && h->root.type == bfd_link_hash_defweak)
11004 return TRUE;
11005 return FALSE;
11006 }
11007
11008
11009 static bfd_boolean
11010 pcrel_reloc_fits (xtensa_opcode opc,
11011 int opnd,
11012 bfd_vma self_address,
11013 bfd_vma dest_address)
11014 {
11015 xtensa_isa isa = xtensa_default_isa;
11016 uint32 valp = dest_address;
11017 if (xtensa_operand_do_reloc (isa, opc, opnd, &valp, self_address)
11018 || xtensa_operand_encode (isa, opc, opnd, &valp))
11019 return FALSE;
11020 return TRUE;
11021 }
11022
11023
11024 static bfd_boolean
11025 xtensa_is_property_section (asection *sec)
11026 {
11027 if (xtensa_is_insntable_section (sec)
11028 || xtensa_is_littable_section (sec)
11029 || xtensa_is_proptable_section (sec))
11030 return TRUE;
11031
11032 return FALSE;
11033 }
11034
11035
11036 static bfd_boolean
11037 xtensa_is_insntable_section (asection *sec)
11038 {
11039 if (CONST_STRNEQ (sec->name, XTENSA_INSN_SEC_NAME)
11040 || CONST_STRNEQ (sec->name, ".gnu.linkonce.x."))
11041 return TRUE;
11042
11043 return FALSE;
11044 }
11045
11046
11047 static bfd_boolean
11048 xtensa_is_littable_section (asection *sec)
11049 {
11050 if (CONST_STRNEQ (sec->name, XTENSA_LIT_SEC_NAME)
11051 || CONST_STRNEQ (sec->name, ".gnu.linkonce.p."))
11052 return TRUE;
11053
11054 return FALSE;
11055 }
11056
11057
11058 static bfd_boolean
11059 xtensa_is_proptable_section (asection *sec)
11060 {
11061 if (CONST_STRNEQ (sec->name, XTENSA_PROP_SEC_NAME)
11062 || CONST_STRNEQ (sec->name, ".gnu.linkonce.prop."))
11063 return TRUE;
11064
11065 return FALSE;
11066 }
11067
11068
11069 static int
11070 internal_reloc_compare (const void *ap, const void *bp)
11071 {
11072 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
11073 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
11074
11075 if (a->r_offset != b->r_offset)
11076 return (a->r_offset - b->r_offset);
11077
11078 /* We don't need to sort on these criteria for correctness,
11079 but enforcing a more strict ordering prevents unstable qsort
11080 from behaving differently with different implementations.
11081 Without the code below we get correct but different results
11082 on Solaris 2.7 and 2.8. We would like to always produce the
11083 same results no matter the host. */
11084
11085 if (a->r_info != b->r_info)
11086 return (a->r_info - b->r_info);
11087
11088 return (a->r_addend - b->r_addend);
11089 }
11090
11091
11092 static int
11093 internal_reloc_matches (const void *ap, const void *bp)
11094 {
11095 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
11096 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
11097
11098 /* Check if one entry overlaps with the other; this shouldn't happen
11099 except when searching for a match. */
11100 return (a->r_offset - b->r_offset);
11101 }
11102
11103
11104 /* Predicate function used to look up a section in a particular group. */
11105
11106 static bfd_boolean
11107 match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
11108 {
11109 const char *gname = inf;
11110 const char *group_name = elf_group_name (sec);
11111
11112 return (group_name == gname
11113 || (group_name != NULL
11114 && gname != NULL
11115 && strcmp (group_name, gname) == 0));
11116 }
11117
11118
11119 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
11120
11121 static char *
11122 xtensa_property_section_name (asection *sec, const char *base_name)
11123 {
11124 const char *suffix, *group_name;
11125 char *prop_sec_name;
11126
11127 group_name = elf_group_name (sec);
11128 if (group_name)
11129 {
11130 suffix = strrchr (sec->name, '.');
11131 if (suffix == sec->name)
11132 suffix = 0;
11133 prop_sec_name = (char *) bfd_malloc (strlen (base_name) + 1
11134 + (suffix ? strlen (suffix) : 0));
11135 strcpy (prop_sec_name, base_name);
11136 if (suffix)
11137 strcat (prop_sec_name, suffix);
11138 }
11139 else if (strncmp (sec->name, ".gnu.linkonce.", linkonce_len) == 0)
11140 {
11141 char *linkonce_kind = 0;
11142
11143 if (strcmp (base_name, XTENSA_INSN_SEC_NAME) == 0)
11144 linkonce_kind = "x.";
11145 else if (strcmp (base_name, XTENSA_LIT_SEC_NAME) == 0)
11146 linkonce_kind = "p.";
11147 else if (strcmp (base_name, XTENSA_PROP_SEC_NAME) == 0)
11148 linkonce_kind = "prop.";
11149 else
11150 abort ();
11151
11152 prop_sec_name = (char *) bfd_malloc (strlen (sec->name)
11153 + strlen (linkonce_kind) + 1);
11154 memcpy (prop_sec_name, ".gnu.linkonce.", linkonce_len);
11155 strcpy (prop_sec_name + linkonce_len, linkonce_kind);
11156
11157 suffix = sec->name + linkonce_len;
11158 /* For backward compatibility, replace "t." instead of inserting
11159 the new linkonce_kind (but not for "prop" sections). */
11160 if (CONST_STRNEQ (suffix, "t.") && linkonce_kind[1] == '.')
11161 suffix += 2;
11162 strcat (prop_sec_name + linkonce_len, suffix);
11163 }
11164 else
11165 prop_sec_name = strdup (base_name);
11166
11167 return prop_sec_name;
11168 }
11169
11170
11171 static asection *
11172 xtensa_get_property_section (asection *sec, const char *base_name)
11173 {
11174 char *prop_sec_name;
11175 asection *prop_sec;
11176
11177 prop_sec_name = xtensa_property_section_name (sec, base_name);
11178 prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
11179 match_section_group,
11180 (void *) elf_group_name (sec));
11181 free (prop_sec_name);
11182 return prop_sec;
11183 }
11184
11185
11186 asection *
11187 xtensa_make_property_section (asection *sec, const char *base_name)
11188 {
11189 char *prop_sec_name;
11190 asection *prop_sec;
11191
11192 /* Check if the section already exists. */
11193 prop_sec_name = xtensa_property_section_name (sec, base_name);
11194 prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
11195 match_section_group,
11196 (void *) elf_group_name (sec));
11197 /* If not, create it. */
11198 if (! prop_sec)
11199 {
11200 flagword flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY);
11201 flags |= (bfd_get_section_flags (sec->owner, sec)
11202 & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES));
11203
11204 prop_sec = bfd_make_section_anyway_with_flags
11205 (sec->owner, strdup (prop_sec_name), flags);
11206 if (! prop_sec)
11207 return 0;
11208
11209 elf_group_name (prop_sec) = elf_group_name (sec);
11210 }
11211
11212 free (prop_sec_name);
11213 return prop_sec;
11214 }
11215
11216
11217 flagword
11218 xtensa_get_property_predef_flags (asection *sec)
11219 {
11220 if (xtensa_is_insntable_section (sec))
11221 return (XTENSA_PROP_INSN
11222 | XTENSA_PROP_NO_TRANSFORM
11223 | XTENSA_PROP_INSN_NO_REORDER);
11224
11225 if (xtensa_is_littable_section (sec))
11226 return (XTENSA_PROP_LITERAL
11227 | XTENSA_PROP_NO_TRANSFORM
11228 | XTENSA_PROP_INSN_NO_REORDER);
11229
11230 return 0;
11231 }
11232
11233 \f
11234 /* Other functions called directly by the linker. */
11235
11236 bfd_boolean
11237 xtensa_callback_required_dependence (bfd *abfd,
11238 asection *sec,
11239 struct bfd_link_info *link_info,
11240 deps_callback_t callback,
11241 void *closure)
11242 {
11243 Elf_Internal_Rela *internal_relocs;
11244 bfd_byte *contents;
11245 unsigned i;
11246 bfd_boolean ok = TRUE;
11247 bfd_size_type sec_size;
11248
11249 sec_size = bfd_get_section_limit (abfd, sec);
11250
11251 /* ".plt*" sections have no explicit relocations but they contain L32R
11252 instructions that reference the corresponding ".got.plt*" sections. */
11253 if ((sec->flags & SEC_LINKER_CREATED) != 0
11254 && CONST_STRNEQ (sec->name, ".plt"))
11255 {
11256 asection *sgotplt;
11257
11258 /* Find the corresponding ".got.plt*" section. */
11259 if (sec->name[4] == '\0')
11260 sgotplt = bfd_get_linker_section (sec->owner, ".got.plt");
11261 else
11262 {
11263 char got_name[14];
11264 int chunk = 0;
11265
11266 BFD_ASSERT (sec->name[4] == '.');
11267 chunk = strtol (&sec->name[5], NULL, 10);
11268
11269 sprintf (got_name, ".got.plt.%u", chunk);
11270 sgotplt = bfd_get_linker_section (sec->owner, got_name);
11271 }
11272 BFD_ASSERT (sgotplt);
11273
11274 /* Assume worst-case offsets: L32R at the very end of the ".plt"
11275 section referencing a literal at the very beginning of
11276 ".got.plt". This is very close to the real dependence, anyway. */
11277 (*callback) (sec, sec_size, sgotplt, 0, closure);
11278 }
11279
11280 /* Only ELF files are supported for Xtensa. Check here to avoid a segfault
11281 when building uclibc, which runs "ld -b binary /dev/null". */
11282 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11283 return ok;
11284
11285 internal_relocs = retrieve_internal_relocs (abfd, sec,
11286 link_info->keep_memory);
11287 if (internal_relocs == NULL
11288 || sec->reloc_count == 0)
11289 return ok;
11290
11291 /* Cache the contents for the duration of this scan. */
11292 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
11293 if (contents == NULL && sec_size != 0)
11294 {
11295 ok = FALSE;
11296 goto error_return;
11297 }
11298
11299 if (!xtensa_default_isa)
11300 xtensa_default_isa = xtensa_isa_init (0, 0);
11301
11302 for (i = 0; i < sec->reloc_count; i++)
11303 {
11304 Elf_Internal_Rela *irel = &internal_relocs[i];
11305 if (is_l32r_relocation (abfd, sec, contents, irel))
11306 {
11307 r_reloc l32r_rel;
11308 asection *target_sec;
11309 bfd_vma target_offset;
11310
11311 r_reloc_init (&l32r_rel, abfd, irel, contents, sec_size);
11312 target_sec = NULL;
11313 target_offset = 0;
11314 /* L32Rs must be local to the input file. */
11315 if (r_reloc_is_defined (&l32r_rel))
11316 {
11317 target_sec = r_reloc_get_section (&l32r_rel);
11318 target_offset = l32r_rel.target_offset;
11319 }
11320 (*callback) (sec, irel->r_offset, target_sec, target_offset,
11321 closure);
11322 }
11323 }
11324
11325 error_return:
11326 release_internal_relocs (sec, internal_relocs);
11327 release_contents (sec, contents);
11328 return ok;
11329 }
11330
11331 /* The default literal sections should always be marked as "code" (i.e.,
11332 SHF_EXECINSTR). This is particularly important for the Linux kernel
11333 module loader so that the literals are not placed after the text. */
11334 static const struct bfd_elf_special_section elf_xtensa_special_sections[] =
11335 {
11336 { STRING_COMMA_LEN (".fini.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11337 { STRING_COMMA_LEN (".init.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11338 { STRING_COMMA_LEN (".literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11339 { STRING_COMMA_LEN (".xtensa.info"), 0, SHT_NOTE, 0 },
11340 { NULL, 0, 0, 0, 0 }
11341 };
11342 \f
11343 #define ELF_TARGET_ID XTENSA_ELF_DATA
11344 #ifndef ELF_ARCH
11345 #define TARGET_LITTLE_SYM xtensa_elf32_le_vec
11346 #define TARGET_LITTLE_NAME "elf32-xtensa-le"
11347 #define TARGET_BIG_SYM xtensa_elf32_be_vec
11348 #define TARGET_BIG_NAME "elf32-xtensa-be"
11349 #define ELF_ARCH bfd_arch_xtensa
11350
11351 #define ELF_MACHINE_CODE EM_XTENSA
11352 #define ELF_MACHINE_ALT1 EM_XTENSA_OLD
11353
11354 #if XCHAL_HAVE_MMU
11355 #define ELF_MAXPAGESIZE (1 << XCHAL_MMU_MIN_PTE_PAGE_SIZE)
11356 #else /* !XCHAL_HAVE_MMU */
11357 #define ELF_MAXPAGESIZE 1
11358 #endif /* !XCHAL_HAVE_MMU */
11359 #endif /* ELF_ARCH */
11360
11361 #define elf_backend_can_gc_sections 1
11362 #define elf_backend_can_refcount 1
11363 #define elf_backend_plt_readonly 1
11364 #define elf_backend_got_header_size 4
11365 #define elf_backend_want_dynbss 0
11366 #define elf_backend_want_got_plt 1
11367
11368 #define elf_info_to_howto elf_xtensa_info_to_howto_rela
11369
11370 #define bfd_elf32_mkobject elf_xtensa_mkobject
11371
11372 #define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
11373 #define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
11374 #define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
11375 #define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
11376 #define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
11377 #define bfd_elf32_bfd_reloc_name_lookup \
11378 elf_xtensa_reloc_name_lookup
11379 #define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
11380 #define bfd_elf32_bfd_link_hash_table_create elf_xtensa_link_hash_table_create
11381
11382 #define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
11383 #define elf_backend_check_relocs elf_xtensa_check_relocs
11384 #define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
11385 #define elf_backend_discard_info elf_xtensa_discard_info
11386 #define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
11387 #define elf_backend_final_write_processing elf_xtensa_final_write_processing
11388 #define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
11389 #define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
11390 #define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
11391 #define elf_backend_gc_sweep_hook elf_xtensa_gc_sweep_hook
11392 #define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
11393 #define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
11394 #define elf_backend_hide_symbol elf_xtensa_hide_symbol
11395 #define elf_backend_object_p elf_xtensa_object_p
11396 #define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
11397 #define elf_backend_relocate_section elf_xtensa_relocate_section
11398 #define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
11399 #define elf_backend_always_size_sections elf_xtensa_always_size_sections
11400 #define elf_backend_omit_section_dynsym \
11401 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
11402 #define elf_backend_special_sections elf_xtensa_special_sections
11403 #define elf_backend_action_discarded elf_xtensa_action_discarded
11404 #define elf_backend_copy_indirect_symbol elf_xtensa_copy_indirect_symbol
11405
11406 #include "elf32-target.h"