BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Calculating the function size automaticly, asm knowledge req

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Language BASM
View previous topic :: View next topic  
Author Message
Davy Landman
Guest





PostPosted: Thu Apr 26, 2007 9:44 pm    Post subject: Calculating the function size automaticly, asm knowledge req Reply with quote



Hi Community,

I've been quite for a while due to my internship ending... But been walking
around with an idea I got when trying to find the sizes for the functions.

Looking at the MAP files I can calculate the function size but it's rounded
by DWORD because each next function is DWORD aligned. (as all of you
offcourse know..)

But I figured, it could be a start point.. because I know it's not more than
3 bytes off... So I created a function which filters the functions out of
the MAP file and calculates the function sizes.

But now the last part... I figured it would be posible to start a backwards
scan from @FunctionPointer + DWORDRoundedLength for the first non padding
byte... and than I'd have the actual function size (that is without the
lookup tables etc)

But my knowledge about asm is very limited... I would guess that searching
for the first non NOP or the last ret would be the end of the function...

But here are people who can help me a lot better defining the function end
point Characteristics.

In the end I would think it could be part of the BV where you'll just
compile it with the map file on (changes nothing in the binary) and click a
button to get all the function sizes.

So I would think this could be usefull?

Kind Regards,
Davy Landman
Back to top
Dennis
Guest





PostPosted: Fri Apr 27, 2007 7:00 pm    Post subject: Re: Calculating the function size automaticly, asm knowledge Reply with quote



Hi Davy

That would be very usefull. I hope some of the hardcore BASM guys find time
to help you finish the routines.

Best regards
Dennis Kjaer Christensen
Back to top
Davy Landman
Guest





PostPosted: Mon Apr 30, 2007 11:49 pm    Post subject: Re: Calculating the function size automaticly, asm knowledge Reply with quote



Hi,

I created the following function to determine the number of empty
instructions from a funtion..
function DetermineLastLength(const ALastPart: LongWord): Integer;
var
pFunctionBlock: PByteArray;
begin
Result := 4;
pFunctionBlock := @ALastPart;
while Result > 0 do
begin
if pFunctionBlock[Result - 1] <> $90 then // NOP
begin
if pFunctionBlock[Result -1] = $C3 then // RET
begin
if (Result = 1) or (pFunctionBlock[Result -2] <> $C3) then // detect
RET RET combination..?
begin
Break;
end;
end;
if pFunctionBlock[Result - 1] = $EB then // JMP
Break;
end;
Dec(Result);
end;
Result := 4- Result;
end;

are there any situations I forgot?

Kind Regards,
Davy Landman
"Davy Landman" <davy.landman.remove.spam (AT) gmail (DOT) com> wrote in message
news:4630d71e$1 (AT) newsgroups (DOT) borland.com...
Quote:
Hi Community,

I've been quite for a while due to my internship ending... But been
walking around with an idea I got when trying to find the sizes for the
functions.

Looking at the MAP files I can calculate the function size but it's
rounded by DWORD because each next function is DWORD aligned. (as all of
you offcourse know..)

But I figured, it could be a start point.. because I know it's not more
than 3 bytes off... So I created a function which filters the functions
out of the MAP file and calculates the function sizes.

But now the last part... I figured it would be posible to start a
backwards scan from @FunctionPointer + DWORDRoundedLength for the first
non padding byte... and than I'd have the actual function size (that is
without the lookup tables etc)

But my knowledge about asm is very limited... I would guess that searching
for the first non NOP or the last ret would be the end of the function...

But here are people who can help me a lot better defining the function
end point Characteristics.

In the end I would think it could be part of the BV where you'll just
compile it with the map file on (changes nothing in the binary) and click
a button to get all the function sizes.

So I would think this could be usefull?

Kind Regards,
Davy Landman
Back to top
Per Larsen
Guest





PostPosted: Tue May 01, 2007 12:22 am    Post subject: Re: Calculating the function size automaticly, asm knowledge Reply with quote

Quote:
are there any situations I forgot?

Depends what exactly it is you want to achieve/what level of compliance with
any kind of Delphi routine you want.

Take this function as an example:

function MapSample: string;
begin
Result := 'Hello World';
end;

According to the MAP file, it's 44 bytes long. That's correct in a sense,
but only the first 23 of those bytes are part of instructions, then there
are 9 bytes of padding followed by 12 bytes of constant data:

[.dpr.35] begin
push ebp
mov ebp, esp
push ecx
mov [ebp-04h], eax
[.dpr.36] Result := 'Hello World';
mov eax, [ebp-04h]
mov edx, offset @@1
call System::LStrAsg
[.dpr.37] end;
pop ecx
pop ebp
ret
db 00h FFh FFh FFh ; ....
db FFh 0Bh 00h 00h ; ....
db 00h ; .
@@1:
db 48h 65h 6Ch 6Ch ; Hell
db 6Fh 20h 57h 6Fh ; o Wo
db 72h 6Ch 64h 00h ; rld.


This is with D2007.

My point here is that you cant really tell by looking at the bytes of the
tail end of a routine alone if you're looking at data or instructions. Local
data like this may not occur very often (if at all) in the context where you
are planning to use it, however.

- Per
Back to top
Davy Landman
Guest





PostPosted: Tue May 08, 2007 10:33 pm    Post subject: Re: Calculating the function size automaticly, asm knowledge Reply with quote

Hi,

I forgot to answer due to busy stuff from work..
Quote:

Depends what exactly it is you want to achieve/what level of compliance
with any kind of Delphi routine you want.

The most offcourse.


Quote:
Take this function as an example:
very good example!
According to the MAP file, it's 44 bytes long. That's correct in a sense,
but only the first 23 of those bytes are part of instructions, then there
are 9 bytes of padding followed by 12 bytes of constant data:
... thats more difficult indeed... especialy the padding...


The function register could perhaps be extended with an extra param which is
the LookupTable size...

and than I'll need to find a way to detect the db stuff..

Anybody got an idea?

Kind regards,
Davy Landman
Back to top
Davy Landman
Guest





PostPosted: Tue May 08, 2007 10:51 pm    Post subject: Re: Calculating the function size automaticly, asm knowledge Reply with quote

Quote:
and than I'll need to find a way to detect the db stuff..
I mean the padding in the front.. because i allready now the size of the

lookuptable via the function's extra param
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Language BASM All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.