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