objtool: Use target file class size instead of a compiled constant
authorChristophe Leroy <christophe.leroy@csgroup.eu>
Mon, 14 Nov 2022 17:57:48 +0000 (23:27 +0530)
committerMichael Ellerman <mpe@ellerman.id.au>
Fri, 18 Nov 2022 08:00:16 +0000 (19:00 +1100)
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
tools/objtool/check.c
tools/objtool/elf.c
tools/objtool/include/objtool/elf.h

index ad5dab1..b64518c 100644 (file)
@@ -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;
index 7e24b09..3373986 100644 (file)
@@ -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,7 +1139,10 @@ static struct section *elf_create_rela_reloc_section(struct elf *elf, struct sec
        strcpy(relocname, ".rela");
        strcat(relocname, base->name);
 
-       sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
+       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)
                return NULL;
@@ -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;
index 16f4067..78b3aa2 100644 (file)
@@ -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);