Fix a segfault caused by under-allocating an array.
[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
a017a724
AM
2443 /* Count the number of input BFDs, find the top input section id,
2444 and the top output section index. We can't use output_bfd
2445 section_count here to find the top output section index as some
2446 sections may have been removed, and _bfd_strip_section_from_output
2447 doesn't renumber the indices. Also, sections created by the
2448 linker aren't counted, and to make matters worse, aren't even on
2449 the output_bfd section list. We could probably just ignore
2450 sections created by the linker, but this way seems safer. */
2451 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0, top_index = 0;
30667bf3
AM
2452 input_bfd != NULL;
2453 input_bfd = input_bfd->link_next)
2454 {
2455 bfd_count += 1;
25f72752
AM
2456 for (section = input_bfd->sections;
2457 section != NULL;
2458 section = section->next)
2459 {
2460 if (top_id < section->id)
2461 top_id = section->id;
a017a724
AM
2462 if (section->output_section != NULL
2463 && section->output_section->owner == output_bfd
2464 && top_index < section->output_section->index)
2465 top_index = section->output_section->index;
25f72752 2466 }
30667bf3
AM
2467 }
2468
25f72752
AM
2469 hplink->stub_group
2470 = (struct map_stub *) bfd_zmalloc (sizeof (struct map_stub) * (top_id + 1));
2471 if (hplink->stub_group == NULL)
30667bf3
AM
2472 return false;
2473
a017a724 2474 /* Now make a list of input sections for each output section. */
25f72752
AM
2475 input_list
2476 = (asection **) bfd_zmalloc (sizeof (asection *) * (top_index + 1));
2477 if (input_list == NULL)
2478 return false;
2479
2480 for (input_bfd = info->input_bfds;
2481 input_bfd != NULL;
2482 input_bfd = input_bfd->link_next)
2483 {
2484 for (section = input_bfd->sections;
2485 section != NULL;
2486 section = section->next)
2487 {
2488 if (section->output_section != NULL
2489 && section->output_section->owner == output_bfd)
2490 {
2491 list = input_list + section->output_section->index;
2492 /* Steal the link_sec pointer for our list. */
2493#define PREV_SEC(sec) (hplink->stub_group[(sec)->id].link_sec)
2494 /* This happens to make the list in reverse order, which
2495 is what we want. */
2496 PREV_SEC (section) = *list;
2497 *list = section;
2498 }
2499 }
2500 }
2501
2502 /* See whether we can group stub sections together. Grouping stub
2503 sections may result in fewer stubs. More importantly, we need to
2504 put all .init* and .fini* stubs at the beginning of the .init or
2505 .fini output sections respectively, because glibc splits the
2506 _init and _fini functions into multiple parts. Putting a stub in
2507 the middle of a function is not a good idea. */
a017a724 2508 list = input_list + top_index;
25f72752
AM
2509 while (list-- != input_list)
2510 {
2511 asection *tail = *list;
2512 while (tail != NULL)
2513 {
2514 asection *curr;
2515 asection *prev;
2516 bfd_size_type total;
2517
2518 curr = tail;
2519 if (tail->_cooked_size)
2520 total = tail->_cooked_size;
2521 else
2522 total = tail->_raw_size;
2523 while ((prev = PREV_SEC (curr)) != NULL
2524 && ((total += curr->output_offset - prev->output_offset)
2525 < 250000))
2526 curr = prev;
2527
2528 /* OK, the size from the start of CURR to the end is less
2529 than 250000 bytes and thus can be handled by one stub
2530 section. (or the tail section is itself larger than
2531 250000 bytes, in which case we may be toast.)
2532 We should really be keeping track of the total size of
2533 stubs added here, as stubs contribute to the final output
2534 section size. That's a little tricky, and this way will
2535 only break if stubs added total more than 12144 bytes, or
2536 1518 long branch stubs. It seems unlikely for more than
2537 1518 different functions to be called, especially from
2538 code only 250000 bytes long. */
2539 do
2540 {
2541 prev = PREV_SEC (tail);
2542 /* Set up this stub group. */
2543 hplink->stub_group[tail->id].link_sec = curr;
2544 }
2545 while (tail != curr && (tail = prev) != NULL);
2546
2547 /* But wait, there's more! Input sections up to 250000
2548 bytes before the stub section can be handled by it too. */
2549 total = 0;
2550 while (prev != NULL
2551 && ((total += tail->output_offset - prev->output_offset)
2552 < 250000))
2553 {
2554 tail = prev;
2555 prev = PREV_SEC (tail);
2556 hplink->stub_group[tail->id].link_sec = curr;
2557 }
2558 tail = prev;
2559 }
2560 }
2561 free (input_list);
30667bf3
AM
2562
2563 /* We want to read in symbol extension records only once. To do this
2564 we need to read in the local symbols in parallel and save them for
2565 later use; so hold pointers to the local symbols in an array. */
2566 all_local_syms
2567 = (Elf_Internal_Sym **) bfd_zmalloc (sizeof (Elf_Internal_Sym *)
2568 * bfd_count);
2569 if (all_local_syms == NULL)
25f72752 2570 return false;
30667bf3
AM
2571
2572 /* Walk over all the input BFDs, swapping in local symbols.
2573 If we are creating a shared library, create hash entries for the
2574 export stubs. */
25f72752 2575 for (input_bfd = info->input_bfds, bfd_indx = 0;
30667bf3 2576 input_bfd != NULL;
25f72752 2577 input_bfd = input_bfd->link_next, bfd_indx++)
30667bf3
AM
2578 {
2579 Elf_Internal_Shdr *symtab_hdr;
2580 Elf_Internal_Sym *isym;
25f72752 2581 Elf32_External_Sym *ext_syms, *esym, *end_sy;
edd21aca 2582
252b5132
RH
2583 /* We'll need the symbol table in a second. */
2584 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2585 if (symtab_hdr->sh_info == 0)
2586 continue;
2587
edd21aca
AM
2588 /* We need an array of the local symbols attached to the input bfd.
2589 Unfortunately, we're going to have to read & swap them in. */
2590 local_syms = (Elf_Internal_Sym *)
2591 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf_Internal_Sym));
2592 if (local_syms == NULL)
2593 {
2594 goto error_ret_free_local;
2595 }
25f72752 2596 all_local_syms[bfd_indx] = local_syms;
edd21aca
AM
2597 ext_syms = (Elf32_External_Sym *)
2598 bfd_malloc (symtab_hdr->sh_info * sizeof (Elf32_External_Sym));
2599 if (ext_syms == NULL)
2600 {
2601 goto error_ret_free_local;
2602 }
2603
2604 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
30667bf3 2605 || (bfd_read (ext_syms, 1,
edd21aca
AM
2606 (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)),
2607 input_bfd)
2608 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2609 {
2610 free (ext_syms);
2611 goto error_ret_free_local;
2612 }
2613
2614 /* Swap the local symbols in. */
2615 isym = local_syms;
2616 esym = ext_syms;
25f72752 2617 for (end_sy = esym + symtab_hdr->sh_info; esym < end_sy; esym++, isym++)
edd21aca
AM
2618 bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
2619
2620 /* Now we can free the external symbols. */
2621 free (ext_syms);
edd21aca 2622
d5c73c2f 2623#if ! LONG_BRANCH_PIC_IN_SHLIB
25f72752
AM
2624 /* If this is a shared link, find all the stub reloc sections. */
2625 if (info->shared)
2626 for (section = input_bfd->sections;
2627 section != NULL;
2628 section = section->next)
2629 {
2630 char *name;
2631 asection *reloc_sec;
d5c73c2f 2632
25f72752
AM
2633 name = bfd_malloc (strlen (section->name)
2634 + sizeof STUB_SUFFIX
2635 + 5);
2636 if (name == NULL)
2637 return false;
2638 sprintf (name, ".rela%s%s", section->name, STUB_SUFFIX);
2639 reloc_sec = bfd_get_section_by_name (hplink->root.dynobj, name);
2640 hplink->stub_group[section->id].reloc_sec = reloc_sec;
2641 free (name);
2642 }
d5c73c2f 2643#endif
d5c73c2f 2644
30667bf3
AM
2645 if (info->shared && hplink->multi_subspace)
2646 {
25f72752
AM
2647 struct elf_link_hash_entry **sym_hashes;
2648 struct elf_link_hash_entry **end_hashes;
30667bf3
AM
2649 unsigned int symcount;
2650
2651 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2652 - symtab_hdr->sh_info);
25f72752
AM
2653 sym_hashes = elf_sym_hashes (input_bfd);
2654 end_hashes = sym_hashes + symcount;
30667bf3
AM
2655
2656 /* Look through the global syms for functions; We need to
2657 build export stubs for all globally visible functions. */
25f72752 2658 for (; sym_hashes < end_hashes; sym_hashes++)
30667bf3
AM
2659 {
2660 struct elf32_hppa_link_hash_entry *hash;
2661
25f72752 2662 hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
30667bf3
AM
2663
2664 while (hash->elf.root.type == bfd_link_hash_indirect
2665 || hash->elf.root.type == bfd_link_hash_warning)
2666 hash = ((struct elf32_hppa_link_hash_entry *)
2667 hash->elf.root.u.i.link);
2668
2669 /* At this point in the link, undefined syms have been
2670 resolved, so we need to check that the symbol was
2671 defined in this BFD. */
2672 if ((hash->elf.root.type == bfd_link_hash_defined
2673 || hash->elf.root.type == bfd_link_hash_defweak)
2674 && hash->elf.type == STT_FUNC
2675 && hash->elf.root.u.def.section->output_section != NULL
25f72752
AM
2676 && (hash->elf.root.u.def.section->output_section->owner
2677 == output_bfd)
30667bf3
AM
2678 && hash->elf.root.u.def.section->owner == input_bfd
2679 && (hash->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
2680 && !(hash->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2681 && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2682 {
2683 asection *sec;
2684 const char *stub_name;
2685 struct elf32_hppa_stub_hash_entry *stub_entry;
2686
2687 sec = hash->elf.root.u.def.section;
2688 stub_name = hash->elf.root.root.string;
2689 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2690 stub_name,
2691 false, false);
2692 if (stub_entry == NULL)
2693 {
25f72752 2694 stub_entry = hppa_add_stub (stub_name, sec, hplink);
30667bf3
AM
2695 if (!stub_entry)
2696 goto error_ret_free_local;
2697
2698 stub_entry->target_value = hash->elf.root.u.def.value;
2699 stub_entry->target_section = hash->elf.root.u.def.section;
2700 stub_entry->stub_type = hppa_stub_export;
2701 stub_entry->h = hash;
2702 stub_changed = 1;
2703 }
2704 else
2705 {
2706 (*_bfd_error_handler) (_("%s: duplicate export stub %s"),
2707 bfd_get_filename (input_bfd),
2708 stub_name);
2709 }
2710 }
2711 }
30667bf3
AM
2712 }
2713 }
edd21aca
AM
2714
2715 while (1)
2716 {
30667bf3
AM
2717 asection *stub_sec;
2718
25f72752 2719 for (input_bfd = info->input_bfds, bfd_indx = 0;
30667bf3 2720 input_bfd != NULL;
25f72752 2721 input_bfd = input_bfd->link_next, bfd_indx++)
30667bf3
AM
2722 {
2723 Elf_Internal_Shdr *symtab_hdr;
2724
2725 /* We'll need the symbol table in a second. */
2726 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2727 if (symtab_hdr->sh_info == 0)
2728 continue;
2729
25f72752 2730 local_syms = all_local_syms[bfd_indx];
30667bf3
AM
2731
2732 /* Walk over each section attached to the input bfd. */
2733 for (section = input_bfd->sections;
2734 section != NULL;
25f72752 2735 section = section->next)
30667bf3
AM
2736 {
2737 Elf_Internal_Shdr *input_rel_hdr;
2738 Elf32_External_Rela *external_relocs, *erelaend, *erela;
2739 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2740
2741 /* If there aren't any relocs, then there's nothing more
2742 to do. */
2743 if ((section->flags & SEC_RELOC) == 0
2744 || section->reloc_count == 0)
2745 continue;
2746
25f72752
AM
2747 /* If this section is a link-once section that will be
2748 discarded, then don't create any stubs. */
2749 if (section->output_section == NULL
2750 || section->output_section->owner != output_bfd)
2751 continue;
2752
30667bf3
AM
2753 /* Allocate space for the external relocations. */
2754 external_relocs
2755 = ((Elf32_External_Rela *)
2756 bfd_malloc (section->reloc_count
2757 * sizeof (Elf32_External_Rela)));
2758 if (external_relocs == NULL)
2759 {
2760 goto error_ret_free_local;
2761 }
2762
2763 /* Likewise for the internal relocations. */
2764 internal_relocs = ((Elf_Internal_Rela *)
2765 bfd_malloc (section->reloc_count
2766 * sizeof (Elf_Internal_Rela)));
2767 if (internal_relocs == NULL)
2768 {
2769 free (external_relocs);
2770 goto error_ret_free_local;
2771 }
2772
2773 /* Read in the external relocs. */
2774 input_rel_hdr = &elf_section_data (section)->rel_hdr;
2775 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
2776 || bfd_read (external_relocs, 1,
2777 input_rel_hdr->sh_size,
2778 input_bfd) != input_rel_hdr->sh_size)
2779 {
2780 free (external_relocs);
2781 error_ret_free_internal:
2782 free (internal_relocs);
2783 goto error_ret_free_local;
2784 }
2785
2786 /* Swap in the relocs. */
2787 erela = external_relocs;
2788 erelaend = erela + section->reloc_count;
2789 irela = internal_relocs;
2790 for (; erela < erelaend; erela++, irela++)
2791 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
2792
2793 /* We're done with the external relocs, free them. */
2794 free (external_relocs);
2795
2796 /* Now examine each relocation. */
2797 irela = internal_relocs;
2798 irelaend = irela + section->reloc_count;
2799 for (; irela < irelaend; irela++)
2800 {
2801 unsigned int r_type, r_indx;
2802 enum elf32_hppa_stub_type stub_type;
2803 struct elf32_hppa_stub_hash_entry *stub_entry;
2804 asection *sym_sec;
2805 bfd_vma sym_value;
2806 bfd_vma destination;
2807 struct elf32_hppa_link_hash_entry *hash;
2808 char *stub_name;
25f72752 2809 const asection *id_sec;
30667bf3
AM
2810
2811 r_type = ELF32_R_TYPE (irela->r_info);
2812 r_indx = ELF32_R_SYM (irela->r_info);
2813
2814 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2815 {
2816 bfd_set_error (bfd_error_bad_value);
2817 goto error_ret_free_internal;
2818 }
2819
2820 /* Only look for stubs on call instructions. */
2821 if (r_type != (unsigned int) R_PARISC_PCREL12F
2822 && r_type != (unsigned int) R_PARISC_PCREL17F
2823 && r_type != (unsigned int) R_PARISC_PCREL22F)
2824 continue;
2825
2826 /* Now determine the call target, its name, value,
2827 section. */
2828 sym_sec = NULL;
2829 sym_value = 0;
2830 destination = 0;
2831 hash = NULL;
2832 if (r_indx < symtab_hdr->sh_info)
2833 {
2834 /* It's a local symbol. */
2835 Elf_Internal_Sym *sym;
2836 Elf_Internal_Shdr *hdr;
2837
2838 sym = local_syms + r_indx;
2839 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2840 sym_sec = hdr->bfd_section;
2841 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2842 sym_value = sym->st_value;
2843 destination = (sym_value + irela->r_addend
2844 + sym_sec->output_offset
2845 + sym_sec->output_section->vma);
2846 }
2847 else
2848 {
2849 /* It's an external symbol. */
2850 int e_indx;
2851
2852 e_indx = r_indx - symtab_hdr->sh_info;
2853 hash = ((struct elf32_hppa_link_hash_entry *)
2854 elf_sym_hashes (input_bfd)[e_indx]);
2855
2856 while (hash->elf.root.type == bfd_link_hash_indirect
2857 || hash->elf.root.type == bfd_link_hash_warning)
2858 hash = ((struct elf32_hppa_link_hash_entry *)
2859 hash->elf.root.u.i.link);
2860
2861 if (hash->elf.root.type == bfd_link_hash_defined
2862 || hash->elf.root.type == bfd_link_hash_defweak)
2863 {
2864 sym_sec = hash->elf.root.u.def.section;
2865 sym_value = hash->elf.root.u.def.value;
2866 if (sym_sec->output_section != NULL)
2867 destination = (sym_value + irela->r_addend
2868 + sym_sec->output_offset
2869 + sym_sec->output_section->vma);
2870 }
2871 else if (hash->elf.root.type == bfd_link_hash_undefweak)
2872 {
2873 if (! info->shared)
2874 continue;
2875 }
2876 else if (hash->elf.root.type == bfd_link_hash_undefined)
2877 {
2878 if (! (info->shared
2879 && !info->no_undefined
2880 && (ELF_ST_VISIBILITY (hash->elf.other)
2881 == STV_DEFAULT)))
2882 continue;
2883 }
2884 else
2885 {
2886 bfd_set_error (bfd_error_bad_value);
2887 goto error_ret_free_internal;
2888 }
2889 }
2890
2891 /* Determine what (if any) linker stub is needed. */
2892 stub_type = hppa_type_of_stub (section, irela, hash,
2893 destination);
2894 if (stub_type == hppa_stub_none)
2895 continue;
2896
25f72752
AM
2897 /* Support for grouping stub sections. */
2898 id_sec = hplink->stub_group[section->id].link_sec;
2899
30667bf3 2900 /* Get the name of this stub. */
25f72752 2901 stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
30667bf3
AM
2902 if (!stub_name)
2903 goto error_ret_free_internal;
2904
2905 stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
2906 stub_name,
2907 false, false);
2908 if (stub_entry != NULL)
2909 {
2910 /* The proper stub has already been created. */
2911 free (stub_name);
2912 continue;
2913 }
2914
25f72752 2915 stub_entry = hppa_add_stub (stub_name, section, hplink);
30667bf3
AM
2916 if (stub_entry == NULL)
2917 {
2918 free (stub_name);
2919 goto error_ret_free_local;
2920 }
2921
2922 stub_entry->target_value = sym_value;
2923 stub_entry->target_section = sym_sec;
2924 stub_entry->stub_type = stub_type;
2925 if (info->shared)
2926 {
2927 if (stub_type == hppa_stub_import)
2928 stub_entry->stub_type = hppa_stub_import_shared;
2929 else if (stub_type == hppa_stub_long_branch
2930 && (LONG_BRANCH_PIC_IN_SHLIB || hash == NULL))
2931 stub_entry->stub_type = hppa_stub_long_branch_shared;
2932 }
2933 stub_entry->h = hash;
2934 stub_changed = 1;
2935 }
2936
2937 /* We're done with the internal relocs, free them. */
2938 free (internal_relocs);
2939 }
2940 }
2941
2942 if (!stub_changed)
2943 break;
2944
2945 /* OK, we've added some stubs. Find out the new size of the
2946 stub sections. */
30667bf3
AM
2947 for (stub_sec = hplink->stub_bfd->sections;
2948 stub_sec != NULL;
2949 stub_sec = stub_sec->next)
2950 {
74d1c347
AM
2951 stub_sec->_raw_size = 0;
2952 stub_sec->_cooked_size = 0;
2953 }
2954#if ! LONG_BRANCH_PIC_IN_SHLIB
25f72752
AM
2955 {
2956 int i;
2957
2958 for (i = top_id; i >= 0; --i)
2959 {
2960 /* This will probably hit the same section many times.. */
2961 stub_sec = hplink->stub_group[i].reloc_sec;
2962 if (stub_sec != NULL)
2963 {
2964 stub_sec->_raw_size = 0;
2965 stub_sec->_cooked_size = 0;
2966 }
2967 }
2968 }
74d1c347
AM
2969#endif
2970
2971 bfd_hash_traverse (&hplink->stub_hash_table,
2972 hppa_size_one_stub,
2973 hplink);
2974
30667bf3
AM
2975 /* Ask the linker to do its stuff. */
2976 (*hplink->layout_sections_again) ();
2977 stub_changed = 0;
2978 }
2979
25f72752 2980 ret = 1;
30667bf3
AM
2981
2982 error_ret_free_local:
25f72752
AM
2983 while (bfd_count-- > 0)
2984 if (all_local_syms[bfd_count])
2985 free (all_local_syms[bfd_count]);
30667bf3
AM
2986 free (all_local_syms);
2987
25f72752 2988 return ret;
30667bf3
AM
2989}
2990
2991
2992/* For a final link, this function is called after we have sized the
2993 stubs to provide a value for __gp. */
2994
2995boolean
2996elf32_hppa_set_gp (abfd, info)
2997 bfd *abfd;
2998 struct bfd_link_info *info;
2999{
74d1c347 3000 struct elf32_hppa_link_hash_table *hplink;
30667bf3
AM
3001 struct elf_link_hash_entry *h;
3002 asection *sec;
3003 bfd_vma gp_val;
3004
74d1c347
AM
3005 hplink = hppa_link_hash_table (info);
3006 h = elf_link_hash_lookup (&hplink->root, "$global$",
30667bf3
AM
3007 false, false, false);
3008
3009 if (h != NULL && h->root.type == bfd_link_hash_defined)
3010 {
3011 gp_val = h->root.u.def.value;
3012 sec = h->root.u.def.section;
3013 }
3014 else
3015 {
74d1c347
AM
3016 /* Choose to point our LTP at, in this order, one of .plt, .got,
3017 or .data, if these sections exist. In the case of choosing
3018 .plt try to make the LTP ideal for addressing anywhere in the
3019 .plt or .got with a 14 bit signed offset. Typically, the end
3020 of the .plt is the start of the .got, so choose .plt + 0x2000
3021 if either the .plt or .got is larger than 0x2000. If both
3022 the .plt and .got are smaller than 0x2000, choose the end of
3023 the .plt section. */
3024
3025 sec = hplink->splt;
3026 if (sec != NULL)
30667bf3 3027 {
74d1c347
AM
3028 gp_val = sec->_raw_size;
3029 if (gp_val > 0x2000
3030 || (hplink->sgot && hplink->sgot->_raw_size > 0x2000))
3031 {
3032 gp_val = 0x2000;
3033 }
3034 }
3035 else
3036 {
3037 gp_val = 0;
3038 sec = hplink->sgot;
3039 if (sec != NULL)
3040 {
3041 /* We know we don't have a .plt. If .got is large,
3042 offset our LTP. */
3043 if (sec->_raw_size > 0x2000)
3044 gp_val = 0x2000;
3045 }
3046 else
3047 {
3048 /* No .plt or .got. Who cares what the LTP is? */
3049 sec = bfd_get_section_by_name (abfd, ".data");
3050 }
30667bf3
AM
3051 }
3052 }
3053
74d1c347
AM
3054 if (sec != NULL)
3055 gp_val += sec->output_section->vma + sec->output_offset;
3056
3057 elf_gp (abfd) = gp_val;
30667bf3
AM
3058 return true;
3059}
3060
3061
3062/* Build all the stubs associated with the current output file. The
3063 stubs are kept in a hash table attached to the main linker hash
3064 table. We also set up the .plt entries for statically linked PIC
3065 functions here. This function is called via hppaelf_finish in the
3066 linker. */
3067
3068boolean
3069elf32_hppa_build_stubs (info)
3070 struct bfd_link_info *info;
3071{
3072 asection *stub_sec;
3073 struct bfd_hash_table *table;
3074 struct elf32_hppa_link_hash_table *hplink;
3075
3076 hplink = hppa_link_hash_table (info);
3077
3078 for (stub_sec = hplink->stub_bfd->sections;
3079 stub_sec != NULL;
3080 stub_sec = stub_sec->next)
3081 {
74d1c347 3082 size_t size;
30667bf3
AM
3083
3084 /* Allocate memory to hold the linker stubs. */
74d1c347 3085 size = stub_sec->_raw_size;
30667bf3
AM
3086 stub_sec->contents = (unsigned char *) bfd_zalloc (hplink->stub_bfd,
3087 size);
3088 if (stub_sec->contents == NULL && size != 0)
3089 return false;
74d1c347 3090 stub_sec->_raw_size = 0;
30667bf3
AM
3091 }
3092
3093 /* Build the stubs as directed by the stub hash table. */
30667bf3
AM
3094 table = &hplink->stub_hash_table;
3095 bfd_hash_traverse (table, hppa_build_one_stub, info);
3096
3097 return true;
3098}
3099
3100
3101/* Perform a relocation as part of a final link. */
3102
3103static bfd_reloc_status_type
25f72752 3104final_link_relocate (input_section, contents, rel, value, hplink, sym_sec, h)
30667bf3
AM
3105 asection *input_section;
3106 bfd_byte *contents;
3107 const Elf_Internal_Rela *rel;
3108 bfd_vma value;
25f72752 3109 struct elf32_hppa_link_hash_table *hplink;
30667bf3
AM
3110 asection *sym_sec;
3111 struct elf32_hppa_link_hash_entry *h;
3112{
3113 int insn;
3114 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3115 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3116 int r_format = howto->bitsize;
3117 enum hppa_reloc_field_selector_type_alt r_field;
3118 bfd *input_bfd = input_section->owner;
3119 bfd_vma offset = rel->r_offset;
3120 bfd_vma max_branch_offset = 0;
3121 bfd_byte *hit_data = contents + offset;
3122 bfd_signed_vma addend = rel->r_addend;
3123 bfd_vma location;
3124 struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
3125 int val;
3126
3127 if (r_type == R_PARISC_NONE)
3128 return bfd_reloc_ok;
3129
3130 insn = bfd_get_32 (input_bfd, hit_data);
3131
3132 /* Find out where we are and where we're going. */
3133 location = (offset +
3134 input_section->output_offset +
3135 input_section->output_section->vma);
3136
3137 switch (r_type)
3138 {
3139 case R_PARISC_PCREL12F:
3140 case R_PARISC_PCREL17F:
3141 case R_PARISC_PCREL22F:
3142 /* If this is a call to a function defined in another dynamic
3143 library, or if it is a call to a PIC function in the same
74d1c347
AM
3144 object, or if this is a shared link and it is a call to a
3145 weak symbol which may or may not be in the same object, then
3146 find the import stub in the stub hash. */
30667bf3
AM
3147 if (sym_sec == NULL
3148 || sym_sec->output_section == NULL
74d1c347
AM
3149 || (h != NULL &&
3150 (h->pic_call
3151 || (h->elf.root.type == bfd_link_hash_defweak
3152 && h->elf.dynindx != -1
3153 && h->elf.plt.offset != (bfd_vma) -1))))
30667bf3
AM
3154 {
3155 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
25f72752 3156 h, rel, hplink);
30667bf3
AM
3157 if (stub_entry != NULL)
3158 {
3159 value = (stub_entry->stub_offset
3160 + stub_entry->stub_sec->output_offset
3161 + stub_entry->stub_sec->output_section->vma);
3162 addend = 0;
3163 }
3164 else if (sym_sec == NULL && h != NULL
3165 && h->elf.root.type == bfd_link_hash_undefweak)
3166 {
3167 /* It's OK if undefined weak. Make undefined weak
3168 branches go nowhere. */
3169 value = location;
3170 addend = 0;
3171 }
3172 else
3173 return bfd_reloc_notsupported;
3174 }
3175 /* Fall thru. */
3176
3177 case R_PARISC_PCREL21L:
3178 case R_PARISC_PCREL17C:
3179 case R_PARISC_PCREL17R:
3180 case R_PARISC_PCREL14R:
3181 case R_PARISC_PCREL14F:
3182 /* Make it a pc relative offset. */
3183 value -= location;
3184 addend -= 8;
3185 break;
3186
3187 case R_PARISC_DPREL21L:
3188 case R_PARISC_DPREL14R:
3189 case R_PARISC_DPREL14F:
3190 /* For all the DP relative relocations, we need to examine the symbol's
3191 section. If it's a code section, then "data pointer relative" makes
3192 no sense. In that case we don't adjust the "value", and for 21 bit
3193 addil instructions, we change the source addend register from %dp to
3194 %r0. This situation commonly arises when a variable's "constness"
3195 is declared differently from the way the variable is defined. For
3196 instance: "extern int foo" with foo defined as "const int foo". */
3197 if (sym_sec == NULL)
3198 break;
3199 if ((sym_sec->flags & SEC_CODE) != 0)
3200 {
3201 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3202 == (((int) OP_ADDIL << 26) | (27 << 21)))
3203 {
3204 insn &= ~ (0x1f << 21);
74d1c347 3205#if 1 /* debug them. */
30667bf3
AM
3206 (*_bfd_error_handler)
3207 (_("%s(%s+0x%lx): fixing %s"),
3208 bfd_get_filename (input_bfd),
3209 input_section->name,
3210 (long) rel->r_offset,
3211 howto->name);
3212#endif
3213 }
3214 /* Now try to make things easy for the dynamic linker. */
3215
3216 break;
3217 }
74d1c347 3218 /* Fall thru. */
30667bf3
AM
3219
3220 case R_PARISC_DLTIND21L:
3221 case R_PARISC_DLTIND14R:
3222 case R_PARISC_DLTIND14F:
3223 value -= elf_gp (input_section->output_section->owner);
3224 break;
3225
3226 default:
3227 break;
3228 }
3229
3230 switch (r_type)
3231 {
3232 case R_PARISC_DIR32:
3233 case R_PARISC_DIR17F:
3234 case R_PARISC_PCREL17C:
3235 case R_PARISC_PCREL14F:
3236 case R_PARISC_DPREL14F:
3237 case R_PARISC_PLABEL32:
3238 case R_PARISC_DLTIND14F:
3239 case R_PARISC_SEGBASE:
3240 case R_PARISC_SEGREL32:
3241 r_field = e_fsel;
3242 break;
3243
3244 case R_PARISC_DIR21L:
3245 case R_PARISC_PCREL21L:
3246 case R_PARISC_DPREL21L:
3247 case R_PARISC_PLABEL21L:
3248 case R_PARISC_DLTIND21L:
3249 r_field = e_lrsel;
3250 break;
3251
3252 case R_PARISC_DIR17R:
3253 case R_PARISC_PCREL17R:
3254 case R_PARISC_DIR14R:
3255 case R_PARISC_PCREL14R:
3256 case R_PARISC_DPREL14R:
3257 case R_PARISC_PLABEL14R:
3258 case R_PARISC_DLTIND14R:
3259 r_field = e_rrsel;
3260 break;
3261
3262 case R_PARISC_PCREL12F:
3263 case R_PARISC_PCREL17F:
3264 case R_PARISC_PCREL22F:
3265 r_field = e_fsel;
3266
3267 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3268 {
3269 max_branch_offset = (1 << (17-1)) << 2;
3270 }
3271 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3272 {
3273 max_branch_offset = (1 << (12-1)) << 2;
3274 }
3275 else
3276 {
3277 max_branch_offset = (1 << (22-1)) << 2;
3278 }
3279
3280 /* sym_sec is NULL on undefined weak syms or when shared on
3281 undefined syms. We've already checked for a stub for the
3282 shared undefined case. */
3283 if (sym_sec == NULL)
3284 break;
3285
3286 /* If the branch is out of reach, then redirect the
3287 call to the local stub for this function. */
3288 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3289 {
3290 stub_entry = hppa_get_stub_entry (input_section, sym_sec,
25f72752 3291 h, rel, hplink);
30667bf3
AM
3292 if (stub_entry == NULL)
3293 return bfd_reloc_notsupported;
3294
3295 /* Munge up the value and addend so that we call the stub
3296 rather than the procedure directly. */
3297 value = (stub_entry->stub_offset
3298 + stub_entry->stub_sec->output_offset
3299 + stub_entry->stub_sec->output_section->vma
3300 - location);
3301 addend = -8;
3302 }
3303 break;
3304
3305 /* Something we don't know how to handle. */
3306 default:
3307 return bfd_reloc_notsupported;
3308 }
3309
3310 /* Make sure we can reach the stub. */
3311 if (max_branch_offset != 0
3312 && value + addend + max_branch_offset >= 2*max_branch_offset)
3313 {
3314 (*_bfd_error_handler)
3315 (_("%s(%s+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3316 bfd_get_filename (input_bfd),
3317 input_section->name,
3318 (long) rel->r_offset,
3319 stub_entry->root.string);
3320 return bfd_reloc_notsupported;
3321 }
3322
3323 val = hppa_field_adjust (value, addend, r_field);
3324
3325 switch (r_type)
3326 {
3327 case R_PARISC_PCREL12F:
3328 case R_PARISC_PCREL17C:
3329 case R_PARISC_PCREL17F:
3330 case R_PARISC_PCREL17R:
3331 case R_PARISC_PCREL22F:
3332 case R_PARISC_DIR17F:
3333 case R_PARISC_DIR17R:
3334 /* This is a branch. Divide the offset by four.
3335 Note that we need to decide whether it's a branch or
3336 otherwise by inspecting the reloc. Inspecting insn won't
3337 work as insn might be from a .word directive. */
3338 val >>= 2;
3339 break;
3340
3341 default:
3342 break;
3343 }
3344
3345 insn = hppa_rebuild_insn (insn, val, r_format);
3346
3347 /* Update the instruction word. */
74d1c347 3348 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
30667bf3
AM
3349 return bfd_reloc_ok;
3350}
3351
3352
3353/* Relocate an HPPA ELF section. */
3354
3355static boolean
3356elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
3357 contents, relocs, local_syms, local_sections)
3358 bfd *output_bfd;
3359 struct bfd_link_info *info;
3360 bfd *input_bfd;
3361 asection *input_section;
3362 bfd_byte *contents;
3363 Elf_Internal_Rela *relocs;
3364 Elf_Internal_Sym *local_syms;
3365 asection **local_sections;
3366{
3367 bfd *dynobj;
3368 bfd_vma *local_got_offsets;
3369 struct elf32_hppa_link_hash_table *hplink;
3370 Elf_Internal_Shdr *symtab_hdr;
3371 Elf_Internal_Rela *rel;
3372 Elf_Internal_Rela *relend;
3373 asection *sreloc;
3374
3375 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3376
30667bf3 3377 hplink = hppa_link_hash_table (info);
74d1c347
AM
3378 dynobj = hplink->root.dynobj;
3379 local_got_offsets = elf_local_got_offsets (input_bfd);
30667bf3
AM
3380 sreloc = NULL;
3381
3382 rel = relocs;
3383 relend = relocs + input_section->reloc_count;
3384 for (; rel < relend; rel++)
3385 {
3386 unsigned int r_type;
3387 reloc_howto_type *howto;
3388 unsigned int r_symndx;
3389 struct elf32_hppa_link_hash_entry *h;
3390 Elf_Internal_Sym *sym;
3391 asection *sym_sec;
3392 bfd_vma relocation;
3393 bfd_reloc_status_type r;
3394 const char *sym_name;
74d1c347 3395 boolean plabel;
30667bf3
AM
3396
3397 r_type = ELF32_R_TYPE (rel->r_info);
3398 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3399 {
3400 bfd_set_error (bfd_error_bad_value);
3401 return false;
3402 }
3403 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3404 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3405 continue;
3406
3407 r_symndx = ELF32_R_SYM (rel->r_info);
3408
3409 if (info->relocateable)
3410 {
3411 /* This is a relocateable link. We don't have to change
3412 anything, unless the reloc is against a section symbol,
3413 in which case we have to adjust according to where the
3414 section symbol winds up in the output section. */
3415 if (r_symndx < symtab_hdr->sh_info)
3416 {
3417 sym = local_syms + r_symndx;
3418 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3419 {
3420 sym_sec = local_sections[r_symndx];
3421 rel->r_addend += sym_sec->output_offset;
3422 }
3423 }
3424 continue;
3425 }
3426
3427 /* This is a final link. */
3428 h = NULL;
3429 sym = NULL;
3430 sym_sec = NULL;
3431 if (r_symndx < symtab_hdr->sh_info)
3432 {
3433 /* This is a local symbol, h defaults to NULL. */
3434 sym = local_syms + r_symndx;
3435 sym_sec = local_sections[r_symndx];
3436 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
3437 ? 0 : sym->st_value)
3438 + sym_sec->output_offset
3439 + sym_sec->output_section->vma);
3440 }
3441 else
3442 {
3443 int indx;
3444
3445 /* It's a global; Find its entry in the link hash. */
3446 indx = r_symndx - symtab_hdr->sh_info;
3447 h = ((struct elf32_hppa_link_hash_entry *)
3448 elf_sym_hashes (input_bfd)[indx]);
3449 while (h->elf.root.type == bfd_link_hash_indirect
3450 || h->elf.root.type == bfd_link_hash_warning)
3451 h = (struct elf32_hppa_link_hash_entry *) h->elf.root.u.i.link;
3452
3453 relocation = 0;
3454 if (h->elf.root.type == bfd_link_hash_defined
3455 || h->elf.root.type == bfd_link_hash_defweak)
3456 {
3457 sym_sec = h->elf.root.u.def.section;
3458 /* If sym_sec->output_section is NULL, then it's a
3459 symbol defined in a shared library. */
3460 if (sym_sec->output_section != NULL)
3461 relocation = (h->elf.root.u.def.value
3462 + sym_sec->output_offset
3463 + sym_sec->output_section->vma);
3464 }
3465 else if (h->elf.root.type == bfd_link_hash_undefweak)
3466 ;
3467 else if (info->shared && !info->no_undefined
3468 && ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT)
3469 {
3470 if (info->symbolic)
3471 if (!((*info->callbacks->undefined_symbol)
3472 (info, h->elf.root.root.string, input_bfd,
3473 input_section, rel->r_offset, false)))
3474 return false;
3475 }
3476 else
3477 {
3478 if (!((*info->callbacks->undefined_symbol)
3479 (info, h->elf.root.root.string, input_bfd,
3480 input_section, rel->r_offset, true)))
3481 return false;
3482 }
3483 }
3484
3485 /* Do any required modifications to the relocation value, and
25f72752
AM
3486 determine what types of dynamic info we need to output, if
3487 any. */
74d1c347 3488 plabel = 0;
30667bf3
AM
3489 switch (r_type)
3490 {
3491 case R_PARISC_DLTIND14F:
3492 case R_PARISC_DLTIND14R:
3493 case R_PARISC_DLTIND21L:
3494 /* Relocation is to the entry for this symbol in the global
3495 offset table. */
3496 if (h != NULL)
3497 {
3498 bfd_vma off;
3499
3500 off = h->elf.got.offset;
3501 BFD_ASSERT (off != (bfd_vma) -1);
3502
74d1c347 3503 if (! hplink->root.dynamic_sections_created
30667bf3
AM
3504 || (info->shared
3505 && (info->symbolic || h->elf.dynindx == -1)
3506 && (h->elf.elf_link_hash_flags
3507 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3508 {
3509 /* This is actually a static link, or it is a
3510 -Bsymbolic link and the symbol is defined
3511 locally, or the symbol was forced to be local
3512 because of a version file. We must initialize
3513 this entry in the global offset table. Since the
3514 offset must always be a multiple of 4, we use the
3515 least significant bit to record whether we have
3516 initialized it already.
3517
3518 When doing a dynamic link, we create a .rela.got
3519 relocation entry to initialize the value. This
3520 is done in the finish_dynamic_symbol routine. */
3521 if ((off & 1) != 0)
3522 off &= ~1;
3523 else
3524 {
3525 bfd_put_32 (output_bfd, relocation,
3526 hplink->sgot->contents + off);
3527 h->elf.got.offset |= 1;
3528 }
3529 }
3530
3531 relocation = off;
3532 }
3533 else
3534 {
3535 /* Local symbol case. */
3536 bfd_vma off;
3537
3538 BFD_ASSERT (local_got_offsets != NULL
3539 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3540
3541 off = local_got_offsets[r_symndx];
3542
3543 /* The offset must always be a multiple of 4. We use
3544 the least significant bit to record whether we have
3545 already generated the necessary reloc. */
3546 if ((off & 1) != 0)
3547 off &= ~1;
3548 else
3549 {
3550 bfd_put_32 (output_bfd, relocation,
3551 hplink->sgot->contents + off);
3552
3553 if (info->shared)
3554 {
74d1c347 3555 /* Output a dynamic *ABS* relocation for this
30667bf3
AM
3556 GOT entry. In this case it is relative to
3557 the base of the object because the symbol
3558 index is zero. */
3559 Elf_Internal_Rela outrel;
3560 asection *srelgot = hplink->srelgot;
3561
3562 outrel.r_offset = (off
3563 + hplink->sgot->output_offset
3564 + hplink->sgot->output_section->vma);
74d1c347 3565 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
30667bf3
AM
3566 outrel.r_addend = relocation;
3567 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3568 ((Elf32_External_Rela *)
3569 srelgot->contents
3570 + srelgot->reloc_count));
3571 ++srelgot->reloc_count;
3572 }
252b5132 3573
30667bf3
AM
3574 local_got_offsets[r_symndx] |= 1;
3575 }
252b5132 3576
30667bf3
AM
3577 relocation = off;
3578 }
252b5132 3579
30667bf3
AM
3580 /* Add the base of the GOT to the relocation value. */
3581 relocation += (hplink->sgot->output_offset
3582 + hplink->sgot->output_section->vma);
3583 break;
252b5132 3584
30667bf3
AM
3585 case R_PARISC_PLABEL14R:
3586 case R_PARISC_PLABEL21L:
3587 case R_PARISC_PLABEL32:
74d1c347 3588 if (hplink->root.dynamic_sections_created)
252b5132 3589 {
74d1c347
AM
3590 bfd_vma off;
3591
3592 /* If we have a global symbol with a PLT slot, then
3593 redirect this relocation to it. */
3594 if (h != NULL)
3595 {
3596 off = h->elf.plt.offset;
3597 }
3598 else
3599 {
3600 int indx;
3601
3602 indx = r_symndx + symtab_hdr->sh_info;
3603 off = local_got_offsets[indx];
3604
3605 /* As for the local .got entry case, we use the last
3606 bit to record whether we've already initialised
3607 this local .plt entry. */
3608 if ((off & 1) != 0)
3609 off &= ~1;
3610 else
3611 {
3612 bfd_put_32 (output_bfd,
3613 relocation,
3614 hplink->splt->contents + off);
3615 bfd_put_32 (output_bfd,
3616 elf_gp (hplink->splt->output_section->owner),
3617 hplink->splt->contents + off + 4);
3618
3619 if (info->shared)
3620 {
3621 /* Output a dynamic IPLT relocation for this
3622 PLT entry. */
3623 Elf_Internal_Rela outrel;
3624 asection *srelplt = hplink->srelplt;
3625
3626 outrel.r_offset = (off
3627 + hplink->splt->output_offset
3628 + hplink->splt->output_section->vma);
3629 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3630 outrel.r_addend = relocation;
3631 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3632 ((Elf32_External_Rela *)
3633 srelplt->contents
3634 + srelplt->reloc_count));
3635 ++srelplt->reloc_count;
3636 }
3637
3638 local_got_offsets[indx] |= 1;
3639 }
3640 }
3641
3642 BFD_ASSERT (off < (bfd_vma) -2);
3643
3644 /* PLABELs contain function pointers. Relocation is to
3645 the entry for the function in the .plt. The magic +2
3646 offset signals to $$dyncall that the function pointer
3647 is in the .plt and thus has a gp pointer too.
3648 Exception: Undefined PLABELs should have a value of
3649 zero. */
3650 if (h == NULL
3651 || (h->elf.root.type != bfd_link_hash_undefweak
3652 && h->elf.root.type != bfd_link_hash_undefined))
3653 {
3654 relocation = (off
3655 + hplink->splt->output_offset
3656 + hplink->splt->output_section->vma
3657 + 2);
3658 }
3659 plabel = 1;
30667bf3
AM
3660 }
3661 /* Fall through and possibly emit a dynamic relocation. */
3662
3663 case R_PARISC_DIR17F:
3664 case R_PARISC_DIR17R:
3665 case R_PARISC_DIR14R:
3666 case R_PARISC_DIR21L:
3667 case R_PARISC_DPREL14F:
3668 case R_PARISC_DPREL14R:
3669 case R_PARISC_DPREL21L:
3670 case R_PARISC_DIR32:
3671 /* The reloc types handled here and this conditional
3672 expression must match the code in check_relocs and
3673 hppa_discard_copies. ie. We need exactly the same
3674 condition as in check_relocs, with some extra conditions
3675 (dynindx test in this case) to cater for relocs removed
3676 by hppa_discard_copies. */
3677 if ((input_section->flags & SEC_ALLOC) != 0
3678 && info->shared
3679#if RELATIVE_DYNAMIC_RELOCS
3680 && (is_absolute_reloc (r_type)
3681 || ((!info->symbolic
3682 || (h != NULL
3683 && ((h->elf.elf_link_hash_flags
3684 & ELF_LINK_HASH_DEF_REGULAR) == 0
3685 || h->elf.root.type == bfd_link_hash_defweak)))
3686 && (h == NULL || h->elf.dynindx != -1)))
3687#endif
3688 )
3689 {
3690 Elf_Internal_Rela outrel;
3691 boolean skip;
252b5132 3692
30667bf3
AM
3693 /* When generating a shared object, these relocations
3694 are copied into the output file to be resolved at run
3695 time. */
252b5132 3696
30667bf3 3697 if (sreloc == NULL)
edd21aca 3698 {
30667bf3
AM
3699 const char *name;
3700
3701 name = (bfd_elf_string_from_elf_section
3702 (input_bfd,
3703 elf_elfheader (input_bfd)->e_shstrndx,
3704 elf_section_data (input_section)->rel_hdr.sh_name));
3705 if (name == NULL)
3706 return false;
3707 sreloc = bfd_get_section_by_name (dynobj, name);
3708 BFD_ASSERT (sreloc != NULL);
edd21aca 3709 }
252b5132 3710
30667bf3
AM
3711 outrel.r_offset = rel->r_offset;
3712 outrel.r_addend = rel->r_addend;
3713 skip = false;
3714 if (elf_section_data (input_section)->stab_info != NULL)
edd21aca 3715 {
30667bf3
AM
3716 bfd_vma off;
3717
3718 off = (_bfd_stab_section_offset
74d1c347 3719 (output_bfd, &hplink->root.stab_info,
30667bf3
AM
3720 input_section,
3721 &elf_section_data (input_section)->stab_info,
3722 rel->r_offset));
3723 if (off == (bfd_vma) -1)
3724 skip = true;
3725 outrel.r_offset = off;
edd21aca 3726 }
252b5132 3727
30667bf3
AM
3728 outrel.r_offset += (input_section->output_offset
3729 + input_section->output_section->vma);
3730
3731 if (skip)
252b5132 3732 {
30667bf3 3733 memset (&outrel, 0, sizeof (outrel));
252b5132 3734 }
74d1c347
AM
3735 else if (h != NULL
3736 && h->elf.dynindx != -1
3737 && (plabel
3738 || !info->symbolic
30667bf3
AM
3739 || (h->elf.elf_link_hash_flags
3740 & ELF_LINK_HASH_DEF_REGULAR) == 0))
252b5132 3741 {
30667bf3
AM
3742 outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3743 }
3744 else /* It's a local symbol, or one marked to become local. */
3745 {
3746 int indx = 0;
edd21aca 3747
30667bf3
AM
3748 /* Add the absolute offset of the symbol. */
3749 outrel.r_addend += relocation;
edd21aca 3750
74d1c347
AM
3751 /* Global plabels need to be processed by the
3752 dynamic linker so that functions have at most one
3753 fptr. For this reason, we need to differentiate
3754 between global and local plabels, which we do by
3755 providing the function symbol for a global plabel
3756 reloc, and no symbol for local plabels. */
3757 if (! plabel
3758 && sym_sec != NULL
30667bf3
AM
3759 && sym_sec->output_section != NULL
3760 && ! bfd_is_abs_section (sym_sec))
252b5132 3761 {
30667bf3
AM
3762 indx = elf_section_data (sym_sec->output_section)->dynindx;
3763 /* We are turning this relocation into one
3764 against a section symbol, so subtract out the
3765 output section's address but not the offset
3766 of the input section in the output section. */
3767 outrel.r_addend -= sym_sec->output_section->vma;
252b5132 3768 }
252b5132 3769
30667bf3
AM
3770 outrel.r_info = ELF32_R_INFO (indx, r_type);
3771 }
edd21aca 3772
30667bf3
AM
3773 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3774 ((Elf32_External_Rela *)
3775 sreloc->contents
3776 + sreloc->reloc_count));
3777 ++sreloc->reloc_count;
3778 }
3779 break;
edd21aca 3780
30667bf3
AM
3781 default:
3782 break;
3783 }
252b5132 3784
30667bf3 3785 r = final_link_relocate (input_section, contents, rel, relocation,
25f72752 3786 hplink, sym_sec, h);
252b5132 3787
30667bf3
AM
3788 if (r == bfd_reloc_ok)
3789 continue;
252b5132 3790
30667bf3
AM
3791 if (h != NULL)
3792 sym_name = h->elf.root.root.string;
3793 else
3794 {
3795 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3796 symtab_hdr->sh_link,
3797 sym->st_name);
3798 if (sym_name == NULL)
3799 return false;
3800 if (*sym_name == '\0')
3801 sym_name = bfd_section_name (input_bfd, sym_sec);
3802 }
edd21aca 3803
30667bf3 3804 howto = elf_hppa_howto_table + r_type;
252b5132 3805
30667bf3
AM
3806 if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
3807 {
3808 (*_bfd_error_handler)
3809 (_("%s(%s+0x%lx): cannot handle %s for %s"),
3810 bfd_get_filename (input_bfd),
3811 input_section->name,
3812 (long) rel->r_offset,
3813 howto->name,
3814 sym_name);
3815 }
3816 else
3817 {
3818 if (!((*info->callbacks->reloc_overflow)
3819 (info, sym_name, howto->name, (bfd_vma) 0,
3820 input_bfd, input_section, rel->r_offset)))
3821 return false;
3822 }
3823 }
edd21aca 3824
30667bf3
AM
3825 return true;
3826}
252b5132 3827
edd21aca 3828
30667bf3
AM
3829/* Finish up dynamic symbol handling. We set the contents of various
3830 dynamic sections here. */
252b5132 3831
30667bf3
AM
3832static boolean
3833elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
3834 bfd *output_bfd;
3835 struct bfd_link_info *info;
3836 struct elf_link_hash_entry *h;
3837 Elf_Internal_Sym *sym;
3838{
3839 struct elf32_hppa_link_hash_table *hplink;
3840 bfd *dynobj;
edd21aca 3841
30667bf3 3842 hplink = hppa_link_hash_table (info);
74d1c347 3843 dynobj = hplink->root.dynobj;
30667bf3 3844
30667bf3
AM
3845 if (h->plt.offset != (bfd_vma) -1)
3846 {
3847 bfd_vma value;
3848 Elf_Internal_Rela rel;
3849
3850 /* This symbol has an entry in the procedure linkage table. Set
3851 it up.
3852
3853 The format of a plt entry is
74d1c347
AM
3854 <funcaddr>
3855 <__gp>
3856 <used by ld.so>
3857
3858 The last field is present only for plt entries that are used
3859 by global plabels. */
30667bf3
AM
3860
3861 /* We do not actually care about the value in the PLT entry if
3862 we are creating a shared library and the symbol is still
3863 undefined; We create a dynamic relocation to fill in the
3864 correct value. */
3865 value = 0;
3866 if (h->root.type == bfd_link_hash_defined
3867 || h->root.type == bfd_link_hash_defweak)
3868 {
3869 value = h->root.u.def.value;
3870 if (h->root.u.def.section->output_section != NULL)
3871 value += (h->root.u.def.section->output_offset
3872 + h->root.u.def.section->output_section->vma);
252b5132 3873 }
edd21aca 3874
74d1c347
AM
3875 bfd_put_32 (hplink->splt->owner,
3876 value,
30667bf3 3877 hplink->splt->contents + h->plt.offset);
74d1c347
AM
3878 bfd_put_32 (hplink->splt->owner,
3879 elf_gp (hplink->splt->output_section->owner),
30667bf3 3880 hplink->splt->contents + h->plt.offset + 4);
74d1c347
AM
3881 if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
3882 && ((struct elf32_hppa_link_hash_entry *) h)->plabel
30667bf3 3883 && h->dynindx != -1)
74d1c347
AM
3884 {
3885 memset (hplink->splt->contents + h->plt.offset + 8,
3886 0, PLABEL_PLT_ENTRY_SIZE - PLT_ENTRY_SIZE);
3887 }
3888
3889 if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
30667bf3
AM
3890 {
3891 /* Create a dynamic IPLT relocation for this entry. */
3892 rel.r_offset = (h->plt.offset
3893 + hplink->splt->output_offset
3894 + hplink->splt->output_section->vma);
74d1c347
AM
3895 if (! ((struct elf32_hppa_link_hash_entry *) h)->plt_abs
3896 && h->dynindx != -1)
3897 {
3898 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
3899 rel.r_addend = 0;
3900 }
3901 else
3902 {
3903 /* This symbol has been marked to become local, and is
3904 used by a plabel so must be kept in the .plt. */
3905 rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3906 rel.r_addend = value;
3907 }
30667bf3
AM
3908
3909 bfd_elf32_swap_reloca_out (hplink->splt->output_section->owner,
3910 &rel,
3911 ((Elf32_External_Rela *)
3912 hplink->srelplt->contents
3913 + hplink->srelplt->reloc_count));
3914 hplink->srelplt->reloc_count++;
3915 }
3916
3917 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3918 {
3919 /* Mark the symbol as undefined, rather than as defined in
3920 the .plt section. Leave the value alone. */
3921 sym->st_shndx = SHN_UNDEF;
3922 }
3923 }
edd21aca 3924
30667bf3
AM
3925 if (h->got.offset != (bfd_vma) -1)
3926 {
3927 Elf_Internal_Rela rel;
3928
3929 /* This symbol has an entry in the global offset table. Set it
3930 up. */
3931
3932 rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
3933 + hplink->sgot->output_offset
3934 + hplink->sgot->output_section->vma);
3935
3936 /* If this is a static link, or it is a -Bsymbolic link and the
3937 symbol is defined locally or was forced to be local because
3938 of a version file, we just want to emit a RELATIVE reloc.
3939 The entry in the global offset table will already have been
3940 initialized in the relocate_section function. */
74d1c347 3941 if (! hplink->root.dynamic_sections_created
30667bf3
AM
3942 || (info->shared
3943 && (info->symbolic || h->dynindx == -1)
3944 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
3945 {
74d1c347 3946 rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
30667bf3
AM
3947 rel.r_addend = (h->root.u.def.value
3948 + h->root.u.def.section->output_offset
3949 + h->root.u.def.section->output_section->vma);
3950 }
3951 else
3952 {
3953 BFD_ASSERT((h->got.offset & 1) == 0);
3954 bfd_put_32 (output_bfd, (bfd_vma) 0,
3955 hplink->sgot->contents + h->got.offset);
3956 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
3957 rel.r_addend = 0;
3958 }
edd21aca 3959
30667bf3
AM
3960 bfd_elf32_swap_reloca_out (output_bfd, &rel,
3961 ((Elf32_External_Rela *)
3962 hplink->srelgot->contents
3963 + hplink->srelgot->reloc_count));
3964 ++hplink->srelgot->reloc_count;
3965 }
edd21aca 3966
30667bf3
AM
3967 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3968 {
3969 asection *s;
3970 Elf_Internal_Rela rel;
3971
3972 /* This symbol needs a copy reloc. Set it up. */
3973
3974 BFD_ASSERT (h->dynindx != -1
3975 && (h->root.type == bfd_link_hash_defined
3976 || h->root.type == bfd_link_hash_defweak));
3977
3978 s = hplink->srelbss;
3979
3980 rel.r_offset = (h->root.u.def.value
3981 + h->root.u.def.section->output_offset
3982 + h->root.u.def.section->output_section->vma);
3983 rel.r_addend = 0;
3984 rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
3985 bfd_elf32_swap_reloca_out (output_bfd, &rel,
3986 ((Elf32_External_Rela *) s->contents
3987 + s->reloc_count));
3988 ++s->reloc_count;
3989 }
3990
3991 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3992 if (h->root.root.string[0] == '_'
3993 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
3994 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
3995 {
3996 sym->st_shndx = SHN_ABS;
3997 }
3998
3999 return true;
4000}
4001
4002
4003/* Finish up the dynamic sections. */
4004
4005static boolean
4006elf32_hppa_finish_dynamic_sections (output_bfd, info)
4007 bfd *output_bfd;
4008 struct bfd_link_info *info;
4009{
4010 bfd *dynobj;
4011 struct elf32_hppa_link_hash_table *hplink;
4012 asection *sdyn;
4013
30667bf3 4014 hplink = hppa_link_hash_table (info);
74d1c347 4015 dynobj = hplink->root.dynobj;
30667bf3
AM
4016
4017 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4018
74d1c347 4019 if (hplink->root.dynamic_sections_created)
30667bf3
AM
4020 {
4021 Elf32_External_Dyn *dyncon, *dynconend;
4022
4023 BFD_ASSERT (sdyn != NULL);
4024
4025 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4026 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4027 for (; dyncon < dynconend; dyncon++)
edd21aca 4028 {
30667bf3
AM
4029 Elf_Internal_Dyn dyn;
4030 asection *s;
4031
4032 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4033
4034 switch (dyn.d_tag)
4035 {
4036 default:
4037 break;
4038
4039 case DT_PLTGOT:
4040 /* Use PLTGOT to set the GOT register. */
4041 dyn.d_un.d_ptr = elf_gp (output_bfd);
4042 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4043 break;
4044
4045 case DT_JMPREL:
4046 s = hplink->srelplt;
4047 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4048 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4049 break;
4050
4051 case DT_PLTRELSZ:
4052 s = hplink->srelplt;
4053 if (s->_cooked_size != 0)
4054 dyn.d_un.d_val = s->_cooked_size;
4055 else
4056 dyn.d_un.d_val = s->_raw_size;
4057 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4058 break;
74d1c347
AM
4059
4060 case DT_INIT:
4061 case DT_FINI:
4062 {
4063 struct elf_link_hash_entry *h;
4064 const char *funcname;
4065
4066 if (dyn.d_tag == DT_INIT)
4067 funcname = info->init_function;
4068 else
4069 funcname = info->fini_function;
4070
4071 h = elf_link_hash_lookup (&hplink->root, funcname,
4072 false, false, false);
4073
4074 /* This is a function pointer. The magic +2 offset
4075 signals to $$dyncall that the function pointer
4076 is in the .plt and thus has a gp pointer too. */
4077 dyn.d_un.d_ptr = (h->plt.offset
4078 + hplink->splt->output_offset
4079 + hplink->splt->output_section->vma
4080 + 2);
4081 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4082 break;
4083 }
30667bf3 4084 }
edd21aca 4085 }
252b5132 4086 }
edd21aca 4087
30667bf3
AM
4088 if (hplink->sgot->_raw_size != 0)
4089 {
74d1c347
AM
4090 /* Fill in the first entry in the global offset table.
4091 We use it to point to our dynamic section, if we have one. */
30667bf3
AM
4092 bfd_put_32 (output_bfd,
4093 (sdyn != NULL
4094 ? sdyn->output_section->vma + sdyn->output_offset
4095 : (bfd_vma) 0),
4096 hplink->sgot->contents);
4097
74d1c347
AM
4098 /* The second entry is reserved for use by the dynamic linker. */
4099 bfd_put_32 (output_bfd, (bfd_vma) 0, hplink->sgot->contents + 4);
4100
30667bf3 4101 /* Set .got entry size. */
74d1c347
AM
4102 elf_section_data (hplink->sgot->output_section)
4103 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
30667bf3
AM
4104 }
4105
4106 /* Set plt entry size. */
4107 if (hplink->splt->_raw_size != 0)
74d1c347
AM
4108 elf_section_data (hplink->splt->output_section)
4109 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
30667bf3 4110
252b5132 4111 return true;
30667bf3 4112}
252b5132 4113
edd21aca 4114
30667bf3
AM
4115/* Called when writing out an object file to decide the type of a
4116 symbol. */
4117static int
4118elf32_hppa_elf_get_symbol_type (elf_sym, type)
4119 Elf_Internal_Sym *elf_sym;
4120 int type;
4121{
4122 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4123 return STT_PARISC_MILLI;
4124 else
4125 return type;
252b5132
RH
4126}
4127
30667bf3 4128
252b5132 4129/* Misc BFD support code. */
30667bf3
AM
4130#define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4131#define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4132#define elf_info_to_howto elf_hppa_info_to_howto
4133#define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
252b5132 4134
252b5132 4135/* Stuff for the BFD linker. */
30667bf3
AM
4136#define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
4137#define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4138#define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
4139#define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
4140#define elf_backend_check_relocs elf32_hppa_check_relocs
4141#define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4142#define elf_backend_fake_sections elf_hppa_fake_sections
4143#define elf_backend_relocate_section elf32_hppa_relocate_section
74d1c347 4144#define elf_backend_hide_symbol elf32_hppa_hide_symbol
30667bf3
AM
4145#define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4146#define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4147#define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4148#define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4149#define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
4150#define elf_backend_object_p elf32_hppa_object_p
4151#define elf_backend_final_write_processing elf_hppa_final_write_processing
4152#define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
4153
4154#define elf_backend_can_gc_sections 1
4155#define elf_backend_plt_alignment 2
4156#define elf_backend_want_got_plt 0
4157#define elf_backend_plt_readonly 0
4158#define elf_backend_want_plt_sym 0
74d1c347 4159#define elf_backend_got_header_size 8
252b5132
RH
4160
4161#define TARGET_BIG_SYM bfd_elf32_hppa_vec
4162#define TARGET_BIG_NAME "elf32-hppa"
4163#define ELF_ARCH bfd_arch_hppa
4164#define ELF_MACHINE_CODE EM_PARISC
4165#define ELF_MAXPAGESIZE 0x1000
4166
4167#include "elf32-target.h"
This page took 0.269857 seconds and 4 git commands to generate.