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