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