[toolchain] binutils: remove unrelated hunk in 2.18 avr32 support patch
[openwrt/svn-archive/archive.git] / toolchain / binutils / patches / 2.18 / 500-avr32.patch
1 --- a/bfd/archures.c
2 +++ b/bfd/archures.c
3 @@ -346,6 +346,11 @@ DESCRIPTION
4 .#define bfd_mach_avr4 4
5 .#define bfd_mach_avr5 5
6 .#define bfd_mach_avr6 6
7 +. bfd_arch_avr32, {* Atmel AVR32 *}
8 +.#define bfd_mach_avr32_ap 7000
9 +.#define bfd_mach_avr32_uc 3000
10 +.#define bfd_mach_avr32_ucr1 3001
11 +.#define bfd_mach_avr32_ucr2 3002
12 . bfd_arch_bfin, {* ADI Blackfin *}
13 .#define bfd_mach_bfin 1
14 . bfd_arch_cr16, {* National Semiconductor CompactRISC (ie CR16). *}
15 @@ -438,6 +443,7 @@ extern const bfd_arch_info_type bfd_alph
16 extern const bfd_arch_info_type bfd_arc_arch;
17 extern const bfd_arch_info_type bfd_arm_arch;
18 extern const bfd_arch_info_type bfd_avr_arch;
19 +extern const bfd_arch_info_type bfd_avr32_arch;
20 extern const bfd_arch_info_type bfd_bfin_arch;
21 extern const bfd_arch_info_type bfd_cr16_arch;
22 extern const bfd_arch_info_type bfd_cr16c_arch;
23 @@ -509,6 +515,7 @@ static const bfd_arch_info_type * const
24 &bfd_arc_arch,
25 &bfd_arm_arch,
26 &bfd_avr_arch,
27 + &bfd_avr32_arch,
28 &bfd_bfin_arch,
29 &bfd_cr16_arch,
30 &bfd_cr16c_arch,
31 --- a/bfd/config.bfd
32 +++ b/bfd/config.bfd
33 @@ -335,6 +335,10 @@ case "${targ}" in
34 targ_underscore=yes
35 ;;
36
37 + avr32-*-*)
38 + targ_defvec=bfd_elf32_avr32_vec
39 + ;;
40 +
41 c30-*-*aout* | tic30-*-*aout*)
42 targ_defvec=tic30_aout_vec
43 ;;
44 --- a/bfd/configure.in
45 +++ b/bfd/configure.in
46 @@ -619,6 +619,7 @@ do
47 bfd_efi_app_ia64_vec) tb="$tb efi-app-ia64.lo pepigen.lo cofflink.lo"; target_size=64 ;;
48 bfd_elf32_am33lin_vec) tb="$tb elf32-am33lin.lo elf32.lo $elf" ;;
49 bfd_elf32_avr_vec) tb="$tb elf32-avr.lo elf32.lo $elf" ;;
50 + bfd_elf32_avr32_vec) tb="$tb elf32-avr32.lo elf32.lo $elf" ;;
51 bfd_elf32_bfin_vec) tb="$tb elf32-bfin.lo elf32.lo $elf" ;;
52 bfd_elf32_bfinfdpic_vec) tb="$tb elf32-bfin.lo elf32.lo $elf" ;;
53 bfd_elf32_big_generic_vec) tb="$tb elf32-gen.lo elf32.lo $elf" ;;
54 --- /dev/null
55 +++ b/bfd/cpu-avr32.c
56 @@ -0,0 +1,51 @@
57 +/* BFD library support routines for AVR32.
58 + Copyright 2003-2006 Atmel Corporation.
59 +
60 + Written by Haavard Skinnemoen, Atmel Norway, <hskinnemoen@atmel.com>
61 +
62 + This is part of BFD, the Binary File Descriptor library.
63 +
64 + This program is free software; you can redistribute it and/or modify
65 + it under the terms of the GNU General Public License as published by
66 + the Free Software Foundation; either version 2 of the License, or
67 + (at your option) any later version.
68 +
69 + This program is distributed in the hope that it will be useful,
70 + but WITHOUT ANY WARRANTY; without even the implied warranty of
71 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72 + GNU General Public License for more details.
73 +
74 + You should have received a copy of the GNU General Public License
75 + along with this program; if not, write to the Free Software
76 + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
77 +
78 +#include "bfd.h"
79 +#include "sysdep.h"
80 +#include "libbfd.h"
81 +
82 +#define N(machine, print, default, next) \
83 + { \
84 + 32, /* 32 bits in a word */ \
85 + 32, /* 32 bits in an address */ \
86 + 8, /* 8 bits in a byte */ \
87 + bfd_arch_avr32, /* architecture */ \
88 + machine, /* machine */ \
89 + "avr32", /* arch name */ \
90 + print, /* printable name */ \
91 + 1, /* section align power */ \
92 + default, /* the default machine? */ \
93 + bfd_default_compatible, \
94 + bfd_default_scan, \
95 + next, \
96 + }
97 +
98 +static const bfd_arch_info_type cpu_info[] =
99 +{
100 + N(bfd_mach_avr32_ap, "avr32:ap", FALSE, &cpu_info[1]),
101 + N(bfd_mach_avr32_uc, "avr32:uc", FALSE, &cpu_info[2]),
102 + N(bfd_mach_avr32_ucr1, "avr32:ucr1", FALSE, &cpu_info[3]),
103 + N(bfd_mach_avr32_ucr2, "avr32:ucr2", FALSE, NULL),
104 +};
105 +
106 +const bfd_arch_info_type bfd_avr32_arch =
107 + N(bfd_mach_avr32_ap, "avr32", TRUE, &cpu_info[0]);
108 --- /dev/null
109 +++ b/bfd/elf32-avr32.c
110 @@ -0,0 +1,3915 @@
111 +/* AVR32-specific support for 32-bit ELF.
112 + Copyright 2003-2006 Atmel Corporation.
113 +
114 + Written by Haavard Skinnemoen, Atmel Norway, <hskinnemoen@atmel.com>
115 +
116 + This file is part of BFD, the Binary File Descriptor library.
117 +
118 + This program is free software; you can redistribute it and/or modify
119 + it under the terms of the GNU General Public License as published by
120 + the Free Software Foundation; either version 2 of the License, or
121 + (at your option) any later version.
122 +
123 + This program is distributed in the hope that it will be useful,
124 + but WITHOUT ANY WARRANTY; without even the implied warranty of
125 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
126 + GNU General Public License for more details.
127 +
128 + You should have received a copy of the GNU General Public License
129 + along with this program; if not, write to the Free Software
130 + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
131 +
132 +#include "bfd.h"
133 +#include "sysdep.h"
134 +#include "bfdlink.h"
135 +#include "libbfd.h"
136 +#include "elf-bfd.h"
137 +#include "elf/avr32.h"
138 +#include "elf32-avr32.h"
139 +
140 +#define xDEBUG
141 +#define xRELAX_DEBUG
142 +
143 +#ifdef DEBUG
144 +# define pr_debug(fmt, args...) fprintf(stderr, fmt, ##args)
145 +#else
146 +# define pr_debug(fmt, args...) do { } while (0)
147 +#endif
148 +
149 +#ifdef RELAX_DEBUG
150 +# define RDBG(fmt, args...) fprintf(stderr, fmt, ##args)
151 +#else
152 +# define RDBG(fmt, args...) do { } while (0)
153 +#endif
154 +
155 +/* When things go wrong, we want it to blow up, damnit! */
156 +#undef BFD_ASSERT
157 +#undef abort
158 +#define BFD_ASSERT(expr) \
159 + do \
160 + { \
161 + if (!(expr)) \
162 + { \
163 + bfd_assert(__FILE__, __LINE__); \
164 + abort(); \
165 + } \
166 + } \
167 + while (0)
168 +
169 +/* The name of the dynamic interpreter. This is put in the .interp section. */
170 +#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
171 +
172 +#define AVR32_GOT_HEADER_SIZE 8
173 +#define AVR32_FUNCTION_STUB_SIZE 8
174 +
175 +#define ELF_R_INFO(x, y) ELF32_R_INFO(x, y)
176 +#define ELF_R_TYPE(x) ELF32_R_TYPE(x)
177 +#define ELF_R_SYM(x) ELF32_R_SYM(x)
178 +
179 +#define NOP_OPCODE 0xd703
180 +
181 +
182 +/* Mapping between BFD relocations and ELF relocations */
183 +
184 +static reloc_howto_type *
185 +bfd_elf32_bfd_reloc_type_lookup(bfd *abfd, bfd_reloc_code_real_type code);
186 +
187 +static reloc_howto_type *
188 +bfd_elf32_bfd_reloc_name_lookup(bfd *abfd, const char *r_name);
189 +
190 +static void
191 +avr32_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst);
192 +
193 +/* Generic HOWTO */
194 +#define GENH(name, align, size, bitsize, pcrel, bitpos, complain, mask) \
195 + HOWTO(name, align, size, bitsize, pcrel, bitpos, \
196 + complain_overflow_##complain, bfd_elf_generic_reloc, #name, \
197 + FALSE, 0, mask, pcrel)
198 +
199 +static reloc_howto_type elf_avr32_howto_table[] = {
200 + /* NAME ALN SZ BSZ PCREL BP COMPLAIN MASK */
201 + GENH(R_AVR32_NONE, 0, 0, 0, FALSE, 0, dont, 0x00000000),
202 +
203 + GENH(R_AVR32_32, 0, 2, 32, FALSE, 0, dont, 0xffffffff),
204 + GENH(R_AVR32_16, 0, 1, 16, FALSE, 0, bitfield, 0x0000ffff),
205 + GENH(R_AVR32_8, 0, 0, 8, FALSE, 0, bitfield, 0x000000ff),
206 + GENH(R_AVR32_32_PCREL, 0, 2, 32, TRUE, 0, signed, 0xffffffff),
207 + GENH(R_AVR32_16_PCREL, 0, 1, 16, TRUE, 0, signed, 0x0000ffff),
208 + GENH(R_AVR32_8_PCREL, 0, 0, 8, TRUE, 0, signed, 0x000000ff),
209 +
210 + /* Difference between two symbol (sym2 - sym1). The reloc encodes
211 + the value of sym1. The field contains the difference before any
212 + relaxing is done. */
213 + GENH(R_AVR32_DIFF32, 0, 2, 32, FALSE, 0, dont, 0xffffffff),
214 + GENH(R_AVR32_DIFF16, 0, 1, 16, FALSE, 0, signed, 0x0000ffff),
215 + GENH(R_AVR32_DIFF8, 0, 0, 8, FALSE, 0, signed, 0x000000ff),
216 +
217 + GENH(R_AVR32_GOT32, 0, 2, 32, FALSE, 0, signed, 0xffffffff),
218 + GENH(R_AVR32_GOT16, 0, 1, 16, FALSE, 0, signed, 0x0000ffff),
219 + GENH(R_AVR32_GOT8, 0, 0, 8, FALSE, 0, signed, 0x000000ff),
220 +
221 + GENH(R_AVR32_21S, 0, 2, 21, FALSE, 0, signed, 0x1e10ffff),
222 + GENH(R_AVR32_16U, 0, 2, 16, FALSE, 0, unsigned, 0x0000ffff),
223 + GENH(R_AVR32_16S, 0, 2, 16, FALSE, 0, signed, 0x0000ffff),
224 + GENH(R_AVR32_8S, 0, 1, 8, FALSE, 4, signed, 0x00000ff0),
225 + GENH(R_AVR32_8S_EXT, 0, 2, 8, FALSE, 0, signed, 0x000000ff),
226 +
227 + GENH(R_AVR32_22H_PCREL, 1, 2, 21, TRUE, 0, signed, 0x1e10ffff),
228 + GENH(R_AVR32_18W_PCREL, 2, 2, 16, TRUE, 0, signed, 0x0000ffff),
229 + GENH(R_AVR32_16B_PCREL, 0, 2, 16, TRUE, 0, signed, 0x0000ffff),
230 + GENH(R_AVR32_16N_PCREL, 0, 2, 16, TRUE, 0, signed, 0x0000ffff),
231 + GENH(R_AVR32_14UW_PCREL, 2, 2, 12, TRUE, 0, unsigned, 0x0000f0ff),
232 + GENH(R_AVR32_11H_PCREL, 1, 1, 10, TRUE, 4, signed, 0x00000ff3),
233 + GENH(R_AVR32_10UW_PCREL, 2, 2, 8, TRUE, 0, unsigned, 0x000000ff),
234 + GENH(R_AVR32_9H_PCREL, 1, 1, 8, TRUE, 4, signed, 0x00000ff0),
235 + GENH(R_AVR32_9UW_PCREL, 2, 1, 7, TRUE, 4, unsigned, 0x000007f0),
236 +
237 + GENH(R_AVR32_HI16, 16, 2, 16, FALSE, 0, dont, 0x0000ffff),
238 + GENH(R_AVR32_LO16, 0, 2, 16, FALSE, 0, dont, 0x0000ffff),
239 +
240 + GENH(R_AVR32_GOTPC, 0, 2, 32, FALSE, 0, dont, 0xffffffff),
241 + GENH(R_AVR32_GOTCALL, 2, 2, 21, FALSE, 0, signed, 0x1e10ffff),
242 + GENH(R_AVR32_LDA_GOT, 2, 2, 21, FALSE, 0, signed, 0x1e10ffff),
243 + GENH(R_AVR32_GOT21S, 0, 2, 21, FALSE, 0, signed, 0x1e10ffff),
244 + GENH(R_AVR32_GOT18SW, 2, 2, 16, FALSE, 0, signed, 0x0000ffff),
245 + GENH(R_AVR32_GOT16S, 0, 2, 16, FALSE, 0, signed, 0x0000ffff),
246 + GENH(R_AVR32_GOT7UW, 2, 1, 5, FALSE, 4, unsigned, 0x000001f0),
247 +
248 + GENH(R_AVR32_32_CPENT, 0, 2, 32, FALSE, 0, dont, 0xffffffff),
249 + GENH(R_AVR32_CPCALL, 2, 2, 16, TRUE, 0, signed, 0x0000ffff),
250 + GENH(R_AVR32_16_CP, 0, 2, 16, TRUE, 0, signed, 0x0000ffff),
251 + GENH(R_AVR32_9W_CP, 2, 1, 7, TRUE, 4, unsigned, 0x000007f0),
252 +
253 + GENH(R_AVR32_RELATIVE, 0, 2, 32, FALSE, 0, signed, 0xffffffff),
254 + GENH(R_AVR32_GLOB_DAT, 0, 2, 32, FALSE, 0, dont, 0xffffffff),
255 + GENH(R_AVR32_JMP_SLOT, 0, 2, 32, FALSE, 0, dont, 0xffffffff),
256 +
257 + GENH(R_AVR32_ALIGN, 0, 1, 0, FALSE, 0, unsigned, 0x00000000),
258 +
259 + GENH(R_AVR32_15S, 2, 2, 15, FALSE, 0, signed, 0x00007fff),
260 +};
261 +
262 +struct elf_reloc_map
263 +{
264 + bfd_reloc_code_real_type bfd_reloc_val;
265 + unsigned char elf_reloc_val;
266 +};
267 +
268 +static const struct elf_reloc_map avr32_reloc_map[] =
269 +{
270 + { BFD_RELOC_NONE, R_AVR32_NONE },
271 +
272 + { BFD_RELOC_32, R_AVR32_32 },
273 + { BFD_RELOC_16, R_AVR32_16 },
274 + { BFD_RELOC_8, R_AVR32_8 },
275 + { BFD_RELOC_32_PCREL, R_AVR32_32_PCREL },
276 + { BFD_RELOC_16_PCREL, R_AVR32_16_PCREL },
277 + { BFD_RELOC_8_PCREL, R_AVR32_8_PCREL },
278 + { BFD_RELOC_AVR32_DIFF32, R_AVR32_DIFF32 },
279 + { BFD_RELOC_AVR32_DIFF16, R_AVR32_DIFF16 },
280 + { BFD_RELOC_AVR32_DIFF8, R_AVR32_DIFF8 },
281 + { BFD_RELOC_AVR32_GOT32, R_AVR32_GOT32 },
282 + { BFD_RELOC_AVR32_GOT16, R_AVR32_GOT16 },
283 + { BFD_RELOC_AVR32_GOT8, R_AVR32_GOT8 },
284 +
285 + { BFD_RELOC_AVR32_21S, R_AVR32_21S },
286 + { BFD_RELOC_AVR32_16U, R_AVR32_16U },
287 + { BFD_RELOC_AVR32_16S, R_AVR32_16S },
288 + { BFD_RELOC_AVR32_SUB5, R_AVR32_16S },
289 + { BFD_RELOC_AVR32_8S_EXT, R_AVR32_8S_EXT },
290 + { BFD_RELOC_AVR32_8S, R_AVR32_8S },
291 +
292 + { BFD_RELOC_AVR32_22H_PCREL, R_AVR32_22H_PCREL },
293 + { BFD_RELOC_AVR32_18W_PCREL, R_AVR32_18W_PCREL },
294 + { BFD_RELOC_AVR32_16B_PCREL, R_AVR32_16B_PCREL },
295 + { BFD_RELOC_AVR32_16N_PCREL, R_AVR32_16N_PCREL },
296 + { BFD_RELOC_AVR32_11H_PCREL, R_AVR32_11H_PCREL },
297 + { BFD_RELOC_AVR32_10UW_PCREL, R_AVR32_10UW_PCREL },
298 + { BFD_RELOC_AVR32_9H_PCREL, R_AVR32_9H_PCREL },
299 + { BFD_RELOC_AVR32_9UW_PCREL, R_AVR32_9UW_PCREL },
300 +
301 + { BFD_RELOC_HI16, R_AVR32_HI16 },
302 + { BFD_RELOC_LO16, R_AVR32_LO16 },
303 +
304 + { BFD_RELOC_AVR32_GOTPC, R_AVR32_GOTPC },
305 + { BFD_RELOC_AVR32_GOTCALL, R_AVR32_GOTCALL },
306 + { BFD_RELOC_AVR32_LDA_GOT, R_AVR32_LDA_GOT },
307 + { BFD_RELOC_AVR32_GOT21S, R_AVR32_GOT21S },
308 + { BFD_RELOC_AVR32_GOT18SW, R_AVR32_GOT18SW },
309 + { BFD_RELOC_AVR32_GOT16S, R_AVR32_GOT16S },
310 + /* GOT7UW should never be generated by the assembler */
311 +
312 + { BFD_RELOC_AVR32_32_CPENT, R_AVR32_32_CPENT },
313 + { BFD_RELOC_AVR32_CPCALL, R_AVR32_CPCALL },
314 + { BFD_RELOC_AVR32_16_CP, R_AVR32_16_CP },
315 + { BFD_RELOC_AVR32_9W_CP, R_AVR32_9W_CP },
316 +
317 + { BFD_RELOC_AVR32_ALIGN, R_AVR32_ALIGN },
318 +
319 + { BFD_RELOC_AVR32_15S, R_AVR32_15S },
320 +};
321 +
322 +static reloc_howto_type *
323 +bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
324 + bfd_reloc_code_real_type code)
325 +{
326 + unsigned int i;
327 +
328 + for (i = 0; i < sizeof(avr32_reloc_map) / sizeof(struct elf_reloc_map); i++)
329 + {
330 + if (avr32_reloc_map[i].bfd_reloc_val == code)
331 + return &elf_avr32_howto_table[avr32_reloc_map[i].elf_reloc_val];
332 + }
333 +
334 + return NULL;
335 +}
336 +
337 +static reloc_howto_type *
338 +bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
339 + const char *r_name)
340 +{
341 + unsigned int i;
342 +
343 + for (i = 0;
344 + i < sizeof (elf_avr32_howto_table) / sizeof (elf_avr32_howto_table[0]);
345 + i++)
346 + if (elf_avr32_howto_table[i].name != NULL
347 + && strcasecmp (elf_avr32_howto_table[i].name, r_name) == 0)
348 + return &elf_avr32_howto_table[i];
349 +
350 + return NULL;
351 +}
352 +
353 +/* Set the howto pointer for an AVR32 ELF reloc. */
354 +static void
355 +avr32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
356 + arelent *cache_ptr,
357 + Elf_Internal_Rela *dst)
358 +{
359 + unsigned int r_type;
360 +
361 + r_type = ELF32_R_TYPE (dst->r_info);
362 + BFD_ASSERT (r_type < (unsigned int) R_AVR32_max);
363 + cache_ptr->howto = &elf_avr32_howto_table[r_type];
364 +}
365 +
366 +
367 +/* AVR32 ELF linker hash table and associated hash entries. */
368 +
369 +static struct bfd_hash_entry *
370 +avr32_elf_link_hash_newfunc(struct bfd_hash_entry *entry,
371 + struct bfd_hash_table *table,
372 + const char *string);
373 +static void
374 +avr32_elf_copy_indirect_symbol(struct bfd_link_info *info,
375 + struct elf_link_hash_entry *dir,
376 + struct elf_link_hash_entry *ind);
377 +static struct bfd_link_hash_table *
378 +avr32_elf_link_hash_table_create(bfd *abfd);
379 +
380 +/*
381 + Try to limit memory usage to something reasonable when sorting the
382 + GOT. If just a couple of entries end up getting more references
383 + than this, it won't affect performance at all, but if there are many
384 + of them, we could end up with the wrong symbols being assigned the
385 + first GOT entries.
386 +*/
387 +#define MAX_NR_GOT_HOLES 2048
388 +
389 +/*
390 + AVR32 GOT entry. We need to keep track of refcounts and offsets
391 + simultaneously, since we need the offsets during relaxation, and we
392 + also want to be able to drop GOT entries during relaxation. In
393 + addition to this, we want to keep the list of GOT entries sorted so
394 + that we can keep the most-used entries at the lowest offsets.
395 +*/
396 +struct got_entry
397 +{
398 + struct got_entry *next;
399 + struct got_entry **pprev;
400 + int refcount;
401 + bfd_signed_vma offset;
402 +};
403 +
404 +struct elf_avr32_link_hash_entry
405 +{
406 + struct elf_link_hash_entry root;
407 +
408 + /* Number of runtime relocations against this symbol. */
409 + unsigned int possibly_dynamic_relocs;
410 +
411 + /* If there are anything but R_AVR32_GOT18 relocations against this
412 + symbol, it means that someone may be taking the address of the
413 + function, and we should therefore not create a stub. */
414 + bfd_boolean no_fn_stub;
415 +
416 + /* If there is a R_AVR32_32 relocation in a read-only section
417 + against this symbol, we could be in trouble. If we're linking a
418 + shared library or this symbol is defined in one, it means we must
419 + emit a run-time reloc for it and that's not allowed in read-only
420 + sections. */
421 + asection *readonly_reloc_sec;
422 + bfd_vma readonly_reloc_offset;
423 +
424 + /* Record which frag (if any) contains the symbol. This is used
425 + during relaxation in order to avoid having to update all symbols
426 + whenever we move something. For local symbols, this information
427 + is in the local_sym_frag member of struct elf_obj_tdata. */
428 + struct fragment *sym_frag;
429 +};
430 +#define avr32_elf_hash_entry(ent) ((struct elf_avr32_link_hash_entry *)(ent))
431 +
432 +struct elf_avr32_link_hash_table
433 +{
434 + struct elf_link_hash_table root;
435 +
436 + /* Shortcuts to get to dynamic linker sections. */
437 + asection *sgot;
438 + asection *srelgot;
439 + asection *sstub;
440 +
441 + /* We use a variation of Pigeonhole Sort to sort the GOT. After the
442 + initial refcounts have been determined, we initialize
443 + nr_got_holes to the highest refcount ever seen and allocate an
444 + array of nr_got_holes entries for got_hole. Each GOT entry is
445 + then stored in this array at the index given by its refcount.
446 +
447 + When a GOT entry has its refcount decremented during relaxation,
448 + it is moved to a lower index in the got_hole array.
449 + */
450 + struct got_entry **got_hole;
451 + int nr_got_holes;
452 +
453 + /* Dynamic relocations to local symbols. Only used when linking a
454 + shared library and -Bsymbolic is not given. */
455 + unsigned int local_dynamic_relocs;
456 +
457 + bfd_boolean relocations_analyzed;
458 + bfd_boolean symbols_adjusted;
459 + bfd_boolean repeat_pass;
460 + bfd_boolean direct_data_refs;
461 + unsigned int relax_iteration;
462 + unsigned int relax_pass;
463 +};
464 +#define avr32_elf_hash_table(p) \
465 + ((struct elf_avr32_link_hash_table *)((p)->hash))
466 +
467 +static struct bfd_hash_entry *
468 +avr32_elf_link_hash_newfunc(struct bfd_hash_entry *entry,
469 + struct bfd_hash_table *table,
470 + const char *string)
471 +{
472 + struct elf_avr32_link_hash_entry *ret = avr32_elf_hash_entry(entry);
473 +
474 + /* Allocate the structure if it hasn't already been allocated by a
475 + subclass */
476 + if (ret == NULL)
477 + ret = (struct elf_avr32_link_hash_entry *)
478 + bfd_hash_allocate(table, sizeof(struct elf_avr32_link_hash_entry));
479 +
480 + if (ret == NULL)
481 + return NULL;
482 +
483 + memset(ret, 0, sizeof(struct elf_avr32_link_hash_entry));
484 +
485 + /* Give the superclass a chance */
486 + ret = (struct elf_avr32_link_hash_entry *)
487 + _bfd_elf_link_hash_newfunc((struct bfd_hash_entry *)ret, table, string);
488 +
489 + return (struct bfd_hash_entry *)ret;
490 +}
491 +
492 +/* Copy data from an indirect symbol to its direct symbol, hiding the
493 + old indirect symbol. Process additional relocation information.
494 + Also called for weakdefs, in which case we just let
495 + _bfd_elf_link_hash_copy_indirect copy the flags for us. */
496 +
497 +static void
498 +avr32_elf_copy_indirect_symbol(struct bfd_link_info *info,
499 + struct elf_link_hash_entry *dir,
500 + struct elf_link_hash_entry *ind)
501 +{
502 + struct elf_avr32_link_hash_entry *edir, *eind;
503 +
504 + _bfd_elf_link_hash_copy_indirect (info, dir, ind);
505 +
506 + if (ind->root.type != bfd_link_hash_indirect)
507 + return;
508 +
509 + edir = (struct elf_avr32_link_hash_entry *)dir;
510 + eind = (struct elf_avr32_link_hash_entry *)ind;
511 +
512 + edir->possibly_dynamic_relocs += eind->possibly_dynamic_relocs;
513 + edir->no_fn_stub = edir->no_fn_stub || eind->no_fn_stub;
514 +}
515 +
516 +static struct bfd_link_hash_table *
517 +avr32_elf_link_hash_table_create(bfd *abfd)
518 +{
519 + struct elf_avr32_link_hash_table *ret;
520 +
521 + ret = bfd_zmalloc(sizeof(*ret));
522 + if (ret == NULL)
523 + return NULL;
524 +
525 + if (! _bfd_elf_link_hash_table_init(&ret->root, abfd,
526 + avr32_elf_link_hash_newfunc,
527 + sizeof (struct elf_avr32_link_hash_entry)))
528 + {
529 + free(ret);
530 + return NULL;
531 + }
532 +
533 + /* Prevent the BFD core from creating bogus got_entry pointers */
534 + ret->root.init_got_refcount.glist = NULL;
535 + ret->root.init_plt_refcount.glist = NULL;
536 + ret->root.init_got_offset.glist = NULL;
537 + ret->root.init_plt_offset.glist = NULL;
538 +
539 + return &ret->root.root;
540 +}
541 +
542 +
543 +/* Initial analysis and creation of dynamic sections and symbols */
544 +
545 +static asection *
546 +create_dynamic_section(bfd *dynobj, const char *name, flagword flags,
547 + unsigned int align_power);
548 +static struct elf_link_hash_entry *
549 +create_dynamic_symbol(bfd *dynobj, struct bfd_link_info *info,
550 + const char *name, asection *sec,
551 + bfd_vma offset);
552 +static bfd_boolean
553 +avr32_elf_create_got_section (bfd *dynobj, struct bfd_link_info *info);
554 +static bfd_boolean
555 +avr32_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info);
556 +static bfd_boolean
557 +avr32_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
558 + const Elf_Internal_Rela *relocs);
559 +static bfd_boolean
560 +avr32_elf_adjust_dynamic_symbol(struct bfd_link_info *info,
561 + struct elf_link_hash_entry *h);
562 +
563 +static asection *
564 +create_dynamic_section(bfd *dynobj, const char *name, flagword flags,
565 + unsigned int align_power)
566 +{
567 + asection *sec;
568 +
569 + sec = bfd_make_section(dynobj, name);
570 + if (!sec
571 + || !bfd_set_section_flags(dynobj, sec, flags)
572 + || !bfd_set_section_alignment(dynobj, sec, align_power))
573 + return NULL;
574 +
575 + return sec;
576 +}
577 +
578 +static struct elf_link_hash_entry *
579 +create_dynamic_symbol(bfd *dynobj, struct bfd_link_info *info,
580 + const char *name, asection *sec,
581 + bfd_vma offset)
582 +{
583 + struct bfd_link_hash_entry *bh = NULL;
584 + struct elf_link_hash_entry *h;
585 + const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
586 +
587 + if (!(_bfd_generic_link_add_one_symbol
588 + (info, dynobj, name, BSF_GLOBAL, sec, offset, NULL, FALSE,
589 + bed->collect, &bh)))
590 + return NULL;
591 +
592 + h = (struct elf_link_hash_entry *)bh;
593 + h->def_regular = 1;
594 + h->type = STT_OBJECT;
595 + h->other = STV_HIDDEN;
596 +
597 + return h;
598 +}
599 +
600 +static bfd_boolean
601 +avr32_elf_create_got_section (bfd *dynobj, struct bfd_link_info *info)
602 +{
603 + struct elf_avr32_link_hash_table *htab;
604 + flagword flags;
605 + const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
606 +
607 + htab = avr32_elf_hash_table(info);
608 + flags = bed->dynamic_sec_flags;
609 +
610 + if (htab->sgot)
611 + return TRUE;
612 +
613 + htab->sgot = create_dynamic_section(dynobj, ".got", flags, 2);
614 + if (!htab->srelgot)
615 + htab->srelgot = create_dynamic_section(dynobj, ".rela.got",
616 + flags | SEC_READONLY, 2);
617 +
618 + if (!htab->sgot || !htab->srelgot)
619 + return FALSE;
620 +
621 + htab->root.hgot = create_dynamic_symbol(dynobj, info, "_GLOBAL_OFFSET_TABLE_",
622 + htab->sgot, 0);
623 + if (!htab->root.hgot)
624 + return FALSE;
625 +
626 + /* Make room for the GOT header */
627 + htab->sgot->size += bed->got_header_size;
628 +
629 + return TRUE;
630 +}
631 +
632 +/* (1) Create all dynamic (i.e. linker generated) sections that we may
633 + need during the link */
634 +
635 +static bfd_boolean
636 +avr32_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
637 +{
638 + struct elf_avr32_link_hash_table *htab;
639 + flagword flags;
640 + const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
641 +
642 + pr_debug("(1) create dynamic sections\n");
643 +
644 + htab = avr32_elf_hash_table(info);
645 + flags = bed->dynamic_sec_flags;
646 +
647 + if (!avr32_elf_create_got_section (dynobj, info))
648 + return FALSE;
649 +
650 + if (!htab->sstub)
651 + htab->sstub = create_dynamic_section(dynobj, ".stub",
652 + flags | SEC_READONLY | SEC_CODE, 2);
653 +
654 + if (!htab->sstub)
655 + return FALSE;
656 +
657 + return TRUE;
658 +}
659 +
660 +/* (2) Go through all the relocs and count any potential GOT- or
661 + PLT-references to each symbol */
662 +
663 +static bfd_boolean
664 +avr32_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
665 + const Elf_Internal_Rela *relocs)
666 +{
667 + Elf_Internal_Shdr *symtab_hdr;
668 + struct elf_avr32_link_hash_table *htab;
669 + struct elf_link_hash_entry **sym_hashes;
670 + const Elf_Internal_Rela *rel, *rel_end;
671 + struct got_entry **local_got_ents;
672 + struct got_entry *got;
673 + const struct elf_backend_data *bed = get_elf_backend_data (abfd);
674 + asection *sgot;
675 + bfd *dynobj;
676 +
677 + pr_debug("(2) check relocs for %s:<%s> (size 0x%lx)\n",
678 + abfd->filename, sec->name, sec->size);
679 +
680 + if (info->relocatable)
681 + return TRUE;
682 +
683 + dynobj = elf_hash_table(info)->dynobj;
684 + symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
685 + sym_hashes = elf_sym_hashes(abfd);
686 + htab = avr32_elf_hash_table(info);
687 + local_got_ents = elf_local_got_ents(abfd);
688 + sgot = htab->sgot;
689 +
690 + rel_end = relocs + sec->reloc_count;
691 + for (rel = relocs; rel < rel_end; rel++)
692 + {
693 + unsigned long r_symndx, r_type;
694 + struct elf_avr32_link_hash_entry *h;
695 +
696 + r_symndx = ELF32_R_SYM(rel->r_info);
697 + r_type = ELF32_R_TYPE(rel->r_info);
698 +
699 + /* Local symbols use local_got_ents, while others store the same
700 + information in the hash entry */
701 + if (r_symndx < symtab_hdr->sh_info)
702 + {
703 + pr_debug(" (2a) processing local symbol %lu\n", r_symndx);
704 + h = NULL;
705 + }
706 + else
707 + {
708 + h = (struct elf_avr32_link_hash_entry *)
709 + sym_hashes[r_symndx - symtab_hdr->sh_info];
710 + while (h->root.type == bfd_link_hash_indirect
711 + || h->root.type == bfd_link_hash_warning)
712 + h = (struct elf_avr32_link_hash_entry *)h->root.root.u.i.link;
713 + pr_debug(" (2a) processing symbol %s\n", h->root.root.root.string);
714 + }
715 +
716 + /* Some relocs require special sections to be created. */
717 + switch (r_type)
718 + {
719 + case R_AVR32_GOT32:
720 + case R_AVR32_GOT16:
721 + case R_AVR32_GOT8:
722 + case R_AVR32_GOT21S:
723 + case R_AVR32_GOT18SW:
724 + case R_AVR32_GOT16S:
725 + case R_AVR32_GOT7UW:
726 + case R_AVR32_LDA_GOT:
727 + case R_AVR32_GOTCALL:
728 + if (rel->r_addend)
729 + {
730 + if (info->callbacks->reloc_dangerous
731 + (info, _("Non-zero addend on GOT-relative relocation"),
732 + abfd, sec, rel->r_offset) == FALSE)
733 + return FALSE;
734 + }
735 + /* fall through */
736 + case R_AVR32_GOTPC:
737 + if (dynobj == NULL)
738 + elf_hash_table(info)->dynobj = dynobj = abfd;
739 + if (sgot == NULL && !avr32_elf_create_got_section(dynobj, info))
740 + return FALSE;
741 + break;
742 + case R_AVR32_32:
743 + /* We may need to create .rela.dyn later on. */
744 + if (dynobj == NULL
745 + && (info->shared || h != NULL)
746 + && (sec->flags & SEC_ALLOC))
747 + elf_hash_table(info)->dynobj = dynobj = abfd;
748 + break;
749 + }
750 +
751 + if (h != NULL && r_type != R_AVR32_GOT18SW)
752 + h->no_fn_stub = TRUE;
753 +
754 + switch (r_type)
755 + {
756 + case R_AVR32_GOT32:
757 + case R_AVR32_GOT16:
758 + case R_AVR32_GOT8:
759 + case R_AVR32_GOT21S:
760 + case R_AVR32_GOT18SW:
761 + case R_AVR32_GOT16S:
762 + case R_AVR32_GOT7UW:
763 + case R_AVR32_LDA_GOT:
764 + case R_AVR32_GOTCALL:
765 + if (h != NULL)
766 + {
767 + got = h->root.got.glist;
768 + if (!got)
769 + {
770 + got = bfd_zalloc(abfd, sizeof(struct got_entry));
771 + if (!got)
772 + return FALSE;
773 + h->root.got.glist = got;
774 + }
775 + }
776 + else
777 + {
778 + if (!local_got_ents)
779 + {
780 + bfd_size_type size;
781 + bfd_size_type i;
782 + struct got_entry *tmp_entry;
783 +
784 + size = symtab_hdr->sh_info;
785 + size *= sizeof(struct got_entry *) + sizeof(struct got_entry);
786 + local_got_ents = bfd_zalloc(abfd, size);
787 + if (!local_got_ents)
788 + return FALSE;
789 +
790 + elf_local_got_ents(abfd) = local_got_ents;
791 +
792 + tmp_entry = (struct got_entry *)(local_got_ents
793 + + symtab_hdr->sh_info);
794 + for (i = 0; i < symtab_hdr->sh_info; i++)
795 + local_got_ents[i] = &tmp_entry[i];
796 + }
797 +
798 + got = local_got_ents[r_symndx];
799 + }
800 +
801 + got->refcount++;
802 + if (got->refcount > htab->nr_got_holes)
803 + htab->nr_got_holes = got->refcount;
804 + break;
805 +
806 + case R_AVR32_32:
807 + if ((info->shared || h != NULL)
808 + && (sec->flags & SEC_ALLOC))
809 + {
810 + if (htab->srelgot == NULL)
811 + {
812 + htab->srelgot = create_dynamic_section(dynobj, ".rela.got",
813 + bed->dynamic_sec_flags
814 + | SEC_READONLY, 2);
815 + if (htab->srelgot == NULL)
816 + return FALSE;
817 + }
818 +
819 + if (sec->flags & SEC_READONLY
820 + && !h->readonly_reloc_sec)
821 + {
822 + h->readonly_reloc_sec = sec;
823 + h->readonly_reloc_offset = rel->r_offset;
824 + }
825 +
826 + if (h != NULL)
827 + {
828 + pr_debug("Non-GOT reference to symbol %s\n",
829 + h->root.root.root.string);
830 + h->possibly_dynamic_relocs++;
831 + }
832 + else
833 + {
834 + pr_debug("Non-GOT reference to local symbol %lu\n",
835 + r_symndx);
836 + htab->local_dynamic_relocs++;
837 + }
838 + }
839 +
840 + break;
841 +
842 + /* TODO: GNU_VTINHERIT and GNU_VTENTRY */
843 + }
844 + }
845 +
846 + return TRUE;
847 +}
848 +
849 +/* (3) Adjust a symbol defined by a dynamic object and referenced by a
850 + regular object. The current definition is in some section of the
851 + dynamic object, but we're not including those sections. We have to
852 + change the definition to something the rest of the link can
853 + understand. */
854 +
855 +static bfd_boolean
856 +avr32_elf_adjust_dynamic_symbol(struct bfd_link_info *info,
857 + struct elf_link_hash_entry *h)
858 +{
859 + struct elf_avr32_link_hash_table *htab;
860 + struct elf_avr32_link_hash_entry *havr;
861 + bfd *dynobj;
862 +
863 + pr_debug("(3) adjust dynamic symbol %s\n", h->root.root.string);
864 +
865 + htab = avr32_elf_hash_table(info);
866 + havr = (struct elf_avr32_link_hash_entry *)h;
867 + dynobj = elf_hash_table(info)->dynobj;
868 +
869 + /* Make sure we know what is going on here. */
870 + BFD_ASSERT (dynobj != NULL
871 + && (h->u.weakdef != NULL
872 + || (h->def_dynamic
873 + && h->ref_regular
874 + && !h->def_regular)));
875 +
876 + /* We don't want dynamic relocations in read-only sections. */
877 + if (havr->readonly_reloc_sec)
878 + {
879 + if (info->callbacks->reloc_dangerous
880 + (info, _("dynamic relocation in read-only section"),
881 + havr->readonly_reloc_sec->owner, havr->readonly_reloc_sec,
882 + havr->readonly_reloc_offset) == FALSE)
883 + return FALSE;
884 + }
885 +
886 + /* If this is a function, create a stub if possible and set the
887 + symbol to the stub location. */
888 + if (0 && !havr->no_fn_stub)
889 + {
890 + if (!h->def_regular)
891 + {
892 + asection *s = htab->sstub;
893 +
894 + BFD_ASSERT(s != NULL);
895 +
896 + h->root.u.def.section = s;
897 + h->root.u.def.value = s->size;
898 + h->plt.offset = s->size;
899 + s->size += AVR32_FUNCTION_STUB_SIZE;
900 +
901 + return TRUE;
902 + }
903 + }
904 + else if (h->type == STT_FUNC)
905 + {
906 + /* This will set the entry for this symbol in the GOT to 0, and
907 + the dynamic linker will take care of this. */
908 + h->root.u.def.value = 0;
909 + return TRUE;
910 + }
911 +
912 + /* If this is a weak symbol, and there is a real definition, the
913 + processor independent code will have arranged for us to see the
914 + real definition first, and we can just use the same value. */
915 + if (h->u.weakdef != NULL)
916 + {
917 + BFD_ASSERT(h->u.weakdef->root.type == bfd_link_hash_defined
918 + || h->u.weakdef->root.type == bfd_link_hash_defweak);
919 + h->root.u.def.section = h->u.weakdef->root.u.def.section;
920 + h->root.u.def.value = h->u.weakdef->root.u.def.value;
921 + return TRUE;
922 + }
923 +
924 + /* This is a reference to a symbol defined by a dynamic object which
925 + is not a function. */
926 +
927 + return TRUE;
928 +}
929 +
930 +
931 +/* Garbage-collection of unused sections */
932 +
933 +static asection *
934 +avr32_elf_gc_mark_hook(asection *sec,
935 + struct bfd_link_info *info ATTRIBUTE_UNUSED,
936 + Elf_Internal_Rela *rel,
937 + struct elf_link_hash_entry *h,
938 + Elf_Internal_Sym *sym)
939 +{
940 + if (h)
941 + {
942 + switch (ELF32_R_TYPE(rel->r_info))
943 + {
944 + /* TODO: VTINHERIT/VTENTRY */
945 + default:
946 + switch (h->root.type)
947 + {
948 + case bfd_link_hash_defined:
949 + case bfd_link_hash_defweak:
950 + return h->root.u.def.section;
951 +
952 + case bfd_link_hash_common:
953 + return h->root.u.c.p->section;
954 +
955 + default:
956 + break;
957 + }
958 + }
959 + }
960 + else
961 + return bfd_section_from_elf_index(sec->owner, sym->st_shndx);
962 +
963 + return NULL;
964 +}
965 +
966 +/* Update the GOT entry reference counts for the section being removed. */
967 +static bfd_boolean
968 +avr32_elf_gc_sweep_hook(bfd *abfd,
969 + struct bfd_link_info *info ATTRIBUTE_UNUSED,
970 + asection *sec,
971 + const Elf_Internal_Rela *relocs)
972 +{
973 + Elf_Internal_Shdr *symtab_hdr;
974 + struct elf_avr32_link_hash_entry **sym_hashes;
975 + struct got_entry **local_got_ents;
976 + const Elf_Internal_Rela *rel, *relend;
977 +
978 + if (!(sec->flags & SEC_ALLOC))
979 + return TRUE;
980 +
981 + symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
982 + sym_hashes = (struct elf_avr32_link_hash_entry **)elf_sym_hashes(abfd);
983 + local_got_ents = elf_local_got_ents(abfd);
984 +
985 + relend = relocs + sec->reloc_count;
986 + for (rel = relocs; rel < relend; rel++)
987 + {
988 + unsigned long r_symndx;
989 + unsigned int r_type;
990 + struct elf_avr32_link_hash_entry *h = NULL;
991 +
992 + r_symndx = ELF32_R_SYM(rel->r_info);
993 + if (r_symndx >= symtab_hdr->sh_info)
994 + {
995 + h = sym_hashes[r_symndx - symtab_hdr->sh_info];
996 + while (h->root.root.type == bfd_link_hash_indirect
997 + || h->root.root.type == bfd_link_hash_warning)
998 + h = (struct elf_avr32_link_hash_entry *)h->root.root.u.i.link;
999 + }
1000 +
1001 + r_type = ELF32_R_TYPE(rel->r_info);
1002 +
1003 + switch (r_type)
1004 + {
1005 + case R_AVR32_GOT32:
1006 + case R_AVR32_GOT16:
1007 + case R_AVR32_GOT8:
1008 + case R_AVR32_GOT21S:
1009 + case R_AVR32_GOT18SW:
1010 + case R_AVR32_GOT16S:
1011 + case R_AVR32_GOT7UW:
1012 + case R_AVR32_LDA_GOT:
1013 + case R_AVR32_GOTCALL:
1014 + if (h)
1015 + h->root.got.glist->refcount--;
1016 + else
1017 + local_got_ents[r_symndx]->refcount--;
1018 + break;
1019 +
1020 + case R_AVR32_32:
1021 + if (info->shared || h)
1022 + {
1023 + if (h)
1024 + h->possibly_dynamic_relocs--;
1025 + else
1026 + avr32_elf_hash_table(info)->local_dynamic_relocs--;
1027 + }
1028 +
1029 + default:
1030 + break;
1031 + }
1032 + }
1033 +
1034 + return TRUE;
1035 +}
1036 +
1037 +/* Sizing and refcounting of dynamic sections */
1038 +
1039 +static void
1040 +insert_got_entry(struct elf_avr32_link_hash_table *htab, struct got_entry *got);
1041 +static void
1042 +unref_got_entry(struct elf_avr32_link_hash_table *htab, struct got_entry *got);
1043 +static void
1044 +ref_got_entry(struct elf_avr32_link_hash_table *htab, struct got_entry *got);
1045 +static bfd_boolean
1046 +assign_got_offsets(struct elf_avr32_link_hash_table *htab);
1047 +static bfd_boolean
1048 +allocate_dynrelocs(struct elf_link_hash_entry *h, void *_info);
1049 +static bfd_boolean
1050 +avr32_elf_size_dynamic_sections (bfd *output_bfd,
1051 + struct bfd_link_info *info);
1052 +
1053 +static void
1054 +insert_got_entry(struct elf_avr32_link_hash_table *htab, struct got_entry *got)
1055 +{
1056 + /* Any entries with got_refcount > htab->nr_got_holes end up in the
1057 + * last pigeonhole without any sorting. We expect the number of such
1058 + * entries to be small, so it is very unlikely to affect
1059 + * performance. */
1060 + int entry = got->refcount;
1061 +
1062 + if (entry > htab->nr_got_holes)
1063 + entry = htab->nr_got_holes;
1064 +
1065 + got->pprev = &htab->got_hole[entry];
1066 + got->next = htab->got_hole[entry];
1067 +
1068 + if (got->next)
1069 + got->next->pprev = &got->next;
1070 +
1071 + htab->got_hole[entry] = got;
1072 +}
1073 +
1074 +/* Decrement the refcount of a GOT entry and update its position in
1075 + the pigeonhole array. */
1076 +static void
1077 +unref_got_entry(struct elf_avr32_link_hash_table *htab, struct got_entry *got)
1078 +{
1079 + BFD_ASSERT(got->refcount > 0);
1080 +
1081 + if (got->next)
1082 + got->next->pprev = got->pprev;
1083 +
1084 + *(got->pprev) = got->next;
1085 + got->refcount--;
1086 + insert_got_entry(htab, got);
1087 +}
1088 +
1089 +static void
1090 +ref_got_entry(struct elf_avr32_link_hash_table *htab, struct got_entry *got)
1091 +{
1092 + if (got->next)
1093 + got->next->pprev = got->pprev;
1094 +
1095 + *(got->pprev) = got->next;
1096 + got->refcount++;
1097 + insert_got_entry(htab, got);
1098 +
1099 + BFD_ASSERT(got->refcount > 0);
1100 +}
1101 +
1102 +/* Assign offsets to all GOT entries we intend to keep. The entries
1103 + that are referenced most often are placed at low offsets so that we
1104 + can use compact instructions as much as possible.
1105 +
1106 + Returns TRUE if any offsets or the total size of the GOT changed. */
1107 +
1108 +static bfd_boolean
1109 +assign_got_offsets(struct elf_avr32_link_hash_table *htab)
1110 +{
1111 + struct got_entry *got;
1112 + bfd_size_type got_size = 0;
1113 + bfd_boolean changed = FALSE;
1114 + bfd_signed_vma offset;
1115 + int i;
1116 +
1117 + /* The GOT header provides the address of the DYNAMIC segment, so
1118 + we need that even if the GOT is otherwise empty. */
1119 + if (htab->root.dynamic_sections_created)
1120 + got_size = AVR32_GOT_HEADER_SIZE;
1121 +
1122 + for (i = htab->nr_got_holes; i > 0; i--)
1123 + {
1124 + got = htab->got_hole[i];
1125 + while (got)
1126 + {
1127 + if (got->refcount > 0)
1128 + {
1129 + offset = got_size;
1130 + if (got->offset != offset)
1131 + {
1132 + RDBG("GOT offset changed: %ld -> %ld\n",
1133 + got->offset, offset);
1134 + changed = TRUE;
1135 + }
1136 + got->offset = offset;
1137 + got_size += 4;
1138 + }
1139 + got = got->next;
1140 + }
1141 + }
1142 +
1143 + if (htab->sgot->size != got_size)
1144 + {
1145 + RDBG("GOT size changed: %lu -> %lu\n", htab->sgot->size,
1146 + got_size);
1147 + changed = TRUE;
1148 + }
1149 + htab->sgot->size = got_size;
1150 +
1151 + RDBG("assign_got_offsets: total size %lu (%s)\n",
1152 + got_size, changed ? "changed" : "no change");
1153 +
1154 + return changed;
1155 +}
1156 +
1157 +static bfd_boolean
1158 +allocate_dynrelocs(struct elf_link_hash_entry *h, void *_info)
1159 +{
1160 + struct bfd_link_info *info = _info;
1161 + struct elf_avr32_link_hash_table *htab;
1162 + struct elf_avr32_link_hash_entry *havr;
1163 + struct got_entry *got;
1164 +
1165 + pr_debug(" (4b) allocate_dynrelocs: %s\n", h->root.root.string);
1166 +
1167 + if (h->root.type == bfd_link_hash_indirect)
1168 + return TRUE;
1169 +
1170 + if (h->root.type == bfd_link_hash_warning)
1171 + /* When warning symbols are created, they **replace** the "real"
1172 + entry in the hash table, thus we never get to see the real
1173 + symbol in a hash traversal. So look at it now. */
1174 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
1175 +
1176 + htab = avr32_elf_hash_table(info);
1177 + havr = (struct elf_avr32_link_hash_entry *)h;
1178 +
1179 + got = h->got.glist;
1180 +
1181 + /* If got is NULL, the symbol is never referenced through the GOT */
1182 + if (got && got->refcount > 0)
1183 + {
1184 + insert_got_entry(htab, got);
1185 +
1186 + /* Shared libraries need relocs for all GOT entries unless the
1187 + symbol is forced local or -Bsymbolic is used. Others need
1188 + relocs for everything that is not guaranteed to be defined in
1189 + a regular object. */
1190 + if ((info->shared
1191 + && !info->symbolic
1192 + && h->dynindx != -1)
1193 + || (htab->root.dynamic_sections_created
1194 + && h->def_dynamic
1195 + && !h->def_regular))
1196 + htab->srelgot->size += sizeof(Elf32_External_Rela);
1197 + }
1198 +
1199 + if (havr->possibly_dynamic_relocs
1200 + && (info->shared
1201 + || (elf_hash_table(info)->dynamic_sections_created
1202 + && h->def_dynamic
1203 + && !h->def_regular)))
1204 + {
1205 + pr_debug("Allocating %d dynamic reloc against symbol %s...\n",
1206 + havr->possibly_dynamic_relocs, h->root.root.string);
1207 + htab->srelgot->size += (havr->possibly_dynamic_relocs
1208 + * sizeof(Elf32_External_Rela));
1209 + }
1210 +
1211 + return TRUE;
1212 +}
1213 +
1214 +/* (4) Calculate the sizes of the linker-generated sections and
1215 + allocate memory for them. */
1216 +
1217 +static bfd_boolean
1218 +avr32_elf_size_dynamic_sections (bfd *output_bfd,
1219 + struct bfd_link_info *info)
1220 +{
1221 + struct elf_avr32_link_hash_table *htab;
1222 + bfd *dynobj;
1223 + asection *s;
1224 + bfd *ibfd;
1225 + bfd_boolean relocs;
1226 +
1227 + pr_debug("(4) size dynamic sections\n");
1228 +
1229 + htab = avr32_elf_hash_table(info);
1230 + dynobj = htab->root.dynobj;
1231 + BFD_ASSERT(dynobj != NULL);
1232 +
1233 + if (htab->root.dynamic_sections_created)
1234 + {
1235 + /* Initialize the contents of the .interp section to the name of
1236 + the dynamic loader */
1237 + if (info->executable)
1238 + {
1239 + s = bfd_get_section_by_name(dynobj, ".interp");
1240 + BFD_ASSERT(s != NULL);
1241 + s->size = sizeof(ELF_DYNAMIC_INTERPRETER);
1242 + s->contents = (unsigned char *)ELF_DYNAMIC_INTERPRETER;
1243 + }
1244 + }
1245 +
1246 + if (htab->nr_got_holes > 0)
1247 + {
1248 + /* Allocate holes for the pigeonhole sort algorithm */
1249 + pr_debug("Highest GOT refcount: %d\n", htab->nr_got_holes);
1250 +
1251 + /* Limit the memory usage by clipping the number of pigeonholes
1252 + * at a predefined maximum. All entries with a higher refcount
1253 + * will end up in the last pigeonhole. */
1254 + if (htab->nr_got_holes >= MAX_NR_GOT_HOLES)
1255 + {
1256 + htab->nr_got_holes = MAX_NR_GOT_HOLES - 1;
1257 +
1258 + pr_debug("Limiting maximum number of GOT pigeonholes to %u\n",
1259 + htab->nr_got_holes);
1260 + }
1261 + htab->got_hole = bfd_zalloc(output_bfd,
1262 + sizeof(struct got_entry *)
1263 + * (htab->nr_got_holes + 1));
1264 + if (!htab->got_hole)
1265 + return FALSE;
1266 +
1267 + /* Set up .got offsets for local syms. */
1268 + for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1269 + {
1270 + struct got_entry **local_got;
1271 + struct got_entry **end_local_got;
1272 + Elf_Internal_Shdr *symtab_hdr;
1273 + bfd_size_type locsymcount;
1274 +
1275 + pr_debug(" (4a) processing file %s...\n", ibfd->filename);
1276 +
1277 + BFD_ASSERT(bfd_get_flavour(ibfd) == bfd_target_elf_flavour);
1278 +
1279 + local_got = elf_local_got_ents(ibfd);
1280 + if (!local_got)
1281 + continue;
1282 +
1283 + symtab_hdr = &elf_tdata(ibfd)->symtab_hdr;
1284 + locsymcount = symtab_hdr->sh_info;
1285 + end_local_got = local_got + locsymcount;
1286 +
1287 + for (; local_got < end_local_got; ++local_got)
1288 + insert_got_entry(htab, *local_got);
1289 + }
1290 + }
1291 +
1292 + /* Allocate global sym .got entries and space for global sym
1293 + dynamic relocs */
1294 + elf_link_hash_traverse(&htab->root, allocate_dynrelocs, info);
1295 +
1296 + /* Now that we have sorted the GOT entries, we are ready to
1297 + assign offsets and determine the initial size of the GOT. */
1298 + if (htab->sgot)
1299 + assign_got_offsets(htab);
1300 +
1301 + /* Allocate space for local sym dynamic relocs */
1302 + BFD_ASSERT(htab->local_dynamic_relocs == 0 || info->shared);
1303 + if (htab->local_dynamic_relocs)
1304 + htab->srelgot->size += (htab->local_dynamic_relocs
1305 + * sizeof(Elf32_External_Rela));
1306 +
1307 + /* We now have determined the sizes of the various dynamic
1308 + sections. Allocate memory for them. */
1309 + relocs = FALSE;
1310 + for (s = dynobj->sections; s; s = s->next)
1311 + {
1312 + if ((s->flags & SEC_LINKER_CREATED) == 0)
1313 + continue;
1314 +
1315 + if (s == htab->sgot
1316 + || s == htab->sstub)
1317 + {
1318 + /* Strip this section if we don't need it */
1319 + }
1320 + else if (strncmp (bfd_get_section_name(dynobj, s), ".rela", 5) == 0)
1321 + {
1322 + if (s->size != 0)
1323 + relocs = TRUE;
1324 +
1325 + s->reloc_count = 0;
1326 + }
1327 + else
1328 + {
1329 + /* It's not one of our sections */
1330 + continue;
1331 + }
1332 +
1333 + if (s->size == 0)
1334 + {
1335 + /* Strip unneeded sections */
1336 + pr_debug("Stripping section %s from output...\n", s->name);
1337 + /* deleted function in 2.17
1338 + _bfd_strip_section_from_output(info, s);
1339 + */
1340 + continue;
1341 + }
1342 +
1343 + s->contents = bfd_zalloc(dynobj, s->size);
1344 + if (s->contents == NULL)
1345 + return FALSE;
1346 + }
1347 +
1348 + if (htab->root.dynamic_sections_created)
1349 + {
1350 + /* Add some entries to the .dynamic section. We fill in the
1351 + values later, in sh_elf_finish_dynamic_sections, but we
1352 + must add the entries now so that we get the correct size for
1353 + the .dynamic section. The DT_DEBUG entry is filled in by the
1354 + dynamic linker and used by the debugger. */
1355 +#define add_dynamic_entry(TAG, VAL) _bfd_elf_add_dynamic_entry(info, TAG, VAL)
1356 +
1357 + if (!add_dynamic_entry(DT_PLTGOT, 0))
1358 + return FALSE;
1359 + if (!add_dynamic_entry(DT_AVR32_GOTSZ, 0))
1360 + return FALSE;
1361 +
1362 + if (info->executable)
1363 + {
1364 + if (!add_dynamic_entry(DT_DEBUG, 0))
1365 + return FALSE;
1366 + }
1367 + if (relocs)
1368 + {
1369 + if (!add_dynamic_entry(DT_RELA, 0)
1370 + || !add_dynamic_entry(DT_RELASZ, 0)
1371 + || !add_dynamic_entry(DT_RELAENT,
1372 + sizeof(Elf32_External_Rela)))
1373 + return FALSE;
1374 + }
1375 + }
1376 +#undef add_dynamic_entry
1377 +
1378 + return TRUE;
1379 +}
1380 +
1381 +
1382 +/* Access to internal relocations, section contents and symbols.
1383 + (stolen from the xtensa port) */
1384 +
1385 +static Elf_Internal_Rela *
1386 +retrieve_internal_relocs (bfd *abfd, asection *sec, bfd_boolean keep_memory);
1387 +static void
1388 +pin_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs);
1389 +static void
1390 +release_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs);
1391 +static bfd_byte *
1392 +retrieve_contents (bfd *abfd, asection *sec, bfd_boolean keep_memory);
1393 +/*
1394 +static void
1395 +pin_contents (asection *sec, bfd_byte *contents);
1396 +*/
1397 +static void
1398 +release_contents (asection *sec, bfd_byte *contents);
1399 +static Elf_Internal_Sym *
1400 +retrieve_local_syms (bfd *input_bfd, bfd_boolean keep_memory);
1401 +/*
1402 +static void
1403 +pin_local_syms (bfd *input_bfd, Elf_Internal_Sym *isymbuf);
1404 +*/
1405 +static void
1406 +release_local_syms (bfd *input_bfd, Elf_Internal_Sym *isymbuf);
1407 +
1408 +/* During relaxation, we need to modify relocations, section contents,
1409 + and symbol definitions, and we need to keep the original values from
1410 + being reloaded from the input files, i.e., we need to "pin" the
1411 + modified values in memory. We also want to continue to observe the
1412 + setting of the "keep-memory" flag. The following functions wrap the
1413 + standard BFD functions to take care of this for us. */
1414 +
1415 +static Elf_Internal_Rela *
1416 +retrieve_internal_relocs (bfd *abfd, asection *sec, bfd_boolean keep_memory)
1417 +{
1418 + /* _bfd_elf_link_read_relocs knows about caching, so no need for us
1419 + to be clever here. */
1420 + return _bfd_elf_link_read_relocs(abfd, sec, NULL, NULL, keep_memory);
1421 +}
1422 +
1423 +static void
1424 +pin_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
1425 +{
1426 + elf_section_data (sec)->relocs = internal_relocs;
1427 +}
1428 +
1429 +static void
1430 +release_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
1431 +{
1432 + if (internal_relocs
1433 + && elf_section_data (sec)->relocs != internal_relocs)
1434 + free (internal_relocs);
1435 +}
1436 +
1437 +static bfd_byte *
1438 +retrieve_contents (bfd *abfd, asection *sec, bfd_boolean keep_memory)
1439 +{
1440 + bfd_byte *contents;
1441 + bfd_size_type sec_size;
1442 +
1443 + sec_size = bfd_get_section_limit (abfd, sec);
1444 + contents = elf_section_data (sec)->this_hdr.contents;
1445 +
1446 + if (contents == NULL && sec_size != 0)
1447 + {
1448 + if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1449 + {
1450 + if (contents)
1451 + free (contents);
1452 + return NULL;
1453 + }
1454 + if (keep_memory)
1455 + elf_section_data (sec)->this_hdr.contents = contents;
1456 + }
1457 + return contents;
1458 +}
1459 +
1460 +/*
1461 +static void
1462 +pin_contents (asection *sec, bfd_byte *contents)
1463 +{
1464 + elf_section_data (sec)->this_hdr.contents = contents;
1465 +}
1466 +*/
1467 +static void
1468 +release_contents (asection *sec, bfd_byte *contents)
1469 +{
1470 + if (contents && elf_section_data (sec)->this_hdr.contents != contents)
1471 + free (contents);
1472 +}
1473 +
1474 +static Elf_Internal_Sym *
1475 +retrieve_local_syms (bfd *input_bfd, bfd_boolean keep_memory)
1476 +{
1477 + Elf_Internal_Shdr *symtab_hdr;
1478 + Elf_Internal_Sym *isymbuf;
1479 + size_t locsymcount;
1480 +
1481 + symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1482 + locsymcount = symtab_hdr->sh_info;
1483 +
1484 + isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1485 + if (isymbuf == NULL && locsymcount != 0)
1486 + {
1487 + isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
1488 + NULL, NULL, NULL);
1489 + if (isymbuf && keep_memory)
1490 + symtab_hdr->contents = (unsigned char *) isymbuf;
1491 + }
1492 +
1493 + return isymbuf;
1494 +}
1495 +
1496 +/*
1497 +static void
1498 +pin_local_syms (bfd *input_bfd, Elf_Internal_Sym *isymbuf)
1499 +{
1500 + elf_tdata (input_bfd)->symtab_hdr.contents = (unsigned char *)isymbuf;
1501 +}
1502 +
1503 +*/
1504 +static void
1505 +release_local_syms (bfd *input_bfd, Elf_Internal_Sym *isymbuf)
1506 +{
1507 + if (isymbuf && (elf_tdata (input_bfd)->symtab_hdr.contents
1508 + != (unsigned char *)isymbuf))
1509 + free (isymbuf);
1510 +}
1511 +
1512 +\f/* Data structures used during relaxation. */
1513 +
1514 +enum relax_state_id {
1515 + RS_ERROR = -1,
1516 + RS_NONE = 0,
1517 + RS_ALIGN,
1518 + RS_CPENT,
1519 + RS_PIC_CALL,
1520 + RS_PIC_MCALL,
1521 + RS_PIC_RCALL2,
1522 + RS_PIC_RCALL1,
1523 + RS_PIC_LDA,
1524 + RS_PIC_LDW4,
1525 + RS_PIC_LDW3,
1526 + RS_PIC_SUB5,
1527 + RS_NOPIC_MCALL,
1528 + RS_NOPIC_RCALL2,
1529 + RS_NOPIC_RCALL1,
1530 + RS_NOPIC_LDW4,
1531 + RS_NOPIC_LDDPC,
1532 + RS_NOPIC_SUB5,
1533 + RS_NOPIC_MOV2,
1534 + RS_NOPIC_MOV1,
1535 + RS_RCALL2,
1536 + RS_RCALL1,
1537 + RS_BRC2,
1538 + RS_BRC1,
1539 + RS_BRAL,
1540 + RS_RJMP,
1541 + RS_MAX,
1542 +};
1543 +
1544 +enum reference_type {
1545 + REF_ABSOLUTE,
1546 + REF_PCREL,
1547 + REF_CPOOL,
1548 + REF_GOT,
1549 +};
1550 +
1551 +struct relax_state
1552 +{
1553 + const char *name;
1554 + enum relax_state_id id;
1555 + enum relax_state_id direct;
1556 + enum relax_state_id next;
1557 + enum relax_state_id prev;
1558 +
1559 + enum reference_type reftype;
1560 +
1561 + unsigned int r_type;
1562 +
1563 + bfd_vma opcode;
1564 + bfd_vma opcode_mask;
1565 +
1566 + bfd_signed_vma range_min;
1567 + bfd_signed_vma range_max;
1568 +
1569 + bfd_size_type size;
1570 +};
1571 +
1572 +/*
1573 + * This is for relocs that
1574 + * a) has an addend or is of type R_AVR32_DIFF32, and
1575 + * b) references a different section than it's in, and
1576 + * c) references a section that is relaxable
1577 + *
1578 + * as well as relocs that references the constant pool, in which case
1579 + * the add_frag member points to the frag containing the constant pool
1580 + * entry.
1581 + *
1582 + * Such relocs must be fixed up whenever we delete any code. Sections
1583 + * that don't have any relocs with all of the above properties don't
1584 + * have any additional reloc data, but sections that do will have
1585 + * additional data for all its relocs.
1586 + */
1587 +struct avr32_reloc_data
1588 +{
1589 + struct fragment *add_frag;
1590 + struct fragment *sub_frag;
1591 +};
1592 +
1593 +/*
1594 + * A 'fragment' is a relaxable entity, that is, code may be added or
1595 + * deleted at the end of a fragment. When this happens, all subsequent
1596 + * fragments in the list will have their offsets updated.
1597 + */
1598 +struct fragment
1599 +{
1600 + enum relax_state_id state;
1601 + enum relax_state_id initial_state;
1602 +
1603 + Elf_Internal_Rela *rela;
1604 + bfd_size_type size;
1605 + bfd_vma offset;
1606 + int size_adjust;
1607 + int offset_adjust;
1608 + bfd_boolean has_grown;
1609 +
1610 + /* Only used by constant pool entries. When this drops to zero, the
1611 + frag is discarded (i.e. size_adjust is set to -4.) */
1612 + int refcount;
1613 +};
1614 +
1615 +struct avr32_relax_data
1616 +{
1617 + unsigned int frag_count;
1618 + struct fragment *frag;
1619 + struct avr32_reloc_data *reloc_data;
1620 +
1621 + /* TRUE if this section has one or more relaxable relocations */
1622 + bfd_boolean is_relaxable;
1623 + unsigned int iteration;
1624 +};
1625 +
1626 +struct avr32_section_data
1627 +{
1628 + struct bfd_elf_section_data elf;
1629 + struct avr32_relax_data relax_data;
1630 +};
1631 +
1632 +\f/* Relax state definitions */
1633 +
1634 +#define PIC_MOV2_OPCODE 0xe0600000
1635 +#define PIC_MOV2_MASK 0xe1e00000
1636 +#define PIC_MOV2_RANGE_MIN (-1048576 * 4)
1637 +#define PIC_MOV2_RANGE_MAX (1048575 * 4)
1638 +#define PIC_MCALL_OPCODE 0xf0160000
1639 +#define PIC_MCALL_MASK 0xffff0000
1640 +#define PIC_MCALL_RANGE_MIN (-131072)
1641 +#define PIC_MCALL_RANGE_MAX (131068)
1642 +#define RCALL2_OPCODE 0xe0a00000
1643 +#define RCALL2_MASK 0xe1ef0000
1644 +#define RCALL2_RANGE_MIN (-2097152)
1645 +#define RCALL2_RANGE_MAX (2097150)
1646 +#define RCALL1_OPCODE 0xc00c0000
1647 +#define RCALL1_MASK 0xf00c0000
1648 +#define RCALL1_RANGE_MIN (-1024)
1649 +#define RCALL1_RANGE_MAX (1022)
1650 +#define PIC_LDW4_OPCODE 0xecf00000
1651 +#define PIC_LDW4_MASK 0xfff00000
1652 +#define PIC_LDW4_RANGE_MIN (-32768)
1653 +#define PIC_LDW4_RANGE_MAX (32767)
1654 +#define PIC_LDW3_OPCODE 0x6c000000
1655 +#define PIC_LDW3_MASK 0xfe000000
1656 +#define PIC_LDW3_RANGE_MIN (0)
1657 +#define PIC_LDW3_RANGE_MAX (124)
1658 +#define SUB5_PC_OPCODE 0xfec00000
1659 +#define SUB5_PC_MASK 0xfff00000
1660 +#define SUB5_PC_RANGE_MIN (-32768)
1661 +#define SUB5_PC_RANGE_MAX (32767)
1662 +#define NOPIC_MCALL_OPCODE 0xf01f0000
1663 +#define NOPIC_MCALL_MASK 0xffff0000
1664 +#define NOPIC_MCALL_RANGE_MIN PIC_MCALL_RANGE_MIN
1665 +#define NOPIC_MCALL_RANGE_MAX PIC_MCALL_RANGE_MAX
1666 +#define NOPIC_LDW4_OPCODE 0xfef00000
1667 +#define NOPIC_LDW4_MASK 0xfff00000
1668 +#define NOPIC_LDW4_RANGE_MIN PIC_LDW4_RANGE_MIN
1669 +#define NOPIC_LDW4_RANGE_MAX PIC_LDW4_RANGE_MAX
1670 +#define LDDPC_OPCODE 0x48000000
1671 +#define LDDPC_MASK 0xf8000000
1672 +#define LDDPC_RANGE_MIN 0
1673 +#define LDDPC_RANGE_MAX 508
1674 +
1675 +#define NOPIC_MOV2_OPCODE 0xe0600000
1676 +#define NOPIC_MOV2_MASK 0xe1e00000
1677 +#define NOPIC_MOV2_RANGE_MIN (-1048576)
1678 +#define NOPIC_MOV2_RANGE_MAX (1048575)
1679 +#define NOPIC_MOV1_OPCODE 0x30000000
1680 +#define NOPIC_MOV1_MASK 0xf0000000
1681 +#define NOPIC_MOV1_RANGE_MIN (-128)
1682 +#define NOPIC_MOV1_RANGE_MAX (127)
1683 +
1684 +/* Only brc2 variants with cond[3] == 0 is considered, since the
1685 + others are not relaxable. bral is a special case and is handled
1686 + separately. */
1687 +#define BRC2_OPCODE 0xe0800000
1688 +#define BRC2_MASK 0xe1e80000
1689 +#define BRC2_RANGE_MIN (-2097152)
1690 +#define BRC2_RANGE_MAX (2097150)
1691 +#define BRC1_OPCODE 0xc0000000
1692 +#define BRC1_MASK 0xf0080000
1693 +#define BRC1_RANGE_MIN (-256)
1694 +#define BRC1_RANGE_MAX (254)
1695 +#define BRAL_OPCODE 0xe08f0000
1696 +#define BRAL_MASK 0xe1ef0000
1697 +#define BRAL_RANGE_MIN BRC2_RANGE_MIN
1698 +#define BRAL_RANGE_MAX BRC2_RANGE_MAX
1699 +#define RJMP_OPCODE 0xc0080000
1700 +#define RJMP_MASK 0xf00c0000
1701 +#define RJMP_RANGE_MIN (-1024)
1702 +#define RJMP_RANGE_MAX (1022)
1703 +
1704 +/* Define a relax state using the GOT */
1705 +#define RG(id, dir, next, prev, r_type, opc, size) \
1706 + { "RS_"#id, RS_##id, RS_##dir, RS_##next, RS_##prev, REF_GOT, \
1707 + R_AVR32_##r_type, opc##_OPCODE, opc##_MASK, \
1708 + opc##_RANGE_MIN, opc##_RANGE_MAX, size }
1709 +/* Define a relax state using the Constant Pool */
1710 +#define RC(id, dir, next, prev, r_type, opc, size) \
1711 + { "RS_"#id, RS_##id, RS_##dir, RS_##next, RS_##prev, REF_CPOOL, \
1712 + R_AVR32_##r_type, opc##_OPCODE, opc##_MASK, \
1713 + opc##_RANGE_MIN, opc##_RANGE_MAX, size }
1714 +
1715 +/* Define a relax state using pc-relative direct reference */
1716 +#define RP(id, dir, next, prev, r_type, opc, size) \
1717 + { "RS_"#id, RS_##id, RS_##dir, RS_##next, RS_##prev, REF_PCREL, \
1718 + R_AVR32_##r_type, opc##_OPCODE, opc##_MASK, \
1719 + opc##_RANGE_MIN, opc##_RANGE_MAX, size }
1720 +
1721 +/* Define a relax state using non-pc-relative direct reference */
1722 +#define RD(id, dir, next, prev, r_type, opc, size) \
1723 + { "RS_"#id, RS_##id, RS_##dir, RS_##next, RS_##prev, REF_ABSOLUTE, \
1724 + R_AVR32_##r_type, opc##_OPCODE, opc##_MASK, \
1725 + opc##_RANGE_MIN, opc##_RANGE_MAX, size }
1726 +
1727 +/* Define a relax state that will be handled specially */
1728 +#define RS(id, r_type, size) \
1729 + { "RS_"#id, RS_##id, RS_NONE, RS_NONE, RS_NONE, REF_ABSOLUTE, \
1730 + R_AVR32_##r_type, 0, 0, 0, 0, size }
1731 +
1732 +const struct relax_state relax_state[RS_MAX] = {
1733 + RS(NONE, NONE, 0),
1734 + RS(ALIGN, ALIGN, 0),
1735 + RS(CPENT, 32_CPENT, 4),
1736 +
1737 + RG(PIC_CALL, PIC_RCALL1, PIC_MCALL, NONE, GOTCALL, PIC_MOV2, 10),
1738 + RG(PIC_MCALL, PIC_RCALL1, NONE, PIC_CALL, GOT18SW, PIC_MCALL, 4),
1739 + RP(PIC_RCALL2, NONE, PIC_RCALL1, PIC_MCALL, 22H_PCREL, RCALL2, 4),
1740 + RP(PIC_RCALL1, NONE, NONE, PIC_RCALL2, 11H_PCREL, RCALL1, 2),
1741 +
1742 + RG(PIC_LDA, PIC_SUB5, PIC_LDW4, NONE, LDA_GOT, PIC_MOV2, 8),
1743 + RG(PIC_LDW4, PIC_SUB5, PIC_LDW3, PIC_LDA, GOT16S, PIC_LDW4, 4),
1744 + RG(PIC_LDW3, PIC_SUB5, NONE, PIC_LDW4, GOT7UW, PIC_LDW3, 2),
1745 + RP(PIC_SUB5, NONE, NONE, PIC_LDW3, 16N_PCREL, SUB5_PC, 4),
1746 +
1747 + RC(NOPIC_MCALL, NOPIC_RCALL1, NONE, NONE, CPCALL, NOPIC_MCALL, 4),
1748 + RP(NOPIC_RCALL2, NONE, NOPIC_RCALL1, NOPIC_MCALL, 22H_PCREL, RCALL2, 4),
1749 + RP(NOPIC_RCALL1, NONE, NONE, NOPIC_RCALL2, 11H_PCREL, RCALL1, 2),
1750 +
1751 + RC(NOPIC_LDW4, NOPIC_MOV1, NOPIC_LDDPC, NONE, 16_CP, NOPIC_LDW4, 4),
1752 + RC(NOPIC_LDDPC, NOPIC_MOV1, NONE, NOPIC_LDW4, 9W_CP, LDDPC, 2),
1753 + RP(NOPIC_SUB5, NOPIC_MOV1, NONE, NOPIC_LDDPC, 16N_PCREL, SUB5_PC, 4),
1754 + RD(NOPIC_MOV2, NONE, NOPIC_MOV1, NOPIC_SUB5, 21S, NOPIC_MOV2, 4),
1755 + RD(NOPIC_MOV1, NONE, NONE, NOPIC_MOV2, 8S, NOPIC_MOV1, 2),
1756 +
1757 + RP(RCALL2, NONE, RCALL1, NONE, 22H_PCREL, RCALL2, 4),
1758 + RP(RCALL1, NONE, NONE, RCALL2, 11H_PCREL, RCALL1, 2),
1759 + RP(BRC2, NONE, BRC1, NONE, 22H_PCREL, BRC2, 4),
1760 + RP(BRC1, NONE, NONE, BRC2, 9H_PCREL, BRC1, 2),
1761 + RP(BRAL, NONE, RJMP, NONE, 22H_PCREL, BRAL, 4),
1762 + RP(RJMP, NONE, NONE, BRAL, 11H_PCREL, RJMP, 2),
1763 +};
1764 +
1765 +static bfd_boolean
1766 +avr32_elf_new_section_hook(bfd *abfd, asection *sec)
1767 +{
1768 + struct avr32_section_data *sdata;
1769 +
1770 + sdata = bfd_zalloc(abfd, sizeof(struct avr32_section_data));
1771 + if (!sdata)
1772 + return FALSE;
1773 +
1774 + sec->used_by_bfd = sdata;
1775 + return _bfd_elf_new_section_hook(abfd, sec);
1776 +}
1777 +
1778 +static struct avr32_relax_data *
1779 +avr32_relax_data(asection *sec)
1780 +{
1781 + struct avr32_section_data *sdata;
1782 +
1783 + BFD_ASSERT(sec->used_by_bfd);
1784 +
1785 + sdata = (struct avr32_section_data *)elf_section_data(sec);
1786 + return &sdata->relax_data;
1787 +}
1788 +
1789 +\f/* Link-time relaxation */
1790 +
1791 +static bfd_boolean
1792 +avr32_elf_relax_section(bfd *abfd, asection *sec,
1793 + struct bfd_link_info *info, bfd_boolean *again);
1794 +
1795 +enum relax_pass_id {
1796 + RELAX_PASS_SIZE_FRAGS,
1797 + RELAX_PASS_MOVE_DATA,
1798 +};
1799 +
1800 +/* Stolen from the xtensa port */
1801 +static int
1802 +internal_reloc_compare (const void *ap, const void *bp)
1803 +{
1804 + const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
1805 + const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
1806 +
1807 + if (a->r_offset != b->r_offset)
1808 + return (a->r_offset - b->r_offset);
1809 +
1810 + /* We don't need to sort on these criteria for correctness,
1811 + but enforcing a more strict ordering prevents unstable qsort
1812 + from behaving differently with different implementations.
1813 + Without the code below we get correct but different results
1814 + on Solaris 2.7 and 2.8. We would like to always produce the
1815 + same results no matter the host. */
1816 +
1817 + if (a->r_info != b->r_info)
1818 + return (a->r_info - b->r_info);
1819 +
1820 + return (a->r_addend - b->r_addend);
1821 +}
1822 +
1823 +static enum relax_state_id
1824 +get_pcrel22_relax_state(bfd *abfd, asection *sec, struct bfd_link_info *info,
1825 + const Elf_Internal_Rela *rela)
1826 +{
1827 + bfd_byte *contents;
1828 + bfd_vma insn;
1829 + enum relax_state_id rs = RS_NONE;
1830 +
1831 + contents = retrieve_contents(abfd, sec, info->keep_memory);
1832 + if (!contents)
1833 + return RS_ERROR;
1834 +
1835 + insn = bfd_get_32(abfd, contents + rela->r_offset);
1836 + if ((insn & RCALL2_MASK) == RCALL2_OPCODE)
1837 + rs = RS_RCALL2;
1838 + else if ((insn & BRAL_MASK) == BRAL_OPCODE)
1839 + /* Optimizing bral -> rjmp gets us into all kinds of
1840 + trouble with jump tables. Better not do it. */
1841 + rs = RS_NONE;
1842 + else if ((insn & BRC2_MASK) == BRC2_OPCODE)
1843 + rs = RS_BRC2;
1844 +
1845 + release_contents(sec, contents);
1846 +
1847 + return rs;
1848 +}
1849 +
1850 +static enum relax_state_id
1851 +get_initial_relax_state(bfd *abfd, asection *sec, struct bfd_link_info *info,
1852 + const Elf_Internal_Rela *rela)
1853 +{
1854 + switch (ELF_R_TYPE(rela->r_info))
1855 + {
1856 + case R_AVR32_GOTCALL:
1857 + return RS_PIC_CALL;
1858 + case R_AVR32_GOT18SW:
1859 + return RS_PIC_MCALL;
1860 + case R_AVR32_LDA_GOT:
1861 + return RS_PIC_LDA;
1862 + case R_AVR32_GOT16S:
1863 + return RS_PIC_LDW4;
1864 + case R_AVR32_CPCALL:
1865 + return RS_NOPIC_MCALL;
1866 + case R_AVR32_16_CP:
1867 + return RS_NOPIC_LDW4;
1868 + case R_AVR32_9W_CP:
1869 + return RS_NOPIC_LDDPC;
1870 + case R_AVR32_ALIGN:
1871 + return RS_ALIGN;
1872 + case R_AVR32_32_CPENT:
1873 + return RS_CPENT;
1874 + case R_AVR32_22H_PCREL:
1875 + return get_pcrel22_relax_state(abfd, sec, info, rela);
1876 + case R_AVR32_9H_PCREL:
1877 + return RS_BRC1;
1878 + default:
1879 + return RS_NONE;
1880 + }
1881 +}
1882 +
1883 +static bfd_boolean
1884 +reloc_is_cpool_ref(const Elf_Internal_Rela *rela)
1885 +{
1886 + switch (ELF_R_TYPE(rela->r_info))
1887 + {
1888 + case R_AVR32_CPCALL:
1889 + case R_AVR32_16_CP:
1890 + case R_AVR32_9W_CP:
1891 + return TRUE;
1892 + default:
1893 + return FALSE;
1894 + }
1895 +}
1896 +
1897 +static struct fragment *
1898 +new_frag(bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1899 + struct avr32_relax_data *rd, enum relax_state_id state,
1900 + Elf_Internal_Rela *rela)
1901 +{
1902 + struct fragment *frag;
1903 + bfd_size_type r_size;
1904 + bfd_vma r_offset;
1905 + unsigned int i = rd->frag_count;
1906 +
1907 + BFD_ASSERT(state >= RS_NONE && state < RS_MAX);
1908 +
1909 + rd->frag_count++;
1910 + frag = bfd_realloc(rd->frag, sizeof(struct fragment) * rd->frag_count);
1911 + if (!frag)
1912 + return NULL;
1913 + rd->frag = frag;
1914 +
1915 + frag += i;
1916 + memset(frag, 0, sizeof(struct fragment));
1917 +
1918 + if (state == RS_ALIGN)
1919 + r_size = (((rela->r_offset + (1 << rela->r_addend) - 1)
1920 + & ~((1 << rela->r_addend) - 1)) - rela->r_offset);
1921 + else
1922 + r_size = relax_state[state].size;
1923 +
1924 + if (rela)
1925 + r_offset = rela->r_offset;
1926 + else
1927 + r_offset = sec->size;
1928 +
1929 + if (i == 0)
1930 + {
1931 + frag->offset = 0;
1932 + frag->size = r_offset + r_size;
1933 + }
1934 + else
1935 + {
1936 + frag->offset = rd->frag[i - 1].offset + rd->frag[i - 1].size;
1937 + frag->size = r_offset + r_size - frag->offset;
1938 + }
1939 +
1940 + if (state != RS_CPENT)
1941 + /* Make sure we don't discard this frag */
1942 + frag->refcount = 1;
1943 +
1944 + frag->initial_state = frag->state = state;
1945 + frag->rela = rela;
1946 +
1947 + return frag;
1948 +}
1949 +
1950 +static struct fragment *
1951 +find_frag(asection *sec, bfd_vma offset)
1952 +{
1953 + struct fragment *first, *last;
1954 + struct avr32_relax_data *rd = avr32_relax_data(sec);
1955 +
1956 + if (rd->frag_count == 0)
1957 + return NULL;
1958 +
1959 + first = &rd->frag[0];
1960 + last = &rd->frag[rd->frag_count - 1];
1961 +
1962 + /* This may be a reloc referencing the end of a section. The last
1963 + frag will never have a reloc associated with it, so its size will
1964 + never change, thus the offset adjustment of the last frag will
1965 + always be the same as the offset adjustment of the end of the
1966 + section. */
1967 + if (offset == sec->size)
1968 + {
1969 + BFD_ASSERT(last->offset + last->size == sec->size);
1970 + BFD_ASSERT(!last->rela);
1971 + return last;
1972 + }
1973 +
1974 + while (first <= last)
1975 + {
1976 + struct fragment *mid;
1977 +
1978 + mid = (last - first) / 2 + first;
1979 + if ((mid->offset + mid->size) <= offset)
1980 + first = mid + 1;
1981 + else if (mid->offset > offset)
1982 + last = mid - 1;
1983 + else
1984 + return mid;
1985 + }
1986 +
1987 + return NULL;
1988 +}
1989 +
1990 +/* Look through all relocs in a section and determine if any relocs
1991 + may be affected by relaxation in other sections. If so, allocate
1992 + an array of additional relocation data which links the affected
1993 + relocations to the frag(s) where the relaxation may occur.
1994 +
1995 + This function also links cpool references to cpool entries and
1996 + increments the refcount of the latter when this happens. */
1997 +
1998 +static bfd_boolean
1999 +allocate_reloc_data(bfd *abfd, asection *sec, Elf_Internal_Rela *relocs,
2000 + struct bfd_link_info *info)
2001 +{
2002 + Elf_Internal_Shdr *symtab_hdr;
2003 + Elf_Internal_Sym *isymbuf = NULL;
2004 + struct avr32_relax_data *rd;
2005 + unsigned int i;
2006 + bfd_boolean ret = FALSE;
2007 +
2008 + symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2009 + rd = avr32_relax_data(sec);
2010 +
2011 + RDBG("%s<%s>: allocate_reloc_data\n", abfd->filename, sec->name);
2012 +
2013 + for (i = 0; i < sec->reloc_count; i++)
2014 + {
2015 + Elf_Internal_Rela *rel = &relocs[i];
2016 + asection *sym_sec;
2017 + unsigned long r_symndx;
2018 + bfd_vma sym_value;
2019 +
2020 + if (!rel->r_addend && ELF_R_TYPE(rel->r_info) != R_AVR32_DIFF32
2021 + && !reloc_is_cpool_ref(rel))
2022 + continue;
2023 +
2024 + r_symndx = ELF_R_SYM(rel->r_info);
2025 +
2026 + if (r_symndx < symtab_hdr->sh_info)
2027 + {
2028 + Elf_Internal_Sym *isym;
2029 +
2030 + if (!isymbuf)
2031 + isymbuf = retrieve_local_syms(abfd, info->keep_memory);
2032 + if (!isymbuf)
2033 + return FALSE;
2034 +
2035 + isym = &isymbuf[r_symndx];
2036 + sym_sec = bfd_section_from_elf_index(abfd, isym->st_shndx);
2037 + sym_value = isym->st_value;
2038 + }
2039 + else
2040 + {
2041 + struct elf_link_hash_entry *h;
2042 +
2043 + h = elf_sym_hashes(abfd)[r_symndx - symtab_hdr->sh_info];
2044 +
2045 + while (h->root.type == bfd_link_hash_indirect
2046 + || h->root.type == bfd_link_hash_warning)
2047 + h = (struct elf_link_hash_entry *)h->root.u.i.link;
2048 +
2049 + if (h->root.type != bfd_link_hash_defined
2050 + && h->root.type != bfd_link_hash_defweak)
2051 + continue;
2052 +
2053 + sym_sec = h->root.u.def.section;
2054 + sym_value = h->root.u.def.value;
2055 + }
2056 +
2057 + if (sym_sec && avr32_relax_data(sym_sec)->is_relaxable)
2058 + {
2059 + bfd_size_type size;
2060 + struct fragment *frag;
2061 +
2062 + if (!rd->reloc_data)
2063 + {
2064 + size = sizeof(struct avr32_reloc_data) * sec->reloc_count;
2065 + rd->reloc_data = bfd_zalloc(abfd, size);
2066 + if (!rd->reloc_data)
2067 + goto out;
2068 + }
2069 +
2070 + RDBG("[%3d] 0x%04lx: target: 0x%lx + 0x%lx",
2071 + i, rel->r_offset, sym_value, rel->r_addend);
2072 +
2073 + frag = find_frag(sym_sec, sym_value + rel->r_addend);
2074 + BFD_ASSERT(frag);
2075 + rd->reloc_data[i].add_frag = frag;
2076 +
2077 + RDBG(" -> %s<%s>:%04lx\n", sym_sec->owner->filename, sym_sec->name,
2078 + frag->rela ? frag->rela->r_offset : sym_sec->size);
2079 +
2080 + if (reloc_is_cpool_ref(rel))
2081 + {
2082 + BFD_ASSERT(ELF_R_TYPE(frag->rela->r_info) == R_AVR32_32_CPENT);
2083 + frag->refcount++;
2084 + }
2085 +
2086 + if (ELF_R_TYPE(rel->r_info) == R_AVR32_DIFF32)
2087 + {
2088 + bfd_byte *contents;
2089 + bfd_signed_vma diff;
2090 +
2091 + contents = retrieve_contents(abfd, sec, info->keep_memory);
2092 + if (!contents)
2093 + goto out;
2094 +
2095 + diff = bfd_get_signed_32(abfd, contents + rel->r_offset);
2096 + frag = find_frag(sym_sec, sym_value + rel->r_addend + diff);
2097 + BFD_ASSERT(frag);
2098 + rd->reloc_data[i].sub_frag = frag;
2099 +
2100 + release_contents(sec, contents);
2101 + }
2102 + }
2103 + }
2104 +
2105 + ret = TRUE;
2106 +
2107 + out:
2108 + release_local_syms(abfd, isymbuf);
2109 + return ret;
2110 +}
2111 +
2112 +static bfd_boolean
2113 +global_sym_set_frag(struct elf_avr32_link_hash_entry *havr,
2114 + struct bfd_link_info *info ATTRIBUTE_UNUSED)
2115 +{
2116 + struct fragment *frag;
2117 + asection *sec;
2118 +
2119 + if (havr->root.root.type != bfd_link_hash_defined
2120 + && havr->root.root.type != bfd_link_hash_defweak)
2121 + return TRUE;
2122 +
2123 + sec = havr->root.root.u.def.section;
2124 + if (bfd_is_const_section(sec)
2125 + || !avr32_relax_data(sec)->is_relaxable)
2126 + return TRUE;
2127 +
2128 + frag = find_frag(sec, havr->root.root.u.def.value);
2129 + if (!frag)
2130 + {
2131 + unsigned int i;
2132 + struct avr32_relax_data *rd = avr32_relax_data(sec);
2133 +
2134 + RDBG("In %s: No frag for %s <%s+%lu> (limit %lu)\n",
2135 + sec->owner->filename, havr->root.root.root.string,
2136 + sec->name, havr->root.root.u.def.value, sec->size);
2137 + for (i = 0; i < rd->frag_count; i++)
2138 + RDBG(" %8lu - %8lu\n", rd->frag[i].offset,
2139 + rd->frag[i].offset + rd->frag[i].size);
2140 + }
2141 + BFD_ASSERT(frag);
2142 +
2143 + havr->sym_frag = frag;
2144 + return TRUE;
2145 +}
2146 +
2147 +static bfd_boolean
2148 +analyze_relocations(struct bfd_link_info *info)
2149 +{
2150 + bfd *abfd;
2151 + asection *sec;
2152 +
2153 + /* Divide all relaxable sections into fragments */
2154 + for (abfd = info->input_bfds; abfd; abfd = abfd->link_next)
2155 + {
2156 + if (!(elf_elfheader(abfd)->e_flags & EF_AVR32_LINKRELAX))
2157 + {
2158 + if (!(*info->callbacks->warning)
2159 + (info, _("input is not relaxable"), NULL, abfd, NULL, 0))
2160 + return FALSE;
2161 + continue;
2162 + }
2163 +
2164 + for (sec = abfd->sections; sec; sec = sec->next)
2165 + {
2166 + struct avr32_relax_data *rd;
2167 + struct fragment *frag;
2168 + Elf_Internal_Rela *relocs;
2169 + unsigned int i;
2170 + bfd_boolean ret = TRUE;
2171 +
2172 + if (!(sec->flags & SEC_RELOC) || sec->reloc_count == 0)
2173 + continue;
2174 +
2175 + rd = avr32_relax_data(sec);
2176 +
2177 + relocs = retrieve_internal_relocs(abfd, sec, info->keep_memory);
2178 + if (!relocs)
2179 + return FALSE;
2180 +
2181 + qsort(relocs, sec->reloc_count, sizeof(Elf_Internal_Rela),
2182 + internal_reloc_compare);
2183 +
2184 + for (i = 0; i < sec->reloc_count; i++)
2185 + {
2186 + enum relax_state_id state;
2187 +
2188 + ret = FALSE;
2189 + state = get_initial_relax_state(abfd, sec, info, &relocs[i]);
2190 + if (state == RS_ERROR)
2191 + break;
2192 +
2193 + if (state)
2194 + {
2195 + frag = new_frag(abfd, sec, rd, state, &relocs[i]);
2196 + if (!frag)
2197 + break;
2198 +
2199 + pin_internal_relocs(sec, relocs);
2200 + rd->is_relaxable = TRUE;
2201 + }
2202 +
2203 + ret = TRUE;
2204 + }
2205 +
2206 + release_internal_relocs(sec, relocs);
2207 + if (!ret)
2208 + return ret;
2209 +
2210 + if (rd->is_relaxable)
2211 + {
2212 + frag = new_frag(abfd, sec, rd, RS_NONE, NULL);
2213 + if (!frag)
2214 + return FALSE;
2215 + }
2216 + }
2217 + }
2218 +
2219 + /* Link each global symbol to the fragment where it's defined. */
2220 + elf_link_hash_traverse(elf_hash_table(info), global_sym_set_frag, info);
2221 +
2222 + /* Do the same for local symbols. */
2223 + for (abfd = info->input_bfds; abfd; abfd = abfd->link_next)
2224 + {
2225 + Elf_Internal_Sym *isymbuf, *isym;
2226 + struct fragment **local_sym_frag;
2227 + unsigned int i, sym_count;
2228 +
2229 + sym_count = elf_tdata(abfd)->symtab_hdr.sh_info;
2230 + if (sym_count == 0)
2231 + continue;
2232 +
2233 + local_sym_frag = bfd_zalloc(abfd, sym_count * sizeof(struct fragment *));
2234 + if (!local_sym_frag)
2235 + return FALSE;
2236 + elf_tdata(abfd)->local_sym_frag = local_sym_frag;
2237 +
2238 + isymbuf = retrieve_local_syms(abfd, info->keep_memory);
2239 + if (!isymbuf)
2240 + return FALSE;
2241 +
2242 + for (i = 0; i < sym_count; i++)
2243 + {
2244 + struct avr32_relax_data *rd;
2245 + struct fragment *frag;
2246 + asection *sec;
2247 +
2248 + isym = &isymbuf[i];
2249 +
2250 + sec = bfd_section_from_elf_index(abfd, isym->st_shndx);
2251 + if (!sec)
2252 + continue;
2253 +
2254 + rd = avr32_relax_data(sec);
2255 + if (!rd->is_relaxable)
2256 + continue;
2257 +
2258 + frag = find_frag(sec, isym->st_value);
2259 + BFD_ASSERT(frag);
2260 +
2261 + local_sym_frag[i] = frag;
2262 + }
2263 +
2264 + release_local_syms(abfd, isymbuf);
2265 + }
2266 +
2267 + /* And again for relocs with addends and constant pool references */
2268 + for (abfd = info->input_bfds; abfd; abfd = abfd->link_next)
2269 + for (sec = abfd->sections; sec; sec = sec->next)
2270 + {
2271 + Elf_Internal_Rela *relocs;
2272 + bfd_boolean ret;
2273 +
2274 + if (!(sec->flags & SEC_RELOC) || sec->reloc_count == 0)
2275 + continue;
2276 +
2277 + relocs = retrieve_internal_relocs(abfd, sec, info->keep_memory);
2278 + if (!relocs)
2279 + return FALSE;
2280 +
2281 + ret = allocate_reloc_data(abfd, sec, relocs, info);
2282 +
2283 + release_internal_relocs(sec, relocs);
2284 + if (ret == FALSE)
2285 + return ret;
2286 + }
2287 +
2288 + return TRUE;
2289 +}
2290 +
2291 +static bfd_boolean
2292 +rs_is_good_enough(const struct relax_state *rs, struct fragment *frag,
2293 + bfd_vma symval, bfd_vma addr, struct got_entry *got,
2294 + struct avr32_reloc_data *ind_data,
2295 + bfd_signed_vma offset_adjust)
2296 +{
2297 + bfd_signed_vma target = 0;
2298 +
2299 + switch (rs->reftype)
2300 + {
2301 + case REF_ABSOLUTE:
2302 + target = symval;
2303 + break;
2304 + case REF_PCREL:
2305 + target = symval - addr;
2306 + break;
2307 + case REF_CPOOL:
2308 + /* cpool frags are always in the same section and always after
2309 + all frags referring to it. So it's always correct to add in
2310 + offset_adjust here. */
2311 + target = (ind_data->add_frag->offset + ind_data->add_frag->offset_adjust
2312 + + offset_adjust - frag->offset - frag->offset_adjust);
2313 + break;
2314 + case REF_GOT:
2315 + target = got->offset;
2316 + break;
2317 + default:
2318 + abort();
2319 + }
2320 +
2321 + if (target >= rs->range_min && target <= rs->range_max)
2322 + return TRUE;
2323 + else
2324 + return FALSE;
2325 +}
2326 +
2327 +static bfd_boolean
2328 +avr32_size_frags(bfd *abfd, asection *sec, struct bfd_link_info *info)
2329 +{
2330 + struct elf_avr32_link_hash_table *htab;
2331 + struct avr32_relax_data *rd;
2332 + Elf_Internal_Shdr *symtab_hdr;
2333 + Elf_Internal_Rela *relocs = NULL;
2334 + Elf_Internal_Sym *isymbuf = NULL;
2335 + struct got_entry **local_got_ents;
2336 + struct fragment **local_sym_frag;
2337 + bfd_boolean ret = FALSE;
2338 + bfd_signed_vma delta = 0;
2339 + unsigned int i;
2340 +
2341 + htab = avr32_elf_hash_table(info);
2342 + rd = avr32_relax_data(sec);
2343 +
2344 + if (sec == htab->sgot)
2345 + {
2346 + RDBG("Relaxing GOT section (vma: 0x%lx)\n",
2347 + sec->output_section->vma + sec->output_offset);
2348 + if (assign_got_offsets(htab))
2349 + htab->repeat_pass = TRUE;
2350 + return TRUE;
2351 + }
2352 +
2353 + if (!rd->is_relaxable)
2354 + return TRUE;
2355 +
2356 + if (!sec->rawsize)
2357 + sec->rawsize = sec->size;
2358 +
2359 + symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2360 + relocs = retrieve_internal_relocs(abfd, sec, info->keep_memory);
2361 + if (!relocs)
2362 + goto out;
2363 +
2364 + isymbuf = retrieve_local_syms(abfd, info->keep_memory);
2365 + if (!isymbuf)
2366 + goto out;
2367 +
2368 + local_got_ents = elf_local_got_ents(abfd);
2369 + local_sym_frag = elf_tdata(abfd)->local_sym_frag;
2370 +
2371 + RDBG("size_frags: %s<%s>\n vma: 0x%08lx, size: 0x%08lx\n",
2372 + abfd->filename, sec->name,
2373 + sec->output_section->vma + sec->output_offset, sec->size);
2374 +
2375 + for (i = 0; i < rd->frag_count; i++)
2376 + {
2377 + struct fragment *frag = &rd->frag[i];
2378 + struct avr32_reloc_data *r_data = NULL, *ind_data = NULL;
2379 + const struct relax_state *state, *next_state;
2380 + struct fragment *target_frag = NULL;
2381 + asection *sym_sec = NULL;
2382 + Elf_Internal_Rela *rela;
2383 + struct got_entry *got;
2384 + bfd_vma symval, r_offset, addend, addr;
2385 + bfd_signed_vma size_adjust = 0, distance;
2386 + unsigned long r_symndx;
2387 + bfd_boolean defined = TRUE, dynamic = FALSE;
2388 + unsigned char sym_type;
2389 +
2390 + frag->offset_adjust += delta;
2391 + state = next_state = &relax_state[frag->state];
2392 + rela = frag->rela;
2393 +
2394 + BFD_ASSERT(state->id == frag->state);
2395 +
2396 + RDBG(" 0x%04lx%c%d: %s [size %ld]", rela ? rela->r_offset : sec->rawsize,
2397 + (frag->offset_adjust < 0)?'-':'+',
2398 + abs(frag->offset_adjust), state->name, state->size);
2399 +
2400 + if (!rela)
2401 + {
2402 + RDBG(": no reloc, ignoring\n");
2403 + continue;
2404 + }
2405 +
2406 + BFD_ASSERT((unsigned int)(rela - relocs) < sec->reloc_count);
2407 + BFD_ASSERT(state != RS_NONE);
2408 +
2409 + r_offset = rela->r_offset + frag->offset_adjust;
2410 + addr = sec->output_section->vma + sec->output_offset + r_offset;
2411 +
2412 + switch (frag->state)
2413 + {
2414 + case RS_ALIGN:
2415 + size_adjust = ((addr + (1 << rela->r_addend) - 1)
2416 + & ~((1 << rela->r_addend) - 1));
2417 + size_adjust -= (sec->output_section->vma + sec->output_offset
2418 + + frag->offset + frag->offset_adjust
2419 + + frag->size + frag->size_adjust);
2420 +
2421 + RDBG(": adjusting size %lu -> %lu\n", frag->size + frag->size_adjust,
2422 + frag->size + frag->size_adjust + size_adjust);
2423 + break;
2424 +
2425 + case RS_CPENT:
2426 + if (frag->refcount == 0 && frag->size_adjust == 0)
2427 + {
2428 + RDBG(": discarding frag\n");
2429 + size_adjust = -4;
2430 + }
2431 + else if (frag->refcount > 0 && frag->size_adjust < 0)
2432 + {
2433 + RDBG(": un-discarding frag\n");
2434 + size_adjust = 4;
2435 + }
2436 + break;
2437 +
2438 + default:
2439 + if (rd->reloc_data)
2440 + r_data = &rd->reloc_data[frag->rela - relocs];
2441 +
2442 + /* If this is a cpool reference, we want the symbol that the
2443 + cpool entry refers to, not the symbol for the cpool entry
2444 + itself, as we already know what frag it's in. */
2445 + if (relax_state[frag->initial_state].reftype == REF_CPOOL)
2446 + {
2447 + Elf_Internal_Rela *irela = r_data->add_frag->rela;
2448 +
2449 + r_symndx = ELF_R_SYM(irela->r_info);
2450 + addend = irela->r_addend;
2451 +
2452 + /* The constant pool must be in the same section as the
2453 + reloc referring to it. */
2454 + BFD_ASSERT((unsigned long)(irela - relocs) < sec->reloc_count);
2455 +
2456 + ind_data = r_data;
2457 + r_data = &rd->reloc_data[irela - relocs];
2458 + }
2459 + else
2460 + {
2461 + r_symndx = ELF_R_SYM(rela->r_info);
2462 + addend = rela->r_addend;
2463 + }
2464 +
2465 + /* Get the value of the symbol referred to by the reloc. */
2466 + if (r_symndx < symtab_hdr->sh_info)
2467 + {
2468 + Elf_Internal_Sym *isym;
2469 +
2470 + isym = isymbuf + r_symndx;
2471 + symval = 0;
2472 +
2473 + RDBG(" local sym %lu: ", r_symndx);
2474 +
2475 + if (isym->st_shndx == SHN_UNDEF)
2476 + defined = FALSE;
2477 + else if (isym->st_shndx == SHN_ABS)
2478 + sym_sec = bfd_abs_section_ptr;
2479 + else if (isym->st_shndx == SHN_COMMON)
2480 + sym_sec = bfd_com_section_ptr;
2481 + else
2482 + sym_sec = bfd_section_from_elf_index(abfd, isym->st_shndx);
2483 +
2484 + symval = isym->st_value;
2485 + sym_type = ELF_ST_TYPE(isym->st_info);
2486 + target_frag = local_sym_frag[r_symndx];
2487 +
2488 + if (local_got_ents)
2489 + got = local_got_ents[r_symndx];
2490 + else
2491 + got = NULL;
2492 + }
2493 + else
2494 + {
2495 + /* Global symbol */
2496 + unsigned long index;
2497 + struct elf_link_hash_entry *h;
2498 + struct elf_avr32_link_hash_entry *havr;
2499 +
2500 + index = r_symndx - symtab_hdr->sh_info;
2501 + h = elf_sym_hashes(abfd)[index];
2502 + BFD_ASSERT(h != NULL);
2503 +
2504 + while (h->root.type == bfd_link_hash_indirect
2505 + || h->root.type == bfd_link_hash_warning)
2506 + h = (struct elf_link_hash_entry *)h->root.u.i.link;
2507 +
2508 + havr = (struct elf_avr32_link_hash_entry *)h;
2509 + got = h->got.glist;
2510 +
2511 + symval = 0;
2512 +
2513 + RDBG(" %s: ", h->root.root.string);
2514 +
2515 + if (h->root.type != bfd_link_hash_defined
2516 + && h->root.type != bfd_link_hash_defweak)
2517 + {
2518 + RDBG("(undef)");
2519 + defined = FALSE;
2520 + }
2521 + else if ((info->shared && !info->symbolic && h->dynindx != -1)
2522 + || (htab->root.dynamic_sections_created
2523 + && h->def_dynamic && !h->def_regular))
2524 + {
2525 + RDBG("(dynamic)");
2526 + dynamic = TRUE;
2527 + sym_sec = h->root.u.def.section;
2528 + }
2529 + else
2530 + {
2531 + sym_sec = h->root.u.def.section;
2532 + symval = h->root.u.def.value;
2533 + target_frag = havr->sym_frag;
2534 + }
2535 +
2536 + sym_type = h->type;
2537 + }
2538 +
2539 + /* Thanks to elf32-ppc for this one. */
2540 + if (sym_sec && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2541 + {
2542 + /* At this stage in linking, no SEC_MERGE symbol has been
2543 + adjusted, so all references to such symbols need to be
2544 + passed through _bfd_merged_section_offset. (Later, in
2545 + relocate_section, all SEC_MERGE symbols *except* for
2546 + section symbols have been adjusted.)
2547 +
2548 + SEC_MERGE sections are not relaxed by us, as they
2549 + shouldn't contain any code. */
2550 +
2551 + BFD_ASSERT(!target_frag && !(r_data && r_data->add_frag));
2552 +
2553 + /* gas may reduce relocations against symbols in SEC_MERGE
2554 + sections to a relocation against the section symbol when
2555 + the original addend was zero. When the reloc is against
2556 + a section symbol we should include the addend in the
2557 + offset passed to _bfd_merged_section_offset, since the
2558 + location of interest is the original symbol. On the
2559 + other hand, an access to "sym+addend" where "sym" is not
2560 + a section symbol should not include the addend; Such an
2561 + access is presumed to be an offset from "sym"; The
2562 + location of interest is just "sym". */
2563 + RDBG("\n MERGE: %s: 0x%lx+0x%lx+0x%lx -> ",
2564 + (sym_type == STT_SECTION)?"section":"not section",
2565 + sym_sec->output_section->vma + sym_sec->output_offset,
2566 + symval, addend);
2567 +
2568 + if (sym_type == STT_SECTION)
2569 + symval += addend;
2570 +
2571 + symval = (_bfd_merged_section_offset
2572 + (abfd, &sym_sec,
2573 + elf_section_data(sym_sec)->sec_info, symval));
2574 +
2575 + if (sym_type != STT_SECTION)
2576 + symval += addend;
2577 + }
2578 + else
2579 + symval += addend;
2580 +
2581 + if (defined && !dynamic)
2582 + {
2583 + RDBG("0x%lx+0x%lx",
2584 + sym_sec->output_section->vma + sym_sec->output_offset,
2585 + symval);
2586 + symval += sym_sec->output_section->vma + sym_sec->output_offset;
2587 + }
2588 +
2589 + if (r_data && r_data->add_frag)
2590 + /* If the add_frag pointer is set, it means that this reloc
2591 + has an addend that may be affected by relaxation. */
2592 + target_frag = r_data->add_frag;
2593 +
2594 + if (target_frag)
2595 + {
2596 + symval += target_frag->offset_adjust;
2597 +
2598 + /* If target_frag comes after this frag in the same
2599 + section, we should assume that it will be moved by
2600 + the same amount we are. */
2601 + if ((target_frag - rd->frag) < (int)rd->frag_count
2602 + && target_frag > frag)
2603 + symval += delta;
2604 + }
2605 +
2606 + distance = symval - addr;
2607 +
2608 + /* First, try to make a direct reference. If the symbol is
2609 + dynamic or undefined, we must take care not to change its
2610 + reference type, that is, we can't make it direct.
2611 +
2612 + Also, it seems like some sections may actually be resized
2613 + after the relaxation code is done, so we can't really
2614 + trust that our "distance" is correct. There's really no
2615 + easy solution to this problem, so we'll just disallow
2616 + direct references to SEC_DATA sections.
2617 +
2618 + Oh, and .bss isn't actually SEC_DATA, so we disallow
2619 + !SEC_HAS_CONTENTS as well. */
2620 + if (!dynamic && defined
2621 + && (htab->direct_data_refs
2622 + || (!(sym_sec->flags & SEC_DATA)
2623 + && (sym_sec->flags & SEC_HAS_CONTENTS)))
2624 + && next_state->direct)
2625 + {
2626 + next_state = &relax_state[next_state->direct];
2627 + RDBG(" D-> %s", next_state->name);
2628 + }
2629 +
2630 + /* Iterate backwards until we find a state that fits. */
2631 + while (next_state->prev
2632 + && !rs_is_good_enough(next_state, frag, symval, addr,
2633 + got, ind_data, delta))
2634 + {
2635 + next_state = &relax_state[next_state->prev];
2636 + RDBG(" P-> %s", next_state->name);
2637 + }
2638 +
2639 + /* Then try to find the best possible state. */
2640 + while (next_state->next)
2641 + {
2642 + const struct relax_state *candidate;
2643 +
2644 + candidate = &relax_state[next_state->next];
2645 + if (!rs_is_good_enough(candidate, frag, symval, addr, got,
2646 + ind_data, delta))
2647 + break;
2648 +
2649 + next_state = candidate;
2650 + RDBG(" N-> %s", next_state->name);
2651 + }
2652 +
2653 + RDBG(" [size %ld]\n", next_state->size);
2654 +
2655 + BFD_ASSERT(next_state->id);
2656 + BFD_ASSERT(!dynamic || next_state->reftype == REF_GOT);
2657 +
2658 + size_adjust = next_state->size - state->size;
2659 +
2660 + /* There's a theoretical possibility that shrinking one frag
2661 + may cause another to grow, which may cause the first one to
2662 + grow as well, and we're back where we started. Avoid this
2663 + scenario by disallowing a frag that has grown to ever
2664 + shrink again. */
2665 + if (state->reftype == REF_GOT && next_state->reftype != REF_GOT)
2666 + {
2667 + if (frag->has_grown)
2668 + next_state = state;
2669 + else
2670 + unref_got_entry(htab, got);
2671 + }
2672 + else if (state->reftype != REF_GOT && next_state->reftype == REF_GOT)
2673 + {
2674 + ref_got_entry(htab, got);
2675 + frag->has_grown = TRUE;
2676 + }
2677 + else if (state->reftype == REF_CPOOL
2678 + && next_state->reftype != REF_CPOOL)
2679 + {
2680 + if (frag->has_grown)
2681 + next_state = state;
2682 + else
2683 + ind_data->add_frag->refcount--;
2684 + }
2685 + else if (state->reftype != REF_CPOOL
2686 + && next_state->reftype == REF_CPOOL)
2687 + {
2688 + ind_data->add_frag->refcount++;
2689 + frag->has_grown = TRUE;
2690 + }
2691 + else
2692 + {
2693 + if (frag->has_grown && size_adjust < 0)
2694 + next_state = state;
2695 + else if (size_adjust > 0)
2696 + frag->has_grown = TRUE;
2697 + }
2698 +
2699 + size_adjust = next_state->size - state->size;
2700 + frag->state = next_state->id;
2701 +
2702 + break;
2703 + }
2704 +
2705 + if (size_adjust)
2706 + htab->repeat_pass = TRUE;
2707 +
2708 + frag->size_adjust += size_adjust;
2709 + sec->size += size_adjust;
2710 + delta += size_adjust;
2711 +
2712 + BFD_ASSERT((frag->offset + frag->offset_adjust
2713 + + frag->size + frag->size_adjust)
2714 + == (frag[1].offset + frag[1].offset_adjust + delta));
2715 + }
2716 +
2717 + ret = TRUE;
2718 +
2719 + out:
2720 + release_local_syms(abfd, isymbuf);
2721 + release_internal_relocs(sec, relocs);
2722 + return ret;
2723 +}
2724 +
2725 +static bfd_boolean
2726 +adjust_global_symbol(struct elf_avr32_link_hash_entry *havr,
2727 + struct bfd_link_info *info ATTRIBUTE_UNUSED)
2728 +{
2729 + struct elf_link_hash_entry *h = &havr->root;
2730 +
2731 + if (havr->sym_frag && (h->root.type == bfd_link_hash_defined
2732 + || h->root.type == bfd_link_hash_defweak))
2733 + {
2734 + RDBG("adjust_global_symbol: %s 0x%08lx -> 0x%08lx\n",
2735 + h->root.root.string, h->root.u.def.value,
2736 + h->root.u.def.value + havr->sym_frag->offset_adjust);
2737 + h->root.u.def.value += havr->sym_frag->offset_adjust;
2738 + }
2739 + return TRUE;
2740 +}
2741 +
2742 +static bfd_boolean
2743 +adjust_syms(struct bfd_link_info *info)
2744 +{
2745 + struct elf_avr32_link_hash_table *htab;
2746 + bfd *abfd;
2747 +
2748 + htab = avr32_elf_hash_table(info);
2749 + elf_link_hash_traverse(&htab->root, adjust_global_symbol, info);
2750 +
2751 + for (abfd = info->input_bfds; abfd; abfd = abfd->link_next)
2752 + {
2753 + Elf_Internal_Sym *isymbuf;
2754 + struct fragment **local_sym_frag, *frag;
2755 + unsigned int i, sym_count;
2756 +
2757 + sym_count = elf_tdata(abfd)->symtab_hdr.sh_info;
2758 + if (sym_count == 0)
2759 + continue;
2760 +
2761 + isymbuf = retrieve_local_syms(abfd, info->keep_memory);
2762 + if (!isymbuf)
2763 + return FALSE;
2764 +
2765 + local_sym_frag = elf_tdata(abfd)->local_sym_frag;
2766 +
2767 + for (i = 0; i < sym_count; i++)
2768 + {
2769 + frag = local_sym_frag[i];
2770 + if (frag)
2771 + {
2772 + RDBG("adjust_local_symbol: %s[%u] 0x%08lx -> 0x%08lx\n",
2773 + abfd->filename, i, isymbuf[i].st_value,
2774 + isymbuf[i].st_value + frag->offset_adjust);
2775 + isymbuf[i].st_value += frag->offset_adjust;
2776 + }
2777 + }
2778 +
2779 + release_local_syms(abfd, isymbuf);
2780 + }
2781 +
2782 + htab->symbols_adjusted = TRUE;
2783 + return TRUE;
2784 +}
2785 +
2786 +static bfd_boolean
2787 +adjust_relocs(bfd *abfd, asection *sec, struct bfd_link_info *info)
2788 +{
2789 + struct avr32_relax_data *rd;
2790 + Elf_Internal_Rela *relocs;
2791 + Elf_Internal_Shdr *symtab_hdr;
2792 + unsigned int i;
2793 + bfd_boolean ret = FALSE;
2794 +
2795 + rd = avr32_relax_data(sec);
2796 + if (!rd->reloc_data)
2797 + return TRUE;
2798 +
2799 + RDBG("adjust_relocs: %s<%s> (count: %u)\n", abfd->filename, sec->name,
2800 + sec->reloc_count);
2801 +
2802 + relocs = retrieve_internal_relocs(abfd, sec, info->keep_memory);
2803 + if (!relocs)
2804 + return FALSE;
2805 +
2806 + symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2807 +
2808 + for (i = 0; i < sec->reloc_count; i++)
2809 + {
2810 + Elf_Internal_Rela *rela = &relocs[i];
2811 + struct avr32_reloc_data *r_data = &rd->reloc_data[i];
2812 + struct fragment *sym_frag;
2813 + unsigned long r_symndx;
2814 +
2815 + if (r_data->add_frag)
2816 + {
2817 + r_symndx = ELF_R_SYM(rela->r_info);
2818 +
2819 + if (r_symndx < symtab_hdr->sh_info)
2820 + sym_frag = elf_tdata(abfd)->local_sym_frag[r_symndx];
2821 + else
2822 + {
2823 + struct elf_link_hash_entry *h;
2824 +
2825 + h = elf_sym_hashes(abfd)[r_symndx - symtab_hdr->sh_info];
2826 +
2827 + while (h->root.type == bfd_link_hash_indirect
2828 + || h->root.type == bfd_link_hash_warning)
2829 + h = (struct elf_link_hash_entry *)h->root.u.i.link;
2830 +
2831 + BFD_ASSERT(h->root.type == bfd_link_hash_defined
2832 + || h->root.type == bfd_link_hash_defweak);
2833 +
2834 + sym_frag = ((struct elf_avr32_link_hash_entry *)h)->sym_frag;
2835 + }
2836 +
2837 + RDBG(" addend: 0x%08lx -> 0x%08lx\n",
2838 + rela->r_addend,
2839 + rela->r_addend + r_data->add_frag->offset_adjust
2840 + - (sym_frag ? sym_frag->offset_adjust : 0));
2841 +
2842 + /* If this is against a section symbol, we won't find any
2843 + sym_frag, so we'll just adjust the addend. */
2844 + rela->r_addend += r_data->add_frag->offset_adjust;
2845 + if (sym_frag)
2846 + rela->r_addend -= sym_frag->offset_adjust;
2847 +
2848 + if (r_data->sub_frag)
2849 + {
2850 + bfd_byte *contents;
2851 + bfd_signed_vma diff;
2852 +
2853 + contents = retrieve_contents(abfd, sec, info->keep_memory);
2854 + if (!contents)
2855 + goto out;
2856 +
2857 + /* I realize now that sub_frag is misnamed. It's
2858 + actually add_frag which is subtracted in this
2859 + case... */
2860 + diff = bfd_get_signed_32(abfd, contents + rela->r_offset);
2861 + diff += (r_data->sub_frag->offset_adjust
2862 + - r_data->add_frag->offset_adjust);
2863 + bfd_put_32(abfd, diff, contents + rela->r_offset);
2864 +
2865 + RDBG(" 0x%lx: DIFF32 updated: 0x%lx\n", rela->r_offset, diff);
2866 +
2867 + release_contents(sec, contents);
2868 + }
2869 + }
2870 + else
2871 + BFD_ASSERT(!r_data->sub_frag);
2872 + }
2873 +
2874 + ret = TRUE;
2875 +
2876 + out:
2877 + release_internal_relocs(sec, relocs);
2878 + return ret;
2879 +}
2880 +
2881 +static bfd_boolean
2882 +avr32_move_data(bfd *abfd, asection *sec, struct bfd_link_info *info)
2883 +{
2884 + struct elf_avr32_link_hash_table *htab;
2885 + struct avr32_relax_data *rd;
2886 + struct fragment *frag, *fragend;
2887 + Elf_Internal_Rela *relocs = NULL;
2888 + bfd_byte *contents = NULL;
2889 + unsigned int i;
2890 + bfd_boolean ret = FALSE;
2891 +
2892 + htab = avr32_elf_hash_table(info);
2893 + rd = avr32_relax_data(sec);
2894 +
2895 + if (!htab->symbols_adjusted)
2896 + if (!adjust_syms(info))
2897 + return FALSE;
2898 +
2899 + if (rd->is_relaxable)
2900 + {
2901 + /* Resize the section first, so that we can be sure that enough
2902 + memory is allocated in case the section has grown. */
2903 + if (sec->size > sec->rawsize
2904 + && elf_section_data(sec)->this_hdr.contents)
2905 + {
2906 + /* We must not use cached data if the section has grown. */
2907 + free(elf_section_data(sec)->this_hdr.contents);
2908 + elf_section_data(sec)->this_hdr.contents = NULL;
2909 + }
2910 +
2911 + relocs = retrieve_internal_relocs(abfd, sec, info->keep_memory);
2912 + if (!relocs)
2913 + goto out;
2914 + contents = retrieve_contents(abfd, sec, info->keep_memory);
2915 + if (!contents)
2916 + goto out;
2917 +
2918 + fragend = rd->frag + rd->frag_count;
2919 +
2920 + RDBG("move_data: %s<%s>: relocs=%p, contents=%p\n",
2921 + abfd->filename, sec->name, relocs, contents);
2922 +
2923 + /* First, move the data into place. We must take care to move
2924 + frags in the right order so that we don't accidentally
2925 + overwrite parts of the next frag. */
2926 + for (frag = rd->frag; frag < fragend; frag++)
2927 + {
2928 + RDBG(" 0x%08lx%c0x%x: size 0x%lx%c0x%x\n",
2929 + frag->offset, frag->offset_adjust >= 0 ? '+' : '-',
2930 + abs(frag->offset_adjust),
2931 + frag->size, frag->size_adjust >= 0 ? '+' : '-',
2932 + abs(frag->size_adjust));
2933 + if (frag->offset_adjust > 0)
2934 + {
2935 + struct fragment *prev = frag - 1;
2936 + struct fragment *last;
2937 +
2938 + for (last = frag; last < fragend && last->offset_adjust > 0;
2939 + last++) ;
2940 +
2941 + if (last == fragend)
2942 + last--;
2943 +
2944 + for (frag = last; frag != prev; frag--)
2945 + {
2946 + if (frag->offset_adjust
2947 + && frag->size + frag->size_adjust > 0)
2948 + {
2949 + RDBG("memmove 0x%lx -> 0x%lx (size %lu)\n",
2950 + frag->offset, frag->offset + frag->offset_adjust,
2951 + frag->size + frag->size_adjust);
2952 + memmove(contents + frag->offset + frag->offset_adjust,
2953 + contents + frag->offset,
2954 + frag->size + frag->size_adjust);
2955 + }
2956 + }
2957 + frag = last;
2958 + }
2959 + else if (frag->offset_adjust && frag->size + frag->size_adjust > 0)
2960 + {
2961 + RDBG("memmove 0x%lx -> 0x%lx (size %lu)\n",
2962 + frag->offset, frag->offset + frag->offset_adjust,
2963 + frag->size + frag->size_adjust);
2964 + memmove(contents + frag->offset + frag->offset_adjust,
2965 + contents + frag->offset,
2966 + frag->size + frag->size_adjust);
2967 + }
2968 + }
2969 +
2970 + i = 0;
2971 +
2972 + for (frag = rd->frag; frag < fragend; frag++)
2973 + {
2974 + const struct relax_state *state, *istate;
2975 + struct avr32_reloc_data *r_data = NULL;
2976 +
2977 + istate = &relax_state[frag->initial_state];
2978 + state = &relax_state[frag->state];
2979 +
2980 + if (rd->reloc_data)
2981 + r_data = &rd->reloc_data[frag->rela - relocs];
2982 +
2983 + BFD_ASSERT((long)(frag->size + frag->size_adjust) >= 0);
2984 + BFD_ASSERT(state->reftype != REF_CPOOL
2985 + || r_data->add_frag->refcount > 0);
2986 +
2987 + if (istate->reftype == REF_CPOOL && state->reftype != REF_CPOOL)
2988 + {
2989 + struct fragment *ifrag;
2990 +
2991 + /* An indirect reference through the cpool has been
2992 + converted to a direct reference. We must update the
2993 + reloc to point to the symbol itself instead of the
2994 + constant pool entry. The reloc type will be updated
2995 + later. */
2996 + ifrag = r_data->add_frag;
2997 + frag->rela->r_info = ifrag->rela->r_info;
2998 + frag->rela->r_addend = ifrag->rela->r_addend;
2999 +
3000 + /* Copy the reloc data so the addend will be adjusted
3001 + correctly later. */
3002 + *r_data = rd->reloc_data[ifrag->rela - relocs];
3003 + }
3004 +
3005 + /* Move all relocs covered by this frag. */
3006 + if (frag->rela)
3007 + BFD_ASSERT(&relocs[i] <= frag->rela);
3008 + else
3009 + BFD_ASSERT((frag + 1) == fragend && frag->state == RS_NONE);
3010 +
3011 + if (frag == rd->frag)
3012 + BFD_ASSERT(i == 0);
3013 + else
3014 + BFD_ASSERT(&relocs[i] > frag[-1].rela);
3015 +
3016 + /* If non-null, frag->rela is the last relocation in the
3017 + fragment. frag->rela can only be null in the last
3018 + fragment, so in that case, we'll just do the rest. */
3019 + for (; (i < sec->reloc_count
3020 + && (!frag->rela || &relocs[i] <= frag->rela)); i++)
3021 + {
3022 + RDBG("[%4u] r_offset 0x%08lx -> 0x%08lx\n", i, relocs[i].r_offset,
3023 + relocs[i].r_offset + frag->offset_adjust);
3024 + relocs[i].r_offset += frag->offset_adjust;
3025 + }
3026 +
3027 + if (frag->refcount == 0)
3028 + {
3029 + /* If this frag is to be discarded, make sure we won't
3030 + relocate it later on. */
3031 + BFD_ASSERT(frag->state == RS_CPENT);
3032 + frag->rela->r_info = ELF_R_INFO(ELF_R_SYM(frag->rela->r_info),
3033 + R_AVR32_NONE);
3034 + }
3035 + else if (frag->state == RS_ALIGN)
3036 + {
3037 + bfd_vma addr, addr_end;
3038 +
3039 + addr = frag->rela->r_offset;
3040 + addr_end = (frag->offset + frag->offset_adjust
3041 + + frag->size + frag->size_adjust);
3042 +
3043 + /* If the section is executable, insert NOPs.
3044 + Otherwise, insert zeroes. */
3045 + if (sec->flags & SEC_CODE)
3046 + {
3047 + if (addr & 1)
3048 + {
3049 + bfd_put_8(abfd, 0, contents + addr);
3050 + addr++;
3051 + }
3052 +
3053 + BFD_ASSERT(!((addr_end - addr) & 1));
3054 +
3055 + while (addr < addr_end)
3056 + {
3057 + bfd_put_16(abfd, NOP_OPCODE, contents + addr);
3058 + addr += 2;
3059 + }
3060 + }
3061 + else
3062 + memset(contents + addr, 0, addr_end - addr);
3063 + }
3064 + else if (state->opcode_mask)
3065 + {
3066 + bfd_vma insn;
3067 +
3068 + /* Update the opcode and the relocation type unless it's a
3069 + "special" relax state (i.e. RS_NONE, RS_ALIGN or
3070 + RS_CPENT.), in which case the opcode mask is zero. */
3071 + insn = bfd_get_32(abfd, contents + frag->rela->r_offset);
3072 + insn &= ~state->opcode_mask;
3073 + insn |= state->opcode;
3074 + RDBG(" 0x%lx: inserting insn %08lx\n",
3075 + frag->rela->r_offset, insn);
3076 + bfd_put_32(abfd, insn, contents + frag->rela->r_offset);
3077 +
3078 + frag->rela->r_info = ELF_R_INFO(ELF_R_SYM(frag->rela->r_info),
3079 + state->r_type);
3080 + }
3081 +
3082 + if ((frag + 1) == fragend)
3083 + BFD_ASSERT((frag->offset + frag->size + frag->offset_adjust
3084 + + frag->size_adjust) == sec->size);
3085 + else
3086 + BFD_ASSERT((frag->offset + frag->size + frag->offset_adjust
3087 + + frag->size_adjust)
3088 + == (frag[1].offset + frag[1].offset_adjust));
3089 + }
3090 + }
3091 +
3092 + /* Adjust reloc addends and DIFF32 differences */
3093 + if (!adjust_relocs(abfd, sec, info))
3094 + return FALSE;
3095 +
3096 + ret = TRUE;
3097 +
3098 + out:
3099 + release_contents(sec, contents);
3100 + release_internal_relocs(sec, relocs);
3101 + return ret;
3102 +}
3103 +
3104 +static bfd_boolean
3105 +avr32_elf_relax_section(bfd *abfd, asection *sec,
3106 + struct bfd_link_info *info, bfd_boolean *again)
3107 +{
3108 + struct elf_avr32_link_hash_table *htab;
3109 + struct avr32_relax_data *rd;
3110 +
3111 + *again = FALSE;
3112 + if (info->relocatable)
3113 + return TRUE;
3114 +
3115 + htab = avr32_elf_hash_table(info);
3116 + if ((!(sec->flags & SEC_RELOC) || sec->reloc_count == 0)
3117 + && sec != htab->sgot)
3118 + return TRUE;
3119 +
3120 + if (!htab->relocations_analyzed)
3121 + {
3122 + if (!analyze_relocations(info))
3123 + return FALSE;
3124 + htab->relocations_analyzed = TRUE;
3125 + }
3126 +
3127 + rd = avr32_relax_data(sec);
3128 +
3129 + if (rd->iteration != htab->relax_iteration)
3130 + {
3131 + if (!htab->repeat_pass)
3132 + htab->relax_pass++;
3133 + htab->relax_iteration++;
3134 + htab->repeat_pass = FALSE;
3135 + }
3136 +
3137 + rd->iteration++;
3138 +
3139 + switch (htab->relax_pass)
3140 + {
3141 + case RELAX_PASS_SIZE_FRAGS:
3142 + if (!avr32_size_frags(abfd, sec, info))
3143 + return FALSE;
3144 + *again = TRUE;
3145 + break;
3146 + case RELAX_PASS_MOVE_DATA:
3147 + if (!avr32_move_data(abfd, sec, info))
3148 + return FALSE;
3149 + break;
3150 + }
3151 +
3152 + return TRUE;
3153 +}
3154 +
3155 +
3156 +/* Relocation */
3157 +
3158 +static bfd_reloc_status_type
3159 +avr32_check_reloc_value(asection *sec, Elf_Internal_Rela *rela,
3160 + bfd_signed_vma relocation, reloc_howto_type *howto);
3161 +static bfd_reloc_status_type
3162 +avr32_final_link_relocate(reloc_howto_type *howto, bfd *input_bfd,
3163 + asection *input_section, bfd_byte *contents,
3164 + Elf_Internal_Rela *rel, bfd_vma value);
3165 +static bfd_boolean
3166 +avr32_elf_relocate_section(bfd *output_bfd, struct bfd_link_info *info,
3167 + bfd *input_bfd, asection *input_section,
3168 + bfd_byte *contents, Elf_Internal_Rela *relocs,
3169 + Elf_Internal_Sym *local_syms,
3170 + asection **local_sections);
3171 +
3172 +
3173 +#define symbol_address(symbol) \
3174 + symbol->value + symbol->section->output_section->vma \
3175 + + symbol->section->output_offset
3176 +
3177 +#define avr32_elf_insert_field(size, field, abfd, reloc_entry, data) \
3178 + do \
3179 + { \
3180 + unsigned long x; \
3181 + x = bfd_get_##size (abfd, data + reloc_entry->address); \
3182 + x &= ~reloc_entry->howto->dst_mask; \
3183 + x |= field & reloc_entry->howto->dst_mask; \
3184 + bfd_put_##size (abfd, (bfd_vma) x, data + reloc_entry->address); \
3185 + } \
3186 + while(0)
3187 +
3188 +static bfd_reloc_status_type
3189 +avr32_check_reloc_value(asection *sec ATTRIBUTE_UNUSED,
3190 + Elf_Internal_Rela *rela ATTRIBUTE_UNUSED,
3191 + bfd_signed_vma relocation,
3192 + reloc_howto_type *howto)
3193 +{
3194 + bfd_vma reloc_u;
3195 +
3196 + /* We take "complain_overflow_dont" to mean "don't complain on
3197 + alignment either". This way, we don't have to special-case
3198 + R_AVR32_HI16 */
3199 + if (howto->complain_on_overflow == complain_overflow_dont)
3200 + return bfd_reloc_ok;
3201 +
3202 + /* Check if the value is correctly aligned */
3203 + if (relocation & ((1 << howto->rightshift) - 1))
3204 + {
3205 + RDBG("misaligned: %s<%s+%lx>: %s: 0x%lx (align %u)\n",
3206 + sec->owner->filename, sec->name, rela->r_offset,
3207 + howto->name, relocation, howto->rightshift);
3208 + return bfd_reloc_overflow;
3209 + }
3210 +
3211 + /* Now, get rid of the unnecessary bits */
3212 + relocation >>= howto->rightshift;
3213 + reloc_u = (bfd_vma)relocation;
3214 +
3215 + switch (howto->complain_on_overflow)
3216 + {
3217 + case complain_overflow_unsigned:
3218 + case complain_overflow_bitfield:
3219 + if (reloc_u > (unsigned long)((1 << howto->bitsize) - 1))
3220 + {
3221 + RDBG("unsigned overflow: %s<%s+%lx>: %s: 0x%lx (size %u)\n",
3222 + sec->owner->filename, sec->name, rela->r_offset,
3223 + howto->name, reloc_u, howto->bitsize);
3224 + RDBG("reloc vma: 0x%lx\n",
3225 + sec->output_section->vma + sec->output_offset + rela->r_offset);
3226 +
3227 + return bfd_reloc_overflow;
3228 + }
3229 + break;
3230 + case complain_overflow_signed:
3231 + if (relocation > (1 << (howto->bitsize - 1)) - 1)
3232 + {
3233 + RDBG("signed overflow: %s<%s+%lx>: %s: 0x%lx (size %u)\n",
3234 + sec->owner->filename, sec->name, rela->r_offset,
3235 + howto->name, reloc_u, howto->bitsize);
3236 + RDBG("reloc vma: 0x%lx\n",
3237 + sec->output_section->vma + sec->output_offset + rela->r_offset);
3238 +
3239 + return bfd_reloc_overflow;
3240 + }
3241 + if (relocation < -(1 << (howto->bitsize - 1)))
3242 + {
3243 + RDBG("signed overflow: %s<%s+%lx>: %s: -0x%lx (size %u)\n",
3244 + sec->owner->filename, sec->name, rela->r_offset,
3245 + howto->name, -relocation, howto->bitsize);
3246 + RDBG("reloc vma: 0x%lx\n",
3247 + sec->output_section->vma + sec->output_offset + rela->r_offset);
3248 +
3249 + return bfd_reloc_overflow;
3250 + }
3251 + break;
3252 + default:
3253 + abort();
3254 + }
3255 +
3256 + return bfd_reloc_ok;
3257 +}
3258 +
3259 +
3260 +static bfd_reloc_status_type
3261 +avr32_final_link_relocate(reloc_howto_type *howto,
3262 + bfd *input_bfd,
3263 + asection *input_section,
3264 + bfd_byte *contents,
3265 + Elf_Internal_Rela *rel,
3266 + bfd_vma value)
3267 +{
3268 + bfd_vma field;
3269 + bfd_vma relocation;
3270 + bfd_reloc_status_type status;
3271 + bfd_byte *p = contents + rel->r_offset;
3272 + unsigned long x;
3273 +
3274 + pr_debug(" (6b) final link relocate\n");
3275 +
3276 + /* Sanity check the address */
3277 + if (rel->r_offset > input_section->size)
3278 + {
3279 + (*_bfd_error_handler)
3280 + ("%B: %A+0x%lx: offset out of range (section size: 0x%lx)",
3281 + input_bfd, input_section, rel->r_offset, input_section->size);
3282 + return bfd_reloc_outofrange;
3283 + }
3284 +
3285 + relocation = value + rel->r_addend;
3286 +
3287 + if (howto->pc_relative)
3288 + {
3289 + bfd_vma addr;
3290 +
3291 + addr = input_section->output_section->vma
3292 + + input_section->output_offset + rel->r_offset;
3293 + addr &= ~0UL << howto->rightshift;
3294 + relocation -= addr;
3295 + }
3296 +
3297 + switch (ELF32_R_TYPE(rel->r_info))
3298 + {
3299 + case R_AVR32_16N_PCREL:
3300 + /* sub reg, pc, . - (sym + addend) */
3301 + relocation = -relocation;
3302 + break;
3303 + }
3304 +
3305 + status = avr32_check_reloc_value(input_section, rel, relocation, howto);
3306 +
3307 + relocation >>= howto->rightshift;
3308 + if (howto->bitsize == 21)
3309 + field = (relocation & 0xffff)
3310 + | ((relocation & 0x10000) << 4)
3311 + | ((relocation & 0x1e0000) << 8);
3312 + else if (howto->bitsize == 12)
3313 + field = (relocation & 0xff) | ((relocation & 0xf00) << 4);
3314 + else if (howto->bitsize == 10)
3315 + field = ((relocation & 0xff) << 4)
3316 + | ((relocation & 0x300) >> 8);
3317 + else
3318 + field = relocation << howto->bitpos;
3319 +
3320 + switch (howto->size)
3321 + {
3322 + case 0:
3323 + x = bfd_get_8 (input_bfd, p);
3324 + x &= ~howto->dst_mask;
3325 + x |= field & howto->dst_mask;
3326 + bfd_put_8 (input_bfd, (bfd_vma) x, p);
3327 + break;
3328 + case 1:
3329 + x = bfd_get_16 (input_bfd, p);
3330 + x &= ~howto->dst_mask;
3331 + x |= field & howto->dst_mask;
3332 + bfd_put_16 (input_bfd, (bfd_vma) x, p);
3333 + break;
3334 + case 2:
3335 + x = bfd_get_32 (input_bfd, p);
3336 + x &= ~howto->dst_mask;
3337 + x |= field & howto->dst_mask;
3338 + bfd_put_32 (input_bfd, (bfd_vma) x, p);
3339 + break;
3340 + default:
3341 + abort();
3342 + }
3343 +
3344 + return status;
3345 +}
3346 +
3347 +/* (6) Apply relocations to the normal (non-dynamic) sections */
3348 +
3349 +static bfd_boolean
3350 +avr32_elf_relocate_section(bfd *output_bfd, struct bfd_link_info *info,
3351 + bfd *input_bfd, asection *input_section,
3352 + bfd_byte *contents, Elf_Internal_Rela *relocs,
3353 + Elf_Internal_Sym *local_syms,
3354 + asection **local_sections)
3355 +{
3356 + struct elf_avr32_link_hash_table *htab;
3357 + Elf_Internal_Shdr *symtab_hdr;
3358 + Elf_Internal_Rela *rel, *relend;
3359 + struct elf_link_hash_entry **sym_hashes;
3360 + struct got_entry **local_got_ents;
3361 + asection *sgot;
3362 + asection *srelgot;
3363 +
3364 + pr_debug("(6) relocate section %s:<%s> (size 0x%lx)\n",
3365 + input_bfd->filename, input_section->name, input_section->size);
3366 +
3367 + /* If we're doing a partial link, we don't have to do anything since
3368 + we're using RELA relocations */
3369 + if (info->relocatable)
3370 + return TRUE;
3371 +
3372 + htab = avr32_elf_hash_table(info);
3373 + symtab_hdr = &elf_tdata(input_bfd)->symtab_hdr;
3374 + sym_hashes = elf_sym_hashes(input_bfd);
3375 + local_got_ents = elf_local_got_ents(input_bfd);
3376 + sgot = htab->sgot;
3377 + srelgot = htab->srelgot;
3378 +
3379 + relend = relocs + input_section->reloc_count;
3380 + for (rel = relocs; rel < relend; rel++)
3381 + {
3382 + unsigned long r_type, r_symndx;
3383 + reloc_howto_type *howto;
3384 + Elf_Internal_Sym *sym = NULL;
3385 + struct elf_link_hash_entry *h = NULL;
3386 + asection *sec = NULL;
3387 + bfd_vma value;
3388 + bfd_vma offset;
3389 + bfd_reloc_status_type status;
3390 +
3391 + r_type = ELF32_R_TYPE(rel->r_info);
3392 + r_symndx = ELF32_R_SYM(rel->r_info);
3393 +
3394 + if (r_type == R_AVR32_NONE
3395 + || r_type == R_AVR32_ALIGN
3396 + || r_type == R_AVR32_DIFF32
3397 + || r_type == R_AVR32_DIFF16
3398 + || r_type == R_AVR32_DIFF8)
3399 + continue;
3400 +
3401 + /* Sanity check */
3402 + if (r_type > R_AVR32_max)
3403 + {
3404 + bfd_set_error(bfd_error_bad_value);
3405 + return FALSE;
3406 + }
3407 +
3408 + howto = &elf_avr32_howto_table[r_type];
3409 +
3410 + if (r_symndx < symtab_hdr->sh_info)
3411 + {
3412 + sym = local_syms + r_symndx;
3413 + sec = local_sections[r_symndx];
3414 +
3415 + pr_debug(" (6a) processing %s against local symbol %lu\n",
3416 + howto->name, r_symndx);
3417 +
3418 + /* The following function changes rel->r_addend behind our back. */
3419 + value = _bfd_elf_rela_local_sym(output_bfd, sym, &sec, rel);
3420 + pr_debug(" => value: %lx, addend: %lx\n", value, rel->r_addend);
3421 + }
3422 + else
3423 + {
3424 + if (sym_hashes == NULL)
3425 + return FALSE;
3426 +
3427 + h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3428 + while (h->root.type == bfd_link_hash_indirect
3429 + || h->root.type == bfd_link_hash_warning)
3430 + h = (struct elf_link_hash_entry *)h->root.u.i.link;
3431 +
3432 + pr_debug(" (6a) processing %s against symbol %s\n",
3433 + howto->name, h->root.root.string);
3434 +
3435 + if (h->root.type == bfd_link_hash_defined
3436 + || h->root.type == bfd_link_hash_defweak)
3437 + {
3438 + bfd_boolean dyn;
3439 +
3440 + dyn = htab->root.dynamic_sections_created;
3441 + sec = h->root.u.def.section;
3442 +
3443 + if (sec->output_section)
3444 + value = (h->root.u.def.value
3445 + + sec->output_section->vma
3446 + + sec->output_offset);
3447 + else
3448 + value = h->root.u.def.value;
3449 + }
3450 + else if (h->root.type == bfd_link_hash_undefweak)
3451 + value = 0;
3452 + else if (info->unresolved_syms_in_objects == RM_IGNORE
3453 + && ELF_ST_VISIBILITY(h->other) == STV_DEFAULT)
3454 + value = 0;
3455 + else
3456 + {
3457 + bfd_boolean err;
3458 + err = (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
3459 + || ELF_ST_VISIBILITY(h->other) != STV_DEFAULT);
3460 + if (!info->callbacks->undefined_symbol
3461 + (info, h->root.root.string, input_bfd,
3462 + input_section, rel->r_offset, err))
3463 + return FALSE;
3464 + value = 0;
3465 + }
3466 +
3467 + pr_debug(" => value: %lx, addend: %lx\n", value, rel->r_addend);
3468 + }
3469 +
3470 + switch (r_type)
3471 + {
3472 + case R_AVR32_GOT32:
3473 + case R_AVR32_GOT16:
3474 + case R_AVR32_GOT8:
3475 + case R_AVR32_GOT21S:
3476 + case R_AVR32_GOT18SW:
3477 + case R_AVR32_GOT16S:
3478 + case R_AVR32_GOT7UW:
3479 + case R_AVR32_LDA_GOT:
3480 + case R_AVR32_GOTCALL:
3481 + BFD_ASSERT(sgot != NULL);
3482 +
3483 + if (h != NULL)
3484 + {
3485 + BFD_ASSERT(h->got.glist->refcount > 0);
3486 + offset = h->got.glist->offset;
3487 +
3488 + BFD_ASSERT(offset < sgot->size);
3489 + if (!elf_hash_table(info)->dynamic_sections_created
3490 + || (h->def_regular
3491 + && (!info->shared
3492 + || info->symbolic
3493 + || h->dynindx == -1)))
3494 + {
3495 + /* This is actually a static link, or it is a
3496 + -Bsymbolic link and the symbol is defined
3497 + locally, or the symbol was forced to be local. */
3498 + bfd_put_32(output_bfd, value, sgot->contents + offset);
3499 + }
3500 + }
3501 + else
3502 + {
3503 + BFD_ASSERT(local_got_ents &&
3504 + local_got_ents[r_symndx]->refcount > 0);
3505 + offset = local_got_ents[r_symndx]->offset;
3506 +
3507 + /* Local GOT entries don't have relocs. If this is a
3508 + shared library, the dynamic linker will add the load
3509 + address to the initial value at startup. */
3510 + BFD_ASSERT(offset < sgot->size);
3511 + pr_debug("Initializing GOT entry at offset %lu: 0x%lx\n",
3512 + offset, value);
3513 + bfd_put_32 (output_bfd, value, sgot->contents + offset);
3514 + }
3515 +
3516 + value = sgot->output_offset + offset;
3517 + pr_debug("GOT reference: New value %lx\n", value);
3518 + break;
3519 +
3520 + case R_AVR32_GOTPC:
3521 + /* This relocation type is for constant pool entries used in
3522 + the calculation "Rd = PC - (PC - GOT)", where the
3523 + constant pool supplies the constant (PC - GOT)
3524 + offset. The symbol value + addend indicates where the
3525 + value of PC is taken. */
3526 + value -= sgot->output_section->vma;
3527 + break;
3528 +
3529 + case R_AVR32_32_PCREL:
3530 + /* We must adjust r_offset to account for discarded data in
3531 + the .eh_frame section. This is probably not the right
3532 + way to do this, since AFAICS all other architectures do
3533 + it some other way. I just can't figure out how... */
3534 + {
3535 + bfd_vma r_offset;
3536 +
3537 + r_offset = _bfd_elf_section_offset(output_bfd, info,
3538 + input_section,
3539 + rel->r_offset);
3540 + if (r_offset == (bfd_vma)-1
3541 + || r_offset == (bfd_vma)-2)
3542 + continue;
3543 + rel->r_offset = r_offset;
3544 + }
3545 + break;
3546 +
3547 + case R_AVR32_32:
3548 + /* We need to emit a run-time relocation in the following cases:
3549 + - we're creating a shared library
3550 + - the symbol is not defined in any regular objects
3551 +
3552 + Of course, sections that aren't going to be part of the
3553 + run-time image will not get any relocs, and undefined
3554 + symbols won't have any either (only weak undefined
3555 + symbols should get this far). */
3556 + if ((info->shared
3557 + || (elf_hash_table(info)->dynamic_sections_created
3558 + && h != NULL
3559 + && h->def_dynamic
3560 + && !h->def_regular))
3561 + && r_symndx != 0
3562 + && (input_section->flags & SEC_ALLOC))
3563 + {
3564 + Elf_Internal_Rela outrel;
3565 + bfd_byte *loc;
3566 + bfd_boolean skip, relocate;
3567 + struct elf_avr32_link_hash_entry *avrh;
3568 +
3569 + pr_debug("Going to generate dynamic reloc...\n");
3570 +
3571 + skip = FALSE;
3572 + relocate = FALSE;
3573 +
3574 + outrel.r_offset = _bfd_elf_section_offset(output_bfd, info,
3575 + input_section,
3576 + rel->r_offset);
3577 + if (outrel.r_offset == (bfd_vma)-1)
3578 + skip = TRUE;
3579 + else if (outrel.r_offset == (bfd_vma)-2)
3580 + skip = TRUE, relocate = TRUE;
3581 +
3582 + outrel.r_offset += (input_section->output_section->vma
3583 + + input_section->output_offset);
3584 +
3585 + pr_debug(" ... offset %lx, dynindx %ld\n",
3586 + outrel.r_offset, h ? h->dynindx : -1);
3587 +
3588 + if (skip)
3589 + memset(&outrel, 0, sizeof(outrel));
3590 + else
3591 + {
3592 + avrh = (struct elf_avr32_link_hash_entry *)h;
3593 + /* h->dynindx may be -1 if this symbol was marked to
3594 + become local. */
3595 + if (h == NULL
3596 + || ((info->symbolic || h->dynindx == -1)
3597 + && h->def_regular))
3598 + {
3599 + relocate = TRUE;
3600 + outrel.r_info = ELF32_R_INFO(0, R_AVR32_RELATIVE);
3601 + outrel.r_addend = value + rel->r_addend;
3602 + pr_debug(" ... R_AVR32_RELATIVE\n");
3603 + }
3604 + else
3605 + {
3606 + BFD_ASSERT(h->dynindx != -1);
3607 + relocate = TRUE;
3608 + outrel.r_info = ELF32_R_INFO(h->dynindx, R_AVR32_GLOB_DAT);
3609 + outrel.r_addend = rel->r_addend;
3610 + pr_debug(" ... R_AVR32_GLOB_DAT\n");
3611 + }
3612 + }
3613 +
3614 + pr_debug("srelgot reloc_count: %d, size %lu\n",
3615 + srelgot->reloc_count, srelgot->size);
3616 +
3617 + loc = srelgot->contents;
3618 + loc += srelgot->reloc_count++ * sizeof(Elf32_External_Rela);
3619 + bfd_elf32_swap_reloca_out(output_bfd, &outrel, loc);
3620 +
3621 + BFD_ASSERT(srelgot->reloc_count * sizeof(Elf32_External_Rela)
3622 + <= srelgot->size);
3623 +
3624 + if (!relocate)
3625 + continue;
3626 + }
3627 + break;
3628 + }
3629 +
3630 + status = avr32_final_link_relocate(howto, input_bfd, input_section,