Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/objtool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <asm/orc_types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "objtool.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "warn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) static const char *reg_name(unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	case ORC_REG_PREV_SP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 		return "prevsp";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	case ORC_REG_DX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 		return "dx";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	case ORC_REG_DI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		return "di";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	case ORC_REG_BP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		return "bp";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	case ORC_REG_SP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		return "sp";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	case ORC_REG_R10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		return "r10";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	case ORC_REG_R13:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		return "r13";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	case ORC_REG_BP_INDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		return "bp(ind)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	case ORC_REG_SP_INDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		return "sp(ind)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		return "?";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static const char *orc_type_name(unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	case UNWIND_HINT_TYPE_CALL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		return "call";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	case UNWIND_HINT_TYPE_REGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return "regs";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	case UNWIND_HINT_TYPE_REGS_PARTIAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return "regs (partial)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		return "?";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static void print_reg(unsigned int reg, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (reg == ORC_REG_BP_INDIRECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		printf("(bp%+d)", offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	else if (reg == ORC_REG_SP_INDIRECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		printf("(sp%+d)", offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	else if (reg == ORC_REG_UNDEFINED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		printf("(und)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		printf("%s%+d", reg_name(reg), offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) int orc_dump(const char *_objname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	int fd, nr_entries, i, *orc_ip = NULL, orc_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct orc_entry *orc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	size_t nr_sections;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	Elf64_Addr orc_ip_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	size_t shstrtab_idx, strtab_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	Elf *elf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	Elf_Scn *scn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	GElf_Shdr sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	GElf_Rela rela;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	GElf_Sym sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	objname = _objname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	elf_version(EV_CURRENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	fd = open(objname, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (fd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		perror("open");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (!elf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		WARN_ELF("elf_begin");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (elf_getshdrnum(elf, &nr_sections)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		WARN_ELF("elf_getshdrnum");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (elf_getshdrstrndx(elf, &shstrtab_idx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		WARN_ELF("elf_getshdrstrndx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	for (i = 0; i < nr_sections; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		scn = elf_getscn(elf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (!scn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			WARN_ELF("elf_getscn");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		if (!gelf_getshdr(scn, &sh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			WARN_ELF("gelf_getshdr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			WARN_ELF("elf_strptr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			return -1;
^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) 		data = elf_getdata(scn, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			WARN_ELF("elf_getdata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		if (!strcmp(name, ".symtab")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			symtab = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		} else if (!strcmp(name, ".strtab")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			strtab_idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		} else if (!strcmp(name, ".orc_unwind")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			orc = data->d_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			orc_size = sh.sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		} else if (!strcmp(name, ".orc_unwind_ip")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			orc_ip = data->d_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			orc_ip_addr = sh.sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		} else if (!strcmp(name, ".rela.orc_unwind_ip")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			rela_orc_ip = data;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (!symtab || !strtab_idx || !orc || !orc_ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (orc_size % sizeof(*orc) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		WARN("bad .orc_unwind section size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	nr_entries = orc_size / sizeof(*orc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	for (i = 0; i < nr_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		if (rela_orc_ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			if (!gelf_getrela(rela_orc_ip, i, &rela)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				WARN_ELF("gelf_getrela");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			if (!gelf_getsym(symtab, GELF_R_SYM(rela.r_info), &sym)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				WARN_ELF("gelf_getsym");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 				scn = elf_getscn(elf, sym.st_shndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 				if (!scn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 					WARN_ELF("elf_getscn");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 					return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				if (!gelf_getshdr(scn, &sh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 					WARN_ELF("gelf_getshdr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 					return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 				name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 				if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 					WARN_ELF("elf_strptr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 					return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				name = elf_strptr(elf, strtab_idx, sym.st_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 				if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 					WARN_ELF("elf_strptr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 					return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			printf("%s+%llx:", name, (unsigned long long)rela.r_addend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			printf("%llx:", (unsigned long long)(orc_ip_addr + (i * sizeof(int)) + orc_ip[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		printf(" sp:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		print_reg(orc[i].sp_reg, orc[i].sp_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		printf(" bp:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		print_reg(orc[i].bp_reg, orc[i].bp_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		printf(" type:%s end:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		       orc_type_name(orc[i].type), orc[i].end);
^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) 	elf_end(elf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }