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