lineinfo.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2019 by the Free Pascal development team
  4. Stabs Line Info Retriever, Amiga-NG version
  5. can parse relocatable ELF executables
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. This unit should not be compiled in objfpc mode, since this would make it
  14. dependent on objpas unit.
  15. }
  16. {$IFNDEF FPC_DOTTEDUNITS}
  17. unit lineinfo;
  18. {$ENDIF FPC_DOTTEDUNITS}
  19. interface
  20. {$S-}
  21. {$Q-}
  22. {$IF FPC_VERSION<3}
  23. type
  24. CodePointer = Pointer;
  25. {$ENDIF}
  26. function GetLineInfo(addr:ptruint;var func,source:string;var line:longint) : boolean;
  27. function StabBackTraceStr(addr:CodePointer):string;
  28. procedure CloseStabs;
  29. var
  30. // Allows more efficient operation by reusing previously loaded debug data
  31. // when the target module filename is the same. However, if an invalid memory
  32. // address is supplied then further calls may result in an undefined behaviour.
  33. // In summary: enable for speed, disable for resilience.
  34. AllowReuseOfLineInfoData: Boolean = True;
  35. implementation
  36. {$IFDEF FPC_DOTTEDUNITS}
  37. uses
  38. System.ExeInfo,System.Strings;
  39. {$ELSE FPC_DOTTEDUNITS}
  40. uses
  41. exeinfo,strings;
  42. {$ENDIF FPC_DOTTEDUNITS}
  43. const
  44. N_Function = $24;
  45. N_TextLine = $44;
  46. N_DataLine = $46;
  47. N_BssLine = $48;
  48. N_SourceFile = $64;
  49. N_IncludeFile = $84;
  50. maxstabs = 128; { size of the stabs buffer }
  51. maxstabsreloc = 128; { size of the stabs reloc buffer }
  52. var
  53. { GDB after 4.18 uses offset to function begin
  54. in text section but OS/2 version still uses 4.16 PM }
  55. StabsFunctionRelative: boolean;
  56. StabsNeedsRelocation: boolean;
  57. type
  58. pstab=^tstab;
  59. tstab=packed record
  60. strpos : longint;
  61. ntype : byte;
  62. nother : byte;
  63. ndesc : word;
  64. nvalue : dword;
  65. end;
  66. type
  67. pelf32_rela = ^telf32_rela;
  68. telf32_rela = packed record
  69. r_offset: pointer;
  70. r_info: dword;
  71. r_addend: longint;
  72. end;
  73. type
  74. pelf32_rel = ^telf32_rel;
  75. telf32_rel = packed record
  76. r_offset: pointer;
  77. r_info: dword;
  78. end;
  79. type
  80. pelf32_sym = ^telf32_sym;
  81. telf32_sym = packed record
  82. st_name: dword;
  83. st_addr: pointer;
  84. st_size: dword;
  85. st_info: byte;
  86. st_other: byte;
  87. st_shndx: word;
  88. end;
  89. {$ifdef cpui386}
  90. type
  91. pelf32_reloc = ^telf32_reloc;
  92. telf32_reloc = telf32_rel;
  93. {$else}
  94. type
  95. pelf32_reloc = ^telf32_reloc;
  96. telf32_reloc = telf32_rela;
  97. {$endif}
  98. { We use static variable so almost no stack is required, and is thus
  99. more safe when an error has occurred in the program }
  100. {$WARNING This code is not thread-safe, and needs improvement }
  101. var
  102. e : TExeFile;
  103. stabcnt, { amount of stabs }
  104. stablen,
  105. stabofs, { absolute stab section offset in executable }
  106. stabstrlen,
  107. stabstrofs : longint; { absolute stabstr section offset in executable }
  108. dirlength : longint; { length of the dirctory part of the source file }
  109. stabs : array[0..maxstabs-1] of tstab; { buffer }
  110. stabsreloc : array[0..maxstabsreloc-1] of telf32_reloc;
  111. textofs,
  112. textlen: longint;
  113. symtabofs,
  114. symtablen: longint;
  115. funcstab, { stab with current function info }
  116. linestab, { stab with current line info }
  117. dirstab, { stab with current directory info }
  118. filestab : tstab; { stab with current file info }
  119. filename,
  120. lastfilename, { store last processed file }
  121. dbgfn : ansistring;
  122. lastopenstabs: Boolean; { store last result of processing a file }
  123. stabrelocofs,stabreloclen: longint;
  124. function OpenStabs(addr : pointer) : boolean;
  125. var
  126. baseaddr : pointer;
  127. begin
  128. // False by default
  129. OpenStabs:=false;
  130. // Empty so can test if GetModuleByAddr has worked
  131. filename := '';
  132. // Get filename by address using GetModuleByAddr
  133. GetModuleByAddr(addr,baseaddr,filename);
  134. {$ifdef DEBUG_LINEINFO}
  135. writeln(stderr,filename,' Baseaddr: ',hexstr(ptruint(baseaddr),sizeof(baseaddr)*2));
  136. {$endif DEBUG_LINEINFO}
  137. // Check if GetModuleByAddr has worked
  138. if filename = '' then
  139. exit;
  140. // If target filename same as previous, then re-use previous result
  141. if AllowReuseOfLineInfoData and (filename = lastfilename) then
  142. begin
  143. {$ifdef DEBUG_LINEINFO}
  144. writeln(stderr,'Reusing debug data');
  145. {$endif DEBUG_LINEINFO}
  146. OpenStabs:=lastopenstabs;
  147. exit;
  148. end;
  149. // Close previously opened stabs
  150. CloseStabs;
  151. // Reset last open stabs result
  152. lastopenstabs := false;
  153. // Save newly processed filename
  154. lastfilename := filename;
  155. // Open exe file or debug link
  156. if not OpenExeFile(e,filename) then
  157. exit;
  158. if ReadDebugLink(e,dbgfn) then
  159. begin
  160. CloseExeFile(e);
  161. if not OpenExeFile(e,dbgfn) then
  162. exit;
  163. end;
  164. // Find stab section
  165. {$ifdef BeOS}
  166. { Do not change ProcessAddress field for BeOS/Haiku
  167. if baseAddr is lower than ProcessAdress }
  168. if ptruint(baseaddr)>ptruint(e.processaddress) then
  169. {$endif BeOS}
  170. e.processaddress:=ptruint(baseaddr)-e.processaddress;
  171. StabsFunctionRelative := E.FunctionRelative;
  172. if FindExeSection(e,'.text',textofs,textlen) and
  173. FindExeSection(e,'.stab',stabofs,stablen) and
  174. FindExeSection(e,'.stabstr',stabstrofs,stabstrlen) then
  175. begin
  176. stabcnt:=stablen div sizeof(tstab);
  177. lastopenstabs:=true;
  178. OpenStabs:=true;
  179. end
  180. else
  181. CloseExeFile(e);
  182. end;
  183. procedure CloseStabs;
  184. begin
  185. if e.isopen then
  186. CloseExeFile(e);
  187. // Reset last processed filename
  188. lastfilename := '';
  189. end;
  190. var
  191. relocidx: longint;
  192. reloclen: longint;
  193. relocofs: longint;
  194. relocleft: longint;
  195. currentreloc: longint;
  196. function InitRelocs: boolean;
  197. var
  198. res: boolean;
  199. begin
  200. {$ifdef cpui386}
  201. res:=FindExeSection(e,'.rel.stab',stabrelocofs,stabreloclen);
  202. if res then
  203. res:=res and FindExeSection(e,'.symtab',symtabofs,symtablen);
  204. {$else}
  205. res:=FindExeSection(e,'.rela.stab',stabrelocofs,stabreloclen);
  206. {$endif}
  207. if res then
  208. begin
  209. reloclen:=maxstabsreloc;
  210. relocidx:=reloclen;
  211. relocofs:=stabrelocofs;
  212. relocleft:=stabreloclen;
  213. currentreloc:=-1;
  214. end;
  215. InitRelocs:=res;
  216. end;
  217. function min(a,b: longint): longint; inline;
  218. begin
  219. if a<b then min:=a else min:=b;
  220. end;
  221. function GetNextReloc: boolean;
  222. var
  223. origpos: longint;
  224. res: longint;
  225. readlen: longint;
  226. begin
  227. GetNextReloc:=false;
  228. if relocleft <= 0 then
  229. exit;
  230. inc(relocidx);
  231. if relocidx >= reloclen then
  232. begin
  233. origpos:=filepos(e.f);
  234. seek(e.f,relocofs);
  235. readlen:=min(relocleft,maxstabsreloc*sizeof(telf32_reloc));
  236. blockread(e.f,stabsreloc,readlen,res);
  237. reloclen:=res div sizeof(telf32_reloc);
  238. dec(relocleft,res);
  239. if reloclen <= 0 then
  240. exit;
  241. relocofs:=filepos(e.f);
  242. relocidx:=0;
  243. seek(e.f,origpos);
  244. end;
  245. currentreloc:=relocidx;
  246. GetNextReloc:=true;
  247. end;
  248. function GetSym(symnr: longint): telf32_sym;
  249. var
  250. origpos: longint;
  251. begin
  252. origpos:=filepos(e.f);
  253. seek(e.f,symtabofs+(symnr*sizeof(telf32_sym)));
  254. blockread(e.f,GetSym,sizeof(telf32_sym));
  255. seek(e.f,origpos);
  256. end;
  257. procedure RelocStabsEntries(stab: pstab; stablen: longint);
  258. const
  259. R_386_32 = 1;
  260. var
  261. origpos: longint;
  262. intostabsofs: longint;
  263. j: longint;
  264. rel: pelf32_reloc;
  265. sym: telf32_sym;
  266. begin
  267. origpos:=filepos(e.f);
  268. intostabsofs:=origpos-(stabofs+stablen*sizeof(tstab));
  269. j:=0;
  270. repeat
  271. rel:=@stabsreloc[currentreloc];
  272. while pointer(intostabsofs + (sizeof(tstab) * j) + 8) < rel^.r_offset do
  273. begin
  274. inc(j);
  275. if j >= stablen then exit;
  276. end;
  277. if (pointer(intostabsofs + (sizeof(tstab) * j) + 8) = rel^.r_offset) then
  278. begin
  279. {$ifdef cpui386}
  280. if byte(rel^.r_info) = R_386_32 then
  281. begin
  282. sym:=GetSym(rel^.r_info shr 8);
  283. inc(stab[j].nvalue,ptruint(sym.st_addr));
  284. end;
  285. {$endif}
  286. {$ifdef cpupowerpc}
  287. inc(stab[j].nvalue,rel^.r_addend);
  288. {$endif}
  289. end;
  290. until not GetNextReloc;
  291. end;
  292. function GetLineInfo(addr:ptruint;var func,source:string;var line:longint) : boolean;
  293. var
  294. res,
  295. stabsleft,
  296. stabscnt,i : longint;
  297. found : boolean;
  298. lastfunc : tstab;
  299. lastline : tstab;
  300. begin
  301. GetLineInfo:=false;
  302. {$ifdef DEBUG_LINEINFO}
  303. writeln(stderr,'GetLineInfo called');
  304. {$endif DEBUG_LINEINFO}
  305. fillchar(func,high(func)+1,0);
  306. fillchar(source,high(source)+1,0);
  307. line:=0;
  308. if not OpenStabs(pointer(addr)) then
  309. exit;
  310. { correct the value to the correct address in the file }
  311. { processaddress is set in OpenStabs }
  312. addr := dword(addr - e.processaddress);
  313. { if the address is outside our text segment, ignore it }
  314. if addr > textlen then
  315. exit;
  316. StabsNeedsRelocation:=InitRelocs and GetNextReloc;
  317. {$ifdef DEBUG_LINEINFO}
  318. writeln(stderr,'Addr: ',hexstr(addr,sizeof(addr)*2));
  319. {$endif DEBUG_LINEINFO}
  320. fillchar(funcstab,sizeof(tstab),0);
  321. fillchar(filestab,sizeof(tstab),0);
  322. fillchar(dirstab,sizeof(tstab),0);
  323. fillchar(linestab,sizeof(tstab),0);
  324. fillchar(lastfunc,sizeof(tstab),0);
  325. found:=false;
  326. seek(e.f,stabofs);
  327. stabsleft:=stabcnt;
  328. repeat
  329. if stabsleft>maxstabs then
  330. stabscnt:=maxstabs
  331. else
  332. stabscnt:=stabsleft;
  333. blockread(e.f,stabs,stabscnt*sizeof(tstab),res);
  334. stabscnt:=res div sizeof(tstab);
  335. if StabsNeedsRelocation then
  336. relocstabsentries(@stabs,stabscnt);
  337. for i:=0 to stabscnt-1 do
  338. begin
  339. case stabs[i].ntype of
  340. N_BssLine,
  341. N_DataLine:
  342. begin
  343. // for code line info, we don't care about these
  344. end;
  345. N_TextLine :
  346. begin
  347. lastline:=stabs[i];
  348. if StabsFunctionRelative then
  349. inc(lastline.nvalue,lastfunc.nvalue);
  350. if (addr>=linestab.nvalue) and (addr<lastline.nvalue) then
  351. begin
  352. found:=true;
  353. break;
  354. end;
  355. linestab:=lastline;
  356. end;
  357. N_Function :
  358. begin
  359. lastfunc:=stabs[i];
  360. if (stabs[i].nvalue<=addr) and
  361. (stabs[i].nvalue>funcstab.nvalue) then
  362. begin
  363. funcstab:=stabs[i];
  364. fillchar(linestab,sizeof(tstab),0);
  365. end;
  366. end;
  367. N_SourceFile,
  368. N_IncludeFile :
  369. begin
  370. if (stabs[i].nvalue<=addr) and
  371. (stabs[i].nvalue>=filestab.nvalue) then
  372. begin
  373. { if same value and type then the first one
  374. contained the directory PM }
  375. if (stabs[i].nvalue=filestab.nvalue) and
  376. (stabs[i].ntype=filestab.ntype) then
  377. dirstab:=filestab
  378. else
  379. fillchar(dirstab,sizeof(tstab),0);
  380. filestab:=stabs[i];
  381. fillchar(linestab,sizeof(tstab),0);
  382. { if new file then func is not valid anymore PM }
  383. if stabs[i].ntype=N_SourceFile then
  384. begin
  385. fillchar(funcstab,sizeof(tstab),0);
  386. fillchar(lastfunc,sizeof(tstab),0);
  387. end;
  388. end;
  389. end;
  390. end;
  391. end;
  392. dec(stabsleft,stabscnt);
  393. until found or (stabsleft=0);
  394. { get the line,source,function info }
  395. line:=linestab.ndesc;
  396. if dirstab.ntype<>0 then
  397. begin
  398. seek(e.f,stabstrofs+dirstab.strpos);
  399. blockread(e.f,source[1],high(source)-1,res);
  400. dirlength:=strlen(@source[1]);
  401. source[0]:=chr(dirlength);
  402. end
  403. else
  404. dirlength:=0;
  405. if filestab.ntype<>0 then
  406. begin
  407. seek(e.f,stabstrofs+filestab.strpos);
  408. blockread(e.f,source[dirlength+1],high(source)-(dirlength+1),res);
  409. source[0]:=chr(strlen(@source[1]));
  410. end;
  411. if funcstab.ntype<>0 then
  412. begin
  413. seek(e.f,stabstrofs+funcstab.strpos);
  414. blockread(e.f,func[1],high(func)-1,res);
  415. func[0]:=chr(strlen(@func[1]));
  416. i:=pos(':',func);
  417. if i>0 then
  418. Delete(func,i,255);
  419. end;
  420. if not AllowReuseOfLineInfoData then
  421. CloseStabs;
  422. GetLineInfo:=true;
  423. end;
  424. function StabBackTraceStr(addr:CodePointer):string;
  425. var
  426. func,
  427. source : string;
  428. hs : string;
  429. line : longint;
  430. Store : TBackTraceStrFunc;
  431. Success : boolean;
  432. begin
  433. {$ifdef DEBUG_LINEINFO}
  434. writeln(stderr,'StabBackTraceStr called');
  435. {$endif DEBUG_LINEINFO}
  436. { reset to prevent infinite recursion if problems inside the code PM }
  437. Success:=false;
  438. Store:=BackTraceStrFunc;
  439. BackTraceStrFunc:=@SysBackTraceStr;
  440. { on most architectures, (but not everywhere, Sparc is a notable exception)
  441. for valid stacktraces you have to substract sizeof(pointer), or similar
  442. instruction length from the trace address otherwise the lineinfo might
  443. be off-by-one, because of course the backtrace addresses don't point to
  444. the jump instructions, but the following address, which might belong to
  445. a different source line entirely (KB) }
  446. Success:=GetLineInfo(ptruint(addr-sizeof(pointer)),func,source,line);
  447. { create string }
  448. {$ifdef netware}
  449. { we need addr relative to code start on netware }
  450. dec(addr,ptruint(system.NWGetCodeStart));
  451. StabBackTraceStr:=' CodeStart + $'+HexStr(ptruint(addr),sizeof(ptruint)*2);
  452. {$else}
  453. if (addr<pointer(e.processaddress)) or (dword(addr-pointer(e.processaddress)) > textlen) then
  454. StabBackTraceStr:=' Addr $'+hexstr(addr)
  455. else
  456. StabBackTraceStr:=' Offs $'+hexstr(addr-e.processaddress);
  457. {$endif}
  458. if Success then
  459. begin
  460. if func<>'' then
  461. StabBackTraceStr:=StabBackTraceStr+' '+func;
  462. if source<>'' then
  463. begin
  464. if func<>'' then
  465. StabBackTraceStr:=StabBackTraceStr+', ';
  466. if line<>0 then
  467. begin
  468. str(line,hs);
  469. StabBackTraceStr:=StabBackTraceStr+' line '+hs;
  470. end;
  471. StabBackTraceStr:=StabBackTraceStr+' of '+source;
  472. end;
  473. end;
  474. BackTraceStrFunc:=Store;
  475. end;
  476. initialization
  477. lastfilename := '';
  478. lastopenstabs := false;
  479. BackTraceStrFunc:=@StabBackTraceStr;
  480. finalization
  481. CloseStabs;
  482. end.