OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [binutils-2.18.50/] [bfd/] [elf32-sh-symbian.c] - Diff between revs 156 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 156 Rev 816
/* Renesas / SuperH specific support for Symbian 32-bit ELF files
/* Renesas / SuperH specific support for Symbian 32-bit ELF files
   Copyright 2004, 2005, 2006, 2007
   Copyright 2004, 2005, 2006, 2007
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
   Contributed by Red Hat
   Contributed by Red Hat
 
 
   This file is part of BFD, the Binary File Descriptor library.
   This file is part of BFD, the Binary File Descriptor library.
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */
   MA 02110-1301, USA.  */
 
 
 
 
/* Stop elf32-sh.c from defining any target vectors.  */
/* Stop elf32-sh.c from defining any target vectors.  */
#define SH_TARGET_ALREADY_DEFINED
#define SH_TARGET_ALREADY_DEFINED
#define sh_find_elf_flags           sh_symbian_find_elf_flags
#define sh_find_elf_flags           sh_symbian_find_elf_flags
#define sh_elf_get_flags_from_mach  sh_symbian_elf_get_flags_from_mach 
#define sh_elf_get_flags_from_mach  sh_symbian_elf_get_flags_from_mach 
#include "elf32-sh.c"
#include "elf32-sh.c"
 
 
 
 
//#define SYMBIAN_DEBUG 1
//#define SYMBIAN_DEBUG 1
#define SYMBIAN_DEBUG 0
#define SYMBIAN_DEBUG 0
 
 
#define DIRECTIVE_HEADER        "#<SYMEDIT>#\n"
#define DIRECTIVE_HEADER        "#<SYMEDIT>#\n"
#define DIRECTIVE_IMPORT        "IMPORT "
#define DIRECTIVE_IMPORT        "IMPORT "
#define DIRECTIVE_EXPORT        "EXPORT "
#define DIRECTIVE_EXPORT        "EXPORT "
#define DIRECTIVE_AS            "AS "
#define DIRECTIVE_AS            "AS "
 
 
/* Macro to advance 's' until either it reaches 'e' or the
/* Macro to advance 's' until either it reaches 'e' or the
   character pointed to by 's' is equal to 'c'.  If 'e' is
   character pointed to by 's' is equal to 'c'.  If 'e' is
   reached and SYMBIAN_DEBUG is enabled then the error message 'm'
   reached and SYMBIAN_DEBUG is enabled then the error message 'm'
   is displayed.  */
   is displayed.  */
#define SKIP_UNTIL(s,e,c,m)                                     \
#define SKIP_UNTIL(s,e,c,m)                                     \
  do                                                            \
  do                                                            \
    {                                                           \
    {                                                           \
      while (s < e && *s != c)                                  \
      while (s < e && *s != c)                                  \
        ++ s;                                                   \
        ++ s;                                                   \
      if (s >= e)                                               \
      if (s >= e)                                               \
        {                                                       \
        {                                                       \
          if (SYMBIAN_DEBUG)                                    \
          if (SYMBIAN_DEBUG)                                    \
            fprintf (stderr, "Corrupt directive: %s\n", m);     \
            fprintf (stderr, "Corrupt directive: %s\n", m);     \
          result = FALSE;                                       \
          result = FALSE;                                       \
        }                                                       \
        }                                                       \
    }                                                           \
    }                                                           \
  while (0);                                                     \
  while (0);                                                     \
  if (!result)                                                  \
  if (!result)                                                  \
     break;
     break;
 
 
/* Like SKIP_UNTIL except there are two terminator characters
/* Like SKIP_UNTIL except there are two terminator characters
   c1 and c2.  */
   c1 and c2.  */
#define SKIP_UNTIL2(s,e,c1,c2,m)                                \
#define SKIP_UNTIL2(s,e,c1,c2,m)                                \
  do                                                            \
  do                                                            \
    {                                                           \
    {                                                           \
      while (s < e && *s != c1 && *s != c2)                     \
      while (s < e && *s != c1 && *s != c2)                     \
        ++ s;                                                   \
        ++ s;                                                   \
      if (s >= e)                                               \
      if (s >= e)                                               \
        {                                                       \
        {                                                       \
          if (SYMBIAN_DEBUG)                                    \
          if (SYMBIAN_DEBUG)                                    \
            fprintf (stderr, "Corrupt directive: %s\n", m);     \
            fprintf (stderr, "Corrupt directive: %s\n", m);     \
          result = FALSE;                                       \
          result = FALSE;                                       \
        }                                                       \
        }                                                       \
    }                                                           \
    }                                                           \
  while (0);                                                     \
  while (0);                                                     \
  if (!result)                                                  \
  if (!result)                                                  \
     break;
     break;
 
 
/* Macro to advance 's' until either it reaches 'e' or the
/* Macro to advance 's' until either it reaches 'e' or the
   character pointed to by 's' is not equal to 'c'.  If 'e'
   character pointed to by 's' is not equal to 'c'.  If 'e'
   is reached and SYMBIAN_DEBUG is enabled then the error message
   is reached and SYMBIAN_DEBUG is enabled then the error message
   'm' is displayed.  */
   'm' is displayed.  */
#define SKIP_WHILE(s,e,c,m)                                     \
#define SKIP_WHILE(s,e,c,m)                                     \
  do                                                            \
  do                                                            \
    {                                                           \
    {                                                           \
      while (s < e && *s == c)                                  \
      while (s < e && *s == c)                                  \
        ++ s;                                                   \
        ++ s;                                                   \
      if (s >= e)                                               \
      if (s >= e)                                               \
        {                                                       \
        {                                                       \
          if (SYMBIAN_DEBUG)                                    \
          if (SYMBIAN_DEBUG)                                    \
            fprintf (stderr, "Corrupt directive: %s\n", m);     \
            fprintf (stderr, "Corrupt directive: %s\n", m);     \
          result = FALSE;                                       \
          result = FALSE;                                       \
        }                                                       \
        }                                                       \
    }                                                           \
    }                                                           \
  while (0);                                                     \
  while (0);                                                     \
  if (!result)                                                  \
  if (!result)                                                  \
     break;
     break;
 
 
 
 
typedef struct symbol_rename
typedef struct symbol_rename
{
{
  struct symbol_rename *       next;
  struct symbol_rename *       next;
  char *                       current_name;
  char *                       current_name;
  char *                       new_name;
  char *                       new_name;
  struct elf_link_hash_entry * current_hash;
  struct elf_link_hash_entry * current_hash;
  unsigned long                new_symndx;
  unsigned long                new_symndx;
}
}
symbol_rename;
symbol_rename;
 
 
static symbol_rename * rename_list = NULL;
static symbol_rename * rename_list = NULL;
 
 
/* Accumulate a list of symbols to be renamed.  */
/* Accumulate a list of symbols to be renamed.  */
 
 
static bfd_boolean
static bfd_boolean
sh_symbian_import_as (struct bfd_link_info *info, bfd * abfd,
sh_symbian_import_as (struct bfd_link_info *info, bfd * abfd,
                      char * current_name, char * new_name)
                      char * current_name, char * new_name)
{
{
  struct elf_link_hash_entry * new_hash;
  struct elf_link_hash_entry * new_hash;
  symbol_rename * node;
  symbol_rename * node;
 
 
  if (SYMBIAN_DEBUG)
  if (SYMBIAN_DEBUG)
    fprintf (stderr, "IMPORT '%s' AS '%s'\n", current_name, new_name);
    fprintf (stderr, "IMPORT '%s' AS '%s'\n", current_name, new_name);
 
 
  for (node = rename_list; node; node = node->next)
  for (node = rename_list; node; node = node->next)
    if (strcmp (node->current_name, current_name) == 0)
    if (strcmp (node->current_name, current_name) == 0)
      {
      {
        if (strcmp (node->new_name, new_name) == 0)
        if (strcmp (node->new_name, new_name) == 0)
          /* Already added to rename list.  */
          /* Already added to rename list.  */
          return TRUE;
          return TRUE;
 
 
        bfd_set_error (bfd_error_invalid_operation);
        bfd_set_error (bfd_error_invalid_operation);
        _bfd_error_handler (_("%B: IMPORT AS directive for %s conceals previous IMPORT AS"),
        _bfd_error_handler (_("%B: IMPORT AS directive for %s conceals previous IMPORT AS"),
                            abfd, current_name);
                            abfd, current_name);
        return FALSE;
        return FALSE;
      }
      }
 
 
  if ((node = bfd_malloc (sizeof * node)) == NULL)
  if ((node = bfd_malloc (sizeof * node)) == NULL)
    {
    {
      if (SYMBIAN_DEBUG)
      if (SYMBIAN_DEBUG)
        fprintf (stderr, "IMPORT AS: No mem for new rename node\n");
        fprintf (stderr, "IMPORT AS: No mem for new rename node\n");
      return FALSE;
      return FALSE;
    }
    }
 
 
  if ((node->current_name = bfd_malloc (strlen (current_name) + 1)) == NULL)
  if ((node->current_name = bfd_malloc (strlen (current_name) + 1)) == NULL)
    {
    {
      if (SYMBIAN_DEBUG)
      if (SYMBIAN_DEBUG)
        fprintf (stderr, "IMPORT AS: No mem for current name field in rename node\n");
        fprintf (stderr, "IMPORT AS: No mem for current name field in rename node\n");
      free (node);
      free (node);
      return FALSE;
      return FALSE;
    }
    }
  else
  else
    strcpy (node->current_name, current_name);
    strcpy (node->current_name, current_name);
 
 
  if ((node->new_name = bfd_malloc (strlen (new_name) + 1)) == NULL)
  if ((node->new_name = bfd_malloc (strlen (new_name) + 1)) == NULL)
    {
    {
      if (SYMBIAN_DEBUG)
      if (SYMBIAN_DEBUG)
        fprintf (stderr, "IMPORT AS: No mem for new name field in rename node\n");
        fprintf (stderr, "IMPORT AS: No mem for new name field in rename node\n");
      free (node->current_name);
      free (node->current_name);
      free (node);
      free (node);
      return FALSE;
      return FALSE;
    }
    }
  else
  else
    strcpy (node->new_name, new_name);
    strcpy (node->new_name, new_name);
 
 
  node->next = rename_list;
  node->next = rename_list;
  node->current_hash = NULL;
  node->current_hash = NULL;
  node->new_symndx = 0;
  node->new_symndx = 0;
  rename_list = node;
  rename_list = node;
 
 
  new_hash = elf_link_hash_lookup (elf_hash_table (info), node->new_name, TRUE, FALSE, TRUE);
  new_hash = elf_link_hash_lookup (elf_hash_table (info), node->new_name, TRUE, FALSE, TRUE);
  bfd_elf_link_record_dynamic_symbol (info, new_hash);
  bfd_elf_link_record_dynamic_symbol (info, new_hash);
  if (new_hash->root.type == bfd_link_hash_new)
  if (new_hash->root.type == bfd_link_hash_new)
    new_hash->root.type = bfd_link_hash_undefined;
    new_hash->root.type = bfd_link_hash_undefined;
 
 
  return TRUE;
  return TRUE;
}
}
 
 
 
 
static bfd_boolean
static bfd_boolean
sh_symbian_import (bfd * abfd ATTRIBUTE_UNUSED, char * name)
sh_symbian_import (bfd * abfd ATTRIBUTE_UNUSED, char * name)
{
{
  if (SYMBIAN_DEBUG)
  if (SYMBIAN_DEBUG)
    fprintf (stderr, "IMPORT '%s'\n", name);
    fprintf (stderr, "IMPORT '%s'\n", name);
 
 
  /* XXX: Generate an import somehow ?  */
  /* XXX: Generate an import somehow ?  */
 
 
  return TRUE;
  return TRUE;
}
}
 
 
static bfd_boolean
static bfd_boolean
sh_symbian_export (bfd * abfd ATTRIBUTE_UNUSED, char * name)
sh_symbian_export (bfd * abfd ATTRIBUTE_UNUSED, char * name)
{
{
  if (SYMBIAN_DEBUG)
  if (SYMBIAN_DEBUG)
    fprintf (stderr, "EXPORT '%s'\n", name);
    fprintf (stderr, "EXPORT '%s'\n", name);
 
 
  /* XXX: Generate an export somehow ?  */
  /* XXX: Generate an export somehow ?  */
 
 
  return TRUE;
  return TRUE;
}
}
 
 
/* Process any magic embedded commands in the .directive. section.
/* Process any magic embedded commands in the .directive. section.
   Returns TRUE upon sucecss, but if it fails it sets bfd_error and
   Returns TRUE upon sucecss, but if it fails it sets bfd_error and
   returns FALSE.  */
   returns FALSE.  */
 
 
