Subversion Repositories Kolibri OS

Rev

Rev 7983 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7983 Rev 8097
Line 1... Line 1...
1
(*
1
(*
2
    BSD 2-Clause License
2
    BSD 2-Clause License
Line 3... Line 3...
3
 
3
 
4
    Copyright (c) 2018-2019, Anton Krotov
4
    Copyright (c) 2018-2020, Anton Krotov
5
    All rights reserved.
5
    All rights reserved.
Line 6... Line 6...
6
*)
6
*)
Line 17... Line 17...
17
    SIZE_OF_DWORD = 4;
17
    SIZE_OF_DWORD = 4;
Line 18... Line 18...
18
 
18
 
Line 19... Line -...
19
 
-
 
20
TYPE
-
 
21
 
19
 
Line 22... Line 20...
22
    FILE = WR.FILE;
20
TYPE
23
 
21
 
Line 24... Line 22...
24
    HEADER = RECORD
22
    HEADER = RECORD
Line 25... Line -...
25
 
-
 
26
        menuet01: ARRAY 9 OF CHAR;
-
 
27
        ver, start, size, mem, sp, param, path: INTEGER
-
 
28
 
-
 
29
    END;
-
 
30
 
-
 
31
 
-
 
32
PROCEDURE align (n, _align: INTEGER): INTEGER;
-
 
33
BEGIN
-
 
34
    IF n MOD _align # 0 THEN
-
 
35
        n := n + _align - (n MOD _align)
23
 
36
    END
24
        menuet01: ARRAY 9 OF CHAR;
37
 
25
        ver, start, size, mem, sp, param, path: INTEGER
38
    RETURN n
26
 
Line 39... Line 27...
39
END align;
27
    END;
40
 
28
 
41
 
29
 
42
PROCEDURE Import* (program: BIN.PROGRAM; idata: INTEGER; VAR ImportTable: CHL.INTLIST; VAR len, libcount, size: INTEGER);
30
PROCEDURE Import* (program: BIN.PROGRAM; idata: INTEGER; VAR ImportTable: CHL.INTLIST; VAR len, libcount, size: INTEGER);
43
VAR
31
VAR
44
    i:      INTEGER;
32
    i:   INTEGER;
45
    import: BIN.IMPRT;
33
    imp: BIN.IMPRT;
46
 
34
 
47
BEGIN
35
BEGIN
Line 48... Line 36...
48
    libcount  := 0;
36
    libcount  := 0;
49
    import := program.imp_list.first(BIN.IMPRT);
37
    imp := program.imp_list.first(BIN.IMPRT);
Line 61... Line 49...
61
    FOR i := 0 TO size DIV SIZE_OF_DWORD - 1 DO
49
    FOR i := 0 TO size DIV SIZE_OF_DWORD - 1 DO
62
        CHL.PushInt(ImportTable, 0)
50
        CHL.PushInt(ImportTable, 0)
63
    END;
51
    END;
Line 64... Line 52...
64
 
52
 
65
    i := 0;
53
    i := 0;
66
    import := program.imp_list.first(BIN.IMPRT);
54
    imp := program.imp_list.first(BIN.IMPRT);
Line 67... Line 55...
67
    WHILE import # NIL DO
55
    WHILE imp # NIL DO
68
 
56
 
69
        IF import.label = 0 THEN
57
        IF imp.label = 0 THEN
70
            CHL.SetInt(ImportTable, len, 0);
58
            CHL.SetInt(ImportTable, len, 0);
71
            INC(len);
59
            INC(len);
72
            CHL.SetInt(ImportTable, i, idata + len * SIZE_OF_DWORD);
60
            CHL.SetInt(ImportTable, i, idata + len * SIZE_OF_DWORD);
73
            INC(i);
61
            INC(i);
74
            CHL.SetInt(ImportTable, i, import.nameoffs + size + idata);
62
            CHL.SetInt(ImportTable, i, imp.nameoffs + size + idata);
75
            INC(i)
63
            INC(i)
76
        ELSE
64
        ELSE
