On 13 May 2005 18:16:25 -0400, Randy Yates
<ra*********@sonyericsson.com> wrote:
I work in an embedded environment in which we often use a mix of C and
assembly code. Thus a recurring requirement is to be able to take a C
header file with structure definitions as input and create an assembly
include file with the structure member offsets defined.
The problem is that the structure offsets are [potentially] a
complicated function of other syntactic elements such as other
structures (I could have a structure as a member of another
structure), #defines (e.g., for array sizes), etc. Does anyone know of
a good way or good program to determine these offsets?
Your compiler <g>. Seriously, the only thing which knows about how the
structures are laid out in memory is your compiler. If you are lucky,
in debug mode it might actually put that information into the object
file (so that a debugger can allow you to access fields in the
structure); if you're even luckier the object file format might be
documented well enough to allow you to extract the information, and even
have a utility which will do so as text (objdump will do it for ELF
files, as used by gcc).
Otherwise:
You could write a program which parses the heaer files and writes source
to print offsetof for each field in the structure, then compile that
program and use the results. But you'd need to run that program on the
target...
Otherwise:
You could write a utility which does a parse (i.e. a chunk of a C
compiler) and builds the structures, then (using rules you give it, like
"int is 4 bytes aligned on a 2 byte boundary") produces the assembler.
I'd feed the headers through the preprocessor first so you get rid of
all the macros, includes and conditionals and don't have to process
those. At least one company I know uses that method (they have
different 'rules' for each compiler and target), but I'm not allowed to
name them and the code is proprietary. If I were between jobs I'd offer
to write it under contract, it's the sort of thing I've written before,
but unfortunately I don't have time right now...
Chris C