1 /* Ubicom IP2xxx specific support for 32-bit ELF
2 Copyright (C) 2000-2017 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 /* Struct used to pass miscellaneous paramaters which
28 helps to avoid overly long parameter lists. */
31 Elf_Internal_Shdr
* symtab_hdr
;
32 Elf_Internal_Rela
* irelbase
;
34 Elf_Internal_Sym
* isymbuf
;
39 unsigned short opcode
;
43 static bfd_boolean ip2k_relaxed
= FALSE
;
45 static const struct ip2k_opcode ip2k_page_opcode
[] =
47 {0x0010, 0xFFF8}, /* Page. */
51 #define IS_PAGE_OPCODE(code) \
52 ip2k_is_opcode (code, ip2k_page_opcode)
54 static const struct ip2k_opcode ip2k_jmp_opcode
[] =
56 {0xE000, 0xE000}, /* Jmp. */
60 #define IS_JMP_OPCODE(code) \
61 ip2k_is_opcode (code, ip2k_jmp_opcode)
63 static const struct ip2k_opcode ip2k_snc_opcode
[] =
65 {0xA00B, 0xFFFF}, /* Snc. */
69 #define IS_SNC_OPCODE(code) \
70 ip2k_is_opcode (code, ip2k_snc_opcode)
72 static const struct ip2k_opcode ip2k_inc_1sp_opcode
[] =
74 {0x2B81, 0xFFFF}, /* Inc 1(SP). */
78 #define IS_INC_1SP_OPCODE(code) \
79 ip2k_is_opcode (code, ip2k_inc_1sp_opcode)
81 static const struct ip2k_opcode ip2k_add_2sp_w_opcode
[] =
83 {0x1F82, 0xFFFF}, /* Add 2(SP),w. */
87 #define IS_ADD_2SP_W_OPCODE(code) \
88 ip2k_is_opcode (code, ip2k_add_2sp_w_opcode)
90 static const struct ip2k_opcode ip2k_add_w_wreg_opcode
[] =
92 {0x1C0A, 0xFFFF}, /* Add w,wreg. */
93 {0x1E0A, 0xFFFF}, /* Add wreg,w. */
97 #define IS_ADD_W_WREG_OPCODE(code) \
98 ip2k_is_opcode (code, ip2k_add_w_wreg_opcode)
100 static const struct ip2k_opcode ip2k_add_pcl_w_opcode
[] =
102 {0x1E09, 0xFFFF}, /* Add pcl,w. */
106 #define IS_ADD_PCL_W_OPCODE(code) \
107 ip2k_is_opcode (code, ip2k_add_pcl_w_opcode)
109 static const struct ip2k_opcode ip2k_skip_opcodes
[] =
111 {0xB000, 0xF000}, /* sb */
112 {0xA000, 0xF000}, /* snb */
113 {0x7600, 0xFE00}, /* cse/csne #lit */
114 {0x5800, 0xFC00}, /* incsnz */
115 {0x4C00, 0xFC00}, /* decsnz */
116 {0x4000, 0xFC00}, /* cse/csne */
117 {0x3C00, 0xFC00}, /* incsz */
118 {0x2C00, 0xFC00}, /* decsz */
122 #define IS_SKIP_OPCODE(code) \
123 ip2k_is_opcode (code, ip2k_skip_opcodes)
125 /* Relocation tables. */
126 static reloc_howto_type ip2k_elf_howto_table
[] =
128 #define IP2K_HOWTO(t,rs,s,bs,pr,bp,name,sm,dm) \
129 HOWTO(t, /* type */ \
130 rs, /* rightshift */ \
131 s, /* size (0 = byte, 1 = short, 2 = long) */ \
133 pr, /* pc_relative */ \
135 complain_overflow_dont,/* complain_on_overflow */ \
136 bfd_elf_generic_reloc,/* special_function */ \
138 FALSE, /* partial_inplace */ \
141 pr) /* pcrel_offset */
143 /* This reloc does nothing. */
144 IP2K_HOWTO (R_IP2K_NONE
, 0,3,0, FALSE
, 0, "R_IP2K_NONE", 0, 0),
145 /* A 16 bit absolute relocation. */
146 IP2K_HOWTO (R_IP2K_16
, 0,1,16, FALSE
, 0, "R_IP2K_16", 0, 0xffff),
147 /* A 32 bit absolute relocation. */
148 IP2K_HOWTO (R_IP2K_32
, 0,2,32, FALSE
, 0, "R_IP2K_32", 0, 0xffffffff),
149 /* A 8-bit data relocation for the FR9 field. Ninth bit is computed specially. */
150 IP2K_HOWTO (R_IP2K_FR9
, 0,1,9, FALSE
, 0, "R_IP2K_FR9", 0, 0x00ff),
151 /* A 4-bit data relocation. */
152 IP2K_HOWTO (R_IP2K_BANK
, 8,1,4, FALSE
, 0, "R_IP2K_BANK", 0, 0x000f),
153 /* A 13-bit insn relocation - word address => right-shift 1 bit extra. */
154 IP2K_HOWTO (R_IP2K_ADDR16CJP
, 1,1,13, FALSE
, 0, "R_IP2K_ADDR16CJP", 0, 0x1fff),
155 /* A 3-bit insn relocation - word address => right-shift 1 bit extra. */
156 IP2K_HOWTO (R_IP2K_PAGE3
, 14,1,3, FALSE
, 0, "R_IP2K_PAGE3", 0, 0x0007),
157 /* Two 8-bit data relocations. */
158 IP2K_HOWTO (R_IP2K_LO8DATA
, 0,1,8, FALSE
, 0, "R_IP2K_LO8DATA", 0, 0x00ff),
159 IP2K_HOWTO (R_IP2K_HI8DATA
, 8,1,8, FALSE
, 0, "R_IP2K_HI8DATA", 0, 0x00ff),
160 /* Two 8-bit insn relocations. word address => right-shift 1 bit extra. */
161 IP2K_HOWTO (R_IP2K_LO8INSN
, 1,1,8, FALSE
, 0, "R_IP2K_LO8INSN", 0, 0x00ff),
162 IP2K_HOWTO (R_IP2K_HI8INSN
, 9,1,8, FALSE
, 0, "R_IP2K_HI8INSN", 0, 0x00ff),
164 /* Special 1 bit relocation for SKIP instructions. */
165 IP2K_HOWTO (R_IP2K_PC_SKIP
, 1,1,1, FALSE
, 12, "R_IP2K_PC_SKIP", 0xfffe, 0x1000),
166 /* 16 bit word address. */
167 IP2K_HOWTO (R_IP2K_TEXT
, 1,1,16, FALSE
, 0, "R_IP2K_TEXT", 0, 0xffff),
168 /* A 7-bit offset relocation for the FR9 field. Eigth and ninth bit comes from insn. */
169 IP2K_HOWTO (R_IP2K_FR_OFFSET
, 0,1,9, FALSE
, 0, "R_IP2K_FR_OFFSET", 0x180, 0x007f),
170 /* Bits 23:16 of an address. */
171 IP2K_HOWTO (R_IP2K_EX8DATA
, 16,1,8, FALSE
, 0, "R_IP2K_EX8DATA", 0, 0x00ff),
175 /* Map BFD reloc types to IP2K ELF reloc types. */
177 static reloc_howto_type
*
178 ip2k_reloc_type_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
179 bfd_reloc_code_real_type code
)
181 /* Note that the ip2k_elf_howto_table is indxed by the R_
182 constants. Thus, the order that the howto records appear in the
183 table *must* match the order of the relocation types defined in
184 include/elf/ip2k.h. */
189 return &ip2k_elf_howto_table
[ (int) R_IP2K_NONE
];
191 return &ip2k_elf_howto_table
[ (int) R_IP2K_16
];
193 return &ip2k_elf_howto_table
[ (int) R_IP2K_32
];
194 case BFD_RELOC_IP2K_FR9
:
195 return &ip2k_elf_howto_table
[ (int) R_IP2K_FR9
];
196 case BFD_RELOC_IP2K_BANK
:
197 return &ip2k_elf_howto_table
[ (int) R_IP2K_BANK
];
198 case BFD_RELOC_IP2K_ADDR16CJP
:
199 return &ip2k_elf_howto_table
[ (int) R_IP2K_ADDR16CJP
];
200 case BFD_RELOC_IP2K_PAGE3
:
201 return &ip2k_elf_howto_table
[ (int) R_IP2K_PAGE3
];
202 case BFD_RELOC_IP2K_LO8DATA
:
203 return &ip2k_elf_howto_table
[ (int) R_IP2K_LO8DATA
];
204 case BFD_RELOC_IP2K_HI8DATA
:
205 return &ip2k_elf_howto_table
[ (int) R_IP2K_HI8DATA
];
206 case BFD_RELOC_IP2K_LO8INSN
:
207 return &ip2k_elf_howto_table
[ (int) R_IP2K_LO8INSN
];
208 case BFD_RELOC_IP2K_HI8INSN
:
209 return &ip2k_elf_howto_table
[ (int) R_IP2K_HI8INSN
];
210 case BFD_RELOC_IP2K_PC_SKIP
:
211 return &ip2k_elf_howto_table
[ (int) R_IP2K_PC_SKIP
];
212 case BFD_RELOC_IP2K_TEXT
:
213 return &ip2k_elf_howto_table
[ (int) R_IP2K_TEXT
];
214 case BFD_RELOC_IP2K_FR_OFFSET
:
215 return &ip2k_elf_howto_table
[ (int) R_IP2K_FR_OFFSET
];
216 case BFD_RELOC_IP2K_EX8DATA
:
217 return &ip2k_elf_howto_table
[ (int) R_IP2K_EX8DATA
];
219 /* Pacify gcc -Wall. */
225 static reloc_howto_type
*
226 ip2k_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
, const char *r_name
)
231 i
< sizeof (ip2k_elf_howto_table
) / sizeof (ip2k_elf_howto_table
[0]);
233 if (ip2k_elf_howto_table
[i
].name
!= NULL
234 && strcasecmp (ip2k_elf_howto_table
[i
].name
, r_name
) == 0)
235 return &ip2k_elf_howto_table
[i
];
241 ip2k_get_mem (bfd
*abfd ATTRIBUTE_UNUSED
,
247 * ptr
++ = bfd_get_8 (abfd
, addr
++);
251 ip2k_is_opcode (bfd_byte
*code
, const struct ip2k_opcode
*opcodes
)
253 unsigned short insn
= (code
[0] << 8) | code
[1];
255 while (opcodes
->mask
!= 0)
257 if ((insn
& opcodes
->mask
) == opcodes
->opcode
)
266 #define PAGENO(ABSADDR) ((ABSADDR) & 0xFFFFC000)
267 #define BASEADDR(SEC) ((SEC)->output_section->vma + (SEC)->output_offset)
269 #define UNDEFINED_SYMBOL (~(bfd_vma)0)
271 /* Return the value of the symbol associated with the relocation IREL. */
274 symbol_value (bfd
*abfd
,
275 Elf_Internal_Shdr
*symtab_hdr
,
276 Elf_Internal_Sym
*isymbuf
,
277 Elf_Internal_Rela
*irel
)
279 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
281 Elf_Internal_Sym
*isym
;
284 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
285 if (isym
->st_shndx
== SHN_UNDEF
)
286 sym_sec
= bfd_und_section_ptr
;
287 else if (isym
->st_shndx
== SHN_ABS
)
288 sym_sec
= bfd_abs_section_ptr
;
289 else if (isym
->st_shndx
== SHN_COMMON
)
290 sym_sec
= bfd_com_section_ptr
;
292 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
294 return isym
->st_value
+ BASEADDR (sym_sec
);
299 struct elf_link_hash_entry
*h
;
301 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
302 h
= elf_sym_hashes (abfd
)[indx
];
303 BFD_ASSERT (h
!= NULL
);
305 if (h
->root
.type
!= bfd_link_hash_defined
306 && h
->root
.type
!= bfd_link_hash_defweak
)
307 return UNDEFINED_SYMBOL
;
309 return (h
->root
.u
.def
.value
+ BASEADDR (h
->root
.u
.def
.section
));
313 /* Determine if the instruction sequence matches that for
314 the prologue of a switch dispatch table with fewer than
343 ip2k_is_switch_table_128 (bfd
*abfd ATTRIBUTE_UNUSED
,
351 /* Check current page-jmp. */
352 if (addr
+ 4 > sec
->size
)
355 ip2k_get_mem (abfd
, contents
+ addr
, 4, code
);
357 if ((! IS_PAGE_OPCODE (code
+ 0))
358 || (! IS_JMP_OPCODE (code
+ 2)))
367 /* Check previous 2 instructions. */
368 ip2k_get_mem (abfd
, contents
+ addr
- 4, 4, code
);
369 if ((IS_ADD_W_WREG_OPCODE (code
+ 0))
370 && (IS_ADD_PCL_W_OPCODE (code
+ 2)))
373 if ((! IS_PAGE_OPCODE (code
+ 0))
374 || (! IS_JMP_OPCODE (code
+ 2)))
382 /* Determine if the instruction sequence matches that for
383 the prologue switch dispatch table with fewer than
384 256 entries but more than 127.
387 push %lo8insn(label) ; Push address of table
389 add w,wreg ; index*2 => offset
391 inc 1(sp) ; Propagate MSB into table address
392 add 2(sp),w ; Add low bits of offset to table address
393 snc ; and handle any carry-out
396 page __indjmp ; Do an indirect jump to that location
398 label: ; case dispatch table starts here
408 push %lo8insn(label) ; Push address of table
410 add 2(sp),w ; Add low bits of offset to table address
411 snc ; and handle any carry-out
414 page __indjmp ; Do an indirect jump to that location
416 label: ; case dispatch table starts here
423 ip2k_is_switch_table_256 (bfd
*abfd ATTRIBUTE_UNUSED
,
431 /* Check current page-jmp. */
432 if (addr
+ 4 > sec
->size
)
435 ip2k_get_mem (abfd
, contents
+ addr
, 4, code
);
436 if ((! IS_PAGE_OPCODE (code
+ 0))
437 || (! IS_JMP_OPCODE (code
+ 2)))
446 /* Check previous 8 instructions. */
447 ip2k_get_mem (abfd
, contents
+ addr
- 16, 16, code
);
448 if ((IS_ADD_W_WREG_OPCODE (code
+ 0))
449 && (IS_SNC_OPCODE (code
+ 2))
450 && (IS_INC_1SP_OPCODE (code
+ 4))
451 && (IS_ADD_2SP_W_OPCODE (code
+ 6))
452 && (IS_SNC_OPCODE (code
+ 8))
453 && (IS_INC_1SP_OPCODE (code
+ 10))
454 && (IS_PAGE_OPCODE (code
+ 12))
455 && (IS_JMP_OPCODE (code
+ 14)))
458 if ((IS_ADD_W_WREG_OPCODE (code
+ 2))
459 && (IS_SNC_OPCODE (code
+ 4))
460 && (IS_INC_1SP_OPCODE (code
+ 6))
461 && (IS_ADD_2SP_W_OPCODE (code
+ 8))
462 && (IS_SNC_OPCODE (code
+ 10))
463 && (IS_INC_1SP_OPCODE (code
+ 12))
464 && (IS_JMP_OPCODE (code
+ 14)))
467 if ((! IS_PAGE_OPCODE (code
+ 0))
468 || (! IS_JMP_OPCODE (code
+ 2)))
476 /* Returns the expected page state for the given instruction not including
477 the effect of page instructions. */
480 ip2k_nominal_page_bits (bfd
*abfd ATTRIBUTE_UNUSED
,
485 bfd_vma page
= PAGENO (BASEADDR (sec
) + addr
);
487 /* Check if section flows into this page. If not then the page
488 bits are assumed to match the PC. This will be true unless
489 the user has a page instruction without a call/jump, in which
490 case they are on their own. */
491 if (PAGENO (BASEADDR (sec
)) == page
)
494 /* Section flows across page boundary. The page bits should match
495 the PC unless there is a possible flow from the previous page,
496 in which case it is not possible to determine the value of the
498 while (PAGENO (BASEADDR (sec
) + addr
- 2) == page
)
503 ip2k_get_mem (abfd
, contents
+ addr
, 2, code
);
504 if (!IS_PAGE_OPCODE (code
))
507 /* Found a page instruction, check if jump table. */
508 if (ip2k_is_switch_table_128 (abfd
, sec
, addr
, contents
) != -1)
509 /* Jump table => page is conditional. */
512 if (ip2k_is_switch_table_256 (abfd
, sec
, addr
, contents
) != -1)
513 /* Jump table => page is conditional. */
516 /* Found a page instruction, check if conditional. */
519 ip2k_get_mem (abfd
, contents
+ addr
- 2, 2, code
);
520 if (IS_SKIP_OPCODE (code
))
521 /* Page is conditional. */
525 /* Unconditional page instruction => page bits should be correct. */
529 /* Flow from previous page => page bits are impossible to determine. */
534 ip2k_test_page_insn (bfd
*abfd ATTRIBUTE_UNUSED
,
536 Elf_Internal_Rela
*irel
,
541 /* Get the value of the symbol referred to by the reloc. */
542 symval
= symbol_value (abfd
, misc
->symtab_hdr
, misc
->isymbuf
, irel
);
543 if (symval
== UNDEFINED_SYMBOL
)
544 /* This appears to be a reference to an undefined
545 symbol. Just ignore it--it will be caught by the
546 regular reloc processing. */
549 /* Test if we can delete this page instruction. */
550 if (PAGENO (symval
+ irel
->r_addend
) !=
551 ip2k_nominal_page_bits (abfd
, sec
, irel
->r_offset
, misc
->contents
))
557 /* Parts of a Stabs entry. */
566 /* Adjust all the relocations entries after adding or inserting instructions. */
569 adjust_all_relocations (bfd
*abfd
,
576 Elf_Internal_Shdr
*symtab_hdr
;
577 Elf_Internal_Sym
*isymbuf
, *isym
, *isymend
;
579 Elf_Internal_Rela
*irel
, *irelend
, *irelbase
;
580 struct elf_link_hash_entry
**sym_hashes
;
581 struct elf_link_hash_entry
**end_hashes
;
582 unsigned int symcount
;
585 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
586 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
588 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
590 irelbase
= elf_section_data (sec
)->relocs
;
591 irelend
= irelbase
+ sec
->reloc_count
;
593 for (irel
= irelbase
; irel
< irelend
; irel
++)
595 if (ELF32_R_TYPE (irel
->r_info
) != R_IP2K_NONE
)
597 /* Get the value of the symbol referred to by the reloc. */
598 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
602 /* A local symbol. */
603 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
604 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
606 if (isym
->st_shndx
== shndx
)
608 bfd_vma baseaddr
= BASEADDR (sec
);
609 bfd_vma symval
= BASEADDR (sym_sec
) + isym
->st_value
612 if ((baseaddr
+ addr
+ noadj
) <= symval
613 && symval
< (baseaddr
+ endaddr
))
614 irel
->r_addend
+= count
;
619 /* Do this only for PC space relocations. */
620 if (addr
<= irel
->r_offset
&& irel
->r_offset
< endaddr
)
621 irel
->r_offset
+= count
;
624 /* Now fix the stab relocations. */
625 stab
= bfd_get_section_by_name (abfd
, ".stab");
628 bfd_byte
*stabcontents
, *stabend
, *stabp
;
629 bfd_size_type stab_size
= stab
->rawsize
? stab
->rawsize
: stab
->size
;
631 irelbase
= elf_section_data (stab
)->relocs
;
632 irelend
= irelbase
+ stab
->reloc_count
;
634 /* Pull out the contents of the stab section. */
635 if (elf_section_data (stab
)->this_hdr
.contents
!= NULL
)
636 stabcontents
= elf_section_data (stab
)->this_hdr
.contents
;
639 if (!bfd_malloc_and_get_section (abfd
, stab
, &stabcontents
))
641 if (stabcontents
!= NULL
)
646 /* We need to remember this. */
647 elf_section_data (stab
)->this_hdr
.contents
= stabcontents
;
650 stabend
= stabcontents
+ stab_size
;
652 for (irel
= irelbase
; irel
< irelend
; irel
++)
654 if (ELF32_R_TYPE (irel
->r_info
) != R_IP2K_NONE
)
656 /* Get the value of the symbol referred to by the reloc. */
657 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
661 /* A local symbol. */
662 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
663 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
670 bfd_vma baseaddr
= BASEADDR (sec
);
671 bfd_vma symval
= BASEADDR (sym_sec
) + isym
->st_value
674 if ((baseaddr
+ addr
) <= symval
675 && symval
<= (baseaddr
+ endaddr
))
676 irel
->r_addend
+= count
;
678 /* Go hunt up a function and fix its line info if needed. */
679 stabp
= stabcontents
+ irel
->r_offset
- 8;
681 /* Go pullout the stab entry. */
682 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
683 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
685 name
= bfd_get_stab_name (type
);
687 if (strcmp (name
, "FUN") == 0)
689 int function_adjusted
= 0;
691 if (symval
> (baseaddr
+ addr
))
692 /* Not in this function. */
695 /* Hey we got a function hit. */
697 for (;stabp
< stabend
; stabp
+= STABSIZE
)
699 /* Go pullout the stab entry. */
700 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
701 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
703 name
= bfd_get_stab_name (type
);
705 if (strcmp (name
, "FUN") == 0)
707 /* Hit another function entry. */
708 if (function_adjusted
)
710 /* Adjust the value. */
713 /* We need to put it back. */
714 bfd_h_put_32 (abfd
, value
,stabp
+ VALOFF
);
717 /* And then bale out. */
721 if (strcmp (name
, "SLINE") == 0)
723 /* Got a line entry. */
724 if ((baseaddr
+ addr
) <= (symval
+ value
))
726 /* Adjust the line entry. */
729 /* We need to put it back. */
730 bfd_h_put_32 (abfd
, value
,stabp
+ VALOFF
);
731 function_adjusted
= 1;
742 /* When adding an instruction back it is sometimes necessary to move any
743 global or local symbol that was referencing the first instruction of
744 the moved block to refer to the first instruction of the inserted block.
746 For example adding a PAGE instruction before a CALL or JMP requires
747 that any label on the CALL or JMP is moved to the PAGE insn. */
750 /* Adjust the local symbols defined in this section. */
751 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
752 for (isym
= isymbuf
; isym
< isymend
; isym
++)
754 if (isym
->st_shndx
== shndx
755 && addr
<= isym
->st_value
756 && isym
->st_value
< endaddr
)
757 isym
->st_value
+= count
;
760 /* Now adjust the global symbols defined in this section. */
761 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
762 - symtab_hdr
->sh_info
);
763 sym_hashes
= elf_sym_hashes (abfd
);
764 end_hashes
= sym_hashes
+ symcount
;
765 for (; sym_hashes
< end_hashes
; sym_hashes
++)
767 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
769 if ((sym_hash
->root
.type
== bfd_link_hash_defined
770 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
771 && sym_hash
->root
.u
.def
.section
== sec
)
773 if (addr
<= sym_hash
->root
.u
.def
.value
774 && sym_hash
->root
.u
.def
.value
< endaddr
)
775 sym_hash
->root
.u
.def
.value
+= count
;
782 /* Delete some bytes from a section while relaxing. */
785 ip2k_elf_relax_delete_bytes (bfd
*abfd
,
790 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
791 bfd_vma endaddr
= sec
->size
;
793 /* Actually delete the bytes. */
794 memmove (contents
+ addr
, contents
+ addr
+ count
,
795 endaddr
- addr
- count
);
799 adjust_all_relocations (abfd
, sec
, addr
+ count
, endaddr
, -count
, 0);
804 ip2k_delete_page_insn (bfd
*abfd ATTRIBUTE_UNUSED
,
806 Elf_Internal_Rela
*irel
,
810 /* Note that we've changed the relocs, section contents, etc. */
811 elf_section_data (sec
)->relocs
= misc
->irelbase
;
812 elf_section_data (sec
)->this_hdr
.contents
= misc
->contents
;
813 misc
->symtab_hdr
->contents
= (bfd_byte
*) misc
->isymbuf
;
815 /* Fix the relocation's type. */
816 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
), R_IP2K_NONE
);
818 /* Delete the PAGE insn. */
819 if (!ip2k_elf_relax_delete_bytes (abfd
, sec
, irel
->r_offset
, 2))
822 /* Modified => will need to iterate relaxation again. */
829 ip2k_relax_switch_table_128 (bfd
*abfd ATTRIBUTE_UNUSED
,
831 Elf_Internal_Rela
*irel
,
835 Elf_Internal_Rela
*irelend
= misc
->irelbase
+ sec
->reloc_count
;
836 Elf_Internal_Rela
*ireltest
= irel
;
840 /* Test all page instructions. */
841 addr
= irel
->r_offset
;
844 if (addr
+ 4 > sec
->size
)
847 ip2k_get_mem (abfd
, misc
->contents
+ addr
, 4, code
);
848 if ((! IS_PAGE_OPCODE (code
+ 0))
849 || (! IS_JMP_OPCODE (code
+ 2)))
852 /* Validate relocation entry (every entry should have a matching
853 relocation entry). */
854 if (ireltest
>= irelend
)
856 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
860 if (ireltest
->r_offset
!= addr
)
862 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
866 if (! ip2k_test_page_insn (abfd
, sec
, ireltest
, misc
))
867 /* Un-removable page insn => nothing can be done. */
874 /* Relaxable. Adjust table header. */
875 ip2k_get_mem (abfd
, misc
->contents
+ irel
->r_offset
- 4, 4, code
);
876 if ((! IS_ADD_W_WREG_OPCODE (code
+ 0))
877 || (! IS_ADD_PCL_W_OPCODE (code
+ 2)))
879 _bfd_error_handler (_("ip2k relaxer: switch table header corrupt."));
883 if (!ip2k_elf_relax_delete_bytes (abfd
, sec
, irel
->r_offset
- 4, 2))
888 /* Delete all page instructions in table. */
889 while (irel
< ireltest
)
891 if (!ip2k_delete_page_insn (abfd
, sec
, irel
, again
, misc
))
900 ip2k_relax_switch_table_256 (bfd
*abfd ATTRIBUTE_UNUSED
,
902 Elf_Internal_Rela
*irel
,
906 Elf_Internal_Rela
*irelend
= misc
->irelbase
+ sec
->reloc_count
;
907 Elf_Internal_Rela
*ireltest
= irel
;
911 /* Test all page instructions. */
912 addr
= irel
->r_offset
;
916 if (addr
+ 4 > sec
->size
)
919 ip2k_get_mem (abfd
, misc
->contents
+ addr
, 4, code
);
921 if ((! IS_PAGE_OPCODE (code
+ 0))
922 || (! IS_JMP_OPCODE (code
+ 2)))
925 /* Validate relocation entry (every entry should have a matching
926 relocation entry). */
927 if (ireltest
>= irelend
)
929 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
933 if (ireltest
->r_offset
!= addr
)
935 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
939 if (!ip2k_test_page_insn (abfd
, sec
, ireltest
, misc
))
940 /* Un-removable page insn => nothing can be done. */
947 /* Relaxable. Adjust table header. */
948 ip2k_get_mem (abfd
, misc
->contents
+ irel
->r_offset
- 4, 2, code
);
949 if (IS_PAGE_OPCODE (code
))
950 addr
= irel
->r_offset
- 16;
952 addr
= irel
->r_offset
- 14;
954 ip2k_get_mem (abfd
, misc
->contents
+ addr
, 12, code
);
955 if ((!IS_ADD_W_WREG_OPCODE (code
+ 0))
956 || (!IS_SNC_OPCODE (code
+ 2))
957 || (!IS_INC_1SP_OPCODE (code
+ 4))
958 || (!IS_ADD_2SP_W_OPCODE (code
+ 6))
959 || (!IS_SNC_OPCODE (code
+ 8))
960 || (!IS_INC_1SP_OPCODE (code
+ 10)))
962 _bfd_error_handler (_("ip2k relaxer: switch table header corrupt."));
966 /* Delete first 3 opcodes. */
967 if (!ip2k_elf_relax_delete_bytes (abfd
, sec
, addr
+ 0, 6))
972 /* Delete all page instructions in table. */
973 while (irel
< ireltest
)
975 if (!ip2k_delete_page_insn (abfd
, sec
, irel
, again
, misc
))
983 /* This function handles relaxation of a section in a specific page. */
986 ip2k_elf_relax_section_page (bfd
*abfd
,
990 unsigned long page_start
,
991 unsigned long page_end
)
993 Elf_Internal_Rela
*irelend
= misc
->irelbase
+ sec
->reloc_count
;
994 Elf_Internal_Rela
*irel
;
995 int switch_table_128
;
996 int switch_table_256
;
998 /* Walk thru the section looking for relaxation opportunities. */
999 for (irel
= misc
->irelbase
; irel
< irelend
; irel
++)
1001 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_IP2K_PAGE3
)
1002 /* Ignore non page instructions. */
1005 if (BASEADDR (sec
) + irel
->r_offset
< page_start
)
1006 /* Ignore page instructions on earlier page - they have
1007 already been processed. Remember that there is code flow
1008 that crosses a page boundary. */
1011 if (BASEADDR (sec
) + irel
->r_offset
> page_end
)
1012 /* Flow beyond end of page => nothing more to do for this page. */
1015 /* Detect switch tables. */
1016 switch_table_128
= ip2k_is_switch_table_128 (abfd
, sec
, irel
->r_offset
, misc
->contents
);
1017 switch_table_256
= ip2k_is_switch_table_256 (abfd
, sec
, irel
->r_offset
, misc
->contents
);
1019 if ((switch_table_128
> 0) || (switch_table_256
> 0))
1020 /* If the index is greater than 0 then it has already been processed. */
1023 if (switch_table_128
== 0)
1025 if (!ip2k_relax_switch_table_128 (abfd
, sec
, irel
, again
, misc
))
1031 if (switch_table_256
== 0)
1033 if (!ip2k_relax_switch_table_256 (abfd
, sec
, irel
, again
, misc
))
1040 if (ip2k_test_page_insn (abfd
, sec
, irel
, misc
))
1042 if (!ip2k_delete_page_insn (abfd
, sec
, irel
, again
, misc
))
1052 /* This function handles relaxing for the ip2k.
1054 Principle: Start with the first page and remove page instructions that
1055 are not require on this first page. By removing page instructions more
1056 code will fit into this page - repeat until nothing more can be achieved
1057 for this page. Move on to the next page.
1059 Processing the pages one at a time from the lowest page allows a removal
1060 only policy to be used - pages can be removed but are never reinserted. */
1063 ip2k_elf_relax_section (bfd
*abfd
,
1065 struct bfd_link_info
*link_info
,
1068 Elf_Internal_Shdr
*symtab_hdr
;
1069 Elf_Internal_Rela
*internal_relocs
;
1070 bfd_byte
*contents
= NULL
;
1071 Elf_Internal_Sym
*isymbuf
= NULL
;
1072 static asection
* first_section
= NULL
;
1073 static unsigned long search_addr
;
1074 static unsigned long page_start
= 0;
1075 static unsigned long page_end
= 0;
1076 static unsigned int pass
= 0;
1077 static bfd_boolean new_pass
= FALSE
;
1078 static bfd_boolean changed
= FALSE
;
1081 /* Assume nothing changes. */
1084 if (first_section
== NULL
)
1086 ip2k_relaxed
= TRUE
;
1087 first_section
= sec
;
1090 if (first_section
== sec
)
1096 /* We don't have to do anything for a relocatable link,
1097 if this section does not have relocs, or if this is
1098 not a code section. */
1099 if (bfd_link_relocatable (link_info
)
1100 || (sec
->flags
& SEC_RELOC
) == 0
1101 || sec
->reloc_count
== 0
1102 || (sec
->flags
& SEC_CODE
) == 0)
1105 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1107 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
1108 link_info
->keep_memory
);
1109 if (internal_relocs
== NULL
)
1112 /* Get section contents cached copy if it exists. */
1113 if (contents
== NULL
)
1115 /* Get cached copy if it exists. */
1116 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1117 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1120 /* Go get them off disk. */
1121 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
1126 /* Read this BFD's symbols cached copy if it exists. */
1127 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
1129 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1130 if (isymbuf
== NULL
)
1131 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1132 symtab_hdr
->sh_info
, 0,
1134 if (isymbuf
== NULL
)
1138 misc
.symtab_hdr
= symtab_hdr
;
1139 misc
.isymbuf
= isymbuf
;
1140 misc
.irelbase
= internal_relocs
;
1141 misc
.contents
= contents
;
1143 /* This is where all the relaxation actually get done. */
1144 if ((pass
== 1) || (new_pass
&& !changed
))
1146 /* On the first pass we simply search for the lowest page that
1147 we havn't relaxed yet. Note that the pass count is reset
1148 each time a page is complete in order to move on to the next page.
1149 If we can't find any more pages then we are finished. */
1154 changed
= TRUE
; /* Pre-initialize to break out of pass 1. */
1155 search_addr
= 0xFFFFFFFF;
1158 if ((BASEADDR (sec
) + sec
->size
< search_addr
)
1159 && (BASEADDR (sec
) + sec
->size
> page_end
))
1161 if (BASEADDR (sec
) <= page_end
)
1162 search_addr
= page_end
+ 1;
1164 search_addr
= BASEADDR (sec
);
1166 /* Found a page => more work to do. */
1176 page_start
= PAGENO (search_addr
);
1177 page_end
= page_start
| 0x00003FFF;
1180 /* Only process sections in range. */
1181 if ((BASEADDR (sec
) + sec
->size
>= page_start
)
1182 && (BASEADDR (sec
) <= page_end
))
1184 if (!ip2k_elf_relax_section_page (abfd
, sec
, &changed
, &misc
, page_start
, page_end
))
1190 /* Perform some house keeping after relaxing the section. */
1193 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1195 if (! link_info
->keep_memory
)
1198 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1201 if (contents
!= NULL
1202 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1204 if (! link_info
->keep_memory
)
1208 /* Cache the section contents for elf_link_input_bfd. */
1209 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1213 if (internal_relocs
!= NULL
1214 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1215 free (internal_relocs
);
1221 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1223 if (contents
!= NULL
1224 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1226 if (internal_relocs
!= NULL
1227 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1228 free (internal_relocs
);
1232 /* Set the howto pointer for a IP2K ELF reloc. */
1235 ip2k_info_to_howto_rela (bfd
* abfd ATTRIBUTE_UNUSED
,
1236 arelent
* cache_ptr
,
1237 Elf_Internal_Rela
* dst
)
1239 unsigned int r_type
;
1241 r_type
= ELF32_R_TYPE (dst
->r_info
);
1242 if (r_type
>= (unsigned int) R_IP2K_max
)
1244 /* xgettext:c-format */
1245 _bfd_error_handler (_("%B: invalid IP2K reloc number: %d"), abfd
, r_type
);
1248 cache_ptr
->howto
= & ip2k_elf_howto_table
[r_type
];
1251 /* Perform a single relocation.
1252 By default we use the standard BFD routines. */
1254 static bfd_reloc_status_type
1255 ip2k_final_link_relocate (reloc_howto_type
* howto
,
1257 asection
* input_section
,
1258 bfd_byte
* contents
,
1259 Elf_Internal_Rela
* rel
,
1262 static bfd_vma page_addr
= 0;
1264 bfd_reloc_status_type r
= bfd_reloc_ok
;
1265 switch (howto
->type
)
1267 /* Handle data space relocations. */
1270 if ((relocation
& IP2K_DATA_MASK
) == IP2K_DATA_VALUE
)
1271 relocation
&= ~IP2K_DATA_MASK
;
1273 r
= bfd_reloc_notsupported
;
1276 case R_IP2K_LO8DATA
:
1277 case R_IP2K_HI8DATA
:
1278 case R_IP2K_EX8DATA
:
1281 /* Handle insn space relocations. */
1283 page_addr
= BASEADDR (input_section
) + rel
->r_offset
;
1284 if ((relocation
& IP2K_INSN_MASK
) == IP2K_INSN_VALUE
)
1285 relocation
&= ~IP2K_INSN_MASK
;
1287 r
= bfd_reloc_notsupported
;
1290 case R_IP2K_ADDR16CJP
:
1291 if (BASEADDR (input_section
) + rel
->r_offset
!= page_addr
+ 2)
1293 /* No preceding page instruction, verify that it isn't needed. */
1294 if (PAGENO (relocation
+ rel
->r_addend
) !=
1295 ip2k_nominal_page_bits (input_bfd
, input_section
,
1296 rel
->r_offset
, contents
))
1297 /* xgettext:c-format */
1298 _bfd_error_handler (_("ip2k linker: missing page instruction at %#Lx (dest = %#Lx)"),
1299 BASEADDR (input_section
) + rel
->r_offset
,
1300 relocation
+ rel
->r_addend
);
1302 else if (ip2k_relaxed
)
1304 /* Preceding page instruction. Verify that the page instruction is
1305 really needed. One reason for the relaxation to miss a page is if
1306 the section is not marked as executable. */
1307 if (!ip2k_is_switch_table_128 (input_bfd
, input_section
,
1308 rel
->r_offset
- 2, contents
)
1309 && !ip2k_is_switch_table_256 (input_bfd
, input_section
,
1310 rel
->r_offset
- 2, contents
)
1311 && (PAGENO (relocation
+ rel
->r_addend
) ==
1312 ip2k_nominal_page_bits (input_bfd
, input_section
,
1313 rel
->r_offset
- 2, contents
)))
1314 /* xgettext:c-format */
1315 _bfd_error_handler (_("ip2k linker: redundant page instruction at %#Lx (dest = %#Lx)"),
1317 relocation
+ rel
->r_addend
);
1319 if ((relocation
& IP2K_INSN_MASK
) == IP2K_INSN_VALUE
)
1320 relocation
&= ~IP2K_INSN_MASK
;
1322 r
= bfd_reloc_notsupported
;
1325 case R_IP2K_LO8INSN
:
1326 case R_IP2K_HI8INSN
:
1327 case R_IP2K_PC_SKIP
:
1328 if ((relocation
& IP2K_INSN_MASK
) == IP2K_INSN_VALUE
)
1329 relocation
&= ~IP2K_INSN_MASK
;
1331 r
= bfd_reloc_notsupported
;
1335 /* If this is a relocation involving a TEXT
1336 symbol, reduce it to a word address. */
1337 if ((relocation
& IP2K_INSN_MASK
) == IP2K_INSN_VALUE
)
1338 howto
= &ip2k_elf_howto_table
[ (int) R_IP2K_TEXT
];
1341 /* Pass others through. */
1346 /* Only install relocation if above tests did not disqualify it. */
1347 if (r
== bfd_reloc_ok
)
1348 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1349 contents
, rel
->r_offset
,
1350 relocation
, rel
->r_addend
);
1355 /* Relocate a IP2K ELF section.
1357 The RELOCATE_SECTION function is called by the new ELF backend linker
1358 to handle the relocations for a section.
1360 The relocs are always passed as Rela structures; if the section
1361 actually uses Rel structures, the r_addend field will always be
1364 This function is responsible for adjusting the section contents as
1365 necessary, and (if using Rela relocs and generating a relocatable
1366 output file) adjusting the reloc addend as necessary.
1368 This function does not have to worry about setting the reloc
1369 address or the reloc symbol index.
1371 LOCAL_SYMS is a pointer to the swapped in local symbols.
1373 LOCAL_SECTIONS is an array giving the section in the input file
1374 corresponding to the st_shndx field of each local symbol.
1376 The global hash table entry for the global symbols can be found
1377 via elf_sym_hashes (input_bfd).
1379 When generating relocatable output, this function must handle
1380 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1381 going to be the section symbol corresponding to the output
1382 section, which means that the addend must be adjusted
1386 ip2k_elf_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1387 struct bfd_link_info
*info
,
1389 asection
*input_section
,
1391 Elf_Internal_Rela
*relocs
,
1392 Elf_Internal_Sym
*local_syms
,
1393 asection
**local_sections
)
1395 Elf_Internal_Shdr
*symtab_hdr
;
1396 struct elf_link_hash_entry
**sym_hashes
;
1397 Elf_Internal_Rela
*rel
;
1398 Elf_Internal_Rela
*relend
;
1400 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
1401 sym_hashes
= elf_sym_hashes (input_bfd
);
1402 relend
= relocs
+ input_section
->reloc_count
;
1404 for (rel
= relocs
; rel
< relend
; rel
++)
1406 reloc_howto_type
* howto
;
1407 unsigned long r_symndx
;
1408 Elf_Internal_Sym
* sym
;
1410 struct elf_link_hash_entry
* h
;
1412 bfd_reloc_status_type r
;
1413 const char * name
= NULL
;
1416 r_type
= ELF32_R_TYPE (rel
->r_info
);
1417 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1418 howto
= ip2k_elf_howto_table
+ r_type
;
1423 if (r_symndx
< symtab_hdr
->sh_info
)
1425 sym
= local_syms
+ r_symndx
;
1426 sec
= local_sections
[r_symndx
];
1427 relocation
= BASEADDR (sec
) + sym
->st_value
;
1429 name
= bfd_elf_string_from_elf_section
1430 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
1431 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
1435 bfd_boolean warned
, ignored
;
1436 bfd_boolean unresolved_reloc
;
1438 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1439 r_symndx
, symtab_hdr
, sym_hashes
,
1441 unresolved_reloc
, warned
, ignored
);
1443 name
= h
->root
.root
.string
;
1446 if (sec
!= NULL
&& discarded_section (sec
))
1447 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1448 rel
, 1, relend
, howto
, 0, contents
);
1450 if (bfd_link_relocatable (info
))
1453 /* Finally, the sole IP2K-specific part. */
1454 r
= ip2k_final_link_relocate (howto
, input_bfd
, input_section
,
1455 contents
, rel
, relocation
);
1457 if (r
!= bfd_reloc_ok
)
1459 const char * msg
= NULL
;
1463 case bfd_reloc_overflow
:
1464 (*info
->callbacks
->reloc_overflow
)
1465 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
1466 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
1469 case bfd_reloc_undefined
:
1470 (*info
->callbacks
->undefined_symbol
)
1471 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
1474 case bfd_reloc_outofrange
:
1475 msg
= _("internal error: out of range error");
1478 /* This is how ip2k_final_link_relocate tells us of a non-kosher
1479 reference between insn & data address spaces. */
1480 case bfd_reloc_notsupported
:
1481 if (sym
!= NULL
) /* Only if it's not an unresolved symbol. */
1482 msg
= _("unsupported relocation between data/insn address spaces");
1485 case bfd_reloc_dangerous
:
1486 msg
= _("internal error: dangerous relocation");
1490 msg
= _("internal error: unknown error");
1495 (*info
->callbacks
->warning
) (info
, msg
, name
, input_bfd
,
1496 input_section
, rel
->r_offset
);
1503 #define TARGET_BIG_SYM ip2k_elf32_vec
1504 #define TARGET_BIG_NAME "elf32-ip2k"
1506 #define ELF_ARCH bfd_arch_ip2k
1507 #define ELF_MACHINE_CODE EM_IP2K
1508 #define ELF_MACHINE_ALT1 EM_IP2K_OLD
1509 #define ELF_MAXPAGESIZE 1 /* No pages on the IP2K. */
1511 #define elf_info_to_howto_rel NULL
1512 #define elf_info_to_howto ip2k_info_to_howto_rela
1514 #define elf_backend_can_gc_sections 1
1515 #define elf_backend_rela_normal 1
1516 #define elf_backend_relocate_section ip2k_elf_relocate_section
1518 #define elf_symbol_leading_char '_'
1519 #define bfd_elf32_bfd_reloc_type_lookup ip2k_reloc_type_lookup
1520 #define bfd_elf32_bfd_reloc_name_lookup ip2k_reloc_name_lookup
1521 #define bfd_elf32_bfd_relax_section ip2k_elf_relax_section
1523 #include "elf32-target.h"