]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
or1k: Support long jump offsets with -mcmodel=large
authorStafford Horne <shorne@gmail.com>
Mon, 12 May 2025 20:47:21 +0000 (21:47 +0100)
committerStafford Horne <shorne@gmail.com>
Mon, 2 Jun 2025 10:05:06 +0000 (11:05 +0100)
The -mcmodel=large option was originally added to handle generation of
large binaries with large PLTs.  However, when compiling the Linux
kernel with allyesconfig the output binary is so large that the jump
instruction 26-bit immediate is not large enough to store the jump
offset to some symbols when linking.  Example error:

  relocation truncated to fit: R_OR1K_INSN_REL_26 against symbol `do_fpe_trap' defined in .text section in arch/openrisc/kernel/traps.o

We fix this by forcing jump offsets to registers when -mcmodel=large.

Note, to get the Linux kernel allyesconfig config to work with OpenRISC,
this patch is needed along with some other patches to the Linux hand
coded assembly bits.

gcc/ChangeLog:

* config/or1k/predicates.md (call_insn_operand): Add condition
to not allow symbol_ref operands with TARGET_CMODEL_LARGE.
* config/or1k/or1k.opt: Document new -mcmodel=large
implications.
* doc/invoke.texi: Likewise.

gcc/testsuite/ChangeLog:

* gcc.target/or1k/call-1.c: New test.
* gcc.target/or1k/got-1.c: New test.

gcc/config/or1k/or1k.opt
gcc/config/or1k/predicates.md
gcc/doc/invoke.texi
gcc/testsuite/gcc.target/or1k/call-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/or1k/got-1.c [new file with mode: 0644]

index 00c5560330067af129698ed86df6c293f5e175a7..d252de082041c531286e27fbea946e1d20e75d39 100644 (file)
@@ -69,8 +69,8 @@ are used to perform unordered floating point compare and set flag operations.
 mcmodel=
 Target RejectNegative Joined Enum(or1k_cmodel_type) Var(or1k_code_model) Init(CMODEL_SMALL)
 Specify the code model used for accessing memory addresses.  Specifying large
-enables generating binaries with large global offset tables.  By default the
-value is small.
+enables generating binaries with large global offset tables and calling
+functions anywhere in an executable.  By default the value is small.
 
 Enum
 Name(or1k_cmodel_type) Type(enum or1k_cmodel_type)
index 11bb5181436f5a8f5d6c6d1f3b484b1a1f942c22..144f4d7b57789a0d358d3c43bea1dc06081c3986 100644 (file)
@@ -61,7 +61,8 @@
         (match_test "TARGET_ROR"))))
 
 (define_predicate "call_insn_operand"
-  (ior (match_code "symbol_ref")
+  (ior (and (match_code "symbol_ref")
+           (match_test "!TARGET_CMODEL_LARGE"))
        (match_operand 0 "register_operand")))
 
 (define_predicate "high_operand"
index 0150ad088798090184757b01cb6d6983a3719402..8de00854a05e791c5aa2090df9b2a5c106e49f19 100644 (file)
@@ -30911,12 +30911,13 @@ to store the immediate to a register first.
 @opindex mcmodel=
 @opindex mcmodel=small
 @item -mcmodel=small
-Generate OpenRISC code for the small model: The GOT is limited to 64k. This is
-the default model.
+Generate OpenRISC code for the small model: The GOT is limited to 64k and
+function call jumps are limited to 64M offsets. This is the default model.
 
 @opindex mcmodel=large
 @item -mcmodel=large
-Generate OpenRISC code for the large model: The GOT may grow up to 4G in size.
+Generate OpenRISC code for the large model: The GOT may grow up to 4G in size
+and function call jumps can target the full 4G address space.
 
 
 @end table
diff --git a/gcc/testsuite/gcc.target/or1k/call-1.c b/gcc/testsuite/gcc.target/or1k/call-1.c
new file mode 100644 (file)
index 0000000..593e402
--- /dev/null
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mcmodel=large" } */
+
+/* Generate local and global function calls. */
+
+extern int geti (void);
+
+__attribute__ ((noinline)) int
+calc (int a, int b)
+{
+  return a * b + 255;
+}
+
+int
+main (void)
+{
+  return geti () + calc (3, 4);
+}
+
+/* Ensure the 2 calls use register not immediate jumps.  */
+/* { dg-final { scan-assembler-times "l.movhi\\s+" 2 } } */
+/* { dg-final { scan-assembler-times "l.jalr\\s+" 2 } } */
diff --git a/gcc/testsuite/gcc.target/or1k/got-1.c b/gcc/testsuite/gcc.target/or1k/got-1.c
new file mode 100644 (file)
index 0000000..5357096
--- /dev/null
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fPIC -mcmodel=large" } */
+
+/* Generate references to the GOT. */
+
+extern int geti (void);
+extern int j;
+
+int
+calc (int a)
+{
+  return a * j + geti ();
+}
+
+/* Ensure the 2 references use gotha relocations and that the function call does
+   not use an immediate jump instruction.  */
+/* { dg-final { scan-assembler-times "gotha" 2 } } */
+/* { dg-final { scan-assembler "l.jalr\\s+" } } */