1 /* Scenix IP2xxx specific support for 32-bit ELF
2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
26 /* Struct used to pass miscellaneous paramaters which
27 helps to avoid overly long parameter lists. */
30 Elf_Internal_Shdr * symtab_hdr;
31 Elf_Internal_Rela * irelbase;
33 bfd_byte * free_contents;
34 Elf32_External_Sym * extsyms;
35 Elf32_External_Sym * free_extsyms;
36 Elf_Internal_Rela * free_relocs;
40 static reloc_howto_type * ip2k_reloc_type_lookup PARAMS ((bfd *, bfd_reloc_code_real_type));
41 static void ip2k_info_to_howto_rela PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
42 static asection * ip2k_elf_gc_mark_hook PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *, struct elf_link_hash_entry *, Elf_Internal_Sym *));
43 static boolean ip2k_elf_gc_sweep_hook PARAMS ((bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *));
44 static bfd_vma symbol_value PARAMS ((bfd *, Elf_Internal_Shdr *, Elf32_External_Sym *, Elf_Internal_Rela *));
45 static void adjust_all_relocations PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, int, int));
46 static boolean ip2k_elf_relax_delete_bytes PARAMS ((bfd *, asection *, bfd_vma, int));
47 static boolean ip2k_elf_relax_add_bytes PARAMS ((bfd *, asection *, bfd_vma, const bfd_byte *, int, int));
48 static boolean add_page_insn PARAMS ((bfd *, asection *, Elf_Internal_Rela *, struct misc *));
49 static boolean ip2k_elf_relax_section PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
50 static boolean relax_switch_dispatch_tables_pass1 PARAMS ((bfd *, asection *, bfd_vma, struct misc *));
51 static boolean unrelax_dispatch_table_entries PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, boolean *, struct misc *));
52 static boolean unrelax_switch_dispatch_tables_passN PARAMS ((bfd *, asection *, bfd_vma, boolean *, struct misc *));
53 static boolean is_switch_128_dispatch_table_p PARAMS ((bfd *, bfd_vma, boolean, struct misc *));
54 static boolean is_switch_256_dispatch_table_p PARAMS ((bfd *, bfd_vma, boolean, struct misc *));
55 static void tidyup_after_error PARAMS ((struct misc *));
56 static boolean ip2k_elf_relax_section_pass1 PARAMS ((bfd *, asection *, boolean *, struct misc *));
57 static boolean ip2k_elf_relax_section_passN PARAMS ((bfd *, asection *, boolean *, boolean *, struct misc *));
58 static bfd_reloc_status_type ip2k_final_link_relocate PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, bfd_vma));
59 static boolean ip2k_elf_relocate_section PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
61 #define IS_OPCODE(CODE0,CODE1,OPCODE) \
62 ((CODE0) == (OPCODE)[0] && (CODE1) == (OPCODE)[1])
64 #define PAGE_INSN_0 0x00
65 #define PAGE_INSN_1 0x10
67 static const bfd_byte page_opcode[] =
69 PAGE_INSN_0, PAGE_INSN_1
72 #define IS_PAGE_OPCODE(CODE0,CODE1) \
73 IS_OPCODE (CODE0, CODE1, page_opcode)
75 #define JMP_INSN_0 0xE0
76 #define JMP_INSN_1 0x00
78 static const bfd_byte jmp_opcode[] =
80 JMP_INSN_0, JMP_INSN_1
83 #define IS_JMP_OPCODE(CODE0,CODE1) \
84 IS_OPCODE (CODE0, CODE1, jmp_opcode)
86 #define CALL_INSN_0 0xC0
87 #define CALL_INSN_1 0x00
89 static const bfd_byte call_opcode[] =
91 CALL_INSN_0, CALL_INSN_1
94 #define IS_CALL_OPCODE(CODE0,CODE1) \
95 IS_OPCODE (CODE0, CODE1, call_opcode)
97 #define ADD_PCL_W_INSN_0 0x1E
98 #define ADD_PCL_W_INSN_1 0x09
100 static const bfd_byte add_pcl_w_opcode[] =
102 ADD_PCL_W_INSN_0, ADD_PCL_W_INSN_1
105 #define IS_ADD_PCL_W_OPCODE(CODE0,CODE1) \
106 IS_OPCODE (CODE0, CODE1, add_pcl_w_opcode)
108 #define ADD_W_WREG_INSN_0 0x1C
109 #define ADD_W_WREG_INSN_1 0x0A
111 static const bfd_byte add_w_wreg_opcode[] =
113 ADD_W_WREG_INSN_0, ADD_W_WREG_INSN_1
116 #define IS_ADD_W_WREG_OPCODE(CODE0,CODE1) \
117 IS_OPCODE (CODE0, CODE1, add_w_wreg_opcode)
119 #define SNC_INSN_0 0xA0
120 #define SNC_INSN_1 0x0B
122 static const bfd_byte snc_opcode[] =
124 SNC_INSN_0, SNC_INSN_1
127 #define IS_SNC_OPCODE(CODE0,CODE1) \
128 IS_OPCODE (CODE0, CODE1, snc_opcode)
130 #define INC_1_SP_INSN_0 0x2B
131 #define INC_1_SP_INSN_1 0x81
133 static const bfd_byte inc_1_sp_opcode[] =
135 INC_1_SP_INSN_0, INC_1_SP_INSN_1
138 #define IS_INC_1_SP_OPCODE(CODE0,CODE1) \
139 IS_OPCODE (CODE0, CODE1, inc_1_sp_opcode)
141 #define ADD_2_SP_W_INSN_0 0x1F
142 #define ADD_2_SP_W_INSN_1 0x82
144 static const bfd_byte add_2_sp_w_opcode[] =
146 ADD_2_SP_W_INSN_0, ADD_2_SP_W_INSN_1
149 #define IS_ADD_2_SP_W_OPCODE(CODE0,CODE1) \
150 IS_OPCODE (CODE0, CODE1, add_2_sp_w_opcode)
152 /* Relocation tables. */
153 static reloc_howto_type ip2k_elf_howto_table [] =
155 #define IP2K_HOWTO(t,rs,s,bs,pr,bp,name,sm,dm) \
156 HOWTO(t, /* type */ \
157 rs, /* rightshift */ \
158 s, /* size (0 = byte, 1 = short, 2 = long) */ \
160 pr, /* pc_relative */ \
162 complain_overflow_dont,/* complain_on_overflow */ \
163 bfd_elf_generic_reloc,/* special_function */ \
165 false, /* partial_inplace */ \
168 pr) /* pcrel_offset */
170 /* This reloc does nothing. */
171 IP2K_HOWTO (R_IP2K_NONE, 0,2,32, false, 0, "R_IP2K_NONE", 0, 0),
172 /* A 16 bit absolute relocation. */
173 IP2K_HOWTO (R_IP2K_16, 0,1,16, false, 0, "R_IP2K_16", 0, 0xffff),
174 /* A 32 bit absolute relocation. */
175 IP2K_HOWTO (R_IP2K_32, 0,2,32, false, 0, "R_IP2K_32", 0, 0xffffffff),
176 /* A 8-bit data relocation for the FR9 field. Ninth bit is computed specially. */
177 IP2K_HOWTO (R_IP2K_FR9, 0,1,9, false, 0, "R_IP2K_FR9", 0, 0x00ff),
178 /* A 4-bit data relocation. */
179 IP2K_HOWTO (R_IP2K_BANK, 8,1,4, false, 0, "R_IP2K_BANK", 0, 0x000f),
180 /* A 13-bit insn relocation - word address => right-shift 1 bit extra. */
181 IP2K_HOWTO (R_IP2K_ADDR16CJP, 1,1,13, false, 0, "R_IP2K_ADDR16CJP", 0, 0x1fff),
182 /* A 3-bit insn relocation - word address => right-shift 1 bit extra. */
183 IP2K_HOWTO (R_IP2K_PAGE3, 14,1,3, false, 0, "R_IP2K_PAGE3", 0, 0x0007),
184 /* Two 8-bit data relocations. */
185 IP2K_HOWTO (R_IP2K_LO8DATA, 0,1,8, false, 0, "R_IP2K_LO8DATA", 0, 0x00ff),
186 IP2K_HOWTO (R_IP2K_HI8DATA, 8,1,8, false, 0, "R_IP2K_HI8DATA", 0, 0x00ff),
187 /* Two 8-bit insn relocations. word address => right-shift 1 bit extra. */
188 IP2K_HOWTO (R_IP2K_LO8INSN, 1,1,8, false, 0, "R_IP2K_LO8INSN", 0, 0x00ff),
189 IP2K_HOWTO (R_IP2K_HI8INSN, 9,1,8, false, 0, "R_IP2K_HI8INSN", 0, 0x00ff),
191 /* Special 1 bit relocation for SKIP instructions. */
192 IP2K_HOWTO (R_IP2K_PC_SKIP, 1,1,1, false, 12, "R_IP2K_PC_SKIP", 0xfffe, 0x1000),
193 /* 16 bit word address. */
194 IP2K_HOWTO (R_IP2K_TEXT, 1,1,16, false, 0, "R_IP2K_TEXT", 0, 0xffff),
195 /* A 7-bit offset relocation for the FR9 field. Eigth and ninth bit comes from insn. */
196 IP2K_HOWTO (R_IP2K_FR_OFFSET, 0,1,9, false, 0, "R_IP2K_FR_OFFSET", 0x180, 0x007f),
197 /* Bits 23:16 of an address. */
198 IP2K_HOWTO (R_IP2K_EX8DATA, 16,1,8, false, 0, "R_IP2K_EX8DATA", 0, 0x00ff),
202 /* Map BFD reloc types to IP2K ELF reloc types. */
203 static reloc_howto_type *
204 ip2k_reloc_type_lookup (abfd, code)
205 bfd * abfd ATTRIBUTE_UNUSED;
206 bfd_reloc_code_real_type code;
208 /* Note that the ip2k_elf_howto_table is indxed by the R_
209 constants. Thus, the order that the howto records appear in the
210 table *must* match the order of the relocation types defined in
211 include/elf/ip2k.h. */
216 return &ip2k_elf_howto_table[ (int) R_IP2K_NONE];
218 return &ip2k_elf_howto_table[ (int) R_IP2K_16];
220 return &ip2k_elf_howto_table[ (int) R_IP2K_32];
221 case BFD_RELOC_IP2K_FR9:
222 return &ip2k_elf_howto_table[ (int) R_IP2K_FR9];
223 case BFD_RELOC_IP2K_BANK:
224 return &ip2k_elf_howto_table[ (int) R_IP2K_BANK];
225 case BFD_RELOC_IP2K_ADDR16CJP:
226 return &ip2k_elf_howto_table[ (int) R_IP2K_ADDR16CJP];
227 case BFD_RELOC_IP2K_PAGE3:
228 return &ip2k_elf_howto_table[ (int) R_IP2K_PAGE3];
229 case BFD_RELOC_IP2K_LO8DATA:
230 return &ip2k_elf_howto_table[ (int) R_IP2K_LO8DATA];
231 case BFD_RELOC_IP2K_HI8DATA:
232 return &ip2k_elf_howto_table[ (int) R_IP2K_HI8DATA];
233 case BFD_RELOC_IP2K_LO8INSN:
234 return &ip2k_elf_howto_table[ (int) R_IP2K_LO8INSN];
235 case BFD_RELOC_IP2K_HI8INSN:
236 return &ip2k_elf_howto_table[ (int) R_IP2K_HI8INSN];
237 case BFD_RELOC_IP2K_PC_SKIP:
238 return &ip2k_elf_howto_table[ (int) R_IP2K_PC_SKIP];
239 case BFD_RELOC_IP2K_TEXT:
240 return &ip2k_elf_howto_table[ (int) R_IP2K_TEXT];
241 case BFD_RELOC_IP2K_FR_OFFSET:
242 return &ip2k_elf_howto_table[ (int) R_IP2K_FR_OFFSET];
243 case BFD_RELOC_IP2K_EX8DATA:
244 return &ip2k_elf_howto_table[ (int) R_IP2K_EX8DATA];
246 /* Pacify gcc -Wall. */
252 #define PAGENO(ABSADDR) ((ABSADDR) & 0x1C000)
253 #define BASEADDR(SEC) ((SEC)->output_section->vma + (SEC)->output_offset)
255 #define UNDEFINED_SYMBOL (~(bfd_vma)0)
257 /* Return the value of the symbol associated with the relocation IREL. */
260 symbol_value (abfd, symtab_hdr, extsyms, irel)
262 Elf_Internal_Shdr *symtab_hdr;
263 Elf32_External_Sym *extsyms;
264 Elf_Internal_Rela *irel;
266 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
268 Elf_External_Sym_Shndx *sym_shndx;
269 Elf_Internal_Shdr *shndx_hdr;
270 Elf_Internal_Sym isym;
273 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
274 sym_shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
275 sym_shndx = sym_shndx ? sym_shndx + ELF32_R_SYM (irel->r_info) : NULL;
276 bfd_elf32_swap_symbol_in (abfd, extsyms + ELF32_R_SYM (irel->r_info),
278 if (isym.st_shndx == SHN_UNDEF)
279 sym_sec = bfd_und_section_ptr;
280 else if (isym.st_shndx == SHN_ABS)
281 sym_sec = bfd_abs_section_ptr;
282 else if (isym.st_shndx == SHN_COMMON)
283 sym_sec = bfd_com_section_ptr;
285 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
287 return isym.st_value + BASEADDR (sym_sec);
292 struct elf_link_hash_entry *h;
294 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
295 h = elf_sym_hashes (abfd)[indx];
296 BFD_ASSERT (h != NULL);
298 if (h->root.type != bfd_link_hash_defined
299 && h->root.type != bfd_link_hash_defweak)
300 return UNDEFINED_SYMBOL;
302 return (h->root.u.def.value + BASEADDR (h->root.u.def.section));
306 /* Determine if the instruction sequence matches that for
307 the prologue of a switch dispatch table with fewer than
336 is_switch_128_dispatch_table_p (abfd, addr, relaxed, misc)
337 bfd *abfd ATTRIBUTE_UNUSED;
342 bfd_byte code0, code1;
347 code0 = bfd_get_8 (abfd, misc->contents + addr - 2);
348 code1 = bfd_get_8 (abfd, misc->contents + addr - 1);
350 /* Is it ADD PCL,W */
351 if (! IS_ADD_PCL_W_OPCODE (code0, code1))
354 code0 = bfd_get_8 (abfd, misc->contents + addr - 4);
355 code1 = bfd_get_8 (abfd, misc->contents + addr - 3);
358 /* Is it ADD W,WREG */
359 return ! IS_ADD_W_WREG_OPCODE (code0, code1);
363 /* Is it ADD W,WREG */
364 if (! IS_ADD_W_WREG_OPCODE (code0, code1))
367 code0 = bfd_get_8 (abfd, misc->contents + addr - 6);
368 code1 = bfd_get_8 (abfd, misc->contents + addr - 5);
370 /* Is it JMP $nnnn */
371 if (! IS_JMP_OPCODE (code0, code1))
375 /* It looks like we've found the prologue for
376 a 1-127 entry switch dispatch table. */
380 /* Determine if the instruction sequence matches that for
381 the prologue switch dispatch table with fewer than
382 256 entries but more than 127.
385 push %lo8insn(label) ; Push address of table
387 add w,wreg ; index*2 => offset
389 inc 1(sp) ; Propagate MSB into table address
390 add 2(sp),w ; Add low bits of offset to table address
391 snc ; and handle any carry-out
394 page __indjmp ; Do an indirect jump to that location
396 label: ; case dispatch table starts here
406 push %lo8insn(label) ; Push address of table
408 add 2(sp),w ; Add low bits of offset to table address
409 snc ; and handle any carry-out
412 page __indjmp ; Do an indirect jump to that location
414 label: ; case dispatch table starts here
421 is_switch_256_dispatch_table_p (abfd, addr, relaxed, misc)
422 bfd *abfd ATTRIBUTE_UNUSED;
427 bfd_byte code0, code1;
432 code0 = bfd_get_8 (abfd, misc->contents + addr - 2);
433 code1 = bfd_get_8 (abfd, misc->contents + addr - 1);
435 /* Is it INC 1(SP). */
436 if (! IS_INC_1_SP_OPCODE (code0, code1))
439 code0 = bfd_get_8 (abfd, misc->contents + addr - 4);
440 code1 = bfd_get_8 (abfd, misc->contents + addr - 3);
443 if (! IS_SNC_OPCODE (code0, code1))
446 code0 = bfd_get_8 (abfd, misc->contents + addr - 6);
447 code1 = bfd_get_8 (abfd, misc->contents + addr - 5);
449 /* Is it ADD 2(SP),W. */
450 if (! IS_ADD_2_SP_W_OPCODE (code0, code1))
453 code0 = bfd_get_8 (abfd, misc->contents + addr - 8);
454 code1 = bfd_get_8 (abfd, misc->contents + addr - 7);
457 /* Is it INC 1(SP). */
458 return ! IS_INC_1_SP_OPCODE (code0, code1);
462 /* Is it INC 1(SP). */
463 if (! IS_INC_1_SP_OPCODE (code0, code1))
466 code0 = bfd_get_8 (abfd, misc->contents + addr - 10);
467 code1 = bfd_get_8 (abfd, misc->contents + addr - 9);
470 if (! IS_SNC_OPCODE (code0, code1))
473 code0 = bfd_get_8 (abfd, misc->contents + addr - 12);
474 code1 = bfd_get_8 (abfd, misc->contents + addr - 11);
476 /* Is it ADD W,WREG. */
477 if (! IS_ADD_W_WREG_OPCODE (code0, code1))
481 /* It looks like we've found the prologue for
482 a 128-255 entry switch dispatch table. */
487 relax_switch_dispatch_tables_pass1 (abfd, sec, addr, misc)
493 if (addr + 3 < sec->_cooked_size)
495 bfd_byte code0 = bfd_get_8 (abfd, misc->contents + addr + 2);
496 bfd_byte code1 = bfd_get_8 (abfd, misc->contents + addr + 3);
498 if (IS_JMP_OPCODE (code0, code1)
499 && is_switch_128_dispatch_table_p (abfd, addr, false, misc))
501 /* Delete ADD W,WREG from prologue. */
502 ip2k_elf_relax_delete_bytes (abfd, sec, addr - (2 * 2), (1 * 2));
506 if (IS_JMP_OPCODE (code0, code1)
507 && is_switch_256_dispatch_table_p (abfd, addr, false, misc))
509 /* Delete ADD W,WREG; SNC ; INC 1(SP) from prologue. */
510 ip2k_elf_relax_delete_bytes (abfd, sec, addr - 6 * 2, 3 * 2);
519 unrelax_dispatch_table_entries (abfd, sec, first, last, changed, misc)
527 bfd_vma addr = first;
531 bfd_byte code0 = bfd_get_8 (abfd, misc->contents + addr);
532 bfd_byte code1 = bfd_get_8 (abfd, misc->contents + addr + 1);
534 /* We are only expecting to find PAGE or JMP insns
535 in the dispatch table. If we find anything else
536 something has gone wrong failed the relaxation
537 which will cause the link to be aborted. */
539 if (IS_PAGE_OPCODE (code0, code1))
540 /* Skip the PAGE and JMP insns. */
542 else if (IS_JMP_OPCODE (code0, code1))
544 Elf_Internal_Rela * irelend = misc->irelbase
546 Elf_Internal_Rela * irel;
548 /* Find the relocation entry. */
549 for (irel = misc->irelbase; irel < irelend; irel++)
551 if (irel->r_offset == addr
552 && ELF32_R_TYPE (irel->r_info) == R_IP2K_ADDR16CJP)
554 if (! add_page_insn (abfd, sec, irel, misc))
555 /* Something has gone wrong. */
563 /* If we fell off the end something has gone wrong. */
565 /* Something has gone wrong. */
568 /* Skip the PAGE and JMP isns. */
570 /* Acount for the new PAGE insn. */
574 /* Something has gone wrong. */
582 unrelax_switch_dispatch_tables_passN (abfd, sec, addr, changed, misc)
589 if (2 <= addr && (addr + 3) < sec->_cooked_size)
591 bfd_byte code0 = bfd_get_8 (abfd, misc->contents + addr - 2);
592 bfd_byte code1 = bfd_get_8 (abfd, misc->contents + addr - 1);
594 if (IS_PAGE_OPCODE (code0, code1))
597 code0 = bfd_get_8 (abfd, misc->contents + addr + 2);
598 code1 = bfd_get_8 (abfd, misc->contents + addr + 3);
602 code0 = bfd_get_8 (abfd, misc->contents + addr);
603 code1 = bfd_get_8 (abfd, misc->contents + addr + 1);
606 if (IS_JMP_OPCODE (code0, code1)
607 && is_switch_128_dispatch_table_p (abfd, addr, true, misc))
609 bfd_vma first = addr;
610 bfd_vma last = first;
611 boolean relaxed = true;
613 /* On the final pass we must check if *all* entries in the
614 dispatch table are relaxed. If *any* are not relaxed
615 then we must unrelax *all* the entries in the dispach
616 table and also unrelax the dispatch table prologue. */
618 /* Find the last entry in the dispach table. */
619 while (last < sec->_cooked_size)
621 code0 = bfd_get_8 (abfd, misc->contents + last);
622 code1 = bfd_get_8 (abfd, misc->contents + last + 1);
624 if (IS_PAGE_OPCODE (code0, code1))
626 else if (! IS_JMP_OPCODE (code0, code1))
632 /* We should have found the end of the dispatch table
633 before reaching the end of the section. If we've have
634 reached the end then fail the relaxation which will
635 cause the link to be aborted. */
636 if (last >= sec->_cooked_size)
637 /* Something has gone wrong. */
640 /* If we found an unrelaxed entry then
641 unlrelax all the switch table entries. */
644 if (! unrelax_dispatch_table_entries (abfd, sec, first,
645 last, changed, misc))
646 /* Something has gone wrong. */
649 if (! is_switch_128_dispatch_table_p (abfd, addr, true, misc))
650 /* Something has gone wrong. */
653 /* Unrelax the prologue. */
655 /* Insert an ADD W,WREG insnstruction. */
656 if (! ip2k_elf_relax_add_bytes (abfd, sec,
659 sizeof (add_w_wreg_opcode),
661 /* Something has gone wrong. */
668 if (IS_JMP_OPCODE (code0, code1)
669 && is_switch_256_dispatch_table_p (abfd, addr, true, misc))
671 bfd_vma first = addr;
673 boolean relaxed = true;
675 /* On the final pass we must check if *all* entries in the
676 dispatch table are relaxed. If *any* are not relaxed
677 then we must unrelax *all* the entries in the dispach
678 table and also unrelax the dispatch table prologue. */
680 /* Note the 1st PAGE/JMP instructions are part of the
681 prologue and can safely be relaxed. */
683 code0 = bfd_get_8 (abfd, misc->contents + first);
684 code1 = bfd_get_8 (abfd, misc->contents + first + 1);
686 if (IS_PAGE_OPCODE (code0, code1))
689 code0 = bfd_get_8 (abfd, misc->contents + first);
690 code1 = bfd_get_8 (abfd, misc->contents + first + 1);
693 if (! IS_JMP_OPCODE (code0, code1))
694 /* Something has gone wrong. */
700 /* Find the last entry in the dispach table. */
701 while (last < sec->_cooked_size)
703 code0 = bfd_get_8 (abfd, misc->contents + last);
704 code1 = bfd_get_8 (abfd, misc->contents + last + 1);
706 if (IS_PAGE_OPCODE (code0, code1))
708 else if (! IS_JMP_OPCODE (code0, code1))
714 /* We should have found the end of the dispatch table
715 before reaching the end of the section. If we have
716 reached the end of the section then fail the
718 if (last >= sec->_cooked_size)
721 /* If we found an unrelaxed entry then
722 unrelax all the switch table entries. */
725 if (! unrelax_dispatch_table_entries (abfd, sec, first,
726 last, changed, misc))
729 if (! is_switch_256_dispatch_table_p (abfd, addr, true, misc))
732 /* Unrelax the prologue. */
734 /* Insert an INC 1(SP) insnstruction. */
735 if (! ip2k_elf_relax_add_bytes (abfd, sec,
738 sizeof (inc_1_sp_opcode),
742 /* Insert an SNC insnstruction. */
743 if (! ip2k_elf_relax_add_bytes (abfd, sec,
750 /* Insert an ADD W,WREG insnstruction. */
751 if (! ip2k_elf_relax_add_bytes (abfd, sec,
754 sizeof (add_w_wreg_opcode),
766 /* This function handles relaxing for the ip2k. */
769 ip2k_elf_relax_section (abfd, sec, link_info, again)
772 struct bfd_link_info *link_info;
775 Elf_External_Sym_Shndx *shndx_buf;
776 Elf_Internal_Shdr *shndx_hdr;
777 static asection * first_section = NULL;
778 static asection * last_section = NULL;
779 static boolean changed = false;
780 static boolean final_pass = false;
781 static unsigned int pass = 0;
785 /* Assume nothing changes. */
788 if (first_section == NULL)
791 if (first_section == sec)
797 /* If we make too many passes then it's a sign that
798 something is wrong and we fail the relaxation.
799 Note if everything is working correctly then the
800 relaxation should converge reasonably quickly. */
804 /* We don't have to do anything for a relocatable link,
805 if this section does not have relocs, or if this is
806 not a code section. */
807 if (link_info->relocateable
808 || (sec->flags & SEC_RELOC) == 0
809 || sec->reloc_count == 0
810 || (sec->flags & SEC_CODE) == 0)
816 misc.symtab_hdr = NULL;
817 misc.irelbase = NULL;
818 misc.contents = NULL;
819 misc.free_contents = NULL;
821 misc.free_extsyms = NULL;
822 misc.free_relocs = NULL;
824 /* If this is the first time we have been called
825 for this section, initialise the cooked size. */
826 if (sec->_cooked_size == 0)
827 sec->_cooked_size = sec->_raw_size;
829 misc.symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
830 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
832 misc.irelbase = _bfd_elf32_link_read_relocs (abfd, sec, NULL,
833 (Elf_Internal_Rela *)NULL,
834 link_info->keep_memory);
835 if (misc.irelbase == NULL)
837 tidyup_after_error (&misc);
841 if (! link_info->keep_memory)
842 misc.free_relocs = misc.irelbase;
844 /* Make sure the stac.rela stuff gets read in. */
845 stab = bfd_get_section_by_name (abfd, ".stab");
849 /* So stab does exits. */
850 Elf_Internal_Rela * irelbase;
852 irelbase = _bfd_elf32_link_read_relocs (abfd, stab, NULL,
853 (Elf_Internal_Rela *)NULL,
854 link_info->keep_memory);
857 /* Get section contents cached copy if it exists. */
858 if (elf_section_data (sec)->this_hdr.contents != NULL)
859 misc.contents = elf_section_data (sec)->this_hdr.contents;
862 /* Go get them of disk. */
863 misc.contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
864 if (misc.contents == NULL)
866 tidyup_after_error (&misc);
870 misc.free_contents = misc.contents;
871 if (! bfd_get_section_contents (abfd, sec, misc.contents,
875 tidyup_after_error (&misc);
880 /* Read this BFD's symbols cached copy if it exists. */
881 if (misc.symtab_hdr->contents != NULL)
882 misc.extsyms = (Elf32_External_Sym *) misc.symtab_hdr->contents;
885 /* Go get them off disk. */
886 misc.extsyms = ((Elf32_External_Sym *)bfd_malloc (misc.symtab_hdr->sh_size));
887 if (misc.extsyms == NULL)
889 tidyup_after_error (&misc);
893 misc.free_extsyms = misc.extsyms;
894 if (bfd_seek (abfd, misc.symtab_hdr->sh_offset, SEEK_SET) != 0
895 || (bfd_read (misc.extsyms, 1, misc.symtab_hdr->sh_size, abfd)
896 != misc.symtab_hdr->sh_size))
898 tidyup_after_error (&misc);
903 if (shndx_hdr->sh_size != 0)
907 amt = misc.symtab_hdr->sh_info * sizeof (Elf_External_Sym_Shndx);
908 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
909 if (shndx_buf == NULL)
911 tidyup_after_error (&misc);
914 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
915 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
917 tidyup_after_error (&misc);
920 shndx_hdr->contents = (PTR) shndx_buf;
923 /* This is where all the relaxation actually get done. */
927 /* On the first pass we remove *all* page instructions and
928 relax the prolog for switch dispatch tables. This gets
929 us to the starting point for subsequent passes where
930 we add page instructions back in as needed. */
932 if (! ip2k_elf_relax_section_pass1 (abfd, sec, again, &misc))
934 tidyup_after_error (&misc);
942 /* Add page instructions back in as needed but we ignore
943 the issue with sections (functions) crossing a page
944 boundary until we have converged to an approximate
945 solution (i.e. nothing has changed on this relaxation
946 pass) and we then know roughly where the page boundaries
949 After we have have converged to an approximate solution
950 we set the final pass flag and continue relaxing. On these
951 final passes if a section (function) cross page boundary
952 we will add *all* the page instructions back into such
955 After adding *all* page instructions back into a section
956 which crosses a page bounbdary we reset the final pass flag
957 so the we will again interate until we find a new approximate
958 solution which is closer to the final solution. */
960 if (! ip2k_elf_relax_section_passN (abfd, sec, again,
963 tidyup_after_error (&misc);
969 /* If nothing has changed on this relaxation
970 pass restart the final relaxaton pass. */
971 if (! changed && last_section == sec)
973 /* If this was the final pass and we didn't reset
974 the final pass flag then we are done, otherwise
975 do another final pass. */
984 /* Perform some house keeping after relaxing the section. */
986 if (misc.free_relocs != NULL)
988 free (misc.free_relocs);
989 misc.free_relocs = NULL;
992 if (misc.free_contents != NULL)
994 if (! link_info->keep_memory)
995 free (misc.free_contents);
998 /* Cache the section contents for elf_link_input_bfd. */
999 elf_section_data (sec)->this_hdr.contents = misc.contents;
1002 misc.free_contents = NULL;
1005 if (misc.free_extsyms != NULL)
1007 if (! link_info->keep_memory)
1008 free (misc.free_extsyms);
1011 /* Cache the symbols for elf_link_input_bfd. */
1012 misc.symtab_hdr->contents = misc.extsyms;
1015 misc.free_extsyms = NULL;
1022 tidyup_after_error (misc)
1025 if (misc->free_relocs != NULL)
1027 free (misc->free_relocs);
1028 misc->free_relocs = NULL;
1031 if (misc->free_contents != NULL)
1033 free (misc->free_contents);
1034 misc->free_contents = NULL;
1037 if (misc->free_extsyms != NULL)
1039 free (misc->free_extsyms);
1040 misc->free_extsyms = NULL;
1046 /* This function handles relaxation during the first pass. */
1049 ip2k_elf_relax_section_pass1 (abfd, sec, again, misc)
1055 Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
1056 Elf_Internal_Rela *irel;
1058 /* Walk thru the section looking for relaxation opertunities. */
1059 for (irel = misc->irelbase; irel < irelend; irel++)
1061 if (ELF32_R_TYPE (irel->r_info) == (int) R_IP2K_PAGE3)
1063 bfd_byte code0 = bfd_get_8 (abfd,
1064 misc->contents + irel->r_offset);
1065 bfd_byte code1 = bfd_get_8 (abfd,
1066 misc->contents + irel->r_offset + 1);
1068 /* Verify that this is the PAGE opcode. */
1069 if (IS_PAGE_OPCODE (code0, code1))
1071 /* Note that we've changed the relocs, section contents, etc. */
1072 elf_section_data (sec)->relocs = misc->irelbase;
1073 misc->free_relocs = NULL;
1075 elf_section_data (sec)->this_hdr.contents = misc->contents;
1076 misc->free_contents = NULL;
1078 misc->symtab_hdr->contents = (bfd_byte *) misc->extsyms;
1079 misc->free_extsyms = NULL;
1081 /* Handle switch dispatch tables/prologues. */
1082 if (! relax_switch_dispatch_tables_pass1 (abfd, sec,
1083 irel->r_offset, misc))
1086 /* Fix the relocation's type. */
1087 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1090 /* Delete the PAGE insn. */
1091 if (! ip2k_elf_relax_delete_bytes (abfd, sec,
1093 sizeof (page_opcode)))
1096 /* That will change things, so, we should relax again.
1097 Note that this is not required, and it may be slow. */
1106 /* This function handles relaxation for 2nd and subsequent passes. */
1109 ip2k_elf_relax_section_passN (abfd, sec, again, final_pass, misc)
1113 boolean *final_pass;
1116 Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
1117 Elf_Internal_Rela *irel;
1120 /* If we are on the final relaxation pass and the section crosses
1121 then set a flag to indicate that *all* page instructions need
1122 to be added back into this section. */
1125 add_all = (PAGENO (BASEADDR (sec))
1126 != PAGENO (BASEADDR (sec) + sec->_cooked_size));
1128 /* If this section crosses a page boundary set the crossed
1129 page boundary flag. */
1131 sec->userdata = sec;
1134 /* If the section had previously crossed a page boundary
1135 but on this pass does not then reset crossed page
1136 boundary flag and rerun the 1st relaxation pass on
1140 sec->userdata = NULL;
1141 if (! ip2k_elf_relax_section_pass1 (abfd, sec, again, misc))
1149 /* Walk thru the section looking for call/jmp
1150 instructions which need a page instruction. */
1151 for (irel = misc->irelbase; irel < irelend; irel++)
1153 if (ELF32_R_TYPE (irel->r_info) == (int) R_IP2K_ADDR16CJP)
1155 /* Get the value of the symbol referred to by the reloc. */
1156 bfd_vma symval = symbol_value (abfd, misc->symtab_hdr, misc->extsyms,
1158 bfd_byte code0, code1;
1160 if (symval == UNDEFINED_SYMBOL)
1162 /* This appears to be a reference to an undefined
1163 symbol. Just ignore it--it will be caught by the
1164 regular reloc processing. */
1168 /* For simplicity of coding, we are going to modify the section
1169 contents, the section relocs, and the BFD symbol table. We
1170 must tell the rest of the code not to free up this
1171 information. It would be possible to instead create a table
1172 of changes which have to be made, as is done in coff-mips.c;
1173 that would be more work, but would require less memory when
1174 the linker is run. */
1176 /* Get the opcode. */
1177 code0 = bfd_get_8 (abfd, misc->contents + irel->r_offset);
1178 code1 = bfd_get_8 (abfd, misc->contents + irel->r_offset + 1);
1180 if (IS_JMP_OPCODE (code0, code1) || IS_CALL_OPCODE (code0, code1))
1184 if (! unrelax_switch_dispatch_tables_passN (abfd, sec,
1193 code0 = bfd_get_8 (abfd, misc->contents + irel->r_offset - 2);
1194 code1 = bfd_get_8 (abfd, misc->contents + irel->r_offset - 1);
1196 if (! IS_PAGE_OPCODE (code0, code1))
1198 bfd_vma value = symval + irel->r_addend;
1199 bfd_vma addr = BASEADDR (sec) + irel->r_offset;
1201 if (add_all || PAGENO (addr) != PAGENO (value))
1203 if (! add_page_insn (abfd, sec, irel, misc))
1206 /* That will have changed things, so, we must relax again. */
1214 /* If anything changed reset the final pass flag. */
1216 *final_pass = false;
1221 /* Parts of a Stabs entry. */
1223 #define STRDXOFF (0)
1225 #define OTHEROFF (5)
1228 #define STABSIZE (12)
1230 /* Adjust all the relocations entries after adding or inserting instructions. */
1233 adjust_all_relocations (abfd, sec, addr, endaddr, count, noadj)
1241 Elf_Internal_Shdr *symtab_hdr;
1242 Elf32_External_Sym *extsyms;
1245 Elf_Internal_Rela *irel, *irelend, *irelbase;
1246 Elf32_External_Sym *esym, *esymend;
1248 bfd_byte *stabp, *stabend, *stabcontents;
1249 Elf_Internal_Shdr *shndx_hdr;
1250 Elf_External_Sym_Shndx *sym_shndx;
1252 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1253 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1255 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1257 contents = elf_section_data (sec)->this_hdr.contents;
1259 irelbase = elf_section_data (sec)->relocs;
1260 irelend = irelbase + sec->reloc_count;
1262 for (irel = irelbase; irel < irelend; irel++)
1264 if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE)
1266 /* Get the value of the symbol referred to by the reloc. */
1267 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1269 Elf_Internal_Sym isym;
1271 Elf_External_Sym_Shndx *sym_shndx;
1272 Elf_Internal_Shdr *shndx_hdr;
1274 /* A local symbol. */
1276 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1277 sym_shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1278 sym_shndx = (sym_shndx
1279 ? sym_shndx + ELF32_R_SYM (irel->r_info) : NULL);
1280 bfd_elf32_swap_symbol_in (abfd,
1281 extsyms + ELF32_R_SYM (irel->r_info),
1284 if (isym.st_shndx == SHN_UNDEF)
1285 sym_sec = bfd_und_section_ptr;
1286 else if (isym.st_shndx == SHN_ABS)
1287 sym_sec = bfd_abs_section_ptr;
1288 else if (isym.st_shndx == SHN_COMMON)
1289 sym_sec = bfd_com_section_ptr;
1291 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1295 bfd_vma baseaddr = BASEADDR (sec);
1296 bfd_vma symval = BASEADDR (sym_sec) + isym.st_value
1299 if ((baseaddr + addr + noadj) <= symval
1300 && symval < (baseaddr + endaddr))
1301 irel->r_addend += count;
1306 /* Do this only for PC space relocations. */
1307 if (addr <= irel->r_offset && irel->r_offset < endaddr)
1308 irel->r_offset += count;
1311 /* Now fix the stab relocations. */
1312 stab = bfd_get_section_by_name (abfd, ".stab");
1315 irelbase = elf_section_data (stab)->relocs;
1316 irelend = irelbase + stab->reloc_count;
1318 /* Pull out the contents of the stab section. */
1319 if (elf_section_data (stab)->this_hdr.contents != NULL)
1320 stabcontents = elf_section_data (stab)->this_hdr.contents;
1323 stabcontents = (bfd_byte *) bfd_alloc (abfd, stab->_raw_size);
1324 if (stabcontents == NULL)
1326 if (! bfd_get_section_contents (abfd, stab, stabcontents,
1327 (file_ptr) 0, stab->_raw_size))
1330 /* We need to remember this. */
1331 elf_section_data (stab)->this_hdr.contents = stabcontents;
1334 stabend = stabcontents + stab->_raw_size;
1336 for (irel = irelbase; irel < irelend; irel++)
1338 if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE)
1340 /* Get the value of the symbol referred to by the reloc. */
1341 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1343 Elf_Internal_Sym isym;
1345 Elf_External_Sym_Shndx *sym_shndx;
1346 Elf_Internal_Shdr *shndx_hdr;
1348 /* A local symbol. */
1349 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1350 sym_shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1351 sym_shndx = (sym_shndx
1352 ? sym_shndx + ELF32_R_SYM (irel->r_info)
1355 bfd_elf32_swap_symbol_in (abfd,
1357 + ELF32_R_SYM (irel->r_info)),
1360 if (isym.st_shndx == SHN_UNDEF)
1361 sym_sec = bfd_und_section_ptr;
1362 else if (isym.st_shndx == SHN_ABS)
1363 sym_sec = bfd_abs_section_ptr;
1364 else if (isym.st_shndx == SHN_COMMON)
1365 sym_sec = bfd_com_section_ptr;
1367 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1373 unsigned char type, other;
1374 unsigned short desc;
1376 bfd_vma baseaddr = BASEADDR (sec);
1377 bfd_vma symval = BASEADDR (sym_sec) + isym.st_value
1380 if ((baseaddr + addr) <= symval
1381 && symval <= (baseaddr + endaddr))
1382 irel->r_addend += count;
1384 /* Go hunt up a function and fix its line info if needed. */
1385 stabp = stabcontents + irel->r_offset - 8;
1387 /* Go pullout the stab entry. */
1388 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1389 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1390 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1391 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1392 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1394 name = bfd_get_stab_name (type);
1396 if (strcmp (name, "FUN") == 0)
1398 int function_adjusted = 0;
1400 if (symval > (baseaddr + addr))
1401 /* Not in this function. */
1404 /* Hey we got a function hit. */
1406 for (;stabp < stabend; stabp += STABSIZE)
1408 /* Go pullout the stab entry. */
1409 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1410 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1411 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1412 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1413 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1414 name = bfd_get_stab_name (type);
1416 if (strcmp (name, "FUN") == 0)
1418 /* Hit another function entry. */
1419 if (function_adjusted)
1421 /* Adjust the value. */
1424 /* We need to put it back. */
1425 bfd_h_put_32 (abfd, value,stabp + VALOFF);
1428 /* And then bale out. */
1432 if (strcmp (name, "SLINE") == 0)
1434 /* Got a line entry. */
1435 if ((baseaddr + addr) <= (symval + value))
1437 /* Adjust the line entry. */
1440 /* We need to put it back. */
1441 bfd_h_put_32 (abfd, value,stabp + VALOFF);
1442 function_adjusted = 1;
1453 /* When adding an instruction back it is sometimes necessary to move any
1454 global or local symbol that was referencing the first instruction of
1455 the moved block to refer to the first instruction of the inserted block.
1457 For example adding a PAGE instruction before a CALL or JMP requires
1458 that any label on the CALL or JMP is moved to the PAGE insn. */
1461 /* Adjust the local symbols defined in this section. */
1462 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1463 sym_shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1465 esymend = esym + symtab_hdr->sh_info;
1466 for (; esym < esymend; esym++, sym_shndx = (sym_shndx ? sym_shndx + 1: NULL))
1468 Elf_Internal_Sym isym;
1469 Elf_External_Sym_Shndx dummy;
1471 bfd_elf32_swap_symbol_in (abfd, esym, sym_shndx, &isym);
1473 if (isym.st_shndx == shndx)
1475 if (addr <= isym.st_value && isym.st_value < endaddr)
1477 isym.st_value += count;
1478 bfd_elf32_swap_symbol_out (abfd, &isym, esym, &dummy);
1483 /* Now adjust the global symbols defined in this section. */
1484 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1485 sym_shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1486 esym = extsyms + symtab_hdr->sh_info;
1487 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1488 for (index = 0; esym < esymend;
1489 esym++, index++, sym_shndx = (sym_shndx ? sym_shndx + 1: NULL))
1491 Elf_Internal_Sym isym;
1492 struct elf_link_hash_entry *sym_hash;
1494 bfd_elf32_swap_symbol_in (abfd, esym, sym_shndx, &isym);
1495 sym_hash = elf_sym_hashes (abfd)[index];
1497 if (isym.st_shndx == shndx
1498 && (sym_hash->root.type == bfd_link_hash_defined
1499 || sym_hash->root.type == bfd_link_hash_defweak)
1500 && sym_hash->root.u.def.section == sec)
1502 if (addr <= sym_hash->root.u.def.value
1503 && sym_hash->root.u.def.value < endaddr)
1505 Elf_External_Sym_Shndx dummy;
1507 sym_hash->root.u.def.value += count;
1508 bfd_elf32_swap_symbol_out (abfd, &isym, esym, &dummy);
1517 add_page_insn (abfd, sec, irel, misc)
1520 Elf_Internal_Rela *irel;
1523 /* Note that we've changed the relocs, section contents, etc. */
1524 elf_section_data (sec)->relocs = misc->irelbase;
1525 misc->free_relocs = NULL;
1527 elf_section_data (sec)->this_hdr.contents = misc->contents;
1528 misc->free_contents = NULL;
1530 misc->symtab_hdr->contents = (bfd_byte *) misc->extsyms;
1531 misc->free_extsyms = NULL;
1533 /* Add the PAGE insn. */
1534 if (! ip2k_elf_relax_add_bytes (abfd, sec, irel->r_offset,
1536 sizeof (page_opcode),
1537 sizeof (page_opcode)))
1541 Elf32_Internal_Rela * jrel = irel - 1;
1543 /* Add relocation for PAGE insn added. */
1544 if (ELF32_R_TYPE (jrel->r_info) != R_IP2K_NONE)
1546 bfd_byte code0, code1;
1549 /* Get the opcode. */
1550 code0 = bfd_get_8 (abfd, misc->contents + irel->r_offset);
1551 code1 = bfd_get_8 (abfd, misc->contents + irel->r_offset + 1);
1553 if (IS_JMP_OPCODE (code0, code1))
1554 msg = "\tJMP instruction missing a preceeding PAGE instruction in %s\n\n";
1556 else if (IS_CALL_OPCODE (code0, code1))
1557 msg = "\tCALL instruction missing a preceeding PAGE instruction in %s\n\n";
1561 fprintf (stderr, "\n\t *** LINKER RELAXATION failure ***\n");
1562 fprintf (stderr, msg, sec->owner->filename);
1568 jrel->r_addend = irel->r_addend;
1569 jrel->r_offset = irel->r_offset - sizeof (page_opcode);
1570 jrel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1577 /* Insert bytes into a section while relaxing. */
1580 ip2k_elf_relax_add_bytes (abfd, sec, addr, bytes, count, noadj)
1584 const bfd_byte *bytes;
1588 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
1589 bfd_vma endaddr = sec->_cooked_size;
1591 /* Make room to insert the bytes. */
1592 memmove (contents + addr + count, contents + addr, endaddr - addr);
1594 /* Insert the bytes into the section. */
1595 memcpy (contents + addr, bytes, count);
1597 sec->_cooked_size += count;
1599 adjust_all_relocations (abfd, sec, addr, endaddr, count, noadj);
1603 /* Delete some bytes from a section while relaxing. */
1606 ip2k_elf_relax_delete_bytes (abfd, sec, addr, count)
1612 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
1613 bfd_vma endaddr = sec->_cooked_size;
1615 /* Actually delete the bytes. */
1616 memmove (contents + addr, contents + addr + count,
1617 endaddr - addr - count);
1619 sec->_cooked_size -= count;
1621 adjust_all_relocations (abfd, sec, addr + count, endaddr, -count, 0);
1625 /* -------------------------------------------------------------------- */
1627 /* XXX: The following code is the result of a cut&paste. This unfortunate
1628 practice is very widespread in the various target back-end files. */
1630 /* Set the howto pointer for a IP2K ELF reloc. */
1633 ip2k_info_to_howto_rela (abfd, cache_ptr, dst)
1634 bfd * abfd ATTRIBUTE_UNUSED;
1635 arelent * cache_ptr;
1636 Elf32_Internal_Rela * dst;
1638 unsigned int r_type;
1640 r_type = ELF32_R_TYPE (dst->r_info);
1644 cache_ptr->howto = & ip2k_elf_howto_table [r_type];
1649 /* Perform a single relocation.
1650 By default we use the standard BFD routines. */
1652 static bfd_reloc_status_type
1653 ip2k_final_link_relocate (howto, input_bfd, input_section, contents, rel,
1655 reloc_howto_type * howto;
1657 asection * input_section;
1658 bfd_byte * contents;
1659 Elf_Internal_Rela * rel;
1662 bfd_reloc_status_type r = bfd_reloc_ok;
1664 switch (howto->type)
1666 /* Handle data space relocations. */
1669 if ((relocation & IP2K_DATA_MASK) == IP2K_DATA_VALUE)
1670 relocation &= ~IP2K_DATA_MASK;
1672 r = bfd_reloc_notsupported;
1675 case R_IP2K_LO8DATA:
1676 case R_IP2K_HI8DATA:
1677 case R_IP2K_EX8DATA:
1680 /* Handle insn space relocations. */
1681 case R_IP2K_ADDR16CJP:
1683 case R_IP2K_LO8INSN:
1684 case R_IP2K_HI8INSN:
1685 case R_IP2K_PC_SKIP:
1686 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1687 relocation &= ~IP2K_INSN_MASK;
1689 r = bfd_reloc_notsupported;
1693 /* If this is a relocation involving a TEXT
1694 symbol, reduce it to a word address. */
1695 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1696 howto = &ip2k_elf_howto_table[ (int) R_IP2K_TEXT];
1699 /* Pass others through. */
1704 /* Only install relocation if above tests did not disqualify it. */
1705 if (r == bfd_reloc_ok)
1706 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1707 contents, rel->r_offset,
1708 relocation, rel->r_addend);
1713 /* Relocate a IP2K ELF section.
1714 There is some attempt to make this function usable for many architectures,
1715 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
1716 if only to serve as a learning tool.
1718 The RELOCATE_SECTION function is called by the new ELF backend linker
1719 to handle the relocations for a section.
1721 The relocs are always passed as Rela structures; if the section
1722 actually uses Rel structures, the r_addend field will always be
1725 This function is responsible for adjusting the section contents as
1726 necessary, and (if using Rela relocs and generating a relocateable
1727 output file) adjusting the reloc addend as necessary.
1729 This function does not have to worry about setting the reloc
1730 address or the reloc symbol index.
1732 LOCAL_SYMS is a pointer to the swapped in local symbols.
1734 LOCAL_SECTIONS is an array giving the section in the input file
1735 corresponding to the st_shndx field of each local symbol.
1737 The global hash table entry for the global symbols can be found
1738 via elf_sym_hashes (input_bfd).
1740 When generating relocateable output, this function must handle
1741 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1742 going to be the section symbol corresponding to the output
1743 section, which means that the addend must be adjusted
1747 ip2k_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1748 contents, relocs, local_syms, local_sections)
1749 bfd * output_bfd ATTRIBUTE_UNUSED;
1750 struct bfd_link_info * info;
1752 asection * input_section;
1753 bfd_byte * contents;
1754 Elf_Internal_Rela * relocs;
1755 Elf_Internal_Sym * local_syms;
1756 asection ** local_sections;
1758 Elf_Internal_Shdr * symtab_hdr;
1759 struct elf_link_hash_entry ** sym_hashes;
1760 Elf_Internal_Rela * rel;
1761 Elf_Internal_Rela * relend;
1763 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1764 sym_hashes = elf_sym_hashes (input_bfd);
1765 relend = relocs + input_section->reloc_count;
1767 for (rel = relocs; rel < relend; rel ++)
1769 reloc_howto_type * howto;
1770 unsigned long r_symndx;
1771 Elf_Internal_Sym * sym;
1773 struct elf_link_hash_entry * h;
1775 bfd_reloc_status_type r;
1776 const char * name = NULL;
1779 r_type = ELF32_R_TYPE (rel->r_info);
1781 r_symndx = ELF32_R_SYM (rel->r_info);
1783 if (info->relocateable)
1785 /* This is a relocateable link. We don't have to change
1786 anything, unless the reloc is against a section symbol,
1787 in which case we have to adjust according to where the
1788 section symbol winds up in the output section. */
1789 if (r_symndx < symtab_hdr->sh_info)
1791 sym = local_syms + r_symndx;
1793 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1795 sec = local_sections [r_symndx];
1796 rel->r_addend += sec->output_offset + sym->st_value;
1803 /* This is a final link. */
1804 howto = ip2k_elf_howto_table + ELF32_R_TYPE (rel->r_info);
1809 if (r_symndx < symtab_hdr->sh_info)
1811 sym = local_syms + r_symndx;
1812 sec = local_sections [r_symndx];
1813 relocation = BASEADDR (sec) + sym->st_value;
1815 name = bfd_elf_string_from_elf_section
1816 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1817 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1821 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
1823 while (h->root.type == bfd_link_hash_indirect
1824 || h->root.type == bfd_link_hash_warning)
1825 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1827 name = h->root.root.string;
1829 if (h->root.type == bfd_link_hash_defined
1830 || h->root.type == bfd_link_hash_defweak)
1832 sec = h->root.u.def.section;
1833 relocation = h->root.u.def.value + BASEADDR (sec);
1835 else if (h->root.type == bfd_link_hash_undefweak)
1841 if (! ((*info->callbacks->undefined_symbol)
1842 (info, h->root.root.string, input_bfd,
1843 input_section, rel->r_offset,
1844 (! info->shared || info->no_undefined))))
1850 /* Finally, the sole IP2K-specific part. */
1851 r = ip2k_final_link_relocate (howto, input_bfd, input_section,
1852 contents, rel, relocation);
1854 if (r != bfd_reloc_ok)
1856 const char * msg = (const char *) NULL;
1860 case bfd_reloc_overflow:
1861 r = info->callbacks->reloc_overflow
1862 (info, name, howto->name, (bfd_vma) 0,
1863 input_bfd, input_section, rel->r_offset);
1866 case bfd_reloc_undefined:
1867 r = info->callbacks->undefined_symbol
1868 (info, name, input_bfd, input_section, rel->r_offset, true);
1871 case bfd_reloc_outofrange:
1872 msg = _("internal error: out of range error");
1875 /* This is how ip2k_final_link_relocate tells us of a non-kosher
1876 reference between insn & data address spaces. */
1877 case bfd_reloc_notsupported:
1878 if (sym != NULL) /* Only if it's not an unresolved symbol. */
1879 msg = _("unsupported relocation between data/insn address spaces");
1882 case bfd_reloc_dangerous:
1883 msg = _("internal error: dangerous relocation");
1887 msg = _("internal error: unknown error");
1892 r = info->callbacks->warning
1893 (info, msg, name, input_bfd, input_section, rel->r_offset);
1904 ip2k_elf_gc_mark_hook (sec, info, rel, h, sym)
1906 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1907 Elf_Internal_Rela *rel;
1908 struct elf_link_hash_entry *h;
1909 Elf_Internal_Sym *sym;
1913 switch (ELF32_R_TYPE (rel->r_info))
1916 case R_IP2K_GNU_VTINHERIT:
1917 case R_IP2K_GNU_VTENTRY:
1922 switch (h->root.type)
1924 case bfd_link_hash_defined:
1925 case bfd_link_hash_defweak:
1926 return h->root.u.def.section;
1928 case bfd_link_hash_common:
1929 return h->root.u.c.p->section;
1938 if (!(elf_bad_symtab (sec->owner)
1939 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1940 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1941 && sym->st_shndx != SHN_COMMON))
1943 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1950 ip2k_elf_gc_sweep_hook (abfd, info, sec, relocs)
1951 bfd *abfd ATTRIBUTE_UNUSED;
1952 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1953 asection *sec ATTRIBUTE_UNUSED;
1954 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1956 /* we don't use got and plt entries for ip2k */
1961 /* -------------------------------------------------------------------- */
1964 #define TARGET_BIG_SYM bfd_elf32_ip2k_vec
1965 #define TARGET_BIG_NAME "elf32-ip2k"
1967 #define ELF_ARCH bfd_arch_ip2k
1968 #define ELF_MACHINE_CODE EM_IP2K
1969 #define ELF_MAXPAGESIZE 1 /* No pages on the IP2K */
1974 #define elf_info_to_howto_rel NULL
1975 #define elf_info_to_howto ip2k_info_to_howto_rela
1977 #define elf_backend_can_gc_sections 1
1978 #define elf_backend_gc_mark_hook ip2k_elf_gc_mark_hook
1979 #define elf_backend_gc_sweep_hook ip2k_elf_gc_sweep_hook
1981 #define elf_backend_relocate_section ip2k_elf_relocate_section
1983 #define elf_symbol_leading_char '_'
1984 #define bfd_elf32_bfd_reloc_type_lookup ip2k_reloc_type_lookup
1985 #define bfd_elf32_bfd_relax_section ip2k_elf_relax_section
1988 #include "elf32-target.h"