blob: 2a5f8628d9ba8e7af28370c23b6ed8eafe678cb3 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001From 59d4b8c48e5040af7e02b34eb26ea602ec82a38e Mon Sep 17 00:00:00 2001
2From: James Cowgill <james410@cowgill.org.uk>
3Date: Mon, 5 Jan 2015 15:17:02 +0000
4Subject: [PATCH 3/3] Add mips n64 relocation format hack
5
6MIPSEL N64 ELF files use a slightly different format for storing relocation
7entries which is incompatible with the normal R_SYM / R_INFO macros.
8To workaround this, we rearrange the bytes in the relocation's r_info field
9when reading and writing the relocations.
10
11This patch also ensures that strip.c sets the correct value of e_machine
12before manipulating relocations so that these changes take effect.
13
14Signed-off-by: James Cowgill <james410@cowgill.org.uk>
15
16Upstream-Status: Backport [from debian]
17Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
18---
19 libelf/gelf_getrel.c | 25 +++++++++++++++++++++++--
20 libelf/gelf_getrela.c | 25 +++++++++++++++++++++++--
21 libelf/gelf_update_rel.c | 20 +++++++++++++++++++-
22 libelf/gelf_update_rela.c | 20 +++++++++++++++++++-
23 src/strip.c | 17 +++++++++++++++++
24 5 files changed, 101 insertions(+), 6 deletions(-)
25
26Index: b/libelf/gelf_getrel.c
27===================================================================
28--- a/libelf/gelf_getrel.c
29+++ b/libelf/gelf_getrel.c
30@@ -36,6 +36,7 @@
31
32 #include "libelfP.h"
33
34+#define EF_MIPS_ABI 0x0000F000
35
36 GElf_Rel *
37 gelf_getrel (Elf_Data *data, int ndx, GElf_Rel *dst)
38@@ -89,8 +90,28 @@ gelf_getrel (Elf_Data *data, int ndx, GE
39 result = NULL;
40 }
41 else
42- result = memcpy (dst, &((Elf64_Rel *) data_scn->d.d_buf)[ndx],
43- sizeof (Elf64_Rel));
44+ {
45+ GElf_Ehdr hdr;
46+ result = memcpy (dst, &((Elf64_Rel *) data_scn->d.d_buf)[ndx],
47+ sizeof (Elf64_Rel));
48+
49+ if (gelf_getehdr(scn->elf, &hdr) != NULL &&
50+ hdr.e_ident[EI_DATA] == ELFDATA2LSB &&
51+ hdr.e_machine == EM_MIPS &&
52+ (hdr.e_flags & EF_MIPS_ABI) == 0)
53+ {
54+ /*
55+ * The relocation format is mangled on MIPSEL N64
56+ * We'll adjust it so at least R_SYM will work on it
57+ */
58+ GElf_Xword r_info = dst->r_info;
59+ dst->r_info = (r_info << 32) |
60+ ((r_info >> 8) & 0xFF000000) |
61+ ((r_info >> 24) & 0x00FF0000) |
62+ ((r_info >> 40) & 0x0000FF00) |
63+ ((r_info >> 56) & 0x000000FF);
64+ }
65+ }
66 }
67
68 rwlock_unlock (scn->elf->lock);
69Index: b/libelf/gelf_getrela.c
70===================================================================
71--- a/libelf/gelf_getrela.c
72+++ b/libelf/gelf_getrela.c
73@@ -36,6 +36,7 @@
74
75 #include "libelfP.h"
76
77+#define EF_MIPS_ABI 0x0000F000
78
79 GElf_Rela *
80 gelf_getrela (Elf_Data *data, int ndx, GElf_Rela *dst)
81@@ -90,8 +91,28 @@ gelf_getrela (Elf_Data *data, int ndx, G
82 result = NULL;
83 }
84 else
85- result = memcpy (dst, &((Elf64_Rela *) data_scn->d.d_buf)[ndx],
86- sizeof (Elf64_Rela));
87+ {
88+ GElf_Ehdr hdr;
89+ result = memcpy (dst, &((Elf64_Rela *) data_scn->d.d_buf)[ndx],
90+ sizeof (Elf64_Rela));
91+
92+ if (gelf_getehdr(scn->elf, &hdr) != NULL &&
93+ hdr.e_ident[EI_DATA] == ELFDATA2LSB &&
94+ hdr.e_machine == EM_MIPS &&
95+ (hdr.e_flags & EF_MIPS_ABI) == 0)
96+ {
97+ /*
98+ * The relocation format is mangled on MIPSEL N64
99+ * We'll adjust it so at least R_SYM will work on it
100+ */
101+ GElf_Xword r_info = dst->r_info;
102+ dst->r_info = (r_info << 32) |
103+ ((r_info >> 8) & 0xFF000000) |
104+ ((r_info >> 24) & 0x00FF0000) |
105+ ((r_info >> 40) & 0x0000FF00) |
106+ ((r_info >> 56) & 0x000000FF);
107+ }
108+ }
109 }
110
111 rwlock_unlock (scn->elf->lock);
112Index: b/libelf/gelf_update_rel.c
113===================================================================
114--- a/libelf/gelf_update_rel.c
115+++ b/libelf/gelf_update_rel.c
116@@ -36,6 +36,7 @@
117
118 #include "libelfP.h"
119
120+#define EF_MIPS_ABI 0x0000F000
121
122 int
123 gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
124@@ -86,6 +87,9 @@ gelf_update_rel (Elf_Data *dst, int ndx,
125 }
126 else
127 {
128+ GElf_Ehdr hdr;
129+ GElf_Rel value = *src;
130+
131 /* Check whether we have to resize the data buffer. */
132 if (INVALID_NDX (ndx, Elf64_Rel, &data_scn->d))
133 {
134@@ -93,7 +97,21 @@ gelf_update_rel (Elf_Data *dst, int ndx,
135 goto out;
136 }
137
138- ((Elf64_Rel *) data_scn->d.d_buf)[ndx] = *src;
139+ if (gelf_getehdr(scn->elf, &hdr) != NULL &&
140+ hdr.e_ident[EI_DATA] == ELFDATA2LSB &&
141+ hdr.e_machine == EM_MIPS &&
142+ (hdr.e_flags & EF_MIPS_ABI) == 0)
143+ {
144+ /* Undo the MIPSEL N64 hack from gelf_getrel */
145+ GElf_Xword r_info = value.r_info;
146+ value.r_info = (r_info >> 32) |
147+ ((r_info << 8) & 0x000000FF00000000) |
148+ ((r_info << 24) & 0x0000FF0000000000) |
149+ ((r_info << 40) & 0x00FF000000000000) |
150+ ((r_info << 56) & 0xFF00000000000000);
151+ }
152+
153+ ((Elf64_Rel *) data_scn->d.d_buf)[ndx] = value;
154 }
155
156 result = 1;
157Index: b/libelf/gelf_update_rela.c
158===================================================================
159--- a/libelf/gelf_update_rela.c
160+++ b/libelf/gelf_update_rela.c
161@@ -36,6 +36,7 @@
162
163 #include "libelfP.h"
164
165+#define EF_MIPS_ABI 0x0000F000
166
167 int
168 gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
169@@ -89,6 +90,9 @@ gelf_update_rela (Elf_Data *dst, int ndx
170 }
171 else
172 {
173+ GElf_Ehdr hdr;
174+ GElf_Rela value = *src;
175+
176 /* Check whether we have to resize the data buffer. */
177 if (INVALID_NDX (ndx, Elf64_Rela, &data_scn->d))
178 {
179@@ -96,7 +100,21 @@ gelf_update_rela (Elf_Data *dst, int ndx
180 goto out;
181 }
182
183- ((Elf64_Rela *) data_scn->d.d_buf)[ndx] = *src;
184+ if (gelf_getehdr(scn->elf, &hdr) != NULL &&
185+ hdr.e_ident[EI_DATA] == ELFDATA2LSB &&
186+ hdr.e_machine == EM_MIPS &&
187+ (hdr.e_flags & EF_MIPS_ABI) == 0)
188+ {
189+ /* Undo the MIPSEL N64 hack from gelf_getrel */
190+ GElf_Xword r_info = value.r_info;
191+ value.r_info = (r_info >> 32) |
192+ ((r_info << 8) & 0x000000FF00000000) |
193+ ((r_info << 24) & 0x0000FF0000000000) |
194+ ((r_info << 40) & 0x00FF000000000000) |
195+ ((r_info << 56) & 0xFF00000000000000);
196+ }
197+
198+ ((Elf64_Rela *) data_scn->d.d_buf)[ndx] = value;
199 }
200
201 result = 1;
202Index: b/src/strip.c
203===================================================================
204--- a/src/strip.c
205+++ b/src/strip.c
206@@ -532,6 +532,23 @@ handle_elf (int fd, Elf *elf, const char
207 goto fail;
208 }
209
210+ /* Copy identity part of the ELF header now */
211+ newehdr = gelf_getehdr (newelf, &newehdr_mem);
212+ if (newehdr == NULL)
213+ INTERNAL_ERROR (fname);
214+
215+ memcpy (newehdr->e_ident, ehdr->e_ident, EI_NIDENT);
216+ newehdr->e_type = ehdr->e_type;
217+ newehdr->e_machine = ehdr->e_machine;
218+ newehdr->e_version = ehdr->e_version;
219+
220+ if (gelf_update_ehdr (newelf, newehdr) == 0)
221+ {
222+ error (0, 0, gettext ("%s: error while creating ELF header: %s"),
223+ fname, elf_errmsg (-1));
224+ return 1;
225+ }
226+
227 /* Copy over the old program header if needed. */
228 if (ehdr->e_type != ET_REL)
229 for (cnt = 0; cnt < phnum; ++cnt)