* elfxx-ia64.c (elfNN_ia64_check_relocs): Handle IPLT relocs.
[binutils-gdb.git] / bfd / elfxx-ia64.c
1 /* IA-64 support for 64-bit ELF
2 Copyright 1998, 1999, 2000 Free Software Foundation, Inc.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "opcode/ia64.h"
26 #include "elf/ia64.h"
27
28
29 /*
30 * THE RULES for all the stuff the linker creates --
31 *
32 * GOT Entries created in response to LTOFF or LTOFF_FPTR
33 * relocations. Dynamic relocs created for dynamic
34 * symbols in an application; REL relocs for locals
35 * in a shared library.
36 *
37 * FPTR The canonical function descriptor. Created for local
38 * symbols in applications. Descriptors for dynamic symbols
39 * and local symbols in shared libraries are created by
40 * ld.so. Thus there are no dynamic relocs against these
41 * objects. The FPTR relocs for such _are_ passed through
42 * to the dynamic relocation tables.
43 *
44 * FULL_PLT Created for a PCREL21B relocation against a dynamic symbol.
45 * Requires the creation of a PLTOFF entry. This does not
46 * require any dynamic relocations.
47 *
48 * PLTOFF Created by PLTOFF relocations. For local symbols, this
49 * is an alternate function descriptor, and in shared libraries
50 * requires two REL relocations. Note that this cannot be
51 * transformed into an FPTR relocation, since it must be in
52 * range of the GP. For dynamic symbols, this is a function
53 * descriptor for a MIN_PLT entry, and requires one IPLT reloc.
54 *
55 * MIN_PLT Created by PLTOFF entries against dynamic symbols. This
56 * does not reqire dynamic relocations.
57 */
58
59 #define USE_RELA /* we want RELA relocs, not REL */
60
61 #define NELEMS(a) ((int) (sizeof (a) / sizeof ((a)[0])))
62
63 typedef struct bfd_hash_entry *(*new_hash_entry_func)
64 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
65
66 /* In dynamically (linker-) created sections, we generally need to keep track
67 of the place a symbol or expression got allocated to. This is done via hash
68 tables that store entries of the following type. */
69
70 struct elfNN_ia64_dyn_sym_info
71 {
72 /* The addend for which this entry is relevant. */
73 bfd_vma addend;
74
75 /* Next addend in the list. */
76 struct elfNN_ia64_dyn_sym_info *next;
77
78 bfd_vma got_offset;
79 bfd_vma fptr_offset;
80 bfd_vma pltoff_offset;
81 bfd_vma plt_offset;
82 bfd_vma plt2_offset;
83
84 /* The symbol table entry, if any, that this was derrived from. */
85 struct elf_link_hash_entry *h;
86
87 /* Used to count non-got, non-plt relocations for delayed sizing
88 of relocation sections. */
89 struct elfNN_ia64_dyn_reloc_entry
90 {
91 struct elfNN_ia64_dyn_reloc_entry *next;
92 asection *srel;
93 int type;
94 int count;
95 } *reloc_entries;
96
97 /* True when the section contents have been updated. */
98 unsigned got_done : 1;
99 unsigned fptr_done : 1;
100 unsigned pltoff_done : 1;
101
102 /* True for the different kinds of linker data we want created. */
103 unsigned want_got : 1;
104 unsigned want_fptr : 1;
105 unsigned want_ltoff_fptr : 1;
106 unsigned want_plt : 1;
107 unsigned want_plt2 : 1;
108 unsigned want_pltoff : 1;
109 };
110
111 struct elfNN_ia64_local_hash_entry
112 {
113 struct bfd_hash_entry root;
114 struct elfNN_ia64_dyn_sym_info *info;
115 };
116
117 struct elfNN_ia64_local_hash_table
118 {
119 struct bfd_hash_table root;
120 /* No additional fields for now. */
121 };
122
123 struct elfNN_ia64_link_hash_entry
124 {
125 struct elf_link_hash_entry root;
126 struct elfNN_ia64_dyn_sym_info *info;
127 };
128
129 struct elfNN_ia64_link_hash_table
130 {
131 /* The main hash table */
132 struct elf_link_hash_table root;
133
134 asection *got_sec; /* the linkage table section (or NULL) */
135 asection *rel_got_sec; /* dynamic relocation section for same */
136 asection *fptr_sec; /* function descriptor table (or NULL) */
137 asection *plt_sec; /* the primary plt section (or NULL) */
138 asection *pltoff_sec; /* private descriptors for plt (or NULL) */
139 asection *rel_pltoff_sec; /* dynamic relocation section for same */
140
141 bfd_size_type minplt_entries; /* number of minplt entries */
142
143 struct elfNN_ia64_local_hash_table loc_hash_table;
144 };
145
146 #define elfNN_ia64_hash_table(p) \
147 ((struct elfNN_ia64_link_hash_table *) ((p)->hash))
148
149 static bfd_reloc_status_type elfNN_ia64_reloc
150 PARAMS ((bfd *abfd, arelent *reloc, asymbol *sym, PTR data,
151 asection *input_section, bfd *output_bfd, char **error_message));
152 static reloc_howto_type * lookup_howto
153 PARAMS ((unsigned int rtype));
154 static reloc_howto_type *elfNN_ia64_reloc_type_lookup
155 PARAMS ((bfd *abfd, bfd_reloc_code_real_type bfd_code));
156 static void elfNN_ia64_info_to_howto
157 PARAMS ((bfd *abfd, arelent *bfd_reloc, ElfNN_Internal_Rela *elf_reloc));
158 static boolean elfNN_ia64_relax_section
159 PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
160 boolean *again));
161 static boolean elfNN_ia64_section_from_shdr
162 PARAMS ((bfd *, ElfNN_Internal_Shdr *, char *));
163 static boolean elfNN_ia64_fake_sections
164 PARAMS ((bfd *abfd, ElfNN_Internal_Shdr *hdr, asection *sec));
165 static boolean elfNN_ia64_add_symbol_hook
166 PARAMS ((bfd *abfd, struct bfd_link_info *info, const Elf_Internal_Sym *sym,
167 const char **namep, flagword *flagsp, asection **secp,
168 bfd_vma *valp));
169 static int elfNN_ia64_additional_program_headers
170 PARAMS ((bfd *abfd));
171 static boolean elfNN_ia64_is_local_label_name
172 PARAMS ((bfd *abfd, const char *name));
173 static boolean elfNN_ia64_dynamic_symbol_p
174 PARAMS ((struct elf_link_hash_entry *h, struct bfd_link_info *info));
175 static boolean elfNN_ia64_local_hash_table_init
176 PARAMS ((struct elfNN_ia64_local_hash_table *ht, bfd *abfd,
177 new_hash_entry_func new));
178 static struct bfd_hash_entry *elfNN_ia64_new_loc_hash_entry
179 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
180 const char *string));
181 static struct bfd_hash_entry *elfNN_ia64_new_elf_hash_entry
182 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
183 const char *string));
184 static struct bfd_link_hash_table *elfNN_ia64_hash_table_create
185 PARAMS ((bfd *abfd));
186 static struct elfNN_ia64_local_hash_entry *elfNN_ia64_local_hash_lookup
187 PARAMS ((struct elfNN_ia64_local_hash_table *table, const char *string,
188 boolean create, boolean copy));
189 static void elfNN_ia64_dyn_sym_traverse
190 PARAMS ((struct elfNN_ia64_link_hash_table *ia64_info,
191 boolean (*func)(struct elfNN_ia64_dyn_sym_info *, PTR),
192 PTR info));
193 static boolean elfNN_ia64_create_dynamic_sections
194 PARAMS ((bfd *abfd, struct bfd_link_info *info));
195 static struct elfNN_ia64_dyn_sym_info * get_dyn_sym_info
196 PARAMS ((struct elfNN_ia64_link_hash_table *ia64_info,
197 struct elf_link_hash_entry *h,
198 bfd *abfd, const Elf_Internal_Rela *rel, boolean create));
199 static asection *get_got
200 PARAMS ((bfd *abfd, struct bfd_link_info *info,
201 struct elfNN_ia64_link_hash_table *ia64_info));
202 static asection *get_fptr
203 PARAMS ((bfd *abfd, struct bfd_link_info *info,
204 struct elfNN_ia64_link_hash_table *ia64_info));
205 static asection *get_pltoff
206 PARAMS ((bfd *abfd, struct bfd_link_info *info,
207 struct elfNN_ia64_link_hash_table *ia64_info));
208 static asection *get_reloc_section
209 PARAMS ((bfd *abfd, struct elfNN_ia64_link_hash_table *ia64_info,
210 asection *sec, boolean create));
211 static boolean count_dyn_reloc
212 PARAMS ((bfd *abfd, struct elfNN_ia64_dyn_sym_info *dyn_i,
213 asection *srel, int type));
214 static boolean elfNN_ia64_check_relocs
215 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
216 const Elf_Internal_Rela *relocs));
217 static boolean elfNN_ia64_adjust_dynamic_symbol
218 PARAMS ((struct bfd_link_info *info, struct elf_link_hash_entry *h));
219 static unsigned long global_sym_index
220 PARAMS ((struct elf_link_hash_entry *h));
221 static boolean allocate_fptr
222 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
223 static boolean allocate_global_data_got
224 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
225 static boolean allocate_global_fptr_got
226 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
227 static boolean allocate_local_got
228 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
229 static boolean allocate_pltoff_entries
230 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
231 static boolean allocate_plt_entries
232 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
233 static boolean allocate_plt2_entries
234 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
235 static boolean allocate_dynrel_entries
236 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
237 static boolean elfNN_ia64_size_dynamic_sections
238 PARAMS ((bfd *output_bfd, struct bfd_link_info *info));
239 static bfd_reloc_status_type elfNN_ia64_install_value
240 PARAMS ((bfd *abfd, bfd_byte *hit_addr, bfd_vma val, unsigned int r_type));
241 static void elfNN_ia64_install_dyn_reloc
242 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
243 asection *srel, bfd_vma offset, unsigned int type,
244 long dynindx, bfd_vma addend));
245 static bfd_vma set_got_entry
246 PARAMS ((bfd *abfd, struct bfd_link_info *info,
247 struct elfNN_ia64_dyn_sym_info *dyn_i, long dynindx,
248 bfd_vma addend, bfd_vma value, unsigned int dyn_r_type));
249 static bfd_vma set_fptr_entry
250 PARAMS ((bfd *abfd, struct bfd_link_info *info,
251 struct elfNN_ia64_dyn_sym_info *dyn_i,
252 bfd_vma value));
253 static bfd_vma set_pltoff_entry
254 PARAMS ((bfd *abfd, struct bfd_link_info *info,
255 struct elfNN_ia64_dyn_sym_info *dyn_i,
256 bfd_vma value, boolean));
257 static boolean elfNN_ia64_final_link
258 PARAMS ((bfd *abfd, struct bfd_link_info *info));
259 static boolean elfNN_ia64_relocate_section
260 PARAMS ((bfd *output_bfd, struct bfd_link_info *info, bfd *input_bfd,
261 asection *input_section, bfd_byte *contents,
262 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
263 asection **local_sections));
264 static boolean elfNN_ia64_finish_dynamic_symbol
265 PARAMS ((bfd *output_bfd, struct bfd_link_info *info,
266 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
267 static boolean elfNN_ia64_finish_dynamic_sections
268 PARAMS ((bfd *abfd, struct bfd_link_info *info));
269 static boolean elfNN_ia64_set_private_flags
270 PARAMS ((bfd *abfd, flagword flags));
271 static boolean elfNN_ia64_copy_private_bfd_data
272 PARAMS ((bfd *ibfd, bfd *obfd));
273 static boolean elfNN_ia64_merge_private_bfd_data
274 PARAMS ((bfd *ibfd, bfd *obfd));
275 static boolean elfNN_ia64_print_private_bfd_data
276 PARAMS ((bfd *abfd, PTR ptr));
277
278 \f
279 /* ia64-specific relocation */
280
281 /* Perform a relocation. Not much to do here as all the hard work is
282 done in elfNN_ia64_final_link_relocate. */
283 static bfd_reloc_status_type
284 elfNN_ia64_reloc (abfd, reloc, sym, data, input_section,
285 output_bfd, error_message)
286 bfd *abfd;
287 arelent *reloc;
288 asymbol *sym;
289 PTR data;
290 asection *input_section;
291 bfd *output_bfd;
292 char **error_message;
293 {
294 if (output_bfd)
295 {
296 reloc->address += input_section->output_offset;
297 return bfd_reloc_ok;
298 }
299 *error_message = "Unsupported call to elfNN_ia64_reloc";
300 return bfd_reloc_notsupported;
301 }
302
303 #define IA64_HOWTO(TYPE, NAME, SIZE, PCREL, IN) \
304 HOWTO (TYPE, 0, SIZE, 0, PCREL, 0, complain_overflow_signed, \
305 elfNN_ia64_reloc, NAME, false, 0, 0, IN)
306
307 /* This table has to be sorted according to increasing number of the
308 TYPE field. */
309 static reloc_howto_type ia64_howto_table[] =
310 {
311 IA64_HOWTO (R_IA64_NONE, "NONE", 0, false, true),
312
313 IA64_HOWTO (R_IA64_IMM14, "IMM14", 0, false, true),
314 IA64_HOWTO (R_IA64_IMM22, "IMM22", 0, false, true),
315 IA64_HOWTO (R_IA64_IMM64, "IMM64", 0, false, true),
316 IA64_HOWTO (R_IA64_DIR32MSB, "DIR32MSB", 2, false, true),
317 IA64_HOWTO (R_IA64_DIR32LSB, "DIR32LSB", 2, false, true),
318 IA64_HOWTO (R_IA64_DIR64MSB, "DIR64MSB", 4, false, true),
319 IA64_HOWTO (R_IA64_DIR64LSB, "DIR64LSB", 4, false, true),
320
321 IA64_HOWTO (R_IA64_GPREL22, "GPREL22", 0, false, true),
322 IA64_HOWTO (R_IA64_GPREL64I, "GPREL64I", 0, false, true),
323 IA64_HOWTO (R_IA64_GPREL32MSB, "GPREL32MSB", 2, false, true),
324 IA64_HOWTO (R_IA64_GPREL32LSB, "GPREL32LSB", 2, false, true),
325 IA64_HOWTO (R_IA64_GPREL64MSB, "GPREL64MSB", 4, false, true),
326 IA64_HOWTO (R_IA64_GPREL64LSB, "GPREL64LSB", 4, false, true),
327
328 IA64_HOWTO (R_IA64_LTOFF22, "LTOFF22", 0, false, true),
329 IA64_HOWTO (R_IA64_LTOFF64I, "LTOFF64I", 0, false, true),
330
331 IA64_HOWTO (R_IA64_PLTOFF22, "PLTOFF22", 0, false, true),
332 IA64_HOWTO (R_IA64_PLTOFF64I, "PLTOFF64I", 0, false, true),
333 IA64_HOWTO (R_IA64_PLTOFF64MSB, "PLTOFF64MSB", 4, false, true),
334 IA64_HOWTO (R_IA64_PLTOFF64LSB, "PLTOFF64LSB", 4, false, true),
335
336 IA64_HOWTO (R_IA64_FPTR64I, "FPTR64I", 0, false, true),
337 IA64_HOWTO (R_IA64_FPTR32MSB, "FPTR32MSB", 2, false, true),
338 IA64_HOWTO (R_IA64_FPTR32LSB, "FPTR32LSB", 2, false, true),
339 IA64_HOWTO (R_IA64_FPTR64MSB, "FPTR64MSB", 4, false, true),
340 IA64_HOWTO (R_IA64_FPTR64LSB, "FPTR64LSB", 4, false, true),
341
342 IA64_HOWTO (R_IA64_PCREL60B, "PCREL60B", 0, true, true),
343 IA64_HOWTO (R_IA64_PCREL21B, "PCREL21B", 0, true, true),
344 IA64_HOWTO (R_IA64_PCREL21M, "PCREL21M", 0, true, true),
345 IA64_HOWTO (R_IA64_PCREL21F, "PCREL21F", 0, true, true),
346 IA64_HOWTO (R_IA64_PCREL32MSB, "PCREL32MSB", 2, true, true),
347 IA64_HOWTO (R_IA64_PCREL32LSB, "PCREL32LSB", 2, true, true),
348 IA64_HOWTO (R_IA64_PCREL64MSB, "PCREL64MSB", 4, true, true),
349 IA64_HOWTO (R_IA64_PCREL64LSB, "PCREL64LSB", 4, true, true),
350
351 IA64_HOWTO (R_IA64_LTOFF_FPTR22, "LTOFF_FPTR22", 0, false, true),
352 IA64_HOWTO (R_IA64_LTOFF_FPTR64I, "LTOFF_FPTR64I", 0, false, true),
353 IA64_HOWTO (R_IA64_LTOFF_FPTR64MSB, "LTOFF_FPTR64MSB", 4, false, true),
354 IA64_HOWTO (R_IA64_LTOFF_FPTR64LSB, "LTOFF_FPTR64LSB", 4, false, true),
355
356 IA64_HOWTO (R_IA64_SEGREL32MSB, "SEGREL32MSB", 2, false, true),
357 IA64_HOWTO (R_IA64_SEGREL32LSB, "SEGREL32LSB", 2, false, true),
358 IA64_HOWTO (R_IA64_SEGREL64MSB, "SEGREL64MSB", 4, false, true),
359 IA64_HOWTO (R_IA64_SEGREL64LSB, "SEGREL64LSB", 4, false, true),
360
361 IA64_HOWTO (R_IA64_SECREL32MSB, "SECREL32MSB", 2, false, true),
362 IA64_HOWTO (R_IA64_SECREL32LSB, "SECREL32LSB", 2, false, true),
363 IA64_HOWTO (R_IA64_SECREL64MSB, "SECREL64MSB", 4, false, true),
364 IA64_HOWTO (R_IA64_SECREL64LSB, "SECREL64LSB", 4, false, true),
365
366 IA64_HOWTO (R_IA64_REL32MSB, "REL32MSB", 2, false, true),
367 IA64_HOWTO (R_IA64_REL32LSB, "REL32LSB", 2, false, true),
368 IA64_HOWTO (R_IA64_REL64MSB, "REL64MSB", 4, false, true),
369 IA64_HOWTO (R_IA64_REL64LSB, "REL64LSB", 4, false, true),
370
371 IA64_HOWTO (R_IA64_LTV32MSB, "LTV32MSB", 2, false, true),
372 IA64_HOWTO (R_IA64_LTV32LSB, "LTV32LSB", 2, false, true),
373 IA64_HOWTO (R_IA64_LTV64MSB, "LTV64MSB", 4, false, true),
374 IA64_HOWTO (R_IA64_LTV64LSB, "LTV64LSB", 4, false, true),
375
376 IA64_HOWTO (R_IA64_PCREL21BI, "PCREL21BI", 0, true, true),
377 IA64_HOWTO (R_IA64_PCREL22, "PCREL22", 0, true, true),
378 IA64_HOWTO (R_IA64_PCREL64I, "PCREL64I", 0, true, true),
379
380 IA64_HOWTO (R_IA64_IPLTMSB, "IPLTMSB", 4, false, true),
381 IA64_HOWTO (R_IA64_IPLTLSB, "IPLTLSB", 4, false, true),
382 IA64_HOWTO (R_IA64_COPY, "COPY", 4, false, true),
383 IA64_HOWTO (R_IA64_LTOFF22X, "LTOFF22X", 0, false, true),
384 IA64_HOWTO (R_IA64_LDXMOV, "LDXMOV", 0, false, true),
385
386 IA64_HOWTO (R_IA64_TPREL22, "TPREL22", 0, false, false),
387 IA64_HOWTO (R_IA64_TPREL64MSB, "TPREL64MSB", 8, false, false),
388 IA64_HOWTO (R_IA64_TPREL64LSB, "TPREL64LSB", 8, false, false),
389 IA64_HOWTO (R_IA64_LTOFF_TP22, "LTOFF_TP22", 0, false, false),
390 };
391
392 static unsigned char elf_code_to_howto_index[R_IA64_MAX_RELOC_CODE + 1];
393
394 /* Given a BFD reloc type, return the matching HOWTO structure. */
395
396 static reloc_howto_type*
397 lookup_howto (rtype)
398 unsigned int rtype;
399 {
400 static int inited = 0;
401 int i;
402
403 if (!inited)
404 {
405 inited = 1;
406
407 memset (elf_code_to_howto_index, 0xff, sizeof (elf_code_to_howto_index));
408 for (i = 0; i < NELEMS (ia64_howto_table); ++i)
409 elf_code_to_howto_index[ia64_howto_table[i].type] = i;
410 }
411
412 BFD_ASSERT (rtype <= R_IA64_MAX_RELOC_CODE);
413 i = elf_code_to_howto_index[rtype];
414 if (i >= NELEMS (ia64_howto_table))
415 return 0;
416 return ia64_howto_table + i;
417 }
418
419 static reloc_howto_type*
420 elfNN_ia64_reloc_type_lookup (abfd, bfd_code)
421 bfd *abfd;
422 bfd_reloc_code_real_type bfd_code;
423 {
424 unsigned int rtype;
425
426 switch (bfd_code)
427 {
428 case BFD_RELOC_NONE: rtype = R_IA64_NONE; break;
429
430 case BFD_RELOC_IA64_IMM14: rtype = R_IA64_IMM14; break;
431 case BFD_RELOC_IA64_IMM22: rtype = R_IA64_IMM22; break;
432 case BFD_RELOC_IA64_IMM64: rtype = R_IA64_IMM64; break;
433
434 case BFD_RELOC_IA64_DIR32MSB: rtype = R_IA64_DIR32MSB; break;
435 case BFD_RELOC_IA64_DIR32LSB: rtype = R_IA64_DIR32LSB; break;
436 case BFD_RELOC_IA64_DIR64MSB: rtype = R_IA64_DIR64MSB; break;
437 case BFD_RELOC_IA64_DIR64LSB: rtype = R_IA64_DIR64LSB; break;
438
439 case BFD_RELOC_IA64_GPREL22: rtype = R_IA64_GPREL22; break;
440 case BFD_RELOC_IA64_GPREL64I: rtype = R_IA64_GPREL64I; break;
441 case BFD_RELOC_IA64_GPREL32MSB: rtype = R_IA64_GPREL32MSB; break;
442 case BFD_RELOC_IA64_GPREL32LSB: rtype = R_IA64_GPREL32LSB; break;
443 case BFD_RELOC_IA64_GPREL64MSB: rtype = R_IA64_GPREL64MSB; break;
444 case BFD_RELOC_IA64_GPREL64LSB: rtype = R_IA64_GPREL64LSB; break;
445
446 case BFD_RELOC_IA64_LTOFF22: rtype = R_IA64_LTOFF22; break;
447 case BFD_RELOC_IA64_LTOFF64I: rtype = R_IA64_LTOFF64I; break;
448
449 case BFD_RELOC_IA64_PLTOFF22: rtype = R_IA64_PLTOFF22; break;
450 case BFD_RELOC_IA64_PLTOFF64I: rtype = R_IA64_PLTOFF64I; break;
451 case BFD_RELOC_IA64_PLTOFF64MSB: rtype = R_IA64_PLTOFF64MSB; break;
452 case BFD_RELOC_IA64_PLTOFF64LSB: rtype = R_IA64_PLTOFF64LSB; break;
453 case BFD_RELOC_IA64_FPTR64I: rtype = R_IA64_FPTR64I; break;
454 case BFD_RELOC_IA64_FPTR32MSB: rtype = R_IA64_FPTR32MSB; break;
455 case BFD_RELOC_IA64_FPTR32LSB: rtype = R_IA64_FPTR32LSB; break;
456 case BFD_RELOC_IA64_FPTR64MSB: rtype = R_IA64_FPTR64MSB; break;
457 case BFD_RELOC_IA64_FPTR64LSB: rtype = R_IA64_FPTR64LSB; break;
458
459 case BFD_RELOC_IA64_PCREL21B: rtype = R_IA64_PCREL21B; break;
460 case BFD_RELOC_IA64_PCREL21BI: rtype = R_IA64_PCREL21BI; break;
461 case BFD_RELOC_IA64_PCREL21M: rtype = R_IA64_PCREL21M; break;
462 case BFD_RELOC_IA64_PCREL21F: rtype = R_IA64_PCREL21F; break;
463 case BFD_RELOC_IA64_PCREL22: rtype = R_IA64_PCREL22; break;
464 case BFD_RELOC_IA64_PCREL60B: rtype = R_IA64_PCREL60B; break;
465 case BFD_RELOC_IA64_PCREL64I: rtype = R_IA64_PCREL64I; break;
466 case BFD_RELOC_IA64_PCREL32MSB: rtype = R_IA64_PCREL32MSB; break;
467 case BFD_RELOC_IA64_PCREL32LSB: rtype = R_IA64_PCREL32LSB; break;
468 case BFD_RELOC_IA64_PCREL64MSB: rtype = R_IA64_PCREL64MSB; break;
469 case BFD_RELOC_IA64_PCREL64LSB: rtype = R_IA64_PCREL64LSB; break;
470
471 case BFD_RELOC_IA64_LTOFF_FPTR22: rtype = R_IA64_LTOFF_FPTR22; break;
472 case BFD_RELOC_IA64_LTOFF_FPTR64I: rtype = R_IA64_LTOFF_FPTR64I; break;
473 case BFD_RELOC_IA64_LTOFF_FPTR64MSB: rtype = R_IA64_LTOFF_FPTR64MSB; break;
474 case BFD_RELOC_IA64_LTOFF_FPTR64LSB: rtype = R_IA64_LTOFF_FPTR64LSB; break;
475
476 case BFD_RELOC_IA64_SEGREL32MSB: rtype = R_IA64_SEGREL32MSB; break;
477 case BFD_RELOC_IA64_SEGREL32LSB: rtype = R_IA64_SEGREL32LSB; break;
478 case BFD_RELOC_IA64_SEGREL64MSB: rtype = R_IA64_SEGREL64MSB; break;
479 case BFD_RELOC_IA64_SEGREL64LSB: rtype = R_IA64_SEGREL64LSB; break;
480
481 case BFD_RELOC_IA64_SECREL32MSB: rtype = R_IA64_SECREL32MSB; break;
482 case BFD_RELOC_IA64_SECREL32LSB: rtype = R_IA64_SECREL32LSB; break;
483 case BFD_RELOC_IA64_SECREL64MSB: rtype = R_IA64_SECREL64MSB; break;
484 case BFD_RELOC_IA64_SECREL64LSB: rtype = R_IA64_SECREL64LSB; break;
485
486 case BFD_RELOC_IA64_REL32MSB: rtype = R_IA64_REL32MSB; break;
487 case BFD_RELOC_IA64_REL32LSB: rtype = R_IA64_REL32LSB; break;
488 case BFD_RELOC_IA64_REL64MSB: rtype = R_IA64_REL64MSB; break;
489 case BFD_RELOC_IA64_REL64LSB: rtype = R_IA64_REL64LSB; break;
490
491 case BFD_RELOC_IA64_LTV32MSB: rtype = R_IA64_LTV32MSB; break;
492 case BFD_RELOC_IA64_LTV32LSB: rtype = R_IA64_LTV32LSB; break;
493 case BFD_RELOC_IA64_LTV64MSB: rtype = R_IA64_LTV64MSB; break;
494 case BFD_RELOC_IA64_LTV64LSB: rtype = R_IA64_LTV64LSB; break;
495
496 case BFD_RELOC_IA64_IPLTMSB: rtype = R_IA64_IPLTMSB; break;
497 case BFD_RELOC_IA64_IPLTLSB: rtype = R_IA64_IPLTLSB; break;
498 case BFD_RELOC_IA64_COPY: rtype = R_IA64_COPY; break;
499 case BFD_RELOC_IA64_LTOFF22X: rtype = R_IA64_LTOFF22X; break;
500 case BFD_RELOC_IA64_LDXMOV: rtype = R_IA64_LDXMOV; break;
501
502 case BFD_RELOC_IA64_TPREL22: rtype = R_IA64_TPREL22; break;
503 case BFD_RELOC_IA64_TPREL64MSB: rtype = R_IA64_TPREL64MSB; break;
504 case BFD_RELOC_IA64_TPREL64LSB: rtype = R_IA64_TPREL64LSB; break;
505 case BFD_RELOC_IA64_LTOFF_TP22: rtype = R_IA64_LTOFF_TP22; break;
506
507 default: return 0;
508 }
509 return lookup_howto (rtype);
510 }
511
512 /* Given a ELF reloc, return the matching HOWTO structure. */
513
514 static void
515 elfNN_ia64_info_to_howto (abfd, bfd_reloc, elf_reloc)
516 bfd *abfd;
517 arelent *bfd_reloc;
518 ElfNN_Internal_Rela *elf_reloc;
519 {
520 bfd_reloc->howto = lookup_howto (ELFNN_R_TYPE (elf_reloc->r_info));
521 }
522 \f
523 #define PLT_HEADER_SIZE (3 * 16)
524 #define PLT_MIN_ENTRY_SIZE (1 * 16)
525 #define PLT_FULL_ENTRY_SIZE (2 * 16)
526 #define PLT_RESERVED_WORDS 3
527
528 static const bfd_byte plt_header[PLT_HEADER_SIZE] =
529 {
530 0x0b, 0x10, 0x00, 0x1c, 0x00, 0x21, /* [MMI] mov r2=r14;; */
531 0xe0, 0x00, 0x08, 0x00, 0x48, 0x00, /* addl r14=0,r2 */
532 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
533 0x0b, 0x80, 0x20, 0x1c, 0x18, 0x14, /* [MMI] ld8 r16=[r14],8;; */
534 0x10, 0x41, 0x38, 0x30, 0x28, 0x00, /* ld8 r17=[r14],8 */
535 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
536 0x11, 0x08, 0x00, 0x1c, 0x18, 0x10, /* [MIB] ld8 r1=[r14] */
537 0x60, 0x88, 0x04, 0x80, 0x03, 0x00, /* mov b6=r17 */
538 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
539 };
540
541 static const bfd_byte plt_min_entry[PLT_MIN_ENTRY_SIZE] =
542 {
543 0x11, 0x78, 0x00, 0x00, 0x00, 0x24, /* [MIB] mov r15=0 */
544 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, /* nop.i 0x0 */
545 0x00, 0x00, 0x00, 0x40 /* br.few 0 <PLT0>;; */
546 };
547
548 static const bfd_byte plt_full_entry[PLT_FULL_ENTRY_SIZE] =
549 {
550 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
551 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
552 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
553 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
554 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
555 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
556 };
557
558 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
559
560 /* Select out of range branch fixup type. Note that Itanium does
561 not support brl, and so it gets emulated by the kernel. */
562 #undef USE_BRL
563
564 static const bfd_byte oor_brl[16] =
565 {
566 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
567 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* brl.sptk.few tgt;; */
568 0x00, 0x00, 0x00, 0xc0
569 };
570
571 static const bfd_byte oor_ip[48] =
572 {
573 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
574 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, /* movl r15=0 */
575 0x01, 0x00, 0x00, 0x60,
576 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MII] nop.m 0 */
577 0x00, 0x01, 0x00, 0x60, 0x00, 0x00, /* mov r16=ip;; */
578 0xf2, 0x80, 0x00, 0x80, /* add r16=r15,r16;; */
579 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MIB] nop.m 0 */
580 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
581 0x60, 0x00, 0x80, 0x00 /* br b6;; */
582 };
583 \f
584 /* These functions do relaxation for IA-64 ELF.
585
586 This is primarily to support branches to targets out of range;
587 relaxation of R_IA64_LTOFF22X and R_IA64_LDXMOV not yet supported. */
588
589 static boolean
590 elfNN_ia64_relax_section (abfd, sec, link_info, again)
591 bfd *abfd;
592 asection *sec;
593 struct bfd_link_info *link_info;
594 boolean *again;
595 {
596 struct one_fixup
597 {
598 struct one_fixup *next;
599 asection *tsec;
600 bfd_vma toff;
601 bfd_vma trampoff;
602 };
603
604 Elf_Internal_Shdr *symtab_hdr;
605 Elf_Internal_Rela *internal_relocs;
606 Elf_Internal_Rela *free_relocs;
607 Elf_Internal_Rela *irel, *irelend;
608 bfd_byte *contents;
609 bfd_byte *free_contents;
610 ElfNN_External_Sym *extsyms;
611 ElfNN_External_Sym *free_extsyms;
612 struct elfNN_ia64_link_hash_table *ia64_info;
613 struct one_fixup *fixups = NULL;
614 boolean changed_contents = false;
615 boolean changed_relocs = false;
616
617 /* Assume we're not going to change any sizes, and we'll only need
618 one pass. */
619 *again = false;
620
621 /* Nothing to do if there are no relocations. */
622 if ((sec->flags & SEC_RELOC) == 0
623 || sec->reloc_count == 0)
624 return true;
625
626 /* If this is the first time we have been called for this section,
627 initialize the cooked size. */
628 if (sec->_cooked_size == 0)
629 sec->_cooked_size = sec->_raw_size;
630
631 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
632
633 /* Load the relocations for this section. */
634 internal_relocs = (_bfd_elfNN_link_read_relocs
635 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
636 link_info->keep_memory));
637 if (internal_relocs == NULL)
638 goto error_return;
639 free_relocs = NULL;
640 if (! link_info->keep_memory)
641 free_relocs = internal_relocs;
642
643 ia64_info = elfNN_ia64_hash_table (link_info);
644 irelend = internal_relocs + sec->reloc_count;
645
646 for (irel = internal_relocs; irel < irelend; irel++)
647 if (ELFNN_R_TYPE (irel->r_info) == (int) R_IA64_PCREL21B)
648 break;
649
650 /* No branch-type relocations. */
651 if (irel == irelend)
652 {
653 if (free_relocs != NULL)
654 free (free_relocs);
655 return true;
656 }
657
658 /* Get the section contents. */
659 free_contents = NULL;
660 if (elf_section_data (sec)->this_hdr.contents != NULL)
661 contents = elf_section_data (sec)->this_hdr.contents;
662 else
663 {
664 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
665 if (contents == NULL)
666 goto error_return;
667 free_contents = contents;
668
669 if (! bfd_get_section_contents (abfd, sec, contents,
670 (file_ptr) 0, sec->_raw_size))
671 goto error_return;
672 }
673
674 /* Read this BFD's symbols. */
675 free_extsyms = NULL;
676 if (symtab_hdr->contents != NULL)
677 extsyms = (ElfNN_External_Sym *) symtab_hdr->contents;
678 else
679 {
680 extsyms = (ElfNN_External_Sym *) bfd_malloc (symtab_hdr->sh_size);
681 if (extsyms == NULL)
682 goto error_return;
683 free_extsyms = extsyms;
684 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
685 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
686 != symtab_hdr->sh_size))
687 goto error_return;
688 }
689
690 for (; irel < irelend; irel++)
691 {
692 bfd_vma symaddr, reladdr, trampoff, toff, roff;
693 Elf_Internal_Sym isym;
694 asection *tsec;
695 struct one_fixup *f;
696
697 if (ELFNN_R_TYPE (irel->r_info) != (int) R_IA64_PCREL21B)
698 continue;
699
700 /* Get the value of the symbol referred to by the reloc. */
701 if (ELFNN_R_SYM (irel->r_info) < symtab_hdr->sh_info)
702 {
703 /* A local symbol. */
704 bfd_elfNN_swap_symbol_in (abfd,
705 extsyms + ELFNN_R_SYM (irel->r_info),
706 &isym);
707 if (isym.st_shndx == SHN_UNDEF)
708 continue; /* We can't do anthing with undefined symbols. */
709 else if (isym.st_shndx == SHN_ABS)
710 tsec = bfd_abs_section_ptr;
711 else if (isym.st_shndx == SHN_COMMON)
712 tsec = bfd_com_section_ptr;
713 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
714 tsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
715 else
716 continue; /* who knows. */
717
718 toff = isym.st_value;
719 }
720 else
721 {
722 unsigned long indx;
723 struct elf_link_hash_entry *h;
724 struct elfNN_ia64_dyn_sym_info *dyn_i;
725
726 indx = ELFNN_R_SYM (irel->r_info) - symtab_hdr->sh_info;
727 h = elf_sym_hashes (abfd)[indx];
728 BFD_ASSERT (h != NULL);
729
730 while (h->root.type == bfd_link_hash_indirect
731 || h->root.type == bfd_link_hash_warning)
732 h = (struct elf_link_hash_entry *) h->root.u.i.link;
733
734 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, false);
735
736 /* For branches to dynamic symbols, we're interested instead
737 in a branch to the PLT entry. */
738 if (dyn_i && dyn_i->want_plt2)
739 {
740 tsec = ia64_info->plt_sec;
741 toff = dyn_i->plt2_offset;
742 }
743 else
744 {
745 /* We can't do anthing with undefined symbols. */
746 if (h->root.type == bfd_link_hash_undefined
747 || h->root.type == bfd_link_hash_undefweak)
748 continue;
749
750 tsec = h->root.u.def.section;
751 toff = h->root.u.def.value;
752 }
753 }
754
755 symaddr = (tsec->output_section->vma
756 + tsec->output_offset
757 + toff
758 + irel->r_addend);
759
760 roff = irel->r_offset;
761 reladdr = (sec->output_section->vma
762 + sec->output_offset
763 + roff) & -4;
764
765 /* If the branch is in range, no need to do anything. */
766 if ((bfd_signed_vma) (symaddr - reladdr) >= -0x1000000
767 && (bfd_signed_vma) (symaddr - reladdr) <= 0x0FFFFF0)
768 continue;
769
770 /* If the branch and target are in the same section, you've
771 got one honking big section and we can't help you. You'll
772 get an error message later. */
773 if (tsec == sec)
774 continue;
775
776 /* Look for an existing fixup to this address. */
777 for (f = fixups; f ; f = f->next)
778 if (f->tsec == tsec && f->toff == toff)
779 break;
780
781 if (f == NULL)
782 {
783 /* Two alternatives: If it's a branch to a PLT entry, we can
784 make a copy of the FULL_PLT entry. Otherwise, we'll have
785 to use a `brl' insn to get where we're going. */
786
787 int size;
788
789 if (tsec == ia64_info->plt_sec)
790 size = sizeof (plt_full_entry);
791 else
792 {
793 #ifdef USE_BRL
794 size = sizeof (oor_brl);
795 #else
796 size = sizeof (oor_ip);
797 #endif
798 }
799
800 /* Resize the current section to make room for the new branch. */
801 trampoff = (sec->_cooked_size + 15) & -16;
802 contents = (bfd_byte *) bfd_realloc (contents, trampoff + size);
803 if (contents == NULL)
804 goto error_return;
805 sec->_cooked_size = trampoff + size;
806
807 if (tsec == ia64_info->plt_sec)
808 {
809 memcpy (contents + trampoff, plt_full_entry, size);
810
811 /* Hijack the old relocation for use as the PLTOFF reloc. */
812 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
813 R_IA64_PLTOFF22);
814 irel->r_offset = trampoff;
815 }
816 else
817 {
818 #ifdef USE_BRL
819 memcpy (contents + trampoff, oor_brl, size);
820 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
821 R_IA64_PCREL60B);
822 irel->r_offset = trampoff + 2;
823 #else
824 memcpy (contents + trampoff, oor_ip, size);
825 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
826 R_IA64_PCREL64I);
827 irel->r_addend -= 16;
828 irel->r_offset = trampoff + 2;
829 #endif
830 }
831
832 /* Record the fixup so we don't do it again this section. */
833 f = (struct one_fixup *) bfd_malloc (sizeof (*f));
834 f->next = fixups;
835 f->tsec = tsec;
836 f->toff = toff;
837 f->trampoff = trampoff;
838 fixups = f;
839 }
840 else
841 {
842 /* Nop out the reloc, since we're finalizing things here. */
843 irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
844 }
845
846 /* Fix up the existing branch to hit the trampoline. Hope like
847 hell this doesn't overflow too. */
848 if (elfNN_ia64_install_value (abfd, contents + roff,
849 f->trampoff - (roff & -4),
850 R_IA64_PCREL21B) != bfd_reloc_ok)
851 goto error_return;
852
853 changed_contents = true;
854 changed_relocs = true;
855 }
856
857 /* Clean up and go home. */
858 while (fixups)
859 {
860 struct one_fixup *f = fixups;
861 fixups = fixups->next;
862 free (f);
863 }
864
865 if (changed_relocs)
866 elf_section_data (sec)->relocs = internal_relocs;
867 else if (free_relocs != NULL)
868 free (free_relocs);
869
870 if (changed_contents)
871 elf_section_data (sec)->this_hdr.contents = contents;
872 else if (free_contents != NULL)
873 {
874 if (! link_info->keep_memory)
875 free (free_contents);
876 else
877 {
878 /* Cache the section contents for elf_link_input_bfd. */
879 elf_section_data (sec)->this_hdr.contents = contents;
880 }
881 }
882
883 if (free_extsyms != NULL)
884 {
885 if (! link_info->keep_memory)
886 free (free_extsyms);
887 else
888 {
889 /* Cache the symbols for elf_link_input_bfd. */
890 symtab_hdr->contents = extsyms;
891 }
892 }
893
894 *again = changed_contents || changed_relocs;
895 return true;
896
897 error_return:
898 if (free_relocs != NULL)
899 free (free_relocs);
900 if (free_contents != NULL)
901 free (free_contents);
902 if (free_extsyms != NULL)
903 free (free_extsyms);
904 return false;
905 }
906 \f
907 /* Handle an IA-64 specific section when reading an object file. This
908 is called when elfcode.h finds a section with an unknown type. */
909
910 static boolean
911 elfNN_ia64_section_from_shdr (abfd, hdr, name)
912 bfd *abfd;
913 ElfNN_Internal_Shdr *hdr;
914 char *name;
915 {
916 asection *newsect;
917
918 /* There ought to be a place to keep ELF backend specific flags, but
919 at the moment there isn't one. We just keep track of the
920 sections by their name, instead. Fortunately, the ABI gives
921 suggested names for all the MIPS specific sections, so we will
922 probably get away with this. */
923 switch (hdr->sh_type)
924 {
925 case SHT_IA_64_UNWIND:
926 if (strcmp (name, ELF_STRING_ia64_unwind) != 0)
927 return false;
928 break;
929
930 case SHT_IA_64_EXT:
931 if (strcmp (name, ELF_STRING_ia64_archext) != 0)
932 return false;
933 break;
934
935 default:
936 return false;
937 }
938
939 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
940 return false;
941 newsect = hdr->bfd_section;
942
943 return true;
944 }
945
946 /* Convert IA-64 specific section flags to bfd internal section flags. */
947
948 /* ??? There is no bfd internal flag equivalent to the SHF_IA_64_NORECOV
949 flag. */
950
951 static boolean
952 elfNN_ia64_section_flags (flags, hdr)
953 flagword *flags;
954 ElfNN_Internal_Shdr *hdr;
955 {
956 if (hdr->sh_flags & SHF_IA_64_SHORT)
957 *flags |= SEC_SMALL_DATA;
958
959 return true;
960 }
961
962 /* Set the correct type for an IA-64 ELF section. We do this by the
963 section name, which is a hack, but ought to work. */
964
965 static boolean
966 elfNN_ia64_fake_sections (abfd, hdr, sec)
967 bfd *abfd;
968 ElfNN_Internal_Shdr *hdr;
969 asection *sec;
970 {
971 register const char *name;
972
973 name = bfd_get_section_name (abfd, sec);
974
975 if (strcmp (name, ELF_STRING_ia64_unwind) == 0)
976 hdr->sh_type = SHT_IA_64_UNWIND;
977 else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
978 hdr->sh_type = SHT_IA_64_EXT;
979 else if (strcmp (name, ".reloc") == 0)
980 /*
981 * This is an ugly, but unfortunately necessary hack that is
982 * needed when producing EFI binaries on IA-64. It tells
983 * elf.c:elf_fake_sections() not to consider ".reloc" as a section
984 * containing ELF relocation info. We need this hack in order to
985 * be able to generate ELF binaries that can be translated into
986 * EFI applications (which are essentially COFF objects). Those
987 * files contain a COFF ".reloc" section inside an ELFNN object,
988 * which would normally cause BFD to segfault because it would
989 * attempt to interpret this section as containing relocation
990 * entries for section "oc". With this hack enabled, ".reloc"
991 * will be treated as a normal data section, which will avoid the
992 * segfault. However, you won't be able to create an ELFNN binary
993 * with a section named "oc" that needs relocations, but that's
994 * the kind of ugly side-effects you get when detecting section
995 * types based on their names... In practice, this limitation is
996 * unlikely to bite.
997 */
998 hdr->sh_type = SHT_PROGBITS;
999
1000 if (sec->flags & SEC_SMALL_DATA)
1001 hdr->sh_flags |= SHF_IA_64_SHORT;
1002
1003 return true;
1004 }
1005
1006 /* Hook called by the linker routine which adds symbols from an object
1007 file. We use it to put .comm items in .sbss, and not .bss. */
1008
1009 static boolean
1010 elfNN_ia64_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1011 bfd *abfd;
1012 struct bfd_link_info *info;
1013 const Elf_Internal_Sym *sym;
1014 const char **namep;
1015 flagword *flagsp;
1016 asection **secp;
1017 bfd_vma *valp;
1018 {
1019 if (sym->st_shndx == SHN_COMMON
1020 && !info->relocateable
1021 && sym->st_size <= bfd_get_gp_size (abfd))
1022 {
1023 /* Common symbols less than or equal to -G nn bytes are
1024 automatically put into .sbss. */
1025
1026 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1027
1028 if (scomm == NULL)
1029 {
1030 scomm = bfd_make_section (abfd, ".scommon");
1031 if (scomm == NULL
1032 || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
1033 | SEC_IS_COMMON
1034 | SEC_LINKER_CREATED)))
1035 return false;
1036 }
1037
1038 *secp = scomm;
1039 *valp = sym->st_size;
1040 }
1041
1042 return true;
1043 }
1044
1045 /* Return the number of additional phdrs we will need. */
1046
1047 static int
1048 elfNN_ia64_additional_program_headers (abfd)
1049 bfd *abfd;
1050 {
1051 asection *s;
1052 int ret = 0;
1053
1054 /* See if we need a PT_IA_64_ARCHEXT segment. */
1055 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1056 if (s && (s->flags & SEC_LOAD))
1057 ++ret;
1058
1059 /* See if we need a PT_IA_64_UNWIND segment. */
1060 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
1061 if (s && (s->flags & SEC_LOAD))
1062 ++ret;
1063
1064 return ret;
1065 }
1066
1067 static boolean
1068 elfNN_ia64_modify_segment_map (abfd)
1069 bfd *abfd;
1070 {
1071 struct elf_segment_map *m, **pm;
1072 asection *s;
1073
1074 /* If we need a PT_IA_64_ARCHEXT segment, it must come before
1075 all PT_LOAD segments. */
1076 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1077 if (s && (s->flags & SEC_LOAD))
1078 {
1079 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1080 if (m->p_type == PT_IA_64_ARCHEXT)
1081 break;
1082 if (m == NULL)
1083 {
1084 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
1085 if (m == NULL)
1086 return false;
1087
1088 m->p_type = PT_IA_64_ARCHEXT;
1089 m->count = 1;
1090 m->sections[0] = s;
1091
1092 /* We want to put it after the PHDR and INTERP segments. */
1093 pm = &elf_tdata (abfd)->segment_map;
1094 while (*pm != NULL
1095 && ((*pm)->p_type == PT_PHDR
1096 || (*pm)->p_type == PT_INTERP))
1097 pm = &(*pm)->next;
1098
1099 m->next = *pm;
1100 *pm = m;
1101 }
1102 }
1103
1104 /* Install the PT_IA_64_UNWIND segment, if needed. */
1105 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
1106 if (s && (s->flags & SEC_LOAD))
1107 {
1108 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1109 if (m->p_type == PT_IA_64_UNWIND)
1110 break;
1111 if (m == NULL)
1112 {
1113 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
1114 if (m == NULL)
1115 return false;
1116
1117 m->p_type = PT_IA_64_UNWIND;
1118 m->count = 1;
1119 m->sections[0] = s;
1120 m->next = NULL;
1121
1122 /* We want to put it last. */
1123 pm = &elf_tdata (abfd)->segment_map;
1124 while (*pm != NULL)
1125 pm = &(*pm)->next;
1126 *pm = m;
1127 }
1128 }
1129
1130 /* Turn on PF_IA_64_NORECOV if needed. This involves traversing all of
1131 the input sections for each output section in the segment and testing
1132 for SHF_IA_64_NORECOV on each. */
1133 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1134 if (m->p_type == PT_LOAD)
1135 {
1136 int i;
1137 for (i = m->count - 1; i >= 0; --i)
1138 {
1139 struct bfd_link_order *order = m->sections[i]->link_order_head;
1140 while (order)
1141 {
1142 if (order->type == bfd_indirect_link_order)
1143 {
1144 asection *is = order->u.indirect.section;
1145 bfd_vma flags = elf_section_data(is)->this_hdr.sh_flags;
1146 if (flags & SHF_IA_64_NORECOV)
1147 {
1148 m->p_flags |= PF_IA_64_NORECOV;
1149 goto found;
1150 }
1151 }
1152 order = order->next;
1153 }
1154 }
1155 found:;
1156 }
1157
1158 return true;
1159 }
1160
1161
1162 /* According to the Tahoe assembler spec, all labels starting with a
1163 '.' are local. */
1164
1165 static boolean
1166 elfNN_ia64_is_local_label_name (abfd, name)
1167 bfd *abfd;
1168 const char *name;
1169 {
1170 return name[0] == '.';
1171 }
1172
1173 /* Should we do dynamic things to this symbol? */
1174
1175 static boolean
1176 elfNN_ia64_dynamic_symbol_p (h, info)
1177 struct elf_link_hash_entry *h;
1178 struct bfd_link_info *info;
1179 {
1180 if (h == NULL)
1181 return false;
1182
1183 while (h->root.type == bfd_link_hash_indirect
1184 || h->root.type == bfd_link_hash_warning)
1185 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1186
1187 if (h->dynindx == -1)
1188 return false;
1189
1190 if (h->root.type == bfd_link_hash_undefweak
1191 || h->root.type == bfd_link_hash_defweak)
1192 return true;
1193
1194 if ((info->shared && !info->symbolic)
1195 || ((h->elf_link_hash_flags
1196 & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))
1197 == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
1198 return true;
1199
1200 return false;
1201 }
1202 \f
1203 static boolean
1204 elfNN_ia64_local_hash_table_init (ht, abfd, new)
1205 struct elfNN_ia64_local_hash_table *ht;
1206 bfd *abfd;
1207 new_hash_entry_func new;
1208 {
1209 memset (ht, 0, sizeof(*ht));
1210 return bfd_hash_table_init (&ht->root, new);
1211 }
1212
1213 static struct bfd_hash_entry*
1214 elfNN_ia64_new_loc_hash_entry (entry, table, string)
1215 struct bfd_hash_entry *entry;
1216 struct bfd_hash_table *table;
1217 const char *string;
1218 {
1219 struct elfNN_ia64_local_hash_entry *ret;
1220 ret = (struct elfNN_ia64_local_hash_entry *) entry;
1221
1222 /* Allocate the structure if it has not already been allocated by a
1223 subclass. */
1224 if (!ret)
1225 ret = bfd_hash_allocate (table, sizeof (*ret));
1226
1227 if (!ret)
1228 return 0;
1229
1230 /* Initialize our local data. All zeros, and definitely easier
1231 than setting a handful of bit fields. */
1232 memset (ret, 0, sizeof(*ret));
1233
1234 /* Call the allocation method of the superclass. */
1235 ret = ((struct elfNN_ia64_local_hash_entry *)
1236 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
1237
1238 return (struct bfd_hash_entry *) ret;
1239 }
1240
1241 static struct bfd_hash_entry*
1242 elfNN_ia64_new_elf_hash_entry (entry, table, string)
1243 struct bfd_hash_entry *entry;
1244 struct bfd_hash_table *table;
1245 const char *string;
1246 {
1247 struct elfNN_ia64_link_hash_entry *ret;
1248 ret = (struct elfNN_ia64_link_hash_entry *) entry;
1249
1250 /* Allocate the structure if it has not already been allocated by a
1251 subclass. */
1252 if (!ret)
1253 ret = bfd_hash_allocate (table, sizeof (*ret));
1254
1255 if (!ret)
1256 return 0;
1257
1258 /* Initialize our local data. All zeros, and definitely easier
1259 than setting a handful of bit fields. */
1260 memset (ret, 0, sizeof(*ret));
1261
1262 /* Call the allocation method of the superclass. */
1263 ret = ((struct elfNN_ia64_link_hash_entry *)
1264 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1265 table, string));
1266
1267 return (struct bfd_hash_entry *) ret;
1268 }
1269
1270 static void
1271 elfNN_ia64_hash_copy_indirect (xdir, xind)
1272 struct elf_link_hash_entry *xdir, *xind;
1273 {
1274 struct elfNN_ia64_link_hash_entry *dir, *ind;
1275
1276 dir = (struct elfNN_ia64_link_hash_entry *)xdir;
1277 ind = (struct elfNN_ia64_link_hash_entry *)xind;
1278
1279 /* Copy down any references that we may have already seen to the
1280 symbol which just became indirect. */
1281
1282 dir->root.elf_link_hash_flags |=
1283 (ind->root.elf_link_hash_flags
1284 & (ELF_LINK_HASH_REF_DYNAMIC
1285 | ELF_LINK_HASH_REF_REGULAR
1286 | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
1287
1288 /* Copy over the got and plt data. This would have been done
1289 by check_relocs. */
1290
1291 if (dir->info == NULL)
1292 {
1293 struct elfNN_ia64_dyn_sym_info *dyn_i;
1294
1295 dir->info = dyn_i = ind->info;
1296 ind->info = NULL;
1297
1298 /* Fix up the dyn_sym_info pointers to the global symbol. */
1299 for (; dyn_i; dyn_i = dyn_i->next)
1300 dyn_i->h = &dir->root;
1301 }
1302 BFD_ASSERT (ind->info == NULL);
1303
1304 /* Copy over the dynindx. */
1305
1306 if (dir->root.dynindx == -1)
1307 {
1308 dir->root.dynindx = ind->root.dynindx;
1309 dir->root.dynstr_index = ind->root.dynstr_index;
1310 ind->root.dynindx = -1;
1311 ind->root.dynstr_index = 0;
1312 }
1313 BFD_ASSERT (ind->root.dynindx == -1);
1314 }
1315
1316 static void
1317 elfNN_ia64_hash_hide_symbol (info, xh)
1318 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1319 struct elf_link_hash_entry *xh;
1320 {
1321 struct elfNN_ia64_link_hash_entry *h;
1322 struct elfNN_ia64_dyn_sym_info *dyn_i;
1323
1324 h = (struct elfNN_ia64_link_hash_entry *)xh;
1325
1326 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1327 h->root.dynindx = -1;
1328
1329 for (dyn_i = h->info; dyn_i; dyn_i = dyn_i->next)
1330 dyn_i->want_plt2 = 0;
1331 }
1332
1333 /* Create the derived linker hash table. The IA-64 ELF port uses this
1334 derived hash table to keep information specific to the IA-64 ElF
1335 linker (without using static variables). */
1336
1337 static struct bfd_link_hash_table*
1338 elfNN_ia64_hash_table_create (abfd)
1339 bfd *abfd;
1340 {
1341 struct elfNN_ia64_link_hash_table *ret;
1342
1343 ret = bfd_alloc (abfd, sizeof (*ret));
1344 if (!ret)
1345 return 0;
1346 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
1347 elfNN_ia64_new_elf_hash_entry))
1348 {
1349 bfd_release (abfd, ret);
1350 return 0;
1351 }
1352
1353 if (!elfNN_ia64_local_hash_table_init (&ret->loc_hash_table, abfd,
1354 elfNN_ia64_new_loc_hash_entry))
1355 return 0;
1356 return &ret->root.root;
1357 }
1358
1359 /* Look up an entry in a Alpha ELF linker hash table. */
1360
1361 static INLINE struct elfNN_ia64_local_hash_entry *
1362 elfNN_ia64_local_hash_lookup(table, string, create, copy)
1363 struct elfNN_ia64_local_hash_table *table;
1364 const char *string;
1365 boolean create, copy;
1366 {
1367 return ((struct elfNN_ia64_local_hash_entry *)
1368 bfd_hash_lookup (&table->root, string, create, copy));
1369 }
1370
1371 /* Traverse both local and global hash tables. */
1372
1373 struct elfNN_ia64_dyn_sym_traverse_data
1374 {
1375 boolean (*func) PARAMS ((struct elfNN_ia64_dyn_sym_info *, PTR));
1376 PTR data;
1377 };
1378
1379 static boolean
1380 elfNN_ia64_global_dyn_sym_thunk (xentry, xdata)
1381 struct bfd_hash_entry *xentry;
1382 PTR xdata;
1383 {
1384 struct elfNN_ia64_link_hash_entry *entry
1385 = (struct elfNN_ia64_link_hash_entry *) xentry;
1386 struct elfNN_ia64_dyn_sym_traverse_data *data
1387 = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
1388 struct elfNN_ia64_dyn_sym_info *dyn_i;
1389
1390 for (dyn_i = entry->info; dyn_i; dyn_i = dyn_i->next)
1391 if (! (*data->func) (dyn_i, data->data))
1392 return false;
1393 return true;
1394 }
1395
1396 static boolean
1397 elfNN_ia64_local_dyn_sym_thunk (xentry, xdata)
1398 struct bfd_hash_entry *xentry;
1399 PTR xdata;
1400 {
1401 struct elfNN_ia64_local_hash_entry *entry
1402 = (struct elfNN_ia64_local_hash_entry *) xentry;
1403 struct elfNN_ia64_dyn_sym_traverse_data *data
1404 = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
1405 struct elfNN_ia64_dyn_sym_info *dyn_i;
1406
1407 for (dyn_i = entry->info; dyn_i; dyn_i = dyn_i->next)
1408 if (! (*data->func) (dyn_i, data->data))
1409 return false;
1410 return true;
1411 }
1412
1413 static void
1414 elfNN_ia64_dyn_sym_traverse (ia64_info, func, data)
1415 struct elfNN_ia64_link_hash_table *ia64_info;
1416 boolean (*func) PARAMS ((struct elfNN_ia64_dyn_sym_info *, PTR));
1417 PTR data;
1418 {
1419 struct elfNN_ia64_dyn_sym_traverse_data xdata;
1420
1421 xdata.func = func;
1422 xdata.data = data;
1423
1424 elf_link_hash_traverse (&ia64_info->root,
1425 elfNN_ia64_global_dyn_sym_thunk, &xdata);
1426 bfd_hash_traverse (&ia64_info->loc_hash_table.root,
1427 elfNN_ia64_local_dyn_sym_thunk, &xdata);
1428 }
1429 \f
1430 static boolean
1431 elfNN_ia64_create_dynamic_sections (abfd, info)
1432 bfd *abfd;
1433 struct bfd_link_info *info;
1434 {
1435 struct elfNN_ia64_link_hash_table *ia64_info;
1436 struct elf_link_hash_entry *h;
1437 asection *s;
1438
1439 if (! _bfd_elf_create_dynamic_sections (abfd, info))
1440 return false;
1441
1442 ia64_info = elfNN_ia64_hash_table (info);
1443
1444 ia64_info->plt_sec = bfd_get_section_by_name (abfd, ".plt");
1445 ia64_info->got_sec = bfd_get_section_by_name (abfd, ".got");
1446
1447 {
1448 flagword flags = bfd_get_section_flags (abfd, ia64_info->got_sec);
1449 bfd_set_section_flags (abfd, ia64_info->got_sec, SEC_SMALL_DATA | flags);
1450 }
1451
1452 if (!get_pltoff (abfd, info, ia64_info))
1453 return false;
1454
1455 s = bfd_make_section(abfd, ".rela.IA_64.pltoff");
1456 if (s == NULL
1457 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1458 | SEC_HAS_CONTENTS
1459 | SEC_IN_MEMORY
1460 | SEC_LINKER_CREATED
1461 | SEC_READONLY))
1462 || !bfd_set_section_alignment (abfd, s, 3))
1463 return false;
1464 ia64_info->rel_pltoff_sec = s;
1465
1466 s = bfd_make_section(abfd, ".rela.got");
1467 if (s == NULL
1468 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1469 | SEC_HAS_CONTENTS
1470 | SEC_IN_MEMORY
1471 | SEC_LINKER_CREATED
1472 | SEC_READONLY))
1473 || !bfd_set_section_alignment (abfd, s, 3))
1474 return false;
1475 ia64_info->rel_got_sec = s;
1476
1477 return true;
1478 }
1479
1480 /* Find and/or create a descriptor for dynamic symbol info. This will
1481 vary based on global or local symbol, and the addend to the reloc. */
1482
1483 static struct elfNN_ia64_dyn_sym_info *
1484 get_dyn_sym_info (ia64_info, h, abfd, rel, create)
1485 struct elfNN_ia64_link_hash_table *ia64_info;
1486 struct elf_link_hash_entry *h;
1487 bfd *abfd;
1488 const Elf_Internal_Rela *rel;
1489 boolean create;
1490 {
1491 struct elfNN_ia64_dyn_sym_info **pp;
1492 struct elfNN_ia64_dyn_sym_info *dyn_i;
1493 bfd_vma addend = rel ? rel->r_addend : 0;
1494
1495 if (h)
1496 pp = &((struct elfNN_ia64_link_hash_entry *)h)->info;
1497 else
1498 {
1499 struct elfNN_ia64_local_hash_entry *loc_h;
1500 char *addr_name;
1501 size_t len;
1502
1503 /* Construct a string for use in the elfNN_ia64_local_hash_table.
1504 The name describes what was once anonymous memory. */
1505
1506 len = sizeof(void*)*2 + 1 + sizeof(bfd_vma)*4 + 1 + 1;
1507 len += 10; /* %p slop */
1508
1509 addr_name = alloca (len);
1510 sprintf (addr_name, "%p:%lx", abfd, ELFNN_R_SYM (rel->r_info));
1511
1512 /* Collect the canonical entry data for this address. */
1513 loc_h = elfNN_ia64_local_hash_lookup (&ia64_info->loc_hash_table,
1514 addr_name, create, create);
1515 BFD_ASSERT (loc_h);
1516
1517 pp = &loc_h->info;
1518 }
1519
1520 for (dyn_i = *pp; dyn_i && dyn_i->addend != addend; dyn_i = *pp)
1521 pp = &dyn_i->next;
1522
1523 if (dyn_i == NULL && create)
1524 {
1525 dyn_i = (struct elfNN_ia64_dyn_sym_info *)
1526 bfd_zalloc (abfd, sizeof *dyn_i);
1527 *pp = dyn_i;
1528 dyn_i->addend = addend;
1529 }
1530
1531 return dyn_i;
1532 }
1533
1534 static asection *
1535 get_got (abfd, info, ia64_info)
1536 bfd *abfd;
1537 struct bfd_link_info *info;
1538 struct elfNN_ia64_link_hash_table *ia64_info;
1539 {
1540 asection *got, *srel;
1541 bfd *dynobj;
1542
1543 got = ia64_info->got_sec;
1544 if (!got)
1545 {
1546 flagword flags;
1547
1548 dynobj = ia64_info->root.dynobj;
1549 if (!dynobj)
1550 ia64_info->root.dynobj = dynobj = abfd;
1551 if (!_bfd_elf_create_got_section (dynobj, info))
1552 return 0;
1553
1554 got = bfd_get_section_by_name (dynobj, ".got");
1555 BFD_ASSERT (got);
1556 ia64_info->got_sec = got;
1557
1558 flags = bfd_get_section_flags (abfd, got);
1559 bfd_set_section_flags (abfd, got, SEC_SMALL_DATA | flags);
1560 }
1561
1562 return got;
1563 }
1564
1565 /* Create function descriptor section (.opd). This section is called .opd
1566 because it contains "official prodecure descriptors". The "official"
1567 refers to the fact that these descriptors are used when taking the address
1568 of a procedure, thus ensuring a unique address for each procedure. */
1569
1570 static asection *
1571 get_fptr (abfd, info, ia64_info)
1572 bfd *abfd;
1573 struct bfd_link_info *info;
1574 struct elfNN_ia64_link_hash_table *ia64_info;
1575 {
1576 asection *fptr;
1577 bfd *dynobj;
1578
1579 fptr = ia64_info->fptr_sec;
1580 if (!fptr)
1581 {
1582 dynobj = ia64_info->root.dynobj;
1583 if (!dynobj)
1584 ia64_info->root.dynobj = dynobj = abfd;
1585
1586 fptr = bfd_make_section (dynobj, ".opd");
1587 if (!fptr
1588 || !bfd_set_section_flags (dynobj, fptr,
1589 (SEC_ALLOC
1590 | SEC_LOAD
1591 | SEC_HAS_CONTENTS
1592 | SEC_IN_MEMORY
1593 | SEC_READONLY
1594 | SEC_LINKER_CREATED))
1595 || !bfd_set_section_alignment (abfd, fptr, 4))
1596 {
1597 BFD_ASSERT (0);
1598 return NULL;
1599 }
1600
1601 ia64_info->fptr_sec = fptr;
1602 }
1603
1604 return fptr;
1605 }
1606
1607 static asection *
1608 get_pltoff (abfd, info, ia64_info)
1609 bfd *abfd;
1610 struct bfd_link_info *info;
1611 struct elfNN_ia64_link_hash_table *ia64_info;
1612 {
1613 asection *pltoff;
1614 bfd *dynobj;
1615
1616 pltoff = ia64_info->pltoff_sec;
1617 if (!pltoff)
1618 {
1619 dynobj = ia64_info->root.dynobj;
1620 if (!dynobj)
1621 ia64_info->root.dynobj = dynobj = abfd;
1622
1623 pltoff = bfd_make_section (dynobj, ELF_STRING_ia64_pltoff);
1624 if (!pltoff
1625 || !bfd_set_section_flags (dynobj, pltoff,
1626 (SEC_ALLOC
1627 | SEC_LOAD
1628 | SEC_HAS_CONTENTS
1629 | SEC_IN_MEMORY
1630 | SEC_SMALL_DATA
1631 | SEC_LINKER_CREATED))
1632 || !bfd_set_section_alignment (abfd, pltoff, 4))
1633 {
1634 BFD_ASSERT (0);
1635 return NULL;
1636 }
1637
1638 ia64_info->pltoff_sec = pltoff;
1639 }
1640
1641 return pltoff;
1642 }
1643
1644 static asection *
1645 get_reloc_section (abfd, ia64_info, sec, create)
1646 bfd *abfd;
1647 struct elfNN_ia64_link_hash_table *ia64_info;
1648 asection *sec;
1649 boolean create;
1650 {
1651 const char *srel_name;
1652 asection *srel;
1653 bfd *dynobj;
1654
1655 srel_name = (bfd_elf_string_from_elf_section
1656 (abfd, elf_elfheader(abfd)->e_shstrndx,
1657 elf_section_data(sec)->rel_hdr.sh_name));
1658 if (srel_name == NULL)
1659 return NULL;
1660
1661 BFD_ASSERT ((strncmp (srel_name, ".rela", 5) == 0
1662 && strcmp (bfd_get_section_name (abfd, sec),
1663 srel_name+5) == 0)
1664 || (strncmp (srel_name, ".rel", 4) == 0
1665 && strcmp (bfd_get_section_name (abfd, sec),
1666 srel_name+4) == 0));
1667
1668 dynobj = ia64_info->root.dynobj;
1669 if (!dynobj)
1670 ia64_info->root.dynobj = dynobj = abfd;
1671
1672 srel = bfd_get_section_by_name (dynobj, srel_name);
1673 if (srel == NULL && create)
1674 {
1675 srel = bfd_make_section (dynobj, srel_name);
1676 if (srel == NULL
1677 || !bfd_set_section_flags (dynobj, srel,
1678 (SEC_ALLOC
1679 | SEC_LOAD
1680 | SEC_HAS_CONTENTS
1681 | SEC_IN_MEMORY
1682 | SEC_LINKER_CREATED
1683 | SEC_READONLY))
1684 || !bfd_set_section_alignment (dynobj, srel, 3))
1685 return NULL;
1686 }
1687
1688 return srel;
1689 }
1690
1691 static boolean
1692 count_dyn_reloc (abfd, dyn_i, srel, type)
1693 bfd *abfd;
1694 struct elfNN_ia64_dyn_sym_info *dyn_i;
1695 asection *srel;
1696 int type;
1697 {
1698 struct elfNN_ia64_dyn_reloc_entry *rent;
1699
1700 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
1701 if (rent->srel == srel && rent->type == type)
1702 break;
1703
1704 if (!rent)
1705 {
1706 rent = (struct elfNN_ia64_dyn_reloc_entry *)
1707 bfd_alloc (abfd, sizeof (*rent));
1708 if (!rent)
1709 return false;
1710
1711 rent->next = dyn_i->reloc_entries;
1712 rent->srel = srel;
1713 rent->type = type;
1714 rent->count = 0;
1715 dyn_i->reloc_entries = rent;
1716 }
1717 rent->count++;
1718
1719 return true;
1720 }
1721
1722 static boolean
1723 elfNN_ia64_check_relocs (abfd, info, sec, relocs)
1724 bfd *abfd;
1725 struct bfd_link_info *info;
1726 asection *sec;
1727 const Elf_Internal_Rela *relocs;
1728 {
1729 struct elfNN_ia64_link_hash_table *ia64_info;
1730 const Elf_Internal_Rela *relend;
1731 Elf_Internal_Shdr *symtab_hdr;
1732 const Elf_Internal_Rela *rel;
1733 asection *got, *fptr, *srel;
1734
1735 if (info->relocateable)
1736 return true;
1737
1738 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1739 ia64_info = elfNN_ia64_hash_table (info);
1740
1741 got = fptr = srel = NULL;
1742
1743 relend = relocs + sec->reloc_count;
1744 for (rel = relocs; rel < relend; ++rel)
1745 {
1746 enum {
1747 NEED_GOT = 1,
1748 NEED_FPTR = 2,
1749 NEED_PLTOFF = 4,
1750 NEED_MIN_PLT = 8,
1751 NEED_FULL_PLT = 16,
1752 NEED_DYNREL = 32,
1753 NEED_LTOFF_FPTR = 64,
1754 };
1755
1756 struct elf_link_hash_entry *h = NULL;
1757 unsigned long r_symndx = ELFNN_R_SYM (rel->r_info);
1758 struct elfNN_ia64_dyn_sym_info *dyn_i;
1759 int need_entry;
1760 boolean maybe_dynamic;
1761 int dynrel_type;
1762
1763 if (r_symndx >= symtab_hdr->sh_info)
1764 {
1765 /* We're dealing with a global symbol -- find its hash entry
1766 and mark it as being referenced. */
1767 long indx = r_symndx - symtab_hdr->sh_info;
1768 h = elf_sym_hashes (abfd)[indx];
1769 while (h->root.type == bfd_link_hash_indirect
1770 || h->root.type == bfd_link_hash_warning)
1771 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1772
1773 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1774 }
1775
1776 /* We can only get preliminary data on whether a symbol is
1777 locally or externally defined, as not all of the input files
1778 have yet been processed. Do something with what we know, as
1779 this may help reduce memory usage and processing time later. */
1780 maybe_dynamic = false;
1781 if (h && ((info->shared && ! info->symbolic)
1782 || ! (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
1783 || h->root.type == bfd_link_hash_defweak))
1784 maybe_dynamic = true;
1785
1786 need_entry = 0;
1787 switch (ELFNN_R_TYPE (rel->r_info))
1788 {
1789 case R_IA64_TPREL22:
1790 case R_IA64_TPREL64MSB:
1791 case R_IA64_TPREL64LSB:
1792 case R_IA64_LTOFF_TP22:
1793 return false;
1794
1795 case R_IA64_LTOFF_FPTR22:
1796 case R_IA64_LTOFF_FPTR64I:
1797 case R_IA64_LTOFF_FPTR64MSB:
1798 case R_IA64_LTOFF_FPTR64LSB:
1799 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
1800 break;
1801
1802 case R_IA64_FPTR64I:
1803 case R_IA64_FPTR32MSB:
1804 case R_IA64_FPTR32LSB:
1805 case R_IA64_FPTR64MSB:
1806 case R_IA64_FPTR64LSB:
1807 if (info->shared || h)
1808 need_entry = NEED_FPTR | NEED_DYNREL;
1809 else
1810 need_entry = NEED_FPTR;
1811 dynrel_type = R_IA64_FPTR64LSB;
1812 break;
1813
1814 case R_IA64_LTOFF22:
1815 case R_IA64_LTOFF22X:
1816 case R_IA64_LTOFF64I:
1817 need_entry = NEED_GOT;
1818 break;
1819
1820 case R_IA64_PLTOFF22:
1821 case R_IA64_PLTOFF64I:
1822 case R_IA64_PLTOFF64MSB:
1823 case R_IA64_PLTOFF64LSB:
1824 need_entry = NEED_PLTOFF;
1825 if (h)
1826 {
1827 if (maybe_dynamic)
1828 need_entry |= NEED_MIN_PLT;
1829 }
1830 else
1831 {
1832 (*info->callbacks->warning)
1833 (info, _("@pltoff reloc against local symbol"), 0,
1834 abfd, 0, 0);
1835 }
1836 break;
1837
1838 case R_IA64_PCREL21B:
1839 case R_IA64_PCREL60B:
1840 /* Depending on where this symbol is defined, we may or may not
1841 need a full plt entry. Only skip if we know we'll not need
1842 the entry -- static or symbolic, and the symbol definition
1843 has already been seen. */
1844 if (maybe_dynamic && rel->r_addend == 0)
1845 need_entry = NEED_FULL_PLT;
1846 break;
1847
1848 case R_IA64_IMM14:
1849 case R_IA64_IMM22:
1850 case R_IA64_IMM64:
1851 case R_IA64_DIR32MSB:
1852 case R_IA64_DIR32LSB:
1853 case R_IA64_DIR64MSB:
1854 case R_IA64_DIR64LSB:
1855 /* Shared objects will always need at least a REL relocation. */
1856 if (info->shared || maybe_dynamic)
1857 need_entry = NEED_DYNREL;
1858 dynrel_type = R_IA64_DIR64LSB;
1859 break;
1860
1861 case R_IA64_IPLTMSB:
1862 case R_IA64_IPLTLSB:
1863 /* Shared objects will always need at least a REL relocation. */
1864 if (info->shared || maybe_dynamic)
1865 need_entry = NEED_DYNREL;
1866 dynrel_type = R_IA64_IPLTLSB;
1867 break;
1868
1869 case R_IA64_PCREL22:
1870 case R_IA64_PCREL64I:
1871 case R_IA64_PCREL32MSB:
1872 case R_IA64_PCREL32LSB:
1873 case R_IA64_PCREL64MSB:
1874 case R_IA64_PCREL64LSB:
1875 if (maybe_dynamic)
1876 need_entry = NEED_DYNREL;
1877 dynrel_type = R_IA64_PCREL64LSB;
1878 break;
1879 }
1880
1881 if (!need_entry)
1882 continue;
1883
1884 if ((need_entry & NEED_FPTR) != 0
1885 && rel->r_addend)
1886 {
1887 (*info->callbacks->warning)
1888 (info, _("non-zero addend in @fptr reloc"), 0,
1889 abfd, 0, 0);
1890 }
1891
1892 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, true);
1893
1894 /* Record whether or not this is a local symbol. */
1895 dyn_i->h = h;
1896
1897 /* Create what's needed. */
1898 if (need_entry & NEED_GOT)
1899 {
1900 if (!got)
1901 {
1902 got = get_got (abfd, info, ia64_info);
1903 if (!got)
1904 return false;
1905 }
1906 dyn_i->want_got = 1;
1907 }
1908 if (need_entry & NEED_FPTR)
1909 {
1910 if (!fptr)
1911 {
1912 fptr = get_fptr (abfd, info, ia64_info);
1913 if (!fptr)
1914 return false;
1915 }
1916
1917 /* FPTRs for shared libraries are allocated by the dynamic
1918 linker. Make sure this local symbol will appear in the
1919 dynamic symbol table. */
1920 if (!h && info->shared)
1921 {
1922 if (! (_bfd_elfNN_link_record_local_dynamic_symbol
1923 (info, abfd, r_symndx)))
1924 return false;
1925 }
1926
1927 dyn_i->want_fptr = 1;
1928 }
1929 if (need_entry & NEED_LTOFF_FPTR)
1930 dyn_i->want_ltoff_fptr = 1;
1931 if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
1932 {
1933 if (!ia64_info->root.dynobj)
1934 ia64_info->root.dynobj = abfd;
1935 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1936 dyn_i->want_plt = 1;
1937 }
1938 if (need_entry & NEED_FULL_PLT)
1939 dyn_i->want_plt2 = 1;
1940 if (need_entry & NEED_PLTOFF)
1941 dyn_i->want_pltoff = 1;
1942 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
1943 {
1944 if (!srel)
1945 {
1946 srel = get_reloc_section (abfd, ia64_info, sec, true);
1947 if (!srel)
1948 return false;
1949 }
1950 if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type))
1951 return false;
1952 }
1953 }
1954
1955 return true;
1956 }
1957
1958 struct elfNN_ia64_allocate_data
1959 {
1960 struct bfd_link_info *info;
1961 bfd_size_type ofs;
1962 };
1963
1964 /* For cleanliness, and potentially faster dynamic loading, allocate
1965 external GOT entries first. */
1966
1967 static boolean
1968 allocate_global_data_got (dyn_i, data)
1969 struct elfNN_ia64_dyn_sym_info *dyn_i;
1970 PTR data;
1971 {
1972 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
1973
1974 if (dyn_i->want_got
1975 && ! dyn_i->want_fptr
1976 && elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info))
1977 {
1978 dyn_i->got_offset = x->ofs;
1979 x->ofs += 8;
1980 }
1981 return true;
1982 }
1983
1984 /* Next, allocate all the GOT entries used by LTOFF_FPTR relocs. */
1985
1986 static boolean
1987 allocate_global_fptr_got (dyn_i, data)
1988 struct elfNN_ia64_dyn_sym_info *dyn_i;
1989 PTR data;
1990 {
1991 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
1992
1993 if (dyn_i->want_got
1994 && dyn_i->want_fptr
1995 && elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info))
1996 {
1997 dyn_i->got_offset = x->ofs;
1998 x->ofs += 8;
1999 }
2000 return true;
2001 }
2002
2003 /* Lastly, allocate all the GOT entries for local data. */
2004
2005 static boolean
2006 allocate_local_got (dyn_i, data)
2007 struct elfNN_ia64_dyn_sym_info *dyn_i;
2008 PTR data;
2009 {
2010 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2011
2012 if (dyn_i->want_got
2013 && ! elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info))
2014 {
2015 dyn_i->got_offset = x->ofs;
2016 x->ofs += 8;
2017 }
2018 return true;
2019 }
2020
2021 /* Search for the index of a global symbol in it's defining object file. */
2022
2023 static unsigned long
2024 global_sym_index (h)
2025 struct elf_link_hash_entry *h;
2026 {
2027 struct elf_link_hash_entry **p;
2028 bfd *obj;
2029
2030 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2031 || h->root.type == bfd_link_hash_defweak);
2032
2033 obj = h->root.u.def.section->owner;
2034 for (p = elf_sym_hashes (obj); *p != h; ++p)
2035 continue;
2036
2037 return p - elf_sym_hashes (obj) + elf_tdata (obj)->symtab_hdr.sh_info;
2038 }
2039
2040 /* Allocate function descriptors. We can do these for every function
2041 in a main executable that is not exported. */
2042
2043 static boolean
2044 allocate_fptr (dyn_i, data)
2045 struct elfNN_ia64_dyn_sym_info *dyn_i;
2046 PTR data;
2047 {
2048 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2049
2050 if (dyn_i->want_fptr)
2051 {
2052 struct elf_link_hash_entry *h = dyn_i->h;
2053
2054 if (h)
2055 while (h->root.type == bfd_link_hash_indirect
2056 || h->root.type == bfd_link_hash_warning)
2057 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2058
2059 if (x->info->shared)
2060 {
2061 if (h && h->dynindx == -1)
2062 {
2063 BFD_ASSERT ((h->root.type == bfd_link_hash_defined)
2064 || (h->root.type == bfd_link_hash_defweak));
2065
2066 if (!_bfd_elfNN_link_record_local_dynamic_symbol
2067 (x->info, h->root.u.def.section->owner,
2068 global_sym_index (h)))
2069 return false;
2070 }
2071
2072 dyn_i->want_fptr = 0;
2073 }
2074 else if (h == NULL || h->dynindx == -1)
2075 {
2076 dyn_i->fptr_offset = x->ofs;
2077 x->ofs += 16;
2078 }
2079 else
2080 dyn_i->want_fptr = 0;
2081 }
2082 return true;
2083 }
2084
2085 /* Allocate all the minimal PLT entries. */
2086
2087 static boolean
2088 allocate_plt_entries (dyn_i, data)
2089 struct elfNN_ia64_dyn_sym_info *dyn_i;
2090 PTR data;
2091 {
2092 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2093
2094 if (dyn_i->want_plt)
2095 {
2096 struct elf_link_hash_entry *h = dyn_i->h;
2097
2098 if (h)
2099 while (h->root.type == bfd_link_hash_indirect
2100 || h->root.type == bfd_link_hash_warning)
2101 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2102
2103 /* ??? Versioned symbols seem to lose ELF_LINK_HASH_NEEDS_PLT. */
2104 if (elfNN_ia64_dynamic_symbol_p (h, x->info))
2105 {
2106 bfd_size_type offset = x->ofs;
2107 if (offset == 0)
2108 offset = PLT_HEADER_SIZE;
2109 dyn_i->plt_offset = offset;
2110 x->ofs = offset + PLT_MIN_ENTRY_SIZE;
2111
2112 dyn_i->want_pltoff = 1;
2113 }
2114 else
2115 {
2116 dyn_i->want_plt = 0;
2117 dyn_i->want_plt2 = 0;
2118 }
2119 }
2120 return true;
2121 }
2122
2123 /* Allocate all the full PLT entries. */
2124
2125 static boolean
2126 allocate_plt2_entries (dyn_i, data)
2127 struct elfNN_ia64_dyn_sym_info *dyn_i;
2128 PTR data;
2129 {
2130 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2131
2132 if (dyn_i->want_plt2)
2133 {
2134 struct elf_link_hash_entry *h = dyn_i->h;
2135 bfd_size_type ofs = x->ofs;
2136
2137 dyn_i->plt2_offset = ofs;
2138 x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
2139
2140 while (h->root.type == bfd_link_hash_indirect
2141 || h->root.type == bfd_link_hash_warning)
2142 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2143 dyn_i->h->plt.offset = ofs;
2144 }
2145 return true;
2146 }
2147
2148 /* Allocate all the PLTOFF entries requested by relocations and
2149 plt entries. We can't share space with allocated FPTR entries,
2150 because the latter are not necessarily addressable by the GP.
2151 ??? Relaxation might be able to determine that they are. */
2152
2153 static boolean
2154 allocate_pltoff_entries (dyn_i, data)
2155 struct elfNN_ia64_dyn_sym_info *dyn_i;
2156 PTR data;
2157 {
2158 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2159
2160 if (dyn_i->want_pltoff)
2161 {
2162 dyn_i->pltoff_offset = x->ofs;
2163 x->ofs += 16;
2164 }
2165 return true;
2166 }
2167
2168 /* Allocate dynamic relocations for those symbols that turned out
2169 to be dynamic. */
2170
2171 static boolean
2172 allocate_dynrel_entries (dyn_i, data)
2173 struct elfNN_ia64_dyn_sym_info *dyn_i;
2174 PTR data;
2175 {
2176 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2177 struct elfNN_ia64_link_hash_table *ia64_info;
2178 struct elfNN_ia64_dyn_reloc_entry *rent;
2179 boolean dynamic_symbol, shared;
2180
2181 ia64_info = elfNN_ia64_hash_table (x->info);
2182 dynamic_symbol = elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info);
2183 shared = x->info->shared;
2184
2185 /* Take care of the normal data relocations. */
2186
2187 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2188 {
2189 int count = rent->count;
2190
2191 switch (rent->type)
2192 {
2193 case R_IA64_FPTR64LSB:
2194 /* Allocate one iff !want_fptr, which by this point will
2195 be true only if we're actually allocating one statically
2196 in the main executable. */
2197 if (dyn_i->want_fptr)
2198 continue;
2199 break;
2200 case R_IA64_PCREL64LSB:
2201 if (!dynamic_symbol)
2202 continue;
2203 break;
2204 case R_IA64_DIR64LSB:
2205 if (!dynamic_symbol && !shared)
2206 continue;
2207 break;
2208 case R_IA64_IPLTLSB:
2209 if (!dynamic_symbol && !shared)
2210 continue;
2211 /* Use two REL relocations for IPLT relocations
2212 against local symbols. */
2213 if (!dynamic_symbol)
2214 count *= 2;
2215 break;
2216 default:
2217 abort ();
2218 }
2219 rent->srel->_raw_size += sizeof (ElfNN_External_Rela) * count;
2220 }
2221
2222 /* Take care of the GOT and PLT relocations. */
2223
2224 if (((dynamic_symbol || shared) && dyn_i->want_got)
2225 || (dyn_i->want_ltoff_fptr && dyn_i->h && dyn_i->h->dynindx != -1))
2226 ia64_info->rel_got_sec->_raw_size += sizeof (ElfNN_External_Rela);
2227
2228 if (dyn_i->want_pltoff)
2229 {
2230 bfd_size_type t = 0;
2231
2232 /* Dynamic symbols get one IPLT relocation. Local symbols in
2233 shared libraries get two REL relocations. Local symbols in
2234 main applications get nothing. */
2235 if (dynamic_symbol)
2236 t = sizeof (ElfNN_External_Rela);
2237 else if (shared)
2238 t = 2 * sizeof (ElfNN_External_Rela);
2239
2240 ia64_info->rel_pltoff_sec->_raw_size += t;
2241 }
2242
2243 return true;
2244 }
2245
2246 static boolean
2247 elfNN_ia64_adjust_dynamic_symbol (info, h)
2248 struct bfd_link_info *info;
2249 struct elf_link_hash_entry *h;
2250 {
2251 /* ??? Undefined symbols with PLT entries should be re-defined
2252 to be the PLT entry. */
2253
2254 /* If this is a weak symbol, and there is a real definition, the
2255 processor independent code will have arranged for us to see the
2256 real definition first, and we can just use the same value. */
2257 if (h->weakdef != NULL)
2258 {
2259 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2260 || h->weakdef->root.type == bfd_link_hash_defweak);
2261 h->root.u.def.section = h->weakdef->root.u.def.section;
2262 h->root.u.def.value = h->weakdef->root.u.def.value;
2263 return true;
2264 }
2265
2266 /* If this is a reference to a symbol defined by a dynamic object which
2267 is not a function, we might allocate the symbol in our .dynbss section
2268 and allocate a COPY dynamic relocation.
2269
2270 But IA-64 code is canonically PIC, so as a rule we can avoid this sort
2271 of hackery. */
2272
2273 return true;
2274 }
2275
2276 static boolean
2277 elfNN_ia64_size_dynamic_sections (output_bfd, info)
2278 bfd *output_bfd;
2279 struct bfd_link_info *info;
2280 {
2281 struct elfNN_ia64_allocate_data data;
2282 struct elfNN_ia64_link_hash_table *ia64_info;
2283 asection *sec;
2284 bfd *dynobj;
2285 boolean reltext = false;
2286 boolean relplt = false;
2287
2288 dynobj = elf_hash_table(info)->dynobj;
2289 ia64_info = elfNN_ia64_hash_table (info);
2290 BFD_ASSERT(dynobj != NULL);
2291 data.info = info;
2292
2293 /* Set the contents of the .interp section to the interpreter. */
2294 if (ia64_info->root.dynamic_sections_created
2295 && !info->shared)
2296 {
2297 sec = bfd_get_section_by_name (dynobj, ".interp");
2298 BFD_ASSERT (sec != NULL);
2299 sec->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
2300 sec->_raw_size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
2301 }
2302
2303 /* DT_INIT and DT_FINI get function descriptors not raw code addresses.
2304 Force their symbols to have pltoff entries so we can use those. */
2305 if (ia64_info->root.dynamic_sections_created)
2306 {
2307 struct elf_link_hash_entry *h;
2308 struct elfNN_ia64_dyn_sym_info *dyn_i;
2309
2310 if (info->init_function
2311 && (h = elf_link_hash_lookup (elf_hash_table (info),
2312 info->init_function, false,
2313 false, false))
2314 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2315 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2316 {
2317 dyn_i = get_dyn_sym_info (ia64_info, h, output_bfd, NULL, true);
2318 dyn_i->want_pltoff = 1;
2319 }
2320
2321 if (info->fini_function
2322 && (h = elf_link_hash_lookup (elf_hash_table (info),
2323 info->fini_function, false,
2324 false, false))
2325 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2326 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2327 {
2328 dyn_i = get_dyn_sym_info (ia64_info, h, output_bfd, NULL, true);
2329 dyn_i->want_pltoff = 1;
2330 }
2331 }
2332
2333 /* Allocate the GOT entries. */
2334
2335 if (ia64_info->got_sec)
2336 {
2337 data.ofs = 0;
2338 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
2339 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
2340 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
2341 ia64_info->got_sec->_raw_size = data.ofs;
2342 }
2343
2344 /* Allocate the FPTR entries. */
2345
2346 if (ia64_info->fptr_sec)
2347 {
2348 data.ofs = 0;
2349 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
2350 ia64_info->fptr_sec->_raw_size = data.ofs;
2351 }
2352
2353 /* Now that we've seen all of the input files, we can decide which
2354 symbols need plt entries. Allocate the minimal PLT entries first.
2355 We do this even though dynamic_sections_created may be false, because
2356 this has the side-effect of clearing want_plt and want_plt2. */
2357
2358 data.ofs = 0;
2359 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
2360
2361 ia64_info->minplt_entries = 0;
2362 if (data.ofs)
2363 {
2364 ia64_info->minplt_entries
2365 = (data.ofs - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
2366 }
2367
2368 /* Align the pointer for the plt2 entries. */
2369 data.ofs = (data.ofs + 31) & -32;
2370
2371 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
2372 if (data.ofs != 0)
2373 {
2374 BFD_ASSERT (ia64_info->root.dynamic_sections_created);
2375
2376 ia64_info->plt_sec->_raw_size = data.ofs;
2377
2378 /* If we've got a .plt, we need some extra memory for the dynamic
2379 linker. We stuff these in .got.plt. */
2380 sec = bfd_get_section_by_name (dynobj, ".got.plt");
2381 sec->_raw_size = 8 * PLT_RESERVED_WORDS;
2382 }
2383
2384 /* Allocate the PLTOFF entries. */
2385
2386 if (ia64_info->pltoff_sec)
2387 {
2388 data.ofs = 0;
2389 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
2390 ia64_info->pltoff_sec->_raw_size = data.ofs;
2391 }
2392
2393 if (ia64_info->root.dynamic_sections_created)
2394 {
2395 /* Allocate space for the dynamic relocations that turned out to be
2396 required. */
2397
2398 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
2399 }
2400
2401 /* We have now determined the sizes of the various dynamic sections.
2402 Allocate memory for them. */
2403 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2404 {
2405 boolean strip;
2406
2407 if (!(sec->flags & SEC_LINKER_CREATED))
2408 continue;
2409
2410 /* If we don't need this section, strip it from the output file.
2411 There were several sections primarily related to dynamic
2412 linking that must be create before the linker maps input
2413 sections to output sections. The linker does that before
2414 bfd_elf_size_dynamic_sections is called, and it is that
2415 function which decides whether anything needs to go into
2416 these sections. */
2417
2418 strip = (sec->_raw_size == 0);
2419
2420 if (sec == ia64_info->got_sec)
2421 strip = false;
2422 else if (sec == ia64_info->rel_got_sec)
2423 {
2424 if (strip)
2425 ia64_info->rel_got_sec = NULL;
2426 else
2427 /* We use the reloc_count field as a counter if we need to
2428 copy relocs into the output file. */
2429 sec->reloc_count = 0;
2430 }
2431 else if (sec == ia64_info->fptr_sec)
2432 {
2433 if (strip)
2434 ia64_info->fptr_sec = NULL;
2435 }
2436 else if (sec == ia64_info->plt_sec)
2437 {
2438 if (strip)
2439 ia64_info->plt_sec = NULL;
2440 }
2441 else if (sec == ia64_info->pltoff_sec)
2442 {
2443 if (strip)
2444 ia64_info->pltoff_sec = NULL;
2445 }
2446 else if (sec == ia64_info->rel_pltoff_sec)
2447 {
2448 if (strip)
2449 ia64_info->rel_pltoff_sec = NULL;
2450 else
2451 {
2452 relplt = true;
2453 /* We use the reloc_count field as a counter if we need to
2454 copy relocs into the output file. */
2455 sec->reloc_count = 0;
2456 }
2457 }
2458 else
2459 {
2460 const char *name;
2461
2462 /* It's OK to base decisions on the section name, because none
2463 of the dynobj section names depend upon the input files. */
2464 name = bfd_get_section_name (dynobj, sec);
2465
2466 if (strcmp (name, ".got.plt") == 0)
2467 strip = false;
2468 else if (strncmp (name, ".rel", 4) == 0)
2469 {
2470 if (!strip)
2471 {
2472 const char *outname;
2473 asection *target;
2474
2475 /* If this relocation section applies to a read only
2476 section, then we probably need a DT_TEXTREL entry. */
2477 outname = bfd_get_section_name (output_bfd,
2478 sec->output_section);
2479 if (outname[4] == 'a')
2480 outname += 5;
2481 else
2482 outname += 4;
2483
2484 target = bfd_get_section_by_name (output_bfd, outname);
2485 if (target != NULL
2486 && (target->flags & SEC_READONLY) != 0
2487 && (target->flags & SEC_ALLOC) != 0)
2488 reltext = true;
2489
2490 /* We use the reloc_count field as a counter if we need to
2491 copy relocs into the output file. */
2492 sec->reloc_count = 0;
2493 }
2494 }
2495 else
2496 continue;
2497 }
2498
2499 if (strip)
2500 _bfd_strip_section_from_output (info, sec);
2501 else
2502 {
2503 /* Allocate memory for the section contents. */
2504 sec->contents = (bfd_byte *) bfd_zalloc(dynobj, sec->_raw_size);
2505 if (sec->contents == NULL && sec->_raw_size != 0)
2506 return false;
2507 }
2508 }
2509
2510 if (elf_hash_table (info)->dynamic_sections_created)
2511 {
2512 /* Add some entries to the .dynamic section. We fill in the values
2513 later (in finish_dynamic_sections) but we must add the entries now
2514 so that we get the correct size for the .dynamic section. */
2515
2516 if (!info->shared)
2517 {
2518 /* The DT_DEBUG entry is filled in by the dynamic linker and used
2519 by the debugger. */
2520 if (!bfd_elfNN_add_dynamic_entry (info, DT_DEBUG, 0))
2521 return false;
2522 }
2523
2524 if (! bfd_elfNN_add_dynamic_entry (info, DT_IA_64_PLT_RESERVE, 0))
2525 return false;
2526 if (! bfd_elfNN_add_dynamic_entry (info, DT_PLTGOT, 0))
2527 return false;
2528
2529 if (relplt)
2530 {
2531 if (! bfd_elfNN_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2532 || ! bfd_elfNN_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2533 || ! bfd_elfNN_add_dynamic_entry (info, DT_JMPREL, 0))
2534 return false;
2535 }
2536
2537 if (! bfd_elfNN_add_dynamic_entry (info, DT_RELA, 0)
2538 || ! bfd_elfNN_add_dynamic_entry (info, DT_RELASZ, 0)
2539 || ! bfd_elfNN_add_dynamic_entry (info, DT_RELAENT,
2540 sizeof(ElfNN_External_Rela)))
2541 return false;
2542
2543 if (reltext)
2544 {
2545 if (! bfd_elfNN_add_dynamic_entry (info, DT_TEXTREL, 0))
2546 return false;
2547 info->flags |= DF_TEXTREL;
2548 }
2549 }
2550
2551 /* ??? Perhaps force __gp local. */
2552
2553 return true;
2554 }
2555
2556 static bfd_reloc_status_type
2557 elfNN_ia64_install_value (abfd, hit_addr, val, r_type)
2558 bfd *abfd;
2559 bfd_byte *hit_addr;
2560 bfd_vma val;
2561 unsigned int r_type;
2562 {
2563 const struct ia64_operand *op;
2564 int bigendian = 0, shift = 0;
2565 bfd_vma t0, t1, insn, dword;
2566 enum ia64_opnd opnd;
2567 const char *err;
2568 size_t size = 8;
2569
2570 opnd = IA64_OPND_NIL;
2571 switch (r_type)
2572 {
2573 case R_IA64_NONE:
2574 case R_IA64_LDXMOV:
2575 return bfd_reloc_ok;
2576
2577 /* Instruction relocations. */
2578
2579 case R_IA64_IMM14: opnd = IA64_OPND_IMM14; break;
2580
2581 case R_IA64_PCREL21F: opnd = IA64_OPND_TGT25; break;
2582 case R_IA64_PCREL21M: opnd = IA64_OPND_TGT25b; break;
2583 case R_IA64_PCREL60B: opnd = IA64_OPND_TGT64; break;
2584 case R_IA64_PCREL21B:
2585 case R_IA64_PCREL21BI:
2586 opnd = IA64_OPND_TGT25c;
2587 break;
2588
2589 case R_IA64_IMM22:
2590 case R_IA64_GPREL22:
2591 case R_IA64_LTOFF22:
2592 case R_IA64_LTOFF22X:
2593 case R_IA64_PLTOFF22:
2594 case R_IA64_PCREL22:
2595 case R_IA64_LTOFF_FPTR22:
2596 opnd = IA64_OPND_IMM22;
2597 break;
2598
2599 case R_IA64_IMM64:
2600 case R_IA64_GPREL64I:
2601 case R_IA64_LTOFF64I:
2602 case R_IA64_PLTOFF64I:
2603 case R_IA64_PCREL64I:
2604 case R_IA64_FPTR64I:
2605 case R_IA64_LTOFF_FPTR64I:
2606 opnd = IA64_OPND_IMMU64;
2607 break;
2608
2609 /* Data relocations. */
2610
2611 case R_IA64_DIR32MSB:
2612 case R_IA64_GPREL32MSB:
2613 case R_IA64_FPTR32MSB:
2614 case R_IA64_PCREL32MSB:
2615 case R_IA64_SEGREL32MSB:
2616 case R_IA64_SECREL32MSB:
2617 case R_IA64_LTV32MSB:
2618 size = 4; bigendian = 1;
2619 break;
2620
2621 case R_IA64_DIR32LSB:
2622 case R_IA64_GPREL32LSB:
2623 case R_IA64_FPTR32LSB:
2624 case R_IA64_PCREL32LSB:
2625 case R_IA64_SEGREL32LSB:
2626 case R_IA64_SECREL32LSB:
2627 case R_IA64_LTV32LSB:
2628 size = 4; bigendian = 0;
2629 break;
2630
2631 case R_IA64_DIR64MSB:
2632 case R_IA64_GPREL64MSB:
2633 case R_IA64_PLTOFF64MSB:
2634 case R_IA64_FPTR64MSB:
2635 case R_IA64_PCREL64MSB:
2636 case R_IA64_LTOFF_FPTR64MSB:
2637 case R_IA64_SEGREL64MSB:
2638 case R_IA64_SECREL64MSB:
2639 case R_IA64_LTV64MSB:
2640 size = 8; bigendian = 1;
2641 break;
2642
2643 case R_IA64_DIR64LSB:
2644 case R_IA64_GPREL64LSB:
2645 case R_IA64_PLTOFF64LSB:
2646 case R_IA64_FPTR64LSB:
2647 case R_IA64_PCREL64LSB:
2648 case R_IA64_LTOFF_FPTR64LSB:
2649 case R_IA64_SEGREL64LSB:
2650 case R_IA64_SECREL64LSB:
2651 case R_IA64_LTV64LSB:
2652 size = 8; bigendian = 0;
2653 break;
2654
2655 /* Unsupported / Dynamic relocations. */
2656 default:
2657 return bfd_reloc_notsupported;
2658 }
2659
2660 switch (opnd)
2661 {
2662 case IA64_OPND_IMMU64:
2663 hit_addr -= (long) hit_addr & 0x3;
2664 t0 = bfd_get_64 (abfd, hit_addr);
2665 t1 = bfd_get_64 (abfd, hit_addr + 8);
2666
2667 /* tmpl/s: bits 0.. 5 in t0
2668 slot 0: bits 5..45 in t0
2669 slot 1: bits 46..63 in t0, bits 0..22 in t1
2670 slot 2: bits 23..63 in t1 */
2671
2672 /* First, clear the bits that form the 64 bit constant. */
2673 t0 &= ~(0x3ffffLL << 46);
2674 t1 &= ~(0x7fffffLL
2675 | (( (0x07fLL << 13) | (0x1ffLL << 27)
2676 | (0x01fLL << 22) | (0x001LL << 21)
2677 | (0x001LL << 36)) << 23));
2678
2679 t0 |= ((val >> 22) & 0x03ffffLL) << 46; /* 18 lsbs of imm41 */
2680 t1 |= ((val >> 40) & 0x7fffffLL) << 0; /* 23 msbs of imm41 */
2681 t1 |= ( (((val >> 0) & 0x07f) << 13) /* imm7b */
2682 | (((val >> 7) & 0x1ff) << 27) /* imm9d */
2683 | (((val >> 16) & 0x01f) << 22) /* imm5c */
2684 | (((val >> 21) & 0x001) << 21) /* ic */
2685 | (((val >> 63) & 0x001) << 36)) << 23; /* i */
2686
2687 bfd_put_64 (abfd, t0, hit_addr);
2688 bfd_put_64 (abfd, t1, hit_addr + 8);
2689 break;
2690
2691 case IA64_OPND_TGT64:
2692 hit_addr -= (long) hit_addr & 0x3;
2693 t0 = bfd_get_64 (abfd, hit_addr);
2694 t1 = bfd_get_64 (abfd, hit_addr + 8);
2695
2696 /* tmpl/s: bits 0.. 5 in t0
2697 slot 0: bits 5..45 in t0
2698 slot 1: bits 46..63 in t0, bits 0..22 in t1
2699 slot 2: bits 23..63 in t1 */
2700
2701 /* First, clear the bits that form the 64 bit constant. */
2702 t0 &= ~(0x3ffffLL << 46);
2703 t1 &= ~(0x7fffffLL
2704 | ((1LL << 36 | 0xfffffLL << 13) << 23));
2705
2706 val >>= 4;
2707 t0 |= ((val >> 20) & 0xffffLL) << 2 << 46; /* 16 lsbs of imm39 */
2708 t1 |= ((val >> 36) & 0x7fffffLL) << 0; /* 23 msbs of imm39 */
2709 t1 |= ((((val >> 0) & 0xfffffLL) << 13) /* imm20b */
2710 | (((val >> 59) & 0x1LL) << 36)) << 23; /* i */
2711
2712 bfd_put_64 (abfd, t0, hit_addr);
2713 bfd_put_64 (abfd, t1, hit_addr + 8);
2714 break;
2715
2716 default:
2717 switch ((long) hit_addr & 0x3)
2718 {
2719 case 0: shift = 5; break;
2720 case 1: shift = 14; hit_addr += 3; break;
2721 case 2: shift = 23; hit_addr += 6; break;
2722 case 3: return bfd_reloc_notsupported; /* shouldn't happen... */
2723 }
2724 dword = bfd_get_64 (abfd, hit_addr);
2725 insn = (dword >> shift) & 0x1ffffffffffLL;
2726
2727 op = elf64_ia64_operands + opnd;
2728 err = (*op->insert) (op, val, &insn);
2729 if (err)
2730 return bfd_reloc_overflow;
2731
2732 dword &= ~(0x1ffffffffffLL << shift);
2733 dword |= (insn << shift);
2734 bfd_put_64 (abfd, dword, hit_addr);
2735 break;
2736
2737 case IA64_OPND_NIL:
2738 /* A data relocation. */
2739 if (bigendian)
2740 if (size == 4)
2741 bfd_putb32 (val, hit_addr);
2742 else
2743 bfd_putb64 (val, hit_addr);
2744 else
2745 if (size == 4)
2746 bfd_putl32 (val, hit_addr);
2747 else
2748 bfd_putl64 (val, hit_addr);
2749 break;
2750 }
2751
2752 return bfd_reloc_ok;
2753 }
2754
2755 static void
2756 elfNN_ia64_install_dyn_reloc (abfd, info, sec, srel, offset, type,
2757 dynindx, addend)
2758 bfd *abfd;
2759 struct bfd_link_info *info;
2760 asection *sec;
2761 asection *srel;
2762 bfd_vma offset;
2763 unsigned int type;
2764 long dynindx;
2765 bfd_vma addend;
2766 {
2767 Elf_Internal_Rela outrel;
2768
2769 outrel.r_offset = (sec->output_section->vma
2770 + sec->output_offset
2771 + offset);
2772
2773 BFD_ASSERT (dynindx != -1);
2774 outrel.r_info = ELFNN_R_INFO (dynindx, type);
2775 outrel.r_addend = addend;
2776
2777 if (elf_section_data (sec)->stab_info != NULL)
2778 {
2779 /* This may be NULL for linker-generated relocations, as it is
2780 inconvenient to pass all the bits around. And this shouldn't
2781 happen. */
2782 BFD_ASSERT (info != NULL);
2783
2784 offset = (_bfd_stab_section_offset
2785 (abfd, &elf_hash_table (info)->stab_info, sec,
2786 &elf_section_data (sec)->stab_info, offset));
2787 if (offset == (bfd_vma) -1)
2788 {
2789 /* Run for the hills. We shouldn't be outputting a relocation
2790 for this. So do what everyone else does and output a no-op. */
2791 outrel.r_info = ELFNN_R_INFO (0, R_IA64_NONE);
2792 outrel.r_addend = 0;
2793 offset = 0;
2794 }
2795 outrel.r_offset = offset;
2796 }
2797
2798 bfd_elfNN_swap_reloca_out (abfd, &outrel,
2799 ((ElfNN_External_Rela *) srel->contents
2800 + srel->reloc_count++));
2801 BFD_ASSERT (sizeof(ElfNN_External_Rela) * srel->reloc_count
2802 <= srel->_cooked_size);
2803 }
2804
2805 /* Store an entry for target address TARGET_ADDR in the linkage table
2806 and return the gp-relative address of the linkage table entry. */
2807
2808 static bfd_vma
2809 set_got_entry (abfd, info, dyn_i, dynindx, addend, value, dyn_r_type)
2810 bfd *abfd;
2811 struct bfd_link_info *info;
2812 struct elfNN_ia64_dyn_sym_info *dyn_i;
2813 long dynindx;
2814 bfd_vma addend;
2815 bfd_vma value;
2816 unsigned int dyn_r_type;
2817 {
2818 struct elfNN_ia64_link_hash_table *ia64_info;
2819 asection *got_sec;
2820
2821 ia64_info = elfNN_ia64_hash_table (info);
2822 got_sec = ia64_info->got_sec;
2823
2824 BFD_ASSERT ((dyn_i->got_offset & 7) == 0);
2825
2826 if (! dyn_i->got_done)
2827 {
2828 dyn_i->got_done = true;
2829
2830 /* Store the target address in the linkage table entry. */
2831 bfd_put_64 (abfd, value, got_sec->contents + dyn_i->got_offset);
2832
2833 /* Install a dynamic relocation if needed. */
2834 if (info->shared
2835 || elfNN_ia64_dynamic_symbol_p (dyn_i->h, info)
2836 || (dynindx != -1 && dyn_r_type == R_IA64_FPTR64LSB))
2837 {
2838 if (dynindx == -1)
2839 {
2840 dyn_r_type = R_IA64_REL64LSB;
2841 dynindx = 0;
2842 addend = value;
2843 }
2844
2845 if (bfd_big_endian (abfd))
2846 {
2847 switch (dyn_r_type)
2848 {
2849 case R_IA64_REL64LSB:
2850 dyn_r_type = R_IA64_REL64MSB;
2851 break;
2852 case R_IA64_DIR64LSB:
2853 dyn_r_type = R_IA64_DIR64MSB;
2854 break;
2855 case R_IA64_FPTR64LSB:
2856 dyn_r_type = R_IA64_FPTR64MSB;
2857 break;
2858 default:
2859 BFD_ASSERT (false);
2860 break;
2861 }
2862 }
2863
2864 elfNN_ia64_install_dyn_reloc (abfd, NULL, got_sec,
2865 ia64_info->rel_got_sec,
2866 dyn_i->got_offset, dyn_r_type,
2867 dynindx, addend);
2868 }
2869 }
2870
2871 /* Return the address of the linkage table entry. */
2872 value = (got_sec->output_section->vma
2873 + got_sec->output_offset
2874 + dyn_i->got_offset);
2875
2876 return value;
2877 }
2878
2879 /* Fill in a function descriptor consisting of the function's code
2880 address and its global pointer. Return the descriptor's address. */
2881
2882 static bfd_vma
2883 set_fptr_entry (abfd, info, dyn_i, value)
2884 bfd *abfd;
2885 struct bfd_link_info *info;
2886 struct elfNN_ia64_dyn_sym_info *dyn_i;
2887 bfd_vma value;
2888 {
2889 struct elfNN_ia64_link_hash_table *ia64_info;
2890 asection *fptr_sec;
2891
2892 ia64_info = elfNN_ia64_hash_table (info);
2893 fptr_sec = ia64_info->fptr_sec;
2894
2895 if (!dyn_i->fptr_done)
2896 {
2897 dyn_i->fptr_done = 1;
2898
2899 /* Fill in the function descriptor. */
2900 bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
2901 bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
2902 fptr_sec->contents + dyn_i->fptr_offset + 8);
2903 }
2904
2905 /* Return the descriptor's address. */
2906 value = (fptr_sec->output_section->vma
2907 + fptr_sec->output_offset
2908 + dyn_i->fptr_offset);
2909
2910 return value;
2911 }
2912
2913 /* Fill in a PLTOFF entry consisting of the function's code address
2914 and its global pointer. Return the descriptor's address. */
2915
2916 static bfd_vma
2917 set_pltoff_entry (abfd, info, dyn_i, value, is_plt)
2918 bfd *abfd;
2919 struct bfd_link_info *info;
2920 struct elfNN_ia64_dyn_sym_info *dyn_i;
2921 bfd_vma value;
2922 boolean is_plt;
2923 {
2924 struct elfNN_ia64_link_hash_table *ia64_info;
2925 asection *pltoff_sec;
2926
2927 ia64_info = elfNN_ia64_hash_table (info);
2928 pltoff_sec = ia64_info->pltoff_sec;
2929
2930 /* Don't do anything if this symbol uses a real PLT entry. In
2931 that case, we'll fill this in during finish_dynamic_symbol. */
2932 if ((! dyn_i->want_plt || is_plt)
2933 && !dyn_i->pltoff_done)
2934 {
2935 bfd_vma gp = _bfd_get_gp_value (abfd);
2936
2937 /* Fill in the function descriptor. */
2938 bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
2939 bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
2940
2941 /* Install dynamic relocations if needed. */
2942 if (!is_plt && info->shared)
2943 {
2944 unsigned int dyn_r_type;
2945
2946 if (bfd_big_endian (abfd))
2947 dyn_r_type = R_IA64_REL64MSB;
2948 else
2949 dyn_r_type = R_IA64_REL64LSB;
2950
2951 elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
2952 ia64_info->rel_pltoff_sec,
2953 dyn_i->pltoff_offset,
2954 dyn_r_type, 0, value);
2955 elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
2956 ia64_info->rel_pltoff_sec,
2957 dyn_i->pltoff_offset + 8,
2958 dyn_r_type, 0, gp);
2959 }
2960
2961 dyn_i->pltoff_done = 1;
2962 }
2963
2964 /* Return the descriptor's address. */
2965 value = (pltoff_sec->output_section->vma
2966 + pltoff_sec->output_offset
2967 + dyn_i->pltoff_offset);
2968
2969 return value;
2970 }
2971
2972 /* Called through qsort to sort the .IA_64.unwind section during a
2973 non-relocatable link. Set elfNN_ia64_unwind_entry_compare_bfd
2974 to the output bfd so we can do proper endianness frobbing. */
2975
2976 static bfd *elfNN_ia64_unwind_entry_compare_bfd;
2977
2978 static int
2979 elfNN_ia64_unwind_entry_compare (a, b)
2980 PTR a;
2981 PTR b;
2982 {
2983 bfd_vma av, bv;
2984
2985 av = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, a);
2986 bv = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, b);
2987
2988 return (av < bv ? -1 : av > bv ? 1 : 0);
2989 }
2990
2991 static boolean
2992 elfNN_ia64_final_link (abfd, info)
2993 bfd *abfd;
2994 struct bfd_link_info *info;
2995 {
2996 struct elfNN_ia64_link_hash_table *ia64_info;
2997 asection *unwind_output_sec;
2998
2999 ia64_info = elfNN_ia64_hash_table (info);
3000
3001 /* Make sure we've got ourselves a nice fat __gp value. */
3002 if (!info->relocateable)
3003 {
3004 bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
3005 bfd_vma min_short_vma = min_vma, max_short_vma = 0;
3006 struct elf_link_hash_entry *gp;
3007 bfd_vma gp_val;
3008 asection *os;
3009
3010 /* Find the min and max vma of all sections marked short. Also
3011 collect min and max vma of any type, for use in selecting a
3012 nice gp. */
3013 for (os = abfd->sections; os ; os = os->next)
3014 {
3015 bfd_vma lo, hi;
3016
3017 if ((os->flags & SEC_ALLOC) == 0)
3018 continue;
3019
3020 lo = os->vma;
3021 hi = os->vma + os->_raw_size;
3022 if (hi < lo)
3023 hi = (bfd_vma) -1;
3024
3025 if (min_vma > lo)
3026 min_vma = lo;
3027 if (max_vma < hi)
3028 max_vma = hi;
3029 if (os->flags & SEC_SMALL_DATA)
3030 {
3031 if (min_short_vma > lo)
3032 min_short_vma = lo;
3033 if (max_short_vma < hi)
3034 max_short_vma = hi;
3035 }
3036 }
3037
3038 /* See if the user wants to force a value. */
3039 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
3040 false, false);
3041
3042 if (gp
3043 && (gp->root.type == bfd_link_hash_defined
3044 || gp->root.type == bfd_link_hash_defweak))
3045 {
3046 asection *gp_sec = gp->root.u.def.section;
3047 gp_val = (gp->root.u.def.value
3048 + gp_sec->output_section->vma
3049 + gp_sec->output_offset);
3050 }
3051 else
3052 {
3053 /* Pick a sensible value. */
3054
3055 asection *got_sec = ia64_info->got_sec;
3056
3057 /* Start with just the address of the .got. */
3058 if (got_sec)
3059 gp_val = got_sec->output_section->vma;
3060 else if (max_short_vma != 0)
3061 gp_val = min_short_vma;
3062 else
3063 gp_val = min_vma;
3064
3065 /* If it is possible to address the entire image, but we
3066 don't with the choice above, adjust. */
3067 if (max_vma - min_vma < 0x400000
3068 && max_vma - gp_val <= 0x200000
3069 && gp_val - min_vma > 0x200000)
3070 gp_val = min_vma + 0x200000;
3071 else if (max_short_vma != 0)
3072 {
3073 /* If we don't cover all the short data, adjust. */
3074 if (max_short_vma - gp_val >= 0x200000)
3075 gp_val = min_short_vma + 0x200000;
3076
3077 /* If we're addressing stuff past the end, adjust back. */
3078 if (gp_val > max_vma)
3079 gp_val = max_vma - 0x200000 + 8;
3080 }
3081 }
3082
3083 /* Validate whether all SHF_IA_64_SHORT sections are within
3084 range of the chosen GP. */
3085
3086 if (max_short_vma != 0)
3087 {
3088 if (max_short_vma - min_short_vma >= 0x400000)
3089 {
3090 (*_bfd_error_handler)
3091 (_("%s: short data segment overflowed (0x%lx >= 0x400000)"),
3092 bfd_get_filename (abfd),
3093 (unsigned long)(max_short_vma - min_short_vma));
3094 return false;
3095 }
3096 else if ((gp_val > min_short_vma
3097 && gp_val - min_short_vma > 0x200000)
3098 || (gp_val < max_short_vma
3099 && max_short_vma - gp_val >= 0x200000))
3100 {
3101 (*_bfd_error_handler)
3102 (_("%s: __gp does not cover short data segment"),
3103 bfd_get_filename (abfd));
3104 return false;
3105 }
3106 }
3107
3108 _bfd_set_gp_value (abfd, gp_val);
3109 }
3110
3111 /* Tricky bits. DT_INIT and DT_FINI use a pltoff entry, which is
3112 normally initialized in finish_dynamic_sections. Except that
3113 we need all non-plt pltoff entries to be initialized before
3114 finish_dynamic_symbols. This because the array of relocations
3115 used for plt entries (aka DT_JMPREL) begins after all the
3116 non-plt pltoff relocations. If the order gets confused, we
3117 munge either the array or the array base. */
3118 if (ia64_info->root.dynamic_sections_created)
3119 {
3120 struct elf_link_hash_entry *h;
3121 struct elfNN_ia64_dyn_sym_info *dyn_i;
3122 bfd_vma addr;
3123
3124 if (info->init_function
3125 && (h = elf_link_hash_lookup (elf_hash_table (info),
3126 info->init_function, false,
3127 false, false))
3128 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3129 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3130 {
3131 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3132 addr = (h->root.u.def.section->output_section->vma
3133 + h->root.u.def.section->output_offset
3134 + h->root.u.def.value);
3135 (void) set_pltoff_entry (abfd, info, dyn_i, addr, false);
3136 }
3137
3138 if (info->fini_function
3139 && (h = elf_link_hash_lookup (elf_hash_table (info),
3140 info->fini_function, false,
3141 false, false))
3142 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3143 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3144 {
3145 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3146 addr = (h->root.u.def.section->output_section->vma
3147 + h->root.u.def.section->output_offset
3148 + h->root.u.def.value);
3149 (void) set_pltoff_entry (abfd, info, dyn_i, addr, false);
3150 }
3151 }
3152
3153 /* If we're producing a final executable, we need to sort the contents
3154 of the .IA_64.unwind section. Force this section to be relocated
3155 into memory rather than written immediately to the output file. */
3156 unwind_output_sec = NULL;
3157 if (!info->relocateable)
3158 {
3159 asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
3160 if (s)
3161 {
3162 unwind_output_sec = s->output_section;
3163 unwind_output_sec->contents
3164 = bfd_malloc (unwind_output_sec->_raw_size);
3165 if (unwind_output_sec->contents == NULL)
3166 return false;
3167 }
3168 }
3169
3170 /* Invoke the regular ELF backend linker to do all the work. */
3171 if (!bfd_elfNN_bfd_final_link (abfd, info))
3172 return false;
3173
3174 if (unwind_output_sec)
3175 {
3176 elfNN_ia64_unwind_entry_compare_bfd = abfd;
3177 qsort (unwind_output_sec->contents, unwind_output_sec->_raw_size / 24,
3178 24, elfNN_ia64_unwind_entry_compare);
3179
3180 if (! bfd_set_section_contents (abfd, unwind_output_sec,
3181 unwind_output_sec->contents, 0,
3182 unwind_output_sec->_raw_size))
3183 return false;
3184 }
3185
3186 return true;
3187 }
3188
3189 static boolean
3190 elfNN_ia64_relocate_section (output_bfd, info, input_bfd, input_section,
3191 contents, relocs, local_syms, local_sections)
3192 bfd *output_bfd;
3193 struct bfd_link_info *info;
3194 bfd *input_bfd;
3195 asection *input_section;
3196 bfd_byte *contents;
3197 Elf_Internal_Rela *relocs;
3198 Elf_Internal_Sym *local_syms;
3199 asection **local_sections;
3200 {
3201 struct elfNN_ia64_link_hash_table *ia64_info;
3202 Elf_Internal_Shdr *symtab_hdr;
3203 Elf_Internal_Rela *rel;
3204 Elf_Internal_Rela *relend;
3205 asection *srel;
3206 boolean ret_val = true; /* for non-fatal errors */
3207 bfd_vma gp_val;
3208
3209 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3210 ia64_info = elfNN_ia64_hash_table (info);
3211
3212 /* Infect various flags from the input section to the output section. */
3213 if (info->relocateable)
3214 {
3215 bfd_vma flags;
3216
3217 flags = elf_section_data(input_section)->this_hdr.sh_flags;
3218 flags &= SHF_IA_64_NORECOV;
3219
3220 elf_section_data(input_section->output_section)
3221 ->this_hdr.sh_flags |= flags;
3222 }
3223
3224 gp_val = _bfd_get_gp_value (output_bfd);
3225 srel = get_reloc_section (input_bfd, ia64_info, input_section, false);
3226
3227 rel = relocs;
3228 relend = relocs + input_section->reloc_count;
3229 for (; rel < relend; ++rel)
3230 {
3231 struct elf_link_hash_entry *h;
3232 struct elfNN_ia64_dyn_sym_info *dyn_i;
3233 bfd_reloc_status_type r;
3234 reloc_howto_type *howto;
3235 unsigned long r_symndx;
3236 Elf_Internal_Sym *sym;
3237 unsigned int r_type;
3238 bfd_vma value;
3239 asection *sym_sec;
3240 bfd_byte *hit_addr;
3241 boolean dynamic_symbol_p;
3242 boolean undef_weak_ref;
3243
3244 r_type = ELFNN_R_TYPE (rel->r_info);
3245 if (r_type > R_IA64_MAX_RELOC_CODE)
3246 {
3247 (*_bfd_error_handler)
3248 (_("%s: unknown relocation type %d"),
3249 bfd_get_filename (input_bfd), (int)r_type);
3250 bfd_set_error (bfd_error_bad_value);
3251 ret_val = false;
3252 continue;
3253 }
3254 howto = lookup_howto (r_type);
3255 r_symndx = ELFNN_R_SYM (rel->r_info);
3256
3257 if (info->relocateable)
3258 {
3259 /* This is a relocateable link. We don't have to change
3260 anything, unless the reloc is against a section symbol,
3261 in which case we have to adjust according to where the
3262 section symbol winds up in the output section. */
3263 if (r_symndx < symtab_hdr->sh_info)
3264 {
3265 sym = local_syms + r_symndx;
3266 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3267 {
3268 sym_sec = local_sections[r_symndx];
3269 rel->r_addend += sym_sec->output_offset;
3270 }
3271 }
3272 continue;
3273 }
3274
3275 /* This is a final link. */
3276
3277 h = NULL;
3278 sym = NULL;
3279 sym_sec = NULL;
3280 undef_weak_ref = false;
3281
3282 if (r_symndx < symtab_hdr->sh_info)
3283 {
3284 /* Reloc against local symbol. */
3285 sym = local_syms + r_symndx;
3286 sym_sec = local_sections[r_symndx];
3287 value = (sym_sec->output_section->vma
3288 + sym_sec->output_offset
3289 + sym->st_value);
3290 }
3291 else
3292 {
3293 long indx;
3294
3295 /* Reloc against global symbol. */
3296 indx = r_symndx - symtab_hdr->sh_info;
3297 h = elf_sym_hashes (input_bfd)[indx];
3298 while (h->root.type == bfd_link_hash_indirect
3299 || h->root.type == bfd_link_hash_warning)
3300 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3301
3302 value = 0;
3303 if (h->root.type == bfd_link_hash_defined
3304 || h->root.type == bfd_link_hash_defweak)
3305 {
3306 sym_sec = h->root.u.def.section;
3307
3308 /* Detect the cases that sym_sec->output_section is
3309 expected to be NULL -- all cases in which the symbol
3310 is defined in another shared module. This includes
3311 PLT relocs for which we've created a PLT entry and
3312 other relocs for which we're prepared to create
3313 dynamic relocations. */
3314 /* ??? Just accept it NULL and continue. */
3315
3316 if (sym_sec->output_section != NULL)
3317 {
3318 value = (h->root.u.def.value
3319 + sym_sec->output_section->vma
3320 + sym_sec->output_offset);
3321 }
3322 }
3323 else if (h->root.type == bfd_link_hash_undefweak)
3324 undef_weak_ref = true;
3325 else if (info->shared && !info->symbolic
3326 && !info->no_undefined
3327 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3328 ;
3329 else
3330 {
3331 if (! ((*info->callbacks->undefined_symbol)
3332 (info, h->root.root.string, input_bfd,
3333 input_section, rel->r_offset,
3334 (!info->shared || info->no_undefined
3335 || ELF_ST_VISIBILITY (h->other)))))
3336 return false;
3337 ret_val = false;
3338 continue;
3339 }
3340 }
3341
3342 hit_addr = contents + rel->r_offset;
3343 value += rel->r_addend;
3344 dynamic_symbol_p = elfNN_ia64_dynamic_symbol_p (h, info);
3345
3346 switch (r_type)
3347 {
3348 case R_IA64_NONE:
3349 case R_IA64_LDXMOV:
3350 continue;
3351
3352 case R_IA64_IMM14:
3353 case R_IA64_IMM22:
3354 case R_IA64_IMM64:
3355 case R_IA64_DIR32MSB:
3356 case R_IA64_DIR32LSB:
3357 case R_IA64_DIR64MSB:
3358 case R_IA64_DIR64LSB:
3359 /* Install a dynamic relocation for this reloc. */
3360 if ((dynamic_symbol_p || info->shared)
3361 && (input_section->flags & SEC_ALLOC) != 0)
3362 {
3363 unsigned int dyn_r_type;
3364 long dynindx;
3365 bfd_vma addend;
3366
3367 BFD_ASSERT (srel != NULL);
3368
3369 /* If we don't need dynamic symbol lookup, find a
3370 matching RELATIVE relocation. */
3371 dyn_r_type = r_type;
3372 if (dynamic_symbol_p)
3373 {
3374 dynindx = h->dynindx;
3375 addend = rel->r_addend;
3376 value = 0;
3377 }
3378 else
3379 {
3380 switch (r_type)
3381 {
3382 case R_IA64_DIR32MSB:
3383 dyn_r_type = R_IA64_REL32MSB;
3384 break;
3385 case R_IA64_DIR32LSB:
3386 dyn_r_type = R_IA64_REL32LSB;
3387 break;
3388 case R_IA64_DIR64MSB:
3389 dyn_r_type = R_IA64_REL64MSB;
3390 break;
3391 case R_IA64_DIR64LSB:
3392 dyn_r_type = R_IA64_REL64LSB;
3393 break;
3394
3395 default:
3396 /* We can't represent this without a dynamic symbol.
3397 Adjust the relocation to be against an output
3398 section symbol, which are always present in the
3399 dynamic symbol table. */
3400 /* ??? People shouldn't be doing non-pic code in
3401 shared libraries. Hork. */
3402 (*_bfd_error_handler)
3403 (_("%s: linking non-pic code in a shared library"),
3404 bfd_get_filename (input_bfd));
3405 ret_val = false;
3406 continue;
3407 }
3408 dynindx = 0;
3409 addend = value;
3410 }
3411
3412 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
3413 srel, rel->r_offset, dyn_r_type,
3414 dynindx, addend);
3415 }
3416 /* FALLTHRU */
3417
3418 case R_IA64_LTV32MSB:
3419 case R_IA64_LTV32LSB:
3420 case R_IA64_LTV64MSB:
3421 case R_IA64_LTV64LSB:
3422 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3423 break;
3424
3425 case R_IA64_GPREL22:
3426 case R_IA64_GPREL64I:
3427 case R_IA64_GPREL32MSB:
3428 case R_IA64_GPREL32LSB:
3429 case R_IA64_GPREL64MSB:
3430 case R_IA64_GPREL64LSB:
3431 if (dynamic_symbol_p)
3432 {
3433 (*_bfd_error_handler)
3434 (_("%s: @gprel relocation against dynamic symbol %s"),
3435 bfd_get_filename (input_bfd), h->root.root.string);
3436 ret_val = false;
3437 continue;
3438 }
3439 value -= gp_val;
3440 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3441 break;
3442
3443 case R_IA64_LTOFF22:
3444 case R_IA64_LTOFF22X:
3445 case R_IA64_LTOFF64I:
3446 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3447 value = set_got_entry (input_bfd, info, dyn_i, (h ? h->dynindx : -1),
3448 rel->r_addend, value, R_IA64_DIR64LSB);
3449 value -= gp_val;
3450 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3451 break;
3452
3453 case R_IA64_PLTOFF22:
3454 case R_IA64_PLTOFF64I:
3455 case R_IA64_PLTOFF64MSB:
3456 case R_IA64_PLTOFF64LSB:
3457 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3458 value = set_pltoff_entry (output_bfd, info, dyn_i, value, false);
3459 value -= gp_val;
3460 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3461 break;
3462
3463 case R_IA64_FPTR64I:
3464 case R_IA64_FPTR32MSB:
3465 case R_IA64_FPTR32LSB:
3466 case R_IA64_FPTR64MSB:
3467 case R_IA64_FPTR64LSB:
3468 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3469 if (dyn_i->want_fptr)
3470 {
3471 if (!undef_weak_ref)
3472 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3473 }
3474 else
3475 {
3476 long dynindx;
3477
3478 /* Otherwise, we expect the dynamic linker to create
3479 the entry. */
3480
3481 if (h)
3482 {
3483 if (h->dynindx != -1)
3484 dynindx = h->dynindx;
3485 else
3486 dynindx = (_bfd_elf_link_lookup_local_dynindx
3487 (info, h->root.u.def.section->owner,
3488 global_sym_index (h)));
3489 }
3490 else
3491 {
3492 dynindx = (_bfd_elf_link_lookup_local_dynindx
3493 (info, input_bfd, r_symndx));
3494 }
3495
3496 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
3497 srel, rel->r_offset, r_type,
3498 dynindx, rel->r_addend);
3499 value = 0;
3500 }
3501
3502 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3503 break;
3504
3505 case R_IA64_LTOFF_FPTR22:
3506 case R_IA64_LTOFF_FPTR64I:
3507 case R_IA64_LTOFF_FPTR64MSB:
3508 case R_IA64_LTOFF_FPTR64LSB:
3509 {
3510 long dynindx;
3511
3512 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3513 if (dyn_i->want_fptr)
3514 {
3515 BFD_ASSERT (h == NULL || h->dynindx == -1)
3516 if (!undef_weak_ref)
3517 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3518 dynindx = -1;
3519 }
3520 else
3521 {
3522 /* Otherwise, we expect the dynamic linker to create
3523 the entry. */
3524 if (h)
3525 {
3526 if (h->dynindx != -1)
3527 dynindx = h->dynindx;
3528 else
3529 dynindx = (_bfd_elf_link_lookup_local_dynindx
3530 (info, h->root.u.def.section->owner,
3531 global_sym_index (h)));
3532 }
3533 else
3534 dynindx = (_bfd_elf_link_lookup_local_dynindx
3535 (info, input_bfd, r_symndx));
3536 value = 0;
3537 }
3538
3539 value = set_got_entry (output_bfd, info, dyn_i, dynindx,
3540 rel->r_addend, value, R_IA64_FPTR64LSB);
3541 value -= gp_val;
3542 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3543 }
3544 break;
3545
3546 case R_IA64_PCREL32MSB:
3547 case R_IA64_PCREL32LSB:
3548 case R_IA64_PCREL64MSB:
3549 case R_IA64_PCREL64LSB:
3550 /* Install a dynamic relocation for this reloc. */
3551 if (dynamic_symbol_p)
3552 {
3553 BFD_ASSERT (srel != NULL);
3554
3555 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
3556 srel, rel->r_offset, r_type,
3557 h->dynindx, rel->r_addend);
3558 }
3559 goto finish_pcrel;
3560
3561 case R_IA64_PCREL21BI:
3562 case R_IA64_PCREL21F:
3563 case R_IA64_PCREL21M:
3564 /* ??? These two are only used for speculation fixup code.
3565 They should never be dynamic. */
3566 if (dynamic_symbol_p)
3567 {
3568 (*_bfd_error_handler)
3569 (_("%s: dynamic relocation against speculation fixup"),
3570 bfd_get_filename (input_bfd));
3571 ret_val = false;
3572 continue;
3573 }
3574 if (undef_weak_ref)
3575 {
3576 (*_bfd_error_handler)
3577 (_("%s: speculation fixup against undefined weak symbol"),
3578 bfd_get_filename (input_bfd));
3579 ret_val = false;
3580 continue;
3581 }
3582 goto finish_pcrel;
3583
3584 case R_IA64_PCREL21B:
3585 case R_IA64_PCREL60B:
3586 /* We should have created a PLT entry for any dynamic symbol. */
3587 dyn_i = NULL;
3588 if (h)
3589 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3590
3591 if (dyn_i && dyn_i->want_plt2)
3592 {
3593 /* Should have caught this earlier. */
3594 BFD_ASSERT (rel->r_addend == 0);
3595
3596 value = (ia64_info->plt_sec->output_section->vma
3597 + ia64_info->plt_sec->output_offset
3598 + dyn_i->plt2_offset);
3599 }
3600 else
3601 {
3602 /* Since there's no PLT entry, Validate that this is
3603 locally defined. */
3604 BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
3605
3606 /* If the symbol is undef_weak, we shouldn't be trying
3607 to call it. There's every chance that we'd wind up
3608 with an out-of-range fixup here. Don't bother setting
3609 any value at all. */
3610 if (undef_weak_ref)
3611 continue;
3612 }
3613 goto finish_pcrel;
3614
3615 case R_IA64_PCREL22:
3616 case R_IA64_PCREL64I:
3617 finish_pcrel:
3618 /* Make pc-relative. */
3619 value -= (input_section->output_section->vma
3620 + input_section->output_offset
3621 + rel->r_offset) & ~ (bfd_vma) 0x3;
3622 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3623 break;
3624
3625 case R_IA64_SEGREL32MSB:
3626 case R_IA64_SEGREL32LSB:
3627 case R_IA64_SEGREL64MSB:
3628 case R_IA64_SEGREL64LSB:
3629 {
3630 struct elf_segment_map *m;
3631 Elf_Internal_Phdr *p;
3632
3633 /* Find the segment that contains the output_section. */
3634 for (m = elf_tdata (output_bfd)->segment_map,
3635 p = elf_tdata (output_bfd)->phdr;
3636 m != NULL;
3637 m = m->next, p++)
3638 {
3639 int i;
3640 for (i = m->count - 1; i >= 0; i--)
3641 if (m->sections[i] == sym_sec->output_section)
3642 break;
3643 if (i >= 0)
3644 break;
3645 }
3646
3647 if (m == NULL)
3648 {
3649 /* If the input section was discarded from the output, then
3650 do nothing. */
3651
3652 if (bfd_is_abs_section (sym_sec->output_section))
3653 r = bfd_reloc_ok;
3654 else
3655 r = bfd_reloc_notsupported;
3656 }
3657 else
3658 {
3659 /* The VMA of the segment is the vaddr of the associated
3660 program header. */
3661 if (value > p->p_vaddr)
3662 value -= p->p_vaddr;
3663 else
3664 value = 0;
3665 r = elfNN_ia64_install_value (output_bfd, hit_addr, value,
3666 r_type);
3667 }
3668 break;
3669 }
3670
3671 case R_IA64_SECREL32MSB:
3672 case R_IA64_SECREL32LSB:
3673 case R_IA64_SECREL64MSB:
3674 case R_IA64_SECREL64LSB:
3675 /* Make output-section relative. */
3676 if (value > input_section->output_section->vma)
3677 value -= input_section->output_section->vma;
3678 else
3679 value = 0;
3680 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3681 break;
3682
3683 case R_IA64_IPLTMSB:
3684 case R_IA64_IPLTLSB:
3685 /* Install a dynamic relocation for this reloc. */
3686 if ((dynamic_symbol_p || info->shared)
3687 && (input_section->flags & SEC_ALLOC) != 0)
3688 {
3689 long dynindx;
3690
3691 BFD_ASSERT (srel != NULL);
3692
3693 /* If we don't need dynamic symbol lookup, install two
3694 RELATIVE relocations. */
3695 if (! dynamic_symbol_p)
3696 {
3697 unsigned int dyn_r_type;
3698
3699 if (r_type == R_IA64_IPLTMSB)
3700 dyn_r_type = R_IA64_REL64MSB;
3701 else
3702 dyn_r_type = R_IA64_REL64LSB;
3703
3704 elfNN_ia64_install_dyn_reloc (output_bfd, info,
3705 input_section,
3706 srel, rel->r_offset,
3707 dyn_r_type, 0, value);
3708 elfNN_ia64_install_dyn_reloc (output_bfd, info,
3709 input_section,
3710 srel, rel->r_offset + 8,
3711 dyn_r_type, 0, gp_val);
3712 }
3713 else
3714 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
3715 srel, rel->r_offset, r_type,
3716 h->dynindx, rel->r_addend);
3717 }
3718
3719 if (r_type == R_IA64_IPLTMSB)
3720 r_type = R_IA64_DIR64MSB;
3721 else
3722 r_type = R_IA64_DIR64LSB;
3723 elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3724 r = elfNN_ia64_install_value (output_bfd, hit_addr + 8, gp_val,
3725 r_type);
3726 break;
3727
3728 default:
3729 r = bfd_reloc_notsupported;
3730 break;
3731 }
3732
3733 switch (r)
3734 {
3735 case bfd_reloc_ok:
3736 break;
3737
3738 case bfd_reloc_undefined:
3739 /* This can happen for global table relative relocs if
3740 __gp is undefined. This is a panic situation so we
3741 don't try to continue. */
3742 (*info->callbacks->undefined_symbol)
3743 (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
3744 return false;
3745
3746 case bfd_reloc_notsupported:
3747 {
3748 const char *name;
3749
3750 if (h)
3751 name = h->root.root.string;
3752 else
3753 {
3754 name = bfd_elf_string_from_elf_section (input_bfd,
3755 symtab_hdr->sh_link,
3756 sym->st_name);
3757 if (name == NULL)
3758 return false;
3759 if (*name == '\0')
3760 name = bfd_section_name (input_bfd, input_section);
3761 }
3762 if (!(*info->callbacks->warning) (info, _("unsupported reloc"),
3763 name, input_bfd,
3764 input_section, rel->r_offset))
3765 return false;
3766 ret_val = false;
3767 }
3768 break;
3769
3770 case bfd_reloc_dangerous:
3771 case bfd_reloc_outofrange:
3772 case bfd_reloc_overflow:
3773 default:
3774 {
3775 const char *name;
3776
3777 if (h)
3778 name = h->root.root.string;
3779 else
3780 {
3781 name = bfd_elf_string_from_elf_section (input_bfd,
3782 symtab_hdr->sh_link,
3783 sym->st_name);
3784 if (name == NULL)
3785 return false;
3786 if (*name == '\0')
3787 name = bfd_section_name (input_bfd, input_section);
3788 }
3789 if (!(*info->callbacks->reloc_overflow) (info, name,
3790 howto->name, 0,
3791 input_bfd,
3792 input_section,
3793 rel->r_offset))
3794 return false;
3795 ret_val = false;
3796 }
3797 break;
3798 }
3799 }
3800
3801 return ret_val;
3802 }
3803
3804 static boolean
3805 elfNN_ia64_finish_dynamic_symbol (output_bfd, info, h, sym)
3806 bfd *output_bfd;
3807 struct bfd_link_info *info;
3808 struct elf_link_hash_entry *h;
3809 Elf_Internal_Sym *sym;
3810 {
3811 struct elfNN_ia64_link_hash_table *ia64_info;
3812 struct elfNN_ia64_dyn_sym_info *dyn_i;
3813
3814 ia64_info = elfNN_ia64_hash_table (info);
3815 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3816
3817 /* Fill in the PLT data, if required. */
3818 if (dyn_i && dyn_i->want_plt)
3819 {
3820 Elf_Internal_Rela outrel;
3821 bfd_byte *loc;
3822 asection *plt_sec;
3823 bfd_vma plt_addr, pltoff_addr, gp_val, index;
3824 ElfNN_External_Rela *rel;
3825
3826 gp_val = _bfd_get_gp_value (output_bfd);
3827
3828 /* Initialize the minimal PLT entry. */
3829
3830 index = (dyn_i->plt_offset - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
3831 plt_sec = ia64_info->plt_sec;
3832 loc = plt_sec->contents + dyn_i->plt_offset;
3833
3834 memcpy (loc, plt_min_entry, PLT_MIN_ENTRY_SIZE);
3835 elfNN_ia64_install_value (output_bfd, loc, index, R_IA64_IMM22);
3836 elfNN_ia64_install_value (output_bfd, loc+2, -dyn_i->plt_offset,
3837 R_IA64_PCREL21B);
3838
3839 plt_addr = (plt_sec->output_section->vma
3840 + plt_sec->output_offset
3841 + dyn_i->plt_offset);
3842 pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, true);
3843
3844 /* Initialize the FULL PLT entry, if needed. */
3845 if (dyn_i->want_plt2)
3846 {
3847 loc = plt_sec->contents + dyn_i->plt2_offset;
3848
3849 memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
3850 elfNN_ia64_install_value (output_bfd, loc, pltoff_addr - gp_val,
3851 R_IA64_IMM22);
3852
3853 /* Mark the symbol as undefined, rather than as defined in the
3854 plt section. Leave the value alone. */
3855 /* ??? We didn't redefine it in adjust_dynamic_symbol in the
3856 first place. But perhaps elflink.h did some for us. */
3857 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3858 sym->st_shndx = SHN_UNDEF;
3859 }
3860
3861 /* Create the dynamic relocation. */
3862 outrel.r_offset = pltoff_addr;
3863 if (bfd_little_endian (output_bfd))
3864 outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTLSB);
3865 else
3866 outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTMSB);
3867 outrel.r_addend = 0;
3868
3869 /* This is fun. In the .IA_64.pltoff section, we've got entries
3870 that correspond both to real PLT entries, and those that
3871 happened to resolve to local symbols but need to be created
3872 to satisfy @pltoff relocations. The .rela.IA_64.pltoff
3873 relocations for the real PLT should come at the end of the
3874 section, so that they can be indexed by plt entry at runtime.
3875
3876 We emitted all of the relocations for the non-PLT @pltoff
3877 entries during relocate_section. So we can consider the
3878 existing sec->reloc_count to be the base of the array of
3879 PLT relocations. */
3880
3881 rel = (ElfNN_External_Rela *)ia64_info->rel_pltoff_sec->contents;
3882 rel += ia64_info->rel_pltoff_sec->reloc_count;
3883
3884 bfd_elfNN_swap_reloca_out (output_bfd, &outrel, rel + index);
3885 }
3886
3887 /* Mark some specially defined symbols as absolute. */
3888 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3889 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3890 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3891 sym->st_shndx = SHN_ABS;
3892
3893 return true;
3894 }
3895
3896 static boolean
3897 elfNN_ia64_finish_dynamic_sections (abfd, info)
3898 bfd *abfd;
3899 struct bfd_link_info *info;
3900 {
3901 struct elfNN_ia64_link_hash_table *ia64_info;
3902 bfd *dynobj;
3903
3904 ia64_info = elfNN_ia64_hash_table (info);
3905 dynobj = ia64_info->root.dynobj;
3906
3907 if (elf_hash_table (info)->dynamic_sections_created)
3908 {
3909 ElfNN_External_Dyn *dyncon, *dynconend;
3910 asection *sdyn, *sgotplt;
3911 bfd_vma gp_val;
3912
3913 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3914 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3915 BFD_ASSERT (sdyn != NULL);
3916 dyncon = (ElfNN_External_Dyn *) sdyn->contents;
3917 dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3918
3919 gp_val = _bfd_get_gp_value (abfd);
3920
3921 for (; dyncon < dynconend; dyncon++)
3922 {
3923 Elf_Internal_Dyn dyn;
3924
3925 bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
3926
3927 switch (dyn.d_tag)
3928 {
3929 case DT_PLTGOT:
3930 dyn.d_un.d_ptr = gp_val;
3931 break;
3932
3933 case DT_PLTRELSZ:
3934 dyn.d_un.d_val = (ia64_info->minplt_entries
3935 * sizeof (ElfNN_External_Rela));
3936 break;
3937
3938 case DT_JMPREL:
3939 /* See the comment above in finish_dynamic_symbol. */
3940 dyn.d_un.d_ptr = (ia64_info->rel_pltoff_sec->output_section->vma
3941 + ia64_info->rel_pltoff_sec->output_offset
3942 + (ia64_info->rel_pltoff_sec->reloc_count
3943 * sizeof (ElfNN_External_Rela)));
3944 break;
3945
3946 case DT_IA_64_PLT_RESERVE:
3947 dyn.d_un.d_ptr = (sgotplt->output_section->vma
3948 + sgotplt->output_offset);
3949 break;
3950
3951 case DT_RELASZ:
3952 /* Do not have RELASZ include JMPREL. This makes things
3953 easier on ld.so. This is not what the rest of BFD set up. */
3954 dyn.d_un.d_val -= (ia64_info->minplt_entries
3955 * sizeof (ElfNN_External_Rela));
3956 break;
3957
3958 case DT_INIT:
3959 case DT_FINI:
3960 {
3961 struct elf_link_hash_entry *h;
3962 struct elfNN_ia64_dyn_sym_info *dyn_i;
3963 const char *which;
3964
3965 if (dyn.d_tag == DT_INIT)
3966 which = info->init_function;
3967 else
3968 which = info->fini_function;
3969
3970 h = elf_link_hash_lookup (elf_hash_table (info), which,
3971 false, false, false);
3972 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3973 dyn.d_un.d_ptr = set_pltoff_entry (abfd, info, dyn_i,
3974 dyn.d_un.d_ptr, 0);
3975 }
3976 }
3977
3978 bfd_elfNN_swap_dyn_out (abfd, &dyn, dyncon);
3979 }
3980
3981 /* Initialize the PLT0 entry */
3982 if (ia64_info->plt_sec)
3983 {
3984 bfd_byte *loc = ia64_info->plt_sec->contents;
3985 bfd_vma pltres;
3986
3987 memcpy (loc, plt_header, PLT_HEADER_SIZE);
3988
3989 pltres = (sgotplt->output_section->vma
3990 + sgotplt->output_offset
3991 - gp_val);
3992
3993 elfNN_ia64_install_value (abfd, loc+1, pltres, R_IA64_GPREL22);
3994 }
3995 }
3996
3997 return true;
3998 }
3999 \f
4000 /* ELF file flag handling: */
4001
4002 /* Function to keep IA-64 specific file flags. */
4003 static boolean
4004 elfNN_ia64_set_private_flags (abfd, flags)
4005 bfd *abfd;
4006 flagword flags;
4007 {
4008 BFD_ASSERT (!elf_flags_init (abfd)
4009 || elf_elfheader (abfd)->e_flags == flags);
4010
4011 elf_elfheader (abfd)->e_flags = flags;
4012 elf_flags_init (abfd) = true;
4013 return true;
4014 }
4015
4016 /* Copy backend specific data from one object module to another */
4017 static boolean
4018 elfNN_ia64_copy_private_bfd_data (ibfd, obfd)
4019 bfd *ibfd, *obfd;
4020 {
4021 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4022 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4023 return true;
4024
4025 BFD_ASSERT (!elf_flags_init (obfd)
4026 || (elf_elfheader (obfd)->e_flags
4027 == elf_elfheader (ibfd)->e_flags));
4028
4029 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
4030 elf_flags_init (obfd) = true;
4031 return true;
4032 }
4033
4034 /* Merge backend specific data from an object file to the output
4035 object file when linking. */
4036 static boolean
4037 elfNN_ia64_merge_private_bfd_data (ibfd, obfd)
4038 bfd *ibfd, *obfd;
4039 {
4040 flagword out_flags;
4041 flagword in_flags;
4042 boolean ok = true;
4043
4044 /* Don't even pretend to support mixed-format linking. */
4045 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4046 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4047 return false;
4048
4049 in_flags = elf_elfheader (ibfd)->e_flags;
4050 out_flags = elf_elfheader (obfd)->e_flags;
4051
4052 if (! elf_flags_init (obfd))
4053 {
4054 elf_flags_init (obfd) = true;
4055 elf_elfheader (obfd)->e_flags = in_flags;
4056
4057 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4058 && bfd_get_arch_info (obfd)->the_default)
4059 {
4060 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4061 bfd_get_mach (ibfd));
4062 }
4063
4064 return true;
4065 }
4066
4067 /* Check flag compatibility. */
4068 if (in_flags == out_flags)
4069 return true;
4070
4071 /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set. */
4072 if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
4073 elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
4074
4075 if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
4076 {
4077 (*_bfd_error_handler)
4078 (_("%s: linking trap-on-NULL-dereference with non-trapping files"),
4079 bfd_get_filename (ibfd));
4080
4081 bfd_set_error (bfd_error_bad_value);
4082 ok = false;
4083 }
4084 if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
4085 {
4086 (*_bfd_error_handler)
4087 (_("%s: linking big-endian files with little-endian files"),
4088 bfd_get_filename (ibfd));
4089
4090 bfd_set_error (bfd_error_bad_value);
4091 ok = false;
4092 }
4093 if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
4094 {
4095 (*_bfd_error_handler)
4096 (_("%s: linking 64-bit files with 32-bit files"),
4097 bfd_get_filename (ibfd));
4098
4099 bfd_set_error (bfd_error_bad_value);
4100 ok = false;
4101 }
4102 if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
4103 {
4104 (*_bfd_error_handler)
4105 (_("%s: linking constant-gp files with non-constant-gp files"),
4106 bfd_get_filename (ibfd));
4107
4108 bfd_set_error (bfd_error_bad_value);
4109 ok = false;
4110 }
4111 if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
4112 != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4113 {
4114 (*_bfd_error_handler)
4115 (_("%s: linking auto-pic files with non-auto-pic files"),
4116 bfd_get_filename (ibfd));
4117
4118 bfd_set_error (bfd_error_bad_value);
4119 ok = false;
4120 }
4121
4122 return ok;
4123 }
4124
4125 static boolean
4126 elfNN_ia64_print_private_bfd_data (abfd, ptr)
4127 bfd *abfd;
4128 PTR ptr;
4129 {
4130 FILE *file = (FILE *) ptr;
4131 flagword flags = elf_elfheader (abfd)->e_flags;
4132
4133 BFD_ASSERT (abfd != NULL && ptr != NULL);
4134
4135 fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
4136 (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
4137 (flags & EF_IA_64_EXT) ? "EXT, " : "",
4138 (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
4139 (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
4140 (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
4141 (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
4142 (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
4143 (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
4144
4145 _bfd_elf_print_private_bfd_data (abfd, ptr);
4146 return true;
4147 }
4148 \f
4149 #define TARGET_LITTLE_SYM bfd_elfNN_ia64_little_vec
4150 #define TARGET_LITTLE_NAME "elfNN-ia64-little"
4151 #define TARGET_BIG_SYM bfd_elfNN_ia64_big_vec
4152 #define TARGET_BIG_NAME "elfNN-ia64-big"
4153 #define ELF_ARCH bfd_arch_ia64
4154 #define ELF_MACHINE_CODE EM_IA_64
4155 #define ELF_MACHINE_ALT1 1999 /* EAS2.3 */
4156 #define ELF_MACHINE_ALT2 1998 /* EAS2.2 */
4157 #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
4158
4159 #define elf_backend_section_from_shdr \
4160 elfNN_ia64_section_from_shdr
4161 #define elf_backend_section_flags \
4162 elfNN_ia64_section_flags
4163 #define elf_backend_fake_sections \
4164 elfNN_ia64_fake_sections
4165 #define elf_backend_add_symbol_hook \
4166 elfNN_ia64_add_symbol_hook
4167 #define elf_backend_additional_program_headers \
4168 elfNN_ia64_additional_program_headers
4169 #define elf_backend_modify_segment_map \
4170 elfNN_ia64_modify_segment_map
4171 #define elf_info_to_howto \
4172 elfNN_ia64_info_to_howto
4173
4174 #define bfd_elfNN_bfd_reloc_type_lookup \
4175 elfNN_ia64_reloc_type_lookup
4176 #define bfd_elfNN_bfd_is_local_label_name \
4177 elfNN_ia64_is_local_label_name
4178 #define bfd_elfNN_bfd_relax_section \
4179 elfNN_ia64_relax_section
4180
4181 /* Stuff for the BFD linker: */
4182 #define bfd_elfNN_bfd_link_hash_table_create \
4183 elfNN_ia64_hash_table_create
4184 #define elf_backend_create_dynamic_sections \
4185 elfNN_ia64_create_dynamic_sections
4186 #define elf_backend_check_relocs \
4187 elfNN_ia64_check_relocs
4188 #define elf_backend_adjust_dynamic_symbol \
4189 elfNN_ia64_adjust_dynamic_symbol
4190 #define elf_backend_size_dynamic_sections \
4191 elfNN_ia64_size_dynamic_sections
4192 #define elf_backend_relocate_section \
4193 elfNN_ia64_relocate_section
4194 #define elf_backend_finish_dynamic_symbol \
4195 elfNN_ia64_finish_dynamic_symbol
4196 #define elf_backend_finish_dynamic_sections \
4197 elfNN_ia64_finish_dynamic_sections
4198 #define bfd_elfNN_bfd_final_link \
4199 elfNN_ia64_final_link
4200
4201 #define bfd_elfNN_bfd_copy_private_bfd_data \
4202 elfNN_ia64_copy_private_bfd_data
4203 #define bfd_elfNN_bfd_merge_private_bfd_data \
4204 elfNN_ia64_merge_private_bfd_data
4205 #define bfd_elfNN_bfd_set_private_flags \
4206 elfNN_ia64_set_private_flags
4207 #define bfd_elfNN_bfd_print_private_bfd_data \
4208 elfNN_ia64_print_private_bfd_data
4209
4210 #define elf_backend_plt_readonly 1
4211 #define elf_backend_want_plt_sym 0
4212 #define elf_backend_plt_alignment 5
4213 #define elf_backend_got_header_size 0
4214 #define elf_backend_plt_header_size PLT_HEADER_SIZE
4215 #define elf_backend_want_got_plt 1
4216 #define elf_backend_may_use_rel_p 1
4217 #define elf_backend_may_use_rela_p 1
4218 #define elf_backend_default_use_rela_p 1
4219 #define elf_backend_want_dynbss 0
4220 #define elf_backend_copy_indirect_symbol elfNN_ia64_hash_copy_indirect
4221 #define elf_backend_hide_symbol elfNN_ia64_hash_hide_symbol
4222
4223 #include "elfNN-target.h"