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