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