* ld-elf/group1.d: Accept OBJECT symbols.
[binutils.git] / bfd / elf32-hppa.c
blobd1539be1b95522a196049fbe11d0d23e4d544a63
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;
941 /* Don't try to create the .plt and .got twice. */
942 htab = hppa_link_hash_table (info);
943 if (htab->splt != NULL)
944 return TRUE;
946 /* Call the generic code to do most of the work. */
947 if (! _bfd_elf_create_dynamic_sections (abfd, info))
948 return FALSE;
950 htab->splt = bfd_get_section_by_name (abfd, ".plt");
951 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
953 htab->sgot = bfd_get_section_by_name (abfd, ".got");
954 htab->srelgot = bfd_make_section (abfd, ".rela.got");
955 if (htab->srelgot == NULL
956 || ! bfd_set_section_flags (abfd, htab->srelgot,
957 (SEC_ALLOC
958 | SEC_LOAD
959 | SEC_HAS_CONTENTS
960 | SEC_IN_MEMORY
961 | SEC_LINKER_CREATED
962 | SEC_READONLY))
963 || ! bfd_set_section_alignment (abfd, htab->srelgot, 2))
964 return FALSE;
966 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
967 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
969 return TRUE;
972 /* Copy the extra info we tack onto an elf_link_hash_entry. */
974 static void
975 elf32_hppa_copy_indirect_symbol (const struct elf_backend_data *bed,
976 struct elf_link_hash_entry *dir,
977 struct elf_link_hash_entry *ind)
979 struct elf32_hppa_link_hash_entry *edir, *eind;
981 edir = (struct elf32_hppa_link_hash_entry *) dir;
982 eind = (struct elf32_hppa_link_hash_entry *) ind;
984 if (eind->dyn_relocs != NULL)
986 if (edir->dyn_relocs != NULL)
988 struct elf32_hppa_dyn_reloc_entry **pp;
989 struct elf32_hppa_dyn_reloc_entry *p;
991 if (ind->root.type == bfd_link_hash_indirect)
992 abort ();
994 /* Add reloc counts against the weak sym to the strong sym
995 list. Merge any entries against the same section. */
996 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
998 struct elf32_hppa_dyn_reloc_entry *q;
1000 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1001 if (q->sec == p->sec)
1003 #if RELATIVE_DYNRELOCS
1004 q->relative_count += p->relative_count;
1005 #endif
1006 q->count += p->count;
1007 *pp = p->next;
1008 break;
1010 if (q == NULL)
1011 pp = &p->next;
1013 *pp = edir->dyn_relocs;
1016 edir->dyn_relocs = eind->dyn_relocs;
1017 eind->dyn_relocs = NULL;
1020 if (ELIMINATE_COPY_RELOCS
1021 && ind->root.type != bfd_link_hash_indirect
1022 && dir->dynamic_adjusted)
1024 /* If called to transfer flags for a weakdef during processing
1025 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1026 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1027 dir->ref_dynamic |= ind->ref_dynamic;
1028 dir->ref_regular |= ind->ref_regular;
1029 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1030 dir->needs_plt |= ind->needs_plt;
1032 else
1033 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1036 /* Look through the relocs for a section during the first phase, and
1037 calculate needed space in the global offset table, procedure linkage
1038 table, and dynamic reloc sections. At this point we haven't
1039 necessarily read all the input files. */
1041 static bfd_boolean
1042 elf32_hppa_check_relocs (bfd *abfd,
1043 struct bfd_link_info *info,
1044 asection *sec,
1045 const Elf_Internal_Rela *relocs)
1047 Elf_Internal_Shdr *symtab_hdr;
1048 struct elf_link_hash_entry **sym_hashes;
1049 const Elf_Internal_Rela *rel;
1050 const Elf_Internal_Rela *rel_end;
1051 struct elf32_hppa_link_hash_table *htab;
1052 asection *sreloc;
1053 asection *stubreloc;
1055 if (info->relocatable)
1056 return TRUE;
1058 htab = hppa_link_hash_table (info);
1059 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1060 sym_hashes = elf_sym_hashes (abfd);
1061 sreloc = NULL;
1062 stubreloc = NULL;
1064 rel_end = relocs + sec->reloc_count;
1065 for (rel = relocs; rel < rel_end; rel++)
1067 enum {
1068 NEED_GOT = 1,
1069 NEED_PLT = 2,
1070 NEED_DYNREL = 4,
1071 PLT_PLABEL = 8
1074 unsigned int r_symndx, r_type;
1075 struct elf32_hppa_link_hash_entry *h;
1076 int need_entry;
1078 r_symndx = ELF32_R_SYM (rel->r_info);
1080 if (r_symndx < symtab_hdr->sh_info)
1081 h = NULL;
1082 else
1083 h = ((struct elf32_hppa_link_hash_entry *)
1084 sym_hashes[r_symndx - symtab_hdr->sh_info]);
1086 r_type = ELF32_R_TYPE (rel->r_info);
1088 switch (r_type)
1090 case R_PARISC_DLTIND14F:
1091 case R_PARISC_DLTIND14R:
1092 case R_PARISC_DLTIND21L:
1093 /* This symbol requires a global offset table entry. */
1094 need_entry = NEED_GOT;
1095 break;
1097 case R_PARISC_PLABEL14R: /* "Official" procedure labels. */
1098 case R_PARISC_PLABEL21L:
1099 case R_PARISC_PLABEL32:
1100 /* If the addend is non-zero, we break badly. */
1101 if (rel->r_addend != 0)
1102 abort ();
1104 /* If we are creating a shared library, then we need to
1105 create a PLT entry for all PLABELs, because PLABELs with
1106 local symbols may be passed via a pointer to another
1107 object. Additionally, output a dynamic relocation
1108 pointing to the PLT entry.
1109 For executables, the original 32-bit ABI allowed two
1110 different styles of PLABELs (function pointers): For
1111 global functions, the PLABEL word points into the .plt
1112 two bytes past a (function address, gp) pair, and for
1113 local functions the PLABEL points directly at the
1114 function. The magic +2 for the first type allows us to
1115 differentiate between the two. As you can imagine, this
1116 is a real pain when it comes to generating code to call
1117 functions indirectly or to compare function pointers.
1118 We avoid the mess by always pointing a PLABEL into the
1119 .plt, even for local functions. */
1120 need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
1121 break;
1123 case R_PARISC_PCREL12F:
1124 htab->has_12bit_branch = 1;
1125 goto branch_common;
1127 case R_PARISC_PCREL17C:
1128 case R_PARISC_PCREL17F:
1129 htab->has_17bit_branch = 1;
1130 goto branch_common;
1132 case R_PARISC_PCREL22F:
1133 htab->has_22bit_branch = 1;
1134 branch_common:
1135 /* Function calls might need to go through the .plt, and
1136 might require long branch stubs. */
1137 if (h == NULL)
1139 /* We know local syms won't need a .plt entry, and if
1140 they need a long branch stub we can't guarantee that
1141 we can reach the stub. So just flag an error later
1142 if we're doing a shared link and find we need a long
1143 branch stub. */
1144 continue;
1146 else
1148 /* Global symbols will need a .plt entry if they remain
1149 global, and in most cases won't need a long branch
1150 stub. Unfortunately, we have to cater for the case
1151 where a symbol is forced local by versioning, or due
1152 to symbolic linking, and we lose the .plt entry. */
1153 need_entry = NEED_PLT;
1154 if (h->elf.type == STT_PARISC_MILLI)
1155 need_entry = 0;
1157 break;
1159 case R_PARISC_SEGBASE: /* Used to set segment base. */
1160 case R_PARISC_SEGREL32: /* Relative reloc, used for unwind. */
1161 case R_PARISC_PCREL14F: /* PC relative load/store. */
1162 case R_PARISC_PCREL14R:
1163 case R_PARISC_PCREL17R: /* External branches. */
1164 case R_PARISC_PCREL21L: /* As above, and for load/store too. */
1165 case R_PARISC_PCREL32:
1166 /* We don't need to propagate the relocation if linking a
1167 shared object since these are section relative. */
1168 continue;
1170 case R_PARISC_DPREL14F: /* Used for gp rel data load/store. */
1171 case R_PARISC_DPREL14R:
1172 case R_PARISC_DPREL21L:
1173 if (info->shared)
1175 (*_bfd_error_handler)
1176 (_("%B: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1177 abfd,
1178 elf_hppa_howto_table[r_type].name);
1179 bfd_set_error (bfd_error_bad_value);
1180 return FALSE;
1182 /* Fall through. */
1184 case R_PARISC_DIR17F: /* Used for external branches. */
1185 case R_PARISC_DIR17R:
1186 case R_PARISC_DIR14F: /* Used for load/store from absolute locn. */
1187 case R_PARISC_DIR14R:
1188 case R_PARISC_DIR21L: /* As above, and for ext branches too. */
1189 #if 0
1190 /* Help debug shared library creation. Any of the above
1191 relocs can be used in shared libs, but they may cause
1192 pages to become unshared. */
1193 if (info->shared)
1195 (*_bfd_error_handler)
1196 (_("%B: relocation %s should not be used when making a shared object; recompile with -fPIC"),
1197 abfd,
1198 elf_hppa_howto_table[r_type].name);
1200 /* Fall through. */
1201 #endif
1203 case R_PARISC_DIR32: /* .word relocs. */
1204 /* We may want to output a dynamic relocation later. */
1205 need_entry = NEED_DYNREL;
1206 break;
1208 /* This relocation describes the C++ object vtable hierarchy.
1209 Reconstruct it for later use during GC. */
1210 case R_PARISC_GNU_VTINHERIT:
1211 if (!bfd_elf_gc_record_vtinherit (abfd, sec, &h->elf, rel->r_offset))
1212 return FALSE;
1213 continue;
1215 /* This relocation describes which C++ vtable entries are actually
1216 used. Record for later use during GC. */
1217 case R_PARISC_GNU_VTENTRY:
1218 if (!bfd_elf_gc_record_vtentry (abfd, sec, &h->elf, rel->r_addend))
1219 return FALSE;
1220 continue;
1222 default:
1223 continue;
1226 /* Now carry out our orders. */
1227 if (need_entry & NEED_GOT)
1229 /* Allocate space for a GOT entry, as well as a dynamic
1230 relocation for this entry. */
1231 if (htab->sgot == NULL)
1233 if (htab->elf.dynobj == NULL)
1234 htab->elf.dynobj = abfd;
1235 if (!elf32_hppa_create_dynamic_sections (htab->elf.dynobj, info))
1236 return FALSE;
1239 if (h != NULL)
1241 h->elf.got.refcount += 1;
1243 else
1245 bfd_signed_vma *local_got_refcounts;
1247 /* This is a global offset table entry for a local symbol. */
1248 local_got_refcounts = elf_local_got_refcounts (abfd);
1249 if (local_got_refcounts == NULL)
1251 bfd_size_type size;
1253 /* Allocate space for local got offsets and local
1254 plt offsets. Done this way to save polluting
1255 elf_obj_tdata with another target specific
1256 pointer. */
1257 size = symtab_hdr->sh_info;
1258 size *= 2 * sizeof (bfd_signed_vma);
1259 local_got_refcounts = bfd_zalloc (abfd, size);
1260 if (local_got_refcounts == NULL)
1261 return FALSE;
1262 elf_local_got_refcounts (abfd) = local_got_refcounts;
1264 local_got_refcounts[r_symndx] += 1;
1268 if (need_entry & NEED_PLT)
1270 /* If we are creating a shared library, and this is a reloc
1271 against a weak symbol or a global symbol in a dynamic
1272 object, then we will be creating an import stub and a
1273 .plt entry for the symbol. Similarly, on a normal link
1274 to symbols defined in a dynamic object we'll need the
1275 import stub and a .plt entry. We don't know yet whether
1276 the symbol is defined or not, so make an entry anyway and
1277 clean up later in adjust_dynamic_symbol. */
1278 if ((sec->flags & SEC_ALLOC) != 0)
1280 if (h != NULL)
1282 h->elf.needs_plt = 1;
1283 h->elf.plt.refcount += 1;
1285 /* If this .plt entry is for a plabel, mark it so
1286 that adjust_dynamic_symbol will keep the entry
1287 even if it appears to be local. */
1288 if (need_entry & PLT_PLABEL)
1289 h->plabel = 1;
1291 else if (need_entry & PLT_PLABEL)
1293 bfd_signed_vma *local_got_refcounts;
1294 bfd_signed_vma *local_plt_refcounts;
1296 local_got_refcounts = elf_local_got_refcounts (abfd);
1297 if (local_got_refcounts == NULL)
1299 bfd_size_type size;
1301 /* Allocate space for local got offsets and local
1302 plt offsets. */
1303 size = symtab_hdr->sh_info;
1304 size *= 2 * sizeof (bfd_signed_vma);
1305 local_got_refcounts = bfd_zalloc (abfd, size);
1306 if (local_got_refcounts == NULL)
1307 return FALSE;
1308 elf_local_got_refcounts (abfd) = local_got_refcounts;
1310 local_plt_refcounts = (local_got_refcounts
1311 + symtab_hdr->sh_info);
1312 local_plt_refcounts[r_symndx] += 1;
1317 if (need_entry & NEED_DYNREL)
1319 /* Flag this symbol as having a non-got, non-plt reference
1320 so that we generate copy relocs if it turns out to be
1321 dynamic. */
1322 if (h != NULL && !info->shared)
1323 h->elf.non_got_ref = 1;
1325 /* If we are creating a shared library then we need to copy
1326 the reloc into the shared library. However, if we are
1327 linking with -Bsymbolic, we need only copy absolute
1328 relocs or relocs against symbols that are not defined in
1329 an object we are including in the link. PC- or DP- or
1330 DLT-relative relocs against any local sym or global sym
1331 with DEF_REGULAR set, can be discarded. At this point we
1332 have not seen all the input files, so it is possible that
1333 DEF_REGULAR is not set now but will be set later (it is
1334 never cleared). We account for that possibility below by
1335 storing information in the dyn_relocs field of the
1336 hash table entry.
1338 A similar situation to the -Bsymbolic case occurs when
1339 creating shared libraries and symbol visibility changes
1340 render the symbol local.
1342 As it turns out, all the relocs we will be creating here
1343 are absolute, so we cannot remove them on -Bsymbolic
1344 links or visibility changes anyway. A STUB_REL reloc
1345 is absolute too, as in that case it is the reloc in the
1346 stub we will be creating, rather than copying the PCREL
1347 reloc in the branch.
1349 If on the other hand, we are creating an executable, we
1350 may need to keep relocations for symbols satisfied by a
1351 dynamic library if we manage to avoid copy relocs for the
1352 symbol. */
1353 if ((info->shared
1354 && (sec->flags & SEC_ALLOC) != 0
1355 && (IS_ABSOLUTE_RELOC (r_type)
1356 || (h != NULL
1357 && (!info->symbolic
1358 || h->elf.root.type == bfd_link_hash_defweak
1359 || !h->elf.def_regular))))
1360 || (ELIMINATE_COPY_RELOCS
1361 && !info->shared
1362 && (sec->flags & SEC_ALLOC) != 0
1363 && h != NULL
1364 && (h->elf.root.type == bfd_link_hash_defweak
1365 || !h->elf.def_regular)))
1367 struct elf32_hppa_dyn_reloc_entry *p;
1368 struct elf32_hppa_dyn_reloc_entry **head;
1370 /* Create a reloc section in dynobj and make room for
1371 this reloc. */
1372 if (sreloc == NULL)
1374 char *name;
1375 bfd *dynobj;
1377 name = (bfd_elf_string_from_elf_section
1378 (abfd,
1379 elf_elfheader (abfd)->e_shstrndx,
1380 elf_section_data (sec)->rel_hdr.sh_name));
1381 if (name == NULL)
1383 (*_bfd_error_handler)
1384 (_("Could not find relocation section for %s"),
1385 sec->name);
1386 bfd_set_error (bfd_error_bad_value);
1387 return FALSE;
1390 if (htab->elf.dynobj == NULL)
1391 htab->elf.dynobj = abfd;
1393 dynobj = htab->elf.dynobj;
1394 sreloc = bfd_get_section_by_name (dynobj, name);
1395 if (sreloc == NULL)
1397 flagword flags;
1399 sreloc = bfd_make_section (dynobj, name);
1400 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1401 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1402 if ((sec->flags & SEC_ALLOC) != 0)
1403 flags |= SEC_ALLOC | SEC_LOAD;
1404 if (sreloc == NULL
1405 || !bfd_set_section_flags (dynobj, sreloc, flags)
1406 || !bfd_set_section_alignment (dynobj, sreloc, 2))
1407 return FALSE;
1410 elf_section_data (sec)->sreloc = sreloc;
1413 /* If this is a global symbol, we count the number of
1414 relocations we need for this symbol. */
1415 if (h != NULL)
1417 head = &h->dyn_relocs;
1419 else
1421 /* Track dynamic relocs needed for local syms too.
1422 We really need local syms available to do this
1423 easily. Oh well. */
1425 asection *s;
1426 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1427 sec, r_symndx);
1428 if (s == NULL)
1429 return FALSE;
1431 head = ((struct elf32_hppa_dyn_reloc_entry **)
1432 &elf_section_data (s)->local_dynrel);
1435 p = *head;
1436 if (p == NULL || p->sec != sec)
1438 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
1439 if (p == NULL)
1440 return FALSE;
1441 p->next = *head;
1442 *head = p;
1443 p->sec = sec;
1444 p->count = 0;
1445 #if RELATIVE_DYNRELOCS
1446 p->relative_count = 0;
1447 #endif
1450 p->count += 1;
1451 #if RELATIVE_DYNRELOCS
1452 if (!IS_ABSOLUTE_RELOC (rtype))
1453 p->relative_count += 1;
1454 #endif
1459 return TRUE;
1462 /* Return the section that should be marked against garbage collection
1463 for a given relocation. */
1465 static asection *
1466 elf32_hppa_gc_mark_hook (asection *sec,
1467 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1468 Elf_Internal_Rela *rel,
1469 struct elf_link_hash_entry *h,
1470 Elf_Internal_Sym *sym)
1472 if (h != NULL)
1474 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1476 case R_PARISC_GNU_VTINHERIT:
1477 case R_PARISC_GNU_VTENTRY:
1478 break;
1480 default:
1481 switch (h->root.type)
1483 case bfd_link_hash_defined:
1484 case bfd_link_hash_defweak:
1485 return h->root.u.def.section;
1487 case bfd_link_hash_common:
1488 return h->root.u.c.p->section;
1490 default:
1491 break;
1495 else
1496 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1498 return NULL;
1501 /* Update the got and plt entry reference counts for the section being
1502 removed. */
1504 static bfd_boolean
1505 elf32_hppa_gc_sweep_hook (bfd *abfd,
1506 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1507 asection *sec,
1508 const Elf_Internal_Rela *relocs)
1510 Elf_Internal_Shdr *symtab_hdr;
1511 struct elf_link_hash_entry **sym_hashes;
1512 bfd_signed_vma *local_got_refcounts;
1513 bfd_signed_vma *local_plt_refcounts;
1514 const Elf_Internal_Rela *rel, *relend;
1516 elf_section_data (sec)->local_dynrel = NULL;
1518 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1519 sym_hashes = elf_sym_hashes (abfd);
1520 local_got_refcounts = elf_local_got_refcounts (abfd);
1521 local_plt_refcounts = local_got_refcounts;
1522 if (local_plt_refcounts != NULL)
1523 local_plt_refcounts += symtab_hdr->sh_info;
1525 relend = relocs + sec->reloc_count;
1526 for (rel = relocs; rel < relend; rel++)
1528 unsigned long r_symndx;
1529 unsigned int r_type;
1530 struct elf_link_hash_entry *h = NULL;
1532 r_symndx = ELF32_R_SYM (rel->r_info);
1533 if (r_symndx >= symtab_hdr->sh_info)
1535 struct elf32_hppa_link_hash_entry *eh;
1536 struct elf32_hppa_dyn_reloc_entry **pp;
1537 struct elf32_hppa_dyn_reloc_entry *p;
1539 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1540 while (h->root.type == bfd_link_hash_indirect
1541 || h->root.type == bfd_link_hash_warning)
1542 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1543 eh = (struct elf32_hppa_link_hash_entry *) h;
1545 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1546 if (p->sec == sec)
1548 /* Everything must go for SEC. */
1549 *pp = p->next;
1550 break;
1554 r_type = ELF32_R_TYPE (rel->r_info);
1555 switch (r_type)
1557 case R_PARISC_DLTIND14F:
1558 case R_PARISC_DLTIND14R:
1559 case R_PARISC_DLTIND21L:
1560 if (h != NULL)
1562 if (h->got.refcount > 0)
1563 h->got.refcount -= 1;
1565 else if (local_got_refcounts != NULL)
1567 if (local_got_refcounts[r_symndx] > 0)
1568 local_got_refcounts[r_symndx] -= 1;
1570 break;
1572 case R_PARISC_PCREL12F:
1573 case R_PARISC_PCREL17C:
1574 case R_PARISC_PCREL17F:
1575 case R_PARISC_PCREL22F:
1576 if (h != NULL)
1578 if (h->plt.refcount > 0)
1579 h->plt.refcount -= 1;
1581 break;
1583 case R_PARISC_PLABEL14R:
1584 case R_PARISC_PLABEL21L:
1585 case R_PARISC_PLABEL32:
1586 if (h != NULL)
1588 if (h->plt.refcount > 0)
1589 h->plt.refcount -= 1;
1591 else if (local_plt_refcounts != NULL)
1593 if (local_plt_refcounts[r_symndx] > 0)
1594 local_plt_refcounts[r_symndx] -= 1;
1596 break;
1598 default:
1599 break;
1603 return TRUE;
1606 /* Support for core dump NOTE sections. */
1608 static bfd_boolean
1609 elf32_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1611 int offset;
1612 size_t size;
1614 switch (note->descsz)
1616 default:
1617 return FALSE;
1619 case 396: /* Linux/hppa */
1620 /* pr_cursig */
1621 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1623 /* pr_pid */
1624 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1626 /* pr_reg */
1627 offset = 72;
1628 size = 320;
1630 break;
1633 /* Make a ".reg/999" section. */
1634 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1635 size, note->descpos + offset);
1638 static bfd_boolean
1639 elf32_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1641 switch (note->descsz)
1643 default:
1644 return FALSE;
1646 case 124: /* Linux/hppa elf_prpsinfo. */
1647 elf_tdata (abfd)->core_program
1648 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1649 elf_tdata (abfd)->core_command
1650 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1653 /* Note that for some reason, a spurious space is tacked
1654 onto the end of the args in some (at least one anyway)
1655 implementations, so strip it off if it exists. */
1657 char *command = elf_tdata (abfd)->core_command;
1658 int n = strlen (command);
1660 if (0 < n && command[n - 1] == ' ')
1661 command[n - 1] = '\0';
1664 return TRUE;
1667 /* Our own version of hide_symbol, so that we can keep plt entries for
1668 plabels. */
1670 static void
1671 elf32_hppa_hide_symbol (struct bfd_link_info *info,
1672 struct elf_link_hash_entry *h,
1673 bfd_boolean force_local)
1675 if (force_local)
1677 h->forced_local = 1;
1678 if (h->dynindx != -1)
1680 h->dynindx = -1;
1681 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1682 h->dynstr_index);
1686 if (! ((struct elf32_hppa_link_hash_entry *) h)->plabel)
1688 h->needs_plt = 0;
1689 h->plt = elf_hash_table (info)->init_refcount;
1693 /* Adjust a symbol defined by a dynamic object and referenced by a
1694 regular object. The current definition is in some section of the
1695 dynamic object, but we're not including those sections. We have to
1696 change the definition to something the rest of the link can
1697 understand. */
1699 static bfd_boolean
1700 elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
1701 struct elf_link_hash_entry *h)
1703 struct elf32_hppa_link_hash_table *htab;
1704 asection *s;
1705 unsigned int power_of_two;
1707 /* If this is a function, put it in the procedure linkage table. We
1708 will fill in the contents of the procedure linkage table later. */
1709 if (h->type == STT_FUNC
1710 || h->needs_plt)
1712 if (h->plt.refcount <= 0
1713 || (h->def_regular
1714 && h->root.type != bfd_link_hash_defweak
1715 && ! ((struct elf32_hppa_link_hash_entry *) h)->plabel
1716 && (!info->shared || info->symbolic)))
1718 /* The .plt entry is not needed when:
1719 a) Garbage collection has removed all references to the
1720 symbol, or
1721 b) We know for certain the symbol is defined in this
1722 object, and it's not a weak definition, nor is the symbol
1723 used by a plabel relocation. Either this object is the
1724 application or we are doing a shared symbolic link. */
1726 h->plt.offset = (bfd_vma) -1;
1727 h->needs_plt = 0;
1730 return TRUE;
1732 else
1733 h->plt.offset = (bfd_vma) -1;
1735 /* If this is a weak symbol, and there is a real definition, the
1736 processor independent code will have arranged for us to see the
1737 real definition first, and we can just use the same value. */
1738 if (h->u.weakdef != NULL)
1740 if (h->u.weakdef->root.type != bfd_link_hash_defined
1741 && h->u.weakdef->root.type != bfd_link_hash_defweak)
1742 abort ();
1743 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1744 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1745 if (ELIMINATE_COPY_RELOCS)
1746 h->non_got_ref = h->u.weakdef->non_got_ref;
1747 return TRUE;
1750 /* This is a reference to a symbol defined by a dynamic object which
1751 is not a function. */
1753 /* If we are creating a shared library, we must presume that the
1754 only references to the symbol are via the global offset table.
1755 For such cases we need not do anything here; the relocations will
1756 be handled correctly by relocate_section. */
1757 if (info->shared)
1758 return TRUE;
1760 /* If there are no references to this symbol that do not use the
1761 GOT, we don't need to generate a copy reloc. */
1762 if (!h->non_got_ref)
1763 return TRUE;
1765 if (ELIMINATE_COPY_RELOCS)
1767 struct elf32_hppa_link_hash_entry *eh;
1768 struct elf32_hppa_dyn_reloc_entry *p;
1770 eh = (struct elf32_hppa_link_hash_entry *) h;
1771 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1773 s = p->sec->output_section;
1774 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1775 break;
1778 /* If we didn't find any dynamic relocs in read-only sections, then
1779 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1780 if (p == NULL)
1782 h->non_got_ref = 0;
1783 return TRUE;
1787 /* We must allocate the symbol in our .dynbss section, which will
1788 become part of the .bss section of the executable. There will be
1789 an entry for this symbol in the .dynsym section. The dynamic
1790 object will contain position independent code, so all references
1791 from the dynamic object to this symbol will go through the global
1792 offset table. The dynamic linker will use the .dynsym entry to
1793 determine the address it must put in the global offset table, so
1794 both the dynamic object and the regular object will refer to the
1795 same memory location for the variable. */
1797 htab = hppa_link_hash_table (info);
1799 /* We must generate a COPY reloc to tell the dynamic linker to
1800 copy the initial value out of the dynamic object and into the
1801 runtime process image. */
1802 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1804 htab->srelbss->size += sizeof (Elf32_External_Rela);
1805 h->needs_copy = 1;
1808 /* We need to figure out the alignment required for this symbol. I
1809 have no idea how other ELF linkers handle this. */
1811 power_of_two = bfd_log2 (h->size);
1812 if (power_of_two > 3)
1813 power_of_two = 3;
1815 /* Apply the required alignment. */
1816 s = htab->sdynbss;
1817 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1818 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1820 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1821 return FALSE;
1824 /* Define the symbol as being at this point in the section. */
1825 h->root.u.def.section = s;
1826 h->root.u.def.value = s->size;
1828 /* Increment the section size to make room for the symbol. */
1829 s->size += h->size;
1831 return TRUE;
1834 /* Allocate space in the .plt for entries that won't have relocations.
1835 ie. plabel entries. */
1837 static bfd_boolean
1838 allocate_plt_static (struct elf_link_hash_entry *h, void *inf)
1840 struct bfd_link_info *info;
1841 struct elf32_hppa_link_hash_table *htab;
1842 asection *s;
1844 if (h->root.type == bfd_link_hash_indirect)
1845 return TRUE;
1847 if (h->root.type == bfd_link_hash_warning)
1848 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1850 info = inf;
1851 htab = hppa_link_hash_table (info);
1852 if (htab->elf.dynamic_sections_created
1853 && h->plt.refcount > 0)
1855 /* Make sure this symbol is output as a dynamic symbol.
1856 Undefined weak syms won't yet be marked as dynamic. */
1857 if (h->dynindx == -1
1858 && !h->forced_local
1859 && h->type != STT_PARISC_MILLI)
1861 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1862 return FALSE;
1865 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
1867 /* Allocate these later. From this point on, h->plabel
1868 means that the plt entry is only used by a plabel.
1869 We'll be using a normal plt entry for this symbol, so
1870 clear the plabel indicator. */
1871 ((struct elf32_hppa_link_hash_entry *) h)->plabel = 0;
1873 else if (((struct elf32_hppa_link_hash_entry *) h)->plabel)
1875 /* Make an entry in the .plt section for plabel references
1876 that won't have a .plt entry for other reasons. */
1877 s = htab->splt;
1878 h->plt.offset = s->size;
1879 s->size += PLT_ENTRY_SIZE;
1881 else
1883 /* No .plt entry needed. */
1884 h->plt.offset = (bfd_vma) -1;
1885 h->needs_plt = 0;
1888 else
1890 h->plt.offset = (bfd_vma) -1;
1891 h->needs_plt = 0;
1894 return TRUE;
1897 /* Allocate space in .plt, .got and associated reloc sections for
1898 global syms. */
1900 static bfd_boolean
1901 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1903 struct bfd_link_info *info;
1904 struct elf32_hppa_link_hash_table *htab;
1905 asection *s;
1906 struct elf32_hppa_link_hash_entry *eh;
1907 struct elf32_hppa_dyn_reloc_entry *p;
1909 if (h->root.type == bfd_link_hash_indirect)
1910 return TRUE;
1912 if (h->root.type == bfd_link_hash_warning)
1913 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1915 info = inf;
1916 htab = hppa_link_hash_table (info);
1917 if (htab->elf.dynamic_sections_created
1918 && h->plt.offset != (bfd_vma) -1
1919 && !((struct elf32_hppa_link_hash_entry *) h)->plabel)
1921 /* Make an entry in the .plt section. */
1922 s = htab->splt;
1923 h->plt.offset = s->size;
1924 s->size += PLT_ENTRY_SIZE;
1926 /* We also need to make an entry in the .rela.plt section. */
1927 htab->srelplt->size += sizeof (Elf32_External_Rela);
1928 htab->need_plt_stub = 1;
1931 if (h->got.refcount > 0)
1933 /* Make sure this symbol is output as a dynamic symbol.
1934 Undefined weak syms won't yet be marked as dynamic. */
1935 if (h->dynindx == -1
1936 && !h->forced_local
1937 && h->type != STT_PARISC_MILLI)
1939 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1940 return FALSE;
1943 s = htab->sgot;
1944 h->got.offset = s->size;
1945 s->size += GOT_ENTRY_SIZE;
1946 if (htab->elf.dynamic_sections_created
1947 && (info->shared
1948 || (h->dynindx != -1
1949 && !h->forced_local)))
1951 htab->srelgot->size += sizeof (Elf32_External_Rela);
1954 else
1955 h->got.offset = (bfd_vma) -1;
1957 eh = (struct elf32_hppa_link_hash_entry *) h;
1958 if (eh->dyn_relocs == NULL)
1959 return TRUE;
1961 /* If this is a -Bsymbolic shared link, then we need to discard all
1962 space allocated for dynamic pc-relative relocs against symbols
1963 defined in a regular object. For the normal shared case, discard
1964 space for relocs that have become local due to symbol visibility
1965 changes. */
1966 if (info->shared)
1968 #if RELATIVE_DYNRELOCS
1969 if (SYMBOL_CALLS_LOCAL (info, h))
1971 struct elf32_hppa_dyn_reloc_entry **pp;
1973 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1975 p->count -= p->relative_count;
1976 p->relative_count = 0;
1977 if (p->count == 0)
1978 *pp = p->next;
1979 else
1980 pp = &p->next;
1983 #endif
1985 /* Also discard relocs on undefined weak syms with non-default
1986 visibility. */
1987 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1988 && h->root.type == bfd_link_hash_undefweak)
1989 eh->dyn_relocs = NULL;
1991 else
1993 /* For the non-shared case, discard space for relocs against
1994 symbols which turn out to need copy relocs or are not
1995 dynamic. */
1996 if (!h->non_got_ref
1997 && ((ELIMINATE_COPY_RELOCS
1998 && h->def_dynamic
1999 && !h->def_regular)
2000 || (htab->elf.dynamic_sections_created
2001 && (h->root.type == bfd_link_hash_undefweak
2002 || h->root.type == bfd_link_hash_undefined))))
2004 /* Make sure this symbol is output as a dynamic symbol.
2005 Undefined weak syms won't yet be marked as dynamic. */
2006 if (h->dynindx == -1
2007 && !h->forced_local
2008 && h->type != STT_PARISC_MILLI)
2010 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2011 return FALSE;
2014 /* If that succeeded, we know we'll be keeping all the
2015 relocs. */
2016 if (h->dynindx != -1)
2017 goto keep;
2020 eh->dyn_relocs = NULL;
2021 return TRUE;
2023 keep: ;
2026 /* Finally, allocate space. */
2027 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2029 asection *sreloc = elf_section_data (p->sec)->sreloc;
2030 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2033 return TRUE;
2036 /* This function is called via elf_link_hash_traverse to force
2037 millicode symbols local so they do not end up as globals in the
2038 dynamic symbol table. We ought to be able to do this in
2039 adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2040 for all dynamic symbols. Arguably, this is a bug in
2041 elf_adjust_dynamic_symbol. */
2043 static bfd_boolean
2044 clobber_millicode_symbols (struct elf_link_hash_entry *h,
2045 struct bfd_link_info *info)
2047 if (h->root.type == bfd_link_hash_warning)
2048 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2050 if (h->type == STT_PARISC_MILLI
2051 && !h->forced_local)
2053 elf32_hppa_hide_symbol (info, h, TRUE);
2055 return TRUE;
2058 /* Find any dynamic relocs that apply to read-only sections. */
2060 static bfd_boolean
2061 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2063 struct elf32_hppa_link_hash_entry *eh;
2064 struct elf32_hppa_dyn_reloc_entry *p;
2066 if (h->root.type == bfd_link_hash_warning)
2067 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2069 eh = (struct elf32_hppa_link_hash_entry *) h;
2070 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2072 asection *s = p->sec->output_section;
2074 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2076 struct bfd_link_info *info = inf;
2078 info->flags |= DF_TEXTREL;
2080 /* Not an error, just cut short the traversal. */
2081 return FALSE;
2084 return TRUE;
2087 /* Set the sizes of the dynamic sections. */
2089 static bfd_boolean
2090 elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2091 struct bfd_link_info *info)
2093 struct elf32_hppa_link_hash_table *htab;
2094 bfd *dynobj;
2095 bfd *ibfd;
2096 asection *s;
2097 bfd_boolean relocs;
2099 htab = hppa_link_hash_table (info);
2100 dynobj = htab->elf.dynobj;
2101 if (dynobj == NULL)
2102 abort ();
2104 if (htab->elf.dynamic_sections_created)
2106 /* Set the contents of the .interp section to the interpreter. */
2107 if (info->executable)
2109 s = bfd_get_section_by_name (dynobj, ".interp");
2110 if (s == NULL)
2111 abort ();
2112 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2113 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2116 /* Force millicode symbols local. */
2117 elf_link_hash_traverse (&htab->elf,
2118 clobber_millicode_symbols,
2119 info);
2122 /* Set up .got and .plt offsets for local syms, and space for local
2123 dynamic relocs. */
2124 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2126 bfd_signed_vma *local_got;
2127 bfd_signed_vma *end_local_got;
2128 bfd_signed_vma *local_plt;
2129 bfd_signed_vma *end_local_plt;
2130 bfd_size_type locsymcount;
2131 Elf_Internal_Shdr *symtab_hdr;
2132 asection *srel;
2134 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2135 continue;
2137 for (s = ibfd->sections; s != NULL; s = s->next)
2139 struct elf32_hppa_dyn_reloc_entry *p;
2141 for (p = ((struct elf32_hppa_dyn_reloc_entry *)
2142 elf_section_data (s)->local_dynrel);
2143 p != NULL;
2144 p = p->next)
2146 if (!bfd_is_abs_section (p->sec)
2147 && bfd_is_abs_section (p->sec->output_section))
2149 /* Input section has been discarded, either because
2150 it is a copy of a linkonce section or due to
2151 linker script /DISCARD/, so we'll be discarding
2152 the relocs too. */
2154 else if (p->count != 0)
2156 srel = elf_section_data (p->sec)->sreloc;
2157 srel->size += p->count * sizeof (Elf32_External_Rela);
2158 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2159 info->flags |= DF_TEXTREL;
2164 local_got = elf_local_got_refcounts (ibfd);
2165 if (!local_got)
2166 continue;
2168 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2169 locsymcount = symtab_hdr->sh_info;
2170 end_local_got = local_got + locsymcount;
2171 s = htab->sgot;
2172 srel = htab->srelgot;
2173 for (; local_got < end_local_got; ++local_got)
2175 if (*local_got > 0)
2177 *local_got = s->size;
2178 s->size += GOT_ENTRY_SIZE;
2179 if (info->shared)
2180 srel->size += sizeof (Elf32_External_Rela);
2182 else
2183 *local_got = (bfd_vma) -1;
2186 local_plt = end_local_got;
2187 end_local_plt = local_plt + locsymcount;
2188 if (! htab->elf.dynamic_sections_created)
2190 /* Won't be used, but be safe. */
2191 for (; local_plt < end_local_plt; ++local_plt)
2192 *local_plt = (bfd_vma) -1;
2194 else
2196 s = htab->splt;
2197 srel = htab->srelplt;
2198 for (; local_plt < end_local_plt; ++local_plt)
2200 if (*local_plt > 0)
2202 *local_plt = s->size;
2203 s->size += PLT_ENTRY_SIZE;
2204 if (info->shared)
2205 srel->size += sizeof (Elf32_External_Rela);
2207 else
2208 *local_plt = (bfd_vma) -1;
2213 /* Do all the .plt entries without relocs first. The dynamic linker
2214 uses the last .plt reloc to find the end of the .plt (and hence
2215 the start of the .got) for lazy linking. */
2216 elf_link_hash_traverse (&htab->elf, allocate_plt_static, info);
2218 /* Allocate global sym .plt and .got entries, and space for global
2219 sym dynamic relocs. */
2220 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2222 /* The check_relocs and adjust_dynamic_symbol entry points have
2223 determined the sizes of the various dynamic sections. Allocate
2224 memory for them. */
2225 relocs = FALSE;
2226 for (s = dynobj->sections; s != NULL; s = s->next)
2228 if ((s->flags & SEC_LINKER_CREATED) == 0)
2229 continue;
2231 if (s == htab->splt)
2233 if (htab->need_plt_stub)
2235 /* Make space for the plt stub at the end of the .plt
2236 section. We want this stub right at the end, up
2237 against the .got section. */
2238 int gotalign = bfd_section_alignment (dynobj, htab->sgot);
2239 int pltalign = bfd_section_alignment (dynobj, s);
2240 bfd_size_type mask;
2242 if (gotalign > pltalign)
2243 bfd_set_section_alignment (dynobj, s, gotalign);
2244 mask = ((bfd_size_type) 1 << gotalign) - 1;
2245 s->size = (s->size + sizeof (plt_stub) + mask) & ~mask;
2248 else if (s == htab->sgot)
2250 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2252 if (s->size != 0)
2254 /* Remember whether there are any reloc sections other
2255 than .rela.plt. */
2256 if (s != htab->srelplt)
2257 relocs = TRUE;
2259 /* We use the reloc_count field as a counter if we need
2260 to copy relocs into the output file. */
2261 s->reloc_count = 0;
2264 else
2266 /* It's not one of our sections, so don't allocate space. */
2267 continue;
2270 if (s->size == 0)
2272 /* If we don't need this section, strip it from the
2273 output file. This is mostly to handle .rela.bss and
2274 .rela.plt. We must create both sections in
2275 create_dynamic_sections, because they must be created
2276 before the linker maps input sections to output
2277 sections. The linker does that before
2278 adjust_dynamic_symbol is called, and it is that
2279 function which decides whether anything needs to go
2280 into these sections. */
2281 _bfd_strip_section_from_output (info, s);
2282 continue;
2285 /* Allocate memory for the section contents. Zero it, because
2286 we may not fill in all the reloc sections. */
2287 s->contents = bfd_zalloc (dynobj, s->size);
2288 if (s->contents == NULL && s->size != 0)
2289 return FALSE;
2292 if (htab->elf.dynamic_sections_created)
2294 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2295 actually has nothing to do with the PLT, it is how we
2296 communicate the LTP value of a load module to the dynamic
2297 linker. */
2298 #define add_dynamic_entry(TAG, VAL) \
2299 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2301 if (!add_dynamic_entry (DT_PLTGOT, 0))
2302 return FALSE;
2304 /* Add some entries to the .dynamic section. We fill in the
2305 values later, in elf32_hppa_finish_dynamic_sections, but we
2306 must add the entries now so that we get the correct size for
2307 the .dynamic section. The DT_DEBUG entry is filled in by the
2308 dynamic linker and used by the debugger. */
2309 if (!info->shared)
2311 if (!add_dynamic_entry (DT_DEBUG, 0))
2312 return FALSE;
2315 if (htab->srelplt->size != 0)
2317 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
2318 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2319 || !add_dynamic_entry (DT_JMPREL, 0))
2320 return FALSE;
2323 if (relocs)
2325 if (!add_dynamic_entry (DT_RELA, 0)
2326 || !add_dynamic_entry (DT_RELASZ, 0)
2327 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2328 return FALSE;
2330 /* If any dynamic relocs apply to a read-only section,
2331 then we need a DT_TEXTREL entry. */
2332 if ((info->flags & DF_TEXTREL) == 0)
2333 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
2335 if ((info->flags & DF_TEXTREL) != 0)
2337 if (!add_dynamic_entry (DT_TEXTREL, 0))
2338 return FALSE;
2342 #undef add_dynamic_entry
2344 return TRUE;
2347 /* External entry points for sizing and building linker stubs. */
2349 /* Set up various things so that we can make a list of input sections
2350 for each output section included in the link. Returns -1 on error,
2351 0 when no stubs will be needed, and 1 on success. */
2354 elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2356 bfd *input_bfd;
2357 unsigned int bfd_count;
2358 int top_id, top_index;
2359 asection *section;
2360 asection **input_list, **list;
2361 bfd_size_type amt;
2362 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2364 /* Count the number of input BFDs and find the top input section id. */
2365 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2366 input_bfd != NULL;
2367 input_bfd = input_bfd->link_next)
2369 bfd_count += 1;
2370 for (section = input_bfd->sections;
2371 section != NULL;
2372 section = section->next)
2374 if (top_id < section->id)
2375 top_id = section->id;
2378 htab->bfd_count = bfd_count;
2380 amt = sizeof (struct map_stub) * (top_id + 1);
2381 htab->stub_group = bfd_zmalloc (amt);
2382 if (htab->stub_group == NULL)
2383 return -1;
2385 /* We can't use output_bfd->section_count here to find the top output
2386 section index as some sections may have been removed, and
2387 _bfd_strip_section_from_output doesn't renumber the indices. */
2388 for (section = output_bfd->sections, top_index = 0;
2389 section != NULL;
2390 section = section->next)
2392 if (top_index < section->index)
2393 top_index = section->index;
2396 htab->top_index = top_index;
2397 amt = sizeof (asection *) * (top_index + 1);
2398 input_list = bfd_malloc (amt);
2399 htab->input_list = input_list;
2400 if (input_list == NULL)
2401 return -1;
2403 /* For sections we aren't interested in, mark their entries with a
2404 value we can check later. */
2405 list = input_list + top_index;
2407 *list = bfd_abs_section_ptr;
2408 while (list-- != input_list);
2410 for (section = output_bfd->sections;
2411 section != NULL;
2412 section = section->next)
2414 if ((section->flags & SEC_CODE) != 0)
2415 input_list[section->index] = NULL;
2418 return 1;
2421 /* The linker repeatedly calls this function for each input section,
2422 in the order that input sections are linked into output sections.
2423 Build lists of input sections to determine groupings between which
2424 we may insert linker stubs. */
2426 void
2427 elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
2429 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2431 if (isec->output_section->index <= htab->top_index)
2433 asection **list = htab->input_list + isec->output_section->index;
2434 if (*list != bfd_abs_section_ptr)
2436 /* Steal the link_sec pointer for our list. */
2437 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2438 /* This happens to make the list in reverse order,
2439 which is what we want. */
2440 PREV_SEC (isec) = *list;
2441 *list = isec;
2446 /* See whether we can group stub sections together. Grouping stub
2447 sections may result in fewer stubs. More importantly, we need to
2448 put all .init* and .fini* stubs at the beginning of the .init or
2449 .fini output sections respectively, because glibc splits the
2450 _init and _fini functions into multiple parts. Putting a stub in
2451 the middle of a function is not a good idea. */
2453 static void
2454 group_sections (struct elf32_hppa_link_hash_table *htab,
2455 bfd_size_type stub_group_size,
2456 bfd_boolean stubs_always_before_branch)
2458 asection **list = htab->input_list + htab->top_index;
2461 asection *tail = *list;
2462 if (tail == bfd_abs_section_ptr)
2463 continue;
2464 while (tail != NULL)
2466 asection *curr;
2467 asection *prev;
2468 bfd_size_type total;
2469 bfd_boolean big_sec;
2471 curr = tail;
2472 total = tail->size;
2473 big_sec = total >= stub_group_size;
2475 while ((prev = PREV_SEC (curr)) != NULL
2476 && ((total += curr->output_offset - prev->output_offset)
2477 < stub_group_size))
2478 curr = prev;
2480 /* OK, the size from the start of CURR to the end is less
2481 than 240000 bytes and thus can be handled by one stub
2482 section. (or the tail section is itself larger than
2483 240000 bytes, in which case we may be toast.)
2484 We should really be keeping track of the total size of
2485 stubs added here, as stubs contribute to the final output
2486 section size. That's a little tricky, and this way will
2487 only break if stubs added total more than 22144 bytes, or
2488 2768 long branch stubs. It seems unlikely for more than
2489 2768 different functions to be called, especially from
2490 code only 240000 bytes long. This limit used to be
2491 250000, but c++ code tends to generate lots of little
2492 functions, and sometimes violated the assumption. */
2495 prev = PREV_SEC (tail);
2496 /* Set up this stub group. */
2497 htab->stub_group[tail->id].link_sec = curr;
2499 while (tail != curr && (tail = prev) != NULL);
2501 /* But wait, there's more! Input sections up to 240000
2502 bytes before the stub section can be handled by it too.
2503 Don't do this if we have a really large section after the
2504 stubs, as adding more stubs increases the chance that
2505 branches may not reach into the stub section. */
2506 if (!stubs_always_before_branch && !big_sec)
2508 total = 0;
2509 while (prev != NULL
2510 && ((total += tail->output_offset - prev->output_offset)
2511 < stub_group_size))
2513 tail = prev;
2514 prev = PREV_SEC (tail);
2515 htab->stub_group[tail->id].link_sec = curr;
2518 tail = prev;
2521 while (list-- != htab->input_list);
2522 free (htab->input_list);
2523 #undef PREV_SEC
2526 /* Read in all local syms for all input bfds, and create hash entries
2527 for export stubs if we are building a multi-subspace shared lib.
2528 Returns -1 on error, 1 if export stubs created, 0 otherwise. */
2530 static int
2531 get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
2533 unsigned int bfd_indx;
2534 Elf_Internal_Sym *local_syms, **all_local_syms;
2535 int stub_changed = 0;
2536 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2538 /* We want to read in symbol extension records only once. To do this
2539 we need to read in the local symbols in parallel and save them for
2540 later use; so hold pointers to the local symbols in an array. */
2541 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2542 all_local_syms = bfd_zmalloc (amt);
2543 htab->all_local_syms = all_local_syms;
2544 if (all_local_syms == NULL)
2545 return -1;
2547 /* Walk over all the input BFDs, swapping in local symbols.
2548 If we are creating a shared library, create hash entries for the
2549 export stubs. */
2550 for (bfd_indx = 0;
2551 input_bfd != NULL;
2552 input_bfd = input_bfd->link_next, bfd_indx++)
2554 Elf_Internal_Shdr *symtab_hdr;
2556 /* We'll need the symbol table in a second. */
2557 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2558 if (symtab_hdr->sh_info == 0)
2559 continue;
2561 /* We need an array of the local symbols attached to the input bfd. */
2562 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2563 if (local_syms == NULL)
2565 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2566 symtab_hdr->sh_info, 0,
2567 NULL, NULL, NULL);
2568 /* Cache them for elf_link_input_bfd. */
2569 symtab_hdr->contents = (unsigned char *) local_syms;
2571 if (local_syms == NULL)
2572 return -1;
2574 all_local_syms[bfd_indx] = local_syms;
2576 if (info->shared && htab->multi_subspace)
2578 struct elf_link_hash_entry **sym_hashes;
2579 struct elf_link_hash_entry **end_hashes;
2580 unsigned int symcount;
2582 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2583 - symtab_hdr->sh_info);
2584 sym_hashes = elf_sym_hashes (input_bfd);
2585 end_hashes = sym_hashes + symcount;
2587 /* Look through the global syms for functions; We need to
2588 build export stubs for all globally visible functions. */
2589 for (; sym_hashes < end_hashes; sym_hashes++)
2591 struct elf32_hppa_link_hash_entry *hash;
2593 hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
2595 while (hash->elf.root.type == bfd_link_hash_indirect
2596 || hash->elf.root.type == bfd_link_hash_warning)
2597 hash = ((struct elf32_hppa_link_hash_entry *)
2598 hash->elf.root.u.i.link);
2600 /* At this point in the link, undefined syms have been
2601 resolved, so we need to check that the symbol was
2602 defined in this BFD. */
2603 if ((hash->elf.root.type == bfd_link_hash_defined
2604 || hash->elf.root.type == bfd_link_hash_defweak)
2605 && hash->elf.type == STT_FUNC
2606 && hash->elf.root.u.def.section->output_section != NULL
2607 && (hash->elf.root.u.def.section->output_section->owner
2608 == output_bfd)
2609 && hash->elf.root.u.def.section->owner == input_bfd
2610 && hash->elf.def_regular
2611 && !hash->elf.forced_local
2612 && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2614 asection *sec;
2615 const char *stub_name;
2616 struct elf32_hppa_stub_hash_entry *stub_entry;
2618 sec = hash->elf.root.u.def.section;
2619 stub_name = hash->elf.root.root.string;
2620 stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table,
2621 stub_name,
2622 FALSE, FALSE);
2623 if (stub_entry == NULL)
2625 stub_entry = hppa_add_stub (stub_name, sec, htab);
2626 if (!stub_entry)
2627 return -1;
2629 stub_entry->target_value = hash->elf.root.u.def.value;
2630 stub_entry->target_section = hash->elf.root.u.def.section;
2631 stub_entry->stub_type = hppa_stub_export;
2632 stub_entry->h = hash;
2633 stub_changed = 1;
2635 else
2637 (*_bfd_error_handler) (_("%B: duplicate export stub %s"),
2638 input_bfd,
2639 stub_name);
2646 return stub_changed;
2649 /* Determine and set the size of the stub section for a final link.
2651 The basic idea here is to examine all the relocations looking for
2652 PC-relative calls to a target that is unreachable with a "bl"
2653 instruction. */
2655 bfd_boolean
2656 elf32_hppa_size_stubs
2657 (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
2658 bfd_boolean multi_subspace, bfd_signed_vma group_size,
2659 asection * (*add_stub_section) (const char *, asection *),
2660 void (*layout_sections_again) (void))
2662 bfd_size_type stub_group_size;
2663 bfd_boolean stubs_always_before_branch;
2664 bfd_boolean stub_changed;
2665 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2667 /* Stash our params away. */
2668 htab->stub_bfd = stub_bfd;
2669 htab->multi_subspace = multi_subspace;
2670 htab->add_stub_section = add_stub_section;
2671 htab->layout_sections_again = layout_sections_again;
2672 stubs_always_before_branch = group_size < 0;
2673 if (group_size < 0)
2674 stub_group_size = -group_size;
2675 else
2676 stub_group_size = group_size;
2677 if (stub_group_size == 1)
2679 /* Default values. */
2680 if (stubs_always_before_branch)
2682 stub_group_size = 7680000;
2683 if (htab->has_17bit_branch || htab->multi_subspace)
2684 stub_group_size = 240000;
2685 if (htab->has_12bit_branch)
2686 stub_group_size = 7500;
2688 else
2690 stub_group_size = 6971392;
2691 if (htab->has_17bit_branch || htab->multi_subspace)
2692 stub_group_size = 217856;
2693 if (htab->has_12bit_branch)
2694 stub_group_size = 6808;
2698 group_sections (htab, stub_group_size, stubs_always_before_branch);
2700 switch (get_local_syms (output_bfd, info->input_bfds, info))
2702 default:
2703 if (htab->all_local_syms)
2704 goto error_ret_free_local;
2705 return FALSE;
2707 case 0:
2708 stub_changed = FALSE;
2709 break;
2711 case 1:
2712 stub_changed = TRUE;
2713 break;
2716 while (1)
2718 bfd *input_bfd;
2719 unsigned int bfd_indx;
2720 asection *stub_sec;
2722 for (input_bfd = info->input_bfds, bfd_indx = 0;
2723 input_bfd != NULL;
2724 input_bfd = input_bfd->link_next, bfd_indx++)
2726 Elf_Internal_Shdr *symtab_hdr;
2727 asection *section;
2728 Elf_Internal_Sym *local_syms;
2730 /* We'll need the symbol table in a second. */
2731 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2732 if (symtab_hdr->sh_info == 0)
2733 continue;
2735 local_syms = htab->all_local_syms[bfd_indx];
2737 /* Walk over each section attached to the input bfd. */
2738 for (section = input_bfd->sections;
2739 section != NULL;
2740 section = section->next)
2742 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2744 /* If there aren't any relocs, then there's nothing more
2745 to do. */
2746 if ((section->flags & SEC_RELOC) == 0
2747 || section->reloc_count == 0)
2748 continue;
2750 /* If this section is a link-once section that will be
2751 discarded, then don't create any stubs. */
2752 if (section->output_section == NULL
2753 || section->output_section->owner != output_bfd)
2754 continue;
2756 /* Get the relocs. */
2757 internal_relocs
2758 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2759 info->keep_memory);
2760 if (internal_relocs == NULL)
2761 goto error_ret_free_local;
2763 /* Now examine each relocation. */
2764 irela = internal_relocs;
2765 irelaend = irela + section->reloc_count;
2766 for (; irela < irelaend; irela++)
2768 unsigned int r_type, r_indx;
2769 enum elf32_hppa_stub_type stub_type;
2770 struct elf32_hppa_stub_hash_entry *stub_entry;
2771 asection *sym_sec;
2772 bfd_vma sym_value;
2773 bfd_vma destination;
2774 struct elf32_hppa_link_hash_entry *hash;
2775 char *stub_name;
2776 const asection *id_sec;
2778 r_type = ELF32_R_TYPE (irela->r_info);
2779 r_indx = ELF32_R_SYM (irela->r_info);
2781 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2783 bfd_set_error (bfd_error_bad_value);
2784 error_ret_free_internal:
2785 if (elf_section_data (section)->relocs == NULL)
2786 free (internal_relocs);
2787 goto error_ret_free_local;
2790 /* Only look for stubs on call instructions. */
2791 if (r_type != (unsigned int) R_PARISC_PCREL12F
2792 && r_type != (unsigned int) R_PARISC_PCREL17F
2793 && r_type != (unsigned int) R_PARISC_PCREL22F)
2794 continue;
2796 /* Now determine the call target, its name, value,
2797 section. */
2798 sym_sec = NULL;
2799 sym_value = 0;
2800 destination = 0;
2801 hash = NULL;
2802 if (r_indx < symtab_hdr->sh_info)
2804 /* It's a local symbol. */
2805 Elf_Internal_Sym *sym;
2806 Elf_Internal_Shdr *hdr;
2808 sym = local_syms + r_indx;
2809 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2810 sym_sec = hdr->bfd_section;
2811 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2812 sym_value = sym->st_value;
2813 destination = (sym_value + irela->r_addend
2814 + sym_sec->output_offset
2815 + sym_sec->output_section->vma);
2817 else
2819 /* It's an external symbol. */
2820 int e_indx;
2822 e_indx = r_indx - symtab_hdr->sh_info;
2823 hash = ((struct elf32_hppa_link_hash_entry *)
2824 elf_sym_hashes (input_bfd)[e_indx]);
2826 while (hash->elf.root.type == bfd_link_hash_indirect
2827 || hash->elf.root.type == bfd_link_hash_warning)
2828 hash = ((struct elf32_hppa_link_hash_entry *)
2829 hash->elf.root.u.i.link);
2831 if (hash->elf.root.type == bfd_link_hash_defined
2832 || hash->elf.root.type == bfd_link_hash_defweak)
2834 sym_sec = hash->elf.root.u.def.section;
2835 sym_value = hash->elf.root.u.def.value;
2836 if (sym_sec->output_section != NULL)
2837 destination = (sym_value + irela->r_addend
2838 + sym_sec->output_offset
2839 + sym_sec->output_section->vma);
2841 else if (hash->elf.root.type == bfd_link_hash_undefweak)
2843 if (! info->shared)
2844 continue;
2846 else if (hash->elf.root.type == bfd_link_hash_undefined)
2848 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2849 && (ELF_ST_VISIBILITY (hash->elf.other)
2850 == STV_DEFAULT)
2851 && hash->elf.type != STT_PARISC_MILLI))
2852 continue;
2854 else
2856 bfd_set_error (bfd_error_bad_value);
2857 goto error_ret_free_internal;
2861 /* Determine what (if any) linker stub is needed. */
2862 stub_type = hppa_type_of_stub (section, irela, hash,
2863 destination, info);
2864 if (stub_type == hppa_stub_none)
2865 continue;
2867 /* Support for grouping stub sections. */
2868 id_sec = htab->stub_group[section->id].link_sec;
2870 /* Get the name of this stub. */
2871 stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
2872 if (!stub_name)
2873 goto error_ret_free_internal;
2875 stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table,
2876 stub_name,
2877 FALSE, FALSE);
2878 if (stub_entry != NULL)
2880 /* The proper stub has already been created. */
2881 free (stub_name);
2882 continue;
2885 stub_entry = hppa_add_stub (stub_name, section, htab);
2886 if (stub_entry == NULL)
2888 free (stub_name);
2889 goto error_ret_free_internal;
2892 stub_entry->target_value = sym_value;
2893 stub_entry->target_section = sym_sec;
2894 stub_entry->stub_type = stub_type;
2895 if (info->shared)
2897 if (stub_type == hppa_stub_import)
2898 stub_entry->stub_type = hppa_stub_import_shared;
2899 else if (stub_type == hppa_stub_long_branch)
2900 stub_entry->stub_type = hppa_stub_long_branch_shared;
2902 stub_entry->h = hash;
2903 stub_changed = TRUE;
2906 /* We're done with the internal relocs, free them. */
2907 if (elf_section_data (section)->relocs == NULL)
2908 free (internal_relocs);
2912 if (!stub_changed)
2913 break;
2915 /* OK, we've added some stubs. Find out the new size of the
2916 stub sections. */
2917 for (stub_sec = htab->stub_bfd->sections;
2918 stub_sec != NULL;
2919 stub_sec = stub_sec->next)
2920 stub_sec->size = 0;
2922 bfd_hash_traverse (&htab->stub_hash_table, hppa_size_one_stub, htab);
2924 /* Ask the linker to do its stuff. */
2925 (*htab->layout_sections_again) ();
2926 stub_changed = FALSE;
2929 free (htab->all_local_syms);
2930 return TRUE;
2932 error_ret_free_local:
2933 free (htab->all_local_syms);
2934 return FALSE;
2937 /* For a final link, this function is called after we have sized the
2938 stubs to provide a value for __gp. */
2940 bfd_boolean
2941 elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
2943 struct bfd_link_hash_entry *h;
2944 asection *sec = NULL;
2945 bfd_vma gp_val = 0;
2946 struct elf32_hppa_link_hash_table *htab;
2948 htab = hppa_link_hash_table (info);
2949 h = bfd_link_hash_lookup (&htab->elf.root, "$global$", FALSE, FALSE, FALSE);
2951 if (h != NULL
2952 && (h->type == bfd_link_hash_defined
2953 || h->type == bfd_link_hash_defweak))
2955 gp_val = h->u.def.value;
2956 sec = h->u.def.section;
2958 else
2960 asection *splt = bfd_get_section_by_name (abfd, ".plt");
2961 asection *sgot = bfd_get_section_by_name (abfd, ".got");
2963 /* Choose to point our LTP at, in this order, one of .plt, .got,
2964 or .data, if these sections exist. In the case of choosing
2965 .plt try to make the LTP ideal for addressing anywhere in the
2966 .plt or .got with a 14 bit signed offset. Typically, the end
2967 of the .plt is the start of the .got, so choose .plt + 0x2000
2968 if either the .plt or .got is larger than 0x2000. If both
2969 the .plt and .got are smaller than 0x2000, choose the end of
2970 the .plt section. */
2971 sec = strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0
2972 ? NULL : splt;
2973 if (sec != NULL)
2975 gp_val = sec->size;
2976 if (gp_val > 0x2000 || (sgot && sgot->size > 0x2000))
2978 gp_val = 0x2000;
2981 else
2983 sec = sgot;
2984 if (sec != NULL)
2986 if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") != 0)
2988 /* We know we don't have a .plt. If .got is large,
2989 offset our LTP. */
2990 if (sec->size > 0x2000)
2991 gp_val = 0x2000;
2994 else
2996 /* No .plt or .got. Who cares what the LTP is? */
2997 sec = bfd_get_section_by_name (abfd, ".data");
3001 if (h != NULL)
3003 h->type = bfd_link_hash_defined;
3004 h->u.def.value = gp_val;
3005 if (sec != NULL)
3006 h->u.def.section = sec;
3007 else
3008 h->u.def.section = bfd_abs_section_ptr;
3012 if (sec != NULL && sec->output_section != NULL)
3013 gp_val += sec->output_section->vma + sec->output_offset;
3015 elf_gp (abfd) = gp_val;
3016 return TRUE;
3019 /* Build all the stubs associated with the current output file. The
3020 stubs are kept in a hash table attached to the main linker hash
3021 table. We also set up the .plt entries for statically linked PIC
3022 functions here. This function is called via hppaelf_finish in the
3023 linker. */
3025 bfd_boolean
3026 elf32_hppa_build_stubs (struct bfd_link_info *info)
3028 asection *stub_sec;
3029 struct bfd_hash_table *table;
3030 struct elf32_hppa_link_hash_table *htab;
3032 htab = hppa_link_hash_table (info);
3034 for (stub_sec = htab->stub_bfd->sections;
3035 stub_sec != NULL;
3036 stub_sec = stub_sec->next)
3038 bfd_size_type size;
3040 /* Allocate memory to hold the linker stubs. */
3041 size = stub_sec->size;
3042 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3043 if (stub_sec->contents == NULL && size != 0)
3044 return FALSE;
3045 stub_sec->size = 0;
3048 /* Build the stubs as directed by the stub hash table. */
3049 table = &htab->stub_hash_table;
3050 bfd_hash_traverse (table, hppa_build_one_stub, info);
3052 return TRUE;
3055 /* Perform a final link. */
3057 static bfd_boolean
3058 elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
3060 /* Invoke the regular ELF linker to do all the work. */
3061 if (!bfd_elf_final_link (abfd, info))
3062 return FALSE;
3064 /* If we're producing a final executable, sort the contents of the
3065 unwind section. */
3066 return elf_hppa_sort_unwind (abfd);
3069 /* Record the lowest address for the data and text segments. */
3071 static void
3072 hppa_record_segment_addr (bfd *abfd ATTRIBUTE_UNUSED,
3073 asection *section,
3074 void *data)
3076 struct elf32_hppa_link_hash_table *htab;
3078 htab = (struct elf32_hppa_link_hash_table *) data;
3080 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3082 bfd_vma value = section->vma - section->filepos;
3084 if ((section->flags & SEC_READONLY) != 0)
3086 if (value < htab->text_segment_base)
3087 htab->text_segment_base = value;
3089 else
3091 if (value < htab->data_segment_base)
3092 htab->data_segment_base = value;
3097 /* Perform a relocation as part of a final link. */
3099 static bfd_reloc_status_type
3100 final_link_relocate (asection *input_section,
3101 bfd_byte *contents,
3102 const Elf_Internal_Rela *rel,
3103 bfd_vma value,
3104 struct elf32_hppa_link_hash_table *htab,
3105 asection *sym_sec,
3106 struct elf32_hppa_link_hash_entry *h,
3107 struct bfd_link_info *info)
3109 int insn;
3110 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3111 unsigned int orig_r_type = r_type;
3112 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3113 int r_format = howto->bitsize;
3114 enum hppa_reloc_field_selector_type_alt r_field;
3115 bfd *input_bfd = input_section->owner;
3116 bfd_vma offset = rel->r_offset;
3117 bfd_vma max_branch_offset = 0;
3118 bfd_byte *hit_data = contents + offset;
3119 bfd_signed_vma addend = rel->r_addend;
3120 bfd_vma location;
3121 struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
3122 int val;
3124 if (r_type == R_PARISC_NONE)
3125 return bfd_reloc_ok;
3127 insn = bfd_get_32 (input_bfd, hit_data);
3129 /* Find out where we are and where we're going. */
3130 location = (offset +
3131 input_section->output_offset +
3132 input_section->output_section->vma);
3134 /* If we are not building a shared library, convert DLTIND relocs to
3135 DPREL relocs. */
3136 if (!info->shared)
3138 switch (r_type)
3140 case R_PARISC_DLTIND21L:
3141 r_type = R_PARISC_DPREL21L;
3142 break;
3144 case R_PARISC_DLTIND14R:
3145 r_type = R_PARISC_DPREL14R;
3146 break;
3148 case R_PARISC_DLTIND14F:
3149 r_type = R_PARISC_DPREL14F;
3150 break;
3154 switch (r_type)
3156 case R_PARISC_PCREL12F:
3157 case R_PARISC_PCREL17F:
3158 case R_PARISC_PCREL22F:
3159 /* If this call should go via the plt, find the import stub in
3160 the stub hash. */
3161 if (sym_sec == NULL
3162 || sym_sec->output_section == NULL
3163 || (h != NULL
3164 && h->elf.plt.offset != (bfd_vma) -1
3165 && h->elf.dynindx != -1
3166 && !h->plabel
3167 && (info->shared
3168 || !h->elf.def_regular
3169 || h->elf.root.type == bfd_link_hash_defweak)))
3171 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3172 h, rel, htab);
3173 if (stub_entry != NULL)
3175 value = (stub_entry->stub_offset
3176 + stub_entry->stub_sec->output_offset
3177 + stub_entry->stub_sec->output_section->vma);
3178 addend = 0;
3180 else if (sym_sec == NULL && h != NULL
3181 && h->elf.root.type == bfd_link_hash_undefweak)
3183 /* It's OK if undefined weak. Calls to undefined weak
3184 symbols behave as if the "called" function
3185 immediately returns. We can thus call to a weak
3186 function without first checking whether the function
3187 is defined. */
3188 value = location;
3189 addend = 8;
3191 else
3192 return bfd_reloc_undefined;
3194 /* Fall thru. */
3196 case R_PARISC_PCREL21L:
3197 case R_PARISC_PCREL17C:
3198 case R_PARISC_PCREL17R:
3199 case R_PARISC_PCREL14R:
3200 case R_PARISC_PCREL14F:
3201 case R_PARISC_PCREL32:
3202 /* Make it a pc relative offset. */
3203 value -= location;
3204 addend -= 8;
3205 break;
3207 case R_PARISC_DPREL21L:
3208 case R_PARISC_DPREL14R:
3209 case R_PARISC_DPREL14F:
3210 /* Convert instructions that use the linkage table pointer (r19) to
3211 instructions that use the global data pointer (dp). This is the
3212 most efficient way of using PIC code in an incomplete executable,
3213 but the user must follow the standard runtime conventions for
3214 accessing data for this to work. */
3215 if (orig_r_type == R_PARISC_DLTIND21L)
3217 /* Convert addil instructions if the original reloc was a
3218 DLTIND21L. GCC sometimes uses a register other than r19 for
3219 the operation, so we must convert any addil instruction
3220 that uses this relocation. */
3221 if ((insn & 0xfc000000) == ((int) OP_ADDIL << 26))
3222 insn = ADDIL_DP;
3223 else
3224 /* We must have a ldil instruction. It's too hard to find
3225 and convert the associated add instruction, so issue an
3226 error. */
3227 (*_bfd_error_handler)
3228 (_("%B(%A+0x%lx): %s fixup for insn 0x%x is not supported in a non-shared link"),
3229 input_bfd,
3230 input_section,
3231 (long) rel->r_offset,
3232 howto->name,
3233 insn);
3235 else if (orig_r_type == R_PARISC_DLTIND14F)
3237 /* This must be a format 1 load/store. Change the base
3238 register to dp. */
3239 insn = (insn & 0xfc1ffff) | (27 << 21);
3242 /* For all the DP relative relocations, we need to examine the symbol's
3243 section. If it has no section or if it's a code section, then
3244 "data pointer relative" makes no sense. In that case we don't
3245 adjust the "value", and for 21 bit addil instructions, we change the
3246 source addend register from %dp to %r0. This situation commonly
3247 arises for undefined weak symbols and when a variable's "constness"
3248 is declared differently from the way the variable is defined. For
3249 instance: "extern int foo" with foo defined as "const int foo". */
3250 if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
3252 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3253 == (((int) OP_ADDIL << 26) | (27 << 21)))
3255 insn &= ~ (0x1f << 21);
3256 #if 0 /* debug them. */
3257 (*_bfd_error_handler)
3258 (_("%B(%A+0x%lx): fixing %s"),
3259 input_bfd,
3260 input_section,
3261 (long) rel->r_offset,
3262 howto->name);
3263 #endif
3265 /* Now try to make things easy for the dynamic linker. */
3267 break;
3269 /* Fall thru. */
3271 case R_PARISC_DLTIND21L:
3272 case R_PARISC_DLTIND14R:
3273 case R_PARISC_DLTIND14F:
3274 value -= elf_gp (input_section->output_section->owner);
3275 break;
3277 case R_PARISC_SEGREL32:
3278 if ((sym_sec->flags & SEC_CODE) != 0)
3279 value -= htab->text_segment_base;
3280 else
3281 value -= htab->data_segment_base;
3282 break;
3284 default:
3285 break;
3288 switch (r_type)
3290 case R_PARISC_DIR32:
3291 case R_PARISC_DIR14F:
3292 case R_PARISC_DIR17F:
3293 case R_PARISC_PCREL17C:
3294 case R_PARISC_PCREL14F:
3295 case R_PARISC_PCREL32:
3296 case R_PARISC_DPREL14F:
3297 case R_PARISC_PLABEL32:
3298 case R_PARISC_DLTIND14F:
3299 case R_PARISC_SEGBASE:
3300 case R_PARISC_SEGREL32:
3301 r_field = e_fsel;
3302 break;
3304 case R_PARISC_DLTIND21L:
3305 case R_PARISC_PCREL21L:
3306 case R_PARISC_PLABEL21L:
3307 r_field = e_lsel;
3308 break;
3310 case R_PARISC_DIR21L:
3311 case R_PARISC_DPREL21L:
3312 r_field = e_lrsel;
3313 break;
3315 case R_PARISC_PCREL17R:
3316 case R_PARISC_PCREL14R:
3317 case R_PARISC_PLABEL14R:
3318 case R_PARISC_DLTIND14R:
3319 r_field = e_rsel;
3320 break;
3322 case R_PARISC_DIR17R:
3323 case R_PARISC_DIR14R:
3324 case R_PARISC_DPREL14R:
3325 r_field = e_rrsel;
3326 break;
3328 case R_PARISC_PCREL12F:
3329 case R_PARISC_PCREL17F:
3330 case R_PARISC_PCREL22F:
3331 r_field = e_fsel;
3333 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3335 max_branch_offset = (1 << (17-1)) << 2;
3337 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3339 max_branch_offset = (1 << (12-1)) << 2;
3341 else
3343 max_branch_offset = (1 << (22-1)) << 2;
3346 /* sym_sec is NULL on undefined weak syms or when shared on
3347 undefined syms. We've already checked for a stub for the
3348 shared undefined case. */
3349 if (sym_sec == NULL)
3350 break;
3352 /* If the branch is out of reach, then redirect the
3353 call to the local stub for this function. */
3354 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3356 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3357 h, rel, htab);
3358 if (stub_entry == NULL)
3359 return bfd_reloc_undefined;
3361 /* Munge up the value and addend so that we call the stub
3362 rather than the procedure directly. */
3363 value = (stub_entry->stub_offset
3364 + stub_entry->stub_sec->output_offset
3365 + stub_entry->stub_sec->output_section->vma
3366 - location);
3367 addend = -8;
3369 break;
3371 /* Something we don't know how to handle. */
3372 default:
3373 return bfd_reloc_notsupported;
3376 /* Make sure we can reach the stub. */
3377 if (max_branch_offset != 0
3378 && value + addend + max_branch_offset >= 2*max_branch_offset)
3380 (*_bfd_error_handler)
3381 (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3382 input_bfd,
3383 input_section,
3384 (long) rel->r_offset,
3385 stub_entry->root.string);
3386 bfd_set_error (bfd_error_bad_value);
3387 return bfd_reloc_notsupported;
3390 val = hppa_field_adjust (value, addend, r_field);
3392 switch (r_type)
3394 case R_PARISC_PCREL12F:
3395 case R_PARISC_PCREL17C:
3396 case R_PARISC_PCREL17F:
3397 case R_PARISC_PCREL17R:
3398 case R_PARISC_PCREL22F:
3399 case R_PARISC_DIR17F:
3400 case R_PARISC_DIR17R:
3401 /* This is a branch. Divide the offset by four.
3402 Note that we need to decide whether it's a branch or
3403 otherwise by inspecting the reloc. Inspecting insn won't
3404 work as insn might be from a .word directive. */
3405 val >>= 2;
3406 break;
3408 default:
3409 break;
3412 insn = hppa_rebuild_insn (insn, val, r_format);
3414 /* Update the instruction word. */
3415 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3416 return bfd_reloc_ok;
3419 /* Relocate an HPPA ELF section. */
3421 static bfd_boolean
3422 elf32_hppa_relocate_section (bfd *output_bfd,
3423 struct bfd_link_info *info,
3424 bfd *input_bfd,
3425 asection *input_section,
3426 bfd_byte *contents,
3427 Elf_Internal_Rela *relocs,
3428 Elf_Internal_Sym *local_syms,
3429 asection **local_sections)
3431 bfd_vma *local_got_offsets;
3432 struct elf32_hppa_link_hash_table *htab;
3433 Elf_Internal_Shdr *symtab_hdr;
3434 Elf_Internal_Rela *rel;
3435 Elf_Internal_Rela *relend;
3437 if (info->relocatable)
3438 return TRUE;
3440 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3442 htab = hppa_link_hash_table (info);
3443 local_got_offsets = elf_local_got_offsets (input_bfd);
3445 rel = relocs;
3446 relend = relocs + input_section->reloc_count;
3447 for (; rel < relend; rel++)
3449 unsigned int r_type;
3450 reloc_howto_type *howto;
3451 unsigned int r_symndx;
3452 struct elf32_hppa_link_hash_entry *h;
3453 Elf_Internal_Sym *sym;
3454 asection *sym_sec;
3455 bfd_vma relocation;
3456 bfd_reloc_status_type r;
3457 const char *sym_name;
3458 bfd_boolean plabel;
3459 bfd_boolean warned_undef;
3461 r_type = ELF32_R_TYPE (rel->r_info);
3462 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3464 bfd_set_error (bfd_error_bad_value);
3465 return FALSE;
3467 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3468 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3469 continue;
3471 /* This is a final link. */
3472 r_symndx = ELF32_R_SYM (rel->r_info);
3473 h = NULL;
3474 sym = NULL;
3475 sym_sec = NULL;
3476 warned_undef = FALSE;
3477 if (r_symndx < symtab_hdr->sh_info)
3479 /* This is a local symbol, h defaults to NULL. */
3480 sym = local_syms + r_symndx;
3481 sym_sec = local_sections[r_symndx];
3482 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
3484 else
3486 struct elf_link_hash_entry *hh;
3487 bfd_boolean unresolved_reloc;
3488 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3490 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3491 r_symndx, symtab_hdr, sym_hashes,
3492 hh, sym_sec, relocation,
3493 unresolved_reloc, warned_undef);
3495 if (relocation == 0
3496 && hh->root.type != bfd_link_hash_defined
3497 && hh->root.type != bfd_link_hash_defweak
3498 && hh->root.type != bfd_link_hash_undefweak)
3500 if (info->unresolved_syms_in_objects == RM_IGNORE
3501 && ELF_ST_VISIBILITY (hh->other) == STV_DEFAULT
3502 && hh->type == STT_PARISC_MILLI)
3504 if (! info->callbacks->undefined_symbol
3505 (info, hh->root.root.string, input_bfd,
3506 input_section, rel->r_offset, FALSE))
3507 return FALSE;
3508 warned_undef = TRUE;
3511 h = (struct elf32_hppa_link_hash_entry *) hh;
3514 /* Do any required modifications to the relocation value, and
3515 determine what types of dynamic info we need to output, if
3516 any. */
3517 plabel = 0;
3518 switch (r_type)
3520 case R_PARISC_DLTIND14F:
3521 case R_PARISC_DLTIND14R:
3522 case R_PARISC_DLTIND21L:
3524 bfd_vma off;
3525 bfd_boolean do_got = 0;
3527 /* Relocation is to the entry for this symbol in the
3528 global offset table. */
3529 if (h != NULL)
3531 bfd_boolean dyn;
3533 off = h->elf.got.offset;
3534 dyn = htab->elf.dynamic_sections_created;
3535 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
3536 &h->elf))
3538 /* If we aren't going to call finish_dynamic_symbol,
3539 then we need to handle initialisation of the .got
3540 entry and create needed relocs here. Since the
3541 offset must always be a multiple of 4, we use the
3542 least significant bit to record whether we have
3543 initialised it already. */
3544 if ((off & 1) != 0)
3545 off &= ~1;
3546 else
3548 h->elf.got.offset |= 1;
3549 do_got = 1;
3553 else
3555 /* Local symbol case. */
3556 if (local_got_offsets == NULL)
3557 abort ();
3559 off = local_got_offsets[r_symndx];
3561 /* The offset must always be a multiple of 4. We use
3562 the least significant bit to record whether we have
3563 already generated the necessary reloc. */
3564 if ((off & 1) != 0)
3565 off &= ~1;
3566 else
3568 local_got_offsets[r_symndx] |= 1;
3569 do_got = 1;
3573 if (do_got)
3575 if (info->shared)
3577 /* Output a dynamic relocation for this GOT entry.
3578 In this case it is relative to the base of the
3579 object because the symbol index is zero. */
3580 Elf_Internal_Rela outrel;
3581 bfd_byte *loc;
3582 asection *s = htab->srelgot;
3584 outrel.r_offset = (off
3585 + htab->sgot->output_offset
3586 + htab->sgot->output_section->vma);
3587 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3588 outrel.r_addend = relocation;
3589 loc = s->contents;
3590 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3591 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3593 else
3594 bfd_put_32 (output_bfd, relocation,
3595 htab->sgot->contents + off);
3598 if (off >= (bfd_vma) -2)
3599 abort ();
3601 /* Add the base of the GOT to the relocation value. */
3602 relocation = (off
3603 + htab->sgot->output_offset
3604 + htab->sgot->output_section->vma);
3606 break;
3608 case R_PARISC_SEGREL32:
3609 /* If this is the first SEGREL relocation, then initialize
3610 the segment base values. */
3611 if (htab->text_segment_base == (bfd_vma) -1)
3612 bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
3613 break;
3615 case R_PARISC_PLABEL14R:
3616 case R_PARISC_PLABEL21L:
3617 case R_PARISC_PLABEL32:
3618 if (htab->elf.dynamic_sections_created)
3620 bfd_vma off;
3621 bfd_boolean do_plt = 0;
3623 /* If we have a global symbol with a PLT slot, then
3624 redirect this relocation to it. */
3625 if (h != NULL)
3627 off = h->elf.plt.offset;
3628 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared,
3629 &h->elf))
3631 /* In a non-shared link, adjust_dynamic_symbols
3632 isn't called for symbols forced local. We
3633 need to write out the plt entry here. */
3634 if ((off & 1) != 0)
3635 off &= ~1;
3636 else
3638 h->elf.plt.offset |= 1;
3639 do_plt = 1;
3643 else
3645 bfd_vma *local_plt_offsets;
3647 if (local_got_offsets == NULL)
3648 abort ();
3650 local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
3651 off = local_plt_offsets[r_symndx];
3653 /* As for the local .got entry case, we use the last
3654 bit to record whether we've already initialised
3655 this local .plt entry. */
3656 if ((off & 1) != 0)
3657 off &= ~1;
3658 else
3660 local_plt_offsets[r_symndx] |= 1;
3661 do_plt = 1;
3665 if (do_plt)
3667 if (info->shared)
3669 /* Output a dynamic IPLT relocation for this
3670 PLT entry. */
3671 Elf_Internal_Rela outrel;
3672 bfd_byte *loc;
3673 asection *s = htab->srelplt;
3675 outrel.r_offset = (off
3676 + htab->splt->output_offset
3677 + htab->splt->output_section->vma);
3678 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3679 outrel.r_addend = relocation;
3680 loc = s->contents;
3681 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3682 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3684 else
3686 bfd_put_32 (output_bfd,
3687 relocation,
3688 htab->splt->contents + off);
3689 bfd_put_32 (output_bfd,
3690 elf_gp (htab->splt->output_section->owner),
3691 htab->splt->contents + off + 4);
3695 if (off >= (bfd_vma) -2)
3696 abort ();
3698 /* PLABELs contain function pointers. Relocation is to
3699 the entry for the function in the .plt. The magic +2
3700 offset signals to $$dyncall that the function pointer
3701 is in the .plt and thus has a gp pointer too.
3702 Exception: Undefined PLABELs should have a value of
3703 zero. */
3704 if (h == NULL
3705 || (h->elf.root.type != bfd_link_hash_undefweak
3706 && h->elf.root.type != bfd_link_hash_undefined))
3708 relocation = (off
3709 + htab->splt->output_offset
3710 + htab->splt->output_section->vma
3711 + 2);
3713 plabel = 1;
3715 /* Fall through and possibly emit a dynamic relocation. */
3717 case R_PARISC_DIR17F:
3718 case R_PARISC_DIR17R:
3719 case R_PARISC_DIR14F:
3720 case R_PARISC_DIR14R:
3721 case R_PARISC_DIR21L:
3722 case R_PARISC_DPREL14F:
3723 case R_PARISC_DPREL14R:
3724 case R_PARISC_DPREL21L:
3725 case R_PARISC_DIR32:
3726 /* r_symndx will be zero only for relocs against symbols
3727 from removed linkonce sections, or sections discarded by
3728 a linker script. */
3729 if (r_symndx == 0
3730 || (input_section->flags & SEC_ALLOC) == 0)
3731 break;
3733 /* The reloc types handled here and this conditional
3734 expression must match the code in ..check_relocs and
3735 allocate_dynrelocs. ie. We need exactly the same condition
3736 as in ..check_relocs, with some extra conditions (dynindx
3737 test in this case) to cater for relocs removed by
3738 allocate_dynrelocs. If you squint, the non-shared test
3739 here does indeed match the one in ..check_relocs, the
3740 difference being that here we test DEF_DYNAMIC as well as
3741 !DEF_REGULAR. All common syms end up with !DEF_REGULAR,
3742 which is why we can't use just that test here.
3743 Conversely, DEF_DYNAMIC can't be used in check_relocs as
3744 there all files have not been loaded. */
3745 if ((info->shared
3746 && (h == NULL
3747 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
3748 || h->elf.root.type != bfd_link_hash_undefweak)
3749 && (IS_ABSOLUTE_RELOC (r_type)
3750 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
3751 || (!info->shared
3752 && h != NULL
3753 && h->elf.dynindx != -1
3754 && !h->elf.non_got_ref
3755 && ((ELIMINATE_COPY_RELOCS
3756 && h->elf.def_dynamic
3757 && !h->elf.def_regular)
3758 || h->elf.root.type == bfd_link_hash_undefweak
3759 || h->elf.root.type == bfd_link_hash_undefined)))
3761 Elf_Internal_Rela outrel;
3762 bfd_boolean skip;
3763 asection *sreloc;
3764 bfd_byte *loc;
3766 /* When generating a shared object, these relocations
3767 are copied into the output file to be resolved at run
3768 time. */
3770 outrel.r_addend = rel->r_addend;
3771 outrel.r_offset =
3772 _bfd_elf_section_offset (output_bfd, info, input_section,
3773 rel->r_offset);
3774 skip = (outrel.r_offset == (bfd_vma) -1
3775 || outrel.r_offset == (bfd_vma) -2);
3776 outrel.r_offset += (input_section->output_offset
3777 + input_section->output_section->vma);
3779 if (skip)
3781 memset (&outrel, 0, sizeof (outrel));
3783 else if (h != NULL
3784 && h->elf.dynindx != -1
3785 && (plabel
3786 || !IS_ABSOLUTE_RELOC (r_type)
3787 || !info->shared
3788 || !info->symbolic
3789 || !h->elf.def_regular))
3791 outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3793 else /* It's a local symbol, or one marked to become local. */
3795 int indx = 0;
3797 /* Add the absolute offset of the symbol. */
3798 outrel.r_addend += relocation;
3800 /* Global plabels need to be processed by the
3801 dynamic linker so that functions have at most one
3802 fptr. For this reason, we need to differentiate
3803 between global and local plabels, which we do by
3804 providing the function symbol for a global plabel
3805 reloc, and no symbol for local plabels. */
3806 if (! plabel
3807 && sym_sec != NULL
3808 && sym_sec->output_section != NULL
3809 && ! bfd_is_abs_section (sym_sec))
3811 /* Skip this relocation if the output section has
3812 been discarded. */
3813 if (bfd_is_abs_section (sym_sec->output_section))
3814 break;
3816 indx = elf_section_data (sym_sec->output_section)->dynindx;
3817 /* We are turning this relocation into one
3818 against a section symbol, so subtract out the
3819 output section's address but not the offset
3820 of the input section in the output section. */
3821 outrel.r_addend -= sym_sec->output_section->vma;
3824 outrel.r_info = ELF32_R_INFO (indx, r_type);
3826 #if 0
3827 /* EH info can cause unaligned DIR32 relocs.
3828 Tweak the reloc type for the dynamic linker. */
3829 if (r_type == R_PARISC_DIR32 && (outrel.r_offset & 3) != 0)
3830 outrel.r_info = ELF32_R_INFO (ELF32_R_SYM (outrel.r_info),
3831 R_PARISC_DIR32U);
3832 #endif
3833 sreloc = elf_section_data (input_section)->sreloc;
3834 if (sreloc == NULL)
3835 abort ();
3837 loc = sreloc->contents;
3838 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3839 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3841 break;
3843 default:
3844 break;
3847 r = final_link_relocate (input_section, contents, rel, relocation,
3848 htab, sym_sec, h, info);
3850 if (r == bfd_reloc_ok)
3851 continue;
3853 if (h != NULL)
3854 sym_name = h->elf.root.root.string;
3855 else
3857 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3858 symtab_hdr->sh_link,
3859 sym->st_name);
3860 if (sym_name == NULL)
3861 return FALSE;
3862 if (*sym_name == '\0')
3863 sym_name = bfd_section_name (input_bfd, sym_sec);
3866 howto = elf_hppa_howto_table + r_type;
3868 if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
3870 if (r == bfd_reloc_notsupported || !warned_undef)
3872 (*_bfd_error_handler)
3873 (_("%B(%A+0x%lx): cannot handle %s for %s"),
3874 input_bfd,
3875 input_section,
3876 (long) rel->r_offset,
3877 howto->name,
3878 sym_name);
3879 bfd_set_error (bfd_error_bad_value);
3880 return FALSE;
3883 else
3885 if (!((*info->callbacks->reloc_overflow)
3886 (info, (h ? &h->elf.root : NULL), sym_name, howto->name,
3887 (bfd_vma) 0, input_bfd, input_section, rel->r_offset)))
3888 return FALSE;
3892 return TRUE;
3895 /* Finish up dynamic symbol handling. We set the contents of various
3896 dynamic sections here. */
3898 static bfd_boolean
3899 elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
3900 struct bfd_link_info *info,
3901 struct elf_link_hash_entry *h,
3902 Elf_Internal_Sym *sym)
3904 struct elf32_hppa_link_hash_table *htab;
3905 Elf_Internal_Rela rel;
3906 bfd_byte *loc;
3908 htab = hppa_link_hash_table (info);
3910 if (h->plt.offset != (bfd_vma) -1)
3912 bfd_vma value;
3914 if (h->plt.offset & 1)
3915 abort ();
3917 /* This symbol has an entry in the procedure linkage table. Set
3918 it up.
3920 The format of a plt entry is
3921 <funcaddr>
3922 <__gp>
3924 value = 0;
3925 if (h->root.type == bfd_link_hash_defined
3926 || h->root.type == bfd_link_hash_defweak)
3928 value = h->root.u.def.value;
3929 if (h->root.u.def.section->output_section != NULL)
3930 value += (h->root.u.def.section->output_offset
3931 + h->root.u.def.section->output_section->vma);
3934 /* Create a dynamic IPLT relocation for this entry. */
3935 rel.r_offset = (h->plt.offset
3936 + htab->splt->output_offset
3937 + htab->splt->output_section->vma);
3938 if (h->dynindx != -1)
3940 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
3941 rel.r_addend = 0;
3943 else
3945 /* This symbol has been marked to become local, and is
3946 used by a plabel so must be kept in the .plt. */
3947 rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3948 rel.r_addend = value;
3951 loc = htab->srelplt->contents;
3952 loc += htab->srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
3953 bfd_elf32_swap_reloca_out (htab->splt->output_section->owner, &rel, loc);
3955 if (!h->def_regular)
3957 /* Mark the symbol as undefined, rather than as defined in
3958 the .plt section. Leave the value alone. */
3959 sym->st_shndx = SHN_UNDEF;
3963 if (h->got.offset != (bfd_vma) -1)
3965 /* This symbol has an entry in the global offset table. Set it
3966 up. */
3968 rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
3969 + htab->sgot->output_offset
3970 + htab->sgot->output_section->vma);
3972 /* If this is a -Bsymbolic link and the symbol is defined
3973 locally or was forced to be local because of a version file,
3974 we just want to emit a RELATIVE reloc. The entry in the
3975 global offset table will already have been initialized in the
3976 relocate_section function. */
3977 if (info->shared
3978 && (info->symbolic || h->dynindx == -1)
3979 && h->def_regular)
3981 rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3982 rel.r_addend = (h->root.u.def.value
3983 + h->root.u.def.section->output_offset
3984 + h->root.u.def.section->output_section->vma);
3986 else
3988 if ((h->got.offset & 1) != 0)
3989 abort ();
3990 bfd_put_32 (output_bfd, 0, htab->sgot->contents + h->got.offset);
3991 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
3992 rel.r_addend = 0;
3995 loc = htab->srelgot->contents;
3996 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3997 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
4000 if (h->needs_copy)
4002 asection *s;
4004 /* This symbol needs a copy reloc. Set it up. */
4006 if (! (h->dynindx != -1
4007 && (h->root.type == bfd_link_hash_defined
4008 || h->root.type == bfd_link_hash_defweak)))
4009 abort ();
4011 s = htab->srelbss;
4013 rel.r_offset = (h->root.u.def.value
4014 + h->root.u.def.section->output_offset
4015 + h->root.u.def.section->output_section->vma);
4016 rel.r_addend = 0;
4017 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
4018 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4019 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
4022 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4023 if (h->root.root.string[0] == '_'
4024 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4025 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
4027 sym->st_shndx = SHN_ABS;
4030 return TRUE;
4033 /* Used to decide how to sort relocs in an optimal manner for the
4034 dynamic linker, before writing them out. */
4036 static enum elf_reloc_type_class
4037 elf32_hppa_reloc_type_class (const Elf_Internal_Rela *rela)
4039 if (ELF32_R_SYM (rela->r_info) == 0)
4040 return reloc_class_relative;
4042 switch ((int) ELF32_R_TYPE (rela->r_info))
4044 case R_PARISC_IPLT:
4045 return reloc_class_plt;
4046 case R_PARISC_COPY:
4047 return reloc_class_copy;
4048 default:
4049 return reloc_class_normal;
4053 /* Finish up the dynamic sections. */
4055 static bfd_boolean
4056 elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
4057 struct bfd_link_info *info)
4059 bfd *dynobj;
4060 struct elf32_hppa_link_hash_table *htab;
4061 asection *sdyn;
4063 htab = hppa_link_hash_table (info);
4064 dynobj = htab->elf.dynobj;
4066 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4068 if (htab->elf.dynamic_sections_created)
4070 Elf32_External_Dyn *dyncon, *dynconend;
4072 if (sdyn == NULL)
4073 abort ();
4075 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4076 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4077 for (; dyncon < dynconend; dyncon++)
4079 Elf_Internal_Dyn dyn;
4080 asection *s;
4082 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4084 switch (dyn.d_tag)
4086 default:
4087 continue;
4089 case DT_PLTGOT:
4090 /* Use PLTGOT to set the GOT register. */
4091 dyn.d_un.d_ptr = elf_gp (output_bfd);
4092 break;
4094 case DT_JMPREL:
4095 s = htab->srelplt;
4096 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4097 break;
4099 case DT_PLTRELSZ:
4100 s = htab->srelplt;
4101 dyn.d_un.d_val = s->size;
4102 break;
4104 case DT_RELASZ:
4105 /* Don't count procedure linkage table relocs in the
4106 overall reloc count. */
4107 s = htab->srelplt;
4108 if (s == NULL)
4109 continue;
4110 dyn.d_un.d_val -= s->size;
4111 break;
4113 case DT_RELA:
4114 /* We may not be using the standard ELF linker script.
4115 If .rela.plt is the first .rela section, we adjust
4116 DT_RELA to not include it. */
4117 s = htab->srelplt;
4118 if (s == NULL)
4119 continue;
4120 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4121 continue;
4122 dyn.d_un.d_ptr += s->size;
4123 break;
4126 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4130 if (htab->sgot != NULL && htab->sgot->size != 0)
4132 /* Fill in the first entry in the global offset table.
4133 We use it to point to our dynamic section, if we have one. */
4134 bfd_put_32 (output_bfd,
4135 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
4136 htab->sgot->contents);
4138 /* The second entry is reserved for use by the dynamic linker. */
4139 memset (htab->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4141 /* Set .got entry size. */
4142 elf_section_data (htab->sgot->output_section)
4143 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4146 if (htab->splt != NULL && htab->splt->size != 0)
4148 /* Set plt entry size. */
4149 elf_section_data (htab->splt->output_section)
4150 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4152 if (htab->need_plt_stub)
4154 /* Set up the .plt stub. */
4155 memcpy (htab->splt->contents
4156 + htab->splt->size - sizeof (plt_stub),
4157 plt_stub, sizeof (plt_stub));
4159 if ((htab->splt->output_offset
4160 + htab->splt->output_section->vma
4161 + htab->splt->size)
4162 != (htab->sgot->output_offset
4163 + htab->sgot->output_section->vma))
4165 (*_bfd_error_handler)
4166 (_(".got section not immediately after .plt section"));
4167 return FALSE;
4172 return TRUE;
4175 /* Tweak the OSABI field of the elf header. */
4177 static void
4178 elf32_hppa_post_process_headers (bfd *abfd,
4179 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4181 Elf_Internal_Ehdr * i_ehdrp;
4183 i_ehdrp = elf_elfheader (abfd);
4185 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
4187 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
4189 else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
4191 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NETBSD;
4193 else
4195 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4199 /* Called when writing out an object file to decide the type of a
4200 symbol. */
4201 static int
4202 elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
4204 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4205 return STT_PARISC_MILLI;
4206 else
4207 return type;
4210 /* Misc BFD support code. */
4211 #define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4212 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4213 #define elf_info_to_howto elf_hppa_info_to_howto
4214 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
4216 /* Stuff for the BFD linker. */
4217 #define bfd_elf32_bfd_final_link elf32_hppa_final_link
4218 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4219 #define bfd_elf32_bfd_link_hash_table_free elf32_hppa_link_hash_table_free
4220 #define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
4221 #define elf_backend_copy_indirect_symbol elf32_hppa_copy_indirect_symbol
4222 #define elf_backend_check_relocs elf32_hppa_check_relocs
4223 #define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4224 #define elf_backend_fake_sections elf_hppa_fake_sections
4225 #define elf_backend_relocate_section elf32_hppa_relocate_section
4226 #define elf_backend_hide_symbol elf32_hppa_hide_symbol
4227 #define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4228 #define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4229 #define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4230 #define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4231 #define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
4232 #define elf_backend_grok_prstatus elf32_hppa_grok_prstatus
4233 #define elf_backend_grok_psinfo elf32_hppa_grok_psinfo
4234 #define elf_backend_object_p elf32_hppa_object_p
4235 #define elf_backend_final_write_processing elf_hppa_final_write_processing
4236 #define elf_backend_post_process_headers elf32_hppa_post_process_headers
4237 #define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
4238 #define elf_backend_reloc_type_class elf32_hppa_reloc_type_class
4240 #define elf_backend_can_gc_sections 1
4241 #define elf_backend_can_refcount 1
4242 #define elf_backend_plt_alignment 2
4243 #define elf_backend_want_got_plt 0
4244 #define elf_backend_plt_readonly 0
4245 #define elf_backend_want_plt_sym 0
4246 #define elf_backend_got_header_size 8
4247 #define elf_backend_rela_normal 1
4249 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
4250 #define TARGET_BIG_NAME "elf32-hppa"
4251 #define ELF_ARCH bfd_arch_hppa
4252 #define ELF_MACHINE_CODE EM_PARISC
4253 #define ELF_MAXPAGESIZE 0x1000
4255 #include "elf32-target.h"
4257 #undef TARGET_BIG_SYM
4258 #define TARGET_BIG_SYM bfd_elf32_hppa_linux_vec
4259 #undef TARGET_BIG_NAME
4260 #define TARGET_BIG_NAME "elf32-hppa-linux"
4262 #define INCLUDED_TARGET_FILE 1
4263 #include "elf32-target.h"
4265 #undef TARGET_BIG_SYM
4266 #define TARGET_BIG_SYM bfd_elf32_hppa_nbsd_vec
4267 #undef TARGET_BIG_NAME
4268 #define TARGET_BIG_NAME "elf32-hppa-netbsd"
4270 #include "elf32-target.h"