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