scripts/sortextable: Clean up the code to meet the kernel coding style better
Fix various style errors and inconsistencies, no functional changes intended. Signed-off-by: Shile Zhang <shile.zhang@linux.alibaba.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Masahiro Yamada <yamada.masahiro@socionext.com> Cc: Michal Marek <michal.lkml@markovi.net> Cc: linux-kbuild@vger.kernel.org Link: https://lkml.kernel.org/r/20191204004633.88660-3-shile.zhang@linux.alibaba.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
This commit is contained in:
@@ -50,6 +50,14 @@
|
|||||||
#define EM_ARCV2 195
|
#define EM_ARCV2 195
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static uint32_t (*r)(const uint32_t *);
|
||||||
|
static uint16_t (*r2)(const uint16_t *);
|
||||||
|
static uint64_t (*r8)(const uint64_t *);
|
||||||
|
static void (*w)(uint32_t, uint32_t *);
|
||||||
|
static void (*w2)(uint16_t, uint16_t *);
|
||||||
|
static void (*w8)(uint64_t, uint64_t *);
|
||||||
|
typedef void (*table_sort_t)(char *, int);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get the whole file as a programming convenience in order to avoid
|
* Get the whole file as a programming convenience in order to avoid
|
||||||
* malloc+lseek+read+free of many pieces. If successful, then mmap
|
* malloc+lseek+read+free of many pieces. If successful, then mmap
|
||||||
@@ -75,6 +83,7 @@ static void *mmap_file(char const *fname, size_t *size)
|
|||||||
fprintf(stderr, "not a regular file: %s\n", fname);
|
fprintf(stderr, "not a regular file: %s\n", fname);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
if (addr == MAP_FAILED) {
|
if (addr == MAP_FAILED) {
|
||||||
fprintf(stderr, "Could not mmap file: %s\n", fname);
|
fprintf(stderr, "Could not mmap file: %s\n", fname);
|
||||||
@@ -88,64 +97,65 @@ out:
|
|||||||
return addr;
|
return addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint64_t r8be(const uint64_t *x)
|
|
||||||
{
|
|
||||||
return get_unaligned_be64(x);
|
|
||||||
}
|
|
||||||
static uint32_t rbe(const uint32_t *x)
|
static uint32_t rbe(const uint32_t *x)
|
||||||
{
|
{
|
||||||
return get_unaligned_be32(x);
|
return get_unaligned_be32(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t r2be(const uint16_t *x)
|
static uint16_t r2be(const uint16_t *x)
|
||||||
{
|
{
|
||||||
return get_unaligned_be16(x);
|
return get_unaligned_be16(x);
|
||||||
}
|
}
|
||||||
static uint64_t r8le(const uint64_t *x)
|
|
||||||
|
static uint64_t r8be(const uint64_t *x)
|
||||||
{
|
{
|
||||||
return get_unaligned_le64(x);
|
return get_unaligned_be64(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t rle(const uint32_t *x)
|
static uint32_t rle(const uint32_t *x)
|
||||||
{
|
{
|
||||||
return get_unaligned_le32(x);
|
return get_unaligned_le32(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t r2le(const uint16_t *x)
|
static uint16_t r2le(const uint16_t *x)
|
||||||
{
|
{
|
||||||
return get_unaligned_le16(x);
|
return get_unaligned_le16(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint64_t r8le(const uint64_t *x)
|
||||||
|
{
|
||||||
|
return get_unaligned_le64(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void wbe(uint32_t val, uint32_t *x)
|
||||||
|
{
|
||||||
|
put_unaligned_be32(val, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void w2be(uint16_t val, uint16_t *x)
|
||||||
|
{
|
||||||
|
put_unaligned_be16(val, x);
|
||||||
|
}
|
||||||
|
|
||||||
static void w8be(uint64_t val, uint64_t *x)
|
static void w8be(uint64_t val, uint64_t *x)
|
||||||
{
|
{
|
||||||
put_unaligned_be64(val, x);
|
put_unaligned_be64(val, x);
|
||||||
}
|
}
|
||||||
static void wbe(uint32_t val, uint32_t *x)
|
|
||||||
{
|
|
||||||
put_unaligned_be32(val, x);
|
|
||||||
}
|
|
||||||
static void w2be(uint16_t val, uint16_t *x)
|
|
||||||
{
|
|
||||||
put_unaligned_be16(val, x);
|
|
||||||
}
|
|
||||||
static void w8le(uint64_t val, uint64_t *x)
|
|
||||||
{
|
|
||||||
put_unaligned_le64(val, x);
|
|
||||||
}
|
|
||||||
static void wle(uint32_t val, uint32_t *x)
|
static void wle(uint32_t val, uint32_t *x)
|
||||||
{
|
{
|
||||||
put_unaligned_le32(val, x);
|
put_unaligned_le32(val, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void w2le(uint16_t val, uint16_t *x)
|
static void w2le(uint16_t val, uint16_t *x)
|
||||||
{
|
{
|
||||||
put_unaligned_le16(val, x);
|
put_unaligned_le16(val, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint64_t (*r8)(const uint64_t *);
|
static void w8le(uint64_t val, uint64_t *x)
|
||||||
static uint32_t (*r)(const uint32_t *);
|
{
|
||||||
static uint16_t (*r2)(const uint16_t *);
|
put_unaligned_le64(val, x);
|
||||||
static void (*w8)(uint64_t, uint64_t *);
|
}
|
||||||
static void (*w)(uint32_t, uint32_t *);
|
|
||||||
static void (*w2)(uint16_t, uint16_t *);
|
|
||||||
|
|
||||||
typedef void (*table_sort_t)(char *, int);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
|
* Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
|
||||||
@@ -188,11 +198,35 @@ static int compare_relative_table(const void *a, const void *b)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void sort_relative_table(char *extab_image, int image_size)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Do the same thing the runtime sort does, first normalize to
|
||||||
|
* being relative to the start of the section.
|
||||||
|
*/
|
||||||
|
while (i < image_size) {
|
||||||
|
uint32_t *loc = (uint32_t *)(extab_image + i);
|
||||||
|
w(r(loc) + i, loc);
|
||||||
|
i += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
qsort(extab_image, image_size / 8, 8, compare_relative_table);
|
||||||
|
|
||||||
|
/* Now denormalize. */
|
||||||
|
i = 0;
|
||||||
|
while (i < image_size) {
|
||||||
|
uint32_t *loc = (uint32_t *)(extab_image + i);
|
||||||
|
w(r(loc) - i, loc);
|
||||||
|
i += 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void x86_sort_relative_table(char *extab_image, int image_size)
|
static void x86_sort_relative_table(char *extab_image, int image_size)
|
||||||
{
|
{
|
||||||
int i;
|
int i = 0;
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (i < image_size) {
|
while (i < image_size) {
|
||||||
uint32_t *loc = (uint32_t *)(extab_image + i);
|
uint32_t *loc = (uint32_t *)(extab_image + i);
|
||||||
|
|
||||||
@@ -217,44 +251,13 @@ static void x86_sort_relative_table(char *extab_image, int image_size)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sort_relative_table(char *extab_image, int image_size)
|
static int do_file(char const *const fname, void *addr)
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Do the same thing the runtime sort does, first normalize to
|
|
||||||
* being relative to the start of the section.
|
|
||||||
*/
|
|
||||||
i = 0;
|
|
||||||
while (i < image_size) {
|
|
||||||
uint32_t *loc = (uint32_t *)(extab_image + i);
|
|
||||||
w(r(loc) + i, loc);
|
|
||||||
i += 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
qsort(extab_image, image_size / 8, 8, compare_relative_table);
|
|
||||||
|
|
||||||
/* Now denormalize. */
|
|
||||||
i = 0;
|
|
||||||
while (i < image_size) {
|
|
||||||
uint32_t *loc = (uint32_t *)(extab_image + i);
|
|
||||||
w(r(loc) - i, loc);
|
|
||||||
i += 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
do_file(char const *const fname, void *addr)
|
|
||||||
{
|
|
||||||
table_sort_t custom_sort = NULL;
|
|
||||||
Elf32_Ehdr *ehdr = addr;
|
|
||||||
int rc = -1;
|
int rc = -1;
|
||||||
|
Elf32_Ehdr *ehdr = addr;
|
||||||
|
table_sort_t custom_sort = NULL;
|
||||||
|
|
||||||
switch (ehdr->e_ident[EI_DATA]) {
|
switch (ehdr->e_ident[EI_DATA]) {
|
||||||
default:
|
|
||||||
fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
|
|
||||||
ehdr->e_ident[EI_DATA], fname);
|
|
||||||
return -1;
|
|
||||||
case ELFDATA2LSB:
|
case ELFDATA2LSB:
|
||||||
r = rle;
|
r = rle;
|
||||||
r2 = r2le;
|
r2 = r2le;
|
||||||
@@ -271,25 +274,24 @@ do_file(char const *const fname, void *addr)
|
|||||||
w2 = w2be;
|
w2 = w2be;
|
||||||
w8 = w8be;
|
w8 = w8be;
|
||||||
break;
|
break;
|
||||||
} /* end switch */
|
default:
|
||||||
if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0
|
fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
|
||||||
|| (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN)
|
ehdr->e_ident[EI_DATA], fname);
|
||||||
|| ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
|
||||||
|
(r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
|
||||||
|
ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
|
||||||
fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
|
fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
custom_sort = NULL;
|
|
||||||
switch (r2(&ehdr->e_machine)) {
|
switch (r2(&ehdr->e_machine)) {
|
||||||
default:
|
|
||||||
fprintf(stderr, "unrecognized e_machine %d %s\n",
|
|
||||||
r2(&ehdr->e_machine), fname);
|
|
||||||
break;
|
|
||||||
case EM_386:
|
case EM_386:
|
||||||
case EM_X86_64:
|
case EM_X86_64:
|
||||||
custom_sort = x86_sort_relative_table;
|
custom_sort = x86_sort_relative_table;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case EM_S390:
|
case EM_S390:
|
||||||
case EM_AARCH64:
|
case EM_AARCH64:
|
||||||
case EM_PARISC:
|
case EM_PARISC:
|
||||||
@@ -304,40 +306,45 @@ do_file(char const *const fname, void *addr)
|
|||||||
case EM_MIPS:
|
case EM_MIPS:
|
||||||
case EM_XTENSA:
|
case EM_XTENSA:
|
||||||
break;
|
break;
|
||||||
} /* end switch */
|
default:
|
||||||
|
fprintf(stderr, "unrecognized e_machine %d %s\n",
|
||||||
|
r2(&ehdr->e_machine), fname);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
switch (ehdr->e_ident[EI_CLASS]) {
|
switch (ehdr->e_ident[EI_CLASS]) {
|
||||||
default:
|
|
||||||
fprintf(stderr, "unrecognized ELF class %d %s\n",
|
|
||||||
ehdr->e_ident[EI_CLASS], fname);
|
|
||||||
break;
|
|
||||||
case ELFCLASS32:
|
case ELFCLASS32:
|
||||||
if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
|
if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
|
||||||
|| r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
|
r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
|
"unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
rc = do32(ehdr, fname, custom_sort);
|
rc = do32(ehdr, fname, custom_sort);
|
||||||
break;
|
break;
|
||||||
case ELFCLASS64: {
|
case ELFCLASS64:
|
||||||
|
{
|
||||||
Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
|
Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
|
||||||
if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
|
if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
|
||||||
|| r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
|
r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
|
"unrecognized ET_EXEC/ET_DYN file: %s\n",
|
||||||
|
fname);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
rc = do64(ghdr, fname, custom_sort);
|
rc = do64(ghdr, fname, custom_sort);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "unrecognized ELF class %d %s\n",
|
||||||
|
ehdr->e_ident[EI_CLASS], fname);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} /* end switch */
|
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int main(int argc, char *argv[])
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
{
|
||||||
int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
|
int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
@@ -361,5 +368,6 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
munmap(addr, size);
|
munmap(addr, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
return !!n_error;
|
return !!n_error;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,8 +87,9 @@ static int compare_extable(const void *a, const void *b)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int do_func(Elf_Ehdr *ehdr,
|
||||||
do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort)
|
char const *const fname,
|
||||||
|
table_sort_t custom_sort)
|
||||||
{
|
{
|
||||||
Elf_Shdr *shdr;
|
Elf_Shdr *shdr;
|
||||||
Elf_Shdr *shstrtab_sec;
|
Elf_Shdr *shstrtab_sec;
|
||||||
@@ -126,7 +127,7 @@ do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort)
|
|||||||
secstrtab = (const char *)ehdr + _r(&shstrtab_sec->sh_offset);
|
secstrtab = (const char *)ehdr + _r(&shstrtab_sec->sh_offset);
|
||||||
for (i = 0; i < num_sections; i++) {
|
for (i = 0; i < num_sections; i++) {
|
||||||
idx = r(&shdr[i].sh_name);
|
idx = r(&shdr[i].sh_name);
|
||||||
if (strcmp(secstrtab + idx, "__ex_table") == 0) {
|
if (!strcmp(secstrtab + idx, "__ex_table")) {
|
||||||
extab_sec = shdr + i;
|
extab_sec = shdr + i;
|
||||||
extab_index = i;
|
extab_index = i;
|
||||||
}
|
}
|
||||||
@@ -136,25 +137,25 @@ do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort)
|
|||||||
relocs = (void *)ehdr + _r(&shdr[i].sh_offset);
|
relocs = (void *)ehdr + _r(&shdr[i].sh_offset);
|
||||||
relocs_size = _r(&shdr[i].sh_size);
|
relocs_size = _r(&shdr[i].sh_size);
|
||||||
}
|
}
|
||||||
if (strcmp(secstrtab + idx, ".symtab") == 0)
|
if (!strcmp(secstrtab + idx, ".symtab"))
|
||||||
symtab_sec = shdr + i;
|
symtab_sec = shdr + i;
|
||||||
if (strcmp(secstrtab + idx, ".strtab") == 0)
|
if (!strcmp(secstrtab + idx, ".strtab"))
|
||||||
strtab_sec = shdr + i;
|
strtab_sec = shdr + i;
|
||||||
if (r(&shdr[i].sh_type) == SHT_SYMTAB_SHNDX)
|
if (r(&shdr[i].sh_type) == SHT_SYMTAB_SHNDX)
|
||||||
symtab_shndx_start = (Elf32_Word *)(
|
symtab_shndx_start = (Elf32_Word *)(
|
||||||
(const char *)ehdr + _r(&shdr[i].sh_offset));
|
(const char *)ehdr + _r(&shdr[i].sh_offset));
|
||||||
}
|
}
|
||||||
if (strtab_sec == NULL) {
|
if (!strtab_sec) {
|
||||||
fprintf(stderr, "no .strtab in file: %s\n", fname);
|
fprintf(stderr, "no .strtab in file: %s\n", fname);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (symtab_sec == NULL) {
|
if (!symtab_sec) {
|
||||||
fprintf(stderr, "no .symtab in file: %s\n", fname);
|
fprintf(stderr, "no .symtab in file: %s\n", fname);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
symtab = (const Elf_Sym *)((const char *)ehdr +
|
symtab = (const Elf_Sym *)((const char *)ehdr +
|
||||||
_r(&symtab_sec->sh_offset));
|
_r(&symtab_sec->sh_offset));
|
||||||
if (extab_sec == NULL) {
|
if (!extab_sec) {
|
||||||
fprintf(stderr, "no __ex_table in file: %s\n", fname);
|
fprintf(stderr, "no __ex_table in file: %s\n", fname);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -181,12 +182,12 @@ do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort)
|
|||||||
if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
|
if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
|
||||||
continue;
|
continue;
|
||||||
idx = r(&sym->st_name);
|
idx = r(&sym->st_name);
|
||||||
if (strcmp(strtab + idx, "main_extable_sort_needed") == 0) {
|
if (!strcmp(strtab + idx, "main_extable_sort_needed")) {
|
||||||
sort_needed_sym = sym;
|
sort_needed_sym = sym;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sort_needed_sym == NULL) {
|
if (!sort_needed_sym) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"no main_extable_sort_needed symbol in file: %s\n",
|
"no main_extable_sort_needed symbol in file: %s\n",
|
||||||
fname);
|
fname);
|
||||||
|
|||||||
Reference in New Issue
Block a user