| 1 | /* Matsushita 10200 specific support for 32-bit ELF
|
|---|
| 2 | Copyright 1996, 1997, 1998, 1999, 2000, 2001
|
|---|
| 3 | Free Software Foundation, Inc.
|
|---|
| 4 |
|
|---|
| 5 | This file is part of BFD, the Binary File Descriptor library.
|
|---|
| 6 |
|
|---|
| 7 | This program is free software; you can redistribute it and/or modify
|
|---|
| 8 | it under the terms of the GNU General Public License as published by
|
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 10 | (at your option) any later version.
|
|---|
| 11 |
|
|---|
| 12 | This program is distributed in the hope that it will be useful,
|
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | GNU General Public License for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License
|
|---|
| 18 | along with this program; if not, write to the Free Software
|
|---|
| 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|---|
| 20 |
|
|---|
| 21 | #include "bfd.h"
|
|---|
| 22 | #include "sysdep.h"
|
|---|
| 23 | #include "libbfd.h"
|
|---|
| 24 | #include "elf-bfd.h"
|
|---|
| 25 |
|
|---|
| 26 | static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
|
|---|
| 27 | PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
|
|---|
| 28 | static void mn10200_info_to_howto
|
|---|
| 29 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
|
|---|
| 30 | static boolean mn10200_elf_relax_delete_bytes
|
|---|
| 31 | PARAMS ((bfd *, asection *, bfd_vma, int));
|
|---|
| 32 | static boolean mn10200_elf_symbol_address_p
|
|---|
| 33 | PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
|
|---|
| 34 |
|
|---|
| 35 | /* We have to use RELA instructions since md_apply_fix3 in the assembler
|
|---|
| 36 | does absolutely nothing. */
|
|---|
| 37 | #define USE_RELA
|
|---|
| 38 |
|
|---|
| 39 | enum reloc_type {
|
|---|
| 40 | R_MN10200_NONE = 0,
|
|---|
| 41 | R_MN10200_32,
|
|---|
| 42 | R_MN10200_16,
|
|---|
| 43 | R_MN10200_8,
|
|---|
| 44 | R_MN10200_24,
|
|---|
| 45 | R_MN10200_PCREL8,
|
|---|
| 46 | R_MN10200_PCREL16,
|
|---|
| 47 | R_MN10200_PCREL24,
|
|---|
| 48 | R_MN10200_MAX
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | static reloc_howto_type elf_mn10200_howto_table[] = {
|
|---|
| 52 | /* Dummy relocation. Does nothing. */
|
|---|
| 53 | HOWTO (R_MN10200_NONE,
|
|---|
| 54 | 0,
|
|---|
| 55 | 2,
|
|---|
| 56 | 16,
|
|---|
| 57 | false,
|
|---|
| 58 | 0,
|
|---|
| 59 | complain_overflow_bitfield,
|
|---|
| 60 | bfd_elf_generic_reloc,
|
|---|
| 61 | "R_MN10200_NONE",
|
|---|
| 62 | false,
|
|---|
| 63 | 0,
|
|---|
| 64 | 0,
|
|---|
| 65 | false),
|
|---|
| 66 | /* Standard 32 bit reloc. */
|
|---|
| 67 | HOWTO (R_MN10200_32,
|
|---|
| 68 | 0,
|
|---|
| 69 | 2,
|
|---|
| 70 | 32,
|
|---|
| 71 | false,
|
|---|
| 72 | 0,
|
|---|
| 73 | complain_overflow_bitfield,
|
|---|
| 74 | bfd_elf_generic_reloc,
|
|---|
| 75 | "R_MN10200_32",
|
|---|
| 76 | false,
|
|---|
| 77 | 0xffffffff,
|
|---|
| 78 | 0xffffffff,
|
|---|
| 79 | false),
|
|---|
| 80 | /* Standard 16 bit reloc. */
|
|---|
| 81 | HOWTO (R_MN10200_16,
|
|---|
| 82 | 0,
|
|---|
| 83 | 1,
|
|---|
| 84 | 16,
|
|---|
| 85 | false,
|
|---|
| 86 | 0,
|
|---|
| 87 | complain_overflow_bitfield,
|
|---|
| 88 | bfd_elf_generic_reloc,
|
|---|
| 89 | "R_MN10200_16",
|
|---|
| 90 | false,
|
|---|
| 91 | 0xffff,
|
|---|
| 92 | 0xffff,
|
|---|
| 93 | false),
|
|---|
| 94 | /* Standard 8 bit reloc. */
|
|---|
| 95 | HOWTO (R_MN10200_8,
|
|---|
| 96 | 0,
|
|---|
| 97 | 0,
|
|---|
| 98 | 8,
|
|---|
| 99 | false,
|
|---|
| 100 | 0,
|
|---|
| 101 | complain_overflow_bitfield,
|
|---|
| 102 | bfd_elf_generic_reloc,
|
|---|
| 103 | "R_MN10200_8",
|
|---|
| 104 | false,
|
|---|
| 105 | 0xff,
|
|---|
| 106 | 0xff,
|
|---|
| 107 | false),
|
|---|
| 108 | /* Standard 24 bit reloc. */
|
|---|
| 109 | HOWTO (R_MN10200_24,
|
|---|
| 110 | 0,
|
|---|
| 111 | 2,
|
|---|
| 112 | 24,
|
|---|
| 113 | false,
|
|---|
| 114 | 0,
|
|---|
| 115 | complain_overflow_bitfield,
|
|---|
| 116 | bfd_elf_generic_reloc,
|
|---|
| 117 | "R_MN10200_24",
|
|---|
| 118 | false,
|
|---|
| 119 | 0xffffff,
|
|---|
| 120 | 0xffffff,
|
|---|
| 121 | false),
|
|---|
| 122 | /* Simple 8 pc-relative reloc. */
|
|---|
| 123 | HOWTO (R_MN10200_PCREL8,
|
|---|
| 124 | 0,
|
|---|
| 125 | 0,
|
|---|
| 126 | 8,
|
|---|
| 127 | true,
|
|---|
| 128 | 0,
|
|---|
| 129 | complain_overflow_bitfield,
|
|---|
| 130 | bfd_elf_generic_reloc,
|
|---|
| 131 | "R_MN10200_PCREL8",
|
|---|
| 132 | false,
|
|---|
| 133 | 0xff,
|
|---|
| 134 | 0xff,
|
|---|
| 135 | true),
|
|---|
| 136 | /* Simple 16 pc-relative reloc. */
|
|---|
| 137 | HOWTO (R_MN10200_PCREL16,
|
|---|
| 138 | 0,
|
|---|
| 139 | 1,
|
|---|
| 140 | 16,
|
|---|
| 141 | true,
|
|---|
| 142 | 0,
|
|---|
| 143 | complain_overflow_bitfield,
|
|---|
| 144 | bfd_elf_generic_reloc,
|
|---|
| 145 | "R_MN10200_PCREL16",
|
|---|
| 146 | false,
|
|---|
| 147 | 0xffff,
|
|---|
| 148 | 0xffff,
|
|---|
| 149 | true),
|
|---|
| 150 | /* Simple 32bit pc-relative reloc with a 1 byte adjustment
|
|---|
| 151 | to get the pc-relative offset correct. */
|
|---|
| 152 | HOWTO (R_MN10200_PCREL24,
|
|---|
| 153 | 0,
|
|---|
| 154 | 2,
|
|---|
| 155 | 24,
|
|---|
| 156 | true,
|
|---|
| 157 | 0,
|
|---|
| 158 | complain_overflow_bitfield,
|
|---|
| 159 | bfd_elf_generic_reloc,
|
|---|
| 160 | "R_MN10200_PCREL24",
|
|---|
| 161 | false,
|
|---|
| 162 | 0xffffff,
|
|---|
| 163 | 0xffffff,
|
|---|
| 164 | true),
|
|---|
| 165 | };
|
|---|
| 166 |
|
|---|
| 167 | struct mn10200_reloc_map {
|
|---|
| 168 | bfd_reloc_code_real_type bfd_reloc_val;
|
|---|
| 169 | unsigned char elf_reloc_val;
|
|---|
| 170 | };
|
|---|
| 171 |
|
|---|
| 172 | static const struct mn10200_reloc_map mn10200_reloc_map[] = {
|
|---|
| 173 | { BFD_RELOC_NONE , R_MN10200_NONE , },
|
|---|
| 174 | { BFD_RELOC_32 , R_MN10200_32 , },
|
|---|
| 175 | { BFD_RELOC_16 , R_MN10200_16 , },
|
|---|
| 176 | { BFD_RELOC_8 , R_MN10200_8 , },
|
|---|
| 177 | { BFD_RELOC_24 , R_MN10200_24 , },
|
|---|
| 178 | { BFD_RELOC_8_PCREL , R_MN10200_PCREL8 , },
|
|---|
| 179 | { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, },
|
|---|
| 180 | { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, },
|
|---|
| 181 | };
|
|---|
| 182 |
|
|---|
| 183 | static reloc_howto_type *
|
|---|
| 184 | bfd_elf32_bfd_reloc_type_lookup (abfd, code)
|
|---|
| 185 | bfd *abfd ATTRIBUTE_UNUSED;
|
|---|
| 186 | bfd_reloc_code_real_type code;
|
|---|
| 187 | {
|
|---|
| 188 | unsigned int i;
|
|---|
| 189 |
|
|---|
| 190 | for (i = 0;
|
|---|
| 191 | i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map);
|
|---|
| 192 | i++)
|
|---|
| 193 | {
|
|---|
| 194 | if (mn10200_reloc_map[i].bfd_reloc_val == code)
|
|---|
| 195 | return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val];
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | return NULL;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /* Set the howto pointer for an MN10200 ELF reloc. */
|
|---|
| 202 |
|
|---|
| 203 | static void
|
|---|
| 204 | mn10200_info_to_howto (abfd, cache_ptr, dst)
|
|---|
| 205 | bfd *abfd ATTRIBUTE_UNUSED;
|
|---|
| 206 | arelent *cache_ptr;
|
|---|
| 207 | Elf32_Internal_Rela *dst;
|
|---|
| 208 | {
|
|---|
| 209 | unsigned int r_type;
|
|---|
| 210 |
|
|---|
| 211 | r_type = ELF32_R_TYPE (dst->r_info);
|
|---|
| 212 | BFD_ASSERT (r_type < (unsigned int) R_MN10200_MAX);
|
|---|
| 213 | cache_ptr->howto = &elf_mn10200_howto_table[r_type];
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /* Perform a relocation as part of a final link. */
|
|---|
| 217 | static bfd_reloc_status_type
|
|---|
| 218 | mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
|
|---|
| 219 | input_section, contents, offset, value,
|
|---|
| 220 | addend, info, sym_sec, is_local)
|
|---|
| 221 | reloc_howto_type *howto;
|
|---|
| 222 | bfd *input_bfd;
|
|---|
| 223 | bfd *output_bfd ATTRIBUTE_UNUSED;
|
|---|
| 224 | asection *input_section;
|
|---|
| 225 | bfd_byte *contents;
|
|---|
| 226 | bfd_vma offset;
|
|---|
| 227 | bfd_vma value;
|
|---|
| 228 | bfd_vma addend;
|
|---|
| 229 | struct bfd_link_info *info ATTRIBUTE_UNUSED;
|
|---|
| 230 | asection *sym_sec ATTRIBUTE_UNUSED;
|
|---|
| 231 | int is_local ATTRIBUTE_UNUSED;
|
|---|
| 232 | {
|
|---|
| 233 | unsigned long r_type = howto->type;
|
|---|
| 234 | bfd_byte *hit_data = contents + offset;
|
|---|
| 235 |
|
|---|
| 236 | switch (r_type)
|
|---|
| 237 | {
|
|---|
| 238 |
|
|---|
| 239 | case R_MN10200_NONE:
|
|---|
| 240 | return bfd_reloc_ok;
|
|---|
| 241 |
|
|---|
| 242 | case R_MN10200_32:
|
|---|
| 243 | value += addend;
|
|---|
| 244 | bfd_put_32 (input_bfd, value, hit_data);
|
|---|
| 245 | return bfd_reloc_ok;
|
|---|
| 246 |
|
|---|
| 247 | case R_MN10200_16:
|
|---|
| 248 | value += addend;
|
|---|
| 249 |
|
|---|
| 250 | if ((long) value > 0x7fff || (long) value < -0x8000)
|
|---|
| 251 | return bfd_reloc_overflow;
|
|---|
| 252 |
|
|---|
| 253 | bfd_put_16 (input_bfd, value, hit_data);
|
|---|
| 254 | return bfd_reloc_ok;
|
|---|
| 255 |
|
|---|
| 256 | case R_MN10200_8:
|
|---|
| 257 | value += addend;
|
|---|
| 258 |
|
|---|
| 259 | if ((long) value > 0x7f || (long) value < -0x80)
|
|---|
| 260 | return bfd_reloc_overflow;
|
|---|
| 261 |
|
|---|
| 262 | bfd_put_8 (input_bfd, value, hit_data);
|
|---|
| 263 | return bfd_reloc_ok;
|
|---|
| 264 |
|
|---|
| 265 | case R_MN10200_24:
|
|---|
| 266 | value += addend;
|
|---|
| 267 |
|
|---|
| 268 | if ((long) value > 0x7fffff || (long) value < -0x800000)
|
|---|
| 269 | return bfd_reloc_overflow;
|
|---|
| 270 |
|
|---|
| 271 | value &= 0xffffff;
|
|---|
| 272 | value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
|
|---|
| 273 | bfd_put_32 (input_bfd, value, hit_data);
|
|---|
| 274 | return bfd_reloc_ok;
|
|---|
| 275 |
|
|---|
| 276 | case R_MN10200_PCREL8:
|
|---|
| 277 | value -= (input_section->output_section->vma
|
|---|
| 278 | + input_section->output_offset);
|
|---|
| 279 | value -= (offset + 1);
|
|---|
| 280 | value += addend;
|
|---|
| 281 |
|
|---|
| 282 | if ((long) value > 0xff || (long) value < -0x100)
|
|---|
| 283 | return bfd_reloc_overflow;
|
|---|
| 284 |
|
|---|
| 285 | bfd_put_8 (input_bfd, value, hit_data);
|
|---|
| 286 | return bfd_reloc_ok;
|
|---|
| 287 |
|
|---|
| 288 | case R_MN10200_PCREL16:
|
|---|
| 289 | value -= (input_section->output_section->vma
|
|---|
| 290 | + input_section->output_offset);
|
|---|
| 291 | value -= (offset + 2);
|
|---|
| 292 | value += addend;
|
|---|
| 293 |
|
|---|
| 294 | if ((long) value > 0xffff || (long) value < -0x10000)
|
|---|
| 295 | return bfd_reloc_overflow;
|
|---|
| 296 |
|
|---|
| 297 | bfd_put_16 (input_bfd, value, hit_data);
|
|---|
| 298 | return bfd_reloc_ok;
|
|---|
| 299 |
|
|---|
| 300 | case R_MN10200_PCREL24:
|
|---|
| 301 | value -= (input_section->output_section->vma
|
|---|
| 302 | + input_section->output_offset);
|
|---|
| 303 | value -= (offset + 3);
|
|---|
| 304 | value += addend;
|
|---|
| 305 |
|
|---|
| 306 | if ((long) value > 0xffffff || (long) value < -0x1000000)
|
|---|
| 307 | return bfd_reloc_overflow;
|
|---|
| 308 |
|
|---|
| 309 | value &= 0xffffff;
|
|---|
| 310 | value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
|
|---|
| 311 | bfd_put_32 (input_bfd, value, hit_data);
|
|---|
| 312 | return bfd_reloc_ok;
|
|---|
| 313 |
|
|---|
| 314 | default:
|
|---|
| 315 | return bfd_reloc_notsupported;
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 | |
|---|
| 319 |
|
|---|
| 320 | /* Relocate an MN10200 ELF section. */
|
|---|
| 321 | static boolean
|
|---|
| 322 | mn10200_elf_relocate_section (output_bfd, info, input_bfd, input_section,
|
|---|
| 323 | contents, relocs, local_syms, local_sections)
|
|---|
| 324 | bfd *output_bfd;
|
|---|
| 325 | struct bfd_link_info *info;
|
|---|
| 326 | bfd *input_bfd;
|
|---|
| 327 | asection *input_section;
|
|---|
| 328 | bfd_byte *contents;
|
|---|
| 329 | Elf_Internal_Rela *relocs;
|
|---|
| 330 | Elf_Internal_Sym *local_syms;
|
|---|
| 331 | asection **local_sections;
|
|---|
| 332 | {
|
|---|
| 333 | Elf_Internal_Shdr *symtab_hdr;
|
|---|
| 334 | struct elf_link_hash_entry **sym_hashes;
|
|---|
| 335 | Elf_Internal_Rela *rel, *relend;
|
|---|
| 336 |
|
|---|
| 337 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
|
|---|
| 338 | sym_hashes = elf_sym_hashes (input_bfd);
|
|---|
| 339 |
|
|---|
| 340 | rel = relocs;
|
|---|
| 341 | relend = relocs + input_section->reloc_count;
|
|---|
| 342 | for (; rel < relend; rel++)
|
|---|
| 343 | {
|
|---|
| 344 | int r_type;
|
|---|
| 345 | reloc_howto_type *howto;
|
|---|
| 346 | unsigned long r_symndx;
|
|---|
| 347 | Elf_Internal_Sym *sym;
|
|---|
| 348 | asection *sec;
|
|---|
| 349 | struct elf_link_hash_entry *h;
|
|---|
| 350 | bfd_vma relocation;
|
|---|
| 351 | bfd_reloc_status_type r;
|
|---|
| 352 |
|
|---|
| 353 | r_symndx = ELF32_R_SYM (rel->r_info);
|
|---|
| 354 | r_type = ELF32_R_TYPE (rel->r_info);
|
|---|
| 355 | howto = elf_mn10200_howto_table + r_type;
|
|---|
| 356 |
|
|---|
| 357 | if (info->relocateable)
|
|---|
| 358 | {
|
|---|
| 359 | /* This is a relocateable link. We don't have to change
|
|---|
| 360 | anything, unless the reloc is against a section symbol,
|
|---|
| 361 | in which case we have to adjust according to where the
|
|---|
| 362 | section symbol winds up in the output section. */
|
|---|
| 363 | if (r_symndx < symtab_hdr->sh_info)
|
|---|
| 364 | {
|
|---|
| 365 | sym = local_syms + r_symndx;
|
|---|
| 366 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
|
|---|
| 367 | {
|
|---|
| 368 | sec = local_sections[r_symndx];
|
|---|
| 369 | rel->r_addend += sec->output_offset + sym->st_value;
|
|---|
| 370 | }
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | continue;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | /* This is a final link. */
|
|---|
| 377 | h = NULL;
|
|---|
| 378 | sym = NULL;
|
|---|
| 379 | sec = NULL;
|
|---|
| 380 | if (r_symndx < symtab_hdr->sh_info)
|
|---|
| 381 | {
|
|---|
| 382 | sym = local_syms + r_symndx;
|
|---|
| 383 | sec = local_sections[r_symndx];
|
|---|
| 384 | relocation = (sec->output_section->vma
|
|---|
| 385 | + sec->output_offset
|
|---|
| 386 | + sym->st_value);
|
|---|
| 387 | }
|
|---|
| 388 | else
|
|---|
| 389 | {
|
|---|
| 390 | h = sym_hashes[r_symndx - symtab_hdr->sh_info];
|
|---|
| 391 | while (h->root.type == bfd_link_hash_indirect
|
|---|
| 392 | || h->root.type == bfd_link_hash_warning)
|
|---|
| 393 | h = (struct elf_link_hash_entry *) h->root.u.i.link;
|
|---|
| 394 | if (h->root.type == bfd_link_hash_defined
|
|---|
| 395 | || h->root.type == bfd_link_hash_defweak)
|
|---|
| 396 | {
|
|---|
| 397 | sec = h->root.u.def.section;
|
|---|
| 398 | relocation = (h->root.u.def.value
|
|---|
| 399 | + sec->output_section->vma
|
|---|
| 400 | + sec->output_offset);
|
|---|
| 401 | }
|
|---|
| 402 | else if (h->root.type == bfd_link_hash_undefweak)
|
|---|
| 403 | relocation = 0;
|
|---|
| 404 | else
|
|---|
| 405 | {
|
|---|
| 406 | if (! ((*info->callbacks->undefined_symbol)
|
|---|
| 407 | (info, h->root.root.string, input_bfd,
|
|---|
| 408 | input_section, rel->r_offset, true)))
|
|---|
| 409 | return false;
|
|---|
| 410 | relocation = 0;
|
|---|
| 411 | }
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
|
|---|
| 415 | input_section,
|
|---|
| 416 | contents, rel->r_offset,
|
|---|
| 417 | relocation, rel->r_addend,
|
|---|
| 418 | info, sec, h == NULL);
|
|---|
| 419 |
|
|---|
| 420 | if (r != bfd_reloc_ok)
|
|---|
| 421 | {
|
|---|
| 422 | const char *name;
|
|---|
| 423 | const char *msg = (const char *) 0;
|
|---|
| 424 |
|
|---|
| 425 | if (h != NULL)
|
|---|
| 426 | name = h->root.root.string;
|
|---|
| 427 | else
|
|---|
| 428 | {
|
|---|
| 429 | name = (bfd_elf_string_from_elf_section
|
|---|
| 430 | (input_bfd, symtab_hdr->sh_link, sym->st_name));
|
|---|
| 431 | if (name == NULL || *name == '\0')
|
|---|
| 432 | name = bfd_section_name (input_bfd, sec);
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | switch (r)
|
|---|
| 436 | {
|
|---|
| 437 | case bfd_reloc_overflow:
|
|---|
| 438 | if (! ((*info->callbacks->reloc_overflow)
|
|---|
| 439 | (info, name, howto->name, (bfd_vma) 0,
|
|---|
| 440 | input_bfd, input_section, rel->r_offset)))
|
|---|
| 441 | return false;
|
|---|
| 442 | break;
|
|---|
| 443 |
|
|---|
| 444 | case bfd_reloc_undefined:
|
|---|
| 445 | if (! ((*info->callbacks->undefined_symbol)
|
|---|
| 446 | (info, name, input_bfd, input_section,
|
|---|
| 447 | rel->r_offset, true)))
|
|---|
| 448 | return false;
|
|---|
| 449 | break;
|
|---|
| 450 |
|
|---|
| 451 | case bfd_reloc_outofrange:
|
|---|
| 452 | msg = _("internal error: out of range error");
|
|---|
| 453 | goto common_error;
|
|---|
| 454 |
|
|---|
| 455 | case bfd_reloc_notsupported:
|
|---|
| 456 | msg = _("internal error: unsupported relocation error");
|
|---|
| 457 | goto common_error;
|
|---|
| 458 |
|
|---|
| 459 | case bfd_reloc_dangerous:
|
|---|
| 460 | msg = _("internal error: dangerous error");
|
|---|
| 461 | goto common_error;
|
|---|
| 462 |
|
|---|
| 463 | default:
|
|---|
| 464 | msg = _("internal error: unknown error");
|
|---|
| 465 | /* fall through */
|
|---|
| 466 |
|
|---|
| 467 | common_error:
|
|---|
| 468 | if (!((*info->callbacks->warning)
|
|---|
| 469 | (info, msg, name, input_bfd, input_section,
|
|---|
| 470 | rel->r_offset)))
|
|---|
| 471 | return false;
|
|---|
| 472 | break;
|
|---|
| 473 | }
|
|---|
| 474 | }
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | return true;
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 | /* This function handles relaxing for the mn10200.
|
|---|
| 481 |
|
|---|
| 482 | There's quite a few relaxing opportunites available on the mn10200:
|
|---|
| 483 |
|
|---|
| 484 | * jsr:24 -> jsr:16 2 bytes
|
|---|
| 485 |
|
|---|
| 486 | * jmp:24 -> jmp:16 2 bytes
|
|---|
| 487 | * jmp:16 -> bra:8 1 byte
|
|---|
| 488 |
|
|---|
| 489 | * If the previous instruction is a conditional branch
|
|---|
| 490 | around the jump/bra, we may be able to reverse its condition
|
|---|
| 491 | and change its target to the jump's target. The jump/bra
|
|---|
| 492 | can then be deleted. 2 bytes
|
|---|
| 493 |
|
|---|
| 494 | * mov abs24 -> mov abs16 2 byte savings
|
|---|
| 495 |
|
|---|
| 496 | * Most instructions which accept imm24 can relax to imm16 2 bytes
|
|---|
| 497 | - Most instructions which accept imm16 can relax to imm8 1 byte
|
|---|
| 498 |
|
|---|
| 499 | * Most instructions which accept d24 can relax to d16 2 bytes
|
|---|
| 500 | - Most instructions which accept d16 can relax to d8 1 byte
|
|---|
| 501 |
|
|---|
| 502 | abs24, imm24, d24 all look the same at the reloc level. It
|
|---|
| 503 | might make the code simpler if we had different relocs for
|
|---|
| 504 | the various relaxable operand types.
|
|---|
| 505 |
|
|---|
| 506 | We don't handle imm16->imm8 or d16->d8 as they're very rare
|
|---|
| 507 | and somewhat more difficult to support. */
|
|---|
| 508 |
|
|---|
| 509 | static boolean
|
|---|
| 510 | mn10200_elf_relax_section (abfd, sec, link_info, again)
|
|---|
| 511 | bfd *abfd;
|
|---|
| 512 | asection *sec;
|
|---|
| 513 | struct bfd_link_info *link_info;
|
|---|
| 514 | boolean *again;
|
|---|
| 515 | {
|
|---|
| 516 | Elf_Internal_Shdr *symtab_hdr;
|
|---|
| 517 | Elf_Internal_Rela *internal_relocs;
|
|---|
| 518 | Elf_Internal_Rela *free_relocs = NULL;
|
|---|
| 519 | Elf_Internal_Rela *irel, *irelend;
|
|---|
| 520 | bfd_byte *contents = NULL;
|
|---|
| 521 | bfd_byte *free_contents = NULL;
|
|---|
| 522 | Elf32_External_Sym *extsyms = NULL;
|
|---|
| 523 | Elf32_External_Sym *free_extsyms = NULL;
|
|---|
| 524 |
|
|---|
| 525 | /* Assume nothing changes. */
|
|---|
| 526 | *again = false;
|
|---|
| 527 |
|
|---|
| 528 | /* We don't have to do anything for a relocateable link, if
|
|---|
| 529 | this section does not have relocs, or if this is not a
|
|---|
| 530 | code section. */
|
|---|
| 531 | if (link_info->relocateable
|
|---|
| 532 | || (sec->flags & SEC_RELOC) == 0
|
|---|
| 533 | || sec->reloc_count == 0
|
|---|
| 534 | || (sec->flags & SEC_CODE) == 0)
|
|---|
| 535 | return true;
|
|---|
| 536 |
|
|---|
| 537 | /* If this is the first time we have been called for this section,
|
|---|
| 538 | initialize the cooked size. */
|
|---|
| 539 | if (sec->_cooked_size == 0)
|
|---|
| 540 | sec->_cooked_size = sec->_raw_size;
|
|---|
| 541 |
|
|---|
| 542 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
|
|---|
| 543 |
|
|---|
| 544 | /* Get a copy of the native relocations. */
|
|---|
| 545 | internal_relocs = (_bfd_elf32_link_read_relocs
|
|---|
| 546 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
|
|---|
| 547 | link_info->keep_memory));
|
|---|
| 548 | if (internal_relocs == NULL)
|
|---|
| 549 | goto error_return;
|
|---|
| 550 | if (! link_info->keep_memory)
|
|---|
| 551 | free_relocs = internal_relocs;
|
|---|
| 552 |
|
|---|
| 553 | /* Walk through them looking for relaxing opportunities. */
|
|---|
| 554 | irelend = internal_relocs + sec->reloc_count;
|
|---|
| 555 | for (irel = internal_relocs; irel < irelend; irel++)
|
|---|
| 556 | {
|
|---|
| 557 | bfd_vma symval;
|
|---|
| 558 |
|
|---|
| 559 | /* If this isn't something that can be relaxed, then ignore
|
|---|
| 560 | this reloc. */
|
|---|
| 561 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
|
|---|
| 562 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
|
|---|
| 563 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
|
|---|
| 564 | continue;
|
|---|
| 565 |
|
|---|
| 566 | /* Get the section contents if we haven't done so already. */
|
|---|
| 567 | if (contents == NULL)
|
|---|
| 568 | {
|
|---|
| 569 | /* Get cached copy if it exists. */
|
|---|
| 570 | if (elf_section_data (sec)->this_hdr.contents != NULL)
|
|---|
| 571 | contents = elf_section_data (sec)->this_hdr.contents;
|
|---|
| 572 | else
|
|---|
| 573 | {
|
|---|
| 574 | /* Go get them off disk. */
|
|---|
| 575 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
|
|---|
| 576 | if (contents == NULL)
|
|---|
| 577 | goto error_return;
|
|---|
| 578 | free_contents = contents;
|
|---|
| 579 |
|
|---|
| 580 | if (! bfd_get_section_contents (abfd, sec, contents,
|
|---|
| 581 | (file_ptr) 0, sec->_raw_size))
|
|---|
| 582 | goto error_return;
|
|---|
| 583 | }
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | /* Read this BFD's symbols if we haven't done so already. */
|
|---|
| 587 | if (extsyms == NULL)
|
|---|
| 588 | {
|
|---|
| 589 | /* Get cached copy if it exists. */
|
|---|
| 590 | if (symtab_hdr->contents != NULL)
|
|---|
| 591 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
|
|---|
| 592 | else
|
|---|
| 593 | {
|
|---|
| 594 | /* Go get them off disk. */
|
|---|
| 595 | extsyms = ((Elf32_External_Sym *)
|
|---|
| 596 | bfd_malloc (symtab_hdr->sh_size));
|
|---|
| 597 | if (extsyms == NULL)
|
|---|
| 598 | goto error_return;
|
|---|
| 599 | free_extsyms = extsyms;
|
|---|
| 600 | if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
|
|---|
| 601 | || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
|
|---|
| 602 | != symtab_hdr->sh_size))
|
|---|
| 603 | goto error_return;
|
|---|
| 604 | }
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | /* Get the value of the symbol referred to by the reloc. */
|
|---|
| 608 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
|
|---|
| 609 | {
|
|---|
| 610 | Elf_Internal_Sym isym;
|
|---|
| 611 | asection *sym_sec;
|
|---|
| 612 |
|
|---|
| 613 | /* A local symbol. */
|
|---|
| 614 | bfd_elf32_swap_symbol_in (abfd,
|
|---|
| 615 | extsyms + ELF32_R_SYM (irel->r_info),
|
|---|
| 616 | &isym);
|
|---|
| 617 |
|
|---|
| 618 | sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
|
|---|
| 619 | symval = (isym.st_value
|
|---|
| 620 | + sym_sec->output_section->vma
|
|---|
| 621 | + sym_sec->output_offset);
|
|---|
| 622 | }
|
|---|
| 623 | else
|
|---|
| 624 | {
|
|---|
| 625 | unsigned long indx;
|
|---|
| 626 | struct elf_link_hash_entry *h;
|
|---|
| 627 |
|
|---|
| 628 | /* An external symbol. */
|
|---|
| 629 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
|
|---|
| 630 | h = elf_sym_hashes (abfd)[indx];
|
|---|
| 631 | BFD_ASSERT (h != NULL);
|
|---|
| 632 | if (h->root.type != bfd_link_hash_defined
|
|---|
| 633 | && h->root.type != bfd_link_hash_defweak)
|
|---|
| 634 | {
|
|---|
| 635 | /* This appears to be a reference to an undefined
|
|---|
| 636 | symbol. Just ignore it--it will be caught by the
|
|---|
| 637 | regular reloc processing. */
|
|---|
| 638 | continue;
|
|---|
| 639 | }
|
|---|
| 640 |
|
|---|
| 641 | symval = (h->root.u.def.value
|
|---|
| 642 | + h->root.u.def.section->output_section->vma
|
|---|
| 643 | + h->root.u.def.section->output_offset);
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | /* For simplicity of coding, we are going to modify the section
|
|---|
| 647 | contents, the section relocs, and the BFD symbol table. We
|
|---|
| 648 | must tell the rest of the code not to free up this
|
|---|
| 649 | information. It would be possible to instead create a table
|
|---|
| 650 | of changes which have to be made, as is done in coff-mips.c;
|
|---|
| 651 | that would be more work, but would require less memory when
|
|---|
| 652 | the linker is run. */
|
|---|
| 653 |
|
|---|
| 654 | /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
|
|---|
| 655 | branch/call. */
|
|---|
| 656 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
|
|---|
| 657 | {
|
|---|
| 658 | bfd_vma value = symval;
|
|---|
| 659 |
|
|---|
| 660 | /* Deal with pc-relative gunk. */
|
|---|
| 661 | value -= (sec->output_section->vma + sec->output_offset);
|
|---|
| 662 | value -= (irel->r_offset + 3);
|
|---|
| 663 | value += irel->r_addend;
|
|---|
| 664 |
|
|---|
| 665 | /* See if the value will fit in 16 bits, note the high value is
|
|---|
| 666 | 0x7fff + 2 as the target will be two bytes closer if we are
|
|---|
| 667 | able to relax. */
|
|---|
| 668 | if ((long) value < 0x8001 && (long) value > -0x8000)
|
|---|
| 669 | {
|
|---|
| 670 | unsigned char code;
|
|---|
| 671 |
|
|---|
| 672 | /* Get the opcode. */
|
|---|
| 673 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
|
|---|
| 674 |
|
|---|
| 675 | if (code != 0xe0 && code != 0xe1)
|
|---|
| 676 | continue;
|
|---|
| 677 |
|
|---|
| 678 | /* Note that we've changed the relocs, section contents, etc. */
|
|---|
| 679 | elf_section_data (sec)->relocs = internal_relocs;
|
|---|
| 680 | free_relocs = NULL;
|
|---|
| 681 |
|
|---|
| 682 | elf_section_data (sec)->this_hdr.contents = contents;
|
|---|
| 683 | free_contents = NULL;
|
|---|
| 684 |
|
|---|
| 685 | symtab_hdr->contents = (bfd_byte *) extsyms;
|
|---|
| 686 | free_extsyms = NULL;
|
|---|
| 687 |
|
|---|
| 688 | /* Fix the opcode. */
|
|---|
| 689 | if (code == 0xe0)
|
|---|
| 690 | bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
|
|---|
| 691 | else if (code == 0xe1)
|
|---|
| 692 | bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
|
|---|
| 693 |
|
|---|
| 694 | /* Fix the relocation's type. */
|
|---|
| 695 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
|
|---|
| 696 | R_MN10200_PCREL16);
|
|---|
| 697 |
|
|---|
| 698 | /* The opcode got shorter too, so we have to fix the offset. */
|
|---|
| 699 | irel->r_offset -= 1;
|
|---|
| 700 |
|
|---|
| 701 | /* Delete two bytes of data. */
|
|---|
| 702 | if (!mn10200_elf_relax_delete_bytes (abfd, sec,
|
|---|
| 703 | irel->r_offset + 1, 2))
|
|---|
| 704 | goto error_return;
|
|---|
| 705 |
|
|---|
| 706 | /* That will change things, so, we should relax again.
|
|---|
| 707 | Note that this is not required, and it may be slow. */
|
|---|
| 708 | *again = true;
|
|---|
| 709 | }
|
|---|
| 710 | }
|
|---|
| 711 |
|
|---|
| 712 | /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
|
|---|
| 713 | branch. */
|
|---|
| 714 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
|
|---|
| 715 | {
|
|---|
| 716 | bfd_vma value = symval;
|
|---|
| 717 |
|
|---|
| 718 | /* Deal with pc-relative gunk. */
|
|---|
| 719 | value -= (sec->output_section->vma + sec->output_offset);
|
|---|
| 720 | value -= (irel->r_offset + 2);
|
|---|
| 721 | value += irel->r_addend;
|
|---|
| 722 |
|
|---|
| 723 | /* See if the value will fit in 8 bits, note the high value is
|
|---|
| 724 | 0x7f + 1 as the target will be one bytes closer if we are
|
|---|
| 725 | able to relax. */
|
|---|
| 726 | if ((long) value < 0x80 && (long) value > -0x80)
|
|---|
| 727 | {
|
|---|
| 728 | unsigned char code;
|
|---|
| 729 |
|
|---|
| 730 | /* Get the opcode. */
|
|---|
| 731 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
|
|---|
| 732 |
|
|---|
| 733 | if (code != 0xfc)
|
|---|
| 734 | continue;
|
|---|
| 735 |
|
|---|
| 736 | /* Note that we've changed the relocs, section contents, etc. */
|
|---|
| 737 | elf_section_data (sec)->relocs = internal_relocs;
|
|---|
| 738 | free_relocs = NULL;
|
|---|
| 739 |
|
|---|
| 740 | elf_section_data (sec)->this_hdr.contents = contents;
|
|---|
| 741 | free_contents = NULL;
|
|---|
| 742 |
|
|---|
| 743 | symtab_hdr->contents = (bfd_byte *) extsyms;
|
|---|
| 744 | free_extsyms = NULL;
|
|---|
| 745 |
|
|---|
| 746 | /* Fix the opcode. */
|
|---|
| 747 | bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
|
|---|
| 748 |
|
|---|
| 749 | /* Fix the relocation's type. */
|
|---|
| 750 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
|
|---|
| 751 | R_MN10200_PCREL8);
|
|---|
| 752 |
|
|---|
| 753 | /* Delete one byte of data. */
|
|---|
| 754 | if (!mn10200_elf_relax_delete_bytes (abfd, sec,
|
|---|
| 755 | irel->r_offset + 1, 1))
|
|---|
| 756 | goto error_return;
|
|---|
| 757 |
|
|---|
| 758 | /* That will change things, so, we should relax again.
|
|---|
| 759 | Note that this is not required, and it may be slow. */
|
|---|
| 760 | *again = true;
|
|---|
| 761 | }
|
|---|
| 762 | }
|
|---|
| 763 |
|
|---|
| 764 | /* Try to eliminate an unconditional 8 bit pc-relative branch
|
|---|
| 765 | which immediately follows a conditional 8 bit pc-relative
|
|---|
| 766 | branch around the unconditional branch.
|
|---|
| 767 |
|
|---|
| 768 | original: new:
|
|---|
| 769 | bCC lab1 bCC' lab2
|
|---|
| 770 | bra lab2
|
|---|
| 771 | lab1: lab1:
|
|---|
| 772 |
|
|---|
| 773 | This happens when the bCC can't reach lab2 at assembly time,
|
|---|
| 774 | but due to other relaxations it can reach at link time. */
|
|---|
| 775 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
|
|---|
| 776 | {
|
|---|
| 777 | Elf_Internal_Rela *nrel;
|
|---|
| 778 | bfd_vma value = symval;
|
|---|
| 779 | unsigned char code;
|
|---|
| 780 |
|
|---|
| 781 | /* Deal with pc-relative gunk. */
|
|---|
| 782 | value -= (sec->output_section->vma + sec->output_offset);
|
|---|
| 783 | value -= (irel->r_offset + 1);
|
|---|
| 784 | value += irel->r_addend;
|
|---|
| 785 |
|
|---|
| 786 | /* Do nothing if this reloc is the last byte in the section. */
|
|---|
| 787 | if (irel->r_offset == sec->_cooked_size)
|
|---|
| 788 | continue;
|
|---|
| 789 |
|
|---|
| 790 | /* See if the next instruction is an unconditional pc-relative
|
|---|
| 791 | branch, more often than not this test will fail, so we
|
|---|
| 792 | test it first to speed things up. */
|
|---|
| 793 | code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
|
|---|
| 794 | if (code != 0xea)
|
|---|
| 795 | continue;
|
|---|
| 796 |
|
|---|
| 797 | /* Also make sure the next relocation applies to the next
|
|---|
| 798 | instruction and that it's a pc-relative 8 bit branch. */
|
|---|
| 799 | nrel = irel + 1;
|
|---|
| 800 | if (nrel == irelend
|
|---|
| 801 | || irel->r_offset + 2 != nrel->r_offset
|
|---|
| 802 | || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
|
|---|
| 803 | continue;
|
|---|
| 804 |
|
|---|
| 805 | /* Make sure our destination immediately follows the
|
|---|
| 806 | unconditional branch. */
|
|---|
| 807 | if (symval != (sec->output_section->vma + sec->output_offset
|
|---|
| 808 | + irel->r_offset + 3))
|
|---|
| 809 | continue;
|
|---|
| 810 |
|
|---|
| 811 | /* Now make sure we are a conditional branch. This may not
|
|---|
| 812 | be necessary, but why take the chance.
|
|---|
| 813 |
|
|---|
| 814 | Note these checks assume that R_MN10200_PCREL8 relocs
|
|---|
| 815 | only occur on bCC and bCCx insns. If they occured
|
|---|
| 816 | elsewhere, we'd need to know the start of this insn
|
|---|
| 817 | for this check to be accurate. */
|
|---|
| 818 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
|
|---|
| 819 | if (code != 0xe0 && code != 0xe1 && code != 0xe2
|
|---|
| 820 | && code != 0xe3 && code != 0xe4 && code != 0xe5
|
|---|
| 821 | && code != 0xe6 && code != 0xe7 && code != 0xe8
|
|---|
| 822 | && code != 0xe9 && code != 0xec && code != 0xed
|
|---|
| 823 | && code != 0xee && code != 0xef && code != 0xfc
|
|---|
| 824 | && code != 0xfd && code != 0xfe && code != 0xff)
|
|---|
| 825 | continue;
|
|---|
| 826 |
|
|---|
| 827 | /* We also have to be sure there is no symbol/label
|
|---|
| 828 | at the unconditional branch. */
|
|---|
| 829 | if (mn10200_elf_symbol_address_p (abfd, sec, extsyms,
|
|---|
| 830 | irel->r_offset + 1))
|
|---|
| 831 | continue;
|
|---|
| 832 |
|
|---|
| 833 | /* Note that we've changed the relocs, section contents, etc. */
|
|---|
| 834 | elf_section_data (sec)->relocs = internal_relocs;
|
|---|
| 835 | free_relocs = NULL;
|
|---|
| 836 |
|
|---|
| 837 | elf_section_data (sec)->this_hdr.contents = contents;
|
|---|
| 838 | free_contents = NULL;
|
|---|
| 839 |
|
|---|
| 840 | symtab_hdr->contents = (bfd_byte *) extsyms;
|
|---|
| 841 | free_extsyms = NULL;
|
|---|
| 842 |
|
|---|
| 843 | /* Reverse the condition of the first branch. */
|
|---|
| 844 | switch (code)
|
|---|
| 845 | {
|
|---|
| 846 | case 0xfc:
|
|---|
| 847 | code = 0xfd;
|
|---|
| 848 | break;
|
|---|
| 849 | case 0xfd:
|
|---|
| 850 | code = 0xfc;
|
|---|
| 851 | break;
|
|---|
| 852 | case 0xfe:
|
|---|
| 853 | code = 0xff;
|
|---|
| 854 | break;
|
|---|
| 855 | case 0xff:
|
|---|
| 856 | code = 0xfe;
|
|---|
| 857 | break;
|
|---|
| 858 | case 0xe8:
|
|---|
| 859 | code = 0xe9;
|
|---|
| 860 | break;
|
|---|
| 861 | case 0xe9:
|
|---|
| 862 | code = 0xe8;
|
|---|
| 863 | break;
|
|---|
| 864 | case 0xe0:
|
|---|
| 865 | code = 0xe2;
|
|---|
| 866 | break;
|
|---|
| 867 | case 0xe2:
|
|---|
| 868 | code = 0xe0;
|
|---|
| 869 | break;
|
|---|
| 870 | case 0xe3:
|
|---|
| 871 | code = 0xe1;
|
|---|
| 872 | break;
|
|---|
| 873 | case 0xe1:
|
|---|
| 874 | code = 0xe3;
|
|---|
| 875 | break;
|
|---|
| 876 | case 0xe4:
|
|---|
| 877 | code = 0xe6;
|
|---|
| 878 | break;
|
|---|
| 879 | case 0xe6:
|
|---|
| 880 | code = 0xe4;
|
|---|
| 881 | break;
|
|---|
| 882 | case 0xe7:
|
|---|
| 883 | code = 0xe5;
|
|---|
| 884 | break;
|
|---|
| 885 | case 0xe5:
|
|---|
| 886 | code = 0xe7;
|
|---|
| 887 | break;
|
|---|
| 888 | case 0xec:
|
|---|
| 889 | code = 0xed;
|
|---|
| 890 | break;
|
|---|
| 891 | case 0xed:
|
|---|
| 892 | code = 0xec;
|
|---|
| 893 | break;
|
|---|
| 894 | case 0xee:
|
|---|
| 895 | code = 0xef;
|
|---|
| 896 | break;
|
|---|
| 897 | case 0xef:
|
|---|
| 898 | code = 0xee;
|
|---|
| 899 | break;
|
|---|
| 900 | }
|
|---|
| 901 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
|
|---|
| 902 |
|
|---|
| 903 | /* Set the reloc type and symbol for the first branch
|
|---|
| 904 | from the second branch. */
|
|---|
| 905 | irel->r_info = nrel->r_info;
|
|---|
| 906 |
|
|---|
| 907 | /* Make the reloc for the second branch a null reloc. */
|
|---|
| 908 | nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
|
|---|
| 909 | R_MN10200_NONE);
|
|---|
| 910 |
|
|---|
| 911 | /* Delete two bytes of data. */
|
|---|
| 912 | if (!mn10200_elf_relax_delete_bytes (abfd, sec,
|
|---|
| 913 | irel->r_offset + 1, 2))
|
|---|
| 914 | goto error_return;
|
|---|
| 915 |
|
|---|
| 916 | /* That will change things, so, we should relax again.
|
|---|
| 917 | Note that this is not required, and it may be slow. */
|
|---|
| 918 | *again = true;
|
|---|
| 919 | }
|
|---|
| 920 |
|
|---|
| 921 | /* Try to turn a 24bit immediate, displacement or absolute address
|
|---|
| 922 | into a 16bit immediate, displacement or absolute address. */
|
|---|
| 923 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24)
|
|---|
| 924 | {
|
|---|
| 925 | bfd_vma value = symval;
|
|---|
| 926 |
|
|---|
| 927 | /* See if the value will fit in 16 bits.
|
|---|
| 928 | We allow any 16bit match here. We prune those we can't
|
|---|
| 929 | handle below. */
|
|---|
| 930 | if ((long) value < 0x7fff && (long) value > -0x8000)
|
|---|
| 931 | {
|
|---|
| 932 | unsigned char code;
|
|---|
| 933 |
|
|---|
| 934 | /* All insns which have 24bit operands are 5 bytes long,
|
|---|
| 935 | the first byte will always be 0xf4, but we double check
|
|---|
| 936 | it just in case. */
|
|---|
| 937 |
|
|---|
| 938 | /* Get the first opcode. */
|
|---|
| 939 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
|
|---|
| 940 |
|
|---|
| 941 | if (code != 0xf4)
|
|---|
| 942 | continue;
|
|---|
| 943 |
|
|---|
| 944 | /* Get the second opcode. */
|
|---|
| 945 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
|
|---|
| 946 |
|
|---|
| 947 | switch (code & 0xfc)
|
|---|
| 948 | {
|
|---|
| 949 | /* mov imm24,dn -> mov imm16,dn */
|
|---|
| 950 | case 0x70:
|
|---|
| 951 | /* Not safe if the high bit is on as relaxing may
|
|---|
| 952 | move the value out of high mem and thus not fit
|
|---|
| 953 | in a signed 16bit value. */
|
|---|
| 954 | if (value & 0x8000)
|
|---|
| 955 | continue;
|
|---|
| 956 |
|
|---|
| 957 | /* Note that we've changed the reldection contents, etc. */
|
|---|
| 958 | elf_section_data (sec)->relocs = internal_relocs;
|
|---|
| 959 | free_relocs = NULL;
|
|---|
| 960 |
|
|---|
| 961 | elf_section_data (sec)->this_hdr.contents = contents;
|
|---|
| 962 | free_contents = NULL;
|
|---|
| 963 |
|
|---|
| 964 | symtab_hdr->contents = (bfd_byte *) extsyms;
|
|---|
| 965 | free_extsyms = NULL;
|
|---|
| 966 |
|
|---|
| 967 | /* Fix the opcode. */
|
|---|
| 968 | bfd_put_8 (abfd, 0xf8 + (code & 0x03),
|
|---|
| 969 | contents + irel->r_offset - 2);
|
|---|
| 970 |
|
|---|
| 971 | /* Fix the relocation's type. */
|
|---|
| 972 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
|
|---|
| 973 | R_MN10200_16);
|
|---|
| 974 |
|
|---|
| 975 | /* The opcode got shorter too, so we have to fix the
|
|---|
| 976 | offset. */
|
|---|
| 977 | irel->r_offset -= 1;
|
|---|
| 978 |
|
|---|
| 979 | /* Delete two bytes of data. */
|
|---|
| 980 | if (!mn10200_elf_relax_delete_bytes (abfd, sec,
|
|---|
| 981 | irel->r_offset + 1, 2))
|
|---|
| 982 | goto error_return;
|
|---|
| 983 |
|
|---|
| 984 | /* That will change things, so, we should relax again.
|
|---|
| 985 | Note that this is not required, and it may be slow. */
|
|---|
| 986 | *again = true;
|
|---|
| 987 | break;
|
|---|
| 988 |
|
|---|
| 989 | /* mov imm24,an -> mov imm16,an
|
|---|
| 990 | cmp imm24,an -> cmp imm16,an
|
|---|
| 991 | mov (abs24),dn -> mov (abs16),dn
|
|---|
| 992 | mov dn,(abs24) -> mov dn,(abs16)
|
|---|
| 993 | movb dn,(abs24) -> movb dn,(abs16)
|
|---|
| 994 | movbu (abs24),dn -> movbu (abs16),dn */
|
|---|
| 995 | case 0x74:
|
|---|
| 996 | case 0x7c:
|
|---|
| 997 | case 0xc0:
|
|---|
| 998 | case 0x40:
|
|---|
| 999 | case 0x44:
|
|---|
| 1000 | case 0xc8:
|
|---|
| 1001 | /* Note that we've changed the reldection contents, etc. */
|
|---|
| 1002 | elf_section_data (sec)->relocs = internal_relocs;
|
|---|
| 1003 | free_relocs = NULL;
|
|---|
| 1004 |
|
|---|
| 1005 | elf_section_data (sec)->this_hdr.contents = contents;
|
|---|
| 1006 | free_contents = NULL;
|
|---|
| 1007 |
|
|---|
| 1008 | symtab_hdr->contents = (bfd_byte *) extsyms;
|
|---|
| 1009 | free_extsyms = NULL;
|
|---|
| 1010 |
|
|---|
| 1011 | if ((code & 0xfc) == 0x74)
|
|---|
| 1012 | code = 0xdc + (code & 0x03);
|
|---|
| 1013 | else if ((code & 0xfc) == 0x7c)
|
|---|
| 1014 | code = 0xec + (code & 0x03);
|
|---|
| 1015 | else if ((code & 0xfc) == 0xc0)
|
|---|
| 1016 | code = 0xc8 + (code & 0x03);
|
|---|
| 1017 | else if ((code & 0xfc) == 0x40)
|
|---|
| 1018 | code = 0xc0 + (code & 0x03);
|
|---|
| 1019 | else if ((code & 0xfc) == 0x44)
|
|---|
| 1020 | code = 0xc4 + (code & 0x03);
|
|---|
| 1021 | else if ((code & 0xfc) == 0xc8)
|
|---|
| 1022 | code = 0xcc + (code & 0x03);
|
|---|
| 1023 |
|
|---|
| 1024 | /* Fix the opcode. */
|
|---|
| 1025 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
|
|---|
| 1026 |
|
|---|
| 1027 | /* Fix the relocation's type. */
|
|---|
| 1028 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
|
|---|
| 1029 | R_MN10200_16);
|
|---|
| 1030 |
|
|---|
| 1031 | /* The opcode got shorter too, so we have to fix the
|
|---|
| 1032 | offset. */
|
|---|
| 1033 | irel->r_offset -= 1;
|
|---|
| 1034 |
|
|---|
| 1035 | /* Delete two bytes of data. */
|
|---|
| 1036 | if (!mn10200_elf_relax_delete_bytes (abfd, sec,
|
|---|
| 1037 | irel->r_offset + 1, 2))
|
|---|
| 1038 | goto error_return;
|
|---|
| 1039 |
|
|---|
| 1040 | /* That will change things, so, we should relax again.
|
|---|
| 1041 | Note that this is not required, and it may be slow. */
|
|---|
| 1042 | *again = true;
|
|---|
| 1043 | break;
|
|---|
| 1044 |
|
|---|
| 1045 | /* cmp imm24,dn -> cmp imm16,dn
|
|---|
| 1046 | mov (abs24),an -> mov (abs16),an
|
|---|
| 1047 | mov an,(abs24) -> mov an,(abs16)
|
|---|
| 1048 | add imm24,dn -> add imm16,dn
|
|---|
| 1049 | add imm24,an -> add imm16,an
|
|---|
| 1050 | sub imm24,dn -> sub imm16,dn
|
|---|
| 1051 | sub imm24,an -> sub imm16,an
|
|---|
| 1052 | And all d24->d16 in memory ops. */
|
|---|
| 1053 | case 0x78:
|
|---|
| 1054 | case 0xd0:
|
|---|
| 1055 | case 0x50:
|
|---|
| 1056 | case 0x60:
|
|---|
| 1057 | case 0x64:
|
|---|
| 1058 | case 0x68:
|
|---|
| 1059 | case 0x6c:
|
|---|
| 1060 | case 0x80:
|
|---|
| 1061 | case 0xf0:
|
|---|
| 1062 | case 0x00:
|
|---|
| 1063 | case 0x10:
|
|---|
| 1064 | case 0xb0:
|
|---|
| 1065 | case 0x30:
|
|---|
| 1066 | case 0xa0:
|
|---|
| 1067 | case 0x20:
|
|---|
| 1068 | case 0x90:
|
|---|
| 1069 | /* Not safe if the high bit is on as relaxing may
|
|---|
| 1070 | move the value out of high mem and thus not fit
|
|---|
| 1071 | in a signed 16bit value. */
|
|---|
| 1072 | if (((code & 0xfc) == 0x78
|
|---|
| 1073 | || (code & 0xfc) == 0x60
|
|---|
| 1074 | || (code & 0xfc) == 0x64
|
|---|
| 1075 | || (code & 0xfc) == 0x68
|
|---|
| 1076 | || (code & 0xfc) == 0x6c
|
|---|
| 1077 | || (code & 0xfc) == 0x80
|
|---|
| 1078 | || (code & 0xfc) == 0xf0
|
|---|
| 1079 | || (code & 0xfc) == 0x00
|
|---|
| 1080 | || (code & 0xfc) == 0x10
|
|---|
| 1081 | || (code & 0xfc) == 0xb0
|
|---|
| 1082 | || (code & 0xfc) == 0x30
|
|---|
| 1083 | || (code & 0xfc) == 0xa0
|
|---|
| 1084 | || (code & 0xfc) == 0x20
|
|---|
| 1085 | || (code & 0xfc) == 0x90)
|
|---|
| 1086 | && (value & 0x8000) != 0)
|
|---|
| 1087 | continue;
|
|---|
| 1088 |
|
|---|
| 1089 | /* Note that we've changed the reldection contents, etc. */
|
|---|
| 1090 | elf_section_data (sec)->relocs = internal_relocs;
|
|---|
| 1091 | free_relocs = NULL;
|
|---|
| 1092 |
|
|---|
| 1093 | elf_section_data (sec)->this_hdr.contents = contents;
|
|---|
| 1094 | free_contents = NULL;
|
|---|
| 1095 |
|
|---|
| 1096 | symtab_hdr->contents = (bfd_byte *) extsyms;
|
|---|
| 1097 | free_extsyms = NULL;
|
|---|
| 1098 |
|
|---|
| 1099 | /* Fix the opcode. */
|
|---|
| 1100 | bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2);
|
|---|
| 1101 |
|
|---|
| 1102 | if ((code & 0xfc) == 0x78)
|
|---|
| 1103 | code = 0x48 + (code & 0x03);
|
|---|
| 1104 | else if ((code & 0xfc) == 0xd0)
|
|---|
| 1105 | code = 0x30 + (code & 0x03);
|
|---|
| 1106 | else if ((code & 0xfc) == 0x50)
|
|---|
| 1107 | code = 0x20 + (code & 0x03);
|
|---|
| 1108 | else if ((code & 0xfc) == 0x60)
|
|---|
| 1109 | code = 0x18 + (code & 0x03);
|
|---|
| 1110 | else if ((code & 0xfc) == 0x64)
|
|---|
| 1111 | code = 0x08 + (code & 0x03);
|
|---|
| 1112 | else if ((code & 0xfc) == 0x68)
|
|---|
| 1113 | code = 0x1c + (code & 0x03);
|
|---|
| 1114 | else if ((code & 0xfc) == 0x6c)
|
|---|
| 1115 | code = 0x0c + (code & 0x03);
|
|---|
| 1116 | else if ((code & 0xfc) == 0x80)
|
|---|
| 1117 | code = 0xc0 + (code & 0x07);
|
|---|
| 1118 | else if ((code & 0xfc) == 0xf0)
|
|---|
| 1119 | code = 0xb0 + (code & 0x07);
|
|---|
| 1120 | else if ((code & 0xfc) == 0x00)
|
|---|
| 1121 | code = 0x80 + (code & 0x07);
|
|---|
| 1122 | else if ((code & 0xfc) == 0x10)
|
|---|
| 1123 | code = 0xa0 + (code & 0x07);
|
|---|
| 1124 | else if ((code & 0xfc) == 0xb0)
|
|---|
| 1125 | code = 0x70 + (code & 0x07);
|
|---|
| 1126 | else if ((code & 0xfc) == 0x30)
|
|---|
| 1127 | code = 0x60 + (code & 0x07);
|
|---|
| 1128 | else if ((code & 0xfc) == 0xa0)
|
|---|
| 1129 | code = 0xd0 + (code & 0x07);
|
|---|
| 1130 | else if ((code & 0xfc) == 0x20)
|
|---|
| 1131 | code = 0x90 + (code & 0x07);
|
|---|
| 1132 | else if ((code & 0xfc) == 0x90)
|
|---|
| 1133 | code = 0x50 + (code & 0x07);
|
|---|
| 1134 |
|
|---|
| 1135 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
|
|---|
| 1136 |
|
|---|
| 1137 | /* Fix the relocation's type. */
|
|---|
| 1138 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
|
|---|
| 1139 | R_MN10200_16);
|
|---|
| 1140 |
|
|---|
| 1141 | /* Delete one bytes of data. */
|
|---|
| 1142 | if (!mn10200_elf_relax_delete_bytes (abfd, sec,
|
|---|
| 1143 | irel->r_offset + 2, 1))
|
|---|
| 1144 | goto error_return;
|
|---|
| 1145 |
|
|---|
| 1146 | /* That will change things, so, we should relax again.
|
|---|
| 1147 | Note that this is not required, and it may be slow. */
|
|---|
| 1148 | *again = true;
|
|---|
| 1149 | break;
|
|---|
| 1150 |
|
|---|
| 1151 | /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
|
|---|
| 1152 | case 0xc4:
|
|---|
| 1153 | /* Note that we've changed the reldection contents, etc. */
|
|---|
| 1154 | elf_section_data (sec)->relocs = internal_relocs;
|
|---|
| 1155 | free_relocs = NULL;
|
|---|
| 1156 |
|
|---|
| 1157 | elf_section_data (sec)->this_hdr.contents = contents;
|
|---|
| 1158 | free_contents = NULL;
|
|---|
| 1159 |
|
|---|
| 1160 | symtab_hdr->contents = (bfd_byte *) extsyms;
|
|---|
| 1161 | free_extsyms = NULL;
|
|---|
| 1162 |
|
|---|
| 1163 | bfd_put_8 (abfd, 0xcc + (code & 0x03),
|
|---|
| 1164 | contents + irel->r_offset - 2);
|
|---|
| 1165 |
|
|---|
| 1166 | bfd_put_8 (abfd, 0xb8 + (code & 0x03),
|
|---|
| 1167 | contents + irel->r_offset - 1);
|
|---|
| 1168 |
|
|---|
| 1169 | /* Fix the relocation's type. */
|
|---|
| 1170 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
|
|---|
| 1171 | R_MN10200_16);
|
|---|
| 1172 |
|
|---|
| 1173 | /* The reloc will be applied one byte in front of its
|
|---|
| 1174 | current location. */
|
|---|
| 1175 | irel->r_offset -= 1;
|
|---|
| 1176 |
|
|---|
| 1177 | /* Delete one bytes of data. */
|
|---|
| 1178 | if (!mn10200_elf_relax_delete_bytes (abfd, sec,
|
|---|
| 1179 | irel->r_offset + 2, 1))
|
|---|
| 1180 | goto error_return;
|
|---|
| 1181 |
|
|---|
| 1182 | /* That will change things, so, we should relax again.
|
|---|
| 1183 | Note that this is not required, and it may be slow. */
|
|---|
| 1184 | *again = true;
|
|---|
| 1185 | break;
|
|---|
| 1186 | }
|
|---|
| 1187 | }
|
|---|
| 1188 | }
|
|---|
| 1189 | }
|
|---|
| 1190 |
|
|---|
| 1191 | if (free_relocs != NULL)
|
|---|
| 1192 | {
|
|---|
| 1193 | free (free_relocs);
|
|---|
| 1194 | free_relocs = NULL;
|
|---|
| 1195 | }
|
|---|
| 1196 |
|
|---|
| 1197 | if (free_contents != NULL)
|
|---|
| 1198 | {
|
|---|
| 1199 | if (! link_info->keep_memory)
|
|---|
| 1200 | free (free_contents);
|
|---|
| 1201 | else
|
|---|
| 1202 | {
|
|---|
| 1203 | /* Cache the section contents for elf_link_input_bfd. */
|
|---|
| 1204 | elf_section_data (sec)->this_hdr.contents = contents;
|
|---|
| 1205 | }
|
|---|
| 1206 | free_contents = NULL;
|
|---|
| 1207 | }
|
|---|
| 1208 |
|
|---|
| 1209 | if (free_extsyms != NULL)
|
|---|
| 1210 | {
|
|---|
| 1211 | if (! link_info->keep_memory)
|
|---|
| 1212 | free (free_extsyms);
|
|---|
| 1213 | else
|
|---|
| 1214 | {
|
|---|
| 1215 | /* Cache the symbols for elf_link_input_bfd. */
|
|---|
| 1216 | symtab_hdr->contents = extsyms;
|
|---|
| 1217 | }
|
|---|
| 1218 | free_extsyms = NULL;
|
|---|
| 1219 | }
|
|---|
| 1220 |
|
|---|
| 1221 | return true;
|
|---|
| 1222 |
|
|---|
| 1223 | error_return:
|
|---|
| 1224 | if (free_relocs != NULL)
|
|---|
| 1225 | free (free_relocs);
|
|---|
| 1226 | if (free_contents != NULL)
|
|---|
| 1227 | free (free_contents);
|
|---|
| 1228 | if (free_extsyms != NULL)
|
|---|
| 1229 | free (free_extsyms);
|
|---|
| 1230 | return false;
|
|---|
| 1231 | }
|
|---|
| 1232 |
|
|---|
| 1233 | /* Delete some bytes from a section while relaxing. */
|
|---|
| 1234 |
|
|---|
| 1235 | static boolean
|
|---|
| 1236 | mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
|
|---|
| 1237 | bfd *abfd;
|
|---|
| 1238 | asection *sec;
|
|---|
| 1239 | bfd_vma addr;
|
|---|
| 1240 | int count;
|
|---|
| 1241 | {
|
|---|
| 1242 | Elf_Internal_Shdr *symtab_hdr;
|
|---|
| 1243 | Elf32_External_Sym *extsyms;
|
|---|
| 1244 | int shndx, index;
|
|---|
| 1245 | bfd_byte *contents;
|
|---|
| 1246 | Elf_Internal_Rela *irel, *irelend;
|
|---|
| 1247 | Elf_Internal_Rela *irelalign;
|
|---|
| 1248 | bfd_vma toaddr;
|
|---|
| 1249 | Elf32_External_Sym *esym, *esymend;
|
|---|
| 1250 | struct elf_link_hash_entry *sym_hash;
|
|---|
| 1251 |
|
|---|
| 1252 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
|
|---|
| 1253 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
|
|---|
| 1254 |
|
|---|
| 1255 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
|
|---|
| 1256 |
|
|---|
| 1257 | contents = elf_section_data (sec)->this_hdr.contents;
|
|---|
| 1258 |
|
|---|
| 1259 | /* The deletion must stop at the next ALIGN reloc for an aligment
|
|---|
| 1260 | power larger than the number of bytes we are deleting. */
|
|---|
| 1261 |
|
|---|
| 1262 | irelalign = NULL;
|
|---|
| 1263 | toaddr = sec->_cooked_size;
|
|---|
| 1264 |
|
|---|
| 1265 | irel = elf_section_data (sec)->relocs;
|
|---|
| 1266 | irelend = irel + sec->reloc_count;
|
|---|
| 1267 |
|
|---|
| 1268 | /* Actually delete the bytes. */
|
|---|
| 1269 | memmove (contents + addr, contents + addr + count, toaddr - addr - count);
|
|---|
| 1270 | sec->_cooked_size -= count;
|
|---|
| 1271 |
|
|---|
| 1272 | /* Adjust all the relocs. */
|
|---|
| 1273 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
|
|---|
| 1274 | {
|
|---|
| 1275 | /* Get the new reloc address. */
|
|---|
| 1276 | if ((irel->r_offset > addr
|
|---|
| 1277 | && irel->r_offset < toaddr))
|
|---|
| 1278 | irel->r_offset -= count;
|
|---|
| 1279 | }
|
|---|
| 1280 |
|
|---|
| 1281 | /* Adjust the local symbols defined in this section. */
|
|---|
| 1282 | esym = extsyms;
|
|---|
| 1283 | esymend = esym + symtab_hdr->sh_info;
|
|---|
| 1284 | for (; esym < esymend; esym++)
|
|---|
| 1285 | {
|
|---|
| 1286 | Elf_Internal_Sym isym;
|
|---|
| 1287 |
|
|---|
| 1288 | bfd_elf32_swap_symbol_in (abfd, esym, &isym);
|
|---|
| 1289 |
|
|---|
| 1290 | if (isym.st_shndx == shndx
|
|---|
| 1291 | && isym.st_value > addr
|
|---|
| 1292 | && isym.st_value < toaddr)
|
|---|
| 1293 | {
|
|---|
| 1294 | isym.st_value -= count;
|
|---|
| 1295 | bfd_elf32_swap_symbol_out (abfd, &isym, esym);
|
|---|
| 1296 | }
|
|---|
| 1297 | }
|
|---|
| 1298 |
|
|---|
| 1299 | /* Now adjust the global symbols defined in this section. */
|
|---|
| 1300 | esym = extsyms + symtab_hdr->sh_info;
|
|---|
| 1301 | esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
|
|---|
| 1302 | for (index = 0; esym < esymend; esym++, index++)
|
|---|
| 1303 | {
|
|---|
| 1304 | Elf_Internal_Sym isym;
|
|---|
| 1305 |
|
|---|
| 1306 | bfd_elf32_swap_symbol_in (abfd, esym, &isym);
|
|---|
| 1307 | sym_hash = elf_sym_hashes (abfd)[index];
|
|---|
| 1308 | if (isym.st_shndx == shndx
|
|---|
| 1309 | && ((sym_hash)->root.type == bfd_link_hash_defined
|
|---|
| 1310 | || (sym_hash)->root.type == bfd_link_hash_defweak)
|
|---|
| 1311 | && (sym_hash)->root.u.def.section == sec
|
|---|
| 1312 | && (sym_hash)->root.u.def.value > addr
|
|---|
| 1313 | && (sym_hash)->root.u.def.value < toaddr)
|
|---|
| 1314 | {
|
|---|
| 1315 | (sym_hash)->root.u.def.value -= count;
|
|---|
| 1316 | }
|
|---|
| 1317 | }
|
|---|
| 1318 |
|
|---|
| 1319 | return true;
|
|---|
| 1320 | }
|
|---|
| 1321 |
|
|---|
| 1322 | /* Return true if a symbol exists at the given address, else return
|
|---|
| 1323 | false. */
|
|---|
| 1324 | static boolean
|
|---|
| 1325 | mn10200_elf_symbol_address_p (abfd, sec, extsyms, addr)
|
|---|
| 1326 | bfd *abfd;
|
|---|
| 1327 | asection *sec;
|
|---|
| 1328 | Elf32_External_Sym *extsyms;
|
|---|
| 1329 | bfd_vma addr;
|
|---|
| 1330 | {
|
|---|
| 1331 | Elf_Internal_Shdr *symtab_hdr;
|
|---|
| 1332 | int shndx;
|
|---|
| 1333 | Elf32_External_Sym *esym, *esymend;
|
|---|
| 1334 | struct elf_link_hash_entry **sym_hash, **sym_hash_end;
|
|---|
| 1335 |
|
|---|
| 1336 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
|
|---|
| 1337 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
|
|---|
| 1338 |
|
|---|
| 1339 | /* Examine all the symbols. */
|
|---|
| 1340 | esym = extsyms;
|
|---|
| 1341 | esymend = esym + symtab_hdr->sh_info;
|
|---|
| 1342 | for (; esym < esymend; esym++)
|
|---|
| 1343 | {
|
|---|
| 1344 | Elf_Internal_Sym isym;
|
|---|
| 1345 |
|
|---|
| 1346 | bfd_elf32_swap_symbol_in (abfd, esym, &isym);
|
|---|
| 1347 |
|
|---|
| 1348 | if (isym.st_shndx == shndx
|
|---|
| 1349 | && isym.st_value == addr)
|
|---|
| 1350 | return true;
|
|---|
| 1351 | }
|
|---|
| 1352 |
|
|---|
| 1353 | sym_hash = elf_sym_hashes (abfd);
|
|---|
| 1354 | sym_hash_end = (sym_hash
|
|---|
| 1355 | + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
|
|---|
| 1356 | - symtab_hdr->sh_info));
|
|---|
| 1357 | for (; sym_hash < sym_hash_end; sym_hash++)
|
|---|
| 1358 | {
|
|---|
| 1359 | if (((*sym_hash)->root.type == bfd_link_hash_defined
|
|---|
| 1360 | || (*sym_hash)->root.type == bfd_link_hash_defweak)
|
|---|
| 1361 | && (*sym_hash)->root.u.def.section == sec
|
|---|
| 1362 | && (*sym_hash)->root.u.def.value == addr)
|
|---|
| 1363 | return true;
|
|---|
| 1364 | }
|
|---|
| 1365 | return false;
|
|---|
| 1366 | }
|
|---|
| 1367 |
|
|---|
| 1368 | /* This is a version of bfd_generic_get_relocated_section_contents
|
|---|
| 1369 | which uses mn10200_elf_relocate_section. */
|
|---|
| 1370 |
|
|---|
| 1371 | static bfd_byte *
|
|---|
| 1372 | mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
|
|---|
| 1373 | data, relocateable, symbols)
|
|---|
| 1374 | bfd *output_bfd;
|
|---|
| 1375 | struct bfd_link_info *link_info;
|
|---|
| 1376 | struct bfd_link_order *link_order;
|
|---|
| 1377 | bfd_byte *data;
|
|---|
| 1378 | boolean relocateable;
|
|---|
| 1379 | asymbol **symbols;
|
|---|
| 1380 | {
|
|---|
| 1381 | Elf_Internal_Shdr *symtab_hdr;
|
|---|
| 1382 | asection *input_section = link_order->u.indirect.section;
|
|---|
| 1383 | bfd *input_bfd = input_section->owner;
|
|---|
| 1384 | asection **sections = NULL;
|
|---|
| 1385 | Elf_Internal_Rela *internal_relocs = NULL;
|
|---|
| 1386 | Elf32_External_Sym *external_syms = NULL;
|
|---|
| 1387 | Elf_Internal_Sym *internal_syms = NULL;
|
|---|
| 1388 |
|
|---|
| 1389 | /* We only need to handle the case of relaxing, or of having a
|
|---|
| 1390 | particular set of section contents, specially. */
|
|---|
| 1391 | if (relocateable
|
|---|
| 1392 | || elf_section_data (input_section)->this_hdr.contents == NULL)
|
|---|
| 1393 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
|
|---|
| 1394 | link_order, data,
|
|---|
| 1395 | relocateable,
|
|---|
| 1396 | symbols);
|
|---|
| 1397 |
|
|---|
| 1398 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
|
|---|
| 1399 |
|
|---|
| 1400 | memcpy (data, elf_section_data (input_section)->this_hdr.contents,
|
|---|
| 1401 | input_section->_raw_size);
|
|---|
| 1402 |
|
|---|
| 1403 | if ((input_section->flags & SEC_RELOC) != 0
|
|---|
| 1404 | && input_section->reloc_count > 0)
|
|---|
| 1405 | {
|
|---|
| 1406 | Elf_Internal_Sym *isymp;
|
|---|
| 1407 | asection **secpp;
|
|---|
| 1408 | Elf32_External_Sym *esym, *esymend;
|
|---|
| 1409 |
|
|---|
| 1410 | if (symtab_hdr->contents != NULL)
|
|---|
| 1411 | external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
|
|---|
| 1412 | else
|
|---|
| 1413 | {
|
|---|
| 1414 | external_syms = ((Elf32_External_Sym *)
|
|---|
| 1415 | bfd_malloc (symtab_hdr->sh_info
|
|---|
| 1416 | * sizeof (Elf32_External_Sym)));
|
|---|
| 1417 | if (external_syms == NULL && symtab_hdr->sh_info > 0)
|
|---|
| 1418 | goto error_return;
|
|---|
| 1419 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
|
|---|
| 1420 | || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
|
|---|
| 1421 | symtab_hdr->sh_info, input_bfd)
|
|---|
| 1422 | != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
|
|---|
| 1423 | goto error_return;
|
|---|
| 1424 | }
|
|---|
| 1425 |
|
|---|
| 1426 | internal_relocs = (_bfd_elf32_link_read_relocs
|
|---|
| 1427 | (input_bfd, input_section, (PTR) NULL,
|
|---|
| 1428 | (Elf_Internal_Rela *) NULL, false));
|
|---|
| 1429 | if (internal_relocs == NULL)
|
|---|
| 1430 | goto error_return;
|
|---|
| 1431 |
|
|---|
| 1432 | internal_syms = ((Elf_Internal_Sym *)
|
|---|
| 1433 | bfd_malloc (symtab_hdr->sh_info
|
|---|
| 1434 | * sizeof (Elf_Internal_Sym)));
|
|---|
| 1435 | if (internal_syms == NULL && symtab_hdr->sh_info > 0)
|
|---|
| 1436 | goto error_return;
|
|---|
| 1437 |
|
|---|
| 1438 | sections = (asection **) bfd_malloc (symtab_hdr->sh_info
|
|---|
| 1439 | * sizeof (asection *));
|
|---|
| 1440 | if (sections == NULL && symtab_hdr->sh_info > 0)
|
|---|
| 1441 | goto error_return;
|
|---|
| 1442 |
|
|---|
| 1443 | isymp = internal_syms;
|
|---|
| 1444 | secpp = sections;
|
|---|
| 1445 | esym = external_syms;
|
|---|
| 1446 | esymend = esym + symtab_hdr->sh_info;
|
|---|
| 1447 | for (; esym < esymend; ++esym, ++isymp, ++secpp)
|
|---|
| 1448 | {
|
|---|
| 1449 | asection *isec;
|
|---|
| 1450 |
|
|---|
| 1451 | bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
|
|---|
| 1452 |
|
|---|
| 1453 | if (isymp->st_shndx == SHN_UNDEF)
|
|---|
| 1454 | isec = bfd_und_section_ptr;
|
|---|
| 1455 | else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
|
|---|
| 1456 | isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
|
|---|
| 1457 | else if (isymp->st_shndx == SHN_ABS)
|
|---|
| 1458 | isec = bfd_abs_section_ptr;
|
|---|
| 1459 | else if (isymp->st_shndx == SHN_COMMON)
|
|---|
| 1460 | isec = bfd_com_section_ptr;
|
|---|
| 1461 | else
|
|---|
| 1462 | {
|
|---|
| 1463 | /* Who knows? */
|
|---|
| 1464 | isec = NULL;
|
|---|
| 1465 | }
|
|---|
| 1466 |
|
|---|
| 1467 | *secpp = isec;
|
|---|
| 1468 | }
|
|---|
| 1469 |
|
|---|
| 1470 | if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
|
|---|
| 1471 | input_section, data, internal_relocs,
|
|---|
| 1472 | internal_syms, sections))
|
|---|
| 1473 | goto error_return;
|
|---|
| 1474 |
|
|---|
| 1475 | if (sections != NULL)
|
|---|
| 1476 | free (sections);
|
|---|
| 1477 | sections = NULL;
|
|---|
| 1478 | if (internal_syms != NULL)
|
|---|
| 1479 | free (internal_syms);
|
|---|
| 1480 | internal_syms = NULL;
|
|---|
| 1481 | if (external_syms != NULL && symtab_hdr->contents == NULL)
|
|---|
| 1482 | free (external_syms);
|
|---|
| 1483 | external_syms = NULL;
|
|---|
| 1484 | if (internal_relocs != elf_section_data (input_section)->relocs)
|
|---|
| 1485 | free (internal_relocs);
|
|---|
| 1486 | internal_relocs = NULL;
|
|---|
| 1487 | }
|
|---|
| 1488 |
|
|---|
| 1489 | return data;
|
|---|
| 1490 |
|
|---|
| 1491 | error_return:
|
|---|
| 1492 | if (internal_relocs != NULL
|
|---|
| 1493 | && internal_relocs != elf_section_data (input_section)->relocs)
|
|---|
| 1494 | free (internal_relocs);
|
|---|
| 1495 | if (external_syms != NULL && symtab_hdr->contents == NULL)
|
|---|
| 1496 | free (external_syms);
|
|---|
| 1497 | if (internal_syms != NULL)
|
|---|
| 1498 | free (internal_syms);
|
|---|
| 1499 | if (sections != NULL)
|
|---|
| 1500 | free (sections);
|
|---|
| 1501 | return NULL;
|
|---|
| 1502 | }
|
|---|
| 1503 |
|
|---|
| 1504 | #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
|
|---|
| 1505 | #define TARGET_LITTLE_NAME "elf32-mn10200"
|
|---|
| 1506 | #define ELF_ARCH bfd_arch_mn10200
|
|---|
| 1507 | #define ELF_MACHINE_CODE EM_CYGNUS_MN10200
|
|---|
| 1508 | #define ELF_MAXPAGESIZE 0x1000
|
|---|
| 1509 |
|
|---|
| 1510 | #define elf_info_to_howto mn10200_info_to_howto
|
|---|
| 1511 | #define elf_info_to_howto_rel 0
|
|---|
| 1512 | #define elf_backend_relocate_section mn10200_elf_relocate_section
|
|---|
| 1513 | #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
|
|---|
| 1514 | #define bfd_elf32_bfd_get_relocated_section_contents \
|
|---|
| 1515 | mn10200_elf_get_relocated_section_contents
|
|---|
| 1516 |
|
|---|
| 1517 | #define elf_symbol_leading_char '_'
|
|---|
| 1518 |
|
|---|
| 1519 | #include "elf32-target.h"
|
|---|