^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * sorttable.c: Sort the kernel's table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Added ORC unwind tables sort support and other updates:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 1999-2019 Alibaba Group Holding Limited. by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Shile Zhang <shile.zhang@linux.alibaba.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright 2011 - 2012 Cavium, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Based on code taken from recortmcount.c which is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Restructured to fit Linux format, as well as other updates:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Strategy: alter the vmlinux file in-place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <tools/be_byteshift.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <tools/le_byteshift.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #ifndef EM_ARCOMPACT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define EM_ARCOMPACT 93
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #ifndef EM_XTENSA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define EM_XTENSA 94
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #ifndef EM_AARCH64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define EM_AARCH64 183
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #ifndef EM_MICROBLAZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define EM_MICROBLAZE 189
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #ifndef EM_ARCV2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define EM_ARCV2 195
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static uint32_t (*r)(const uint32_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static uint16_t (*r2)(const uint16_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static uint64_t (*r8)(const uint64_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static void (*w)(uint32_t, uint32_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static void (*w2)(uint16_t, uint16_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static void (*w8)(uint64_t, uint64_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) typedef void (*table_sort_t)(char *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * Get the whole file as a programming convenience in order to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * malloc+lseek+read+free of many pieces. If successful, then mmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * avoids copying unused pieces; else just read the whole file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * Open for both read and write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void *mmap_file(char const *fname, size_t *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct stat sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) void *addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) fd = open(fname, O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) perror(fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (fstat(fd, &sb) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) perror(fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!S_ISREG(sb.st_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) fprintf(stderr, "not a regular file: %s\n", fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (addr == MAP_FAILED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) fprintf(stderr, "Could not mmap file: %s\n", fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *size = sb.st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static uint32_t rbe(const uint32_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return get_unaligned_be32(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static uint16_t r2be(const uint16_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return get_unaligned_be16(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static uint64_t r8be(const uint64_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return get_unaligned_be64(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static uint32_t rle(const uint32_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return get_unaligned_le32(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static uint16_t r2le(const uint16_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return get_unaligned_le16(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static uint64_t r8le(const uint64_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return get_unaligned_le64(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static void wbe(uint32_t val, uint32_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) put_unaligned_be32(val, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static void w2be(uint16_t val, uint16_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) put_unaligned_be16(val, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static void w8be(uint64_t val, uint64_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) put_unaligned_be64(val, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static void wle(uint32_t val, uint32_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) put_unaligned_le32(val, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static void w2le(uint16_t val, uint16_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) put_unaligned_le16(val, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static void w8le(uint64_t val, uint64_t *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) put_unaligned_le64(val, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * the way to -256..-1, to avoid conflicting with real section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * indices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static inline int is_shndx_special(unsigned int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static inline unsigned int get_secindex(unsigned int shndx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) unsigned int sym_offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) const Elf32_Word *symtab_shndx_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (is_shndx_special(shndx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return SPECIAL(shndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (shndx != SHN_XINDEX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return shndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return r(&symtab_shndx_start[sym_offs]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* 32 bit and 64 bit are very similar */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #include "sorttable.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) #define SORTTABLE_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) #include "sorttable.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int compare_relative_table(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int32_t av = (int32_t)r(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int32_t bv = (int32_t)r(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (av < bv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (av > bv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static void sort_relative_table(char *extab_image, int image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * Do the same thing the runtime sort does, first normalize to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * being relative to the start of the section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) while (i < image_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) uint32_t *loc = (uint32_t *)(extab_image + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) w(r(loc) + i, loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) i += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) qsort(extab_image, image_size / 8, 8, compare_relative_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* Now denormalize. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) while (i < image_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) uint32_t *loc = (uint32_t *)(extab_image + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) w(r(loc) - i, loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) i += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static void x86_sort_relative_table(char *extab_image, int image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) while (i < image_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) uint32_t *loc = (uint32_t *)(extab_image + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) w(r(loc) + i, loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) w(r(loc + 1) + i + 4, loc + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) w(r(loc + 2) + i + 8, loc + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) i += sizeof(uint32_t) * 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) qsort(extab_image, image_size / 12, 12, compare_relative_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) while (i < image_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) uint32_t *loc = (uint32_t *)(extab_image + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) w(r(loc) - i, loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) w(r(loc + 1) - (i + 4), loc + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) w(r(loc + 2) - (i + 8), loc + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) i += sizeof(uint32_t) * 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static void s390_sort_relative_table(char *extab_image, int image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) for (i = 0; i < image_size; i += 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) char *loc = extab_image + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) uint64_t handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) w(r((uint32_t *)loc) + i, (uint32_t *)loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) w(r((uint32_t *)(loc + 4)) + (i + 4), (uint32_t *)(loc + 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * 0 is a special self-relative handler value, which means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * handler should be ignored. It is safe, because it means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * handler field points to itself, which should never happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * When creating extable-relative values, keep it as 0, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * this should never occur either: it would mean that handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * field points to the first extable entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) handler = r8((uint64_t *)(loc + 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) handler += i + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) w8(handler, (uint64_t *)(loc + 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) qsort(extab_image, image_size / 16, 16, compare_relative_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) for (i = 0; i < image_size; i += 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) char *loc = extab_image + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) uint64_t handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) w(r((uint32_t *)loc) - i, (uint32_t *)loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) w(r((uint32_t *)(loc + 4)) - (i + 4), (uint32_t *)(loc + 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) handler = r8((uint64_t *)(loc + 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) handler -= i + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) w8(handler, (uint64_t *)(loc + 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static int do_file(char const *const fname, void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) int rc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) Elf32_Ehdr *ehdr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) table_sort_t custom_sort = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) switch (ehdr->e_ident[EI_DATA]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) case ELFDATA2LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) r = rle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) r2 = r2le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) r8 = r8le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) w = wle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) w2 = w2le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) w8 = w8le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) case ELFDATA2MSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) r = rbe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) r2 = r2be;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) r8 = r8be;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) w = wbe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) w2 = w2be;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) w8 = w8be;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ehdr->e_ident[EI_DATA], fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) switch (r2(&ehdr->e_machine)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) case EM_386:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) case EM_X86_64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) custom_sort = x86_sort_relative_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) case EM_S390:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) custom_sort = s390_sort_relative_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) case EM_AARCH64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) case EM_PARISC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) case EM_PPC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) case EM_PPC64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) custom_sort = sort_relative_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) case EM_ARCOMPACT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) case EM_ARCV2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) case EM_ARM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) case EM_MICROBLAZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) case EM_MIPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) case EM_XTENSA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) fprintf(stderr, "unrecognized e_machine %d %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) r2(&ehdr->e_machine), fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) switch (ehdr->e_ident[EI_CLASS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) case ELFCLASS32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) rc = do_sort_32(ehdr, fname, custom_sort);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) case ELFCLASS64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) "unrecognized ET_EXEC/ET_DYN file: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) rc = do_sort_64(ghdr, fname, custom_sort);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) fprintf(stderr, "unrecognized ELF class %d %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) ehdr->e_ident[EI_CLASS], fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) size_t size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) void *addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (argc < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) fprintf(stderr, "usage: sorttable vmlinux...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* Process each file in turn, allowing deep failure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) for (i = 1; i < argc; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) addr = mmap_file(argv[i], &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (!addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) ++n_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (do_file(argv[i], addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ++n_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) munmap(addr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return !!n_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }