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