Commit 86ea7f36 authored by Christophe Leroy's avatar Christophe Leroy Committed by Michael Ellerman
Browse files

objtool: Use target file class size instead of a compiled constant



In order to allow using objtool on cross-built kernels,
determine size of long from elf data instead of using
sizeof(long) at build time.

For the time being this covers only mcount.

[Sathvika Vasireddy: Rename variable "size" to "addrsize" and function
"elf_class_size()" to "elf_class_addrsize()", and modify
create_mcount_loc_sections() function to follow reverse christmas tree
format to order local variable declarations.]

Tested-by: default avatarNaveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Reviewed-by: default avatarNaveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Acked-by: default avatarJosh Poimboeuf <jpoimboe@kernel.org>
Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: default avatarChristophe Leroy <christophe.leroy@csgroup.eu>
Signed-off-by: default avatarSathvika Vasireddy <sv@linux.ibm.com>
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20221114175754.1131267-11-sv@linux.ibm.com
parent 0646c28b
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -852,9 +852,9 @@ static int create_ibt_endbr_seal_sections(struct objtool_file *file)

static int create_mcount_loc_sections(struct objtool_file *file)
{
	struct section *sec;
	unsigned long *loc;
	int addrsize = elf_class_addrsize(file->elf);
	struct instruction *insn;
	struct section *sec;
	int idx;

	sec = find_section_by_name(file->elf, "__mcount_loc");
@@ -871,23 +871,25 @@ static int create_mcount_loc_sections(struct objtool_file *file)
	list_for_each_entry(insn, &file->mcount_loc_list, call_node)
		idx++;

	sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
	sec = elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx);
	if (!sec)
		return -1;

	sec->sh.sh_addralign = addrsize;

	idx = 0;
	list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
		void *loc;

		loc = (unsigned long *)sec->data->d_buf + idx;
		memset(loc, 0, sizeof(unsigned long));
		loc = sec->data->d_buf + idx;
		memset(loc, 0, addrsize);

		if (elf_add_reloc_to_insn(file->elf, sec,
					  idx * sizeof(unsigned long),
		if (elf_add_reloc_to_insn(file->elf, sec, idx,
					  R_X86_64_64,
					  insn->sec, insn->offset))
			return -1;

		idx++;
		idx += addrsize;
	}

	return 0;
+6 −2
Original line number Diff line number Diff line
@@ -1129,6 +1129,7 @@ static struct section *elf_create_rela_reloc_section(struct elf *elf, struct sec
{
	char *relocname;
	struct section *sec;
	int addrsize = elf_class_addrsize(elf);

	relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
	if (!relocname) {
@@ -1138,6 +1139,9 @@ static struct section *elf_create_rela_reloc_section(struct elf *elf, struct sec
	strcpy(relocname, ".rela");
	strcat(relocname, base->name);

	if (addrsize == sizeof(u32))
		sec = elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 0);
	else
		sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
	free(relocname);
	if (!sec)
@@ -1147,7 +1151,7 @@ static struct section *elf_create_rela_reloc_section(struct elf *elf, struct sec
	sec->base = base;

	sec->sh.sh_type = SHT_RELA;
	sec->sh.sh_addralign = 8;
	sec->sh.sh_addralign = addrsize;
	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
	sec->sh.sh_info = base->idx;
	sec->sh.sh_flags = SHF_INFO_LINK;
+8 −0
Original line number Diff line number Diff line
@@ -142,6 +142,14 @@ static inline bool has_multiple_files(struct elf *elf)
	return elf->num_files > 1;
}

static inline int elf_class_addrsize(struct elf *elf)
{
	if (elf->ehdr.e_ident[EI_CLASS] == ELFCLASS32)
		return sizeof(u32);
	else
		return sizeof(u64);
}

struct elf *elf_open_read(const char *name, int flags);
struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr);