This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

New objcopy option: --alt-machine-code


After we switched the ELF machine code of one of our tool-chains to an
officially-assigned number, one of our customers asked us whether
there was any way to generate binaries that could still be recognized
by old tool-chains, or by their own proprietary tools.  Since AFAIK
there wasn't any easy way to do it, I came up with this patch, that
Nick Clifton already approved, so I'm checking it in.

Index: bfd/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	* bfd.c (bfd_alt_mach_code): New function.
	* bfd-in2.h: Rebuilt.

Index: bfd/bfd-in2.h
===================================================================
RCS file: /cvs/src/src/bfd/bfd-in2.h,v
retrieving revision 1.109
diff -u -p -r1.109 bfd-in2.h
--- bfd/bfd-in2.h 2001/08/23 15:45:19 1.109
+++ bfd/bfd-in2.h 2001/08/24 13:29:32
@@ -3296,6 +3296,9 @@ extern bfd_byte *bfd_get_relocated_secti
                  struct bfd_link_order *, bfd_byte *,
                  boolean, asymbol **));
 
+boolean
+bfd_alt_mach_code PARAMS ((bfd *abfd, int index));
+
 symindex
 bfd_get_next_mapent PARAMS ((bfd *abfd, symindex previous, carsym **sym));
 
Index: bfd/bfd.c
===================================================================
RCS file: /cvs/src/src/bfd/bfd.c,v
retrieving revision 1.20
diff -u -p -r1.20 bfd.c
--- bfd/bfd.c 2001/08/17 15:56:57 1.20
+++ bfd/bfd.c 2001/08/24 13:29:32
@@ -1288,3 +1288,58 @@ bfd_fprintf_vma (abfd, stream, value)
   else
     fprintf_vma ((FILE *) stream, value);
 }
+
+/*
+FUNCTION
+	bfd_alt_mach_code
+
+SYNOPSIS
+	boolean bfd_alt_mach_code(bfd *abfd, int index);
+
+DESCRIPTION
+
+	When more than one machine code number is available for the
+	same machine type, this function can be used to switch between
+	the preferred one (index == 0) and any others.  Currently,
+	only ELF supports this feature, with up to two alternate
+	machine codes.
+*/
+
+boolean
+bfd_alt_mach_code (abfd, index)
+     bfd *abfd;
+     int index;
+{
+  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+    {
+      int code;
+
+      switch (index)
+	{
+	case 0:
+	  code = get_elf_backend_data (abfd)->elf_machine_code;
+	  break;
+
+	case 1:
+	  code = get_elf_backend_data (abfd)->elf_machine_alt1;
+	  if (code == 0)
+	    return false;
+	  break;
+
+	case 2:
+	  code = get_elf_backend_data (abfd)->elf_machine_alt2;
+	  if (code == 0)
+	    return false;
+	  break;
+
+	default:
+	  return false;
+	}
+
+      elf_elfheader (abfd)->e_machine = code;
+
+      return true;
+    }
+
+  return false;
+}
Index: binutils/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	* objcopy.c (use_alt_mach_code): New variable.
	(OPTION_ALT_MACH_CODE): Define.
	(copy_options): Added --alt-machine-code.
	(copy_main): Handle it.
	(copy_object): Switch to alternate machine code if requested.
	* doc/binutils.texi: Document new option.
	* NEWS: Likewise.

Index: binutils/NEWS
===================================================================
RCS file: /cvs/src/src/binutils/NEWS,v
retrieving revision 1.18
diff -u -p -r1.18 NEWS
--- binutils/NEWS 2001/07/31 13:49:38 1.18
+++ binutils/NEWS 2001/08/24 13:44:19
@@ -11,6 +11,10 @@
 
 * Support for OpenRISC by Johan Rydberg.
 
+* New command line switch to objcopy --alt-machine-code which creates a binary
+  with an alternate machine code if one is defined in the architecture
+  description.  Only supported for ELF targets.  By Alexandre Oliva.
+
 * New command line switch to objcopy -B (or --binary-architecture) which sets
   the architecture of the output file to the given argument.  This option only
   makes sense, if the input target is binary.  Otherwise it is ignored.