static bfd_boolean
static bfd_boolean
sh_symbian_process_embedded_commands (struct bfd_link_info *info, bfd * abfd,
sh_symbian_process_embedded_commands (struct bfd_link_info *info, bfd * abfd,
                                      asection * sec, bfd_byte * contents)
                                      asection * sec, bfd_byte * contents)
{
{
  char *s;
  char *s;
  char *e;
  char *e;
  bfd_boolean result = TRUE;
  bfd_boolean result = TRUE;
  bfd_size_type sz = sec->rawsize ? sec->rawsize : sec->size;
  bfd_size_type sz = sec->rawsize ? sec->rawsize : sec->size;
 
 
  for (s = (char *) contents, e = s + sz; s < e;)
  for (s = (char *) contents, e = s + sz; s < e;)
    {
    {
      char * directive = s;
      char * directive = s;
 
 
      switch (*s)
      switch (*s)
        {
        {
          /* I want to use "case DIRECTIVE_HEADER [0]:" here but gcc won't let me :-(  */
          /* I want to use "case DIRECTIVE_HEADER [0]:" here but gcc won't let me :-(  */
        case '#':
        case '#':
          if (strcmp (s, DIRECTIVE_HEADER))
          if (strcmp (s, DIRECTIVE_HEADER))
            result = FALSE;
            result = FALSE;
          else
          else
            /* Just ignore the header.
            /* Just ignore the header.
               XXX: Strictly speaking we ought to check that the header
               XXX: Strictly speaking we ought to check that the header
               is present and that it is the first thing in the file.  */
               is present and that it is the first thing in the file.  */
            s += strlen (DIRECTIVE_HEADER) + 1;
            s += strlen (DIRECTIVE_HEADER) + 1;
          break;
          break;
 
 
        case 'I':
        case 'I':
          if (! CONST_STRNEQ (s, DIRECTIVE_IMPORT))
          if (! CONST_STRNEQ (s, DIRECTIVE_IMPORT))
            result = FALSE;
            result = FALSE;
          else
          else
            {
            {
              char * new_name;
              char * new_name;
              char * new_name_end;
              char * new_name_end;
              char   name_end_char;
              char   name_end_char;
 
 
              /* Skip the IMPORT directive.  */
              /* Skip the IMPORT directive.  */
              s += strlen (DIRECTIVE_IMPORT);
              s += strlen (DIRECTIVE_IMPORT);
 
 
              new_name = s;
              new_name = s;
              /* Find the end of the new name.  */
              /* Find the end of the new name.  */
              while (s < e && *s != ' ' && *s != '\n')
              while (s < e && *s != ' ' && *s != '\n')
                ++ s;
                ++ s;
              if (s >= e)
              if (s >= e)
                {
                {
                  /* We have reached the end of the .directive section
                  /* We have reached the end of the .directive section
                     without encountering a string terminator.  This is
                     without encountering a string terminator.  This is
                     allowed for IMPORT directives.  */
                     allowed for IMPORT directives.  */
                  new_name_end   = e - 1;
                  new_name_end   = e - 1;
                  name_end_char  = * new_name_end;
                  name_end_char  = * new_name_end;
                  * new_name_end = 0;
                  * new_name_end = 0;
                  result = sh_symbian_import (abfd, new_name);
                  result = sh_symbian_import (abfd, new_name);
                  * new_name_end = name_end_char;
                  * new_name_end = name_end_char;
                  break;
                  break;
                }
                }
 
 
              /* Remember where the name ends.  */
              /* Remember where the name ends.  */
              new_name_end = s;
              new_name_end = s;
              /* Skip any whitespace before the 'AS'.  */
              /* Skip any whitespace before the 'AS'.  */
              SKIP_WHILE (s, e, ' ', "IMPORT: Name just followed by spaces");
              SKIP_WHILE (s, e, ' ', "IMPORT: Name just followed by spaces");
              /* Terminate the new name.  (Do this after skiping...)  */
              /* Terminate the new name.  (Do this after skiping...)  */
              name_end_char = * new_name_end;
              name_end_char = * new_name_end;
              * new_name_end = 0;
              * new_name_end = 0;
 
 
              /* Check to see if 'AS '... is present.  If so we have an
              /* Check to see if 'AS '... is present.  If so we have an
                 IMPORT AS directive, otherwise we have an IMPORT directive.  */
                 IMPORT AS directive, otherwise we have an IMPORT directive.  */
              if (! CONST_STRNEQ (s, DIRECTIVE_AS))
              if (! CONST_STRNEQ (s, DIRECTIVE_AS))
                {
                {
                  /* Skip the new-line at the end of the name.  */
                  /* Skip the new-line at the end of the name.  */
                  if (SYMBIAN_DEBUG && name_end_char != '\n')
                  if (SYMBIAN_DEBUG && name_end_char != '\n')
                    fprintf (stderr, "IMPORT: No newline at end of directive\n");
                    fprintf (stderr, "IMPORT: No newline at end of directive\n");
                  else
                  else
                    s ++;
                    s ++;
 
 
                  result = sh_symbian_import (abfd, new_name);
                  result = sh_symbian_import (abfd, new_name);
 
 
                  /* Skip past the NUL character.  */
                  /* Skip past the NUL character.  */
                  if (* s ++ != 0)
                  if (* s ++ != 0)
                    {
                    {
                      if (SYMBIAN_DEBUG)
                      if (SYMBIAN_DEBUG)
                        fprintf (stderr, "IMPORT: No NUL at end of directive\n");
                        fprintf (stderr, "IMPORT: No NUL at end of directive\n");
                    }
                    }
                }
                }
              else
              else
                {
                {
                  char * current_name;
                  char * current_name;
                  char * current_name_end;
                  char * current_name_end;
                  char   current_name_end_char;
                  char   current_name_end_char;
 
 
                  /* Skip the 'AS '.  */
                  /* Skip the 'AS '.  */
                  s += strlen (DIRECTIVE_AS);
                  s += strlen (DIRECTIVE_AS);
                  /* Skip any white space after the 'AS '.  */
                  /* Skip any white space after the 'AS '.  */
                  SKIP_WHILE (s, e, ' ', "IMPORT AS: Nothing after AS");
                  SKIP_WHILE (s, e, ' ', "IMPORT AS: Nothing after AS");
                  current_name = s;
                  current_name = s;
                  /* Find the end of the current name.  */
                  /* Find the end of the current name.  */
                  SKIP_UNTIL2 (s, e, ' ', '\n', "IMPORT AS: No newline at the end of the current name");
                  SKIP_UNTIL2 (s, e, ' ', '\n', "IMPORT AS: No newline at the end of the current name");
                  /* Skip (backwards) over spaces at the end of the current name.  */
                  /* Skip (backwards) over spaces at the end of the current name.  */
                  current_name_end = s;
                  current_name_end = s;
                  current_name_end_char = * current_name_end;
                  current_name_end_char = * current_name_end;
 
 
                  SKIP_WHILE (s, e, ' ', "IMPORT AS: Current name just followed by spaces");
                  SKIP_WHILE (s, e, ' ', "IMPORT AS: Current name just followed by spaces");
                  /* Skip past the newline character.  */
                  /* Skip past the newline character.  */
                  if (* s ++ != '\n')
                  if (* s ++ != '\n')
                    if (SYMBIAN_DEBUG)
                    if (SYMBIAN_DEBUG)
                      fprintf (stderr, "IMPORT AS: No newline at end of directive\n");
                      fprintf (stderr, "IMPORT AS: No newline at end of directive\n");
 
 
                  /* Terminate the current name after having performed the skips.  */
                  /* Terminate the current name after having performed the skips.  */
                  * current_name_end = 0;
                  * current_name_end = 0;
 
 
                  result = sh_symbian_import_as (info, abfd, current_name, new_name);
                  result = sh_symbian_import_as (info, abfd, current_name, new_name);
 
 
                  /* The next character should be a NUL.  */
                  /* The next character should be a NUL.  */
                  if (* s != 0)
                  if (* s != 0)
                    {
                    {
                      if (SYMBIAN_DEBUG)
                      if (SYMBIAN_DEBUG)
                        fprintf (stderr, "IMPORT AS: Junk at end of directive\n");
                        fprintf (stderr, "IMPORT AS: Junk at end of directive\n");
                      result = FALSE;
                      result = FALSE;
                    }
                    }
                  s ++;
                  s ++;
 
 
                  * current_name_end = current_name_end_char;
                  * current_name_end = current_name_end_char;
                }
                }
 
 
              /* Restore the characters we overwrote, since
              /* Restore the characters we overwrote, since
                 the .directive section will be emitted.  */
                 the .directive section will be emitted.  */
              * new_name_end = name_end_char;
              * new_name_end = name_end_char;
            }
            }
          break;
          break;
 
 
        case 'E':
        case 'E':
          if (! CONST_STRNEQ (s, DIRECTIVE_EXPORT))
          if (! CONST_STRNEQ (s, DIRECTIVE_EXPORT))
            result = FALSE;
            result = FALSE;
          else
          else
            {
            {
              char * name;
              char * name;
              char * name_end;
              char * name_end;
              char   name_end_char;
              char   name_end_char;
 
 
              /* Skip the directive.  */
              /* Skip the directive.  */
              s += strlen (DIRECTIVE_EXPORT);
              s += strlen (DIRECTIVE_EXPORT);
              name = s;
              name = s;
              /* Find the end of the name to be exported.  */
              /* Find the end of the name to be exported.  */
              SKIP_UNTIL (s, e, '\n', "EXPORT: no newline at end of directive");
              SKIP_UNTIL (s, e, '\n', "EXPORT: no newline at end of directive");
              /* Skip (backwards) over spaces at end of exported name.  */
              /* Skip (backwards) over spaces at end of exported name.  */
              for (name_end = s; name_end[-1] == ' '; name_end --)
              for (name_end = s; name_end[-1] == ' '; name_end --)
                ;
                ;
              /* name_end now points at the first character after the
              /* name_end now points at the first character after the
                 end of the exported name, so we can termiante it  */
                 end of the exported name, so we can termiante it  */
              name_end_char = * name_end;
              name_end_char = * name_end;
              * name_end = 0;
              * name_end = 0;
              /* Skip passed the newline character.  */
              /* Skip passed the newline character.  */
              s ++;
              s ++;
 
 
              result = sh_symbian_export (abfd, name);
              result = sh_symbian_export (abfd, name);
 
 
              /* The next character should be a NUL.  */
              /* The next character should be a NUL.  */
              if (* s != 0)
              if (* s != 0)
                {
                {
                  if (SYMBIAN_DEBUG)
                  if (SYMBIAN_DEBUG)
                    fprintf (stderr, "EXPORT: Junk at end of directive\n");
                    fprintf (stderr, "EXPORT: Junk at end of directive\n");
                  result = FALSE;
                  result = FALSE;
                }
                }
              s++;
              s++;
 
 
              /* Restore the character we deleted.  */
              /* Restore the character we deleted.  */
              * name_end = name_end_char;
              * name_end = name_end_char;
            }
            }
          break;
          break;
 
 
        default:
        default:
          result = FALSE;
          result = FALSE;
          break;
          break;
        }
        }
 
 
      if (! result)
      if (! result)
        {
        {
          if (SYMBIAN_DEBUG)
          if (SYMBIAN_DEBUG)
            fprintf (stderr, "offset into .directive section: %ld\n",
            fprintf (stderr, "offset into .directive section: %ld\n",
                     (long) (directive - (char *) contents));
                     (long) (directive - (char *) contents));
 
 
          bfd_set_error (bfd_error_invalid_operation);
          bfd_set_error (bfd_error_invalid_operation);
          _bfd_error_handler (_("%B: Unrecognised .directive command: %s"),
          _bfd_error_handler (_("%B: Unrecognised .directive command: %s"),
                              abfd, directive);
                              abfd, directive);
          break;
          break;
        }
        }
    }
    }
 
 
  return result;
  return result;
}
}
 
 
 
 
/* Scan a bfd for a .directive section, and if found process it.
/* Scan a bfd for a .directive section, and if found process it.
   Returns TRUE upon success, FALSE otherwise.  */
   Returns TRUE upon success, FALSE otherwise.  */
