| 1 | /* M32R-specific support for 32-bit ELF. |
| 2 | Copyright (C) 1996, 1997 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of BFD, the Binary File Descriptor library. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 19 | |
| 20 | #include "bfd.h" |
| 21 | #include "sysdep.h" |
| 22 | #include "libbfd.h" |
| 23 | #include "elf-bfd.h" |
| 24 | #include "elf/m32r.h" |
| 25 | |
| 26 | static bfd_reloc_status_type m32r_elf_10_pcrel_reloc |
| 27 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); |
| 28 | static bfd_reloc_status_type m32r_elf_do_10_pcrel_reloc |
| 29 | PARAMS ((bfd *, reloc_howto_type *, asection *, |
| 30 | bfd_byte *, bfd_vma, asection *, bfd_vma, bfd_vma)); |
| 31 | static bfd_reloc_status_type m32r_elf_hi16_reloc |
| 32 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); |
| 33 | static void m32r_elf_relocate_hi16 |
| 34 | PARAMS ((bfd *, int, Elf_Internal_Rela *, Elf_Internal_Rela *, |
| 35 | bfd_byte *, bfd_vma)); |
| 36 | bfd_reloc_status_type m32r_elf_lo16_reloc |
| 37 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); |
| 38 | static bfd_reloc_status_type m32r_elf_sda16_reloc |
| 39 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); |
| 40 | static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup |
| 41 | PARAMS ((bfd *abfd, bfd_reloc_code_real_type code)); |
| 42 | static void m32r_info_to_howto_rel |
| 43 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *)); |
| 44 | boolean _bfd_m32r_elf_section_from_bfd_section |
| 45 | PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *)); |
| 46 | void _bfd_m32r_elf_symbol_processing |
| 47 | PARAMS ((bfd *, asymbol *)); |
| 48 | static boolean m32r_elf_add_symbol_hook |
| 49 | PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *, |
| 50 | const char **, flagword *, asection **, bfd_vma *)); |
| 51 | static boolean m32r_elf_relocate_section |
| 52 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
| 53 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); |
| 54 | #if 0 /* not yet */ |
| 55 | static boolean m32r_elf_relax_delete_bytes |
| 56 | PARAMS ((bfd *, asection *, bfd_vma, int)); |
| 57 | #endif |
| 58 | |
| 59 | /* Use REL instead of RELA to save space. |
| 60 | This only saves space in libraries and object files, but perhaps |
| 61 | relocs will be put in ROM? All in all though, REL relocs are a pain |
| 62 | to work with. */ |
| 63 | #define USE_REL |
| 64 | |
| 65 | static reloc_howto_type m32r_elf_howto_table[] = |
| 66 | { |
| 67 | /* This reloc does nothing. */ |
| 68 | HOWTO (R_M32R_NONE, /* type */ |
| 69 | 0, /* rightshift */ |
| 70 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 71 | 32, /* bitsize */ |
| 72 | false, /* pc_relative */ |
| 73 | 0, /* bitpos */ |
| 74 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 75 | bfd_elf_generic_reloc, /* special_function */ |
| 76 | "R_M32R_NONE", /* name */ |
| 77 | false, /* partial_inplace */ |
| 78 | 0, /* src_mask */ |
| 79 | 0, /* dst_mask */ |
| 80 | false), /* pcrel_offset */ |
| 81 | |
| 82 | /* A 16 bit absolute relocation. */ |
| 83 | HOWTO (R_M32R_16, /* type */ |
| 84 | 0, /* rightshift */ |
| 85 | 1, /* size (0 = byte, 1 = short, 2 = long) */ |
| 86 | 16, /* bitsize */ |
| 87 | false, /* pc_relative */ |
| 88 | 0, /* bitpos */ |
| 89 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 90 | bfd_elf_generic_reloc, /* special_function */ |
| 91 | "R_M32R_16", /* name */ |
| 92 | true, /* partial_inplace */ |
| 93 | 0xffff, /* src_mask */ |
| 94 | 0xffff, /* dst_mask */ |
| 95 | false), /* pcrel_offset */ |
| 96 | |
| 97 | /* A 32 bit absolute relocation. */ |
| 98 | HOWTO (R_M32R_32, /* type */ |
| 99 | 0, /* rightshift */ |
| 100 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 101 | 32, /* bitsize */ |
| 102 | false, /* pc_relative */ |
| 103 | 0, /* bitpos */ |
| 104 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 105 | bfd_elf_generic_reloc, /* special_function */ |
| 106 | "R_M32R_32", /* name */ |
| 107 | true, /* partial_inplace */ |
| 108 | 0xffffffff, /* src_mask */ |
| 109 | 0xffffffff, /* dst_mask */ |
| 110 | false), /* pcrel_offset */ |
| 111 | |
| 112 | /* A 24 bit address. */ |
| 113 | HOWTO (R_M32R_24, /* type */ |
| 114 | 0, /* rightshift */ |
| 115 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 116 | 24, /* bitsize */ |
| 117 | false, /* pc_relative */ |
| 118 | 0, /* bitpos */ |
| 119 | complain_overflow_unsigned, /* complain_on_overflow */ |
| 120 | bfd_elf_generic_reloc, /* special_function */ |
| 121 | "R_M32R_24", /* name */ |
| 122 | true, /* partial_inplace */ |
| 123 | 0xffffff, /* src_mask */ |
| 124 | 0xffffff, /* dst_mask */ |
| 125 | false), /* pcrel_offset */ |
| 126 | |
| 127 | /* An PC Relative 10-bit relocation, shifted by 2. |
| 128 | This reloc is complicated because relocations are relative to pc & -4. |
| 129 | i.e. branches in the right insn slot use the address of the left insn |
| 130 | slot for pc. */ |
| 131 | /* ??? It's not clear whether this should have partial_inplace set or not. |
| 132 | Branch relaxing in the assembler can store the addend in the insn, |
| 133 | and if bfd_install_relocation gets called the addend may get added |
| 134 | again. */ |
| 135 | HOWTO (R_M32R_10_PCREL, /* type */ |
| 136 | 2, /* rightshift */ |
| 137 | 1, /* size (0 = byte, 1 = short, 2 = long) */ |
| 138 | 10, /* bitsize */ |
| 139 | true, /* pc_relative */ |
| 140 | 0, /* bitpos */ |
| 141 | complain_overflow_signed, /* complain_on_overflow */ |
| 142 | m32r_elf_10_pcrel_reloc, /* special_function */ |
| 143 | "R_M32R_10_PCREL", /* name */ |
| 144 | false, /* partial_inplace */ |
| 145 | 0xff, /* src_mask */ |
| 146 | 0xff, /* dst_mask */ |
| 147 | true), /* pcrel_offset */ |
| 148 | |
| 149 | /* A relative 18 bit relocation, right shifted by 2. */ |
| 150 | HOWTO (R_M32R_18_PCREL, /* type */ |
| 151 | 2, /* rightshift */ |
| 152 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 153 | 18, /* bitsize */ |
| 154 | true, /* pc_relative */ |
| 155 | 0, /* bitpos */ |
| 156 | complain_overflow_signed, /* complain_on_overflow */ |
| 157 | bfd_elf_generic_reloc, /* special_function */ |
| 158 | "R_M32R_18_PCREL", /* name */ |
| 159 | false, /* partial_inplace */ |
| 160 | 0xffff, /* src_mask */ |
| 161 | 0xffff, /* dst_mask */ |
| 162 | true), /* pcrel_offset */ |
| 163 | |
| 164 | /* A relative 26 bit relocation, right shifted by 2. */ |
| 165 | /* ??? It's not clear whether this should have partial_inplace set or not. |
| 166 | Branch relaxing in the assembler can store the addend in the insn, |
| 167 | and if bfd_install_relocation gets called the addend may get added |
| 168 | again. */ |
| 169 | HOWTO (R_M32R_26_PCREL, /* type */ |
| 170 | 2, /* rightshift */ |
| 171 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 172 | 26, /* bitsize */ |
| 173 | true, /* pc_relative */ |
| 174 | 0, /* bitpos */ |
| 175 | complain_overflow_signed, /* complain_on_overflow */ |
| 176 | bfd_elf_generic_reloc, /* special_function */ |
| 177 | "R_M32R_26_PCREL", /* name */ |
| 178 | false, /* partial_inplace */ |
| 179 | 0xffffff, /* src_mask */ |
| 180 | 0xffffff, /* dst_mask */ |
| 181 | true), /* pcrel_offset */ |
| 182 | |
| 183 | /* High 16 bits of address when lower 16 is or'd in. */ |
| 184 | HOWTO (R_M32R_HI16_ULO, /* type */ |
| 185 | 16, /* rightshift */ |
| 186 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 187 | 16, /* bitsize */ |
| 188 | false, /* pc_relative */ |
| 189 | 0, /* bitpos */ |
| 190 | complain_overflow_dont, /* complain_on_overflow */ |
| 191 | m32r_elf_hi16_reloc, /* special_function */ |
| 192 | "R_M32R_HI16_ULO", /* name */ |
| 193 | true, /* partial_inplace */ |
| 194 | 0x0000ffff, /* src_mask */ |
| 195 | 0x0000ffff, /* dst_mask */ |
| 196 | false), /* pcrel_offset */ |
| 197 | |
| 198 | /* High 16 bits of address when lower 16 is added in. */ |
| 199 | HOWTO (R_M32R_HI16_SLO, /* type */ |
| 200 | 16, /* rightshift */ |
| 201 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 202 | 16, /* bitsize */ |
| 203 | false, /* pc_relative */ |
| 204 | 0, /* bitpos */ |
| 205 | complain_overflow_dont, /* complain_on_overflow */ |
| 206 | m32r_elf_hi16_reloc, /* special_function */ |
| 207 | "R_M32R_HI16_SLO", /* name */ |
| 208 | true, /* partial_inplace */ |
| 209 | 0x0000ffff, /* src_mask */ |
| 210 | 0x0000ffff, /* dst_mask */ |
| 211 | false), /* pcrel_offset */ |
| 212 | |
| 213 | /* Lower 16 bits of address. */ |
| 214 | HOWTO (R_M32R_LO16, /* type */ |
| 215 | 0, /* rightshift */ |
| 216 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 217 | 16, /* bitsize */ |
| 218 | false, /* pc_relative */ |
| 219 | 0, /* bitpos */ |
| 220 | complain_overflow_dont, /* complain_on_overflow */ |
| 221 | m32r_elf_lo16_reloc, /* special_function */ |
| 222 | "R_M32R_LO16", /* name */ |
| 223 | true, /* partial_inplace */ |
| 224 | 0x0000ffff, /* src_mask */ |
| 225 | 0x0000ffff, /* dst_mask */ |
| 226 | false), /* pcrel_offset */ |
| 227 | |
| 228 | /* Small data area 16 bits offset. */ |
| 229 | HOWTO (R_M32R_SDA16, /* type */ |
| 230 | 0, /* rightshift */ |
| 231 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 232 | 16, /* bitsize */ |
| 233 | false, /* pc_relative */ |
| 234 | 0, /* bitpos */ |
| 235 | complain_overflow_signed, /* complain_on_overflow */ |
| 236 | m32r_elf_sda16_reloc, /* special_function */ |
| 237 | "R_M32R_SDA16", /* name */ |
| 238 | true, /* partial_inplace */ /* FIXME: correct? */ |
| 239 | 0x0000ffff, /* src_mask */ |
| 240 | 0x0000ffff, /* dst_mask */ |
| 241 | false), /* pcrel_offset */ |
| 242 | }; |
| 243 | \f |
| 244 | /* Handle the R_M32R_10_PCREL reloc. */ |
| 245 | |
| 246 | static bfd_reloc_status_type |
| 247 | m32r_elf_10_pcrel_reloc (abfd, reloc_entry, symbol, data, |
| 248 | input_section, output_bfd, error_message) |
| 249 | bfd *abfd; |
| 250 | arelent *reloc_entry; |
| 251 | asymbol *symbol; |
| 252 | PTR data; |
| 253 | asection *input_section; |
| 254 | bfd *output_bfd; |
| 255 | char **error_message; |
| 256 | { |
| 257 | /* This part is from bfd_elf_generic_reloc. */ |
| 258 | if (output_bfd != (bfd *) NULL |
| 259 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
| 260 | && (! reloc_entry->howto->partial_inplace |
| 261 | || reloc_entry->addend == 0)) |
| 262 | { |
| 263 | reloc_entry->address += input_section->output_offset; |
| 264 | return bfd_reloc_ok; |
| 265 | } |
| 266 | |
| 267 | if (output_bfd != NULL) |
| 268 | { |
| 269 | /* FIXME: See bfd_perform_relocation. Is this right? */ |
| 270 | return bfd_reloc_continue; |
| 271 | } |
| 272 | |
| 273 | return m32r_elf_do_10_pcrel_reloc (abfd, reloc_entry->howto, |
| 274 | input_section, |
| 275 | data, reloc_entry->address, |
| 276 | symbol->section, |
| 277 | (symbol->value |
| 278 | + symbol->section->output_section->vma |
| 279 | + symbol->section->output_offset), |
| 280 | reloc_entry->addend); |
| 281 | } |
| 282 | |
| 283 | /* Utility to actually perform an R_M32R_10_PCREL reloc. */ |
| 284 | |
| 285 | static bfd_reloc_status_type |
| 286 | m32r_elf_do_10_pcrel_reloc (abfd, howto, input_section, data, offset, |
| 287 | symbol_section, symbol_value, addend) |
| 288 | bfd *abfd; |
| 289 | reloc_howto_type *howto; |
| 290 | asection *input_section; |
| 291 | bfd_byte *data; |
| 292 | bfd_vma offset; |
| 293 | asection *symbol_section; |
| 294 | bfd_vma symbol_value; |
| 295 | bfd_vma addend; |
| 296 | { |
| 297 | bfd_signed_vma relocation; |
| 298 | unsigned long x; |
| 299 | bfd_reloc_status_type status; |
| 300 | |
| 301 | /* Sanity check the address (offset in section). */ |
| 302 | if (offset > input_section->_cooked_size) |
| 303 | return bfd_reloc_outofrange; |
| 304 | |
| 305 | relocation = symbol_value + addend; |
| 306 | /* Make it pc relative. */ |
| 307 | relocation -= (input_section->output_section->vma |
| 308 | + input_section->output_offset); |
| 309 | /* These jumps mask off the lower two bits of the current address |
| 310 | before doing pcrel calculations. */ |
| 311 | relocation -= (offset & -4L); |
| 312 | |
| 313 | if (relocation < -0x200 || relocation > 0x1ff) |
| 314 | status = bfd_reloc_overflow; |
| 315 | else |
| 316 | status = bfd_reloc_ok; |
| 317 | |
| 318 | x = bfd_get_16 (abfd, data + offset); |
| 319 | relocation >>= howto->rightshift; |
| 320 | relocation <<= howto->bitpos; |
| 321 | x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask); |
| 322 | bfd_put_16 (abfd, x, data + offset); |
| 323 | |
| 324 | return status; |
| 325 | } |
| 326 | |
| 327 | /* Handle the R_M32R_HI16_[SU]LO relocs. |
| 328 | HI16_SLO is for the add3 and load/store with displacement instructions. |
| 329 | HI16_ULO is for the or3 instruction. |
| 330 | For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to |
| 331 | the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then |
| 332 | we must add one to the high 16 bytes (which will get subtracted off when |
| 333 | the low 16 bits are added). |
| 334 | These relocs have to be done in combination with an R_M32R_LO16 reloc |
| 335 | because there is a carry from the LO16 to the HI16. Here we just save |
| 336 | the information we need; we do the actual relocation when we see the LO16. |
| 337 | This code is copied from the elf32-mips.c. We also support an arbitrary |
| 338 | number of HI16 relocs to be associated with a single LO16 reloc. The |
| 339 | assembler sorts the relocs to ensure each HI16 immediately precedes its |
| 340 | LO16. However if there are multiple copies, the assembler may not find |
| 341 | the real LO16 so it picks the first one it finds. */ |
| 342 | |
| 343 | struct m32r_hi16 |
| 344 | { |
| 345 | struct m32r_hi16 *next; |
| 346 | bfd_byte *addr; |
| 347 | bfd_vma addend; |
| 348 | }; |
| 349 | |
| 350 | /* FIXME: This should not be a static variable. */ |
| 351 | |
| 352 | static struct m32r_hi16 *m32r_hi16_list; |
| 353 | |
| 354 | static bfd_reloc_status_type |
| 355 | m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data, |
| 356 | input_section, output_bfd, error_message) |
| 357 | bfd *abfd; |
| 358 | arelent *reloc_entry; |
| 359 | asymbol *symbol; |
| 360 | PTR data; |
| 361 | asection *input_section; |
| 362 | bfd *output_bfd; |
| 363 | char **error_message; |
| 364 | { |
| 365 | bfd_reloc_status_type ret; |
| 366 | bfd_vma relocation; |
| 367 | struct m32r_hi16 *n; |
| 368 | |
| 369 | /* This part is from bfd_elf_generic_reloc. |
| 370 | If we're relocating, and this an external symbol, we don't want |
| 371 | to change anything. */ |
| 372 | if (output_bfd != (bfd *) NULL |
| 373 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
| 374 | && reloc_entry->addend == 0) |
| 375 | { |
| 376 | reloc_entry->address += input_section->output_offset; |
| 377 | return bfd_reloc_ok; |
| 378 | } |
| 379 | |
| 380 | /* Sanity check the address (offset in section). */ |
| 381 | if (reloc_entry->address > input_section->_cooked_size) |
| 382 | return bfd_reloc_outofrange; |
| 383 | |
| 384 | ret = bfd_reloc_ok; |
| 385 | if (bfd_is_und_section (symbol->section) |
| 386 | && output_bfd == (bfd *) NULL) |
| 387 | ret = bfd_reloc_undefined; |
| 388 | |
| 389 | if (bfd_is_com_section (symbol->section)) |
| 390 | relocation = 0; |
| 391 | else |
| 392 | relocation = symbol->value; |
| 393 | |
| 394 | relocation += symbol->section->output_section->vma; |
| 395 | relocation += symbol->section->output_offset; |
| 396 | relocation += reloc_entry->addend; |
| 397 | |
| 398 | /* Save the information, and let LO16 do the actual relocation. */ |
| 399 | n = (struct m32r_hi16 *) bfd_malloc (sizeof *n); |
| 400 | if (n == NULL) |
| 401 | return bfd_reloc_outofrange; |
| 402 | n->addr = (bfd_byte *) data + reloc_entry->address; |
| 403 | n->addend = relocation; |
| 404 | n->next = m32r_hi16_list; |
| 405 | m32r_hi16_list = n; |
| 406 | |
| 407 | if (output_bfd != (bfd *) NULL) |
| 408 | reloc_entry->address += input_section->output_offset; |
| 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | /* Handle an M32R ELF HI16 reloc. */ |
| 414 | |
| 415 | static void |
| 416 | m32r_elf_relocate_hi16 (input_bfd, type, relhi, rello, contents, addend) |
| 417 | bfd *input_bfd; |
| 418 | int type; |
| 419 | Elf_Internal_Rela *relhi; |
| 420 | Elf_Internal_Rela *rello; |
| 421 | bfd_byte *contents; |
| 422 | bfd_vma addend; |
| 423 | { |
| 424 | unsigned long insn; |
| 425 | bfd_vma addlo; |
| 426 | |
| 427 | insn = bfd_get_32 (input_bfd, contents + relhi->r_offset); |
| 428 | |
| 429 | addlo = bfd_get_32 (input_bfd, contents + rello->r_offset); |
| 430 | if (type == R_M32R_HI16_SLO) |
| 431 | addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000; |
| 432 | else |
| 433 | addlo &= 0xffff; |
| 434 | |
| 435 | addend += ((insn & 0xffff) << 16) + addlo; |
| 436 | |
| 437 | /* Reaccount for sign extension of low part. */ |
| 438 | if (type == R_M32R_HI16_SLO |
| 439 | && (addend & 0x8000) != 0) |
| 440 | addend += 0x10000; |
| 441 | |
| 442 | bfd_put_32 (input_bfd, |
| 443 | (insn & 0xffff0000) | ((addend >> 16) & 0xffff), |
| 444 | contents + relhi->r_offset); |
| 445 | } |
| 446 | |
| 447 | /* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit |
| 448 | inplace relocation; this function exists in order to do the |
| 449 | R_M32R_HI16_[SU]LO relocation described above. */ |
| 450 | |
| 451 | bfd_reloc_status_type |
| 452 | m32r_elf_lo16_reloc (abfd, reloc_entry, symbol, data, |
| 453 | input_section, output_bfd, error_message) |
| 454 | bfd *abfd; |
| 455 | arelent *reloc_entry; |
| 456 | asymbol *symbol; |
| 457 | PTR data; |
| 458 | asection *input_section; |
| 459 | bfd *output_bfd; |
| 460 | char **error_message; |
| 461 | { |
| 462 | if (m32r_hi16_list != NULL) |
| 463 | { |
| 464 | struct m32r_hi16 *l; |
| 465 | |
| 466 | l = m32r_hi16_list; |
| 467 | while (l != NULL) |
| 468 | { |
| 469 | unsigned long insn; |
| 470 | unsigned long val; |
| 471 | unsigned long vallo; |
| 472 | struct m32r_hi16 *next; |
| 473 | |
| 474 | /* Do the HI16 relocation. Note that we actually don't need |
| 475 | to know anything about the LO16 itself, except where to |
| 476 | find the low 16 bits of the addend needed by the LO16. */ |
| 477 | insn = bfd_get_32 (abfd, l->addr); |
| 478 | vallo = ((bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address) |
| 479 | & 0xffff) ^ 0x8000) - 0x8000; |
| 480 | val = ((insn & 0xffff) << 16) + vallo; |
| 481 | val += l->addend; |
| 482 | |
| 483 | /* Reaccount for sign extension of low part. */ |
| 484 | if ((val & 0x8000) != 0) |
| 485 | val += 0x10000; |
| 486 | |
| 487 | insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff); |
| 488 | bfd_put_32 (abfd, insn, l->addr); |
| 489 | |
| 490 | next = l->next; |
| 491 | free (l); |
| 492 | l = next; |
| 493 | } |
| 494 | |
| 495 | m32r_hi16_list = NULL; |
| 496 | } |
| 497 | |
| 498 | /* Now do the LO16 reloc in the usual way. */ |
| 499 | return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data, |
| 500 | input_section, output_bfd, error_message); |
| 501 | } |
| 502 | |
| 503 | /* Handle the R_M32R_SDA16 reloc. |
| 504 | This reloc is used to compute the address of objects in the small data area |
| 505 | and to perform loads and stores from that area. |
| 506 | The lower 16 bits are sign extended and added to the register specified |
| 507 | in the instruction, which is assumed to point to _SDA_BASE_. */ |
| 508 | |
| 509 | static bfd_reloc_status_type |
| 510 | m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data, |
| 511 | input_section, output_bfd, error_message) |
| 512 | bfd *abfd; |
| 513 | arelent *reloc_entry; |
| 514 | asymbol *symbol; |
| 515 | PTR data; |
| 516 | asection *input_section; |
| 517 | bfd *output_bfd; |
| 518 | char **error_message; |
| 519 | { |
| 520 | bfd_vma sda_base; |
| 521 | unsigned long x; |
| 522 | reloc_howto_type *howto; |
| 523 | |
| 524 | /* This part is from bfd_elf_generic_reloc. */ |
| 525 | if (output_bfd != (bfd *) NULL |
| 526 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
| 527 | && (! reloc_entry->howto->partial_inplace |
| 528 | || reloc_entry->addend == 0)) |
| 529 | { |
| 530 | reloc_entry->address += input_section->output_offset; |
| 531 | return bfd_reloc_ok; |
| 532 | } |
| 533 | |
| 534 | if (output_bfd != NULL) |
| 535 | { |
| 536 | /* FIXME: See bfd_perform_relocation. Is this right? */ |
| 537 | return bfd_reloc_continue; |
| 538 | } |
| 539 | |
| 540 | /* FIXME: not sure what to do here yet. But then again, the linker |
| 541 | may never call us. */ |
| 542 | abort (); |
| 543 | } |
| 544 | \f |
| 545 | /* Map BFD reloc types to M32R ELF reloc types. */ |
| 546 | |
| 547 | struct m32r_reloc_map |
| 548 | { |
| 549 | unsigned char bfd_reloc_val; |
| 550 | unsigned char elf_reloc_val; |
| 551 | }; |
| 552 | |
| 553 | static const struct m32r_reloc_map m32r_reloc_map[] = |
| 554 | { |
| 555 | { BFD_RELOC_NONE, R_M32R_NONE }, |
| 556 | { BFD_RELOC_16, R_M32R_16 }, |
| 557 | { BFD_RELOC_32, R_M32R_32 }, |
| 558 | { BFD_RELOC_M32R_24, R_M32R_24 }, |
| 559 | { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL }, |
| 560 | { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL }, |
| 561 | { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL }, |
| 562 | { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO }, |
| 563 | { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO }, |
| 564 | { BFD_RELOC_M32R_LO16, R_M32R_LO16 }, |
| 565 | { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 }, |
| 566 | }; |
| 567 | |
| 568 | static reloc_howto_type * |
| 569 | bfd_elf32_bfd_reloc_type_lookup (abfd, code) |
| 570 | bfd *abfd; |
| 571 | bfd_reloc_code_real_type code; |
| 572 | { |
| 573 | unsigned int i; |
| 574 | |
| 575 | for (i = 0; |
| 576 | i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map); |
| 577 | i++) |
| 578 | { |
| 579 | if (m32r_reloc_map[i].bfd_reloc_val == code) |
| 580 | return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val]; |
| 581 | } |
| 582 | |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | /* Set the howto pointer for an M32R ELF reloc. */ |
| 587 | |
| 588 | static void |
| 589 | m32r_info_to_howto_rel (abfd, cache_ptr, dst) |
| 590 | bfd *abfd; |
| 591 | arelent *cache_ptr; |
| 592 | Elf32_Internal_Rel *dst; |
| 593 | { |
| 594 | unsigned int r_type; |
| 595 | |
| 596 | r_type = ELF32_R_TYPE (dst->r_info); |
| 597 | BFD_ASSERT (r_type < (unsigned int) R_M32R_max); |
| 598 | cache_ptr->howto = &m32r_elf_howto_table[r_type]; |
| 599 | } |
| 600 | \f |
| 601 | /* Given a BFD section, try to locate the corresponding ELF section |
| 602 | index. */ |
| 603 | |
| 604 | boolean |
| 605 | _bfd_m32r_elf_section_from_bfd_section (abfd, hdr, sec, retval) |
| 606 | bfd *abfd; |
| 607 | Elf32_Internal_Shdr *hdr; |
| 608 | asection *sec; |
| 609 | int *retval; |
| 610 | { |
| 611 | if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0) |
| 612 | { |
| 613 | *retval = SHN_M32R_SCOMMON; |
| 614 | return true; |
| 615 | } |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | /* M32R ELF uses two common sections. One is the usual one, and the other |
| 620 | is for small objects. All the small objects are kept together, and then |
| 621 | referenced via one register, which yields faster assembler code. It is |
| 622 | up to the compiler to emit an instruction to load the register with |
| 623 | _SDA_BASE. This is what we use for the small common section. This |
| 624 | approach is copied from elf32-mips.c. */ |
| 625 | static asection m32r_elf_scom_section; |
| 626 | static asymbol m32r_elf_scom_symbol; |
| 627 | static asymbol *m32r_elf_scom_symbol_ptr; |
| 628 | |
| 629 | /* Handle the special M32R section numbers that a symbol may use. */ |
| 630 | |
| 631 | void |
| 632 | _bfd_m32r_elf_symbol_processing (abfd, asym) |
| 633 | bfd *abfd; |
| 634 | asymbol *asym; |
| 635 | { |
| 636 | elf_symbol_type *elfsym; |
| 637 | |
| 638 | elfsym = (elf_symbol_type *) asym; |
| 639 | |
| 640 | switch (elfsym->internal_elf_sym.st_shndx) |
| 641 | { |
| 642 | case SHN_M32R_SCOMMON: |
| 643 | if (m32r_elf_scom_section.name == NULL) |
| 644 | { |
| 645 | /* Initialize the small common section. */ |
| 646 | m32r_elf_scom_section.name = ".scommon"; |
| 647 | m32r_elf_scom_section.flags = SEC_IS_COMMON; |
| 648 | m32r_elf_scom_section.output_section = &m32r_elf_scom_section; |
| 649 | m32r_elf_scom_section.symbol = &m32r_elf_scom_symbol; |
| 650 | m32r_elf_scom_section.symbol_ptr_ptr = &m32r_elf_scom_symbol_ptr; |
| 651 | m32r_elf_scom_symbol.name = ".scommon"; |
| 652 | m32r_elf_scom_symbol.flags = BSF_SECTION_SYM; |
| 653 | m32r_elf_scom_symbol.section = &m32r_elf_scom_section; |
| 654 | m32r_elf_scom_symbol_ptr = &m32r_elf_scom_symbol; |
| 655 | } |
| 656 | asym->section = &m32r_elf_scom_section; |
| 657 | asym->value = elfsym->internal_elf_sym.st_size; |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /* Hook called by the linker routine which adds symbols from an object |
| 663 | file. We must handle the special M32R section numbers here. |
| 664 | We also keep watching for whether we need to create the sdata special |
| 665 | linker sections. */ |
| 666 | |
| 667 | static boolean |
| 668 | m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp) |
| 669 | bfd *abfd; |
| 670 | struct bfd_link_info *info; |
| 671 | const Elf_Internal_Sym *sym; |
| 672 | const char **namep; |
| 673 | flagword *flagsp; |
| 674 | asection **secp; |
| 675 | bfd_vma *valp; |
| 676 | { |
| 677 | if (! info->relocateable |
| 678 | && (*namep)[0] == '_' && (*namep)[1] == 'S' |
| 679 | && strcmp (*namep, "_SDA_BASE_") == 0) |
| 680 | { |
| 681 | /* This is simpler than using _bfd_elf_create_linker_section |
| 682 | (our needs are simpler than ppc's needs). Also |
| 683 | _bfd_elf_create_linker_section currently has a bug where if a .sdata |
| 684 | section already exists a new one is created that follows it which |
| 685 | screws of _SDA_BASE_ address calcs because output_offset != 0. */ |
| 686 | struct elf_link_hash_entry *h; |
| 687 | asection *s = bfd_get_section_by_name (abfd, ".sdata"); |
| 688 | |
| 689 | /* The following code was cobbled from elf32-ppc.c and elflink.c. */ |
| 690 | |
| 691 | if (s == NULL) |
| 692 | { |
| 693 | int flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS |
| 694 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); |
| 695 | |
| 696 | s = bfd_make_section_anyway (abfd, ".sdata"); |
| 697 | if (s == NULL) |
| 698 | return false; |
| 699 | bfd_set_section_flags (abfd, s, flags); |
| 700 | bfd_set_section_alignment (abfd, s, 2); |
| 701 | } |
| 702 | |
| 703 | h = (struct elf_link_hash_entry *) |
| 704 | bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, false); |
| 705 | |
| 706 | if ((h == NULL || h->root.type == bfd_link_hash_undefined) |
| 707 | && !(_bfd_generic_link_add_one_symbol (info, |
| 708 | abfd, |
| 709 | "_SDA_BASE_", |
| 710 | BSF_GLOBAL, |
| 711 | s, |
| 712 | 32768, |
| 713 | (const char *) NULL, |
| 714 | false, |
| 715 | get_elf_backend_data (abfd)->collect, |
| 716 | (struct bfd_link_hash_entry **) &h))) |
| 717 | return false; |
| 718 | h->type = STT_OBJECT; |
| 719 | } |
| 720 | |
| 721 | switch (sym->st_shndx) |
| 722 | { |
| 723 | case SHN_M32R_SCOMMON: |
| 724 | *secp = bfd_make_section_old_way (abfd, ".scommon"); |
| 725 | (*secp)->flags |= SEC_IS_COMMON; |
| 726 | *valp = sym->st_size; |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | return true; |
| 731 | } |
| 732 | |
| 733 | /* We have to figure out the SDA_BASE value, so that we can adjust the |
| 734 | symbol value correctly. We look up the symbol _SDA_BASE_ in the output |
| 735 | BFD. If we can't find it, we're stuck. We cache it in the ELF |
| 736 | target data. We don't need to adjust the symbol value for an |
| 737 | external symbol if we are producing relocateable output. */ |
| 738 | |
| 739 | static bfd_reloc_status_type |
| 740 | m32r_elf_final_sda_base (output_bfd, info, error_message, psb) |
| 741 | bfd *output_bfd; |
| 742 | struct bfd_link_info *info; |
| 743 | const char **error_message; |
| 744 | bfd_vma *psb; |
| 745 | { |
| 746 | if (elf_gp (output_bfd) == 0) |
| 747 | { |
| 748 | struct bfd_link_hash_entry *h; |
| 749 | |
| 750 | h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true); |
| 751 | if (h != (struct bfd_link_hash_entry *) NULL |
| 752 | && h->type == bfd_link_hash_defined) |
| 753 | elf_gp (output_bfd) = (h->u.def.value |
| 754 | + h->u.def.section->output_section->vma |
| 755 | + h->u.def.section->output_offset); |
| 756 | else |
| 757 | { |
| 758 | /* Only get the error once. */ |
| 759 | *psb = elf_gp (output_bfd) = 4; |
| 760 | *error_message = |
| 761 | (const char *) "SDA relocation when _SDA_BASE_ not defined"; |
| 762 | return bfd_reloc_dangerous; |
| 763 | } |
| 764 | } |
| 765 | *psb = elf_gp (output_bfd); |
| 766 | return bfd_reloc_ok; |
| 767 | } |
| 768 | \f |
| 769 | /* Relocate an M32R/D ELF section. |
| 770 | There is some attempt to make this function usable for many architectures, |
| 771 | both USE_REL and USE_RELA ['twould be nice if such a critter existed], |
| 772 | if only to serve as a learning tool. |
| 773 | |
| 774 | The RELOCATE_SECTION function is called by the new ELF backend linker |
| 775 | to handle the relocations for a section. |
| 776 | |
| 777 | The relocs are always passed as Rela structures; if the section |
| 778 | actually uses Rel structures, the r_addend field will always be |
| 779 | zero. |
| 780 | |
| 781 | This function is responsible for adjust the section contents as |
| 782 | necessary, and (if using Rela relocs and generating a |
| 783 | relocateable output file) adjusting the reloc addend as |
| 784 | necessary. |
| 785 | |
| 786 | This function does not have to worry about setting the reloc |
| 787 | address or the reloc symbol index. |
| 788 | |
| 789 | LOCAL_SYMS is a pointer to the swapped in local symbols. |
| 790 | |
| 791 | LOCAL_SECTIONS is an array giving the section in the input file |
| 792 | corresponding to the st_shndx field of each local symbol. |
| 793 | |
| 794 | The global hash table entry for the global symbols can be found |
| 795 | via elf_sym_hashes (input_bfd). |
| 796 | |
| 797 | When generating relocateable output, this function must handle |
| 798 | STB_LOCAL/STT_SECTION symbols specially. The output symbol is |
| 799 | going to be the section symbol corresponding to the output |
| 800 | section, which means that the addend must be adjusted |
| 801 | accordingly. */ |
| 802 | |
| 803 | static boolean |
| 804 | m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section, |
| 805 | contents, relocs, local_syms, local_sections) |
| 806 | bfd *output_bfd; |
| 807 | struct bfd_link_info *info; |
| 808 | bfd *input_bfd; |
| 809 | asection *input_section; |
| 810 | bfd_byte *contents; |
| 811 | Elf_Internal_Rela *relocs; |
| 812 | Elf_Internal_Sym *local_syms; |
| 813 | asection **local_sections; |
| 814 | { |
| 815 | Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 816 | struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd); |
| 817 | Elf_Internal_Rela *rel, *relend; |
| 818 | bfd *dynobj = elf_hash_table (info)->dynobj; |
| 819 | /* Assume success. */ |
| 820 | boolean ret = true; |
| 821 | |
| 822 | rel = relocs; |
| 823 | relend = relocs + input_section->reloc_count; |
| 824 | for (; rel < relend; rel++) |
| 825 | { |
| 826 | int r_type; |
| 827 | reloc_howto_type *howto; |
| 828 | unsigned long r_symndx; |
| 829 | /* We can't modify r_addend here as elf_link_input_bfd has an assert to |
| 830 | ensure it's zero (we use REL relocs, not RELA). Therefore this |
| 831 | should be assigning zero to `addend', but for clarity we use |
| 832 | `r_addend'. */ |
| 833 | bfd_vma addend = rel->r_addend; |
| 834 | bfd_vma offset = rel->r_offset; |
| 835 | struct elf_link_hash_entry *h; |
| 836 | Elf_Internal_Sym *sym; |
| 837 | asection *sec; |
| 838 | const char *sym_name; |
| 839 | bfd_reloc_status_type r; |
| 840 | const char *errmsg = NULL; |
| 841 | |
| 842 | r_type = ELF32_R_TYPE (rel->r_info); |
| 843 | if (r_type < 0 || r_type >= (int) R_M32R_max) |
| 844 | { |
| 845 | (*_bfd_error_handler) ("%s: unknown relocation type %d", |
| 846 | bfd_get_filename (input_bfd), |
| 847 | (int) r_type); |
| 848 | bfd_set_error (bfd_error_bad_value); |
| 849 | ret = false; |
| 850 | continue; |
| 851 | } |
| 852 | |
| 853 | howto = m32r_elf_howto_table + r_type; |
| 854 | r_symndx = ELF32_R_SYM (rel->r_info); |
| 855 | |
| 856 | if (info->relocateable) |
| 857 | { |
| 858 | /* This is a relocateable link. We don't have to change |
| 859 | anything, unless the reloc is against a section symbol, |
| 860 | in which case we have to adjust according to where the |
| 861 | section symbol winds up in the output section. */ |
| 862 | sec = NULL; |
| 863 | if (r_symndx >= symtab_hdr->sh_info) |
| 864 | { |
| 865 | /* External symbol. */ |
| 866 | continue; |
| 867 | } |
| 868 | |
| 869 | /* Local symbol. */ |
| 870 | sym = local_syms + r_symndx; |
| 871 | sym_name = "<local symbol>"; |
| 872 | /* STT_SECTION: symbol is associated with a section. */ |
| 873 | if (ELF_ST_TYPE (sym->st_info) != STT_SECTION) |
| 874 | { |
| 875 | /* Symbol isn't associated with a section. Nothing to do. */ |
| 876 | continue; |
| 877 | } |
| 878 | |
| 879 | sec = local_sections[r_symndx]; |
| 880 | addend += sec->output_offset + sym->st_value; |
| 881 | #ifndef USE_REL |
| 882 | /* This can't be done for USE_REL because it doesn't mean anything |
| 883 | and elf_link_input_bfd asserts this stays zero. */ |
| 884 | rel->r_addend = addend; |
| 885 | #endif |
| 886 | |
| 887 | #ifndef USE_REL |
| 888 | /* Addends are stored with relocs. We're done. */ |
| 889 | continue; |
| 890 | #else /* USE_REL */ |
| 891 | /* If partial_inplace, we need to store any additional addend |
| 892 | back in the section. */ |
| 893 | if (! howto->partial_inplace) |
| 894 | continue; |
| 895 | /* ??? Here is a nice place to call a special_function |
| 896 | like handler. */ |
| 897 | if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO) |
| 898 | r = _bfd_relocate_contents (howto, input_bfd, |
| 899 | addend, contents + offset); |
| 900 | else |
| 901 | { |
| 902 | Elf_Internal_Rela *lorel; |
| 903 | |
| 904 | /* We allow an arbitrary number of HI16 relocs before the |
| 905 | LO16 reloc. This permits gcc to emit the HI and LO relocs |
| 906 | itself. */ |
| 907 | for (lorel = rel + 1; |
| 908 | (lorel < relend |
| 909 | && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO |
| 910 | || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO)); |
| 911 | lorel++) |
| 912 | continue; |
| 913 | if (lorel < relend |
| 914 | && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16) |
| 915 | { |
| 916 | m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel, |
| 917 | contents, addend); |
| 918 | r = bfd_reloc_ok; |
| 919 | } |
| 920 | else |
| 921 | r = _bfd_relocate_contents (howto, input_bfd, |
| 922 | addend, contents + offset); |
| 923 | } |
| 924 | #endif /* USE_REL */ |
| 925 | } |
| 926 | else |
| 927 | { |
| 928 | bfd_vma relocation; |
| 929 | |
| 930 | /* This is a final link. */ |
| 931 | h = NULL; |
| 932 | sym = NULL; |
| 933 | sec = NULL; |
| 934 | |
| 935 | if (r_symndx < symtab_hdr->sh_info) |
| 936 | { |
| 937 | /* Local symbol. */ |
| 938 | sym = local_syms + r_symndx; |
| 939 | sec = local_sections[r_symndx]; |
| 940 | sym_name = "<local symbol>"; |
| 941 | relocation = (sec->output_section->vma |
| 942 | + sec->output_offset |
| 943 | + sym->st_value); |
| 944 | } |
| 945 | else |
| 946 | { |
| 947 | /* External symbol. */ |
| 948 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
| 949 | while (h->root.type == bfd_link_hash_indirect |
| 950 | || h->root.type == bfd_link_hash_warning) |
| 951 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
| 952 | sym_name = h->root.root.string; |
| 953 | |
| 954 | if (h->root.type == bfd_link_hash_defined |
| 955 | || h->root.type == bfd_link_hash_defweak) |
| 956 | { |
| 957 | sec = h->root.u.def.section; |
| 958 | if (sec->output_section == NULL) |
| 959 | relocation = 0; |
| 960 | else |
| 961 | relocation = (h->root.u.def.value |
| 962 | + sec->output_section->vma |
| 963 | + sec->output_offset); |
| 964 | } |
| 965 | else if (h->root.type == bfd_link_hash_undefweak) |
| 966 | relocation = 0; |
| 967 | else |
| 968 | { |
| 969 | if (! ((*info->callbacks->undefined_symbol) |
| 970 | (info, h->root.root.string, input_bfd, |
| 971 | input_section, offset))) |
| 972 | return false; |
| 973 | relocation = 0; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | /* Sanity check the address. */ |
| 978 | if (offset > input_section->_raw_size) |
| 979 | { |
| 980 | r = bfd_reloc_outofrange; |
| 981 | goto check_reloc; |
| 982 | } |
| 983 | |
| 984 | switch ((int) r_type) |
| 985 | { |
| 986 | case (int) R_M32R_10_PCREL : |
| 987 | r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section, |
| 988 | contents, offset, |
| 989 | sec, relocation, addend); |
| 990 | break; |
| 991 | |
| 992 | case (int) R_M32R_HI16_SLO : |
| 993 | case (int) R_M32R_HI16_ULO : |
| 994 | { |
| 995 | Elf_Internal_Rela *lorel; |
| 996 | |
| 997 | /* We allow an arbitrary number of HI16 relocs before the |
| 998 | LO16 reloc. This permits gcc to emit the HI and LO relocs |
| 999 | itself. */ |
| 1000 | for (lorel = rel + 1; |
| 1001 | (lorel < relend |
| 1002 | && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO |
| 1003 | || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO)); |
| 1004 | lorel++) |
| 1005 | continue; |
| 1006 | if (lorel < relend |
| 1007 | && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16) |
| 1008 | { |
| 1009 | m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel, |
| 1010 | contents, relocation + addend); |
| 1011 | r = bfd_reloc_ok; |
| 1012 | } |
| 1013 | else |
| 1014 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
| 1015 | contents, offset, |
| 1016 | relocation, addend); |
| 1017 | } |
| 1018 | break; |
| 1019 | |
| 1020 | case (int) R_M32R_SDA16 : |
| 1021 | { |
| 1022 | const char *name; |
| 1023 | |
| 1024 | BFD_ASSERT (sec != NULL); |
| 1025 | name = bfd_get_section_name (abfd, sec); |
| 1026 | |
| 1027 | if (strcmp (name, ".sdata") == 0 |
| 1028 | || strcmp (name, ".sbss") == 0 |
| 1029 | || strcmp (name, ".scommon") == 0) |
| 1030 | { |
| 1031 | bfd_vma sda_base; |
| 1032 | bfd *out_bfd = sec->output_section->owner; |
| 1033 | |
| 1034 | r = m32r_elf_final_sda_base (out_bfd, info, |
| 1035 | &errmsg, |
| 1036 | &sda_base); |
| 1037 | if (r != bfd_reloc_ok) |
| 1038 | { |
| 1039 | ret = false; |
| 1040 | goto check_reloc; |
| 1041 | } |
| 1042 | |
| 1043 | /* At this point `relocation' contains the object's |
| 1044 | address. */ |
| 1045 | relocation -= sda_base; |
| 1046 | /* Now it contains the offset from _SDA_BASE_. */ |
| 1047 | } |
| 1048 | else |
| 1049 | { |
| 1050 | (*_bfd_error_handler) ("%s: The target (%s) of an %s relocation is in the wrong section (%s)", |
| 1051 | bfd_get_filename (input_bfd), |
| 1052 | sym_name, |
| 1053 | m32r_elf_howto_table[(int) r_type].name, |
| 1054 | bfd_get_section_name (abfd, sec)); |
| 1055 | /*bfd_set_error (bfd_error_bad_value); ??? why? */ |
| 1056 | ret = false; |
| 1057 | continue; |
| 1058 | } |
| 1059 | } |
| 1060 | /* fall through */ |
| 1061 | |
| 1062 | default : |
| 1063 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
| 1064 | contents, offset, |
| 1065 | relocation, addend); |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | check_reloc: |
| 1071 | |
| 1072 | if (r != bfd_reloc_ok) |
| 1073 | { |
| 1074 | /* FIXME: This should be generic enough to go in a utility. */ |
| 1075 | const char *name; |
| 1076 | |
| 1077 | if (h != NULL) |
| 1078 | name = h->root.root.string; |
| 1079 | else |
| 1080 | { |
| 1081 | name = (bfd_elf_string_from_elf_section |
| 1082 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); |
| 1083 | if (name == NULL || *name == '\0') |
| 1084 | name = bfd_section_name (input_bfd, sec); |
| 1085 | } |
| 1086 | |
| 1087 | if (errmsg != NULL) |
| 1088 | goto common_error; |
| 1089 | |
| 1090 | switch (r) |
| 1091 | { |
| 1092 | case bfd_reloc_overflow: |
| 1093 | if (! ((*info->callbacks->reloc_overflow) |
| 1094 | (info, name, howto->name, (bfd_vma) 0, |
| 1095 | input_bfd, input_section, offset))) |
| 1096 | return false; |
| 1097 | break; |
| 1098 | |
| 1099 | case bfd_reloc_undefined: |
| 1100 | if (! ((*info->callbacks->undefined_symbol) |
| 1101 | (info, name, input_bfd, input_section, |
| 1102 | offset))) |
| 1103 | return false; |
| 1104 | break; |
| 1105 | |
| 1106 | case bfd_reloc_outofrange: |
| 1107 | errmsg = "internal error: out of range error"; |
| 1108 | goto common_error; |
| 1109 | |
| 1110 | case bfd_reloc_notsupported: |
| 1111 | errmsg = "internal error: unsupported relocation error"; |
| 1112 | goto common_error; |
| 1113 | |
| 1114 | case bfd_reloc_dangerous: |
| 1115 | errmsg = "internal error: dangerous error"; |
| 1116 | goto common_error; |
| 1117 | |
| 1118 | default: |
| 1119 | errmsg = "internal error: unknown error"; |
| 1120 | /* fall through */ |
| 1121 | |
| 1122 | common_error: |
| 1123 | if (!((*info->callbacks->warning) |
| 1124 | (info, errmsg, name, input_bfd, input_section, |
| 1125 | offset))) |
| 1126 | return false; |
| 1127 | break; |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | return ret; |
| 1133 | } |
| 1134 | \f |
| 1135 | #if 0 /* relaxing not supported yet */ |
| 1136 | |
| 1137 | /* This function handles relaxing for the m32r. |
| 1138 | Relaxing on the m32r is tricky because of instruction alignment |
| 1139 | requirements (4 byte instructions must be aligned on 4 byte boundaries). |
| 1140 | |
| 1141 | The following relaxing opportunities are handled: |
| 1142 | |
| 1143 | seth/add3/jl -> bl24 or bl8 |
| 1144 | seth/add3 -> ld24 |
| 1145 | |
| 1146 | It would be nice to handle bl24 -> bl8 but given: |
| 1147 | |
| 1148 | - 4 byte insns must be on 4 byte boundaries |
| 1149 | - branch instructions only branch to insns on 4 byte boundaries |
| 1150 | |
| 1151 | this isn't much of a win because the insn in the 2 "deleted" bytes |
| 1152 | must become a nop. With some complexity some real relaxation could be |
| 1153 | done but the frequency just wouldn't make it worth it; it's better to |
| 1154 | try to do all the code compaction one can elsewhere. |
| 1155 | When the chip supports parallel 16 bit insns, things may change. |
| 1156 | */ |
| 1157 | |
| 1158 | static boolean |
| 1159 | m32r_elf_relax_section (abfd, sec, link_info, again) |
| 1160 | bfd *abfd; |
| 1161 | asection *sec; |
| 1162 | struct bfd_link_info *link_info; |
| 1163 | boolean *again; |
| 1164 | { |
| 1165 | Elf_Internal_Shdr *symtab_hdr; |
| 1166 | /* The Rela structures are used here because that's what |
| 1167 | _bfd_elf32_link_read_relocs uses [for convenience - it sets the addend |
| 1168 | field to 0]. */ |
| 1169 | Elf_Internal_Rela *internal_relocs; |
| 1170 | Elf_Internal_Rela *free_relocs = NULL; |
| 1171 | Elf_Internal_Rela *irel, *irelend; |
| 1172 | bfd_byte *contents = NULL; |
| 1173 | bfd_byte *free_contents = NULL; |
| 1174 | Elf32_External_Sym *extsyms = NULL; |
| 1175 | Elf32_External_Sym *free_extsyms = NULL; |
| 1176 | |
| 1177 | /* Assume nothing changes. */ |
| 1178 | *again = false; |
| 1179 | |
| 1180 | /* We don't have to do anything for a relocateable link, if |
| 1181 | this section does not have relocs, or if this is not a |
| 1182 | code section. */ |
| 1183 | if (link_info->relocateable |
| 1184 | || (sec->flags & SEC_RELOC) == 0 |
| 1185 | || sec->reloc_count == 0 |
| 1186 | || (sec->flags & SEC_CODE) == 0 |
| 1187 | || 0 /* FIXME: check SHF_M32R_CAN_RELAX */) |
| 1188 | return true; |
| 1189 | |
| 1190 | /* If this is the first time we have been called for this section, |
| 1191 | initialize the cooked size. */ |
| 1192 | if (sec->_cooked_size == 0) |
| 1193 | sec->_cooked_size = sec->_raw_size; |
| 1194 | |
| 1195 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 1196 | |
| 1197 | /* Get a copy of the native relocations. */ |
| 1198 | internal_relocs = (_bfd_elf32_link_read_relocs |
| 1199 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, |
| 1200 | link_info->keep_memory)); |
| 1201 | if (internal_relocs == NULL) |
| 1202 | goto error_return; |
| 1203 | if (! link_info->keep_memory) |
| 1204 | free_relocs = internal_relocs; |
| 1205 | |
| 1206 | /* Walk through them looking for relaxing opportunities. */ |
| 1207 | irelend = internal_relocs + sec->reloc_count; |
| 1208 | for (irel = internal_relocs; irel < irelend; irel++) |
| 1209 | { |
| 1210 | bfd_vma symval; |
| 1211 | |
| 1212 | /* If this isn't something that can be relaxed, then ignore |
| 1213 | this reloc. */ |
| 1214 | if (ELF32_R_TYPE (irel->r_info) != (int) R_M32R_HI16_SLO) |
| 1215 | continue; |
| 1216 | |
| 1217 | /* Get the section contents if we haven't done so already. */ |
| 1218 | if (contents == NULL) |
| 1219 | { |
| 1220 | /* Get cached copy if it exists. */ |
| 1221 | if (elf_section_data (sec)->this_hdr.contents != NULL) |
| 1222 | contents = elf_section_data (sec)->this_hdr.contents; |
| 1223 | else |
| 1224 | { |
| 1225 | /* Go get them off disk. */ |
| 1226 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); |
| 1227 | if (contents == NULL) |
| 1228 | goto error_return; |
| 1229 | free_contents = contents; |
| 1230 | |
| 1231 | if (! bfd_get_section_contents (abfd, sec, contents, |
| 1232 | (file_ptr) 0, sec->_raw_size)) |
| 1233 | goto error_return; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | /* Read this BFD's symbols if we haven't done so already. */ |
| 1238 | if (extsyms == NULL) |
| 1239 | { |
| 1240 | /* Get cached copy if it exists. */ |
| 1241 | if (symtab_hdr->contents != NULL) |
| 1242 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; |
| 1243 | else |
| 1244 | { |
| 1245 | /* Go get them off disk. */ |
| 1246 | extsyms = ((Elf32_External_Sym *) |
| 1247 | bfd_malloc (symtab_hdr->sh_size)); |
| 1248 | if (extsyms == NULL) |
| 1249 | goto error_return; |
| 1250 | free_extsyms = extsyms; |
| 1251 | if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0 |
| 1252 | || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd) |
| 1253 | != symtab_hdr->sh_size)) |
| 1254 | goto error_return; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | /* Get the value of the symbol referred to by the reloc. */ |
| 1259 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) |
| 1260 | { |
| 1261 | Elf_Internal_Sym isym; |
| 1262 | asection *sym_sec; |
| 1263 | |
| 1264 | /* A local symbol. */ |
| 1265 | bfd_elf32_swap_symbol_in (abfd, |
| 1266 | extsyms + ELF32_R_SYM (irel->r_info), |
| 1267 | &isym); |
| 1268 | |
| 1269 | sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx); |
| 1270 | symval = (isym.st_value |
| 1271 | + sym_sec->output_section->vma |
| 1272 | + sym_sec->output_offset); |
| 1273 | } |
| 1274 | else |
| 1275 | { |
| 1276 | unsigned long indx; |
| 1277 | struct elf_link_hash_entry *h; |
| 1278 | |
| 1279 | /* An external symbol. */ |
| 1280 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; |
| 1281 | h = elf_sym_hashes (abfd)[indx]; |
| 1282 | BFD_ASSERT (h != NULL); |
| 1283 | if (h->root.type != bfd_link_hash_defined |
| 1284 | && h->root.type != bfd_link_hash_defweak) |
| 1285 | { |
| 1286 | /* This appears to be a reference to an undefined |
| 1287 | symbol. Just ignore it--it will be caught by the |
| 1288 | regular reloc processing. */ |
| 1289 | continue; |
| 1290 | } |
| 1291 | |
| 1292 | symval = (h->root.u.def.value |
| 1293 | + h->root.u.def.section->output_section->vma |
| 1294 | + h->root.u.def.section->output_offset); |
| 1295 | } |
| 1296 | |
| 1297 | /* For simplicity of coding, we are going to modify the section |
| 1298 | contents, the section relocs, and the BFD symbol table. We |
| 1299 | must tell the rest of the code not to free up this |
| 1300 | information. It would be possible to instead create a table |
| 1301 | of changes which have to be made, as is done in coff-mips.c; |
| 1302 | that would be more work, but would require less memory when |
| 1303 | the linker is run. */ |
| 1304 | |
| 1305 | /* Try to change a seth/add3/jl subroutine call to bl24 or bl8. |
| 1306 | This sequence is generated by the compiler when compiling in |
| 1307 | 32 bit mode. Also look for seth/add3 -> ld24. */ |
| 1308 | |
| 1309 | if (ELF32_R_TYPE (irel->r_info) == (int) R_M32R_HI16_SLO) |
| 1310 | { |
| 1311 | Elf_Internal_Rela *nrel; |
| 1312 | bfd_vma pc = (sec->output_section->vma + sec->output_offset |
| 1313 | + irel->r_offset); |
| 1314 | bfd_signed_vma pcrel_value = symval - pc; |
| 1315 | unsigned int code,reg; |
| 1316 | int addend,nop_p,bl8_p,to_delete; |
| 1317 | |
| 1318 | /* The tests are ordered so that we get out as quickly as possible |
| 1319 | if this isn't something we can relax, taking into account that |
| 1320 | we are looking for two separate possibilities (jl/ld24). */ |
| 1321 | |
| 1322 | /* Do nothing if no room in the section for this to be what we're |
| 1323 | looking for. */ |
| 1324 | if (irel->r_offset > sec->_cooked_size - 8) |
| 1325 | continue; |
| 1326 | |
| 1327 | /* Make sure the next relocation applies to the next |
| 1328 | instruction and that it's the add3's reloc. */ |
| 1329 | nrel = irel + 1; |
| 1330 | if (nrel == irelend |
| 1331 | || irel->r_offset + 4 != nrel->r_offset |
| 1332 | || ELF32_R_TYPE (nrel->r_info) != (int) R_M32R_LO16) |
| 1333 | continue; |
| 1334 | |
| 1335 | /* See if the instructions are seth/add3. */ |
| 1336 | /* FIXME: This is where macros from cgen can come in. */ |
| 1337 | code = bfd_get_16 (abfd, contents + irel->r_offset + 0); |
| 1338 | if ((code & 0xf0ff) != 0xd0c0) |
| 1339 | continue; /* not seth rN,foo */ |
| 1340 | reg = (code & 0x0f00) >> 8; |
| 1341 | code = bfd_get_16 (abfd, contents + irel->r_offset + 4); |
| 1342 | if (code != (0x80a0 | reg | (reg << 8))) |
| 1343 | continue; /* not add3 rN,rN,foo */ |
| 1344 | |
| 1345 | /* At this point we've confirmed we have seth/add3. Now check |
| 1346 | whether the next insn is a jl, in which case try to change this |
| 1347 | to bl24 or bl8. */ |
| 1348 | |
| 1349 | /* Ensure the branch target is in range. |
| 1350 | The bl24 instruction has a 24 bit operand which is the target |
| 1351 | address right shifted by 2, giving a signed range of 26 bits. |
| 1352 | Note that 4 bytes are added to the high value because the target |
| 1353 | will be at least 4 bytes closer if we can relax. It'll actually |
| 1354 | be 4 or 8 bytes closer, but we don't know which just yet and |
| 1355 | the difference isn't significant enough to worry about. */ |
| 1356 | #ifndef USE_REL /* put in for learning purposes */ |
| 1357 | pcrel_value += irel->r_addend; |
| 1358 | #else |
| 1359 | addend = bfd_get_signed_16 (abfd, contents + irel->r_offset + 2); |
| 1360 | pcrel_value += addend; |
| 1361 | #endif |
| 1362 | |
| 1363 | if (pcrel_value >= -(1 << 25) && pcrel_value < (1 << 25) + 4 |
| 1364 | /* Do nothing if no room in the section for this to be what we're |
| 1365 | looking for. */ |
| 1366 | && (irel->r_offset <= sec->_cooked_size - 12) |
| 1367 | /* Ensure the next insn is "jl rN". */ |
| 1368 | && ((code = bfd_get_16 (abfd, contents + irel->r_offset + 8)), |
| 1369 | code != (0x1ec0 | reg))) |
| 1370 | { |
| 1371 | /* We can relax to bl24/bl8. */ |
| 1372 | |
| 1373 | /* See if there's a nop following the jl. |
| 1374 | Also see if we can use a bl8 insn. */ |
| 1375 | code = bfd_get_16 (abfd, contents + irel->r_offset + 10); |
| 1376 | nop_p = (code & 0x7fff) == 7000; |
| 1377 | bl8_p = pcrel_value >= -0x200 && pcrel_value < 0x200; |
| 1378 | |
| 1379 | if (bl8_p) |
| 1380 | { |
| 1381 | /* Change "seth rN,foo" to "bl8 foo || nop". |
| 1382 | We OR in CODE just in case it's not a nop (technically, |
| 1383 | CODE currently must be a nop, but for cleanness we |
| 1384 | allow it to be anything). */ |
| 1385 | #ifndef USE_REL /* put in for learning purposes */ |
| 1386 | code = 0x7e000000 | code; |
| 1387 | #else |
| 1388 | code = (0x7e000000 + (((addend >> 2) & 0xff) << 16)) | code; |
| 1389 | #endif |
| 1390 | to_delete = 8; |
| 1391 | } |
| 1392 | else |
| 1393 | { |
| 1394 | /* Change the seth rN,foo to a bl24 foo. */ |
| 1395 | #ifndef USE_REL /* put in for learning purposes */ |
| 1396 | code = 0xfe000000; |
| 1397 | #else |
| 1398 | code = 0xfe000000 + ((addend >> 2) & 0xffffff); |
| 1399 | #endif |
| 1400 | to_delete = nop_p ? 8 : 4; |
| 1401 | } |
| 1402 | |
| 1403 | bfd_put_32 (abfd, code, contents + irel->r_offset); |
| 1404 | |
| 1405 | /* Set the new reloc type. */ |
| 1406 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info), |
| 1407 | bl8_p ? R_M32R_10_PCREL : R_M32R_26_PCREL); |
| 1408 | |
| 1409 | /* Delete the add3 reloc by making it a null reloc. */ |
| 1410 | nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info), |
| 1411 | R_M32R_NONE); |
| 1412 | } |
| 1413 | else if (addend >= 0 |
| 1414 | && symval + addend <= 0xffffff) |
| 1415 | { |
| 1416 | /* We can relax to ld24. */ |
| 1417 | |
| 1418 | code = 0xe0000000 | (reg << 24) | (addend & 0xffffff); |
| 1419 | bfd_put_32 (abfd, code, contents + irel->r_offset); |
| 1420 | to_delete = 4; |
| 1421 | /* Tell the following code a nop filler isn't needed. */ |
| 1422 | nop_p = 1; |
| 1423 | } |
| 1424 | else |
| 1425 | { |
| 1426 | /* Can't do anything here. */ |
| 1427 | continue; |
| 1428 | } |
| 1429 | |
| 1430 | /* Note that we've changed the relocs, section contents, etc. */ |
| 1431 | elf_section_data (sec)->relocs = internal_relocs; |
| 1432 | free_relocs = NULL; |
| 1433 | |
| 1434 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1435 | free_contents = NULL; |
| 1436 | |
| 1437 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 1438 | free_extsyms = NULL; |
| 1439 | |
| 1440 | /* Delete TO_DELETE bytes of data. */ |
| 1441 | if (!m32r_elf_relax_delete_bytes (abfd, sec, |
| 1442 | irel->r_offset + 4, to_delete)) |
| 1443 | goto error_return; |
| 1444 | |
| 1445 | /* Now that the following bytes have been moved into place, see if |
| 1446 | we need to replace the jl with a nop. This happens when we had |
| 1447 | to use a bl24 insn and the insn following the jl isn't a nop. |
| 1448 | Technically, this situation can't happen (since the insn can |
| 1449 | never be executed) but to be clean we do this. When the chip |
| 1450 | supports parallel 16 bit insns things may change. |
| 1451 | We don't need to do this in the case of relaxing to ld24, |
| 1452 | and the above code sets nop_p so this isn't done. */ |
| 1453 | if (! nop_p && to_delete == 4) |
| 1454 | bfd_put_16 (abfd, 0x7000, contents + irel->r_offset + 4); |
| 1455 | |
| 1456 | /* That will change things, so we should relax again. |
| 1457 | Note that this is not required, and it may be slow. */ |
| 1458 | *again = true; |
| 1459 | |
| 1460 | continue; |
| 1461 | } |
| 1462 | |
| 1463 | /* loop to try the next reloc */ |
| 1464 | } |
| 1465 | |
| 1466 | if (free_relocs != NULL) |
| 1467 | { |
| 1468 | free (free_relocs); |
| 1469 | free_relocs = NULL; |
| 1470 | } |
| 1471 | |
| 1472 | if (free_contents != NULL) |
| 1473 | { |
| 1474 | if (! link_info->keep_memory) |
| 1475 | free (free_contents); |
| 1476 | else |
| 1477 | { |
| 1478 | /* Cache the section contents for elf_link_input_bfd. */ |
| 1479 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1480 | } |
| 1481 | free_contents = NULL; |
| 1482 | } |
| 1483 | |
| 1484 | if (free_extsyms != NULL) |
| 1485 | { |
| 1486 | if (! link_info->keep_memory) |
| 1487 | free (free_extsyms); |
| 1488 | else |
| 1489 | { |
| 1490 | /* Cache the symbols for elf_link_input_bfd. */ |
| 1491 | symtab_hdr->contents = extsyms; |
| 1492 | } |
| 1493 | free_extsyms = NULL; |
| 1494 | } |
| 1495 | |
| 1496 | return true; |
| 1497 | |
| 1498 | error_return: |
| 1499 | if (free_relocs != NULL) |
| 1500 | free (free_relocs); |
| 1501 | if (free_contents != NULL) |
| 1502 | free (free_contents); |
| 1503 | if (free_extsyms != NULL) |
| 1504 | free (free_extsyms); |
| 1505 | return false; |
| 1506 | } |
| 1507 | |
| 1508 | /* Delete some bytes from a section while relaxing. */ |
| 1509 | |
| 1510 | static boolean |
| 1511 | m32r_elf_relax_delete_bytes (abfd, sec, addr, count) |
| 1512 | bfd *abfd; |
| 1513 | asection *sec; |
| 1514 | bfd_vma addr; |
| 1515 | int count; |
| 1516 | { |
| 1517 | Elf_Internal_Shdr *symtab_hdr; |
| 1518 | Elf32_External_Sym *extsyms; |
| 1519 | int shndx, index; |
| 1520 | bfd_byte *contents; |
| 1521 | Elf_Internal_Rela *irel, *irelend; |
| 1522 | Elf_Internal_Rela *irelalign; |
| 1523 | bfd_vma toaddr; |
| 1524 | Elf32_External_Sym *esym, *esymend; |
| 1525 | struct elf_link_hash_entry *sym_hash; |
| 1526 | |
| 1527 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 1528 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; |
| 1529 | |
| 1530 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
| 1531 | |
| 1532 | contents = elf_section_data (sec)->this_hdr.contents; |
| 1533 | |
| 1534 | /* The deletion must stop at the next ALIGN reloc for an aligment |
| 1535 | power larger than the number of bytes we are deleting. */ |
| 1536 | |
| 1537 | irelalign = NULL; |
| 1538 | toaddr = sec->_cooked_size; |
| 1539 | |
| 1540 | irel = elf_section_data (sec)->relocs; |
| 1541 | irelend = irel + sec->reloc_count; |
| 1542 | |
| 1543 | /* Actually delete the bytes. */ |
| 1544 | memmove (contents + addr, contents + addr + count, toaddr - addr - count); |
| 1545 | sec->_cooked_size -= count; |
| 1546 | |
| 1547 | /* Adjust all the relocs. */ |
| 1548 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) |
| 1549 | { |
| 1550 | /* Get the new reloc address. */ |
| 1551 | if ((irel->r_offset > addr |
| 1552 | && irel->r_offset < toaddr)) |
| 1553 | irel->r_offset -= count; |
| 1554 | } |
| 1555 | |
| 1556 | /* Adjust the local symbols defined in this section. */ |
| 1557 | esym = extsyms; |
| 1558 | esymend = esym + symtab_hdr->sh_info; |
| 1559 | for (; esym < esymend; esym++) |
| 1560 | { |
| 1561 | Elf_Internal_Sym isym; |
| 1562 | |
| 1563 | bfd_elf32_swap_symbol_in (abfd, esym, &isym); |
| 1564 | |
| 1565 | if (isym.st_shndx == shndx |
| 1566 | && isym.st_value > addr |
| 1567 | && isym.st_value < toaddr) |
| 1568 | { |
| 1569 | isym.st_value -= count; |
| 1570 | bfd_elf32_swap_symbol_out (abfd, &isym, esym); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | /* Now adjust the global symbols defined in this section. */ |
| 1575 | esym = extsyms + symtab_hdr->sh_info; |
| 1576 | esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)); |
| 1577 | for (index = 0; esym < esymend; esym++, index++) |
| 1578 | { |
| 1579 | Elf_Internal_Sym isym; |
| 1580 | |
| 1581 | bfd_elf32_swap_symbol_in (abfd, esym, &isym); |
| 1582 | sym_hash = elf_sym_hashes (abfd)[index]; |
| 1583 | if (isym.st_shndx == shndx |
| 1584 | && ((sym_hash)->root.type == bfd_link_hash_defined |
| 1585 | || (sym_hash)->root.type == bfd_link_hash_defweak) |
| 1586 | && (sym_hash)->root.u.def.section == sec |
| 1587 | && (sym_hash)->root.u.def.value > addr |
| 1588 | && (sym_hash)->root.u.def.value < toaddr) |
| 1589 | { |
| 1590 | (sym_hash)->root.u.def.value -= count; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | return true; |
| 1595 | } |
| 1596 | |
| 1597 | /* This is a version of bfd_generic_get_relocated_section_contents |
| 1598 | which uses m32r_elf_relocate_section. */ |
| 1599 | |
| 1600 | static bfd_byte * |
| 1601 | m32r_elf_get_relocated_section_contents (output_bfd, link_info, link_order, |
| 1602 | data, relocateable, symbols) |
| 1603 | bfd *output_bfd; |
| 1604 | struct bfd_link_info *link_info; |
| 1605 | struct bfd_link_order *link_order; |
| 1606 | bfd_byte *data; |
| 1607 | boolean relocateable; |
| 1608 | asymbol **symbols; |
| 1609 | { |
| 1610 | Elf_Internal_Shdr *symtab_hdr; |
| 1611 | asection *input_section = link_order->u.indirect.section; |
| 1612 | bfd *input_bfd = input_section->owner; |
| 1613 | asection **sections = NULL; |
| 1614 | Elf_Internal_Rela *internal_relocs = NULL; |
| 1615 | Elf32_External_Sym *external_syms = NULL; |
| 1616 | Elf_Internal_Sym *internal_syms = NULL; |
| 1617 | |
| 1618 | /* We only need to handle the case of relaxing, or of having a |
| 1619 | particular set of section contents, specially. */ |
| 1620 | if (relocateable |
| 1621 | || elf_section_data (input_section)->this_hdr.contents == NULL) |
| 1622 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info, |
| 1623 | link_order, data, |
| 1624 | relocateable, |
| 1625 | symbols); |
| 1626 | |
| 1627 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 1628 | |
| 1629 | memcpy (data, elf_section_data (input_section)->this_hdr.contents, |
| 1630 | input_section->_raw_size); |
| 1631 | |
| 1632 | if ((input_section->flags & SEC_RELOC) != 0 |
| 1633 | && input_section->reloc_count > 0) |
| 1634 | { |
| 1635 | Elf_Internal_Sym *isymp; |
| 1636 | asection **secpp; |
| 1637 | Elf32_External_Sym *esym, *esymend; |
| 1638 | |
| 1639 | if (symtab_hdr->contents != NULL) |
| 1640 | external_syms = (Elf32_External_Sym *) symtab_hdr->contents; |
| 1641 | else |
| 1642 | { |
| 1643 | external_syms = ((Elf32_External_Sym *) |
| 1644 | bfd_malloc (symtab_hdr->sh_info |
| 1645 | * sizeof (Elf32_External_Sym))); |
| 1646 | if (external_syms == NULL && symtab_hdr->sh_info > 0) |
| 1647 | goto error_return; |
| 1648 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 |
| 1649 | || (bfd_read (external_syms, sizeof (Elf32_External_Sym), |
| 1650 | symtab_hdr->sh_info, input_bfd) |
| 1651 | != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)))) |
| 1652 | goto error_return; |
| 1653 | } |
| 1654 | |
| 1655 | internal_relocs = (_bfd_elf32_link_read_relocs |
| 1656 | (input_bfd, input_section, (PTR) NULL, |
| 1657 | (Elf_Internal_Rela *) NULL, false)); |
| 1658 | if (internal_relocs == NULL) |
| 1659 | goto error_return; |
| 1660 | |
| 1661 | internal_syms = ((Elf_Internal_Sym *) |
| 1662 | bfd_malloc (symtab_hdr->sh_info |
| 1663 | * sizeof (Elf_Internal_Sym))); |
| 1664 | if (internal_syms == NULL && symtab_hdr->sh_info > 0) |
| 1665 | goto error_return; |
| 1666 | |
| 1667 | sections = (asection **) bfd_malloc (symtab_hdr->sh_info |
| 1668 | * sizeof (asection *)); |
| 1669 | if (sections == NULL && symtab_hdr->sh_info > 0) |
| 1670 | goto error_return; |
| 1671 | |
| 1672 | isymp = internal_syms; |
| 1673 | secpp = sections; |
| 1674 | esym = external_syms; |
| 1675 | esymend = esym + symtab_hdr->sh_info; |
| 1676 | for (; esym < esymend; ++esym, ++isymp, ++secpp) |
| 1677 | { |
| 1678 | asection *isec; |
| 1679 | |
| 1680 | bfd_elf32_swap_symbol_in (input_bfd, esym, isymp); |
| 1681 | |
| 1682 | if (isymp->st_shndx == SHN_UNDEF) |
| 1683 | isec = bfd_und_section_ptr; |
| 1684 | else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE) |
| 1685 | isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx); |
| 1686 | else if (isymp->st_shndx == SHN_ABS) |
| 1687 | isec = bfd_abs_section_ptr; |
| 1688 | else if (isymp->st_shndx == SHN_COMMON) |
| 1689 | isec = bfd_com_section_ptr; |
| 1690 | else if (isymp->st_shndx == SHN_M32R_SCOMMON) |
| 1691 | isec = &m32r_elf_scom_section; |
| 1692 | else |
| 1693 | { |
| 1694 | /* Who knows? */ |
| 1695 | isec = NULL; |
| 1696 | } |
| 1697 | |
| 1698 | *secpp = isec; |
| 1699 | } |
| 1700 | |
| 1701 | if (! m32r_elf_relocate_section (output_bfd, link_info, input_bfd, |
| 1702 | input_section, data, internal_relocs, |
| 1703 | internal_syms, sections)) |
| 1704 | goto error_return; |
| 1705 | |
| 1706 | if (sections != NULL) |
| 1707 | free (sections); |
| 1708 | sections = NULL; |
| 1709 | if (internal_syms != NULL) |
| 1710 | free (internal_syms); |
| 1711 | internal_syms = NULL; |
| 1712 | if (external_syms != NULL && symtab_hdr->contents == NULL) |
| 1713 | free (external_syms); |
| 1714 | external_syms = NULL; |
| 1715 | if (internal_relocs != elf_section_data (input_section)->relocs) |
| 1716 | free (internal_relocs); |
| 1717 | internal_relocs = NULL; |
| 1718 | } |
| 1719 | |
| 1720 | return data; |
| 1721 | |
| 1722 | error_return: |
| 1723 | if (internal_relocs != NULL |
| 1724 | && internal_relocs != elf_section_data (input_section)->relocs) |
| 1725 | free (internal_relocs); |
| 1726 | if (external_syms != NULL && symtab_hdr->contents == NULL) |
| 1727 | free (external_syms); |
| 1728 | if (internal_syms != NULL) |
| 1729 | free (internal_syms); |
| 1730 | if (sections != NULL) |
| 1731 | free (sections); |
| 1732 | return NULL; |
| 1733 | } |
| 1734 | |
| 1735 | #endif /* #if 0 */ |
| 1736 | \f |
| 1737 | /* Set the right machine number. */ |
| 1738 | static boolean |
| 1739 | m32r_elf_object_p (abfd) |
| 1740 | bfd *abfd; |
| 1741 | { |
| 1742 | switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH) |
| 1743 | { |
| 1744 | default: |
| 1745 | case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break; |
| 1746 | case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break; |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | /* Store the machine number in the flags field. */ |
| 1751 | void |
| 1752 | m32r_elf_final_write_processing (abfd, linker) |
| 1753 | bfd * abfd; |
| 1754 | boolean linker; |
| 1755 | { |
| 1756 | unsigned long val; |
| 1757 | |
| 1758 | switch (bfd_get_mach (abfd)) |
| 1759 | { |
| 1760 | default: |
| 1761 | case bfd_mach_m32r: val = E_M32R_ARCH; break; |
| 1762 | case bfd_mach_m32rx: val = E_M32RX_ARCH; break; |
| 1763 | } |
| 1764 | |
| 1765 | elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH; |
| 1766 | elf_elfheader (abfd)->e_flags |= val; |
| 1767 | } |
| 1768 | |
| 1769 | /* Function to keep M32R specific file flags. */ |
| 1770 | boolean |
| 1771 | m32r_elf_set_private_flags (abfd, flags) |
| 1772 | bfd * abfd; |
| 1773 | flagword flags; |
| 1774 | { |
| 1775 | BFD_ASSERT (!elf_flags_init (abfd) |
| 1776 | || elf_elfheader (abfd)->e_flags == flags); |
| 1777 | |
| 1778 | elf_elfheader (abfd)->e_flags = flags; |
| 1779 | elf_flags_init (abfd) = true; |
| 1780 | return true; |
| 1781 | } |
| 1782 | |
| 1783 | /* Copy backend specific data from one object module to another */ |
| 1784 | boolean |
| 1785 | m32r_elf_copy_private_bfd_data (ibfd, obfd) |
| 1786 | bfd * ibfd; |
| 1787 | bfd * obfd; |
| 1788 | { |
| 1789 | if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
| 1790 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
| 1791 | return true; |
| 1792 | |
| 1793 | BFD_ASSERT (!elf_flags_init (obfd) |
| 1794 | || (elf_elfheader (obfd)->e_flags |
| 1795 | == elf_elfheader (ibfd)->e_flags)); |
| 1796 | |
| 1797 | elf_gp (obfd) = elf_gp (ibfd); |
| 1798 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; |
| 1799 | elf_flags_init (obfd) = true; |
| 1800 | return true; |
| 1801 | } |
| 1802 | |
| 1803 | /* Merge backend specific data from an object file to the output |
| 1804 | object file when linking. */ |
| 1805 | boolean |
| 1806 | m32r_elf_merge_private_bfd_data (ibfd, obfd) |
| 1807 | bfd * ibfd; |
| 1808 | bfd * obfd; |
| 1809 | { |
| 1810 | flagword old_flags; |
| 1811 | flagword new_flags; |
| 1812 | |
| 1813 | if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
| 1814 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
| 1815 | return true; |
| 1816 | |
| 1817 | new_flags = elf_elfheader (ibfd)->e_flags; |
| 1818 | old_flags = elf_elfheader (obfd)->e_flags; |
| 1819 | |
| 1820 | if (! elf_flags_init (obfd)) |
| 1821 | { |
| 1822 | elf_flags_init (obfd) = true; |
| 1823 | elf_elfheader (obfd)->e_flags = new_flags; |
| 1824 | |
| 1825 | if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) |
| 1826 | && bfd_get_arch_info (obfd)->the_default) |
| 1827 | { |
| 1828 | return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd)); |
| 1829 | } |
| 1830 | |
| 1831 | return true; |
| 1832 | } |
| 1833 | |
| 1834 | /* Check flag compatibility. */ |
| 1835 | if (new_flags == old_flags) |
| 1836 | return true; |
| 1837 | |
| 1838 | if ((new_flags & EF_M32R_ARCH) != (old_flags & EF_M32R_ARCH)) |
| 1839 | { |
| 1840 | if ((new_flags & EF_M32R_ARCH) == E_M32RX_ARCH) |
| 1841 | { |
| 1842 | _bfd_error_handler ("%s: Instruction set mismatch with previous modules", |
| 1843 | bfd_get_filename (ibfd)); |
| 1844 | #if 1 |
| 1845 | bfd_set_error (bfd_error_bad_value); |
| 1846 | return false; |
| 1847 | #endif |
| 1848 | } |
| 1849 | else |
| 1850 | { |
| 1851 | _bfd_error_handler ("%s: Merging M32R instructions with M32RX instructions", |
| 1852 | bfd_get_filename (ibfd)); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | return true; |
| 1857 | } |
| 1858 | |
| 1859 | /* Display the flags field */ |
| 1860 | static boolean |
| 1861 | m32r_elf_print_private_bfd_data (abfd, ptr) |
| 1862 | bfd * abfd; |
| 1863 | PTR ptr; |
| 1864 | { |
| 1865 | FILE * file = (FILE *) ptr; |
| 1866 | |
| 1867 | BFD_ASSERT (abfd != NULL && ptr != NULL) |
| 1868 | |
| 1869 | fprintf (file, "private flags = %x", elf_elfheader (abfd)->e_flags); |
| 1870 | |
| 1871 | switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH) |
| 1872 | { |
| 1873 | default: |
| 1874 | case E_M32R_ARCH: fprintf (file, ": m32r instructions"); break; |
| 1875 | case E_M32RX_ARCH: fprintf (file, ": m32rx instructions"); break; |
| 1876 | } |
| 1877 | |
| 1878 | fputc ('\n', file); |
| 1879 | |
| 1880 | return true; |
| 1881 | } |
| 1882 | |
| 1883 | \f |
| 1884 | |
| 1885 | #define ELF_ARCH bfd_arch_m32r |
| 1886 | #define ELF_MACHINE_CODE EM_CYGNUS_M32R |
| 1887 | #define ELF_MAXPAGESIZE 0x1000 |
| 1888 | |
| 1889 | #define TARGET_BIG_SYM bfd_elf32_m32r_vec |
| 1890 | #define TARGET_BIG_NAME "elf32-m32r" |
| 1891 | |
| 1892 | #define elf_info_to_howto 0 |
| 1893 | #define elf_info_to_howto_rel m32r_info_to_howto_rel |
| 1894 | #define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section |
| 1895 | #define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing |
| 1896 | #define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook |
| 1897 | #define elf_backend_relocate_section m32r_elf_relocate_section |
| 1898 | |
| 1899 | #if 0 /* not yet */ |
| 1900 | /* relax support */ |
| 1901 | #define bfd_elf32_bfd_relax_section m32r_elf_relax_section |
| 1902 | #define bfd_elf32_bfd_get_relocated_section_contents \ |
| 1903 | m32r_elf_get_relocated_section_contents |
| 1904 | #endif |
| 1905 | |
| 1906 | #define elf_backend_object_p m32r_elf_object_p |
| 1907 | #define elf_backend_final_write_processing m32r_elf_final_write_processing |
| 1908 | #define bfd_elf32_bfd_copy_private_bfd_data m32r_elf_copy_private_bfd_data |
| 1909 | #define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data |
| 1910 | #define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags |
| 1911 | #define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data |
| 1912 | |
| 1913 | #include "elf32-target.h" |