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