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