hc
2023-11-06 e3e12f52b214121840b44c91de5b3e5af5d3eb84
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
From 141dee7402871c7b3994838c3eddf64a92d67be7 Mon Sep 17 00:00:00 2001
From: Stafford Horne <shorne@gmail.com>
Date: Sun, 2 May 2021 06:02:15 +0900
Subject: [PATCH] or1k: Implement relocation R_OR1K_GOT_AHI16 for gotha()
 
The gotha() relocation mnemonic will be outputted by OpenRISC GCC when
using the -mcmodel=large option.  This relocation is used along with
got() to generate 32-bit GOT offsets.  This increases the previous GOT
offset limit from the previous 16-bit (64K) limit.
 
This is needed on large binaries where the GOT grows larger than 64k.
 
bfd/ChangeLog:
 
   PR 21464
   * bfd-in2.h: Add BFD_RELOC_OR1K_GOT_AHI16 relocation.
   * elf32-or1k.c (or1k_elf_howto_table, or1k_reloc_map): Likewise.
   (or1k_final_link_relocate, or1k_elf_relocate_section,
   or1k_elf_check_relocs): Likewise.
   * libbfd.h (bfd_reloc_code_real_names): Likewise.
   * reloc.c: Likewise.
 
cpu/ChangeLog:
 
   PR 21464
   * or1k.opc (or1k_imm16_relocs, parse_reloc): Define parse logic
   for gotha() relocation.
 
include/ChangeLog:
 
   PR 21464
   * elf/or1k.h (elf_or1k_reloc_type): Define R_OR1K_GOT_AHI16 number.
 
opcodes/ChangeLog:
 
   PR 21464
   * or1k-asm.c: Regenerate.
 
gas/ChangeLog:
 
   PR 21464
   * testsuite/gas/or1k/reloc-1.s: Add test for new relocation.
   * testsuite/gas/or1k/reloc-1.d: Add test result for new
   relocation.
 
Cc: Giulio Benetti <giulio.benetti@benettiengineering.com>
 
fixup reloc, add tests
 
Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
---
 bfd/bfd-in2.h                    |  1 +
 bfd/elf32-or1k.c                 | 21 ++++++++++++++++++++-
 bfd/libbfd.h                     |  1 +
 bfd/reloc.c                      |  2 ++
 cpu/or1k.opc                     |  7 ++++++-
 gas/testsuite/gas/or1k/reloc-1.d |  4 +++-
 gas/testsuite/gas/or1k/reloc-1.s |  4 ++++
 include/elf/or1k.h               |  1 +
 opcodes/or1k-asm.c               |  7 ++++++-
 9 files changed, 44 insertions(+), 4 deletions(-)
 
diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 7eff85b7eaa..e76b9e7a319 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -5048,6 +5048,7 @@ then it may be truncated to 8 bits.  */
   BFD_RELOC_OR1K_TLS_TPOFF,
   BFD_RELOC_OR1K_TLS_DTPOFF,
   BFD_RELOC_OR1K_TLS_DTPMOD,
+  BFD_RELOC_OR1K_GOT_AHI16,
 
 /* H8 elf Relocations.  */
   BFD_RELOC_H8_DIR16A8,
diff --git a/bfd/elf32-or1k.c b/bfd/elf32-or1k.c
index 4f82cc4c667..48461854a96 100644
--- a/bfd/elf32-or1k.c
+++ b/bfd/elf32-or1k.c
@@ -808,6 +808,20 @@ static reloc_howto_type or1k_elf_howto_table[] =
      0,            /* Source Mask.  */
      0x03ffffff,        /* Dest Mask.  */
      TRUE),            /* PC relative offset?  */
+
+  HOWTO (R_OR1K_GOT_AHI16,     /* type */
+    16,                    /* rightshift */
+    2,                     /* size (0 = byte, 1 = short, 2 = long) */
+    16,                    /* bitsize */
+    FALSE,                 /* pc_relative */
+    0,                     /* bitpos */
+    complain_overflow_signed, /* complain_on_overflow */
+    bfd_elf_generic_reloc, /* special_function */
+    "R_OR1K_GOT_AHI16",    /* name */
+    FALSE,                 /* partial_inplace */
+    0,                     /* src_mask */
+    0xffff,                /* dst_mask */
+    FALSE),                /* pcrel_offset */
 };
 
 /* Map BFD reloc types to Or1k ELF reloc types.  */
