2003-09-30 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / bfd / elf32-hppa.c
blobbe0727f46bbf49e2ece950ceb2ea097b39831eae
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 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 enum elf32_hppa_stub_type {
145 hppa_stub_long_branch,
146 hppa_stub_long_branch_shared,
147 hppa_stub_import,
148 hppa_stub_import_shared,
149 hppa_stub_export,
150 hppa_stub_none
153 struct elf32_hppa_stub_hash_entry {
155 /* Base hash table entry structure. */
156 struct bfd_hash_entry root;
158 /* The stub section. */
159 asection *stub_sec;
161 /* Offset within stub_sec of the beginning of this stub. */
162 bfd_vma stub_offset;
164 /* Given the symbol's value and its section we can determine its final
165 value when building the stubs (so the stub knows where to jump. */
166 bfd_vma target_value;
167 asection *target_section;
169 enum elf32_hppa_stub_type stub_type;
171 /* The symbol table entry, if any, that this was derived from. */
172 struct elf32_hppa_link_hash_entry *h;
174 /* Where this stub is being called from, or, in the case of combined
175 stub sections, the first input section in the group. */
176 asection *id_sec;
179 struct elf32_hppa_link_hash_entry {
181 struct elf_link_hash_entry elf;
183 /* A pointer to the most recently used stub hash entry against this
184 symbol. */
185 struct elf32_hppa_stub_hash_entry *stub_cache;
187 /* Used to count relocations for delayed sizing of relocation
188 sections. */
189 struct elf32_hppa_dyn_reloc_entry {
191 /* Next relocation in the chain. */
192 struct elf32_hppa_dyn_reloc_entry *next;
194 /* The input section of the reloc. */
195 asection *sec;
197 /* Number of relocs copied in this section. */
198 bfd_size_type count;
200 #if RELATIVE_DYNRELOCS
201 /* Number of relative relocs copied for the input section. */
202 bfd_size_type relative_count;
203 #endif
204 } *dyn_relocs;
206 /* Set if this symbol is used by a plabel reloc. */
207 unsigned int plabel:1;
210 struct elf32_hppa_link_hash_table {
212 /* The main hash table. */
213 struct elf_link_hash_table elf;
215 /* The stub hash table. */
216 struct bfd_hash_table stub_hash_table;
218 /* Linker stub bfd. */
219 bfd *stub_bfd;
221 /* Linker call-backs. */
222 asection * (*add_stub_section) (const char *, asection *);
223 void (*layout_sections_again) (void);
225 /* Array to keep track of which stub sections have been created, and
226 information on stub grouping. */
227 struct map_stub {
228 /* This is the section to which stubs in the group will be
229 attached. */
230 asection *link_sec;
231 /* The stub section. */
232 asection *stub_sec;
233 } *stub_group;
235 /* Assorted information used by elf32_hppa_size_stubs. */
236 unsigned int bfd_count;
237 int top_index;
238 asection **input_list;
239 Elf_Internal_Sym **all_local_syms;
241 /* Short-cuts to get to dynamic linker sections. */
242 asection *sgot;
243 asection *srelgot;
244 asection *splt;
245 asection *srelplt;
246 asection *sdynbss;
247 asection *srelbss;
249 /* Used during a final link to store the base of the text and data
250 segments so that we can perform SEGREL relocations. */
251 bfd_vma text_segment_base;
252 bfd_vma data_segment_base;
254 /* Whether we support multiple sub-spaces for shared libs. */
255 unsigned int multi_subspace:1;
257 /* Flags set when various size branches are detected. Used to
258 select suitable defaults for the stub group size. */
259 unsigned int has_12bit_branch:1;
260 unsigned int has_17bit_branch:1;
261 unsigned int has_22bit_branch:1;
263 /* Set if we need a .plt stub to support lazy dynamic linking. */
264 unsigned int need_plt_stub:1;
266 /* Small local sym to section mapping cache. */
267 struct sym_sec_cache sym_sec;
270 /* Various hash macros and functions. */
271 #define hppa_link_hash_table(p) \
272 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
274 #define hppa_stub_hash_lookup(table, string, create, copy) \
275 ((struct elf32_hppa_stub_hash_entry *) \
276 bfd_hash_lookup ((table), (string), (create), (copy)))
278 /* Assorted hash table functions. */
280 /* Initialize an entry in the stub hash table. */
282 static struct bfd_hash_entry *
283 stub_hash_newfunc (struct bfd_hash_entry *entry,
284 struct bfd_hash_table *table,
285 const char *string)
287 /* Allocate the structure if it has not already been allocated by a
288 subclass. */
289 if (entry == NULL)
291 entry = bfd_hash_allocate (table,
292 sizeof (struct elf32_hppa_stub_hash_entry));
293 if (entry == NULL)
294 return entry;
297 /* Call the allocation method of the superclass. */
298 entry = bfd_hash_newfunc (entry, table, string);
299 if (entry != NULL)
301 struct elf32_hppa_stub_hash_entry *eh;
303 /* Initialize the local fields. */
304 eh = (struct elf32_hppa_stub_hash_entry *) entry;
305 eh->stub_sec = NULL;
306 eh->stub_offset = 0;
307 eh->target_value = 0;
308 eh->target_section = NULL;
309 eh->stub_type = hppa_stub_long_branch;
310 eh->h = NULL;
311 eh->id_sec = NULL;
314 return entry;
317 /* Initialize an entry in the link hash table. */
319 static struct bfd_hash_entry *
320 hppa_link_hash_newfunc (struct bfd_hash_entry *entry,
321 struct bfd_hash_table *table,
322 const char *string)
324 /* Allocate the structure if it has not already been allocated by a
325 subclass. */
326 if (entry == NULL)
328 entry = bfd_hash_allocate (table,
329 sizeof (struct elf32_hppa_link_hash_entry));
330 if (entry == NULL)
331 return entry;
334 /* Call the allocation method of the superclass. */
335 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
336 if (entry != NULL)
338 struct elf32_hppa_link_hash_entry *eh;
340 /* Initialize the local fields. */
341 eh = (struct elf32_hppa_link_hash_entry *) entry;
342 eh->stub_cache = NULL;
343 eh->dyn_relocs = NULL;
344 eh->plabel = 0;
347 return entry;
350 /* Create the derived linker hash table. The PA ELF port uses the derived
351 hash table to keep information specific to the PA ELF linker (without
352 using static variables). */
354 static struct bfd_link_hash_table *
355 elf32_hppa_link_hash_table_create (bfd *abfd)
357 struct elf32_hppa_link_hash_table *ret;
358 bfd_size_type amt = sizeof (*ret);
360 ret = bfd_malloc (amt);
361 if (ret == NULL)
362 return NULL;
364 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, hppa_link_hash_newfunc))
366 free (ret);
367 return NULL;
370 /* Init the stub hash table too. */
371 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc))
372 return NULL;
374 ret->stub_bfd = NULL;
375 ret->add_stub_section = NULL;
376 ret->layout_sections_again = NULL;
377 ret->stub_group = NULL;
378 ret->sgot = NULL;
379 ret->srelgot = NULL;
380 ret->splt = NULL;
381 ret->srelplt = NULL;
382 ret->sdynbss = NULL;
383 ret->srelbss = NULL;
384 ret->text_segment_base = (bfd_vma) -1;
385 ret->data_segment_base = (bfd_vma) -1;
386 ret->multi_subspace = 0;
387 ret->has_12bit_branch = 0;
388 ret->has_17bit_branch = 0;
389 ret->has_22bit_branch = 0;
390 ret->need_plt_stub = 0;
391 ret->sym_sec.abfd = NULL;
393 return &ret->elf.root;
396 /* Free the derived linker hash table. */
398 static void
399 elf32_hppa_link_hash_table_free (struct bfd_link_hash_table *hash)
401 struct elf32_hppa_link_hash_table *ret
402 = (struct elf32_hppa_link_hash_table *) hash;
404 bfd_hash_table_free (&ret->stub_hash_table);
405 _bfd_generic_link_hash_table_free (hash);
408 /* Build a name for an entry in the stub hash table. */
410 static char *
411 hppa_stub_name (const asection *input_section,
412 const asection *sym_sec,
413 const struct elf32_hppa_link_hash_entry *hash,
414 const Elf_Internal_Rela *rel)
416 char *stub_name;
417 bfd_size_type len;
419 if (hash)
421 len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
422 stub_name = bfd_malloc (len);
423 if (stub_name != NULL)
425 sprintf (stub_name, "%08x_%s+%x",
426 input_section->id & 0xffffffff,
427 hash->elf.root.root.string,
428 (int) rel->r_addend & 0xffffffff);
431 else
433 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
434 stub_name = bfd_malloc (len);
435 if (stub_name != NULL)
437 sprintf (stub_name, "%08x_%x:%x+%x",
438 input_section->id & 0xffffffff,
439 sym_sec->id & 0xffffffff,
440 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
441 (int) rel->r_addend & 0xffffffff);
444 return stub_name;
447 /* Look up an entry in the stub hash. Stub entries are cached because
448 creating the stub name takes a bit of time. */
450 static struct elf32_hppa_stub_hash_entry *
451 hppa_get_stub_entry (const asection *input_section,
452 const asection *sym_sec,
453 struct elf32_hppa_link_hash_entry *hash,
454 const Elf_Internal_Rela *rel,
455 struct elf32_hppa_link_hash_table *htab)
457 struct elf32_hppa_stub_hash_entry *stub_entry;
458 const asection *id_sec;
460 /* If this input section is part of a group of sections sharing one
461 stub section, then use the id of the first section in the group.
462 Stub names need to include a section id, as there may well be
463 more than one stub used to reach say, printf, and we need to
464 distinguish between them. */
465 id_sec = htab->stub_group[input_section->id].link_sec;
467 if (hash != NULL && hash->stub_cache != NULL
468 && hash->stub_cache->h == hash
469 && hash->stub_cache->id_sec == id_sec)
471 stub_entry = hash->stub_cache;
473 else
475 char *stub_name;
477 stub_name = hppa_stub_name (id_sec, sym_sec, hash, rel);
478 if (stub_name == NULL)
479 return NULL;
481 stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table,
482 stub_name, FALSE, FALSE);
483 if (hash != NULL)
484 hash->stub_cache = stub_entry;
486 free (stub_name);
489 return stub_entry;
492 /* Add a new stub entry to the stub hash. Not all fields of the new
493 stub entry are initialised. */
495 static struct elf32_hppa_stub_hash_entry *
496 hppa_add_stub (const char *stub_name,
497 asection *section,
498 struct elf32_hppa_link_hash_table *htab)
500 asection *link_sec;
501 asection *stub_sec;
502 struct elf32_hppa_stub_hash_entry *stub_entry;
504 link_sec = htab->stub_group[section->id].link_sec;
505 stub_sec = htab->stub_group[section->id].stub_sec;
506 if (stub_sec == NULL)
508 stub_sec = htab->stub_group[link_sec->id].stub_sec;
509 if (stub_sec == NULL)
511 size_t namelen;
512 bfd_size_type len;
513 char *s_name;
515 namelen = strlen (link_sec->name);
516 len = namelen + sizeof (STUB_SUFFIX);
517 s_name = bfd_alloc (htab->stub_bfd, len);
518 if (s_name == NULL)
519 return NULL;
521 memcpy (s_name, link_sec->name, namelen);
522 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
523 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
524 if (stub_sec == NULL)
525 return NULL;
526 htab->stub_group[link_sec->id].stub_sec = stub_sec;
528 htab->stub_group[section->id].stub_sec = stub_sec;
531 /* Enter this entry into the linker stub hash table. */
532 stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table, stub_name,
533 TRUE, FALSE);
534 if (stub_entry == NULL)
536 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
537 bfd_archive_filename (section->owner),
538 stub_name);
539 return NULL;
542 stub_entry->stub_sec = stub_sec;
543 stub_entry->stub_offset = 0;
544 stub_entry->id_sec = link_sec;
545 return stub_entry;
548 /* Determine the type of stub needed, if any, for a call. */
550 static enum elf32_hppa_stub_type
551 hppa_type_of_stub (asection *input_sec,
552 const Elf_Internal_Rela *rel,
553 struct elf32_hppa_link_hash_entry *hash,
554 bfd_vma destination,
555 struct bfd_link_info *info)
557 bfd_vma location;
558 bfd_vma branch_offset;
559 bfd_vma max_branch_offset;
560 unsigned int r_type;
562 if (hash != NULL
563 && hash->elf.plt.offset != (bfd_vma) -1
564 && hash->elf.dynindx != -1
565 && !hash->plabel
566 && (info->shared
567 || !(hash->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
568 || hash->elf.root.type == bfd_link_hash_defweak))
570 /* We need an import stub. Decide between hppa_stub_import
571 and hppa_stub_import_shared later. */
572 return hppa_stub_import;
575 /* Determine where the call point is. */
576 location = (input_sec->output_offset
577 + input_sec->output_section->vma
578 + rel->r_offset);
580 branch_offset = destination - location - 8;
581 r_type = ELF32_R_TYPE (rel->r_info);
583 /* Determine if a long branch stub is needed. parisc branch offsets
584 are relative to the second instruction past the branch, ie. +8
585 bytes on from the branch instruction location. The offset is
586 signed and counts in units of 4 bytes. */
587 if (r_type == (unsigned int) R_PARISC_PCREL17F)
589 max_branch_offset = (1 << (17-1)) << 2;
591 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
593 max_branch_offset = (1 << (12-1)) << 2;
595 else /* R_PARISC_PCREL22F. */
597 max_branch_offset = (1 << (22-1)) << 2;
600 if (branch_offset + max_branch_offset >= 2*max_branch_offset)
601 return hppa_stub_long_branch;
603 return hppa_stub_none;
606 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
607 IN_ARG contains the link info pointer. */
609 #define LDIL_R1 0x20200000 /* ldil LR'XXX,%r1 */
610 #define BE_SR4_R1 0xe0202002 /* be,n RR'XXX(%sr4,%r1) */
612 #define BL_R1 0xe8200000 /* b,l .+8,%r1 */
613 #define ADDIL_R1 0x28200000 /* addil LR'XXX,%r1,%r1 */
614 #define DEPI_R1 0xd4201c1e /* depi 0,31,2,%r1 */
616 #define ADDIL_DP 0x2b600000 /* addil LR'XXX,%dp,%r1 */
617 #define LDW_R1_R21 0x48350000 /* ldw RR'XXX(%sr0,%r1),%r21 */
618 #define BV_R0_R21 0xeaa0c000 /* bv %r0(%r21) */
619 #define LDW_R1_R19 0x48330000 /* ldw RR'XXX(%sr0,%r1),%r19 */
621 #define ADDIL_R19 0x2a600000 /* addil LR'XXX,%r19,%r1 */
622 #define LDW_R1_DP 0x483b0000 /* ldw RR'XXX(%sr0,%r1),%dp */
624 #define LDSID_R21_R1 0x02a010a1 /* ldsid (%sr0,%r21),%r1 */
625 #define MTSP_R1 0x00011820 /* mtsp %r1,%sr0 */
626 #define BE_SR0_R21 0xe2a00000 /* be 0(%sr0,%r21) */
627 #define STW_RP 0x6bc23fd1 /* stw %rp,-24(%sr0,%sp) */
629 #define BL22_RP 0xe800a002 /* b,l,n XXX,%rp */
630 #define BL_RP 0xe8400002 /* b,l,n XXX,%rp */
631 #define NOP 0x08000240 /* nop */
632 #define LDW_RP 0x4bc23fd1 /* ldw -24(%sr0,%sp),%rp */
633 #define LDSID_RP_R1 0x004010a1 /* ldsid (%sr0,%rp),%r1 */
634 #define BE_SR0_RP 0xe0400002 /* be,n 0(%sr0,%rp) */
636 #ifndef R19_STUBS
637 #define R19_STUBS 1
638 #endif
640 #if R19_STUBS
641 #define LDW_R1_DLT LDW_R1_R19
642 #else
643 #define LDW_R1_DLT LDW_R1_DP
644 #endif
646 static bfd_boolean
647 hppa_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
649 struct elf32_hppa_stub_hash_entry *stub_entry;
650 struct bfd_link_info *info;
651 struct elf32_hppa_link_hash_table *htab;
652 asection *stub_sec;
653 bfd *stub_bfd;
654 bfd_byte *loc;
655 bfd_vma sym_value;
656 bfd_vma insn;
657 bfd_vma off;
658 int val;
659 int size;
661 /* Massage our args to the form they really have. */
662 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
663 info = in_arg;
665 htab = hppa_link_hash_table (info);
666 stub_sec = stub_entry->stub_sec;
668 /* Make a note of the offset within the stubs for this entry. */
669 stub_entry->stub_offset = stub_sec->_raw_size;
670 loc = stub_sec->contents + stub_entry->stub_offset;
672 stub_bfd = stub_sec->owner;
674 switch (stub_entry->stub_type)
676 case hppa_stub_long_branch:
677 /* Create the long branch. A long branch is formed with "ldil"
678 loading the upper bits of the target address into a register,
679 then branching with "be" which adds in the lower bits.
680 The "be" has its delay slot nullified. */
681 sym_value = (stub_entry->target_value
682 + stub_entry->target_section->output_offset
683 + stub_entry->target_section->output_section->vma);
685 val = hppa_field_adjust (sym_value, 0, e_lrsel);
686 insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
687 bfd_put_32 (stub_bfd, insn, loc);
689 val = hppa_field_adjust (sym_value, 0, e_rrsel) >> 2;
690 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
691 bfd_put_32 (stub_bfd, insn, loc + 4);
693 size = 8;
694 break;
696 case hppa_stub_long_branch_shared:
697 /* Branches are relative. This is where we are going to. */
698 sym_value = (stub_entry->target_value
699 + stub_entry->target_section->output_offset
700 + stub_entry->target_section->output_section->vma);
702 /* And this is where we are coming from, more or less. */
703 sym_value -= (stub_entry->stub_offset
704 + stub_sec->output_offset
705 + stub_sec->output_section->vma);
707 bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
708 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
709 insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
710 bfd_put_32 (stub_bfd, insn, loc + 4);
712 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
713 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
714 bfd_put_32 (stub_bfd, insn, loc + 8);
715 size = 12;
716 break;
718 case hppa_stub_import:
719 case hppa_stub_import_shared:
720 off = stub_entry->h->elf.plt.offset;
721 if (off >= (bfd_vma) -2)
722 abort ();
724 off &= ~ (bfd_vma) 1;
725 sym_value = (off
726 + htab->splt->output_offset
727 + htab->splt->output_section->vma
728 - elf_gp (htab->splt->output_section->owner));
730 insn = ADDIL_DP;
731 #if R19_STUBS
732 if (stub_entry->stub_type == hppa_stub_import_shared)
733 insn = ADDIL_R19;
734 #endif
735 val = hppa_field_adjust (sym_value, 0, e_lrsel),
736 insn = hppa_rebuild_insn ((int) insn, val, 21);
737 bfd_put_32 (stub_bfd, insn, loc);
739 /* It is critical to use lrsel/rrsel here because we are using
740 two different offsets (+0 and +4) from sym_value. If we use
741 lsel/rsel then with unfortunate sym_values we will round
742 sym_value+4 up to the next 2k block leading to a mis-match
743 between the lsel and rsel value. */
744 val = hppa_field_adjust (sym_value, 0, e_rrsel);
745 insn = hppa_rebuild_insn ((int) LDW_R1_R21, val, 14);
746 bfd_put_32 (stub_bfd, insn, loc + 4);
748 if (htab->multi_subspace)
750 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
751 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
752 bfd_put_32 (stub_bfd, insn, loc + 8);
754 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
755 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
756 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21, loc + 20);
757 bfd_put_32 (stub_bfd, (bfd_vma) STW_RP, loc + 24);
759 size = 28;
761 else
763 bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 8);
764 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
765 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
766 bfd_put_32 (stub_bfd, insn, loc + 12);
768 size = 16;
771 break;
773 case hppa_stub_export:
774 /* Branches are relative. This is where we are going to. */
775 sym_value = (stub_entry->target_value
776 + stub_entry->target_section->output_offset
777 + stub_entry->target_section->output_section->vma);
779 /* And this is where we are coming from. */
780 sym_value -= (stub_entry->stub_offset
781 + stub_sec->output_offset
782 + stub_sec->output_section->vma);
784 if (sym_value - 8 + (1 << (17 + 1)) >= (1 << (17 + 2))
785 && (!htab->has_22bit_branch
786 || sym_value - 8 + (1 << (22 + 1)) >= (1 << (22 + 2))))
788 (*_bfd_error_handler)
789 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
790 bfd_archive_filename (stub_entry->target_section->owner),
791 stub_sec->name,
792 (long) stub_entry->stub_offset,
793 stub_entry->root.string);
794 bfd_set_error (bfd_error_bad_value);
795 return FALSE;
798 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
799 if (!htab->has_22bit_branch)
800 insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
801 else
802 insn = hppa_rebuild_insn ((int) BL22_RP, val, 22);
803 bfd_put_32 (stub_bfd, insn, loc);
805 bfd_put_32 (stub_bfd, (bfd_vma) NOP, loc + 4);
806 bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP, loc + 8);
807 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
808 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
809 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP, loc + 20);
811 /* Point the function symbol at the stub. */
812 stub_entry->h->elf.root.u.def.section = stub_sec;
813 stub_entry->h->elf.root.u.def.value = stub_sec->_raw_size;
815 size = 24;
816 break;
818 default:
819 BFD_FAIL ();
820 return FALSE;
823 stub_sec->_raw_size += size;
824 return TRUE;
827 #undef LDIL_R1
828 #undef BE_SR4_R1
829 #undef BL_R1
830 #undef ADDIL_R1
831 #undef DEPI_R1
832 #undef LDW_R1_R21
833 #undef LDW_R1_DLT
834 #undef LDW_R1_R19
835 #undef ADDIL_R19
836 #undef LDW_R1_DP
837 #undef LDSID_R21_R1
838 #undef MTSP_R1
839 #undef BE_SR0_R21
840 #undef STW_RP
841 #undef BV_R0_R21
842 #undef BL_RP
843 #undef NOP
844 #undef LDW_RP
845 #undef LDSID_RP_R1
846 #undef BE_SR0_RP
848 /* As above, but don't actually build the stub. Just bump offset so
849 we know stub section sizes. */
851 static bfd_boolean
852 hppa_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
854 struct elf32_hppa_stub_hash_entry *stub_entry;
855 struct elf32_hppa_link_hash_table *htab;
856 int size;
858 /* Massage our args to the form they really have. */
859 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
860 htab = in_arg;
862 if (stub_entry->stub_type == hppa_stub_long_branch)
863 size = 8;
864 else if (stub_entry->stub_type == hppa_stub_long_branch_shared)
865 size = 12;
866 else if (stub_entry->stub_type == hppa_stub_export)
867 size = 24;
868 else /* hppa_stub_import or hppa_stub_import_shared. */
870 if (htab->multi_subspace)
871 size = 28;
872 else
873 size = 16;
876 stub_entry->stub_sec->_raw_size += size;
877 return TRUE;
880 /* Return nonzero if ABFD represents an HPPA ELF32 file.
881 Additionally we set the default architecture and machine. */
883 static bfd_boolean
884 elf32_hppa_object_p (bfd *abfd)
886 Elf_Internal_Ehdr * i_ehdrp;
887 unsigned int flags;
889 i_ehdrp = elf_elfheader (abfd);
890 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
892 /* GCC on hppa-linux produces binaries with OSABI=Linux,
893 but the kernel produces corefiles with OSABI=SysV. */
894 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_LINUX &&
895 i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
896 return FALSE;
898 else
900 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX)
901 return FALSE;
904 flags = i_ehdrp->e_flags;
905 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
907 case EFA_PARISC_1_0:
908 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
909 case EFA_PARISC_1_1:
910 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
911 case EFA_PARISC_2_0:
912 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
913 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
914 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
916 return TRUE;
919 /* Create the .plt and .got sections, and set up our hash table
920 short-cuts to various dynamic sections. */
922 static bfd_boolean
923 elf32_hppa_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
925 struct elf32_hppa_link_hash_table *htab;
927 /* Don't try to create the .plt and .got twice. */
928 htab = hppa_link_hash_table (info);
929 if (htab->splt != NULL)
930 return TRUE;
932 /* Call the generic code to do most of the work. */
933 if (! _bfd_elf_create_dynamic_sections (abfd, info))
934 return FALSE;
936 htab->splt = bfd_get_section_by_name (abfd, ".plt");
937 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
939 htab->sgot = bfd_get_section_by_name (abfd, ".got");
940 htab->srelgot = bfd_make_section (abfd, ".rela.got");
941 if (htab->srelgot == NULL
942 || ! bfd_set_section_flags (abfd, htab->srelgot,
943 (SEC_ALLOC
944 | SEC_LOAD
945 | SEC_HAS_CONTENTS
946 | SEC_IN_MEMORY
947 | SEC_LINKER_CREATED
948 | SEC_READONLY))
949 || ! bfd_set_section_alignment (abfd, htab->srelgot, 2))
950 return FALSE;
952 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
953 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
955 return TRUE;
958 /* Copy the extra info we tack onto an elf_link_hash_entry. */
960 static void
961 elf32_hppa_copy_indirect_symbol (const struct elf_backend_data *bed,
962 struct elf_link_hash_entry *dir,
963 struct elf_link_hash_entry *ind)
965 struct elf32_hppa_link_hash_entry *edir, *eind;
967 edir = (struct elf32_hppa_link_hash_entry *) dir;
968 eind = (struct elf32_hppa_link_hash_entry *) ind;
970 if (eind->dyn_relocs != NULL)
972 if (edir->dyn_relocs != NULL)
974 struct elf32_hppa_dyn_reloc_entry **pp;
975 struct elf32_hppa_dyn_reloc_entry *p;
977 if (ind->root.type == bfd_link_hash_indirect)
978 abort ();
980 /* Add reloc counts against the weak sym to the strong sym
981 list. Merge any entries against the same section. */
982 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
984 struct elf32_hppa_dyn_reloc_entry *q;
986 for (q = edir->dyn_relocs; q != NULL; q = q->next)
987 if (q->sec == p->sec)
989 #if RELATIVE_DYNRELOCS
990 q->relative_count += p->relative_count;
991 #endif
992 q->count += p->count;
993 *pp = p->next;
994 break;
996 if (q == NULL)
997 pp = &p->next;
999 *pp = edir->dyn_relocs;
1002 edir->dyn_relocs = eind->dyn_relocs;
1003 eind->dyn_relocs = NULL;
1006 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1009 /* Look through the relocs for a section during the first phase, and
1010 calculate needed space in the global offset table, procedure linkage
1011 table, and dynamic reloc sections. At this point we haven't
1012 necessarily read all the input files. */
1014 static bfd_boolean
1015 elf32_hppa_check_relocs (bfd *abfd,
1016 struct bfd_link_info *info,
1017 asection *sec,
1018 const Elf_Internal_Rela *relocs)
1020 Elf_Internal_Shdr *symtab_hdr;
1021 struct elf_link_hash_entry **sym_hashes;
1022 const Elf_Internal_Rela *rel;
1023 const Elf_Internal_Rela *rel_end;
1024 struct elf32_hppa_link_hash_table *htab;
1025 asection *sreloc;
1026 asection *stubreloc;
1028 if (info->relocatable)
1029 return TRUE;
1031 htab = hppa_link_hash_table (info);
1032 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1033 sym_hashes = elf_sym_hashes (abfd);
1034 sreloc = NULL;
1035 stubreloc = NULL;
1037 rel_end = relocs + sec->reloc_count;
1038 for (rel = relocs; rel < rel_end; rel++)
1040 enum {
1041 NEED_GOT = 1,
1042 NEED_PLT = 2,
1043 NEED_DYNREL = 4,
1044 PLT_PLABEL = 8
1047 unsigned int r_symndx, r_type;
1048 struct elf32_hppa_link_hash_entry *h;
1049 int need_entry;
1051 r_symndx = ELF32_R_SYM (rel->r_info);
1053 if (r_symndx < symtab_hdr->sh_info)
1054 h = NULL;
1055 else
1056 h = ((struct elf32_hppa_link_hash_entry *)
1057 sym_hashes[r_symndx - symtab_hdr->sh_info]);
1059 r_type = ELF32_R_TYPE (rel->r_info);
1061 switch (r_type)
1063 case R_PARISC_DLTIND14F:
1064 case R_PARISC_DLTIND14R:
1065 case R_PARISC_DLTIND21L:
1066 /* This symbol requires a global offset table entry. */
1067 need_entry = NEED_GOT;
1068 break;
1070 case R_PARISC_PLABEL14R: /* "Official" procedure labels. */
1071 case R_PARISC_PLABEL21L:
1072 case R_PARISC_PLABEL32:
1073 /* If the addend is non-zero, we break badly. */
1074 if (rel->r_addend != 0)
1075 abort ();
1077 /* If we are creating a shared library, then we need to
1078 create a PLT entry for all PLABELs, because PLABELs with
1079 local symbols may be passed via a pointer to another
1080 object. Additionally, output a dynamic relocation
1081 pointing to the PLT entry.
1082 For executables, the original 32-bit ABI allowed two
1083 different styles of PLABELs (function pointers): For
1084 global functions, the PLABEL word points into the .plt
1085 two bytes past a (function address, gp) pair, and for
1086 local functions the PLABEL points directly at the
1087 function. The magic +2 for the first type allows us to
1088 differentiate between the two. As you can imagine, this
1089 is a real pain when it comes to generating code to call
1090 functions indirectly or to compare function pointers.
1091 We avoid the mess by always pointing a PLABEL into the
1092 .plt, even for local functions. */
1093 need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
1094 break;
1096 case R_PARISC_PCREL12F:
1097 htab->has_12bit_branch = 1;
1098 goto branch_common;
1100 case R_PARISC_PCREL17C:
1101 case R_PARISC_PCREL17F:
1102 htab->has_17bit_branch = 1;
1103 goto branch_common;
1105 case R_PARISC_PCREL22F:
1106 htab->has_22bit_branch = 1;
1107 branch_common:
1108 /* Function calls might need to go through the .plt, and
1109 might require long branch stubs. */
1110 if (h == NULL)
1112 /* We know local syms won't need a .plt entry, and if
1113 they need a long branch stub we can't guarantee that
1114 we can reach the stub. So just flag an error later
1115 if we're doing a shared link and find we need a long
1116 branch stub. */
1117 continue;
1119 else
1121 /* Global symbols will need a .plt entry if they remain
1122 global, and in most cases won't need a long branch
1123 stub. Unfortunately, we have to cater for the case
1124 where a symbol is forced local by versioning, or due
1125 to symbolic linking, and we lose the .plt entry. */
1126 need_entry = NEED_PLT;
1127 if (h->elf.type == STT_PARISC_MILLI)
1128 need_entry = 0;
1130 break;
1132 case R_PARISC_SEGBASE: /* Used to set segment base. */
1133 case R_PARISC_SEGREL32: /* Relative reloc, used for unwind. */
1134 case R_PARISC_PCREL14F: /* PC relative load/store. */
1135 case R_PARISC_PCREL14R:
1136 case R_PARISC_PCREL17R: /* External branches. */
1137 case R_PARISC_PCREL21L: /* As above, and for load/store too. */
1138 /* We don't need to propagate the relocation if linking a
1139 shared object since these are section relative. */
1140 continue;
1142 case R_PARISC_DPREL14F: /* Used for gp rel data load/store. */
1143 case R_PARISC_DPREL14R:
1144 case R_PARISC_DPREL21L:
1145 if (info->shared)
1147 (*_bfd_error_handler)
1148 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1149 bfd_archive_filename (abfd),
1150 elf_hppa_howto_table[r_type].name);
1151 bfd_set_error (bfd_error_bad_value);
1152 return FALSE;
1154 /* Fall through. */
1156 case R_PARISC_DIR17F: /* Used for external branches. */
1157 case R_PARISC_DIR17R:
1158 case R_PARISC_DIR14F: /* Used for load/store from absolute locn. */
1159 case R_PARISC_DIR14R:
1160 case R_PARISC_DIR21L: /* As above, and for ext branches too. */
1161 #if 0
1162 /* Help debug shared library creation. Any of the above
1163 relocs can be used in shared libs, but they may cause
1164 pages to become unshared. */
1165 if (info->shared)
1167 (*_bfd_error_handler)
1168 (_("%s: relocation %s should not be used when making a shared object; recompile with -fPIC"),
1169 bfd_archive_filename (abfd),
1170 elf_hppa_howto_table[r_type].name);
1172 /* Fall through. */
1173 #endif
1175 case R_PARISC_DIR32: /* .word relocs. */
1176 /* We may want to output a dynamic relocation later. */
1177 need_entry = NEED_DYNREL;
1178 break;
1180 /* This relocation describes the C++ object vtable hierarchy.
1181 Reconstruct it for later use during GC. */
1182 case R_PARISC_GNU_VTINHERIT:
1183 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec,
1184 &h->elf, rel->r_offset))
1185 return FALSE;
1186 continue;
1188 /* This relocation describes which C++ vtable entries are actually
1189 used. Record for later use during GC. */
1190 case R_PARISC_GNU_VTENTRY:
1191 if (!_bfd_elf32_gc_record_vtentry (abfd, sec,
1192 &h->elf, rel->r_addend))
1193 return FALSE;
1194 continue;
1196 default:
1197 continue;
1200 /* Now carry out our orders. */
1201 if (need_entry & NEED_GOT)
1203 /* Allocate space for a GOT entry, as well as a dynamic
1204 relocation for this entry. */
1205 if (htab->sgot == NULL)
1207 if (htab->elf.dynobj == NULL)
1208 htab->elf.dynobj = abfd;
1209 if (!elf32_hppa_create_dynamic_sections (htab->elf.dynobj, info))
1210 return FALSE;
1213 if (h != NULL)
1215 h->elf.got.refcount += 1;
1217 else
1219 bfd_signed_vma *local_got_refcounts;
1221 /* This is a global offset table entry for a local symbol. */
1222 local_got_refcounts = elf_local_got_refcounts (abfd);
1223 if (local_got_refcounts == NULL)
1225 bfd_size_type size;
1227 /* Allocate space for local got offsets and local
1228 plt offsets. Done this way to save polluting
1229 elf_obj_tdata with another target specific
1230 pointer. */
1231 size = symtab_hdr->sh_info;
1232 size *= 2 * sizeof (bfd_signed_vma);
1233 local_got_refcounts = bfd_zalloc (abfd, size);
1234 if (local_got_refcounts == NULL)
1235 return FALSE;
1236 elf_local_got_refcounts (abfd) = local_got_refcounts;
1238 local_got_refcounts[r_symndx] += 1;
1242 if (need_entry & NEED_PLT)
1244 /* If we are creating a shared library, and this is a reloc
1245 against a weak symbol or a global symbol in a dynamic
1246 object, then we will be creating an import stub and a
1247 .plt entry for the symbol. Similarly, on a normal link
1248 to symbols defined in a dynamic object we'll need the
1249 import stub and a .plt entry. We don't know yet whether
1250 the symbol is defined or not, so make an entry anyway and
1251 clean up later in adjust_dynamic_symbol. */
1252 if ((sec->flags & SEC_ALLOC) != 0)
1254 if (h != NULL)
1256 h->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1257 h->elf.plt.refcount += 1;
1259 /* If this .plt entry is for a plabel, mark it so
1260 that adjust_dynamic_symbol will keep the entry
1261 even if it appears to be local. */
1262 if (need_entry & PLT_PLABEL)
1263 h->plabel = 1;
1265 else if (need_entry & PLT_PLABEL)
1267 bfd_signed_vma *local_got_refcounts;
1268 bfd_signed_vma *local_plt_refcounts;
1270 local_got_refcounts = elf_local_got_refcounts (abfd);
1271 if (local_got_refcounts == NULL)
1273 bfd_size_type size;
1275 /* Allocate space for local got offsets and local
1276 plt offsets. */
1277 size = symtab_hdr->sh_info;
1278 size *= 2 * sizeof (bfd_signed_vma);
1279 local_got_refcounts = bfd_zalloc (abfd, size);
1280 if (local_got_refcounts == NULL)
1281 return FALSE;
1282 elf_local_got_refcounts (abfd) = local_got_refcounts;
1284 local_plt_refcounts = (local_got_refcounts
1285 + symtab_hdr->sh_info);
1286 local_plt_refcounts[r_symndx] += 1;
1291 if (need_entry & NEED_DYNREL)
1293 /* Flag this symbol as having a non-got, non-plt reference
1294 so that we generate copy relocs if it turns out to be
1295 dynamic. */
1296 if (h != NULL && !info->shared)
1297 h->elf.elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1299 /* If we are creating a shared library then we need to copy
1300 the reloc into the shared library. However, if we are
1301 linking with -Bsymbolic, we need only copy absolute
1302 relocs or relocs against symbols that are not defined in
1303 an object we are including in the link. PC- or DP- or
1304 DLT-relative relocs against any local sym or global sym
1305 with DEF_REGULAR set, can be discarded. At this point we
1306 have not seen all the input files, so it is possible that
1307 DEF_REGULAR is not set now but will be set later (it is
1308 never cleared). We account for that possibility below by
1309 storing information in the dyn_relocs field of the
1310 hash table entry.
1312 A similar situation to the -Bsymbolic case occurs when
1313 creating shared libraries and symbol visibility changes
1314 render the symbol local.
1316 As it turns out, all the relocs we will be creating here
1317 are absolute, so we cannot remove them on -Bsymbolic
1318 links or visibility changes anyway. A STUB_REL reloc
1319 is absolute too, as in that case it is the reloc in the
1320 stub we will be creating, rather than copying the PCREL
1321 reloc in the branch.
1323 If on the other hand, we are creating an executable, we
1324 may need to keep relocations for symbols satisfied by a
1325 dynamic library if we manage to avoid copy relocs for the
1326 symbol. */
1327 if ((info->shared
1328 && (sec->flags & SEC_ALLOC) != 0
1329 && (IS_ABSOLUTE_RELOC (r_type)
1330 || (h != NULL
1331 && (!info->symbolic
1332 || h->elf.root.type == bfd_link_hash_defweak
1333 || (h->elf.elf_link_hash_flags
1334 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1335 || (!info->shared
1336 && (sec->flags & SEC_ALLOC) != 0
1337 && h != NULL
1338 && (h->elf.root.type == bfd_link_hash_defweak
1339 || (h->elf.elf_link_hash_flags
1340 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1342 struct elf32_hppa_dyn_reloc_entry *p;
1343 struct elf32_hppa_dyn_reloc_entry **head;
1345 /* Create a reloc section in dynobj and make room for
1346 this reloc. */
1347 if (sreloc == NULL)
1349 char *name;
1350 bfd *dynobj;
1352 name = (bfd_elf_string_from_elf_section
1353 (abfd,
1354 elf_elfheader (abfd)->e_shstrndx,
1355 elf_section_data (sec)->rel_hdr.sh_name));
1356 if (name == NULL)
1358 (*_bfd_error_handler)
1359 (_("Could not find relocation section for %s"),
1360 sec->name);
1361 bfd_set_error (bfd_error_bad_value);
1362 return FALSE;
1365 if (htab->elf.dynobj == NULL)
1366 htab->elf.dynobj = abfd;
1368 dynobj = htab->elf.dynobj;
1369 sreloc = bfd_get_section_by_name (dynobj, name);
1370 if (sreloc == NULL)
1372 flagword flags;
1374 sreloc = bfd_make_section (dynobj, name);
1375 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1376 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1377 if ((sec->flags & SEC_ALLOC) != 0)
1378 flags |= SEC_ALLOC | SEC_LOAD;
1379 if (sreloc == NULL
1380 || !bfd_set_section_flags (dynobj, sreloc, flags)
1381 || !bfd_set_section_alignment (dynobj, sreloc, 2))
1382 return FALSE;
1385 elf_section_data (sec)->sreloc = sreloc;
1388 /* If this is a global symbol, we count the number of
1389 relocations we need for this symbol. */
1390 if (h != NULL)
1392 head = &h->dyn_relocs;
1394 else
1396 /* Track dynamic relocs needed for local syms too.
1397 We really need local syms available to do this
1398 easily. Oh well. */
1400 asection *s;
1401 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1402 sec, r_symndx);
1403 if (s == NULL)
1404 return FALSE;
1406 head = ((struct elf32_hppa_dyn_reloc_entry **)
1407 &elf_section_data (s)->local_dynrel);
1410 p = *head;
1411 if (p == NULL || p->sec != sec)
1413 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
1414 if (p == NULL)
1415 return FALSE;
1416 p->next = *head;
1417 *head = p;
1418 p->sec = sec;
1419 p->count = 0;
1420 #if RELATIVE_DYNRELOCS
1421 p->relative_count = 0;
1422 #endif
1425 p->count += 1;
1426 #if RELATIVE_DYNRELOCS
1427 if (!IS_ABSOLUTE_RELOC (rtype))
1428 p->relative_count += 1;
1429 #endif
1434 return TRUE;
1437 /* Return the section that should be marked against garbage collection
1438 for a given relocation. */
1440 static asection *
1441 elf32_hppa_gc_mark_hook (asection *sec,
1442 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1443 Elf_Internal_Rela *rel,
1444 struct elf_link_hash_entry *h,
1445 Elf_Internal_Sym *sym)
1447 if (h != NULL)
1449 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1451 case R_PARISC_GNU_VTINHERIT:
1452 case R_PARISC_GNU_VTENTRY:
1453 break;
1455 default:
1456 switch (h->root.type)
1458 case bfd_link_hash_defined:
1459 case bfd_link_hash_defweak:
1460 return h->root.u.def.section;
1462 case bfd_link_hash_common:
1463 return h->root.u.c.p->section;
1465 default:
1466 break;
1470 else
1471 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1473 return NULL;
1476 /* Update the got and plt entry reference counts for the section being
1477 removed. */
1479 static bfd_boolean
1480 elf32_hppa_gc_sweep_hook (bfd *abfd,
1481 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1482 asection *sec,
1483 const Elf_Internal_Rela *relocs)
1485 Elf_Internal_Shdr *symtab_hdr;
1486 struct elf_link_hash_entry **sym_hashes;
1487 bfd_signed_vma *local_got_refcounts;
1488 bfd_signed_vma *local_plt_refcounts;
1489 const Elf_Internal_Rela *rel, *relend;
1491 elf_section_data (sec)->local_dynrel = NULL;
1493 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1494 sym_hashes = elf_sym_hashes (abfd);
1495 local_got_refcounts = elf_local_got_refcounts (abfd);
1496 local_plt_refcounts = local_got_refcounts;
1497 if (local_plt_refcounts != NULL)
1498 local_plt_refcounts += symtab_hdr->sh_info;
1500 relend = relocs + sec->reloc_count;
1501 for (rel = relocs; rel < relend; rel++)
1503 unsigned long r_symndx;
1504 unsigned int r_type;
1505 struct elf_link_hash_entry *h = NULL;
1507 r_symndx = ELF32_R_SYM (rel->r_info);
1508 if (r_symndx >= symtab_hdr->sh_info)
1510 struct elf32_hppa_link_hash_entry *eh;
1511 struct elf32_hppa_dyn_reloc_entry **pp;
1512 struct elf32_hppa_dyn_reloc_entry *p;
1514 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1515 eh = (struct elf32_hppa_link_hash_entry *) h;
1517 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1518 if (p->sec == sec)
1520 /* Everything must go for SEC. */
1521 *pp = p->next;
1522 break;
1526 r_type = ELF32_R_TYPE (rel->r_info);
1527 switch (r_type)
1529 case R_PARISC_DLTIND14F:
1530 case R_PARISC_DLTIND14R:
1531 case R_PARISC_DLTIND21L:
1532 if (h != NULL)
1534 if (h->got.refcount > 0)
1535 h->got.refcount -= 1;
1537 else if (local_got_refcounts != NULL)
1539 if (local_got_refcounts[r_symndx] > 0)
1540 local_got_refcounts[r_symndx] -= 1;
1542 break;
1544 case R_PARISC_PCREL12F:
1545 case R_PARISC_PCREL17C:
1546 case R_PARISC_PCREL17F:
1547 case R_PARISC_PCREL22F:
1548 if (h != NULL)
1550 if (h->plt.refcount > 0)
1551 h->plt.refcount -= 1;
1553 break;
1555 case R_PARISC_PLABEL14R:
1556 case R_PARISC_PLABEL21L:
1557 case R_PARISC_PLABEL32:
1558 if (h != NULL)
1560 if (h->plt.refcount > 0)
1561 h->plt.refcount -= 1;
1563 else if (local_plt_refcounts != NULL)
1565 if (local_plt_refcounts[r_symndx] > 0)
1566 local_plt_refcounts[r_symndx] -= 1;
1568 break;
1570 default:
1571 break;
1575 return TRUE;
1578 /* Our own version of hide_symbol, so that we can keep plt entries for
1579 plabels. */
1581 static void
1582 elf32_hppa_hide_symbol (struct bfd_link_info *info,
1583 struct elf_link_hash_entry *h,
1584 bfd_boolean force_local)
1586 if (force_local)
1588 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1589 if (h->dynindx != -1)
1591 h->dynindx = -1;
1592 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1593 h->dynstr_index);
1597 if (! ((struct elf32_hppa_link_hash_entry *) h)->plabel)
1599 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1600 h->plt.offset = (bfd_vma) -1;
1604 /* This is the condition under which elf32_hppa_finish_dynamic_symbol
1605 will be called from elflink.h. If elflink.h doesn't call our
1606 finish_dynamic_symbol routine, we'll need to do something about
1607 initializing any .plt and .got entries in elf32_hppa_relocate_section. */
1608 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1609 ((DYN) \
1610 && ((INFO)->shared \
1611 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1612 && ((H)->dynindx != -1 \
1613 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1615 /* Adjust a symbol defined by a dynamic object and referenced by a
1616 regular object. The current definition is in some section of the
1617 dynamic object, but we're not including those sections. We have to
1618 change the definition to something the rest of the link can
1619 understand. */
1621 static bfd_boolean
1622 elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
1623 struct elf_link_hash_entry *h)
1625 struct elf32_hppa_link_hash_table *htab;
1626 struct elf32_hppa_link_hash_entry *eh;
1627 struct elf32_hppa_dyn_reloc_entry *p;
1628 asection *s;
1629 unsigned int power_of_two;
1631 /* If this is a function, put it in the procedure linkage table. We
1632 will fill in the contents of the procedure linkage table later. */
1633 if (h->type == STT_FUNC
1634 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1636 if (h->plt.refcount <= 0
1637 || ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1638 && h->root.type != bfd_link_hash_defweak
1639 && ! ((struct elf32_hppa_link_hash_entry *) h)->plabel
1640 && (!info->shared || info->symbolic)))
1642 /* The .plt entry is not needed when:
1643 a) Garbage collection has removed all references to the
1644 symbol, or
1645 b) We know for certain the symbol is defined in this
1646 object, and it's not a weak definition, nor is the symbol
1647 used by a plabel relocation. Either this object is the
1648 application or we are doing a shared symbolic link. */
1650 h->plt.offset = (bfd_vma) -1;
1651 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1654 return TRUE;
1656 else
1657 h->plt.offset = (bfd_vma) -1;
1659 /* If this is a weak symbol, and there is a real definition, the
1660 processor independent code will have arranged for us to see the
1661 real definition first, and we can just use the same value. */
1662 if (h->weakdef != NULL)
1664 if (h->weakdef->root.type != bfd_link_hash_defined
1665 && h->weakdef->root.type != bfd_link_hash_defweak)
1666 abort ();
1667 h->root.u.def.section = h->weakdef->root.u.def.section;
1668 h->root.u.def.value = h->weakdef->root.u.def.value;
1669 return TRUE;
1672 /* This is a reference to a symbol defined by a dynamic object which
1673 is not a function. */
1675 /* If we are creating a shared library, we must presume that the
1676 only references to the symbol are via the global offset table.
1677 For such cases we need not do anything here; the relocations will
1678 be handled correctly by relocate_section. */
1679 if (info->shared)
1680 return TRUE;
1682 /* If there are no references to this symbol that do not use the
1683 GOT, we don't need to generate a copy reloc. */
1684 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1685 return TRUE;
1687 eh = (struct elf32_hppa_link_hash_entry *) h;
1688 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1690 s = p->sec->output_section;
1691 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1692 break;
1695 /* If we didn't find any dynamic relocs in read-only sections, then
1696 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1697 if (p == NULL)
1699 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1700 return TRUE;
1703 /* We must allocate the symbol in our .dynbss section, which will
1704 become part of the .bss section of the executable. There will be
1705 an entry for this symbol in the .dynsym section. The dynamic
1706 object will contain position independent code, so all references
1707 from the dynamic object to this symbol will go through the global
1708 offset table. The dynamic linker will use the .dynsym entry to
1709 determine the address it must put in the global offset table, so
1710 both the dynamic object and the regular object will refer to the
1711 same memory location for the variable. */
1713 htab = hppa_link_hash_table (info);
1715 /* We must generate a COPY reloc to tell the dynamic linker to
1716 copy the initial value out of the dynamic object and into the
1717 runtime process image. */
1718 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1720 htab->srelbss->_raw_size += sizeof (Elf32_External_Rela);
1721 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1724 /* We need to figure out the alignment required for this symbol. I
1725 have no idea how other ELF linkers handle this. */
1727 power_of_two = bfd_log2 (h->size);
1728 if (power_of_two > 3)
1729 power_of_two = 3;
1731 /* Apply the required alignment. */
1732 s = htab->sdynbss;
1733 s->_raw_size = BFD_ALIGN (s->_raw_size,
1734 (bfd_size_type) (1 << power_of_two));
1735 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1737 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1738 return FALSE;
1741 /* Define the symbol as being at this point in the section. */
1742 h->root.u.def.section = s;
1743 h->root.u.def.value = s->_raw_size;
1745 /* Increment the section size to make room for the symbol. */
1746 s->_raw_size += h->size;
1748 return TRUE;
1751 /* Allocate space in the .plt for entries that won't have relocations.
1752 ie. plabel entries. */
1754 static bfd_boolean
1755 allocate_plt_static (struct elf_link_hash_entry *h, void *inf)
1757 struct bfd_link_info *info;
1758 struct elf32_hppa_link_hash_table *htab;
1759 asection *s;
1761 if (h->root.type == bfd_link_hash_indirect)
1762 return TRUE;
1764 if (h->root.type == bfd_link_hash_warning)
1765 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1767 info = inf;
1768 htab = hppa_link_hash_table (info);
1769 if (htab->elf.dynamic_sections_created
1770 && h->plt.refcount > 0)
1772 /* Make sure this symbol is output as a dynamic symbol.
1773 Undefined weak syms won't yet be marked as dynamic. */
1774 if (h->dynindx == -1
1775 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
1776 && h->type != STT_PARISC_MILLI)
1778 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1779 return FALSE;
1782 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1784 /* Allocate these later. From this point on, h->plabel
1785 means that the plt entry is only used by a plabel.
1786 We'll be using a normal plt entry for this symbol, so
1787 clear the plabel indicator. */
1788 ((struct elf32_hppa_link_hash_entry *) h)->plabel = 0;
1790 else if (((struct elf32_hppa_link_hash_entry *) h)->plabel)
1792 /* Make an entry in the .plt section for plabel references
1793 that won't have a .plt entry for other reasons. */
1794 s = htab->splt;
1795 h->plt.offset = s->_raw_size;
1796 s->_raw_size += PLT_ENTRY_SIZE;
1798 else
1800 /* No .plt entry needed. */
1801 h->plt.offset = (bfd_vma) -1;
1802 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1805 else
1807 h->plt.offset = (bfd_vma) -1;
1808 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1811 return TRUE;
1814 /* Allocate space in .plt, .got and associated reloc sections for
1815 global syms. */
1817 static bfd_boolean
1818 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1820 struct bfd_link_info *info;
1821 struct elf32_hppa_link_hash_table *htab;
1822 asection *s;
1823 struct elf32_hppa_link_hash_entry *eh;
1824 struct elf32_hppa_dyn_reloc_entry *p;
1826 if (h->root.type == bfd_link_hash_indirect)
1827 return TRUE;
1829 if (h->root.type == bfd_link_hash_warning)
1830 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1832 info = inf;
1833 htab = hppa_link_hash_table (info);
1834 if (htab->elf.dynamic_sections_created
1835 && h->plt.offset != (bfd_vma) -1
1836 && !((struct elf32_hppa_link_hash_entry *) h)->plabel)
1838 /* Make an entry in the .plt section. */
1839 s = htab->splt;
1840 h->plt.offset = s->_raw_size;
1841 s->_raw_size += PLT_ENTRY_SIZE;
1843 /* We also need to make an entry in the .rela.plt section. */
1844 htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
1845 htab->need_plt_stub = 1;
1848 if (h->got.refcount > 0)
1850 /* Make sure this symbol is output as a dynamic symbol.
1851 Undefined weak syms won't yet be marked as dynamic. */
1852 if (h->dynindx == -1
1853 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
1854 && h->type != STT_PARISC_MILLI)
1856 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1857 return FALSE;
1860 s = htab->sgot;
1861 h->got.offset = s->_raw_size;
1862 s->_raw_size += GOT_ENTRY_SIZE;
1863 if (htab->elf.dynamic_sections_created
1864 && (info->shared
1865 || (h->dynindx != -1
1866 && h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0))
1868 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1871 else
1872 h->got.offset = (bfd_vma) -1;
1874 eh = (struct elf32_hppa_link_hash_entry *) h;
1875 if (eh->dyn_relocs == NULL)
1876 return TRUE;
1878 /* If this is a -Bsymbolic shared link, then we need to discard all
1879 space allocated for dynamic pc-relative relocs against symbols
1880 defined in a regular object. For the normal shared case, discard
1881 space for relocs that have become local due to symbol visibility
1882 changes. */
1883 if (info->shared)
1885 #if RELATIVE_DYNRELOCS
1886 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1887 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1888 || info->symbolic))
1890 struct elf32_hppa_dyn_reloc_entry **pp;
1892 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1894 p->count -= p->relative_count;
1895 p->relative_count = 0;
1896 if (p->count == 0)
1897 *pp = p->next;
1898 else
1899 pp = &p->next;
1902 #endif
1904 else
1906 /* For the non-shared case, discard space for relocs against
1907 symbols which turn out to need copy relocs or are not
1908 dynamic. */
1909 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1910 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1911 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1912 || (htab->elf.dynamic_sections_created
1913 && (h->root.type == bfd_link_hash_undefweak
1914 || h->root.type == bfd_link_hash_undefined))))
1916 /* Make sure this symbol is output as a dynamic symbol.
1917 Undefined weak syms won't yet be marked as dynamic. */
1918 if (h->dynindx == -1
1919 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
1920 && h->type != STT_PARISC_MILLI)
1922 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1923 return FALSE;
1926 /* If that succeeded, we know we'll be keeping all the
1927 relocs. */
1928 if (h->dynindx != -1)
1929 goto keep;
1932 eh->dyn_relocs = NULL;
1933 return TRUE;
1935 keep: ;
1938 /* Finally, allocate space. */
1939 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1941 asection *sreloc = elf_section_data (p->sec)->sreloc;
1942 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
1945 return TRUE;
1948 /* This function is called via elf_link_hash_traverse to force
1949 millicode symbols local so they do not end up as globals in the
1950 dynamic symbol table. We ought to be able to do this in
1951 adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
1952 for all dynamic symbols. Arguably, this is a bug in
1953 elf_adjust_dynamic_symbol. */
1955 static bfd_boolean
1956 clobber_millicode_symbols (struct elf_link_hash_entry *h,
1957 struct bfd_link_info *info)
1959 if (h->root.type == bfd_link_hash_warning)
1960 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1962 if (h->type == STT_PARISC_MILLI
1963 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1965 elf32_hppa_hide_symbol (info, h, TRUE);
1967 return TRUE;
1970 /* Find any dynamic relocs that apply to read-only sections. */
1972 static bfd_boolean
1973 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1975 struct elf32_hppa_link_hash_entry *eh;
1976 struct elf32_hppa_dyn_reloc_entry *p;
1978 if (h->root.type == bfd_link_hash_warning)
1979 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1981 eh = (struct elf32_hppa_link_hash_entry *) h;
1982 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1984 asection *s = p->sec->output_section;
1986 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1988 struct bfd_link_info *info = inf;
1990 info->flags |= DF_TEXTREL;
1992 /* Not an error, just cut short the traversal. */
1993 return FALSE;
1996 return TRUE;
1999 /* Set the sizes of the dynamic sections. */
2001 static bfd_boolean
2002 elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2003 struct bfd_link_info *info)
2005 struct elf32_hppa_link_hash_table *htab;
2006 bfd *dynobj;
2007 bfd *ibfd;
2008 asection *s;
2009 bfd_boolean relocs;
2011 htab = hppa_link_hash_table (info);
2012 dynobj = htab->elf.dynobj;
2013 if (dynobj == NULL)
2014 abort ();
2016 if (htab->elf.dynamic_sections_created)
2018 /* Set the contents of the .interp section to the interpreter. */
2019 if (! info->shared)
2021 s = bfd_get_section_by_name (dynobj, ".interp");
2022 if (s == NULL)
2023 abort ();
2024 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2025 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2028 /* Force millicode symbols local. */
2029 elf_link_hash_traverse (&htab->elf,
2030 clobber_millicode_symbols,
2031 info);
2034 /* Set up .got and .plt offsets for local syms, and space for local
2035 dynamic relocs. */
2036 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2038 bfd_signed_vma *local_got;
2039 bfd_signed_vma *end_local_got;
2040 bfd_signed_vma *local_plt;
2041 bfd_signed_vma *end_local_plt;
2042 bfd_size_type locsymcount;
2043 Elf_Internal_Shdr *symtab_hdr;
2044 asection *srel;
2046 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2047 continue;
2049 for (s = ibfd->sections; s != NULL; s = s->next)
2051 struct elf32_hppa_dyn_reloc_entry *p;
2053 for (p = ((struct elf32_hppa_dyn_reloc_entry *)
2054 elf_section_data (s)->local_dynrel);
2055 p != NULL;
2056 p = p->next)
2058 if (!bfd_is_abs_section (p->sec)
2059 && bfd_is_abs_section (p->sec->output_section))
2061 /* Input section has been discarded, either because
2062 it is a copy of a linkonce section or due to
2063 linker script /DISCARD/, so we'll be discarding
2064 the relocs too. */
2066 else if (p->count != 0)
2068 srel = elf_section_data (p->sec)->sreloc;
2069 srel->_raw_size += p->count * sizeof (Elf32_External_Rela);
2070 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2071 info->flags |= DF_TEXTREL;
2076 local_got = elf_local_got_refcounts (ibfd);
2077 if (!local_got)
2078 continue;
2080 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2081 locsymcount = symtab_hdr->sh_info;
2082 end_local_got = local_got + locsymcount;
2083 s = htab->sgot;
2084 srel = htab->srelgot;
2085 for (; local_got < end_local_got; ++local_got)
2087 if (*local_got > 0)
2089 *local_got = s->_raw_size;
2090 s->_raw_size += GOT_ENTRY_SIZE;
2091 if (info->shared)
2092 srel->_raw_size += sizeof (Elf32_External_Rela);
2094 else
2095 *local_got = (bfd_vma) -1;
2098 local_plt = end_local_got;
2099 end_local_plt = local_plt + locsymcount;
2100 if (! htab->elf.dynamic_sections_created)
2102 /* Won't be used, but be safe. */
2103 for (; local_plt < end_local_plt; ++local_plt)
2104 *local_plt = (bfd_vma) -1;
2106 else
2108 s = htab->splt;
2109 srel = htab->srelplt;
2110 for (; local_plt < end_local_plt; ++local_plt)
2112 if (*local_plt > 0)
2114 *local_plt = s->_raw_size;
2115 s->_raw_size += PLT_ENTRY_SIZE;
2116 if (info->shared)
2117 srel->_raw_size += sizeof (Elf32_External_Rela);
2119 else
2120 *local_plt = (bfd_vma) -1;
2125 /* Do all the .plt entries without relocs first. The dynamic linker
2126 uses the last .plt reloc to find the end of the .plt (and hence
2127 the start of the .got) for lazy linking. */
2128 elf_link_hash_traverse (&htab->elf, allocate_plt_static, info);
2130 /* Allocate global sym .plt and .got entries, and space for global
2131 sym dynamic relocs. */
2132 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2134 /* The check_relocs and adjust_dynamic_symbol entry points have
2135 determined the sizes of the various dynamic sections. Allocate
2136 memory for them. */
2137 relocs = FALSE;
2138 for (s = dynobj->sections; s != NULL; s = s->next)
2140 if ((s->flags & SEC_LINKER_CREATED) == 0)
2141 continue;
2143 if (s == htab->splt)
2145 if (htab->need_plt_stub)
2147 /* Make space for the plt stub at the end of the .plt
2148 section. We want this stub right at the end, up
2149 against the .got section. */
2150 int gotalign = bfd_section_alignment (dynobj, htab->sgot);
2151 int pltalign = bfd_section_alignment (dynobj, s);
2152 bfd_size_type mask;
2154 if (gotalign > pltalign)
2155 bfd_set_section_alignment (dynobj, s, gotalign);
2156 mask = ((bfd_size_type) 1 << gotalign) - 1;
2157 s->_raw_size = (s->_raw_size + sizeof (plt_stub) + mask) & ~mask;
2160 else if (s == htab->sgot)
2162 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2164 if (s->_raw_size != 0)
2166 /* Remember whether there are any reloc sections other
2167 than .rela.plt. */
2168 if (s != htab->srelplt)
2169 relocs = TRUE;
2171 /* We use the reloc_count field as a counter if we need
2172 to copy relocs into the output file. */
2173 s->reloc_count = 0;
2176 else
2178 /* It's not one of our sections, so don't allocate space. */
2179 continue;
2182 if (s->_raw_size == 0)
2184 /* If we don't need this section, strip it from the
2185 output file. This is mostly to handle .rela.bss and
2186 .rela.plt. We must create both sections in
2187 create_dynamic_sections, because they must be created
2188 before the linker maps input sections to output
2189 sections. The linker does that before
2190 adjust_dynamic_symbol is called, and it is that
2191 function which decides whether anything needs to go
2192 into these sections. */
2193 _bfd_strip_section_from_output (info, s);
2194 continue;
2197 /* Allocate memory for the section contents. Zero it, because
2198 we may not fill in all the reloc sections. */
2199 s->contents = bfd_zalloc (dynobj, s->_raw_size);
2200 if (s->contents == NULL && s->_raw_size != 0)
2201 return FALSE;
2204 if (htab->elf.dynamic_sections_created)
2206 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2207 actually has nothing to do with the PLT, it is how we
2208 communicate the LTP value of a load module to the dynamic
2209 linker. */
2210 #define add_dynamic_entry(TAG, VAL) \
2211 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2213 if (!add_dynamic_entry (DT_PLTGOT, 0))
2214 return FALSE;
2216 /* Add some entries to the .dynamic section. We fill in the
2217 values later, in elf32_hppa_finish_dynamic_sections, but we
2218 must add the entries now so that we get the correct size for
2219 the .dynamic section. The DT_DEBUG entry is filled in by the
2220 dynamic linker and used by the debugger. */
2221 if (!info->shared)
2223 if (!add_dynamic_entry (DT_DEBUG, 0))
2224 return FALSE;
2227 if (htab->srelplt->_raw_size != 0)
2229 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
2230 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2231 || !add_dynamic_entry (DT_JMPREL, 0))
2232 return FALSE;
2235 if (relocs)
2237 if (!add_dynamic_entry (DT_RELA, 0)
2238 || !add_dynamic_entry (DT_RELASZ, 0)
2239 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2240 return FALSE;
2242 /* If any dynamic relocs apply to a read-only section,
2243 then we need a DT_TEXTREL entry. */
2244 if ((info->flags & DF_TEXTREL) == 0)
2245 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
2247 if ((info->flags & DF_TEXTREL) != 0)
2249 if (!add_dynamic_entry (DT_TEXTREL, 0))
2250 return FALSE;
2254 #undef add_dynamic_entry
2256 return TRUE;
2259 /* External entry points for sizing and building linker stubs. */
2261 /* Set up various things so that we can make a list of input sections
2262 for each output section included in the link. Returns -1 on error,
2263 0 when no stubs will be needed, and 1 on success. */
2266 elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2268 bfd *input_bfd;
2269 unsigned int bfd_count;
2270 int top_id, top_index;
2271 asection *section;
2272 asection **input_list, **list;
2273 bfd_size_type amt;
2274 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2276 if (htab->elf.root.creator->flavour != bfd_target_elf_flavour)
2277 return 0;
2279 /* Count the number of input BFDs and find the top input section id. */
2280 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2281 input_bfd != NULL;
2282 input_bfd = input_bfd->link_next)
2284 bfd_count += 1;
2285 for (section = input_bfd->sections;
2286 section != NULL;
2287 section = section->next)
2289 if (top_id < section->id)
2290 top_id = section->id;
2293 htab->bfd_count = bfd_count;
2295 amt = sizeof (struct map_stub) * (top_id + 1);
2296 htab->stub_group = bfd_zmalloc (amt);
2297 if (htab->stub_group == NULL)
2298 return -1;
2300 /* We can't use output_bfd->section_count here to find the top output
2301 section index as some sections may have been removed, and
2302 _bfd_strip_section_from_output doesn't renumber the indices. */
2303 for (section = output_bfd->sections, top_index = 0;
2304 section != NULL;
2305 section = section->next)
2307 if (top_index < section->index)
2308 top_index = section->index;
2311 htab->top_index = top_index;
2312 amt = sizeof (asection *) * (top_index + 1);
2313 input_list = bfd_malloc (amt);
2314 htab->input_list = input_list;
2315 if (input_list == NULL)
2316 return -1;
2318 /* For sections we aren't interested in, mark their entries with a
2319 value we can check later. */
2320 list = input_list + top_index;
2322 *list = bfd_abs_section_ptr;
2323 while (list-- != input_list);
2325 for (section = output_bfd->sections;
2326 section != NULL;
2327 section = section->next)
2329 if ((section->flags & SEC_CODE) != 0)
2330 input_list[section->index] = NULL;
2333 return 1;
2336 /* The linker repeatedly calls this function for each input section,
2337 in the order that input sections are linked into output sections.
2338 Build lists of input sections to determine groupings between which
2339 we may insert linker stubs. */
2341 void
2342 elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
2344 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2346 if (isec->output_section->index <= htab->top_index)
2348 asection **list = htab->input_list + isec->output_section->index;
2349 if (*list != bfd_abs_section_ptr)
2351 /* Steal the link_sec pointer for our list. */
2352 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2353 /* This happens to make the list in reverse order,
2354 which is what we want. */
2355 PREV_SEC (isec) = *list;
2356 *list = isec;
2361 /* See whether we can group stub sections together. Grouping stub
2362 sections may result in fewer stubs. More importantly, we need to
2363 put all .init* and .fini* stubs at the beginning of the .init or
2364 .fini output sections respectively, because glibc splits the
2365 _init and _fini functions into multiple parts. Putting a stub in
2366 the middle of a function is not a good idea. */
2368 static void
2369 group_sections (struct elf32_hppa_link_hash_table *htab,
2370 bfd_size_type stub_group_size,
2371 bfd_boolean stubs_always_before_branch)
2373 asection **list = htab->input_list + htab->top_index;
2376 asection *tail = *list;
2377 if (tail == bfd_abs_section_ptr)
2378 continue;
2379 while (tail != NULL)
2381 asection *curr;
2382 asection *prev;
2383 bfd_size_type total;
2384 bfd_boolean big_sec;
2386 curr = tail;
2387 if (tail->_cooked_size)
2388 total = tail->_cooked_size;
2389 else
2390 total = tail->_raw_size;
2391 big_sec = total >= stub_group_size;
2393 while ((prev = PREV_SEC (curr)) != NULL
2394 && ((total += curr->output_offset - prev->output_offset)
2395 < stub_group_size))
2396 curr = prev;
2398 /* OK, the size from the start of CURR to the end is less
2399 than 240000 bytes and thus can be handled by one stub
2400 section. (or the tail section is itself larger than
2401 240000 bytes, in which case we may be toast.)
2402 We should really be keeping track of the total size of
2403 stubs added here, as stubs contribute to the final output
2404 section size. That's a little tricky, and this way will
2405 only break if stubs added total more than 22144 bytes, or
2406 2768 long branch stubs. It seems unlikely for more than
2407 2768 different functions to be called, especially from
2408 code only 240000 bytes long. This limit used to be
2409 250000, but c++ code tends to generate lots of little
2410 functions, and sometimes violated the assumption. */
2413 prev = PREV_SEC (tail);
2414 /* Set up this stub group. */
2415 htab->stub_group[tail->id].link_sec = curr;
2417 while (tail != curr && (tail = prev) != NULL);
2419 /* But wait, there's more! Input sections up to 240000
2420 bytes before the stub section can be handled by it too.
2421 Don't do this if we have a really large section after the
2422 stubs, as adding more stubs increases the chance that
2423 branches may not reach into the stub section. */
2424 if (!stubs_always_before_branch && !big_sec)
2426 total = 0;
2427 while (prev != NULL
2428 && ((total += tail->output_offset - prev->output_offset)
2429 < stub_group_size))
2431 tail = prev;
2432 prev = PREV_SEC (tail);
2433 htab->stub_group[tail->id].link_sec = curr;
2436 tail = prev;
2439 while (list-- != htab->input_list);
2440 free (htab->input_list);
2441 #undef PREV_SEC
2444 /* Read in all local syms for all input bfds, and create hash entries
2445 for export stubs if we are building a multi-subspace shared lib.
2446 Returns -1 on error, 1 if export stubs created, 0 otherwise. */
2448 static int
2449 get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
2451 unsigned int bfd_indx;
2452 Elf_Internal_Sym *local_syms, **all_local_syms;
2453 int stub_changed = 0;
2454 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2456 /* We want to read in symbol extension records only once. To do this
2457 we need to read in the local symbols in parallel and save them for
2458 later use; so hold pointers to the local symbols in an array. */
2459 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2460 all_local_syms = bfd_zmalloc (amt);
2461 htab->all_local_syms = all_local_syms;
2462 if (all_local_syms == NULL)
2463 return -1;
2465 /* Walk over all the input BFDs, swapping in local symbols.
2466 If we are creating a shared library, create hash entries for the
2467 export stubs. */
2468 for (bfd_indx = 0;
2469 input_bfd != NULL;
2470 input_bfd = input_bfd->link_next, bfd_indx++)
2472 Elf_Internal_Shdr *symtab_hdr;
2474 /* We'll need the symbol table in a second. */
2475 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2476 if (symtab_hdr->sh_info == 0)
2477 continue;
2479 /* We need an array of the local symbols attached to the input bfd. */
2480 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2481 if (local_syms == NULL)
2483 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2484 symtab_hdr->sh_info, 0,
2485 NULL, NULL, NULL);
2486 /* Cache them for elf_link_input_bfd. */
2487 symtab_hdr->contents = (unsigned char *) local_syms;
2489 if (local_syms == NULL)
2490 return -1;
2492 all_local_syms[bfd_indx] = local_syms;
2494 if (info->shared && htab->multi_subspace)
2496 struct elf_link_hash_entry **sym_hashes;
2497 struct elf_link_hash_entry **end_hashes;
2498 unsigned int symcount;
2500 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2501 - symtab_hdr->sh_info);
2502 sym_hashes = elf_sym_hashes (input_bfd);
2503 end_hashes = sym_hashes + symcount;
2505 /* Look through the global syms for functions; We need to
2506 build export stubs for all globally visible functions. */
2507 for (; sym_hashes < end_hashes; sym_hashes++)
2509 struct elf32_hppa_link_hash_entry *hash;
2511 hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
2513 while (hash->elf.root.type == bfd_link_hash_indirect
2514 || hash->elf.root.type == bfd_link_hash_warning)
2515 hash = ((struct elf32_hppa_link_hash_entry *)
2516 hash->elf.root.u.i.link);
2518 /* At this point in the link, undefined syms have been
2519 resolved, so we need to check that the symbol was
2520 defined in this BFD. */
2521 if ((hash->elf.root.type == bfd_link_hash_defined
2522 || hash->elf.root.type == bfd_link_hash_defweak)
2523 && hash->elf.type == STT_FUNC
2524 && hash->elf.root.u.def.section->output_section != NULL
2525 && (hash->elf.root.u.def.section->output_section->owner
2526 == output_bfd)
2527 && hash->elf.root.u.def.section->owner == input_bfd
2528 && (hash->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
2529 && !(hash->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2530 && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2532 asection *sec;
2533 const char *stub_name;
2534 struct elf32_hppa_stub_hash_entry *stub_entry;
2536 sec = hash->elf.root.u.def.section;
2537 stub_name = hash->elf.root.root.string;
2538 stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table,
2539 stub_name,
2540 FALSE, FALSE);
2541 if (stub_entry == NULL)
2543 stub_entry = hppa_add_stub (stub_name, sec, htab);
2544 if (!stub_entry)
2545 return -1;
2547 stub_entry->target_value = hash->elf.root.u.def.value;
2548 stub_entry->target_section = hash->elf.root.u.def.section;
2549 stub_entry->stub_type = hppa_stub_export;
2550 stub_entry->h = hash;
2551 stub_changed = 1;
2553 else
2555 (*_bfd_error_handler) (_("%s: duplicate export stub %s"),
2556 bfd_archive_filename (input_bfd),
2557 stub_name);
2564 return stub_changed;
2567 /* Determine and set the size of the stub section for a final link.
2569 The basic idea here is to examine all the relocations looking for
2570 PC-relative calls to a target that is unreachable with a "bl"
2571 instruction. */
2573 bfd_boolean
2574 elf32_hppa_size_stubs
2575 (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
2576 bfd_boolean multi_subspace, bfd_signed_vma group_size,
2577 asection * (*add_stub_section) (const char *, asection *),
2578 void (*layout_sections_again) (void))
2580 bfd_size_type stub_group_size;
2581 bfd_boolean stubs_always_before_branch;
2582 bfd_boolean stub_changed;
2583 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2585 /* Stash our params away. */
2586 htab->stub_bfd = stub_bfd;
2587 htab->multi_subspace = multi_subspace;
2588 htab->add_stub_section = add_stub_section;
2589 htab->layout_sections_again = layout_sections_again;
2590 stubs_always_before_branch = group_size < 0;
2591 if (group_size < 0)
2592 stub_group_size = -group_size;
2593 else
2594 stub_group_size = group_size;
2595 if (stub_group_size == 1)
2597 /* Default values. */
2598 if (stubs_always_before_branch)
2600 stub_group_size = 7680000;
2601 if (htab->has_17bit_branch || htab->multi_subspace)
2602 stub_group_size = 240000;
2603 if (htab->has_12bit_branch)
2604 stub_group_size = 7500;
2606 else
2608 stub_group_size = 6971392;
2609 if (htab->has_17bit_branch || htab->multi_subspace)
2610 stub_group_size = 217856;
2611 if (htab->has_12bit_branch)
2612 stub_group_size = 6808;
2616 group_sections (htab, stub_group_size, stubs_always_before_branch);
2618 switch (get_local_syms (output_bfd, info->input_bfds, info))
2620 default:
2621 if (htab->all_local_syms)
2622 goto error_ret_free_local;
2623 return FALSE;
2625 case 0:
2626 stub_changed = FALSE;
2627 break;
2629 case 1:
2630 stub_changed = TRUE;
2631 break;
2634 while (1)
2636 bfd *input_bfd;
2637 unsigned int bfd_indx;
2638 asection *stub_sec;
2640 for (input_bfd = info->input_bfds, bfd_indx = 0;
2641 input_bfd != NULL;
2642 input_bfd = input_bfd->link_next, bfd_indx++)
2644 Elf_Internal_Shdr *symtab_hdr;
2645 asection *section;
2646 Elf_Internal_Sym *local_syms;
2648 /* We'll need the symbol table in a second. */
2649 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2650 if (symtab_hdr->sh_info == 0)
2651 continue;
2653 local_syms = htab->all_local_syms[bfd_indx];
2655 /* Walk over each section attached to the input bfd. */
2656 for (section = input_bfd->sections;
2657 section != NULL;
2658 section = section->next)
2660 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2662 /* If there aren't any relocs, then there's nothing more
2663 to do. */
2664 if ((section->flags & SEC_RELOC) == 0
2665 || section->reloc_count == 0)
2666 continue;
2668 /* If this section is a link-once section that will be
2669 discarded, then don't create any stubs. */
2670 if (section->output_section == NULL
2671 || section->output_section->owner != output_bfd)
2672 continue;
2674 /* Get the relocs. */
2675 internal_relocs
2676 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2677 info->keep_memory);
2678 if (internal_relocs == NULL)
2679 goto error_ret_free_local;
2681 /* Now examine each relocation. */
2682 irela = internal_relocs;
2683 irelaend = irela + section->reloc_count;
2684 for (; irela < irelaend; irela++)
2686 unsigned int r_type, r_indx;
2687 enum elf32_hppa_stub_type stub_type;
2688 struct elf32_hppa_stub_hash_entry *stub_entry;
2689 asection *sym_sec;
2690 bfd_vma sym_value;
2691 bfd_vma destination;
2692 struct elf32_hppa_link_hash_entry *hash;
2693 char *stub_name;
2694 const asection *id_sec;
2696 r_type = ELF32_R_TYPE (irela->r_info);
2697 r_indx = ELF32_R_SYM (irela->r_info);
2699 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2701 bfd_set_error (bfd_error_bad_value);
2702 error_ret_free_internal:
2703 if (elf_section_data (section)->relocs == NULL)
2704 free (internal_relocs);
2705 goto error_ret_free_local;
2708 /* Only look for stubs on call instructions. */
2709 if (r_type != (unsigned int) R_PARISC_PCREL12F
2710 && r_type != (unsigned int) R_PARISC_PCREL17F
2711 && r_type != (unsigned int) R_PARISC_PCREL22F)
2712 continue;
2714 /* Now determine the call target, its name, value,
2715 section. */
2716 sym_sec = NULL;
2717 sym_value = 0;
2718 destination = 0;
2719 hash = NULL;
2720 if (r_indx < symtab_hdr->sh_info)
2722 /* It's a local symbol. */
2723 Elf_Internal_Sym *sym;
2724 Elf_Internal_Shdr *hdr;
2726 sym = local_syms + r_indx;
2727 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2728 sym_sec = hdr->bfd_section;
2729 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2730 sym_value = sym->st_value;
2731 destination = (sym_value + irela->r_addend
2732 + sym_sec->output_offset
2733 + sym_sec->output_section->vma);
2735 else
2737 /* It's an external symbol. */
2738 int e_indx;
2740 e_indx = r_indx - symtab_hdr->sh_info;
2741 hash = ((struct elf32_hppa_link_hash_entry *)
2742 elf_sym_hashes (input_bfd)[e_indx]);
2744 while (hash->elf.root.type == bfd_link_hash_indirect
2745 || hash->elf.root.type == bfd_link_hash_warning)
2746 hash = ((struct elf32_hppa_link_hash_entry *)
2747 hash->elf.root.u.i.link);
2749 if (hash->elf.root.type == bfd_link_hash_defined
2750 || hash->elf.root.type == bfd_link_hash_defweak)
2752 sym_sec = hash->elf.root.u.def.section;
2753 sym_value = hash->elf.root.u.def.value;
2754 if (sym_sec->output_section != NULL)
2755 destination = (sym_value + irela->r_addend
2756 + sym_sec->output_offset
2757 + sym_sec->output_section->vma);
2759 else if (hash->elf.root.type == bfd_link_hash_undefweak)
2761 if (! info->shared)
2762 continue;
2764 else if (hash->elf.root.type == bfd_link_hash_undefined)
2766 if (! (info->shared
2767 && info->unresolved_syms_in_objects == RM_IGNORE
2768 && (ELF_ST_VISIBILITY (hash->elf.other)
2769 == STV_DEFAULT)
2770 && hash->elf.type != STT_PARISC_MILLI))
2771 continue;
2773 else
2775 bfd_set_error (bfd_error_bad_value);
2776 goto error_ret_free_internal;
2780 /* Determine what (if any) linker stub is needed. */
2781 stub_type = hppa_type_of_stub (section, irela, hash,
2782 destination, info);
2783 if (stub_type == hppa_stub_none)
2784 continue;
2786 /* Support for grouping stub sections. */
2787 id_sec = htab->stub_group[section->id].link_sec;
2789 /* Get the name of this stub. */
2790 stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
2791 if (!stub_name)
2792 goto error_ret_free_internal;
2794 stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table,
2795 stub_name,
2796 FALSE, FALSE);
2797 if (stub_entry != NULL)
2799 /* The proper stub has already been created. */
2800 free (stub_name);
2801 continue;
2804 stub_entry = hppa_add_stub (stub_name, section, htab);
2805 if (stub_entry == NULL)
2807 free (stub_name);
2808 goto error_ret_free_internal;
2811 stub_entry->target_value = sym_value;
2812 stub_entry->target_section = sym_sec;
2813 stub_entry->stub_type = stub_type;
2814 if (info->shared)
2816 if (stub_type == hppa_stub_import)
2817 stub_entry->stub_type = hppa_stub_import_shared;
2818 else if (stub_type == hppa_stub_long_branch)
2819 stub_entry->stub_type = hppa_stub_long_branch_shared;
2821 stub_entry->h = hash;
2822 stub_changed = TRUE;
2825 /* We're done with the internal relocs, free them. */
2826 if (elf_section_data (section)->relocs == NULL)
2827 free (internal_relocs);
2831 if (!stub_changed)
2832 break;
2834 /* OK, we've added some stubs. Find out the new size of the
2835 stub sections. */
2836 for (stub_sec = htab->stub_bfd->sections;
2837 stub_sec != NULL;
2838 stub_sec = stub_sec->next)
2840 stub_sec->_raw_size = 0;
2841 stub_sec->_cooked_size = 0;
2844 bfd_hash_traverse (&htab->stub_hash_table, hppa_size_one_stub, htab);
2846 /* Ask the linker to do its stuff. */
2847 (*htab->layout_sections_again) ();
2848 stub_changed = FALSE;
2851 free (htab->all_local_syms);
2852 return TRUE;
2854 error_ret_free_local:
2855 free (htab->all_local_syms);
2856 return FALSE;
2859 /* For a final link, this function is called after we have sized the
2860 stubs to provide a value for __gp. */
2862 bfd_boolean
2863 elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
2865 struct bfd_link_hash_entry *h;
2866 asection *sec = NULL;
2867 bfd_vma gp_val = 0;
2868 struct elf32_hppa_link_hash_table *htab;
2870 htab = hppa_link_hash_table (info);
2871 h = bfd_link_hash_lookup (&htab->elf.root, "$global$", FALSE, FALSE, FALSE);
2873 if (h != NULL
2874 && (h->type == bfd_link_hash_defined
2875 || h->type == bfd_link_hash_defweak))
2877 gp_val = h->u.def.value;
2878 sec = h->u.def.section;
2880 else
2882 asection *splt;
2883 asection *sgot;
2885 if (htab->elf.root.creator->flavour == bfd_target_elf_flavour)
2887 splt = htab->splt;
2888 sgot = htab->sgot;
2890 else
2892 /* If we're not elf, look up the output sections in the
2893 hope we may actually find them. */
2894 splt = bfd_get_section_by_name (abfd, ".plt");
2895 sgot = bfd_get_section_by_name (abfd, ".got");
2898 /* Choose to point our LTP at, in this order, one of .plt, .got,
2899 or .data, if these sections exist. In the case of choosing
2900 .plt try to make the LTP ideal for addressing anywhere in the
2901 .plt or .got with a 14 bit signed offset. Typically, the end
2902 of the .plt is the start of the .got, so choose .plt + 0x2000
2903 if either the .plt or .got is larger than 0x2000. If both
2904 the .plt and .got are smaller than 0x2000, choose the end of
2905 the .plt section. */
2906 sec = splt;
2907 if (sec != NULL)
2909 gp_val = sec->_raw_size;
2910 if (gp_val > 0x2000 || (sgot && sgot->_raw_size > 0x2000))
2912 gp_val = 0x2000;
2915 else
2917 sec = sgot;
2918 if (sec != NULL)
2920 /* We know we don't have a .plt. If .got is large,
2921 offset our LTP. */
2922 if (sec->_raw_size > 0x2000)
2923 gp_val = 0x2000;
2925 else
2927 /* No .plt or .got. Who cares what the LTP is? */
2928 sec = bfd_get_section_by_name (abfd, ".data");
2932 if (h != NULL)
2934 h->type = bfd_link_hash_defined;
2935 h->u.def.value = gp_val;
2936 if (sec != NULL)
2937 h->u.def.section = sec;
2938 else
2939 h->u.def.section = bfd_abs_section_ptr;
2943 if (sec != NULL && sec->output_section != NULL)
2944 gp_val += sec->output_section->vma + sec->output_offset;
2946 elf_gp (abfd) = gp_val;
2947 return TRUE;
2950 /* Build all the stubs associated with the current output file. The
2951 stubs are kept in a hash table attached to the main linker hash
2952 table. We also set up the .plt entries for statically linked PIC
2953 functions here. This function is called via hppaelf_finish in the
2954 linker. */
2956 bfd_boolean
2957 elf32_hppa_build_stubs (struct bfd_link_info *info)
2959 asection *stub_sec;
2960 struct bfd_hash_table *table;
2961 struct elf32_hppa_link_hash_table *htab;
2963 htab = hppa_link_hash_table (info);
2965 for (stub_sec = htab->stub_bfd->sections;
2966 stub_sec != NULL;
2967 stub_sec = stub_sec->next)
2969 bfd_size_type size;
2971 /* Allocate memory to hold the linker stubs. */
2972 size = stub_sec->_raw_size;
2973 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2974 if (stub_sec->contents == NULL && size != 0)
2975 return FALSE;
2976 stub_sec->_raw_size = 0;
2979 /* Build the stubs as directed by the stub hash table. */
2980 table = &htab->stub_hash_table;
2981 bfd_hash_traverse (table, hppa_build_one_stub, info);
2983 return TRUE;
2986 /* Perform a final link. */
2988 static bfd_boolean
2989 elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
2991 /* Invoke the regular ELF linker to do all the work. */
2992 if (!bfd_elf32_bfd_final_link (abfd, info))
2993 return FALSE;
2995 /* If we're producing a final executable, sort the contents of the
2996 unwind section. */
2997 return elf_hppa_sort_unwind (abfd);
3000 /* Record the lowest address for the data and text segments. */
3002 static void
3003 hppa_record_segment_addr (bfd *abfd ATTRIBUTE_UNUSED,
3004 asection *section,
3005 void *data)
3007 struct elf32_hppa_link_hash_table *htab;
3009 htab = (struct elf32_hppa_link_hash_table *) data;
3011 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3013 bfd_vma value = section->vma - section->filepos;
3015 if ((section->flags & SEC_READONLY) != 0)
3017 if (value < htab->text_segment_base)
3018 htab->text_segment_base = value;
3020 else
3022 if (value < htab->data_segment_base)
3023 htab->data_segment_base = value;
3028 /* Perform a relocation as part of a final link. */
3030 static bfd_reloc_status_type
3031 final_link_relocate (asection *input_section,
3032 bfd_byte *contents,
3033 const Elf_Internal_Rela *rel,
3034 bfd_vma value,
3035 struct elf32_hppa_link_hash_table *htab,
3036 asection *sym_sec,
3037 struct elf32_hppa_link_hash_entry *h,
3038 struct bfd_link_info *info)
3040 int insn;
3041 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3042 unsigned int orig_r_type = r_type;
3043 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3044 int r_format = howto->bitsize;
3045 enum hppa_reloc_field_selector_type_alt r_field;
3046 bfd *input_bfd = input_section->owner;
3047 bfd_vma offset = rel->r_offset;
3048 bfd_vma max_branch_offset = 0;
3049 bfd_byte *hit_data = contents + offset;
3050 bfd_signed_vma addend = rel->r_addend;
3051 bfd_vma location;
3052 struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
3053 int val;
3055 if (r_type == R_PARISC_NONE)
3056 return bfd_reloc_ok;
3058 insn = bfd_get_32 (input_bfd, hit_data);
3060 /* Find out where we are and where we're going. */
3061 location = (offset +
3062 input_section->output_offset +
3063 input_section->output_section->vma);
3065 /* If we are not building a shared library, convert DLTIND relocs to
3066 DPREL relocs. */
3067 if (!info->shared)
3069 switch (r_type)
3071 case R_PARISC_DLTIND21L:
3072 r_type = R_PARISC_DPREL21L;
3073 break;
3075 case R_PARISC_DLTIND14R:
3076 r_type = R_PARISC_DPREL14R;
3077 break;
3079 case R_PARISC_DLTIND14F:
3080 r_type = R_PARISC_DPREL14F;
3081 break;
3085 switch (r_type)
3087 case R_PARISC_PCREL12F:
3088 case R_PARISC_PCREL17F:
3089 case R_PARISC_PCREL22F:
3090 /* If this call should go via the plt, find the import stub in
3091 the stub hash. */
3092 if (sym_sec == NULL
3093 || sym_sec->output_section == NULL
3094 || (h != NULL
3095 && h->elf.plt.offset != (bfd_vma) -1
3096 && h->elf.dynindx != -1
3097 && !h->plabel
3098 && (info->shared
3099 || !(h->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
3100 || h->elf.root.type == bfd_link_hash_defweak)))
3102 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3103 h, rel, htab);
3104 if (stub_entry != NULL)
3106 value = (stub_entry->stub_offset
3107 + stub_entry->stub_sec->output_offset
3108 + stub_entry->stub_sec->output_section->vma);
3109 addend = 0;
3111 else if (sym_sec == NULL && h != NULL
3112 && h->elf.root.type == bfd_link_hash_undefweak)
3114 /* It's OK if undefined weak. Calls to undefined weak
3115 symbols behave as if the "called" function
3116 immediately returns. We can thus call to a weak
3117 function without first checking whether the function
3118 is defined. */
3119 value = location;
3120 addend = 8;
3122 else
3123 return bfd_reloc_undefined;
3125 /* Fall thru. */
3127 case R_PARISC_PCREL21L:
3128 case R_PARISC_PCREL17C:
3129 case R_PARISC_PCREL17R:
3130 case R_PARISC_PCREL14R:
3131 case R_PARISC_PCREL14F:
3132 /* Make it a pc relative offset. */
3133 value -= location;
3134 addend -= 8;
3135 break;
3137 case R_PARISC_DPREL21L:
3138 case R_PARISC_DPREL14R:
3139 case R_PARISC_DPREL14F:
3140 /* Convert instructions that use the linkage table pointer (r19) to
3141 instructions that use the global data pointer (dp). This is the
3142 most efficient way of using PIC code in an incomplete executable,
3143 but the user must follow the standard runtime conventions for
3144 accessing data for this to work. */
3145 if (orig_r_type == R_PARISC_DLTIND21L)
3147 /* Convert addil instructions if the original reloc was a
3148 DLTIND21L. GCC sometimes uses a register other than r19 for
3149 the operation, so we must convert any addil instruction
3150 that uses this relocation. */
3151 if ((insn & 0xfc000000) == ((int) OP_ADDIL << 26))
3152 insn = ADDIL_DP;
3153 else
3154 /* We must have a ldil instruction. It's too hard to find
3155 and convert the associated add instruction, so issue an
3156 error. */
3157 (*_bfd_error_handler)
3158 (_("%s(%s+0x%lx): %s fixup for insn 0x%x is not supported in a non-shared link"),
3159 bfd_archive_filename (input_bfd),
3160 input_section->name,
3161 (long) rel->r_offset,
3162 howto->name,
3163 insn);
3165 else if (orig_r_type == R_PARISC_DLTIND14F)
3167 /* This must be a format 1 load/store. Change the base
3168 register to dp. */
3169 insn = (insn & 0xfc1ffff) | (27 << 21);
3172 /* For all the DP relative relocations, we need to examine the symbol's
3173 section. If it has no section or if it's a code section, then
3174 "data pointer relative" makes no sense. In that case we don't
3175 adjust the "value", and for 21 bit addil instructions, we change the
3176 source addend register from %dp to %r0. This situation commonly
3177 arises for undefined weak symbols and when a variable's "constness"
3178 is declared differently from the way the variable is defined. For
3179 instance: "extern int foo" with foo defined as "const int foo". */
3180 if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
3182 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3183 == (((int) OP_ADDIL << 26) | (27 << 21)))
3185 insn &= ~ (0x1f << 21);
3186 #if 0 /* debug them. */
3187 (*_bfd_error_handler)
3188 (_("%s(%s+0x%lx): fixing %s"),
3189 bfd_archive_filename (input_bfd),
3190 input_section->name,
3191 (long) rel->r_offset,
3192 howto->name);
3193 #endif
3195 /* Now try to make things easy for the dynamic linker. */
3197 break;
3199 /* Fall thru. */
3201 case R_PARISC_DLTIND21L:
3202 case R_PARISC_DLTIND14R:
3203 case R_PARISC_DLTIND14F:
3204 value -= elf_gp (input_section->output_section->owner);
3205 break;
3207 case R_PARISC_SEGREL32:
3208 if ((sym_sec->flags & SEC_CODE) != 0)
3209 value -= htab->text_segment_base;
3210 else
3211 value -= htab->data_segment_base;
3212 break;
3214 default:
3215 break;
3218 switch (r_type)
3220 case R_PARISC_DIR32:
3221 case R_PARISC_DIR14F:
3222 case R_PARISC_DIR17F:
3223 case R_PARISC_PCREL17C:
3224 case R_PARISC_PCREL14F:
3225 case R_PARISC_DPREL14F:
3226 case R_PARISC_PLABEL32:
3227 case R_PARISC_DLTIND14F:
3228 case R_PARISC_SEGBASE:
3229 case R_PARISC_SEGREL32:
3230 r_field = e_fsel;
3231 break;
3233 case R_PARISC_DLTIND21L:
3234 case R_PARISC_PCREL21L:
3235 case R_PARISC_PLABEL21L:
3236 r_field = e_lsel;
3237 break;
3239 case R_PARISC_DIR21L:
3240 case R_PARISC_DPREL21L:
3241 r_field = e_lrsel;
3242 break;
3244 case R_PARISC_PCREL17R:
3245 case R_PARISC_PCREL14R:
3246 case R_PARISC_PLABEL14R:
3247 case R_PARISC_DLTIND14R:
3248 r_field = e_rsel;
3249 break;
3251 case R_PARISC_DIR17R:
3252 case R_PARISC_DIR14R:
3253 case R_PARISC_DPREL14R:
3254 r_field = e_rrsel;
3255 break;
3257 case R_PARISC_PCREL12F:
3258 case R_PARISC_PCREL17F:
3259 case R_PARISC_PCREL22F:
3260 r_field = e_fsel;
3262 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3264 max_branch_offset = (1 << (17-1)) << 2;
3266 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3268 max_branch_offset = (1 << (12-1)) << 2;
3270 else
3272 max_branch_offset = (1 << (22-1)) << 2;
3275 /* sym_sec is NULL on undefined weak syms or when shared on
3276 undefined syms. We've already checked for a stub for the
3277 shared undefined case. */
3278 if (sym_sec == NULL)
3279 break;
3281 /* If the branch is out of reach, then redirect the
3282 call to the local stub for this function. */
3283 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3285 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3286 h, rel, htab);
3287 if (stub_entry == NULL)
3288 return bfd_reloc_undefined;
3290 /* Munge up the value and addend so that we call the stub
3291 rather than the procedure directly. */
3292 value = (stub_entry->stub_offset
3293 + stub_entry->stub_sec->output_offset
3294 + stub_entry->stub_sec->output_section->vma
3295 - location);
3296 addend = -8;
3298 break;
3300 /* Something we don't know how to handle. */
3301 default:
3302 return bfd_reloc_notsupported;
3305 /* Make sure we can reach the stub. */
3306 if (max_branch_offset != 0
3307 && value + addend + max_branch_offset >= 2*max_branch_offset)
3309 (*_bfd_error_handler)
3310 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3311 bfd_archive_filename (input_bfd),
3312 input_section->name,
3313 (long) rel->r_offset,
3314 stub_entry->root.string);
3315 bfd_set_error (bfd_error_bad_value);
3316 return bfd_reloc_notsupported;
3319 val = hppa_field_adjust (value, addend, r_field);
3321 switch (r_type)
3323 case R_PARISC_PCREL12F:
3324 case R_PARISC_PCREL17C:
3325 case R_PARISC_PCREL17F:
3326 case R_PARISC_PCREL17R:
3327 case R_PARISC_PCREL22F:
3328 case R_PARISC_DIR17F:
3329 case R_PARISC_DIR17R:
3330 /* This is a branch. Divide the offset by four.
3331 Note that we need to decide whether it's a branch or
3332 otherwise by inspecting the reloc. Inspecting insn won't
3333 work as insn might be from a .word directive. */
3334 val >>= 2;
3335 break;
3337 default:
3338 break;
3341 insn = hppa_rebuild_insn (insn, val, r_format);
3343 /* Update the instruction word. */
3344 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3345 return bfd_reloc_ok;
3348 /* Relocate an HPPA ELF section. */
3350 static bfd_boolean
3351 elf32_hppa_relocate_section (bfd *output_bfd,
3352 struct bfd_link_info *info,
3353 bfd *input_bfd,
3354 asection *input_section,
3355 bfd_byte *contents,
3356 Elf_Internal_Rela *relocs,
3357 Elf_Internal_Sym *local_syms,
3358 asection **local_sections)
3360 bfd_vma *local_got_offsets;
3361 struct elf32_hppa_link_hash_table *htab;
3362 Elf_Internal_Shdr *symtab_hdr;
3363 Elf_Internal_Rela *rel;
3364 Elf_Internal_Rela *relend;
3366 if (info->relocatable)
3367 return TRUE;
3369 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3371 htab = hppa_link_hash_table (info);
3372 local_got_offsets = elf_local_got_offsets (input_bfd);
3374 rel = relocs;
3375 relend = relocs + input_section->reloc_count;
3376 for (; rel < relend; rel++)
3378 unsigned int r_type;
3379 reloc_howto_type *howto;
3380 unsigned int r_symndx;
3381 struct elf32_hppa_link_hash_entry *h;
3382 Elf_Internal_Sym *sym;
3383 asection *sym_sec;
3384 bfd_vma relocation;
3385 bfd_reloc_status_type r;
3386 const char *sym_name;
3387 bfd_boolean plabel;
3388 bfd_boolean warned_undef;
3390 r_type = ELF32_R_TYPE (rel->r_info);
3391 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3393 bfd_set_error (bfd_error_bad_value);
3394 return FALSE;
3396 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3397 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3398 continue;
3400 /* This is a final link. */
3401 r_symndx = ELF32_R_SYM (rel->r_info);
3402 h = NULL;
3403 sym = NULL;
3404 sym_sec = NULL;
3405 warned_undef = FALSE;
3406 if (r_symndx < symtab_hdr->sh_info)
3408 /* This is a local symbol, h defaults to NULL. */
3409 sym = local_syms + r_symndx;
3410 sym_sec = local_sections[r_symndx];
3411 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sym_sec, rel);
3413 else
3415 struct elf_link_hash_entry *hh;
3416 bfd_boolean unresolved_reloc;
3418 RELOC_FOR_GLOBAL_SYMBOL (hh, elf_sym_hashes (input_bfd), r_symndx, symtab_hdr,
3419 relocation, sym_sec, unresolved_reloc, info,
3420 warned_undef);
3422 if (relocation == 0
3423 && hh->root.type != bfd_link_hash_defined
3424 && hh->root.type != bfd_link_hash_defweak
3425 && hh->root.type != bfd_link_hash_undefweak)
3427 if (!info->executable
3428 && info->unresolved_syms_in_objects == RM_IGNORE
3429 && ELF_ST_VISIBILITY (hh->other) == STV_DEFAULT
3430 && hh->type == STT_PARISC_MILLI)
3432 if (! info->callbacks->undefined_symbol
3433 (info, hh->root.root.string, input_bfd,
3434 input_section, rel->r_offset,
3435 ((info->shared && info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)
3436 || (!info->shared && info->unresolved_syms_in_objects == RM_GENERATE_ERROR))))
3437 return FALSE;
3438 warned_undef = TRUE;
3441 h = (struct elf32_hppa_link_hash_entry *) hh;
3444 /* Do any required modifications to the relocation value, and
3445 determine what types of dynamic info we need to output, if
3446 any. */
3447 plabel = 0;
3448 switch (r_type)
3450 case R_PARISC_DLTIND14F:
3451 case R_PARISC_DLTIND14R:
3452 case R_PARISC_DLTIND21L:
3454 bfd_vma off;
3455 bfd_boolean do_got = 0;
3457 /* Relocation is to the entry for this symbol in the
3458 global offset table. */
3459 if (h != NULL)
3461 bfd_boolean dyn;
3463 off = h->elf.got.offset;
3464 dyn = htab->elf.dynamic_sections_created;
3465 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, &h->elf))
3467 /* If we aren't going to call finish_dynamic_symbol,
3468 then we need to handle initialisation of the .got
3469 entry and create needed relocs here. Since the
3470 offset must always be a multiple of 4, we use the
3471 least significant bit to record whether we have
3472 initialised it already. */
3473 if ((off & 1) != 0)
3474 off &= ~1;
3475 else
3477 h->elf.got.offset |= 1;
3478 do_got = 1;
3482 else
3484 /* Local symbol case. */
3485 if (local_got_offsets == NULL)
3486 abort ();
3488 off = local_got_offsets[r_symndx];
3490 /* The offset must always be a multiple of 4. We use
3491 the least significant bit to record whether we have
3492 already generated the necessary reloc. */
3493 if ((off & 1) != 0)
3494 off &= ~1;
3495 else
3497 local_got_offsets[r_symndx] |= 1;
3498 do_got = 1;
3502 if (do_got)
3504 if (info->shared)
3506 /* Output a dynamic relocation for this GOT entry.
3507 In this case it is relative to the base of the
3508 object because the symbol index is zero. */
3509 Elf_Internal_Rela outrel;
3510 bfd_byte *loc;
3511 asection *s = htab->srelgot;
3513 outrel.r_offset = (off
3514 + htab->sgot->output_offset
3515 + htab->sgot->output_section->vma);
3516 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3517 outrel.r_addend = relocation;
3518 loc = s->contents;
3519 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3520 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3522 else
3523 bfd_put_32 (output_bfd, relocation,
3524 htab->sgot->contents + off);
3527 if (off >= (bfd_vma) -2)
3528 abort ();
3530 /* Add the base of the GOT to the relocation value. */
3531 relocation = (off
3532 + htab->sgot->output_offset
3533 + htab->sgot->output_section->vma);
3535 break;
3537 case R_PARISC_SEGREL32:
3538 /* If this is the first SEGREL relocation, then initialize
3539 the segment base values. */
3540 if (htab->text_segment_base == (bfd_vma) -1)
3541 bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
3542 break;
3544 case R_PARISC_PLABEL14R:
3545 case R_PARISC_PLABEL21L:
3546 case R_PARISC_PLABEL32:
3547 if (htab->elf.dynamic_sections_created)
3549 bfd_vma off;
3550 bfd_boolean do_plt = 0;
3552 /* If we have a global symbol with a PLT slot, then
3553 redirect this relocation to it. */
3554 if (h != NULL)
3556 off = h->elf.plt.offset;
3557 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, &h->elf))
3559 /* In a non-shared link, adjust_dynamic_symbols
3560 isn't called for symbols forced local. We
3561 need to write out the plt entry here. */
3562 if ((off & 1) != 0)
3563 off &= ~1;
3564 else
3566 h->elf.plt.offset |= 1;
3567 do_plt = 1;
3571 else
3573 bfd_vma *local_plt_offsets;
3575 if (local_got_offsets == NULL)
3576 abort ();
3578 local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
3579 off = local_plt_offsets[r_symndx];
3581 /* As for the local .got entry case, we use the last
3582 bit to record whether we've already initialised
3583 this local .plt entry. */
3584 if ((off & 1) != 0)
3585 off &= ~1;
3586 else
3588 local_plt_offsets[r_symndx] |= 1;
3589 do_plt = 1;
3593 if (do_plt)
3595 if (info->shared)
3597 /* Output a dynamic IPLT relocation for this
3598 PLT entry. */
3599 Elf_Internal_Rela outrel;
3600 bfd_byte *loc;
3601 asection *s = htab->srelplt;
3603 outrel.r_offset = (off
3604 + htab->splt->output_offset
3605 + htab->splt->output_section->vma);
3606 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3607 outrel.r_addend = relocation;
3608 loc = s->contents;
3609 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3610 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3612 else
3614 bfd_put_32 (output_bfd,
3615 relocation,
3616 htab->splt->contents + off);
3617 bfd_put_32 (output_bfd,
3618 elf_gp (htab->splt->output_section->owner),
3619 htab->splt->contents + off + 4);
3623 if (off >= (bfd_vma) -2)
3624 abort ();
3626 /* PLABELs contain function pointers. Relocation is to
3627 the entry for the function in the .plt. The magic +2
3628 offset signals to $$dyncall that the function pointer
3629 is in the .plt and thus has a gp pointer too.
3630 Exception: Undefined PLABELs should have a value of
3631 zero. */
3632 if (h == NULL
3633 || (h->elf.root.type != bfd_link_hash_undefweak
3634 && h->elf.root.type != bfd_link_hash_undefined))
3636 relocation = (off
3637 + htab->splt->output_offset
3638 + htab->splt->output_section->vma
3639 + 2);
3641 plabel = 1;
3643 /* Fall through and possibly emit a dynamic relocation. */
3645 case R_PARISC_DIR17F:
3646 case R_PARISC_DIR17R:
3647 case R_PARISC_DIR14F:
3648 case R_PARISC_DIR14R:
3649 case R_PARISC_DIR21L:
3650 case R_PARISC_DPREL14F:
3651 case R_PARISC_DPREL14R:
3652 case R_PARISC_DPREL21L:
3653 case R_PARISC_DIR32:
3654 /* r_symndx will be zero only for relocs against symbols
3655 from removed linkonce sections, or sections discarded by
3656 a linker script. */
3657 if (r_symndx == 0
3658 || (input_section->flags & SEC_ALLOC) == 0)
3659 break;
3661 /* The reloc types handled here and this conditional
3662 expression must match the code in ..check_relocs and
3663 allocate_dynrelocs. ie. We need exactly the same condition
3664 as in ..check_relocs, with some extra conditions (dynindx
3665 test in this case) to cater for relocs removed by
3666 allocate_dynrelocs. If you squint, the non-shared test
3667 here does indeed match the one in ..check_relocs, the
3668 difference being that here we test DEF_DYNAMIC as well as
3669 !DEF_REGULAR. All common syms end up with !DEF_REGULAR,
3670 which is why we can't use just that test here.
3671 Conversely, DEF_DYNAMIC can't be used in check_relocs as
3672 there all files have not been loaded. */
3673 if ((info->shared
3674 && (IS_ABSOLUTE_RELOC (r_type)
3675 || (h != NULL
3676 && h->elf.dynindx != -1
3677 && (!info->symbolic
3678 || (h->elf.elf_link_hash_flags
3679 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3680 || (!info->shared
3681 && h != NULL
3682 && h->elf.dynindx != -1
3683 && (h->elf.elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3684 && (((h->elf.elf_link_hash_flags
3685 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3686 && (h->elf.elf_link_hash_flags
3687 & ELF_LINK_HASH_DEF_REGULAR) == 0)
3688 || h->elf.root.type == bfd_link_hash_undefweak
3689 || h->elf.root.type == bfd_link_hash_undefined)))
3691 Elf_Internal_Rela outrel;
3692 bfd_boolean skip;
3693 asection *sreloc;
3694 bfd_byte *loc;
3696 /* When generating a shared object, these relocations
3697 are copied into the output file to be resolved at run
3698 time. */
3700 outrel.r_addend = rel->r_addend;
3701 outrel.r_offset =
3702 _bfd_elf_section_offset (output_bfd, info, input_section,
3703 rel->r_offset);
3704 skip = (outrel.r_offset == (bfd_vma) -1
3705 || outrel.r_offset == (bfd_vma) -2);
3706 outrel.r_offset += (input_section->output_offset
3707 + input_section->output_section->vma);
3709 if (skip)
3711 memset (&outrel, 0, sizeof (outrel));
3713 else if (h != NULL
3714 && h->elf.dynindx != -1
3715 && (plabel
3716 || !IS_ABSOLUTE_RELOC (r_type)
3717 || !info->shared
3718 || !info->symbolic
3719 || (h->elf.elf_link_hash_flags
3720 & ELF_LINK_HASH_DEF_REGULAR) == 0))
3722 outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3724 else /* It's a local symbol, or one marked to become local. */
3726 int indx = 0;
3728 /* Add the absolute offset of the symbol. */
3729 outrel.r_addend += relocation;
3731 /* Global plabels need to be processed by the
3732 dynamic linker so that functions have at most one
3733 fptr. For this reason, we need to differentiate
3734 between global and local plabels, which we do by
3735 providing the function symbol for a global plabel
3736 reloc, and no symbol for local plabels. */
3737 if (! plabel
3738 && sym_sec != NULL
3739 && sym_sec->output_section != NULL
3740 && ! bfd_is_abs_section (sym_sec))
3742 indx = elf_section_data (sym_sec->output_section)->dynindx;
3743 /* We are turning this relocation into one
3744 against a section symbol, so subtract out the
3745 output section's address but not the offset
3746 of the input section in the output section. */
3747 outrel.r_addend -= sym_sec->output_section->vma;
3750 outrel.r_info = ELF32_R_INFO (indx, r_type);
3752 #if 0
3753 /* EH info can cause unaligned DIR32 relocs.
3754 Tweak the reloc type for the dynamic linker. */
3755 if (r_type == R_PARISC_DIR32 && (outrel.r_offset & 3) != 0)
3756 outrel.r_info = ELF32_R_INFO (ELF32_R_SYM (outrel.r_info),
3757 R_PARISC_DIR32U);
3758 #endif
3759 sreloc = elf_section_data (input_section)->sreloc;
3760 if (sreloc == NULL)
3761 abort ();
3763 loc = sreloc->contents;
3764 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3765 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3767 break;
3769 default:
3770 break;
3773 r = final_link_relocate (input_section, contents, rel, relocation,
3774 htab, sym_sec, h, info);
3776 if (r == bfd_reloc_ok)
3777 continue;
3779 if (h != NULL)
3780 sym_name = h->elf.root.root.string;
3781 else
3783 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3784 symtab_hdr->sh_link,
3785 sym->st_name);
3786 if (sym_name == NULL)
3787 return FALSE;
3788 if (*sym_name == '\0')
3789 sym_name = bfd_section_name (input_bfd, sym_sec);
3792 howto = elf_hppa_howto_table + r_type;
3794 if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
3796 if (r == bfd_reloc_notsupported || !warned_undef)
3798 (*_bfd_error_handler)
3799 (_("%s(%s+0x%lx): cannot handle %s for %s"),
3800 bfd_archive_filename (input_bfd),
3801 input_section->name,
3802 (long) rel->r_offset,
3803 howto->name,
3804 sym_name);
3805 bfd_set_error (bfd_error_bad_value);
3806 return FALSE;
3809 else
3811 if (!((*info->callbacks->reloc_overflow)
3812 (info, sym_name, howto->name, 0, input_bfd, input_section,
3813 rel->r_offset)))
3814 return FALSE;
3818 return TRUE;
3821 /* Finish up dynamic symbol handling. We set the contents of various
3822 dynamic sections here. */
3824 static bfd_boolean
3825 elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
3826 struct bfd_link_info *info,
3827 struct elf_link_hash_entry *h,
3828 Elf_Internal_Sym *sym)
3830 struct elf32_hppa_link_hash_table *htab;
3831 Elf_Internal_Rela rel;
3832 bfd_byte *loc;
3834 htab = hppa_link_hash_table (info);
3836 if (h->plt.offset != (bfd_vma) -1)
3838 bfd_vma value;
3840 if (h->plt.offset & 1)
3841 abort ();
3843 /* This symbol has an entry in the procedure linkage table. Set
3844 it up.
3846 The format of a plt entry is
3847 <funcaddr>
3848 <__gp>
3850 value = 0;
3851 if (h->root.type == bfd_link_hash_defined
3852 || h->root.type == bfd_link_hash_defweak)
3854 value = h->root.u.def.value;
3855 if (h->root.u.def.section->output_section != NULL)
3856 value += (h->root.u.def.section->output_offset
3857 + h->root.u.def.section->output_section->vma);
3860 /* Create a dynamic IPLT relocation for this entry. */
3861 rel.r_offset = (h->plt.offset
3862 + htab->splt->output_offset
3863 + htab->splt->output_section->vma);
3864 if (h->dynindx != -1)
3866 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
3867 rel.r_addend = 0;
3869 else
3871 /* This symbol has been marked to become local, and is
3872 used by a plabel so must be kept in the .plt. */
3873 rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3874 rel.r_addend = value;
3877 loc = htab->srelplt->contents;
3878 loc += htab->srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
3879 bfd_elf32_swap_reloca_out (htab->splt->output_section->owner, &rel, loc);
3881 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3883 /* Mark the symbol as undefined, rather than as defined in
3884 the .plt section. Leave the value alone. */
3885 sym->st_shndx = SHN_UNDEF;
3889 if (h->got.offset != (bfd_vma) -1)
3891 /* This symbol has an entry in the global offset table. Set it
3892 up. */
3894 rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
3895 + htab->sgot->output_offset
3896 + htab->sgot->output_section->vma);
3898 /* If this is a -Bsymbolic link and the symbol is defined
3899 locally or was forced to be local because of a version file,
3900 we just want to emit a RELATIVE reloc. The entry in the
3901 global offset table will already have been initialized in the
3902 relocate_section function. */
3903 if (info->shared
3904 && (info->symbolic || h->dynindx == -1)
3905 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3907 rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3908 rel.r_addend = (h->root.u.def.value
3909 + h->root.u.def.section->output_offset
3910 + h->root.u.def.section->output_section->vma);
3912 else
3914 if ((h->got.offset & 1) != 0)
3915 abort ();
3916 bfd_put_32 (output_bfd, 0, htab->sgot->contents + h->got.offset);
3917 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
3918 rel.r_addend = 0;
3921 loc = htab->srelgot->contents;
3922 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3923 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3926 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3928 asection *s;
3930 /* This symbol needs a copy reloc. Set it up. */
3932 if (! (h->dynindx != -1
3933 && (h->root.type == bfd_link_hash_defined
3934 || h->root.type == bfd_link_hash_defweak)))
3935 abort ();
3937 s = htab->srelbss;
3939 rel.r_offset = (h->root.u.def.value
3940 + h->root.u.def.section->output_offset
3941 + h->root.u.def.section->output_section->vma);
3942 rel.r_addend = 0;
3943 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
3944 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3945 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3948 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3949 if (h->root.root.string[0] == '_'
3950 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
3951 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
3953 sym->st_shndx = SHN_ABS;
3956 return TRUE;
3959 /* Used to decide how to sort relocs in an optimal manner for the
3960 dynamic linker, before writing them out. */
3962 static enum elf_reloc_type_class
3963 elf32_hppa_reloc_type_class (const Elf_Internal_Rela *rela)
3965 if (ELF32_R_SYM (rela->r_info) == 0)
3966 return reloc_class_relative;
3968 switch ((int) ELF32_R_TYPE (rela->r_info))
3970 case R_PARISC_IPLT:
3971 return reloc_class_plt;
3972 case R_PARISC_COPY:
3973 return reloc_class_copy;
3974 default:
3975 return reloc_class_normal;
3979 /* Finish up the dynamic sections. */
3981 static bfd_boolean
3982 elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
3983 struct bfd_link_info *info)
3985 bfd *dynobj;
3986 struct elf32_hppa_link_hash_table *htab;
3987 asection *sdyn;
3989 htab = hppa_link_hash_table (info);
3990 dynobj = htab->elf.dynobj;
3992 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3994 if (htab->elf.dynamic_sections_created)
3996 Elf32_External_Dyn *dyncon, *dynconend;
3998 if (sdyn == NULL)
3999 abort ();
4001 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4002 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4003 for (; dyncon < dynconend; dyncon++)
4005 Elf_Internal_Dyn dyn;
4006 asection *s;
4008 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4010 switch (dyn.d_tag)
4012 default:
4013 continue;
4015 case DT_PLTGOT:
4016 /* Use PLTGOT to set the GOT register. */
4017 dyn.d_un.d_ptr = elf_gp (output_bfd);
4018 break;
4020 case DT_JMPREL:
4021 s = htab->srelplt;
4022 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4023 break;
4025 case DT_PLTRELSZ:
4026 s = htab->srelplt;
4027 dyn.d_un.d_val = s->_raw_size;
4028 break;
4030 case DT_RELASZ:
4031 /* Don't count procedure linkage table relocs in the
4032 overall reloc count. */
4033 s = htab->srelplt;
4034 if (s == NULL)
4035 continue;
4036 dyn.d_un.d_val -= s->_raw_size;
4037 break;
4039 case DT_RELA:
4040 /* We may not be using the standard ELF linker script.
4041 If .rela.plt is the first .rela section, we adjust
4042 DT_RELA to not include it. */
4043 s = htab->srelplt;
4044 if (s == NULL)
4045 continue;
4046 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4047 continue;
4048 dyn.d_un.d_ptr += s->_raw_size;
4049 break;
4052 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4056 if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
4058 /* Fill in the first entry in the global offset table.
4059 We use it to point to our dynamic section, if we have one. */
4060 bfd_put_32 (output_bfd,
4061 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
4062 htab->sgot->contents);
4064 /* The second entry is reserved for use by the dynamic linker. */
4065 memset (htab->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4067 /* Set .got entry size. */
4068 elf_section_data (htab->sgot->output_section)
4069 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4072 if (htab->splt != NULL && htab->splt->_raw_size != 0)
4074 /* Set plt entry size. */
4075 elf_section_data (htab->splt->output_section)
4076 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4078 if (htab->need_plt_stub)
4080 /* Set up the .plt stub. */
4081 memcpy (htab->splt->contents
4082 + htab->splt->_raw_size - sizeof (plt_stub),
4083 plt_stub, sizeof (plt_stub));
4085 if ((htab->splt->output_offset
4086 + htab->splt->output_section->vma
4087 + htab->splt->_raw_size)
4088 != (htab->sgot->output_offset
4089 + htab->sgot->output_section->vma))
4091 (*_bfd_error_handler)
4092 (_(".got section not immediately after .plt section"));
4093 return FALSE;
4098 return TRUE;
4101 /* Tweak the OSABI field of the elf header. */
4103 static void
4104 elf32_hppa_post_process_headers (bfd *abfd,
4105 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4107 Elf_Internal_Ehdr * i_ehdrp;
4109 i_ehdrp = elf_elfheader (abfd);
4111 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
4113 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
4115 else
4117 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4121 /* Called when writing out an object file to decide the type of a
4122 symbol. */
4123 static int
4124 elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
4126 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4127 return STT_PARISC_MILLI;
4128 else
4129 return type;
4132 /* Misc BFD support code. */
4133 #define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4134 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4135 #define elf_info_to_howto elf_hppa_info_to_howto
4136 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
4138 /* Stuff for the BFD linker. */
4139 #define bfd_elf32_bfd_final_link elf32_hppa_final_link
4140 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4141 #define bfd_elf32_bfd_link_hash_table_free elf32_hppa_link_hash_table_free
4142 #define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
4143 #define elf_backend_copy_indirect_symbol elf32_hppa_copy_indirect_symbol
4144 #define elf_backend_check_relocs elf32_hppa_check_relocs
4145 #define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4146 #define elf_backend_fake_sections elf_hppa_fake_sections
4147 #define elf_backend_relocate_section elf32_hppa_relocate_section
4148 #define elf_backend_hide_symbol elf32_hppa_hide_symbol
4149 #define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4150 #define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4151 #define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4152 #define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4153 #define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
4154 #define elf_backend_object_p elf32_hppa_object_p
4155 #define elf_backend_final_write_processing elf_hppa_final_write_processing
4156 #define elf_backend_post_process_headers elf32_hppa_post_process_headers
4157 #define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
4158 #define elf_backend_reloc_type_class elf32_hppa_reloc_type_class
4160 #define elf_backend_can_gc_sections 1
4161 #define elf_backend_can_refcount 1
4162 #define elf_backend_plt_alignment 2
4163 #define elf_backend_want_got_plt 0
4164 #define elf_backend_plt_readonly 0
4165 #define elf_backend_want_plt_sym 0
4166 #define elf_backend_got_header_size 8
4167 #define elf_backend_rela_normal 1
4169 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
4170 #define TARGET_BIG_NAME "elf32-hppa"
4171 #define ELF_ARCH bfd_arch_hppa
4172 #define ELF_MACHINE_CODE EM_PARISC
4173 #define ELF_MAXPAGESIZE 0x1000
4175 #include "elf32-target.h"
4177 #undef TARGET_BIG_SYM
4178 #define TARGET_BIG_SYM bfd_elf32_hppa_linux_vec
4179 #undef TARGET_BIG_NAME
4180 #define TARGET_BIG_NAME "elf32-hppa-linux"
4182 #define INCLUDED_TARGET_FILE 1
4183 #include "elf32-target.h"