objtool: Support implicit addend relocations
cover objtool: Support implicit addend relocations
Future tools that use objtool to process ELF object files may require reading or even modifying relocations with implicit addends. This series cleans up the naming of the relocation struct in objtool, variable names, and function names to be consistent with a more generic use rather than being specific to those applying explicit addends.
From there we switch to the specification compliant method of finding the section the relocations apply to and fall back to the old naming-heuristic basis if that's not available.
Finally, add support for implicit addends. Note that we don't completely convert the orc tools to work with both kinds of relocations. I'm hoping to get feedback on whether it makes more sense to convert it or leave it as-is.
Matt Helsley (3):
objtool: Rename rela to reloc
objtool: Find relocation base section using sh_info
objtool: Add support for relocations without addends
tools/objtool/arch.h | 2 +- tools/objtool/arch/x86/decode.c | 2 +- tools/objtool/check.c | 196 +++++++++++++------------- tools/objtool/check.h | 2 +- tools/objtool/elf.c | 237 +++++++++++++++++++++----------- tools/objtool/elf.h | 27 ++-- tools/objtool/orc_gen.c | 46 +++---- tools/objtool/special.c | 28 ++-- 8 files changed, 311 insertions(+), 229 deletions(-)
base-commit: 7c0577f4e609f7278ebd6d21e2de82b42f110944
prerequisite-patch-id: 387905b1da8b1293e7736b28fcc1585583e3e7cf
prerequisite-patch-id: 2fd7ac6c4dfd717ab117fc771623c01040188b4e
prerequisite-patch-id: bfeefd995e3f80d6fa0edfa2205d98be31fbee4e
prerequisite-patch-id: abcb5732607ad17f4c99c06b8bb83c9db1b15c42
prerequisite-patch-id: 5179e659d828bed48051d365f669a8ba3a36c206
Signature · 1 line
2.20.1
RFC PATCH 1/3 objtool: Rename rela to reloc
Before supporting additional relocation types rename the relevant types and functions from "rela" to "reloc". This work can largely be done with the following regex:
Notable exceptions include gelf_* library calls and standard/expected section names which still use "rela" because they encode the type of relocation expected. Also, keep "rela" in the struct because it encodes a specify type of relocation we currently expect.
It will eventually turn into a member of an anonymous union when a susequent patch adds implicit addend, or "rel", relocation support.
--- tools/objtool/arch.h | 2 +- tools/objtool/arch/x86/decode.c | 2 +- tools/objtool/check.c | 196 ++++++++++++++++---------------- tools/objtool/check.h | 2 +- tools/objtool/elf.c | 138 +++++++++++----------- tools/objtool/elf.h | 22 ++-- tools/objtool/orc_gen.c | 46 ++++---- tools/objtool/special.c | 28 ++--- 8 files changed, 218 insertions(+), 218 deletions(-)
tools/objtool/arch.h +1 -1
@@ -82,6 +82,6 @@ bool arch_callee_saved_reg(unsigned char reg);unsigned long arch_jump_destination (struct instruction *insn); -unsigned long arch_dest_rela_offset (int addend); +unsigned long arch_dest_reloc_offset (int addend);#endif /* _ARCH_H */
tools/objtool/arch/x86/decode.c +1 -1
@@ -67,7 +67,7 @@ bool arch_callee_saved_reg(unsigned char reg) } } -unsigned long arch_dest_rela_offset (int addend) +unsigned long arch_dest_reloc_offset (int addend) {return addend +4 ; }
tools/objtool/check.c +98 -98
484 lines not on this page · raw message
tools/objtool/check.h +1 -1
@@ -37,7 +37,7 @@ struct instruction {struct symbol *call_dest;struct instruction *jump_dest;struct instruction *first_jump_src; -struct rela *jump_table; +struct reloc *jump_table;struct list_head alts;struct symbol *func;struct list_head stack_ops;
tools/objtool/elf.c +69 -69
277 lines not on this page · raw message
tools/objtool/elf.h +11 -11
@@ -32,8 +32,8 @@ struct section {GElf_Shdr sh;struct rb_root symbol_tree;struct list_head symbol_list; -struct list_head rela_list; -struct section *base, *rela; +struct list_head reloc_list; +struct section *base, *reloc;struct symbol *sym;Elf_Data *data;char *name;
@@ -58,7 +58,7 @@ struct symbol {bool uaccess_safe; }; -struct rela { +struct reloc {struct list_head list;struct hlist_node hash;GElf_Rela rela;
@@ -82,7 +82,7 @@ struct elf {DECLARE_HASHTABLE (symbol_name_hash,ELF_HASH_BITS );DECLARE_HASHTABLE (section_hash,ELF_HASH_BITS );DECLARE_HASHTABLE (section_name_hash,ELF_HASH_BITS ); -DECLARE_HASHTABLE (rela_hash,ELF_HASH_BITS ); +DECLARE_HASHTABLE (reloc_hash,ELF_HASH_BITS ); };#define OFFSET_STRIDE_BITS 4
@@ -109,15 +109,15 @@ static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)return ol; } -static inline u32 rela_hash (struct rela *rela) +static inline u32 reloc_hash (struct reloc *reloc) { -return sec_offset_hash (rela->sec, rela->offset); +return sec_offset_hash (reloc->sec, reloc->offset); }struct elf *elf_open_read (const char *name,int flags);struct section *elf_create_section (struct elf *elf,const char *name,size_t entsize,int nr); -struct section *elf_create_rela_section (struct elf *elf,struct section *base); -void elf_add_rela (struct elf *elf,struct rela *rela); +struct section *elf_create_reloc_section (struct elf *elf,struct section *base); +void elf_add_reloc (struct elf *elf,struct reloc *reloc);int elf_write (const struct elf *elf);void elf_close (struct elf *elf);
@@ -126,11 +126,11 @@ struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);struct symbol *find_symbol_by_offset (struct section *sec,unsigned long offset);struct symbol *find_symbol_by_name (const struct elf *elf,const char *name);struct symbol *find_symbol_containing (const struct section *sec,unsigned long offset); -struct rela *find_rela_by_dest (const struct elf *elf,struct section *sec,unsigned long offset); -struct rela *find_rela_by_dest_range (const struct elf *elf,struct section *sec, +struct reloc *find_reloc_by_dest (const struct elf *elf,struct section *sec,unsigned long offset); +struct reloc *find_reloc_by_dest_range (const struct elf *elf,struct section *sec,unsigned long offset,unsigned int len);struct symbol *find_func_containing (struct section *sec,unsigned long offset); -int elf_rebuild_rela_section (struct section *sec); +int elf_rebuild_reloc_section (struct section *sec);#define for_each_sec (file, sec) \ list_for_each_entry(sec, &file->elf->sections, list)
tools/objtool/orc_gen.c +23 -23
115 lines not on this page · raw message
tools/objtool/special.c +14 -14
@@ -72,7 +72,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry, struct section *sec, int idx, struct special_alt *alt) { - struct rela *orig_rela, *new_rela; + struct reloc *orig_reloc, *new_reloc;unsigned long offset; offset = idx * entry->size;
@@ -118,30 +118,30 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry, } } - orig_rela =find_rela_by_dest (elf, sec, offset + entry->orig); -if (!orig_rela) { -WARN_FUNC ("can't find orig rela" , sec, offset + entry->orig); + orig_reloc =find_reloc_by_dest (elf, sec, offset + entry->orig); +if (!orig_reloc) { +WARN_FUNC ("can't find orig reloc" , sec, offset + entry->orig);return -1 ; } -if (orig_rela->sym->type !=STT_SECTION ) { -WARN_FUNC ("don't know how to handle non-section rela symbol %s" , - sec, offset + entry->orig, orig_rela->sym->name); +if (orig_reloc->sym->type !=STT_SECTION ) { +WARN_FUNC ("don't know how to handle non-section reloc symbol %s" , + sec, offset + entry->orig, orig_reloc->sym->name);return -1 ; } - alt->orig_sec = orig_rela->sym->sec; - alt->orig_off = orig_rela->addend; + alt->orig_sec = orig_reloc->sym->sec; + alt->orig_off = orig_reloc->addend;if (!entry->group || alt->new_len) { - new_rela =find_rela_by_dest (elf, sec, offset + entry->new); -if (!new_rela) { -WARN_FUNC ("can't find new rela" , + new_reloc =find_reloc_by_dest (elf, sec, offset + entry->new); +if (!new_reloc) { +WARN_FUNC ("can't find new reloc" , sec, offset + entry->new);return -1 ; } - alt->new_sec = new_rela->sym->sec; - alt->new_off = (unsigned int )new_rela->addend; + alt->new_sec = new_reloc->sym->sec; + alt->new_off = (unsigned int )new_reloc->addend;/* _ASM_EXTABLE_EX hack */ if (alt->new_off >=0x7ffffff0 )
Signature · 1 line
2.20.1
RFC PATCH 3/3 objtool: Add support for relocations without addends
In reply to Matt HelsleyCurrently objtool only collects information about relocations with addends. In recordmcount, which we are about to merge into objtool, some supported architectures do not use rela relocations. Since object files use one or the other the list can be reused.
--- tools/objtool/elf.c | 109 ++++++++++++++++++++++++++++++++++++-------- tools/objtool/elf.h | 5 +- 2 files changed, 95 insertions(+), 19 deletions(-)
tools/objtool/elf.c +91 -18
@@ -7,6 +7,7 @@* Copyright (C) 2014 Seth Jennings <[email protected]> */ +#define _GNU_SOURCE#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>
@@ -490,6 +491,32 @@ void elf_add_reloc(struct elf *elf, struct reloc *reloc)elf_hash_add (elf->reloc_hash, &reloc->hash,reloc_hash (reloc)); } +static int read_rel_reloc (struct section *sec,int i,struct reloc *reloc,unsigned int *symndx) +{ +if (!gelf_getrel (sec->data, i, &reloc->rel)) { +WARN_ELF ("gelf_getrel" ); +return -1 ; + } + reloc->type =GELF_R_TYPE (reloc->rel.r_info); + reloc->addend =0 ; + reloc->offset = reloc->rel.r_offset; + *symndx =GELF_R_SYM (reloc->rel.r_info); +return 0 ; +} + +static int read_rela_reloc (struct section *sec,int i,struct reloc *reloc,unsigned int *symndx) +{ +if (!gelf_getrela (sec->data, i, &reloc->rela)) { +WARN_ELF ("gelf_getrela" ); +return -1 ; + } + reloc->type =GELF_R_TYPE (reloc->rela.r_info); + reloc->addend = reloc->rela.r_addend; + reloc->offset = reloc->rela.r_offset; + *symndx =GELF_R_SYM (reloc->rela.r_info); +return 0 ; +} +static int read_relocs (struct elf *elf) {struct section *sec;
@@ -497,17 +524,21 @@ static int read_relocs(struct elf *elf)int i;unsigned int symndx;unsigned long nr_reloc, max_reloc =0 , tot_reloc =0 ; +int (*read_reloc)(struct section *,int ,struct reloc *,unsigned int *);list_for_each_entry (sec, &elf->sections, list) { -if (sec->sh.sh_type !=SHT_RELA ) -continue ; +switch (sec->sh.sh_type) { +case SHT_REL : read_reloc = read_rel_reloc;break ; +case SHT_RELA : read_reloc = read_rela_reloc;break ; +default :continue ; + }if (sec->sh.sh_flags &SHF_INFO_LINK ) { sec->base =find_section_by_index (elf, sec->sh.sh_info); }else if (strncmp (sec->name,".rel" ,4 ) ==0 ) {WARN ("Using naming heuristic to find base of reloc section %s" , sec->name); - sec->base =find_section_by_name (elf, sec->name +5 ); + sec->base =find_section_by_name (elf,strchrnul (sec->name +1 ,'.' )); }if (!sec->base) { WARN("can't find base section for reloc section %s",
@@ -525,16 +556,8 @@ static int read_relocs(struct elf *elf)return -1 ; }memset (reloc,0 ,sizeof (*reloc)); - -if (!gelf_getrela (sec->data, i, &reloc->rela)) { -WARN_ELF ("gelf_getrela" ); +if (read_reloc (sec, i, reloc, &symndx))return -1 ; - } - - reloc->type =GELF_R_TYPE (reloc->rela.r_info); - reloc->addend = reloc->rela.r_addend; - reloc->offset = reloc->rela.r_offset; - symndx =GELF_R_SYM (reloc->rela.r_info); reloc->sym =find_symbol_by_index (elf, symndx); reloc->sec = sec;if (!reloc->sym) {
@@ -752,16 +775,50 @@ struct section *elf_create_reloc_section(struct elf *elf, struct section *base)return sec; } -int elf_rebuild_reloc_section (struct section *sec) +static int elf_rebuild_rel_reloc_section (struct section *sec,int nr) {struct reloc *reloc; -int nr, idx =0 , size; -GElf_Rela *relocs; +int idx =0 , size; +GElf_Rel *relocs; + +/* +* Allocate a buffer for relocations with addends but also use +* it for other relocations too. The section type determines +* the size of the section, the buffer used, and the entries. +*/ + size = nr *sizeof (*relocs); + relocs =malloc (size); +if (!relocs) { +perror ("malloc" ); +return -1 ; + } - nr =0 ; -list_for_each_entry (reloc, &sec->reloc_list, list) - nr++; + sec->data->d_buf = relocs; + sec->data->d_size = size; + + sec->sh.sh_size = size; + + idx =0 ; +list_for_each_entry (reloc, &sec->reloc_list, list) { + relocs[idx].r_offset = reloc->offset; + relocs[idx].r_info =GELF_R_INFO (reloc->sym->idx, reloc->type); + idx++; + } + +return 0 ; +} + +static int elf_rebuild_rela_reloc_section (struct section *sec,int nr) +{ +struct reloc *reloc; +int idx =0 , size; +GElf_Rela *relocs; +/* +* Allocate a buffer for relocations with addends but also use +* it for other relocations too. The section type determines +* the size of the section, the buffer used, and the entries. +*/ size = nr *sizeof (*relocs); relocs =malloc (size);if (!relocs) {
@@ -785,6 +842,22 @@ int elf_rebuild_reloc_section(struct section *sec)return 0 ; } +int elf_rebuild_reloc_section (struct section *sec) +{ +struct reloc *reloc; +int nr; + + nr =0 ; +list_for_each_entry (reloc, &sec->reloc_list, list) + nr++; + +switch (sec->sh.sh_type) { +case SHT_REL :return elf_rebuild_rel_reloc_section (sec, nr); +case SHT_RELA :return elf_rebuild_rela_reloc_section (sec, nr); +default :return -1 ; + } +} +int elf_write (const struct elf *elf) {struct section *sec;
tools/objtool/elf.h +4 -1
@@ -61,7 +61,10 @@ struct symbol {struct reloc {struct list_head list;struct hlist_node hash; -GElf_Rela rela; +union { +GElf_Rela rela; +GElf_Rel rel; + };struct section *sec;struct symbol *sym;unsigned int type;
Signature · 1 line
2.20.1
RFC PATCH 2/3 objtool: Find relocation base section using sh_info
In reply to Matt HelsleyCurrently objtool uses a naming heuristic to find the "base" section to apply the relocation(s) to. The standard defines the SHF_INFO_LINK flag (SHF => in the section header flags) which indicates when the section header's sh_info field can be used to find the necessary section.
Warns when the heuristic is used as a fallback and changes the name heuristic calculation to handle rela (explicit addend) and now rel (implicit addend) relocations.
--- tools/objtool/elf.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/objtool/elf.c +7 -1
@@ -502,7 +502,13 @@ static int read_relocs(struct elf *elf)if (sec->sh.sh_type !=SHT_RELA )continue ; - sec->base =find_section_by_name (elf, sec->name +5 ); +if (sec->sh.sh_flags &SHF_INFO_LINK ) { + sec->base =find_section_by_index (elf, sec->sh.sh_info); + }else if (strncmp (sec->name,".rel" ,4 ) ==0 ) { +WARN ("Using naming heuristic to find base of reloc section %s" , + sec->name); + sec->base =find_section_by_name (elf, sec->name +5 ); + }if (!sec->base) {WARN ("can't find base section for reloc section %s" , sec->name);
Signature · 1 line
2.20.1
Re: [RFC][PATCH 1/3] objtool: Rename rela to reloc
In reply to Matt HelsleyQuoted 8 lines · Matt Helsley
Before supporting additional relocation types rename the relevant types and functions from "rela" to "reloc". This work can largely be done with the following regex:
Notable exceptions include gelf_* library calls and standard/expected section names which still use "rela" because they encode the type of relocation expected. Also, keep "rela" in the struct because it encodes a specify type of relocation
Missing regex?
BTW it would be really nice if the regex were exact if possible, for stable backporting reasons.
Signature · 1 line
Josh
Re: [RFC][PATCH 2/3] objtool: Find relocation base section using sh_info
In reply to Matt HelsleyQuoted 9 lines · Matt Helsley
Currently objtool uses a naming heuristic to find the "base" section to apply the relocation(s) to. The standard defines the
SHF_INFO_LINKflag (SHF => in the section header flags) which indicates when the section header'ssh_infofield can be used to find the necessary section.Warns when the heuristic is used as a fallback and changes the name heuristic calculation to handle rela (explicit addend) and now rel (implicit addend) relocations.
Does this fallback case actually happen?
Signature · 1 line
Josh
Re: [RFC][PATCH 3/3] objtool: Add support for relocations without addends
In reply to Matt Helsleytools/objtool/elf.c static int read_relocs(struct elf *elf) · line 532
Quoted 10 lines · Matt Helsley
tools/objtool/elf.c +1
-4@@ -525,16 +556,8 @@ static int read_relocs(struct elf *elf)return -1 ; }memset (reloc,0 ,sizeof (*reloc)); - -if (!gelf_getrela (sec->data, i, &reloc->rela)) { -WARN_ELF ("gelf_getrela" ); +if (read_reloc (sec, i, reloc, &symndx))return -1 ; - }
I try to avoid function pointers, them being slow and ugly and all.
Since it's just a single call site, I'd rather a direct call. For example, call read_reloc(), which has an if-then to decide which subfunction to call.
Signature · 1 line
Josh
Re: [RFC][PATCH 1/3] objtool: Rename rela to reloc
In reply to Josh PoimboeufQuoted 11 lines · Josh Poimboeuf
Quoted 8 lines · Matt Helsley
Before supporting additional relocation types rename the relevant types and functions from "rela" to "reloc". This work can largely be done with the following regex:
Notable exceptions include gelf_* library calls and standard/expected section names which still use "rela" because they encode the type of relocation expected. Also, keep "rela" in the struct because it encodes a specify type of relocation
Missing regex?
I briefly looked into it and went with a manual process instead. I can try and work backwards to reproduce the patch with a regex though.
Quoted 2 lines
BTW it would be really nice if the regex were exact if possible, for stable backporting reasons.
Agreed. I'll see what I can come up with and put it and any necessary details in the commit message for the next posting.
Cheers,
-Matt Helsley
Re: [RFC][PATCH 2/3] objtool: Find relocation base section using sh_info
In reply to Josh PoimboeufQuoted 7 lines · Josh Poimboeuf
Quoted 6 lines · Matt Helsley
Currently objtool uses a naming heuristic to find the "base" section to apply the relocation(s) to. The standard defines the
SHF_INFO_LINKflag (SHF => in the section header flags) which indicates when the section header'ssh_infofield can be used to find the necessary section.
Quoted 5 lines
Quoted 3 lines
Warns when the heuristic is used as a fallback and changes the name heuristic calculation to handle rela (explicit addend) and now rel (implicit addend) relocations.
Does this fallback case actually happen?
Not that I could see. I was thinking about taking it out but I haven't tried this set with clang or other toolchains. So I was wondering if you think holding off before removing it would be wise or if you'd rather just remove it.
Cheers,
-Matt Helsley
Re: [RFC][PATCH 3/3] objtool: Add support for relocations without addends
In reply to Josh PoimboeufQuoted 14 lines · Josh Poimboeuf
tools/objtool/elf.c static int read_relocs(struct elf *elf) · line 532
Quoted 10 lines · Matt Helsley
tools/objtool/elf.c +1
-4@@ -525,16 +556,8 @@ static int read_relocs(struct elf *elf)return -1 ; }memset (reloc,0 ,sizeof (*reloc)); - -if (!gelf_getrela (sec->data, i, &reloc->rela)) { -WARN_ELF ("gelf_getrela" ); +if (read_reloc (sec, i, reloc, &symndx))return -1 ; - }I try to avoid function pointers, them being slow and ugly and all.
Quoted 3 lines
Since it's just a single call site, I'd rather a direct call. For example, call
read_reloc(), which has an if-then to decide which subfunction to call.
OK, I'll take the function pointers out in favor of switches.
Cheers,
-Matt Helsley
Re: [RFC][PATCH 2/3] objtool: Find relocation base section using sh_info
In reply to Matt HelsleyQuoted 12 lines · Matt Helsley
Quoted 11 lines · Josh Poimboeuf
Quoted 9 lines · Matt Helsley
Currently objtool uses a naming heuristic to find the "base" section to apply the relocation(s) to. The standard defines the
SHF_INFO_LINKflag (SHF => in the section header flags) which indicates when the section header'ssh_infofield can be used to find the necessary section.Warns when the heuristic is used as a fallback and changes the name heuristic calculation to handle rela (explicit addend) and now rel (implicit addend) relocations.
Quoted 6 lines
Quoted 1 line
Does this fallback case actually happen?
Not that I could see. I was thinking about taking it out but I haven't tried this set with clang or other toolchains. So I was wondering if you think holding off before removing it would be wise or if you'd rather just remove it.
I just realized somebody already submitted an almost identical patch:
https://lkml.kernel.org/r/[email protected]
Which I'll be merging soon... so you can just drop this one.
Then you can base your next version of this set on top of that patch, if it hasn't been merged yet.
Signature · 1 line
Josh
Re: [RFC][PATCH 2/3] objtool: Find relocation base section using sh_info
Quoted 20 lines · Josh Poimboeuf
Quoted 12 lines · Matt Helsley
Quoted 11 lines · Josh Poimboeuf
Quoted 9 lines · Matt Helsley
Currently objtool uses a naming heuristic to find the "base" section to apply the relocation(s) to. The standard defines the
SHF_INFO_LINKflag (SHF => in the section header flags) which indicates when the section header'ssh_infofield can be used to find the necessary section.Warns when the heuristic is used as a fallback and changes the name heuristic calculation to handle rela (explicit addend) and now rel (implicit addend) relocations.
Quoted 6 lines
Quoted 1 line
Does this fallback case actually happen?
Not that I could see. I was thinking about taking it out but I haven't tried this set with clang or other toolchains. So I was wondering if you think holding off before removing it would be wise or if you'd rather just remove it.
Quoted 5 lines
I just realized somebody already submitted an almost identical patch:
https://lkml.kernel.org/r/[email protected]
Which I'll be merging soon... so you can just drop this one.
OK, I'll drop this patch.
Cheers,
-Matt