473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How To Write A Program To Convert Structure Member Offsets?

Hi,

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?
--
Randy Yates
Sony Ericsson Mobile Communications
Research Triangle Park, NC, USA
ra*********@sonyericsson.com, 919-472-1124
Nov 14 '05 #1
7 3406
>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?


Can you parse a structure definition to get the names of all the
members in it?

Write a program to write a program to write the assembly-language
include file.

The first program parses the structure definition and makes a table
of the member name and the offset (using offsetof()), along with
any necessary code.

The second program (which includes the C header file and the table)
you compile and run, and the compiler (same one you use for compiling
the stuff that uses the C header) fills in all the offsetof() stuff.
Knowing the offsets, it spits out an assembly-language include file
with the right stuff in it.

Gordon L. Burditt
Nov 14 '05 #2
go***********@burditt.org (Gordon Burditt) writes:
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?


Can you parse a structure definition to get the names of all the
members in it?

Write a program to write a program to write the assembly-language
include file.

The first program parses the structure definition and makes a table
of the member name and the offset (using offsetof()), along with
any necessary code.

The second program (which includes the C header file and the table)
you compile and run, and the compiler (same one you use for compiling
the stuff that uses the C header) fills in all the offsetof() stuff.
Knowing the offsets, it spits out an assembly-language include file
with the right stuff in it.


Yes, great idea Gordon. I actually thought of this but since the target
code is not native (i.e., it's cross-compiled) I didn't see an immediate
way to run it, but on second thought I think I can run it in the simulator.
(FYI, this is a CEVA Teak DSP.) Thanks for getting me through my mental
block!
--
% Randy Yates % "Bird, on the wing,
%% Fuquay-Varina, NC % goes floating by
%%% 919-577-9882 % but there's a teardrop in his eye..."
%%%% <ya***@ieee.org> % 'One Summer Dream', *Face The Music*, ELO
http://home.earthlink.net/~yatescr
Nov 14 '05 #3
# 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.

One possibility is don't use an explicit struct. Use #defines that expand into
the struct perhaps like

struct-defines.h:
#undef STRUCTanon
#undef STRUCTname
#undef FIELD
#undef FIELDwidth
#undef andFIELD

#define STRUCTanon(fields) struct {fields};
#define STRUCTname(name,fields) struct name {fields};
#define FIELD(type,name) type name;
#define FIELDwidth(name,width) int name: width;
#define andFIELD

list-struct.h
STRUCTname(List,
FIELD(struct List*,link)
andFIELD
FIELD(int,payload)
)

list.h
#include "struct-defines.h"
#include "list-struct.h"

You can create alternative defines in other header files. By including the
something-else-defines.h and then list-struct.h, you can have structure expand
into something quite different; perhaps a size of, or a function to serialise
or unserialise the struct; perhaps they become assembly statements.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
You face forward, or you face the possibility of shock and damage.
Nov 14 '05 #4
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
Nov 14 '05 #5
Chris Croughton wrote:
.... snip ...
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...


You can see an example of this sort of thing in the extension and
debugging provisions of my nmalloc package for DJGPP. The internal
structure of the memory arena is private, and can be freely altered
in the package. However a defined type contains the offsets of
various fields, and is returned as a structure by a function call
(the function and structure type are in the implementation
namespace). Using this the debuggery can adapt itself to the
actual malloc package. This makes the whole thing fairly
portable. See:

<http://cbfalconer.home.att.net/download/nmalloc.zip>

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Nov 14 '05 #6
In article <xx*************@usrts005.corpusers.net>,
Randy Yates <ra*********@sonyericsson.com> wrote:
Hi,

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?


Lots of printf statements? Recompile and run the program for each
architecture.
Nov 14 '05 #7
On Sat, 14 May 2005 16:22:41 GMT, CBFalconer
<cb********@yahoo.com> wrote:
Using this the debuggery


Freudian slip, typo or intentional? Just curious, I often refer to
"debuggering" programs that someone else has 'buggered' (i.e. put bugs
in through the back door -- without going through change control)...

Chris C
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: junky_fellow | last post by:
I am trying to find the offset of a member "mbr" inside a structure "str" as follows: offset = &(struct str *)0->mbr; But, on compilation I get the following error: cc: Error: m1.c, line 55:...
12
by: anonymous | last post by:
Hi folks, I am in a fix trying to copy data to an array which is member of a structure. What I am doing right now is: char array = {0,1,2,3,4,5,6,7}; memcpy(structure.array, array, 8); Is...
6
by: Ken | last post by:
When running a program in the debugger, what would cause it to crash without any error messages? I get "The program has exited with code 0 (0x0)". The program is a MDI app with threading for...
23
by: Eric J.Hu | last post by:
Hi, I have following code, want do pointer convert. It always complain: vcnvt.c: In function `main': vcnvt.c:20: warning: dereferencing `void *' pointer vcnvt.c:20: request for member `key'...
9
by: cman | last post by:
What are the mechanics involved in the calculation of an offset of a structure member, demonstrated in this piece of code? #define list_entry(ptr, type, member) \ ((type *)((char...
2
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
3
by: abhivg | last post by:
Hi, I am trying to port a 32 bit Unix application to 64 bit Windows. While compiling on Windows I am getting a number of warnings related to structure padding. More specifically "warning C4820:...
5
by: xmllmx | last post by:
Please forgive me for cross-posting. I've post this to microsoft.publoc.vc.mfc. But I can't get any response. Maybe only MFC- related topics are cared there. To begin with code: union XXX {...
14
by: deepak | last post by:
Hi Experts, I'm getting this compilation error while trying to access a member in structure. at what time we will get this error message? Thanks, Deepak
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.