* config/mt-gnu (CXXFLAGS): Include SYSROOT_CFLAGS_FOR_TARGET.
[binutils.git] / bfd / elf32-ip2k.c
blob135ef1314ba58948b099ab35ef6b08260fe68836
1 /* Ubicom IP2xxx specific support for 32-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/ip2k.h"
28 /* Struct used to pass miscellaneous paramaters which
29 helps to avoid overly long parameter lists. */
30 struct misc
32 Elf_Internal_Shdr * symtab_hdr;
33 Elf_Internal_Rela * irelbase;
34 bfd_byte * contents;
35 Elf_Internal_Sym * isymbuf;
38 struct ip2k_opcode
40 unsigned short opcode;
41 unsigned short mask;
44 static bfd_boolean ip2k_relaxed = FALSE;
46 static const struct ip2k_opcode ip2k_page_opcode[] =
48 {0x0010, 0xFFF8}, /* Page. */
49 {0x0000, 0x0000},
52 #define IS_PAGE_OPCODE(code) \
53 ip2k_is_opcode (code, ip2k_page_opcode)
55 static const struct ip2k_opcode ip2k_jmp_opcode[] =
57 {0xE000, 0xE000}, /* Jmp. */
58 {0x0000, 0x0000},
61 #define IS_JMP_OPCODE(code) \
62 ip2k_is_opcode (code, ip2k_jmp_opcode)
64 static const struct ip2k_opcode ip2k_snc_opcode[] =
66 {0xA00B, 0xFFFF}, /* Snc. */
67 {0x0000, 0x0000},
70 #define IS_SNC_OPCODE(code) \
71 ip2k_is_opcode (code, ip2k_snc_opcode)
73 static const struct ip2k_opcode ip2k_inc_1sp_opcode[] =
75 {0x2B81, 0xFFFF}, /* Inc 1(SP). */
76 {0x0000, 0x0000},
79 #define IS_INC_1SP_OPCODE(code) \
80 ip2k_is_opcode (code, ip2k_inc_1sp_opcode)
82 static const struct ip2k_opcode ip2k_add_2sp_w_opcode[] =
84 {0x1F82, 0xFFFF}, /* Add 2(SP),w. */
85 {0x0000, 0x0000},
88 #define IS_ADD_2SP_W_OPCODE(code) \
89 ip2k_is_opcode (code, ip2k_add_2sp_w_opcode)
91 static const struct ip2k_opcode ip2k_add_w_wreg_opcode[] =
93 {0x1C0A, 0xFFFF}, /* Add w,wreg. */
94 {0x1E0A, 0xFFFF}, /* Add wreg,w. */
95 {0x0000, 0x0000},
98 #define IS_ADD_W_WREG_OPCODE(code) \
99 ip2k_is_opcode (code, ip2k_add_w_wreg_opcode)
101 static const struct ip2k_opcode ip2k_add_pcl_w_opcode[] =
103 {0x1E09, 0xFFFF}, /* Add pcl,w. */
104 {0x0000, 0x0000},
107 #define IS_ADD_PCL_W_OPCODE(code) \
108 ip2k_is_opcode (code, ip2k_add_pcl_w_opcode)
110 static const struct ip2k_opcode ip2k_skip_opcodes[] =
112 {0xB000, 0xF000}, /* sb */
113 {0xA000, 0xF000}, /* snb */
114 {0x7600, 0xFE00}, /* cse/csne #lit */
115 {0x5800, 0xFC00}, /* incsnz */
116 {0x4C00, 0xFC00}, /* decsnz */
117 {0x4000, 0xFC00}, /* cse/csne */
118 {0x3C00, 0xFC00}, /* incsz */
119 {0x2C00, 0xFC00}, /* decsz */
120 {0x0000, 0x0000},
123 #define IS_SKIP_OPCODE(code) \
124 ip2k_is_opcode (code, ip2k_skip_opcodes)
126 /* Relocation tables. */
127 static reloc_howto_type ip2k_elf_howto_table [] =
129 #define IP2K_HOWTO(t,rs,s,bs,pr,bp,name,sm,dm) \
130 HOWTO(t, /* type */ \
131 rs, /* rightshift */ \
132 s, /* size (0 = byte, 1 = short, 2 = long) */ \
133 bs, /* bitsize */ \
134 pr, /* pc_relative */ \
135 bp, /* bitpos */ \
136 complain_overflow_dont,/* complain_on_overflow */ \
137 bfd_elf_generic_reloc,/* special_function */ \
138 name, /* name */ \
139 FALSE, /* partial_inplace */ \
140 sm, /* src_mask */ \
141 dm, /* dst_mask */ \
142 pr) /* pcrel_offset */
144 /* This reloc does nothing. */
145 IP2K_HOWTO (R_IP2K_NONE, 0,2,32, FALSE, 0, "R_IP2K_NONE", 0, 0),
146 /* A 16 bit absolute relocation. */
147 IP2K_HOWTO (R_IP2K_16, 0,1,16, FALSE, 0, "R_IP2K_16", 0, 0xffff),
148 /* A 32 bit absolute relocation. */
149 IP2K_HOWTO (R_IP2K_32, 0,2,32, FALSE, 0, "R_IP2K_32", 0, 0xffffffff),
150 /* A 8-bit data relocation for the FR9 field. Ninth bit is computed specially. */
151 IP2K_HOWTO (R_IP2K_FR9, 0,1,9, FALSE, 0, "R_IP2K_FR9", 0, 0x00ff),
152 /* A 4-bit data relocation. */
153 IP2K_HOWTO (R_IP2K_BANK, 8,1,4, FALSE, 0, "R_IP2K_BANK", 0, 0x000f),
154 /* A 13-bit insn relocation - word address => right-shift 1 bit extra. */
155 IP2K_HOWTO (R_IP2K_ADDR16CJP, 1,1,13, FALSE, 0, "R_IP2K_ADDR16CJP", 0, 0x1fff),
156 /* A 3-bit insn relocation - word address => right-shift 1 bit extra. */
157 IP2K_HOWTO (R_IP2K_PAGE3, 14,1,3, FALSE, 0, "R_IP2K_PAGE3", 0, 0x0007),
158 /* Two 8-bit data relocations. */
159 IP2K_HOWTO (R_IP2K_LO8DATA, 0,1,8, FALSE, 0, "R_IP2K_LO8DATA", 0, 0x00ff),
160 IP2K_HOWTO (R_IP2K_HI8DATA, 8,1,8, FALSE, 0, "R_IP2K_HI8DATA", 0, 0x00ff),
161 /* Two 8-bit insn relocations. word address => right-shift 1 bit extra. */
162 IP2K_HOWTO (R_IP2K_LO8INSN, 1,1,8, FALSE, 0, "R_IP2K_LO8INSN", 0, 0x00ff),
163 IP2K_HOWTO (R_IP2K_HI8INSN, 9,1,8, FALSE, 0, "R_IP2K_HI8INSN", 0, 0x00ff),
165 /* Special 1 bit relocation for SKIP instructions. */
166 IP2K_HOWTO (R_IP2K_PC_SKIP, 1,1,1, FALSE, 12, "R_IP2K_PC_SKIP", 0xfffe, 0x1000),
167 /* 16 bit word address. */
168 IP2K_HOWTO (R_IP2K_TEXT, 1,1,16, FALSE, 0, "R_IP2K_TEXT", 0, 0xffff),
169 /* A 7-bit offset relocation for the FR9 field. Eigth and ninth bit comes from insn. */
170 IP2K_HOWTO (R_IP2K_FR_OFFSET, 0,1,9, FALSE, 0, "R_IP2K_FR_OFFSET", 0x180, 0x007f),
171 /* Bits 23:16 of an address. */
172 IP2K_HOWTO (R_IP2K_EX8DATA, 16,1,8, FALSE, 0, "R_IP2K_EX8DATA", 0, 0x00ff),
176 /* Map BFD reloc types to IP2K ELF reloc types. */
178 static reloc_howto_type *
179 ip2k_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
180 bfd_reloc_code_real_type code)
182 /* Note that the ip2k_elf_howto_table is indxed by the R_
183 constants. Thus, the order that the howto records appear in the
184 table *must* match the order of the relocation types defined in
185 include/elf/ip2k.h. */
187 switch (code)
189 case BFD_RELOC_NONE:
190 return &ip2k_elf_howto_table[ (int) R_IP2K_NONE];
191 case BFD_RELOC_16:
192 return &ip2k_elf_howto_table[ (int) R_IP2K_16];
193 case BFD_RELOC_32:
194 return &ip2k_elf_howto_table[ (int) R_IP2K_32];
195 case BFD_RELOC_IP2K_FR9:
196 return &ip2k_elf_howto_table[ (int) R_IP2K_FR9];
197 case BFD_RELOC_IP2K_BANK:
198 return &ip2k_elf_howto_table[ (int) R_IP2K_BANK];
199 case BFD_RELOC_IP2K_ADDR16CJP:
200 return &ip2k_elf_howto_table[ (int) R_IP2K_ADDR16CJP];
201 case BFD_RELOC_IP2K_PAGE3:
202 return &ip2k_elf_howto_table[ (int) R_IP2K_PAGE3];
203 case BFD_RELOC_IP2K_LO8DATA:
204 return &ip2k_elf_howto_table[ (int) R_IP2K_LO8DATA];
205 case BFD_RELOC_IP2K_HI8DATA:
206 return &ip2k_elf_howto_table[ (int) R_IP2K_HI8DATA];
207 case BFD_RELOC_IP2K_LO8INSN:
208 return &ip2k_elf_howto_table[ (int) R_IP2K_LO8INSN];
209 case BFD_RELOC_IP2K_HI8INSN:
210 return &ip2k_elf_howto_table[ (int) R_IP2K_HI8INSN];
211 case BFD_RELOC_IP2K_PC_SKIP:
212 return &ip2k_elf_howto_table[ (int) R_IP2K_PC_SKIP];
213 case BFD_RELOC_IP2K_TEXT:
214 return &ip2k_elf_howto_table[ (int) R_IP2K_TEXT];
215 case BFD_RELOC_IP2K_FR_OFFSET:
216 return &ip2k_elf_howto_table[ (int) R_IP2K_FR_OFFSET];
217 case BFD_RELOC_IP2K_EX8DATA:
218 return &ip2k_elf_howto_table[ (int) R_IP2K_EX8DATA];
219 default:
220 /* Pacify gcc -Wall. */
221 return NULL;
223 return NULL;
226 static void
227 ip2k_get_mem (bfd *abfd ATTRIBUTE_UNUSED,
228 bfd_byte *addr,
229 int length,
230 bfd_byte *ptr)
232 while (length --)
233 * ptr ++ = bfd_get_8 (abfd, addr ++);
236 static bfd_boolean
237 ip2k_is_opcode (bfd_byte *code, const struct ip2k_opcode *opcodes)
239 unsigned short insn = (code[0] << 8) | code[1];
241 while (opcodes->mask != 0)
243 if ((insn & opcodes->mask) == opcodes->opcode)
244 return TRUE;
246 opcodes ++;
249 return FALSE;
252 #define PAGENO(ABSADDR) ((ABSADDR) & 0xFFFFC000)
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. */
259 static bfd_vma
260 symbol_value (bfd *abfd,
261 Elf_Internal_Shdr *symtab_hdr,
262 Elf_Internal_Sym *isymbuf,
263 Elf_Internal_Rela *irel)
265 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
267 Elf_Internal_Sym *isym;
268 asection *sym_sec;
270 isym = isymbuf + ELF32_R_SYM (irel->r_info);
271 if (isym->st_shndx == SHN_UNDEF)
272 sym_sec = bfd_und_section_ptr;
273 else if (isym->st_shndx == SHN_ABS)
274 sym_sec = bfd_abs_section_ptr;
275 else if (isym->st_shndx == SHN_COMMON)
276 sym_sec = bfd_com_section_ptr;
277 else
278 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
280 return isym->st_value + BASEADDR (sym_sec);
282 else
284 unsigned long indx;
285 struct elf_link_hash_entry *h;
287 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
288 h = elf_sym_hashes (abfd)[indx];
289 BFD_ASSERT (h != NULL);
291 if (h->root.type != bfd_link_hash_defined
292 && h->root.type != bfd_link_hash_defweak)
293 return UNDEFINED_SYMBOL;
295 return (h->root.u.def.value + BASEADDR (h->root.u.def.section));
299 /* Determine if the instruction sequence matches that for
300 the prologue of a switch dispatch table with fewer than
301 128 entries.
304 page $nnn0
305 jmp $nnn0
306 add w,wreg
307 add pcl,w
308 addr=>
309 page $nnn1
310 jmp $nnn1
311 page $nnn2
312 jmp $nnn2
314 page $nnnN
315 jmp $nnnN
317 After relaxation.
319 page $nnn0
320 jmp $nnn0
321 add pcl,w
322 addr=>
323 jmp $nnn1
324 jmp $nnn2
326 jmp $nnnN */
328 static int
329 ip2k_is_switch_table_128 (bfd *abfd ATTRIBUTE_UNUSED,
330 asection *sec,
331 bfd_vma addr,
332 bfd_byte *contents)
334 bfd_byte code[4];
335 int index = 0;
337 /* Check current page-jmp. */
338 if (addr + 4 > sec->size)
339 return -1;
341 ip2k_get_mem (abfd, contents + addr, 4, code);
343 if ((! IS_PAGE_OPCODE (code + 0))
344 || (! IS_JMP_OPCODE (code + 2)))
345 return -1;
347 /* Search back. */
348 while (1)
350 if (addr < 4)
351 return -1;
353 /* Check previous 2 instructions. */
354 ip2k_get_mem (abfd, contents + addr - 4, 4, code);
355 if ((IS_ADD_W_WREG_OPCODE (code + 0))
356 && (IS_ADD_PCL_W_OPCODE (code + 2)))
357 return index;
359 if ((! IS_PAGE_OPCODE (code + 0))
360 || (! IS_JMP_OPCODE (code + 2)))
361 return -1;
363 index++;
364 addr -= 4;
368 /* Determine if the instruction sequence matches that for
369 the prologue switch dispatch table with fewer than
370 256 entries but more than 127.
372 Before relaxation.
373 push %lo8insn(label) ; Push address of table
374 push %hi8insn(label)
375 add w,wreg ; index*2 => offset
376 snc ; CARRY SET?
377 inc 1(sp) ; Propagate MSB into table address
378 add 2(sp),w ; Add low bits of offset to table address
379 snc ; and handle any carry-out
380 inc 1(sp)
381 addr=>
382 page __indjmp ; Do an indirect jump to that location
383 jmp __indjmp
384 label: ; case dispatch table starts here
385 page $nnn1
386 jmp $nnn1
387 page $nnn2
388 jmp $nnn2
390 page $nnnN
391 jmp $nnnN
393 After relaxation.
394 push %lo8insn(label) ; Push address of table
395 push %hi8insn(label)
396 add 2(sp),w ; Add low bits of offset to table address
397 snc ; and handle any carry-out
398 inc 1(sp)
399 addr=>
400 page __indjmp ; Do an indirect jump to that location
401 jmp __indjmp
402 label: ; case dispatch table starts here
403 jmp $nnn1
404 jmp $nnn2
406 jmp $nnnN */
408 static int
409 ip2k_is_switch_table_256 (bfd *abfd ATTRIBUTE_UNUSED,
410 asection *sec,
411 bfd_vma addr,
412 bfd_byte *contents)
414 bfd_byte code[16];
415 int index = 0;
417 /* Check current page-jmp. */
418 if (addr + 4 > sec->size)
419 return -1;
421 ip2k_get_mem (abfd, contents + addr, 4, code);
422 if ((! IS_PAGE_OPCODE (code + 0))
423 || (! IS_JMP_OPCODE (code + 2)))
424 return -1;
426 /* Search back. */
427 while (1)
429 if (addr < 16)
430 return -1;
432 /* Check previous 8 instructions. */
433 ip2k_get_mem (abfd, contents + addr - 16, 16, code);
434 if ((IS_ADD_W_WREG_OPCODE (code + 0))
435 && (IS_SNC_OPCODE (code + 2))
436 && (IS_INC_1SP_OPCODE (code + 4))
437 && (IS_ADD_2SP_W_OPCODE (code + 6))
438 && (IS_SNC_OPCODE (code + 8))
439 && (IS_INC_1SP_OPCODE (code + 10))
440 && (IS_PAGE_OPCODE (code + 12))
441 && (IS_JMP_OPCODE (code + 14)))
442 return index;
444 if ((IS_ADD_W_WREG_OPCODE (code + 2))
445 && (IS_SNC_OPCODE (code + 4))
446 && (IS_INC_1SP_OPCODE (code + 6))
447 && (IS_ADD_2SP_W_OPCODE (code + 8))
448 && (IS_SNC_OPCODE (code + 10))
449 && (IS_INC_1SP_OPCODE (code + 12))
450 && (IS_JMP_OPCODE (code + 14)))
451 return index;
453 if ((! IS_PAGE_OPCODE (code + 0))
454 || (! IS_JMP_OPCODE (code + 2)))
455 return -1;
457 index++;
458 addr -= 4;
462 /* Returns the expected page state for the given instruction not including
463 the effect of page instructions. */
465 static bfd_vma
466 ip2k_nominal_page_bits (bfd *abfd ATTRIBUTE_UNUSED,
467 asection *sec,
468 bfd_vma addr,
469 bfd_byte *contents)
471 bfd_vma page = PAGENO (BASEADDR (sec) + addr);
473 /* Check if section flows into this page. If not then the page
474 bits are assumed to match the PC. This will be true unless
475 the user has a page instruction without a call/jump, in which
476 case they are on their own. */
477 if (PAGENO (BASEADDR (sec)) == page)
478 return page;
480 /* Section flows across page boundary. The page bits should match
481 the PC unless there is a possible flow from the previous page,
482 in which case it is not possible to determine the value of the
483 page bits. */
484 while (PAGENO (BASEADDR (sec) + addr - 2) == page)
486 bfd_byte code[2];
488 addr -= 2;
489 ip2k_get_mem (abfd, contents + addr, 2, code);
490 if (!IS_PAGE_OPCODE (code))
491 continue;
493 /* Found a page instruction, check if jump table. */
494 if (ip2k_is_switch_table_128 (abfd, sec, addr, contents) != -1)
495 /* Jump table => page is conditional. */
496 continue;
498 if (ip2k_is_switch_table_256 (abfd, sec, addr, contents) != -1)
499 /* Jump table => page is conditional. */
500 continue;
502 /* Found a page instruction, check if conditional. */
503 if (addr >= 2)
505 ip2k_get_mem (abfd, contents + addr - 2, 2, code);
506 if (IS_SKIP_OPCODE (code))
507 /* Page is conditional. */
508 continue;
511 /* Unconditional page instruction => page bits should be correct. */
512 return page;
515 /* Flow from previous page => page bits are impossible to determine. */
516 return 0;
519 static bfd_boolean
520 ip2k_test_page_insn (bfd *abfd ATTRIBUTE_UNUSED,
521 asection *sec,
522 Elf_Internal_Rela *irel,
523 struct misc *misc)
525 bfd_vma symval;
527 /* Get the value of the symbol referred to by the reloc. */
528 symval = symbol_value (abfd, misc->symtab_hdr, misc->isymbuf, irel);
529 if (symval == UNDEFINED_SYMBOL)
530 /* This appears to be a reference to an undefined
531 symbol. Just ignore it--it will be caught by the
532 regular reloc processing. */
533 return FALSE;
535 /* Test if we can delete this page instruction. */
536 if (PAGENO (symval + irel->r_addend) !=
537 ip2k_nominal_page_bits (abfd, sec, irel->r_offset, misc->contents))
538 return FALSE;
540 return TRUE;
543 /* Parts of a Stabs entry. */
545 #define STRDXOFF 0
546 #define TYPEOFF 4
547 #define OTHEROFF 5
548 #define DESCOFF 6
549 #define VALOFF 8
550 #define STABSIZE 12
552 /* Adjust all the relocations entries after adding or inserting instructions. */
554 static void
555 adjust_all_relocations (bfd *abfd,
556 asection *sec,
557 bfd_vma addr,
558 bfd_vma endaddr,
559 int count,
560 int noadj)
562 Elf_Internal_Shdr *symtab_hdr;
563 Elf_Internal_Sym *isymbuf, *isym, *isymend;
564 unsigned int shndx;
565 bfd_byte *contents;
566 Elf_Internal_Rela *irel, *irelend, *irelbase;
567 struct elf_link_hash_entry **sym_hashes;
568 struct elf_link_hash_entry **end_hashes;
569 unsigned int symcount;
570 asection *stab;
572 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
573 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
575 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
577 contents = elf_section_data (sec)->this_hdr.contents;
579 irelbase = elf_section_data (sec)->relocs;
580 irelend = irelbase + sec->reloc_count;
582 for (irel = irelbase; irel < irelend; irel++)
584 if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE)
586 /* Get the value of the symbol referred to by the reloc. */
587 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
589 asection *sym_sec;
591 /* A local symbol. */
592 isym = isymbuf + ELF32_R_SYM (irel->r_info);
593 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
595 if (isym->st_shndx == shndx)
597 bfd_vma baseaddr = BASEADDR (sec);
598 bfd_vma symval = BASEADDR (sym_sec) + isym->st_value
599 + irel->r_addend;
601 if ((baseaddr + addr + noadj) <= symval
602 && symval < (baseaddr + endaddr))
603 irel->r_addend += count;
608 /* Do this only for PC space relocations. */
609 if (addr <= irel->r_offset && irel->r_offset < endaddr)
610 irel->r_offset += count;
613 /* Now fix the stab relocations. */
614 stab = bfd_get_section_by_name (abfd, ".stab");
615 if (stab)
617 bfd_byte *stabcontents, *stabend, *stabp;
618 bfd_size_type stab_size = stab->rawsize ? stab->rawsize : stab->size;
620 irelbase = elf_section_data (stab)->relocs;
621 irelend = irelbase + stab->reloc_count;
623 /* Pull out the contents of the stab section. */
624 if (elf_section_data (stab)->this_hdr.contents != NULL)
625 stabcontents = elf_section_data (stab)->this_hdr.contents;
626 else
628 if (!bfd_malloc_and_get_section (abfd, stab, &stabcontents))
630 if (stabcontents != NULL)
631 free (stabcontents);
632 return;
635 /* We need to remember this. */
636 elf_section_data (stab)->this_hdr.contents = stabcontents;
639 stabend = stabcontents + stab_size;
641 for (irel = irelbase; irel < irelend; irel++)
643 if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE)
645 /* Get the value of the symbol referred to by the reloc. */
646 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
648 asection *sym_sec;
650 /* A local symbol. */
651 isym = isymbuf + ELF32_R_SYM (irel->r_info);
652 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
654 if (sym_sec == sec)
656 const char *name;
657 unsigned long strx;
658 unsigned char type, other;
659 unsigned short desc;
660 bfd_vma value;
661 bfd_vma baseaddr = BASEADDR (sec);
662 bfd_vma symval = BASEADDR (sym_sec) + isym->st_value
663 + irel->r_addend;
665 if ((baseaddr + addr) <= symval
666 && symval <= (baseaddr + endaddr))
667 irel->r_addend += count;
669 /* Go hunt up a function and fix its line info if needed. */
670 stabp = stabcontents + irel->r_offset - 8;
672 /* Go pullout the stab entry. */
673 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
674 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
675 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
676 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
677 value = bfd_h_get_32 (abfd, stabp + VALOFF);
679 name = bfd_get_stab_name (type);
681 if (strcmp (name, "FUN") == 0)
683 int function_adjusted = 0;
685 if (symval > (baseaddr + addr))
686 /* Not in this function. */
687 continue;
689 /* Hey we got a function hit. */
690 stabp += STABSIZE;
691 for (;stabp < stabend; stabp += STABSIZE)
693 /* Go pullout the stab entry. */
694 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
695 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
696 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
697 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
698 value = bfd_h_get_32 (abfd, stabp + VALOFF);
700 name = bfd_get_stab_name (type);
702 if (strcmp (name, "FUN") == 0)
704 /* Hit another function entry. */
705 if (function_adjusted)
707 /* Adjust the value. */
708 value += count;
710 /* We need to put it back. */
711 bfd_h_put_32 (abfd, value,stabp + VALOFF);
714 /* And then bale out. */
715 break;
718 if (strcmp (name, "SLINE") == 0)
720 /* Got a line entry. */
721 if ((baseaddr + addr) <= (symval + value))
723 /* Adjust the line entry. */
724 value += count;
726 /* We need to put it back. */
727 bfd_h_put_32 (abfd, value,stabp + VALOFF);
728 function_adjusted = 1;
739 /* When adding an instruction back it is sometimes necessary to move any
740 global or local symbol that was referencing the first instruction of
741 the moved block to refer to the first instruction of the inserted block.
743 For example adding a PAGE instruction before a CALL or JMP requires
744 that any label on the CALL or JMP is moved to the PAGE insn. */
745 addr += noadj;
747 /* Adjust the local symbols defined in this section. */
748 isymend = isymbuf + symtab_hdr->sh_info;
749 for (isym = isymbuf; isym < isymend; isym++)
751 if (isym->st_shndx == shndx
752 && addr <= isym->st_value
753 && isym->st_value < endaddr)
754 isym->st_value += count;
757 /* Now adjust the global symbols defined in this section. */
758 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
759 - symtab_hdr->sh_info);
760 sym_hashes = elf_sym_hashes (abfd);
761 end_hashes = sym_hashes + symcount;
762 for (; sym_hashes < end_hashes; sym_hashes++)
764 struct elf_link_hash_entry *sym_hash = *sym_hashes;
766 if ((sym_hash->root.type == bfd_link_hash_defined
767 || sym_hash->root.type == bfd_link_hash_defweak)
768 && sym_hash->root.u.def.section == sec)
770 if (addr <= sym_hash->root.u.def.value
771 && sym_hash->root.u.def.value < endaddr)
772 sym_hash->root.u.def.value += count;
776 return;
779 /* Delete some bytes from a section while relaxing. */
781 static bfd_boolean
782 ip2k_elf_relax_delete_bytes (bfd *abfd,
783 asection *sec,
784 bfd_vma addr,
785 int count)
787 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
788 bfd_vma endaddr = sec->size;
790 /* Actually delete the bytes. */
791 memmove (contents + addr, contents + addr + count,
792 endaddr - addr - count);
794 sec->size -= count;
796 adjust_all_relocations (abfd, sec, addr + count, endaddr, -count, 0);
797 return TRUE;
800 static bfd_boolean
801 ip2k_delete_page_insn (bfd *abfd ATTRIBUTE_UNUSED,
802 asection *sec,
803 Elf_Internal_Rela *irel,
804 bfd_boolean *again,
805 struct misc *misc)
807 /* Note that we've changed the relocs, section contents, etc. */
808 elf_section_data (sec)->relocs = misc->irelbase;
809 elf_section_data (sec)->this_hdr.contents = misc->contents;
810 misc->symtab_hdr->contents = (bfd_byte *) misc->isymbuf;
812 /* Fix the relocation's type. */
813 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_IP2K_NONE);
815 /* Delete the PAGE insn. */
816 if (!ip2k_elf_relax_delete_bytes (abfd, sec, irel->r_offset, 2))
817 return FALSE;
819 /* Modified => will need to iterate relaxation again. */
820 *again = TRUE;
822 return TRUE;
825 static bfd_boolean
826 ip2k_relax_switch_table_128 (bfd *abfd ATTRIBUTE_UNUSED,
827 asection *sec,
828 Elf_Internal_Rela *irel,
829 bfd_boolean *again,
830 struct misc *misc)
832 Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
833 Elf_Internal_Rela *ireltest = irel;
834 bfd_byte code[4];
835 bfd_vma addr;
837 /* Test all page instructions. */
838 addr = irel->r_offset;
839 while (1)
841 if (addr + 4 > sec->size)
842 break;
844 ip2k_get_mem (abfd, misc->contents + addr, 4, code);
845 if ((! IS_PAGE_OPCODE (code + 0))
846 || (! IS_JMP_OPCODE (code + 2)))
847 break;
849 /* Validate relocation entry (every entry should have a matching
850 relocation entry). */
851 if (ireltest >= irelend)
853 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
854 return FALSE;
857 if (ireltest->r_offset != addr)
859 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
860 return FALSE;
863 if (! ip2k_test_page_insn (abfd, sec, ireltest, misc))
864 /* Un-removable page insn => nothing can be done. */
865 return TRUE;
867 addr += 4;
868 ireltest += 2;
871 /* Relaxable. Adjust table header. */
872 ip2k_get_mem (abfd, misc->contents + irel->r_offset - 4, 4, code);
873 if ((! IS_ADD_W_WREG_OPCODE (code + 0))
874 || (! IS_ADD_PCL_W_OPCODE (code + 2)))
876 _bfd_error_handler (_("ip2k relaxer: switch table header corrupt."));
877 return FALSE;
880 if (!ip2k_elf_relax_delete_bytes (abfd, sec, irel->r_offset - 4, 2))
881 return FALSE;
883 *again = TRUE;
885 /* Delete all page instructions in table. */
886 while (irel < ireltest)
888 if (!ip2k_delete_page_insn (abfd, sec, irel, again, misc))
889 return FALSE;
890 irel += 2;
893 return TRUE;
896 static bfd_boolean
897 ip2k_relax_switch_table_256 (bfd *abfd ATTRIBUTE_UNUSED,
898 asection *sec,
899 Elf_Internal_Rela *irel,
900 bfd_boolean *again,
901 struct misc *misc)
903 Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
904 Elf_Internal_Rela *ireltest = irel;
905 bfd_byte code[12];
906 bfd_vma addr;
908 /* Test all page instructions. */
909 addr = irel->r_offset;
911 while (1)
913 if (addr + 4 > sec->size)
914 break;
916 ip2k_get_mem (abfd, misc->contents + addr, 4, code);
918 if ((! IS_PAGE_OPCODE (code + 0))
919 || (! IS_JMP_OPCODE (code + 2)))
920 break;
922 /* Validate relocation entry (every entry should have a matching
923 relocation entry). */
924 if (ireltest >= irelend)
926 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
927 return FALSE;
930 if (ireltest->r_offset != addr)
932 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
933 return FALSE;
936 if (!ip2k_test_page_insn (abfd, sec, ireltest, misc))
937 /* Un-removable page insn => nothing can be done. */
938 return TRUE;
940 addr += 4;
941 ireltest += 2;
944 /* Relaxable. Adjust table header. */
945 ip2k_get_mem (abfd, misc->contents + irel->r_offset - 4, 2, code);
946 if (IS_PAGE_OPCODE (code))
947 addr = irel->r_offset - 16;
948 else
949 addr = irel->r_offset - 14;
951 ip2k_get_mem (abfd, misc->contents + addr, 12, code);
952 if ((!IS_ADD_W_WREG_OPCODE (code + 0))
953 || (!IS_SNC_OPCODE (code + 2))
954 || (!IS_INC_1SP_OPCODE (code + 4))
955 || (!IS_ADD_2SP_W_OPCODE (code + 6))
956 || (!IS_SNC_OPCODE (code + 8))
957 || (!IS_INC_1SP_OPCODE (code + 10)))
959 _bfd_error_handler (_("ip2k relaxer: switch table header corrupt."));
960 return FALSE;
963 /* Delete first 3 opcodes. */
964 if (!ip2k_elf_relax_delete_bytes (abfd, sec, addr + 0, 6))
965 return FALSE;
967 *again = TRUE;
969 /* Delete all page instructions in table. */
970 while (irel < ireltest)
972 if (!ip2k_delete_page_insn (abfd, sec, irel, again, misc))
973 return FALSE;
974 irel += 2;
977 return TRUE;
980 /* This function handles relaxation of a section in a specific page. */
982 static bfd_boolean
983 ip2k_elf_relax_section_page (bfd *abfd,
984 asection *sec,
985 bfd_boolean *again,
986 struct misc *misc,
987 unsigned long page_start,
988 unsigned long page_end)
990 Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
991 Elf_Internal_Rela *irel;
992 int switch_table_128;
993 int switch_table_256;
995 /* Walk thru the section looking for relaxation opportunities. */
996 for (irel = misc->irelbase; irel < irelend; irel++)
998 if (ELF32_R_TYPE (irel->r_info) != (int) R_IP2K_PAGE3)
999 /* Ignore non page instructions. */
1000 continue;
1002 if (BASEADDR (sec) + irel->r_offset < page_start)
1003 /* Ignore page instructions on earlier page - they have
1004 already been processed. Remember that there is code flow
1005 that crosses a page boundary. */
1006 continue;
1008 if (BASEADDR (sec) + irel->r_offset > page_end)
1009 /* Flow beyond end of page => nothing more to do for this page. */
1010 return TRUE;
1012 /* Detect switch tables. */
1013 switch_table_128 = ip2k_is_switch_table_128 (abfd, sec, irel->r_offset, misc->contents);
1014 switch_table_256 = ip2k_is_switch_table_256 (abfd, sec, irel->r_offset, misc->contents);
1016 if ((switch_table_128 > 0) || (switch_table_256 > 0))
1017 /* If the index is greater than 0 then it has already been processed. */
1018 continue;
1020 if (switch_table_128 == 0)
1022 if (!ip2k_relax_switch_table_128 (abfd, sec, irel, again, misc))
1023 return FALSE;
1025 continue;
1028 if (switch_table_256 == 0)
1030 if (!ip2k_relax_switch_table_256 (abfd, sec, irel, again, misc))
1031 return FALSE;
1033 continue;
1036 /* Simple relax. */
1037 if (ip2k_test_page_insn (abfd, sec, irel, misc))
1039 if (!ip2k_delete_page_insn (abfd, sec, irel, again, misc))
1040 return FALSE;
1042 continue;
1046 return TRUE;
1049 /* This function handles relaxing for the ip2k.
1051 Principle: Start with the first page and remove page instructions that
1052 are not require on this first page. By removing page instructions more
1053 code will fit into this page - repeat until nothing more can be achieved
1054 for this page. Move on to the next page.
1056 Processing the pages one at a time from the lowest page allows a removal
1057 only policy to be used - pages can be removed but are never reinserted. */
1059 static bfd_boolean
1060 ip2k_elf_relax_section (bfd *abfd,
1061 asection *sec,
1062 struct bfd_link_info *link_info,
1063 bfd_boolean *again)
1065 Elf_Internal_Shdr *symtab_hdr;
1066 Elf_Internal_Rela *internal_relocs;
1067 bfd_byte *contents = NULL;
1068 Elf_Internal_Sym *isymbuf = NULL;
1069 static asection * first_section = NULL;
1070 static unsigned long search_addr;
1071 static unsigned long page_start = 0;
1072 static unsigned long page_end = 0;
1073 static unsigned int pass = 0;
1074 static bfd_boolean new_pass = FALSE;
1075 static bfd_boolean changed = FALSE;
1076 struct misc misc;
1077 asection *stab;
1079 /* Assume nothing changes. */
1080 *again = FALSE;
1082 if (first_section == NULL)
1084 ip2k_relaxed = TRUE;
1085 first_section = sec;
1088 if (first_section == sec)
1090 pass++;
1091 new_pass = TRUE;
1094 /* We don't have to do anything for a relocatable link,
1095 if this section does not have relocs, or if this is
1096 not a code section. */
1097 if (link_info->relocatable
1098 || (sec->flags & SEC_RELOC) == 0
1099 || sec->reloc_count == 0
1100 || (sec->flags & SEC_CODE) == 0)
1101 return TRUE;
1103 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1105 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
1106 link_info->keep_memory);
1107 if (internal_relocs == NULL)
1108 goto error_return;
1110 /* Make sure the stac.rela stuff gets read in. */
1111 stab = bfd_get_section_by_name (abfd, ".stab");
1113 if (stab)
1115 /* So stab does exits. */
1116 Elf_Internal_Rela * irelbase;
1118 irelbase = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
1119 link_info->keep_memory);
1122 /* Get section contents cached copy if it exists. */
1123 if (contents == NULL)
1125 /* Get cached copy if it exists. */
1126 if (elf_section_data (sec)->this_hdr.contents != NULL)
1127 contents = elf_section_data (sec)->this_hdr.contents;
1128 else
1130 /* Go get them off disk. */
1131 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1132 goto error_return;
1136 /* Read this BFD's symbols cached copy if it exists. */
1137 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1139 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1140 if (isymbuf == NULL)
1141 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1142 symtab_hdr->sh_info, 0,
1143 NULL, NULL, NULL);
1144 if (isymbuf == NULL)
1145 goto error_return;
1148 misc.symtab_hdr = symtab_hdr;
1149 misc.isymbuf = isymbuf;
1150 misc.irelbase = internal_relocs;
1151 misc.contents = contents;
1153 /* This is where all the relaxation actually get done. */
1154 if ((pass == 1) || (new_pass && !changed))
1156 /* On the first pass we simply search for the lowest page that
1157 we havn't relaxed yet. Note that the pass count is reset
1158 each time a page is complete in order to move on to the next page.
1159 If we can't find any more pages then we are finished. */
1160 if (new_pass)
1162 pass = 1;
1163 new_pass = FALSE;
1164 changed = TRUE; /* Pre-initialize to break out of pass 1. */
1165 search_addr = 0xFFFFFFFF;
1168 if ((BASEADDR (sec) + sec->size < search_addr)
1169 && (BASEADDR (sec) + sec->size > page_end))
1171 if (BASEADDR (sec) <= page_end)
1172 search_addr = page_end + 1;
1173 else
1174 search_addr = BASEADDR (sec);
1176 /* Found a page => more work to do. */
1177 *again = TRUE;
1180 else
1182 if (new_pass)
1184 new_pass = FALSE;
1185 changed = FALSE;
1186 page_start = PAGENO (search_addr);
1187 page_end = page_start | 0x00003FFF;
1190 /* Only process sections in range. */
1191 if ((BASEADDR (sec) + sec->size >= page_start)
1192 && (BASEADDR (sec) <= page_end))
1194 if (!ip2k_elf_relax_section_page (abfd, sec, &changed, &misc, page_start, page_end))
1195 return FALSE;
1197 *again = TRUE;
1200 /* Perform some house keeping after relaxing the section. */
1202 if (isymbuf != NULL
1203 && symtab_hdr->contents != (unsigned char *) isymbuf)
1205 if (! link_info->keep_memory)
1206 free (isymbuf);
1207 else
1208 symtab_hdr->contents = (unsigned char *) isymbuf;
1211 if (contents != NULL
1212 && elf_section_data (sec)->this_hdr.contents != contents)
1214 if (! link_info->keep_memory)
1215 free (contents);
1216 else
1218 /* Cache the section contents for elf_link_input_bfd. */
1219 elf_section_data (sec)->this_hdr.contents = contents;
1223 if (internal_relocs != NULL
1224 && elf_section_data (sec)->relocs != internal_relocs)
1225 free (internal_relocs);
1227 return TRUE;
1229 error_return:
1230 if (isymbuf != NULL
1231 && symtab_hdr->contents != (unsigned char *) isymbuf)
1232 free (isymbuf);
1233 if (contents != NULL
1234 && elf_section_data (sec)->this_hdr.contents != contents)
1235 free (contents);
1236 if (internal_relocs != NULL
1237 && elf_section_data (sec)->relocs != internal_relocs)
1238 free (internal_relocs);
1239 return FALSE;
1242 /* Set the howto pointer for a IP2K ELF reloc. */
1244 static void
1245 ip2k_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
1246 arelent * cache_ptr,
1247 Elf_Internal_Rela * dst)
1249 unsigned int r_type;
1251 r_type = ELF32_R_TYPE (dst->r_info);
1252 cache_ptr->howto = & ip2k_elf_howto_table [r_type];
1255 /* Perform a single relocation.
1256 By default we use the standard BFD routines. */
1258 static bfd_reloc_status_type
1259 ip2k_final_link_relocate (reloc_howto_type * howto,
1260 bfd * input_bfd,
1261 asection * input_section,
1262 bfd_byte * contents,
1263 Elf_Internal_Rela * rel,
1264 bfd_vma relocation)
1266 static bfd_vma page_addr = 0;
1268 bfd_reloc_status_type r = bfd_reloc_ok;
1269 switch (howto->type)
1271 /* Handle data space relocations. */
1272 case R_IP2K_FR9:
1273 case R_IP2K_BANK:
1274 if ((relocation & IP2K_DATA_MASK) == IP2K_DATA_VALUE)
1275 relocation &= ~IP2K_DATA_MASK;
1276 else
1277 r = bfd_reloc_notsupported;
1278 break;
1280 case R_IP2K_LO8DATA:
1281 case R_IP2K_HI8DATA:
1282 case R_IP2K_EX8DATA:
1283 break;
1285 /* Handle insn space relocations. */
1286 case R_IP2K_PAGE3:
1287 page_addr = BASEADDR (input_section) + rel->r_offset;
1288 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1289 relocation &= ~IP2K_INSN_MASK;
1290 else
1291 r = bfd_reloc_notsupported;
1292 break;
1294 case R_IP2K_ADDR16CJP:
1295 if (BASEADDR (input_section) + rel->r_offset != page_addr + 2)
1297 /* No preceding page instruction, verify that it isn't needed. */
1298 if (PAGENO (relocation + rel->r_addend) !=
1299 ip2k_nominal_page_bits (input_bfd, input_section,
1300 rel->r_offset, contents))
1301 _bfd_error_handler (_("ip2k linker: missing page instruction at 0x%08lx (dest = 0x%08lx)."),
1302 BASEADDR (input_section) + rel->r_offset,
1303 relocation + rel->r_addend);
1305 else if (ip2k_relaxed)
1307 /* Preceding page instruction. Verify that the page instruction is
1308 really needed. One reason for the relaxation to miss a page is if
1309 the section is not marked as executable. */
1310 if (!ip2k_is_switch_table_128 (input_bfd, input_section,
1311 rel->r_offset - 2, contents)
1312 && !ip2k_is_switch_table_256 (input_bfd, input_section,
1313 rel->r_offset - 2, contents)
1314 && (PAGENO (relocation + rel->r_addend) ==
1315 ip2k_nominal_page_bits (input_bfd, input_section,
1316 rel->r_offset - 2, contents)))
1317 _bfd_error_handler (_("ip2k linker: redundant page instruction at 0x%08lx (dest = 0x%08lx)."),
1318 page_addr,
1319 relocation + rel->r_addend);
1321 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1322 relocation &= ~IP2K_INSN_MASK;
1323 else
1324 r = bfd_reloc_notsupported;
1325 break;
1327 case R_IP2K_LO8INSN:
1328 case R_IP2K_HI8INSN:
1329 case R_IP2K_PC_SKIP:
1330 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1331 relocation &= ~IP2K_INSN_MASK;
1332 else
1333 r = bfd_reloc_notsupported;
1334 break;
1336 case R_IP2K_16:
1337 /* If this is a relocation involving a TEXT
1338 symbol, reduce it to a word address. */
1339 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1340 howto = &ip2k_elf_howto_table[ (int) R_IP2K_TEXT];
1341 break;
1343 /* Pass others through. */
1344 default:
1345 break;
1348 /* Only install relocation if above tests did not disqualify it. */
1349 if (r == bfd_reloc_ok)
1350 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1351 contents, rel->r_offset,
1352 relocation, rel->r_addend);
1354 return r;
1357 /* Relocate a IP2K ELF section.
1359 The RELOCATE_SECTION function is called by the new ELF backend linker
1360 to handle the relocations for a section.
1362 The relocs are always passed as Rela structures; if the section
1363 actually uses Rel structures, the r_addend field will always be
1364 zero.
1366 This function is responsible for adjusting the section contents as
1367 necessary, and (if using Rela relocs and generating a relocatable
1368 output file) adjusting the reloc addend as necessary.
1370 This function does not have to worry about setting the reloc
1371 address or the reloc symbol index.
1373 LOCAL_SYMS is a pointer to the swapped in local symbols.
1375 LOCAL_SECTIONS is an array giving the section in the input file
1376 corresponding to the st_shndx field of each local symbol.
1378 The global hash table entry for the global symbols can be found
1379 via elf_sym_hashes (input_bfd).
1381 When generating relocatable output, this function must handle
1382 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1383 going to be the section symbol corresponding to the output
1384 section, which means that the addend must be adjusted
1385 accordingly. */
1387 static bfd_boolean
1388 ip2k_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1389 struct bfd_link_info *info,
1390 bfd *input_bfd,
1391 asection *input_section,
1392 bfd_byte *contents,
1393 Elf_Internal_Rela *relocs,
1394 Elf_Internal_Sym *local_syms,
1395 asection **local_sections)
1397 Elf_Internal_Shdr *symtab_hdr;
1398 struct elf_link_hash_entry **sym_hashes;
1399 Elf_Internal_Rela *rel;
1400 Elf_Internal_Rela *relend;
1402 if (info->relocatable)
1403 return TRUE;
1405 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1406 sym_hashes = elf_sym_hashes (input_bfd);
1407 relend = relocs + input_section->reloc_count;
1409 for (rel = relocs; rel < relend; rel ++)
1411 reloc_howto_type * howto;
1412 unsigned long r_symndx;
1413 Elf_Internal_Sym * sym;
1414 asection * sec;
1415 struct elf_link_hash_entry * h;
1416 bfd_vma relocation;
1417 bfd_reloc_status_type r;
1418 const char * name = NULL;
1419 int r_type;
1421 /* This is a final link. */
1422 r_type = ELF32_R_TYPE (rel->r_info);
1423 r_symndx = ELF32_R_SYM (rel->r_info);
1424 howto = ip2k_elf_howto_table + ELF32_R_TYPE (rel->r_info);
1425 h = NULL;
1426 sym = NULL;
1427 sec = NULL;
1429 if (r_symndx < symtab_hdr->sh_info)
1431 sym = local_syms + r_symndx;
1432 sec = local_sections [r_symndx];
1433 relocation = BASEADDR (sec) + sym->st_value;
1435 name = bfd_elf_string_from_elf_section
1436 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1437 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1439 else
1441 bfd_boolean warned;
1442 bfd_boolean unresolved_reloc;
1444 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1445 r_symndx, symtab_hdr, sym_hashes,
1446 h, sec, relocation,
1447 unresolved_reloc, warned);
1449 name = h->root.root.string;
1452 /* Finally, the sole IP2K-specific part. */
1453 r = ip2k_final_link_relocate (howto, input_bfd, input_section,
1454 contents, rel, relocation);
1456 if (r != bfd_reloc_ok)
1458 const char * msg = NULL;
1460 switch (r)
1462 case bfd_reloc_overflow:
1463 r = info->callbacks->reloc_overflow
1464 (info, (h ? &h->root : NULL), name, howto->name,
1465 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1466 break;
1468 case bfd_reloc_undefined:
1469 r = info->callbacks->undefined_symbol
1470 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1471 break;
1473 case bfd_reloc_outofrange:
1474 msg = _("internal error: out of range error");
1475 break;
1477 /* This is how ip2k_final_link_relocate tells us of a non-kosher
1478 reference between insn & data address spaces. */
1479 case bfd_reloc_notsupported:
1480 if (sym != NULL) /* Only if it's not an unresolved symbol. */
1481 msg = _("unsupported relocation between data/insn address spaces");
1482 break;
1484 case bfd_reloc_dangerous:
1485 msg = _("internal error: dangerous relocation");
1486 break;
1488 default:
1489 msg = _("internal error: unknown error");
1490 break;
1493 if (msg)
1494 r = info->callbacks->warning
1495 (info, msg, name, input_bfd, input_section, rel->r_offset);
1497 if (! r)
1498 return FALSE;
1502 return TRUE;
1505 static asection *
1506 ip2k_elf_gc_mark_hook (asection *sec,
1507 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1508 Elf_Internal_Rela *rel,
1509 struct elf_link_hash_entry *h,
1510 Elf_Internal_Sym *sym)
1512 if (h != NULL)
1514 switch (ELF32_R_TYPE (rel->r_info))
1516 default:
1517 switch (h->root.type)
1519 case bfd_link_hash_defined:
1520 case bfd_link_hash_defweak:
1521 return h->root.u.def.section;
1523 case bfd_link_hash_common:
1524 return h->root.u.c.p->section;
1526 default:
1527 break;
1531 else
1533 if (!(elf_bad_symtab (sec->owner)
1534 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1535 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1536 && sym->st_shndx != SHN_COMMON))
1537 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1539 return NULL;
1542 static bfd_boolean
1543 ip2k_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
1544 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1545 asection *sec ATTRIBUTE_UNUSED,
1546 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
1548 /* We don't use got and plt entries for ip2k. */
1549 return TRUE;
1552 #define TARGET_BIG_SYM bfd_elf32_ip2k_vec
1553 #define TARGET_BIG_NAME "elf32-ip2k"
1555 #define ELF_ARCH bfd_arch_ip2k
1556 #define ELF_MACHINE_CODE EM_IP2K
1557 #define ELF_MACHINE_ALT1 EM_IP2K_OLD
1558 #define ELF_MAXPAGESIZE 1 /* No pages on the IP2K. */
1560 #define elf_info_to_howto_rel NULL
1561 #define elf_info_to_howto ip2k_info_to_howto_rela
1563 #define elf_backend_can_gc_sections 1
1564 #define elf_backend_rela_normal 1
1565 #define elf_backend_gc_mark_hook ip2k_elf_gc_mark_hook
1566 #define elf_backend_gc_sweep_hook ip2k_elf_gc_sweep_hook
1567 #define elf_backend_relocate_section ip2k_elf_relocate_section
1569 #define elf_symbol_leading_char '_'
1570 #define bfd_elf32_bfd_reloc_type_lookup ip2k_reloc_type_lookup
1571 #define bfd_elf32_bfd_relax_section ip2k_elf_relax_section
1573 #include "elf32-target.h"