|
| 1 | +Fix const-correctness issues for glibc >= 2.43. |
| 2 | + |
| 3 | +glibc 2.43 introduced ISO C23 const-preserving _Generic macros for strchr, |
| 4 | +memchr, strrchr, bsearch, and similar functions. When the input is const, |
| 5 | +these functions now return a const pointer, causing -Werror=discarded-qualifiers |
| 6 | +failures. Update variable types and remove unnecessary non-const casts. |
| 7 | + |
| 8 | +Upstream fix: https://sourceware.org/git/?p=elfutils.git;a=commit;h=4a5cf8be906d5991e7527e69e3f2ceaa74811301 |
| 9 | + |
| 10 | +diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-client.c |
| 11 | +--- a/debuginfod/debuginfod-client.c |
| 12 | ++++ b/debuginfod/debuginfod-client.c |
| 13 | +@@ -3104,7 +3104,7 @@ |
| 14 | + /* Sanity check header value is of the form Header: Value. |
| 15 | + It should contain at least one colon that isn't the first or |
| 16 | + last character. */ |
| 17 | +- char *colon = strchr (header, ':'); /* first colon */ |
| 18 | ++ const char *colon = strchr (header, ':'); /* first colon */ |
| 19 | + if (colon == NULL /* present */ |
| 20 | + || colon == header /* not at beginning - i.e., have a header name */ |
| 21 | + || *(colon + 1) == '\0') /* not at end - i.e., have a value */ |
| 22 | +diff --git a/libcpu/riscv_disasm.c b/libcpu/riscv_disasm.c |
| 23 | +--- a/libcpu/riscv_disasm.c |
| 24 | ++++ b/libcpu/riscv_disasm.c |
| 25 | +@@ -77,7 +77,7 @@ |
| 26 | + "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", |
| 27 | + "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6" |
| 28 | + }; |
| 29 | +-#define REG(nr) ((char *) regnames[nr]) |
| 30 | ++#define REG(nr) regnames[nr] |
| 31 | + #define REGP(nr) REG (8 + (nr)) |
| 32 | + |
| 33 | + |
| 34 | +@@ -88,7 +88,7 @@ |
| 35 | + "fa6", "fa7", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", |
| 36 | + "fs8", "fs9", "fs10", "fs11", "ft8", "ft9", "ft10", "ft11" |
| 37 | + }; |
| 38 | +-#define FREG(nr) ((char *) fregnames[nr]) |
| 39 | ++#define FREG(nr) fregnames[nr] |
| 40 | + #define FREGP(nr) FREG (8 + (nr)) |
| 41 | + |
| 42 | + |
| 43 | +@@ -163,12 +163,12 @@ |
| 44 | + break; |
| 45 | + } |
| 46 | + |
| 47 | +- char *mne = NULL; |
| 48 | ++ const char *mne = NULL; |
| 49 | + /* Max length is 24, which is "illegal", so we print it as |
| 50 | + "0x<48 hex chars>" |
| 51 | + See: No instruction encodings defined for these sizes yet, below */ |
| 52 | + char mnebuf[50]; |
| 53 | +- char *op[5] = { NULL, NULL, NULL, NULL, NULL }; |
| 54 | ++ const char *op[5] = { NULL, NULL, NULL, NULL, NULL }; |
| 55 | + char immbuf[32]; |
| 56 | + size_t len; |
| 57 | + char *strp = NULL; |
| 58 | +@@ -400,7 +400,7 @@ |
| 59 | + { |
| 60 | + "sub", "xor", "or", "and", "subw", "addw", NULL, NULL |
| 61 | + }; |
| 62 | +- mne = (char *) arithmne[((first >> 10) & 0x4) | ((first >> 5) & 0x3)]; |
| 63 | ++ mne = arithmne[((first >> 10) & 0x4) | ((first >> 5) & 0x3)]; |
| 64 | + } |
| 65 | + op[0] = op[1] = REGP ((first >> 7) & 0x7); |
| 66 | + break; |
| 67 | +@@ -572,7 +572,7 @@ |
| 68 | + { |
| 69 | + NULL, NULL, "flw", "fld", "flq", NULL, NULL, NULL |
| 70 | + }; |
| 71 | +- mne = (char *) (idx == 0x00 ? loadmne[func] : floadmne[func]); |
| 72 | ++ mne = idx == 0x00 ? loadmne[func] : floadmne[func]; |
| 73 | + break; |
| 74 | + case 0x03: |
| 75 | + // MISC-MEM |
| 76 | +@@ -595,8 +595,8 @@ |
| 77 | + uint32_t succ = (word >> 24) & 0xf; |
| 78 | + if (pred != 0xf || succ != 0xf) |
| 79 | + { |
| 80 | +- op[0] = (char *) order[succ]; |
| 81 | +- op[1] = (char *) order[pred]; |
| 82 | ++ op[0] = order[succ]; |
| 83 | ++ op[1] = order[pred]; |
| 84 | + } |
| 85 | + mne = "fence"; |
| 86 | + } |
| 87 | +@@ -614,7 +614,7 @@ |
| 88 | + "addi", NULL, "slti", "sltiu", "xori", NULL, "ori", "andi" |
| 89 | + }; |
| 90 | + func = (word >> 12) & 0x7; |
| 91 | +- mne = (char *) opimmmne[func]; |
| 92 | ++ mne = opimmmne[func]; |
| 93 | + if (mne == NULL) |
| 94 | + { |
| 95 | + const uint64_t shiftmask = ebl->class == ELFCLASS32 ? 0x1f : 0x3f; |
| 96 | +@@ -697,7 +697,7 @@ |
| 97 | + { |
| 98 | + NULL, NULL, "fsw", "fsd", "fsq", NULL, NULL, NULL |
| 99 | + }; |
| 100 | +- mne = (char *) (idx == 0x08 ? storemne[func] : fstoremne[func]); |
| 101 | ++ mne = idx == 0x08 ? storemne[func] : fstoremne[func]; |
| 102 | + break; |
| 103 | + case 0x0b: |
| 104 | + // AMO |
| 105 | +@@ -778,7 +778,7 @@ |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | +- mne = (char *) (idx == 0x0c ? arithmne2[func] : arithmne3[func]); |
| 110 | ++ mne = idx == 0x0c ? arithmne2[func] : arithmne3[func]; |
| 111 | + op[1] = REG (rs1); |
| 112 | + op[2] = REG (rs2); |
| 113 | + } |
| 114 | +@@ -811,7 +811,7 @@ |
| 115 | + op[2] = FREG (rs2); |
| 116 | + op[3] = FREG (rs3); |
| 117 | + if (rm != 0x7) |
| 118 | +- op[4] = (char *) rndmode[rm]; |
| 119 | ++ op[4] = rndmode[rm]; |
| 120 | + } |
| 121 | + break; |
| 122 | + case 0x14: |
| 123 | +@@ -839,7 +839,7 @@ |
| 124 | + op[1] = FREG (rs1); |
| 125 | + op[2] = FREG (rs2); |
| 126 | + if (rm != 0x7) |
| 127 | +- op[3] = (char *) rndmode[rm]; |
| 128 | ++ op[3] = rndmode[rm]; |
| 129 | + } |
| 130 | + else if (func == 0x1c && width != 2 && rs2 == 0 && rm <= 1) |
| 131 | + { |
| 132 | +@@ -950,7 +950,7 @@ |
| 133 | + } |
| 134 | + mne = mnebuf; |
| 135 | + if (rm != 0x7 && (func == 0x18 || width == 0 || rs2 >= 2)) |
| 136 | +- op[2] = (char *) rndmode[rm]; |
| 137 | ++ op[2] = rndmode[rm]; |
| 138 | + } |
| 139 | + else if (func == 0x0b && rs2 == 0) |
| 140 | + { |
| 141 | +@@ -961,7 +961,7 @@ |
| 142 | + *cp = '\0'; |
| 143 | + mne = mnebuf; |
| 144 | + if (rm != 0x7) |
| 145 | +- op[2] = (char *) rndmode[rm]; |
| 146 | ++ op[2] = rndmode[rm]; |
| 147 | + } |
| 148 | + else if (func == 0x05 && rm < 2) |
| 149 | + { |
| 150 | +@@ -1007,7 +1007,7 @@ |
| 151 | + "beq", "bne", NULL, NULL, "blt", "bge", "bltu", "bgeu" |
| 152 | + }; |
| 153 | + func = (word >> 12) & 0x7; |
| 154 | +- mne = (char *) branchmne[func]; |
| 155 | ++ mne = branchmne[func]; |
| 156 | + if (rs1 == 0 && func == 5) |
| 157 | + { |
| 158 | + op[0] = op[1]; |
| 159 | +@@ -1035,7 +1035,7 @@ |
| 160 | + else if (func == 5 || func == 7) |
| 161 | + { |
| 162 | + // binutils use these opcodes and the reverse parameter order |
| 163 | +- char *tmp = op[0]; |
| 164 | ++ const char *tmp = op[0]; |
| 165 | + op[0] = op[1]; |
| 166 | + op[1] = tmp; |
| 167 | + mne = func == 5 ? "ble" : "bleu"; |
| 168 | +@@ -1103,7 +1103,7 @@ |
| 169 | + { |
| 170 | + NULL, "frflags", "frrm", "frsr", |
| 171 | + }; |
| 172 | +- mne = (char *) unprivrw[csr - 0x000]; |
| 173 | ++ mne = unprivrw[csr - 0x000]; |
| 174 | + } |
| 175 | + else if (csr >= 0xc00 && csr <= 0xc03) |
| 176 | + { |
| 177 | +@@ -1111,7 +1111,7 @@ |
| 178 | + { |
| 179 | + "rdcycle", "rdtime", "rdinstret" |
| 180 | + }; |
| 181 | +- mne = (char *) unprivrolow[csr - 0xc00]; |
| 182 | ++ mne = unprivrolow[csr - 0xc00]; |
| 183 | + } |
| 184 | + op[0] = REG ((word >> 7) & 0x1f); |
| 185 | + } |
| 186 | +@@ -1128,7 +1128,7 @@ |
| 187 | + { |
| 188 | + NULL, "fsflagsi", "fsrmi", NULL |
| 189 | + }; |
| 190 | +- mne = (char *) ((word & 0x4000) == 0 ? unprivrs : unprivrsi)[csr - 0x000]; |
| 191 | ++ mne = ((word & 0x4000) == 0 ? unprivrs : unprivrsi)[csr - 0x000]; |
| 192 | + |
| 193 | + if ((word & 0x4000) == 0) |
| 194 | + op[0] = REG ((word >> 15) & 0x1f); |
| 195 | +@@ -1259,12 +1259,12 @@ |
| 196 | + if (rd != 0) |
| 197 | + op[last++] = REG (rd); |
| 198 | + struct known_csrs key = { csr, NULL }; |
| 199 | +- struct known_csrs *found = bsearch (&key, known, |
| 200 | +- sizeof (known) / sizeof (known[0]), |
| 201 | +- sizeof (known[0]), |
| 202 | +- compare_csr); |
| 203 | ++ const struct known_csrs *found = bsearch (&key, known, |
| 204 | ++ sizeof (known) / sizeof (known[0]), |
| 205 | ++ sizeof (known[0]), |
| 206 | ++ compare_csr); |
| 207 | + if (found) |
| 208 | +- op[last] = (char *) found->name; |
| 209 | ++ op[last] = found->name; |
| 210 | + else |
| 211 | + { |
| 212 | + snprintf (addrbuf, sizeof (addrbuf), "0x%" PRIx32, csr); |
| 213 | +@@ -1289,7 +1289,7 @@ |
| 214 | + else if (instr == 3 && rd == 0) |
| 215 | + mne = "csrc"; |
| 216 | + else |
| 217 | +- mne = (char *) mnecsr[instr]; |
| 218 | ++ mne = mnecsr[instr]; |
| 219 | + } |
| 220 | + break; |
| 221 | + default: |
| 222 | +diff --git a/libdw/dwarf_getsrclines.c b/libdw/dwarf_getsrclines.c |
| 223 | +--- a/libdw/dwarf_getsrclines.c |
| 224 | ++++ b/libdw/dwarf_getsrclines.c |
| 225 | +@@ -364,7 +364,7 @@ |
| 226 | + const unsigned char *dirp = linep; |
| 227 | + while (dirp < lineendp && *dirp != 0) |
| 228 | + { |
| 229 | +- uint8_t *endp = memchr (dirp, '\0', lineendp - dirp); |
| 230 | ++ const uint8_t *endp = memchr (dirp, '\0', lineendp - dirp); |
| 231 | + if (endp == NULL) |
| 232 | + goto invalid_data; |
| 233 | + ++ndirs; |
| 234 | +@@ -440,7 +440,7 @@ |
| 235 | + for (unsigned int n = 1; n < ndirlist; n++) |
| 236 | + { |
| 237 | + dirarray[n].dir = (char *) linep; |
| 238 | +- uint8_t *endp = memchr (linep, '\0', lineendp - linep); |
| 239 | ++ const uint8_t *endp = memchr (linep, '\0', lineendp - linep); |
| 240 | + assert (endp != NULL); // Checked above when calculating ndirlist. |
| 241 | + dirarray[n].len = endp - linep; |
| 242 | + linep = endp + 1; |
| 243 | +@@ -927,7 +927,7 @@ |
| 244 | + case DW_LNE_define_file: |
| 245 | + { |
| 246 | + char *fname = (char *) linep; |
| 247 | +- uint8_t *endp = memchr (linep, '\0', lineendp - linep); |
| 248 | ++ const uint8_t *endp = memchr (linep, '\0', lineendp - linep); |
| 249 | + if (endp == NULL) |
| 250 | + goto invalid_data; |
| 251 | + size_t fnamelen = endp - linep; |
| 252 | +diff --git a/src/readelf.c b/src/readelf.c |
| 253 | +--- a/src/readelf.c |
| 254 | ++++ b/src/readelf.c |
| 255 | +@@ -8269,7 +8269,7 @@ |
| 256 | + valuestr = dwarf_filesrc (files, num, NULL, NULL); |
| 257 | + if (valuestr != NULL) |
| 258 | + { |
| 259 | +- char *filename = strrchr (valuestr, '/'); |
| 260 | ++ const char *filename = strrchr (valuestr, '/'); |
| 261 | + if (filename != NULL) |
| 262 | + valuestr = filename + 1; |
| 263 | + } |
| 264 | +@@ -9033,7 +9033,7 @@ |
| 265 | + Dwarf_Off str_offsets_base, FILE *out) |
| 266 | + { |
| 267 | + Dwarf_Word val; |
| 268 | +- unsigned char *endp; |
| 269 | ++ const unsigned char *endp; |
| 270 | + Elf_Data *data; |
| 271 | + char *str; |
| 272 | + switch (form) |
| 273 | +@@ -9530,7 +9530,7 @@ |
| 274 | + { |
| 275 | + while (linep < lineendp && *linep != 0) |
| 276 | + { |
| 277 | +- unsigned char *endp = memchr (linep, '\0', lineendp - linep); |
| 278 | ++ const unsigned char *endp = memchr (linep, '\0', lineendp - linep); |
| 279 | + if (unlikely (endp == NULL)) |
| 280 | + goto invalid_unit; |
| 281 | + |
| 282 | +@@ -9764,7 +9764,7 @@ |
| 283 | + case DW_LNE_define_file: |
| 284 | + { |
| 285 | + char *fname = (char *) linep; |
| 286 | +- unsigned char *endp = memchr (linep, '\0', |
| 287 | ++ const unsigned char *endp = memchr (linep, '\0', |
| 288 | + lineendp - linep); |
| 289 | + if (unlikely (endp == NULL)) |
| 290 | + goto invalid_unit; |
0 commit comments