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