Index: src/bfd/bfd-in2.h =================================================================== --- src.orig/bfd/bfd-in2.h 2009-11-15 08:47:41.502879000 +0100 +++ src/bfd/bfd-in2.h 2009-11-15 09:09:47.193879900 +0100 @@ -5692,6 +5692,11 @@ const bfd_target *bfd_find_target (const char *target_name, bfd *abfd); +const bfd_target *bfd_get_target_info (const char *target_name, + bfd *abfd, + bfd_boolean *is_bigendian, + int *underscoring, + const char **def_target_arch); const char ** bfd_target_list (void); const bfd_target *bfd_search_for_target Index: src/bfd/targets.c =================================================================== --- src.orig/bfd/targets.c 2009-11-15 08:47:41.508879000 +0100 +++ src/bfd/targets.c 2009-11-15 09:00:00.528879900 +0100 @@ -1428,6 +1428,120 @@ return target; } +/* Helper function for bfd_get_target_info to determine the target's + architecture. This method handles bfd internal target names as + tuples and triplets. */ +static bfd_boolean +_bfd_find_arch_match (const char *tname, const char **arch, + const char **def_target_arch) +{ + if (!arch) + return FALSE; + + while (*arch != NULL) + { + const char *in_a = strstr (*arch, tname); + char end_ch = (in_a ? in_a[strlen (tname)] : 0); + + if (in_a && (in_a == *arch || in_a[-1] == ':') + && end_ch == 0) + { + *def_target_arch = *arch; + return TRUE; + } + arch++; + } + return FALSE; +} + +/* +FUNCTION + bfd_get_target_info +SYNOPSIS + const bfd_target *bfd_get_target_info (const char *target_name, + bfd *abfd, + bfd_boolean *is_bigendian, + int *underscoring, + const char **def_target_arch); +DESCRIPTION + Return a pointer to the transfer vector for the object target + named @var{target_name}. If @var{target_name} is <>, + choose the one in the environment variable <>; if + that is null or not defined, then choose the first entry in the + target list. Passing in the string "default" or setting the + environment variable to "default" will cause the first entry in + the target list to be returned, and "target_defaulted" will be + set in the BFD if @var{abfd} isn't <>. This causes + <> to loop over all the targets to find the + one that matches the file being read. + If @var{is_bigendian} is not <>, then set this value to target's + endian mode. True for big-endian, FALSE for little-endian or for + invalid target. + If @var{underscoring} is not <>, then set this value to target's + underscoring mode. Zero for none-underscoring, -1 for invalid target, + else the value of target vector's symbol underscoring. + If @var{def_target_arch} is not <>, then set it to the architecture + string specified by the target_name. +*/ +const bfd_target * +bfd_get_target_info (const char *target_name, bfd *abfd, + bfd_boolean *is_bigendian, + int *underscoring, const char **def_target_arch) +{ + const bfd_target *target_vec; + if (is_bigendian) + *is_bigendian = FALSE; + if (underscoring) + *underscoring = -1; + if (def_target_arch) + *def_target_arch = NULL; + target_vec = bfd_find_target (target_name, abfd); + if (! target_vec) + return NULL; + if (is_bigendian) + *is_bigendian = ((target_vec->byteorder == BFD_ENDIAN_BIG) ? TRUE + : FALSE); + if (underscoring) + *underscoring = ((int) target_vec->symbol_leading_char) & 0xff; + + if (def_target_arch) + { + const char *tname = target_vec->name; + const char **arches = bfd_arch_list (); + + if (arches && tname) + { + char *hyp = strchr (tname, '-'); + + if (hyp != NULL) + { + tname = ++hyp; + + /* Make sure we detect architecture names + for triplets like "pe-arm-wince-little". */ + if (!_bfd_find_arch_match (tname, arches, def_target_arch)) + { + char *new_tname = (char *) alloca (strlen (hyp) + 1); + strcpy (new_tname, hyp); + while ((hyp = strrchr (new_tname, '-')) != NULL) + { + *hyp = 0; + if (_bfd_find_arch_match (new_tname, arches, + def_target_arch)) + break; + } + } + } + else + _bfd_find_arch_match (tname, arches, def_target_arch); + } + + if (arches) + free (arches); + } + return target_vec; +} + /* FUNCTION bfd_target_list Index: src/binutils/windmc.c =================================================================== --- src.orig/binutils/windmc.c 2009-11-15 08:47:41.511879000 +0100 +++ src/binutils/windmc.c 2009-11-15 08:55:43.723879900 +0100 @@ -236,42 +236,12 @@ const bfd_target *target_vec; def_target_arch = NULL; - target_vec = bfd_find_target (target, abfd); + target_vec = bfd_get_target_info (target, abfd, &target_is_bigendian, NULL, + &def_target_arch); if (! target_vec) fatal ("Can't detect target endianess and architecture."); - target_is_bigendian = ((target_vec->byteorder == BFD_ENDIAN_BIG) ? 1 : 0); - - { - const char * tname = target_vec->name; - const char ** arches = bfd_arch_list (); - - if (arches && tname) - { - const char ** arch = arches; - - if (strchr (tname, '-') != NULL) - tname = strchr (tname, '-') + 1; - - while (*arch != NULL) - { - const char *in_a = strstr (*arch, tname); - char end_ch = (in_a ? in_a[strlen (tname)] : 0); - - if (in_a && (in_a == *arch || in_a[-1] == ':') - && end_ch == 0) - { - def_target_arch = *arch; - break; - } - arch++; - } - } - - free (arches); - - if (! def_target_arch) - fatal ("Can't detect architecture."); - } + if (! def_target_arch) + fatal ("Can't detect architecture."); } static int Index: src/binutils/windres.c =================================================================== --- src.orig/binutils/windres.c 2009-11-15 08:47:41.514879000 +0100 +++ src/binutils/windres.c 2009-11-15 08:55:43.730879900 +0100 @@ -1057,71 +1057,18 @@ return 0; } -static int -find_arch_match(const char *tname,const char **arch) -{ - while (*arch != NULL) - { - const char *in_a = strstr (*arch, tname); - char end_ch = (in_a ? in_a[strlen(tname)] : 0); - - if (in_a && (in_a == *arch || in_a[-1] == ':') - && end_ch == 0) - { - def_target_arch = *arch; - return 1; - } - arch++; - } - return 0; -} - static void set_endianess (bfd *abfd, const char *target) { const bfd_target *target_vec; def_target_arch = NULL; - target_vec = bfd_find_target (target, abfd); + target_vec = bfd_get_target_info (target, abfd, &target_is_bigendian, NULL, + &def_target_arch); if (! target_vec) fatal ("Can't detect target endianess and architecture."); - target_is_bigendian = ((target_vec->byteorder == BFD_ENDIAN_BIG) ? 1 : 0); - - { - const char * tname = target_vec->name; - const char ** arches = bfd_arch_list(); - - if (arches && tname) - { - char *hyp = strchr (tname, '-'); - - if (hyp != NULL) - { - tname = ++hyp; - - /* Make sure we dectect architecture names - for triplets like "pe-arm-wince-little". */ - if (!find_arch_match (tname, arches)) - { - char *new_tname = (char *) alloca (strlen (hyp) + 1); - strcpy (new_tname, hyp); - while ((hyp = strrchr (new_tname, '-')) != NULL) - { - *hyp = 0; - if (find_arch_match (new_tname, arches)) - break; - } - } - } - else - find_arch_match (tname, arches); - } - - free (arches); - - if (! def_target_arch) - fatal ("Can't detect architecture."); - } + if (! def_target_arch) + fatal ("Can't detect architecture."); } bfd *