[PATCH v2 09/14] objtool: Extract elf_strtab_concat()

From: Peter Zijlstra
Date: Thu Mar 18 2021 - 13:25:24 EST


Create a common helper to append strings to a strtab.

Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
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;
+ }
+
+ 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 = name;
+ data->d_size = strlen(name) + 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,
unsigned int sh_flags, size_t entsize, int nr)
{
- struct section *sec, *shstrtab;
+ struct section *sec;
size_t size = entsize * nr;
Elf_Scn *s;
- Elf_Data *data;

sec = malloc(sizeof(*sec));
if (!sec) {
@@ -739,36 +777,9 @@ struct section *elf_create_section(struc
sec->sh.sh_addralign = 1;
sec->sh.sh_flags = SHF_ALLOC | sh_flags;

-
- /* 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;
- }
-
- s = elf_getscn(elf->elf, shstrtab->idx);
- if (!s) {
- WARN_ELF("elf_getscn");
+ sec->sh.sh_name = elf_strtab_concat(elf, sec->name, ".shstrtab");
+ if (sec->sh.sh_name == -1)
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);
elf_hash_add(elf->section_hash, &sec->hash, sec->idx);