NAME
  bin_replace_string - Replace C strings in binaries or ELFs

SYNOPSIS
  bin_replace_string [-hv] [-o <output file>] [--] <string> <replacement> <input file>

DESCRIPTION
  bin_replace_string is meant for performing very simple manipulation
  on C-style strings in already-compiled files. It can search a file
  for a given null-terminated string and replace that with another
  string of the same or lesser length.

  This is utility is intended to be useful when the sourcecode for the
  given input file is not available. For example, a vendor may ship a
  binary with a hardcoded filesystem path which points to the wrong
  place. If you're fortunate and the correct path is shorter than the
  incorrect one, you can use bin_replace_string to substitute the
  correct path for the incorrect one.

  bin_replace_string will use libelf(3elf) to parse ELFs. The purpose
  of interpreting ELF files at this level is to limit operation of
  bin_replace_string to an ELF's .rodata section. It is safer to
  modify .rodata because this is where all static constant strings are
  stored. Thus, if the string which must be replaced is short enough
  that it matches a sensitive part of the ELF (such as an arbitrary
  chunk of executable code), then that sensitive portion of the ELF
  will not be touched and thus prevent one of many strange potential
  problems.

  bin_replace_string assumes that the input file is an ELF unless if
  the '-r' option is given. Thus, if there is a need to edit a portion
  of a given ELF file outside of the .rodata section, specify '-r'. Do
  the same if working with non-ELF-formatted files.

  Note that replacing strings in this manner is not guaranteed to
  work. One probable problem brought up by Flameeyes is found in
  compilers optimizing static constant strings through aliasing. For
  example, if a program has the declaration of const char *a =
  "abcdefg"; and also has const char *b = "defg";, the compiler may
  replace this with:

    const char *a = "abcdefg";
    const char *b = a + 3;

  In this case, asking bin_replace_string to replace "bcdefg" with
  "ba" will cause a to point to "aba" and b to point to an empty
  string (""). This is because bin_replace_string currently pads the
  replacement string with zeroes until it is the same length as the
  string-to-be-replaced. As this example also points out, there is no
  method to anchor the string being replaced at the beginning of a
  string (and thus if the string being replace matches the tail ends
  of other strings, those tails will be replaced with the replacement
  string as well). This may be both useful and detrimental depending
  on the purposes of anyone using bin_replace_string.

OPTIONS
 -h	Displays the list of options with short descriptions.
 -v	Displays the version string and copyright line for
	bin_replace_string.

 -r	Do not treat the file as an ELF. Instead, scan the entire file for
	instances of string and replace them all with
	replacement. Useful if an ELF file needs to be changed outside
	of its .rodata section or if working with arbitrary binary
	files which have C-formatted strings (I suppose a dump format
	or data file might contain such a string).
 -o <output file>	Write output to this file instead of back to
	the input file.

EXAMPLE
  Fix a path to a dlopen(3)ed file so that the system linker loader
  path is used instead of the original developer's given absolute
  pathname so that a proper multilib setup can be preserved.

    $ bin_replace_string /usr/lib/cups/filter/libscmssc.so libscmssc.so rastertosamsungsplc

SEE ALSO
  sed(1)

BUGS
  Please report bugs or feature requests to
  https://ohnopub.net/bugzilla . I will be glad to receive ideas which
  make this program more useful and want to know of any problems that
  exist in it.

AUTHOR
  Nathan Phillip Brink (binki) <ohnobinki@ohnopublishing.net>
