This is the mail archive of the binutils@sourceware.org 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]
Other format: [Raw text]

Re: arbitrary ELF machine-code


Hi Alexander,

$/opt/bin/mips-linux-uclibc-objcopy --alt-machine-code 0x1e hello.elf hello.bin
/opt/bin/mips-linux-uclibc-objcopy: alternate machine code index must be positive

Ah, OK, now I understand. There are two problems here. The first is that the code to parse the value provided with the --alt-machine-code switch was not very sophisticated. The second was that this switch was intended to allow the user to switch between alternative machine code values that are known to refer to the same target architecture and not to set the machine code to some arbitrary value.


The attached patch should resolve both of these problems. Please could you try it out and let me know how you get on.

Cheers
  Nick

binutils/ChangeLog

2006-02-19 Nick Clifton <nickc@redhat.com>

	* objcopy.c (use_alt_mach_code): Change type to unsigned long.
	(copy_object):  If bfd_alt_mach_code fails emit a more helpful
	message and if the target architecture is ELF use the alternative
	as replacement value for the e_machine number.
	(copy_main): Use strtoul to parse the number provided with the
	--alt-mach-code switch.
	* doc/binutils.texi (--alt-mach-code): Document that this switch
	can now set the absolute e_machine value.

Index: binutils/objcopy.c
===================================================================
RCS file: /cvs/src/src/binutils/objcopy.c,v
retrieving revision 1.94
diff -c -3 -p -r1.94 objcopy.c
*** binutils/objcopy.c	30 Jan 2006 13:06:53 -0000	1.94
--- binutils/objcopy.c	19 Feb 2006 10:54:11 -0000
*************** static bfd_byte gap_fill = 0;
*** 147,154 ****
  static bfd_boolean pad_to_set = FALSE;
  static bfd_vma pad_to;
  
! /* Use alternate machine code?  */
! static int use_alt_mach_code = 0;
  
  /* Output BFD flags user wants to set or clear */
  static flagword bfd_flags_to_set;
--- 147,154 ----
  static bfd_boolean pad_to_set = FALSE;
  static bfd_vma pad_to;
  
! /* Use alternative machine code?  */
! static unsigned long use_alt_mach_code = 0;
  
  /* Output BFD flags user wants to set or clear */
  static flagword bfd_flags_to_set;
*************** copy_usage (FILE *stream, int exit_statu
*** 473,479 ****
       --globalize-symbols <file>    --globalize-symbol for all 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\
       --writable-text               Mark the output text as writable\n\
       --readonly-text               Make the output text write protected\n\
       --pure                        Mark the output file as demand paged\n\
--- 473,479 ----
       --globalize-symbols <file>    --globalize-symbol for all 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 the target's <index>'th alternative machine\n\
       --writable-text               Mark the output text as writable\n\
       --readonly-text               Make the output text write protected\n\
       --pure                        Mark the output file as demand paged\n\
*************** copy_object (bfd *ibfd, bfd *obfd)
*** 1667,1675 ****
    /* 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
!       && ! bfd_alt_mach_code (obfd, use_alt_mach_code))
!     non_fatal (_("unknown alternate machine code, ignored"));
  
    return TRUE;
  }
--- 1667,1687 ----
    /* 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 (_("this target does not support %lu alternative machine codes"),
! 		     use_alt_mach_code);
! 	  if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
! 	    {
! 	      non_fatal (_("treating that number as an absolute e_machine value instead"));
! 	      elf_elfheader (obfd)->e_machine = use_alt_mach_code;
! 	    }
! 	  else
! 	    non_fatal (_("ignoring the alternative value"));
! 	}
!     }
  
    return TRUE;
  }
*************** copy_main (int argc, char *argv[])
*** 3069,3077 ****
  	  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 OPTION_PREFIX_SYMBOLS:
--- 3081,3089 ----
  	  break;
  
  	case OPTION_ALT_MACH_CODE:
! 	  use_alt_mach_code = strtoul (optarg, NULL, 0);
! 	  if (use_alt_mach_code == 0)
! 	    fatal (_("unable to parse alternative machine code"));
  	  break;
  
  	case OPTION_PREFIX_SYMBOLS:
Index: binutils/readelf.c
===================================================================
RCS file: /cvs/src/src/binutils/readelf.c,v
retrieving revision 1.338
diff -c -3 -p -r1.338 readelf.c
*** binutils/readelf.c	17 Feb 2006 14:36:21 -0000	1.338
--- binutils/readelf.c	19 Feb 2006 10:54:13 -0000
*************** process_program_headers (FILE *file)
*** 3453,3462 ****
  	  printf ("   %2.2d     ", i);
  
  	  for (j = 1; j < elf_header.e_shnum; j++, section++)
! 	    {
! 	      if (ELF_IS_SECTION_IN_SEGMENT_MEMORY(section, segment))
! 		printf ("%s ", SECTION_NAME (section));
! 	    }
  
  	  putc ('\n',stdout);
  	}
--- 3453,3460 ----
  	  printf ("   %2.2d     ", i);
  
  	  for (j = 1; j < elf_header.e_shnum; j++, section++)
! 	    if (ELF_IS_SECTION_IN_SEGMENT_MEMORY (section, segment))
! 	      printf ("%s ", SECTION_NAME (section));
  
  	  putc ('\n',stdout);
  	}
Index: binutils/doc/binutils.texi
===================================================================
RCS file: /cvs/src/src/binutils/doc/binutils.texi,v
retrieving revision 1.86
diff -c -3 -p -r1.86 binutils.texi
*** binutils/doc/binutils.texi	17 Nov 2005 01:01:05 -0000	1.86
--- binutils/doc/binutils.texi	19 Feb 2006 10:54:14 -0000
*************** If the output architecture has alternate
*** 1399,1405 ****
  @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 --writable-text
  Mark the output text as writable.  This option isn't meaningful for all
--- 1399,1407 ----
  @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.  For ELF based architectures if the @var{index}
! alternative does not exist then the value is treated as an absolute
! number to be stored in the e_machine field of the ELF header.
  
  @item --writable-text
  Mark the output text as writable.  This option isn't meaningful for all

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