00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #define OR32 1
00026
00027 #include "bfd.h"
00028 #include "sysdep.h"
00029 #include "libbfd.h"
00030 #include "coff/or32.h"
00031 #include "coff/internal.h"
00032 #include "libcoff.h"
00033
00034 static long get_symbol_value
00035 PARAMS ((asymbol *));
00036 static bfd_reloc_status_type or32_reloc
00037 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
00038 static bfd_boolean coff_or32_relocate_section
00039 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
00040 struct internal_reloc *, struct internal_syment *, asection **));
00041 static bfd_boolean coff_or32_adjust_symndx
00042 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
00043 struct internal_reloc *, bfd_boolean *));
00044 static void reloc_processing
00045 PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
00046
00047 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
00048
00049 #define INSERT_HWORD(WORD,HWORD) \
00050 (((WORD) & 0xffff0000) | ((HWORD)& 0x0000ffff))
00051 #define EXTRACT_HWORD(WORD) \
00052 ((WORD) & 0x0000ffff)
00053 #define SIGN_EXTEND_HWORD(HWORD) \
00054 ((HWORD) & 0x8000 ? (HWORD)|(~0xffffL) : (HWORD))
00055
00056 #define INSERT_JUMPTARG(WORD,JT) \
00057 (((WORD) & 0xfc000000) | ((JT)& 0x03ffffff))
00058 #define EXTRACT_JUMPTARG(WORD) \
00059 ((WORD) & 0x03ffffff)
00060 #define SIGN_EXTEND_JUMPTARG(JT) \
00061 ((JT) & 0x04000000 ? (JT)|(~0x03ffffffL) : (JT))
00062
00063
00064
00065 static long
00066 get_symbol_value (symbol)
00067 asymbol *symbol;
00068 {
00069 long relocation = 0;
00070
00071 if (bfd_is_com_section (symbol->section))
00072 relocation = 0;
00073 else
00074 relocation = symbol->value +
00075 symbol->section->output_section->vma +
00076 symbol->section->output_offset;
00077
00078 return relocation;
00079 }
00080
00081
00082
00083 static bfd_reloc_status_type
00084 or32_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
00085 error_message)
00086 bfd *abfd;
00087 arelent *reloc_entry;
00088 asymbol *symbol_in;
00089 PTR data;
00090 asection *input_section;
00091 bfd *output_bfd;
00092 char **error_message;
00093 {
00094
00095
00096 static bfd_boolean part1_consth_active = FALSE;
00097 static unsigned long part1_consth_value;
00098
00099 unsigned long insn;
00100 unsigned long sym_value;
00101 unsigned long unsigned_value;
00102 unsigned short r_type;
00103 long signed_value;
00104
00105 unsigned long addr = reloc_entry->address ;
00106 bfd_byte *hit_data =addr + (bfd_byte *)(data);
00107
00108 r_type = reloc_entry->howto->type;
00109
00110 if (output_bfd)
00111 {
00112
00113 reloc_entry->address += input_section->output_offset;
00114 return bfd_reloc_ok;
00115 }
00116
00117 if (symbol_in != NULL
00118 && bfd_is_und_section (symbol_in->section))
00119 {
00120
00121 if (r_type == R_IHIHALF)
00122 {
00123 part1_consth_active = TRUE;
00124 part1_consth_value = 0;
00125 }
00126
00127 return bfd_reloc_undefined;
00128 }
00129
00130 if ((part1_consth_active) && (r_type != R_IHCONST))
00131 {
00132 part1_consth_active = FALSE;
00133 *error_message = (char *) "Missing IHCONST";
00134
00135 return bfd_reloc_dangerous;
00136 }
00137
00138 sym_value = get_symbol_value (symbol_in);
00139
00140 switch (r_type)
00141 {
00142 case R_IREL:
00143 insn = bfd_get_32(abfd, hit_data);
00144
00145
00146 signed_value = EXTRACT_JUMPTARG (insn);
00147 signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
00148 signed_value <<= 2;
00149
00150
00151 if (signed_value == - (long) reloc_entry->address)
00152 signed_value = 0;
00153
00154 signed_value += sym_value + reloc_entry->addend;
00155
00156
00157 signed_value -= (reloc_entry->address
00158 + input_section->output_section->vma
00159 + input_section->output_offset);
00160 if (signed_value > 0x7ffffff || signed_value < -0x8000000)
00161 return bfd_reloc_overflow;
00162
00163 signed_value >>= 2;
00164 insn = INSERT_JUMPTARG (insn, signed_value);
00165 bfd_put_32 (abfd, insn, hit_data);
00166 break;
00167
00168 case R_ILOHALF:
00169 insn = bfd_get_32 (abfd, hit_data);
00170 unsigned_value = EXTRACT_HWORD (insn);
00171 unsigned_value += sym_value + reloc_entry->addend;
00172 insn = INSERT_HWORD (insn, unsigned_value);
00173 bfd_put_32 (abfd, insn, hit_data);
00174 break;
00175
00176 case R_IHIHALF:
00177 insn = bfd_get_32 (abfd, hit_data);
00178
00179
00180
00181 part1_consth_active = TRUE;
00182 part1_consth_value = sym_value + reloc_entry->addend;
00183
00184
00185 break;
00186
00187 case R_IHCONST:
00188 insn = bfd_get_32 (abfd, hit_data);
00189
00190
00191
00192 if (! part1_consth_active)
00193 {
00194 *error_message = (char *) "Missing IHIHALF";
00195 return bfd_reloc_dangerous;
00196 }
00197
00198
00199 unsigned_value = 0;
00200 unsigned_value += reloc_entry->addend;
00201 unsigned_value += part1_consth_value;
00202 unsigned_value = unsigned_value >> 16;
00203 insn = INSERT_HWORD (insn, unsigned_value);
00204 part1_consth_active = FALSE;
00205 bfd_put_32 (abfd, insn, hit_data);
00206 break;
00207
00208 case R_BYTE:
00209 insn = bfd_get_8 (abfd, hit_data);
00210 unsigned_value = insn + sym_value + reloc_entry->addend;
00211 if (unsigned_value & 0xffffff00)
00212 return bfd_reloc_overflow;
00213 bfd_put_8 (abfd, unsigned_value, hit_data);
00214 break;
00215
00216 case R_HWORD:
00217 insn = bfd_get_16 (abfd, hit_data);
00218 unsigned_value = insn + sym_value + reloc_entry->addend;
00219 if (unsigned_value & 0xffff0000)
00220 return bfd_reloc_overflow;
00221 bfd_put_16 (abfd, insn, hit_data);
00222 break;
00223
00224 case R_WORD:
00225 insn = bfd_get_32 (abfd, hit_data);
00226 insn += sym_value + reloc_entry->addend;
00227 bfd_put_32 (abfd, insn, hit_data);
00228 break;
00229
00230 default:
00231 *error_message = _("Unrecognized reloc");
00232 return bfd_reloc_dangerous;
00233 }
00234
00235 return bfd_reloc_ok;
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 static reloc_howto_type howto_table[] =
00253 {
00254 { R_ABS, 0, 3, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "ABS", TRUE, 0xffffffff,0xffffffff, FALSE },
00255 EMPTY_HOWTO (1),
00256 EMPTY_HOWTO (2),
00257 EMPTY_HOWTO (3),
00258 EMPTY_HOWTO (4),
00259 EMPTY_HOWTO (5),
00260 EMPTY_HOWTO (6),
00261 EMPTY_HOWTO (7),
00262 EMPTY_HOWTO (8),
00263 EMPTY_HOWTO (9),
00264 EMPTY_HOWTO (10),
00265 EMPTY_HOWTO (11),
00266 EMPTY_HOWTO (12),
00267 EMPTY_HOWTO (13),
00268 EMPTY_HOWTO (14),
00269 EMPTY_HOWTO (15),
00270 EMPTY_HOWTO (16),
00271 EMPTY_HOWTO (17),
00272 EMPTY_HOWTO (18),
00273 EMPTY_HOWTO (19),
00274 EMPTY_HOWTO (20),
00275 EMPTY_HOWTO (21),
00276 EMPTY_HOWTO (22),
00277 EMPTY_HOWTO (23),
00278 { R_IREL, 0, 3, 32, TRUE, 0, complain_overflow_signed, or32_reloc, "IREL", TRUE, 0xffffffff,0xffffffff, FALSE },
00279 { R_IABS, 0, 3, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "IABS", TRUE, 0xffffffff,0xffffffff, FALSE },
00280 { R_ILOHALF, 0, 3, 16, TRUE, 0, complain_overflow_signed, or32_reloc, "ILOHALF", TRUE, 0x0000ffff,0x0000ffff, FALSE },
00281 { R_IHIHALF, 0, 3, 16, TRUE, 16,complain_overflow_signed, or32_reloc, "IHIHALF", TRUE, 0xffff0000,0xffff0000, FALSE },
00282 { R_IHCONST, 0, 3, 16, TRUE, 0, complain_overflow_signed, or32_reloc, "IHCONST", TRUE, 0xffff0000,0xffff0000, FALSE },
00283 { R_BYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, or32_reloc, "BYTE", TRUE, 0x000000ff,0x000000ff, FALSE },
00284 { R_HWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, or32_reloc, "HWORD", TRUE, 0x0000ffff,0x0000ffff, FALSE },
00285 { R_WORD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "WORD", TRUE, 0xffffffff,0xffffffff, FALSE },
00286 };
00287
00288 #define BADMAG(x) OR32BADMAG (x)
00289
00290 #define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
00291 reloc_processing (relent, reloc, symbols, abfd, section)
00292
00293 static void
00294 reloc_processing (relent,reloc, symbols, abfd, section)
00295 arelent *relent;
00296 struct internal_reloc *reloc;
00297 asymbol **symbols;
00298 bfd *abfd;
00299 asection *section;
00300 {
00301 static bfd_vma ihihalf_vaddr = (bfd_vma) -1;
00302
00303 relent->address = reloc->r_vaddr;
00304 relent->howto = howto_table + reloc->r_type;
00305
00306 if (reloc->r_type == R_IHCONST)
00307 {
00308
00309
00310
00311
00312
00313
00314 if (ihihalf_vaddr == (bfd_vma) -1)
00315 abort ();
00316
00317 relent->address = ihihalf_vaddr;
00318 ihihalf_vaddr = (bfd_vma) -1;
00319 relent->addend = reloc->r_symndx;
00320 relent->sym_ptr_ptr= bfd_abs_section_ptr->symbol_ptr_ptr;
00321 }
00322 else
00323 {
00324 asymbol *ptr;
00325 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
00326
00327 ptr = *(relent->sym_ptr_ptr);
00328
00329 relent->addend = 0;
00330 relent->address-= section->vma;
00331
00332 if (reloc->r_type == R_IHIHALF)
00333 ihihalf_vaddr = relent->address;
00334 else if (ihihalf_vaddr != (bfd_vma) -1)
00335 abort ();
00336 }
00337 }
00338
00339
00340
00341 static bfd_boolean
00342 coff_or32_relocate_section (output_bfd, info, input_bfd, input_section,
00343 contents, relocs, syms, sections)
00344 bfd *output_bfd ATTRIBUTE_UNUSED;
00345 struct bfd_link_info *info;
00346 bfd *input_bfd;
00347 asection *input_section;
00348 bfd_byte *contents;
00349 struct internal_reloc *relocs;
00350 struct internal_syment *syms;
00351 asection **sections;
00352 {
00353 struct internal_reloc *rel;
00354 struct internal_reloc *relend;
00355 bfd_boolean hihalf;
00356 bfd_vma hihalf_val;
00357
00358
00359
00360
00361 if (info->relocatable)
00362 return TRUE;
00363
00364 hihalf = FALSE;
00365 hihalf_val = 0;
00366
00367 rel = relocs;
00368 relend = rel + input_section->reloc_count;
00369
00370 for (; rel < relend; rel++)
00371 {
00372 long symndx;
00373 bfd_byte *loc;
00374 struct coff_link_hash_entry *h;
00375 struct internal_syment *sym;
00376 asection *sec;
00377 bfd_vma val;
00378 bfd_boolean overflow;
00379 unsigned long insn;
00380 long signed_value;
00381 unsigned long unsigned_value;
00382 bfd_reloc_status_type rstat;
00383
00384 symndx = rel->r_symndx;
00385 loc = contents + rel->r_vaddr - input_section->vma;
00386
00387 if (symndx == -1 || rel->r_type == R_IHCONST)
00388 h = NULL;
00389 else
00390 h = obj_coff_sym_hashes (input_bfd)[symndx];
00391
00392 sym = NULL;
00393 sec = NULL;
00394 val = 0;
00395
00396
00397
00398
00399 if (rel->r_type != R_IHCONST)
00400 {
00401 if (h == NULL)
00402 {
00403 if (symndx == -1)
00404 sec = bfd_abs_section_ptr;
00405 else
00406 {
00407 sym = syms + symndx;
00408 sec = sections[symndx];
00409 val = (sec->output_section->vma
00410 + sec->output_offset
00411 + sym->n_value
00412 - sec->vma);
00413 }
00414 }
00415 else
00416 {
00417 if (h->root.type == bfd_link_hash_defined
00418 || h->root.type == bfd_link_hash_defweak)
00419 {
00420 sec = h->root.u.def.section;
00421 val = (h->root.u.def.value
00422 + sec->output_section->vma
00423 + sec->output_offset);
00424 }
00425 else
00426 {
00427 if (! ((*info->callbacks->undefined_symbol)
00428 (info, h->root.root.string, input_bfd, input_section,
00429 rel->r_vaddr - input_section->vma, TRUE)))
00430 return FALSE;
00431 }
00432 }
00433
00434 if (hihalf)
00435 {
00436 if (! ((*info->callbacks->reloc_dangerous)
00437 (info, "missing IHCONST reloc", input_bfd,
00438 input_section, rel->r_vaddr - input_section->vma)))
00439 return FALSE;
00440 hihalf = FALSE;
00441 }
00442 }
00443
00444 overflow = FALSE;
00445
00446 switch (rel->r_type)
00447 {
00448 default:
00449 bfd_set_error (bfd_error_bad_value);
00450 return FALSE;
00451
00452 case R_IREL:
00453 insn = bfd_get_32 (input_bfd, loc);
00454
00455
00456 signed_value = EXTRACT_JUMPTARG (insn);
00457 signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
00458 signed_value <<= 2;
00459
00460
00461 signed_value += val;
00462
00463
00464 signed_value -= (input_section->output_section->vma
00465 + input_section->output_offset
00466 + (rel->r_vaddr - input_section->vma));
00467 if (signed_value > 0x7ffffff || signed_value < - 0x8000000)
00468 {
00469 overflow = TRUE;
00470 signed_value = 0;
00471 }
00472
00473
00474 signed_value >>= 2;
00475 insn = INSERT_JUMPTARG(insn, signed_value);
00476
00477 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
00478 break;
00479
00480 case R_ILOHALF:
00481 insn = bfd_get_32 (input_bfd, loc);
00482 unsigned_value = EXTRACT_HWORD (insn);
00483 unsigned_value += val;
00484 insn = INSERT_HWORD (insn, unsigned_value);
00485 bfd_put_32 (input_bfd, insn, loc);
00486 break;
00487
00488 case R_IHIHALF:
00489
00490 hihalf = TRUE;
00491 hihalf_val = val;
00492 break;
00493
00494 case R_IHCONST:
00495 if (! hihalf)
00496 {
00497 if (! ((*info->callbacks->reloc_dangerous)
00498 (info, "missing IHIHALF reloc", input_bfd,
00499 input_section, rel->r_vaddr - input_section->vma)))
00500 return FALSE;
00501 hihalf_val = 0;
00502 }
00503
00504 insn = bfd_get_32 (input_bfd, loc);
00505 unsigned_value = rel->r_symndx + hihalf_val;
00506 unsigned_value >>= 16;
00507 insn = INSERT_HWORD (insn, unsigned_value);
00508 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
00509
00510 hihalf = FALSE;
00511 break;
00512
00513 case R_BYTE:
00514 case R_HWORD:
00515 case R_WORD:
00516 rstat = _bfd_relocate_contents (howto_table + rel->r_type,
00517 input_bfd, val, loc);
00518 if (rstat == bfd_reloc_overflow)
00519 overflow = TRUE;
00520 else if (rstat != bfd_reloc_ok)
00521 abort ();
00522 break;
00523 }
00524
00525 if (overflow)
00526 {
00527 const char *name;
00528 char buf[SYMNMLEN + 1];
00529
00530 if (symndx == -1)
00531 name = "*ABS*";
00532 else if (h != NULL)
00533 name = NULL;
00534 else if (sym == NULL)
00535 name = "*unknown*";
00536 else if (sym->_n._n_n._n_zeroes == 0
00537 && sym->_n._n_n._n_offset != 0)
00538 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
00539 else
00540 {
00541 strncpy (buf, sym->_n._n_name, SYMNMLEN);
00542 buf[SYMNMLEN] = '\0';
00543 name = buf;
00544 }
00545
00546 if (! ((*info->callbacks->reloc_overflow)
00547 (info, (h ? &h->root : NULL), name,
00548 howto_table[rel->r_type].name, (bfd_vma) 0, input_bfd,
00549 input_section, rel->r_vaddr - input_section->vma)))
00550 return FALSE;
00551 }
00552 }
00553
00554 return TRUE;
00555 }
00556
00557 #define coff_relocate_section coff_or32_relocate_section
00558
00559
00560
00561
00562 static bfd_boolean
00563 coff_or32_adjust_symndx (obfd, info, ibfd, sec, irel, adjustedp)
00564 bfd *obfd ATTRIBUTE_UNUSED;
00565 struct bfd_link_info *info ATTRIBUTE_UNUSED;
00566 bfd *ibfd ATTRIBUTE_UNUSED;
00567 asection *sec ATTRIBUTE_UNUSED;
00568 struct internal_reloc *irel;
00569 bfd_boolean *adjustedp;
00570 {
00571 if (irel->r_type == R_IHCONST)
00572 *adjustedp = TRUE;
00573 else
00574 *adjustedp = FALSE;
00575 return TRUE;
00576 }
00577
00578 #define coff_adjust_symndx coff_or32_adjust_symndx
00579
00580 #include "coffcode.h"
00581
00582 const bfd_target or32coff_big_vec =
00583 {
00584 "coff-or32-big",
00585 bfd_target_coff_flavour,
00586 BFD_ENDIAN_BIG,
00587 BFD_ENDIAN_BIG,
00588
00589 (HAS_RELOC | EXEC_P |
00590 HAS_LINENO | HAS_DEBUG |
00591 HAS_SYMS | HAS_LOCALS | WP_TEXT),
00592
00593 (SEC_HAS_CONTENTS | SEC_ALLOC |
00594 SEC_LOAD | SEC_RELOC |
00595 SEC_READONLY ),
00596 '_',
00597 '/',
00598 15,
00599
00600
00601 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
00602 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
00603 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
00604
00605
00606 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
00607 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
00608 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
00609
00610 {
00611 _bfd_dummy_target,
00612 coff_object_p,
00613 bfd_generic_archive_p,
00614 _bfd_dummy_target
00615 },
00616 {
00617 bfd_false,
00618 coff_mkobject,
00619 _bfd_generic_mkarchive,
00620 bfd_false
00621 },
00622 {
00623 bfd_false,
00624 coff_write_object_contents,
00625 _bfd_write_archive_contents,
00626 bfd_false
00627 },
00628
00629 BFD_JUMP_TABLE_GENERIC (coff),
00630 BFD_JUMP_TABLE_COPY (coff),
00631 BFD_JUMP_TABLE_CORE (_bfd_nocore),
00632 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
00633 BFD_JUMP_TABLE_SYMBOLS (coff),
00634 BFD_JUMP_TABLE_RELOCS (coff),
00635 BFD_JUMP_TABLE_WRITE (coff),
00636 BFD_JUMP_TABLE_LINK (coff),
00637 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
00638
00639
00640 #ifdef TARGET_LITTLE_SYM
00641 & TARGET_LITTLE_SYM,
00642 #else
00643 NULL,
00644 #endif
00645
00646 COFF_SWAP_TABLE
00647 };