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