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