Page 1 of 1

alignment in structs

Posted: Sun Aug 17, 2003 11:00 pm
by Adek336
hello i use g++ and i´m facing the following problem: a struct with a short int and a long int has the size of 8 bytes, so i have to divide the long int into two short ints. If I don´t, the struct won´t work very well as a GDTR. Is it possible to disable this allignment?

Cheers,
Adrian

RE:alignment in structs

Posted: Sun Aug 17, 2003 11:00 pm
by mikeleany
Use the packed attribute, like this:

  struct gdtr
  {
    short Limit;
    long Base;
  } attribute ((packed));

If you want you can replace "attribute" and/or "packed" with "__attribute__" and/or "__packed__".

This prevents the compiler from padding the structure and forces Base to immediately follow Limit, so it will have a size of 6 bytes. This method is gcc specific, so it probably won't work on other compilers.

RE:alignment in structs

Posted: Sun Aug 17, 2003 11:00 pm
by Adek336
Thank you! now it is 6 bytes long. But in meantime I got myself another stupid problem:
1) I need to access void foo_bar() from g++ in gas
2) but g++ gives it the name ¨ZN4_foo_barv¨
3) how can I make g++ to name it JUST foo_bar?

Thanx in advance,
Adrian.

RE:alignment in structs

Posted: Sun Aug 17, 2003 11:00 pm
by mikeleany
Declare your function like this:

void foo_bar() asm ("foo_bar");

This forces your function's assembly name to be exactly whatever you put in the asm statement.

You know, you should check out "C extensions" and "C++ extensions" in the gcc documentation. It will answer a lot of questions like these ones. Especially the sections that have to do with attributes and assembly. But there's a lot of other good stuff in there too.

RE:alignment in structs

Posted: Sun Aug 17, 2003 11:00 pm
by carbonBased
or define it as extern c...

extern "C" {
  void foo_far(void) { }
}

I think that'll work anyway...

Cheers,
Jeff

RE:alignment in structs

Posted: Mon Aug 18, 2003 11:00 pm
by mikeleany
Oh, I guess that's the normal way, isn't it? You don't need the outside set of curly braces if it's only one function though.

RE:alignment in structs

Posted: Mon Aug 25, 2003 11:00 pm
by Dragon88
That may or may not work. If he's calling the function from within his C++ code then that will work fine, but if he's calling from ASM code, then he would need to remember that gcc always puts a '_' in front of C functions.

Cheers,
Rambo (yeah, that's my real name)

RE:alignment in structs

Posted: Tue Aug 26, 2003 11:00 pm
by mikeleany
Be careful using the word "always". gcc sometimes puts the underscore in front of C functions, but when I compile my kernel, it never does. If I remember right, the reason it doesn't use the underscore has something to do with the ELF output format, but I could be wrong.

RE:alignment in structs

Posted: Wed Aug 27, 2003 11:00 pm
by carbonBased
Exactly right.

The ELF object/binary specification states that C functions are not prefixed with underscores.  DOS executables, however (such as those produced by "GCC for DOS" (aka DJGPP)) are prefixed with underscores.

Not sure about Windows PE executables; I would suspect they also no longer use underscores.

Cheers,
Jeff

RE: Underscoring

Posted: Wed Aug 27, 2003 11:00 pm
by Stefan
AFAIK, ELF is the only format that says DO NOT USE UNDERSCORES, it is really up to the compiler most of the time whether to use underscores or not. Back when I was stranded on a Wyndoze machine, all of my windows object files from LCC-WIN32 and Borland C++ had underscores. Microsoft system DLLs do not use underscores, they use import libraries to put underscores on the imports... It doesn't even have to be leading underscores, although that is the norm on Intel assembly systems.