Commit | Line | Data |
---|---|---|
e9f53129 AM |
1 | /* SPU specific support for 32-bit ELF |
2 | ||
cd4a7468 | 3 | Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
e9f53129 AM |
4 | |
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
e9f53129 AM |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License along | |
18 | with this program; if not, write to the Free Software Foundation, Inc., | |
19 | 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ | |
20 | ||
e9f53129 | 21 | #include "sysdep.h" |
9dcc4794 | 22 | #include "libiberty.h" |
3db64b00 | 23 | #include "bfd.h" |
e9f53129 AM |
24 | #include "bfdlink.h" |
25 | #include "libbfd.h" | |
26 | #include "elf-bfd.h" | |
27 | #include "elf/spu.h" | |
28 | #include "elf32-spu.h" | |
29 | ||
30 | /* We use RELA style relocs. Don't define USE_REL. */ | |
31 | ||
32 | static bfd_reloc_status_type spu_elf_rel9 (bfd *, arelent *, asymbol *, | |
33 | void *, asection *, | |
34 | bfd *, char **); | |
35 | ||
36 | /* Values of type 'enum elf_spu_reloc_type' are used to index this | |
37 | array, so it must be declared in the order of that type. */ | |
38 | ||
39 | static reloc_howto_type elf_howto_table[] = { | |
40 | HOWTO (R_SPU_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont, | |
41 | bfd_elf_generic_reloc, "SPU_NONE", | |
42 | FALSE, 0, 0x00000000, FALSE), | |
43 | HOWTO (R_SPU_ADDR10, 4, 2, 10, FALSE, 14, complain_overflow_bitfield, | |
44 | bfd_elf_generic_reloc, "SPU_ADDR10", | |
45 | FALSE, 0, 0x00ffc000, FALSE), | |
46 | HOWTO (R_SPU_ADDR16, 2, 2, 16, FALSE, 7, complain_overflow_bitfield, | |
47 | bfd_elf_generic_reloc, "SPU_ADDR16", | |
48 | FALSE, 0, 0x007fff80, FALSE), | |
49 | HOWTO (R_SPU_ADDR16_HI, 16, 2, 16, FALSE, 7, complain_overflow_bitfield, | |
50 | bfd_elf_generic_reloc, "SPU_ADDR16_HI", | |
51 | FALSE, 0, 0x007fff80, FALSE), | |
52 | HOWTO (R_SPU_ADDR16_LO, 0, 2, 16, FALSE, 7, complain_overflow_dont, | |
53 | bfd_elf_generic_reloc, "SPU_ADDR16_LO", | |
54 | FALSE, 0, 0x007fff80, FALSE), | |
55 | HOWTO (R_SPU_ADDR18, 0, 2, 18, FALSE, 7, complain_overflow_bitfield, | |
56 | bfd_elf_generic_reloc, "SPU_ADDR18", | |
57 | FALSE, 0, 0x01ffff80, FALSE), | |
b427ea91 | 58 | HOWTO (R_SPU_ADDR32, 0, 2, 32, FALSE, 0, complain_overflow_dont, |
e9f53129 AM |
59 | bfd_elf_generic_reloc, "SPU_ADDR32", |
60 | FALSE, 0, 0xffffffff, FALSE), | |
61 | HOWTO (R_SPU_REL16, 2, 2, 16, TRUE, 7, complain_overflow_bitfield, | |
62 | bfd_elf_generic_reloc, "SPU_REL16", | |
63 | FALSE, 0, 0x007fff80, TRUE), | |
64 | HOWTO (R_SPU_ADDR7, 0, 2, 7, FALSE, 14, complain_overflow_dont, | |
65 | bfd_elf_generic_reloc, "SPU_ADDR7", | |
66 | FALSE, 0, 0x001fc000, FALSE), | |
67 | HOWTO (R_SPU_REL9, 2, 2, 9, TRUE, 0, complain_overflow_signed, | |
68 | spu_elf_rel9, "SPU_REL9", | |
69 | FALSE, 0, 0x0180007f, TRUE), | |
70 | HOWTO (R_SPU_REL9I, 2, 2, 9, TRUE, 0, complain_overflow_signed, | |
71 | spu_elf_rel9, "SPU_REL9I", | |
72 | FALSE, 0, 0x0000c07f, TRUE), | |
73 | HOWTO (R_SPU_ADDR10I, 0, 2, 10, FALSE, 14, complain_overflow_signed, | |
74 | bfd_elf_generic_reloc, "SPU_ADDR10I", | |
75 | FALSE, 0, 0x00ffc000, FALSE), | |
76 | HOWTO (R_SPU_ADDR16I, 0, 2, 16, FALSE, 7, complain_overflow_signed, | |
77 | bfd_elf_generic_reloc, "SPU_ADDR16I", | |
78 | FALSE, 0, 0x007fff80, FALSE), | |
b427ea91 | 79 | HOWTO (R_SPU_REL32, 0, 2, 32, TRUE, 0, complain_overflow_dont, |
e9f53129 AM |
80 | bfd_elf_generic_reloc, "SPU_REL32", |
81 | FALSE, 0, 0xffffffff, TRUE), | |
4f4416b5 AM |
82 | HOWTO (R_SPU_ADDR16X, 0, 2, 16, FALSE, 7, complain_overflow_bitfield, |
83 | bfd_elf_generic_reloc, "SPU_ADDR16X", | |
84 | FALSE, 0, 0x007fff80, FALSE), | |
b427ea91 | 85 | HOWTO (R_SPU_PPU32, 0, 2, 32, FALSE, 0, complain_overflow_dont, |
ece5ef60 AM |
86 | bfd_elf_generic_reloc, "SPU_PPU32", |
87 | FALSE, 0, 0xffffffff, FALSE), | |
b427ea91 | 88 | HOWTO (R_SPU_PPU64, 0, 4, 64, FALSE, 0, complain_overflow_dont, |
ece5ef60 AM |
89 | bfd_elf_generic_reloc, "SPU_PPU64", |
90 | FALSE, 0, -1, FALSE), | |
e9f53129 AM |
91 | }; |
92 | ||
93 | static struct bfd_elf_special_section const spu_elf_special_sections[] = { | |
8374f9d4 | 94 | { "._ea", 4, 0, SHT_PROGBITS, SHF_WRITE }, |
e9f53129 AM |
95 | { ".toe", 4, 0, SHT_NOBITS, SHF_ALLOC }, |
96 | { NULL, 0, 0, 0, 0 } | |
97 | }; | |
98 | ||
99 | static enum elf_spu_reloc_type | |
100 | spu_elf_bfd_to_reloc_type (bfd_reloc_code_real_type code) | |
101 | { | |
102 | switch (code) | |
103 | { | |
104 | default: | |
105 | return R_SPU_NONE; | |
106 | case BFD_RELOC_SPU_IMM10W: | |
107 | return R_SPU_ADDR10; | |
108 | case BFD_RELOC_SPU_IMM16W: | |
109 | return R_SPU_ADDR16; | |
110 | case BFD_RELOC_SPU_LO16: | |
111 | return R_SPU_ADDR16_LO; | |
112 | case BFD_RELOC_SPU_HI16: | |
113 | return R_SPU_ADDR16_HI; | |
114 | case BFD_RELOC_SPU_IMM18: | |
115 | return R_SPU_ADDR18; | |
116 | case BFD_RELOC_SPU_PCREL16: | |
117 | return R_SPU_REL16; | |
118 | case BFD_RELOC_SPU_IMM7: | |
119 | return R_SPU_ADDR7; | |
120 | case BFD_RELOC_SPU_IMM8: | |
121 | return R_SPU_NONE; | |
122 | case BFD_RELOC_SPU_PCREL9a: | |
123 | return R_SPU_REL9; | |
124 | case BFD_RELOC_SPU_PCREL9b: | |
125 | return R_SPU_REL9I; | |
126 | case BFD_RELOC_SPU_IMM10: | |
127 | return R_SPU_ADDR10I; | |
128 | case BFD_RELOC_SPU_IMM16: | |
129 | return R_SPU_ADDR16I; | |
130 | case BFD_RELOC_32: | |
131 | return R_SPU_ADDR32; | |
132 | case BFD_RELOC_32_PCREL: | |
133 | return R_SPU_REL32; | |
ece5ef60 AM |
134 | case BFD_RELOC_SPU_PPU32: |
135 | return R_SPU_PPU32; | |
136 | case BFD_RELOC_SPU_PPU64: | |
137 | return R_SPU_PPU64; | |
e9f53129 AM |
138 | } |
139 | } | |
140 | ||
141 | static void | |
142 | spu_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, | |
143 | arelent *cache_ptr, | |
144 | Elf_Internal_Rela *dst) | |
145 | { | |
146 | enum elf_spu_reloc_type r_type; | |
147 | ||
148 | r_type = (enum elf_spu_reloc_type) ELF32_R_TYPE (dst->r_info); | |
149 | BFD_ASSERT (r_type < R_SPU_max); | |
150 | cache_ptr->howto = &elf_howto_table[(int) r_type]; | |
151 | } | |
152 | ||
153 | static reloc_howto_type * | |
154 | spu_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
155 | bfd_reloc_code_real_type code) | |
156 | { | |
b16f296e AM |
157 | enum elf_spu_reloc_type r_type = spu_elf_bfd_to_reloc_type (code); |
158 | ||
159 | if (r_type == R_SPU_NONE) | |
160 | return NULL; | |
161 | ||
162 | return elf_howto_table + r_type; | |
e9f53129 AM |
163 | } |
164 | ||
157090f7 AM |
165 | static reloc_howto_type * |
166 | spu_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
167 | const char *r_name) | |
168 | { | |
169 | unsigned int i; | |
170 | ||
171 | for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++) | |
172 | if (elf_howto_table[i].name != NULL | |
173 | && strcasecmp (elf_howto_table[i].name, r_name) == 0) | |
174 | return &elf_howto_table[i]; | |
175 | ||
176 | return NULL; | |
177 | } | |
178 | ||
e9f53129 AM |
179 | /* Apply R_SPU_REL9 and R_SPU_REL9I relocs. */ |
180 | ||
181 | static bfd_reloc_status_type | |
182 | spu_elf_rel9 (bfd *abfd, arelent *reloc_entry, asymbol *symbol, | |
183 | void *data, asection *input_section, | |
184 | bfd *output_bfd, char **error_message) | |
185 | { | |
186 | bfd_size_type octets; | |
187 | bfd_vma val; | |
188 | long insn; | |
189 | ||
190 | /* If this is a relocatable link (output_bfd test tells us), just | |
191 | call the generic function. Any adjustment will be done at final | |
192 | link time. */ | |
193 | if (output_bfd != NULL) | |
194 | return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data, | |
195 | input_section, output_bfd, error_message); | |
196 | ||
197 | if (reloc_entry->address > bfd_get_section_limit (abfd, input_section)) | |
198 | return bfd_reloc_outofrange; | |
199 | octets = reloc_entry->address * bfd_octets_per_byte (abfd); | |
200 | ||
201 | /* Get symbol value. */ | |
202 | val = 0; | |
203 | if (!bfd_is_com_section (symbol->section)) | |
204 | val = symbol->value; | |
205 | if (symbol->section->output_section) | |
206 | val += symbol->section->output_section->vma; | |
207 | ||
208 | val += reloc_entry->addend; | |
209 | ||
210 | /* Make it pc-relative. */ | |
211 | val -= input_section->output_section->vma + input_section->output_offset; | |
212 | ||
213 | val >>= 2; | |
214 | if (val + 256 >= 512) | |
215 | return bfd_reloc_overflow; | |
216 | ||
217 | insn = bfd_get_32 (abfd, (bfd_byte *) data + octets); | |
218 | ||
219 | /* Move two high bits of value to REL9I and REL9 position. | |
220 | The mask will take care of selecting the right field. */ | |
221 | val = (val & 0x7f) | ((val & 0x180) << 7) | ((val & 0x180) << 16); | |
222 | insn &= ~reloc_entry->howto->dst_mask; | |
223 | insn |= val & reloc_entry->howto->dst_mask; | |
224 | bfd_put_32 (abfd, insn, (bfd_byte *) data + octets); | |
225 | return bfd_reloc_ok; | |
226 | } | |
227 | ||
228 | static bfd_boolean | |
229 | spu_elf_new_section_hook (bfd *abfd, asection *sec) | |
230 | { | |
231 | if (!sec->used_by_bfd) | |
232 | { | |
233 | struct _spu_elf_section_data *sdata; | |
234 | ||
235 | sdata = bfd_zalloc (abfd, sizeof (*sdata)); | |
236 | if (sdata == NULL) | |
237 | return FALSE; | |
238 | sec->used_by_bfd = sdata; | |
239 | } | |
240 | ||
241 | return _bfd_elf_new_section_hook (abfd, sec); | |
242 | } | |
243 | ||
124b52c6 AM |
244 | /* Set up overlay info for executables. */ |
245 | ||
246 | static bfd_boolean | |
247 | spu_elf_object_p (bfd *abfd) | |
248 | { | |
249 | if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) | |
250 | { | |
251 | unsigned int i, num_ovl, num_buf; | |
252 | Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr; | |
253 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); | |
254 | Elf_Internal_Phdr *last_phdr = NULL; | |
255 | ||
256 | for (num_buf = 0, num_ovl = 0, i = 0; i < ehdr->e_phnum; i++, phdr++) | |
257 | if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_OVERLAY) != 0) | |
258 | { | |
259 | unsigned int j; | |
260 | ||
261 | ++num_ovl; | |
262 | if (last_phdr == NULL | |
263 | || ((last_phdr->p_vaddr ^ phdr->p_vaddr) & 0x3ffff) != 0) | |
264 | ++num_buf; | |
265 | last_phdr = phdr; | |
266 | for (j = 1; j < elf_numsections (abfd); j++) | |
267 | { | |
268 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[j]; | |
269 | ||
270 | if (ELF_IS_SECTION_IN_SEGMENT_MEMORY (shdr, phdr)) | |
271 | { | |
272 | asection *sec = shdr->bfd_section; | |
273 | spu_elf_section_data (sec)->u.o.ovl_index = num_ovl; | |
274 | spu_elf_section_data (sec)->u.o.ovl_buf = num_buf; | |
275 | } | |
276 | } | |
277 | } | |
278 | } | |
279 | return TRUE; | |
280 | } | |
281 | ||
e9f53129 AM |
282 | /* Specially mark defined symbols named _EAR_* with BSF_KEEP so that |
283 | strip --strip-unneeded will not remove them. */ | |
284 | ||
285 | static void | |
286 | spu_elf_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym) | |
287 | { | |
288 | if (sym->name != NULL | |
289 | && sym->section != bfd_abs_section_ptr | |
290 | && strncmp (sym->name, "_EAR_", 5) == 0) | |
291 | sym->flags |= BSF_KEEP; | |
292 | } | |
293 | ||
294 | /* SPU ELF linker hash table. */ | |
295 | ||
296 | struct spu_link_hash_table | |
297 | { | |
298 | struct elf_link_hash_table elf; | |
299 | ||
64615358 AM |
300 | struct spu_elf_params *params; |
301 | ||
e9f53129 | 302 | /* Shortcuts to overlay sections. */ |
e9f53129 | 303 | asection *ovtab; |
cd4a7468 | 304 | asection *init; |
47f6dab9 AM |
305 | asection *toe; |
306 | asection **ovl_sec; | |
307 | ||
308 | /* Count of stubs in each overlay section. */ | |
309 | unsigned int *stub_count; | |
310 | ||
311 | /* The stub section for each overlay section. */ | |
312 | asection **stub_sec; | |
e9f53129 | 313 | |
a7e11ee1 | 314 | struct elf_link_hash_entry *ovly_entry[2]; |
e9f53129 | 315 | |
e9f53129 AM |
316 | /* Number of overlay buffers. */ |
317 | unsigned int num_buf; | |
318 | ||
319 | /* Total number of overlays. */ | |
320 | unsigned int num_overlays; | |
321 | ||
cd4a7468 AM |
322 | /* For soft icache. */ |
323 | unsigned int line_size_log2; | |
324 | unsigned int num_lines_log2; | |
97fc8d84 | 325 | unsigned int fromelem_size_log2; |
cd4a7468 | 326 | |
9dcc4794 AM |
327 | /* How much memory we have. */ |
328 | unsigned int local_store; | |
329 | /* Local store --auto-overlay should reserve for non-overlay | |
330 | functions and data. */ | |
331 | unsigned int overlay_fixed; | |
332 | /* Local store --auto-overlay should reserve for stack and heap. */ | |
333 | unsigned int reserved; | |
99302af9 AM |
334 | /* If reserved is not specified, stack analysis will calculate a value |
335 | for the stack. This parameter adjusts that value to allow for | |
336 | negative sp access (the ABI says 2000 bytes below sp are valid, | |
337 | and the overlay manager uses some of this area). */ | |
338 | int extra_stack_space; | |
9dcc4794 AM |
339 | /* Count of overlay stubs needed in non-overlay area. */ |
340 | unsigned int non_ovly_stub; | |
341 | ||
e9f53129 | 342 | /* Set on error. */ |
47f6dab9 | 343 | unsigned int stub_err : 1; |
e9f53129 AM |
344 | }; |
345 | ||
47f6dab9 | 346 | /* Hijack the generic got fields for overlay stub accounting. */ |
e9f53129 | 347 | |
47f6dab9 | 348 | struct got_entry |
e9f53129 | 349 | { |
47f6dab9 AM |
350 | struct got_entry *next; |
351 | unsigned int ovl; | |
cd4a7468 AM |
352 | union { |
353 | bfd_vma addend; | |
354 | bfd_vma br_addr; | |
355 | }; | |
47f6dab9 | 356 | bfd_vma stub_addr; |
e9f53129 AM |
357 | }; |
358 | ||
47f6dab9 AM |
359 | #define spu_hash_table(p) \ |
360 | ((struct spu_link_hash_table *) ((p)->hash)) | |
e9f53129 | 361 | |
64615358 AM |
362 | struct call_info |
363 | { | |
364 | struct function_info *fun; | |
365 | struct call_info *next; | |
366 | unsigned int count; | |
367 | unsigned int max_depth; | |
368 | unsigned int is_tail : 1; | |
369 | unsigned int is_pasted : 1; | |
5ebe2858 | 370 | unsigned int broken_cycle : 1; |
cd4a7468 | 371 | unsigned int priority : 13; |
64615358 AM |
372 | }; |
373 | ||
374 | struct function_info | |
375 | { | |
376 | /* List of functions called. Also branches to hot/cold part of | |
377 | function. */ | |
378 | struct call_info *call_list; | |
379 | /* For hot/cold part of function, point to owner. */ | |
380 | struct function_info *start; | |
381 | /* Symbol at start of function. */ | |
382 | union { | |
383 | Elf_Internal_Sym *sym; | |
384 | struct elf_link_hash_entry *h; | |
385 | } u; | |
386 | /* Function section. */ | |
387 | asection *sec; | |
388 | asection *rodata; | |
389 | /* Where last called from, and number of sections called from. */ | |
390 | asection *last_caller; | |
391 | unsigned int call_count; | |
392 | /* Address range of (this part of) function. */ | |
393 | bfd_vma lo, hi; | |
cd4a7468 AM |
394 | /* Offset where we found a store of lr, or -1 if none found. */ |
395 | bfd_vma lr_store; | |
396 | /* Offset where we found the stack adjustment insn. */ | |
397 | bfd_vma sp_adjust; | |
64615358 AM |
398 | /* Stack usage. */ |
399 | int stack; | |
400 | /* Distance from root of call tree. Tail and hot/cold branches | |
401 | count as one deeper. We aren't counting stack frames here. */ | |
402 | unsigned int depth; | |
403 | /* Set if global symbol. */ | |
404 | unsigned int global : 1; | |
405 | /* Set if known to be start of function (as distinct from a hunk | |
406 | in hot/cold section. */ | |
407 | unsigned int is_func : 1; | |
408 | /* Set if not a root node. */ | |
409 | unsigned int non_root : 1; | |
410 | /* Flags used during call tree traversal. It's cheaper to replicate | |
411 | the visit flags than have one which needs clearing after a traversal. */ | |
412 | unsigned int visit1 : 1; | |
413 | unsigned int visit2 : 1; | |
414 | unsigned int marking : 1; | |
415 | unsigned int visit3 : 1; | |
416 | unsigned int visit4 : 1; | |
417 | unsigned int visit5 : 1; | |
418 | unsigned int visit6 : 1; | |
419 | unsigned int visit7 : 1; | |
420 | }; | |
421 | ||
422 | struct spu_elf_stack_info | |
423 | { | |
424 | int num_fun; | |
425 | int max_fun; | |
426 | /* Variable size array describing functions, one per contiguous | |
427 | address range belonging to a function. */ | |
428 | struct function_info fun[1]; | |
429 | }; | |
430 | ||
cd4a7468 AM |
431 | static struct function_info *find_function (asection *, bfd_vma, |
432 | struct bfd_link_info *); | |
433 | ||
e9f53129 AM |
434 | /* Create a spu ELF linker hash table. */ |
435 | ||
436 | static struct bfd_link_hash_table * | |
437 | spu_elf_link_hash_table_create (bfd *abfd) | |
438 | { | |
439 | struct spu_link_hash_table *htab; | |
440 | ||
441 | htab = bfd_malloc (sizeof (*htab)); | |
442 | if (htab == NULL) | |
443 | return NULL; | |
444 | ||
445 | if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, | |
446 | _bfd_elf_link_hash_newfunc, | |
447 | sizeof (struct elf_link_hash_entry))) | |
448 | { | |
449 | free (htab); | |
450 | return NULL; | |
451 | } | |
452 | ||
47f6dab9 AM |
453 | memset (&htab->ovtab, 0, |
454 | sizeof (*htab) - offsetof (struct spu_link_hash_table, ovtab)); | |
e9f53129 | 455 | |
47f6dab9 AM |
456 | htab->elf.init_got_refcount.refcount = 0; |
457 | htab->elf.init_got_refcount.glist = NULL; | |
458 | htab->elf.init_got_offset.offset = 0; | |
459 | htab->elf.init_got_offset.glist = NULL; | |
e9f53129 AM |
460 | return &htab->elf.root; |
461 | } | |
462 | ||
64615358 AM |
463 | void |
464 | spu_elf_setup (struct bfd_link_info *info, struct spu_elf_params *params) | |
465 | { | |
97fc8d84 UW |
466 | bfd_vma max_branch_log2; |
467 | ||
64615358 AM |
468 | struct spu_link_hash_table *htab = spu_hash_table (info); |
469 | htab->params = params; | |
cd4a7468 AM |
470 | htab->line_size_log2 = bfd_log2 (htab->params->line_size); |
471 | htab->num_lines_log2 = bfd_log2 (htab->params->num_lines); | |
97fc8d84 UW |
472 | |
473 | /* For the software i-cache, we provide a "from" list whose size | |
474 | is a power-of-two number of quadwords, big enough to hold one | |
475 | byte per outgoing branch. Compute this number here. */ | |
476 | max_branch_log2 = bfd_log2 (htab->params->max_branch); | |
477 | htab->fromelem_size_log2 = max_branch_log2 > 4 ? max_branch_log2 - 4 : 0; | |
64615358 AM |
478 | } |
479 | ||
e9f53129 AM |
480 | /* Find the symbol for the given R_SYMNDX in IBFD and set *HP and *SYMP |
481 | to (hash, NULL) for global symbols, and (NULL, sym) for locals. Set | |
482 | *SYMSECP to the symbol's section. *LOCSYMSP caches local syms. */ | |
483 | ||
484 | static bfd_boolean | |
485 | get_sym_h (struct elf_link_hash_entry **hp, | |
486 | Elf_Internal_Sym **symp, | |
487 | asection **symsecp, | |
488 | Elf_Internal_Sym **locsymsp, | |
489 | unsigned long r_symndx, | |
490 | bfd *ibfd) | |
491 | { | |
492 | Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
493 | ||
494 | if (r_symndx >= symtab_hdr->sh_info) | |
495 | { | |
496 | struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd); | |
497 | struct elf_link_hash_entry *h; | |
498 | ||
499 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
500 | while (h->root.type == bfd_link_hash_indirect | |
501 | || h->root.type == bfd_link_hash_warning) | |
502 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
503 | ||
504 | if (hp != NULL) | |
505 | *hp = h; | |
506 | ||
507 | if (symp != NULL) | |
508 | *symp = NULL; | |
509 | ||
510 | if (symsecp != NULL) | |
511 | { | |
512 | asection *symsec = NULL; | |
513 | if (h->root.type == bfd_link_hash_defined | |
514 | || h->root.type == bfd_link_hash_defweak) | |
515 | symsec = h->root.u.def.section; | |
516 | *symsecp = symsec; | |
517 | } | |
518 | } | |
519 | else | |
520 | { | |
521 | Elf_Internal_Sym *sym; | |
522 | Elf_Internal_Sym *locsyms = *locsymsp; | |
523 | ||
524 | if (locsyms == NULL) | |
525 | { | |
526 | locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
527 | if (locsyms == NULL) | |
1f27ab8d AM |
528 | locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, |
529 | symtab_hdr->sh_info, | |
530 | 0, NULL, NULL, NULL); | |
e9f53129 AM |
531 | if (locsyms == NULL) |
532 | return FALSE; | |
533 | *locsymsp = locsyms; | |
534 | } | |
535 | sym = locsyms + r_symndx; | |
536 | ||
537 | if (hp != NULL) | |
538 | *hp = NULL; | |
539 | ||
540 | if (symp != NULL) | |
541 | *symp = sym; | |
542 | ||
543 | if (symsecp != NULL) | |
cb33740c | 544 | *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx); |
e9f53129 | 545 | } |
49fa1e15 | 546 | |
e9f53129 AM |
547 | return TRUE; |
548 | } | |
549 | ||
e9f53129 AM |
550 | /* Create the note section if not already present. This is done early so |
551 | that the linker maps the sections to the right place in the output. */ | |
552 | ||
553 | bfd_boolean | |
64615358 | 554 | spu_elf_create_sections (struct bfd_link_info *info) |
e9f53129 AM |
555 | { |
556 | bfd *ibfd; | |
557 | ||
58eb693e | 558 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) |
e9f53129 AM |
559 | if (bfd_get_section_by_name (ibfd, SPU_PTNOTE_SPUNAME) != NULL) |
560 | break; | |
561 | ||
562 | if (ibfd == NULL) | |
563 | { | |
564 | /* Make SPU_PTNOTE_SPUNAME section. */ | |
565 | asection *s; | |
566 | size_t name_len; | |
567 | size_t size; | |
568 | bfd_byte *data; | |
569 | flagword flags; | |
570 | ||
571 | ibfd = info->input_bfds; | |
572 | flags = SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
573 | s = bfd_make_section_anyway_with_flags (ibfd, SPU_PTNOTE_SPUNAME, flags); | |
574 | if (s == NULL | |
575 | || !bfd_set_section_alignment (ibfd, s, 4)) | |
576 | return FALSE; | |
577 | ||
c65be8d7 | 578 | name_len = strlen (bfd_get_filename (info->output_bfd)) + 1; |
e9f53129 AM |
579 | size = 12 + ((sizeof (SPU_PLUGIN_NAME) + 3) & -4); |
580 | size += (name_len + 3) & -4; | |
581 | ||
582 | if (!bfd_set_section_size (ibfd, s, size)) | |
583 | return FALSE; | |
584 | ||
585 | data = bfd_zalloc (ibfd, size); | |
586 | if (data == NULL) | |
587 | return FALSE; | |
588 | ||
589 | bfd_put_32 (ibfd, sizeof (SPU_PLUGIN_NAME), data + 0); | |
590 | bfd_put_32 (ibfd, name_len, data + 4); | |
591 | bfd_put_32 (ibfd, 1, data + 8); | |
592 | memcpy (data + 12, SPU_PLUGIN_NAME, sizeof (SPU_PLUGIN_NAME)); | |
593 | memcpy (data + 12 + ((sizeof (SPU_PLUGIN_NAME) + 3) & -4), | |
c65be8d7 | 594 | bfd_get_filename (info->output_bfd), name_len); |
e9f53129 AM |
595 | s->contents = data; |
596 | } | |
597 | ||
598 | return TRUE; | |
599 | } | |
600 | ||
e9f53129 AM |
601 | /* qsort predicate to sort sections by vma. */ |
602 | ||
603 | static int | |
604 | sort_sections (const void *a, const void *b) | |
605 | { | |
606 | const asection *const *s1 = a; | |
607 | const asection *const *s2 = b; | |
608 | bfd_signed_vma delta = (*s1)->vma - (*s2)->vma; | |
609 | ||
610 | if (delta != 0) | |
611 | return delta < 0 ? -1 : 1; | |
612 | ||
613 | return (*s1)->index - (*s2)->index; | |
614 | } | |
615 | ||
ad2adbc8 AM |
616 | /* Identify overlays in the output bfd, and number them. |
617 | Returns 0 on error, 1 if no overlays, 2 if overlays. */ | |
e9f53129 | 618 | |
ad2adbc8 | 619 | int |
c65be8d7 | 620 | spu_elf_find_overlays (struct bfd_link_info *info) |
e9f53129 AM |
621 | { |
622 | struct spu_link_hash_table *htab = spu_hash_table (info); | |
623 | asection **alloc_sec; | |
624 | unsigned int i, n, ovl_index, num_buf; | |
625 | asection *s; | |
626 | bfd_vma ovl_end; | |
ad2adbc8 AM |
627 | static const char *const entry_names[2][2] = { |
628 | { "__ovly_load", "__icache_br_handler" }, | |
629 | { "__ovly_return", "__icache_call_handler" } | |
630 | }; | |
e9f53129 | 631 | |
c65be8d7 | 632 | if (info->output_bfd->section_count < 2) |
ad2adbc8 | 633 | return 1; |
e9f53129 | 634 | |
c65be8d7 AM |
635 | alloc_sec |
636 | = bfd_malloc (info->output_bfd->section_count * sizeof (*alloc_sec)); | |
e9f53129 | 637 | if (alloc_sec == NULL) |
ad2adbc8 | 638 | return 0; |
e9f53129 AM |
639 | |
640 | /* Pick out all the alloced sections. */ | |
c65be8d7 | 641 | for (n = 0, s = info->output_bfd->sections; s != NULL; s = s->next) |
e9f53129 AM |
642 | if ((s->flags & SEC_ALLOC) != 0 |
643 | && (s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != SEC_THREAD_LOCAL | |
644 | && s->size != 0) | |
645 | alloc_sec[n++] = s; | |
646 | ||
647 | if (n == 0) | |
648 | { | |
649 | free (alloc_sec); | |
ad2adbc8 | 650 | return 1; |
e9f53129 AM |
651 | } |
652 | ||
653 | /* Sort them by vma. */ | |
654 | qsort (alloc_sec, n, sizeof (*alloc_sec), sort_sections); | |
655 | ||
e9f53129 | 656 | ovl_end = alloc_sec[0]->vma + alloc_sec[0]->size; |
cd4a7468 | 657 | if (htab->params->ovly_flavour == ovly_soft_icache) |
e9f53129 | 658 | { |
cd4a7468 AM |
659 | /* Look for an overlapping vma to find the first overlay section. */ |
660 | bfd_vma vma_start = 0; | |
661 | bfd_vma lma_start = 0; | |
662 | ||
663 | for (i = 1; i < n; i++) | |
e9f53129 | 664 | { |
cd4a7468 AM |
665 | s = alloc_sec[i]; |
666 | if (s->vma < ovl_end) | |
667 | { | |
668 | asection *s0 = alloc_sec[i - 1]; | |
669 | vma_start = s0->vma; | |
77ae44b0 AM |
670 | if (strncmp (s0->name, ".ovl.init", 9) != 0) |
671 | lma_start = s0->lma; | |
672 | else | |
673 | lma_start = s->lma; | |
cd4a7468 AM |
674 | ovl_end = (s0->vma |
675 | + ((bfd_vma) 1 | |
676 | << (htab->num_lines_log2 + htab->line_size_log2))); | |
677 | --i; | |
678 | break; | |
679 | } | |
680 | else | |
681 | ovl_end = s->vma + s->size; | |
682 | } | |
e9f53129 | 683 | |
cd4a7468 AM |
684 | /* Now find any sections within the cache area. */ |
685 | for (ovl_index = 0, num_buf = 0; i < n; i++) | |
686 | { | |
687 | s = alloc_sec[i]; | |
688 | if (s->vma >= ovl_end) | |
689 | break; | |
690 | ||
691 | /* A section in an overlay area called .ovl.init is not | |
692 | an overlay, in the sense that it might be loaded in | |
693 | by the overlay manager, but rather the initial | |
694 | section contents for the overlay buffer. */ | |
695 | if (strncmp (s->name, ".ovl.init", 9) != 0) | |
e9f53129 | 696 | { |
cd4a7468 AM |
697 | num_buf = ((s->vma - vma_start) >> htab->line_size_log2) + 1; |
698 | if (((s->vma - vma_start) & (htab->params->line_size - 1)) | |
699 | || ((s->lma - lma_start) & (htab->params->line_size - 1))) | |
700 | { | |
701 | info->callbacks->einfo (_("%X%P: overlay section %A " | |
702 | "does not start on a cache line.\n"), | |
703 | s); | |
3209bffa | 704 | bfd_set_error (bfd_error_bad_value); |
ad2adbc8 | 705 | return 0; |
cd4a7468 AM |
706 | } |
707 | else if (s->size > htab->params->line_size) | |
708 | { | |
709 | info->callbacks->einfo (_("%X%P: overlay section %A " | |
710 | "is larger than a cache line.\n"), | |
711 | s); | |
3209bffa | 712 | bfd_set_error (bfd_error_bad_value); |
ad2adbc8 | 713 | return 0; |
cd4a7468 AM |
714 | } |
715 | ||
716 | alloc_sec[ovl_index++] = s; | |
717 | spu_elf_section_data (s)->u.o.ovl_index | |
718 | = ((s->lma - lma_start) >> htab->line_size_log2) + 1; | |
719 | spu_elf_section_data (s)->u.o.ovl_buf = num_buf; | |
e9f53129 | 720 | } |
cd4a7468 AM |
721 | } |
722 | ||
723 | /* Ensure there are no more overlay sections. */ | |
724 | for ( ; i < n; i++) | |
725 | { | |
726 | s = alloc_sec[i]; | |
727 | if (s->vma < ovl_end) | |
e9f53129 | 728 | { |
cd4a7468 AM |
729 | info->callbacks->einfo (_("%X%P: overlay section %A " |
730 | "is not in cache area.\n"), | |
731 | alloc_sec[i-1]); | |
3209bffa | 732 | bfd_set_error (bfd_error_bad_value); |
ad2adbc8 | 733 | return 0; |
e9f53129 | 734 | } |
cd4a7468 AM |
735 | else |
736 | ovl_end = s->vma + s->size; | |
737 | } | |
738 | } | |
739 | else | |
740 | { | |
741 | /* Look for overlapping vmas. Any with overlap must be overlays. | |
742 | Count them. Also count the number of overlay regions. */ | |
743 | for (ovl_index = 0, num_buf = 0, i = 1; i < n; i++) | |
744 | { | |
745 | s = alloc_sec[i]; | |
746 | if (s->vma < ovl_end) | |
747 | { | |
748 | asection *s0 = alloc_sec[i - 1]; | |
749 | ||
750 | if (spu_elf_section_data (s0)->u.o.ovl_index == 0) | |
751 | { | |
752 | ++num_buf; | |
753 | if (strncmp (s0->name, ".ovl.init", 9) != 0) | |
754 | { | |
755 | alloc_sec[ovl_index] = s0; | |
756 | spu_elf_section_data (s0)->u.o.ovl_index = ++ovl_index; | |
757 | spu_elf_section_data (s0)->u.o.ovl_buf = num_buf; | |
758 | } | |
759 | else | |
760 | ovl_end = s->vma + s->size; | |
761 | } | |
762 | if (strncmp (s->name, ".ovl.init", 9) != 0) | |
763 | { | |
764 | alloc_sec[ovl_index] = s; | |
765 | spu_elf_section_data (s)->u.o.ovl_index = ++ovl_index; | |
766 | spu_elf_section_data (s)->u.o.ovl_buf = num_buf; | |
767 | if (s0->vma != s->vma) | |
768 | { | |
769 | info->callbacks->einfo (_("%X%P: overlay sections %A " | |
770 | "and %A do not start at the " | |
771 | "same address.\n"), | |
772 | s0, s); | |
3209bffa | 773 | bfd_set_error (bfd_error_bad_value); |
ad2adbc8 | 774 | return 0; |
cd4a7468 AM |
775 | } |
776 | if (ovl_end < s->vma + s->size) | |
777 | ovl_end = s->vma + s->size; | |
778 | } | |
779 | } | |
780 | else | |
47f6dab9 | 781 | ovl_end = s->vma + s->size; |
e9f53129 | 782 | } |
e9f53129 AM |
783 | } |
784 | ||
785 | htab->num_overlays = ovl_index; | |
786 | htab->num_buf = num_buf; | |
47f6dab9 | 787 | htab->ovl_sec = alloc_sec; |
ad2adbc8 AM |
788 | |
789 | if (ovl_index == 0) | |
790 | return 1; | |
791 | ||
792 | for (i = 0; i < 2; i++) | |
793 | { | |
794 | const char *name; | |
795 | struct elf_link_hash_entry *h; | |
796 | ||
797 | name = entry_names[i][htab->params->ovly_flavour]; | |
798 | h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE); | |
799 | if (h == NULL) | |
800 | return 0; | |
801 | ||
802 | if (h->root.type == bfd_link_hash_new) | |
803 | { | |
804 | h->root.type = bfd_link_hash_undefined; | |
805 | h->ref_regular = 1; | |
806 | h->ref_regular_nonweak = 1; | |
807 | h->non_elf = 0; | |
808 | } | |
809 | htab->ovly_entry[i] = h; | |
810 | } | |
811 | ||
812 | return 2; | |
e9f53129 AM |
813 | } |
814 | ||
cd4a7468 AM |
815 | /* Non-zero to use bra in overlay stubs rather than br. */ |
816 | #define BRA_STUBS 0 | |
817 | ||
818 | #define BRA 0x30000000 | |
819 | #define BRASL 0x31000000 | |
47f6dab9 | 820 | #define BR 0x32000000 |
cd4a7468 | 821 | #define BRSL 0x33000000 |
e9f53129 | 822 | #define NOP 0x40200000 |
47f6dab9 AM |
823 | #define LNOP 0x00200000 |
824 | #define ILA 0x42000000 | |
e9f53129 | 825 | |
49fa1e15 | 826 | /* Return true for all relative and absolute branch instructions. |
e9f53129 AM |
827 | bra 00110000 0.. |
828 | brasl 00110001 0.. | |
829 | br 00110010 0.. | |
830 | brsl 00110011 0.. | |
831 | brz 00100000 0.. | |
832 | brnz 00100001 0.. | |
833 | brhz 00100010 0.. | |
49fa1e15 AM |
834 | brhnz 00100011 0.. */ |
835 | ||
836 | static bfd_boolean | |
837 | is_branch (const unsigned char *insn) | |
838 | { | |
839 | return (insn[0] & 0xec) == 0x20 && (insn[1] & 0x80) == 0; | |
840 | } | |
841 | ||
fad9eaf0 AM |
842 | /* Return true for all indirect branch instructions. |
843 | bi 00110101 000 | |
844 | bisl 00110101 001 | |
845 | iret 00110101 010 | |
846 | bisled 00110101 011 | |
847 | biz 00100101 000 | |
848 | binz 00100101 001 | |
849 | bihz 00100101 010 | |
850 | bihnz 00100101 011 */ | |
851 | ||
852 | static bfd_boolean | |
853 | is_indirect_branch (const unsigned char *insn) | |
854 | { | |
855 | return (insn[0] & 0xef) == 0x25 && (insn[1] & 0x80) == 0; | |
856 | } | |
857 | ||
49fa1e15 | 858 | /* Return true for branch hint instructions. |
e9f53129 AM |
859 | hbra 0001000.. |
860 | hbrr 0001001.. */ | |
861 | ||
862 | static bfd_boolean | |
49fa1e15 | 863 | is_hint (const unsigned char *insn) |
e9f53129 | 864 | { |
49fa1e15 | 865 | return (insn[0] & 0xfc) == 0x10; |
e9f53129 AM |
866 | } |
867 | ||
fdba2fcd | 868 | /* True if INPUT_SECTION might need overlay stubs. */ |
aa7a0635 AM |
869 | |
870 | static bfd_boolean | |
64615358 | 871 | maybe_needs_stubs (asection *input_section) |
fdba2fcd AM |
872 | { |
873 | /* No stubs for debug sections and suchlike. */ | |
874 | if ((input_section->flags & SEC_ALLOC) == 0) | |
875 | return FALSE; | |
876 | ||
877 | /* No stubs for link-once sections that will be discarded. */ | |
64615358 | 878 | if (input_section->output_section == bfd_abs_section_ptr) |
fdba2fcd AM |
879 | return FALSE; |
880 | ||
881 | /* Don't create stubs for .eh_frame references. */ | |
882 | if (strcmp (input_section->name, ".eh_frame") == 0) | |
883 | return FALSE; | |
884 | ||
885 | return TRUE; | |
886 | } | |
887 | ||
888 | enum _stub_type | |
889 | { | |
890 | no_stub, | |
cd4a7468 AM |
891 | call_ovl_stub, |
892 | br000_ovl_stub, | |
893 | br001_ovl_stub, | |
894 | br010_ovl_stub, | |
895 | br011_ovl_stub, | |
896 | br100_ovl_stub, | |
897 | br101_ovl_stub, | |
898 | br110_ovl_stub, | |
899 | br111_ovl_stub, | |
fdba2fcd AM |
900 | nonovl_stub, |
901 | stub_error | |
902 | }; | |
903 | ||
904 | /* Return non-zero if this reloc symbol should go via an overlay stub. | |
905 | Return 2 if the stub must be in non-overlay area. */ | |
906 | ||
907 | static enum _stub_type | |
908 | needs_ovl_stub (struct elf_link_hash_entry *h, | |
909 | Elf_Internal_Sym *sym, | |
aa7a0635 AM |
910 | asection *sym_sec, |
911 | asection *input_section, | |
fdba2fcd AM |
912 | Elf_Internal_Rela *irela, |
913 | bfd_byte *contents, | |
914 | struct bfd_link_info *info) | |
aa7a0635 | 915 | { |
fdba2fcd AM |
916 | struct spu_link_hash_table *htab = spu_hash_table (info); |
917 | enum elf_spu_reloc_type r_type; | |
918 | unsigned int sym_type; | |
cd4a7468 | 919 | bfd_boolean branch, hint, call; |
fdba2fcd | 920 | enum _stub_type ret = no_stub; |
cd4a7468 | 921 | bfd_byte insn[4]; |
aa7a0635 AM |
922 | |
923 | if (sym_sec == NULL | |
64615358 | 924 | || sym_sec->output_section == bfd_abs_section_ptr |
2c67c5f3 | 925 | || spu_elf_section_data (sym_sec->output_section) == NULL) |
fdba2fcd | 926 | return ret; |
aa7a0635 | 927 | |
fdba2fcd AM |
928 | if (h != NULL) |
929 | { | |
930 | /* Ensure no stubs for user supplied overlay manager syms. */ | |
ad2adbc8 | 931 | if (h == htab->ovly_entry[0] || h == htab->ovly_entry[1]) |
fdba2fcd AM |
932 | return ret; |
933 | ||
934 | /* setjmp always goes via an overlay stub, because then the return | |
935 | and hence the longjmp goes via __ovly_return. That magically | |
936 | makes setjmp/longjmp between overlays work. */ | |
937 | if (strncmp (h->root.root.string, "setjmp", 6) == 0 | |
938 | && (h->root.root.string[6] == '\0' || h->root.root.string[6] == '@')) | |
cd4a7468 | 939 | ret = call_ovl_stub; |
fdba2fcd | 940 | } |
aa7a0635 | 941 | |
fdba2fcd AM |
942 | if (h != NULL) |
943 | sym_type = h->type; | |
944 | else | |
945 | sym_type = ELF_ST_TYPE (sym->st_info); | |
946 | ||
947 | r_type = ELF32_R_TYPE (irela->r_info); | |
948 | branch = FALSE; | |
cd4a7468 AM |
949 | hint = FALSE; |
950 | call = FALSE; | |
fdba2fcd AM |
951 | if (r_type == R_SPU_REL16 || r_type == R_SPU_ADDR16) |
952 | { | |
fdba2fcd AM |
953 | if (contents == NULL) |
954 | { | |
955 | contents = insn; | |
956 | if (!bfd_get_section_contents (input_section->owner, | |
957 | input_section, | |
958 | contents, | |
959 | irela->r_offset, 4)) | |
960 | return stub_error; | |
961 | } | |
962 | else | |
963 | contents += irela->r_offset; | |
964 | ||
cd4a7468 AM |
965 | branch = is_branch (contents); |
966 | hint = is_hint (contents); | |
967 | if (branch || hint) | |
fdba2fcd | 968 | { |
cd4a7468 AM |
969 | call = (contents[0] & 0xfd) == 0x31; |
970 | if (call | |
fdba2fcd | 971 | && sym_type != STT_FUNC |
9dcc4794 | 972 | && contents != insn) |
fdba2fcd AM |
973 | { |
974 | /* It's common for people to write assembly and forget | |
975 | to give function symbols the right type. Handle | |
976 | calls to such symbols, but warn so that (hopefully) | |
977 | people will fix their code. We need the symbol | |
978 | type to be correct to distinguish function pointer | |
979 | initialisation from other pointer initialisations. */ | |
980 | const char *sym_name; | |
981 | ||
982 | if (h != NULL) | |
983 | sym_name = h->root.root.string; | |
984 | else | |
985 | { | |
986 | Elf_Internal_Shdr *symtab_hdr; | |
987 | symtab_hdr = &elf_tdata (input_section->owner)->symtab_hdr; | |
988 | sym_name = bfd_elf_sym_name (input_section->owner, | |
989 | symtab_hdr, | |
990 | sym, | |
991 | sym_sec); | |
992 | } | |
993 | (*_bfd_error_handler) (_("warning: call to non-function" | |
994 | " symbol %s defined in %B"), | |
995 | sym_sec->owner, sym_name); | |
996 | ||
997 | } | |
998 | } | |
999 | } | |
1000 | ||
cd4a7468 AM |
1001 | if ((!branch && htab->params->ovly_flavour == ovly_soft_icache) |
1002 | || (sym_type != STT_FUNC | |
1003 | && !(branch || hint) | |
1004 | && (sym_sec->flags & SEC_CODE) == 0)) | |
1005 | return no_stub; | |
1006 | ||
1007 | /* Usually, symbols in non-overlay sections don't need stubs. */ | |
1008 | if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index == 0 | |
1009 | && !htab->params->non_overlay_stubs) | |
fdba2fcd | 1010 | return ret; |
aa7a0635 AM |
1011 | |
1012 | /* A reference from some other section to a symbol in an overlay | |
1013 | section needs a stub. */ | |
47f6dab9 AM |
1014 | if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index |
1015 | != spu_elf_section_data (input_section->output_section)->u.o.ovl_index) | |
cd4a7468 AM |
1016 | { |
1017 | if (call || sym_type == STT_FUNC) | |
1018 | ret = call_ovl_stub; | |
1019 | else | |
1020 | { | |
1021 | ret = br000_ovl_stub; | |
1022 | ||
1023 | if (branch) | |
1024 | { | |
1025 | unsigned int lrlive = (contents[1] & 0x70) >> 4; | |
1026 | ret += lrlive; | |
1027 | } | |
1028 | } | |
1029 | } | |
aa7a0635 AM |
1030 | |
1031 | /* If this insn isn't a branch then we are possibly taking the | |
cd4a7468 AM |
1032 | address of a function and passing it out somehow. Soft-icache code |
1033 | always generates inline code to do indirect branches. */ | |
1034 | if (!(branch || hint) | |
1035 | && sym_type == STT_FUNC | |
1036 | && htab->params->ovly_flavour != ovly_soft_icache) | |
1037 | ret = nonovl_stub; | |
1038 | ||
1039 | return ret; | |
aa7a0635 AM |
1040 | } |
1041 | ||
47f6dab9 AM |
1042 | static bfd_boolean |
1043 | count_stub (struct spu_link_hash_table *htab, | |
1044 | bfd *ibfd, | |
1045 | asection *isec, | |
fdba2fcd | 1046 | enum _stub_type stub_type, |
47f6dab9 AM |
1047 | struct elf_link_hash_entry *h, |
1048 | const Elf_Internal_Rela *irela) | |
1049 | { | |
1050 | unsigned int ovl = 0; | |
1051 | struct got_entry *g, **head; | |
4a628337 | 1052 | bfd_vma addend; |
47f6dab9 AM |
1053 | |
1054 | /* If this instruction is a branch or call, we need a stub | |
1055 | for it. One stub per function per overlay. | |
1056 | If it isn't a branch, then we are taking the address of | |
1057 | this function so need a stub in the non-overlay area | |
1058 | for it. One stub per function. */ | |
fdba2fcd | 1059 | if (stub_type != nonovl_stub) |
47f6dab9 AM |
1060 | ovl = spu_elf_section_data (isec->output_section)->u.o.ovl_index; |
1061 | ||
1062 | if (h != NULL) | |
1063 | head = &h->got.glist; | |
1064 | else | |
1065 | { | |
1066 | if (elf_local_got_ents (ibfd) == NULL) | |
1067 | { | |
1068 | bfd_size_type amt = (elf_tdata (ibfd)->symtab_hdr.sh_info | |
1069 | * sizeof (*elf_local_got_ents (ibfd))); | |
1070 | elf_local_got_ents (ibfd) = bfd_zmalloc (amt); | |
1071 | if (elf_local_got_ents (ibfd) == NULL) | |
1072 | return FALSE; | |
1073 | } | |
1074 | head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info); | |
1075 | } | |
1076 | ||
cd4a7468 AM |
1077 | if (htab->params->ovly_flavour == ovly_soft_icache) |
1078 | { | |
1079 | htab->stub_count[ovl] += 1; | |
1080 | return TRUE; | |
1081 | } | |
1082 | ||
4a628337 AM |
1083 | addend = 0; |
1084 | if (irela != NULL) | |
1085 | addend = irela->r_addend; | |
47f6dab9 AM |
1086 | |
1087 | if (ovl == 0) | |
1088 | { | |
1089 | struct got_entry *gnext; | |
1090 | ||
4a628337 AM |
1091 | for (g = *head; g != NULL; g = g->next) |
1092 | if (g->addend == addend && g->ovl == 0) | |
1093 | break; | |
1094 | ||
1095 | if (g == NULL) | |
47f6dab9 | 1096 | { |
4a628337 AM |
1097 | /* Need a new non-overlay area stub. Zap other stubs. */ |
1098 | for (g = *head; g != NULL; g = gnext) | |
1099 | { | |
1100 | gnext = g->next; | |
1101 | if (g->addend == addend) | |
1102 | { | |
1103 | htab->stub_count[g->ovl] -= 1; | |
1104 | free (g); | |
1105 | } | |
1106 | } | |
47f6dab9 AM |
1107 | } |
1108 | } | |
1109 | else | |
1110 | { | |
4a628337 AM |
1111 | for (g = *head; g != NULL; g = g->next) |
1112 | if (g->addend == addend && (g->ovl == ovl || g->ovl == 0)) | |
47f6dab9 AM |
1113 | break; |
1114 | } | |
1115 | ||
1116 | if (g == NULL) | |
1117 | { | |
1118 | g = bfd_malloc (sizeof *g); | |
1119 | if (g == NULL) | |
1120 | return FALSE; | |
1121 | g->ovl = ovl; | |
4a628337 | 1122 | g->addend = addend; |
47f6dab9 AM |
1123 | g->stub_addr = (bfd_vma) -1; |
1124 | g->next = *head; | |
1125 | *head = g; | |
1126 | ||
1127 | htab->stub_count[ovl] += 1; | |
1128 | } | |
1129 | ||
1130 | return TRUE; | |
1131 | } | |
1132 | ||
64615358 | 1133 | /* Support two sizes of overlay stubs, a slower more compact stub of two |
a7e11ee1 AM |
1134 | intructions, and a faster stub of four instructions. |
1135 | Soft-icache stubs are four or eight words. */ | |
1136 | ||
1137 | static unsigned int | |
1138 | ovl_stub_size (struct spu_elf_params *params) | |
1139 | { | |
1140 | return 16 << params->ovly_flavour >> params->compact_stub; | |
1141 | } | |
64615358 AM |
1142 | |
1143 | static unsigned int | |
a7e11ee1 | 1144 | ovl_stub_size_log2 (struct spu_elf_params *params) |
64615358 | 1145 | { |
a7e11ee1 | 1146 | return 4 + params->ovly_flavour - params->compact_stub; |
64615358 AM |
1147 | } |
1148 | ||
47f6dab9 AM |
1149 | /* Two instruction overlay stubs look like: |
1150 | ||
1151 | brsl $75,__ovly_load | |
1152 | .word target_ovl_and_address | |
1153 | ||
1154 | ovl_and_address is a word with the overlay number in the top 14 bits | |
1155 | and local store address in the bottom 18 bits. | |
1156 | ||
1157 | Four instruction overlay stubs look like: | |
1158 | ||
1159 | ila $78,ovl_number | |
1160 | lnop | |
1161 | ila $79,target_address | |
cd4a7468 AM |
1162 | br __ovly_load |
1163 | ||
1164 | Software icache stubs are: | |
1165 | ||
1166 | .word target_index | |
1167 | .word target_ia; | |
1168 | .word lrlive_branchlocalstoreaddr; | |
1169 | brasl $75,__icache_br_handler | |
1170 | .quad xor_pattern | |
1171 | */ | |
47f6dab9 AM |
1172 | |
1173 | static bfd_boolean | |
cd4a7468 | 1174 | build_stub (struct bfd_link_info *info, |
47f6dab9 AM |
1175 | bfd *ibfd, |
1176 | asection *isec, | |
fdba2fcd | 1177 | enum _stub_type stub_type, |
47f6dab9 AM |
1178 | struct elf_link_hash_entry *h, |
1179 | const Elf_Internal_Rela *irela, | |
1180 | bfd_vma dest, | |
1181 | asection *dest_sec) | |
1182 | { | |
cd4a7468 AM |
1183 | struct spu_link_hash_table *htab = spu_hash_table (info); |
1184 | unsigned int ovl, dest_ovl, set_id; | |
47f6dab9 AM |
1185 | struct got_entry *g, **head; |
1186 | asection *sec; | |
cd4a7468 AM |
1187 | bfd_vma addend, from, to, br_dest, patt; |
1188 | unsigned int lrlive; | |
47f6dab9 AM |
1189 | |
1190 | ovl = 0; | |
fdba2fcd | 1191 | if (stub_type != nonovl_stub) |
47f6dab9 AM |
1192 | ovl = spu_elf_section_data (isec->output_section)->u.o.ovl_index; |
1193 | ||
1194 | if (h != NULL) | |
1195 | head = &h->got.glist; | |
1196 | else | |
1197 | head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info); | |
1198 | ||
4a628337 AM |
1199 | addend = 0; |
1200 | if (irela != NULL) | |
1201 | addend = irela->r_addend; | |
47f6dab9 | 1202 | |
cd4a7468 AM |
1203 | if (htab->params->ovly_flavour == ovly_soft_icache) |
1204 | { | |
1205 | g = bfd_malloc (sizeof *g); | |
1206 | if (g == NULL) | |
1207 | return FALSE; | |
1208 | g->ovl = ovl; | |
1209 | g->br_addr = 0; | |
1210 | if (irela != NULL) | |
1211 | g->br_addr = (irela->r_offset | |
1212 | + isec->output_offset | |
1213 | + isec->output_section->vma); | |
1214 | g->next = *head; | |
1215 | *head = g; | |
1216 | } | |
1217 | else | |
1218 | { | |
1219 | for (g = *head; g != NULL; g = g->next) | |
1220 | if (g->addend == addend && (g->ovl == ovl || g->ovl == 0)) | |
1221 | break; | |
1222 | if (g == NULL) | |
1223 | abort (); | |
47f6dab9 | 1224 | |
cd4a7468 AM |
1225 | if (g->ovl == 0 && ovl != 0) |
1226 | return TRUE; | |
4a628337 | 1227 | |
cd4a7468 AM |
1228 | if (g->stub_addr != (bfd_vma) -1) |
1229 | return TRUE; | |
1230 | } | |
47f6dab9 AM |
1231 | |
1232 | sec = htab->stub_sec[ovl]; | |
1233 | dest += dest_sec->output_offset + dest_sec->output_section->vma; | |
1234 | from = sec->size + sec->output_offset + sec->output_section->vma; | |
1235 | g->stub_addr = from; | |
a7e11ee1 AM |
1236 | to = (htab->ovly_entry[0]->root.u.def.value |
1237 | + htab->ovly_entry[0]->root.u.def.section->output_offset | |
1238 | + htab->ovly_entry[0]->root.u.def.section->output_section->vma); | |
64615358 AM |
1239 | |
1240 | if (((dest | to | from) & 3) != 0) | |
47f6dab9 AM |
1241 | { |
1242 | htab->stub_err = 1; | |
1243 | return FALSE; | |
1244 | } | |
64615358 | 1245 | dest_ovl = spu_elf_section_data (dest_sec->output_section)->u.o.ovl_index; |
47f6dab9 | 1246 | |
a7e11ee1 AM |
1247 | if (htab->params->ovly_flavour == ovly_normal |
1248 | && !htab->params->compact_stub) | |
47f6dab9 | 1249 | { |
64615358 | 1250 | bfd_put_32 (sec->owner, ILA + ((dest_ovl << 7) & 0x01ffff80) + 78, |
47f6dab9 AM |
1251 | sec->contents + sec->size); |
1252 | bfd_put_32 (sec->owner, LNOP, | |
1253 | sec->contents + sec->size + 4); | |
1254 | bfd_put_32 (sec->owner, ILA + ((dest << 7) & 0x01ffff80) + 79, | |
1255 | sec->contents + sec->size + 8); | |
cd4a7468 AM |
1256 | if (!BRA_STUBS) |
1257 | bfd_put_32 (sec->owner, BR + (((to - (from + 12)) << 5) & 0x007fff80), | |
1258 | sec->contents + sec->size + 12); | |
1259 | else | |
1260 | bfd_put_32 (sec->owner, BRA + ((to << 5) & 0x007fff80), | |
1261 | sec->contents + sec->size + 12); | |
a7e11ee1 AM |
1262 | } |
1263 | else if (htab->params->ovly_flavour == ovly_normal | |
1264 | && htab->params->compact_stub) | |
1265 | { | |
cd4a7468 AM |
1266 | if (!BRA_STUBS) |
1267 | bfd_put_32 (sec->owner, BRSL + (((to - from) << 5) & 0x007fff80) + 75, | |
1268 | sec->contents + sec->size); | |
1269 | else | |
1270 | bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75, | |
1271 | sec->contents + sec->size); | |
64615358 | 1272 | bfd_put_32 (sec->owner, (dest & 0x3ffff) | (dest_ovl << 18), |
47f6dab9 | 1273 | sec->contents + sec->size + 4); |
a7e11ee1 | 1274 | } |
cdc83fba UW |
1275 | else if (htab->params->ovly_flavour == ovly_soft_icache |
1276 | && htab->params->compact_stub) | |
a7e11ee1 | 1277 | { |
cd4a7468 AM |
1278 | lrlive = 0; |
1279 | if (stub_type == nonovl_stub) | |
1280 | ; | |
1281 | else if (stub_type == call_ovl_stub) | |
1282 | /* A brsl makes lr live and *(*sp+16) is live. | |
1283 | Tail calls have the same liveness. */ | |
1284 | lrlive = 5; | |
1285 | else if (!htab->params->lrlive_analysis) | |
1286 | /* Assume stack frame and lr save. */ | |
1287 | lrlive = 1; | |
1288 | else if (irela != NULL) | |
1289 | { | |
1290 | /* Analyse branch instructions. */ | |
1291 | struct function_info *caller; | |
1292 | bfd_vma off; | |
1293 | ||
1294 | caller = find_function (isec, irela->r_offset, info); | |
1295 | if (caller->start == NULL) | |
1296 | off = irela->r_offset; | |
1297 | else | |
1298 | { | |
1299 | struct function_info *found = NULL; | |
1300 | ||
1301 | /* Find the earliest piece of this function that | |
1302 | has frame adjusting instructions. We might | |
1303 | see dynamic frame adjustment (eg. for alloca) | |
1304 | in some later piece, but functions using | |
1305 | alloca always set up a frame earlier. Frame | |
1306 | setup instructions are always in one piece. */ | |
1307 | if (caller->lr_store != (bfd_vma) -1 | |
1308 | || caller->sp_adjust != (bfd_vma) -1) | |
1309 | found = caller; | |
1310 | while (caller->start != NULL) | |
1311 | { | |
1312 | caller = caller->start; | |
1313 | if (caller->lr_store != (bfd_vma) -1 | |
1314 | || caller->sp_adjust != (bfd_vma) -1) | |
1315 | found = caller; | |
1316 | } | |
1317 | if (found != NULL) | |
1318 | caller = found; | |
1319 | off = (bfd_vma) -1; | |
1320 | } | |
1321 | ||
1322 | if (off > caller->sp_adjust) | |
1323 | { | |
1324 | if (off > caller->lr_store) | |
1325 | /* Only *(*sp+16) is live. */ | |
1326 | lrlive = 1; | |
1327 | else | |
1328 | /* If no lr save, then we must be in a | |
1329 | leaf function with a frame. | |
1330 | lr is still live. */ | |
1331 | lrlive = 4; | |
1332 | } | |
1333 | else if (off > caller->lr_store) | |
1334 | { | |
1335 | /* Between lr save and stack adjust. */ | |
1336 | lrlive = 3; | |
1337 | /* This should never happen since prologues won't | |
1338 | be split here. */ | |
1339 | BFD_ASSERT (0); | |
1340 | } | |
1341 | else | |
1342 | /* On entry to function. */ | |
1343 | lrlive = 5; | |
1344 | ||
1345 | if (stub_type != br000_ovl_stub | |
1346 | && lrlive != stub_type - br000_ovl_stub) | |
1347 | info->callbacks->einfo (_("%A:0x%v lrlive .brinfo (%u) differs " | |
1348 | "from analysis (%u)\n"), | |
1349 | isec, irela->r_offset, lrlive, | |
1350 | stub_type - br000_ovl_stub); | |
1351 | } | |
1352 | ||
1353 | /* If given lrlive info via .brinfo, use it. */ | |
1354 | if (stub_type > br000_ovl_stub) | |
1355 | lrlive = stub_type - br000_ovl_stub; | |
1356 | ||
a7e11ee1 AM |
1357 | if (ovl == 0) |
1358 | to = (htab->ovly_entry[1]->root.u.def.value | |
1359 | + htab->ovly_entry[1]->root.u.def.section->output_offset | |
1360 | + htab->ovly_entry[1]->root.u.def.section->output_section->vma); | |
1361 | ||
cdc83fba UW |
1362 | /* The branch that uses this stub goes to stub_addr + 4. We'll |
1363 | set up an xor pattern that can be used by the icache manager | |
1364 | to modify this branch to go directly to its destination. */ | |
1365 | g->stub_addr += 4; | |
1366 | br_dest = g->stub_addr; | |
1367 | if (irela == NULL) | |
a7e11ee1 | 1368 | { |
cdc83fba UW |
1369 | /* Except in the case of _SPUEAR_ stubs, the branch in |
1370 | question is the one in the stub itself. */ | |
1371 | BFD_ASSERT (stub_type == nonovl_stub); | |
1372 | g->br_addr = g->stub_addr; | |
1373 | br_dest = to; | |
a7e11ee1 | 1374 | } |
a7e11ee1 | 1375 | |
cdc83fba UW |
1376 | set_id = ((dest_ovl - 1) >> htab->num_lines_log2) + 1; |
1377 | bfd_put_32 (sec->owner, (set_id << 18) | (dest & 0x3ffff), | |
1378 | sec->contents + sec->size); | |
1379 | bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75, | |
1380 | sec->contents + sec->size + 4); | |
1381 | bfd_put_32 (sec->owner, (lrlive << 29) | (g->br_addr & 0x3ffff), | |
1382 | sec->contents + sec->size + 8); | |
1383 | patt = dest ^ br_dest; | |
1384 | if (irela != NULL && ELF32_R_TYPE (irela->r_info) == R_SPU_REL16) | |
1385 | patt = (dest - g->br_addr) ^ (br_dest - g->br_addr); | |
1386 | bfd_put_32 (sec->owner, (patt << 5) & 0x007fff80, | |
1387 | sec->contents + sec->size + 12); | |
cd4a7468 | 1388 | |
cd4a7468 AM |
1389 | if (ovl == 0) |
1390 | /* Extra space for linked list entries. */ | |
1391 | sec->size += 16; | |
47f6dab9 | 1392 | } |
a7e11ee1 AM |
1393 | else |
1394 | abort (); | |
1395 | ||
1396 | sec->size += ovl_stub_size (htab->params); | |
47f6dab9 | 1397 | |
64615358 | 1398 | if (htab->params->emit_stub_syms) |
47f6dab9 AM |
1399 | { |
1400 | size_t len; | |
1401 | char *name; | |
1402 | int add; | |
1403 | ||
1404 | len = 8 + sizeof (".ovl_call.") - 1; | |
1405 | if (h != NULL) | |
1406 | len += strlen (h->root.root.string); | |
1407 | else | |
1408 | len += 8 + 1 + 8; | |
1409 | add = 0; | |
1410 | if (irela != NULL) | |
1411 | add = (int) irela->r_addend & 0xffffffff; | |
1412 | if (add != 0) | |
1413 | len += 1 + 8; | |
1414 | name = bfd_malloc (len); | |
1415 | if (name == NULL) | |
1416 | return FALSE; | |
1417 | ||
1418 | sprintf (name, "%08x.ovl_call.", g->ovl); | |
1419 | if (h != NULL) | |
1420 | strcpy (name + 8 + sizeof (".ovl_call.") - 1, h->root.root.string); | |
1421 | else | |
1422 | sprintf (name + 8 + sizeof (".ovl_call.") - 1, "%x:%x", | |
1423 | dest_sec->id & 0xffffffff, | |
1424 | (int) ELF32_R_SYM (irela->r_info) & 0xffffffff); | |
1425 | if (add != 0) | |
1426 | sprintf (name + len - 9, "+%x", add); | |
1427 | ||
1428 | h = elf_link_hash_lookup (&htab->elf, name, TRUE, TRUE, FALSE); | |
1429 | free (name); | |
1430 | if (h == NULL) | |
1431 | return FALSE; | |
1432 | if (h->root.type == bfd_link_hash_new) | |
1433 | { | |
1434 | h->root.type = bfd_link_hash_defined; | |
1435 | h->root.u.def.section = sec; | |
a7e11ee1 | 1436 | h->size = ovl_stub_size (htab->params); |
64615358 | 1437 | h->root.u.def.value = sec->size - h->size; |
47f6dab9 AM |
1438 | h->type = STT_FUNC; |
1439 | h->ref_regular = 1; | |
1440 | h->def_regular = 1; | |
1441 | h->ref_regular_nonweak = 1; | |
1442 | h->forced_local = 1; | |
1443 | h->non_elf = 0; | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | return TRUE; | |
1448 | } | |
1449 | ||
f4b39977 AM |
1450 | /* Called via elf_link_hash_traverse to allocate stubs for any _SPUEAR_ |
1451 | symbols. */ | |
1452 | ||
1453 | static bfd_boolean | |
1454 | allocate_spuear_stubs (struct elf_link_hash_entry *h, void *inf) | |
1455 | { | |
1456 | /* Symbols starting with _SPUEAR_ need a stub because they may be | |
1457 | invoked by the PPU. */ | |
380814a6 AM |
1458 | struct bfd_link_info *info = inf; |
1459 | struct spu_link_hash_table *htab = spu_hash_table (info); | |
1460 | asection *sym_sec; | |
1461 | ||
f4b39977 AM |
1462 | if ((h->root.type == bfd_link_hash_defined |
1463 | || h->root.type == bfd_link_hash_defweak) | |
1464 | && h->def_regular | |
380814a6 AM |
1465 | && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0 |
1466 | && (sym_sec = h->root.u.def.section) != NULL | |
64615358 | 1467 | && sym_sec->output_section != bfd_abs_section_ptr |
380814a6 AM |
1468 | && spu_elf_section_data (sym_sec->output_section) != NULL |
1469 | && (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index != 0 | |
64615358 | 1470 | || htab->params->non_overlay_stubs)) |
f4b39977 | 1471 | { |
f3c29e8a | 1472 | return count_stub (htab, NULL, NULL, nonovl_stub, h, NULL); |
f4b39977 AM |
1473 | } |
1474 | ||
1475 | return TRUE; | |
1476 | } | |
1477 | ||
e9f53129 | 1478 | static bfd_boolean |
47f6dab9 | 1479 | build_spuear_stubs (struct elf_link_hash_entry *h, void *inf) |
e9f53129 | 1480 | { |
47f6dab9 AM |
1481 | /* Symbols starting with _SPUEAR_ need a stub because they may be |
1482 | invoked by the PPU. */ | |
380814a6 AM |
1483 | struct bfd_link_info *info = inf; |
1484 | struct spu_link_hash_table *htab = spu_hash_table (info); | |
1485 | asection *sym_sec; | |
1486 | ||
47f6dab9 AM |
1487 | if ((h->root.type == bfd_link_hash_defined |
1488 | || h->root.type == bfd_link_hash_defweak) | |
1489 | && h->def_regular | |
380814a6 AM |
1490 | && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0 |
1491 | && (sym_sec = h->root.u.def.section) != NULL | |
64615358 | 1492 | && sym_sec->output_section != bfd_abs_section_ptr |
380814a6 AM |
1493 | && spu_elf_section_data (sym_sec->output_section) != NULL |
1494 | && (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index != 0 | |
64615358 | 1495 | || htab->params->non_overlay_stubs)) |
47f6dab9 | 1496 | { |
cd4a7468 | 1497 | return build_stub (info, NULL, NULL, nonovl_stub, h, NULL, |
f3c29e8a | 1498 | h->root.u.def.value, sym_sec); |
47f6dab9 AM |
1499 | } |
1500 | ||
e9f53129 AM |
1501 | return TRUE; |
1502 | } | |
1503 | ||
47f6dab9 | 1504 | /* Size or build stubs. */ |
e9f53129 | 1505 | |
47f6dab9 | 1506 | static bfd_boolean |
c65be8d7 | 1507 | process_stubs (struct bfd_link_info *info, bfd_boolean build) |
e9f53129 AM |
1508 | { |
1509 | struct spu_link_hash_table *htab = spu_hash_table (info); | |
1510 | bfd *ibfd; | |
e9f53129 | 1511 | |
e9f53129 AM |
1512 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) |
1513 | { | |
1514 | extern const bfd_target bfd_elf32_spu_vec; | |
1515 | Elf_Internal_Shdr *symtab_hdr; | |
47f6dab9 | 1516 | asection *isec; |
e9f53129 AM |
1517 | Elf_Internal_Sym *local_syms = NULL; |
1518 | ||
1519 | if (ibfd->xvec != &bfd_elf32_spu_vec) | |
1520 | continue; | |
1521 | ||
1522 | /* We'll need the symbol table in a second. */ | |
1523 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
1524 | if (symtab_hdr->sh_info == 0) | |
1525 | continue; | |
1526 | ||
1527 | /* Walk over each section attached to the input bfd. */ | |
47f6dab9 | 1528 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
e9f53129 AM |
1529 | { |
1530 | Elf_Internal_Rela *internal_relocs, *irelaend, *irela; | |
1531 | ||
1532 | /* If there aren't any relocs, then there's nothing more to do. */ | |
47f6dab9 | 1533 | if ((isec->flags & SEC_RELOC) == 0 |
47f6dab9 | 1534 | || isec->reloc_count == 0) |
e9f53129 AM |
1535 | continue; |
1536 | ||
64615358 | 1537 | if (!maybe_needs_stubs (isec)) |
e9f53129 AM |
1538 | continue; |
1539 | ||
1540 | /* Get the relocs. */ | |
47f6dab9 AM |
1541 | internal_relocs = _bfd_elf_link_read_relocs (ibfd, isec, NULL, NULL, |
1542 | info->keep_memory); | |
e9f53129 AM |
1543 | if (internal_relocs == NULL) |
1544 | goto error_ret_free_local; | |
1545 | ||
1546 | /* Now examine each relocation. */ | |
1547 | irela = internal_relocs; | |
47f6dab9 | 1548 | irelaend = irela + isec->reloc_count; |
e9f53129 AM |
1549 | for (; irela < irelaend; irela++) |
1550 | { | |
1551 | enum elf_spu_reloc_type r_type; | |
1552 | unsigned int r_indx; | |
1553 | asection *sym_sec; | |
1554 | Elf_Internal_Sym *sym; | |
1555 | struct elf_link_hash_entry *h; | |
fdba2fcd | 1556 | enum _stub_type stub_type; |
e9f53129 AM |
1557 | |
1558 | r_type = ELF32_R_TYPE (irela->r_info); | |
1559 | r_indx = ELF32_R_SYM (irela->r_info); | |
1560 | ||
1561 | if (r_type >= R_SPU_max) | |
1562 | { | |
1563 | bfd_set_error (bfd_error_bad_value); | |
47f6dab9 AM |
1564 | error_ret_free_internal: |
1565 | if (elf_section_data (isec)->relocs != internal_relocs) | |
1566 | free (internal_relocs); | |
1567 | error_ret_free_local: | |
1568 | if (local_syms != NULL | |
1569 | && (symtab_hdr->contents | |
1570 | != (unsigned char *) local_syms)) | |
1571 | free (local_syms); | |
1572 | return FALSE; | |
e9f53129 AM |
1573 | } |
1574 | ||
1575 | /* Determine the reloc target section. */ | |
1f27ab8d | 1576 | if (!get_sym_h (&h, &sym, &sym_sec, &local_syms, r_indx, ibfd)) |
e9f53129 AM |
1577 | goto error_ret_free_internal; |
1578 | ||
fdba2fcd AM |
1579 | stub_type = needs_ovl_stub (h, sym, sym_sec, isec, irela, |
1580 | NULL, info); | |
1581 | if (stub_type == no_stub) | |
e9f53129 | 1582 | continue; |
fdba2fcd AM |
1583 | else if (stub_type == stub_error) |
1584 | goto error_ret_free_internal; | |
e9f53129 | 1585 | |
47f6dab9 | 1586 | if (htab->stub_count == NULL) |
e9f53129 | 1587 | { |
47f6dab9 AM |
1588 | bfd_size_type amt; |
1589 | amt = (htab->num_overlays + 1) * sizeof (*htab->stub_count); | |
1590 | htab->stub_count = bfd_zmalloc (amt); | |
1591 | if (htab->stub_count == NULL) | |
1592 | goto error_ret_free_internal; | |
e9f53129 AM |
1593 | } |
1594 | ||
47f6dab9 | 1595 | if (!build) |
e9f53129 | 1596 | { |
fdba2fcd | 1597 | if (!count_stub (htab, ibfd, isec, stub_type, h, irela)) |
47f6dab9 | 1598 | goto error_ret_free_internal; |
e9f53129 | 1599 | } |
e9f53129 | 1600 | else |
47f6dab9 AM |
1601 | { |
1602 | bfd_vma dest; | |
1603 | ||
1604 | if (h != NULL) | |
1605 | dest = h->root.u.def.value; | |
1606 | else | |
1607 | dest = sym->st_value; | |
4a628337 | 1608 | dest += irela->r_addend; |
cd4a7468 | 1609 | if (!build_stub (info, ibfd, isec, stub_type, h, irela, |
47f6dab9 AM |
1610 | dest, sym_sec)) |
1611 | goto error_ret_free_internal; | |
1612 | } | |
e9f53129 AM |
1613 | } |
1614 | ||
1615 | /* We're done with the internal relocs, free them. */ | |
47f6dab9 | 1616 | if (elf_section_data (isec)->relocs != internal_relocs) |
e9f53129 AM |
1617 | free (internal_relocs); |
1618 | } | |
1619 | ||
1620 | if (local_syms != NULL | |
1621 | && symtab_hdr->contents != (unsigned char *) local_syms) | |
1622 | { | |
1623 | if (!info->keep_memory) | |
1624 | free (local_syms); | |
1625 | else | |
1626 | symtab_hdr->contents = (unsigned char *) local_syms; | |
1627 | } | |
1628 | } | |
1629 | ||
47f6dab9 AM |
1630 | return TRUE; |
1631 | } | |
1632 | ||
a7e11ee1 AM |
1633 | /* Allocate space for overlay call and return stubs. |
1634 | Return 0 on error, 1 if no stubs, 2 otherwise. */ | |
47f6dab9 AM |
1635 | |
1636 | int | |
64615358 | 1637 | spu_elf_size_stubs (struct bfd_link_info *info) |
47f6dab9 | 1638 | { |
64615358 | 1639 | struct spu_link_hash_table *htab; |
47f6dab9 AM |
1640 | bfd *ibfd; |
1641 | bfd_size_type amt; | |
1642 | flagword flags; | |
1643 | unsigned int i; | |
1644 | asection *stub; | |
1645 | ||
c65be8d7 | 1646 | if (!process_stubs (info, FALSE)) |
47f6dab9 AM |
1647 | return 0; |
1648 | ||
64615358 | 1649 | htab = spu_hash_table (info); |
380814a6 | 1650 | elf_link_hash_traverse (&htab->elf, allocate_spuear_stubs, info); |
47f6dab9 AM |
1651 | if (htab->stub_err) |
1652 | return 0; | |
f4b39977 | 1653 | |
47f6dab9 AM |
1654 | if (htab->stub_count == NULL) |
1655 | return 1; | |
e9f53129 AM |
1656 | |
1657 | ibfd = info->input_bfds; | |
47f6dab9 AM |
1658 | amt = (htab->num_overlays + 1) * sizeof (*htab->stub_sec); |
1659 | htab->stub_sec = bfd_zmalloc (amt); | |
1660 | if (htab->stub_sec == NULL) | |
1661 | return 0; | |
e9f53129 | 1662 | |
47f6dab9 | 1663 | flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY |
e9f53129 | 1664 | | SEC_HAS_CONTENTS | SEC_IN_MEMORY); |
47f6dab9 AM |
1665 | stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags); |
1666 | htab->stub_sec[0] = stub; | |
1667 | if (stub == NULL | |
64615358 | 1668 | || !bfd_set_section_alignment (ibfd, stub, |
a7e11ee1 | 1669 | ovl_stub_size_log2 (htab->params))) |
47f6dab9 | 1670 | return 0; |
a7e11ee1 | 1671 | stub->size = htab->stub_count[0] * ovl_stub_size (htab->params); |
cd4a7468 AM |
1672 | if (htab->params->ovly_flavour == ovly_soft_icache) |
1673 | /* Extra space for linked list entries. */ | |
1674 | stub->size += htab->stub_count[0] * 16; | |
e9f53129 | 1675 | |
47f6dab9 | 1676 | for (i = 0; i < htab->num_overlays; ++i) |
e9f53129 | 1677 | { |
47f6dab9 AM |
1678 | asection *osec = htab->ovl_sec[i]; |
1679 | unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index; | |
1680 | stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags); | |
1681 | htab->stub_sec[ovl] = stub; | |
1682 | if (stub == NULL | |
64615358 | 1683 | || !bfd_set_section_alignment (ibfd, stub, |
a7e11ee1 | 1684 | ovl_stub_size_log2 (htab->params))) |
47f6dab9 | 1685 | return 0; |
a7e11ee1 | 1686 | stub->size = htab->stub_count[ovl] * ovl_stub_size (htab->params); |
e9f53129 | 1687 | } |
e9f53129 | 1688 | |
cd4a7468 AM |
1689 | if (htab->params->ovly_flavour == ovly_soft_icache) |
1690 | { | |
1691 | /* Space for icache manager tables. | |
1692 | a) Tag array, one quadword per cache line. | |
97fc8d84 UW |
1693 | b) Rewrite "to" list, one quadword per cache line. |
1694 | c) Rewrite "from" list, one byte per outgoing branch (rounded up to | |
1695 | a power-of-two number of full quadwords) per cache line. */ | |
1696 | ||
1697 | flags = SEC_ALLOC; | |
1698 | htab->ovtab = bfd_make_section_anyway_with_flags (ibfd, ".ovtab", flags); | |
1699 | if (htab->ovtab == NULL | |
1700 | || !bfd_set_section_alignment (ibfd, htab->ovtab, 4)) | |
1701 | return 0; | |
1702 | ||
1703 | htab->ovtab->size = (16 + 16 + (16 << htab->fromelem_size_log2)) | |
1704 | << htab->num_lines_log2; | |
cd4a7468 | 1705 | |
97fc8d84 | 1706 | flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; |
cd4a7468 AM |
1707 | htab->init = bfd_make_section_anyway_with_flags (ibfd, ".ovini", flags); |
1708 | if (htab->init == NULL | |
1709 | || !bfd_set_section_alignment (ibfd, htab->init, 4)) | |
1710 | return 0; | |
1711 | ||
1712 | htab->init->size = 16; | |
cd4a7468 AM |
1713 | } |
1714 | else | |
1715 | { | |
1716 | /* htab->ovtab consists of two arrays. | |
1717 | . struct { | |
1718 | . u32 vma; | |
1719 | . u32 size; | |
1720 | . u32 file_off; | |
1721 | . u32 buf; | |
1722 | . } _ovly_table[]; | |
1723 | . | |
1724 | . struct { | |
1725 | . u32 mapped; | |
1726 | . } _ovly_buf_table[]; | |
1727 | . */ | |
1728 | ||
97fc8d84 UW |
1729 | flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; |
1730 | htab->ovtab = bfd_make_section_anyway_with_flags (ibfd, ".ovtab", flags); | |
1731 | if (htab->ovtab == NULL | |
1732 | || !bfd_set_section_alignment (ibfd, htab->ovtab, 4)) | |
1733 | return 0; | |
1734 | ||
cd4a7468 AM |
1735 | htab->ovtab->size = htab->num_overlays * 16 + 16 + htab->num_buf * 4; |
1736 | } | |
47f6dab9 AM |
1737 | |
1738 | htab->toe = bfd_make_section_anyway_with_flags (ibfd, ".toe", SEC_ALLOC); | |
1739 | if (htab->toe == NULL | |
1740 | || !bfd_set_section_alignment (ibfd, htab->toe, 4)) | |
1741 | return 0; | |
c1ea05bb | 1742 | htab->toe->size = 16; |
47f6dab9 AM |
1743 | |
1744 | return 2; | |
e9f53129 AM |
1745 | } |
1746 | ||
bbb0fc04 AM |
1747 | /* Called from ld to place overlay manager data sections. This is done |
1748 | after the overlay manager itself is loaded, mainly so that the | |
1749 | linker's htab->init section is placed after any other .ovl.init | |
1750 | sections. */ | |
1751 | ||
1752 | void | |
1753 | spu_elf_place_overlay_data (struct bfd_link_info *info) | |
1754 | { | |
1755 | struct spu_link_hash_table *htab = spu_hash_table (info); | |
1756 | unsigned int i; | |
1757 | const char *ovout; | |
1758 | ||
1759 | if (htab->stub_count == NULL) | |
1760 | return; | |
1761 | ||
1762 | (*htab->params->place_spu_section) (htab->stub_sec[0], NULL, ".text"); | |
1763 | ||
1764 | for (i = 0; i < htab->num_overlays; ++i) | |
1765 | { | |
1766 | asection *osec = htab->ovl_sec[i]; | |
1767 | unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index; | |
1768 | (*htab->params->place_spu_section) (htab->stub_sec[ovl], osec, NULL); | |
1769 | } | |
1770 | ||
1771 | if (htab->params->ovly_flavour == ovly_soft_icache) | |
1772 | (*htab->params->place_spu_section) (htab->init, NULL, ".ovl.init"); | |
1773 | ||
1774 | ovout = ".data"; | |
1775 | if (htab->params->ovly_flavour == ovly_soft_icache) | |
97fc8d84 | 1776 | ovout = ".bss"; |
bbb0fc04 AM |
1777 | (*htab->params->place_spu_section) (htab->ovtab, NULL, ovout); |
1778 | ||
1779 | (*htab->params->place_spu_section) (htab->toe, NULL, ".toe"); | |
1780 | } | |
1781 | ||
e9f53129 AM |
1782 | /* Functions to handle embedded spu_ovl.o object. */ |
1783 | ||
1784 | static void * | |
1785 | ovl_mgr_open (struct bfd *nbfd ATTRIBUTE_UNUSED, void *stream) | |
1786 | { | |
1787 | return stream; | |
1788 | } | |
1789 | ||
1790 | static file_ptr | |
1791 | ovl_mgr_pread (struct bfd *abfd ATTRIBUTE_UNUSED, | |
1792 | void *stream, | |
1793 | void *buf, | |
1794 | file_ptr nbytes, | |
1795 | file_ptr offset) | |
1796 | { | |
1797 | struct _ovl_stream *os; | |
1798 | size_t count; | |
1799 | size_t max; | |
1800 | ||
1801 | os = (struct _ovl_stream *) stream; | |
7a8757b3 | 1802 | max = (const char *) os->end - (const char *) os->start; |
e9f53129 AM |
1803 | |
1804 | if ((ufile_ptr) offset >= max) | |
1805 | return 0; | |
1806 | ||
1807 | count = nbytes; | |
1808 | if (count > max - offset) | |
1809 | count = max - offset; | |
1810 | ||
7a8757b3 | 1811 | memcpy (buf, (const char *) os->start + offset, count); |
e9f53129 AM |
1812 | return count; |
1813 | } | |
1814 | ||
1815 | bfd_boolean | |
1816 | spu_elf_open_builtin_lib (bfd **ovl_bfd, const struct _ovl_stream *stream) | |
1817 | { | |
1818 | *ovl_bfd = bfd_openr_iovec ("builtin ovl_mgr", | |
1819 | "elf32-spu", | |
1820 | ovl_mgr_open, | |
1821 | (void *) stream, | |
1822 | ovl_mgr_pread, | |
f6cf9273 | 1823 | NULL, |
e9f53129 AM |
1824 | NULL); |
1825 | return *ovl_bfd != NULL; | |
1826 | } | |
1827 | ||
cd4a7468 AM |
1828 | static unsigned int |
1829 | overlay_index (asection *sec) | |
1830 | { | |
1831 | if (sec == NULL | |
1832 | || sec->output_section == bfd_abs_section_ptr) | |
1833 | return 0; | |
1834 | return spu_elf_section_data (sec->output_section)->u.o.ovl_index; | |
1835 | } | |
1836 | ||
e9f53129 AM |
1837 | /* Define an STT_OBJECT symbol. */ |
1838 | ||
1839 | static struct elf_link_hash_entry * | |
1840 | define_ovtab_symbol (struct spu_link_hash_table *htab, const char *name) | |
1841 | { | |
1842 | struct elf_link_hash_entry *h; | |
1843 | ||
1844 | h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE); | |
1845 | if (h == NULL) | |
1846 | return NULL; | |
1847 | ||
1848 | if (h->root.type != bfd_link_hash_defined | |
1849 | || !h->def_regular) | |
1850 | { | |
1851 | h->root.type = bfd_link_hash_defined; | |
1852 | h->root.u.def.section = htab->ovtab; | |
1853 | h->type = STT_OBJECT; | |
1854 | h->ref_regular = 1; | |
1855 | h->def_regular = 1; | |
1856 | h->ref_regular_nonweak = 1; | |
1857 | h->non_elf = 0; | |
1858 | } | |
b0c41709 | 1859 | else if (h->root.u.def.section->owner != NULL) |
e9f53129 AM |
1860 | { |
1861 | (*_bfd_error_handler) (_("%B is not allowed to define %s"), | |
1862 | h->root.u.def.section->owner, | |
1863 | h->root.root.string); | |
1864 | bfd_set_error (bfd_error_bad_value); | |
1865 | return NULL; | |
1866 | } | |
b0c41709 AM |
1867 | else |
1868 | { | |
1869 | (*_bfd_error_handler) (_("you are not allowed to define %s in a script"), | |
1870 | h->root.root.string); | |
1871 | bfd_set_error (bfd_error_bad_value); | |
1872 | return NULL; | |
1873 | } | |
e9f53129 AM |
1874 | |
1875 | return h; | |
1876 | } | |
1877 | ||
1878 | /* Fill in all stubs and the overlay tables. */ | |
1879 | ||
cd4a7468 | 1880 | static bfd_boolean |
64615358 | 1881 | spu_elf_build_stubs (struct bfd_link_info *info) |
e9f53129 AM |
1882 | { |
1883 | struct spu_link_hash_table *htab = spu_hash_table (info); | |
1884 | struct elf_link_hash_entry *h; | |
1885 | bfd_byte *p; | |
1886 | asection *s; | |
1887 | bfd *obfd; | |
1888 | unsigned int i; | |
1889 | ||
47f6dab9 AM |
1890 | if (htab->stub_count == NULL) |
1891 | return TRUE; | |
1892 | ||
1893 | for (i = 0; i <= htab->num_overlays; i++) | |
1894 | if (htab->stub_sec[i]->size != 0) | |
1895 | { | |
1896 | htab->stub_sec[i]->contents = bfd_zalloc (htab->stub_sec[i]->owner, | |
1897 | htab->stub_sec[i]->size); | |
1898 | if (htab->stub_sec[i]->contents == NULL) | |
1899 | return FALSE; | |
1900 | htab->stub_sec[i]->rawsize = htab->stub_sec[i]->size; | |
1901 | htab->stub_sec[i]->size = 0; | |
1902 | } | |
e9f53129 | 1903 | |
ad2adbc8 | 1904 | for (i = 0; i < 2; i++) |
cd4a7468 | 1905 | { |
ad2adbc8 AM |
1906 | h = htab->ovly_entry[i]; |
1907 | BFD_ASSERT (h != NULL); | |
a7e11ee1 | 1908 | |
ad2adbc8 AM |
1909 | if ((h->root.type == bfd_link_hash_defined |
1910 | || h->root.type == bfd_link_hash_defweak) | |
1911 | && h->def_regular) | |
1912 | { | |
1913 | s = h->root.u.def.section->output_section; | |
1914 | if (spu_elf_section_data (s)->u.o.ovl_index) | |
1915 | { | |
1916 | (*_bfd_error_handler) (_("%s in overlay section"), | |
1917 | h->root.root.string); | |
1918 | bfd_set_error (bfd_error_bad_value); | |
1919 | return FALSE; | |
1920 | } | |
1921 | } | |
1922 | else | |
1923 | BFD_ASSERT (0); | |
cd4a7468 | 1924 | } |
47f6dab9 | 1925 | |
c65be8d7 AM |
1926 | /* Fill in all the stubs. */ |
1927 | process_stubs (info, TRUE); | |
f3c29e8a AM |
1928 | if (!htab->stub_err) |
1929 | elf_link_hash_traverse (&htab->elf, build_spuear_stubs, info); | |
47f6dab9 | 1930 | |
47f6dab9 | 1931 | if (htab->stub_err) |
f3c29e8a AM |
1932 | { |
1933 | (*_bfd_error_handler) (_("overlay stub relocation overflow")); | |
1934 | bfd_set_error (bfd_error_bad_value); | |
1935 | return FALSE; | |
1936 | } | |
e9f53129 | 1937 | |
47f6dab9 AM |
1938 | for (i = 0; i <= htab->num_overlays; i++) |
1939 | { | |
1940 | if (htab->stub_sec[i]->size != htab->stub_sec[i]->rawsize) | |
1941 | { | |
1942 | (*_bfd_error_handler) (_("stubs don't match calculated size")); | |
1943 | bfd_set_error (bfd_error_bad_value); | |
1944 | return FALSE; | |
1945 | } | |
1946 | htab->stub_sec[i]->rawsize = 0; | |
1947 | } | |
1948 | ||
cd4a7468 AM |
1949 | if (htab->ovtab == NULL || htab->ovtab->size == 0) |
1950 | return TRUE; | |
1951 | ||
e9f53129 AM |
1952 | htab->ovtab->contents = bfd_zalloc (htab->ovtab->owner, htab->ovtab->size); |
1953 | if (htab->ovtab->contents == NULL) | |
1954 | return FALSE; | |
1955 | ||
e9f53129 | 1956 | p = htab->ovtab->contents; |
cd4a7468 | 1957 | if (htab->params->ovly_flavour == ovly_soft_icache) |
e9f53129 | 1958 | { |
97fc8d84 | 1959 | bfd_vma off; |
cd4a7468 | 1960 | |
77ae44b0 | 1961 | h = define_ovtab_symbol (htab, "__icache_tag_array"); |
cd4a7468 AM |
1962 | if (h == NULL) |
1963 | return FALSE; | |
1964 | h->root.u.def.value = 0; | |
1965 | h->size = 16 << htab->num_lines_log2; | |
1966 | off = h->size; | |
77ae44b0 AM |
1967 | |
1968 | h = define_ovtab_symbol (htab, "__icache_tag_array_size"); | |
1969 | if (h == NULL) | |
1970 | return FALSE; | |
1971 | h->root.u.def.value = 16 << htab->num_lines_log2; | |
1972 | h->root.u.def.section = bfd_abs_section_ptr; | |
1973 | ||
97fc8d84 UW |
1974 | h = define_ovtab_symbol (htab, "__icache_rewrite_to"); |
1975 | if (h == NULL) | |
1976 | return FALSE; | |
1977 | h->root.u.def.value = off; | |
1978 | h->size = 16 << htab->num_lines_log2; | |
1979 | off += h->size; | |
1980 | ||
1981 | h = define_ovtab_symbol (htab, "__icache_rewrite_to_size"); | |
1982 | if (h == NULL) | |
1983 | return FALSE; | |
1984 | h->root.u.def.value = 16 << htab->num_lines_log2; | |
1985 | h->root.u.def.section = bfd_abs_section_ptr; | |
cd4a7468 | 1986 | |
97fc8d84 | 1987 | h = define_ovtab_symbol (htab, "__icache_rewrite_from"); |
cd4a7468 AM |
1988 | if (h == NULL) |
1989 | return FALSE; | |
1990 | h->root.u.def.value = off; | |
97fc8d84 | 1991 | h->size = 16 << (htab->fromelem_size_log2 + htab->num_lines_log2); |
cd4a7468 | 1992 | off += h->size; |
97fc8d84 UW |
1993 | |
1994 | h = define_ovtab_symbol (htab, "__icache_rewrite_from_size"); | |
1995 | if (h == NULL) | |
1996 | return FALSE; | |
1997 | h->root.u.def.value = 16 << (htab->fromelem_size_log2 | |
1998 | + htab->num_lines_log2); | |
1999 | h->root.u.def.section = bfd_abs_section_ptr; | |
2000 | ||
2001 | h = define_ovtab_symbol (htab, "__icache_log2_fromelemsize"); | |
2002 | if (h == NULL) | |
2003 | return FALSE; | |
2004 | h->root.u.def.value = htab->fromelem_size_log2; | |
2005 | h->root.u.def.section = bfd_abs_section_ptr; | |
cd4a7468 | 2006 | |
cd4a7468 AM |
2007 | h = define_ovtab_symbol (htab, "__icache_base"); |
2008 | if (h == NULL) | |
2009 | return FALSE; | |
460e679b AM |
2010 | h->root.u.def.value = htab->ovl_sec[0]->vma; |
2011 | h->root.u.def.section = bfd_abs_section_ptr; | |
cd4a7468 | 2012 | h->size = htab->num_buf << htab->line_size_log2; |
e9f53129 | 2013 | |
97fc8d84 UW |
2014 | h = define_ovtab_symbol (htab, "__icache_linesize"); |
2015 | if (h == NULL) | |
2016 | return FALSE; | |
2017 | h->root.u.def.value = 1 << htab->line_size_log2; | |
2018 | h->root.u.def.section = bfd_abs_section_ptr; | |
2019 | ||
2020 | h = define_ovtab_symbol (htab, "__icache_log2_linesize"); | |
2021 | if (h == NULL) | |
2022 | return FALSE; | |
2023 | h->root.u.def.value = htab->line_size_log2; | |
2024 | h->root.u.def.section = bfd_abs_section_ptr; | |
2025 | ||
50e00962 AM |
2026 | h = define_ovtab_symbol (htab, "__icache_neg_log2_linesize"); |
2027 | if (h == NULL) | |
2028 | return FALSE; | |
2029 | h->root.u.def.value = -htab->line_size_log2; | |
2030 | h->root.u.def.section = bfd_abs_section_ptr; | |
2031 | ||
97fc8d84 UW |
2032 | h = define_ovtab_symbol (htab, "__icache_cachesize"); |
2033 | if (h == NULL) | |
2034 | return FALSE; | |
2035 | h->root.u.def.value = 1 << (htab->num_lines_log2 + htab->line_size_log2); | |
2036 | h->root.u.def.section = bfd_abs_section_ptr; | |
2037 | ||
2038 | h = define_ovtab_symbol (htab, "__icache_log2_cachesize"); | |
2039 | if (h == NULL) | |
2040 | return FALSE; | |
2041 | h->root.u.def.value = htab->num_lines_log2 + htab->line_size_log2; | |
2042 | h->root.u.def.section = bfd_abs_section_ptr; | |
2043 | ||
2044 | h = define_ovtab_symbol (htab, "__icache_neg_log2_cachesize"); | |
2045 | if (h == NULL) | |
2046 | return FALSE; | |
2047 | h->root.u.def.value = -(htab->num_lines_log2 + htab->line_size_log2); | |
2048 | h->root.u.def.section = bfd_abs_section_ptr; | |
2049 | ||
cd4a7468 | 2050 | if (htab->init != NULL && htab->init->size != 0) |
e9f53129 | 2051 | { |
cd4a7468 AM |
2052 | htab->init->contents = bfd_zalloc (htab->init->owner, |
2053 | htab->init->size); | |
2054 | if (htab->init->contents == NULL) | |
2055 | return FALSE; | |
47f6dab9 | 2056 | |
cd4a7468 AM |
2057 | h = define_ovtab_symbol (htab, "__icache_fileoff"); |
2058 | if (h == NULL) | |
2059 | return FALSE; | |
2060 | h->root.u.def.value = 0; | |
2061 | h->root.u.def.section = htab->init; | |
2062 | h->size = 8; | |
e9f53129 AM |
2063 | } |
2064 | } | |
cd4a7468 AM |
2065 | else |
2066 | { | |
2067 | /* Write out _ovly_table. */ | |
2068 | /* set low bit of .size to mark non-overlay area as present. */ | |
2069 | p[7] = 1; | |
2070 | obfd = htab->ovtab->output_section->owner; | |
2071 | for (s = obfd->sections; s != NULL; s = s->next) | |
2072 | { | |
2073 | unsigned int ovl_index = spu_elf_section_data (s)->u.o.ovl_index; | |
e9f53129 | 2074 | |
cd4a7468 AM |
2075 | if (ovl_index != 0) |
2076 | { | |
2077 | unsigned long off = ovl_index * 16; | |
2078 | unsigned int ovl_buf = spu_elf_section_data (s)->u.o.ovl_buf; | |
2079 | ||
2080 | bfd_put_32 (htab->ovtab->owner, s->vma, p + off); | |
2081 | bfd_put_32 (htab->ovtab->owner, (s->size + 15) & -16, | |
2082 | p + off + 4); | |
2083 | /* file_off written later in spu_elf_modify_program_headers. */ | |
2084 | bfd_put_32 (htab->ovtab->owner, ovl_buf, p + off + 12); | |
2085 | } | |
2086 | } | |
e9f53129 | 2087 | |
cd4a7468 AM |
2088 | h = define_ovtab_symbol (htab, "_ovly_table"); |
2089 | if (h == NULL) | |
2090 | return FALSE; | |
2091 | h->root.u.def.value = 16; | |
2092 | h->size = htab->num_overlays * 16; | |
e9f53129 | 2093 | |
cd4a7468 AM |
2094 | h = define_ovtab_symbol (htab, "_ovly_table_end"); |
2095 | if (h == NULL) | |
2096 | return FALSE; | |
2097 | h->root.u.def.value = htab->num_overlays * 16 + 16; | |
2098 | h->size = 0; | |
e9f53129 | 2099 | |
cd4a7468 AM |
2100 | h = define_ovtab_symbol (htab, "_ovly_buf_table"); |
2101 | if (h == NULL) | |
2102 | return FALSE; | |
2103 | h->root.u.def.value = htab->num_overlays * 16 + 16; | |
2104 | h->size = htab->num_buf * 4; | |
2105 | ||
2106 | h = define_ovtab_symbol (htab, "_ovly_buf_table_end"); | |
2107 | if (h == NULL) | |
2108 | return FALSE; | |
2109 | h->root.u.def.value = htab->num_overlays * 16 + 16 + htab->num_buf * 4; | |
2110 | h->size = 0; | |
2111 | } | |
e9f53129 AM |
2112 | |
2113 | h = define_ovtab_symbol (htab, "_EAR_"); | |
2114 | if (h == NULL) | |
2115 | return FALSE; | |
47f6dab9 | 2116 | h->root.u.def.section = htab->toe; |
e9f53129 | 2117 | h->root.u.def.value = 0; |
c1ea05bb | 2118 | h->size = 16; |
e9f53129 AM |
2119 | |
2120 | return TRUE; | |
2121 | } | |
2122 | ||
c65be8d7 | 2123 | /* Check that all loadable section VMAs lie in the range |
9dcc4794 | 2124 | LO .. HI inclusive, and stash some parameters for --auto-overlay. */ |
c65be8d7 AM |
2125 | |
2126 | asection * | |
64615358 | 2127 | spu_elf_check_vma (struct bfd_link_info *info) |
c65be8d7 AM |
2128 | { |
2129 | struct elf_segment_map *m; | |
2130 | unsigned int i; | |
9dcc4794 | 2131 | struct spu_link_hash_table *htab = spu_hash_table (info); |
c65be8d7 | 2132 | bfd *abfd = info->output_bfd; |
64615358 AM |
2133 | bfd_vma hi = htab->params->local_store_hi; |
2134 | bfd_vma lo = htab->params->local_store_lo; | |
c65be8d7 | 2135 | |
9dcc4794 | 2136 | htab->local_store = hi + 1 - lo; |
9dcc4794 | 2137 | |
c65be8d7 AM |
2138 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
2139 | if (m->p_type == PT_LOAD) | |
2140 | for (i = 0; i < m->count; i++) | |
2141 | if (m->sections[i]->size != 0 | |
2142 | && (m->sections[i]->vma < lo | |
2143 | || m->sections[i]->vma > hi | |
2144 | || m->sections[i]->vma + m->sections[i]->size - 1 > hi)) | |
2145 | return m->sections[i]; | |
2146 | ||
2147 | return NULL; | |
2148 | } | |
2149 | ||
49fa1e15 | 2150 | /* OFFSET in SEC (presumably) is the beginning of a function prologue. |
cd4a7468 AM |
2151 | Search for stack adjusting insns, and return the sp delta. |
2152 | If a store of lr is found save the instruction offset to *LR_STORE. | |
2153 | If a stack adjusting instruction is found, save that offset to | |
2154 | *SP_ADJUST. */ | |
49fa1e15 AM |
2155 | |
2156 | static int | |
cd4a7468 AM |
2157 | find_function_stack_adjust (asection *sec, |
2158 | bfd_vma offset, | |
2159 | bfd_vma *lr_store, | |
2160 | bfd_vma *sp_adjust) | |
49fa1e15 | 2161 | { |
49fa1e15 AM |
2162 | int reg[128]; |
2163 | ||
2164 | memset (reg, 0, sizeof (reg)); | |
667f3338 | 2165 | for ( ; offset + 4 <= sec->size; offset += 4) |
49fa1e15 AM |
2166 | { |
2167 | unsigned char buf[4]; | |
2168 | int rt, ra; | |
2169 | int imm; | |
2170 | ||
2171 | /* Assume no relocs on stack adjusing insns. */ | |
2172 | if (!bfd_get_section_contents (sec->owner, sec, buf, offset, 4)) | |
2173 | break; | |
2174 | ||
49fa1e15 AM |
2175 | rt = buf[3] & 0x7f; |
2176 | ra = ((buf[2] & 0x3f) << 1) | (buf[3] >> 7); | |
cd4a7468 AM |
2177 | |
2178 | if (buf[0] == 0x24 /* stqd */) | |
2179 | { | |
2180 | if (rt == 0 /* lr */ && ra == 1 /* sp */) | |
2181 | *lr_store = offset; | |
2182 | continue; | |
2183 | } | |
2184 | ||
49fa1e15 AM |
2185 | /* Partly decoded immediate field. */ |
2186 | imm = (buf[1] << 9) | (buf[2] << 1) | (buf[3] >> 7); | |
2187 | ||
2188 | if (buf[0] == 0x1c /* ai */) | |
2189 | { | |
2190 | imm >>= 7; | |
2191 | imm = (imm ^ 0x200) - 0x200; | |
2192 | reg[rt] = reg[ra] + imm; | |
2193 | ||
2194 | if (rt == 1 /* sp */) | |
2195 | { | |
667f3338 | 2196 | if (reg[rt] > 0) |
49fa1e15 | 2197 | break; |
cd4a7468 | 2198 | *sp_adjust = offset; |
49fa1e15 AM |
2199 | return reg[rt]; |
2200 | } | |
2201 | } | |
2202 | else if (buf[0] == 0x18 && (buf[1] & 0xe0) == 0 /* a */) | |
2203 | { | |
2204 | int rb = ((buf[1] & 0x1f) << 2) | ((buf[2] & 0xc0) >> 6); | |
2205 | ||
2206 | reg[rt] = reg[ra] + reg[rb]; | |
2207 | if (rt == 1) | |
667f3338 AM |
2208 | { |
2209 | if (reg[rt] > 0) | |
2210 | break; | |
cd4a7468 | 2211 | *sp_adjust = offset; |
667f3338 AM |
2212 | return reg[rt]; |
2213 | } | |
49fa1e15 | 2214 | } |
9b175a76 UW |
2215 | else if (buf[0] == 0x08 && (buf[1] & 0xe0) == 0 /* sf */) |
2216 | { | |
2217 | int rb = ((buf[1] & 0x1f) << 2) | ((buf[2] & 0xc0) >> 6); | |
2218 | ||
2219 | reg[rt] = reg[rb] - reg[ra]; | |
2220 | if (rt == 1) | |
2221 | { | |
2222 | if (reg[rt] > 0) | |
2223 | break; | |
2224 | *sp_adjust = offset; | |
2225 | return reg[rt]; | |
2226 | } | |
2227 | } | |
49fa1e15 AM |
2228 | else if ((buf[0] & 0xfc) == 0x40 /* il, ilh, ilhu, ila */) |
2229 | { | |
2230 | if (buf[0] >= 0x42 /* ila */) | |
2231 | imm |= (buf[0] & 1) << 17; | |
2232 | else | |
2233 | { | |
2234 | imm &= 0xffff; | |
2235 | ||
2236 | if (buf[0] == 0x40 /* il */) | |
2237 | { | |
2238 | if ((buf[1] & 0x80) == 0) | |
667f3338 | 2239 | continue; |
49fa1e15 AM |
2240 | imm = (imm ^ 0x8000) - 0x8000; |
2241 | } | |
2242 | else if ((buf[1] & 0x80) == 0 /* ilhu */) | |
2243 | imm <<= 16; | |
2244 | } | |
2245 | reg[rt] = imm; | |
2246 | continue; | |
2247 | } | |
2248 | else if (buf[0] == 0x60 && (buf[1] & 0x80) != 0 /* iohl */) | |
2249 | { | |
2250 | reg[rt] |= imm & 0xffff; | |
2251 | continue; | |
2252 | } | |
2253 | else if (buf[0] == 0x04 /* ori */) | |
2254 | { | |
2255 | imm >>= 7; | |
2256 | imm = (imm ^ 0x200) - 0x200; | |
2257 | reg[rt] = reg[ra] | imm; | |
2258 | continue; | |
2259 | } | |
667f3338 AM |
2260 | else if (buf[0] == 0x32 && (buf[1] & 0x80) != 0 /* fsmbi */) |
2261 | { | |
2262 | reg[rt] = ( ((imm & 0x8000) ? 0xff000000 : 0) | |
2263 | | ((imm & 0x4000) ? 0x00ff0000 : 0) | |
2264 | | ((imm & 0x2000) ? 0x0000ff00 : 0) | |
2265 | | ((imm & 0x1000) ? 0x000000ff : 0)); | |
2266 | continue; | |
2267 | } | |
2268 | else if (buf[0] == 0x16 /* andbi */) | |
49fa1e15 | 2269 | { |
667f3338 AM |
2270 | imm >>= 7; |
2271 | imm &= 0xff; | |
2272 | imm |= imm << 8; | |
2273 | imm |= imm << 16; | |
2274 | reg[rt] = reg[ra] & imm; | |
2275 | continue; | |
2276 | } | |
2277 | else if (buf[0] == 0x33 && imm == 1 /* brsl .+4 */) | |
2278 | { | |
2279 | /* Used in pic reg load. Say rt is trashed. Won't be used | |
2280 | in stack adjust, but we need to continue past this branch. */ | |
49fa1e15 AM |
2281 | reg[rt] = 0; |
2282 | continue; | |
2283 | } | |
fad9eaf0 | 2284 | else if (is_branch (buf) || is_indirect_branch (buf)) |
49fa1e15 AM |
2285 | /* If we hit a branch then we must be out of the prologue. */ |
2286 | break; | |
49fa1e15 AM |
2287 | } |
2288 | ||
2289 | return 0; | |
2290 | } | |
2291 | ||
2292 | /* qsort predicate to sort symbols by section and value. */ | |
2293 | ||
2294 | static Elf_Internal_Sym *sort_syms_syms; | |
2295 | static asection **sort_syms_psecs; | |
2296 | ||
2297 | static int | |
2298 | sort_syms (const void *a, const void *b) | |
2299 | { | |
2300 | Elf_Internal_Sym *const *s1 = a; | |
2301 | Elf_Internal_Sym *const *s2 = b; | |
2302 | asection *sec1,*sec2; | |
2303 | bfd_signed_vma delta; | |
2304 | ||
2305 | sec1 = sort_syms_psecs[*s1 - sort_syms_syms]; | |
2306 | sec2 = sort_syms_psecs[*s2 - sort_syms_syms]; | |
2307 | ||
2308 | if (sec1 != sec2) | |
2309 | return sec1->index - sec2->index; | |
2310 | ||
2311 | delta = (*s1)->st_value - (*s2)->st_value; | |
2312 | if (delta != 0) | |
2313 | return delta < 0 ? -1 : 1; | |
2314 | ||
2315 | delta = (*s2)->st_size - (*s1)->st_size; | |
2316 | if (delta != 0) | |
2317 | return delta < 0 ? -1 : 1; | |
2318 | ||
2319 | return *s1 < *s2 ? -1 : 1; | |
2320 | } | |
2321 | ||
49fa1e15 AM |
2322 | /* Allocate a struct spu_elf_stack_info with MAX_FUN struct function_info |
2323 | entries for section SEC. */ | |
2324 | ||
2325 | static struct spu_elf_stack_info * | |
2326 | alloc_stack_info (asection *sec, int max_fun) | |
2327 | { | |
2328 | struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec); | |
2329 | bfd_size_type amt; | |
2330 | ||
2331 | amt = sizeof (struct spu_elf_stack_info); | |
2332 | amt += (max_fun - 1) * sizeof (struct function_info); | |
47f6dab9 AM |
2333 | sec_data->u.i.stack_info = bfd_zmalloc (amt); |
2334 | if (sec_data->u.i.stack_info != NULL) | |
2335 | sec_data->u.i.stack_info->max_fun = max_fun; | |
2336 | return sec_data->u.i.stack_info; | |
49fa1e15 AM |
2337 | } |
2338 | ||
2339 | /* Add a new struct function_info describing a (part of a) function | |
2340 | starting at SYM_H. Keep the array sorted by address. */ | |
2341 | ||
2342 | static struct function_info * | |
2343 | maybe_insert_function (asection *sec, | |
2344 | void *sym_h, | |
2345 | bfd_boolean global, | |
2346 | bfd_boolean is_func) | |
2347 | { | |
2348 | struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec); | |
47f6dab9 | 2349 | struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info; |
49fa1e15 AM |
2350 | int i; |
2351 | bfd_vma off, size; | |
2352 | ||
2353 | if (sinfo == NULL) | |
2354 | { | |
2355 | sinfo = alloc_stack_info (sec, 20); | |
2356 | if (sinfo == NULL) | |
2357 | return NULL; | |
2358 | } | |
2359 | ||
2360 | if (!global) | |
2361 | { | |
2362 | Elf_Internal_Sym *sym = sym_h; | |
2363 | off = sym->st_value; | |
2364 | size = sym->st_size; | |
2365 | } | |
2366 | else | |
2367 | { | |
2368 | struct elf_link_hash_entry *h = sym_h; | |
2369 | off = h->root.u.def.value; | |
2370 | size = h->size; | |
2371 | } | |
2372 | ||
2373 | for (i = sinfo->num_fun; --i >= 0; ) | |
2374 | if (sinfo->fun[i].lo <= off) | |
2375 | break; | |
2376 | ||
2377 | if (i >= 0) | |
2378 | { | |
2379 | /* Don't add another entry for an alias, but do update some | |
2380 | info. */ | |
2381 | if (sinfo->fun[i].lo == off) | |
2382 | { | |
2383 | /* Prefer globals over local syms. */ | |
2384 | if (global && !sinfo->fun[i].global) | |
2385 | { | |
2386 | sinfo->fun[i].global = TRUE; | |
2387 | sinfo->fun[i].u.h = sym_h; | |
2388 | } | |
2389 | if (is_func) | |
2390 | sinfo->fun[i].is_func = TRUE; | |
2391 | return &sinfo->fun[i]; | |
2392 | } | |
2393 | /* Ignore a zero-size symbol inside an existing function. */ | |
2394 | else if (sinfo->fun[i].hi > off && size == 0) | |
2395 | return &sinfo->fun[i]; | |
2396 | } | |
2397 | ||
1f27ab8d | 2398 | if (sinfo->num_fun >= sinfo->max_fun) |
49fa1e15 AM |
2399 | { |
2400 | bfd_size_type amt = sizeof (struct spu_elf_stack_info); | |
2401 | bfd_size_type old = amt; | |
2402 | ||
2403 | old += (sinfo->max_fun - 1) * sizeof (struct function_info); | |
2404 | sinfo->max_fun += 20 + (sinfo->max_fun >> 1); | |
2405 | amt += (sinfo->max_fun - 1) * sizeof (struct function_info); | |
2406 | sinfo = bfd_realloc (sinfo, amt); | |
2407 | if (sinfo == NULL) | |
2408 | return NULL; | |
2409 | memset ((char *) sinfo + old, 0, amt - old); | |
47f6dab9 | 2410 | sec_data->u.i.stack_info = sinfo; |
49fa1e15 | 2411 | } |
1f27ab8d AM |
2412 | |
2413 | if (++i < sinfo->num_fun) | |
2414 | memmove (&sinfo->fun[i + 1], &sinfo->fun[i], | |
2415 | (sinfo->num_fun - i) * sizeof (sinfo->fun[i])); | |
49fa1e15 AM |
2416 | sinfo->fun[i].is_func = is_func; |
2417 | sinfo->fun[i].global = global; | |
2418 | sinfo->fun[i].sec = sec; | |
2419 | if (global) | |
2420 | sinfo->fun[i].u.h = sym_h; | |
2421 | else | |
2422 | sinfo->fun[i].u.sym = sym_h; | |
2423 | sinfo->fun[i].lo = off; | |
2424 | sinfo->fun[i].hi = off + size; | |
cd4a7468 AM |
2425 | sinfo->fun[i].lr_store = -1; |
2426 | sinfo->fun[i].sp_adjust = -1; | |
2427 | sinfo->fun[i].stack = -find_function_stack_adjust (sec, off, | |
2428 | &sinfo->fun[i].lr_store, | |
2429 | &sinfo->fun[i].sp_adjust); | |
49fa1e15 AM |
2430 | sinfo->num_fun += 1; |
2431 | return &sinfo->fun[i]; | |
2432 | } | |
2433 | ||
2434 | /* Return the name of FUN. */ | |
2435 | ||
2436 | static const char * | |
2437 | func_name (struct function_info *fun) | |
2438 | { | |
2439 | asection *sec; | |
2440 | bfd *ibfd; | |
2441 | Elf_Internal_Shdr *symtab_hdr; | |
2442 | ||
2443 | while (fun->start != NULL) | |
2444 | fun = fun->start; | |
2445 | ||
2446 | if (fun->global) | |
2447 | return fun->u.h->root.root.string; | |
2448 | ||
2449 | sec = fun->sec; | |
2450 | if (fun->u.sym->st_name == 0) | |
2451 | { | |
2452 | size_t len = strlen (sec->name); | |
2453 | char *name = bfd_malloc (len + 10); | |
2454 | if (name == NULL) | |
2455 | return "(null)"; | |
2456 | sprintf (name, "%s+%lx", sec->name, | |
2457 | (unsigned long) fun->u.sym->st_value & 0xffffffff); | |
2458 | return name; | |
2459 | } | |
2460 | ibfd = sec->owner; | |
2461 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
2462 | return bfd_elf_sym_name (ibfd, symtab_hdr, fun->u.sym, sec); | |
2463 | } | |
2464 | ||
2465 | /* Read the instruction at OFF in SEC. Return true iff the instruction | |
2466 | is a nop, lnop, or stop 0 (all zero insn). */ | |
2467 | ||
2468 | static bfd_boolean | |
2469 | is_nop (asection *sec, bfd_vma off) | |
2470 | { | |
2471 | unsigned char insn[4]; | |
2472 | ||
2473 | if (off + 4 > sec->size | |
2474 | || !bfd_get_section_contents (sec->owner, sec, insn, off, 4)) | |
2475 | return FALSE; | |
2476 | if ((insn[0] & 0xbf) == 0 && (insn[1] & 0xe0) == 0x20) | |
2477 | return TRUE; | |
2478 | if (insn[0] == 0 && insn[1] == 0 && insn[2] == 0 && insn[3] == 0) | |
2479 | return TRUE; | |
2480 | return FALSE; | |
2481 | } | |
2482 | ||
2483 | /* Extend the range of FUN to cover nop padding up to LIMIT. | |
2484 | Return TRUE iff some instruction other than a NOP was found. */ | |
2485 | ||
2486 | static bfd_boolean | |
2487 | insns_at_end (struct function_info *fun, bfd_vma limit) | |
2488 | { | |
2489 | bfd_vma off = (fun->hi + 3) & -4; | |
2490 | ||
2491 | while (off < limit && is_nop (fun->sec, off)) | |
2492 | off += 4; | |
2493 | if (off < limit) | |
2494 | { | |
2495 | fun->hi = off; | |
2496 | return TRUE; | |
2497 | } | |
2498 | fun->hi = limit; | |
2499 | return FALSE; | |
2500 | } | |
2501 | ||
2502 | /* Check and fix overlapping function ranges. Return TRUE iff there | |
2503 | are gaps in the current info we have about functions in SEC. */ | |
2504 | ||
2505 | static bfd_boolean | |
2506 | check_function_ranges (asection *sec, struct bfd_link_info *info) | |
2507 | { | |
2508 | struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec); | |
47f6dab9 | 2509 | struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info; |
49fa1e15 AM |
2510 | int i; |
2511 | bfd_boolean gaps = FALSE; | |
2512 | ||
2513 | if (sinfo == NULL) | |
2514 | return FALSE; | |
2515 | ||
2516 | for (i = 1; i < sinfo->num_fun; i++) | |
2517 | if (sinfo->fun[i - 1].hi > sinfo->fun[i].lo) | |
2518 | { | |
2519 | /* Fix overlapping symbols. */ | |
2520 | const char *f1 = func_name (&sinfo->fun[i - 1]); | |
2521 | const char *f2 = func_name (&sinfo->fun[i]); | |
2522 | ||
2523 | info->callbacks->einfo (_("warning: %s overlaps %s\n"), f1, f2); | |
2524 | sinfo->fun[i - 1].hi = sinfo->fun[i].lo; | |
2525 | } | |
2526 | else if (insns_at_end (&sinfo->fun[i - 1], sinfo->fun[i].lo)) | |
2527 | gaps = TRUE; | |
2528 | ||
2529 | if (sinfo->num_fun == 0) | |
2530 | gaps = TRUE; | |
2531 | else | |
2532 | { | |
2533 | if (sinfo->fun[0].lo != 0) | |
2534 | gaps = TRUE; | |
2535 | if (sinfo->fun[sinfo->num_fun - 1].hi > sec->size) | |
2536 | { | |
2537 | const char *f1 = func_name (&sinfo->fun[sinfo->num_fun - 1]); | |
2538 | ||
2539 | info->callbacks->einfo (_("warning: %s exceeds section size\n"), f1); | |
2540 | sinfo->fun[sinfo->num_fun - 1].hi = sec->size; | |
2541 | } | |
2542 | else if (insns_at_end (&sinfo->fun[sinfo->num_fun - 1], sec->size)) | |
2543 | gaps = TRUE; | |
2544 | } | |
2545 | return gaps; | |
2546 | } | |
2547 | ||
2548 | /* Search current function info for a function that contains address | |
2549 | OFFSET in section SEC. */ | |
2550 | ||
2551 | static struct function_info * | |
2552 | find_function (asection *sec, bfd_vma offset, struct bfd_link_info *info) | |
2553 | { | |
2554 | struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec); | |
47f6dab9 | 2555 | struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info; |
49fa1e15 AM |
2556 | int lo, hi, mid; |
2557 | ||
2558 | lo = 0; | |
2559 | hi = sinfo->num_fun; | |
2560 | while (lo < hi) | |
2561 | { | |
2562 | mid = (lo + hi) / 2; | |
2563 | if (offset < sinfo->fun[mid].lo) | |
2564 | hi = mid; | |
2565 | else if (offset >= sinfo->fun[mid].hi) | |
2566 | lo = mid + 1; | |
2567 | else | |
2568 | return &sinfo->fun[mid]; | |
2569 | } | |
2570 | info->callbacks->einfo (_("%A:0x%v not found in function table\n"), | |
2571 | sec, offset); | |
3209bffa | 2572 | bfd_set_error (bfd_error_bad_value); |
49fa1e15 AM |
2573 | return NULL; |
2574 | } | |
2575 | ||
9dcc4794 AM |
2576 | /* Add CALLEE to CALLER call list if not already present. Return TRUE |
2577 | if CALLEE was new. If this function return FALSE, CALLEE should | |
2578 | be freed. */ | |
49fa1e15 AM |
2579 | |
2580 | static bfd_boolean | |
2581 | insert_callee (struct function_info *caller, struct call_info *callee) | |
2582 | { | |
055ed83b AM |
2583 | struct call_info **pp, *p; |
2584 | ||
2585 | for (pp = &caller->call_list; (p = *pp) != NULL; pp = &p->next) | |
49fa1e15 AM |
2586 | if (p->fun == callee->fun) |
2587 | { | |
2588 | /* Tail calls use less stack than normal calls. Retain entry | |
2589 | for normal call over one for tail call. */ | |
c65be8d7 AM |
2590 | p->is_tail &= callee->is_tail; |
2591 | if (!p->is_tail) | |
2592 | { | |
2593 | p->fun->start = NULL; | |
2594 | p->fun->is_func = TRUE; | |
2595 | } | |
34ad4cb0 | 2596 | p->count += callee->count; |
055ed83b AM |
2597 | /* Reorder list so most recent call is first. */ |
2598 | *pp = p->next; | |
2599 | p->next = caller->call_list; | |
2600 | caller->call_list = p; | |
49fa1e15 AM |
2601 | return FALSE; |
2602 | } | |
2603 | callee->next = caller->call_list; | |
2604 | caller->call_list = callee; | |
2605 | return TRUE; | |
2606 | } | |
2607 | ||
9dcc4794 AM |
2608 | /* Copy CALL and insert the copy into CALLER. */ |
2609 | ||
2610 | static bfd_boolean | |
2611 | copy_callee (struct function_info *caller, const struct call_info *call) | |
2612 | { | |
2613 | struct call_info *callee; | |
2614 | callee = bfd_malloc (sizeof (*callee)); | |
2615 | if (callee == NULL) | |
2616 | return FALSE; | |
2617 | *callee = *call; | |
2618 | if (!insert_callee (caller, callee)) | |
2619 | free (callee); | |
2620 | return TRUE; | |
2621 | } | |
2622 | ||
055ed83b AM |
2623 | /* We're only interested in code sections. Testing SEC_IN_MEMORY excludes |
2624 | overlay stub sections. */ | |
2625 | ||
2626 | static bfd_boolean | |
64615358 | 2627 | interesting_section (asection *s) |
055ed83b | 2628 | { |
64615358 | 2629 | return (s->output_section != bfd_abs_section_ptr |
055ed83b AM |
2630 | && ((s->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_IN_MEMORY)) |
2631 | == (SEC_ALLOC | SEC_LOAD | SEC_CODE)) | |
2632 | && s->size != 0); | |
2633 | } | |
2634 | ||
49fa1e15 AM |
2635 | /* Rummage through the relocs for SEC, looking for function calls. |
2636 | If CALL_TREE is true, fill in call graph. If CALL_TREE is false, | |
2637 | mark destination symbols on calls as being functions. Also | |
2638 | look at branches, which may be tail calls or go to hot/cold | |
2639 | section part of same function. */ | |
2640 | ||
2641 | static bfd_boolean | |
2642 | mark_functions_via_relocs (asection *sec, | |
2643 | struct bfd_link_info *info, | |
2644 | int call_tree) | |
2645 | { | |
2646 | Elf_Internal_Rela *internal_relocs, *irelaend, *irela; | |
2ec9638b | 2647 | Elf_Internal_Shdr *symtab_hdr; |
d0249648 | 2648 | void *psyms; |
cd4a7468 | 2649 | unsigned int priority = 0; |
49fa1e15 AM |
2650 | static bfd_boolean warned; |
2651 | ||
64615358 | 2652 | if (!interesting_section (sec) |
055ed83b AM |
2653 | || sec->reloc_count == 0) |
2654 | return TRUE; | |
2655 | ||
49fa1e15 AM |
2656 | internal_relocs = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, |
2657 | info->keep_memory); | |
2658 | if (internal_relocs == NULL) | |
2659 | return FALSE; | |
2660 | ||
2661 | symtab_hdr = &elf_tdata (sec->owner)->symtab_hdr; | |
d0249648 | 2662 | psyms = &symtab_hdr->contents; |
49fa1e15 AM |
2663 | irela = internal_relocs; |
2664 | irelaend = irela + sec->reloc_count; | |
2665 | for (; irela < irelaend; irela++) | |
2666 | { | |
2667 | enum elf_spu_reloc_type r_type; | |
2668 | unsigned int r_indx; | |
2669 | asection *sym_sec; | |
2670 | Elf_Internal_Sym *sym; | |
2671 | struct elf_link_hash_entry *h; | |
2672 | bfd_vma val; | |
9dcc4794 | 2673 | bfd_boolean reject, is_call; |
49fa1e15 AM |
2674 | struct function_info *caller; |
2675 | struct call_info *callee; | |
2676 | ||
9dcc4794 | 2677 | reject = FALSE; |
49fa1e15 AM |
2678 | r_type = ELF32_R_TYPE (irela->r_info); |
2679 | if (r_type != R_SPU_REL16 | |
2680 | && r_type != R_SPU_ADDR16) | |
9dcc4794 AM |
2681 | { |
2682 | reject = TRUE; | |
64615358 | 2683 | if (!(call_tree && spu_hash_table (info)->params->auto_overlay)) |
9dcc4794 AM |
2684 | continue; |
2685 | } | |
49fa1e15 AM |
2686 | |
2687 | r_indx = ELF32_R_SYM (irela->r_info); | |
2688 | if (!get_sym_h (&h, &sym, &sym_sec, psyms, r_indx, sec->owner)) | |
2689 | return FALSE; | |
2690 | ||
2691 | if (sym_sec == NULL | |
64615358 | 2692 | || sym_sec->output_section == bfd_abs_section_ptr) |
49fa1e15 AM |
2693 | continue; |
2694 | ||
9dcc4794 AM |
2695 | is_call = FALSE; |
2696 | if (!reject) | |
2697 | { | |
2698 | unsigned char insn[4]; | |
2699 | ||
2700 | if (!bfd_get_section_contents (sec->owner, sec, insn, | |
2701 | irela->r_offset, 4)) | |
2702 | return FALSE; | |
2703 | if (is_branch (insn)) | |
2704 | { | |
2705 | is_call = (insn[0] & 0xfd) == 0x31; | |
cd4a7468 AM |
2706 | priority = insn[1] & 0x0f; |
2707 | priority <<= 8; | |
2708 | priority |= insn[2]; | |
2709 | priority <<= 8; | |
2710 | priority |= insn[3]; | |
2711 | priority >>= 7; | |
9dcc4794 AM |
2712 | if ((sym_sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE)) |
2713 | != (SEC_ALLOC | SEC_LOAD | SEC_CODE)) | |
2714 | { | |
2715 | if (!warned) | |
2716 | info->callbacks->einfo | |
2717 | (_("%B(%A+0x%v): call to non-code section" | |
2718 | " %B(%A), analysis incomplete\n"), | |
2719 | sec->owner, sec, irela->r_offset, | |
2720 | sym_sec->owner, sym_sec); | |
2721 | warned = TRUE; | |
2722 | continue; | |
2723 | } | |
2724 | } | |
2725 | else | |
2726 | { | |
2727 | reject = TRUE; | |
64615358 | 2728 | if (!(call_tree && spu_hash_table (info)->params->auto_overlay) |
9dcc4794 AM |
2729 | || is_hint (insn)) |
2730 | continue; | |
2731 | } | |
2732 | } | |
49fa1e15 | 2733 | |
9dcc4794 | 2734 | if (reject) |
49fa1e15 | 2735 | { |
9dcc4794 AM |
2736 | /* For --auto-overlay, count possible stubs we need for |
2737 | function pointer references. */ | |
2738 | unsigned int sym_type; | |
2739 | if (h) | |
2740 | sym_type = h->type; | |
2741 | else | |
2742 | sym_type = ELF_ST_TYPE (sym->st_info); | |
2743 | if (sym_type == STT_FUNC) | |
2744 | spu_hash_table (info)->non_ovly_stub += 1; | |
49fa1e15 AM |
2745 | continue; |
2746 | } | |
2747 | ||
49fa1e15 AM |
2748 | if (h) |
2749 | val = h->root.u.def.value; | |
2750 | else | |
2751 | val = sym->st_value; | |
2752 | val += irela->r_addend; | |
2753 | ||
2754 | if (!call_tree) | |
2755 | { | |
2756 | struct function_info *fun; | |
2757 | ||
2758 | if (irela->r_addend != 0) | |
2759 | { | |
2760 | Elf_Internal_Sym *fake = bfd_zmalloc (sizeof (*fake)); | |
2761 | if (fake == NULL) | |
2762 | return FALSE; | |
2763 | fake->st_value = val; | |
2764 | fake->st_shndx | |
2765 | = _bfd_elf_section_from_bfd_section (sym_sec->owner, sym_sec); | |
2766 | sym = fake; | |
2767 | } | |
2768 | if (sym) | |
2769 | fun = maybe_insert_function (sym_sec, sym, FALSE, is_call); | |
2770 | else | |
2771 | fun = maybe_insert_function (sym_sec, h, TRUE, is_call); | |
2772 | if (fun == NULL) | |
2773 | return FALSE; | |
2774 | if (irela->r_addend != 0 | |
2775 | && fun->u.sym != sym) | |
2776 | free (sym); | |
2777 | continue; | |
2778 | } | |
2779 | ||
2780 | caller = find_function (sec, irela->r_offset, info); | |
2781 | if (caller == NULL) | |
2782 | return FALSE; | |
2783 | callee = bfd_malloc (sizeof *callee); | |
2784 | if (callee == NULL) | |
2785 | return FALSE; | |
2786 | ||
2787 | callee->fun = find_function (sym_sec, val, info); | |
2788 | if (callee->fun == NULL) | |
2789 | return FALSE; | |
2790 | callee->is_tail = !is_call; | |
9dcc4794 | 2791 | callee->is_pasted = FALSE; |
cd4a7468 | 2792 | callee->priority = priority; |
34ad4cb0 | 2793 | callee->count = 1; |
9dcc4794 AM |
2794 | if (callee->fun->last_caller != sec) |
2795 | { | |
2796 | callee->fun->last_caller = sec; | |
2797 | callee->fun->call_count += 1; | |
2798 | } | |
49fa1e15 AM |
2799 | if (!insert_callee (caller, callee)) |
2800 | free (callee); | |
2801 | else if (!is_call | |
2802 | && !callee->fun->is_func | |
2803 | && callee->fun->stack == 0) | |
2804 | { | |
2805 | /* This is either a tail call or a branch from one part of | |
2806 | the function to another, ie. hot/cold section. If the | |
2807 | destination has been called by some other function then | |
2808 | it is a separate function. We also assume that functions | |
2809 | are not split across input files. */ | |
911f096e | 2810 | if (sec->owner != sym_sec->owner) |
49fa1e15 AM |
2811 | { |
2812 | callee->fun->start = NULL; | |
2813 | callee->fun->is_func = TRUE; | |
2814 | } | |
911f096e | 2815 | else if (callee->fun->start == NULL) |
cc210763 UW |
2816 | { |
2817 | struct function_info *caller_start = caller; | |
2818 | while (caller_start->start) | |
2819 | caller_start = caller_start->start; | |
2820 | ||
2821 | if (caller_start != callee->fun) | |
2822 | callee->fun->start = caller_start; | |
2823 | } | |
911f096e AM |
2824 | else |
2825 | { | |
2826 | struct function_info *callee_start; | |
2827 | struct function_info *caller_start; | |
2828 | callee_start = callee->fun; | |
2829 | while (callee_start->start) | |
2830 | callee_start = callee_start->start; | |
2831 | caller_start = caller; | |
2832 | while (caller_start->start) | |
2833 | caller_start = caller_start->start; | |
2834 | if (caller_start != callee_start) | |
2835 | { | |
2836 | callee->fun->start = NULL; | |
2837 | callee->fun->is_func = TRUE; | |
2838 | } | |
2839 | } | |
49fa1e15 AM |
2840 | } |
2841 | } | |
2842 | ||
2843 | return TRUE; | |
2844 | } | |
2845 | ||
2846 | /* Handle something like .init or .fini, which has a piece of a function. | |
2847 | These sections are pasted together to form a single function. */ | |
2848 | ||
2849 | static bfd_boolean | |
3209bffa | 2850 | pasted_function (asection *sec) |
49fa1e15 AM |
2851 | { |
2852 | struct bfd_link_order *l; | |
2853 | struct _spu_elf_section_data *sec_data; | |
2854 | struct spu_elf_stack_info *sinfo; | |
2855 | Elf_Internal_Sym *fake; | |
2856 | struct function_info *fun, *fun_start; | |
2857 | ||
2858 | fake = bfd_zmalloc (sizeof (*fake)); | |
2859 | if (fake == NULL) | |
2860 | return FALSE; | |
2861 | fake->st_value = 0; | |
2862 | fake->st_size = sec->size; | |
2863 | fake->st_shndx | |
2864 | = _bfd_elf_section_from_bfd_section (sec->owner, sec); | |
2865 | fun = maybe_insert_function (sec, fake, FALSE, FALSE); | |
2866 | if (!fun) | |
2867 | return FALSE; | |
2868 | ||
2869 | /* Find a function immediately preceding this section. */ | |
2870 | fun_start = NULL; | |
2871 | for (l = sec->output_section->map_head.link_order; l != NULL; l = l->next) | |
2872 | { | |
2873 | if (l->u.indirect.section == sec) | |
2874 | { | |
2875 | if (fun_start != NULL) | |
9dcc4794 AM |
2876 | { |
2877 | struct call_info *callee = bfd_malloc (sizeof *callee); | |
2878 | if (callee == NULL) | |
2879 | return FALSE; | |
2880 | ||
2881 | fun->start = fun_start; | |
2882 | callee->fun = fun; | |
2883 | callee->is_tail = TRUE; | |
2884 | callee->is_pasted = TRUE; | |
34ad4cb0 | 2885 | callee->count = 1; |
9dcc4794 AM |
2886 | if (!insert_callee (fun_start, callee)) |
2887 | free (callee); | |
2888 | return TRUE; | |
2889 | } | |
2890 | break; | |
49fa1e15 AM |
2891 | } |
2892 | if (l->type == bfd_indirect_link_order | |
2893 | && (sec_data = spu_elf_section_data (l->u.indirect.section)) != NULL | |
47f6dab9 | 2894 | && (sinfo = sec_data->u.i.stack_info) != NULL |
49fa1e15 AM |
2895 | && sinfo->num_fun != 0) |
2896 | fun_start = &sinfo->fun[sinfo->num_fun - 1]; | |
2897 | } | |
2898 | ||
3209bffa AM |
2899 | /* Don't return an error if we did not find a function preceding this |
2900 | section. The section may have incorrect flags. */ | |
2901 | return TRUE; | |
49fa1e15 AM |
2902 | } |
2903 | ||
49fa1e15 AM |
2904 | /* Map address ranges in code sections to functions. */ |
2905 | ||
2906 | static bfd_boolean | |
c65be8d7 | 2907 | discover_functions (struct bfd_link_info *info) |
49fa1e15 | 2908 | { |
49fa1e15 AM |
2909 | bfd *ibfd; |
2910 | int bfd_idx; | |
2911 | Elf_Internal_Sym ***psym_arr; | |
2912 | asection ***sec_arr; | |
2913 | bfd_boolean gaps = FALSE; | |
2914 | ||
2915 | bfd_idx = 0; | |
2916 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
2917 | bfd_idx++; | |
2918 | ||
2919 | psym_arr = bfd_zmalloc (bfd_idx * sizeof (*psym_arr)); | |
2920 | if (psym_arr == NULL) | |
2921 | return FALSE; | |
2922 | sec_arr = bfd_zmalloc (bfd_idx * sizeof (*sec_arr)); | |
2923 | if (sec_arr == NULL) | |
2924 | return FALSE; | |
49fa1e15 AM |
2925 | |
2926 | for (ibfd = info->input_bfds, bfd_idx = 0; | |
2927 | ibfd != NULL; | |
2928 | ibfd = ibfd->link_next, bfd_idx++) | |
2929 | { | |
2930 | extern const bfd_target bfd_elf32_spu_vec; | |
2931 | Elf_Internal_Shdr *symtab_hdr; | |
2932 | asection *sec; | |
2933 | size_t symcount; | |
2934 | Elf_Internal_Sym *syms, *sy, **psyms, **psy; | |
2935 | asection **psecs, **p; | |
2936 | ||
2937 | if (ibfd->xvec != &bfd_elf32_spu_vec) | |
2938 | continue; | |
2939 | ||
2940 | /* Read all the symbols. */ | |
2941 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
2942 | symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
2943 | if (symcount == 0) | |
055ed83b AM |
2944 | { |
2945 | if (!gaps) | |
2946 | for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next) | |
64615358 | 2947 | if (interesting_section (sec)) |
055ed83b AM |
2948 | { |
2949 | gaps = TRUE; | |
2950 | break; | |
2951 | } | |
2952 | continue; | |
2953 | } | |
49fa1e15 | 2954 | |
1f27ab8d | 2955 | if (symtab_hdr->contents != NULL) |
49fa1e15 | 2956 | { |
1f27ab8d AM |
2957 | /* Don't use cached symbols since the generic ELF linker |
2958 | code only reads local symbols, and we need globals too. */ | |
2959 | free (symtab_hdr->contents); | |
2960 | symtab_hdr->contents = NULL; | |
49fa1e15 | 2961 | } |
1f27ab8d AM |
2962 | syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, 0, |
2963 | NULL, NULL, NULL); | |
2964 | symtab_hdr->contents = (void *) syms; | |
2965 | if (syms == NULL) | |
2966 | return FALSE; | |
49fa1e15 AM |
2967 | |
2968 | /* Select defined function symbols that are going to be output. */ | |
2969 | psyms = bfd_malloc ((symcount + 1) * sizeof (*psyms)); | |
2970 | if (psyms == NULL) | |
2971 | return FALSE; | |
2972 | psym_arr[bfd_idx] = psyms; | |
2973 | psecs = bfd_malloc (symcount * sizeof (*psecs)); | |
2974 | if (psecs == NULL) | |
2975 | return FALSE; | |
2976 | sec_arr[bfd_idx] = psecs; | |
2977 | for (psy = psyms, p = psecs, sy = syms; sy < syms + symcount; ++p, ++sy) | |
2978 | if (ELF_ST_TYPE (sy->st_info) == STT_NOTYPE | |
3209bffa | 2979 | || ELF_ST_TYPE (sy->st_info) == STT_FUNC) |
49fa1e15 AM |
2980 | { |
2981 | asection *s; | |
2982 | ||
2983 | *p = s = bfd_section_from_elf_index (ibfd, sy->st_shndx); | |
64615358 | 2984 | if (s != NULL && interesting_section (s)) |
49fa1e15 AM |
2985 | *psy++ = sy; |
2986 | } | |
2987 | symcount = psy - psyms; | |
2988 | *psy = NULL; | |
2989 | ||
2990 | /* Sort them by section and offset within section. */ | |
2991 | sort_syms_syms = syms; | |
2992 | sort_syms_psecs = psecs; | |
2993 | qsort (psyms, symcount, sizeof (*psyms), sort_syms); | |
2994 | ||
2995 | /* Now inspect the function symbols. */ | |
2996 | for (psy = psyms; psy < psyms + symcount; ) | |
2997 | { | |
2998 | asection *s = psecs[*psy - syms]; | |
2999 | Elf_Internal_Sym **psy2; | |
3000 | ||
3001 | for (psy2 = psy; ++psy2 < psyms + symcount; ) | |
3002 | if (psecs[*psy2 - syms] != s) | |
3003 | break; | |
3004 | ||
3005 | if (!alloc_stack_info (s, psy2 - psy)) | |
3006 | return FALSE; | |
3007 | psy = psy2; | |
3008 | } | |
3009 | ||
3010 | /* First install info about properly typed and sized functions. | |
3011 | In an ideal world this will cover all code sections, except | |
3012 | when partitioning functions into hot and cold sections, | |
3013 | and the horrible pasted together .init and .fini functions. */ | |
3014 | for (psy = psyms; psy < psyms + symcount; ++psy) | |
3015 | { | |
3016 | sy = *psy; | |
3017 | if (ELF_ST_TYPE (sy->st_info) == STT_FUNC) | |
3018 | { | |
3019 | asection *s = psecs[sy - syms]; | |
3020 | if (!maybe_insert_function (s, sy, FALSE, TRUE)) | |
3021 | return FALSE; | |
3022 | } | |
3023 | } | |
3024 | ||
3025 | for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next) | |
64615358 | 3026 | if (interesting_section (sec)) |
49fa1e15 AM |
3027 | gaps |= check_function_ranges (sec, info); |
3028 | } | |
3029 | ||
3030 | if (gaps) | |
3031 | { | |
3032 | /* See if we can discover more function symbols by looking at | |
3033 | relocations. */ | |
3034 | for (ibfd = info->input_bfds, bfd_idx = 0; | |
3035 | ibfd != NULL; | |
3036 | ibfd = ibfd->link_next, bfd_idx++) | |
3037 | { | |
3038 | asection *sec; | |
3039 | ||
3040 | if (psym_arr[bfd_idx] == NULL) | |
3041 | continue; | |
3042 | ||
3043 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
055ed83b AM |
3044 | if (!mark_functions_via_relocs (sec, info, FALSE)) |
3045 | return FALSE; | |
49fa1e15 AM |
3046 | } |
3047 | ||
3048 | for (ibfd = info->input_bfds, bfd_idx = 0; | |
3049 | ibfd != NULL; | |
3050 | ibfd = ibfd->link_next, bfd_idx++) | |
3051 | { | |
3052 | Elf_Internal_Shdr *symtab_hdr; | |
3053 | asection *sec; | |
3054 | Elf_Internal_Sym *syms, *sy, **psyms, **psy; | |
3055 | asection **psecs; | |
3056 | ||
3057 | if ((psyms = psym_arr[bfd_idx]) == NULL) | |
3058 | continue; | |
3059 | ||
3060 | psecs = sec_arr[bfd_idx]; | |
3061 | ||
3062 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
3063 | syms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
3064 | ||
3065 | gaps = FALSE; | |
3066 | for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next) | |
64615358 | 3067 | if (interesting_section (sec)) |
49fa1e15 AM |
3068 | gaps |= check_function_ranges (sec, info); |
3069 | if (!gaps) | |
3070 | continue; | |
3071 | ||
3072 | /* Finally, install all globals. */ | |
3073 | for (psy = psyms; (sy = *psy) != NULL; ++psy) | |
3074 | { | |
3075 | asection *s; | |
3076 | ||
3077 | s = psecs[sy - syms]; | |
3078 | ||
3079 | /* Global syms might be improperly typed functions. */ | |
3080 | if (ELF_ST_TYPE (sy->st_info) != STT_FUNC | |
3081 | && ELF_ST_BIND (sy->st_info) == STB_GLOBAL) | |
3082 | { | |
3083 | if (!maybe_insert_function (s, sy, FALSE, FALSE)) | |
3084 | return FALSE; | |
3085 | } | |
3086 | } | |
055ed83b AM |
3087 | } |
3088 | ||
3089 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
3090 | { | |
3091 | extern const bfd_target bfd_elf32_spu_vec; | |
3092 | asection *sec; | |
3093 | ||
3094 | if (ibfd->xvec != &bfd_elf32_spu_vec) | |
3095 | continue; | |
49fa1e15 AM |
3096 | |
3097 | /* Some of the symbols we've installed as marking the | |
3098 | beginning of functions may have a size of zero. Extend | |
3099 | the range of such functions to the beginning of the | |
3100 | next symbol of interest. */ | |
3101 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
64615358 | 3102 | if (interesting_section (sec)) |
49fa1e15 AM |
3103 | { |
3104 | struct _spu_elf_section_data *sec_data; | |
3105 | struct spu_elf_stack_info *sinfo; | |
3106 | ||
3107 | sec_data = spu_elf_section_data (sec); | |
47f6dab9 | 3108 | sinfo = sec_data->u.i.stack_info; |
3209bffa | 3109 | if (sinfo != NULL && sinfo->num_fun != 0) |
49fa1e15 AM |
3110 | { |
3111 | int fun_idx; | |
3112 | bfd_vma hi = sec->size; | |
3113 | ||
3114 | for (fun_idx = sinfo->num_fun; --fun_idx >= 0; ) | |
3115 | { | |
3116 | sinfo->fun[fun_idx].hi = hi; | |
3117 | hi = sinfo->fun[fun_idx].lo; | |
3118 | } | |
3209bffa AM |
3119 | |
3120 | sinfo->fun[0].lo = 0; | |
49fa1e15 AM |
3121 | } |
3122 | /* No symbols in this section. Must be .init or .fini | |
3123 | or something similar. */ | |
3209bffa | 3124 | else if (!pasted_function (sec)) |
49fa1e15 AM |
3125 | return FALSE; |
3126 | } | |
3127 | } | |
3128 | } | |
3129 | ||
3130 | for (ibfd = info->input_bfds, bfd_idx = 0; | |
3131 | ibfd != NULL; | |
3132 | ibfd = ibfd->link_next, bfd_idx++) | |
3133 | { | |
3134 | if (psym_arr[bfd_idx] == NULL) | |
3135 | continue; | |
3136 | ||
3137 | free (psym_arr[bfd_idx]); | |
3138 | free (sec_arr[bfd_idx]); | |
3139 | } | |
3140 | ||
3141 | free (psym_arr); | |
3142 | free (sec_arr); | |
3143 | ||
3144 | return TRUE; | |
3145 | } | |
3146 | ||
055ed83b AM |
3147 | /* Iterate over all function_info we have collected, calling DOIT on |
3148 | each node if ROOT_ONLY is false. Only call DOIT on root nodes | |
3149 | if ROOT_ONLY. */ | |
3150 | ||
3151 | static bfd_boolean | |
3152 | for_each_node (bfd_boolean (*doit) (struct function_info *, | |
3153 | struct bfd_link_info *, | |
3154 | void *), | |
3155 | struct bfd_link_info *info, | |
3156 | void *param, | |
3157 | int root_only) | |
3158 | { | |
3159 | bfd *ibfd; | |
3160 | ||
3161 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
3162 | { | |
3163 | extern const bfd_target bfd_elf32_spu_vec; | |
3164 | asection *sec; | |
3165 | ||
3166 | if (ibfd->xvec != &bfd_elf32_spu_vec) | |
3167 | continue; | |
3168 | ||
3169 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
3170 | { | |
3171 | struct _spu_elf_section_data *sec_data; | |
3172 | struct spu_elf_stack_info *sinfo; | |
3173 | ||
3174 | if ((sec_data = spu_elf_section_data (sec)) != NULL | |
3175 | && (sinfo = sec_data->u.i.stack_info) != NULL) | |
3176 | { | |
3177 | int i; | |
3178 | for (i = 0; i < sinfo->num_fun; ++i) | |
3179 | if (!root_only || !sinfo->fun[i].non_root) | |
3180 | if (!doit (&sinfo->fun[i], info, param)) | |
3181 | return FALSE; | |
3182 | } | |
3183 | } | |
3184 | } | |
3185 | return TRUE; | |
3186 | } | |
3187 | ||
3188 | /* Transfer call info attached to struct function_info entries for | |
3189 | all of a given function's sections to the first entry. */ | |
3190 | ||
3191 | static bfd_boolean | |
3192 | transfer_calls (struct function_info *fun, | |
3193 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
3194 | void *param ATTRIBUTE_UNUSED) | |
3195 | { | |
3196 | struct function_info *start = fun->start; | |
3197 | ||
3198 | if (start != NULL) | |
3199 | { | |
3200 | struct call_info *call, *call_next; | |
3201 | ||
3202 | while (start->start != NULL) | |
3203 | start = start->start; | |
3204 | for (call = fun->call_list; call != NULL; call = call_next) | |
3205 | { | |
3206 | call_next = call->next; | |
3207 | if (!insert_callee (start, call)) | |
3208 | free (call); | |
3209 | } | |
3210 | fun->call_list = NULL; | |
3211 | } | |
3212 | return TRUE; | |
3213 | } | |
3214 | ||
49fa1e15 AM |
3215 | /* Mark nodes in the call graph that are called by some other node. */ |
3216 | ||
055ed83b AM |
3217 | static bfd_boolean |
3218 | mark_non_root (struct function_info *fun, | |
3219 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
3220 | void *param ATTRIBUTE_UNUSED) | |
49fa1e15 AM |
3221 | { |
3222 | struct call_info *call; | |
3223 | ||
055ed83b AM |
3224 | if (fun->visit1) |
3225 | return TRUE; | |
49fa1e15 AM |
3226 | fun->visit1 = TRUE; |
3227 | for (call = fun->call_list; call; call = call->next) | |
3228 | { | |
3229 | call->fun->non_root = TRUE; | |
055ed83b | 3230 | mark_non_root (call->fun, 0, 0); |
49fa1e15 | 3231 | } |
055ed83b | 3232 | return TRUE; |
49fa1e15 AM |
3233 | } |
3234 | ||
9dcc4794 | 3235 | /* Remove cycles from the call graph. Set depth of nodes. */ |
49fa1e15 | 3236 | |
055ed83b AM |
3237 | static bfd_boolean |
3238 | remove_cycles (struct function_info *fun, | |
3239 | struct bfd_link_info *info, | |
9dcc4794 | 3240 | void *param) |
49fa1e15 AM |
3241 | { |
3242 | struct call_info **callp, *call; | |
9dcc4794 AM |
3243 | unsigned int depth = *(unsigned int *) param; |
3244 | unsigned int max_depth = depth; | |
49fa1e15 | 3245 | |
9dcc4794 | 3246 | fun->depth = depth; |
49fa1e15 AM |
3247 | fun->visit2 = TRUE; |
3248 | fun->marking = TRUE; | |
3249 | ||
3250 | callp = &fun->call_list; | |
3251 | while ((call = *callp) != NULL) | |
3252 | { | |
25076afa | 3253 | call->max_depth = depth + !call->is_pasted; |
49fa1e15 | 3254 | if (!call->fun->visit2) |
055ed83b | 3255 | { |
9dcc4794 | 3256 | if (!remove_cycles (call->fun, info, &call->max_depth)) |
055ed83b | 3257 | return FALSE; |
9dcc4794 AM |
3258 | if (max_depth < call->max_depth) |
3259 | max_depth = call->max_depth; | |
055ed83b | 3260 | } |
49fa1e15 AM |
3261 | else if (call->fun->marking) |
3262 | { | |
cd4a7468 AM |
3263 | struct spu_link_hash_table *htab = spu_hash_table (info); |
3264 | ||
3265 | if (!htab->params->auto_overlay | |
3266 | && htab->params->stack_analysis) | |
9dcc4794 AM |
3267 | { |
3268 | const char *f1 = func_name (fun); | |
3269 | const char *f2 = func_name (call->fun); | |
49fa1e15 | 3270 | |
9dcc4794 AM |
3271 | info->callbacks->info (_("Stack analysis will ignore the call " |
3272 | "from %s to %s\n"), | |
3273 | f1, f2); | |
3274 | } | |
5ebe2858 UW |
3275 | |
3276 | call->broken_cycle = TRUE; | |
49fa1e15 AM |
3277 | } |
3278 | callp = &call->next; | |
3279 | } | |
3280 | fun->marking = FALSE; | |
9dcc4794 | 3281 | *(unsigned int *) param = max_depth; |
055ed83b | 3282 | return TRUE; |
49fa1e15 AM |
3283 | } |
3284 | ||
667f3338 AM |
3285 | /* Check that we actually visited all nodes in remove_cycles. If we |
3286 | didn't, then there is some cycle in the call graph not attached to | |
3287 | any root node. Arbitrarily choose a node in the cycle as a new | |
3288 | root and break the cycle. */ | |
3289 | ||
3290 | static bfd_boolean | |
3291 | mark_detached_root (struct function_info *fun, | |
3292 | struct bfd_link_info *info, | |
3293 | void *param) | |
3294 | { | |
3295 | if (fun->visit2) | |
3296 | return TRUE; | |
3297 | fun->non_root = FALSE; | |
3298 | *(unsigned int *) param = 0; | |
3299 | return remove_cycles (fun, info, param); | |
3300 | } | |
3301 | ||
49fa1e15 AM |
3302 | /* Populate call_list for each function. */ |
3303 | ||
3304 | static bfd_boolean | |
c65be8d7 | 3305 | build_call_tree (struct bfd_link_info *info) |
49fa1e15 | 3306 | { |
49fa1e15 | 3307 | bfd *ibfd; |
9dcc4794 | 3308 | unsigned int depth; |
49fa1e15 AM |
3309 | |
3310 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
3311 | { | |
3312 | extern const bfd_target bfd_elf32_spu_vec; | |
3313 | asection *sec; | |
3314 | ||
3315 | if (ibfd->xvec != &bfd_elf32_spu_vec) | |
3316 | continue; | |
3317 | ||
3318 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
055ed83b AM |
3319 | if (!mark_functions_via_relocs (sec, info, TRUE)) |
3320 | return FALSE; | |
49fa1e15 AM |
3321 | } |
3322 | ||
055ed83b AM |
3323 | /* Transfer call info from hot/cold section part of function |
3324 | to main entry. */ | |
64615358 | 3325 | if (!spu_hash_table (info)->params->auto_overlay |
9dcc4794 | 3326 | && !for_each_node (transfer_calls, info, 0, FALSE)) |
055ed83b | 3327 | return FALSE; |
49fa1e15 | 3328 | |
055ed83b AM |
3329 | /* Find the call graph root(s). */ |
3330 | if (!for_each_node (mark_non_root, info, 0, FALSE)) | |
3331 | return FALSE; | |
49fa1e15 AM |
3332 | |
3333 | /* Remove cycles from the call graph. We start from the root node(s) | |
3334 | so that we break cycles in a reasonable place. */ | |
9dcc4794 | 3335 | depth = 0; |
667f3338 AM |
3336 | if (!for_each_node (remove_cycles, info, &depth, TRUE)) |
3337 | return FALSE; | |
3338 | ||
3339 | return for_each_node (mark_detached_root, info, &depth, FALSE); | |
9dcc4794 AM |
3340 | } |
3341 | ||
cd4a7468 | 3342 | /* qsort predicate to sort calls by priority, max_depth then count. */ |
9dcc4794 AM |
3343 | |
3344 | static int | |
3345 | sort_calls (const void *a, const void *b) | |
3346 | { | |
3347 | struct call_info *const *c1 = a; | |
3348 | struct call_info *const *c2 = b; | |
3349 | int delta; | |
3350 | ||
cd4a7468 AM |
3351 | delta = (*c2)->priority - (*c1)->priority; |
3352 | if (delta != 0) | |
3353 | return delta; | |
3354 | ||
9dcc4794 AM |
3355 | delta = (*c2)->max_depth - (*c1)->max_depth; |
3356 | if (delta != 0) | |
3357 | return delta; | |
3358 | ||
3359 | delta = (*c2)->count - (*c1)->count; | |
3360 | if (delta != 0) | |
3361 | return delta; | |
3362 | ||
667f3338 | 3363 | return (char *) c1 - (char *) c2; |
9dcc4794 AM |
3364 | } |
3365 | ||
3366 | struct _mos_param { | |
3367 | unsigned int max_overlay_size; | |
3368 | }; | |
3369 | ||
3370 | /* Set linker_mark and gc_mark on any sections that we will put in | |
3371 | overlays. These flags are used by the generic ELF linker, but we | |
3372 | won't be continuing on to bfd_elf_final_link so it is OK to use | |
3373 | them. linker_mark is clear before we get here. Set segment_mark | |
3374 | on sections that are part of a pasted function (excluding the last | |
3375 | section). | |
3376 | ||
3377 | Set up function rodata section if --overlay-rodata. We don't | |
3378 | currently include merged string constant rodata sections since | |
3379 | ||
3380 | Sort the call graph so that the deepest nodes will be visited | |
3381 | first. */ | |
3382 | ||
3383 | static bfd_boolean | |
3384 | mark_overlay_section (struct function_info *fun, | |
3385 | struct bfd_link_info *info, | |
3386 | void *param) | |
3387 | { | |
3388 | struct call_info *call; | |
3389 | unsigned int count; | |
3390 | struct _mos_param *mos_param = param; | |
fb266b8b | 3391 | struct spu_link_hash_table *htab = spu_hash_table (info); |
9dcc4794 AM |
3392 | |
3393 | if (fun->visit4) | |
3394 | return TRUE; | |
3395 | ||
3396 | fun->visit4 = TRUE; | |
fb266b8b AM |
3397 | if (!fun->sec->linker_mark |
3398 | && (htab->params->ovly_flavour != ovly_soft_icache | |
3399 | || htab->params->non_ia_text | |
335bd8f2 UW |
3400 | || strncmp (fun->sec->name, ".text.ia.", 9) == 0 |
3401 | || strcmp (fun->sec->name, ".init") == 0 | |
3402 | || strcmp (fun->sec->name, ".fini") == 0)) | |
9dcc4794 | 3403 | { |
4f0d75be AM |
3404 | unsigned int size; |
3405 | ||
9dcc4794 AM |
3406 | fun->sec->linker_mark = 1; |
3407 | fun->sec->gc_mark = 1; | |
3408 | fun->sec->segment_mark = 0; | |
3409 | /* Ensure SEC_CODE is set on this text section (it ought to | |
3410 | be!), and SEC_CODE is clear on rodata sections. We use | |
3411 | this flag to differentiate the two overlay section types. */ | |
3412 | fun->sec->flags |= SEC_CODE; | |
4f0d75be | 3413 | |
fb266b8b AM |
3414 | size = fun->sec->size; |
3415 | if (htab->params->auto_overlay & OVERLAY_RODATA) | |
9dcc4794 AM |
3416 | { |
3417 | char *name = NULL; | |
9dcc4794 AM |
3418 | |
3419 | /* Find the rodata section corresponding to this function's | |
3420 | text section. */ | |
3421 | if (strcmp (fun->sec->name, ".text") == 0) | |
3422 | { | |
3423 | name = bfd_malloc (sizeof (".rodata")); | |
3424 | if (name == NULL) | |
3425 | return FALSE; | |
3426 | memcpy (name, ".rodata", sizeof (".rodata")); | |
3427 | } | |
3428 | else if (strncmp (fun->sec->name, ".text.", 6) == 0) | |
3429 | { | |
3430 | size_t len = strlen (fun->sec->name); | |
3431 | name = bfd_malloc (len + 3); | |
3432 | if (name == NULL) | |
3433 | return FALSE; | |
3434 | memcpy (name, ".rodata", sizeof (".rodata")); | |
3435 | memcpy (name + 7, fun->sec->name + 5, len - 4); | |
3436 | } | |
3437 | else if (strncmp (fun->sec->name, ".gnu.linkonce.t.", 16) == 0) | |
3438 | { | |
3439 | size_t len = strlen (fun->sec->name) + 1; | |
3440 | name = bfd_malloc (len); | |
3441 | if (name == NULL) | |
3442 | return FALSE; | |
3443 | memcpy (name, fun->sec->name, len); | |
3444 | name[14] = 'r'; | |
3445 | } | |
3446 | ||
3447 | if (name != NULL) | |
3448 | { | |
3449 | asection *rodata = NULL; | |
3450 | asection *group_sec = elf_section_data (fun->sec)->next_in_group; | |
3451 | if (group_sec == NULL) | |
3452 | rodata = bfd_get_section_by_name (fun->sec->owner, name); | |
3453 | else | |
3454 | while (group_sec != NULL && group_sec != fun->sec) | |
3455 | { | |
3456 | if (strcmp (group_sec->name, name) == 0) | |
3457 | { | |
3458 | rodata = group_sec; | |
3459 | break; | |
3460 | } | |
3461 | group_sec = elf_section_data (group_sec)->next_in_group; | |
3462 | } | |
3463 | fun->rodata = rodata; | |
3464 | if (fun->rodata) | |
3465 | { | |
fb266b8b AM |
3466 | size += fun->rodata->size; |
3467 | if (htab->params->line_size != 0 | |
3468 | && size > htab->params->line_size) | |
3469 | { | |
3470 | size -= fun->rodata->size; | |
3471 | fun->rodata = NULL; | |
3472 | } | |
3473 | else | |
3474 | { | |
3475 | fun->rodata->linker_mark = 1; | |
3476 | fun->rodata->gc_mark = 1; | |
3477 | fun->rodata->flags &= ~SEC_CODE; | |
3478 | } | |
9dcc4794 AM |
3479 | } |
3480 | free (name); | |
3481 | } | |
9dcc4794 | 3482 | } |
4f0d75be AM |
3483 | if (mos_param->max_overlay_size < size) |
3484 | mos_param->max_overlay_size = size; | |
9dcc4794 AM |
3485 | } |
3486 | ||
3487 | for (count = 0, call = fun->call_list; call != NULL; call = call->next) | |
3488 | count += 1; | |
3489 | ||
3490 | if (count > 1) | |
3491 | { | |
3492 | struct call_info **calls = bfd_malloc (count * sizeof (*calls)); | |
3493 | if (calls == NULL) | |
3494 | return FALSE; | |
3495 | ||
3496 | for (count = 0, call = fun->call_list; call != NULL; call = call->next) | |
3497 | calls[count++] = call; | |
3498 | ||
3499 | qsort (calls, count, sizeof (*calls), sort_calls); | |
3500 | ||
3501 | fun->call_list = NULL; | |
3502 | while (count != 0) | |
3503 | { | |
3504 | --count; | |
3505 | calls[count]->next = fun->call_list; | |
3506 | fun->call_list = calls[count]; | |
3507 | } | |
3508 | free (calls); | |
3509 | } | |
3510 | ||
3511 | for (call = fun->call_list; call != NULL; call = call->next) | |
3512 | { | |
3513 | if (call->is_pasted) | |
3514 | { | |
3515 | /* There can only be one is_pasted call per function_info. */ | |
3516 | BFD_ASSERT (!fun->sec->segment_mark); | |
3517 | fun->sec->segment_mark = 1; | |
3518 | } | |
5ebe2858 UW |
3519 | if (!call->broken_cycle |
3520 | && !mark_overlay_section (call->fun, info, param)) | |
9dcc4794 AM |
3521 | return FALSE; |
3522 | } | |
3523 | ||
3524 | /* Don't put entry code into an overlay. The overlay manager needs | |
cd4a7468 | 3525 | a stack! Also, don't mark .ovl.init as an overlay. */ |
9dcc4794 | 3526 | if (fun->lo + fun->sec->output_offset + fun->sec->output_section->vma |
cd4a7468 AM |
3527 | == info->output_bfd->start_address |
3528 | || strncmp (fun->sec->output_section->name, ".ovl.init", 9) == 0) | |
9dcc4794 AM |
3529 | { |
3530 | fun->sec->linker_mark = 0; | |
3531 | if (fun->rodata != NULL) | |
3532 | fun->rodata->linker_mark = 0; | |
3533 | } | |
3534 | return TRUE; | |
3535 | } | |
3536 | ||
99302af9 AM |
3537 | /* If non-zero then unmark functions called from those within sections |
3538 | that we need to unmark. Unfortunately this isn't reliable since the | |
3539 | call graph cannot know the destination of function pointer calls. */ | |
3540 | #define RECURSE_UNMARK 0 | |
3541 | ||
9dcc4794 AM |
3542 | struct _uos_param { |
3543 | asection *exclude_input_section; | |
3544 | asection *exclude_output_section; | |
3545 | unsigned long clearing; | |
3546 | }; | |
3547 | ||
3548 | /* Undo some of mark_overlay_section's work. */ | |
3549 | ||
3550 | static bfd_boolean | |
3551 | unmark_overlay_section (struct function_info *fun, | |
3552 | struct bfd_link_info *info, | |
3553 | void *param) | |
3554 | { | |
3555 | struct call_info *call; | |
3556 | struct _uos_param *uos_param = param; | |
3557 | unsigned int excluded = 0; | |
3558 | ||
3559 | if (fun->visit5) | |
3560 | return TRUE; | |
3561 | ||
3562 | fun->visit5 = TRUE; | |
3563 | ||
3564 | excluded = 0; | |
3565 | if (fun->sec == uos_param->exclude_input_section | |
3566 | || fun->sec->output_section == uos_param->exclude_output_section) | |
3567 | excluded = 1; | |
3568 | ||
99302af9 AM |
3569 | if (RECURSE_UNMARK) |
3570 | uos_param->clearing += excluded; | |
9dcc4794 | 3571 | |
99302af9 | 3572 | if (RECURSE_UNMARK ? uos_param->clearing : excluded) |
9dcc4794 AM |
3573 | { |
3574 | fun->sec->linker_mark = 0; | |
3575 | if (fun->rodata) | |
3576 | fun->rodata->linker_mark = 0; | |
3577 | } | |
3578 | ||
3579 | for (call = fun->call_list; call != NULL; call = call->next) | |
5ebe2858 UW |
3580 | if (!call->broken_cycle |
3581 | && !unmark_overlay_section (call->fun, info, param)) | |
9dcc4794 AM |
3582 | return FALSE; |
3583 | ||
99302af9 AM |
3584 | if (RECURSE_UNMARK) |
3585 | uos_param->clearing -= excluded; | |
9dcc4794 AM |
3586 | return TRUE; |
3587 | } | |
3588 | ||
3589 | struct _cl_param { | |
3590 | unsigned int lib_size; | |
3591 | asection **lib_sections; | |
3592 | }; | |
3593 | ||
3594 | /* Add sections we have marked as belonging to overlays to an array | |
3595 | for consideration as non-overlay sections. The array consist of | |
3596 | pairs of sections, (text,rodata), for functions in the call graph. */ | |
3597 | ||
3598 | static bfd_boolean | |
3599 | collect_lib_sections (struct function_info *fun, | |
3600 | struct bfd_link_info *info, | |
3601 | void *param) | |
3602 | { | |
3603 | struct _cl_param *lib_param = param; | |
3604 | struct call_info *call; | |
3605 | unsigned int size; | |
3606 | ||
3607 | if (fun->visit6) | |
3608 | return TRUE; | |
3609 | ||
3610 | fun->visit6 = TRUE; | |
3611 | if (!fun->sec->linker_mark || !fun->sec->gc_mark || fun->sec->segment_mark) | |
3612 | return TRUE; | |
3613 | ||
3614 | size = fun->sec->size; | |
3615 | if (fun->rodata) | |
3616 | size += fun->rodata->size; | |
cd4a7468 | 3617 | |
b0c41709 | 3618 | if (size <= lib_param->lib_size) |
9dcc4794 | 3619 | { |
b0c41709 AM |
3620 | *lib_param->lib_sections++ = fun->sec; |
3621 | fun->sec->gc_mark = 0; | |
3622 | if (fun->rodata && fun->rodata->linker_mark && fun->rodata->gc_mark) | |
3623 | { | |
3624 | *lib_param->lib_sections++ = fun->rodata; | |
3625 | fun->rodata->gc_mark = 0; | |
3626 | } | |
3627 | else | |
3628 | *lib_param->lib_sections++ = NULL; | |
9dcc4794 | 3629 | } |
9dcc4794 AM |
3630 | |
3631 | for (call = fun->call_list; call != NULL; call = call->next) | |
5ebe2858 UW |
3632 | if (!call->broken_cycle) |
3633 | collect_lib_sections (call->fun, info, param); | |
9dcc4794 AM |
3634 | |
3635 | return TRUE; | |
3636 | } | |
3637 | ||
3638 | /* qsort predicate to sort sections by call count. */ | |
3639 | ||
3640 | static int | |
3641 | sort_lib (const void *a, const void *b) | |
3642 | { | |
3643 | asection *const *s1 = a; | |
3644 | asection *const *s2 = b; | |
3645 | struct _spu_elf_section_data *sec_data; | |
3646 | struct spu_elf_stack_info *sinfo; | |
3647 | int delta; | |
3648 | ||
3649 | delta = 0; | |
3650 | if ((sec_data = spu_elf_section_data (*s1)) != NULL | |
3651 | && (sinfo = sec_data->u.i.stack_info) != NULL) | |
3652 | { | |
3653 | int i; | |
3654 | for (i = 0; i < sinfo->num_fun; ++i) | |
3655 | delta -= sinfo->fun[i].call_count; | |
3656 | } | |
3657 | ||
3658 | if ((sec_data = spu_elf_section_data (*s2)) != NULL | |
3659 | && (sinfo = sec_data->u.i.stack_info) != NULL) | |
3660 | { | |
3661 | int i; | |
3662 | for (i = 0; i < sinfo->num_fun; ++i) | |
3663 | delta += sinfo->fun[i].call_count; | |
3664 | } | |
3665 | ||
3666 | if (delta != 0) | |
3667 | return delta; | |
3668 | ||
3669 | return s1 - s2; | |
3670 | } | |
3671 | ||
3672 | /* Remove some sections from those marked to be in overlays. Choose | |
3673 | those that are called from many places, likely library functions. */ | |
3674 | ||
3675 | static unsigned int | |
3676 | auto_ovl_lib_functions (struct bfd_link_info *info, unsigned int lib_size) | |
3677 | { | |
3678 | bfd *ibfd; | |
3679 | asection **lib_sections; | |
3680 | unsigned int i, lib_count; | |
3681 | struct _cl_param collect_lib_param; | |
3682 | struct function_info dummy_caller; | |
64615358 | 3683 | struct spu_link_hash_table *htab; |
9dcc4794 AM |
3684 | |
3685 | memset (&dummy_caller, 0, sizeof (dummy_caller)); | |
3686 | lib_count = 0; | |
3687 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
3688 | { | |
3689 | extern const bfd_target bfd_elf32_spu_vec; | |
3690 | asection *sec; | |
3691 | ||
3692 | if (ibfd->xvec != &bfd_elf32_spu_vec) | |
3693 | continue; | |
3694 | ||
3695 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
3696 | if (sec->linker_mark | |
3697 | && sec->size < lib_size | |
3698 | && (sec->flags & SEC_CODE) != 0) | |
3699 | lib_count += 1; | |
3700 | } | |
3701 | lib_sections = bfd_malloc (lib_count * 2 * sizeof (*lib_sections)); | |
3702 | if (lib_sections == NULL) | |
3703 | return (unsigned int) -1; | |
3704 | collect_lib_param.lib_size = lib_size; | |
3705 | collect_lib_param.lib_sections = lib_sections; | |
3706 | if (!for_each_node (collect_lib_sections, info, &collect_lib_param, | |
3707 | TRUE)) | |
3708 | return (unsigned int) -1; | |
3709 | lib_count = (collect_lib_param.lib_sections - lib_sections) / 2; | |
3710 | ||
3711 | /* Sort sections so that those with the most calls are first. */ | |
3712 | if (lib_count > 1) | |
3713 | qsort (lib_sections, lib_count, 2 * sizeof (*lib_sections), sort_lib); | |
3714 | ||
64615358 | 3715 | htab = spu_hash_table (info); |
9dcc4794 AM |
3716 | for (i = 0; i < lib_count; i++) |
3717 | { | |
3718 | unsigned int tmp, stub_size; | |
3719 | asection *sec; | |
3720 | struct _spu_elf_section_data *sec_data; | |
3721 | struct spu_elf_stack_info *sinfo; | |
3722 | ||
3723 | sec = lib_sections[2 * i]; | |
3724 | /* If this section is OK, its size must be less than lib_size. */ | |
3725 | tmp = sec->size; | |
3726 | /* If it has a rodata section, then add that too. */ | |
3727 | if (lib_sections[2 * i + 1]) | |
3728 | tmp += lib_sections[2 * i + 1]->size; | |
3729 | /* Add any new overlay call stubs needed by the section. */ | |
3730 | stub_size = 0; | |
3731 | if (tmp < lib_size | |
3732 | && (sec_data = spu_elf_section_data (sec)) != NULL | |
3733 | && (sinfo = sec_data->u.i.stack_info) != NULL) | |
3734 | { | |
3735 | int k; | |
3736 | struct call_info *call; | |
3737 | ||
3738 | for (k = 0; k < sinfo->num_fun; ++k) | |
3739 | for (call = sinfo->fun[k].call_list; call; call = call->next) | |
3740 | if (call->fun->sec->linker_mark) | |
3741 | { | |
3742 | struct call_info *p; | |
3743 | for (p = dummy_caller.call_list; p; p = p->next) | |
3744 | if (p->fun == call->fun) | |
3745 | break; | |
3746 | if (!p) | |
a7e11ee1 | 3747 | stub_size += ovl_stub_size (htab->params); |
9dcc4794 AM |
3748 | } |
3749 | } | |
3750 | if (tmp + stub_size < lib_size) | |
3751 | { | |
3752 | struct call_info **pp, *p; | |
3753 | ||
3754 | /* This section fits. Mark it as non-overlay. */ | |
3755 | lib_sections[2 * i]->linker_mark = 0; | |
3756 | if (lib_sections[2 * i + 1]) | |
3757 | lib_sections[2 * i + 1]->linker_mark = 0; | |
3758 | lib_size -= tmp + stub_size; | |
3759 | /* Call stubs to the section we just added are no longer | |
3760 | needed. */ | |
3761 | pp = &dummy_caller.call_list; | |
3762 | while ((p = *pp) != NULL) | |
3763 | if (!p->fun->sec->linker_mark) | |
3764 | { | |
a7e11ee1 | 3765 | lib_size += ovl_stub_size (htab->params); |
9dcc4794 AM |
3766 | *pp = p->next; |
3767 | free (p); | |
3768 | } | |
3769 | else | |
3770 | pp = &p->next; | |
3771 | /* Add new call stubs to dummy_caller. */ | |
3772 | if ((sec_data = spu_elf_section_data (sec)) != NULL | |
3773 | && (sinfo = sec_data->u.i.stack_info) != NULL) | |
3774 | { | |
3775 | int k; | |
3776 | struct call_info *call; | |
3777 | ||
3778 | for (k = 0; k < sinfo->num_fun; ++k) | |
3779 | for (call = sinfo->fun[k].call_list; | |
3780 | call; | |
3781 | call = call->next) | |
3782 | if (call->fun->sec->linker_mark) | |
3783 | { | |
3784 | struct call_info *callee; | |
3785 | callee = bfd_malloc (sizeof (*callee)); | |
3786 | if (callee == NULL) | |
3787 | return (unsigned int) -1; | |
3788 | *callee = *call; | |
3789 | if (!insert_callee (&dummy_caller, callee)) | |
3790 | free (callee); | |
3791 | } | |
3792 | } | |
3793 | } | |
3794 | } | |
3795 | while (dummy_caller.call_list != NULL) | |
3796 | { | |
3797 | struct call_info *call = dummy_caller.call_list; | |
3798 | dummy_caller.call_list = call->next; | |
3799 | free (call); | |
3800 | } | |
3801 | for (i = 0; i < 2 * lib_count; i++) | |
3802 | if (lib_sections[i]) | |
3803 | lib_sections[i]->gc_mark = 1; | |
3804 | free (lib_sections); | |
3805 | return lib_size; | |
3806 | } | |
3807 | ||
3808 | /* Build an array of overlay sections. The deepest node's section is | |
2ec9638b | 3809 | added first, then its parent node's section, then everything called |
9dcc4794 AM |
3810 | from the parent section. The idea being to group sections to |
3811 | minimise calls between different overlays. */ | |
3812 | ||
3813 | static bfd_boolean | |
3814 | collect_overlays (struct function_info *fun, | |
3815 | struct bfd_link_info *info, | |
3816 | void *param) | |
3817 | { | |
3818 | struct call_info *call; | |
3819 | bfd_boolean added_fun; | |
3820 | asection ***ovly_sections = param; | |
3821 | ||
3822 | if (fun->visit7) | |
3823 | return TRUE; | |
3824 | ||
3825 | fun->visit7 = TRUE; | |
3826 | for (call = fun->call_list; call != NULL; call = call->next) | |
5ebe2858 | 3827 | if (!call->is_pasted && !call->broken_cycle) |
9dcc4794 AM |
3828 | { |
3829 | if (!collect_overlays (call->fun, info, ovly_sections)) | |
3830 | return FALSE; | |
3831 | break; | |
3832 | } | |
3833 | ||
3834 | added_fun = FALSE; | |
3835 | if (fun->sec->linker_mark && fun->sec->gc_mark) | |
3836 | { | |
3837 | fun->sec->gc_mark = 0; | |
3838 | *(*ovly_sections)++ = fun->sec; | |
3839 | if (fun->rodata && fun->rodata->linker_mark && fun->rodata->gc_mark) | |
3840 | { | |
3841 | fun->rodata->gc_mark = 0; | |
3842 | *(*ovly_sections)++ = fun->rodata; | |
3843 | } | |
3844 | else | |
3845 | *(*ovly_sections)++ = NULL; | |
3846 | added_fun = TRUE; | |
3847 | ||
3848 | /* Pasted sections must stay with the first section. We don't | |
3849 | put pasted sections in the array, just the first section. | |
3850 | Mark subsequent sections as already considered. */ | |
3851 | if (fun->sec->segment_mark) | |
3852 | { | |
3853 | struct function_info *call_fun = fun; | |
3854 | do | |
3855 | { | |
3856 | for (call = call_fun->call_list; call != NULL; call = call->next) | |
3857 | if (call->is_pasted) | |
3858 | { | |
3859 | call_fun = call->fun; | |
3860 | call_fun->sec->gc_mark = 0; | |
3861 | if (call_fun->rodata) | |
3862 | call_fun->rodata->gc_mark = 0; | |
3863 | break; | |
3864 | } | |
3865 | if (call == NULL) | |
3866 | abort (); | |
3867 | } | |
3868 | while (call_fun->sec->segment_mark); | |
3869 | } | |
3870 | } | |
3871 | ||
3872 | for (call = fun->call_list; call != NULL; call = call->next) | |
5ebe2858 UW |
3873 | if (!call->broken_cycle |
3874 | && !collect_overlays (call->fun, info, ovly_sections)) | |
9dcc4794 AM |
3875 | return FALSE; |
3876 | ||
3877 | if (added_fun) | |
3878 | { | |
3879 | struct _spu_elf_section_data *sec_data; | |
3880 | struct spu_elf_stack_info *sinfo; | |
3881 | ||
3882 | if ((sec_data = spu_elf_section_data (fun->sec)) != NULL | |
3883 | && (sinfo = sec_data->u.i.stack_info) != NULL) | |
3884 | { | |
3885 | int i; | |
3886 | for (i = 0; i < sinfo->num_fun; ++i) | |
3887 | if (!collect_overlays (&sinfo->fun[i], info, ovly_sections)) | |
3888 | return FALSE; | |
3889 | } | |
3890 | } | |
3891 | ||
3892 | return TRUE; | |
49fa1e15 AM |
3893 | } |
3894 | ||
055ed83b AM |
3895 | struct _sum_stack_param { |
3896 | size_t cum_stack; | |
3897 | size_t overall_stack; | |
3898 | bfd_boolean emit_stack_syms; | |
3899 | }; | |
3900 | ||
49fa1e15 AM |
3901 | /* Descend the call graph for FUN, accumulating total stack required. */ |
3902 | ||
055ed83b | 3903 | static bfd_boolean |
49fa1e15 AM |
3904 | sum_stack (struct function_info *fun, |
3905 | struct bfd_link_info *info, | |
055ed83b | 3906 | void *param) |
49fa1e15 AM |
3907 | { |
3908 | struct call_info *call; | |
055ed83b AM |
3909 | struct function_info *max; |
3910 | size_t stack, cum_stack; | |
49fa1e15 | 3911 | const char *f1; |
9dcc4794 | 3912 | bfd_boolean has_call; |
055ed83b | 3913 | struct _sum_stack_param *sum_stack_param = param; |
9dcc4794 | 3914 | struct spu_link_hash_table *htab; |
49fa1e15 | 3915 | |
055ed83b AM |
3916 | cum_stack = fun->stack; |
3917 | sum_stack_param->cum_stack = cum_stack; | |
49fa1e15 | 3918 | if (fun->visit3) |
055ed83b | 3919 | return TRUE; |
49fa1e15 | 3920 | |
9dcc4794 | 3921 | has_call = FALSE; |
055ed83b | 3922 | max = NULL; |
49fa1e15 AM |
3923 | for (call = fun->call_list; call; call = call->next) |
3924 | { | |
5ebe2858 UW |
3925 | if (call->broken_cycle) |
3926 | continue; | |
9dcc4794 AM |
3927 | if (!call->is_pasted) |
3928 | has_call = TRUE; | |
055ed83b AM |
3929 | if (!sum_stack (call->fun, info, sum_stack_param)) |
3930 | return FALSE; | |
3931 | stack = sum_stack_param->cum_stack; | |
49fa1e15 AM |
3932 | /* Include caller stack for normal calls, don't do so for |
3933 | tail calls. fun->stack here is local stack usage for | |
3934 | this function. */ | |
9dcc4794 | 3935 | if (!call->is_tail || call->is_pasted || call->fun->start != NULL) |
49fa1e15 | 3936 | stack += fun->stack; |
055ed83b | 3937 | if (cum_stack < stack) |
49fa1e15 | 3938 | { |
055ed83b | 3939 | cum_stack = stack; |
49fa1e15 AM |
3940 | max = call->fun; |
3941 | } | |
3942 | } | |
3943 | ||
055ed83b AM |
3944 | sum_stack_param->cum_stack = cum_stack; |
3945 | stack = fun->stack; | |
3946 | /* Now fun->stack holds cumulative stack. */ | |
3947 | fun->stack = cum_stack; | |
3948 | fun->visit3 = TRUE; | |
3949 | ||
3950 | if (!fun->non_root | |
3951 | && sum_stack_param->overall_stack < cum_stack) | |
3952 | sum_stack_param->overall_stack = cum_stack; | |
3953 | ||
9dcc4794 | 3954 | htab = spu_hash_table (info); |
64615358 | 3955 | if (htab->params->auto_overlay) |
9dcc4794 AM |
3956 | return TRUE; |
3957 | ||
49fa1e15 | 3958 | f1 = func_name (fun); |
cd4a7468 | 3959 | if (htab->params->stack_analysis) |
49fa1e15 | 3960 | { |
cd4a7468 AM |
3961 | if (!fun->non_root) |
3962 | info->callbacks->info (_(" %s: 0x%v\n"), f1, (bfd_vma) cum_stack); | |
3963 | info->callbacks->minfo (_("%s: 0x%v 0x%v\n"), | |
3964 | f1, (bfd_vma) stack, (bfd_vma) cum_stack); | |
49fa1e15 | 3965 | |
cd4a7468 AM |
3966 | if (has_call) |
3967 | { | |
3968 | info->callbacks->minfo (_(" calls:\n")); | |
3969 | for (call = fun->call_list; call; call = call->next) | |
5ebe2858 | 3970 | if (!call->is_pasted && !call->broken_cycle) |
cd4a7468 AM |
3971 | { |
3972 | const char *f2 = func_name (call->fun); | |
3973 | const char *ann1 = call->fun == max ? "*" : " "; | |
3974 | const char *ann2 = call->is_tail ? "t" : " "; | |
3975 | ||
3976 | info->callbacks->minfo (_(" %s%s %s\n"), ann1, ann2, f2); | |
3977 | } | |
3978 | } | |
49fa1e15 AM |
3979 | } |
3980 | ||
055ed83b | 3981 | if (sum_stack_param->emit_stack_syms) |
49fa1e15 | 3982 | { |
49fa1e15 AM |
3983 | char *name = bfd_malloc (18 + strlen (f1)); |
3984 | struct elf_link_hash_entry *h; | |
3985 | ||
055ed83b AM |
3986 | if (name == NULL) |
3987 | return FALSE; | |
3988 | ||
3989 | if (fun->global || ELF_ST_BIND (fun->u.sym->st_info) == STB_GLOBAL) | |
3990 | sprintf (name, "__stack_%s", f1); | |
3991 | else | |
3992 | sprintf (name, "__stack_%x_%s", fun->sec->id & 0xffffffff, f1); | |
3993 | ||
3994 | h = elf_link_hash_lookup (&htab->elf, name, TRUE, TRUE, FALSE); | |
3995 | free (name); | |
3996 | if (h != NULL | |
3997 | && (h->root.type == bfd_link_hash_new | |
3998 | || h->root.type == bfd_link_hash_undefined | |
3999 | || h->root.type == bfd_link_hash_undefweak)) | |
49fa1e15 | 4000 | { |
055ed83b AM |
4001 | h->root.type = bfd_link_hash_defined; |
4002 | h->root.u.def.section = bfd_abs_section_ptr; | |
4003 | h->root.u.def.value = cum_stack; | |
4004 | h->size = 0; | |
4005 | h->type = 0; | |
4006 | h->ref_regular = 1; | |
4007 | h->def_regular = 1; | |
4008 | h->ref_regular_nonweak = 1; | |
4009 | h->forced_local = 1; | |
4010 | h->non_elf = 0; | |
49fa1e15 AM |
4011 | } |
4012 | } | |
4013 | ||
055ed83b | 4014 | return TRUE; |
49fa1e15 AM |
4015 | } |
4016 | ||
9dcc4794 AM |
4017 | /* SEC is part of a pasted function. Return the call_info for the |
4018 | next section of this function. */ | |
4019 | ||
4020 | static struct call_info * | |
4021 | find_pasted_call (asection *sec) | |
4022 | { | |
4023 | struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec); | |
4024 | struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info; | |
4025 | struct call_info *call; | |
4026 | int k; | |
4027 | ||
4028 | for (k = 0; k < sinfo->num_fun; ++k) | |
4029 | for (call = sinfo->fun[k].call_list; call != NULL; call = call->next) | |
4030 | if (call->is_pasted) | |
4031 | return call; | |
4032 | abort (); | |
4033 | return 0; | |
4034 | } | |
4035 | ||
4036 | /* qsort predicate to sort bfds by file name. */ | |
4037 | ||
4038 | static int | |
4039 | sort_bfds (const void *a, const void *b) | |
4040 | { | |
4041 | bfd *const *abfd1 = a; | |
4042 | bfd *const *abfd2 = b; | |
4043 | ||
4044 | return strcmp ((*abfd1)->filename, (*abfd2)->filename); | |
4045 | } | |
4046 | ||
cd4a7468 AM |
4047 | static unsigned int |
4048 | print_one_overlay_section (FILE *script, | |
4049 | unsigned int base, | |
4050 | unsigned int count, | |
4051 | unsigned int ovlynum, | |
4052 | unsigned int *ovly_map, | |
4053 | asection **ovly_sections, | |
4054 | struct bfd_link_info *info) | |
4055 | { | |
4056 | unsigned int j; | |
4057 | ||
4058 | for (j = base; j < count && ovly_map[j] == ovlynum; j++) | |
4059 | { | |
4060 | asection *sec = ovly_sections[2 * j]; | |
4061 | ||
4062 | if (fprintf (script, " %s%c%s (%s)\n", | |
4063 | (sec->owner->my_archive != NULL | |
4064 | ? sec->owner->my_archive->filename : ""), | |
4065 | info->path_separator, | |
4066 | sec->owner->filename, | |
4067 | sec->name) <= 0) | |
4068 | return -1; | |
4069 | if (sec->segment_mark) | |
4070 | { | |
4071 | struct call_info *call = find_pasted_call (sec); | |
4072 | while (call != NULL) | |
4073 | { | |
4074 | struct function_info *call_fun = call->fun; | |
4075 | sec = call_fun->sec; | |
4076 | if (fprintf (script, " %s%c%s (%s)\n", | |
4077 | (sec->owner->my_archive != NULL | |
4078 | ? sec->owner->my_archive->filename : ""), | |
4079 | info->path_separator, | |
4080 | sec->owner->filename, | |
4081 | sec->name) <= 0) | |
4082 | return -1; | |
4083 | for (call = call_fun->call_list; call; call = call->next) | |
4084 | if (call->is_pasted) | |
4085 | break; | |
4086 | } | |
4087 | } | |
4088 | } | |
4089 | ||
4090 | for (j = base; j < count && ovly_map[j] == ovlynum; j++) | |
4091 | { | |
4092 | asection *sec = ovly_sections[2 * j + 1]; | |
4093 | if (sec != NULL | |
4094 | && fprintf (script, " %s%c%s (%s)\n", | |
4095 | (sec->owner->my_archive != NULL | |
4096 | ? sec->owner->my_archive->filename : ""), | |
4097 | info->path_separator, | |
4098 | sec->owner->filename, | |
4099 | sec->name) <= 0) | |
4100 | return -1; | |
4101 | ||
4102 | sec = ovly_sections[2 * j]; | |
4103 | if (sec->segment_mark) | |
4104 | { | |
4105 | struct call_info *call = find_pasted_call (sec); | |
4106 | while (call != NULL) | |
4107 | { | |
4108 | struct function_info *call_fun = call->fun; | |
4109 | sec = call_fun->rodata; | |
4110 | if (sec != NULL | |
4111 | && fprintf (script, " %s%c%s (%s)\n", | |
4112 | (sec->owner->my_archive != NULL | |
4113 | ? sec->owner->my_archive->filename : ""), | |
4114 | info->path_separator, | |
4115 | sec->owner->filename, | |
4116 | sec->name) <= 0) | |
4117 | return -1; | |
4118 | for (call = call_fun->call_list; call; call = call->next) | |
4119 | if (call->is_pasted) | |
4120 | break; | |
4121 | } | |
4122 | } | |
4123 | } | |
4124 | ||
4125 | return j; | |
4126 | } | |
4127 | ||
9dcc4794 AM |
4128 | /* Handle --auto-overlay. */ |
4129 | ||
9dcc4794 | 4130 | static void |
64615358 | 4131 | spu_elf_auto_overlay (struct bfd_link_info *info) |
9dcc4794 AM |
4132 | { |
4133 | bfd *ibfd; | |
4134 | bfd **bfd_arr; | |
4135 | struct elf_segment_map *m; | |
4136 | unsigned int fixed_size, lo, hi; | |
4137 | struct spu_link_hash_table *htab; | |
4138 | unsigned int base, i, count, bfd_count; | |
a3a219a9 | 4139 | unsigned int region, ovlynum; |
9dcc4794 | 4140 | asection **ovly_sections, **ovly_p; |
a3a219a9 | 4141 | unsigned int *ovly_map; |
9dcc4794 AM |
4142 | FILE *script; |
4143 | unsigned int total_overlay_size, overlay_size; | |
cd4a7468 | 4144 | const char *ovly_mgr_entry; |
9dcc4794 AM |
4145 | struct elf_link_hash_entry *h; |
4146 | struct _mos_param mos_param; | |
4147 | struct _uos_param uos_param; | |
4148 | struct function_info dummy_caller; | |
4149 | ||
4150 | /* Find the extents of our loadable image. */ | |
4151 | lo = (unsigned int) -1; | |
4152 | hi = 0; | |
4153 | for (m = elf_tdata (info->output_bfd)->segment_map; m != NULL; m = m->next) | |
4154 | if (m->p_type == PT_LOAD) | |
4155 | for (i = 0; i < m->count; i++) | |
4156 | if (m->sections[i]->size != 0) | |
4157 | { | |
4158 | if (m->sections[i]->vma < lo) | |
4159 | lo = m->sections[i]->vma; | |
4160 | if (m->sections[i]->vma + m->sections[i]->size - 1 > hi) | |
4161 | hi = m->sections[i]->vma + m->sections[i]->size - 1; | |
4162 | } | |
4163 | fixed_size = hi + 1 - lo; | |
4164 | ||
4165 | if (!discover_functions (info)) | |
4166 | goto err_exit; | |
4167 | ||
4168 | if (!build_call_tree (info)) | |
4169 | goto err_exit; | |
4170 | ||
a14c90c1 UW |
4171 | htab = spu_hash_table (info); |
4172 | if (htab->reserved == 0) | |
4173 | { | |
4174 | struct _sum_stack_param sum_stack_param; | |
4175 | ||
4176 | sum_stack_param.emit_stack_syms = 0; | |
4177 | sum_stack_param.overall_stack = 0; | |
4178 | if (!for_each_node (sum_stack, info, &sum_stack_param, TRUE)) | |
4179 | goto err_exit; | |
4180 | htab->reserved = sum_stack_param.overall_stack + htab->extra_stack_space; | |
4181 | } | |
4182 | ||
4183 | /* No need for overlays if everything already fits. */ | |
4184 | if (fixed_size + htab->reserved <= htab->local_store | |
4185 | && htab->params->ovly_flavour != ovly_soft_icache) | |
4186 | { | |
4187 | htab->params->auto_overlay = 0; | |
4188 | return; | |
4189 | } | |
4190 | ||
9dcc4794 AM |
4191 | uos_param.exclude_input_section = 0; |
4192 | uos_param.exclude_output_section | |
4193 | = bfd_get_section_by_name (info->output_bfd, ".interrupt"); | |
4194 | ||
cd4a7468 AM |
4195 | ovly_mgr_entry = "__ovly_load"; |
4196 | if (htab->params->ovly_flavour == ovly_soft_icache) | |
4197 | ovly_mgr_entry = "__icache_br_handler"; | |
4198 | h = elf_link_hash_lookup (&htab->elf, ovly_mgr_entry, | |
9dcc4794 AM |
4199 | FALSE, FALSE, FALSE); |
4200 | if (h != NULL | |
4201 | && (h->root.type == bfd_link_hash_defined | |
4202 | || h->root.type == bfd_link_hash_defweak) | |
4203 | && h->def_regular) | |
4204 | { | |
4205 | /* We have a user supplied overlay manager. */ | |
4206 | uos_param.exclude_input_section = h->root.u.def.section; | |
4207 | } | |
4208 | else | |
4209 | { | |
4210 | /* If no user overlay manager, spu_elf_load_ovl_mgr will add our | |
4211 | builtin version to .text, and will adjust .text size. */ | |
64615358 | 4212 | fixed_size += (*htab->params->spu_elf_load_ovl_mgr) (); |
9dcc4794 AM |
4213 | } |
4214 | ||
4215 | /* Mark overlay sections, and find max overlay section size. */ | |
4216 | mos_param.max_overlay_size = 0; | |
4217 | if (!for_each_node (mark_overlay_section, info, &mos_param, TRUE)) | |
4218 | goto err_exit; | |
4219 | ||
4220 | /* We can't put the overlay manager or interrupt routines in | |
4221 | overlays. */ | |
4222 | uos_param.clearing = 0; | |
4223 | if ((uos_param.exclude_input_section | |
4224 | || uos_param.exclude_output_section) | |
4225 | && !for_each_node (unmark_overlay_section, info, &uos_param, TRUE)) | |
4226 | goto err_exit; | |
4227 | ||
4228 | bfd_count = 0; | |
4229 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
4230 | ++bfd_count; | |
4231 | bfd_arr = bfd_malloc (bfd_count * sizeof (*bfd_arr)); | |
4232 | if (bfd_arr == NULL) | |
4233 | goto err_exit; | |
4234 | ||
4235 | /* Count overlay sections, and subtract their sizes from "fixed_size". */ | |
4236 | count = 0; | |
4237 | bfd_count = 0; | |
4238 | total_overlay_size = 0; | |
4239 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
4240 | { | |
4241 | extern const bfd_target bfd_elf32_spu_vec; | |
4242 | asection *sec; | |
4243 | unsigned int old_count; | |
4244 | ||
4245 | if (ibfd->xvec != &bfd_elf32_spu_vec) | |
4246 | continue; | |
4247 | ||
4248 | old_count = count; | |
4249 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
4250 | if (sec->linker_mark) | |
4251 | { | |
4252 | if ((sec->flags & SEC_CODE) != 0) | |
4253 | count += 1; | |
4254 | fixed_size -= sec->size; | |
4255 | total_overlay_size += sec->size; | |
4256 | } | |
cd4a7468 AM |
4257 | else if ((sec->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD) |
4258 | && sec->output_section->owner == info->output_bfd | |
4259 | && strncmp (sec->output_section->name, ".ovl.init", 9) == 0) | |
4260 | fixed_size -= sec->size; | |
9dcc4794 AM |
4261 | if (count != old_count) |
4262 | bfd_arr[bfd_count++] = ibfd; | |
4263 | } | |
4264 | ||
4265 | /* Since the overlay link script selects sections by file name and | |
4266 | section name, ensure that file names are unique. */ | |
4267 | if (bfd_count > 1) | |
4268 | { | |
4269 | bfd_boolean ok = TRUE; | |
4270 | ||
4271 | qsort (bfd_arr, bfd_count, sizeof (*bfd_arr), sort_bfds); | |
4272 | for (i = 1; i < bfd_count; ++i) | |
4273 | if (strcmp (bfd_arr[i - 1]->filename, bfd_arr[i]->filename) == 0) | |
4274 | { | |
97407faf | 4275 | if (bfd_arr[i - 1]->my_archive == bfd_arr[i]->my_archive) |
9dcc4794 | 4276 | { |
97407faf | 4277 | if (bfd_arr[i - 1]->my_archive && bfd_arr[i]->my_archive) |
9dcc4794 | 4278 | info->callbacks->einfo (_("%s duplicated in %s\n"), |
97407faf | 4279 | bfd_arr[i]->filename, |
9dcc4794 | 4280 | bfd_arr[i]->my_archive->filename); |
97407faf AM |
4281 | else |
4282 | info->callbacks->einfo (_("%s duplicated\n"), | |
4283 | bfd_arr[i]->filename); | |
4284 | ok = FALSE; | |
9dcc4794 | 4285 | } |
9dcc4794 AM |
4286 | } |
4287 | if (!ok) | |
4288 | { | |
9dcc4794 AM |
4289 | info->callbacks->einfo (_("sorry, no support for duplicate " |
4290 | "object files in auto-overlay script\n")); | |
4291 | bfd_set_error (bfd_error_bad_value); | |
4292 | goto err_exit; | |
4293 | } | |
4294 | } | |
4295 | free (bfd_arr); | |
4296 | ||
9dcc4794 | 4297 | fixed_size += htab->reserved; |
a7e11ee1 | 4298 | fixed_size += htab->non_ovly_stub * ovl_stub_size (htab->params); |
9dcc4794 AM |
4299 | if (fixed_size + mos_param.max_overlay_size <= htab->local_store) |
4300 | { | |
cd4a7468 AM |
4301 | if (htab->params->ovly_flavour == ovly_soft_icache) |
4302 | { | |
4303 | /* Stubs in the non-icache area are bigger. */ | |
4304 | fixed_size += htab->non_ovly_stub * 16; | |
4305 | /* Space for icache manager tables. | |
4306 | a) Tag array, one quadword per cache line. | |
97fc8d84 UW |
4307 | - word 0: ia address of present line, init to zero. */ |
4308 | fixed_size += 16 << htab->num_lines_log2; | |
4309 | /* b) Rewrite "to" list, one quadword per cache line. */ | |
cd4a7468 | 4310 | fixed_size += 16 << htab->num_lines_log2; |
97fc8d84 UW |
4311 | /* c) Rewrite "from" list, one byte per outgoing branch (rounded up |
4312 | to a power-of-two number of full quadwords) per cache line. */ | |
4313 | fixed_size += 16 << (htab->fromelem_size_log2 | |
4314 | + htab->num_lines_log2); | |
c1ea05bb UW |
4315 | /* d) Pointer to __ea backing store (toe), 1 quadword. */ |
4316 | fixed_size += 16; | |
cd4a7468 AM |
4317 | } |
4318 | else | |
4319 | { | |
4320 | /* Guess number of overlays. Assuming overlay buffer is on | |
4321 | average only half full should be conservative. */ | |
4322 | ovlynum = (total_overlay_size * 2 * htab->params->num_lines | |
4323 | / (htab->local_store - fixed_size)); | |
4324 | /* Space for _ovly_table[], _ovly_buf_table[] and toe. */ | |
4325 | fixed_size += ovlynum * 16 + 16 + 4 + 16; | |
4326 | } | |
9dcc4794 AM |
4327 | } |
4328 | ||
4329 | if (fixed_size + mos_param.max_overlay_size > htab->local_store) | |
4f0d75be AM |
4330 | info->callbacks->einfo (_("non-overlay size of 0x%v plus maximum overlay " |
4331 | "size of 0x%v exceeds local store\n"), | |
4332 | (bfd_vma) fixed_size, | |
4333 | (bfd_vma) mos_param.max_overlay_size); | |
9dcc4794 AM |
4334 | |
4335 | /* Now see if we should put some functions in the non-overlay area. */ | |
e5e6a5ff | 4336 | else if (fixed_size < htab->overlay_fixed) |
9dcc4794 | 4337 | { |
e5e6a5ff AM |
4338 | unsigned int max_fixed, lib_size; |
4339 | ||
4340 | max_fixed = htab->local_store - mos_param.max_overlay_size; | |
4341 | if (max_fixed > htab->overlay_fixed) | |
4342 | max_fixed = htab->overlay_fixed; | |
4343 | lib_size = max_fixed - fixed_size; | |
9dcc4794 AM |
4344 | lib_size = auto_ovl_lib_functions (info, lib_size); |
4345 | if (lib_size == (unsigned int) -1) | |
4346 | goto err_exit; | |
e5e6a5ff | 4347 | fixed_size = max_fixed - lib_size; |
9dcc4794 AM |
4348 | } |
4349 | ||
4350 | /* Build an array of sections, suitably sorted to place into | |
4351 | overlays. */ | |
4352 | ovly_sections = bfd_malloc (2 * count * sizeof (*ovly_sections)); | |
4353 | if (ovly_sections == NULL) | |
4354 | goto err_exit; | |
4355 | ovly_p = ovly_sections; | |
4356 | if (!for_each_node (collect_overlays, info, &ovly_p, TRUE)) | |
4357 | goto err_exit; | |
4358 | count = (size_t) (ovly_p - ovly_sections) / 2; | |
a3a219a9 AM |
4359 | ovly_map = bfd_malloc (count * sizeof (*ovly_map)); |
4360 | if (ovly_map == NULL) | |
4361 | goto err_exit; | |
9dcc4794 AM |
4362 | |
4363 | memset (&dummy_caller, 0, sizeof (dummy_caller)); | |
cd4a7468 AM |
4364 | overlay_size = (htab->local_store - fixed_size) / htab->params->num_lines; |
4365 | if (htab->params->line_size != 0) | |
4366 | overlay_size = htab->params->line_size; | |
9dcc4794 AM |
4367 | base = 0; |
4368 | ovlynum = 0; | |
4369 | while (base < count) | |
4370 | { | |
4371 | unsigned int size = 0; | |
9dcc4794 AM |
4372 | |
4373 | for (i = base; i < count; i++) | |
4374 | { | |
4375 | asection *sec; | |
4376 | unsigned int tmp; | |
64615358 | 4377 | unsigned int num_stubs; |
9dcc4794 AM |
4378 | struct call_info *call, *pasty; |
4379 | struct _spu_elf_section_data *sec_data; | |
4380 | struct spu_elf_stack_info *sinfo; | |
4381 | int k; | |
4382 | ||
4383 | /* See whether we can add this section to the current | |
4384 | overlay without overflowing our overlay buffer. */ | |
4385 | sec = ovly_sections[2 * i]; | |
4386 | tmp = size + sec->size; | |
4387 | if (ovly_sections[2 * i + 1]) | |
4388 | tmp += ovly_sections[2 * i + 1]->size; | |
4389 | if (tmp > overlay_size) | |
4390 | break; | |
4391 | if (sec->segment_mark) | |
4392 | { | |
4393 | /* Pasted sections must stay together, so add their | |
4394 | sizes too. */ | |
4395 | struct call_info *pasty = find_pasted_call (sec); | |
4396 | while (pasty != NULL) | |
4397 | { | |
4398 | struct function_info *call_fun = pasty->fun; | |
4399 | tmp += call_fun->sec->size; | |
4400 | if (call_fun->rodata) | |
4401 | tmp += call_fun->rodata->size; | |
4402 | for (pasty = call_fun->call_list; pasty; pasty = pasty->next) | |
4403 | if (pasty->is_pasted) | |
4404 | break; | |
4405 | } | |
4406 | } | |
4407 | if (tmp > overlay_size) | |
4408 | break; | |
4409 | ||
4410 | /* If we add this section, we might need new overlay call | |
4411 | stubs. Add any overlay section calls to dummy_call. */ | |
4412 | pasty = NULL; | |
4413 | sec_data = spu_elf_section_data (sec); | |
4414 | sinfo = sec_data->u.i.stack_info; | |
4415 | for (k = 0; k < sinfo->num_fun; ++k) | |
4416 | for (call = sinfo->fun[k].call_list; call; call = call->next) | |
4417 | if (call->is_pasted) | |
4418 | { | |
4419 | BFD_ASSERT (pasty == NULL); | |
4420 | pasty = call; | |
4421 | } | |
4422 | else if (call->fun->sec->linker_mark) | |
4423 | { | |
4424 | if (!copy_callee (&dummy_caller, call)) | |
4425 | goto err_exit; | |
4426 | } | |
4427 | while (pasty != NULL) | |
4428 | { | |
4429 | struct function_info *call_fun = pasty->fun; | |
4430 | pasty = NULL; | |
4431 | for (call = call_fun->call_list; call; call = call->next) | |
4432 | if (call->is_pasted) | |
4433 | { | |
4434 | BFD_ASSERT (pasty == NULL); | |
4435 | pasty = call; | |
4436 | } | |
4437 | else if (!copy_callee (&dummy_caller, call)) | |
4438 | goto err_exit; | |
4439 | } | |
4440 | ||
4441 | /* Calculate call stub size. */ | |
64615358 | 4442 | num_stubs = 0; |
9dcc4794 AM |
4443 | for (call = dummy_caller.call_list; call; call = call->next) |
4444 | { | |
4445 | unsigned int k; | |
34ad4cb0 UW |
4446 | unsigned int stub_delta = 1; |
4447 | ||
4448 | if (htab->params->ovly_flavour == ovly_soft_icache) | |
4449 | stub_delta = call->count; | |
4450 | num_stubs += stub_delta; | |
9dcc4794 | 4451 | |
9dcc4794 AM |
4452 | /* If the call is within this overlay, we won't need a |
4453 | stub. */ | |
4454 | for (k = base; k < i + 1; k++) | |
4455 | if (call->fun->sec == ovly_sections[2 * k]) | |
4456 | { | |
34ad4cb0 | 4457 | num_stubs -= stub_delta; |
9dcc4794 AM |
4458 | break; |
4459 | } | |
4460 | } | |
cd4a7468 AM |
4461 | if (htab->params->ovly_flavour == ovly_soft_icache |
4462 | && num_stubs > htab->params->max_branch) | |
4463 | break; | |
a7e11ee1 | 4464 | if (tmp + num_stubs * ovl_stub_size (htab->params) |
64615358 | 4465 | > overlay_size) |
9dcc4794 | 4466 | break; |
9dcc4794 AM |
4467 | size = tmp; |
4468 | } | |
4469 | ||
4470 | if (i == base) | |
4471 | { | |
4472 | info->callbacks->einfo (_("%B:%A%s exceeds overlay size\n"), | |
4473 | ovly_sections[2 * i]->owner, | |
4474 | ovly_sections[2 * i], | |
4475 | ovly_sections[2 * i + 1] ? " + rodata" : ""); | |
4476 | bfd_set_error (bfd_error_bad_value); | |
4477 | goto err_exit; | |
4478 | } | |
4479 | ||
a3a219a9 AM |
4480 | while (dummy_caller.call_list != NULL) |
4481 | { | |
4482 | struct call_info *call = dummy_caller.call_list; | |
4483 | dummy_caller.call_list = call->next; | |
4484 | free (call); | |
4485 | } | |
4486 | ||
4487 | ++ovlynum; | |
4488 | while (base < i) | |
4489 | ovly_map[base++] = ovlynum; | |
4490 | } | |
4491 | ||
4492 | script = htab->params->spu_elf_open_overlay_script (); | |
4493 | ||
4494 | if (fprintf (script, "SECTIONS\n{\n") <= 0) | |
4495 | goto file_err; | |
4496 | ||
cd4a7468 | 4497 | if (htab->params->ovly_flavour == ovly_soft_icache) |
a3a219a9 | 4498 | { |
cd4a7468 AM |
4499 | if (fprintf (script, |
4500 | " .data.icache ALIGN (16) : { *(.ovtab) *(.data.icache) }\n" | |
4501 | " . = ALIGN (%u);\n" | |
4502 | " .ovl.init : { *(.ovl.init) }\n" | |
4503 | " . = ABSOLUTE (ADDR (.ovl.init));\n", | |
4504 | htab->params->line_size) <= 0) | |
4505 | goto file_err; | |
4506 | ||
a3a219a9 | 4507 | base = 0; |
cd4a7468 AM |
4508 | ovlynum = 1; |
4509 | while (base < count) | |
4510 | { | |
4511 | unsigned int indx = ovlynum - 1; | |
4512 | unsigned int vma, lma; | |
a3a219a9 | 4513 | |
37107878 | 4514 | vma = (indx & (htab->params->num_lines - 1)) << htab->line_size_log2; |
cd4a7468 AM |
4515 | lma = indx << htab->line_size_log2; |
4516 | ||
4517 | if (fprintf (script, " .ovly%u ABSOLUTE (ADDR (.ovl.init)) + %u " | |
4518 | ": AT (ALIGN (LOADADDR (.ovl.init) + SIZEOF (.ovl.init), 16) + %u) {\n", | |
4519 | ovlynum, vma, lma) <= 0) | |
4520 | goto file_err; | |
4521 | ||
4522 | base = print_one_overlay_section (script, base, count, ovlynum, | |
4523 | ovly_map, ovly_sections, info); | |
4524 | if (base == (unsigned) -1) | |
4525 | goto file_err; | |
4526 | ||
4527 | if (fprintf (script, " }\n") <= 0) | |
4528 | goto file_err; | |
4529 | ||
4530 | ovlynum++; | |
4531 | } | |
a3a219a9 | 4532 | |
cd4a7468 AM |
4533 | if (fprintf (script, " . = ABSOLUTE (ADDR (.ovl.init)) + %u;\n", |
4534 | 1 << (htab->num_lines_log2 + htab->line_size_log2)) <= 0) | |
4535 | goto file_err; | |
4536 | } | |
4537 | else | |
4538 | { | |
4539 | if (fprintf (script, | |
4540 | " . = ALIGN (16);\n" | |
4541 | " .ovl.init : { *(.ovl.init) }\n" | |
4542 | " . = ABSOLUTE (ADDR (.ovl.init));\n") <= 0) | |
9dcc4794 | 4543 | goto file_err; |
a3a219a9 | 4544 | |
cd4a7468 | 4545 | for (region = 1; region <= htab->params->num_lines; region++) |
9dcc4794 | 4546 | { |
cd4a7468 AM |
4547 | ovlynum = region; |
4548 | base = 0; | |
4549 | while (base < count && ovly_map[base] < ovlynum) | |
4550 | base++; | |
a3a219a9 | 4551 | |
cd4a7468 AM |
4552 | if (base == count) |
4553 | break; | |
a3a219a9 | 4554 | |
cd4a7468 AM |
4555 | if (region == 1) |
4556 | { | |
4557 | /* We need to set lma since we are overlaying .ovl.init. */ | |
4558 | if (fprintf (script, | |
4559 | " OVERLAY : AT (ALIGN (LOADADDR (.ovl.init) + SIZEOF (.ovl.init), 16))\n {\n") <= 0) | |
4560 | goto file_err; | |
4561 | } | |
4562 | else | |
4563 | { | |
4564 | if (fprintf (script, " OVERLAY :\n {\n") <= 0) | |
a3a219a9 | 4565 | goto file_err; |
9dcc4794 | 4566 | } |
9dcc4794 | 4567 | |
cd4a7468 | 4568 | while (base < count) |
9dcc4794 | 4569 | { |
cd4a7468 | 4570 | if (fprintf (script, " .ovly%u {\n", ovlynum) <= 0) |
a3a219a9 AM |
4571 | goto file_err; |
4572 | ||
cd4a7468 AM |
4573 | base = print_one_overlay_section (script, base, count, ovlynum, |
4574 | ovly_map, ovly_sections, info); | |
4575 | if (base == (unsigned) -1) | |
4576 | goto file_err; | |
4577 | ||
4578 | if (fprintf (script, " }\n") <= 0) | |
4579 | goto file_err; | |
4580 | ||
4581 | ovlynum += htab->params->num_lines; | |
4582 | while (base < count && ovly_map[base] < ovlynum) | |
4583 | base++; | |
9dcc4794 | 4584 | } |
9dcc4794 | 4585 | |
cd4a7468 | 4586 | if (fprintf (script, " }\n") <= 0) |
a3a219a9 | 4587 | goto file_err; |
9dcc4794 AM |
4588 | } |
4589 | ||
9dcc4794 | 4590 | } |
a3a219a9 AM |
4591 | |
4592 | free (ovly_map); | |
9dcc4794 AM |
4593 | free (ovly_sections); |
4594 | ||
a3a219a9 | 4595 | if (fprintf (script, "}\nINSERT BEFORE .text;\n") <= 0) |
9dcc4794 AM |
4596 | goto file_err; |
4597 | if (fclose (script) != 0) | |
4598 | goto file_err; | |
4599 | ||
64615358 AM |
4600 | if (htab->params->auto_overlay & AUTO_RELINK) |
4601 | (*htab->params->spu_elf_relink) (); | |
9dcc4794 AM |
4602 | |
4603 | xexit (0); | |
4604 | ||
4605 | file_err: | |
4606 | bfd_set_error (bfd_error_system_call); | |
4607 | err_exit: | |
4608 | info->callbacks->einfo ("%F%P: auto overlay error: %E\n"); | |
4609 | xexit (1); | |
4610 | } | |
4611 | ||
49fa1e15 AM |
4612 | /* Provide an estimate of total stack required. */ |
4613 | ||
4614 | static bfd_boolean | |
64615358 | 4615 | spu_elf_stack_analysis (struct bfd_link_info *info) |
49fa1e15 | 4616 | { |
64615358 | 4617 | struct spu_link_hash_table *htab; |
055ed83b | 4618 | struct _sum_stack_param sum_stack_param; |
49fa1e15 | 4619 | |
c65be8d7 | 4620 | if (!discover_functions (info)) |
49fa1e15 AM |
4621 | return FALSE; |
4622 | ||
c65be8d7 | 4623 | if (!build_call_tree (info)) |
49fa1e15 AM |
4624 | return FALSE; |
4625 | ||
64615358 | 4626 | htab = spu_hash_table (info); |
cd4a7468 AM |
4627 | if (htab->params->stack_analysis) |
4628 | { | |
4629 | info->callbacks->info (_("Stack size for call graph root nodes.\n")); | |
4630 | info->callbacks->minfo (_("\nStack size for functions. " | |
4631 | "Annotations: '*' max stack, 't' tail call\n")); | |
4632 | } | |
49fa1e15 | 4633 | |
64615358 | 4634 | sum_stack_param.emit_stack_syms = htab->params->emit_stack_syms; |
055ed83b AM |
4635 | sum_stack_param.overall_stack = 0; |
4636 | if (!for_each_node (sum_stack, info, &sum_stack_param, TRUE)) | |
4637 | return FALSE; | |
49fa1e15 | 4638 | |
cd4a7468 AM |
4639 | if (htab->params->stack_analysis) |
4640 | info->callbacks->info (_("Maximum stack required is 0x%v\n"), | |
4641 | (bfd_vma) sum_stack_param.overall_stack); | |
49fa1e15 AM |
4642 | return TRUE; |
4643 | } | |
4644 | ||
4645 | /* Perform a final link. */ | |
4646 | ||
4647 | static bfd_boolean | |
4648 | spu_elf_final_link (bfd *output_bfd, struct bfd_link_info *info) | |
4649 | { | |
4650 | struct spu_link_hash_table *htab = spu_hash_table (info); | |
4651 | ||
64615358 AM |
4652 | if (htab->params->auto_overlay) |
4653 | spu_elf_auto_overlay (info); | |
9dcc4794 | 4654 | |
cd4a7468 AM |
4655 | if ((htab->params->stack_analysis |
4656 | || (htab->params->ovly_flavour == ovly_soft_icache | |
4657 | && htab->params->lrlive_analysis)) | |
64615358 | 4658 | && !spu_elf_stack_analysis (info)) |
cd4a7468 AM |
4659 | info->callbacks->einfo ("%X%P: stack/lrlive analysis error: %E\n"); |
4660 | ||
4661 | if (!spu_elf_build_stubs (info)) | |
4662 | info->callbacks->einfo ("%F%P: can not build overlay stubs: %E\n"); | |
49fa1e15 AM |
4663 | |
4664 | return bfd_elf_final_link (output_bfd, info); | |
4665 | } | |
4666 | ||
ece5ef60 AM |
4667 | /* Called when not normally emitting relocs, ie. !info->relocatable |
4668 | and !info->emitrelocations. Returns a count of special relocs | |
4669 | that need to be emitted. */ | |
4670 | ||
4671 | static unsigned int | |
58217f29 | 4672 | spu_elf_count_relocs (struct bfd_link_info *info, asection *sec) |
ece5ef60 | 4673 | { |
58217f29 | 4674 | Elf_Internal_Rela *relocs; |
ece5ef60 | 4675 | unsigned int count = 0; |
ece5ef60 | 4676 | |
58217f29 AM |
4677 | relocs = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, |
4678 | info->keep_memory); | |
4679 | if (relocs != NULL) | |
ece5ef60 | 4680 | { |
58217f29 AM |
4681 | Elf_Internal_Rela *rel; |
4682 | Elf_Internal_Rela *relend = relocs + sec->reloc_count; | |
4683 | ||
4684 | for (rel = relocs; rel < relend; rel++) | |
4685 | { | |
4686 | int r_type = ELF32_R_TYPE (rel->r_info); | |
4687 | if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64) | |
4688 | ++count; | |
4689 | } | |
4690 | ||
4691 | if (elf_section_data (sec)->relocs != relocs) | |
4692 | free (relocs); | |
ece5ef60 AM |
4693 | } |
4694 | ||
4695 | return count; | |
4696 | } | |
4697 | ||
e9f53129 AM |
4698 | /* Apply RELOCS to CONTENTS of INPUT_SECTION from INPUT_BFD. */ |
4699 | ||
d16c7321 | 4700 | static int |
e9f53129 AM |
4701 | spu_elf_relocate_section (bfd *output_bfd, |
4702 | struct bfd_link_info *info, | |
4703 | bfd *input_bfd, | |
4704 | asection *input_section, | |
4705 | bfd_byte *contents, | |
4706 | Elf_Internal_Rela *relocs, | |
4707 | Elf_Internal_Sym *local_syms, | |
4708 | asection **local_sections) | |
4709 | { | |
4710 | Elf_Internal_Shdr *symtab_hdr; | |
4711 | struct elf_link_hash_entry **sym_hashes; | |
4712 | Elf_Internal_Rela *rel, *relend; | |
4713 | struct spu_link_hash_table *htab; | |
64615358 | 4714 | asection *ea; |
d16c7321 | 4715 | int ret = TRUE; |
ece5ef60 | 4716 | bfd_boolean emit_these_relocs = FALSE; |
cc5ca406 | 4717 | bfd_boolean is_ea_sym; |
fdba2fcd | 4718 | bfd_boolean stubs; |
cd4a7468 | 4719 | unsigned int iovl = 0; |
e9f53129 | 4720 | |
e9f53129 | 4721 | htab = spu_hash_table (info); |
fdba2fcd | 4722 | stubs = (htab->stub_sec != NULL |
64615358 | 4723 | && maybe_needs_stubs (input_section)); |
cd4a7468 | 4724 | iovl = overlay_index (input_section); |
64615358 | 4725 | ea = bfd_get_section_by_name (output_bfd, "._ea"); |
e9f53129 AM |
4726 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
4727 | sym_hashes = (struct elf_link_hash_entry **) (elf_sym_hashes (input_bfd)); | |
4728 | ||
4729 | rel = relocs; | |
4730 | relend = relocs + input_section->reloc_count; | |
4731 | for (; rel < relend; rel++) | |
4732 | { | |
4733 | int r_type; | |
4734 | reloc_howto_type *howto; | |
8374f9d4 | 4735 | unsigned int r_symndx; |
e9f53129 AM |
4736 | Elf_Internal_Sym *sym; |
4737 | asection *sec; | |
4738 | struct elf_link_hash_entry *h; | |
4739 | const char *sym_name; | |
4740 | bfd_vma relocation; | |
4741 | bfd_vma addend; | |
4742 | bfd_reloc_status_type r; | |
4743 | bfd_boolean unresolved_reloc; | |
4744 | bfd_boolean warned; | |
124b52c6 | 4745 | enum _stub_type stub_type; |
e9f53129 AM |
4746 | |
4747 | r_symndx = ELF32_R_SYM (rel->r_info); | |
4748 | r_type = ELF32_R_TYPE (rel->r_info); | |
4749 | howto = elf_howto_table + r_type; | |
4750 | unresolved_reloc = FALSE; | |
4751 | warned = FALSE; | |
e9f53129 AM |
4752 | h = NULL; |
4753 | sym = NULL; | |
4754 | sec = NULL; | |
4755 | if (r_symndx < symtab_hdr->sh_info) | |
4756 | { | |
4757 | sym = local_syms + r_symndx; | |
4758 | sec = local_sections[r_symndx]; | |
4759 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec); | |
4760 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); | |
4761 | } | |
4762 | else | |
4763 | { | |
dc1859a6 AM |
4764 | if (sym_hashes == NULL) |
4765 | return FALSE; | |
4766 | ||
4767 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
4768 | ||
4769 | while (h->root.type == bfd_link_hash_indirect | |
4770 | || h->root.type == bfd_link_hash_warning) | |
4771 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4772 | ||
4773 | relocation = 0; | |
4774 | if (h->root.type == bfd_link_hash_defined | |
4775 | || h->root.type == bfd_link_hash_defweak) | |
4776 | { | |
4777 | sec = h->root.u.def.section; | |
4778 | if (sec == NULL | |
4779 | || sec->output_section == NULL) | |
4780 | /* Set a flag that will be cleared later if we find a | |
4781 | relocation value for this symbol. output_section | |
4782 | is typically NULL for symbols satisfied by a shared | |
4783 | library. */ | |
4784 | unresolved_reloc = TRUE; | |
4785 | else | |
4786 | relocation = (h->root.u.def.value | |
4787 | + sec->output_section->vma | |
4788 | + sec->output_offset); | |
4789 | } | |
4790 | else if (h->root.type == bfd_link_hash_undefweak) | |
4791 | ; | |
4792 | else if (info->unresolved_syms_in_objects == RM_IGNORE | |
4793 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
4794 | ; | |
4795 | else if (!info->relocatable | |
4796 | && !(r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)) | |
4797 | { | |
4798 | bfd_boolean err; | |
4799 | err = (info->unresolved_syms_in_objects == RM_GENERATE_ERROR | |
4800 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT); | |
4801 | if (!info->callbacks->undefined_symbol (info, | |
4802 | h->root.root.string, | |
4803 | input_bfd, | |
4804 | input_section, | |
4805 | rel->r_offset, err)) | |
4806 | return FALSE; | |
4807 | warned = TRUE; | |
4808 | } | |
e9f53129 AM |
4809 | sym_name = h->root.root.string; |
4810 | } | |
4811 | ||
ab96bf03 AM |
4812 | if (sec != NULL && elf_discarded_section (sec)) |
4813 | { | |
4814 | /* For relocs against symbols from removed linkonce sections, | |
4815 | or sections discarded by a linker script, we just want the | |
4816 | section contents zeroed. Avoid any special processing. */ | |
4817 | _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset); | |
4818 | rel->r_info = 0; | |
4819 | rel->r_addend = 0; | |
4820 | continue; | |
4821 | } | |
4822 | ||
4823 | if (info->relocatable) | |
4824 | continue; | |
4825 | ||
cc5ca406 AM |
4826 | is_ea_sym = (ea != NULL |
4827 | && sec != NULL | |
4828 | && sec->output_section == ea); | |
cd4a7468 AM |
4829 | |
4830 | /* If this symbol is in an overlay area, we may need to relocate | |
4831 | to the overlay stub. */ | |
4832 | addend = rel->r_addend; | |
4833 | if (stubs | |
4834 | && !is_ea_sym | |
4835 | && (stub_type = needs_ovl_stub (h, sym, sec, input_section, rel, | |
4836 | contents, info)) != no_stub) | |
4837 | { | |
4838 | unsigned int ovl = 0; | |
4839 | struct got_entry *g, **head; | |
4840 | ||
4841 | if (stub_type != nonovl_stub) | |
4842 | ovl = iovl; | |
4843 | ||
4844 | if (h != NULL) | |
4845 | head = &h->got.glist; | |
4846 | else | |
4847 | head = elf_local_got_ents (input_bfd) + r_symndx; | |
cc5ca406 | 4848 | |
cd4a7468 AM |
4849 | for (g = *head; g != NULL; g = g->next) |
4850 | if (htab->params->ovly_flavour == ovly_soft_icache | |
74681fc4 AM |
4851 | ? (g->ovl == ovl |
4852 | && g->br_addr == (rel->r_offset | |
4853 | + input_section->output_offset | |
4854 | + input_section->output_section->vma)) | |
cd4a7468 AM |
4855 | : g->addend == addend && (g->ovl == ovl || g->ovl == 0)) |
4856 | break; | |
4857 | if (g == NULL) | |
4858 | abort (); | |
4859 | ||
4860 | relocation = g->stub_addr; | |
4861 | addend = 0; | |
4862 | } | |
4863 | else | |
4864 | { | |
4865 | /* For soft icache, encode the overlay index into addresses. */ | |
4866 | if (htab->params->ovly_flavour == ovly_soft_icache | |
d342a8b1 UW |
4867 | && (r_type == R_SPU_ADDR16_HI |
4868 | || r_type == R_SPU_ADDR32 || r_type == R_SPU_REL32) | |
cd4a7468 AM |
4869 | && !is_ea_sym) |
4870 | { | |
4871 | unsigned int ovl = overlay_index (sec); | |
4872 | if (ovl != 0) | |
4873 | { | |
30c665df | 4874 | unsigned int set_id = ((ovl - 1) >> htab->num_lines_log2) + 1; |
cd4a7468 | 4875 | relocation += set_id << 18; |
cd4a7468 AM |
4876 | } |
4877 | } | |
4878 | } | |
4879 | ||
4880 | if (unresolved_reloc) | |
4881 | ; | |
4882 | else if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64) | |
8374f9d4 | 4883 | { |
cc5ca406 | 4884 | if (is_ea_sym) |
8374f9d4 AM |
4885 | { |
4886 | /* ._ea is a special section that isn't allocated in SPU | |
4887 | memory, but rather occupies space in PPU memory as | |
4888 | part of an embedded ELF image. If this reloc is | |
4889 | against a symbol defined in ._ea, then transform the | |
4890 | reloc into an equivalent one without a symbol | |
4891 | relative to the start of the ELF image. */ | |
4892 | rel->r_addend += (relocation | |
4893 | - ea->vma | |
4894 | + elf_section_data (ea)->this_hdr.sh_offset); | |
4895 | rel->r_info = ELF32_R_INFO (0, r_type); | |
4896 | } | |
4897 | emit_these_relocs = TRUE; | |
4898 | continue; | |
4899 | } | |
cd4a7468 | 4900 | else if (is_ea_sym) |
8374f9d4 AM |
4901 | unresolved_reloc = TRUE; |
4902 | ||
e9f53129 AM |
4903 | if (unresolved_reloc) |
4904 | { | |
4905 | (*_bfd_error_handler) | |
4906 | (_("%B(%s+0x%lx): unresolvable %s relocation against symbol `%s'"), | |
4907 | input_bfd, | |
4908 | bfd_get_section_name (input_bfd, input_section), | |
4909 | (long) rel->r_offset, | |
4910 | howto->name, | |
4911 | sym_name); | |
4912 | ret = FALSE; | |
4913 | } | |
4914 | ||
e9f53129 AM |
4915 | r = _bfd_final_link_relocate (howto, |
4916 | input_bfd, | |
4917 | input_section, | |
4918 | contents, | |
4919 | rel->r_offset, relocation, addend); | |
4920 | ||
4921 | if (r != bfd_reloc_ok) | |
4922 | { | |
4923 | const char *msg = (const char *) 0; | |
4924 | ||
4925 | switch (r) | |
4926 | { | |
4927 | case bfd_reloc_overflow: | |
4928 | if (!((*info->callbacks->reloc_overflow) | |
4929 | (info, (h ? &h->root : NULL), sym_name, howto->name, | |
4930 | (bfd_vma) 0, input_bfd, input_section, rel->r_offset))) | |
4931 | return FALSE; | |
4932 | break; | |
4933 | ||
4934 | case bfd_reloc_undefined: | |
4935 | if (!((*info->callbacks->undefined_symbol) | |
4936 | (info, sym_name, input_bfd, input_section, | |
4937 | rel->r_offset, TRUE))) | |
4938 | return FALSE; | |
4939 | break; | |
4940 | ||
4941 | case bfd_reloc_outofrange: | |
4942 | msg = _("internal error: out of range error"); | |
4943 | goto common_error; | |
4944 | ||
4945 | case bfd_reloc_notsupported: | |
4946 | msg = _("internal error: unsupported relocation error"); | |
4947 | goto common_error; | |
4948 | ||
4949 | case bfd_reloc_dangerous: | |
4950 | msg = _("internal error: dangerous error"); | |
4951 | goto common_error; | |
4952 | ||
4953 | default: | |
4954 | msg = _("internal error: unknown error"); | |
4955 | /* fall through */ | |
4956 | ||
4957 | common_error: | |
d16c7321 | 4958 | ret = FALSE; |
e9f53129 AM |
4959 | if (!((*info->callbacks->warning) |
4960 | (info, msg, sym_name, input_bfd, input_section, | |
4961 | rel->r_offset))) | |
4962 | return FALSE; | |
4963 | break; | |
4964 | } | |
4965 | } | |
4966 | } | |
4967 | ||
ece5ef60 AM |
4968 | if (ret |
4969 | && emit_these_relocs | |
ece5ef60 AM |
4970 | && !info->emitrelocations) |
4971 | { | |
4972 | Elf_Internal_Rela *wrel; | |
4973 | Elf_Internal_Shdr *rel_hdr; | |
4974 | ||
4975 | wrel = rel = relocs; | |
4976 | relend = relocs + input_section->reloc_count; | |
4977 | for (; rel < relend; rel++) | |
4978 | { | |
4979 | int r_type; | |
4980 | ||
4981 | r_type = ELF32_R_TYPE (rel->r_info); | |
4982 | if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64) | |
4983 | *wrel++ = *rel; | |
4984 | } | |
4985 | input_section->reloc_count = wrel - relocs; | |
4986 | /* Backflips for _bfd_elf_link_output_relocs. */ | |
4987 | rel_hdr = &elf_section_data (input_section)->rel_hdr; | |
4988 | rel_hdr->sh_size = input_section->reloc_count * rel_hdr->sh_entsize; | |
4989 | ret = 2; | |
4990 | } | |
4991 | ||
e9f53129 AM |
4992 | return ret; |
4993 | } | |
4994 | ||
c1b2796f AM |
4995 | /* Adjust _SPUEAR_ syms to point at their overlay stubs. */ |
4996 | ||
6e0b88f1 | 4997 | static int |
c1b2796f AM |
4998 | spu_elf_output_symbol_hook (struct bfd_link_info *info, |
4999 | const char *sym_name ATTRIBUTE_UNUSED, | |
5000 | Elf_Internal_Sym *sym, | |
5001 | asection *sym_sec ATTRIBUTE_UNUSED, | |
5002 | struct elf_link_hash_entry *h) | |
5003 | { | |
5004 | struct spu_link_hash_table *htab = spu_hash_table (info); | |
5005 | ||
5006 | if (!info->relocatable | |
47f6dab9 | 5007 | && htab->stub_sec != NULL |
c1b2796f AM |
5008 | && h != NULL |
5009 | && (h->root.type == bfd_link_hash_defined | |
5010 | || h->root.type == bfd_link_hash_defweak) | |
5011 | && h->def_regular | |
5012 | && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0) | |
5013 | { | |
4a628337 | 5014 | struct got_entry *g; |
c1b2796f | 5015 | |
4a628337 | 5016 | for (g = h->got.glist; g != NULL; g = g->next) |
cd4a7468 AM |
5017 | if (htab->params->ovly_flavour == ovly_soft_icache |
5018 | ? g->br_addr == g->stub_addr | |
5019 | : g->addend == 0 && g->ovl == 0) | |
4a628337 AM |
5020 | { |
5021 | sym->st_shndx = (_bfd_elf_section_from_bfd_section | |
5022 | (htab->stub_sec[0]->output_section->owner, | |
5023 | htab->stub_sec[0]->output_section)); | |
5024 | sym->st_value = g->stub_addr; | |
5025 | break; | |
5026 | } | |
c1b2796f AM |
5027 | } |
5028 | ||
6e0b88f1 | 5029 | return 1; |
c1b2796f AM |
5030 | } |
5031 | ||
e9f53129 AM |
5032 | static int spu_plugin = 0; |
5033 | ||
5034 | void | |
5035 | spu_elf_plugin (int val) | |
5036 | { | |
5037 | spu_plugin = val; | |
5038 | } | |
5039 | ||
5040 | /* Set ELF header e_type for plugins. */ | |
5041 | ||
5042 | static void | |
5043 | spu_elf_post_process_headers (bfd *abfd, | |
5044 | struct bfd_link_info *info ATTRIBUTE_UNUSED) | |
5045 | { | |
5046 | if (spu_plugin) | |
5047 | { | |
5048 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
5049 | ||
5050 | i_ehdrp->e_type = ET_DYN; | |
5051 | } | |
5052 | } | |
5053 | ||
5054 | /* We may add an extra PT_LOAD segment for .toe. We also need extra | |
5055 | segments for overlays. */ | |
5056 | ||
5057 | static int | |
5058 | spu_elf_additional_program_headers (bfd *abfd, struct bfd_link_info *info) | |
5059 | { | |
ceae84aa | 5060 | int extra = 0; |
e9f53129 AM |
5061 | asection *sec; |
5062 | ||
ceae84aa AM |
5063 | if (info != NULL) |
5064 | { | |
5065 | struct spu_link_hash_table *htab = spu_hash_table (info); | |
5066 | extra = htab->num_overlays; | |
5067 | } | |
5068 | ||
e9f53129 AM |
5069 | if (extra) |
5070 | ++extra; | |
5071 | ||
5072 | sec = bfd_get_section_by_name (abfd, ".toe"); | |
5073 | if (sec != NULL && (sec->flags & SEC_LOAD) != 0) | |
5074 | ++extra; | |
5075 | ||
5076 | return extra; | |
5077 | } | |
5078 | ||
5079 | /* Remove .toe section from other PT_LOAD segments and put it in | |
5080 | a segment of its own. Put overlays in separate segments too. */ | |
5081 | ||
5082 | static bfd_boolean | |
5083 | spu_elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info) | |
5084 | { | |
5085 | asection *toe, *s; | |
452de53c UW |
5086 | struct elf_segment_map *m, *m_overlay; |
5087 | struct elf_segment_map **p, **p_overlay; | |
e9f53129 AM |
5088 | unsigned int i; |
5089 | ||
5090 | if (info == NULL) | |
5091 | return TRUE; | |
5092 | ||
5093 | toe = bfd_get_section_by_name (abfd, ".toe"); | |
5094 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
5095 | if (m->p_type == PT_LOAD && m->count > 1) | |
5096 | for (i = 0; i < m->count; i++) | |
5097 | if ((s = m->sections[i]) == toe | |
47f6dab9 | 5098 | || spu_elf_section_data (s)->u.o.ovl_index != 0) |
e9f53129 AM |
5099 | { |
5100 | struct elf_segment_map *m2; | |
5101 | bfd_vma amt; | |
5102 | ||
5103 | if (i + 1 < m->count) | |
5104 | { | |
5105 | amt = sizeof (struct elf_segment_map); | |
5106 | amt += (m->count - (i + 2)) * sizeof (m->sections[0]); | |
5107 | m2 = bfd_zalloc (abfd, amt); | |
5108 | if (m2 == NULL) | |
5109 | return FALSE; | |
5110 | m2->count = m->count - (i + 1); | |
5111 | memcpy (m2->sections, m->sections + i + 1, | |
5112 | m2->count * sizeof (m->sections[0])); | |
5113 | m2->p_type = PT_LOAD; | |
5114 | m2->next = m->next; | |
5115 | m->next = m2; | |
5116 | } | |
5117 | m->count = 1; | |
5118 | if (i != 0) | |
5119 | { | |
5120 | m->count = i; | |
5121 | amt = sizeof (struct elf_segment_map); | |
5122 | m2 = bfd_zalloc (abfd, amt); | |
5123 | if (m2 == NULL) | |
5124 | return FALSE; | |
5125 | m2->p_type = PT_LOAD; | |
5126 | m2->count = 1; | |
5127 | m2->sections[0] = s; | |
5128 | m2->next = m->next; | |
5129 | m->next = m2; | |
5130 | } | |
5131 | break; | |
5132 | } | |
5133 | ||
452de53c UW |
5134 | |
5135 | /* Some SPU ELF loaders ignore the PF_OVERLAY flag and just load all | |
5136 | PT_LOAD segments. This can cause the .ovl.init section to be | |
5137 | overwritten with the contents of some overlay segment. To work | |
5138 | around this issue, we ensure that all PF_OVERLAY segments are | |
5139 | sorted first amongst the program headers; this ensures that even | |
5140 | with a broken loader, the .ovl.init section (which is not marked | |
5141 | as PF_OVERLAY) will be placed into SPU local store on startup. */ | |
5142 | ||
5143 | /* Move all overlay segments onto a separate list. */ | |
5144 | p = &elf_tdata (abfd)->segment_map; | |
5145 | p_overlay = &m_overlay; | |
5146 | while (*p != NULL) | |
5147 | { | |
5148 | if ((*p)->p_type == PT_LOAD && (*p)->count == 1 | |
5149 | && spu_elf_section_data ((*p)->sections[0])->u.o.ovl_index != 0) | |
5150 | { | |
5151 | struct elf_segment_map *m = *p; | |
5152 | *p = m->next; | |
5153 | *p_overlay = m; | |
5154 | p_overlay = &m->next; | |
5155 | continue; | |
5156 | } | |
5157 | ||
5158 | p = &((*p)->next); | |
5159 | } | |
5160 | ||
5161 | /* Re-insert overlay segments at the head of the segment map. */ | |
5162 | *p_overlay = elf_tdata (abfd)->segment_map; | |
5163 | elf_tdata (abfd)->segment_map = m_overlay; | |
5164 | ||
e9f53129 AM |
5165 | return TRUE; |
5166 | } | |
5167 | ||
7d3287cb AM |
5168 | /* Tweak the section type of .note.spu_name. */ |
5169 | ||
5170 | static bfd_boolean | |
5171 | spu_elf_fake_sections (bfd *obfd ATTRIBUTE_UNUSED, | |
5172 | Elf_Internal_Shdr *hdr, | |
5173 | asection *sec) | |
5174 | { | |
5175 | if (strcmp (sec->name, SPU_PTNOTE_SPUNAME) == 0) | |
5176 | hdr->sh_type = SHT_NOTE; | |
5177 | return TRUE; | |
5178 | } | |
5179 | ||
e9f53129 AM |
5180 | /* Tweak phdrs before writing them out. */ |
5181 | ||
5182 | static int | |
5183 | spu_elf_modify_program_headers (bfd *abfd, struct bfd_link_info *info) | |
5184 | { | |
5185 | const struct elf_backend_data *bed; | |
5186 | struct elf_obj_tdata *tdata; | |
5187 | Elf_Internal_Phdr *phdr, *last; | |
5188 | struct spu_link_hash_table *htab; | |
5189 | unsigned int count; | |
5190 | unsigned int i; | |
5191 | ||
5192 | if (info == NULL) | |
5193 | return TRUE; | |
5194 | ||
5195 | bed = get_elf_backend_data (abfd); | |
5196 | tdata = elf_tdata (abfd); | |
5197 | phdr = tdata->phdr; | |
5198 | count = tdata->program_header_size / bed->s->sizeof_phdr; | |
5199 | htab = spu_hash_table (info); | |
5200 | if (htab->num_overlays != 0) | |
5201 | { | |
5202 | struct elf_segment_map *m; | |
5203 | unsigned int o; | |
5204 | ||
5205 | for (i = 0, m = elf_tdata (abfd)->segment_map; m; ++i, m = m->next) | |
5206 | if (m->count != 0 | |
47f6dab9 | 5207 | && (o = spu_elf_section_data (m->sections[0])->u.o.ovl_index) != 0) |
e9f53129 AM |
5208 | { |
5209 | /* Mark this as an overlay header. */ | |
5210 | phdr[i].p_flags |= PF_OVERLAY; | |
5211 | ||
cd4a7468 AM |
5212 | if (htab->ovtab != NULL && htab->ovtab->size != 0 |
5213 | && htab->params->ovly_flavour != ovly_soft_icache) | |
e9f53129 AM |
5214 | { |
5215 | bfd_byte *p = htab->ovtab->contents; | |
47f6dab9 | 5216 | unsigned int off = o * 16 + 8; |
e9f53129 AM |
5217 | |
5218 | /* Write file_off into _ovly_table. */ | |
5219 | bfd_put_32 (htab->ovtab->owner, phdr[i].p_offset, p + off); | |
5220 | } | |
5221 | } | |
cd4a7468 AM |
5222 | /* Soft-icache has its file offset put in .ovl.init. */ |
5223 | if (htab->init != NULL && htab->init->size != 0) | |
5224 | { | |
5225 | bfd_vma val = elf_section_data (htab->ovl_sec[0])->this_hdr.sh_offset; | |
5226 | ||
5227 | bfd_put_32 (htab->init->owner, val, htab->init->contents + 4); | |
5228 | } | |
e9f53129 AM |
5229 | } |
5230 | ||
5231 | /* Round up p_filesz and p_memsz of PT_LOAD segments to multiples | |
5232 | of 16. This should always be possible when using the standard | |
5233 | linker scripts, but don't create overlapping segments if | |
5234 | someone is playing games with linker scripts. */ | |
5235 | last = NULL; | |
5236 | for (i = count; i-- != 0; ) | |
5237 | if (phdr[i].p_type == PT_LOAD) | |
5238 | { | |
5239 | unsigned adjust; | |
5240 | ||
5241 | adjust = -phdr[i].p_filesz & 15; | |
5242 | if (adjust != 0 | |
5243 | && last != NULL | |
5244 | && phdr[i].p_offset + phdr[i].p_filesz > last->p_offset - adjust) | |
5245 | break; | |
5246 | ||
5247 | adjust = -phdr[i].p_memsz & 15; | |
5248 | if (adjust != 0 | |
5249 | && last != NULL | |
5250 | && phdr[i].p_filesz != 0 | |
5251 | && phdr[i].p_vaddr + phdr[i].p_memsz > last->p_vaddr - adjust | |
5252 | && phdr[i].p_vaddr + phdr[i].p_memsz <= last->p_vaddr) | |
5253 | break; | |
5254 | ||
5255 | if (phdr[i].p_filesz != 0) | |
5256 | last = &phdr[i]; | |
5257 | } | |
5258 | ||
5259 | if (i == (unsigned int) -1) | |
5260 | for (i = count; i-- != 0; ) | |
5261 | if (phdr[i].p_type == PT_LOAD) | |
5262 | { | |
5263 | unsigned adjust; | |
5264 | ||
5265 | adjust = -phdr[i].p_filesz & 15; | |
5266 | phdr[i].p_filesz += adjust; | |
5267 | ||
5268 | adjust = -phdr[i].p_memsz & 15; | |
5269 | phdr[i].p_memsz += adjust; | |
5270 | } | |
5271 | ||
5272 | return TRUE; | |
5273 | } | |
5274 | ||
e9f53129 AM |
5275 | #define TARGET_BIG_SYM bfd_elf32_spu_vec |
5276 | #define TARGET_BIG_NAME "elf32-spu" | |
5277 | #define ELF_ARCH bfd_arch_spu | |
5278 | #define ELF_MACHINE_CODE EM_SPU | |
5279 | /* This matches the alignment need for DMA. */ | |
5280 | #define ELF_MAXPAGESIZE 0x80 | |
5281 | #define elf_backend_rela_normal 1 | |
5282 | #define elf_backend_can_gc_sections 1 | |
5283 | ||
5284 | #define bfd_elf32_bfd_reloc_type_lookup spu_elf_reloc_type_lookup | |
ad2adbc8 | 5285 | #define bfd_elf32_bfd_reloc_name_lookup spu_elf_reloc_name_lookup |
e9f53129 | 5286 | #define elf_info_to_howto spu_elf_info_to_howto |
ece5ef60 | 5287 | #define elf_backend_count_relocs spu_elf_count_relocs |
e9f53129 AM |
5288 | #define elf_backend_relocate_section spu_elf_relocate_section |
5289 | #define elf_backend_symbol_processing spu_elf_backend_symbol_processing | |
c1b2796f | 5290 | #define elf_backend_link_output_symbol_hook spu_elf_output_symbol_hook |
124b52c6 | 5291 | #define elf_backend_object_p spu_elf_object_p |
e9f53129 AM |
5292 | #define bfd_elf32_new_section_hook spu_elf_new_section_hook |
5293 | #define bfd_elf32_bfd_link_hash_table_create spu_elf_link_hash_table_create | |
e9f53129 AM |
5294 | |
5295 | #define elf_backend_additional_program_headers spu_elf_additional_program_headers | |
5296 | #define elf_backend_modify_segment_map spu_elf_modify_segment_map | |
5297 | #define elf_backend_modify_program_headers spu_elf_modify_program_headers | |
5298 | #define elf_backend_post_process_headers spu_elf_post_process_headers | |
7d3287cb | 5299 | #define elf_backend_fake_sections spu_elf_fake_sections |
e9f53129 | 5300 | #define elf_backend_special_sections spu_elf_special_sections |
49fa1e15 | 5301 | #define bfd_elf32_bfd_final_link spu_elf_final_link |
e9f53129 AM |
5302 | |
5303 | #include "elf32-target.h" |