1 /* Renesas / SuperH specific support for Symbian 32-bit ELF files
3 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Stop elf32-sh.c from defining any target vectors. */
23 #define SH_TARGET_ALREADY_DEFINED
24 #define sh_find_elf_flags sh_symbian_find_elf_flags
25 #define sh_elf_get_flags_from_mach sh_symbian_elf_get_flags_from_mach
32 #define DIRECTIVE_HEADER "#<SYMEDIT>#\n"
33 #define DIRECTIVE_IMPORT "IMPORT "
34 #define DIRECTIVE_EXPORT "EXPORT "
35 #define DIRECTIVE_AS "AS "
37 /* Macro to advance 's' until either it reaches 'e' or the
38 character pointed to by 's' is equal to 'c'. If 'e' is
39 reached and DEBUG is enabled then the error message 'm'
41 #define SKIP_UNTIL(s,e,c,m) \
44 while (s < e && *s != c) \
49 fprintf (stderr, "Corrupt directive: %s\n", m); \
57 /* Like SKIP_UNTIL except there are two terminator characters
59 #define SKIP_UNTIL2(s,e,c1,c2,m) \
62 while (s < e && *s != c1 && *s != c2) \
67 fprintf (stderr, "Corrupt directive: %s\n", m); \
75 /* Macro to advance 's' until either it reaches 'e' or the
76 character pointed to by 's' is not equal to 'c'. If 'e'
77 is reached and DEBUG is enabled then the error message
79 #define SKIP_WHILE(s,e,c,m) \
82 while (s < e && *s == c) \
87 fprintf (stderr, "Corrupt directive: %s\n", m); \
96 typedef struct symbol_rename
98 struct symbol_rename
* next
;
99 bfd_byte
* current_name
;
101 struct elf_link_hash_entry
* current_hash
;
102 unsigned long new_symndx
;
106 static symbol_rename
* rename_list
= NULL
;
108 /* Accumulate a list of symbols to be renamed. */
111 sh_symbian_import_as (struct bfd_link_info
*info
, bfd
* abfd
,
112 bfd_byte
* current_name
, bfd_byte
* new_name
)
114 struct elf_link_hash_entry
* new_hash
;
115 symbol_rename
* node
;
118 fprintf (stderr
, "IMPORT '%s' AS '%s'\n", current_name
, new_name
);
120 for (node
= rename_list
; node
; node
= node
->next
)
121 if (strcmp (node
->current_name
, current_name
) == 0)
123 if (strcmp (node
->new_name
, new_name
) == 0)
124 /* Already added to rename list. */
127 bfd_set_error (bfd_error_invalid_operation
);
128 _bfd_error_handler (_("%B: IMPORT AS directive for %s conceals previous IMPORT AS"),
133 if ((node
= bfd_malloc (sizeof * node
)) == NULL
)
136 fprintf (stderr
, "IMPORT AS: No mem for new rename node\n");
140 if ((node
->current_name
= bfd_malloc (strlen (current_name
) + 1)) == NULL
)
143 fprintf (stderr
, "IMPORT AS: No mem for current name field in rename node\n");
148 strcpy (node
->current_name
, current_name
);
150 if ((node
->new_name
= bfd_malloc (strlen (new_name
) + 1)) == NULL
)
153 fprintf (stderr
, "IMPORT AS: No mem for new name field in rename node\n");
154 free (node
->current_name
);
159 strcpy (node
->new_name
, new_name
);
161 node
->next
= rename_list
;
162 node
->current_hash
= NULL
;
163 node
->new_symndx
= 0;
166 new_hash
= elf_link_hash_lookup (elf_hash_table (info
), node
->new_name
, TRUE
, FALSE
, TRUE
);
167 bfd_elf_link_record_dynamic_symbol (info
, new_hash
);
168 if (new_hash
->root
.type
== bfd_link_hash_new
)
169 new_hash
->root
.type
= bfd_link_hash_undefined
;
176 sh_symbian_import (bfd
* abfd ATTRIBUTE_UNUSED
, bfd_byte
* name
)
179 fprintf (stderr
, "IMPORT '%s'\n", name
);
181 /* XXX: Generate an import somehow ? */
187 sh_symbian_export (bfd
* abfd ATTRIBUTE_UNUSED
, bfd_byte
* name
)
190 fprintf (stderr
, "EXPORT '%s'\n", name
);
192 /* XXX: Generate an export somehow ? */
197 /* Process any magic embedded commands in the .directive. section.
198 Returns TRUE upon sucecss, but if it fails it sets bfd_error and
202 sh_symbian_process_embedded_commands (struct bfd_link_info
*info
, bfd
* abfd
,
203 asection
* sec
, bfd_byte
* contents
)
207 bfd_boolean result
= TRUE
;
208 bfd_size_type sz
= sec
->rawsize
? sec
->rawsize
: sec
->size
;
210 for (s
= contents
, e
= s
+ sz
; s
< e
;)
212 bfd_byte
* directive
= s
;
216 /* I want to use "case DIRECTIVE_HEADER [0]:" here but gcc won't let me :-( */
218 if (strcmp (s
, DIRECTIVE_HEADER
))
221 /* Just ignore the header.
222 XXX: Strictly speaking we ought to check that the header
223 is present and that it is the first thing in the file. */
224 s
+= strlen (DIRECTIVE_HEADER
) + 1;
228 if (strncmp (s
, DIRECTIVE_IMPORT
, strlen (DIRECTIVE_IMPORT
)))
233 bfd_byte
* new_name_end
;
234 bfd_byte name_end_char
;
236 /* Skip the IMPORT directive. */
237 s
+= strlen (DIRECTIVE_IMPORT
);
240 /* Find the end of the new name. */
241 while (s
< e
&& *s
!= ' ' && *s
!= '\n')
245 /* We have reached the end of the .directive section
246 without encountering a string terminator. This is
247 allowed for IMPORT directives. */
248 new_name_end
= e
- 1;
249 name_end_char
= * new_name_end
;
251 result
= sh_symbian_import (abfd
, new_name
);
252 * new_name_end
= name_end_char
;
256 /* Remember where the name ends. */
258 /* Skip any whitespace before the 'AS'. */
259 SKIP_WHILE (s
, e
, ' ', "IMPORT: Name just followed by spaces");
260 /* Terminate the new name. (Do this after skiping...) */
261 name_end_char
= * new_name_end
;
264 /* Check to see if 'AS '... is present. If se we have an IMPORT AS
265 directive, otherwise we have an IMPORT directive. */
266 if (strncmp (s
, DIRECTIVE_AS
, strlen (DIRECTIVE_AS
)))
268 /* Skip the new-line at the end of the name. */
269 if (DEBUG
&& name_end_char
!= '\n')
270 fprintf (stderr
, "IMPORT: No newline at end of directive\n");
274 result
= sh_symbian_import (abfd
, new_name
);
276 /* Skip past the NUL character. */
280 fprintf (stderr
, "IMPORT: No NUL at end of directive\n");
285 bfd_byte
* current_name
;
286 bfd_byte
* current_name_end
;
287 bfd_byte current_name_end_char
;
289 /* Skip the 'AS '. */
290 s
+= strlen (DIRECTIVE_AS
);
291 /* Skip any white space after the 'AS '. */
292 SKIP_WHILE (s
, e
, ' ', "IMPORT AS: Nothing after AS");
294 /* Find the end of the current name. */
295 SKIP_UNTIL2 (s
, e
, ' ', '\n', "IMPORT AS: No newline at the end of the current name");
296 /* Skip (backwards) over spaces at the end of the current name. */
297 current_name_end
= s
;
298 current_name_end_char
= * current_name_end
;
300 SKIP_WHILE (s
, e
, ' ', "IMPORT AS: Current name just followed by spaces");
301 /* Skip past the newline character. */
304 fprintf (stderr
, "IMPORT AS: No newline at end of directive\n");
306 /* Terminate the current name after having performed the skips. */
307 * current_name_end
= 0;
309 result
= sh_symbian_import_as (info
, abfd
, current_name
, new_name
);
311 /* The next character should be a NUL. */
315 fprintf (stderr
, "IMPORT AS: Junk at end of directive\n");
320 * current_name_end
= current_name_end_char
;
323 /* Restore the characters we overwrote, since
324 the .directive section will be emitted. */
325 * new_name_end
= name_end_char
;
330 if (strncmp (s
, DIRECTIVE_EXPORT
, strlen (DIRECTIVE_EXPORT
)))
336 bfd_byte name_end_char
;
338 /* Skip the directive. */
339 s
+= strlen (DIRECTIVE_EXPORT
);
341 /* Find the end of the name to be exported. */
342 SKIP_UNTIL (s
, e
, '\n', "EXPORT: no newline at end of directive");
343 /* Skip (backwards) over spaces at end of exported name. */
344 for (name_end
= s
; name_end
[-1] == ' '; name_end
--)
346 /* name_end now points at the first character after the
347 end of the exported name, so we can termiante it */
348 name_end_char
= * name_end
;
350 /* Skip passed the newline character. */
353 result
= sh_symbian_export (abfd
, name
);
355 /* The next character should be a NUL. */
359 fprintf (stderr
, "EXPORT: Junk at end of directive\n");
364 /* Restore the character we deleted. */
365 * name_end
= name_end_char
;
377 fprintf (stderr
, "offset into .directive section: %d\n", directive
- contents
);
379 bfd_set_error (bfd_error_invalid_operation
);
380 _bfd_error_handler (_("%B: Unrecognised .directive command: %s"),
390 /* Scan a bfd for a .directive section, and if found process it.
391 Returns TRUE upon success, FALSE otherwise. */
392 bfd_boolean
bfd_elf32_sh_symbian_process_directives (struct bfd_link_info
*info
, bfd
* abfd
);
395 bfd_elf32_sh_symbian_process_directives (struct bfd_link_info
*info
, bfd
* abfd
)
397 bfd_boolean result
= FALSE
;
399 asection
* sec
= bfd_get_section_by_name (abfd
, ".directive");
405 sz
= sec
->rawsize
? sec
->rawsize
: sec
->size
;
406 contents
= bfd_malloc (sz
);
409 bfd_set_error (bfd_error_no_memory
);
412 if (bfd_get_section_contents (abfd
, sec
, contents
, 0, sz
))
413 result
= sh_symbian_process_embedded_commands (info
, abfd
, sec
, contents
);
420 /* Intercept the normal sh_relocate_section() function
421 and magle the relocs to allow for symbol renaming. */
424 sh_symbian_relocate_section (bfd
* output_bfd
,
425 struct bfd_link_info
* info
,
427 asection
* input_section
,
429 Elf_Internal_Rela
* relocs
,
430 Elf_Internal_Sym
* local_syms
,
431 asection
** local_sections
)
433 /* When performing a final link we implement the IMPORT AS directives. */
434 if (!info
->relocatable
)
436 Elf_Internal_Rela
* rel
;
437 Elf_Internal_Rela
* relend
;
438 Elf_Internal_Shdr
* symtab_hdr
;
439 struct elf_link_hash_entry
** sym_hashes
;
440 struct elf_link_hash_entry
** sym_hashes_end
;
441 struct elf_link_hash_table
* hash_table
;
443 bfd_size_type num_global_syms
;
444 unsigned long num_local_syms
;
446 BFD_ASSERT (! elf_bad_symtab (input_bfd
));
448 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
449 hash_table
= elf_hash_table (info
);
450 num_local_syms
= symtab_hdr
->sh_info
;
451 num_global_syms
= symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
452 num_global_syms
-= num_local_syms
;
453 sym_hashes
= elf_sym_hashes (input_bfd
);
454 sym_hashes_end
= sym_hashes
+ num_global_syms
;
456 /* First scan the rename table, caching the hash entry and the new index. */
457 for (ptr
= rename_list
; ptr
; ptr
= ptr
->next
)
459 struct elf_link_hash_entry
* new_hash
;
460 struct elf_link_hash_entry
** h
;
462 ptr
->current_hash
= elf_link_hash_lookup (hash_table
, ptr
->current_name
, FALSE
, FALSE
, TRUE
);
464 if (ptr
->current_hash
== NULL
)
467 fprintf (stderr
, "IMPORT AS: current symbol '%s' does not exist\n", ptr
->current_name
);
471 new_hash
= elf_link_hash_lookup (hash_table
, ptr
->new_name
, FALSE
, FALSE
, TRUE
);
473 /* If we could not find the symbol then it is a new, undefined symbol.
474 Symbian want this behaviour - ie they want to be able to rename the
475 reference in a reloc from one undefined symbol to another, new and
476 undefined symbol. So we create that symbol here. */
477 if (new_hash
== NULL
)
479 asection
* psec
= bfd_und_section_ptr
;
480 Elf_Internal_Sym new_sym
;
481 bfd_vma new_value
= 0;
483 bfd_boolean override
;
484 bfd_boolean type_change_ok
;
485 bfd_boolean size_change_ok
;
487 new_sym
.st_value
= 0;
489 new_sym
.st_name
= -1;
490 new_sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_FUNC
);
491 new_sym
.st_other
= ELF_ST_VISIBILITY (STV_DEFAULT
);
492 new_sym
.st_shndx
= SHN_UNDEF
;
494 if (! _bfd_elf_merge_symbol (input_bfd
, info
, ptr
->new_name
, & new_sym
, & psec
,
495 & new_value
, & new_hash
, & skip
, & override
, & type_change_ok
,
498 _bfd_error_handler (_("%B: Failed to add renamed symbol %s"),
499 input_bfd
, ptr
->new_name
);
502 /* XXX - should we check psec, skip, override etc ? */
504 new_hash
->root
.type
= bfd_link_hash_undefined
;
506 /* Allow the symbol to become local if necessary. */
507 if (new_hash
->dynindx
== -1)
508 new_hash
->def_regular
= 1;
511 fprintf (stderr
, "Created new symbol %s\n", ptr
->new_name
);
514 /* Convert the new_hash value into a index into the table of symbol hashes. */
515 for (h
= sym_hashes
; h
< sym_hashes_end
; h
++)
519 ptr
->new_symndx
= h
- sym_hashes
+ num_local_syms
;
521 fprintf (stderr
, "Converted new hash to index of %ld\n", ptr
->new_symndx
);
525 /* If the new symbol is not in the hash table then it must be
526 because it is one of the newly created undefined symbols
527 manufactured above. So we extend the sym has table here to
528 include this extra symbol. */
529 if (h
== sym_hashes_end
)
531 struct elf_link_hash_entry
** new_sym_hashes
;
533 /* This is not very efficient, but it works. */
535 new_sym_hashes
= bfd_alloc (input_bfd
, num_global_syms
* sizeof * sym_hashes
);
536 if (new_sym_hashes
== NULL
)
539 fprintf (stderr
, "Out of memory extending hash table\n");
542 memcpy (new_sym_hashes
, sym_hashes
, (num_global_syms
- 1) * sizeof * sym_hashes
);
543 new_sym_hashes
[num_global_syms
- 1] = new_hash
;
544 elf_sym_hashes (input_bfd
) = sym_hashes
= new_sym_hashes
;
545 sym_hashes_end
= sym_hashes
+ num_global_syms
;
546 symtab_hdr
->sh_size
= (num_global_syms
+ num_local_syms
) * sizeof (Elf32_External_Sym
);
548 ptr
->new_symndx
= num_global_syms
- 1 + num_local_syms
;
551 fprintf (stderr
, "Extended symbol hash table to insert new symbol as index %ld\n",
556 /* Walk the reloc list looking for references to renamed symbols.
557 When we find one, we alter the index in the reloc to point to the new symbol. */
558 for (rel
= relocs
, relend
= relocs
+ input_section
->reloc_count
;
563 unsigned long r_symndx
;
564 struct elf_link_hash_entry
* h
;
566 r_symndx
= ELF32_R_SYM (rel
->r_info
);
567 r_type
= ELF32_R_TYPE (rel
->r_info
);
569 /* Ignore unused relocs. */
570 if ((r_type
>= (int) R_SH_GNU_VTINHERIT
571 && r_type
<= (int) R_SH_LABEL
)
572 || r_type
== (int) R_SH_NONE
574 || r_type
>= R_SH_max
)
577 /* Ignore relocs against local symbols. */
578 if (r_symndx
< num_local_syms
)
581 BFD_ASSERT (r_symndx
< (num_global_syms
+ num_local_syms
));
582 h
= sym_hashes
[r_symndx
- num_local_syms
];
583 BFD_ASSERT (h
!= NULL
);
585 while ( h
->root
.type
== bfd_link_hash_indirect
586 || h
->root
.type
== bfd_link_hash_warning
)
587 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
589 /* If the symbol is defined there is no need to rename it.
590 XXX - is this true ? */
591 if ( h
->root
.type
== bfd_link_hash_defined
592 || h
->root
.type
== bfd_link_hash_defweak
593 || h
->root
.type
== bfd_link_hash_undefweak
)
596 for (ptr
= rename_list
; ptr
; ptr
= ptr
->next
)
597 if (h
== ptr
->current_hash
)
599 BFD_ASSERT (ptr
->new_symndx
);
601 fprintf (stderr
, "convert reloc %lx from using index %ld to using index %ld\n",
602 (long) rel
->r_info
, (long) ELF32_R_SYM (rel
->r_info
), ptr
->new_symndx
);
603 rel
->r_info
= ELF32_R_INFO (ptr
->new_symndx
, r_type
);
609 return sh_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
610 contents
, relocs
, local_syms
, local_sections
);
614 sh_symbian_check_directives (bfd
*abfd
, struct bfd_link_info
*info
)
616 return bfd_elf32_sh_symbian_process_directives (info
, abfd
);
619 #define TARGET_LITTLE_SYM bfd_elf32_shl_symbian_vec
620 #define TARGET_LITTLE_NAME "elf32-shl-symbian"
622 #undef elf_backend_relocate_section
623 #define elf_backend_relocate_section sh_symbian_relocate_section
624 #undef elf_backend_check_directives
625 #define elf_backend_check_directives sh_symbian_check_directives
627 #include "elf32-target.h"