IDA-Pro and Pascal: Sets & Propagating Types

In Pascal there is the Set object, that you set (n<256) bits and then can later check if bit n is set or not. Sort of like a bool array.

When you decompile a DOS Pascal program the IDA-Pro Flirt signatures will find the Set functions, in this example we will focus Set::MemberOf

Set MemberOf
Set MemberOf

arg_0 is the Set object and arg_4 is the byte we are checking to see if it’s set. When this code is called it looks like this:

Set MemberOf in use
Set MemberOf in use

and the byte_152FE location is an unknown mess like so:

Set data
Set data

as we know this data is a Set object, it would be nice if it was represented as such. Now we could Declare this a structure variable (Alt-Q) by hand and then rename it.

Set Declare a structure variable (Alt-Q)
Set Declare a structure variable (Alt-Q)

This works for a few small cases, but in the Gold Box games, Sets are used to manage lots of things so there are too many of them. The best trick here is to get IDA-Pro to do the work for us.

First you will need to have created a Set structure (needed for the above manual process) that is 0x20 bytes long.

set-structure
set-structure

Now go back to Set::MemberOf and Associate a prototype to a function (Y) and change the prototype from:

Set Associate a prototype to a function (Y)
Set Associate a prototype to a function (Y)
int __stdcall far Set__MemberOf(__int32 _set);

to:

int __stdcall far Set__MemberOf(Set set, char);

and ta-da the code call Set::MemberOf is tidy:

set-member-of-in-use-clean
set-member-of-in-use-clean

and all the Set data blocks are typed for us also:

set-data-clean
set-data-clean

Magic!