The current TO_NATIVE() has some limitations: 1) You cannot cast the argument. 2) You cannot pass a variable marked as 'const'. 3) Passing an array is a bug, but it is not detected. Impelement TO_NATIVE() using bswap_*() functions. These are GNU extensions. If we face portability issues, we can port the code from include/uapi/linux/swab.h. With this change, get_rel_type_and_sym() can be simplified by casting the arguments directly.
Showing the changes to scripts (2 of 2 files). Show all
Unified · Split
| @@ -1410,15 +1410,10 @@ static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info, · 1409 unmodified lines | |||
| 1410 | return; | 1410 | return; |
| 1411 | } | 1411 | } |
| 1412 | 1412 | ||
| 1413 | if (is_64bit) { | 1413 | if (is_64bit) |
| 1414 | Elf64_Xword r_info64 = r_info; | 1414 | r_info = TO_NATIVE((Elf64_Xword)r_info); |
| 1415 | 1415 | else | |
| 1416 | r_info = TO_NATIVE(r_info64); | 1416 | r_info = TO_NATIVE((Elf32_Word)r_info); |
| 1417 | } else { | ||
| 1418 | Elf32_Word r_info32 = r_info; | ||
| 1419 | |||
| 1420 | r_info = TO_NATIVE(r_info32); | ||
| 1421 | } | ||
| 1422 | 1417 | ||
| 1423 | *r_type = ELF_R_TYPE(r_info); | 1418 | *r_type = ELF_R_TYPE(r_info); |
| 1424 | *r_sym = ELF_R_SYM(r_info); | 1419 | *r_sym = ELF_R_SYM(r_info); |
| @@ -1,4 +1,5 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0 */ | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #include <byteswap.h> | ||
| 2 | #include <stdbool.h> | 3 | #include <stdbool.h> |
| 3 | #include <stdio.h> | 4 | #include <stdio.h> |
| 4 | #include <stdlib.h> | 5 | #include <stdlib.h> |
| @@ -51,21 +52,19 @@ · 46 unmodified lines | |||
| 51 | #define ELF_R_TYPE ELF64_R_TYPE | 52 | #define ELF_R_TYPE ELF64_R_TYPE |
| 52 | #endif | 53 | #endif |
| 53 | 54 | ||
| 54 | #if KERNEL_ELFDATA != HOST_ELFDATA | 55 | #define bswap(x) \ |
| 56 | ({ \ | ||
| 57 | _Static_assert(sizeof(x) == 1 || sizeof(x) == 2 || \ | ||
| 58 | sizeof(x) == 4 || sizeof(x) == 8, "bug"); \ | ||
| 59 | (typeof(x))(sizeof(x) == 2 ? bswap_16(x) : \ | ||
| 60 | sizeof(x) == 4 ? bswap_32(x) : \ | ||
| 61 | sizeof(x) == 8 ? bswap_64(x) : \ | ||
| 62 | x); \ | ||
| 63 | }) | ||
| 55 | 64 | ||
| 56 | static inline void __endian(const void *src, void *dest, unsigned int size) | 65 | #if KERNEL_ELFDATA != HOST_ELFDATA |
| 57 | { | ||
| 58 | unsigned int i; | ||
| 59 | for (i = 0; i < size; i++) | ||
| 60 | ((unsigned char*)dest)[i] = ((unsigned char*)src)[size - i-1]; | ||
| 61 | } | ||
| 62 | 66 | ||
| 63 | #define TO_NATIVE(x) \ | 67 | #define TO_NATIVE(x) (bswap(x)) |
| 64 | ({ \ | ||
| 65 | typeof(x) __x; \ | ||
| 66 | __endian(&(x), &(__x), sizeof(__x)); \ | ||
| 67 | __x; \ | ||
| 68 | }) | ||
| 69 | 68 | ||
| 70 | #else /* endianness matches */ | 69 | #else /* endianness matches */ |
| 71 | 70 | ||