From 7f3102de2d4cdff3dcaf1201d3ac37965edcb3c7 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Sat, 30 Nov 2024 17:25:20 +0100 Subject: [PATCH 1/2] Produce correct x86 assembly code for identifiers starting with `$` They must be parenthesized in some contexts to avoid being confused for immediate operands. Fixes: #540 --- test | 2 +- x86/TargetPrinter.ml | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/test b/test index bbfd6c8f3d..154895fe43 160000 --- a/test +++ b/test @@ -1 +1 @@ -Subproject commit bbfd6c8f3de5d8e38988bd7f27af3a151e239bb4 +Subproject commit 154895fe439c0ca50b60a711c30464509cd72f7f diff --git a/x86/TargetPrinter.ml b/x86/TargetPrinter.ml index 86fa4d6d29..2755cb25fd 100644 --- a/x86/TargetPrinter.ml +++ b/x86/TargetPrinter.ml @@ -106,6 +106,7 @@ module type SYSTEM = val comment: string val raw_symbol: out_channel -> string -> unit val symbol: out_channel -> P.t -> unit + val symbol_paren: out_channel -> P.t -> unit val label: out_channel -> int -> unit val name_of_section: section_name -> string val stack_alignment: int @@ -130,6 +131,12 @@ module ELF_System : SYSTEM = let symbol = elf_symbol + let symbol_paren oc symb = + let s = extern_atom symb in + if String.length s > 0 && s.[0] = '$' + then fprintf oc "(%s)" s + else fprintf oc "%s" s + let label = elf_label let name_of_section = function @@ -164,8 +171,8 @@ module ELF_System : SYSTEM = let print_mov_rs oc rd id = if Archi.ptr64 - then fprintf oc " movq %a@GOTPCREL(%%rip), %a\n" symbol id ireg64 rd - else fprintf oc " movl $%a, %a\n" symbol id ireg32 rd + then fprintf oc " movq %a@GOTPCREL(%%rip), %a\n" symbol_paren id ireg64 rd + else fprintf oc " movl $%a, %a\n" symbol_paren id ireg32 rd let print_fun_info = elf_print_fun_info @@ -196,6 +203,9 @@ module MacOS_System : SYSTEM = let symbol oc symb = raw_symbol oc (extern_atom symb) + let symbol_paren = symbol + (* the leading '_' protects the leading '$' *) + let label oc lbl = fprintf oc "L%d" lbl @@ -262,6 +272,12 @@ module Cygwin_System : SYSTEM = let symbol oc symb = raw_symbol oc (extern_atom symb) + let symbol_paren oc symb = + let s = extern_atom symb in + if String.length s > 0 && s.[0] = '$' + then fprintf oc "(%a)" raw_symbol s + else raw_symbol oc s + let label oc lbl = fprintf oc "L%d" lbl @@ -341,13 +357,13 @@ module Target(System: SYSTEM):TARGET = (* RIP-relative addressing *) let ofs' = Z.to_int64 ofs in if ofs' = 0L - then fprintf oc "%a(%%rip)" symbol id + then fprintf oc "%a(%%rip)" symbol_paren id else fprintf oc "(%a + %Ld)(%%rip)" symbol id ofs' end else begin (* Absolute addressing *) let ofs' = Z.to_int32 ofs in if ofs' = 0l - then fprintf oc "%a" symbol id + then fprintf oc "%a" symbol_paren id else fprintf oc "(%a + %ld)" symbol id ofs' end end; @@ -707,7 +723,7 @@ module Target(System: SYSTEM):TARGET = | Pjmp_l(l) -> fprintf oc " jmp %a\n" label (transl_label l) | Pjmp_s(f, sg) -> - fprintf oc " jmp %a\n" symbol f + fprintf oc " jmp %a\n" symbol_paren f | Pjmp_r(r, sg) -> fprintf oc " jmp *%a\n" ireg r | Pjcc(c, l) -> @@ -733,7 +749,7 @@ module Target(System: SYSTEM):TARGET = fprintf oc " jmp *%a(, %a, 4)\n" label l ireg r end | Pcall_s(f, sg) -> - fprintf oc " call %a\n" symbol f; + fprintf oc " call %a\n" symbol_paren f; if (not Archi.ptr64) && sg.sig_cc.cc_structret then fprintf oc " pushl %%eax\n" | Pcall_r(r, sg) -> From cdfff5e3263b3e68065b71ea1ca814d92f26d1a1 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Sun, 1 Dec 2024 11:40:34 +0100 Subject: [PATCH 2/2] Produce correct ARM assembly code for identifiers starting with `$` They must be parenthesized in some contexts. --- arm/TargetPrinter.ml | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/arm/TargetPrinter.ml b/arm/TargetPrinter.ml index 9bbdde9d87..ea6430c729 100644 --- a/arm/TargetPrinter.ml +++ b/arm/TargetPrinter.ml @@ -105,7 +105,18 @@ struct let symbol = elf_symbol - let symbol_offset = elf_symbol_offset + let symbol_paren oc symb = + let s = extern_atom symb in + if String.length s > 0 && s.[0] = '$' + then fprintf oc "(%s)" s + else fprintf oc "%s" s + + let symbol_offset oc (symb, ofs) = + let ofs = camlint64_of_ptrofs ofs in + if ofs = 0L then + symbol_paren oc symb + else + fprintf oc "(%a + %Ld)" symbol symb ofs let ireg oc r = output_string oc (int_reg_name r) let freg oc r = output_string oc (float_reg_name r) @@ -227,11 +238,11 @@ struct | Pbne lbl -> fprintf oc " bne %a\n" print_label lbl | Pbsymb(id, sg) -> - fprintf oc " b %a\n" symbol id + fprintf oc " b %a\n" symbol_paren id | Pbreg(r, sg) -> fprintf oc " bx %a\n" ireg r | Pblsymb(id, sg) -> - fprintf oc " bl %a\n" symbol id + fprintf oc " bl %a\n" symbol_paren id | Pblreg(r, sg) -> fprintf oc " blx %a\n" ireg r | Pbic(r1, r2, so) ->