blob: a2954c91eaa94b93d9a9c53d4d40fa82367a2c0f [file] [log] [blame]
Brad Bishop26bdd442019-08-16 17:08:17 -04001From 68a91eb7ac94f0e0c6e9ebd7ad148c80cbc227df Mon Sep 17 00:00:00 2001
2From: Nagaraju Mekala <nmekala@xilix.com>
3Date: Mon, 6 Feb 2017 15:53:08 +0530
4Subject: [PATCH] Fixup debug_loc sections after linker relaxation Adds a new
5 reloctype R_MICROBLAZE_32_NONE, used for passing reloc info from the
6 assembler to the linker when the linker manages to fully resolve a local
7 symbol reference.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08008
9This is a workaround for design flaws in the assembler to
10linker interface with regards to linker relaxation.
11
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080012Signed-off-by: Edgar E. Iglesias <edgar.iglesias@gmail.com>
Brad Bishop26bdd442019-08-16 17:08:17 -040013Signed-off-by: Nagaraju Mekala <nagaraju.mekala@xilinx.com>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080014
15---
Brad Bishop26bdd442019-08-16 17:08:17 -040016 bfd/bfd-in2.h | 9 ++++++--
17 bfd/elf32-microblaze.c | 53 ++++++++++++++++++++++++++++++++++------------
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080018 bfd/libbfd.h | 1 +
19 bfd/reloc.c | 6 ++++++
20 binutils/readelf.c | 4 ++++
21 gas/config/tc-microblaze.c | 5 ++++-
Brad Bishop26bdd442019-08-16 17:08:17 -040022 include/elf/microblaze.h | 2 ++
23 7 files changed, 64 insertions(+), 16 deletions(-)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080024
Brad Bishop26bdd442019-08-16 17:08:17 -040025diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
26index e251d7e..fe6933a 100644
27--- a/bfd/bfd-in2.h
28+++ b/bfd/bfd-in2.h
29@@ -5867,10 +5867,15 @@ value relative to the read-write small data area anchor */
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080030 expressions of the form "Symbol Op Symbol" */
31 BFD_RELOC_MICROBLAZE_32_SYM_OP_SYM,
32
33-/* This is a 64 bit reloc that stores the 32 bit pc relative
34+/* This is a 32 bit reloc that stores the 32 bit pc relative
35 value in two words (with an imm instruction). No relocation is
36 done here - only used for relaxing */
37- BFD_RELOC_MICROBLAZE_64_NONE,
38+ BFD_RELOC_MICROBLAZE_32_NONE,
39+
40+/* This is a 64 bit reloc that stores the 32 bit pc relative
41+ * +value in two words (with an imm instruction). No relocation is
42+ * +done here - only used for relaxing */
43+ BFD_RELOC_MICROBLAZE_64_NONE,
44
45 /* This is a 64 bit reloc that stores the 32 bit pc relative
46 value in two words (with an imm instruction). The relocation is
Brad Bishop26bdd442019-08-16 17:08:17 -040047diff --git a/bfd/elf32-microblaze.c b/bfd/elf32-microblaze.c
48index 359484d..1c69c26 100644
49--- a/bfd/elf32-microblaze.c
50+++ b/bfd/elf32-microblaze.c
51@@ -176,7 +176,21 @@ static reloc_howto_type microblaze_elf_howto_raw[] =
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080052 0x0000ffff, /* Dest Mask. */
53 FALSE), /* PC relative offset? */
54
Brad Bishop26bdd442019-08-16 17:08:17 -040055- /* This reloc does nothing. Used for relaxation. */
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080056+ HOWTO (R_MICROBLAZE_32_NONE, /* Type. */
57+ 0, /* Rightshift. */
58+ 2, /* Size (0 = byte, 1 = short, 2 = long). */
59+ 32, /* Bitsize. */
60+ TRUE, /* PC_relative. */
61+ 0, /* Bitpos. */
62+ complain_overflow_bitfield, /* Complain on overflow. */
63+ NULL, /* Special Function. */
64+ "R_MICROBLAZE_32_NONE",/* Name. */
65+ FALSE, /* Partial Inplace. */
66+ 0, /* Source Mask. */
67+ 0, /* Dest Mask. */
68+ FALSE), /* PC relative offset? */
69+
Brad Bishop26bdd442019-08-16 17:08:17 -040070+ /* This reloc does nothing. Used for relaxation. */
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080071 HOWTO (R_MICROBLAZE_64_NONE, /* Type. */
72 0, /* Rightshift. */
Brad Bishop26bdd442019-08-16 17:08:17 -040073 3, /* Size (0 = byte, 1 = short, 2 = long). */
74@@ -562,6 +576,9 @@ microblaze_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080075 case BFD_RELOC_NONE:
76 microblaze_reloc = R_MICROBLAZE_NONE;
77 break;
78+ case BFD_RELOC_MICROBLAZE_32_NONE:
79+ microblaze_reloc = R_MICROBLAZE_32_NONE;
80+ break;
81 case BFD_RELOC_MICROBLAZE_64_NONE:
82 microblaze_reloc = R_MICROBLAZE_64_NONE;
83 break;
Brad Bishop26bdd442019-08-16 17:08:17 -040084@@ -1918,6 +1935,7 @@ microblaze_elf_relax_section (bfd *abfd,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080085 }
86 break;
87 case R_MICROBLAZE_NONE:
Brad Bishop26bdd442019-08-16 17:08:17 -040088+ case R_MICROBLAZE_32_NONE:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080089 {
90 /* This was a PC-relative instruction that was
91 completely resolved. */
Brad Bishop26bdd442019-08-16 17:08:17 -040092@@ -1926,12 +1944,18 @@ microblaze_elf_relax_section (bfd *abfd,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080093 target_address = irel->r_addend + irel->r_offset;
94 sfix = calc_fixup (irel->r_offset, 0, sec);
95 efix = calc_fixup (target_address, 0, sec);
96+
Brad Bishop26bdd442019-08-16 17:08:17 -040097+ /* Validate the in-band val. */
98+ val = bfd_get_32 (abfd, contents + irel->r_offset);
99+ if (val != irel->r_addend && ELF32_R_TYPE (irel->r_info) == R_MICROBLAZE_32_NONE) {
100+ fprintf(stderr, "%d: CORRUPT relax reloc %x %lx\n", __LINE__, val, irel->r_addend);
101+ }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800102 irel->r_addend -= (efix - sfix);
103 /* Should use HOWTO. */
104 microblaze_bfd_write_imm_value_32 (abfd, contents + irel->r_offset,
Brad Bishop26bdd442019-08-16 17:08:17 -0400105 irel->r_addend);
106- }
107- break;
108+ }
109+ break;
110 case R_MICROBLAZE_64_NONE:
111 {
112 /* This was a PC-relative 64-bit instruction that was
113@@ -1973,12 +1997,16 @@ microblaze_elf_relax_section (bfd *abfd,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800114 irelscanend = irelocs + o->reloc_count;
115 for (irelscan = irelocs; irelscan < irelscanend; irelscan++)
116 {
117- if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_NONE)
Brad Bishop26bdd442019-08-16 17:08:17 -0400118+ if (1 && ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32_NONE)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800119 {
120 unsigned int val;
121
122 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
123
124+ /* hax: We only do the following fixup for debug location lists. */
125+ if (strcmp(".debug_loc", o->name))
126+ continue;
127+
128 /* This was a PC-relative instruction that was completely resolved. */
129 if (ocontents == NULL)
130 {
Brad Bishop26bdd442019-08-16 17:08:17 -0400131@@ -1999,18 +2027,17 @@ microblaze_elf_relax_section (bfd *abfd,
132 (file_ptr) 0,
133 o->rawsize))
134 goto error_return;
135- elf_section_data (o)->this_hdr.contents = ocontents;
136- }
137- }
138- irelscan->r_addend -= calc_fixup (irelscan->r_addend
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800139- + isym->st_value, sec);
Brad Bishop26bdd442019-08-16 17:08:17 -0400140+ elf_section_data (o)->this_hdr.contents = ocontents;
141+ }
142+ }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800143 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800144+ if (val != irelscan->r_addend) {
145+ fprintf(stderr, "%d: CORRUPT relax reloc! %x %lx\n", __LINE__, val, irelscan->r_addend);
146+ }
147+ irelscan->r_addend -= calc_fixup (irelscan->r_addend, 0, sec);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800148 microblaze_bfd_write_imm_value_32 (abfd, ocontents + irelscan->r_offset,
149 irelscan->r_addend);
150 }
151- if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_64_NONE) {
152- fprintf(stderr, "Unhandled NONE 64\n");
153- }
154 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32)
155 {
156 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
Brad Bishop26bdd442019-08-16 17:08:17 -0400157@@ -2070,7 +2097,7 @@ microblaze_elf_relax_section (bfd *abfd,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800158 elf_section_data (o)->this_hdr.contents = ocontents;
159 }
160 }
161- irelscan->r_addend -= calc_fixup (irel->r_addend
162+ irelscan->r_addend -= calc_fixup (irelscan->r_addend
163 + isym->st_value,
164 0,
165 sec);
Brad Bishop26bdd442019-08-16 17:08:17 -0400166diff --git a/bfd/libbfd.h b/bfd/libbfd.h
167index 36284d7..feb9fad 100644
168--- a/bfd/libbfd.h
169+++ b/bfd/libbfd.h
170@@ -2901,6 +2901,7 @@ static const char *const bfd_reloc_code_real_names[] = { "@@uninitialized@@",
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800171 "BFD_RELOC_MICROBLAZE_32_ROSDA",
172 "BFD_RELOC_MICROBLAZE_32_RWSDA",
173 "BFD_RELOC_MICROBLAZE_32_SYM_OP_SYM",
174+ "BFD_RELOC_MICROBLAZE_32_NONE",
175 "BFD_RELOC_MICROBLAZE_64_NONE",
176 "BFD_RELOC_MICROBLAZE_64_GOTPC",
177 "BFD_RELOC_MICROBLAZE_64_GOT",
Brad Bishop26bdd442019-08-16 17:08:17 -0400178diff --git a/bfd/reloc.c b/bfd/reloc.c
179index e6446a7..87753ae 100644
180--- a/bfd/reloc.c
181+++ b/bfd/reloc.c
182@@ -6796,6 +6796,12 @@ ENUMDOC
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800183 This is a 32 bit reloc for the microblaze to handle
184 expressions of the form "Symbol Op Symbol"
185 ENUM
186+ BFD_RELOC_MICROBLAZE_32_NONE
187+ENUMDOC
188+ This is a 32 bit reloc that stores the 32 bit pc relative
189+ value in two words (with an imm instruction). No relocation is
190+ done here - only used for relaxing
191+ENUM
192 BFD_RELOC_MICROBLAZE_64_NONE
193 ENUMDOC
194 This is a 64 bit reloc that stores the 32 bit pc relative
Brad Bishop26bdd442019-08-16 17:08:17 -0400195diff --git a/binutils/readelf.c b/binutils/readelf.c
196index 9df3742..1bbc2d1 100644
197--- a/binutils/readelf.c
198+++ b/binutils/readelf.c
199@@ -13020,6 +13020,10 @@ is_none_reloc (Filedata * filedata, unsigned int reloc_type)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800200 || reloc_type == 32 /* R_AVR_DIFF32. */);
201 case EM_METAG:
202 return reloc_type == 3; /* R_METAG_NONE. */
203+ case EM_MICROBLAZE:
Brad Bishop26bdd442019-08-16 17:08:17 -0400204+ return reloc_type == 30 /* R_MICROBLAZE_32_NONE. */
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800205+ || reloc_type == 0 /* R_MICROBLAZE_NONE. */
206+ || reloc_type == 9; /* R_MICROBLAZE_64_NONE. */
207 case EM_NDS32:
208 return (reloc_type == 0 /* R_XTENSA_NONE. */
209 || reloc_type == 204 /* R_NDS32_DIFF8. */
Brad Bishop26bdd442019-08-16 17:08:17 -0400210diff --git a/gas/config/tc-microblaze.c b/gas/config/tc-microblaze.c
211index 3e72840..fa665b4 100644
212--- a/gas/config/tc-microblaze.c
213+++ b/gas/config/tc-microblaze.c
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800214@@ -2201,7 +2201,9 @@ md_apply_fix (fixS * fixP,
215 /* This fixup has been resolved. Create a reloc in case the linker
216 moves code around due to relaxing. */
217 if (fixP->fx_r_type == BFD_RELOC_64_PCREL)
218- fixP->fx_r_type = BFD_RELOC_MICROBLAZE_64_NONE;
219+ fixP->fx_r_type = BFD_RELOC_MICROBLAZE_64_NONE;
220+ else if (fixP->fx_r_type == BFD_RELOC_32)
221+ fixP->fx_r_type = BFD_RELOC_MICROBLAZE_32_NONE;
222 else
223 fixP->fx_r_type = BFD_RELOC_NONE;
224 fixP->fx_addsy = section_symbol (absolute_section);
Brad Bishop26bdd442019-08-16 17:08:17 -0400225@@ -2426,6 +2428,7 @@ tc_gen_reloc (asection * section ATTRIBUTE_UNUSED, fixS * fixp)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800226 switch (fixp->fx_r_type)
227 {
228 case BFD_RELOC_NONE:
229+ case BFD_RELOC_MICROBLAZE_32_NONE:
230 case BFD_RELOC_MICROBLAZE_64_NONE:
231 case BFD_RELOC_32:
232 case BFD_RELOC_MICROBLAZE_32_LO:
Brad Bishop26bdd442019-08-16 17:08:17 -0400233diff --git a/include/elf/microblaze.h b/include/elf/microblaze.h
234index 830b5ad..6ee0966 100644
235--- a/include/elf/microblaze.h
236+++ b/include/elf/microblaze.h
237@@ -61,6 +61,8 @@ START_RELOC_NUMBERS (elf_microblaze_reloc_type)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800238 RELOC_NUMBER (R_MICROBLAZE_TEXTPCREL_64, 30) /* PC-relative TEXT offset. */
239 RELOC_NUMBER (R_MICROBLAZE_TEXTREL_64, 31) /* TEXT Entry offset 64-bit. */
240 RELOC_NUMBER (R_MICROBLAZE_TEXTREL_32_LO, 32) /* TEXT Entry offset 32-bit. */
241+ RELOC_NUMBER (R_MICROBLAZE_32_NONE, 33)
Brad Bishop26bdd442019-08-16 17:08:17 -0400242+
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800243 END_RELOC_NUMBERS (R_MICROBLAZE_max)
244
245 /* Global base address names. */