/usr/share/fpcsrc/2.6.2/compiler/impdef.pas is in fpc-source-2.6.2 2.6.2-8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | {
Copyright (c) 1998-2002 by Pavel
This unit finds the export defs from PE files
C source code of DEWIN Windows disassembler (written by A. Milukov) was
partially used
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 impdef;
{$ifndef STANDALONE}
{$i fpcdefs.inc}
{$endif}
interface
uses
SysUtils;
var
as_name,
ar_name : string;
function makedef(const binname,
{$IFDEF STANDALONE}
textname,
{$ENDIF}
libname:string):longbool;
implementation
{$IFDEF STANDALONE}
var
__textname : string;
const
kind : array[longbool] of pchar=('',' DATA');
{$ENDIF}
var
f:file;
{$IFDEF STANDALONE}
t:text;
FileCreated:longbool;
{$ENDIF}
lname:string;
impname:string;
TheWord:array[0..1]of char;
PEoffset:cardinal;
loaded:longint;
function DOSstubOK(var x:cardinal):longbool;
begin
blockread(f,TheWord,2,loaded);
if loaded<>2 then
DOSstubOK:=false
else
begin
DOSstubOK:=TheWord='MZ';
seek(f,$3C);
blockread(f,x,4,loaded);
if(loaded<>4)or(x>filesize(f))then
DOSstubOK:=false;
end;
end;
function isPE(x:longint):longbool;
begin
seek(f,x);
blockread(f,TheWord,2,loaded);
isPE:=(loaded=2)and(TheWord='PE');
end;
var
cstring : array[0..127]of char;
function GetEdata(PE:cardinal):longbool;
type
TObjInfo=packed record
ObjName:array[0..7]of char;
VirtSize,
VirtAddr,
RawSize,
RawOffset,
Reloc,
LineNum:cardinal;
RelCount,
LineCount:word;
flags:cardinal;
end;
var
i:cardinal;
ObjOfs:cardinal;
Obj:TObjInfo;
APE_obj,APE_Optsize:word;
ExportRVA:cardinal;
delta:cardinal;
const
IMAGE_SCN_CNT_CODE=$00000020;
const
{$ifdef unix}
DirSep = '/';
{$else}
{$if defined(amiga) or defined(morphos)}
DirSep = '/';
{$else}
DirSep = '\';
{$endif}
{$endif}
var
path:string;
_d:dirstr;
_n:namestr;
_e:extstr;
common_created:longbool;
procedure cleardir(const s,ext:string);
var
ff:file;
dir:searchrec;
attr:word;
begin
findfirst(s+dirsep+ext,anyfile,dir);
while (doserror=0) do
begin
assign(ff,s+dirsep+dir.name);
GetFattr(ff,attr);
if not((DOSError<>0)or(Attr and Directory<>0))then
Erase(ff);
findnext(dir);
end;
findclose(dir);
end;
procedure CreateTempDir(const s:string);
var
attr:word;
ff:file;
begin
assign(ff,s);
GetFattr(ff,attr);
if DosError=0 then
begin
cleardir(s,'*.sw');
cleardir(s,'*.swo');
end
else
begin
{$I-}
mkdir(s);
{$I+}
if ioresult<>0 then;
end;
end;
procedure call_as(const name:string);
begin
FlushOutput;
ExecuteProcess(as_name,'-o '+name+'o '+name);
end;
procedure call_ar;
var
f:file;
attr:word;
begin
{$IFDEF STANDALONE}
if impname='' then
exit;
{$ENDIF}
assign(f,impname);
GetFAttr(f,attr);
If DOSError=0 then
erase(f);
FlushOutput;
ExecuteProcess(ar_name,'rs '+impname+' '+path+dirsep+'*.swo');
cleardir(path,'*.sw');
cleardir(path,'*.swo');
{$i-}
RmDir(path);
{$i+}
if ioresult<>0 then;
end;
procedure makeasm(index:cardinal;name:pchar;isData:longbool);
type
tt=array[1..1]of pchar;
pt=^tt;
const
fn_template:array[1..24]of pchar=(
'.section .idata$2',
'.rva .L4',
'.long 0,0',
'.rva ',
'.rva .L5',
'.section .idata$4',
'.L4:',
'.rva .L6',
'.long 0',
'.section .idata$5',
'.L5:',
'.text',
'.globl ',
':',
'jmp *.L7',
'.balign 4,144',
'.section .idata$5',
'.L7:',
'.rva .L6',
'.long 0',
'.section .idata$6',
'.L6:',
'.short 0',
'.ascii "\000"'
);
var_template:array[1..19]of pchar=(
'.section .idata$2',
'.rva .L7',
'.long 0,0',
'.rva ',
'.rva .L8',
'.section .idata$4',
'.L7:',
'.rva .L9',
'.long 0',
'.section .idata$5',
'.L8:',
'.globl ',
':',
'.rva .L9',
'.long 0',
'.section .idata$6',
'.L9:',
'.short 0',
'.ascii "\000"'
);
__template:array[longbool]of pointer=(@fn_template,@var_template);
common_part:array[1..5]of pchar=(
'.balign 2,0',
'.section .idata$7',
'.globl ',
':',
'.ascii "\000"'
);
posit:array[longbool,1..4]of longint=((4,13,14,24),(4,12,13,19));
var
template:array[longbool]of pt absolute __template;
f:text;
s:string;
i:longint;
n:string;
common_name,asmout:string;
__d:dirstr;
__n:namestr;
__x:extstr;
begin
if not common_created then
begin
common_name:='_$'+_n+'@common';
asmout:=path+dirsep+'0.sw';
assign(f,asmout);
rewrite(f);
for i:=1 to 5 do
begin
s:=StrPas(Common_part[i]);
case i of
3:
s:=s+common_name;
4:
s:=common_name+s;
5:
begin
fsplit(lname,__d,__n,__x);
insert(__n+__x,s,9);
end;
end;
writeln(f,s);
end;
close(f);
call_as(asmout);
common_created:=true;
end;
n:=strpas(name);
str(succ(index):0,s);
asmout:=path+dirsep+s+'.sw';
assign(f,asmout);
rewrite(f);
for i:=1 to posit[isData,4]do
begin
s:=StrPas(template[isData]^[i]);
if i=posit[isData,1]then
s:=s+common_name
else if i=posit[isData,2]then
s:=s+n
else if i=posit[isData,3]then
s:=n+s
else if i=posit[isData,4]then
insert(n,s,9);
writeln(f,s);
end;
close(f);
call_as(asmout);
end;
procedure ProcessEdata;
type
a8=array[0..7]of char;
function GetSectionName(rva:cardinal;var Flags:cardinal):a8;
var
i:cardinal;
LocObjOfs:cardinal;
LocObj:TObjInfo;
begin
GetSectionName:='';
Flags:=0;
LocObjOfs:=APE_OptSize+PEoffset+24;
for i:=1 to APE_obj do
begin
seek(f,LocObjOfs);
blockread(f,LocObj,sizeof(LocObj));
if(rva>=LocObj.VirtAddr)and(rva<=LocObj.VirtAddr+LocObj.RawSize)then
begin
GetSectionName:=a8(LocObj.ObjName);
Flags:=LocObj.flags;
end;
end;
end;
var
j,Fl:cardinal;
ulongval,procEntry:cardinal;
Ordinal:word;
isData:longbool;
ExpDir:packed record
flag,
stamp:cardinal;
Major,
Minor:word;
Name,
Base,
NumFuncs,
NumNames,
AddrFuncs,
AddrNames,
AddrOrds:cardinal;
end;
begin
with Obj do
begin
seek(f,RawOffset+delta);
blockread(f,ExpDir,sizeof(ExpDir));
fsplit(impname,_d,_n,_e);
path:=_d+_n+'.ils';
{$IFDEF STANDALONE}
if impname<>'' then
{$ENDIF}
CreateTempDir(path);
Common_created:=false;
for j:=0 to pred(ExpDir.NumNames)do
begin
seek(f,RawOffset-VirtAddr+ExpDir.AddrOrds+j*2);
blockread(f,Ordinal,2);
seek(f,RawOffset-VirtAddr+ExpDir.AddrFuncs+Cardinal(Ordinal*4));
blockread(f,ProcEntry,4);
seek(f,RawOffset-VirtAddr+ExpDir.AddrNames+j*4);
blockread(f,ulongval,4);
seek(f,RawOffset-VirtAddr+ulongval);
blockread(f,cstring,sizeof(cstring));
{$IFDEF STANDALONE}
if not FileCreated then
begin
FileCreated:=true;
if(__textname<>'')or(impname='')then
begin
rewrite(t);
writeln(t,'EXPORTS');
end;
end;
{$ENDIF}
isData:=GetSectionName(procentry,Fl)='';
if not isData then
isData:=Fl and IMAGE_SCN_CNT_CODE<>IMAGE_SCN_CNT_CODE;
{$IFDEF STANDALONE}
if(__textname<>'')or(impname='')then
writeln(t,cstring,' @',succ(ordinal):0,' ',kind[isData]);
if impname<>''then
{$ENDIF}
makeasm(j,cstring,isData);
end;
call_ar;
end;
end;
begin
GetEdata:=false;
{$IFDEF STANDALONE}
FileCreated:=false;
{$ENDIF}
seek(f,PE+120);
blockread(f,ExportRVA,4);
seek(f,PE+6);
blockread(f,APE_Obj,2);
seek(f,PE+20);
blockread(f,APE_OptSize,2);
ObjOfs:=APE_OptSize+PEoffset+24;
for i:=1 to APE_obj do
begin
seek(f,ObjOfs);
blockread(f,Obj,sizeof(Obj));
inc(ObjOfs,sizeof(Obj));
with Obj do
if(VirtAddr<=ExportRva)and(ExportRva<VirtAddr+VirtSize)then
begin
delta:=ExportRva-VirtAddr;
ProcessEdata;
GetEdata:=true;
end;
end;
end;
function makedef(const binname,
{$IFDEF STANDALONE}
textname,
{$ENDIF}
libname:string):longbool;
var
OldFileMode:longint;
begin
assign(f,binname);
{$IFDEF STANDALONE}
FileCreated:=false;
assign(t,textname);
__textname:=textname;
{$ENDIF}
impname:=libname;
lname:=binname;
OldFileMode:=filemode;
{$I-}
filemode:=0;
reset(f,1);
filemode:=OldFileMode;
{$I+}
if IOResult<>0 then
begin
makedef:=false;
exit;
end;
if not DOSstubOK(PEoffset)then
makedef:=false
else if not IsPE(PEoffset)then
makedef:=false
else
makedef:=GetEdata(PEoffset);
close(f);
{$IFDEF STANDALONE}
if FileCreated then
if(textname<>'')or(impname='')then
close(t);
{$ENDIF}
end;
end.
|