libraw1394 struct layouts, i386 vs. x86_64
Ben Scott
dragonhawk at gmail.com
Wed Jan 7 16:10:38 EST 2009
On Wed, Jan 7, 2009 at 1:22 PM, Michael ODonnell
<michael.odonnell at comcast.net> wrote:
> ... it disagrees with the kernel module it's interfacing with about the layouts
> of various structs ...
> ... padding out some structs with extra dummy members ...
> ... b0rken hackery is not, of course, how I want to solve this ...
On Wed, Jan 7, 2009 at 3:14 PM, Michael ODonnell
<michael.odonnell at comcast.net> wrote:
> ... those pragma directives ... force tighter packing ...
This is one of the things Linux does very badly.
As I understand it, Mr. Torvalds feels an official binary
programming interface to the kernel would hamper kernel development.
So the kernel project doesn't attempt to provide one. I guess he
thinks userland programmers should develop their own a binary
interfaces by examining the kernel source, and also track kernel
versions and changes themselves. Needless to say, that is almost
always infeasible. As a result, most userland developers fall back on
coping the header files from the kernel source tree.
But copying C headers around like that is also a bad idea, for the
very reason you're encountering: The C standard provides no guarantees
about how struct's will be laid out in memory. The compiler can do
(and often does) whatever it want. As we see here, compilers often
align/pad struct's to optimize for the architecture they're targeting.
There's no good solution to this mess. I first encountered this
sort of problem when I was trying to get software that assumed "all
the world is 32-bit" to work properly on Linux on the 64-bit DEC
Alpha. That was back in 1997, so apparently improvement isn't
something we should hope for.
I'd say the least-bad solution is to patch the library. Use the
pragma discovered to tell the compiler to do as *little* padding as
possible. Then manually add padding, conditioned with ifdef's on
compiler symbols which inform as to word size of the target
architecture.
Moral being: If you're a programmer, do not use C struct's for
anything but internal-use-only purposes. Do not pass them to/from
external code. And *NEVER* write or read C struct's directly to
files.
-- Ben
More information about the gnhlug-discuss
mailing list