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