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