Skip to content

Commit 417a4dc

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
objtool: Extract elf_strtab_concat()
Create a common helper to append strings to a strtab. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Miroslav Benes <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent d0c5c4c commit 417a4dc

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

tools/objtool/elf.c

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,48 @@ struct elf *elf_open_read(const char *name, int flags)
673673
return NULL;
674674
}
675675

676+
static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
677+
{
678+
Elf_Data *data;
679+
Elf_Scn *s;
680+
int len;
681+
682+
if (!strtab)
683+
strtab = find_section_by_name(elf, ".strtab");
684+
if (!strtab) {
685+
WARN("can't find .strtab section");
686+
return -1;
687+
}
688+
689+
s = elf_getscn(elf->elf, strtab->idx);
690+
if (!s) {
691+
WARN_ELF("elf_getscn");
692+
return -1;
693+
}
694+
695+
data = elf_newdata(s);
696+
if (!data) {
697+
WARN_ELF("elf_newdata");
698+
return -1;
699+
}
700+
701+
data->d_buf = str;
702+
data->d_size = strlen(str) + 1;
703+
data->d_align = 1;
704+
705+
len = strtab->len;
706+
strtab->len += data->d_size;
707+
strtab->changed = true;
708+
709+
return len;
710+
}
711+
676712
struct section *elf_create_section(struct elf *elf, const char *name,
677713
unsigned int sh_flags, size_t entsize, int nr)
678714
{
679715
struct section *sec, *shstrtab;
680716
size_t size = entsize * nr;
681717
Elf_Scn *s;
682-
Elf_Data *data;
683718

684719
sec = malloc(sizeof(*sec));
685720
if (!sec) {
@@ -736,7 +771,6 @@ struct section *elf_create_section(struct elf *elf, const char *name,
736771
sec->sh.sh_addralign = 1;
737772
sec->sh.sh_flags = SHF_ALLOC | sh_flags;
738773

739-
740774
/* Add section name to .shstrtab (or .strtab for Clang) */
741775
shstrtab = find_section_by_name(elf, ".shstrtab");
742776
if (!shstrtab)
@@ -745,27 +779,9 @@ struct section *elf_create_section(struct elf *elf, const char *name,
745779
WARN("can't find .shstrtab or .strtab section");
746780
return NULL;
747781
}
748-
749-
s = elf_getscn(elf->elf, shstrtab->idx);
750-
if (!s) {
751-
WARN_ELF("elf_getscn");
782+
sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
783+
if (sec->sh.sh_name == -1)
752784
return NULL;
753-
}
754-
755-
data = elf_newdata(s);
756-
if (!data) {
757-
WARN_ELF("elf_newdata");
758-
return NULL;
759-
}
760-
761-
data->d_buf = sec->name;
762-
data->d_size = strlen(name) + 1;
763-
data->d_align = 1;
764-
765-
sec->sh.sh_name = shstrtab->len;
766-
767-
shstrtab->len += strlen(name) + 1;
768-
shstrtab->changed = true;
769785

770786
list_add_tail(&sec->list, &elf->sections);
771787
elf_hash_add(elf->section_hash, &sec->hash, sec->idx);

0 commit comments

Comments
 (0)