Skip to content

Commit 86ea7f3

Browse files
chleroympe
authored andcommitted
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 <[email protected]> Reviewed-by: Naveen N. Rao <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Christophe Leroy <[email protected]> Signed-off-by: Sathvika Vasireddy <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0646c28 commit 86ea7f3

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

tools/objtool/check.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,9 @@ static int create_ibt_endbr_seal_sections(struct objtool_file *file)
852852

853853
static int create_mcount_loc_sections(struct objtool_file *file)
854854
{
855-
struct section *sec;
856-
unsigned long *loc;
855+
int addrsize = elf_class_addrsize(file->elf);
857856
struct instruction *insn;
857+
struct section *sec;
858858
int idx;
859859

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

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);
875875
if (!sec)
876876
return -1;
877877

878+
sec->sh.sh_addralign = addrsize;
879+
878880
idx = 0;
879881
list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
882+
void *loc;
880883

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);
883886

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,
886888
R_X86_64_64,
887889
insn->sec, insn->offset))
888890
return -1;
889891

890-
idx++;
892+
idx += addrsize;
891893
}
892894

893895
return 0;

tools/objtool/elf.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,7 @@ static struct section *elf_create_rela_reloc_section(struct elf *elf, struct sec
11291129
{
11301130
char *relocname;
11311131
struct section *sec;
1132+
int addrsize = elf_class_addrsize(elf);
11321133

11331134
relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
11341135
if (!relocname) {
@@ -1138,7 +1139,10 @@ static struct section *elf_create_rela_reloc_section(struct elf *elf, struct sec
11381139
strcpy(relocname, ".rela");
11391140
strcat(relocname, base->name);
11401141

1141-
sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
1142+
if (addrsize == sizeof(u32))
1143+
sec = elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 0);
1144+
else
1145+
sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
11421146
free(relocname);
11431147
if (!sec)
11441148
return NULL;
@@ -1147,7 +1151,7 @@ static struct section *elf_create_rela_reloc_section(struct elf *elf, struct sec
11471151
sec->base = base;
11481152

11491153
sec->sh.sh_type = SHT_RELA;
1150-
sec->sh.sh_addralign = 8;
1154+
sec->sh.sh_addralign = addrsize;
11511155
sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
11521156
sec->sh.sh_info = base->idx;
11531157
sec->sh.sh_flags = SHF_INFO_LINK;

tools/objtool/include/objtool/elf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ static inline bool has_multiple_files(struct elf *elf)
142142
return elf->num_files > 1;
143143
}
144144

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+
145153
struct elf *elf_open_read(const char *name, int flags);
146154
struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr);
147155

0 commit comments

Comments
 (0)