Files
rtems/tools/build/binpatch.c
Joel Sherrill 29e68b7584 Patch from Ralf Corsepius <corsepiu@faw.uni-ulm.de>:
This patch is an addition to "The big-patch"

  CHANGES:
  * FIX: c/Makefile.am: bogus comment which changed the behavior of
    c/Makefile.am removed
  * FIX: make/custom/ts_i386ex.cfg did not set HAS_NETWORKING correctly
    (Me thinks it might have been me who added this bogus setting :-).

  * NEW: removing make targets get, protos, debug_install, profile_install

  * NEW: replacing clobber with distclean
  * NEW: Reimplement distclean and clean as reverse depth first make
    targets (adaptation to automake's behavior)
  * NEW: removing RCS_CLEAN from make distclean (tools/build/rcs_clean is
    still in - remove it?)
  * NEW: "$(RM) Makefile" added to make distclean (adaptation to
    automake's behavior)
  * NEW: "$(RM) config.cache config.log" to CLOBBER_ADDITIONS in
    [lib|exec|tests]/Makefile.in (adaptation to automake's behavior)
  * NEW: "$(CLEAN_PROTOS)" removed (Not used anywhere)
  * NEW: binpatch.c moved from i386 bsp tools to tools/build (AFAIS,
    binpatch is not specific to the pc386 BSP at all)
  * NEW: AC_EXEEXT added to all configure scripts which contain AC_PROG_CC
    (Cygwin support)

  * NEW/Experimental: An experimental implementation of temporary
    installation tree support in libbsp/i386/pc386/tools/Makefile.am, based
    on dependency tracking with make, instead of applying INSTALL_CHANGE.


  REMARK:
  * This patch is small in size, but changes the behavior of "make
    clean|distclean|clobber" basically.
  * This patch does not alter building/compiling RTEMS, ie. there should
    be no need to rerun all "make all" building tests.

  KNOWN BUGS:
  * make RTEMS_BSP="..." distclean in c/ runs "make distclean" in BSPs
    subdirectories passed through RTEMS_BSP and in "c/." only, but does not
    descend into other BSP subdirectories previously configured with
    different settings of make RTEMS_BSP="...".
    => Workaround: always use the same setting of RTEMS_BSP when working
    inside the build-tree.

  * "make [distclean|clean]" do not clean subdirectories, which have been
    configured at configuration time, but  which are not used due to
    make-time configuration (e.g. macros/networking/rdgb subdirectories).
    This will problem will vanish by itself when migrating from make-time to
    configuration-time configuration

  APPLYING THE PATCH

      mv c/src/lib/libbsp/i386/pc386/tools/binpatch.c tools/build
      patch -p1 < rtems-rc-19990709-2.diff
      autogen
1999-07-26 21:26:44 +00:00

157 lines
2.9 KiB
C

/*
* $Id$
*/
#include <stdio.h>
#include <stdlib.h>
/*
* This function will patch binary file
*/
static char buf[512];
static void
usage(void)
{
printf("usage: binpatch [-h] <ofile> <ifile> <reloc> <off> <byte0> "
"[<byte1> [<byte2> [<byte3>]]]\n");
printf("this function patches binary file at specified offset with\n");
printf("up to 4 bytes provided on command line \n");
printf("-h - prints this message\n\n");
printf("<ofile> - output file\n");
printf("<ifile> - input ifile\n");
printf("<reloc> - relocation address of image\n");
printf("<off> - offset of patch, offset in file is at off - reloc\n");
printf("<byte0> - byte 0 of patch\n");
printf("<byte1> - byte 1 of patch\n");
printf("<byte2> - byte 1 of patch\n");
printf("<byte3> - byte 1 of patch\n");
return;
}
int
main(int argc, char **argv)
{
int c;
FILE *ofp, *ifp;
char patch[4], *end;
int patchLen, tmp, i, off, cnt, patched, len, reloc;
/* parse command line options */
while ((c = getopt(argc, argv, "h")) >= 0)
{
switch (c)
{
case 'h':
usage();
return 0;
default:
usage();
return 1;
}
}
if(argc < 6)
{
usage();
return 1;
}
/* Let us get offset in file */
reloc = strtol(argv[3], &end, 0);
if(end == argv[3] || off < 0)
{
fprintf(stderr, "bad reloc value %s\n", argv[3]);
return 1;
}
off = strtol(argv[4], &end, 0);
if(end == argv[4] || off < 0 || off < reloc)
{
fprintf(stderr, "bad offset value %s\n", argv[4]);
return 1;
}
off -= reloc;
/* Let us get patch */
patchLen = argc - 5;
for(i=0; i<patchLen; i++)
{
tmp = strtol(argv[5+i], &end, 0);
if(end == argv[4+i] || tmp < 0 || tmp > 0xff)
{
fprintf(stderr, "bad byte value %s\n", argv[5+i]);
return 1;
}
patch[i] = tmp;
}
ifp = fopen(argv[2], "r");
if(ifp == NULL)
{
fprintf(stderr, "unable to open file %s\n", argv[2]);
return 1;
}
ofp = fopen(argv[1], "w");
if(ofp == NULL)
{
fprintf(stderr, "unable to open file %s\n", argv[1]);
return 1;
}
cnt = 0;
patched = 0;
for(;;)
{
len = fread(buf, 1, sizeof(buf), ifp);
if(len == 0)
{
break;
}
if(cnt <= off && (cnt + len) > off)
{
/* Perform patch */
for(i=0; i<patchLen && (off+i)<(cnt+len); i++)
{
buf[off-cnt+i] = patch[i];
}
patched = 1;
}
else if(cnt > off && cnt < (off + patchLen))
{
/* Perform patch */
for(i=cnt-off; i<patchLen; i++)
{
buf[off-cnt+i] = patch[i];
}
patched = 1;
}
fwrite(buf, 1, len, ofp);
cnt += len;
}
fclose(ifp);
fclose(ofp);
if(!patched)
{
fprintf(stderr, "warning: offset is beyond input file length\n");
fprintf(stderr, " no patch is performed\n");
}
return 0;
}