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