public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Easier way to generate stuff like asm-offsets.
@ 2009-11-21  0:56 Kaz Kylheku
  2009-11-21  4:43 ` Ben Pfaff
  2009-11-21  6:10 ` Michal Jaegermann
  0 siblings, 2 replies; 3+ messages in thread
From: Kaz Kylheku @ 2009-11-21  0:56 UTC (permalink / raw)
  To: linux-kernel

Hey all,

You can use declarations and the ``nm'' utility to discover sizes of
types, structure offsets and stuff like that.

There is no need to grep material from the assembly code itself.

I'm looking into this as a technique for to use in ./configure scripts
when a program is being cross-compiled.

Then I remembered that the Linux kernel does something to pull out
structure member offsets
for use in assembly code. How are they doing that, I wondered? I took
a look and saw that it
was done with a complicated hack.

Here is the simple trick:

  /* file test.c */

  #include <stddef.h>  /* for offsetof macro */

  struct foo { char padding[43]; int member; };

  char offsetof_foo_member[offsetof(struct foo, member)];

Now we compile test.c:

$ /path/to/toolchain/bin/arch-prefix-gcc test.c -c

Then we can use the ``nm'' tool (the toolchain should have binutils in it):

$ /path/to/toolchain/bin/arch-prefix-nm -t d -P foo.c   # decimal
output, posix format
offsetof_foo_member C 00000044 00000044

There we go, 44 bytes.  Simple.  This is almost an #include file;
we just have to do some simple filtering, such as:

$ /path/to/toolchain/bin/arch-prefix-nm -t d -P test.o | awk '{ print
"#define", $1, $4 }'
#define offsetof_foo_member 00000044

:)

Cheers ...

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Easier way to generate stuff like asm-offsets.
  2009-11-21  0:56 Easier way to generate stuff like asm-offsets Kaz Kylheku
@ 2009-11-21  4:43 ` Ben Pfaff
  2009-11-21  6:10 ` Michal Jaegermann
  1 sibling, 0 replies; 3+ messages in thread
From: Ben Pfaff @ 2009-11-21  4:43 UTC (permalink / raw)
  To: linux-kernel

Kaz Kylheku <kkylheku@gmail.com> writes:

> Then we can use the ``nm'' tool (the toolchain should have binutils in it):
>
> $ /path/to/toolchain/bin/arch-prefix-nm -t d -P foo.c   # decimal
> output, posix format
> offsetof_foo_member C 00000044 00000044

If those numbers are in decimal...

> There we go, 44 bytes.  Simple.  This is almost an #include file;
> we just have to do some simple filtering, such as:
>
> $ /path/to/toolchain/bin/arch-prefix-nm -t d -P test.o | awk '{ print
> "#define", $1, $4 }'
> #define offsetof_foo_member 00000044

...then the #define is wrong because it's interpreted as octal
there.

Cool trick!
-- 
Ben Pfaff 
http://benpfaff.org


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Easier way to generate stuff like asm-offsets.
  2009-11-21  0:56 Easier way to generate stuff like asm-offsets Kaz Kylheku
  2009-11-21  4:43 ` Ben Pfaff
@ 2009-11-21  6:10 ` Michal Jaegermann
  1 sibling, 0 replies; 3+ messages in thread
From: Michal Jaegermann @ 2009-11-21  6:10 UTC (permalink / raw)
  To: Kaz Kylheku; +Cc: linux-kernel

On Fri, Nov 20, 2009 at 04:56:14PM -0800, Kaz Kylheku wrote:
> 
> Here is the simple trick:
....
> 
> Now we compile test.c:
> 
> $ /path/to/toolchain/bin/arch-prefix-gcc test.c -c
> 
> Then we can use the ``nm'' tool (the toolchain should have binutils in it):
> 
> $ /path/to/toolchain/bin/arch-prefix-nm -t d -P foo.c   # decimal
> output, posix format
> offsetof_foo_member C 00000044 00000044
> 
> There we go, 44 bytes.  Simple.  This is almost an #include file;
> we just have to do some simple filtering, such as:
> 
> $ /path/to/toolchain/bin/arch-prefix-nm -t d -P test.o | awk '{ print "#define", $1, $4 }'
> #define offsetof_foo_member 00000044

If that is a "decimal output" then resulting define has a wrong format.
You do not want octal.  Fix that with
   ... | awk '{print "#define", $1, 0 + $4}'
to force a conversion of a string to a number.

   Michal

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-11-21  7:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-21  0:56 Easier way to generate stuff like asm-offsets Kaz Kylheku
2009-11-21  4:43 ` Ben Pfaff
2009-11-21  6:10 ` Michal Jaegermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox