Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc inline asm template for constant with out hash

I am trying to emit a global SYMBOL based on a #define VALUE. My attempt is as follows:

__asm__ (".globl SYMBOL");
__asm__ (".set SYMBOL, %0" :: "i" (VALUE));

What is emitted by gcc to the assembler is the following:

.globl SYMBOL
.set SYMBOL, #VALUE

How can I get rid of the hash in the .set before VALUE. FWIW, my target is ARM.

like image 411
KentH Avatar asked Nov 02 '25 03:11

KentH


2 Answers

Use the c operand modifier:

__asm__ (".set SYMBOL, %c0" : : "i" (VALUE));

Try on godbolt

gcc used to document this feature as supported only on x86, but it actually worked on ARM as well, and as of gcc 13, it is documented as supported on all targets.

(The other AArch64-specific modifiers, like %w0 to emit w11 in place of x11, are likewise supported a long way back, for compatibility with armclang. They are still not documented by gcc 13, but are documented in the trunk code, so presumably will be a fully documented feature for gcc 14.)

like image 100
Nate Eldredge Avatar answered Nov 04 '25 06:11

Nate Eldredge


You can use stringizing.

#define VALUE 89
#define xstr(s) str(s)
#define str(s) #s

__asm__ (".globl SYMBOL");
__asm__ (".set SYMBOL, " str(VALUE));

The 'VALUE' must conform to something that gas will take as working with set. They could be fixed addresses from some vendor documentation or a listing output that is parsed. If you want 'VALUE' use str(s), if you want '89' then use xstr(s). You did not describe the actual use case.

like image 33
artless noise Avatar answered Nov 04 '25 05:11

artless noise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!