On Thu, Mar 18, 2021 at 06:11:12PM +0100, Peter Zijlstra wrote:
> Create a common helper to append strings to a strtab.
> 
> Signed-off-by: Peter Zijlstra (Intel) <pet...@infradead.org>
> ---
>  tools/objtool/elf.c |   73 
> +++++++++++++++++++++++++++++-----------------------
>  1 file changed, 42 insertions(+), 31 deletions(-)
> 
> --- a/tools/objtool/elf.c
> +++ b/tools/objtool/elf.c
> @@ -676,13 +676,51 @@ struct elf *elf_open_read(const char *na
>       return NULL;
>  }
>  
> +static int elf_strtab_concat(struct elf *elf, char *name, const char 
> *strtab_name)
> +{
> +     struct section *strtab = NULL;
> +     Elf_Data *data;
> +     Elf_Scn *s;
> +     int len;
> +
> +     if (strtab_name)
> +             strtab = find_section_by_name(elf, strtab_name);
> +     if (!strtab)
> +             strtab = find_section_by_name(elf, ".strtab");
> +     if (!strtab) {
> +             WARN("can't find %s or .strtab section", strtab_name);
> +             return -1;
> +     }

This part's a bit mysterious (and it loses the Clang comment).  Maybe we
can leave the section finding logic in elf_create_section()?

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index b85db6efb9d3..db9ad54894d8 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -676,19 +676,17 @@ struct elf *elf_open_read(const char *name, int flags)
        return NULL;
 }
 
-static int elf_strtab_concat(struct elf *elf, char *name, const char 
*strtab_name)
+static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
 {
        struct section *strtab = NULL;
        Elf_Data *data;
        Elf_Scn *s;
        int len;
 
-       if (strtab_name)
-               strtab = find_section_by_name(elf, strtab_name);
        if (!strtab)
                strtab = find_section_by_name(elf, ".strtab");
        if (!strtab) {
-               WARN("can't find %s or .strtab section", strtab_name);
+               WARN("can't find .strtab section");
                return -1;
        }
 
@@ -718,7 +716,7 @@ static int elf_strtab_concat(struct elf *elf, char *name, 
const char *strtab_nam
 struct section *elf_create_section(struct elf *elf, const char *name,
                                   unsigned int sh_flags, size_t entsize, int 
nr)
 {
-       struct section *sec;
+       struct section *sec, *shstrtab;
        size_t size = entsize * nr;
        Elf_Scn *s;
 
@@ -777,7 +775,15 @@ struct section *elf_create_section(struct elf *elf, const 
char *name,
        sec->sh.sh_addralign = 1;
        sec->sh.sh_flags = SHF_ALLOC | sh_flags;
 
-       sec->sh.sh_name = elf_strtab_concat(elf, sec->name, ".shstrtab");
+       /* Add section name to .shstrtab (or .strtab for Clang) */
+       shstrtab = find_section_by_name(elf, ".shstrtab");
+       if (!shstrtab)
+               shstrtab = find_section_by_name(elf, ".strtab");
+       if (!shstrtab) {
+               WARN("can't find .shstrtab or .strtab section");
+               return NULL;
+       }
+       sec->sh.sh_name = elf_add_string(elf, sec->name, shstrtab);
        if (sec->sh.sh_name == -1)
                return NULL;
 

Reply via email to