Annotation of gforth/comp-i.fs, revision 1.25
1.1 anton 1: \ Compare nonrelocatable images and produce a relocatable image
2:
1.23 anton 3: \ Copyright (C) 1996,1997,1998,2002,2003,2004,2007,2010 Free Software Foundation, Inc.
1.1 anton 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
1.19 anton 9: \ as published by the Free Software Foundation, either version 3
1.1 anton 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
1.19 anton 18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1 anton 19:
20: s" address-unit-bits" environment? drop constant bits/au
1.17 pazsan 21: 7 constant dodoes-tag
1.1 anton 22:
23: : write-cell { w^ w file-id -- ior }
24: \ write a cell to the file
25: w cell file-id write-file ;
26:
1.7 anton 27: : bset ( bmask c-addr -- )
28: tuck c@ or swap c! ;
29:
1.1 anton 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
1.7 anton 34: r> addr + bset ;
1.1 anton 35:
1.9 anton 36: : image-data { image1 image2 i-field expected-offset -- base offset }
37: image1 i-field + @ image2 i-field + @ over - { base offset }
38: offset 0=
39: if
40: ." : images have the same base address; producing only a data-relocatable image" cr
41: else
1.15 anton 42: \ the following sanity check produces false positices with exec-shield
43: \ offset abs expected-offset <> abort" images produced by different engines"
1.9 anton 44: ." offset=" offset . cr
45: 0 image1 i-field + ! 0 image2 i-field + !
46: endif
47: base offset ;
1.11 pazsan 48:
49: : >tag ( index -- tag )
1.13 pazsan 50: dup dodoes-tag 2 + > IF
1.11 pazsan 51: $21 1 DO dup tag-offsets I cells + @ < IF
1.13 pazsan 52: tag-offsets I 1- cells + @ - I 1- 9 lshift + negate
1.11 pazsan 53: UNLOOP EXIT THEN LOOP
54: THEN -2 swap - ;
1.9 anton 55:
1.1 anton 56: : compare-images { image1 image2 reloc-bits size file-id -- }
57: \G compares image1 and image2 (of size cells) and sets reloc-bits.
58: \G offset is the difference for relocated addresses
59: \ this definition is certainly to long and too complex, but is
60: \ hard to factor.
61: image1 @ image2 @ over - { dbase doffset }
62: doffset 0= abort" images have the same dictionary base address"
63: ." data offset=" doffset . cr
1.9 anton 64: ." code" image1 image2 cell 26 cells image-data { cbase coffset }
1.25 ! pazsan 65: ." xt" image1 image2 13 cells 22 cells image-data { xbase xoffset }
1.1 anton 66: size 0
67: u+do
68: image1 i th @ image2 i th @ { cell1 cell2 }
69: cell1 doffset + cell2 =
70: if
71: cell1 dbase - file-id write-cell throw
72: i reloc-bits set-bit
73: else
74: coffset 0<> cell1 coffset + cell2 = and
75: if
76: cell1 cbase - cell / { tag }
1.11 pazsan 77: tag >tag $4000 xor file-id write-cell throw
1.1 anton 78: i reloc-bits set-bit
79: else
1.9 anton 80: xoffset 0<> cell1 xoffset + cell2 = and
1.1 anton 81: if
1.9 anton 82: cell1 xbase - cell / { tag }
1.11 pazsan 83: tag >tag file-id write-cell throw
1.9 anton 84: i reloc-bits set-bit
85: else
86: cell1 file-id write-cell throw
87: cell1 cell2 <>
88: if
89: 0 i th 9 u.r cell1 17 u.r cell2 17 u.r cr
90: endif
1.1 anton 91: endif
92: endif
93: endif
94: loop ;
95:
96: : comp-image ( "image-file1" "image-file2" "new-image" -- )
97: name slurp-file { image1 size1 }
1.21 anton 98: image1 size1 s" Gforth4" search 0= abort" not a Gforth image"
1.1 anton 99: drop 8 + image1 - { header-offset }
100: size1 aligned size1 <> abort" unaligned image size"
1.8 anton 101: image1 header-offset + 2 cells + @ header-offset + size1 <> abort" header gives wrong size"
1.1 anton 102: name slurp-file { image2 size2 }
103: size1 size2 <> abort" image sizes differ"
104: name ( "new-image" ) w/o bin create-file throw { outfile }
105: size1 header-offset - 1- cell / bits/au / 1+ { reloc-size }
106: reloc-size allocate throw { reloc-bits }
107: reloc-bits reloc-size erase
108: image1 header-offset outfile write-file throw
109: base @ hex
110: image1 header-offset + image2 header-offset + reloc-bits
111: size1 header-offset - aligned cell / outfile compare-images
112: base !
113: reloc-bits reloc-size outfile write-file throw
114: outfile close-file throw ;
115:
116:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>