exeinfo.pp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2008 by Peter Vreman
  4. Executable file reading functions
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. This unit should not be compiled in objfpc mode, since this would make it
  13. dependent on objpas unit.
  14. }
  15. unit exeinfo;
  16. interface
  17. {$S-}
  18. type
  19. TExeFile=record
  20. f : file;
  21. // cached filesize
  22. size : int64;
  23. isopen : boolean;
  24. nsects : longint;
  25. sechdrofs,
  26. secstrofs : ptruint;
  27. processaddress : ptruint;
  28. FunctionRelative: boolean;
  29. // Offset of the binary image forming permanent offset to all retrieved values
  30. ImgOffset: ptruint;
  31. filename : string;
  32. // Allocate static buffer for reading data
  33. buf : array[0..4095] of byte;
  34. bufsize,
  35. bufcnt : longint;
  36. end;
  37. function OpenExeFile(var e:TExeFile;const fn:string):boolean;
  38. function FindExeSection(var e:TExeFile;const secname:string;var secofs,seclen:longint):boolean;
  39. function CloseExeFile(var e:TExeFile):boolean;
  40. function ReadDebugLink(var e:TExeFile;var dbgfn:string):boolean;
  41. procedure GetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: string);
  42. implementation
  43. uses
  44. strings{$ifdef windows},windows{$endif windows};
  45. {$ifdef unix}
  46. procedure GetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: string);
  47. begin
  48. if assigned(UnixGetModuleByAddrHook) then
  49. UnixGetModuleByAddrHook(addr,baseaddr,filename)
  50. else
  51. begin
  52. baseaddr:=nil;
  53. filename:=ParamStr(0);
  54. end;
  55. end;
  56. {$else unix}
  57. {$ifdef windows}
  58. var
  59. Tmm: TMemoryBasicInformation;
  60. {$ifdef FPC_OS_UNICODE}
  61. TST: array[0..Max_Path] of WideChar;
  62. {$else}
  63. TST: array[0..Max_Path] of Char;
  64. {$endif FPC_OS_UNICODE}
  65. procedure GetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: string);
  66. begin
  67. baseaddr:=nil;
  68. if VirtualQuery(addr, @Tmm, SizeOf(Tmm))<>sizeof(Tmm) then
  69. filename:=ParamStr(0)
  70. else
  71. begin
  72. baseaddr:=Tmm.AllocationBase;
  73. TST[0]:= #0;
  74. GetModuleFileName(THandle(Tmm.AllocationBase), TST, Length(TST));
  75. {$ifdef FPC_OS_UNICODE}
  76. filename:= String(PWideChar(@TST));
  77. {$else}
  78. filename:= String(PChar(@TST));
  79. {$endif FPC_OS_UNICODE}
  80. end;
  81. end;
  82. {$else windows}
  83. procedure GetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: string);
  84. begin
  85. baseaddr:= nil;
  86. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  87. filename:=ParamStr(0);
  88. {$else FPC_HAS_FEATURE_COMMANDARGS}
  89. filename:='';
  90. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  91. end;
  92. {$endif windows}
  93. {$endif unix}
  94. {****************************************************************************
  95. Executable Loaders
  96. ****************************************************************************}
  97. {$if defined(freebsd) or defined(netbsd) or defined (openbsd) or defined(linux) or defined(sunos) or defined(android) or defined(dragonfly)}
  98. {$ifdef cpu64}
  99. {$define ELF64}
  100. {$else}
  101. {$define ELF32}
  102. {$endif}
  103. {$endif}
  104. {$if defined(win32) or defined(wince)}
  105. {$define PE32}
  106. {$endif}
  107. {$if defined(win64)}
  108. {$define PE32PLUS}
  109. {$endif}
  110. {$ifdef netwlibc}
  111. {$define netware}
  112. {$endif}
  113. {$IFDEF OS2}
  114. {$DEFINE EMX}
  115. {$ENDIF OS2}
  116. {****************************************************************************
  117. DOS Stub
  118. ****************************************************************************}
  119. {$if defined(EMX) or defined(PE32) or defined(PE32PLUS) or defined(GO32V2)}
  120. type
  121. tdosheader = packed record
  122. e_magic : word;
  123. e_cblp : word;
  124. e_cp : word;
  125. e_crlc : word;
  126. e_cparhdr : word;
  127. e_minalloc : word;
  128. e_maxalloc : word;
  129. e_ss : word;
  130. e_sp : word;
  131. e_csum : word;
  132. e_ip : word;
  133. e_cs : word;
  134. e_lfarlc : word;
  135. e_ovno : word;
  136. e_res : array[0..3] of word;
  137. e_oemid : word;
  138. e_oeminfo : word;
  139. e_res2 : array[0..9] of word;
  140. e_lfanew : longint;
  141. end;
  142. {$endif EMX or PE32 or PE32PLUS or GO32v2}
  143. {****************************************************************************
  144. NLM
  145. ****************************************************************************}
  146. {$ifdef netware}
  147. function getByte(var f:file):byte;
  148. begin
  149. BlockRead (f,getByte,1);
  150. end;
  151. procedure Skip (var f:file; bytes : longint);
  152. var i : longint;
  153. begin
  154. for i := 1 to bytes do getbyte(f);
  155. end;
  156. function get0String (var f:file) : string;
  157. var c : char;
  158. begin
  159. get0String := '';
  160. c := char (getbyte(f));
  161. while (c <> #0) do
  162. begin
  163. get0String := get0String + c;
  164. c := char (getbyte(f));
  165. end;
  166. end;
  167. function getint32 (var f:file): longint;
  168. begin
  169. blockread (F, getint32, 4);
  170. end;
  171. const SIZE_OF_NLM_INTERNAL_FIXED_HEADER = 130;
  172. SIZE_OF_NLM_INTERNAL_VERSION_HEADER = 32;
  173. SIZE_OF_NLM_INTERNAL_EXTENDED_HEADER = 124;
  174. function openNetwareNLM(var e:TExeFile):boolean;
  175. var valid : boolean;
  176. name : string;
  177. hdrLength,
  178. dataOffset,
  179. dataLength : longint;
  180. function getLString : String;
  181. var Res:string;
  182. begin
  183. blockread (e.F, res, 1);
  184. if length (res) > 0 THEN
  185. blockread (e.F, res[1], length (res));
  186. getbyte(e.f);
  187. getLString := res;
  188. end;
  189. function getFixString (Len : byte) : string;
  190. var i : byte;
  191. begin
  192. getFixString := '';
  193. for I := 1 to Len do
  194. getFixString := getFixString + char (getbyte(e.f));
  195. end;
  196. function getword : word;
  197. begin
  198. blockread (e.F, getword, 2);
  199. end;
  200. begin
  201. e.sechdrofs := 0;
  202. openNetwareNLM:=false;
  203. // read and check header
  204. Skip (e.f,SIZE_OF_NLM_INTERNAL_FIXED_HEADER);
  205. getLString; // NLM Description
  206. getInt32(e.f); // Stacksize
  207. getInt32(e.f); // Reserved
  208. skip(e.f,5); // old Thread Name
  209. getLString; // Screen Name
  210. getLString; // Thread Name
  211. hdrLength := -1;
  212. dataOffset := -1;
  213. dataLength := -1;
  214. valid := true;
  215. repeat
  216. name := getFixString (8);
  217. if (name = 'VeRsIoN#') then
  218. begin
  219. Skip (e.f,SIZE_OF_NLM_INTERNAL_VERSION_HEADER-8);
  220. end else
  221. if (name = 'CoPyRiGh') then
  222. begin
  223. getword; // T=
  224. getLString; // Copyright String
  225. end else
  226. if (name = 'MeSsAgEs') then
  227. begin
  228. skip (e.f,SIZE_OF_NLM_INTERNAL_EXTENDED_HEADER - 8);
  229. end else
  230. if (name = 'CuStHeAd') then
  231. begin
  232. hdrLength := getInt32(e.f);
  233. dataOffset := getInt32(e.f);
  234. dataLength := getInt32(e.f);
  235. Skip (e.f,8); // dateStamp
  236. Valid := false;
  237. end else
  238. Valid := false;
  239. until not valid;
  240. if (hdrLength = -1) or (dataOffset = -1) or (dataLength = -1) then
  241. exit;
  242. Seek (e.F, dataOffset);
  243. e.sechdrofs := dataOffset;
  244. openNetwareNLM := (e.sechdrofs > 0);
  245. end;
  246. function FindSectionNetwareNLM(var e:TExeFile;const asecname:string;var secofs,seclen:longint):boolean;
  247. var name : string;
  248. alignAmount : longint;
  249. begin
  250. seek(e.f,e.sechdrofs);
  251. (* The format of the section information is:
  252. null terminated section name
  253. zeroes to adjust to 4 byte boundary
  254. 4 byte section data file pointer
  255. 4 byte section size *)
  256. Repeat
  257. Name := Get0String(e.f);
  258. alignAmount := 4 - ((length (Name) + 1) MOD 4);
  259. Skip (e.f,AlignAmount);
  260. if (Name = asecname) then
  261. begin
  262. secOfs := getInt32(e.f);
  263. secLen := getInt32(e.f);
  264. end else
  265. Skip(e.f,8);
  266. until (Name = '') or (Name = asecname);
  267. FindSectionNetwareNLM := (Name=asecname);
  268. end;
  269. {$endif}
  270. {****************************************************************************
  271. COFF
  272. ****************************************************************************}
  273. {$if defined(PE32) or defined(PE32PLUS) or defined(GO32V2)}
  274. type
  275. tcoffsechdr=packed record
  276. name : array[0..7] of char;
  277. vsize : longint;
  278. rvaofs : longint;
  279. datalen : longint;
  280. datapos : longint;
  281. relocpos : longint;
  282. lineno1 : longint;
  283. nrelocs : word;
  284. lineno2 : word;
  285. flags : longint;
  286. end;
  287. coffsymbol=packed record
  288. name : array[0..3] of char; { real is [0..7], which overlaps the strofs ! }
  289. strofs : longint;
  290. value : longint;
  291. section : smallint;
  292. empty : word;
  293. typ : byte;
  294. aux : byte;
  295. end;
  296. function FindSectionCoff(var e:TExeFile;const asecname:string;var secofs,seclen:longint):boolean;
  297. var
  298. i : longint;
  299. sechdr : tcoffsechdr;
  300. secname : string;
  301. secnamebuf : array[0..255] of char;
  302. code,
  303. oldofs,
  304. bufsize : longint;
  305. strofs : cardinal;
  306. begin
  307. FindSectionCoff:=false;
  308. { read section info }
  309. seek(e.f,e.sechdrofs);
  310. for i:=1 to e.nsects do
  311. begin
  312. blockread(e.f,sechdr,sizeof(sechdr),bufsize);
  313. move(sechdr.name,secnamebuf,8);
  314. secnamebuf[8]:=#0;
  315. secname:=strpas(secnamebuf);
  316. if secname[1]='/' then
  317. begin
  318. Val(Copy(secname,2,8),strofs,code);
  319. if code=0 then
  320. begin
  321. fillchar(secnamebuf,sizeof(secnamebuf),0);
  322. oldofs:=filepos(e.f);
  323. seek(e.f,e.secstrofs+strofs);
  324. blockread(e.f,secnamebuf,sizeof(secnamebuf),bufsize);
  325. seek(e.f,oldofs);
  326. secname:=strpas(secnamebuf);
  327. end
  328. else
  329. secname:='';
  330. end;
  331. if asecname=secname then
  332. begin
  333. secofs:=cardinal(sechdr.datapos) + E.ImgOffset;
  334. {$ifdef GO32V2}
  335. seclen:=sechdr.datalen;
  336. {$else GO32V2}
  337. { In PECOFF, datalen includes file padding up to the next section.
  338. vsize is the actual payload size if it does not exceed datalen,
  339. otherwise it is .bss (or alike) section that we should ignore. }
  340. if sechdr.vsize<=sechdr.datalen then
  341. seclen:=sechdr.vsize
  342. else
  343. exit;
  344. {$endif GO32V2}
  345. FindSectionCoff:=true;
  346. exit;
  347. end;
  348. end;
  349. end;
  350. {$endif PE32 or PE32PLUS or GO32V2}
  351. {$ifdef go32v2}
  352. function OpenGo32Coff(var e:TExeFile):boolean;
  353. type
  354. tgo32coffheader=packed record
  355. mach : word;
  356. nsects : word;
  357. time : longint;
  358. sympos : longint;
  359. syms : longint;
  360. opthdr : word;
  361. flag : word;
  362. other : array[0..27] of byte;
  363. end;
  364. const
  365. ParagraphSize = 512;
  366. var
  367. coffheader : tgo32coffheader;
  368. DosHeader: TDosHeader;
  369. BRead: cardinal;
  370. begin
  371. OpenGo32Coff:=false;
  372. { read and check header }
  373. if E.Size < SizeOf (DosHeader) then
  374. Exit;
  375. BlockRead (E.F, DosHeader, SizeOf (DosHeader), BRead);
  376. if BRead <> SizeOf (DosHeader) then
  377. Exit;
  378. if DosHeader.E_Magic = $5A4D then
  379. begin
  380. E.ImgOffset := DosHeader.e_cp * ParagraphSize;
  381. if DosHeader.e_cblp > 0 then
  382. E.ImgOffset := E.ImgOffset + DosHeader.e_cblp - ParagraphSize;
  383. end;
  384. if e.size < E.ImgOffset + sizeof(coffheader) then
  385. exit;
  386. seek(e.f,E.ImgOffset);
  387. blockread(e.f,coffheader,sizeof(coffheader));
  388. if coffheader.mach<>$14c then
  389. exit;
  390. e.sechdrofs:=filepos(e.f);
  391. e.nsects:=coffheader.nsects;
  392. e.secstrofs:=coffheader.sympos+coffheader.syms*sizeof(coffsymbol)+4;
  393. if e.secstrofs>e.size then
  394. exit;
  395. OpenGo32Coff:=true;
  396. end;
  397. {$endif Go32v2}
  398. {$ifdef PE32}
  399. function OpenPeCoff(var e:TExeFile):boolean;
  400. type
  401. tpeheader = packed record
  402. PEMagic : longint;
  403. Machine : word;
  404. NumberOfSections : word;
  405. TimeDateStamp : longint;
  406. PointerToSymbolTable : longint;
  407. NumberOfSymbols : longint;
  408. SizeOfOptionalHeader : word;
  409. Characteristics : word;
  410. Magic : word;
  411. MajorLinkerVersion : byte;
  412. MinorLinkerVersion : byte;
  413. SizeOfCode : longint;
  414. SizeOfInitializedData : longint;
  415. SizeOfUninitializedData : longint;
  416. AddressOfEntryPoint : longint;
  417. BaseOfCode : longint;
  418. BaseOfData : longint;
  419. ImageBase : longint;
  420. SectionAlignment : longint;
  421. FileAlignment : longint;
  422. MajorOperatingSystemVersion : word;
  423. MinorOperatingSystemVersion : word;
  424. MajorImageVersion : word;
  425. MinorImageVersion : word;
  426. MajorSubsystemVersion : word;
  427. MinorSubsystemVersion : word;
  428. Reserved1 : longint;
  429. SizeOfImage : longint;
  430. SizeOfHeaders : longint;
  431. CheckSum : longint;
  432. Subsystem : word;
  433. DllCharacteristics : word;
  434. SizeOfStackReserve : longint;
  435. SizeOfStackCommit : longint;
  436. SizeOfHeapReserve : longint;
  437. SizeOfHeapCommit : longint;
  438. LoaderFlags : longint;
  439. NumberOfRvaAndSizes : longint;
  440. DataDirectory : array[1..$80] of byte;
  441. end;
  442. var
  443. dosheader : tdosheader;
  444. peheader : tpeheader;
  445. begin
  446. OpenPeCoff:=false;
  447. { read and check header }
  448. if e.size<sizeof(dosheader) then
  449. exit;
  450. blockread(e.f,dosheader,sizeof(tdosheader));
  451. seek(e.f,dosheader.e_lfanew);
  452. blockread(e.f,peheader,sizeof(tpeheader));
  453. if peheader.pemagic<>$4550 then
  454. exit;
  455. e.sechdrofs:=filepos(e.f);
  456. e.nsects:=peheader.NumberOfSections;
  457. e.secstrofs:=peheader.PointerToSymbolTable+peheader.NumberOfSymbols*sizeof(coffsymbol);
  458. if e.secstrofs>e.size then
  459. exit;
  460. e.processaddress:=peheader.ImageBase;
  461. OpenPeCoff:=true;
  462. end;
  463. {$endif PE32}
  464. {$ifdef PE32PLUS}
  465. function OpenPePlusCoff(var e:TExeFile):boolean;
  466. type
  467. tpeheader = packed record
  468. PEMagic : longint;
  469. Machine : word;
  470. NumberOfSections : word;
  471. TimeDateStamp : longint;
  472. PointerToSymbolTable : longint;
  473. NumberOfSymbols : longint;
  474. SizeOfOptionalHeader : word;
  475. Characteristics : word;
  476. Magic : word;
  477. MajorLinkerVersion : byte;
  478. MinorLinkerVersion : byte;
  479. SizeOfCode : longint;
  480. SizeOfInitializedData : longint;
  481. SizeOfUninitializedData : longint;
  482. AddressOfEntryPoint : longint;
  483. BaseOfCode : longint;
  484. ImageBase : qword;
  485. SectionAlignment : longint;
  486. FileAlignment : longint;
  487. MajorOperatingSystemVersion : word;
  488. MinorOperatingSystemVersion : word;
  489. MajorImageVersion : word;
  490. MinorImageVersion : word;
  491. MajorSubsystemVersion : word;
  492. MinorSubsystemVersion : word;
  493. Reserved1 : longint;
  494. SizeOfImage : longint;
  495. SizeOfHeaders : longint;
  496. CheckSum : longint;
  497. Subsystem : word;
  498. DllCharacteristics : word;
  499. SizeOfStackReserve : qword;
  500. SizeOfStackCommit : qword;
  501. SizeOfHeapReserve : qword;
  502. SizeOfHeapCommit : qword;
  503. LoaderFlags : longint;
  504. NumberOfRvaAndSizes : longint;
  505. DataDirectory : array[1..$80] of byte;
  506. end;
  507. var
  508. dosheader : tdosheader;
  509. peheader : tpeheader;
  510. begin
  511. OpenPePlusCoff:=false;
  512. { read and check header }
  513. if E.Size<sizeof(dosheader) then
  514. exit;
  515. blockread(E.F,dosheader,sizeof(tdosheader));
  516. seek(E.F,dosheader.e_lfanew);
  517. blockread(E.F,peheader,sizeof(tpeheader));
  518. if peheader.pemagic<>$4550 then
  519. exit;
  520. e.sechdrofs:=filepos(e.f);
  521. e.nsects:=peheader.NumberOfSections;
  522. e.secstrofs:=peheader.PointerToSymbolTable+peheader.NumberOfSymbols*sizeof(coffsymbol);
  523. if e.secstrofs>e.size then
  524. exit;
  525. e.processaddress:=peheader.ImageBase;
  526. OpenPePlusCoff:=true;
  527. end;
  528. {$endif PE32PLUS}
  529. {****************************************************************************
  530. AOUT
  531. ****************************************************************************}
  532. {$IFDEF EMX}
  533. type
  534. TEmxHeader = packed record
  535. Version: array [1..16] of char;
  536. Bound: word;
  537. AoutOfs: longint;
  538. Options: array [1..42] of char;
  539. end;
  540. TAoutHeader = packed record
  541. Magic: word;
  542. Machine: byte;
  543. Flags: byte;
  544. TextSize: longint;
  545. DataSize: longint;
  546. BssSize: longint;
  547. SymbSize: longint;
  548. EntryPoint: longint;
  549. TextRelocSize: longint;
  550. DataRelocSize: longint;
  551. end;
  552. const
  553. PageSizeFill = $FFF;
  554. var
  555. DosHeader: TDosHeader;
  556. EmxHeader: TEmxHeader;
  557. AoutHeader: TAoutHeader;
  558. StabOfs: PtrUInt;
  559. S4: string [4];
  560. function OpenEMXaout (var E: TExeFile): boolean;
  561. begin
  562. OpenEMXaout := false;
  563. { GDB after 4.18 uses offset to function begin
  564. in text section but OS/2 version still uses 4.16 PM }
  565. E.FunctionRelative := false;
  566. { read and check header }
  567. if E.Size > SizeOf (DosHeader) then
  568. begin
  569. BlockRead (E.F, DosHeader, SizeOf (TDosHeader));
  570. {$IFDEF DEBUG_LINEINFO}
  571. WriteLn (StdErr, 'DosHeader.E_CParHdr = ', DosHeader.E_cParHdr);
  572. {$ENDIF DEBUG_LINEINFO}
  573. if E.Size > DosHeader.e_cparhdr shl 4 + SizeOf (TEmxHeader) then
  574. begin
  575. Seek (E.F, DosHeader.e_cparhdr shl 4);
  576. BlockRead (E.F, EmxHeader, SizeOf (TEmxHeader));
  577. S4 [0] := #4;
  578. Move (EmxHeader.Version, S4 [1], 4);
  579. if (S4 = 'emx ') and
  580. (E.Size > EmxHeader.AoutOfs + SizeOf (TAoutHeader)) then
  581. begin
  582. {$IFDEF DEBUG_LINEINFO}
  583. WriteLn (StdErr, 'EmxHeader.AoutOfs = ', EmxHeader.AoutOfs, '/', HexStr (pointer (EmxHeader.AoutOfs)));
  584. {$ENDIF DEBUG_LINEINFO}
  585. Seek (E.F, EmxHeader.AoutOfs);
  586. BlockRead (E.F, AoutHeader, SizeOf (TAoutHeader));
  587. {$IFDEF DEBUG_LINEINFO}
  588. WriteLn (StdErr, 'AoutHeader.Magic = ', AoutHeader.Magic);
  589. {$ENDIF DEBUG_LINEINFO}
  590. { if AOutHeader.Magic = $10B then}
  591. StabOfs := (EmxHeader.AoutOfs or PageSizeFill) + 1
  592. + AoutHeader.TextSize
  593. + AoutHeader.DataSize
  594. + AoutHeader.TextRelocSize
  595. + AoutHeader.DataRelocSize;
  596. {$IFDEF DEBUG_LINEINFO}
  597. WriteLn (StdErr, 'AoutHeader.TextSize = ', AoutHeader.TextSize, '/', HexStr (pointer (AoutHeader.TextSize)));
  598. WriteLn (StdErr, 'AoutHeader.DataSize = ', AoutHeader.DataSize, '/', HexStr (pointer (AoutHeader.DataSize)));
  599. WriteLn (StdErr, 'AoutHeader.TextRelocSize = ', AoutHeader.TextRelocSize, '/', HexStr (pointer (AoutHeader.TextRelocSize)));
  600. WriteLn (StdErr, 'AoutHeader.DataRelocSize = ', AoutHeader.DataRelocSize, '/', HexStr (pointer (AoutHeader.DataRelocSize)));
  601. WriteLn (StdErr, 'AoutHeader.SymbSize = ', AoutHeader.SymbSize, '/', HexStr (pointer (AoutHeader.SymbSize)));
  602. WriteLn (StdErr, 'StabOfs = ', StabOfs, '/', HexStr (pointer (StabOfs)));
  603. {$ENDIF DEBUG_LINEINFO}
  604. if E.Size > StabOfs + AoutHeader.SymbSize then
  605. OpenEMXaout := true;
  606. end;
  607. end;
  608. end;
  609. end;
  610. function FindSectionEMXaout (var E: TExeFile; const ASecName: string;
  611. var SecOfs, SecLen: longint): boolean;
  612. begin
  613. FindSectionEMXaout := false;
  614. if ASecName = '.stab' then
  615. begin
  616. SecOfs := StabOfs;
  617. SecLen := AoutHeader.SymbSize;
  618. FindSectionEMXaout := true;
  619. end else
  620. if ASecName = '.stabstr' then
  621. begin
  622. SecOfs := StabOfs + AoutHeader.SymbSize;
  623. SecLen := E.Size - Pred (SecOfs);
  624. FindSectionEMXaout := true;
  625. end;
  626. end;
  627. {$ENDIF EMX}
  628. {****************************************************************************
  629. ELF
  630. ****************************************************************************}
  631. {$if defined(ELF32) or defined(BEOS)}
  632. type
  633. telfheader=packed record
  634. magic0123 : longint;
  635. file_class : byte;
  636. data_encoding : byte;
  637. file_version : byte;
  638. padding : array[$07..$0f] of byte;
  639. e_type : word;
  640. e_machine : word;
  641. e_version : longword;
  642. e_entry : longword; // entrypoint
  643. e_phoff : longword; // program header offset
  644. e_shoff : longword; // sections header offset
  645. e_flags : longword;
  646. e_ehsize : word; // elf header size in bytes
  647. e_phentsize : word; // size of an entry in the program header array
  648. e_phnum : word; // 0..e_phnum-1 of entrys
  649. e_shentsize : word; // size of an entry in sections header array
  650. e_shnum : word; // 0..e_shnum-1 of entrys
  651. e_shstrndx : word; // index of string section header
  652. end;
  653. telfsechdr=packed record
  654. sh_name : longword;
  655. sh_type : longword;
  656. sh_flags : longword;
  657. sh_addr : longword;
  658. sh_offset : longword;
  659. sh_size : longword;
  660. sh_link : longword;
  661. sh_info : longword;
  662. sh_addralign : longword;
  663. sh_entsize : longword;
  664. end;
  665. {$endif ELF32 or BEOS}
  666. {$ifdef ELF64}
  667. type
  668. telfheader=packed record
  669. magic0123 : longint;
  670. file_class : byte;
  671. data_encoding : byte;
  672. file_version : byte;
  673. padding : array[$07..$0f] of byte;
  674. e_type : word;
  675. e_machine : word;
  676. e_version : longword;
  677. e_entry : int64; // entrypoint
  678. e_phoff : int64; // program header offset
  679. e_shoff : int64; // sections header offset
  680. e_flags : longword;
  681. e_ehsize : word; // elf header size in bytes
  682. e_phentsize : word; // size of an entry in the program header array
  683. e_phnum : word; // 0..e_phnum-1 of entrys
  684. e_shentsize : word; // size of an entry in sections header array
  685. e_shnum : word; // 0..e_shnum-1 of entrys
  686. e_shstrndx : word; // index of string section header
  687. end;
  688. type
  689. telfsechdr=packed record
  690. sh_name : longword;
  691. sh_type : longword;
  692. sh_flags : int64;
  693. sh_addr : int64;
  694. sh_offset : int64;
  695. sh_size : int64;
  696. sh_link : longword;
  697. sh_info : longword;
  698. sh_addralign : int64;
  699. sh_entsize : int64;
  700. end;
  701. {$endif ELF64}
  702. {$if defined(ELF32) or defined(ELF64) or defined(BEOS)}
  703. function OpenElf(var e:TExeFile):boolean;
  704. var
  705. elfheader : telfheader;
  706. elfsec : telfsechdr;
  707. begin
  708. OpenElf:=false;
  709. { read and check header }
  710. if e.size<sizeof(telfheader) then
  711. exit;
  712. blockread(e.f,elfheader,sizeof(telfheader));
  713. if elfheader.magic0123<>{$ifdef ENDIAN_LITTLE}$464c457f{$else}$7f454c46{$endif} then
  714. exit;
  715. if elfheader.e_shentsize<>sizeof(telfsechdr) then
  716. exit;
  717. { read section names }
  718. seek(e.f,elfheader.e_shoff+elfheader.e_shstrndx*cardinal(sizeof(telfsechdr)));
  719. blockread(e.f,elfsec,sizeof(telfsechdr));
  720. e.secstrofs:=elfsec.sh_offset;
  721. e.sechdrofs:=elfheader.e_shoff;
  722. e.nsects:=elfheader.e_shnum;
  723. OpenElf:=true;
  724. end;
  725. function FindSectionElf(var e:TExeFile;const asecname:string;var secofs,seclen:longint):boolean;
  726. var
  727. elfsec : telfsechdr;
  728. secname : string;
  729. secnamebuf : array[0..255] of char;
  730. oldofs,
  731. bufsize,i : longint;
  732. begin
  733. FindSectionElf:=false;
  734. seek(e.f,e.sechdrofs);
  735. for i:=1 to e.nsects do
  736. begin
  737. blockread(e.f,elfsec,sizeof(telfsechdr));
  738. fillchar(secnamebuf,sizeof(secnamebuf),0);
  739. oldofs:=filepos(e.f);
  740. seek(e.f,e.secstrofs+elfsec.sh_name);
  741. blockread(e.f,secnamebuf,sizeof(secnamebuf)-1,bufsize);
  742. seek(e.f,oldofs);
  743. secname:=strpas(secnamebuf);
  744. if asecname=secname then
  745. begin
  746. secofs:=elfsec.sh_offset;
  747. seclen:=elfsec.sh_size;
  748. FindSectionElf:=true;
  749. exit;
  750. end;
  751. end;
  752. end;
  753. {$endif ELF32 or ELF64 or BEOS}
  754. {$ifdef beos}
  755. {$i ptypes.inc}
  756. type
  757. // Descriptive formats
  758. status_t = Longint;
  759. team_id = Longint;
  760. image_id = Longint;
  761. { image types }
  762. const
  763. B_APP_IMAGE = 1;
  764. B_LIBRARY_IMAGE = 2;
  765. B_ADD_ON_IMAGE = 3;
  766. B_SYSTEM_IMAGE = 4;
  767. B_OK = 0;
  768. type
  769. image_info = packed record
  770. id : image_id;
  771. _type : longint;
  772. sequence: longint;
  773. init_order: longint;
  774. init_routine: pointer;
  775. term_routine: pointer;
  776. device: dev_t;
  777. node: ino_t;
  778. name: array[0..MAXPATHLEN-1] of char;
  779. { name: string[255];
  780. name2: string[255];
  781. name3: string[255];
  782. name4: string[255];
  783. name5: string[5];
  784. }
  785. text: pointer;
  786. data: pointer;
  787. text_size: longint;
  788. data_size: longint;
  789. end;
  790. function get_next_image_info(team: team_id; var cookie:longint; var info:image_info; size: size_t) : status_t;cdecl; external 'root' name '_get_next_image_info';
  791. function OpenElf32Beos(var e:TExeFile):boolean;
  792. var
  793. cookie : longint;
  794. info : image_info;
  795. begin
  796. // The only BeOS specific part is setting the processaddress
  797. cookie := 0;
  798. OpenElf32Beos:=false;
  799. fillchar(info, sizeof(image_info), 0);
  800. while get_next_image_info(0,cookie,info,sizeof(info))=B_OK do
  801. begin
  802. if e.filename=String(pchar(@info.name)) then
  803. begin
  804. if (info._type = B_APP_IMAGE) then
  805. e.processaddress := cardinal(info.text)
  806. else
  807. e.processaddress := 0;
  808. OpenElf32Beos := OpenElf(e);
  809. exit;
  810. end;
  811. end;
  812. end;
  813. {$endif beos}
  814. {****************************************************************************
  815. MACHO
  816. ****************************************************************************}
  817. {$ifdef darwin}
  818. type
  819. MachoFatHeader= packed record
  820. magic: longint;
  821. nfatarch: longint;
  822. end;
  823. MachoHeader=packed record
  824. magic: longword;
  825. cpu_type_t: longint;
  826. cpu_subtype_t: longint;
  827. filetype: longint;
  828. ncmds: longint;
  829. sizeofcmds: longint;
  830. flags: longint;
  831. end;
  832. cmdblock=packed record
  833. cmd: longint;
  834. cmdsize: longint;
  835. end;
  836. symbSeg=packed record
  837. symoff : longint;
  838. nsyms : longint;
  839. stroff : longint;
  840. strsize: longint;
  841. end;
  842. tstab=packed record
  843. strpos : longint;
  844. ntype : byte;
  845. nother : byte;
  846. ndesc : word;
  847. nvalue : dword;
  848. end;
  849. function OpenMachO32PPC(var e:TExeFile):boolean;
  850. var
  851. mh:MachoHeader;
  852. begin
  853. OpenMachO32PPC:= false;
  854. E.FunctionRelative:=false;
  855. if e.size<sizeof(mh) then
  856. exit;
  857. blockread (e.f, mh, sizeof(mh));
  858. e.sechdrofs:=filepos(e.f);
  859. e.nsects:=mh.ncmds;
  860. OpenMachO32PPC:=true;
  861. end;
  862. function FindSectionMachO32PPC(var e:TExeFile;const asecname:string;var secofs,seclen:longint):boolean;
  863. var
  864. i: longint;
  865. block:cmdblock;
  866. symbolsSeg: symbSeg;
  867. begin
  868. FindSectionMachO32PPC:=false;
  869. seek(e.f,e.sechdrofs);
  870. for i:= 1 to e.nsects do
  871. begin
  872. {$I-}
  873. blockread (e.f, block, sizeof(block));
  874. {$I+}
  875. if IOResult <> 0 then
  876. Exit;
  877. if block.cmd = $2 then
  878. begin
  879. blockread (e.f, symbolsSeg, sizeof(symbolsSeg));
  880. if asecname='.stab' then
  881. begin
  882. secofs:=symbolsSeg.symoff;
  883. { the caller will divide again by sizeof(tstab) }
  884. seclen:=symbolsSeg.nsyms*sizeof(tstab);
  885. FindSectionMachO32PPC:=true;
  886. end
  887. else if asecname='.stabstr' then
  888. begin
  889. secofs:=symbolsSeg.stroff;
  890. seclen:=symbolsSeg.strsize;
  891. FindSectionMachO32PPC:=true;
  892. end;
  893. exit;
  894. end;
  895. Seek(e.f, FilePos (e.f) + block.cmdsize - sizeof(block));
  896. end;
  897. end;
  898. {$endif darwin}
  899. {****************************************************************************
  900. CRC
  901. ****************************************************************************}
  902. var
  903. Crc32Tbl : array[0..255] of cardinal;
  904. procedure MakeCRC32Tbl;
  905. var
  906. crc : cardinal;
  907. i,n : integer;
  908. begin
  909. for i:=0 to 255 do
  910. begin
  911. crc:=i;
  912. for n:=1 to 8 do
  913. if (crc and 1)<>0 then
  914. crc:=(crc shr 1) xor cardinal($edb88320)
  915. else
  916. crc:=crc shr 1;
  917. Crc32Tbl[i]:=crc;
  918. end;
  919. end;
  920. Function UpdateCrc32(InitCrc:cardinal;const InBuf;InLen:LongInt):cardinal;
  921. var
  922. i : LongInt;
  923. p : pchar;
  924. begin
  925. if Crc32Tbl[1]=0 then
  926. MakeCrc32Tbl;
  927. p:=@InBuf;
  928. UpdateCrc32:=not InitCrc;
  929. for i:=1 to InLen do
  930. begin
  931. UpdateCrc32:=Crc32Tbl[byte(UpdateCrc32) xor byte(p^)] xor (UpdateCrc32 shr 8);
  932. inc(p);
  933. end;
  934. UpdateCrc32:=not UpdateCrc32;
  935. end;
  936. {****************************************************************************
  937. Generic Executable Open/Close
  938. ****************************************************************************}
  939. type
  940. TOpenProc=function(var e:TExeFile):boolean;
  941. TFindSectionProc=function(var e:TExeFile;const asecname:string;var secofs,seclen:longint):boolean;
  942. TExeProcRec=record
  943. openproc : TOpenProc;
  944. findproc : TFindSectionProc;
  945. end;
  946. const
  947. ExeProcs : TExeProcRec = (
  948. {$ifdef go32v2}
  949. openproc : @OpenGo32Coff;
  950. findproc : @FindSectionCoff;
  951. {$endif}
  952. {$ifdef PE32}
  953. openproc : @OpenPeCoff;
  954. findproc : @FindSectionCoff;
  955. {$endif}
  956. {$ifdef PE32PLUS}
  957. openproc : @OpenPePlusCoff;
  958. findproc : @FindSectionCoff;
  959. {$endif PE32PLUS}
  960. {$if defined(ELF32) or defined(ELF64)}
  961. openproc : @OpenElf;
  962. findproc : @FindSectionElf;
  963. {$endif ELF32 or ELF64}
  964. {$ifdef BEOS}
  965. openproc : @OpenElf32Beos;
  966. findproc : @FindSectionElf;
  967. {$endif BEOS}
  968. {$ifdef darwin}
  969. openproc : @OpenMachO32PPC;
  970. findproc : @FindSectionMachO32PPC;
  971. {$endif darwin}
  972. {$IFDEF EMX}
  973. openproc : @OpenEMXaout;
  974. findproc : @FindSectionEMXaout;
  975. {$ENDIF EMX}
  976. {$ifdef netware}
  977. openproc : @OpenNetwareNLM;
  978. findproc : @FindSectionNetwareNLM;
  979. {$endif}
  980. );
  981. function OpenExeFile(var e:TExeFile;const fn:string):boolean;
  982. var
  983. ofm : word;
  984. begin
  985. OpenExeFile:=false;
  986. fillchar(e,sizeof(e),0);
  987. e.bufsize:=sizeof(e.buf);
  988. e.filename:=fn;
  989. if fn='' then // we don't want to read stdin
  990. exit;
  991. assign(e.f,fn);
  992. {$I-}
  993. ofm:=filemode;
  994. filemode:=$40;
  995. reset(e.f,1);
  996. filemode:=ofm;
  997. {$I+}
  998. if ioresult<>0 then
  999. exit;
  1000. e.isopen:=true;
  1001. // cache filesize
  1002. e.size:=filesize(e.f);
  1003. E.FunctionRelative := true;
  1004. E.ImgOffset := 0;
  1005. if ExeProcs.OpenProc<>nil then
  1006. OpenExeFile:=ExeProcs.OpenProc(e);
  1007. end;
  1008. function CloseExeFile(var e:TExeFile):boolean;
  1009. begin
  1010. CloseExeFile:=false;
  1011. if not e.isopen then
  1012. exit;
  1013. e.isopen:=false;
  1014. close(e.f);
  1015. CloseExeFile:=true;
  1016. end;
  1017. function FindExeSection(var e:TExeFile;const secname:string;var secofs,seclen:longint):boolean;
  1018. begin
  1019. FindExeSection:=false;
  1020. if not e.isopen then
  1021. exit;
  1022. if ExeProcs.FindProc<>nil then
  1023. FindExeSection:=ExeProcs.FindProc(e,secname,secofs,seclen);
  1024. end;
  1025. function CheckDbgFile(var e:TExeFile;const fn:string;dbgcrc:cardinal):boolean;
  1026. var
  1027. c : cardinal;
  1028. ofm : word;
  1029. g : file;
  1030. begin
  1031. CheckDbgFile:=false;
  1032. assign(g,fn);
  1033. {$I-}
  1034. ofm:=filemode;
  1035. filemode:=$40;
  1036. reset(g,1);
  1037. filemode:=ofm;
  1038. {$I+}
  1039. if ioresult<>0 then
  1040. exit;
  1041. { We reuse the buffer from e here to prevent too much stack allocation }
  1042. c:=0;
  1043. repeat
  1044. blockread(g,e.buf,e.bufsize,e.bufcnt);
  1045. c:=UpdateCrc32(c,e.buf,e.bufcnt);
  1046. until e.bufcnt<e.bufsize;
  1047. close(g);
  1048. CheckDbgFile:=(dbgcrc=c);
  1049. end;
  1050. function ReadDebugLink(var e:TExeFile;var dbgfn:string):boolean;
  1051. var
  1052. dbglink : array[0..255] of char;
  1053. i,
  1054. dbglinklen,
  1055. dbglinkofs : longint;
  1056. dbgcrc : cardinal;
  1057. begin
  1058. ReadDebugLink:=false;
  1059. if not FindExeSection(e,'.gnu_debuglink',dbglinkofs,dbglinklen) then
  1060. exit;
  1061. if dbglinklen>sizeof(dbglink)-1 then
  1062. exit;
  1063. fillchar(dbglink,sizeof(dbglink),0);
  1064. seek(e.f,dbglinkofs);
  1065. blockread(e.f,dbglink,dbglinklen);
  1066. dbgfn:=strpas(dbglink);
  1067. if length(dbgfn)=0 then
  1068. exit;
  1069. i:=align(length(dbgfn)+1,4);
  1070. if (i+4)>dbglinklen then
  1071. exit;
  1072. move(dbglink[i],dbgcrc,4);
  1073. { current dir }
  1074. if CheckDbgFile(e,dbgfn,dbgcrc) then
  1075. begin
  1076. ReadDebugLink:=true;
  1077. exit;
  1078. end;
  1079. { executable dir }
  1080. i:=length(e.filename);
  1081. while (i>0) and not(e.filename[i] in AllowDirectorySeparators) do
  1082. dec(i);
  1083. if i>0 then
  1084. begin
  1085. dbgfn:=copy(e.filename,1,i)+dbgfn;
  1086. if CheckDbgFile(e,dbgfn,dbgcrc) then
  1087. begin
  1088. ReadDebugLink:=true;
  1089. exit;
  1090. end;
  1091. end;
  1092. end;
  1093. end.