* elf-hppa.h: New file. Common stuff for elf32 and elf64 PA
[deliverable/binutils-gdb.git] / bfd / elf32-hppa.c
1 /* BFD back-end for HP PA-RISC ELF files.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 1997
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
11 This file is part of BFD, the Binary File Descriptor library.
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libbfd.h"
30 #include "elf-bfd.h"
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
37
38 /* The internal type of a symbol table extension entry. */
39 typedef unsigned long symext_entryS;
40
41 /* The external type of a symbol table extension entry. */
42 #define ELF32_PARISC_SX_SIZE (4)
43 #define ELF32_PARISC_SX_GET(bfd, addr) bfd_h_get_32 ((bfd), (addr))
44 #define ELF32_PARISC_SX_PUT(bfd, val, addr) \
45 bfd_h_put_32 ((bfd), (val), (addr))
46
47 /* HPPA symbol table extension entry types */
48 enum elf32_hppa_symextn_types
49 {
50 PARISC_SXT_NULL,
51 PARISC_SXT_SYMNDX,
52 PARISC_SXT_ARG_RELOC,
53 };
54
55 /* These macros compose and decompose the value of a symextn entry:
56
57 entry_type = ELF32_PARISC_SX_TYPE(word);
58 entry_value = ELF32_PARISC_SX_VAL(word);
59 word = ELF32_PARISC_SX_WORD(type,val); */
60
61 #define ELF32_PARISC_SX_TYPE(p) ((p) >> 24)
62 #define ELF32_PARISC_SX_VAL(p) ((p) & 0xFFFFFF)
63 #define ELF32_PARISC_SX_WORD(type,val) (((type) << 24) + (val & 0xFFFFFF))
64
65 /* The following was added facilitate implementation of the .hppa_symextn
66 section. This section is built after the symbol table is built in the
67 elf_write_object_contents routine (called from bfd_close). It is built
68 so late because it requires information that is not known until
69 the symbol and string table sections have been allocated, and
70 the symbol table has been built. */
71
72 #define SYMEXTN_SECTION_NAME ".PARISC.symext"
73
74 struct symext_chain
75 {
76 symext_entryS entry;
77 struct symext_chain *next;
78 };
79
80 typedef struct symext_chain symext_chainS;
81
82 /* We use three different hash tables to hold information for
83 linking PA ELF objects.
84
85 The first is the elf32_hppa_link_hash_table which is derived
86 from the standard ELF linker hash table. We use this as a place to
87 attach other hash tables and static information.
88
89 The second is the stub hash table which is derived from the
90 base BFD hash table. The stub hash table holds the information
91 necessary to build the linker stubs during a link.
92
93 The last hash table keeps track of argument location information needed
94 to build hash tables. Each function with nonzero argument location
95 bits will have an entry in this table. */
96
97 /* Hash table for linker stubs. */
98
99 struct elf32_hppa_stub_hash_entry
100 {
101 /* Base hash table entry structure, we can get the name of the stub
102 (and thus know exactly what actions it performs) from the base
103 hash table entry. */
104 struct bfd_hash_entry root;
105
106 /* Offset of the beginning of this stub. */
107 bfd_vma offset;
108
109 /* Given the symbol's value and its section we can determine its final
110 value when building the stubs (so the stub knows where to jump. */
111 symvalue target_value;
112 asection *target_section;
113 };
114
115 struct elf32_hppa_stub_hash_table
116 {
117 /* The hash table itself. */
118 struct bfd_hash_table root;
119
120 /* The stub BFD. */
121 bfd *stub_bfd;
122
123 /* Where to place the next stub. */
124 bfd_byte *location;
125
126 /* Current offset in the stub section. */
127 unsigned int offset;
128
129 };
130
131 /* Hash table for argument location information. */
132
133 struct elf32_hppa_args_hash_entry
134 {
135 /* Base hash table entry structure. */
136 struct bfd_hash_entry root;
137
138 /* The argument location bits for this entry. */
139 int arg_bits;
140 };
141
142 struct elf32_hppa_args_hash_table
143 {
144 /* The hash table itself. */
145 struct bfd_hash_table root;
146 };
147
148 struct elf32_hppa_link_hash_entry
149 {
150 struct elf_link_hash_entry root;
151 };
152
153 struct elf32_hppa_link_hash_table
154 {
155 /* The main hash table. */
156 struct elf_link_hash_table root;
157
158 /* The stub hash table. */
159 struct elf32_hppa_stub_hash_table *stub_hash_table;
160
161 /* The argument relocation bits hash table. */
162 struct elf32_hppa_args_hash_table *args_hash_table;
163
164 /* A count of the number of output symbols. */
165 unsigned int output_symbol_count;
166
167 /* Stuff so we can handle DP relative relocations. */
168 long global_value;
169 int global_sym_defined;
170 };
171
172 /* FIXME. */
173 #define ARGUMENTS 0
174 #define RETURN_VALUE 1
175
176 /* The various argument relocations that may be performed. */
177 typedef enum
178 {
179 /* No relocation. */
180 NO,
181 /* Relocate 32 bits from GR to FP register. */
182 GF,
183 /* Relocate 64 bits from a GR pair to FP pair. */
184 GD,
185 /* Relocate 32 bits from FP to GR. */
186 FG,
187 /* Relocate 64 bits from FP pair to GR pair. */
188 DG,
189 } arg_reloc_type;
190
191 /* What is being relocated (eg which argument or the return value). */
192 typedef enum
193 {
194 ARG0, ARG1, ARG2, ARG3, RET,
195 } arg_reloc_location;
196
197
198 /* ELF32/HPPA relocation support
199
200 This file contains ELF32/HPPA relocation support as specified
201 in the Stratus FTX/Golf Object File Format (SED-1762) dated
202 February 1994. */
203
204 #include "elf32-hppa.h"
205 #include "hppa_stubs.h"
206
207 static bfd_reloc_status_type hppa_elf_reloc
208 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
209
210 static unsigned long hppa_elf_relocate_insn
211 PARAMS ((bfd *, asection *, unsigned long, unsigned long, long,
212 long, unsigned long, unsigned long, unsigned long));
213
214 static bfd_reloc_status_type hppa_elf_reloc
215 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd*, char **));
216
217 static boolean elf32_hppa_set_section_contents
218 PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
219
220 static boolean elf32_hppa_backend_symbol_table_processing
221 PARAMS ((bfd *, elf_symbol_type *, unsigned int));
222
223 static void elf32_hppa_backend_begin_write_processing
224 PARAMS ((bfd *, struct bfd_link_info *));
225
226 static void elf32_hppa_backend_final_write_processing
227 PARAMS ((bfd *, boolean));
228
229 static void add_entry_to_symext_chain
230 PARAMS ((bfd *, unsigned int, unsigned int, symext_chainS **,
231 symext_chainS **));
232
233 static void
234 elf_hppa_tc_make_sections PARAMS ((bfd *, symext_chainS *));
235
236 static boolean hppa_elf_is_local_label_name PARAMS ((bfd *, const char *));
237
238 static boolean elf32_hppa_add_symbol_hook
239 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
240 const char **, flagword *, asection **, bfd_vma *));
241
242 static bfd_reloc_status_type elf32_hppa_bfd_final_link_relocate
243 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *,
244 bfd_byte *, bfd_vma, bfd_vma, bfd_vma, struct bfd_link_info *,
245 asection *, const char *, int));
246
247 static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
248 PARAMS ((bfd *));
249
250 static struct bfd_hash_entry *
251 elf32_hppa_stub_hash_newfunc
252 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
253
254 static struct bfd_hash_entry *
255 elf32_hppa_args_hash_newfunc
256 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
257
258 static boolean
259 elf32_hppa_relocate_section
260 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
261 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
262
263 static boolean
264 elf32_hppa_stub_hash_table_init
265 PARAMS ((struct elf32_hppa_stub_hash_table *, bfd *,
266 struct bfd_hash_entry *(*) PARAMS ((struct bfd_hash_entry *,
267 struct bfd_hash_table *,
268 const char *))));
269
270 static boolean
271 elf32_hppa_build_one_stub PARAMS ((struct bfd_hash_entry *, PTR));
272
273 static boolean
274 elf32_hppa_read_symext_info
275 PARAMS ((bfd *, Elf_Internal_Shdr *, struct elf32_hppa_args_hash_table *,
276 Elf_Internal_Sym *));
277
278 static unsigned int elf32_hppa_size_of_stub
279 PARAMS ((unsigned int, unsigned int, bfd_vma, bfd_vma, const char *));
280
281 static boolean elf32_hppa_arg_reloc_needed
282 PARAMS ((unsigned int, unsigned int, arg_reloc_type []));
283
284 static void elf32_hppa_name_of_stub
285 PARAMS ((unsigned int, unsigned int, bfd_vma, bfd_vma, char *));
286
287 static boolean elf32_hppa_size_symext PARAMS ((struct bfd_hash_entry *, PTR));
288
289 static boolean elf32_hppa_link_output_symbol_hook
290 PARAMS ((bfd *, struct bfd_link_info *, const char *,
291 Elf_Internal_Sym *, asection *));
292
293 /* Where (what register type) is an argument comming from? */
294 typedef enum
295 {
296 AR_NO,
297 AR_GR,
298 AR_FR,
299 AR_FU,
300 AR_FPDBL1,
301 AR_FPDBL2,
302 } arg_location;
303
304 /* Horizontal represents the callee's argument location information,
305 vertical represents caller's argument location information. Value at a
306 particular X,Y location represents what (if any) argument relocation
307 needs to be performed to make caller and callee agree. */
308
309 static CONST arg_reloc_type arg_mismatches[6][6] =
310 {
311 {NO, NO, NO, NO, NO, NO},
312 {NO, NO, GF, NO, GD, NO},
313 {NO, FG, NO, NO, NO, NO},
314 {NO, NO, NO, NO, NO, NO},
315 {NO, DG, NO, NO, NO, NO},
316 {NO, DG, NO, NO, NO, NO},
317 };
318
319 /* Likewise, but reversed for the return value. */
320 static CONST arg_reloc_type ret_mismatches[6][6] =
321 {
322 {NO, NO, NO, NO, NO, NO},
323 {NO, NO, FG, NO, DG, NO},
324 {NO, GF, NO, NO, NO, NO},
325 {NO, NO, NO, NO, NO, NO},
326 {NO, GD, NO, NO, NO, NO},
327 {NO, GD, NO, NO, NO, NO},
328 };
329
330 /* Misc static crud for symbol extension records. */
331 static symext_chainS *symext_rootP;
332 static symext_chainS *symext_lastP;
333 static bfd_size_type symext_chain_size;
334
335 /* FIXME: We should be able to try this static variable! */
336 static bfd_byte *symextn_contents;
337
338
339 /* For linker stub hash tables. */
340 #define elf32_hppa_stub_hash_lookup(table, string, create, copy) \
341 ((struct elf32_hppa_stub_hash_entry *) \
342 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
343
344 #define elf32_hppa_stub_hash_traverse(table, func, info) \
345 (bfd_hash_traverse \
346 (&(table)->root, \
347 (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
348 (info)))
349
350 /* For linker args hash tables. */
351 #define elf32_hppa_args_hash_lookup(table, string, create, copy) \
352 ((struct elf32_hppa_args_hash_entry *) \
353 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
354
355 #define elf32_hppa_args_hash_traverse(table, func, info) \
356 (bfd_hash_traverse \
357 (&(table)->root, \
358 (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
359 (info)))
360
361 #define elf32_hppa_args_hash_table_init(table, newfunc) \
362 (bfd_hash_table_init \
363 (&(table)->root, \
364 (struct bfd_hash_entry *(*) PARAMS ((struct bfd_hash_entry *, \
365 struct bfd_hash_table *, \
366 const char *))) (newfunc)))
367
368 /* For HPPA linker hash table. */
369
370 #define elf32_hppa_link_hash_lookup(table, string, create, copy, follow)\
371 ((struct elf32_hppa_link_hash_entry *) \
372 elf_link_hash_lookup (&(table)->root, (string), (create), \
373 (copy), (follow)))
374
375 #define elf32_hppa_link_hash_traverse(table, func, info) \
376 (elf_link_hash_traverse \
377 (&(table)->root, \
378 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
379 (info)))
380
381 /* Get the PA ELF linker hash table from a link_info structure. */
382
383 #define elf32_hppa_hash_table(p) \
384 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
385
386
387 /* Extract specific argument location bits for WHICH from
388 the full argument location in AR. */
389 #define EXTRACT_ARBITS(ar, which) ((ar) >> (8 - ((which) * 2))) & 3
390
391 /* Assorted hash table functions. */
392
393 /* Initialize an entry in the stub hash table. */
394
395 static struct bfd_hash_entry *
396 elf32_hppa_stub_hash_newfunc (entry, table, string)
397 struct bfd_hash_entry *entry;
398 struct bfd_hash_table *table;
399 const char *string;
400 {
401 struct elf32_hppa_stub_hash_entry *ret;
402
403 ret = (struct elf32_hppa_stub_hash_entry *) entry;
404
405 /* Allocate the structure if it has not already been allocated by a
406 subclass. */
407 if (ret == NULL)
408 ret = ((struct elf32_hppa_stub_hash_entry *)
409 bfd_hash_allocate (table,
410 sizeof (struct elf32_hppa_stub_hash_entry)));
411 if (ret == NULL)
412 return NULL;
413
414 /* Call the allocation method of the superclass. */
415 ret = ((struct elf32_hppa_stub_hash_entry *)
416 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
417
418 if (ret)
419 {
420 /* Initialize the local fields. */
421 ret->offset = 0;
422 ret->target_value = 0;
423 ret->target_section = NULL;
424 }
425
426 return (struct bfd_hash_entry *) ret;
427 }
428
429 /* Initialize a stub hash table. */
430
431 static boolean
432 elf32_hppa_stub_hash_table_init (table, stub_bfd, newfunc)
433 struct elf32_hppa_stub_hash_table *table;
434 bfd *stub_bfd;
435 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
436 struct bfd_hash_table *,
437 const char *));
438 {
439 table->offset = 0;
440 table->location = 0;
441 table->stub_bfd = stub_bfd;
442 return (bfd_hash_table_init (&table->root, newfunc));
443 }
444
445 /* Initialize an entry in the argument location hash table. */
446
447 static struct bfd_hash_entry *
448 elf32_hppa_args_hash_newfunc (entry, table, string)
449 struct bfd_hash_entry *entry;
450 struct bfd_hash_table *table;
451 const char *string;
452 {
453 struct elf32_hppa_args_hash_entry *ret;
454
455 ret = (struct elf32_hppa_args_hash_entry *) entry;
456
457 /* Allocate the structure if it has not already been allocated by a
458 subclass. */
459 if (ret == NULL)
460 ret = ((struct elf32_hppa_args_hash_entry *)
461 bfd_hash_allocate (table,
462 sizeof (struct elf32_hppa_args_hash_entry)));
463 if (ret == NULL)
464 return NULL;
465
466 /* Call the allocation method of the superclass. */
467 ret = ((struct elf32_hppa_args_hash_entry *)
468 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
469
470 /* Initialize the local fields. */
471 if (ret)
472 ret->arg_bits = 0;
473
474 return (struct bfd_hash_entry *) ret;
475 }
476
477 /* Create the derived linker hash table. The PA ELF port uses the derived
478 hash table to keep information specific to the PA ELF linker (without
479 using static variables). */
480
481 static struct bfd_link_hash_table *
482 elf32_hppa_link_hash_table_create (abfd)
483 bfd *abfd;
484 {
485 struct elf32_hppa_link_hash_table *ret;
486
487 ret = ((struct elf32_hppa_link_hash_table *)
488 bfd_alloc (abfd, sizeof (struct elf32_hppa_link_hash_table)));
489 if (ret == NULL)
490 return NULL;
491 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
492 _bfd_elf_link_hash_newfunc))
493 {
494 bfd_release (abfd, ret);
495 return NULL;
496 }
497 ret->stub_hash_table = NULL;
498 ret->args_hash_table = NULL;
499 ret->output_symbol_count = 0;
500 ret->global_value = 0;
501 ret->global_sym_defined = 0;
502
503 return &ret->root.root;
504 }
505
506 /* Relocate the given INSN given the various input parameters.
507
508 FIXME: endianness and sizeof (long) issues abound here. */
509
510 static unsigned long
511 hppa_elf_relocate_insn (abfd, input_sect, insn, address, sym_value,
512 r_addend, r_format, r_field, pcrel)
513 bfd *abfd;
514 asection *input_sect;
515 unsigned long insn;
516 unsigned long address;
517 long sym_value;
518 long r_addend;
519 unsigned long r_format;
520 unsigned long r_field;
521 unsigned long pcrel;
522 {
523 unsigned char opcode = get_opcode (insn);
524 long constant_value;
525
526 switch (opcode)
527 {
528 case LDO:
529 case LDB:
530 case LDH:
531 case LDW:
532 case LDWM:
533 case STB:
534 case STH:
535 case STW:
536 case STWM:
537 case COMICLR:
538 case SUBI:
539 case ADDIT:
540 case ADDI:
541 case LDIL:
542 case ADDIL:
543 constant_value = HPPA_R_CONSTANT (r_addend);
544
545 if (pcrel)
546 sym_value -= address;
547
548 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
549 return hppa_rebuild_insn (abfd, insn, sym_value, r_format);
550
551 case BL:
552 case BE:
553 case BLE:
554 /* XXX computing constant_value is not needed??? */
555 constant_value = assemble_17 ((insn & 0x001f0000) >> 16,
556 (insn & 0x00001ffc) >> 2,
557 insn & 1);
558
559 constant_value = (constant_value << 15) >> 15;
560 if (pcrel)
561 {
562 sym_value -=
563 address + input_sect->output_offset
564 + input_sect->output_section->vma;
565 sym_value = hppa_field_adjust (sym_value, -8, r_field);
566 }
567 else
568 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
569
570 return hppa_rebuild_insn (abfd, insn, sym_value >> 2, r_format);
571
572 default:
573 if (opcode == 0)
574 {
575 constant_value = HPPA_R_CONSTANT (r_addend);
576
577 if (pcrel)
578 sym_value -= address;
579
580 return hppa_field_adjust (sym_value, constant_value, r_field);
581 }
582 else
583 abort ();
584 }
585 }
586
587 /* Relocate an HPPA ELF section. */
588
589 static boolean
590 elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
591 contents, relocs, local_syms, local_sections)
592 bfd *output_bfd;
593 struct bfd_link_info *info;
594 bfd *input_bfd;
595 asection *input_section;
596 bfd_byte *contents;
597 Elf_Internal_Rela *relocs;
598 Elf_Internal_Sym *local_syms;
599 asection **local_sections;
600 {
601 Elf_Internal_Shdr *symtab_hdr;
602 Elf_Internal_Rela *rel;
603 Elf_Internal_Rela *relend;
604
605 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
606
607 rel = relocs;
608 relend = relocs + input_section->reloc_count;
609 for (; rel < relend; rel++)
610 {
611 int r_type;
612 reloc_howto_type *howto;
613 unsigned long r_symndx;
614 struct elf_link_hash_entry *h;
615 Elf_Internal_Sym *sym;
616 asection *sym_sec;
617 bfd_vma relocation;
618 bfd_reloc_status_type r;
619 const char *sym_name;
620
621 r_type = ELF32_R_TYPE (rel->r_info);
622 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
623 {
624 bfd_set_error (bfd_error_bad_value);
625 return false;
626 }
627 howto = elf_hppa_howto_table + r_type;
628
629 r_symndx = ELF32_R_SYM (rel->r_info);
630
631 if (info->relocateable)
632 {
633 /* This is a relocateable link. We don't have to change
634 anything, unless the reloc is against a section symbol,
635 in which case we have to adjust according to where the
636 section symbol winds up in the output section. */
637 if (r_symndx < symtab_hdr->sh_info)
638 {
639 sym = local_syms + r_symndx;
640 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
641 {
642 sym_sec = local_sections[r_symndx];
643 rel->r_addend += sym_sec->output_offset;
644 }
645 }
646
647 continue;
648 }
649
650 /* This is a final link. */
651 h = NULL;
652 sym = NULL;
653 sym_sec = NULL;
654 if (r_symndx < symtab_hdr->sh_info)
655 {
656 sym = local_syms + r_symndx;
657 sym_sec = local_sections[r_symndx];
658 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
659 ? 0 : sym->st_value)
660 + sym_sec->output_offset
661 + sym_sec->output_section->vma);
662 }
663 else
664 {
665 long indx;
666
667 indx = r_symndx - symtab_hdr->sh_info;
668 h = elf_sym_hashes (input_bfd)[indx];
669 while (h->root.type == bfd_link_hash_indirect
670 || h->root.type == bfd_link_hash_warning)
671 h = (struct elf_link_hash_entry *) h->root.u.i.link;
672 if (h->root.type == bfd_link_hash_defined
673 || h->root.type == bfd_link_hash_defweak)
674 {
675 sym_sec = h->root.u.def.section;
676 relocation = (h->root.u.def.value
677 + sym_sec->output_offset
678 + sym_sec->output_section->vma);
679 }
680 else if (h->root.type == bfd_link_hash_undefweak)
681 relocation = 0;
682 else
683 {
684 if (!((*info->callbacks->undefined_symbol)
685 (info, h->root.root.string, input_bfd,
686 input_section, rel->r_offset)))
687 return false;
688 break;
689 }
690 }
691
692 if (h != NULL)
693 sym_name = h->root.root.string;
694 else
695 {
696 sym_name = bfd_elf_string_from_elf_section (input_bfd,
697 symtab_hdr->sh_link,
698 sym->st_name);
699 if (sym_name == NULL)
700 return false;
701 if (*sym_name == '\0')
702 sym_name = bfd_section_name (input_bfd, sym_sec);
703 }
704
705 /* If args_hash_table is NULL, then we have encountered some
706 kind of link error (ex. undefined symbols). Do not try to
707 apply any relocations, continue the loop so we can notify
708 the user of several errors in a single attempted link. */
709 if (elf32_hppa_hash_table (info)->args_hash_table == NULL)
710 continue;
711
712 r = elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
713 input_section, contents,
714 rel->r_offset, relocation,
715 rel->r_addend, info, sym_sec,
716 sym_name, h == NULL);
717
718 if (r != bfd_reloc_ok)
719 {
720 switch (r)
721 {
722 /* This can happen for DP relative relocs if $global$ is
723 undefined. This is a panic situation so we don't try
724 to continue. */
725 case bfd_reloc_undefined:
726 case bfd_reloc_notsupported:
727 if (!((*info->callbacks->undefined_symbol)
728 (info, "$global$", input_bfd,
729 input_section, rel->r_offset)))
730 return false;
731 return false;
732 case bfd_reloc_dangerous:
733 {
734 /* We use this return value to indicate that we performed
735 a "dangerous" relocation. This doesn't mean we did
736 the wrong thing, it just means there may be some cleanup
737 that needs to be done here.
738
739 In particular we had to swap the last call insn and its
740 delay slot. If the delay slot insn needed a relocation,
741 then we'll need to adjust the next relocation entry's
742 offset to account for the fact that the insn moved.
743
744 This hair wouldn't be necessary if we inserted stubs
745 between procedures and used a "bl" to get to the stub. */
746 if (rel != relend)
747 {
748 Elf_Internal_Rela *next_rel = rel + 1;
749
750 if (rel->r_offset + 4 == next_rel->r_offset)
751 next_rel->r_offset -= 4;
752 }
753 break;
754 }
755 default:
756 case bfd_reloc_outofrange:
757 case bfd_reloc_overflow:
758 {
759 if (!((*info->callbacks->reloc_overflow)
760 (info, sym_name, howto->name, (bfd_vma) 0,
761 input_bfd, input_section, rel->r_offset)))
762 return false;
763 }
764 break;
765 }
766 }
767 }
768
769 return true;
770 }
771
772 /* Set the contents of a particular section at a particular location. */
773
774 static boolean
775 elf32_hppa_set_section_contents (abfd, section, location, offset, count)
776 bfd *abfd;
777 sec_ptr section;
778 PTR location;
779 file_ptr offset;
780 bfd_size_type count;
781 {
782 /* Ignore write requests for the symbol extension section until we've
783 had the chance to rebuild it ourselves. */
784 if (!strcmp (section->name, ".PARISC.symextn") && !symext_chain_size)
785 return true;
786 else
787 return _bfd_elf_set_section_contents (abfd, section, location,
788 offset, count);
789 }
790
791 /* Actually perform a relocation. NOTE this is (mostly) superceeded
792 by elf32_hppa_bfd_final_link_relocate which is called by the new
793 fast linker. */
794
795 static bfd_reloc_status_type
796 hppa_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
797 error_message)
798 bfd *abfd;
799 arelent *reloc_entry;
800 asymbol *symbol_in;
801 PTR data;
802 asection *input_section;
803 bfd *output_bfd;
804 char **error_message;
805 {
806 /* It is no longer valid to call hppa_elf_reloc when creating
807 a final executable. */
808 if (output_bfd)
809 {
810 reloc_entry->address += input_section->output_offset;
811
812 /* Work around lossage in generic elf code to write relocations.
813 (maps different section symbols into the same symbol index). */
814 if ((symbol_in->flags & BSF_SECTION_SYM)
815 && symbol_in->section)
816 reloc_entry->addend += symbol_in->section->output_offset;
817 return bfd_reloc_ok;
818 }
819 else
820 {
821 *error_message = (char *) _("Unsupported call to hppa_elf_reloc");
822 return bfd_reloc_notsupported;
823 }
824 }
825
826 /* Actually perform a relocation as part of a final link. This can get
827 rather hairy when linker stubs are needed. */
828
829 static bfd_reloc_status_type
830 elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
831 input_section, contents, offset, value,
832 addend, info, sym_sec, sym_name, is_local)
833 reloc_howto_type *howto;
834 bfd *input_bfd;
835 bfd *output_bfd;
836 asection *input_section;
837 bfd_byte *contents;
838 bfd_vma offset;
839 bfd_vma value;
840 bfd_vma addend;
841 struct bfd_link_info *info;
842 asection *sym_sec;
843 const char *sym_name;
844 int is_local;
845 {
846 unsigned long insn;
847 unsigned long r_type = howto->type;
848 unsigned long r_format = howto->bitsize;
849 unsigned long r_field = e_fsel;
850 bfd_byte *hit_data = contents + offset;
851 boolean r_pcrel = howto->pc_relative;
852
853 insn = bfd_get_32 (input_bfd, hit_data);
854
855 /* Make sure we have a value for $global$. FIXME isn't this effectively
856 just like the gp pointer on MIPS? Can we use those routines for this
857 purpose? */
858 if (!elf32_hppa_hash_table (info)->global_sym_defined)
859 {
860 struct elf_link_hash_entry *h;
861 asection *sec;
862
863 h = elf_link_hash_lookup (elf_hash_table (info), "$global$", false,
864 false, false);
865
866 /* If there isn't a $global$, then we're in deep trouble. */
867 if (h == NULL)
868 return bfd_reloc_notsupported;
869
870 /* If $global$ isn't a defined symbol, then we're still in deep
871 trouble. */
872 if (h->root.type != bfd_link_hash_defined)
873 return bfd_reloc_undefined;
874
875 sec = h->root.u.def.section;
876 elf32_hppa_hash_table (info)->global_value = (h->root.u.def.value
877 + sec->output_section->vma
878 + sec->output_offset);
879 elf32_hppa_hash_table (info)->global_sym_defined = 1;
880 }
881
882 switch (r_type)
883 {
884 case R_PARISC_NONE:
885 break;
886
887 case R_PARISC_DIR32:
888 case R_PARISC_DIR17F:
889 case R_PARISC_PCREL17C:
890 r_field = e_fsel;
891 goto do_basic_type_1;
892 case R_PARISC_DIR21L:
893 case R_PARISC_PCREL21L:
894 r_field = e_lrsel;
895 goto do_basic_type_1;
896 case R_PARISC_DIR17R:
897 case R_PARISC_PCREL17R:
898 case R_PARISC_DIR14R:
899 case R_PARISC_PCREL14R:
900 r_field = e_rrsel;
901 goto do_basic_type_1;
902
903 /* For all the DP relative relocations, we need to examine the symbol's
904 section. If it's a code section, then "data pointer relative" makes
905 no sense. In that case we don't adjust the "value", and for 21 bit
906 addil instructions, we change the source addend register from %dp to
907 %r0. */
908 case R_PARISC_DPREL21L:
909 r_field = e_lrsel;
910 if (sym_sec->flags & SEC_CODE)
911 {
912 if ((insn & 0xfc000000) >> 26 == 0xa
913 && (insn & 0x03e00000) >> 21 == 0x1b)
914 insn &= ~0x03e00000;
915 }
916 else
917 value -= elf32_hppa_hash_table (info)->global_value;
918 goto do_basic_type_1;
919 case R_PARISC_DPREL14R:
920 r_field = e_rrsel;
921 if ((sym_sec->flags & SEC_CODE) == 0)
922 value -= elf32_hppa_hash_table (info)->global_value;
923 goto do_basic_type_1;
924 case R_PARISC_DPREL14F:
925 r_field = e_fsel;
926 if ((sym_sec->flags & SEC_CODE) == 0)
927 value -= elf32_hppa_hash_table (info)->global_value;
928 goto do_basic_type_1;
929
930 /* These cases are separate as they may involve a lot more work
931 to deal with linker stubs. */
932 case R_PARISC_PLABEL32:
933 case R_PARISC_PLABEL21L:
934 case R_PARISC_PLABEL14R:
935 case R_PARISC_PCREL17F:
936 {
937 bfd_vma location;
938 unsigned int len, caller_args, callee_args;
939 arg_reloc_type arg_reloc_types[5];
940 struct elf32_hppa_args_hash_table *args_hash_table;
941 struct elf32_hppa_args_hash_entry *args_hash;
942 char *new_name, *stub_name;
943
944 /* Get the field selector right. We'll need it in a minute. */
945 if (r_type == R_PARISC_PCREL17F
946 || r_type == R_PARISC_PLABEL32)
947 r_field = e_fsel;
948 else if (r_type == R_PARISC_PLABEL21L)
949 r_field = e_lrsel;
950 else if (r_type == R_PARISC_PLABEL14R)
951 r_field = e_rrsel;
952
953 /* Find out where we are and where we're going. */
954 location = (offset +
955 input_section->output_offset +
956 input_section->output_section->vma);
957
958 /* Now look for the argument relocation bits associated with the
959 target. */
960 len = strlen (sym_name) + 1;
961 if (is_local)
962 len += 9;
963 new_name = bfd_malloc (len);
964 if (!new_name)
965 return bfd_reloc_notsupported;
966 strcpy (new_name, sym_name);
967
968 /* Local symbols have unique IDs. */
969 if (is_local)
970 sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
971
972 args_hash_table = elf32_hppa_hash_table (info)->args_hash_table;
973
974 args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
975 new_name, false, false);
976 if (args_hash == NULL)
977 callee_args = 0;
978 else
979 callee_args = args_hash->arg_bits;
980
981 /* If this is a CALL relocation, then get the caller's bits
982 from the addend. Else use the magic 0x155 value for PLABELS.
983
984 Also we don't care about the destination (value) for PLABELS. */
985 if (r_type == R_PARISC_PCREL17F)
986 caller_args = HPPA_R_ARG_RELOC (addend);
987 else
988 {
989 caller_args = 0x155;
990 location = value;
991 }
992
993 /* Any kind of linker stub needed? */
994 if (((int)(value - location) > 0x3ffff)
995 || ((int)(value - location) < (int)0xfffc0000)
996 || elf32_hppa_arg_reloc_needed (caller_args, callee_args,
997 arg_reloc_types))
998 {
999 struct elf32_hppa_stub_hash_table *stub_hash_table;
1000 struct elf32_hppa_stub_hash_entry *stub_hash;
1001 asection *stub_section;
1002
1003 /* Build a name for the stub. */
1004
1005 len = strlen (new_name);
1006 len += 23;
1007 stub_name = bfd_malloc (len);
1008 if (!stub_name)
1009 return bfd_reloc_notsupported;
1010 elf32_hppa_name_of_stub (caller_args, callee_args,
1011 location, value, stub_name);
1012 strcat (stub_name, new_name);
1013 free (new_name);
1014
1015 stub_hash_table = elf32_hppa_hash_table (info)->stub_hash_table;
1016
1017 stub_hash
1018 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
1019 false, false);
1020
1021 /* We're done with that name. */
1022 free (stub_name);
1023
1024 /* The stub BFD only has one section. */
1025 stub_section = stub_hash_table->stub_bfd->sections;
1026
1027 if (stub_hash != NULL)
1028 {
1029
1030 if (r_type == R_PARISC_PCREL17F)
1031 {
1032 unsigned long delay_insn;
1033 unsigned int opcode, rtn_reg, ldo_target_reg, ldo_src_reg;
1034
1035 /* We'll need to peek at the next insn. */
1036 delay_insn = bfd_get_32 (input_bfd, hit_data + 4);
1037 opcode = get_opcode (delay_insn);
1038
1039 /* We also need to know the return register for this
1040 call. */
1041 rtn_reg = (insn & 0x03e00000) >> 21;
1042
1043 ldo_src_reg = (delay_insn & 0x03e00000) >> 21;
1044 ldo_target_reg = (delay_insn & 0x001f0000) >> 16;
1045
1046 /* Munge up the value and other parameters for
1047 hppa_elf_relocate_insn. */
1048
1049 value = (stub_hash->offset
1050 + stub_section->output_offset
1051 + stub_section->output_section->vma);
1052
1053 r_format = 17;
1054 r_field = e_fsel;
1055 r_pcrel = 0;
1056 addend = 0;
1057
1058 /* We need to peek at the delay insn and determine if
1059 we'll need to swap the branch and its delay insn. */
1060 if ((insn & 2)
1061 || (opcode == LDO
1062 && ldo_target_reg == rtn_reg)
1063 || (delay_insn == 0x08000240))
1064 {
1065 /* No need to swap the branch and its delay slot, but
1066 we do need to make sure to jump past the return
1067 pointer update in the stub. */
1068 value += 4;
1069
1070 /* If the delay insn does a return pointer adjustment,
1071 then we have to make sure it stays valid. */
1072 if (opcode == LDO
1073 && ldo_target_reg == rtn_reg)
1074 {
1075 delay_insn &= 0xfc00ffff;
1076 delay_insn |= ((31 << 21) | (31 << 16));
1077 bfd_put_32 (input_bfd, delay_insn, hit_data + 4);
1078 }
1079 /* Use a BLE to reach the stub. */
1080 insn = BLE_SR4_R0;
1081 }
1082 else
1083 {
1084 /* Wonderful, we have to swap the call insn and its
1085 delay slot. */
1086 bfd_put_32 (input_bfd, delay_insn, hit_data);
1087 /* Use a BLE,n to reach the stub. */
1088 insn = (BLE_SR4_R0 | 0x2);
1089 bfd_put_32 (input_bfd, insn, hit_data + 4);
1090 insn = hppa_elf_relocate_insn (input_bfd,
1091 input_section,
1092 insn, offset + 4,
1093 value, addend,
1094 r_format, r_field,
1095 r_pcrel);
1096 /* Update the instruction word. */
1097 bfd_put_32 (input_bfd, insn, hit_data + 4);
1098 return bfd_reloc_dangerous;
1099 }
1100 }
1101 else
1102 {
1103 /* PLABEL stuff is easy. */
1104
1105 value = (stub_hash->offset
1106 + stub_section->output_offset
1107 + stub_section->output_section->vma);
1108 /* We don't need the RP adjustment for PLABELs. */
1109 value += 4;
1110 if (r_type == R_PARISC_PLABEL32)
1111 r_format = 32;
1112 else if (r_type == R_PARISC_PLABEL21L)
1113 r_format = 21;
1114 else if (r_type == R_PARISC_PLABEL14R)
1115 r_format = 14;
1116
1117 r_pcrel = 0;
1118 addend = 0;
1119 }
1120 }
1121 else
1122 return bfd_reloc_notsupported;
1123 }
1124 goto do_basic_type_1;
1125 }
1126
1127 do_basic_type_1:
1128 insn = hppa_elf_relocate_insn (input_bfd, input_section, insn,
1129 offset, value, addend, r_format,
1130 r_field, r_pcrel);
1131 break;
1132
1133 /* Something we don't know how to handle. */
1134 default:
1135 return bfd_reloc_notsupported;
1136 }
1137
1138 /* Update the instruction word. */
1139 bfd_put_32 (input_bfd, insn, hit_data);
1140 return (bfd_reloc_ok);
1141 }
1142
1143 /* Return true if SYM represents a local label symbol. */
1144
1145 static boolean
1146 hppa_elf_is_local_label_name (abfd, name)
1147 bfd *abfd;
1148 const char *name;
1149 {
1150 return (name[0] == 'L' && name[1] == '$');
1151 }
1152
1153 /* Do any backend specific processing when beginning to write an object
1154 file. For PA ELF we need to determine the size of the symbol extension
1155 section *before* any other output processing happens. */
1156
1157 static void
1158 elf32_hppa_backend_begin_write_processing (abfd, info)
1159 bfd *abfd;
1160 struct bfd_link_info *info;
1161 {
1162 unsigned int i;
1163 asection *symextn_sec;
1164
1165 /* Size up the symbol extension section. */
1166 if ((abfd->outsymbols == NULL
1167 && info == NULL)
1168 || symext_chain_size != 0)
1169 return;
1170
1171 if (info == NULL)
1172 {
1173 /* We were not called from the BFD ELF linker code, so we need
1174 to examine the output BFD's outsymbols.
1175
1176 Note we can not build the symbol extensions now as the symbol
1177 map hasn't been set up. */
1178 for (i = 0; i < abfd->symcount; i++)
1179 {
1180 elf_symbol_type *symbol = (elf_symbol_type *)abfd->outsymbols[i];
1181
1182 /* Only functions ever need an entry in the symbol extension
1183 section. */
1184 if (!(symbol->symbol.flags & BSF_FUNCTION))
1185 continue;
1186
1187 /* And only if they specify the locations of their arguments. */
1188 if (symbol->tc_data.hppa_arg_reloc == 0)
1189 continue;
1190
1191 /* Yup. This function symbol needs an entry. */
1192 symext_chain_size += 2 * ELF32_PARISC_SX_SIZE;
1193 }
1194 }
1195 else if (info->relocateable == true)
1196 {
1197 struct elf32_hppa_args_hash_table *table;
1198 table = elf32_hppa_hash_table (info)->args_hash_table;
1199
1200 /* Determine the size of the symbol extension section. */
1201 elf32_hppa_args_hash_traverse (table,
1202 elf32_hppa_size_symext,
1203 &symext_chain_size);
1204 }
1205
1206 /* Now create the section and set its size. We'll fill in the
1207 contents later. */
1208 symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
1209 if (symextn_sec == NULL)
1210 symextn_sec = bfd_make_section (abfd, SYMEXTN_SECTION_NAME);
1211
1212 bfd_set_section_flags (abfd, symextn_sec,
1213 SEC_LOAD | SEC_HAS_CONTENTS | SEC_DATA);
1214 symextn_sec->output_section = symextn_sec;
1215 symextn_sec->output_offset = 0;
1216 bfd_set_section_alignment (abfd, symextn_sec, 2);
1217 bfd_set_section_size (abfd, symextn_sec, symext_chain_size);
1218 }
1219
1220 /* Called for each entry in the args location hash table. For each
1221 entry we bump the size pointer by 2 records (16 bytes). */
1222
1223 static boolean
1224 elf32_hppa_size_symext (gen_entry, in_args)
1225 struct bfd_hash_entry *gen_entry;
1226 PTR in_args;
1227 {
1228 bfd_size_type *sizep = (bfd_size_type *)in_args;
1229
1230 *sizep += 2 * ELF32_PARISC_SX_SIZE;
1231 return true;
1232 }
1233
1234 /* Backend routine called by the linker for each output symbol.
1235
1236 For PA ELF we use this opportunity to add an appropriate entry
1237 to the symbol extension chain for function symbols. */
1238
1239 static boolean
1240 elf32_hppa_link_output_symbol_hook (abfd, info, name, sym, section)
1241 bfd *abfd;
1242 struct bfd_link_info *info;
1243 const char *name;
1244 Elf_Internal_Sym *sym;
1245 asection *section;
1246 {
1247 char *new_name;
1248 unsigned int len, index;
1249 struct elf32_hppa_args_hash_table *args_hash_table;
1250 struct elf32_hppa_args_hash_entry *args_hash;
1251
1252 /* If the args hash table is NULL, then we've encountered an error
1253 of some sorts (for example, an undefined symbol). In that case
1254 we've got nothing else to do.
1255
1256 NOTE: elf_link_output_symbol will abort if we return false here! */
1257 if (elf32_hppa_hash_table (info)->args_hash_table == NULL)
1258 return true;
1259
1260 index = elf32_hppa_hash_table (info)->output_symbol_count++;
1261
1262 /* We need to look up this symbol in the args hash table to see if
1263 it has argument relocation bits. */
1264 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1265 return true;
1266
1267 /* We know it's a function symbol of some kind. */
1268 len = strlen (name) + 1;
1269 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
1270 len += 9;
1271
1272 new_name = bfd_malloc (len);
1273 if (new_name == NULL)
1274 return false;
1275
1276 strcpy (new_name, name);
1277 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
1278 sprintf (new_name + len - 10, "_%08x", (int)section);
1279
1280 /* Now that we have the unique name, we can look it up in the
1281 args hash table. */
1282 args_hash_table = elf32_hppa_hash_table (info)->args_hash_table;
1283 args_hash = elf32_hppa_args_hash_lookup (args_hash_table, new_name,
1284 false, false);
1285 free (new_name);
1286 if (args_hash == NULL)
1287 return true;
1288
1289 /* We know this symbol has arg reloc bits. */
1290 add_entry_to_symext_chain (abfd, args_hash->arg_bits,
1291 index, &symext_rootP, &symext_lastP);
1292 return true;
1293 }
1294
1295 /* Perform any processing needed late in the object file writing process.
1296 For PA ELF we build and set the contents of the symbol extension
1297 section. */
1298
1299 static void
1300 elf32_hppa_backend_final_write_processing (abfd, linker)
1301 bfd *abfd;
1302 boolean linker;
1303 {
1304 asection *symextn_sec;
1305 unsigned int i;
1306
1307 /* Now build the symbol extension section. */
1308 if (symext_chain_size == 0)
1309 return;
1310
1311 if (! linker)
1312 {
1313 /* We were not called from the backend linker, so we still need
1314 to build the symbol extension chain.
1315
1316 Look at each symbol, adding the appropriate information to the
1317 symbol extension section list as necessary. */
1318 for (i = 0; i < abfd->symcount; i++)
1319 {
1320 elf_symbol_type *symbol = (elf_symbol_type *) abfd->outsymbols[i];
1321
1322 /* Only functions ever need an entry in the symbol extension
1323 section. */
1324 if (!(symbol->symbol.flags & BSF_FUNCTION))
1325 continue;
1326
1327 /* And only if they specify the locations of their arguments. */
1328 if (symbol->tc_data.hppa_arg_reloc == 0)
1329 continue;
1330
1331 /* Add this symbol's information to the chain. */
1332 add_entry_to_symext_chain (abfd, symbol->tc_data.hppa_arg_reloc,
1333 symbol->symbol.udata.i, &symext_rootP,
1334 &symext_lastP);
1335 }
1336 }
1337
1338 /* Now fill in the contents of the symbol extension section. */
1339 elf_hppa_tc_make_sections (abfd, symext_rootP);
1340
1341 /* And attach that as the section's contents. */
1342 symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
1343 if (symextn_sec == (asection *) 0)
1344 abort();
1345
1346 symextn_sec->contents = (void *)symextn_contents;
1347
1348 bfd_set_section_contents (abfd, symextn_sec, symextn_sec->contents,
1349 symextn_sec->output_offset, symextn_sec->_raw_size);
1350 }
1351
1352 /* Update the symbol extention chain to include the symbol pointed to
1353 by SYMBOLP if SYMBOLP is a function symbol. Used internally and by GAS. */
1354
1355 static void
1356 add_entry_to_symext_chain (abfd, arg_reloc, sym_idx, symext_root, symext_last)
1357 bfd *abfd;
1358 unsigned int arg_reloc;
1359 unsigned int sym_idx;
1360 symext_chainS **symext_root;
1361 symext_chainS **symext_last;
1362 {
1363 symext_chainS *symextP;
1364
1365 /* Allocate memory and initialize this entry. */
1366 symextP = (symext_chainS *) bfd_alloc (abfd, sizeof (symext_chainS) * 2);
1367 if (!symextP)
1368 abort(); /* FIXME */
1369
1370 symextP[0].entry = ELF32_PARISC_SX_WORD (PARISC_SXT_SYMNDX, sym_idx);
1371 symextP[0].next = &symextP[1];
1372
1373 symextP[1].entry = ELF32_PARISC_SX_WORD (PARISC_SXT_ARG_RELOC, arg_reloc);
1374 symextP[1].next = NULL;
1375
1376 /* Now update the chain itself so it can be walked later to build
1377 the symbol extension section. */
1378 if (*symext_root == NULL)
1379 {
1380 *symext_root = &symextP[0];
1381 *symext_last = &symextP[1];
1382 }
1383 else
1384 {
1385 (*symext_last)->next = &symextP[0];
1386 *symext_last = &symextP[1];
1387 }
1388 }
1389
1390 /* Build the symbol extension section. */
1391
1392 static void
1393 elf_hppa_tc_make_sections (abfd, symext_root)
1394 bfd *abfd;
1395 symext_chainS *symext_root;
1396 {
1397 symext_chainS *symextP;
1398 unsigned int i;
1399 asection *symextn_sec;
1400
1401 symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
1402
1403 /* Grab some memory for the contents of the symbol extension section
1404 itself. */
1405 symextn_contents = (bfd_byte *) bfd_zalloc (abfd,
1406 symextn_sec->_raw_size);
1407 if (!symextn_contents)
1408 abort(); /* FIXME */
1409
1410 /* Fill in the contents of the symbol extension chain. */
1411 for (i = 0, symextP = symext_root; symextP; symextP = symextP->next, ++i)
1412 ELF32_PARISC_SX_PUT (abfd, (bfd_vma) symextP->entry,
1413 symextn_contents + i * ELF32_PARISC_SX_SIZE);
1414
1415 return;
1416 }
1417
1418 /* Do some PA ELF specific work after reading in the symbol table.
1419 In particular attach the argument relocation from the
1420 symbol extension section to the appropriate symbols. */
1421
1422 static boolean
1423 elf32_hppa_backend_symbol_table_processing (abfd, esyms,symcnt)
1424 bfd *abfd;
1425 elf_symbol_type *esyms;
1426 unsigned int symcnt;
1427 {
1428 Elf32_Internal_Shdr *symextn_hdr =
1429 bfd_elf_find_section (abfd, SYMEXTN_SECTION_NAME);
1430 unsigned int i, current_sym_idx = 0;
1431
1432 /* If no symbol extension existed, then all symbol extension information
1433 is assumed to be zero. */
1434 if (symextn_hdr == NULL)
1435 {
1436 for (i = 0; i < symcnt; i++)
1437 esyms[i].tc_data.hppa_arg_reloc = 0;
1438 return (true);
1439 }
1440
1441 /* FIXME: Why not use bfd_get_section_contents here? Also should give
1442 memory back when we're done. */
1443 /* Allocate a buffer of the appropriate size for the symextn section. */
1444 symextn_hdr->contents = bfd_zalloc(abfd,symextn_hdr->sh_size);
1445 if (!symextn_hdr->contents)
1446 return false;
1447
1448 /* Read in the symextn section. */
1449 if (bfd_seek (abfd, symextn_hdr->sh_offset, SEEK_SET) == -1)
1450 return false;
1451 if (bfd_read ((PTR) symextn_hdr->contents, 1, symextn_hdr->sh_size, abfd)
1452 != symextn_hdr->sh_size)
1453 return false;
1454
1455 /* Parse entries in the symbol extension section, updating the symtab
1456 entries as we go */
1457 for (i = 0; i < symextn_hdr->sh_size / ELF32_PARISC_SX_SIZE; i++)
1458 {
1459 symext_entryS se =
1460 ELF32_PARISC_SX_GET (abfd,
1461 ((unsigned char *)symextn_hdr->contents
1462 + i * ELF32_PARISC_SX_SIZE));
1463 unsigned int se_value = ELF32_PARISC_SX_VAL (se);
1464 unsigned int se_type = ELF32_PARISC_SX_TYPE (se);
1465
1466 switch (se_type)
1467 {
1468 case PARISC_SXT_NULL:
1469 break;
1470
1471 case PARISC_SXT_SYMNDX:
1472 if (se_value >= symcnt)
1473 {
1474 bfd_set_error (bfd_error_bad_value);
1475 return (false);
1476 }
1477 current_sym_idx = se_value - 1;
1478 break;
1479
1480 case PARISC_SXT_ARG_RELOC:
1481 esyms[current_sym_idx].tc_data.hppa_arg_reloc = se_value;
1482 break;
1483
1484 default:
1485 bfd_set_error (bfd_error_bad_value);
1486 return (false);
1487 }
1488 }
1489 return (true);
1490 }
1491
1492 /* Read and attach the symbol extension information for the symbols
1493 in INPUT_BFD to the argument location hash table. Handle locals
1494 if DO_LOCALS is true; likewise for globals when DO_GLOBALS is true. */
1495
1496 static boolean
1497 elf32_hppa_read_symext_info (input_bfd, symtab_hdr, args_hash_table, local_syms)
1498 bfd *input_bfd;
1499 Elf_Internal_Shdr *symtab_hdr;
1500 struct elf32_hppa_args_hash_table *args_hash_table;
1501 Elf_Internal_Sym *local_syms;
1502 {
1503 asection *symextn_sec;
1504 bfd_byte *contents;
1505 unsigned int i, n_entries, current_index = 0;
1506
1507 /* Get the symbol extension section for this BFD. If no section exists
1508 then there's nothing to do. Likewise if the section exists, but
1509 has no contents. */
1510 symextn_sec = bfd_get_section_by_name (input_bfd, SYMEXTN_SECTION_NAME);
1511 if (symextn_sec == NULL)
1512 return true;
1513
1514 /* Done separately so we can turn off SEC_HAS_CONTENTS (see below). */
1515 if (symextn_sec->_raw_size == 0)
1516 {
1517 symextn_sec->flags &= ~SEC_HAS_CONTENTS;
1518 return true;
1519 }
1520
1521 contents = (bfd_byte *) bfd_malloc ((size_t) symextn_sec->_raw_size);
1522 if (contents == NULL)
1523 return false;
1524
1525 /* How gross. We turn off SEC_HAS_CONTENTS for the input symbol extension
1526 sections to keep the generic ELF/BFD code from trying to do anything
1527 with them. We have to undo that hack temporarily so that we can read
1528 in the contents with the generic code. */
1529 symextn_sec->flags |= SEC_HAS_CONTENTS;
1530 if (bfd_get_section_contents (input_bfd, symextn_sec, contents,
1531 0, symextn_sec->_raw_size) == false)
1532 {
1533 symextn_sec->flags &= ~SEC_HAS_CONTENTS;
1534 free (contents);
1535 return false;
1536 }
1537
1538 /* Gross. Turn off SEC_HAS_CONTENTS for the input symbol extension
1539 sections (see above). */
1540 symextn_sec->flags &= ~SEC_HAS_CONTENTS;
1541
1542 n_entries = symextn_sec->_raw_size / ELF32_PARISC_SX_SIZE;
1543 for (i = 0; i < n_entries; i++)
1544 {
1545 symext_entryS entry =
1546 ELF32_PARISC_SX_GET (input_bfd, contents + i * ELF32_PARISC_SX_SIZE);
1547 unsigned int value = ELF32_PARISC_SX_VAL (entry);
1548 unsigned int type = ELF32_PARISC_SX_TYPE (entry);
1549 struct elf32_hppa_args_hash_entry *args_hash;
1550
1551 switch (type)
1552 {
1553 case PARISC_SXT_NULL:
1554 break;
1555
1556 case PARISC_SXT_SYMNDX:
1557 if (value >= symtab_hdr->sh_size / sizeof (Elf32_External_Sym))
1558 {
1559 bfd_set_error (bfd_error_bad_value);
1560 free (contents);
1561 return false;
1562 }
1563 current_index = value;
1564 break;
1565
1566 case PARISC_SXT_ARG_RELOC:
1567 if (current_index < symtab_hdr->sh_info)
1568 {
1569 Elf_Internal_Shdr *hdr;
1570 char *new_name;
1571 const char *sym_name;
1572 asection *sym_sec;
1573 unsigned int len;
1574
1575 hdr = elf_elfsections (input_bfd)[local_syms[current_index].st_shndx];
1576 sym_sec = hdr->bfd_section;
1577 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1578 symtab_hdr->sh_link,
1579 local_syms[current_index].st_name);
1580 len = strlen (sym_name) + 10;
1581 new_name = bfd_malloc (len);
1582 if (new_name == NULL)
1583 {
1584 free (contents);
1585 return false;
1586 }
1587 strcpy (new_name, sym_name);
1588 sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
1589
1590 /* This is a global symbol with argument location info.
1591 We need to enter it into the hash table. */
1592 args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
1593 new_name, true,
1594 true);
1595 free (new_name);
1596 if (args_hash == NULL)
1597 {
1598 free (contents);
1599 return false;
1600 }
1601 args_hash->arg_bits = value;
1602 break;
1603 }
1604 else if (current_index >= symtab_hdr->sh_info)
1605 {
1606 struct elf_link_hash_entry *h;
1607
1608 current_index -= symtab_hdr->sh_info;
1609 h = elf_sym_hashes(input_bfd)[current_index];
1610 /* This is a global symbol with argument location
1611 information. We need to enter it into the hash table. */
1612 args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
1613 h->root.root.string,
1614 true, true);
1615 if (args_hash == NULL)
1616 {
1617 bfd_set_error (bfd_error_bad_value);
1618 free (contents);
1619 return false;
1620 }
1621 args_hash->arg_bits = value;
1622 break;
1623 }
1624 else
1625 break;
1626
1627 default:
1628 bfd_set_error (bfd_error_bad_value);
1629 free (contents);
1630 return false;
1631 }
1632 }
1633 free (contents);
1634 return true;
1635 }
1636
1637 /* Undo the generic ELF code's subtraction of section->vma from the
1638 value of each external symbol. */
1639
1640 static boolean
1641 elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1642 bfd *abfd;
1643 struct bfd_link_info *info;
1644 const Elf_Internal_Sym *sym;
1645 const char **namep;
1646 flagword *flagsp;
1647 asection **secp;
1648 bfd_vma *valp;
1649 {
1650 *valp += (*secp)->vma;
1651 return true;
1652 }
1653
1654 /* Determine the name of the stub needed to perform a call assuming the
1655 argument relocation bits for caller and callee are in CALLER and CALLEE
1656 for a call from LOCATION to DESTINATION. Copy the name into STUB_NAME. */
1657
1658 static void
1659 elf32_hppa_name_of_stub (caller, callee, location, destination, stub_name)
1660 unsigned int caller, callee;
1661 bfd_vma location, destination;
1662 char *stub_name;
1663 {
1664 arg_reloc_type arg_reloc_types[5];
1665
1666 if (elf32_hppa_arg_reloc_needed (caller, callee, arg_reloc_types))
1667 {
1668 arg_reloc_location i;
1669 /* Fill in the basic template. */
1670 strcpy (stub_name, "__XX_XX_XX_XX_XX_stub_");
1671
1672 /* Now fix the specifics. */
1673 for (i = ARG0; i <= RET; i++)
1674 switch (arg_reloc_types[i])
1675 {
1676 case NO:
1677 stub_name[3 * i + 2] = 'N';
1678 stub_name[3 * i + 3] = 'O';
1679 break;
1680 case GF:
1681 stub_name[3 * i + 2] = 'G';
1682 stub_name[3 * i + 3] = 'F';
1683 break;
1684 case FG:
1685 stub_name[3 * i + 2] = 'F';
1686 stub_name[3 * i + 3] = 'G';
1687 break;
1688 case GD:
1689 stub_name[3 * i + 2] = 'G';
1690 stub_name[3 * i + 3] = 'D';
1691 break;
1692 case DG:
1693 stub_name[3 * i + 2] = 'D';
1694 stub_name[3 * i + 3] = 'G';
1695 break;
1696 }
1697 }
1698 else
1699 strcpy (stub_name, "_____long_branch_stub_");
1700 }
1701
1702 /* Determine if an argument relocation stub is needed to perform a
1703 call assuming the argument relocation bits for caller and callee
1704 are in CALLER and CALLEE. Place the type of relocations (if any)
1705 into stub_types_p. */
1706
1707 static boolean
1708 elf32_hppa_arg_reloc_needed (caller, callee, stub_types)
1709 unsigned int caller, callee;
1710 arg_reloc_type stub_types[5];
1711 {
1712 /* Special case for no relocations. */
1713 if (caller == 0 || callee == 0)
1714 return 0;
1715 else
1716 {
1717 arg_location caller_loc[5];
1718 arg_location callee_loc[5];
1719
1720 /* Extract the location information for the argument and return
1721 value on both the caller and callee sides. */
1722 caller_loc[ARG0] = EXTRACT_ARBITS (caller, ARG0);
1723 callee_loc[ARG0] = EXTRACT_ARBITS (callee, ARG0);
1724 caller_loc[ARG1] = EXTRACT_ARBITS (caller, ARG1);
1725 callee_loc[ARG1] = EXTRACT_ARBITS (callee, ARG1);
1726 caller_loc[ARG2] = EXTRACT_ARBITS (caller, ARG2);
1727 callee_loc[ARG2] = EXTRACT_ARBITS (callee, ARG2);
1728 caller_loc[ARG3] = EXTRACT_ARBITS (caller, ARG3);
1729 callee_loc[ARG3] = EXTRACT_ARBITS (callee, ARG3);
1730 caller_loc[RET] = EXTRACT_ARBITS (caller, RET);
1731 callee_loc[RET] = EXTRACT_ARBITS (callee, RET);
1732
1733 /* Check some special combinations. This is necessary to
1734 deal with double precision FP arguments. */
1735 if (caller_loc[ARG0] == AR_FU || caller_loc[ARG1] == AR_FU)
1736 {
1737 caller_loc[ARG0] = AR_FPDBL1;
1738 caller_loc[ARG1] = AR_NO;
1739 }
1740 if (caller_loc[ARG2] == AR_FU || caller_loc[ARG3] == AR_FU)
1741 {
1742 caller_loc[ARG2] = AR_FPDBL2;
1743 caller_loc[ARG3] = AR_NO;
1744 }
1745 if (callee_loc[ARG0] == AR_FU || callee_loc[ARG1] == AR_FU)
1746 {
1747 callee_loc[ARG0] = AR_FPDBL1;
1748 callee_loc[ARG1] = AR_NO;
1749 }
1750 if (callee_loc[ARG2] == AR_FU || callee_loc[ARG3] == AR_FU)
1751 {
1752 callee_loc[ARG2] = AR_FPDBL2;
1753 callee_loc[ARG3] = AR_NO;
1754 }
1755
1756 /* Now look up any relocation needed for each argument and the
1757 return value. */
1758 stub_types[ARG0] = arg_mismatches[caller_loc[ARG0]][callee_loc[ARG0]];
1759 stub_types[ARG1] = arg_mismatches[caller_loc[ARG1]][callee_loc[ARG1]];
1760 stub_types[ARG2] = arg_mismatches[caller_loc[ARG2]][callee_loc[ARG2]];
1761 stub_types[ARG3] = arg_mismatches[caller_loc[ARG3]][callee_loc[ARG3]];
1762 stub_types[RET] = ret_mismatches[caller_loc[RET]][callee_loc[RET]];
1763
1764 return (stub_types[ARG0] != NO
1765 || stub_types[ARG1] != NO
1766 || stub_types[ARG2] != NO
1767 || stub_types[ARG3] != NO
1768 || stub_types[RET] != NO);
1769 }
1770 }
1771
1772 /* Compute the size of the stub needed to call from LOCATION to DESTINATION
1773 (a function named SYM_NAME), with argument relocation bits CALLER and
1774 CALLEE. Return zero if no stub is needed to perform such a call. */
1775
1776 static unsigned int
1777 elf32_hppa_size_of_stub (callee, caller, location, destination, sym_name)
1778 unsigned int callee, caller;
1779 bfd_vma location, destination;
1780 const char *sym_name;
1781 {
1782 arg_reloc_type arg_reloc_types[5];
1783
1784 /* Determine if a long branch or argument relocation stub is needed.
1785 If an argument relocation stub is needed, the relocation will be
1786 stored into arg_reloc_types. */
1787 if (!(((int)(location - destination) > 0x3ffff)
1788 || ((int)(location - destination) < (int)0xfffc0000)
1789 || elf32_hppa_arg_reloc_needed (caller, callee, arg_reloc_types)))
1790 return 0;
1791
1792 /* Some kind of stub is needed. Determine how big it needs to be.
1793 First check for argument relocation stubs as they also handle
1794 long calls. Then check for long calls to millicode and finally
1795 the normal long calls. */
1796 if (arg_reloc_types[ARG0] != NO
1797 || arg_reloc_types[ARG1] != NO
1798 || arg_reloc_types[ARG2] != NO
1799 || arg_reloc_types[ARG3] != NO
1800 || arg_reloc_types[RET] != NO)
1801 {
1802 /* Some kind of argument relocation stub is needed. */
1803 unsigned int len = 16;
1804 arg_reloc_location i;
1805
1806 /* Each GR or FG relocation takes 2 insns, each GD or DG
1807 relocation takes 3 insns. Plus 4 more insns for the
1808 RP adjustment, ldil & (be | ble) and copy. */
1809 for (i = ARG0; i <= RET; i++)
1810 switch (arg_reloc_types[i])
1811 {
1812 case GF:
1813 case FG:
1814 len += 8;
1815 break;
1816
1817 case GD:
1818 case DG:
1819 len += 12;
1820 break;
1821
1822 default:
1823 break;
1824 }
1825
1826 /* Extra instructions are needed if we're relocating a return value. */
1827 if (arg_reloc_types[RET] != NO)
1828 len += 12;
1829
1830 return len;
1831 }
1832 else if (!strncmp ("$$", sym_name, 2)
1833 && strcmp ("$$dyncall", sym_name))
1834 return 12;
1835 else
1836 return 16;
1837 }
1838
1839 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
1840 IN_ARGS contains the stub BFD and link info pointers. */
1841
1842 static boolean
1843 elf32_hppa_build_one_stub (gen_entry, in_args)
1844 struct bfd_hash_entry *gen_entry;
1845 PTR in_args;
1846 {
1847 void **args = (void **)in_args;
1848 bfd *stub_bfd = (bfd *)args[0];
1849 struct bfd_link_info *info = (struct bfd_link_info *)args[1];
1850 struct elf32_hppa_stub_hash_entry *entry;
1851 struct elf32_hppa_stub_hash_table *stub_hash_table;
1852 bfd_byte *loc;
1853 symvalue sym_value;
1854 const char *sym_name;
1855
1856 /* Initialize pointers to the stub hash table, the particular entry we
1857 are building a stub for, and where (in memory) we should place the stub
1858 instructions. */
1859 entry = (struct elf32_hppa_stub_hash_entry *)gen_entry;
1860 stub_hash_table = elf32_hppa_hash_table(info)->stub_hash_table;
1861 loc = stub_hash_table->location;
1862
1863 /* Make a note of the offset within the stubs for this entry. */
1864 entry->offset = stub_hash_table->offset;
1865
1866 /* The symbol's name starts at offset 22. */
1867 sym_name = entry->root.string + 22;
1868
1869 sym_value = (entry->target_value
1870 + entry->target_section->output_offset
1871 + entry->target_section->output_section->vma);
1872
1873 if (strncmp ("_____long_branch_stub_", entry->root.string, 22))
1874 {
1875 /* This must be an argument or return value relocation stub. */
1876 unsigned long insn;
1877 arg_reloc_location i;
1878 bfd_byte *begin_loc = loc;
1879
1880 /* First the return pointer adjustment. Depending on exact calling
1881 sequence this instruction may be skipped. */
1882 bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
1883 loc += 4;
1884
1885 /* If we are relocating a return value, then we're going to have
1886 to return into the stub. So we have to save off the user's
1887 return pointer into the stack at RP'. */
1888 if (strncmp (entry->root.string + 14, "NO", 2))
1889 {
1890 bfd_put_32 (stub_bfd, STW_R31_M8R30, loc);
1891 loc += 4;
1892 }
1893
1894 /* Iterate over the argument relocations, emitting instructions
1895 to move them around as necessary. */
1896 for (i = ARG0; i <= ARG3; i++)
1897 {
1898 if (!strncmp (entry->root.string + 3 * i + 2, "GF", 2))
1899 {
1900 bfd_put_32 (stub_bfd, STW_ARG_M16R30 | ((26 - i) << 16), loc);
1901 bfd_put_32 (stub_bfd, FLDW_M16R30_FARG | (4 + i), loc + 4);
1902 loc += 8;
1903 }
1904 else if (!strncmp (entry->root.string + 3 * i + 2, "FG", 2))
1905 {
1906 bfd_put_32 (stub_bfd, FSTW_FARG_M16R30 | (4 + i), loc);
1907 bfd_put_32 (stub_bfd, LDW_M16R30_ARG | ((26 - i) << 16), loc + 4);
1908 loc += 8;
1909 }
1910 else if (!strncmp (entry->root.string + 3 * i + 2, "GD", 2))
1911 {
1912 bfd_put_32 (stub_bfd, STW_ARG_M12R30 | ((26 - i) << 16), loc);
1913 bfd_put_32 (stub_bfd, STW_ARG_M16R30 | ((25 - i) << 16), loc + 4);
1914 bfd_put_32 (stub_bfd, FLDD_M16R30_FARG | (5 + i), loc + 8);
1915 loc += 12;
1916 }
1917 else if (!strncmp (entry->root.string + 3 * i + 2, "DG", 2))
1918 {
1919 bfd_put_32 (stub_bfd, FSTD_FARG_M16R30 | (5 + i), loc);
1920 bfd_put_32 (stub_bfd, LDW_M12R30_ARG | ((26 - i) << 16), loc + 4);
1921 bfd_put_32 (stub_bfd, LDW_M16R30_ARG | ((25 - i) << 16), loc + 8);
1922 loc += 12;
1923 }
1924 }
1925
1926 /* Load the high bits of the target address into %r1. */
1927 insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
1928 hppa_field_adjust (sym_value, 0, e_lrsel), 21);
1929 bfd_put_32 (stub_bfd, insn, loc);
1930 loc += 4;
1931
1932 /* If we are relocating a return value, then we're going to have
1933 to return into the stub, then perform the return value relocation. */
1934 if (strncmp (entry->root.string + 14, "NO", 2))
1935 {
1936 /* To return to the stub we "ble" to the target and copy the return
1937 pointer from %r31 into %r2. */
1938 insn = hppa_rebuild_insn (stub_bfd,
1939 BLE_SR4_R1,
1940 hppa_field_adjust (sym_value, 0,
1941 e_rrsel) >> 2,
1942 17);
1943 bfd_put_32 (stub_bfd, insn, loc);
1944 bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 4);
1945
1946 /* Reload the return pointer for our caller from the stack. */
1947 bfd_put_32 (stub_bfd, LDW_M8R30_R31, loc + 8);
1948 loc += 12;
1949
1950 /* Perform the return value relocation. */
1951 if (!strncmp (entry->root.string + 14, "GF", 2))
1952 {
1953 bfd_put_32 (stub_bfd, STW_ARG_M16R30 | (28 << 16), loc);
1954 bfd_put_32 (stub_bfd, FLDW_M16R30_FARG | 4, loc + 4);
1955 loc += 8;
1956 }
1957 else if (!strncmp (entry->root.string + 14, "FG", 2))
1958 {
1959 bfd_put_32 (stub_bfd, FSTW_FARG_M16R30 | 4, loc);
1960 bfd_put_32 (stub_bfd, LDW_M16R30_ARG | (28 << 16), loc + 4);
1961 loc += 8;
1962 }
1963 else if (!strncmp (entry->root.string + 2, "GD", 2))
1964 {
1965 bfd_put_32 (stub_bfd, STW_ARG_M12R30 | (28 << 16), loc);
1966 bfd_put_32 (stub_bfd, STW_ARG_M16R30 | (29 << 16), loc + 4);
1967 bfd_put_32 (stub_bfd, FLDD_M16R30_FARG | 4, loc + 8);
1968 loc += 12;
1969 }
1970 else if (!strncmp (entry->root.string + 2, "DG", 2))
1971 {
1972 bfd_put_32 (stub_bfd, FSTD_FARG_M16R30 | 4, loc);
1973 bfd_put_32 (stub_bfd, LDW_M12R30_ARG | (28 << 16), loc + 4);
1974 bfd_put_32 (stub_bfd, LDW_M16R30_ARG | (29 << 16), loc + 8);
1975 loc += 12;
1976 }
1977 /* Branch back to the user's code now. */
1978 bfd_put_32 (stub_bfd, BV_N_0_R31, loc);
1979 loc += 4;
1980 }
1981 else
1982 {
1983 /* No return value relocation, so we can simply "be" to the
1984 target and copy out return pointer into %r2. */
1985 insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1,
1986 hppa_field_adjust (sym_value, 0,
1987 e_rrsel) >> 2, 17);
1988 bfd_put_32 (stub_bfd, insn, loc);
1989 bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 4);
1990 loc += 8;
1991 }
1992
1993 /* Update the location and offsets. */
1994 stub_hash_table->location += (loc - begin_loc);
1995 stub_hash_table->offset += (loc - begin_loc);
1996 }
1997 else
1998 {
1999 /* Create one of two variant long branch stubs. One for $$dyncall and
2000 normal calls, the other for calls to millicode. */
2001 unsigned long insn;
2002 int millicode_call = 0;
2003
2004 if (!strncmp ("$$", sym_name, 2) && strcmp ("$$dyncall", sym_name))
2005 millicode_call = 1;
2006
2007 /* First the return pointer adjustment. Depending on exact calling
2008 sequence this instruction may be skipped. */
2009 bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
2010
2011 /* The next two instructions are the long branch itself. A long branch
2012 is formed with "ldil" loading the upper bits of the target address
2013 into a register, then branching with "be" which adds in the lower bits.
2014 Long branches to millicode nullify the delay slot of the "be". */
2015 insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
2016 hppa_field_adjust (sym_value, 0, e_lrsel), 21);
2017 bfd_put_32 (stub_bfd, insn, loc + 4);
2018 insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1 | (millicode_call ? 2 : 0),
2019 hppa_field_adjust (sym_value, 0, e_rrsel) >> 2,
2020 17);
2021 bfd_put_32 (stub_bfd, insn, loc + 8);
2022
2023 if (!millicode_call)
2024 {
2025 /* The sequence to call this stub places the return pointer into %r31,
2026 the final target expects the return pointer in %r2, so copy the
2027 return pointer into the proper register. */
2028 bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 12);
2029
2030 /* Update the location and offsets. */
2031 stub_hash_table->location += 16;
2032 stub_hash_table->offset += 16;
2033 }
2034 else
2035 {
2036 /* Update the location and offsets. */
2037 stub_hash_table->location += 12;
2038 stub_hash_table->offset += 12;
2039 }
2040
2041 }
2042 return true;
2043 }
2044
2045 /* External entry points for sizing and building linker stubs. */
2046
2047 /* Build all the stubs associated with the current output file. The
2048 stubs are kept in a hash table attached to the main linker hash
2049 table. This is called via hppaelf_finish in the linker. */
2050
2051 boolean
2052 elf32_hppa_build_stubs (stub_bfd, info)
2053 bfd *stub_bfd;
2054 struct bfd_link_info *info;
2055 {
2056 /* The stub BFD only has one section. */
2057 asection *stub_sec = stub_bfd->sections;
2058 struct elf32_hppa_stub_hash_table *table;
2059 unsigned int size;
2060 void *args[2];
2061
2062 /* So we can pass both the BFD for the stubs and the link info
2063 structure to the routine which actually builds stubs. */
2064 args[0] = stub_bfd;
2065 args[1] = info;
2066
2067 /* Allocate memory to hold the linker stubs. */
2068 size = bfd_section_size (stub_bfd, stub_sec);
2069 stub_sec->contents = (unsigned char *) bfd_zalloc (stub_bfd, size);
2070 if (stub_sec->contents == NULL)
2071 return false;
2072 table = elf32_hppa_hash_table(info)->stub_hash_table;
2073 table->location = stub_sec->contents;
2074
2075 /* Build the stubs as directed by the stub hash table. */
2076 elf32_hppa_stub_hash_traverse (table, elf32_hppa_build_one_stub, args);
2077
2078 return true;
2079 }
2080
2081 /* Determine and set the size of the stub section for a final link.
2082
2083 The basic idea here is to examine all the relocations looking for
2084 PC-relative calls to a target that is unreachable with a "bl"
2085 instruction or calls where the caller and callee disagree on the
2086 location of their arguments or return value. */
2087
2088 boolean
2089 elf32_hppa_size_stubs (stub_bfd, output_bfd, link_info)
2090 bfd *stub_bfd;
2091 bfd *output_bfd;
2092 struct bfd_link_info *link_info;
2093 {
2094 bfd *input_bfd;
2095 asection *section, *stub_sec = 0;
2096 Elf_Internal_Shdr *symtab_hdr;
2097 Elf_Internal_Sym *local_syms, *isym, **all_local_syms;
2098 Elf32_External_Sym *ext_syms, *esym;
2099 unsigned int i, index, bfd_count = 0;
2100 struct elf32_hppa_stub_hash_table *stub_hash_table = 0;
2101 struct elf32_hppa_args_hash_table *args_hash_table = 0;
2102
2103 /* Create and initialize the stub hash table. */
2104 stub_hash_table = ((struct elf32_hppa_stub_hash_table *)
2105 bfd_malloc (sizeof (struct elf32_hppa_stub_hash_table)));
2106 if (!stub_hash_table)
2107 goto error_return;
2108
2109 if (!elf32_hppa_stub_hash_table_init (stub_hash_table, stub_bfd,
2110 elf32_hppa_stub_hash_newfunc))
2111 goto error_return;
2112
2113 /* Likewise for the argument location hash table. */
2114 args_hash_table = ((struct elf32_hppa_args_hash_table *)
2115 bfd_malloc (sizeof (struct elf32_hppa_args_hash_table)));
2116 if (!args_hash_table)
2117 goto error_return;
2118
2119 if (!elf32_hppa_args_hash_table_init (args_hash_table,
2120 elf32_hppa_args_hash_newfunc))
2121 goto error_return;
2122
2123 /* Attach the hash tables to the main hash table. */
2124 elf32_hppa_hash_table(link_info)->stub_hash_table = stub_hash_table;
2125 elf32_hppa_hash_table(link_info)->args_hash_table = args_hash_table;
2126
2127 /* Count the number of input BFDs. */
2128 for (input_bfd = link_info->input_bfds;
2129 input_bfd != NULL;
2130 input_bfd = input_bfd->link_next)
2131 bfd_count++;
2132
2133 /* We want to read in symbol extension records only once. To do this
2134 we need to read in the local symbols in parallel and save them for
2135 later use; so hold pointers to the local symbols in an array. */
2136 all_local_syms
2137 = (Elf_Internal_Sym **) bfd_malloc (sizeof (Elf_Internal_Sym *)
2138 * bfd_count);
2139 if (all_local_syms == NULL)
2140 goto error_return;
2141 memset (all_local_syms, 0, sizeof (Elf_Internal_Sym *) * bfd_count);
2142
2143 /* Walk over all the input BFDs adding entries to the args hash table
2144 for all the external functions. */
2145 for (input_bfd = link_info->input_bfds, index = 0;
2146 input_bfd != NULL;
2147 input_bfd = input_bfd->link_next, index++)
2148 {
2149 /* We'll need the symbol table in a second. */
2150 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2151 if (symtab_hdr->sh_info == 0)
2152 continue;
2153
2154 /* We need an array of the local symbols attached to the input bfd.
2155 Unfortunately, we're going to have to read & swap them in. */
2156 local_syms
2157 = (Elf_Internal_Sym *) bfd_malloc (symtab_hdr->sh_info
2158 * sizeof (Elf_Internal_Sym));
2159 if (local_syms == NULL)
2160 {
2161 for (i = 0; i < bfd_count; i++)
2162 if (all_local_syms[i])
2163 free (all_local_syms[i]);
2164 free (all_local_syms);
2165 goto error_return;
2166 }
2167 all_local_syms[index] = local_syms;
2168
2169 ext_syms
2170 = (Elf32_External_Sym *) bfd_malloc (symtab_hdr->sh_info
2171 * sizeof (Elf32_External_Sym));
2172 if (ext_syms == NULL)
2173 {
2174 for (i = 0; i < bfd_count; i++)
2175 if (all_local_syms[i])
2176 free (all_local_syms[i]);
2177 free (all_local_syms);
2178 goto error_return;
2179 }
2180
2181 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2182 || bfd_read (ext_syms, 1,
2183 (symtab_hdr->sh_info
2184 * sizeof (Elf32_External_Sym)), input_bfd)
2185 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)))
2186 {
2187 for (i = 0; i < bfd_count; i++)
2188 if (all_local_syms[i])
2189 free (all_local_syms[i]);
2190 free (all_local_syms);
2191 free (ext_syms);
2192 goto error_return;
2193 }
2194
2195 /* Swap the local symbols in. */
2196 isym = local_syms;
2197 esym = ext_syms;
2198 for (i = 0; i < symtab_hdr->sh_info; i++, esym++, isym++)
2199 bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
2200
2201 /* Now we can free the external symbols. */
2202 free (ext_syms);
2203
2204 if (elf32_hppa_read_symext_info (input_bfd, symtab_hdr, args_hash_table,
2205 local_syms) == false)
2206 {
2207 for (i = 0; i < bfd_count; i++)
2208 if (all_local_syms[i])
2209 free (all_local_syms[i]);
2210 free (all_local_syms);
2211 goto error_return;
2212 }
2213 }
2214
2215 /* Magic as we know the stub bfd only has one section. */
2216 stub_sec = stub_bfd->sections;
2217
2218 /* If generating a relocateable output file, then we don't
2219 have to examine the relocs. */
2220 if (link_info->relocateable)
2221 {
2222 for (i = 0; i < bfd_count; i++)
2223 if (all_local_syms[i])
2224 free (all_local_syms[i]);
2225 free (all_local_syms);
2226 return true;
2227 }
2228
2229 /* Now that we have argument location information for all the global
2230 functions we can start looking for stubs. */
2231 for (input_bfd = link_info->input_bfds, index = 0;
2232 input_bfd != NULL;
2233 input_bfd = input_bfd->link_next, index++)
2234 {
2235 /* We'll need the symbol table in a second. */
2236 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2237 if (symtab_hdr->sh_info == 0)
2238 continue;
2239
2240 local_syms = all_local_syms[index];
2241
2242 /* Walk over each section attached to the input bfd. */
2243 for (section = input_bfd->sections;
2244 section != NULL;
2245 section = section->next)
2246 {
2247 Elf_Internal_Shdr *input_rel_hdr;
2248 Elf32_External_Rela *external_relocs, *erelaend, *erela;
2249 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2250
2251 /* If there aren't any relocs, then there's nothing to do. */
2252 if ((section->flags & SEC_RELOC) == 0
2253 || section->reloc_count == 0)
2254 continue;
2255
2256 /* Allocate space for the external relocations. */
2257 external_relocs
2258 = ((Elf32_External_Rela *)
2259 bfd_malloc (section->reloc_count
2260 * sizeof (Elf32_External_Rela)));
2261 if (external_relocs == NULL)
2262 {
2263 for (i = 0; i < bfd_count; i++)
2264 if (all_local_syms[i])
2265 free (all_local_syms[i]);
2266 free (all_local_syms);
2267 goto error_return;
2268 }
2269
2270 /* Likewise for the internal relocations. */
2271 internal_relocs
2272 = ((Elf_Internal_Rela *)
2273 bfd_malloc (section->reloc_count * sizeof (Elf_Internal_Rela)));
2274 if (internal_relocs == NULL)
2275 {
2276 free (external_relocs);
2277 for (i = 0; i < bfd_count; i++)
2278 if (all_local_syms[i])
2279 free (all_local_syms[i]);
2280 free (all_local_syms);
2281 goto error_return;
2282 }
2283
2284 /* Read in the external relocs. */
2285 input_rel_hdr = &elf_section_data (section)->rel_hdr;
2286 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
2287 || bfd_read (external_relocs, 1, input_rel_hdr->sh_size,
2288 input_bfd) != input_rel_hdr->sh_size)
2289 {
2290 free (external_relocs);
2291 free (internal_relocs);
2292 for (i = 0; i < bfd_count; i++)
2293 if (all_local_syms[i])
2294 free (all_local_syms[i]);
2295 free (all_local_syms);
2296 goto error_return;
2297 }
2298
2299 /* Swap in the relocs. */
2300 erela = external_relocs;
2301 erelaend = erela + section->reloc_count;
2302 irela = internal_relocs;
2303 for (; erela < erelaend; erela++, irela++)
2304 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
2305
2306 /* We're done with the external relocs, free them. */
2307 free (external_relocs);
2308
2309 /* Now examine each relocation. */
2310 irela = internal_relocs;
2311 irelaend = irela + section->reloc_count;
2312 for (; irela < irelaend; irela++)
2313 {
2314 long r_type, callee_args, caller_args, size_of_stub;
2315 unsigned long r_index;
2316 struct elf_link_hash_entry *hash;
2317 struct elf32_hppa_stub_hash_entry *stub_hash;
2318 struct elf32_hppa_args_hash_entry *args_hash;
2319 Elf_Internal_Sym *sym;
2320 asection *sym_sec;
2321 const char *sym_name;
2322 symvalue sym_value;
2323 bfd_vma location, destination;
2324 char *new_name = NULL;
2325
2326 r_type = ELF32_R_TYPE (irela->r_info);
2327 r_index = ELF32_R_SYM (irela->r_info);
2328
2329 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
2330 {
2331 bfd_set_error (bfd_error_bad_value);
2332 free (internal_relocs);
2333 for (i = 0; i < bfd_count; i++)
2334 if (all_local_syms[i])
2335 free (all_local_syms[i]);
2336 free (all_local_syms);
2337 goto error_return;
2338 }
2339
2340 /* Only look for stubs on call instructions or plabel
2341 references. */
2342 if (r_type != R_PARISC_PCREL17F
2343 && r_type != R_PARISC_PLABEL32
2344 && r_type != R_PARISC_PLABEL21L
2345 && r_type != R_PARISC_PLABEL14R)
2346 continue;
2347
2348 /* Now determine the call target, its name, value, section
2349 and argument relocation bits. */
2350 hash = NULL;
2351 sym = NULL;
2352 sym_sec = NULL;
2353 if (r_index < symtab_hdr->sh_info)
2354 {
2355 /* It's a local symbol. */
2356 Elf_Internal_Shdr *hdr;
2357
2358 sym = local_syms + r_index;
2359 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2360 sym_sec = hdr->bfd_section;
2361 sym_name = bfd_elf_string_from_elf_section (input_bfd,
2362 symtab_hdr->sh_link,
2363 sym->st_name);
2364 sym_value = (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2365 ? 0 : sym->st_value);
2366 destination = (sym_value
2367 + sym_sec->output_offset
2368 + sym_sec->output_section->vma);
2369
2370 /* Tack on an ID so we can uniquely identify this local
2371 symbol in the stub or arg info hash tables. */
2372 new_name = bfd_malloc (strlen (sym_name) + 10);
2373 if (new_name == 0)
2374 {
2375 free (internal_relocs);
2376 for (i = 0; i < bfd_count; i++)
2377 if (all_local_syms[i])
2378 free (all_local_syms[i]);
2379 free (all_local_syms);
2380 goto error_return;
2381 }
2382 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
2383 sym_name = new_name;
2384 }
2385 else
2386 {
2387 /* It's an external symbol. */
2388 long index;
2389
2390 index = r_index - symtab_hdr->sh_info;
2391 hash = elf_sym_hashes (input_bfd)[index];
2392 if (hash->root.type == bfd_link_hash_defined
2393 || hash->root.type == bfd_link_hash_defweak)
2394 {
2395 sym_sec = hash->root.u.def.section;
2396 sym_name = hash->root.root.string;
2397 sym_value = hash->root.u.def.value;
2398 destination = (sym_value
2399 + sym_sec->output_offset
2400 + sym_sec->output_section->vma);
2401 }
2402 else
2403 {
2404 bfd_set_error (bfd_error_bad_value);
2405 free (internal_relocs);
2406 for (i = 0; i < bfd_count; i++)
2407 if (all_local_syms[i])
2408 free (all_local_syms[i]);
2409 free (all_local_syms);
2410 goto error_return;
2411 }
2412 }
2413
2414 args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
2415 sym_name, false, false);
2416
2417 /* Get both caller and callee argument information. */
2418 if (args_hash == NULL)
2419 callee_args = 0;
2420 else
2421 callee_args = args_hash->arg_bits;
2422
2423 /* For calls get the caller's bits from the addend of
2424 the call relocation. For PLABELS the caller's bits
2425 are assumed to have all args & return values in general
2426 registers (0x155). */
2427 if (r_type == R_PARISC_PCREL17F)
2428 caller_args = HPPA_R_ARG_RELOC (irela->r_addend);
2429 else
2430 caller_args = 0x155;
2431
2432 /* Now determine where the call point is. */
2433 location = (section->output_offset
2434 + section->output_section->vma
2435 + irela->r_offset);
2436
2437 /* We only care about the destination for PCREL function
2438 calls (eg. we don't care for PLABELS). */
2439 if (r_type != R_PARISC_PCREL17F)
2440 location = destination;
2441
2442 /* Determine what (if any) linker stub is needed and its
2443 size (in bytes). */
2444 size_of_stub = elf32_hppa_size_of_stub (callee_args,
2445 caller_args,
2446 location,
2447 destination,
2448 sym_name);
2449 if (size_of_stub != 0)
2450 {
2451 char *stub_name;
2452 unsigned int len;
2453
2454 /* Get the name of this stub. */
2455 len = strlen (sym_name);
2456 len += 23;
2457
2458 stub_name = bfd_malloc (len);
2459 if (!stub_name)
2460 {
2461 /* Because sym_name was mallocd above for local
2462 symbols. */
2463 if (r_index < symtab_hdr->sh_info)
2464 free (new_name);
2465
2466 free (internal_relocs);
2467 for (i = 0; i < bfd_count; i++)
2468 if (all_local_syms[i])
2469 free (all_local_syms[i]);
2470 free (all_local_syms);
2471 goto error_return;
2472 }
2473 elf32_hppa_name_of_stub (caller_args, callee_args,
2474 location, destination, stub_name);
2475 strcat (stub_name + 22, sym_name);
2476
2477 /* Because sym_name was malloced above for local symbols. */
2478 if (r_index < symtab_hdr->sh_info)
2479 free (new_name);
2480
2481 stub_hash
2482 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
2483 false, false);
2484 if (stub_hash != NULL)
2485 {
2486 /* The proper stub has already been created, nothing
2487 else to do. */
2488 free (stub_name);
2489 }
2490 else
2491 {
2492 bfd_set_section_size (stub_bfd, stub_sec,
2493 (bfd_section_size (stub_bfd,
2494 stub_sec)
2495 + size_of_stub));
2496
2497 /* Enter this entry into the linker stub hash table. */
2498 stub_hash
2499 = elf32_hppa_stub_hash_lookup (stub_hash_table,
2500 stub_name, true, true);
2501 if (stub_hash == NULL)
2502 {
2503 free (stub_name);
2504 free (internal_relocs);
2505 for (i = 0; i < bfd_count; i++)
2506 if (all_local_syms[i])
2507 free (all_local_syms[i]);
2508 free (all_local_syms);
2509 goto error_return;
2510 }
2511
2512 /* We'll need these to determine the address that the
2513 stub will branch to. */
2514 stub_hash->target_value = sym_value;
2515 stub_hash->target_section = sym_sec;
2516 }
2517 free (stub_name);
2518 }
2519 }
2520 /* We're done with the internal relocs, free them. */
2521 free (internal_relocs);
2522 }
2523 }
2524 /* We're done with the local symbols, free them. */
2525 for (i = 0; i < bfd_count; i++)
2526 if (all_local_syms[i])
2527 free (all_local_syms[i]);
2528 free (all_local_syms);
2529 return true;
2530
2531 error_return:
2532 /* Return gracefully, avoiding dangling references to the hash tables. */
2533 if (stub_hash_table)
2534 {
2535 elf32_hppa_hash_table(link_info)->stub_hash_table = NULL;
2536 free (stub_hash_table);
2537 }
2538 if (args_hash_table)
2539 {
2540 elf32_hppa_hash_table(link_info)->args_hash_table = NULL;
2541 free (args_hash_table);
2542 }
2543 /* Set the size of the stub section to zero since we're never going
2544 to create them. Avoids losing when we try to get its contents
2545 too. */
2546 bfd_set_section_size (stub_bfd, stub_sec, 0);
2547 return false;
2548 }
2549
2550 /* Misc BFD support code. */
2551 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
2552 #define bfd_elf32_bfd_is_local_label_name hppa_elf_is_local_label_name
2553 #define elf_info_to_howto elf_hppa_info_to_howto
2554 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
2555
2556 /* Symbol extension stuff. */
2557 #define bfd_elf32_set_section_contents elf32_hppa_set_section_contents
2558 #define elf_backend_symbol_table_processing \
2559 elf32_hppa_backend_symbol_table_processing
2560 #define elf_backend_begin_write_processing \
2561 elf32_hppa_backend_begin_write_processing
2562 #define elf_backend_final_write_processing \
2563 elf32_hppa_backend_final_write_processing
2564
2565 /* Stuff for the BFD linker. */
2566 #define elf_backend_relocate_section elf32_hppa_relocate_section
2567 #define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
2568 #define elf_backend_link_output_symbol_hook \
2569 elf32_hppa_link_output_symbol_hook
2570 #define bfd_elf32_bfd_link_hash_table_create \
2571 elf32_hppa_link_hash_table_create
2572
2573 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
2574 #define TARGET_BIG_NAME "elf32-hppa"
2575 #define ELF_ARCH bfd_arch_hppa
2576 #define ELF_MACHINE_CODE EM_PARISC
2577 #define ELF_MAXPAGESIZE 0x1000
2578
2579 #include "elf32-target.h"
This page took 0.081297 seconds and 4 git commands to generate.