]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-hppa.c
* config/tc-m68k.c: Add some ATTRIBUTE_UNUSED.
[thirdparty/binutils-gdb.git] / bfd / elf32-hppa.c
CommitLineData
252b5132 1/* BFD back-end for HP PA-RISC ELF files.
e049a0de 2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
252b5132
RH
3 Free Software Foundation, Inc.
4
5 Written by
6
7 Center for Software Science
8 Department of Computer Science
9 University of Utah
10
11This file is part of BFD, the Binary File Descriptor library.
12
13This program is free software; you can redistribute it and/or modify
14it under the terms of the GNU General Public License as published by
15the Free Software Foundation; either version 2 of the License, or
16(at your option) any later version.
17
18This program is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with this program; if not, write to the Free Software
25Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26
27#include "bfd.h"
28#include "sysdep.h"
252b5132
RH
29#include "libbfd.h"
30#include "elf-bfd.h"
9e103c9c
JL
31#include "elf/hppa.h"
32#include "libhppa.h"
33#include "elf32-hppa.h"
34#define ARCH_SIZE 32
35#include "elf-hppa.h"
36
252b5132 37
252b5132
RH
38/* We use three different hash tables to hold information for
39 linking PA ELF objects.
40
41 The first is the elf32_hppa_link_hash_table which is derived
42 from the standard ELF linker hash table. We use this as a place to
43 attach other hash tables and static information.
44
45 The second is the stub hash table which is derived from the
46 base BFD hash table. The stub hash table holds the information
31bd8f25 47 necessary to build the linker stubs during a link. */
252b5132
RH
48
49/* Hash table for linker stubs. */
50
51struct elf32_hppa_stub_hash_entry
52{
53 /* Base hash table entry structure, we can get the name of the stub
54 (and thus know exactly what actions it performs) from the base
55 hash table entry. */
56 struct bfd_hash_entry root;
57
58 /* Offset of the beginning of this stub. */
59 bfd_vma offset;
60
61 /* Given the symbol's value and its section we can determine its final
62 value when building the stubs (so the stub knows where to jump. */
63 symvalue target_value;
64 asection *target_section;
65};
66
67struct elf32_hppa_stub_hash_table
68{
69 /* The hash table itself. */
70 struct bfd_hash_table root;
71
72 /* The stub BFD. */
73 bfd *stub_bfd;
74
75 /* Where to place the next stub. */
76 bfd_byte *location;
77
78 /* Current offset in the stub section. */
79 unsigned int offset;
80
81};
82
252b5132
RH
83struct elf32_hppa_link_hash_entry
84{
85 struct elf_link_hash_entry root;
86};
87
88struct elf32_hppa_link_hash_table
89{
90 /* The main hash table. */
91 struct elf_link_hash_table root;
92
93 /* The stub hash table. */
94 struct elf32_hppa_stub_hash_table *stub_hash_table;
95
252b5132
RH
96 /* A count of the number of output symbols. */
97 unsigned int output_symbol_count;
98
99 /* Stuff so we can handle DP relative relocations. */
100 long global_value;
101 int global_sym_defined;
102};
103
252b5132
RH
104/* ELF32/HPPA relocation support
105
106 This file contains ELF32/HPPA relocation support as specified
107 in the Stratus FTX/Golf Object File Format (SED-1762) dated
108 February 1994. */
109
110#include "elf32-hppa.h"
111#include "hppa_stubs.h"
112
252b5132
RH
113static unsigned long hppa_elf_relocate_insn
114 PARAMS ((bfd *, asection *, unsigned long, unsigned long, long,
115 long, unsigned long, unsigned long, unsigned long));
116
252b5132
RH
117static boolean hppa_elf_is_local_label_name PARAMS ((bfd *, const char *));
118
119static boolean elf32_hppa_add_symbol_hook
120 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
121 const char **, flagword *, asection **, bfd_vma *));
122
123static bfd_reloc_status_type elf32_hppa_bfd_final_link_relocate
124 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *,
125 bfd_byte *, bfd_vma, bfd_vma, bfd_vma, struct bfd_link_info *,
126 asection *, const char *, int));
127
128static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
129 PARAMS ((bfd *));
130
131static struct bfd_hash_entry *
132elf32_hppa_stub_hash_newfunc
133 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
134
252b5132
RH
135static boolean
136elf32_hppa_relocate_section
137 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
138 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
139
140static boolean
141elf32_hppa_stub_hash_table_init
142 PARAMS ((struct elf32_hppa_stub_hash_table *, bfd *,
143 struct bfd_hash_entry *(*) PARAMS ((struct bfd_hash_entry *,
144 struct bfd_hash_table *,
145 const char *))));
146
147static boolean
148elf32_hppa_build_one_stub PARAMS ((struct bfd_hash_entry *, PTR));
149
252b5132 150static unsigned int elf32_hppa_size_of_stub
31bd8f25 151 PARAMS ((bfd_vma, bfd_vma, const char *));
252b5132
RH
152
153static void elf32_hppa_name_of_stub
31bd8f25 154 PARAMS ((bfd_vma, bfd_vma, char *));
252b5132
RH
155
156/* For linker stub hash tables. */
157#define elf32_hppa_stub_hash_lookup(table, string, create, copy) \
158 ((struct elf32_hppa_stub_hash_entry *) \
159 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
160
161#define elf32_hppa_stub_hash_traverse(table, func, info) \
162 (bfd_hash_traverse \
163 (&(table)->root, \
164 (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
165 (info)))
166
252b5132
RH
167/* For HPPA linker hash table. */
168
169#define elf32_hppa_link_hash_lookup(table, string, create, copy, follow)\
170 ((struct elf32_hppa_link_hash_entry *) \
171 elf_link_hash_lookup (&(table)->root, (string), (create), \
172 (copy), (follow)))
173
174#define elf32_hppa_link_hash_traverse(table, func, info) \
175 (elf_link_hash_traverse \
176 (&(table)->root, \
177 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
178 (info)))
179
180/* Get the PA ELF linker hash table from a link_info structure. */
181
182#define elf32_hppa_hash_table(p) \
183 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
184
185
252b5132
RH
186/* Assorted hash table functions. */
187
188/* Initialize an entry in the stub hash table. */
189
190static struct bfd_hash_entry *
191elf32_hppa_stub_hash_newfunc (entry, table, string)
192 struct bfd_hash_entry *entry;
193 struct bfd_hash_table *table;
194 const char *string;
195{
196 struct elf32_hppa_stub_hash_entry *ret;
197
198 ret = (struct elf32_hppa_stub_hash_entry *) entry;
199
200 /* Allocate the structure if it has not already been allocated by a
201 subclass. */
202 if (ret == NULL)
203 ret = ((struct elf32_hppa_stub_hash_entry *)
204 bfd_hash_allocate (table,
205 sizeof (struct elf32_hppa_stub_hash_entry)));
206 if (ret == NULL)
207 return NULL;
208
209 /* Call the allocation method of the superclass. */
210 ret = ((struct elf32_hppa_stub_hash_entry *)
211 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
212
213 if (ret)
214 {
215 /* Initialize the local fields. */
216 ret->offset = 0;
217 ret->target_value = 0;
218 ret->target_section = NULL;
219 }
220
221 return (struct bfd_hash_entry *) ret;
222}
223
224/* Initialize a stub hash table. */
225
226static boolean
227elf32_hppa_stub_hash_table_init (table, stub_bfd, newfunc)
228 struct elf32_hppa_stub_hash_table *table;
229 bfd *stub_bfd;
230 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
231 struct bfd_hash_table *,
232 const char *));
233{
234 table->offset = 0;
235 table->location = 0;
236 table->stub_bfd = stub_bfd;
237 return (bfd_hash_table_init (&table->root, newfunc));
238}
239
252b5132
RH
240/* Create the derived linker hash table. The PA ELF port uses the derived
241 hash table to keep information specific to the PA ELF linker (without
242 using static variables). */
243
244static struct bfd_link_hash_table *
245elf32_hppa_link_hash_table_create (abfd)
246 bfd *abfd;
247{
248 struct elf32_hppa_link_hash_table *ret;
249
250 ret = ((struct elf32_hppa_link_hash_table *)
251 bfd_alloc (abfd, sizeof (struct elf32_hppa_link_hash_table)));
252 if (ret == NULL)
253 return NULL;
254 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
255 _bfd_elf_link_hash_newfunc))
256 {
257 bfd_release (abfd, ret);
258 return NULL;
259 }
260 ret->stub_hash_table = NULL;
252b5132
RH
261 ret->output_symbol_count = 0;
262 ret->global_value = 0;
263 ret->global_sym_defined = 0;
264
265 return &ret->root.root;
266}
267
268/* Relocate the given INSN given the various input parameters.
269
270 FIXME: endianness and sizeof (long) issues abound here. */
271
272static unsigned long
273hppa_elf_relocate_insn (abfd, input_sect, insn, address, sym_value,
274 r_addend, r_format, r_field, pcrel)
275 bfd *abfd;
276 asection *input_sect;
277 unsigned long insn;
278 unsigned long address;
279 long sym_value;
280 long r_addend;
281 unsigned long r_format;
282 unsigned long r_field;
283 unsigned long pcrel;
284{
285 unsigned char opcode = get_opcode (insn);
286 long constant_value;
287
288 switch (opcode)
289 {
290 case LDO:
291 case LDB:
292 case LDH:
293 case LDW:
294 case LDWM:
295 case STB:
296 case STH:
297 case STW:
298 case STWM:
299 case COMICLR:
300 case SUBI:
301 case ADDIT:
302 case ADDI:
303 case LDIL:
304 case ADDIL:
305 constant_value = HPPA_R_CONSTANT (r_addend);
306
307 if (pcrel)
308 sym_value -= address;
309
310 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
311 return hppa_rebuild_insn (abfd, insn, sym_value, r_format);
312
313 case BL:
314 case BE:
315 case BLE:
316 /* XXX computing constant_value is not needed??? */
317 constant_value = assemble_17 ((insn & 0x001f0000) >> 16,
318 (insn & 0x00001ffc) >> 2,
319 insn & 1);
320
321 constant_value = (constant_value << 15) >> 15;
322 if (pcrel)
323 {
324 sym_value -=
325 address + input_sect->output_offset
326 + input_sect->output_section->vma;
327 sym_value = hppa_field_adjust (sym_value, -8, r_field);
328 }
329 else
330 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
331
332 return hppa_rebuild_insn (abfd, insn, sym_value >> 2, r_format);
333
334 default:
335 if (opcode == 0)
336 {
337 constant_value = HPPA_R_CONSTANT (r_addend);
338
339 if (pcrel)
340 sym_value -= address;
341
342 return hppa_field_adjust (sym_value, constant_value, r_field);
343 }
344 else
345 abort ();
346 }
347}
348
349/* Relocate an HPPA ELF section. */
350
351static boolean
352elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
353 contents, relocs, local_syms, local_sections)
354 bfd *output_bfd;
355 struct bfd_link_info *info;
356 bfd *input_bfd;
357 asection *input_section;
358 bfd_byte *contents;
359 Elf_Internal_Rela *relocs;
360 Elf_Internal_Sym *local_syms;
361 asection **local_sections;
362{
363 Elf_Internal_Shdr *symtab_hdr;
364 Elf_Internal_Rela *rel;
365 Elf_Internal_Rela *relend;
366
367 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
368
369 rel = relocs;
370 relend = relocs + input_section->reloc_count;
371 for (; rel < relend; rel++)
372 {
373 int r_type;
374 reloc_howto_type *howto;
375 unsigned long r_symndx;
376 struct elf_link_hash_entry *h;
377 Elf_Internal_Sym *sym;
378 asection *sym_sec;
379 bfd_vma relocation;
380 bfd_reloc_status_type r;
381 const char *sym_name;
382
383 r_type = ELF32_R_TYPE (rel->r_info);
384 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
385 {
386 bfd_set_error (bfd_error_bad_value);
387 return false;
388 }
389 howto = elf_hppa_howto_table + r_type;
390
391 r_symndx = ELF32_R_SYM (rel->r_info);
392
393 if (info->relocateable)
394 {
395 /* This is a relocateable link. We don't have to change
396 anything, unless the reloc is against a section symbol,
397 in which case we have to adjust according to where the
398 section symbol winds up in the output section. */
399 if (r_symndx < symtab_hdr->sh_info)
400 {
401 sym = local_syms + r_symndx;
402 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
403 {
404 sym_sec = local_sections[r_symndx];
405 rel->r_addend += sym_sec->output_offset;
406 }
407 }
408
409 continue;
410 }
411
412 /* This is a final link. */
413 h = NULL;
414 sym = NULL;
415 sym_sec = NULL;
416 if (r_symndx < symtab_hdr->sh_info)
417 {
418 sym = local_syms + r_symndx;
419 sym_sec = local_sections[r_symndx];
420 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
421 ? 0 : sym->st_value)
422 + sym_sec->output_offset
423 + sym_sec->output_section->vma);
424 }
425 else
426 {
427 long indx;
428
429 indx = r_symndx - symtab_hdr->sh_info;
430 h = elf_sym_hashes (input_bfd)[indx];
431 while (h->root.type == bfd_link_hash_indirect
432 || h->root.type == bfd_link_hash_warning)
433 h = (struct elf_link_hash_entry *) h->root.u.i.link;
434 if (h->root.type == bfd_link_hash_defined
435 || h->root.type == bfd_link_hash_defweak)
436 {
437 sym_sec = h->root.u.def.section;
438 relocation = (h->root.u.def.value
439 + sym_sec->output_offset
440 + sym_sec->output_section->vma);
441 }
442 else if (h->root.type == bfd_link_hash_undefweak)
443 relocation = 0;
444 else
445 {
446 if (!((*info->callbacks->undefined_symbol)
447 (info, h->root.root.string, input_bfd,
448 input_section, rel->r_offset)))
449 return false;
450 break;
451 }
452 }
453
454 if (h != NULL)
455 sym_name = h->root.root.string;
456 else
457 {
458 sym_name = bfd_elf_string_from_elf_section (input_bfd,
459 symtab_hdr->sh_link,
460 sym->st_name);
461 if (sym_name == NULL)
462 return false;
463 if (*sym_name == '\0')
464 sym_name = bfd_section_name (input_bfd, sym_sec);
465 }
466
252b5132
RH
467 r = elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
468 input_section, contents,
469 rel->r_offset, relocation,
470 rel->r_addend, info, sym_sec,
471 sym_name, h == NULL);
472
473 if (r != bfd_reloc_ok)
474 {
475 switch (r)
476 {
477 /* This can happen for DP relative relocs if $global$ is
478 undefined. This is a panic situation so we don't try
479 to continue. */
480 case bfd_reloc_undefined:
481 case bfd_reloc_notsupported:
482 if (!((*info->callbacks->undefined_symbol)
483 (info, "$global$", input_bfd,
484 input_section, rel->r_offset)))
485 return false;
486 return false;
487 case bfd_reloc_dangerous:
488 {
489 /* We use this return value to indicate that we performed
490 a "dangerous" relocation. This doesn't mean we did
491 the wrong thing, it just means there may be some cleanup
492 that needs to be done here.
493
494 In particular we had to swap the last call insn and its
495 delay slot. If the delay slot insn needed a relocation,
496 then we'll need to adjust the next relocation entry's
497 offset to account for the fact that the insn moved.
498
499 This hair wouldn't be necessary if we inserted stubs
500 between procedures and used a "bl" to get to the stub. */
501 if (rel != relend)
502 {
503 Elf_Internal_Rela *next_rel = rel + 1;
504
505 if (rel->r_offset + 4 == next_rel->r_offset)
506 next_rel->r_offset -= 4;
507 }
508 break;
509 }
510 default:
511 case bfd_reloc_outofrange:
512 case bfd_reloc_overflow:
513 {
514 if (!((*info->callbacks->reloc_overflow)
515 (info, sym_name, howto->name, (bfd_vma) 0,
516 input_bfd, input_section, rel->r_offset)))
517 return false;
518 }
519 break;
520 }
521 }
522 }
523
524 return true;
525}
526
252b5132
RH
527/* Actually perform a relocation as part of a final link. This can get
528 rather hairy when linker stubs are needed. */
529
530static bfd_reloc_status_type
531elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
532 input_section, contents, offset, value,
533 addend, info, sym_sec, sym_name, is_local)
534 reloc_howto_type *howto;
535 bfd *input_bfd;
536 bfd *output_bfd;
537 asection *input_section;
538 bfd_byte *contents;
539 bfd_vma offset;
540 bfd_vma value;
541 bfd_vma addend;
542 struct bfd_link_info *info;
543 asection *sym_sec;
544 const char *sym_name;
545 int is_local;
546{
547 unsigned long insn;
548 unsigned long r_type = howto->type;
549 unsigned long r_format = howto->bitsize;
550 unsigned long r_field = e_fsel;
551 bfd_byte *hit_data = contents + offset;
552 boolean r_pcrel = howto->pc_relative;
553
554 insn = bfd_get_32 (input_bfd, hit_data);
555
556 /* Make sure we have a value for $global$. FIXME isn't this effectively
557 just like the gp pointer on MIPS? Can we use those routines for this
558 purpose? */
559 if (!elf32_hppa_hash_table (info)->global_sym_defined)
560 {
561 struct elf_link_hash_entry *h;
562 asection *sec;
563
564 h = elf_link_hash_lookup (elf_hash_table (info), "$global$", false,
565 false, false);
566
567 /* If there isn't a $global$, then we're in deep trouble. */
568 if (h == NULL)
569 return bfd_reloc_notsupported;
570
571 /* If $global$ isn't a defined symbol, then we're still in deep
572 trouble. */
573 if (h->root.type != bfd_link_hash_defined)
574 return bfd_reloc_undefined;
575
576 sec = h->root.u.def.section;
577 elf32_hppa_hash_table (info)->global_value = (h->root.u.def.value
578 + sec->output_section->vma
579 + sec->output_offset);
580 elf32_hppa_hash_table (info)->global_sym_defined = 1;
581 }
582
583 switch (r_type)
584 {
585 case R_PARISC_NONE:
586 break;
587
588 case R_PARISC_DIR32:
589 case R_PARISC_DIR17F:
590 case R_PARISC_PCREL17C:
591 r_field = e_fsel;
592 goto do_basic_type_1;
593 case R_PARISC_DIR21L:
594 case R_PARISC_PCREL21L:
595 r_field = e_lrsel;
596 goto do_basic_type_1;
597 case R_PARISC_DIR17R:
598 case R_PARISC_PCREL17R:
599 case R_PARISC_DIR14R:
600 case R_PARISC_PCREL14R:
601 r_field = e_rrsel;
602 goto do_basic_type_1;
603
604 /* For all the DP relative relocations, we need to examine the symbol's
605 section. If it's a code section, then "data pointer relative" makes
606 no sense. In that case we don't adjust the "value", and for 21 bit
607 addil instructions, we change the source addend register from %dp to
608 %r0. */
609 case R_PARISC_DPREL21L:
610 r_field = e_lrsel;
611 if (sym_sec->flags & SEC_CODE)
612 {
613 if ((insn & 0xfc000000) >> 26 == 0xa
614 && (insn & 0x03e00000) >> 21 == 0x1b)
615 insn &= ~0x03e00000;
616 }
617 else
618 value -= elf32_hppa_hash_table (info)->global_value;
619 goto do_basic_type_1;
620 case R_PARISC_DPREL14R:
621 r_field = e_rrsel;
622 if ((sym_sec->flags & SEC_CODE) == 0)
623 value -= elf32_hppa_hash_table (info)->global_value;
624 goto do_basic_type_1;
625 case R_PARISC_DPREL14F:
626 r_field = e_fsel;
627 if ((sym_sec->flags & SEC_CODE) == 0)
628 value -= elf32_hppa_hash_table (info)->global_value;
629 goto do_basic_type_1;
630
631 /* These cases are separate as they may involve a lot more work
632 to deal with linker stubs. */
633 case R_PARISC_PLABEL32:
634 case R_PARISC_PLABEL21L:
635 case R_PARISC_PLABEL14R:
636 case R_PARISC_PCREL17F:
637 {
638 bfd_vma location;
e049a0de 639 unsigned int len;
252b5132
RH
640 char *new_name, *stub_name;
641
642 /* Get the field selector right. We'll need it in a minute. */
643 if (r_type == R_PARISC_PCREL17F
644 || r_type == R_PARISC_PLABEL32)
645 r_field = e_fsel;
646 else if (r_type == R_PARISC_PLABEL21L)
647 r_field = e_lrsel;
648 else if (r_type == R_PARISC_PLABEL14R)
649 r_field = e_rrsel;
650
651 /* Find out where we are and where we're going. */
652 location = (offset +
653 input_section->output_offset +
654 input_section->output_section->vma);
655
252b5132
RH
656 len = strlen (sym_name) + 1;
657 if (is_local)
658 len += 9;
659 new_name = bfd_malloc (len);
660 if (!new_name)
661 return bfd_reloc_notsupported;
662 strcpy (new_name, sym_name);
663
664 /* Local symbols have unique IDs. */
665 if (is_local)
666 sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
667
252b5132
RH
668 /* Any kind of linker stub needed? */
669 if (((int)(value - location) > 0x3ffff)
31bd8f25 670 || ((int)(value - location) < (int)0xfffc0000))
252b5132
RH
671 {
672 struct elf32_hppa_stub_hash_table *stub_hash_table;
673 struct elf32_hppa_stub_hash_entry *stub_hash;
674 asection *stub_section;
675
676 /* Build a name for the stub. */
677
678 len = strlen (new_name);
679 len += 23;
680 stub_name = bfd_malloc (len);
681 if (!stub_name)
682 return bfd_reloc_notsupported;
31bd8f25 683 elf32_hppa_name_of_stub (location, value, stub_name);
252b5132
RH
684 strcat (stub_name, new_name);
685 free (new_name);
686
687 stub_hash_table = elf32_hppa_hash_table (info)->stub_hash_table;
688
689 stub_hash
690 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
691 false, false);
692
693 /* We're done with that name. */
694 free (stub_name);
695
696 /* The stub BFD only has one section. */
697 stub_section = stub_hash_table->stub_bfd->sections;
698
699 if (stub_hash != NULL)
700 {
252b5132
RH
701 if (r_type == R_PARISC_PCREL17F)
702 {
703 unsigned long delay_insn;
704 unsigned int opcode, rtn_reg, ldo_target_reg, ldo_src_reg;
705
706 /* We'll need to peek at the next insn. */
707 delay_insn = bfd_get_32 (input_bfd, hit_data + 4);
708 opcode = get_opcode (delay_insn);
709
710 /* We also need to know the return register for this
711 call. */
712 rtn_reg = (insn & 0x03e00000) >> 21;
713
714 ldo_src_reg = (delay_insn & 0x03e00000) >> 21;
715 ldo_target_reg = (delay_insn & 0x001f0000) >> 16;
716
717 /* Munge up the value and other parameters for
718 hppa_elf_relocate_insn. */
719
720 value = (stub_hash->offset
721 + stub_section->output_offset
722 + stub_section->output_section->vma);
723
724 r_format = 17;
725 r_field = e_fsel;
726 r_pcrel = 0;
727 addend = 0;
728
729 /* We need to peek at the delay insn and determine if
730 we'll need to swap the branch and its delay insn. */
731 if ((insn & 2)
732 || (opcode == LDO
733 && ldo_target_reg == rtn_reg)
734 || (delay_insn == 0x08000240))
735 {
736 /* No need to swap the branch and its delay slot, but
737 we do need to make sure to jump past the return
738 pointer update in the stub. */
739 value += 4;
740
741 /* If the delay insn does a return pointer adjustment,
742 then we have to make sure it stays valid. */
743 if (opcode == LDO
744 && ldo_target_reg == rtn_reg)
745 {
746 delay_insn &= 0xfc00ffff;
747 delay_insn |= ((31 << 21) | (31 << 16));
748 bfd_put_32 (input_bfd, delay_insn, hit_data + 4);
749 }
750 /* Use a BLE to reach the stub. */
751 insn = BLE_SR4_R0;
752 }
753 else
754 {
755 /* Wonderful, we have to swap the call insn and its
756 delay slot. */
757 bfd_put_32 (input_bfd, delay_insn, hit_data);
758 /* Use a BLE,n to reach the stub. */
759 insn = (BLE_SR4_R0 | 0x2);
760 bfd_put_32 (input_bfd, insn, hit_data + 4);
761 insn = hppa_elf_relocate_insn (input_bfd,
762 input_section,
763 insn, offset + 4,
764 value, addend,
765 r_format, r_field,
766 r_pcrel);
767 /* Update the instruction word. */
768 bfd_put_32 (input_bfd, insn, hit_data + 4);
769 return bfd_reloc_dangerous;
770 }
771 }
31bd8f25
JL
772 else
773 return bfd_reloc_notsupported;
774 }
252b5132
RH
775 }
776 goto do_basic_type_1;
777 }
778
779do_basic_type_1:
780 insn = hppa_elf_relocate_insn (input_bfd, input_section, insn,
781 offset, value, addend, r_format,
782 r_field, r_pcrel);
783 break;
784
785 /* Something we don't know how to handle. */
786 default:
787 return bfd_reloc_notsupported;
788 }
789
790 /* Update the instruction word. */
791 bfd_put_32 (input_bfd, insn, hit_data);
792 return (bfd_reloc_ok);
793}
794
252b5132
RH
795/* Return true if SYM represents a local label symbol. */
796
797static boolean
798hppa_elf_is_local_label_name (abfd, name)
799 bfd *abfd;
800 const char *name;
801{
802 return (name[0] == 'L' && name[1] == '$');
803}
804
252b5132
RH
805/* Undo the generic ELF code's subtraction of section->vma from the
806 value of each external symbol. */
807
808static boolean
809elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
810 bfd *abfd;
811 struct bfd_link_info *info;
812 const Elf_Internal_Sym *sym;
813 const char **namep;
814 flagword *flagsp;
815 asection **secp;
816 bfd_vma *valp;
817{
818 *valp += (*secp)->vma;
819 return true;
820}
821
822/* Determine the name of the stub needed to perform a call assuming the
823 argument relocation bits for caller and callee are in CALLER and CALLEE
824 for a call from LOCATION to DESTINATION. Copy the name into STUB_NAME. */
825
826static void
31bd8f25 827elf32_hppa_name_of_stub (location, destination, stub_name)
252b5132
RH
828 bfd_vma location, destination;
829 char *stub_name;
830{
31bd8f25 831 strcpy (stub_name, "_____long_branch_stub_");
252b5132
RH
832}
833
834/* Compute the size of the stub needed to call from LOCATION to DESTINATION
835 (a function named SYM_NAME), with argument relocation bits CALLER and
836 CALLEE. Return zero if no stub is needed to perform such a call. */
837
838static unsigned int
31bd8f25 839elf32_hppa_size_of_stub (location, destination, sym_name)
252b5132
RH
840 bfd_vma location, destination;
841 const char *sym_name;
842{
31bd8f25 843 /* Determine if a long branch stub is needed. */
252b5132 844 if (!(((int)(location - destination) > 0x3ffff)
31bd8f25 845 || ((int)(location - destination) < (int)0xfffc0000)))
252b5132
RH
846 return 0;
847
31bd8f25 848 if (!strncmp ("$$", sym_name, 2)
252b5132
RH
849 && strcmp ("$$dyncall", sym_name))
850 return 12;
851 else
852 return 16;
853}
854
855/* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
856 IN_ARGS contains the stub BFD and link info pointers. */
857
858static boolean
859elf32_hppa_build_one_stub (gen_entry, in_args)
860 struct bfd_hash_entry *gen_entry;
861 PTR in_args;
862{
863 void **args = (void **)in_args;
864 bfd *stub_bfd = (bfd *)args[0];
865 struct bfd_link_info *info = (struct bfd_link_info *)args[1];
866 struct elf32_hppa_stub_hash_entry *entry;
867 struct elf32_hppa_stub_hash_table *stub_hash_table;
868 bfd_byte *loc;
869 symvalue sym_value;
870 const char *sym_name;
871
872 /* Initialize pointers to the stub hash table, the particular entry we
873 are building a stub for, and where (in memory) we should place the stub
874 instructions. */
875 entry = (struct elf32_hppa_stub_hash_entry *)gen_entry;
876 stub_hash_table = elf32_hppa_hash_table(info)->stub_hash_table;
877 loc = stub_hash_table->location;
878
879 /* Make a note of the offset within the stubs for this entry. */
880 entry->offset = stub_hash_table->offset;
881
882 /* The symbol's name starts at offset 22. */
883 sym_name = entry->root.string + 22;
884
885 sym_value = (entry->target_value
886 + entry->target_section->output_offset
887 + entry->target_section->output_section->vma);
888
31bd8f25 889 if (1)
252b5132
RH
890 {
891 /* Create one of two variant long branch stubs. One for $$dyncall and
892 normal calls, the other for calls to millicode. */
893 unsigned long insn;
894 int millicode_call = 0;
895
896 if (!strncmp ("$$", sym_name, 2) && strcmp ("$$dyncall", sym_name))
897 millicode_call = 1;
898
899 /* First the return pointer adjustment. Depending on exact calling
900 sequence this instruction may be skipped. */
901 bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
902
903 /* The next two instructions are the long branch itself. A long branch
904 is formed with "ldil" loading the upper bits of the target address
905 into a register, then branching with "be" which adds in the lower bits.
906 Long branches to millicode nullify the delay slot of the "be". */
907 insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
908 hppa_field_adjust (sym_value, 0, e_lrsel), 21);
909 bfd_put_32 (stub_bfd, insn, loc + 4);
910 insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1 | (millicode_call ? 2 : 0),
911 hppa_field_adjust (sym_value, 0, e_rrsel) >> 2,
912 17);
913 bfd_put_32 (stub_bfd, insn, loc + 8);
914
915 if (!millicode_call)
916 {
917 /* The sequence to call this stub places the return pointer into %r31,
918 the final target expects the return pointer in %r2, so copy the
919 return pointer into the proper register. */
920 bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 12);
921
922 /* Update the location and offsets. */
923 stub_hash_table->location += 16;
924 stub_hash_table->offset += 16;
925 }
926 else
927 {
928 /* Update the location and offsets. */
929 stub_hash_table->location += 12;
930 stub_hash_table->offset += 12;
931 }
932
933 }
934 return true;
935}
936
937/* External entry points for sizing and building linker stubs. */
938
939/* Build all the stubs associated with the current output file. The
940 stubs are kept in a hash table attached to the main linker hash
941 table. This is called via hppaelf_finish in the linker. */
942
943boolean
944elf32_hppa_build_stubs (stub_bfd, info)
945 bfd *stub_bfd;
946 struct bfd_link_info *info;
947{
948 /* The stub BFD only has one section. */
949 asection *stub_sec = stub_bfd->sections;
950 struct elf32_hppa_stub_hash_table *table;
951 unsigned int size;
952 void *args[2];
953
954 /* So we can pass both the BFD for the stubs and the link info
955 structure to the routine which actually builds stubs. */
956 args[0] = stub_bfd;
957 args[1] = info;
958
959 /* Allocate memory to hold the linker stubs. */
960 size = bfd_section_size (stub_bfd, stub_sec);
961 stub_sec->contents = (unsigned char *) bfd_zalloc (stub_bfd, size);
962 if (stub_sec->contents == NULL)
963 return false;
964 table = elf32_hppa_hash_table(info)->stub_hash_table;
965 table->location = stub_sec->contents;
966
967 /* Build the stubs as directed by the stub hash table. */
968 elf32_hppa_stub_hash_traverse (table, elf32_hppa_build_one_stub, args);
969
970 return true;
971}
972
973/* Determine and set the size of the stub section for a final link.
974
975 The basic idea here is to examine all the relocations looking for
976 PC-relative calls to a target that is unreachable with a "bl"
977 instruction or calls where the caller and callee disagree on the
978 location of their arguments or return value. */
979
980boolean
981elf32_hppa_size_stubs (stub_bfd, output_bfd, link_info)
982 bfd *stub_bfd;
983 bfd *output_bfd;
984 struct bfd_link_info *link_info;
985{
986 bfd *input_bfd;
987 asection *section, *stub_sec = 0;
988 Elf_Internal_Shdr *symtab_hdr;
e049a0de 989 Elf_Internal_Sym *local_syms, **all_local_syms;
252b5132
RH
990 unsigned int i, index, bfd_count = 0;
991 struct elf32_hppa_stub_hash_table *stub_hash_table = 0;
252b5132
RH
992
993 /* Create and initialize the stub hash table. */
994 stub_hash_table = ((struct elf32_hppa_stub_hash_table *)
995 bfd_malloc (sizeof (struct elf32_hppa_stub_hash_table)));
996 if (!stub_hash_table)
997 goto error_return;
998
999 if (!elf32_hppa_stub_hash_table_init (stub_hash_table, stub_bfd,
1000 elf32_hppa_stub_hash_newfunc))
1001 goto error_return;
1002
252b5132
RH
1003 /* Attach the hash tables to the main hash table. */
1004 elf32_hppa_hash_table(link_info)->stub_hash_table = stub_hash_table;
252b5132
RH
1005
1006 /* Count the number of input BFDs. */
1007 for (input_bfd = link_info->input_bfds;
1008 input_bfd != NULL;
1009 input_bfd = input_bfd->link_next)
1010 bfd_count++;
1011
252b5132
RH
1012 /* Magic as we know the stub bfd only has one section. */
1013 stub_sec = stub_bfd->sections;
1014
1015 /* If generating a relocateable output file, then we don't
1016 have to examine the relocs. */
1017 if (link_info->relocateable)
1018 {
1019 for (i = 0; i < bfd_count; i++)
1020 if (all_local_syms[i])
1021 free (all_local_syms[i]);
1022 free (all_local_syms);
1023 return true;
1024 }
1025
1026 /* Now that we have argument location information for all the global
1027 functions we can start looking for stubs. */
1028 for (input_bfd = link_info->input_bfds, index = 0;
1029 input_bfd != NULL;
1030 input_bfd = input_bfd->link_next, index++)
1031 {
1032 /* We'll need the symbol table in a second. */
1033 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1034 if (symtab_hdr->sh_info == 0)
1035 continue;
1036
1037 local_syms = all_local_syms[index];
1038
1039 /* Walk over each section attached to the input bfd. */
1040 for (section = input_bfd->sections;
1041 section != NULL;
1042 section = section->next)
1043 {
1044 Elf_Internal_Shdr *input_rel_hdr;
1045 Elf32_External_Rela *external_relocs, *erelaend, *erela;
1046 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1047
1048 /* If there aren't any relocs, then there's nothing to do. */
1049 if ((section->flags & SEC_RELOC) == 0
1050 || section->reloc_count == 0)
1051 continue;
1052
1053 /* Allocate space for the external relocations. */
1054 external_relocs
1055 = ((Elf32_External_Rela *)
1056 bfd_malloc (section->reloc_count
1057 * sizeof (Elf32_External_Rela)));
1058 if (external_relocs == NULL)
1059 {
1060 for (i = 0; i < bfd_count; i++)
1061 if (all_local_syms[i])
1062 free (all_local_syms[i]);
1063 free (all_local_syms);
1064 goto error_return;
1065 }
1066
1067 /* Likewise for the internal relocations. */
1068 internal_relocs
1069 = ((Elf_Internal_Rela *)
1070 bfd_malloc (section->reloc_count * sizeof (Elf_Internal_Rela)));
1071 if (internal_relocs == NULL)
1072 {
1073 free (external_relocs);
1074 for (i = 0; i < bfd_count; i++)
1075 if (all_local_syms[i])
1076 free (all_local_syms[i]);
1077 free (all_local_syms);
1078 goto error_return;
1079 }
1080
1081 /* Read in the external relocs. */
1082 input_rel_hdr = &elf_section_data (section)->rel_hdr;
1083 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
1084 || bfd_read (external_relocs, 1, input_rel_hdr->sh_size,
1085 input_bfd) != input_rel_hdr->sh_size)
1086 {
1087 free (external_relocs);
1088 free (internal_relocs);
1089 for (i = 0; i < bfd_count; i++)
1090 if (all_local_syms[i])
1091 free (all_local_syms[i]);
1092 free (all_local_syms);
1093 goto error_return;
1094 }
1095
1096 /* Swap in the relocs. */
1097 erela = external_relocs;
1098 erelaend = erela + section->reloc_count;
1099 irela = internal_relocs;
1100 for (; erela < erelaend; erela++, irela++)
1101 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
1102
1103 /* We're done with the external relocs, free them. */
1104 free (external_relocs);
1105
1106 /* Now examine each relocation. */
1107 irela = internal_relocs;
1108 irelaend = irela + section->reloc_count;
1109 for (; irela < irelaend; irela++)
1110 {
31bd8f25 1111 long r_type, size_of_stub;
252b5132
RH
1112 unsigned long r_index;
1113 struct elf_link_hash_entry *hash;
1114 struct elf32_hppa_stub_hash_entry *stub_hash;
252b5132
RH
1115 Elf_Internal_Sym *sym;
1116 asection *sym_sec;
1117 const char *sym_name;
1118 symvalue sym_value;
1119 bfd_vma location, destination;
1120 char *new_name = NULL;
1121
1122 r_type = ELF32_R_TYPE (irela->r_info);
1123 r_index = ELF32_R_SYM (irela->r_info);
1124
1125 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
1126 {
1127 bfd_set_error (bfd_error_bad_value);
1128 free (internal_relocs);
1129 for (i = 0; i < bfd_count; i++)
1130 if (all_local_syms[i])
1131 free (all_local_syms[i]);
1132 free (all_local_syms);
1133 goto error_return;
1134 }
1135
1136 /* Only look for stubs on call instructions or plabel
1137 references. */
1138 if (r_type != R_PARISC_PCREL17F
1139 && r_type != R_PARISC_PLABEL32
1140 && r_type != R_PARISC_PLABEL21L
1141 && r_type != R_PARISC_PLABEL14R)
1142 continue;
1143
1144 /* Now determine the call target, its name, value, section
1145 and argument relocation bits. */
1146 hash = NULL;
1147 sym = NULL;
1148 sym_sec = NULL;
1149 if (r_index < symtab_hdr->sh_info)
1150 {
1151 /* It's a local symbol. */
1152 Elf_Internal_Shdr *hdr;
1153
1154 sym = local_syms + r_index;
1155 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
1156 sym_sec = hdr->bfd_section;
1157 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1158 symtab_hdr->sh_link,
1159 sym->st_name);
1160 sym_value = (ELF_ST_TYPE (sym->st_info) == STT_SECTION
1161 ? 0 : sym->st_value);
1162 destination = (sym_value
1163 + sym_sec->output_offset
1164 + sym_sec->output_section->vma);
1165
1166 /* Tack on an ID so we can uniquely identify this local
1167 symbol in the stub or arg info hash tables. */
1168 new_name = bfd_malloc (strlen (sym_name) + 10);
1169 if (new_name == 0)
1170 {
1171 free (internal_relocs);
1172 for (i = 0; i < bfd_count; i++)
1173 if (all_local_syms[i])
1174 free (all_local_syms[i]);
1175 free (all_local_syms);
1176 goto error_return;
1177 }
1178 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1179 sym_name = new_name;
1180 }
1181 else
1182 {
1183 /* It's an external symbol. */
1184 long index;
1185
1186 index = r_index - symtab_hdr->sh_info;
1187 hash = elf_sym_hashes (input_bfd)[index];
1188 if (hash->root.type == bfd_link_hash_defined
1189 || hash->root.type == bfd_link_hash_defweak)
1190 {
1191 sym_sec = hash->root.u.def.section;
1192 sym_name = hash->root.root.string;
1193 sym_value = hash->root.u.def.value;
1194 destination = (sym_value
1195 + sym_sec->output_offset
1196 + sym_sec->output_section->vma);
1197 }
1198 else
1199 {
1200 bfd_set_error (bfd_error_bad_value);
1201 free (internal_relocs);
1202 for (i = 0; i < bfd_count; i++)
1203 if (all_local_syms[i])
1204 free (all_local_syms[i]);
1205 free (all_local_syms);
1206 goto error_return;
1207 }
1208 }
1209
252b5132
RH
1210 /* Now determine where the call point is. */
1211 location = (section->output_offset
1212 + section->output_section->vma
1213 + irela->r_offset);
1214
1215 /* We only care about the destination for PCREL function
1216 calls (eg. we don't care for PLABELS). */
1217 if (r_type != R_PARISC_PCREL17F)
1218 location = destination;
1219
1220 /* Determine what (if any) linker stub is needed and its
1221 size (in bytes). */
31bd8f25 1222 size_of_stub = elf32_hppa_size_of_stub (location,
252b5132
RH
1223 destination,
1224 sym_name);
1225 if (size_of_stub != 0)
1226 {
1227 char *stub_name;
1228 unsigned int len;
1229
1230 /* Get the name of this stub. */
1231 len = strlen (sym_name);
1232 len += 23;
1233
1234 stub_name = bfd_malloc (len);
1235 if (!stub_name)
1236 {
1237 /* Because sym_name was mallocd above for local
1238 symbols. */
1239 if (r_index < symtab_hdr->sh_info)
1240 free (new_name);
1241
1242 free (internal_relocs);
1243 for (i = 0; i < bfd_count; i++)
1244 if (all_local_syms[i])
1245 free (all_local_syms[i]);
1246 free (all_local_syms);
1247 goto error_return;
1248 }
31bd8f25 1249 elf32_hppa_name_of_stub (location, destination, stub_name);
252b5132
RH
1250 strcat (stub_name + 22, sym_name);
1251
1252 /* Because sym_name was malloced above for local symbols. */
1253 if (r_index < symtab_hdr->sh_info)
1254 free (new_name);
1255
1256 stub_hash
1257 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
1258 false, false);
1259 if (stub_hash != NULL)
1260 {
1261 /* The proper stub has already been created, nothing
1262 else to do. */
1263 free (stub_name);
1264 }
1265 else
1266 {
1267 bfd_set_section_size (stub_bfd, stub_sec,
1268 (bfd_section_size (stub_bfd,
1269 stub_sec)
1270 + size_of_stub));
1271
1272 /* Enter this entry into the linker stub hash table. */
1273 stub_hash
1274 = elf32_hppa_stub_hash_lookup (stub_hash_table,
1275 stub_name, true, true);
1276 if (stub_hash == NULL)
1277 {
1278 free (stub_name);
1279 free (internal_relocs);
1280 for (i = 0; i < bfd_count; i++)
1281 if (all_local_syms[i])
1282 free (all_local_syms[i]);
1283 free (all_local_syms);
1284 goto error_return;
1285 }
1286
1287 /* We'll need these to determine the address that the
1288 stub will branch to. */
1289 stub_hash->target_value = sym_value;
1290 stub_hash->target_section = sym_sec;
1291 }
1292 free (stub_name);
1293 }
1294 }
1295 /* We're done with the internal relocs, free them. */
1296 free (internal_relocs);
1297 }
1298 }
1299 /* We're done with the local symbols, free them. */
1300 for (i = 0; i < bfd_count; i++)
1301 if (all_local_syms[i])
1302 free (all_local_syms[i]);
1303 free (all_local_syms);
1304 return true;
1305
1306error_return:
1307 /* Return gracefully, avoiding dangling references to the hash tables. */
1308 if (stub_hash_table)
1309 {
1310 elf32_hppa_hash_table(link_info)->stub_hash_table = NULL;
1311 free (stub_hash_table);
1312 }
252b5132
RH
1313 /* Set the size of the stub section to zero since we're never going
1314 to create them. Avoids losing when we try to get its contents
1315 too. */
1316 bfd_set_section_size (stub_bfd, stub_sec, 0);
1317 return false;
1318}
1319
1320/* Misc BFD support code. */
1321#define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
1322#define bfd_elf32_bfd_is_local_label_name hppa_elf_is_local_label_name
9e103c9c
JL
1323#define elf_info_to_howto elf_hppa_info_to_howto
1324#define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
252b5132 1325
252b5132
RH
1326/* Stuff for the BFD linker. */
1327#define elf_backend_relocate_section elf32_hppa_relocate_section
1328#define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
252b5132
RH
1329#define bfd_elf32_bfd_link_hash_table_create \
1330 elf32_hppa_link_hash_table_create
1331
1332#define TARGET_BIG_SYM bfd_elf32_hppa_vec
1333#define TARGET_BIG_NAME "elf32-hppa"
1334#define ELF_ARCH bfd_arch_hppa
1335#define ELF_MACHINE_CODE EM_PARISC
1336#define ELF_MAXPAGESIZE 0x1000
1337
1338#include "elf32-target.h"