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]
Other format: [Raw text]

[patch] Support for tweaking BFD flags with objcopy


Hello,

 There are two flags in BFD that for certain targets are derived from
linker options or format-specific file header flags and for others are
guessed, possibly wrong, with a simple heuristics.  They are D_PAGED and
WP_TEXT.  If guessed incorrectly, they cannot be adjusted for formats that
store that information in file header flags.  This may lead to problems 
with executing resulting binaries.

 Here's a patch originally written by Ralf Baechle, with a few cosmetic 
clean-ups done by me.  It adds a few options for objcopy to tweak the 
flags while converting a binary.

2003-12-23  Ralf Baechle  <ralf@gnu.org>
	    Maciej W. Rozycki  <macro@ds2.pg.gda.pl>

	* objcopy.c (bfd_flags_to_set, bfd_flags_to_clear): New variables 
	to handle flags for tweaking.
	(copy_options): New options: impure, pure, readonly-text, 
	writable-text.
	(copy_usage, copy_object, command_line_switch): Handle the new 
	options.
	* doc/binutils.texi: Document the new options.

 The patch is useful e.g. for converting MIPS binaries from ELF to ECOFF,
when the resulting image is fed to firmware which expects a specific image
setup.

 What do you think?

  Maciej

-- 
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: macro@ds2.pg.gda.pl, PGP key available        +

binutils-2.14.90-20031222-ralf-objcopy.patch
diff -up --recursive --new-file binutils-2.14.90-20031222.macro/binutils/doc/binutils.texi binutils-2.14.90-20031222/binutils/doc/binutils.texi
--- binutils-2.14.90-20031222.macro/binutils/doc/binutils.texi	2003-11-27 04:25:18.000000000 +0000
+++ binutils-2.14.90-20031222/binutils/doc/binutils.texi	2003-12-23 09:06:44.000000000 +0000
@@ -976,6 +976,10 @@ objcopy [@option{-F} @var{bfdname}|@opti
         [@option{--prefix-alloc-sections=}@var{string}]
         [@option{--add-gnu-debuglink=}@var{path-to-file}]
         [@option{--only-keep-debug}]
+        [@option{--writable-text}]
+        [@option{--readonly-text}]
+        [@option{--pure}]
+        [@option{--impure}]
         [@option{-v}|@option{--verbose}]
         [@option{-V}|@option{--version}]  
         [@option{--help}] [@option{--info}]
@@ -1357,6 +1361,22 @@ a machine is assigned an official code a
 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
+object file formats.
+
+@item --readonly-text
+Make the output text write protected.  This option isn't meaningful for all
+object file formats.
+
+@item --pure
+Mark the output file as demand paged.  This option isn't meaningful for all
+object file formats.
+
+@item --impure
+Mark the output file as impure.  This option isn't meaningful for all
+object file formats.
+
 @item --prefix-symbols=@var{string}
 Prefix all symbols in the output file with @var{string}.
 
diff -up --recursive --new-file binutils-2.14.90-20031222.macro/binutils/objcopy.c binutils-2.14.90-20031222/binutils/objcopy.c
--- binutils-2.14.90-20031222.macro/binutils/objcopy.c	2003-12-12 04:25:21.000000000 +0000
+++ binutils-2.14.90-20031222/binutils/objcopy.c	2003-12-23 09:07:41.000000000 +0000
@@ -148,6 +148,10 @@ 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;
+static flagword bfd_flags_to_clear;
+
 /* List of sections to add.  */
 struct section_add
 {
@@ -237,7 +241,11 @@ enum command_line_switch
     OPTION_PREFIX_ALLOC_SECTIONS,
     OPTION_FORMATS_INFO,
     OPTION_ADD_GNU_DEBUGLINK,
-    OPTION_ONLY_KEEP_DEBUG
+    OPTION_ONLY_KEEP_DEBUG,
+    OPTION_READONLY_TEXT,
+    OPTION_WRITABLE_TEXT,
+    OPTION_PURE,
+    OPTION_IMPURE
   };
 
 /* Options to handle if running as "strip".  */
@@ -295,6 +303,7 @@ static struct option copy_options[] =
   {"format", required_argument, 0, 'F'}, /* Obsolete */
   {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
   {"help", no_argument, 0, 'h'},
+  {"impure", no_argument, 0, OPTION_IMPURE},
   {"info", no_argument, 0, OPTION_FORMATS_INFO},
   {"input-format", required_argument, 0, 'I'}, /* Obsolete */
   {"input-target", required_argument, 0, 'I'},
@@ -316,6 +325,8 @@ static struct option copy_options[] =
   {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
   {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
   {"preserve-dates", no_argument, 0, 'p'},
+  {"pure", no_argument, 0, OPTION_PURE},
+  {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
   {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
   {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
   {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
@@ -337,6 +348,7 @@ static struct option copy_options[] =
   {"weaken-symbol", required_argument, 0, 'W'},
   {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
   {"wildcard", no_argument, 0, 'w'},
+  {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
   {0, no_argument, 0, 0}
 };
 
@@ -434,6 +446,10 @@ copy_usage (FILE *stream, int exit_statu
      --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\
+     --impure                      Mark the output file as impure\n\
      --prefix-symbols <prefix>     Add <prefix> to start of every symbol name\n\
      --prefix-sections <prefix>    Add <prefix> to start of every section name\n\
      --prefix-alloc-sections <prefix>\n\
@@ -1127,10 +1143,15 @@ copy_object (bfd *ibfd, bfd *obfd)
      need to be set for a core file.  */
   if (bfd_get_format (obfd) != bfd_core)
     {
+      flagword flags;
+
+      flags = bfd_get_file_flags (ibfd);
+      flags |= bfd_flags_to_set;
+      flags &= ~bfd_flags_to_clear;
+      flags &= bfd_applicable_file_flags (obfd);
+
       if (!bfd_set_start_address (obfd, start)
-	  || !bfd_set_file_flags (obfd,
-				  (bfd_get_file_flags (ibfd)
-				   & bfd_applicable_file_flags (obfd))))
+	  || !bfd_set_file_flags (obfd, flags))
 	RETURN_NONFATAL (bfd_get_filename (ibfd));
     }
 
@@ -2784,6 +2805,26 @@ copy_main (int argc, char *argv[])
 	  prefix_alloc_sections_string = optarg;
 	  break;
 
+	case OPTION_READONLY_TEXT:
+	  bfd_flags_to_set |= WP_TEXT;
+	  bfd_flags_to_clear &= ~WP_TEXT;
+	  break;
+
+	case OPTION_WRITABLE_TEXT:
+	  bfd_flags_to_clear |= WP_TEXT;
+	  bfd_flags_to_set &= ~WP_TEXT;
+	  break;
+
+	case OPTION_PURE:
+	  bfd_flags_to_set |= D_PAGED;
+	  bfd_flags_to_clear &= ~D_PAGED;
+	  break;
+
+	case OPTION_IMPURE:
+	  bfd_flags_to_clear |= D_PAGED;
+	  bfd_flags_to_set &= ~D_PAGED;
+	  break;
+
 	case 0:
 	  /* We've been given a long option.  */
 	  break;


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