diff mbox series

[RFC,v3,03/12] objtool: Use target file class size instead of a compiled constant

Message ID 20220624183238.388144-4-sv@linux.ibm.com (mailing list archive)
State Handled Elsewhere
Headers show
Series objtool: Enable and implement --mcount option on powerpc | expand

Commit Message

Sathvika Vasireddy June 24, 2022, 6:32 p.m. UTC
From: Christophe Leroy <christophe.leroy@csgroup.eu>

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.

Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
 tools/objtool/check.c               | 16 +++++++++-------
 tools/objtool/elf.c                 |  8 ++++++--
 tools/objtool/include/objtool/elf.h |  8 ++++++++
 3 files changed, 23 insertions(+), 9 deletions(-)

Comments

Christophe Leroy July 8, 2022, 5:35 p.m. UTC | #1
Le 24/06/2022 à 20:32, Sathvika Vasireddy a écrit :
> From: Christophe Leroy <christophe.leroy@csgroup.eu>
> 
> 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.
> 
> Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
> ---
>   tools/objtool/check.c               | 16 +++++++++-------
>   tools/objtool/elf.c                 |  8 ++++++--
>   tools/objtool/include/objtool/elf.h |  8 ++++++++
>   3 files changed, 23 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index cef1dd54d505..fabc0ea88747 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -802,9 +802,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;
>   	struct instruction *insn;
>   	int idx;
> +	int size = elf_class_size(file->elf);

Should be renamed addrsize as per Naveen comment.

>   
>   	sec = find_section_by_name(file->elf, "__mcount_loc");
>   	if (sec) {
> @@ -820,23 +820,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, size, idx);
>   	if (!sec)
>   		return -1;
>   
> +	sec->sh.sh_addralign = size;
> +
>   	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, size);
>   
> -		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 += size;
>   	}
>   
>   	return 0;
> diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
> index c25e957c1e52..63218f5799c2 100644
> --- a/tools/objtool/elf.c
> +++ b/tools/objtool/elf.c
> @@ -1124,6 +1124,7 @@ static struct section *elf_create_rela_reloc_section(struct elf *elf, struct sec
>   {
>   	char *relocname;
>   	struct section *sec;
> +	int size = elf_class_size(elf);

Should be renamed addrsize as per Naveen comment.

>   
>   	relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
>   	if (!relocname) {
> @@ -1133,7 +1134,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 (size == 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;
> @@ -1142,7 +1146,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 = size;
>   	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
>   	sec->sh.sh_info = base->idx;
>   	sec->sh.sh_flags = SHF_INFO_LINK;
> diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
> index adebfbc2b518..c720c4476828 100644
> --- a/tools/objtool/include/objtool/elf.h
> +++ b/tools/objtool/include/objtool/elf.h
> @@ -141,6 +141,14 @@ static inline bool has_multiple_files(struct elf *elf)
>   	return elf->num_files > 1;
>   }
>   
> +static inline int elf_class_size(struct elf *elf)

Should be renamed elf_class_addrsize() as per Naveen comment.

> +{
> +	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);
>
diff mbox series

Patch

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index cef1dd54d505..fabc0ea88747 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -802,9 +802,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;
 	struct instruction *insn;
 	int idx;
+	int size = elf_class_size(file->elf);
 
 	sec = find_section_by_name(file->elf, "__mcount_loc");
 	if (sec) {
@@ -820,23 +820,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, size, idx);
 	if (!sec)
 		return -1;
 
+	sec->sh.sh_addralign = size;
+
 	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, size);
 
-		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 += size;
 	}
 
 	return 0;
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index c25e957c1e52..63218f5799c2 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1124,6 +1124,7 @@  static struct section *elf_create_rela_reloc_section(struct elf *elf, struct sec
 {
 	char *relocname;
 	struct section *sec;
+	int size = elf_class_size(elf);
 
 	relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
 	if (!relocname) {
@@ -1133,7 +1134,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 (size == 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;
@@ -1142,7 +1146,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 = size;
 	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
 	sec->sh.sh_info = base->idx;
 	sec->sh.sh_flags = SHF_INFO_LINK;
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index adebfbc2b518..c720c4476828 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -141,6 +141,14 @@  static inline bool has_multiple_files(struct elf *elf)
 	return elf->num_files > 1;
 }
 
+static inline int elf_class_size(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);