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