1: \ Compare nonrelocatable images and produce a relocatable image
2:
3: \ Copyright (C) 1996,1997,1998 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20:
21: s" address-unit-bits" environment? drop constant bits/au
22: 6 constant dodoes-tag
23:
24: : write-cell { w^ w file-id -- ior }
25: \ write a cell to the file
26: w cell file-id write-file ;
27:
28: : th ( addr1 n -- addr2 )
29: cells + ;
30:
31: : bset ( bmask c-addr -- )
32: tuck c@ or swap c! ;
33:
34: : set-bit { u addr -- }
35: \ set bit u in bit-vector addr
36: u bits/au /mod
37: >r 1 bits/au 1- rot - lshift
38: r> addr + bset ;
39:
40: : image-data { image1 image2 i-field expected-offset -- base offset }
41: image1 i-field + @ image2 i-field + @ over - { base offset }
42: offset 0=
43: if
44: ." : images have the same base address; producing only a data-relocatable image" cr
45: else
46: offset abs expected-offset <> abort" images produced by different engines"
47: ." offset=" offset . cr
48: 0 image1 i-field + ! 0 image2 i-field + !
49: endif
50: base offset ;
51:
52:
53: : compare-images { image1 image2 reloc-bits size file-id -- }
54: \G compares image1 and image2 (of size cells) and sets reloc-bits.
55: \G offset is the difference for relocated addresses
56: \ this definition is certainly to long and too complex, but is
57: \ hard to factor.
58: image1 @ image2 @ over - { dbase doffset }
59: doffset 0= abort" images have the same dictionary base address"
60: ." data offset=" doffset . cr
61: ." code" image1 image2 cell 26 cells image-data { cbase coffset }
62: ." xt" image1 image2 11 cells 22 cells image-data { xbase xoffset }
63: size 0
64: u+do
65: image1 i th @ image2 i th @ { cell1 cell2 }
66: cell1 doffset + cell2 =
67: if
68: cell1 dbase - file-id write-cell throw
69: i reloc-bits set-bit
70: else
71: coffset 0<> cell1 coffset + cell2 = and
72: if
73: cell1 cbase - cell / { tag }
74: tag dodoes-tag =
75: if
76: \ make sure that the next cell will not be tagged
77: \ !! can probably be optimized away with hybrid threading
78: dbase negate image1 i 1+ th +!
79: dbase doffset + negate image2 i 1+ th +!
80: endif
81: -2 tag $4000 or - file-id write-cell throw
82: i reloc-bits set-bit
83: else
84: xoffset 0<> cell1 xoffset + cell2 = and
85: if
86: cell1 xbase - cell / { tag }
87: tag dodoes-tag =
88: if
89: \ make sure that the next cell will not be tagged
90: \ !! can probably be optimized away with hybrid threading
91: dbase negate image1 i 1+ th +!
92: dbase doffset + negate image2 i 1+ th +!
93: endif
94: -2 tag - file-id write-cell throw
95: i reloc-bits set-bit
96: else
97: cell1 file-id write-cell throw
98: cell1 cell2 <>
99: if
100: 0 i th 9 u.r cell1 17 u.r cell2 17 u.r cr
101: endif
102: endif
103: endif
104: endif
105: loop ;
106:
107: : comp-image ( "image-file1" "image-file2" "new-image" -- )
108: name slurp-file { image1 size1 }
109: image1 size1 s" Gforth3" search 0= abort" not a Gforth image"
110: drop 8 + image1 - { header-offset }
111: size1 aligned size1 <> abort" unaligned image size"
112: image1 header-offset + 2 cells + @ header-offset + size1 <> abort" header gives wrong size"
113: name slurp-file { image2 size2 }
114: size1 size2 <> abort" image sizes differ"
115: name ( "new-image" ) w/o bin create-file throw { outfile }
116: size1 header-offset - 1- cell / bits/au / 1+ { reloc-size }
117: reloc-size allocate throw { reloc-bits }
118: reloc-bits reloc-size erase
119: image1 header-offset outfile write-file throw
120: base @ hex
121: image1 header-offset + image2 header-offset + reloc-bits
122: size1 header-offset - aligned cell / outfile compare-images
123: base !
124: reloc-bits reloc-size outfile write-file throw
125: outfile close-file throw ;
126:
127:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>