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