Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asm variable in C code

Tags:

c

assembly

I am reading Secure Programming Cookbook for C and C++ from John Viega. There is a code snippet where I need some help to understand:

asm(".long 0xCEFAEDFE \n"
    "crc32_stored:    \n"
    ".long 0xFFFFFFFF \n"
    ".long 0xCEFAEDFE \n"
);

int main(){
    //crc32_stored used here as a variable
}

What do these lines exactly mean: "crc32_stored:\n", ".long 0xFFFFFFFF \n"? Is this a variable definition and initialization?

Trying to compile the code from the book I got the following error:

error: ‘crc32_stored’ undeclared (first use in this function)
like image 813
robert Avatar asked Sep 27 '22 08:09

robert


1 Answers

crc32_stored: is simply a label, which in assembler is just an alias for a memory address. Since the label itself does not take up any space in the object code the address represented by crc32_stored is the address of .long 0xFFFFFFFF which assembles to four FF-bytes. In the object code the label will show up as a symbol, which means pretty much the same thing (just an alias for an address).

In C, a variable is (in a way) yet another way to express the same thing: A name that refers to a certain address in memory, but it has additional type information, i.e. int or long. You can create a variable in C with int crc32_stored = 0xFFFFFFFF; which (minus the type information) is equivalent to assembly crc32_stored: .long 0xFFFFFFFF, but that will create a different alias to yet another address.

You can tell the C compiler to not reserve a new address for the name "crc32_stored" but to create only the alias part and then to couple it with the address of a symbol with the same name. That is done with a declaration using the "extern" storage-class specifier, as in extern int crc32_stored. By this you "promise" to later link against another object file that will have this symbol.

Obviously you have to take care yourself that the C type information matches the intention of the assembly code (i.e. there are 4 bytes at the given address that should be interpreted as a signed 32-bit integer).

Addendum: Without the extra declaration the symbol is not visible from C code, because the assembler parts are processed separately. The symbols can not be exported to C code automatically because the type information is missing. (An assembly label does not even include information about whether it points to data or code.)

like image 118
Jan Avatar answered Sep 30 '22 08:09

Jan