External/weak SOM fixes, branch limit corrections.
[deliverable/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
a017a724 1540 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
30667bf3
AM
1541#endif
1542 )
1543 {
1544 boolean doit;
1545 asection *srel;
1546
1547 srel = sreloc;
1548 if ((need_entry & NEED_STUBREL))
1549 srel = stubreloc;
1550
1551 /* Create a reloc section in dynobj and make room for
1552 this reloc. */
1553 if (srel == NULL)
1554 {
1555 char *name;
1556
1557 if (dynobj == NULL)
74d1c347 1558 hplink->root.dynobj = dynobj = abfd;
30667bf3
AM
1559
1560 name = bfd_elf_string_from_elf_section
1561 (abfd,
1562 elf_elfheader (abfd)->e_shstrndx,
1563 elf_section_data (sec)->rel_hdr.sh_name);
1564 if (name == NULL)
1565 {
1566 (*_bfd_error_handler)
1567 (_("Could not find relocation section for %s"),
1568 sec->name);
1569 bfd_set_error (bfd_error_bad_value);
1570 return false;
1571 }
1572
1573 if ((need_entry & NEED_STUBREL))
1574 {
74d1c347 1575 size_t len = strlen (name) + sizeof (STUB_SUFFIX);
30667bf3
AM
1576 char *newname = bfd_malloc (len);
1577
1578 if (newname == NULL)
1579 return false;
1580 strcpy (newname, name);
1581 strcpy (newname + len - sizeof (STUB_SUFFIX),
1582 STUB_SUFFIX);
1583 name = newname;
1584 }
1585
1586 srel = bfd_get_section_by_name (dynobj, name);
1587 if (srel == NULL)
1588 {
1589 flagword flags;
1590
1591 srel = bfd_make_section (dynobj, name);
1592 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1593 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1594 if ((sec->flags & SEC_ALLOC) != 0)
1595 flags |= SEC_ALLOC | SEC_LOAD;
1596 if (srel == NULL
1597 || !bfd_set_section_flags (dynobj, srel, flags)
1598 || !bfd_set_section_alignment (dynobj, srel, 2))
1599 return false;
1600 }
1601 else if ((need_entry & NEED_STUBREL))
1602 free (name);
1603
1604 if ((need_entry & NEED_STUBREL))
1605 stubreloc = srel;
1606 else
1607 sreloc = srel;
1608 }
1609
1610#if ! LONG_BRANCH_PIC_IN_SHLIB
1611 /* If this is a function call, we only need one dynamic
1612 reloc for the stub as all calls to a particular
1613 function will go through the same stub. Actually, a
1614 long branch stub needs two relocations, but we count
1615 on some intelligence on the part of the dynamic
1616 linker. */
1617 if ((need_entry & NEED_STUBREL))
1618 {
1619 doit = h->stub_reloc_sec != stubreloc;
1620 h->stub_reloc_sec = stubreloc;
1621 }
1622 else
1623#endif
1624 doit = 1;
1625
1626 if (doit)
1627 {
1628 srel->_raw_size += sizeof (Elf32_External_Rela);
1629
1630#if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
1631 /* Keep track of relocations we have entered for
1632 this global symbol, so that we can discard them
1633 later if necessary. */
1634 if (h != NULL
1635 && (0
1636#if RELATIVE_DYNAMIC_RELOCS
1637 || ! is_absolute_reloc (rtype)
1638#endif
1639 || (need_entry & NEED_STUBREL)))
1640 {
1641 struct elf32_hppa_dyn_reloc_entry *p;
252b5132 1642
30667bf3
AM
1643 for (p = h->reloc_entries; p != NULL; p = p->next)
1644 if (p->section == srel)
1645 break;
edd21aca 1646
30667bf3
AM
1647 if (p == NULL)
1648 {
1649 p = ((struct elf32_hppa_dyn_reloc_entry *)
1650 bfd_alloc (dynobj, sizeof *p));
1651 if (p == NULL)
1652 return false;
1653 p->next = h->reloc_entries;
1654 h->reloc_entries = p;
1655 p->section = srel;
1656 p->count = 0;
1657 }
edd21aca 1658
30667bf3
AM
1659 /* NEED_STUBREL and NEED_DYNREL are never both
1660 set. Leave the count at zero for the
1661 NEED_STUBREL case as we only ever have one
1662 stub reloc per section per symbol, and this
1663 simplifies code in hppa_discard_copies. */
1664 if (! (need_entry & NEED_STUBREL))
1665 ++p->count;
1666 }
1667#endif
1668 }
1669 }
1670 }
1671 }
edd21aca
AM
1672
1673 return true;
1674}
1675
30667bf3
AM
1676
1677/* Return the section that should be marked against garbage collection
1678 for a given relocation. */
1679
1680static asection *
1681elf32_hppa_gc_mark_hook (abfd, info, rel, h, sym)
1682 bfd *abfd;
1683 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1684 Elf_Internal_Rela *rel;
1685 struct elf_link_hash_entry *h;
1686 Elf_Internal_Sym *sym;
1687{
1688 if (h != NULL)
1689 {
1690 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1691 {
1692 case R_PARISC_GNU_VTINHERIT:
1693 case R_PARISC_GNU_VTENTRY:
1694 break;
1695
1696 default:
1697 switch (h->root.type)
1698 {
1699 case bfd_link_hash_defined:
1700 case bfd_link_hash_defweak:
1701 return h->root.u.def.section;
1702
1703 case bfd_link_hash_common:
1704 return h->root.u.c.p->section;
1705
1706 default:
1707 break;
1708 }
1709 }
1710 }
1711 else
1712 {
1713 if (!(elf_bad_symtab (abfd)
1714 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1715 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1716 && sym->st_shndx != SHN_COMMON))
1717 {
1718 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1719 }
1720 }
1721
1722 return NULL;
1723}
1724
1725
1726/* Update the got and plt entry reference counts for the section being
1727 removed. */
edd21aca
AM
1728
1729static boolean
30667bf3
AM
1730elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
1731 bfd *abfd;
1732 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1733 asection *sec;
1734 const Elf_Internal_Rela *relocs;
edd21aca 1735{
30667bf3
AM
1736 Elf_Internal_Shdr *symtab_hdr;
1737 struct elf_link_hash_entry **sym_hashes;
1738 bfd_signed_vma *local_got_refcounts;
74d1c347 1739 bfd_signed_vma *local_plt_refcounts;
30667bf3
AM
1740 const Elf_Internal_Rela *rel, *relend;
1741 unsigned long r_symndx;
1742 struct elf_link_hash_entry *h;
74d1c347 1743 struct elf32_hppa_link_hash_table *hplink;
30667bf3
AM
1744 bfd *dynobj;
1745 asection *sgot;
1746 asection *srelgot;
1747
1748 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1749 sym_hashes = elf_sym_hashes (abfd);
1750 local_got_refcounts = elf_local_got_refcounts (abfd);
74d1c347
AM
1751 local_plt_refcounts = local_got_refcounts;
1752 if (local_plt_refcounts != NULL)
1753 local_plt_refcounts += symtab_hdr->sh_info;
1754 hplink = hppa_link_hash_table (info);
1755 dynobj = hplink->root.dynobj;
30667bf3
AM
1756 if (dynobj == NULL)
1757 return true;
1758
74d1c347
AM
1759 sgot = hplink->sgot;
1760 srelgot = hplink->srelgot;
30667bf3
AM
1761
1762 relend = relocs + sec->reloc_count;
1763 for (rel = relocs; rel < relend; rel++)
1764 switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1765 {
1766 case R_PARISC_DLTIND14F:
1767 case R_PARISC_DLTIND14R:
1768 case R_PARISC_DLTIND21L:
1769 r_symndx = ELF32_R_SYM (rel->r_info);
1770 if (r_symndx >= symtab_hdr->sh_info)
1771 {
1772 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1773 if (h->got.refcount > 0)
1774 {
1775 h->got.refcount -= 1;
1776 if (h->got.refcount == 0)
1777 {
74d1c347 1778 sgot->_raw_size -= GOT_ENTRY_SIZE;
30667bf3
AM
1779 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1780 }
1781 }
1782 }
1783 else if (local_got_refcounts != NULL)
1784 {
1785 if (local_got_refcounts[r_symndx] > 0)
1786 {
1787 local_got_refcounts[r_symndx] -= 1;
1788 if (local_got_refcounts[r_symndx] == 0)
1789 {
74d1c347 1790 sgot->_raw_size -= GOT_ENTRY_SIZE;
30667bf3
AM
1791 if (info->shared)
1792 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1793 }
1794 }
1795 }
1796 break;
edd21aca 1797
30667bf3
AM
1798 case R_PARISC_PCREL12F:
1799 case R_PARISC_PCREL17C:
1800 case R_PARISC_PCREL17F:
1801 case R_PARISC_PCREL22F:
1802 r_symndx = ELF32_R_SYM (rel->r_info);
1803 if (r_symndx >= symtab_hdr->sh_info)
1804 {
1805 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1806 if (h->plt.refcount > 0)
1807 h->plt.refcount -= 1;
1808 }
1809 break;
edd21aca 1810
74d1c347
AM
1811 case R_PARISC_PLABEL14R:
1812 case R_PARISC_PLABEL21L:
1813 case R_PARISC_PLABEL32:
1814 r_symndx = ELF32_R_SYM (rel->r_info);
1815 if (r_symndx >= symtab_hdr->sh_info)
1816 {
1817 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1818 if (h->plt.refcount > 0)
1819 h->plt.refcount -= 1;
1820 }
1821 else if (local_plt_refcounts != NULL)
1822 {
1823 if (local_plt_refcounts[r_symndx] > 0)
1824 local_plt_refcounts[r_symndx] -= 1;
1825 }
1826 break;
1827
30667bf3
AM
1828 default:
1829 break;
1830 }
252b5132 1831
252b5132
RH
1832 return true;
1833}
1834
252b5132 1835
74d1c347
AM
1836/* Our own version of hide_symbol, so that we can keep plt entries for
1837 plabels. */
1838
1839static void
1840elf32_hppa_hide_symbol (info, h)
1841 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1842 struct elf_link_hash_entry *h;
1843{
1844 h->dynindx = -1;
1845 if (! ((struct elf32_hppa_link_hash_entry *) h)->plabel)
1846 {
1847 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1848 h->plt.offset = (bfd_vma) -1;
1849 }
1850}
1851
1852
30667bf3
AM
1853/* Adjust a symbol defined by a dynamic object and referenced by a
1854 regular object. The current definition is in some section of the
1855 dynamic object, but we're not including those sections. We have to
1856 change the definition to something the rest of the link can
1857 understand. */
252b5132 1858
30667bf3
AM
1859static boolean
1860elf32_hppa_adjust_dynamic_symbol (info, h)
1861 struct bfd_link_info *info;
1862 struct elf_link_hash_entry *h;
252b5132 1863{
30667bf3
AM
1864 bfd *dynobj;
1865 struct elf32_hppa_link_hash_table *hplink;
1866 asection *s;
1867
30667bf3 1868 hplink = hppa_link_hash_table (info);
74d1c347 1869 dynobj = hplink->root.dynobj;
30667bf3
AM
1870
1871 /* If this is a function, put it in the procedure linkage table. We
1872 will fill in the contents of the procedure linkage table later,
1873 when we know the address of the .got section. */
1874 if (h->type == STT_FUNC
1875 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1876 {
1877 if (h->plt.refcount <= 0
1878 || ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1879 && h->root.type != bfd_link_hash_defweak
74d1c347 1880 && ! ((struct elf32_hppa_link_hash_entry *) h)->plabel
30667bf3
AM
1881 && (!info->shared || info->symbolic)))
1882 {
1883 /* The .plt entry is not needed when:
1884 a) Garbage collection has removed all references to the
1885 symbol, or
1886 b) We know for certain the symbol is defined in this
74d1c347
AM
1887 object, and it's not a weak definition, nor is the symbol
1888 used by a plabel relocation. Either this object is the
1889 application or we are doing a shared symbolic link. */
1890
1891 /* As a special sop to the hppa ABI, we keep a .plt entry
1892 for functions in sections containing PIC code. */
30667bf3
AM
1893 if (!info->shared
1894 && h->plt.refcount > 0
1895 && (h->root.type == bfd_link_hash_defined
1896 || h->root.type == bfd_link_hash_defweak)
1897 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0)
1898 {
1899 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
1900 }
1901 else
1902 {
1903 h->plt.offset = (bfd_vma) -1;
1904 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1905 return true;
1906 }
1907 }
1908
1909 /* Make an entry in the .plt section. */
1910 s = hplink->splt;
1911 h->plt.offset = s->_raw_size;
74d1c347
AM
1912 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
1913 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
1914 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1915 {
1916 /* Add some extra space for the dynamic linker to use. */
1917 s->_raw_size += PLABEL_PLT_ENTRY_SIZE;
1918 }
1919 else
1920 s->_raw_size += PLT_ENTRY_SIZE;
30667bf3
AM
1921
1922 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
1923 {
1924 /* Make sure this symbol is output as a dynamic symbol. */
74d1c347
AM
1925 if (h->dynindx == -1
1926 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
30667bf3
AM
1927 {
1928 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1929 return false;
1930 }
1931
1932 /* We also need to make an entry in the .rela.plt section. */
1933 s = hplink->srelplt;
1934 s->_raw_size += sizeof (Elf32_External_Rela);
1935 }
1936 return true;
1937 }
edd21aca 1938
30667bf3
AM
1939 /* If this is a weak symbol, and there is a real definition, the
1940 processor independent code will have arranged for us to see the
1941 real definition first, and we can just use the same value. */
1942 if (h->weakdef != NULL)
edd21aca 1943 {
30667bf3
AM
1944 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1945 || h->weakdef->root.type == bfd_link_hash_defweak);
1946 h->root.u.def.section = h->weakdef->root.u.def.section;
1947 h->root.u.def.value = h->weakdef->root.u.def.value;
1948 return true;
1949 }
edd21aca 1950
30667bf3
AM
1951 /* This is a reference to a symbol defined by a dynamic object which
1952 is not a function. */
1953
1954 /* If we are creating a shared library, we must presume that the
1955 only references to the symbol are via the global offset table.
1956 For such cases we need not do anything here; the relocations will
1957 be handled correctly by relocate_section. */
1958 if (info->shared)
1959 return true;
1960
1961 /* If there are no references to this symbol that do not use the
1962 GOT, we don't need to generate a copy reloc. */
1963 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1964 return true;
1965
1966 /* We must allocate the symbol in our .dynbss section, which will
1967 become part of the .bss section of the executable. There will be
1968 an entry for this symbol in the .dynsym section. The dynamic
1969 object will contain position independent code, so all references
1970 from the dynamic object to this symbol will go through the global
1971 offset table. The dynamic linker will use the .dynsym entry to
1972 determine the address it must put in the global offset table, so
1973 both the dynamic object and the regular object will refer to the
1974 same memory location for the variable. */
1975
1976 s = hplink->sdynbss;
1977
1978 /* We must generate a COPY reloc to tell the dynamic linker to
1979 copy the initial value out of the dynamic object and into the
1980 runtime process image. We need to remember the offset into the
1981 .rela.bss section we are going to use. */
1982 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1983 {
1984 asection *srel;
1985
1986 srel = hplink->srelbss;
1987 srel->_raw_size += sizeof (Elf32_External_Rela);
1988 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
edd21aca 1989 }
252b5132 1990
30667bf3
AM
1991 {
1992 /* We need to figure out the alignment required for this symbol. I
1993 have no idea how other ELF linkers handle this. */
1994 unsigned int power_of_two;
1995
1996 power_of_two = bfd_log2 (h->size);
1997 if (power_of_two > 3)
1998 power_of_two = 3;
1999
2000 /* Apply the required alignment. */
2001 s->_raw_size = BFD_ALIGN (s->_raw_size,
2002 (bfd_size_type) (1 << power_of_two));
2003 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2004 {
2005 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2006 return false;
2007 }
2008 }
2009 /* Define the symbol as being at this point in the section. */
2010 h->root.u.def.section = s;
2011 h->root.u.def.value = s->_raw_size;
edd21aca 2012
30667bf3
AM
2013 /* Increment the section size to make room for the symbol. */
2014 s->_raw_size += h->size;
252b5132
RH
2015
2016 return true;
2017}
2018
252b5132 2019
30667bf3
AM
2020/* Called via elf_link_hash_traverse to create .plt entries for an
2021 application that uses statically linked PIC functions. Similar to
2022 the first part of elf32_hppa_adjust_dynamic_symbol. */
252b5132 2023
30667bf3
AM
2024static boolean
2025hppa_handle_PIC_calls (h, inf)
2026 struct elf_link_hash_entry *h;
2027 PTR inf;
252b5132 2028{
30667bf3
AM
2029 struct bfd_link_info *info;
2030 bfd *dynobj;
2031 struct elf32_hppa_link_hash_table *hplink;
2032 asection *s;
2033
2034 if (! (h->plt.refcount > 0
2035 && (h->root.type == bfd_link_hash_defined
2036 || h->root.type == bfd_link_hash_defweak)
2037 && (h->root.u.def.section->flags & SEC_HAS_GOT_REF) != 0))
252b5132 2038 {
30667bf3
AM
2039 h->plt.offset = (bfd_vma) -1;
2040 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2041 return true;
252b5132
RH
2042 }
2043
74d1c347 2044 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
30667bf3 2045 ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
edd21aca 2046
30667bf3 2047 info = (struct bfd_link_info *) inf;
30667bf3 2048 hplink = hppa_link_hash_table (info);
74d1c347 2049 dynobj = hplink->root.dynobj;
edd21aca 2050
30667bf3
AM
2051 /* Make an entry in the .plt section. */
2052 s = hplink->splt;
2053 h->plt.offset = s->_raw_size;
2054 s->_raw_size += PLT_ENTRY_SIZE;
2055
2056 return true;
2057}
2058
2059
74d1c347
AM
2060#if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
2061 || RELATIVE_DYNAMIC_RELOCS)
30667bf3
AM
2062/* This function is called via elf_link_hash_traverse to discard space
2063 we allocated for relocs that it turned out we didn't need. */
2064
2065static boolean
2066hppa_discard_copies (h, inf)
2067 struct elf_link_hash_entry *h;
2068 PTR inf;
2069{
2070 struct elf32_hppa_dyn_reloc_entry *s;
2071 struct elf32_hppa_link_hash_entry *eh;
2072 struct bfd_link_info *info;
2073
2074 eh = (struct elf32_hppa_link_hash_entry *) h;
2075 info = (struct bfd_link_info *) inf;
2076
74d1c347 2077#if ! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT
30667bf3
AM
2078 /* Handle the stub reloc case. If we have a plt entry for the
2079 function, we won't be needing long branch stubs. s->count will
2080 only be zero for stub relocs, which provides a handy way of
2081 flagging these relocs, and means we need do nothing special for
2082 the forced local and symbolic link case. */
2083 if (eh->stub_reloc_sec != NULL
2084 && eh->elf.plt.offset != (bfd_vma) -1)
2085 {
2086 for (s = eh->reloc_entries; s != NULL; s = s->next)
2087 if (s->count == 0)
2088 s->section->_raw_size -= sizeof (Elf32_External_Rela);
2089 }
2090#endif
2091
74d1c347 2092#if RELATIVE_DYNAMIC_RELOCS
30667bf3
AM
2093 /* If a symbol has been forced local or we have found a regular
2094 definition for the symbolic link case, then we won't be needing
2095 any relocs. */
30667bf3
AM
2096 if (eh->elf.dynindx == -1
2097 || ((eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
a017a724 2098 && !is_absolute_reloc (r_type)
30667bf3
AM
2099 && info->symbolic))
2100 {
2101 for (s = eh->reloc_entries; s != NULL; s = s->next)
2102 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2103 }
2104#endif
2105
2106 return true;
2107}
2108#endif
2109
2110
d5c73c2f
AM
2111/* This function is called via elf_link_hash_traverse to force
2112 millicode symbols local so they do not end up as globals in the
2113 dynamic symbol table. We ought to be able to do this in
2114 adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2115 for all dynamic symbols. Arguably, this is a bug in
2116 elf_adjust_dynamic_symbol. */
2117
2118static boolean
2119clobber_millicode_symbols (h, info)
2120 struct elf_link_hash_entry *h;
2121 struct bfd_link_info *info;
2122{
25f72752
AM
2123 /* Note! We only want to remove these from the dynamic symbol
2124 table. Therefore we do not set ELF_LINK_FORCED_LOCAL. */
d5c73c2f 2125 if (h->type == STT_PARISC_MILLI)
25f72752 2126 elf32_hppa_hide_symbol(info, h);
d5c73c2f
AM
2127 return true;
2128}
2129
2130
30667bf3
AM
2131/* Set the sizes of the dynamic sections. */
2132
2133static boolean
2134elf32_hppa_size_dynamic_sections (output_bfd, info)
2135 bfd *output_bfd;
2136 struct bfd_link_info *info;
2137{
2138 struct elf32_hppa_link_hash_table *hplink;
2139 bfd *dynobj;
2140 asection *s;
2141 boolean relocs;
2142 boolean reltext;
2143
2144 hplink = hppa_link_hash_table (info);
74d1c347 2145 dynobj = hplink->root.dynobj;
30667bf3
AM
2146 BFD_ASSERT (dynobj != NULL);
2147
74d1c347 2148 if (hplink->root.dynamic_sections_created)
30667bf3 2149 {
74d1c347
AM
2150 const char *funcname;
2151 bfd *i;
2152
30667bf3
AM
2153 /* Set the contents of the .interp section to the interpreter. */
2154 if (! info->shared)
2155 {
2156 s = bfd_get_section_by_name (dynobj, ".interp");
2157 BFD_ASSERT (s != NULL);
2158 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2159 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2160 }
74d1c347 2161
d5c73c2f
AM
2162 /* Force millicode symbols local. */
2163 elf_link_hash_traverse (&hplink->root,
2164 clobber_millicode_symbols,
2165 info);
2166
74d1c347
AM
2167 /* DT_INIT and DT_FINI need a .plt entry. Make sure they have
2168 one. */
2169 funcname = info->init_function;
2170 while (1)
2171 {
2172 if (funcname != NULL)
2173 {
2174 struct elf_link_hash_entry *h;
2175
2176 h = elf_link_hash_lookup (&hplink->root,
2177 funcname,
2178 false, false, false);
2179 if (h != NULL
2180 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2181 | ELF_LINK_HASH_DEF_REGULAR)))
2182 {
2183 if (h->plt.refcount <= 0)
2184 {
2185 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2186
2187 /* Make an entry in the .plt section. We know
2188 the function doesn't have a plabel by the
25f72752 2189 refcount. */
74d1c347
AM
2190 s = hplink->splt;
2191 h->plt.offset = s->_raw_size;
2192 s->_raw_size += PLT_ENTRY_SIZE;
2193
2194 /* Make sure this symbol is output as a dynamic
2195 symbol. */
2196 if (h->dynindx == -1)
2197 {
2198 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2199 return false;
2200 }
2201
2202 /* Make an entry for the reloc too. */
2203 s = hplink->srelplt;
2204 s->_raw_size += sizeof (Elf32_External_Rela);
2205 }
2206
2207 ((struct elf32_hppa_link_hash_entry *) h)->plt_abs = 1;
2208 }
2209 }
2210 if (funcname == info->fini_function)
2211 break;
2212 funcname = info->fini_function;
2213 }
2214
2215 /* Set up .plt offsets for local plabels. */
2216 for (i = info->input_bfds; i; i = i->link_next)
2217 {
2218 bfd_signed_vma *local_plt;
2219 bfd_signed_vma *end_local_plt;
2220 bfd_size_type locsymcount;
2221 Elf_Internal_Shdr *symtab_hdr;
2222
2223 local_plt = elf_local_got_refcounts (i);
2224 if (!local_plt)
2225 continue;
2226
2227 symtab_hdr = &elf_tdata (i)->symtab_hdr;
2228 locsymcount = symtab_hdr->sh_info;
2229 local_plt += locsymcount;
2230 end_local_plt = local_plt + locsymcount;
2231
2232 for (; local_plt < end_local_plt; ++local_plt)
2233 {
2234 if (*local_plt > 0)
2235 {
2236 s = hplink->splt;
2237 *local_plt = s->_raw_size;
2238 s->_raw_size += PLT_ENTRY_SIZE;
2239 if (info->shared)
2240 hplink->srelplt->_raw_size += sizeof (Elf32_External_Rela);
2241 }
2242 else
2243 *local_plt = (bfd_vma) -1;
2244 }
2245 }
30667bf3
AM
2246 }
2247 else
2248 {
2249 /* Run through the function symbols, looking for any that are
2250 PIC, and allocate space for the necessary .plt entries so
2251 that %r19 will be set up. */
2252 if (! info->shared)
2253 elf_link_hash_traverse (&hplink->root,
2254 hppa_handle_PIC_calls,
2255 info);
2256
2257 /* We may have created entries in the .rela.got section.
2258 However, if we are not creating the dynamic sections, we will
2259 not actually use these entries. Reset the size of .rela.got,
2260 which will cause it to get stripped from the output file
2261 below. */
2262 hplink->srelgot->_raw_size = 0;
2263 }
2264
74d1c347
AM
2265#if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
2266 || RELATIVE_DYNAMIC_RELOCS)
30667bf3
AM
2267 /* If this is a -Bsymbolic shared link, then we need to discard all
2268 relocs against symbols defined in a regular object. We also need
2269 to lose relocs we've allocated for long branch stubs if we know
2270 we won't be generating a stub. */
2271 if (info->shared)
2272 elf_link_hash_traverse (&hplink->root,
2273 hppa_discard_copies,
2274 info);
2275#endif
2276
2277 /* The check_relocs and adjust_dynamic_symbol entry points have
2278 determined the sizes of the various dynamic sections. Allocate
2279 memory for them. */
2280 relocs = false;
2281 reltext = false;
2282 for (s = dynobj->sections; s != NULL; s = s->next)
2283 {
2284 const char *name;
2285
2286 if ((s->flags & SEC_LINKER_CREATED) == 0)
2287 continue;
2288
2289 /* It's OK to base decisions on the section name, because none
2290 of the dynobj section names depend upon the input files. */
2291 name = bfd_get_section_name (dynobj, s);
2292
2293 if (strncmp (name, ".rela", 5) == 0)
2294 {
2295 if (s->_raw_size != 0)
2296 {
2297 asection *target;
2298
2299 /* Remember whether there are any reloc sections other
2300 than .rela.plt. */
2301 if (strcmp (name+5, ".plt") != 0)
2302 {
2303 const char *outname;
2304
2305 relocs = true;
2306
2307 /* If this relocation section applies to a read only
2308 section, then we probably need a DT_TEXTREL
2309 entry. The entries in the .rela.plt section
2310 really apply to the .got section, which we
2311 created ourselves and so know is not readonly. */
2312 outname = bfd_get_section_name (output_bfd,
2313 s->output_section);
2314 target = bfd_get_section_by_name (output_bfd, outname + 5);
2315 if (target != NULL
2316 && (target->flags & SEC_READONLY) != 0
2317 && (target->flags & SEC_ALLOC) != 0)
2318 reltext = true;
2319 }
2320
2321 /* We use the reloc_count field as a counter if we need
2322 to copy relocs into the output file. */
2323 s->reloc_count = 0;
2324 }
2325 }
2326 else if (strcmp (name, ".plt") == 0)
2327 ;
2328 else if (strcmp (name, ".got") == 0)
2329 ;
2330 else
2331 {
2332 /* It's not one of our sections, so don't allocate space. */
2333 continue;
2334 }
2335
2336 if (s->_raw_size == 0)
2337 {
2338 /* If we don't need this section, strip it from the
2339 output file. This is mostly to handle .rela.bss and
2340 .rela.plt. We must create both sections in
2341 create_dynamic_sections, because they must be created
2342 before the linker maps input sections to output
2343 sections. The linker does that before
2344 adjust_dynamic_symbol is called, and it is that
2345 function which decides whether anything needs to go
2346 into these sections. */
2347 _bfd_strip_section_from_output (info, s);
2348 continue;
2349 }
2350
2351 /* Allocate memory for the section contents. Zero it, because
2352 we may not fill in all the reloc sections. */
2353 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2354 if (s->contents == NULL && s->_raw_size != 0)
2355 return false;
2356 }
2357
74d1c347 2358 if (hplink->root.dynamic_sections_created)
30667bf3
AM
2359 {
2360 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2361 actually has nothing to do with the PLT, it is how we
2362 communicate the LTP value of a load module to the dynamic
2363 linker. */
2364 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
2365 return false;
2366
2367 /* Add some entries to the .dynamic section. We fill in the
2368 values later, in elf32_hppa_finish_dynamic_sections, but we
2369 must add the entries now so that we get the correct size for
2370 the .dynamic section. The DT_DEBUG entry is filled in by the
2371 dynamic linker and used by the debugger. */
2372 if (! info->shared)
2373 {
2374 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2375 return false;
2376 }
2377
2378 if (hplink->srelplt->_raw_size != 0)
2379 {
2380 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2381 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2382 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2383 return false;
2384 }
2385
2386 if (relocs)
2387 {
2388 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2389 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2390 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2391 sizeof (Elf32_External_Rela)))
2392 return false;
2393 }
2394
2395 if (reltext)
2396 {
2397 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2398 return false;
2399 info->flags |= DF_TEXTREL;
2400 }
2401 }
2402
2403 return true;
2404}
2405
2406
2407/* External entry points for sizing and building linker stubs. */
2408
2409/* Determine and set the size of the stub section for a final link.
2410
2411 The basic idea here is to examine all the relocations looking for
2412 PC-relative calls to a target that is unreachable with a "bl"
2413 instruction. */
2414
2415boolean
25f72752 2416elf32_hppa_size_stubs (output_bfd, stub_bfd, info, multi_subspace,
30667bf3 2417 add_stub_section, layout_sections_again)
25f72752 2418 bfd *output_bfd;
30667bf3 2419 bfd *stub_bfd;
30667bf3 2420 struct bfd_link_info *info;
25f72752 2421 boolean multi_subspace;
30667bf3
AM
2422 asection * (*add_stub_section) PARAMS ((const char *, asection *));
2423 void (*layout_sections_again) PARAMS ((void));
2424{
2425 bfd *input_bfd;
2426 asection *section;
25f72752 2427 asection **input_list, **list;
30667bf3 2428 Elf_Internal_Sym *local_syms, **all_local_syms;
25f72752
AM
2429 unsigned int bfd_indx, bfd_count;
2430 int top_id, top_index;
30667bf3
AM
2431 struct elf32_hppa_link_hash_table *hplink;
2432 boolean stub_changed = 0;
25f72752 2433 boolean ret = 0;
30667bf3
AM
2434
2435 hplink = hppa_link_hash_table (info);
2436
2437 /* Stash our params away. */
2438 hplink->stub_bfd = stub_bfd;
2439 hplink->multi_subspace = multi_subspace;
2440 hplink->add_stub_section = add_stub_section;
2441 hplink->layout_sections_again = layout_sections_again;
2442
1badb539
AM
2443 /* Count the number of input BFDs and find the top input section id. */
2444 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
30667bf3
AM
2445 input_bfd != NULL;
2446 input_bfd = input_bfd->link_next)
2447 {
2448 bfd_count += 1;
25f72752
AM
2449 for (section = input_bfd->sections;
2450 section != NULL;
2451 section = section->next)
2452 {
2453 if (top_id < section->id)
2454 top_id = section->id;
2455 }
30667bf3
AM
2456 }
2457
25f72752
AM
2458 hplink->stub_group
2459 = (struct map_stub *) bfd_zmalloc (sizeof (struct map_stub) * (top_id + 1));
2460 if (hplink->stub_group == NULL)
30667bf3
AM
2461 return false;
2462
1badb539
AM
2463 /* Make a list of input sections for each output section included in
2464 the link.
2465
2466 We can't use output_bfd->section_count here to find the top output
2467 section index as some sections may have been removed, and
2468 _bfd_strip_section_from_output doesn't renumber the indices. */
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
25f72752 2477 input_list
1badb539 2478 = (asection **) bfd_malloc (sizeof (asection *) * (top_index + 1));
25f72752
AM
2479 if (input_list == NULL)
2480 return false;
2481
1badb539
AM
2482 /* For sections we aren't interested in, mark their entries with a
2483 value we can check later. */
2484 list = input_list + top_index;
2485 do
2486 *list = bfd_abs_section_ptr;
2487 while (list-- != input_list);
2488
2489 for (section = output_bfd->sections;
2490 section != NULL;
2491 section = section->next)
2492 {
2493 if ((section->flags & SEC_CODE) != 0 && section->index <= top_index)
2494 input_list[section->index] = NULL;
2495 }
2496
2497 /* Now actually build the lists. */
25f72752
AM
2498 for (input_bfd = info->input_bfds;
2499 input_bfd != NULL;
2500 input_bfd = input_bfd->link_next)
2501 {
2502 for (section = input_bfd->sections;
2503 section != NULL;
2504 section = section->next)
2505 {
2506 if (section->output_section != NULL
1badb539
AM
2507 && section->output_section->owner == output_bfd
2508 && section->output_section->index <= top_index)
25f72752
AM
2509 {
2510 list = input_list + section->output_section->index;
1badb539
AM
2511 if (*list != bfd_abs_section_ptr)
2512 {
2513 /* Steal the link_sec pointer for our list. */
25f72752 2514#define PREV_SEC(sec) (hplink->stub_group[(sec)->id].link_sec)
1badb539
AM
2515 /* This happens to make the list in reverse order,
2516 which is what we want. */
2517 PREV_SEC (section) = *list;
2518 *list = section;
2519 }
25f72752
AM
2520 }
2521 }
2522 }
2523
2524 /* See whether we can group stub sections together. Grouping stub
2525 sections may result in fewer stubs. More importantly, we need to
2526 put all .init* and .fini* stubs at the beginning of the .init or
2527 .fini output sections respectively, because glibc splits the
2528 _init and _fini functions into multiple parts. Putting a stub in
2529 the middle of a function is not a good idea. */
a017a724 2530 list = input_list + top_index;
1badb539 2531 do
25f72752
AM
2532 {
2533 asection *tail = *list;
1badb539
AM
2534 if (tail == bfd_abs_section_ptr)
2535 continue;
25f72752
AM
2536 while (tail != NULL)
2537 {
2538 asection *curr;
2539 asection *prev;
2540 bfd_size_type total;
2541
2542 curr = tail;
2543 if (tail->_cooked_size)
2544 total = tail->_cooked_size;
2545 else
2546 total = tail->_raw_size;
2547 while ((prev = PREV_SEC (curr)) != NULL
2548 && ((total += curr->output_offset - prev->output_offset)
2549 < 250000))
2550 curr = prev;
2551
2552 /* OK, the size from the start of CURR to the end is less
2553 than 250000 bytes and thus can be handled by one stub
2554 section. (or the tail section is itself larger than
2555 250000 bytes, in which case we may be toast.)
2556 We should really be keeping track of the total size of
2557 stubs added here, as stubs contribute to the final output
2558 section size. That's a little tricky, and this way will
2559 only break if stubs added total more than 12144 bytes, or
2560 1518 long branch stubs. It seems unlikely for more than
2561 1518 different functions to be called, especially from
2562 code only 250000 bytes long. */
2563 do
2564 {
2565 prev = PREV_SEC (tail);
2566 /* Set up this stub group. */
2567 hplink->stub_group[tail->id].link_sec = curr;
2568 }
2569 while (tail != curr && (tail = prev) != NULL);
2570
2571 /* But wait, there's more! Input sections up to 250000
2572 bytes before the stub section can be handled by it too. */
2573 total = 0;
2574 while (prev != NULL
2575 && ((total += tail->output_offset - prev->output_offset)
2576 < 250000))
2577 {
2578 tail = prev;
2579 prev = PREV_SEC (tail);
2580 hplink->stub_group[tail->id].link_sec = curr;
2581 }
2582 tail = prev;
2583 }
2584 }
1badb539 2585 while (list-- != input_list);
25f72752 2586 free (input_list);
1badb539 2587#undef PREV_SEC
30667bf3
AM
2588
2589 /* We want to read in symbol extension records only once. To do this
2590 we need to read in the local symbols in parallel and save them for
2591 later use; so hold pointers to the local symbols in an array. */
2592 all_local_syms
2593 = (Elf_Internal_Sym **) bfd_zmalloc (sizeof (Elf_Internal_Sym *)
2594 * bfd_count);
2595 if (all_local_syms == NULL)
25f72752 2596 return false;
30667bf3
AM
2597
2598 /* Walk over all the input BFDs, swapping in local symbols.
2599 If we are creating a shared library, create hash entries for the
2600 export stubs. */
25f72752 2601 for (input_bfd = info->input_bfds, bfd_indx = 0;
30667bf3 2602 input_bfd != NULL;
25f72752 2603 input_bfd = input_bfd->link_next, bfd_indx++)
30667bf3
AM
2604 {
2605 Elf_Internal_Shdr *symtab_hdr;
2606 Elf_Internal_Sym *isym;
25f72752 2607 Elf32_External_Sym *ext_syms, *esym, *end_sy;
edd21aca 2608
252b5132
RH
2609 /* We'll need the symbol table in a second. */
2610 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2611 if (symtab_hdr->sh_info == 0)
2612 continue;
2613
edd21aca
AM
2614 /* We need an array of the local symbols attached to the input bfd.
2615 Unfortunately, we're going to have to read & swap them in. */
2616 local_syms = (Elf_Internal_Sym *)
2617 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf_Internal_Sym));
2618 if (local_syms == NULL)
2619 {
2620 goto error_ret_free_local;
2621 }
25f72752 2622 all_local_syms[bfd_indx] = local_syms;
edd21aca
AM
2623 ext_syms = (Elf32_External_Sym *)
2624 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf32_External_Sym));
2625 if (ext_syms == NULL)
2626 {
2627 goto error_ret_free_local;
2628 }
2629
2630 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
30667bf3 2631 || (bfd_read (ext_syms, 1,
edd21aca
AM
2632 (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)),
2633 input_bfd)
2634 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2635 {
2636 free (ext_syms);
2637 goto error_ret_free_local;
2638 }
2639
2640 /* Swap the local symbols in. */
2641 isym = local_syms;
2642 esym = ext_syms;
25f72752 2643 for (end_sy = esym + symtab_hdr->sh_info; esym < end_sy; esym++, isym++)
edd21aca
AM
2644 bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
2645
2646 /* Now we can free the external symbols. */
2647 free (ext_syms);
edd21aca 2648
d5c73c2f 2649#if ! LONG_BRANCH_PIC_IN_SHLIB
25f72752
AM
2650 /* If this is a shared link, find all the stub reloc sections. */
2651 if (info->shared)
2652 for (section = input_bfd->sections;
2653 section != NULL;
2654 section = section->next)
2655 {
2656 char *name;
2657 asection *reloc_sec;
d5c73c2f 2658
25f72752
AM
2659 name = bfd_malloc (strlen (section->name)
2660 + sizeof STUB_SUFFIX
2661 + 5);
2662 if (name == NULL)
2663 return false;
2664 sprintf (name, ".rela%s%s", section->name, STUB_SUFFIX);
2665 reloc_sec = bfd_get_section_by_name (hplink->root.dynobj, name);
2666 hplink->stub_group[section->id].reloc_sec = reloc_sec;
2667 free (name);
2668 }
d5c73c2f 2669#endif
d5c73c2f 2670
30667bf3
AM
2671 if (info->shared && hplink->multi_subspace)
2672 {
25f72752
AM
2673 struct elf_link_hash_entry **sym_hashes;
2674 struct elf_link_hash_entry **end_hashes;
30667bf3
AM
2675 unsigned int symcount;
2676
2677 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2678 - symtab_hdr->sh_info);
25f72752
AM
2679 sym_hashes = elf_sym_hashes (input_bfd);
2680 end_hashes = sym_hashes + symcount;
30667bf3
AM
2681
2682 /* Look through the global syms for functions; We need to
2683 build export stubs for all globally visible functions. */
25f72752 2684 for (; sym_hashes < end_hashes; sym_hashes++)
30667bf3
AM
2685 {
2686 struct elf32_hppa_link_hash_entry *hash;
2687
25f72752 2688 hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
30667bf3
AM
2689
2690 while (hash->elf.root.type == bfd_link_hash_indirect
2691 || hash->elf.root.type == bfd_link_hash_warning)
2692 hash = ((struct elf32_hppa_link_hash_entry *)
2693 hash->elf.root.u.i.link);
2694
2695 /* At this point in the link, undefined syms have been
2696 resolved, so we need to check that the symbol was
2697 defined in this BFD. */
2698 if ((hash->elf.root.type == bfd_link_hash_defined
2699 || hash->elf.root.type == bfd_link_hash_defweak)
2700 && hash->elf.type == STT_FUNC
2701 && hash->elf.root.u.def.section->output_section != NULL
25f72752
AM
2702 && (hash->elf.root.u.def.section->output_section->owner
2703 == output_bfd)
30667bf3
AM
2704 && hash->elf.root.u.def.section->owner == input_bfd
2705 && (hash->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
2706 && !(hash->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2707 && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2708 {
2709 asection *sec;
2710 const char *stub_name;
2711 struct elf32_hppa_stub_hash_entry *stub_entry;
2712
2713 sec = hash->elf.root.u.def.section;
2714 stub_name = hash->elf.root.root.string;
2715 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2716 stub_name,
2717 false, false);
2718 if (stub_entry == NULL)
2719 {
25f72752 2720 stub_entry = hppa_add_stub (stub_name, sec, hplink);
30667bf3
AM
2721 if (!stub_entry)
2722 goto error_ret_free_local;
2723
2724 stub_entry->target_value = hash->elf.root.u.def.value;
2725 stub_entry->target_section = hash->elf.root.u.def.section;
2726 stub_entry->stub_type = hppa_stub_export;
2727 stub_entry->h = hash;
2728 stub_changed = 1;
2729 }
2730 else
2731 {
2732 (*_bfd_error_handler) (_("%s: duplicate export stub %s"),
2733 bfd_get_filename (input_bfd),
2734 stub_name);
2735 }
2736 }
2737 }
30667bf3
AM
2738 }
2739 }
edd21aca
AM
2740
2741 while (1)
2742 {
30667bf3
AM
2743 asection *stub_sec;
2744
25f72752 2745 for (input_bfd = info->input_bfds, bfd_indx = 0;
30667bf3 2746 input_bfd != NULL;
25f72752 2747 input_bfd = input_bfd->link_next, bfd_indx++)
30667bf3
AM
2748 {
2749 Elf_Internal_Shdr *symtab_hdr;
2750
2751 /* We'll need the symbol table in a second. */
2752 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2753 if (symtab_hdr->sh_info == 0)
2754 continue;
2755
25f72752 2756 local_syms = all_local_syms[bfd_indx];
30667bf3
AM
2757
2758 /* Walk over each section attached to the input bfd. */
2759 for (section = input_bfd->sections;
2760 section != NULL;
25f72752 2761 section = section->next)
30667bf3
AM
2762 {
2763 Elf_Internal_Shdr *input_rel_hdr;
2764 Elf32_External_Rela *external_relocs, *erelaend, *erela;
2765 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2766
2767 /* If there aren't any relocs, then there's nothing more
2768 to do. */
2769 if ((section->flags & SEC_RELOC) == 0
2770 || section->reloc_count == 0)
2771 continue;
2772
25f72752
AM
2773 /* If this section is a link-once section that will be
2774 discarded, then don't create any stubs. */
2775 if (section->output_section == NULL
2776 || section->output_section->owner != output_bfd)
2777 continue;
2778
30667bf3
AM
2779 /* Allocate space for the external relocations. */
2780 external_relocs
2781 = ((Elf32_External_Rela *)
2782 bfd_malloc (section->reloc_count
2783 * sizeof (Elf32_External_Rela)));
2784 if (external_relocs == NULL)
2785 {
2786 goto error_ret_free_local;
2787 }
2788
2789 /* Likewise for the internal relocations. */
2790 internal_relocs = ((Elf_Internal_Rela *)
2791 bfd_malloc (section->reloc_count
2792 * sizeof (Elf_Internal_Rela)));
2793 if (internal_relocs == NULL)
2794 {
2795 free (external_relocs);
2796 goto error_ret_free_local;
2797 }
2798
2799 /* Read in the external relocs. */
2800 input_rel_hdr = &elf_section_data (section)->rel_hdr;
2801 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
2802 || bfd_read (external_relocs, 1,
2803 input_rel_hdr->sh_size,
2804 input_bfd) != input_rel_hdr->sh_size)
2805 {
2806 free (external_relocs);
2807 error_ret_free_internal:
2808 free (internal_relocs);
2809 goto error_ret_free_local;
2810 }
2811
2812 /* Swap in the relocs. */
2813 erela = external_relocs;
2814 erelaend = erela + section->reloc_count;
2815 irela = internal_relocs;
2816 for (; erela < erelaend; erela++, irela++)
2817 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
2818
2819 /* We're done with the external relocs, free them. */
2820 free (external_relocs);
2821
2822 /* Now examine each relocation. */
2823 irela = internal_relocs;
2824 irelaend = irela + section->reloc_count;
2825 for (; irela < irelaend; irela++)
2826 {
2827 unsigned int r_type, r_indx;
2828 enum elf32_hppa_stub_type stub_type;
2829 struct elf32_hppa_stub_hash_entry *stub_entry;
2830 asection *sym_sec;
2831 bfd_vma sym_value;
2832 bfd_vma destination;
2833 struct elf32_hppa_link_hash_entry *hash;
2834 char *stub_name;
25f72752 2835 const asection *id_sec;
30667bf3
AM
2836
2837 r_type = ELF32_R_TYPE (irela->r_info);
2838 r_indx = ELF32_R_SYM (irela->r_info);
2839
2840 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2841 {
2842 bfd_set_error (bfd_error_bad_value);
2843 goto error_ret_free_internal;
2844 }
2845
2846 /* Only look for stubs on call instructions. */
2847 if (r_type != (unsigned int) R_PARISC_PCREL12F
2848 && r_type != (unsigned int) R_PARISC_PCREL17F
2849 && r_type != (unsigned int) R_PARISC_PCREL22F)
2850 continue;
2851
2852 /* Now determine the call target, its name, value,
2853 section. */
2854 sym_sec = NULL;
2855 sym_value = 0;
2856 destination = 0;
2857 hash = NULL;
2858 if (r_indx < symtab_hdr->sh_info)
2859 {
2860 /* It's a local symbol. */
2861 Elf_Internal_Sym *sym;
2862 Elf_Internal_Shdr *hdr;
2863
2864 sym = local_syms + r_indx;
2865 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2866 sym_sec = hdr->bfd_section;
2867 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2868 sym_value = sym->st_value;
2869 destination = (sym_value + irela->r_addend
2870 + sym_sec->output_offset
2871 + sym_sec->output_section->vma);
2872 }
2873 else
2874 {
2875 /* It's an external symbol. */
2876 int e_indx;
2877
2878 e_indx = r_indx - symtab_hdr->sh_info;
2879 hash = ((struct elf32_hppa_link_hash_entry *)
2880 elf_sym_hashes (input_bfd)[e_indx]);
2881
2882 while (hash->elf.root.type == bfd_link_hash_indirect
2883 || hash->elf.root.type == bfd_link_hash_warning)
2884 hash = ((struct elf32_hppa_link_hash_entry *)
2885 hash->elf.root.u.i.link);
2886
2887 if (hash->elf.root.type == bfd_link_hash_defined
2888 || hash->elf.root.type == bfd_link_hash_defweak)
2889 {
2890 sym_sec = hash->elf.root.u.def.section;
2891 sym_value = hash->elf.root.u.def.value;
2892 if (sym_sec->output_section != NULL)
2893 destination = (sym_value + irela->r_addend
2894 + sym_sec->output_offset
2895 + sym_sec->output_section->vma);
2896 }
2897 else if (hash->elf.root.type == bfd_link_hash_undefweak)
2898 {
2899 if (! info->shared)
2900 continue;
2901 }
2902 else if (hash->elf.root.type == bfd_link_hash_undefined)
2903 {
2904 if (! (info->shared
2905 && !info->no_undefined
2906 && (ELF_ST_VISIBILITY (hash->elf.other)
2907 == STV_DEFAULT)))
2908 continue;
2909 }
2910 else
2911 {
2912 bfd_set_error (bfd_error_bad_value);
2913 goto error_ret_free_internal;
2914 }
2915 }
2916
2917 /* Determine what (if any) linker stub is needed. */
2918 stub_type = hppa_type_of_stub (section, irela, hash,
2919 destination);
2920 if (stub_type == hppa_stub_none)
2921 continue;
2922
25f72752
AM
2923 /* Support for grouping stub sections. */
2924 id_sec = hplink->stub_group[section->id].link_sec;
2925
30667bf3 2926 /* Get the name of this stub. */
25f72752 2927 stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
30667bf3
AM
2928 if (!stub_name)
2929 goto error_ret_free_internal;
2930
2931 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2932 stub_name,
2933 false, false);
2934 if (stub_entry != NULL)
2935 {
2936 /* The proper stub has already been created. */
2937 free (stub_name);
2938 continue;
2939 }
2940
25f72752 2941 stub_entry = hppa_add_stub (stub_name, section, hplink);
30667bf3
AM
2942 if (stub_entry == NULL)
2943 {
2944 free (stub_name);
2945 goto error_ret_free_local;
2946 }
2947
2948 stub_entry->target_value = sym_value;
2949 stub_entry->target_section = sym_sec;
2950 stub_entry->stub_type = stub_type;
2951 if (info->shared)
2952 {
2953 if (stub_type == hppa_stub_import)
2954 stub_entry->stub_type = hppa_stub_import_shared;
2955 else if (stub_type == hppa_stub_long_branch
2956 && (LONG_BRANCH_PIC_IN_SHLIB || hash == NULL))
2957 stub_entry->stub_type = hppa_stub_long_branch_shared;
2958 }
2959 stub_entry->h = hash;
2960 stub_changed = 1;
2961 }
2962
2963 /* We're done with the internal relocs, free them. */
2964 free (internal_relocs);
2965 }
2966 }
2967
2968 if (!stub_changed)
2969 break;
2970
2971 /* OK, we've added some stubs. Find out the new size of the
2972 stub sections. */
30667bf3
AM
2973 for (stub_sec = hplink->stub_bfd->sections;
2974 stub_sec != NULL;
2975 stub_sec = stub_sec->next)
2976 {
74d1c347
AM
2977 stub_sec->_raw_size = 0;
2978 stub_sec->_cooked_size = 0;
2979 }
2980#if ! LONG_BRANCH_PIC_IN_SHLIB
25f72752
AM
2981 {
2982 int i;
2983
2984 for (i = top_id; i >= 0; --i)
2985 {
2986 /* This will probably hit the same section many times.. */
2987 stub_sec = hplink->stub_group[i].reloc_sec;
2988 if (stub_sec != NULL)
2989 {
2990 stub_sec->_raw_size = 0;
2991 stub_sec->_cooked_size = 0;
2992 }
2993 }
2994 }
74d1c347
AM
2995#endif
2996
2997 bfd_hash_traverse (&hplink->stub_hash_table,
2998 hppa_size_one_stub,
2999 hplink);
3000
30667bf3
AM
3001 /* Ask the linker to do its stuff. */
3002 (*hplink->layout_sections_again) ();
3003 stub_changed = 0;
3004 }
3005
25f72752 3006 ret = 1;
30667bf3
AM
3007
3008 error_ret_free_local:
25f72752
AM
3009 while (bfd_count-- > 0)
3010 if (all_local_syms[bfd_count])
3011 free (all_local_syms[bfd_count]);
30667bf3
AM
3012 free (all_local_syms);
3013
25f72752 3014 return ret;
30667bf3
AM
3015}
3016
3017
3018/* For a final link, this function is called after we have sized the
3019 stubs to provide a value for __gp. */
3020
3021boolean
3022elf32_hppa_set_gp (abfd, info)
3023 bfd *abfd;
3024 struct bfd_link_info *info;
3025{
74d1c347 3026 struct elf32_hppa_link_hash_table *hplink;
30667bf3
AM
3027 struct elf_link_hash_entry *h;
3028 asection *sec;
3029 bfd_vma gp_val;
3030
74d1c347
AM
3031 hplink = hppa_link_hash_table (info);
3032 h = elf_link_hash_lookup (&hplink->root, "$global$",
30667bf3
AM
3033 false, false, false);
3034
3035 if (h != NULL && h->root.type == bfd_link_hash_defined)
3036 {
3037 gp_val = h->root.u.def.value;
3038 sec = h->root.u.def.section;
3039 }
3040 else
3041 {
74d1c347
AM
3042 /* Choose to point our LTP at, in this order, one of .plt, .got,
3043 or .data, if these sections exist. In the case of choosing
3044 .plt try to make the LTP ideal for addressing anywhere in the
3045 .plt or .got with a 14 bit signed offset. Typically, the end
3046 of the .plt is the start of the .got, so choose .plt + 0x2000
3047 if either the .plt or .got is larger than 0x2000. If both
3048 the .plt and .got are smaller than 0x2000, choose the end of
3049 the .plt section. */
3050
3051 sec = hplink->splt;
3052 if (sec != NULL)
30667bf3 3053 {
74d1c347
AM
3054 gp_val = sec->_raw_size;
3055 if (gp_val > 0x2000
3056 || (hplink->sgot && hplink->sgot->_raw_size > 0x2000))
3057 {
3058 gp_val = 0x2000;
3059 }
3060 }
3061 else
3062 {
3063 gp_val = 0;
3064 sec = hplink->sgot;
3065 if (sec != NULL)
3066 {
3067 /* We know we don't have a .plt. If .got is large,
3068 offset our LTP. */
3069 if (sec->_raw_size > 0x2000)
3070 gp_val = 0x2000;
3071 }
3072 else
3073 {
3074 /* No .plt or .got. Who cares what the LTP is? */
3075 sec = bfd_get_section_by_name (abfd, ".data");
3076 }
30667bf3
AM
3077 }
3078 }
3079
74d1c347
AM
3080 if (sec != NULL)
3081 gp_val += sec->output_section->vma + sec->output_offset;
3082
3083 elf_gp (abfd) = gp_val;
30667bf3
AM
3084 return true;
3085}
3086
3087
3088/* Build all the stubs associated with the current output file. The
3089 stubs are kept in a hash table attached to the main linker hash
3090 table. We also set up the .plt entries for statically linked PIC
3091 functions here. This function is called via hppaelf_finish in the
3092 linker. */
3093
3094boolean
3095elf32_hppa_build_stubs (info)
3096 struct bfd_link_info *info;
3097{
3098 asection *stub_sec;
3099 struct bfd_hash_table *table;
3100 struct elf32_hppa_link_hash_table *hplink;
3101
3102 hplink = hppa_link_hash_table (info);
3103
3104 for (stub_sec = hplink->stub_bfd->sections;
3105 stub_sec != NULL;
3106 stub_sec = stub_sec->next)
3107 {
74d1c347 3108 size_t size;
30667bf3
AM
3109
3110 /* Allocate memory to hold the linker stubs. */
74d1c347 3111 size = stub_sec->_raw_size;
30667bf3
AM
3112 stub_sec->contents = (unsigned char *) bfd_zalloc (hplink->stub_bfd,
3113 size);
3114 if (stub_sec->contents == NULL && size != 0)
3115 return false;
74d1c347 3116 stub_sec->_raw_size = 0;
30667bf3
AM
3117 }
3118
3119 /* Build the stubs as directed by the stub hash table. */
30667bf3
AM
3120 table = &hplink->stub_hash_table;
3121 bfd_hash_traverse (table, hppa_build_one_stub, info);
3122
3123 return true;
3124}
3125
3126
3127/* Perform a relocation as part of a final link. */
3128
3129static bfd_reloc_status_type
25f72752 3130final_link_relocate (input_section, contents, rel, value, hplink, sym_sec, h)
30667bf3
AM
3131 asection *input_section;
3132 bfd_byte *contents;
3133 const Elf_Internal_Rela *rel;
3134 bfd_vma value;
25f72752 3135 struct elf32_hppa_link_hash_table *hplink;
30667bf3
AM
3136 asection *sym_sec;
3137 struct elf32_hppa_link_hash_entry *h;
3138{
3139 int insn;
3140 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3141 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3142 int r_format = howto->bitsize;
3143 enum hppa_reloc_field_selector_type_alt r_field;
3144 bfd *input_bfd = input_section->owner;
3145 bfd_vma offset = rel->r_offset;
3146 bfd_vma max_branch_offset = 0;
3147 bfd_byte *hit_data = contents + offset;
3148 bfd_signed_vma addend = rel->r_addend;
3149 bfd_vma location;
3150 struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
3151 int val;
3152
3153 if (r_type == R_PARISC_NONE)
3154 return bfd_reloc_ok;
3155
3156 insn = bfd_get_32 (input_bfd, hit_data);
3157
3158 /* Find out where we are and where we're going. */
3159 location = (offset +
3160 input_section->output_offset +
3161 input_section->output_section->vma);
3162
3163 switch (r_type)
3164 {
3165 case R_PARISC_PCREL12F:
3166 case R_PARISC_PCREL17F:
3167 case R_PARISC_PCREL22F:
3168 /* If this is a call to a function defined in another dynamic
3169 library, or if it is a call to a PIC function in the same
74d1c347
AM
3170 object, or if this is a shared link and it is a call to a
3171 weak symbol which may or may not be in the same object, then
3172 find the import stub in the stub hash. */
30667bf3
AM
3173 if (sym_sec == NULL
3174 || sym_sec->output_section == NULL
74d1c347
AM
3175 || (h != NULL &&
3176 (h->pic_call
3177 || (h->elf.root.type == bfd_link_hash_defweak
3178 && h->elf.dynindx != -1
3179 && h->elf.plt.offset != (bfd_vma) -1))))
30667bf3
AM
3180 {
3181 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
25f72752 3182 h, rel, hplink);
30667bf3
AM
3183 if (stub_entry != NULL)
3184 {
3185 value = (stub_entry->stub_offset
3186 + stub_entry->stub_sec->output_offset
3187 + stub_entry->stub_sec->output_section->vma);
3188 addend = 0;
3189 }
3190 else if (sym_sec == NULL && h != NULL
3191 && h->elf.root.type == bfd_link_hash_undefweak)
3192 {
3193 /* It's OK if undefined weak. Make undefined weak
3194 branches go nowhere. */
3195 value = location;
3196 addend = 0;
3197 }
3198 else
3199 return bfd_reloc_notsupported;
3200 }
3201 /* Fall thru. */
3202
3203 case R_PARISC_PCREL21L:
3204 case R_PARISC_PCREL17C:
3205 case R_PARISC_PCREL17R:
3206 case R_PARISC_PCREL14R:
3207 case R_PARISC_PCREL14F:
3208 /* Make it a pc relative offset. */
3209 value -= location;
3210 addend -= 8;
3211 break;
3212
3213 case R_PARISC_DPREL21L:
3214 case R_PARISC_DPREL14R:
3215 case R_PARISC_DPREL14F:
3216 /* For all the DP relative relocations, we need to examine the symbol's
3217 section. If it's a code section, then "data pointer relative" makes
3218 no sense. In that case we don't adjust the "value", and for 21 bit
3219 addil instructions, we change the source addend register from %dp to
3220 %r0. This situation commonly arises when a variable's "constness"
3221 is declared differently from the way the variable is defined. For
3222 instance: "extern int foo" with foo defined as "const int foo". */
3223 if (sym_sec == NULL)
3224 break;
3225 if ((sym_sec->flags & SEC_CODE) != 0)
3226 {
3227 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3228 == (((int) OP_ADDIL << 26) | (27 << 21)))
3229 {
3230 insn &= ~ (0x1f << 21);
74d1c347 3231#if 1 /* debug them. */
30667bf3
AM
3232 (*_bfd_error_handler)
3233 (_("%s(%s+0x%lx): fixing %s"),
3234 bfd_get_filename (input_bfd),
3235 input_section->name,
3236 (long) rel->r_offset,
3237 howto->name);
3238#endif
3239 }
3240 /* Now try to make things easy for the dynamic linker. */
3241
3242 break;
3243 }
74d1c347 3244 /* Fall thru. */
30667bf3
AM
3245
3246 case R_PARISC_DLTIND21L:
3247 case R_PARISC_DLTIND14R:
3248 case R_PARISC_DLTIND14F:
3249 value -= elf_gp (input_section->output_section->owner);
3250 break;
3251
3252 default:
3253 break;
3254 }
3255
3256 switch (r_type)
3257 {
3258 case R_PARISC_DIR32:
3259 case R_PARISC_DIR17F:
3260 case R_PARISC_PCREL17C:
3261 case R_PARISC_PCREL14F:
3262 case R_PARISC_DPREL14F:
3263 case R_PARISC_PLABEL32:
3264 case R_PARISC_DLTIND14F:
3265 case R_PARISC_SEGBASE:
3266 case R_PARISC_SEGREL32:
3267 r_field = e_fsel;
3268 break;
3269
3270 case R_PARISC_DIR21L:
3271 case R_PARISC_PCREL21L:
3272 case R_PARISC_DPREL21L:
3273 case R_PARISC_PLABEL21L:
3274 case R_PARISC_DLTIND21L:
3275 r_field = e_lrsel;
3276 break;
3277
3278 case R_PARISC_DIR17R:
3279 case R_PARISC_PCREL17R:
3280 case R_PARISC_DIR14R:
3281 case R_PARISC_PCREL14R:
3282 case R_PARISC_DPREL14R:
3283 case R_PARISC_PLABEL14R:
3284 case R_PARISC_DLTIND14R:
3285 r_field = e_rrsel;
3286 break;
3287
3288 case R_PARISC_PCREL12F:
3289 case R_PARISC_PCREL17F:
3290 case R_PARISC_PCREL22F:
3291 r_field = e_fsel;
3292
3293 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3294 {
3295 max_branch_offset = (1 << (17-1)) << 2;
3296 }
3297 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3298 {
3299 max_branch_offset = (1 << (12-1)) << 2;
3300 }
3301 else
3302 {
3303 max_branch_offset = (1 << (22-1)) << 2;
3304 }
3305
3306 /* sym_sec is NULL on undefined weak syms or when shared on
3307 undefined syms. We've already checked for a stub for the
3308 shared undefined case. */
3309 if (sym_sec == NULL)
3310 break;
3311
3312 /* If the branch is out of reach, then redirect the
3313 call to the local stub for this function. */
3314 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3315 {
3316 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
25f72752 3317 h, rel, hplink);
30667bf3
AM
3318 if (stub_entry == NULL)
3319 return bfd_reloc_notsupported;
3320
3321 /* Munge up the value and addend so that we call the stub
3322 rather than the procedure directly. */
3323 value = (stub_entry->stub_offset
3324 + stub_entry->stub_sec->output_offset
3325 + stub_entry->stub_sec->output_section->vma
3326 - location);
3327 addend = -8;
3328 }
3329 break;
3330
3331 /* Something we don't know how to handle. */
3332 default:
3333 return bfd_reloc_notsupported;
3334 }
3335
3336 /* Make sure we can reach the stub. */
3337 if (max_branch_offset != 0
3338 && value + addend + max_branch_offset >= 2*max_branch_offset)
3339 {
3340 (*_bfd_error_handler)
3341 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3342 bfd_get_filename (input_bfd),
3343 input_section->name,
3344 (long) rel->r_offset,
3345 stub_entry->root.string);
3346 return bfd_reloc_notsupported;
3347 }
3348
3349 val = hppa_field_adjust (value, addend, r_field);
3350
3351 switch (r_type)
3352 {
3353 case R_PARISC_PCREL12F:
3354 case R_PARISC_PCREL17C:
3355 case R_PARISC_PCREL17F:
3356 case R_PARISC_PCREL17R:
3357 case R_PARISC_PCREL22F:
3358 case R_PARISC_DIR17F:
3359 case R_PARISC_DIR17R:
3360 /* This is a branch. Divide the offset by four.
3361 Note that we need to decide whether it's a branch or
3362 otherwise by inspecting the reloc. Inspecting insn won't
3363 work as insn might be from a .word directive. */
3364 val >>= 2;
3365 break;
3366
3367 default:
3368 break;
3369 }
3370
3371 insn = hppa_rebuild_insn (insn, val, r_format);
3372
3373 /* Update the instruction word. */
74d1c347 3374 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
30667bf3
AM
3375 return bfd_reloc_ok;
3376}
3377
3378
3379/* Relocate an HPPA ELF section. */
3380
3381static boolean
3382elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
3383 contents, relocs, local_syms, local_sections)
3384 bfd *output_bfd;
3385 struct bfd_link_info *info;
3386 bfd *input_bfd;
3387 asection *input_section;
3388 bfd_byte *contents;
3389 Elf_Internal_Rela *relocs;
3390 Elf_Internal_Sym *local_syms;
3391 asection **local_sections;
3392{
3393 bfd *dynobj;
3394 bfd_vma *local_got_offsets;
3395 struct elf32_hppa_link_hash_table *hplink;
3396 Elf_Internal_Shdr *symtab_hdr;
3397 Elf_Internal_Rela *rel;
3398 Elf_Internal_Rela *relend;
3399 asection *sreloc;
3400
3401 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3402
30667bf3 3403 hplink = hppa_link_hash_table (info);
74d1c347
AM
3404 dynobj = hplink->root.dynobj;
3405 local_got_offsets = elf_local_got_offsets (input_bfd);
30667bf3
AM
3406 sreloc = NULL;
3407
3408 rel = relocs;
3409 relend = relocs + input_section->reloc_count;
3410 for (; rel < relend; rel++)
3411 {
3412 unsigned int r_type;
3413 reloc_howto_type *howto;
3414 unsigned int r_symndx;
3415 struct elf32_hppa_link_hash_entry *h;
3416 Elf_Internal_Sym *sym;
3417 asection *sym_sec;
3418 bfd_vma relocation;
3419 bfd_reloc_status_type r;
3420 const char *sym_name;
74d1c347 3421 boolean plabel;
30667bf3
AM
3422
3423 r_type = ELF32_R_TYPE (rel->r_info);
3424 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3425 {
3426 bfd_set_error (bfd_error_bad_value);
3427 return false;
3428 }
3429 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3430 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3431 continue;
3432
3433 r_symndx = ELF32_R_SYM (rel->r_info);
3434
3435 if (info->relocateable)
3436 {
3437 /* This is a relocateable link. We don't have to change
3438 anything, unless the reloc is against a section symbol,
3439 in which case we have to adjust according to where the
3440 section symbol winds up in the output section. */
3441 if (r_symndx < symtab_hdr->sh_info)
3442 {
3443 sym = local_syms + r_symndx;
3444 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3445 {
3446 sym_sec = local_sections[r_symndx];
3447 rel->r_addend += sym_sec->output_offset;
3448 }
3449 }
3450 continue;
3451 }
3452
3453 /* This is a final link. */
3454 h = NULL;
3455 sym = NULL;
3456 sym_sec = NULL;
3457 if (r_symndx < symtab_hdr->sh_info)
3458 {
3459 /* This is a local symbol, h defaults to NULL. */
3460 sym = local_syms + r_symndx;
3461 sym_sec = local_sections[r_symndx];
3462 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
3463 ? 0 : sym->st_value)
3464 + sym_sec->output_offset
3465 + sym_sec->output_section->vma);
3466 }
3467 else
3468 {
3469 int indx;
3470
3471 /* It's a global; Find its entry in the link hash. */
3472 indx = r_symndx - symtab_hdr->sh_info;
3473 h = ((struct elf32_hppa_link_hash_entry *)
3474 elf_sym_hashes (input_bfd)[indx]);
3475 while (h->elf.root.type == bfd_link_hash_indirect
3476 || h->elf.root.type == bfd_link_hash_warning)
3477 h = (struct elf32_hppa_link_hash_entry *) h->elf.root.u.i.link;
3478
3479 relocation = 0;
3480 if (h->elf.root.type == bfd_link_hash_defined
3481 || h->elf.root.type == bfd_link_hash_defweak)
3482 {
3483 sym_sec = h->elf.root.u.def.section;
3484 /* If sym_sec->output_section is NULL, then it's a
3485 symbol defined in a shared library. */
3486 if (sym_sec->output_section != NULL)
3487 relocation = (h->elf.root.u.def.value
3488 + sym_sec->output_offset
3489 + sym_sec->output_section->vma);
3490 }
3491 else if (h->elf.root.type == bfd_link_hash_undefweak)
3492 ;
3493 else if (info->shared && !info->no_undefined
3494 && ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT)
3495 {
3496 if (info->symbolic)
3497 if (!((*info->callbacks->undefined_symbol)
3498 (info, h->elf.root.root.string, input_bfd,
3499 input_section, rel->r_offset, false)))
3500 return false;
3501 }
3502 else
3503 {
3504 if (!((*info->callbacks->undefined_symbol)
3505 (info, h->elf.root.root.string, input_bfd,
3506 input_section, rel->r_offset, true)))
3507 return false;
3508 }
3509 }
3510
3511 /* Do any required modifications to the relocation value, and
25f72752
AM
3512 determine what types of dynamic info we need to output, if
3513 any. */
74d1c347 3514 plabel = 0;
30667bf3
AM
3515 switch (r_type)
3516 {
3517 case R_PARISC_DLTIND14F:
3518 case R_PARISC_DLTIND14R:
3519 case R_PARISC_DLTIND21L:
3520 /* Relocation is to the entry for this symbol in the global
3521 offset table. */
3522 if (h != NULL)
3523 {
3524 bfd_vma off;
3525
3526 off = h->elf.got.offset;
3527 BFD_ASSERT (off != (bfd_vma) -1);
3528
74d1c347 3529 if (! hplink->root.dynamic_sections_created
30667bf3
AM
3530 || (info->shared
3531 && (info->symbolic || h->elf.dynindx == -1)
3532 && (h->elf.elf_link_hash_flags
3533 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3534 {
3535 /* This is actually a static link, or it is a
3536 -Bsymbolic link and the symbol is defined
3537 locally, or the symbol was forced to be local
3538 because of a version file. We must initialize
3539 this entry in the global offset table. Since the
3540 offset must always be a multiple of 4, we use the
3541 least significant bit to record whether we have
3542 initialized it already.
3543
3544 When doing a dynamic link, we create a .rela.got
3545 relocation entry to initialize the value. This
3546 is done in the finish_dynamic_symbol routine. */
3547 if ((off & 1) != 0)
3548 off &= ~1;
3549 else
3550 {
3551 bfd_put_32 (output_bfd, relocation,
3552 hplink->sgot->contents + off);
3553 h->elf.got.offset |= 1;
3554 }
3555 }
3556
3557 relocation = off;
3558 }
3559 else
3560 {
3561 /* Local symbol case. */
3562 bfd_vma off;
3563
3564 BFD_ASSERT (local_got_offsets != NULL
3565 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3566
3567 off = local_got_offsets[r_symndx];
3568
3569 /* The offset must always be a multiple of 4. We use
3570 the least significant bit to record whether we have
3571 already generated the necessary reloc. */
3572 if ((off & 1) != 0)
3573 off &= ~1;
3574 else
3575 {
3576 bfd_put_32 (output_bfd, relocation,
3577 hplink->sgot->contents + off);
3578
3579 if (info->shared)
3580 {
74d1c347 3581 /* Output a dynamic *ABS* relocation for this
30667bf3
AM
3582 GOT entry. In this case it is relative to
3583 the base of the object because the symbol
3584 index is zero. */
3585 Elf_Internal_Rela outrel;
3586 asection *srelgot = hplink->srelgot;
3587
3588 outrel.r_offset = (off
3589 + hplink->sgot->output_offset
3590 + hplink->sgot->output_section->vma);
74d1c347 3591 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
30667bf3
AM
3592 outrel.r_addend = relocation;
3593 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3594 ((Elf32_External_Rela *)
3595 srelgot->contents
3596 + srelgot->reloc_count));
3597 ++srelgot->reloc_count;
3598 }
252b5132 3599
30667bf3
AM
3600 local_got_offsets[r_symndx] |= 1;
3601 }
252b5132 3602
30667bf3
AM
3603 relocation = off;
3604 }
252b5132 3605
30667bf3
AM
3606 /* Add the base of the GOT to the relocation value. */
3607 relocation += (hplink->sgot->output_offset
3608 + hplink->sgot->output_section->vma);
3609 break;
252b5132 3610
30667bf3
AM
3611 case R_PARISC_PLABEL14R:
3612 case R_PARISC_PLABEL21L:
3613 case R_PARISC_PLABEL32:
74d1c347 3614 if (hplink->root.dynamic_sections_created)
252b5132 3615 {
74d1c347
AM
3616 bfd_vma off;
3617
3618 /* If we have a global symbol with a PLT slot, then
3619 redirect this relocation to it. */
3620 if (h != NULL)
3621 {
3622 off = h->elf.plt.offset;
3623 }
3624 else
3625 {
3626 int indx;
3627
3628 indx = r_symndx + symtab_hdr->sh_info;
3629 off = local_got_offsets[indx];
3630
3631 /* As for the local .got entry case, we use the last
3632 bit to record whether we've already initialised
3633 this local .plt entry. */
3634 if ((off & 1) != 0)
3635 off &= ~1;
3636 else
3637 {
3638 bfd_put_32 (output_bfd,
3639 relocation,
3640 hplink->splt->contents + off);
3641 bfd_put_32 (output_bfd,
3642 elf_gp (hplink->splt->output_section->owner),
3643 hplink->splt->contents + off + 4);
3644
3645 if (info->shared)
3646 {
3647 /* Output a dynamic IPLT relocation for this
3648 PLT entry. */
3649 Elf_Internal_Rela outrel;
3650 asection *srelplt = hplink->srelplt;
3651
3652 outrel.r_offset = (off
3653 + hplink->splt->output_offset
3654 + hplink->splt->output_section->vma);
3655 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3656 outrel.r_addend = relocation;
3657 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3658 ((Elf32_External_Rela *)
3659 srelplt->contents
3660 + srelplt->reloc_count));
3661 ++srelplt->reloc_count;
3662 }
3663
3664 local_got_offsets[indx] |= 1;
3665 }
3666 }
3667
3668 BFD_ASSERT (off < (bfd_vma) -2);
3669
3670 /* PLABELs contain function pointers. Relocation is to
3671 the entry for the function in the .plt. The magic +2
3672 offset signals to $$dyncall that the function pointer
3673 is in the .plt and thus has a gp pointer too.
3674 Exception: Undefined PLABELs should have a value of
3675 zero. */
3676 if (h == NULL
3677 || (h->elf.root.type != bfd_link_hash_undefweak
3678 && h->elf.root.type != bfd_link_hash_undefined))
3679 {
3680 relocation = (off
3681 + hplink->splt->output_offset
3682 + hplink->splt->output_section->vma
3683 + 2);
3684 }
3685 plabel = 1;
30667bf3
AM
3686 }
3687 /* Fall through and possibly emit a dynamic relocation. */
3688
3689 case R_PARISC_DIR17F:
3690 case R_PARISC_DIR17R:
3691 case R_PARISC_DIR14R:
3692 case R_PARISC_DIR21L:
3693 case R_PARISC_DPREL14F:
3694 case R_PARISC_DPREL14R:
3695 case R_PARISC_DPREL21L:
3696 case R_PARISC_DIR32:
3697 /* The reloc types handled here and this conditional
3698 expression must match the code in check_relocs and
3699 hppa_discard_copies. ie. We need exactly the same
3700 condition as in check_relocs, with some extra conditions
3701 (dynindx test in this case) to cater for relocs removed
3702 by hppa_discard_copies. */
3703 if ((input_section->flags & SEC_ALLOC) != 0
3704 && info->shared
3705#if RELATIVE_DYNAMIC_RELOCS
3706 && (is_absolute_reloc (r_type)
3707 || ((!info->symbolic
3708 || (h != NULL
3709 && ((h->elf.elf_link_hash_flags
3710 & ELF_LINK_HASH_DEF_REGULAR) == 0
3711 || h->elf.root.type == bfd_link_hash_defweak)))
3712 && (h == NULL || h->elf.dynindx != -1)))
3713#endif
3714 )
3715 {
3716 Elf_Internal_Rela outrel;
3717 boolean skip;
252b5132 3718
30667bf3
AM
3719 /* When generating a shared object, these relocations
3720 are copied into the output file to be resolved at run
3721 time. */
252b5132 3722
30667bf3 3723 if (sreloc == NULL)
edd21aca 3724 {
30667bf3
AM
3725 const char *name;
3726
3727 name = (bfd_elf_string_from_elf_section
3728 (input_bfd,
3729 elf_elfheader (input_bfd)->e_shstrndx,
3730 elf_section_data (input_section)->rel_hdr.sh_name));
3731 if (name == NULL)
3732 return false;
3733 sreloc = bfd_get_section_by_name (dynobj, name);
3734 BFD_ASSERT (sreloc != NULL);
edd21aca 3735 }
252b5132 3736
30667bf3
AM
3737 outrel.r_offset = rel->r_offset;
3738 outrel.r_addend = rel->r_addend;
3739 skip = false;
3740 if (elf_section_data (input_section)->stab_info != NULL)
edd21aca 3741 {
30667bf3
AM
3742 bfd_vma off;
3743
3744 off = (_bfd_stab_section_offset
74d1c347 3745 (output_bfd, &hplink->root.stab_info,
30667bf3
AM
3746 input_section,
3747 &elf_section_data (input_section)->stab_info,
3748 rel->r_offset));
3749 if (off == (bfd_vma) -1)
3750 skip = true;
3751 outrel.r_offset = off;
edd21aca 3752 }
252b5132 3753
30667bf3
AM
3754 outrel.r_offset += (input_section->output_offset
3755 + input_section->output_section->vma);
3756
3757 if (skip)
252b5132 3758 {
30667bf3 3759 memset (&outrel, 0, sizeof (outrel));
252b5132 3760 }
74d1c347
AM
3761 else if (h != NULL
3762 && h->elf.dynindx != -1
3763 && (plabel
3764 || !info->symbolic
30667bf3
AM
3765 || (h->elf.elf_link_hash_flags
3766 & ELF_LINK_HASH_DEF_REGULAR) == 0))
252b5132 3767 {
30667bf3
AM
3768 outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3769 }
3770 else /* It's a local symbol, or one marked to become local. */
3771 {
3772 int indx = 0;
edd21aca 3773
30667bf3
AM
3774 /* Add the absolute offset of the symbol. */
3775 outrel.r_addend += relocation;
edd21aca 3776
74d1c347
AM
3777 /* Global plabels need to be processed by the
3778 dynamic linker so that functions have at most one
3779 fptr. For this reason, we need to differentiate
3780 between global and local plabels, which we do by
3781 providing the function symbol for a global plabel
3782 reloc, and no symbol for local plabels. */
3783 if (! plabel
3784 && sym_sec != NULL
30667bf3
AM
3785 && sym_sec->output_section != NULL
3786 && ! bfd_is_abs_section (sym_sec))
252b5132 3787 {
30667bf3
AM
3788 indx = elf_section_data (sym_sec->output_section)->dynindx;
3789 /* We are turning this relocation into one
3790 against a section symbol, so subtract out the
3791 output section's address but not the offset
3792 of the input section in the output section. */
3793 outrel.r_addend -= sym_sec->output_section->vma;
252b5132 3794 }
252b5132 3795
30667bf3
AM
3796 outrel.r_info = ELF32_R_INFO (indx, r_type);
3797 }
edd21aca 3798
30667bf3
AM
3799 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3800 ((Elf32_External_Rela *)
3801 sreloc->contents
3802 + sreloc->reloc_count));
3803 ++sreloc->reloc_count;
3804 }
3805 break;
edd21aca 3806
30667bf3
AM
3807 default:
3808 break;
3809 }
252b5132 3810
30667bf3 3811 r = final_link_relocate (input_section, contents, rel, relocation,
25f72752 3812 hplink, sym_sec, h);
252b5132 3813
30667bf3
AM
3814 if (r == bfd_reloc_ok)
3815 continue;
252b5132 3816
30667bf3
AM
3817 if (h != NULL)
3818 sym_name = h->elf.root.root.string;
3819 else
3820 {
3821 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3822 symtab_hdr->sh_link,
3823 sym->st_name);
3824 if (sym_name == NULL)
3825 return false;
3826 if (*sym_name == '\0')
3827 sym_name = bfd_section_name (input_bfd, sym_sec);
3828 }
edd21aca 3829
30667bf3 3830 howto = elf_hppa_howto_table + r_type;
252b5132 3831
30667bf3
AM
3832 if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
3833 {
3834 (*_bfd_error_handler)
3835 (_("%s(%s+0x%lx): cannot handle %s for %s"),
3836 bfd_get_filename (input_bfd),
3837 input_section->name,
3838 (long) rel->r_offset,
3839 howto->name,
3840 sym_name);
3841 }
3842 else
3843 {
3844 if (!((*info->callbacks->reloc_overflow)
3845 (info, sym_name, howto->name, (bfd_vma) 0,
3846 input_bfd, input_section, rel->r_offset)))
3847 return false;
3848 }
3849 }
edd21aca 3850
30667bf3
AM
3851 return true;
3852}
252b5132 3853
edd21aca 3854
30667bf3
AM
3855/* Finish up dynamic symbol handling. We set the contents of various
3856 dynamic sections here. */
252b5132 3857
30667bf3
AM
3858static boolean
3859elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
3860 bfd *output_bfd;
3861 struct bfd_link_info *info;
3862 struct elf_link_hash_entry *h;
3863 Elf_Internal_Sym *sym;
3864{
3865 struct elf32_hppa_link_hash_table *hplink;
3866 bfd *dynobj;
edd21aca 3867
30667bf3 3868 hplink = hppa_link_hash_table (info);
74d1c347 3869 dynobj = hplink->root.dynobj;
30667bf3 3870
30667bf3
AM
3871 if (h->plt.offset != (bfd_vma) -1)
3872 {
3873 bfd_vma value;
3874 Elf_Internal_Rela rel;
3875
3876 /* This symbol has an entry in the procedure linkage table. Set
3877 it up.
3878
3879 The format of a plt entry is
74d1c347
AM
3880 <funcaddr>
3881 <__gp>
3882 <used by ld.so>
3883
3884 The last field is present only for plt entries that are used
3885 by global plabels. */
30667bf3
AM
3886
3887 /* We do not actually care about the value in the PLT entry if
3888 we are creating a shared library and the symbol is still
3889 undefined; We create a dynamic relocation to fill in the
3890 correct value. */
3891 value = 0;
3892 if (h->root.type == bfd_link_hash_defined
3893 || h->root.type == bfd_link_hash_defweak)
3894 {
3895 value = h->root.u.def.value;
3896 if (h->root.u.def.section->output_section != NULL)
3897 value += (h->root.u.def.section->output_offset
3898 + h->root.u.def.section->output_section->vma);
252b5132 3899 }
edd21aca 3900
74d1c347
AM
3901 bfd_put_32 (hplink->splt->owner,
3902 value,
30667bf3 3903 hplink->splt->contents + h->plt.offset);
74d1c347
AM
3904 bfd_put_32 (hplink->splt->owner,
3905 elf_gp (hplink->splt->output_section->owner),
30667bf3 3906 hplink->splt->contents + h->plt.offset + 4);
74d1c347
AM
3907 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
3908 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
30667bf3 3909 && h->dynindx != -1)
74d1c347
AM
3910 {
3911 memset (hplink->splt->contents + h->plt.offset + 8,
3912 0, PLABEL_PLT_ENTRY_SIZE - PLT_ENTRY_SIZE);
3913 }
3914
3915 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
30667bf3
AM
3916 {
3917 /* Create a dynamic IPLT relocation for this entry. */
3918 rel.r_offset = (h->plt.offset
3919 + hplink->splt->output_offset
3920 + hplink->splt->output_section->vma);
74d1c347
AM
3921 if (! ((struct elf32_hppa_link_hash_entry *) h)->plt_abs
3922 && h->dynindx != -1)
3923 {
3924 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
3925 rel.r_addend = 0;
3926 }
3927 else
3928 {
3929 /* This symbol has been marked to become local, and is
3930 used by a plabel so must be kept in the .plt. */
3931 rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3932 rel.r_addend = value;
3933 }
30667bf3
AM
3934
3935 bfd_elf32_swap_reloca_out (hplink->splt->output_section->owner,
3936 &rel,
3937 ((Elf32_External_Rela *)
3938 hplink->srelplt->contents
3939 + hplink->srelplt->reloc_count));
3940 hplink->srelplt->reloc_count++;
3941 }
3942
3943 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3944 {
3945 /* Mark the symbol as undefined, rather than as defined in
3946 the .plt section. Leave the value alone. */
3947 sym->st_shndx = SHN_UNDEF;
3948 }
3949 }
edd21aca 3950
30667bf3
AM
3951 if (h->got.offset != (bfd_vma) -1)
3952 {
3953 Elf_Internal_Rela rel;
3954
3955 /* This symbol has an entry in the global offset table. Set it
3956 up. */
3957
3958 rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
3959 + hplink->sgot->output_offset
3960 + hplink->sgot->output_section->vma);
3961
3962 /* If this is a static link, or it is a -Bsymbolic link and the
3963 symbol is defined locally or was forced to be local because
3964 of a version file, we just want to emit a RELATIVE reloc.
3965 The entry in the global offset table will already have been
3966 initialized in the relocate_section function. */
74d1c347 3967 if (! hplink->root.dynamic_sections_created
30667bf3
AM
3968 || (info->shared
3969 && (info->symbolic || h->dynindx == -1)
3970 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
3971 {
74d1c347 3972 rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
30667bf3
AM
3973 rel.r_addend = (h->root.u.def.value
3974 + h->root.u.def.section->output_offset
3975 + h->root.u.def.section->output_section->vma);
3976 }
3977 else
3978 {
3979 BFD_ASSERT((h->got.offset & 1) == 0);
3980 bfd_put_32 (output_bfd, (bfd_vma) 0,
3981 hplink->sgot->contents + h->got.offset);
3982 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
3983 rel.r_addend = 0;
3984 }
edd21aca 3985
30667bf3
AM
3986 bfd_elf32_swap_reloca_out (output_bfd, &rel,
3987 ((Elf32_External_Rela *)
3988 hplink->srelgot->contents
3989 + hplink->srelgot->reloc_count));
3990 ++hplink->srelgot->reloc_count;
3991 }
edd21aca 3992
30667bf3
AM
3993 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3994 {
3995 asection *s;
3996 Elf_Internal_Rela rel;
3997
3998 /* This symbol needs a copy reloc. Set it up. */
3999
4000 BFD_ASSERT (h->dynindx != -1
4001 && (h->root.type == bfd_link_hash_defined
4002 || h->root.type == bfd_link_hash_defweak));
4003
4004 s = hplink->srelbss;
4005
4006 rel.r_offset = (h->root.u.def.value
4007 + h->root.u.def.section->output_offset
4008 + h->root.u.def.section->output_section->vma);
4009 rel.r_addend = 0;
4010 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
4011 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4012 ((Elf32_External_Rela *) s->contents
4013 + s->reloc_count));
4014 ++s->reloc_count;
4015 }
4016
4017 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4018 if (h->root.root.string[0] == '_'
4019 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4020 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
4021 {
4022 sym->st_shndx = SHN_ABS;
4023 }
4024
4025 return true;
4026}
4027
4028
4029/* Finish up the dynamic sections. */
4030
4031static boolean
4032elf32_hppa_finish_dynamic_sections (output_bfd, info)
4033 bfd *output_bfd;
4034 struct bfd_link_info *info;
4035{
4036 bfd *dynobj;
4037 struct elf32_hppa_link_hash_table *hplink;
4038 asection *sdyn;
4039
30667bf3 4040 hplink = hppa_link_hash_table (info);
74d1c347 4041 dynobj = hplink->root.dynobj;
30667bf3
AM
4042
4043 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4044
74d1c347 4045 if (hplink->root.dynamic_sections_created)
30667bf3
AM
4046 {
4047 Elf32_External_Dyn *dyncon, *dynconend;
4048
4049 BFD_ASSERT (sdyn != NULL);
4050
4051 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4052 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4053 for (; dyncon < dynconend; dyncon++)
edd21aca 4054 {
30667bf3
AM
4055 Elf_Internal_Dyn dyn;
4056 asection *s;
4057
4058 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4059
4060 switch (dyn.d_tag)
4061 {
4062 default:
4063 break;
4064
4065 case DT_PLTGOT:
4066 /* Use PLTGOT to set the GOT register. */
4067 dyn.d_un.d_ptr = elf_gp (output_bfd);
4068 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4069 break;
4070
4071 case DT_JMPREL:
4072 s = hplink->srelplt;
4073 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4074 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4075 break;
4076
4077 case DT_PLTRELSZ:
4078 s = hplink->srelplt;
4079 if (s->_cooked_size != 0)
4080 dyn.d_un.d_val = s->_cooked_size;
4081 else
4082 dyn.d_un.d_val = s->_raw_size;
4083 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4084 break;
74d1c347
AM
4085
4086 case DT_INIT:
4087 case DT_FINI:
4088 {
4089 struct elf_link_hash_entry *h;
4090 const char *funcname;
4091
4092 if (dyn.d_tag == DT_INIT)
4093 funcname = info->init_function;
4094 else
4095 funcname = info->fini_function;
4096
4097 h = elf_link_hash_lookup (&hplink->root, funcname,
4098 false, false, false);
4099
4100 /* This is a function pointer. The magic +2 offset
4101 signals to $$dyncall that the function pointer
4102 is in the .plt and thus has a gp pointer too. */
4103 dyn.d_un.d_ptr = (h->plt.offset
4104 + hplink->splt->output_offset
4105 + hplink->splt->output_section->vma
4106 + 2);
4107 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4108 break;
4109 }
30667bf3 4110 }
edd21aca 4111 }
252b5132 4112 }
edd21aca 4113
30667bf3
AM
4114 if (hplink->sgot->_raw_size != 0)
4115 {
74d1c347
AM
4116 /* Fill in the first entry in the global offset table.
4117 We use it to point to our dynamic section, if we have one. */
30667bf3
AM
4118 bfd_put_32 (output_bfd,
4119 (sdyn != NULL
4120 ? sdyn->output_section->vma + sdyn->output_offset
4121 : (bfd_vma) 0),
4122 hplink->sgot->contents);
4123
74d1c347
AM
4124 /* The second entry is reserved for use by the dynamic linker. */
4125 bfd_put_32 (output_bfd, (bfd_vma) 0, hplink->sgot->contents + 4);
4126
30667bf3 4127 /* Set .got entry size. */
74d1c347
AM
4128 elf_section_data (hplink->sgot->output_section)
4129 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
30667bf3
AM
4130 }
4131
4132 /* Set plt entry size. */
4133 if (hplink->splt->_raw_size != 0)
74d1c347
AM
4134 elf_section_data (hplink->splt->output_section)
4135 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
30667bf3 4136
252b5132 4137 return true;
30667bf3 4138}
252b5132 4139
edd21aca 4140
30667bf3
AM
4141/* Called when writing out an object file to decide the type of a
4142 symbol. */
4143static int
4144elf32_hppa_elf_get_symbol_type (elf_sym, type)
4145 Elf_Internal_Sym *elf_sym;
4146 int type;
4147{
4148 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4149 return STT_PARISC_MILLI;
4150 else
4151 return type;
252b5132
RH
4152}
4153
30667bf3 4154
252b5132 4155/* Misc BFD support code. */
30667bf3
AM
4156#define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4157#define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4158#define elf_info_to_howto elf_hppa_info_to_howto
4159#define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
252b5132 4160
252b5132 4161/* Stuff for the BFD linker. */
30667bf3
AM
4162#define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
4163#define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4164#define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
4165#define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
4166#define elf_backend_check_relocs elf32_hppa_check_relocs
4167#define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4168#define elf_backend_fake_sections elf_hppa_fake_sections
4169#define elf_backend_relocate_section elf32_hppa_relocate_section
74d1c347 4170#define elf_backend_hide_symbol elf32_hppa_hide_symbol
30667bf3
AM
4171#define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4172#define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4173#define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4174#define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4175#define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
4176#define elf_backend_object_p elf32_hppa_object_p
4177#define elf_backend_final_write_processing elf_hppa_final_write_processing
4178#define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
4179
4180#define elf_backend_can_gc_sections 1
4181#define elf_backend_plt_alignment 2
4182#define elf_backend_want_got_plt 0
4183#define elf_backend_plt_readonly 0
4184#define elf_backend_want_plt_sym 0
74d1c347 4185#define elf_backend_got_header_size 8
252b5132
RH
4186
4187#define TARGET_BIG_SYM bfd_elf32_hppa_vec
4188#define TARGET_BIG_NAME "elf32-hppa"
4189#define ELF_ARCH bfd_arch_hppa
4190#define ELF_MACHINE_CODE EM_PARISC
4191#define ELF_MAXPAGESIZE 0x1000
4192
4193#include "elf32-target.h"
This page took 0.239385 seconds and 4 git commands to generate.