fpc/compiler/owbase.pas
peter 17bc033747 Merged revisions 2791-2793,2798-2800,2806-2825,2829-2830,2833,2839,2898 via svnmerge from
http://svn.freepascal.org/svn/fpc/branches/linker/compiler

........
r2791 | peter | 2006-03-06 14:57:20 +0100 (Mon, 06 Mar 2006) | 3 lines

  * disable internal linker if -s is used
  * enable section smartlink by default for internal linker

........
r2792 | peter | 2006-03-06 14:58:23 +0100 (Mon, 06 Mar 2006) | 2 lines

  * support long sectionnames

........
r2793 | peter | 2006-03-06 15:04:12 +0100 (Mon, 06 Mar 2006) | 2 lines

  * register symbols in section also when reading .o files

........
r2798 | peter | 2006-03-07 10:08:07 +0100 (Tue, 07 Mar 2006) | 2 lines

  * symbolrefs need to be loaded from relocations when loading a .o

........
r2799 | peter | 2006-03-07 16:17:52 +0100 (Tue, 07 Mar 2006) | 3 lines

  * remove unreferenced sections
  * set stacksize in peopthaeder

........
r2800 | peter | 2006-03-07 17:02:46 +0100 (Tue, 07 Mar 2006) | 2 lines

  * objsection.fullname added

........
........
r2807 | peter | 2006-03-08 08:18:04 +0100 (Wed, 08 Mar 2006) | 2 lines

  * powerpc64 fixes

........
r2808 | peter | 2006-03-08 08:35:53 +0100 (Wed, 08 Mar 2006) | 2 lines

  * register x86_64_pecoff

........
r2809 | peter | 2006-03-08 11:26:38 +0100 (Wed, 08 Mar 2006) | 2 lines

  * optimize and cleanup matches()

........
r2810 | peter | 2006-03-08 12:25:28 +0100 (Wed, 08 Mar 2006) | 2 lines

  * small tweak to readdata to copy values direct without calling move()

........
r2811 | peter | 2006-03-08 15:55:21 +0100 (Wed, 08 Mar 2006) | 2 lines

  * compile fix

........
........
........
........
........
........
r2817 | peter | 2006-03-09 14:20:52 +0100 (Thu, 09 Mar 2006) | 2 lines

  * more readable with long secnames

........
........
........
........
........
........
........
........
r2825 | peter | 2006-03-10 09:52:05 +0100 (Fri, 10 Mar 2006) | 2 lines

  * don't initialize/finalize external variables

........
r2829 | peter | 2006-03-10 10:58:08 +0100 (Fri, 10 Mar 2006) | 2 lines

  * merge 64bit assembler

........
r2830 | peter | 2006-03-10 12:25:08 +0100 (Fri, 10 Mar 2006) | 2 lines

  * TElfAssembler rename

........
r2833 | peter | 2006-03-10 15:22:27 +0100 (Fri, 10 Mar 2006) | 3 lines

  * support & prefix to force identifier parsing, used to access fields that
    have the names of a register

........
r2839 | peter | 2006-03-10 19:37:11 +0100 (Fri, 10 Mar 2006) | 2 lines

  * merge stabs section flags

........
r2898 | peter | 2006-03-12 23:18:18 +0100 (Sun, 12 Mar 2006) | 2 lines

  * reorder instructions for better first match

........

git-svn-id: trunk@2902 -
2006-03-13 09:29:57 +00:00

344 lines
6.5 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;
bufleft,
idx : longint;
begin
inc(fsize,len);
inc(fobjsize,len);
p:=pchar(@b);
idx:=0;
while len>0 do
begin
bufleft:=bufsize-bufidx;
if len>bufleft then
begin
move(p[idx],buf[bufidx],bufleft);
dec(len,bufleft);
inc(idx,bufleft);
inc(bufidx,bufleft);
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;
lenleft,
bufleft,
idx : longint;
begin
result:=false;
if bufmax=0 then
if not readbuf then
exit;
p:=pchar(@b);
idx:=0;
lenleft:=len;
while lenleft>0 do
begin
bufleft:=bufmax-bufidx;
if lenleft>bufleft then
begin
move(buf[bufidx],p[idx],bufleft);
dec(lenleft,bufleft);
inc(idx,bufleft);
inc(bufidx,bufleft);
if not readbuf then
exit;
end
else
begin
move(buf[bufidx],p[idx],lenleft);
inc(bufidx,lenleft);
inc(idx,lenleft);
break;
end;
end;
result:=(idx=len);
end;
function tobjectreader.readarray(a:TDynamicArray;len:longint):boolean;
var
orglen,
bufleft,
idx : longint;
begin
readarray:=false;
if bufmax=0 then
if not readbuf then
exit;
orglen:=len;
idx:=0;
while len>0 do
begin
bufleft:=bufmax-bufidx;
if len>bufleft then
begin
a.Write(buf[bufidx],bufleft);
dec(len,bufleft);
inc(idx,bufleft);
inc(bufidx,bufleft);
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.