lineinfo.pp 14 KB

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