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