@@ -871,6 +885,7 @@ static const struct or1k_reloc_map or1k_reloc_map[] =
   { BFD_RELOC_OR1K_TLS_IE_LO13,    R_OR1K_TLS_IE_LO13 },
   { BFD_RELOC_OR1K_SLO13,    R_OR1K_SLO13 },
   { BFD_RELOC_OR1K_PLTA26,    R_OR1K_PLTA26 },
+  { BFD_RELOC_OR1K_GOT_AHI16,    R_OR1K_GOT_AHI16 },
 };
 
 /* tls_type is a mask used to track how each symbol is accessed,
@@ -1111,6 +1126,7 @@ or1k_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
   switch (howto->type)
     {
     case R_OR1K_AHI16:
+    case R_OR1K_GOT_AHI16:
     case R_OR1K_GOTOFF_AHI16:
     case R_OR1K_TLS_IE_AHI16:
     case R_OR1K_TLS_LE_AHI16:
@@ -1373,6 +1389,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
         }
       break;
 
+    case R_OR1K_GOT_AHI16:
     case R_OR1K_GOT16:
     case R_OR1K_GOT_PG21:
     case R_OR1K_GOT_LO13:
@@ -1464,7 +1481,8 @@ or1k_elf_relocate_section (bfd *output_bfd,
         /* The GOT_PG21 and GOT_LO13 relocs are pc-relative,
            while the GOT16 reloc is GOT relative.  */
         relocation = got_base + off;
-        if (r_type == R_OR1K_GOT16)
+        if (r_type == R_OR1K_GOT16
+        || r_type == R_OR1K_GOT_AHI16)
           relocation -= got_sym_value;
 
       /* Addend should be zero.  */
@@ -1990,6 +2008,7 @@ or1k_elf_check_relocs (bfd *abfd,
         }
       break;
 
+    case R_OR1K_GOT_AHI16:
     case R_OR1K_GOT16:
     case R_OR1K_GOT_PG21:
     case R_OR1K_GOT_LO13:
diff --git a/bfd/libbfd.h b/bfd/libbfd.h
index 7271a2ad5a1..e0ee2dafc07 100644
--- a/bfd/libbfd.h
+++ b/bfd/libbfd.h
@@ -2757,6 +2757,7 @@ static const char *const bfd_reloc_code_real_names[] = { "@@uninitialized@@",
   "BFD_RELOC_OR1K_TLS_TPOFF",
   "BFD_RELOC_OR1K_TLS_DTPOFF",
   "BFD_RELOC_OR1K_TLS_DTPMOD",
+  "BFD_RELOC_OR1K_GOT_AHI16",
   "BFD_RELOC_H8_DIR16A8",
   "BFD_RELOC_H8_DIR16R8",
   "BFD_RELOC_H8_DIR24A8",
diff --git a/bfd/reloc.c b/bfd/reloc.c
index 4f4b95a0b7f..fe94e0d8f91 100644
--- a/bfd/reloc.c
+++ b/bfd/reloc.c
@@ -6204,6 +6204,8 @@ ENUMX
   BFD_RELOC_OR1K_GOTPC_HI16
 ENUMX
   BFD_RELOC_OR1K_GOTPC_LO16
+ENUMX
+  BFD_RELOC_OR1K_GOT_AHI16
 ENUMX
   BFD_RELOC_OR1K_GOT16
 ENUMX
diff --git a/cpu/or1k.opc b/cpu/or1k.opc
index f0adcbb00a5..5d20a1f33a7 100644
--- a/cpu/or1k.opc
+++ b/cpu/or1k.opc
@@ -193,7 +193,7 @@ static const bfd_reloc_code_real_type or1k_imm16_relocs[][6] = {
     BFD_RELOC_OR1K_GOT_LO13,
     BFD_RELOC_UNUSED,
     BFD_RELOC_UNUSED,
-    BFD_RELOC_UNUSED },
+    BFD_RELOC_OR1K_GOT_AHI16 },
   { BFD_RELOC_OR1K_GOTPC_LO16,
     BFD_RELOC_UNUSED,
     BFD_RELOC_UNUSED,
@@ -296,6 +296,11 @@ parse_reloc (const char **strp)
     str += 5;
     cls = RCLASS_TPOFF;
       }
