hc
2023-11-20 2e7bd41e4e8ab3d1efdabd9e263a2f7fe79bff8c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
From 0f61f76454a9420f158f626cb09a4fbc08c3709e Mon Sep 17 00:00:00 2001
From: Stafford Horne <shorne@gmail.com>
Date: Sun, 2 May 2021 06:02:16 +0900
Subject: [PATCH] or1k: Avoid R_OR1K_GOT16 overflow failures in presence
 of R_OR1K_GOT_AHI16
 
Now that we support R_OR1K_GOT_AHI16 we can relax the R_OR1K_GOT16
overflow validation check if the section has R_OR1K_GOT_AHI16.
 
We cannot simple disable R_OR1K_GOT16 overflow validation as there will
still be binaries that will have only R_OR1K_GOT16.  The
R_OR1K_GOT_AHI16 relocation will only be added by GCC when building with
the option -mcmodel=large.
 
This assumes that R_OR1K_GOT_AHI16 will come before R_OR1K_GOT16, which
is the code pattern that will be emitted by GCC.
 
bfd/ChangeLog:
 
   PR 21464
   * elf32-or1k.c (or1k_elf_relocate_section): Relax R_OR1K_GOT16
   overflow check if we have R_OR1K_GOT_AHI16 followed by
   R_OR1K_GOT16.
 
Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
---
 bfd/elf32-or1k.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
 
diff --git a/bfd/elf32-or1k.c b/bfd/elf32-or1k.c
index a4a64f73b7c..07fff3602a3 100644
--- a/bfd/elf32-or1k.c
+++ b/bfd/elf32-or1k.c
@@ -1248,6 +1248,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
   asection *sgot, *splt;
   bfd_vma plt_base, got_base, got_sym_value;
   bfd_boolean ret_val = TRUE;
+  bfd_boolean saw_gotha = FALSE;
 
   if (htab == NULL)
     return FALSE;
@@ -1456,6 +1457,16 @@ or1k_elf_relocate_section (bfd *output_bfd,
         || r_type == R_OR1K_GOT_AHI16)
           relocation -= got_sym_value;
 
+        if (r_type == R_OR1K_GOT_AHI16)
+          saw_gotha = TRUE;
+
+        /* If we have a R_OR1K_GOT16 followed by a R_OR1K_GOT_AHI16
+           relocation we assume the code is doing the right thing to avoid
+           overflows.  Here we mask the lower 16-bit of the relocation to
+           avoid overflow validation failures.  */
+        if (r_type == R_OR1K_GOT16 && saw_gotha)
+          relocation &= 0xffff;
+
       /* Addend should be zero.  */
       if (rel->r_addend != 0)
         {
-- 
2.25.1