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