32 bit FreeBSD compiled binary coredumps on 64 bit(amd) FreeBSD

Heiko Wundram (Beenic) wundram at beenic.net
Wed Feb 20 08:12:09 UTC 2008

Am Mittwoch, 20. Februar 2008 07:17:30 schrieb navneet Upadhyay:
>        I am compiling the binary on 32 bit FreeBSD and running it on 64
> (amd64)bit FreeBSD . FreeBSD says it is possible to do so.
>        But my application core dumps . I investigated the reason which is
> as follows :
> The problem is in the call retval = sysctl(mib, 4, &kp, &sz, NULL, 0);
> where sz is size of kp and where kp is a structure of type kinfo_proc. The
> size of this structure on 32bit system is 768 and on 64 bit system is 1088.
> The call works on 32 bit system but when run on 64 bit system it coredumps
> , because of the size mismatch of kinfo_proc .
> If i hardcode the sz to 1088 then it works on amd64 systems , how do i deal
> with it. I am anticipating lot of coredumps like that, what is a generic
> solution for such kinds of problems.

Without investigating further whether the structure up to byte 768 is 
different (wrt. structure member offsets, and thus wrt. to hardcoded 
constants in the binary file), which would be a real showstopper for the 
i386"-emulation" on amd64 (and thus I can't see it being that way), see the 
documentation of sysctl:

     Upon successful completion, the value 0 is returned; otherwise the
     value -1 is returned and the global variable errno is set to indicate the

     [ENOMEM]           The length pointed to by oldlenp is too short to hold
                        the requested value.

So, basically, you should check in the call whether sysctl returned -1 (with 
errno set to ENOMEM), and enlarge the buffer if so, until it doesn't 
return -1 anymore. This should handle i386 and amd64 transparently (if the 
offsets up to byte 768 are equal/similar).

In case you're trying to recompile the application on 64-bit, you should use 
sizeof() anyway to automatically adapt the initial buffer size reserved for 
the output buffer depending on the definition of the structure (which will 
also spare you pain if a FreeBSD upgrade changes the structure).

Heiko Wundram
Product & Application Development

More information about the freebsd-questions mailing list