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