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