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