source: rtems/tools/build/binpatch.c @ 29e68b75

4.104.114.84.9
Last change on this file since 29e68b75 was 29e68b75, checked in by Joel Sherrill <joel.sherrill@…>, on Jul 26, 1999 at 9:26:44 PM

Patch from Ralf Corsepius <corsepiu@…>:

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

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 *  $Id$
3 */
4
5
6#include <stdio.h>
7#include <stdlib.h>
8
9/*
10 * This function will patch binary file
11 */
12
13
14static char buf[512];
15
16static void 
17usage(void)
18{
19  printf("usage: binpatch [-h] <ofile> <ifile> <reloc> <off> <byte0> "
20         "[<byte1> [<byte2> [<byte3>]]]\n");
21  printf("this function patches binary file at specified offset with\n");
22  printf("up to 4 bytes provided on command line \n");
23  printf("-h       - prints this message\n\n");
24  printf("<ofile>  - output file\n");
25  printf("<ifile>  - input ifile\n");
26  printf("<reloc>  - relocation address of image\n");
27  printf("<off>    - offset of patch, offset in file is at off - reloc\n");
28  printf("<byte0>  - byte 0 of patch\n");
29  printf("<byte1>  - byte 1 of patch\n");
30  printf("<byte2>  - byte 1 of patch\n");
31  printf("<byte3>  - byte 1 of patch\n");
32
33  return;
34}
35
36int
37main(int argc, char **argv)
38{
39  int   c;
40  FILE  *ofp, *ifp;
41  char  patch[4], *end;
42  int   patchLen, tmp, i, off, cnt, patched, len, reloc;
43 
44 
45  /* parse command line options */
46  while ((c = getopt(argc, argv, "h")) >= 0)
47    {
48      switch (c)
49        {
50        case 'h':
51          usage();
52          return 0;
53        default:
54          usage();
55          return 1;
56        }
57    }
58
59  if(argc < 6)
60    {
61      usage();
62      return 1;
63    }
64
65  /* Let us get offset in file */
66  reloc = strtol(argv[3], &end, 0);
67  if(end == argv[3] || off < 0)
68    {
69      fprintf(stderr, "bad reloc value %s\n", argv[3]);
70      return 1;
71    }
72
73  off = strtol(argv[4], &end, 0);
74  if(end == argv[4] || off < 0 || off < reloc)
75    {
76      fprintf(stderr, "bad offset value %s\n", argv[4]);
77      return 1;
78    }
79
80  off -= reloc;
81
82  /* Let us get patch  */
83  patchLen = argc - 5;
84
85  for(i=0; i<patchLen; i++)
86    {
87      tmp = strtol(argv[5+i], &end, 0);
88
89      if(end == argv[4+i] || tmp < 0 || tmp > 0xff)
90        {
91          fprintf(stderr, "bad byte value %s\n", argv[5+i]);
92          return 1;
93        }
94      patch[i] = tmp;
95    }
96     
97  ifp = fopen(argv[2], "r");
98  if(ifp == NULL)
99    {
100      fprintf(stderr, "unable to open file %s\n", argv[2]);
101      return 1;
102    }
103
104  ofp = fopen(argv[1], "w");
105  if(ofp == NULL)
106    {
107      fprintf(stderr, "unable to open file %s\n", argv[1]);
108      return 1;
109    }
110
111  cnt     = 0;
112  patched = 0;
113  for(;;)
114    {
115      len = fread(buf, 1, sizeof(buf), ifp);
116
117      if(len == 0)
118        {
119          break;
120        }
121
122      if(cnt <= off && (cnt + len) > off)
123        {
124          /* Perform patch */
125          for(i=0; i<patchLen && (off+i)<(cnt+len); i++)
126            {
127              buf[off-cnt+i] = patch[i];
128            }
129          patched = 1;
130        }
131      else if(cnt > off && cnt < (off + patchLen))
132        {
133          /* Perform patch */
134          for(i=cnt-off; i<patchLen; i++)
135            {
136              buf[off-cnt+i] = patch[i];
137            }
138          patched = 1;
139        }
140         
141      fwrite(buf, 1, len, ofp);
142
143      cnt += len;
144    }
145
146  fclose(ifp);
147  fclose(ofp);
148 
149  if(!patched)
150    {
151      fprintf(stderr, "warning: offset is beyond input file length\n");
152      fprintf(stderr, "         no patch is performed\n");
153    }
154
155  return 0;
156}
Note: See TracBrowser for help on using the repository browser.