Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Reviewed-by: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Signed-off-by: Sathvika Vasireddy <sv@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20221114175754.1131267-11-sv@linux.ibm.com

authored by

Christophe Leroy and committed by
Michael Ellerman
86ea7f36 0646c28b

+24 -10
+10 -8
tools/objtool/check.c
··· 852 852 853 853 static int create_mcount_loc_sections(struct objtool_file *file) 854 854 { 855 - struct section *sec; 856 - unsigned long *loc; 855 + int addrsize = elf_class_addrsize(file->elf); 857 856 struct instruction *insn; 857 + struct section *sec; 858 858 int idx; 859 859 860 860 sec = find_section_by_name(file->elf, "__mcount_loc"); ··· 871 871 list_for_each_entry(insn, &file->mcount_loc_list, call_node) 872 872 idx++; 873 873 874 - sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx); 874 + sec = elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx); 875 875 if (!sec) 876 876 return -1; 877 877 878 + sec->sh.sh_addralign = addrsize; 879 + 878 880 idx = 0; 879 881 list_for_each_entry(insn, &file->mcount_loc_list, call_node) { 882 + void *loc; 880 883 881 - loc = (unsigned long *)sec->data->d_buf + idx; 882 - memset(loc, 0, sizeof(unsigned long)); 884 + loc = sec->data->d_buf + idx; 885 + memset(loc, 0, addrsize); 883 886 884 - if (elf_add_reloc_to_insn(file->elf, sec, 885 - idx * sizeof(unsigned long), 887 + if (elf_add_reloc_to_insn(file->elf, sec, idx, 886 888 R_X86_64_64, 887 889 insn->sec, insn->offset)) 888 890 return -1; 889 891 890 - idx++; 892 + idx += addrsize; 891 893 } 892 894 893 895 return 0;
+6 -2
tools/objtool/elf.c
··· 1129 1129 { 1130 1130 char *relocname; 1131 1131 struct section *sec; 1132 + int addrsize = elf_class_addrsize(elf); 1132 1133 1133 1134 relocname = malloc(strlen(base->name) + strlen(".rela") + 1); 1134 1135 if (!relocname) { ··· 1139 1138 strcpy(relocname, ".rela"); 1140 1139 strcat(relocname, base->name); 1141 1140 1142 - sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0); 1141 + if (addrsize == sizeof(u32)) 1142 + sec = elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 0); 1143 + else 1144 + sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0); 1143 1145 free(relocname); 1144 1146 if (!sec) 1145 1147 return NULL; ··· 1151 1147 sec->base = base; 1152 1148 1153 1149 sec->sh.sh_type = SHT_RELA; 1154 - sec->sh.sh_addralign = 8; 1150 + sec->sh.sh_addralign = addrsize; 1155 1151 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; 1156 1152 sec->sh.sh_info = base->idx; 1157 1153 sec->sh.sh_flags = SHF_INFO_LINK;
+8
tools/objtool/include/objtool/elf.h
··· 142 142 return elf->num_files > 1; 143 143 } 144 144 145 + static inline int elf_class_addrsize(struct elf *elf) 146 + { 147 + if (elf->ehdr.e_ident[EI_CLASS] == ELFCLASS32) 148 + return sizeof(u32); 149 + else 150 + return sizeof(u64); 151 + } 152 + 145 153 struct elf *elf_open_read(const char *name, int flags); 146 154 struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr); 147 155