]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
i386: Quote user-defined symbols in assembly in Intel syntax
authorLIU Hao <lh_mouse@126.com>
Sat, 22 Feb 2025 05:11:51 +0000 (13:11 +0800)
committerJonathan Yong <10walls@gmail.com>
Sun, 25 May 2025 12:04:50 +0000 (12:04 +0000)
With `-masm=intel`, GCC generates registers without % prefixes. If a
user-declared symbol happens to match a register, it will confuse the
assembler. User-defined symbols should be quoted, so they are not to
be mistaken for registers or operators.

Support for quoted symbols were added in Binutils 2.26, originally
for ARM assembly, where registers are also unprefixed:
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d02603dc201f80cd9d2a1f4b1a16110b1e04222b

This change is required for `@SECREL32` to work in Intel syntax when
targeting Windows, where `@` is allowed as part of a symbol. GNU AS
fails to parse a plain symbol with that suffix:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80881#c79

gcc/ChangeLog:

PR target/53929
PR target/80881
* config/i386/i386-protos.h (ix86_asm_output_labelref): Declare new
function for quoting user-defined symbols in Intel syntax.
* config/i386/i386.cc (ix86_asm_output_labelref): Implement it.
* config/i386/i386.h (ASM_OUTPUT_LABELREF): Use it.
* config/i386/cygming.h (ASM_OUTPUT_LABELREF): Use it.

gcc/config/i386/cygming.h
gcc/config/i386/i386-protos.h
gcc/config/i386/i386.cc
gcc/config/i386/i386.h

index 743cc38f5852dc24bcfc895b55c97d94d324b04b..0a3173c4e937c62dd4d3a10b2f07b4b61faecc6b 100644 (file)
@@ -246,9 +246,10 @@ do {                                                       \
 #undef ASM_OUTPUT_LABELREF
 #define  ASM_OUTPUT_LABELREF(STREAM, NAME)     \
 do {                                           \
+  const char *prefix = "";                     \
   if ((NAME)[0] != FASTCALL_PREFIX)            \
-    fputs (user_label_prefix, (STREAM));       \
-  fputs ((NAME), (STREAM));                    \
+    prefix = user_label_prefix;                        \
+  ix86_asm_output_labelref ((STREAM), prefix, (NAME)); \
 } while (0)
 
 /* This does much the same in memory rather than to a stream.  */
index e85b925704ba3d2403d4e03db7ba34057212b1f6..10863ab9e9de40199ee969f81d655d4be7ccb2bc 100644 (file)
@@ -199,6 +199,7 @@ extern int ix86_attr_length_vex_default (rtx_insn *, bool, bool);
 extern rtx ix86_libcall_value (machine_mode);
 extern bool ix86_function_arg_regno_p (int);
 extern void ix86_asm_output_function_label (FILE *, const char *, tree);
+extern void ix86_asm_output_labelref (FILE *, const char *, const char *);
 extern void ix86_call_abi_override (const_tree);
 extern int ix86_reg_parm_stack_space (const_tree);
 
index 5cb66dadb43ed6c3e8803539716252a51967327d..1b7dbd425d69f20a2db57580b917521b52d45775 100644 (file)
@@ -1716,6 +1716,19 @@ ix86_asm_output_function_label (FILE *out_file, const char *fname,
     }
 }
 
+/* Output a user-defined label.  In AT&T syntax, registers are prefixed
+   with %, so labels require no punctuation.  In Intel syntax, registers
+   are unprefixed, so labels may clash with registers or other operators,
+   and require quoting.  */
+void
+ix86_asm_output_labelref (FILE *file, const char *prefix, const char *label)
+{
+  if (ASSEMBLER_DIALECT == ASM_ATT)
+    fprintf (file, "%s%s", prefix, label);
+  else
+    fprintf (file, "\"%s%s\"", prefix, label);
+}
+
 /* Implementation of call abi switching target hook. Specific to FNDECL
    the specific call register sets are set.  See also
    ix86_conditional_register_usage for more details.  */
index 5aa056ff553bc61c5844f13961c8654aa324217b..ccc62fc3e7ca53c86ec7e541ea6a42b5a7eab92a 100644 (file)
@@ -2267,6 +2267,13 @@ extern unsigned int const svr4_debugger_register_map[FIRST_PSEUDO_REGISTER];
   } while (0)
 #endif
 
+/* In Intel syntax, we have to quote user-defined labels that would
+   match (unprefixed) registers or operators.  */
+
+#undef ASM_OUTPUT_LABELREF
+#define ASM_OUTPUT_LABELREF(STREAM, NAME)      \
+  ix86_asm_output_labelref ((STREAM), user_label_prefix, (NAME))
+
 /* Under some conditions we need jump tables in the text section,
    because the assembler cannot handle label differences between
    sections.  */