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