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