* readelf.c (display_debug_frames): Use data factor for
[binutils.git] / bfd / elf32-hppa.c
blob592501102310af162510019c722d5e105a2f9d02
1 /* BFD back-end for HP PA-RISC ELF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005 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 "elf32-hppa.h"
36 #include "elf-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 GOT_ENTRY_SIZE 4
118 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
120 static const bfd_byte plt_stub[] =
122 0x0e, 0x80, 0x10, 0x96, /* 1: ldw 0(%r20),%r22 */
123 0xea, 0xc0, 0xc0, 0x00, /* bv %r0(%r22) */
124 0x0e, 0x88, 0x10, 0x95, /* ldw 4(%r20),%r21 */
125 #define PLT_STUB_ENTRY (3*4)
126 0xea, 0x9f, 0x1f, 0xdd, /* b,l 1b,%r20 */
127 0xd6, 0x80, 0x1c, 0x1e, /* depi 0,31,2,%r20 */
128 0x00, 0xc0, 0xff, 0xee, /* 9: .word fixup_func */
129 0xde, 0xad, 0xbe, 0xef /* .word fixup_ltp */
132 /* Section name for stubs is the associated section name plus this
133 string. */
134 #define STUB_SUFFIX ".stub"
136 /* We don't need to copy certain PC- or GP-relative dynamic relocs
137 into a shared object's dynamic section. All the relocs of the
138 limited class we are interested in, are absolute. */
139 #ifndef RELATIVE_DYNRELOCS
140 #define RELATIVE_DYNRELOCS 0
141 #define IS_ABSOLUTE_RELOC(r_type) 1
142 #endif
144 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
145 copying dynamic variables from a shared lib into an app's dynbss
146 section, and instead use a dynamic relocation to point into the
147 shared lib. */
148 #define ELIMINATE_COPY_RELOCS 1
150 enum elf32_hppa_stub_type {
151 hppa_stub_long_branch,
152 hppa_stub_long_branch_shared,
153 hppa_stub_import,
154 hppa_stub_import_shared,
155 hppa_stub_export,
156 hppa_stub_none
159 struct elf32_hppa_stub_hash_entry {
161 /* Base hash table entry structure. */
162 struct bfd_hash_entry root;
164 /* The stub section. */
165 asection *stub_sec;
167 /* Offset within stub_sec of the beginning of this stub. */
168 bfd_vma stub_offset;
170 /* Given the symbol's value and its section we can determine its final
171 value when building the stubs (so the stub knows where to jump. */
172 bfd_vma target_value;
173 asection *target_section;
175 enum elf32_hppa_stub_type stub_type;
177 /* The symbol table entry, if any, that this was derived from. */
178 struct elf32_hppa_link_hash_entry *h;
180 /* Where this stub is being called from, or, in the case of combined
181 stub sections, the first input section in the group. */
182 asection *id_sec;
185 struct elf32_hppa_link_hash_entry {
187 struct elf_link_hash_entry elf;
189 /* A pointer to the most recently used stub hash entry against this
190 symbol. */
191 struct elf32_hppa_stub_hash_entry *stub_cache;
193 /* Used to count relocations for delayed sizing of relocation
194 sections. */
195 struct elf32_hppa_dyn_reloc_entry {
197 /* Next relocation in the chain. */
198 struct elf32_hppa_dyn_reloc_entry *next;
200 /* The input section of the reloc. */
201 asection *sec;
203 /* Number of relocs copied in this section. */
204 bfd_size_type count;
206 #if RELATIVE_DYNRELOCS
207 /* Number of relative relocs copied for the input section. */
208 bfd_size_type relative_count;
209 #endif
210 } *dyn_relocs;
212 /* Set if this symbol is used by a plabel reloc. */
213 unsigned int plabel:1;
216 struct elf32_hppa_link_hash_table {
218 /* The main hash table. */
219 struct elf_link_hash_table elf;
221 /* The stub hash table. */
222 struct bfd_hash_table stub_hash_table;
224 /* Linker stub bfd. */
225 bfd *stub_bfd;
227 /* Linker call-backs. */
228 asection * (*add_stub_section) (const char *, asection *);
229 void (*layout_sections_again) (void);
231 /* Array to keep track of which stub sections have been created, and
232 information on stub grouping. */
233 struct map_stub {
234 /* This is the section to which stubs in the group will be
235 attached. */
236 asection *link_sec;
237 /* The stub section. */
238 asection *stub_sec;
239 } *stub_group;
241 /* Assorted information used by elf32_hppa_size_stubs. */
242 unsigned int bfd_count;
243 int top_index;
244 asection **input_list;
245 Elf_Internal_Sym **all_local_syms;
247 /* Short-cuts to get to dynamic linker sections. */
248 asection *sgot;
249 asection *srelgot;
250 asection *splt;
251 asection *srelplt;
252 asection *sdynbss;
253 asection *srelbss;
255 /* Used during a final link to store the base of the text and data
256 segments so that we can perform SEGREL relocations. */
257 bfd_vma text_segment_base;
258 bfd_vma data_segment_base;
260 /* Whether we support multiple sub-spaces for shared libs. */
261 unsigned int multi_subspace:1;
263 /* Flags set when various size branches are detected. Used to
264 select suitable defaults for the stub group size. */
265 unsigned int has_12bit_branch:1;
266 unsigned int has_17bit_branch:1;
267 unsigned int has_22bit_branch:1;
269 /* Set if we need a .plt stub to support lazy dynamic linking. */
270 unsigned int need_plt_stub:1;
272 /* Small local sym to section mapping cache. */
273 struct sym_sec_cache sym_sec;
276 /* Various hash macros and functions. */
277 #define hppa_link_hash_table(p) \
278 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
280 #define hppa_stub_hash_lookup(table, string, create, copy) \
281 ((struct elf32_hppa_stub_hash_entry *) \
282 bfd_hash_lookup ((table), (string), (create), (copy)))
284 /* Assorted hash table functions. */
286 /* Initialize an entry in the stub hash table. */
288 static struct bfd_hash_entry *
289 stub_hash_newfunc (struct bfd_hash_entry *entry,
290 struct bfd_hash_table *table,
291 const char *string)
293 /* Allocate the structure if it has not already been allocated by a
294 subclass. */
295 if (entry == NULL)
297 entry = bfd_hash_allocate (table,
298 sizeof (struct elf32_hppa_stub_hash_entry));
299 if (entry == NULL)
300 return entry;
303 /* Call the allocation method of the superclass. */
304 entry = bfd_hash_newfunc (entry, table, string);
305 if (entry != NULL)
307 struct elf32_hppa_stub_hash_entry *eh;
309 /* Initialize the local fields. */
310 eh = (struct elf32_hppa_stub_hash_entry *) entry;
311 eh->stub_sec = NULL;
312 eh->stub_offset = 0;
313 eh->target_value = 0;
314 eh->target_section = NULL;
315 eh->stub_type = hppa_stub_long_branch;
316 eh->h = NULL;
317 eh->id_sec = NULL;
320 return entry;
323 /* Initialize an entry in the link hash table. */
325 static struct bfd_hash_entry *
326 hppa_link_hash_newfunc (struct bfd_hash_entry *entry,
327 struct bfd_hash_table *table,
328 const char *string)
330 /* Allocate the structure if it has not already been allocated by a
331 subclass. */
332 if (entry == NULL)
334 entry = bfd_hash_allocate (table,
335 sizeof (struct elf32_hppa_link_hash_entry));
336 if (entry == NULL)
337 return entry;
340 /* Call the allocation method of the superclass. */
341 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
342 if (entry != NULL)
344 struct elf32_hppa_link_hash_entry *eh;
346 /* Initialize the local fields. */
347 eh = (struct elf32_hppa_link_hash_entry *) entry;
348 eh->stub_cache = NULL;
349 eh->dyn_relocs = NULL;
350 eh->plabel = 0;
353 return entry;
356 /* Create the derived linker hash table. The PA ELF port uses the derived
357 hash table to keep information specific to the PA ELF linker (without
358 using static variables). */
360 static struct bfd_link_hash_table *
361 elf32_hppa_link_hash_table_create (bfd *abfd)
363 struct elf32_hppa_link_hash_table *ret;
364 bfd_size_type amt = sizeof (*ret);
366 ret = bfd_malloc (amt);
367 if (ret == NULL)
368 return NULL;
370 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, hppa_link_hash_newfunc))
372 free (ret);
373 return NULL;
376 /* Init the stub hash table too. */
377 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc))
378 return NULL;
380 ret->stub_bfd = NULL;
381 ret->add_stub_section = NULL;
382 ret->layout_sections_again = NULL;
383 ret->stub_group = NULL;
384 ret->sgot = NULL;
385 ret->srelgot = NULL;
386 ret->splt = NULL;
387 ret->srelplt = NULL;
388 ret->sdynbss = NULL;
389 ret->srelbss = NULL;
390 ret->text_segment_base = (bfd_vma) -1;
391 ret->data_segment_base = (bfd_vma) -1;
392 ret->multi_subspace = 0;
393 ret->has_12bit_branch = 0;
394 ret->has_17bit_branch = 0;
395 ret->has_22bit_branch = 0;
396 ret->need_plt_stub = 0;
397 ret->sym_sec.abfd = NULL;
399 return &ret->elf.root;
402 /* Free the derived linker hash table. */
404 static void
405 elf32_hppa_link_hash_table_free (struct bfd_link_hash_table *hash)
407 struct elf32_hppa_link_hash_table *ret
408 = (struct elf32_hppa_link_hash_table *) hash;
410 bfd_hash_table_free (&ret->stub_hash_table);
411 _bfd_generic_link_hash_table_free (hash);
414 /* Build a name for an entry in the stub hash table. */
416 static char *
417 hppa_stub_name (const asection *input_section,
418 const asection *sym_sec,
419 const struct elf32_hppa_link_hash_entry *hash,
420 const Elf_Internal_Rela *rel)
422 char *stub_name;
423 bfd_size_type len;
425 if (hash)
427 len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
428 stub_name = bfd_malloc (len);
429 if (stub_name != NULL)
431 sprintf (stub_name, "%08x_%s+%x",
432 input_section->id & 0xffffffff,
433 hash->elf.root.root.string,
434 (int) rel->r_addend & 0xffffffff);
437 else
439 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
440 stub_name = bfd_malloc (len);
441 if (stub_name != NULL)
443 sprintf (stub_name, "%08x_%x:%x+%x",
444 input_section->id & 0xffffffff,
445 sym_sec->id & 0xffffffff,
446 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
447 (int) rel->r_addend & 0xffffffff);
450 return stub_name;
453 /* Look up an entry in the stub hash. Stub entries are cached because
454 creating the stub name takes a bit of time. */
456 static struct elf32_hppa_stub_hash_entry *
457 hppa_get_stub_entry (const asection *input_section,
458 const asection *sym_sec,
459 struct elf32_hppa_link_hash_entry *hash,
460 const Elf_Internal_Rela *rel,
461 struct elf32_hppa_link_hash_table *htab)
463 struct elf32_hppa_stub_hash_entry *stub_entry;
464 const asection *id_sec;
466 /* If this input section is part of a group of sections sharing one
467 stub section, then use the id of the first section in the group.
468 Stub names need to include a section id, as there may well be
469 more than one stub used to reach say, printf, and we need to
470 distinguish between them. */
471 id_sec = htab->stub_group[input_section->id].link_sec;
473 if (hash != NULL && hash->stub_cache != NULL
474 && hash->stub_cache->h == hash
475 && hash->stub_cache->id_sec == id_sec)
477 stub_entry = hash->stub_cache;
479 else
481 char *stub_name;
483 stub_name = hppa_stub_name (id_sec, sym_sec, hash, rel);
484 if (stub_name == NULL)
485 return NULL;
487 stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table,
488 stub_name, FALSE, FALSE);
489 if (hash != NULL)
490 hash->stub_cache = stub_entry;
492 free (stub_name);
495 return stub_entry;
498 /* Add a new stub entry to the stub hash. Not all fields of the new
499 stub entry are initialised. */
501 static struct elf32_hppa_stub_hash_entry *
502 hppa_add_stub (const char *stub_name,
503 asection *section,
504 struct elf32_hppa_link_hash_table *htab)
506 asection *link_sec;
507 asection *stub_sec;
508 struct elf32_hppa_stub_hash_entry *stub_entry;
510 link_sec = htab->stub_group[section->id].link_sec;
511 stub_sec = htab->stub_group[section->id].stub_sec;
512 if (stub_sec == NULL)
514 stub_sec = htab->stub_group[link_sec->id].stub_sec;
515 if (stub_sec == NULL)
517 size_t namelen;
518 bfd_size_type len;
519 char *s_name;
521 namelen = strlen (link_sec->name);
522 len = namelen + sizeof (STUB_SUFFIX);
523 s_name = bfd_alloc (htab->stub_bfd, len);
524 if (s_name == NULL)
525 return NULL;
527 memcpy (s_name, link_sec->name, namelen);
528 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
529 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
530 if (stub_sec == NULL)
531 return NULL;
532 htab->stub_group[link_sec->id].stub_sec = stub_sec;
534 htab->stub_group[section->id].stub_sec = stub_sec;
537 /* Enter this entry into the linker stub hash table. */
538 stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table, stub_name,
539 TRUE, FALSE);
540 if (stub_entry == NULL)
542 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
543 section->owner,
544 stub_name);
545 return NULL;
548 stub_entry->stub_sec = stub_sec;
549 stub_entry->stub_offset = 0;
550 stub_entry->id_sec = link_sec;
551 return stub_entry;
554 /* Determine the type of stub needed, if any, for a call. */
556 static enum elf32_hppa_stub_type
557 hppa_type_of_stub (asection *input_sec,
558 const Elf_Internal_Rela *rel,
559 struct elf32_hppa_link_hash_entry *hash,
560 bfd_vma destination,
561 struct bfd_link_info *info)
563 bfd_vma location;
564 bfd_vma branch_offset;
565 bfd_vma max_branch_offset;
566 unsigned int r_type;
568 if (hash != NULL
569 && hash->elf.plt.offset != (bfd_vma) -1
570 && hash->elf.dynindx != -1
571 && !hash->plabel
572 && (info->shared
573 || !hash->elf.def_regular
574 || hash->elf.root.type == bfd_link_hash_defweak))
576 /* We need an import stub. Decide between hppa_stub_import
577 and hppa_stub_import_shared later. */
578 return hppa_stub_import;
581 /* Determine where the call point is. */
582 location = (input_sec->output_offset
583 + input_sec->output_section->vma
584 + rel->r_offset);
586 branch_offset = destination - location - 8;
587 r_type = ELF32_R_TYPE (rel->r_info);
589 /* Determine if a long branch stub is needed. parisc branch offsets
590 are relative to the second instruction past the branch, ie. +8
591 bytes on from the branch instruction location. The offset is
592 signed and counts in units of 4 bytes. */
593 if (r_type == (unsigned int) R_PARISC_PCREL17F)
595 max_branch_offset = (1 << (17-1)) << 2;
597 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
599 max_branch_offset = (1 << (12-1)) << 2;
601 else /* R_PARISC_PCREL22F. */
603 max_branch_offset = (1 << (22-1)) << 2;
606 if (branch_offset + max_branch_offset >= 2*max_branch_offset)
607 return hppa_stub_long_branch;
609 return hppa_stub_none;
612 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
613 IN_ARG contains the link info pointer. */
615 #define LDIL_R1 0x20200000 /* ldil LR'XXX,%r1 */
616 #define BE_SR4_R1 0xe0202002 /* be,n RR'XXX(%sr4,%r1) */
618 #define BL_R1 0xe8200000 /* b,l .+8,%r1 */
619 #define ADDIL_R1 0x28200000 /* addil LR'XXX,%r1,%r1 */
620 #define DEPI_R1 0xd4201c1e /* depi 0,31,2,%r1 */
622 #define ADDIL_DP 0x2b600000 /* addil LR'XXX,%dp,%r1 */
623 #define LDW_R1_R21 0x48350000 /* ldw RR'XXX(%sr0,%r1),%r21 */
624 #define BV_R0_R21 0xeaa0c000 /* bv %r0(%r21) */
625 #define LDW_R1_R19 0x48330000 /* ldw RR'XXX(%sr0,%r1),%r19 */
627 #define ADDIL_R19 0x2a600000 /* addil LR'XXX,%r19,%r1 */
628 #define LDW_R1_DP 0x483b0000 /* ldw RR'XXX(%sr0,%r1),%dp */
630 #define LDSID_R21_R1 0x02a010a1 /* ldsid (%sr0,%r21),%r1 */
631 #define MTSP_R1 0x00011820 /* mtsp %r1,%sr0 */
632 #define BE_SR0_R21 0xe2a00000 /* be 0(%sr0,%r21) */
633 #define STW_RP 0x6bc23fd1 /* stw %rp,-24(%sr0,%sp) */
635 #define BL22_RP 0xe800a002 /* b,l,n XXX,%rp */
636 #define BL_RP 0xe8400002 /* b,l,n XXX,%rp */
637 #define NOP 0x08000240 /* nop */
638 #define LDW_RP 0x4bc23fd1 /* ldw -24(%sr0,%sp),%rp */
639 #define LDSID_RP_R1 0x004010a1 /* ldsid (%sr0,%rp),%r1 */
640 #define BE_SR0_RP 0xe0400002 /* be,n 0(%sr0,%rp) */
642 #ifndef R19_STUBS
643 #define R19_STUBS 1
644 #endif
646 #if R19_STUBS
647 #define LDW_R1_DLT LDW_R1_R19
648 #else
649 #define LDW_R1_DLT LDW_R1_DP
650 #endif
652 static bfd_boolean
653 hppa_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
655 struct elf32_hppa_stub_hash_entry *stub_entry;
656 struct bfd_link_info *info;
657 struct elf32_hppa_link_hash_table *htab;
658 asection *stub_sec;
659 bfd *stub_bfd;
660 bfd_byte *loc;
661 bfd_vma sym_value;
662 bfd_vma insn;
663 bfd_vma off;
664 int val;
665 int size;
667 /* Massage our args to the form they really have. */
668 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
669 info = in_arg;
671 htab = hppa_link_hash_table (info);
672 stub_sec = stub_entry->stub_sec;
674 /* Make a note of the offset within the stubs for this entry. */
675 stub_entry->stub_offset = stub_sec->size;
676 loc = stub_sec->contents + stub_entry->stub_offset;
678 stub_bfd = stub_sec->owner;
680 switch (stub_entry->stub_type)
682 case hppa_stub_long_branch:
683 /* Create the long branch. A long branch is formed with "ldil"
684 loading the upper bits of the target address into a register,
685 then branching with "be" which adds in the lower bits.
686 The "be" has its delay slot nullified. */
687 sym_value = (stub_entry->target_value
688 + stub_entry->target_section->output_offset
689 + stub_entry->target_section->output_section->vma);
691 val = hppa_field_adjust (sym_value, 0, e_lrsel);
692 insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
693 bfd_put_32 (stub_bfd, insn, loc);
695 val = hppa_field_adjust (sym_value, 0, e_rrsel) >> 2;
696 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
697 bfd_put_32 (stub_bfd, insn, loc + 4);
699 size = 8;
700 break;
702 case hppa_stub_long_branch_shared:
703 /* Branches are relative. This is where we are going to. */
704 sym_value = (stub_entry->target_value
705 + stub_entry->target_section->output_offset
706 + stub_entry->target_section->output_section->vma);
708 /* And this is where we are coming from, more or less. */
709 sym_value -= (stub_entry->stub_offset
710 + stub_sec->output_offset
711 + stub_sec->output_section->vma);
713 bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
714 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
715 insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
716 bfd_put_32 (stub_bfd, insn, loc + 4);
718 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
719 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
720 bfd_put_32 (stub_bfd, insn, loc + 8);
721 size = 12;
722 break;
724 case hppa_stub_import:
725 case hppa_stub_import_shared:
726 off = stub_entry->h->elf.plt.offset;
727 if (off >= (bfd_vma) -2)
728 abort ();
730 off &= ~ (bfd_vma) 1;
731 sym_value = (off
732 + htab->splt->output_offset
733 + htab->splt->output_section->vma
734 - elf_gp (htab->splt->output_section->owner));
736 insn = ADDIL_DP;
737 #if R19_STUBS
738 if (stub_entry->stub_type == hppa_stub_import_shared)
739 insn = ADDIL_R19;
740 #endif
741 val = hppa_field_adjust (sym_value, 0, e_lrsel),
742 insn = hppa_rebuild_insn ((int) insn, val, 21);
743 bfd_put_32 (stub_bfd, insn, loc);
745 /* It is critical to use lrsel/rrsel here because we are using
746 two different offsets (+0 and +4) from sym_value. If we use
747 lsel/rsel then with unfortunate sym_values we will round
748 sym_value+4 up to the next 2k block leading to a mis-match
749 between the lsel and rsel value. */
750 val = hppa_field_adjust (sym_value, 0, e_rrsel);
751 insn = hppa_rebuild_insn ((int) LDW_R1_R21, val, 14);
752 bfd_put_32 (stub_bfd, insn, loc + 4);
754 if (htab->multi_subspace)
756 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
757 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
758 bfd_put_32 (stub_bfd, insn, loc + 8);
760 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
761 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
762 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21, loc + 20);
763 bfd_put_32 (stub_bfd, (bfd_vma) STW_RP, loc + 24);
765 size = 28;
767 else
769 bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 8);
770 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
771 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
772 bfd_put_32 (stub_bfd, insn, loc + 12);
774 size = 16;
777 break;
779 case hppa_stub_export:
780 /* Branches are relative. This is where we are going to. */
781 sym_value = (stub_entry->target_value
782 + stub_entry->target_section->output_offset
783 + stub_entry->target_section->output_section->vma);
785 /* And this is where we are coming from. */
786 sym_value -= (stub_entry->stub_offset
787 + stub_sec->output_offset
788 + stub_sec->output_section->vma);
790 if (sym_value - 8 + (1 << (17 + 1)) >= (1 << (17 + 2))
791 && (!htab->has_22bit_branch
792 || sym_value - 8 + (1 << (22 + 1)) >= (1 << (22 + 2))))
794 (*_bfd_error_handler)
795 (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
796 stub_entry->target_section->owner,
797 stub_sec,
798 (long) stub_entry->stub_offset,
799 stub_entry->root.string);
800 bfd_set_error (bfd_error_bad_value);
801 return FALSE;
804 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
805 if (!htab->has_22bit_branch)
806 insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
807 else
808 insn = hppa_rebuild_insn ((int) BL22_RP, val, 22);
809 bfd_put_32 (stub_bfd, insn, loc);
811 bfd_put_32 (stub_bfd, (bfd_vma) NOP, loc + 4);
812 bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP, loc + 8);
813 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
814 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
815 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP, loc + 20);
817 /* Point the function symbol at the stub. */
818 stub_entry->h->elf.root.u.def.section = stub_sec;
819 stub_entry->h->elf.root.u.def.value = stub_sec->size;
821 size = 24;
822 break;
824 default:
825 BFD_FAIL ();
826 return FALSE;
829 stub_sec->size += size;
830 return TRUE;
833 #undef LDIL_R1
834 #undef BE_SR4_R1
835 #undef BL_R1
836 #undef ADDIL_R1
837 #undef DEPI_R1
838 #undef LDW_R1_R21
839 #undef LDW_R1_DLT
840 #undef LDW_R1_R19
841 #undef ADDIL_R19
842 #undef LDW_R1_DP
843 #undef LDSID_R21_R1
844 #undef MTSP_R1
845 #undef BE_SR0_R21
846 #undef STW_RP
847 #undef BV_R0_R21
848 #undef BL_RP
849 #undef NOP
850 #undef LDW_RP
851 #undef LDSID_RP_R1
852 #undef BE_SR0_RP
854 /* As above, but don't actually build the stub. Just bump offset so
855 we know stub section sizes. */
857 static bfd_boolean
858 hppa_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
860 struct elf32_hppa_stub_hash_entry *stub_entry;
861 struct elf32_hppa_link_hash_table *htab;
862 int size;
864 /* Massage our args to the form they really have. */
865 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
866 htab = in_arg;
868 if (stub_entry->stub_type == hppa_stub_long_branch)
869 size = 8;
870 else if (stub_entry->stub_type == hppa_stub_long_branch_shared)
871 size = 12;
872 else if (stub_entry->stub_type == hppa_stub_export)
873 size = 24;
874 else /* hppa_stub_import or hppa_stub_import_shared. */
876 if (htab->multi_subspace)
877 size = 28;
878 else
879 size = 16;
882 stub_entry->stub_sec->size += size;
883 return TRUE;
886 /* Return nonzero if ABFD represents an HPPA ELF32 file.
887 Additionally we set the default architecture and machine. */
889 static bfd_boolean
890 elf32_hppa_object_p (bfd *abfd)
892 Elf_Internal_Ehdr * i_ehdrp;
893 unsigned int flags;
895 i_ehdrp = elf_elfheader (abfd);
896 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
898 /* GCC on hppa-linux produces binaries with OSABI=Linux,
899 but the kernel produces corefiles with OSABI=SysV. */
900 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_LINUX &&
901 i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
902 return FALSE;
904 else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
906 /* GCC on hppa-netbsd produces binaries with OSABI=NetBSD,
907 but the kernel produces corefiles with OSABI=SysV. */
908 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NETBSD &&
909 i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
910 return FALSE;
912 else
914 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX)
915 return FALSE;
918 flags = i_ehdrp->e_flags;
919 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
921 case EFA_PARISC_1_0:
922 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
923 case EFA_PARISC_1_1:
924 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
925 case EFA_PARISC_2_0:
926 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
927 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
928 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
930 return TRUE;
933 /* Create the .plt and .got sections, and set up our hash table
934 short-cuts to various dynamic sections. */
936 static bfd_boolean
937 elf32_hppa_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
939 struct elf32_hppa_link_hash_table *htab;
940 struct elf_link_hash_entry *h;
942 /* Don't try to create the .plt and .got twice. */
943 htab = hppa_link_hash_table (info);
944 if (htab->splt != NULL)
945 return TRUE;
947 /* Call the generic code to do most of the work. */
948 if (! _bfd_elf_create_dynamic_sections (abfd, info))
949 return FALSE;
951 htab->splt = bfd_get_section_by_name (abfd, ".plt");
952 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
954 htab->sgot = bfd_get_section_by_name (abfd, ".got");
955 htab->srelgot = bfd_make_section (abfd, ".rela.got");
956 if (htab->srelgot == NULL
957 || ! bfd_set_section_flags (abfd, htab->srelgot,
958 (SEC_ALLOC
959 | SEC_LOAD
960 | SEC_HAS_CONTENTS
961 | SEC_IN_MEMORY
962 | SEC_LINKER_CREATED
963 | SEC_READONLY))
964 || ! bfd_set_section_alignment (abfd, htab->srelgot, 2))
965 return FALSE;
967 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
968 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
970 /* hppa-linux needs _GLOBAL_OFFSET_TABLE_ to be visible from the main
971 application, because __canonicalize_funcptr_for_compare needs it. */
972 h = elf_hash_table (info)->hgot;
973 h->forced_local = 0;
974 h->other = STV_DEFAULT;
975 return bfd_elf_link_record_dynamic_symbol (info, h);
978 /* Copy the extra info we tack onto an elf_link_hash_entry. */
980 static void
981 elf32_hppa_copy_indirect_symbol (const struct elf_backend_data *bed,
982 struct elf_link_hash_entry *dir,
983 struct elf_link_hash_entry *ind)
985 struct elf32_hppa_link_hash_entry *edir, *eind;
987 edir = (struct elf32_hppa_link_hash_entry *) dir;
988 eind = (struct elf32_hppa_link_hash_entry *) ind;
990 if (eind->dyn_relocs != NULL)
992 if (edir->dyn_relocs != NULL)
994 struct elf32_hppa_dyn_reloc_entry **pp;
995 struct elf32_hppa_dyn_reloc_entry *p;
997 if (ind->root.type == bfd_link_hash_indirect)
998 abort ();
1000 /* Add reloc counts against the weak sym to the strong sym
1001 list. Merge any entries against the same section. */
1002 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1004 struct elf32_hppa_dyn_reloc_entry *q;
1006 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1007 if (q->sec == p->sec)
1009 #if RELATIVE_DYNRELOCS
1010 q->relative_count += p->relative_count;
1011 #endif
1012 q->count += p->count;
1013 *pp = p->next;
1014 break;
1016 if (q == NULL)
1017 pp = &p->next;
1019 *pp = edir->dyn_relocs;
1022 edir->dyn_relocs = eind->dyn_relocs;
1023 eind->dyn_relocs = NULL;
1026 if (ELIMINATE_COPY_RELOCS
1027 && ind->root.type != bfd_link_hash_indirect
1028 && dir->dynamic_adjusted)
1030 /* If called to transfer flags for a weakdef during processing
1031 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1032 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1033 dir->ref_dynamic |= ind->ref_dynamic;
1034 dir->ref_regular |= ind->ref_regular;
1035 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1036 dir->needs_plt |= ind->needs_plt;
1038 else
1039 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1042 /* Look through the relocs for a section during the first phase, and
1043 calculate needed space in the global offset table, procedure linkage
1044 table, and dynamic reloc sections. At this point we haven't
1045 necessarily read all the input files. */
1047 static bfd_boolean
1048 elf32_hppa_check_relocs (bfd *abfd,
1049 struct bfd_link_info *info,
1050 asection *sec,
1051 const Elf_Internal_Rela *relocs)
1053 Elf_Internal_Shdr *symtab_hdr;
1054 struct elf_link_hash_entry **sym_hashes;
1055 const Elf_Internal_Rela *rel;
1056 const Elf_Internal_Rela *rel_end;
1057 struct elf32_hppa_link_hash_table *htab;
1058 asection *sreloc;
1059 asection *stubreloc;
1061 if (info->relocatable)
1062 return TRUE;
1064 htab = hppa_link_hash_table (info);
1065 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1066 sym_hashes = elf_sym_hashes (abfd);
1067 sreloc = NULL;
1068 stubreloc = NULL;
1070 rel_end = relocs + sec->reloc_count;
1071 for (rel = relocs; rel < rel_end; rel++)
1073 enum {
1074 NEED_GOT = 1,
1075 NEED_PLT = 2,
1076 NEED_DYNREL = 4,
1077 PLT_PLABEL = 8
1080 unsigned int r_symndx, r_type;
1081 struct elf32_hppa_link_hash_entry *h;
1082 int need_entry;
1084 r_symndx = ELF32_R_SYM (rel->r_info);
1086 if (r_symndx < symtab_hdr->sh_info)
1087 h = NULL;
1088 else
1089 h = ((struct elf32_hppa_link_hash_entry *)
1090 sym_hashes[r_symndx - symtab_hdr->sh_info]);
1092 r_type = ELF32_R_TYPE (rel->r_info);
1094 switch (r_type)
1096 case R_PARISC_DLTIND14F:
1097 case R_PARISC_DLTIND14R:
1098 case R_PARISC_DLTIND21L:
1099 /* This symbol requires a global offset table entry. */
1100 need_entry = NEED_GOT;
1101 break;
1103 case R_PARISC_PLABEL14R: /* "Official" procedure labels. */
1104 case R_PARISC_PLABEL21L:
1105 case R_PARISC_PLABEL32:
1106 /* If the addend is non-zero, we break badly. */
1107 if (rel->r_addend != 0)
1108 abort ();
1110 /* If we are creating a shared library, then we need to
1111 create a PLT entry for all PLABELs, because PLABELs with
1112 local symbols may be passed via a pointer to another
1113 object. Additionally, output a dynamic relocation
1114 pointing to the PLT entry.
1115 For executables, the original 32-bit ABI allowed two
1116 different styles of PLABELs (function pointers): For
1117 global functions, the PLABEL word points into the .plt
1118 two bytes past a (function address, gp) pair, and for
1119 local functions the PLABEL points directly at the
1120 function. The magic +2 for the first type allows us to
1121 differentiate between the two. As you can imagine, this
1122 is a real pain when it comes to generating code to call
1123 functions indirectly or to compare function pointers.
1124 We avoid the mess by always pointing a PLABEL into the
1125 .plt, even for local functions. */
1126 need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
1127 break;
1129 case R_PARISC_PCREL12F:
1130 htab->has_12bit_branch = 1;
1131 goto branch_common;
1133 case R_PARISC_PCREL17C:
1134 case R_PARISC_PCREL17F:
1135 htab->has_17bit_branch = 1;
1136 goto branch_common;
1138 case R_PARISC_PCREL22F:
1139 htab->has_22bit_branch = 1;
1140 branch_common:
1141 /* Function calls might need to go through the .plt, and
1142 might require long branch stubs. */
1143 if (h == NULL)
1145 /* We know local syms won't need a .plt entry, and if
1146 they need a long branch stub we can't guarantee that
1147 we can reach the stub. So just flag an error later
1148 if we're doing a shared link and find we need a long
1149 branch stub. */
1150 continue;
1152 else
1154 /* Global symbols will need a .plt entry if they remain
1155 global, and in most cases won't need a long branch
1156 stub. Unfortunately, we have to cater for the case
1157 where a symbol is forced local by versioning, or due
1158 to symbolic linking, and we lose the .plt entry. */
1159 need_entry = NEED_PLT;
1160 if (h->elf.type == STT_PARISC_MILLI)
1161 need_entry = 0;
1163 break;
1165 case R_PARISC_SEGBASE: /* Used to set segment base. */
1166 case R_PARISC_SEGREL32: /* Relative reloc, used for unwind. */
1167 case R_PARISC_PCREL14F: /* PC relative load/store. */
1168 case R_PARISC_PCREL14R:
1169 case R_PARISC_PCREL17R: /* External branches. */
1170 case R_PARISC_PCREL21L: /* As above, and for load/store too. */
1171 case R_PARISC_PCREL32:
1172 /* We don't need to propagate the relocation if linking a
1173 shared object since these are section relative. */
1174 continue;
1176 case R_PARISC_DPREL14F: /* Used for gp rel data load/store. */
1177 case R_PARISC_DPREL14R:
1178 case R_PARISC_DPREL21L:
1179 if (info->shared)
1181 (*_bfd_error_handler)
1182 (_("%B: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1183 abfd,
1184 elf_hppa_howto_table[r_type].name);
1185 bfd_set_error (bfd_error_bad_value);
1186 return FALSE;
1188 /* Fall through. */
1190 case R_PARISC_DIR17F: /* Used for external branches. */
1191 case R_PARISC_DIR17R:
1192 case R_PARISC_DIR14F: /* Used for load/store from absolute locn. */
1193 case R_PARISC_DIR14R:
1194 case R_PARISC_DIR21L: /* As above, and for ext branches too. */
1195 case R_PARISC_DIR32: /* .word relocs. */
1196 /* We may want to output a dynamic relocation later. */
1197 need_entry = NEED_DYNREL;
1198 break;
1200 /* This relocation describes the C++ object vtable hierarchy.
1201 Reconstruct it for later use during GC. */
1202 case R_PARISC_GNU_VTINHERIT:
1203 if (!bfd_elf_gc_record_vtinherit (abfd, sec, &h->elf, rel->r_offset))
1204 return FALSE;
1205 continue;
1207 /* This relocation describes which C++ vtable entries are actually
1208 used. Record for later use during GC. */
1209 case R_PARISC_GNU_VTENTRY:
1210 if (!bfd_elf_gc_record_vtentry (abfd, sec, &h->elf, rel->r_addend))
1211 return FALSE;
1212 continue;
1214 default:
1215 continue;
1218 /* Now carry out our orders. */
1219 if (need_entry & NEED_GOT)
1221 /* Allocate space for a GOT entry, as well as a dynamic
1222 relocation for this entry. */
1223 if (htab->sgot == NULL)
1225 if (htab->elf.dynobj == NULL)
1226 htab->elf.dynobj = abfd;
1227 if (!elf32_hppa_create_dynamic_sections (htab->elf.dynobj, info))
1228 return FALSE;
1231 if (h != NULL)
1233 h->elf.got.refcount += 1;
1235 else
1237 bfd_signed_vma *local_got_refcounts;
1239 /* This is a global offset table entry for a local symbol. */
1240 local_got_refcounts = elf_local_got_refcounts (abfd);
1241 if (local_got_refcounts == NULL)
1243 bfd_size_type size;
1245 /* Allocate space for local got offsets and local
1246 plt offsets. Done this way to save polluting
1247 elf_obj_tdata with another target specific
1248 pointer. */
1249 size = symtab_hdr->sh_info;
1250 size *= 2 * sizeof (bfd_signed_vma);
1251 local_got_refcounts = bfd_zalloc (abfd, size);
1252 if (local_got_refcounts == NULL)
1253 return FALSE;
1254 elf_local_got_refcounts (abfd) = local_got_refcounts;
1256 local_got_refcounts[r_symndx] += 1;
1260 if (need_entry & NEED_PLT)
1262 /* If we are creating a shared library, and this is a reloc
1263 against a weak symbol or a global symbol in a dynamic
1264 object, then we will be creating an import stub and a
1265 .plt entry for the symbol. Similarly, on a normal link
1266 to symbols defined in a dynamic object we'll need the
1267 import stub and a .plt entry. We don't know yet whether
1268 the symbol is defined or not, so make an entry anyway and
1269 clean up later in adjust_dynamic_symbol. */
1270 if ((sec->flags & SEC_ALLOC) != 0)
1272 if (h != NULL)
1274 h->elf.needs_plt = 1;
1275 h->elf.plt.refcount += 1;
1277 /* If this .plt entry is for a plabel, mark it so
1278 that adjust_dynamic_symbol will keep the entry
1279 even if it appears to be local. */
1280 if (need_entry & PLT_PLABEL)
1281 h->plabel = 1;
1283 else if (need_entry & PLT_PLABEL)
1285 bfd_signed_vma *local_got_refcounts;
1286 bfd_signed_vma *local_plt_refcounts;
1288 local_got_refcounts = elf_local_got_refcounts (abfd);
1289 if (local_got_refcounts == NULL)
1291 bfd_size_type size;
1293 /* Allocate space for local got offsets and local
1294 plt offsets. */
1295 size = symtab_hdr->sh_info;
1296 size *= 2 * sizeof (bfd_signed_vma);
1297 local_got_refcounts = bfd_zalloc (abfd, size);
1298 if (local_got_refcounts == NULL)
1299 return FALSE;
1300 elf_local_got_refcounts (abfd) = local_got_refcounts;
1302 local_plt_refcounts = (local_got_refcounts
1303 + symtab_hdr->sh_info);
1304 local_plt_refcounts[r_symndx] += 1;
1309 if (need_entry & NEED_DYNREL)
1311 /* Flag this symbol as having a non-got, non-plt reference
1312 so that we generate copy relocs if it turns out to be
1313 dynamic. */
1314 if (h != NULL && !info->shared)
1315 h->elf.non_got_ref = 1;
1317 /* If we are creating a shared library then we need to copy
1318 the reloc into the shared library. However, if we are
1319 linking with -Bsymbolic, we need only copy absolute
1320 relocs or relocs against symbols that are not defined in
1321 an object we are including in the link. PC- or DP- or
1322 DLT-relative relocs against any local sym or global sym
1323 with DEF_REGULAR set, can be discarded. At this point we
1324 have not seen all the input files, so it is possible that
1325 DEF_REGULAR is not set now but will be set later (it is
1326 never cleared). We account for that possibility below by
1327 storing information in the dyn_relocs field of the
1328 hash table entry.
1330 A similar situation to the -Bsymbolic case occurs when
1331 creating shared libraries and symbol visibility changes
1332 render the symbol local.
1334 As it turns out, all the relocs we will be creating here
1335 are absolute, so we cannot remove them on -Bsymbolic
1336 links or visibility changes anyway. A STUB_REL reloc
1337 is absolute too, as in that case it is the reloc in the
1338 stub we will be creating, rather than copying the PCREL
1339 reloc in the branch.
1341 If on the other hand, we are creating an executable, we
1342 may need to keep relocations for symbols satisfied by a
1343 dynamic library if we manage to avoid copy relocs for the
1344 symbol. */
1345 if ((info->shared
1346 && (sec->flags & SEC_ALLOC) != 0
1347 && (IS_ABSOLUTE_RELOC (r_type)
1348 || (h != NULL
1349 && (!info->symbolic
1350 || h->elf.root.type == bfd_link_hash_defweak
1351 || !h->elf.def_regular))))
1352 || (ELIMINATE_COPY_RELOCS
1353 && !info->shared
1354 && (sec->flags & SEC_ALLOC) != 0
1355 && h != NULL
1356 && (h->elf.root.type == bfd_link_hash_defweak
1357 || !h->elf.def_regular)))
1359 struct elf32_hppa_dyn_reloc_entry *p;
1360 struct elf32_hppa_dyn_reloc_entry **head;
1362 /* Create a reloc section in dynobj and make room for
1363 this reloc. */
1364 if (sreloc == NULL)
1366 char *name;
1367 bfd *dynobj;
1369 name = (bfd_elf_string_from_elf_section
1370 (abfd,
1371 elf_elfheader (abfd)->e_shstrndx,
1372 elf_section_data (sec)->rel_hdr.sh_name));
1373 if (name == NULL)
1375 (*_bfd_error_handler)
1376 (_("Could not find relocation section for %s"),
1377 sec->name);
1378 bfd_set_error (bfd_error_bad_value);
1379 return FALSE;
1382 if (htab->elf.dynobj == NULL)
1383 htab->elf.dynobj = abfd;
1385 dynobj = htab->elf.dynobj;
1386 sreloc = bfd_get_section_by_name (dynobj, name);
1387 if (sreloc == NULL)
1389 flagword flags;
1391 sreloc = bfd_make_section (dynobj, name);
1392 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1393 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1394 if ((sec->flags & SEC_ALLOC) != 0)
1395 flags |= SEC_ALLOC | SEC_LOAD;
1396 if (sreloc == NULL
1397 || !bfd_set_section_flags (dynobj, sreloc, flags)
1398 || !bfd_set_section_alignment (dynobj, sreloc, 2))
1399 return FALSE;
1402 elf_section_data (sec)->sreloc = sreloc;
1405 /* If this is a global symbol, we count the number of
1406 relocations we need for this symbol. */
1407 if (h != NULL)
1409 head = &h->dyn_relocs;
1411 else
1413 /* Track dynamic relocs needed for local syms too.
1414 We really need local syms available to do this
1415 easily. Oh well. */
1417 asection *s;
1418 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1419 sec, r_symndx);
1420 if (s == NULL)
1421 return FALSE;
1423 head = ((struct elf32_hppa_dyn_reloc_entry **)
1424 &elf_section_data (s)->local_dynrel);
1427 p = *head;
1428 if (p == NULL || p->sec != sec)
1430 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
1431 if (p == NULL)
1432 return FALSE;
1433 p->next = *head;
1434 *head = p;
1435 p->sec = sec;
1436 p->count = 0;
1437 #if RELATIVE_DYNRELOCS
1438 p->relative_count = 0;
1439 #endif
1442 p->count += 1;
1443 #if RELATIVE_DYNRELOCS
1444 if (!IS_ABSOLUTE_RELOC (rtype))
1445 p->relative_count += 1;
1446 #endif
1451 return TRUE;
1454 /* Return the section that should be marked against garbage collection
1455 for a given relocation. */
1457 static asection *
1458 elf32_hppa_gc_mark_hook (asection *sec,
1459 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1460 Elf_Internal_Rela *rel,
1461 struct elf_link_hash_entry *h,
1462 Elf_Internal_Sym *sym)
1464 if (h != NULL)
1466 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1468 case R_PARISC_GNU_VTINHERIT:
1469 case R_PARISC_GNU_VTENTRY:
1470 break;
1472 default:
1473 switch (h->root.type)
1475 case bfd_link_hash_defined:
1476 case bfd_link_hash_defweak:
1477 return h->root.u.def.section;
1479 case bfd_link_hash_common:
1480 return h->root.u.c.p->section;
1482 default:
1483 break;
1487 else
1488 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1490 return NULL;
1493 /* Update the got and plt entry reference counts for the section being
1494 removed. */
1496 static bfd_boolean
1497 elf32_hppa_gc_sweep_hook (bfd *abfd,
1498 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1499 asection *sec,
1500 const Elf_Internal_Rela *relocs)
1502 Elf_Internal_Shdr *symtab_hdr;
1503 struct elf_link_hash_entry **sym_hashes;
1504 bfd_signed_vma *local_got_refcounts;
1505 bfd_signed_vma *local_plt_refcounts;
1506 const Elf_Internal_Rela *rel, *relend;
1508 elf_section_data (sec)->local_dynrel = NULL;
1510 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1511 sym_hashes = elf_sym_hashes (abfd);
1512 local_got_refcounts = elf_local_got_refcounts (abfd);
1513 local_plt_refcounts = local_got_refcounts;
1514 if (local_plt_refcounts != NULL)
1515 local_plt_refcounts += symtab_hdr->sh_info;
1517 relend = relocs + sec->reloc_count;
1518 for (rel = relocs; rel < relend; rel++)
1520 unsigned long r_symndx;
1521 unsigned int r_type;
1522 struct elf_link_hash_entry *h = NULL;
1524 r_symndx = ELF32_R_SYM (rel->r_info);
1525 if (r_symndx >= symtab_hdr->sh_info)
1527 struct elf32_hppa_link_hash_entry *eh;
1528 struct elf32_hppa_dyn_reloc_entry **pp;
1529 struct elf32_hppa_dyn_reloc_entry *p;
1531 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1532 while (h->root.type == bfd_link_hash_indirect
1533 || h->root.type == bfd_link_hash_warning)
1534 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1535 eh = (struct elf32_hppa_link_hash_entry *) h;
1537 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1538 if (p->sec == sec)
1540 /* Everything must go for SEC. */
1541 *pp = p->next;
1542 break;
1546 r_type = ELF32_R_TYPE (rel->r_info);
1547 switch (r_type)
1549 case R_PARISC_DLTIND14F:
1550 case R_PARISC_DLTIND14R:
1551 case R_PARISC_DLTIND21L:
1552 if (h != NULL)
1554 if (h->got.refcount > 0)
1555 h->got.refcount -= 1;
1557 else if (local_got_refcounts != NULL)
1559 if (local_got_refcounts[r_symndx] > 0)
1560 local_got_refcounts[r_symndx] -= 1;
1562 break;
1564 case R_PARISC_PCREL12F:
1565 case R_PARISC_PCREL17C:
1566 case R_PARISC_PCREL17F:
1567 case R_PARISC_PCREL22F:
1568 if (h != NULL)
1570 if (h->plt.refcount > 0)
1571 h->plt.refcount -= 1;
1573 break;
1575 case R_PARISC_PLABEL14R:
1576 case R_PARISC_PLABEL21L:
1577 case R_PARISC_PLABEL32:
1578 if (h != NULL)
1580 if (h->plt.refcount > 0)
1581 h->plt.refcount -= 1;
1583 else if (local_plt_refcounts != NULL)
1585 if (local_plt_refcounts[r_symndx] > 0)
1586 local_plt_refcounts[r_symndx] -= 1;
1588 break;
1590 default:
1591 break;
1595 return TRUE;
1598 /* Support for core dump NOTE sections. */
1600 static bfd_boolean
1601 elf32_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1603 int offset;
1604 size_t size;
1606 switch (note->descsz)
1608 default:
1609 return FALSE;
1611 case 396: /* Linux/hppa */
1612 /* pr_cursig */
1613 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1615 /* pr_pid */
1616 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1618 /* pr_reg */
1619 offset = 72;
1620 size = 320;
1622 break;
1625 /* Make a ".reg/999" section. */
1626 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1627 size, note->descpos + offset);
1630 static bfd_boolean
1631 elf32_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1633 switch (note->descsz)
1635 default:
1636 return FALSE;
1638 case 124: /* Linux/hppa elf_prpsinfo. */
1639 elf_tdata (abfd)->core_program
1640 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1641 elf_tdata (abfd)->core_command
1642 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1645 /* Note that for some reason, a spurious space is tacked
1646 onto the end of the args in some (at least one anyway)
1647 implementations, so strip it off if it exists. */
1649 char *command = elf_tdata (abfd)->core_command;
1650 int n = strlen (command);
1652 if (0 < n && command[n - 1] == ' ')
1653 command[n - 1] = '\0';
1656 return TRUE;
1659 /* Our own version of hide_symbol, so that we can keep plt entries for
1660 plabels. */
1662 static void
1663 elf32_hppa_hide_symbol (struct bfd_link_info *info,
1664 struct elf_link_hash_entry *h,
1665 bfd_boolean force_local)
1667 if (force_local)
1669 h->forced_local = 1;
1670 if (h->dynindx != -1)
1672 h->dynindx = -1;
1673 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1674 h->dynstr_index);
1678 if (! ((struct elf32_hppa_link_hash_entry *) h)->plabel)
1680 h->needs_plt = 0;
1681 h->plt = elf_hash_table (info)->init_refcount;
1685 /* Adjust a symbol defined by a dynamic object and referenced by a
1686 regular object. The current definition is in some section of the
1687 dynamic object, but we're not including those sections. We have to
1688 change the definition to something the rest of the link can
1689 understand. */
1691 static bfd_boolean
1692 elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
1693 struct elf_link_hash_entry *h)
1695 struct elf32_hppa_link_hash_table *htab;
1696 asection *s;
1697 unsigned int power_of_two;
1699 /* If this is a function, put it in the procedure linkage table. We
1700 will fill in the contents of the procedure linkage table later. */
1701 if (h->type == STT_FUNC
1702 || h->needs_plt)
1704 if (h->plt.refcount <= 0
1705 || (h->def_regular
1706 && h->root.type != bfd_link_hash_defweak
1707 && ! ((struct elf32_hppa_link_hash_entry *) h)->plabel
1708 && (!info->shared || info->symbolic)))
1710 /* The .plt entry is not needed when:
1711 a) Garbage collection has removed all references to the
1712 symbol, or
1713 b) We know for certain the symbol is defined in this
1714 object, and it's not a weak definition, nor is the symbol
1715 used by a plabel relocation. Either this object is the
1716 application or we are doing a shared symbolic link. */
1718 h->plt.offset = (bfd_vma) -1;
1719 h->needs_plt = 0;
1722 return TRUE;
1724 else
1725 h->plt.offset = (bfd_vma) -1;
1727 /* If this is a weak symbol, and there is a real definition, the
1728 processor independent code will have arranged for us to see the
1729 real definition first, and we can just use the same value. */
1730 if (h->u.weakdef != NULL)
1732 if (h->u.weakdef->root.type != bfd_link_hash_defined
1733 && h->u.weakdef->root.type != bfd_link_hash_defweak)
1734 abort ();
1735 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1736 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1737 if (ELIMINATE_COPY_RELOCS)
1738 h->non_got_ref = h->u.weakdef->non_got_ref;
1739 return TRUE;
1742 /* This is a reference to a symbol defined by a dynamic object which
1743 is not a function. */
1745 /* If we are creating a shared library, we must presume that the
1746 only references to the symbol are via the global offset table.
1747 For such cases we need not do anything here; the relocations will
1748 be handled correctly by relocate_section. */
1749 if (info->shared)
1750 return TRUE;
1752 /* If there are no references to this symbol that do not use the
1753 GOT, we don't need to generate a copy reloc. */
1754 if (!h->non_got_ref)
1755 return TRUE;
1757 if (ELIMINATE_COPY_RELOCS)
1759 struct elf32_hppa_link_hash_entry *eh;
1760 struct elf32_hppa_dyn_reloc_entry *p;
1762 eh = (struct elf32_hppa_link_hash_entry *) h;
1763 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1765 s = p->sec->output_section;
1766 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1767 break;
1770 /* If we didn't find any dynamic relocs in read-only sections, then
1771 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1772 if (p == NULL)
1774 h->non_got_ref = 0;
1775 return TRUE;
1779 /* We must allocate the symbol in our .dynbss section, which will
1780 become part of the .bss section of the executable. There will be
1781 an entry for this symbol in the .dynsym section. The dynamic
1782 object will contain position independent code, so all references
1783 from the dynamic object to this symbol will go through the global
1784 offset table. The dynamic linker will use the .dynsym entry to
1785 determine the address it must put in the global offset table, so
1786 both the dynamic object and the regular object will refer to the
1787 same memory location for the variable. */
1789 htab = hppa_link_hash_table (info);
1791 /* We must generate a COPY reloc to tell the dynamic linker to
1792 copy the initial value out of the dynamic object and into the
1793 runtime process image. */
1794 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1796 htab->srelbss->size += sizeof (Elf32_External_Rela);
1797 h->needs_copy = 1;
1800 /* We need to figure out the alignment required for this symbol. I
1801 have no idea how other ELF linkers handle this. */
1803 power_of_two = bfd_log2 (h->size);
1804 if (power_of_two > 3)
1805 power_of_two = 3;
1807 /* Apply the required alignment. */
1808 s = htab->sdynbss;
1809 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1810 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1812 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1813 return FALSE;
1816 /* Define the symbol as being at this point in the section. */
1817 h->root.u.def.section = s;
1818 h->root.u.def.value = s->size;
1820 /* Increment the section size to make room for the symbol. */
1821 s->size += h->size;
1823 return TRUE;
1826 /* Allocate space in the .plt for entries that won't have relocations.
1827 ie. plabel entries. */
1829 static bfd_boolean
1830 allocate_plt_static (struct elf_link_hash_entry *h, void *inf)
1832 struct bfd_link_info *info;
1833 struct elf32_hppa_link_hash_table *htab;
1834 asection *s;
1836 if (h->root.type == bfd_link_hash_indirect)
1837 return TRUE;
1839 if (h->root.type == bfd_link_hash_warning)
1840 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1842 info = inf;
1843 htab = hppa_link_hash_table (info);
1844 if (htab->elf.dynamic_sections_created
1845 && h->plt.refcount > 0)
1847 /* Make sure this symbol is output as a dynamic symbol.
1848 Undefined weak syms won't yet be marked as dynamic. */
1849 if (h->dynindx == -1
1850 && !h->forced_local
1851 && h->type != STT_PARISC_MILLI)
1853 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1854 return FALSE;
1857 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
1859 /* Allocate these later. From this point on, h->plabel
1860 means that the plt entry is only used by a plabel.
1861 We'll be using a normal plt entry for this symbol, so
1862 clear the plabel indicator. */
1863 ((struct elf32_hppa_link_hash_entry *) h)->plabel = 0;
1865 else if (((struct elf32_hppa_link_hash_entry *) h)->plabel)
1867 /* Make an entry in the .plt section for plabel references
1868 that won't have a .plt entry for other reasons. */
1869 s = htab->splt;
1870 h->plt.offset = s->size;
1871 s->size += PLT_ENTRY_SIZE;
1873 else
1875 /* No .plt entry needed. */
1876 h->plt.offset = (bfd_vma) -1;
1877 h->needs_plt = 0;
1880 else
1882 h->plt.offset = (bfd_vma) -1;
1883 h->needs_plt = 0;
1886 return TRUE;
1889 /* Allocate space in .plt, .got and associated reloc sections for
1890 global syms. */
1892 static bfd_boolean
1893 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1895 struct bfd_link_info *info;
1896 struct elf32_hppa_link_hash_table *htab;
1897 asection *s;
1898 struct elf32_hppa_link_hash_entry *eh;
1899 struct elf32_hppa_dyn_reloc_entry *p;
1901 if (h->root.type == bfd_link_hash_indirect)
1902 return TRUE;
1904 if (h->root.type == bfd_link_hash_warning)
1905 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1907 info = inf;
1908 htab = hppa_link_hash_table (info);
1909 if (htab->elf.dynamic_sections_created
1910 && h->plt.offset != (bfd_vma) -1
1911 && !((struct elf32_hppa_link_hash_entry *) h)->plabel)
1913 /* Make an entry in the .plt section. */
1914 s = htab->splt;
1915 h->plt.offset = s->size;
1916 s->size += PLT_ENTRY_SIZE;
1918 /* We also need to make an entry in the .rela.plt section. */
1919 htab->srelplt->size += sizeof (Elf32_External_Rela);
1920 htab->need_plt_stub = 1;
1923 if (h->got.refcount > 0)
1925 /* Make sure this symbol is output as a dynamic symbol.
1926 Undefined weak syms won't yet be marked as dynamic. */
1927 if (h->dynindx == -1
1928 && !h->forced_local
1929 && h->type != STT_PARISC_MILLI)
1931 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1932 return FALSE;
1935 s = htab->sgot;
1936 h->got.offset = s->size;
1937 s->size += GOT_ENTRY_SIZE;
1938 if (htab->elf.dynamic_sections_created
1939 && (info->shared
1940 || (h->dynindx != -1
1941 && !h->forced_local)))
1943 htab->srelgot->size += sizeof (Elf32_External_Rela);
1946 else
1947 h->got.offset = (bfd_vma) -1;
1949 eh = (struct elf32_hppa_link_hash_entry *) h;
1950 if (eh->dyn_relocs == NULL)
1951 return TRUE;
1953 /* If this is a -Bsymbolic shared link, then we need to discard all
1954 space allocated for dynamic pc-relative relocs against symbols
1955 defined in a regular object. For the normal shared case, discard
1956 space for relocs that have become local due to symbol visibility
1957 changes. */
1958 if (info->shared)
1960 #if RELATIVE_DYNRELOCS
1961 if (SYMBOL_CALLS_LOCAL (info, h))
1963 struct elf32_hppa_dyn_reloc_entry **pp;
1965 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1967 p->count -= p->relative_count;
1968 p->relative_count = 0;
1969 if (p->count == 0)
1970 *pp = p->next;
1971 else
1972 pp = &p->next;
1975 #endif
1977 /* Also discard relocs on undefined weak syms with non-default
1978 visibility. */
1979 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1980 && h->root.type == bfd_link_hash_undefweak)
1981 eh->dyn_relocs = NULL;
1983 else
1985 /* For the non-shared case, discard space for relocs against
1986 symbols which turn out to need copy relocs or are not
1987 dynamic. */
1988 if (!h->non_got_ref
1989 && ((ELIMINATE_COPY_RELOCS
1990 && h->def_dynamic
1991 && !h->def_regular)
1992 || (htab->elf.dynamic_sections_created
1993 && (h->root.type == bfd_link_hash_undefweak
1994 || h->root.type == bfd_link_hash_undefined))))
1996 /* Make sure this symbol is output as a dynamic symbol.
1997 Undefined weak syms won't yet be marked as dynamic. */
1998 if (h->dynindx == -1
1999 && !h->forced_local
2000 && h->type != STT_PARISC_MILLI)
2002 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2003 return FALSE;
2006 /* If that succeeded, we know we'll be keeping all the
2007 relocs. */
2008 if (h->dynindx != -1)
2009 goto keep;
2012 eh->dyn_relocs = NULL;
2013 return TRUE;
2015 keep: ;
2018 /* Finally, allocate space. */
2019 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2021 asection *sreloc = elf_section_data (p->sec)->sreloc;
2022 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2025 return TRUE;
2028 /* This function is called via elf_link_hash_traverse to force
2029 millicode symbols local so they do not end up as globals in the
2030 dynamic symbol table. We ought to be able to do this in
2031 adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2032 for all dynamic symbols. Arguably, this is a bug in
2033 elf_adjust_dynamic_symbol. */
2035 static bfd_boolean
2036 clobber_millicode_symbols (struct elf_link_hash_entry *h,
2037 struct bfd_link_info *info)
2039 if (h->root.type == bfd_link_hash_warning)
2040 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2042 if (h->type == STT_PARISC_MILLI
2043 && !h->forced_local)
2045 elf32_hppa_hide_symbol (info, h, TRUE);
2047 return TRUE;
2050 /* Find any dynamic relocs that apply to read-only sections. */
2052 static bfd_boolean
2053 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2055 struct elf32_hppa_link_hash_entry *eh;
2056 struct elf32_hppa_dyn_reloc_entry *p;
2058 if (h->root.type == bfd_link_hash_warning)
2059 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2061 eh = (struct elf32_hppa_link_hash_entry *) h;
2062 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2064 asection *s = p->sec->output_section;
2066 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2068 struct bfd_link_info *info = inf;
2070 info->flags |= DF_TEXTREL;
2072 /* Not an error, just cut short the traversal. */
2073 return FALSE;
2076 return TRUE;
2079 /* Set the sizes of the dynamic sections. */
2081 static bfd_boolean
2082 elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2083 struct bfd_link_info *info)
2085 struct elf32_hppa_link_hash_table *htab;
2086 bfd *dynobj;
2087 bfd *ibfd;
2088 asection *s;
2089 bfd_boolean relocs;
2091 htab = hppa_link_hash_table (info);
2092 dynobj = htab->elf.dynobj;
2093 if (dynobj == NULL)
2094 abort ();
2096 if (htab->elf.dynamic_sections_created)
2098 /* Set the contents of the .interp section to the interpreter. */
2099 if (info->executable)
2101 s = bfd_get_section_by_name (dynobj, ".interp");
2102 if (s == NULL)
2103 abort ();
2104 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2105 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2108 /* Force millicode symbols local. */
2109 elf_link_hash_traverse (&htab->elf,
2110 clobber_millicode_symbols,
2111 info);
2114 /* Set up .got and .plt offsets for local syms, and space for local
2115 dynamic relocs. */
2116 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2118 bfd_signed_vma *local_got;
2119 bfd_signed_vma *end_local_got;
2120 bfd_signed_vma *local_plt;
2121 bfd_signed_vma *end_local_plt;
2122 bfd_size_type locsymcount;
2123 Elf_Internal_Shdr *symtab_hdr;
2124 asection *srel;
2126 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2127 continue;
2129 for (s = ibfd->sections; s != NULL; s = s->next)
2131 struct elf32_hppa_dyn_reloc_entry *p;
2133 for (p = ((struct elf32_hppa_dyn_reloc_entry *)
2134 elf_section_data (s)->local_dynrel);
2135 p != NULL;
2136 p = p->next)
2138 if (!bfd_is_abs_section (p->sec)
2139 && bfd_is_abs_section (p->sec->output_section))
2141 /* Input section has been discarded, either because
2142 it is a copy of a linkonce section or due to
2143 linker script /DISCARD/, so we'll be discarding
2144 the relocs too. */
2146 else if (p->count != 0)
2148 srel = elf_section_data (p->sec)->sreloc;
2149 srel->size += p->count * sizeof (Elf32_External_Rela);
2150 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2151 info->flags |= DF_TEXTREL;
2156 local_got = elf_local_got_refcounts (ibfd);
2157 if (!local_got)
2158 continue;
2160 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2161 locsymcount = symtab_hdr->sh_info;
2162 end_local_got = local_got + locsymcount;
2163 s = htab->sgot;
2164 srel = htab->srelgot;
2165 for (; local_got < end_local_got; ++local_got)
2167 if (*local_got > 0)
2169 *local_got = s->size;
2170 s->size += GOT_ENTRY_SIZE;
2171 if (info->shared)
2172 srel->size += sizeof (Elf32_External_Rela);
2174 else
2175 *local_got = (bfd_vma) -1;
2178 local_plt = end_local_got;
2179 end_local_plt = local_plt + locsymcount;
2180 if (! htab->elf.dynamic_sections_created)
2182 /* Won't be used, but be safe. */
2183 for (; local_plt < end_local_plt; ++local_plt)
2184 *local_plt = (bfd_vma) -1;
2186 else
2188 s = htab->splt;
2189 srel = htab->srelplt;
2190 for (; local_plt < end_local_plt; ++local_plt)
2192 if (*local_plt > 0)
2194 *local_plt = s->size;
2195 s->size += PLT_ENTRY_SIZE;
2196 if (info->shared)
2197 srel->size += sizeof (Elf32_External_Rela);
2199 else
2200 *local_plt = (bfd_vma) -1;
2205 /* Do all the .plt entries without relocs first. The dynamic linker
2206 uses the last .plt reloc to find the end of the .plt (and hence
2207 the start of the .got) for lazy linking. */
2208 elf_link_hash_traverse (&htab->elf, allocate_plt_static, info);
2210 /* Allocate global sym .plt and .got entries, and space for global
2211 sym dynamic relocs. */
2212 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2214 /* The check_relocs and adjust_dynamic_symbol entry points have
2215 determined the sizes of the various dynamic sections. Allocate
2216 memory for them. */
2217 relocs = FALSE;
2218 for (s = dynobj->sections; s != NULL; s = s->next)
2220 if ((s->flags & SEC_LINKER_CREATED) == 0)
2221 continue;
2223 if (s == htab->splt)
2225 if (htab->need_plt_stub)
2227 /* Make space for the plt stub at the end of the .plt
2228 section. We want this stub right at the end, up
2229 against the .got section. */
2230 int gotalign = bfd_section_alignment (dynobj, htab->sgot);
2231 int pltalign = bfd_section_alignment (dynobj, s);
2232 bfd_size_type mask;
2234 if (gotalign > pltalign)
2235 bfd_set_section_alignment (dynobj, s, gotalign);
2236 mask = ((bfd_size_type) 1 << gotalign) - 1;
2237 s->size = (s->size + sizeof (plt_stub) + mask) & ~mask;
2240 else if (s == htab->sgot)
2242 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2244 if (s->size != 0)
2246 /* Remember whether there are any reloc sections other
2247 than .rela.plt. */
2248 if (s != htab->srelplt)
2249 relocs = TRUE;
2251 /* We use the reloc_count field as a counter if we need
2252 to copy relocs into the output file. */
2253 s->reloc_count = 0;
2256 else
2258 /* It's not one of our sections, so don't allocate space. */
2259 continue;
2262 if (s->size == 0)
2264 /* If we don't need this section, strip it from the
2265 output file. This is mostly to handle .rela.bss and
2266 .rela.plt. We must create both sections in
2267 create_dynamic_sections, because they must be created
2268 before the linker maps input sections to output
2269 sections. The linker does that before
2270 adjust_dynamic_symbol is called, and it is that
2271 function which decides whether anything needs to go
2272 into these sections. */
2273 _bfd_strip_section_from_output (info, s);
2274 continue;
2277 /* Allocate memory for the section contents. Zero it, because
2278 we may not fill in all the reloc sections. */
2279 s->contents = bfd_zalloc (dynobj, s->size);
2280 if (s->contents == NULL && s->size != 0)
2281 return FALSE;
2284 if (htab->elf.dynamic_sections_created)
2286 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2287 actually has nothing to do with the PLT, it is how we
2288 communicate the LTP value of a load module to the dynamic
2289 linker. */
2290 #define add_dynamic_entry(TAG, VAL) \
2291 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2293 if (!add_dynamic_entry (DT_PLTGOT, 0))
2294 return FALSE;
2296 /* Add some entries to the .dynamic section. We fill in the
2297 values later, in elf32_hppa_finish_dynamic_sections, but we
2298 must add the entries now so that we get the correct size for
2299 the .dynamic section. The DT_DEBUG entry is filled in by the
2300 dynamic linker and used by the debugger. */
2301 if (!info->shared)
2303 if (!add_dynamic_entry (DT_DEBUG, 0))
2304 return FALSE;
2307 if (htab->srelplt->size != 0)
2309 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
2310 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2311 || !add_dynamic_entry (DT_JMPREL, 0))
2312 return FALSE;
2315 if (relocs)
2317 if (!add_dynamic_entry (DT_RELA, 0)
2318 || !add_dynamic_entry (DT_RELASZ, 0)
2319 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2320 return FALSE;
2322 /* If any dynamic relocs apply to a read-only section,
2323 then we need a DT_TEXTREL entry. */
2324 if ((info->flags & DF_TEXTREL) == 0)
2325 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
2327 if ((info->flags & DF_TEXTREL) != 0)
2329 if (!add_dynamic_entry (DT_TEXTREL, 0))
2330 return FALSE;
2334 #undef add_dynamic_entry
2336 return TRUE;
2339 /* External entry points for sizing and building linker stubs. */
2341 /* Set up various things so that we can make a list of input sections
2342 for each output section included in the link. Returns -1 on error,
2343 0 when no stubs will be needed, and 1 on success. */
2346 elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2348 bfd *input_bfd;
2349 unsigned int bfd_count;
2350 int top_id, top_index;
2351 asection *section;
2352 asection **input_list, **list;
2353 bfd_size_type amt;
2354 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2356 /* Count the number of input BFDs and find the top input section id. */
2357 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2358 input_bfd != NULL;
2359 input_bfd = input_bfd->link_next)
2361 bfd_count += 1;
2362 for (section = input_bfd->sections;
2363 section != NULL;
2364 section = section->next)
2366 if (top_id < section->id)
2367 top_id = section->id;
2370 htab->bfd_count = bfd_count;
2372 amt = sizeof (struct map_stub) * (top_id + 1);
2373 htab->stub_group = bfd_zmalloc (amt);
2374 if (htab->stub_group == NULL)
2375 return -1;
2377 /* We can't use output_bfd->section_count here to find the top output
2378 section index as some sections may have been removed, and
2379 _bfd_strip_section_from_output doesn't renumber the indices. */
2380 for (section = output_bfd->sections, top_index = 0;
2381 section != NULL;
2382 section = section->next)
2384 if (top_index < section->index)
2385 top_index = section->index;
2388 htab->top_index = top_index;
2389 amt = sizeof (asection *) * (top_index + 1);
2390 input_list = bfd_malloc (amt);
2391 htab->input_list = input_list;
2392 if (input_list == NULL)
2393 return -1;
2395 /* For sections we aren't interested in, mark their entries with a
2396 value we can check later. */
2397 list = input_list + top_index;
2399 *list = bfd_abs_section_ptr;
2400 while (list-- != input_list);
2402 for (section = output_bfd->sections;
2403 section != NULL;
2404 section = section->next)
2406 if ((section->flags & SEC_CODE) != 0)
2407 input_list[section->index] = NULL;
2410 return 1;
2413 /* The linker repeatedly calls this function for each input section,
2414 in the order that input sections are linked into output sections.
2415 Build lists of input sections to determine groupings between which
2416 we may insert linker stubs. */
2418 void
2419 elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
2421 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2423 if (isec->output_section->index <= htab->top_index)
2425 asection **list = htab->input_list + isec->output_section->index;
2426 if (*list != bfd_abs_section_ptr)
2428 /* Steal the link_sec pointer for our list. */
2429 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2430 /* This happens to make the list in reverse order,
2431 which is what we want. */
2432 PREV_SEC (isec) = *list;
2433 *list = isec;
2438 /* See whether we can group stub sections together. Grouping stub
2439 sections may result in fewer stubs. More importantly, we need to
2440 put all .init* and .fini* stubs at the beginning of the .init or
2441 .fini output sections respectively, because glibc splits the
2442 _init and _fini functions into multiple parts. Putting a stub in
2443 the middle of a function is not a good idea. */
2445 static void
2446 group_sections (struct elf32_hppa_link_hash_table *htab,
2447 bfd_size_type stub_group_size,
2448 bfd_boolean stubs_always_before_branch)
2450 asection **list = htab->input_list + htab->top_index;
2453 asection *tail = *list;
2454 if (tail == bfd_abs_section_ptr)
2455 continue;
2456 while (tail != NULL)
2458 asection *curr;
2459 asection *prev;
2460 bfd_size_type total;
2461 bfd_boolean big_sec;
2463 curr = tail;
2464 total = tail->size;
2465 big_sec = total >= stub_group_size;
2467 while ((prev = PREV_SEC (curr)) != NULL
2468 && ((total += curr->output_offset - prev->output_offset)
2469 < stub_group_size))
2470 curr = prev;
2472 /* OK, the size from the start of CURR to the end is less
2473 than 240000 bytes and thus can be handled by one stub
2474 section. (or the tail section is itself larger than
2475 240000 bytes, in which case we may be toast.)
2476 We should really be keeping track of the total size of
2477 stubs added here, as stubs contribute to the final output
2478 section size. That's a little tricky, and this way will
2479 only break if stubs added total more than 22144 bytes, or
2480 2768 long branch stubs. It seems unlikely for more than
2481 2768 different functions to be called, especially from
2482 code only 240000 bytes long. This limit used to be
2483 250000, but c++ code tends to generate lots of little
2484 functions, and sometimes violated the assumption. */
2487 prev = PREV_SEC (tail);
2488 /* Set up this stub group. */
2489 htab->stub_group[tail->id].link_sec = curr;
2491 while (tail != curr && (tail = prev) != NULL);
2493 /* But wait, there's more! Input sections up to 240000
2494 bytes before the stub section can be handled by it too.
2495 Don't do this if we have a really large section after the
2496 stubs, as adding more stubs increases the chance that
2497 branches may not reach into the stub section. */
2498 if (!stubs_always_before_branch && !big_sec)
2500 total = 0;
2501 while (prev != NULL
2502 && ((total += tail->output_offset - prev->output_offset)
2503 < stub_group_size))
2505 tail = prev;
2506 prev = PREV_SEC (tail);
2507 htab->stub_group[tail->id].link_sec = curr;
2510 tail = prev;
2513 while (list-- != htab->input_list);
2514 free (htab->input_list);
2515 #undef PREV_SEC
2518 /* Read in all local syms for all input bfds, and create hash entries
2519 for export stubs if we are building a multi-subspace shared lib.
2520 Returns -1 on error, 1 if export stubs created, 0 otherwise. */
2522 static int
2523 get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
2525 unsigned int bfd_indx;
2526 Elf_Internal_Sym *local_syms, **all_local_syms;
2527 int stub_changed = 0;
2528 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2530 /* We want to read in symbol extension records only once. To do this
2531 we need to read in the local symbols in parallel and save them for
2532 later use; so hold pointers to the local symbols in an array. */
2533 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2534 all_local_syms = bfd_zmalloc (amt);
2535 htab->all_local_syms = all_local_syms;
2536 if (all_local_syms == NULL)
2537 return -1;
2539 /* Walk over all the input BFDs, swapping in local symbols.
2540 If we are creating a shared library, create hash entries for the
2541 export stubs. */
2542 for (bfd_indx = 0;
2543 input_bfd != NULL;
2544 input_bfd = input_bfd->link_next, bfd_indx++)
2546 Elf_Internal_Shdr *symtab_hdr;
2548 /* We'll need the symbol table in a second. */
2549 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2550 if (symtab_hdr->sh_info == 0)
2551 continue;
2553 /* We need an array of the local symbols attached to the input bfd. */
2554 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2555 if (local_syms == NULL)
2557 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2558 symtab_hdr->sh_info, 0,
2559 NULL, NULL, NULL);
2560 /* Cache them for elf_link_input_bfd. */
2561 symtab_hdr->contents = (unsigned char *) local_syms;
2563 if (local_syms == NULL)
2564 return -1;
2566 all_local_syms[bfd_indx] = local_syms;
2568 if (info->shared && htab->multi_subspace)
2570 struct elf_link_hash_entry **sym_hashes;
2571 struct elf_link_hash_entry **end_hashes;
2572 unsigned int symcount;
2574 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2575 - symtab_hdr->sh_info);
2576 sym_hashes = elf_sym_hashes (input_bfd);
2577 end_hashes = sym_hashes + symcount;
2579 /* Look through the global syms for functions; We need to
2580 build export stubs for all globally visible functions. */
2581 for (; sym_hashes < end_hashes; sym_hashes++)
2583 struct elf32_hppa_link_hash_entry *hash;
2585 hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
2587 while (hash->elf.root.type == bfd_link_hash_indirect
2588 || hash->elf.root.type == bfd_link_hash_warning)
2589 hash = ((struct elf32_hppa_link_hash_entry *)
2590 hash->elf.root.u.i.link);
2592 /* At this point in the link, undefined syms have been
2593 resolved, so we need to check that the symbol was
2594 defined in this BFD. */
2595 if ((hash->elf.root.type == bfd_link_hash_defined
2596 || hash->elf.root.type == bfd_link_hash_defweak)
2597 && hash->elf.type == STT_FUNC
2598 && hash->elf.root.u.def.section->output_section != NULL
2599 && (hash->elf.root.u.def.section->output_section->owner
2600 == output_bfd)
2601 && hash->elf.root.u.def.section->owner == input_bfd
2602 && hash->elf.def_regular
2603 && !hash->elf.forced_local
2604 && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2606 asection *sec;
2607 const char *stub_name;
2608 struct elf32_hppa_stub_hash_entry *stub_entry;
2610 sec = hash->elf.root.u.def.section;
2611 stub_name = hash->elf.root.root.string;
2612 stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table,
2613 stub_name,
2614 FALSE, FALSE);
2615 if (stub_entry == NULL)
2617 stub_entry = hppa_add_stub (stub_name, sec, htab);
2618 if (!stub_entry)
2619 return -1;
2621 stub_entry->target_value = hash->elf.root.u.def.value;
2622 stub_entry->target_section = hash->elf.root.u.def.section;
2623 stub_entry->stub_type = hppa_stub_export;
2624 stub_entry->h = hash;
2625 stub_changed = 1;
2627 else
2629 (*_bfd_error_handler) (_("%B: duplicate export stub %s"),
2630 input_bfd,
2631 stub_name);
2638 return stub_changed;
2641 /* Determine and set the size of the stub section for a final link.
2643 The basic idea here is to examine all the relocations looking for
2644 PC-relative calls to a target that is unreachable with a "bl"
2645 instruction. */
2647 bfd_boolean
2648 elf32_hppa_size_stubs
2649 (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
2650 bfd_boolean multi_subspace, bfd_signed_vma group_size,
2651 asection * (*add_stub_section) (const char *, asection *),
2652 void (*layout_sections_again) (void))
2654 bfd_size_type stub_group_size;
2655 bfd_boolean stubs_always_before_branch;
2656 bfd_boolean stub_changed;
2657 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2659 /* Stash our params away. */
2660 htab->stub_bfd = stub_bfd;
2661 htab->multi_subspace = multi_subspace;
2662 htab->add_stub_section = add_stub_section;
2663 htab->layout_sections_again = layout_sections_again;
2664 stubs_always_before_branch = group_size < 0;
2665 if (group_size < 0)
2666 stub_group_size = -group_size;
2667 else
2668 stub_group_size = group_size;
2669 if (stub_group_size == 1)
2671 /* Default values. */
2672 if (stubs_always_before_branch)
2674 stub_group_size = 7680000;
2675 if (htab->has_17bit_branch || htab->multi_subspace)
2676 stub_group_size = 240000;
2677 if (htab->has_12bit_branch)
2678 stub_group_size = 7500;
2680 else
2682 stub_group_size = 6971392;
2683 if (htab->has_17bit_branch || htab->multi_subspace)
2684 stub_group_size = 217856;
2685 if (htab->has_12bit_branch)
2686 stub_group_size = 6808;
2690 group_sections (htab, stub_group_size, stubs_always_before_branch);
2692 switch (get_local_syms (output_bfd, info->input_bfds, info))
2694 default:
2695 if (htab->all_local_syms)
2696 goto error_ret_free_local;
2697 return FALSE;
2699 case 0:
2700 stub_changed = FALSE;
2701 break;
2703 case 1:
2704 stub_changed = TRUE;
2705 break;
2708 while (1)
2710 bfd *input_bfd;
2711 unsigned int bfd_indx;
2712 asection *stub_sec;
2714 for (input_bfd = info->input_bfds, bfd_indx = 0;
2715 input_bfd != NULL;
2716 input_bfd = input_bfd->link_next, bfd_indx++)
2718 Elf_Internal_Shdr *symtab_hdr;
2719 asection *section;
2720 Elf_Internal_Sym *local_syms;
2722 /* We'll need the symbol table in a second. */
2723 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2724 if (symtab_hdr->sh_info == 0)
2725 continue;
2727 local_syms = htab->all_local_syms[bfd_indx];
2729 /* Walk over each section attached to the input bfd. */
2730 for (section = input_bfd->sections;
2731 section != NULL;
2732 section = section->next)
2734 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2736 /* If there aren't any relocs, then there's nothing more
2737 to do. */
2738 if ((section->flags & SEC_RELOC) == 0
2739 || section->reloc_count == 0)
2740 continue;
2742 /* If this section is a link-once section that will be
2743 discarded, then don't create any stubs. */
2744 if (section->output_section == NULL
2745 || section->output_section->owner != output_bfd)
2746 continue;
2748 /* Get the relocs. */
2749 internal_relocs
2750 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2751 info->keep_memory);
2752 if (internal_relocs == NULL)
2753 goto error_ret_free_local;
2755 /* Now examine each relocation. */
2756 irela = internal_relocs;
2757 irelaend = irela + section->reloc_count;
2758 for (; irela < irelaend; irela++)
2760 unsigned int r_type, r_indx;
2761 enum elf32_hppa_stub_type stub_type;
2762 struct elf32_hppa_stub_hash_entry *stub_entry;
2763 asection *sym_sec;
2764 bfd_vma sym_value;
2765 bfd_vma destination;
2766 struct elf32_hppa_link_hash_entry *hash;
2767 char *stub_name;
2768 const asection *id_sec;
2770 r_type = ELF32_R_TYPE (irela->r_info);
2771 r_indx = ELF32_R_SYM (irela->r_info);
2773 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2775 bfd_set_error (bfd_error_bad_value);
2776 error_ret_free_internal:
2777 if (elf_section_data (section)->relocs == NULL)
2778 free (internal_relocs);
2779 goto error_ret_free_local;
2782 /* Only look for stubs on call instructions. */
2783 if (r_type != (unsigned int) R_PARISC_PCREL12F
2784 && r_type != (unsigned int) R_PARISC_PCREL17F
2785 && r_type != (unsigned int) R_PARISC_PCREL22F)
2786 continue;
2788 /* Now determine the call target, its name, value,
2789 section. */
2790 sym_sec = NULL;
2791 sym_value = 0;
2792 destination = 0;
2793 hash = NULL;
2794 if (r_indx < symtab_hdr->sh_info)
2796 /* It's a local symbol. */
2797 Elf_Internal_Sym *sym;
2798 Elf_Internal_Shdr *hdr;
2800 sym = local_syms + r_indx;
2801 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2802 sym_sec = hdr->bfd_section;
2803 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2804 sym_value = sym->st_value;
2805 destination = (sym_value + irela->r_addend
2806 + sym_sec->output_offset
2807 + sym_sec->output_section->vma);
2809 else
2811 /* It's an external symbol. */
2812 int e_indx;
2814 e_indx = r_indx - symtab_hdr->sh_info;
2815 hash = ((struct elf32_hppa_link_hash_entry *)
2816 elf_sym_hashes (input_bfd)[e_indx]);
2818 while (hash->elf.root.type == bfd_link_hash_indirect
2819 || hash->elf.root.type == bfd_link_hash_warning)
2820 hash = ((struct elf32_hppa_link_hash_entry *)
2821 hash->elf.root.u.i.link);
2823 if (hash->elf.root.type == bfd_link_hash_defined
2824 || hash->elf.root.type == bfd_link_hash_defweak)
2826 sym_sec = hash->elf.root.u.def.section;
2827 sym_value = hash->elf.root.u.def.value;
2828 if (sym_sec->output_section != NULL)
2829 destination = (sym_value + irela->r_addend
2830 + sym_sec->output_offset
2831 + sym_sec->output_section->vma);
2833 else if (hash->elf.root.type == bfd_link_hash_undefweak)
2835 if (! info->shared)
2836 continue;
2838 else if (hash->elf.root.type == bfd_link_hash_undefined)
2840 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2841 && (ELF_ST_VISIBILITY (hash->elf.other)
2842 == STV_DEFAULT)
2843 && hash->elf.type != STT_PARISC_MILLI))
2844 continue;
2846 else
2848 bfd_set_error (bfd_error_bad_value);
2849 goto error_ret_free_internal;
2853 /* Determine what (if any) linker stub is needed. */
2854 stub_type = hppa_type_of_stub (section, irela, hash,
2855 destination, info);
2856 if (stub_type == hppa_stub_none)
2857 continue;
2859 /* Support for grouping stub sections. */
2860 id_sec = htab->stub_group[section->id].link_sec;
2862 /* Get the name of this stub. */
2863 stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
2864 if (!stub_name)
2865 goto error_ret_free_internal;
2867 stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table,
2868 stub_name,
2869 FALSE, FALSE);
2870 if (stub_entry != NULL)
2872 /* The proper stub has already been created. */
2873 free (stub_name);
2874 continue;
2877 stub_entry = hppa_add_stub (stub_name, section, htab);
2878 if (stub_entry == NULL)
2880 free (stub_name);
2881 goto error_ret_free_internal;
2884 stub_entry->target_value = sym_value;
2885 stub_entry->target_section = sym_sec;
2886 stub_entry->stub_type = stub_type;
2887 if (info->shared)
2889 if (stub_type == hppa_stub_import)
2890 stub_entry->stub_type = hppa_stub_import_shared;
2891 else if (stub_type == hppa_stub_long_branch)
2892 stub_entry->stub_type = hppa_stub_long_branch_shared;
2894 stub_entry->h = hash;
2895 stub_changed = TRUE;
2898 /* We're done with the internal relocs, free them. */
2899 if (elf_section_data (section)->relocs == NULL)
2900 free (internal_relocs);
2904 if (!stub_changed)
2905 break;
2907 /* OK, we've added some stubs. Find out the new size of the
2908 stub sections. */
2909 for (stub_sec = htab->stub_bfd->sections;
2910 stub_sec != NULL;
2911 stub_sec = stub_sec->next)
2912 stub_sec->size = 0;
2914 bfd_hash_traverse (&htab->stub_hash_table, hppa_size_one_stub, htab);
2916 /* Ask the linker to do its stuff. */
2917 (*htab->layout_sections_again) ();
2918 stub_changed = FALSE;
2921 free (htab->all_local_syms);
2922 return TRUE;
2924 error_ret_free_local:
2925 free (htab->all_local_syms);
2926 return FALSE;
2929 /* For a final link, this function is called after we have sized the
2930 stubs to provide a value for __gp. */
2932 bfd_boolean
2933 elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
2935 struct bfd_link_hash_entry *h;
2936 asection *sec = NULL;
2937 bfd_vma gp_val = 0;
2938 struct elf32_hppa_link_hash_table *htab;
2940 htab = hppa_link_hash_table (info);
2941 h = bfd_link_hash_lookup (&htab->elf.root, "$global$", FALSE, FALSE, FALSE);
2943 if (h != NULL
2944 && (h->type == bfd_link_hash_defined
2945 || h->type == bfd_link_hash_defweak))
2947 gp_val = h->u.def.value;
2948 sec = h->u.def.section;
2950 else
2952 asection *splt = bfd_get_section_by_name (abfd, ".plt");
2953 asection *sgot = bfd_get_section_by_name (abfd, ".got");
2955 /* Choose to point our LTP at, in this order, one of .plt, .got,
2956 or .data, if these sections exist. In the case of choosing
2957 .plt try to make the LTP ideal for addressing anywhere in the
2958 .plt or .got with a 14 bit signed offset. Typically, the end
2959 of the .plt is the start of the .got, so choose .plt + 0x2000
2960 if either the .plt or .got is larger than 0x2000. If both
2961 the .plt and .got are smaller than 0x2000, choose the end of
2962 the .plt section. */
2963 sec = strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0
2964 ? NULL : splt;
2965 if (sec != NULL)
2967 gp_val = sec->size;
2968 if (gp_val > 0x2000 || (sgot && sgot->size > 0x2000))
2970 gp_val = 0x2000;
2973 else
2975 sec = sgot;
2976 if (sec != NULL)
2978 if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") != 0)
2980 /* We know we don't have a .plt. If .got is large,
2981 offset our LTP. */
2982 if (sec->size > 0x2000)
2983 gp_val = 0x2000;
2986 else
2988 /* No .plt or .got. Who cares what the LTP is? */
2989 sec = bfd_get_section_by_name (abfd, ".data");
2993 if (h != NULL)
2995 h->type = bfd_link_hash_defined;
2996 h->u.def.value = gp_val;
2997 if (sec != NULL)
2998 h->u.def.section = sec;
2999 else
3000 h->u.def.section = bfd_abs_section_ptr;
3004 if (sec != NULL && sec->output_section != NULL)
3005 gp_val += sec->output_section->vma + sec->output_offset;
3007 elf_gp (abfd) = gp_val;
3008 return TRUE;
3011 /* Build all the stubs associated with the current output file. The
3012 stubs are kept in a hash table attached to the main linker hash
3013 table. We also set up the .plt entries for statically linked PIC
3014 functions here. This function is called via hppaelf_finish in the
3015 linker. */
3017 bfd_boolean
3018 elf32_hppa_build_stubs (struct bfd_link_info *info)
3020 asection *stub_sec;
3021 struct bfd_hash_table *table;
3022 struct elf32_hppa_link_hash_table *htab;
3024 htab = hppa_link_hash_table (info);
3026 for (stub_sec = htab->stub_bfd->sections;
3027 stub_sec != NULL;
3028 stub_sec = stub_sec->next)
3030 bfd_size_type size;
3032 /* Allocate memory to hold the linker stubs. */
3033 size = stub_sec->size;
3034 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3035 if (stub_sec->contents == NULL && size != 0)
3036 return FALSE;
3037 stub_sec->size = 0;
3040 /* Build the stubs as directed by the stub hash table. */
3041 table = &htab->stub_hash_table;
3042 bfd_hash_traverse (table, hppa_build_one_stub, info);
3044 return TRUE;
3047 /* Perform a final link. */
3049 static bfd_boolean
3050 elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
3052 /* Invoke the regular ELF linker to do all the work. */
3053 if (!bfd_elf_final_link (abfd, info))
3054 return FALSE;
3056 /* If we're producing a final executable, sort the contents of the
3057 unwind section. */
3058 return elf_hppa_sort_unwind (abfd);
3061 /* Record the lowest address for the data and text segments. */
3063 static void
3064 hppa_record_segment_addr (bfd *abfd ATTRIBUTE_UNUSED,
3065 asection *section,
3066 void *data)
3068 struct elf32_hppa_link_hash_table *htab;
3070 htab = (struct elf32_hppa_link_hash_table *) data;
3072 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3074 bfd_vma value = section->vma - section->filepos;
3076 if ((section->flags & SEC_READONLY) != 0)
3078 if (value < htab->text_segment_base)
3079 htab->text_segment_base = value;
3081 else
3083 if (value < htab->data_segment_base)
3084 htab->data_segment_base = value;
3089 /* Perform a relocation as part of a final link. */
3091 static bfd_reloc_status_type
3092 final_link_relocate (asection *input_section,
3093 bfd_byte *contents,
3094 const Elf_Internal_Rela *rel,
3095 bfd_vma value,
3096 struct elf32_hppa_link_hash_table *htab,
3097 asection *sym_sec,
3098 struct elf32_hppa_link_hash_entry *h,
3099 struct bfd_link_info *info)
3101 int insn;
3102 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3103 unsigned int orig_r_type = r_type;
3104 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3105 int r_format = howto->bitsize;
3106 enum hppa_reloc_field_selector_type_alt r_field;
3107 bfd *input_bfd = input_section->owner;
3108 bfd_vma offset = rel->r_offset;
3109 bfd_vma max_branch_offset = 0;
3110 bfd_byte *hit_data = contents + offset;
3111 bfd_signed_vma addend = rel->r_addend;
3112 bfd_vma location;
3113 struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
3114 int val;
3116 if (r_type == R_PARISC_NONE)
3117 return bfd_reloc_ok;
3119 insn = bfd_get_32 (input_bfd, hit_data);
3121 /* Find out where we are and where we're going. */
3122 location = (offset +
3123 input_section->output_offset +
3124 input_section->output_section->vma);
3126 /* If we are not building a shared library, convert DLTIND relocs to
3127 DPREL relocs. */
3128 if (!info->shared)
3130 switch (r_type)
3132 case R_PARISC_DLTIND21L:
3133 r_type = R_PARISC_DPREL21L;
3134 break;
3136 case R_PARISC_DLTIND14R:
3137 r_type = R_PARISC_DPREL14R;
3138 break;
3140 case R_PARISC_DLTIND14F:
3141 r_type = R_PARISC_DPREL14F;
3142 break;
3146 switch (r_type)
3148 case R_PARISC_PCREL12F:
3149 case R_PARISC_PCREL17F:
3150 case R_PARISC_PCREL22F:
3151 /* If this call should go via the plt, find the import stub in
3152 the stub hash. */
3153 if (sym_sec == NULL
3154 || sym_sec->output_section == NULL
3155 || (h != NULL
3156 && h->elf.plt.offset != (bfd_vma) -1
3157 && h->elf.dynindx != -1
3158 && !h->plabel
3159 && (info->shared
3160 || !h->elf.def_regular
3161 || h->elf.root.type == bfd_link_hash_defweak)))
3163 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3164 h, rel, htab);
3165 if (stub_entry != NULL)
3167 value = (stub_entry->stub_offset
3168 + stub_entry->stub_sec->output_offset
3169 + stub_entry->stub_sec->output_section->vma);
3170 addend = 0;
3172 else if (sym_sec == NULL && h != NULL
3173 && h->elf.root.type == bfd_link_hash_undefweak)
3175 /* It's OK if undefined weak. Calls to undefined weak
3176 symbols behave as if the "called" function
3177 immediately returns. We can thus call to a weak
3178 function without first checking whether the function
3179 is defined. */
3180 value = location;
3181 addend = 8;
3183 else
3184 return bfd_reloc_undefined;
3186 /* Fall thru. */
3188 case R_PARISC_PCREL21L:
3189 case R_PARISC_PCREL17C:
3190 case R_PARISC_PCREL17R:
3191 case R_PARISC_PCREL14R:
3192 case R_PARISC_PCREL14F:
3193 case R_PARISC_PCREL32:
3194 /* Make it a pc relative offset. */
3195 value -= location;
3196 addend -= 8;
3197 break;
3199 case R_PARISC_DPREL21L:
3200 case R_PARISC_DPREL14R:
3201 case R_PARISC_DPREL14F:
3202 /* Convert instructions that use the linkage table pointer (r19) to
3203 instructions that use the global data pointer (dp). This is the
3204 most efficient way of using PIC code in an incomplete executable,
3205 but the user must follow the standard runtime conventions for
3206 accessing data for this to work. */
3207 if (orig_r_type == R_PARISC_DLTIND21L)
3209 /* Convert addil instructions if the original reloc was a
3210 DLTIND21L. GCC sometimes uses a register other than r19 for
3211 the operation, so we must convert any addil instruction
3212 that uses this relocation. */
3213 if ((insn & 0xfc000000) == ((int) OP_ADDIL << 26))
3214 insn = ADDIL_DP;
3215 else
3216 /* We must have a ldil instruction. It's too hard to find
3217 and convert the associated add instruction, so issue an
3218 error. */
3219 (*_bfd_error_handler)
3220 (_("%B(%A+0x%lx): %s fixup for insn 0x%x is not supported in a non-shared link"),
3221 input_bfd,
3222 input_section,
3223 (long) rel->r_offset,
3224 howto->name,
3225 insn);
3227 else if (orig_r_type == R_PARISC_DLTIND14F)
3229 /* This must be a format 1 load/store. Change the base
3230 register to dp. */
3231 insn = (insn & 0xfc1ffff) | (27 << 21);
3234 /* For all the DP relative relocations, we need to examine the symbol's
3235 section. If it has no section or if it's a code section, then
3236 "data pointer relative" makes no sense. In that case we don't
3237 adjust the "value", and for 21 bit addil instructions, we change the
3238 source addend register from %dp to %r0. This situation commonly
3239 arises for undefined weak symbols and when a variable's "constness"
3240 is declared differently from the way the variable is defined. For
3241 instance: "extern int foo" with foo defined as "const int foo". */
3242 if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
3244 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3245 == (((int) OP_ADDIL << 26) | (27 << 21)))
3247 insn &= ~ (0x1f << 21);
3249 /* Now try to make things easy for the dynamic linker. */
3251 break;
3253 /* Fall thru. */
3255 case R_PARISC_DLTIND21L:
3256 case R_PARISC_DLTIND14R:
3257 case R_PARISC_DLTIND14F:
3258 value -= elf_gp (input_section->output_section->owner);
3259 break;
3261 case R_PARISC_SEGREL32:
3262 if ((sym_sec->flags & SEC_CODE) != 0)
3263 value -= htab->text_segment_base;
3264 else
3265 value -= htab->data_segment_base;
3266 break;
3268 default:
3269 break;
3272 switch (r_type)
3274 case R_PARISC_DIR32:
3275 case R_PARISC_DIR14F:
3276 case R_PARISC_DIR17F:
3277 case R_PARISC_PCREL17C:
3278 case R_PARISC_PCREL14F:
3279 case R_PARISC_PCREL32:
3280 case R_PARISC_DPREL14F:
3281 case R_PARISC_PLABEL32:
3282 case R_PARISC_DLTIND14F:
3283 case R_PARISC_SEGBASE:
3284 case R_PARISC_SEGREL32:
3285 r_field = e_fsel;
3286 break;
3288 case R_PARISC_DLTIND21L:
3289 case R_PARISC_PCREL21L:
3290 case R_PARISC_PLABEL21L:
3291 r_field = e_lsel;
3292 break;
3294 case R_PARISC_DIR21L:
3295 case R_PARISC_DPREL21L:
3296 r_field = e_lrsel;
3297 break;
3299 case R_PARISC_PCREL17R:
3300 case R_PARISC_PCREL14R:
3301 case R_PARISC_PLABEL14R:
3302 case R_PARISC_DLTIND14R:
3303 r_field = e_rsel;
3304 break;
3306 case R_PARISC_DIR17R:
3307 case R_PARISC_DIR14R:
3308 case R_PARISC_DPREL14R:
3309 r_field = e_rrsel;
3310 break;
3312 case R_PARISC_PCREL12F:
3313 case R_PARISC_PCREL17F:
3314 case R_PARISC_PCREL22F:
3315 r_field = e_fsel;
3317 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3319 max_branch_offset = (1 << (17-1)) << 2;
3321 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3323 max_branch_offset = (1 << (12-1)) << 2;
3325 else
3327 max_branch_offset = (1 << (22-1)) << 2;
3330 /* sym_sec is NULL on undefined weak syms or when shared on
3331 undefined syms. We've already checked for a stub for the
3332 shared undefined case. */
3333 if (sym_sec == NULL)
3334 break;
3336 /* If the branch is out of reach, then redirect the
3337 call to the local stub for this function. */
3338 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3340 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3341 h, rel, htab);
3342 if (stub_entry == NULL)
3343 return bfd_reloc_undefined;
3345 /* Munge up the value and addend so that we call the stub
3346 rather than the procedure directly. */
3347 value = (stub_entry->stub_offset
3348 + stub_entry->stub_sec->output_offset
3349 + stub_entry->stub_sec->output_section->vma
3350 - location);
3351 addend = -8;
3353 break;
3355 /* Something we don't know how to handle. */
3356 default:
3357 return bfd_reloc_notsupported;
3360 /* Make sure we can reach the stub. */
3361 if (max_branch_offset != 0
3362 && value + addend + max_branch_offset >= 2*max_branch_offset)
3364 (*_bfd_error_handler)
3365 (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3366 input_bfd,
3367 input_section,
3368 (long) rel->r_offset,
3369 stub_entry->root.string);
3370 bfd_set_error (bfd_error_bad_value);
3371 return bfd_reloc_notsupported;
3374 val = hppa_field_adjust (value, addend, r_field);
3376 switch (r_type)
3378 case R_PARISC_PCREL12F:
3379 case R_PARISC_PCREL17C:
3380 case R_PARISC_PCREL17F:
3381 case R_PARISC_PCREL17R:
3382 case R_PARISC_PCREL22F:
3383 case R_PARISC_DIR17F:
3384 case R_PARISC_DIR17R:
3385 /* This is a branch. Divide the offset by four.
3386 Note that we need to decide whether it's a branch or
3387 otherwise by inspecting the reloc. Inspecting insn won't
3388 work as insn might be from a .word directive. */
3389 val >>= 2;
3390 break;
3392 default:
3393 break;
3396 insn = hppa_rebuild_insn (insn, val, r_format);
3398 /* Update the instruction word. */
3399 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3400 return bfd_reloc_ok;
3403 /* Relocate an HPPA ELF section. */
3405 static bfd_boolean
3406 elf32_hppa_relocate_section (bfd *output_bfd,
3407 struct bfd_link_info *info,
3408 bfd *input_bfd,
3409 asection *input_section,
3410 bfd_byte *contents,
3411 Elf_Internal_Rela *relocs,
3412 Elf_Internal_Sym *local_syms,
3413 asection **local_sections)
3415 bfd_vma *local_got_offsets;
3416 struct elf32_hppa_link_hash_table *htab;
3417 Elf_Internal_Shdr *symtab_hdr;
3418 Elf_Internal_Rela *rel;
3419 Elf_Internal_Rela *relend;
3421 if (info->relocatable)
3422 return TRUE;
3424 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3426 htab = hppa_link_hash_table (info);
3427 local_got_offsets = elf_local_got_offsets (input_bfd);
3429 rel = relocs;
3430 relend = relocs + input_section->reloc_count;
3431 for (; rel < relend; rel++)
3433 unsigned int r_type;
3434 reloc_howto_type *howto;
3435 unsigned int r_symndx;
3436 struct elf32_hppa_link_hash_entry *h;
3437 Elf_Internal_Sym *sym;
3438 asection *sym_sec;
3439 bfd_vma relocation;
3440 bfd_reloc_status_type r;
3441 const char *sym_name;
3442 bfd_boolean plabel;
3443 bfd_boolean warned_undef;
3445 r_type = ELF32_R_TYPE (rel->r_info);
3446 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3448 bfd_set_error (bfd_error_bad_value);
3449 return FALSE;
3451 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3452 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3453 continue;
3455 /* This is a final link. */
3456 r_symndx = ELF32_R_SYM (rel->r_info);
3457 h = NULL;
3458 sym = NULL;
3459 sym_sec = NULL;
3460 warned_undef = FALSE;
3461 if (r_symndx < symtab_hdr->sh_info)
3463 /* This is a local symbol, h defaults to NULL. */
3464 sym = local_syms + r_symndx;
3465 sym_sec = local_sections[r_symndx];
3466 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
3468 else
3470 struct elf_link_hash_entry *hh;
3471 bfd_boolean unresolved_reloc;
3472 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3474 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3475 r_symndx, symtab_hdr, sym_hashes,
3476 hh, sym_sec, relocation,
3477 unresolved_reloc, warned_undef);
3479 if (relocation == 0
3480 && hh->root.type != bfd_link_hash_defined
3481 && hh->root.type != bfd_link_hash_defweak
3482 && hh->root.type != bfd_link_hash_undefweak)
3484 if (info->unresolved_syms_in_objects == RM_IGNORE
3485 && ELF_ST_VISIBILITY (hh->other) == STV_DEFAULT
3486 && hh->type == STT_PARISC_MILLI)
3488 if (! info->callbacks->undefined_symbol
3489 (info, hh->root.root.string, input_bfd,
3490 input_section, rel->r_offset, FALSE))
3491 return FALSE;
3492 warned_undef = TRUE;
3495 h = (struct elf32_hppa_link_hash_entry *) hh;
3498 /* Do any required modifications to the relocation value, and
3499 determine what types of dynamic info we need to output, if
3500 any. */
3501 plabel = 0;
3502 switch (r_type)
3504 case R_PARISC_DLTIND14F:
3505 case R_PARISC_DLTIND14R:
3506 case R_PARISC_DLTIND21L:
3508 bfd_vma off;
3509 bfd_boolean do_got = 0;
3511 /* Relocation is to the entry for this symbol in the
3512 global offset table. */
3513 if (h != NULL)
3515 bfd_boolean dyn;
3517 off = h->elf.got.offset;
3518 dyn = htab->elf.dynamic_sections_created;
3519 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
3520 &h->elf))
3522 /* If we aren't going to call finish_dynamic_symbol,
3523 then we need to handle initialisation of the .got
3524 entry and create needed relocs here. Since the
3525 offset must always be a multiple of 4, we use the
3526 least significant bit to record whether we have
3527 initialised it already. */
3528 if ((off & 1) != 0)
3529 off &= ~1;
3530 else
3532 h->elf.got.offset |= 1;
3533 do_got = 1;
3537 else
3539 /* Local symbol case. */
3540 if (local_got_offsets == NULL)
3541 abort ();
3543 off = local_got_offsets[r_symndx];
3545 /* The offset must always be a multiple of 4. We use
3546 the least significant bit to record whether we have
3547 already generated the necessary reloc. */
3548 if ((off & 1) != 0)
3549 off &= ~1;
3550 else
3552 local_got_offsets[r_symndx] |= 1;
3553 do_got = 1;
3557 if (do_got)
3559 if (info->shared)
3561 /* Output a dynamic relocation for this GOT entry.
3562 In this case it is relative to the base of the
3563 object because the symbol index is zero. */
3564 Elf_Internal_Rela outrel;
3565 bfd_byte *loc;
3566 asection *s = htab->srelgot;
3568 outrel.r_offset = (off
3569 + htab->sgot->output_offset
3570 + htab->sgot->output_section->vma);
3571 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3572 outrel.r_addend = relocation;
3573 loc = s->contents;
3574 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3575 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3577 else
3578 bfd_put_32 (output_bfd, relocation,
3579 htab->sgot->contents + off);
3582 if (off >= (bfd_vma) -2)
3583 abort ();
3585 /* Add the base of the GOT to the relocation value. */
3586 relocation = (off
3587 + htab->sgot->output_offset
3588 + htab->sgot->output_section->vma);
3590 break;
3592 case R_PARISC_SEGREL32:
3593 /* If this is the first SEGREL relocation, then initialize
3594 the segment base values. */
3595 if (htab->text_segment_base == (bfd_vma) -1)
3596 bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
3597 break;
3599 case R_PARISC_PLABEL14R:
3600 case R_PARISC_PLABEL21L:
3601 case R_PARISC_PLABEL32:
3602 if (htab->elf.dynamic_sections_created)
3604 bfd_vma off;
3605 bfd_boolean do_plt = 0;
3607 /* If we have a global symbol with a PLT slot, then
3608 redirect this relocation to it. */
3609 if (h != NULL)
3611 off = h->elf.plt.offset;
3612 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared,
3613 &h->elf))
3615 /* In a non-shared link, adjust_dynamic_symbols
3616 isn't called for symbols forced local. We
3617 need to write out the plt entry here. */
3618 if ((off & 1) != 0)
3619 off &= ~1;
3620 else
3622 h->elf.plt.offset |= 1;
3623 do_plt = 1;
3627 else
3629 bfd_vma *local_plt_offsets;
3631 if (local_got_offsets == NULL)
3632 abort ();
3634 local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
3635 off = local_plt_offsets[r_symndx];
3637 /* As for the local .got entry case, we use the last
3638 bit to record whether we've already initialised
3639 this local .plt entry. */
3640 if ((off & 1) != 0)
3641 off &= ~1;
3642 else
3644 local_plt_offsets[r_symndx] |= 1;
3645 do_plt = 1;
3649 if (do_plt)
3651 if (info->shared)
3653 /* Output a dynamic IPLT relocation for this
3654 PLT entry. */
3655 Elf_Internal_Rela outrel;
3656 bfd_byte *loc;
3657 asection *s = htab->srelplt;
3659 outrel.r_offset = (off
3660 + htab->splt->output_offset
3661 + htab->splt->output_section->vma);
3662 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3663 outrel.r_addend = relocation;
3664 loc = s->contents;
3665 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3666 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3668 else
3670 bfd_put_32 (output_bfd,
3671 relocation,
3672 htab->splt->contents + off);
3673 bfd_put_32 (output_bfd,
3674 elf_gp (htab->splt->output_section->owner),
3675 htab->splt->contents + off + 4);
3679 if (off >= (bfd_vma) -2)
3680 abort ();
3682 /* PLABELs contain function pointers. Relocation is to
3683 the entry for the function in the .plt. The magic +2
3684 offset signals to $$dyncall that the function pointer
3685 is in the .plt and thus has a gp pointer too.
3686 Exception: Undefined PLABELs should have a value of
3687 zero. */
3688 if (h == NULL
3689 || (h->elf.root.type != bfd_link_hash_undefweak
3690 && h->elf.root.type != bfd_link_hash_undefined))
3692 relocation = (off
3693 + htab->splt->output_offset
3694 + htab->splt->output_section->vma
3695 + 2);
3697 plabel = 1;
3699 /* Fall through and possibly emit a dynamic relocation. */
3701 case R_PARISC_DIR17F:
3702 case R_PARISC_DIR17R:
3703 case R_PARISC_DIR14F:
3704 case R_PARISC_DIR14R:
3705 case R_PARISC_DIR21L:
3706 case R_PARISC_DPREL14F:
3707 case R_PARISC_DPREL14R:
3708 case R_PARISC_DPREL21L:
3709 case R_PARISC_DIR32:
3710 /* r_symndx will be zero only for relocs against symbols
3711 from removed linkonce sections, or sections discarded by
3712 a linker script. */
3713 if (r_symndx == 0
3714 || (input_section->flags & SEC_ALLOC) == 0)
3715 break;
3717 /* The reloc types handled here and this conditional
3718 expression must match the code in ..check_relocs and
3719 allocate_dynrelocs. ie. We need exactly the same condition
3720 as in ..check_relocs, with some extra conditions (dynindx
3721 test in this case) to cater for relocs removed by
3722 allocate_dynrelocs. If you squint, the non-shared test
3723 here does indeed match the one in ..check_relocs, the
3724 difference being that here we test DEF_DYNAMIC as well as
3725 !DEF_REGULAR. All common syms end up with !DEF_REGULAR,
3726 which is why we can't use just that test here.
3727 Conversely, DEF_DYNAMIC can't be used in check_relocs as
3728 there all files have not been loaded. */
3729 if ((info->shared
3730 && (h == NULL
3731 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
3732 || h->elf.root.type != bfd_link_hash_undefweak)
3733 && (IS_ABSOLUTE_RELOC (r_type)
3734 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
3735 || (!info->shared
3736 && h != NULL
3737 && h->elf.dynindx != -1
3738 && !h->elf.non_got_ref
3739 && ((ELIMINATE_COPY_RELOCS
3740 && h->elf.def_dynamic
3741 && !h->elf.def_regular)
3742 || h->elf.root.type == bfd_link_hash_undefweak
3743 || h->elf.root.type == bfd_link_hash_undefined)))
3745 Elf_Internal_Rela outrel;
3746 bfd_boolean skip;
3747 asection *sreloc;
3748 bfd_byte *loc;
3750 /* When generating a shared object, these relocations
3751 are copied into the output file to be resolved at run
3752 time. */
3754 outrel.r_addend = rel->r_addend;
3755 outrel.r_offset =
3756 _bfd_elf_section_offset (output_bfd, info, input_section,
3757 rel->r_offset);
3758 skip = (outrel.r_offset == (bfd_vma) -1
3759 || outrel.r_offset == (bfd_vma) -2);
3760 outrel.r_offset += (input_section->output_offset
3761 + input_section->output_section->vma);
3763 if (skip)
3765 memset (&outrel, 0, sizeof (outrel));
3767 else if (h != NULL
3768 && h->elf.dynindx != -1
3769 && (plabel
3770 || !IS_ABSOLUTE_RELOC (r_type)
3771 || !info->shared
3772 || !info->symbolic
3773 || !h->elf.def_regular))
3775 outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3777 else /* It's a local symbol, or one marked to become local. */
3779 int indx = 0;
3781 /* Add the absolute offset of the symbol. */
3782 outrel.r_addend += relocation;
3784 /* Global plabels need to be processed by the
3785 dynamic linker so that functions have at most one
3786 fptr. For this reason, we need to differentiate
3787 between global and local plabels, which we do by
3788 providing the function symbol for a global plabel
3789 reloc, and no symbol for local plabels. */
3790 if (! plabel
3791 && sym_sec != NULL
3792 && sym_sec->output_section != NULL
3793 && ! bfd_is_abs_section (sym_sec))
3795 /* Skip this relocation if the output section has
3796 been discarded. */
3797 if (bfd_is_abs_section (sym_sec->output_section))
3798 break;
3800 indx = elf_section_data (sym_sec->output_section)->dynindx;
3801 /* We are turning this relocation into one
3802 against a section symbol, so subtract out the
3803 output section's address but not the offset
3804 of the input section in the output section. */
3805 outrel.r_addend -= sym_sec->output_section->vma;
3808 outrel.r_info = ELF32_R_INFO (indx, r_type);
3810 sreloc = elf_section_data (input_section)->sreloc;
3811 if (sreloc == NULL)
3812 abort ();
3814 loc = sreloc->contents;
3815 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3816 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3818 break;
3820 default:
3821 break;
3824 r = final_link_relocate (input_section, contents, rel, relocation,
3825 htab, sym_sec, h, info);
3827 if (r == bfd_reloc_ok)
3828 continue;
3830 if (h != NULL)
3831 sym_name = h->elf.root.root.string;
3832 else
3834 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3835 symtab_hdr->sh_link,
3836 sym->st_name);
3837 if (sym_name == NULL)
3838 return FALSE;
3839 if (*sym_name == '\0')
3840 sym_name = bfd_section_name (input_bfd, sym_sec);
3843 howto = elf_hppa_howto_table + r_type;
3845 if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
3847 if (r == bfd_reloc_notsupported || !warned_undef)
3849 (*_bfd_error_handler)
3850 (_("%B(%A+0x%lx): cannot handle %s for %s"),
3851 input_bfd,
3852 input_section,
3853 (long) rel->r_offset,
3854 howto->name,
3855 sym_name);
3856 bfd_set_error (bfd_error_bad_value);
3857 return FALSE;
3860 else
3862 if (!((*info->callbacks->reloc_overflow)
3863 (info, (h ? &h->elf.root : NULL), sym_name, howto->name,
3864 (bfd_vma) 0, input_bfd, input_section, rel->r_offset)))
3865 return FALSE;
3869 return TRUE;
3872 /* Finish up dynamic symbol handling. We set the contents of various
3873 dynamic sections here. */
3875 static bfd_boolean
3876 elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
3877 struct bfd_link_info *info,
3878 struct elf_link_hash_entry *h,
3879 Elf_Internal_Sym *sym)
3881 struct elf32_hppa_link_hash_table *htab;
3882 Elf_Internal_Rela rel;
3883 bfd_byte *loc;
3885 htab = hppa_link_hash_table (info);
3887 if (h->plt.offset != (bfd_vma) -1)
3889 bfd_vma value;
3891 if (h->plt.offset & 1)
3892 abort ();
3894 /* This symbol has an entry in the procedure linkage table. Set
3895 it up.
3897 The format of a plt entry is
3898 <funcaddr>
3899 <__gp>
3901 value = 0;
3902 if (h->root.type == bfd_link_hash_defined
3903 || h->root.type == bfd_link_hash_defweak)
3905 value = h->root.u.def.value;
3906 if (h->root.u.def.section->output_section != NULL)
3907 value += (h->root.u.def.section->output_offset
3908 + h->root.u.def.section->output_section->vma);
3911 /* Create a dynamic IPLT relocation for this entry. */
3912 rel.r_offset = (h->plt.offset
3913 + htab->splt->output_offset
3914 + htab->splt->output_section->vma);
3915 if (h->dynindx != -1)
3917 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
3918 rel.r_addend = 0;
3920 else
3922 /* This symbol has been marked to become local, and is
3923 used by a plabel so must be kept in the .plt. */
3924 rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3925 rel.r_addend = value;
3928 loc = htab->srelplt->contents;
3929 loc += htab->srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
3930 bfd_elf32_swap_reloca_out (htab->splt->output_section->owner, &rel, loc);
3932 if (!h->def_regular)
3934 /* Mark the symbol as undefined, rather than as defined in
3935 the .plt section. Leave the value alone. */
3936 sym->st_shndx = SHN_UNDEF;
3940 if (h->got.offset != (bfd_vma) -1)
3942 /* This symbol has an entry in the global offset table. Set it
3943 up. */
3945 rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
3946 + htab->sgot->output_offset
3947 + htab->sgot->output_section->vma);
3949 /* If this is a -Bsymbolic link and the symbol is defined
3950 locally or was forced to be local because of a version file,
3951 we just want to emit a RELATIVE reloc. The entry in the
3952 global offset table will already have been initialized in the
3953 relocate_section function. */
3954 if (info->shared
3955 && (info->symbolic || h->dynindx == -1)
3956 && h->def_regular)
3958 rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3959 rel.r_addend = (h->root.u.def.value
3960 + h->root.u.def.section->output_offset
3961 + h->root.u.def.section->output_section->vma);
3963 else
3965 if ((h->got.offset & 1) != 0)
3966 abort ();
3967 bfd_put_32 (output_bfd, 0, htab->sgot->contents + h->got.offset);
3968 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
3969 rel.r_addend = 0;
3972 loc = htab->srelgot->contents;
3973 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3974 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3977 if (h->needs_copy)
3979 asection *s;
3981 /* This symbol needs a copy reloc. Set it up. */
3983 if (! (h->dynindx != -1
3984 && (h->root.type == bfd_link_hash_defined
3985 || h->root.type == bfd_link_hash_defweak)))
3986 abort ();
3988 s = htab->srelbss;
3990 rel.r_offset = (h->root.u.def.value
3991 + h->root.u.def.section->output_offset
3992 + h->root.u.def.section->output_section->vma);
3993 rel.r_addend = 0;
3994 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
3995 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3996 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3999 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4000 if (h->root.root.string[0] == '_'
4001 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4002 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
4004 sym->st_shndx = SHN_ABS;
4007 return TRUE;
4010 /* Used to decide how to sort relocs in an optimal manner for the
4011 dynamic linker, before writing them out. */
4013 static enum elf_reloc_type_class
4014 elf32_hppa_reloc_type_class (const Elf_Internal_Rela *rela)
4016 if (ELF32_R_SYM (rela->r_info) == 0)
4017 return reloc_class_relative;
4019 switch ((int) ELF32_R_TYPE (rela->r_info))
4021 case R_PARISC_IPLT:
4022 return reloc_class_plt;
4023 case R_PARISC_COPY:
4024 return reloc_class_copy;
4025 default:
4026 return reloc_class_normal;
4030 /* Finish up the dynamic sections. */
4032 static bfd_boolean
4033 elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
4034 struct bfd_link_info *info)
4036 bfd *dynobj;
4037 struct elf32_hppa_link_hash_table *htab;
4038 asection *sdyn;
4040 htab = hppa_link_hash_table (info);
4041 dynobj = htab->elf.dynobj;
4043 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4045 if (htab->elf.dynamic_sections_created)
4047 Elf32_External_Dyn *dyncon, *dynconend;
4049 if (sdyn == NULL)
4050 abort ();
4052 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4053 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4054 for (; dyncon < dynconend; dyncon++)
4056 Elf_Internal_Dyn dyn;
4057 asection *s;
4059 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4061 switch (dyn.d_tag)
4063 default:
4064 continue;
4066 case DT_PLTGOT:
4067 /* Use PLTGOT to set the GOT register. */
4068 dyn.d_un.d_ptr = elf_gp (output_bfd);
4069 break;
4071 case DT_JMPREL:
4072 s = htab->srelplt;
4073 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4074 break;
4076 case DT_PLTRELSZ:
4077 s = htab->srelplt;
4078 dyn.d_un.d_val = s->size;
4079 break;
4081 case DT_RELASZ:
4082 /* Don't count procedure linkage table relocs in the
4083 overall reloc count. */
4084 s = htab->srelplt;
4085 if (s == NULL)
4086 continue;
4087 dyn.d_un.d_val -= s->size;
4088 break;
4090 case DT_RELA:
4091 /* We may not be using the standard ELF linker script.
4092 If .rela.plt is the first .rela section, we adjust
4093 DT_RELA to not include it. */
4094 s = htab->srelplt;
4095 if (s == NULL)
4096 continue;
4097 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4098 continue;
4099 dyn.d_un.d_ptr += s->size;
4100 break;
4103 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4107 if (htab->sgot != NULL && htab->sgot->size != 0)
4109 /* Fill in the first entry in the global offset table.
4110 We use it to point to our dynamic section, if we have one. */
4111 bfd_put_32 (output_bfd,
4112 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
4113 htab->sgot->contents);
4115 /* The second entry is reserved for use by the dynamic linker. */
4116 memset (htab->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4118 /* Set .got entry size. */
4119 elf_section_data (htab->sgot->output_section)
4120 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4123 if (htab->splt != NULL && htab->splt->size != 0)
4125 /* Set plt entry size. */
4126 elf_section_data (htab->splt->output_section)
4127 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4129 if (htab->need_plt_stub)
4131 /* Set up the .plt stub. */
4132 memcpy (htab->splt->contents
4133 + htab->splt->size - sizeof (plt_stub),
4134 plt_stub, sizeof (plt_stub));
4136 if ((htab->splt->output_offset
4137 + htab->splt->output_section->vma
4138 + htab->splt->size)
4139 != (htab->sgot->output_offset
4140 + htab->sgot->output_section->vma))
4142 (*_bfd_error_handler)
4143 (_(".got section not immediately after .plt section"));
4144 return FALSE;
4149 return TRUE;
4152 /* Tweak the OSABI field of the elf header. */
4154 static void
4155 elf32_hppa_post_process_headers (bfd *abfd,
4156 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4158 Elf_Internal_Ehdr * i_ehdrp;
4160 i_ehdrp = elf_elfheader (abfd);
4162 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
4164 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
4166 else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
4168 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NETBSD;
4170 else
4172 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4176 /* Called when writing out an object file to decide the type of a
4177 symbol. */
4178 static int
4179 elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
4181 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4182 return STT_PARISC_MILLI;
4183 else
4184 return type;
4187 /* Misc BFD support code. */
4188 #define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4189 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4190 #define elf_info_to_howto elf_hppa_info_to_howto
4191 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
4193 /* Stuff for the BFD linker. */
4194 #define bfd_elf32_bfd_final_link elf32_hppa_final_link
4195 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4196 #define bfd_elf32_bfd_link_hash_table_free elf32_hppa_link_hash_table_free
4197 #define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
4198 #define elf_backend_copy_indirect_symbol elf32_hppa_copy_indirect_symbol
4199 #define elf_backend_check_relocs elf32_hppa_check_relocs
4200 #define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4201 #define elf_backend_fake_sections elf_hppa_fake_sections
4202 #define elf_backend_relocate_section elf32_hppa_relocate_section
4203 #define elf_backend_hide_symbol elf32_hppa_hide_symbol
4204 #define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4205 #define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4206 #define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4207 #define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4208 #define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
4209 #define elf_backend_grok_prstatus elf32_hppa_grok_prstatus
4210 #define elf_backend_grok_psinfo elf32_hppa_grok_psinfo
4211 #define elf_backend_object_p elf32_hppa_object_p
4212 #define elf_backend_final_write_processing elf_hppa_final_write_processing
4213 #define elf_backend_post_process_headers elf32_hppa_post_process_headers
4214 #define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
4215 #define elf_backend_reloc_type_class elf32_hppa_reloc_type_class
4217 #define elf_backend_can_gc_sections 1
4218 #define elf_backend_can_refcount 1
4219 #define elf_backend_plt_alignment 2
4220 #define elf_backend_want_got_plt 0
4221 #define elf_backend_plt_readonly 0
4222 #define elf_backend_want_plt_sym 0
4223 #define elf_backend_got_header_size 8
4224 #define elf_backend_rela_normal 1
4226 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
4227 #define TARGET_BIG_NAME "elf32-hppa"
4228 #define ELF_ARCH bfd_arch_hppa
4229 #define ELF_MACHINE_CODE EM_PARISC
4230 #define ELF_MAXPAGESIZE 0x1000
4232 #include "elf32-target.h"
4234 #undef TARGET_BIG_SYM
4235 #define TARGET_BIG_SYM bfd_elf32_hppa_linux_vec
4236 #undef TARGET_BIG_NAME
4237 #define TARGET_BIG_NAME "elf32-hppa-linux"
4239 #define INCLUDED_TARGET_FILE 1
4240 #include "elf32-target.h"
4242 #undef TARGET_BIG_SYM
4243 #define TARGET_BIG_SYM bfd_elf32_hppa_nbsd_vec
4244 #undef TARGET_BIG_NAME
4245 #define TARGET_BIG_NAME "elf32-hppa-netbsd"
4247 #include "elf32-target.h"