History

modpost: define TO_NATIVE() using bswap_* functions

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.

Masahiro Yamada committed 3 years ago

Commit bd78c9d1 parent ac96a152 files · +16 −22Browse files

Unified · Split

2 files
  1. scripts/mod/modpost.c +4 −9
  2. scripts/mod/modpost.h +12 −13
scripts/mod/modpost.c+4−9
@@ -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);
scripts/mod/modpost.h+12−13
@@ -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_TYPE52#define ELF_R_TYPE ELF64_R_TYPE
52#endif53#endif
53 54
54#if KERNEL_ELFDATA != HOST_ELFDATA55#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
56static 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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t