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