1: \ Compare nonrelocatable images and produce a relocatable image
2:
3: \ Copyright (C) 1996 Free Software Foundation, Inc.
4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
9: \ as published by the Free Software Foundation; either version 2
10: \ of the License, or (at your option) any later version.
11:
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16:
17: \ You should have received a copy of the GNU General Public License
18: \ along with this program; if not, write to the Free Software
19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20:
21: s" address-unit-bits" environment? drop constant bits/au
22:
23: : write-cell { w^ w file-id -- ior }
24: \ write a cell to the file
25: w cell file-id write-file ;
26:
27: : th ( addr1 n -- addr2 )
28: cells + ;
29:
30: : set-bit { u addr -- }
31: \ set bit u in bit-vector addr
32: u bits/au /mod
33: >r 1 bits/au 1- rot - lshift
34: r> addr + cset ;
35:
36: : compare-images { image1 image2 reloc-bits size file-id -- }
37: \ compares image1 and image2 (of size cells) and sets reloc-bits.
38: \ offset is the difference for relocated addresses
39: image1 @ image2 @ over - { base offset }
40: offset 0= abort" images have the same base"
41: ." offset=" offset . cr
42: size 0
43: u+do
44: image1 i th @ image2 i th @ { cell1 cell2 }
45: cell1 offset + cell2 = if
46: cell1 base - file-id write-cell throw
47: i reloc-bits set-bit
48: else
49: cell1 file-id write-cell throw
50: cell1 cell2 <> if
51: 0 i th 9 u.r cell1 17 u.r cell2 17 u.r cr
52: endif
53: endif
54: loop ;
55:
56: : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
57: \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
58: r/o bin open-file throw >r
59: here $7fffffff r@ read-file throw
60: r> close-file throw
61: here swap
62: dup allot ;
63:
64: : comp-image ( "image-file1" "image-file2" "new-image" -- )
65: name { d: image-file1 }
66: name { d: image-file2 }
67: name { d: new-image }
68: maxalign image-file1 slurp-file { image1 size1 }
69: maxalign image-file2 slurp-file { image2 size2 }
70: image1 size1 s" Gforth1" search 0= abort" not a Gforth image"
71: drop 8 + image1 - { header-offset }
72: size1 size2 <> abort" image sizes differ"
73: size1 aligned size1 <> abort" unaligned image size"
74: size1 image1 header-offset + 2 cells + @ header-offset + <> abort" header gives wrong size"
75: new-image w/o bin create-file throw { outfile }
76: size1 header-offset - 1- cell / bits/au / 1+ { reloc-size }
77: maxalign here { reloc-bits }
78: reloc-size allot
79: reloc-bits reloc-size erase
80: image1 header-offset outfile write-file throw
81: base @ hex
82: image1 header-offset + image2 header-offset + reloc-bits
83: size1 header-offset - aligned cell / outfile compare-images
84: base !
85: reloc-bits reloc-size outfile write-file throw
86: outfile close-file throw ;
87:
88:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>