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