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