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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
{
Copyright (c) 2002 by Daniel Mantione, Peter Vreman
Contains the binary reader and writer for the linear executable
format used by OS/2
* This code was inspired by the NASM sources
The Netwide Assembler is copyright (C) 1996 Simon Tatham and
Julian Hall. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit oglx;
{$i fpcdefs.inc}
interface
uses
{ common }
cclasses,
{ target }
systems,
{ assembler }
cpubase,aasmbase,assemble,link,
{ output }
ogbase,ogmap,ogcoff;
{ An LX executable is called a module; it can be either an executable
or a DLL.
A module consists of objects. In other executable formats, these
are usually called sections.
Objects consist of pages.
The objects are numbered, numbers do not have any special meaning. The
pages of the object are loaded into memory with the access rights specified
the object table entry. (DM)}
{ For the operating system the object numbers have no special meaning.
However, for Free Pascal generated executables, I define: (DM)}
const code_object = 0;
data_object = 1;
bss_object = 2;
stack_object = 3;
heap_object = 4;
type Tlxheader = packed record
magic:word; {'LX'}
byteorder:byte; {0 = little 1 = big endian.}
wordorder:byte; {0 = little 1 = big endian.}
format_level:cardinal; {Nothing else than LX level
0 has ever been defined.}
cpu_type:word; {1 = 286, 2 = 386, 3 = 486,
4 = pentium.}
os_type:word; {1 = OS/2, 2 = Windows,
3 = Siemens MS-Dos 4.0,
4 = Windows 386.}
module_version:cardinal; {Version of executable,
defined by user.}
module_flags:cardinal; {Flags.}
module_page_count:cardinal; {Amount of pages in module.}
eip_object,eip:cardinal; {Initial EIP, object nr and
offset within object.}
esp_object,esp:cardinal; {Initial ESP, object nr and
offset within object.}
page_size,page_shift:cardinal; {Page size, in bytes and
1 << pageshift.}
fixup_sect_size:cardinal;
fixup_sect_checksum:cardinal;
loader_sect_size:cardinal;
loader_sect_chksum:cardinal;
object_table_offset:cardinal; {Location of object table.}
object_count:cardinal; {Amount of objects in module.}
object_pagetable_ofs:cardinal; {Location of object page
table.}
object_iterpages_ofs:cardinal;
resource_table_ofs:cardinal; {Location of resource table.}
resource_count:cardinal; {Amount of resources in
resource table.}
resid_name_tbl_ofs:cardinal;
entry_table_offset:cardinal;
module_dir_offset:cardinal;
module_dir_count:cardinal;
fixup_pagetab_ofs:cardinal;
fixup_recrab_ofs:cardinal;
import_modtab_ofs:cardinal;
import_modtab_count:cardinal;
data_pages_offset:cardinal;
preload_page_count:cardinal;
nonresid_table_ofs:cardinal;
nonresid_table_len:cardinal;
nonresid_tbl_chksum:cardinal;
auto_ds_object_no:cardinal; {Not used by OS/2.}
debug_info_offset:cardinal;
inst_preload_count:cardinal;
inst_demand_count:cardinal;
heapsize:cardinal; {Only used for 16-bit programs.}
end;
Tlxobject_flags = (ofreadable,ofwriteable,ofexecutable,ofresource,
ofdiscardable,ofshared,ofpreload,ofinvalid,
ofzerofilled);
Tlxobject_flag_set = set of Tlxobject_flags;
Tlxobject_table_entry = packed record
virtual_size:cardinal;
reloc_base_addr:cardinal;
object_flags:Tlxobject_flag_set;
page_table_index:cardinal;
page_count:cardinal;
reserved:cardinal;
end;
Tlxexeoutput = class(texeoutput)
private
{ FCoffsyms,
FCoffStrs : tdynamicarray;
win32 : boolean;}
nsects,
nsyms,
sympos : longint;
procedure write_symbol(const name:string;strpos,value,section,typ,aux:longint);
procedure write_symbols;
protected
function writedata:boolean;override;
public
constructor createos2;
function newobjectinput:tobjectinput;override;
procedure CalculateMemoryMap;override;
procedure GenerateExecutable(const fn:string);override;
end;
Tlxlinker = class(tinternallinker)
constructor create;override;
end;
implementation
uses
strings,
cutils,verbose,
globtype,globals,fmodule,
owar;
{****************************************************************************
tcoffexeoutput
****************************************************************************}
constructor Tlxexeoutput.createos2;
begin
inherited create;
end;
function Tlxexeoutput.newobjectinput:tobjectinput;
begin
result:=tcoffobjectinput.createdjgpp;
end;
procedure Tlxexeoutput.write_symbol(const name:string;strpos,value,section,typ,aux:longint);
{ var
sym : coffsymbol;}
begin
{ FillChar(sym,sizeof(sym),0);
if strpos=-1 then
move(name[1],sym.name,length(name))
else
sym.strpos:=strpos;
sym.value:=value;
sym.section:=section;
sym.typ:=typ;
sym.aux:=aux;
FWriter.write(sym,sizeof(sym));}
end;
procedure Tlxexeoutput.write_symbols;
{ var
filename : string[18];
sec : TSection;
namestr : string[8];
nameidx,
value,
sectionval,
i : longint;
globalval : byte;
secrec : coffsectionrec;
objdata : TObjData;
p : tasmsymbol;
s : string;}
begin
(* objdata:=TObjData(objdatalist.first);
while assigned(objdata) do
begin
with tcoffObjData(objdata) do
begin
{ The symbols used }
p:=Tasmsymbol(symbols.First);
while assigned(p) do
begin
if p.section=sec_common then
sectionval:=sections[sec_bss].secsymidx
else
sectionval:=sections[p.section].secsymidx;
if p.currbind=AB_LOCAL then
globalval:=3
else
globalval:=2;
{ if local of global then set the section value to the address
of the symbol }
if p.currbind in [AB_LOCAL,AB_GLOBAL] then
value:=p.address
else
value:=p.size;
{ symbolname }
s:=p.name;
if length(s)>8 then
begin
nameidx:=FCoffStrs.size+4;
FCoffStrs.writestr(s);
FCoffStrs.writestr(#0);
end
else
begin
nameidx:=-1;
namestr:=s;
end;
write_symbol(namestr,nameidx,value,sectionval,globalval,0);
p:=tasmsymbol(p.indexnext);
end;
end;
objdata:=TObjData(objdata.next);
end;*)
end;
procedure Tlxexeoutput.CalculateMemoryMap;
{ var
objdata : TObjData;
secsymidx,
mempos,
datapos : longint;
sec : TSection;
sym : tasmsymbol;
s : TObjSection;}
begin
(* { retrieve amount of sections }
nsects:=0;
secsymidx:=0;
for sec:=low(TSection) to high(TSection) do
begin
if sections[sec].available then
begin
inc(nsects);
inc(secsymidx);
sections[sec].secsymidx:=secsymidx;
end;
end;
{ calculate start positions after the headers }
datapos:=sizeof(coffheader)+sizeof(coffoptheader)+sizeof(coffsechdr)*nsects;
mempos:=sizeof(coffheader)+sizeof(coffoptheader)+sizeof(coffsechdr)*nsects;
if not win32 then
inc(mempos,sizeof(go32v2stub)+$1000);
{ add sections }
MapObjData(datapos,mempos);
{ end symbol }
AddGlobalSym('_etext',sections[sec_code].mempos+sections[sec_code].memsize);
AddGlobalSym('_edata',sections[sec_data].mempos+sections[sec_data].memsize);
AddGlobalSym('end',mempos);
{ symbols }
nsyms:=0;
sympos:=0;
if not(cs_link_strip in current_settings.globalswitches) then
begin
sympos:=datapos;
objdata:=TObjData(objdatalist.first);
while assigned(objdata) do
begin
inc(nsyms,objdata.symbols.count);
objdata:=TObjData(objdata.next);
end;
end;*)
end;
function gen_section_header(sec:Tsection;obj:cardinal):Tlxobject_table_entry;
virtual_size:cardinal;
reloc_base_addr:cardinal;
object_flags:Tlxobject_flag_set;
page_table_index:cardinal;
page_count:cardinal;
reserved:cardinal;
begin
gen_section_header.virtual_size:=sections[sec.memsize];
end;
function Tlxexeoutput.writedata:boolean;
var header:Tlxheader;
hsym:Tasmsymbol;
code_object_header,data_object_header,bss_object_header,stack_object_header,
heap_object_header:Tlxobject_table_entry;
begin
result:=false;
fillchar(header,sizeof(header),0);
header.magic:=$584c; {'LX'}
header.cpu_type:=2; {Intel 386}
header.os_type:=1; {OS/2}
{Set the initial EIP.}
header.eip_object:=code_object;
hsym:=tasmsymbol(globalsyms.Find('start'));
if not assigned(hsym) then
begin
comment(V_Error,'Entrypoint "start" not defined');
exit;
end;
header.eip:=hsym.address-sections[sec_code].mempos;
{Set the initial ESP.}
header.esp_object:=stack_object;
header.esp:=stacksize;
Fwriter.write(header,sizeof(header));
for sec:=low(Tsection) to high(Tsection) do
if sections[sec].available then
if not(sec in [sec_code,sec_data,sec_bss,sec_stab,sec_stabstr]) then
begin
result:=false;
exit;
end;
code_object_header:=gen_section_header(sec_code,code_object);
data_object_header:=gen_section_header(sec_data,data_object);
bss_object_header:=gen_section_header(sec_bss,bss_object);
result:=true;
end;
procedure Tlxexeoutput.GenerateExecutable(const fn:string);
begin
{ AddGlobalSym('_etext',0);
AddGlobalSym('_edata',0);
AddGlobalSym('end',0);
if not CalculateSymbols then
exit;
CalculateMemoryMap;
FixupSymbols;
FixupRelocations;
writeexefile(fn);}
end;
{****************************************************************************
TCoffLinker
****************************************************************************}
constructor Tlxlinker.Create;
begin
inherited Create;
CArObjectReader:=TArObjectReader;
exeoutput:=Tlxexeoutput.createos2;
end;
{*****************************************************************************
Initialize
*****************************************************************************}
begin
{ RegisterAssembler(as_i386_coff_info,TCoffAssembler);
RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
RegisterLinker(ld_i386_coff,Tlxlinker);}
end.
|