bfd_boolean bfd_elf32_sh_symbian_process_directives (struct bfd_link_info *info, bfd * abfd);
bfd_boolean bfd_elf32_sh_symbian_process_directives (struct bfd_link_info *info, bfd * abfd);
 
 
bfd_boolean
bfd_boolean
bfd_elf32_sh_symbian_process_directives (struct bfd_link_info *info, bfd * abfd)
bfd_elf32_sh_symbian_process_directives (struct bfd_link_info *info, bfd * abfd)
{
{
  bfd_boolean result = FALSE;
  bfd_boolean result = FALSE;
  bfd_byte *  contents;
  bfd_byte *  contents;
  asection *  sec = bfd_get_section_by_name (abfd, ".directive");
  asection *  sec = bfd_get_section_by_name (abfd, ".directive");
  bfd_size_type sz;
  bfd_size_type sz;
 
 
  if (!sec)
  if (!sec)
    return TRUE;
    return TRUE;
 
 
  sz = sec->rawsize ? sec->rawsize : sec->size;
  sz = sec->rawsize ? sec->rawsize : sec->size;
  contents = bfd_malloc (sz);
  contents = bfd_malloc (sz);
 
 
  if (!contents)
  if (!contents)
    bfd_set_error (bfd_error_no_memory);
    bfd_set_error (bfd_error_no_memory);
  else
  else
    {
    {
      if (bfd_get_section_contents (abfd, sec, contents, 0, sz))
      if (bfd_get_section_contents (abfd, sec, contents, 0, sz))
        result = sh_symbian_process_embedded_commands (info, abfd, sec, contents);
        result = sh_symbian_process_embedded_commands (info, abfd, sec, contents);
      free (contents);
      free (contents);
    }
    }
 
 
  return result;
  return result;
}
}
 
 
/* Intercept the normal sh_relocate_section() function
/* Intercept the normal sh_relocate_section() function
   and magle the relocs to allow for symbol renaming.  */
   and magle the relocs to allow for symbol renaming.  */
 
 
