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