2001-03-22 Philip Blundell <philb@gnu.org>
[binutils.git] / bfd / elf32-hppa.c
blob06b60200dcd674f60549601407880013bb0cee7f
1 /* BFD back-end for HP PA-RISC ELF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001
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"
38 /* In order to gain some understanding of code in this file without
39 knowing all the intricate details of the linker, note the
40 following:
42 Functions named elf32_hppa_* are called by external routines, other
43 functions are only called locally. elf32_hppa_* functions appear
44 in this file more or less in the order in which they are called
45 from external routines. eg. elf32_hppa_check_relocs is called
46 early in the link process, elf32_hppa_finish_dynamic_sections is
47 one of the last functions. */
49 /* We use two hash tables to hold information for linking PA ELF objects.
51 The first is the elf32_hppa_link_hash_table which is derived
52 from the standard ELF linker hash table. We use this as a place to
53 attach other hash tables and static information.
55 The second is the stub hash table which is derived from the
56 base BFD hash table. The stub hash table holds the information
57 necessary to build the linker stubs during a link.
59 There are a number of different stubs generated by the linker.
61 Long branch stub:
62 : ldil LR'X,%r1
63 : be,n RR'X(%sr4,%r1)
65 PIC long branch stub:
66 : b,l .+8,%r1
67 : addil LR'X - ($PIC_pcrel$0 - 4),%r1
68 : be,n RR'X - ($PIC_pcrel$0 - 8)(%sr4,%r1)
70 Import stub to call shared library routine from normal object file
71 (single sub-space version)
72 : addil LR'lt_ptr+ltoff,%dp ; get procedure entry point
73 : ldw RR'lt_ptr+ltoff(%r1),%r21
74 : bv %r0(%r21)
75 : ldw RR'lt_ptr+ltoff+4(%r1),%r19 ; get new dlt value.
77 Import stub to call shared library routine from shared library
78 (single sub-space version)
79 : addil LR'ltoff,%r19 ; get procedure entry point
80 : ldw RR'ltoff(%r1),%r21
81 : bv %r0(%r21)
82 : ldw RR'ltoff+4(%r1),%r19 ; get new dlt value.
84 Import stub to call shared library routine from normal object file
85 (multiple sub-space support)
86 : addil LR'lt_ptr+ltoff,%dp ; get procedure entry point
87 : ldw RR'lt_ptr+ltoff(%r1),%r21
88 : ldw RR'lt_ptr+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 Import stub to call shared library routine from shared library
95 (multiple sub-space support)
96 : addil LR'ltoff,%r19 ; get procedure entry point
97 : ldw RR'ltoff(%r1),%r21
98 : ldw RR'ltoff+4(%r1),%r19 ; get new dlt value.
99 : ldsid (%r21),%r1
100 : mtsp %r1,%sr0
101 : be 0(%sr0,%r21) ; branch to target
102 : stw %rp,-24(%sp) ; save rp
104 Export stub to return from shared lib routine (multiple sub-space support)
105 One of these is created for each exported procedure in a shared
106 library (and stored in the shared lib). Shared lib routines are
107 called via the first instruction in the export stub so that we can
108 do an inter-space return. Not required for single sub-space.
109 : bl,n X,%rp ; trap the return
110 : nop
111 : ldw -24(%sp),%rp ; restore the original rp
112 : ldsid (%rp),%r1
113 : mtsp %r1,%sr0
114 : be,n 0(%sr0,%rp) ; inter-space return */
116 #define PLT_ENTRY_SIZE 8
117 #define PLABEL_PLT_ENTRY_SIZE PLT_ENTRY_SIZE
118 #define GOT_ENTRY_SIZE 4
119 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
121 static const bfd_byte plt_stub[] =
123 0x0e, 0x80, 0x10, 0x96, /* 1: ldw 0(%r20),%r22 */
124 0xea, 0xc0, 0xc0, 0x00, /* bv %r0(%r22) */
125 0x0e, 0x88, 0x10, 0x95, /* ldw 4(%r20),%r21 */
126 #define PLT_STUB_ENTRY (3*4)
127 0xea, 0x9f, 0x1f, 0xdd, /* b,l 1b,%r20 */
128 0xd6, 0x80, 0x1c, 0x1e, /* depi 0,31,2,%r20 */
129 0x00, 0xc0, 0xff, 0xee, /* 9: .word fixup_func */
130 0xde, 0xad, 0xbe, 0xef /* .word fixup_ltp */
133 /* Section name for stubs is the associated section name plus this
134 string. */
135 #define STUB_SUFFIX ".stub"
137 /* Setting the following non-zero makes all long branch stubs
138 generated during a shared link of the PIC variety. This saves on
139 relocs, but costs one extra instruction per stub. */
140 #ifndef LONG_BRANCH_PIC_IN_SHLIB
141 #define LONG_BRANCH_PIC_IN_SHLIB 1
142 #endif
144 /* Set this non-zero to use import stubs instead of long branch stubs
145 where a .plt entry exists for the symbol. This is a fairly useless
146 option as import stubs are bigger than PIC long branch stubs. */
147 #ifndef LONG_BRANCH_VIA_PLT
148 #define LONG_BRANCH_VIA_PLT 0
149 #endif
151 /* We don't need to copy any PC- or GP-relative dynamic relocs into a
152 shared object's dynamic section. */
153 #ifndef RELATIVE_DYNAMIC_RELOCS
154 #define RELATIVE_DYNAMIC_RELOCS 0
155 #endif
157 enum elf32_hppa_stub_type {
158 hppa_stub_long_branch,
159 hppa_stub_long_branch_shared,
160 hppa_stub_import,
161 hppa_stub_import_shared,
162 hppa_stub_export,
163 hppa_stub_none
166 struct elf32_hppa_stub_hash_entry {
168 /* Base hash table entry structure. */
169 struct bfd_hash_entry root;
171 /* The stub section. */
172 asection *stub_sec;
174 #if ! LONG_BRANCH_PIC_IN_SHLIB
175 /* It's associated reloc section. */
176 asection *reloc_sec;
177 #endif
179 /* Offset within stub_sec of the beginning of this stub. */
180 bfd_vma stub_offset;
182 /* Given the symbol's value and its section we can determine its final
183 value when building the stubs (so the stub knows where to jump. */
184 bfd_vma target_value;
185 asection *target_section;
187 enum elf32_hppa_stub_type stub_type;
189 /* The symbol table entry, if any, that this was derived from. */
190 struct elf32_hppa_link_hash_entry *h;
192 /* Where this stub is being called from, or, in the case of combined
193 stub sections, the first input section in the group. */
194 asection *id_sec;
197 struct elf32_hppa_link_hash_entry {
199 struct elf_link_hash_entry elf;
201 /* A pointer to the most recently used stub hash entry against this
202 symbol. */
203 struct elf32_hppa_stub_hash_entry *stub_cache;
205 #if ! LONG_BRANCH_PIC_IN_SHLIB
206 /* Used to track whether we have allocated space for a long branch
207 stub relocation for this symbol in the given section. */
208 asection *stub_reloc_sec;
209 #endif
211 #if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
212 /* Used to count relocations for delayed sizing of relocation
213 sections. */
214 struct elf32_hppa_dyn_reloc_entry {
216 /* Next relocation in the chain. */
217 struct elf32_hppa_dyn_reloc_entry *next;
219 /* The section in dynobj. */
220 asection *section;
222 /* Number of relocs copied in this section. */
223 bfd_size_type count;
224 } *reloc_entries;
225 #endif
227 /* Set during a static link if we detect a function is PIC. */
228 unsigned int maybe_pic_call:1;
230 /* Set if the only reason we need a .plt entry is for a non-PIC to
231 PIC function call. */
232 unsigned int pic_call:1;
234 /* Set if this symbol is used by a plabel reloc. */
235 unsigned int plabel:1;
237 /* Set if this symbol is an init or fini function and thus should
238 use an absolute reloc. */
239 unsigned int plt_abs:1;
242 struct elf32_hppa_link_hash_table {
244 /* The main hash table. */
245 struct elf_link_hash_table root;
247 /* The stub hash table. */
248 struct bfd_hash_table stub_hash_table;
250 /* Linker stub bfd. */
251 bfd *stub_bfd;
253 /* Linker call-backs. */
254 asection * (*add_stub_section) PARAMS ((const char *, asection *));
255 void (*layout_sections_again) PARAMS ((void));
257 /* Array to keep track of which stub sections have been created, and
258 information on stub grouping. */
259 struct map_stub {
260 /* This is the section to which stubs in the group will be
261 attached. */
262 asection *link_sec;
263 /* The stub section. */
264 asection *stub_sec;
265 #if ! LONG_BRANCH_PIC_IN_SHLIB
266 /* The stub section's reloc section. */
267 asection *reloc_sec;
268 #endif
269 } *stub_group;
271 /* Short-cuts to get to dynamic linker sections. */
272 asection *sgot;
273 asection *srelgot;
274 asection *splt;
275 asection *srelplt;
276 asection *sdynbss;
277 asection *srelbss;
279 /* Used during a final link to store the base of the text and data
280 segments so that we can perform SEGREL relocations. */
281 bfd_vma text_segment_base;
282 bfd_vma data_segment_base;
284 /* Whether we support multiple sub-spaces for shared libs. */
285 unsigned int multi_subspace:1;
287 /* Flags set when PCREL12F and PCREL17F branches detected. Used to
288 select suitable defaults for the stub group size. */
289 unsigned int has_12bit_branch:1;
290 unsigned int has_17bit_branch:1;
292 /* Set if we need a .plt stub to support lazy dynamic linking. */
293 unsigned int need_plt_stub:1;
296 /* Various hash macros and functions. */
297 #define hppa_link_hash_table(p) \
298 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
300 #define hppa_stub_hash_lookup(table, string, create, copy) \
301 ((struct elf32_hppa_stub_hash_entry *) \
302 bfd_hash_lookup ((table), (string), (create), (copy)))
304 static struct bfd_hash_entry *stub_hash_newfunc
305 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
307 static struct bfd_hash_entry *hppa_link_hash_newfunc
308 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
310 static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
311 PARAMS ((bfd *));
313 /* Stub handling functions. */
314 static char *hppa_stub_name
315 PARAMS ((const asection *, const asection *,
316 const struct elf32_hppa_link_hash_entry *,
317 const Elf_Internal_Rela *));
319 static struct elf32_hppa_stub_hash_entry *hppa_get_stub_entry
320 PARAMS ((const asection *, const asection *,
321 struct elf32_hppa_link_hash_entry *,
322 const Elf_Internal_Rela *,
323 struct elf32_hppa_link_hash_table *));
325 static struct elf32_hppa_stub_hash_entry *hppa_add_stub
326 PARAMS ((const char *, asection *, struct elf32_hppa_link_hash_table *));
328 static enum elf32_hppa_stub_type hppa_type_of_stub
329 PARAMS ((asection *, const Elf_Internal_Rela *,
330 struct elf32_hppa_link_hash_entry *, bfd_vma));
332 static boolean hppa_build_one_stub
333 PARAMS ((struct bfd_hash_entry *, PTR));
335 static boolean hppa_size_one_stub
336 PARAMS ((struct bfd_hash_entry *, PTR));
338 /* BFD and elf backend functions. */
339 static boolean elf32_hppa_object_p PARAMS ((bfd *));
341 static boolean elf32_hppa_add_symbol_hook
342 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
343 const char **, flagword *, asection **, bfd_vma *));
345 static boolean elf32_hppa_create_dynamic_sections
346 PARAMS ((bfd *, struct bfd_link_info *));
348 static boolean elf32_hppa_check_relocs
349 PARAMS ((bfd *, struct bfd_link_info *,
350 asection *, const Elf_Internal_Rela *));
352 static asection *elf32_hppa_gc_mark_hook
353 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
354 struct elf_link_hash_entry *, Elf_Internal_Sym *));
356 static boolean elf32_hppa_gc_sweep_hook
357 PARAMS ((bfd *, struct bfd_link_info *,
358 asection *, const Elf_Internal_Rela *));
360 static void elf32_hppa_hide_symbol
361 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
363 static boolean elf32_hppa_adjust_dynamic_symbol
364 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
366 static boolean hppa_handle_PIC_calls
367 PARAMS ((struct elf_link_hash_entry *, PTR));
369 #if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
370 || RELATIVE_DYNAMIC_RELOCS)
371 static boolean hppa_discard_copies
372 PARAMS ((struct elf_link_hash_entry *, PTR));
373 #endif
375 static boolean clobber_millicode_symbols
376 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
378 static boolean elf32_hppa_size_dynamic_sections
379 PARAMS ((bfd *, struct bfd_link_info *));
381 static boolean elf32_hppa_final_link
382 PARAMS ((bfd *, struct bfd_link_info *));
384 static void hppa_record_segment_addr
385 PARAMS ((bfd *, asection *, PTR));
387 static bfd_reloc_status_type final_link_relocate
388 PARAMS ((asection *, bfd_byte *, const Elf_Internal_Rela *,
389 bfd_vma, struct elf32_hppa_link_hash_table *, asection *,
390 struct elf32_hppa_link_hash_entry *));
392 static boolean elf32_hppa_relocate_section
393 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
394 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
396 static int hppa_unwind_entry_compare
397 PARAMS ((const PTR, const PTR));
399 static boolean elf32_hppa_finish_dynamic_symbol
400 PARAMS ((bfd *, struct bfd_link_info *,
401 struct elf_link_hash_entry *, Elf_Internal_Sym *));
403 static boolean elf32_hppa_finish_dynamic_sections
404 PARAMS ((bfd *, struct bfd_link_info *));
406 static void elf32_hppa_post_process_headers
407 PARAMS ((bfd *, struct bfd_link_info *));
409 static int elf32_hppa_elf_get_symbol_type
410 PARAMS ((Elf_Internal_Sym *, int));
412 /* Assorted hash table functions. */
414 /* Initialize an entry in the stub hash table. */
416 static struct bfd_hash_entry *
417 stub_hash_newfunc (entry, table, string)
418 struct bfd_hash_entry *entry;
419 struct bfd_hash_table *table;
420 const char *string;
422 struct elf32_hppa_stub_hash_entry *ret;
424 ret = (struct elf32_hppa_stub_hash_entry *) entry;
426 /* Allocate the structure if it has not already been allocated by a
427 subclass. */
428 if (ret == NULL)
430 ret = ((struct elf32_hppa_stub_hash_entry *)
431 bfd_hash_allocate (table,
432 sizeof (struct elf32_hppa_stub_hash_entry)));
433 if (ret == NULL)
434 return NULL;
437 /* Call the allocation method of the superclass. */
438 ret = ((struct elf32_hppa_stub_hash_entry *)
439 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
441 if (ret)
443 /* Initialize the local fields. */
444 ret->stub_sec = NULL;
445 #if ! LONG_BRANCH_PIC_IN_SHLIB
446 ret->reloc_sec = NULL;
447 #endif
448 ret->stub_offset = 0;
449 ret->target_value = 0;
450 ret->target_section = NULL;
451 ret->stub_type = hppa_stub_long_branch;
452 ret->h = NULL;
453 ret->id_sec = NULL;
456 return (struct bfd_hash_entry *) ret;
459 /* Initialize an entry in the link hash table. */
461 static struct bfd_hash_entry *
462 hppa_link_hash_newfunc (entry, table, string)
463 struct bfd_hash_entry *entry;
464 struct bfd_hash_table *table;
465 const char *string;
467 struct elf32_hppa_link_hash_entry *ret;
469 ret = (struct elf32_hppa_link_hash_entry *) entry;
471 /* Allocate the structure if it has not already been allocated by a
472 subclass. */
473 if (ret == NULL)
475 ret = ((struct elf32_hppa_link_hash_entry *)
476 bfd_hash_allocate (table,
477 sizeof (struct elf32_hppa_link_hash_entry)));
478 if (ret == NULL)
479 return NULL;
482 /* Call the allocation method of the superclass. */
483 ret = ((struct elf32_hppa_link_hash_entry *)
484 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
485 table, string));
487 if (ret)
489 /* Initialize the local fields. */
490 #if ! LONG_BRANCH_PIC_IN_SHLIB
491 ret->stub_reloc_sec = NULL;
492 #endif
493 ret->stub_cache = NULL;
494 #if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
495 ret->reloc_entries = NULL;
496 #endif
497 ret->maybe_pic_call = 0;
498 ret->pic_call = 0;
499 ret->plabel = 0;
500 ret->plt_abs = 0;
503 return (struct bfd_hash_entry *) ret;
506 /* Create the derived linker hash table. The PA ELF port uses the derived
507 hash table to keep information specific to the PA ELF linker (without
508 using static variables). */
510 static struct bfd_link_hash_table *
511 elf32_hppa_link_hash_table_create (abfd)
512 bfd *abfd;
514 struct elf32_hppa_link_hash_table *ret;
516 ret = ((struct elf32_hppa_link_hash_table *) bfd_alloc (abfd, sizeof (*ret)));
517 if (ret == NULL)
518 return NULL;
520 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd, hppa_link_hash_newfunc))
522 bfd_release (abfd, ret);
523 return NULL;
526 /* Init the stub hash table too. */
527 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc))
528 return NULL;
530 ret->stub_bfd = NULL;
531 ret->add_stub_section = NULL;
532 ret->layout_sections_again = NULL;
533 ret->stub_group = NULL;
534 ret->sgot = NULL;
535 ret->srelgot = NULL;
536 ret->splt = NULL;
537 ret->srelplt = NULL;
538 ret->sdynbss = NULL;
539 ret->srelbss = NULL;
540 ret->text_segment_base = (bfd_vma) -1;
541 ret->data_segment_base = (bfd_vma) -1;
542 ret->multi_subspace = 0;
543 ret->has_12bit_branch = 0;
544 ret->has_17bit_branch = 0;
545 ret->need_plt_stub = 0;
547 return &ret->root.root;
550 /* Build a name for an entry in the stub hash table. */
552 static char *
553 hppa_stub_name (input_section, sym_sec, hash, rel)
554 const asection *input_section;
555 const asection *sym_sec;
556 const struct elf32_hppa_link_hash_entry *hash;
557 const Elf_Internal_Rela *rel;
559 char *stub_name;
560 size_t len;
562 if (hash)
564 len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
565 stub_name = bfd_malloc (len);
566 if (stub_name != NULL)
568 sprintf (stub_name, "%08x_%s+%x",
569 input_section->id & 0xffffffff,
570 hash->elf.root.root.string,
571 (int) rel->r_addend & 0xffffffff);
574 else
576 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
577 stub_name = bfd_malloc (len);
578 if (stub_name != NULL)
580 sprintf (stub_name, "%08x_%x:%x+%x",
581 input_section->id & 0xffffffff,
582 sym_sec->id & 0xffffffff,
583 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
584 (int) rel->r_addend & 0xffffffff);
587 return stub_name;
590 /* Look up an entry in the stub hash. Stub entries are cached because
591 creating the stub name takes a bit of time. */
593 static struct elf32_hppa_stub_hash_entry *
594 hppa_get_stub_entry (input_section, sym_sec, hash, rel, hplink)
595 const asection *input_section;
596 const asection *sym_sec;
597 struct elf32_hppa_link_hash_entry *hash;
598 const Elf_Internal_Rela *rel;
599 struct elf32_hppa_link_hash_table *hplink;
601 struct elf32_hppa_stub_hash_entry *stub_entry;
602 const asection *id_sec;
604 /* If this input section is part of a group of sections sharing one
605 stub section, then use the id of the first section in the group.
606 Stub names need to include a section id, as there may well be
607 more than one stub used to reach say, printf, and we need to
608 distinguish between them. */
609 id_sec = hplink->stub_group[input_section->id].link_sec;
611 if (hash != NULL && hash->stub_cache != NULL
612 && hash->stub_cache->h == hash
613 && hash->stub_cache->id_sec == id_sec)
615 stub_entry = hash->stub_cache;
617 else
619 char *stub_name;
621 stub_name = hppa_stub_name (id_sec, sym_sec, hash, rel);
622 if (stub_name == NULL)
623 return NULL;
625 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
626 stub_name, false, false);
627 if (stub_entry == NULL)
629 if (hash == NULL || hash->elf.root.type != bfd_link_hash_undefweak)
630 (*_bfd_error_handler) (_("%s(%s+0x%lx): cannot find stub entry %s"),
631 bfd_get_filename (input_section->owner),
632 input_section->name,
633 (long) rel->r_offset,
634 stub_name);
636 else
638 if (hash != NULL)
639 hash->stub_cache = stub_entry;
642 free (stub_name);
645 return stub_entry;
648 /* Add a new stub entry to the stub hash. Not all fields of the new
649 stub entry are initialised. */
651 static struct elf32_hppa_stub_hash_entry *
652 hppa_add_stub (stub_name, section, hplink)
653 const char *stub_name;
654 asection *section;
655 struct elf32_hppa_link_hash_table *hplink;
657 asection *link_sec;
658 asection *stub_sec;
659 struct elf32_hppa_stub_hash_entry *stub_entry;
661 link_sec = hplink->stub_group[section->id].link_sec;
662 stub_sec = hplink->stub_group[section->id].stub_sec;
663 if (stub_sec == NULL)
665 stub_sec = hplink->stub_group[link_sec->id].stub_sec;
666 if (stub_sec == NULL)
668 size_t len;
669 char *s_name;
671 len = strlen (link_sec->name) + sizeof (STUB_SUFFIX);
672 s_name = bfd_alloc (hplink->stub_bfd, len);
673 if (s_name == NULL)
674 return NULL;
676 strcpy (s_name, link_sec->name);
677 strcpy (s_name + len - sizeof (STUB_SUFFIX), STUB_SUFFIX);
678 stub_sec = (*hplink->add_stub_section) (s_name, link_sec);
679 if (stub_sec == NULL)
680 return NULL;
681 hplink->stub_group[link_sec->id].stub_sec = stub_sec;
683 hplink->stub_group[section->id].stub_sec = stub_sec;
686 /* Enter this entry into the linker stub hash table. */
687 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table, stub_name,
688 true, false);
689 if (stub_entry == NULL)
691 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
692 bfd_get_filename (section->owner),
693 stub_name);
694 return NULL;
697 stub_entry->stub_sec = stub_sec;
698 #if ! LONG_BRANCH_PIC_IN_SHLIB
699 stub_entry->reloc_sec = hplink->stub_group[section->id].reloc_sec;
700 #endif
701 stub_entry->stub_offset = 0;
702 stub_entry->id_sec = link_sec;
703 return stub_entry;
706 /* Determine the type of stub needed, if any, for a call. */
708 static enum elf32_hppa_stub_type
709 hppa_type_of_stub (input_sec, rel, hash, destination)
710 asection *input_sec;
711 const Elf_Internal_Rela *rel;
712 struct elf32_hppa_link_hash_entry *hash;
713 bfd_vma destination;
715 bfd_vma location;
716 bfd_vma branch_offset;
717 bfd_vma max_branch_offset;
718 unsigned int r_type;
720 if (hash != NULL
721 && (((hash->elf.root.type == bfd_link_hash_defined
722 || hash->elf.root.type == bfd_link_hash_defweak)
723 && hash->elf.root.u.def.section->output_section == NULL)
724 || (hash->elf.root.type == bfd_link_hash_defweak
725 && hash->elf.dynindx != -1
726 && hash->elf.plt.offset != (bfd_vma) -1)
727 || hash->elf.root.type == bfd_link_hash_undefweak
728 || hash->elf.root.type == bfd_link_hash_undefined
729 || (hash->maybe_pic_call && !(input_sec->flags & SEC_HAS_GOT_REF))))
731 /* If output_section is NULL, then it's a symbol defined in a
732 shared library. We will need an import stub. Decide between
733 hppa_stub_import and hppa_stub_import_shared later. For
734 shared links we need stubs for undefined or weak syms too;
735 They will presumably be resolved by the dynamic linker. */
736 return hppa_stub_import;
739 /* Determine where the call point is. */
740 location = (input_sec->output_offset
741 + input_sec->output_section->vma
742 + rel->r_offset);
744 branch_offset = destination - location - 8;
745 r_type = ELF32_R_TYPE (rel->r_info);
747 /* Determine if a long branch stub is needed. parisc branch offsets
748 are relative to the second instruction past the branch, ie. +8
749 bytes on from the branch instruction location. The offset is
750 signed and counts in units of 4 bytes. */
751 if (r_type == (unsigned int) R_PARISC_PCREL17F)
753 max_branch_offset = (1 << (17-1)) << 2;
755 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
757 max_branch_offset = (1 << (12-1)) << 2;
759 else /* R_PARISC_PCREL22F. */
761 max_branch_offset = (1 << (22-1)) << 2;
764 if (branch_offset + max_branch_offset >= 2*max_branch_offset)
766 #if LONG_BRANCH_VIA_PLT
767 if (hash != NULL
768 && hash->elf.dynindx != -1
769 && hash->elf.plt.offset != (bfd_vma) -1)
771 /* If we are doing a shared link and find we need a long
772 branch stub, then go via the .plt if possible. */
773 return hppa_stub_import;
775 else
776 #endif
777 return hppa_stub_long_branch;
779 return hppa_stub_none;
782 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
783 IN_ARG contains the link info pointer. */
785 #define LDIL_R1 0x20200000 /* ldil LR'XXX,%r1 */
786 #define BE_SR4_R1 0xe0202002 /* be,n RR'XXX(%sr4,%r1) */
788 #define BL_R1 0xe8200000 /* b,l .+8,%r1 */
789 #define ADDIL_R1 0x28200000 /* addil LR'XXX,%r1,%r1 */
790 #define DEPI_R1 0xd4201c1e /* depi 0,31,2,%r1 */
792 #define ADDIL_DP 0x2b600000 /* addil LR'XXX,%dp,%r1 */
793 #define LDW_R1_R21 0x48350000 /* ldw RR'XXX(%sr0,%r1),%r21 */
794 #define BV_R0_R21 0xeaa0c000 /* bv %r0(%r21) */
795 #define LDW_R1_R19 0x48330000 /* ldw RR'XXX(%sr0,%r1),%r19 */
797 #define ADDIL_R19 0x2a600000 /* addil LR'XXX,%r19,%r1 */
798 #define LDW_R1_DP 0x483b0000 /* ldw RR'XXX(%sr0,%r1),%dp */
800 #define LDSID_R21_R1 0x02a010a1 /* ldsid (%sr0,%r21),%r1 */
801 #define MTSP_R1 0x00011820 /* mtsp %r1,%sr0 */
802 #define BE_SR0_R21 0xe2a00000 /* be 0(%sr0,%r21) */
803 #define STW_RP 0x6bc23fd1 /* stw %rp,-24(%sr0,%sp) */
805 #define BL_RP 0xe8400002 /* b,l,n XXX,%rp */
806 #define NOP 0x08000240 /* nop */
807 #define LDW_RP 0x4bc23fd1 /* ldw -24(%sr0,%sp),%rp */
808 #define LDSID_RP_R1 0x004010a1 /* ldsid (%sr0,%rp),%r1 */
809 #define BE_SR0_RP 0xe0400002 /* be,n 0(%sr0,%rp) */
811 #ifndef R19_STUBS
812 #define R19_STUBS 1
813 #endif
815 #if R19_STUBS
816 #define LDW_R1_DLT LDW_R1_R19
817 #else
818 #define LDW_R1_DLT LDW_R1_DP
819 #endif
821 static boolean
822 hppa_build_one_stub (gen_entry, in_arg)
823 struct bfd_hash_entry *gen_entry;
824 PTR in_arg;
826 struct elf32_hppa_stub_hash_entry *stub_entry;
827 struct bfd_link_info *info;
828 struct elf32_hppa_link_hash_table *hplink;
829 asection *stub_sec;
830 bfd *stub_bfd;
831 bfd_byte *loc;
832 bfd_vma sym_value;
833 bfd_vma insn;
834 int val;
835 int size;
837 /* Massage our args to the form they really have. */
838 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
839 info = (struct bfd_link_info *) in_arg;
841 hplink = hppa_link_hash_table (info);
842 stub_sec = stub_entry->stub_sec;
844 /* Make a note of the offset within the stubs for this entry. */
845 stub_entry->stub_offset = stub_sec->_raw_size;
846 loc = stub_sec->contents + stub_entry->stub_offset;
848 stub_bfd = stub_sec->owner;
850 switch (stub_entry->stub_type)
852 case hppa_stub_long_branch:
853 /* Create the long branch. A long branch is formed with "ldil"
854 loading the upper bits of the target address into a register,
855 then branching with "be" which adds in the lower bits.
856 The "be" has its delay slot nullified. */
857 sym_value = (stub_entry->target_value
858 + stub_entry->target_section->output_offset
859 + stub_entry->target_section->output_section->vma);
861 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_lrsel);
862 insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
863 bfd_put_32 (stub_bfd, insn, loc);
865 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_rrsel) >> 2;
866 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
867 bfd_put_32 (stub_bfd, insn, loc + 4);
869 #if ! LONG_BRANCH_PIC_IN_SHLIB
870 if (info->shared)
872 /* Output a dynamic relocation for this stub. We only
873 output one PCREL21L reloc per stub, trusting that the
874 dynamic linker will also fix the implied PCREL17R for the
875 second instruction. PCREL21L dynamic relocs had better
876 never be emitted for some other purpose... */
877 asection *srel;
878 Elf_Internal_Rela outrel;
880 if (stub_entry->h == NULL)
882 (*_bfd_error_handler)
883 (_("%s(%s+0x%lx): cannot relocate %s, recompile with -ffunction-sections"),
884 bfd_get_filename (stub_entry->target_section->owner),
885 stub_sec->name,
886 (long) stub_entry->stub_offset,
887 stub_entry->root.string);
888 bfd_set_error (bfd_error_bad_value);
889 return false;
892 srel = stub_entry->reloc_sec;
893 if (srel == NULL)
895 (*_bfd_error_handler)
896 (_("Could not find relocation section for %s"),
897 stub_sec->name);
898 bfd_set_error (bfd_error_bad_value);
899 return false;
902 outrel.r_offset = (stub_entry->stub_offset
903 + stub_sec->output_offset
904 + stub_sec->output_section->vma);
905 outrel.r_info = ELF32_R_INFO (0, R_PARISC_PCREL21L);
906 outrel.r_addend = sym_value;
907 bfd_elf32_swap_reloca_out (stub_sec->output_section->owner,
908 &outrel,
909 ((Elf32_External_Rela *)
910 srel->contents + srel->reloc_count));
911 ++srel->reloc_count;
913 #endif
914 size = 8;
915 break;
917 case hppa_stub_long_branch_shared:
918 /* Branches are relative. This is where we are going to. */
919 sym_value = (stub_entry->target_value
920 + stub_entry->target_section->output_offset
921 + stub_entry->target_section->output_section->vma);
923 /* And this is where we are coming from, more or less. */
924 sym_value -= (stub_entry->stub_offset
925 + stub_sec->output_offset
926 + stub_sec->output_section->vma);
928 bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
929 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
930 insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
931 bfd_put_32 (stub_bfd, insn, loc + 4);
933 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
934 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
935 bfd_put_32 (stub_bfd, insn, loc + 8);
936 size = 12;
937 break;
939 case hppa_stub_import:
940 case hppa_stub_import_shared:
941 sym_value = (stub_entry->h->elf.plt.offset
942 + hplink->splt->output_offset
943 + hplink->splt->output_section->vma
944 - elf_gp (hplink->splt->output_section->owner));
946 insn = ADDIL_DP;
947 #if R19_STUBS
948 if (stub_entry->stub_type == hppa_stub_import_shared)
949 insn = ADDIL_R19;
950 #endif
951 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_lrsel),
952 insn = hppa_rebuild_insn ((int) insn, val, 21);
953 bfd_put_32 (stub_bfd, insn, loc);
955 /* It is critical to use lrsel/rrsel here because we are using
956 two different offsets (+0 and +4) from sym_value. If we use
957 lsel/rsel then with unfortunate sym_values we will round
958 sym_value+4 up to the next 2k block leading to a mis-match
959 between the lsel and rsel value. */
960 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_rrsel);
961 insn = hppa_rebuild_insn ((int) LDW_R1_R21, val, 14);
962 bfd_put_32 (stub_bfd, insn, loc + 4);
964 if (hplink->multi_subspace)
966 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
967 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
968 bfd_put_32 (stub_bfd, insn, loc + 8);
970 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
971 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
972 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21, loc + 20);
973 bfd_put_32 (stub_bfd, (bfd_vma) STW_RP, loc + 24);
975 size = 28;
977 else
979 bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 8);
980 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
981 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
982 bfd_put_32 (stub_bfd, insn, loc + 12);
984 size = 16;
987 if (!info->shared
988 && stub_entry->h != NULL
989 && stub_entry->h->pic_call)
991 /* Build the .plt entry needed to call a PIC function from
992 statically linked code. We don't need any relocs. */
993 bfd *dynobj;
994 struct elf32_hppa_link_hash_entry *eh;
995 bfd_vma value;
997 dynobj = hplink->root.dynobj;
998 eh = (struct elf32_hppa_link_hash_entry *) stub_entry->h;
1000 BFD_ASSERT (eh->elf.root.type == bfd_link_hash_defined
1001 || eh->elf.root.type == bfd_link_hash_defweak);
1003 value = (eh->elf.root.u.def.value
1004 + eh->elf.root.u.def.section->output_offset
1005 + eh->elf.root.u.def.section->output_section->vma);
1007 /* Fill in the entry in the procedure linkage table.
1009 The format of a plt entry is
1010 <funcaddr>
1011 <__gp>. */
1013 bfd_put_32 (hplink->splt->owner, value,
1014 hplink->splt->contents + eh->elf.plt.offset);
1015 value = elf_gp (hplink->splt->output_section->owner);
1016 bfd_put_32 (hplink->splt->owner, value,
1017 hplink->splt->contents + eh->elf.plt.offset + 4);
1019 break;
1021 case hppa_stub_export:
1022 /* Branches are relative. This is where we are going to. */
1023 sym_value = (stub_entry->target_value
1024 + stub_entry->target_section->output_offset
1025 + stub_entry->target_section->output_section->vma);
1027 /* And this is where we are coming from. */
1028 sym_value -= (stub_entry->stub_offset
1029 + stub_sec->output_offset
1030 + stub_sec->output_section->vma);
1032 if (sym_value - 8 + 0x40000 >= 0x80000)
1034 (*_bfd_error_handler)
1035 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
1036 bfd_get_filename (stub_entry->target_section->owner),
1037 stub_sec->name,
1038 (long) stub_entry->stub_offset,
1039 stub_entry->root.string);
1040 bfd_set_error (bfd_error_bad_value);
1041 return false;
1044 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
1045 insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
1046 bfd_put_32 (stub_bfd, insn, loc);
1048 bfd_put_32 (stub_bfd, (bfd_vma) NOP, loc + 4);
1049 bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP, loc + 8);
1050 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
1051 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
1052 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP, loc + 20);
1054 /* Point the function symbol at the stub. */
1055 stub_entry->h->elf.root.u.def.section = stub_sec;
1056 stub_entry->h->elf.root.u.def.value = stub_sec->_raw_size;
1058 size = 24;
1059 break;
1061 default:
1062 BFD_FAIL ();
1063 return false;
1066 stub_sec->_raw_size += size;
1067 return true;
1070 #undef LDIL_R1
1071 #undef BE_SR4_R1
1072 #undef BL_R1
1073 #undef ADDIL_R1
1074 #undef DEPI_R1
1075 #undef ADDIL_DP
1076 #undef LDW_R1_R21
1077 #undef LDW_R1_DLT
1078 #undef LDW_R1_R19
1079 #undef ADDIL_R19
1080 #undef LDW_R1_DP
1081 #undef LDSID_R21_R1
1082 #undef MTSP_R1
1083 #undef BE_SR0_R21
1084 #undef STW_RP
1085 #undef BV_R0_R21
1086 #undef BL_RP
1087 #undef NOP
1088 #undef LDW_RP
1089 #undef LDSID_RP_R1
1090 #undef BE_SR0_RP
1092 /* As above, but don't actually build the stub. Just bump offset so
1093 we know stub section sizes. */
1095 static boolean
1096 hppa_size_one_stub (gen_entry, in_arg)
1097 struct bfd_hash_entry *gen_entry;
1098 PTR in_arg;
1100 struct elf32_hppa_stub_hash_entry *stub_entry;
1101 struct elf32_hppa_link_hash_table *hplink;
1102 int size;
1104 /* Massage our args to the form they really have. */
1105 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
1106 hplink = (struct elf32_hppa_link_hash_table *) in_arg;
1108 if (stub_entry->stub_type == hppa_stub_long_branch)
1110 #if ! LONG_BRANCH_PIC_IN_SHLIB
1111 if (stub_entry->reloc_sec != NULL)
1112 stub_entry->reloc_sec->_raw_size += sizeof (Elf32_External_Rela);
1113 #endif
1114 size = 8;
1116 else if (stub_entry->stub_type == hppa_stub_long_branch_shared)
1117 size = 12;
1118 else if (stub_entry->stub_type == hppa_stub_export)
1119 size = 24;
1120 else /* hppa_stub_import or hppa_stub_import_shared. */
1122 if (hplink->multi_subspace)
1123 size = 28;
1124 else
1125 size = 16;
1128 stub_entry->stub_sec->_raw_size += size;
1129 return true;
1132 /* Return nonzero if ABFD represents an HPPA ELF32 file.
1133 Additionally we set the default architecture and machine. */
1135 static boolean
1136 elf32_hppa_object_p (abfd)
1137 bfd *abfd;
1139 unsigned int flags = elf_elfheader (abfd)->e_flags;
1141 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
1143 case EFA_PARISC_1_0:
1144 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
1145 case EFA_PARISC_1_1:
1146 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
1147 case EFA_PARISC_2_0:
1148 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
1149 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
1150 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
1152 return true;
1155 /* Undo the generic ELF code's subtraction of section->vma from the
1156 value of each external symbol. */
1158 static boolean
1159 elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1160 bfd *abfd ATTRIBUTE_UNUSED;
1161 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1162 const Elf_Internal_Sym *sym ATTRIBUTE_UNUSED;
1163 const char **namep ATTRIBUTE_UNUSED;
1164 flagword *flagsp ATTRIBUTE_UNUSED;
1165 asection **secp;
1166 bfd_vma *valp;
1168 *valp += (*secp)->vma;
1169 return true;
1172 /* Create the .plt and .got sections, and set up our hash table
1173 short-cuts to various dynamic sections. */
1175 static boolean
1176 elf32_hppa_create_dynamic_sections (abfd, info)
1177 bfd *abfd;
1178 struct bfd_link_info *info;
1180 struct elf32_hppa_link_hash_table *hplink;
1182 /* Don't try to create the .plt and .got twice. */
1183 hplink = hppa_link_hash_table (info);
1184 if (hplink->splt != NULL)
1185 return true;
1187 /* Call the generic code to do most of the work. */
1188 if (! _bfd_elf_create_dynamic_sections (abfd, info))
1189 return false;
1191 hplink->splt = bfd_get_section_by_name (abfd, ".plt");
1192 hplink->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
1194 hplink->sgot = bfd_get_section_by_name (abfd, ".got");
1195 hplink->srelgot = bfd_make_section (abfd, ".rela.got");
1196 if (hplink->srelgot == NULL
1197 || ! bfd_set_section_flags (abfd, hplink->srelgot,
1198 (SEC_ALLOC
1199 | SEC_LOAD
1200 | SEC_HAS_CONTENTS
1201 | SEC_IN_MEMORY
1202 | SEC_LINKER_CREATED
1203 | SEC_READONLY))
1204 || ! bfd_set_section_alignment (abfd, hplink->srelgot, 2))
1205 return false;
1207 hplink->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
1208 hplink->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
1210 return true;
1213 /* Look through the relocs for a section during the first phase, and
1214 allocate space in the global offset table or procedure linkage
1215 table. At this point we haven't necessarily read all the input
1216 files. */
1218 static boolean
1219 elf32_hppa_check_relocs (abfd, info, sec, relocs)
1220 bfd *abfd;
1221 struct bfd_link_info *info;
1222 asection *sec;
1223 const Elf_Internal_Rela *relocs;
1225 bfd *dynobj;
1226 Elf_Internal_Shdr *symtab_hdr;
1227 struct elf_link_hash_entry **sym_hashes;
1228 bfd_signed_vma *local_got_refcounts;
1229 const Elf_Internal_Rela *rel;
1230 const Elf_Internal_Rela *rel_end;
1231 struct elf32_hppa_link_hash_table *hplink;
1232 asection *sreloc;
1233 asection *stubreloc;
1235 if (info->relocateable)
1236 return true;
1238 hplink = hppa_link_hash_table (info);
1239 dynobj = hplink->root.dynobj;
1240 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1241 sym_hashes = elf_sym_hashes (abfd);
1242 local_got_refcounts = elf_local_got_refcounts (abfd);
1243 sreloc = NULL;
1244 stubreloc = NULL;
1246 rel_end = relocs + sec->reloc_count;
1247 for (rel = relocs; rel < rel_end; rel++)
1249 enum {
1250 NEED_GOT = 1,
1251 NEED_PLT = 2,
1252 NEED_DYNREL = 4,
1253 #if LONG_BRANCH_PIC_IN_SHLIB
1254 NEED_STUBREL = 0, /* We won't be needing them in this case. */
1255 #else
1256 NEED_STUBREL = 8,
1257 #endif
1258 PLT_PLABEL = 16
1261 unsigned int r_symndx, r_type;
1262 struct elf32_hppa_link_hash_entry *h;
1263 int need_entry;
1265 r_symndx = ELF32_R_SYM (rel->r_info);
1267 if (r_symndx < symtab_hdr->sh_info)
1268 h = NULL;
1269 else
1270 h = ((struct elf32_hppa_link_hash_entry *)
1271 sym_hashes[r_symndx - symtab_hdr->sh_info]);
1273 r_type = ELF32_R_TYPE (rel->r_info);
1275 switch (r_type)
1277 case R_PARISC_DLTIND14F:
1278 case R_PARISC_DLTIND14R:
1279 case R_PARISC_DLTIND21L:
1280 /* This symbol requires a global offset table entry. */
1281 need_entry = NEED_GOT;
1283 /* Mark this section as containing PIC code. */
1284 sec->flags |= SEC_HAS_GOT_REF;
1285 break;
1287 case R_PARISC_PLABEL14R: /* "Official" procedure labels. */
1288 case R_PARISC_PLABEL21L:
1289 case R_PARISC_PLABEL32:
1290 /* If the addend is non-zero, we break badly. */
1291 BFD_ASSERT (rel->r_addend == 0);
1293 /* If we are creating a shared library, then we need to
1294 create a PLT entry for all PLABELs, because PLABELs with
1295 local symbols may be passed via a pointer to another
1296 object. Additionally, output a dynamic relocation
1297 pointing to the PLT entry. */
1298 need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
1299 break;
1301 case R_PARISC_PCREL12F:
1302 hplink->has_12bit_branch = 1;
1303 /* Fall thru. */
1304 case R_PARISC_PCREL17C:
1305 case R_PARISC_PCREL17F:
1306 hplink->has_17bit_branch = 1;
1307 /* Fall thru. */
1308 case R_PARISC_PCREL22F:
1309 /* Function calls might need to go through the .plt, and
1310 might require long branch stubs. */
1311 if (h == NULL)
1313 /* We know local syms won't need a .plt entry, and if
1314 they need a long branch stub we can't guarantee that
1315 we can reach the stub. So just flag an error later
1316 if we're doing a shared link and find we need a long
1317 branch stub. */
1318 continue;
1320 else
1322 /* Global symbols will need a .plt entry if they remain
1323 global, and in most cases won't need a long branch
1324 stub. Unfortunately, we have to cater for the case
1325 where a symbol is forced local by versioning, or due
1326 to symbolic linking, and we lose the .plt entry. */
1327 need_entry = NEED_PLT | NEED_STUBREL;
1329 break;
1331 case R_PARISC_SEGBASE: /* Used to set segment base. */
1332 case R_PARISC_SEGREL32: /* Relative reloc, used for unwind. */
1333 case R_PARISC_PCREL14F: /* PC relative load/store. */
1334 case R_PARISC_PCREL14R:
1335 case R_PARISC_PCREL17R: /* External branches. */
1336 case R_PARISC_PCREL21L: /* As above, and for load/store too. */
1337 /* We don't need to propagate the relocation if linking a
1338 shared object since these are section relative. */
1339 continue;
1341 case R_PARISC_DPREL14F: /* Used for gp rel data load/store. */
1342 case R_PARISC_DPREL14R:
1343 case R_PARISC_DPREL21L:
1344 if (info->shared)
1346 (*_bfd_error_handler)
1347 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1348 bfd_get_filename (abfd),
1349 elf_hppa_howto_table[r_type].name);
1350 bfd_set_error (bfd_error_bad_value);
1351 return false;
1353 /* Fall through. */
1355 case R_PARISC_DIR17F: /* Used for external branches. */
1356 case R_PARISC_DIR17R:
1357 case R_PARISC_DIR14F: /* Used for load/store from absolute locn. */
1358 case R_PARISC_DIR14R:
1359 case R_PARISC_DIR21L: /* As above, and for ext branches too. */
1360 #if 1
1361 /* Help debug shared library creation. Any of the above
1362 relocs can be used in shared libs, but they may cause
1363 pages to become unshared. */
1364 if (info->shared)
1366 (*_bfd_error_handler)
1367 (_("%s: relocation %s should not be used when making a shared object; recompile with -fPIC"),
1368 bfd_get_filename (abfd),
1369 elf_hppa_howto_table[r_type].name);
1371 /* Fall through. */
1372 #endif
1374 case R_PARISC_DIR32: /* .word relocs. */
1375 /* We may want to output a dynamic relocation later. */
1376 need_entry = NEED_DYNREL;
1377 break;
1379 /* This relocation describes the C++ object vtable hierarchy.
1380 Reconstruct it for later use during GC. */
1381 case R_PARISC_GNU_VTINHERIT:
1382 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec,
1383 &h->elf, rel->r_offset))
1384 return false;
1385 continue;
1387 /* This relocation describes which C++ vtable entries are actually
1388 used. Record for later use during GC. */
1389 case R_PARISC_GNU_VTENTRY:
1390 if (!_bfd_elf32_gc_record_vtentry (abfd, sec,
1391 &h->elf, rel->r_addend))
1392 return false;
1393 continue;
1395 default:
1396 continue;
1399 /* Now carry out our orders. */
1400 if (need_entry & NEED_GOT)
1402 /* Allocate space for a GOT entry, as well as a dynamic
1403 relocation for this entry. */
1404 if (dynobj == NULL)
1405 hplink->root.dynobj = dynobj = abfd;
1407 if (hplink->sgot == NULL)
1409 if (! elf32_hppa_create_dynamic_sections (dynobj, info))
1410 return false;
1413 if (h != NULL)
1415 if (h->elf.got.refcount == -1)
1417 h->elf.got.refcount = 1;
1419 /* Make sure this symbol is output as a dynamic symbol. */
1420 if (h->elf.dynindx == -1)
1422 if (! bfd_elf32_link_record_dynamic_symbol (info,
1423 &h->elf))
1424 return false;
1427 hplink->sgot->_raw_size += GOT_ENTRY_SIZE;
1428 hplink->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1430 else
1431 h->elf.got.refcount += 1;
1433 else
1435 /* This is a global offset table entry for a local symbol. */
1436 if (local_got_refcounts == NULL)
1438 size_t size;
1440 /* Allocate space for local got offsets and local
1441 plt offsets. Done this way to save polluting
1442 elf_obj_tdata with another target specific
1443 pointer. */
1444 size = symtab_hdr->sh_info * 2 * sizeof (bfd_signed_vma);
1445 local_got_refcounts = ((bfd_signed_vma *)
1446 bfd_alloc (abfd, size));
1447 if (local_got_refcounts == NULL)
1448 return false;
1449 elf_local_got_refcounts (abfd) = local_got_refcounts;
1450 memset (local_got_refcounts, -1, size);
1452 if (local_got_refcounts[r_symndx] == -1)
1454 local_got_refcounts[r_symndx] = 1;
1456 hplink->sgot->_raw_size += GOT_ENTRY_SIZE;
1457 if (info->shared)
1459 /* If we are generating a shared object, we need to
1460 output a reloc so that the dynamic linker can
1461 adjust this GOT entry (because the address
1462 the shared library is loaded at is not fixed). */
1463 hplink->srelgot->_raw_size +=
1464 sizeof (Elf32_External_Rela);
1467 else
1468 local_got_refcounts[r_symndx] += 1;
1472 if (need_entry & NEED_PLT)
1474 /* If we are creating a shared library, and this is a reloc
1475 against a weak symbol or a global symbol in a dynamic
1476 object, then we will be creating an import stub and a
1477 .plt entry for the symbol. Similarly, on a normal link
1478 to symbols defined in a dynamic object we'll need the
1479 import stub and a .plt entry. We don't know yet whether
1480 the symbol is defined or not, so make an entry anyway and
1481 clean up later in adjust_dynamic_symbol. */
1482 if ((sec->flags & SEC_ALLOC) != 0)
1484 if (h != NULL)
1486 if (h->elf.plt.refcount == -1)
1488 h->elf.plt.refcount = 1;
1489 h->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1491 else
1492 h->elf.plt.refcount += 1;
1494 /* If this .plt entry is for a plabel, mark it so
1495 that adjust_dynamic_symbol will keep the entry
1496 even if it appears to be local. */
1497 if (need_entry & PLT_PLABEL)
1498 h->plabel = 1;
1500 else if (need_entry & PLT_PLABEL)
1502 int indx;
1504 if (local_got_refcounts == NULL)
1506 size_t size;
1508 /* Allocate space for local got offsets and local
1509 plt offsets. */
1510 size = symtab_hdr->sh_info * 2 * sizeof (bfd_signed_vma);
1511 local_got_refcounts = ((bfd_signed_vma *)
1512 bfd_alloc (abfd, size));
1513 if (local_got_refcounts == NULL)
1514 return false;
1515 elf_local_got_refcounts (abfd) = local_got_refcounts;
1516 memset (local_got_refcounts, -1, size);
1518 indx = r_symndx + symtab_hdr->sh_info;
1519 if (local_got_refcounts[indx] == -1)
1520 local_got_refcounts[indx] = 1;
1521 else
1522 local_got_refcounts[indx] += 1;
1527 if (need_entry & (NEED_DYNREL | NEED_STUBREL))
1529 /* Flag this symbol as having a non-got, non-plt reference
1530 so that we generate copy relocs if it turns out to be
1531 dynamic. */
1532 if (h != NULL)
1533 h->elf.elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1535 /* If we are creating a shared library then we need to copy
1536 the reloc into the shared library. However, if we are
1537 linking with -Bsymbolic, we need only copy absolute
1538 relocs or relocs against symbols that are not defined in
1539 an object we are including in the link. PC- or DP- or
1540 DLT-relative relocs against any local sym or global sym
1541 with DEF_REGULAR set, can be discarded. At this point we
1542 have not seen all the input files, so it is possible that
1543 DEF_REGULAR is not set now but will be set later (it is
1544 never cleared). We account for that possibility below by
1545 storing information in the reloc_entries field of the
1546 hash table entry.
1548 A similar situation to the -Bsymbolic case occurs when
1549 creating shared libraries and symbol visibility changes
1550 render the symbol local.
1552 As it turns out, all the relocs we will be creating here
1553 are absolute, so we cannot remove them on -Bsymbolic
1554 links or visibility changes anyway. A STUB_REL reloc
1555 is absolute too, as in that case it is the reloc in the
1556 stub we will be creating, rather than copying the PCREL
1557 reloc in the branch. */
1558 if ((sec->flags & SEC_ALLOC) != 0
1559 && info->shared
1560 #if RELATIVE_DYNAMIC_RELOCS
1561 && (!info->symbolic
1562 || is_absolute_reloc (r_type)
1563 || (h != NULL
1564 && ((h->elf.elf_link_hash_flags
1565 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1566 #endif
1569 boolean doit;
1570 asection *srel;
1572 srel = sreloc;
1573 if ((need_entry & NEED_STUBREL))
1574 srel = stubreloc;
1576 /* Create a reloc section in dynobj and make room for
1577 this reloc. */
1578 if (srel == NULL)
1580 char *name;
1582 if (dynobj == NULL)
1583 hplink->root.dynobj = dynobj = abfd;
1585 name = bfd_elf_string_from_elf_section
1586 (abfd,
1587 elf_elfheader (abfd)->e_shstrndx,
1588 elf_section_data (sec)->rel_hdr.sh_name);
1589 if (name == NULL)
1591 (*_bfd_error_handler)
1592 (_("Could not find relocation section for %s"),
1593 sec->name);
1594 bfd_set_error (bfd_error_bad_value);
1595 return false;
1598 if ((need_entry & NEED_STUBREL))
1600 size_t len = strlen (name) + sizeof (STUB_SUFFIX);
1601 char *newname = bfd_malloc (len);
1603 if (newname == NULL)
1604 return false;
1605 strcpy (newname, name);
1606 strcpy (newname + len - sizeof (STUB_SUFFIX),
1607 STUB_SUFFIX);
1608 name = newname;
1611 srel = bfd_get_section_by_name (dynobj, name);
1612 if (srel == NULL)
1614 flagword flags;
1616 srel = bfd_make_section (dynobj, name);
1617 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1618 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1619 if ((sec->flags & SEC_ALLOC) != 0)
1620 flags |= SEC_ALLOC | SEC_LOAD;
1621 if (srel == NULL
1622 || !bfd_set_section_flags (dynobj, srel, flags)
1623 || !bfd_set_section_alignment (dynobj, srel, 2))
1624 return false;
1626 else if ((need_entry & NEED_STUBREL))
1627 free (name);
1629 if ((need_entry & NEED_STUBREL))
1630 stubreloc = srel;
1631 else
1632 sreloc = srel;
1635 #if ! LONG_BRANCH_PIC_IN_SHLIB
1636 /* If this is a function call, we only need one dynamic
1637 reloc for the stub as all calls to a particular
1638 function will go through the same stub. Actually, a
1639 long branch stub needs two relocations, but we count
1640 on some intelligence on the part of the dynamic
1641 linker. */
1642 if ((need_entry & NEED_STUBREL))
1644 doit = h->stub_reloc_sec != stubreloc;
1645 h->stub_reloc_sec = stubreloc;
1647 else
1648 #endif
1649 doit = 1;
1651 if (doit)
1653 srel->_raw_size += sizeof (Elf32_External_Rela);
1655 #if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
1656 /* Keep track of relocations we have entered for
1657 this global symbol, so that we can discard them
1658 later if necessary. */
1659 if (h != NULL
1660 && (0
1661 #if RELATIVE_DYNAMIC_RELOCS
1662 || ! is_absolute_reloc (rtype)
1663 #endif
1664 || (need_entry & NEED_STUBREL)))
1666 struct elf32_hppa_dyn_reloc_entry *p;
1668 for (p = h->reloc_entries; p != NULL; p = p->next)
1669 if (p->section == srel)
1670 break;
1672 if (p == NULL)
1674 p = ((struct elf32_hppa_dyn_reloc_entry *)
1675 bfd_alloc (dynobj, sizeof *p));
1676 if (p == NULL)
1677 return false;
1678 p->next = h->reloc_entries;
1679 h->reloc_entries = p;
1680 p->section = srel;
1681 p->count = 0;
1684 /* NEED_STUBREL and NEED_DYNREL are never both
1685 set. Leave the count at zero for the
1686 NEED_STUBREL case as we only ever have one
1687 stub reloc per section per symbol, and this
1688 simplifies code in hppa_discard_copies. */
1689 if (! (need_entry & NEED_STUBREL))
1690 ++p->count;
1692 #endif
1698 return true;
1701 /* Return the section that should be marked against garbage collection
1702 for a given relocation. */
1704 static asection *
1705 elf32_hppa_gc_mark_hook (abfd, info, rel, h, sym)
1706 bfd *abfd;
1707 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1708 Elf_Internal_Rela *rel;
1709 struct elf_link_hash_entry *h;
1710 Elf_Internal_Sym *sym;
1712 if (h != NULL)
1714 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1716 case R_PARISC_GNU_VTINHERIT:
1717 case R_PARISC_GNU_VTENTRY:
1718 break;
1720 default:
1721 switch (h->root.type)
1723 case bfd_link_hash_defined:
1724 case bfd_link_hash_defweak:
1725 return h->root.u.def.section;
1727 case bfd_link_hash_common:
1728 return h->root.u.c.p->section;
1730 default:
1731 break;
1735 else
1737 if (!(elf_bad_symtab (abfd)
1738 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1739 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1740 && sym->st_shndx != SHN_COMMON))
1742 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1746 return NULL;
1749 /* Update the got and plt entry reference counts for the section being
1750 removed. */
1752 static boolean
1753 elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
1754 bfd *abfd;
1755 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1756 asection *sec;
1757 const Elf_Internal_Rela *relocs;
1759 Elf_Internal_Shdr *symtab_hdr;
1760 struct elf_link_hash_entry **sym_hashes;
1761 bfd_signed_vma *local_got_refcounts;
1762 bfd_signed_vma *local_plt_refcounts;
1763 const Elf_Internal_Rela *rel, *relend;
1764 unsigned long r_symndx;
1765 struct elf_link_hash_entry *h;
1766 struct elf32_hppa_link_hash_table *hplink;
1767 bfd *dynobj;
1768 asection *sgot;
1769 asection *srelgot;
1771 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1772 sym_hashes = elf_sym_hashes (abfd);
1773 local_got_refcounts = elf_local_got_refcounts (abfd);
1774 local_plt_refcounts = local_got_refcounts;
1775 if (local_plt_refcounts != NULL)
1776 local_plt_refcounts += symtab_hdr->sh_info;
1777 hplink = hppa_link_hash_table (info);
1778 dynobj = hplink->root.dynobj;
1779 if (dynobj == NULL)
1780 return true;
1782 sgot = hplink->sgot;
1783 srelgot = hplink->srelgot;
1785 relend = relocs + sec->reloc_count;
1786 for (rel = relocs; rel < relend; rel++)
1787 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1789 case R_PARISC_DLTIND14F:
1790 case R_PARISC_DLTIND14R:
1791 case R_PARISC_DLTIND21L:
1792 r_symndx = ELF32_R_SYM (rel->r_info);
1793 if (r_symndx >= symtab_hdr->sh_info)
1795 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1796 if (h->got.refcount > 0)
1798 h->got.refcount -= 1;
1799 if (h->got.refcount == 0)
1801 sgot->_raw_size -= GOT_ENTRY_SIZE;
1802 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1806 else if (local_got_refcounts != NULL)
1808 if (local_got_refcounts[r_symndx] > 0)
1810 local_got_refcounts[r_symndx] -= 1;
1811 if (local_got_refcounts[r_symndx] == 0)
1813 sgot->_raw_size -= GOT_ENTRY_SIZE;
1814 if (info->shared)
1815 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1819 break;
1821 case R_PARISC_PCREL12F:
1822 case R_PARISC_PCREL17C:
1823 case R_PARISC_PCREL17F:
1824 case R_PARISC_PCREL22F:
1825 r_symndx = ELF32_R_SYM (rel->r_info);
1826 if (r_symndx >= symtab_hdr->sh_info)
1828 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1829 if (h->plt.refcount > 0)
1830 h->plt.refcount -= 1;
1832 break;
1834 case R_PARISC_PLABEL14R:
1835 case R_PARISC_PLABEL21L:
1836 case R_PARISC_PLABEL32:
1837 r_symndx = ELF32_R_SYM (rel->r_info);
1838 if (r_symndx >= symtab_hdr->sh_info)
1840 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1841 if (h->plt.refcount > 0)
1842 h->plt.refcount -= 1;
1844 else if (local_plt_refcounts != NULL)
1846 if (local_plt_refcounts[r_symndx] > 0)
1847 local_plt_refcounts[r_symndx] -= 1;
1849 break;
1851 default:
1852 break;
1855 return true;
1858 /* Our own version of hide_symbol, so that we can keep plt entries for
1859 plabels. */
1861 static void
1862 elf32_hppa_hide_symbol (info, h)
1863 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1864 struct elf_link_hash_entry *h;
1866 h->dynindx = -1;
1867 if (! ((struct elf32_hppa_link_hash_entry *) h)->plabel)
1869 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1870 h->plt.offset = (bfd_vma) -1;
1874 /* Adjust a symbol defined by a dynamic object and referenced by a
1875 regular object. The current definition is in some section of the
1876 dynamic object, but we're not including those sections. We have to
1877 change the definition to something the rest of the link can
1878 understand. */
1880 static boolean
1881 elf32_hppa_adjust_dynamic_symbol (info, h)
1882 struct bfd_link_info *info;
1883 struct elf_link_hash_entry *h;
1885 bfd *dynobj;
1886 struct elf32_hppa_link_hash_table *hplink;
1887 asection *s;
1889 hplink = hppa_link_hash_table (info);
1890 dynobj = hplink->root.dynobj;
1892 /* If this is a function, put it in the procedure linkage table. We
1893 will fill in the contents of the procedure linkage table later,
1894 when we know the address of the .got section. */
1895 if (h->type == STT_FUNC
1896 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1898 if (!info->shared
1899 && h->plt.refcount > 0
1900 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1901 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0)
1903 ((struct elf32_hppa_link_hash_entry *) h)->maybe_pic_call = 1;
1906 if (h->plt.refcount <= 0
1907 || ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1908 && h->root.type != bfd_link_hash_defweak
1909 && ! ((struct elf32_hppa_link_hash_entry *) h)->plabel
1910 && (!info->shared || info->symbolic)))
1912 /* The .plt entry is not needed when:
1913 a) Garbage collection has removed all references to the
1914 symbol, or
1915 b) We know for certain the symbol is defined in this
1916 object, and it's not a weak definition, nor is the symbol
1917 used by a plabel relocation. Either this object is the
1918 application or we are doing a shared symbolic link. */
1920 /* As a special sop to the hppa ABI, we keep a .plt entry
1921 for functions in sections containing PIC code. */
1922 if (((struct elf32_hppa_link_hash_entry *) h)->maybe_pic_call)
1923 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
1924 else
1926 h->plt.offset = (bfd_vma) -1;
1927 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1928 return true;
1932 /* Make an entry in the .plt section. */
1933 s = hplink->splt;
1934 h->plt.offset = s->_raw_size;
1935 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
1936 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
1937 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1939 /* Add some extra space for the dynamic linker to use. */
1940 s->_raw_size += PLABEL_PLT_ENTRY_SIZE;
1942 else
1943 s->_raw_size += PLT_ENTRY_SIZE;
1945 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
1947 /* Make sure this symbol is output as a dynamic symbol. */
1948 if (h->dynindx == -1
1949 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1951 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1952 return false;
1955 /* We also need to make an entry in the .rela.plt section. */
1956 s = hplink->srelplt;
1957 s->_raw_size += sizeof (Elf32_External_Rela);
1959 hplink->need_plt_stub = 1;
1961 return true;
1964 /* If this is a weak symbol, and there is a real definition, the
1965 processor independent code will have arranged for us to see the
1966 real definition first, and we can just use the same value. */
1967 if (h->weakdef != NULL)
1969 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1970 || h->weakdef->root.type == bfd_link_hash_defweak);
1971 h->root.u.def.section = h->weakdef->root.u.def.section;
1972 h->root.u.def.value = h->weakdef->root.u.def.value;
1973 return true;
1976 /* This is a reference to a symbol defined by a dynamic object which
1977 is not a function. */
1979 /* If we are creating a shared library, we must presume that the
1980 only references to the symbol are via the global offset table.
1981 For such cases we need not do anything here; the relocations will
1982 be handled correctly by relocate_section. */
1983 if (info->shared)
1984 return true;
1986 /* If there are no references to this symbol that do not use the
1987 GOT, we don't need to generate a copy reloc. */
1988 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1989 return true;
1991 /* We must allocate the symbol in our .dynbss section, which will
1992 become part of the .bss section of the executable. There will be
1993 an entry for this symbol in the .dynsym section. The dynamic
1994 object will contain position independent code, so all references
1995 from the dynamic object to this symbol will go through the global
1996 offset table. The dynamic linker will use the .dynsym entry to
1997 determine the address it must put in the global offset table, so
1998 both the dynamic object and the regular object will refer to the
1999 same memory location for the variable. */
2001 s = hplink->sdynbss;
2003 /* We must generate a COPY reloc to tell the dynamic linker to
2004 copy the initial value out of the dynamic object and into the
2005 runtime process image. We need to remember the offset into the
2006 .rela.bss section we are going to use. */
2007 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2009 asection *srel;
2011 srel = hplink->srelbss;
2012 srel->_raw_size += sizeof (Elf32_External_Rela);
2013 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2017 /* We need to figure out the alignment required for this symbol. I
2018 have no idea how other ELF linkers handle this. */
2019 unsigned int power_of_two;
2021 power_of_two = bfd_log2 (h->size);
2022 if (power_of_two > 3)
2023 power_of_two = 3;
2025 /* Apply the required alignment. */
2026 s->_raw_size = BFD_ALIGN (s->_raw_size,
2027 (bfd_size_type) (1 << power_of_two));
2028 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2030 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2031 return false;
2034 /* Define the symbol as being at this point in the section. */
2035 h->root.u.def.section = s;
2036 h->root.u.def.value = s->_raw_size;
2038 /* Increment the section size to make room for the symbol. */
2039 s->_raw_size += h->size;
2041 return true;
2044 /* Called via elf_link_hash_traverse to create .plt entries for an
2045 application that uses statically linked PIC functions. Similar to
2046 the first part of elf32_hppa_adjust_dynamic_symbol. */
2048 static boolean
2049 hppa_handle_PIC_calls (h, inf)
2050 struct elf_link_hash_entry *h;
2051 PTR inf;
2053 struct bfd_link_info *info;
2054 bfd *dynobj;
2055 struct elf32_hppa_link_hash_table *hplink;
2056 asection *s;
2058 if (! (h->plt.refcount > 0
2059 && (h->root.type == bfd_link_hash_defined
2060 || h->root.type == bfd_link_hash_defweak)
2061 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0))
2063 h->plt.offset = (bfd_vma) -1;
2064 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2065 return true;
2068 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2069 ((struct elf32_hppa_link_hash_entry *) h)->maybe_pic_call = 1;
2070 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
2072 info = (struct bfd_link_info *) inf;
2073 hplink = hppa_link_hash_table (info);
2074 dynobj = hplink->root.dynobj;
2076 /* Make an entry in the .plt section. */
2077 s = hplink->splt;
2078 h->plt.offset = s->_raw_size;
2079 s->_raw_size += PLT_ENTRY_SIZE;
2081 return true;
2084 #if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
2085 || RELATIVE_DYNAMIC_RELOCS)
2086 /* This function is called via elf_link_hash_traverse to discard space
2087 we allocated for relocs that it turned out we didn't need. */
2089 static boolean
2090 hppa_discard_copies (h, inf)
2091 struct elf_link_hash_entry *h;
2092 PTR inf;
2094 struct elf32_hppa_dyn_reloc_entry *s;
2095 struct elf32_hppa_link_hash_entry *eh;
2096 struct bfd_link_info *info;
2098 eh = (struct elf32_hppa_link_hash_entry *) h;
2099 info = (struct bfd_link_info *) inf;
2101 #if ! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT
2102 /* Handle the stub reloc case. If we have a plt entry for the
2103 function, we won't be needing long branch stubs. s->count will
2104 only be zero for stub relocs, which provides a handy way of
2105 flagging these relocs, and means we need do nothing special for
2106 the forced local and symbolic link case. */
2107 if (eh->stub_reloc_sec != NULL
2108 && eh->elf.plt.offset != (bfd_vma) -1)
2110 for (s = eh->reloc_entries; s != NULL; s = s->next)
2111 if (s->count == 0)
2112 s->section->_raw_size -= sizeof (Elf32_External_Rela);
2114 #endif
2116 #if RELATIVE_DYNAMIC_RELOCS
2117 /* If a symbol has been forced local or we have found a regular
2118 definition for the symbolic link case, then we won't be needing
2119 any relocs. */
2120 if (eh->elf.dynindx == -1
2121 || ((eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2122 && !is_absolute_reloc (r_type)
2123 && info->symbolic))
2125 for (s = eh->reloc_entries; s != NULL; s = s->next)
2126 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2128 #endif
2130 return true;
2132 #endif
2134 /* This function is called via elf_link_hash_traverse to force
2135 millicode symbols local so they do not end up as globals in the
2136 dynamic symbol table. We ought to be able to do this in
2137 adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2138 for all dynamic symbols. Arguably, this is a bug in
2139 elf_adjust_dynamic_symbol. */
2141 static boolean
2142 clobber_millicode_symbols (h, info)
2143 struct elf_link_hash_entry *h;
2144 struct bfd_link_info *info;
2146 /* Note! We only want to remove these from the dynamic symbol
2147 table. Therefore we do not set ELF_LINK_FORCED_LOCAL. */
2148 if (h->type == STT_PARISC_MILLI)
2149 elf32_hppa_hide_symbol (info, h);
2150 return true;
2153 /* Set the sizes of the dynamic sections. */
2155 static boolean
2156 elf32_hppa_size_dynamic_sections (output_bfd, info)
2157 bfd *output_bfd;
2158 struct bfd_link_info *info;
2160 struct elf32_hppa_link_hash_table *hplink;
2161 bfd *dynobj;
2162 asection *s;
2163 boolean relocs;
2164 boolean reltext;
2166 hplink = hppa_link_hash_table (info);
2167 dynobj = hplink->root.dynobj;
2168 BFD_ASSERT (dynobj != NULL);
2170 if (hplink->root.dynamic_sections_created)
2172 bfd *i;
2174 /* Set the contents of the .interp section to the interpreter. */
2175 if (! info->shared)
2177 s = bfd_get_section_by_name (dynobj, ".interp");
2178 BFD_ASSERT (s != NULL);
2179 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2180 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2183 /* Force millicode symbols local. */
2184 elf_link_hash_traverse (&hplink->root,
2185 clobber_millicode_symbols,
2186 info);
2188 /* Set up .plt offsets for local plabels. */
2189 for (i = info->input_bfds; i; i = i->link_next)
2191 bfd_signed_vma *local_plt;
2192 bfd_signed_vma *end_local_plt;
2193 bfd_size_type locsymcount;
2194 Elf_Internal_Shdr *symtab_hdr;
2196 local_plt = elf_local_got_refcounts (i);
2197 if (!local_plt)
2198 continue;
2200 symtab_hdr = &elf_tdata (i)->symtab_hdr;
2201 locsymcount = symtab_hdr->sh_info;
2202 local_plt += locsymcount;
2203 end_local_plt = local_plt + locsymcount;
2205 for (; local_plt < end_local_plt; ++local_plt)
2207 if (*local_plt > 0)
2209 s = hplink->splt;
2210 *local_plt = s->_raw_size;
2211 s->_raw_size += PLT_ENTRY_SIZE;
2212 if (info->shared)
2213 hplink->srelplt->_raw_size += sizeof (Elf32_External_Rela);
2215 else
2216 *local_plt = (bfd_vma) -1;
2220 else
2222 /* Run through the function symbols, looking for any that are
2223 PIC, and allocate space for the necessary .plt entries so
2224 that %r19 will be set up. */
2225 if (! info->shared)
2226 elf_link_hash_traverse (&hplink->root,
2227 hppa_handle_PIC_calls,
2228 info);
2230 /* We may have created entries in the .rela.got section.
2231 However, if we are not creating the dynamic sections, we will
2232 not actually use these entries. Reset the size of .rela.got,
2233 which will cause it to get stripped from the output file
2234 below. */
2235 hplink->srelgot->_raw_size = 0;
2238 #if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
2239 || RELATIVE_DYNAMIC_RELOCS)
2240 /* If this is a -Bsymbolic shared link, then we need to discard all
2241 relocs against symbols defined in a regular object. We also need
2242 to lose relocs we've allocated for long branch stubs if we know
2243 we won't be generating a stub. */
2244 if (info->shared)
2245 elf_link_hash_traverse (&hplink->root,
2246 hppa_discard_copies,
2247 info);
2248 #endif
2250 /* The check_relocs and adjust_dynamic_symbol entry points have
2251 determined the sizes of the various dynamic sections. Allocate
2252 memory for them. */
2253 relocs = false;
2254 reltext = false;
2255 for (s = dynobj->sections; s != NULL; s = s->next)
2257 const char *name;
2259 if ((s->flags & SEC_LINKER_CREATED) == 0)
2260 continue;
2262 /* It's OK to base decisions on the section name, because none
2263 of the dynobj section names depend upon the input files. */
2264 name = bfd_get_section_name (dynobj, s);
2266 if (strncmp (name, ".rela", 5) == 0)
2268 if (s->_raw_size != 0)
2270 asection *target;
2271 const char *outname;
2273 /* Remember whether there are any reloc sections other
2274 than .rela.plt. */
2275 if (strcmp (name+5, ".plt") != 0)
2276 relocs = true;
2278 /* If this relocation section applies to a read only
2279 section, then we probably need a DT_TEXTREL entry. */
2280 outname = bfd_get_section_name (output_bfd,
2281 s->output_section);
2282 target = bfd_get_section_by_name (output_bfd, outname + 5);
2283 if (target != NULL
2284 && (target->flags & SEC_READONLY) != 0
2285 && (target->flags & SEC_ALLOC) != 0)
2286 reltext = true;
2288 /* We use the reloc_count field as a counter if we need
2289 to copy relocs into the output file. */
2290 s->reloc_count = 0;
2293 else if (strcmp (name, ".plt") == 0)
2295 if (hplink->need_plt_stub)
2297 /* Make space for the plt stub at the end of the .plt
2298 section. We want this stub right at the end, up
2299 against the .got section. */
2300 int gotalign = bfd_section_alignment (dynobj, hplink->sgot);
2301 int pltalign = bfd_section_alignment (dynobj, s);
2302 bfd_size_type mask;
2304 if (gotalign > pltalign)
2305 bfd_set_section_alignment (dynobj, s, gotalign);
2306 mask = ((bfd_size_type) 1 << gotalign) - 1;
2307 s->_raw_size = (s->_raw_size + sizeof (plt_stub) + mask) & ~mask;
2310 else if (strcmp (name, ".got") == 0)
2312 else
2314 /* It's not one of our sections, so don't allocate space. */
2315 continue;
2318 if (s->_raw_size == 0)
2320 /* If we don't need this section, strip it from the
2321 output file. This is mostly to handle .rela.bss and
2322 .rela.plt. We must create both sections in
2323 create_dynamic_sections, because they must be created
2324 before the linker maps input sections to output
2325 sections. The linker does that before
2326 adjust_dynamic_symbol is called, and it is that
2327 function which decides whether anything needs to go
2328 into these sections. */
2329 _bfd_strip_section_from_output (info, s);
2330 continue;
2333 /* Allocate memory for the section contents. Zero it, because
2334 we may not fill in all the reloc sections. */
2335 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2336 if (s->contents == NULL && s->_raw_size != 0)
2337 return false;
2340 if (hplink->root.dynamic_sections_created)
2342 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2343 actually has nothing to do with the PLT, it is how we
2344 communicate the LTP value of a load module to the dynamic
2345 linker. */
2346 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
2347 return false;
2349 /* Add some entries to the .dynamic section. We fill in the
2350 values later, in elf32_hppa_finish_dynamic_sections, but we
2351 must add the entries now so that we get the correct size for
2352 the .dynamic section. The DT_DEBUG entry is filled in by the
2353 dynamic linker and used by the debugger. */
2354 if (! info->shared)
2356 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2357 return false;
2360 if (hplink->srelplt->_raw_size != 0)
2362 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2363 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2364 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2365 return false;
2368 if (relocs)
2370 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2371 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2372 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2373 sizeof (Elf32_External_Rela)))
2374 return false;
2377 if (reltext)
2379 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2380 return false;
2381 info->flags |= DF_TEXTREL;
2385 return true;
2388 /* External entry points for sizing and building linker stubs. */
2390 /* Determine and set the size of the stub section for a final link.
2392 The basic idea here is to examine all the relocations looking for
2393 PC-relative calls to a target that is unreachable with a "bl"
2394 instruction. */
2396 boolean
2397 elf32_hppa_size_stubs (output_bfd, stub_bfd, info, multi_subspace, group_size,
2398 add_stub_section, layout_sections_again)
2399 bfd *output_bfd;
2400 bfd *stub_bfd;
2401 struct bfd_link_info *info;
2402 boolean multi_subspace;
2403 bfd_signed_vma group_size;
2404 asection * (*add_stub_section) PARAMS ((const char *, asection *));
2405 void (*layout_sections_again) PARAMS ((void));
2407 bfd *input_bfd;
2408 asection *section;
2409 asection **input_list, **list;
2410 Elf_Internal_Sym *local_syms, **all_local_syms;
2411 unsigned int bfd_indx, bfd_count;
2412 int top_id, top_index;
2413 struct elf32_hppa_link_hash_table *hplink;
2414 bfd_size_type stub_group_size;
2415 boolean stubs_always_before_branch;
2416 boolean stub_changed = 0;
2417 boolean ret = 0;
2419 hplink = hppa_link_hash_table (info);
2421 /* Stash our params away. */
2422 hplink->stub_bfd = stub_bfd;
2423 hplink->multi_subspace = multi_subspace;
2424 hplink->add_stub_section = add_stub_section;
2425 hplink->layout_sections_again = layout_sections_again;
2426 stubs_always_before_branch = group_size < 0;
2427 if (group_size < 0)
2428 stub_group_size = -group_size;
2429 else
2430 stub_group_size = group_size;
2431 if (stub_group_size == 1)
2433 /* Default values. */
2434 stub_group_size = 8000000;
2435 if (hplink->has_17bit_branch || hplink->multi_subspace)
2436 stub_group_size = 250000;
2437 if (hplink->has_12bit_branch)
2438 stub_group_size = 7812;
2441 /* Count the number of input BFDs and find the top input section id. */
2442 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2443 input_bfd != NULL;
2444 input_bfd = input_bfd->link_next)
2446 bfd_count += 1;
2447 for (section = input_bfd->sections;
2448 section != NULL;
2449 section = section->next)
2451 if (top_id < section->id)
2452 top_id = section->id;
2456 hplink->stub_group
2457 = (struct map_stub *) bfd_zmalloc (sizeof (struct map_stub) * (top_id + 1));
2458 if (hplink->stub_group == NULL)
2459 return false;
2461 /* Make a list of input sections for each output section included in
2462 the link.
2464 We can't use output_bfd->section_count here to find the top output
2465 section index as some sections may have been removed, and
2466 _bfd_strip_section_from_output doesn't renumber the indices. */
2467 for (section = output_bfd->sections, top_index = 0;
2468 section != NULL;
2469 section = section->next)
2471 if (top_index < section->index)
2472 top_index = section->index;
2475 input_list
2476 = (asection **) bfd_malloc (sizeof (asection *) * (top_index + 1));
2477 if (input_list == NULL)
2478 return false;
2480 /* For sections we aren't interested in, mark their entries with a
2481 value we can check later. */
2482 list = input_list + top_index;
2484 *list = bfd_abs_section_ptr;
2485 while (list-- != input_list);
2487 for (section = output_bfd->sections;
2488 section != NULL;
2489 section = section->next)
2491 if ((section->flags & SEC_CODE) != 0)
2492 input_list[section->index] = NULL;
2495 /* Now actually build the lists. */
2496 for (input_bfd = info->input_bfds;
2497 input_bfd != NULL;
2498 input_bfd = input_bfd->link_next)
2500 for (section = input_bfd->sections;
2501 section != NULL;
2502 section = section->next)
2504 if (section->output_section != NULL
2505 && section->output_section->owner == output_bfd
2506 && section->output_section->index <= top_index)
2508 list = input_list + section->output_section->index;
2509 if (*list != bfd_abs_section_ptr)
2511 /* Steal the link_sec pointer for our list. */
2512 #define PREV_SEC(sec) (hplink->stub_group[(sec)->id].link_sec)
2513 /* This happens to make the list in reverse order,
2514 which is what we want. */
2515 PREV_SEC (section) = *list;
2516 *list = section;
2522 /* See whether we can group stub sections together. Grouping stub
2523 sections may result in fewer stubs. More importantly, we need to
2524 put all .init* and .fini* stubs at the beginning of the .init or
2525 .fini output sections respectively, because glibc splits the
2526 _init and _fini functions into multiple parts. Putting a stub in
2527 the middle of a function is not a good idea. */
2528 list = input_list + top_index;
2531 asection *tail = *list;
2532 if (tail == bfd_abs_section_ptr)
2533 continue;
2534 while (tail != NULL)
2536 asection *curr;
2537 asection *prev;
2538 bfd_size_type total;
2540 curr = tail;
2541 if (tail->_cooked_size)
2542 total = tail->_cooked_size;
2543 else
2544 total = tail->_raw_size;
2545 while ((prev = PREV_SEC (curr)) != NULL
2546 && ((total += curr->output_offset - prev->output_offset)
2547 < stub_group_size))
2548 curr = prev;
2550 /* OK, the size from the start of CURR to the end is less
2551 than 250000 bytes and thus can be handled by one stub
2552 section. (or the tail section is itself larger than
2553 250000 bytes, in which case we may be toast.)
2554 We should really be keeping track of the total size of
2555 stubs added here, as stubs contribute to the final output
2556 section size. That's a little tricky, and this way will
2557 only break if stubs added total more than 12144 bytes, or
2558 1518 long branch stubs. It seems unlikely for more than
2559 1518 different functions to be called, especially from
2560 code only 250000 bytes long. */
2563 prev = PREV_SEC (tail);
2564 /* Set up this stub group. */
2565 hplink->stub_group[tail->id].link_sec = curr;
2567 while (tail != curr && (tail = prev) != NULL);
2569 /* But wait, there's more! Input sections up to 250000
2570 bytes before the stub section can be handled by it too. */
2571 if (!stubs_always_before_branch)
2573 total = 0;
2574 while (prev != NULL
2575 && ((total += tail->output_offset - prev->output_offset)
2576 < stub_group_size))
2578 tail = prev;
2579 prev = PREV_SEC (tail);
2580 hplink->stub_group[tail->id].link_sec = curr;
2583 tail = prev;
2586 while (list-- != input_list);
2587 free (input_list);
2588 #undef PREV_SEC
2590 /* We want to read in symbol extension records only once. To do this
2591 we need to read in the local symbols in parallel and save them for
2592 later use; so hold pointers to the local symbols in an array. */
2593 all_local_syms
2594 = (Elf_Internal_Sym **) bfd_zmalloc (sizeof (Elf_Internal_Sym *)
2595 * bfd_count);
2596 if (all_local_syms == NULL)
2597 return false;
2599 /* Walk over all the input BFDs, swapping in local symbols.
2600 If we are creating a shared library, create hash entries for the
2601 export stubs. */
2602 for (input_bfd = info->input_bfds, bfd_indx = 0;
2603 input_bfd != NULL;
2604 input_bfd = input_bfd->link_next, bfd_indx++)
2606 Elf_Internal_Shdr *symtab_hdr;
2607 Elf_Internal_Sym *isym;
2608 Elf32_External_Sym *ext_syms, *esym, *end_sy;
2610 /* We'll need the symbol table in a second. */
2611 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2612 if (symtab_hdr->sh_info == 0)
2613 continue;
2615 /* We need an array of the local symbols attached to the input bfd.
2616 Unfortunately, we're going to have to read & swap them in. */
2617 local_syms = (Elf_Internal_Sym *)
2618 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf_Internal_Sym));
2619 if (local_syms == NULL)
2621 goto error_ret_free_local;
2623 all_local_syms[bfd_indx] = local_syms;
2624 ext_syms = (Elf32_External_Sym *)
2625 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf32_External_Sym));
2626 if (ext_syms == NULL)
2628 goto error_ret_free_local;
2631 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2632 || (bfd_read (ext_syms, 1,
2633 (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)),
2634 input_bfd)
2635 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2637 free (ext_syms);
2638 goto error_ret_free_local;
2641 /* Swap the local symbols in. */
2642 isym = local_syms;
2643 esym = ext_syms;
2644 for (end_sy = esym + symtab_hdr->sh_info; esym < end_sy; esym++, isym++)
2645 bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
2647 /* Now we can free the external symbols. */
2648 free (ext_syms);
2650 #if ! LONG_BRANCH_PIC_IN_SHLIB
2651 /* If this is a shared link, find all the stub reloc sections. */
2652 if (info->shared)
2653 for (section = input_bfd->sections;
2654 section != NULL;
2655 section = section->next)
2657 char *name;
2658 asection *reloc_sec;
2660 name = bfd_malloc (strlen (section->name)
2661 + sizeof STUB_SUFFIX
2662 + 5);
2663 if (name == NULL)
2664 return false;
2665 sprintf (name, ".rela%s%s", section->name, STUB_SUFFIX);
2666 reloc_sec = bfd_get_section_by_name (hplink->root.dynobj, name);
2667 hplink->stub_group[section->id].reloc_sec = reloc_sec;
2668 free (name);
2670 #endif
2672 if (info->shared && hplink->multi_subspace)
2674 struct elf_link_hash_entry **sym_hashes;
2675 struct elf_link_hash_entry **end_hashes;
2676 unsigned int symcount;
2678 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2679 - symtab_hdr->sh_info);
2680 sym_hashes = elf_sym_hashes (input_bfd);
2681 end_hashes = sym_hashes + symcount;
2683 /* Look through the global syms for functions; We need to
2684 build export stubs for all globally visible functions. */
2685 for (; sym_hashes < end_hashes; sym_hashes++)
2687 struct elf32_hppa_link_hash_entry *hash;
2689 hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
2691 while (hash->elf.root.type == bfd_link_hash_indirect
2692 || hash->elf.root.type == bfd_link_hash_warning)
2693 hash = ((struct elf32_hppa_link_hash_entry *)
2694 hash->elf.root.u.i.link);
2696 /* At this point in the link, undefined syms have been
2697 resolved, so we need to check that the symbol was
2698 defined in this BFD. */
2699 if ((hash->elf.root.type == bfd_link_hash_defined
2700 || hash->elf.root.type == bfd_link_hash_defweak)
2701 && hash->elf.type == STT_FUNC
2702 && hash->elf.root.u.def.section->output_section != NULL
2703 && (hash->elf.root.u.def.section->output_section->owner
2704 == output_bfd)
2705 && hash->elf.root.u.def.section->owner == input_bfd
2706 && (hash->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
2707 && !(hash->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2708 && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2710 asection *sec;
2711 const char *stub_name;
2712 struct elf32_hppa_stub_hash_entry *stub_entry;
2714 sec = hash->elf.root.u.def.section;
2715 stub_name = hash->elf.root.root.string;
2716 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2717 stub_name,
2718 false, false);
2719 if (stub_entry == NULL)
2721 stub_entry = hppa_add_stub (stub_name, sec, hplink);
2722 if (!stub_entry)
2723 goto error_ret_free_local;
2725 stub_entry->target_value = hash->elf.root.u.def.value;
2726 stub_entry->target_section = hash->elf.root.u.def.section;
2727 stub_entry->stub_type = hppa_stub_export;
2728 stub_entry->h = hash;
2729 stub_changed = 1;
2731 else
2733 (*_bfd_error_handler) (_("%s: duplicate export stub %s"),
2734 bfd_get_filename (input_bfd),
2735 stub_name);
2742 while (1)
2744 asection *stub_sec;
2746 for (input_bfd = info->input_bfds, bfd_indx = 0;
2747 input_bfd != NULL;
2748 input_bfd = input_bfd->link_next, bfd_indx++)
2750 Elf_Internal_Shdr *symtab_hdr;
2752 /* We'll need the symbol table in a second. */
2753 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2754 if (symtab_hdr->sh_info == 0)
2755 continue;
2757 local_syms = all_local_syms[bfd_indx];
2759 /* Walk over each section attached to the input bfd. */
2760 for (section = input_bfd->sections;
2761 section != NULL;
2762 section = section->next)
2764 Elf_Internal_Shdr *input_rel_hdr;
2765 Elf32_External_Rela *external_relocs, *erelaend, *erela;
2766 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2768 /* If there aren't any relocs, then there's nothing more
2769 to do. */
2770 if ((section->flags & SEC_RELOC) == 0
2771 || section->reloc_count == 0)
2772 continue;
2774 /* If this section is a link-once section that will be
2775 discarded, then don't create any stubs. */
2776 if (section->output_section == NULL
2777 || section->output_section->owner != output_bfd)
2778 continue;
2780 /* Allocate space for the external relocations. */
2781 external_relocs
2782 = ((Elf32_External_Rela *)
2783 bfd_malloc (section->reloc_count
2784 * sizeof (Elf32_External_Rela)));
2785 if (external_relocs == NULL)
2787 goto error_ret_free_local;
2790 /* Likewise for the internal relocations. */
2791 internal_relocs = ((Elf_Internal_Rela *)
2792 bfd_malloc (section->reloc_count
2793 * sizeof (Elf_Internal_Rela)));
2794 if (internal_relocs == NULL)
2796 free (external_relocs);
2797 goto error_ret_free_local;
2800 /* Read in the external relocs. */
2801 input_rel_hdr = &elf_section_data (section)->rel_hdr;
2802 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
2803 || bfd_read (external_relocs, 1,
2804 input_rel_hdr->sh_size,
2805 input_bfd) != input_rel_hdr->sh_size)
2807 free (external_relocs);
2808 error_ret_free_internal:
2809 free (internal_relocs);
2810 goto error_ret_free_local;
2813 /* Swap in the relocs. */
2814 erela = external_relocs;
2815 erelaend = erela + section->reloc_count;
2816 irela = internal_relocs;
2817 for (; erela < erelaend; erela++, irela++)
2818 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
2820 /* We're done with the external relocs, free them. */
2821 free (external_relocs);
2823 /* Now examine each relocation. */
2824 irela = internal_relocs;
2825 irelaend = irela + section->reloc_count;
2826 for (; irela < irelaend; irela++)
2828 unsigned int r_type, r_indx;
2829 enum elf32_hppa_stub_type stub_type;
2830 struct elf32_hppa_stub_hash_entry *stub_entry;
2831 asection *sym_sec;
2832 bfd_vma sym_value;
2833 bfd_vma destination;
2834 struct elf32_hppa_link_hash_entry *hash;
2835 char *stub_name;
2836 const asection *id_sec;
2838 r_type = ELF32_R_TYPE (irela->r_info);
2839 r_indx = ELF32_R_SYM (irela->r_info);
2841 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2843 bfd_set_error (bfd_error_bad_value);
2844 goto error_ret_free_internal;
2847 /* Only look for stubs on call instructions. */
2848 if (r_type != (unsigned int) R_PARISC_PCREL12F
2849 && r_type != (unsigned int) R_PARISC_PCREL17F
2850 && r_type != (unsigned int) R_PARISC_PCREL22F)
2851 continue;
2853 /* Now determine the call target, its name, value,
2854 section. */
2855 sym_sec = NULL;
2856 sym_value = 0;
2857 destination = 0;
2858 hash = NULL;
2859 if (r_indx < symtab_hdr->sh_info)
2861 /* It's a local symbol. */
2862 Elf_Internal_Sym *sym;
2863 Elf_Internal_Shdr *hdr;
2865 sym = local_syms + r_indx;
2866 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2867 sym_sec = hdr->bfd_section;
2868 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2869 sym_value = sym->st_value;
2870 destination = (sym_value + irela->r_addend
2871 + sym_sec->output_offset
2872 + sym_sec->output_section->vma);
2874 else
2876 /* It's an external symbol. */
2877 int e_indx;
2879 e_indx = r_indx - symtab_hdr->sh_info;
2880 hash = ((struct elf32_hppa_link_hash_entry *)
2881 elf_sym_hashes (input_bfd)[e_indx]);
2883 while (hash->elf.root.type == bfd_link_hash_indirect
2884 || hash->elf.root.type == bfd_link_hash_warning)
2885 hash = ((struct elf32_hppa_link_hash_entry *)
2886 hash->elf.root.u.i.link);
2888 if (hash->elf.root.type == bfd_link_hash_defined
2889 || hash->elf.root.type == bfd_link_hash_defweak)
2891 sym_sec = hash->elf.root.u.def.section;
2892 sym_value = hash->elf.root.u.def.value;
2893 if (sym_sec->output_section != NULL)
2894 destination = (sym_value + irela->r_addend
2895 + sym_sec->output_offset
2896 + sym_sec->output_section->vma);
2898 else if (hash->elf.root.type == bfd_link_hash_undefweak)
2900 if (! info->shared)
2901 continue;
2903 else if (hash->elf.root.type == bfd_link_hash_undefined)
2905 if (! (info->shared
2906 && !info->no_undefined
2907 && (ELF_ST_VISIBILITY (hash->elf.other)
2908 == STV_DEFAULT)))
2909 continue;
2911 else
2913 bfd_set_error (bfd_error_bad_value);
2914 goto error_ret_free_internal;
2918 /* Determine what (if any) linker stub is needed. */
2919 stub_type = hppa_type_of_stub (section, irela, hash,
2920 destination);
2921 if (stub_type == hppa_stub_none)
2922 continue;
2924 /* Support for grouping stub sections. */
2925 id_sec = hplink->stub_group[section->id].link_sec;
2927 /* Get the name of this stub. */
2928 stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
2929 if (!stub_name)
2930 goto error_ret_free_internal;
2932 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2933 stub_name,
2934 false, false);
2935 if (stub_entry != NULL)
2937 /* The proper stub has already been created. */
2938 free (stub_name);
2939 continue;
2942 stub_entry = hppa_add_stub (stub_name, section, hplink);
2943 if (stub_entry == NULL)
2945 free (stub_name);
2946 goto error_ret_free_local;
2949 stub_entry->target_value = sym_value;
2950 stub_entry->target_section = sym_sec;
2951 stub_entry->stub_type = stub_type;
2952 if (info->shared)
2954 if (stub_type == hppa_stub_import)
2955 stub_entry->stub_type = hppa_stub_import_shared;
2956 else if (stub_type == hppa_stub_long_branch
2957 && (LONG_BRANCH_PIC_IN_SHLIB || hash == NULL))
2958 stub_entry->stub_type = hppa_stub_long_branch_shared;
2960 stub_entry->h = hash;
2961 stub_changed = 1;
2964 /* We're done with the internal relocs, free them. */
2965 free (internal_relocs);
2969 if (!stub_changed)
2970 break;
2972 /* OK, we've added some stubs. Find out the new size of the
2973 stub sections. */
2974 for (stub_sec = hplink->stub_bfd->sections;
2975 stub_sec != NULL;
2976 stub_sec = stub_sec->next)
2978 stub_sec->_raw_size = 0;
2979 stub_sec->_cooked_size = 0;
2981 #if ! LONG_BRANCH_PIC_IN_SHLIB
2983 int i;
2985 for (i = top_id; i >= 0; --i)
2987 /* This will probably hit the same section many times.. */
2988 stub_sec = hplink->stub_group[i].reloc_sec;
2989 if (stub_sec != NULL)
2991 stub_sec->_raw_size = 0;
2992 stub_sec->_cooked_size = 0;
2996 #endif
2998 bfd_hash_traverse (&hplink->stub_hash_table,
2999 hppa_size_one_stub,
3000 hplink);
3002 /* Ask the linker to do its stuff. */
3003 (*hplink->layout_sections_again) ();
3004 stub_changed = 0;
3007 ret = 1;
3009 error_ret_free_local:
3010 while (bfd_count-- > 0)
3011 if (all_local_syms[bfd_count])
3012 free (all_local_syms[bfd_count]);
3013 free (all_local_syms);
3015 return ret;
3018 /* For a final link, this function is called after we have sized the
3019 stubs to provide a value for __gp. */
3021 boolean
3022 elf32_hppa_set_gp (abfd, info)
3023 bfd *abfd;
3024 struct bfd_link_info *info;
3026 struct elf32_hppa_link_hash_table *hplink;
3027 struct elf_link_hash_entry *h;
3028 asection *sec;
3029 bfd_vma gp_val;
3031 hplink = hppa_link_hash_table (info);
3032 h = elf_link_hash_lookup (&hplink->root, "$global$",
3033 false, false, false);
3035 if (h != NULL
3036 && (h->root.type == bfd_link_hash_defined
3037 || h->root.type == bfd_link_hash_defweak))
3039 gp_val = h->root.u.def.value;
3040 sec = h->root.u.def.section;
3042 else
3044 /* Choose to point our LTP at, in this order, one of .plt, .got,
3045 or .data, if these sections exist. In the case of choosing
3046 .plt try to make the LTP ideal for addressing anywhere in the
3047 .plt or .got with a 14 bit signed offset. Typically, the end
3048 of the .plt is the start of the .got, so choose .plt + 0x2000
3049 if either the .plt or .got is larger than 0x2000. If both
3050 the .plt and .got are smaller than 0x2000, choose the end of
3051 the .plt section. */
3053 sec = hplink->splt;
3054 if (sec != NULL)
3056 gp_val = sec->_raw_size;
3057 if (gp_val > 0x2000
3058 || (hplink->sgot && hplink->sgot->_raw_size > 0x2000))
3060 gp_val = 0x2000;
3063 else
3065 gp_val = 0;
3066 sec = hplink->sgot;
3067 if (sec != NULL)
3069 /* We know we don't have a .plt. If .got is large,
3070 offset our LTP. */
3071 if (sec->_raw_size > 0x2000)
3072 gp_val = 0x2000;
3074 else
3076 /* No .plt or .got. Who cares what the LTP is? */
3077 sec = bfd_get_section_by_name (abfd, ".data");
3081 if (h != NULL)
3083 h->root.type = bfd_link_hash_defined;
3084 h->root.u.def.value = gp_val;
3085 if (sec != NULL)
3086 h->root.u.def.section = sec;
3087 else
3088 h->root.u.def.section = bfd_abs_section_ptr;
3092 if (sec != NULL && sec->output_section != NULL)
3093 gp_val += sec->output_section->vma + sec->output_offset;
3095 elf_gp (abfd) = gp_val;
3096 return true;
3099 /* Build all the stubs associated with the current output file. The
3100 stubs are kept in a hash table attached to the main linker hash
3101 table. We also set up the .plt entries for statically linked PIC
3102 functions here. This function is called via hppaelf_finish in the
3103 linker. */
3105 boolean
3106 elf32_hppa_build_stubs (info)
3107 struct bfd_link_info *info;
3109 asection *stub_sec;
3110 struct bfd_hash_table *table;
3111 struct elf32_hppa_link_hash_table *hplink;
3113 hplink = hppa_link_hash_table (info);
3115 for (stub_sec = hplink->stub_bfd->sections;
3116 stub_sec != NULL;
3117 stub_sec = stub_sec->next)
3119 size_t size;
3121 /* Allocate memory to hold the linker stubs. */
3122 size = stub_sec->_raw_size;
3123 stub_sec->contents = (unsigned char *) bfd_zalloc (hplink->stub_bfd,
3124 size);
3125 if (stub_sec->contents == NULL && size != 0)
3126 return false;
3127 stub_sec->_raw_size = 0;
3130 /* Build the stubs as directed by the stub hash table. */
3131 table = &hplink->stub_hash_table;
3132 bfd_hash_traverse (table, hppa_build_one_stub, info);
3134 return true;
3137 /* Perform a final link. */
3139 static boolean
3140 elf32_hppa_final_link (abfd, info)
3141 bfd *abfd;
3142 struct bfd_link_info *info;
3144 asection *s;
3146 /* Invoke the regular ELF garbage collecting linker to do all the
3147 work. */
3148 if (!_bfd_elf32_gc_common_final_link (abfd, info))
3149 return false;
3151 /* If we're producing a final executable, sort the contents of the
3152 unwind section. Magic section names, but this is much safer than
3153 having elf32_hppa_relocate_section remember where SEGREL32 relocs
3154 occurred. Consider what happens if someone inept creates a
3155 linker script that puts unwind information in .text. */
3156 s = bfd_get_section_by_name (abfd, ".PARISC.unwind");
3157 if (s != NULL)
3159 bfd_size_type size;
3160 char *contents;
3162 size = s->_raw_size;
3163 contents = bfd_malloc (size);
3164 if (contents == NULL)
3165 return false;
3167 if (! bfd_get_section_contents (abfd, s, contents, (file_ptr) 0, size))
3168 return false;
3170 qsort (contents, size / 16, 16, hppa_unwind_entry_compare);
3172 if (! bfd_set_section_contents (abfd, s, contents, (file_ptr) 0, size))
3173 return false;
3175 return true;
3178 /* Record the lowest address for the data and text segments. */
3180 static void
3181 hppa_record_segment_addr (abfd, section, data)
3182 bfd *abfd ATTRIBUTE_UNUSED;
3183 asection *section;
3184 PTR data;
3186 struct elf32_hppa_link_hash_table *hplink;
3188 hplink = (struct elf32_hppa_link_hash_table *) data;
3190 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3192 bfd_vma value = section->vma - section->filepos;
3194 if ((section->flags & SEC_READONLY) != 0)
3196 if (value < hplink->text_segment_base)
3197 hplink->text_segment_base = value;
3199 else
3201 if (value < hplink->data_segment_base)
3202 hplink->data_segment_base = value;
3207 /* Perform a relocation as part of a final link. */
3209 static bfd_reloc_status_type
3210 final_link_relocate (input_section, contents, rel, value, hplink, sym_sec, h)
3211 asection *input_section;
3212 bfd_byte *contents;
3213 const Elf_Internal_Rela *rel;
3214 bfd_vma value;
3215 struct elf32_hppa_link_hash_table *hplink;
3216 asection *sym_sec;
3217 struct elf32_hppa_link_hash_entry *h;
3219 int insn;
3220 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3221 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3222 int r_format = howto->bitsize;
3223 enum hppa_reloc_field_selector_type_alt r_field;
3224 bfd *input_bfd = input_section->owner;
3225 bfd_vma offset = rel->r_offset;
3226 bfd_vma max_branch_offset = 0;
3227 bfd_byte *hit_data = contents + offset;
3228 bfd_signed_vma addend = rel->r_addend;
3229 bfd_vma location;
3230 struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
3231 int val;
3233 if (r_type == R_PARISC_NONE)
3234 return bfd_reloc_ok;
3236 insn = bfd_get_32 (input_bfd, hit_data);
3238 /* Find out where we are and where we're going. */
3239 location = (offset +
3240 input_section->output_offset +
3241 input_section->output_section->vma);
3243 switch (r_type)
3245 case R_PARISC_PCREL12F:
3246 case R_PARISC_PCREL17F:
3247 case R_PARISC_PCREL22F:
3248 /* If this is a call to a function defined in another dynamic
3249 library, or if it is a call to a PIC function in the same
3250 object, or if this is a shared link and it is a call to a
3251 weak symbol which may or may not be in the same object, then
3252 find the import stub in the stub hash. */
3253 if (sym_sec == NULL
3254 || sym_sec->output_section == NULL
3255 || (h != NULL
3256 && ((h->maybe_pic_call
3257 && !(input_section->flags & SEC_HAS_GOT_REF))
3258 || (h->elf.root.type == bfd_link_hash_defweak
3259 && h->elf.dynindx != -1
3260 && h->elf.plt.offset != (bfd_vma) -1))))
3262 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3263 h, rel, hplink);
3264 if (stub_entry != NULL)
3266 value = (stub_entry->stub_offset
3267 + stub_entry->stub_sec->output_offset
3268 + stub_entry->stub_sec->output_section->vma);
3269 addend = 0;
3271 else if (sym_sec == NULL && h != NULL
3272 && h->elf.root.type == bfd_link_hash_undefweak)
3274 /* It's OK if undefined weak. Make undefined weak
3275 branches go nowhere. */
3276 value = location;
3277 addend = 0;
3279 else
3280 return bfd_reloc_notsupported;
3282 /* Fall thru. */
3284 case R_PARISC_PCREL21L:
3285 case R_PARISC_PCREL17C:
3286 case R_PARISC_PCREL17R:
3287 case R_PARISC_PCREL14R:
3288 case R_PARISC_PCREL14F:
3289 /* Make it a pc relative offset. */
3290 value -= location;
3291 addend -= 8;
3292 break;
3294 case R_PARISC_DPREL21L:
3295 case R_PARISC_DPREL14R:
3296 case R_PARISC_DPREL14F:
3297 /* For all the DP relative relocations, we need to examine the symbol's
3298 section. If it's a code section, then "data pointer relative" makes
3299 no sense. In that case we don't adjust the "value", and for 21 bit
3300 addil instructions, we change the source addend register from %dp to
3301 %r0. This situation commonly arises when a variable's "constness"
3302 is declared differently from the way the variable is defined. For
3303 instance: "extern int foo" with foo defined as "const int foo". */
3304 if (sym_sec == NULL)
3305 break;
3306 if ((sym_sec->flags & SEC_CODE) != 0)
3308 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3309 == (((int) OP_ADDIL << 26) | (27 << 21)))
3311 insn &= ~ (0x1f << 21);
3312 #if 1 /* debug them. */
3313 (*_bfd_error_handler)
3314 (_("%s(%s+0x%lx): fixing %s"),
3315 bfd_get_filename (input_bfd),
3316 input_section->name,
3317 (long) rel->r_offset,
3318 howto->name);
3319 #endif
3321 /* Now try to make things easy for the dynamic linker. */
3323 break;
3325 /* Fall thru. */
3327 case R_PARISC_DLTIND21L:
3328 case R_PARISC_DLTIND14R:
3329 case R_PARISC_DLTIND14F:
3330 value -= elf_gp (input_section->output_section->owner);
3331 break;
3333 case R_PARISC_SEGREL32:
3334 if ((sym_sec->flags & SEC_CODE) != 0)
3335 value -= hplink->text_segment_base;
3336 else
3337 value -= hplink->data_segment_base;
3338 break;
3340 default:
3341 break;
3344 switch (r_type)
3346 case R_PARISC_DIR32:
3347 case R_PARISC_DIR14F:
3348 case R_PARISC_DIR17F:
3349 case R_PARISC_PCREL17C:
3350 case R_PARISC_PCREL14F:
3351 case R_PARISC_DPREL14F:
3352 case R_PARISC_PLABEL32:
3353 case R_PARISC_DLTIND14F:
3354 case R_PARISC_SEGBASE:
3355 case R_PARISC_SEGREL32:
3356 r_field = e_fsel;
3357 break;
3359 case R_PARISC_DIR21L:
3360 case R_PARISC_PCREL21L:
3361 case R_PARISC_DPREL21L:
3362 case R_PARISC_PLABEL21L:
3363 case R_PARISC_DLTIND21L:
3364 r_field = e_lrsel;
3365 break;
3367 case R_PARISC_DIR17R:
3368 case R_PARISC_PCREL17R:
3369 case R_PARISC_DIR14R:
3370 case R_PARISC_PCREL14R:
3371 case R_PARISC_DPREL14R:
3372 case R_PARISC_PLABEL14R:
3373 case R_PARISC_DLTIND14R:
3374 r_field = e_rrsel;
3375 break;
3377 case R_PARISC_PCREL12F:
3378 case R_PARISC_PCREL17F:
3379 case R_PARISC_PCREL22F:
3380 r_field = e_fsel;
3382 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3384 max_branch_offset = (1 << (17-1)) << 2;
3386 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3388 max_branch_offset = (1 << (12-1)) << 2;
3390 else
3392 max_branch_offset = (1 << (22-1)) << 2;
3395 /* sym_sec is NULL on undefined weak syms or when shared on
3396 undefined syms. We've already checked for a stub for the
3397 shared undefined case. */
3398 if (sym_sec == NULL)
3399 break;
3401 /* If the branch is out of reach, then redirect the
3402 call to the local stub for this function. */
3403 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3405 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3406 h, rel, hplink);
3407 if (stub_entry == NULL)
3408 return bfd_reloc_notsupported;
3410 /* Munge up the value and addend so that we call the stub
3411 rather than the procedure directly. */
3412 value = (stub_entry->stub_offset
3413 + stub_entry->stub_sec->output_offset
3414 + stub_entry->stub_sec->output_section->vma
3415 - location);
3416 addend = -8;
3418 break;
3420 /* Something we don't know how to handle. */
3421 default:
3422 return bfd_reloc_notsupported;
3425 /* Make sure we can reach the stub. */
3426 if (max_branch_offset != 0
3427 && value + addend + max_branch_offset >= 2*max_branch_offset)
3429 (*_bfd_error_handler)
3430 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3431 bfd_get_filename (input_bfd),
3432 input_section->name,
3433 (long) rel->r_offset,
3434 stub_entry->root.string);
3435 return bfd_reloc_notsupported;
3438 val = hppa_field_adjust (value, addend, r_field);
3440 switch (r_type)
3442 case R_PARISC_PCREL12F:
3443 case R_PARISC_PCREL17C:
3444 case R_PARISC_PCREL17F:
3445 case R_PARISC_PCREL17R:
3446 case R_PARISC_PCREL22F:
3447 case R_PARISC_DIR17F:
3448 case R_PARISC_DIR17R:
3449 /* This is a branch. Divide the offset by four.
3450 Note that we need to decide whether it's a branch or
3451 otherwise by inspecting the reloc. Inspecting insn won't
3452 work as insn might be from a .word directive. */
3453 val >>= 2;
3454 break;
3456 default:
3457 break;
3460 insn = hppa_rebuild_insn (insn, val, r_format);
3462 /* Update the instruction word. */
3463 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3464 return bfd_reloc_ok;
3467 /* Relocate an HPPA ELF section. */
3469 static boolean
3470 elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
3471 contents, relocs, local_syms, local_sections)
3472 bfd *output_bfd;
3473 struct bfd_link_info *info;
3474 bfd *input_bfd;
3475 asection *input_section;
3476 bfd_byte *contents;
3477 Elf_Internal_Rela *relocs;
3478 Elf_Internal_Sym *local_syms;
3479 asection **local_sections;
3481 bfd *dynobj;
3482 bfd_vma *local_got_offsets;
3483 struct elf32_hppa_link_hash_table *hplink;
3484 Elf_Internal_Shdr *symtab_hdr;
3485 Elf_Internal_Rela *rel;
3486 Elf_Internal_Rela *relend;
3487 asection *sreloc;
3489 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3491 hplink = hppa_link_hash_table (info);
3492 dynobj = hplink->root.dynobj;
3493 local_got_offsets = elf_local_got_offsets (input_bfd);
3494 sreloc = NULL;
3496 rel = relocs;
3497 relend = relocs + input_section->reloc_count;
3498 for (; rel < relend; rel++)
3500 unsigned int r_type;
3501 reloc_howto_type *howto;
3502 unsigned int r_symndx;
3503 struct elf32_hppa_link_hash_entry *h;
3504 Elf_Internal_Sym *sym;
3505 asection *sym_sec;
3506 bfd_vma relocation;
3507 bfd_reloc_status_type r;
3508 const char *sym_name;
3509 boolean plabel;
3511 r_type = ELF32_R_TYPE (rel->r_info);
3512 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3514 bfd_set_error (bfd_error_bad_value);
3515 return false;
3517 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3518 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3519 continue;
3521 r_symndx = ELF32_R_SYM (rel->r_info);
3523 if (info->relocateable)
3525 /* This is a relocateable link. We don't have to change
3526 anything, unless the reloc is against a section symbol,
3527 in which case we have to adjust according to where the
3528 section symbol winds up in the output section. */
3529 if (r_symndx < symtab_hdr->sh_info)
3531 sym = local_syms + r_symndx;
3532 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3534 sym_sec = local_sections[r_symndx];
3535 rel->r_addend += sym_sec->output_offset;
3538 continue;
3541 /* This is a final link. */
3542 h = NULL;
3543 sym = NULL;
3544 sym_sec = NULL;
3545 if (r_symndx < symtab_hdr->sh_info)
3547 /* This is a local symbol, h defaults to NULL. */
3548 sym = local_syms + r_symndx;
3549 sym_sec = local_sections[r_symndx];
3550 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
3551 ? 0 : sym->st_value)
3552 + sym_sec->output_offset
3553 + sym_sec->output_section->vma);
3555 else
3557 int indx;
3559 /* It's a global; Find its entry in the link hash. */
3560 indx = r_symndx - symtab_hdr->sh_info;
3561 h = ((struct elf32_hppa_link_hash_entry *)
3562 elf_sym_hashes (input_bfd)[indx]);
3563 while (h->elf.root.type == bfd_link_hash_indirect
3564 || h->elf.root.type == bfd_link_hash_warning)
3565 h = (struct elf32_hppa_link_hash_entry *) h->elf.root.u.i.link;
3567 relocation = 0;
3568 if (h->elf.root.type == bfd_link_hash_defined
3569 || h->elf.root.type == bfd_link_hash_defweak)
3571 sym_sec = h->elf.root.u.def.section;
3572 /* If sym_sec->output_section is NULL, then it's a
3573 symbol defined in a shared library. */
3574 if (sym_sec->output_section != NULL)
3575 relocation = (h->elf.root.u.def.value
3576 + sym_sec->output_offset
3577 + sym_sec->output_section->vma);
3579 else if (h->elf.root.type == bfd_link_hash_undefweak)
3581 else if (info->shared && !info->no_undefined
3582 && ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT)
3584 if (info->symbolic)
3585 if (!((*info->callbacks->undefined_symbol)
3586 (info, h->elf.root.root.string, input_bfd,
3587 input_section, rel->r_offset, false)))
3588 return false;
3590 else
3592 if (!((*info->callbacks->undefined_symbol)
3593 (info, h->elf.root.root.string, input_bfd,
3594 input_section, rel->r_offset, true)))
3595 return false;
3599 /* Do any required modifications to the relocation value, and
3600 determine what types of dynamic info we need to output, if
3601 any. */
3602 plabel = 0;
3603 switch (r_type)
3605 case R_PARISC_DLTIND14F:
3606 case R_PARISC_DLTIND14R:
3607 case R_PARISC_DLTIND21L:
3608 /* Relocation is to the entry for this symbol in the global
3609 offset table. */
3610 if (h != NULL)
3612 bfd_vma off;
3614 off = h->elf.got.offset;
3615 BFD_ASSERT (off != (bfd_vma) -1);
3617 if (! hplink->root.dynamic_sections_created
3618 || (info->shared
3619 && (info->symbolic || h->elf.dynindx == -1)
3620 && (h->elf.elf_link_hash_flags
3621 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3623 /* This is actually a static link, or it is a
3624 -Bsymbolic link and the symbol is defined
3625 locally, or the symbol was forced to be local
3626 because of a version file. We must initialize
3627 this entry in the global offset table. Since the
3628 offset must always be a multiple of 4, we use the
3629 least significant bit to record whether we have
3630 initialized it already.
3632 When doing a dynamic link, we create a .rela.got
3633 relocation entry to initialize the value. This
3634 is done in the finish_dynamic_symbol routine. */
3635 if ((off & 1) != 0)
3636 off &= ~1;
3637 else
3639 bfd_put_32 (output_bfd, relocation,
3640 hplink->sgot->contents + off);
3641 h->elf.got.offset |= 1;
3645 relocation = off;
3647 else
3649 /* Local symbol case. */
3650 bfd_vma off;
3652 BFD_ASSERT (local_got_offsets != NULL
3653 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3655 off = local_got_offsets[r_symndx];
3657 /* The offset must always be a multiple of 4. We use
3658 the least significant bit to record whether we have
3659 already generated the necessary reloc. */
3660 if ((off & 1) != 0)
3661 off &= ~1;
3662 else
3664 bfd_put_32 (output_bfd, relocation,
3665 hplink->sgot->contents + off);
3667 if (info->shared)
3669 /* Output a dynamic *ABS* relocation for this
3670 GOT entry. In this case it is relative to
3671 the base of the object because the symbol
3672 index is zero. */
3673 Elf_Internal_Rela outrel;
3674 asection *srelgot = hplink->srelgot;
3676 outrel.r_offset = (off
3677 + hplink->sgot->output_offset
3678 + hplink->sgot->output_section->vma);
3679 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3680 outrel.r_addend = relocation;
3681 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3682 ((Elf32_External_Rela *)
3683 srelgot->contents
3684 + srelgot->reloc_count));
3685 ++srelgot->reloc_count;
3688 local_got_offsets[r_symndx] |= 1;
3691 relocation = off;
3694 /* Add the base of the GOT to the relocation value. */
3695 relocation += (hplink->sgot->output_offset
3696 + hplink->sgot->output_section->vma);
3697 break;
3699 case R_PARISC_SEGREL32:
3700 /* If this is the first SEGREL relocation, then initialize
3701 the segment base values. */
3702 if (hplink->text_segment_base == (bfd_vma) -1)
3703 bfd_map_over_sections (output_bfd,
3704 hppa_record_segment_addr,
3705 hplink);
3706 break;
3708 case R_PARISC_PLABEL14R:
3709 case R_PARISC_PLABEL21L:
3710 case R_PARISC_PLABEL32:
3711 if (hplink->root.dynamic_sections_created)
3713 bfd_vma off;
3715 /* If we have a global symbol with a PLT slot, then
3716 redirect this relocation to it. */
3717 if (h != NULL)
3719 off = h->elf.plt.offset;
3721 else
3723 int indx;
3725 indx = r_symndx + symtab_hdr->sh_info;
3726 off = local_got_offsets[indx];
3728 /* As for the local .got entry case, we use the last
3729 bit to record whether we've already initialised
3730 this local .plt entry. */
3731 if ((off & 1) != 0)
3732 off &= ~1;
3733 else
3735 bfd_put_32 (output_bfd,
3736 relocation,
3737 hplink->splt->contents + off);
3738 bfd_put_32 (output_bfd,
3739 elf_gp (hplink->splt->output_section->owner),
3740 hplink->splt->contents + off + 4);
3742 if (info->shared)
3744 /* Output a dynamic IPLT relocation for this
3745 PLT entry. */
3746 Elf_Internal_Rela outrel;
3747 asection *srelplt = hplink->srelplt;
3749 outrel.r_offset = (off
3750 + hplink->splt->output_offset
3751 + hplink->splt->output_section->vma);
3752 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3753 outrel.r_addend = relocation;
3754 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3755 ((Elf32_External_Rela *)
3756 srelplt->contents
3757 + srelplt->reloc_count));
3758 ++srelplt->reloc_count;
3761 local_got_offsets[indx] |= 1;
3765 BFD_ASSERT (off < (bfd_vma) -2);
3767 /* PLABELs contain function pointers. Relocation is to
3768 the entry for the function in the .plt. The magic +2
3769 offset signals to $$dyncall that the function pointer
3770 is in the .plt and thus has a gp pointer too.
3771 Exception: Undefined PLABELs should have a value of
3772 zero. */
3773 if (h == NULL
3774 || (h->elf.root.type != bfd_link_hash_undefweak
3775 && h->elf.root.type != bfd_link_hash_undefined))
3777 relocation = (off
3778 + hplink->splt->output_offset
3779 + hplink->splt->output_section->vma
3780 + 2);
3782 plabel = 1;
3784 /* Fall through and possibly emit a dynamic relocation. */
3786 case R_PARISC_DIR17F:
3787 case R_PARISC_DIR17R:
3788 case R_PARISC_DIR14F:
3789 case R_PARISC_DIR14R:
3790 case R_PARISC_DIR21L:
3791 case R_PARISC_DPREL14F:
3792 case R_PARISC_DPREL14R:
3793 case R_PARISC_DPREL21L:
3794 case R_PARISC_DIR32:
3795 /* The reloc types handled here and this conditional
3796 expression must match the code in check_relocs and
3797 hppa_discard_copies. ie. We need exactly the same
3798 condition as in check_relocs, with some extra conditions
3799 (dynindx test in this case) to cater for relocs removed
3800 by hppa_discard_copies. */
3801 if ((input_section->flags & SEC_ALLOC) != 0
3802 && info->shared
3803 #if RELATIVE_DYNAMIC_RELOCS
3804 && (is_absolute_reloc (r_type)
3805 || ((!info->symbolic
3806 || (h != NULL
3807 && ((h->elf.elf_link_hash_flags
3808 & ELF_LINK_HASH_DEF_REGULAR) == 0
3809 || h->elf.root.type == bfd_link_hash_defweak)))
3810 && (h == NULL || h->elf.dynindx != -1)))
3811 #endif
3814 Elf_Internal_Rela outrel;
3815 boolean skip;
3817 /* When generating a shared object, these relocations
3818 are copied into the output file to be resolved at run
3819 time. */
3821 if (sreloc == NULL)
3823 const char *name;
3825 name = (bfd_elf_string_from_elf_section
3826 (input_bfd,
3827 elf_elfheader (input_bfd)->e_shstrndx,
3828 elf_section_data (input_section)->rel_hdr.sh_name));
3829 if (name == NULL)
3830 return false;
3831 sreloc = bfd_get_section_by_name (dynobj, name);
3832 BFD_ASSERT (sreloc != NULL);
3835 outrel.r_offset = rel->r_offset;
3836 outrel.r_addend = rel->r_addend;
3837 skip = false;
3838 if (elf_section_data (input_section)->stab_info != NULL)
3840 bfd_vma off;
3842 off = (_bfd_stab_section_offset
3843 (output_bfd, &hplink->root.stab_info,
3844 input_section,
3845 &elf_section_data (input_section)->stab_info,
3846 rel->r_offset));
3847 if (off == (bfd_vma) -1)
3848 skip = true;
3849 outrel.r_offset = off;
3852 outrel.r_offset += (input_section->output_offset
3853 + input_section->output_section->vma);
3855 if (skip)
3857 memset (&outrel, 0, sizeof (outrel));
3859 else if (h != NULL
3860 && h->elf.dynindx != -1
3861 && (plabel
3862 || !info->symbolic
3863 || (h->elf.elf_link_hash_flags
3864 & ELF_LINK_HASH_DEF_REGULAR) == 0))
3866 outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3868 else /* It's a local symbol, or one marked to become local. */
3870 int indx = 0;
3872 /* Add the absolute offset of the symbol. */
3873 outrel.r_addend += relocation;
3875 /* Global plabels need to be processed by the
3876 dynamic linker so that functions have at most one
3877 fptr. For this reason, we need to differentiate
3878 between global and local plabels, which we do by
3879 providing the function symbol for a global plabel
3880 reloc, and no symbol for local plabels. */
3881 if (! plabel
3882 && sym_sec != NULL
3883 && sym_sec->output_section != NULL
3884 && ! bfd_is_abs_section (sym_sec))
3886 indx = elf_section_data (sym_sec->output_section)->dynindx;
3887 /* We are turning this relocation into one
3888 against a section symbol, so subtract out the
3889 output section's address but not the offset
3890 of the input section in the output section. */
3891 outrel.r_addend -= sym_sec->output_section->vma;
3894 outrel.r_info = ELF32_R_INFO (indx, r_type);
3897 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3898 ((Elf32_External_Rela *)
3899 sreloc->contents
3900 + sreloc->reloc_count));
3901 ++sreloc->reloc_count;
3903 break;
3905 default:
3906 break;
3909 r = final_link_relocate (input_section, contents, rel, relocation,
3910 hplink, sym_sec, h);
3912 if (r == bfd_reloc_ok)
3913 continue;
3915 if (h != NULL)
3916 sym_name = h->elf.root.root.string;
3917 else
3919 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3920 symtab_hdr->sh_link,
3921 sym->st_name);
3922 if (sym_name == NULL)
3923 return false;
3924 if (*sym_name == '\0')
3925 sym_name = bfd_section_name (input_bfd, sym_sec);
3928 howto = elf_hppa_howto_table + r_type;
3930 if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
3932 (*_bfd_error_handler)
3933 (_("%s(%s+0x%lx): cannot handle %s for %s"),
3934 bfd_get_filename (input_bfd),
3935 input_section->name,
3936 (long) rel->r_offset,
3937 howto->name,
3938 sym_name);
3940 else
3942 if (!((*info->callbacks->reloc_overflow)
3943 (info, sym_name, howto->name, (bfd_vma) 0,
3944 input_bfd, input_section, rel->r_offset)))
3945 return false;
3949 return true;
3952 /* Comparison function for qsort to sort unwind section during a
3953 final link. */
3955 static int
3956 hppa_unwind_entry_compare (a, b)
3957 const PTR a;
3958 const PTR b;
3960 const bfd_byte *ap, *bp;
3961 unsigned long av, bv;
3963 ap = (const bfd_byte *) a;
3964 av = (unsigned long) ap[0] << 24;
3965 av |= (unsigned long) ap[1] << 16;
3966 av |= (unsigned long) ap[2] << 8;
3967 av |= (unsigned long) ap[3];
3969 bp = (const bfd_byte *) b;
3970 bv = (unsigned long) bp[0] << 24;
3971 bv |= (unsigned long) bp[1] << 16;
3972 bv |= (unsigned long) bp[2] << 8;
3973 bv |= (unsigned long) bp[3];
3975 return av < bv ? -1 : av > bv ? 1 : 0;
3978 /* Finish up dynamic symbol handling. We set the contents of various
3979 dynamic sections here. */
3981 static boolean
3982 elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
3983 bfd *output_bfd;
3984 struct bfd_link_info *info;
3985 struct elf_link_hash_entry *h;
3986 Elf_Internal_Sym *sym;
3988 struct elf32_hppa_link_hash_table *hplink;
3989 bfd *dynobj;
3991 hplink = hppa_link_hash_table (info);
3992 dynobj = hplink->root.dynobj;
3994 if (h->plt.offset != (bfd_vma) -1)
3996 bfd_vma value;
3998 /* This symbol has an entry in the procedure linkage table. Set
3999 it up.
4001 The format of a plt entry is
4002 <funcaddr>
4003 <__gp>
4005 value = 0;
4006 if (h->root.type == bfd_link_hash_defined
4007 || h->root.type == bfd_link_hash_defweak)
4009 value = h->root.u.def.value;
4010 if (h->root.u.def.section->output_section != NULL)
4011 value += (h->root.u.def.section->output_offset
4012 + h->root.u.def.section->output_section->vma);
4015 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
4017 Elf_Internal_Rela rel;
4019 /* Create a dynamic IPLT relocation for this entry. */
4020 rel.r_offset = (h->plt.offset
4021 + hplink->splt->output_offset
4022 + hplink->splt->output_section->vma);
4023 if (! ((struct elf32_hppa_link_hash_entry *) h)->plt_abs
4024 && h->dynindx != -1)
4026 /* To support lazy linking, the function pointer is
4027 initialised to point to a special stub stored at the
4028 end of the .plt. This is only done for plt entries
4029 with a non-*ABS* dynamic relocation. */
4030 value = (hplink->splt->output_offset
4031 + hplink->splt->output_section->vma
4032 + hplink->splt->_raw_size
4033 - sizeof (plt_stub)
4034 + PLT_STUB_ENTRY);
4035 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
4036 rel.r_addend = 0;
4038 else
4040 /* This symbol has been marked to become local, and is
4041 used by a plabel so must be kept in the .plt. */
4042 rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
4043 rel.r_addend = value;
4046 bfd_elf32_swap_reloca_out (hplink->splt->output_section->owner,
4047 &rel,
4048 ((Elf32_External_Rela *)
4049 hplink->srelplt->contents
4050 + hplink->srelplt->reloc_count));
4051 hplink->srelplt->reloc_count++;
4054 bfd_put_32 (hplink->splt->owner,
4055 value,
4056 hplink->splt->contents + h->plt.offset);
4057 bfd_put_32 (hplink->splt->owner,
4058 elf_gp (hplink->splt->output_section->owner),
4059 hplink->splt->contents + h->plt.offset + 4);
4060 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
4061 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
4062 && h->dynindx != -1)
4064 memset (hplink->splt->contents + h->plt.offset + 8,
4065 0, PLABEL_PLT_ENTRY_SIZE - PLT_ENTRY_SIZE);
4068 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4070 /* Mark the symbol as undefined, rather than as defined in
4071 the .plt section. Leave the value alone. */
4072 sym->st_shndx = SHN_UNDEF;
4076 if (h->got.offset != (bfd_vma) -1)
4078 Elf_Internal_Rela rel;
4080 /* This symbol has an entry in the global offset table. Set it
4081 up. */
4083 rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
4084 + hplink->sgot->output_offset
4085 + hplink->sgot->output_section->vma);
4087 /* If this is a static link, or it is a -Bsymbolic link and the
4088 symbol is defined locally or was forced to be local because
4089 of a version file, we just want to emit a RELATIVE reloc.
4090 The entry in the global offset table will already have been
4091 initialized in the relocate_section function. */
4092 if (! hplink->root.dynamic_sections_created
4093 || (info->shared
4094 && (info->symbolic || h->dynindx == -1)
4095 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
4097 rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
4098 rel.r_addend = (h->root.u.def.value
4099 + h->root.u.def.section->output_offset
4100 + h->root.u.def.section->output_section->vma);
4102 else
4104 BFD_ASSERT((h->got.offset & 1) == 0);
4105 bfd_put_32 (output_bfd, (bfd_vma) 0,
4106 hplink->sgot->contents + h->got.offset);
4107 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
4108 rel.r_addend = 0;
4111 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4112 ((Elf32_External_Rela *)
4113 hplink->srelgot->contents
4114 + hplink->srelgot->reloc_count));
4115 ++hplink->srelgot->reloc_count;
4118 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4120 asection *s;
4121 Elf_Internal_Rela rel;
4123 /* This symbol needs a copy reloc. Set it up. */
4125 BFD_ASSERT (h->dynindx != -1
4126 && (h->root.type == bfd_link_hash_defined
4127 || h->root.type == bfd_link_hash_defweak));
4129 s = hplink->srelbss;
4131 rel.r_offset = (h->root.u.def.value
4132 + h->root.u.def.section->output_offset
4133 + h->root.u.def.section->output_section->vma);
4134 rel.r_addend = 0;
4135 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
4136 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4137 ((Elf32_External_Rela *) s->contents
4138 + s->reloc_count));
4139 ++s->reloc_count;
4142 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4143 if (h->root.root.string[0] == '_'
4144 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4145 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
4147 sym->st_shndx = SHN_ABS;
4150 return true;
4153 /* Finish up the dynamic sections. */
4155 static boolean
4156 elf32_hppa_finish_dynamic_sections (output_bfd, info)
4157 bfd *output_bfd;
4158 struct bfd_link_info *info;
4160 bfd *dynobj;
4161 struct elf32_hppa_link_hash_table *hplink;
4162 asection *sdyn;
4164 hplink = hppa_link_hash_table (info);
4165 dynobj = hplink->root.dynobj;
4167 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4169 if (hplink->root.dynamic_sections_created)
4171 Elf32_External_Dyn *dyncon, *dynconend;
4173 BFD_ASSERT (sdyn != NULL);
4175 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4176 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4177 for (; dyncon < dynconend; dyncon++)
4179 Elf_Internal_Dyn dyn;
4180 asection *s;
4182 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4184 switch (dyn.d_tag)
4186 default:
4187 break;
4189 case DT_PLTGOT:
4190 /* Use PLTGOT to set the GOT register. */
4191 dyn.d_un.d_ptr = elf_gp (output_bfd);
4192 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4193 break;
4195 case DT_JMPREL:
4196 s = hplink->srelplt;
4197 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4198 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4199 break;
4201 case DT_PLTRELSZ:
4202 s = hplink->srelplt;
4203 if (s->_cooked_size != 0)
4204 dyn.d_un.d_val = s->_cooked_size;
4205 else
4206 dyn.d_un.d_val = s->_raw_size;
4207 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4208 break;
4213 if (hplink->sgot->_raw_size != 0)
4215 /* Fill in the first entry in the global offset table.
4216 We use it to point to our dynamic section, if we have one. */
4217 bfd_put_32 (output_bfd,
4218 (sdyn != NULL
4219 ? sdyn->output_section->vma + sdyn->output_offset
4220 : (bfd_vma) 0),
4221 hplink->sgot->contents);
4223 /* The second entry is reserved for use by the dynamic linker. */
4224 memset (hplink->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4226 /* Set .got entry size. */
4227 elf_section_data (hplink->sgot->output_section)
4228 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4231 if (hplink->splt->_raw_size != 0)
4233 /* Set plt entry size. */
4234 elf_section_data (hplink->splt->output_section)
4235 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4237 if (hplink->need_plt_stub)
4239 /* Set up the .plt stub. */
4240 memcpy (hplink->splt->contents
4241 + hplink->splt->_raw_size - sizeof (plt_stub),
4242 plt_stub, sizeof (plt_stub));
4244 if ((hplink->splt->output_offset
4245 + hplink->splt->output_section->vma
4246 + hplink->splt->_raw_size)
4247 != (hplink->sgot->output_offset
4248 + hplink->sgot->output_section->vma))
4250 (*_bfd_error_handler)
4251 (_(".got section not immediately after .plt section"));
4252 return false;
4257 return true;
4260 /* Tweak the OSABI field of the elf header. */
4262 static void
4263 elf32_hppa_post_process_headers (abfd, link_info)
4264 bfd *abfd;
4265 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
4267 Elf_Internal_Ehdr * i_ehdrp;
4269 i_ehdrp = elf_elfheader (abfd);
4271 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
4273 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
4275 else
4277 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4281 /* Called when writing out an object file to decide the type of a
4282 symbol. */
4283 static int
4284 elf32_hppa_elf_get_symbol_type (elf_sym, type)
4285 Elf_Internal_Sym *elf_sym;
4286 int type;
4288 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4289 return STT_PARISC_MILLI;
4290 else
4291 return type;
4294 /* Misc BFD support code. */
4295 #define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4296 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4297 #define elf_info_to_howto elf_hppa_info_to_howto
4298 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
4300 /* Stuff for the BFD linker. */
4301 #define bfd_elf32_bfd_final_link elf32_hppa_final_link
4302 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4303 #define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
4304 #define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
4305 #define elf_backend_check_relocs elf32_hppa_check_relocs
4306 #define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4307 #define elf_backend_fake_sections elf_hppa_fake_sections
4308 #define elf_backend_relocate_section elf32_hppa_relocate_section
4309 #define elf_backend_hide_symbol elf32_hppa_hide_symbol
4310 #define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4311 #define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4312 #define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4313 #define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4314 #define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
4315 #define elf_backend_object_p elf32_hppa_object_p
4316 #define elf_backend_final_write_processing elf_hppa_final_write_processing
4317 #define elf_backend_post_process_headers elf32_hppa_post_process_headers
4318 #define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
4320 #define elf_backend_can_gc_sections 1
4321 #define elf_backend_plt_alignment 2
4322 #define elf_backend_want_got_plt 0
4323 #define elf_backend_plt_readonly 0
4324 #define elf_backend_want_plt_sym 0
4325 #define elf_backend_got_header_size 8
4327 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
4328 #define TARGET_BIG_NAME "elf32-hppa"
4329 #define ELF_ARCH bfd_arch_hppa
4330 #define ELF_MACHINE_CODE EM_PARISC
4331 #define ELF_MAXPAGESIZE 0x1000
4333 #include "elf32-target.h"
4335 #undef TARGET_BIG_SYM
4336 #define TARGET_BIG_SYM bfd_elf32_hppa_linux_vec
4337 #undef TARGET_BIG_NAME
4338 #define TARGET_BIG_NAME "elf32-hppa-linux"
4340 #define INCLUDED_TARGET_FILE 1
4341 #include "elf32-target.h"