static bfd_boolean
static bfd_boolean
sh_symbian_relocate_section (bfd *                  output_bfd,
sh_symbian_relocate_section (bfd *                  output_bfd,
                             struct bfd_link_info * info,
                             struct bfd_link_info * info,
                             bfd *                  input_bfd,
                             bfd *                  input_bfd,
                             asection *             input_section,
                             asection *             input_section,
                             bfd_byte *             contents,
                             bfd_byte *             contents,
                             Elf_Internal_Rela *    relocs,
                             Elf_Internal_Rela *    relocs,
                             Elf_Internal_Sym *     local_syms,
                             Elf_Internal_Sym *     local_syms,
                             asection **            local_sections)
                             asection **            local_sections)
{
{
  /* When performing a final link we implement the IMPORT AS directives.  */
  /* When performing a final link we implement the IMPORT AS directives.  */
  if (!info->relocatable)
  if (!info->relocatable)
    {
    {
      Elf_Internal_Rela *            rel;
      Elf_Internal_Rela *            rel;
      Elf_Internal_Rela *            relend;
      Elf_Internal_Rela *            relend;
      Elf_Internal_Shdr *            symtab_hdr;
      Elf_Internal_Shdr *            symtab_hdr;
      struct elf_link_hash_entry **  sym_hashes;
      struct elf_link_hash_entry **  sym_hashes;
      struct elf_link_hash_entry **  sym_hashes_end;
      struct elf_link_hash_entry **  sym_hashes_end;
      struct elf_link_hash_table *   hash_table;
      struct elf_link_hash_table *   hash_table;
      symbol_rename *                ptr;
      symbol_rename *                ptr;
      bfd_size_type                  num_global_syms;
      bfd_size_type                  num_global_syms;
      unsigned long                  num_local_syms;
      unsigned long                  num_local_syms;
 
 
      BFD_ASSERT (! elf_bad_symtab (input_bfd));
      BFD_ASSERT (! elf_bad_symtab (input_bfd));
 
 
      symtab_hdr       = & elf_tdata (input_bfd)->symtab_hdr;
      symtab_hdr       = & elf_tdata (input_bfd)->symtab_hdr;
      hash_table       = elf_hash_table (info);
      hash_table       = elf_hash_table (info);
      num_local_syms   = symtab_hdr->sh_info;
      num_local_syms   = symtab_hdr->sh_info;
      num_global_syms  = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
      num_global_syms  = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
      num_global_syms -= num_local_syms;
      num_global_syms -= num_local_syms;
      sym_hashes       = elf_sym_hashes (input_bfd);
      sym_hashes       = elf_sym_hashes (input_bfd);
      sym_hashes_end   = sym_hashes + num_global_syms;
      sym_hashes_end   = sym_hashes + num_global_syms;
 
 
      /* First scan the rename table, caching the hash entry and the new index.  */
      /* First scan the rename table, caching the hash entry and the new index.  */
      for (ptr = rename_list; ptr; ptr = ptr->next)
      for (ptr = rename_list; ptr; ptr = ptr->next)
        {
        {
          struct elf_link_hash_entry *   new_hash;
          struct elf_link_hash_entry *   new_hash;
          struct elf_link_hash_entry **  h;
          struct elf_link_hash_entry **  h;
 
 
          ptr->current_hash = elf_link_hash_lookup (hash_table, ptr->current_name, FALSE, FALSE, TRUE);
          ptr->current_hash = elf_link_hash_lookup (hash_table, ptr->current_name, FALSE, FALSE, TRUE);
 
 
          if (ptr->current_hash == NULL)
          if (ptr->current_hash == NULL)
            {
            {
              if (SYMBIAN_DEBUG)
              if (SYMBIAN_DEBUG)
                fprintf (stderr, "IMPORT AS: current symbol '%s' does not exist\n", ptr->current_name);
                fprintf (stderr, "IMPORT AS: current symbol '%s' does not exist\n", ptr->current_name);
              continue;
              continue;
            }
            }
 
 
          new_hash = elf_link_hash_lookup (hash_table, ptr->new_name, FALSE, FALSE, TRUE);
          new_hash = elf_link_hash_lookup (hash_table, ptr->new_name, FALSE, FALSE, TRUE);
 
 
          /* If we could not find the symbol then it is a new, undefined symbol.
          /* If we could not find the symbol then it is a new, undefined symbol.
             Symbian want this behaviour - ie they want to be able to rename the
             Symbian want this behaviour - ie they want to be able to rename the
             reference in a reloc from one undefined symbol to another, new and
             reference in a reloc from one undefined symbol to another, new and
             undefined symbol.  So we create that symbol here.  */
             undefined symbol.  So we create that symbol here.  */
          if (new_hash == NULL)
          if (new_hash == NULL)
            {
            {
              asection *                     psec = bfd_und_section_ptr;
              asection *                     psec = bfd_und_section_ptr;
              Elf_Internal_Sym               new_sym;
              Elf_Internal_Sym               new_sym;
              bfd_vma                        new_value = 0;
              bfd_vma                        new_value = 0;
              bfd_boolean                    skip;
              bfd_boolean                    skip;
              bfd_boolean                    override;
              bfd_boolean                    override;
              bfd_boolean                    type_change_ok;
              bfd_boolean                    type_change_ok;
              bfd_boolean                    size_change_ok;
              bfd_boolean                    size_change_ok;
 
 
              new_sym.st_value = 0;
              new_sym.st_value = 0;
              new_sym.st_size  = 0;
              new_sym.st_size  = 0;
              new_sym.st_name  = -1;
              new_sym.st_name  = -1;
              new_sym.st_info  = ELF_ST_INFO (STB_GLOBAL, STT_FUNC);
              new_sym.st_info  = ELF_ST_INFO (STB_GLOBAL, STT_FUNC);
              new_sym.st_other = ELF_ST_VISIBILITY (STV_DEFAULT);
              new_sym.st_other = ELF_ST_VISIBILITY (STV_DEFAULT);
              new_sym.st_shndx = SHN_UNDEF;
              new_sym.st_shndx = SHN_UNDEF;
 
 
              if (! _bfd_elf_merge_symbol (input_bfd, info,
              if (! _bfd_elf_merge_symbol (input_bfd, info,
                                           ptr->new_name, & new_sym,
                                           ptr->new_name, & new_sym,
                                           & psec, & new_value, NULL,
                                           & psec, & new_value, NULL,
                                           & new_hash, & skip,
                                           & new_hash, & skip,
                                           & override, & type_change_ok,
                                           & override, & type_change_ok,
                                           & size_change_ok))
                                           & size_change_ok))
                {
                {
                  _bfd_error_handler (_("%B: Failed to add renamed symbol %s"),
                  _bfd_error_handler (_("%B: Failed to add renamed symbol %s"),
                                      input_bfd, ptr->new_name);
                                      input_bfd, ptr->new_name);
                  continue;
                  continue;
                }
                }
              /* XXX - should we check psec, skip, override etc ?  */
              /* XXX - should we check psec, skip, override etc ?  */
 
 
              new_hash->root.type = bfd_link_hash_undefined;
              new_hash->root.type = bfd_link_hash_undefined;
 
 
              /* Allow the symbol to become local if necessary.  */
              /* Allow the symbol to become local if necessary.  */
              if (new_hash->dynindx == -1)
              if (new_hash->dynindx == -1)
                new_hash->def_regular = 1;
                new_hash->def_regular = 1;
 
 
              if (SYMBIAN_DEBUG)
              if (SYMBIAN_DEBUG)
                fprintf (stderr, "Created new symbol %s\n", ptr->new_name);
                fprintf (stderr, "Created new symbol %s\n", ptr->new_name);
            }
            }
 
 
          /* Convert the new_hash value into a index into the table of symbol hashes.  */
          /* Convert the new_hash value into a index into the table of symbol hashes.  */
          for (h = sym_hashes; h < sym_hashes_end; h ++)
          for (h = sym_hashes; h < sym_hashes_end; h ++)
            {
            {
              if (* h == new_hash)
              if (* h == new_hash)
                {
                {
                  ptr->new_symndx = h - sym_hashes + num_local_syms;
                  ptr->new_symndx = h - sym_hashes + num_local_syms;
                  if (SYMBIAN_DEBUG)
                  if (SYMBIAN_DEBUG)
                    fprintf (stderr, "Converted new hash to index of %ld\n", ptr->new_symndx);
                    fprintf (stderr, "Converted new hash to index of %ld\n", ptr->new_symndx);
                  break;
                  break;
                }
                }
            }
            }
          /* If the new symbol is not in the hash table then it must be
          /* If the new symbol is not in the hash table then it must be
             because it is one of the newly created undefined symbols
             because it is one of the newly created undefined symbols
             manufactured above.  So we extend the sym has table here to
             manufactured above.  So we extend the sym has table here to
             include this extra symbol.  */
             include this extra symbol.  */
          if (h == sym_hashes_end)
          if (h == sym_hashes_end)
            {
            {
              struct elf_link_hash_entry **  new_sym_hashes;
              struct elf_link_hash_entry **  new_sym_hashes;
 
 
              /* This is not very efficient, but it works.  */
              /* This is not very efficient, but it works.  */
              ++ num_global_syms;
              ++ num_global_syms;
              new_sym_hashes = bfd_alloc (input_bfd, num_global_syms * sizeof * sym_hashes);
              new_sym_hashes = bfd_alloc (input_bfd, num_global_syms * sizeof * sym_hashes);
              if (new_sym_hashes == NULL)
              if (new_sym_hashes == NULL)
                {
                {
                  if (SYMBIAN_DEBUG)
                  if (SYMBIAN_DEBUG)
                    fprintf (stderr, "Out of memory extending hash table\n");
                    fprintf (stderr, "Out of memory extending hash table\n");
                  continue;
                  continue;
                }
                }
              memcpy (new_sym_hashes, sym_hashes, (num_global_syms - 1) * sizeof * sym_hashes);
              memcpy (new_sym_hashes, sym_hashes, (num_global_syms - 1) * sizeof * sym_hashes);
              new_sym_hashes[num_global_syms - 1] = new_hash;
              new_sym_hashes[num_global_syms - 1] = new_hash;
              elf_sym_hashes (input_bfd) = sym_hashes = new_sym_hashes;
              elf_sym_hashes (input_bfd) = sym_hashes = new_sym_hashes;
              sym_hashes_end = sym_hashes + num_global_syms;
              sym_hashes_end = sym_hashes + num_global_syms;
              symtab_hdr->sh_size  = (num_global_syms + num_local_syms) * sizeof (Elf32_External_Sym);
              symtab_hdr->sh_size  = (num_global_syms + num_local_syms) * sizeof (Elf32_External_Sym);
 
 
              ptr->new_symndx = num_global_syms - 1 + num_local_syms;
              ptr->new_symndx = num_global_syms - 1 + num_local_syms;
 
 
              if (SYMBIAN_DEBUG)
              if (SYMBIAN_DEBUG)
                fprintf (stderr, "Extended symbol hash table to insert new symbol as index %ld\n",
                fprintf (stderr, "Extended symbol hash table to insert new symbol as index %ld\n",
                         ptr->new_symndx);
                         ptr->new_symndx);
            }
            }
        }
        }
 
 
      /* Walk the reloc list looking for references to renamed symbols.
      /* Walk the reloc list looking for references to renamed symbols.
         When we find one, we alter the index in the reloc to point to the new symbol.  */
         When we find one, we alter the index in the reloc to point to the new symbol.  */
      for (rel = relocs, relend = relocs + input_section->reloc_count;
      for (rel = relocs, relend = relocs + input_section->reloc_count;
           rel < relend;
           rel < relend;
           rel ++)
           rel ++)
        {
        {
          int                          r_type;
          int                          r_type;
          unsigned long                r_symndx;
          unsigned long                r_symndx;
          struct elf_link_hash_entry * h;
          struct elf_link_hash_entry * h;
 
 
          r_symndx = ELF32_R_SYM (rel->r_info);
          r_symndx = ELF32_R_SYM (rel->r_info);
          r_type = ELF32_R_TYPE (rel->r_info);
          r_type = ELF32_R_TYPE (rel->r_info);
 
 
          /* Ignore unused relocs.  */
          /* Ignore unused relocs.  */
          if ((r_type >= (int) R_SH_GNU_VTINHERIT
          if ((r_type >= (int) R_SH_GNU_VTINHERIT
               && r_type <= (int) R_SH_LABEL)
               && r_type <= (int) R_SH_LABEL)
              || r_type == (int) R_SH_NONE
              || r_type == (int) R_SH_NONE
              || r_type < 0
              || r_type < 0
              || r_type >= R_SH_max)
              || r_type >= R_SH_max)
            continue;
            continue;
 
 
          /* Ignore relocs against local symbols.  */
          /* Ignore relocs against local symbols.  */
          if (r_symndx < num_local_syms)
          if (r_symndx < num_local_syms)
            continue;
            continue;
 
 
          BFD_ASSERT (r_symndx < (num_global_syms + num_local_syms));
          BFD_ASSERT (r_symndx < (num_global_syms + num_local_syms));
          h = sym_hashes[r_symndx - num_local_syms];
          h = sym_hashes[r_symndx - num_local_syms];
          BFD_ASSERT (h != NULL);
          BFD_ASSERT (h != NULL);
 
 
          while (   h->root.type == bfd_link_hash_indirect
          while (   h->root.type == bfd_link_hash_indirect
                 || h->root.type == bfd_link_hash_warning)
                 || h->root.type == bfd_link_hash_warning)
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
 
 
          /* If the symbol is defined there is no need to rename it.
          /* If the symbol is defined there is no need to rename it.
             XXX - is this true ?  */
             XXX - is this true ?  */
          if (   h->root.type == bfd_link_hash_defined
          if (   h->root.type == bfd_link_hash_defined
              || h->root.type == bfd_link_hash_defweak
              || h->root.type == bfd_link_hash_defweak
              || h->root.type == bfd_link_hash_undefweak)
              || h->root.type == bfd_link_hash_undefweak)
            continue;
            continue;
 
 
          for (ptr = rename_list; ptr; ptr = ptr->next)
          for (ptr = rename_list; ptr; ptr = ptr->next)
            if (h == ptr->current_hash)
            if (h == ptr->current_hash)
              {
              {
                BFD_ASSERT (ptr->new_symndx);
                BFD_ASSERT (ptr->new_symndx);
                if (SYMBIAN_DEBUG)
                if (SYMBIAN_DEBUG)
                  fprintf (stderr, "convert reloc %lx from using index %ld to using index %ld\n",
                  fprintf (stderr, "convert reloc %lx from using index %ld to using index %ld\n",
                           (long) rel->r_info, (long) ELF32_R_SYM (rel->r_info), ptr->new_symndx);
                           (long) rel->r_info, (long) ELF32_R_SYM (rel->r_info), ptr->new_symndx);
                rel->r_info = ELF32_R_INFO (ptr->new_symndx, r_type);
                rel->r_info = ELF32_R_INFO (ptr->new_symndx, r_type);
                break;
                break;
              }
              }
        }
        }
    }
    }
 
 
  return sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
  return sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                                  contents, relocs, local_syms, local_sections);
                                  contents, relocs, local_syms, local_sections);
}
}
 
 
static bfd_boolean
static bfd_boolean
sh_symbian_check_directives (bfd *abfd, struct bfd_link_info *info)
sh_symbian_check_directives (bfd *abfd, struct bfd_link_info *info)
{
{
  return bfd_elf32_sh_symbian_process_directives (info, abfd);
  return bfd_elf32_sh_symbian_process_directives (info, abfd);
}
}
 
 
#define TARGET_LITTLE_SYM       bfd_elf32_shl_symbian_vec
#define TARGET_LITTLE_SYM       bfd_elf32_shl_symbian_vec
#define TARGET_LITTLE_NAME      "elf32-shl-symbian"
#define TARGET_LITTLE_NAME      "elf32-shl-symbian"
 
 
#undef  elf_backend_relocate_section
#undef  elf_backend_relocate_section
#define elf_backend_relocate_section    sh_symbian_relocate_section
#define elf_backend_relocate_section    sh_symbian_relocate_section
#undef  elf_backend_check_directives
#undef  elf_backend_check_directives
#define elf_backend_check_directives    sh_symbian_check_directives
#define elf_backend_check_directives    sh_symbian_check_directives
 
 
#include "elf32-target.h"
#include "elf32-target.h"
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.