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