77
            CHL.SetInt(ImportTable, len, import.nameoffs + size + idata);
65
            CHL.SetInt(ImportTable, len, imp.nameoffs + size + idata);
78
            import.label := len * SIZE_OF_DWORD;
66
            imp.label := len * SIZE_OF_DWORD;
Line 79... Line 67...
79
            INC(len)
67
            INC(len)
80
        END;
68
        END;
81
 
69
 
82
        import := import.next(BIN.IMPRT)
70
        imp := imp.next(BIN.IMPRT)
83
    END;
71
    END;
84
    CHL.SetInt(ImportTable, len, 0);
72
    CHL.SetInt(ImportTable, len, 0);
85
    CHL.SetInt(ImportTable, i, 0);
73
    CHL.SetInt(ImportTable, i, 0);
86
    CHL.SetInt(ImportTable, i + 1, 0);
74
    CHL.SetInt(ImportTable, i + 1, 0);
Line 87... Line 75...
87
    INC(len);
75
    INC(len);
Line 98... Line 86...
98
 
86
 
99
 
87
 
Line 100... Line 88...
100
VAR
88
VAR
Line 101... Line 89...
101
    header: HEADER;
89
    header: HEADER;
102
 
90
 
103
    base, text, data, idata, bss: INTEGER;
91
    base, text, data, idata, bss, offset: INTEGER;
104
 
92
 
Line 105... Line 93...
105
    reloc:   BIN.RELOC;
93
    reloc:   BIN.RELOC;
Line 106... Line -...
106
    iproc:   BIN.IMPRT;
-
 
107
    L:       INTEGER;
-
 
108
    delta:   INTEGER;
94
    iproc:   BIN.IMPRT;
109
 
95
    L:       INTEGER;
Line 110... Line 96...
110
    i: INTEGER;
96
    delta:   INTEGER;
Line 111... Line 97...
111
 
97
 
Line 112... Line 98...
112
    File: FILE;
98
    i: INTEGER;
113
 
99
 
Line 114... Line 100...
114
    ImportTable: CHL.INTLIST;
100
    ImportTable: CHL.INTLIST;
115
    ILen, libcount, isize: INTEGER;
101
    ILen, libcount, isize: INTEGER;
116
 
102
 
Line 117... Line 103...
117
    icount, dcount, ccount: INTEGER;
103
    icount, dcount, ccount: INTEGER;
118
 
104
 
119
    code: CHL.BYTELIST;
105
    code: CHL.BYTELIST;
Line 120... Line 106...
120
 
106
 
Line 121... Line 107...
121
BEGIN
107
BEGIN
Line 122... Line 108...
122
    base := 0;
108
    base := 0;
123
 
109
 
124
    icount := CHL.Length(program.import);
110
    icount := CHL.Length(program._import);
125
    dcount := CHL.Length(program.data);
111
    dcount := CHL.Length(program.data);
126
    ccount := CHL.Length(program.code);
112
    ccount := CHL.Length(program.code);
127
 
113
 
128
    text  := base + HEADER_SIZE;
114
    text  := base + HEADER_SIZE;
129
    data  := align(text + ccount, FileAlignment);
115
    data  := WR.align(text + ccount, FileAlignment);
Line 130... Line 116...
130
    idata := align(data + dcount, FileAlignment);
116
    idata := WR.align(data + dcount, FileAlignment);
131
 
117
 
132
    Import(program, idata, ImportTable, ILen, libcount, isize);
118
    Import(program, idata, ImportTable, ILen, libcount, isize);
Line -... Line 119...
-
 
119
 
133
 
120
    bss := WR.align(idata + isize, FileAlignment);
134
    bss := align(idata + isize, FileAlignment);
121
 
Line 135... Line 122...
135
 
122
    header.menuet01 := "MENUET01";
Line 136... Line 123...
136
    header.menuet01 := "MENUET01";
123
    header.ver      :=  1;
137
    header.ver      :=  1;
124
    header.start    :=  text;
138
    header.start    :=  text;
125
    header.size     :=  idata + isize - base;
