Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp4142627ybc; Thu, 14 Nov 2019 22:50:18 -0800 (PST) X-Google-Smtp-Source: APXvYqwG+Ljpzsn2tXhjNcohcsHFMCi5kn9DdJaoDp99y4rqSh2iAK7Ah4VTehMcTzpme0FPqhNp X-Received: by 2002:a17:906:3393:: with SMTP id v19mr11967338eja.117.1573800618363; Thu, 14 Nov 2019 22:50:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573800618; cv=none; d=google.com; s=arc-20160816; b=bhLVq2MYmuRgyJostNktJj/SQtJm3g1CfOG0XIbNkWQ9Klf+EiaEeDSI00JQJqgu4X xnr3W4ze7Dzu1XRktaUAw0PtYSkJbuGljzOqxaNDCV3Y1NV+VoV0hteC/JE/6oc0zf2I PVuALwu2ydVeKfTwOHdsdCcIf68p1uNIfHg13EYH+AOV+8CrVbA0nVc0/Yt3AKLsjLB0 MByumcEsKBToJXKz3/nnUsMuXj1YPGwcrAgXU4DbsluU1U1j7u3lH2JAHLkDTQuUs9n8 IhJ/Hnz9lvrI9QwU7tWRSTRyggKF8vHBow8KNowuBEsKDaXD6sW1VF7Tx+uX4bvKGwB0 V9lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=9Oc3td7D0DfjFgozQ56QnD4jFxvk0k/iR0Ybr8Z8Msw=; b=ipvb8jUX708txMSCNuSjLyfDbqoPACb2+stZq5QGTMvVZ+seJjf+ATzK4Btu/oRv11 BVFhT4dwVSvx0z1jeQuwGahlB1Hf+BLdbFLHeV5/+Tp5UE1wjBnQQWZxwam2tRvXJbNZ Sorl8HY4shtIrXhJg4xFUkH9KQYApK0XGKGYKY/zH1JH3+MkUXGKYUQx37TPh6jsejWP PzPKCdopAJwD7068E4yOEfrzbncs/pBbmiNFNoWPwItP0RBiedL0Y3SY6SicKmkXtiP9 tKeVHgs1YoVUxevZtyv4ZEXJgAOWKrHNfJLAtmnO8QotOHLOHo+whC/J0Oo4CqUzYEVx B+GA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n12si5725627edr.448.2019.11.14.22.49.52; Thu, 14 Nov 2019 22:50:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727220AbfKOGsV (ORCPT + 99 others); Fri, 15 Nov 2019 01:48:21 -0500 Received: from out4436.biz.mail.alibaba.com ([47.88.44.36]:26050 "EHLO out4436.biz.mail.alibaba.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727096AbfKOGsU (ORCPT ); Fri, 15 Nov 2019 01:48:20 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R331e4;CH=green;DM=||false|;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e07486;MF=shile.zhang@linux.alibaba.com;NM=1;PH=DS;RN=12;SR=0;TI=SMTPD_---0Ti7ujx6_1573800472; Received: from e18g09479.et15sqa.tbsite.net(mailfrom:shile.zhang@linux.alibaba.com fp:SMTPD_---0Ti7ujx6_1573800472) by smtp.aliyun-inc.com(127.0.0.1); Fri, 15 Nov 2019 14:48:06 +0800 From: Shile Zhang To: Peter Zijlstra , Josh Poimboeuf , Masahiro Yamada , Michal Marek , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org Cc: "H . Peter Anvin" , linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, Shile Zhang Subject: [RFC PATCH v3 2/7] scripts/sortextable: kernel coding style formating Date: Fri, 15 Nov 2019 14:47:45 +0800 Message-Id: <20191115064750.47888-3-shile.zhang@linux.alibaba.com> X-Mailer: git-send-email 2.24.0.rc2 In-Reply-To: <20191115064750.47888-1-shile.zhang@linux.alibaba.com> References: <20191115064750.47888-1-shile.zhang@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix the inconsistent function format and kernel code style, referred to commit 3aec8638246f ("recordmcount: Kernel style function signature formatting") and commit 2e63152bc190 ("recordmcount: Kernel style formatting") Make the code more readable and extendable, no functional changes. Signed-off-by: Shile Zhang --- scripts/sortextable.c | 182 ++++++++++++++++++++++-------------------- scripts/sortextable.h | 31 +++---- 2 files changed, 111 insertions(+), 102 deletions(-) diff --git a/scripts/sortextable.c b/scripts/sortextable.c index cd9762ba4467..e5384e86b58c 100644 --- a/scripts/sortextable.c +++ b/scripts/sortextable.c @@ -50,6 +50,14 @@ #define EM_ARCV2 195 #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 * 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); goto out; } + addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (addr == MAP_FAILED) { fprintf(stderr, "Could not mmap file: %s\n", fname); @@ -88,64 +97,65 @@ static void *mmap_file(char const *fname, size_t *size) return addr; } -static uint64_t r8be(const uint64_t *x) -{ - return get_unaligned_be64(x); -} static uint32_t rbe(const uint32_t *x) { return get_unaligned_be32(x); } + static uint16_t r2be(const uint16_t *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) { return get_unaligned_le32(x); } + static uint16_t r2le(const uint16_t *x) { return get_unaligned_le16(x); } -static void w8be(uint64_t val, uint64_t *x) +static uint64_t r8le(const uint64_t *x) { - put_unaligned_be64(val, 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 w8le(uint64_t val, uint64_t *x) + +static void w8be(uint64_t val, uint64_t *x) { - put_unaligned_le64(val, x); + put_unaligned_be64(val, x); } + static void wle(uint32_t val, uint32_t *x) { put_unaligned_le32(val, x); } + static void w2le(uint16_t val, uint16_t *x) { put_unaligned_le16(val, x); } -static uint64_t (*r8)(const uint64_t *); -static uint32_t (*r)(const uint32_t *); -static uint16_t (*r2)(const uint16_t *); -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); +static void w8le(uint64_t val, uint64_t *x) +{ + put_unaligned_le64(val, x); +} /* * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of @@ -188,108 +198,100 @@ static int compare_relative_table(const void *a, const void *b) return 0; } -static void x86_sort_relative_table(char *extab_image, int image_size) +static void sort_relative_table(char *extab_image, int image_size) { - int i; + int i = 0; - 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); - w(r(loc + 1) + i + 4, loc + 1); - w(r(loc + 2) + i + 8, loc + 2); - - i += sizeof(uint32_t) * 3; + i += 4; } - qsort(extab_image, image_size / 12, 12, compare_relative_table); + 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); - w(r(loc + 1) - (i + 4), loc + 1); - w(r(loc + 2) - (i + 8), loc + 2); - - i += sizeof(uint32_t) * 3; + i += 4; } } -static void 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; - /* - * 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; + w(r(loc + 1) + i + 4, loc + 1); + w(r(loc + 2) + i + 8, loc + 2); + + i += sizeof(uint32_t) * 3; } - qsort(extab_image, image_size / 8, 8, compare_relative_table); + qsort(extab_image, image_size / 12, 12, 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; + w(r(loc + 1) - (i + 4), loc + 1); + w(r(loc + 2) - (i + 8), loc + 2); + + i += sizeof(uint32_t) * 3; } } -static int -do_file(char const *const fname, void *addr) +static int do_file(char const *const fname, void *addr) { - table_sort_t custom_sort = NULL; - Elf32_Ehdr *ehdr = addr; int rc = -1; + Elf32_Ehdr *ehdr = addr; + table_sort_t custom_sort = NULL; 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: - r = rle; - r2 = r2le; - r8 = r8le; - w = wle; - w2 = w2le; - w8 = w8le; + r = rle; + r2 = r2le; + r8 = r8le; + w = wle; + w2 = w2le; + w8 = w8le; break; case ELFDATA2MSB: - r = rbe; - r2 = r2be; - r8 = r8be; - w = wbe; - w2 = w2be; - w8 = w8be; + r = rbe; + r2 = r2be; + r8 = r8be; + w = wbe; + w2 = w2be; + w8 = w8be; break; - } /* end switch */ - 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) { + default: + fprintf(stderr, "unrecognized ELF data encoding %d: %s\n", + ehdr->e_ident[EI_DATA], fname); + 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); return -1; } - custom_sort = NULL; 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_X86_64: custom_sort = x86_sort_relative_table; break; - case EM_S390: case EM_AARCH64: case EM_PARISC: @@ -304,40 +306,45 @@ do_file(char const *const fname, void *addr) case EM_MIPS: case EM_XTENSA: 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]) { - default: - fprintf(stderr, "unrecognized ELF class %d %s\n", - ehdr->e_ident[EI_CLASS], fname); - break; case ELFCLASS32: - if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) - || r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) { + if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) || + r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) { fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file: %s\n", fname); break; } rc = do32(ehdr, fname, custom_sort); break; - case ELFCLASS64: { + case ELFCLASS64: + { Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr; - if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) - || r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) { + if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) || + r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) { fprintf(stderr, - "unrecognized ET_EXEC/ET_DYN file: %s\n", fname); + "unrecognized ET_EXEC/ET_DYN file: %s\n", + fname); break; } rc = do64(ghdr, fname, custom_sort); + } + break; + default: + fprintf(stderr, "unrecognized ELF class %d %s\n", + ehdr->e_ident[EI_CLASS], fname); break; } - } /* end switch */ return rc; } -int -main(int argc, char *argv[]) +int main(int argc, char *argv[]) { int i, n_error = 0; /* gcc-4.3.0 false positive complaint */ size_t size = 0; @@ -361,5 +368,6 @@ main(int argc, char *argv[]) munmap(addr, size); } + return !!n_error; } diff --git a/scripts/sortextable.h b/scripts/sortextable.h index 5a62e94df678..b7e407e09f59 100644 --- a/scripts/sortextable.h +++ b/scripts/sortextable.h @@ -6,7 +6,7 @@ * * Some of this code was taken out of recordmcount.h written by: * - * Copyright 2009 John F. Reiser . All rights reserved. + * Copyright 2009 John F. Reiser . All rights reserved. * Copyright 2010 Steven Rostedt , Red Hat Inc. */ @@ -87,8 +87,9 @@ static int compare_extable(const void *a, const void *b) return 0; } -static int -do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort) +static int do_func(Elf_Ehdr *ehdr, + char const *const fname, + table_sort_t custom_sort) { Elf_Shdr *shdr; 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); for (i = 0; i < num_sections; i++) { idx = r(&shdr[i].sh_name); - if (strcmp(secstrtab + idx, "__ex_table") == 0) { + if (!strcmp(secstrtab + idx, "__ex_table")) { extab_sec = shdr + i; extab_index = i; } @@ -136,26 +137,26 @@ do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort) relocs = (void *)ehdr + _r(&shdr[i].sh_offset); relocs_size = _r(&shdr[i].sh_size); } - if (strcmp(secstrtab + idx, ".symtab") == 0) + if (!strcmp(secstrtab + idx, ".symtab")) symtab_sec = shdr + i; - if (strcmp(secstrtab + idx, ".strtab") == 0) + if (!strcmp(secstrtab + idx, ".strtab")) strtab_sec = shdr + i; if (r(&shdr[i].sh_type) == SHT_SYMTAB_SHNDX) symtab_shndx_start = (Elf32_Word *)( (const char *)ehdr + _r(&shdr[i].sh_offset)); } - if (strtab_sec == NULL) { - fprintf(stderr, "no .strtab in file: %s\n", fname); + if (!strtab_sec) { + fprintf(stderr, "no .strtab in file: %s\n", fname); return -1; } - if (symtab_sec == NULL) { - fprintf(stderr, "no .symtab in file: %s\n", fname); + if (!symtab_sec) { + fprintf(stderr, "no .symtab in file: %s\n", fname); return -1; } symtab = (const Elf_Sym *)((const char *)ehdr + _r(&symtab_sec->sh_offset)); - if (extab_sec == NULL) { - fprintf(stderr, "no __ex_table in file: %s\n", fname); + if (!extab_sec) { + fprintf(stderr, "no __ex_table in file: %s\n", fname); return -1; } strtab = (const char *)ehdr + _r(&strtab_sec->sh_offset); @@ -181,14 +182,14 @@ do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort) if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT) continue; 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; break; } } - if (sort_needed_sym == NULL) { + if (!sort_needed_sym) { fprintf(stderr, - "no main_extable_sort_needed symbol in file: %s\n", + "no main_extable_sort_needed symbol in file: %s\n", fname); return -1; } -- 2.24.0.rc2