Skip to contentSkip to conversation

C structures and gas

DISCUSSION Corey Holcomb-Hockin 4 participants 4 messages Nov 12, 2002 – Nov 28, 2002

lore
Share & tools
lore
#0 lore raw

What is the best way to access C structures with gas assembly? I haven't gotten any good ideas from looking at assembly generated from C code. It looks like it keeps track of offsets and the size of structures on its own. I would have a hard time keeping track of offsets and changing them if the structure changed.

Corey Holcomb-Hockin

#1 lore raw
Quoted 5 lines · Corey Holcomb-Hockin

What is the best way to access C structures with gas assembly? I haven't gotten any good ideas from looking at assembly generated from C code. It looks like it keeps track of offsets and the size of structures on its own. I would have a hard time keeping track of offsets and changing them if the structure changed.

Yes. The way I did it, I use constants and "chain" them one after another. The more constants the better (replace even the numbers 0, 2, 4 below with constants). For example,

SIZEOF_SA = 16

SA = -SIZEOF_SA
SA_SIN_FAMILY = SA + 0
SA_SIN_PORT = SA_SIN_FAMILY + 2

# etc...
# always start the next variable declared on the stack based on
# the beginning of the previous one
SOCKET_ARGS = SA - 3*4
SOCKET_ARG0 = SOCKET_ARGS
SOCKET_ARG1 = SOCKET_ARG0 + 4
# etc..
# set STACK_TOP to the last variable
STACK_TOP = SOCKET_ARGS
# data here...

_start:
// setup stack...
pushl %ebp
movl %esp, %ebp
addl $STACK_TOP, %esp

# ...
#2 lore raw
In reply to Corey Holcomb-Hockin

Well, the best way to do it in Gas is to use HLA and have HLA generate the Gas code for you :-)
Seriously, though, Gas doesn't have much in the way of abstract data typing capabilities. If you want to move beyond the minimal features available in Gas or NASM, you're a prime candidate to learn assembly under HLA (the High Level Assembler). Not only does HLA support records (structures), unions, and classes, but it provides some very flexible options for controlling the alignment of objects within the structures so that your assembly structs can be made compatible with just about any compiler out there (C or otherwise).
You can check out HLA at http://webster.cs.ucr.edu
Randy Hyde
----- Original Message -----
From: "Corey Holcomb-Hockin" <[email protected]>
To: <[email protected]>
Sent: Monday, November 11, 2002 4:43 PM
Subject: C structures and gas

Quoted 12 lines

What is the best way to access C structures with gas assembly? I haven't gotten any good ideas from looking at assembly generated from C code. It looks like it keeps track of offsets and the size of structures on its own. I would have a hard time keeping track of offsets and changing them if the structure changed.

Corey Holcomb-Hockin

-
To unsubscribe from this list: send the line "unsubscribe linux-assembly" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
In reply to Corey Holcomb-Hockin
Quoted 8 lines · Corey Holcomb-Hockin

What is the best way to access C structures with gas assembly? I haven't gotten any good ideas from looking at assembly generated from C code. It looks like it keeps track of offsets and the size of structures on its own. I would have a hard time keeping track of offsets and changing them if the structure changed.

Corey Holcomb-Hockin

Have a look how linux does this in the ia64 tree. See /usr/src/linux/arch/ia64/tools/print_offset.c. There are three steps:

1. compile print_offsets
2. run print_offsets (creates offsets.h)
3. include offsets.h into *.S

Christian