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