6

Normally I struct is a fixed size. Is it possible to define a structure which contains an element which has different sizes?

To illustrate what I mean is this.

The layout of the data in the file looks like this:

ID          WORD 0
FunctionPtr DWORD OFFSET Fkt
Name        db 'Name of the function',0
align 4

'Name' is now a C-String which is as long until it reaches the 0-byte at the end and then follows the align instruction. So is it possible to tell IDA that the structure is including the string, no matter how long it is?

2
  • Are you sure that that's how it's actually implemented in the code? At least in c this would be the place where you either have a static array of some fixed size or a pointer to the beginning of the string which you have malloced space for. If that isn't the case the struct (once again thinking c, sorry) would have to have different names for all the different sizes or be some kind of union.
    – lfxgroove
    Commented Nov 18, 2013 at 19:27
  • Yes, I'm sure. There is a counter value at the head, which tells how many entries there are, and the follows an array of such structures.
    – Devolus
    Commented Nov 19, 2013 at 9:07

2 Answers 2

4

Unless the string is at the end of the structure, it doesn't make sense to try and make this struct in IDA, because it probably doesn't even exist in C (or whatever the original language was).

Now if the string is at the end of the struct, this might correlate to an actual C struct definition. This is called a "zero-length" array, or "flexible array member". Of course, the size of this array is zero, as far as sizeof is concerned.

Unfortunately, this still doesn't exist in IDA. What I've done in the past (again, where the string is last) is go ahead and create a one-byte field, so you at least know where the string starts. This will complicate things if you try to use size mystruct for any constants in the code, but at least your string offset will be correct.

1

IDA introduced "Variable Length Structures" and describe the usage of it with a very straight forward tutorial.

In short, you need to make sure the "flexible" item is the last item of the struct, and define it as an array of size 0. To do this, define a new struct member char Name, click on it and press *. Then, set the "Array size" to 0.

Now, the shortcoming is that IDA won't look for a null terminator to define the string, but instead, you'd have to tell it its size every time you want to use the structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.