Line 139... Line 126...
139
    header.size     :=  idata + isize - base;
126
    header.mem      :=  WR.align(header.size + program.stack + program.bss + PARAM_SIZE * 2 + 4096, FileAlignment);
140
    header.mem      :=  align(header.size + program.stack + program.bss + PARAM_SIZE * 2 + 4096, FileAlignment);
127
    header.sp       :=  base + header.mem - PARAM_SIZE * 2;
Line 141... Line 128...
141
    header.sp       :=  base + header.mem - PARAM_SIZE * 2;
128
    header.param    :=  header.sp;
142
    header.param    :=  header.sp;
129
    header.path     :=  header.param + PARAM_SIZE;
Line 143... Line 130...
143
    header.path     :=  header.param + PARAM_SIZE;
130
 
144
 
131
    code := program.code;
Line 145... Line 132...
145
    code := program.code;
132
    reloc := program.rel_list.first(BIN.RELOC);
146
    reloc := program.rel_list.first(BIN.RELOC);
133
    WHILE reloc # NIL DO
Line 147... Line 134...
147
    WHILE reloc # NIL DO
134
 
148
 
135
        offset := reloc.offset;
Line 149... Line 136...
149
        L := BIN.get32le(code, reloc.offset);
136
        L := BIN.get32le(code, offset);
150
        delta := 3 - reloc.offset - text;
137
        delta := 3 - offset - text;
Line 151... Line 138...
151
 
138
 
152
        CASE reloc.opcode OF
139
        CASE reloc.opcode OF
153
 
140
 
Line 154... Line 141...
154
        |BIN.RIMP:
141
        |BIN.RIMP:
155
              iproc := BIN.GetIProc(program, L);
142
              iproc := BIN.GetIProc(program, L);
Line 156... Line 143...
156
              BIN.put32le(code, reloc.offset, idata + iproc.label)
143
              delta := idata + iproc.label
-
 
144
 
Line 157... Line 145...
157
 
145
        |BIN.RBSS:
158
        |BIN.RBSS:
146
              delta := L + bss
Line 159... Line 147...
159
              BIN.put32le(code, reloc.offset, L + bss)
147
 
Line 160... Line 148...
160
 
148
        |BIN.RDATA:
161
        |BIN.RDATA:
149
              delta := L + data
162
              BIN.put32le(code, reloc.offset, L + data)
150
 
Line 163... Line 151...
163
 
151
        |BIN.RCODE:
164
        |BIN.RCODE:
152
              delta := BIN.GetLabel(program, L) + text
165
              BIN.put32le(code, reloc.offset, BIN.GetLabel(program, L) + text)
153
 
166
 
154
        |BIN.PICDATA:
167
        |BIN.PICDATA:
155
              INC(delta, L + data)
168
              BIN.put32le(code, reloc.offset, L + data + delta)
156
 
169
 
157
        |BIN.PICCODE:
Line 170... Line 158...
170
        |BIN.PICCODE:
158
              INC(delta, BIN.GetLabel(program, L) + text)
171
              BIN.put32le(code, reloc.offset, BIN.GetLabel(program, L) + text + delta)
159
 
Line 172... Line 160...
172
 
160
        |BIN.PICBSS:
173
        |BIN.PICBSS:
161
              INC(delta, L + bss)
Line 174... Line 162...
174
              BIN.put32le(code, reloc.offset, L + bss + delta)
162
 
175
 
163
        |BIN.PICIMP:
176
        |BIN.PICIMP:
164
              iproc := BIN.GetIProc(program, L);
Line 177... Line 165...
177
              iproc := BIN.GetIProc(program, L);
165
              INC(delta, idata + iproc.label)
178
              BIN.put32le(code, reloc.offset, idata + iproc.label + delta)
-
 
179
 
-
 
Line -... Line 166...
-
 
166
 
180
        |BIN.IMPTAB:
167
        |BIN.IMPTAB:
Line 181... Line 168...
181
              BIN.put32le(code, reloc.offset, idata + delta)
168
              INC(delta, idata)
182
 
169