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