fpc/compiler/owbase.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

342 lines
6.4 KiB
ObjectPascal

{
Copyright (c) 1998-2002 by Peter Vreman
Contains the base stuff for writing for object files to disk
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 owbase;
{$i fpcdefs.inc}
interface
uses
cstreams,
cclasses;
type
tobjectwriter=class
private
f : TCFileStream;
opened : boolean;
buf : pchar;
bufidx : longint;
procedure writebuf;
protected
fsize,
fobjsize : longint;
public
constructor create;
destructor destroy;override;
function createfile(const fn:string):boolean;virtual;
procedure closefile;virtual;
procedure writesym(const sym:string);virtual;
procedure write(const b;len:longint);virtual;
procedure WriteZeros(l:longint);
procedure writearray(a:TDynamicArray);
property Size:longint read FSize;
property ObjSize:longint read FObjSize;
end;
tobjectreader=class
private
f : TCFileStream;
opened : boolean;
buf : pchar;
bufidx,
bufmax : longint;
function readbuf:boolean;
public
constructor create;
destructor destroy;override;
function openfile(const fn:string):boolean;virtual;
procedure closefile;virtual;
procedure seek(len:longint);
function read(out b;len:longint):boolean;virtual;
function readarray(a:TDynamicArray;len:longint):boolean;
end;
implementation
uses
verbose, globals;
const
bufsize = 32768;
{****************************************************************************
TObjectWriter
****************************************************************************}
constructor tobjectwriter.create;
begin
getmem(buf,bufsize);
bufidx:=0;
opened:=false;
fsize:=0;
end;
destructor tobjectwriter.destroy;
begin
if opened then
closefile;
freemem(buf,bufsize);
end;
function tobjectwriter.createfile(const fn:string):boolean;
begin
createfile:=false;
f:=TCFileStream.Create(fn,fmCreate);
if CStreamError<>0 then
begin
Message1(exec_e_cant_create_objectfile,fn);
exit;
end;
bufidx:=0;
fsize:=0;
fobjsize:=0;
opened:=true;
createfile:=true;
end;
procedure tobjectwriter.closefile;
var
fn : string;
begin
if bufidx>0 then
writebuf;
fn:=f.filename;
f.free;
{ Remove if size is 0 }
if size=0 then
RemoveFile(fn);
opened:=false;
fsize:=0;
fobjsize:=0;
end;
procedure tobjectwriter.writebuf;
begin
f.write(buf^,bufidx);
bufidx:=0;
end;
procedure tobjectwriter.writesym(const sym:string);
begin
end;
procedure tobjectwriter.write(const b;len:longint);
var
p : pchar;
left,
idx : longint;
begin
inc(fsize,len);
inc(fobjsize,len);
p:=pchar(@b);
idx:=0;
while len>0 do
begin
left:=bufsize-bufidx;
if len>left then
begin
move(p[idx],buf[bufidx],left);
dec(len,left);
inc(idx,left);
inc(bufidx,left);
writebuf;
end
else
begin
move(p[idx],buf[bufidx],len);
inc(bufidx,len);
exit;
end;
end;
end;
procedure tobjectwriter.WriteZeros(l:longint);
var
empty : array[0..1023] of byte;
begin
if l>sizeof(empty) then
internalerror(200404081);
if l>0 then
begin
fillchar(empty,l,0);
Write(empty,l);
end;
end;
procedure tobjectwriter.writearray(a:TDynamicArray);
var
hp : pdynamicblock;
begin
hp:=a.firstblock;
while assigned(hp) do
begin
write(hp^.data,hp^.used);
hp:=hp^.next;
end;
end;
{****************************************************************************
TObjectReader
****************************************************************************}
constructor tobjectreader.create;
begin
getmem(buf,bufsize);
bufidx:=0;
bufmax:=0;
opened:=false;
end;
destructor tobjectreader.destroy;
begin
if opened then
closefile;
freemem(buf,bufsize);
end;
function tobjectreader.openfile(const fn:string):boolean;
begin
openfile:=false;
f:=TCFileStream.Create(fn,fmOpenRead);
if CStreamError<>0 then
begin
Comment(V_Error,'Can''t open object file: '+fn);
exit;
end;
bufidx:=0;
bufmax:=0;
opened:=true;
openfile:=true;
end;
procedure tobjectreader.closefile;
begin
f.free;
opened:=false;
bufidx:=0;
bufmax:=0;
end;
function tobjectreader.readbuf:boolean;
begin
bufmax:=f.read(buf^,bufsize);
bufidx:=0;
readbuf:=(bufmax>0);
end;
procedure tobjectreader.seek(len:longint);
begin
f.seek(len,soFromBeginning);
bufidx:=0;
bufmax:=0;
end;
function tobjectreader.read(out b;len:longint):boolean;
var
p : pchar;
left,
idx : longint;
begin
read:=false;
if bufmax=0 then
if not readbuf then
exit;
p:=pchar(@b);
idx:=0;
while len>0 do
begin
left:=bufmax-bufidx;
if len>left then
begin
move(buf[bufidx],p[idx],left);
dec(len,left);
inc(idx,left);
inc(bufidx,left);
if not readbuf then
exit;
end
else
begin
move(buf[bufidx],p[idx],len);
inc(bufidx,len);
inc(idx,len);
break;
end;
end;
read:=(idx=len);
end;
function tobjectreader.readarray(a:TDynamicArray;len:longint):boolean;
var
orglen,
left,
idx : longint;
begin
readarray:=false;
if bufmax=0 then
if not readbuf then
exit;
orglen:=len;
idx:=0;
while len>0 do
begin
left:=bufmax-bufidx;
if len>left then
begin
a.Write(buf[bufidx],left);
dec(len,left);
inc(idx,left);
inc(bufidx,left);
if not readbuf then
exit;
end
else
begin
a.Write(buf[bufidx],len);
inc(bufidx,len);
inc(idx,len);
break;
end;
end;
readarray:=(idx=orglen);
end;
end.