Index: binutils/objcopy.c
===================================================================
RCS file: /cvs/src/src/binutils/objcopy.c,v
retrieving revision 1.25
diff -u -p -r1.25 objcopy.c
--- binutils/objcopy.c 2001/07/31 13:49:38 1.25
+++ binutils/objcopy.c 2001/08/24 13:44:19
@@ -171,6 +171,9 @@ static bfd_byte gap_fill = 0;
 static boolean pad_to_set = false;
 static bfd_vma pad_to;
 
+/* Use alternate machine code?  */
+static int use_alt_mach_code = 0;
+
 /* List of sections to add.  */
 struct section_add
 {
@@ -240,6 +243,7 @@ static boolean weaken = false;
 #define OPTION_KEEPGLOBAL_SYMBOLS (OPTION_LOCALIZE_SYMBOLS + 1)
 #define OPTION_WEAKEN_SYMBOLS (OPTION_KEEPGLOBAL_SYMBOLS + 1)
 #define OPTION_RENAME_SECTION (OPTION_WEAKEN_SYMBOLS + 1)
+#define OPTION_ALT_MACH_CODE (OPTION_RENAME_SECTION + 1)
 
 /* Options to handle if running as "strip".  */
 
@@ -326,6 +330,7 @@ static struct option copy_options[] =
   {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
   {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
   {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
+  {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
   {0, no_argument, 0, 0}
 };
 
@@ -408,6 +413,7 @@ copy_usage (stream, exit_status)
      --localize-symbols <file>     -L for all symbols listed in <file>\n\
      --keep-global-symbols <file>  -G for all symbols listed in <file>\n\
      --weaken-symbols <file>       -W for all symbols listed in <file>\n\
+     --alt-machine-code <index>    Use alternate machine code for output\n\
   -v --verbose                     List all object files modified\n\
   -V --version                     Display this program's version number\n\
   -h --help                        Display this output\n\
@@ -1244,6 +1250,15 @@ copy_object (ibfd, obfd)
       status = 1;
       return;
     }
+
+  /* Switch to the alternate machine code.  We have to do this at the
+     very end, because we only initialize the header when we create
+     the first section.  */
+  if (use_alt_mach_code != 0)
+    {
+      if (!bfd_alt_mach_code (obfd, use_alt_mach_code))
+	non_fatal (_("unknown alternate machine code, ignored"));
+    }
 }
 
 /* Read each archive element in turn from IBFD, copy the
@@ -2460,6 +2475,12 @@ copy_main (argc, argv)
 
 	case OPTION_WEAKEN_SYMBOLS:
 	  add_specific_symbols (optarg, &weaken_specific_list);
+	  break;
+
+	case OPTION_ALT_MACH_CODE:
+	  use_alt_mach_code = atoi (optarg);
+	  if (use_alt_mach_code <= 0)
+	    fatal (_("alternate machine code index must be positive"));
 	  break;
 
 	case 0:
Index: binutils/doc/binutils.texi
===================================================================
RCS file: /cvs/src/src/binutils/doc/binutils.texi,v
retrieving revision 1.5
diff -u -p -r1.5 binutils.texi
--- binutils/doc/binutils.texi 2001/08/05 21:01:26 1.5
+++ binutils/doc/binutils.texi 2001/08/24 13:44:21
@@ -954,6 +954,7 @@ objcopy [@option{-F} @var{bfdname}|@opti
         [@option{--keep-global-symbols=}@var{filename}]
         [@option{--localize-symbols=}@var{filename}]
         [@option{--weaken-symbols=}@var{filename}]
+        [@option{--alt-machine-code=@var{index}}]
         [@option{-v}|@option{--verbose}]
         [@option{-V}|@option{--version}]  
         [@option{--help}]
@@ -1305,6 +1306,13 @@ Apply @option{--weaken-symbol} option to
 @var{filename}.  @var{filename} is simply a flat file, with one symbol
 name per line.  Line comments may be introduced by the hash character.
 This option may be given more than once.
+
+@item --alt-machine-code=@var{index}
+If the output architecture has alternate machine codes, use the
+@var{index}th code instead of the default one.  This is useful in case
+a machine is assigned an official code and the tool-chain adopts the 
+new code, but other applications still depend on the original code
+being used.
 
 @item -V
 @itemx --version

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]