blob: 3817234b9ebc02187fcac7a6c480c4e203d23fce [file] [log] [blame]
Brad Bishop286d45c2018-10-02 15:21:57 -04001From 5bf68bc39976903929f730b6eed18686c3563c05 Mon Sep 17 00:00:00 2001
2From: Nagaraju Mekala <nagaraju.mekala@xilinx.com>
3Date: Mon, 28 Aug 2017 19:53:56 -0700
4Subject: [PATCH] Fixup MicroBlaze debug_loc sections after linker relaxation
5
6Fixup debug_loc sections after linker relaxation Adds a new reloctype
7R_MICROBLAZE_32_NONE, used for passing reloc info from the assembler to
8the linker when the linker manages to fully resolve a local symbol
9reference.
10
11This is a workaround for design flaws in the assembler to
12linker interface with regards to linker relaxation.
13
1403/2018
15Rebased for binutils 2.30
16
17Signed-off-by: Edgar E. Iglesias <edgar.iglesias@gmail.com>
18Signed-off-by: Nagaraju Mekala <nmekala@xilinx.com>
19Signed-off-by: Manjukumar Matha <manjukumar.harthikote-matha@xilinx.com>
20Signed-off-by: Alejandro Enedino Hernandez Samaniego <alejandr@xilinx.com>
21
22Upstream-Status: Pending
23
24---
25 bfd/bfd-in2.h | 9 +++++++--
26 bfd/elf32-microblaze.c | 45 ++++++++++++++++++++++++++++++++++++++-------
27 bfd/libbfd.h | 1 +
28 bfd/reloc.c | 6 ++++++
29 binutils/readelf.c | 4 ++++
30 gas/config/tc-microblaze.c | 5 ++++-
31 include/elf/microblaze.h | 1 +
32 7 files changed, 61 insertions(+), 10 deletions(-)
33
34diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
35index 4228603..1906195 100644
36--- a/bfd/bfd-in2.h
37+++ b/bfd/bfd-in2.h
38@@ -5826,10 +5826,15 @@ value relative to the read-write small data area anchor */
39 expressions of the form "Symbol Op Symbol" */
40 BFD_RELOC_MICROBLAZE_32_SYM_OP_SYM,
41
42-/* This is a 64 bit reloc that stores the 32 bit pc relative
43+/* This is a 32 bit reloc that stores the 32 bit pc relative
44 value in two words (with an imm instruction). No relocation is
45 done here - only used for relaxing */
46- BFD_RELOC_MICROBLAZE_64_NONE,
47+ BFD_RELOC_MICROBLAZE_32_NONE,
48+
49+/* This is a 64 bit reloc that stores the 32 bit pc relative
50+ * +value in two words (with an imm instruction). No relocation is
51+ * +done here - only used for relaxing */
52+ BFD_RELOC_MICROBLAZE_64_NONE,
53
54 /* This is a 64 bit reloc that stores the 32 bit pc relative
55 value in two words (with an imm instruction). The relocation is
56diff --git a/bfd/elf32-microblaze.c b/bfd/elf32-microblaze.c
57index a1d810c..fc0d3e1 100644
58--- a/bfd/elf32-microblaze.c
59+++ b/bfd/elf32-microblaze.c
60@@ -176,6 +176,20 @@ static reloc_howto_type microblaze_elf_howto_raw[] =
61 0x0000ffff, /* Dest Mask. */
62 FALSE), /* PC relative offset? */
63
64+ HOWTO (R_MICROBLAZE_32_NONE, /* Type. */
65+ 0, /* Rightshift. */
66+ 2, /* Size (0 = byte, 1 = short, 2 = long). */
67+ 32, /* Bitsize. */
68+ TRUE, /* PC_relative. */
69+ 0, /* Bitpos. */
70+ complain_overflow_bitfield, /* Complain on overflow. */
71+ NULL, /* Special Function. */
72+ "R_MICROBLAZE_32_NONE",/* Name. */
73+ FALSE, /* Partial Inplace. */
74+ 0, /* Source Mask. */
75+ 0, /* Dest Mask. */
76+ FALSE), /* PC relative offset? */
77+
78 /* This reloc does nothing. Used for relaxation. */
79 HOWTO (R_MICROBLAZE_64_NONE, /* Type. */
80 0, /* Rightshift. */
81@@ -532,6 +546,9 @@ microblaze_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
82 case BFD_RELOC_NONE:
83 microblaze_reloc = R_MICROBLAZE_NONE;
84 break;
85+ case BFD_RELOC_MICROBLAZE_32_NONE:
86+ microblaze_reloc = R_MICROBLAZE_32_NONE;
87+ break;
88 case BFD_RELOC_MICROBLAZE_64_NONE:
89 microblaze_reloc = R_MICROBLAZE_64_NONE;
90 break;
91@@ -1832,14 +1849,23 @@ microblaze_elf_relax_section (bfd *abfd,
92 }
93 break;
94 case R_MICROBLAZE_NONE:
95+ case R_MICROBLAZE_32_NONE:
96 {
97 /* This was a PC-relative instruction that was
98 completely resolved. */
99 int sfix, efix;
100+ unsigned int val;
101 bfd_vma target_address;
102 target_address = irel->r_addend + irel->r_offset;
103 sfix = calc_fixup (irel->r_offset, 0, sec);
104 efix = calc_fixup (target_address, 0, sec);
105+
106+ /* Validate the in-band val. */
107+ val = bfd_get_32 (abfd, contents + irel->r_offset);
108+ if (val != irel->r_addend && ELF32_R_TYPE (irel->r_info) == R_MICROBLAZE_32_NONE) {
109+ fprintf(stderr, "%d: CORRUPT relax reloc %x %lx\n", __LINE__, val, irel->r_addend);
110+ }
111+
112 irel->r_addend -= (efix - sfix);
113 /* Should use HOWTO. */
114 microblaze_bfd_write_imm_value_32 (abfd, contents + irel->r_offset,
115@@ -1887,12 +1913,16 @@ microblaze_elf_relax_section (bfd *abfd,
116 irelscanend = irelocs + o->reloc_count;
117 for (irelscan = irelocs; irelscan < irelscanend; irelscan++)
118 {
119- if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_NONE)
120+ if (1 && ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32_NONE)
121 {
122 unsigned int val;
123
124 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
125
126+ /* hax: We only do the following fixup for debug location lists. */
127+ if (strcmp(".debug_loc", o->name))
128+ continue;
129+
130 /* This was a PC-relative instruction that was completely resolved. */
131 if (ocontents == NULL)
132 {
133@@ -1917,15 +1947,16 @@ microblaze_elf_relax_section (bfd *abfd,
134 }
135 }
136
137- irelscan->r_addend -= calc_fixup (irelscan->r_addend
138- + isym->st_value, sec);
139 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
140+
141+ if (val != irelscan->r_addend) {
142+ fprintf(stderr, "%d: CORRUPT relax reloc! %x %lx\n", __LINE__, val, irelscan->r_addend);
143+ }
144+ irelscan->r_addend -= calc_fixup (irelscan->r_addend, 0, sec);
145+
146 microblaze_bfd_write_imm_value_32 (abfd, ocontents + irelscan->r_offset,
147 irelscan->r_addend);
148 }
149- if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_64_NONE) {
150- fprintf(stderr, "Unhandled NONE 64\n");
151- }
152 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32)
153 {
154 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
155@@ -1985,7 +2016,7 @@ microblaze_elf_relax_section (bfd *abfd,
156 elf_section_data (o)->this_hdr.contents = ocontents;
157 }
158 }
159- irelscan->r_addend -= calc_fixup (irel->r_addend
160+ irelscan->r_addend -= calc_fixup (irelscan->r_addend
161 + isym->st_value,
162 0,
163 sec);
164diff --git a/bfd/libbfd.h b/bfd/libbfd.h
165index 2f5f16e..854bb0c 100644
166--- a/bfd/libbfd.h
167+++ b/bfd/libbfd.h
168@@ -2853,6 +2853,7 @@ static const char *const bfd_reloc_code_real_names[] = { "@@uninitialized@@",
169 "BFD_RELOC_MICROBLAZE_32_ROSDA",
170 "BFD_RELOC_MICROBLAZE_32_RWSDA",
171 "BFD_RELOC_MICROBLAZE_32_SYM_OP_SYM",
172+ "BFD_RELOC_MICROBLAZE_32_NONE",
173 "BFD_RELOC_MICROBLAZE_64_NONE",
174 "BFD_RELOC_MICROBLAZE_64_GOTPC",
175 "BFD_RELOC_MICROBLAZE_64_GOT",
176diff --git a/bfd/reloc.c b/bfd/reloc.c
177index a1353a2..4b57de7 100644
178--- a/bfd/reloc.c
179+++ b/bfd/reloc.c
180@@ -6903,6 +6903,12 @@ ENUMDOC
181 This is a 32 bit reloc for the microblaze to handle
182 expressions of the form "Symbol Op Symbol"
183 ENUM
184+ BFD_RELOC_MICROBLAZE_32_NONE
185+ENUMDOC
186+ This is a 32 bit reloc that stores the 32 bit pc relative
187+ value in two words (with an imm instruction). No relocation is
188+ done here - only used for relaxing
189+ENUM
190 BFD_RELOC_MICROBLAZE_64_NONE
191 ENUMDOC
192 This is a 64 bit reloc that stores the 32 bit pc relative
193diff --git a/binutils/readelf.c b/binutils/readelf.c
194index fed0387..92f655d 100644
195--- a/binutils/readelf.c
196+++ b/binutils/readelf.c
197@@ -12774,6 +12774,10 @@ is_none_reloc (Filedata * filedata, unsigned int reloc_type)
198 || reloc_type == 32 /* R_AVR_DIFF32. */);
199 case EM_METAG:
200 return reloc_type == 3; /* R_METAG_NONE. */
201+ case EM_MICROBLAZE:
202+ return reloc_type == 30 /* R_MICROBLAZE_32_NONE. */
203+ || reloc_type == 0 /* R_MICROBLAZE_NONE. */
204+ || reloc_type == 9; /* R_MICROBLAZE_64_NONE. */
205 case EM_NDS32:
206 return (reloc_type == 0 /* R_XTENSA_NONE. */
207 || reloc_type == 204 /* R_NDS32_DIFF8. */
208diff --git a/gas/config/tc-microblaze.c b/gas/config/tc-microblaze.c
209index 50dbfc7..d66e949 100644
210--- a/gas/config/tc-microblaze.c
211+++ b/gas/config/tc-microblaze.c
212@@ -2179,7 +2179,9 @@ md_apply_fix (fixS * fixP,
213 /* This fixup has been resolved. Create a reloc in case the linker
214 moves code around due to relaxing. */
215 if (fixP->fx_r_type == BFD_RELOC_64_PCREL)
216- fixP->fx_r_type = BFD_RELOC_MICROBLAZE_64_NONE;
217+ fixP->fx_r_type = BFD_RELOC_MICROBLAZE_64_NONE;
218+ else if (fixP->fx_r_type == BFD_RELOC_32)
219+ fixP->fx_r_type = BFD_RELOC_MICROBLAZE_32_NONE;
220 else
221 fixP->fx_r_type = BFD_RELOC_NONE;
222 fixP->fx_addsy = section_symbol (absolute_section);
223@@ -2401,6 +2403,7 @@ tc_gen_reloc (asection * section ATTRIBUTE_UNUSED, fixS * fixp)
224 switch (fixp->fx_r_type)
225 {
226 case BFD_RELOC_NONE:
227+ case BFD_RELOC_MICROBLAZE_32_NONE:
228 case BFD_RELOC_MICROBLAZE_64_NONE:
229 case BFD_RELOC_32:
230 case BFD_RELOC_MICROBLAZE_32_LO:
231diff --git a/include/elf/microblaze.h b/include/elf/microblaze.h
232index ae98099..c8cc57b 100644
233--- a/include/elf/microblaze.h
234+++ b/include/elf/microblaze.h
235@@ -58,6 +58,7 @@ START_RELOC_NUMBERS (elf_microblaze_reloc_type)
236 RELOC_NUMBER (R_MICROBLAZE_TLSDTPREL64, 27) /* TLS Offset Within TLS Block */
237 RELOC_NUMBER (R_MICROBLAZE_TLSGOTTPREL32, 28) /* TLS Offset From Thread Pointer */
238 RELOC_NUMBER (R_MICROBLAZE_TLSTPREL32, 29) /* TLS Offset From Thread Pointer */
239+ RELOC_NUMBER (R_MICROBLAZE_32_NONE, 30)
240
241 END_RELOC_NUMBERS (R_MICROBLAZE_max)
242