Commit fcf93355 authored by Josh Poimboeuf's avatar Josh Poimboeuf

objtool: Keep GElf_Rel[a] structs synced

Keep the GElf_Rela structs synced with their 'struct reloc' counterparts
instead of having to go back and "rebuild" them later.

Link: https://lore.kernel.org/r/156d8a3e528a11e5c8577cf552890ed1f2b9567b.1685464332.git.jpoimboe@kernel.orgSigned-off-by: default avatarJosh Poimboeuf <jpoimboe@kernel.org>
parent 6342a20e
...@@ -534,16 +534,18 @@ static int read_symbols(struct elf *elf) ...@@ -534,16 +534,18 @@ static int read_symbols(struct elf *elf)
} }
/* /*
* Ensure that any reloc section containing references to @sym is marked * @sym's idx has changed. Update the relocs which reference it.
* changed such that it will get re-generated in elf_rebuild_reloc_sections()
* with the new symbol index.
*/ */
static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym) static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
{ {
struct reloc *reloc; struct reloc *reloc;
list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) {
mark_sec_changed(elf, reloc->sec, true); if (elf_write_reloc(elf, reloc))
return -1;
}
return 0;
} }
/* /*
...@@ -716,13 +718,14 @@ __elf_create_symbol(struct elf *elf, struct symbol *sym) ...@@ -716,13 +718,14 @@ __elf_create_symbol(struct elf *elf, struct symbol *sym)
hlist_del(&old->hash); hlist_del(&old->hash);
elf_hash_add(symbol, &old->hash, old->idx); elf_hash_add(symbol, &old->hash, old->idx);
elf_dirty_reloc_sym(elf, old);
if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
WARN("elf_update_symbol move"); WARN("elf_update_symbol move");
return NULL; return NULL;
} }
if (elf_update_sym_relocs(elf, old))
return NULL;
new_idx = first_non_local; new_idx = first_non_local;
} }
...@@ -833,12 +836,13 @@ static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec, ...@@ -833,12 +836,13 @@ static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
reloc->sym = sym; reloc->sym = sym;
reloc->addend = addend; reloc->addend = addend;
if (elf_write_reloc(elf, reloc))
return NULL;
list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list);
list_add_tail(&reloc->list, &rsec->reloc_list); list_add_tail(&reloc->list, &rsec->reloc_list);
elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
mark_sec_changed(elf, rsec, true);
return reloc; return reloc;
} }
...@@ -1203,31 +1207,6 @@ struct section *elf_create_section_pair(struct elf *elf, const char *name, ...@@ -1203,31 +1207,6 @@ struct section *elf_create_section_pair(struct elf *elf, const char *name,
return sec; return sec;
} }
static int elf_rebuild_reloc_section(struct elf *elf, struct section *rsec)
{
struct reloc *reloc;
int idx = 0, ret;
idx = 0;
list_for_each_entry(reloc, &rsec->reloc_list, list) {
reloc->rel.r_offset = reloc->offset;
reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
if (rsec->sh.sh_type == SHT_RELA) {
reloc->rela.r_addend = reloc->addend;
ret = gelf_update_rela(rsec->data, idx, &reloc->rela);
} else {
ret = gelf_update_rel(rsec->data, idx, &reloc->rel);
}
if (!ret) {
WARN_ELF("gelf_update_rel");
return -1;
}
idx++;
}
return 0;
}
int elf_write_insn(struct elf *elf, struct section *sec, int elf_write_insn(struct elf *elf, struct section *sec,
unsigned long offset, unsigned int len, unsigned long offset, unsigned int len,
const char *insn) const char *insn)
...@@ -1351,12 +1330,6 @@ int elf_write(struct elf *elf) ...@@ -1351,12 +1330,6 @@ int elf_write(struct elf *elf)
return -1; return -1;
} }
if (sec->base &&
elf_rebuild_reloc_section(elf, sec)) {
WARN("elf_rebuild_reloc_section");
return -1;
}
mark_sec_changed(elf, sec, false); mark_sec_changed(elf, sec, false);
} }
} }
......
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