diff options
| author | David Daney <david.daney@cavium.com> | 2012-04-19 17:59:55 -0400 |
|---|---|---|
| committer | H. Peter Anvin <hpa@linux.intel.com> | 2012-04-19 18:06:55 -0400 |
| commit | a79f248b9b309ebb5f34ca6a8fd1eb9e18db5720 (patch) | |
| tree | bab3fe4ac76370948b4ca62b41253c488ca96c38 /scripts | |
| parent | e816b57a337ea3b755de72bec38c10c864f23015 (diff) | |
scripts: Add sortextable to sort the kernel's exception table.
Using this build-time sort saves time booting as we don't have to burn
cycles sorting the exception table.
Signed-off-by: David Daney <david.daney@cavium.com>
Link: http://lkml.kernel.org/r/1334872799-14589-2-git-send-email-ddaney.cavm@gmail.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/.gitignore | 1 | ||||
| -rw-r--r-- | scripts/Makefile | 1 | ||||
| -rw-r--r-- | scripts/sortextable.c | 271 | ||||
| -rw-r--r-- | scripts/sortextable.h | 168 |
4 files changed, 441 insertions, 0 deletions
diff --git a/scripts/.gitignore b/scripts/.gitignore index 105b21f08185..65f362d931b5 100644 --- a/scripts/.gitignore +++ b/scripts/.gitignore | |||
| @@ -9,3 +9,4 @@ unifdef | |||
| 9 | ihex2fw | 9 | ihex2fw |
| 10 | recordmcount | 10 | recordmcount |
| 11 | docproc | 11 | docproc |
| 12 | sortextable | ||
diff --git a/scripts/Makefile b/scripts/Makefile index df7678febf27..43e19b9fc641 100644 --- a/scripts/Makefile +++ b/scripts/Makefile | |||
| @@ -13,6 +13,7 @@ hostprogs-$(CONFIG_LOGO) += pnmtologo | |||
| 13 | hostprogs-$(CONFIG_VT) += conmakehash | 13 | hostprogs-$(CONFIG_VT) += conmakehash |
| 14 | hostprogs-$(CONFIG_IKCONFIG) += bin2c | 14 | hostprogs-$(CONFIG_IKCONFIG) += bin2c |
| 15 | hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount | 15 | hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount |
| 16 | hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable | ||
| 16 | 17 | ||
| 17 | always := $(hostprogs-y) $(hostprogs-m) | 18 | always := $(hostprogs-y) $(hostprogs-m) |
| 18 | 19 | ||
diff --git a/scripts/sortextable.c b/scripts/sortextable.c new file mode 100644 index 000000000000..f51f1d43da63 --- /dev/null +++ b/scripts/sortextable.c | |||
| @@ -0,0 +1,271 @@ | |||
| 1 | /* | ||
| 2 | * sortextable.c: Sort the kernel's exception table | ||
| 3 | * | ||
| 4 | * Copyright 2011 Cavium, Inc. | ||
| 5 | * | ||
| 6 | * Based on code taken from recortmcount.c which is: | ||
| 7 | * | ||
| 8 | * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved. | ||
| 9 | * Licensed under the GNU General Public License, version 2 (GPLv2). | ||
| 10 | * | ||
| 11 | * Restructured to fit Linux format, as well as other updates: | ||
| 12 | * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc. | ||
| 13 | */ | ||
| 14 | |||
| 15 | /* | ||
| 16 | * Strategy: alter the vmlinux file in-place. | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include <sys/types.h> | ||
| 20 | #include <sys/mman.h> | ||
| 21 | #include <sys/stat.h> | ||
| 22 | #include <getopt.h> | ||
| 23 | #include <elf.h> | ||
| 24 | #include <fcntl.h> | ||
| 25 | #include <setjmp.h> | ||
| 26 | #include <stdio.h> | ||
| 27 | #include <stdlib.h> | ||
| 28 | #include <string.h> | ||
| 29 | #include <unistd.h> | ||
| 30 | |||
| 31 | static int fd_map; /* File descriptor for file being modified. */ | ||
| 32 | static int mmap_failed; /* Boolean flag. */ | ||
| 33 | static void *ehdr_curr; /* current ElfXX_Ehdr * for resource cleanup */ | ||
| 34 | static struct stat sb; /* Remember .st_size, etc. */ | ||
| 35 | static jmp_buf jmpenv; /* setjmp/longjmp per-file error escape */ | ||
| 36 | |||
| 37 | /* setjmp() return values */ | ||
| 38 | enum { | ||
| 39 | SJ_SETJMP = 0, /* hardwired first return */ | ||
| 40 | SJ_FAIL, | ||
| 41 | SJ_SUCCEED | ||
| 42 | }; | ||
| 43 | |||
| 44 | /* Per-file resource cleanup when multiple files. */ | ||
| 45 | static void | ||
| 46 | cleanup(void) | ||
| 47 | { | ||
| 48 | if (!mmap_failed) | ||
| 49 | munmap(ehdr_curr, sb.st_size); | ||
| 50 | close(fd_map); | ||
| 51 | } | ||
| 52 | |||
| 53 | static void __attribute__((noreturn)) | ||
| 54 | fail_file(void) | ||
| 55 | { | ||
| 56 | cleanup(); | ||
| 57 | longjmp(jmpenv, SJ_FAIL); | ||
| 58 | } | ||
| 59 | |||
| 60 | static void __attribute__((noreturn)) | ||
| 61 | succeed_file(void) | ||
| 62 | { | ||
| 63 | cleanup(); | ||
| 64 | longjmp(jmpenv, SJ_SUCCEED); | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | /* | ||
| 69 | * Get the whole file as a programming convenience in order to avoid | ||
| 70 | * malloc+lseek+read+free of many pieces. If successful, then mmap | ||
| 71 | * avoids copying unused pieces; else just read the whole file. | ||
| 72 | * Open for both read and write. | ||
| 73 | */ | ||
| 74 | static void *mmap_file(char const *fname) | ||
| 75 | { | ||
| 76 | void *addr; | ||
| 77 | |||
| 78 | fd_map = open(fname, O_RDWR); | ||
| 79 | if (fd_map < 0 || fstat(fd_map, &sb) < 0) { | ||
| 80 | perror(fname); | ||
| 81 | fail_file(); | ||
| 82 | } | ||
| 83 | if (!S_ISREG(sb.st_mode)) { | ||
| 84 | fprintf(stderr, "not a regular file: %s\n", fname); | ||
| 85 | fail_file(); | ||
| 86 | } | ||
| 87 | addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, | ||
| 88 | fd_map, 0); | ||
| 89 | if (addr == MAP_FAILED) { | ||
| 90 | mmap_failed = 1; | ||
| 91 | fprintf(stderr, "Could not mmap file: %s\n", fname); | ||
| 92 | fail_file(); | ||
| 93 | } | ||
| 94 | return addr; | ||
| 95 | } | ||
| 96 | |||
| 97 | /* w8rev, w8nat, ...: Handle endianness. */ | ||
| 98 | |||
| 99 | static uint64_t w8rev(uint64_t const x) | ||
| 100 | { | ||
| 101 | return ((0xff & (x >> (0 * 8))) << (7 * 8)) | ||
| 102 | | ((0xff & (x >> (1 * 8))) << (6 * 8)) | ||
| 103 | | ((0xff & (x >> (2 * 8))) << (5 * 8)) | ||
| 104 | | ((0xff & (x >> (3 * 8))) << (4 * 8)) | ||
| 105 | | ((0xff & (x >> (4 * 8))) << (3 * 8)) | ||
| 106 | | ((0xff & (x >> (5 * 8))) << (2 * 8)) | ||
| 107 | | ((0xff & (x >> (6 * 8))) << (1 * 8)) | ||
| 108 | | ((0xff & (x >> (7 * 8))) << (0 * 8)); | ||
| 109 | } | ||
| 110 | |||
| 111 | static uint32_t w4rev(uint32_t const x) | ||
| 112 | { | ||
| 113 | return ((0xff & (x >> (0 * 8))) << (3 * 8)) | ||
| 114 | | ((0xff & (x >> (1 * 8))) << (2 * 8)) | ||
| 115 | | ((0xff & (x >> (2 * 8))) << (1 * 8)) | ||
| 116 | | ((0xff & (x >> (3 * 8))) << (0 * 8)); | ||
| 117 | } | ||
| 118 | |||
| 119 | static uint32_t w2rev(uint16_t const x) | ||
| 120 | { | ||
| 121 | return ((0xff & (x >> (0 * 8))) << (1 * 8)) | ||
| 122 | | ((0xff & (x >> (1 * 8))) << (0 * 8)); | ||
| 123 | } | ||
| 124 | |||
| 125 | static uint64_t w8nat(uint64_t const x) | ||
| 126 | { | ||
| 127 | return x; | ||
| 128 | } | ||
| 129 | |||
| 130 | static uint32_t w4nat(uint32_t const x) | ||
| 131 | { | ||
| 132 | return x; | ||
| 133 | } | ||
| 134 | |||
| 135 | static uint32_t w2nat(uint16_t const x) | ||
| 136 | { | ||
| 137 | return x; | ||
| 138 | } | ||
| 139 | |||
| 140 | static uint64_t (*w8)(uint64_t); | ||
| 141 | static uint32_t (*w)(uint32_t); | ||
| 142 | static uint32_t (*w2)(uint16_t); | ||
| 143 | |||
| 144 | |||
| 145 | /* 32 bit and 64 bit are very similar */ | ||
| 146 | #include "sortextable.h" | ||
| 147 | #define SORTEXTABLE_64 | ||
| 148 | #include "sortextable.h" | ||
| 149 | |||
| 150 | |||
| 151 | static void | ||
| 152 | do_file(char const *const fname) | ||
| 153 | { | ||
| 154 | Elf32_Ehdr *const ehdr = mmap_file(fname); | ||
| 155 | |||
| 156 | ehdr_curr = ehdr; | ||
| 157 | w = w4nat; | ||
| 158 | w2 = w2nat; | ||
| 159 | w8 = w8nat; | ||
| 160 | switch (ehdr->e_ident[EI_DATA]) { | ||
| 161 | static unsigned int const endian = 1; | ||
| 162 | default: | ||
| 163 | fprintf(stderr, "unrecognized ELF data encoding %d: %s\n", | ||
| 164 | ehdr->e_ident[EI_DATA], fname); | ||
| 165 | fail_file(); | ||
| 166 | break; | ||
| 167 | case ELFDATA2LSB: | ||
| 168 | if (*(unsigned char const *)&endian != 1) { | ||
| 169 | /* main() is big endian, file.o is little endian. */ | ||
| 170 | w = w4rev; | ||
| 171 | w2 = w2rev; | ||
| 172 | w8 = w8rev; | ||
| 173 | } | ||
| 174 | break; | ||
| 175 | case ELFDATA2MSB: | ||
| 176 | if (*(unsigned char const *)&endian != 0) { | ||
| 177 | /* main() is little endian, file.o is big endian. */ | ||
| 178 | w = w4rev; | ||
| 179 | w2 = w2rev; | ||
