From: Peter Zijlstra <peterz@infradead.org>
To: x86@kernel.org, jpoimboe@redhat.com, jgross@suse.com, mbenes@suze.cz
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org
Subject: [PATCH 6/9] objtool: Add elf_create_undef_symbol()
Date: Fri, 12 Mar 2021 18:16:19 +0100 [thread overview]
Message-ID: <20210312171653.710872453@infradead.org> (raw)
In-Reply-To: 20210312171613.533405394@infradead.org
Allow objtool to create undefined symbols; this allows creating
relocations to symbols not currently in the symbol table.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
tools/objtool/elf.c | 180 +++++++++++++++++++++++++++---------
tools/objtool/include/objtool/elf.h | 1
2 files changed, 139 insertions(+), 42 deletions(-)
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -316,12 +316,60 @@ static int read_sections(struct elf *elf
return 0;
}
+static bool elf_symbol_add(struct elf *elf, struct symbol *sym, Elf32_Word shndx)
+{
+ struct list_head *entry;
+ struct rb_node *pnode;
+
+ sym->type = GELF_ST_TYPE(sym->sym.st_info);
+ sym->bind = GELF_ST_BIND(sym->sym.st_info);
+
+ if ((sym->sym.st_shndx > SHN_UNDEF &&
+ sym->sym.st_shndx < SHN_LORESERVE) ||
+ (shndx != SHN_XINDEX && sym->sym.st_shndx == SHN_XINDEX)) {
+ if (sym->sym.st_shndx != SHN_XINDEX)
+ shndx = sym->sym.st_shndx;
+
+ sym->sec = find_section_by_index(elf, shndx);
+ if (!sym->sec) {
+ WARN("couldn't find section for symbol %s",
+ sym->name);
+ return false;
+ }
+ if (sym->type == STT_SECTION) {
+ sym->name = sym->sec->name;
+ sym->sec->sym = sym;
+ }
+ } else
+ sym->sec = find_section_by_index(elf, 0);
+
+ sym->offset = sym->sym.st_value;
+ sym->len = sym->sym.st_size;
+
+ rb_add(&sym->node, &sym->sec->symbol_tree, symbol_to_offset);
+ pnode = rb_prev(&sym->node);
+ if (pnode)
+ entry = &rb_entry(pnode, struct symbol, node)->list;
+ else
+ entry = &sym->sec->symbol_list;
+ list_add(&sym->list, entry);
+ elf_hash_add(elf->symbol_hash, &sym->hash, sym->idx);
+ elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
+
+ /*
+ * Don't store empty STT_NOTYPE symbols in the rbtree. They
+ * can exist within a function, confusing the sorting.
+ */
+ if (!sym->len)
+ rb_erase(&sym->node, &sym->sec->symbol_tree);
+
+ return true;
+}
+
static int read_symbols(struct elf *elf)
{
struct section *symtab, *symtab_shndx, *sec;
struct symbol *sym, *pfunc;
- struct list_head *entry;
- struct rb_node *pnode;
int symbols_nr, i;
char *coldstr;
Elf_Data *shndx_data = NULL;
@@ -366,47 +414,11 @@ static int read_symbols(struct elf *elf)
goto err;
}
- sym->type = GELF_ST_TYPE(sym->sym.st_info);
- sym->bind = GELF_ST_BIND(sym->sym.st_info);
+ if (!shndx_data)
+ shndx = SHN_XINDEX;
- if ((sym->sym.st_shndx > SHN_UNDEF &&
- sym->sym.st_shndx < SHN_LORESERVE) ||
- (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
- if (sym->sym.st_shndx != SHN_XINDEX)
- shndx = sym->sym.st_shndx;
-
- sym->sec = find_section_by_index(elf, shndx);
- if (!sym->sec) {
- WARN("couldn't find section for symbol %s",
- sym->name);
- goto err;
- }
- if (sym->type == STT_SECTION) {
- sym->name = sym->sec->name;
- sym->sec->sym = sym;
- }
- } else
- sym->sec = find_section_by_index(elf, 0);
-
- sym->offset = sym->sym.st_value;
- sym->len = sym->sym.st_size;
-
- rb_add(&sym->node, &sym->sec->symbol_tree, symbol_to_offset);
- pnode = rb_prev(&sym->node);
- if (pnode)
- entry = &rb_entry(pnode, struct symbol, node)->list;
- else
- entry = &sym->sec->symbol_list;
- list_add(&sym->list, entry);
- elf_hash_add(elf->symbol_hash, &sym->hash, sym->idx);
- elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
-
- /*
- * Don't store empty STT_NOTYPE symbols in the rbtree. They
- * can exist within a function, confusing the sorting.
- */
- if (!sym->len)
- rb_erase(&sym->node, &sym->sec->symbol_tree);
+ if (!elf_symbol_add(elf, sym, shndx))
+ goto err;
}
if (stats)
@@ -640,6 +652,90 @@ struct elf *elf_open_read(const char *na
return NULL;
}
+struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name)
+{
+ struct section *strtab, *symtab;
+ struct symbol *sym;
+ Elf_Scn *s;
+ Elf_Data *data;
+
+ sym = malloc(sizeof(*sym));
+ if (!sym) {
+ perror("malloc");
+ return NULL;
+ }
+ memset(sym, 0, sizeof(*sym));
+
+ sym->name = strdup(name);
+
+ strtab = find_section_by_name(elf, ".strtab");
+ if (!strtab) {
+ WARN("can't find .strtab");
+ return NULL;
+ }
+
+ s = elf_getscn(elf->elf, strtab->idx);
+ if (!s) {
+ WARN_ELF("elf_getscn");
+ return NULL;
+ }
+
+ data = elf_newdata(s);
+ if (!data) {
+ WARN_ELF("elf_newdata");
+ return NULL;
+ }
+
+ data->d_buf = sym->name;
+ data->d_size = strlen(sym->name) + 1;
+ data->d_align = 1;
+
+ sym->sym.st_name = strtab->len;
+ sym->sym.st_info = 0x10; /* STB_GLOBAL, STT_NOTYPE */
+ // st_other 0
+ // st_shndx 0
+ // st_value 0
+ // st_size 0
+
+ strtab->len += data->d_size;
+ strtab->changed = true;
+
+
+ symtab = find_section_by_name(elf, ".symtab");
+ if (!symtab) {
+ WARN("can't find .symtab");
+ return NULL;
+ }
+
+ s = elf_getscn(elf->elf, symtab->idx);
+ if (!s) {
+ WARN_ELF("elf_getscn");
+ return NULL;
+ }
+
+ data = elf_newdata(s);
+ if (!data) {
+ WARN_ELF("elf_newdata");
+ return NULL;
+ }
+
+ data->d_buf = &sym->sym;
+ data->d_size = sizeof(sym->sym);
+ data->d_align = 1;
+
+ sym->idx = symtab->len / sizeof(sym->sym);
+
+ symtab->len += data->d_size;
+ symtab->changed = true;
+
+ if (!elf_symbol_add(elf, sym, SHN_XINDEX)) {
+ WARN("elf_symbol_add");
+ return NULL;
+ }
+
+ return sym;
+}
+
struct section *elf_create_section(struct elf *elf, const char *name,
unsigned int sh_flags, size_t entsize, int nr)
{
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -128,6 +128,7 @@ int elf_write_insn(struct elf *elf, stru
unsigned long offset, unsigned int len,
const char *insn);
int elf_write_reloc(struct elf *elf, struct reloc *reloc);
+struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name);
int elf_write(struct elf *elf);
void elf_close(struct elf *elf);
next prev parent reply other threads:[~2021-03-12 17:18 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-12 17:16 [PATCH 0/9] x86,objtool: Optimize !RETPOLINE Peter Zijlstra
2021-03-12 17:16 ` [PATCH 1/9] x86/retpoline: Simplify retpolines Peter Zijlstra
2021-03-12 17:16 ` [PATCH 2/9] objtool: Correctly handle retpoline thunk calls Peter Zijlstra
2021-03-16 21:19 ` Josh Poimboeuf
2021-03-12 17:16 ` [PATCH 3/9] objtool: Per arch retpoline naming Peter Zijlstra
2021-03-12 17:16 ` [PATCH 4/9] objtool: Fix static_call list generation Peter Zijlstra
2021-03-17 3:18 ` Josh Poimboeuf
2021-03-12 17:16 ` [PATCH 5/9] objtool: Rework rebuild_reloc logic Peter Zijlstra
2021-03-17 3:34 ` Josh Poimboeuf
2021-03-17 8:12 ` Peter Zijlstra
2021-03-18 0:49 ` Josh Poimboeuf
2021-03-18 12:57 ` Peter Zijlstra
2021-03-18 16:36 ` Josh Poimboeuf
2021-03-18 17:04 ` Peter Zijlstra
2021-03-18 17:38 ` Josh Poimboeuf
2021-03-19 0:19 ` Josh Poimboeuf
2021-03-19 9:22 ` Peter Zijlstra
2021-03-19 15:15 ` Josh Poimboeuf
2021-03-12 17:16 ` Peter Zijlstra [this message]
2021-03-17 13:52 ` [PATCH 6/9] objtool: Add elf_create_undef_symbol() Miroslav Benes
2021-03-17 14:13 ` Peter Zijlstra
2021-03-17 14:39 ` Miroslav Benes
2021-03-17 15:08 ` Sami Tolvanen
2021-03-18 0:46 ` Josh Poimboeuf
2021-03-18 7:56 ` Peter Zijlstra
2021-03-12 17:16 ` [PATCH 7/9] objtool: Allow archs to rewrite retpolines Peter Zijlstra
2021-03-12 17:16 ` [PATCH 8/9] objtool: Skip magical retpoline .altinstr_replacement Peter Zijlstra
2021-03-12 17:16 ` [PATCH 9/9] objtool,x86: Rewrite retpoline thunk calls Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210312171653.710872453@infradead.org \
--to=peterz@infradead.org \
--cc=jgross@suse.com \
--cc=jpoimboe@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mbenes@suze.cz \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).