+    else if (strncasecmp (str, "got", 3) == 0)
+      {
+    str += 3;
+    cls = RCLASS_GOT;
+      }
 
     if (strncasecmp (str, "hi(", 3) == 0)
       {
diff --git a/gas/testsuite/gas/or1k/reloc-1.d b/gas/testsuite/gas/or1k/reloc-1.d
index d1bcf5608bb..3a001c4ed99 100644
--- a/gas/testsuite/gas/or1k/reloc-1.d
+++ b/gas/testsuite/gas/or1k/reloc-1.d
@@ -68,5 +68,7 @@ OFFSET   TYPE              VALUE
 000000ec R_OR1K_LO13       x
 000000f0 R_OR1K_GOT_LO13   x
 000000f4 R_OR1K_SLO13      x
-
+000000f8 R_OR1K_GOT_AHI16  x
+000000fc R_OR1K_GOT_AHI16  x
+00000100 R_OR1K_GOT_AHI16  x
 
diff --git a/gas/testsuite/gas/or1k/reloc-1.s b/gas/testsuite/gas/or1k/reloc-1.s
index e76abef6532..562609aa869 100644
--- a/gas/testsuite/gas/or1k/reloc-1.s
+++ b/gas/testsuite/gas/or1k/reloc-1.s
@@ -74,3 +74,7 @@
     l.lbz    r5,po(x)(r3)
     l.lbz    r5,gotpo(x)(r3)
     l.sb    po(x)(r3),r6
+
+    l.movhi    r4,gotha(x)
+    l.ori    r3,r4,gotha(x)
+    l.addi    r3,r4,gotha(x)
diff --git a/include/elf/or1k.h b/include/elf/or1k.h
index 7f281481061..69ab90584a2 100644
--- a/include/elf/or1k.h
+++ b/include/elf/or1k.h
@@ -77,6 +77,7 @@ START_RELOC_NUMBERS (elf_or1k_reloc_type)
   RELOC_NUMBER (R_OR1K_TLS_IE_LO13,   51)
   RELOC_NUMBER (R_OR1K_SLO13,         52)
   RELOC_NUMBER (R_OR1K_PLTA26,        53)
+  RELOC_NUMBER (R_OR1K_GOT_AHI16,     54)
 END_RELOC_NUMBERS (R_OR1K_max)
 
 #define EF_OR1K_NODELAY (1UL << 0)
diff --git a/opcodes/or1k-asm.c b/opcodes/or1k-asm.c
index 045ab6230b6..fdccf67f9e1 100644
--- a/opcodes/or1k-asm.c
+++ b/opcodes/or1k-asm.c
@@ -177,7 +177,7 @@ static const bfd_reloc_code_real_type or1k_imm16_relocs[][6] = {
     BFD_RELOC_OR1K_GOT_LO13,
     BFD_RELOC_UNUSED,
     BFD_RELOC_UNUSED,
-    BFD_RELOC_UNUSED },
+    BFD_RELOC_OR1K_GOT_AHI16 },
   { BFD_RELOC_OR1K_GOTPC_LO16,
     BFD_RELOC_UNUSED,
     BFD_RELOC_UNUSED,
@@ -280,6 +280,11 @@ parse_reloc (const char **strp)
     str += 5;
     cls = RCLASS_TPOFF;
       }
+    else if (strncasecmp (str, "got", 3) == 0)
+      {
+    str += 3;
+    cls = RCLASS_GOT;
+      }
 
     if (strncasecmp (str, "hi(", 3) == 0)
       {
-- 
2.25.1