* Makefile.in (all-bootstrap): Added all-texinfo and all-zlib.
[binutils.git] / bfd / elf32-hppa.c
blobb645b5fdb2de3027d74f23f07d7189e275bdc595
1 /* BFD back-end for HP PA-RISC ELF files.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 Original code by
6 Center for Software Science
7 Department of Computer Science
8 University of Utah
9 Largely rewritten by Alan Modra <alan@linuxcare.com.au>
11 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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 #include "elf32-hppa.h"
39 /* We use two hash tables to hold information for linking PA ELF objects.
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.
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
47 necessary to build the linker stubs during a link.
49 There are a number of different stubs generated by the linker.
51 Long branch stub:
52 : ldil LR'X,%r1
53 : be,n RR'X(%sr4,%r1)
55 PIC long branch stub:
56 : b,l .+8,%r1
57 : addil L'X - ($PIC_pcrel$0 - 4),%r1
58 : be,n R'X - ($PIC_pcrel$0 - 8)(%sr4,%r1)
60 Import stub to call shared library routine from normal object file
61 (single sub-space version)
62 : addil L'lt_ptr+ltoff,%dp ; get procedure entry point
63 : ldw R'lt_ptr+ltoff(%r1),%r21
64 : bv %r0(%r21)
65 : ldw R'lt_ptr+ltoff+4(%r1),%r19 ; get new dlt value.
67 Import stub to call shared library routine from shared library
68 (single sub-space version)
69 : addil L'ltoff,%r19 ; get procedure entry point
70 : ldw R'ltoff(%r1),%r21
71 : bv %r0(%r21)
72 : ldw R'ltoff+4(%r1),%r19 ; get new dlt value.
74 Import stub to call shared library routine from normal object file
75 (multiple sub-space support)
76 : addil L'lt_ptr+ltoff,%dp ; get procedure entry point
77 : ldw R'lt_ptr+ltoff(%r1),%r21
78 : ldw R'lt_ptr+ltoff+4(%r1),%r19 ; get new dlt value.
79 : ldsid (%r21),%r1
80 : mtsp %r1,%sr0
81 : be 0(%sr0,%r21) ; branch to target
82 : stw %rp,-24(%sp) ; save rp
84 Import stub to call shared library routine from shared library
85 (multiple sub-space support)
86 : addil L'ltoff,%r19 ; get procedure entry point
87 : ldw R'ltoff(%r1),%r21
88 : ldw R'ltoff+4(%r1),%r19 ; get new dlt value.
89 : ldsid (%r21),%r1
90 : mtsp %r1,%sr0
91 : be 0(%sr0,%r21) ; branch to target
92 : stw %rp,-24(%sp) ; save rp
94 Export stub to return from shared lib routine (multiple sub-space support)
95 One of these is created for each exported procedure in a shared
96 library (and stored in the shared lib). Shared lib routines are
97 called via the first instruction in the export stub so that we can
98 do an inter-space return. Not required for single sub-space.
99 : bl,n X,%rp ; trap the return
100 : nop
101 : ldw -24(%sp),%rp ; restore the original rp
102 : ldsid (%rp),%r1
103 : mtsp %r1,%sr0
104 : be,n 0(%sr0,%rp) ; inter-space return
107 #define PLT_ENTRY_SIZE 8
108 #define GOT_ENTRY_SIZE 4
109 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
111 /* Section name for stubs is the associated section name plus this
112 string. */
113 #define STUB_SUFFIX ".stub"
115 /* Setting the following non-zero makes all long branch stubs
116 generated during a shared link of the PIC variety. This saves on
117 relocs, but costs one extra instruction per stub. */
118 #ifndef LONG_BRANCH_PIC_IN_SHLIB
119 #define LONG_BRANCH_PIC_IN_SHLIB 1
120 #endif
122 /* We don't need to copy any PC- or GP-relative dynamic relocs into a
123 shared object's dynamic section. */
124 #ifndef RELATIVE_DYNAMIC_RELOCS
125 #define RELATIVE_DYNAMIC_RELOCS 0
126 #endif
129 enum elf32_hppa_stub_type {
130 hppa_stub_long_branch,
131 hppa_stub_long_branch_shared,
132 hppa_stub_import,
133 hppa_stub_import_shared,
134 hppa_stub_export,
135 hppa_stub_none
139 struct elf32_hppa_stub_hash_entry {
141 /* Base hash table entry structure. */
142 struct bfd_hash_entry root;
144 /* The stub section. */
145 asection *stub_sec;
147 #if ! LONG_BRANCH_PIC_IN_SHLIB
148 /* It's associated reloc section. */
149 asection *reloc_sec;
150 #endif
152 /* Offset within stub_sec of the beginning of this stub. */
153 bfd_vma stub_offset;
155 /* Given the symbol's value and its section we can determine its final
156 value when building the stubs (so the stub knows where to jump. */
157 bfd_vma target_value;
158 asection *target_section;
160 enum elf32_hppa_stub_type stub_type;
162 /* The symbol table entry, if any, that this was derived from. */
163 struct elf32_hppa_link_hash_entry *h;
165 /* Where this stub is being called from. */
166 asection *input_section;
170 struct elf32_hppa_link_hash_entry {
172 struct elf_link_hash_entry elf;
174 /* A pointer to the most recently used stub hash entry against this
175 symbol. */
176 struct elf32_hppa_stub_hash_entry *stub_cache;
178 #if ! LONG_BRANCH_PIC_IN_SHLIB
179 /* Used to track whether we have allocated space for a long branch
180 stub relocation for this symbol in the given section. */
181 asection *stub_reloc_sec;
182 #endif
184 #if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
185 /* Used to count relocations for delayed sizing of relocation
186 sections. */
187 struct elf32_hppa_dyn_reloc_entry {
189 /* Next relocation in the chain. */
190 struct elf32_hppa_dyn_reloc_entry *next;
192 /* The section in dynobj. */
193 asection *section;
195 /* Number of relocs copied in this section. */
196 bfd_size_type count;
197 } *reloc_entries;
198 #endif
200 /* Set during a static link if we detect a function is PIC. */
201 boolean pic_call;
205 struct elf32_hppa_link_hash_table {
207 /* The main hash table. */
208 struct elf_link_hash_table root;
210 /* The stub hash table. */
211 struct bfd_hash_table stub_hash_table;
213 /* Linker stub bfd. */
214 bfd *stub_bfd;
216 /* Whether we support multiple sub-spaces for shared libs. */
217 boolean multi_subspace;
219 /* Linker call-backs. */
220 asection * (*add_stub_section) PARAMS ((const char *, asection *));
221 void (*layout_sections_again) PARAMS ((void));
223 /* Arrays to keep track of which stub sections have been created. */
224 asection **stub_section_created;
225 #if ! LONG_BRANCH_PIC_IN_SHLIB
226 asection **reloc_section_created;
227 #endif
228 int first_init_sec;
229 int first_fini_sec;
231 /* Current offsets in the stub sections. */
232 bfd_vma *offset;
234 /* Short-cuts to get to dynamic linker sections. */
235 asection *sgot;
236 asection *srelgot;
237 asection *splt;
238 asection *srelplt;
239 asection *sdynbss;
240 asection *srelbss;
244 /* Functions named elf32_hppa_* are called by external routines, other
245 functions are only called locally. elf32_hppa_* functions appear
246 in this file more or less in the order in which they are called
247 from external routines. eg. elf32_hppa_check_relocs is called
248 early in the link process, elf32_hppa_finish_dynamic_sections is
249 one of the last functions. */
252 /* Various hash macros and functions. */
253 #define hppa_link_hash_table(p) \
254 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
256 #define hppa_stub_hash_lookup(table, string, create, copy) \
257 ((struct elf32_hppa_stub_hash_entry *) \
258 bfd_hash_lookup ((table), (string), (create), (copy)))
260 static struct bfd_hash_entry *stub_hash_newfunc
261 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
263 static struct bfd_hash_entry *hppa_link_hash_newfunc
264 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
266 static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
267 PARAMS ((bfd *));
270 /* Stub handling functions. */
271 static char *hppa_stub_name
272 PARAMS ((const asection *, const asection *,
273 const struct elf32_hppa_link_hash_entry *,
274 const Elf_Internal_Rela *));
276 static struct elf32_hppa_stub_hash_entry *hppa_get_stub_entry
277 PARAMS ((const asection *, const asection *,
278 struct elf32_hppa_link_hash_entry *,
279 const Elf_Internal_Rela *, struct bfd_link_info *));
281 static struct elf32_hppa_stub_hash_entry *hppa_add_stub
282 PARAMS ((const char *, asection *, unsigned int,
283 struct bfd_link_info *));
285 static enum elf32_hppa_stub_type hppa_type_of_stub
286 PARAMS ((asection *, const Elf_Internal_Rela *,
287 struct elf32_hppa_link_hash_entry *, bfd_vma));
289 static boolean hppa_build_one_stub
290 PARAMS ((struct bfd_hash_entry *, PTR));
292 static boolean hppa_size_one_stub
293 PARAMS ((struct bfd_hash_entry *, PTR));
296 /* BFD and elf backend functions. */
297 static boolean elf32_hppa_object_p PARAMS ((bfd *));
299 static boolean elf32_hppa_add_symbol_hook
300 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
301 const char **, flagword *, asection **, bfd_vma *));
303 static boolean elf32_hppa_create_dynamic_sections
304 PARAMS ((bfd *, struct bfd_link_info *));
306 static boolean elf32_hppa_check_relocs
307 PARAMS ((bfd *, struct bfd_link_info *,
308 asection *, const Elf_Internal_Rela *));
310 static asection *elf32_hppa_gc_mark_hook
311 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
312 struct elf_link_hash_entry *, Elf_Internal_Sym *));
314 static boolean elf32_hppa_gc_sweep_hook
315 PARAMS ((bfd *, struct bfd_link_info *,
316 asection *, const Elf_Internal_Rela *));
318 static boolean elf32_hppa_adjust_dynamic_symbol
319 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
321 static boolean hppa_handle_PIC_calls
322 PARAMS ((struct elf_link_hash_entry *, PTR));
324 #if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
325 static boolean hppa_discard_copies
326 PARAMS ((struct elf_link_hash_entry *, PTR));
327 #endif
329 static boolean elf32_hppa_size_dynamic_sections
330 PARAMS ((bfd *, struct bfd_link_info *));
332 static bfd_reloc_status_type final_link_relocate
333 PARAMS ((asection *, bfd_byte *, const Elf_Internal_Rela *,
334 bfd_vma, struct bfd_link_info *, asection *,
335 struct elf32_hppa_link_hash_entry *));
337 static boolean elf32_hppa_relocate_section
338 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
339 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
341 static boolean elf32_hppa_finish_dynamic_symbol
342 PARAMS ((bfd *, struct bfd_link_info *,
343 struct elf_link_hash_entry *, Elf_Internal_Sym *));
345 static boolean elf32_hppa_finish_dynamic_sections
346 PARAMS ((bfd *, struct bfd_link_info *));
348 static int elf32_hppa_elf_get_symbol_type
349 PARAMS ((Elf_Internal_Sym *, int));
352 /* Assorted hash table functions. */
354 /* Initialize an entry in the stub hash table. */
356 static struct bfd_hash_entry *
357 stub_hash_newfunc (entry, table, string)
358 struct bfd_hash_entry *entry;
359 struct bfd_hash_table *table;
360 const char *string;
362 struct elf32_hppa_stub_hash_entry *ret;
364 ret = (struct elf32_hppa_stub_hash_entry *) entry;
366 /* Allocate the structure if it has not already been allocated by a
367 subclass. */
368 if (ret == NULL)
370 ret = ((struct elf32_hppa_stub_hash_entry *)
371 bfd_hash_allocate (table,
372 sizeof (struct elf32_hppa_stub_hash_entry)));
373 if (ret == NULL)
374 return NULL;
377 /* Call the allocation method of the superclass. */
378 ret = ((struct elf32_hppa_stub_hash_entry *)
379 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
381 if (ret)
383 /* Initialize the local fields. */
384 ret->stub_sec = NULL;
385 #if ! LONG_BRANCH_PIC_IN_SHLIB
386 ret->reloc_sec = NULL;
387 #endif
388 ret->stub_offset = 0;
389 ret->target_value = 0;
390 ret->target_section = NULL;
391 ret->stub_type = hppa_stub_long_branch;
392 ret->h = NULL;
393 ret->input_section = NULL;
396 return (struct bfd_hash_entry *) ret;
400 /* Initialize an entry in the link hash table. */
402 static struct bfd_hash_entry *
403 hppa_link_hash_newfunc (entry, table, string)
404 struct bfd_hash_entry *entry;
405 struct bfd_hash_table *table;
406 const char *string;
408 struct elf32_hppa_link_hash_entry *ret;
410 ret = (struct elf32_hppa_link_hash_entry *) entry;
412 /* Allocate the structure if it has not already been allocated by a
413 subclass. */
414 if (ret == NULL)
416 ret = ((struct elf32_hppa_link_hash_entry *)
417 bfd_hash_allocate (table,
418 sizeof (struct elf32_hppa_link_hash_entry)));
419 if (ret == NULL)
420 return NULL;
423 /* Call the allocation method of the superclass. */
424 ret = ((struct elf32_hppa_link_hash_entry *)
425 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
426 table, string));
428 if (ret)
430 /* Initialize the local fields. */
431 #if ! LONG_BRANCH_PIC_IN_SHLIB
432 ret->stub_reloc_sec = NULL;
433 #endif
434 ret->stub_cache = NULL;
435 #if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
436 ret->reloc_entries = NULL;
437 #endif
438 ret->pic_call = 0;
441 return (struct bfd_hash_entry *) ret;
445 /* Create the derived linker hash table. The PA ELF port uses the derived
446 hash table to keep information specific to the PA ELF linker (without
447 using static variables). */
449 static struct bfd_link_hash_table *
450 elf32_hppa_link_hash_table_create (abfd)
451 bfd *abfd;
453 struct elf32_hppa_link_hash_table *ret;
455 ret = ((struct elf32_hppa_link_hash_table *) bfd_alloc (abfd, sizeof (*ret)));
456 if (ret == NULL)
457 return NULL;
459 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd, hppa_link_hash_newfunc))
461 bfd_release (abfd, ret);
462 return NULL;
465 /* Init the stub hash table too. */
466 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc))
467 return NULL;
469 ret->stub_bfd = NULL;
470 ret->multi_subspace = 0;
471 ret->add_stub_section = NULL;
472 ret->layout_sections_again = NULL;
473 ret->stub_section_created = NULL;
474 #if ! LONG_BRANCH_PIC_IN_SHLIB
475 ret->reloc_section_created = NULL;
476 #endif
477 ret->first_init_sec = 0;
478 ret->first_fini_sec = 0;
479 ret->offset = NULL;
480 ret->sgot = NULL;
481 ret->srelgot = NULL;
482 ret->splt = NULL;
483 ret->srelplt = NULL;
484 ret->sdynbss = NULL;
485 ret->srelbss = NULL;
487 return &ret->root.root;
491 /* Build a name for an entry in the stub hash table. */
493 static char *
494 hppa_stub_name (input_section, sym_sec, hash, rel)
495 const asection *input_section;
496 const asection *sym_sec;
497 const struct elf32_hppa_link_hash_entry *hash;
498 const Elf_Internal_Rela *rel;
500 char *stub_name;
501 unsigned int len;
503 if (hash)
505 len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
506 stub_name = bfd_malloc (len);
507 if (stub_name != NULL)
509 sprintf (stub_name, "%08x_%s+%x",
510 input_section->id & 0xffffffff,
511 hash->elf.root.root.string,
512 (int) rel->r_addend & 0xffffffff);
515 else
517 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
518 stub_name = bfd_malloc (len);
519 if (stub_name != NULL)
521 sprintf (stub_name, "%08x_%x:%x+%x",
522 input_section->id & 0xffffffff,
523 sym_sec->id & 0xffffffff,
524 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
525 (int) rel->r_addend & 0xffffffff);
528 return stub_name;
532 /* Look up an entry in the stub hash. Stub entries are cached because
533 creating the stub name takes a bit of time. */
535 static struct elf32_hppa_stub_hash_entry *
536 hppa_get_stub_entry (input_section, sym_sec, hash, rel, info)
537 const asection *input_section;
538 const asection *sym_sec;
539 struct elf32_hppa_link_hash_entry *hash;
540 const Elf_Internal_Rela *rel;
541 struct bfd_link_info *info;
543 struct elf32_hppa_stub_hash_entry *stub_entry;
545 if (hash != NULL && hash->stub_cache != NULL
546 && hash->stub_cache->h == hash
547 && hash->stub_cache->input_section == input_section)
549 stub_entry = hash->stub_cache;
551 else
553 struct bfd_hash_table *stub_hash_table;
554 char *stub_name;
556 stub_name = hppa_stub_name (input_section, sym_sec, hash, rel);
557 if (stub_name == NULL)
558 return NULL;
560 stub_hash_table = &hppa_link_hash_table (info)->stub_hash_table;
562 stub_entry = hppa_stub_hash_lookup (stub_hash_table, stub_name,
563 false, false);
564 if (stub_entry == NULL)
566 if (hash == NULL || hash->elf.root.type != bfd_link_hash_undefweak)
567 (*_bfd_error_handler) (_("%s(%s+0x%lx): cannot find stub entry %s"),
568 bfd_get_filename (input_section->owner),
569 input_section->name,
570 (long) rel->r_offset,
571 stub_name);
573 else
575 if (hash != NULL)
576 hash->stub_cache = stub_entry;
579 free (stub_name);
582 return stub_entry;
586 /* Add a new stub entry to the stub hash. Not all fields of the new
587 stub entry are initialised. */
589 static struct elf32_hppa_stub_hash_entry *
590 hppa_add_stub (stub_name, section, sec_count, info)
591 const char *stub_name;
592 asection *section;
593 unsigned int sec_count;
594 struct bfd_link_info *info;
596 asection *stub_sec;
597 #if ! LONG_BRANCH_PIC_IN_SHLIB
598 asection *reloc_sec;
599 #endif
600 struct elf32_hppa_stub_hash_entry *stub_entry;
601 struct elf32_hppa_link_hash_table *hplink;
603 hplink = hppa_link_hash_table (info);
604 stub_sec = hplink->stub_section_created[sec_count];
605 if (stub_sec == NULL)
607 int special_sec = 0;
609 /* We only want one stub for .init and .fini because glibc
610 splits the _init and _fini functions into two parts. We
611 don't want to put a stub in the middle of a function.
612 It would be better to merge all the stub sections for an
613 output section if the output section + stubs is small enough.
614 This would fix the .init and .fini case and also allow stubs
615 to be merged. It's more linker work though. */
616 if (strncmp (section->name, ".init", 5) == 0)
618 stub_sec = hplink->stub_section_created[hplink->first_init_sec];
619 special_sec = 1;
621 else if (strncmp (section->name, ".fini", 5) == 0)
623 stub_sec = hplink->stub_section_created[hplink->first_fini_sec];
624 special_sec = 2;
626 if (stub_sec == NULL)
628 int len;
629 char *s_name;
631 len = strlen (section->name) + sizeof (STUB_SUFFIX);
632 s_name = bfd_alloc (hplink->stub_bfd, len);
633 if (s_name == NULL)
634 return NULL;
636 strcpy (s_name, section->name);
637 strcpy (s_name + len - sizeof (STUB_SUFFIX), STUB_SUFFIX);
638 stub_sec = (*hplink->add_stub_section) (s_name, section);
639 if (stub_sec == NULL)
640 return NULL;
642 if (special_sec != 0)
644 if (special_sec == 1)
645 hplink->first_init_sec = sec_count;
646 else
647 hplink->first_fini_sec = sec_count;
650 hplink->stub_section_created[sec_count] = stub_sec;
653 #if ! LONG_BRANCH_PIC_IN_SHLIB
654 reloc_sec = hplink->reloc_section_created[sec_count];
655 if (reloc_sec == NULL && info->shared)
657 char *name;
659 name = bfd_malloc (sizeof ".rela" + strlen (stub_sec->name));
660 if (name == NULL)
661 return NULL;
662 strcpy (name, ".rela");
663 strcpy (name + sizeof ".rela" - 1, stub_sec->name);
664 reloc_sec = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
665 name);
666 hplink->reloc_section_created[sec_count] = reloc_sec;
667 free (name);
669 #endif
671 /* Enter this entry into the linker stub hash table. */
672 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table, stub_name,
673 true, false);
674 if (stub_entry == NULL)
676 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
677 bfd_get_filename (section->owner),
678 stub_name);
679 return NULL;
682 stub_entry->stub_sec = stub_sec;
683 #if ! LONG_BRANCH_PIC_IN_SHLIB
684 stub_entry->reloc_sec = reloc_sec;
685 #endif
686 stub_entry->stub_offset = 0;
687 stub_entry->input_section = section;
688 return stub_entry;
692 /* Determine the type of stub needed, if any, for a call. */
694 static enum elf32_hppa_stub_type
695 hppa_type_of_stub (input_sec, rel, hash, destination)
696 asection *input_sec;
697 const Elf_Internal_Rela *rel;
698 struct elf32_hppa_link_hash_entry *hash;
699 bfd_vma destination;
701 bfd_vma location;
702 bfd_vma branch_offset;
703 bfd_vma max_branch_offset;
704 unsigned int r_type;
706 if (hash != NULL
707 && (((hash->elf.root.type == bfd_link_hash_defined
708 || hash->elf.root.type == bfd_link_hash_defweak)
709 && hash->elf.root.u.def.section->output_section == NULL)
710 || hash->elf.root.type == bfd_link_hash_undefweak
711 || hash->elf.root.type == bfd_link_hash_undefined
712 || hash->pic_call))
714 /* If output_section is NULL, then it's a symbol defined in a
715 shared library. We will need an import stub. Decide between
716 hppa_stub_import and hppa_stub_import_shared later.
717 For shared links we need stubs for undefined syms too; They
718 will presumably be resolved by the dynamic linker. */
719 return hppa_stub_import;
722 /* Determine where the call point is. */
723 location = (input_sec->output_offset
724 + input_sec->output_section->vma
725 + rel->r_offset);
727 branch_offset = destination - location - 8;
728 r_type = ELF32_R_TYPE (rel->r_info);
730 /* Determine if a long branch stub is needed. parisc branch offsets
731 are relative to the second instruction past the branch, ie. +8
732 bytes on from the branch instruction location. The offset is
733 signed and counts in units of 4 bytes. */
734 if (r_type == (unsigned int) R_PARISC_PCREL17F)
736 max_branch_offset = (1 << (17-1)) << 2;
738 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
740 max_branch_offset = (1 << (12-1)) << 2;
742 else /* R_PARISC_PCREL22F */
744 max_branch_offset = (1 << (22-1)) << 2;
747 if (branch_offset + max_branch_offset >= 2*max_branch_offset)
749 #if 0
750 if (hash != NULL
751 && hash->elf.dynindx != -1
752 && hash->elf.plt.offset != (bfd_vma) -1)
754 /* If we are doing a shared link and find we need a long
755 branch stub, then go via the .plt if possible. */
756 return hppa_stub_import;
758 else
759 #endif
760 return hppa_stub_long_branch;
762 return hppa_stub_none;
766 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
767 IN_ARG contains the link info pointer. */
769 #define LDIL_R1 0x20200000 /* ldil LR'XXX,%r1 */
770 #define BE_SR4_R1 0xe0202002 /* be,n RR'XXX(%sr4,%r1) */
772 #define BL_R1 0xe8200000 /* b,l .+8,%r1 */
773 #define ADDIL_R1 0x28200000 /* addil L'XXX,%r1,%r1 */
774 #define DEPI_R1 0xd4201c1e /* depi 0,31,2,%r1 */
776 #define ADDIL_DP 0x2b600000 /* addil L'XXX,%dp,%r1 */
777 #define LDW_R1_R21 0x48350000 /* ldw R'XXX(%sr0,%r1),%r21 */
778 #define BV_R0_R21 0xeaa0c000 /* bv %r0(%r21) */
779 #define LDW_R1_R19 0x48330000 /* ldw R'XXX(%sr0,%r1),%r19 */
781 #define ADDIL_R19 0x2a600000 /* addil L'XXX,%r19,%r1 */
782 #define LDW_R1_DP 0x483b0000 /* ldw R'XXX(%sr0,%r1),%dp */
784 #define LDSID_R21_R1 0x02a010a1 /* ldsid (%sr0,%r21),%r1 */
785 #define MTSP_R1 0x00011820 /* mtsp %r1,%sr0 */
786 #define BE_SR0_R21 0xe2a00000 /* be 0(%sr0,%r21) */
787 #define STW_RP 0x6bc23fd1 /* stw %rp,-24(%sr0,%sp) */
789 #define BL_RP 0xe8400002 /* b,l,n XXX,%rp */
790 #define NOP 0x08000240 /* nop */
791 #define LDW_RP 0x4bc23fd1 /* ldw -24(%sr0,%sp),%rp */
792 #define LDSID_RP_R1 0x004010a1 /* ldsid (%sr0,%rp),%r1 */
793 #define BE_SR0_RP 0xe0400002 /* be,n 0(%sr0,%rp) */
795 #ifndef R19_STUBS
796 #define R19_STUBS 1
797 #endif
799 #if R19_STUBS
800 #define LDW_R1_DLT LDW_R1_R19
801 #else
802 #define LDW_R1_DLT LDW_R1_DP
803 #endif
805 static boolean
806 hppa_build_one_stub (gen_entry, in_arg)
807 struct bfd_hash_entry *gen_entry;
808 PTR in_arg;
810 struct elf32_hppa_stub_hash_entry *stub_entry;
811 struct bfd_link_info *info;
812 struct elf32_hppa_link_hash_table *hplink;
813 asection *stub_sec;
814 bfd *stub_bfd;
815 bfd_byte *loc;
816 bfd_vma sym_value;
817 unsigned int insn;
818 int size;
820 /* Massage our args to the form they really have. */
821 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
822 info = (struct bfd_link_info *) in_arg;
824 hplink = hppa_link_hash_table (info);
825 stub_sec = stub_entry->stub_sec;
827 /* Make a note of the offset within the stubs for this entry. */
828 stub_entry->stub_offset = hplink->offset[stub_sec->index];
829 loc = stub_sec->contents + stub_entry->stub_offset;
831 stub_bfd = stub_sec->owner;
833 switch (stub_entry->stub_type)
835 case hppa_stub_long_branch:
836 /* Create the long branch. A long branch is formed with "ldil"
837 loading the upper bits of the target address into a register,
838 then branching with "be" which adds in the lower bits.
839 The "be" has its delay slot nullified. */
840 sym_value = (stub_entry->target_value
841 + stub_entry->target_section->output_offset
842 + stub_entry->target_section->output_section->vma);
844 insn = hppa_rebuild_insn ((int) LDIL_R1,
845 hppa_field_adjust (sym_value, 0, e_lrsel),
846 21);
847 bfd_put_32 (stub_bfd, insn, loc);
849 insn = hppa_rebuild_insn ((int) BE_SR4_R1,
850 hppa_field_adjust (sym_value, 0, e_rrsel) >> 2,
851 17);
852 bfd_put_32 (stub_bfd, insn, loc + 4);
854 #if ! LONG_BRANCH_PIC_IN_SHLIB
855 if (info->shared)
857 /* Output a dynamic relocation for this stub. We only
858 output one PCREL21L reloc per stub, trusting that the
859 dynamic linker will also fix the implied PCREL17R for the
860 second instruction. PCREL21L dynamic relocs had better
861 never be emitted for some other purpose... */
862 asection *srel;
863 Elf_Internal_Rela outrel;
865 if (stub_entry->h == NULL)
867 (*_bfd_error_handler)
868 (_("%s(%s+0x%lx): cannot relocate %s, recompile with -ffunction-sections"),
869 bfd_get_filename (stub_entry->target_section->owner),
870 stub_sec->name,
871 (long) stub_entry->stub_offset,
872 stub_entry->root.string);
873 bfd_set_error (bfd_error_bad_value);
874 return false;
877 srel = stub_entry->reloc_sec;
878 if (srel == NULL)
880 (*_bfd_error_handler)
881 (_("Could not find relocation section for %s"),
882 stub_sec->name);
883 bfd_set_error (bfd_error_bad_value);
884 return false;
887 outrel.r_offset = (stub_entry->stub_offset
888 + stub_sec->output_offset
889 + stub_sec->output_section->vma);
890 outrel.r_info = ELF32_R_INFO (0, R_PARISC_PCREL21L);
891 outrel.r_addend = sym_value;
892 bfd_elf32_swap_reloca_out (stub_sec->output_section->owner,
893 &outrel,
894 ((Elf32_External_Rela *)
895 srel->contents + srel->reloc_count));
896 ++srel->reloc_count;
898 #endif
899 size = 8;
900 break;
902 case hppa_stub_long_branch_shared:
903 /* Branches are relative. This is where we are going to. */
904 sym_value = (stub_entry->target_value
905 + stub_entry->target_section->output_offset
906 + stub_entry->target_section->output_section->vma);
908 /* And this is where we are coming from, more or less. */
909 sym_value -= (stub_entry->stub_offset
910 + stub_sec->output_offset
911 + stub_sec->output_section->vma);
913 bfd_put_32 (stub_bfd, (unsigned int) BL_R1, loc);
914 insn = hppa_rebuild_insn ((int) ADDIL_R1,
915 hppa_field_adjust (sym_value, -8, e_lsel),
916 21);
917 bfd_put_32 (stub_bfd, insn, loc + 4);
919 insn = hppa_rebuild_insn ((int) BE_SR4_R1,
920 hppa_field_adjust (sym_value, -8, e_rsel) >> 2,
921 17);
922 bfd_put_32 (stub_bfd, insn, loc + 8);
923 size = 12;
924 break;
926 case hppa_stub_import:
927 case hppa_stub_import_shared:
928 sym_value = (stub_entry->h->elf.plt.offset
929 + hplink->splt->output_offset
930 + hplink->splt->output_section->vma
931 - elf_gp (hplink->splt->output_section->owner));
933 insn = ADDIL_DP;
934 #if R19_STUBS
935 if (stub_entry->stub_type == hppa_stub_import_shared)
936 insn = ADDIL_R19;
937 #endif
938 insn = hppa_rebuild_insn ((int) insn,
939 hppa_field_adjust (sym_value, 0, e_lsel),
940 21);
941 bfd_put_32 (stub_bfd, insn, loc);
943 insn = hppa_rebuild_insn ((int) LDW_R1_R21,
944 hppa_field_adjust (sym_value, 0, e_rsel),
945 14);
946 bfd_put_32 (stub_bfd, insn, loc + 4);
948 if (hplink->multi_subspace)
950 insn = hppa_rebuild_insn ((int) LDW_R1_DLT,
951 hppa_field_adjust (sym_value, 4, e_rsel),
952 14);
953 bfd_put_32 (stub_bfd, insn, loc + 8);
955 bfd_put_32 (stub_bfd, (unsigned int) LDSID_R21_R1, loc + 12);
956 bfd_put_32 (stub_bfd, (unsigned int) MTSP_R1, loc + 16);
957 bfd_put_32 (stub_bfd, (unsigned int) BE_SR0_R21, loc + 20);
958 bfd_put_32 (stub_bfd, (unsigned int) STW_RP, loc + 24);
960 size = 28;
962 else
964 bfd_put_32 (stub_bfd, (unsigned int) BV_R0_R21, loc + 8);
965 insn = hppa_rebuild_insn ((int) LDW_R1_DLT,
966 hppa_field_adjust (sym_value, 4, e_rsel),
967 14);
968 bfd_put_32 (stub_bfd, insn, loc + 12);
970 size = 16;
973 if (!info->shared
974 && stub_entry->h != NULL
975 && stub_entry->h->pic_call)
977 /* Build the .plt entry needed to call a PIC function from
978 statically linked code. We don't need any relocs. */
979 bfd *dynobj;
980 struct elf32_hppa_link_hash_entry *eh;
981 bfd_vma value;
983 dynobj = elf_hash_table (info)->dynobj;
984 eh = (struct elf32_hppa_link_hash_entry *) stub_entry->h;
986 BFD_ASSERT (eh->elf.root.type == bfd_link_hash_defined
987 || eh->elf.root.type == bfd_link_hash_defweak);
989 value = (eh->elf.root.u.def.value
990 + eh->elf.root.u.def.section->output_offset
991 + eh->elf.root.u.def.section->output_section->vma);
993 /* Fill in the entry in the procedure linkage table.
995 The format of a plt entry is
996 <funcaddr> <__gp>. */
998 bfd_put_32 (hplink->splt->owner, value,
999 hplink->splt->contents + eh->elf.plt.offset);
1000 value = elf_gp (hplink->splt->output_section->owner);
1001 bfd_put_32 (hplink->splt->owner, value,
1002 hplink->splt->contents + eh->elf.plt.offset + 4);
1004 break;
1006 case hppa_stub_export:
1007 /* Branches are relative. This is where we are going to. */
1008 sym_value = (stub_entry->target_value
1009 + stub_entry->target_section->output_offset
1010 + stub_entry->target_section->output_section->vma);
1012 /* And this is where we are coming from. */
1013 sym_value -= (stub_entry->stub_offset
1014 + stub_sec->output_offset
1015 + stub_sec->output_section->vma);
1017 if (sym_value - 8 + 0x40000 >= 0x80000)
1019 (*_bfd_error_handler)
1020 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
1021 bfd_get_filename (stub_entry->target_section->owner),
1022 stub_sec->name,
1023 (long) stub_entry->stub_offset,
1024 stub_entry->root.string);
1025 bfd_set_error (bfd_error_bad_value);
1026 return false;
1029 insn = hppa_rebuild_insn ((int) BL_RP,
1030 hppa_field_adjust (sym_value, -8, e_fsel) >> 2,
1031 17);
1032 bfd_put_32 (stub_bfd, insn, loc);
1034 bfd_put_32 (stub_bfd, (unsigned int) NOP, loc + 4);
1035 bfd_put_32 (stub_bfd, (unsigned int) LDW_RP, loc + 8);
1036 bfd_put_32 (stub_bfd, (unsigned int) LDSID_RP_R1, loc + 12);
1037 bfd_put_32 (stub_bfd, (unsigned int) MTSP_R1, loc + 16);
1038 bfd_put_32 (stub_bfd, (unsigned int) BE_SR0_RP, loc + 20);
1040 /* Point the function symbol at the stub. */
1041 stub_entry->h->elf.root.u.def.section = stub_sec;
1042 stub_entry->h->elf.root.u.def.value = hplink->offset[stub_sec->index];
1044 size = 24;
1045 break;
1047 default:
1048 BFD_FAIL ();
1049 return false;
1052 hplink->offset[stub_sec->index] += size;
1053 return true;
1056 #undef LDIL_R1
1057 #undef BE_SR4_R1
1058 #undef BL_R1
1059 #undef ADDIL_R1
1060 #undef DEPI_R1
1061 #undef ADDIL_DP
1062 #undef LDW_R1_R21
1063 #undef LDW_R1_DLT
1064 #undef LDW_R1_R19
1065 #undef ADDIL_R19
1066 #undef LDW_R1_DP
1067 #undef LDSID_R21_R1
1068 #undef MTSP_R1
1069 #undef BE_SR0_R21
1070 #undef STW_RP
1071 #undef BV_R0_R21
1072 #undef BL_RP
1073 #undef NOP
1074 #undef LDW_RP
1075 #undef LDSID_RP_R1
1076 #undef BE_SR0_RP
1079 /* As above, but don't actually build the stub. Just bump offset so
1080 we know stub section sizes. */
1082 static boolean
1083 hppa_size_one_stub (gen_entry, in_arg)
1084 struct bfd_hash_entry *gen_entry;
1085 PTR in_arg;
1087 struct elf32_hppa_stub_hash_entry *stub_entry;
1088 struct elf32_hppa_link_hash_table *hplink;
1089 int size;
1091 /* Massage our args to the form they really have. */
1092 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
1093 hplink = (struct elf32_hppa_link_hash_table *) in_arg;
1095 if (stub_entry->stub_type == hppa_stub_long_branch)
1096 size = 8;
1097 else if (stub_entry->stub_type == hppa_stub_long_branch_shared)
1098 size = 12;
1099 else if (stub_entry->stub_type == hppa_stub_export)
1100 size = 24;
1101 else /* hppa_stub_import or hppa_stub_import_shared */
1103 if (hplink->multi_subspace)
1104 size = 28;
1105 else
1106 size = 16;
1109 hplink->offset[stub_entry->stub_sec->index] += size;
1110 return true;
1114 /* Return nonzero if ABFD represents an HPPA ELF32 file.
1115 Additionally we set the default architecture and machine. */
1117 static boolean
1118 elf32_hppa_object_p (abfd)
1119 bfd *abfd;
1121 unsigned int flags = elf_elfheader (abfd)->e_flags;
1123 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
1125 case EFA_PARISC_1_0:
1126 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
1127 case EFA_PARISC_1_1:
1128 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
1129 case EFA_PARISC_2_0:
1130 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
1131 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
1132 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
1134 return true;
1138 /* Undo the generic ELF code's subtraction of section->vma from the
1139 value of each external symbol. */
1141 static boolean
1142 elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1143 bfd *abfd ATTRIBUTE_UNUSED;
1144 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1145 const Elf_Internal_Sym *sym ATTRIBUTE_UNUSED;
1146 const char **namep ATTRIBUTE_UNUSED;
1147 flagword *flagsp ATTRIBUTE_UNUSED;
1148 asection **secp;
1149 bfd_vma *valp;
1151 *valp += (*secp)->vma;
1152 return true;
1156 /* Create the .plt and .got sections, and set up our hash table
1157 short-cuts to various dynamic sections. */
1159 static boolean
1160 elf32_hppa_create_dynamic_sections (abfd, info)
1161 bfd *abfd;
1162 struct bfd_link_info *info;
1164 flagword flags;
1165 asection *s;
1166 struct elf32_hppa_link_hash_table *hplink;
1168 /* Don't try to create the .plt and .got twice. */
1169 hplink = hppa_link_hash_table (info);
1170 if (hplink->splt != NULL)
1171 return true;
1173 /* Call the generic code to do most of the work. */
1174 if (! _bfd_elf_create_dynamic_sections (abfd, info))
1175 return false;
1177 /* Our .plt just contains pointers. I suppose we should be using
1178 .plt.got but .plt.got doesn't make too much sense without a .plt
1179 section. Set the flags to say the .plt isn't executable. */
1180 s = bfd_get_section_by_name (abfd, ".plt");
1181 flags = bfd_get_section_flags (abfd, s);
1182 if (! bfd_set_section_flags (abfd, s, flags & ~SEC_CODE))
1183 return false;
1184 hplink->splt = s;
1185 hplink->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
1187 hplink->sgot = bfd_get_section_by_name (abfd, ".got");
1188 hplink->srelgot = bfd_make_section (abfd, ".rela.got");
1189 if (hplink->srelgot == NULL
1190 || ! bfd_set_section_flags (abfd, hplink->srelgot,
1191 (SEC_ALLOC
1192 | SEC_LOAD
1193 | SEC_HAS_CONTENTS
1194 | SEC_IN_MEMORY
1195 | SEC_LINKER_CREATED
1196 | SEC_READONLY))
1197 || ! bfd_set_section_alignment (abfd, hplink->srelgot, 2))
1198 return false;
1200 hplink->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
1201 hplink->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
1203 return true;
1207 /* Look through the relocs for a section during the first phase, and
1208 allocate space in the global offset table or procedure linkage
1209 table. At this point we haven't necessarily read all the input
1210 files. */
1212 static boolean
1213 elf32_hppa_check_relocs (abfd, info, sec, relocs)
1214 bfd *abfd;
1215 struct bfd_link_info *info;
1216 asection *sec;
1217 const Elf_Internal_Rela *relocs;
1219 bfd *dynobj;
1220 Elf_Internal_Shdr *symtab_hdr;
1221 struct elf_link_hash_entry **sym_hashes;
1222 bfd_signed_vma *local_got_refcounts;
1223 const Elf_Internal_Rela *rel;
1224 const Elf_Internal_Rela *rel_end;
1225 struct elf32_hppa_link_hash_table *hplink;
1226 asection *sreloc;
1227 asection *stubreloc;
1229 if (info->relocateable)
1230 return true;
1232 hplink = hppa_link_hash_table (info);
1233 dynobj = elf_hash_table (info)->dynobj;
1234 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1235 sym_hashes = elf_sym_hashes (abfd);
1236 local_got_refcounts = elf_local_got_refcounts (abfd);
1237 sreloc = NULL;
1238 stubreloc = NULL;
1240 rel_end = relocs + sec->reloc_count;
1241 for (rel = relocs; rel < rel_end; rel++)
1243 enum {
1244 NEED_GOT = 1,
1245 NEED_PLT = 2,
1246 NEED_DYNREL = 4,
1247 #if LONG_BRANCH_PIC_IN_SHLIB
1248 NEED_STUBREL = 0 /* We won't be needing them in this case. */
1249 #else
1250 NEED_STUBREL = 8
1251 #endif
1254 unsigned int r_symndx, r_type;
1255 struct elf32_hppa_link_hash_entry *h;
1256 int need_entry;
1258 r_symndx = ELF32_R_SYM (rel->r_info);
1260 if (r_symndx < symtab_hdr->sh_info)
1261 h = NULL;
1262 else
1263 h = ((struct elf32_hppa_link_hash_entry *)
1264 sym_hashes[r_symndx - symtab_hdr->sh_info]);
1266 r_type = ELF32_R_TYPE (rel->r_info);
1268 switch (r_type)
1270 case R_PARISC_DLTIND14F:
1271 case R_PARISC_DLTIND14R:
1272 case R_PARISC_DLTIND21L:
1273 /* This symbol requires a global offset table entry. */
1274 need_entry = NEED_GOT;
1276 /* Mark this section as containing PIC code. */
1277 sec->flags |= SEC_HAS_GOT_REF;
1278 break;
1280 case R_PARISC_PLABEL14R: /* "Official" procedure labels. */
1281 case R_PARISC_PLABEL21L:
1282 case R_PARISC_PLABEL32:
1283 if (h == NULL)
1285 /* If this is a local symbol we do not need to create a
1286 PLT entry, but if we are creating a shared object we
1287 have to output a relocation for the PLABEL itself. */
1288 need_entry = NEED_DYNREL;
1290 else
1292 /* If it is a global symbol, then we do need to create a
1293 PLT entry, and additionally, if we are creating a
1294 shared object, we need to output a dynamic relocation
1295 pointing to that PLT entry. */
1296 need_entry = NEED_PLT | NEED_DYNREL;
1298 break;
1300 case R_PARISC_PCREL12F:
1301 case R_PARISC_PCREL17C:
1302 case R_PARISC_PCREL17F:
1303 case R_PARISC_PCREL22F:
1304 /* Handle calls, and function pointers as they might need to
1305 go through the .plt, and might require long branch stubs. */
1306 if (h == NULL)
1308 /* We know local syms won't need a .plt entry, and if
1309 they need a long branch stub we can't guarantee that
1310 we can reach the stub. So just flag an error later
1311 if we're doing a shared link and find we need a long
1312 branch stub. */
1313 continue;
1315 else
1317 /* Global symbols will need a .plt entry if they remain
1318 global, and in most cases won't need a long branch
1319 stub. Unfortunately, we have to cater for the case
1320 where a symbol is forced local by versioning, or due
1321 to symbolic linking, and we lose the .plt entry. */
1322 need_entry = NEED_PLT | NEED_STUBREL;
1324 break;
1326 case R_PARISC_SEGBASE: /* Used to set segment base. */
1327 case R_PARISC_SEGREL32: /* Relative reloc. */
1328 case R_PARISC_PCREL14F: /* PC relative load/store. */
1329 case R_PARISC_PCREL14R:
1330 case R_PARISC_PCREL17R: /* External branches. */
1331 case R_PARISC_PCREL21L: /* As above, and for load/store too. */
1332 /* We don't need to propagate the relocation if linking a
1333 shared object since these are section relative. */
1334 continue;
1336 case R_PARISC_DPREL14F: /* Used for gp rel data load/store. */
1337 case R_PARISC_DPREL14R:
1338 case R_PARISC_DPREL21L:
1339 if (info->shared)
1341 (*_bfd_error_handler)
1342 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1343 bfd_get_filename (abfd),
1344 elf_hppa_howto_table[r_type].name);
1345 bfd_set_error (bfd_error_bad_value);
1346 return false;
1348 /* Fall through. */
1350 case R_PARISC_DIR17F: /* Used for external branches. */
1351 case R_PARISC_DIR17R:
1352 case R_PARISC_DIR14R: /* Used for load/store from absolute locn. */
1353 case R_PARISC_DIR21L: /* As above, and for ext branches too. */
1354 #if 1
1355 /* Help debug shared library creation. Any of the above
1356 relocs can be used in shared libs, but they may cause
1357 pages to become unshared. */
1358 if (info->shared)
1360 (*_bfd_error_handler)
1361 (_("%s: relocation %s should not be used when making a shared object; recompile with -fPIC"),
1362 bfd_get_filename (abfd),
1363 elf_hppa_howto_table[r_type].name);
1365 /* Fall through. */
1366 #endif
1368 case R_PARISC_DIR32: /* .word, PARISC.unwind relocs. */
1369 /* We may want to output a dynamic relocation later. */
1370 need_entry = NEED_DYNREL;
1371 break;
1373 /* This relocation describes the C++ object vtable hierarchy.
1374 Reconstruct it for later use during GC. */
1375 case R_PARISC_GNU_VTINHERIT:
1376 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec,
1377 &h->elf, rel->r_offset))
1378 return false;
1379 continue;
1381 /* This relocation describes which C++ vtable entries are actually
1382 used. Record for later use during GC. */
1383 case R_PARISC_GNU_VTENTRY:
1384 if (!_bfd_elf32_gc_record_vtentry (abfd, sec,
1385 &h->elf, rel->r_offset))
1386 return false;
1387 continue;
1389 default:
1390 continue;
1393 /* Now carry out our orders. */
1394 if (need_entry & NEED_GOT)
1396 /* Allocate space for a GOT entry, as well as a dynamic
1397 relocation for this entry. */
1398 if (dynobj == NULL)
1399 elf_hash_table (info)->dynobj = dynobj = abfd;
1401 if (hplink->sgot == NULL)
1403 if (! elf32_hppa_create_dynamic_sections (dynobj, info))
1404 return false;
1407 if (h != NULL)
1409 if (h->elf.got.refcount == -1)
1411 h->elf.got.refcount = 1;
1413 /* Make sure this symbol is output as a dynamic symbol. */
1414 if (h->elf.dynindx == -1)
1416 if (! bfd_elf32_link_record_dynamic_symbol (info,
1417 &h->elf))
1418 return false;
1421 hplink->sgot->_raw_size += GOT_ENTRY_SIZE;
1422 hplink->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1424 else
1425 h->elf.got.refcount += 1;
1427 else
1429 /* This is a global offset table entry for a local symbol. */
1430 if (local_got_refcounts == NULL)
1432 size_t size;
1434 size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
1435 local_got_refcounts = ((bfd_signed_vma *)
1436 bfd_alloc (abfd, size));
1437 if (local_got_refcounts == NULL)
1438 return false;
1439 elf_local_got_refcounts (abfd) = local_got_refcounts;
1440 memset (local_got_refcounts, -1, size);
1442 if (local_got_refcounts[r_symndx] == -1)
1444 local_got_refcounts[r_symndx] = 1;
1446 hplink->sgot->_raw_size += GOT_ENTRY_SIZE;
1447 if (info->shared)
1449 /* If we are generating a shared object, we need to
1450 output a reloc so that the dynamic linker can
1451 adjust this GOT entry (because the address
1452 the shared library is loaded at is not fixed). */
1453 hplink->srelgot->_raw_size +=
1454 sizeof (Elf32_External_Rela);
1457 else
1458 local_got_refcounts[r_symndx] += 1;
1462 if (need_entry & NEED_PLT)
1464 /* If we are creating a shared library, and this is a reloc
1465 against a weak symbol or a global symbol in a dynamic
1466 object, then we will be creating an import stub and a
1467 .plt entry for the symbol. Similarly, on a normal link
1468 to symbols defined in a dynamic object we'll need the
1469 import stub and a .plt entry. We don't know yet whether
1470 the symbol is defined or not, so make an entry anyway and
1471 clean up later in adjust_dynamic_symbol. */
1472 if ((sec->flags & SEC_ALLOC) != 0)
1474 if (h->elf.plt.refcount == -1)
1476 h->elf.plt.refcount = 1;
1477 h->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1479 else
1480 h->elf.plt.refcount += 1;
1484 if (need_entry & (NEED_DYNREL | NEED_STUBREL))
1486 /* Flag this symbol as having a non-got, non-plt reference
1487 so that we generate copy relocs if it turns out to be
1488 dynamic. */
1489 if (h != NULL)
1490 h->elf.elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1492 /* If we are creating a shared library then we need to copy
1493 the reloc into the shared library. However, if we are
1494 linking with -Bsymbolic, we need only copy absolute
1495 relocs or relocs against symbols that are not defined in
1496 an object we are including in the link. PC- or DP- or
1497 DLT-relative relocs against any local sym or global sym
1498 with DEF_REGULAR set, can be discarded. At this point we
1499 have not seen all the input files, so it is possible that
1500 DEF_REGULAR is not set now but will be set later (it is
1501 never cleared). We account for that possibility below by
1502 storing information in the reloc_entries field of the
1503 hash table entry.
1505 A similar situation to the -Bsymbolic case occurs when
1506 creating shared libraries and symbol visibility changes
1507 render the symbol local.
1509 As it turns out, all the relocs we will be creating here
1510 are absolute, so we cannot remove them on -Bsymbolic
1511 links or visibility changes anyway. A STUB_REL reloc
1512 is absolute too, as in that case it is the reloc in the
1513 stub we will be creating, rather than copying the PCREL
1514 reloc in the branch. */
1515 if ((sec->flags & SEC_ALLOC) != 0
1516 && info->shared
1517 #if RELATIVE_DYNAMIC_RELOCS
1518 && (!info->symbolic
1519 || is_absolute_reloc (r_type)
1520 || (h != NULL
1521 && ((h->elf.elf_link_hash_flags
1522 & ELF_LINK_HASH_DEF_REGULAR) == 0
1523 || h->elf.root.type == bfd_link_hash_defweak)))
1524 #endif
1527 boolean doit;
1528 asection *srel;
1530 srel = sreloc;
1531 if ((need_entry & NEED_STUBREL))
1532 srel = stubreloc;
1534 /* Create a reloc section in dynobj and make room for
1535 this reloc. */
1536 if (srel == NULL)
1538 char *name;
1540 if (dynobj == NULL)
1541 elf_hash_table (info)->dynobj = dynobj = abfd;
1543 name = bfd_elf_string_from_elf_section
1544 (abfd,
1545 elf_elfheader (abfd)->e_shstrndx,
1546 elf_section_data (sec)->rel_hdr.sh_name);
1547 if (name == NULL)
1549 (*_bfd_error_handler)
1550 (_("Could not find relocation section for %s"),
1551 sec->name);
1552 bfd_set_error (bfd_error_bad_value);
1553 return false;
1556 if ((need_entry & NEED_STUBREL))
1558 int len = strlen (name) + sizeof (STUB_SUFFIX);
1559 char *newname = bfd_malloc (len);
1561 if (newname == NULL)
1562 return false;
1563 strcpy (newname, name);
1564 strcpy (newname + len - sizeof (STUB_SUFFIX),
1565 STUB_SUFFIX);
1566 name = newname;
1569 srel = bfd_get_section_by_name (dynobj, name);
1570 if (srel == NULL)
1572 flagword flags;
1574 srel = bfd_make_section (dynobj, name);
1575 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1576 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1577 if ((sec->flags & SEC_ALLOC) != 0)
1578 flags |= SEC_ALLOC | SEC_LOAD;
1579 if (srel == NULL
1580 || !bfd_set_section_flags (dynobj, srel, flags)
1581 || !bfd_set_section_alignment (dynobj, srel, 2))
1582 return false;
1584 else if ((need_entry & NEED_STUBREL))
1585 free (name);
1587 if ((need_entry & NEED_STUBREL))
1588 stubreloc = srel;
1589 else
1590 sreloc = srel;
1593 #if ! LONG_BRANCH_PIC_IN_SHLIB
1594 /* If this is a function call, we only need one dynamic
1595 reloc for the stub as all calls to a particular
1596 function will go through the same stub. Actually, a
1597 long branch stub needs two relocations, but we count
1598 on some intelligence on the part of the dynamic
1599 linker. */
1600 if ((need_entry & NEED_STUBREL))
1602 doit = h->stub_reloc_sec != stubreloc;
1603 h->stub_reloc_sec = stubreloc;
1605 else
1606 #endif
1607 doit = 1;
1609 if (doit)
1611 srel->_raw_size += sizeof (Elf32_External_Rela);
1613 #if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
1614 /* Keep track of relocations we have entered for
1615 this global symbol, so that we can discard them
1616 later if necessary. */
1617 if (h != NULL
1618 && (0
1619 #if RELATIVE_DYNAMIC_RELOCS
1620 || ! is_absolute_reloc (rtype)
1621 #endif
1622 || (need_entry & NEED_STUBREL)))
1624 struct elf32_hppa_dyn_reloc_entry *p;
1626 for (p = h->reloc_entries; p != NULL; p = p->next)
1627 if (p->section == srel)
1628 break;
1630 if (p == NULL)
1632 p = ((struct elf32_hppa_dyn_reloc_entry *)
1633 bfd_alloc (dynobj, sizeof *p));
1634 if (p == NULL)
1635 return false;
1636 p->next = h->reloc_entries;
1637 h->reloc_entries = p;
1638 p->section = srel;
1639 p->count = 0;
1642 /* NEED_STUBREL and NEED_DYNREL are never both
1643 set. Leave the count at zero for the
1644 NEED_STUBREL case as we only ever have one
1645 stub reloc per section per symbol, and this
1646 simplifies code in hppa_discard_copies. */
1647 if (! (need_entry & NEED_STUBREL))
1648 ++p->count;
1650 #endif
1656 return true;
1660 /* Return the section that should be marked against garbage collection
1661 for a given relocation. */
1663 static asection *
1664 elf32_hppa_gc_mark_hook (abfd, info, rel, h, sym)
1665 bfd *abfd;
1666 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1667 Elf_Internal_Rela *rel;
1668 struct elf_link_hash_entry *h;
1669 Elf_Internal_Sym *sym;
1671 if (h != NULL)
1673 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1675 case R_PARISC_GNU_VTINHERIT:
1676 case R_PARISC_GNU_VTENTRY:
1677 break;
1679 default:
1680 switch (h->root.type)
1682 case bfd_link_hash_defined:
1683 case bfd_link_hash_defweak:
1684 return h->root.u.def.section;
1686 case bfd_link_hash_common:
1687 return h->root.u.c.p->section;
1689 default:
1690 break;
1694 else
1696 if (!(elf_bad_symtab (abfd)
1697 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1698 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1699 && sym->st_shndx != SHN_COMMON))
1701 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1705 return NULL;
1709 /* Update the got and plt entry reference counts for the section being
1710 removed. */
1712 static boolean
1713 elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
1714 bfd *abfd;
1715 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1716 asection *sec;
1717 const Elf_Internal_Rela *relocs;
1719 Elf_Internal_Shdr *symtab_hdr;
1720 struct elf_link_hash_entry **sym_hashes;
1721 bfd_signed_vma *local_got_refcounts;
1722 const Elf_Internal_Rela *rel, *relend;
1723 unsigned long r_symndx;
1724 struct elf_link_hash_entry *h;
1725 bfd *dynobj;
1726 asection *sgot;
1727 asection *srelgot;
1729 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1730 sym_hashes = elf_sym_hashes (abfd);
1731 local_got_refcounts = elf_local_got_refcounts (abfd);
1733 dynobj = elf_hash_table (info)->dynobj;
1734 if (dynobj == NULL)
1735 return true;
1737 sgot = hppa_link_hash_table (info)->sgot;
1738 srelgot = hppa_link_hash_table (info)->srelgot;
1740 relend = relocs + sec->reloc_count;
1741 for (rel = relocs; rel < relend; rel++)
1742 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1744 case R_PARISC_DLTIND14F:
1745 case R_PARISC_DLTIND14R:
1746 case R_PARISC_DLTIND21L:
1747 r_symndx = ELF32_R_SYM (rel->r_info);
1748 if (r_symndx >= symtab_hdr->sh_info)
1750 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1751 if (h->got.refcount > 0)
1753 h->got.refcount -= 1;
1754 if (h->got.refcount == 0)
1756 sgot->_raw_size -= 4;
1757 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1761 else if (local_got_refcounts != NULL)
1763 if (local_got_refcounts[r_symndx] > 0)
1765 local_got_refcounts[r_symndx] -= 1;
1766 if (local_got_refcounts[r_symndx] == 0)
1768 sgot->_raw_size -= 4;
1769 if (info->shared)
1770 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1774 break;
1776 case R_PARISC_PLABEL14R:
1777 case R_PARISC_PLABEL21L:
1778 case R_PARISC_PLABEL32:
1779 case R_PARISC_PCREL12F:
1780 case R_PARISC_PCREL17C:
1781 case R_PARISC_PCREL17F:
1782 case R_PARISC_PCREL22F:
1783 r_symndx = ELF32_R_SYM (rel->r_info);
1784 if (r_symndx >= symtab_hdr->sh_info)
1786 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1787 if (h->plt.refcount > 0)
1788 h->plt.refcount -= 1;
1790 break;
1792 default:
1793 break;
1796 return true;
1800 /* Adjust a symbol defined by a dynamic object and referenced by a
1801 regular object. The current definition is in some section of the
1802 dynamic object, but we're not including those sections. We have to
1803 change the definition to something the rest of the link can
1804 understand. */
1806 static boolean
1807 elf32_hppa_adjust_dynamic_symbol (info, h)
1808 struct bfd_link_info *info;
1809 struct elf_link_hash_entry *h;
1811 bfd *dynobj;
1812 struct elf32_hppa_link_hash_table *hplink;
1813 asection *s;
1815 dynobj = elf_hash_table (info)->dynobj;
1816 hplink = hppa_link_hash_table (info);
1818 /* If this is a function, put it in the procedure linkage table. We
1819 will fill in the contents of the procedure linkage table later,
1820 when we know the address of the .got section. */
1821 if (h->type == STT_FUNC
1822 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1824 if (h->plt.refcount <= 0
1825 || ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1826 && h->root.type != bfd_link_hash_defweak
1827 && (!info->shared || info->symbolic)))
1829 /* The .plt entry is not needed when:
1830 a) Garbage collection has removed all references to the
1831 symbol, or
1832 b) We know for certain the symbol is defined in this
1833 object, and it's not a weak definition. Either this
1834 object is the application or we are doing a shared
1835 symbolic link. As a special sop to the hppa ABI, we
1836 keep a .plt entry for functions in sections containing
1837 PIC code. */
1838 if (!info->shared
1839 && h->plt.refcount > 0
1840 && (h->root.type == bfd_link_hash_defined
1841 || h->root.type == bfd_link_hash_defweak)
1842 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0)
1844 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
1846 else
1848 h->plt.offset = (bfd_vma) -1;
1849 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1850 return true;
1854 /* Make an entry in the .plt section. */
1855 s = hplink->splt;
1856 h->plt.offset = s->_raw_size;
1857 s->_raw_size += PLT_ENTRY_SIZE;
1859 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
1861 /* Make sure this symbol is output as a dynamic symbol. */
1862 if (h->dynindx == -1)
1864 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1865 return false;
1868 /* We also need to make an entry in the .rela.plt section. */
1869 s = hplink->srelplt;
1870 s->_raw_size += sizeof (Elf32_External_Rela);
1872 return true;
1875 /* If this is a weak symbol, and there is a real definition, the
1876 processor independent code will have arranged for us to see the
1877 real definition first, and we can just use the same value. */
1878 if (h->weakdef != NULL)
1880 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1881 || h->weakdef->root.type == bfd_link_hash_defweak);
1882 h->root.u.def.section = h->weakdef->root.u.def.section;
1883 h->root.u.def.value = h->weakdef->root.u.def.value;
1884 return true;
1887 /* This is a reference to a symbol defined by a dynamic object which
1888 is not a function. */
1890 /* If we are creating a shared library, we must presume that the
1891 only references to the symbol are via the global offset table.
1892 For such cases we need not do anything here; the relocations will
1893 be handled correctly by relocate_section. */
1894 if (info->shared)
1895 return true;
1897 /* If there are no references to this symbol that do not use the
1898 GOT, we don't need to generate a copy reloc. */
1899 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1900 return true;
1902 /* We must allocate the symbol in our .dynbss section, which will
1903 become part of the .bss section of the executable. There will be
1904 an entry for this symbol in the .dynsym section. The dynamic
1905 object will contain position independent code, so all references
1906 from the dynamic object to this symbol will go through the global
1907 offset table. The dynamic linker will use the .dynsym entry to
1908 determine the address it must put in the global offset table, so
1909 both the dynamic object and the regular object will refer to the
1910 same memory location for the variable. */
1912 s = hplink->sdynbss;
1914 /* We must generate a COPY reloc to tell the dynamic linker to
1915 copy the initial value out of the dynamic object and into the
1916 runtime process image. We need to remember the offset into the
1917 .rela.bss section we are going to use. */
1918 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1920 asection *srel;
1922 srel = hplink->srelbss;
1923 srel->_raw_size += sizeof (Elf32_External_Rela);
1924 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1928 /* We need to figure out the alignment required for this symbol. I
1929 have no idea how other ELF linkers handle this. */
1930 unsigned int power_of_two;
1932 power_of_two = bfd_log2 (h->size);
1933 if (power_of_two > 3)
1934 power_of_two = 3;
1936 /* Apply the required alignment. */
1937 s->_raw_size = BFD_ALIGN (s->_raw_size,
1938 (bfd_size_type) (1 << power_of_two));
1939 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1941 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1942 return false;
1945 /* Define the symbol as being at this point in the section. */
1946 h->root.u.def.section = s;
1947 h->root.u.def.value = s->_raw_size;
1949 /* Increment the section size to make room for the symbol. */
1950 s->_raw_size += h->size;
1952 return true;
1956 /* Called via elf_link_hash_traverse to create .plt entries for an
1957 application that uses statically linked PIC functions. Similar to
1958 the first part of elf32_hppa_adjust_dynamic_symbol. */
1960 static boolean
1961 hppa_handle_PIC_calls (h, inf)
1962 struct elf_link_hash_entry *h;
1963 PTR inf;
1965 struct bfd_link_info *info;
1966 bfd *dynobj;
1967 struct elf32_hppa_link_hash_table *hplink;
1968 asection *s;
1970 if (! (h->plt.refcount > 0
1971 && (h->root.type == bfd_link_hash_defined
1972 || h->root.type == bfd_link_hash_defweak)
1973 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0))
1975 h->plt.offset = (bfd_vma) -1;
1976 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1977 return true;
1980 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
1982 info = (struct bfd_link_info *) inf;
1983 dynobj = elf_hash_table (info)->dynobj;
1984 hplink = hppa_link_hash_table (info);
1986 /* Make an entry in the .plt section. */
1987 s = hplink->splt;
1988 h->plt.offset = s->_raw_size;
1989 s->_raw_size += PLT_ENTRY_SIZE;
1991 return true;
1995 #if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
1996 /* This function is called via elf_link_hash_traverse to discard space
1997 we allocated for relocs that it turned out we didn't need. */
1999 static boolean
2000 hppa_discard_copies (h, inf)
2001 struct elf_link_hash_entry *h;
2002 PTR inf;
2004 struct elf32_hppa_dyn_reloc_entry *s;
2005 struct elf32_hppa_link_hash_entry *eh;
2006 struct bfd_link_info *info;
2008 eh = (struct elf32_hppa_link_hash_entry *) h;
2009 info = (struct bfd_link_info *) inf;
2011 #if ! LONG_BRANCH_PIC_IN_SHLIB
2012 /* Handle the stub reloc case. If we have a plt entry for the
2013 function, we won't be needing long branch stubs. s->count will
2014 only be zero for stub relocs, which provides a handy way of
2015 flagging these relocs, and means we need do nothing special for
2016 the forced local and symbolic link case. */
2017 if (eh->stub_reloc_sec != NULL
2018 && eh->elf.plt.offset != (bfd_vma) -1)
2020 for (s = eh->reloc_entries; s != NULL; s = s->next)
2021 if (s->count == 0)
2022 s->section->_raw_size -= sizeof (Elf32_External_Rela);
2024 #endif
2026 /* If a symbol has been forced local or we have found a regular
2027 definition for the symbolic link case, then we won't be needing
2028 any relocs. */
2029 #if RELATIVE_DYNAMIC_RELOCS
2030 if (eh->elf.dynindx == -1
2031 || ((eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2032 && eh->elf.root.type != bfd_link_hash_defweak
2033 && info->symbolic))
2035 for (s = eh->reloc_entries; s != NULL; s = s->next)
2036 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2038 #endif
2040 return true;
2042 #endif
2045 /* Set the sizes of the dynamic sections. */
2047 static boolean
2048 elf32_hppa_size_dynamic_sections (output_bfd, info)
2049 bfd *output_bfd;
2050 struct bfd_link_info *info;
2052 struct elf32_hppa_link_hash_table *hplink;
2053 bfd *dynobj;
2054 asection *s;
2055 boolean relocs;
2056 boolean reltext;
2058 hplink = hppa_link_hash_table (info);
2059 dynobj = elf_hash_table (info)->dynobj;
2060 BFD_ASSERT (dynobj != NULL);
2062 if (elf_hash_table (info)->dynamic_sections_created)
2064 /* Set the contents of the .interp section to the interpreter. */
2065 if (! info->shared)
2067 s = bfd_get_section_by_name (dynobj, ".interp");
2068 BFD_ASSERT (s != NULL);
2069 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2070 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2073 else
2075 /* Run through the function symbols, looking for any that are
2076 PIC, and allocate space for the necessary .plt entries so
2077 that %r19 will be set up. */
2078 if (! info->shared)
2079 elf_link_hash_traverse (&hplink->root,
2080 hppa_handle_PIC_calls,
2081 info);
2083 /* We may have created entries in the .rela.got section.
2084 However, if we are not creating the dynamic sections, we will
2085 not actually use these entries. Reset the size of .rela.got,
2086 which will cause it to get stripped from the output file
2087 below. */
2088 hplink->srelgot->_raw_size = 0;
2091 #if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
2092 /* If this is a -Bsymbolic shared link, then we need to discard all
2093 relocs against symbols defined in a regular object. We also need
2094 to lose relocs we've allocated for long branch stubs if we know
2095 we won't be generating a stub. */
2096 if (info->shared)
2097 elf_link_hash_traverse (&hplink->root,
2098 hppa_discard_copies,
2099 info);
2100 #endif
2102 /* The check_relocs and adjust_dynamic_symbol entry points have
2103 determined the sizes of the various dynamic sections. Allocate
2104 memory for them. */
2105 relocs = false;
2106 reltext = false;
2107 for (s = dynobj->sections; s != NULL; s = s->next)
2109 const char *name;
2111 if ((s->flags & SEC_LINKER_CREATED) == 0)
2112 continue;
2114 /* It's OK to base decisions on the section name, because none
2115 of the dynobj section names depend upon the input files. */
2116 name = bfd_get_section_name (dynobj, s);
2118 if (strncmp (name, ".rela", 5) == 0)
2120 if (s->_raw_size != 0)
2122 asection *target;
2124 /* Remember whether there are any reloc sections other
2125 than .rela.plt. */
2126 if (strcmp (name+5, ".plt") != 0)
2128 const char *outname;
2130 relocs = true;
2132 /* If this relocation section applies to a read only
2133 section, then we probably need a DT_TEXTREL
2134 entry. The entries in the .rela.plt section
2135 really apply to the .got section, which we
2136 created ourselves and so know is not readonly. */
2137 outname = bfd_get_section_name (output_bfd,
2138 s->output_section);
2139 target = bfd_get_section_by_name (output_bfd, outname + 5);
2140 if (target != NULL
2141 && (target->flags & SEC_READONLY) != 0
2142 && (target->flags & SEC_ALLOC) != 0)
2143 reltext = true;
2146 /* We use the reloc_count field as a counter if we need
2147 to copy relocs into the output file. */
2148 s->reloc_count = 0;
2151 else if (strcmp (name, ".plt") == 0)
2153 else if (strcmp (name, ".got") == 0)
2155 else
2157 /* It's not one of our sections, so don't allocate space. */
2158 continue;
2161 if (s->_raw_size == 0)
2163 /* If we don't need this section, strip it from the
2164 output file. This is mostly to handle .rela.bss and
2165 .rela.plt. We must create both sections in
2166 create_dynamic_sections, because they must be created
2167 before the linker maps input sections to output
2168 sections. The linker does that before
2169 adjust_dynamic_symbol is called, and it is that
2170 function which decides whether anything needs to go
2171 into these sections. */
2172 _bfd_strip_section_from_output (info, s);
2173 continue;
2176 /* Allocate memory for the section contents. Zero it, because
2177 we may not fill in all the reloc sections. */
2178 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2179 if (s->contents == NULL && s->_raw_size != 0)
2180 return false;
2183 if (elf_hash_table (info)->dynamic_sections_created)
2185 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2186 actually has nothing to do with the PLT, it is how we
2187 communicate the LTP value of a load module to the dynamic
2188 linker. */
2189 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
2190 return false;
2192 /* Add some entries to the .dynamic section. We fill in the
2193 values later, in elf32_hppa_finish_dynamic_sections, but we
2194 must add the entries now so that we get the correct size for
2195 the .dynamic section. The DT_DEBUG entry is filled in by the
2196 dynamic linker and used by the debugger. */
2197 if (! info->shared)
2199 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2200 return false;
2203 if (hplink->srelplt->_raw_size != 0)
2205 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2206 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2207 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2208 return false;
2211 if (relocs)
2213 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2214 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2215 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2216 sizeof (Elf32_External_Rela)))
2217 return false;
2220 if (reltext)
2222 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2223 return false;
2224 info->flags |= DF_TEXTREL;
2228 return true;
2232 /* External entry points for sizing and building linker stubs. */
2234 /* Determine and set the size of the stub section for a final link.
2236 The basic idea here is to examine all the relocations looking for
2237 PC-relative calls to a target that is unreachable with a "bl"
2238 instruction. */
2240 boolean
2241 elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
2242 add_stub_section, layout_sections_again)
2243 bfd *stub_bfd;
2244 boolean multi_subspace;
2245 struct bfd_link_info *info;
2246 asection * (*add_stub_section) PARAMS ((const char *, asection *));
2247 void (*layout_sections_again) PARAMS ((void));
2249 bfd *input_bfd;
2250 asection *section;
2251 Elf_Internal_Sym *local_syms, **all_local_syms;
2252 unsigned int i, indx, bfd_count, sec_count;
2253 struct elf32_hppa_link_hash_table *hplink;
2254 boolean stub_changed = 0;
2256 hplink = hppa_link_hash_table (info);
2258 /* Stash our params away. */
2259 hplink->stub_bfd = stub_bfd;
2260 hplink->multi_subspace = multi_subspace;
2261 hplink->add_stub_section = add_stub_section;
2262 hplink->layout_sections_again = layout_sections_again;
2264 /* Count the number of input BFDs and the total number of input sections. */
2265 for (input_bfd = info->input_bfds, bfd_count = 0, sec_count = 0;
2266 input_bfd != NULL;
2267 input_bfd = input_bfd->link_next)
2269 bfd_count += 1;
2270 sec_count += input_bfd->section_count;
2273 hplink->stub_section_created
2274 = (asection **) bfd_zmalloc (sizeof (asection *) * sec_count);
2275 if (hplink->stub_section_created == NULL)
2276 return false;
2278 #if ! LONG_BRANCH_PIC_IN_SHLIB
2279 hplink->reloc_section_created
2280 = (asection **) bfd_zmalloc (sizeof (asection *) * sec_count);
2281 if (hplink->reloc_section_created == NULL)
2282 goto error_ret_free_stub;
2283 #endif
2285 /* We want to read in symbol extension records only once. To do this
2286 we need to read in the local symbols in parallel and save them for
2287 later use; so hold pointers to the local symbols in an array. */
2288 all_local_syms
2289 = (Elf_Internal_Sym **) bfd_zmalloc (sizeof (Elf_Internal_Sym *)
2290 * bfd_count);
2291 if (all_local_syms == NULL)
2292 goto error_ret_free_reloc;
2294 /* Walk over all the input BFDs, swapping in local symbols.
2295 If we are creating a shared library, create hash entries for the
2296 export stubs. */
2297 for (input_bfd = info->input_bfds, indx = 0, sec_count = 0;
2298 input_bfd != NULL;
2299 input_bfd = input_bfd->link_next, indx++)
2301 Elf_Internal_Shdr *symtab_hdr;
2302 Elf_Internal_Sym *isym;
2303 Elf32_External_Sym *ext_syms, *esym;
2305 /* We'll need the symbol table in a second. */
2306 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2307 if (symtab_hdr->sh_info == 0)
2308 continue;
2310 /* We need an array of the local symbols attached to the input bfd.
2311 Unfortunately, we're going to have to read & swap them in. */
2312 local_syms = (Elf_Internal_Sym *)
2313 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf_Internal_Sym));
2314 if (local_syms == NULL)
2316 goto error_ret_free_local;
2318 all_local_syms[indx] = local_syms;
2319 ext_syms = (Elf32_External_Sym *)
2320 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf32_External_Sym));
2321 if (ext_syms == NULL)
2323 goto error_ret_free_local;
2326 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2327 || (bfd_read (ext_syms, 1,
2328 (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)),
2329 input_bfd)
2330 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2332 free (ext_syms);
2333 goto error_ret_free_local;
2336 /* Swap the local symbols in. */
2337 isym = local_syms;
2338 esym = ext_syms;
2339 for (i = 0; i < symtab_hdr->sh_info; i++, esym++, isym++)
2340 bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
2342 /* Now we can free the external symbols. */
2343 free (ext_syms);
2345 if (info->shared && hplink->multi_subspace)
2347 unsigned int symndx;
2348 unsigned int symcount;
2350 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2351 - symtab_hdr->sh_info);
2353 /* Look through the global syms for functions; We need to
2354 build export stubs for all globally visible functions. */
2355 for (symndx = 0; symndx < symcount; symndx++)
2357 struct elf32_hppa_link_hash_entry *hash;
2359 hash = ((struct elf32_hppa_link_hash_entry *)
2360 elf_sym_hashes (input_bfd)[symndx]);
2362 while (hash->elf.root.type == bfd_link_hash_indirect
2363 || hash->elf.root.type == bfd_link_hash_warning)
2364 hash = ((struct elf32_hppa_link_hash_entry *)
2365 hash->elf.root.u.i.link);
2367 /* At this point in the link, undefined syms have been
2368 resolved, so we need to check that the symbol was
2369 defined in this BFD. */
2370 if ((hash->elf.root.type == bfd_link_hash_defined
2371 || hash->elf.root.type == bfd_link_hash_defweak)
2372 && hash->elf.type == STT_FUNC
2373 && hash->elf.root.u.def.section->output_section != NULL
2374 && hash->elf.root.u.def.section->owner == input_bfd
2375 && (hash->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
2376 && !(hash->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2377 && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2379 asection *sec;
2380 const char *stub_name;
2381 struct elf32_hppa_stub_hash_entry *stub_entry;
2383 sec = hash->elf.root.u.def.section;
2384 stub_name = hash->elf.root.root.string;
2385 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2386 stub_name,
2387 false, false);
2388 if (stub_entry == NULL)
2390 stub_entry = hppa_add_stub (stub_name,
2391 sec,
2392 sec_count + sec->index,
2393 info);
2394 if (!stub_entry)
2395 goto error_ret_free_local;
2397 stub_entry->target_value = hash->elf.root.u.def.value;
2398 stub_entry->target_section = hash->elf.root.u.def.section;
2399 stub_entry->stub_type = hppa_stub_export;
2400 stub_entry->h = hash;
2401 stub_changed = 1;
2403 else
2405 (*_bfd_error_handler) (_("%s: duplicate export stub %s"),
2406 bfd_get_filename (input_bfd),
2407 stub_name);
2411 sec_count += input_bfd->section_count;
2415 while (1)
2417 asection *stub_sec;
2419 for (input_bfd = info->input_bfds, indx = 0, sec_count = 0;
2420 input_bfd != NULL;
2421 input_bfd = input_bfd->link_next, indx++)
2423 Elf_Internal_Shdr *symtab_hdr;
2425 /* We'll need the symbol table in a second. */
2426 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2427 if (symtab_hdr->sh_info == 0)
2428 continue;
2430 local_syms = all_local_syms[indx];
2432 /* Walk over each section attached to the input bfd. */
2433 for (section = input_bfd->sections;
2434 section != NULL;
2435 section = section->next, sec_count++)
2437 Elf_Internal_Shdr *input_rel_hdr;
2438 Elf32_External_Rela *external_relocs, *erelaend, *erela;
2439 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2441 /* If there aren't any relocs, then there's nothing more
2442 to do. */
2443 if ((section->flags & SEC_RELOC) == 0
2444 || section->reloc_count == 0)
2445 continue;
2447 /* Allocate space for the external relocations. */
2448 external_relocs
2449 = ((Elf32_External_Rela *)
2450 bfd_malloc (section->reloc_count
2451 * sizeof (Elf32_External_Rela)));
2452 if (external_relocs == NULL)
2454 goto error_ret_free_local;
2457 /* Likewise for the internal relocations. */
2458 internal_relocs = ((Elf_Internal_Rela *)
2459 bfd_malloc (section->reloc_count
2460 * sizeof (Elf_Internal_Rela)));
2461 if (internal_relocs == NULL)
2463 free (external_relocs);
2464 goto error_ret_free_local;
2467 /* Read in the external relocs. */
2468 input_rel_hdr = &elf_section_data (section)->rel_hdr;
2469 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
2470 || bfd_read (external_relocs, 1,
2471 input_rel_hdr->sh_size,
2472 input_bfd) != input_rel_hdr->sh_size)
2474 free (external_relocs);
2475 error_ret_free_internal:
2476 free (internal_relocs);
2477 goto error_ret_free_local;
2480 /* Swap in the relocs. */
2481 erela = external_relocs;
2482 erelaend = erela + section->reloc_count;
2483 irela = internal_relocs;
2484 for (; erela < erelaend; erela++, irela++)
2485 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
2487 /* We're done with the external relocs, free them. */
2488 free (external_relocs);
2490 /* Now examine each relocation. */
2491 irela = internal_relocs;
2492 irelaend = irela + section->reloc_count;
2493 for (; irela < irelaend; irela++)
2495 unsigned int r_type, r_indx;
2496 enum elf32_hppa_stub_type stub_type;
2497 struct elf32_hppa_stub_hash_entry *stub_entry;
2498 asection *sym_sec;
2499 bfd_vma sym_value;
2500 bfd_vma destination;
2501 struct elf32_hppa_link_hash_entry *hash;
2502 char *stub_name;
2504 r_type = ELF32_R_TYPE (irela->r_info);
2505 r_indx = ELF32_R_SYM (irela->r_info);
2507 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2509 bfd_set_error (bfd_error_bad_value);
2510 goto error_ret_free_internal;
2513 /* Only look for stubs on call instructions. */
2514 if (r_type != (unsigned int) R_PARISC_PCREL12F
2515 && r_type != (unsigned int) R_PARISC_PCREL17F
2516 && r_type != (unsigned int) R_PARISC_PCREL22F)
2517 continue;
2519 /* Now determine the call target, its name, value,
2520 section. */
2521 sym_sec = NULL;
2522 sym_value = 0;
2523 destination = 0;
2524 hash = NULL;
2525 if (r_indx < symtab_hdr->sh_info)
2527 /* It's a local symbol. */
2528 Elf_Internal_Sym *sym;
2529 Elf_Internal_Shdr *hdr;
2531 sym = local_syms + r_indx;
2532 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2533 sym_sec = hdr->bfd_section;
2534 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2535 sym_value = sym->st_value;
2536 destination = (sym_value + irela->r_addend
2537 + sym_sec->output_offset
2538 + sym_sec->output_section->vma);
2540 else
2542 /* It's an external symbol. */
2543 int e_indx;
2545 e_indx = r_indx - symtab_hdr->sh_info;
2546 hash = ((struct elf32_hppa_link_hash_entry *)
2547 elf_sym_hashes (input_bfd)[e_indx]);
2549 while (hash->elf.root.type == bfd_link_hash_indirect
2550 || hash->elf.root.type == bfd_link_hash_warning)
2551 hash = ((struct elf32_hppa_link_hash_entry *)
2552 hash->elf.root.u.i.link);
2554 if (hash->elf.root.type == bfd_link_hash_defined
2555 || hash->elf.root.type == bfd_link_hash_defweak)
2557 sym_sec = hash->elf.root.u.def.section;
2558 sym_value = hash->elf.root.u.def.value;
2559 if (sym_sec->output_section != NULL)
2560 destination = (sym_value + irela->r_addend
2561 + sym_sec->output_offset
2562 + sym_sec->output_section->vma);
2564 else if (hash->elf.root.type == bfd_link_hash_undefweak)
2566 if (! info->shared)
2567 continue;
2569 else if (hash->elf.root.type == bfd_link_hash_undefined)
2571 if (! (info->shared
2572 && !info->no_undefined
2573 && (ELF_ST_VISIBILITY (hash->elf.other)
2574 == STV_DEFAULT)))
2575 continue;
2577 else
2579 bfd_set_error (bfd_error_bad_value);
2580 goto error_ret_free_internal;
2584 /* Determine what (if any) linker stub is needed. */
2585 stub_type = hppa_type_of_stub (section, irela, hash,
2586 destination);
2587 if (stub_type == hppa_stub_none)
2588 continue;
2590 /* Get the name of this stub. */
2591 stub_name = hppa_stub_name (section, sym_sec, hash, irela);
2592 if (!stub_name)
2593 goto error_ret_free_internal;
2595 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2596 stub_name,
2597 false, false);
2598 if (stub_entry != NULL)
2600 /* The proper stub has already been created. */
2601 free (stub_name);
2602 continue;
2605 stub_entry = hppa_add_stub (stub_name, section,
2606 sec_count, info);
2607 if (stub_entry == NULL)
2609 free (stub_name);
2610 goto error_ret_free_local;
2613 stub_entry->target_value = sym_value;
2614 stub_entry->target_section = sym_sec;
2615 stub_entry->stub_type = stub_type;
2616 if (info->shared)
2618 if (stub_type == hppa_stub_import)
2619 stub_entry->stub_type = hppa_stub_import_shared;
2620 else if (stub_type == hppa_stub_long_branch
2621 && (LONG_BRANCH_PIC_IN_SHLIB || hash == NULL))
2622 stub_entry->stub_type = hppa_stub_long_branch_shared;
2624 stub_entry->h = hash;
2625 stub_changed = 1;
2628 /* We're done with the internal relocs, free them. */
2629 free (internal_relocs);
2633 if (!stub_changed)
2634 break;
2636 /* OK, we've added some stubs. Find out the new size of the
2637 stub sections. */
2638 hplink->offset = (bfd_vma *)
2639 bfd_realloc (hplink->offset,
2640 hplink->stub_bfd->section_count * sizeof (bfd_vma));
2641 if (hplink->offset == NULL)
2642 goto error_ret_free_local;
2644 memset (hplink->offset, 0,
2645 hplink->stub_bfd->section_count * sizeof (bfd_vma));
2647 bfd_hash_traverse (&hplink->stub_hash_table,
2648 hppa_size_one_stub,
2649 hplink);
2651 for (stub_sec = hplink->stub_bfd->sections;
2652 stub_sec != NULL;
2653 stub_sec = stub_sec->next)
2655 bfd_set_section_size (hplink->stub_bfd, stub_sec,
2656 hplink->offset[stub_sec->index]);
2658 /* Ask the linker to do its stuff. */
2659 (*hplink->layout_sections_again) ();
2660 stub_changed = 0;
2663 /* We're done with the local symbols, free them. */
2664 for (i = 0; i < bfd_count; i++)
2665 if (all_local_syms[i])
2666 free (all_local_syms[i]);
2667 free (all_local_syms);
2668 #if ! LONG_BRANCH_PIC_IN_SHLIB
2669 free (hplink->reloc_section_created);
2670 #endif
2671 free (hplink->stub_section_created);
2672 return true;
2674 error_ret_free_local:
2675 for (i = 0; i < bfd_count; i++)
2676 if (all_local_syms[i])
2677 free (all_local_syms[i]);
2678 free (all_local_syms);
2680 error_ret_free_reloc:
2681 #if ! LONG_BRANCH_PIC_IN_SHLIB
2682 free (hplink->reloc_section_created);
2683 error_ret_free_stub:
2684 #endif
2685 free (hplink->stub_section_created);
2686 return false;
2690 /* For a final link, this function is called after we have sized the
2691 stubs to provide a value for __gp. */
2693 boolean
2694 elf32_hppa_set_gp (abfd, info)
2695 bfd *abfd;
2696 struct bfd_link_info *info;
2698 struct elf_link_hash_entry *h;
2699 asection *sec;
2700 bfd_vma gp_val;
2702 h = elf_link_hash_lookup (elf_hash_table (info), "$global$",
2703 false, false, false);
2705 if (h != NULL && h->root.type == bfd_link_hash_defined)
2707 gp_val = h->root.u.def.value;
2708 sec = h->root.u.def.section;
2710 else
2712 /* If $global$ isn't defined, we make one up ourselves
2713 from the start of .data, .plt, or .got. */
2714 struct elf32_hppa_link_hash_table *hplink;
2716 hplink = hppa_link_hash_table (info);
2717 gp_val = 0;
2718 sec = bfd_get_section_by_name (abfd, ".data");
2719 if (sec == NULL)
2720 sec = hplink->splt;
2721 if (sec == NULL)
2722 sec = hplink->sgot;
2723 if (sec == NULL)
2725 (*info->callbacks->undefined_symbol)
2726 (info, "$global$", abfd, NULL, 0, true);
2727 return false;
2731 elf_gp (abfd) = (gp_val
2732 + sec->output_section->vma
2733 + sec->output_offset);
2734 return true;
2738 /* Build all the stubs associated with the current output file. The
2739 stubs are kept in a hash table attached to the main linker hash
2740 table. We also set up the .plt entries for statically linked PIC
2741 functions here. This function is called via hppaelf_finish in the
2742 linker. */
2744 boolean
2745 elf32_hppa_build_stubs (info)
2746 struct bfd_link_info *info;
2748 asection *stub_sec;
2749 struct bfd_hash_table *table;
2750 struct elf32_hppa_link_hash_table *hplink;
2752 hplink = hppa_link_hash_table (info);
2754 for (stub_sec = hplink->stub_bfd->sections;
2755 stub_sec != NULL;
2756 stub_sec = stub_sec->next)
2758 unsigned int size;
2760 /* Allocate memory to hold the linker stubs. */
2761 size = bfd_section_size (hplink->stub_bfd, stub_sec);
2762 stub_sec->contents = (unsigned char *) bfd_zalloc (hplink->stub_bfd,
2763 size);
2764 if (stub_sec->contents == NULL && size != 0)
2765 return false;
2768 /* Build the stubs as directed by the stub hash table. */
2769 memset (hplink->offset, 0,
2770 hplink->stub_bfd->section_count * sizeof (bfd_vma));
2772 table = &hplink->stub_hash_table;
2773 bfd_hash_traverse (table, hppa_build_one_stub, info);
2775 return true;
2779 /* Perform a relocation as part of a final link. */
2781 static bfd_reloc_status_type
2782 final_link_relocate (input_section, contents, rel, value, info, sym_sec, h)
2783 asection *input_section;
2784 bfd_byte *contents;
2785 const Elf_Internal_Rela *rel;
2786 bfd_vma value;
2787 struct bfd_link_info *info;
2788 asection *sym_sec;
2789 struct elf32_hppa_link_hash_entry *h;
2791 int insn;
2792 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
2793 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
2794 int r_format = howto->bitsize;
2795 enum hppa_reloc_field_selector_type_alt r_field;
2796 bfd *input_bfd = input_section->owner;
2797 bfd_vma offset = rel->r_offset;
2798 bfd_vma max_branch_offset = 0;
2799 bfd_byte *hit_data = contents + offset;
2800 bfd_signed_vma addend = rel->r_addend;
2801 bfd_vma location;
2802 struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
2803 int val;
2805 if (r_type == R_PARISC_NONE)
2806 return bfd_reloc_ok;
2808 insn = bfd_get_32 (input_bfd, hit_data);
2810 /* Find out where we are and where we're going. */
2811 location = (offset +
2812 input_section->output_offset +
2813 input_section->output_section->vma);
2815 switch (r_type)
2817 case R_PARISC_PCREL12F:
2818 case R_PARISC_PCREL17F:
2819 case R_PARISC_PCREL22F:
2820 /* If this is a call to a function defined in another dynamic
2821 library, or if it is a call to a PIC function in the same
2822 object, then find the import stub in the stub hash. */
2823 if (sym_sec == NULL
2824 || sym_sec->output_section == NULL
2825 || (h != NULL && h->pic_call))
2827 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
2828 h, rel, info);
2829 if (stub_entry != NULL)
2831 value = (stub_entry->stub_offset
2832 + stub_entry->stub_sec->output_offset
2833 + stub_entry->stub_sec->output_section->vma);
2834 addend = 0;
2836 else if (sym_sec == NULL && h != NULL
2837 && h->elf.root.type == bfd_link_hash_undefweak)
2839 /* It's OK if undefined weak. Make undefined weak
2840 branches go nowhere. */
2841 value = location;
2842 addend = 0;
2844 else
2845 return bfd_reloc_notsupported;
2847 /* Fall thru. */
2849 case R_PARISC_PCREL21L:
2850 case R_PARISC_PCREL17C:
2851 case R_PARISC_PCREL17R:
2852 case R_PARISC_PCREL14R:
2853 case R_PARISC_PCREL14F:
2854 /* Make it a pc relative offset. */
2855 value -= location;
2856 addend -= 8;
2857 break;
2859 case R_PARISC_DPREL21L:
2860 case R_PARISC_DPREL14R:
2861 case R_PARISC_DPREL14F:
2862 /* For all the DP relative relocations, we need to examine the symbol's
2863 section. If it's a code section, then "data pointer relative" makes
2864 no sense. In that case we don't adjust the "value", and for 21 bit
2865 addil instructions, we change the source addend register from %dp to
2866 %r0. This situation commonly arises when a variable's "constness"
2867 is declared differently from the way the variable is defined. For
2868 instance: "extern int foo" with foo defined as "const int foo". */
2869 if (sym_sec == NULL)
2870 break;
2871 if ((sym_sec->flags & SEC_CODE) != 0)
2873 if ((insn & ((0x3f << 26) | (0x1f << 21)))
2874 == (((int) OP_ADDIL << 26) | (27 << 21)))
2876 insn &= ~ (0x1f << 21);
2877 #if 1 /* debug them */
2878 (*_bfd_error_handler)
2879 (_("%s(%s+0x%lx): fixing %s"),
2880 bfd_get_filename (input_bfd),
2881 input_section->name,
2882 (long) rel->r_offset,
2883 howto->name);
2884 #endif
2886 /* Now try to make things easy for the dynamic linker. */
2888 break;
2890 /* Fall thru */
2892 case R_PARISC_DLTIND21L:
2893 case R_PARISC_DLTIND14R:
2894 case R_PARISC_DLTIND14F:
2895 value -= elf_gp (input_section->output_section->owner);
2896 break;
2898 default:
2899 break;
2902 switch (r_type)
2904 case R_PARISC_DIR32:
2905 case R_PARISC_DIR17F:
2906 case R_PARISC_PCREL17C:
2907 case R_PARISC_PCREL14F:
2908 case R_PARISC_DPREL14F:
2909 case R_PARISC_PLABEL32:
2910 case R_PARISC_DLTIND14F:
2911 case R_PARISC_SEGBASE:
2912 case R_PARISC_SEGREL32:
2913 r_field = e_fsel;
2914 break;
2916 case R_PARISC_DIR21L:
2917 case R_PARISC_PCREL21L:
2918 case R_PARISC_DPREL21L:
2919 case R_PARISC_PLABEL21L:
2920 case R_PARISC_DLTIND21L:
2921 r_field = e_lrsel;
2922 break;
2924 case R_PARISC_DIR17R:
2925 case R_PARISC_PCREL17R:
2926 case R_PARISC_DIR14R:
2927 case R_PARISC_PCREL14R:
2928 case R_PARISC_DPREL14R:
2929 case R_PARISC_PLABEL14R:
2930 case R_PARISC_DLTIND14R:
2931 r_field = e_rrsel;
2932 break;
2934 case R_PARISC_PCREL12F:
2935 case R_PARISC_PCREL17F:
2936 case R_PARISC_PCREL22F:
2937 r_field = e_fsel;
2939 if (r_type == (unsigned int) R_PARISC_PCREL17F)
2941 max_branch_offset = (1 << (17-1)) << 2;
2943 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
2945 max_branch_offset = (1 << (12-1)) << 2;
2947 else
2949 max_branch_offset = (1 << (22-1)) << 2;
2952 /* sym_sec is NULL on undefined weak syms or when shared on
2953 undefined syms. We've already checked for a stub for the
2954 shared undefined case. */
2955 if (sym_sec == NULL)
2956 break;
2958 /* If the branch is out of reach, then redirect the
2959 call to the local stub for this function. */
2960 if (value + addend + max_branch_offset >= 2*max_branch_offset)
2962 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
2963 h, rel, info);
2964 if (stub_entry == NULL)
2965 return bfd_reloc_notsupported;
2967 /* Munge up the value and addend so that we call the stub
2968 rather than the procedure directly. */
2969 value = (stub_entry->stub_offset
2970 + stub_entry->stub_sec->output_offset
2971 + stub_entry->stub_sec->output_section->vma
2972 - location);
2973 addend = -8;
2975 break;
2977 /* Something we don't know how to handle. */
2978 default:
2979 return bfd_reloc_notsupported;
2982 /* Make sure we can reach the stub. */
2983 if (max_branch_offset != 0
2984 && value + addend + max_branch_offset >= 2*max_branch_offset)
2986 (*_bfd_error_handler)
2987 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
2988 bfd_get_filename (input_bfd),
2989 input_section->name,
2990 (long) rel->r_offset,
2991 stub_entry->root.string);
2992 return bfd_reloc_notsupported;
2995 val = hppa_field_adjust (value, addend, r_field);
2997 switch (r_type)
2999 case R_PARISC_PCREL12F:
3000 case R_PARISC_PCREL17C:
3001 case R_PARISC_PCREL17F:
3002 case R_PARISC_PCREL17R:
3003 case R_PARISC_PCREL22F:
3004 case R_PARISC_DIR17F:
3005 case R_PARISC_DIR17R:
3006 /* This is a branch. Divide the offset by four.
3007 Note that we need to decide whether it's a branch or
3008 otherwise by inspecting the reloc. Inspecting insn won't
3009 work as insn might be from a .word directive. */
3010 val >>= 2;
3011 break;
3013 default:
3014 break;
3017 insn = hppa_rebuild_insn (insn, val, r_format);
3019 /* Update the instruction word. */
3020 bfd_put_32 (input_bfd, (unsigned int) insn, hit_data);
3021 return bfd_reloc_ok;
3025 /* Relocate an HPPA ELF section. */
3027 static boolean
3028 elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
3029 contents, relocs, local_syms, local_sections)
3030 bfd *output_bfd;
3031 struct bfd_link_info *info;
3032 bfd *input_bfd;
3033 asection *input_section;
3034 bfd_byte *contents;
3035 Elf_Internal_Rela *relocs;
3036 Elf_Internal_Sym *local_syms;
3037 asection **local_sections;
3039 bfd *dynobj;
3040 bfd_vma *local_got_offsets;
3041 struct elf32_hppa_link_hash_table *hplink;
3042 Elf_Internal_Shdr *symtab_hdr;
3043 Elf_Internal_Rela *rel;
3044 Elf_Internal_Rela *relend;
3045 asection *sreloc;
3047 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3049 dynobj = elf_hash_table (info)->dynobj;
3050 local_got_offsets = elf_local_got_offsets (input_bfd);
3051 hplink = hppa_link_hash_table (info);
3052 sreloc = NULL;
3054 rel = relocs;
3055 relend = relocs + input_section->reloc_count;
3056 for (; rel < relend; rel++)
3058 unsigned int r_type;
3059 reloc_howto_type *howto;
3060 unsigned int r_symndx;
3061 struct elf32_hppa_link_hash_entry *h;
3062 Elf_Internal_Sym *sym;
3063 asection *sym_sec;
3064 bfd_vma relocation;
3065 bfd_reloc_status_type r;
3066 const char *sym_name;
3067 boolean pltrel;
3069 r_type = ELF32_R_TYPE (rel->r_info);
3070 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3072 bfd_set_error (bfd_error_bad_value);
3073 return false;
3075 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3076 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3077 continue;
3079 r_symndx = ELF32_R_SYM (rel->r_info);
3081 if (info->relocateable)
3083 /* This is a relocateable link. We don't have to change
3084 anything, unless the reloc is against a section symbol,
3085 in which case we have to adjust according to where the
3086 section symbol winds up in the output section. */
3087 if (r_symndx < symtab_hdr->sh_info)
3089 sym = local_syms + r_symndx;
3090 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3092 sym_sec = local_sections[r_symndx];
3093 rel->r_addend += sym_sec->output_offset;
3096 continue;
3099 /* This is a final link. */
3100 h = NULL;
3101 sym = NULL;
3102 sym_sec = NULL;
3103 if (r_symndx < symtab_hdr->sh_info)
3105 /* This is a local symbol, h defaults to NULL. */
3106 sym = local_syms + r_symndx;
3107 sym_sec = local_sections[r_symndx];
3108 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
3109 ? 0 : sym->st_value)
3110 + sym_sec->output_offset
3111 + sym_sec->output_section->vma);
3113 else
3115 int indx;
3117 /* It's a global; Find its entry in the link hash. */
3118 indx = r_symndx - symtab_hdr->sh_info;
3119 h = ((struct elf32_hppa_link_hash_entry *)
3120 elf_sym_hashes (input_bfd)[indx]);
3121 while (h->elf.root.type == bfd_link_hash_indirect
3122 || h->elf.root.type == bfd_link_hash_warning)
3123 h = (struct elf32_hppa_link_hash_entry *) h->elf.root.u.i.link;
3125 relocation = 0;
3126 if (h->elf.root.type == bfd_link_hash_defined
3127 || h->elf.root.type == bfd_link_hash_defweak)
3129 sym_sec = h->elf.root.u.def.section;
3130 /* If sym_sec->output_section is NULL, then it's a
3131 symbol defined in a shared library. */
3132 if (sym_sec->output_section != NULL)
3133 relocation = (h->elf.root.u.def.value
3134 + sym_sec->output_offset
3135 + sym_sec->output_section->vma);
3137 else if (h->elf.root.type == bfd_link_hash_undefweak)
3139 else if (info->shared && !info->no_undefined
3140 && ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT)
3142 if (info->symbolic)
3143 if (!((*info->callbacks->undefined_symbol)
3144 (info, h->elf.root.root.string, input_bfd,
3145 input_section, rel->r_offset, false)))
3146 return false;
3148 else
3150 if (!((*info->callbacks->undefined_symbol)
3151 (info, h->elf.root.root.string, input_bfd,
3152 input_section, rel->r_offset, true)))
3153 return false;
3157 /* Do any required modifications to the relocation value, and
3158 determine what types of dynamic info we need to output, if
3159 any. */
3160 pltrel = 0;
3161 switch (r_type)
3163 case R_PARISC_DLTIND14F:
3164 case R_PARISC_DLTIND14R:
3165 case R_PARISC_DLTIND21L:
3166 /* Relocation is to the entry for this symbol in the global
3167 offset table. */
3168 if (h != NULL)
3170 bfd_vma off;
3172 off = h->elf.got.offset;
3173 BFD_ASSERT (off != (bfd_vma) -1);
3175 if (! elf_hash_table (info)->dynamic_sections_created
3176 || (info->shared
3177 && (info->symbolic || h->elf.dynindx == -1)
3178 && (h->elf.elf_link_hash_flags
3179 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3181 /* This is actually a static link, or it is a
3182 -Bsymbolic link and the symbol is defined
3183 locally, or the symbol was forced to be local
3184 because of a version file. We must initialize
3185 this entry in the global offset table. Since the
3186 offset must always be a multiple of 4, we use the
3187 least significant bit to record whether we have
3188 initialized it already.
3190 When doing a dynamic link, we create a .rela.got
3191 relocation entry to initialize the value. This
3192 is done in the finish_dynamic_symbol routine. */
3193 if ((off & 1) != 0)
3194 off &= ~1;
3195 else
3197 bfd_put_32 (output_bfd, relocation,
3198 hplink->sgot->contents + off);
3199 h->elf.got.offset |= 1;
3203 relocation = off;
3205 else
3207 /* Local symbol case. */
3208 bfd_vma off;
3210 BFD_ASSERT (local_got_offsets != NULL
3211 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3213 off = local_got_offsets[r_symndx];
3215 /* The offset must always be a multiple of 4. We use
3216 the least significant bit to record whether we have
3217 already generated the necessary reloc. */
3218 if ((off & 1) != 0)
3219 off &= ~1;
3220 else
3222 bfd_put_32 (output_bfd, relocation,
3223 hplink->sgot->contents + off);
3225 if (info->shared)
3227 /* Output a dynamic SEGREL32 relocation for this
3228 GOT entry. In this case it is relative to
3229 the base of the object because the symbol
3230 index is zero. */
3231 Elf_Internal_Rela outrel;
3232 asection *srelgot = hplink->srelgot;
3234 outrel.r_offset = (off
3235 + hplink->sgot->output_offset
3236 + hplink->sgot->output_section->vma);
3237 outrel.r_info = ELF32_R_INFO (0, R_PARISC_SEGREL32);
3238 outrel.r_addend = relocation;
3239 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3240 ((Elf32_External_Rela *)
3241 srelgot->contents
3242 + srelgot->reloc_count));
3243 ++srelgot->reloc_count;
3246 local_got_offsets[r_symndx] |= 1;
3249 relocation = off;
3252 /* Add the base of the GOT to the relocation value. */
3253 relocation += (hplink->sgot->output_offset
3254 + hplink->sgot->output_section->vma);
3255 break;
3257 case R_PARISC_PLABEL14R:
3258 case R_PARISC_PLABEL21L:
3259 case R_PARISC_PLABEL32:
3260 /* If we have a global symbol with a PLT slot, then redirect
3261 this relocation to it. */
3262 if (h != NULL
3263 && h->elf.dynindx != -1
3264 && h->elf.plt.offset != (bfd_vma) -1)
3266 /* PLABELs contain function pointers. Relocation is
3267 to the entry for the function in the .plt. The
3268 magic +2 offset signals to $$dyncall that the
3269 function pointer is in the .plt and thus has a gp
3270 pointer too. */
3271 relocation = (h->elf.plt.offset
3272 + hplink->splt->output_offset
3273 + hplink->splt->output_section->vma
3274 + 2);
3275 pltrel = 1;
3277 /* Fall through and possibly emit a dynamic relocation. */
3279 case R_PARISC_DIR17F:
3280 case R_PARISC_DIR17R:
3281 case R_PARISC_DIR14R:
3282 case R_PARISC_DIR21L:
3283 case R_PARISC_DPREL14F:
3284 case R_PARISC_DPREL14R:
3285 case R_PARISC_DPREL21L:
3286 case R_PARISC_DIR32:
3287 /* The reloc types handled here and this conditional
3288 expression must match the code in check_relocs and
3289 hppa_discard_copies. ie. We need exactly the same
3290 condition as in check_relocs, with some extra conditions
3291 (dynindx test in this case) to cater for relocs removed
3292 by hppa_discard_copies. */
3293 if ((input_section->flags & SEC_ALLOC) != 0
3294 && info->shared
3295 #if RELATIVE_DYNAMIC_RELOCS
3296 && (is_absolute_reloc (r_type)
3297 || ((!info->symbolic
3298 || (h != NULL
3299 && ((h->elf.elf_link_hash_flags
3300 & ELF_LINK_HASH_DEF_REGULAR) == 0
3301 || h->elf.root.type == bfd_link_hash_defweak)))
3302 && (h == NULL || h->elf.dynindx != -1)))
3303 #endif
3306 Elf_Internal_Rela outrel;
3307 boolean skip;
3309 /* When generating a shared object, these relocations
3310 are copied into the output file to be resolved at run
3311 time. */
3313 if (sreloc == NULL)
3315 const char *name;
3317 name = (bfd_elf_string_from_elf_section
3318 (input_bfd,
3319 elf_elfheader (input_bfd)->e_shstrndx,
3320 elf_section_data (input_section)->rel_hdr.sh_name));
3321 if (name == NULL)
3322 return false;
3323 sreloc = bfd_get_section_by_name (dynobj, name);
3324 BFD_ASSERT (sreloc != NULL);
3327 outrel.r_offset = rel->r_offset;
3328 outrel.r_addend = rel->r_addend;
3329 skip = false;
3330 if (elf_section_data (input_section)->stab_info != NULL)
3332 bfd_vma off;
3334 off = (_bfd_stab_section_offset
3335 (output_bfd, &elf_hash_table (info)->stab_info,
3336 input_section,
3337 &elf_section_data (input_section)->stab_info,
3338 rel->r_offset));
3339 if (off == (bfd_vma) -1)
3340 skip = true;
3341 outrel.r_offset = off;
3344 outrel.r_offset += (input_section->output_offset
3345 + input_section->output_section->vma);
3347 if (skip)
3349 memset (&outrel, 0, sizeof (outrel));
3351 else if (!pltrel
3352 && h != NULL
3353 && ((!info->symbolic && h->elf.dynindx != -1)
3354 || (h->elf.elf_link_hash_flags
3355 & ELF_LINK_HASH_DEF_REGULAR) == 0))
3357 outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3359 else /* It's a local symbol, or one marked to become local. */
3361 int indx = 0;
3363 /* Add the absolute offset of the symbol. */
3364 outrel.r_addend += relocation;
3366 if (sym_sec != NULL
3367 && sym_sec->output_section != NULL
3368 && ! bfd_is_abs_section (sym_sec))
3370 indx = elf_section_data (sym_sec->output_section)->dynindx;
3371 /* We are turning this relocation into one
3372 against a section symbol, so subtract out the
3373 output section's address but not the offset
3374 of the input section in the output section. */
3375 outrel.r_addend -= sym_sec->output_section->vma;
3378 outrel.r_info = ELF32_R_INFO (indx, r_type);
3381 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3382 ((Elf32_External_Rela *)
3383 sreloc->contents
3384 + sreloc->reloc_count));
3385 ++sreloc->reloc_count;
3387 break;
3389 default:
3390 break;
3393 r = final_link_relocate (input_section, contents, rel, relocation,
3394 info, sym_sec, h);
3396 if (r == bfd_reloc_ok)
3397 continue;
3399 if (h != NULL)
3400 sym_name = h->elf.root.root.string;
3401 else
3403 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3404 symtab_hdr->sh_link,
3405 sym->st_name);
3406 if (sym_name == NULL)
3407 return false;
3408 if (*sym_name == '\0')
3409 sym_name = bfd_section_name (input_bfd, sym_sec);
3412 howto = elf_hppa_howto_table + r_type;
3414 if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
3416 (*_bfd_error_handler)
3417 (_("%s(%s+0x%lx): cannot handle %s for %s"),
3418 bfd_get_filename (input_bfd),
3419 input_section->name,
3420 (long) rel->r_offset,
3421 howto->name,
3422 sym_name);
3424 else
3426 if (!((*info->callbacks->reloc_overflow)
3427 (info, sym_name, howto->name, (bfd_vma) 0,
3428 input_bfd, input_section, rel->r_offset)))
3429 return false;
3433 return true;
3437 /* Finish up dynamic symbol handling. We set the contents of various
3438 dynamic sections here. */
3440 static boolean
3441 elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
3442 bfd *output_bfd;
3443 struct bfd_link_info *info;
3444 struct elf_link_hash_entry *h;
3445 Elf_Internal_Sym *sym;
3447 struct elf32_hppa_link_hash_table *hplink;
3448 bfd *dynobj;
3450 hplink = hppa_link_hash_table (info);
3451 dynobj = elf_hash_table (info)->dynobj;
3453 /* Millicode symbols should not be put in the dynamic
3454 symbol table under any circumstances. */
3455 if (ELF_ST_TYPE (sym->st_info) == STT_PARISC_MILLI)
3456 h->dynindx = -1;
3458 if (h->plt.offset != (bfd_vma) -1)
3460 bfd_vma value;
3461 Elf_Internal_Rela rel;
3463 /* This symbol has an entry in the procedure linkage table. Set
3464 it up.
3466 The format of a plt entry is
3467 <funcaddr> <__gp>. */
3469 /* We do not actually care about the value in the PLT entry if
3470 we are creating a shared library and the symbol is still
3471 undefined; We create a dynamic relocation to fill in the
3472 correct value. */
3473 value = 0;
3474 if (h->root.type == bfd_link_hash_defined
3475 || h->root.type == bfd_link_hash_defweak)
3477 value = h->root.u.def.value;
3478 if (h->root.u.def.section->output_section != NULL)
3479 value += (h->root.u.def.section->output_offset
3480 + h->root.u.def.section->output_section->vma);
3483 bfd_put_32 (hplink->splt->owner, value,
3484 hplink->splt->contents + h->plt.offset);
3485 value = elf_gp (hplink->splt->output_section->owner);
3486 bfd_put_32 (hplink->splt->owner, value,
3487 hplink->splt->contents + h->plt.offset + 4);
3489 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call
3490 && h->dynindx != -1)
3492 /* Create a dynamic IPLT relocation for this entry. */
3493 rel.r_offset = (h->plt.offset
3494 + hplink->splt->output_offset
3495 + hplink->splt->output_section->vma);
3496 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
3497 rel.r_addend = 0;
3499 bfd_elf32_swap_reloca_out (hplink->splt->output_section->owner,
3500 &rel,
3501 ((Elf32_External_Rela *)
3502 hplink->srelplt->contents
3503 + hplink->srelplt->reloc_count));
3504 hplink->srelplt->reloc_count++;
3507 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3509 /* Mark the symbol as undefined, rather than as defined in
3510 the .plt section. Leave the value alone. */
3511 sym->st_shndx = SHN_UNDEF;
3515 if (h->got.offset != (bfd_vma) -1)
3517 Elf_Internal_Rela rel;
3519 /* This symbol has an entry in the global offset table. Set it
3520 up. */
3522 rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
3523 + hplink->sgot->output_offset
3524 + hplink->sgot->output_section->vma);
3526 /* If this is a static link, or it is a -Bsymbolic link and the
3527 symbol is defined locally or was forced to be local because
3528 of a version file, we just want to emit a RELATIVE reloc.
3529 The entry in the global offset table will already have been
3530 initialized in the relocate_section function. */
3531 if (! elf_hash_table (info)->dynamic_sections_created
3532 || (info->shared
3533 && (info->symbolic || h->dynindx == -1)
3534 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
3536 rel.r_info = ELF32_R_INFO (0, R_PARISC_SEGREL32);
3537 rel.r_addend = (h->root.u.def.value
3538 + h->root.u.def.section->output_offset
3539 + h->root.u.def.section->output_section->vma);
3541 else
3543 BFD_ASSERT((h->got.offset & 1) == 0);
3544 bfd_put_32 (output_bfd, (bfd_vma) 0,
3545 hplink->sgot->contents + h->got.offset);
3546 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
3547 rel.r_addend = 0;
3550 bfd_elf32_swap_reloca_out (output_bfd, &rel,
3551 ((Elf32_External_Rela *)
3552 hplink->srelgot->contents
3553 + hplink->srelgot->reloc_count));
3554 ++hplink->srelgot->reloc_count;
3557 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3559 asection *s;
3560 Elf_Internal_Rela rel;
3562 /* This symbol needs a copy reloc. Set it up. */
3564 BFD_ASSERT (h->dynindx != -1
3565 && (h->root.type == bfd_link_hash_defined
3566 || h->root.type == bfd_link_hash_defweak));
3568 s = hplink->srelbss;
3570 rel.r_offset = (h->root.u.def.value
3571 + h->root.u.def.section->output_offset
3572 + h->root.u.def.section->output_section->vma);
3573 rel.r_addend = 0;
3574 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
3575 bfd_elf32_swap_reloca_out (output_bfd, &rel,
3576 ((Elf32_External_Rela *) s->contents
3577 + s->reloc_count));
3578 ++s->reloc_count;
3581 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3582 if (h->root.root.string[0] == '_'
3583 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
3584 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
3586 sym->st_shndx = SHN_ABS;
3589 return true;
3593 /* Finish up the dynamic sections. */
3595 static boolean
3596 elf32_hppa_finish_dynamic_sections (output_bfd, info)
3597 bfd *output_bfd;
3598 struct bfd_link_info *info;
3600 bfd *dynobj;
3601 struct elf32_hppa_link_hash_table *hplink;
3602 asection *sdyn;
3604 dynobj = elf_hash_table (info)->dynobj;
3605 hplink = hppa_link_hash_table (info);
3607 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3609 if (elf_hash_table (info)->dynamic_sections_created)
3611 Elf32_External_Dyn *dyncon, *dynconend;
3613 BFD_ASSERT (sdyn != NULL);
3615 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3616 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3617 for (; dyncon < dynconend; dyncon++)
3619 Elf_Internal_Dyn dyn;
3620 asection *s;
3622 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3624 switch (dyn.d_tag)
3626 default:
3627 break;
3629 case DT_PLTGOT:
3630 /* Use PLTGOT to set the GOT register. */
3631 dyn.d_un.d_ptr = elf_gp (output_bfd);
3632 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3633 break;
3635 case DT_JMPREL:
3636 s = hplink->srelplt;
3637 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3638 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3639 break;
3641 case DT_PLTRELSZ:
3642 s = hplink->srelplt;
3643 if (s->_cooked_size != 0)
3644 dyn.d_un.d_val = s->_cooked_size;
3645 else
3646 dyn.d_un.d_val = s->_raw_size;
3647 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3648 break;
3653 /* Fill in the first entry in the global offset table.
3654 We use it to point to our dynamic section, if we have one. */
3655 if (hplink->sgot->_raw_size != 0)
3657 bfd_put_32 (output_bfd,
3658 (sdyn != NULL
3659 ? sdyn->output_section->vma + sdyn->output_offset
3660 : (bfd_vma) 0),
3661 hplink->sgot->contents);
3663 /* Set .got entry size. */
3664 elf_section_data (hplink->sgot->output_section)->this_hdr.sh_entsize = 4;
3667 /* Set plt entry size. */
3668 if (hplink->splt->_raw_size != 0)
3669 elf_section_data (hplink->splt->output_section)->this_hdr.sh_entsize = 8;
3671 return true;
3675 /* Called when writing out an object file to decide the type of a
3676 symbol. */
3677 static int
3678 elf32_hppa_elf_get_symbol_type (elf_sym, type)
3679 Elf_Internal_Sym *elf_sym;
3680 int type;
3682 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
3683 return STT_PARISC_MILLI;
3684 else
3685 return type;
3689 /* Misc BFD support code. */
3690 #define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
3691 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
3692 #define elf_info_to_howto elf_hppa_info_to_howto
3693 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
3695 /* Stuff for the BFD linker. */
3696 #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
3697 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
3698 #define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
3699 #define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
3700 #define elf_backend_check_relocs elf32_hppa_check_relocs
3701 #define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
3702 #define elf_backend_fake_sections elf_hppa_fake_sections
3703 #define elf_backend_relocate_section elf32_hppa_relocate_section
3704 #define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
3705 #define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
3706 #define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
3707 #define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
3708 #define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
3709 #define elf_backend_object_p elf32_hppa_object_p
3710 #define elf_backend_final_write_processing elf_hppa_final_write_processing
3711 #define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
3713 #define elf_backend_can_gc_sections 1
3714 #define elf_backend_plt_alignment 2
3715 #define elf_backend_want_got_plt 0
3716 #define elf_backend_plt_readonly 0
3717 #define elf_backend_want_plt_sym 0
3718 #define elf_backend_got_header_size 4
3720 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
3721 #define TARGET_BIG_NAME "elf32-hppa"
3722 #define ELF_ARCH bfd_arch_hppa
3723 #define ELF_MACHINE_CODE EM_PARISC
3724 #define ELF_MAXPAGESIZE 0x1000
3726 #include "elf32-target.h"