fpc/compiler/oglx.pas
peter 785550d7e3 Merged revisions 2669,2673,2677,2683,2696,2699-2702,2704,2708,2712-2715,2718,2722-2723,2728-2730,2740,2769 via svnmerge from
svn+ssh://peter@www.freepascal.org/FPC/svn/fpc/branches/linker/compiler

........
r2669 | peter | 2006-02-23 09:31:21 +0100 (Thu, 23 Feb 2006) | 2 lines

  * add compiler dir

........
r2673 | peter | 2006-02-23 17:08:56 +0100 (Thu, 23 Feb 2006) | 2 lines

  * enabled more code

........
r2677 | peter | 2006-02-24 17:46:29 +0100 (Fri, 24 Feb 2006) | 2 lines

  * pe stub and headers

........
r2683 | peter | 2006-02-25 23:13:24 +0100 (Sat, 25 Feb 2006) | 2 lines

  * section options cleanup

........
r2696 | peter | 2006-02-26 20:27:41 +0100 (Sun, 26 Feb 2006) | 2 lines

  * fixed typecasts

........
r2699 | peter | 2006-02-26 23:04:32 +0100 (Sun, 26 Feb 2006) | 2 lines

  * simple linking works

........
r2700 | peter | 2006-02-27 09:44:50 +0100 (Mon, 27 Feb 2006) | 2 lines

  * internal linker script

........
r2701 | peter | 2006-02-27 12:05:12 +0100 (Mon, 27 Feb 2006) | 2 lines

  * make elf working again

........
r2702 | peter | 2006-02-27 14:04:43 +0100 (Mon, 27 Feb 2006) | 3 lines

  * disable dwarf for smartlinking with .a
  * fix section start in new .a file

........
r2704 | peter | 2006-02-27 18:30:43 +0100 (Mon, 27 Feb 2006) | 2 lines

  * stab section fixes

........
r2708 | peter | 2006-02-28 19:29:17 +0100 (Tue, 28 Feb 2006) | 2 lines

  * basic work to merge stabs sections

........
r2712 | peter | 2006-02-28 23:17:48 +0100 (Tue, 28 Feb 2006) | 2 lines

  * unload tmodules before linking

........
r2713 | peter | 2006-02-28 23:18:51 +0100 (Tue, 28 Feb 2006) | 2 lines

  * fixed stabs linking

........
r2714 | peter | 2006-02-28 23:19:19 +0100 (Tue, 28 Feb 2006) | 2 lines

  * show code and data size

........
r2715 | peter | 2006-02-28 23:25:35 +0100 (Tue, 28 Feb 2006) | 2 lines

  * unload .stabs from objdata after it is merged

........
r2718 | peter | 2006-03-01 12:24:38 +0100 (Wed, 01 Mar 2006) | 3 lines

  * memsize/datasize cleanup
  * check for exports/resources when adding module to linker

........
r2722 | peter | 2006-03-03 09:12:20 +0100 (Fri, 03 Mar 2006) | 2 lines

  * new TObjSymbol splitted from TAsmSymbol

........
r2723 | peter | 2006-03-03 14:08:55 +0100 (Fri, 03 Mar 2006) | 2 lines

  * coff fixes after recent objsymbol changes

........
r2728 | peter | 2006-03-03 22:43:04 +0100 (Fri, 03 Mar 2006) | 2 lines

  * fixed coff writer

........
r2729 | peter | 2006-03-04 01:10:32 +0100 (Sat, 04 Mar 2006) | 2 lines

  * fix read-only opening

........
r2730 | peter | 2006-03-04 01:11:16 +0100 (Sat, 04 Mar 2006) | 2 lines

  * Read edata from DLLs, basic work

........
r2740 | peter | 2006-03-04 21:13:43 +0100 (Sat, 04 Mar 2006) | 3 lines

  * deletedef added
  * don't remove defs from index when we are already clearing everything

........
r2769 | peter | 2006-03-05 21:42:33 +0100 (Sun, 05 Mar 2006) | 4 lines

  * moved TObj classes to ogbase
  * ObjSection.SymbolRefs and SymbolDefines list
  * DLL importing

........

git-svn-id: trunk@2771 -
2006-03-05 21:10:37 +00:00

395 lines
12 KiB
ObjectPascal

{
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;
{****************************************************************************
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 aktglobalswitches) 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.search('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;
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.