* emultempl/elf32.em: For SEC_EXCLUDE sections, ensure that
[binutils.git] / bfd / elf32-hppa.c
blob7fe328e7039e10bf673e792a0dcc43f4f7a04476
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. All the relocs of the limited
153 class we are interested in, are absolute. See check_relocs. */
154 #ifndef IS_ABSOLUTE_RELOC
155 #define IS_ABSOLUTE_RELOC(r_type) 1
156 #endif
158 enum elf32_hppa_stub_type {
159 hppa_stub_long_branch,
160 hppa_stub_long_branch_shared,
161 hppa_stub_import,
162 hppa_stub_import_shared,
163 hppa_stub_export,
164 hppa_stub_none
167 struct elf32_hppa_stub_hash_entry {
169 /* Base hash table entry structure. */
170 struct bfd_hash_entry root;
172 /* The stub section. */
173 asection *stub_sec;
175 #if ! LONG_BRANCH_PIC_IN_SHLIB
176 /* It's associated reloc section. */
177 asection *reloc_sec;
178 #endif
180 /* Offset within stub_sec of the beginning of this stub. */
181 bfd_vma stub_offset;
183 /* Given the symbol's value and its section we can determine its final
184 value when building the stubs (so the stub knows where to jump. */
185 bfd_vma target_value;
186 asection *target_section;
188 enum elf32_hppa_stub_type stub_type;
190 /* The symbol table entry, if any, that this was derived from. */
191 struct elf32_hppa_link_hash_entry *h;
193 /* Where this stub is being called from, or, in the case of combined
194 stub sections, the first input section in the group. */
195 asection *id_sec;
198 struct elf32_hppa_link_hash_entry {
200 struct elf_link_hash_entry elf;
202 /* A pointer to the most recently used stub hash entry against this
203 symbol. */
204 struct elf32_hppa_stub_hash_entry *stub_cache;
206 #if ! LONG_BRANCH_PIC_IN_SHLIB
207 /* Used to track whether we have allocated space for a long branch
208 stub relocation for this symbol in the given section. */
209 asection *stub_reloc_sec;
210 #endif
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;
226 /* Set during a static link if we detect a function is PIC. */
227 unsigned int maybe_pic_call:1;
229 /* Set if the only reason we need a .plt entry is for a non-PIC to
230 PIC function call. */
231 unsigned int pic_call:1;
233 /* Set if this symbol is used by a plabel reloc. */
234 unsigned int plabel:1;
236 /* Set if this symbol is an init or fini function and thus should
237 use an absolute reloc. */
238 unsigned int plt_abs:1;
241 struct elf32_hppa_link_hash_table {
243 /* The main hash table. */
244 struct elf_link_hash_table root;
246 /* The stub hash table. */
247 struct bfd_hash_table stub_hash_table;
249 /* Linker stub bfd. */
250 bfd *stub_bfd;
252 /* Linker call-backs. */
253 asection * (*add_stub_section) PARAMS ((const char *, asection *));
254 void (*layout_sections_again) PARAMS ((void));
256 /* Array to keep track of which stub sections have been created, and
257 information on stub grouping. */
258 struct map_stub {
259 /* This is the section to which stubs in the group will be
260 attached. */
261 asection *link_sec;
262 /* The stub section. */
263 asection *stub_sec;
264 #if ! LONG_BRANCH_PIC_IN_SHLIB
265 /* The stub section's reloc section. */
266 asection *reloc_sec;
267 #endif
268 } *stub_group;
270 /* Short-cuts to get to dynamic linker sections. */
271 asection *sgot;
272 asection *srelgot;
273 asection *splt;
274 asection *srelplt;
275 asection *sdynbss;
276 asection *srelbss;
278 /* Used during a final link to store the base of the text and data
279 segments so that we can perform SEGREL relocations. */
280 bfd_vma text_segment_base;
281 bfd_vma data_segment_base;
283 /* Whether we support multiple sub-spaces for shared libs. */
284 unsigned int multi_subspace:1;
286 /* Flags set when PCREL12F and PCREL17F branches detected. Used to
287 select suitable defaults for the stub group size. */
288 unsigned int has_12bit_branch:1;
289 unsigned int has_17bit_branch:1;
291 /* Set if we need a .plt stub to support lazy dynamic linking. */
292 unsigned int need_plt_stub:1;
295 /* Various hash macros and functions. */
296 #define hppa_link_hash_table(p) \
297 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
299 #define hppa_stub_hash_lookup(table, string, create, copy) \
300 ((struct elf32_hppa_stub_hash_entry *) \
301 bfd_hash_lookup ((table), (string), (create), (copy)))
303 static struct bfd_hash_entry *stub_hash_newfunc
304 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
306 static struct bfd_hash_entry *hppa_link_hash_newfunc
307 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
309 static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
310 PARAMS ((bfd *));
312 /* Stub handling functions. */
313 static char *hppa_stub_name
314 PARAMS ((const asection *, const asection *,
315 const struct elf32_hppa_link_hash_entry *,
316 const Elf_Internal_Rela *));
318 static struct elf32_hppa_stub_hash_entry *hppa_get_stub_entry
319 PARAMS ((const asection *, const asection *,
320 struct elf32_hppa_link_hash_entry *,
321 const Elf_Internal_Rela *,
322 struct elf32_hppa_link_hash_table *));
324 static struct elf32_hppa_stub_hash_entry *hppa_add_stub
325 PARAMS ((const char *, asection *, struct elf32_hppa_link_hash_table *));
327 static enum elf32_hppa_stub_type hppa_type_of_stub
328 PARAMS ((asection *, const Elf_Internal_Rela *,
329 struct elf32_hppa_link_hash_entry *, bfd_vma));
331 static boolean hppa_build_one_stub
332 PARAMS ((struct bfd_hash_entry *, PTR));
334 static boolean hppa_size_one_stub
335 PARAMS ((struct bfd_hash_entry *, PTR));
337 /* BFD and elf backend functions. */
338 static boolean elf32_hppa_object_p PARAMS ((bfd *));
340 static boolean elf32_hppa_add_symbol_hook
341 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
342 const char **, flagword *, asection **, bfd_vma *));
344 static boolean elf32_hppa_create_dynamic_sections
345 PARAMS ((bfd *, struct bfd_link_info *));
347 static boolean elf32_hppa_check_relocs
348 PARAMS ((bfd *, struct bfd_link_info *,
349 asection *, const Elf_Internal_Rela *));
351 static asection *elf32_hppa_gc_mark_hook
352 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
353 struct elf_link_hash_entry *, Elf_Internal_Sym *));
355 static boolean elf32_hppa_gc_sweep_hook
356 PARAMS ((bfd *, struct bfd_link_info *,
357 asection *, const Elf_Internal_Rela *));
359 static void elf32_hppa_hide_symbol
360 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
362 static boolean elf32_hppa_adjust_dynamic_symbol
363 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
365 static boolean hppa_handle_PIC_calls
366 PARAMS ((struct elf_link_hash_entry *, PTR));
368 static boolean allocate_plt_and_got_and_discard_relocs
369 PARAMS ((struct elf_link_hash_entry *, PTR));
371 static boolean clobber_millicode_symbols
372 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
374 static boolean elf32_hppa_size_dynamic_sections
375 PARAMS ((bfd *, struct bfd_link_info *));
377 static boolean elf32_hppa_final_link
378 PARAMS ((bfd *, struct bfd_link_info *));
380 static void hppa_record_segment_addr
381 PARAMS ((bfd *, asection *, PTR));
383 static bfd_reloc_status_type final_link_relocate
384 PARAMS ((asection *, bfd_byte *, const Elf_Internal_Rela *,
385 bfd_vma, struct elf32_hppa_link_hash_table *, asection *,
386 struct elf32_hppa_link_hash_entry *));
388 static boolean elf32_hppa_relocate_section
389 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
390 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
392 static int hppa_unwind_entry_compare
393 PARAMS ((const PTR, const PTR));
395 static boolean elf32_hppa_finish_dynamic_symbol
396 PARAMS ((bfd *, struct bfd_link_info *,
397 struct elf_link_hash_entry *, Elf_Internal_Sym *));
399 static boolean elf32_hppa_finish_dynamic_sections
400 PARAMS ((bfd *, struct bfd_link_info *));
402 static void elf32_hppa_post_process_headers
403 PARAMS ((bfd *, struct bfd_link_info *));
405 static int elf32_hppa_elf_get_symbol_type
406 PARAMS ((Elf_Internal_Sym *, int));
408 /* Assorted hash table functions. */
410 /* Initialize an entry in the stub hash table. */
412 static struct bfd_hash_entry *
413 stub_hash_newfunc (entry, table, string)
414 struct bfd_hash_entry *entry;
415 struct bfd_hash_table *table;
416 const char *string;
418 struct elf32_hppa_stub_hash_entry *ret;
420 ret = (struct elf32_hppa_stub_hash_entry *) entry;
422 /* Allocate the structure if it has not already been allocated by a
423 subclass. */
424 if (ret == NULL)
426 ret = ((struct elf32_hppa_stub_hash_entry *)
427 bfd_hash_allocate (table,
428 sizeof (struct elf32_hppa_stub_hash_entry)));
429 if (ret == NULL)
430 return NULL;
433 /* Call the allocation method of the superclass. */
434 ret = ((struct elf32_hppa_stub_hash_entry *)
435 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
437 if (ret)
439 /* Initialize the local fields. */
440 ret->stub_sec = NULL;
441 #if ! LONG_BRANCH_PIC_IN_SHLIB
442 ret->reloc_sec = NULL;
443 #endif
444 ret->stub_offset = 0;
445 ret->target_value = 0;
446 ret->target_section = NULL;
447 ret->stub_type = hppa_stub_long_branch;
448 ret->h = NULL;
449 ret->id_sec = NULL;
452 return (struct bfd_hash_entry *) ret;
455 /* Initialize an entry in the link hash table. */
457 static struct bfd_hash_entry *
458 hppa_link_hash_newfunc (entry, table, string)
459 struct bfd_hash_entry *entry;
460 struct bfd_hash_table *table;
461 const char *string;
463 struct elf32_hppa_link_hash_entry *ret;
465 ret = (struct elf32_hppa_link_hash_entry *) entry;
467 /* Allocate the structure if it has not already been allocated by a
468 subclass. */
469 if (ret == NULL)
471 ret = ((struct elf32_hppa_link_hash_entry *)
472 bfd_hash_allocate (table,
473 sizeof (struct elf32_hppa_link_hash_entry)));
474 if (ret == NULL)
475 return NULL;
478 /* Call the allocation method of the superclass. */
479 ret = ((struct elf32_hppa_link_hash_entry *)
480 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
481 table, string));
483 if (ret)
485 /* Initialize the local fields. */
486 #if ! LONG_BRANCH_PIC_IN_SHLIB
487 ret->stub_reloc_sec = NULL;
488 #endif
489 ret->stub_cache = NULL;
490 ret->reloc_entries = NULL;
491 ret->maybe_pic_call = 0;
492 ret->pic_call = 0;
493 ret->plabel = 0;
494 ret->plt_abs = 0;
497 return (struct bfd_hash_entry *) ret;
500 /* Create the derived linker hash table. The PA ELF port uses the derived
501 hash table to keep information specific to the PA ELF linker (without
502 using static variables). */
504 static struct bfd_link_hash_table *
505 elf32_hppa_link_hash_table_create (abfd)
506 bfd *abfd;
508 struct elf32_hppa_link_hash_table *ret;
510 ret = ((struct elf32_hppa_link_hash_table *) bfd_alloc (abfd, sizeof (*ret)));
511 if (ret == NULL)
512 return NULL;
514 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd, hppa_link_hash_newfunc))
516 bfd_release (abfd, ret);
517 return NULL;
520 /* Init the stub hash table too. */
521 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc))
522 return NULL;
524 ret->stub_bfd = NULL;
525 ret->add_stub_section = NULL;
526 ret->layout_sections_again = NULL;
527 ret->stub_group = NULL;
528 ret->sgot = NULL;
529 ret->srelgot = NULL;
530 ret->splt = NULL;
531 ret->srelplt = NULL;
532 ret->sdynbss = NULL;
533 ret->srelbss = NULL;
534 ret->text_segment_base = (bfd_vma) -1;
535 ret->data_segment_base = (bfd_vma) -1;
536 ret->multi_subspace = 0;
537 ret->has_12bit_branch = 0;
538 ret->has_17bit_branch = 0;
539 ret->need_plt_stub = 0;
541 return &ret->root.root;
544 /* Build a name for an entry in the stub hash table. */
546 static char *
547 hppa_stub_name (input_section, sym_sec, hash, rel)
548 const asection *input_section;
549 const asection *sym_sec;
550 const struct elf32_hppa_link_hash_entry *hash;
551 const Elf_Internal_Rela *rel;
553 char *stub_name;
554 size_t len;
556 if (hash)
558 len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
559 stub_name = bfd_malloc (len);
560 if (stub_name != NULL)
562 sprintf (stub_name, "%08x_%s+%x",
563 input_section->id & 0xffffffff,
564 hash->elf.root.root.string,
565 (int) rel->r_addend & 0xffffffff);
568 else
570 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
571 stub_name = bfd_malloc (len);
572 if (stub_name != NULL)
574 sprintf (stub_name, "%08x_%x:%x+%x",
575 input_section->id & 0xffffffff,
576 sym_sec->id & 0xffffffff,
577 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
578 (int) rel->r_addend & 0xffffffff);
581 return stub_name;
584 /* Look up an entry in the stub hash. Stub entries are cached because
585 creating the stub name takes a bit of time. */
587 static struct elf32_hppa_stub_hash_entry *
588 hppa_get_stub_entry (input_section, sym_sec, hash, rel, hplink)
589 const asection *input_section;
590 const asection *sym_sec;
591 struct elf32_hppa_link_hash_entry *hash;
592 const Elf_Internal_Rela *rel;
593 struct elf32_hppa_link_hash_table *hplink;
595 struct elf32_hppa_stub_hash_entry *stub_entry;
596 const asection *id_sec;
598 /* If this input section is part of a group of sections sharing one
599 stub section, then use the id of the first section in the group.
600 Stub names need to include a section id, as there may well be
601 more than one stub used to reach say, printf, and we need to
602 distinguish between them. */
603 id_sec = hplink->stub_group[input_section->id].link_sec;
605 if (hash != NULL && hash->stub_cache != NULL
606 && hash->stub_cache->h == hash
607 && hash->stub_cache->id_sec == id_sec)
609 stub_entry = hash->stub_cache;
611 else
613 char *stub_name;
615 stub_name = hppa_stub_name (id_sec, sym_sec, hash, rel);
616 if (stub_name == NULL)
617 return NULL;
619 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
620 stub_name, false, false);
621 if (stub_entry == NULL)
623 if (hash == NULL || hash->elf.root.type != bfd_link_hash_undefweak)
624 (*_bfd_error_handler) (_("%s(%s+0x%lx): cannot find stub entry %s"),
625 bfd_get_filename (input_section->owner),
626 input_section->name,
627 (long) rel->r_offset,
628 stub_name);
630 else
632 if (hash != NULL)
633 hash->stub_cache = stub_entry;
636 free (stub_name);
639 return stub_entry;
642 /* Add a new stub entry to the stub hash. Not all fields of the new
643 stub entry are initialised. */
645 static struct elf32_hppa_stub_hash_entry *
646 hppa_add_stub (stub_name, section, hplink)
647 const char *stub_name;
648 asection *section;
649 struct elf32_hppa_link_hash_table *hplink;
651 asection *link_sec;
652 asection *stub_sec;
653 struct elf32_hppa_stub_hash_entry *stub_entry;
655 link_sec = hplink->stub_group[section->id].link_sec;
656 stub_sec = hplink->stub_group[section->id].stub_sec;
657 if (stub_sec == NULL)
659 stub_sec = hplink->stub_group[link_sec->id].stub_sec;
660 if (stub_sec == NULL)
662 size_t len;
663 char *s_name;
665 len = strlen (link_sec->name) + sizeof (STUB_SUFFIX);
666 s_name = bfd_alloc (hplink->stub_bfd, len);
667 if (s_name == NULL)
668 return NULL;
670 strcpy (s_name, link_sec->name);
671 strcpy (s_name + len - sizeof (STUB_SUFFIX), STUB_SUFFIX);
672 stub_sec = (*hplink->add_stub_section) (s_name, link_sec);
673 if (stub_sec == NULL)
674 return NULL;
675 hplink->stub_group[link_sec->id].stub_sec = stub_sec;
677 hplink->stub_group[section->id].stub_sec = stub_sec;
680 /* Enter this entry into the linker stub hash table. */
681 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table, stub_name,
682 true, false);
683 if (stub_entry == NULL)
685 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
686 bfd_get_filename (section->owner),
687 stub_name);
688 return NULL;
691 stub_entry->stub_sec = stub_sec;
692 #if ! LONG_BRANCH_PIC_IN_SHLIB
693 stub_entry->reloc_sec = hplink->stub_group[section->id].reloc_sec;
694 #endif
695 stub_entry->stub_offset = 0;
696 stub_entry->id_sec = link_sec;
697 return stub_entry;
700 /* Determine the type of stub needed, if any, for a call. */
702 static enum elf32_hppa_stub_type
703 hppa_type_of_stub (input_sec, rel, hash, destination)
704 asection *input_sec;
705 const Elf_Internal_Rela *rel;
706 struct elf32_hppa_link_hash_entry *hash;
707 bfd_vma destination;
709 bfd_vma location;
710 bfd_vma branch_offset;
711 bfd_vma max_branch_offset;
712 unsigned int r_type;
714 if (hash != NULL
715 && (((hash->elf.root.type == bfd_link_hash_defined
716 || hash->elf.root.type == bfd_link_hash_defweak)
717 && hash->elf.root.u.def.section->output_section == NULL)
718 || (hash->elf.root.type == bfd_link_hash_defweak
719 && hash->elf.dynindx != -1
720 && hash->elf.plt.offset != (bfd_vma) -1)
721 || hash->elf.root.type == bfd_link_hash_undefweak
722 || hash->elf.root.type == bfd_link_hash_undefined
723 || (hash->maybe_pic_call && !(input_sec->flags & SEC_HAS_GOT_REF))))
725 /* If output_section is NULL, then it's a symbol defined in a
726 shared library. We will need an import stub. Decide between
727 hppa_stub_import and hppa_stub_import_shared later. For
728 shared links we need stubs for undefined or weak syms too;
729 They will presumably be resolved by the dynamic linker. */
730 return hppa_stub_import;
733 /* Determine where the call point is. */
734 location = (input_sec->output_offset
735 + input_sec->output_section->vma
736 + rel->r_offset);
738 branch_offset = destination - location - 8;
739 r_type = ELF32_R_TYPE (rel->r_info);
741 /* Determine if a long branch stub is needed. parisc branch offsets
742 are relative to the second instruction past the branch, ie. +8
743 bytes on from the branch instruction location. The offset is
744 signed and counts in units of 4 bytes. */
745 if (r_type == (unsigned int) R_PARISC_PCREL17F)
747 max_branch_offset = (1 << (17-1)) << 2;
749 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
751 max_branch_offset = (1 << (12-1)) << 2;
753 else /* R_PARISC_PCREL22F. */
755 max_branch_offset = (1 << (22-1)) << 2;
758 if (branch_offset + max_branch_offset >= 2*max_branch_offset)
760 #if LONG_BRANCH_VIA_PLT
761 if (hash != NULL
762 && hash->elf.dynindx != -1
763 && hash->elf.plt.offset != (bfd_vma) -1
764 && hash->elf.type != STT_PARISC_MILLI)
766 /* If we are doing a shared link and find we need a long
767 branch stub, then go via the .plt if possible. */
768 return hppa_stub_import;
770 else
771 #endif
772 return hppa_stub_long_branch;
774 return hppa_stub_none;
777 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
778 IN_ARG contains the link info pointer. */
780 #define LDIL_R1 0x20200000 /* ldil LR'XXX,%r1 */
781 #define BE_SR4_R1 0xe0202002 /* be,n RR'XXX(%sr4,%r1) */
783 #define BL_R1 0xe8200000 /* b,l .+8,%r1 */
784 #define ADDIL_R1 0x28200000 /* addil LR'XXX,%r1,%r1 */
785 #define DEPI_R1 0xd4201c1e /* depi 0,31,2,%r1 */
787 #define ADDIL_DP 0x2b600000 /* addil LR'XXX,%dp,%r1 */
788 #define LDW_R1_R21 0x48350000 /* ldw RR'XXX(%sr0,%r1),%r21 */
789 #define BV_R0_R21 0xeaa0c000 /* bv %r0(%r21) */
790 #define LDW_R1_R19 0x48330000 /* ldw RR'XXX(%sr0,%r1),%r19 */
792 #define ADDIL_R19 0x2a600000 /* addil LR'XXX,%r19,%r1 */
793 #define LDW_R1_DP 0x483b0000 /* ldw RR'XXX(%sr0,%r1),%dp */
795 #define LDSID_R21_R1 0x02a010a1 /* ldsid (%sr0,%r21),%r1 */
796 #define MTSP_R1 0x00011820 /* mtsp %r1,%sr0 */
797 #define BE_SR0_R21 0xe2a00000 /* be 0(%sr0,%r21) */
798 #define STW_RP 0x6bc23fd1 /* stw %rp,-24(%sr0,%sp) */
800 #define BL_RP 0xe8400002 /* b,l,n XXX,%rp */
801 #define NOP 0x08000240 /* nop */
802 #define LDW_RP 0x4bc23fd1 /* ldw -24(%sr0,%sp),%rp */
803 #define LDSID_RP_R1 0x004010a1 /* ldsid (%sr0,%rp),%r1 */
804 #define BE_SR0_RP 0xe0400002 /* be,n 0(%sr0,%rp) */
806 #ifndef R19_STUBS
807 #define R19_STUBS 1
808 #endif
810 #if R19_STUBS
811 #define LDW_R1_DLT LDW_R1_R19
812 #else
813 #define LDW_R1_DLT LDW_R1_DP
814 #endif
816 static boolean
817 hppa_build_one_stub (gen_entry, in_arg)
818 struct bfd_hash_entry *gen_entry;
819 PTR in_arg;
821 struct elf32_hppa_stub_hash_entry *stub_entry;
822 struct bfd_link_info *info;
823 struct elf32_hppa_link_hash_table *hplink;
824 asection *stub_sec;
825 bfd *stub_bfd;
826 bfd_byte *loc;
827 bfd_vma sym_value;
828 bfd_vma insn;
829 bfd_vma off;
830 int val;
831 int size;
833 /* Massage our args to the form they really have. */
834 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
835 info = (struct bfd_link_info *) in_arg;
837 hplink = hppa_link_hash_table (info);
838 stub_sec = stub_entry->stub_sec;
840 /* Make a note of the offset within the stubs for this entry. */
841 stub_entry->stub_offset = stub_sec->_raw_size;
842 loc = stub_sec->contents + stub_entry->stub_offset;
844 stub_bfd = stub_sec->owner;
846 switch (stub_entry->stub_type)
848 case hppa_stub_long_branch:
849 /* Create the long branch. A long branch is formed with "ldil"
850 loading the upper bits of the target address into a register,
851 then branching with "be" which adds in the lower bits.
852 The "be" has its delay slot nullified. */
853 sym_value = (stub_entry->target_value
854 + stub_entry->target_section->output_offset
855 + stub_entry->target_section->output_section->vma);
857 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_lrsel);
858 insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
859 bfd_put_32 (stub_bfd, insn, loc);
861 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_rrsel) >> 2;
862 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
863 bfd_put_32 (stub_bfd, insn, loc + 4);
865 #if ! LONG_BRANCH_PIC_IN_SHLIB
866 if (info->shared)
868 /* Output a dynamic relocation for this stub. We only
869 output one PCREL21L reloc per stub, trusting that the
870 dynamic linker will also fix the implied PCREL17R for the
871 second instruction. PCREL21L dynamic relocs had better
872 never be emitted for some other purpose... */
873 asection *srel;
874 Elf_Internal_Rela outrel;
876 if (stub_entry->h == NULL)
878 (*_bfd_error_handler)
879 (_("%s(%s+0x%lx): cannot relocate %s, recompile with -ffunction-sections"),
880 bfd_get_filename (stub_entry->target_section->owner),
881 stub_sec->name,
882 (long) stub_entry->stub_offset,
883 stub_entry->root.string);
884 bfd_set_error (bfd_error_bad_value);
885 return false;
888 srel = stub_entry->reloc_sec;
889 if (srel == NULL)
891 (*_bfd_error_handler)
892 (_("Could not find relocation section for %s"),
893 stub_sec->name);
894 bfd_set_error (bfd_error_bad_value);
895 return false;
898 outrel.r_offset = (stub_entry->stub_offset
899 + stub_sec->output_offset
900 + stub_sec->output_section->vma);
901 outrel.r_info = ELF32_R_INFO (0, R_PARISC_PCREL21L);
902 outrel.r_addend = sym_value;
903 bfd_elf32_swap_reloca_out (stub_sec->output_section->owner,
904 &outrel,
905 ((Elf32_External_Rela *)
906 srel->contents + srel->reloc_count));
907 ++srel->reloc_count;
909 #endif
910 size = 8;
911 break;
913 case hppa_stub_long_branch_shared:
914 /* Branches are relative. This is where we are going to. */
915 sym_value = (stub_entry->target_value
916 + stub_entry->target_section->output_offset
917 + stub_entry->target_section->output_section->vma);
919 /* And this is where we are coming from, more or less. */
920 sym_value -= (stub_entry->stub_offset
921 + stub_sec->output_offset
922 + stub_sec->output_section->vma);
924 bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
925 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
926 insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
927 bfd_put_32 (stub_bfd, insn, loc + 4);
929 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
930 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
931 bfd_put_32 (stub_bfd, insn, loc + 8);
932 size = 12;
933 break;
935 case hppa_stub_import:
936 case hppa_stub_import_shared:
937 off = stub_entry->h->elf.plt.offset;
938 if (off >= (bfd_vma) -2)
939 abort ();
941 off &= ~ (bfd_vma) 1;
942 sym_value = (off
943 + hplink->splt->output_offset
944 + hplink->splt->output_section->vma
945 - elf_gp (hplink->splt->output_section->owner));
947 insn = ADDIL_DP;
948 #if R19_STUBS
949 if (stub_entry->stub_type == hppa_stub_import_shared)
950 insn = ADDIL_R19;
951 #endif
952 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_lrsel),
953 insn = hppa_rebuild_insn ((int) insn, val, 21);
954 bfd_put_32 (stub_bfd, insn, loc);
956 /* It is critical to use lrsel/rrsel here because we are using
957 two different offsets (+0 and +4) from sym_value. If we use
958 lsel/rsel then with unfortunate sym_values we will round
959 sym_value+4 up to the next 2k block leading to a mis-match
960 between the lsel and rsel value. */
961 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_rrsel);
962 insn = hppa_rebuild_insn ((int) LDW_R1_R21, val, 14);
963 bfd_put_32 (stub_bfd, insn, loc + 4);
965 if (hplink->multi_subspace)
967 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
968 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
969 bfd_put_32 (stub_bfd, insn, loc + 8);
971 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
972 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
973 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21, loc + 20);
974 bfd_put_32 (stub_bfd, (bfd_vma) STW_RP, loc + 24);
976 size = 28;
978 else
980 bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 8);
981 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
982 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
983 bfd_put_32 (stub_bfd, insn, loc + 12);
985 size = 16;
988 if (!info->shared
989 && stub_entry->h != NULL
990 && stub_entry->h->pic_call)
992 /* Build the .plt entry needed to call a PIC function from
993 statically linked code. We don't need any relocs. */
994 bfd *dynobj;
995 struct elf32_hppa_link_hash_entry *eh;
996 bfd_vma value;
998 dynobj = hplink->root.dynobj;
999 eh = (struct elf32_hppa_link_hash_entry *) stub_entry->h;
1001 if (eh->elf.root.type != bfd_link_hash_defined
1002 && eh->elf.root.type != bfd_link_hash_defweak)
1003 abort ();
1005 value = (eh->elf.root.u.def.value
1006 + eh->elf.root.u.def.section->output_offset
1007 + eh->elf.root.u.def.section->output_section->vma);
1009 /* Fill in the entry in the procedure linkage table.
1011 The format of a plt entry is
1012 <funcaddr>
1013 <__gp>. */
1015 bfd_put_32 (hplink->splt->owner, value,
1016 hplink->splt->contents + off);
1017 value = elf_gp (hplink->splt->output_section->owner);
1018 bfd_put_32 (hplink->splt->owner, value,
1019 hplink->splt->contents + off + 4);
1021 break;
1023 case hppa_stub_export:
1024 /* Branches are relative. This is where we are going to. */
1025 sym_value = (stub_entry->target_value
1026 + stub_entry->target_section->output_offset
1027 + stub_entry->target_section->output_section->vma);
1029 /* And this is where we are coming from. */
1030 sym_value -= (stub_entry->stub_offset
1031 + stub_sec->output_offset
1032 + stub_sec->output_section->vma);
1034 if (sym_value - 8 + 0x40000 >= 0x80000)
1036 (*_bfd_error_handler)
1037 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
1038 bfd_get_filename (stub_entry->target_section->owner),
1039 stub_sec->name,
1040 (long) stub_entry->stub_offset,
1041 stub_entry->root.string);
1042 bfd_set_error (bfd_error_bad_value);
1043 return false;
1046 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
1047 insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
1048 bfd_put_32 (stub_bfd, insn, loc);
1050 bfd_put_32 (stub_bfd, (bfd_vma) NOP, loc + 4);
1051 bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP, loc + 8);
1052 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
1053 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
1054 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP, loc + 20);
1056 /* Point the function symbol at the stub. */
1057 stub_entry->h->elf.root.u.def.section = stub_sec;
1058 stub_entry->h->elf.root.u.def.value = stub_sec->_raw_size;
1060 size = 24;
1061 break;
1063 default:
1064 BFD_FAIL ();
1065 return false;
1068 stub_sec->_raw_size += size;
1069 return true;
1072 #undef LDIL_R1
1073 #undef BE_SR4_R1
1074 #undef BL_R1
1075 #undef ADDIL_R1
1076 #undef DEPI_R1
1077 #undef ADDIL_DP
1078 #undef LDW_R1_R21
1079 #undef LDW_R1_DLT
1080 #undef LDW_R1_R19
1081 #undef ADDIL_R19
1082 #undef LDW_R1_DP
1083 #undef LDSID_R21_R1
1084 #undef MTSP_R1
1085 #undef BE_SR0_R21
1086 #undef STW_RP
1087 #undef BV_R0_R21
1088 #undef BL_RP
1089 #undef NOP
1090 #undef LDW_RP
1091 #undef LDSID_RP_R1
1092 #undef BE_SR0_RP
1094 /* As above, but don't actually build the stub. Just bump offset so
1095 we know stub section sizes. */
1097 static boolean
1098 hppa_size_one_stub (gen_entry, in_arg)
1099 struct bfd_hash_entry *gen_entry;
1100 PTR in_arg;
1102 struct elf32_hppa_stub_hash_entry *stub_entry;
1103 struct elf32_hppa_link_hash_table *hplink;
1104 int size;
1106 /* Massage our args to the form they really have. */
1107 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
1108 hplink = (struct elf32_hppa_link_hash_table *) in_arg;
1110 if (stub_entry->stub_type == hppa_stub_long_branch)
1112 #if ! LONG_BRANCH_PIC_IN_SHLIB
1113 if (stub_entry->reloc_sec != NULL)
1114 stub_entry->reloc_sec->_raw_size += sizeof (Elf32_External_Rela);
1115 #endif
1116 size = 8;
1118 else if (stub_entry->stub_type == hppa_stub_long_branch_shared)
1119 size = 12;
1120 else if (stub_entry->stub_type == hppa_stub_export)
1121 size = 24;
1122 else /* hppa_stub_import or hppa_stub_import_shared. */
1124 if (hplink->multi_subspace)
1125 size = 28;
1126 else
1127 size = 16;
1130 stub_entry->stub_sec->_raw_size += size;
1131 return true;
1134 /* Return nonzero if ABFD represents an HPPA ELF32 file.
1135 Additionally we set the default architecture and machine. */
1137 static boolean
1138 elf32_hppa_object_p (abfd)
1139 bfd *abfd;
1141 Elf_Internal_Ehdr * i_ehdrp;
1142 unsigned int flags;
1144 i_ehdrp = elf_elfheader (abfd);
1145 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
1147 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_LINUX)
1148 return false;
1150 else
1152 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX)
1153 return false;
1156 flags = i_ehdrp->e_flags;
1157 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
1159 case EFA_PARISC_1_0:
1160 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
1161 case EFA_PARISC_1_1:
1162 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
1163 case EFA_PARISC_2_0:
1164 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
1165 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
1166 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
1168 return true;
1171 /* Undo the generic ELF code's subtraction of section->vma from the
1172 value of each external symbol. */
1174 static boolean
1175 elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1176 bfd *abfd ATTRIBUTE_UNUSED;
1177 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1178 const Elf_Internal_Sym *sym ATTRIBUTE_UNUSED;
1179 const char **namep ATTRIBUTE_UNUSED;
1180 flagword *flagsp ATTRIBUTE_UNUSED;
1181 asection **secp;
1182 bfd_vma *valp;
1184 *valp += (*secp)->vma;
1185 return true;
1188 /* Create the .plt and .got sections, and set up our hash table
1189 short-cuts to various dynamic sections. */
1191 static boolean
1192 elf32_hppa_create_dynamic_sections (abfd, info)
1193 bfd *abfd;
1194 struct bfd_link_info *info;
1196 struct elf32_hppa_link_hash_table *hplink;
1198 /* Don't try to create the .plt and .got twice. */
1199 hplink = hppa_link_hash_table (info);
1200 if (hplink->splt != NULL)
1201 return true;
1203 /* Call the generic code to do most of the work. */
1204 if (! _bfd_elf_create_dynamic_sections (abfd, info))
1205 return false;
1207 hplink->splt = bfd_get_section_by_name (abfd, ".plt");
1208 hplink->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
1210 hplink->sgot = bfd_get_section_by_name (abfd, ".got");
1211 hplink->srelgot = bfd_make_section (abfd, ".rela.got");
1212 if (hplink->srelgot == NULL
1213 || ! bfd_set_section_flags (abfd, hplink->srelgot,
1214 (SEC_ALLOC
1215 | SEC_LOAD
1216 | SEC_HAS_CONTENTS
1217 | SEC_IN_MEMORY
1218 | SEC_LINKER_CREATED
1219 | SEC_READONLY))
1220 || ! bfd_set_section_alignment (abfd, hplink->srelgot, 2))
1221 return false;
1223 hplink->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
1224 hplink->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
1226 return true;
1229 /* Look through the relocs for a section during the first phase, and
1230 allocate space in the global offset table or procedure linkage
1231 table. At this point we haven't necessarily read all the input
1232 files. */
1234 static boolean
1235 elf32_hppa_check_relocs (abfd, info, sec, relocs)
1236 bfd *abfd;
1237 struct bfd_link_info *info;
1238 asection *sec;
1239 const Elf_Internal_Rela *relocs;
1241 bfd *dynobj;
1242 Elf_Internal_Shdr *symtab_hdr;
1243 struct elf_link_hash_entry **sym_hashes;
1244 bfd_signed_vma *local_got_refcounts;
1245 const Elf_Internal_Rela *rel;
1246 const Elf_Internal_Rela *rel_end;
1247 struct elf32_hppa_link_hash_table *hplink;
1248 asection *sreloc;
1249 asection *stubreloc;
1251 if (info->relocateable)
1252 return true;
1254 hplink = hppa_link_hash_table (info);
1255 dynobj = hplink->root.dynobj;
1256 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1257 sym_hashes = elf_sym_hashes (abfd);
1258 local_got_refcounts = elf_local_got_refcounts (abfd);
1259 sreloc = NULL;
1260 stubreloc = NULL;
1262 rel_end = relocs + sec->reloc_count;
1263 for (rel = relocs; rel < rel_end; rel++)
1265 enum {
1266 NEED_GOT = 1,
1267 NEED_PLT = 2,
1268 NEED_DYNREL = 4,
1269 #if LONG_BRANCH_PIC_IN_SHLIB
1270 NEED_STUBREL = 0, /* We won't be needing them in this case. */
1271 #else
1272 NEED_STUBREL = 8,
1273 #endif
1274 PLT_PLABEL = 16
1277 unsigned int r_symndx, r_type;
1278 struct elf32_hppa_link_hash_entry *h;
1279 int need_entry;
1281 r_symndx = ELF32_R_SYM (rel->r_info);
1283 if (r_symndx < symtab_hdr->sh_info)
1284 h = NULL;
1285 else
1286 h = ((struct elf32_hppa_link_hash_entry *)
1287 sym_hashes[r_symndx - symtab_hdr->sh_info]);
1289 r_type = ELF32_R_TYPE (rel->r_info);
1291 switch (r_type)
1293 case R_PARISC_DLTIND14F:
1294 case R_PARISC_DLTIND14R:
1295 case R_PARISC_DLTIND21L:
1296 /* This symbol requires a global offset table entry. */
1297 need_entry = NEED_GOT;
1299 /* Mark this section as containing PIC code. */
1300 sec->flags |= SEC_HAS_GOT_REF;
1301 break;
1303 case R_PARISC_PLABEL14R: /* "Official" procedure labels. */
1304 case R_PARISC_PLABEL21L:
1305 case R_PARISC_PLABEL32:
1306 /* If the addend is non-zero, we break badly. */
1307 if (rel->r_addend != 0)
1308 abort ();
1310 /* If we are creating a shared library, then we need to
1311 create a PLT entry for all PLABELs, because PLABELs with
1312 local symbols may be passed via a pointer to another
1313 object. Additionally, output a dynamic relocation
1314 pointing to the PLT entry.
1315 For executables, the original 32-bit ABI allowed two
1316 different styles of PLABELs (function pointers): For
1317 global functions, the PLABEL word points into the .plt
1318 two bytes past a (function address, gp) pair, and for
1319 local functions the PLABEL points directly at the
1320 function. The magic +2 for the first type allows us to
1321 differentiate between the two. As you can imagine, this
1322 is a real pain when it comes to generating code to call
1323 functions indirectly or to compare function pointers.
1324 We avoid the mess by always pointing a PLABEL into the
1325 .plt, even for local functions. */
1326 need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
1327 break;
1329 case R_PARISC_PCREL12F:
1330 hplink->has_12bit_branch = 1;
1331 /* Fall thru. */
1332 case R_PARISC_PCREL17C:
1333 case R_PARISC_PCREL17F:
1334 hplink->has_17bit_branch = 1;
1335 /* Fall thru. */
1336 case R_PARISC_PCREL22F:
1337 /* Function calls might need to go through the .plt, and
1338 might require long branch stubs. */
1339 if (h == NULL)
1341 /* We know local syms won't need a .plt entry, and if
1342 they need a long branch stub we can't guarantee that
1343 we can reach the stub. So just flag an error later
1344 if we're doing a shared link and find we need a long
1345 branch stub. */
1346 continue;
1348 else
1350 /* Global symbols will need a .plt entry if they remain
1351 global, and in most cases won't need a long branch
1352 stub. Unfortunately, we have to cater for the case
1353 where a symbol is forced local by versioning, or due
1354 to symbolic linking, and we lose the .plt entry. */
1355 need_entry = NEED_PLT | NEED_STUBREL;
1356 if (h->elf.type == STT_PARISC_MILLI)
1357 need_entry = NEED_STUBREL;
1359 break;
1361 case R_PARISC_SEGBASE: /* Used to set segment base. */
1362 case R_PARISC_SEGREL32: /* Relative reloc, used for unwind. */
1363 case R_PARISC_PCREL14F: /* PC relative load/store. */
1364 case R_PARISC_PCREL14R:
1365 case R_PARISC_PCREL17R: /* External branches. */
1366 case R_PARISC_PCREL21L: /* As above, and for load/store too. */
1367 /* We don't need to propagate the relocation if linking a
1368 shared object since these are section relative. */
1369 continue;
1371 case R_PARISC_DPREL14F: /* Used for gp rel data load/store. */
1372 case R_PARISC_DPREL14R:
1373 case R_PARISC_DPREL21L:
1374 if (info->shared)
1376 (*_bfd_error_handler)
1377 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1378 bfd_get_filename (abfd),
1379 elf_hppa_howto_table[r_type].name);
1380 bfd_set_error (bfd_error_bad_value);
1381 return false;
1383 /* Fall through. */
1385 case R_PARISC_DIR17F: /* Used for external branches. */
1386 case R_PARISC_DIR17R:
1387 case R_PARISC_DIR14F: /* Used for load/store from absolute locn. */
1388 case R_PARISC_DIR14R:
1389 case R_PARISC_DIR21L: /* As above, and for ext branches too. */
1390 #if 1
1391 /* Help debug shared library creation. Any of the above
1392 relocs can be used in shared libs, but they may cause
1393 pages to become unshared. */
1394 if (info->shared)
1396 (*_bfd_error_handler)
1397 (_("%s: relocation %s should not be used when making a shared object; recompile with -fPIC"),
1398 bfd_get_filename (abfd),
1399 elf_hppa_howto_table[r_type].name);
1401 /* Fall through. */
1402 #endif
1404 case R_PARISC_DIR32: /* .word relocs. */
1405 /* We may want to output a dynamic relocation later. */
1406 need_entry = NEED_DYNREL;
1407 break;
1409 /* This relocation describes the C++ object vtable hierarchy.
1410 Reconstruct it for later use during GC. */
1411 case R_PARISC_GNU_VTINHERIT:
1412 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec,
1413 &h->elf, rel->r_offset))
1414 return false;
1415 continue;
1417 /* This relocation describes which C++ vtable entries are actually
1418 used. Record for later use during GC. */
1419 case R_PARISC_GNU_VTENTRY:
1420 if (!_bfd_elf32_gc_record_vtentry (abfd, sec,
1421 &h->elf, rel->r_addend))
1422 return false;
1423 continue;
1425 default:
1426 continue;
1429 /* Now carry out our orders. */
1430 if (need_entry & NEED_GOT)
1432 /* Allocate space for a GOT entry, as well as a dynamic
1433 relocation for this entry. */
1434 if (dynobj == NULL)
1435 hplink->root.dynobj = dynobj = abfd;
1437 if (hplink->sgot == NULL)
1439 if (! elf32_hppa_create_dynamic_sections (dynobj, info))
1440 return false;
1443 if (h != NULL)
1445 if (h->elf.got.refcount == -1)
1446 h->elf.got.refcount = 1;
1447 else
1448 h->elf.got.refcount += 1;
1450 else
1452 /* This is a global offset table entry for a local symbol. */
1453 if (local_got_refcounts == NULL)
1455 size_t size;
1457 /* Allocate space for local got offsets and local
1458 plt offsets. Done this way to save polluting
1459 elf_obj_tdata with another target specific
1460 pointer. */
1461 size = symtab_hdr->sh_info * 2 * sizeof (bfd_signed_vma);
1462 local_got_refcounts = ((bfd_signed_vma *)
1463 bfd_alloc (abfd, size));
1464 if (local_got_refcounts == NULL)
1465 return false;
1466 elf_local_got_refcounts (abfd) = local_got_refcounts;
1467 memset (local_got_refcounts, -1, size);
1469 if (local_got_refcounts[r_symndx] == -1)
1470 local_got_refcounts[r_symndx] = 1;
1471 else
1472 local_got_refcounts[r_symndx] += 1;
1476 if (need_entry & NEED_PLT)
1478 /* If we are creating a shared library, and this is a reloc
1479 against a weak symbol or a global symbol in a dynamic
1480 object, then we will be creating an import stub and a
1481 .plt entry for the symbol. Similarly, on a normal link
1482 to symbols defined in a dynamic object we'll need the
1483 import stub and a .plt entry. We don't know yet whether
1484 the symbol is defined or not, so make an entry anyway and
1485 clean up later in adjust_dynamic_symbol. */
1486 if ((sec->flags & SEC_ALLOC) != 0)
1488 if (h != NULL)
1490 if (h->elf.plt.refcount == -1)
1492 h->elf.plt.refcount = 1;
1493 h->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1495 else
1496 h->elf.plt.refcount += 1;
1498 /* If this .plt entry is for a plabel, mark it so
1499 that adjust_dynamic_symbol will keep the entry
1500 even if it appears to be local. */
1501 if (need_entry & PLT_PLABEL)
1502 h->plabel = 1;
1504 else if (need_entry & PLT_PLABEL)
1506 bfd_signed_vma *local_plt_refcounts;
1508 if (local_got_refcounts == NULL)
1510 size_t size;
1512 /* Allocate space for local got offsets and local
1513 plt offsets. */
1514 size = symtab_hdr->sh_info * 2 * sizeof (bfd_signed_vma);
1515 local_got_refcounts = ((bfd_signed_vma *)
1516 bfd_alloc (abfd, size));
1517 if (local_got_refcounts == NULL)
1518 return false;
1519 elf_local_got_refcounts (abfd) = local_got_refcounts;
1520 memset (local_got_refcounts, -1, size);
1522 local_plt_refcounts = (local_got_refcounts
1523 + symtab_hdr->sh_info);
1524 if (local_plt_refcounts[r_symndx] == -1)
1525 local_plt_refcounts[r_symndx] = 1;
1526 else
1527 local_plt_refcounts[r_symndx] += 1;
1532 if (need_entry & (NEED_DYNREL | NEED_STUBREL))
1534 /* Flag this symbol as having a non-got, non-plt reference
1535 so that we generate copy relocs if it turns out to be
1536 dynamic. */
1537 if (need_entry == NEED_DYNREL
1538 && h != NULL
1539 && !info->shared
1540 && (sec->flags & SEC_READONLY) != 0)
1541 h->elf.elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1543 /* If we are creating a shared library then we need to copy
1544 the reloc into the shared library. However, if we are
1545 linking with -Bsymbolic, we need only copy absolute
1546 relocs or relocs against symbols that are not defined in
1547 an object we are including in the link. PC- or DP- or
1548 DLT-relative relocs against any local sym or global sym
1549 with DEF_REGULAR set, can be discarded. At this point we
1550 have not seen all the input files, so it is possible that
1551 DEF_REGULAR is not set now but will be set later (it is
1552 never cleared). We account for that possibility below by
1553 storing information in the reloc_entries field of the
1554 hash table entry.
1556 A similar situation to the -Bsymbolic case occurs when
1557 creating shared libraries and symbol visibility changes
1558 render the symbol local.
1560 As it turns out, all the relocs we will be creating here
1561 are absolute, so we cannot remove them on -Bsymbolic
1562 links or visibility changes anyway. A STUB_REL reloc
1563 is absolute too, as in that case it is the reloc in the
1564 stub we will be creating, rather than copying the PCREL
1565 reloc in the branch. */
1566 if ((info->shared
1567 && (sec->flags & SEC_ALLOC) != 0
1568 && (IS_ABSOLUTE_RELOC (r_type)
1569 || (h != NULL
1570 && (!info->symbolic
1571 || h->elf.root.type == bfd_link_hash_defweak
1572 || (h->elf.elf_link_hash_flags
1573 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1574 || (!info->shared
1575 && (sec->flags & SEC_ALLOC) != 0
1576 && h != NULL
1577 && (h->elf.elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1578 && (h->elf.root.type == bfd_link_hash_defweak
1579 || (h->elf.elf_link_hash_flags
1580 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1582 boolean doit;
1583 asection *srel;
1585 srel = sreloc;
1586 if ((need_entry & NEED_STUBREL))
1587 srel = stubreloc;
1589 /* Create a reloc section in dynobj and make room for
1590 this reloc. */
1591 if (srel == NULL)
1593 char *name;
1595 if (dynobj == NULL)
1596 hplink->root.dynobj = dynobj = abfd;
1598 name = bfd_elf_string_from_elf_section
1599 (abfd,
1600 elf_elfheader (abfd)->e_shstrndx,
1601 elf_section_data (sec)->rel_hdr.sh_name);
1602 if (name == NULL)
1604 (*_bfd_error_handler)
1605 (_("Could not find relocation section for %s"),
1606 sec->name);
1607 bfd_set_error (bfd_error_bad_value);
1608 return false;
1611 if ((need_entry & NEED_STUBREL))
1613 size_t len = strlen (name) + sizeof (STUB_SUFFIX);
1614 char *newname = bfd_malloc (len);
1616 if (newname == NULL)
1617 return false;
1618 strcpy (newname, name);
1619 strcpy (newname + len - sizeof (STUB_SUFFIX),
1620 STUB_SUFFIX);
1621 name = newname;
1624 srel = bfd_get_section_by_name (dynobj, name);
1625 if (srel == NULL)
1627 flagword flags;
1629 srel = bfd_make_section (dynobj, name);
1630 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1631 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1632 if ((sec->flags & SEC_ALLOC) != 0)
1633 flags |= SEC_ALLOC | SEC_LOAD;
1634 if (srel == NULL
1635 || !bfd_set_section_flags (dynobj, srel, flags)
1636 || !bfd_set_section_alignment (dynobj, srel, 2))
1637 return false;
1639 else if ((need_entry & NEED_STUBREL))
1640 free (name);
1642 if ((need_entry & NEED_STUBREL))
1643 stubreloc = srel;
1644 else
1645 sreloc = srel;
1648 #if ! LONG_BRANCH_PIC_IN_SHLIB
1649 /* If this is a function call, we only need one dynamic
1650 reloc for the stub as all calls to a particular
1651 function will go through the same stub. Actually, a
1652 long branch stub needs two relocations, but we count
1653 on some intelligence on the part of the dynamic
1654 linker. */
1655 if ((need_entry & NEED_STUBREL))
1657 doit = h->stub_reloc_sec != stubreloc;
1658 h->stub_reloc_sec = stubreloc;
1660 else
1661 #endif
1662 doit = 1;
1664 if (doit)
1666 srel->_raw_size += sizeof (Elf32_External_Rela);
1668 /* Keep track of relocations we have entered for
1669 this global symbol, so that we can discard them
1670 later if necessary. */
1671 if (!info->shared
1672 || (h != NULL
1673 && (! IS_ABSOLUTE_RELOC (rtype)
1674 || (need_entry & NEED_STUBREL))))
1676 struct elf32_hppa_dyn_reloc_entry *p;
1678 for (p = h->reloc_entries; p != NULL; p = p->next)
1679 if (p->section == srel)
1680 break;
1682 if (p == NULL)
1684 p = ((struct elf32_hppa_dyn_reloc_entry *)
1685 bfd_alloc (dynobj, sizeof *p));
1686 if (p == NULL)
1687 return false;
1688 p->next = h->reloc_entries;
1689 h->reloc_entries = p;
1690 p->section = srel;
1691 p->count = 0;
1694 /* NEED_STUBREL and NEED_DYNREL are never both
1695 set. Leave the count at zero for the
1696 NEED_STUBREL case as we only ever have one
1697 stub reloc per section per symbol, and this
1698 simplifies code to discard unneeded relocs. */
1699 if (! (need_entry & NEED_STUBREL))
1700 ++p->count;
1707 return true;
1710 /* Return the section that should be marked against garbage collection
1711 for a given relocation. */
1713 static asection *
1714 elf32_hppa_gc_mark_hook (abfd, info, rel, h, sym)
1715 bfd *abfd;
1716 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1717 Elf_Internal_Rela *rel;
1718 struct elf_link_hash_entry *h;
1719 Elf_Internal_Sym *sym;
1721 if (h != NULL)
1723 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1725 case R_PARISC_GNU_VTINHERIT:
1726 case R_PARISC_GNU_VTENTRY:
1727 break;
1729 default:
1730 switch (h->root.type)
1732 case bfd_link_hash_defined:
1733 case bfd_link_hash_defweak:
1734 return h->root.u.def.section;
1736 case bfd_link_hash_common:
1737 return h->root.u.c.p->section;
1739 default:
1740 break;
1744 else
1746 if (!(elf_bad_symtab (abfd)
1747 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1748 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1749 && sym->st_shndx != SHN_COMMON))
1751 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1755 return NULL;
1758 /* Update the got and plt entry reference counts for the section being
1759 removed. */
1761 static boolean
1762 elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
1763 bfd *abfd;
1764 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1765 asection *sec;
1766 const Elf_Internal_Rela *relocs;
1768 Elf_Internal_Shdr *symtab_hdr;
1769 struct elf_link_hash_entry **sym_hashes;
1770 bfd_signed_vma *local_got_refcounts;
1771 bfd_signed_vma *local_plt_refcounts;
1772 const Elf_Internal_Rela *rel, *relend;
1773 unsigned long r_symndx;
1774 struct elf_link_hash_entry *h;
1775 struct elf32_hppa_link_hash_table *hplink;
1776 bfd *dynobj;
1778 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1779 sym_hashes = elf_sym_hashes (abfd);
1780 local_got_refcounts = elf_local_got_refcounts (abfd);
1781 local_plt_refcounts = local_got_refcounts;
1782 if (local_plt_refcounts != NULL)
1783 local_plt_refcounts += symtab_hdr->sh_info;
1784 hplink = hppa_link_hash_table (info);
1785 dynobj = hplink->root.dynobj;
1786 if (dynobj == NULL)
1787 return true;
1789 relend = relocs + sec->reloc_count;
1790 for (rel = relocs; rel < relend; rel++)
1791 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1793 case R_PARISC_DLTIND14F:
1794 case R_PARISC_DLTIND14R:
1795 case R_PARISC_DLTIND21L:
1796 r_symndx = ELF32_R_SYM (rel->r_info);
1797 if (r_symndx >= symtab_hdr->sh_info)
1799 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1800 if (h->got.refcount > 0)
1801 h->got.refcount -= 1;
1803 else if (local_got_refcounts != NULL)
1805 if (local_got_refcounts[r_symndx] > 0)
1806 local_got_refcounts[r_symndx] -= 1;
1808 break;
1810 case R_PARISC_PCREL12F:
1811 case R_PARISC_PCREL17C:
1812 case R_PARISC_PCREL17F:
1813 case R_PARISC_PCREL22F:
1814 r_symndx = ELF32_R_SYM (rel->r_info);
1815 if (r_symndx >= symtab_hdr->sh_info)
1817 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1818 if (h->plt.refcount > 0)
1819 h->plt.refcount -= 1;
1821 break;
1823 case R_PARISC_PLABEL14R:
1824 case R_PARISC_PLABEL21L:
1825 case R_PARISC_PLABEL32:
1826 r_symndx = ELF32_R_SYM (rel->r_info);
1827 if (r_symndx >= symtab_hdr->sh_info)
1829 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1830 if (h->plt.refcount > 0)
1831 h->plt.refcount -= 1;
1833 else if (local_plt_refcounts != NULL)
1835 if (local_plt_refcounts[r_symndx] > 0)
1836 local_plt_refcounts[r_symndx] -= 1;
1838 break;
1840 default:
1841 break;
1844 return true;
1847 /* Our own version of hide_symbol, so that we can keep plt entries for
1848 plabels. */
1850 static void
1851 elf32_hppa_hide_symbol (info, h)
1852 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1853 struct elf_link_hash_entry *h;
1855 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1856 h->dynindx = -1;
1857 if (! ((struct elf32_hppa_link_hash_entry *) h)->plabel)
1859 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1860 h->plt.offset = (bfd_vma) -1;
1864 /* This is the condition under which elf32_hppa_finish_dynamic_symbol
1865 will be called from elflink.h. If elflink.h doesn't call our
1866 finish_dynamic_symbol routine, we'll need to do something about
1867 initializing any .plt and .got entries in elf32_hppa_relocate_section. */
1868 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1869 ((DYN) \
1870 && ((INFO)->shared \
1871 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1872 && ((H)->dynindx != -1 \
1873 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
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;
1932 return true;
1935 /* If this is a weak symbol, and there is a real definition, the
1936 processor independent code will have arranged for us to see the
1937 real definition first, and we can just use the same value. */
1938 if (h->weakdef != NULL)
1940 if (h->weakdef->root.type != bfd_link_hash_defined
1941 && h->weakdef->root.type != bfd_link_hash_defweak)
1942 abort ();
1943 h->root.u.def.section = h->weakdef->root.u.def.section;
1944 h->root.u.def.value = h->weakdef->root.u.def.value;
1945 return true;
1948 /* This is a reference to a symbol defined by a dynamic object which
1949 is not a function. */
1951 /* If we are creating a shared library, we must presume that the
1952 only references to the symbol are via the global offset table.
1953 For such cases we need not do anything here; the relocations will
1954 be handled correctly by relocate_section. */
1955 if (info->shared)
1956 return true;
1958 /* If there are no references to this symbol that do not use the
1959 GOT, we don't need to generate a copy reloc. */
1960 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1961 return true;
1963 /* We must allocate the symbol in our .dynbss section, which will
1964 become part of the .bss section of the executable. There will be
1965 an entry for this symbol in the .dynsym section. The dynamic
1966 object will contain position independent code, so all references
1967 from the dynamic object to this symbol will go through the global
1968 offset table. The dynamic linker will use the .dynsym entry to
1969 determine the address it must put in the global offset table, so
1970 both the dynamic object and the regular object will refer to the
1971 same memory location for the variable. */
1973 s = hplink->sdynbss;
1975 /* We must generate a COPY reloc to tell the dynamic linker to
1976 copy the initial value out of the dynamic object and into the
1977 runtime process image. We need to remember the offset into the
1978 .rela.bss section we are going to use. */
1979 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1981 asection *srel;
1983 srel = hplink->srelbss;
1984 srel->_raw_size += sizeof (Elf32_External_Rela);
1985 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1989 /* We need to figure out the alignment required for this symbol. I
1990 have no idea how other ELF linkers handle this. */
1991 unsigned int power_of_two;
1993 power_of_two = bfd_log2 (h->size);
1994 if (power_of_two > 3)
1995 power_of_two = 3;
1997 /* Apply the required alignment. */
1998 s->_raw_size = BFD_ALIGN (s->_raw_size,
1999 (bfd_size_type) (1 << power_of_two));
2000 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2002 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2003 return false;
2006 /* Define the symbol as being at this point in the section. */
2007 h->root.u.def.section = s;
2008 h->root.u.def.value = s->_raw_size;
2010 /* Increment the section size to make room for the symbol. */
2011 s->_raw_size += h->size;
2013 return true;
2016 /* Called via elf_link_hash_traverse to create .plt entries for an
2017 application that uses statically linked PIC functions. Similar to
2018 the first part of elf32_hppa_adjust_dynamic_symbol. */
2020 static boolean
2021 hppa_handle_PIC_calls (h, inf)
2022 struct elf_link_hash_entry *h;
2023 PTR inf ATTRIBUTE_UNUSED;
2025 if (! (h->plt.refcount > 0
2026 && (h->root.type == bfd_link_hash_defined
2027 || h->root.type == bfd_link_hash_defweak)
2028 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0))
2030 h->plt.offset = (bfd_vma) -1;
2031 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2032 return true;
2035 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2036 ((struct elf32_hppa_link_hash_entry *) h)->maybe_pic_call = 1;
2037 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
2039 return true;
2042 /* Allocate space in .plt, .got and associated reloc sections for
2043 global syms. */
2045 static boolean
2046 allocate_plt_and_got_and_discard_relocs (h, inf)
2047 struct elf_link_hash_entry *h;
2048 PTR inf;
2050 struct bfd_link_info *info;
2051 struct elf32_hppa_link_hash_table *hplink;
2052 asection *s;
2053 struct elf32_hppa_link_hash_entry *eh;
2055 if (h->root.type == bfd_link_hash_indirect
2056 || h->root.type == bfd_link_hash_warning)
2057 return true;
2059 info = (struct bfd_link_info *) inf;
2060 hplink = hppa_link_hash_table (info);
2061 if ((hplink->root.dynamic_sections_created
2062 && h->plt.refcount > 0)
2063 || ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
2065 /* Make sure this symbol is output as a dynamic symbol.
2066 Undefined weak syms won't yet be marked as dynamic. */
2067 if (h->dynindx == -1
2068 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
2069 && h->type != STT_PARISC_MILLI
2070 && !((struct elf32_hppa_link_hash_entry *) h)->pic_call)
2072 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2073 return false;
2076 /* Make an entry in the .plt section. */
2077 s = hplink->splt;
2078 h->plt.offset = s->_raw_size;
2079 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
2080 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
2081 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2083 /* Add some extra space for the dynamic linker to use. */
2084 s->_raw_size += PLABEL_PLT_ENTRY_SIZE;
2086 else
2087 s->_raw_size += PLT_ENTRY_SIZE;
2089 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call
2090 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
2092 /* We also need to make an entry in the .rela.plt section. */
2093 hplink->srelplt->_raw_size += sizeof (Elf32_External_Rela);
2094 hplink->need_plt_stub = 1;
2097 else
2099 h->plt.offset = (bfd_vma) -1;
2100 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2103 if (h->got.refcount > 0)
2105 boolean dyn;
2107 /* Make sure this symbol is output as a dynamic symbol.
2108 Undefined weak syms won't yet be marked as dynamic. */
2109 if (h->dynindx == -1
2110 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
2111 && h->type != STT_PARISC_MILLI)
2113 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2114 return false;
2117 s = hplink->sgot;
2118 h->got.offset = s->_raw_size;
2119 s->_raw_size += GOT_ENTRY_SIZE;
2120 dyn = hplink->root.dynamic_sections_created;
2121 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
2122 hplink->srelgot->_raw_size += sizeof (Elf32_External_Rela);
2124 else
2125 h->got.offset = (bfd_vma) -1;
2127 /* If this is a -Bsymbolic shared link, then we need to discard all
2128 space allocated for dynamic relocs against symbols defined in a
2129 regular object. For the normal shared case, discard space for
2130 relocs that have become local due to symbol visibility changes.
2131 For the non-shared case, discard space for symbols which turn out
2132 to need copy relocs or are not dynamic. We also need to lose
2133 relocs we've allocated for long branch stubs if we know we won't
2134 be generating a stub. */
2136 eh = (struct elf32_hppa_link_hash_entry *) h;
2137 if (eh->reloc_entries == NULL)
2138 return true;
2140 /* First handle the non-shared case. */
2141 if (!info->shared
2142 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2143 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2144 || (hplink->root.dynamic_sections_created
2145 && (h->root.type == bfd_link_hash_undefweak
2146 || h->root.type == bfd_link_hash_undefined))))
2148 /* Make sure this symbol is output as a dynamic symbol.
2149 Undefined weak syms won't yet be marked as dynamic. */
2150 if (h->dynindx == -1
2151 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
2152 && h->type != STT_PARISC_MILLI)
2154 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2155 return false;
2158 /* If that succeeded, we know we'll be keeping all the relocs. */
2159 if (h->dynindx != -1)
2160 return true;
2163 #if ! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT
2164 /* Handle the stub reloc case. If we have a plt entry for the
2165 function, we won't be needing long branch stubs. c->count will
2166 only be zero for stub relocs, which provides a handy way of
2167 flagging these relocs, and means we need do nothing special for
2168 the forced local and symbolic link case. */
2169 if (eh->stub_reloc_sec != NULL
2170 && eh->elf.plt.offset != (bfd_vma) -1)
2172 struct elf32_hppa_dyn_reloc_entry *c;
2174 for (c = eh->reloc_entries; c != NULL; c = c->next)
2175 if (c->count == 0)
2176 c->section->_raw_size -= sizeof (Elf32_External_Rela);
2178 #endif
2180 /* Discard any relocs in the non-shared case. For the shared case,
2181 if a symbol has been forced local or we have found a regular
2182 definition for the symbolic link case, then we won't be needing
2183 any relocs. */
2184 if (!info->shared
2185 || ((eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2186 && ((eh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2187 || info->symbolic)))
2189 struct elf32_hppa_dyn_reloc_entry *c;
2191 for (c = eh->reloc_entries; c != NULL; c = c->next)
2192 c->section->_raw_size -= c->count * sizeof (Elf32_External_Rela);
2195 return true;
2198 /* This function is called via elf_link_hash_traverse to force
2199 millicode symbols local so they do not end up as globals in the
2200 dynamic symbol table. We ought to be able to do this in
2201 adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2202 for all dynamic symbols. Arguably, this is a bug in
2203 elf_adjust_dynamic_symbol. */
2205 static boolean
2206 clobber_millicode_symbols (h, info)
2207 struct elf_link_hash_entry *h;
2208 struct bfd_link_info *info;
2210 /* We only want to remove these from the dynamic symbol table.
2211 Therefore we do not leave ELF_LINK_FORCED_LOCAL set. */
2212 if (h->type == STT_PARISC_MILLI)
2214 unsigned short oldflags = h->elf_link_hash_flags;
2215 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
2216 elf32_hppa_hide_symbol (info, h);
2217 h->elf_link_hash_flags &= ~ELF_LINK_FORCED_LOCAL;
2218 h->elf_link_hash_flags |= oldflags & ELF_LINK_FORCED_LOCAL;
2220 return true;
2223 /* Set the sizes of the dynamic sections. */
2225 static boolean
2226 elf32_hppa_size_dynamic_sections (output_bfd, info)
2227 bfd *output_bfd;
2228 struct bfd_link_info *info;
2230 struct elf32_hppa_link_hash_table *hplink;
2231 bfd *dynobj;
2232 bfd *i;
2233 asection *s;
2234 boolean relocs;
2235 boolean reltext;
2237 hplink = hppa_link_hash_table (info);
2238 dynobj = hplink->root.dynobj;
2239 if (dynobj == NULL)
2240 abort ();
2242 if (hplink->root.dynamic_sections_created)
2244 /* Set the contents of the .interp section to the interpreter. */
2245 if (! info->shared)
2247 s = bfd_get_section_by_name (dynobj, ".interp");
2248 if (s == NULL)
2249 abort ();
2250 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2251 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2254 /* Force millicode symbols local. */
2255 elf_link_hash_traverse (&hplink->root,
2256 clobber_millicode_symbols,
2257 info);
2259 else
2261 /* Run through the function symbols, looking for any that are
2262 PIC, and allocate space for the necessary .plt entries so
2263 that %r19 will be set up. */
2264 if (! info->shared)
2265 elf_link_hash_traverse (&hplink->root,
2266 hppa_handle_PIC_calls,
2267 info);
2270 /* Set up .got and .plt offsets for local syms. */
2271 for (i = info->input_bfds; i; i = i->link_next)
2273 bfd_signed_vma *local_got;
2274 bfd_signed_vma *end_local_got;
2275 bfd_signed_vma *local_plt;
2276 bfd_signed_vma *end_local_plt;
2277 bfd_size_type locsymcount;
2278 Elf_Internal_Shdr *symtab_hdr;
2279 asection *srel;
2281 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
2282 continue;
2284 local_got = elf_local_got_refcounts (i);
2285 if (!local_got)
2286 continue;
2288 symtab_hdr = &elf_tdata (i)->symtab_hdr;
2289 locsymcount = symtab_hdr->sh_info;
2290 end_local_got = local_got + locsymcount;
2291 s = hplink->sgot;
2292 srel = hplink->srelgot;
2293 for (; local_got < end_local_got; ++local_got)
2295 if (*local_got > 0)
2297 *local_got = s->_raw_size;
2298 s->_raw_size += GOT_ENTRY_SIZE;
2299 if (info->shared)
2300 srel->_raw_size += sizeof (Elf32_External_Rela);
2302 else
2303 *local_got = (bfd_vma) -1;
2306 local_plt = end_local_got;
2307 end_local_plt = local_plt + locsymcount;
2308 if (! hplink->root.dynamic_sections_created)
2310 /* Won't be used, but be safe. */
2311 for (; local_plt < end_local_plt; ++local_plt)
2312 *local_plt = (bfd_vma) -1;
2314 else
2316 s = hplink->splt;
2317 srel = hplink->srelplt;
2318 for (; local_plt < end_local_plt; ++local_plt)
2320 if (*local_plt > 0)
2322 *local_plt = s->_raw_size;
2323 s->_raw_size += PLT_ENTRY_SIZE;
2324 if (info->shared)
2325 srel->_raw_size += sizeof (Elf32_External_Rela);
2327 else
2328 *local_plt = (bfd_vma) -1;
2333 /* Allocate global sym .plt and .got entries. Also discard all
2334 unneeded relocs. */
2335 elf_link_hash_traverse (&hplink->root,
2336 allocate_plt_and_got_and_discard_relocs,
2337 (PTR) info);
2339 /* The check_relocs and adjust_dynamic_symbol entry points have
2340 determined the sizes of the various dynamic sections. Allocate
2341 memory for them. */
2342 relocs = false;
2343 reltext = false;
2344 for (s = dynobj->sections; s != NULL; s = s->next)
2346 if ((s->flags & SEC_LINKER_CREATED) == 0)
2347 continue;
2349 if (s == hplink->splt)
2351 if (hplink->need_plt_stub)
2353 /* Make space for the plt stub at the end of the .plt
2354 section. We want this stub right at the end, up
2355 against the .got section. */
2356 int gotalign = bfd_section_alignment (dynobj, hplink->sgot);
2357 int pltalign = bfd_section_alignment (dynobj, s);
2358 bfd_size_type mask;
2360 if (gotalign > pltalign)
2361 bfd_set_section_alignment (dynobj, s, gotalign);
2362 mask = ((bfd_size_type) 1 << gotalign) - 1;
2363 s->_raw_size = (s->_raw_size + sizeof (plt_stub) + mask) & ~mask;
2366 else if (s == hplink->sgot)
2368 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2370 if (s->_raw_size != 0)
2372 asection *target;
2373 const char *outname;
2375 /* Remember whether there are any reloc sections other
2376 than .rela.plt. */
2377 if (s != hplink->srelplt)
2378 relocs = true;
2380 /* If this relocation section applies to a read only
2381 section, then we probably need a DT_TEXTREL entry. */
2382 outname = bfd_get_section_name (output_bfd,
2383 s->output_section);
2384 target = bfd_get_section_by_name (output_bfd, outname + 5);
2385 if (target != NULL
2386 && (target->flags & SEC_READONLY) != 0
2387 && (target->flags & SEC_ALLOC) != 0)
2388 reltext = true;
2390 /* We use the reloc_count field as a counter if we need
2391 to copy relocs into the output file. */
2392 s->reloc_count = 0;
2395 else
2397 /* It's not one of our sections, so don't allocate space. */
2398 continue;
2401 if (s->_raw_size == 0)
2403 /* If we don't need this section, strip it from the
2404 output file. This is mostly to handle .rela.bss and
2405 .rela.plt. We must create both sections in
2406 create_dynamic_sections, because they must be created
2407 before the linker maps input sections to output
2408 sections. The linker does that before
2409 adjust_dynamic_symbol is called, and it is that
2410 function which decides whether anything needs to go
2411 into these sections. */
2412 _bfd_strip_section_from_output (info, s);
2413 continue;
2416 /* Allocate memory for the section contents. Zero it, because
2417 we may not fill in all the reloc sections. */
2418 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2419 if (s->contents == NULL && s->_raw_size != 0)
2420 return false;
2423 if (hplink->root.dynamic_sections_created)
2425 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2426 actually has nothing to do with the PLT, it is how we
2427 communicate the LTP value of a load module to the dynamic
2428 linker. */
2429 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
2430 return false;
2432 /* Add some entries to the .dynamic section. We fill in the
2433 values later, in elf32_hppa_finish_dynamic_sections, but we
2434 must add the entries now so that we get the correct size for
2435 the .dynamic section. The DT_DEBUG entry is filled in by the
2436 dynamic linker and used by the debugger. */
2437 if (! info->shared)
2439 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2440 return false;
2443 if (hplink->srelplt->_raw_size != 0)
2445 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2446 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2447 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2448 return false;
2451 if (relocs)
2453 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2454 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2455 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2456 sizeof (Elf32_External_Rela)))
2457 return false;
2460 if (reltext)
2462 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2463 return false;
2464 info->flags |= DF_TEXTREL;
2468 return true;
2471 /* External entry points for sizing and building linker stubs. */
2473 /* Determine and set the size of the stub section for a final link.
2475 The basic idea here is to examine all the relocations looking for
2476 PC-relative calls to a target that is unreachable with a "bl"
2477 instruction. */
2479 boolean
2480 elf32_hppa_size_stubs (output_bfd, stub_bfd, info, multi_subspace, group_size,
2481 add_stub_section, layout_sections_again)
2482 bfd *output_bfd;
2483 bfd *stub_bfd;
2484 struct bfd_link_info *info;
2485 boolean multi_subspace;
2486 bfd_signed_vma group_size;
2487 asection * (*add_stub_section) PARAMS ((const char *, asection *));
2488 void (*layout_sections_again) PARAMS ((void));
2490 bfd *input_bfd;
2491 asection *section;
2492 asection **input_list, **list;
2493 Elf_Internal_Sym *local_syms, **all_local_syms;
2494 unsigned int bfd_indx, bfd_count;
2495 int top_id, top_index;
2496 struct elf32_hppa_link_hash_table *hplink;
2497 bfd_size_type stub_group_size;
2498 boolean stubs_always_before_branch;
2499 boolean stub_changed = 0;
2500 boolean ret = 0;
2502 hplink = hppa_link_hash_table (info);
2504 /* Stash our params away. */
2505 hplink->stub_bfd = stub_bfd;
2506 hplink->multi_subspace = multi_subspace;
2507 hplink->add_stub_section = add_stub_section;
2508 hplink->layout_sections_again = layout_sections_again;
2509 stubs_always_before_branch = group_size < 0;
2510 if (group_size < 0)
2511 stub_group_size = -group_size;
2512 else
2513 stub_group_size = group_size;
2514 if (stub_group_size == 1)
2516 /* Default values. */
2517 stub_group_size = 8000000;
2518 if (hplink->has_17bit_branch || hplink->multi_subspace)
2519 stub_group_size = 250000;
2520 if (hplink->has_12bit_branch)
2521 stub_group_size = 7812;
2524 /* Count the number of input BFDs and find the top input section id. */
2525 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2526 input_bfd != NULL;
2527 input_bfd = input_bfd->link_next)
2529 bfd_count += 1;
2530 for (section = input_bfd->sections;
2531 section != NULL;
2532 section = section->next)
2534 if (top_id < section->id)
2535 top_id = section->id;
2539 hplink->stub_group
2540 = (struct map_stub *) bfd_zmalloc (sizeof (struct map_stub) * (top_id + 1));
2541 if (hplink->stub_group == NULL)
2542 return false;
2544 /* Make a list of input sections for each output section included in
2545 the link.
2547 We can't use output_bfd->section_count here to find the top output
2548 section index as some sections may have been removed, and
2549 _bfd_strip_section_from_output doesn't renumber the indices. */
2550 for (section = output_bfd->sections, top_index = 0;
2551 section != NULL;
2552 section = section->next)
2554 if (top_index < section->index)
2555 top_index = section->index;
2558 input_list
2559 = (asection **) bfd_malloc (sizeof (asection *) * (top_index + 1));
2560 if (input_list == NULL)
2561 return false;
2563 /* For sections we aren't interested in, mark their entries with a
2564 value we can check later. */
2565 list = input_list + top_index;
2567 *list = bfd_abs_section_ptr;
2568 while (list-- != input_list);
2570 for (section = output_bfd->sections;
2571 section != NULL;
2572 section = section->next)
2574 if ((section->flags & SEC_CODE) != 0)
2575 input_list[section->index] = NULL;
2578 /* Now actually build the lists. */
2579 for (input_bfd = info->input_bfds;
2580 input_bfd != NULL;
2581 input_bfd = input_bfd->link_next)
2583 for (section = input_bfd->sections;
2584 section != NULL;
2585 section = section->next)
2587 if (section->output_section != NULL
2588 && section->output_section->owner == output_bfd
2589 && section->output_section->index <= top_index)
2591 list = input_list + section->output_section->index;
2592 if (*list != bfd_abs_section_ptr)
2594 /* Steal the link_sec pointer for our list. */
2595 #define PREV_SEC(sec) (hplink->stub_group[(sec)->id].link_sec)
2596 /* This happens to make the list in reverse order,
2597 which is what we want. */
2598 PREV_SEC (section) = *list;
2599 *list = section;
2605 /* See whether we can group stub sections together. Grouping stub
2606 sections may result in fewer stubs. More importantly, we need to
2607 put all .init* and .fini* stubs at the beginning of the .init or
2608 .fini output sections respectively, because glibc splits the
2609 _init and _fini functions into multiple parts. Putting a stub in
2610 the middle of a function is not a good idea. */
2611 list = input_list + top_index;
2614 asection *tail = *list;
2615 if (tail == bfd_abs_section_ptr)
2616 continue;
2617 while (tail != NULL)
2619 asection *curr;
2620 asection *prev;
2621 bfd_size_type total;
2623 curr = tail;
2624 if (tail->_cooked_size)
2625 total = tail->_cooked_size;
2626 else
2627 total = tail->_raw_size;
2628 while ((prev = PREV_SEC (curr)) != NULL
2629 && ((total += curr->output_offset - prev->output_offset)
2630 < stub_group_size))
2631 curr = prev;
2633 /* OK, the size from the start of CURR to the end is less
2634 than 250000 bytes and thus can be handled by one stub
2635 section. (or the tail section is itself larger than
2636 250000 bytes, in which case we may be toast.)
2637 We should really be keeping track of the total size of
2638 stubs added here, as stubs contribute to the final output
2639 section size. That's a little tricky, and this way will
2640 only break if stubs added total more than 12144 bytes, or
2641 1518 long branch stubs. It seems unlikely for more than
2642 1518 different functions to be called, especially from
2643 code only 250000 bytes long. */
2646 prev = PREV_SEC (tail);
2647 /* Set up this stub group. */
2648 hplink->stub_group[tail->id].link_sec = curr;
2650 while (tail != curr && (tail = prev) != NULL);
2652 /* But wait, there's more! Input sections up to 250000
2653 bytes before the stub section can be handled by it too. */
2654 if (!stubs_always_before_branch)
2656 total = 0;
2657 while (prev != NULL
2658 && ((total += tail->output_offset - prev->output_offset)
2659 < stub_group_size))
2661 tail = prev;
2662 prev = PREV_SEC (tail);
2663 hplink->stub_group[tail->id].link_sec = curr;
2666 tail = prev;
2669 while (list-- != input_list);
2670 free (input_list);
2671 #undef PREV_SEC
2673 /* We want to read in symbol extension records only once. To do this
2674 we need to read in the local symbols in parallel and save them for
2675 later use; so hold pointers to the local symbols in an array. */
2676 all_local_syms
2677 = (Elf_Internal_Sym **) bfd_zmalloc (sizeof (Elf_Internal_Sym *)
2678 * bfd_count);
2679 if (all_local_syms == NULL)
2680 return false;
2682 /* Walk over all the input BFDs, swapping in local symbols.
2683 If we are creating a shared library, create hash entries for the
2684 export stubs. */
2685 for (input_bfd = info->input_bfds, bfd_indx = 0;
2686 input_bfd != NULL;
2687 input_bfd = input_bfd->link_next, bfd_indx++)
2689 Elf_Internal_Shdr *symtab_hdr;
2690 Elf_Internal_Sym *isym;
2691 Elf32_External_Sym *ext_syms, *esym, *end_sy;
2693 /* We'll need the symbol table in a second. */
2694 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2695 if (symtab_hdr->sh_info == 0)
2696 continue;
2698 /* We need an array of the local symbols attached to the input bfd.
2699 Unfortunately, we're going to have to read & swap them in. */
2700 local_syms = (Elf_Internal_Sym *)
2701 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf_Internal_Sym));
2702 if (local_syms == NULL)
2704 goto error_ret_free_local;
2706 all_local_syms[bfd_indx] = local_syms;
2707 ext_syms = (Elf32_External_Sym *)
2708 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf32_External_Sym));
2709 if (ext_syms == NULL)
2711 goto error_ret_free_local;
2714 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2715 || (bfd_read (ext_syms, 1,
2716 (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)),
2717 input_bfd)
2718 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2720 free (ext_syms);
2721 goto error_ret_free_local;
2724 /* Swap the local symbols in. */
2725 isym = local_syms;
2726 esym = ext_syms;
2727 for (end_sy = esym + symtab_hdr->sh_info; esym < end_sy; esym++, isym++)
2728 bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
2730 /* Now we can free the external symbols. */
2731 free (ext_syms);
2733 #if ! LONG_BRANCH_PIC_IN_SHLIB
2734 /* If this is a shared link, find all the stub reloc sections. */
2735 if (info->shared)
2736 for (section = input_bfd->sections;
2737 section != NULL;
2738 section = section->next)
2740 char *name;
2741 asection *reloc_sec;
2743 name = bfd_malloc (strlen (section->name)
2744 + sizeof STUB_SUFFIX
2745 + 5);
2746 if (name == NULL)
2747 return false;
2748 sprintf (name, ".rela%s%s", section->name, STUB_SUFFIX);
2749 reloc_sec = bfd_get_section_by_name (hplink->root.dynobj, name);
2750 hplink->stub_group[section->id].reloc_sec = reloc_sec;
2751 free (name);
2753 #endif
2755 if (info->shared && hplink->multi_subspace)
2757 struct elf_link_hash_entry **sym_hashes;
2758 struct elf_link_hash_entry **end_hashes;
2759 unsigned int symcount;
2761 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2762 - symtab_hdr->sh_info);
2763 sym_hashes = elf_sym_hashes (input_bfd);
2764 end_hashes = sym_hashes + symcount;
2766 /* Look through the global syms for functions; We need to
2767 build export stubs for all globally visible functions. */
2768 for (; sym_hashes < end_hashes; sym_hashes++)
2770 struct elf32_hppa_link_hash_entry *hash;
2772 hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
2774 while (hash->elf.root.type == bfd_link_hash_indirect
2775 || hash->elf.root.type == bfd_link_hash_warning)
2776 hash = ((struct elf32_hppa_link_hash_entry *)
2777 hash->elf.root.u.i.link);
2779 /* At this point in the link, undefined syms have been
2780 resolved, so we need to check that the symbol was
2781 defined in this BFD. */
2782 if ((hash->elf.root.type == bfd_link_hash_defined
2783 || hash->elf.root.type == bfd_link_hash_defweak)
2784 && hash->elf.type == STT_FUNC
2785 && hash->elf.root.u.def.section->output_section != NULL
2786 && (hash->elf.root.u.def.section->output_section->owner
2787 == output_bfd)
2788 && hash->elf.root.u.def.section->owner == input_bfd
2789 && (hash->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
2790 && !(hash->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2791 && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2793 asection *sec;
2794 const char *stub_name;
2795 struct elf32_hppa_stub_hash_entry *stub_entry;
2797 sec = hash->elf.root.u.def.section;
2798 stub_name = hash->elf.root.root.string;
2799 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2800 stub_name,
2801 false, false);
2802 if (stub_entry == NULL)
2804 stub_entry = hppa_add_stub (stub_name, sec, hplink);
2805 if (!stub_entry)
2806 goto error_ret_free_local;
2808 stub_entry->target_value = hash->elf.root.u.def.value;
2809 stub_entry->target_section = hash->elf.root.u.def.section;
2810 stub_entry->stub_type = hppa_stub_export;
2811 stub_entry->h = hash;
2812 stub_changed = 1;
2814 else
2816 (*_bfd_error_handler) (_("%s: duplicate export stub %s"),
2817 bfd_get_filename (input_bfd),
2818 stub_name);
2825 while (1)
2827 asection *stub_sec;
2829 for (input_bfd = info->input_bfds, bfd_indx = 0;
2830 input_bfd != NULL;
2831 input_bfd = input_bfd->link_next, bfd_indx++)
2833 Elf_Internal_Shdr *symtab_hdr;
2835 /* We'll need the symbol table in a second. */
2836 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2837 if (symtab_hdr->sh_info == 0)
2838 continue;
2840 local_syms = all_local_syms[bfd_indx];
2842 /* Walk over each section attached to the input bfd. */
2843 for (section = input_bfd->sections;
2844 section != NULL;
2845 section = section->next)
2847 Elf_Internal_Shdr *input_rel_hdr;
2848 Elf32_External_Rela *external_relocs, *erelaend, *erela;
2849 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2851 /* If there aren't any relocs, then there's nothing more
2852 to do. */
2853 if ((section->flags & SEC_RELOC) == 0
2854 || section->reloc_count == 0)
2855 continue;
2857 /* If this section is a link-once section that will be
2858 discarded, then don't create any stubs. */
2859 if (section->output_section == NULL
2860 || section->output_section->owner != output_bfd)
2861 continue;
2863 /* Allocate space for the external relocations. */
2864 external_relocs
2865 = ((Elf32_External_Rela *)
2866 bfd_malloc (section->reloc_count
2867 * sizeof (Elf32_External_Rela)));
2868 if (external_relocs == NULL)
2870 goto error_ret_free_local;
2873 /* Likewise for the internal relocations. */
2874 internal_relocs = ((Elf_Internal_Rela *)
2875 bfd_malloc (section->reloc_count
2876 * sizeof (Elf_Internal_Rela)));
2877 if (internal_relocs == NULL)
2879 free (external_relocs);
2880 goto error_ret_free_local;
2883 /* Read in the external relocs. */
2884 input_rel_hdr = &elf_section_data (section)->rel_hdr;
2885 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
2886 || bfd_read (external_relocs, 1,
2887 input_rel_hdr->sh_size,
2888 input_bfd) != input_rel_hdr->sh_size)
2890 free (external_relocs);
2891 error_ret_free_internal:
2892 free (internal_relocs);
2893 goto error_ret_free_local;
2896 /* Swap in the relocs. */
2897 erela = external_relocs;
2898 erelaend = erela + section->reloc_count;
2899 irela = internal_relocs;
2900 for (; erela < erelaend; erela++, irela++)
2901 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
2903 /* We're done with the external relocs, free them. */
2904 free (external_relocs);
2906 /* Now examine each relocation. */
2907 irela = internal_relocs;
2908 irelaend = irela + section->reloc_count;
2909 for (; irela < irelaend; irela++)
2911 unsigned int r_type, r_indx;
2912 enum elf32_hppa_stub_type stub_type;
2913 struct elf32_hppa_stub_hash_entry *stub_entry;
2914 asection *sym_sec;
2915 bfd_vma sym_value;
2916 bfd_vma destination;
2917 struct elf32_hppa_link_hash_entry *hash;
2918 char *stub_name;
2919 const asection *id_sec;
2921 r_type = ELF32_R_TYPE (irela->r_info);
2922 r_indx = ELF32_R_SYM (irela->r_info);
2924 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2926 bfd_set_error (bfd_error_bad_value);
2927 goto error_ret_free_internal;
2930 /* Only look for stubs on call instructions. */
2931 if (r_type != (unsigned int) R_PARISC_PCREL12F
2932 && r_type != (unsigned int) R_PARISC_PCREL17F
2933 && r_type != (unsigned int) R_PARISC_PCREL22F)
2934 continue;
2936 /* Now determine the call target, its name, value,
2937 section. */
2938 sym_sec = NULL;
2939 sym_value = 0;
2940 destination = 0;
2941 hash = NULL;
2942 if (r_indx < symtab_hdr->sh_info)
2944 /* It's a local symbol. */
2945 Elf_Internal_Sym *sym;
2946 Elf_Internal_Shdr *hdr;
2948 sym = local_syms + r_indx;
2949 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2950 sym_sec = hdr->bfd_section;
2951 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2952 sym_value = sym->st_value;
2953 destination = (sym_value + irela->r_addend
2954 + sym_sec->output_offset
2955 + sym_sec->output_section->vma);
2957 else
2959 /* It's an external symbol. */
2960 int e_indx;
2962 e_indx = r_indx - symtab_hdr->sh_info;
2963 hash = ((struct elf32_hppa_link_hash_entry *)
2964 elf_sym_hashes (input_bfd)[e_indx]);
2966 while (hash->elf.root.type == bfd_link_hash_indirect
2967 || hash->elf.root.type == bfd_link_hash_warning)
2968 hash = ((struct elf32_hppa_link_hash_entry *)
2969 hash->elf.root.u.i.link);
2971 if (hash->elf.root.type == bfd_link_hash_defined
2972 || hash->elf.root.type == bfd_link_hash_defweak)
2974 sym_sec = hash->elf.root.u.def.section;
2975 sym_value = hash->elf.root.u.def.value;
2976 if (sym_sec->output_section != NULL)
2977 destination = (sym_value + irela->r_addend
2978 + sym_sec->output_offset
2979 + sym_sec->output_section->vma);
2981 else if (hash->elf.root.type == bfd_link_hash_undefweak)
2983 if (! info->shared)
2984 continue;
2986 else if (hash->elf.root.type == bfd_link_hash_undefined)
2988 if (! (info->shared
2989 && !info->no_undefined
2990 && (ELF_ST_VISIBILITY (hash->elf.other)
2991 == STV_DEFAULT)))
2992 continue;
2994 else
2996 bfd_set_error (bfd_error_bad_value);
2997 goto error_ret_free_internal;
3001 /* Determine what (if any) linker stub is needed. */
3002 stub_type = hppa_type_of_stub (section, irela, hash,
3003 destination);
3004 if (stub_type == hppa_stub_none)
3005 continue;
3007 /* Support for grouping stub sections. */
3008 id_sec = hplink->stub_group[section->id].link_sec;
3010 /* Get the name of this stub. */
3011 stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
3012 if (!stub_name)
3013 goto error_ret_free_internal;
3015 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
3016 stub_name,
3017 false, false);
3018 if (stub_entry != NULL)
3020 /* The proper stub has already been created. */
3021 free (stub_name);
3022 continue;
3025 stub_entry = hppa_add_stub (stub_name, section, hplink);
3026 if (stub_entry == NULL)
3028 free (stub_name);
3029 goto error_ret_free_local;
3032 stub_entry->target_value = sym_value;
3033 stub_entry->target_section = sym_sec;
3034 stub_entry->stub_type = stub_type;
3035 if (info->shared)
3037 if (stub_type == hppa_stub_import)
3038 stub_entry->stub_type = hppa_stub_import_shared;
3039 else if (stub_type == hppa_stub_long_branch
3040 && (LONG_BRANCH_PIC_IN_SHLIB || hash == NULL))
3041 stub_entry->stub_type = hppa_stub_long_branch_shared;
3043 stub_entry->h = hash;
3044 stub_changed = 1;
3047 /* We're done with the internal relocs, free them. */
3048 free (internal_relocs);
3052 if (!stub_changed)
3053 break;
3055 /* OK, we've added some stubs. Find out the new size of the
3056 stub sections. */
3057 for (stub_sec = hplink->stub_bfd->sections;
3058 stub_sec != NULL;
3059 stub_sec = stub_sec->next)
3061 stub_sec->_raw_size = 0;
3062 stub_sec->_cooked_size = 0;
3064 #if ! LONG_BRANCH_PIC_IN_SHLIB
3066 int i;
3068 for (i = top_id; i >= 0; --i)
3070 /* This will probably hit the same section many times.. */
3071 stub_sec = hplink->stub_group[i].reloc_sec;
3072 if (stub_sec != NULL)
3074 stub_sec->_raw_size = 0;
3075 stub_sec->_cooked_size = 0;
3079 #endif
3081 bfd_hash_traverse (&hplink->stub_hash_table,
3082 hppa_size_one_stub,
3083 hplink);
3085 /* Ask the linker to do its stuff. */
3086 (*hplink->layout_sections_again) ();
3087 stub_changed = 0;
3090 ret = 1;
3092 error_ret_free_local:
3093 while (bfd_count-- > 0)
3094 if (all_local_syms[bfd_count])
3095 free (all_local_syms[bfd_count]);
3096 free (all_local_syms);
3098 return ret;
3101 /* For a final link, this function is called after we have sized the
3102 stubs to provide a value for __gp. */
3104 boolean
3105 elf32_hppa_set_gp (abfd, info)
3106 bfd *abfd;
3107 struct bfd_link_info *info;
3109 struct elf32_hppa_link_hash_table *hplink;
3110 struct elf_link_hash_entry *h;
3111 asection *sec;
3112 bfd_vma gp_val;
3114 hplink = hppa_link_hash_table (info);
3115 h = elf_link_hash_lookup (&hplink->root, "$global$",
3116 false, false, false);
3118 if (h != NULL
3119 && (h->root.type == bfd_link_hash_defined
3120 || h->root.type == bfd_link_hash_defweak))
3122 gp_val = h->root.u.def.value;
3123 sec = h->root.u.def.section;
3125 else
3127 /* Choose to point our LTP at, in this order, one of .plt, .got,
3128 or .data, if these sections exist. In the case of choosing
3129 .plt try to make the LTP ideal for addressing anywhere in the
3130 .plt or .got with a 14 bit signed offset. Typically, the end
3131 of the .plt is the start of the .got, so choose .plt + 0x2000
3132 if either the .plt or .got is larger than 0x2000. If both
3133 the .plt and .got are smaller than 0x2000, choose the end of
3134 the .plt section. */
3136 sec = hplink->splt;
3137 if (sec != NULL)
3139 gp_val = sec->_raw_size;
3140 if (gp_val > 0x2000
3141 || (hplink->sgot && hplink->sgot->_raw_size > 0x2000))
3143 gp_val = 0x2000;
3146 else
3148 gp_val = 0;
3149 sec = hplink->sgot;
3150 if (sec != NULL)
3152 /* We know we don't have a .plt. If .got is large,
3153 offset our LTP. */
3154 if (sec->_raw_size > 0x2000)
3155 gp_val = 0x2000;
3157 else
3159 /* No .plt or .got. Who cares what the LTP is? */
3160 sec = bfd_get_section_by_name (abfd, ".data");
3164 if (h != NULL)
3166 h->root.type = bfd_link_hash_defined;
3167 h->root.u.def.value = gp_val;
3168 if (sec != NULL)
3169 h->root.u.def.section = sec;
3170 else
3171 h->root.u.def.section = bfd_abs_section_ptr;
3175 if (sec != NULL && sec->output_section != NULL)
3176 gp_val += sec->output_section->vma + sec->output_offset;
3178 elf_gp (abfd) = gp_val;
3179 return true;
3182 /* Build all the stubs associated with the current output file. The
3183 stubs are kept in a hash table attached to the main linker hash
3184 table. We also set up the .plt entries for statically linked PIC
3185 functions here. This function is called via hppaelf_finish in the
3186 linker. */
3188 boolean
3189 elf32_hppa_build_stubs (info)
3190 struct bfd_link_info *info;
3192 asection *stub_sec;
3193 struct bfd_hash_table *table;
3194 struct elf32_hppa_link_hash_table *hplink;
3196 hplink = hppa_link_hash_table (info);
3198 for (stub_sec = hplink->stub_bfd->sections;
3199 stub_sec != NULL;
3200 stub_sec = stub_sec->next)
3202 size_t size;
3204 /* Allocate memory to hold the linker stubs. */
3205 size = stub_sec->_raw_size;
3206 stub_sec->contents = (unsigned char *) bfd_zalloc (hplink->stub_bfd,
3207 size);
3208 if (stub_sec->contents == NULL && size != 0)
3209 return false;
3210 stub_sec->_raw_size = 0;
3213 /* Build the stubs as directed by the stub hash table. */
3214 table = &hplink->stub_hash_table;
3215 bfd_hash_traverse (table, hppa_build_one_stub, info);
3217 return true;
3220 /* Perform a final link. */
3222 static boolean
3223 elf32_hppa_final_link (abfd, info)
3224 bfd *abfd;
3225 struct bfd_link_info *info;
3227 asection *s;
3229 /* Invoke the regular ELF linker to do all the work. */
3230 if (!bfd_elf32_bfd_final_link (abfd, info))
3231 return false;
3233 /* If we're producing a final executable, sort the contents of the
3234 unwind section. Magic section names, but this is much safer than
3235 having elf32_hppa_relocate_section remember where SEGREL32 relocs
3236 occurred. Consider what happens if someone inept creates a
3237 linker script that puts unwind information in .text. */
3238 s = bfd_get_section_by_name (abfd, ".PARISC.unwind");
3239 if (s != NULL)
3241 bfd_size_type size;
3242 char *contents;
3244 size = s->_raw_size;
3245 contents = bfd_malloc (size);
3246 if (contents == NULL)
3247 return false;
3249 if (! bfd_get_section_contents (abfd, s, contents, (file_ptr) 0, size))
3250 return false;
3252 qsort (contents, size / 16, 16, hppa_unwind_entry_compare);
3254 if (! bfd_set_section_contents (abfd, s, contents, (file_ptr) 0, size))
3255 return false;
3257 return true;
3260 /* Record the lowest address for the data and text segments. */
3262 static void
3263 hppa_record_segment_addr (abfd, section, data)
3264 bfd *abfd ATTRIBUTE_UNUSED;
3265 asection *section;
3266 PTR data;
3268 struct elf32_hppa_link_hash_table *hplink;
3270 hplink = (struct elf32_hppa_link_hash_table *) data;
3272 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3274 bfd_vma value = section->vma - section->filepos;
3276 if ((section->flags & SEC_READONLY) != 0)
3278 if (value < hplink->text_segment_base)
3279 hplink->text_segment_base = value;
3281 else
3283 if (value < hplink->data_segment_base)
3284 hplink->data_segment_base = value;
3289 /* Perform a relocation as part of a final link. */
3291 static bfd_reloc_status_type
3292 final_link_relocate (input_section, contents, rel, value, hplink, sym_sec, h)
3293 asection *input_section;
3294 bfd_byte *contents;
3295 const Elf_Internal_Rela *rel;
3296 bfd_vma value;
3297 struct elf32_hppa_link_hash_table *hplink;
3298 asection *sym_sec;
3299 struct elf32_hppa_link_hash_entry *h;
3301 int insn;
3302 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3303 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3304 int r_format = howto->bitsize;
3305 enum hppa_reloc_field_selector_type_alt r_field;
3306 bfd *input_bfd = input_section->owner;
3307 bfd_vma offset = rel->r_offset;
3308 bfd_vma max_branch_offset = 0;
3309 bfd_byte *hit_data = contents + offset;
3310 bfd_signed_vma addend = rel->r_addend;
3311 bfd_vma location;
3312 struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
3313 int val;
3315 if (r_type == R_PARISC_NONE)
3316 return bfd_reloc_ok;
3318 insn = bfd_get_32 (input_bfd, hit_data);
3320 /* Find out where we are and where we're going. */
3321 location = (offset +
3322 input_section->output_offset +
3323 input_section->output_section->vma);
3325 switch (r_type)
3327 case R_PARISC_PCREL12F:
3328 case R_PARISC_PCREL17F:
3329 case R_PARISC_PCREL22F:
3330 /* If this is a call to a function defined in another dynamic
3331 library, or if it is a call to a PIC function in the same
3332 object, or if this is a shared link and it is a call to a
3333 weak symbol which may or may not be in the same object, then
3334 find the import stub in the stub hash. */
3335 if (sym_sec == NULL
3336 || sym_sec->output_section == NULL
3337 || (h != NULL
3338 && ((h->maybe_pic_call
3339 && !(input_section->flags & SEC_HAS_GOT_REF))
3340 || (h->elf.root.type == bfd_link_hash_defweak
3341 && h->elf.dynindx != -1
3342 && h->elf.plt.offset != (bfd_vma) -1))))
3344 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3345 h, rel, hplink);
3346 if (stub_entry != NULL)
3348 value = (stub_entry->stub_offset
3349 + stub_entry->stub_sec->output_offset
3350 + stub_entry->stub_sec->output_section->vma);
3351 addend = 0;
3353 else if (sym_sec == NULL && h != NULL
3354 && h->elf.root.type == bfd_link_hash_undefweak)
3356 /* It's OK if undefined weak. Calls to undefined weak
3357 symbols behave as if the "called" function
3358 immediately returns. We can thus call to a weak
3359 function without first checking whether the function
3360 is defined. */
3361 value = location;
3362 addend = 8;
3364 else
3365 return bfd_reloc_notsupported;
3367 /* Fall thru. */
3369 case R_PARISC_PCREL21L:
3370 case R_PARISC_PCREL17C:
3371 case R_PARISC_PCREL17R:
3372 case R_PARISC_PCREL14R:
3373 case R_PARISC_PCREL14F:
3374 /* Make it a pc relative offset. */
3375 value -= location;
3376 addend -= 8;
3377 break;
3379 case R_PARISC_DPREL21L:
3380 case R_PARISC_DPREL14R:
3381 case R_PARISC_DPREL14F:
3382 /* For all the DP relative relocations, we need to examine the symbol's
3383 section. If it's a code section, then "data pointer relative" makes
3384 no sense. In that case we don't adjust the "value", and for 21 bit
3385 addil instructions, we change the source addend register from %dp to
3386 %r0. This situation commonly arises when a variable's "constness"
3387 is declared differently from the way the variable is defined. For
3388 instance: "extern int foo" with foo defined as "const int foo". */
3389 if (sym_sec == NULL)
3390 break;
3391 if ((sym_sec->flags & SEC_CODE) != 0)
3393 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3394 == (((int) OP_ADDIL << 26) | (27 << 21)))
3396 insn &= ~ (0x1f << 21);
3397 #if 1 /* debug them. */
3398 (*_bfd_error_handler)
3399 (_("%s(%s+0x%lx): fixing %s"),
3400 bfd_get_filename (input_bfd),
3401 input_section->name,
3402 (long) rel->r_offset,
3403 howto->name);
3404 #endif
3406 /* Now try to make things easy for the dynamic linker. */
3408 break;
3410 /* Fall thru. */
3412 case R_PARISC_DLTIND21L:
3413 case R_PARISC_DLTIND14R:
3414 case R_PARISC_DLTIND14F:
3415 value -= elf_gp (input_section->output_section->owner);
3416 break;
3418 case R_PARISC_SEGREL32:
3419 if ((sym_sec->flags & SEC_CODE) != 0)
3420 value -= hplink->text_segment_base;
3421 else
3422 value -= hplink->data_segment_base;
3423 break;
3425 default:
3426 break;
3429 switch (r_type)
3431 case R_PARISC_DIR32:
3432 case R_PARISC_DIR14F:
3433 case R_PARISC_DIR17F:
3434 case R_PARISC_PCREL17C:
3435 case R_PARISC_PCREL14F:
3436 case R_PARISC_DPREL14F:
3437 case R_PARISC_PLABEL32:
3438 case R_PARISC_DLTIND14F:
3439 case R_PARISC_SEGBASE:
3440 case R_PARISC_SEGREL32:
3441 r_field = e_fsel;
3442 break;
3444 case R_PARISC_DIR21L:
3445 case R_PARISC_PCREL21L:
3446 case R_PARISC_DPREL21L:
3447 case R_PARISC_PLABEL21L:
3448 case R_PARISC_DLTIND21L:
3449 r_field = e_lrsel;
3450 break;
3452 case R_PARISC_DIR17R:
3453 case R_PARISC_PCREL17R:
3454 case R_PARISC_DIR14R:
3455 case R_PARISC_PCREL14R:
3456 case R_PARISC_DPREL14R:
3457 case R_PARISC_PLABEL14R:
3458 case R_PARISC_DLTIND14R:
3459 r_field = e_rrsel;
3460 break;
3462 case R_PARISC_PCREL12F:
3463 case R_PARISC_PCREL17F:
3464 case R_PARISC_PCREL22F:
3465 r_field = e_fsel;
3467 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3469 max_branch_offset = (1 << (17-1)) << 2;
3471 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3473 max_branch_offset = (1 << (12-1)) << 2;
3475 else
3477 max_branch_offset = (1 << (22-1)) << 2;
3480 /* sym_sec is NULL on undefined weak syms or when shared on
3481 undefined syms. We've already checked for a stub for the
3482 shared undefined case. */
3483 if (sym_sec == NULL)
3484 break;
3486 /* If the branch is out of reach, then redirect the
3487 call to the local stub for this function. */
3488 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3490 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3491 h, rel, hplink);
3492 if (stub_entry == NULL)
3493 return bfd_reloc_notsupported;
3495 /* Munge up the value and addend so that we call the stub
3496 rather than the procedure directly. */
3497 value = (stub_entry->stub_offset
3498 + stub_entry->stub_sec->output_offset
3499 + stub_entry->stub_sec->output_section->vma
3500 - location);
3501 addend = -8;
3503 break;
3505 /* Something we don't know how to handle. */
3506 default:
3507 return bfd_reloc_notsupported;
3510 /* Make sure we can reach the stub. */
3511 if (max_branch_offset != 0
3512 && value + addend + max_branch_offset >= 2*max_branch_offset)
3514 (*_bfd_error_handler)
3515 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3516 bfd_get_filename (input_bfd),
3517 input_section->name,
3518 (long) rel->r_offset,
3519 stub_entry->root.string);
3520 return bfd_reloc_notsupported;
3523 val = hppa_field_adjust (value, addend, r_field);
3525 switch (r_type)
3527 case R_PARISC_PCREL12F:
3528 case R_PARISC_PCREL17C:
3529 case R_PARISC_PCREL17F:
3530 case R_PARISC_PCREL17R:
3531 case R_PARISC_PCREL22F:
3532 case R_PARISC_DIR17F:
3533 case R_PARISC_DIR17R:
3534 /* This is a branch. Divide the offset by four.
3535 Note that we need to decide whether it's a branch or
3536 otherwise by inspecting the reloc. Inspecting insn won't
3537 work as insn might be from a .word directive. */
3538 val >>= 2;
3539 break;
3541 default:
3542 break;
3545 insn = hppa_rebuild_insn (insn, val, r_format);
3547 /* Update the instruction word. */
3548 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3549 return bfd_reloc_ok;
3552 /* Relocate an HPPA ELF section. */
3554 static boolean
3555 elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
3556 contents, relocs, local_syms, local_sections)
3557 bfd *output_bfd;
3558 struct bfd_link_info *info;
3559 bfd *input_bfd;
3560 asection *input_section;
3561 bfd_byte *contents;
3562 Elf_Internal_Rela *relocs;
3563 Elf_Internal_Sym *local_syms;
3564 asection **local_sections;
3566 bfd *dynobj;
3567 bfd_vma *local_got_offsets;
3568 struct elf32_hppa_link_hash_table *hplink;
3569 Elf_Internal_Shdr *symtab_hdr;
3570 Elf_Internal_Rela *rel;
3571 Elf_Internal_Rela *relend;
3572 asection *sreloc;
3574 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3576 hplink = hppa_link_hash_table (info);
3577 dynobj = hplink->root.dynobj;
3578 local_got_offsets = elf_local_got_offsets (input_bfd);
3579 sreloc = NULL;
3581 rel = relocs;
3582 relend = relocs + input_section->reloc_count;
3583 for (; rel < relend; rel++)
3585 unsigned int r_type;
3586 reloc_howto_type *howto;
3587 unsigned int r_symndx;
3588 struct elf32_hppa_link_hash_entry *h;
3589 Elf_Internal_Sym *sym;
3590 asection *sym_sec;
3591 bfd_vma relocation;
3592 bfd_reloc_status_type r;
3593 const char *sym_name;
3594 boolean plabel;
3595 bfd_vma off;
3597 r_type = ELF32_R_TYPE (rel->r_info);
3598 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3600 bfd_set_error (bfd_error_bad_value);
3601 return false;
3603 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3604 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3605 continue;
3607 r_symndx = ELF32_R_SYM (rel->r_info);
3609 if (info->relocateable)
3611 /* This is a relocateable link. We don't have to change
3612 anything, unless the reloc is against a section symbol,
3613 in which case we have to adjust according to where the
3614 section symbol winds up in the output section. */
3615 if (r_symndx < symtab_hdr->sh_info)
3617 sym = local_syms + r_symndx;
3618 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3620 sym_sec = local_sections[r_symndx];
3621 rel->r_addend += sym_sec->output_offset;
3624 continue;
3627 /* This is a final link. */
3628 h = NULL;
3629 sym = NULL;
3630 sym_sec = NULL;
3631 if (r_symndx < symtab_hdr->sh_info)
3633 /* This is a local symbol, h defaults to NULL. */
3634 sym = local_syms + r_symndx;
3635 sym_sec = local_sections[r_symndx];
3636 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
3637 ? 0 : sym->st_value)
3638 + sym_sec->output_offset
3639 + sym_sec->output_section->vma);
3641 else
3643 int indx;
3645 /* It's a global; Find its entry in the link hash. */
3646 indx = r_symndx - symtab_hdr->sh_info;
3647 h = ((struct elf32_hppa_link_hash_entry *)
3648 elf_sym_hashes (input_bfd)[indx]);
3649 while (h->elf.root.type == bfd_link_hash_indirect
3650 || h->elf.root.type == bfd_link_hash_warning)
3651 h = (struct elf32_hppa_link_hash_entry *) h->elf.root.u.i.link;
3653 relocation = 0;
3654 if (h->elf.root.type == bfd_link_hash_defined
3655 || h->elf.root.type == bfd_link_hash_defweak)
3657 sym_sec = h->elf.root.u.def.section;
3658 /* If sym_sec->output_section is NULL, then it's a
3659 symbol defined in a shared library. */
3660 if (sym_sec->output_section != NULL)
3661 relocation = (h->elf.root.u.def.value
3662 + sym_sec->output_offset
3663 + sym_sec->output_section->vma);
3665 else if (h->elf.root.type == bfd_link_hash_undefweak)
3667 else if (info->shared && !info->no_undefined
3668 && ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
3669 && h->elf.type != STT_PARISC_MILLI)
3671 if (info->symbolic)
3672 if (!((*info->callbacks->undefined_symbol)
3673 (info, h->elf.root.root.string, input_bfd,
3674 input_section, rel->r_offset, false)))
3675 return false;
3677 else
3679 if (!((*info->callbacks->undefined_symbol)
3680 (info, h->elf.root.root.string, input_bfd,
3681 input_section, rel->r_offset, true)))
3682 return false;
3686 /* Do any required modifications to the relocation value, and
3687 determine what types of dynamic info we need to output, if
3688 any. */
3689 plabel = 0;
3690 switch (r_type)
3692 case R_PARISC_DLTIND14F:
3693 case R_PARISC_DLTIND14R:
3694 case R_PARISC_DLTIND21L:
3695 /* Relocation is to the entry for this symbol in the global
3696 offset table. */
3697 if (h != NULL)
3699 boolean dyn;
3701 off = h->elf.got.offset;
3702 dyn = hplink->root.dynamic_sections_created;
3703 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, &h->elf))
3705 /* This is actually a static link, or it is a
3706 -Bsymbolic link and the symbol is defined
3707 locally, or the symbol was forced to be local
3708 because of a version file. We must initialize
3709 this entry in the global offset table. Since the
3710 offset must always be a multiple of 4, we use the
3711 least significant bit to record whether we have
3712 initialized it already.
3714 When doing a dynamic link, we create a .rela.got
3715 relocation entry to initialize the value. This
3716 is done in the finish_dynamic_symbol routine. */
3717 if ((off & 1) != 0)
3718 off &= ~1;
3719 else
3721 bfd_put_32 (output_bfd, relocation,
3722 hplink->sgot->contents + off);
3723 h->elf.got.offset |= 1;
3727 else
3729 /* Local symbol case. */
3730 if (local_got_offsets == NULL)
3731 abort ();
3733 off = local_got_offsets[r_symndx];
3735 /* The offset must always be a multiple of 4. We use
3736 the least significant bit to record whether we have
3737 already generated the necessary reloc. */
3738 if ((off & 1) != 0)
3739 off &= ~1;
3740 else
3742 bfd_put_32 (output_bfd, relocation,
3743 hplink->sgot->contents + off);
3745 if (info->shared)
3747 /* Output a dynamic relocation for this GOT
3748 entry. In this case it is relative to the
3749 base of the object because the symbol index
3750 is zero. */
3751 Elf_Internal_Rela outrel;
3752 asection *srelgot = hplink->srelgot;
3754 outrel.r_offset = (off
3755 + hplink->sgot->output_offset
3756 + hplink->sgot->output_section->vma);
3757 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3758 outrel.r_addend = relocation;
3759 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3760 ((Elf32_External_Rela *)
3761 srelgot->contents
3762 + srelgot->reloc_count));
3763 ++srelgot->reloc_count;
3766 local_got_offsets[r_symndx] |= 1;
3770 if (off >= (bfd_vma) -2)
3771 abort ();
3773 /* Add the base of the GOT to the relocation value. */
3774 relocation = (off
3775 + hplink->sgot->output_offset
3776 + hplink->sgot->output_section->vma);
3777 break;
3779 case R_PARISC_SEGREL32:
3780 /* If this is the first SEGREL relocation, then initialize
3781 the segment base values. */
3782 if (hplink->text_segment_base == (bfd_vma) -1)
3783 bfd_map_over_sections (output_bfd,
3784 hppa_record_segment_addr,
3785 hplink);
3786 break;
3788 case R_PARISC_PLABEL14R:
3789 case R_PARISC_PLABEL21L:
3790 case R_PARISC_PLABEL32:
3791 if (hplink->root.dynamic_sections_created)
3793 /* If we have a global symbol with a PLT slot, then
3794 redirect this relocation to it. */
3795 if (h != NULL)
3797 off = h->elf.plt.offset;
3798 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, &h->elf))
3800 /* In a non-shared link, adjust_dynamic_symbols
3801 isn't called for symbols forced local. We
3802 need to write out the plt entry here. */
3803 if ((off & 1) != 0)
3804 off &= ~1;
3805 else
3807 bfd_put_32 (output_bfd,
3808 relocation,
3809 hplink->splt->contents + off);
3810 bfd_put_32 (output_bfd,
3811 elf_gp (hplink->splt->output_section->owner),
3812 hplink->splt->contents + off + 4);
3813 h->elf.plt.offset |= 1;
3817 else
3819 bfd_vma *local_plt_offsets;
3821 if (local_got_offsets == NULL)
3822 abort ();
3824 local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
3825 off = local_plt_offsets[r_symndx];
3827 /* As for the local .got entry case, we use the last
3828 bit to record whether we've already initialised
3829 this local .plt entry. */
3830 if ((off & 1) != 0)
3831 off &= ~1;
3832 else
3834 bfd_put_32 (output_bfd,
3835 relocation,
3836 hplink->splt->contents + off);
3837 bfd_put_32 (output_bfd,
3838 elf_gp (hplink->splt->output_section->owner),
3839 hplink->splt->contents + off + 4);
3841 if (info->shared)
3843 /* Output a dynamic IPLT relocation for this
3844 PLT entry. */
3845 Elf_Internal_Rela outrel;
3846 asection *srelplt = hplink->srelplt;
3848 outrel.r_offset = (off
3849 + hplink->splt->output_offset
3850 + hplink->splt->output_section->vma);
3851 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3852 outrel.r_addend = relocation;
3853 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3854 ((Elf32_External_Rela *)
3855 srelplt->contents
3856 + srelplt->reloc_count));
3857 ++srelplt->reloc_count;
3860 local_plt_offsets[r_symndx] |= 1;
3864 if (off >= (bfd_vma) -2)
3865 abort ();
3867 /* PLABELs contain function pointers. Relocation is to
3868 the entry for the function in the .plt. The magic +2
3869 offset signals to $$dyncall that the function pointer
3870 is in the .plt and thus has a gp pointer too.
3871 Exception: Undefined PLABELs should have a value of
3872 zero. */
3873 if (h == NULL
3874 || (h->elf.root.type != bfd_link_hash_undefweak
3875 && h->elf.root.type != bfd_link_hash_undefined))
3877 relocation = (off
3878 + hplink->splt->output_offset
3879 + hplink->splt->output_section->vma
3880 + 2);
3882 plabel = 1;
3884 /* Fall through and possibly emit a dynamic relocation. */
3886 case R_PARISC_DIR17F:
3887 case R_PARISC_DIR17R:
3888 case R_PARISC_DIR14F:
3889 case R_PARISC_DIR14R:
3890 case R_PARISC_DIR21L:
3891 case R_PARISC_DPREL14F:
3892 case R_PARISC_DPREL14R:
3893 case R_PARISC_DPREL21L:
3894 case R_PARISC_DIR32:
3895 /* The reloc types handled here and this conditional
3896 expression must match the code in check_relocs and
3897 hppa_discard_copies. ie. We need exactly the same
3898 condition as in check_relocs, with some extra conditions
3899 (dynindx test in this case) to cater for relocs removed
3900 by hppa_discard_copies. If you squint, the non-shared
3901 test here does indeed match the one in check_relocs, the
3902 difference being that here we test DEF_DYNAMIC rather
3903 than a maybe-DEF_DYNAMIC via !DEF_REGULAR. Common syms
3904 end up with !DEF_REGULAR, which is why we can't use that
3905 here. Conversely, DEF_DYNAMIC can't be used in
3906 check_relocs as there all files have not been loaded. */
3907 if ((info->shared
3908 && (input_section->flags & SEC_ALLOC) != 0
3909 && (IS_ABSOLUTE_RELOC (r_type)
3910 || (h != NULL
3911 && h->elf.dynindx != -1
3912 && (!info->symbolic
3913 || (h->elf.elf_link_hash_flags
3914 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3915 || (!info->shared
3916 && (input_section->flags & SEC_ALLOC) != 0
3917 && h != NULL
3918 && h->elf.dynindx != -1
3919 && (h->elf.elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3920 && ((h->elf.elf_link_hash_flags
3921 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3922 || h->elf.root.type == bfd_link_hash_undefweak
3923 || h->elf.root.type == bfd_link_hash_undefined)))
3925 Elf_Internal_Rela outrel;
3926 boolean skip;
3928 /* When generating a shared object, these relocations
3929 are copied into the output file to be resolved at run
3930 time. */
3932 if (sreloc == NULL)
3934 const char *name;
3936 name = (bfd_elf_string_from_elf_section
3937 (input_bfd,
3938 elf_elfheader (input_bfd)->e_shstrndx,
3939 elf_section_data (input_section)->rel_hdr.sh_name));
3940 if (name == NULL)
3941 return false;
3942 sreloc = bfd_get_section_by_name (dynobj, name);
3943 if (sreloc == NULL)
3944 abort ();
3947 outrel.r_offset = rel->r_offset;
3948 outrel.r_addend = rel->r_addend;
3949 skip = false;
3950 if (elf_section_data (input_section)->stab_info != NULL)
3952 bfd_vma off;
3954 off = (_bfd_stab_section_offset
3955 (output_bfd, &hplink->root.stab_info,
3956 input_section,
3957 &elf_section_data (input_section)->stab_info,
3958 rel->r_offset));
3959 if (off == (bfd_vma) -1)
3960 skip = true;
3961 outrel.r_offset = off;
3964 outrel.r_offset += (input_section->output_offset
3965 + input_section->output_section->vma);
3967 if (skip)
3969 memset (&outrel, 0, sizeof (outrel));
3971 else if (h != NULL
3972 && h->elf.dynindx != -1
3973 && (plabel
3974 || !IS_ABSOLUTE_RELOC (r_type)
3975 || !info->shared
3976 || !info->symbolic
3977 || (h->elf.elf_link_hash_flags
3978 & ELF_LINK_HASH_DEF_REGULAR) == 0))
3980 outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3982 else /* It's a local symbol, or one marked to become local. */
3984 int indx = 0;
3986 /* Add the absolute offset of the symbol. */
3987 outrel.r_addend += relocation;
3989 /* Global plabels need to be processed by the
3990 dynamic linker so that functions have at most one
3991 fptr. For this reason, we need to differentiate
3992 between global and local plabels, which we do by
3993 providing the function symbol for a global plabel
3994 reloc, and no symbol for local plabels. */
3995 if (! plabel
3996 && sym_sec != NULL
3997 && sym_sec->output_section != NULL
3998 && ! bfd_is_abs_section (sym_sec))
4000 indx = elf_section_data (sym_sec->output_section)->dynindx;
4001 /* We are turning this relocation into one
4002 against a section symbol, so subtract out the
4003 output section's address but not the offset
4004 of the input section in the output section. */
4005 outrel.r_addend -= sym_sec->output_section->vma;
4008 outrel.r_info = ELF32_R_INFO (indx, r_type);
4010 #if 0
4011 /* EH info can cause unaligned DIR32 relocs.
4012 Tweak the reloc type for the dynamic linker. */
4013 if (r_type == R_PARISC_DIR32 && (outrel.r_offset & 3) != 0)
4014 outrel.r_info = ELF32_R_INFO (ELF32_R_SYM (outrel.r_info),
4015 R_PARISC_DIR32U);
4016 #endif
4017 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4018 ((Elf32_External_Rela *)
4019 sreloc->contents
4020 + sreloc->reloc_count));
4021 ++sreloc->reloc_count;
4023 break;
4025 default:
4026 break;
4029 r = final_link_relocate (input_section, contents, rel, relocation,
4030 hplink, sym_sec, h);
4032 if (r == bfd_reloc_ok)
4033 continue;
4035 if (h != NULL)
4036 sym_name = h->elf.root.root.string;
4037 else
4039 sym_name = bfd_elf_string_from_elf_section (input_bfd,
4040 symtab_hdr->sh_link,
4041 sym->st_name);
4042 if (sym_name == NULL)
4043 return false;
4044 if (*sym_name == '\0')
4045 sym_name = bfd_section_name (input_bfd, sym_sec);
4048 howto = elf_hppa_howto_table + r_type;
4050 if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
4052 (*_bfd_error_handler)
4053 (_("%s(%s+0x%lx): cannot handle %s for %s"),
4054 bfd_get_filename (input_bfd),
4055 input_section->name,
4056 (long) rel->r_offset,
4057 howto->name,
4058 sym_name);
4060 else
4062 if (!((*info->callbacks->reloc_overflow)
4063 (info, sym_name, howto->name, (bfd_vma) 0,
4064 input_bfd, input_section, rel->r_offset)))
4065 return false;
4069 return true;
4072 /* Comparison function for qsort to sort unwind section during a
4073 final link. */
4075 static int
4076 hppa_unwind_entry_compare (a, b)
4077 const PTR a;
4078 const PTR b;
4080 const bfd_byte *ap, *bp;
4081 unsigned long av, bv;
4083 ap = (const bfd_byte *) a;
4084 av = (unsigned long) ap[0] << 24;
4085 av |= (unsigned long) ap[1] << 16;
4086 av |= (unsigned long) ap[2] << 8;
4087 av |= (unsigned long) ap[3];
4089 bp = (const bfd_byte *) b;
4090 bv = (unsigned long) bp[0] << 24;
4091 bv |= (unsigned long) bp[1] << 16;
4092 bv |= (unsigned long) bp[2] << 8;
4093 bv |= (unsigned long) bp[3];
4095 return av < bv ? -1 : av > bv ? 1 : 0;
4098 /* Finish up dynamic symbol handling. We set the contents of various
4099 dynamic sections here. */
4101 static boolean
4102 elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
4103 bfd *output_bfd;
4104 struct bfd_link_info *info;
4105 struct elf_link_hash_entry *h;
4106 Elf_Internal_Sym *sym;
4108 struct elf32_hppa_link_hash_table *hplink;
4109 bfd *dynobj;
4111 hplink = hppa_link_hash_table (info);
4112 dynobj = hplink->root.dynobj;
4114 if (h->plt.offset != (bfd_vma) -1)
4116 bfd_vma value;
4118 if (h->plt.offset & 1)
4119 abort ();
4121 /* This symbol has an entry in the procedure linkage table. Set
4122 it up.
4124 The format of a plt entry is
4125 <funcaddr>
4126 <__gp>
4128 value = 0;
4129 if (h->root.type == bfd_link_hash_defined
4130 || h->root.type == bfd_link_hash_defweak)
4132 value = h->root.u.def.value;
4133 if (h->root.u.def.section->output_section != NULL)
4134 value += (h->root.u.def.section->output_offset
4135 + h->root.u.def.section->output_section->vma);
4138 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
4140 Elf_Internal_Rela rel;
4142 /* Create a dynamic IPLT relocation for this entry. */
4143 rel.r_offset = (h->plt.offset
4144 + hplink->splt->output_offset
4145 + hplink->splt->output_section->vma);
4146 if (! ((struct elf32_hppa_link_hash_entry *) h)->plt_abs
4147 && h->dynindx != -1)
4149 /* To support lazy linking, the function pointer is
4150 initialised to point to a special stub stored at the
4151 end of the .plt. This is not done for plt entries
4152 with a base-relative dynamic relocation. */
4153 value = (hplink->splt->output_offset
4154 + hplink->splt->output_section->vma
4155 + hplink->splt->_raw_size
4156 - sizeof (plt_stub)
4157 + PLT_STUB_ENTRY);
4158 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
4159 rel.r_addend = 0;
4161 else
4163 /* This symbol has been marked to become local, and is
4164 used by a plabel so must be kept in the .plt. */
4165 rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
4166 rel.r_addend = value;
4169 bfd_elf32_swap_reloca_out (hplink->splt->output_section->owner,
4170 &rel,
4171 ((Elf32_External_Rela *)
4172 hplink->srelplt->contents
4173 + hplink->srelplt->reloc_count));
4174 hplink->srelplt->reloc_count++;
4177 bfd_put_32 (hplink->splt->owner,
4178 value,
4179 hplink->splt->contents + h->plt.offset);
4180 bfd_put_32 (hplink->splt->owner,
4181 elf_gp (hplink->splt->output_section->owner),
4182 hplink->splt->contents + h->plt.offset + 4);
4183 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
4184 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
4185 && h->dynindx != -1)
4187 memset (hplink->splt->contents + h->plt.offset + 8,
4188 0, PLABEL_PLT_ENTRY_SIZE - PLT_ENTRY_SIZE);
4191 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4193 /* Mark the symbol as undefined, rather than as defined in
4194 the .plt section. Leave the value alone. */
4195 sym->st_shndx = SHN_UNDEF;
4199 if (h->got.offset != (bfd_vma) -1)
4201 Elf_Internal_Rela rel;
4203 /* This symbol has an entry in the global offset table. Set it
4204 up. */
4206 rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
4207 + hplink->sgot->output_offset
4208 + hplink->sgot->output_section->vma);
4210 /* If this is a -Bsymbolic link and the symbol is defined
4211 locally or was forced to be local because of a version file,
4212 we just want to emit a RELATIVE reloc. The entry in the
4213 global offset table will already have been initialized in the
4214 relocate_section function. */
4215 if (info->shared
4216 && (info->symbolic || h->dynindx == -1)
4217 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
4219 rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
4220 rel.r_addend = (h->root.u.def.value
4221 + h->root.u.def.section->output_offset
4222 + h->root.u.def.section->output_section->vma);
4224 else
4226 if ((h->got.offset & 1) != 0)
4227 abort ();
4228 bfd_put_32 (output_bfd, (bfd_vma) 0,
4229 hplink->sgot->contents + h->got.offset);
4230 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
4231 rel.r_addend = 0;
4234 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4235 ((Elf32_External_Rela *)
4236 hplink->srelgot->contents
4237 + hplink->srelgot->reloc_count));
4238 ++hplink->srelgot->reloc_count;
4241 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4243 asection *s;
4244 Elf_Internal_Rela rel;
4246 /* This symbol needs a copy reloc. Set it up. */
4248 if (! (h->dynindx != -1
4249 && (h->root.type == bfd_link_hash_defined
4250 || h->root.type == bfd_link_hash_defweak)))
4251 abort ();
4253 s = hplink->srelbss;
4255 rel.r_offset = (h->root.u.def.value
4256 + h->root.u.def.section->output_offset
4257 + h->root.u.def.section->output_section->vma);
4258 rel.r_addend = 0;
4259 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
4260 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4261 ((Elf32_External_Rela *) s->contents
4262 + s->reloc_count));
4263 ++s->reloc_count;
4266 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4267 if (h->root.root.string[0] == '_'
4268 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4269 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
4271 sym->st_shndx = SHN_ABS;
4274 return true;
4277 /* Finish up the dynamic sections. */
4279 static boolean
4280 elf32_hppa_finish_dynamic_sections (output_bfd, info)
4281 bfd *output_bfd;
4282 struct bfd_link_info *info;
4284 bfd *dynobj;
4285 struct elf32_hppa_link_hash_table *hplink;
4286 asection *sdyn;
4288 hplink = hppa_link_hash_table (info);
4289 dynobj = hplink->root.dynobj;
4291 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4293 if (hplink->root.dynamic_sections_created)
4295 Elf32_External_Dyn *dyncon, *dynconend;
4297 if (sdyn == NULL)
4298 abort ();
4300 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4301 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4302 for (; dyncon < dynconend; dyncon++)
4304 Elf_Internal_Dyn dyn;
4305 asection *s;
4307 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4309 switch (dyn.d_tag)
4311 default:
4312 break;
4314 case DT_PLTGOT:
4315 /* Use PLTGOT to set the GOT register. */
4316 dyn.d_un.d_ptr = elf_gp (output_bfd);
4317 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4318 break;
4320 case DT_JMPREL:
4321 s = hplink->srelplt;
4322 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4323 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4324 break;
4326 case DT_PLTRELSZ:
4327 s = hplink->srelplt;
4328 if (s->_cooked_size != 0)
4329 dyn.d_un.d_val = s->_cooked_size;
4330 else
4331 dyn.d_un.d_val = s->_raw_size;
4332 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4333 break;
4338 if (hplink->sgot != NULL && hplink->sgot->_raw_size != 0)
4340 /* Fill in the first entry in the global offset table.
4341 We use it to point to our dynamic section, if we have one. */
4342 bfd_put_32 (output_bfd,
4343 (sdyn != NULL
4344 ? sdyn->output_section->vma + sdyn->output_offset
4345 : (bfd_vma) 0),
4346 hplink->sgot->contents);
4348 /* The second entry is reserved for use by the dynamic linker. */
4349 memset (hplink->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4351 /* Set .got entry size. */
4352 elf_section_data (hplink->sgot->output_section)
4353 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4356 if (hplink->splt != NULL && hplink->splt->_raw_size != 0)
4358 /* Set plt entry size. */
4359 elf_section_data (hplink->splt->output_section)
4360 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4362 if (hplink->need_plt_stub)
4364 /* Set up the .plt stub. */
4365 memcpy (hplink->splt->contents
4366 + hplink->splt->_raw_size - sizeof (plt_stub),
4367 plt_stub, sizeof (plt_stub));
4369 if ((hplink->splt->output_offset
4370 + hplink->splt->output_section->vma
4371 + hplink->splt->_raw_size)
4372 != (hplink->sgot->output_offset
4373 + hplink->sgot->output_section->vma))
4375 (*_bfd_error_handler)
4376 (_(".got section not immediately after .plt section"));
4377 return false;
4382 return true;
4385 /* Tweak the OSABI field of the elf header. */
4387 static void
4388 elf32_hppa_post_process_headers (abfd, link_info)
4389 bfd *abfd;
4390 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
4392 Elf_Internal_Ehdr * i_ehdrp;
4394 i_ehdrp = elf_elfheader (abfd);
4396 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
4398 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
4400 else
4402 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4406 /* Called when writing out an object file to decide the type of a
4407 symbol. */
4408 static int
4409 elf32_hppa_elf_get_symbol_type (elf_sym, type)
4410 Elf_Internal_Sym *elf_sym;
4411 int type;
4413 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4414 return STT_PARISC_MILLI;
4415 else
4416 return type;
4419 /* Misc BFD support code. */
4420 #define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4421 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4422 #define elf_info_to_howto elf_hppa_info_to_howto
4423 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
4425 /* Stuff for the BFD linker. */
4426 #define bfd_elf32_bfd_final_link elf32_hppa_final_link
4427 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4428 #define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
4429 #define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
4430 #define elf_backend_check_relocs elf32_hppa_check_relocs
4431 #define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4432 #define elf_backend_fake_sections elf_hppa_fake_sections
4433 #define elf_backend_relocate_section elf32_hppa_relocate_section
4434 #define elf_backend_hide_symbol elf32_hppa_hide_symbol
4435 #define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4436 #define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4437 #define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4438 #define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4439 #define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
4440 #define elf_backend_object_p elf32_hppa_object_p
4441 #define elf_backend_final_write_processing elf_hppa_final_write_processing
4442 #define elf_backend_post_process_headers elf32_hppa_post_process_headers
4443 #define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
4445 #define elf_backend_can_gc_sections 1
4446 #define elf_backend_plt_alignment 2
4447 #define elf_backend_want_got_plt 0
4448 #define elf_backend_plt_readonly 0
4449 #define elf_backend_want_plt_sym 0
4450 #define elf_backend_got_header_size 8
4452 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
4453 #define TARGET_BIG_NAME "elf32-hppa"
4454 #define ELF_ARCH bfd_arch_hppa
4455 #define ELF_MACHINE_CODE EM_PARISC
4456 #define ELF_MAXPAGESIZE 0x1000
4458 #include "elf32-target.h"
4460 #undef TARGET_BIG_SYM
4461 #define TARGET_BIG_SYM bfd_elf32_hppa_linux_vec
4462 #undef TARGET_BIG_NAME
4463 #define TARGET_BIG_NAME "elf32-hppa-linux"
4465 #define INCLUDED_TARGET_FILE 1
4466 #include "elf32-target.h"