Bug 12719 - strange behavior with immediate symbols when using inline assembly
Summary: strange behavior with immediate symbols when using inline assembly
Status: RESOLVED INVALID
Alias: None
Product: binutils
Classification: Unclassified
Component: gas (show other bugs)
Version: 2.21
: P2 minor
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-01 13:36 UTC by Theorizer
Modified: 2011-05-01 13:45 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
example sources (450 bytes, application/x-gzip)
2011-05-01 13:36 UTC, Theorizer
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Theorizer 2011-05-01 13:36:37 UTC
Created attachment 5699 [details]
example sources

With the gnu c-compiler, assuming the structure

typedef struct foo {
    int a;
    int b;
} foo;

and the define below

#define offsetof(type, member) __builtin_offsetof(type, member)

one can write the following inline assembly lines to define a symbol for offset of member "b" within the foo structure above:

asm(".global foo.b;"
    "foo.b = %0\n"
    :: "i" (offsetof(foo, b)));

The problem is that by default gcc generates the following (it assumes at&t syntax):
.global foo.b; foo.b = $4

This line is although compiled by "as" but the symbol foo.b won't be defined (instead only the $4 symbol is declared to be undefined (extern).

If you however apply the -masm=intel compiler switch the statements below will be generated as expected:
.global foo.b; foo.b = 4

by this workaround "as" will properly define the foo.b symbol and my test project (see in attachments) builds correctly.

To test it apply the following commands on the attached files:

gcc main.c add_one.S -o test

or

gcc -masm=intel main.c add_one.S -o test

in the first case it doesn't link, in the latter one it builds perfectly.
The test code is assumed to be built and run on x86-64 Linux and FreeBSD systems.

I tested the issue with binutils versions 2.15, 2.20, 2.21. All are the same in this regard.
Comment 1 H.J. Lu 2011-05-01 13:45:15 UTC
Inline asm constraints are for instructions.

asm(".global foo.b;"
    "foo.b = %0\n"
    :: "i" (offsetof(foo, b)));

isn't an instruction.  In any case, it isn't an assembler issue.