Add command line switches to objcopy to provide lists of symbols in files
[deliverable/binutils-gdb.git] / bfd / elf32-hppa.c
... / ...
CommitLineData
1/* BFD back-end for HP PA-RISC ELF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
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>
10
11This file is part of BFD, the Binary File Descriptor library.
12
13This program is free software; you can redistribute it and/or modify
14it under the terms of the GNU General Public License as published by
15the Free Software Foundation; either version 2 of the License, or
16(at your option) any later version.
17
18This program is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with this program; if not, write to the Free Software
25Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26
27#include "bfd.h"
28#include "sysdep.h"
29#include "libbfd.h"
30#include "elf-bfd.h"
31#include "elf/hppa.h"
32#include "libhppa.h"
33#include "elf32-hppa.h"
34#define ARCH_SIZE 32
35#include "elf-hppa.h"
36#include "elf32-hppa.h"
37
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:
41
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. */
48
49/* We use two hash tables to hold information for linking PA ELF objects.
50
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.
54
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.
58
59 There are a number of different stubs generated by the linker.
60
61 Long branch stub:
62 : ldil LR'X,%r1
63 : be,n RR'X(%sr4,%r1)
64
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)
69
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.
76
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.
83
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
93
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
103
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 */
115
116#define PLT_ENTRY_SIZE 8
117#define PLABEL_PLT_ENTRY_SIZE PLT_ENTRY_SIZE
118#define GOT_ENTRY_SIZE 4
119#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
120
121static const bfd_byte plt_stub[] =
122{
123 0x0e, 0x80, 0x10, 0x96, /* 1: ldw 0(%r20),%r22 */
124 0xea, 0xc0, 0xc0, 0x00, /* bv %r0(%r22) */
125 0x0e, 0x88, 0x10, 0x95, /* ldw 4(%r20),%r21 */
126#define PLT_STUB_ENTRY (3*4)
127 0xea, 0x9f, 0x1f, 0xdd, /* b,l 1b,%r20 */
128 0xd6, 0x80, 0x1c, 0x1e, /* depi 0,31,2,%r20 */
129 0x00, 0xc0, 0xff, 0xee, /* 9: .word fixup_func */
130 0xde, 0xad, 0xbe, 0xef /* .word fixup_ltp */
131};
132
133/* Section name for stubs is the associated section name plus this
134 string. */
135#define STUB_SUFFIX ".stub"
136
137/* Setting the following non-zero makes all long branch stubs
138 generated during a shared link of the PIC variety. This saves on
139 relocs, but costs one extra instruction per stub. */
140#ifndef LONG_BRANCH_PIC_IN_SHLIB
141#define LONG_BRANCH_PIC_IN_SHLIB 1
142#endif
143
144/* Set this non-zero to use import stubs instead of long branch stubs
145 where a .plt entry exists for the symbol. This is a fairly useless
146 option as import stubs are bigger than PIC long branch stubs. */
147#ifndef LONG_BRANCH_VIA_PLT
148#define LONG_BRANCH_VIA_PLT 0
149#endif
150
151/* We don't need to copy any PC- or GP-relative dynamic relocs into a
152 shared object's dynamic section. */
153#ifndef RELATIVE_DYNAMIC_RELOCS
154#define RELATIVE_DYNAMIC_RELOCS 0
155#endif
156
157enum elf32_hppa_stub_type {
158 hppa_stub_long_branch,
159 hppa_stub_long_branch_shared,
160 hppa_stub_import,
161 hppa_stub_import_shared,
162 hppa_stub_export,
163 hppa_stub_none
164};
165
166struct elf32_hppa_stub_hash_entry {
167
168 /* Base hash table entry structure. */
169 struct bfd_hash_entry root;
170
171 /* The stub section. */
172 asection *stub_sec;
173
174#if ! LONG_BRANCH_PIC_IN_SHLIB
175 /* It's associated reloc section. */
176 asection *reloc_sec;
177#endif
178
179 /* Offset within stub_sec of the beginning of this stub. */
180 bfd_vma stub_offset;
181
182 /* Given the symbol's value and its section we can determine its final
183 value when building the stubs (so the stub knows where to jump. */
184 bfd_vma target_value;
185 asection *target_section;
186
187 enum elf32_hppa_stub_type stub_type;
188
189 /* The symbol table entry, if any, that this was derived from. */
190 struct elf32_hppa_link_hash_entry *h;
191
192 /* Where this stub is being called from, or, in the case of combined
193 stub sections, the first input section in the group. */
194 asection *id_sec;
195};
196
197struct elf32_hppa_link_hash_entry {
198
199 struct elf_link_hash_entry elf;
200
201 /* A pointer to the most recently used stub hash entry against this
202 symbol. */
203 struct elf32_hppa_stub_hash_entry *stub_cache;
204
205#if ! LONG_BRANCH_PIC_IN_SHLIB
206 /* Used to track whether we have allocated space for a long branch
207 stub relocation for this symbol in the given section. */
208 asection *stub_reloc_sec;
209#endif
210
211#if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
212 /* Used to count relocations for delayed sizing of relocation
213 sections. */
214 struct elf32_hppa_dyn_reloc_entry {
215
216 /* Next relocation in the chain. */
217 struct elf32_hppa_dyn_reloc_entry *next;
218
219 /* The section in dynobj. */
220 asection *section;
221
222 /* Number of relocs copied in this section. */
223 bfd_size_type count;
224 } *reloc_entries;
225#endif
226
227 /* Set during a static link if we detect a function is PIC. */
228 unsigned int maybe_pic_call:1;
229
230 /* Set if the only reason we need a .plt entry is for a non-PIC to
231 PIC function call. */
232 unsigned int pic_call:1;
233
234 /* Set if this symbol is used by a plabel reloc. */
235 unsigned int plabel:1;
236
237 /* Set if this symbol is an init or fini function and thus should
238 use an absolute reloc. */
239 unsigned int plt_abs:1;
240};
241
242struct elf32_hppa_link_hash_table {
243
244 /* The main hash table. */
245 struct elf_link_hash_table root;
246
247 /* The stub hash table. */
248 struct bfd_hash_table stub_hash_table;
249
250 /* Linker stub bfd. */
251 bfd *stub_bfd;
252
253 /* Linker call-backs. */
254 asection * (*add_stub_section) PARAMS ((const char *, asection *));
255 void (*layout_sections_again) PARAMS ((void));
256
257 /* Array to keep track of which stub sections have been created, and
258 information on stub grouping. */
259 struct map_stub {
260 /* This is the section to which stubs in the group will be
261 attached. */
262 asection *link_sec;
263 /* The stub section. */
264 asection *stub_sec;
265#if ! LONG_BRANCH_PIC_IN_SHLIB
266 /* The stub section's reloc section. */
267 asection *reloc_sec;
268#endif
269 } *stub_group;
270
271 /* Short-cuts to get to dynamic linker sections. */
272 asection *sgot;
273 asection *srelgot;
274 asection *splt;
275 asection *srelplt;
276 asection *sdynbss;
277 asection *srelbss;
278
279 /* Used during a final link to store the base of the text and data
280 segments so that we can perform SEGREL relocations. */
281 bfd_vma text_segment_base;
282 bfd_vma data_segment_base;
283
284 /* Whether we support multiple sub-spaces for shared libs. */
285 unsigned int multi_subspace:1;
286
287 /* Flags set when PCREL12F and PCREL17F branches detected. Used to
288 select suitable defaults for the stub group size. */
289 unsigned int has_12bit_branch:1;
290 unsigned int has_17bit_branch:1;
291
292 /* Set if we need a .plt stub to support lazy dynamic linking. */
293 unsigned int need_plt_stub:1;
294};
295
296/* Various hash macros and functions. */
297#define hppa_link_hash_table(p) \
298 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
299
300#define hppa_stub_hash_lookup(table, string, create, copy) \
301 ((struct elf32_hppa_stub_hash_entry *) \
302 bfd_hash_lookup ((table), (string), (create), (copy)))
303
304static struct bfd_hash_entry *stub_hash_newfunc
305 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
306
307static struct bfd_hash_entry *hppa_link_hash_newfunc
308 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
309
310static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
311 PARAMS ((bfd *));
312
313/* Stub handling functions. */
314static char *hppa_stub_name
315 PARAMS ((const asection *, const asection *,
316 const struct elf32_hppa_link_hash_entry *,
317 const Elf_Internal_Rela *));
318
319static struct elf32_hppa_stub_hash_entry *hppa_get_stub_entry
320 PARAMS ((const asection *, const asection *,
321 struct elf32_hppa_link_hash_entry *,
322 const Elf_Internal_Rela *,
323 struct elf32_hppa_link_hash_table *));
324
325static struct elf32_hppa_stub_hash_entry *hppa_add_stub
326 PARAMS ((const char *, asection *, struct elf32_hppa_link_hash_table *));
327
328static enum elf32_hppa_stub_type hppa_type_of_stub
329 PARAMS ((asection *, const Elf_Internal_Rela *,
330 struct elf32_hppa_link_hash_entry *, bfd_vma));
331
332static boolean hppa_build_one_stub
333 PARAMS ((struct bfd_hash_entry *, PTR));
334
335static boolean hppa_size_one_stub
336 PARAMS ((struct bfd_hash_entry *, PTR));
337
338/* BFD and elf backend functions. */
339static boolean elf32_hppa_object_p PARAMS ((bfd *));
340
341static boolean elf32_hppa_add_symbol_hook
342 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
343 const char **, flagword *, asection **, bfd_vma *));
344
345static boolean elf32_hppa_create_dynamic_sections
346 PARAMS ((bfd *, struct bfd_link_info *));
347
348static boolean elf32_hppa_check_relocs
349 PARAMS ((bfd *, struct bfd_link_info *,
350 asection *, const Elf_Internal_Rela *));
351
352static asection *elf32_hppa_gc_mark_hook
353 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
354 struct elf_link_hash_entry *, Elf_Internal_Sym *));
355
356static boolean elf32_hppa_gc_sweep_hook
357 PARAMS ((bfd *, struct bfd_link_info *,
358 asection *, const Elf_Internal_Rela *));
359
360static void elf32_hppa_hide_symbol
361 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
362
363static boolean elf32_hppa_adjust_dynamic_symbol
364 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
365
366static boolean hppa_handle_PIC_calls
367 PARAMS ((struct elf_link_hash_entry *, PTR));
368
369#if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
370 || RELATIVE_DYNAMIC_RELOCS)
371static boolean hppa_discard_copies
372 PARAMS ((struct elf_link_hash_entry *, PTR));
373#endif
374
375static boolean clobber_millicode_symbols
376 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
377
378static boolean elf32_hppa_size_dynamic_sections
379 PARAMS ((bfd *, struct bfd_link_info *));
380
381static boolean elf32_hppa_final_link
382 PARAMS ((bfd *, struct bfd_link_info *));
383
384static void hppa_record_segment_addr
385 PARAMS ((bfd *, asection *, PTR));
386
387static bfd_reloc_status_type final_link_relocate
388 PARAMS ((asection *, bfd_byte *, const Elf_Internal_Rela *,
389 bfd_vma, struct elf32_hppa_link_hash_table *, asection *,
390 struct elf32_hppa_link_hash_entry *));
391
392static boolean elf32_hppa_relocate_section
393 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
394 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
395
396static int hppa_unwind_entry_compare
397 PARAMS ((const PTR, const PTR));
398
399static boolean elf32_hppa_finish_dynamic_symbol
400 PARAMS ((bfd *, struct bfd_link_info *,
401 struct elf_link_hash_entry *, Elf_Internal_Sym *));
402
403static boolean elf32_hppa_finish_dynamic_sections
404 PARAMS ((bfd *, struct bfd_link_info *));
405
406static void elf32_hppa_post_process_headers
407 PARAMS ((bfd *, struct bfd_link_info *));
408
409static int elf32_hppa_elf_get_symbol_type
410 PARAMS ((Elf_Internal_Sym *, int));
411
412/* Assorted hash table functions. */
413
414/* Initialize an entry in the stub hash table. */
415
416static struct bfd_hash_entry *
417stub_hash_newfunc (entry, table, string)
418 struct bfd_hash_entry *entry;
419 struct bfd_hash_table *table;
420 const char *string;
421{
422 struct elf32_hppa_stub_hash_entry *ret;
423
424 ret = (struct elf32_hppa_stub_hash_entry *) entry;
425
426 /* Allocate the structure if it has not already been allocated by a
427 subclass. */
428 if (ret == NULL)
429 {
430 ret = ((struct elf32_hppa_stub_hash_entry *)
431 bfd_hash_allocate (table,
432 sizeof (struct elf32_hppa_stub_hash_entry)));
433 if (ret == NULL)
434 return NULL;
435 }
436
437 /* Call the allocation method of the superclass. */
438 ret = ((struct elf32_hppa_stub_hash_entry *)
439 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
440
441 if (ret)
442 {
443 /* Initialize the local fields. */
444 ret->stub_sec = NULL;
445#if ! LONG_BRANCH_PIC_IN_SHLIB
446 ret->reloc_sec = NULL;
447#endif
448 ret->stub_offset = 0;
449 ret->target_value = 0;
450 ret->target_section = NULL;
451 ret->stub_type = hppa_stub_long_branch;
452 ret->h = NULL;
453 ret->id_sec = NULL;
454 }
455
456 return (struct bfd_hash_entry *) ret;
457}
458
459/* Initialize an entry in the link hash table. */
460
461static struct bfd_hash_entry *
462hppa_link_hash_newfunc (entry, table, string)
463 struct bfd_hash_entry *entry;
464 struct bfd_hash_table *table;
465 const char *string;
466{
467 struct elf32_hppa_link_hash_entry *ret;
468
469 ret = (struct elf32_hppa_link_hash_entry *) entry;
470
471 /* Allocate the structure if it has not already been allocated by a
472 subclass. */
473 if (ret == NULL)
474 {
475 ret = ((struct elf32_hppa_link_hash_entry *)
476 bfd_hash_allocate (table,
477 sizeof (struct elf32_hppa_link_hash_entry)));
478 if (ret == NULL)
479 return NULL;
480 }
481
482 /* Call the allocation method of the superclass. */
483 ret = ((struct elf32_hppa_link_hash_entry *)
484 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
485 table, string));
486
487 if (ret)
488 {
489 /* Initialize the local fields. */
490#if ! LONG_BRANCH_PIC_IN_SHLIB
491 ret->stub_reloc_sec = NULL;
492#endif
493 ret->stub_cache = NULL;
494#if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
495 ret->reloc_entries = NULL;
496#endif
497 ret->maybe_pic_call = 0;
498 ret->pic_call = 0;
499 ret->plabel = 0;
500 ret->plt_abs = 0;
501 }
502
503 return (struct bfd_hash_entry *) ret;
504}
505
506/* Create the derived linker hash table. The PA ELF port uses the derived
507 hash table to keep information specific to the PA ELF linker (without
508 using static variables). */
509
510static struct bfd_link_hash_table *
511elf32_hppa_link_hash_table_create (abfd)
512 bfd *abfd;
513{
514 struct elf32_hppa_link_hash_table *ret;
515
516 ret = ((struct elf32_hppa_link_hash_table *) bfd_alloc (abfd, sizeof (*ret)));
517 if (ret == NULL)
518 return NULL;
519
520 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd, hppa_link_hash_newfunc))
521 {
522 bfd_release (abfd, ret);
523 return NULL;
524 }
525
526 /* Init the stub hash table too. */
527 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc))
528 return NULL;
529
530 ret->stub_bfd = NULL;
531 ret->add_stub_section = NULL;
532 ret->layout_sections_again = NULL;
533 ret->stub_group = NULL;
534 ret->sgot = NULL;
535 ret->srelgot = NULL;
536 ret->splt = NULL;
537 ret->srelplt = NULL;
538 ret->sdynbss = NULL;
539 ret->srelbss = NULL;
540 ret->text_segment_base = (bfd_vma) -1;
541 ret->data_segment_base = (bfd_vma) -1;
542 ret->multi_subspace = 0;
543 ret->has_12bit_branch = 0;
544 ret->has_17bit_branch = 0;
545 ret->need_plt_stub = 0;
546
547 return &ret->root.root;
548}
549
550/* Build a name for an entry in the stub hash table. */
551
552static char *
553hppa_stub_name (input_section, sym_sec, hash, rel)
554 const asection *input_section;
555 const asection *sym_sec;
556 const struct elf32_hppa_link_hash_entry *hash;
557 const Elf_Internal_Rela *rel;
558{
559 char *stub_name;
560 size_t len;
561
562 if (hash)
563 {
564 len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
565 stub_name = bfd_malloc (len);
566 if (stub_name != NULL)
567 {
568 sprintf (stub_name, "%08x_%s+%x",
569 input_section->id & 0xffffffff,
570 hash->elf.root.root.string,
571 (int) rel->r_addend & 0xffffffff);
572 }
573 }
574 else
575 {
576 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
577 stub_name = bfd_malloc (len);
578 if (stub_name != NULL)
579 {
580 sprintf (stub_name, "%08x_%x:%x+%x",
581 input_section->id & 0xffffffff,
582 sym_sec->id & 0xffffffff,
583 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
584 (int) rel->r_addend & 0xffffffff);
585 }
586 }
587 return stub_name;
588}
589
590/* Look up an entry in the stub hash. Stub entries are cached because
591 creating the stub name takes a bit of time. */
592
593static struct elf32_hppa_stub_hash_entry *
594hppa_get_stub_entry (input_section, sym_sec, hash, rel, hplink)
595 const asection *input_section;
596 const asection *sym_sec;
597 struct elf32_hppa_link_hash_entry *hash;
598 const Elf_Internal_Rela *rel;
599 struct elf32_hppa_link_hash_table *hplink;
600{
601 struct elf32_hppa_stub_hash_entry *stub_entry;
602 const asection *id_sec;
603
604 /* If this input section is part of a group of sections sharing one
605 stub section, then use the id of the first section in the group.
606 Stub names need to include a section id, as there may well be
607 more than one stub used to reach say, printf, and we need to
608 distinguish between them. */
609 id_sec = hplink->stub_group[input_section->id].link_sec;
610
611 if (hash != NULL && hash->stub_cache != NULL
612 && hash->stub_cache->h == hash
613 && hash->stub_cache->id_sec == id_sec)
614 {
615 stub_entry = hash->stub_cache;
616 }
617 else
618 {
619 char *stub_name;
620
621 stub_name = hppa_stub_name (id_sec, sym_sec, hash, rel);
622 if (stub_name == NULL)
623 return NULL;
624
625 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
626 stub_name, false, false);
627 if (stub_entry == NULL)
628 {
629 if (hash == NULL || hash->elf.root.type != bfd_link_hash_undefweak)
630 (*_bfd_error_handler) (_("%s(%s+0x%lx): cannot find stub entry %s"),
631 bfd_get_filename (input_section->owner),
632 input_section->name,
633 (long) rel->r_offset,
634 stub_name);
635 }
636 else
637 {
638 if (hash != NULL)
639 hash->stub_cache = stub_entry;
640 }
641
642 free (stub_name);
643 }
644
645 return stub_entry;
646}
647
648/* Add a new stub entry to the stub hash. Not all fields of the new
649 stub entry are initialised. */
650
651static struct elf32_hppa_stub_hash_entry *
652hppa_add_stub (stub_name, section, hplink)
653 const char *stub_name;
654 asection *section;
655 struct elf32_hppa_link_hash_table *hplink;
656{
657 asection *link_sec;
658 asection *stub_sec;
659 struct elf32_hppa_stub_hash_entry *stub_entry;
660
661 link_sec = hplink->stub_group[section->id].link_sec;
662 stub_sec = hplink->stub_group[section->id].stub_sec;
663 if (stub_sec == NULL)
664 {
665 stub_sec = hplink->stub_group[link_sec->id].stub_sec;
666 if (stub_sec == NULL)
667 {
668 size_t len;
669 char *s_name;
670
671 len = strlen (link_sec->name) + sizeof (STUB_SUFFIX);
672 s_name = bfd_alloc (hplink->stub_bfd, len);
673 if (s_name == NULL)
674 return NULL;
675
676 strcpy (s_name, link_sec->name);
677 strcpy (s_name + len - sizeof (STUB_SUFFIX), STUB_SUFFIX);
678 stub_sec = (*hplink->add_stub_section) (s_name, link_sec);
679 if (stub_sec == NULL)
680 return NULL;
681 hplink->stub_group[link_sec->id].stub_sec = stub_sec;
682 }
683 hplink->stub_group[section->id].stub_sec = stub_sec;
684 }
685
686 /* Enter this entry into the linker stub hash table. */
687 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table, stub_name,
688 true, false);
689 if (stub_entry == NULL)
690 {
691 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
692 bfd_get_filename (section->owner),
693 stub_name);
694 return NULL;
695 }
696
697 stub_entry->stub_sec = stub_sec;
698#if ! LONG_BRANCH_PIC_IN_SHLIB
699 stub_entry->reloc_sec = hplink->stub_group[section->id].reloc_sec;
700#endif
701 stub_entry->stub_offset = 0;
702 stub_entry->id_sec = link_sec;
703 return stub_entry;
704}
705
706/* Determine the type of stub needed, if any, for a call. */
707
708static enum elf32_hppa_stub_type
709hppa_type_of_stub (input_sec, rel, hash, destination)
710 asection *input_sec;
711 const Elf_Internal_Rela *rel;
712 struct elf32_hppa_link_hash_entry *hash;
713 bfd_vma destination;
714{
715 bfd_vma location;
716 bfd_vma branch_offset;
717 bfd_vma max_branch_offset;
718 unsigned int r_type;
719
720 if (hash != NULL
721 && (((hash->elf.root.type == bfd_link_hash_defined
722 || hash->elf.root.type == bfd_link_hash_defweak)
723 && hash->elf.root.u.def.section->output_section == NULL)
724 || (hash->elf.root.type == bfd_link_hash_defweak
725 && hash->elf.dynindx != -1
726 && hash->elf.plt.offset != (bfd_vma) -1)
727 || hash->elf.root.type == bfd_link_hash_undefweak
728 || hash->elf.root.type == bfd_link_hash_undefined
729 || (hash->maybe_pic_call && !(input_sec->flags & SEC_HAS_GOT_REF))))
730 {
731 /* If output_section is NULL, then it's a symbol defined in a
732 shared library. We will need an import stub. Decide between
733 hppa_stub_import and hppa_stub_import_shared later. For
734 shared links we need stubs for undefined or weak syms too;
735 They will presumably be resolved by the dynamic linker. */
736 return hppa_stub_import;
737 }
738
739 /* Determine where the call point is. */
740 location = (input_sec->output_offset
741 + input_sec->output_section->vma
742 + rel->r_offset);
743
744 branch_offset = destination - location - 8;
745 r_type = ELF32_R_TYPE (rel->r_info);
746
747 /* Determine if a long branch stub is needed. parisc branch offsets
748 are relative to the second instruction past the branch, ie. +8
749 bytes on from the branch instruction location. The offset is
750 signed and counts in units of 4 bytes. */
751 if (r_type == (unsigned int) R_PARISC_PCREL17F)
752 {
753 max_branch_offset = (1 << (17-1)) << 2;
754 }
755 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
756 {
757 max_branch_offset = (1 << (12-1)) << 2;
758 }
759 else /* R_PARISC_PCREL22F. */
760 {
761 max_branch_offset = (1 << (22-1)) << 2;
762 }
763
764 if (branch_offset + max_branch_offset >= 2*max_branch_offset)
765 {
766#if LONG_BRANCH_VIA_PLT
767 if (hash != NULL
768 && hash->elf.dynindx != -1
769 && hash->elf.plt.offset != (bfd_vma) -1)
770 {
771 /* If we are doing a shared link and find we need a long
772 branch stub, then go via the .plt if possible. */
773 return hppa_stub_import;
774 }
775 else
776#endif
777 return hppa_stub_long_branch;
778 }
779 return hppa_stub_none;
780}
781
782/* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
783 IN_ARG contains the link info pointer. */
784
785#define LDIL_R1 0x20200000 /* ldil LR'XXX,%r1 */
786#define BE_SR4_R1 0xe0202002 /* be,n RR'XXX(%sr4,%r1) */
787
788#define BL_R1 0xe8200000 /* b,l .+8,%r1 */
789#define ADDIL_R1 0x28200000 /* addil LR'XXX,%r1,%r1 */
790#define DEPI_R1 0xd4201c1e /* depi 0,31,2,%r1 */
791
792#define ADDIL_DP 0x2b600000 /* addil LR'XXX,%dp,%r1 */
793#define LDW_R1_R21 0x48350000 /* ldw RR'XXX(%sr0,%r1),%r21 */
794#define BV_R0_R21 0xeaa0c000 /* bv %r0(%r21) */
795#define LDW_R1_R19 0x48330000 /* ldw RR'XXX(%sr0,%r1),%r19 */
796
797#define ADDIL_R19 0x2a600000 /* addil LR'XXX,%r19,%r1 */
798#define LDW_R1_DP 0x483b0000 /* ldw RR'XXX(%sr0,%r1),%dp */
799
800#define LDSID_R21_R1 0x02a010a1 /* ldsid (%sr0,%r21),%r1 */
801#define MTSP_R1 0x00011820 /* mtsp %r1,%sr0 */
802#define BE_SR0_R21 0xe2a00000 /* be 0(%sr0,%r21) */
803#define STW_RP 0x6bc23fd1 /* stw %rp,-24(%sr0,%sp) */
804
805#define BL_RP 0xe8400002 /* b,l,n XXX,%rp */
806#define NOP 0x08000240 /* nop */
807#define LDW_RP 0x4bc23fd1 /* ldw -24(%sr0,%sp),%rp */
808#define LDSID_RP_R1 0x004010a1 /* ldsid (%sr0,%rp),%r1 */
809#define BE_SR0_RP 0xe0400002 /* be,n 0(%sr0,%rp) */
810
811#ifndef R19_STUBS
812#define R19_STUBS 1
813#endif
814
815#if R19_STUBS
816#define LDW_R1_DLT LDW_R1_R19
817#else
818#define LDW_R1_DLT LDW_R1_DP
819#endif
820
821static boolean
822hppa_build_one_stub (gen_entry, in_arg)
823 struct bfd_hash_entry *gen_entry;
824 PTR in_arg;
825{
826 struct elf32_hppa_stub_hash_entry *stub_entry;
827 struct bfd_link_info *info;
828 struct elf32_hppa_link_hash_table *hplink;
829 asection *stub_sec;
830 bfd *stub_bfd;
831 bfd_byte *loc;
832 bfd_vma sym_value;
833 bfd_vma insn;
834 bfd_vma off;
835 int val;
836 int size;
837
838 /* Massage our args to the form they really have. */
839 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
840 info = (struct bfd_link_info *) in_arg;
841
842 hplink = hppa_link_hash_table (info);
843 stub_sec = stub_entry->stub_sec;
844
845 /* Make a note of the offset within the stubs for this entry. */
846 stub_entry->stub_offset = stub_sec->_raw_size;
847 loc = stub_sec->contents + stub_entry->stub_offset;
848
849 stub_bfd = stub_sec->owner;
850
851 switch (stub_entry->stub_type)
852 {
853 case hppa_stub_long_branch:
854 /* Create the long branch. A long branch is formed with "ldil"
855 loading the upper bits of the target address into a register,
856 then branching with "be" which adds in the lower bits.
857 The "be" has its delay slot nullified. */
858 sym_value = (stub_entry->target_value
859 + stub_entry->target_section->output_offset
860 + stub_entry->target_section->output_section->vma);
861
862 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_lrsel);
863 insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
864 bfd_put_32 (stub_bfd, insn, loc);
865
866 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_rrsel) >> 2;
867 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
868 bfd_put_32 (stub_bfd, insn, loc + 4);
869
870#if ! LONG_BRANCH_PIC_IN_SHLIB
871 if (info->shared)
872 {
873 /* Output a dynamic relocation for this stub. We only
874 output one PCREL21L reloc per stub, trusting that the
875 dynamic linker will also fix the implied PCREL17R for the
876 second instruction. PCREL21L dynamic relocs had better
877 never be emitted for some other purpose... */
878 asection *srel;
879 Elf_Internal_Rela outrel;
880
881 if (stub_entry->h == NULL)
882 {
883 (*_bfd_error_handler)
884 (_("%s(%s+0x%lx): cannot relocate %s, recompile with -ffunction-sections"),
885 bfd_get_filename (stub_entry->target_section->owner),
886 stub_sec->name,
887 (long) stub_entry->stub_offset,
888 stub_entry->root.string);
889 bfd_set_error (bfd_error_bad_value);
890 return false;
891 }
892
893 srel = stub_entry->reloc_sec;
894 if (srel == NULL)
895 {
896 (*_bfd_error_handler)
897 (_("Could not find relocation section for %s"),
898 stub_sec->name);
899 bfd_set_error (bfd_error_bad_value);
900 return false;
901 }
902
903 outrel.r_offset = (stub_entry->stub_offset
904 + stub_sec->output_offset
905 + stub_sec->output_section->vma);
906 outrel.r_info = ELF32_R_INFO (0, R_PARISC_PCREL21L);
907 outrel.r_addend = sym_value;
908 bfd_elf32_swap_reloca_out (stub_sec->output_section->owner,
909 &outrel,
910 ((Elf32_External_Rela *)
911 srel->contents + srel->reloc_count));
912 ++srel->reloc_count;
913 }
914#endif
915 size = 8;
916 break;
917
918 case hppa_stub_long_branch_shared:
919 /* Branches are relative. This is where we are going to. */
920 sym_value = (stub_entry->target_value
921 + stub_entry->target_section->output_offset
922 + stub_entry->target_section->output_section->vma);
923
924 /* And this is where we are coming from, more or less. */
925 sym_value -= (stub_entry->stub_offset
926 + stub_sec->output_offset
927 + stub_sec->output_section->vma);
928
929 bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
930 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
931 insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
932 bfd_put_32 (stub_bfd, insn, loc + 4);
933
934 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
935 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
936 bfd_put_32 (stub_bfd, insn, loc + 8);
937 size = 12;
938 break;
939
940 case hppa_stub_import:
941 case hppa_stub_import_shared:
942 off = stub_entry->h->elf.plt.offset;
943 if (off >= (bfd_vma) -2)
944 abort ();
945
946 off &= ~ (bfd_vma) 1;
947 sym_value = (off
948 + hplink->splt->output_offset
949 + hplink->splt->output_section->vma
950 - elf_gp (hplink->splt->output_section->owner));
951
952 insn = ADDIL_DP;
953#if R19_STUBS
954 if (stub_entry->stub_type == hppa_stub_import_shared)
955 insn = ADDIL_R19;
956#endif
957 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_lrsel),
958 insn = hppa_rebuild_insn ((int) insn, val, 21);
959 bfd_put_32 (stub_bfd, insn, loc);
960
961 /* It is critical to use lrsel/rrsel here because we are using
962 two different offsets (+0 and +4) from sym_value. If we use
963 lsel/rsel then with unfortunate sym_values we will round
964 sym_value+4 up to the next 2k block leading to a mis-match
965 between the lsel and rsel value. */
966 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_rrsel);
967 insn = hppa_rebuild_insn ((int) LDW_R1_R21, val, 14);
968 bfd_put_32 (stub_bfd, insn, loc + 4);
969
970 if (hplink->multi_subspace)
971 {
972 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
973 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
974 bfd_put_32 (stub_bfd, insn, loc + 8);
975
976 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
977 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
978 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21, loc + 20);
979 bfd_put_32 (stub_bfd, (bfd_vma) STW_RP, loc + 24);
980
981 size = 28;
982 }
983 else
984 {
985 bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 8);
986 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
987 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
988 bfd_put_32 (stub_bfd, insn, loc + 12);
989
990 size = 16;
991 }
992
993 if (!info->shared
994 && stub_entry->h != NULL
995 && stub_entry->h->pic_call)
996 {
997 /* Build the .plt entry needed to call a PIC function from
998 statically linked code. We don't need any relocs. */
999 bfd *dynobj;
1000 struct elf32_hppa_link_hash_entry *eh;
1001 bfd_vma value;
1002
1003 dynobj = hplink->root.dynobj;
1004 eh = (struct elf32_hppa_link_hash_entry *) stub_entry->h;
1005
1006 if (eh->elf.root.type != bfd_link_hash_defined
1007 && eh->elf.root.type != bfd_link_hash_defweak)
1008 abort ();
1009
1010 value = (eh->elf.root.u.def.value
1011 + eh->elf.root.u.def.section->output_offset
1012 + eh->elf.root.u.def.section->output_section->vma);
1013
1014 /* Fill in the entry in the procedure linkage table.
1015
1016 The format of a plt entry is
1017 <funcaddr>
1018 <__gp>. */
1019
1020 bfd_put_32 (hplink->splt->owner, value,
1021 hplink->splt->contents + off);
1022 value = elf_gp (hplink->splt->output_section->owner);
1023 bfd_put_32 (hplink->splt->owner, value,
1024 hplink->splt->contents + off + 4);
1025 }
1026 break;
1027
1028 case hppa_stub_export:
1029 /* Branches are relative. This is where we are going to. */
1030 sym_value = (stub_entry->target_value
1031 + stub_entry->target_section->output_offset
1032 + stub_entry->target_section->output_section->vma);
1033
1034 /* And this is where we are coming from. */
1035 sym_value -= (stub_entry->stub_offset
1036 + stub_sec->output_offset
1037 + stub_sec->output_section->vma);
1038
1039 if (sym_value - 8 + 0x40000 >= 0x80000)
1040 {
1041 (*_bfd_error_handler)
1042 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
1043 bfd_get_filename (stub_entry->target_section->owner),
1044 stub_sec->name,
1045 (long) stub_entry->stub_offset,
1046 stub_entry->root.string);
1047 bfd_set_error (bfd_error_bad_value);
1048 return false;
1049 }
1050
1051 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
1052 insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
1053 bfd_put_32 (stub_bfd, insn, loc);
1054
1055 bfd_put_32 (stub_bfd, (bfd_vma) NOP, loc + 4);
1056 bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP, loc + 8);
1057 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
1058 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
1059 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP, loc + 20);
1060
1061 /* Point the function symbol at the stub. */
1062 stub_entry->h->elf.root.u.def.section = stub_sec;
1063 stub_entry->h->elf.root.u.def.value = stub_sec->_raw_size;
1064
1065 size = 24;
1066 break;
1067
1068 default:
1069 BFD_FAIL ();
1070 return false;
1071 }
1072
1073 stub_sec->_raw_size += size;
1074 return true;
1075}
1076
1077#undef LDIL_R1
1078#undef BE_SR4_R1
1079#undef BL_R1
1080#undef ADDIL_R1
1081#undef DEPI_R1
1082#undef ADDIL_DP
1083#undef LDW_R1_R21
1084#undef LDW_R1_DLT
1085#undef LDW_R1_R19
1086#undef ADDIL_R19
1087#undef LDW_R1_DP
1088#undef LDSID_R21_R1
1089#undef MTSP_R1
1090#undef BE_SR0_R21
1091#undef STW_RP
1092#undef BV_R0_R21
1093#undef BL_RP
1094#undef NOP
1095#undef LDW_RP
1096#undef LDSID_RP_R1
1097#undef BE_SR0_RP
1098
1099/* As above, but don't actually build the stub. Just bump offset so
1100 we know stub section sizes. */
1101
1102static boolean
1103hppa_size_one_stub (gen_entry, in_arg)
1104 struct bfd_hash_entry *gen_entry;
1105 PTR in_arg;
1106{
1107 struct elf32_hppa_stub_hash_entry *stub_entry;
1108 struct elf32_hppa_link_hash_table *hplink;
1109 int size;
1110
1111 /* Massage our args to the form they really have. */
1112 stub_entry = (struct elf32_hppa_stub_hash_entry *) gen_entry;
1113 hplink = (struct elf32_hppa_link_hash_table *) in_arg;
1114
1115 if (stub_entry->stub_type == hppa_stub_long_branch)
1116 {
1117#if ! LONG_BRANCH_PIC_IN_SHLIB
1118 if (stub_entry->reloc_sec != NULL)
1119 stub_entry->reloc_sec->_raw_size += sizeof (Elf32_External_Rela);
1120#endif
1121 size = 8;
1122 }
1123 else if (stub_entry->stub_type == hppa_stub_long_branch_shared)
1124 size = 12;
1125 else if (stub_entry->stub_type == hppa_stub_export)
1126 size = 24;
1127 else /* hppa_stub_import or hppa_stub_import_shared. */
1128 {
1129 if (hplink->multi_subspace)
1130 size = 28;
1131 else
1132 size = 16;
1133 }
1134
1135 stub_entry->stub_sec->_raw_size += size;
1136 return true;
1137}
1138
1139/* Return nonzero if ABFD represents an HPPA ELF32 file.
1140 Additionally we set the default architecture and machine. */
1141
1142static boolean
1143elf32_hppa_object_p (abfd)
1144 bfd *abfd;
1145{
1146 unsigned int flags = elf_elfheader (abfd)->e_flags;
1147
1148 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
1149 {
1150 case EFA_PARISC_1_0:
1151 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
1152 case EFA_PARISC_1_1:
1153 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
1154 case EFA_PARISC_2_0:
1155 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
1156 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
1157 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
1158 }
1159 return true;
1160}
1161
1162/* Undo the generic ELF code's subtraction of section->vma from the
1163 value of each external symbol. */
1164
1165static boolean
1166elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1167 bfd *abfd ATTRIBUTE_UNUSED;
1168 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1169 const Elf_Internal_Sym *sym ATTRIBUTE_UNUSED;
1170 const char **namep ATTRIBUTE_UNUSED;
1171 flagword *flagsp ATTRIBUTE_UNUSED;
1172 asection **secp;
1173 bfd_vma *valp;
1174{
1175 *valp += (*secp)->vma;
1176 return true;
1177}
1178
1179/* Create the .plt and .got sections, and set up our hash table
1180 short-cuts to various dynamic sections. */
1181
1182static boolean
1183elf32_hppa_create_dynamic_sections (abfd, info)
1184 bfd *abfd;
1185 struct bfd_link_info *info;
1186{
1187 struct elf32_hppa_link_hash_table *hplink;
1188
1189 /* Don't try to create the .plt and .got twice. */
1190 hplink = hppa_link_hash_table (info);
1191 if (hplink->splt != NULL)
1192 return true;
1193
1194 /* Call the generic code to do most of the work. */
1195 if (! _bfd_elf_create_dynamic_sections (abfd, info))
1196 return false;
1197
1198 hplink->splt = bfd_get_section_by_name (abfd, ".plt");
1199 hplink->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
1200
1201 hplink->sgot = bfd_get_section_by_name (abfd, ".got");
1202 hplink->srelgot = bfd_make_section (abfd, ".rela.got");
1203 if (hplink->srelgot == NULL
1204 || ! bfd_set_section_flags (abfd, hplink->srelgot,
1205 (SEC_ALLOC
1206 | SEC_LOAD
1207 | SEC_HAS_CONTENTS
1208 | SEC_IN_MEMORY
1209 | SEC_LINKER_CREATED
1210 | SEC_READONLY))
1211 || ! bfd_set_section_alignment (abfd, hplink->srelgot, 2))
1212 return false;
1213
1214 hplink->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
1215 hplink->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
1216
1217 return true;
1218}
1219
1220/* Look through the relocs for a section during the first phase, and
1221 allocate space in the global offset table or procedure linkage
1222 table. At this point we haven't necessarily read all the input
1223 files. */
1224
1225static boolean
1226elf32_hppa_check_relocs (abfd, info, sec, relocs)
1227 bfd *abfd;
1228 struct bfd_link_info *info;
1229 asection *sec;
1230 const Elf_Internal_Rela *relocs;
1231{
1232 bfd *dynobj;
1233 Elf_Internal_Shdr *symtab_hdr;
1234 struct elf_link_hash_entry **sym_hashes;
1235 bfd_signed_vma *local_got_refcounts;
1236 const Elf_Internal_Rela *rel;
1237 const Elf_Internal_Rela *rel_end;
1238 struct elf32_hppa_link_hash_table *hplink;
1239 asection *sreloc;
1240 asection *stubreloc;
1241
1242 if (info->relocateable)
1243 return true;
1244
1245 hplink = hppa_link_hash_table (info);
1246 dynobj = hplink->root.dynobj;
1247 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1248 sym_hashes = elf_sym_hashes (abfd);
1249 local_got_refcounts = elf_local_got_refcounts (abfd);
1250 sreloc = NULL;
1251 stubreloc = NULL;
1252
1253 rel_end = relocs + sec->reloc_count;
1254 for (rel = relocs; rel < rel_end; rel++)
1255 {
1256 enum {
1257 NEED_GOT = 1,
1258 NEED_PLT = 2,
1259 NEED_DYNREL = 4,
1260#if LONG_BRANCH_PIC_IN_SHLIB
1261 NEED_STUBREL = 0, /* We won't be needing them in this case. */
1262#else
1263 NEED_STUBREL = 8,
1264#endif
1265 PLT_PLABEL = 16
1266 };
1267
1268 unsigned int r_symndx, r_type;
1269 struct elf32_hppa_link_hash_entry *h;
1270 int need_entry;
1271
1272 r_symndx = ELF32_R_SYM (rel->r_info);
1273
1274 if (r_symndx < symtab_hdr->sh_info)
1275 h = NULL;
1276 else
1277 h = ((struct elf32_hppa_link_hash_entry *)
1278 sym_hashes[r_symndx - symtab_hdr->sh_info]);
1279
1280 r_type = ELF32_R_TYPE (rel->r_info);
1281
1282 switch (r_type)
1283 {
1284 case R_PARISC_DLTIND14F:
1285 case R_PARISC_DLTIND14R:
1286 case R_PARISC_DLTIND21L:
1287 /* This symbol requires a global offset table entry. */
1288 need_entry = NEED_GOT;
1289
1290 /* Mark this section as containing PIC code. */
1291 sec->flags |= SEC_HAS_GOT_REF;
1292 break;
1293
1294 case R_PARISC_PLABEL14R: /* "Official" procedure labels. */
1295 case R_PARISC_PLABEL21L:
1296 case R_PARISC_PLABEL32:
1297 /* If the addend is non-zero, we break badly. */
1298 if (rel->r_addend != 0)
1299 abort ();
1300
1301 /* If we are creating a shared library, then we need to
1302 create a PLT entry for all PLABELs, because PLABELs with
1303 local symbols may be passed via a pointer to another
1304 object. Additionally, output a dynamic relocation
1305 pointing to the PLT entry. */
1306 need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
1307 break;
1308
1309 case R_PARISC_PCREL12F:
1310 hplink->has_12bit_branch = 1;
1311 /* Fall thru. */
1312 case R_PARISC_PCREL17C:
1313 case R_PARISC_PCREL17F:
1314 hplink->has_17bit_branch = 1;
1315 /* Fall thru. */
1316 case R_PARISC_PCREL22F:
1317 /* Function calls might need to go through the .plt, and
1318 might require long branch stubs. */
1319 if (h == NULL)
1320 {
1321 /* We know local syms won't need a .plt entry, and if
1322 they need a long branch stub we can't guarantee that
1323 we can reach the stub. So just flag an error later
1324 if we're doing a shared link and find we need a long
1325 branch stub. */
1326 continue;
1327 }
1328 else
1329 {
1330 /* Global symbols will need a .plt entry if they remain
1331 global, and in most cases won't need a long branch
1332 stub. Unfortunately, we have to cater for the case
1333 where a symbol is forced local by versioning, or due
1334 to symbolic linking, and we lose the .plt entry. */
1335 need_entry = NEED_PLT | NEED_STUBREL;
1336 }
1337 break;
1338
1339 case R_PARISC_SEGBASE: /* Used to set segment base. */
1340 case R_PARISC_SEGREL32: /* Relative reloc, used for unwind. */
1341 case R_PARISC_PCREL14F: /* PC relative load/store. */
1342 case R_PARISC_PCREL14R:
1343 case R_PARISC_PCREL17R: /* External branches. */
1344 case R_PARISC_PCREL21L: /* As above, and for load/store too. */
1345 /* We don't need to propagate the relocation if linking a
1346 shared object since these are section relative. */
1347 continue;
1348
1349 case R_PARISC_DPREL14F: /* Used for gp rel data load/store. */
1350 case R_PARISC_DPREL14R:
1351 case R_PARISC_DPREL21L:
1352 if (info->shared)
1353 {
1354 (*_bfd_error_handler)
1355 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1356 bfd_get_filename (abfd),
1357 elf_hppa_howto_table[r_type].name);
1358 bfd_set_error (bfd_error_bad_value);
1359 return false;
1360 }
1361 /* Fall through. */
1362
1363 case R_PARISC_DIR17F: /* Used for external branches. */
1364 case R_PARISC_DIR17R:
1365 case R_PARISC_DIR14F: /* Used for load/store from absolute locn. */
1366 case R_PARISC_DIR14R:
1367 case R_PARISC_DIR21L: /* As above, and for ext branches too. */
1368#if 1
1369 /* Help debug shared library creation. Any of the above
1370 relocs can be used in shared libs, but they may cause
1371 pages to become unshared. */
1372 if (info->shared)
1373 {
1374 (*_bfd_error_handler)
1375 (_("%s: relocation %s should not be used when making a shared object; recompile with -fPIC"),
1376 bfd_get_filename (abfd),
1377 elf_hppa_howto_table[r_type].name);
1378 }
1379 /* Fall through. */
1380#endif
1381
1382 case R_PARISC_DIR32: /* .word relocs. */
1383 /* We may want to output a dynamic relocation later. */
1384 need_entry = NEED_DYNREL;
1385 break;
1386
1387 /* This relocation describes the C++ object vtable hierarchy.
1388 Reconstruct it for later use during GC. */
1389 case R_PARISC_GNU_VTINHERIT:
1390 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec,
1391 &h->elf, rel->r_offset))
1392 return false;
1393 continue;
1394
1395 /* This relocation describes which C++ vtable entries are actually
1396 used. Record for later use during GC. */
1397 case R_PARISC_GNU_VTENTRY:
1398 if (!_bfd_elf32_gc_record_vtentry (abfd, sec,
1399 &h->elf, rel->r_addend))
1400 return false;
1401 continue;
1402
1403 default:
1404 continue;
1405 }
1406
1407 /* Now carry out our orders. */
1408 if (need_entry & NEED_GOT)
1409 {
1410 /* Allocate space for a GOT entry, as well as a dynamic
1411 relocation for this entry. */
1412 if (dynobj == NULL)
1413 hplink->root.dynobj = dynobj = abfd;
1414
1415 if (hplink->sgot == NULL)
1416 {
1417 if (! elf32_hppa_create_dynamic_sections (dynobj, info))
1418 return false;
1419 }
1420
1421 if (h != NULL)
1422 {
1423 if (h->elf.got.refcount == -1)
1424 {
1425 h->elf.got.refcount = 1;
1426
1427 /* Make sure this symbol is output as a dynamic symbol. */
1428 if (h->elf.dynindx == -1)
1429 {
1430 if (! bfd_elf32_link_record_dynamic_symbol (info,
1431 &h->elf))
1432 return false;
1433 }
1434
1435 hplink->sgot->_raw_size += GOT_ENTRY_SIZE;
1436 hplink->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1437 }
1438 else
1439 h->elf.got.refcount += 1;
1440 }
1441 else
1442 {
1443 /* This is a global offset table entry for a local symbol. */
1444 if (local_got_refcounts == NULL)
1445 {
1446 size_t size;
1447
1448 /* Allocate space for local got offsets and local
1449 plt offsets. Done this way to save polluting
1450 elf_obj_tdata with another target specific
1451 pointer. */
1452 size = symtab_hdr->sh_info * 2 * sizeof (bfd_signed_vma);
1453 local_got_refcounts = ((bfd_signed_vma *)
1454 bfd_alloc (abfd, size));
1455 if (local_got_refcounts == NULL)
1456 return false;
1457 elf_local_got_refcounts (abfd) = local_got_refcounts;
1458 memset (local_got_refcounts, -1, size);
1459 }
1460 if (local_got_refcounts[r_symndx] == -1)
1461 {
1462 local_got_refcounts[r_symndx] = 1;
1463
1464 hplink->sgot->_raw_size += GOT_ENTRY_SIZE;
1465 if (info->shared)
1466 {
1467 /* If we are generating a shared object, we need to
1468 output a reloc so that the dynamic linker can
1469 adjust this GOT entry (because the address
1470 the shared library is loaded at is not fixed). */
1471 hplink->srelgot->_raw_size +=
1472 sizeof (Elf32_External_Rela);
1473 }
1474 }
1475 else
1476 local_got_refcounts[r_symndx] += 1;
1477 }
1478 }
1479
1480 if (need_entry & NEED_PLT)
1481 {
1482 /* If we are creating a shared library, and this is a reloc
1483 against a weak symbol or a global symbol in a dynamic
1484 object, then we will be creating an import stub and a
1485 .plt entry for the symbol. Similarly, on a normal link
1486 to symbols defined in a dynamic object we'll need the
1487 import stub and a .plt entry. We don't know yet whether
1488 the symbol is defined or not, so make an entry anyway and
1489 clean up later in adjust_dynamic_symbol. */
1490 if ((sec->flags & SEC_ALLOC) != 0)
1491 {
1492 if (h != NULL)
1493 {
1494 if (h->elf.plt.refcount == -1)
1495 {
1496 h->elf.plt.refcount = 1;
1497 h->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1498 }
1499 else
1500 h->elf.plt.refcount += 1;
1501
1502 /* If this .plt entry is for a plabel, mark it so
1503 that adjust_dynamic_symbol will keep the entry
1504 even if it appears to be local. */
1505 if (need_entry & PLT_PLABEL)
1506 h->plabel = 1;
1507 }
1508 else if (need_entry & PLT_PLABEL)
1509 {
1510 int indx;
1511
1512 if (local_got_refcounts == NULL)
1513 {
1514 size_t size;
1515
1516 /* Allocate space for local got offsets and local
1517 plt offsets. */
1518 size = symtab_hdr->sh_info * 2 * sizeof (bfd_signed_vma);
1519 local_got_refcounts = ((bfd_signed_vma *)
1520 bfd_alloc (abfd, size));
1521 if (local_got_refcounts == NULL)
1522 return false;
1523 elf_local_got_refcounts (abfd) = local_got_refcounts;
1524 memset (local_got_refcounts, -1, size);
1525 }
1526 indx = r_symndx + symtab_hdr->sh_info;
1527 if (local_got_refcounts[indx] == -1)
1528 local_got_refcounts[indx] = 1;
1529 else
1530 local_got_refcounts[indx] += 1;
1531 }
1532 }
1533 }
1534
1535 if (need_entry & (NEED_DYNREL | NEED_STUBREL))
1536 {
1537 /* Flag this symbol as having a non-got, non-plt reference
1538 so that we generate copy relocs if it turns out to be
1539 dynamic. */
1540 if (h != NULL)
1541 h->elf.elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1542
1543 /* If we are creating a shared library then we need to copy
1544 the reloc into the shared library. However, if we are
1545 linking with -Bsymbolic, we need only copy absolute
1546 relocs or relocs against symbols that are not defined in
1547 an object we are including in the link. PC- or DP- or
1548 DLT-relative relocs against any local sym or global sym
1549 with DEF_REGULAR set, can be discarded. At this point we
1550 have not seen all the input files, so it is possible that
1551 DEF_REGULAR is not set now but will be set later (it is
1552 never cleared). We account for that possibility below by
1553 storing information in the reloc_entries field of the
1554 hash table entry.
1555
1556 A similar situation to the -Bsymbolic case occurs when
1557 creating shared libraries and symbol visibility changes
1558 render the symbol local.
1559
1560 As it turns out, all the relocs we will be creating here
1561 are absolute, so we cannot remove them on -Bsymbolic
1562 links or visibility changes anyway. A STUB_REL reloc
1563 is absolute too, as in that case it is the reloc in the
1564 stub we will be creating, rather than copying the PCREL
1565 reloc in the branch. */
1566 if ((sec->flags & SEC_ALLOC) != 0
1567 && info->shared
1568#if RELATIVE_DYNAMIC_RELOCS
1569 && (!info->symbolic
1570 || is_absolute_reloc (r_type)
1571 || (h != NULL
1572 && ((h->elf.elf_link_hash_flags
1573 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1574#endif
1575 )
1576 {
1577 boolean doit;
1578 asection *srel;
1579
1580 srel = sreloc;
1581 if ((need_entry & NEED_STUBREL))
1582 srel = stubreloc;
1583
1584 /* Create a reloc section in dynobj and make room for
1585 this reloc. */
1586 if (srel == NULL)
1587 {
1588 char *name;
1589
1590 if (dynobj == NULL)
1591 hplink->root.dynobj = dynobj = abfd;
1592
1593 name = bfd_elf_string_from_elf_section
1594 (abfd,
1595 elf_elfheader (abfd)->e_shstrndx,
1596 elf_section_data (sec)->rel_hdr.sh_name);
1597 if (name == NULL)
1598 {
1599 (*_bfd_error_handler)
1600 (_("Could not find relocation section for %s"),
1601 sec->name);
1602 bfd_set_error (bfd_error_bad_value);
1603 return false;
1604 }
1605
1606 if ((need_entry & NEED_STUBREL))
1607 {
1608 size_t len = strlen (name) + sizeof (STUB_SUFFIX);
1609 char *newname = bfd_malloc (len);
1610
1611 if (newname == NULL)
1612 return false;
1613 strcpy (newname, name);
1614 strcpy (newname + len - sizeof (STUB_SUFFIX),
1615 STUB_SUFFIX);
1616 name = newname;
1617 }
1618
1619 srel = bfd_get_section_by_name (dynobj, name);
1620 if (srel == NULL)
1621 {
1622 flagword flags;
1623
1624 srel = bfd_make_section (dynobj, name);
1625 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1626 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1627 if ((sec->flags & SEC_ALLOC) != 0)
1628 flags |= SEC_ALLOC | SEC_LOAD;
1629 if (srel == NULL
1630 || !bfd_set_section_flags (dynobj, srel, flags)
1631 || !bfd_set_section_alignment (dynobj, srel, 2))
1632 return false;
1633 }
1634 else if ((need_entry & NEED_STUBREL))
1635 free (name);
1636
1637 if ((need_entry & NEED_STUBREL))
1638 stubreloc = srel;
1639 else
1640 sreloc = srel;
1641 }
1642
1643#if ! LONG_BRANCH_PIC_IN_SHLIB
1644 /* If this is a function call, we only need one dynamic
1645 reloc for the stub as all calls to a particular
1646 function will go through the same stub. Actually, a
1647 long branch stub needs two relocations, but we count
1648 on some intelligence on the part of the dynamic
1649 linker. */
1650 if ((need_entry & NEED_STUBREL))
1651 {
1652 doit = h->stub_reloc_sec != stubreloc;
1653 h->stub_reloc_sec = stubreloc;
1654 }
1655 else
1656#endif
1657 doit = 1;
1658
1659 if (doit)
1660 {
1661 srel->_raw_size += sizeof (Elf32_External_Rela);
1662
1663#if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
1664 /* Keep track of relocations we have entered for
1665 this global symbol, so that we can discard them
1666 later if necessary. */
1667 if (h != NULL
1668 && (0
1669#if RELATIVE_DYNAMIC_RELOCS
1670 || ! is_absolute_reloc (rtype)
1671#endif
1672 || (need_entry & NEED_STUBREL)))
1673 {
1674 struct elf32_hppa_dyn_reloc_entry *p;
1675
1676 for (p = h->reloc_entries; p != NULL; p = p->next)
1677 if (p->section == srel)
1678 break;
1679
1680 if (p == NULL)
1681 {
1682 p = ((struct elf32_hppa_dyn_reloc_entry *)
1683 bfd_alloc (dynobj, sizeof *p));
1684 if (p == NULL)
1685 return false;
1686 p->next = h->reloc_entries;
1687 h->reloc_entries = p;
1688 p->section = srel;
1689 p->count = 0;
1690 }
1691
1692 /* NEED_STUBREL and NEED_DYNREL are never both
1693 set. Leave the count at zero for the
1694 NEED_STUBREL case as we only ever have one
1695 stub reloc per section per symbol, and this
1696 simplifies code in hppa_discard_copies. */
1697 if (! (need_entry & NEED_STUBREL))
1698 ++p->count;
1699 }
1700#endif
1701 }
1702 }
1703 }
1704 }
1705
1706 return true;
1707}
1708
1709/* Return the section that should be marked against garbage collection
1710 for a given relocation. */
1711
1712static asection *
1713elf32_hppa_gc_mark_hook (abfd, info, rel, h, sym)
1714 bfd *abfd;
1715 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1716 Elf_Internal_Rela *rel;
1717 struct elf_link_hash_entry *h;
1718 Elf_Internal_Sym *sym;
1719{
1720 if (h != NULL)
1721 {
1722 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1723 {
1724 case R_PARISC_GNU_VTINHERIT:
1725 case R_PARISC_GNU_VTENTRY:
1726 break;
1727
1728 default:
1729 switch (h->root.type)
1730 {
1731 case bfd_link_hash_defined:
1732 case bfd_link_hash_defweak:
1733 return h->root.u.def.section;
1734
1735 case bfd_link_hash_common:
1736 return h->root.u.c.p->section;
1737
1738 default:
1739 break;
1740 }
1741 }
1742 }
1743 else
1744 {
1745 if (!(elf_bad_symtab (abfd)
1746 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1747 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1748 && sym->st_shndx != SHN_COMMON))
1749 {
1750 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1751 }
1752 }
1753
1754 return NULL;
1755}
1756
1757/* Update the got and plt entry reference counts for the section being
1758 removed. */
1759
1760static boolean
1761elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
1762 bfd *abfd;
1763 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1764 asection *sec;
1765 const Elf_Internal_Rela *relocs;
1766{
1767 Elf_Internal_Shdr *symtab_hdr;
1768 struct elf_link_hash_entry **sym_hashes;
1769 bfd_signed_vma *local_got_refcounts;
1770 bfd_signed_vma *local_plt_refcounts;
1771 const Elf_Internal_Rela *rel, *relend;
1772 unsigned long r_symndx;
1773 struct elf_link_hash_entry *h;
1774 struct elf32_hppa_link_hash_table *hplink;
1775 bfd *dynobj;
1776 asection *sgot;
1777 asection *srelgot;
1778
1779 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1780 sym_hashes = elf_sym_hashes (abfd);
1781 local_got_refcounts = elf_local_got_refcounts (abfd);
1782 local_plt_refcounts = local_got_refcounts;
1783 if (local_plt_refcounts != NULL)
1784 local_plt_refcounts += symtab_hdr->sh_info;
1785 hplink = hppa_link_hash_table (info);
1786 dynobj = hplink->root.dynobj;
1787 if (dynobj == NULL)
1788 return true;
1789
1790 sgot = hplink->sgot;
1791 srelgot = hplink->srelgot;
1792
1793 relend = relocs + sec->reloc_count;
1794 for (rel = relocs; rel < relend; rel++)
1795 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1796 {
1797 case R_PARISC_DLTIND14F:
1798 case R_PARISC_DLTIND14R:
1799 case R_PARISC_DLTIND21L:
1800 r_symndx = ELF32_R_SYM (rel->r_info);
1801 if (r_symndx >= symtab_hdr->sh_info)
1802 {
1803 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1804 if (h->got.refcount > 0)
1805 {
1806 h->got.refcount -= 1;
1807 if (h->got.refcount == 0)
1808 {
1809 sgot->_raw_size -= GOT_ENTRY_SIZE;
1810 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1811 }
1812 }
1813 }
1814 else if (local_got_refcounts != NULL)
1815 {
1816 if (local_got_refcounts[r_symndx] > 0)
1817 {
1818 local_got_refcounts[r_symndx] -= 1;
1819 if (local_got_refcounts[r_symndx] == 0)
1820 {
1821 sgot->_raw_size -= GOT_ENTRY_SIZE;
1822 if (info->shared)
1823 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1824 }
1825 }
1826 }
1827 break;
1828
1829 case R_PARISC_PCREL12F:
1830 case R_PARISC_PCREL17C:
1831 case R_PARISC_PCREL17F:
1832 case R_PARISC_PCREL22F:
1833 r_symndx = ELF32_R_SYM (rel->r_info);
1834 if (r_symndx >= symtab_hdr->sh_info)
1835 {
1836 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1837 if (h->plt.refcount > 0)
1838 h->plt.refcount -= 1;
1839 }
1840 break;
1841
1842 case R_PARISC_PLABEL14R:
1843 case R_PARISC_PLABEL21L:
1844 case R_PARISC_PLABEL32:
1845 r_symndx = ELF32_R_SYM (rel->r_info);
1846 if (r_symndx >= symtab_hdr->sh_info)
1847 {
1848 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1849 if (h->plt.refcount > 0)
1850 h->plt.refcount -= 1;
1851 }
1852 else if (local_plt_refcounts != NULL)
1853 {
1854 if (local_plt_refcounts[r_symndx] > 0)
1855 local_plt_refcounts[r_symndx] -= 1;
1856 }
1857 break;
1858
1859 default:
1860 break;
1861 }
1862
1863 return true;
1864}
1865
1866/* Our own version of hide_symbol, so that we can keep plt entries for
1867 plabels. */
1868
1869static void
1870elf32_hppa_hide_symbol (info, h)
1871 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1872 struct elf_link_hash_entry *h;
1873{
1874 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1875 h->dynindx = -1;
1876 if (! ((struct elf32_hppa_link_hash_entry *) h)->plabel)
1877 {
1878 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1879 h->plt.offset = (bfd_vma) -1;
1880 }
1881}
1882
1883/* Adjust a symbol defined by a dynamic object and referenced by a
1884 regular object. The current definition is in some section of the
1885 dynamic object, but we're not including those sections. We have to
1886 change the definition to something the rest of the link can
1887 understand. */
1888
1889static boolean
1890elf32_hppa_adjust_dynamic_symbol (info, h)
1891 struct bfd_link_info *info;
1892 struct elf_link_hash_entry *h;
1893{
1894 bfd *dynobj;
1895 struct elf32_hppa_link_hash_table *hplink;
1896 asection *s;
1897
1898 hplink = hppa_link_hash_table (info);
1899 dynobj = hplink->root.dynobj;
1900
1901 /* If this is a function, put it in the procedure linkage table. We
1902 will fill in the contents of the procedure linkage table later,
1903 when we know the address of the .got section. */
1904 if (h->type == STT_FUNC
1905 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1906 {
1907 if (!info->shared
1908 && h->plt.refcount > 0
1909 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1910 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0)
1911 {
1912 ((struct elf32_hppa_link_hash_entry *) h)->maybe_pic_call = 1;
1913 }
1914
1915 if (h->plt.refcount <= 0
1916 || ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1917 && h->root.type != bfd_link_hash_defweak
1918 && ! ((struct elf32_hppa_link_hash_entry *) h)->plabel
1919 && (!info->shared || info->symbolic)))
1920 {
1921 /* The .plt entry is not needed when:
1922 a) Garbage collection has removed all references to the
1923 symbol, or
1924 b) We know for certain the symbol is defined in this
1925 object, and it's not a weak definition, nor is the symbol
1926 used by a plabel relocation. Either this object is the
1927 application or we are doing a shared symbolic link. */
1928
1929 /* As a special sop to the hppa ABI, we keep a .plt entry
1930 for functions in sections containing PIC code. */
1931 if (((struct elf32_hppa_link_hash_entry *) h)->maybe_pic_call)
1932 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
1933 else
1934 {
1935 h->plt.offset = (bfd_vma) -1;
1936 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1937 return true;
1938 }
1939 }
1940
1941 /* Make an entry in the .plt section. */
1942 s = hplink->splt;
1943 h->plt.offset = s->_raw_size;
1944 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
1945 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
1946 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1947 {
1948 /* Add some extra space for the dynamic linker to use. */
1949 s->_raw_size += PLABEL_PLT_ENTRY_SIZE;
1950 }
1951 else
1952 s->_raw_size += PLT_ENTRY_SIZE;
1953
1954 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
1955 {
1956 /* Make sure this symbol is output as a dynamic symbol. */
1957 if (h->dynindx == -1
1958 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1959 {
1960 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1961 return false;
1962 }
1963
1964 if (h->dynindx != -1 || info->shared)
1965 {
1966 /* We also need to make an entry in the .rela.plt section. */
1967 s = hplink->srelplt;
1968 s->_raw_size += sizeof (Elf32_External_Rela);
1969
1970 hplink->need_plt_stub = 1;
1971 }
1972 }
1973 return true;
1974 }
1975
1976 /* If this is a weak symbol, and there is a real definition, the
1977 processor independent code will have arranged for us to see the
1978 real definition first, and we can just use the same value. */
1979 if (h->weakdef != NULL)
1980 {
1981 if (h->weakdef->root.type != bfd_link_hash_defined
1982 && h->weakdef->root.type != bfd_link_hash_defweak)
1983 abort ();
1984 h->root.u.def.section = h->weakdef->root.u.def.section;
1985 h->root.u.def.value = h->weakdef->root.u.def.value;
1986 return true;
1987 }
1988
1989 /* This is a reference to a symbol defined by a dynamic object which
1990 is not a function. */
1991
1992 /* If we are creating a shared library, we must presume that the
1993 only references to the symbol are via the global offset table.
1994 For such cases we need not do anything here; the relocations will
1995 be handled correctly by relocate_section. */
1996 if (info->shared)
1997 return true;
1998
1999 /* If there are no references to this symbol that do not use the
2000 GOT, we don't need to generate a copy reloc. */
2001 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2002 return true;
2003
2004 /* We must allocate the symbol in our .dynbss section, which will
2005 become part of the .bss section of the executable. There will be
2006 an entry for this symbol in the .dynsym section. The dynamic
2007 object will contain position independent code, so all references
2008 from the dynamic object to this symbol will go through the global
2009 offset table. The dynamic linker will use the .dynsym entry to
2010 determine the address it must put in the global offset table, so
2011 both the dynamic object and the regular object will refer to the
2012 same memory location for the variable. */
2013
2014 s = hplink->sdynbss;
2015
2016 /* We must generate a COPY reloc to tell the dynamic linker to
2017 copy the initial value out of the dynamic object and into the
2018 runtime process image. We need to remember the offset into the
2019 .rela.bss section we are going to use. */
2020 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2021 {
2022 asection *srel;
2023
2024 srel = hplink->srelbss;
2025 srel->_raw_size += sizeof (Elf32_External_Rela);
2026 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2027 }
2028
2029 {
2030 /* We need to figure out the alignment required for this symbol. I
2031 have no idea how other ELF linkers handle this. */
2032 unsigned int power_of_two;
2033
2034 power_of_two = bfd_log2 (h->size);
2035 if (power_of_two > 3)
2036 power_of_two = 3;
2037
2038 /* Apply the required alignment. */
2039 s->_raw_size = BFD_ALIGN (s->_raw_size,
2040 (bfd_size_type) (1 << power_of_two));
2041 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2042 {
2043 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2044 return false;
2045 }
2046 }
2047 /* Define the symbol as being at this point in the section. */
2048 h->root.u.def.section = s;
2049 h->root.u.def.value = s->_raw_size;
2050
2051 /* Increment the section size to make room for the symbol. */
2052 s->_raw_size += h->size;
2053
2054 return true;
2055}
2056
2057/* Called via elf_link_hash_traverse to create .plt entries for an
2058 application that uses statically linked PIC functions. Similar to
2059 the first part of elf32_hppa_adjust_dynamic_symbol. */
2060
2061static boolean
2062hppa_handle_PIC_calls (h, inf)
2063 struct elf_link_hash_entry *h;
2064 PTR inf;
2065{
2066 struct bfd_link_info *info;
2067 bfd *dynobj;
2068 struct elf32_hppa_link_hash_table *hplink;
2069 asection *s;
2070
2071 if (! (h->plt.refcount > 0
2072 && (h->root.type == bfd_link_hash_defined
2073 || h->root.type == bfd_link_hash_defweak)
2074 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0))
2075 {
2076 h->plt.offset = (bfd_vma) -1;
2077 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2078 return true;
2079 }
2080
2081 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2082 ((struct elf32_hppa_link_hash_entry *) h)->maybe_pic_call = 1;
2083 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
2084
2085 info = (struct bfd_link_info *) inf;
2086 hplink = hppa_link_hash_table (info);
2087 dynobj = hplink->root.dynobj;
2088
2089 /* Make an entry in the .plt section. */
2090 s = hplink->splt;
2091 h->plt.offset = s->_raw_size;
2092 s->_raw_size += PLT_ENTRY_SIZE;
2093
2094 return true;
2095}
2096
2097#if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
2098 || RELATIVE_DYNAMIC_RELOCS)
2099/* This function is called via elf_link_hash_traverse to discard space
2100 we allocated for relocs that it turned out we didn't need. */
2101
2102static boolean
2103hppa_discard_copies (h, inf)
2104 struct elf_link_hash_entry *h;
2105 PTR inf;
2106{
2107 struct elf32_hppa_dyn_reloc_entry *s;
2108 struct elf32_hppa_link_hash_entry *eh;
2109 struct bfd_link_info *info;
2110
2111 eh = (struct elf32_hppa_link_hash_entry *) h;
2112 info = (struct bfd_link_info *) inf;
2113
2114#if ! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT
2115 /* Handle the stub reloc case. If we have a plt entry for the
2116 function, we won't be needing long branch stubs. s->count will
2117 only be zero for stub relocs, which provides a handy way of
2118 flagging these relocs, and means we need do nothing special for
2119 the forced local and symbolic link case. */
2120 if (eh->stub_reloc_sec != NULL
2121 && eh->elf.plt.offset != (bfd_vma) -1)
2122 {
2123 for (s = eh->reloc_entries; s != NULL; s = s->next)
2124 if (s->count == 0)
2125 s->section->_raw_size -= sizeof (Elf32_External_Rela);
2126 }
2127#endif
2128
2129#if RELATIVE_DYNAMIC_RELOCS
2130 /* If a symbol has been forced local or we have found a regular
2131 definition for the symbolic link case, then we won't be needing
2132 any relocs. */
2133 if (eh->elf.dynindx == -1
2134 || ((eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2135 && !is_absolute_reloc (r_type)
2136 && info->symbolic))
2137 {
2138 for (s = eh->reloc_entries; s != NULL; s = s->next)
2139 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2140 }
2141#endif
2142
2143 return true;
2144}
2145#endif
2146
2147/* This function is called via elf_link_hash_traverse to force
2148 millicode symbols local so they do not end up as globals in the
2149 dynamic symbol table. We ought to be able to do this in
2150 adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2151 for all dynamic symbols. Arguably, this is a bug in
2152 elf_adjust_dynamic_symbol. */
2153
2154static boolean
2155clobber_millicode_symbols (h, info)
2156 struct elf_link_hash_entry *h;
2157 struct bfd_link_info *info;
2158{
2159 /* We only want to remove these from the dynamic symbol table.
2160 Therefore we do not leave ELF_LINK_FORCED_LOCAL set. */
2161 if (h->type == STT_PARISC_MILLI)
2162 {
2163 unsigned short oldflags = h->elf_link_hash_flags;
2164 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
2165 elf32_hppa_hide_symbol (info, h);
2166 h->elf_link_hash_flags &= ~ELF_LINK_FORCED_LOCAL;
2167 h->elf_link_hash_flags |= oldflags & ELF_LINK_FORCED_LOCAL;
2168 }
2169 return true;
2170}
2171
2172/* Set the sizes of the dynamic sections. */
2173
2174static boolean
2175elf32_hppa_size_dynamic_sections (output_bfd, info)
2176 bfd *output_bfd;
2177 struct bfd_link_info *info;
2178{
2179 struct elf32_hppa_link_hash_table *hplink;
2180 bfd *dynobj;
2181 asection *s;
2182 boolean relocs;
2183 boolean reltext;
2184
2185 hplink = hppa_link_hash_table (info);
2186 dynobj = hplink->root.dynobj;
2187 if (dynobj == NULL)
2188 abort ();
2189
2190 if (hplink->root.dynamic_sections_created)
2191 {
2192 bfd *i;
2193
2194 /* Set the contents of the .interp section to the interpreter. */
2195 if (! info->shared)
2196 {
2197 s = bfd_get_section_by_name (dynobj, ".interp");
2198 if (s == NULL)
2199 abort ();
2200 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2201 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2202 }
2203
2204 /* Force millicode symbols local. */
2205 elf_link_hash_traverse (&hplink->root,
2206 clobber_millicode_symbols,
2207 info);
2208
2209 /* Set up .plt offsets for local plabels. */
2210 for (i = info->input_bfds; i; i = i->link_next)
2211 {
2212 bfd_signed_vma *local_plt;
2213 bfd_signed_vma *end_local_plt;
2214 bfd_size_type locsymcount;
2215 Elf_Internal_Shdr *symtab_hdr;
2216
2217 local_plt = elf_local_got_refcounts (i);
2218 if (!local_plt)
2219 continue;
2220
2221 symtab_hdr = &elf_tdata (i)->symtab_hdr;
2222 locsymcount = symtab_hdr->sh_info;
2223 local_plt += locsymcount;
2224 end_local_plt = local_plt + locsymcount;
2225
2226 for (; local_plt < end_local_plt; ++local_plt)
2227 {
2228 if (*local_plt > 0)
2229 {
2230 s = hplink->splt;
2231 *local_plt = s->_raw_size;
2232 s->_raw_size += PLT_ENTRY_SIZE;
2233 if (info->shared)
2234 hplink->srelplt->_raw_size += sizeof (Elf32_External_Rela);
2235 }
2236 else
2237 *local_plt = (bfd_vma) -1;
2238 }
2239 }
2240 }
2241 else
2242 {
2243 /* Run through the function symbols, looking for any that are
2244 PIC, and allocate space for the necessary .plt entries so
2245 that %r19 will be set up. */
2246 if (! info->shared)
2247 elf_link_hash_traverse (&hplink->root,
2248 hppa_handle_PIC_calls,
2249 info);
2250
2251 /* We may have created entries in the .rela.got section.
2252 However, if we are not creating the dynamic sections, we will
2253 not actually use these entries. Reset the size of .rela.got,
2254 which will cause it to get stripped from the output file
2255 below. */
2256 hplink->srelgot->_raw_size = 0;
2257 }
2258
2259#if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
2260 || RELATIVE_DYNAMIC_RELOCS)
2261 /* If this is a -Bsymbolic shared link, then we need to discard all
2262 relocs against symbols defined in a regular object. We also need
2263 to lose relocs we've allocated for long branch stubs if we know
2264 we won't be generating a stub. */
2265 if (info->shared)
2266 elf_link_hash_traverse (&hplink->root,
2267 hppa_discard_copies,
2268 info);
2269#endif
2270
2271 /* The check_relocs and adjust_dynamic_symbol entry points have
2272 determined the sizes of the various dynamic sections. Allocate
2273 memory for them. */
2274 relocs = false;
2275 reltext = false;
2276 for (s = dynobj->sections; s != NULL; s = s->next)
2277 {
2278 const char *name;
2279
2280 if ((s->flags & SEC_LINKER_CREATED) == 0)
2281 continue;
2282
2283 /* It's OK to base decisions on the section name, because none
2284 of the dynobj section names depend upon the input files. */
2285 name = bfd_get_section_name (dynobj, s);
2286
2287 if (strncmp (name, ".rela", 5) == 0)
2288 {
2289 if (s->_raw_size != 0)
2290 {
2291 asection *target;
2292 const char *outname;
2293
2294 /* Remember whether there are any reloc sections other
2295 than .rela.plt. */
2296 if (strcmp (name+5, ".plt") != 0)
2297 relocs = true;
2298
2299 /* If this relocation section applies to a read only
2300 section, then we probably need a DT_TEXTREL entry. */
2301 outname = bfd_get_section_name (output_bfd,
2302 s->output_section);
2303 target = bfd_get_section_by_name (output_bfd, outname + 5);
2304 if (target != NULL
2305 && (target->flags & SEC_READONLY) != 0
2306 && (target->flags & SEC_ALLOC) != 0)
2307 reltext = true;
2308
2309 /* We use the reloc_count field as a counter if we need
2310 to copy relocs into the output file. */
2311 s->reloc_count = 0;
2312 }
2313 }
2314 else if (strcmp (name, ".plt") == 0)
2315 {
2316 if (hplink->need_plt_stub)
2317 {
2318 /* Make space for the plt stub at the end of the .plt
2319 section. We want this stub right at the end, up
2320 against the .got section. */
2321 int gotalign = bfd_section_alignment (dynobj, hplink->sgot);
2322 int pltalign = bfd_section_alignment (dynobj, s);
2323 bfd_size_type mask;
2324
2325 if (gotalign > pltalign)
2326 bfd_set_section_alignment (dynobj, s, gotalign);
2327 mask = ((bfd_size_type) 1 << gotalign) - 1;
2328 s->_raw_size = (s->_raw_size + sizeof (plt_stub) + mask) & ~mask;
2329 }
2330 }
2331 else if (strcmp (name, ".got") == 0)
2332 ;
2333 else
2334 {
2335 /* It's not one of our sections, so don't allocate space. */
2336 continue;
2337 }
2338
2339 if (s->_raw_size == 0)
2340 {
2341 /* If we don't need this section, strip it from the
2342 output file. This is mostly to handle .rela.bss and
2343 .rela.plt. We must create both sections in
2344 create_dynamic_sections, because they must be created
2345 before the linker maps input sections to output
2346 sections. The linker does that before
2347 adjust_dynamic_symbol is called, and it is that
2348 function which decides whether anything needs to go
2349 into these sections. */
2350 _bfd_strip_section_from_output (info, s);
2351 continue;
2352 }
2353
2354 /* Allocate memory for the section contents. Zero it, because
2355 we may not fill in all the reloc sections. */
2356 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2357 if (s->contents == NULL && s->_raw_size != 0)
2358 return false;
2359 }
2360
2361 if (hplink->root.dynamic_sections_created)
2362 {
2363 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2364 actually has nothing to do with the PLT, it is how we
2365 communicate the LTP value of a load module to the dynamic
2366 linker. */
2367 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
2368 return false;
2369
2370 /* Add some entries to the .dynamic section. We fill in the
2371 values later, in elf32_hppa_finish_dynamic_sections, but we
2372 must add the entries now so that we get the correct size for
2373 the .dynamic section. The DT_DEBUG entry is filled in by the
2374 dynamic linker and used by the debugger. */
2375 if (! info->shared)
2376 {
2377 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2378 return false;
2379 }
2380
2381 if (hplink->srelplt->_raw_size != 0)
2382 {
2383 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2384 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2385 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2386 return false;
2387 }
2388
2389 if (relocs)
2390 {
2391 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2392 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2393 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2394 sizeof (Elf32_External_Rela)))
2395 return false;
2396 }
2397
2398 if (reltext)
2399 {
2400 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2401 return false;
2402 info->flags |= DF_TEXTREL;
2403 }
2404 }
2405
2406 return true;
2407}
2408
2409/* External entry points for sizing and building linker stubs. */
2410
2411/* Determine and set the size of the stub section for a final link.
2412
2413 The basic idea here is to examine all the relocations looking for
2414 PC-relative calls to a target that is unreachable with a "bl"
2415 instruction. */
2416
2417boolean
2418elf32_hppa_size_stubs (output_bfd, stub_bfd, info, multi_subspace, group_size,
2419 add_stub_section, layout_sections_again)
2420 bfd *output_bfd;
2421 bfd *stub_bfd;
2422 struct bfd_link_info *info;
2423 boolean multi_subspace;
2424 bfd_signed_vma group_size;
2425 asection * (*add_stub_section) PARAMS ((const char *, asection *));
2426 void (*layout_sections_again) PARAMS ((void));
2427{
2428 bfd *input_bfd;
2429 asection *section;
2430 asection **input_list, **list;
2431 Elf_Internal_Sym *local_syms, **all_local_syms;
2432 unsigned int bfd_indx, bfd_count;
2433 int top_id, top_index;
2434 struct elf32_hppa_link_hash_table *hplink;
2435 bfd_size_type stub_group_size;
2436 boolean stubs_always_before_branch;
2437 boolean stub_changed = 0;
2438 boolean ret = 0;
2439
2440 hplink = hppa_link_hash_table (info);
2441
2442 /* Stash our params away. */
2443 hplink->stub_bfd = stub_bfd;
2444 hplink->multi_subspace = multi_subspace;
2445 hplink->add_stub_section = add_stub_section;
2446 hplink->layout_sections_again = layout_sections_again;
2447 stubs_always_before_branch = group_size < 0;
2448 if (group_size < 0)
2449 stub_group_size = -group_size;
2450 else
2451 stub_group_size = group_size;
2452 if (stub_group_size == 1)
2453 {
2454 /* Default values. */
2455 stub_group_size = 8000000;
2456 if (hplink->has_17bit_branch || hplink->multi_subspace)
2457 stub_group_size = 250000;
2458 if (hplink->has_12bit_branch)
2459 stub_group_size = 7812;
2460 }
2461
2462 /* Count the number of input BFDs and find the top input section id. */
2463 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2464 input_bfd != NULL;
2465 input_bfd = input_bfd->link_next)
2466 {
2467 bfd_count += 1;
2468 for (section = input_bfd->sections;
2469 section != NULL;
2470 section = section->next)
2471 {
2472 if (top_id < section->id)
2473 top_id = section->id;
2474 }
2475 }
2476
2477 hplink->stub_group
2478 = (struct map_stub *) bfd_zmalloc (sizeof (struct map_stub) * (top_id + 1));
2479 if (hplink->stub_group == NULL)
2480 return false;
2481
2482 /* Make a list of input sections for each output section included in
2483 the link.
2484
2485 We can't use output_bfd->section_count here to find the top output
2486 section index as some sections may have been removed, and
2487 _bfd_strip_section_from_output doesn't renumber the indices. */
2488 for (section = output_bfd->sections, top_index = 0;
2489 section != NULL;
2490 section = section->next)
2491 {
2492 if (top_index < section->index)
2493 top_index = section->index;
2494 }
2495
2496 input_list
2497 = (asection **) bfd_malloc (sizeof (asection *) * (top_index + 1));
2498 if (input_list == NULL)
2499 return false;
2500
2501 /* For sections we aren't interested in, mark their entries with a
2502 value we can check later. */
2503 list = input_list + top_index;
2504 do
2505 *list = bfd_abs_section_ptr;
2506 while (list-- != input_list);
2507
2508 for (section = output_bfd->sections;
2509 section != NULL;
2510 section = section->next)
2511 {
2512 if ((section->flags & SEC_CODE) != 0)
2513 input_list[section->index] = NULL;
2514 }
2515
2516 /* Now actually build the lists. */
2517 for (input_bfd = info->input_bfds;
2518 input_bfd != NULL;
2519 input_bfd = input_bfd->link_next)
2520 {
2521 for (section = input_bfd->sections;
2522 section != NULL;
2523 section = section->next)
2524 {
2525 if (section->output_section != NULL
2526 && section->output_section->owner == output_bfd
2527 && section->output_section->index <= top_index)
2528 {
2529 list = input_list + section->output_section->index;
2530 if (*list != bfd_abs_section_ptr)
2531 {
2532 /* Steal the link_sec pointer for our list. */
2533#define PREV_SEC(sec) (hplink->stub_group[(sec)->id].link_sec)
2534 /* This happens to make the list in reverse order,
2535 which is what we want. */
2536 PREV_SEC (section) = *list;
2537 *list = section;
2538 }
2539 }
2540 }
2541 }
2542
2543 /* See whether we can group stub sections together. Grouping stub
2544 sections may result in fewer stubs. More importantly, we need to
2545 put all .init* and .fini* stubs at the beginning of the .init or
2546 .fini output sections respectively, because glibc splits the
2547 _init and _fini functions into multiple parts. Putting a stub in
2548 the middle of a function is not a good idea. */
2549 list = input_list + top_index;
2550 do
2551 {
2552 asection *tail = *list;
2553 if (tail == bfd_abs_section_ptr)
2554 continue;
2555 while (tail != NULL)
2556 {
2557 asection *curr;
2558 asection *prev;
2559 bfd_size_type total;
2560
2561 curr = tail;
2562 if (tail->_cooked_size)
2563 total = tail->_cooked_size;
2564 else
2565 total = tail->_raw_size;
2566 while ((prev = PREV_SEC (curr)) != NULL
2567 && ((total += curr->output_offset - prev->output_offset)
2568 < stub_group_size))
2569 curr = prev;
2570
2571 /* OK, the size from the start of CURR to the end is less
2572 than 250000 bytes and thus can be handled by one stub
2573 section. (or the tail section is itself larger than
2574 250000 bytes, in which case we may be toast.)
2575 We should really be keeping track of the total size of
2576 stubs added here, as stubs contribute to the final output
2577 section size. That's a little tricky, and this way will
2578 only break if stubs added total more than 12144 bytes, or
2579 1518 long branch stubs. It seems unlikely for more than
2580 1518 different functions to be called, especially from
2581 code only 250000 bytes long. */
2582 do
2583 {
2584 prev = PREV_SEC (tail);
2585 /* Set up this stub group. */
2586 hplink->stub_group[tail->id].link_sec = curr;
2587 }
2588 while (tail != curr && (tail = prev) != NULL);
2589
2590 /* But wait, there's more! Input sections up to 250000
2591 bytes before the stub section can be handled by it too. */
2592 if (!stubs_always_before_branch)
2593 {
2594 total = 0;
2595 while (prev != NULL
2596 && ((total += tail->output_offset - prev->output_offset)
2597 < stub_group_size))
2598 {
2599 tail = prev;
2600 prev = PREV_SEC (tail);
2601 hplink->stub_group[tail->id].link_sec = curr;
2602 }
2603 }
2604 tail = prev;
2605 }
2606 }
2607 while (list-- != input_list);
2608 free (input_list);
2609#undef PREV_SEC
2610
2611 /* We want to read in symbol extension records only once. To do this
2612 we need to read in the local symbols in parallel and save them for
2613 later use; so hold pointers to the local symbols in an array. */
2614 all_local_syms
2615 = (Elf_Internal_Sym **) bfd_zmalloc (sizeof (Elf_Internal_Sym *)
2616 * bfd_count);
2617 if (all_local_syms == NULL)
2618 return false;
2619
2620 /* Walk over all the input BFDs, swapping in local symbols.
2621 If we are creating a shared library, create hash entries for the
2622 export stubs. */
2623 for (input_bfd = info->input_bfds, bfd_indx = 0;
2624 input_bfd != NULL;
2625 input_bfd = input_bfd->link_next, bfd_indx++)
2626 {
2627 Elf_Internal_Shdr *symtab_hdr;
2628 Elf_Internal_Sym *isym;
2629 Elf32_External_Sym *ext_syms, *esym, *end_sy;
2630
2631 /* We'll need the symbol table in a second. */
2632 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2633 if (symtab_hdr->sh_info == 0)
2634 continue;
2635
2636 /* We need an array of the local symbols attached to the input bfd.
2637 Unfortunately, we're going to have to read & swap them in. */
2638 local_syms = (Elf_Internal_Sym *)
2639 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf_Internal_Sym));
2640 if (local_syms == NULL)
2641 {
2642 goto error_ret_free_local;
2643 }
2644 all_local_syms[bfd_indx] = local_syms;
2645 ext_syms = (Elf32_External_Sym *)
2646 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf32_External_Sym));
2647 if (ext_syms == NULL)
2648 {
2649 goto error_ret_free_local;
2650 }
2651
2652 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2653 || (bfd_read (ext_syms, 1,
2654 (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)),
2655 input_bfd)
2656 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2657 {
2658 free (ext_syms);
2659 goto error_ret_free_local;
2660 }
2661
2662 /* Swap the local symbols in. */
2663 isym = local_syms;
2664 esym = ext_syms;
2665 for (end_sy = esym + symtab_hdr->sh_info; esym < end_sy; esym++, isym++)
2666 bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
2667
2668 /* Now we can free the external symbols. */
2669 free (ext_syms);
2670
2671#if ! LONG_BRANCH_PIC_IN_SHLIB
2672 /* If this is a shared link, find all the stub reloc sections. */
2673 if (info->shared)
2674 for (section = input_bfd->sections;
2675 section != NULL;
2676 section = section->next)
2677 {
2678 char *name;
2679 asection *reloc_sec;
2680
2681 name = bfd_malloc (strlen (section->name)
2682 + sizeof STUB_SUFFIX
2683 + 5);
2684 if (name == NULL)
2685 return false;
2686 sprintf (name, ".rela%s%s", section->name, STUB_SUFFIX);
2687 reloc_sec = bfd_get_section_by_name (hplink->root.dynobj, name);
2688 hplink->stub_group[section->id].reloc_sec = reloc_sec;
2689 free (name);
2690 }
2691#endif
2692
2693 if (info->shared && hplink->multi_subspace)
2694 {
2695 struct elf_link_hash_entry **sym_hashes;
2696 struct elf_link_hash_entry **end_hashes;
2697 unsigned int symcount;
2698
2699 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2700 - symtab_hdr->sh_info);
2701 sym_hashes = elf_sym_hashes (input_bfd);
2702 end_hashes = sym_hashes + symcount;
2703
2704 /* Look through the global syms for functions; We need to
2705 build export stubs for all globally visible functions. */
2706 for (; sym_hashes < end_hashes; sym_hashes++)
2707 {
2708 struct elf32_hppa_link_hash_entry *hash;
2709
2710 hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
2711
2712 while (hash->elf.root.type == bfd_link_hash_indirect
2713 || hash->elf.root.type == bfd_link_hash_warning)
2714 hash = ((struct elf32_hppa_link_hash_entry *)
2715 hash->elf.root.u.i.link);
2716
2717 /* At this point in the link, undefined syms have been
2718 resolved, so we need to check that the symbol was
2719 defined in this BFD. */
2720 if ((hash->elf.root.type == bfd_link_hash_defined
2721 || hash->elf.root.type == bfd_link_hash_defweak)
2722 && hash->elf.type == STT_FUNC
2723 && hash->elf.root.u.def.section->output_section != NULL
2724 && (hash->elf.root.u.def.section->output_section->owner
2725 == output_bfd)
2726 && hash->elf.root.u.def.section->owner == input_bfd
2727 && (hash->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
2728 && !(hash->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2729 && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2730 {
2731 asection *sec;
2732 const char *stub_name;
2733 struct elf32_hppa_stub_hash_entry *stub_entry;
2734
2735 sec = hash->elf.root.u.def.section;
2736 stub_name = hash->elf.root.root.string;
2737 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2738 stub_name,
2739 false, false);
2740 if (stub_entry == NULL)
2741 {
2742 stub_entry = hppa_add_stub (stub_name, sec, hplink);
2743 if (!stub_entry)
2744 goto error_ret_free_local;
2745
2746 stub_entry->target_value = hash->elf.root.u.def.value;
2747 stub_entry->target_section = hash->elf.root.u.def.section;
2748 stub_entry->stub_type = hppa_stub_export;
2749 stub_entry->h = hash;
2750 stub_changed = 1;
2751 }
2752 else
2753 {
2754 (*_bfd_error_handler) (_("%s: duplicate export stub %s"),
2755 bfd_get_filename (input_bfd),
2756 stub_name);
2757 }
2758 }
2759 }
2760 }
2761 }
2762
2763 while (1)
2764 {
2765 asection *stub_sec;
2766
2767 for (input_bfd = info->input_bfds, bfd_indx = 0;
2768 input_bfd != NULL;
2769 input_bfd = input_bfd->link_next, bfd_indx++)
2770 {
2771 Elf_Internal_Shdr *symtab_hdr;
2772
2773 /* We'll need the symbol table in a second. */
2774 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2775 if (symtab_hdr->sh_info == 0)
2776 continue;
2777
2778 local_syms = all_local_syms[bfd_indx];
2779
2780 /* Walk over each section attached to the input bfd. */
2781 for (section = input_bfd->sections;
2782 section != NULL;
2783 section = section->next)
2784 {
2785 Elf_Internal_Shdr *input_rel_hdr;
2786 Elf32_External_Rela *external_relocs, *erelaend, *erela;
2787 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2788
2789 /* If there aren't any relocs, then there's nothing more
2790 to do. */
2791 if ((section->flags & SEC_RELOC) == 0
2792 || section->reloc_count == 0)
2793 continue;
2794
2795 /* If this section is a link-once section that will be
2796 discarded, then don't create any stubs. */
2797 if (section->output_section == NULL
2798 || section->output_section->owner != output_bfd)
2799 continue;
2800
2801 /* Allocate space for the external relocations. */
2802 external_relocs
2803 = ((Elf32_External_Rela *)
2804 bfd_malloc (section->reloc_count
2805 * sizeof (Elf32_External_Rela)));
2806 if (external_relocs == NULL)
2807 {
2808 goto error_ret_free_local;
2809 }
2810
2811 /* Likewise for the internal relocations. */
2812 internal_relocs = ((Elf_Internal_Rela *)
2813 bfd_malloc (section->reloc_count
2814 * sizeof (Elf_Internal_Rela)));
2815 if (internal_relocs == NULL)
2816 {
2817 free (external_relocs);
2818 goto error_ret_free_local;
2819 }
2820
2821 /* Read in the external relocs. */
2822 input_rel_hdr = &elf_section_data (section)->rel_hdr;
2823 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
2824 || bfd_read (external_relocs, 1,
2825 input_rel_hdr->sh_size,
2826 input_bfd) != input_rel_hdr->sh_size)
2827 {
2828 free (external_relocs);
2829 error_ret_free_internal:
2830 free (internal_relocs);
2831 goto error_ret_free_local;
2832 }
2833
2834 /* Swap in the relocs. */
2835 erela = external_relocs;
2836 erelaend = erela + section->reloc_count;
2837 irela = internal_relocs;
2838 for (; erela < erelaend; erela++, irela++)
2839 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
2840
2841 /* We're done with the external relocs, free them. */
2842 free (external_relocs);
2843
2844 /* Now examine each relocation. */
2845 irela = internal_relocs;
2846 irelaend = irela + section->reloc_count;
2847 for (; irela < irelaend; irela++)
2848 {
2849 unsigned int r_type, r_indx;
2850 enum elf32_hppa_stub_type stub_type;
2851 struct elf32_hppa_stub_hash_entry *stub_entry;
2852 asection *sym_sec;
2853 bfd_vma sym_value;
2854 bfd_vma destination;
2855 struct elf32_hppa_link_hash_entry *hash;
2856 char *stub_name;
2857 const asection *id_sec;
2858
2859 r_type = ELF32_R_TYPE (irela->r_info);
2860 r_indx = ELF32_R_SYM (irela->r_info);
2861
2862 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2863 {
2864 bfd_set_error (bfd_error_bad_value);
2865 goto error_ret_free_internal;
2866 }
2867
2868 /* Only look for stubs on call instructions. */
2869 if (r_type != (unsigned int) R_PARISC_PCREL12F
2870 && r_type != (unsigned int) R_PARISC_PCREL17F
2871 && r_type != (unsigned int) R_PARISC_PCREL22F)
2872 continue;
2873
2874 /* Now determine the call target, its name, value,
2875 section. */
2876 sym_sec = NULL;
2877 sym_value = 0;
2878 destination = 0;
2879 hash = NULL;
2880 if (r_indx < symtab_hdr->sh_info)
2881 {
2882 /* It's a local symbol. */
2883 Elf_Internal_Sym *sym;
2884 Elf_Internal_Shdr *hdr;
2885
2886 sym = local_syms + r_indx;
2887 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2888 sym_sec = hdr->bfd_section;
2889 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2890 sym_value = sym->st_value;
2891 destination = (sym_value + irela->r_addend
2892 + sym_sec->output_offset
2893 + sym_sec->output_section->vma);
2894 }
2895 else
2896 {
2897 /* It's an external symbol. */
2898 int e_indx;
2899
2900 e_indx = r_indx - symtab_hdr->sh_info;
2901 hash = ((struct elf32_hppa_link_hash_entry *)
2902 elf_sym_hashes (input_bfd)[e_indx]);
2903
2904 while (hash->elf.root.type == bfd_link_hash_indirect
2905 || hash->elf.root.type == bfd_link_hash_warning)
2906 hash = ((struct elf32_hppa_link_hash_entry *)
2907 hash->elf.root.u.i.link);
2908
2909 if (hash->elf.root.type == bfd_link_hash_defined
2910 || hash->elf.root.type == bfd_link_hash_defweak)
2911 {
2912 sym_sec = hash->elf.root.u.def.section;
2913 sym_value = hash->elf.root.u.def.value;
2914 if (sym_sec->output_section != NULL)
2915 destination = (sym_value + irela->r_addend
2916 + sym_sec->output_offset
2917 + sym_sec->output_section->vma);
2918 }
2919 else if (hash->elf.root.type == bfd_link_hash_undefweak)
2920 {
2921 if (! info->shared)
2922 continue;
2923 }
2924 else if (hash->elf.root.type == bfd_link_hash_undefined)
2925 {
2926 if (! (info->shared
2927 && !info->no_undefined
2928 && (ELF_ST_VISIBILITY (hash->elf.other)
2929 == STV_DEFAULT)))
2930 continue;
2931 }
2932 else
2933 {
2934 bfd_set_error (bfd_error_bad_value);
2935 goto error_ret_free_internal;
2936 }
2937 }
2938
2939 /* Determine what (if any) linker stub is needed. */
2940 stub_type = hppa_type_of_stub (section, irela, hash,
2941 destination);
2942 if (stub_type == hppa_stub_none)
2943 continue;
2944
2945 /* Support for grouping stub sections. */
2946 id_sec = hplink->stub_group[section->id].link_sec;
2947
2948 /* Get the name of this stub. */
2949 stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
2950 if (!stub_name)
2951 goto error_ret_free_internal;
2952
2953 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2954 stub_name,
2955 false, false);
2956 if (stub_entry != NULL)
2957 {
2958 /* The proper stub has already been created. */
2959 free (stub_name);
2960 continue;
2961 }
2962
2963 stub_entry = hppa_add_stub (stub_name, section, hplink);
2964 if (stub_entry == NULL)
2965 {
2966 free (stub_name);
2967 goto error_ret_free_local;
2968 }
2969
2970 stub_entry->target_value = sym_value;
2971 stub_entry->target_section = sym_sec;
2972 stub_entry->stub_type = stub_type;
2973 if (info->shared)
2974 {
2975 if (stub_type == hppa_stub_import)
2976 stub_entry->stub_type = hppa_stub_import_shared;
2977 else if (stub_type == hppa_stub_long_branch
2978 && (LONG_BRANCH_PIC_IN_SHLIB || hash == NULL))
2979 stub_entry->stub_type = hppa_stub_long_branch_shared;
2980 }
2981 stub_entry->h = hash;
2982 stub_changed = 1;
2983 }
2984
2985 /* We're done with the internal relocs, free them. */
2986 free (internal_relocs);
2987 }
2988 }
2989
2990 if (!stub_changed)
2991 break;
2992
2993 /* OK, we've added some stubs. Find out the new size of the
2994 stub sections. */
2995 for (stub_sec = hplink->stub_bfd->sections;
2996 stub_sec != NULL;
2997 stub_sec = stub_sec->next)
2998 {
2999 stub_sec->_raw_size = 0;
3000 stub_sec->_cooked_size = 0;
3001 }
3002#if ! LONG_BRANCH_PIC_IN_SHLIB
3003 {
3004 int i;
3005
3006 for (i = top_id; i >= 0; --i)
3007 {
3008 /* This will probably hit the same section many times.. */
3009 stub_sec = hplink->stub_group[i].reloc_sec;
3010 if (stub_sec != NULL)
3011 {
3012 stub_sec->_raw_size = 0;
3013 stub_sec->_cooked_size = 0;
3014 }
3015 }
3016 }
3017#endif
3018
3019 bfd_hash_traverse (&hplink->stub_hash_table,
3020 hppa_size_one_stub,
3021 hplink);
3022
3023 /* Ask the linker to do its stuff. */
3024 (*hplink->layout_sections_again) ();
3025 stub_changed = 0;
3026 }
3027
3028 ret = 1;
3029
3030 error_ret_free_local:
3031 while (bfd_count-- > 0)
3032 if (all_local_syms[bfd_count])
3033 free (all_local_syms[bfd_count]);
3034 free (all_local_syms);
3035
3036 return ret;
3037}
3038
3039/* For a final link, this function is called after we have sized the
3040 stubs to provide a value for __gp. */
3041
3042boolean
3043elf32_hppa_set_gp (abfd, info)
3044 bfd *abfd;
3045 struct bfd_link_info *info;
3046{
3047 struct elf32_hppa_link_hash_table *hplink;
3048 struct elf_link_hash_entry *h;
3049 asection *sec;
3050 bfd_vma gp_val;
3051
3052 hplink = hppa_link_hash_table (info);
3053 h = elf_link_hash_lookup (&hplink->root, "$global$",
3054 false, false, false);
3055
3056 if (h != NULL
3057 && (h->root.type == bfd_link_hash_defined
3058 || h->root.type == bfd_link_hash_defweak))
3059 {
3060 gp_val = h->root.u.def.value;
3061 sec = h->root.u.def.section;
3062 }
3063 else
3064 {
3065 /* Choose to point our LTP at, in this order, one of .plt, .got,
3066 or .data, if these sections exist. In the case of choosing
3067 .plt try to make the LTP ideal for addressing anywhere in the
3068 .plt or .got with a 14 bit signed offset. Typically, the end
3069 of the .plt is the start of the .got, so choose .plt + 0x2000
3070 if either the .plt or .got is larger than 0x2000. If both
3071 the .plt and .got are smaller than 0x2000, choose the end of
3072 the .plt section. */
3073
3074 sec = hplink->splt;
3075 if (sec != NULL)
3076 {
3077 gp_val = sec->_raw_size;
3078 if (gp_val > 0x2000
3079 || (hplink->sgot && hplink->sgot->_raw_size > 0x2000))
3080 {
3081 gp_val = 0x2000;
3082 }
3083 }
3084 else
3085 {
3086 gp_val = 0;
3087 sec = hplink->sgot;
3088 if (sec != NULL)
3089 {
3090 /* We know we don't have a .plt. If .got is large,
3091 offset our LTP. */
3092 if (sec->_raw_size > 0x2000)
3093 gp_val = 0x2000;
3094 }
3095 else
3096 {
3097 /* No .plt or .got. Who cares what the LTP is? */
3098 sec = bfd_get_section_by_name (abfd, ".data");
3099 }
3100 }
3101
3102 if (h != NULL)
3103 {
3104 h->root.type = bfd_link_hash_defined;
3105 h->root.u.def.value = gp_val;
3106 if (sec != NULL)
3107 h->root.u.def.section = sec;
3108 else
3109 h->root.u.def.section = bfd_abs_section_ptr;
3110 }
3111 }
3112
3113 if (sec != NULL && sec->output_section != NULL)
3114 gp_val += sec->output_section->vma + sec->output_offset;
3115
3116 elf_gp (abfd) = gp_val;
3117 return true;
3118}
3119
3120/* Build all the stubs associated with the current output file. The
3121 stubs are kept in a hash table attached to the main linker hash
3122 table. We also set up the .plt entries for statically linked PIC
3123 functions here. This function is called via hppaelf_finish in the
3124 linker. */
3125
3126boolean
3127elf32_hppa_build_stubs (info)
3128 struct bfd_link_info *info;
3129{
3130 asection *stub_sec;
3131 struct bfd_hash_table *table;
3132 struct elf32_hppa_link_hash_table *hplink;
3133
3134 hplink = hppa_link_hash_table (info);
3135
3136 for (stub_sec = hplink->stub_bfd->sections;
3137 stub_sec != NULL;
3138 stub_sec = stub_sec->next)
3139 {
3140 size_t size;
3141
3142 /* Allocate memory to hold the linker stubs. */
3143 size = stub_sec->_raw_size;
3144 stub_sec->contents = (unsigned char *) bfd_zalloc (hplink->stub_bfd,
3145 size);
3146 if (stub_sec->contents == NULL && size != 0)
3147 return false;
3148 stub_sec->_raw_size = 0;
3149 }
3150
3151 /* Build the stubs as directed by the stub hash table. */
3152 table = &hplink->stub_hash_table;
3153 bfd_hash_traverse (table, hppa_build_one_stub, info);
3154
3155 return true;
3156}
3157
3158/* Perform a final link. */
3159
3160static boolean
3161elf32_hppa_final_link (abfd, info)
3162 bfd *abfd;
3163 struct bfd_link_info *info;
3164{
3165 asection *s;
3166
3167 /* Invoke the regular ELF garbage collecting linker to do all the
3168 work. */
3169 if (!_bfd_elf32_gc_common_final_link (abfd, info))
3170 return false;
3171
3172 /* If we're producing a final executable, sort the contents of the
3173 unwind section. Magic section names, but this is much safer than
3174 having elf32_hppa_relocate_section remember where SEGREL32 relocs
3175 occurred. Consider what happens if someone inept creates a
3176 linker script that puts unwind information in .text. */
3177 s = bfd_get_section_by_name (abfd, ".PARISC.unwind");
3178 if (s != NULL)
3179 {
3180 bfd_size_type size;
3181 char *contents;
3182
3183 size = s->_raw_size;
3184 contents = bfd_malloc (size);
3185 if (contents == NULL)
3186 return false;
3187
3188 if (! bfd_get_section_contents (abfd, s, contents, (file_ptr) 0, size))
3189 return false;
3190
3191 qsort (contents, size / 16, 16, hppa_unwind_entry_compare);
3192
3193 if (! bfd_set_section_contents (abfd, s, contents, (file_ptr) 0, size))
3194 return false;
3195 }
3196 return true;
3197}
3198
3199/* Record the lowest address for the data and text segments. */
3200
3201static void
3202hppa_record_segment_addr (abfd, section, data)
3203 bfd *abfd ATTRIBUTE_UNUSED;
3204 asection *section;
3205 PTR data;
3206{
3207 struct elf32_hppa_link_hash_table *hplink;
3208
3209 hplink = (struct elf32_hppa_link_hash_table *) data;
3210
3211 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3212 {
3213 bfd_vma value = section->vma - section->filepos;
3214
3215 if ((section->flags & SEC_READONLY) != 0)
3216 {
3217 if (value < hplink->text_segment_base)
3218 hplink->text_segment_base = value;
3219 }
3220 else
3221 {
3222 if (value < hplink->data_segment_base)
3223 hplink->data_segment_base = value;
3224 }
3225 }
3226}
3227
3228/* Perform a relocation as part of a final link. */
3229
3230static bfd_reloc_status_type
3231final_link_relocate (input_section, contents, rel, value, hplink, sym_sec, h)
3232 asection *input_section;
3233 bfd_byte *contents;
3234 const Elf_Internal_Rela *rel;
3235 bfd_vma value;
3236 struct elf32_hppa_link_hash_table *hplink;
3237 asection *sym_sec;
3238 struct elf32_hppa_link_hash_entry *h;
3239{
3240 int insn;
3241 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3242 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3243 int r_format = howto->bitsize;
3244 enum hppa_reloc_field_selector_type_alt r_field;
3245 bfd *input_bfd = input_section->owner;
3246 bfd_vma offset = rel->r_offset;
3247 bfd_vma max_branch_offset = 0;
3248 bfd_byte *hit_data = contents + offset;
3249 bfd_signed_vma addend = rel->r_addend;
3250 bfd_vma location;
3251 struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
3252 int val;
3253
3254 if (r_type == R_PARISC_NONE)
3255 return bfd_reloc_ok;
3256
3257 insn = bfd_get_32 (input_bfd, hit_data);
3258
3259 /* Find out where we are and where we're going. */
3260 location = (offset +
3261 input_section->output_offset +
3262 input_section->output_section->vma);
3263
3264 switch (r_type)
3265 {
3266 case R_PARISC_PCREL12F:
3267 case R_PARISC_PCREL17F:
3268 case R_PARISC_PCREL22F:
3269 /* If this is a call to a function defined in another dynamic
3270 library, or if it is a call to a PIC function in the same
3271 object, or if this is a shared link and it is a call to a
3272 weak symbol which may or may not be in the same object, then
3273 find the import stub in the stub hash. */
3274 if (sym_sec == NULL
3275 || sym_sec->output_section == NULL
3276 || (h != NULL
3277 && ((h->maybe_pic_call
3278 && !(input_section->flags & SEC_HAS_GOT_REF))
3279 || (h->elf.root.type == bfd_link_hash_defweak
3280 && h->elf.dynindx != -1
3281 && h->elf.plt.offset != (bfd_vma) -1))))
3282 {
3283 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3284 h, rel, hplink);
3285 if (stub_entry != NULL)
3286 {
3287 value = (stub_entry->stub_offset
3288 + stub_entry->stub_sec->output_offset
3289 + stub_entry->stub_sec->output_section->vma);
3290 addend = 0;
3291 }
3292 else if (sym_sec == NULL && h != NULL
3293 && h->elf.root.type == bfd_link_hash_undefweak)
3294 {
3295 /* It's OK if undefined weak. Calls to undefined weak
3296 symbols behave as if the "called" function
3297 immediately returns. We can thus call to a weak
3298 function without first checking whether the function
3299 is defined. */
3300 value = location;
3301 addend = 8;
3302 }
3303 else
3304 return bfd_reloc_notsupported;
3305 }
3306 /* Fall thru. */
3307
3308 case R_PARISC_PCREL21L:
3309 case R_PARISC_PCREL17C:
3310 case R_PARISC_PCREL17R:
3311 case R_PARISC_PCREL14R:
3312 case R_PARISC_PCREL14F:
3313 /* Make it a pc relative offset. */
3314 value -= location;
3315 addend -= 8;
3316 break;
3317
3318 case R_PARISC_DPREL21L:
3319 case R_PARISC_DPREL14R:
3320 case R_PARISC_DPREL14F:
3321 /* For all the DP relative relocations, we need to examine the symbol's
3322 section. If it's a code section, then "data pointer relative" makes
3323 no sense. In that case we don't adjust the "value", and for 21 bit
3324 addil instructions, we change the source addend register from %dp to
3325 %r0. This situation commonly arises when a variable's "constness"
3326 is declared differently from the way the variable is defined. For
3327 instance: "extern int foo" with foo defined as "const int foo". */
3328 if (sym_sec == NULL)
3329 break;
3330 if ((sym_sec->flags & SEC_CODE) != 0)
3331 {
3332 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3333 == (((int) OP_ADDIL << 26) | (27 << 21)))
3334 {
3335 insn &= ~ (0x1f << 21);
3336#if 1 /* debug them. */
3337 (*_bfd_error_handler)
3338 (_("%s(%s+0x%lx): fixing %s"),
3339 bfd_get_filename (input_bfd),
3340 input_section->name,
3341 (long) rel->r_offset,
3342 howto->name);
3343#endif
3344 }
3345 /* Now try to make things easy for the dynamic linker. */
3346
3347 break;
3348 }
3349 /* Fall thru. */
3350
3351 case R_PARISC_DLTIND21L:
3352 case R_PARISC_DLTIND14R:
3353 case R_PARISC_DLTIND14F:
3354 value -= elf_gp (input_section->output_section->owner);
3355 break;
3356
3357 case R_PARISC_SEGREL32:
3358 if ((sym_sec->flags & SEC_CODE) != 0)
3359 value -= hplink->text_segment_base;
3360 else
3361 value -= hplink->data_segment_base;
3362 break;
3363
3364 default:
3365 break;
3366 }
3367
3368 switch (r_type)
3369 {
3370 case R_PARISC_DIR32:
3371 case R_PARISC_DIR14F:
3372 case R_PARISC_DIR17F:
3373 case R_PARISC_PCREL17C:
3374 case R_PARISC_PCREL14F:
3375 case R_PARISC_DPREL14F:
3376 case R_PARISC_PLABEL32:
3377 case R_PARISC_DLTIND14F:
3378 case R_PARISC_SEGBASE:
3379 case R_PARISC_SEGREL32:
3380 r_field = e_fsel;
3381 break;
3382
3383 case R_PARISC_DIR21L:
3384 case R_PARISC_PCREL21L:
3385 case R_PARISC_DPREL21L:
3386 case R_PARISC_PLABEL21L:
3387 case R_PARISC_DLTIND21L:
3388 r_field = e_lrsel;
3389 break;
3390
3391 case R_PARISC_DIR17R:
3392 case R_PARISC_PCREL17R:
3393 case R_PARISC_DIR14R:
3394 case R_PARISC_PCREL14R:
3395 case R_PARISC_DPREL14R:
3396 case R_PARISC_PLABEL14R:
3397 case R_PARISC_DLTIND14R:
3398 r_field = e_rrsel;
3399 break;
3400
3401 case R_PARISC_PCREL12F:
3402 case R_PARISC_PCREL17F:
3403 case R_PARISC_PCREL22F:
3404 r_field = e_fsel;
3405
3406 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3407 {
3408 max_branch_offset = (1 << (17-1)) << 2;
3409 }
3410 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3411 {
3412 max_branch_offset = (1 << (12-1)) << 2;
3413 }
3414 else
3415 {
3416 max_branch_offset = (1 << (22-1)) << 2;
3417 }
3418
3419 /* sym_sec is NULL on undefined weak syms or when shared on
3420 undefined syms. We've already checked for a stub for the
3421 shared undefined case. */
3422 if (sym_sec == NULL)
3423 break;
3424
3425 /* If the branch is out of reach, then redirect the
3426 call to the local stub for this function. */
3427 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3428 {
3429 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3430 h, rel, hplink);
3431 if (stub_entry == NULL)
3432 return bfd_reloc_notsupported;
3433
3434 /* Munge up the value and addend so that we call the stub
3435 rather than the procedure directly. */
3436 value = (stub_entry->stub_offset
3437 + stub_entry->stub_sec->output_offset
3438 + stub_entry->stub_sec->output_section->vma
3439 - location);
3440 addend = -8;
3441 }
3442 break;
3443
3444 /* Something we don't know how to handle. */
3445 default:
3446 return bfd_reloc_notsupported;
3447 }
3448
3449 /* Make sure we can reach the stub. */
3450 if (max_branch_offset != 0
3451 && value + addend + max_branch_offset >= 2*max_branch_offset)
3452 {
3453 (*_bfd_error_handler)
3454 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3455 bfd_get_filename (input_bfd),
3456 input_section->name,
3457 (long) rel->r_offset,
3458 stub_entry->root.string);
3459 return bfd_reloc_notsupported;
3460 }
3461
3462 val = hppa_field_adjust (value, addend, r_field);
3463
3464 switch (r_type)
3465 {
3466 case R_PARISC_PCREL12F:
3467 case R_PARISC_PCREL17C:
3468 case R_PARISC_PCREL17F:
3469 case R_PARISC_PCREL17R:
3470 case R_PARISC_PCREL22F:
3471 case R_PARISC_DIR17F:
3472 case R_PARISC_DIR17R:
3473 /* This is a branch. Divide the offset by four.
3474 Note that we need to decide whether it's a branch or
3475 otherwise by inspecting the reloc. Inspecting insn won't
3476 work as insn might be from a .word directive. */
3477 val >>= 2;
3478 break;
3479
3480 default:
3481 break;
3482 }
3483
3484 insn = hppa_rebuild_insn (insn, val, r_format);
3485
3486 /* Update the instruction word. */
3487 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3488 return bfd_reloc_ok;
3489}
3490
3491/* Relocate an HPPA ELF section. */
3492
3493static boolean
3494elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
3495 contents, relocs, local_syms, local_sections)
3496 bfd *output_bfd;
3497 struct bfd_link_info *info;
3498 bfd *input_bfd;
3499 asection *input_section;
3500 bfd_byte *contents;
3501 Elf_Internal_Rela *relocs;
3502 Elf_Internal_Sym *local_syms;
3503 asection **local_sections;
3504{
3505 bfd *dynobj;
3506 bfd_vma *local_got_offsets;
3507 struct elf32_hppa_link_hash_table *hplink;
3508 Elf_Internal_Shdr *symtab_hdr;
3509 Elf_Internal_Rela *rel;
3510 Elf_Internal_Rela *relend;
3511 asection *sreloc;
3512
3513 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3514
3515 hplink = hppa_link_hash_table (info);
3516 dynobj = hplink->root.dynobj;
3517 local_got_offsets = elf_local_got_offsets (input_bfd);
3518 sreloc = NULL;
3519
3520 rel = relocs;
3521 relend = relocs + input_section->reloc_count;
3522 for (; rel < relend; rel++)
3523 {
3524 unsigned int r_type;
3525 reloc_howto_type *howto;
3526 unsigned int r_symndx;
3527 struct elf32_hppa_link_hash_entry *h;
3528 Elf_Internal_Sym *sym;
3529 asection *sym_sec;
3530 bfd_vma relocation;
3531 bfd_reloc_status_type r;
3532 const char *sym_name;
3533 boolean plabel;
3534
3535 r_type = ELF32_R_TYPE (rel->r_info);
3536 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3537 {
3538 bfd_set_error (bfd_error_bad_value);
3539 return false;
3540 }
3541 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3542 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3543 continue;
3544
3545 r_symndx = ELF32_R_SYM (rel->r_info);
3546
3547 if (info->relocateable)
3548 {
3549 /* This is a relocateable link. We don't have to change
3550 anything, unless the reloc is against a section symbol,
3551 in which case we have to adjust according to where the
3552 section symbol winds up in the output section. */
3553 if (r_symndx < symtab_hdr->sh_info)
3554 {
3555 sym = local_syms + r_symndx;
3556 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3557 {
3558 sym_sec = local_sections[r_symndx];
3559 rel->r_addend += sym_sec->output_offset;
3560 }
3561 }
3562 continue;
3563 }
3564
3565 /* This is a final link. */
3566 h = NULL;
3567 sym = NULL;
3568 sym_sec = NULL;
3569 if (r_symndx < symtab_hdr->sh_info)
3570 {
3571 /* This is a local symbol, h defaults to NULL. */
3572 sym = local_syms + r_symndx;
3573 sym_sec = local_sections[r_symndx];
3574 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
3575 ? 0 : sym->st_value)
3576 + sym_sec->output_offset
3577 + sym_sec->output_section->vma);
3578 }
3579 else
3580 {
3581 int indx;
3582
3583 /* It's a global; Find its entry in the link hash. */
3584 indx = r_symndx - symtab_hdr->sh_info;
3585 h = ((struct elf32_hppa_link_hash_entry *)
3586 elf_sym_hashes (input_bfd)[indx]);
3587 while (h->elf.root.type == bfd_link_hash_indirect
3588 || h->elf.root.type == bfd_link_hash_warning)
3589 h = (struct elf32_hppa_link_hash_entry *) h->elf.root.u.i.link;
3590
3591 relocation = 0;
3592 if (h->elf.root.type == bfd_link_hash_defined
3593 || h->elf.root.type == bfd_link_hash_defweak)
3594 {
3595 sym_sec = h->elf.root.u.def.section;
3596 /* If sym_sec->output_section is NULL, then it's a
3597 symbol defined in a shared library. */
3598 if (sym_sec->output_section != NULL)
3599 relocation = (h->elf.root.u.def.value
3600 + sym_sec->output_offset
3601 + sym_sec->output_section->vma);
3602 }
3603 else if (h->elf.root.type == bfd_link_hash_undefweak)
3604 ;
3605 else if (info->shared && !info->no_undefined
3606 && ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
3607 && h->elf.type != STT_PARISC_MILLI)
3608 {
3609 if (info->symbolic)
3610 if (!((*info->callbacks->undefined_symbol)
3611 (info, h->elf.root.root.string, input_bfd,
3612 input_section, rel->r_offset, false)))
3613 return false;
3614 }
3615 else
3616 {
3617 if (!((*info->callbacks->undefined_symbol)
3618 (info, h->elf.root.root.string, input_bfd,
3619 input_section, rel->r_offset, true)))
3620 return false;
3621 }
3622 }
3623
3624 /* Do any required modifications to the relocation value, and
3625 determine what types of dynamic info we need to output, if
3626 any. */
3627 plabel = 0;
3628 switch (r_type)
3629 {
3630 case R_PARISC_DLTIND14F:
3631 case R_PARISC_DLTIND14R:
3632 case R_PARISC_DLTIND21L:
3633 /* Relocation is to the entry for this symbol in the global
3634 offset table. */
3635 if (h != NULL)
3636 {
3637 bfd_vma off;
3638
3639 off = h->elf.got.offset;
3640 if (off == (bfd_vma) -1)
3641 abort ();
3642
3643 if (! hplink->root.dynamic_sections_created
3644 || (info->shared
3645 && (info->symbolic || h->elf.dynindx == -1)
3646 && (h->elf.elf_link_hash_flags
3647 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3648 {
3649 /* This is actually a static link, or it is a
3650 -Bsymbolic link and the symbol is defined
3651 locally, or the symbol was forced to be local
3652 because of a version file. We must initialize
3653 this entry in the global offset table. Since the
3654 offset must always be a multiple of 4, we use the
3655 least significant bit to record whether we have
3656 initialized it already.
3657
3658 When doing a dynamic link, we create a .rela.got
3659 relocation entry to initialize the value. This
3660 is done in the finish_dynamic_symbol routine. */
3661 if ((off & 1) != 0)
3662 off &= ~1;
3663 else
3664 {
3665 bfd_put_32 (output_bfd, relocation,
3666 hplink->sgot->contents + off);
3667 h->elf.got.offset |= 1;
3668 }
3669 }
3670
3671 relocation = off;
3672 }
3673 else
3674 {
3675 /* Local symbol case. */
3676 bfd_vma off;
3677
3678 if (local_got_offsets == NULL
3679 || (off = local_got_offsets[r_symndx]) == (bfd_vma) -1)
3680 abort ();
3681
3682 /* The offset must always be a multiple of 4. We use
3683 the least significant bit to record whether we have
3684 already generated the necessary reloc. */
3685 if ((off & 1) != 0)
3686 off &= ~1;
3687 else
3688 {
3689 bfd_put_32 (output_bfd, relocation,
3690 hplink->sgot->contents + off);
3691
3692 if (info->shared)
3693 {
3694 /* Output a dynamic relocation for this GOT
3695 entry. In this case it is relative to the
3696 base of the object because the symbol index
3697 is zero. */
3698 Elf_Internal_Rela outrel;
3699 asection *srelgot = hplink->srelgot;
3700
3701 outrel.r_offset = (off
3702 + hplink->sgot->output_offset
3703 + hplink->sgot->output_section->vma);
3704 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3705 outrel.r_addend = relocation;
3706 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3707 ((Elf32_External_Rela *)
3708 srelgot->contents
3709 + srelgot->reloc_count));
3710 ++srelgot->reloc_count;
3711 }
3712
3713 local_got_offsets[r_symndx] |= 1;
3714 }
3715
3716 relocation = off;
3717 }
3718
3719 /* Add the base of the GOT to the relocation value. */
3720 relocation += (hplink->sgot->output_offset
3721 + hplink->sgot->output_section->vma);
3722 break;
3723
3724 case R_PARISC_SEGREL32:
3725 /* If this is the first SEGREL relocation, then initialize
3726 the segment base values. */
3727 if (hplink->text_segment_base == (bfd_vma) -1)
3728 bfd_map_over_sections (output_bfd,
3729 hppa_record_segment_addr,
3730 hplink);
3731 break;
3732
3733 case R_PARISC_PLABEL14R:
3734 case R_PARISC_PLABEL21L:
3735 case R_PARISC_PLABEL32:
3736 if (hplink->root.dynamic_sections_created)
3737 {
3738 bfd_vma off;
3739
3740 /* If we have a global symbol with a PLT slot, then
3741 redirect this relocation to it. */
3742 if (h != NULL)
3743 {
3744 off = h->elf.plt.offset;
3745 if (!info->shared
3746 && (h->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3747 {
3748 /* In a non-shared link, adjust_dynamic_symbols
3749 isn't called for symbols forced local. We
3750 need to write out the plt entry here. */
3751 if ((off & 1) != 0)
3752 off &= ~1;
3753 else
3754 {
3755 bfd_put_32 (output_bfd,
3756 relocation,
3757 hplink->splt->contents + off);
3758 bfd_put_32 (output_bfd,
3759 elf_gp (hplink->splt->output_section->owner),
3760 hplink->splt->contents + off + 4);
3761 h->elf.plt.offset |= 1;
3762 }
3763 }
3764 }
3765 else
3766 {
3767 int indx;
3768
3769 indx = r_symndx + symtab_hdr->sh_info;
3770 off = local_got_offsets[indx];
3771
3772 /* As for the local .got entry case, we use the last
3773 bit to record whether we've already initialised
3774 this local .plt entry. */
3775 if ((off & 1) != 0)
3776 off &= ~1;
3777 else
3778 {
3779 bfd_put_32 (output_bfd,
3780 relocation,
3781 hplink->splt->contents + off);
3782 bfd_put_32 (output_bfd,
3783 elf_gp (hplink->splt->output_section->owner),
3784 hplink->splt->contents + off + 4);
3785
3786 if (info->shared)
3787 {
3788 /* Output a dynamic IPLT relocation for this
3789 PLT entry. */
3790 Elf_Internal_Rela outrel;
3791 asection *srelplt = hplink->srelplt;
3792
3793 outrel.r_offset = (off
3794 + hplink->splt->output_offset
3795 + hplink->splt->output_section->vma);
3796 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3797 outrel.r_addend = relocation;
3798 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3799 ((Elf32_External_Rela *)
3800 srelplt->contents
3801 + srelplt->reloc_count));
3802 ++srelplt->reloc_count;
3803 }
3804
3805 local_got_offsets[indx] |= 1;
3806 }
3807 }
3808
3809 if (off >= (bfd_vma) -2 || (off & 1) != 0)
3810 abort ();
3811
3812 /* PLABELs contain function pointers. Relocation is to
3813 the entry for the function in the .plt. The magic +2
3814 offset signals to $$dyncall that the function pointer
3815 is in the .plt and thus has a gp pointer too.
3816 Exception: Undefined PLABELs should have a value of
3817 zero. */
3818 if (h == NULL
3819 || (h->elf.root.type != bfd_link_hash_undefweak
3820 && h->elf.root.type != bfd_link_hash_undefined))
3821 {
3822 relocation = (off
3823 + hplink->splt->output_offset
3824 + hplink->splt->output_section->vma
3825 + 2);
3826 }
3827 plabel = 1;
3828 }
3829 /* Fall through and possibly emit a dynamic relocation. */
3830
3831 case R_PARISC_DIR17F:
3832 case R_PARISC_DIR17R:
3833 case R_PARISC_DIR14F:
3834 case R_PARISC_DIR14R:
3835 case R_PARISC_DIR21L:
3836 case R_PARISC_DPREL14F:
3837 case R_PARISC_DPREL14R:
3838 case R_PARISC_DPREL21L:
3839 case R_PARISC_DIR32:
3840 /* The reloc types handled here and this conditional
3841 expression must match the code in check_relocs and
3842 hppa_discard_copies. ie. We need exactly the same
3843 condition as in check_relocs, with some extra conditions
3844 (dynindx test in this case) to cater for relocs removed
3845 by hppa_discard_copies. */
3846 if ((input_section->flags & SEC_ALLOC) != 0
3847 && info->shared
3848#if RELATIVE_DYNAMIC_RELOCS
3849 && (is_absolute_reloc (r_type)
3850 || ((!info->symbolic
3851 || (h != NULL
3852 && ((h->elf.elf_link_hash_flags
3853 & ELF_LINK_HASH_DEF_REGULAR) == 0
3854 || h->elf.root.type == bfd_link_hash_defweak)))
3855 && (h == NULL || h->elf.dynindx != -1)))
3856#endif
3857 )
3858 {
3859 Elf_Internal_Rela outrel;
3860 boolean skip;
3861
3862 /* When generating a shared object, these relocations
3863 are copied into the output file to be resolved at run
3864 time. */
3865
3866 if (sreloc == NULL)
3867 {
3868 const char *name;
3869
3870 name = (bfd_elf_string_from_elf_section
3871 (input_bfd,
3872 elf_elfheader (input_bfd)->e_shstrndx,
3873 elf_section_data (input_section)->rel_hdr.sh_name));
3874 if (name == NULL)
3875 return false;
3876 sreloc = bfd_get_section_by_name (dynobj, name);
3877 if (sreloc == NULL)
3878 abort ();
3879 }
3880
3881 outrel.r_offset = rel->r_offset;
3882 outrel.r_addend = rel->r_addend;
3883 skip = false;
3884 if (elf_section_data (input_section)->stab_info != NULL)
3885 {
3886 bfd_vma off;
3887
3888 off = (_bfd_stab_section_offset
3889 (output_bfd, &hplink->root.stab_info,
3890 input_section,
3891 &elf_section_data (input_section)->stab_info,
3892 rel->r_offset));
3893 if (off == (bfd_vma) -1)
3894 skip = true;
3895 outrel.r_offset = off;
3896 }
3897
3898 outrel.r_offset += (input_section->output_offset
3899 + input_section->output_section->vma);
3900
3901 if (skip)
3902 {
3903 memset (&outrel, 0, sizeof (outrel));
3904 }
3905 else if (h != NULL
3906 && h->elf.dynindx != -1
3907 && (plabel
3908 || !info->symbolic
3909 || (h->elf.elf_link_hash_flags
3910 & ELF_LINK_HASH_DEF_REGULAR) == 0))
3911 {
3912 outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3913 }
3914 else /* It's a local symbol, or one marked to become local. */
3915 {
3916 int indx = 0;
3917
3918 /* Add the absolute offset of the symbol. */
3919 outrel.r_addend += relocation;
3920
3921 /* Global plabels need to be processed by the
3922 dynamic linker so that functions have at most one
3923 fptr. For this reason, we need to differentiate
3924 between global and local plabels, which we do by
3925 providing the function symbol for a global plabel
3926 reloc, and no symbol for local plabels. */
3927 if (! plabel
3928 && sym_sec != NULL
3929 && sym_sec->output_section != NULL
3930 && ! bfd_is_abs_section (sym_sec))
3931 {
3932 indx = elf_section_data (sym_sec->output_section)->dynindx;
3933 /* We are turning this relocation into one
3934 against a section symbol, so subtract out the
3935 output section's address but not the offset
3936 of the input section in the output section. */
3937 outrel.r_addend -= sym_sec->output_section->vma;
3938 }
3939
3940 outrel.r_info = ELF32_R_INFO (indx, r_type);
3941 }
3942
3943 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3944 ((Elf32_External_Rela *)
3945 sreloc->contents
3946 + sreloc->reloc_count));
3947 ++sreloc->reloc_count;
3948 }
3949 break;
3950
3951 default:
3952 break;
3953 }
3954
3955 r = final_link_relocate (input_section, contents, rel, relocation,
3956 hplink, sym_sec, h);
3957
3958 if (r == bfd_reloc_ok)
3959 continue;
3960
3961 if (h != NULL)
3962 sym_name = h->elf.root.root.string;
3963 else
3964 {
3965 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3966 symtab_hdr->sh_link,
3967 sym->st_name);
3968 if (sym_name == NULL)
3969 return false;
3970 if (*sym_name == '\0')
3971 sym_name = bfd_section_name (input_bfd, sym_sec);
3972 }
3973
3974 howto = elf_hppa_howto_table + r_type;
3975
3976 if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
3977 {
3978 (*_bfd_error_handler)
3979 (_("%s(%s+0x%lx): cannot handle %s for %s"),
3980 bfd_get_filename (input_bfd),
3981 input_section->name,
3982 (long) rel->r_offset,
3983 howto->name,
3984 sym_name);
3985 }
3986 else
3987 {
3988 if (!((*info->callbacks->reloc_overflow)
3989 (info, sym_name, howto->name, (bfd_vma) 0,
3990 input_bfd, input_section, rel->r_offset)))
3991 return false;
3992 }
3993 }
3994
3995 return true;
3996}
3997
3998/* Comparison function for qsort to sort unwind section during a
3999 final link. */
4000
4001static int
4002hppa_unwind_entry_compare (a, b)
4003 const PTR a;
4004 const PTR b;
4005{
4006 const bfd_byte *ap, *bp;
4007 unsigned long av, bv;
4008
4009 ap = (const bfd_byte *) a;
4010 av = (unsigned long) ap[0] << 24;
4011 av |= (unsigned long) ap[1] << 16;
4012 av |= (unsigned long) ap[2] << 8;
4013 av |= (unsigned long) ap[3];
4014
4015 bp = (const bfd_byte *) b;
4016 bv = (unsigned long) bp[0] << 24;
4017 bv |= (unsigned long) bp[1] << 16;
4018 bv |= (unsigned long) bp[2] << 8;
4019 bv |= (unsigned long) bp[3];
4020
4021 return av < bv ? -1 : av > bv ? 1 : 0;
4022}
4023
4024/* Finish up dynamic symbol handling. We set the contents of various
4025 dynamic sections here. */
4026
4027static boolean
4028elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
4029 bfd *output_bfd;
4030 struct bfd_link_info *info;
4031 struct elf_link_hash_entry *h;
4032 Elf_Internal_Sym *sym;
4033{
4034 struct elf32_hppa_link_hash_table *hplink;
4035 bfd *dynobj;
4036
4037 hplink = hppa_link_hash_table (info);
4038 dynobj = hplink->root.dynobj;
4039
4040 if (h->plt.offset != (bfd_vma) -1)
4041 {
4042 bfd_vma value;
4043
4044 if (h->plt.offset & 1)
4045 abort ();
4046
4047 /* This symbol has an entry in the procedure linkage table. Set
4048 it up.
4049
4050 The format of a plt entry is
4051 <funcaddr>
4052 <__gp>
4053 */
4054 value = 0;
4055 if (h->root.type == bfd_link_hash_defined
4056 || h->root.type == bfd_link_hash_defweak)
4057 {
4058 value = h->root.u.def.value;
4059 if (h->root.u.def.section->output_section != NULL)
4060 value += (h->root.u.def.section->output_offset
4061 + h->root.u.def.section->output_section->vma);
4062 }
4063
4064 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
4065 {
4066 Elf_Internal_Rela rel;
4067
4068 /* Create a dynamic IPLT relocation for this entry. */
4069 rel.r_offset = (h->plt.offset
4070 + hplink->splt->output_offset
4071 + hplink->splt->output_section->vma);
4072 if (! ((struct elf32_hppa_link_hash_entry *) h)->plt_abs
4073 && h->dynindx != -1)
4074 {
4075 /* To support lazy linking, the function pointer is
4076 initialised to point to a special stub stored at the
4077 end of the .plt. This is not done for plt entries
4078 with a base-relative dynamic relocation. */
4079 value = (hplink->splt->output_offset
4080 + hplink->splt->output_section->vma
4081 + hplink->splt->_raw_size
4082 - sizeof (plt_stub)
4083 + PLT_STUB_ENTRY);
4084 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
4085 rel.r_addend = 0;
4086 }
4087 else
4088 {
4089 /* This symbol has been marked to become local, and is
4090 used by a plabel so must be kept in the .plt. */
4091 rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
4092 rel.r_addend = value;
4093 }
4094
4095 bfd_elf32_swap_reloca_out (hplink->splt->output_section->owner,
4096 &rel,
4097 ((Elf32_External_Rela *)
4098 hplink->srelplt->contents
4099 + hplink->srelplt->reloc_count));
4100 hplink->srelplt->reloc_count++;
4101 }
4102
4103 bfd_put_32 (hplink->splt->owner,
4104 value,
4105 hplink->splt->contents + h->plt.offset);
4106 bfd_put_32 (hplink->splt->owner,
4107 elf_gp (hplink->splt->output_section->owner),
4108 hplink->splt->contents + h->plt.offset + 4);
4109 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
4110 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
4111 && h->dynindx != -1)
4112 {
4113 memset (hplink->splt->contents + h->plt.offset + 8,
4114 0, PLABEL_PLT_ENTRY_SIZE - PLT_ENTRY_SIZE);
4115 }
4116
4117 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4118 {
4119 /* Mark the symbol as undefined, rather than as defined in
4120 the .plt section. Leave the value alone. */
4121 sym->st_shndx = SHN_UNDEF;
4122 }
4123 }
4124
4125 if (h->got.offset != (bfd_vma) -1)
4126 {
4127 Elf_Internal_Rela rel;
4128
4129 /* This symbol has an entry in the global offset table. Set it
4130 up. */
4131
4132 rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
4133 + hplink->sgot->output_offset
4134 + hplink->sgot->output_section->vma);
4135
4136 /* If this is a static link, or it is a -Bsymbolic link and the
4137 symbol is defined locally or was forced to be local because
4138 of a version file, we just want to emit a RELATIVE reloc.
4139 The entry in the global offset table will already have been
4140 initialized in the relocate_section function. */
4141 if (! hplink->root.dynamic_sections_created
4142 || (info->shared
4143 && (info->symbolic || h->dynindx == -1)
4144 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
4145 {
4146 rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
4147 rel.r_addend = (h->root.u.def.value
4148 + h->root.u.def.section->output_offset
4149 + h->root.u.def.section->output_section->vma);
4150 }
4151 else
4152 {
4153 if ((h->got.offset & 1) != 0)
4154 abort ();
4155 bfd_put_32 (output_bfd, (bfd_vma) 0,
4156 hplink->sgot->contents + h->got.offset);
4157 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
4158 rel.r_addend = 0;
4159 }
4160
4161 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4162 ((Elf32_External_Rela *)
4163 hplink->srelgot->contents
4164 + hplink->srelgot->reloc_count));
4165 ++hplink->srelgot->reloc_count;
4166 }
4167
4168 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4169 {
4170 asection *s;
4171 Elf_Internal_Rela rel;
4172
4173 /* This symbol needs a copy reloc. Set it up. */
4174
4175 if (! (h->dynindx != -1
4176 && (h->root.type == bfd_link_hash_defined
4177 || h->root.type == bfd_link_hash_defweak)))
4178 abort ();
4179
4180 s = hplink->srelbss;
4181
4182 rel.r_offset = (h->root.u.def.value
4183 + h->root.u.def.section->output_offset
4184 + h->root.u.def.section->output_section->vma);
4185 rel.r_addend = 0;
4186 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
4187 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4188 ((Elf32_External_Rela *) s->contents
4189 + s->reloc_count));
4190 ++s->reloc_count;
4191 }
4192
4193 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4194 if (h->root.root.string[0] == '_'
4195 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4196 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
4197 {
4198 sym->st_shndx = SHN_ABS;
4199 }
4200
4201 return true;
4202}
4203
4204/* Finish up the dynamic sections. */
4205
4206static boolean
4207elf32_hppa_finish_dynamic_sections (output_bfd, info)
4208 bfd *output_bfd;
4209 struct bfd_link_info *info;
4210{
4211 bfd *dynobj;
4212 struct elf32_hppa_link_hash_table *hplink;
4213 asection *sdyn;
4214
4215 hplink = hppa_link_hash_table (info);
4216 dynobj = hplink->root.dynobj;
4217
4218 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4219
4220 if (hplink->root.dynamic_sections_created)
4221 {
4222 Elf32_External_Dyn *dyncon, *dynconend;
4223
4224 if (sdyn == NULL)
4225 abort ();
4226
4227 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4228 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4229 for (; dyncon < dynconend; dyncon++)
4230 {
4231 Elf_Internal_Dyn dyn;
4232 asection *s;
4233
4234 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4235
4236 switch (dyn.d_tag)
4237 {
4238 default:
4239 break;
4240
4241 case DT_PLTGOT:
4242 /* Use PLTGOT to set the GOT register. */
4243 dyn.d_un.d_ptr = elf_gp (output_bfd);
4244 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4245 break;
4246
4247 case DT_JMPREL:
4248 s = hplink->srelplt;
4249 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4250 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4251 break;
4252
4253 case DT_PLTRELSZ:
4254 s = hplink->srelplt;
4255 if (s->_cooked_size != 0)
4256 dyn.d_un.d_val = s->_cooked_size;
4257 else
4258 dyn.d_un.d_val = s->_raw_size;
4259 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4260 break;
4261 }
4262 }
4263 }
4264
4265 if (hplink->sgot->_raw_size != 0)
4266 {
4267 /* Fill in the first entry in the global offset table.
4268 We use it to point to our dynamic section, if we have one. */
4269 bfd_put_32 (output_bfd,
4270 (sdyn != NULL
4271 ? sdyn->output_section->vma + sdyn->output_offset
4272 : (bfd_vma) 0),
4273 hplink->sgot->contents);
4274
4275 /* The second entry is reserved for use by the dynamic linker. */
4276 memset (hplink->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4277
4278 /* Set .got entry size. */
4279 elf_section_data (hplink->sgot->output_section)
4280 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4281 }
4282
4283 if (hplink->splt->_raw_size != 0)
4284 {
4285 /* Set plt entry size. */
4286 elf_section_data (hplink->splt->output_section)
4287 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4288
4289 if (hplink->need_plt_stub)
4290 {
4291 /* Set up the .plt stub. */
4292 memcpy (hplink->splt->contents
4293 + hplink->splt->_raw_size - sizeof (plt_stub),
4294 plt_stub, sizeof (plt_stub));
4295
4296 if ((hplink->splt->output_offset
4297 + hplink->splt->output_section->vma
4298 + hplink->splt->_raw_size)
4299 != (hplink->sgot->output_offset
4300 + hplink->sgot->output_section->vma))
4301 {
4302 (*_bfd_error_handler)
4303 (_(".got section not immediately after .plt section"));
4304 return false;
4305 }
4306 }
4307 }
4308
4309 return true;
4310}
4311
4312/* Tweak the OSABI field of the elf header. */
4313
4314static void
4315elf32_hppa_post_process_headers (abfd, link_info)
4316 bfd *abfd;
4317 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
4318{
4319 Elf_Internal_Ehdr * i_ehdrp;
4320
4321 i_ehdrp = elf_elfheader (abfd);
4322
4323 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
4324 {
4325 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
4326 }
4327 else
4328 {
4329 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4330 }
4331}
4332
4333/* Called when writing out an object file to decide the type of a
4334 symbol. */
4335static int
4336elf32_hppa_elf_get_symbol_type (elf_sym, type)
4337 Elf_Internal_Sym *elf_sym;
4338 int type;
4339{
4340 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4341 return STT_PARISC_MILLI;
4342 else
4343 return type;
4344}
4345
4346/* Misc BFD support code. */
4347#define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4348#define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4349#define elf_info_to_howto elf_hppa_info_to_howto
4350#define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
4351
4352/* Stuff for the BFD linker. */
4353#define bfd_elf32_bfd_final_link elf32_hppa_final_link
4354#define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4355#define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
4356#define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
4357#define elf_backend_check_relocs elf32_hppa_check_relocs
4358#define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4359#define elf_backend_fake_sections elf_hppa_fake_sections
4360#define elf_backend_relocate_section elf32_hppa_relocate_section
4361#define elf_backend_hide_symbol elf32_hppa_hide_symbol
4362#define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4363#define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4364#define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4365#define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4366#define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
4367#define elf_backend_object_p elf32_hppa_object_p
4368#define elf_backend_final_write_processing elf_hppa_final_write_processing
4369#define elf_backend_post_process_headers elf32_hppa_post_process_headers
4370#define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
4371
4372#define elf_backend_can_gc_sections 1
4373#define elf_backend_plt_alignment 2
4374#define elf_backend_want_got_plt 0
4375#define elf_backend_plt_readonly 0
4376#define elf_backend_want_plt_sym 0
4377#define elf_backend_got_header_size 8
4378
4379#define TARGET_BIG_SYM bfd_elf32_hppa_vec
4380#define TARGET_BIG_NAME "elf32-hppa"
4381#define ELF_ARCH bfd_arch_hppa
4382#define ELF_MACHINE_CODE EM_PARISC
4383#define ELF_MAXPAGESIZE 0x1000
4384
4385#include "elf32-target.h"
4386
4387#undef TARGET_BIG_SYM
4388#define TARGET_BIG_SYM bfd_elf32_hppa_linux_vec
4389#undef TARGET_BIG_NAME
4390#define TARGET_BIG_NAME "elf32-hppa-linux"
4391
4392#define INCLUDED_TARGET_FILE 1
4393#include "elf32-target.h"
This page took 0.036736 seconds and 4 git commands to generate.