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