Commit 417a4dc9 authored by Peter Zijlstra's avatar Peter Zijlstra Committed by Ingo Molnar

objtool: Extract elf_strtab_concat()

Create a common helper to append strings to a strtab.
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
Reviewed-by: default avatarMiroslav Benes <mbenes@suse.cz>
Link: https://lkml.kernel.org/r/20210326151259.941474004@infradead.org
parent d0c5c4cc
...@@ -673,13 +673,48 @@ struct elf *elf_open_read(const char *name, int flags) ...@@ -673,13 +673,48 @@ struct elf *elf_open_read(const char *name, int flags)
return NULL; return NULL;
} }
static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
{
Elf_Data *data;
Elf_Scn *s;
int len;
if (!strtab)
strtab = find_section_by_name(elf, ".strtab");
if (!strtab) {
WARN("can't find .strtab section");
return -1;
}
s = elf_getscn(elf->elf, strtab->idx);
if (!s) {
WARN_ELF("elf_getscn");
return -1;
}
data = elf_newdata(s);
if (!data) {
WARN_ELF("elf_newdata");
return -1;
}
data->d_buf = str;
data->d_size = strlen(str) + 1;
data->d_align = 1;
len = strtab->len;
strtab->len += data->d_size;
strtab->changed = true;
return len;
}
struct section *elf_create_section(struct elf *elf, const char *name, struct section *elf_create_section(struct elf *elf, const char *name,
unsigned int sh_flags, size_t entsize, int nr) unsigned int sh_flags, size_t entsize, int nr)
{ {
struct section *sec, *shstrtab; struct section *sec, *shstrtab;
size_t size = entsize * nr; size_t size = entsize * nr;
Elf_Scn *s; Elf_Scn *s;
Elf_Data *data;
sec = malloc(sizeof(*sec)); sec = malloc(sizeof(*sec));
if (!sec) { if (!sec) {
...@@ -736,7 +771,6 @@ struct section *elf_create_section(struct elf *elf, const char *name, ...@@ -736,7 +771,6 @@ struct section *elf_create_section(struct elf *elf, const char *name,
sec->sh.sh_addralign = 1; sec->sh.sh_addralign = 1;
sec->sh.sh_flags = SHF_ALLOC | sh_flags; sec->sh.sh_flags = SHF_ALLOC | sh_flags;
/* Add section name to .shstrtab (or .strtab for Clang) */ /* Add section name to .shstrtab (or .strtab for Clang) */
shstrtab = find_section_by_name(elf, ".shstrtab"); shstrtab = find_section_by_name(elf, ".shstrtab");
if (!shstrtab) if (!shstrtab)
...@@ -745,27 +779,9 @@ struct section *elf_create_section(struct elf *elf, const char *name, ...@@ -745,27 +779,9 @@ struct section *elf_create_section(struct elf *elf, const char *name,
WARN("can't find .shstrtab or .strtab section"); WARN("can't find .shstrtab or .strtab section");
return NULL; return NULL;
} }
sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
s = elf_getscn(elf->elf, shstrtab->idx); if (sec->sh.sh_name == -1)
if (!s) {
WARN_ELF("elf_getscn");
return NULL; return NULL;
}
data = elf_newdata(s);
if (!data) {
WARN_ELF("elf_newdata");
return NULL;
}
data->d_buf = sec->name;
data->d_size = strlen(name) + 1;
data->d_align = 1;
sec->sh.sh_name = shstrtab->len;
shstrtab->len += strlen(name) + 1;
shstrtab->changed = true;
list_add_tail(&sec->list, &elf->sections); list_add_tail(&sec->list, &elf->sections);
elf_hash_add(elf->section_hash, &sec->hash, sec->idx); elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment