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