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