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