Home | History | Annotate | Line # | Download | only in binpatch
binpatch.c revision 1.2.8.1.2.1
      1  1.2.8.1     pk /*	$NetBSD: binpatch.c,v 1.2.8.1.2.1 1999/06/21 00:48:37 thorpej Exp $	*/
      2      1.1    leo 
      3      1.1    leo /*
      4      1.1    leo  * Copyright (c) 1994 Christian E. Hopps
      5      1.1    leo  * All rights reserved.
      6      1.1    leo  *
      7      1.1    leo  * Redistribution and use in source and binary forms, with or without
      8      1.1    leo  * modification, are permitted provided that the following conditions
      9      1.1    leo  * are met:
     10      1.1    leo  * 1. Redistributions of source code must retain the above copyright
     11      1.1    leo  *    notice, this list of conditions and the following disclaimer.
     12      1.1    leo  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1    leo  *    notice, this list of conditions and the following disclaimer in the
     14      1.1    leo  *    documentation and/or other materials provided with the distribution.
     15      1.1    leo  * 3. All advertising materials mentioning features or use of this software
     16      1.1    leo  *    must display the following acknowledgement:
     17      1.1    leo  *      This product includes software developed by Christian E. Hopps.
     18  1.2.8.1     pk  * 4. The name of the author may not be used to endorse or promote products
     19      1.1    leo  *    derived from this software without specific prior written permission
     20      1.1    leo  *
     21      1.1    leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1    leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1    leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1    leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1    leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1    leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1    leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1    leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1    leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1    leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1    leo  */
     32      1.1    leo 
     33      1.1    leo #include <sys/types.h>
     34      1.1    leo #include <a.out.h>
     35      1.1    leo #include <stdio.h>
     36      1.1    leo 
     37      1.1    leo extern char *optarg;
     38      1.1    leo extern int optind;
     39      1.1    leo 
     40      1.1    leo volatile void error ();
     41      1.1    leo 
     42      1.1    leo int test = 1;
     43      1.1    leo int testbss;
     44      1.1    leo char foo = 23;
     45      1.1    leo 
     46      1.1    leo 
     47      1.1    leo int
     48      1.1    leo main(argc, argv)
     49      1.1    leo      int argc;
     50      1.1    leo      char *argv[];
     51      1.1    leo {
     52      1.1    leo   struct exec e;
     53      1.1    leo   int c;
     54      1.1    leo   u_long addr = 0, offset = 0;
     55      1.1    leo   u_long replace = 0, do_replace = 0;
     56      1.1    leo   char *symbol = 0;
     57      1.1    leo   char size = 4;  /* default to long */
     58      1.1    leo   char *fname;
     59      1.1    leo   int fd;
     60      1.1    leo   int type, off;
     61      1.1    leo   u_long  lval;
     62      1.1    leo   u_short sval;
     63      1.1    leo   u_char  cval;
     64      1.1    leo 
     65      1.1    leo 
     66      1.2  lukem   while ((c = getopt (argc, argv, "a:bwlr:s:o:")) != -1)
     67      1.1    leo     switch (c)
     68      1.1    leo       {
     69      1.1    leo       case 'a':
     70      1.1    leo 	if (addr || symbol)
     71      1.1    leo 	  error ("only one address/symbol allowed");
     72      1.1    leo 	if (! strncmp (optarg, "0x", 2))
     73      1.1    leo 	  sscanf (optarg, "%x", &addr);
     74      1.1    leo 	else
     75      1.1    leo 	  addr = atoi (optarg);
     76      1.1    leo 	if (! addr)
     77      1.1    leo 	  error ("invalid address");
     78      1.1    leo 	break;
     79      1.1    leo 
     80      1.1    leo       case 'b':
     81      1.1    leo 	size = 1;
     82      1.1    leo 	break;
     83      1.1    leo 
     84      1.1    leo       case 'w':
     85      1.1    leo 	size = 2;
     86      1.1    leo 	break;
     87      1.1    leo 
     88      1.1    leo       case 'l':
     89      1.1    leo 	size = 4;
     90      1.1    leo 	break;
     91      1.1    leo 
     92      1.1    leo       case 'r':
     93      1.1    leo 	do_replace = 1;
     94      1.1    leo 	if (! strncmp (optarg, "0x", 2))
     95      1.1    leo 	  sscanf (optarg, "%x", &replace);
     96      1.1    leo 	else
     97      1.1    leo 	  replace = atoi (optarg);
     98      1.1    leo 	break;
     99      1.1    leo 
    100      1.1    leo       case 's':
    101      1.1    leo 	if (addr || symbol)
    102      1.1    leo 	  error ("only one address/symbol allowed");
    103      1.1    leo 	symbol = optarg;
    104      1.1    leo 	break;
    105      1.1    leo 
    106      1.1    leo       case 'o':
    107      1.1    leo 	if (offset)
    108      1.1    leo 	  error ("only one offset allowed");
    109      1.1    leo 	if (! strncmp (optarg, "0x", 2))
    110      1.1    leo 	  sscanf (optarg, "%x", &offset);
    111      1.1    leo 	else
    112      1.1    leo           offset = atoi (optarg);
    113      1.1    leo         break;
    114      1.1    leo       }
    115      1.1    leo 
    116      1.1    leo   argv += optind;
    117      1.1    leo   argc -= optind;
    118      1.1    leo 
    119      1.1    leo 
    120      1.1    leo   if (argc < 1)
    121      1.1    leo     error ("No file to patch.");
    122      1.1    leo 
    123      1.1    leo   fname = argv[0];
    124      1.1    leo   if ((fd = open (fname, 0)) < 0)
    125      1.1    leo     error ("Can't open file");
    126      1.1    leo 
    127      1.1    leo   if (read (fd, &e, sizeof (e)) != sizeof (e)
    128      1.1    leo       || N_BADMAG (e))
    129      1.1    leo     error ("Not a valid executable.");
    130      1.1    leo 
    131      1.1    leo   /* fake mid, so the N_ macros work on the amiga.. */
    132      1.1    leo   e.a_midmag |= 127 << 16;
    133      1.1    leo 
    134      1.1    leo   if (symbol)
    135      1.1    leo     {
    136      1.1    leo       struct nlist nl[2];
    137      1.1    leo       nl[0].n_un.n_name = symbol;
    138      1.1    leo       nl[1].n_un.n_name = 0;
    139      1.1    leo       if (nlist (fname, nl) != 0)
    140      1.1    leo 	error ("Symbol not found.");
    141      1.1    leo       addr = nl[0].n_value;
    142      1.1    leo       type = nl[0].n_type & N_TYPE;
    143      1.1    leo     }
    144      1.1    leo   else
    145      1.1    leo     {
    146      1.1    leo       type = N_UNDF;
    147      1.1    leo       if (addr >= N_TXTADDR(e) && addr < N_DATADDR(e))
    148      1.1    leo 	type = N_TEXT;
    149      1.1    leo       else if (addr >= N_DATADDR(e) && addr < N_DATADDR(e) + e.a_data)
    150      1.1    leo 	type = N_DATA;
    151      1.1    leo     }
    152      1.1    leo   addr += offset;
    153      1.1    leo 
    154      1.1    leo   /* if replace-mode, have to reopen the file for writing.
    155      1.1    leo      Can't do that from the beginning, or nlist() will not
    156      1.1    leo      work (at least not under AmigaDOS) */
    157      1.1    leo   if (do_replace)
    158      1.1    leo     {
    159      1.1    leo       close (fd);
    160      1.1    leo       if ((fd = open (fname, 2)) == -1)
    161      1.1    leo 	error ("Can't reopen file for writing.");
    162      1.1    leo     }
    163      1.1    leo 
    164      1.1    leo   if (type != N_TEXT && type != N_DATA)
    165      1.1    leo     error ("address/symbol is not in text or data section.");
    166      1.1    leo 
    167      1.1    leo   if (type == N_TEXT)
    168      1.1    leo     off = addr - N_TXTADDR(e) + N_TXTOFF(e);
    169      1.1    leo   else
    170      1.1    leo     off = addr - N_DATADDR(e) + N_DATOFF(e);
    171      1.1    leo 
    172      1.1    leo   if (lseek (fd, off, 0) == -1)
    173      1.1    leo     error ("lseek");
    174      1.1    leo 
    175      1.1    leo   /* not beautiful, but works on big and little endian machines */
    176      1.1    leo   switch (size)
    177      1.1    leo     {
    178      1.1    leo     case 1:
    179      1.1    leo       if (read (fd, &cval, 1) != 1)
    180      1.1    leo 	error ("cread");
    181      1.1    leo       lval = cval;
    182      1.1    leo       break;
    183      1.1    leo 
    184      1.1    leo     case 2:
    185      1.1    leo       if (read (fd, &sval, 2) != 2)
    186      1.1    leo 	error ("sread");
    187      1.1    leo       lval = sval;
    188      1.1    leo       break;
    189      1.1    leo 
    190      1.1    leo     case 4:
    191      1.1    leo       if (read (fd, &lval, 4) != 4)
    192      1.1    leo 	error ("lread");
    193      1.1    leo       break;
    194      1.1    leo     }
    195      1.1    leo 
    196      1.1    leo 
    197      1.1    leo   if (symbol)
    198      1.1    leo     printf ("%s(0x%x): %d (0x%x)\n", symbol, addr, lval, lval);
    199      1.1    leo   else
    200      1.1    leo     printf ("0x%x: %d (0x%x)\n", addr, lval, lval);
    201      1.1    leo 
    202      1.1    leo   if (do_replace)
    203      1.1    leo     {
    204      1.1    leo       if (lseek (fd, off, 0) == -1)
    205      1.1    leo 	error ("write-lseek");
    206      1.1    leo       switch (size)
    207      1.1    leo 	{
    208      1.1    leo 	case 1:
    209      1.1    leo 	  cval = replace;
    210      1.1    leo 	  if (cval != replace)
    211      1.1    leo 	    error ("byte-value overflow.");
    212      1.1    leo 	  if (write (fd, &cval, 1) != 1)
    213      1.1    leo 	    error ("cwrite");
    214      1.1    leo 	  break;
    215      1.1    leo 
    216      1.1    leo 	case 2:
    217      1.1    leo 	  sval = replace;
    218      1.1    leo 	  if (sval != replace)
    219      1.1    leo 	    error ("word-value overflow.");
    220      1.1    leo 	  if (write (fd, &sval, 2) != 2)
    221      1.1    leo 	    error ("swrite");
    222      1.1    leo 	  break;
    223      1.1    leo 
    224      1.1    leo 	case 4:
    225      1.1    leo 	  if (write (fd, &replace, 4) != 4)
    226      1.1    leo 	    error ("lwrite");
    227      1.1    leo 	  break;
    228      1.1    leo 	}
    229      1.1    leo     }
    230      1.1    leo 
    231      1.1    leo   close (fd);
    232      1.1    leo }
    233      1.1    leo 
    234      1.1    leo 
    235      1.1    leo 
    236      1.1    leo volatile void error (str)
    237      1.1    leo      char *str;
    238      1.1    leo {
    239      1.1    leo   fprintf (stderr, "%s\n", str);
    240      1.1    leo   exit (1);
    241      1.1    leo }
    242