2005-10-10 H.J. Lu <hongjiu.lu@intel.com>
[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,
3eb128b2 3 2002, 2003, 2004, 2005 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
9c5bfbb7 1007elf32_hppa_copy_indirect_symbol (const struct elf_backend_data *bed,
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
875c0872 1023 if (eh_ind->root.type == bfd_link_hash_indirect)
bbd7ec4a
AM
1024 abort ();
1025
1026 /* Add reloc counts against the weak sym to the strong sym
1027 list. Merge any entries against the same section. */
875c0872 1028 for (hdh_pp = &hh_ind->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
bbd7ec4a 1029 {
875c0872 1030 struct elf32_hppa_dyn_reloc_entry *hdh_q;
bbd7ec4a 1031
a63e02c7 1032 for (hdh_q = hh_dir->dyn_relocs; hdh_q != NULL; hdh_q = hdh_q->hdh_next)
875c0872 1033 if (hdh_q->sec == hdh_p->sec)
bbd7ec4a
AM
1034 {
1035#if RELATIVE_DYNRELOCS
875c0872 1036 hdh_q->relative_count += hdh_p->relative_count;
bbd7ec4a 1037#endif
875c0872 1038 hdh_q->count += hdh_p->count;
a63e02c7 1039 *hdh_pp = hdh_p->hdh_next;
bbd7ec4a
AM
1040 break;
1041 }
875c0872 1042 if (hdh_q == NULL)
a63e02c7 1043 hdh_pp = &hdh_p->hdh_next;
bbd7ec4a 1044 }
875c0872 1045 *hdh_pp = hh_dir->dyn_relocs;
bbd7ec4a
AM
1046 }
1047
875c0872
DA
1048 hh_dir->dyn_relocs = hh_ind->dyn_relocs;
1049 hh_ind->dyn_relocs = NULL;
ebe50bae 1050 }
ebe50bae 1051
4fc8051d 1052 if (ELIMINATE_COPY_RELOCS
875c0872
DA
1053 && eh_ind->root.type != bfd_link_hash_indirect
1054 && eh_dir->dynamic_adjusted)
f5385ebf
AM
1055 {
1056 /* If called to transfer flags for a weakdef during processing
1057 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1058 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
875c0872
DA
1059 eh_dir->ref_dynamic |= eh_ind->ref_dynamic;
1060 eh_dir->ref_regular |= eh_ind->ref_regular;
1061 eh_dir->ref_regular_nonweak |= eh_ind->ref_regular_nonweak;
1062 eh_dir->needs_plt |= eh_ind->needs_plt;
f5385ebf 1063 }
4fc8051d 1064 else
875c0872 1065 _bfd_elf_link_hash_copy_indirect (bed, eh_dir, eh_ind);
ebe50bae
AM
1066}
1067
30667bf3 1068/* Look through the relocs for a section during the first phase, and
3ac8354b
AM
1069 calculate needed space in the global offset table, procedure linkage
1070 table, and dynamic reloc sections. At this point we haven't
1071 necessarily read all the input files. */
252b5132 1072
b34976b6 1073static bfd_boolean
c39a58e6
AM
1074elf32_hppa_check_relocs (bfd *abfd,
1075 struct bfd_link_info *info,
1076 asection *sec,
1077 const Elf_Internal_Rela *relocs)
252b5132 1078{
30667bf3 1079 Elf_Internal_Shdr *symtab_hdr;
875c0872
DA
1080 struct elf_link_hash_entry **eh_syms;
1081 const Elf_Internal_Rela *rela;
1082 const Elf_Internal_Rela *rela_end;
83c81bfe 1083 struct elf32_hppa_link_hash_table *htab;
30667bf3
AM
1084 asection *sreloc;
1085 asection *stubreloc;
1086
1049f94e 1087 if (info->relocatable)
b34976b6 1088 return TRUE;
30667bf3 1089
83c81bfe 1090 htab = hppa_link_hash_table (info);
30667bf3 1091 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
875c0872 1092 eh_syms = elf_sym_hashes (abfd);
30667bf3
AM
1093 sreloc = NULL;
1094 stubreloc = NULL;
1095
875c0872
DA
1096 rela_end = relocs + sec->reloc_count;
1097 for (rela = relocs; rela < rela_end; rela++)
30667bf3
AM
1098 {
1099 enum {
1100 NEED_GOT = 1,
1101 NEED_PLT = 2,
1102 NEED_DYNREL = 4,
98ceb8ce 1103 PLT_PLABEL = 8
30667bf3 1104 };
edd21aca 1105
30667bf3 1106 unsigned int r_symndx, r_type;
875c0872
DA
1107 struct elf32_hppa_link_hash_entry *hh;
1108 int need_entry = 0;
252b5132 1109
875c0872 1110 r_symndx = ELF32_R_SYM (rela->r_info);
252b5132 1111
30667bf3 1112 if (r_symndx < symtab_hdr->sh_info)
875c0872 1113 hh = NULL;
30667bf3 1114 else
f7c5057a 1115 {
875c0872 1116 hh = hppa_elf_hash_entry (eh_syms[r_symndx - symtab_hdr->sh_info]);
a63e02c7
DA
1117 while (hh->eh.root.type == bfd_link_hash_indirect
1118 || hh->eh.root.type == bfd_link_hash_warning)
1119 hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
f7c5057a 1120 }
252b5132 1121
875c0872 1122 r_type = ELF32_R_TYPE (rela->r_info);
252b5132 1123
30667bf3
AM
1124 switch (r_type)
1125 {
1126 case R_PARISC_DLTIND14F:
1127 case R_PARISC_DLTIND14R:
1128 case R_PARISC_DLTIND21L:
1129 /* This symbol requires a global offset table entry. */
1130 need_entry = NEED_GOT;
30667bf3
AM
1131 break;
1132
1133 case R_PARISC_PLABEL14R: /* "Official" procedure labels. */
1134 case R_PARISC_PLABEL21L:
1135 case R_PARISC_PLABEL32:
74d1c347 1136 /* If the addend is non-zero, we break badly. */
875c0872 1137 if (rela->r_addend != 0)
49e9d0d3 1138 abort ();
74d1c347
AM
1139
1140 /* If we are creating a shared library, then we need to
1141 create a PLT entry for all PLABELs, because PLABELs with
1142 local symbols may be passed via a pointer to another
1143 object. Additionally, output a dynamic relocation
4dc86686 1144 pointing to the PLT entry.
875c0872 1145
4dc86686
AM
1146 For executables, the original 32-bit ABI allowed two
1147 different styles of PLABELs (function pointers): For
1148 global functions, the PLABEL word points into the .plt
1149 two bytes past a (function address, gp) pair, and for
1150 local functions the PLABEL points directly at the
1151 function. The magic +2 for the first type allows us to
1152 differentiate between the two. As you can imagine, this
1153 is a real pain when it comes to generating code to call
1154 functions indirectly or to compare function pointers.
1155 We avoid the mess by always pointing a PLABEL into the
1156 .plt, even for local functions. */
74d1c347 1157 need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
30667bf3
AM
1158 break;
1159
1160 case R_PARISC_PCREL12F:
83c81bfe 1161 htab->has_12bit_branch = 1;
067fa4a6
AM
1162 goto branch_common;
1163
30667bf3
AM
1164 case R_PARISC_PCREL17C:
1165 case R_PARISC_PCREL17F:
83c81bfe 1166 htab->has_17bit_branch = 1;
067fa4a6
AM
1167 goto branch_common;
1168
30667bf3 1169 case R_PARISC_PCREL22F:
067fa4a6
AM
1170 htab->has_22bit_branch = 1;
1171 branch_common:
47d89dba
AM
1172 /* Function calls might need to go through the .plt, and
1173 might require long branch stubs. */
875c0872 1174 if (hh == NULL)
30667bf3
AM
1175 {
1176 /* We know local syms won't need a .plt entry, and if
1177 they need a long branch stub we can't guarantee that
1178 we can reach the stub. So just flag an error later
1179 if we're doing a shared link and find we need a long
1180 branch stub. */
1181 continue;
1182 }
1183 else
1184 {
1185 /* Global symbols will need a .plt entry if they remain
1186 global, and in most cases won't need a long branch
1187 stub. Unfortunately, we have to cater for the case
1188 where a symbol is forced local by versioning, or due
1189 to symbolic linking, and we lose the .plt entry. */
98ceb8ce 1190 need_entry = NEED_PLT;
a63e02c7 1191 if (hh->eh.type == STT_PARISC_MILLI)
98ceb8ce 1192 need_entry = 0;
30667bf3
AM
1193 }
1194 break;
1195
36751eee 1196 case R_PARISC_SEGBASE: /* Used to set segment base. */
c46b7515 1197 case R_PARISC_SEGREL32: /* Relative reloc, used for unwind. */
30667bf3
AM
1198 case R_PARISC_PCREL14F: /* PC relative load/store. */
1199 case R_PARISC_PCREL14R:
1200 case R_PARISC_PCREL17R: /* External branches. */
1201 case R_PARISC_PCREL21L: /* As above, and for load/store too. */
36751eee 1202 case R_PARISC_PCREL32:
30667bf3
AM
1203 /* We don't need to propagate the relocation if linking a
1204 shared object since these are section relative. */
1205 continue;
1206
1207 case R_PARISC_DPREL14F: /* Used for gp rel data load/store. */
1208 case R_PARISC_DPREL14R:
1209 case R_PARISC_DPREL21L:
1210 if (info->shared)
1211 {
1212 (*_bfd_error_handler)
d003868e
AM
1213 (_("%B: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1214 abfd,
30667bf3
AM
1215 elf_hppa_howto_table[r_type].name);
1216 bfd_set_error (bfd_error_bad_value);
b34976b6 1217 return FALSE;
30667bf3
AM
1218 }
1219 /* Fall through. */
1220
1221 case R_PARISC_DIR17F: /* Used for external branches. */
1222 case R_PARISC_DIR17R:
47d89dba
AM
1223 case R_PARISC_DIR14F: /* Used for load/store from absolute locn. */
1224 case R_PARISC_DIR14R:
30667bf3 1225 case R_PARISC_DIR21L: /* As above, and for ext branches too. */
c46b7515 1226 case R_PARISC_DIR32: /* .word relocs. */
30667bf3
AM
1227 /* We may want to output a dynamic relocation later. */
1228 need_entry = NEED_DYNREL;
1229 break;
1230
1231 /* This relocation describes the C++ object vtable hierarchy.
1232 Reconstruct it for later use during GC. */
1233 case R_PARISC_GNU_VTINHERIT:
a63e02c7 1234 if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh, rela->r_offset))
b34976b6 1235 return FALSE;
30667bf3
AM
1236 continue;
1237
1238 /* This relocation describes which C++ vtable entries are actually
1239 used. Record for later use during GC. */
1240 case R_PARISC_GNU_VTENTRY:
a63e02c7 1241 if (!bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rela->r_addend))
b34976b6 1242 return FALSE;
30667bf3
AM
1243 continue;
1244
1245 default:
1246 continue;
1247 }
1248
1249 /* Now carry out our orders. */
1250 if (need_entry & NEED_GOT)
1251 {
1252 /* Allocate space for a GOT entry, as well as a dynamic
25f72752 1253 relocation for this entry. */
83c81bfe 1254 if (htab->sgot == NULL)
30667bf3 1255 {
a63e02c7
DA
1256 if (htab->etab.dynobj == NULL)
1257 htab->etab.dynobj = abfd;
1258 if (!elf32_hppa_create_dynamic_sections (htab->etab.dynobj, info))
b34976b6 1259 return FALSE;
30667bf3
AM
1260 }
1261
875c0872 1262 if (hh != NULL)
30667bf3 1263 {
a63e02c7 1264 hh->eh.got.refcount += 1;
30667bf3
AM
1265 }
1266 else
1267 {
3ac8354b 1268 bfd_signed_vma *local_got_refcounts;
875c0872 1269 /* This is a global offset table entry for a local symbol. */
3ac8354b 1270 local_got_refcounts = elf_local_got_refcounts (abfd);
30667bf3
AM
1271 if (local_got_refcounts == NULL)
1272 {
dc810e39 1273 bfd_size_type size;
30667bf3 1274
74d1c347
AM
1275 /* Allocate space for local got offsets and local
1276 plt offsets. Done this way to save polluting
1277 elf_obj_tdata with another target specific
1278 pointer. */
dc810e39
AM
1279 size = symtab_hdr->sh_info;
1280 size *= 2 * sizeof (bfd_signed_vma);
c39a58e6 1281 local_got_refcounts = bfd_zalloc (abfd, size);
30667bf3 1282 if (local_got_refcounts == NULL)
b34976b6 1283 return FALSE;
30667bf3 1284 elf_local_got_refcounts (abfd) = local_got_refcounts;
30667bf3 1285 }
ebe50bae 1286 local_got_refcounts[r_symndx] += 1;
30667bf3
AM
1287 }
1288 }
1289
1290 if (need_entry & NEED_PLT)
1291 {
1292 /* If we are creating a shared library, and this is a reloc
1293 against a weak symbol or a global symbol in a dynamic
1294 object, then we will be creating an import stub and a
1295 .plt entry for the symbol. Similarly, on a normal link
1296 to symbols defined in a dynamic object we'll need the
1297 import stub and a .plt entry. We don't know yet whether
1298 the symbol is defined or not, so make an entry anyway and
1299 clean up later in adjust_dynamic_symbol. */
1300 if ((sec->flags & SEC_ALLOC) != 0)
1301 {
875c0872 1302 if (hh != NULL)
30667bf3 1303 {
a63e02c7
DA
1304 hh->eh.needs_plt = 1;
1305 hh->eh.plt.refcount += 1;
74d1c347 1306
36605136
AM
1307 /* If this .plt entry is for a plabel, mark it so
1308 that adjust_dynamic_symbol will keep the entry
1309 even if it appears to be local. */
74d1c347 1310 if (need_entry & PLT_PLABEL)
875c0872 1311 hh->plabel = 1;
74d1c347
AM
1312 }
1313 else if (need_entry & PLT_PLABEL)
1314 {
3ac8354b 1315 bfd_signed_vma *local_got_refcounts;
68fb2e56 1316 bfd_signed_vma *local_plt_refcounts;
74d1c347 1317
3ac8354b 1318 local_got_refcounts = elf_local_got_refcounts (abfd);
74d1c347
AM
1319 if (local_got_refcounts == NULL)
1320 {
dc810e39 1321 bfd_size_type size;
74d1c347
AM
1322
1323 /* Allocate space for local got offsets and local
1324 plt offsets. */
dc810e39
AM
1325 size = symtab_hdr->sh_info;
1326 size *= 2 * sizeof (bfd_signed_vma);
c39a58e6 1327 local_got_refcounts = bfd_zalloc (abfd, size);
74d1c347 1328 if (local_got_refcounts == NULL)
b34976b6 1329 return FALSE;
74d1c347 1330 elf_local_got_refcounts (abfd) = local_got_refcounts;
74d1c347 1331 }
68fb2e56
AM
1332 local_plt_refcounts = (local_got_refcounts
1333 + symtab_hdr->sh_info);
ebe50bae 1334 local_plt_refcounts[r_symndx] += 1;
30667bf3 1335 }
30667bf3
AM
1336 }
1337 }
1338
98ceb8ce 1339 if (need_entry & NEED_DYNREL)
30667bf3
AM
1340 {
1341 /* Flag this symbol as having a non-got, non-plt reference
1342 so that we generate copy relocs if it turns out to be
1343 dynamic. */
875c0872 1344 if (hh != NULL && !info->shared)
a63e02c7 1345 hh->eh.non_got_ref = 1;
30667bf3
AM
1346
1347 /* If we are creating a shared library then we need to copy
1348 the reloc into the shared library. However, if we are
1349 linking with -Bsymbolic, we need only copy absolute
1350 relocs or relocs against symbols that are not defined in
1351 an object we are including in the link. PC- or DP- or
1352 DLT-relative relocs against any local sym or global sym
1353 with DEF_REGULAR set, can be discarded. At this point we
1354 have not seen all the input files, so it is possible that
1355 DEF_REGULAR is not set now but will be set later (it is
1356 never cleared). We account for that possibility below by
98ceb8ce 1357 storing information in the dyn_relocs field of the
30667bf3
AM
1358 hash table entry.
1359
1360 A similar situation to the -Bsymbolic case occurs when
1361 creating shared libraries and symbol visibility changes
1362 render the symbol local.
1363
1364 As it turns out, all the relocs we will be creating here
1365 are absolute, so we cannot remove them on -Bsymbolic
1366 links or visibility changes anyway. A STUB_REL reloc
1367 is absolute too, as in that case it is the reloc in the
1368 stub we will be creating, rather than copying the PCREL
56882138
AM
1369 reloc in the branch.
1370
1371 If on the other hand, we are creating an executable, we
1372 may need to keep relocations for symbols satisfied by a
1373 dynamic library if we manage to avoid copy relocs for the
1374 symbol. */
446f2863
AM
1375 if ((info->shared
1376 && (sec->flags & SEC_ALLOC) != 0
1377 && (IS_ABSOLUTE_RELOC (r_type)
875c0872 1378 || (hh != NULL
446f2863 1379 && (!info->symbolic
a63e02c7
DA
1380 || hh->eh.root.type == bfd_link_hash_defweak
1381 || !hh->eh.def_regular))))
4fc8051d
AM
1382 || (ELIMINATE_COPY_RELOCS
1383 && !info->shared
446f2863 1384 && (sec->flags & SEC_ALLOC) != 0
875c0872 1385 && hh != NULL
a63e02c7
DA
1386 && (hh->eh.root.type == bfd_link_hash_defweak
1387 || !hh->eh.def_regular)))
30667bf3 1388 {
875c0872
DA
1389 struct elf32_hppa_dyn_reloc_entry *hdh_p;
1390 struct elf32_hppa_dyn_reloc_entry **hdh_head;
ec338859 1391
30667bf3
AM
1392 /* Create a reloc section in dynobj and make room for
1393 this reloc. */
98ceb8ce 1394 if (sreloc == NULL)
30667bf3
AM
1395 {
1396 char *name;
3ac8354b 1397 bfd *dynobj;
30667bf3 1398
98ceb8ce
AM
1399 name = (bfd_elf_string_from_elf_section
1400 (abfd,
1401 elf_elfheader (abfd)->e_shstrndx,
1402 elf_section_data (sec)->rel_hdr.sh_name));
30667bf3
AM
1403 if (name == NULL)
1404 {
1405 (*_bfd_error_handler)
1406 (_("Could not find relocation section for %s"),
1407 sec->name);
1408 bfd_set_error (bfd_error_bad_value);
b34976b6 1409 return FALSE;
30667bf3
AM
1410 }
1411
a63e02c7
DA
1412 if (htab->etab.dynobj == NULL)
1413 htab->etab.dynobj = abfd;
3ac8354b 1414
a63e02c7 1415 dynobj = htab->etab.dynobj;
98ceb8ce
AM
1416 sreloc = bfd_get_section_by_name (dynobj, name);
1417 if (sreloc == NULL)
30667bf3
AM
1418 {
1419 flagword flags;
1420
30667bf3
AM
1421 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1422 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1423 if ((sec->flags & SEC_ALLOC) != 0)
1424 flags |= SEC_ALLOC | SEC_LOAD;
3496cb2a
L
1425 sreloc = bfd_make_section_with_flags (dynobj,
1426 name,
1427 flags);
98ceb8ce 1428 if (sreloc == NULL
98ceb8ce 1429 || !bfd_set_section_alignment (dynobj, sreloc, 2))
b34976b6 1430 return FALSE;
30667bf3 1431 }
30667bf3 1432
98ceb8ce 1433 elf_section_data (sec)->sreloc = sreloc;
30667bf3
AM
1434 }
1435
98ceb8ce
AM
1436 /* If this is a global symbol, we count the number of
1437 relocations we need for this symbol. */
875c0872 1438 if (hh != NULL)
30667bf3 1439 {
875c0872 1440 hdh_head = &hh->dyn_relocs;
ec338859
AM
1441 }
1442 else
1443 {
1444 /* Track dynamic relocs needed for local syms too.
1445 We really need local syms available to do this
1446 easily. Oh well. */
1447
875c0872 1448 asection *sr;
6edfbbad
DJ
1449 void *vpp;
1450
875c0872
DA
1451 sr = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1452 sec, r_symndx);
1453 if (sr == NULL)
b34976b6 1454 return FALSE;
30667bf3 1455
6edfbbad
DJ
1456 vpp = &elf_section_data (sr)->local_dynrel;
1457 hdh_head = (struct elf32_hppa_dyn_reloc_entry **) vpp;
ec338859
AM
1458 }
1459
875c0872
DA
1460 hdh_p = *hdh_head;
1461 if (hdh_p == NULL || hdh_p->sec != sec)
ec338859 1462 {
a63e02c7 1463 hdh_p = bfd_alloc (htab->etab.dynobj, sizeof *hdh_p);
875c0872 1464 if (hdh_p == NULL)
b34976b6 1465 return FALSE;
a63e02c7 1466 hdh_p->hdh_next = *hdh_head;
875c0872
DA
1467 *hdh_head = hdh_p;
1468 hdh_p->sec = sec;
1469 hdh_p->count = 0;
98ceb8ce 1470#if RELATIVE_DYNRELOCS
875c0872 1471 hdh_p->relative_count = 0;
98ceb8ce 1472#endif
ec338859 1473 }
98ceb8ce 1474
875c0872 1475 hdh_p->count += 1;
98ceb8ce 1476#if RELATIVE_DYNRELOCS
ec338859 1477 if (!IS_ABSOLUTE_RELOC (rtype))
875c0872 1478 hdh_p->relative_count += 1;
98ceb8ce 1479#endif
30667bf3
AM
1480 }
1481 }
1482 }
edd21aca 1483
b34976b6 1484 return TRUE;
edd21aca
AM
1485}
1486
30667bf3
AM
1487/* Return the section that should be marked against garbage collection
1488 for a given relocation. */
1489
1490static asection *
c39a58e6
AM
1491elf32_hppa_gc_mark_hook (asection *sec,
1492 struct bfd_link_info *info ATTRIBUTE_UNUSED,
875c0872
DA
1493 Elf_Internal_Rela *rela,
1494 struct elf_link_hash_entry *hh,
c39a58e6 1495 Elf_Internal_Sym *sym)
30667bf3 1496{
875c0872 1497 if (hh != NULL)
30667bf3 1498 {
875c0872 1499 switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
30667bf3
AM
1500 {
1501 case R_PARISC_GNU_VTINHERIT:
1502 case R_PARISC_GNU_VTENTRY:
1503 break;
1504
1505 default:
875c0872 1506 switch (hh->root.type)
30667bf3
AM
1507 {
1508 case bfd_link_hash_defined:
1509 case bfd_link_hash_defweak:
875c0872 1510 return hh->root.u.def.section;
30667bf3
AM
1511
1512 case bfd_link_hash_common:
875c0872 1513 return hh->root.u.c.p->section;
30667bf3
AM
1514
1515 default:
1516 break;
1517 }
1518 }
1519 }
1520 else
1e2f5b6e 1521 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
30667bf3
AM
1522
1523 return NULL;
1524}
1525
30667bf3
AM
1526/* Update the got and plt entry reference counts for the section being
1527 removed. */
edd21aca 1528
b34976b6 1529static bfd_boolean
c39a58e6
AM
1530elf32_hppa_gc_sweep_hook (bfd *abfd,
1531 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1532 asection *sec,
1533 const Elf_Internal_Rela *relocs)
edd21aca 1534{
30667bf3 1535 Elf_Internal_Shdr *symtab_hdr;
875c0872 1536 struct elf_link_hash_entry **eh_syms;
30667bf3 1537 bfd_signed_vma *local_got_refcounts;
74d1c347 1538 bfd_signed_vma *local_plt_refcounts;
875c0872 1539 const Elf_Internal_Rela *rela, *relend;
30667bf3 1540
ec338859 1541 elf_section_data (sec)->local_dynrel = NULL;
98ceb8ce 1542
30667bf3 1543 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
875c0872 1544 eh_syms = elf_sym_hashes (abfd);
30667bf3 1545 local_got_refcounts = elf_local_got_refcounts (abfd);
74d1c347
AM
1546 local_plt_refcounts = local_got_refcounts;
1547 if (local_plt_refcounts != NULL)
1548 local_plt_refcounts += symtab_hdr->sh_info;
30667bf3 1549
30667bf3 1550 relend = relocs + sec->reloc_count;
875c0872 1551 for (rela = relocs; rela < relend; rela++)
26e41594
AM
1552 {
1553 unsigned long r_symndx;
1554 unsigned int r_type;
875c0872 1555 struct elf_link_hash_entry *eh = NULL;
26e41594 1556
875c0872 1557 r_symndx = ELF32_R_SYM (rela->r_info);
26e41594
AM
1558 if (r_symndx >= symtab_hdr->sh_info)
1559 {
875c0872
DA
1560 struct elf32_hppa_link_hash_entry *hh;
1561 struct elf32_hppa_dyn_reloc_entry **hdh_pp;
1562 struct elf32_hppa_dyn_reloc_entry *hdh_p;
26e41594 1563
875c0872
DA
1564 eh = eh_syms[r_symndx - symtab_hdr->sh_info];
1565 while (eh->root.type == bfd_link_hash_indirect
1566 || eh->root.type == bfd_link_hash_warning)
1567 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
1568 hh = hppa_elf_hash_entry (eh);
26e41594 1569
a63e02c7 1570 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; hdh_pp = &hdh_p->hdh_next)
875c0872 1571 if (hdh_p->sec == sec)
26e41594
AM
1572 {
1573 /* Everything must go for SEC. */
a63e02c7 1574 *hdh_pp = hdh_p->hdh_next;
26e41594
AM
1575 break;
1576 }
1577 }
1578
875c0872 1579 r_type = ELF32_R_TYPE (rela->r_info);
26e41594
AM
1580 switch (r_type)
1581 {
1582 case R_PARISC_DLTIND14F:
1583 case R_PARISC_DLTIND14R:
1584 case R_PARISC_DLTIND21L:
875c0872 1585 if (eh != NULL)
26e41594 1586 {
875c0872
DA
1587 if (eh->got.refcount > 0)
1588 eh->got.refcount -= 1;
26e41594
AM
1589 }
1590 else if (local_got_refcounts != NULL)
1591 {
1592 if (local_got_refcounts[r_symndx] > 0)
1593 local_got_refcounts[r_symndx] -= 1;
1594 }
1595 break;
98ceb8ce 1596
26e41594
AM
1597 case R_PARISC_PCREL12F:
1598 case R_PARISC_PCREL17C:
1599 case R_PARISC_PCREL17F:
1600 case R_PARISC_PCREL22F:
875c0872 1601 if (eh != NULL)
26e41594 1602 {
875c0872
DA
1603 if (eh->plt.refcount > 0)
1604 eh->plt.refcount -= 1;
26e41594
AM
1605 }
1606 break;
1607
1608 case R_PARISC_PLABEL14R:
1609 case R_PARISC_PLABEL21L:
1610 case R_PARISC_PLABEL32:
875c0872 1611 if (eh != NULL)
26e41594 1612 {
875c0872
DA
1613 if (eh->plt.refcount > 0)
1614 eh->plt.refcount -= 1;
26e41594
AM
1615 }
1616 else if (local_plt_refcounts != NULL)
1617 {
1618 if (local_plt_refcounts[r_symndx] > 0)
1619 local_plt_refcounts[r_symndx] -= 1;
1620 }
1621 break;
1622
1623 default:
1624 break;
1625 }
1626 }
252b5132 1627
b34976b6 1628 return TRUE;
252b5132
RH
1629}
1630
edfc032f
AM
1631/* Support for core dump NOTE sections. */
1632
1633static bfd_boolean
1634elf32_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1635{
1636 int offset;
1637 size_t size;
1638
1639 switch (note->descsz)
1640 {
1641 default:
1642 return FALSE;
1643
1644 case 396: /* Linux/hppa */
1645 /* pr_cursig */
1646 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1647
1648 /* pr_pid */
1649 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1650
1651 /* pr_reg */
1652 offset = 72;
1653 size = 320;
1654
1655 break;
1656 }
1657
1658 /* Make a ".reg/999" section. */
1659 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1660 size, note->descpos + offset);
1661}
1662
1663static bfd_boolean
1664elf32_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1665{
1666 switch (note->descsz)
1667 {
1668 default:
1669 return FALSE;
1670
1671 case 124: /* Linux/hppa elf_prpsinfo. */
1672 elf_tdata (abfd)->core_program
1673 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1674 elf_tdata (abfd)->core_command
1675 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1676 }
1677
1678 /* Note that for some reason, a spurious space is tacked
1679 onto the end of the args in some (at least one anyway)
1680 implementations, so strip it off if it exists. */
1681 {
1682 char *command = elf_tdata (abfd)->core_command;
1683 int n = strlen (command);
1684
1685 if (0 < n && command[n - 1] == ' ')
1686 command[n - 1] = '\0';
1687 }
1688
1689 return TRUE;
1690}
1691
74d1c347
AM
1692/* Our own version of hide_symbol, so that we can keep plt entries for
1693 plabels. */
1694
1695static void
c39a58e6 1696elf32_hppa_hide_symbol (struct bfd_link_info *info,
875c0872 1697 struct elf_link_hash_entry *eh,
c39a58e6 1698 bfd_boolean force_local)
74d1c347 1699{
e5094212
AM
1700 if (force_local)
1701 {
875c0872
DA
1702 eh->forced_local = 1;
1703 if (eh->dynindx != -1)
e5094212 1704 {
875c0872 1705 eh->dynindx = -1;
e5094212 1706 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
875c0872 1707 eh->dynstr_index);
e5094212
AM
1708 }
1709 }
1710
875c0872 1711 if (! hppa_elf_hash_entry(eh)->plabel)
74d1c347 1712 {
875c0872
DA
1713 eh->needs_plt = 0;
1714 eh->plt = elf_hash_table (info)->init_plt_refcount;
74d1c347
AM
1715 }
1716}
1717
30667bf3
AM
1718/* Adjust a symbol defined by a dynamic object and referenced by a
1719 regular object. The current definition is in some section of the
1720 dynamic object, but we're not including those sections. We have to
1721 change the definition to something the rest of the link can
1722 understand. */
252b5132 1723
b34976b6 1724static bfd_boolean
c39a58e6 1725elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
875c0872 1726 struct elf_link_hash_entry *eh)
252b5132 1727{
83c81bfe 1728 struct elf32_hppa_link_hash_table *htab;
875c0872 1729 asection *sec;
3ac8354b 1730 unsigned int power_of_two;
30667bf3
AM
1731
1732 /* If this is a function, put it in the procedure linkage table. We
067fa4a6 1733 will fill in the contents of the procedure linkage table later. */
875c0872
DA
1734 if (eh->type == STT_FUNC
1735 || eh->needs_plt)
30667bf3 1736 {
875c0872
DA
1737 if (eh->plt.refcount <= 0
1738 || (eh->def_regular
1739 && eh->root.type != bfd_link_hash_defweak
1740 && ! hppa_elf_hash_entry (eh)->plabel
30667bf3
AM
1741 && (!info->shared || info->symbolic)))
1742 {
1743 /* The .plt entry is not needed when:
1744 a) Garbage collection has removed all references to the
1745 symbol, or
1746 b) We know for certain the symbol is defined in this
74d1c347
AM
1747 object, and it's not a weak definition, nor is the symbol
1748 used by a plabel relocation. Either this object is the
1749 application or we are doing a shared symbolic link. */
1750
875c0872
DA
1751 eh->plt.offset = (bfd_vma) -1;
1752 eh->needs_plt = 0;
30667bf3 1753 }
4dc86686 1754
b34976b6 1755 return TRUE;
30667bf3 1756 }
bbd7ec4a 1757 else
875c0872 1758 eh->plt.offset = (bfd_vma) -1;
edd21aca 1759
30667bf3
AM
1760 /* If this is a weak symbol, and there is a real definition, the
1761 processor independent code will have arranged for us to see the
1762 real definition first, and we can just use the same value. */
875c0872 1763 if (eh->u.weakdef != NULL)
edd21aca 1764 {
875c0872
DA
1765 if (eh->u.weakdef->root.type != bfd_link_hash_defined
1766 && eh->u.weakdef->root.type != bfd_link_hash_defweak)
49e9d0d3 1767 abort ();
875c0872
DA
1768 eh->root.u.def.section = eh->u.weakdef->root.u.def.section;
1769 eh->root.u.def.value = eh->u.weakdef->root.u.def.value;
4fc8051d 1770 if (ELIMINATE_COPY_RELOCS)
875c0872 1771 eh->non_got_ref = eh->u.weakdef->non_got_ref;
b34976b6 1772 return TRUE;
30667bf3 1773 }
edd21aca 1774
30667bf3
AM
1775 /* This is a reference to a symbol defined by a dynamic object which
1776 is not a function. */
1777
1778 /* If we are creating a shared library, we must presume that the
1779 only references to the symbol are via the global offset table.
1780 For such cases we need not do anything here; the relocations will
1781 be handled correctly by relocate_section. */
1782 if (info->shared)
b34976b6 1783 return TRUE;
30667bf3
AM
1784
1785 /* If there are no references to this symbol that do not use the
1786 GOT, we don't need to generate a copy reloc. */
875c0872 1787 if (!eh->non_got_ref)
b34976b6 1788 return TRUE;
30667bf3 1789
4fc8051d 1790 if (ELIMINATE_COPY_RELOCS)
ebe50bae 1791 {
875c0872
DA
1792 struct elf32_hppa_link_hash_entry *hh;
1793 struct elf32_hppa_dyn_reloc_entry *hdh_p;
ebe50bae 1794
875c0872 1795 hh = hppa_elf_hash_entry (eh);
a63e02c7 1796 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
4fc8051d 1797 {
875c0872
DA
1798 sec = hdh_p->sec->output_section;
1799 if (sec != NULL && (sec->flags & SEC_READONLY) != 0)
4fc8051d
AM
1800 break;
1801 }
1802
1803 /* If we didn't find any dynamic relocs in read-only sections, then
1804 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
875c0872 1805 if (hdh_p == NULL)
4fc8051d 1806 {
875c0872 1807 eh->non_got_ref = 0;
4fc8051d
AM
1808 return TRUE;
1809 }
ebe50bae
AM
1810 }
1811
909272ee
AM
1812 if (eh->size == 0)
1813 {
1814 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1815 eh->root.root.string);
1816 return TRUE;
1817 }
1818
30667bf3
AM
1819 /* We must allocate the symbol in our .dynbss section, which will
1820 become part of the .bss section of the executable. There will be
1821 an entry for this symbol in the .dynsym section. The dynamic
1822 object will contain position independent code, so all references
1823 from the dynamic object to this symbol will go through the global
1824 offset table. The dynamic linker will use the .dynsym entry to
1825 determine the address it must put in the global offset table, so
1826 both the dynamic object and the regular object will refer to the
1827 same memory location for the variable. */
1828
3ac8354b 1829 htab = hppa_link_hash_table (info);
30667bf3
AM
1830
1831 /* We must generate a COPY reloc to tell the dynamic linker to
1832 copy the initial value out of the dynamic object and into the
3ac8354b 1833 runtime process image. */
875c0872 1834 if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0)
30667bf3 1835 {
eea6121a 1836 htab->srelbss->size += sizeof (Elf32_External_Rela);
875c0872 1837 eh->needs_copy = 1;
edd21aca 1838 }
252b5132 1839
3ac8354b
AM
1840 /* We need to figure out the alignment required for this symbol. I
1841 have no idea how other ELF linkers handle this. */
30667bf3 1842
875c0872 1843 power_of_two = bfd_log2 (eh->size);
3ac8354b
AM
1844 if (power_of_two > 3)
1845 power_of_two = 3;
1846
1847 /* Apply the required alignment. */
875c0872
DA
1848 sec = htab->sdynbss;
1849 sec->size = BFD_ALIGN (sec->size, (bfd_size_type) (1 << power_of_two));
a63e02c7 1850 if (power_of_two > bfd_get_section_alignment (htab->etab.dynobj, sec))
3ac8354b 1851 {
a63e02c7 1852 if (! bfd_set_section_alignment (htab->etab.dynobj, sec, power_of_two))
b34976b6 1853 return FALSE;
3ac8354b 1854 }
30667bf3 1855
30667bf3 1856 /* Define the symbol as being at this point in the section. */
875c0872
DA
1857 eh->root.u.def.section = sec;
1858 eh->root.u.def.value = sec->size;
edd21aca 1859
30667bf3 1860 /* Increment the section size to make room for the symbol. */
875c0872 1861 sec->size += eh->size;
252b5132 1862
b34976b6 1863 return TRUE;
252b5132
RH
1864}
1865
e5ee5df1 1866/* Allocate space in the .plt for entries that won't have relocations.
a252afa4 1867 ie. plabel entries. */
a8d02d66 1868
b34976b6 1869static bfd_boolean
875c0872 1870allocate_plt_static (struct elf_link_hash_entry *eh, void *inf)
a8d02d66
AM
1871{
1872 struct bfd_link_info *info;
1873 struct elf32_hppa_link_hash_table *htab;
875c0872
DA
1874 struct elf32_hppa_link_hash_entry *hh;
1875 asection *sec;
a8d02d66 1876
875c0872 1877 if (eh->root.type == bfd_link_hash_indirect)
b34976b6 1878 return TRUE;
a8d02d66 1879
875c0872
DA
1880 if (eh->root.type == bfd_link_hash_warning)
1881 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
e92d460e 1882
875c0872
DA
1883 info = (struct bfd_link_info *) inf;
1884 hh = hppa_elf_hash_entry(eh);
a8d02d66 1885 htab = hppa_link_hash_table (info);
a63e02c7 1886 if (htab->etab.dynamic_sections_created
875c0872 1887 && eh->plt.refcount > 0)
e5ee5df1
AM
1888 {
1889 /* Make sure this symbol is output as a dynamic symbol.
1890 Undefined weak syms won't yet be marked as dynamic. */
875c0872
DA
1891 if (eh->dynindx == -1
1892 && !eh->forced_local
1893 && eh->type != STT_PARISC_MILLI)
a8d02d66 1894 {
875c0872 1895 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
b34976b6 1896 return FALSE;
e5ee5df1
AM
1897 }
1898
875c0872 1899 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, eh))
e5ee5df1 1900 {
067fa4a6
AM
1901 /* Allocate these later. From this point on, h->plabel
1902 means that the plt entry is only used by a plabel.
1903 We'll be using a normal plt entry for this symbol, so
1904 clear the plabel indicator. */
875c0872
DA
1905
1906 hh->plabel = 0;
e5ee5df1 1907 }
875c0872 1908 else if (hh->plabel)
e5ee5df1
AM
1909 {
1910 /* Make an entry in the .plt section for plabel references
1911 that won't have a .plt entry for other reasons. */
875c0872
DA
1912 sec = htab->splt;
1913 eh->plt.offset = sec->size;
1914 sec->size += PLT_ENTRY_SIZE;
a8d02d66
AM
1915 }
1916 else
e5ee5df1
AM
1917 {
1918 /* No .plt entry needed. */
875c0872
DA
1919 eh->plt.offset = (bfd_vma) -1;
1920 eh->needs_plt = 0;
e5ee5df1
AM
1921 }
1922 }
1923 else
1924 {
875c0872
DA
1925 eh->plt.offset = (bfd_vma) -1;
1926 eh->needs_plt = 0;
a8d02d66
AM
1927 }
1928
b34976b6 1929 return TRUE;
a8d02d66
AM
1930}
1931
4dc86686
AM
1932/* Allocate space in .plt, .got and associated reloc sections for
1933 global syms. */
1934
b34976b6 1935static bfd_boolean
875c0872 1936allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
4dc86686
AM
1937{
1938 struct bfd_link_info *info;
83c81bfe 1939 struct elf32_hppa_link_hash_table *htab;
875c0872
DA
1940 asection *sec;
1941 struct elf32_hppa_link_hash_entry *hh;
1942 struct elf32_hppa_dyn_reloc_entry *hdh_p;
4dc86686 1943
875c0872 1944 if (eh->root.type == bfd_link_hash_indirect)
b34976b6 1945 return TRUE;
73a74a62 1946
875c0872
DA
1947 if (eh->root.type == bfd_link_hash_warning)
1948 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
e92d460e 1949
c39a58e6 1950 info = inf;
83c81bfe 1951 htab = hppa_link_hash_table (info);
875c0872
DA
1952 hh = hppa_elf_hash_entry (eh);
1953
a63e02c7 1954 if (htab->etab.dynamic_sections_created
875c0872
DA
1955 && eh->plt.offset != (bfd_vma) -1
1956 && !hh->plabel
1957 && eh->plt.refcount > 0)
4dc86686 1958 {
e5ee5df1 1959 /* Make an entry in the .plt section. */
875c0872
DA
1960 sec = htab->splt;
1961 eh->plt.offset = sec->size;
1962 sec->size += PLT_ENTRY_SIZE;
3ac8354b 1963
e5ee5df1 1964 /* We also need to make an entry in the .rela.plt section. */
eea6121a 1965 htab->srelplt->size += sizeof (Elf32_External_Rela);
e5ee5df1 1966 htab->need_plt_stub = 1;
4dc86686 1967 }
edd21aca 1968
875c0872 1969 if (eh->got.refcount > 0)
4dc86686 1970 {
446f2863
AM
1971 /* Make sure this symbol is output as a dynamic symbol.
1972 Undefined weak syms won't yet be marked as dynamic. */
875c0872
DA
1973 if (eh->dynindx == -1
1974 && !eh->forced_local
1975 && eh->type != STT_PARISC_MILLI)
446f2863 1976 {
875c0872 1977 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
b34976b6 1978 return FALSE;
446f2863
AM
1979 }
1980
875c0872
DA
1981 sec = htab->sgot;
1982 eh->got.offset = sec->size;
1983 sec->size += GOT_ENTRY_SIZE;
a63e02c7 1984 if (htab->etab.dynamic_sections_created
ce757d15 1985 && (info->shared
875c0872
DA
1986 || (eh->dynindx != -1
1987 && !eh->forced_local)))
ce757d15 1988 {
eea6121a 1989 htab->srelgot->size += sizeof (Elf32_External_Rela);
ce757d15 1990 }
4dc86686
AM
1991 }
1992 else
875c0872 1993 eh->got.offset = (bfd_vma) -1;
30667bf3 1994
875c0872 1995 if (hh->dyn_relocs == NULL)
b34976b6 1996 return TRUE;
30667bf3 1997
98ceb8ce
AM
1998 /* If this is a -Bsymbolic shared link, then we need to discard all
1999 space allocated for dynamic pc-relative relocs against symbols
2000 defined in a regular object. For the normal shared case, discard
2001 space for relocs that have become local due to symbol visibility
2002 changes. */
2003 if (info->shared)
446f2863 2004 {
98ceb8ce 2005#if RELATIVE_DYNRELOCS
875c0872 2006 if (SYMBOL_CALLS_LOCAL (info, eh))
446f2863 2007 {
875c0872 2008 struct elf32_hppa_dyn_reloc_entry **hdh_pp;
30667bf3 2009
875c0872 2010 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
98ceb8ce 2011 {
875c0872
DA
2012 hdh_p->count -= hdh_p->relative_count;
2013 hdh_p->relative_count = 0;
2014 if (hdh_p->count == 0)
a63e02c7 2015 *hdh_pp = hdh_p->hdh_next;
98ceb8ce 2016 else
a63e02c7 2017 hdh_pp = &hdh_p->hdh_next;
98ceb8ce
AM
2018 }
2019 }
2020#endif
4fc8051d
AM
2021
2022 /* Also discard relocs on undefined weak syms with non-default
2023 visibility. */
875c0872
DA
2024 if (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT
2025 && eh->root.type == bfd_link_hash_undefweak)
2026 hh->dyn_relocs = NULL;
446f2863 2027 }
98ceb8ce 2028 else
30667bf3 2029 {
98ceb8ce
AM
2030 /* For the non-shared case, discard space for relocs against
2031 symbols which turn out to need copy relocs or are not
2032 dynamic. */
875c0872
DA
2033
2034 if (!eh->non_got_ref
4fc8051d 2035 && ((ELIMINATE_COPY_RELOCS
875c0872
DA
2036 && eh->def_dynamic
2037 && !eh->def_regular)
a63e02c7 2038 || (htab->etab.dynamic_sections_created
875c0872
DA
2039 && (eh->root.type == bfd_link_hash_undefweak
2040 || eh->root.type == bfd_link_hash_undefined))))
98ceb8ce
AM
2041 {
2042 /* Make sure this symbol is output as a dynamic symbol.
2043 Undefined weak syms won't yet be marked as dynamic. */
875c0872
DA
2044 if (eh->dynindx == -1
2045 && !eh->forced_local
2046 && eh->type != STT_PARISC_MILLI)
98ceb8ce 2047 {
875c0872 2048 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
b34976b6 2049 return FALSE;
98ceb8ce
AM
2050 }
2051
2052 /* If that succeeded, we know we'll be keeping all the
2053 relocs. */
875c0872 2054 if (eh->dynindx != -1)
98ceb8ce
AM
2055 goto keep;
2056 }
446f2863 2057
875c0872 2058 hh->dyn_relocs = NULL;
b34976b6 2059 return TRUE;
98ceb8ce 2060
ec338859 2061 keep: ;
30667bf3 2062 }
30667bf3 2063
98ceb8ce 2064 /* Finally, allocate space. */
a63e02c7 2065 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
30667bf3 2066 {
875c0872
DA
2067 asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
2068 sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
30667bf3 2069 }
30667bf3 2070
b34976b6 2071 return TRUE;
30667bf3 2072}
30667bf3 2073
d5c73c2f
AM
2074/* This function is called via elf_link_hash_traverse to force
2075 millicode symbols local so they do not end up as globals in the
2076 dynamic symbol table. We ought to be able to do this in
2077 adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2078 for all dynamic symbols. Arguably, this is a bug in
2079 elf_adjust_dynamic_symbol. */
2080
b34976b6 2081static bfd_boolean
875c0872 2082clobber_millicode_symbols (struct elf_link_hash_entry *eh,
c39a58e6 2083 struct bfd_link_info *info)
d5c73c2f 2084{
875c0872
DA
2085 if (eh->root.type == bfd_link_hash_warning)
2086 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
e92d460e 2087
875c0872
DA
2088 if (eh->type == STT_PARISC_MILLI
2089 && !eh->forced_local)
e0522e89 2090 {
875c0872 2091 elf32_hppa_hide_symbol (info, eh, TRUE);
e0522e89 2092 }
b34976b6 2093 return TRUE;
d5c73c2f
AM
2094}
2095
98ceb8ce
AM
2096/* Find any dynamic relocs that apply to read-only sections. */
2097
b34976b6 2098static bfd_boolean
875c0872 2099readonly_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
98ceb8ce 2100{
875c0872
DA
2101 struct elf32_hppa_link_hash_entry *hh;
2102 struct elf32_hppa_dyn_reloc_entry *hdh_p;
98ceb8ce 2103
875c0872
DA
2104 if (eh->root.type == bfd_link_hash_warning)
2105 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
e92d460e 2106
875c0872 2107 hh = hppa_elf_hash_entry (eh);
a63e02c7 2108 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
98ceb8ce 2109 {
875c0872 2110 asection *sec = hdh_p->sec->output_section;
98ceb8ce 2111
875c0872 2112 if (sec != NULL && (sec->flags & SEC_READONLY) != 0)
98ceb8ce 2113 {
c39a58e6 2114 struct bfd_link_info *info = inf;
98ceb8ce
AM
2115
2116 info->flags |= DF_TEXTREL;
2117
2118 /* Not an error, just cut short the traversal. */
b34976b6 2119 return FALSE;
98ceb8ce
AM
2120 }
2121 }
b34976b6 2122 return TRUE;
98ceb8ce
AM
2123}
2124
30667bf3
AM
2125/* Set the sizes of the dynamic sections. */
2126
b34976b6 2127static bfd_boolean
c39a58e6
AM
2128elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2129 struct bfd_link_info *info)
30667bf3 2130{
83c81bfe 2131 struct elf32_hppa_link_hash_table *htab;
30667bf3 2132 bfd *dynobj;
98ceb8ce 2133 bfd *ibfd;
875c0872 2134 asection *sec;
b34976b6 2135 bfd_boolean relocs;
30667bf3 2136
83c81bfe 2137 htab = hppa_link_hash_table (info);
a63e02c7 2138 dynobj = htab->etab.dynobj;
49e9d0d3
AM
2139 if (dynobj == NULL)
2140 abort ();
30667bf3 2141
a63e02c7 2142 if (htab->etab.dynamic_sections_created)
30667bf3
AM
2143 {
2144 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 2145 if (info->executable)
30667bf3 2146 {
875c0872
DA
2147 sec = bfd_get_section_by_name (dynobj, ".interp");
2148 if (sec == NULL)
49e9d0d3 2149 abort ();
875c0872
DA
2150 sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
2151 sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
30667bf3 2152 }
74d1c347 2153
d5c73c2f 2154 /* Force millicode symbols local. */
a63e02c7 2155 elf_link_hash_traverse (&htab->etab,
d5c73c2f
AM
2156 clobber_millicode_symbols,
2157 info);
68fb2e56 2158 }
d5c73c2f 2159
98ceb8ce
AM
2160 /* Set up .got and .plt offsets for local syms, and space for local
2161 dynamic relocs. */
2162 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
68fb2e56
AM
2163 {
2164 bfd_signed_vma *local_got;
2165 bfd_signed_vma *end_local_got;
2166 bfd_signed_vma *local_plt;
2167 bfd_signed_vma *end_local_plt;
2168 bfd_size_type locsymcount;
2169 Elf_Internal_Shdr *symtab_hdr;
2170 asection *srel;
74d1c347 2171
98ceb8ce 2172 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
68fb2e56 2173 continue;
4dc86686 2174
875c0872 2175 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
98ceb8ce 2176 {
875c0872 2177 struct elf32_hppa_dyn_reloc_entry *hdh_p;
98ceb8ce 2178
875c0872
DA
2179 for (hdh_p = ((struct elf32_hppa_dyn_reloc_entry *)
2180 elf_section_data (sec)->local_dynrel);
2181 hdh_p != NULL;
a63e02c7 2182 hdh_p = hdh_p->hdh_next)
98ceb8ce 2183 {
875c0872
DA
2184 if (!bfd_is_abs_section (hdh_p->sec)
2185 && bfd_is_abs_section (hdh_p->sec->output_section))
ec338859
AM
2186 {
2187 /* Input section has been discarded, either because
2188 it is a copy of a linkonce section or due to
2189 linker script /DISCARD/, so we'll be discarding
2190 the relocs too. */
2191 }
875c0872 2192 else if (hdh_p->count != 0)
ec338859 2193 {
875c0872
DA
2194 srel = elf_section_data (hdh_p->sec)->sreloc;
2195 srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
2196 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
248866a8 2197 info->flags |= DF_TEXTREL;
ec338859 2198 }
98ceb8ce
AM
2199 }
2200 }
2201
2202 local_got = elf_local_got_refcounts (ibfd);
68fb2e56
AM
2203 if (!local_got)
2204 continue;
74d1c347 2205
98ceb8ce 2206 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
68fb2e56
AM
2207 locsymcount = symtab_hdr->sh_info;
2208 end_local_got = local_got + locsymcount;
875c0872 2209 sec = htab->sgot;
83c81bfe 2210 srel = htab->srelgot;
68fb2e56
AM
2211 for (; local_got < end_local_got; ++local_got)
2212 {
2213 if (*local_got > 0)
4dc86686 2214 {
875c0872
DA
2215 *local_got = sec->size;
2216 sec->size += GOT_ENTRY_SIZE;
2217 if (info->shared)
eea6121a 2218 srel->size += sizeof (Elf32_External_Rela);
4dc86686 2219 }
68fb2e56
AM
2220 else
2221 *local_got = (bfd_vma) -1;
2222 }
74d1c347 2223
68fb2e56
AM
2224 local_plt = end_local_got;
2225 end_local_plt = local_plt + locsymcount;
a63e02c7 2226 if (! htab->etab.dynamic_sections_created)
68fb2e56
AM
2227 {
2228 /* Won't be used, but be safe. */
2229 for (; local_plt < end_local_plt; ++local_plt)
2230 *local_plt = (bfd_vma) -1;
2231 }
2232 else
2233 {
875c0872 2234 sec = htab->splt;
83c81bfe 2235 srel = htab->srelplt;
74d1c347
AM
2236 for (; local_plt < end_local_plt; ++local_plt)
2237 {
2238 if (*local_plt > 0)
2239 {
875c0872
DA
2240 *local_plt = sec->size;
2241 sec->size += PLT_ENTRY_SIZE;
74d1c347 2242 if (info->shared)
eea6121a 2243 srel->size += sizeof (Elf32_External_Rela);
74d1c347
AM
2244 }
2245 else
2246 *local_plt = (bfd_vma) -1;
2247 }
2248 }
30667bf3 2249 }
30667bf3 2250
e5ee5df1
AM
2251 /* Do all the .plt entries without relocs first. The dynamic linker
2252 uses the last .plt reloc to find the end of the .plt (and hence
2253 the start of the .got) for lazy linking. */
a63e02c7 2254 elf_link_hash_traverse (&htab->etab, allocate_plt_static, info);
a8d02d66 2255
98ceb8ce
AM
2256 /* Allocate global sym .plt and .got entries, and space for global
2257 sym dynamic relocs. */
a63e02c7 2258 elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
30667bf3
AM
2259
2260 /* The check_relocs and adjust_dynamic_symbol entry points have
2261 determined the sizes of the various dynamic sections. Allocate
2262 memory for them. */
b34976b6 2263 relocs = FALSE;
875c0872 2264 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
30667bf3 2265 {
875c0872 2266 if ((sec->flags & SEC_LINKER_CREATED) == 0)
30667bf3
AM
2267 continue;
2268
875c0872 2269 if (sec == htab->splt)
68fb2e56 2270 {
83c81bfe 2271 if (htab->need_plt_stub)
68fb2e56
AM
2272 {
2273 /* Make space for the plt stub at the end of the .plt
2274 section. We want this stub right at the end, up
2275 against the .got section. */
83c81bfe 2276 int gotalign = bfd_section_alignment (dynobj, htab->sgot);
875c0872 2277 int pltalign = bfd_section_alignment (dynobj, sec);
68fb2e56 2278 bfd_size_type mask;
30667bf3 2279
68fb2e56 2280 if (gotalign > pltalign)
875c0872 2281 bfd_set_section_alignment (dynobj, sec, gotalign);
68fb2e56 2282 mask = ((bfd_size_type) 1 << gotalign) - 1;
875c0872 2283 sec->size = (sec->size + sizeof (plt_stub) + mask) & ~mask;
68fb2e56
AM
2284 }
2285 }
c456f082
AM
2286 else if (sec == htab->sgot
2287 || sec == htab->sdynbss)
68fb2e56 2288 ;
875c0872 2289 else if (strncmp (bfd_get_section_name (dynobj, sec), ".rela", 5) == 0)
30667bf3 2290 {
875c0872 2291 if (sec->size != 0)
30667bf3 2292 {
4e12ff7f
AM
2293 /* Remember whether there are any reloc sections other
2294 than .rela.plt. */
875c0872 2295 if (sec != htab->srelplt)
b34976b6 2296 relocs = TRUE;
47d89dba 2297
30667bf3
AM
2298 /* We use the reloc_count field as a counter if we need
2299 to copy relocs into the output file. */
875c0872 2300 sec->reloc_count = 0;
30667bf3
AM
2301 }
2302 }
30667bf3
AM
2303 else
2304 {
2305 /* It's not one of our sections, so don't allocate space. */
2306 continue;
2307 }
2308
875c0872 2309 if (sec->size == 0)
30667bf3
AM
2310 {
2311 /* If we don't need this section, strip it from the
2312 output file. This is mostly to handle .rela.bss and
2313 .rela.plt. We must create both sections in
2314 create_dynamic_sections, because they must be created
2315 before the linker maps input sections to output
2316 sections. The linker does that before
2317 adjust_dynamic_symbol is called, and it is that
2318 function which decides whether anything needs to go
2319 into these sections. */
875c0872 2320 sec->flags |= SEC_EXCLUDE;
30667bf3
AM
2321 continue;
2322 }
2323
c456f082
AM
2324 if ((sec->flags & SEC_HAS_CONTENTS) == 0)
2325 continue;
2326
30667bf3
AM
2327 /* Allocate memory for the section contents. Zero it, because
2328 we may not fill in all the reloc sections. */
875c0872 2329 sec->contents = bfd_zalloc (dynobj, sec->size);
c456f082 2330 if (sec->contents == NULL)
b34976b6 2331 return FALSE;
30667bf3
AM
2332 }
2333
a63e02c7 2334 if (htab->etab.dynamic_sections_created)
30667bf3
AM
2335 {
2336 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2337 actually has nothing to do with the PLT, it is how we
2338 communicate the LTP value of a load module to the dynamic
2339 linker. */
dc810e39 2340#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2341 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39
AM
2342
2343 if (!add_dynamic_entry (DT_PLTGOT, 0))
b34976b6 2344 return FALSE;
30667bf3
AM
2345
2346 /* Add some entries to the .dynamic section. We fill in the
2347 values later, in elf32_hppa_finish_dynamic_sections, but we
2348 must add the entries now so that we get the correct size for
2349 the .dynamic section. The DT_DEBUG entry is filled in by the
2350 dynamic linker and used by the debugger. */
dc810e39 2351 if (!info->shared)
30667bf3 2352 {
dc810e39 2353 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 2354 return FALSE;
30667bf3
AM
2355 }
2356
eea6121a 2357 if (htab->srelplt->size != 0)
30667bf3 2358 {
dc810e39
AM
2359 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
2360 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2361 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 2362 return FALSE;
30667bf3
AM
2363 }
2364
2365 if (relocs)
2366 {
dc810e39
AM
2367 if (!add_dynamic_entry (DT_RELA, 0)
2368 || !add_dynamic_entry (DT_RELASZ, 0)
2369 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
b34976b6 2370 return FALSE;
30667bf3 2371
98ceb8ce
AM
2372 /* If any dynamic relocs apply to a read-only section,
2373 then we need a DT_TEXTREL entry. */
248866a8 2374 if ((info->flags & DF_TEXTREL) == 0)
a63e02c7 2375 elf_link_hash_traverse (&htab->etab, readonly_dynrelocs, info);
98ceb8ce
AM
2376
2377 if ((info->flags & DF_TEXTREL) != 0)
2378 {
2379 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 2380 return FALSE;
98ceb8ce 2381 }
30667bf3
AM
2382 }
2383 }
dc810e39 2384#undef add_dynamic_entry
30667bf3 2385
b34976b6 2386 return TRUE;
30667bf3
AM
2387}
2388
30667bf3
AM
2389/* External entry points for sizing and building linker stubs. */
2390
b4655ea9
AM
2391/* Set up various things so that we can make a list of input sections
2392 for each output section included in the link. Returns -1 on error,
cedb70c5 2393 0 when no stubs will be needed, and 1 on success. */
30667bf3 2394
b4655ea9 2395int
c39a58e6 2396elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
30667bf3
AM
2397{
2398 bfd *input_bfd;
b4655ea9
AM
2399 unsigned int bfd_count;
2400 int top_id, top_index;
30667bf3 2401 asection *section;
25f72752 2402 asection **input_list, **list;
dc810e39 2403 bfd_size_type amt;
b4655ea9 2404 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
30667bf3 2405
1badb539
AM
2406 /* Count the number of input BFDs and find the top input section id. */
2407 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
30667bf3
AM
2408 input_bfd != NULL;
2409 input_bfd = input_bfd->link_next)
2410 {
2411 bfd_count += 1;
25f72752
AM
2412 for (section = input_bfd->sections;
2413 section != NULL;
2414 section = section->next)
2415 {
2416 if (top_id < section->id)
2417 top_id = section->id;
2418 }
30667bf3 2419 }
b4655ea9 2420 htab->bfd_count = bfd_count;
30667bf3 2421
dc810e39 2422 amt = sizeof (struct map_stub) * (top_id + 1);
c39a58e6 2423 htab->stub_group = bfd_zmalloc (amt);
83c81bfe 2424 if (htab->stub_group == NULL)
b4655ea9 2425 return -1;
1badb539 2426
b4655ea9 2427 /* We can't use output_bfd->section_count here to find the top output
1badb539 2428 section index as some sections may have been removed, and
8423293d 2429 strip_excluded_output_sections doesn't renumber the indices. */
1badb539
AM
2430 for (section = output_bfd->sections, top_index = 0;
2431 section != NULL;
2432 section = section->next)
2433 {
2434 if (top_index < section->index)
2435 top_index = section->index;
2436 }
2437
b4655ea9 2438 htab->top_index = top_index;
dc810e39 2439 amt = sizeof (asection *) * (top_index + 1);
c39a58e6 2440 input_list = bfd_malloc (amt);
b4655ea9 2441 htab->input_list = input_list;
25f72752 2442 if (input_list == NULL)
b4655ea9 2443 return -1;
25f72752 2444
1badb539
AM
2445 /* For sections we aren't interested in, mark their entries with a
2446 value we can check later. */
2447 list = input_list + top_index;
2448 do
2449 *list = bfd_abs_section_ptr;
2450 while (list-- != input_list);
2451
2452 for (section = output_bfd->sections;
2453 section != NULL;
2454 section = section->next)
2455 {
47d89dba 2456 if ((section->flags & SEC_CODE) != 0)
1badb539
AM
2457 input_list[section->index] = NULL;
2458 }
2459
b4655ea9
AM
2460 return 1;
2461}
2462
2463/* The linker repeatedly calls this function for each input section,
2464 in the order that input sections are linked into output sections.
2465 Build lists of input sections to determine groupings between which
2466 we may insert linker stubs. */
2467
2468void
c39a58e6 2469elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
b4655ea9
AM
2470{
2471 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2472
2473 if (isec->output_section->index <= htab->top_index)
25f72752 2474 {
b4655ea9
AM
2475 asection **list = htab->input_list + isec->output_section->index;
2476 if (*list != bfd_abs_section_ptr)
25f72752 2477 {
b4655ea9 2478 /* Steal the link_sec pointer for our list. */
83c81bfe 2479#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
b4655ea9
AM
2480 /* This happens to make the list in reverse order,
2481 which is what we want. */
2482 PREV_SEC (isec) = *list;
2483 *list = isec;
25f72752
AM
2484 }
2485 }
b4655ea9 2486}
25f72752 2487
b4655ea9
AM
2488/* See whether we can group stub sections together. Grouping stub
2489 sections may result in fewer stubs. More importantly, we need to
2490 put all .init* and .fini* stubs at the beginning of the .init or
2491 .fini output sections respectively, because glibc splits the
2492 _init and _fini functions into multiple parts. Putting a stub in
2493 the middle of a function is not a good idea. */
2494
2495static void
c39a58e6
AM
2496group_sections (struct elf32_hppa_link_hash_table *htab,
2497 bfd_size_type stub_group_size,
2498 bfd_boolean stubs_always_before_branch)
b4655ea9
AM
2499{
2500 asection **list = htab->input_list + htab->top_index;
1badb539 2501 do
25f72752
AM
2502 {
2503 asection *tail = *list;
1badb539
AM
2504 if (tail == bfd_abs_section_ptr)
2505 continue;
25f72752
AM
2506 while (tail != NULL)
2507 {
2508 asection *curr;
2509 asection *prev;
2510 bfd_size_type total;
00b28bb0 2511 bfd_boolean big_sec;
25f72752
AM
2512
2513 curr = tail;
eea6121a 2514 total = tail->size;
00b28bb0
AM
2515 big_sec = total >= stub_group_size;
2516
25f72752
AM
2517 while ((prev = PREV_SEC (curr)) != NULL
2518 && ((total += curr->output_offset - prev->output_offset)
47d89dba 2519 < stub_group_size))
25f72752
AM
2520 curr = prev;
2521
2522 /* OK, the size from the start of CURR to the end is less
a248e267 2523 than 240000 bytes and thus can be handled by one stub
25f72752 2524 section. (or the tail section is itself larger than
a248e267 2525 240000 bytes, in which case we may be toast.)
25f72752
AM
2526 We should really be keeping track of the total size of
2527 stubs added here, as stubs contribute to the final output
2528 section size. That's a little tricky, and this way will
a248e267
AM
2529 only break if stubs added total more than 22144 bytes, or
2530 2768 long branch stubs. It seems unlikely for more than
2531 2768 different functions to be called, especially from
2532 code only 240000 bytes long. This limit used to be
2533 250000, but c++ code tends to generate lots of little
2534 functions, and sometimes violated the assumption. */
25f72752
AM
2535 do
2536 {
2537 prev = PREV_SEC (tail);
2538 /* Set up this stub group. */
83c81bfe 2539 htab->stub_group[tail->id].link_sec = curr;
25f72752
AM
2540 }
2541 while (tail != curr && (tail = prev) != NULL);
2542
a248e267 2543 /* But wait, there's more! Input sections up to 240000
00b28bb0
AM
2544 bytes before the stub section can be handled by it too.
2545 Don't do this if we have a really large section after the
2546 stubs, as adding more stubs increases the chance that
2547 branches may not reach into the stub section. */
2548 if (!stubs_always_before_branch && !big_sec)
25f72752 2549 {
47d89dba
AM
2550 total = 0;
2551 while (prev != NULL
2552 && ((total += tail->output_offset - prev->output_offset)
2553 < stub_group_size))
2554 {
2555 tail = prev;
2556 prev = PREV_SEC (tail);
83c81bfe 2557 htab->stub_group[tail->id].link_sec = curr;
47d89dba 2558 }
25f72752
AM
2559 }
2560 tail = prev;
2561 }
2562 }
b4655ea9
AM
2563 while (list-- != htab->input_list);
2564 free (htab->input_list);
1badb539 2565#undef PREV_SEC
b4655ea9
AM
2566}
2567
2568/* Read in all local syms for all input bfds, and create hash entries
2569 for export stubs if we are building a multi-subspace shared lib.
2570 Returns -1 on error, 1 if export stubs created, 0 otherwise. */
2571
2572static int
c39a58e6 2573get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
b4655ea9
AM
2574{
2575 unsigned int bfd_indx;
2576 Elf_Internal_Sym *local_syms, **all_local_syms;
2577 int stub_changed = 0;
2578 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
30667bf3
AM
2579
2580 /* We want to read in symbol extension records only once. To do this
2581 we need to read in the local symbols in parallel and save them for
2582 later use; so hold pointers to the local symbols in an array. */
b4655ea9 2583 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
c39a58e6 2584 all_local_syms = bfd_zmalloc (amt);
b4655ea9 2585 htab->all_local_syms = all_local_syms;
30667bf3 2586 if (all_local_syms == NULL)
b4655ea9 2587 return -1;
30667bf3
AM
2588
2589 /* Walk over all the input BFDs, swapping in local symbols.
2590 If we are creating a shared library, create hash entries for the
2591 export stubs. */
b4655ea9 2592 for (bfd_indx = 0;
30667bf3 2593 input_bfd != NULL;
25f72752 2594 input_bfd = input_bfd->link_next, bfd_indx++)
30667bf3
AM
2595 {
2596 Elf_Internal_Shdr *symtab_hdr;
edd21aca 2597
252b5132
RH
2598 /* We'll need the symbol table in a second. */
2599 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2600 if (symtab_hdr->sh_info == 0)
2601 continue;
2602
6cdc0ccc
AM
2603 /* We need an array of the local symbols attached to the input bfd. */
2604 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
edd21aca 2605 if (local_syms == NULL)
edd21aca 2606 {
6cdc0ccc
AM
2607 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2608 symtab_hdr->sh_info, 0,
2609 NULL, NULL, NULL);
2610 /* Cache them for elf_link_input_bfd. */
2611 symtab_hdr->contents = (unsigned char *) local_syms;
edd21aca 2612 }
6cdc0ccc
AM
2613 if (local_syms == NULL)
2614 return -1;
edd21aca 2615
6cdc0ccc 2616 all_local_syms[bfd_indx] = local_syms;
edd21aca 2617
83c81bfe 2618 if (info->shared && htab->multi_subspace)
30667bf3 2619 {
875c0872
DA
2620 struct elf_link_hash_entry **eh_syms;
2621 struct elf_link_hash_entry **eh_symend;
30667bf3
AM
2622 unsigned int symcount;
2623
2624 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2625 - symtab_hdr->sh_info);
875c0872
DA
2626 eh_syms = (struct elf_link_hash_entry **) elf_sym_hashes (input_bfd);
2627 eh_symend = (struct elf_link_hash_entry **) (eh_syms + symcount);
30667bf3
AM
2628
2629 /* Look through the global syms for functions; We need to
2630 build export stubs for all globally visible functions. */
875c0872 2631 for (; eh_syms < eh_symend; eh_syms++)
30667bf3 2632 {
875c0872 2633 struct elf32_hppa_link_hash_entry *hh;
30667bf3 2634
875c0872 2635 hh = hppa_elf_hash_entry (*eh_syms);
30667bf3 2636
a63e02c7
DA
2637 while (hh->eh.root.type == bfd_link_hash_indirect
2638 || hh->eh.root.type == bfd_link_hash_warning)
2639 hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
30667bf3
AM
2640
2641 /* At this point in the link, undefined syms have been
2642 resolved, so we need to check that the symbol was
2643 defined in this BFD. */
a63e02c7
DA
2644 if ((hh->eh.root.type == bfd_link_hash_defined
2645 || hh->eh.root.type == bfd_link_hash_defweak)
2646 && hh->eh.type == STT_FUNC
2647 && hh->eh.root.u.def.section->output_section != NULL
2648 && (hh->eh.root.u.def.section->output_section->owner
25f72752 2649 == output_bfd)
a63e02c7
DA
2650 && hh->eh.root.u.def.section->owner == input_bfd
2651 && hh->eh.def_regular
2652 && !hh->eh.forced_local
2653 && ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT)
30667bf3
AM
2654 {
2655 asection *sec;
2656 const char *stub_name;
875c0872 2657 struct elf32_hppa_stub_hash_entry *hsh;
30667bf3 2658
a63e02c7
DA
2659 sec = hh->eh.root.u.def.section;
2660 stub_name = hh->eh.root.root.string;
2661 hsh = hppa_stub_hash_lookup (&htab->bstab,
30667bf3 2662 stub_name,
b34976b6 2663 FALSE, FALSE);
875c0872 2664 if (hsh == NULL)
30667bf3 2665 {
875c0872
DA
2666 hsh = hppa_add_stub (stub_name, sec, htab);
2667 if (!hsh)
b4655ea9 2668 return -1;
30667bf3 2669
a63e02c7
DA
2670 hsh->target_value = hh->eh.root.u.def.value;
2671 hsh->target_section = hh->eh.root.u.def.section;
875c0872 2672 hsh->stub_type = hppa_stub_export;
a63e02c7 2673 hsh->hh = hh;
30667bf3
AM
2674 stub_changed = 1;
2675 }
2676 else
2677 {
d003868e
AM
2678 (*_bfd_error_handler) (_("%B: duplicate export stub %s"),
2679 input_bfd,
8f615d07 2680 stub_name);
30667bf3
AM
2681 }
2682 }
2683 }
30667bf3
AM
2684 }
2685 }
edd21aca 2686
b4655ea9
AM
2687 return stub_changed;
2688}
2689
2690/* Determine and set the size of the stub section for a final link.
2691
2692 The basic idea here is to examine all the relocations looking for
2693 PC-relative calls to a target that is unreachable with a "bl"
2694 instruction. */
2695
b34976b6 2696bfd_boolean
c39a58e6
AM
2697elf32_hppa_size_stubs
2698 (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
2699 bfd_boolean multi_subspace, bfd_signed_vma group_size,
2700 asection * (*add_stub_section) (const char *, asection *),
2701 void (*layout_sections_again) (void))
b4655ea9
AM
2702{
2703 bfd_size_type stub_group_size;
b34976b6
AM
2704 bfd_boolean stubs_always_before_branch;
2705 bfd_boolean stub_changed;
b4655ea9
AM
2706 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2707
2708 /* Stash our params away. */
2709 htab->stub_bfd = stub_bfd;
2710 htab->multi_subspace = multi_subspace;
2711 htab->add_stub_section = add_stub_section;
2712 htab->layout_sections_again = layout_sections_again;
2713 stubs_always_before_branch = group_size < 0;
2714 if (group_size < 0)
2715 stub_group_size = -group_size;
2716 else
2717 stub_group_size = group_size;
2718 if (stub_group_size == 1)
2719 {
2720 /* Default values. */
acc990f2
AM
2721 if (stubs_always_before_branch)
2722 {
2723 stub_group_size = 7680000;
2724 if (htab->has_17bit_branch || htab->multi_subspace)
2725 stub_group_size = 240000;
2726 if (htab->has_12bit_branch)
2727 stub_group_size = 7500;
2728 }
2729 else
2730 {
2731 stub_group_size = 6971392;
2732 if (htab->has_17bit_branch || htab->multi_subspace)
2733 stub_group_size = 217856;
2734 if (htab->has_12bit_branch)
2735 stub_group_size = 6808;
2736 }
b4655ea9
AM
2737 }
2738
2739 group_sections (htab, stub_group_size, stubs_always_before_branch);
2740
2741 switch (get_local_syms (output_bfd, info->input_bfds, info))
2742 {
2743 default:
2744 if (htab->all_local_syms)
2745 goto error_ret_free_local;
b34976b6 2746 return FALSE;
b4655ea9
AM
2747
2748 case 0:
b34976b6 2749 stub_changed = FALSE;
b4655ea9
AM
2750 break;
2751
2752 case 1:
b34976b6 2753 stub_changed = TRUE;
b4655ea9
AM
2754 break;
2755 }
2756
edd21aca
AM
2757 while (1)
2758 {
b4655ea9
AM
2759 bfd *input_bfd;
2760 unsigned int bfd_indx;
30667bf3
AM
2761 asection *stub_sec;
2762
25f72752 2763 for (input_bfd = info->input_bfds, bfd_indx = 0;
30667bf3 2764 input_bfd != NULL;
25f72752 2765 input_bfd = input_bfd->link_next, bfd_indx++)
30667bf3
AM
2766 {
2767 Elf_Internal_Shdr *symtab_hdr;
b4655ea9
AM
2768 asection *section;
2769 Elf_Internal_Sym *local_syms;
30667bf3
AM
2770
2771 /* We'll need the symbol table in a second. */
2772 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2773 if (symtab_hdr->sh_info == 0)
2774 continue;
2775
b4655ea9 2776 local_syms = htab->all_local_syms[bfd_indx];
30667bf3
AM
2777
2778 /* Walk over each section attached to the input bfd. */
2779 for (section = input_bfd->sections;
2780 section != NULL;
25f72752 2781 section = section->next)
30667bf3 2782 {
30667bf3
AM
2783 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2784
2785 /* If there aren't any relocs, then there's nothing more
2786 to do. */
2787 if ((section->flags & SEC_RELOC) == 0
2788 || section->reloc_count == 0)
2789 continue;
2790
25f72752
AM
2791 /* If this section is a link-once section that will be
2792 discarded, then don't create any stubs. */
2793 if (section->output_section == NULL
2794 || section->output_section->owner != output_bfd)
2795 continue;
2796
1e2f5b6e
AM
2797 /* Get the relocs. */
2798 internal_relocs
c39a58e6 2799 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 2800 info->keep_memory);
30667bf3 2801 if (internal_relocs == NULL)
1e2f5b6e 2802 goto error_ret_free_local;
30667bf3
AM
2803
2804 /* Now examine each relocation. */
2805 irela = internal_relocs;
2806 irelaend = irela + section->reloc_count;
2807 for (; irela < irelaend; irela++)
2808 {
2809 unsigned int r_type, r_indx;
2810 enum elf32_hppa_stub_type stub_type;
875c0872 2811 struct elf32_hppa_stub_hash_entry *hsh;
30667bf3
AM
2812 asection *sym_sec;
2813 bfd_vma sym_value;
2814 bfd_vma destination;
875c0872 2815 struct elf32_hppa_link_hash_entry *hh;
30667bf3 2816 char *stub_name;
25f72752 2817 const asection *id_sec;
30667bf3
AM
2818
2819 r_type = ELF32_R_TYPE (irela->r_info);
2820 r_indx = ELF32_R_SYM (irela->r_info);
2821
2822 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2823 {
2824 bfd_set_error (bfd_error_bad_value);
1e2f5b6e
AM
2825 error_ret_free_internal:
2826 if (elf_section_data (section)->relocs == NULL)
2827 free (internal_relocs);
2828 goto error_ret_free_local;
30667bf3
AM
2829 }
2830
2831 /* Only look for stubs on call instructions. */
2832 if (r_type != (unsigned int) R_PARISC_PCREL12F
2833 && r_type != (unsigned int) R_PARISC_PCREL17F
2834 && r_type != (unsigned int) R_PARISC_PCREL22F)
2835 continue;
2836
2837 /* Now determine the call target, its name, value,
2838 section. */
2839 sym_sec = NULL;
2840 sym_value = 0;
2841 destination = 0;
875c0872 2842 hh = NULL;
30667bf3
AM
2843 if (r_indx < symtab_hdr->sh_info)
2844 {
2845 /* It's a local symbol. */
2846 Elf_Internal_Sym *sym;
2847 Elf_Internal_Shdr *hdr;
2848
2849 sym = local_syms + r_indx;
2850 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2851 sym_sec = hdr->bfd_section;
2852 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2853 sym_value = sym->st_value;
2854 destination = (sym_value + irela->r_addend
2855 + sym_sec->output_offset
2856 + sym_sec->output_section->vma);
2857 }
2858 else
2859 {
2860 /* It's an external symbol. */
2861 int e_indx;
2862
2863 e_indx = r_indx - symtab_hdr->sh_info;
875c0872 2864 hh = hppa_elf_hash_entry (elf_sym_hashes (input_bfd)[e_indx]);
30667bf3 2865
a63e02c7
DA
2866 while (hh->eh.root.type == bfd_link_hash_indirect
2867 || hh->eh.root.type == bfd_link_hash_warning)
2868 hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
30667bf3 2869
a63e02c7
DA
2870 if (hh->eh.root.type == bfd_link_hash_defined
2871 || hh->eh.root.type == bfd_link_hash_defweak)
30667bf3 2872 {
a63e02c7
DA
2873 sym_sec = hh->eh.root.u.def.section;
2874 sym_value = hh->eh.root.u.def.value;
30667bf3
AM
2875 if (sym_sec->output_section != NULL)
2876 destination = (sym_value + irela->r_addend
2877 + sym_sec->output_offset
2878 + sym_sec->output_section->vma);
2879 }
a63e02c7 2880 else if (hh->eh.root.type == bfd_link_hash_undefweak)
c432ba1a
AM
2881 {
2882 if (! info->shared)
2883 continue;
2884 }
a63e02c7 2885 else if (hh->eh.root.type == bfd_link_hash_undefined)
c432ba1a 2886 {
59c2e50f 2887 if (! (info->unresolved_syms_in_objects == RM_IGNORE
a63e02c7 2888 && (ELF_ST_VISIBILITY (hh->eh.other)
c432ba1a 2889 == STV_DEFAULT)
a63e02c7 2890 && hh->eh.type != STT_PARISC_MILLI))
c432ba1a
AM
2891 continue;
2892 }
30667bf3
AM
2893 else
2894 {
2895 bfd_set_error (bfd_error_bad_value);
2896 goto error_ret_free_internal;
2897 }
2898 }
2899
2900 /* Determine what (if any) linker stub is needed. */
875c0872 2901 stub_type = hppa_type_of_stub (section, irela, hh,
a252afa4 2902 destination, info);
30667bf3
AM
2903 if (stub_type == hppa_stub_none)
2904 continue;
2905
25f72752 2906 /* Support for grouping stub sections. */
83c81bfe 2907 id_sec = htab->stub_group[section->id].link_sec;
25f72752 2908
30667bf3 2909 /* Get the name of this stub. */
875c0872 2910 stub_name = hppa_stub_name (id_sec, sym_sec, hh, irela);
30667bf3
AM
2911 if (!stub_name)
2912 goto error_ret_free_internal;
2913
a63e02c7 2914 hsh = hppa_stub_hash_lookup (&htab->bstab,
30667bf3 2915 stub_name,
b34976b6 2916 FALSE, FALSE);
875c0872 2917 if (hsh != NULL)
30667bf3
AM
2918 {
2919 /* The proper stub has already been created. */
2920 free (stub_name);
2921 continue;
2922 }
2923
875c0872
DA
2924 hsh = hppa_add_stub (stub_name, section, htab);
2925 if (hsh == NULL)
30667bf3
AM
2926 {
2927 free (stub_name);
1e2f5b6e 2928 goto error_ret_free_internal;
30667bf3
AM
2929 }
2930
875c0872
DA
2931 hsh->target_value = sym_value;
2932 hsh->target_section = sym_sec;
2933 hsh->stub_type = stub_type;
30667bf3
AM
2934 if (info->shared)
2935 {
2936 if (stub_type == hppa_stub_import)
875c0872 2937 hsh->stub_type = hppa_stub_import_shared;
98ceb8ce 2938 else if (stub_type == hppa_stub_long_branch)
875c0872 2939 hsh->stub_type = hppa_stub_long_branch_shared;
30667bf3 2940 }
a63e02c7 2941 hsh->hh = hh;
b34976b6 2942 stub_changed = TRUE;
30667bf3
AM
2943 }
2944
2945 /* We're done with the internal relocs, free them. */
1e2f5b6e
AM
2946 if (elf_section_data (section)->relocs == NULL)
2947 free (internal_relocs);
30667bf3
AM
2948 }
2949 }
2950
2951 if (!stub_changed)
2952 break;
2953
2954 /* OK, we've added some stubs. Find out the new size of the
2955 stub sections. */
83c81bfe 2956 for (stub_sec = htab->stub_bfd->sections;
30667bf3
AM
2957 stub_sec != NULL;
2958 stub_sec = stub_sec->next)
eea6121a 2959 stub_sec->size = 0;
74d1c347 2960
a63e02c7 2961 bfd_hash_traverse (&htab->bstab, hppa_size_one_stub, htab);
74d1c347 2962
30667bf3 2963 /* Ask the linker to do its stuff. */
83c81bfe 2964 (*htab->layout_sections_again) ();
b34976b6 2965 stub_changed = FALSE;
30667bf3
AM
2966 }
2967
6cdc0ccc 2968 free (htab->all_local_syms);
b34976b6 2969 return TRUE;
30667bf3
AM
2970
2971 error_ret_free_local:
b4655ea9 2972 free (htab->all_local_syms);
b34976b6 2973 return FALSE;
30667bf3
AM
2974}
2975
30667bf3
AM
2976/* For a final link, this function is called after we have sized the
2977 stubs to provide a value for __gp. */
2978
b34976b6 2979bfd_boolean
c39a58e6 2980elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
30667bf3 2981{
b4655ea9
AM
2982 struct bfd_link_hash_entry *h;
2983 asection *sec = NULL;
2984 bfd_vma gp_val = 0;
83c81bfe 2985 struct elf32_hppa_link_hash_table *htab;
30667bf3 2986
83c81bfe 2987 htab = hppa_link_hash_table (info);
a63e02c7 2988 h = bfd_link_hash_lookup (&htab->etab.root, "$global$", FALSE, FALSE, FALSE);
30667bf3 2989
df8634e3 2990 if (h != NULL
b4655ea9
AM
2991 && (h->type == bfd_link_hash_defined
2992 || h->type == bfd_link_hash_defweak))
30667bf3 2993 {
b4655ea9
AM
2994 gp_val = h->u.def.value;
2995 sec = h->u.def.section;
30667bf3
AM
2996 }
2997 else
2998 {
0eddce27
AM
2999 asection *splt = bfd_get_section_by_name (abfd, ".plt");
3000 asection *sgot = bfd_get_section_by_name (abfd, ".got");
b4655ea9 3001
74d1c347
AM
3002 /* Choose to point our LTP at, in this order, one of .plt, .got,
3003 or .data, if these sections exist. In the case of choosing
3004 .plt try to make the LTP ideal for addressing anywhere in the
3005 .plt or .got with a 14 bit signed offset. Typically, the end
3006 of the .plt is the start of the .got, so choose .plt + 0x2000
3007 if either the .plt or .got is larger than 0x2000. If both
3008 the .plt and .got are smaller than 0x2000, choose the end of
3009 the .plt section. */
225247f0
JT
3010 sec = strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0
3011 ? NULL : splt;
74d1c347 3012 if (sec != NULL)
30667bf3 3013 {
eea6121a
AM
3014 gp_val = sec->size;
3015 if (gp_val > 0x2000 || (sgot && sgot->size > 0x2000))
74d1c347
AM
3016 {
3017 gp_val = 0x2000;
3018 }
3019 }
3020 else
3021 {
b4655ea9 3022 sec = sgot;
74d1c347
AM
3023 if (sec != NULL)
3024 {
225247f0
JT
3025 if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") != 0)
3026 {
3027 /* We know we don't have a .plt. If .got is large,
3028 offset our LTP. */
3029 if (sec->size > 0x2000)
3030 gp_val = 0x2000;
3031 }
74d1c347
AM
3032 }
3033 else
3034 {
3035 /* No .plt or .got. Who cares what the LTP is? */
3036 sec = bfd_get_section_by_name (abfd, ".data");
3037 }
30667bf3 3038 }
df8634e3
AM
3039
3040 if (h != NULL)
3041 {
b4655ea9
AM
3042 h->type = bfd_link_hash_defined;
3043 h->u.def.value = gp_val;
df8634e3 3044 if (sec != NULL)
b4655ea9 3045 h->u.def.section = sec;
df8634e3 3046 else
b4655ea9 3047 h->u.def.section = bfd_abs_section_ptr;
df8634e3 3048 }
30667bf3
AM
3049 }
3050
b32b5d6e 3051 if (sec != NULL && sec->output_section != NULL)
74d1c347
AM
3052 gp_val += sec->output_section->vma + sec->output_offset;
3053
3054 elf_gp (abfd) = gp_val;
b34976b6 3055 return TRUE;
30667bf3
AM
3056}
3057
30667bf3
AM
3058/* Build all the stubs associated with the current output file. The
3059 stubs are kept in a hash table attached to the main linker hash
3060 table. We also set up the .plt entries for statically linked PIC
3061 functions here. This function is called via hppaelf_finish in the
3062 linker. */
3063
b34976b6 3064bfd_boolean
c39a58e6 3065elf32_hppa_build_stubs (struct bfd_link_info *info)
30667bf3
AM
3066{
3067 asection *stub_sec;
3068 struct bfd_hash_table *table;
83c81bfe 3069 struct elf32_hppa_link_hash_table *htab;
30667bf3 3070
83c81bfe 3071 htab = hppa_link_hash_table (info);
30667bf3 3072
83c81bfe 3073 for (stub_sec = htab->stub_bfd->sections;
30667bf3
AM
3074 stub_sec != NULL;
3075 stub_sec = stub_sec->next)
3076 {
dc810e39 3077 bfd_size_type size;
30667bf3
AM
3078
3079 /* Allocate memory to hold the linker stubs. */
eea6121a 3080 size = stub_sec->size;
c39a58e6 3081 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
30667bf3 3082 if (stub_sec->contents == NULL && size != 0)
b34976b6 3083 return FALSE;
eea6121a 3084 stub_sec->size = 0;
30667bf3
AM
3085 }
3086
3087 /* Build the stubs as directed by the stub hash table. */
a63e02c7 3088 table = &htab->bstab;
30667bf3
AM
3089 bfd_hash_traverse (table, hppa_build_one_stub, info);
3090
b34976b6 3091 return TRUE;
30667bf3
AM
3092}
3093
c46b7515
AM
3094/* Perform a final link. */
3095
b34976b6 3096static bfd_boolean
c39a58e6 3097elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
c46b7515 3098{
4dc86686 3099 /* Invoke the regular ELF linker to do all the work. */
c152c796 3100 if (!bfd_elf_final_link (abfd, info))
b34976b6 3101 return FALSE;
c46b7515
AM
3102
3103 /* If we're producing a final executable, sort the contents of the
985142a4 3104 unwind section. */
46fe4e66 3105 return elf_hppa_sort_unwind (abfd);
c46b7515
AM
3106}
3107
3108/* Record the lowest address for the data and text segments. */
3109
3110static void
c39a58e6
AM
3111hppa_record_segment_addr (bfd *abfd ATTRIBUTE_UNUSED,
3112 asection *section,
3113 void *data)
c46b7515 3114{
83c81bfe 3115 struct elf32_hppa_link_hash_table *htab;
c46b7515 3116
875c0872 3117 htab = (struct elf32_hppa_link_hash_table*) data;
c46b7515
AM
3118
3119 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3120 {
3121 bfd_vma value = section->vma - section->filepos;
3122
3123 if ((section->flags & SEC_READONLY) != 0)
3124 {
83c81bfe
AM
3125 if (value < htab->text_segment_base)
3126 htab->text_segment_base = value;
c46b7515
AM
3127 }
3128 else
3129 {
83c81bfe
AM
3130 if (value < htab->data_segment_base)
3131 htab->data_segment_base = value;
c46b7515
AM
3132 }
3133 }
3134}
3135
30667bf3
AM
3136/* Perform a relocation as part of a final link. */
3137
3138static bfd_reloc_status_type
c39a58e6
AM
3139final_link_relocate (asection *input_section,
3140 bfd_byte *contents,
875c0872 3141 const Elf_Internal_Rela *rela,
c39a58e6
AM
3142 bfd_vma value,
3143 struct elf32_hppa_link_hash_table *htab,
3144 asection *sym_sec,
875c0872 3145 struct elf32_hppa_link_hash_entry *hh,
a252afa4 3146 struct bfd_link_info *info)
30667bf3
AM
3147{
3148 int insn;
875c0872 3149 unsigned int r_type = ELF32_R_TYPE (rela->r_info);
a252afa4 3150 unsigned int orig_r_type = r_type;
30667bf3
AM
3151 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3152 int r_format = howto->bitsize;
3153 enum hppa_reloc_field_selector_type_alt r_field;
3154 bfd *input_bfd = input_section->owner;
875c0872 3155 bfd_vma offset = rela->r_offset;
30667bf3
AM
3156 bfd_vma max_branch_offset = 0;
3157 bfd_byte *hit_data = contents + offset;
875c0872 3158 bfd_signed_vma addend = rela->r_addend;
30667bf3 3159 bfd_vma location;
875c0872
DA
3160 struct elf32_hppa_stub_hash_entry *hsh = NULL;
3161 int val;
30667bf3
AM
3162
3163 if (r_type == R_PARISC_NONE)
3164 return bfd_reloc_ok;
3165
3166 insn = bfd_get_32 (input_bfd, hit_data);
3167
3168 /* Find out where we are and where we're going. */
3169 location = (offset +
3170 input_section->output_offset +
3171 input_section->output_section->vma);
3172
a252afa4
DA
3173 /* If we are not building a shared library, convert DLTIND relocs to
3174 DPREL relocs. */
3175 if (!info->shared)
3176 {
3177 switch (r_type)
4fc8051d
AM
3178 {
3179 case R_PARISC_DLTIND21L:
3180 r_type = R_PARISC_DPREL21L;
a252afa4
DA
3181 break;
3182
4fc8051d
AM
3183 case R_PARISC_DLTIND14R:
3184 r_type = R_PARISC_DPREL14R;
a252afa4
DA
3185 break;
3186
4fc8051d
AM
3187 case R_PARISC_DLTIND14F:
3188 r_type = R_PARISC_DPREL14F;
a252afa4
DA
3189 break;
3190 }
3191 }
3192
30667bf3
AM
3193 switch (r_type)
3194 {
3195 case R_PARISC_PCREL12F:
3196 case R_PARISC_PCREL17F:
3197 case R_PARISC_PCREL22F:
067fa4a6
AM
3198 /* If this call should go via the plt, find the import stub in
3199 the stub hash. */
30667bf3
AM
3200 if (sym_sec == NULL
3201 || sym_sec->output_section == NULL
875c0872 3202 || (hh != NULL
a63e02c7
DA
3203 && hh->eh.plt.offset != (bfd_vma) -1
3204 && hh->eh.dynindx != -1
875c0872 3205 && !hh->plabel
a252afa4 3206 && (info->shared
a63e02c7
DA
3207 || !hh->eh.def_regular
3208 || hh->eh.root.type == bfd_link_hash_defweak)))
30667bf3 3209 {
875c0872
DA
3210 hsh = hppa_get_stub_entry (input_section, sym_sec,
3211 hh, rela, htab);
3212 if (hsh != NULL)
30667bf3 3213 {
875c0872
DA
3214 value = (hsh->stub_offset
3215 + hsh->stub_sec->output_offset
3216 + hsh->stub_sec->output_section->vma);
30667bf3
AM
3217 addend = 0;
3218 }
875c0872 3219 else if (sym_sec == NULL && hh != NULL
a63e02c7 3220 && hh->eh.root.type == bfd_link_hash_undefweak)
30667bf3 3221 {
db20fd76
AM
3222 /* It's OK if undefined weak. Calls to undefined weak
3223 symbols behave as if the "called" function
3224 immediately returns. We can thus call to a weak
3225 function without first checking whether the function
3226 is defined. */
30667bf3 3227 value = location;
db20fd76 3228 addend = 8;
30667bf3
AM
3229 }
3230 else
f09ebc7d 3231 return bfd_reloc_undefined;
30667bf3
AM
3232 }
3233 /* Fall thru. */
3234
3235 case R_PARISC_PCREL21L:
3236 case R_PARISC_PCREL17C:
3237 case R_PARISC_PCREL17R:
3238 case R_PARISC_PCREL14R:
3239 case R_PARISC_PCREL14F:
36751eee 3240 case R_PARISC_PCREL32:
30667bf3
AM
3241 /* Make it a pc relative offset. */
3242 value -= location;
3243 addend -= 8;
3244 break;
3245
3246 case R_PARISC_DPREL21L:
3247 case R_PARISC_DPREL14R:
3248 case R_PARISC_DPREL14F:
a252afa4
DA
3249 /* Convert instructions that use the linkage table pointer (r19) to
3250 instructions that use the global data pointer (dp). This is the
3251 most efficient way of using PIC code in an incomplete executable,
3252 but the user must follow the standard runtime conventions for
3253 accessing data for this to work. */
3254 if (orig_r_type == R_PARISC_DLTIND21L)
3255 {
3256 /* Convert addil instructions if the original reloc was a
3257 DLTIND21L. GCC sometimes uses a register other than r19 for
3258 the operation, so we must convert any addil instruction
3259 that uses this relocation. */
3260 if ((insn & 0xfc000000) == ((int) OP_ADDIL << 26))
3261 insn = ADDIL_DP;
3262 else
3263 /* We must have a ldil instruction. It's too hard to find
3264 and convert the associated add instruction, so issue an
3265 error. */
3266 (*_bfd_error_handler)
d003868e
AM
3267 (_("%B(%A+0x%lx): %s fixup for insn 0x%x is not supported in a non-shared link"),
3268 input_bfd,
3269 input_section,
875c0872 3270 offset,
a252afa4
DA
3271 howto->name,
3272 insn);
3273 }
3274 else if (orig_r_type == R_PARISC_DLTIND14F)
3275 {
3276 /* This must be a format 1 load/store. Change the base
3277 register to dp. */
3278 insn = (insn & 0xfc1ffff) | (27 << 21);
3279 }
3280
30667bf3 3281 /* For all the DP relative relocations, we need to examine the symbol's
95d0f04a
DA
3282 section. If it has no section or if it's a code section, then
3283 "data pointer relative" makes no sense. In that case we don't
3284 adjust the "value", and for 21 bit addil instructions, we change the
3285 source addend register from %dp to %r0. This situation commonly
3286 arises for undefined weak symbols and when a variable's "constness"
30667bf3
AM
3287 is declared differently from the way the variable is defined. For
3288 instance: "extern int foo" with foo defined as "const int foo". */
95d0f04a 3289 if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
30667bf3
AM
3290 {
3291 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3292 == (((int) OP_ADDIL << 26) | (27 << 21)))
3293 {
3294 insn &= ~ (0x1f << 21);
30667bf3
AM
3295 }
3296 /* Now try to make things easy for the dynamic linker. */
3297
3298 break;
3299 }
74d1c347 3300 /* Fall thru. */
30667bf3
AM
3301
3302 case R_PARISC_DLTIND21L:
3303 case R_PARISC_DLTIND14R:
3304 case R_PARISC_DLTIND14F:
3305 value -= elf_gp (input_section->output_section->owner);
3306 break;
3307
c46b7515
AM
3308 case R_PARISC_SEGREL32:
3309 if ((sym_sec->flags & SEC_CODE) != 0)
83c81bfe 3310 value -= htab->text_segment_base;
c46b7515 3311 else
83c81bfe 3312 value -= htab->data_segment_base;
c46b7515
AM
3313 break;
3314
30667bf3
AM
3315 default:
3316 break;
3317 }
3318
3319 switch (r_type)
3320 {
3321 case R_PARISC_DIR32:
47d89dba 3322 case R_PARISC_DIR14F:
30667bf3
AM
3323 case R_PARISC_DIR17F:
3324 case R_PARISC_PCREL17C:
3325 case R_PARISC_PCREL14F:
36751eee 3326 case R_PARISC_PCREL32:
30667bf3
AM
3327 case R_PARISC_DPREL14F:
3328 case R_PARISC_PLABEL32:
3329 case R_PARISC_DLTIND14F:
3330 case R_PARISC_SEGBASE:
3331 case R_PARISC_SEGREL32:
3332 r_field = e_fsel;
3333 break;
3334
1bf42538 3335 case R_PARISC_DLTIND21L:
30667bf3 3336 case R_PARISC_PCREL21L:
30667bf3 3337 case R_PARISC_PLABEL21L:
1bf42538
JL
3338 r_field = e_lsel;
3339 break;
3340
3341 case R_PARISC_DIR21L:
3342 case R_PARISC_DPREL21L:
30667bf3
AM
3343 r_field = e_lrsel;
3344 break;
3345
30667bf3 3346 case R_PARISC_PCREL17R:
30667bf3 3347 case R_PARISC_PCREL14R:
30667bf3
AM
3348 case R_PARISC_PLABEL14R:
3349 case R_PARISC_DLTIND14R:
1bf42538
JL
3350 r_field = e_rsel;
3351 break;
3352
3353 case R_PARISC_DIR17R:
3354 case R_PARISC_DIR14R:
3355 case R_PARISC_DPREL14R:
30667bf3
AM
3356 r_field = e_rrsel;
3357 break;
3358
3359 case R_PARISC_PCREL12F:
3360 case R_PARISC_PCREL17F:
3361 case R_PARISC_PCREL22F:
3362 r_field = e_fsel;
3363
3364 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3365 {
3366 max_branch_offset = (1 << (17-1)) << 2;
3367 }
3368 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3369 {
3370 max_branch_offset = (1 << (12-1)) << 2;
3371 }
3372 else
3373 {
3374 max_branch_offset = (1 << (22-1)) << 2;
3375 }
3376
3377 /* sym_sec is NULL on undefined weak syms or when shared on
3378 undefined syms. We've already checked for a stub for the
3379 shared undefined case. */
3380 if (sym_sec == NULL)
3381 break;
3382
3383 /* If the branch is out of reach, then redirect the
3384 call to the local stub for this function. */
3385 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3386 {
875c0872
DA
3387 hsh = hppa_get_stub_entry (input_section, sym_sec,
3388 hh, rela, htab);
3389 if (hsh == NULL)
f09ebc7d 3390 return bfd_reloc_undefined;
30667bf3
AM
3391
3392 /* Munge up the value and addend so that we call the stub
3393 rather than the procedure directly. */
875c0872
DA
3394 value = (hsh->stub_offset
3395 + hsh->stub_sec->output_offset
3396 + hsh->stub_sec->output_section->vma
30667bf3
AM
3397 - location);
3398 addend = -8;
3399 }
3400 break;
3401
3402 /* Something we don't know how to handle. */
3403 default:
3404 return bfd_reloc_notsupported;
3405 }
3406
3407 /* Make sure we can reach the stub. */
3408 if (max_branch_offset != 0
3409 && value + addend + max_branch_offset >= 2*max_branch_offset)
3410 {
3411 (*_bfd_error_handler)
d003868e
AM
3412 (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3413 input_bfd,
3414 input_section,
875c0872 3415 offset,
a63e02c7 3416 hsh->bh_root.string);
ce757d15 3417 bfd_set_error (bfd_error_bad_value);
30667bf3
AM
3418 return bfd_reloc_notsupported;
3419 }
3420
3421 val = hppa_field_adjust (value, addend, r_field);
3422
3423 switch (r_type)
3424 {
3425 case R_PARISC_PCREL12F:
3426 case R_PARISC_PCREL17C:
3427 case R_PARISC_PCREL17F:
3428 case R_PARISC_PCREL17R:
3429 case R_PARISC_PCREL22F:
3430 case R_PARISC_DIR17F:
3431 case R_PARISC_DIR17R:
3432 /* This is a branch. Divide the offset by four.
3433 Note that we need to decide whether it's a branch or
3434 otherwise by inspecting the reloc. Inspecting insn won't
3435 work as insn might be from a .word directive. */
3436 val >>= 2;
3437 break;
3438
3439 default:
3440 break;
3441 }
3442
3443 insn = hppa_rebuild_insn (insn, val, r_format);
3444
3445 /* Update the instruction word. */
74d1c347 3446 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
30667bf3
AM
3447 return bfd_reloc_ok;
3448}
3449
30667bf3
AM
3450/* Relocate an HPPA ELF section. */
3451
b34976b6 3452static bfd_boolean
c39a58e6
AM
3453elf32_hppa_relocate_section (bfd *output_bfd,
3454 struct bfd_link_info *info,
3455 bfd *input_bfd,
3456 asection *input_section,
3457 bfd_byte *contents,
3458 Elf_Internal_Rela *relocs,
3459 Elf_Internal_Sym *local_syms,
3460 asection **local_sections)
30667bf3 3461{
30667bf3 3462 bfd_vma *local_got_offsets;
83c81bfe 3463 struct elf32_hppa_link_hash_table *htab;
30667bf3 3464 Elf_Internal_Shdr *symtab_hdr;
875c0872 3465 Elf_Internal_Rela *rela;
30667bf3 3466 Elf_Internal_Rela *relend;
30667bf3 3467
1049f94e 3468 if (info->relocatable)
b34976b6 3469 return TRUE;
f0fe0e16 3470
30667bf3
AM
3471 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3472
83c81bfe 3473 htab = hppa_link_hash_table (info);
74d1c347 3474 local_got_offsets = elf_local_got_offsets (input_bfd);
30667bf3 3475
875c0872 3476 rela = relocs;
30667bf3 3477 relend = relocs + input_section->reloc_count;
875c0872 3478 for (; rela < relend; rela++)
30667bf3
AM
3479 {
3480 unsigned int r_type;
3481 reloc_howto_type *howto;
3482 unsigned int r_symndx;
875c0872 3483 struct elf32_hppa_link_hash_entry *hh;
30667bf3
AM
3484 Elf_Internal_Sym *sym;
3485 asection *sym_sec;
3486 bfd_vma relocation;
875c0872 3487 bfd_reloc_status_type rstatus;
30667bf3 3488 const char *sym_name;
b34976b6
AM
3489 bfd_boolean plabel;
3490 bfd_boolean warned_undef;
30667bf3 3491
875c0872 3492 r_type = ELF32_R_TYPE (rela->r_info);
30667bf3
AM
3493 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3494 {
3495 bfd_set_error (bfd_error_bad_value);
b34976b6 3496 return FALSE;
30667bf3
AM
3497 }
3498 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3499 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3500 continue;
3501
30667bf3 3502 /* This is a final link. */
875c0872
DA
3503 r_symndx = ELF32_R_SYM (rela->r_info);
3504 hh = NULL;
30667bf3
AM
3505 sym = NULL;
3506 sym_sec = NULL;
b34976b6 3507 warned_undef = FALSE;
30667bf3
AM
3508 if (r_symndx < symtab_hdr->sh_info)
3509 {
3510 /* This is a local symbol, h defaults to NULL. */
3511 sym = local_syms + r_symndx;
3512 sym_sec = local_sections[r_symndx];
875c0872 3513 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rela);
30667bf3
AM
3514 }
3515 else
3516 {
875c0872 3517 struct elf_link_hash_entry *eh;
560e09e9 3518 bfd_boolean unresolved_reloc;
b2a8e766 3519 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
560e09e9 3520
875c0872 3521 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rela,
b2a8e766 3522 r_symndx, symtab_hdr, sym_hashes,
875c0872 3523 eh, sym_sec, relocation,
b2a8e766 3524 unresolved_reloc, warned_undef);
560e09e9
NC
3525
3526 if (relocation == 0
875c0872
DA
3527 && eh->root.type != bfd_link_hash_defined
3528 && eh->root.type != bfd_link_hash_defweak
3529 && eh->root.type != bfd_link_hash_undefweak)
4fc8051d 3530 {
59c2e50f 3531 if (info->unresolved_syms_in_objects == RM_IGNORE
875c0872
DA
3532 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3533 && eh->type == STT_PARISC_MILLI)
560e09e9
NC
3534 {
3535 if (! info->callbacks->undefined_symbol
875c0872
DA
3536 (info, eh->root.root.string, input_bfd,
3537 input_section, rela->r_offset, FALSE))
560e09e9
NC
3538 return FALSE;
3539 warned_undef = TRUE;
3540 }
30667bf3 3541 }
875c0872 3542 hh = hppa_elf_hash_entry (eh);
30667bf3
AM
3543 }
3544
3545 /* Do any required modifications to the relocation value, and
25f72752
AM
3546 determine what types of dynamic info we need to output, if
3547 any. */
74d1c347 3548 plabel = 0;
30667bf3
AM
3549 switch (r_type)
3550 {
3551 case R_PARISC_DLTIND14F:
3552 case R_PARISC_DLTIND14R:
3553 case R_PARISC_DLTIND21L:
ce757d15
AM
3554 {
3555 bfd_vma off;
b34976b6 3556 bfd_boolean do_got = 0;
ce757d15
AM
3557
3558 /* Relocation is to the entry for this symbol in the
3559 global offset table. */
875c0872 3560 if (hh != NULL)
ce757d15 3561 {
b34976b6 3562 bfd_boolean dyn;
ce757d15 3563
a63e02c7
DA
3564 off = hh->eh.got.offset;
3565 dyn = htab->etab.dynamic_sections_created;
c152c796 3566 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
a63e02c7 3567 &hh->eh))
ce757d15
AM
3568 {
3569 /* If we aren't going to call finish_dynamic_symbol,
3570 then we need to handle initialisation of the .got
3571 entry and create needed relocs here. Since the
3572 offset must always be a multiple of 4, we use the
3573 least significant bit to record whether we have
3574 initialised it already. */
3575 if ((off & 1) != 0)
3576 off &= ~1;
3577 else
3578 {
a63e02c7 3579 hh->eh.got.offset |= 1;
ce757d15
AM
3580 do_got = 1;
3581 }
3582 }
3583 }
3584 else
3585 {
3586 /* Local symbol case. */
3587 if (local_got_offsets == NULL)
3588 abort ();
3589
3590 off = local_got_offsets[r_symndx];
3591
3592 /* The offset must always be a multiple of 4. We use
3593 the least significant bit to record whether we have
3594 already generated the necessary reloc. */
3595 if ((off & 1) != 0)
3596 off &= ~1;
3597 else
3598 {
3599 local_got_offsets[r_symndx] |= 1;
3600 do_got = 1;
3601 }
3602 }
68fb2e56 3603
ce757d15
AM
3604 if (do_got)
3605 {
3606 if (info->shared)
3607 {
3608 /* Output a dynamic relocation for this GOT entry.
3609 In this case it is relative to the base of the
3610 object because the symbol index is zero. */
3611 Elf_Internal_Rela outrel;
947216bf 3612 bfd_byte *loc;
875c0872 3613 asection *sec = htab->srelgot;
ce757d15
AM
3614
3615 outrel.r_offset = (off
3616 + htab->sgot->output_offset
3617 + htab->sgot->output_section->vma);
3618 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3619 outrel.r_addend = relocation;
875c0872
DA
3620 loc = sec->contents;
3621 loc += sec->reloc_count++ * sizeof (Elf32_External_Rela);
ce757d15
AM
3622 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3623 }
3624 else
30667bf3 3625 bfd_put_32 (output_bfd, relocation,
83c81bfe 3626 htab->sgot->contents + off);
ce757d15 3627 }
30667bf3 3628
ce757d15
AM
3629 if (off >= (bfd_vma) -2)
3630 abort ();
30667bf3 3631
ce757d15
AM
3632 /* Add the base of the GOT to the relocation value. */
3633 relocation = (off
3634 + htab->sgot->output_offset
3635 + htab->sgot->output_section->vma);
3636 }
30667bf3 3637 break;
252b5132 3638
c46b7515
AM
3639 case R_PARISC_SEGREL32:
3640 /* If this is the first SEGREL relocation, then initialize
3641 the segment base values. */
83c81bfe
AM
3642 if (htab->text_segment_base == (bfd_vma) -1)
3643 bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
c46b7515
AM
3644 break;
3645
30667bf3
AM
3646 case R_PARISC_PLABEL14R:
3647 case R_PARISC_PLABEL21L:
3648 case R_PARISC_PLABEL32:
a63e02c7 3649 if (htab->etab.dynamic_sections_created)
252b5132 3650 {
ce757d15 3651 bfd_vma off;
b34976b6 3652 bfd_boolean do_plt = 0;
74d1c347
AM
3653 /* If we have a global symbol with a PLT slot, then
3654 redirect this relocation to it. */
875c0872 3655 if (hh != NULL)
74d1c347 3656 {
a63e02c7 3657 off = hh->eh.plt.offset;
c152c796 3658 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared,
a63e02c7 3659 &hh->eh))
8dea1268
AM
3660 {
3661 /* In a non-shared link, adjust_dynamic_symbols
3662 isn't called for symbols forced local. We
dc810e39 3663 need to write out the plt entry here. */
8dea1268
AM
3664 if ((off & 1) != 0)
3665 off &= ~1;
3666 else
3667 {
a63e02c7 3668 hh->eh.plt.offset |= 1;
ce757d15 3669 do_plt = 1;
8dea1268
AM
3670 }
3671 }
74d1c347
AM
3672 }
3673 else
3674 {
68fb2e56
AM
3675 bfd_vma *local_plt_offsets;
3676
3677 if (local_got_offsets == NULL)
3678 abort ();
74d1c347 3679
68fb2e56
AM
3680 local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
3681 off = local_plt_offsets[r_symndx];
74d1c347
AM
3682
3683 /* As for the local .got entry case, we use the last
3684 bit to record whether we've already initialised
3685 this local .plt entry. */
3686 if ((off & 1) != 0)
3687 off &= ~1;
ce757d15
AM
3688 else
3689 {
3690 local_plt_offsets[r_symndx] |= 1;
3691 do_plt = 1;
3692 }
3693 }
3694
3695 if (do_plt)
3696 {
3697 if (info->shared)
3698 {
3699 /* Output a dynamic IPLT relocation for this
3700 PLT entry. */
3701 Elf_Internal_Rela outrel;
947216bf
AM
3702 bfd_byte *loc;
3703 asection *s = htab->srelplt;
ce757d15
AM
3704
3705 outrel.r_offset = (off
3706 + htab->splt->output_offset
3707 + htab->splt->output_section->vma);
3708 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3709 outrel.r_addend = relocation;
947216bf
AM
3710 loc = s->contents;
3711 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
ce757d15
AM
3712 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3713 }
74d1c347
AM
3714 else
3715 {
3716 bfd_put_32 (output_bfd,
3717 relocation,
83c81bfe 3718 htab->splt->contents + off);
74d1c347 3719 bfd_put_32 (output_bfd,
83c81bfe
AM
3720 elf_gp (htab->splt->output_section->owner),
3721 htab->splt->contents + off + 4);
74d1c347
AM
3722 }
3723 }
3724
68fb2e56 3725 if (off >= (bfd_vma) -2)
49e9d0d3 3726 abort ();
74d1c347
AM
3727
3728 /* PLABELs contain function pointers. Relocation is to
3729 the entry for the function in the .plt. The magic +2
3730 offset signals to $$dyncall that the function pointer
3731 is in the .plt and thus has a gp pointer too.
3732 Exception: Undefined PLABELs should have a value of
3733 zero. */
875c0872 3734 if (hh == NULL
a63e02c7
DA
3735 || (hh->eh.root.type != bfd_link_hash_undefweak
3736 && hh->eh.root.type != bfd_link_hash_undefined))
74d1c347
AM
3737 {
3738 relocation = (off
83c81bfe
AM
3739 + htab->splt->output_offset
3740 + htab->splt->output_section->vma
74d1c347
AM
3741 + 2);
3742 }
3743 plabel = 1;
30667bf3
AM
3744 }
3745 /* Fall through and possibly emit a dynamic relocation. */
3746
3747 case R_PARISC_DIR17F:
3748 case R_PARISC_DIR17R:
47d89dba 3749 case R_PARISC_DIR14F:
30667bf3
AM
3750 case R_PARISC_DIR14R:
3751 case R_PARISC_DIR21L:
3752 case R_PARISC_DPREL14F:
3753 case R_PARISC_DPREL14R:
3754 case R_PARISC_DPREL21L:
3755 case R_PARISC_DIR32:
ec338859
AM
3756 /* r_symndx will be zero only for relocs against symbols
3757 from removed linkonce sections, or sections discarded by
3758 a linker script. */
3759 if (r_symndx == 0
3760 || (input_section->flags & SEC_ALLOC) == 0)
3761 break;
3762
30667bf3 3763 /* The reloc types handled here and this conditional
56882138 3764 expression must match the code in ..check_relocs and
ec338859 3765 allocate_dynrelocs. ie. We need exactly the same condition
56882138
AM
3766 as in ..check_relocs, with some extra conditions (dynindx
3767 test in this case) to cater for relocs removed by
ec338859 3768 allocate_dynrelocs. If you squint, the non-shared test
56882138
AM
3769 here does indeed match the one in ..check_relocs, the
3770 difference being that here we test DEF_DYNAMIC as well as
3771 !DEF_REGULAR. All common syms end up with !DEF_REGULAR,
3772 which is why we can't use just that test here.
3773 Conversely, DEF_DYNAMIC can't be used in check_relocs as
3774 there all files have not been loaded. */
446f2863 3775 if ((info->shared
875c0872 3776 && (hh == NULL
a63e02c7
DA
3777 || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
3778 || hh->eh.root.type != bfd_link_hash_undefweak)
446f2863 3779 && (IS_ABSOLUTE_RELOC (r_type)
a63e02c7 3780 || !SYMBOL_CALLS_LOCAL (info, &hh->eh)))
446f2863 3781 || (!info->shared
875c0872 3782 && hh != NULL
a63e02c7
DA
3783 && hh->eh.dynindx != -1
3784 && !hh->eh.non_got_ref
4fc8051d 3785 && ((ELIMINATE_COPY_RELOCS
a63e02c7
DA
3786 && hh->eh.def_dynamic
3787 && !hh->eh.def_regular)
3788 || hh->eh.root.type == bfd_link_hash_undefweak
3789 || hh->eh.root.type == bfd_link_hash_undefined)))
30667bf3
AM
3790 {
3791 Elf_Internal_Rela outrel;
b34976b6 3792 bfd_boolean skip;
98ceb8ce 3793 asection *sreloc;
947216bf 3794 bfd_byte *loc;
252b5132 3795
30667bf3
AM
3796 /* When generating a shared object, these relocations
3797 are copied into the output file to be resolved at run
3798 time. */
252b5132 3799
875c0872 3800 outrel.r_addend = rela->r_addend;
c629eae0
JJ
3801 outrel.r_offset =
3802 _bfd_elf_section_offset (output_bfd, info, input_section,
875c0872 3803 rela->r_offset);
0bb2d96a
JJ
3804 skip = (outrel.r_offset == (bfd_vma) -1
3805 || outrel.r_offset == (bfd_vma) -2);
30667bf3
AM
3806 outrel.r_offset += (input_section->output_offset
3807 + input_section->output_section->vma);
875c0872 3808
30667bf3 3809 if (skip)
252b5132 3810 {
30667bf3 3811 memset (&outrel, 0, sizeof (outrel));
252b5132 3812 }
875c0872 3813 else if (hh != NULL
a63e02c7 3814 && hh->eh.dynindx != -1
74d1c347 3815 && (plabel
446f2863
AM
3816 || !IS_ABSOLUTE_RELOC (r_type)
3817 || !info->shared
74d1c347 3818 || !info->symbolic
a63e02c7 3819 || !hh->eh.def_regular))
252b5132 3820 {
a63e02c7 3821 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
30667bf3
AM
3822 }
3823 else /* It's a local symbol, or one marked to become local. */
3824 {
3825 int indx = 0;
edd21aca 3826
30667bf3
AM
3827 /* Add the absolute offset of the symbol. */
3828 outrel.r_addend += relocation;
edd21aca 3829
74d1c347
AM
3830 /* Global plabels need to be processed by the
3831 dynamic linker so that functions have at most one
3832 fptr. For this reason, we need to differentiate
3833 between global and local plabels, which we do by
3834 providing the function symbol for a global plabel
3835 reloc, and no symbol for local plabels. */
3836 if (! plabel
3837 && sym_sec != NULL
30667bf3
AM
3838 && sym_sec->output_section != NULL
3839 && ! bfd_is_abs_section (sym_sec))
252b5132 3840 {
4b71bec0
DA
3841 /* Skip this relocation if the output section has
3842 been discarded. */
3843 if (bfd_is_abs_section (sym_sec->output_section))
3844 break;
3845
30667bf3
AM
3846 indx = elf_section_data (sym_sec->output_section)->dynindx;
3847 /* We are turning this relocation into one
3848 against a section symbol, so subtract out the
3849 output section's address but not the offset
3850 of the input section in the output section. */
3851 outrel.r_addend -= sym_sec->output_section->vma;
252b5132 3852 }
252b5132 3853
30667bf3
AM
3854 outrel.r_info = ELF32_R_INFO (indx, r_type);
3855 }
98ceb8ce
AM
3856 sreloc = elf_section_data (input_section)->sreloc;
3857 if (sreloc == NULL)
3858 abort ();
3859
947216bf
AM
3860 loc = sreloc->contents;
3861 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
98ceb8ce 3862 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
30667bf3
AM
3863 }
3864 break;
edd21aca 3865
30667bf3
AM
3866 default:
3867 break;
3868 }
252b5132 3869
875c0872
DA
3870 rstatus = final_link_relocate (input_section, contents, rela, relocation,
3871 htab, sym_sec, hh, info);
252b5132 3872
875c0872 3873 if (rstatus == bfd_reloc_ok)
30667bf3 3874 continue;
252b5132 3875
875c0872 3876 if (hh != NULL)
a63e02c7 3877 sym_name = hh->eh.root.root.string;
30667bf3
AM
3878 else
3879 {
3880 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3881 symtab_hdr->sh_link,
3882 sym->st_name);
3883 if (sym_name == NULL)
b34976b6 3884 return FALSE;
30667bf3
AM
3885 if (*sym_name == '\0')
3886 sym_name = bfd_section_name (input_bfd, sym_sec);
3887 }
edd21aca 3888
30667bf3 3889 howto = elf_hppa_howto_table + r_type;
252b5132 3890
875c0872 3891 if (rstatus == bfd_reloc_undefined || rstatus == bfd_reloc_notsupported)
30667bf3 3892 {
875c0872 3893 if (rstatus == bfd_reloc_notsupported || !warned_undef)
f09ebc7d
AM
3894 {
3895 (*_bfd_error_handler)
d003868e
AM
3896 (_("%B(%A+0x%lx): cannot handle %s for %s"),
3897 input_bfd,
3898 input_section,
875c0872 3899 (long) rela->r_offset,
f09ebc7d
AM
3900 howto->name,
3901 sym_name);
3902 bfd_set_error (bfd_error_bad_value);
b34976b6 3903 return FALSE;
f09ebc7d 3904 }
30667bf3
AM
3905 }
3906 else
3907 {
3908 if (!((*info->callbacks->reloc_overflow)
a63e02c7 3909 (info, (hh ? &hh->eh.root : NULL), sym_name, howto->name,
875c0872 3910 (bfd_vma) 0, input_bfd, input_section, rela->r_offset)))
b34976b6 3911 return FALSE;
30667bf3
AM
3912 }
3913 }
edd21aca 3914
b34976b6 3915 return TRUE;
30667bf3 3916}
252b5132 3917
30667bf3
AM
3918/* Finish up dynamic symbol handling. We set the contents of various
3919 dynamic sections here. */
252b5132 3920
b34976b6 3921static bfd_boolean
c39a58e6
AM
3922elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
3923 struct bfd_link_info *info,
875c0872 3924 struct elf_link_hash_entry *eh,
c39a58e6 3925 Elf_Internal_Sym *sym)
30667bf3 3926{
83c81bfe 3927 struct elf32_hppa_link_hash_table *htab;
875c0872 3928 Elf_Internal_Rela rela;
a252afa4 3929 bfd_byte *loc;
edd21aca 3930
83c81bfe 3931 htab = hppa_link_hash_table (info);
30667bf3 3932
875c0872 3933 if (eh->plt.offset != (bfd_vma) -1)
30667bf3
AM
3934 {
3935 bfd_vma value;
30667bf3 3936
875c0872 3937 if (eh->plt.offset & 1)
8dea1268
AM
3938 abort ();
3939
30667bf3
AM
3940 /* This symbol has an entry in the procedure linkage table. Set
3941 it up.
3942
3943 The format of a plt entry is
74d1c347
AM
3944 <funcaddr>
3945 <__gp>
47d89dba 3946 */
30667bf3 3947 value = 0;
875c0872
DA
3948 if (eh->root.type == bfd_link_hash_defined
3949 || eh->root.type == bfd_link_hash_defweak)
30667bf3 3950 {
875c0872
DA
3951 value = eh->root.u.def.value;
3952 if (eh->root.u.def.section->output_section != NULL)
3953 value += (eh->root.u.def.section->output_offset
3954 + eh->root.u.def.section->output_section->vma);
252b5132 3955 }
edd21aca 3956
a252afa4 3957 /* Create a dynamic IPLT relocation for this entry. */
875c0872 3958 rela.r_offset = (eh->plt.offset
a252afa4
DA
3959 + htab->splt->output_offset
3960 + htab->splt->output_section->vma);
875c0872 3961 if (eh->dynindx != -1)
30667bf3 3962 {
875c0872
DA
3963 rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_IPLT);
3964 rela.r_addend = 0;
30667bf3 3965 }
ce757d15 3966 else
47d89dba 3967 {
a252afa4
DA
3968 /* This symbol has been marked to become local, and is
3969 used by a plabel so must be kept in the .plt. */
875c0872
DA
3970 rela.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3971 rela.r_addend = value;
47d89dba
AM
3972 }
3973
a252afa4
DA
3974 loc = htab->srelplt->contents;
3975 loc += htab->srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
875c0872 3976 bfd_elf32_swap_reloca_out (htab->splt->output_section->owner, &rela, loc);
a252afa4 3977
875c0872 3978 if (!eh->def_regular)
30667bf3
AM
3979 {
3980 /* Mark the symbol as undefined, rather than as defined in
3981 the .plt section. Leave the value alone. */
3982 sym->st_shndx = SHN_UNDEF;
3983 }
3984 }
edd21aca 3985
875c0872 3986 if (eh->got.offset != (bfd_vma) -1)
30667bf3 3987 {
30667bf3
AM
3988 /* This symbol has an entry in the global offset table. Set it
3989 up. */
3990
875c0872 3991 rela.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
83c81bfe
AM
3992 + htab->sgot->output_offset
3993 + htab->sgot->output_section->vma);
30667bf3 3994
4dc86686
AM
3995 /* If this is a -Bsymbolic link and the symbol is defined
3996 locally or was forced to be local because of a version file,
3997 we just want to emit a RELATIVE reloc. The entry in the
3998 global offset table will already have been initialized in the
3999 relocate_section function. */
4000 if (info->shared
875c0872
DA
4001 && (info->symbolic || eh->dynindx == -1)
4002 && eh->def_regular)
30667bf3 4003 {
875c0872
DA
4004 rela.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
4005 rela.r_addend = (eh->root.u.def.value
4006 + eh->root.u.def.section->output_offset
4007 + eh->root.u.def.section->output_section->vma);
30667bf3
AM
4008 }
4009 else
4010 {
875c0872 4011 if ((eh->got.offset & 1) != 0)
49e9d0d3 4012 abort ();
875c0872
DA
4013
4014 bfd_put_32 (output_bfd, 0, htab->sgot->contents + (eh->got.offset & ~1));
4015 rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_DIR32);
4016 rela.r_addend = 0;
30667bf3 4017 }
edd21aca 4018
947216bf
AM
4019 loc = htab->srelgot->contents;
4020 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
875c0872 4021 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
30667bf3 4022 }
edd21aca 4023
875c0872 4024 if (eh->needs_copy)
30667bf3 4025 {
875c0872 4026 asection *sec;
30667bf3
AM
4027
4028 /* This symbol needs a copy reloc. Set it up. */
4029
875c0872
DA
4030 if (! (eh->dynindx != -1
4031 && (eh->root.type == bfd_link_hash_defined
4032 || eh->root.type == bfd_link_hash_defweak)))
49e9d0d3 4033 abort ();
30667bf3 4034
875c0872 4035 sec = htab->srelbss;
30667bf3 4036
875c0872
DA
4037 rela.r_offset = (eh->root.u.def.value
4038 + eh->root.u.def.section->output_offset
4039 + eh->root.u.def.section->output_section->vma);
4040 rela.r_addend = 0;
4041 rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_COPY);
4042 loc = sec->contents + sec->reloc_count++ * sizeof (Elf32_External_Rela);
4043 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
30667bf3
AM
4044 }
4045
4046 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
875c0872
DA
4047 if (eh->root.root.string[0] == '_'
4048 && (strcmp (eh->root.root.string, "_DYNAMIC") == 0
4049 || strcmp (eh->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
30667bf3
AM
4050 {
4051 sym->st_shndx = SHN_ABS;
4052 }
4053
b34976b6 4054 return TRUE;
30667bf3
AM
4055}
4056
98ceb8ce
AM
4057/* Used to decide how to sort relocs in an optimal manner for the
4058 dynamic linker, before writing them out. */
4059
4060static enum elf_reloc_type_class
c39a58e6 4061elf32_hppa_reloc_type_class (const Elf_Internal_Rela *rela)
98ceb8ce
AM
4062{
4063 if (ELF32_R_SYM (rela->r_info) == 0)
4064 return reloc_class_relative;
4065
4066 switch ((int) ELF32_R_TYPE (rela->r_info))
4067 {
4068 case R_PARISC_IPLT:
4069 return reloc_class_plt;
4070 case R_PARISC_COPY:
4071 return reloc_class_copy;
4072 default:
4073 return reloc_class_normal;
4074 }
4075}
4076
30667bf3
AM
4077/* Finish up the dynamic sections. */
4078
b34976b6 4079static bfd_boolean
c39a58e6
AM
4080elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
4081 struct bfd_link_info *info)
30667bf3
AM
4082{
4083 bfd *dynobj;
83c81bfe 4084 struct elf32_hppa_link_hash_table *htab;
30667bf3
AM
4085 asection *sdyn;
4086
83c81bfe 4087 htab = hppa_link_hash_table (info);
a63e02c7 4088 dynobj = htab->etab.dynobj;
30667bf3
AM
4089
4090 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4091
a63e02c7 4092 if (htab->etab.dynamic_sections_created)
30667bf3
AM
4093 {
4094 Elf32_External_Dyn *dyncon, *dynconend;
4095
49e9d0d3
AM
4096 if (sdyn == NULL)
4097 abort ();
30667bf3
AM
4098
4099 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 4100 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
30667bf3 4101 for (; dyncon < dynconend; dyncon++)
edd21aca 4102 {
30667bf3
AM
4103 Elf_Internal_Dyn dyn;
4104 asection *s;
4105
4106 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4107
4108 switch (dyn.d_tag)
4109 {
4110 default:
3ac8354b 4111 continue;
30667bf3
AM
4112
4113 case DT_PLTGOT:
4114 /* Use PLTGOT to set the GOT register. */
4115 dyn.d_un.d_ptr = elf_gp (output_bfd);
30667bf3
AM
4116 break;
4117
4118 case DT_JMPREL:
83c81bfe 4119 s = htab->srelplt;
30667bf3 4120 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
30667bf3
AM
4121 break;
4122
4123 case DT_PLTRELSZ:
83c81bfe 4124 s = htab->srelplt;
eea6121a 4125 dyn.d_un.d_val = s->size;
30667bf3 4126 break;
4e12ff7f
AM
4127
4128 case DT_RELASZ:
4129 /* Don't count procedure linkage table relocs in the
4130 overall reloc count. */
6348e046
AM
4131 s = htab->srelplt;
4132 if (s == NULL)
4133 continue;
eea6121a 4134 dyn.d_un.d_val -= s->size;
6348e046
AM
4135 break;
4136
4137 case DT_RELA:
4138 /* We may not be using the standard ELF linker script.
4139 If .rela.plt is the first .rela section, we adjust
4140 DT_RELA to not include it. */
4141 s = htab->srelplt;
4142 if (s == NULL)
4143 continue;
4144 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4145 continue;
eea6121a 4146 dyn.d_un.d_ptr += s->size;
4e12ff7f 4147 break;
30667bf3 4148 }
3ac8354b
AM
4149
4150 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
edd21aca 4151 }
252b5132 4152 }
edd21aca 4153
eea6121a 4154 if (htab->sgot != NULL && htab->sgot->size != 0)
30667bf3 4155 {
74d1c347
AM
4156 /* Fill in the first entry in the global offset table.
4157 We use it to point to our dynamic section, if we have one. */
30667bf3 4158 bfd_put_32 (output_bfd,
c39a58e6 4159 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
83c81bfe 4160 htab->sgot->contents);
30667bf3 4161
74d1c347 4162 /* The second entry is reserved for use by the dynamic linker. */
83c81bfe 4163 memset (htab->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
74d1c347 4164
30667bf3 4165 /* Set .got entry size. */
83c81bfe 4166 elf_section_data (htab->sgot->output_section)
74d1c347 4167 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
30667bf3
AM
4168 }
4169
eea6121a 4170 if (htab->splt != NULL && htab->splt->size != 0)
47d89dba
AM
4171 {
4172 /* Set plt entry size. */
83c81bfe 4173 elf_section_data (htab->splt->output_section)
47d89dba
AM
4174 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4175
83c81bfe 4176 if (htab->need_plt_stub)
47d89dba
AM
4177 {
4178 /* Set up the .plt stub. */
83c81bfe 4179 memcpy (htab->splt->contents
eea6121a 4180 + htab->splt->size - sizeof (plt_stub),
47d89dba
AM
4181 plt_stub, sizeof (plt_stub));
4182
83c81bfe
AM
4183 if ((htab->splt->output_offset
4184 + htab->splt->output_section->vma
eea6121a 4185 + htab->splt->size)
83c81bfe
AM
4186 != (htab->sgot->output_offset
4187 + htab->sgot->output_section->vma))
47d89dba
AM
4188 {
4189 (*_bfd_error_handler)
4190 (_(".got section not immediately after .plt section"));
b34976b6 4191 return FALSE;
47d89dba
AM
4192 }
4193 }
4194 }
30667bf3 4195
b34976b6 4196 return TRUE;
30667bf3 4197}
252b5132 4198
d952f17a
AM
4199/* Tweak the OSABI field of the elf header. */
4200
4201static void
c39a58e6
AM
4202elf32_hppa_post_process_headers (bfd *abfd,
4203 struct bfd_link_info *info ATTRIBUTE_UNUSED)
d952f17a
AM
4204{
4205 Elf_Internal_Ehdr * i_ehdrp;
4206
4207 i_ehdrp = elf_elfheader (abfd);
4208
4209 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
4210 {
4211 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
4212 }
225247f0
JT
4213 else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
4214 {
4215 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NETBSD;
4216 }
d952f17a
AM
4217 else
4218 {
4219 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4220 }
4221}
4222
30667bf3
AM
4223/* Called when writing out an object file to decide the type of a
4224 symbol. */
4225static int
c39a58e6 4226elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
30667bf3
AM
4227{
4228 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4229 return STT_PARISC_MILLI;
4230 else
4231 return type;
252b5132
RH
4232}
4233
4234/* Misc BFD support code. */
30667bf3
AM
4235#define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4236#define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4237#define elf_info_to_howto elf_hppa_info_to_howto
4238#define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
252b5132 4239
252b5132 4240/* Stuff for the BFD linker. */
c46b7515 4241#define bfd_elf32_bfd_final_link elf32_hppa_final_link
30667bf3 4242#define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
e2d34d7d 4243#define bfd_elf32_bfd_link_hash_table_free elf32_hppa_link_hash_table_free
30667bf3 4244#define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
ebe50bae 4245#define elf_backend_copy_indirect_symbol elf32_hppa_copy_indirect_symbol
30667bf3
AM
4246#define elf_backend_check_relocs elf32_hppa_check_relocs
4247#define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4248#define elf_backend_fake_sections elf_hppa_fake_sections
4249#define elf_backend_relocate_section elf32_hppa_relocate_section
74d1c347 4250#define elf_backend_hide_symbol elf32_hppa_hide_symbol
30667bf3
AM
4251#define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4252#define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4253#define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4254#define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4255#define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
edfc032f
AM
4256#define elf_backend_grok_prstatus elf32_hppa_grok_prstatus
4257#define elf_backend_grok_psinfo elf32_hppa_grok_psinfo
30667bf3
AM
4258#define elf_backend_object_p elf32_hppa_object_p
4259#define elf_backend_final_write_processing elf_hppa_final_write_processing
d952f17a 4260#define elf_backend_post_process_headers elf32_hppa_post_process_headers
30667bf3 4261#define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
98ceb8ce 4262#define elf_backend_reloc_type_class elf32_hppa_reloc_type_class
8a696751 4263#define elf_backend_action_discarded elf_hppa_action_discarded
30667bf3
AM
4264
4265#define elf_backend_can_gc_sections 1
51b64d56 4266#define elf_backend_can_refcount 1
30667bf3
AM
4267#define elf_backend_plt_alignment 2
4268#define elf_backend_want_got_plt 0
4269#define elf_backend_plt_readonly 0
4270#define elf_backend_want_plt_sym 0
74d1c347 4271#define elf_backend_got_header_size 8
f0fe0e16 4272#define elf_backend_rela_normal 1
252b5132
RH
4273
4274#define TARGET_BIG_SYM bfd_elf32_hppa_vec
4275#define TARGET_BIG_NAME "elf32-hppa"
4276#define ELF_ARCH bfd_arch_hppa
4277#define ELF_MACHINE_CODE EM_PARISC
4278#define ELF_MAXPAGESIZE 0x1000
4279
4280#include "elf32-target.h"
d952f17a
AM
4281
4282#undef TARGET_BIG_SYM
4283#define TARGET_BIG_SYM bfd_elf32_hppa_linux_vec
4284#undef TARGET_BIG_NAME
4285#define TARGET_BIG_NAME "elf32-hppa-linux"
4286
4287#define INCLUDED_TARGET_FILE 1
4288#include "elf32-target.h"
225247f0
JT
4289
4290#undef TARGET_BIG_SYM
4291#define TARGET_BIG_SYM bfd_elf32_hppa_nbsd_vec
4292#undef TARGET_BIG_NAME
4293#define TARGET_BIG_NAME "elf32-hppa-netbsd"
4294
4295#include "elf32-target.h"
This page took 0.742631 seconds and 4 git commands to generate.