* config/tc-mn10200.c (md_parse_option <c, arg>): Add ATTRIBUTE_UNUSED.
[binutils.git] / bfd / elf32-ip2k.c
blobdd2bb97f61ff46f795cc10310708754fde6224b9
1 /* Ubicom 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. */
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #include "elf-bfd.h"
24 #include "elf/ip2k.h"
26 /* Struct used to pass miscellaneous paramaters which
27 helps to avoid overly long parameter lists. */
28 struct misc
30 Elf_Internal_Shdr * symtab_hdr;
31 Elf_Internal_Rela * irelbase;
32 bfd_byte * contents;
33 Elf_Internal_Sym * isymbuf;
36 /* Prototypes. */
37 static reloc_howto_type *ip2k_reloc_type_lookup
38 PARAMS ((bfd *, bfd_reloc_code_real_type));
39 static void ip2k_info_to_howto_rela
40 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
41 static asection * ip2k_elf_gc_mark_hook
42 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
43 struct elf_link_hash_entry *, Elf_Internal_Sym *));
44 static bfd_boolean ip2k_elf_gc_sweep_hook
45 PARAMS ((bfd *, struct bfd_link_info *, asection *,
46 const Elf_Internal_Rela *));
47 static bfd_vma symbol_value
48 PARAMS ((bfd *, Elf_Internal_Shdr *, Elf_Internal_Sym *,
49 Elf_Internal_Rela *));
50 static void adjust_all_relocations
51 PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, int, int));
52 static bfd_boolean ip2k_elf_relax_delete_bytes
53 PARAMS ((bfd *, asection *, bfd_vma, int));
54 static bfd_boolean ip2k_elf_relax_add_bytes
55 PARAMS ((bfd *, asection *, bfd_vma, const bfd_byte *, int, int));
56 static bfd_boolean add_page_insn
57 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, struct misc *));
58 static bfd_boolean ip2k_elf_relax_section
59 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
60 static bfd_boolean relax_switch_dispatch_tables_pass1
61 PARAMS ((bfd *, asection *, bfd_vma, struct misc *));
62 static bfd_boolean unrelax_dispatch_table_entries
63 PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, bfd_boolean *, struct misc *));
64 static bfd_boolean unrelax_switch_dispatch_tables_passN
65 PARAMS ((bfd *, asection *, bfd_vma, bfd_boolean *, struct misc *));
66 static bfd_boolean is_switch_128_dispatch_table_p
67 PARAMS ((bfd *, bfd_vma, bfd_boolean, struct misc *));
68 static bfd_boolean is_switch_256_dispatch_table_p
69 PARAMS ((bfd *, bfd_vma, bfd_boolean, struct misc *));
70 static bfd_boolean ip2k_elf_relax_section_pass1
71 PARAMS ((bfd *, asection *, bfd_boolean *, struct misc *));
72 static bfd_boolean ip2k_elf_relax_section_passN
73 PARAMS ((bfd *, asection *, bfd_boolean *, bfd_boolean *, struct misc *));
74 static bfd_reloc_status_type ip2k_final_link_relocate
75 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
76 Elf_Internal_Rela *, bfd_vma));
77 static bfd_boolean ip2k_elf_relocate_section
78 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
79 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
81 #define IS_OPCODE(CODE0,CODE1,OPCODE) \
82 ((CODE0) == (OPCODE)[0] && (CODE1) == (OPCODE)[1])
84 #define PAGE_INSN_0 0x00
85 #define PAGE_INSN_1 0x10
87 static const bfd_byte page_opcode[] =
89 PAGE_INSN_0, PAGE_INSN_1
92 #define IS_PAGE_OPCODE(CODE0,CODE1) \
93 IS_OPCODE (CODE0, CODE1, page_opcode)
95 #define JMP_INSN_0 0xE0
96 #define JMP_INSN_1 0x00
98 static const bfd_byte jmp_opcode[] =
100 JMP_INSN_0, JMP_INSN_1
103 #define IS_JMP_OPCODE(CODE0,CODE1) \
104 IS_OPCODE (CODE0, CODE1, jmp_opcode)
106 #define CALL_INSN_0 0xC0
107 #define CALL_INSN_1 0x00
109 static const bfd_byte call_opcode[] =
111 CALL_INSN_0, CALL_INSN_1
114 #define IS_CALL_OPCODE(CODE0,CODE1) \
115 IS_OPCODE (CODE0, CODE1, call_opcode)
117 #define ADD_PCL_W_INSN_0 0x1E
118 #define ADD_PCL_W_INSN_1 0x09
120 static const bfd_byte add_pcl_w_opcode[] =
122 ADD_PCL_W_INSN_0, ADD_PCL_W_INSN_1
125 #define IS_ADD_PCL_W_OPCODE(CODE0,CODE1) \
126 IS_OPCODE (CODE0, CODE1, add_pcl_w_opcode)
128 #define ADD_W_WREG_INSN_0 0x1C
129 #define ADD_W_WREG_INSN_1 0x0A
131 static const bfd_byte add_w_wreg_opcode[] =
133 ADD_W_WREG_INSN_0, ADD_W_WREG_INSN_1
136 #define IS_ADD_W_WREG_OPCODE(CODE0,CODE1) \
137 IS_OPCODE (CODE0, CODE1, add_w_wreg_opcode)
139 #define SNC_INSN_0 0xA0
140 #define SNC_INSN_1 0x0B
142 static const bfd_byte snc_opcode[] =
144 SNC_INSN_0, SNC_INSN_1
147 #define IS_SNC_OPCODE(CODE0,CODE1) \
148 IS_OPCODE (CODE0, CODE1, snc_opcode)
150 #define INC_1_SP_INSN_0 0x2B
151 #define INC_1_SP_INSN_1 0x81
153 static const bfd_byte inc_1_sp_opcode[] =
155 INC_1_SP_INSN_0, INC_1_SP_INSN_1
158 #define IS_INC_1_SP_OPCODE(CODE0,CODE1) \
159 IS_OPCODE (CODE0, CODE1, inc_1_sp_opcode)
161 #define ADD_2_SP_W_INSN_0 0x1F
162 #define ADD_2_SP_W_INSN_1 0x82
164 static const bfd_byte add_2_sp_w_opcode[] =
166 ADD_2_SP_W_INSN_0, ADD_2_SP_W_INSN_1
169 #define IS_ADD_2_SP_W_OPCODE(CODE0,CODE1) \
170 IS_OPCODE (CODE0, CODE1, add_2_sp_w_opcode)
172 /* Relocation tables. */
173 static reloc_howto_type ip2k_elf_howto_table [] =
175 #define IP2K_HOWTO(t,rs,s,bs,pr,bp,name,sm,dm) \
176 HOWTO(t, /* type */ \
177 rs, /* rightshift */ \
178 s, /* size (0 = byte, 1 = short, 2 = long) */ \
179 bs, /* bitsize */ \
180 pr, /* pc_relative */ \
181 bp, /* bitpos */ \
182 complain_overflow_dont,/* complain_on_overflow */ \
183 bfd_elf_generic_reloc,/* special_function */ \
184 name, /* name */ \
185 FALSE, /* partial_inplace */ \
186 sm, /* src_mask */ \
187 dm, /* dst_mask */ \
188 pr) /* pcrel_offset */
190 /* This reloc does nothing. */
191 IP2K_HOWTO (R_IP2K_NONE, 0,2,32, FALSE, 0, "R_IP2K_NONE", 0, 0),
192 /* A 16 bit absolute relocation. */
193 IP2K_HOWTO (R_IP2K_16, 0,1,16, FALSE, 0, "R_IP2K_16", 0, 0xffff),
194 /* A 32 bit absolute relocation. */
195 IP2K_HOWTO (R_IP2K_32, 0,2,32, FALSE, 0, "R_IP2K_32", 0, 0xffffffff),
196 /* A 8-bit data relocation for the FR9 field. Ninth bit is computed specially. */
197 IP2K_HOWTO (R_IP2K_FR9, 0,1,9, FALSE, 0, "R_IP2K_FR9", 0, 0x00ff),
198 /* A 4-bit data relocation. */
199 IP2K_HOWTO (R_IP2K_BANK, 8,1,4, FALSE, 0, "R_IP2K_BANK", 0, 0x000f),
200 /* A 13-bit insn relocation - word address => right-shift 1 bit extra. */
201 IP2K_HOWTO (R_IP2K_ADDR16CJP, 1,1,13, FALSE, 0, "R_IP2K_ADDR16CJP", 0, 0x1fff),
202 /* A 3-bit insn relocation - word address => right-shift 1 bit extra. */
203 IP2K_HOWTO (R_IP2K_PAGE3, 14,1,3, FALSE, 0, "R_IP2K_PAGE3", 0, 0x0007),
204 /* Two 8-bit data relocations. */
205 IP2K_HOWTO (R_IP2K_LO8DATA, 0,1,8, FALSE, 0, "R_IP2K_LO8DATA", 0, 0x00ff),
206 IP2K_HOWTO (R_IP2K_HI8DATA, 8,1,8, FALSE, 0, "R_IP2K_HI8DATA", 0, 0x00ff),
207 /* Two 8-bit insn relocations. word address => right-shift 1 bit extra. */
208 IP2K_HOWTO (R_IP2K_LO8INSN, 1,1,8, FALSE, 0, "R_IP2K_LO8INSN", 0, 0x00ff),
209 IP2K_HOWTO (R_IP2K_HI8INSN, 9,1,8, FALSE, 0, "R_IP2K_HI8INSN", 0, 0x00ff),
211 /* Special 1 bit relocation for SKIP instructions. */
212 IP2K_HOWTO (R_IP2K_PC_SKIP, 1,1,1, FALSE, 12, "R_IP2K_PC_SKIP", 0xfffe, 0x1000),
213 /* 16 bit word address. */
214 IP2K_HOWTO (R_IP2K_TEXT, 1,1,16, FALSE, 0, "R_IP2K_TEXT", 0, 0xffff),
215 /* A 7-bit offset relocation for the FR9 field. Eigth and ninth bit comes from insn. */
216 IP2K_HOWTO (R_IP2K_FR_OFFSET, 0,1,9, FALSE, 0, "R_IP2K_FR_OFFSET", 0x180, 0x007f),
217 /* Bits 23:16 of an address. */
218 IP2K_HOWTO (R_IP2K_EX8DATA, 16,1,8, FALSE, 0, "R_IP2K_EX8DATA", 0, 0x00ff),
222 /* Map BFD reloc types to IP2K ELF reloc types. */
223 static reloc_howto_type *
224 ip2k_reloc_type_lookup (abfd, code)
225 bfd * abfd ATTRIBUTE_UNUSED;
226 bfd_reloc_code_real_type code;
228 /* Note that the ip2k_elf_howto_table is indxed by the R_
229 constants. Thus, the order that the howto records appear in the
230 table *must* match the order of the relocation types defined in
231 include/elf/ip2k.h. */
233 switch (code)
235 case BFD_RELOC_NONE:
236 return &ip2k_elf_howto_table[ (int) R_IP2K_NONE];
237 case BFD_RELOC_16:
238 return &ip2k_elf_howto_table[ (int) R_IP2K_16];
239 case BFD_RELOC_32:
240 return &ip2k_elf_howto_table[ (int) R_IP2K_32];
241 case BFD_RELOC_IP2K_FR9:
242 return &ip2k_elf_howto_table[ (int) R_IP2K_FR9];
243 case BFD_RELOC_IP2K_BANK:
244 return &ip2k_elf_howto_table[ (int) R_IP2K_BANK];
245 case BFD_RELOC_IP2K_ADDR16CJP:
246 return &ip2k_elf_howto_table[ (int) R_IP2K_ADDR16CJP];
247 case BFD_RELOC_IP2K_PAGE3:
248 return &ip2k_elf_howto_table[ (int) R_IP2K_PAGE3];
249 case BFD_RELOC_IP2K_LO8DATA:
250 return &ip2k_elf_howto_table[ (int) R_IP2K_LO8DATA];
251 case BFD_RELOC_IP2K_HI8DATA:
252 return &ip2k_elf_howto_table[ (int) R_IP2K_HI8DATA];
253 case BFD_RELOC_IP2K_LO8INSN:
254 return &ip2k_elf_howto_table[ (int) R_IP2K_LO8INSN];
255 case BFD_RELOC_IP2K_HI8INSN:
256 return &ip2k_elf_howto_table[ (int) R_IP2K_HI8INSN];
257 case BFD_RELOC_IP2K_PC_SKIP:
258 return &ip2k_elf_howto_table[ (int) R_IP2K_PC_SKIP];
259 case BFD_RELOC_IP2K_TEXT:
260 return &ip2k_elf_howto_table[ (int) R_IP2K_TEXT];
261 case BFD_RELOC_IP2K_FR_OFFSET:
262 return &ip2k_elf_howto_table[ (int) R_IP2K_FR_OFFSET];
263 case BFD_RELOC_IP2K_EX8DATA:
264 return &ip2k_elf_howto_table[ (int) R_IP2K_EX8DATA];
265 default:
266 /* Pacify gcc -Wall. */
267 return NULL;
269 return NULL;
272 #define PAGENO(ABSADDR) ((ABSADDR) & 0x1C000)
273 #define BASEADDR(SEC) ((SEC)->output_section->vma + (SEC)->output_offset)
275 #define UNDEFINED_SYMBOL (~(bfd_vma)0)
277 /* Return the value of the symbol associated with the relocation IREL. */
279 static bfd_vma
280 symbol_value (abfd, symtab_hdr, isymbuf, irel)
281 bfd *abfd;
282 Elf_Internal_Shdr *symtab_hdr;
283 Elf_Internal_Sym *isymbuf;
284 Elf_Internal_Rela *irel;
286 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
288 Elf_Internal_Sym *isym;
289 asection *sym_sec;
291 isym = isymbuf + ELF32_R_SYM (irel->r_info);
292 if (isym->st_shndx == SHN_UNDEF)
293 sym_sec = bfd_und_section_ptr;
294 else if (isym->st_shndx == SHN_ABS)
295 sym_sec = bfd_abs_section_ptr;
296 else if (isym->st_shndx == SHN_COMMON)
297 sym_sec = bfd_com_section_ptr;
298 else
299 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
301 return isym->st_value + BASEADDR (sym_sec);
303 else
305 unsigned long indx;
306 struct elf_link_hash_entry *h;
308 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
309 h = elf_sym_hashes (abfd)[indx];
310 BFD_ASSERT (h != NULL);
312 if (h->root.type != bfd_link_hash_defined
313 && h->root.type != bfd_link_hash_defweak)
314 return UNDEFINED_SYMBOL;
316 return (h->root.u.def.value + BASEADDR (h->root.u.def.section));
320 /* Determine if the instruction sequence matches that for
321 the prologue of a switch dispatch table with fewer than
322 128 entries.
325 page $nnn0
326 jmp $nnn0
327 add w,wreg
328 add pcl,w
329 addr=>
330 page $nnn1
331 jmp $nnn1
332 page $nnn2
333 jmp $nnn2
335 page $nnnN
336 jmp $nnnN
338 After relaxation.
340 page $nnn0
341 jmp $nnn0
342 add pcl,w
343 addr=>
344 jmp $nnn1
345 jmp $nnn2
347 jmp $nnnN */
349 static bfd_boolean
350 is_switch_128_dispatch_table_p (abfd, addr, relaxed, misc)
351 bfd *abfd ATTRIBUTE_UNUSED;
352 bfd_vma addr;
353 bfd_boolean relaxed;
354 struct misc *misc;
356 bfd_byte code0, code1;
358 if (addr < (3 * 2))
359 return FALSE;
361 code0 = bfd_get_8 (abfd, misc->contents + addr - 2);
362 code1 = bfd_get_8 (abfd, misc->contents + addr - 1);
364 /* Is it ADD PCL,W */
365 if (! IS_ADD_PCL_W_OPCODE (code0, code1))
366 return FALSE;
368 code0 = bfd_get_8 (abfd, misc->contents + addr - 4);
369 code1 = bfd_get_8 (abfd, misc->contents + addr - 3);
371 if (relaxed)
372 /* Is it ADD W,WREG */
373 return ! IS_ADD_W_WREG_OPCODE (code0, code1);
375 else
377 /* Is it ADD W,WREG */
378 if (! IS_ADD_W_WREG_OPCODE (code0, code1))
379 return FALSE;
381 code0 = bfd_get_8 (abfd, misc->contents + addr - 6);
382 code1 = bfd_get_8 (abfd, misc->contents + addr - 5);
384 /* Is it JMP $nnnn */
385 if (! IS_JMP_OPCODE (code0, code1))
386 return FALSE;
389 /* It looks like we've found the prologue for
390 a 1-127 entry switch dispatch table. */
391 return TRUE;
394 /* Determine if the instruction sequence matches that for
395 the prologue switch dispatch table with fewer than
396 256 entries but more than 127.
398 Before relaxation.
399 push %lo8insn(label) ; Push address of table
400 push %hi8insn(label)
401 add w,wreg ; index*2 => offset
402 snc ; CARRY SET?
403 inc 1(sp) ; Propagate MSB into table address
404 add 2(sp),w ; Add low bits of offset to table address
405 snc ; and handle any carry-out
406 inc 1(sp)
407 addr=>
408 page __indjmp ; Do an indirect jump to that location
409 jmp __indjmp
410 label: ; case dispatch table starts here
411 page $nnn1
412 jmp $nnn1
413 page $nnn2
414 jmp $nnn2
416 page $nnnN
417 jmp $nnnN
419 After relaxation.
420 push %lo8insn(label) ; Push address of table
421 push %hi8insn(label)
422 add 2(sp),w ; Add low bits of offset to table address
423 snc ; and handle any carry-out
424 inc 1(sp)
425 addr=>
426 page __indjmp ; Do an indirect jump to that location
427 jmp __indjmp
428 label: ; case dispatch table starts here
429 jmp $nnn1
430 jmp $nnn2
432 jmp $nnnN */
434 static bfd_boolean
435 is_switch_256_dispatch_table_p (abfd, addr, relaxed, misc)
436 bfd *abfd ATTRIBUTE_UNUSED;
437 bfd_vma addr;
438 bfd_boolean relaxed;
439 struct misc *misc;
441 bfd_byte code0, code1;
443 if (addr < (8 * 2))
444 return FALSE;
446 code0 = bfd_get_8 (abfd, misc->contents + addr - 2);
447 code1 = bfd_get_8 (abfd, misc->contents + addr - 1);
449 /* Is it INC 1(SP). */
450 if (! IS_INC_1_SP_OPCODE (code0, code1))
451 return FALSE;
453 code0 = bfd_get_8 (abfd, misc->contents + addr - 4);
454 code1 = bfd_get_8 (abfd, misc->contents + addr - 3);
456 /* Is it SNC. */
457 if (! IS_SNC_OPCODE (code0, code1))
458 return FALSE;
460 code0 = bfd_get_8 (abfd, misc->contents + addr - 6);
461 code1 = bfd_get_8 (abfd, misc->contents + addr - 5);
463 /* Is it ADD 2(SP),W. */
464 if (! IS_ADD_2_SP_W_OPCODE (code0, code1))
465 return FALSE;
467 code0 = bfd_get_8 (abfd, misc->contents + addr - 8);
468 code1 = bfd_get_8 (abfd, misc->contents + addr - 7);
470 if (relaxed)
471 /* Is it INC 1(SP). */
472 return ! IS_INC_1_SP_OPCODE (code0, code1);
474 else
476 /* Is it INC 1(SP). */
477 if (! IS_INC_1_SP_OPCODE (code0, code1))
478 return FALSE;
480 code0 = bfd_get_8 (abfd, misc->contents + addr - 10);
481 code1 = bfd_get_8 (abfd, misc->contents + addr - 9);
483 /* Is it SNC. */
484 if (! IS_SNC_OPCODE (code0, code1))
485 return FALSE;
487 code0 = bfd_get_8 (abfd, misc->contents + addr - 12);
488 code1 = bfd_get_8 (abfd, misc->contents + addr - 11);
490 /* Is it ADD W,WREG. */
491 if (! IS_ADD_W_WREG_OPCODE (code0, code1))
492 return FALSE;
495 /* It looks like we've found the prologue for
496 a 128-255 entry switch dispatch table. */
497 return TRUE;
500 static bfd_boolean
501 relax_switch_dispatch_tables_pass1 (abfd, sec, addr, misc)
502 bfd *abfd;
503 asection *sec;
504 bfd_vma addr;
505 struct misc *misc;
507 if (addr + 3 < sec->_cooked_size)
509 bfd_byte code0 = bfd_get_8 (abfd, misc->contents + addr + 2);
510 bfd_byte code1 = bfd_get_8 (abfd, misc->contents + addr + 3);
512 if (IS_JMP_OPCODE (code0, code1)
513 && is_switch_128_dispatch_table_p (abfd, addr, FALSE, misc))
515 /* Delete ADD W,WREG from prologue. */
516 ip2k_elf_relax_delete_bytes (abfd, sec, addr - (2 * 2), (1 * 2));
517 return TRUE;
520 if (IS_JMP_OPCODE (code0, code1)
521 && is_switch_256_dispatch_table_p (abfd, addr, FALSE, misc))
523 /* Delete ADD W,WREG; SNC ; INC 1(SP) from prologue. */
524 ip2k_elf_relax_delete_bytes (abfd, sec, addr - 6 * 2, 3 * 2);
525 return TRUE;
529 return TRUE;
532 static bfd_boolean
533 unrelax_dispatch_table_entries (abfd, sec, first, last, changed, misc)
534 bfd *abfd;
535 asection *sec;
536 bfd_vma first;
537 bfd_vma last;
538 bfd_boolean *changed;
539 struct misc *misc;
541 bfd_vma addr = first;
543 while (addr < last)
545 bfd_byte code0 = bfd_get_8 (abfd, misc->contents + addr);
546 bfd_byte code1 = bfd_get_8 (abfd, misc->contents + addr + 1);
548 /* We are only expecting to find PAGE or JMP insns
549 in the dispatch table. If we find anything else
550 something has gone wrong failed the relaxation
551 which will cause the link to be aborted. */
553 if (IS_PAGE_OPCODE (code0, code1))
554 /* Skip the PAGE and JMP insns. */
555 addr += 4;
556 else if (IS_JMP_OPCODE (code0, code1))
558 Elf_Internal_Rela * irelend = misc->irelbase
559 + sec->reloc_count;
560 Elf_Internal_Rela * irel;
562 /* Find the relocation entry. */
563 for (irel = misc->irelbase; irel < irelend; irel++)
565 if (irel->r_offset == addr
566 && ELF32_R_TYPE (irel->r_info) == R_IP2K_ADDR16CJP)
568 if (! add_page_insn (abfd, sec, irel, misc))
569 /* Something has gone wrong. */
570 return FALSE;
572 *changed = TRUE;
573 break;
577 /* If we fell off the end something has gone wrong. */
578 if (irel >= irelend)
579 /* Something has gone wrong. */
580 return FALSE;
582 /* Skip the PAGE and JMP isns. */
583 addr += 4;
584 /* Acount for the new PAGE insn. */
585 last += 2;
587 else
588 /* Something has gone wrong. */
589 return FALSE;
592 return TRUE;
595 static bfd_boolean
596 unrelax_switch_dispatch_tables_passN (abfd, sec, addr, changed, misc)
597 bfd *abfd;
598 asection *sec;
599 bfd_vma addr;
600 bfd_boolean *changed;
601 struct misc *misc;
603 if (2 <= addr && (addr + 3) < sec->_cooked_size)
605 bfd_byte code0 = bfd_get_8 (abfd, misc->contents + addr - 2);
606 bfd_byte code1 = bfd_get_8 (abfd, misc->contents + addr - 1);
608 if (IS_PAGE_OPCODE (code0, code1))
610 addr -= 2;
611 code0 = bfd_get_8 (abfd, misc->contents + addr + 2);
612 code1 = bfd_get_8 (abfd, misc->contents + addr + 3);
614 else
616 code0 = bfd_get_8 (abfd, misc->contents + addr);
617 code1 = bfd_get_8 (abfd, misc->contents + addr + 1);
620 if (IS_JMP_OPCODE (code0, code1)
621 && is_switch_128_dispatch_table_p (abfd, addr, TRUE, misc))
623 bfd_vma first = addr;
624 bfd_vma last = first;
625 bfd_boolean relaxed = TRUE;
627 /* On the final pass we must check if *all* entries in the
628 dispatch table are relaxed. If *any* are not relaxed
629 then we must unrelax *all* the entries in the dispach
630 table and also unrelax the dispatch table prologue. */
632 /* Find the last entry in the dispach table. */
633 while (last < sec->_cooked_size)
635 code0 = bfd_get_8 (abfd, misc->contents + last);
636 code1 = bfd_get_8 (abfd, misc->contents + last + 1);
638 if (IS_PAGE_OPCODE (code0, code1))
639 relaxed = FALSE;
640 else if (! IS_JMP_OPCODE (code0, code1))
641 break;
643 last += 2;
646 /* We should have found the end of the dispatch table
647 before reaching the end of the section. If we've have
648 reached the end then fail the relaxation which will
649 cause the link to be aborted. */
650 if (last >= sec->_cooked_size)
651 /* Something has gone wrong. */
652 return FALSE;
654 /* If we found an unrelaxed entry then
655 unlrelax all the switch table entries. */
656 if (! relaxed )
658 if (! unrelax_dispatch_table_entries (abfd, sec, first,
659 last, changed, misc))
660 /* Something has gone wrong. */
661 return FALSE;
663 if (! is_switch_128_dispatch_table_p (abfd, addr, TRUE, misc))
664 /* Something has gone wrong. */
665 return FALSE;
667 /* Unrelax the prologue. */
669 /* Insert an ADD W,WREG insnstruction. */
670 if (! ip2k_elf_relax_add_bytes (abfd, sec,
671 addr - 2,
672 add_w_wreg_opcode,
673 sizeof (add_w_wreg_opcode),
675 /* Something has gone wrong. */
676 return FALSE;
679 return TRUE;
682 if (IS_JMP_OPCODE (code0, code1)
683 && is_switch_256_dispatch_table_p (abfd, addr, TRUE, misc))
685 bfd_vma first = addr;
686 bfd_vma last;
687 bfd_boolean relaxed = TRUE;
689 /* On the final pass we must check if *all* entries in the
690 dispatch table are relaxed. If *any* are not relaxed
691 then we must unrelax *all* the entries in the dispach
692 table and also unrelax the dispatch table prologue. */
694 /* Note the 1st PAGE/JMP instructions are part of the
695 prologue and can safely be relaxed. */
697 code0 = bfd_get_8 (abfd, misc->contents + first);
698 code1 = bfd_get_8 (abfd, misc->contents + first + 1);
700 if (IS_PAGE_OPCODE (code0, code1))
702 first += 2;
703 code0 = bfd_get_8 (abfd, misc->contents + first);
704 code1 = bfd_get_8 (abfd, misc->contents + first + 1);
707 if (! IS_JMP_OPCODE (code0, code1))
708 /* Something has gone wrong. */
709 return FALSE;
711 first += 2;
712 last = first;
714 /* Find the last entry in the dispach table. */
715 while (last < sec->_cooked_size)
717 code0 = bfd_get_8 (abfd, misc->contents + last);
718 code1 = bfd_get_8 (abfd, misc->contents + last + 1);
720 if (IS_PAGE_OPCODE (code0, code1))
721 relaxed = FALSE;
722 else if (! IS_JMP_OPCODE (code0, code1))
723 break;
725 last += 2;
728 /* We should have found the end of the dispatch table
729 before reaching the end of the section. If we have
730 reached the end of the section then fail the
731 relaxation. */
732 if (last >= sec->_cooked_size)
733 return FALSE;
735 /* If we found an unrelaxed entry then
736 unrelax all the switch table entries. */
737 if (! relaxed)
739 if (! unrelax_dispatch_table_entries (abfd, sec, first,
740 last, changed, misc))
741 return FALSE;
743 if (! is_switch_256_dispatch_table_p (abfd, addr, TRUE, misc))
744 return FALSE;
746 /* Unrelax the prologue. */
748 /* Insert an INC 1(SP) insnstruction. */
749 if (! ip2k_elf_relax_add_bytes (abfd, sec,
750 addr - 6,
751 inc_1_sp_opcode,
752 sizeof (inc_1_sp_opcode),
754 return FALSE;
756 /* Insert an SNC insnstruction. */
757 if (! ip2k_elf_relax_add_bytes (abfd, sec,
758 addr - 6,
759 snc_opcode,
760 sizeof (snc_opcode),
762 return FALSE;
764 /* Insert an ADD W,WREG insnstruction. */
765 if (! ip2k_elf_relax_add_bytes (abfd, sec,
766 addr - 6,
767 add_w_wreg_opcode,
768 sizeof (add_w_wreg_opcode),
770 return FALSE;
773 return TRUE;
777 return TRUE;
780 /* This function handles relaxing for the ip2k. */
782 static bfd_boolean
783 ip2k_elf_relax_section (abfd, sec, link_info, again)
784 bfd *abfd;
785 asection *sec;
786 struct bfd_link_info *link_info;
787 bfd_boolean *again;
789 Elf_Internal_Shdr *symtab_hdr;
790 Elf_Internal_Rela *internal_relocs;
791 bfd_byte *contents = NULL;
792 Elf_Internal_Sym *isymbuf = NULL;
793 static asection * first_section = NULL;
794 static asection * last_section = NULL;
795 static bfd_boolean changed = FALSE;
796 static bfd_boolean final_pass = FALSE;
797 static unsigned int pass = 0;
798 struct misc misc;
799 asection *stab;
801 /* Assume nothing changes. */
802 *again = FALSE;
804 if (first_section == NULL)
805 first_section = sec;
807 if (first_section == sec)
809 changed = FALSE;
810 pass++;
813 /* If we make too many passes then it's a sign that
814 something is wrong and we fail the relaxation.
815 Note if everything is working correctly then the
816 relaxation should converge reasonably quickly. */
817 if (pass == 4096)
818 return FALSE;
820 /* We don't have to do anything for a relocatable link,
821 if this section does not have relocs, or if this is
822 not a code section. */
823 if (link_info->relocateable
824 || (sec->flags & SEC_RELOC) == 0
825 || sec->reloc_count == 0
826 || (sec->flags & SEC_CODE) == 0)
827 return TRUE;
829 if (pass == 1)
830 last_section = sec;
832 /* If this is the first time we have been called
833 for this section, initialise the cooked size. */
834 if (sec->_cooked_size == 0)
835 sec->_cooked_size = sec->_raw_size;
837 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
839 internal_relocs = _bfd_elf32_link_read_relocs (abfd, sec, NULL,
840 (Elf_Internal_Rela *)NULL,
841 link_info->keep_memory);
842 if (internal_relocs == NULL)
843 goto error_return;
845 /* Make sure the stac.rela stuff gets read in. */
846 stab = bfd_get_section_by_name (abfd, ".stab");
848 if (stab)
850 /* So stab does exits. */
851 Elf_Internal_Rela * irelbase;
853 irelbase = _bfd_elf32_link_read_relocs (abfd, stab, NULL,
854 (Elf_Internal_Rela *)NULL,
855 link_info->keep_memory);
858 /* Get section contents cached copy if it exists. */
859 if (contents == NULL)
861 /* Get cached copy if it exists. */
862 if (elf_section_data (sec)->this_hdr.contents != NULL)
863 contents = elf_section_data (sec)->this_hdr.contents;
864 else
866 /* Go get them off disk. */
867 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
868 if (contents == NULL)
869 goto error_return;
871 if (! bfd_get_section_contents (abfd, sec, contents,
872 (file_ptr) 0, sec->_raw_size))
873 goto error_return;
877 /* Read this BFD's symbols cached copy if it exists. */
878 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
880 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
881 if (isymbuf == NULL)
882 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
883 symtab_hdr->sh_info, 0,
884 NULL, NULL, NULL);
885 if (isymbuf == NULL)
886 goto error_return;
889 misc.symtab_hdr = symtab_hdr;
890 misc.isymbuf = isymbuf;
891 misc.irelbase = internal_relocs;
892 misc.contents = contents;
894 /* This is where all the relaxation actually get done. */
896 if (pass == 1)
898 /* On the first pass we remove *all* page instructions and
899 relax the prolog for switch dispatch tables. This gets
900 us to the starting point for subsequent passes where
901 we add page instructions back in as needed. */
903 if (! ip2k_elf_relax_section_pass1 (abfd, sec, again, &misc))
904 goto error_return;
906 changed |= *again;
908 else
910 /* Add page instructions back in as needed but we ignore
911 the issue with sections (functions) crossing a page
912 boundary until we have converged to an approximate
913 solution (i.e. nothing has changed on this relaxation
914 pass) and we then know roughly where the page boundaries
915 will end up.
917 After we have have converged to an approximate solution
918 we set the final pass flag and continue relaxing. On these
919 final passes if a section (function) cross page boundary
920 we will add *all* the page instructions back into such
921 sections.
923 After adding *all* page instructions back into a section
924 which crosses a page bounbdary we reset the final pass flag
925 so the we will again interate until we find a new approximate
926 solution which is closer to the final solution. */
928 if (! ip2k_elf_relax_section_passN (abfd, sec, again, &final_pass,
929 &misc))
930 goto error_return;
932 changed |= *again;
934 /* If nothing has changed on this relaxation
935 pass restart the final relaxaton pass. */
936 if (! changed && last_section == sec)
938 /* If this was the final pass and we didn't reset
939 the final pass flag then we are done, otherwise
940 do another final pass. */
941 if (! final_pass)
943 final_pass = TRUE;
944 *again = TRUE;
949 /* Perform some house keeping after relaxing the section. */
951 if (isymbuf != NULL
952 && symtab_hdr->contents != (unsigned char *) isymbuf)
954 if (! link_info->keep_memory)
955 free (isymbuf);
956 else
957 symtab_hdr->contents = (unsigned char *) isymbuf;
960 if (contents != NULL
961 && elf_section_data (sec)->this_hdr.contents != contents)
963 if (! link_info->keep_memory)
964 free (contents);
965 else
967 /* Cache the section contents for elf_link_input_bfd. */
968 elf_section_data (sec)->this_hdr.contents = contents;
972 if (internal_relocs != NULL
973 && elf_section_data (sec)->relocs != internal_relocs)
974 free (internal_relocs);
976 return TRUE;
978 error_return:
979 if (isymbuf != NULL
980 && symtab_hdr->contents != (unsigned char *) isymbuf)
981 free (isymbuf);
982 if (contents != NULL
983 && elf_section_data (sec)->this_hdr.contents != contents)
984 free (contents);
985 if (internal_relocs != NULL
986 && elf_section_data (sec)->relocs != internal_relocs)
987 free (internal_relocs);
988 return FALSE;
991 /* This function handles relaxation during the first pass. */
993 static bfd_boolean
994 ip2k_elf_relax_section_pass1 (abfd, sec, again, misc)
995 bfd *abfd;
996 asection *sec;
997 bfd_boolean *again;
998 struct misc * misc;
1000 Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
1001 Elf_Internal_Rela *irel;
1003 /* Walk thru the section looking for relaxation opertunities. */
1004 for (irel = misc->irelbase; irel < irelend; irel++)
1006 if (ELF32_R_TYPE (irel->r_info) == (int) R_IP2K_PAGE3)
1008 bfd_byte code0 = bfd_get_8 (abfd,
1009 misc->contents + irel->r_offset);
1010 bfd_byte code1 = bfd_get_8 (abfd,
1011 misc->contents + irel->r_offset + 1);
1013 /* Verify that this is the PAGE opcode. */
1014 if (IS_PAGE_OPCODE (code0, code1))
1016 /* Note that we've changed the relocs, section contents, etc. */
1017 elf_section_data (sec)->relocs = misc->irelbase;
1018 elf_section_data (sec)->this_hdr.contents = misc->contents;
1019 misc->symtab_hdr->contents = (bfd_byte *) misc->isymbuf;
1021 /* Handle switch dispatch tables/prologues. */
1022 if (! relax_switch_dispatch_tables_pass1 (abfd, sec,
1023 irel->r_offset, misc))
1024 return FALSE;
1026 /* Fix the relocation's type. */
1027 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1028 R_IP2K_NONE);
1030 /* Delete the PAGE insn. */
1031 if (! ip2k_elf_relax_delete_bytes (abfd, sec,
1032 irel->r_offset,
1033 sizeof (page_opcode)))
1034 return FALSE;
1036 /* That will change things, so, we should relax again.
1037 Note that this is not required, and it may be slow. */
1038 *again = TRUE;
1043 return TRUE;
1046 /* This function handles relaxation for 2nd and subsequent passes. */
1048 static bfd_boolean
1049 ip2k_elf_relax_section_passN (abfd, sec, again, final_pass, misc)
1050 bfd *abfd;
1051 asection *sec;
1052 bfd_boolean *again;
1053 bfd_boolean *final_pass;
1054 struct misc * misc;
1056 Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
1057 Elf_Internal_Rela *irel;
1058 bfd_boolean add_all;
1060 /* If we are on the final relaxation pass and the section crosses
1061 then set a flag to indicate that *all* page instructions need
1062 to be added back into this section. */
1063 if (*final_pass)
1065 add_all = (PAGENO (BASEADDR (sec))
1066 != PAGENO (BASEADDR (sec) + sec->_cooked_size));
1068 /* If this section crosses a page boundary set the crossed
1069 page boundary flag. */
1070 if (add_all)
1071 sec->userdata = sec;
1072 else
1074 /* If the section had previously crossed a page boundary
1075 but on this pass does not then reset crossed page
1076 boundary flag and rerun the 1st relaxation pass on
1077 this section. */
1078 if (sec->userdata)
1080 sec->userdata = NULL;
1081 if (! ip2k_elf_relax_section_pass1 (abfd, sec, again, misc))
1082 return FALSE;
1086 else
1087 add_all = FALSE;
1089 /* Walk thru the section looking for call/jmp
1090 instructions which need a page instruction. */
1091 for (irel = misc->irelbase; irel < irelend; irel++)
1093 if (ELF32_R_TYPE (irel->r_info) == (int) R_IP2K_ADDR16CJP)
1095 /* Get the value of the symbol referred to by the reloc. */
1096 bfd_vma symval = symbol_value (abfd, misc->symtab_hdr, misc->isymbuf,
1097 irel);
1098 bfd_byte code0, code1;
1100 if (symval == UNDEFINED_SYMBOL)
1102 /* This appears to be a reference to an undefined
1103 symbol. Just ignore it--it will be caught by the
1104 regular reloc processing. */
1105 continue;
1108 /* For simplicity of coding, we are going to modify the section
1109 contents, the section relocs, and the BFD symbol table. We
1110 must tell the rest of the code not to free up this
1111 information. It would be possible to instead create a table
1112 of changes which have to be made, as is done in coff-mips.c;
1113 that would be more work, but would require less memory when
1114 the linker is run. */
1116 /* Get the opcode. */
1117 code0 = bfd_get_8 (abfd, misc->contents + irel->r_offset);
1118 code1 = bfd_get_8 (abfd, misc->contents + irel->r_offset + 1);
1120 if (IS_JMP_OPCODE (code0, code1) || IS_CALL_OPCODE (code0, code1))
1122 if (*final_pass)
1124 if (! unrelax_switch_dispatch_tables_passN (abfd, sec,
1125 irel->r_offset,
1126 again, misc))
1127 return FALSE;
1129 if (*again)
1130 add_all = FALSE;
1133 code0 = bfd_get_8 (abfd, misc->contents + irel->r_offset - 2);
1134 code1 = bfd_get_8 (abfd, misc->contents + irel->r_offset - 1);
1136 if (! IS_PAGE_OPCODE (code0, code1))
1138 bfd_vma value = symval + irel->r_addend;
1139 bfd_vma addr = BASEADDR (sec) + irel->r_offset;
1141 if (add_all || PAGENO (addr) != PAGENO (value))
1143 if (! add_page_insn (abfd, sec, irel, misc))
1144 return FALSE;
1146 /* That will have changed things, so, we must relax again. */
1147 *again = TRUE;
1154 /* If anything changed reset the final pass flag. */
1155 if (*again)
1156 *final_pass = FALSE;
1158 return TRUE;
1161 /* Parts of a Stabs entry. */
1163 #define STRDXOFF (0)
1164 #define TYPEOFF (4)
1165 #define OTHEROFF (5)
1166 #define DESCOFF (6)
1167 #define VALOFF (8)
1168 #define STABSIZE (12)
1170 /* Adjust all the relocations entries after adding or inserting instructions. */
1172 static void
1173 adjust_all_relocations (abfd, sec, addr, endaddr, count, noadj)
1174 bfd *abfd;
1175 asection *sec;
1176 bfd_vma addr;
1177 bfd_vma endaddr;
1178 int count;
1179 int noadj;
1181 Elf_Internal_Shdr *symtab_hdr;
1182 Elf_Internal_Sym *isymbuf, *isym, *isymend;
1183 unsigned int shndx;
1184 bfd_byte *contents;
1185 Elf_Internal_Rela *irel, *irelend, *irelbase;
1186 struct elf_link_hash_entry **sym_hashes;
1187 struct elf_link_hash_entry **end_hashes;
1188 unsigned int symcount;
1190 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1191 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1193 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1195 contents = elf_section_data (sec)->this_hdr.contents;
1197 irelbase = elf_section_data (sec)->relocs;
1198 irelend = irelbase + sec->reloc_count;
1200 for (irel = irelbase; irel < irelend; irel++)
1202 if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE)
1204 /* Get the value of the symbol referred to by the reloc. */
1205 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1207 asection *sym_sec;
1209 /* A local symbol. */
1210 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1211 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1213 if (isym->st_shndx == shndx)
1215 bfd_vma baseaddr = BASEADDR (sec);
1216 bfd_vma symval = BASEADDR (sym_sec) + isym->st_value
1217 + irel->r_addend;
1219 if ((baseaddr + addr + noadj) <= symval
1220 && symval < (baseaddr + endaddr))
1221 irel->r_addend += count;
1226 /* Do this only for PC space relocations. */
1227 if (addr <= irel->r_offset && irel->r_offset < endaddr)
1228 irel->r_offset += count;
1231 /* When adding an instruction back it is sometimes necessary to move any
1232 global or local symbol that was referencing the first instruction of
1233 the moved block to refer to the first instruction of the inserted block.
1235 For example adding a PAGE instruction before a CALL or JMP requires
1236 that any label on the CALL or JMP is moved to the PAGE insn. */
1237 addr += noadj;
1239 /* Adjust the local symbols defined in this section. */
1240 isymend = isymbuf + symtab_hdr->sh_info;
1241 for (isym = isymbuf; isym < isymend; isym++)
1243 if (isym->st_shndx == shndx
1244 && addr <= isym->st_value
1245 && isym->st_value < endaddr)
1246 isym->st_value += count;
1249 /* Now adjust the global symbols defined in this section. */
1250 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1251 - symtab_hdr->sh_info);
1252 sym_hashes = elf_sym_hashes (abfd);
1253 end_hashes = sym_hashes + symcount;
1254 for (; sym_hashes < end_hashes; sym_hashes++)
1256 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1257 if ((sym_hash->root.type == bfd_link_hash_defined
1258 || sym_hash->root.type == bfd_link_hash_defweak)
1259 && sym_hash->root.u.def.section == sec)
1261 if (addr <= sym_hash->root.u.def.value
1262 && sym_hash->root.u.def.value < endaddr)
1264 sym_hash->root.u.def.value += count;
1269 return;
1272 static bfd_boolean
1273 add_page_insn (abfd, sec, irel, misc)
1274 bfd *abfd;
1275 asection *sec;
1276 Elf_Internal_Rela *irel;
1277 struct misc *misc;
1279 /* Note that we've changed the relocs, section contents, etc. */
1280 elf_section_data (sec)->relocs = misc->irelbase;
1281 elf_section_data (sec)->this_hdr.contents = misc->contents;
1282 misc->symtab_hdr->contents = (bfd_byte *) misc->isymbuf;
1284 /* Add the PAGE insn. */
1285 if (! ip2k_elf_relax_add_bytes (abfd, sec, irel->r_offset,
1286 page_opcode,
1287 sizeof (page_opcode),
1288 sizeof (page_opcode)))
1289 return FALSE;
1290 else
1292 Elf_Internal_Rela * jrel = irel - 1;
1294 /* Add relocation for PAGE insn added. */
1295 if (ELF32_R_TYPE (jrel->r_info) != R_IP2K_NONE)
1297 bfd_byte code0, code1;
1298 char *msg = NULL;
1300 /* Get the opcode. */
1301 code0 = bfd_get_8 (abfd, misc->contents + irel->r_offset);
1302 code1 = bfd_get_8 (abfd, misc->contents + irel->r_offset + 1);
1304 if (IS_JMP_OPCODE (code0, code1))
1305 msg = "\tJMP instruction missing a preceeding PAGE instruction in %s\n\n";
1307 else if (IS_CALL_OPCODE (code0, code1))
1308 msg = "\tCALL instruction missing a preceeding PAGE instruction in %s\n\n";
1310 if (msg)
1312 fprintf (stderr, "\n\t *** LINKER RELAXATION failure ***\n");
1313 fprintf (stderr, msg, sec->owner->filename);
1316 return FALSE;
1319 jrel->r_addend = irel->r_addend;
1320 jrel->r_offset = irel->r_offset - sizeof (page_opcode);
1321 jrel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1322 R_IP2K_PAGE3);
1325 return TRUE;
1328 /* Insert bytes into a section while relaxing. */
1330 static bfd_boolean
1331 ip2k_elf_relax_add_bytes (abfd, sec, addr, bytes, count, noadj)
1332 bfd *abfd;
1333 asection *sec;
1334 bfd_vma addr;
1335 const bfd_byte *bytes;
1336 int count;
1337 int noadj;
1339 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
1340 bfd_vma endaddr = sec->_cooked_size;
1342 /* Make room to insert the bytes. */
1343 memmove (contents + addr + count, contents + addr, endaddr - addr);
1345 /* Insert the bytes into the section. */
1346 memcpy (contents + addr, bytes, count);
1348 sec->_cooked_size += count;
1350 adjust_all_relocations (abfd, sec, addr, endaddr, count, noadj);
1351 return TRUE;
1354 /* Delete some bytes from a section while relaxing. */
1356 static bfd_boolean
1357 ip2k_elf_relax_delete_bytes (abfd, sec, addr, count)
1358 bfd *abfd;
1359 asection *sec;
1360 bfd_vma addr;
1361 int count;
1363 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
1364 bfd_vma endaddr = sec->_cooked_size;
1366 /* Actually delete the bytes. */
1367 memmove (contents + addr, contents + addr + count,
1368 endaddr - addr - count);
1370 sec->_cooked_size -= count;
1372 adjust_all_relocations (abfd, sec, addr + count, endaddr, -count, 0);
1373 return TRUE;
1376 /* -------------------------------------------------------------------- */
1378 /* XXX: The following code is the result of a cut&paste. This unfortunate
1379 practice is very widespread in the various target back-end files. */
1381 /* Set the howto pointer for a IP2K ELF reloc. */
1383 static void
1384 ip2k_info_to_howto_rela (abfd, cache_ptr, dst)
1385 bfd * abfd ATTRIBUTE_UNUSED;
1386 arelent * cache_ptr;
1387 Elf_Internal_Rela * dst;
1389 unsigned int r_type;
1391 r_type = ELF32_R_TYPE (dst->r_info);
1392 switch (r_type)
1394 default:
1395 cache_ptr->howto = & ip2k_elf_howto_table [r_type];
1396 break;
1400 /* Perform a single relocation.
1401 By default we use the standard BFD routines. */
1403 static bfd_reloc_status_type
1404 ip2k_final_link_relocate (howto, input_bfd, input_section, contents, rel,
1405 relocation)
1406 reloc_howto_type * howto;
1407 bfd * input_bfd;
1408 asection * input_section;
1409 bfd_byte * contents;
1410 Elf_Internal_Rela * rel;
1411 bfd_vma relocation;
1413 bfd_reloc_status_type r = bfd_reloc_ok;
1415 switch (howto->type)
1417 /* Handle data space relocations. */
1418 case R_IP2K_FR9:
1419 case R_IP2K_BANK:
1420 if ((relocation & IP2K_DATA_MASK) == IP2K_DATA_VALUE)
1421 relocation &= ~IP2K_DATA_MASK;
1422 else
1423 r = bfd_reloc_notsupported;
1424 break;
1426 case R_IP2K_LO8DATA:
1427 case R_IP2K_HI8DATA:
1428 case R_IP2K_EX8DATA:
1429 break;
1431 /* Handle insn space relocations. */
1432 case R_IP2K_ADDR16CJP:
1433 case R_IP2K_PAGE3:
1434 case R_IP2K_LO8INSN:
1435 case R_IP2K_HI8INSN:
1436 case R_IP2K_PC_SKIP:
1437 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1438 relocation &= ~IP2K_INSN_MASK;
1439 else
1440 r = bfd_reloc_notsupported;
1441 break;
1443 case R_IP2K_16:
1444 /* If this is a relocation involving a TEXT
1445 symbol, reduce it to a word address. */
1446 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1447 howto = &ip2k_elf_howto_table[ (int) R_IP2K_TEXT];
1448 break;
1450 /* Pass others through. */
1451 default:
1452 break;
1455 /* Only install relocation if above tests did not disqualify it. */
1456 if (r == bfd_reloc_ok)
1457 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1458 contents, rel->r_offset,
1459 relocation, rel->r_addend);
1461 return r;
1464 /* Relocate a IP2K ELF section.
1466 The RELOCATE_SECTION function is called by the new ELF backend linker
1467 to handle the relocations for a section.
1469 The relocs are always passed as Rela structures; if the section
1470 actually uses Rel structures, the r_addend field will always be
1471 zero.
1473 This function is responsible for adjusting the section contents as
1474 necessary, and (if using Rela relocs and generating a relocateable
1475 output file) adjusting the reloc addend as necessary.
1477 This function does not have to worry about setting the reloc
1478 address or the reloc symbol index.
1480 LOCAL_SYMS is a pointer to the swapped in local symbols.
1482 LOCAL_SECTIONS is an array giving the section in the input file
1483 corresponding to the st_shndx field of each local symbol.
1485 The global hash table entry for the global symbols can be found
1486 via elf_sym_hashes (input_bfd).
1488 When generating relocateable output, this function must handle
1489 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1490 going to be the section symbol corresponding to the output
1491 section, which means that the addend must be adjusted
1492 accordingly. */
1494 static bfd_boolean
1495 ip2k_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1496 contents, relocs, local_syms, local_sections)
1497 bfd *output_bfd ATTRIBUTE_UNUSED;
1498 struct bfd_link_info *info;
1499 bfd *input_bfd;
1500 asection *input_section;
1501 bfd_byte *contents;
1502 Elf_Internal_Rela *relocs;
1503 Elf_Internal_Sym *local_syms;
1504 asection **local_sections;
1506 Elf_Internal_Shdr *symtab_hdr;
1507 struct elf_link_hash_entry **sym_hashes;
1508 Elf_Internal_Rela *rel;
1509 Elf_Internal_Rela *relend;
1511 if (info->relocateable)
1512 return TRUE;
1514 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1515 sym_hashes = elf_sym_hashes (input_bfd);
1516 relend = relocs + input_section->reloc_count;
1518 for (rel = relocs; rel < relend; rel ++)
1520 reloc_howto_type * howto;
1521 unsigned long r_symndx;
1522 Elf_Internal_Sym * sym;
1523 asection * sec;
1524 struct elf_link_hash_entry * h;
1525 bfd_vma relocation;
1526 bfd_reloc_status_type r;
1527 const char * name = NULL;
1528 int r_type;
1530 /* This is a final link. */
1531 r_type = ELF32_R_TYPE (rel->r_info);
1532 r_symndx = ELF32_R_SYM (rel->r_info);
1533 howto = ip2k_elf_howto_table + ELF32_R_TYPE (rel->r_info);
1534 h = NULL;
1535 sym = NULL;
1536 sec = NULL;
1538 if (r_symndx < symtab_hdr->sh_info)
1540 sym = local_syms + r_symndx;
1541 sec = local_sections [r_symndx];
1542 relocation = BASEADDR (sec) + sym->st_value;
1544 name = bfd_elf_string_from_elf_section
1545 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1546 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1548 else
1550 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
1552 while (h->root.type == bfd_link_hash_indirect
1553 || h->root.type == bfd_link_hash_warning)
1554 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1556 name = h->root.root.string;
1558 if (h->root.type == bfd_link_hash_defined
1559 || h->root.type == bfd_link_hash_defweak)
1561 sec = h->root.u.def.section;
1562 relocation = h->root.u.def.value + BASEADDR (sec);
1564 else if (h->root.type == bfd_link_hash_undefweak)
1566 relocation = 0;
1568 else
1570 if (! ((*info->callbacks->undefined_symbol)
1571 (info, h->root.root.string, input_bfd,
1572 input_section, rel->r_offset,
1573 (! info->shared || info->no_undefined))))
1574 return FALSE;
1575 relocation = 0;
1579 /* Finally, the sole IP2K-specific part. */
1580 r = ip2k_final_link_relocate (howto, input_bfd, input_section,
1581 contents, rel, relocation);
1583 if (r != bfd_reloc_ok)
1585 const char * msg = (const char *) NULL;
1587 switch (r)
1589 case bfd_reloc_overflow:
1590 r = info->callbacks->reloc_overflow
1591 (info, name, howto->name, (bfd_vma) 0,
1592 input_bfd, input_section, rel->r_offset);
1593 break;
1595 case bfd_reloc_undefined:
1596 r = info->callbacks->undefined_symbol
1597 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1598 break;
1600 case bfd_reloc_outofrange:
1601 msg = _("internal error: out of range error");
1602 break;
1604 /* This is how ip2k_final_link_relocate tells us of a non-kosher
1605 reference between insn & data address spaces. */
1606 case bfd_reloc_notsupported:
1607 if (sym != NULL) /* Only if it's not an unresolved symbol. */
1608 msg = _("unsupported relocation between data/insn address spaces");
1609 break;
1611 case bfd_reloc_dangerous:
1612 msg = _("internal error: dangerous relocation");
1613 break;
1615 default:
1616 msg = _("internal error: unknown error");
1617 break;
1620 if (msg)
1621 r = info->callbacks->warning
1622 (info, msg, name, input_bfd, input_section, rel->r_offset);
1624 if (! r)
1625 return FALSE;
1629 return TRUE;
1632 static asection *
1633 ip2k_elf_gc_mark_hook (sec, info, rel, h, sym)
1634 asection *sec;
1635 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1636 Elf_Internal_Rela *rel;
1637 struct elf_link_hash_entry *h;
1638 Elf_Internal_Sym *sym;
1640 if (h != NULL)
1642 switch (ELF32_R_TYPE (rel->r_info))
1644 #if 0
1645 case R_IP2K_GNU_VTINHERIT:
1646 case R_IP2K_GNU_VTENTRY:
1647 break;
1648 #endif
1650 default:
1651 switch (h->root.type)
1653 case bfd_link_hash_defined:
1654 case bfd_link_hash_defweak:
1655 return h->root.u.def.section;
1657 case bfd_link_hash_common:
1658 return h->root.u.c.p->section;
1660 default:
1661 break;
1665 else
1667 if (!(elf_bad_symtab (sec->owner)
1668 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1669 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1670 && sym->st_shndx != SHN_COMMON))
1672 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1675 return NULL;
1678 static bfd_boolean
1679 ip2k_elf_gc_sweep_hook (abfd, info, sec, relocs)
1680 bfd *abfd ATTRIBUTE_UNUSED;
1681 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1682 asection *sec ATTRIBUTE_UNUSED;
1683 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1685 /* we don't use got and plt entries for ip2k */
1686 return TRUE;
1690 /* -------------------------------------------------------------------- */
1693 #define TARGET_BIG_SYM bfd_elf32_ip2k_vec
1694 #define TARGET_BIG_NAME "elf32-ip2k"
1696 #define ELF_ARCH bfd_arch_ip2k
1697 #define ELF_MACHINE_CODE EM_IP2K
1698 #define ELF_MACHINE_ALT1 EM_IP2K_OLD
1699 #define ELF_MAXPAGESIZE 1 /* No pages on the IP2K */
1701 #define elf_info_to_howto_rel NULL
1702 #define elf_info_to_howto ip2k_info_to_howto_rela
1704 #define elf_backend_can_gc_sections 1
1705 #define elf_backend_rela_normal 1
1706 #define elf_backend_gc_mark_hook ip2k_elf_gc_mark_hook
1707 #define elf_backend_gc_sweep_hook ip2k_elf_gc_sweep_hook
1709 #define elf_backend_relocate_section ip2k_elf_relocate_section
1711 #define elf_symbol_leading_char '_'
1712 #define bfd_elf32_bfd_reloc_type_lookup ip2k_reloc_type_lookup
1713 #define bfd_elf32_bfd_relax_section ip2k_elf_relax_section
1716 #include "elf32-target.h"