1 1.1 christos /* BFD back-end for Renesas Super-H COFF binaries. 2 1.1.1.9 christos Copyright (C) 1993-2024 Free Software Foundation, Inc. 3 1.1 christos Contributed by Cygnus Support. 4 1.1 christos Written by Steve Chamberlain, <sac (at) cygnus.com>. 5 1.1 christos Relaxing code written by Ian Lance Taylor, <ian (at) cygnus.com>. 6 1.1 christos 7 1.1 christos This file is part of BFD, the Binary File Descriptor library. 8 1.1 christos 9 1.1 christos This program is free software; you can redistribute it and/or modify 10 1.1 christos it under the terms of the GNU General Public License as published by 11 1.1 christos the Free Software Foundation; either version 3 of the License, or 12 1.1 christos (at your option) any later version. 13 1.1 christos 14 1.1 christos This program is distributed in the hope that it will be useful, 15 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 16 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 1.1 christos GNU General Public License for more details. 18 1.1 christos 19 1.1 christos You should have received a copy of the GNU General Public License 20 1.1 christos along with this program; if not, write to the Free Software 21 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 22 1.1 christos MA 02110-1301, USA. */ 23 1.1 christos 24 1.1 christos #include "sysdep.h" 25 1.1 christos #include "bfd.h" 26 1.1 christos #include "libiberty.h" 27 1.1 christos #include "libbfd.h" 28 1.1 christos #include "bfdlink.h" 29 1.1 christos #include "coff/sh.h" 30 1.1 christos #include "coff/internal.h" 31 1.1 christos 32 1.1 christos #undef bfd_pe_print_pdata 33 1.1 christos 34 1.1 christos #ifdef COFF_WITH_PE 35 1.1 christos #include "coff/pe.h" 36 1.1 christos 37 1.1 christos #ifndef COFF_IMAGE_WITH_PE 38 1.1.1.8 christos static bool sh_align_load_span 39 1.1.1.2 christos (bfd *, asection *, bfd_byte *, 40 1.1.1.8 christos bool (*) (bfd *, asection *, void *, bfd_byte *, bfd_vma), 41 1.1.1.8 christos void *, bfd_vma **, bfd_vma *, bfd_vma, bfd_vma, bool *); 42 1.1 christos 43 1.1 christos #define _bfd_sh_align_load_span sh_align_load_span 44 1.1 christos #endif 45 1.1 christos 46 1.1 christos #define bfd_pe_print_pdata _bfd_pe_print_ce_compressed_pdata 47 1.1 christos 48 1.1 christos #else 49 1.1 christos 50 1.1 christos #define bfd_pe_print_pdata NULL 51 1.1 christos 52 1.1 christos #endif /* COFF_WITH_PE. */ 53 1.1 christos 54 1.1 christos #include "libcoff.h" 55 1.1 christos 56 1.1 christos /* Internal functions. */ 57 1.1 christos 58 1.1 christos #ifdef COFF_WITH_PE 59 1.1 christos /* Can't build import tables with 2**4 alignment. */ 60 1.1 christos #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2 61 1.1 christos #else 62 1.1 christos /* Default section alignment to 2**4. */ 63 1.1 christos #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 4 64 1.1 christos #endif 65 1.1 christos 66 1.1 christos #ifdef COFF_IMAGE_WITH_PE 67 1.1 christos /* Align PE executables. */ 68 1.1 christos #define COFF_PAGE_SIZE 0x1000 69 1.1 christos #endif 70 1.1 christos 71 1.1 christos /* Generate long file names. */ 72 1.1 christos #define COFF_LONG_FILENAMES 73 1.1 christos 74 1.1 christos #ifdef COFF_WITH_PE 75 1.1 christos /* Return TRUE if this relocation should 76 1.1 christos appear in the output .reloc section. */ 77 1.1.1.2 christos 78 1.1.1.8 christos static bool 79 1.1.1.2 christos in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED, 80 1.1.1.2 christos reloc_howto_type * howto) 81 1.1 christos { 82 1.1 christos return ! howto->pc_relative && howto->type != R_SH_IMAGEBASE; 83 1.1 christos } 84 1.1 christos #endif 85 1.1 christos 86 1.1.1.2 christos static bfd_reloc_status_type 87 1.1.1.2 christos sh_reloc (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); 88 1.1.1.8 christos static bool 89 1.1.1.2 christos sh_relocate_section (bfd *, struct bfd_link_info *, bfd *, asection *, 90 1.1.1.2 christos bfd_byte *, struct internal_reloc *, 91 1.1.1.2 christos struct internal_syment *, asection **); 92 1.1.1.8 christos static bool 93 1.1.1.2 christos sh_align_loads (bfd *, asection *, struct internal_reloc *, 94 1.1.1.8 christos bfd_byte *, bool *); 95 1.1.1.2 christos 96 1.1 christos /* The supported relocations. There are a lot of relocations defined 97 1.1 christos in coff/internal.h which we do not expect to ever see. */ 98 1.1 christos static reloc_howto_type sh_coff_howtos[] = 99 1.1 christos { 100 1.1 christos EMPTY_HOWTO (0), 101 1.1 christos EMPTY_HOWTO (1), 102 1.1 christos #ifdef COFF_WITH_PE 103 1.1 christos /* Windows CE */ 104 1.1 christos HOWTO (R_SH_IMM32CE, /* type */ 105 1.1 christos 0, /* rightshift */ 106 1.1.1.8 christos 4, /* size */ 107 1.1 christos 32, /* bitsize */ 108 1.1.1.8 christos false, /* pc_relative */ 109 1.1 christos 0, /* bitpos */ 110 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 111 1.1 christos sh_reloc, /* special_function */ 112 1.1 christos "r_imm32ce", /* name */ 113 1.1.1.8 christos true, /* partial_inplace */ 114 1.1 christos 0xffffffff, /* src_mask */ 115 1.1 christos 0xffffffff, /* dst_mask */ 116 1.1.1.8 christos false), /* pcrel_offset */ 117 1.1 christos #else 118 1.1 christos EMPTY_HOWTO (2), 119 1.1 christos #endif 120 1.1 christos EMPTY_HOWTO (3), /* R_SH_PCREL8 */ 121 1.1 christos EMPTY_HOWTO (4), /* R_SH_PCREL16 */ 122 1.1 christos EMPTY_HOWTO (5), /* R_SH_HIGH8 */ 123 1.1 christos EMPTY_HOWTO (6), /* R_SH_IMM24 */ 124 1.1 christos EMPTY_HOWTO (7), /* R_SH_LOW16 */ 125 1.1 christos EMPTY_HOWTO (8), 126 1.1 christos EMPTY_HOWTO (9), /* R_SH_PCDISP8BY4 */ 127 1.1 christos 128 1.1 christos HOWTO (R_SH_PCDISP8BY2, /* type */ 129 1.1 christos 1, /* rightshift */ 130 1.1.1.8 christos 2, /* size */ 131 1.1 christos 8, /* bitsize */ 132 1.1.1.8 christos true, /* pc_relative */ 133 1.1 christos 0, /* bitpos */ 134 1.1 christos complain_overflow_signed, /* complain_on_overflow */ 135 1.1 christos sh_reloc, /* special_function */ 136 1.1 christos "r_pcdisp8by2", /* name */ 137 1.1.1.8 christos true, /* partial_inplace */ 138 1.1 christos 0xff, /* src_mask */ 139 1.1 christos 0xff, /* dst_mask */ 140 1.1.1.8 christos true), /* pcrel_offset */ 141 1.1 christos 142 1.1 christos EMPTY_HOWTO (11), /* R_SH_PCDISP8 */ 143 1.1 christos 144 1.1 christos HOWTO (R_SH_PCDISP, /* type */ 145 1.1 christos 1, /* rightshift */ 146 1.1.1.8 christos 2, /* size */ 147 1.1 christos 12, /* bitsize */ 148 1.1.1.8 christos true, /* pc_relative */ 149 1.1 christos 0, /* bitpos */ 150 1.1 christos complain_overflow_signed, /* complain_on_overflow */ 151 1.1 christos sh_reloc, /* special_function */ 152 1.1 christos "r_pcdisp12by2", /* name */ 153 1.1.1.8 christos true, /* partial_inplace */ 154 1.1 christos 0xfff, /* src_mask */ 155 1.1 christos 0xfff, /* dst_mask */ 156 1.1.1.8 christos true), /* pcrel_offset */ 157 1.1 christos 158 1.1 christos EMPTY_HOWTO (13), 159 1.1 christos 160 1.1 christos HOWTO (R_SH_IMM32, /* type */ 161 1.1 christos 0, /* rightshift */ 162 1.1.1.8 christos 4, /* size */ 163 1.1 christos 32, /* bitsize */ 164 1.1.1.8 christos false, /* pc_relative */ 165 1.1 christos 0, /* bitpos */ 166 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 167 1.1 christos sh_reloc, /* special_function */ 168 1.1 christos "r_imm32", /* name */ 169 1.1.1.8 christos true, /* partial_inplace */ 170 1.1 christos 0xffffffff, /* src_mask */ 171 1.1 christos 0xffffffff, /* dst_mask */ 172 1.1.1.8 christos false), /* pcrel_offset */ 173 1.1 christos 174 1.1 christos EMPTY_HOWTO (15), 175 1.1 christos #ifdef COFF_WITH_PE 176 1.1.1.6 christos HOWTO (R_SH_IMAGEBASE, /* type */ 177 1.1.1.6 christos 0, /* rightshift */ 178 1.1.1.8 christos 4, /* size */ 179 1.1.1.6 christos 32, /* bitsize */ 180 1.1.1.8 christos false, /* pc_relative */ 181 1.1.1.6 christos 0, /* bitpos */ 182 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 183 1.1.1.6 christos sh_reloc, /* special_function */ 184 1.1.1.6 christos "rva32", /* name */ 185 1.1.1.8 christos true, /* partial_inplace */ 186 1.1.1.6 christos 0xffffffff, /* src_mask */ 187 1.1.1.6 christos 0xffffffff, /* dst_mask */ 188 1.1.1.8 christos false), /* pcrel_offset */ 189 1.1 christos #else 190 1.1 christos EMPTY_HOWTO (16), /* R_SH_IMM8 */ 191 1.1 christos #endif 192 1.1 christos EMPTY_HOWTO (17), /* R_SH_IMM8BY2 */ 193 1.1 christos EMPTY_HOWTO (18), /* R_SH_IMM8BY4 */ 194 1.1 christos EMPTY_HOWTO (19), /* R_SH_IMM4 */ 195 1.1 christos EMPTY_HOWTO (20), /* R_SH_IMM4BY2 */ 196 1.1 christos EMPTY_HOWTO (21), /* R_SH_IMM4BY4 */ 197 1.1 christos 198 1.1 christos HOWTO (R_SH_PCRELIMM8BY2, /* type */ 199 1.1 christos 1, /* rightshift */ 200 1.1.1.8 christos 2, /* size */ 201 1.1 christos 8, /* bitsize */ 202 1.1.1.8 christos true, /* pc_relative */ 203 1.1 christos 0, /* bitpos */ 204 1.1 christos complain_overflow_unsigned, /* complain_on_overflow */ 205 1.1 christos sh_reloc, /* special_function */ 206 1.1 christos "r_pcrelimm8by2", /* name */ 207 1.1.1.8 christos true, /* partial_inplace */ 208 1.1 christos 0xff, /* src_mask */ 209 1.1 christos 0xff, /* dst_mask */ 210 1.1.1.8 christos true), /* pcrel_offset */ 211 1.1 christos 212 1.1 christos HOWTO (R_SH_PCRELIMM8BY4, /* type */ 213 1.1 christos 2, /* rightshift */ 214 1.1.1.8 christos 2, /* size */ 215 1.1 christos 8, /* bitsize */ 216 1.1.1.8 christos true, /* pc_relative */ 217 1.1 christos 0, /* bitpos */ 218 1.1 christos complain_overflow_unsigned, /* complain_on_overflow */ 219 1.1 christos sh_reloc, /* special_function */ 220 1.1 christos "r_pcrelimm8by4", /* name */ 221 1.1.1.8 christos true, /* partial_inplace */ 222 1.1 christos 0xff, /* src_mask */ 223 1.1 christos 0xff, /* dst_mask */ 224 1.1.1.8 christos true), /* pcrel_offset */ 225 1.1 christos 226 1.1 christos HOWTO (R_SH_IMM16, /* type */ 227 1.1 christos 0, /* rightshift */ 228 1.1.1.8 christos 2, /* size */ 229 1.1 christos 16, /* bitsize */ 230 1.1.1.8 christos false, /* pc_relative */ 231 1.1 christos 0, /* bitpos */ 232 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 233 1.1 christos sh_reloc, /* special_function */ 234 1.1 christos "r_imm16", /* name */ 235 1.1.1.8 christos true, /* partial_inplace */ 236 1.1 christos 0xffff, /* src_mask */ 237 1.1 christos 0xffff, /* dst_mask */ 238 1.1.1.8 christos false), /* pcrel_offset */ 239 1.1 christos 240 1.1 christos HOWTO (R_SH_SWITCH16, /* type */ 241 1.1 christos 0, /* rightshift */ 242 1.1.1.8 christos 2, /* size */ 243 1.1 christos 16, /* bitsize */ 244 1.1.1.8 christos false, /* pc_relative */ 245 1.1 christos 0, /* bitpos */ 246 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 247 1.1 christos sh_reloc, /* special_function */ 248 1.1 christos "r_switch16", /* name */ 249 1.1.1.8 christos true, /* partial_inplace */ 250 1.1 christos 0xffff, /* src_mask */ 251 1.1 christos 0xffff, /* dst_mask */ 252 1.1.1.8 christos false), /* pcrel_offset */ 253 1.1 christos 254 1.1 christos HOWTO (R_SH_SWITCH32, /* type */ 255 1.1 christos 0, /* rightshift */ 256 1.1.1.8 christos 4, /* size */ 257 1.1 christos 32, /* bitsize */ 258 1.1.1.8 christos false, /* pc_relative */ 259 1.1 christos 0, /* bitpos */ 260 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 261 1.1 christos sh_reloc, /* special_function */ 262 1.1 christos "r_switch32", /* name */ 263 1.1.1.8 christos true, /* partial_inplace */ 264 1.1 christos 0xffffffff, /* src_mask */ 265 1.1 christos 0xffffffff, /* dst_mask */ 266 1.1.1.8 christos false), /* pcrel_offset */ 267 1.1 christos 268 1.1 christos HOWTO (R_SH_USES, /* type */ 269 1.1 christos 0, /* rightshift */ 270 1.1.1.8 christos 2, /* size */ 271 1.1 christos 16, /* bitsize */ 272 1.1.1.8 christos false, /* pc_relative */ 273 1.1 christos 0, /* bitpos */ 274 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 275 1.1 christos sh_reloc, /* special_function */ 276 1.1 christos "r_uses", /* name */ 277 1.1.1.8 christos true, /* partial_inplace */ 278 1.1 christos 0xffff, /* src_mask */ 279 1.1 christos 0xffff, /* dst_mask */ 280 1.1.1.8 christos false), /* pcrel_offset */ 281 1.1 christos 282 1.1 christos HOWTO (R_SH_COUNT, /* type */ 283 1.1 christos 0, /* rightshift */ 284 1.1.1.8 christos 4, /* size */ 285 1.1 christos 32, /* bitsize */ 286 1.1.1.8 christos false, /* pc_relative */ 287 1.1 christos 0, /* bitpos */ 288 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 289 1.1 christos sh_reloc, /* special_function */ 290 1.1 christos "r_count", /* name */ 291 1.1.1.8 christos true, /* partial_inplace */ 292 1.1 christos 0xffffffff, /* src_mask */ 293 1.1 christos 0xffffffff, /* dst_mask */ 294 1.1.1.8 christos false), /* pcrel_offset */ 295 1.1 christos 296 1.1 christos HOWTO (R_SH_ALIGN, /* type */ 297 1.1 christos 0, /* rightshift */ 298 1.1.1.8 christos 4, /* size */ 299 1.1 christos 32, /* bitsize */ 300 1.1.1.8 christos false, /* pc_relative */ 301 1.1 christos 0, /* bitpos */ 302 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 303 1.1 christos sh_reloc, /* special_function */ 304 1.1 christos "r_align", /* name */ 305 1.1.1.8 christos true, /* partial_inplace */ 306 1.1 christos 0xffffffff, /* src_mask */ 307 1.1 christos 0xffffffff, /* dst_mask */ 308 1.1.1.8 christos false), /* pcrel_offset */ 309 1.1 christos 310 1.1 christos HOWTO (R_SH_CODE, /* type */ 311 1.1 christos 0, /* rightshift */ 312 1.1.1.8 christos 4, /* size */ 313 1.1 christos 32, /* bitsize */ 314 1.1.1.8 christos false, /* pc_relative */ 315 1.1 christos 0, /* bitpos */ 316 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 317 1.1 christos sh_reloc, /* special_function */ 318 1.1 christos "r_code", /* name */ 319 1.1.1.8 christos true, /* partial_inplace */ 320 1.1 christos 0xffffffff, /* src_mask */ 321 1.1 christos 0xffffffff, /* dst_mask */ 322 1.1.1.8 christos false), /* pcrel_offset */ 323 1.1 christos 324 1.1 christos HOWTO (R_SH_DATA, /* type */ 325 1.1 christos 0, /* rightshift */ 326 1.1.1.8 christos 4, /* size */ 327 1.1 christos 32, /* bitsize */ 328 1.1.1.8 christos false, /* pc_relative */ 329 1.1 christos 0, /* bitpos */ 330 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 331 1.1 christos sh_reloc, /* special_function */ 332 1.1 christos "r_data", /* name */ 333 1.1.1.8 christos true, /* partial_inplace */ 334 1.1 christos 0xffffffff, /* src_mask */ 335 1.1 christos 0xffffffff, /* dst_mask */ 336 1.1.1.8 christos false), /* pcrel_offset */ 337 1.1 christos 338 1.1 christos HOWTO (R_SH_LABEL, /* type */ 339 1.1 christos 0, /* rightshift */ 340 1.1.1.8 christos 4, /* size */ 341 1.1 christos 32, /* bitsize */ 342 1.1.1.8 christos false, /* pc_relative */ 343 1.1 christos 0, /* bitpos */ 344 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 345 1.1 christos sh_reloc, /* special_function */ 346 1.1 christos "r_label", /* name */ 347 1.1.1.8 christos true, /* partial_inplace */ 348 1.1 christos 0xffffffff, /* src_mask */ 349 1.1 christos 0xffffffff, /* dst_mask */ 350 1.1.1.8 christos false), /* pcrel_offset */ 351 1.1 christos 352 1.1 christos HOWTO (R_SH_SWITCH8, /* type */ 353 1.1 christos 0, /* rightshift */ 354 1.1.1.8 christos 1, /* size */ 355 1.1 christos 8, /* bitsize */ 356 1.1.1.8 christos false, /* pc_relative */ 357 1.1 christos 0, /* bitpos */ 358 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */ 359 1.1 christos sh_reloc, /* special_function */ 360 1.1 christos "r_switch8", /* name */ 361 1.1.1.8 christos true, /* partial_inplace */ 362 1.1 christos 0xff, /* src_mask */ 363 1.1 christos 0xff, /* dst_mask */ 364 1.1.1.8 christos false) /* pcrel_offset */ 365 1.1 christos }; 366 1.1 christos 367 1.1 christos #define SH_COFF_HOWTO_COUNT (sizeof sh_coff_howtos / sizeof sh_coff_howtos[0]) 368 1.1 christos 369 1.1 christos /* Check for a bad magic number. */ 370 1.1 christos #define BADMAG(x) SHBADMAG(x) 371 1.1 christos 372 1.1 christos /* Customize coffcode.h (this is not currently used). */ 373 1.1 christos #define SH 1 374 1.1 christos 375 1.1 christos /* FIXME: This should not be set here. */ 376 1.1 christos #define __A_MAGIC_SET__ 377 1.1 christos 378 1.1 christos #ifndef COFF_WITH_PE 379 1.1 christos /* Swap the r_offset field in and out. */ 380 1.1 christos #define SWAP_IN_RELOC_OFFSET H_GET_32 381 1.1 christos #define SWAP_OUT_RELOC_OFFSET H_PUT_32 382 1.1 christos 383 1.1 christos /* Swap out extra information in the reloc structure. */ 384 1.1 christos #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \ 385 1.1 christos do \ 386 1.1 christos { \ 387 1.1 christos dst->r_stuff[0] = 'S'; \ 388 1.1 christos dst->r_stuff[1] = 'C'; \ 389 1.1 christos } \ 390 1.1 christos while (0) 391 1.1 christos #endif 392 1.1 christos 393 1.1 christos /* Get the value of a symbol, when performing a relocation. */ 394 1.1 christos 395 1.1 christos static long 396 1.1.1.2 christos get_symbol_value (asymbol *symbol) 397 1.1 christos { 398 1.1 christos bfd_vma relocation; 399 1.1 christos 400 1.1 christos if (bfd_is_com_section (symbol->section)) 401 1.1 christos relocation = 0; 402 1.1 christos else 403 1.1 christos relocation = (symbol->value + 404 1.1 christos symbol->section->output_section->vma + 405 1.1 christos symbol->section->output_offset); 406 1.1 christos 407 1.1 christos return relocation; 408 1.1 christos } 409 1.1 christos 410 1.1 christos #ifdef COFF_WITH_PE 411 1.1 christos /* Convert an rtype to howto for the COFF backend linker. 412 1.1 christos Copied from coff-i386. */ 413 1.1 christos #define coff_rtype_to_howto coff_sh_rtype_to_howto 414 1.1.1.2 christos 415 1.1 christos 416 1.1 christos static reloc_howto_type * 417 1.1.1.2 christos coff_sh_rtype_to_howto (bfd * abfd ATTRIBUTE_UNUSED, 418 1.1.1.2 christos asection * sec, 419 1.1.1.2 christos struct internal_reloc * rel, 420 1.1.1.2 christos struct coff_link_hash_entry * h, 421 1.1.1.2 christos struct internal_syment * sym, 422 1.1.1.2 christos bfd_vma * addendp) 423 1.1 christos { 424 1.1 christos reloc_howto_type * howto; 425 1.1 christos 426 1.1 christos howto = sh_coff_howtos + rel->r_type; 427 1.1 christos 428 1.1 christos *addendp = 0; 429 1.1 christos 430 1.1 christos if (howto->pc_relative) 431 1.1 christos *addendp += sec->vma; 432 1.1 christos 433 1.1 christos if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0) 434 1.1 christos { 435 1.1 christos /* This is a common symbol. The section contents include the 436 1.1 christos size (sym->n_value) as an addend. The relocate_section 437 1.1 christos function will be adding in the final value of the symbol. We 438 1.1 christos need to subtract out the current size in order to get the 439 1.1 christos correct result. */ 440 1.1 christos BFD_ASSERT (h != NULL); 441 1.1 christos } 442 1.1 christos 443 1.1 christos if (howto->pc_relative) 444 1.1 christos { 445 1.1 christos *addendp -= 4; 446 1.1 christos 447 1.1 christos /* If the symbol is defined, then the generic code is going to 448 1.1.1.6 christos add back the symbol value in order to cancel out an 449 1.1.1.6 christos adjustment it made to the addend. However, we set the addend 450 1.1.1.6 christos to 0 at the start of this function. We need to adjust here, 451 1.1.1.6 christos to avoid the adjustment the generic code will make. FIXME: 452 1.1.1.6 christos This is getting a bit hackish. */ 453 1.1 christos if (sym != NULL && sym->n_scnum != 0) 454 1.1 christos *addendp -= sym->n_value; 455 1.1 christos } 456 1.1 christos 457 1.1 christos if (rel->r_type == R_SH_IMAGEBASE) 458 1.1 christos *addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase; 459 1.1 christos 460 1.1 christos return howto; 461 1.1 christos } 462 1.1 christos 463 1.1 christos #endif /* COFF_WITH_PE */ 464 1.1 christos 465 1.1 christos /* This structure is used to map BFD reloc codes to SH PE relocs. */ 466 1.1 christos struct shcoff_reloc_map 467 1.1 christos { 468 1.1 christos bfd_reloc_code_real_type bfd_reloc_val; 469 1.1 christos unsigned char shcoff_reloc_val; 470 1.1 christos }; 471 1.1 christos 472 1.1 christos #ifdef COFF_WITH_PE 473 1.1 christos /* An array mapping BFD reloc codes to SH PE relocs. */ 474 1.1 christos static const struct shcoff_reloc_map sh_reloc_map[] = 475 1.1 christos { 476 1.1 christos { BFD_RELOC_32, R_SH_IMM32CE }, 477 1.1 christos { BFD_RELOC_RVA, R_SH_IMAGEBASE }, 478 1.1 christos { BFD_RELOC_CTOR, R_SH_IMM32CE }, 479 1.1 christos }; 480 1.1 christos #else 481 1.1 christos /* An array mapping BFD reloc codes to SH PE relocs. */ 482 1.1 christos static const struct shcoff_reloc_map sh_reloc_map[] = 483 1.1 christos { 484 1.1 christos { BFD_RELOC_32, R_SH_IMM32 }, 485 1.1 christos { BFD_RELOC_CTOR, R_SH_IMM32 }, 486 1.1 christos }; 487 1.1 christos #endif 488 1.1 christos 489 1.1 christos /* Given a BFD reloc code, return the howto structure for the 490 1.1 christos corresponding SH PE reloc. */ 491 1.1 christos #define coff_bfd_reloc_type_lookup sh_coff_reloc_type_lookup 492 1.1 christos #define coff_bfd_reloc_name_lookup sh_coff_reloc_name_lookup 493 1.1 christos 494 1.1 christos static reloc_howto_type * 495 1.1.1.6 christos sh_coff_reloc_type_lookup (bfd *abfd, 496 1.1.1.2 christos bfd_reloc_code_real_type code) 497 1.1 christos { 498 1.1 christos unsigned int i; 499 1.1 christos 500 1.1 christos for (i = ARRAY_SIZE (sh_reloc_map); i--;) 501 1.1 christos if (sh_reloc_map[i].bfd_reloc_val == code) 502 1.1 christos return &sh_coff_howtos[(int) sh_reloc_map[i].shcoff_reloc_val]; 503 1.1 christos 504 1.1.1.6 christos _bfd_error_handler (_("%pB: unsupported relocation type %#x"), 505 1.1.1.6 christos abfd, (unsigned int) code); 506 1.1 christos return NULL; 507 1.1 christos } 508 1.1 christos 509 1.1 christos static reloc_howto_type * 510 1.1 christos sh_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, 511 1.1 christos const char *r_name) 512 1.1 christos { 513 1.1 christos unsigned int i; 514 1.1 christos 515 1.1 christos for (i = 0; i < sizeof (sh_coff_howtos) / sizeof (sh_coff_howtos[0]); i++) 516 1.1 christos if (sh_coff_howtos[i].name != NULL 517 1.1 christos && strcasecmp (sh_coff_howtos[i].name, r_name) == 0) 518 1.1 christos return &sh_coff_howtos[i]; 519 1.1 christos 520 1.1 christos return NULL; 521 1.1 christos } 522 1.1 christos 523 1.1 christos /* This macro is used in coffcode.h to get the howto corresponding to 524 1.1 christos an internal reloc. */ 525 1.1 christos 526 1.1 christos #define RTYPE2HOWTO(relent, internal) \ 527 1.1 christos ((relent)->howto = \ 528 1.1 christos ((internal)->r_type < SH_COFF_HOWTO_COUNT \ 529 1.1 christos ? &sh_coff_howtos[(internal)->r_type] \ 530 1.1 christos : (reloc_howto_type *) NULL)) 531 1.1 christos 532 1.1 christos /* This is the same as the macro in coffcode.h, except that it copies 533 1.1 christos r_offset into reloc_entry->addend for some relocs. */ 534 1.1.1.3 christos #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \ 535 1.1.1.3 christos { \ 536 1.1.1.3 christos coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \ 537 1.1.1.3 christos if (ptr && bfd_asymbol_bfd (ptr) != abfd) \ 538 1.1.1.3 christos coffsym = (obj_symbols (abfd) \ 539 1.1.1.3 christos + (cache_ptr->sym_ptr_ptr - symbols)); \ 540 1.1.1.3 christos else if (ptr) \ 541 1.1.1.3 christos coffsym = coff_symbol_from (ptr); \ 542 1.1.1.3 christos if (coffsym != (coff_symbol_type *) NULL \ 543 1.1.1.3 christos && coffsym->native->u.syment.n_scnum == 0) \ 544 1.1.1.3 christos cache_ptr->addend = 0; \ 545 1.1.1.3 christos else if (ptr && bfd_asymbol_bfd (ptr) == abfd \ 546 1.1.1.3 christos && ptr->section != (asection *) NULL) \ 547 1.1.1.9 christos cache_ptr->addend = - (ptr->section->vma \ 548 1.1.1.9 christos + COFF_PE_ADDEND_BIAS (ptr)); \ 549 1.1.1.3 christos else \ 550 1.1.1.3 christos cache_ptr->addend = 0; \ 551 1.1 christos if ((reloc).r_type == R_SH_SWITCH8 \ 552 1.1 christos || (reloc).r_type == R_SH_SWITCH16 \ 553 1.1 christos || (reloc).r_type == R_SH_SWITCH32 \ 554 1.1 christos || (reloc).r_type == R_SH_USES \ 555 1.1 christos || (reloc).r_type == R_SH_COUNT \ 556 1.1 christos || (reloc).r_type == R_SH_ALIGN) \ 557 1.1 christos cache_ptr->addend = (reloc).r_offset; \ 558 1.1 christos } 559 1.1 christos 560 1.1 christos /* This is the howto function for the SH relocations. */ 561 1.1 christos 562 1.1 christos static bfd_reloc_status_type 563 1.1.1.2 christos sh_reloc (bfd * abfd, 564 1.1.1.2 christos arelent * reloc_entry, 565 1.1.1.2 christos asymbol * symbol_in, 566 1.1.1.2 christos void * data, 567 1.1.1.2 christos asection * input_section, 568 1.1.1.2 christos bfd * output_bfd, 569 1.1.1.2 christos char ** error_message ATTRIBUTE_UNUSED) 570 1.1 christos { 571 1.1.1.7 christos bfd_vma insn; 572 1.1 christos bfd_vma sym_value; 573 1.1 christos unsigned short r_type; 574 1.1 christos bfd_vma addr = reloc_entry->address; 575 1.1 christos bfd_byte *hit_data = addr + (bfd_byte *) data; 576 1.1 christos 577 1.1 christos r_type = reloc_entry->howto->type; 578 1.1 christos 579 1.1 christos if (output_bfd != NULL) 580 1.1 christos { 581 1.1 christos /* Partial linking--do nothing. */ 582 1.1 christos reloc_entry->address += input_section->output_offset; 583 1.1 christos return bfd_reloc_ok; 584 1.1 christos } 585 1.1 christos 586 1.1 christos /* Almost all relocs have to do with relaxing. If any work must be 587 1.1 christos done for them, it has been done in sh_relax_section. */ 588 1.1 christos if (r_type != R_SH_IMM32 589 1.1 christos #ifdef COFF_WITH_PE 590 1.1 christos && r_type != R_SH_IMM32CE 591 1.1 christos && r_type != R_SH_IMAGEBASE 592 1.1 christos #endif 593 1.1 christos && (r_type != R_SH_PCDISP 594 1.1 christos || (symbol_in->flags & BSF_LOCAL) != 0)) 595 1.1 christos return bfd_reloc_ok; 596 1.1 christos 597 1.1 christos if (symbol_in != NULL 598 1.1 christos && bfd_is_und_section (symbol_in->section)) 599 1.1 christos return bfd_reloc_undefined; 600 1.1 christos 601 1.1.1.8 christos if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section, 602 1.1.1.8 christos addr)) 603 1.1.1.6 christos return bfd_reloc_outofrange; 604 1.1.1.6 christos 605 1.1 christos sym_value = get_symbol_value (symbol_in); 606 1.1 christos 607 1.1 christos switch (r_type) 608 1.1 christos { 609 1.1 christos case R_SH_IMM32: 610 1.1 christos #ifdef COFF_WITH_PE 611 1.1 christos case R_SH_IMM32CE: 612 1.1 christos #endif 613 1.1 christos insn = bfd_get_32 (abfd, hit_data); 614 1.1 christos insn += sym_value + reloc_entry->addend; 615 1.1.1.7 christos bfd_put_32 (abfd, insn, hit_data); 616 1.1 christos break; 617 1.1 christos #ifdef COFF_WITH_PE 618 1.1 christos case R_SH_IMAGEBASE: 619 1.1 christos insn = bfd_get_32 (abfd, hit_data); 620 1.1 christos insn += sym_value + reloc_entry->addend; 621 1.1 christos insn -= pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase; 622 1.1.1.7 christos bfd_put_32 (abfd, insn, hit_data); 623 1.1 christos break; 624 1.1 christos #endif 625 1.1 christos case R_SH_PCDISP: 626 1.1 christos insn = bfd_get_16 (abfd, hit_data); 627 1.1 christos sym_value += reloc_entry->addend; 628 1.1 christos sym_value -= (input_section->output_section->vma 629 1.1 christos + input_section->output_offset 630 1.1 christos + addr 631 1.1 christos + 4); 632 1.1.1.7 christos sym_value += (((insn & 0xfff) ^ 0x800) - 0x800) << 1; 633 1.1.1.7 christos insn = (insn & 0xf000) | ((sym_value >> 1) & 0xfff); 634 1.1.1.7 christos bfd_put_16 (abfd, insn, hit_data); 635 1.1.1.7 christos if (sym_value + 0x1000 >= 0x2000 || (sym_value & 1) != 0) 636 1.1 christos return bfd_reloc_overflow; 637 1.1 christos break; 638 1.1 christos default: 639 1.1 christos abort (); 640 1.1 christos break; 641 1.1 christos } 642 1.1 christos 643 1.1 christos return bfd_reloc_ok; 644 1.1 christos } 645 1.1 christos 646 1.1 christos #define coff_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match 647 1.1 christos 648 1.1 christos /* We can do relaxing. */ 649 1.1 christos #define coff_bfd_relax_section sh_relax_section 650 1.1 christos 651 1.1 christos /* We use the special COFF backend linker. */ 652 1.1 christos #define coff_relocate_section sh_relocate_section 653 1.1 christos 654 1.1 christos /* When relaxing, we need to use special code to get the relocated 655 1.1 christos section contents. */ 656 1.1 christos #define coff_bfd_get_relocated_section_contents \ 657 1.1 christos sh_coff_get_relocated_section_contents 658 1.1 christos 659 1.1 christos #include "coffcode.h" 660 1.1 christos 661 1.1.1.8 christos static bool 663 1.1.1.2 christos sh_relax_delete_bytes (bfd *, asection *, bfd_vma, int); 664 1.1 christos 665 1.1 christos /* This function handles relaxing on the SH. 666 1.1 christos 667 1.1 christos Function calls on the SH look like this: 668 1.1 christos 669 1.1 christos movl L1,r0 670 1.1 christos ... 671 1.1 christos jsr @r0 672 1.1 christos ... 673 1.1 christos L1: 674 1.1 christos .long function 675 1.1 christos 676 1.1 christos The compiler and assembler will cooperate to create R_SH_USES 677 1.1 christos relocs on the jsr instructions. The r_offset field of the 678 1.1 christos R_SH_USES reloc is the PC relative offset to the instruction which 679 1.1 christos loads the register (the r_offset field is computed as though it 680 1.1 christos were a jump instruction, so the offset value is actually from four 681 1.1 christos bytes past the instruction). The linker can use this reloc to 682 1.1 christos determine just which function is being called, and thus decide 683 1.1 christos whether it is possible to replace the jsr with a bsr. 684 1.1 christos 685 1.1 christos If multiple function calls are all based on a single register load 686 1.1 christos (i.e., the same function is called multiple times), the compiler 687 1.1 christos guarantees that each function call will have an R_SH_USES reloc. 688 1.1 christos Therefore, if the linker is able to convert each R_SH_USES reloc 689 1.1 christos which refers to that address, it can safely eliminate the register 690 1.1 christos load. 691 1.1 christos 692 1.1 christos When the assembler creates an R_SH_USES reloc, it examines it to 693 1.1 christos determine which address is being loaded (L1 in the above example). 694 1.1 christos It then counts the number of references to that address, and 695 1.1 christos creates an R_SH_COUNT reloc at that address. The r_offset field of 696 1.1 christos the R_SH_COUNT reloc will be the number of references. If the 697 1.1 christos linker is able to eliminate a register load, it can use the 698 1.1 christos R_SH_COUNT reloc to see whether it can also eliminate the function 699 1.1 christos address. 700 1.1 christos 701 1.1 christos SH relaxing also handles another, unrelated, matter. On the SH, if 702 1.1 christos a load or store instruction is not aligned on a four byte boundary, 703 1.1 christos the memory cycle interferes with the 32 bit instruction fetch, 704 1.1 christos causing a one cycle bubble in the pipeline. Therefore, we try to 705 1.1 christos align load and store instructions on four byte boundaries if we 706 1.1 christos can, by swapping them with one of the adjacent instructions. */ 707 1.1.1.8 christos 708 1.1.1.2 christos static bool 709 1.1.1.2 christos sh_relax_section (bfd *abfd, 710 1.1.1.2 christos asection *sec, 711 1.1.1.8 christos struct bfd_link_info *link_info, 712 1.1 christos bool *again) 713 1.1 christos { 714 1.1.1.8 christos struct internal_reloc *internal_relocs; 715 1.1 christos bool have_code; 716 1.1 christos struct internal_reloc *irel, *irelend; 717 1.1 christos bfd_byte *contents = NULL; 718 1.1.1.8 christos 719 1.1 christos *again = false; 720 1.1.1.4 christos 721 1.1.1.9 christos if (bfd_link_relocatable (link_info) 722 1.1 christos || (sec->flags & SEC_HAS_CONTENTS) == 0 723 1.1 christos || (sec->flags & SEC_RELOC) == 0 724 1.1.1.8 christos || sec->reloc_count == 0) 725 1.1 christos return true; 726 1.1 christos 727 1.1 christos if (coff_section_data (abfd, sec) == NULL) 728 1.1.1.7 christos { 729 1.1.1.2 christos size_t amt = sizeof (struct coff_section_tdata); 730 1.1 christos sec->used_by_bfd = bfd_zalloc (abfd, amt); 731 1.1.1.8 christos if (sec->used_by_bfd == NULL) 732 1.1 christos return false; 733 1.1 christos } 734 1.1 christos 735 1.1 christos internal_relocs = (_bfd_coff_read_internal_relocs 736 1.1.1.8 christos (abfd, sec, link_info->keep_memory, 737 1.1 christos (bfd_byte *) NULL, false, 738 1.1 christos (struct internal_reloc *) NULL)); 739 1.1 christos if (internal_relocs == NULL) 740 1.1 christos goto error_return; 741 1.1.1.8 christos 742 1.1 christos have_code = false; 743 1.1 christos 744 1.1 christos irelend = internal_relocs + sec->reloc_count; 745 1.1 christos for (irel = internal_relocs; irel < irelend; irel++) 746 1.1 christos { 747 1.1 christos bfd_vma laddr, paddr, symval; 748 1.1 christos unsigned short insn; 749 1.1 christos struct internal_reloc *irelfn, *irelscan, *irelcount; 750 1.1 christos struct internal_syment sym; 751 1.1 christos bfd_signed_vma foff; 752 1.1 christos 753 1.1.1.8 christos if (irel->r_type == R_SH_CODE) 754 1.1 christos have_code = true; 755 1.1 christos 756 1.1 christos if (irel->r_type != R_SH_USES) 757 1.1 christos continue; 758 1.1 christos 759 1.1 christos /* Get the section contents. */ 760 1.1 christos if (contents == NULL) 761 1.1 christos { 762 1.1 christos if (coff_section_data (abfd, sec)->contents != NULL) 763 1.1 christos contents = coff_section_data (abfd, sec)->contents; 764 1.1 christos else 765 1.1 christos { 766 1.1 christos if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 767 1.1 christos goto error_return; 768 1.1 christos } 769 1.1 christos } 770 1.1 christos 771 1.1.1.6 christos /* The r_offset field of the R_SH_USES reloc will point us to 772 1.1.1.6 christos the register load. The 4 is because the r_offset field is 773 1.1.1.6 christos computed as though it were a jump offset, which are based 774 1.1 christos from 4 bytes after the jump instruction. */ 775 1.1 christos laddr = irel->r_vaddr - sec->vma + 4; 776 1.1 christos /* Careful to sign extend the 32-bit offset. */ 777 1.1 christos laddr += ((irel->r_offset & 0xffffffff) ^ 0x80000000) - 0x80000000; 778 1.1 christos if (laddr >= sec->size) 779 1.1.1.5 christos { 780 1.1.1.6 christos /* xgettext: c-format */ 781 1.1.1.6 christos _bfd_error_handler 782 1.1.1.6 christos (_("%pB: %#" PRIx64 ": warning: bad R_SH_USES offset"), 783 1.1 christos abfd, (uint64_t) irel->r_vaddr); 784 1.1 christos continue; 785 1.1 christos } 786 1.1 christos insn = bfd_get_16 (abfd, contents + laddr); 787 1.1 christos 788 1.1 christos /* If the instruction is not mov.l NN,rN, we don't know what to do. */ 789 1.1 christos if ((insn & 0xf000) != 0xd000) 790 1.1.1.5 christos { 791 1.1.1.5 christos _bfd_error_handler 792 1.1.1.6 christos /* xgettext: c-format */ 793 1.1.1.6 christos (_("%pB: %#" PRIx64 ": warning: R_SH_USES points to unrecognized insn %#x"), 794 1.1 christos abfd, (uint64_t) irel->r_vaddr, insn); 795 1.1 christos continue; 796 1.1 christos } 797 1.1 christos 798 1.1.1.6 christos /* Get the address from which the register is being loaded. The 799 1.1.1.6 christos displacement in the mov.l instruction is quadrupled. It is a 800 1.1.1.6 christos displacement from four bytes after the movl instruction, but, 801 1.1.1.6 christos before adding in the PC address, two least significant bits 802 1.1.1.6 christos of the PC are cleared. We assume that the section is aligned 803 1.1 christos on a four byte boundary. */ 804 1.1 christos paddr = insn & 0xff; 805 1.1 christos paddr *= 4; 806 1.1 christos paddr += (laddr + 4) &~ (bfd_vma) 3; 807 1.1 christos if (paddr >= sec->size) 808 1.1.1.5 christos { 809 1.1.1.5 christos _bfd_error_handler 810 1.1.1.6 christos /* xgettext: c-format */ 811 1.1.1.6 christos (_("%pB: %#" PRIx64 ": warning: bad R_SH_USES load offset"), 812 1.1 christos abfd, (uint64_t) irel->r_vaddr); 813 1.1 christos continue; 814 1.1 christos } 815 1.1 christos 816 1.1.1.6 christos /* Get the reloc for the address from which the register is 817 1.1.1.6 christos being loaded. This reloc will tell us which function is 818 1.1 christos actually being called. */ 819 1.1 christos paddr += sec->vma; 820 1.1 christos for (irelfn = internal_relocs; irelfn < irelend; irelfn++) 821 1.1 christos if (irelfn->r_vaddr == paddr 822 1.1 christos #ifdef COFF_WITH_PE 823 1.1 christos && (irelfn->r_type == R_SH_IMM32 824 1.1 christos || irelfn->r_type == R_SH_IMM32CE 825 1.1 christos || irelfn->r_type == R_SH_IMAGEBASE) 826 1.1 christos 827 1.1 christos #else 828 1.1 christos && irelfn->r_type == R_SH_IMM32 829 1.1 christos #endif 830 1.1 christos ) 831 1.1 christos break; 832 1.1 christos if (irelfn >= irelend) 833 1.1.1.5 christos { 834 1.1.1.5 christos _bfd_error_handler 835 1.1.1.6 christos /* xgettext: c-format */ 836 1.1.1.6 christos (_("%pB: %#" PRIx64 ": warning: could not find expected reloc"), 837 1.1 christos abfd, (uint64_t) paddr); 838 1.1 christos continue; 839 1.1 christos } 840 1.1 christos 841 1.1 christos /* Get the value of the symbol referred to by the reloc. */ 842 1.1 christos if (! _bfd_coff_get_external_symbols (abfd)) 843 1.1 christos goto error_return; 844 1.1 christos bfd_coff_swap_sym_in (abfd, 845 1.1 christos ((bfd_byte *) obj_coff_external_syms (abfd) 846 1.1 christos + (irelfn->r_symndx 847 1.1 christos * bfd_coff_symesz (abfd))), 848 1.1 christos &sym); 849 1.1 christos if (sym.n_scnum != 0 && sym.n_scnum != sec->target_index) 850 1.1.1.5 christos { 851 1.1.1.5 christos _bfd_error_handler 852 1.1.1.6 christos /* xgettext: c-format */ 853 1.1.1.6 christos (_("%pB: %#" PRIx64 ": warning: symbol in unexpected section"), 854 1.1 christos abfd, (uint64_t) paddr); 855 1.1 christos continue; 856 1.1 christos } 857 1.1 christos 858 1.1 christos if (sym.n_sclass != C_EXT) 859 1.1 christos { 860 1.1 christos symval = (sym.n_value 861 1.1 christos - sec->vma 862 1.1 christos + sec->output_section->vma 863 1.1 christos + sec->output_offset); 864 1.1 christos } 865 1.1 christos else 866 1.1 christos { 867 1.1 christos struct coff_link_hash_entry *h; 868 1.1 christos 869 1.1 christos h = obj_coff_sym_hashes (abfd)[irelfn->r_symndx]; 870 1.1 christos BFD_ASSERT (h != NULL); 871 1.1 christos if (h->root.type != bfd_link_hash_defined 872 1.1 christos && h->root.type != bfd_link_hash_defweak) 873 1.1 christos { 874 1.1.1.6 christos /* This appears to be a reference to an undefined 875 1.1.1.6 christos symbol. Just ignore it--it will be caught by the 876 1.1 christos regular reloc processing. */ 877 1.1 christos continue; 878 1.1 christos } 879 1.1 christos 880 1.1 christos symval = (h->root.u.def.value 881 1.1 christos + h->root.u.def.section->output_section->vma 882 1.1 christos + h->root.u.def.section->output_offset); 883 1.1 christos } 884 1.1 christos 885 1.1 christos symval += bfd_get_32 (abfd, contents + paddr - sec->vma); 886 1.1 christos 887 1.1 christos /* See if this function call can be shortened. */ 888 1.1 christos foff = (symval 889 1.1 christos - (irel->r_vaddr 890 1.1 christos - sec->vma 891 1.1 christos + sec->output_section->vma 892 1.1 christos + sec->output_offset 893 1.1 christos + 4)); 894 1.1 christos if (foff < -0x1000 || foff >= 0x1000) 895 1.1 christos { 896 1.1 christos /* After all that work, we can't shorten this function call. */ 897 1.1 christos continue; 898 1.1 christos } 899 1.1 christos 900 1.1 christos /* Shorten the function call. */ 901 1.1 christos 902 1.1 christos /* For simplicity of coding, we are going to modify the section 903 1.1 christos contents, the section relocs, and the BFD symbol table. We 904 1.1 christos must tell the rest of the code not to free up this 905 1.1 christos information. It would be possible to instead create a table 906 1.1 christos of changes which have to be made, as is done in coff-mips.c; 907 1.1 christos that would be more work, but would require less memory when 908 1.1 christos the linker is run. */ 909 1.1 christos 910 1.1 christos coff_section_data (abfd, sec)->relocs = internal_relocs; 911 1.1 christos coff_section_data (abfd, sec)->contents = contents; 912 1.1 christos 913 1.1 christos /* Replace the jsr with a bsr. */ 914 1.1 christos 915 1.1.1.6 christos /* Change the R_SH_USES reloc into an R_SH_PCDISP reloc, and 916 1.1 christos replace the jsr with a bsr. */ 917 1.1 christos irel->r_type = R_SH_PCDISP; 918 1.1 christos irel->r_symndx = irelfn->r_symndx; 919 1.1 christos if (sym.n_sclass != C_EXT) 920 1.1 christos { 921 1.1.1.6 christos /* If this needs to be changed because of future relaxing, 922 1.1.1.6 christos it will be handled here like other internal PCDISP 923 1.1 christos relocs. */ 924 1.1 christos bfd_put_16 (abfd, 925 1.1 christos (bfd_vma) 0xb000 | ((foff >> 1) & 0xfff), 926 1.1 christos contents + irel->r_vaddr - sec->vma); 927 1.1 christos } 928 1.1 christos else 929 1.1 christos { 930 1.1.1.6 christos /* We can't fully resolve this yet, because the external 931 1.1.1.6 christos symbol value may be changed by future relaxing. We let 932 1.1 christos the final link phase handle it. */ 933 1.1 christos bfd_put_16 (abfd, (bfd_vma) 0xb000, 934 1.1 christos contents + irel->r_vaddr - sec->vma); 935 1.1 christos } 936 1.1 christos 937 1.1.1.6 christos /* See if there is another R_SH_USES reloc referring to the same 938 1.1 christos register load. */ 939 1.1 christos for (irelscan = internal_relocs; irelscan < irelend; irelscan++) 940 1.1 christos if (irelscan->r_type == R_SH_USES 941 1.1 christos && laddr == irelscan->r_vaddr - sec->vma + 4 + irelscan->r_offset) 942 1.1 christos break; 943 1.1 christos if (irelscan < irelend) 944 1.1 christos { 945 1.1 christos /* Some other function call depends upon this register load, 946 1.1 christos and we have not yet converted that function call. 947 1.1 christos Indeed, we may never be able to convert it. There is 948 1.1 christos nothing else we can do at this point. */ 949 1.1 christos continue; 950 1.1 christos } 951 1.1 christos 952 1.1.1.6 christos /* Look for a R_SH_COUNT reloc on the location where the 953 1.1.1.6 christos function address is stored. Do this before deleting any 954 1.1 christos bytes, to avoid confusion about the address. */ 955 1.1 christos for (irelcount = internal_relocs; irelcount < irelend; irelcount++) 956 1.1 christos if (irelcount->r_vaddr == paddr 957 1.1 christos && irelcount->r_type == R_SH_COUNT) 958 1.1 christos break; 959 1.1 christos 960 1.1 christos /* Delete the register load. */ 961 1.1 christos if (! sh_relax_delete_bytes (abfd, sec, laddr, 2)) 962 1.1 christos goto error_return; 963 1.1 christos 964 1.1.1.6 christos /* That will change things, so, just in case it permits some 965 1.1.1.6 christos other function call to come within range, we should relax 966 1.1.1.8 christos again. Note that this is not required, and it may be slow. */ 967 1.1 christos *again = true; 968 1.1 christos 969 1.1 christos /* Now check whether we got a COUNT reloc. */ 970 1.1 christos if (irelcount >= irelend) 971 1.1.1.5 christos { 972 1.1.1.5 christos _bfd_error_handler 973 1.1.1.6 christos /* xgettext: c-format */ 974 1.1.1.6 christos (_("%pB: %#" PRIx64 ": warning: could not find expected COUNT reloc"), 975 1.1 christos abfd, (uint64_t) paddr); 976 1.1 christos continue; 977 1.1 christos } 978 1.1 christos 979 1.1.1.6 christos /* The number of uses is stored in the r_offset field. We've 980 1.1 christos just deleted one. */ 981 1.1 christos if (irelcount->r_offset == 0) 982 1.1.1.5 christos { 983 1.1.1.6 christos /* xgettext: c-format */ 984 1.1.1.6 christos _bfd_error_handler (_("%pB: %#" PRIx64 ": warning: bad count"), 985 1.1 christos abfd, (uint64_t) paddr); 986 1.1 christos continue; 987 1.1 christos } 988 1.1 christos 989 1.1 christos --irelcount->r_offset; 990 1.1 christos 991 1.1.1.6 christos /* If there are no more uses, we can delete the address. Reload 992 1.1.1.6 christos the address from irelfn, in case it was changed by the 993 1.1 christos previous call to sh_relax_delete_bytes. */ 994 1.1 christos if (irelcount->r_offset == 0) 995 1.1 christos { 996 1.1 christos if (! sh_relax_delete_bytes (abfd, sec, 997 1.1 christos irelfn->r_vaddr - sec->vma, 4)) 998 1.1 christos goto error_return; 999 1.1 christos } 1000 1.1 christos 1001 1.1 christos /* We've done all we can with that function call. */ 1002 1.1 christos } 1003 1.1 christos 1004 1.1 christos /* Look for load and store instructions that we can align on four 1005 1.1 christos byte boundaries. */ 1006 1.1 christos if (have_code) 1007 1.1.1.8 christos { 1008 1.1 christos bool swapped; 1009 1.1 christos 1010 1.1 christos /* Get the section contents. */ 1011 1.1 christos if (contents == NULL) 1012 1.1 christos { 1013 1.1 christos if (coff_section_data (abfd, sec)->contents != NULL) 1014 1.1 christos contents = coff_section_data (abfd, sec)->contents; 1015 1.1 christos else 1016 1.1 christos { 1017 1.1 christos if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 1018 1.1 christos goto error_return; 1019 1.1 christos } 1020 1.1 christos } 1021 1.1 christos 1022 1.1 christos if (! sh_align_loads (abfd, sec, internal_relocs, contents, &swapped)) 1023 1.1 christos goto error_return; 1024 1.1 christos 1025 1.1 christos if (swapped) 1026 1.1 christos { 1027 1.1 christos coff_section_data (abfd, sec)->relocs = internal_relocs; 1028 1.1 christos coff_section_data (abfd, sec)->contents = contents; 1029 1.1 christos } 1030 1.1 christos } 1031 1.1 christos 1032 1.1 christos if (internal_relocs != NULL 1033 1.1 christos && internal_relocs != coff_section_data (abfd, sec)->relocs) 1034 1.1 christos { 1035 1.1 christos if (! link_info->keep_memory) 1036 1.1 christos free (internal_relocs); 1037 1.1 christos else 1038 1.1 christos coff_section_data (abfd, sec)->relocs = internal_relocs; 1039 1.1 christos } 1040 1.1 christos 1041 1.1 christos if (contents != NULL && contents != coff_section_data (abfd, sec)->contents) 1042 1.1 christos { 1043 1.1 christos if (! link_info->keep_memory) 1044 1.1 christos free (contents); 1045 1.1 christos else 1046 1.1 christos /* Cache the section contents for coff_link_input_bfd. */ 1047 1.1 christos coff_section_data (abfd, sec)->contents = contents; 1048 1.1 christos } 1049 1.1.1.8 christos 1050 1.1 christos return true; 1051 1.1 christos 1052 1.1.1.7 christos error_return: 1053 1.1 christos if (internal_relocs != coff_section_data (abfd, sec)->relocs) 1054 1.1.1.7 christos free (internal_relocs); 1055 1.1 christos if (contents != coff_section_data (abfd, sec)->contents) 1056 1.1.1.8 christos free (contents); 1057 1.1 christos return false; 1058 1.1 christos } 1059 1.1 christos 1060 1.1 christos /* Delete some bytes from a section while relaxing. */ 1061 1.1.1.8 christos 1062 1.1.1.2 christos static bool 1063 1.1.1.2 christos sh_relax_delete_bytes (bfd *abfd, 1064 1.1.1.2 christos asection *sec, 1065 1.1.1.2 christos bfd_vma addr, 1066 1.1 christos int count) 1067 1.1 christos { 1068 1.1 christos bfd_byte *contents; 1069 1.1 christos struct internal_reloc *irel, *irelend; 1070 1.1 christos struct internal_reloc *irelalign; 1071 1.1 christos bfd_vma toaddr; 1072 1.1 christos bfd_byte *esym, *esymend; 1073 1.1 christos bfd_size_type symesz; 1074 1.1 christos struct coff_link_hash_entry **sym_hash; 1075 1.1 christos asection *o; 1076 1.1 christos 1077 1.1 christos contents = coff_section_data (abfd, sec)->contents; 1078 1.1.1.6 christos 1079 1.1 christos /* The deletion must stop at the next ALIGN reloc for an alignment 1080 1.1 christos power larger than the number of bytes we are deleting. */ 1081 1.1 christos 1082 1.1 christos irelalign = NULL; 1083 1.1 christos toaddr = sec->size; 1084 1.1 christos 1085 1.1 christos irel = coff_section_data (abfd, sec)->relocs; 1086 1.1 christos irelend = irel + sec->reloc_count; 1087 1.1 christos for (; irel < irelend; irel++) 1088 1.1 christos { 1089 1.1 christos if (irel->r_type == R_SH_ALIGN 1090 1.1 christos && irel->r_vaddr - sec->vma > addr 1091 1.1 christos && count < (1 << irel->r_offset)) 1092 1.1 christos { 1093 1.1 christos irelalign = irel; 1094 1.1 christos toaddr = irel->r_vaddr - sec->vma; 1095 1.1 christos break; 1096 1.1 christos } 1097 1.1 christos } 1098 1.1 christos 1099 1.1 christos /* Actually delete the bytes. */ 1100 1.1 christos memmove (contents + addr, contents + addr + count, 1101 1.1 christos (size_t) (toaddr - addr - count)); 1102 1.1 christos if (irelalign == NULL) 1103 1.1 christos sec->size -= count; 1104 1.1 christos else 1105 1.1 christos { 1106 1.1 christos int i; 1107 1.1 christos 1108 1.1 christos #define NOP_OPCODE (0x0009) 1109 1.1 christos 1110 1.1 christos BFD_ASSERT ((count & 1) == 0); 1111 1.1 christos for (i = 0; i < count; i += 2) 1112 1.1 christos bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i); 1113 1.1 christos } 1114 1.1 christos 1115 1.1 christos /* Adjust all the relocs. */ 1116 1.1 christos for (irel = coff_section_data (abfd, sec)->relocs; irel < irelend; irel++) 1117 1.1 christos { 1118 1.1 christos bfd_vma nraddr, stop; 1119 1.1 christos bfd_vma start = 0; 1120 1.1 christos int insn = 0; 1121 1.1 christos struct internal_syment sym; 1122 1.1 christos int off, adjust, oinsn; 1123 1.1.1.8 christos bfd_signed_vma voff = 0; 1124 1.1 christos bool overflow; 1125 1.1 christos 1126 1.1 christos /* Get the new reloc address. */ 1127 1.1 christos nraddr = irel->r_vaddr - sec->vma; 1128 1.1 christos if ((irel->r_vaddr - sec->vma > addr 1129 1.1 christos && irel->r_vaddr - sec->vma < toaddr) 1130 1.1 christos || (irel->r_type == R_SH_ALIGN 1131 1.1 christos && irel->r_vaddr - sec->vma == toaddr)) 1132 1.1 christos nraddr -= count; 1133 1.1 christos 1134 1.1 christos /* See if this reloc was for the bytes we have deleted, in which 1135 1.1 christos case we no longer care about it. Don't delete relocs which 1136 1.1 christos represent addresses, though. */ 1137 1.1 christos if (irel->r_vaddr - sec->vma >= addr 1138 1.1 christos && irel->r_vaddr - sec->vma < addr + count 1139 1.1 christos && irel->r_type != R_SH_ALIGN 1140 1.1 christos && irel->r_type != R_SH_CODE 1141 1.1 christos && irel->r_type != R_SH_DATA 1142 1.1 christos && irel->r_type != R_SH_LABEL) 1143 1.1 christos irel->r_type = R_SH_UNUSED; 1144 1.1 christos 1145 1.1.1.6 christos /* If this is a PC relative reloc, see if the range it covers 1146 1.1 christos includes the bytes we have deleted. */ 1147 1.1 christos switch (irel->r_type) 1148 1.1 christos { 1149 1.1 christos default: 1150 1.1 christos break; 1151 1.1 christos 1152 1.1 christos case R_SH_PCDISP8BY2: 1153 1.1 christos case R_SH_PCDISP: 1154 1.1 christos case R_SH_PCRELIMM8BY2: 1155 1.1 christos case R_SH_PCRELIMM8BY4: 1156 1.1 christos start = irel->r_vaddr - sec->vma; 1157 1.1 christos insn = bfd_get_16 (abfd, contents + nraddr); 1158 1.1 christos break; 1159 1.1 christos } 1160 1.1 christos 1161 1.1 christos switch (irel->r_type) 1162 1.1 christos { 1163 1.1 christos default: 1164 1.1 christos start = stop = addr; 1165 1.1 christos break; 1166 1.1 christos 1167 1.1 christos case R_SH_IMM32: 1168 1.1 christos #ifdef COFF_WITH_PE 1169 1.1 christos case R_SH_IMM32CE: 1170 1.1 christos case R_SH_IMAGEBASE: 1171 1.1 christos #endif 1172 1.1.1.6 christos /* If this reloc is against a symbol defined in this 1173 1.1.1.6 christos section, and the symbol will not be adjusted below, we 1174 1.1.1.6 christos must check the addend to see it will put the value in 1175 1.1 christos range to be adjusted, and hence must be changed. */ 1176 1.1 christos bfd_coff_swap_sym_in (abfd, 1177 1.1 christos ((bfd_byte *) obj_coff_external_syms (abfd) 1178 1.1 christos + (irel->r_symndx 1179 1.1 christos * bfd_coff_symesz (abfd))), 1180 1.1 christos &sym); 1181 1.1 christos if (sym.n_sclass != C_EXT 1182 1.1 christos && sym.n_scnum == sec->target_index 1183 1.1 christos && ((bfd_vma) sym.n_value <= addr 1184 1.1 christos || (bfd_vma) sym.n_value >= toaddr)) 1185 1.1 christos { 1186 1.1 christos bfd_vma val; 1187 1.1 christos 1188 1.1 christos val = bfd_get_32 (abfd, contents + nraddr); 1189 1.1 christos val += sym.n_value; 1190 1.1 christos if (val > addr && val < toaddr) 1191 1.1 christos bfd_put_32 (abfd, val - count, contents + nraddr); 1192 1.1 christos } 1193 1.1 christos start = stop = addr; 1194 1.1 christos break; 1195 1.1 christos 1196 1.1 christos case R_SH_PCDISP8BY2: 1197 1.1 christos off = insn & 0xff; 1198 1.1 christos if (off & 0x80) 1199 1.1 christos off -= 0x100; 1200 1.1 christos stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2); 1201 1.1 christos break; 1202 1.1 christos 1203 1.1 christos case R_SH_PCDISP: 1204 1.1 christos bfd_coff_swap_sym_in (abfd, 1205 1.1 christos ((bfd_byte *) obj_coff_external_syms (abfd) 1206 1.1 christos + (irel->r_symndx 1207 1.1 christos * bfd_coff_symesz (abfd))), 1208 1.1 christos &sym); 1209 1.1 christos if (sym.n_sclass == C_EXT) 1210 1.1 christos start = stop = addr; 1211 1.1 christos else 1212 1.1 christos { 1213 1.1 christos off = insn & 0xfff; 1214 1.1 christos if (off & 0x800) 1215 1.1 christos off -= 0x1000; 1216 1.1 christos stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2); 1217 1.1 christos } 1218 1.1 christos break; 1219 1.1 christos 1220 1.1 christos case R_SH_PCRELIMM8BY2: 1221 1.1 christos off = insn & 0xff; 1222 1.1 christos stop = start + 4 + off * 2; 1223 1.1 christos break; 1224 1.1 christos 1225 1.1 christos case R_SH_PCRELIMM8BY4: 1226 1.1 christos off = insn & 0xff; 1227 1.1 christos stop = (start &~ (bfd_vma) 3) + 4 + off * 4; 1228 1.1 christos break; 1229 1.1 christos 1230 1.1 christos case R_SH_SWITCH8: 1231 1.1 christos case R_SH_SWITCH16: 1232 1.1 christos case R_SH_SWITCH32: 1233 1.1 christos /* These relocs types represent 1234 1.1 christos .word L2-L1 1235 1.1 christos The r_offset field holds the difference between the reloc 1236 1.1 christos address and L1. That is the start of the reloc, and 1237 1.1 christos adding in the contents gives us the top. We must adjust 1238 1.1 christos both the r_offset field and the section contents. */ 1239 1.1 christos 1240 1.1 christos start = irel->r_vaddr - sec->vma; 1241 1.1 christos stop = (bfd_vma) ((bfd_signed_vma) start - (long) irel->r_offset); 1242 1.1 christos 1243 1.1 christos if (start > addr 1244 1.1 christos && start < toaddr 1245 1.1 christos && (stop <= addr || stop >= toaddr)) 1246 1.1 christos irel->r_offset += count; 1247 1.1 christos else if (stop > addr 1248 1.1 christos && stop < toaddr 1249 1.1 christos && (start <= addr || start >= toaddr)) 1250 1.1 christos irel->r_offset -= count; 1251 1.1 christos 1252 1.1 christos start = stop; 1253 1.1 christos 1254 1.1 christos if (irel->r_type == R_SH_SWITCH16) 1255 1.1 christos voff = bfd_get_signed_16 (abfd, contents + nraddr); 1256 1.1 christos else if (irel->r_type == R_SH_SWITCH8) 1257 1.1 christos voff = bfd_get_8 (abfd, contents + nraddr); 1258 1.1 christos else 1259 1.1 christos voff = bfd_get_signed_32 (abfd, contents + nraddr); 1260 1.1 christos stop = (bfd_vma) ((bfd_signed_vma) start + voff); 1261 1.1 christos 1262 1.1 christos break; 1263 1.1 christos 1264 1.1 christos case R_SH_USES: 1265 1.1 christos start = irel->r_vaddr - sec->vma; 1266 1.1 christos stop = (bfd_vma) ((bfd_signed_vma) start 1267 1.1 christos + (long) irel->r_offset 1268 1.1 christos + 4); 1269 1.1 christos break; 1270 1.1 christos } 1271 1.1 christos 1272 1.1 christos if (start > addr 1273 1.1 christos && start < toaddr 1274 1.1 christos && (stop <= addr || stop >= toaddr)) 1275 1.1 christos adjust = count; 1276 1.1 christos else if (stop > addr 1277 1.1 christos && stop < toaddr 1278 1.1 christos && (start <= addr || start >= toaddr)) 1279 1.1 christos adjust = - count; 1280 1.1 christos else 1281 1.1 christos adjust = 0; 1282 1.1 christos 1283 1.1 christos if (adjust != 0) 1284 1.1 christos { 1285 1.1.1.8 christos oinsn = insn; 1286 1.1 christos overflow = false; 1287 1.1 christos switch (irel->r_type) 1288 1.1 christos { 1289 1.1 christos default: 1290 1.1 christos abort (); 1291 1.1 christos break; 1292 1.1 christos 1293 1.1 christos case R_SH_PCDISP8BY2: 1294 1.1 christos case R_SH_PCRELIMM8BY2: 1295 1.1 christos insn += adjust / 2; 1296 1.1.1.8 christos if ((oinsn & 0xff00) != (insn & 0xff00)) 1297 1.1 christos overflow = true; 1298 1.1 christos bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr); 1299 1.1 christos break; 1300 1.1 christos 1301 1.1 christos case R_SH_PCDISP: 1302 1.1 christos insn += adjust / 2; 1303 1.1.1.8 christos if ((oinsn & 0xf000) != (insn & 0xf000)) 1304 1.1 christos overflow = true; 1305 1.1 christos bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr); 1306 1.1 christos break; 1307 1.1 christos 1308 1.1 christos case R_SH_PCRELIMM8BY4: 1309 1.1 christos BFD_ASSERT (adjust == count || count >= 4); 1310 1.1 christos if (count >= 4) 1311 1.1 christos insn += adjust / 4; 1312 1.1 christos else 1313 1.1 christos { 1314 1.1 christos if ((irel->r_vaddr & 3) == 0) 1315 1.1 christos ++insn; 1316 1.1 christos } 1317 1.1.1.8 christos if ((oinsn & 0xff00) != (insn & 0xff00)) 1318 1.1 christos overflow = true; 1319 1.1 christos bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr); 1320 1.1 christos break; 1321 1.1 christos 1322 1.1 christos case R_SH_SWITCH8: 1323 1.1 christos voff += adjust; 1324 1.1.1.8 christos if (voff < 0 || voff >= 0xff) 1325 1.1 christos overflow = true; 1326 1.1 christos bfd_put_8 (abfd, (bfd_vma) voff, contents + nraddr); 1327 1.1 christos break; 1328 1.1 christos 1329 1.1 christos case R_SH_SWITCH16: 1330 1.1 christos voff += adjust; 1331 1.1.1.8 christos if (voff < - 0x8000 || voff >= 0x8000) 1332 1.1 christos overflow = true; 1333 1.1 christos bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr); 1334 1.1 christos break; 1335 1.1 christos 1336 1.1 christos case R_SH_SWITCH32: 1337 1.1 christos voff += adjust; 1338 1.1 christos bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr); 1339 1.1 christos break; 1340 1.1 christos 1341 1.1 christos case R_SH_USES: 1342 1.1 christos irel->r_offset += adjust; 1343 1.1 christos break; 1344 1.1 christos } 1345 1.1 christos 1346 1.1 christos if (overflow) 1347 1.1.1.5 christos { 1348 1.1.1.5 christos _bfd_error_handler 1349 1.1.1.6 christos /* xgettext: c-format */ 1350 1.1.1.6 christos (_("%pB: %#" PRIx64 ": fatal: reloc overflow while relaxing"), 1351 1.1 christos abfd, (uint64_t) irel->r_vaddr); 1352 1.1.1.8 christos bfd_set_error (bfd_error_bad_value); 1353 1.1 christos return false; 1354 1.1 christos } 1355 1.1 christos } 1356 1.1 christos 1357 1.1 christos irel->r_vaddr = nraddr + sec->vma; 1358 1.1 christos } 1359 1.1 christos 1360 1.1 christos /* Look through all the other sections. If there contain any IMM32 1361 1.1 christos relocs against internal symbols which we are not going to adjust 1362 1.1 christos below, we may need to adjust the addends. */ 1363 1.1 christos for (o = abfd->sections; o != NULL; o = o->next) 1364 1.1 christos { 1365 1.1 christos struct internal_reloc *internal_relocs; 1366 1.1 christos struct internal_reloc *irelscan, *irelscanend; 1367 1.1 christos bfd_byte *ocontents; 1368 1.1 christos 1369 1.1.1.9 christos if (o == sec 1370 1.1 christos || (o->flags & SEC_HAS_CONTENTS) == 0 1371 1.1 christos || (o->flags & SEC_RELOC) == 0 1372 1.1 christos || o->reloc_count == 0) 1373 1.1 christos continue; 1374 1.1 christos 1375 1.1.1.6 christos /* We always cache the relocs. Perhaps, if info->keep_memory is 1376 1.1.1.6 christos FALSE, we should free them, if we are permitted to, when we 1377 1.1 christos leave sh_coff_relax_section. */ 1378 1.1.1.8 christos internal_relocs = (_bfd_coff_read_internal_relocs 1379 1.1 christos (abfd, o, true, (bfd_byte *) NULL, false, 1380 1.1 christos (struct internal_reloc *) NULL)); 1381 1.1.1.8 christos if (internal_relocs == NULL) 1382 1.1 christos return false; 1383 1.1 christos 1384 1.1 christos ocontents = NULL; 1385 1.1 christos irelscanend = internal_relocs + o->reloc_count; 1386 1.1 christos for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++) 1387 1.1 christos { 1388 1.1 christos struct internal_syment sym; 1389 1.1 christos 1390 1.1 christos #ifdef COFF_WITH_PE 1391 1.1 christos if (irelscan->r_type != R_SH_IMM32 1392 1.1 christos && irelscan->r_type != R_SH_IMAGEBASE 1393 1.1 christos && irelscan->r_type != R_SH_IMM32CE) 1394 1.1 christos #else 1395 1.1 christos if (irelscan->r_type != R_SH_IMM32) 1396 1.1 christos #endif 1397 1.1 christos continue; 1398 1.1 christos 1399 1.1 christos bfd_coff_swap_sym_in (abfd, 1400 1.1 christos ((bfd_byte *) obj_coff_external_syms (abfd) 1401 1.1 christos + (irelscan->r_symndx 1402 1.1 christos * bfd_coff_symesz (abfd))), 1403 1.1 christos &sym); 1404 1.1 christos if (sym.n_sclass != C_EXT 1405 1.1 christos && sym.n_scnum == sec->target_index 1406 1.1 christos && ((bfd_vma) sym.n_value <= addr 1407 1.1 christos || (bfd_vma) sym.n_value >= toaddr)) 1408 1.1 christos { 1409 1.1 christos bfd_vma val; 1410 1.1 christos 1411 1.1 christos if (ocontents == NULL) 1412 1.1 christos { 1413 1.1 christos if (coff_section_data (abfd, o)->contents != NULL) 1414 1.1 christos ocontents = coff_section_data (abfd, o)->contents; 1415 1.1 christos else 1416 1.1 christos { 1417 1.1.1.8 christos if (!bfd_malloc_and_get_section (abfd, o, &ocontents)) 1418 1.1 christos return false; 1419 1.1.1.6 christos /* We always cache the section contents. 1420 1.1.1.6 christos Perhaps, if info->keep_memory is FALSE, we 1421 1.1.1.6 christos should free them, if we are permitted to, 1422 1.1 christos when we leave sh_coff_relax_section. */ 1423 1.1 christos coff_section_data (abfd, o)->contents = ocontents; 1424 1.1 christos } 1425 1.1 christos } 1426 1.1 christos 1427 1.1 christos val = bfd_get_32 (abfd, ocontents + irelscan->r_vaddr - o->vma); 1428 1.1 christos val += sym.n_value; 1429 1.1 christos if (val > addr && val < toaddr) 1430 1.1 christos bfd_put_32 (abfd, val - count, 1431 1.1 christos ocontents + irelscan->r_vaddr - o->vma); 1432 1.1 christos } 1433 1.1 christos } 1434 1.1 christos } 1435 1.1 christos 1436 1.1 christos /* Adjusting the internal symbols will not work if something has 1437 1.1 christos already retrieved the generic symbols. It would be possible to 1438 1.1 christos make this work by adjusting the generic symbols at the same time. 1439 1.1 christos However, this case should not arise in normal usage. */ 1440 1.1 christos if (obj_symbols (abfd) != NULL 1441 1.1 christos || obj_raw_syments (abfd) != NULL) 1442 1.1.1.5 christos { 1443 1.1.1.6 christos _bfd_error_handler 1444 1.1 christos (_("%pB: fatal: generic symbols retrieved before relaxing"), abfd); 1445 1.1.1.8 christos bfd_set_error (bfd_error_invalid_operation); 1446 1.1 christos return false; 1447 1.1 christos } 1448 1.1 christos 1449 1.1 christos /* Adjust all the symbols. */ 1450 1.1 christos sym_hash = obj_coff_sym_hashes (abfd); 1451 1.1 christos symesz = bfd_coff_symesz (abfd); 1452 1.1 christos esym = (bfd_byte *) obj_coff_external_syms (abfd); 1453 1.1 christos esymend = esym + obj_raw_syment_count (abfd) * symesz; 1454 1.1 christos while (esym < esymend) 1455 1.1 christos { 1456 1.1 christos struct internal_syment isym; 1457 1.1.1.2 christos 1458 1.1 christos bfd_coff_swap_sym_in (abfd, esym, &isym); 1459 1.1 christos 1460 1.1 christos if (isym.n_scnum == sec->target_index 1461 1.1 christos && (bfd_vma) isym.n_value > addr 1462 1.1 christos && (bfd_vma) isym.n_value < toaddr) 1463 1.1 christos { 1464 1.1 christos isym.n_value -= count; 1465 1.1.1.2 christos 1466 1.1 christos bfd_coff_swap_sym_out (abfd, &isym, esym); 1467 1.1 christos 1468 1.1 christos if (*sym_hash != NULL) 1469 1.1 christos { 1470 1.1 christos BFD_ASSERT ((*sym_hash)->root.type == bfd_link_hash_defined 1471 1.1 christos || (*sym_hash)->root.type == bfd_link_hash_defweak); 1472 1.1 christos BFD_ASSERT ((*sym_hash)->root.u.def.value >= addr 1473 1.1 christos && (*sym_hash)->root.u.def.value < toaddr); 1474 1.1 christos (*sym_hash)->root.u.def.value -= count; 1475 1.1 christos } 1476 1.1 christos } 1477 1.1 christos 1478 1.1 christos esym += (isym.n_numaux + 1) * symesz; 1479 1.1 christos sym_hash += isym.n_numaux + 1; 1480 1.1 christos } 1481 1.1 christos 1482 1.1 christos /* See if we can move the ALIGN reloc forward. We have adjusted 1483 1.1 christos r_vaddr for it already. */ 1484 1.1 christos if (irelalign != NULL) 1485 1.1 christos { 1486 1.1 christos bfd_vma alignto, alignaddr; 1487 1.1 christos 1488 1.1 christos alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_offset); 1489 1.1 christos alignaddr = BFD_ALIGN (irelalign->r_vaddr - sec->vma, 1490 1.1 christos 1 << irelalign->r_offset); 1491 1.1 christos if (alignto != alignaddr) 1492 1.1 christos { 1493 1.1 christos /* Tail recursion. */ 1494 1.1 christos return sh_relax_delete_bytes (abfd, sec, alignaddr, 1495 1.1 christos (int) (alignto - alignaddr)); 1496 1.1 christos } 1497 1.1 christos } 1498 1.1.1.8 christos 1499 1.1 christos return true; 1500 1.1 christos } 1501 1.1 christos 1502 1.1 christos /* This is yet another version of the SH opcode table, used to rapidly 1504 1.1 christos get information about a particular instruction. */ 1505 1.1 christos 1506 1.1 christos /* The opcode map is represented by an array of these structures. The 1507 1.1 christos array is indexed by the high order four bits in the instruction. */ 1508 1.1 christos 1509 1.1 christos struct sh_major_opcode 1510 1.1 christos { 1511 1.1 christos /* A pointer to the instruction list. This is an array which 1512 1.1 christos contains all the instructions with this major opcode. */ 1513 1.1 christos const struct sh_minor_opcode *minor_opcodes; 1514 1.1 christos /* The number of elements in minor_opcodes. */ 1515 1.1 christos unsigned short count; 1516 1.1 christos }; 1517 1.1 christos 1518 1.1 christos /* This structure holds information for a set of SH opcodes. The 1519 1.1 christos instruction code is anded with the mask value, and the resulting 1520 1.1 christos value is used to search the order opcode list. */ 1521 1.1 christos 1522 1.1 christos struct sh_minor_opcode 1523 1.1 christos { 1524 1.1 christos /* The sorted opcode list. */ 1525 1.1 christos const struct sh_opcode *opcodes; 1526 1.1 christos /* The number of elements in opcodes. */ 1527 1.1 christos unsigned short count; 1528 1.1 christos /* The mask value to use when searching the opcode list. */ 1529 1.1 christos unsigned short mask; 1530 1.1 christos }; 1531 1.1 christos 1532 1.1 christos /* This structure holds information for an SH instruction. An array 1533 1.1 christos of these structures is sorted in order by opcode. */ 1534 1.1 christos 1535 1.1 christos struct sh_opcode 1536 1.1 christos { 1537 1.1 christos /* The code for this instruction, after it has been anded with the 1538 1.1 christos mask value in the sh_major_opcode structure. */ 1539 1.1 christos unsigned short opcode; 1540 1.1 christos /* Flags for this instruction. */ 1541 1.1 christos unsigned long flags; 1542 1.1 christos }; 1543 1.1 christos 1544 1.1 christos /* Flag which appear in the sh_opcode structure. */ 1545 1.1 christos 1546 1.1 christos /* This instruction loads a value from memory. */ 1547 1.1 christos #define LOAD (0x1) 1548 1.1 christos 1549 1.1 christos /* This instruction stores a value to memory. */ 1550 1.1 christos #define STORE (0x2) 1551 1.1 christos 1552 1.1 christos /* This instruction is a branch. */ 1553 1.1 christos #define BRANCH (0x4) 1554 1.1 christos 1555 1.1 christos /* This instruction has a delay slot. */ 1556 1.1 christos #define DELAY (0x8) 1557 1.1 christos 1558 1.1 christos /* This instruction uses the value in the register in the field at 1559 1.1 christos mask 0x0f00 of the instruction. */ 1560 1.1 christos #define USES1 (0x10) 1561 1.1 christos #define USES1_REG(x) ((x & 0x0f00) >> 8) 1562 1.1 christos 1563 1.1 christos /* This instruction uses the value in the register in the field at 1564 1.1 christos mask 0x00f0 of the instruction. */ 1565 1.1 christos #define USES2 (0x20) 1566 1.1 christos #define USES2_REG(x) ((x & 0x00f0) >> 4) 1567 1.1 christos 1568 1.1 christos /* This instruction uses the value in register 0. */ 1569 1.1 christos #define USESR0 (0x40) 1570 1.1 christos 1571 1.1 christos /* This instruction sets the value in the register in the field at 1572 1.1 christos mask 0x0f00 of the instruction. */ 1573 1.1 christos #define SETS1 (0x80) 1574 1.1 christos #define SETS1_REG(x) ((x & 0x0f00) >> 8) 1575 1.1 christos 1576 1.1 christos /* This instruction sets the value in the register in the field at 1577 1.1 christos mask 0x00f0 of the instruction. */ 1578 1.1 christos #define SETS2 (0x100) 1579 1.1 christos #define SETS2_REG(x) ((x & 0x00f0) >> 4) 1580 1.1 christos 1581 1.1 christos /* This instruction sets register 0. */ 1582 1.1 christos #define SETSR0 (0x200) 1583 1.1 christos 1584 1.1 christos /* This instruction sets a special register. */ 1585 1.1 christos #define SETSSP (0x400) 1586 1.1 christos 1587 1.1 christos /* This instruction uses a special register. */ 1588 1.1 christos #define USESSP (0x800) 1589 1.1 christos 1590 1.1 christos /* This instruction uses the floating point register in the field at 1591 1.1 christos mask 0x0f00 of the instruction. */ 1592 1.1 christos #define USESF1 (0x1000) 1593 1.1 christos #define USESF1_REG(x) ((x & 0x0f00) >> 8) 1594 1.1 christos 1595 1.1 christos /* This instruction uses the floating point register in the field at 1596 1.1 christos mask 0x00f0 of the instruction. */ 1597 1.1 christos #define USESF2 (0x2000) 1598 1.1 christos #define USESF2_REG(x) ((x & 0x00f0) >> 4) 1599 1.1 christos 1600 1.1 christos /* This instruction uses floating point register 0. */ 1601 1.1 christos #define USESF0 (0x4000) 1602 1.1 christos 1603 1.1 christos /* This instruction sets the floating point register in the field at 1604 1.1 christos mask 0x0f00 of the instruction. */ 1605 1.1 christos #define SETSF1 (0x8000) 1606 1.1 christos #define SETSF1_REG(x) ((x & 0x0f00) >> 8) 1607 1.1 christos 1608 1.1 christos #define USESAS (0x10000) 1609 1.1 christos #define USESAS_REG(x) (((((x) >> 8) - 2) & 3) + 2) 1610 1.1 christos #define USESR8 (0x20000) 1611 1.1 christos #define SETSAS (0x40000) 1612 1.1 christos #define SETSAS_REG(x) USESAS_REG (x) 1613 1.1 christos 1614 1.1 christos #define MAP(a) a, sizeof a / sizeof a[0] 1615 1.1 christos 1616 1.1 christos #ifndef COFF_IMAGE_WITH_PE 1617 1.1 christos 1618 1.1 christos /* The opcode maps. */ 1619 1.1 christos 1620 1.1 christos static const struct sh_opcode sh_opcode00[] = 1621 1.1 christos { 1622 1.1 christos { 0x0008, SETSSP }, /* clrt */ 1623 1.1 christos { 0x0009, 0 }, /* nop */ 1624 1.1 christos { 0x000b, BRANCH | DELAY | USESSP }, /* rts */ 1625 1.1 christos { 0x0018, SETSSP }, /* sett */ 1626 1.1 christos { 0x0019, SETSSP }, /* div0u */ 1627 1.1 christos { 0x001b, 0 }, /* sleep */ 1628 1.1 christos { 0x0028, SETSSP }, /* clrmac */ 1629 1.1 christos { 0x002b, BRANCH | DELAY | SETSSP }, /* rte */ 1630 1.1 christos { 0x0038, USESSP | SETSSP }, /* ldtlb */ 1631 1.1 christos { 0x0048, SETSSP }, /* clrs */ 1632 1.1 christos { 0x0058, SETSSP } /* sets */ 1633 1.1 christos }; 1634 1.1 christos 1635 1.1 christos static const struct sh_opcode sh_opcode01[] = 1636 1.1 christos { 1637 1.1 christos { 0x0003, BRANCH | DELAY | USES1 | SETSSP }, /* bsrf rn */ 1638 1.1 christos { 0x000a, SETS1 | USESSP }, /* sts mach,rn */ 1639 1.1 christos { 0x001a, SETS1 | USESSP }, /* sts macl,rn */ 1640 1.1 christos { 0x0023, BRANCH | DELAY | USES1 }, /* braf rn */ 1641 1.1 christos { 0x0029, SETS1 | USESSP }, /* movt rn */ 1642 1.1 christos { 0x002a, SETS1 | USESSP }, /* sts pr,rn */ 1643 1.1 christos { 0x005a, SETS1 | USESSP }, /* sts fpul,rn */ 1644 1.1 christos { 0x006a, SETS1 | USESSP }, /* sts fpscr,rn / sts dsr,rn */ 1645 1.1 christos { 0x0083, LOAD | USES1 }, /* pref @rn */ 1646 1.1 christos { 0x007a, SETS1 | USESSP }, /* sts a0,rn */ 1647 1.1 christos { 0x008a, SETS1 | USESSP }, /* sts x0,rn */ 1648 1.1 christos { 0x009a, SETS1 | USESSP }, /* sts x1,rn */ 1649 1.1 christos { 0x00aa, SETS1 | USESSP }, /* sts y0,rn */ 1650 1.1 christos { 0x00ba, SETS1 | USESSP } /* sts y1,rn */ 1651 1.1 christos }; 1652 1.1 christos 1653 1.1 christos static const struct sh_opcode sh_opcode02[] = 1654 1.1 christos { 1655 1.1 christos { 0x0002, SETS1 | USESSP }, /* stc <special_reg>,rn */ 1656 1.1 christos { 0x0004, STORE | USES1 | USES2 | USESR0 }, /* mov.b rm,@(r0,rn) */ 1657 1.1 christos { 0x0005, STORE | USES1 | USES2 | USESR0 }, /* mov.w rm,@(r0,rn) */ 1658 1.1 christos { 0x0006, STORE | USES1 | USES2 | USESR0 }, /* mov.l rm,@(r0,rn) */ 1659 1.1 christos { 0x0007, SETSSP | USES1 | USES2 }, /* mul.l rm,rn */ 1660 1.1 christos { 0x000c, LOAD | SETS1 | USES2 | USESR0 }, /* mov.b @(r0,rm),rn */ 1661 1.1 christos { 0x000d, LOAD | SETS1 | USES2 | USESR0 }, /* mov.w @(r0,rm),rn */ 1662 1.1 christos { 0x000e, LOAD | SETS1 | USES2 | USESR0 }, /* mov.l @(r0,rm),rn */ 1663 1.1 christos { 0x000f, LOAD|SETS1|SETS2|SETSSP|USES1|USES2|USESSP }, /* mac.l @rm+,@rn+ */ 1664 1.1 christos }; 1665 1.1 christos 1666 1.1 christos static const struct sh_minor_opcode sh_opcode0[] = 1667 1.1 christos { 1668 1.1 christos { MAP (sh_opcode00), 0xffff }, 1669 1.1 christos { MAP (sh_opcode01), 0xf0ff }, 1670 1.1 christos { MAP (sh_opcode02), 0xf00f } 1671 1.1 christos }; 1672 1.1 christos 1673 1.1 christos static const struct sh_opcode sh_opcode10[] = 1674 1.1 christos { 1675 1.1 christos { 0x1000, STORE | USES1 | USES2 } /* mov.l rm,@(disp,rn) */ 1676 1.1 christos }; 1677 1.1 christos 1678 1.1 christos static const struct sh_minor_opcode sh_opcode1[] = 1679 1.1 christos { 1680 1.1 christos { MAP (sh_opcode10), 0xf000 } 1681 1.1 christos }; 1682 1.1 christos 1683 1.1 christos static const struct sh_opcode sh_opcode20[] = 1684 1.1 christos { 1685 1.1 christos { 0x2000, STORE | USES1 | USES2 }, /* mov.b rm,@rn */ 1686 1.1 christos { 0x2001, STORE | USES1 | USES2 }, /* mov.w rm,@rn */ 1687 1.1 christos { 0x2002, STORE | USES1 | USES2 }, /* mov.l rm,@rn */ 1688 1.1 christos { 0x2004, STORE | SETS1 | USES1 | USES2 }, /* mov.b rm,@-rn */ 1689 1.1 christos { 0x2005, STORE | SETS1 | USES1 | USES2 }, /* mov.w rm,@-rn */ 1690 1.1 christos { 0x2006, STORE | SETS1 | USES1 | USES2 }, /* mov.l rm,@-rn */ 1691 1.1 christos { 0x2007, SETSSP | USES1 | USES2 | USESSP }, /* div0s */ 1692 1.1 christos { 0x2008, SETSSP | USES1 | USES2 }, /* tst rm,rn */ 1693 1.1 christos { 0x2009, SETS1 | USES1 | USES2 }, /* and rm,rn */ 1694 1.1 christos { 0x200a, SETS1 | USES1 | USES2 }, /* xor rm,rn */ 1695 1.1 christos { 0x200b, SETS1 | USES1 | USES2 }, /* or rm,rn */ 1696 1.1 christos { 0x200c, SETSSP | USES1 | USES2 }, /* cmp/str rm,rn */ 1697 1.1 christos { 0x200d, SETS1 | USES1 | USES2 }, /* xtrct rm,rn */ 1698 1.1 christos { 0x200e, SETSSP | USES1 | USES2 }, /* mulu.w rm,rn */ 1699 1.1 christos { 0x200f, SETSSP | USES1 | USES2 } /* muls.w rm,rn */ 1700 1.1 christos }; 1701 1.1 christos 1702 1.1 christos static const struct sh_minor_opcode sh_opcode2[] = 1703 1.1 christos { 1704 1.1 christos { MAP (sh_opcode20), 0xf00f } 1705 1.1 christos }; 1706 1.1 christos 1707 1.1 christos static const struct sh_opcode sh_opcode30[] = 1708 1.1 christos { 1709 1.1 christos { 0x3000, SETSSP | USES1 | USES2 }, /* cmp/eq rm,rn */ 1710 1.1 christos { 0x3002, SETSSP | USES1 | USES2 }, /* cmp/hs rm,rn */ 1711 1.1 christos { 0x3003, SETSSP | USES1 | USES2 }, /* cmp/ge rm,rn */ 1712 1.1 christos { 0x3004, SETSSP | USESSP | USES1 | USES2 }, /* div1 rm,rn */ 1713 1.1 christos { 0x3005, SETSSP | USES1 | USES2 }, /* dmulu.l rm,rn */ 1714 1.1 christos { 0x3006, SETSSP | USES1 | USES2 }, /* cmp/hi rm,rn */ 1715 1.1 christos { 0x3007, SETSSP | USES1 | USES2 }, /* cmp/gt rm,rn */ 1716 1.1 christos { 0x3008, SETS1 | USES1 | USES2 }, /* sub rm,rn */ 1717 1.1 christos { 0x300a, SETS1 | SETSSP | USES1 | USES2 | USESSP }, /* subc rm,rn */ 1718 1.1 christos { 0x300b, SETS1 | SETSSP | USES1 | USES2 }, /* subv rm,rn */ 1719 1.1 christos { 0x300c, SETS1 | USES1 | USES2 }, /* add rm,rn */ 1720 1.1 christos { 0x300d, SETSSP | USES1 | USES2 }, /* dmuls.l rm,rn */ 1721 1.1 christos { 0x300e, SETS1 | SETSSP | USES1 | USES2 | USESSP }, /* addc rm,rn */ 1722 1.1 christos { 0x300f, SETS1 | SETSSP | USES1 | USES2 } /* addv rm,rn */ 1723 1.1 christos }; 1724 1.1 christos 1725 1.1 christos static const struct sh_minor_opcode sh_opcode3[] = 1726 1.1 christos { 1727 1.1 christos { MAP (sh_opcode30), 0xf00f } 1728 1.1 christos }; 1729 1.1 christos 1730 1.1 christos static const struct sh_opcode sh_opcode40[] = 1731 1.1 christos { 1732 1.1 christos { 0x4000, SETS1 | SETSSP | USES1 }, /* shll rn */ 1733 1.1 christos { 0x4001, SETS1 | SETSSP | USES1 }, /* shlr rn */ 1734 1.1 christos { 0x4002, STORE | SETS1 | USES1 | USESSP }, /* sts.l mach,@-rn */ 1735 1.1 christos { 0x4004, SETS1 | SETSSP | USES1 }, /* rotl rn */ 1736 1.1 christos { 0x4005, SETS1 | SETSSP | USES1 }, /* rotr rn */ 1737 1.1 christos { 0x4006, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,mach */ 1738 1.1 christos { 0x4008, SETS1 | USES1 }, /* shll2 rn */ 1739 1.1 christos { 0x4009, SETS1 | USES1 }, /* shlr2 rn */ 1740 1.1 christos { 0x400a, SETSSP | USES1 }, /* lds rm,mach */ 1741 1.1 christos { 0x400b, BRANCH | DELAY | USES1 }, /* jsr @rn */ 1742 1.1 christos { 0x4010, SETS1 | SETSSP | USES1 }, /* dt rn */ 1743 1.1 christos { 0x4011, SETSSP | USES1 }, /* cmp/pz rn */ 1744 1.1 christos { 0x4012, STORE | SETS1 | USES1 | USESSP }, /* sts.l macl,@-rn */ 1745 1.1 christos { 0x4014, SETSSP | USES1 }, /* setrc rm */ 1746 1.1 christos { 0x4015, SETSSP | USES1 }, /* cmp/pl rn */ 1747 1.1 christos { 0x4016, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,macl */ 1748 1.1 christos { 0x4018, SETS1 | USES1 }, /* shll8 rn */ 1749 1.1 christos { 0x4019, SETS1 | USES1 }, /* shlr8 rn */ 1750 1.1 christos { 0x401a, SETSSP | USES1 }, /* lds rm,macl */ 1751 1.1 christos { 0x401b, LOAD | SETSSP | USES1 }, /* tas.b @rn */ 1752 1.1 christos { 0x4020, SETS1 | SETSSP | USES1 }, /* shal rn */ 1753 1.1 christos { 0x4021, SETS1 | SETSSP | USES1 }, /* shar rn */ 1754 1.1 christos { 0x4022, STORE | SETS1 | USES1 | USESSP }, /* sts.l pr,@-rn */ 1755 1.1 christos { 0x4024, SETS1 | SETSSP | USES1 | USESSP }, /* rotcl rn */ 1756 1.1 christos { 0x4025, SETS1 | SETSSP | USES1 | USESSP }, /* rotcr rn */ 1757 1.1 christos { 0x4026, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,pr */ 1758 1.1 christos { 0x4028, SETS1 | USES1 }, /* shll16 rn */ 1759 1.1 christos { 0x4029, SETS1 | USES1 }, /* shlr16 rn */ 1760 1.1 christos { 0x402a, SETSSP | USES1 }, /* lds rm,pr */ 1761 1.1 christos { 0x402b, BRANCH | DELAY | USES1 }, /* jmp @rn */ 1762 1.1 christos { 0x4052, STORE | SETS1 | USES1 | USESSP }, /* sts.l fpul,@-rn */ 1763 1.1 christos { 0x4056, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,fpul */ 1764 1.1 christos { 0x405a, SETSSP | USES1 }, /* lds.l rm,fpul */ 1765 1.1 christos { 0x4062, STORE | SETS1 | USES1 | USESSP }, /* sts.l fpscr / dsr,@-rn */ 1766 1.1 christos { 0x4066, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,fpscr / dsr */ 1767 1.1 christos { 0x406a, SETSSP | USES1 }, /* lds rm,fpscr / lds rm,dsr */ 1768 1.1 christos { 0x4072, STORE | SETS1 | USES1 | USESSP }, /* sts.l a0,@-rn */ 1769 1.1 christos { 0x4076, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,a0 */ 1770 1.1 christos { 0x407a, SETSSP | USES1 }, /* lds.l rm,a0 */ 1771 1.1 christos { 0x4082, STORE | SETS1 | USES1 | USESSP }, /* sts.l x0,@-rn */ 1772 1.1 christos { 0x4086, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,x0 */ 1773 1.1 christos { 0x408a, SETSSP | USES1 }, /* lds.l rm,x0 */ 1774 1.1 christos { 0x4092, STORE | SETS1 | USES1 | USESSP }, /* sts.l x1,@-rn */ 1775 1.1 christos { 0x4096, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,x1 */ 1776 1.1 christos { 0x409a, SETSSP | USES1 }, /* lds.l rm,x1 */ 1777 1.1 christos { 0x40a2, STORE | SETS1 | USES1 | USESSP }, /* sts.l y0,@-rn */ 1778 1.1 christos { 0x40a6, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,y0 */ 1779 1.1 christos { 0x40aa, SETSSP | USES1 }, /* lds.l rm,y0 */ 1780 1.1 christos { 0x40b2, STORE | SETS1 | USES1 | USESSP }, /* sts.l y1,@-rn */ 1781 1.1 christos { 0x40b6, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,y1 */ 1782 1.1 christos { 0x40ba, SETSSP | USES1 } /* lds.l rm,y1 */ 1783 1.1 christos }; 1784 1.1 christos 1785 1.1 christos static const struct sh_opcode sh_opcode41[] = 1786 1.1 christos { 1787 1.1 christos { 0x4003, STORE | SETS1 | USES1 | USESSP }, /* stc.l <special_reg>,@-rn */ 1788 1.1 christos { 0x4007, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,<special_reg> */ 1789 1.1 christos { 0x400c, SETS1 | USES1 | USES2 }, /* shad rm,rn */ 1790 1.1 christos { 0x400d, SETS1 | USES1 | USES2 }, /* shld rm,rn */ 1791 1.1 christos { 0x400e, SETSSP | USES1 }, /* ldc rm,<special_reg> */ 1792 1.1 christos { 0x400f, LOAD|SETS1|SETS2|SETSSP|USES1|USES2|USESSP }, /* mac.w @rm+,@rn+ */ 1793 1.1 christos }; 1794 1.1 christos 1795 1.1 christos static const struct sh_minor_opcode sh_opcode4[] = 1796 1.1 christos { 1797 1.1 christos { MAP (sh_opcode40), 0xf0ff }, 1798 1.1 christos { MAP (sh_opcode41), 0xf00f } 1799 1.1 christos }; 1800 1.1 christos 1801 1.1 christos static const struct sh_opcode sh_opcode50[] = 1802 1.1 christos { 1803 1.1 christos { 0x5000, LOAD | SETS1 | USES2 } /* mov.l @(disp,rm),rn */ 1804 1.1 christos }; 1805 1.1 christos 1806 1.1 christos static const struct sh_minor_opcode sh_opcode5[] = 1807 1.1 christos { 1808 1.1 christos { MAP (sh_opcode50), 0xf000 } 1809 1.1 christos }; 1810 1.1 christos 1811 1.1 christos static const struct sh_opcode sh_opcode60[] = 1812 1.1 christos { 1813 1.1 christos { 0x6000, LOAD | SETS1 | USES2 }, /* mov.b @rm,rn */ 1814 1.1 christos { 0x6001, LOAD | SETS1 | USES2 }, /* mov.w @rm,rn */ 1815 1.1 christos { 0x6002, LOAD | SETS1 | USES2 }, /* mov.l @rm,rn */ 1816 1.1 christos { 0x6003, SETS1 | USES2 }, /* mov rm,rn */ 1817 1.1 christos { 0x6004, LOAD | SETS1 | SETS2 | USES2 }, /* mov.b @rm+,rn */ 1818 1.1 christos { 0x6005, LOAD | SETS1 | SETS2 | USES2 }, /* mov.w @rm+,rn */ 1819 1.1 christos { 0x6006, LOAD | SETS1 | SETS2 | USES2 }, /* mov.l @rm+,rn */ 1820 1.1 christos { 0x6007, SETS1 | USES2 }, /* not rm,rn */ 1821 1.1 christos { 0x6008, SETS1 | USES2 }, /* swap.b rm,rn */ 1822 1.1 christos { 0x6009, SETS1 | USES2 }, /* swap.w rm,rn */ 1823 1.1 christos { 0x600a, SETS1 | SETSSP | USES2 | USESSP }, /* negc rm,rn */ 1824 1.1 christos { 0x600b, SETS1 | USES2 }, /* neg rm,rn */ 1825 1.1 christos { 0x600c, SETS1 | USES2 }, /* extu.b rm,rn */ 1826 1.1 christos { 0x600d, SETS1 | USES2 }, /* extu.w rm,rn */ 1827 1.1 christos { 0x600e, SETS1 | USES2 }, /* exts.b rm,rn */ 1828 1.1 christos { 0x600f, SETS1 | USES2 } /* exts.w rm,rn */ 1829 1.1 christos }; 1830 1.1 christos 1831 1.1 christos static const struct sh_minor_opcode sh_opcode6[] = 1832 1.1 christos { 1833 1.1 christos { MAP (sh_opcode60), 0xf00f } 1834 1.1 christos }; 1835 1.1 christos 1836 1.1 christos static const struct sh_opcode sh_opcode70[] = 1837 1.1 christos { 1838 1.1 christos { 0x7000, SETS1 | USES1 } /* add #imm,rn */ 1839 1.1 christos }; 1840 1.1 christos 1841 1.1 christos static const struct sh_minor_opcode sh_opcode7[] = 1842 1.1 christos { 1843 1.1 christos { MAP (sh_opcode70), 0xf000 } 1844 1.1 christos }; 1845 1.1 christos 1846 1.1 christos static const struct sh_opcode sh_opcode80[] = 1847 1.1 christos { 1848 1.1 christos { 0x8000, STORE | USES2 | USESR0 }, /* mov.b r0,@(disp,rn) */ 1849 1.1 christos { 0x8100, STORE | USES2 | USESR0 }, /* mov.w r0,@(disp,rn) */ 1850 1.1 christos { 0x8200, SETSSP }, /* setrc #imm */ 1851 1.1 christos { 0x8400, LOAD | SETSR0 | USES2 }, /* mov.b @(disp,rm),r0 */ 1852 1.1 christos { 0x8500, LOAD | SETSR0 | USES2 }, /* mov.w @(disp,rn),r0 */ 1853 1.1 christos { 0x8800, SETSSP | USESR0 }, /* cmp/eq #imm,r0 */ 1854 1.1 christos { 0x8900, BRANCH | USESSP }, /* bt label */ 1855 1.1 christos { 0x8b00, BRANCH | USESSP }, /* bf label */ 1856 1.1 christos { 0x8c00, SETSSP }, /* ldrs @(disp,pc) */ 1857 1.1 christos { 0x8d00, BRANCH | DELAY | USESSP }, /* bt/s label */ 1858 1.1 christos { 0x8e00, SETSSP }, /* ldre @(disp,pc) */ 1859 1.1 christos { 0x8f00, BRANCH | DELAY | USESSP } /* bf/s label */ 1860 1.1 christos }; 1861 1.1 christos 1862 1.1 christos static const struct sh_minor_opcode sh_opcode8[] = 1863 1.1 christos { 1864 1.1 christos { MAP (sh_opcode80), 0xff00 } 1865 1.1 christos }; 1866 1.1 christos 1867 1.1 christos static const struct sh_opcode sh_opcode90[] = 1868 1.1 christos { 1869 1.1 christos { 0x9000, LOAD | SETS1 } /* mov.w @(disp,pc),rn */ 1870 1.1 christos }; 1871 1.1 christos 1872 1.1 christos static const struct sh_minor_opcode sh_opcode9[] = 1873 1.1 christos { 1874 1.1 christos { MAP (sh_opcode90), 0xf000 } 1875 1.1 christos }; 1876 1.1 christos 1877 1.1 christos static const struct sh_opcode sh_opcodea0[] = 1878 1.1 christos { 1879 1.1 christos { 0xa000, BRANCH | DELAY } /* bra label */ 1880 1.1 christos }; 1881 1.1 christos 1882 1.1 christos static const struct sh_minor_opcode sh_opcodea[] = 1883 1.1 christos { 1884 1.1 christos { MAP (sh_opcodea0), 0xf000 } 1885 1.1 christos }; 1886 1.1 christos 1887 1.1 christos static const struct sh_opcode sh_opcodeb0[] = 1888 1.1 christos { 1889 1.1 christos { 0xb000, BRANCH | DELAY } /* bsr label */ 1890 1.1 christos }; 1891 1.1 christos 1892 1.1 christos static const struct sh_minor_opcode sh_opcodeb[] = 1893 1.1 christos { 1894 1.1 christos { MAP (sh_opcodeb0), 0xf000 } 1895 1.1 christos }; 1896 1.1 christos 1897 1.1 christos static const struct sh_opcode sh_opcodec0[] = 1898 1.1 christos { 1899 1.1 christos { 0xc000, STORE | USESR0 | USESSP }, /* mov.b r0,@(disp,gbr) */ 1900 1.1 christos { 0xc100, STORE | USESR0 | USESSP }, /* mov.w r0,@(disp,gbr) */ 1901 1.1 christos { 0xc200, STORE | USESR0 | USESSP }, /* mov.l r0,@(disp,gbr) */ 1902 1.1 christos { 0xc300, BRANCH | USESSP }, /* trapa #imm */ 1903 1.1 christos { 0xc400, LOAD | SETSR0 | USESSP }, /* mov.b @(disp,gbr),r0 */ 1904 1.1 christos { 0xc500, LOAD | SETSR0 | USESSP }, /* mov.w @(disp,gbr),r0 */ 1905 1.1 christos { 0xc600, LOAD | SETSR0 | USESSP }, /* mov.l @(disp,gbr),r0 */ 1906 1.1 christos { 0xc700, SETSR0 }, /* mova @(disp,pc),r0 */ 1907 1.1 christos { 0xc800, SETSSP | USESR0 }, /* tst #imm,r0 */ 1908 1.1 christos { 0xc900, SETSR0 | USESR0 }, /* and #imm,r0 */ 1909 1.1 christos { 0xca00, SETSR0 | USESR0 }, /* xor #imm,r0 */ 1910 1.1 christos { 0xcb00, SETSR0 | USESR0 }, /* or #imm,r0 */ 1911 1.1 christos { 0xcc00, LOAD | SETSSP | USESR0 | USESSP }, /* tst.b #imm,@(r0,gbr) */ 1912 1.1 christos { 0xcd00, LOAD | STORE | USESR0 | USESSP }, /* and.b #imm,@(r0,gbr) */ 1913 1.1 christos { 0xce00, LOAD | STORE | USESR0 | USESSP }, /* xor.b #imm,@(r0,gbr) */ 1914 1.1 christos { 0xcf00, LOAD | STORE | USESR0 | USESSP } /* or.b #imm,@(r0,gbr) */ 1915 1.1 christos }; 1916 1.1 christos 1917 1.1 christos static const struct sh_minor_opcode sh_opcodec[] = 1918 1.1 christos { 1919 1.1 christos { MAP (sh_opcodec0), 0xff00 } 1920 1.1 christos }; 1921 1.1 christos 1922 1.1 christos static const struct sh_opcode sh_opcoded0[] = 1923 1.1 christos { 1924 1.1 christos { 0xd000, LOAD | SETS1 } /* mov.l @(disp,pc),rn */ 1925 1.1 christos }; 1926 1.1 christos 1927 1.1 christos static const struct sh_minor_opcode sh_opcoded[] = 1928 1.1 christos { 1929 1.1 christos { MAP (sh_opcoded0), 0xf000 } 1930 1.1 christos }; 1931 1.1 christos 1932 1.1 christos static const struct sh_opcode sh_opcodee0[] = 1933 1.1 christos { 1934 1.1 christos { 0xe000, SETS1 } /* mov #imm,rn */ 1935 1.1 christos }; 1936 1.1 christos 1937 1.1 christos static const struct sh_minor_opcode sh_opcodee[] = 1938 1.1 christos { 1939 1.1 christos { MAP (sh_opcodee0), 0xf000 } 1940 1.1 christos }; 1941 1.1 christos 1942 1.1 christos static const struct sh_opcode sh_opcodef0[] = 1943 1.1 christos { 1944 1.1 christos { 0xf000, SETSF1 | USESF1 | USESF2 }, /* fadd fm,fn */ 1945 1.1 christos { 0xf001, SETSF1 | USESF1 | USESF2 }, /* fsub fm,fn */ 1946 1.1 christos { 0xf002, SETSF1 | USESF1 | USESF2 }, /* fmul fm,fn */ 1947 1.1 christos { 0xf003, SETSF1 | USESF1 | USESF2 }, /* fdiv fm,fn */ 1948 1.1 christos { 0xf004, SETSSP | USESF1 | USESF2 }, /* fcmp/eq fm,fn */ 1949 1.1 christos { 0xf005, SETSSP | USESF1 | USESF2 }, /* fcmp/gt fm,fn */ 1950 1.1 christos { 0xf006, LOAD | SETSF1 | USES2 | USESR0 }, /* fmov.s @(r0,rm),fn */ 1951 1.1 christos { 0xf007, STORE | USES1 | USESF2 | USESR0 }, /* fmov.s fm,@(r0,rn) */ 1952 1.1 christos { 0xf008, LOAD | SETSF1 | USES2 }, /* fmov.s @rm,fn */ 1953 1.1 christos { 0xf009, LOAD | SETS2 | SETSF1 | USES2 }, /* fmov.s @rm+,fn */ 1954 1.1 christos { 0xf00a, STORE | USES1 | USESF2 }, /* fmov.s fm,@rn */ 1955 1.1 christos { 0xf00b, STORE | SETS1 | USES1 | USESF2 }, /* fmov.s fm,@-rn */ 1956 1.1 christos { 0xf00c, SETSF1 | USESF2 }, /* fmov fm,fn */ 1957 1.1 christos { 0xf00e, SETSF1 | USESF1 | USESF2 | USESF0 } /* fmac f0,fm,fn */ 1958 1.1 christos }; 1959 1.1 christos 1960 1.1 christos static const struct sh_opcode sh_opcodef1[] = 1961 1.1 christos { 1962 1.1 christos { 0xf00d, SETSF1 | USESSP }, /* fsts fpul,fn */ 1963 1.1 christos { 0xf01d, SETSSP | USESF1 }, /* flds fn,fpul */ 1964 1.1 christos { 0xf02d, SETSF1 | USESSP }, /* float fpul,fn */ 1965 1.1 christos { 0xf03d, SETSSP | USESF1 }, /* ftrc fn,fpul */ 1966 1.1 christos { 0xf04d, SETSF1 | USESF1 }, /* fneg fn */ 1967 1.1 christos { 0xf05d, SETSF1 | USESF1 }, /* fabs fn */ 1968 1.1 christos { 0xf06d, SETSF1 | USESF1 }, /* fsqrt fn */ 1969 1.1 christos { 0xf07d, SETSSP | USESF1 }, /* ftst/nan fn */ 1970 1.1 christos { 0xf08d, SETSF1 }, /* fldi0 fn */ 1971 1.1 christos { 0xf09d, SETSF1 } /* fldi1 fn */ 1972 1.1 christos }; 1973 1.1 christos 1974 1.1 christos static const struct sh_minor_opcode sh_opcodef[] = 1975 1.1 christos { 1976 1.1 christos { MAP (sh_opcodef0), 0xf00f }, 1977 1.1 christos { MAP (sh_opcodef1), 0xf0ff } 1978 1.1 christos }; 1979 1.1 christos 1980 1.1 christos static struct sh_major_opcode sh_opcodes[] = 1981 1.1 christos { 1982 1.1 christos { MAP (sh_opcode0) }, 1983 1.1 christos { MAP (sh_opcode1) }, 1984 1.1 christos { MAP (sh_opcode2) }, 1985 1.1 christos { MAP (sh_opcode3) }, 1986 1.1 christos { MAP (sh_opcode4) }, 1987 1.1 christos { MAP (sh_opcode5) }, 1988 1.1 christos { MAP (sh_opcode6) }, 1989 1.1 christos { MAP (sh_opcode7) }, 1990 1.1 christos { MAP (sh_opcode8) }, 1991 1.1 christos { MAP (sh_opcode9) }, 1992 1.1 christos { MAP (sh_opcodea) }, 1993 1.1 christos { MAP (sh_opcodeb) }, 1994 1.1 christos { MAP (sh_opcodec) }, 1995 1.1 christos { MAP (sh_opcoded) }, 1996 1.1 christos { MAP (sh_opcodee) }, 1997 1.1 christos { MAP (sh_opcodef) } 1998 1.1 christos }; 1999 1.1 christos 2000 1.1 christos /* The double data transfer / parallel processing insns are not 2001 1.1 christos described here. This will cause sh_align_load_span to leave them alone. */ 2002 1.1 christos 2003 1.1 christos static const struct sh_opcode sh_dsp_opcodef0[] = 2004 1.1 christos { 2005 1.1 christos { 0xf400, USESAS | SETSAS | LOAD | SETSSP }, /* movs.x @-as,ds */ 2006 1.1 christos { 0xf401, USESAS | SETSAS | STORE | USESSP }, /* movs.x ds,@-as */ 2007 1.1 christos { 0xf404, USESAS | LOAD | SETSSP }, /* movs.x @as,ds */ 2008 1.1 christos { 0xf405, USESAS | STORE | USESSP }, /* movs.x ds,@as */ 2009 1.1 christos { 0xf408, USESAS | SETSAS | LOAD | SETSSP }, /* movs.x @as+,ds */ 2010 1.1 christos { 0xf409, USESAS | SETSAS | STORE | USESSP }, /* movs.x ds,@as+ */ 2011 1.1 christos { 0xf40c, USESAS | SETSAS | LOAD | SETSSP | USESR8 }, /* movs.x @as+r8,ds */ 2012 1.1 christos { 0xf40d, USESAS | SETSAS | STORE | USESSP | USESR8 } /* movs.x ds,@as+r8 */ 2013 1.1 christos }; 2014 1.1 christos 2015 1.1 christos static const struct sh_minor_opcode sh_dsp_opcodef[] = 2016 1.1 christos { 2017 1.1 christos { MAP (sh_dsp_opcodef0), 0xfc0d } 2018 1.1 christos }; 2019 1.1 christos 2020 1.1 christos /* Given an instruction, return a pointer to the corresponding 2021 1.1 christos sh_opcode structure. Return NULL if the instruction is not 2022 1.1 christos recognized. */ 2023 1.1.1.2 christos 2024 1.1 christos static const struct sh_opcode * 2025 1.1 christos sh_insn_info (unsigned int insn) 2026 1.1 christos { 2027 1.1 christos const struct sh_major_opcode *maj; 2028 1.1 christos const struct sh_minor_opcode *min, *minend; 2029 1.1 christos 2030 1.1 christos maj = &sh_opcodes[(insn & 0xf000) >> 12]; 2031 1.1 christos min = maj->minor_opcodes; 2032 1.1 christos minend = min + maj->count; 2033 1.1 christos for (; min < minend; min++) 2034 1.1 christos { 2035 1.1 christos unsigned int l; 2036 1.1 christos const struct sh_opcode *op, *opend; 2037 1.1 christos 2038 1.1 christos l = insn & min->mask; 2039 1.1 christos op = min->opcodes; 2040 1.1 christos opend = op + min->count; 2041 1.1.1.6 christos 2042 1.1 christos /* Since the opcodes tables are sorted, we could use a binary 2043 1.1 christos search here if the count were above some cutoff value. */ 2044 1.1 christos for (; op < opend; op++) 2045 1.1 christos if (op->opcode == l) 2046 1.1 christos return op; 2047 1.1 christos } 2048 1.1 christos 2049 1.1 christos return NULL; 2050 1.1 christos } 2051 1.1 christos 2052 1.1.1.8 christos /* See whether an instruction uses a general purpose register. */ 2053 1.1.1.2 christos 2054 1.1.1.2 christos static bool 2055 1.1.1.2 christos sh_insn_uses_reg (unsigned int insn, 2056 1.1 christos const struct sh_opcode *op, 2057 1.1 christos unsigned int reg) 2058 1.1 christos { 2059 1.1 christos unsigned int f; 2060 1.1 christos 2061 1.1 christos f = op->flags; 2062 1.1 christos 2063 1.1.1.8 christos if ((f & USES1) != 0 2064 1.1 christos && USES1_REG (insn) == reg) 2065 1.1 christos return true; 2066 1.1.1.8 christos if ((f & USES2) != 0 2067 1.1 christos && USES2_REG (insn) == reg) 2068 1.1 christos return true; 2069 1.1.1.8 christos if ((f & USESR0) != 0 2070 1.1 christos && reg == 0) 2071 1.1.1.8 christos return true; 2072 1.1 christos if ((f & USESAS) && reg == USESAS_REG (insn)) 2073 1.1.1.8 christos return true; 2074 1.1 christos if ((f & USESR8) && reg == 8) 2075 1.1.1.8 christos return true; 2076 1.1 christos 2077 1.1 christos return false; 2078 1.1 christos } 2079 1.1 christos 2080 1.1.1.8 christos /* See whether an instruction sets a general purpose register. */ 2081 1.1.1.2 christos 2082 1.1.1.2 christos static bool 2083 1.1.1.2 christos sh_insn_sets_reg (unsigned int insn, 2084 1.1 christos const struct sh_opcode *op, 2085 1.1 christos unsigned int reg) 2086 1.1 christos { 2087 1.1 christos unsigned int f; 2088 1.1 christos 2089 1.1 christos f = op->flags; 2090 1.1 christos 2091 1.1.1.8 christos if ((f & SETS1) != 0 2092 1.1 christos && SETS1_REG (insn) == reg) 2093 1.1 christos return true; 2094 1.1.1.8 christos if ((f & SETS2) != 0 2095 1.1 christos && SETS2_REG (insn) == reg) 2096 1.1 christos return true; 2097 1.1.1.8 christos if ((f & SETSR0) != 0 2098 1.1 christos && reg == 0) 2099 1.1.1.8 christos return true; 2100 1.1 christos if ((f & SETSAS) && reg == SETSAS_REG (insn)) 2101 1.1.1.8 christos return true; 2102 1.1 christos 2103 1.1 christos return false; 2104 1.1.1.2 christos } 2105 1.1 christos 2106 1.1.1.8 christos /* See whether an instruction uses or sets a general purpose register */ 2107 1.1.1.2 christos 2108 1.1.1.2 christos static bool 2109 1.1.1.2 christos sh_insn_uses_or_sets_reg (unsigned int insn, 2110 1.1 christos const struct sh_opcode *op, 2111 1.1.1.2 christos unsigned int reg) 2112 1.1.1.8 christos { 2113 1.1 christos if (sh_insn_uses_reg (insn, op, reg)) 2114 1.1.1.2 christos return true; 2115 1.1 christos 2116 1.1 christos return sh_insn_sets_reg (insn, op, reg); 2117 1.1 christos } 2118 1.1 christos 2119 1.1.1.8 christos /* See whether an instruction uses a floating point register. */ 2120 1.1.1.2 christos 2121 1.1.1.2 christos static bool 2122 1.1.1.2 christos sh_insn_uses_freg (unsigned int insn, 2123 1.1 christos const struct sh_opcode *op, 2124 1.1 christos unsigned int freg) 2125 1.1 christos { 2126 1.1 christos unsigned int f; 2127 1.1 christos 2128 1.1 christos f = op->flags; 2129 1.1 christos 2130 1.1 christos /* We can't tell if this is a double-precision insn, so just play safe 2131 1.1 christos and assume that it might be. So not only have we test FREG against 2132 1.1 christos itself, but also even FREG against FREG+1 - if the using insn uses 2133 1.1 christos just the low part of a double precision value - but also an odd 2134 1.1 christos FREG against FREG-1 - if the setting insn sets just the low part 2135 1.1 christos of a double precision value. 2136 1.1 christos So what this all boils down to is that we have to ignore the lowest 2137 1.1 christos bit of the register number. */ 2138 1.1 christos 2139 1.1.1.8 christos if ((f & USESF1) != 0 2140 1.1 christos && (USESF1_REG (insn) & 0xe) == (freg & 0xe)) 2141 1.1 christos return true; 2142 1.1.1.8 christos if ((f & USESF2) != 0 2143 1.1 christos && (USESF2_REG (insn) & 0xe) == (freg & 0xe)) 2144 1.1 christos return true; 2145 1.1.1.8 christos if ((f & USESF0) != 0 2146 1.1 christos && freg == 0) 2147 1.1.1.8 christos return true; 2148 1.1 christos 2149 1.1 christos return false; 2150 1.1 christos } 2151 1.1 christos 2152 1.1.1.8 christos /* See whether an instruction sets a floating point register. */ 2153 1.1.1.2 christos 2154 1.1.1.2 christos static bool 2155 1.1.1.2 christos sh_insn_sets_freg (unsigned int insn, 2156 1.1 christos const struct sh_opcode *op, 2157 1.1 christos unsigned int freg) 2158 1.1 christos { 2159 1.1 christos unsigned int f; 2160 1.1 christos 2161 1.1 christos f = op->flags; 2162 1.1 christos 2163 1.1 christos /* We can't tell if this is a double-precision insn, so just play safe 2164 1.1 christos and assume that it might be. So not only have we test FREG against 2165 1.1 christos itself, but also even FREG against FREG+1 - if the using insn uses 2166 1.1 christos just the low part of a double precision value - but also an odd 2167 1.1 christos FREG against FREG-1 - if the setting insn sets just the low part 2168 1.1 christos of a double precision value. 2169 1.1 christos So what this all boils down to is that we have to ignore the lowest 2170 1.1 christos bit of the register number. */ 2171 1.1 christos 2172 1.1.1.8 christos if ((f & SETSF1) != 0 2173 1.1 christos && (SETSF1_REG (insn) & 0xe) == (freg & 0xe)) 2174 1.1.1.8 christos return true; 2175 1.1 christos 2176 1.1 christos return false; 2177 1.1.1.2 christos } 2178 1.1.1.2 christos 2179 1.1.1.8 christos /* See whether an instruction uses or sets a floating point register */ 2180 1.1.1.2 christos 2181 1.1.1.2 christos static bool 2182 1.1.1.2 christos sh_insn_uses_or_sets_freg (unsigned int insn, 2183 1.1.1.2 christos const struct sh_opcode *op, 2184 1.1.1.2 christos unsigned int reg) 2185 1.1.1.8 christos { 2186 1.1.1.2 christos if (sh_insn_uses_freg (insn, op, reg)) 2187 1.1.1.2 christos return true; 2188 1.1.1.2 christos 2189 1.1.1.2 christos return sh_insn_sets_freg (insn, op, reg); 2190 1.1 christos } 2191 1.1 christos 2192 1.1 christos /* See whether instructions I1 and I2 conflict, assuming I1 comes 2193 1.1 christos before I2. OP1 and OP2 are the corresponding sh_opcode structures. 2194 1.1 christos This should return TRUE if there is a conflict, or FALSE if the 2195 1.1.1.8 christos instructions can be swapped safely. */ 2196 1.1.1.2 christos 2197 1.1.1.2 christos static bool 2198 1.1.1.2 christos sh_insns_conflict (unsigned int i1, 2199 1.1.1.2 christos const struct sh_opcode *op1, 2200 1.1 christos unsigned int i2, 2201 1.1 christos const struct sh_opcode *op2) 2202 1.1 christos { 2203 1.1 christos unsigned int f1, f2; 2204 1.1 christos 2205 1.1 christos f1 = op1->flags; 2206 1.1 christos f2 = op2->flags; 2207 1.1 christos 2208 1.1 christos /* Load of fpscr conflicts with floating point operations. 2209 1.1 christos FIXME: shouldn't test raw opcodes here. */ 2210 1.1.1.8 christos if (((i1 & 0xf0ff) == 0x4066 && (i2 & 0xf000) == 0xf000) 2211 1.1 christos || ((i2 & 0xf0ff) == 0x4066 && (i1 & 0xf000) == 0xf000)) 2212 1.1 christos return true; 2213 1.1 christos 2214 1.1.1.8 christos if ((f1 & (BRANCH | DELAY)) != 0 2215 1.1 christos || (f2 & (BRANCH | DELAY)) != 0) 2216 1.1 christos return true; 2217 1.1 christos 2218 1.1 christos if (((f1 | f2) & SETSSP) 2219 1.1.1.8 christos && (f1 & (SETSSP | USESSP)) 2220 1.1 christos && (f2 & (SETSSP | USESSP))) 2221 1.1 christos return true; 2222 1.1 christos 2223 1.1.1.8 christos if ((f1 & SETS1) != 0 2224 1.1 christos && sh_insn_uses_or_sets_reg (i2, op2, SETS1_REG (i1))) 2225 1.1 christos return true; 2226 1.1.1.8 christos if ((f1 & SETS2) != 0 2227 1.1 christos && sh_insn_uses_or_sets_reg (i2, op2, SETS2_REG (i1))) 2228 1.1 christos return true; 2229 1.1.1.8 christos if ((f1 & SETSR0) != 0 2230 1.1 christos && sh_insn_uses_or_sets_reg (i2, op2, 0)) 2231 1.1 christos return true; 2232 1.1.1.8 christos if ((f1 & SETSAS) 2233 1.1 christos && sh_insn_uses_or_sets_reg (i2, op2, SETSAS_REG (i1))) 2234 1.1 christos return true; 2235 1.1.1.8 christos if ((f1 & SETSF1) != 0 2236 1.1 christos && sh_insn_uses_or_sets_freg (i2, op2, SETSF1_REG (i1))) 2237 1.1 christos return true; 2238 1.1 christos 2239 1.1.1.8 christos if ((f2 & SETS1) != 0 2240 1.1 christos && sh_insn_uses_or_sets_reg (i1, op1, SETS1_REG (i2))) 2241 1.1 christos return true; 2242 1.1.1.8 christos if ((f2 & SETS2) != 0 2243 1.1 christos && sh_insn_uses_or_sets_reg (i1, op1, SETS2_REG (i2))) 2244 1.1 christos return true; 2245 1.1.1.8 christos if ((f2 & SETSR0) != 0 2246 1.1 christos && sh_insn_uses_or_sets_reg (i1, op1, 0)) 2247 1.1 christos return true; 2248 1.1.1.8 christos if ((f2 & SETSAS) 2249 1.1 christos && sh_insn_uses_or_sets_reg (i1, op1, SETSAS_REG (i2))) 2250 1.1 christos return true; 2251 1.1.1.8 christos if ((f2 & SETSF1) != 0 2252 1.1 christos && sh_insn_uses_or_sets_freg (i1, op1, SETSF1_REG (i2))) 2253 1.1 christos return true; 2254 1.1.1.8 christos 2255 1.1 christos /* The instructions do not conflict. */ 2256 1.1 christos return false; 2257 1.1 christos } 2258 1.1 christos 2259 1.1 christos /* I1 is a load instruction, and I2 is some other instruction. Return 2260 1.1.1.8 christos TRUE if I1 loads a register which I2 uses. */ 2261 1.1.1.2 christos 2262 1.1.1.2 christos static bool 2263 1.1.1.2 christos sh_load_use (unsigned int i1, 2264 1.1.1.2 christos const struct sh_opcode *op1, 2265 1.1 christos unsigned int i2, 2266 1.1 christos const struct sh_opcode *op2) 2267 1.1 christos { 2268 1.1 christos unsigned int f1; 2269 1.1 christos 2270 1.1 christos f1 = op1->flags; 2271 1.1.1.8 christos 2272 1.1 christos if ((f1 & LOAD) == 0) 2273 1.1 christos return false; 2274 1.1 christos 2275 1.1 christos /* If both SETS1 and SETSSP are set, that means a load to a special 2276 1.1 christos register using postincrement addressing mode, which we don't care 2277 1.1 christos about here. */ 2278 1.1 christos if ((f1 & SETS1) != 0 2279 1.1.1.8 christos && (f1 & SETSSP) == 0 2280 1.1 christos && sh_insn_uses_reg (i2, op2, (i1 & 0x0f00) >> 8)) 2281 1.1 christos return true; 2282 1.1 christos 2283 1.1.1.8 christos if ((f1 & SETSR0) != 0 2284 1.1 christos && sh_insn_uses_reg (i2, op2, 0)) 2285 1.1 christos return true; 2286 1.1 christos 2287 1.1.1.8 christos if ((f1 & SETSF1) != 0 2288 1.1 christos && sh_insn_uses_freg (i2, op2, (i1 & 0x0f00) >> 8)) 2289 1.1.1.8 christos return true; 2290 1.1 christos 2291 1.1 christos return false; 2292 1.1 christos } 2293 1.1 christos 2294 1.1 christos /* Try to align loads and stores within a span of memory. This is 2295 1.1 christos called by both the ELF and the COFF sh targets. ABFD and SEC are 2296 1.1 christos the BFD and section we are examining. CONTENTS is the contents of 2297 1.1 christos the section. SWAP is the routine to call to swap two instructions. 2298 1.1 christos RELOCS is a pointer to the internal relocation information, to be 2299 1.1 christos passed to SWAP. PLABEL is a pointer to the current label in a 2300 1.1 christos sorted list of labels; LABEL_END is the end of the list. START and 2301 1.1 christos STOP are the range of memory to examine. If a swap is made, 2302 1.1 christos *PSWAPPED is set to TRUE. */ 2303 1.1 christos 2304 1.1 christos #ifdef COFF_WITH_PE 2305 1.1.1.8 christos static 2306 1.1.1.2 christos #endif 2307 1.1.1.2 christos bool 2308 1.1.1.2 christos _bfd_sh_align_load_span (bfd *abfd, 2309 1.1.1.8 christos asection *sec, 2310 1.1.1.2 christos bfd_byte *contents, 2311 1.1.1.2 christos bool (*swap) (bfd *, asection *, void *, bfd_byte *, bfd_vma), 2312 1.1.1.2 christos void * relocs, 2313 1.1.1.2 christos bfd_vma **plabel, 2314 1.1.1.2 christos bfd_vma *label_end, 2315 1.1.1.8 christos bfd_vma start, 2316 1.1 christos bfd_vma stop, 2317 1.1 christos bool *pswapped) 2318 1.1 christos { 2319 1.1 christos int dsp = (abfd->arch_info->mach == bfd_mach_sh_dsp 2320 1.1 christos || abfd->arch_info->mach == bfd_mach_sh3_dsp); 2321 1.1 christos bfd_vma i; 2322 1.1 christos 2323 1.1 christos /* The SH4 has a Harvard architecture, hence aligning loads is not 2324 1.1 christos desirable. In fact, it is counter-productive, since it interferes 2325 1.1.1.8 christos with the schedules generated by the compiler. */ 2326 1.1 christos if (abfd->arch_info->mach == bfd_mach_sh4) 2327 1.1 christos return true; 2328 1.1 christos 2329 1.1 christos /* If we are linking sh[3]-dsp code, swap the FPU instructions for DSP 2330 1.1 christos instructions. */ 2331 1.1 christos if (dsp) 2332 1.1.1.4 christos { 2333 1.1 christos sh_opcodes[0xf].minor_opcodes = sh_dsp_opcodef; 2334 1.1 christos sh_opcodes[0xf].count = sizeof sh_dsp_opcodef / sizeof sh_dsp_opcodef [0]; 2335 1.1 christos } 2336 1.1 christos 2337 1.1 christos /* Instructions should be aligned on 2 byte boundaries. */ 2338 1.1 christos if ((start & 1) == 1) 2339 1.1 christos ++start; 2340 1.1 christos 2341 1.1 christos /* Now look through the unaligned addresses. */ 2342 1.1 christos i = start; 2343 1.1 christos if ((i & 2) == 0) 2344 1.1 christos i += 2; 2345 1.1 christos for (; i < stop; i += 4) 2346 1.1 christos { 2347 1.1 christos unsigned int insn; 2348 1.1 christos const struct sh_opcode *op; 2349 1.1 christos unsigned int prev_insn = 0; 2350 1.1 christos const struct sh_opcode *prev_op = NULL; 2351 1.1 christos 2352 1.1 christos insn = bfd_get_16 (abfd, contents + i); 2353 1.1 christos op = sh_insn_info (insn); 2354 1.1 christos if (op == NULL 2355 1.1 christos || (op->flags & (LOAD | STORE)) == 0) 2356 1.1 christos continue; 2357 1.1 christos 2358 1.1 christos /* This is a load or store which is not on a four byte boundary. */ 2359 1.1 christos 2360 1.1 christos while (*plabel < label_end && **plabel < i) 2361 1.1 christos ++*plabel; 2362 1.1 christos 2363 1.1 christos if (i > start) 2364 1.1 christos { 2365 1.1 christos prev_insn = bfd_get_16 (abfd, contents + i - 2); 2366 1.1 christos /* If INSN is the field b of a parallel processing insn, it is not 2367 1.1 christos a load / store after all. Note that the test here might mistake 2368 1.1 christos the field_b of a pcopy insn for the starting code of a parallel 2369 1.1 christos processing insn; this might miss a swapping opportunity, but at 2370 1.1 christos least we're on the safe side. */ 2371 1.1 christos if (dsp && (prev_insn & 0xfc00) == 0xf800) 2372 1.1 christos continue; 2373 1.1 christos 2374 1.1 christos /* Check if prev_insn is actually the field b of a parallel 2375 1.1 christos processing insn. Again, this can give a spurious match 2376 1.1 christos after a pcopy. */ 2377 1.1 christos if (dsp && i - 2 > start) 2378 1.1 christos { 2379 1.1 christos unsigned pprev_insn = bfd_get_16 (abfd, contents + i - 4); 2380 1.1 christos 2381 1.1 christos if ((pprev_insn & 0xfc00) == 0xf800) 2382 1.1 christos prev_op = NULL; 2383 1.1 christos else 2384 1.1 christos prev_op = sh_insn_info (prev_insn); 2385 1.1 christos } 2386 1.1 christos else 2387 1.1 christos prev_op = sh_insn_info (prev_insn); 2388 1.1 christos 2389 1.1 christos /* If the load/store instruction is in a delay slot, we 2390 1.1 christos can't swap. */ 2391 1.1 christos if (prev_op == NULL 2392 1.1 christos || (prev_op->flags & DELAY) != 0) 2393 1.1 christos continue; 2394 1.1 christos } 2395 1.1 christos if (i > start 2396 1.1 christos && (*plabel >= label_end || **plabel != i) 2397 1.1 christos && prev_op != NULL 2398 1.1 christos && (prev_op->flags & (LOAD | STORE)) == 0 2399 1.1.1.8 christos && ! sh_insns_conflict (prev_insn, prev_op, insn, op)) 2400 1.1 christos { 2401 1.1 christos bool ok; 2402 1.1 christos 2403 1.1 christos /* The load/store instruction does not have a label, and 2404 1.1 christos there is a previous instruction; PREV_INSN is not 2405 1.1 christos itself a load/store instruction, and PREV_INSN and 2406 1.1.1.8 christos INSN do not conflict. */ 2407 1.1 christos 2408 1.1 christos ok = true; 2409 1.1 christos 2410 1.1 christos if (i >= start + 4) 2411 1.1 christos { 2412 1.1 christos unsigned int prev2_insn; 2413 1.1 christos const struct sh_opcode *prev2_op; 2414 1.1 christos 2415 1.1 christos prev2_insn = bfd_get_16 (abfd, contents + i - 4); 2416 1.1 christos prev2_op = sh_insn_info (prev2_insn); 2417 1.1 christos 2418 1.1 christos /* If the instruction before PREV_INSN has a delay 2419 1.1 christos slot--that is, PREV_INSN is in a delay slot--we 2420 1.1 christos can not swap. */ 2421 1.1.1.8 christos if (prev2_op == NULL 2422 1.1 christos || (prev2_op->flags & DELAY) != 0) 2423 1.1 christos ok = false; 2424 1.1 christos 2425 1.1 christos /* If the instruction before PREV_INSN is a load, 2426 1.1 christos and it sets a register which INSN uses, then 2427 1.1 christos putting INSN immediately after PREV_INSN will 2428 1.1 christos cause a pipeline bubble, so there is no point to 2429 1.1 christos making the swap. */ 2430 1.1 christos if (ok 2431 1.1.1.8 christos && (prev2_op->flags & LOAD) != 0 2432 1.1 christos && sh_load_use (prev2_insn, prev2_op, insn, op)) 2433 1.1 christos ok = false; 2434 1.1 christos } 2435 1.1 christos 2436 1.1 christos if (ok) 2437 1.1.1.8 christos { 2438 1.1.1.8 christos if (! (*swap) (abfd, sec, relocs, contents, i - 2)) 2439 1.1 christos return false; 2440 1.1 christos *pswapped = true; 2441 1.1 christos continue; 2442 1.1 christos } 2443 1.1 christos } 2444 1.1 christos 2445 1.1 christos while (*plabel < label_end && **plabel < i + 2) 2446 1.1 christos ++*plabel; 2447 1.1 christos 2448 1.1 christos if (i + 2 < stop 2449 1.1 christos && (*plabel >= label_end || **plabel != i + 2)) 2450 1.1 christos { 2451 1.1 christos unsigned int next_insn; 2452 1.1 christos const struct sh_opcode *next_op; 2453 1.1 christos 2454 1.1 christos /* There is an instruction after the load/store 2455 1.1 christos instruction, and it does not have a label. */ 2456 1.1 christos next_insn = bfd_get_16 (abfd, contents + i + 2); 2457 1.1 christos next_op = sh_insn_info (next_insn); 2458 1.1 christos if (next_op != NULL 2459 1.1 christos && (next_op->flags & (LOAD | STORE)) == 0 2460 1.1.1.8 christos && ! sh_insns_conflict (insn, op, next_insn, next_op)) 2461 1.1 christos { 2462 1.1 christos bool ok; 2463 1.1 christos 2464 1.1 christos /* NEXT_INSN is not itself a load/store instruction, 2465 1.1.1.8 christos and it does not conflict with INSN. */ 2466 1.1 christos 2467 1.1 christos ok = true; 2468 1.1 christos 2469 1.1 christos /* If PREV_INSN is a load, and it sets a register 2470 1.1 christos which NEXT_INSN uses, then putting NEXT_INSN 2471 1.1 christos immediately after PREV_INSN will cause a pipeline 2472 1.1 christos bubble, so there is no reason to make this swap. */ 2473 1.1 christos if (prev_op != NULL 2474 1.1.1.8 christos && (prev_op->flags & LOAD) != 0 2475 1.1 christos && sh_load_use (prev_insn, prev_op, next_insn, next_op)) 2476 1.1 christos ok = false; 2477 1.1 christos 2478 1.1 christos /* If INSN is a load, and it sets a register which 2479 1.1 christos the insn after NEXT_INSN uses, then doing the 2480 1.1 christos swap will cause a pipeline bubble, so there is no 2481 1.1 christos reason to make the swap. However, if the insn 2482 1.1 christos after NEXT_INSN is itself a load or store 2483 1.1 christos instruction, then it is misaligned, so 2484 1.1 christos optimistically hope that it will be swapped 2485 1.1 christos itself, and just live with the pipeline bubble if 2486 1.1 christos it isn't. */ 2487 1.1 christos if (ok 2488 1.1 christos && i + 4 < stop 2489 1.1 christos && (op->flags & LOAD) != 0) 2490 1.1 christos { 2491 1.1 christos unsigned int next2_insn; 2492 1.1 christos const struct sh_opcode *next2_op; 2493 1.1 christos 2494 1.1 christos next2_insn = bfd_get_16 (abfd, contents + i + 4); 2495 1.1 christos next2_op = sh_insn_info (next2_insn); 2496 1.1 christos if (next2_op == NULL 2497 1.1.1.8 christos || ((next2_op->flags & (LOAD | STORE)) == 0 2498 1.1 christos && sh_load_use (insn, op, next2_insn, next2_op))) 2499 1.1 christos ok = false; 2500 1.1 christos } 2501 1.1 christos 2502 1.1 christos if (ok) 2503 1.1.1.8 christos { 2504 1.1.1.8 christos if (! (*swap) (abfd, sec, relocs, contents, i)) 2505 1.1 christos return false; 2506 1.1 christos *pswapped = true; 2507 1.1 christos continue; 2508 1.1 christos } 2509 1.1 christos } 2510 1.1 christos } 2511 1.1.1.8 christos } 2512 1.1 christos 2513 1.1 christos return true; 2514 1.1 christos } 2515 1.1 christos #endif /* not COFF_IMAGE_WITH_PE */ 2516 1.1 christos 2517 1.1.1.8 christos /* Swap two SH instructions. */ 2518 1.1.1.2 christos 2519 1.1.1.2 christos static bool 2520 1.1.1.2 christos sh_swap_insns (bfd * abfd, 2521 1.1.1.2 christos asection * sec, 2522 1.1.1.2 christos void * relocs, 2523 1.1 christos bfd_byte * contents, 2524 1.1 christos bfd_vma addr) 2525 1.1 christos { 2526 1.1 christos struct internal_reloc *internal_relocs = (struct internal_reloc *) relocs; 2527 1.1 christos unsigned short i1, i2; 2528 1.1 christos struct internal_reloc *irel, *irelend; 2529 1.1 christos 2530 1.1 christos /* Swap the instructions themselves. */ 2531 1.1 christos i1 = bfd_get_16 (abfd, contents + addr); 2532 1.1 christos i2 = bfd_get_16 (abfd, contents + addr + 2); 2533 1.1 christos bfd_put_16 (abfd, (bfd_vma) i2, contents + addr); 2534 1.1 christos bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2); 2535 1.1 christos 2536 1.1 christos /* Adjust all reloc addresses. */ 2537 1.1 christos irelend = internal_relocs + sec->reloc_count; 2538 1.1 christos for (irel = internal_relocs; irel < irelend; irel++) 2539 1.1 christos { 2540 1.1 christos int type, add; 2541 1.1.1.6 christos 2542 1.1.1.6 christos /* There are a few special types of relocs that we don't want to 2543 1.1 christos adjust. These relocs do not apply to the instruction itself, 2544 1.1 christos but are only associated with the address. */ 2545 1.1 christos type = irel->r_type; 2546 1.1 christos if (type == R_SH_ALIGN 2547 1.1 christos || type == R_SH_CODE 2548 1.1 christos || type == R_SH_DATA 2549 1.1 christos || type == R_SH_LABEL) 2550 1.1 christos continue; 2551 1.1.1.6 christos 2552 1.1.1.6 christos /* If an R_SH_USES reloc points to one of the addresses being 2553 1.1.1.6 christos swapped, we must adjust it. It would be incorrect to do this 2554 1.1.1.6 christos for a jump, though, since we want to execute both 2555 1.1.1.6 christos instructions after the jump. (We have avoided swapping 2556 1.1 christos around a label, so the jump will not wind up executing an 2557 1.1 christos instruction it shouldn't). */ 2558 1.1 christos if (type == R_SH_USES) 2559 1.1 christos { 2560 1.1 christos bfd_vma off; 2561 1.1 christos 2562 1.1 christos off = irel->r_vaddr - sec->vma + 4 + irel->r_offset; 2563 1.1 christos if (off == addr) 2564 1.1 christos irel->r_offset += 2; 2565 1.1 christos else if (off == addr + 2) 2566 1.1 christos irel->r_offset -= 2; 2567 1.1 christos } 2568 1.1 christos 2569 1.1 christos if (irel->r_vaddr - sec->vma == addr) 2570 1.1 christos { 2571 1.1 christos irel->r_vaddr += 2; 2572 1.1 christos add = -2; 2573 1.1 christos } 2574 1.1 christos else if (irel->r_vaddr - sec->vma == addr + 2) 2575 1.1 christos { 2576 1.1 christos irel->r_vaddr -= 2; 2577 1.1 christos add = 2; 2578 1.1 christos } 2579 1.1 christos else 2580 1.1 christos add = 0; 2581 1.1 christos 2582 1.1 christos if (add != 0) 2583 1.1 christos { 2584 1.1.1.8 christos bfd_byte *loc; 2585 1.1 christos unsigned short insn, oinsn; 2586 1.1 christos bool overflow; 2587 1.1.1.8 christos 2588 1.1 christos loc = contents + irel->r_vaddr - sec->vma; 2589 1.1 christos overflow = false; 2590 1.1 christos switch (type) 2591 1.1 christos { 2592 1.1 christos default: 2593 1.1 christos break; 2594 1.1 christos 2595 1.1 christos case R_SH_PCDISP8BY2: 2596 1.1 christos case R_SH_PCRELIMM8BY2: 2597 1.1 christos insn = bfd_get_16 (abfd, loc); 2598 1.1 christos oinsn = insn; 2599 1.1.1.8 christos insn += add / 2; 2600 1.1 christos if ((oinsn & 0xff00) != (insn & 0xff00)) 2601 1.1 christos overflow = true; 2602 1.1 christos bfd_put_16 (abfd, (bfd_vma) insn, loc); 2603 1.1 christos break; 2604 1.1 christos 2605 1.1 christos case R_SH_PCDISP: 2606 1.1 christos insn = bfd_get_16 (abfd, loc); 2607 1.1 christos oinsn = insn; 2608 1.1.1.8 christos insn += add / 2; 2609 1.1 christos if ((oinsn & 0xf000) != (insn & 0xf000)) 2610 1.1 christos overflow = true; 2611 1.1 christos bfd_put_16 (abfd, (bfd_vma) insn, loc); 2612 1.1 christos break; 2613 1.1 christos 2614 1.1.1.6 christos case R_SH_PCRELIMM8BY4: 2615 1.1.1.6 christos /* This reloc ignores the least significant 3 bits of 2616 1.1.1.6 christos the program counter before adding in the offset. 2617 1.1.1.6 christos This means that if ADDR is at an even address, the 2618 1.1.1.6 christos swap will not affect the offset. If ADDR is an at an 2619 1.1 christos odd address, then the instruction will be crossing a 2620 1.1 christos four byte boundary, and must be adjusted. */ 2621 1.1 christos if ((addr & 3) != 0) 2622 1.1 christos { 2623 1.1 christos insn = bfd_get_16 (abfd, loc); 2624 1.1 christos oinsn = insn; 2625 1.1.1.8 christos insn += add / 2; 2626 1.1 christos if ((oinsn & 0xff00) != (insn & 0xff00)) 2627 1.1 christos overflow = true; 2628 1.1 christos bfd_put_16 (abfd, (bfd_vma) insn, loc); 2629 1.1 christos } 2630 1.1 christos 2631 1.1 christos break; 2632 1.1 christos } 2633 1.1 christos 2634 1.1.1.5 christos if (overflow) 2635 1.1.1.5 christos { 2636 1.1.1.6 christos _bfd_error_handler 2637 1.1.1.6 christos /* xgettext: c-format */ 2638 1.1 christos (_("%pB: %#" PRIx64 ": fatal: reloc overflow while relaxing"), 2639 1.1.1.8 christos abfd, (uint64_t) irel->r_vaddr); 2640 1.1 christos bfd_set_error (bfd_error_bad_value); 2641 1.1 christos return false; 2642 1.1 christos } 2643 1.1 christos } 2644 1.1.1.8 christos } 2645 1.1 christos 2646 1.1.1.2 christos return true; 2647 1.1.1.2 christos } 2648 1.1.1.2 christos 2649 1.1.1.2 christos /* Look for loads and stores which we can align to four byte 2650 1.1.1.2 christos boundaries. See the longer comment above sh_relax_section for why 2651 1.1.1.2 christos this is desirable. This sets *PSWAPPED if some instruction was 2652 1.1.1.8 christos swapped. */ 2653 1.1.1.2 christos 2654 1.1.1.2 christos static bool 2655 1.1.1.2 christos sh_align_loads (bfd *abfd, 2656 1.1.1.2 christos asection *sec, 2657 1.1.1.8 christos struct internal_reloc *internal_relocs, 2658 1.1.1.2 christos bfd_byte *contents, 2659 1.1.1.2 christos bool *pswapped) 2660 1.1.1.2 christos { 2661 1.1.1.2 christos struct internal_reloc *irel, *irelend; 2662 1.1.1.2 christos bfd_vma *labels = NULL; 2663 1.1.1.2 christos bfd_vma *label, *label_end; 2664 1.1.1.8 christos bfd_size_type amt; 2665 1.1.1.2 christos 2666 1.1.1.2 christos *pswapped = false; 2667 1.1.1.2 christos 2668 1.1.1.2 christos irelend = internal_relocs + sec->reloc_count; 2669 1.1.1.2 christos 2670 1.1.1.2 christos /* Get all the addresses with labels on them. */ 2671 1.1.1.2 christos amt = (bfd_size_type) sec->reloc_count * sizeof (bfd_vma); 2672 1.1.1.2 christos labels = (bfd_vma *) bfd_malloc (amt); 2673 1.1.1.2 christos if (labels == NULL) 2674 1.1.1.2 christos goto error_return; 2675 1.1.1.2 christos label_end = labels; 2676 1.1.1.2 christos for (irel = internal_relocs; irel < irelend; irel++) 2677 1.1.1.2 christos { 2678 1.1.1.2 christos if (irel->r_type == R_SH_LABEL) 2679 1.1.1.2 christos { 2680 1.1.1.2 christos *label_end = irel->r_vaddr - sec->vma; 2681 1.1.1.2 christos ++label_end; 2682 1.1.1.2 christos } 2683 1.1.1.2 christos } 2684 1.1.1.2 christos 2685 1.1.1.2 christos /* Note that the assembler currently always outputs relocs in 2686 1.1.1.2 christos address order. If that ever changes, this code will need to sort 2687 1.1.1.2 christos the label values and the relocs. */ 2688 1.1.1.2 christos 2689 1.1.1.2 christos label = labels; 2690 1.1.1.2 christos 2691 1.1.1.2 christos for (irel = internal_relocs; irel < irelend; irel++) 2692 1.1.1.2 christos { 2693 1.1.1.2 christos bfd_vma start, stop; 2694 1.1.1.2 christos 2695 1.1.1.2 christos if (irel->r_type != R_SH_CODE) 2696 1.1.1.2 christos continue; 2697 1.1.1.2 christos 2698 1.1.1.2 christos start = irel->r_vaddr - sec->vma; 2699 1.1.1.2 christos 2700 1.1.1.2 christos for (irel++; irel < irelend; irel++) 2701 1.1.1.2 christos if (irel->r_type == R_SH_DATA) 2702 1.1.1.2 christos break; 2703 1.1.1.2 christos if (irel < irelend) 2704 1.1.1.2 christos stop = irel->r_vaddr - sec->vma; 2705 1.1.1.2 christos else 2706 1.1.1.2 christos stop = sec->size; 2707 1.1.1.2 christos 2708 1.1.1.2 christos if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_swap_insns, 2709 1.1.1.2 christos internal_relocs, &label, 2710 1.1.1.2 christos label_end, start, stop, pswapped)) 2711 1.1.1.2 christos goto error_return; 2712 1.1.1.2 christos } 2713 1.1.1.2 christos 2714 1.1.1.8 christos free (labels); 2715 1.1.1.2 christos 2716 1.1.1.2 christos return true; 2717 1.1.1.7 christos 2718 1.1.1.8 christos error_return: 2719 1.1.1.2 christos free (labels); 2720 1.1 christos return false; 2721 1.1 christos } 2722 1.1 christos 2723 1.1 christos /* This is a modification of _bfd_coff_generic_relocate_section, which 2725 1.1.1.2 christos will handle SH relaxing. */ 2726 1.1.1.2 christos 2727 1.1.1.2 christos static bool 2728 1.1.1.2 christos sh_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED, 2729 1.1.1.2 christos struct bfd_link_info *info, 2730 1.1.1.2 christos bfd *input_bfd, 2731 1.1.1.2 christos asection *input_section, 2732 1.1.1.2 christos bfd_byte *contents, 2733 1.1 christos struct internal_reloc *relocs, 2734 1.1 christos struct internal_syment *syms, 2735 1.1 christos asection **sections) 2736 1.1 christos { 2737 1.1 christos struct internal_reloc *rel; 2738 1.1 christos struct internal_reloc *relend; 2739 1.1 christos 2740 1.1 christos rel = relocs; 2741 1.1 christos relend = rel + input_section->reloc_count; 2742 1.1 christos for (; rel < relend; rel++) 2743 1.1 christos { 2744 1.1 christos long symndx; 2745 1.1 christos struct coff_link_hash_entry *h; 2746 1.1 christos struct internal_syment *sym; 2747 1.1 christos bfd_vma addend; 2748 1.1 christos bfd_vma val; 2749 1.1 christos reloc_howto_type *howto; 2750 1.1.1.6 christos bfd_reloc_status_type rstat; 2751 1.1 christos 2752 1.1 christos /* Almost all relocs have to do with relaxing. If any work must 2753 1.1 christos be done for them, it has been done in sh_relax_section. */ 2754 1.1 christos if (rel->r_type != R_SH_IMM32 2755 1.1 christos #ifdef COFF_WITH_PE 2756 1.1 christos && rel->r_type != R_SH_IMM32CE 2757 1.1 christos && rel->r_type != R_SH_IMAGEBASE 2758 1.1 christos #endif 2759 1.1 christos && rel->r_type != R_SH_PCDISP) 2760 1.1 christos continue; 2761 1.1 christos 2762 1.1 christos symndx = rel->r_symndx; 2763 1.1 christos 2764 1.1 christos if (symndx == -1) 2765 1.1 christos { 2766 1.1 christos h = NULL; 2767 1.1 christos sym = NULL; 2768 1.1 christos } 2769 1.1 christos else 2770 1.1 christos { 2771 1.1.1.5 christos if (symndx < 0 2772 1.1.1.5 christos || (unsigned long) symndx >= obj_raw_syment_count (input_bfd)) 2773 1.1.1.6 christos { 2774 1.1 christos _bfd_error_handler 2775 1.1 christos /* xgettext: c-format */ 2776 1.1.1.8 christos (_("%pB: illegal symbol index %ld in relocs"), 2777 1.1 christos input_bfd, symndx); 2778 1.1 christos bfd_set_error (bfd_error_bad_value); 2779 1.1 christos return false; 2780 1.1 christos } 2781 1.1 christos h = obj_coff_sym_hashes (input_bfd)[symndx]; 2782 1.1 christos sym = syms + symndx; 2783 1.1 christos } 2784 1.1 christos 2785 1.1 christos if (sym != NULL && sym->n_scnum != 0) 2786 1.1 christos addend = - sym->n_value; 2787 1.1 christos else 2788 1.1 christos addend = 0; 2789 1.1 christos 2790 1.1 christos if (rel->r_type == R_SH_PCDISP) 2791 1.1 christos addend -= 4; 2792 1.1 christos 2793 1.1 christos if (rel->r_type >= SH_COFF_HOWTO_COUNT) 2794 1.1 christos howto = NULL; 2795 1.1 christos else 2796 1.1 christos howto = &sh_coff_howtos[rel->r_type]; 2797 1.1 christos 2798 1.1.1.8 christos if (howto == NULL) 2799 1.1 christos { 2800 1.1 christos bfd_set_error (bfd_error_bad_value); 2801 1.1 christos return false; 2802 1.1 christos } 2803 1.1 christos 2804 1.1 christos #ifdef COFF_WITH_PE 2805 1.1 christos if (rel->r_type == R_SH_IMAGEBASE) 2806 1.1 christos addend -= pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase; 2807 1.1 christos #endif 2808 1.1 christos 2809 1.1 christos val = 0; 2810 1.1 christos 2811 1.1 christos if (h == NULL) 2812 1.1 christos { 2813 1.1 christos asection *sec; 2814 1.1 christos 2815 1.1 christos /* There is nothing to do for an internal PCDISP reloc. */ 2816 1.1 christos if (rel->r_type == R_SH_PCDISP) 2817 1.1 christos continue; 2818 1.1 christos 2819 1.1 christos if (symndx == -1) 2820 1.1 christos { 2821 1.1 christos sec = bfd_abs_section_ptr; 2822 1.1 christos val = 0; 2823 1.1 christos } 2824 1.1.1.6 christos else 2825 1.1 christos { 2826 1.1 christos sec = sections[symndx]; 2827 1.1 christos val = (sec->output_section->vma 2828 1.1 christos + sec->output_offset 2829 1.1 christos + sym->n_value 2830 1.1 christos - sec->vma); 2831 1.1 christos } 2832 1.1 christos } 2833 1.1 christos else 2834 1.1 christos { 2835 1.1 christos if (h->root.type == bfd_link_hash_defined 2836 1.1 christos || h->root.type == bfd_link_hash_defweak) 2837 1.1 christos { 2838 1.1 christos asection *sec; 2839 1.1 christos 2840 1.1 christos sec = h->root.u.def.section; 2841 1.1 christos val = (h->root.u.def.value 2842 1.1.1.4 christos + sec->output_section->vma 2843 1.1.1.4 christos + sec->output_offset); 2844 1.1.1.4 christos } 2845 1.1.1.8 christos else if (! bfd_link_relocatable (info)) 2846 1.1 christos (*info->callbacks->undefined_symbol) 2847 1.1 christos (info, h->root.root.string, input_bfd, input_section, 2848 1.1 christos rel->r_vaddr - input_section->vma, true); 2849 1.1 christos } 2850 1.1 christos 2851 1.1 christos rstat = _bfd_final_link_relocate (howto, input_bfd, input_section, 2852 1.1 christos contents, 2853 1.1 christos rel->r_vaddr - input_section->vma, 2854 1.1 christos val, addend); 2855 1.1 christos 2856 1.1 christos switch (rstat) 2857 1.1 christos { 2858 1.1 christos default: 2859 1.1 christos abort (); 2860 1.1 christos case bfd_reloc_ok: 2861 1.1 christos break; 2862 1.1 christos case bfd_reloc_overflow: 2863 1.1 christos { 2864 1.1 christos const char *name; 2865 1.1 christos char buf[SYMNMLEN + 1]; 2866 1.1 christos 2867 1.1 christos if (symndx == -1) 2868 1.1 christos name = "*ABS*"; 2869 1.1 christos else if (h != NULL) 2870 1.1.1.9 christos name = NULL; 2871 1.1.1.9 christos else if (sym->_n._n_n._n_zeroes == 0 2872 1.1.1.9 christos && sym->_n._n_n._n_offset != 0) 2873 1.1.1.9 christos { 2874 1.1.1.9 christos if (sym->_n._n_n._n_offset < obj_coff_strings_len (input_bfd)) 2875 1.1.1.9 christos name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset; 2876 1.1 christos else 2877 1.1 christos name = "?"; 2878 1.1.1.6 christos } 2879 1.1 christos else 2880 1.1 christos { 2881 1.1 christos strncpy (buf, sym->_n._n_name, SYMNMLEN); 2882 1.1 christos buf[SYMNMLEN] = '\0'; 2883 1.1.1.4 christos name = buf; 2884 1.1.1.4 christos } 2885 1.1.1.4 christos 2886 1.1.1.4 christos (*info->callbacks->reloc_overflow) 2887 1.1 christos (info, (h ? &h->root : NULL), name, howto->name, 2888 1.1 christos (bfd_vma) 0, input_bfd, input_section, 2889 1.1 christos rel->r_vaddr - input_section->vma); 2890 1.1 christos } 2891 1.1.1.8 christos } 2892 1.1 christos } 2893 1.1 christos 2894 1.1 christos return true; 2895 1.1 christos } 2896 1.1 christos 2897 1.1 christos /* This is a version of bfd_generic_get_relocated_section_contents 2898 1.1.1.2 christos which uses sh_relocate_section. */ 2899 1.1.1.2 christos 2900 1.1.1.2 christos static bfd_byte * 2901 1.1.1.2 christos sh_coff_get_relocated_section_contents (bfd *output_bfd, 2902 1.1.1.8 christos struct bfd_link_info *link_info, 2903 1.1.1.2 christos struct bfd_link_order *link_order, 2904 1.1 christos bfd_byte *data, 2905 1.1 christos bool relocatable, 2906 1.1 christos asymbol **symbols) 2907 1.1 christos { 2908 1.1 christos asection *input_section = link_order->u.indirect.section; 2909 1.1 christos bfd *input_bfd = input_section->owner; 2910 1.1 christos asection **sections = NULL; 2911 1.1 christos struct internal_reloc *internal_relocs = NULL; 2912 1.1 christos struct internal_syment *internal_syms = NULL; 2913 1.1 christos 2914 1.1 christos /* We only need to handle the case of relaxing, or of having a 2915 1.1 christos particular set of section contents, specially. */ 2916 1.1 christos if (relocatable 2917 1.1 christos || coff_section_data (input_bfd, input_section) == NULL 2918 1.1 christos || coff_section_data (input_bfd, input_section)->contents == NULL) 2919 1.1 christos return bfd_generic_get_relocated_section_contents (output_bfd, link_info, 2920 1.1 christos link_order, data, 2921 1.1.1.8 christos relocatable, 2922 1.1.1.8 christos symbols); 2923 1.1.1.8 christos 2924 1.1.1.8 christos bfd_byte *orig_data = data; 2925 1.1.1.8 christos if (data == NULL) 2926 1.1.1.8 christos { 2927 1.1.1.8 christos data = bfd_malloc (input_section->size); 2928 1.1 christos if (data == NULL) 2929 1.1 christos return NULL; 2930 1.1 christos } 2931 1.1 christos memcpy (data, coff_section_data (input_bfd, input_section)->contents, 2932 1.1 christos (size_t) input_section->size); 2933 1.1 christos 2934 1.1 christos if ((input_section->flags & SEC_RELOC) != 0 2935 1.1 christos && input_section->reloc_count > 0) 2936 1.1 christos { 2937 1.1 christos bfd_size_type symesz = bfd_coff_symesz (input_bfd); 2938 1.1 christos bfd_byte *esym, *esymend; 2939 1.1 christos struct internal_syment *isymp; 2940 1.1 christos asection **secpp; 2941 1.1 christos bfd_size_type amt; 2942 1.1 christos 2943 1.1 christos if (! _bfd_coff_get_external_symbols (input_bfd)) 2944 1.1.1.8 christos goto error_return; 2945 1.1.1.8 christos 2946 1.1 christos internal_relocs = (_bfd_coff_read_internal_relocs 2947 1.1 christos (input_bfd, input_section, false, (bfd_byte *) NULL, 2948 1.1 christos false, (struct internal_reloc *) NULL)); 2949 1.1 christos if (internal_relocs == NULL) 2950 1.1 christos goto error_return; 2951 1.1 christos 2952 1.1 christos amt = obj_raw_syment_count (input_bfd); 2953 1.1 christos amt *= sizeof (struct internal_syment); 2954 1.1 christos internal_syms = (struct internal_syment *) bfd_malloc (amt); 2955 1.1 christos if (internal_syms == NULL) 2956 1.1 christos goto error_return; 2957 1.1 christos 2958 1.1 christos amt = obj_raw_syment_count (input_bfd); 2959 1.1 christos amt *= sizeof (asection *); 2960 1.1 christos sections = (asection **) bfd_malloc (amt); 2961 1.1 christos if (sections == NULL) 2962 1.1 christos goto error_return; 2963 1.1 christos 2964 1.1 christos isymp = internal_syms; 2965 1.1 christos secpp = sections; 2966 1.1 christos esym = (bfd_byte *) obj_coff_external_syms (input_bfd); 2967 1.1.1.2 christos esymend = esym + obj_raw_syment_count (input_bfd) * symesz; 2968 1.1 christos while (esym < esymend) 2969 1.1 christos { 2970 1.1 christos bfd_coff_swap_sym_in (input_bfd, esym, isymp); 2971 1.1 christos 2972 1.1 christos if (isymp->n_scnum != 0) 2973 1.1 christos *secpp = coff_section_from_bfd_index (input_bfd, isymp->n_scnum); 2974 1.1 christos else 2975 1.1 christos { 2976 1.1 christos if (isymp->n_value == 0) 2977 1.1 christos *secpp = bfd_und_section_ptr; 2978 1.1 christos else 2979 1.1 christos *secpp = bfd_com_section_ptr; 2980 1.1 christos } 2981 1.1 christos 2982 1.1 christos esym += (isymp->n_numaux + 1) * symesz; 2983 1.1 christos secpp += isymp->n_numaux + 1; 2984 1.1 christos isymp += isymp->n_numaux + 1; 2985 1.1 christos } 2986 1.1 christos 2987 1.1 christos if (! sh_relocate_section (output_bfd, link_info, input_bfd, 2988 1.1 christos input_section, data, internal_relocs, 2989 1.1 christos internal_syms, sections)) 2990 1.1 christos goto error_return; 2991 1.1 christos 2992 1.1 christos free (sections); 2993 1.1 christos sections = NULL; 2994 1.1 christos free (internal_syms); 2995 1.1 christos internal_syms = NULL; 2996 1.1 christos free (internal_relocs); 2997 1.1 christos internal_relocs = NULL; 2998 1.1 christos } 2999 1.1 christos 3000 1.1.1.7 christos return data; 3001 1.1.1.7 christos 3002 1.1.1.7 christos error_return: 3003 1.1.1.8 christos free (internal_relocs); 3004 1.1.1.8 christos free (internal_syms); 3005 1.1 christos free (sections); 3006 1.1 christos if (orig_data == NULL) 3007 1.1 christos free (data); 3008 1.1 christos return NULL; 3009 1.1 christos } 3010 1.1 christos 3011 1.1.1.3 christos /* The target vectors. */ 3012 1.1 christos 3013 1.1 christos #ifndef TARGET_SHL_SYM 3014 1.1 christos CREATE_BIG_COFF_TARGET_VEC (sh_coff_vec, "coff-sh", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE) 3015 1.1 christos #endif 3016 1.1 christos 3017 1.1.1.3 christos #ifdef TARGET_SHL_SYM 3018 1.1 christos #define TARGET_SYM TARGET_SHL_SYM 3019 1.1 christos #else 3020 1.1 christos #define TARGET_SYM sh_coff_le_vec 3021 1.1 christos #endif 3022 1.1 christos 3023 1.1 christos #ifndef TARGET_SHL_NAME 3024 1.1 christos #define TARGET_SHL_NAME "coff-shl" 3025 1.1 christos #endif 3026 1.1 christos 3027 1.1 christos #ifdef COFF_WITH_PE 3028 1.1 christos CREATE_LITTLE_COFF_TARGET_VEC (TARGET_SYM, TARGET_SHL_NAME, BFD_IS_RELAXABLE, 3029 1.1 christos SEC_CODE | SEC_DATA, '_', NULL, COFF_SWAP_TABLE); 3030 1.1 christos #else 3031 1.1 christos CREATE_LITTLE_COFF_TARGET_VEC (TARGET_SYM, TARGET_SHL_NAME, BFD_IS_RELAXABLE, 3032 1.1 christos 0, '_', NULL, COFF_SWAP_TABLE) 3033 1.1.1.2 christos #endif 3034 1.1 christos 3035 1.1 christos #ifndef TARGET_SHL_SYM 3036 1.1 christos 3037 1.1 christos /* Some people want versions of the SH COFF target which do not align 3038 1.1 christos to 16 byte boundaries. We implement that by adding a couple of new 3039 1.1 christos target vectors. These are just like the ones above, but they 3040 1.1 christos change the default section alignment. To generate them in the 3041 1.1 christos assembler, use -small. To use them in the linker, use -b 3042 1.1 christos coff-sh{l}-small and -oformat coff-sh{l}-small. 3043 1.1 christos 3044 1.1 christos Yes, this is a horrible hack. A general solution for setting 3045 1.1 christos section alignment in COFF is rather complex. ELF handles this 3046 1.1 christos correctly. */ 3047 1.1 christos 3048 1.1.1.7 christos /* Only recognize the small versions if the target was not defaulted. 3049 1.1.1.2 christos Otherwise we won't recognize the non default endianness. */ 3050 1.1 christos 3051 1.1 christos static bfd_cleanup 3052 1.1 christos coff_small_object_p (bfd *abfd) 3053 1.1 christos { 3054 1.1 christos if (abfd->target_defaulted) 3055 1.1 christos { 3056 1.1 christos bfd_set_error (bfd_error_wrong_format); 3057 1.1 christos return NULL; 3058 1.1 christos } 3059 1.1 christos return coff_object_p (abfd); 3060 1.1 christos } 3061 1.1.1.8 christos 3062 1.1.1.2 christos /* Set the section alignment for the small versions. */ 3063 1.1 christos 3064 1.1 christos static bool 3065 1.1.1.8 christos coff_small_new_section_hook (bfd *abfd, asection *section) 3066 1.1 christos { 3067 1.1 christos if (! coff_new_section_hook (abfd, section)) 3068 1.1 christos return false; 3069 1.1 christos 3070 1.1 christos /* We must align to at least a four byte boundary, because longword 3071 1.1 christos accesses must be on a four byte boundary. */ 3072 1.1.1.8 christos if (section->alignment_power == COFF_DEFAULT_SECTION_ALIGNMENT_POWER) 3073 1.1 christos section->alignment_power = 2; 3074 1.1 christos 3075 1.1 christos return true; 3076 1.1 christos } 3077 1.1 christos 3078 1.1.1.9 christos /* This is copied from bfd_coff_std_swap_table so that we can change 3079 1.1 christos the default section alignment power. */ 3080 1.1 christos 3081 1.1 christos static const bfd_coff_backend_data bfd_coff_small_swap_table = 3082 1.1 christos { 3083 1.1 christos coff_swap_aux_in, coff_swap_sym_in, coff_swap_lineno_in, 3084 1.1 christos coff_swap_aux_out, coff_swap_sym_out, 3085 1.1 christos coff_swap_lineno_out, coff_swap_reloc_out, 3086 1.1 christos coff_swap_filehdr_out, coff_swap_aouthdr_out, 3087 1.1.1.8 christos coff_swap_scnhdr_out, 3088 1.1 christos FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN, 3089 1.1.1.8 christos #ifdef COFF_LONG_FILENAMES 3090 1.1 christos true, 3091 1.1 christos #else 3092 1.1 christos false, 3093 1.1 christos #endif 3094 1.1.1.8 christos COFF_DEFAULT_LONG_SECTION_NAMES, 3095 1.1 christos 2, 3096 1.1.1.8 christos #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS 3097 1.1 christos true, 3098 1.1 christos #else 3099 1.1 christos false, 3100 1.1 christos #endif 3101 1.1 christos #ifdef COFF_DEBUG_STRING_WIDE_PREFIX 3102 1.1 christos 4, 3103 1.1.1.3 christos #else 3104 1.1 christos 2, 3105 1.1 christos #endif 3106 1.1 christos 32768, 3107 1.1 christos coff_swap_filehdr_in, coff_swap_aouthdr_in, coff_swap_scnhdr_in, 3108 1.1 christos coff_swap_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook, 3109 1.1 christos coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook, 3110 1.1 christos coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook, 3111 1.1 christos coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate, 3112 1.1 christos coff_classify_symbol, coff_compute_section_file_positions, 3113 1.1 christos coff_start_final_link, coff_relocate_section, coff_rtype_to_howto, 3114 1.1 christos coff_adjust_symndx, coff_link_add_one_symbol, 3115 1.1 christos coff_link_output_has_begun, coff_final_link_postscript, 3116 1.1 christos bfd_pe_print_pdata 3117 1.1 christos }; 3118 1.1 christos 3119 1.1 christos #define coff_small_close_and_cleanup \ 3120 1.1 christos coff_close_and_cleanup 3121 1.1 christos #define coff_small_bfd_free_cached_info \ 3122 1.1 christos coff_bfd_free_cached_info 3123 1.1.1.3 christos #define coff_small_get_section_contents \ 3124 1.1 christos coff_get_section_contents 3125 1.1.1.3 christos 3126 1.1 christos extern const bfd_target sh_coff_small_le_vec; 3127 1.1 christos 3128 1.1 christos const bfd_target sh_coff_small_vec = 3129 1.1 christos { 3130 1.1 christos "coff-sh-small", /* name */ 3131 1.1 christos bfd_target_coff_flavour, 3132 1.1.1.6 christos BFD_ENDIAN_BIG, /* data byte order is big */ 3133 1.1.1.6 christos BFD_ENDIAN_BIG, /* header byte order is big */ 3134 1.1.1.6 christos 3135 1.1 christos (HAS_RELOC | EXEC_P /* object flags */ 3136 1.1 christos | HAS_LINENO | HAS_DEBUG 3137 1.1 christos | HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE), 3138 1.1 christos 3139 1.1 christos (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), 3140 1.1.1.2 christos '_', /* leading symbol underscore */ 3141 1.1.1.8 christos '/', /* ar_pad_char */ 3142 1.1 christos 15, /* ar_max_namelen */ 3143 1.1 christos 0, /* match priority. */ 3144 1.1 christos TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */ 3145 1.1 christos bfd_getb64, bfd_getb_signed_64, bfd_putb64, 3146 1.1 christos bfd_getb32, bfd_getb_signed_32, bfd_putb32, 3147 1.1 christos bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */ 3148 1.1 christos bfd_getb64, bfd_getb_signed_64, bfd_putb64, 3149 1.1.1.6 christos bfd_getb32, bfd_getb_signed_32, bfd_putb32, 3150 1.1.1.6 christos bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */ 3151 1.1.1.6 christos 3152 1.1.1.6 christos { /* bfd_check_format */ 3153 1.1.1.6 christos _bfd_dummy_target, 3154 1.1.1.6 christos coff_small_object_p, 3155 1.1.1.6 christos bfd_generic_archive_p, 3156 1.1.1.6 christos _bfd_dummy_target 3157 1.1.1.6 christos }, 3158 1.1.1.6 christos { /* bfd_set_format */ 3159 1.1.1.6 christos _bfd_bool_bfd_false_error, 3160 1.1.1.6 christos coff_mkobject, 3161 1.1.1.6 christos _bfd_generic_mkarchive, 3162 1.1.1.6 christos _bfd_bool_bfd_false_error 3163 1.1.1.6 christos }, 3164 1.1.1.6 christos { /* bfd_write_contents */ 3165 1.1.1.6 christos _bfd_bool_bfd_false_error, 3166 1.1.1.6 christos coff_write_object_contents, 3167 1.1 christos _bfd_write_archive_contents, 3168 1.1 christos _bfd_bool_bfd_false_error 3169 1.1 christos }, 3170 1.1 christos 3171 1.1 christos BFD_JUMP_TABLE_GENERIC (coff_small), 3172 1.1 christos BFD_JUMP_TABLE_COPY (coff), 3173 1.1 christos BFD_JUMP_TABLE_CORE (_bfd_nocore), 3174 1.1 christos BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), 3175 1.1 christos BFD_JUMP_TABLE_SYMBOLS (coff), 3176 1.1 christos BFD_JUMP_TABLE_RELOCS (coff), 3177 1.1 christos BFD_JUMP_TABLE_WRITE (coff), 3178 1.1.1.6 christos BFD_JUMP_TABLE_LINK (coff), 3179 1.1 christos BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), 3180 1.1.1.6 christos 3181 1.1 christos &sh_coff_small_le_vec, 3182 1.1 christos 3183 1.1.1.3 christos &bfd_coff_small_swap_table 3184 1.1 christos }; 3185 1.1 christos 3186 1.1 christos const bfd_target sh_coff_small_le_vec = 3187 1.1 christos { 3188 1.1 christos "coff-shl-small", /* name */ 3189 1.1 christos bfd_target_coff_flavour, 3190 1.1.1.6 christos BFD_ENDIAN_LITTLE, /* data byte order is little */ 3191 1.1.1.6 christos BFD_ENDIAN_LITTLE, /* header byte order is little endian too*/ 3192 1.1.1.6 christos 3193 1.1 christos (HAS_RELOC | EXEC_P /* object flags */ 3194 1.1 christos | HAS_LINENO | HAS_DEBUG 3195 1.1 christos | HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE), 3196 1.1 christos 3197 1.1 christos (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), 3198 1.1.1.2 christos '_', /* leading symbol underscore */ 3199 1.1.1.8 christos '/', /* ar_pad_char */ 3200 1.1 christos 15, /* ar_max_namelen */ 3201 1.1 christos 0, /* match priority. */ 3202 1.1 christos TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */ 3203 1.1 christos bfd_getl64, bfd_getl_signed_64, bfd_putl64, 3204 1.1 christos bfd_getl32, bfd_getl_signed_32, bfd_putl32, 3205 1.1 christos bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ 3206 1.1 christos bfd_getl64, bfd_getl_signed_64, bfd_putl64, 3207 1.1.1.6 christos bfd_getl32, bfd_getl_signed_32, bfd_putl32, 3208 1.1.1.6 christos bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ 3209 1.1.1.6 christos 3210 1.1.1.6 christos { /* bfd_check_format */ 3211 1.1.1.6 christos _bfd_dummy_target, 3212 1.1.1.6 christos coff_small_object_p, 3213 1.1.1.6 christos bfd_generic_archive_p, 3214 1.1.1.6 christos _bfd_dummy_target 3215 1.1.1.6 christos }, 3216 1.1.1.6 christos { /* bfd_set_format */ 3217 1.1.1.6 christos _bfd_bool_bfd_false_error, 3218 1.1.1.6 christos coff_mkobject, 3219 1.1.1.6 christos _bfd_generic_mkarchive, 3220 1.1.1.6 christos _bfd_bool_bfd_false_error 3221 1.1.1.6 christos }, 3222 1.1.1.6 christos { /* bfd_write_contents */ 3223 1.1.1.6 christos _bfd_bool_bfd_false_error, 3224 1.1.1.6 christos coff_write_object_contents, 3225 1.1 christos _bfd_write_archive_contents, 3226 1.1 christos _bfd_bool_bfd_false_error 3227 1.1 christos }, 3228 1.1 christos 3229 1.1 christos BFD_JUMP_TABLE_GENERIC (coff_small), 3230 1.1 christos BFD_JUMP_TABLE_COPY (coff), 3231 1.1 christos BFD_JUMP_TABLE_CORE (_bfd_nocore), 3232 1.1 christos BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), 3233 1.1 christos BFD_JUMP_TABLE_SYMBOLS (coff), 3234 1.1 christos BFD_JUMP_TABLE_RELOCS (coff), 3235 1.1 christos BFD_JUMP_TABLE_WRITE (coff), 3236 1.1.1.6 christos BFD_JUMP_TABLE_LINK (coff), 3237 1.1 christos BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), 3238 1.1.1.6 christos 3239 1.1 christos &sh_coff_small_vec, 3240 1.1 christos 3241 &bfd_coff_small_swap_table 3242 }; 3243 #endif 3244