File: patchup.c

package info (click to toggle)
icmake 6.30-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,360 kB
  • ctags: 1,415
  • sloc: ansic: 7,727; makefile: 1,465; sh: 244; asm: 126; cpp: 39
file content (44 lines) | stat: -rw-r--r-- 1,559 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
                              P A T C H U P . C
*/

#include "iccomp.h"

void patchup(INT8 *code, unsigned len, unsigned *list, unsigned listlen,
             int pos)
{                                           /* list, listlen: list of */
    register unsigned                       /* offsets to patchup     */
        index,
        beyond_jump;
    char
        *cp,                                /* codepointer */
        jumpsize[2];

    if (!listlen)                           /* done if nothing to patchup */
        return;

    if (pos)
        pos = len;
                                            /* walk all elements to patchup */
    for (index = 0; index < listlen; index++)
    {
        beyond_jump = list[index];
            /*
                beyond-jump is the offset immediately beyond the jump to
                the end of the code. This is the position where the jump
                starts after having read the jump instruction. Hence, the
                jumpsize is determined by the distance 'pos - beyond-jump',
                whereas this jumpsize must be inserted 2 bytes earlier
                than beyond_jump, as part of the jump instruction.
            */

                                            /* determine the size of the jmp */
        *(INT16 *)jumpsize = pos - beyond_jump;
        cp = (char *)code + beyond_jump - 2;/* point to codebytes to patch */

        *cp = jumpsize[0];                  /* copy byte 0 */
        *(cp + 1) = jumpsize[1];            /* copy byte 1 */
    }

    free(list);
}