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