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