12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265 |
- {
- This file is part of the Free Pascal run time library.
- Copyright (c) 2000 by Peter Vreman
- Stabs Line Info Retriever
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- 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.
- **********************************************************************}
- unit lineinfo;
- interface
- {$IFDEF OS2}
- {$DEFINE EMX} (* EMX is the only possibility under OS/2 at the moment *)
- {$ENDIF OS2}
- {$S-}
- procedure GetLineInfo(addr:ptruint;var func,source:string;var line:longint);
- implementation
- uses
- strings;
- const
- N_Function = $24;
- N_TextLine = $44;
- N_DataLine = $46;
- N_BssLine = $48;
- N_SourceFile = $64;
- N_IncludeFile = $84;
- maxstabs = 40; { size of the stabs buffer }
- { GDB after 4.18 uses offset to function begin
- in text section but OS/2 version still uses 4.16 PM }
- StabsFunctionRelative : boolean = true;
- type
- pstab=^tstab;
- tstab=packed record
- strpos : longint;
- ntype : byte;
- nother : byte;
- ndesc : word;
- nvalue : dword;
- end;
- { We use static variable so almost no stack is required, and is thus
- more safe when an error has occured in the program }
- var
- opened : boolean; { set if the file is already open }
- f : file; { current file }
- stabcnt, { amount of stabs }
- stabofs, { absolute stab section offset in executable }
- stabstrofs : longint; { absolute stabstr section offset in executable }
- dirlength : longint; { length of the dirctory part of the source file }
- stabs : array[0..maxstabs-1] of tstab; { buffer }
- funcstab, { stab with current function info }
- linestab, { stab with current line info }
- dirstab, { stab with current directory info }
- filestab : tstab; { stab with current file info }
- { value to subtract to addr parameter to get correct address on file }
- { this should be equal to the process start address in memory }
- processaddress : ptruint;
- {****************************************************************************
- Executable Loaders
- ****************************************************************************}
- {$if defined(netbsd) or defined(freebsd) or defined(linux) or defined(sunos)}
- {$ifdef cpu64}
- {$define ELF64}
- {$else}
- {$define ELF32}
- {$endif}
- {$endif}
- {$if defined(win32) or defined(wince)}
- {$define PE32}
- {$endif}
- {$if defined(win64)}
- {$define PE32PLUS}
- {$endif}
- {$ifdef netwlibc}
- {$define netware}
- {$endif}
- {$ifdef netware}
- const SIZE_OF_NLM_INTERNAL_FIXED_HEADER = 130;
- SIZE_OF_NLM_INTERNAL_VERSION_HEADER = 32;
- SIZE_OF_NLM_INTERNAL_EXTENDED_HEADER = 124;
- function loadNetwareNLM:boolean;
- var valid : boolean;
- name : string;
- StabLength,
- StabStrLength,
- alignAmount,
- hdrLength,
- dataOffset,
- dataLength : longint;
- function getByte:byte;
- begin
- BlockRead (f,getByte,1);
- end;
- procedure Skip (bytes : longint);
- var i : longint;
- begin
- for i := 1 to bytes do getbyte;
- end;
- function getLString : String;
- var Res:string;
- begin
- blockread (F, res, 1);
- if length (res) > 0 THEN
- blockread (F, res[1], length (res));
- getbyte;
- getLString := res;
- end;
- function getFixString (Len : byte) : string;
- var i : byte;
- begin
- getFixString := '';
- for I := 1 to Len do
- getFixString := getFixString + char (getbyte);
- end;
- function get0String : string;
- var c : char;
- begin
- get0String := '';
- c := char (getbyte);
- while (c <> #0) do
- begin
- get0String := get0String + c;
- c := char (getbyte);
- end;
- end;
- function getword : word;
- begin
- blockread (F, getword, 2);
- end;
- function getint32 : longint;
- begin
- blockread (F, getint32, 4);
- end;
- begin
- processaddress := 0;
- LoadNetwareNLM:=false;
- stabofs:=-1;
- stabstrofs:=-1;
- { read and check header }
- Skip (SIZE_OF_NLM_INTERNAL_FIXED_HEADER);
- getLString; // NLM Description
- getInt32; // Stacksize
- getInt32; // Reserved
- skip(5); // old Thread Name
- getLString; // Screen Name
- getLString; // Thread Name
- hdrLength := -1;
- dataOffset := -1;
- dataLength := -1;
- valid := true;
- repeat
- name := getFixString (8);
- if (name = 'VeRsIoN#') then
- begin
- Skip (SIZE_OF_NLM_INTERNAL_VERSION_HEADER-8);
- end else
- if (name = 'CoPyRiGh') then
- begin
- getword; // T=
- getLString; // Copyright String
- end else
- if (name = 'MeSsAgEs') then
- begin
- skip (SIZE_OF_NLM_INTERNAL_EXTENDED_HEADER - 8);
- end else
- if (name = 'CuStHeAd') then
- begin
- hdrLength := getInt32;
- dataOffset := getInt32;
- dataLength := getInt32;
- Skip (8); // dataStamp
- Valid := false;
- end else
- Valid := false;
- until not valid;
- if (hdrLength = -1) or (dataOffset = -1) or (dataLength = -1) then
- exit;
- (* The format of the section information is:
- null terminated section name
- zeroes to adjust to 4 byte boundary
- 4 byte section data file pointer
- 4 byte section size *)
- Seek (F, dataOffset);
- stabOfs := 0;
- stabStrOfs := 0;
- Repeat
- Name := Get0String;
- alignAmount := 4 - ((length (Name) + 1) MOD 4);
- Skip (alignAmount);
- if (Name = '.stab') then
- begin
- stabOfs := getInt32;
- stabLength := getInt32;
- stabcnt:=stabLength div sizeof(tstab);
- end else
- if (Name = '.stabstr') then
- begin
- stabStrOfs := getInt32;
- stabStrLength := getInt32;
- end else
- Skip (8);
- until (Name = '') or ((StabOfs <> 0) and (stabStrOfs <> 0));
- Seek (F,stabOfs);
- //if (StabOfs = 0) then __ConsolePrintf ('StabOfs = 0');
- //if (StabStrOfs = 0) then __ConsolePrintf ('StabStrOfs = 0');
- LoadNetwareNLM := ((stabOfs > 0) and (stabStrOfs > 0));
- end;
- {$endif}
- {$ifdef go32v2}
- function LoadGo32Coff:boolean;
- type
- tcoffheader=packed record
- mach : word;
- nsects : word;
- time : longint;
- sympos : longint;
- syms : longint;
- opthdr : word;
- flag : word;
- other : array[0..27] of byte;
- end;
- tcoffsechdr=packed record
- name : array[0..7] of char;
- vsize : longint;
- rvaofs : longint;
- datalen : longint;
- datapos : longint;
- relocpos : longint;
- lineno1 : longint;
- nrelocs : word;
- lineno2 : word;
- flags : longint;
- end;
- var
- coffheader : tcoffheader;
- coffsec : tcoffsechdr;
- i : longint;
- begin
- processaddress := 0;
- LoadGo32Coff:=false;
- stabofs:=-1;
- stabstrofs:=-1;
- { read and check header }
- if filesize(f)<2048+sizeof(tcoffheader) then
- exit;
- seek(f,2048);
- blockread(f,coffheader,sizeof(tcoffheader));
- if coffheader.mach<>$14c then
- exit;
- { read section info }
- for i:=1to coffheader.nSects do
- begin
- blockread(f,coffsec,sizeof(tcoffsechdr));
- if (coffsec.name[4]='b') and
- (coffsec.name[1]='s') and
- (coffsec.name[2]='t') then
- begin
- if (coffsec.name[5]='s') and
- (coffsec.name[6]='t') then
- stabstrofs:=coffsec.datapos+2048
- else
- begin
- stabofs:=coffsec.datapos+2048;
- stabcnt:=coffsec.datalen div sizeof(tstab);
- end;
- end;
- end;
- LoadGo32Coff:=(stabofs<>-1) and (stabstrofs<>-1);
- end;
- {$endif Go32v2}
- {$ifdef PE32}
- function LoadPeCoff:boolean;
- type
- tdosheader = packed record
- e_magic : word;
- e_cblp : word;
- e_cp : word;
- e_crlc : word;
- e_cparhdr : word;
- e_minalloc : word;
- e_maxalloc : word;
- e_ss : word;
- e_sp : word;
- e_csum : word;
- e_ip : word;
- e_cs : word;
- e_lfarlc : word;
- e_ovno : word;
- e_res : array[0..3] of word;
- e_oemid : word;
- e_oeminfo : word;
- e_res2 : array[0..9] of word;
- e_lfanew : longint;
- end;
- tpeheader = packed record
- PEMagic : longint;
- Machine : word;
- NumberOfSections : word;
- TimeDateStamp : longint;
- PointerToSymbolTable : longint;
- NumberOfSymbols : longint;
- SizeOfOptionalHeader : word;
- Characteristics : word;
- Magic : word;
- MajorLinkerVersion : byte;
- MinorLinkerVersion : byte;
- SizeOfCode : longint;
- SizeOfInitializedData : longint;
- SizeOfUninitializedData : longint;
- AddressOfEntryPoint : longint;
- BaseOfCode : longint;
- BaseOfData : longint;
- ImageBase : longint;
- SectionAlignment : longint;
- FileAlignment : longint;
- MajorOperatingSystemVersion : word;
- MinorOperatingSystemVersion : word;
- MajorImageVersion : word;
- MinorImageVersion : word;
- MajorSubsystemVersion : word;
- MinorSubsystemVersion : word;
- Reserved1 : longint;
- SizeOfImage : longint;
- SizeOfHeaders : longint;
- CheckSum : longint;
- Subsystem : word;
- DllCharacteristics : word;
- SizeOfStackReserve : longint;
- SizeOfStackCommit : longint;
- SizeOfHeapReserve : longint;
- SizeOfHeapCommit : longint;
- LoaderFlags : longint;
- NumberOfRvaAndSizes : longint;
- DataDirectory : array[1..$80] of byte;
- end;
- tcoffsechdr=packed record
- name : array[0..7] of char;
- vsize : longint;
- rvaofs : longint;
- datalen : longint;
- datapos : longint;
- relocpos : longint;
- lineno1 : longint;
- nrelocs : word;
- lineno2 : word;
- flags : longint;
- end;
- var
- dosheader : tdosheader;
- peheader : tpeheader;
- coffsec : tcoffsechdr;
- i : longint;
- begin
- processaddress := 0;
- LoadPeCoff:=false;
- stabofs:=-1;
- stabstrofs:=-1;
- { read and check header }
- if filesize(f)<sizeof(dosheader) then
- exit;
- blockread(f,dosheader,sizeof(tdosheader));
- seek(f,dosheader.e_lfanew);
- blockread(f,peheader,sizeof(tpeheader));
- if peheader.pemagic<>$4550 then
- exit;
- { read section info }
- for i:=1 to peheader.NumberOfSections do
- begin
- blockread(f,coffsec,sizeof(tcoffsechdr));
- if (coffsec.name[4]='b') and
- (coffsec.name[1]='s') and
- (coffsec.name[2]='t') then
- begin
- if (coffsec.name[5]='s') and
- (coffsec.name[6]='t') then
- stabstrofs:=coffsec.datapos
- else
- begin
- stabofs:=coffsec.datapos;
- stabcnt:=coffsec.datalen div sizeof(tstab);
- end;
- end;
- end;
- LoadPeCoff:=(stabofs<>-1) and (stabstrofs<>-1);
- end;
- {$endif PE32}
- {$ifdef PE32PLUS}
- function LoadPeCoff:boolean;
- type
- tdosheader = packed record
- e_magic : word;
- e_cblp : word;
- e_cp : word;
- e_crlc : word;
- e_cparhdr : word;
- e_minalloc : word;
- e_maxalloc : word;
- e_ss : word;
- e_sp : word;
- e_csum : word;
- e_ip : word;
- e_cs : word;
- e_lfarlc : word;
- e_ovno : word;
- e_res : array[0..3] of word;
- e_oemid : word;
- e_oeminfo : word;
- e_res2 : array[0..9] of word;
- e_lfanew : longint;
- end;
- tpeheader = packed record
- PEMagic : longint;
- Machine : word;
- NumberOfSections : word;
- TimeDateStamp : longint;
- PointerToSymbolTable : longint;
- NumberOfSymbols : longint;
- SizeOfOptionalHeader : word;
- Characteristics : word;
- Magic : word;
- MajorLinkerVersion : byte;
- MinorLinkerVersion : byte;
- SizeOfCode : longint;
- SizeOfInitializedData : longint;
- SizeOfUninitializedData : longint;
- AddressOfEntryPoint : longint;
- BaseOfCode : longint;
- BaseOfData : longint;
- ImageBase : longint;
- SectionAlignment : longint;
- FileAlignment : longint;
- MajorOperatingSystemVersion : word;
- MinorOperatingSystemVersion : word;
- MajorImageVersion : word;
- MinorImageVersion : word;
- MajorSubsystemVersion : word;
- MinorSubsystemVersion : word;
- Reserved1 : longint;
- SizeOfImage : longint;
- SizeOfHeaders : longint;
- CheckSum : longint;
- Subsystem : word;
- DllCharacteristics : word;
- SizeOfStackReserve : int64;
- SizeOfStackCommit : int64;
- SizeOfHeapReserve : int64;
- SizeOfHeapCommit : int64;
- LoaderFlags : longint;
- NumberOfRvaAndSizes : longint;
- DataDirectory : array[1..$80] of byte;
- end;
- tcoffsechdr=packed record
- name : array[0..7] of char;
- vsize : longint;
- rvaofs : longint;
- datalen : longint;
- datapos : longint;
- relocpos : longint;
- lineno1 : longint;
- nrelocs : word;
- lineno2 : word;
- flags : longint;
- end;
- var
- dosheader : tdosheader;
- peheader : tpeheader;
- coffsec : tcoffsechdr;
- i : longint;
- begin
- processaddress := 0;
- LoadPeCoff:=false;
- stabofs:=-1;
- stabstrofs:=-1;
- { read and check header }
- if filesize(f)<sizeof(dosheader) then
- exit;
- blockread(f,dosheader,sizeof(tdosheader));
- seek(f,dosheader.e_lfanew);
- blockread(f,peheader,sizeof(tpeheader));
- if peheader.pemagic<>$4550 then
- exit;
- { read section info }
- for i:=1 to peheader.NumberOfSections do
- begin
- blockread(f,coffsec,sizeof(tcoffsechdr));
- if (coffsec.name[4]='b') and
- (coffsec.name[1]='s') and
- (coffsec.name[2]='t') then
- begin
- if (coffsec.name[5]='s') and
- (coffsec.name[6]='t') then
- stabstrofs:=coffsec.datapos
- else
- begin
- stabofs:=coffsec.datapos;
- stabcnt:=coffsec.datalen div sizeof(tstab);
- end;
- end;
- end;
- LoadPeCoff:=(stabofs<>-1) and (stabstrofs<>-1);
- end;
- {$endif PE32PLUS}
- {$IFDEF EMX}
- function LoadEMXaout: boolean;
- type
- TDosHeader = packed record
- e_magic : word;
- e_cblp : word;
- e_cp : word;
- e_crlc : word;
- e_cparhdr : word;
- e_minalloc : word;
- e_maxalloc : word;
- e_ss : word;
- e_sp : word;
- e_csum : word;
- e_ip : word;
- e_cs : word;
- e_lfarlc : word;
- e_ovno : word;
- e_res : array[0..3] of word;
- e_oemid : word;
- e_oeminfo : word;
- e_res2 : array[0..9] of word;
- e_lfanew : longint;
- end;
- TEmxHeader = packed record
- Version: array [1..16] of char;
- Bound: word;
- AoutOfs: longint;
- Options: array [1..42] of char;
- end;
- TAoutHeader = packed record
- Magic: word;
- Machine: byte;
- Flags: byte;
- TextSize: longint;
- DataSize: longint;
- BssSize: longint;
- SymbSize: longint;
- EntryPoint: longint;
- TextRelocSize: longint;
- DataRelocSize: longint;
- end;
- const
- StartPageSize = $1000;
- var
- DosHeader: TDosHeader;
- EmxHeader: TEmxHeader;
- AoutHeader: TAoutHeader;
- S4: string [4];
- begin
- processaddress := 0;
- LoadEMXaout := false;
- StabOfs := -1;
- StabStrOfs := -1;
- { read and check header }
- if FileSize (F) > SizeOf (DosHeader) then
- begin
- BlockRead (F, DosHeader, SizeOf (TDosHeader));
- Seek (F, DosHeader.e_cparhdr shl 4);
- BlockRead (F, EmxHeader, SizeOf (TEmxHeader));
- S4 [0] := #4;
- Move (EmxHeader.Version, S4 [1], 4);
- if S4 = 'emx ' then
- begin
- Seek (F, EmxHeader.AoutOfs);
- BlockRead (F, AoutHeader, SizeOf (TAoutHeader));
- if AOutHeader.Magic=$10B then
- StabOfs := StartPageSize
- else
- StabOfs :=EmxHeader.AoutOfs + SizeOf (TAoutHeader);
- StabOfs := StabOfs
- + AoutHeader.TextSize
- + AoutHeader.DataSize
- + AoutHeader.TextRelocSize
- + AoutHeader.DataRelocSize;
- StabCnt := AoutHeader.SymbSize div SizeOf (TStab);
- StabStrOfs := StabOfs + AoutHeader.SymbSize;
- StabsFunctionRelative:=false;
- LoadEMXaout := (StabOfs <> -1) and (StabStrOfs <> -1);
- end;
- end;
- end;
- {$ENDIF EMX}
- {$ifdef ELF32}
- function LoadElf32:boolean;
- type
- telf32header=packed record
- magic0123 : longint;
- file_class : byte;
- data_encoding : byte;
- file_version : byte;
- padding : array[$07..$0f] of byte;
- e_type : word;
- e_machine : word;
- e_version : longword;
- e_entry : longword; // entrypoint
- e_phoff : longword; // program header offset
- e_shoff : longword; // sections header offset
- e_flags : longword;
- e_ehsize : word; // elf header size in bytes
- e_phentsize : word; // size of an entry in the program header array
- e_phnum : word; // 0..e_phnum-1 of entrys
- e_shentsize : word; // size of an entry in sections header array
- e_shnum : word; // 0..e_shnum-1 of entrys
- e_shstrndx : word; // index of string section header
- end;
- telf32sechdr=packed record
- sh_name : longword;
- sh_type : longword;
- sh_flags : longword;
- sh_addr : longword;
- sh_offset : longword;
- sh_size : longword;
- sh_link : longword;
- sh_info : longword;
- sh_addralign : longword;
- sh_entsize : longword;
- end;
- var
- elfheader : telf32header;
- elfsec : telf32sechdr;
- secnames : array[0..255] of char;
- pname : pchar;
- i : longint;
- begin
- processaddress := 0;
- LoadElf32:=false;
- stabofs:=-1;
- stabstrofs:=-1;
- { read and check header }
- if filesize(f)<sizeof(telf32header) then
- exit;
- blockread(f,elfheader,sizeof(telf32header));
- {$ifdef ENDIAN_LITTLE}
- if elfheader.magic0123<>$464c457f then
- exit;
- {$endif ENDIAN_LITTLE}
- {$ifdef ENDIAN_BIG}
- if elfheader.magic0123<>$7f454c46 then
- exit;
- { this seems to be at least the case for m68k cpu PM }
- {$ifdef cpum68k}
- {StabsFunctionRelative:=false;}
- {$endif cpum68k}
- {$endif ENDIAN_BIG}
- if elfheader.e_shentsize<>sizeof(telf32sechdr) then
- exit;
- { read section names }
- seek(f,elfheader.e_shoff+elfheader.e_shstrndx*cardinal(sizeof(telf32sechdr)));
- blockread(f,elfsec,sizeof(telf32sechdr));
- seek(f,elfsec.sh_offset);
- blockread(f,secnames,sizeof(secnames));
- { read section info }
- seek(f,elfheader.e_shoff);
- for i:=1to elfheader.e_shnum do
- begin
- blockread(f,elfsec,sizeof(telf32sechdr));
- pname:=@secnames[elfsec.sh_name];
- if (pname[4]='b') and
- (pname[1]='s') and
- (pname[2]='t') then
- begin
- if (pname[5]='s') and
- (pname[6]='t') then
- stabstrofs:=elfsec.sh_offset
- else
- begin
- stabofs:=elfsec.sh_offset;
- stabcnt:=elfsec.sh_size div sizeof(tstab);
- end;
- end;
- end;
- LoadElf32:=(stabofs<>-1) and (stabstrofs<>-1);
- end;
- {$endif ELF32}
- {$ifdef ELF64}
- function LoadElf64:boolean;
- type
- telf64header=packed record
- magic0123 : longint;
- file_class : byte;
- data_encoding : byte;
- file_version : byte;
- padding : array[$07..$0f] of byte;
- e_type : word;
- e_machine : word;
- e_version : longword;
- e_entry : int64; // entrypoint
- e_phoff : int64; // program header offset
- e_shoff : int64; // sections header offset
- e_flags : longword;
- e_ehsize : word; // elf header size in bytes
- e_phentsize : word; // size of an entry in the program header array
- e_phnum : word; // 0..e_phnum-1 of entrys
- e_shentsize : word; // size of an entry in sections header array
- e_shnum : word; // 0..e_shnum-1 of entrys
- e_shstrndx : word; // index of string section header
- end;
- telf64sechdr=packed record
- sh_name : longword;
- sh_type : longword;
- sh_flags : int64;
- sh_addr : int64;
- sh_offset : int64;
- sh_size : int64;
- sh_link : longword;
- sh_info : longword;
- sh_addralign : int64;
- sh_entsize : int64;
- end;
- var
- elfheader : telf64header;
- elfsec : telf64sechdr;
- secnames : array[0..255] of char;
- pname : pchar;
- i : longint;
- begin
- processaddress := 0;
- LoadElf64:=false;
- stabofs:=-1;
- stabstrofs:=-1;
- { read and check header }
- if filesize(f)<sizeof(telf64header) then
- exit;
- blockread(f,elfheader,sizeof(telf64header));
- {$ifdef ENDIAN_LITTLE}
- if elfheader.magic0123<>$464c457f then
- exit;
- {$endif ENDIAN_LITTLE}
- {$ifdef ENDIAN_BIG}
- if elfheader.magic0123<>$7f454c46 then
- exit;
- { this seems to be at least the case for m68k cpu PM }
- {$ifdef cpum68k}
- {StabsFunctionRelative:=false;}
- {$endif cpum68k}
- {$endif ENDIAN_BIG}
- if elfheader.e_shentsize<>sizeof(telf64sechdr) then
- exit;
- { read section names }
- seek(f,elfheader.e_shoff+elfheader.e_shstrndx*cardinal(sizeof(telf64sechdr)));
- blockread(f,elfsec,sizeof(telf64sechdr));
- seek(f,elfsec.sh_offset);
- blockread(f,secnames,sizeof(secnames));
- { read section info }
- seek(f,elfheader.e_shoff);
- for i:=1to elfheader.e_shnum do
- begin
- blockread(f,elfsec,sizeof(telf64sechdr));
- pname:=@secnames[elfsec.sh_name];
- if (pname[4]='b') and
- (pname[1]='s') and
- (pname[2]='t') then
- begin
- if (pname[5]='s') and
- (pname[6]='t') then
- stabstrofs:=elfsec.sh_offset
- else
- begin
- stabofs:=elfsec.sh_offset;
- stabcnt:=elfsec.sh_size div sizeof(tstab);
- end;
- end;
- end;
- LoadElf64:=(stabofs<>-1) and (stabstrofs<>-1);
- end;
- {$endif ELF64}
- {$ifdef beos}
- {$i osposixh.inc}
- {$i syscall.inc}
- {$i beos.inc}
- 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';
- function LoadElf32Beos:boolean;
- type
- telf32header=packed record
- magic0123 : longint;
- file_class : byte;
- data_encoding : byte;
- file_version : byte;
- padding : array[$07..$0f] of byte;
- e_type : word;
- e_machine : word;
- e_version : longword;
- e_entry : longword; // entrypoint
- e_phoff : longword; // program header offset
- e_shoff : longword; // sections header offset
- e_flags : longword;
- e_ehsize : word; // elf header size in bytes
- e_phentsize : word; // size of an entry in the program header array
- e_phnum : word; // 0..e_phnum-1 of entrys
- e_shentsize : word; // size of an entry in sections header array
- e_shnum : word; // 0..e_shnum-1 of entrys
- e_shstrndx : word; // index of string section header
- end;
- telf32sechdr=packed record
- sh_name : longword;
- sh_type : longword;
- sh_flags : longword;
- sh_addr : longword;
- sh_offset : longword;
- sh_size : longword;
- sh_link : longword;
- sh_info : longword;
- sh_addralign : longword;
- sh_entsize : longword;
- end;
- var
- elfheader : telf32header;
- elfsec : telf32sechdr;
- secnames : array[0..255] of char;
- pname : pchar;
- i : longint;
- cookie : longint;
- info : image_info;
- result : status_t;
- begin
- cookie := 0;
- fillchar(info, sizeof(image_info), 0);
- get_next_image_info(0,cookie,info,sizeof(info));
- if (info._type = B_APP_IMAGE) then
- processaddress := cardinal(info.text)
- else
- processaddress := 0;
- LoadElf32Beos:=false;
- stabofs:=-1;
- stabstrofs:=-1;
- { read and check header }
- if filesize(f)<sizeof(telf32header) then
- exit;
- blockread(f,elfheader,sizeof(telf32header));
- {$ifdef ENDIAN_LITTLE}
- if elfheader.magic0123<>$464c457f then
- exit;
- {$endif ENDIAN_LITTLE}
- {$ifdef ENDIAN_BIG}
- if elfheader.magic0123<>$7f454c46 then
- exit;
- {$endif ENDIAN_BIG}
- if elfheader.e_shentsize<>sizeof(telf32sechdr) then
- exit;
- { read section names }
- seek(f,elfheader.e_shoff+elfheader.e_shstrndx*cardinal(sizeof(telf32sechdr)));
- blockread(f,elfsec,sizeof(telf32sechdr));
- seek(f,elfsec.sh_offset);
- blockread(f,secnames,sizeof(secnames));
- { read section info }
- seek(f,elfheader.e_shoff);
- for i:=1to elfheader.e_shnum do
- begin
- blockread(f,elfsec,sizeof(telf32sechdr));
- pname:=@secnames[elfsec.sh_name];
- if (pname[4]='b') and
- (pname[1]='s') and
- (pname[2]='t') then
- begin
- if (pname[5]='s') and
- (pname[6]='t') then
- stabstrofs:=elfsec.sh_offset
- else
- begin
- stabofs:=elfsec.sh_offset;
- stabcnt:=elfsec.sh_size div sizeof(tstab);
- end;
- end;
- end;
- LoadElf32Beos:=(stabofs<>-1) and (stabstrofs<>-1);
- end;
- {$endif beos}
- {$ifdef darwin}
- type
- MachoFatHeader=
- packed record
- magic: longint;
- nfatarch: longint;
- end;
- MachoHeader=
- packed record
- magic: longword;
- cpu_type_t: longint;
- cpu_subtype_t: longint;
- filetype: longint;
- ncmds: longint;
- sizeofcmds: longint;
- flags: longint;
- end;
- cmdblock=
- packed record
- cmd: longint;
- cmdsize: longint;
- end;
- symbSeg=
- packed record
- symoff : longint;
- nsyms : longint;
- stroff : longint;
- strsize: longint;
- end;
- function readCommand: boolean;
- var
- block:cmdblock;
- readMore :boolean;
- symbolsSeg: symbSeg;
- begin
- readCommand := false;
- readMore := true;
- blockread (f, block, sizeof(block));
- if block.cmd = $2 then
- begin
- blockread (f, symbolsSeg, sizeof(symbolsSeg));
- stabstrofs:=symbolsSeg.stroff;
- stabofs:=symbolsSeg.symoff;
- stabcnt:=symbolsSeg.nsyms;
- readMore := false;
- readCommand := true;
- exit;
- end;
- if readMore then
- begin
- Seek(f, FilePos (f) + block.cmdsize - sizeof(block));
- end;
- end;
- function LoadMachO32PPC:boolean;
- var
- mh:MachoHeader;
- i: longint;
- begin
- processaddress := 0;
- StabsFunctionRelative:=false;
- LoadMachO32PPC := false;
- blockread (f, mh, sizeof(mh));
- for i:= 1 to mh.ncmds do
- begin
- if readCommand then
- begin
- LoadMachO32PPC := true;
- exit;
- end;
- end;
- end;
- {$endif darwin}
- {****************************************************************************
- Executable Open/Close
- ****************************************************************************}
- procedure CloseStabs;
- begin
- close(f);
- opened:=false;
- end;
- function OpenStabs:boolean;
- var
- ofm : word;
- begin
- OpenStabs:=false;
- assign(f,paramstr(0));
- {$I-}
- ofm:=filemode;
- filemode:=$40;
- reset(f,1);
- filemode:=ofm;
- {$I+}
- if ioresult<>0 then
- exit;
- opened:=true;
- {$ifdef go32v2}
- if LoadGo32Coff then
- begin
- OpenStabs:=true;
- exit;
- end;
- {$endif}
- {$IFDEF EMX}
- if LoadEMXaout then
- begin
- OpenStabs:=true;
- exit;
- end;
- {$ENDIF EMX}
- {$ifdef PE32}
- if LoadPECoff then
- begin
- OpenStabs:=true;
- exit;
- end;
- {$endif}
- {$ifdef PE32PLUS}
- if LoadPECoff then
- begin
- OpenStabs:=true;
- exit;
- end;
- {$endif PE32PLUS}
- {$ifdef ELF32}
- if LoadElf32 then
- begin
- OpenStabs:=true;
- exit;
- end;
- {$endif}
- {$ifdef ELF64}
- if LoadElf64 then
- begin
- OpenStabs:=true;
- exit;
- end;
- {$endif}
- {$ifdef Beos}
- if LoadElf32Beos then
- begin
- OpenStabs:=true;
- exit;
- end;
- {$endif}
- {$ifdef darwin}
- if LoadMachO32PPC then
- begin
- OpenStabs:=true;
- exit;
- end;
- {$endif darwin}
- {$ifdef netware}
- if LoadNetwareNLM then
- begin
- OpenStabs:=true;
- exit;
- end;
- {$endif}
- CloseStabs;
- end;
- {$Q-}
- { this avoids problems with some targets PM }
- procedure GetLineInfo(addr:ptruint;var func,source:string;var line:longint);
- var
- res : {$ifdef tp}integer{$else}longint{$endif};
- stabsleft,
- stabscnt,i : longint;
- found : boolean;
- lastfunc : tstab;
- begin
- fillchar(func,high(func)+1,0);
- fillchar(source,high(source)+1,0);
- line:=0;
- if not opened then
- begin
- if not OpenStabs then
- exit;
- end;
- { correct the value to the correct address in the file }
- { processaddress is set in OpenStabs }
- addr := addr - processaddress;
- //ScreenPrintfL1 (NWLoggerScreen,'addr: %x\n',addr);
- fillchar(funcstab,sizeof(tstab),0);
- fillchar(filestab,sizeof(tstab),0);
- fillchar(dirstab,sizeof(tstab),0);
- fillchar(linestab,sizeof(tstab),0);
- fillchar(lastfunc,sizeof(tstab),0);
- found:=false;
- seek(f,stabofs);
- stabsleft:=stabcnt;
- repeat
- if stabsleft>maxstabs then
- stabscnt:=maxstabs
- else
- stabscnt:=stabsleft;
- blockread(f,stabs,stabscnt*sizeof(tstab),res);
- stabscnt:=res div sizeof(tstab);
- for i:=0 to stabscnt-1 do
- begin
- case stabs[i].ntype of
- N_BssLine,
- N_DataLine,
- N_TextLine :
- begin
- if (stabs[i].ntype=N_TextLine) and StabsFunctionRelative then
- inc(stabs[i].nvalue,lastfunc.nvalue);
- if (stabs[i].nvalue<=addr) and
- (stabs[i].nvalue>linestab.nvalue) then
- begin
- { if it's equal we can stop and take the last info }
- if stabs[i].nvalue=addr then
- found:=true
- else
- linestab:=stabs[i];
- end;
- end;
- N_Function :
- begin
- lastfunc:=stabs[i];
- if (stabs[i].nvalue<=addr) and
- (stabs[i].nvalue>funcstab.nvalue) then
- begin
- funcstab:=stabs[i];
- fillchar(linestab,sizeof(tstab),0);
- end;
- end;
- N_SourceFile,
- N_IncludeFile :
- begin
- if (stabs[i].nvalue<=addr) and
- (stabs[i].nvalue>=filestab.nvalue) then
- begin
- { if same value and type then the first one
- contained the directory PM }
- if (stabs[i].nvalue=filestab.nvalue) and
- (stabs[i].ntype=filestab.ntype) then
- dirstab:=filestab
- else
- fillchar(dirstab,sizeof(tstab),0);
- filestab:=stabs[i];
- fillchar(linestab,sizeof(tstab),0);
- { if new file then func is not valid anymore PM }
- if stabs[i].ntype=N_SourceFile then
- begin
- fillchar(funcstab,sizeof(tstab),0);
- fillchar(lastfunc,sizeof(tstab),0);
- end;
- end;
- end;
- end;
- end;
- dec(stabsleft,stabscnt);
- until found or (stabsleft=0);
- { get the line,source,function info }
- line:=linestab.ndesc;
- if dirstab.ntype<>0 then
- begin
- seek(f,stabstrofs+dirstab.strpos);
- blockread(f,source[1],high(source)-1,res);
- dirlength:=strlen(@source[1]);
- source[0]:=chr(dirlength);
- end
- else
- dirlength:=0;
- if filestab.ntype<>0 then
- begin
- seek(f,stabstrofs+filestab.strpos);
- blockread(f,source[dirlength+1],high(source)-(dirlength+1),res);
- source[0]:=chr(strlen(@source[1]));
- end;
- if funcstab.ntype<>0 then
- begin
- seek(f,stabstrofs+funcstab.strpos);
- blockread(f,func[1],high(func)-1,res);
- func[0]:=chr(strlen(@func[1]));
- i:=pos(':',func);
- if i>0 then
- Delete(func,i,255);
- end;
- end;
- function StabBackTraceStr(addr:Pointer):shortstring;
- var
- func,
- source : string;
- hs : string[32];
- line : longint;
- Store : TBackTraceStrFunc;
- begin
- { reset to prevent infinite recursion if problems inside the code PM }
- {$ifdef netware}
- dec(addr,ptruint(system.NWGetCodeStart)); {we need addr relative to code start on netware}
- {$endif}
- Store:=BackTraceStrFunc;
- BackTraceStrFunc:=@SysBackTraceStr;
- GetLineInfo(ptruint(addr),func,source,line);
- { create string }
- {$ifdef netware}
- StabBackTraceStr:=' CodeStart + $'+HexStr(ptrint(addr),sizeof(ptrint)*2);
- {$else}
- StabBackTraceStr:=' $'+HexStr(ptrint(addr),sizeof(ptrint)*2);
- {$endif}
- if func<>'' then
- StabBackTraceStr:=StabBackTraceStr+' '+func;
- if source<>'' then
- begin
- if func<>'' then
- StabBackTraceStr:=StabBackTraceStr+', ';
- if line<>0 then
- begin
- str(line,hs);
- StabBackTraceStr:=StabBackTraceStr+' line '+hs;
- end;
- StabBackTraceStr:=StabBackTraceStr+' of '+source;
- end;
- if Opened then
- BackTraceStrFunc:=Store;
- end;
- initialization
- BackTraceStrFunc:=@StabBackTraceStr;
- finalization
- if opened then
- CloseStabs;
- end.
|