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