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 <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/objtool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <asm/orc_types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "check.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "warn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) int create_orc(struct objtool_file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	struct instruction *insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	for_each_insn(file, insn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		struct orc_entry *orc = &insn->orc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		struct cfi_reg *cfa = &insn->cfi.cfa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		struct cfi_reg *bp = &insn->cfi.regs[CFI_BP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		if (!insn->sec->text)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		orc->end = insn->cfi.end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		if (cfa->base == CFI_UNDEFINED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 			orc->sp_reg = ORC_REG_UNDEFINED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		switch (cfa->base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		case CFI_SP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 			orc->sp_reg = ORC_REG_SP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		case CFI_SP_INDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 			orc->sp_reg = ORC_REG_SP_INDIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		case CFI_BP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			orc->sp_reg = ORC_REG_BP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		case CFI_BP_INDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 			orc->sp_reg = ORC_REG_BP_INDIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		case CFI_R10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			orc->sp_reg = ORC_REG_R10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		case CFI_R13:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			orc->sp_reg = ORC_REG_R13;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		case CFI_DI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			orc->sp_reg = ORC_REG_DI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		case CFI_DX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			orc->sp_reg = ORC_REG_DX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			WARN_FUNC("unknown CFA base reg %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 				  insn->sec, insn->offset, cfa->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		switch(bp->base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		case CFI_UNDEFINED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			orc->bp_reg = ORC_REG_UNDEFINED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		case CFI_CFA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			orc->bp_reg = ORC_REG_PREV_SP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		case CFI_BP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			orc->bp_reg = ORC_REG_BP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			WARN_FUNC("unknown BP base reg %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 				  insn->sec, insn->offset, bp->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		orc->sp_offset = cfa->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		orc->bp_offset = bp->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		orc->type = insn->cfi.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static int create_orc_entry(struct elf *elf, struct section *u_sec, struct section *ip_relocsec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 				unsigned int idx, struct section *insn_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 				unsigned long insn_off, struct orc_entry *o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct orc_entry *orc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct reloc *reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	/* populate ORC data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	orc = (struct orc_entry *)u_sec->data->d_buf + idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	memcpy(orc, o, sizeof(*orc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	/* populate reloc for ip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	reloc = malloc(sizeof(*reloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (!reloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	memset(reloc, 0, sizeof(*reloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	insn_to_reloc_sym_addend(insn_sec, insn_off, reloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (!reloc->sym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		WARN("missing symbol for insn at offset 0x%lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		     insn_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	reloc->type = R_X86_64_PC32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	reloc->offset = idx * sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	reloc->sec = ip_relocsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	elf_add_reloc(elf, reloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return 0;
^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) int create_orc_sections(struct objtool_file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct instruction *insn, *prev_insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct section *sec, *u_sec, *ip_relocsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct orc_entry empty = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		.sp_reg = ORC_REG_UNDEFINED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		.bp_reg  = ORC_REG_UNDEFINED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		.type    = UNWIND_HINT_TYPE_CALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	sec = find_section_by_name(file->elf, ".orc_unwind");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		WARN("file already has .orc_unwind section, skipping");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	/* count the number of needed orcs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	for_each_sec(file, sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (!sec->text)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		prev_insn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		sec_for_each_insn(file, sec, insn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			if (!prev_insn ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			    memcmp(&insn->orc, &prev_insn->orc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				   sizeof(struct orc_entry))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			prev_insn = insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		/* section terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		if (prev_insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (!idx)
^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) 	/* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (!sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	ip_relocsec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (!ip_relocsec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	/* create .orc_unwind section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	u_sec = elf_create_section(file->elf, ".orc_unwind", 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 				   sizeof(struct orc_entry), idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	/* populate sections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	for_each_sec(file, sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		if (!sec->text)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		prev_insn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		sec_for_each_insn(file, sec, insn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 						 sizeof(struct orc_entry))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				if (create_orc_entry(file->elf, u_sec, ip_relocsec, idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 						     insn->sec, insn->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 						     &insn->orc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 					return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			prev_insn = insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		/* section terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (prev_insn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			if (create_orc_entry(file->elf, u_sec, ip_relocsec, idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 					     prev_insn->sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 					     prev_insn->offset + prev_insn->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 					     &empty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (elf_rebuild_reloc_section(file->elf, ip_relocsec))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }