Set EF_ARM_HASENTRY if the start address is set.
[binutils.git] / bfd / elf32-hppa.c
blob02242154529ed0fd09cfb03d23727258fe5dc47a
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 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1867 h->dynindx = -1;
1868 if (! ((struct elf32_hppa_link_hash_entry *) h)->plabel)
1870 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1871 h->plt.offset = (bfd_vma) -1;
1875 /* Adjust a symbol defined by a dynamic object and referenced by a
1876 regular object. The current definition is in some section of the
1877 dynamic object, but we're not including those sections. We have to
1878 change the definition to something the rest of the link can
1879 understand. */
1881 static boolean
1882 elf32_hppa_adjust_dynamic_symbol (info, h)
1883 struct bfd_link_info *info;
1884 struct elf_link_hash_entry *h;
1886 bfd *dynobj;
1887 struct elf32_hppa_link_hash_table *hplink;
1888 asection *s;
1890 hplink = hppa_link_hash_table (info);
1891 dynobj = hplink->root.dynobj;
1893 /* If this is a function, put it in the procedure linkage table. We
1894 will fill in the contents of the procedure linkage table later,
1895 when we know the address of the .got section. */
1896 if (h->type == STT_FUNC
1897 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1899 if (!info->shared
1900 && h->plt.refcount > 0
1901 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1902 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0)
1904 ((struct elf32_hppa_link_hash_entry *) h)->maybe_pic_call = 1;
1907 if (h->plt.refcount <= 0
1908 || ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1909 && h->root.type != bfd_link_hash_defweak
1910 && ! ((struct elf32_hppa_link_hash_entry *) h)->plabel
1911 && (!info->shared || info->symbolic)))
1913 /* The .plt entry is not needed when:
1914 a) Garbage collection has removed all references to the
1915 symbol, or
1916 b) We know for certain the symbol is defined in this
1917 object, and it's not a weak definition, nor is the symbol
1918 used by a plabel relocation. Either this object is the
1919 application or we are doing a shared symbolic link. */
1921 /* As a special sop to the hppa ABI, we keep a .plt entry
1922 for functions in sections containing PIC code. */
1923 if (((struct elf32_hppa_link_hash_entry *) h)->maybe_pic_call)
1924 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
1925 else
1927 h->plt.offset = (bfd_vma) -1;
1928 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1929 return true;
1933 /* Make an entry in the .plt section. */
1934 s = hplink->splt;
1935 h->plt.offset = s->_raw_size;
1936 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
1937 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
1938 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1940 /* Add some extra space for the dynamic linker to use. */
1941 s->_raw_size += PLABEL_PLT_ENTRY_SIZE;
1943 else
1944 s->_raw_size += PLT_ENTRY_SIZE;
1946 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
1948 /* Make sure this symbol is output as a dynamic symbol. */
1949 if (h->dynindx == -1
1950 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1952 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1953 return false;
1956 /* We also need to make an entry in the .rela.plt section. */
1957 s = hplink->srelplt;
1958 s->_raw_size += sizeof (Elf32_External_Rela);
1960 hplink->need_plt_stub = 1;
1962 return true;
1965 /* If this is a weak symbol, and there is a real definition, the
1966 processor independent code will have arranged for us to see the
1967 real definition first, and we can just use the same value. */
1968 if (h->weakdef != NULL)
1970 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1971 || h->weakdef->root.type == bfd_link_hash_defweak);
1972 h->root.u.def.section = h->weakdef->root.u.def.section;
1973 h->root.u.def.value = h->weakdef->root.u.def.value;
1974 return true;
1977 /* This is a reference to a symbol defined by a dynamic object which
1978 is not a function. */
1980 /* If we are creating a shared library, we must presume that the
1981 only references to the symbol are via the global offset table.
1982 For such cases we need not do anything here; the relocations will
1983 be handled correctly by relocate_section. */
1984 if (info->shared)
1985 return true;
1987 /* If there are no references to this symbol that do not use the
1988 GOT, we don't need to generate a copy reloc. */
1989 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1990 return true;
1992 /* We must allocate the symbol in our .dynbss section, which will
1993 become part of the .bss section of the executable. There will be
1994 an entry for this symbol in the .dynsym section. The dynamic
1995 object will contain position independent code, so all references
1996 from the dynamic object to this symbol will go through the global
1997 offset table. The dynamic linker will use the .dynsym entry to
1998 determine the address it must put in the global offset table, so
1999 both the dynamic object and the regular object will refer to the
2000 same memory location for the variable. */
2002 s = hplink->sdynbss;
2004 /* We must generate a COPY reloc to tell the dynamic linker to
2005 copy the initial value out of the dynamic object and into the
2006 runtime process image. We need to remember the offset into the
2007 .rela.bss section we are going to use. */
2008 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2010 asection *srel;
2012 srel = hplink->srelbss;
2013 srel->_raw_size += sizeof (Elf32_External_Rela);
2014 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2018 /* We need to figure out the alignment required for this symbol. I
2019 have no idea how other ELF linkers handle this. */
2020 unsigned int power_of_two;
2022 power_of_two = bfd_log2 (h->size);
2023 if (power_of_two > 3)
2024 power_of_two = 3;
2026 /* Apply the required alignment. */
2027 s->_raw_size = BFD_ALIGN (s->_raw_size,
2028 (bfd_size_type) (1 << power_of_two));
2029 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2031 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2032 return false;
2035 /* Define the symbol as being at this point in the section. */
2036 h->root.u.def.section = s;
2037 h->root.u.def.value = s->_raw_size;
2039 /* Increment the section size to make room for the symbol. */
2040 s->_raw_size += h->size;
2042 return true;
2045 /* Called via elf_link_hash_traverse to create .plt entries for an
2046 application that uses statically linked PIC functions. Similar to
2047 the first part of elf32_hppa_adjust_dynamic_symbol. */
2049 static boolean
2050 hppa_handle_PIC_calls (h, inf)
2051 struct elf_link_hash_entry *h;
2052 PTR inf;
2054 struct bfd_link_info *info;
2055 bfd *dynobj;
2056 struct elf32_hppa_link_hash_table *hplink;
2057 asection *s;
2059 if (! (h->plt.refcount > 0
2060 && (h->root.type == bfd_link_hash_defined
2061 || h->root.type == bfd_link_hash_defweak)
2062 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0))
2064 h->plt.offset = (bfd_vma) -1;
2065 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2066 return true;
2069 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2070 ((struct elf32_hppa_link_hash_entry *) h)->maybe_pic_call = 1;
2071 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
2073 info = (struct bfd_link_info *) inf;
2074 hplink = hppa_link_hash_table (info);
2075 dynobj = hplink->root.dynobj;
2077 /* Make an entry in the .plt section. */
2078 s = hplink->splt;
2079 h->plt.offset = s->_raw_size;
2080 s->_raw_size += PLT_ENTRY_SIZE;
2082 return true;
2085 #if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
2086 || RELATIVE_DYNAMIC_RELOCS)
2087 /* This function is called via elf_link_hash_traverse to discard space
2088 we allocated for relocs that it turned out we didn't need. */
2090 static boolean
2091 hppa_discard_copies (h, inf)
2092 struct elf_link_hash_entry *h;
2093 PTR inf;
2095 struct elf32_hppa_dyn_reloc_entry *s;
2096 struct elf32_hppa_link_hash_entry *eh;
2097 struct bfd_link_info *info;
2099 eh = (struct elf32_hppa_link_hash_entry *) h;
2100 info = (struct bfd_link_info *) inf;
2102 #if ! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT
2103 /* Handle the stub reloc case. If we have a plt entry for the
2104 function, we won't be needing long branch stubs. s->count will
2105 only be zero for stub relocs, which provides a handy way of
2106 flagging these relocs, and means we need do nothing special for
2107 the forced local and symbolic link case. */
2108 if (eh->stub_reloc_sec != NULL
2109 && eh->elf.plt.offset != (bfd_vma) -1)
2111 for (s = eh->reloc_entries; s != NULL; s = s->next)
2112 if (s->count == 0)
2113 s->section->_raw_size -= sizeof (Elf32_External_Rela);
2115 #endif
2117 #if RELATIVE_DYNAMIC_RELOCS
2118 /* If a symbol has been forced local or we have found a regular
2119 definition for the symbolic link case, then we won't be needing
2120 any relocs. */
2121 if (eh->elf.dynindx == -1
2122 || ((eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2123 && !is_absolute_reloc (r_type)
2124 && info->symbolic))
2126 for (s = eh->reloc_entries; s != NULL; s = s->next)
2127 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2129 #endif
2131 return true;
2133 #endif
2135 /* This function is called via elf_link_hash_traverse to force
2136 millicode symbols local so they do not end up as globals in the
2137 dynamic symbol table. We ought to be able to do this in
2138 adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2139 for all dynamic symbols. Arguably, this is a bug in
2140 elf_adjust_dynamic_symbol. */
2142 static boolean
2143 clobber_millicode_symbols (h, info)
2144 struct elf_link_hash_entry *h;
2145 struct bfd_link_info *info;
2147 /* Note! We only want to remove these from the dynamic symbol
2148 table. Therefore we do not set ELF_LINK_FORCED_LOCAL. */
2149 if (h->type == STT_PARISC_MILLI)
2150 elf32_hppa_hide_symbol (info, h);
2151 return true;
2154 /* Set the sizes of the dynamic sections. */
2156 static boolean
2157 elf32_hppa_size_dynamic_sections (output_bfd, info)
2158 bfd *output_bfd;
2159 struct bfd_link_info *info;
2161 struct elf32_hppa_link_hash_table *hplink;
2162 bfd *dynobj;
2163 asection *s;
2164 boolean relocs;
2165 boolean reltext;
2167 hplink = hppa_link_hash_table (info);
2168 dynobj = hplink->root.dynobj;
2169 BFD_ASSERT (dynobj != NULL);
2171 if (hplink->root.dynamic_sections_created)
2173 bfd *i;
2175 /* Set the contents of the .interp section to the interpreter. */
2176 if (! info->shared)
2178 s = bfd_get_section_by_name (dynobj, ".interp");
2179 BFD_ASSERT (s != NULL);
2180 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2181 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2184 /* Force millicode symbols local. */
2185 elf_link_hash_traverse (&hplink->root,
2186 clobber_millicode_symbols,
2187 info);
2189 /* Set up .plt offsets for local plabels. */
2190 for (i = info->input_bfds; i; i = i->link_next)
2192 bfd_signed_vma *local_plt;
2193 bfd_signed_vma *end_local_plt;
2194 bfd_size_type locsymcount;
2195 Elf_Internal_Shdr *symtab_hdr;
2197 local_plt = elf_local_got_refcounts (i);
2198 if (!local_plt)
2199 continue;
2201 symtab_hdr = &elf_tdata (i)->symtab_hdr;
2202 locsymcount = symtab_hdr->sh_info;
2203 local_plt += locsymcount;
2204 end_local_plt = local_plt + locsymcount;
2206 for (; local_plt < end_local_plt; ++local_plt)
2208 if (*local_plt > 0)
2210 s = hplink->splt;
2211 *local_plt = s->_raw_size;
2212 s->_raw_size += PLT_ENTRY_SIZE;
2213 if (info->shared)
2214 hplink->srelplt->_raw_size += sizeof (Elf32_External_Rela);
2216 else
2217 *local_plt = (bfd_vma) -1;
2221 else
2223 /* Run through the function symbols, looking for any that are
2224 PIC, and allocate space for the necessary .plt entries so
2225 that %r19 will be set up. */
2226 if (! info->shared)
2227 elf_link_hash_traverse (&hplink->root,
2228 hppa_handle_PIC_calls,
2229 info);
2231 /* We may have created entries in the .rela.got section.
2232 However, if we are not creating the dynamic sections, we will
2233 not actually use these entries. Reset the size of .rela.got,
2234 which will cause it to get stripped from the output file
2235 below. */
2236 hplink->srelgot->_raw_size = 0;
2239 #if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
2240 || RELATIVE_DYNAMIC_RELOCS)
2241 /* If this is a -Bsymbolic shared link, then we need to discard all
2242 relocs against symbols defined in a regular object. We also need
2243 to lose relocs we've allocated for long branch stubs if we know
2244 we won't be generating a stub. */
2245 if (info->shared)
2246 elf_link_hash_traverse (&hplink->root,
2247 hppa_discard_copies,
2248 info);
2249 #endif
2251 /* The check_relocs and adjust_dynamic_symbol entry points have
2252 determined the sizes of the various dynamic sections. Allocate
2253 memory for them. */
2254 relocs = false;
2255 reltext = false;
2256 for (s = dynobj->sections; s != NULL; s = s->next)
2258 const char *name;
2260 if ((s->flags & SEC_LINKER_CREATED) == 0)
2261 continue;
2263 /* It's OK to base decisions on the section name, because none
2264 of the dynobj section names depend upon the input files. */
2265 name = bfd_get_section_name (dynobj, s);
2267 if (strncmp (name, ".rela", 5) == 0)
2269 if (s->_raw_size != 0)
2271 asection *target;
2272 const char *outname;
2274 /* Remember whether there are any reloc sections other
2275 than .rela.plt. */
2276 if (strcmp (name+5, ".plt") != 0)
2277 relocs = true;
2279 /* If this relocation section applies to a read only
2280 section, then we probably need a DT_TEXTREL entry. */
2281 outname = bfd_get_section_name (output_bfd,
2282 s->output_section);
2283 target = bfd_get_section_by_name (output_bfd, outname + 5);
2284 if (target != NULL
2285 && (target->flags & SEC_READONLY) != 0
2286 && (target->flags & SEC_ALLOC) != 0)
2287 reltext = true;
2289 /* We use the reloc_count field as a counter if we need
2290 to copy relocs into the output file. */
2291 s->reloc_count = 0;
2294 else if (strcmp (name, ".plt") == 0)
2296 if (hplink->need_plt_stub)
2298 /* Make space for the plt stub at the end of the .plt
2299 section. We want this stub right at the end, up
2300 against the .got section. */
2301 int gotalign = bfd_section_alignment (dynobj, hplink->sgot);
2302 int pltalign = bfd_section_alignment (dynobj, s);
2303 bfd_size_type mask;
2305 if (gotalign > pltalign)
2306 bfd_set_section_alignment (dynobj, s, gotalign);
2307 mask = ((bfd_size_type) 1 << gotalign) - 1;
2308 s->_raw_size = (s->_raw_size + sizeof (plt_stub) + mask) & ~mask;
2311 else if (strcmp (name, ".got") == 0)
2313 else
2315 /* It's not one of our sections, so don't allocate space. */
2316 continue;
2319 if (s->_raw_size == 0)
2321 /* If we don't need this section, strip it from the
2322 output file. This is mostly to handle .rela.bss and
2323 .rela.plt. We must create both sections in
2324 create_dynamic_sections, because they must be created
2325 before the linker maps input sections to output
2326 sections. The linker does that before
2327 adjust_dynamic_symbol is called, and it is that
2328 function which decides whether anything needs to go
2329 into these sections. */
2330 _bfd_strip_section_from_output (info, s);
2331 continue;
2334 /* Allocate memory for the section contents. Zero it, because
2335 we may not fill in all the reloc sections. */
2336 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2337 if (s->contents == NULL && s->_raw_size != 0)
2338 return false;
2341 if (hplink->root.dynamic_sections_created)
2343 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2344 actually has nothing to do with the PLT, it is how we
2345 communicate the LTP value of a load module to the dynamic
2346 linker. */
2347 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
2348 return false;
2350 /* Add some entries to the .dynamic section. We fill in the
2351 values later, in elf32_hppa_finish_dynamic_sections, but we
2352 must add the entries now so that we get the correct size for
2353 the .dynamic section. The DT_DEBUG entry is filled in by the
2354 dynamic linker and used by the debugger. */
2355 if (! info->shared)
2357 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2358 return false;
2361 if (hplink->srelplt->_raw_size != 0)
2363 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2364 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2365 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2366 return false;
2369 if (relocs)
2371 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2372 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2373 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2374 sizeof (Elf32_External_Rela)))
2375 return false;
2378 if (reltext)
2380 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2381 return false;
2382 info->flags |= DF_TEXTREL;
2386 return true;
2389 /* External entry points for sizing and building linker stubs. */
2391 /* Determine and set the size of the stub section for a final link.
2393 The basic idea here is to examine all the relocations looking for
2394 PC-relative calls to a target that is unreachable with a "bl"
2395 instruction. */
2397 boolean
2398 elf32_hppa_size_stubs (output_bfd, stub_bfd, info, multi_subspace, group_size,
2399 add_stub_section, layout_sections_again)
2400 bfd *output_bfd;
2401 bfd *stub_bfd;
2402 struct bfd_link_info *info;
2403 boolean multi_subspace;
2404 bfd_signed_vma group_size;
2405 asection * (*add_stub_section) PARAMS ((const char *, asection *));
2406 void (*layout_sections_again) PARAMS ((void));
2408 bfd *input_bfd;
2409 asection *section;
2410 asection **input_list, **list;
2411 Elf_Internal_Sym *local_syms, **all_local_syms;
2412 unsigned int bfd_indx, bfd_count;
2413 int top_id, top_index;
2414 struct elf32_hppa_link_hash_table *hplink;
2415 bfd_size_type stub_group_size;
2416 boolean stubs_always_before_branch;
2417 boolean stub_changed = 0;
2418 boolean ret = 0;
2420 hplink = hppa_link_hash_table (info);
2422 /* Stash our params away. */
2423 hplink->stub_bfd = stub_bfd;
2424 hplink->multi_subspace = multi_subspace;
2425 hplink->add_stub_section = add_stub_section;
2426 hplink->layout_sections_again = layout_sections_again;
2427 stubs_always_before_branch = group_size < 0;
2428 if (group_size < 0)
2429 stub_group_size = -group_size;
2430 else
2431 stub_group_size = group_size;
2432 if (stub_group_size == 1)
2434 /* Default values. */
2435 stub_group_size = 8000000;
2436 if (hplink->has_17bit_branch || hplink->multi_subspace)
2437 stub_group_size = 250000;
2438 if (hplink->has_12bit_branch)
2439 stub_group_size = 7812;
2442 /* Count the number of input BFDs and find the top input section id. */
2443 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2444 input_bfd != NULL;
2445 input_bfd = input_bfd->link_next)
2447 bfd_count += 1;
2448 for (section = input_bfd->sections;
2449 section != NULL;
2450 section = section->next)
2452 if (top_id < section->id)
2453 top_id = section->id;
2457 hplink->stub_group
2458 = (struct map_stub *) bfd_zmalloc (sizeof (struct map_stub) * (top_id + 1));
2459 if (hplink->stub_group == NULL)
2460 return false;
2462 /* Make a list of input sections for each output section included in
2463 the link.
2465 We can't use output_bfd->section_count here to find the top output
2466 section index as some sections may have been removed, and
2467 _bfd_strip_section_from_output doesn't renumber the indices. */
2468 for (section = output_bfd->sections, top_index = 0;
2469 section != NULL;
2470 section = section->next)
2472 if (top_index < section->index)
2473 top_index = section->index;
2476 input_list
2477 = (asection **) bfd_malloc (sizeof (asection *) * (top_index + 1));
2478 if (input_list == NULL)
2479 return false;
2481 /* For sections we aren't interested in, mark their entries with a
2482 value we can check later. */
2483 list = input_list + top_index;
2485 *list = bfd_abs_section_ptr;
2486 while (list-- != input_list);
2488 for (section = output_bfd->sections;
2489 section != NULL;
2490 section = section->next)
2492 if ((section->flags & SEC_CODE) != 0)
2493 input_list[section->index] = NULL;
2496 /* Now actually build the lists. */
2497 for (input_bfd = info->input_bfds;
2498 input_bfd != NULL;
2499 input_bfd = input_bfd->link_next)
2501 for (section = input_bfd->sections;
2502 section != NULL;
2503 section = section->next)
2505 if (section->output_section != NULL
2506 && section->output_section->owner == output_bfd
2507 && section->output_section->index <= top_index)
2509 list = input_list + section->output_section->index;
2510 if (*list != bfd_abs_section_ptr)
2512 /* Steal the link_sec pointer for our list. */
2513 #define PREV_SEC(sec) (hplink->stub_group[(sec)->id].link_sec)
2514 /* This happens to make the list in reverse order,
2515 which is what we want. */
2516 PREV_SEC (section) = *list;
2517 *list = section;
2523 /* See whether we can group stub sections together. Grouping stub
2524 sections may result in fewer stubs. More importantly, we need to
2525 put all .init* and .fini* stubs at the beginning of the .init or
2526 .fini output sections respectively, because glibc splits the
2527 _init and _fini functions into multiple parts. Putting a stub in
2528 the middle of a function is not a good idea. */
2529 list = input_list + top_index;
2532 asection *tail = *list;
2533 if (tail == bfd_abs_section_ptr)
2534 continue;
2535 while (tail != NULL)
2537 asection *curr;
2538 asection *prev;
2539 bfd_size_type total;
2541 curr = tail;
2542 if (tail->_cooked_size)
2543 total = tail->_cooked_size;
2544 else
2545 total = tail->_raw_size;
2546 while ((prev = PREV_SEC (curr)) != NULL
2547 && ((total += curr->output_offset - prev->output_offset)
2548 < stub_group_size))
2549 curr = prev;
2551 /* OK, the size from the start of CURR to the end is less
2552 than 250000 bytes and thus can be handled by one stub
2553 section. (or the tail section is itself larger than
2554 250000 bytes, in which case we may be toast.)
2555 We should really be keeping track of the total size of
2556 stubs added here, as stubs contribute to the final output
2557 section size. That's a little tricky, and this way will
2558 only break if stubs added total more than 12144 bytes, or
2559 1518 long branch stubs. It seems unlikely for more than
2560 1518 different functions to be called, especially from
2561 code only 250000 bytes long. */
2564 prev = PREV_SEC (tail);
2565 /* Set up this stub group. */
2566 hplink->stub_group[tail->id].link_sec = curr;
2568 while (tail != curr && (tail = prev) != NULL);
2570 /* But wait, there's more! Input sections up to 250000
2571 bytes before the stub section can be handled by it too. */
2572 if (!stubs_always_before_branch)
2574 total = 0;
2575 while (prev != NULL
2576 && ((total += tail->output_offset - prev->output_offset)
2577 < stub_group_size))
2579 tail = prev;
2580 prev = PREV_SEC (tail);
2581 hplink->stub_group[tail->id].link_sec = curr;
2584 tail = prev;
2587 while (list-- != input_list);
2588 free (input_list);
2589 #undef PREV_SEC
2591 /* We want to read in symbol extension records only once. To do this
2592 we need to read in the local symbols in parallel and save them for
2593 later use; so hold pointers to the local symbols in an array. */
2594 all_local_syms
2595 = (Elf_Internal_Sym **) bfd_zmalloc (sizeof (Elf_Internal_Sym *)
2596 * bfd_count);
2597 if (all_local_syms == NULL)
2598 return false;
2600 /* Walk over all the input BFDs, swapping in local symbols.
2601 If we are creating a shared library, create hash entries for the
2602 export stubs. */
2603 for (input_bfd = info->input_bfds, bfd_indx = 0;
2604 input_bfd != NULL;
2605 input_bfd = input_bfd->link_next, bfd_indx++)
2607 Elf_Internal_Shdr *symtab_hdr;
2608 Elf_Internal_Sym *isym;
2609 Elf32_External_Sym *ext_syms, *esym, *end_sy;
2611 /* We'll need the symbol table in a second. */
2612 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2613 if (symtab_hdr->sh_info == 0)
2614 continue;
2616 /* We need an array of the local symbols attached to the input bfd.
2617 Unfortunately, we're going to have to read & swap them in. */
2618 local_syms = (Elf_Internal_Sym *)
2619 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf_Internal_Sym));
2620 if (local_syms == NULL)
2622 goto error_ret_free_local;
2624 all_local_syms[bfd_indx] = local_syms;
2625 ext_syms = (Elf32_External_Sym *)
2626 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf32_External_Sym));
2627 if (ext_syms == NULL)
2629 goto error_ret_free_local;
2632 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2633 || (bfd_read (ext_syms, 1,
2634 (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)),
2635 input_bfd)
2636 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2638 free (ext_syms);
2639 goto error_ret_free_local;
2642 /* Swap the local symbols in. */
2643 isym = local_syms;
2644 esym = ext_syms;
2645 for (end_sy = esym + symtab_hdr->sh_info; esym < end_sy; esym++, isym++)
2646 bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
2648 /* Now we can free the external symbols. */
2649 free (ext_syms);
2651 #if ! LONG_BRANCH_PIC_IN_SHLIB
2652 /* If this is a shared link, find all the stub reloc sections. */
2653 if (info->shared)
2654 for (section = input_bfd->sections;
2655 section != NULL;
2656 section = section->next)
2658 char *name;
2659 asection *reloc_sec;
2661 name = bfd_malloc (strlen (section->name)
2662 + sizeof STUB_SUFFIX
2663 + 5);
2664 if (name == NULL)
2665 return false;
2666 sprintf (name, ".rela%s%s", section->name, STUB_SUFFIX);
2667 reloc_sec = bfd_get_section_by_name (hplink->root.dynobj, name);
2668 hplink->stub_group[section->id].reloc_sec = reloc_sec;
2669 free (name);
2671 #endif
2673 if (info->shared && hplink->multi_subspace)
2675 struct elf_link_hash_entry **sym_hashes;
2676 struct elf_link_hash_entry **end_hashes;
2677 unsigned int symcount;
2679 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2680 - symtab_hdr->sh_info);
2681 sym_hashes = elf_sym_hashes (input_bfd);
2682 end_hashes = sym_hashes + symcount;
2684 /* Look through the global syms for functions; We need to
2685 build export stubs for all globally visible functions. */
2686 for (; sym_hashes < end_hashes; sym_hashes++)
2688 struct elf32_hppa_link_hash_entry *hash;
2690 hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
2692 while (hash->elf.root.type == bfd_link_hash_indirect
2693 || hash->elf.root.type == bfd_link_hash_warning)
2694 hash = ((struct elf32_hppa_link_hash_entry *)
2695 hash->elf.root.u.i.link);
2697 /* At this point in the link, undefined syms have been
2698 resolved, so we need to check that the symbol was
2699 defined in this BFD. */
2700 if ((hash->elf.root.type == bfd_link_hash_defined
2701 || hash->elf.root.type == bfd_link_hash_defweak)
2702 && hash->elf.type == STT_FUNC
2703 && hash->elf.root.u.def.section->output_section != NULL
2704 && (hash->elf.root.u.def.section->output_section->owner
2705 == output_bfd)
2706 && hash->elf.root.u.def.section->owner == input_bfd
2707 && (hash->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
2708 && !(hash->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2709 && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2711 asection *sec;
2712 const char *stub_name;
2713 struct elf32_hppa_stub_hash_entry *stub_entry;
2715 sec = hash->elf.root.u.def.section;
2716 stub_name = hash->elf.root.root.string;
2717 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2718 stub_name,
2719 false, false);
2720 if (stub_entry == NULL)
2722 stub_entry = hppa_add_stub (stub_name, sec, hplink);
2723 if (!stub_entry)
2724 goto error_ret_free_local;
2726 stub_entry->target_value = hash->elf.root.u.def.value;
2727 stub_entry->target_section = hash->elf.root.u.def.section;
2728 stub_entry->stub_type = hppa_stub_export;
2729 stub_entry->h = hash;
2730 stub_changed = 1;
2732 else
2734 (*_bfd_error_handler) (_("%s: duplicate export stub %s"),
2735 bfd_get_filename (input_bfd),
2736 stub_name);
2743 while (1)
2745 asection *stub_sec;
2747 for (input_bfd = info->input_bfds, bfd_indx = 0;
2748 input_bfd != NULL;
2749 input_bfd = input_bfd->link_next, bfd_indx++)
2751 Elf_Internal_Shdr *symtab_hdr;
2753 /* We'll need the symbol table in a second. */
2754 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2755 if (symtab_hdr->sh_info == 0)
2756 continue;
2758 local_syms = all_local_syms[bfd_indx];
2760 /* Walk over each section attached to the input bfd. */
2761 for (section = input_bfd->sections;
2762 section != NULL;
2763 section = section->next)
2765 Elf_Internal_Shdr *input_rel_hdr;
2766 Elf32_External_Rela *external_relocs, *erelaend, *erela;
2767 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2769 /* If there aren't any relocs, then there's nothing more
2770 to do. */
2771 if ((section->flags & SEC_RELOC) == 0
2772 || section->reloc_count == 0)
2773 continue;
2775 /* If this section is a link-once section that will be
2776 discarded, then don't create any stubs. */
2777 if (section->output_section == NULL
2778 || section->output_section->owner != output_bfd)
2779 continue;
2781 /* Allocate space for the external relocations. */
2782 external_relocs
2783 = ((Elf32_External_Rela *)
2784 bfd_malloc (section->reloc_count
2785 * sizeof (Elf32_External_Rela)));
2786 if (external_relocs == NULL)
2788 goto error_ret_free_local;
2791 /* Likewise for the internal relocations. */
2792 internal_relocs = ((Elf_Internal_Rela *)
2793 bfd_malloc (section->reloc_count
2794 * sizeof (Elf_Internal_Rela)));
2795 if (internal_relocs == NULL)
2797 free (external_relocs);
2798 goto error_ret_free_local;
2801 /* Read in the external relocs. */
2802 input_rel_hdr = &elf_section_data (section)->rel_hdr;
2803 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
2804 || bfd_read (external_relocs, 1,
2805 input_rel_hdr->sh_size,
2806 input_bfd) != input_rel_hdr->sh_size)
2808 free (external_relocs);
2809 error_ret_free_internal:
2810 free (internal_relocs);
2811 goto error_ret_free_local;
2814 /* Swap in the relocs. */
2815 erela = external_relocs;
2816 erelaend = erela + section->reloc_count;
2817 irela = internal_relocs;
2818 for (; erela < erelaend; erela++, irela++)
2819 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
2821 /* We're done with the external relocs, free them. */
2822 free (external_relocs);
2824 /* Now examine each relocation. */
2825 irela = internal_relocs;
2826 irelaend = irela + section->reloc_count;
2827 for (; irela < irelaend; irela++)
2829 unsigned int r_type, r_indx;
2830 enum elf32_hppa_stub_type stub_type;
2831 struct elf32_hppa_stub_hash_entry *stub_entry;
2832 asection *sym_sec;
2833 bfd_vma sym_value;
2834 bfd_vma destination;
2835 struct elf32_hppa_link_hash_entry *hash;
2836 char *stub_name;
2837 const asection *id_sec;
2839 r_type = ELF32_R_TYPE (irela->r_info);
2840 r_indx = ELF32_R_SYM (irela->r_info);
2842 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2844 bfd_set_error (bfd_error_bad_value);
2845 goto error_ret_free_internal;
2848 /* Only look for stubs on call instructions. */
2849 if (r_type != (unsigned int) R_PARISC_PCREL12F
2850 && r_type != (unsigned int) R_PARISC_PCREL17F
2851 && r_type != (unsigned int) R_PARISC_PCREL22F)
2852 continue;
2854 /* Now determine the call target, its name, value,
2855 section. */
2856 sym_sec = NULL;
2857 sym_value = 0;
2858 destination = 0;
2859 hash = NULL;
2860 if (r_indx < symtab_hdr->sh_info)
2862 /* It's a local symbol. */
2863 Elf_Internal_Sym *sym;
2864 Elf_Internal_Shdr *hdr;
2866 sym = local_syms + r_indx;
2867 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2868 sym_sec = hdr->bfd_section;
2869 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2870 sym_value = sym->st_value;
2871 destination = (sym_value + irela->r_addend
2872 + sym_sec->output_offset
2873 + sym_sec->output_section->vma);
2875 else
2877 /* It's an external symbol. */
2878 int e_indx;
2880 e_indx = r_indx - symtab_hdr->sh_info;
2881 hash = ((struct elf32_hppa_link_hash_entry *)
2882 elf_sym_hashes (input_bfd)[e_indx]);
2884 while (hash->elf.root.type == bfd_link_hash_indirect
2885 || hash->elf.root.type == bfd_link_hash_warning)
2886 hash = ((struct elf32_hppa_link_hash_entry *)
2887 hash->elf.root.u.i.link);
2889 if (hash->elf.root.type == bfd_link_hash_defined
2890 || hash->elf.root.type == bfd_link_hash_defweak)
2892 sym_sec = hash->elf.root.u.def.section;
2893 sym_value = hash->elf.root.u.def.value;
2894 if (sym_sec->output_section != NULL)
2895 destination = (sym_value + irela->r_addend
2896 + sym_sec->output_offset
2897 + sym_sec->output_section->vma);
2899 else if (hash->elf.root.type == bfd_link_hash_undefweak)
2901 if (! info->shared)
2902 continue;
2904 else if (hash->elf.root.type == bfd_link_hash_undefined)
2906 if (! (info->shared
2907 && !info->no_undefined
2908 && (ELF_ST_VISIBILITY (hash->elf.other)
2909 == STV_DEFAULT)))
2910 continue;
2912 else
2914 bfd_set_error (bfd_error_bad_value);
2915 goto error_ret_free_internal;
2919 /* Determine what (if any) linker stub is needed. */
2920 stub_type = hppa_type_of_stub (section, irela, hash,
2921 destination);
2922 if (stub_type == hppa_stub_none)
2923 continue;
2925 /* Support for grouping stub sections. */
2926 id_sec = hplink->stub_group[section->id].link_sec;
2928 /* Get the name of this stub. */
2929 stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
2930 if (!stub_name)
2931 goto error_ret_free_internal;
2933 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2934 stub_name,
2935 false, false);
2936 if (stub_entry != NULL)
2938 /* The proper stub has already been created. */
2939 free (stub_name);
2940 continue;
2943 stub_entry = hppa_add_stub (stub_name, section, hplink);
2944 if (stub_entry == NULL)
2946 free (stub_name);
2947 goto error_ret_free_local;
2950 stub_entry->target_value = sym_value;
2951 stub_entry->target_section = sym_sec;
2952 stub_entry->stub_type = stub_type;
2953 if (info->shared)
2955 if (stub_type == hppa_stub_import)
2956 stub_entry->stub_type = hppa_stub_import_shared;
2957 else if (stub_type == hppa_stub_long_branch
2958 && (LONG_BRANCH_PIC_IN_SHLIB || hash == NULL))
2959 stub_entry->stub_type = hppa_stub_long_branch_shared;
2961 stub_entry->h = hash;
2962 stub_changed = 1;
2965 /* We're done with the internal relocs, free them. */
2966 free (internal_relocs);
2970 if (!stub_changed)
2971 break;
2973 /* OK, we've added some stubs. Find out the new size of the
2974 stub sections. */
2975 for (stub_sec = hplink->stub_bfd->sections;
2976 stub_sec != NULL;
2977 stub_sec = stub_sec->next)
2979 stub_sec->_raw_size = 0;
2980 stub_sec->_cooked_size = 0;
2982 #if ! LONG_BRANCH_PIC_IN_SHLIB
2984 int i;
2986 for (i = top_id; i >= 0; --i)
2988 /* This will probably hit the same section many times.. */
2989 stub_sec = hplink->stub_group[i].reloc_sec;
2990 if (stub_sec != NULL)
2992 stub_sec->_raw_size = 0;
2993 stub_sec->_cooked_size = 0;
2997 #endif
2999 bfd_hash_traverse (&hplink->stub_hash_table,
3000 hppa_size_one_stub,
3001 hplink);
3003 /* Ask the linker to do its stuff. */
3004 (*hplink->layout_sections_again) ();
3005 stub_changed = 0;
3008 ret = 1;
3010 error_ret_free_local:
3011 while (bfd_count-- > 0)
3012 if (all_local_syms[bfd_count])
3013 free (all_local_syms[bfd_count]);
3014 free (all_local_syms);
3016 return ret;
3019 /* For a final link, this function is called after we have sized the
3020 stubs to provide a value for __gp. */
3022 boolean
3023 elf32_hppa_set_gp (abfd, info)
3024 bfd *abfd;
3025 struct bfd_link_info *info;
3027 struct elf32_hppa_link_hash_table *hplink;
3028 struct elf_link_hash_entry *h;
3029 asection *sec;
3030 bfd_vma gp_val;
3032 hplink = hppa_link_hash_table (info);
3033 h = elf_link_hash_lookup (&hplink->root, "$global$",
3034 false, false, false);
3036 if (h != NULL
3037 && (h->root.type == bfd_link_hash_defined
3038 || h->root.type == bfd_link_hash_defweak))
3040 gp_val = h->root.u.def.value;
3041 sec = h->root.u.def.section;
3043 else
3045 /* Choose to point our LTP at, in this order, one of .plt, .got,
3046 or .data, if these sections exist. In the case of choosing
3047 .plt try to make the LTP ideal for addressing anywhere in the
3048 .plt or .got with a 14 bit signed offset. Typically, the end
3049 of the .plt is the start of the .got, so choose .plt + 0x2000
3050 if either the .plt or .got is larger than 0x2000. If both
3051 the .plt and .got are smaller than 0x2000, choose the end of
3052 the .plt section. */
3054 sec = hplink->splt;
3055 if (sec != NULL)
3057 gp_val = sec->_raw_size;
3058 if (gp_val > 0x2000
3059 || (hplink->sgot && hplink->sgot->_raw_size > 0x2000))
3061 gp_val = 0x2000;
3064 else
3066 gp_val = 0;
3067 sec = hplink->sgot;
3068 if (sec != NULL)
3070 /* We know we don't have a .plt. If .got is large,
3071 offset our LTP. */
3072 if (sec->_raw_size > 0x2000)
3073 gp_val = 0x2000;
3075 else
3077 /* No .plt or .got. Who cares what the LTP is? */
3078 sec = bfd_get_section_by_name (abfd, ".data");
3082 if (h != NULL)
3084 h->root.type = bfd_link_hash_defined;
3085 h->root.u.def.value = gp_val;
3086 if (sec != NULL)
3087 h->root.u.def.section = sec;
3088 else
3089 h->root.u.def.section = bfd_abs_section_ptr;
3093 if (sec != NULL && sec->output_section != NULL)
3094 gp_val += sec->output_section->vma + sec->output_offset;
3096 elf_gp (abfd) = gp_val;
3097 return true;
3100 /* Build all the stubs associated with the current output file. The
3101 stubs are kept in a hash table attached to the main linker hash
3102 table. We also set up the .plt entries for statically linked PIC
3103 functions here. This function is called via hppaelf_finish in the
3104 linker. */
3106 boolean
3107 elf32_hppa_build_stubs (info)
3108 struct bfd_link_info *info;
3110 asection *stub_sec;
3111 struct bfd_hash_table *table;
3112 struct elf32_hppa_link_hash_table *hplink;
3114 hplink = hppa_link_hash_table (info);
3116 for (stub_sec = hplink->stub_bfd->sections;
3117 stub_sec != NULL;
3118 stub_sec = stub_sec->next)
3120 size_t size;
3122 /* Allocate memory to hold the linker stubs. */
3123 size = stub_sec->_raw_size;
3124 stub_sec->contents = (unsigned char *) bfd_zalloc (hplink->stub_bfd,
3125 size);
3126 if (stub_sec->contents == NULL && size != 0)
3127 return false;
3128 stub_sec->_raw_size = 0;
3131 /* Build the stubs as directed by the stub hash table. */
3132 table = &hplink->stub_hash_table;
3133 bfd_hash_traverse (table, hppa_build_one_stub, info);
3135 return true;
3138 /* Perform a final link. */
3140 static boolean
3141 elf32_hppa_final_link (abfd, info)
3142 bfd *abfd;
3143 struct bfd_link_info *info;
3145 asection *s;
3147 /* Invoke the regular ELF garbage collecting linker to do all the
3148 work. */
3149 if (!_bfd_elf32_gc_common_final_link (abfd, info))
3150 return false;
3152 /* If we're producing a final executable, sort the contents of the
3153 unwind section. Magic section names, but this is much safer than
3154 having elf32_hppa_relocate_section remember where SEGREL32 relocs
3155 occurred. Consider what happens if someone inept creates a
3156 linker script that puts unwind information in .text. */
3157 s = bfd_get_section_by_name (abfd, ".PARISC.unwind");
3158 if (s != NULL)
3160 bfd_size_type size;
3161 char *contents;
3163 size = s->_raw_size;
3164 contents = bfd_malloc (size);
3165 if (contents == NULL)
3166 return false;
3168 if (! bfd_get_section_contents (abfd, s, contents, (file_ptr) 0, size))
3169 return false;
3171 qsort (contents, size / 16, 16, hppa_unwind_entry_compare);
3173 if (! bfd_set_section_contents (abfd, s, contents, (file_ptr) 0, size))
3174 return false;
3176 return true;
3179 /* Record the lowest address for the data and text segments. */
3181 static void
3182 hppa_record_segment_addr (abfd, section, data)
3183 bfd *abfd ATTRIBUTE_UNUSED;
3184 asection *section;
3185 PTR data;
3187 struct elf32_hppa_link_hash_table *hplink;
3189 hplink = (struct elf32_hppa_link_hash_table *) data;
3191 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3193 bfd_vma value = section->vma - section->filepos;
3195 if ((section->flags & SEC_READONLY) != 0)
3197 if (value < hplink->text_segment_base)
3198 hplink->text_segment_base = value;
3200 else
3202 if (value < hplink->data_segment_base)
3203 hplink->data_segment_base = value;
3208 /* Perform a relocation as part of a final link. */
3210 static bfd_reloc_status_type
3211 final_link_relocate (input_section, contents, rel, value, hplink, sym_sec, h)
3212 asection *input_section;
3213 bfd_byte *contents;
3214 const Elf_Internal_Rela *rel;
3215 bfd_vma value;
3216 struct elf32_hppa_link_hash_table *hplink;
3217 asection *sym_sec;
3218 struct elf32_hppa_link_hash_entry *h;
3220 int insn;
3221 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3222 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3223 int r_format = howto->bitsize;
3224 enum hppa_reloc_field_selector_type_alt r_field;
3225 bfd *input_bfd = input_section->owner;
3226 bfd_vma offset = rel->r_offset;
3227 bfd_vma max_branch_offset = 0;
3228 bfd_byte *hit_data = contents + offset;
3229 bfd_signed_vma addend = rel->r_addend;
3230 bfd_vma location;
3231 struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
3232 int val;
3234 if (r_type == R_PARISC_NONE)
3235 return bfd_reloc_ok;
3237 insn = bfd_get_32 (input_bfd, hit_data);
3239 /* Find out where we are and where we're going. */
3240 location = (offset +
3241 input_section->output_offset +
3242 input_section->output_section->vma);
3244 switch (r_type)
3246 case R_PARISC_PCREL12F:
3247 case R_PARISC_PCREL17F:
3248 case R_PARISC_PCREL22F:
3249 /* If this is a call to a function defined in another dynamic
3250 library, or if it is a call to a PIC function in the same
3251 object, or if this is a shared link and it is a call to a
3252 weak symbol which may or may not be in the same object, then
3253 find the import stub in the stub hash. */
3254 if (sym_sec == NULL
3255 || sym_sec->output_section == NULL
3256 || (h != NULL
3257 && ((h->maybe_pic_call
3258 && !(input_section->flags & SEC_HAS_GOT_REF))
3259 || (h->elf.root.type == bfd_link_hash_defweak
3260 && h->elf.dynindx != -1
3261 && h->elf.plt.offset != (bfd_vma) -1))))
3263 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3264 h, rel, hplink);
3265 if (stub_entry != NULL)
3267 value = (stub_entry->stub_offset
3268 + stub_entry->stub_sec->output_offset
3269 + stub_entry->stub_sec->output_section->vma);
3270 addend = 0;
3272 else if (sym_sec == NULL && h != NULL
3273 && h->elf.root.type == bfd_link_hash_undefweak)
3275 /* It's OK if undefined weak. Calls to undefined weak
3276 symbols behave as if the "called" function
3277 immediately returns. We can thus call to a weak
3278 function without first checking whether the function
3279 is defined. */
3280 value = location;
3281 addend = 8;
3283 else
3284 return bfd_reloc_notsupported;
3286 /* Fall thru. */
3288 case R_PARISC_PCREL21L:
3289 case R_PARISC_PCREL17C:
3290 case R_PARISC_PCREL17R:
3291 case R_PARISC_PCREL14R:
3292 case R_PARISC_PCREL14F:
3293 /* Make it a pc relative offset. */
3294 value -= location;
3295 addend -= 8;
3296 break;
3298 case R_PARISC_DPREL21L:
3299 case R_PARISC_DPREL14R:
3300 case R_PARISC_DPREL14F:
3301 /* For all the DP relative relocations, we need to examine the symbol's
3302 section. If it's a code section, then "data pointer relative" makes
3303 no sense. In that case we don't adjust the "value", and for 21 bit
3304 addil instructions, we change the source addend register from %dp to
3305 %r0. This situation commonly arises when a variable's "constness"
3306 is declared differently from the way the variable is defined. For
3307 instance: "extern int foo" with foo defined as "const int foo". */
3308 if (sym_sec == NULL)
3309 break;
3310 if ((sym_sec->flags & SEC_CODE) != 0)
3312 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3313 == (((int) OP_ADDIL << 26) | (27 << 21)))
3315 insn &= ~ (0x1f << 21);
3316 #if 1 /* debug them. */
3317 (*_bfd_error_handler)
3318 (_("%s(%s+0x%lx): fixing %s"),
3319 bfd_get_filename (input_bfd),
3320 input_section->name,
3321 (long) rel->r_offset,
3322 howto->name);
3323 #endif
3325 /* Now try to make things easy for the dynamic linker. */
3327 break;
3329 /* Fall thru. */
3331 case R_PARISC_DLTIND21L:
3332 case R_PARISC_DLTIND14R:
3333 case R_PARISC_DLTIND14F:
3334 value -= elf_gp (input_section->output_section->owner);
3335 break;
3337 case R_PARISC_SEGREL32:
3338 if ((sym_sec->flags & SEC_CODE) != 0)
3339 value -= hplink->text_segment_base;
3340 else
3341 value -= hplink->data_segment_base;
3342 break;
3344 default:
3345 break;
3348 switch (r_type)
3350 case R_PARISC_DIR32:
3351 case R_PARISC_DIR14F:
3352 case R_PARISC_DIR17F:
3353 case R_PARISC_PCREL17C:
3354 case R_PARISC_PCREL14F:
3355 case R_PARISC_DPREL14F:
3356 case R_PARISC_PLABEL32:
3357 case R_PARISC_DLTIND14F:
3358 case R_PARISC_SEGBASE:
3359 case R_PARISC_SEGREL32:
3360 r_field = e_fsel;
3361 break;
3363 case R_PARISC_DIR21L:
3364 case R_PARISC_PCREL21L:
3365 case R_PARISC_DPREL21L:
3366 case R_PARISC_PLABEL21L:
3367 case R_PARISC_DLTIND21L:
3368 r_field = e_lrsel;
3369 break;
3371 case R_PARISC_DIR17R:
3372 case R_PARISC_PCREL17R:
3373 case R_PARISC_DIR14R:
3374 case R_PARISC_PCREL14R:
3375 case R_PARISC_DPREL14R:
3376 case R_PARISC_PLABEL14R:
3377 case R_PARISC_DLTIND14R:
3378 r_field = e_rrsel;
3379 break;
3381 case R_PARISC_PCREL12F:
3382 case R_PARISC_PCREL17F:
3383 case R_PARISC_PCREL22F:
3384 r_field = e_fsel;
3386 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3388 max_branch_offset = (1 << (17-1)) << 2;
3390 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3392 max_branch_offset = (1 << (12-1)) << 2;
3394 else
3396 max_branch_offset = (1 << (22-1)) << 2;
3399 /* sym_sec is NULL on undefined weak syms or when shared on
3400 undefined syms. We've already checked for a stub for the
3401 shared undefined case. */
3402 if (sym_sec == NULL)
3403 break;
3405 /* If the branch is out of reach, then redirect the
3406 call to the local stub for this function. */
3407 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3409 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3410 h, rel, hplink);
3411 if (stub_entry == NULL)
3412 return bfd_reloc_notsupported;
3414 /* Munge up the value and addend so that we call the stub
3415 rather than the procedure directly. */
3416 value = (stub_entry->stub_offset
3417 + stub_entry->stub_sec->output_offset
3418 + stub_entry->stub_sec->output_section->vma
3419 - location);
3420 addend = -8;
3422 break;
3424 /* Something we don't know how to handle. */
3425 default:
3426 return bfd_reloc_notsupported;
3429 /* Make sure we can reach the stub. */
3430 if (max_branch_offset != 0
3431 && value + addend + max_branch_offset >= 2*max_branch_offset)
3433 (*_bfd_error_handler)
3434 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3435 bfd_get_filename (input_bfd),
3436 input_section->name,
3437 (long) rel->r_offset,
3438 stub_entry->root.string);
3439 return bfd_reloc_notsupported;
3442 val = hppa_field_adjust (value, addend, r_field);
3444 switch (r_type)
3446 case R_PARISC_PCREL12F:
3447 case R_PARISC_PCREL17C:
3448 case R_PARISC_PCREL17F:
3449 case R_PARISC_PCREL17R:
3450 case R_PARISC_PCREL22F:
3451 case R_PARISC_DIR17F:
3452 case R_PARISC_DIR17R:
3453 /* This is a branch. Divide the offset by four.
3454 Note that we need to decide whether it's a branch or
3455 otherwise by inspecting the reloc. Inspecting insn won't
3456 work as insn might be from a .word directive. */
3457 val >>= 2;
3458 break;
3460 default:
3461 break;
3464 insn = hppa_rebuild_insn (insn, val, r_format);
3466 /* Update the instruction word. */
3467 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3468 return bfd_reloc_ok;
3471 /* Relocate an HPPA ELF section. */
3473 static boolean
3474 elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
3475 contents, relocs, local_syms, local_sections)
3476 bfd *output_bfd;
3477 struct bfd_link_info *info;
3478 bfd *input_bfd;
3479 asection *input_section;
3480 bfd_byte *contents;
3481 Elf_Internal_Rela *relocs;
3482 Elf_Internal_Sym *local_syms;
3483 asection **local_sections;
3485 bfd *dynobj;
3486 bfd_vma *local_got_offsets;
3487 struct elf32_hppa_link_hash_table *hplink;
3488 Elf_Internal_Shdr *symtab_hdr;
3489 Elf_Internal_Rela *rel;
3490 Elf_Internal_Rela *relend;
3491 asection *sreloc;
3493 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3495 hplink = hppa_link_hash_table (info);
3496 dynobj = hplink->root.dynobj;
3497 local_got_offsets = elf_local_got_offsets (input_bfd);
3498 sreloc = NULL;
3500 rel = relocs;
3501 relend = relocs + input_section->reloc_count;
3502 for (; rel < relend; rel++)
3504 unsigned int r_type;
3505 reloc_howto_type *howto;
3506 unsigned int r_symndx;
3507 struct elf32_hppa_link_hash_entry *h;
3508 Elf_Internal_Sym *sym;
3509 asection *sym_sec;
3510 bfd_vma relocation;
3511 bfd_reloc_status_type r;
3512 const char *sym_name;
3513 boolean plabel;
3515 r_type = ELF32_R_TYPE (rel->r_info);
3516 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3518 bfd_set_error (bfd_error_bad_value);
3519 return false;
3521 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3522 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3523 continue;
3525 r_symndx = ELF32_R_SYM (rel->r_info);
3527 if (info->relocateable)
3529 /* This is a relocateable link. We don't have to change
3530 anything, unless the reloc is against a section symbol,
3531 in which case we have to adjust according to where the
3532 section symbol winds up in the output section. */
3533 if (r_symndx < symtab_hdr->sh_info)
3535 sym = local_syms + r_symndx;
3536 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3538 sym_sec = local_sections[r_symndx];
3539 rel->r_addend += sym_sec->output_offset;
3542 continue;
3545 /* This is a final link. */
3546 h = NULL;
3547 sym = NULL;
3548 sym_sec = NULL;
3549 if (r_symndx < symtab_hdr->sh_info)
3551 /* This is a local symbol, h defaults to NULL. */
3552 sym = local_syms + r_symndx;
3553 sym_sec = local_sections[r_symndx];
3554 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
3555 ? 0 : sym->st_value)
3556 + sym_sec->output_offset
3557 + sym_sec->output_section->vma);
3559 else
3561 int indx;
3563 /* It's a global; Find its entry in the link hash. */
3564 indx = r_symndx - symtab_hdr->sh_info;
3565 h = ((struct elf32_hppa_link_hash_entry *)
3566 elf_sym_hashes (input_bfd)[indx]);
3567 while (h->elf.root.type == bfd_link_hash_indirect
3568 || h->elf.root.type == bfd_link_hash_warning)
3569 h = (struct elf32_hppa_link_hash_entry *) h->elf.root.u.i.link;
3571 relocation = 0;
3572 if (h->elf.root.type == bfd_link_hash_defined
3573 || h->elf.root.type == bfd_link_hash_defweak)
3575 sym_sec = h->elf.root.u.def.section;
3576 /* If sym_sec->output_section is NULL, then it's a
3577 symbol defined in a shared library. */
3578 if (sym_sec->output_section != NULL)
3579 relocation = (h->elf.root.u.def.value
3580 + sym_sec->output_offset
3581 + sym_sec->output_section->vma);
3583 else if (h->elf.root.type == bfd_link_hash_undefweak)
3585 else if (info->shared && !info->no_undefined
3586 && ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT)
3588 if (info->symbolic)
3589 if (!((*info->callbacks->undefined_symbol)
3590 (info, h->elf.root.root.string, input_bfd,
3591 input_section, rel->r_offset, false)))
3592 return false;
3594 else
3596 if (!((*info->callbacks->undefined_symbol)
3597 (info, h->elf.root.root.string, input_bfd,
3598 input_section, rel->r_offset, true)))
3599 return false;
3603 /* Do any required modifications to the relocation value, and
3604 determine what types of dynamic info we need to output, if
3605 any. */
3606 plabel = 0;
3607 switch (r_type)
3609 case R_PARISC_DLTIND14F:
3610 case R_PARISC_DLTIND14R:
3611 case R_PARISC_DLTIND21L:
3612 /* Relocation is to the entry for this symbol in the global
3613 offset table. */
3614 if (h != NULL)
3616 bfd_vma off;
3618 off = h->elf.got.offset;
3619 BFD_ASSERT (off != (bfd_vma) -1);
3621 if (! hplink->root.dynamic_sections_created
3622 || (info->shared
3623 && (info->symbolic || h->elf.dynindx == -1)
3624 && (h->elf.elf_link_hash_flags
3625 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3627 /* This is actually a static link, or it is a
3628 -Bsymbolic link and the symbol is defined
3629 locally, or the symbol was forced to be local
3630 because of a version file. We must initialize
3631 this entry in the global offset table. Since the
3632 offset must always be a multiple of 4, we use the
3633 least significant bit to record whether we have
3634 initialized it already.
3636 When doing a dynamic link, we create a .rela.got
3637 relocation entry to initialize the value. This
3638 is done in the finish_dynamic_symbol routine. */
3639 if ((off & 1) != 0)
3640 off &= ~1;
3641 else
3643 bfd_put_32 (output_bfd, relocation,
3644 hplink->sgot->contents + off);
3645 h->elf.got.offset |= 1;
3649 relocation = off;
3651 else
3653 /* Local symbol case. */
3654 bfd_vma off;
3656 BFD_ASSERT (local_got_offsets != NULL
3657 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3659 off = local_got_offsets[r_symndx];
3661 /* The offset must always be a multiple of 4. We use
3662 the least significant bit to record whether we have
3663 already generated the necessary reloc. */
3664 if ((off & 1) != 0)
3665 off &= ~1;
3666 else
3668 bfd_put_32 (output_bfd, relocation,
3669 hplink->sgot->contents + off);
3671 if (info->shared)
3673 /* Output a dynamic *ABS* relocation for this
3674 GOT entry. In this case it is relative to
3675 the base of the object because the symbol
3676 index is zero. */
3677 Elf_Internal_Rela outrel;
3678 asection *srelgot = hplink->srelgot;
3680 outrel.r_offset = (off
3681 + hplink->sgot->output_offset
3682 + hplink->sgot->output_section->vma);
3683 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3684 outrel.r_addend = relocation;
3685 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3686 ((Elf32_External_Rela *)
3687 srelgot->contents
3688 + srelgot->reloc_count));
3689 ++srelgot->reloc_count;
3692 local_got_offsets[r_symndx] |= 1;
3695 relocation = off;
3698 /* Add the base of the GOT to the relocation value. */
3699 relocation += (hplink->sgot->output_offset
3700 + hplink->sgot->output_section->vma);
3701 break;
3703 case R_PARISC_SEGREL32:
3704 /* If this is the first SEGREL relocation, then initialize
3705 the segment base values. */
3706 if (hplink->text_segment_base == (bfd_vma) -1)
3707 bfd_map_over_sections (output_bfd,
3708 hppa_record_segment_addr,
3709 hplink);
3710 break;
3712 case R_PARISC_PLABEL14R:
3713 case R_PARISC_PLABEL21L:
3714 case R_PARISC_PLABEL32:
3715 if (hplink->root.dynamic_sections_created)
3717 bfd_vma off;
3719 /* If we have a global symbol with a PLT slot, then
3720 redirect this relocation to it. */
3721 if (h != NULL)
3723 off = h->elf.plt.offset;
3725 else
3727 int indx;
3729 indx = r_symndx + symtab_hdr->sh_info;
3730 off = local_got_offsets[indx];
3732 /* As for the local .got entry case, we use the last
3733 bit to record whether we've already initialised
3734 this local .plt entry. */
3735 if ((off & 1) != 0)
3736 off &= ~1;
3737 else
3739 bfd_put_32 (output_bfd,
3740 relocation,
3741 hplink->splt->contents + off);
3742 bfd_put_32 (output_bfd,
3743 elf_gp (hplink->splt->output_section->owner),
3744 hplink->splt->contents + off + 4);
3746 if (info->shared)
3748 /* Output a dynamic IPLT relocation for this
3749 PLT entry. */
3750 Elf_Internal_Rela outrel;
3751 asection *srelplt = hplink->srelplt;
3753 outrel.r_offset = (off
3754 + hplink->splt->output_offset
3755 + hplink->splt->output_section->vma);
3756 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3757 outrel.r_addend = relocation;
3758 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3759 ((Elf32_External_Rela *)
3760 srelplt->contents
3761 + srelplt->reloc_count));
3762 ++srelplt->reloc_count;
3765 local_got_offsets[indx] |= 1;
3769 BFD_ASSERT (off < (bfd_vma) -2);
3771 /* PLABELs contain function pointers. Relocation is to
3772 the entry for the function in the .plt. The magic +2
3773 offset signals to $$dyncall that the function pointer
3774 is in the .plt and thus has a gp pointer too.
3775 Exception: Undefined PLABELs should have a value of
3776 zero. */
3777 if (h == NULL
3778 || (h->elf.root.type != bfd_link_hash_undefweak
3779 && h->elf.root.type != bfd_link_hash_undefined))
3781 relocation = (off
3782 + hplink->splt->output_offset
3783 + hplink->splt->output_section->vma
3784 + 2);
3786 plabel = 1;
3788 /* Fall through and possibly emit a dynamic relocation. */
3790 case R_PARISC_DIR17F:
3791 case R_PARISC_DIR17R:
3792 case R_PARISC_DIR14F:
3793 case R_PARISC_DIR14R:
3794 case R_PARISC_DIR21L:
3795 case R_PARISC_DPREL14F:
3796 case R_PARISC_DPREL14R:
3797 case R_PARISC_DPREL21L:
3798 case R_PARISC_DIR32:
3799 /* The reloc types handled here and this conditional
3800 expression must match the code in check_relocs and
3801 hppa_discard_copies. ie. We need exactly the same
3802 condition as in check_relocs, with some extra conditions
3803 (dynindx test in this case) to cater for relocs removed
3804 by hppa_discard_copies. */
3805 if ((input_section->flags & SEC_ALLOC) != 0
3806 && info->shared
3807 #if RELATIVE_DYNAMIC_RELOCS
3808 && (is_absolute_reloc (r_type)
3809 || ((!info->symbolic
3810 || (h != NULL
3811 && ((h->elf.elf_link_hash_flags
3812 & ELF_LINK_HASH_DEF_REGULAR) == 0
3813 || h->elf.root.type == bfd_link_hash_defweak)))
3814 && (h == NULL || h->elf.dynindx != -1)))
3815 #endif
3818 Elf_Internal_Rela outrel;
3819 boolean skip;
3821 /* When generating a shared object, these relocations
3822 are copied into the output file to be resolved at run
3823 time. */
3825 if (sreloc == NULL)
3827 const char *name;
3829 name = (bfd_elf_string_from_elf_section
3830 (input_bfd,
3831 elf_elfheader (input_bfd)->e_shstrndx,
3832 elf_section_data (input_section)->rel_hdr.sh_name));
3833 if (name == NULL)
3834 return false;
3835 sreloc = bfd_get_section_by_name (dynobj, name);
3836 BFD_ASSERT (sreloc != NULL);
3839 outrel.r_offset = rel->r_offset;
3840 outrel.r_addend = rel->r_addend;
3841 skip = false;
3842 if (elf_section_data (input_section)->stab_info != NULL)
3844 bfd_vma off;
3846 off = (_bfd_stab_section_offset
3847 (output_bfd, &hplink->root.stab_info,
3848 input_section,
3849 &elf_section_data (input_section)->stab_info,
3850 rel->r_offset));
3851 if (off == (bfd_vma) -1)
3852 skip = true;
3853 outrel.r_offset = off;
3856 outrel.r_offset += (input_section->output_offset
3857 + input_section->output_section->vma);
3859 if (skip)
3861 memset (&outrel, 0, sizeof (outrel));
3863 else if (h != NULL
3864 && h->elf.dynindx != -1
3865 && (plabel
3866 || !info->symbolic
3867 || (h->elf.elf_link_hash_flags
3868 & ELF_LINK_HASH_DEF_REGULAR) == 0))
3870 outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3872 else /* It's a local symbol, or one marked to become local. */
3874 int indx = 0;
3876 /* Add the absolute offset of the symbol. */
3877 outrel.r_addend += relocation;
3879 /* Global plabels need to be processed by the
3880 dynamic linker so that functions have at most one
3881 fptr. For this reason, we need to differentiate
3882 between global and local plabels, which we do by
3883 providing the function symbol for a global plabel
3884 reloc, and no symbol for local plabels. */
3885 if (! plabel
3886 && sym_sec != NULL
3887 && sym_sec->output_section != NULL
3888 && ! bfd_is_abs_section (sym_sec))
3890 indx = elf_section_data (sym_sec->output_section)->dynindx;
3891 /* We are turning this relocation into one
3892 against a section symbol, so subtract out the
3893 output section's address but not the offset
3894 of the input section in the output section. */
3895 outrel.r_addend -= sym_sec->output_section->vma;
3898 outrel.r_info = ELF32_R_INFO (indx, r_type);
3901 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3902 ((Elf32_External_Rela *)
3903 sreloc->contents
3904 + sreloc->reloc_count));
3905 ++sreloc->reloc_count;
3907 break;
3909 default:
3910 break;
3913 r = final_link_relocate (input_section, contents, rel, relocation,
3914 hplink, sym_sec, h);
3916 if (r == bfd_reloc_ok)
3917 continue;
3919 if (h != NULL)
3920 sym_name = h->elf.root.root.string;
3921 else
3923 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3924 symtab_hdr->sh_link,
3925 sym->st_name);
3926 if (sym_name == NULL)
3927 return false;
3928 if (*sym_name == '\0')
3929 sym_name = bfd_section_name (input_bfd, sym_sec);
3932 howto = elf_hppa_howto_table + r_type;
3934 if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
3936 (*_bfd_error_handler)
3937 (_("%s(%s+0x%lx): cannot handle %s for %s"),
3938 bfd_get_filename (input_bfd),
3939 input_section->name,
3940 (long) rel->r_offset,
3941 howto->name,
3942 sym_name);
3944 else
3946 if (!((*info->callbacks->reloc_overflow)
3947 (info, sym_name, howto->name, (bfd_vma) 0,
3948 input_bfd, input_section, rel->r_offset)))
3949 return false;
3953 return true;
3956 /* Comparison function for qsort to sort unwind section during a
3957 final link. */
3959 static int
3960 hppa_unwind_entry_compare (a, b)
3961 const PTR a;
3962 const PTR b;
3964 const bfd_byte *ap, *bp;
3965 unsigned long av, bv;
3967 ap = (const bfd_byte *) a;
3968 av = (unsigned long) ap[0] << 24;
3969 av |= (unsigned long) ap[1] << 16;
3970 av |= (unsigned long) ap[2] << 8;
3971 av |= (unsigned long) ap[3];
3973 bp = (const bfd_byte *) b;
3974 bv = (unsigned long) bp[0] << 24;
3975 bv |= (unsigned long) bp[1] << 16;
3976 bv |= (unsigned long) bp[2] << 8;
3977 bv |= (unsigned long) bp[3];
3979 return av < bv ? -1 : av > bv ? 1 : 0;
3982 /* Finish up dynamic symbol handling. We set the contents of various
3983 dynamic sections here. */
3985 static boolean
3986 elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
3987 bfd *output_bfd;
3988 struct bfd_link_info *info;
3989 struct elf_link_hash_entry *h;
3990 Elf_Internal_Sym *sym;
3992 struct elf32_hppa_link_hash_table *hplink;
3993 bfd *dynobj;
3995 hplink = hppa_link_hash_table (info);
3996 dynobj = hplink->root.dynobj;
3998 if (h->plt.offset != (bfd_vma) -1)
4000 bfd_vma value;
4002 /* This symbol has an entry in the procedure linkage table. Set
4003 it up.
4005 The format of a plt entry is
4006 <funcaddr>
4007 <__gp>
4009 value = 0;
4010 if (h->root.type == bfd_link_hash_defined
4011 || h->root.type == bfd_link_hash_defweak)
4013 value = h->root.u.def.value;
4014 if (h->root.u.def.section->output_section != NULL)
4015 value += (h->root.u.def.section->output_offset
4016 + h->root.u.def.section->output_section->vma);
4019 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
4021 Elf_Internal_Rela rel;
4023 /* Create a dynamic IPLT relocation for this entry. */
4024 rel.r_offset = (h->plt.offset
4025 + hplink->splt->output_offset
4026 + hplink->splt->output_section->vma);
4027 if (! ((struct elf32_hppa_link_hash_entry *) h)->plt_abs
4028 && h->dynindx != -1)
4030 /* To support lazy linking, the function pointer is
4031 initialised to point to a special stub stored at the
4032 end of the .plt. This is only done for plt entries
4033 with a non-*ABS* dynamic relocation. */
4034 value = (hplink->splt->output_offset
4035 + hplink->splt->output_section->vma
4036 + hplink->splt->_raw_size
4037 - sizeof (plt_stub)
4038 + PLT_STUB_ENTRY);
4039 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
4040 rel.r_addend = 0;
4042 else
4044 /* This symbol has been marked to become local, and is
4045 used by a plabel so must be kept in the .plt. */
4046 rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
4047 rel.r_addend = value;
4050 bfd_elf32_swap_reloca_out (hplink->splt->output_section->owner,
4051 &rel,
4052 ((Elf32_External_Rela *)
4053 hplink->srelplt->contents
4054 + hplink->srelplt->reloc_count));
4055 hplink->srelplt->reloc_count++;
4058 bfd_put_32 (hplink->splt->owner,
4059 value,
4060 hplink->splt->contents + h->plt.offset);
4061 bfd_put_32 (hplink->splt->owner,
4062 elf_gp (hplink->splt->output_section->owner),
4063 hplink->splt->contents + h->plt.offset + 4);
4064 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
4065 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
4066 && h->dynindx != -1)
4068 memset (hplink->splt->contents + h->plt.offset + 8,
4069 0, PLABEL_PLT_ENTRY_SIZE - PLT_ENTRY_SIZE);
4072 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4074 /* Mark the symbol as undefined, rather than as defined in
4075 the .plt section. Leave the value alone. */
4076 sym->st_shndx = SHN_UNDEF;
4080 if (h->got.offset != (bfd_vma) -1)
4082 Elf_Internal_Rela rel;
4084 /* This symbol has an entry in the global offset table. Set it
4085 up. */
4087 rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
4088 + hplink->sgot->output_offset
4089 + hplink->sgot->output_section->vma);
4091 /* If this is a static link, or it is a -Bsymbolic link and the
4092 symbol is defined locally or was forced to be local because
4093 of a version file, we just want to emit a RELATIVE reloc.
4094 The entry in the global offset table will already have been
4095 initialized in the relocate_section function. */
4096 if (! hplink->root.dynamic_sections_created
4097 || (info->shared
4098 && (info->symbolic || h->dynindx == -1)
4099 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
4101 rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
4102 rel.r_addend = (h->root.u.def.value
4103 + h->root.u.def.section->output_offset
4104 + h->root.u.def.section->output_section->vma);
4106 else
4108 BFD_ASSERT((h->got.offset & 1) == 0);
4109 bfd_put_32 (output_bfd, (bfd_vma) 0,
4110 hplink->sgot->contents + h->got.offset);
4111 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
4112 rel.r_addend = 0;
4115 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4116 ((Elf32_External_Rela *)
4117 hplink->srelgot->contents
4118 + hplink->srelgot->reloc_count));
4119 ++hplink->srelgot->reloc_count;
4122 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4124 asection *s;
4125 Elf_Internal_Rela rel;
4127 /* This symbol needs a copy reloc. Set it up. */
4129 BFD_ASSERT (h->dynindx != -1
4130 && (h->root.type == bfd_link_hash_defined
4131 || h->root.type == bfd_link_hash_defweak));
4133 s = hplink->srelbss;
4135 rel.r_offset = (h->root.u.def.value
4136 + h->root.u.def.section->output_offset
4137 + h->root.u.def.section->output_section->vma);
4138 rel.r_addend = 0;
4139 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
4140 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4141 ((Elf32_External_Rela *) s->contents
4142 + s->reloc_count));
4143 ++s->reloc_count;
4146 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4147 if (h->root.root.string[0] == '_'
4148 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4149 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
4151 sym->st_shndx = SHN_ABS;
4154 return true;
4157 /* Finish up the dynamic sections. */
4159 static boolean
4160 elf32_hppa_finish_dynamic_sections (output_bfd, info)
4161 bfd *output_bfd;
4162 struct bfd_link_info *info;
4164 bfd *dynobj;
4165 struct elf32_hppa_link_hash_table *hplink;
4166 asection *sdyn;
4168 hplink = hppa_link_hash_table (info);
4169 dynobj = hplink->root.dynobj;
4171 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4173 if (hplink->root.dynamic_sections_created)
4175 Elf32_External_Dyn *dyncon, *dynconend;
4177 BFD_ASSERT (sdyn != NULL);
4179 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4180 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4181 for (; dyncon < dynconend; dyncon++)
4183 Elf_Internal_Dyn dyn;
4184 asection *s;
4186 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4188 switch (dyn.d_tag)
4190 default:
4191 break;
4193 case DT_PLTGOT:
4194 /* Use PLTGOT to set the GOT register. */
4195 dyn.d_un.d_ptr = elf_gp (output_bfd);
4196 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4197 break;
4199 case DT_JMPREL:
4200 s = hplink->srelplt;
4201 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4202 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4203 break;
4205 case DT_PLTRELSZ:
4206 s = hplink->srelplt;
4207 if (s->_cooked_size != 0)
4208 dyn.d_un.d_val = s->_cooked_size;
4209 else
4210 dyn.d_un.d_val = s->_raw_size;
4211 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4212 break;
4217 if (hplink->sgot->_raw_size != 0)
4219 /* Fill in the first entry in the global offset table.
4220 We use it to point to our dynamic section, if we have one. */
4221 bfd_put_32 (output_bfd,
4222 (sdyn != NULL
4223 ? sdyn->output_section->vma + sdyn->output_offset
4224 : (bfd_vma) 0),
4225 hplink->sgot->contents);
4227 /* The second entry is reserved for use by the dynamic linker. */
4228 memset (hplink->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4230 /* Set .got entry size. */
4231 elf_section_data (hplink->sgot->output_section)
4232 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4235 if (hplink->splt->_raw_size != 0)
4237 /* Set plt entry size. */
4238 elf_section_data (hplink->splt->output_section)
4239 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4241 if (hplink->need_plt_stub)
4243 /* Set up the .plt stub. */
4244 memcpy (hplink->splt->contents
4245 + hplink->splt->_raw_size - sizeof (plt_stub),
4246 plt_stub, sizeof (plt_stub));
4248 if ((hplink->splt->output_offset
4249 + hplink->splt->output_section->vma
4250 + hplink->splt->_raw_size)
4251 != (hplink->sgot->output_offset
4252 + hplink->sgot->output_section->vma))
4254 (*_bfd_error_handler)
4255 (_(".got section not immediately after .plt section"));
4256 return false;
4261 return true;
4264 /* Tweak the OSABI field of the elf header. */
4266 static void
4267 elf32_hppa_post_process_headers (abfd, link_info)
4268 bfd *abfd;
4269 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
4271 Elf_Internal_Ehdr * i_ehdrp;
4273 i_ehdrp = elf_elfheader (abfd);
4275 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
4277 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
4279 else
4281 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4285 /* Called when writing out an object file to decide the type of a
4286 symbol. */
4287 static int
4288 elf32_hppa_elf_get_symbol_type (elf_sym, type)
4289 Elf_Internal_Sym *elf_sym;
4290 int type;
4292 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4293 return STT_PARISC_MILLI;
4294 else
4295 return type;
4298 /* Misc BFD support code. */
4299 #define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4300 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4301 #define elf_info_to_howto elf_hppa_info_to_howto
4302 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
4304 /* Stuff for the BFD linker. */
4305 #define bfd_elf32_bfd_final_link elf32_hppa_final_link
4306 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4307 #define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
4308 #define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
4309 #define elf_backend_check_relocs elf32_hppa_check_relocs
4310 #define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4311 #define elf_backend_fake_sections elf_hppa_fake_sections
4312 #define elf_backend_relocate_section elf32_hppa_relocate_section
4313 #define elf_backend_hide_symbol elf32_hppa_hide_symbol
4314 #define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4315 #define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4316 #define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4317 #define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4318 #define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
4319 #define elf_backend_object_p elf32_hppa_object_p
4320 #define elf_backend_final_write_processing elf_hppa_final_write_processing
4321 #define elf_backend_post_process_headers elf32_hppa_post_process_headers
4322 #define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
4324 #define elf_backend_can_gc_sections 1
4325 #define elf_backend_plt_alignment 2
4326 #define elf_backend_want_got_plt 0
4327 #define elf_backend_plt_readonly 0
4328 #define elf_backend_want_plt_sym 0
4329 #define elf_backend_got_header_size 8
4331 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
4332 #define TARGET_BIG_NAME "elf32-hppa"
4333 #define ELF_ARCH bfd_arch_hppa
4334 #define ELF_MACHINE_CODE EM_PARISC
4335 #define ELF_MAXPAGESIZE 0x1000
4337 #include "elf32-target.h"
4339 #undef TARGET_BIG_SYM
4340 #define TARGET_BIG_SYM bfd_elf32_hppa_linux_vec
4341 #undef TARGET_BIG_NAME
4342 #define TARGET_BIG_NAME "elf32-hppa-linux"
4344 #define INCLUDED_TARGET_FILE 1
4345 #include "elf32-target.h"