]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
LoongArch: Enforce 4-byte align for machine instructions insn-4-byte-align
authorzhaozhou <zhaozhou@loongson.cn>
Thu, 16 Apr 2026 09:31:41 +0000 (17:31 +0800)
committermengqinggang <mengqinggang@loongson.cn>
Mon, 27 Apr 2026 08:31:33 +0000 (16:31 +0800)
If no alignment is specified in the assembly file, LoongArch will not
perform forced alignment. When this object file (.o) is linked into an
executable, instructions may not be 4-byte aligned, which will
eventually cause instruction fetch errors.

For example, the above issue can occur when generating a shared object
file (.so) with the -nostdlib option.

gas/config/tc-loongarch.c
gas/testsuite/gas/loongarch/insn_align_4.d [new file with mode: 0644]
gas/testsuite/gas/loongarch/insn_align_4.s [new file with mode: 0644]

index 1eb35f2c75ed0a65122960a47f2a302c1112c8a0..d067318ce2403bff9548b5620bc7a2f661a3c764 100644 (file)
@@ -1432,6 +1432,11 @@ loongarch_assemble_INSNs (char *str, unsigned int expand_from_macro)
       if (*str == '\0')
        break;
 
+      /* LoongArch instructions require 4-byte alignment.  When emitting
+        instructions into any section, record the appropriate section
+        alignment.  */
+      record_alignment (now_seg, 2);
+
       struct loongarch_cl_insn the_one;
       memset (&the_one, 0, sizeof (the_one));
       the_one.name = str;
diff --git a/gas/testsuite/gas/loongarch/insn_align_4.d b/gas/testsuite/gas/loongarch/insn_align_4.d
new file mode 100644 (file)
index 0000000..31a5d35
--- /dev/null
@@ -0,0 +1,7 @@
+#as:
+#readelf: -S
+
+#...
+  \[ [0-9]+\] \.text             PROGBITS         [0-9a-f]{16}  [0-9a-f]{8}
+       [0-9a-f]{16}  [0-9a-f]{16}  AX       0     0     4
+#...
diff --git a/gas/testsuite/gas/loongarch/insn_align_4.s b/gas/testsuite/gas/loongarch/insn_align_4.s
new file mode 100644 (file)
index 0000000..7b4922c
--- /dev/null
@@ -0,0 +1,4 @@
+.type test, @function
+test:
+  jirl $ra, $ra, 0
+.size test, .-test