objtool: Constify most of the instruction decoding loop
cover objtool: Constify most of the instruction decoding loop
Constify some of the decoding primitives, to ascertain and enforce that arch_decode_instructions() is a read-only consumer of the various ELF data structures. (Which it is.)
This is in preparation to parallelize the most expensive parts of objtool, but makes sense independently as well.
These bits can also be found at:
git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git WIP.core/objtool
(Subject to rebasing.)
Thanks,
Ingo
====
Ingo Molnar (3):
objtool: Constify 'struct elf *' parameters
objtool: Rename elf_read() to elf_open_read()
objtool: Constify arch_decode_instruction()
tools/objtool/arch.h | 2 +- tools/objtool/arch/x86/decode.c | 6 +++--- tools/objtool/check.c | 2 +- tools/objtool/elf.c | 12 ++++++------ tools/objtool/elf.h | 22 +++++++++++----------- 5 files changed, 22 insertions(+), 22 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/3 objtool: Constify 'struct elf *' parameters
In preparation to parallelize certain parts of objtool, map out which uses of various data structures are read-only vs. read-write.
As a first step constify 'struct elf' pointer passing, most of the secondary uses of it in find_symbol_*() methods are read-only.
Also, while at it, better group the 'struct elf' handling methods in elf.h.
--- tools/objtool/elf.c | 10 +++++----- tools/objtool/elf.h | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-)
tools/objtool/elf.c +5 -5
@@ -127,7 +127,7 @@ static int symbol_by_offset(const void *key, const struct rb_node *node)return 0 ; } -struct section *find_section_by_name (struct elf *elf,const char *name) +struct section *find_section_by_name (const struct elf *elf,const char *name) {struct section *sec;
@@ -217,7 +217,7 @@ struct symbol *find_func_containing(struct section *sec, unsigned long offset)return NULL ; } -struct symbol *find_symbol_by_name (struct elf *elf,const char *name) +struct symbol *find_symbol_by_name (const struct elf *elf,const char *name) {struct symbol *sym;
@@ -228,7 +228,7 @@ struct symbol *find_symbol_by_name(struct elf *elf, const char *name)return NULL ; } -struct rela *find_rela_by_dest_range (struct elf *elf,struct section *sec, +struct rela *find_rela_by_dest_range (const struct elf *elf,struct section *sec,unsigned long offset,unsigned int len) {struct rela *rela, *r =NULL ;
@@ -257,7 +257,7 @@ struct rela *find_rela_by_dest_range(struct elf *elf, struct section *sec,return NULL ; } -struct rela *find_rela_by_dest (struct elf *elf,struct section *sec,unsigned long offset) +struct rela *find_rela_by_dest (const struct elf *elf,struct section *sec,unsigned long offset) {return find_rela_by_dest_range (elf, sec, offset,1 ); }
@@ -769,7 +769,7 @@ int elf_rebuild_rela_section(struct section *sec)return 0 ; } -int elf_write (struct elf *elf) +int elf_write (const struct elf *elf) {struct section *sec;Elf_Scn *s;
tools/objtool/elf.h +10 -10
@@ -114,22 +114,22 @@ static inline u32 rela_hash(struct rela *rela) }struct elf *elf_read (const char *name,int flags); -struct section *find_section_by_name (struct elf *elf,const char *name); +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); +int elf_write (const struct elf *elf); +void elf_close (struct elf *elf); + +struct section *find_section_by_name (const struct elf *elf,const char *name);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 (struct elf *elf,const char *name); +struct symbol *find_symbol_by_name (const struct elf *elf,const char *name);struct symbol *find_symbol_containing (struct section *sec,unsigned long offset); -struct rela *find_rela_by_dest (struct elf *elf,struct section *sec,unsigned long offset); -struct rela *find_rela_by_dest_range (struct elf *elf,struct section *sec, +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,unsigned long offset,unsigned int len);struct symbol *find_func_containing (struct section *sec,unsigned long offset); -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);int elf_rebuild_rela_section (struct section *sec); -int elf_write (struct elf *elf); -void elf_close (struct elf *elf); -void elf_add_rela (struct elf *elf,struct rela *rela);#define for_each_sec (file, sec) \ list_for_each_entry(sec, &file->elf->sections, list)
Signature · 1 line
2.20.1
PATCH 2/3 objtool: Rename elf_read() to elf_open_read()
In reply to Ingo Molnar'struct elf *' handling is an open/close paradigm, make sure the naming matches that:
elf_open_read()
elf_write()
elf_close()
--- tools/objtool/check.c | 2 +- tools/objtool/elf.c | 2 +- tools/objtool/elf.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
tools/objtool/check.c +1 -1
@@ -2614,7 +2614,7 @@ int check(const char *_objname, bool orc) objname = _objname; - file.elf =elf_read (objname, orc ?O_RDWR :O_RDONLY ); + file.elf =elf_open_read (objname, orc ?O_RDWR :O_RDONLY );if (!file.elf)return 1 ;
tools/objtool/elf.c +1 -1
@@ -542,7 +542,7 @@ static int read_relas(struct elf *elf)return 0 ; } -struct elf *elf_read (const char *name,int flags) +struct elf *elf_open_read (const char *name,int flags) {struct elf *elf;Elf_Cmd cmd;
tools/objtool/elf.h +1 -1
@@ -113,7 +113,7 @@ static inline u32 rela_hash(struct rela *rela)return sec_offset_hash (rela->sec, rela->offset); } -struct elf *elf_read (const char *name,int flags); +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);
Signature · 1 line
2.20.1
PATCH 3/3 objtool: Constify arch_decode_instruction()
In reply to Ingo MolnarMostly straightforward constification, except that WARN_FUNC() needs a writable pointer while we have read-only pointers, so deflect this to WARN().
--- tools/objtool/arch.h | 2 +- tools/objtool/arch/x86/decode.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
tools/objtool/arch.h +1 -1
@@ -72,7 +72,7 @@ struct instruction;void arch_initial_func_cfi_state (struct cfi_init_state *state); -int arch_decode_instruction(struct elf *elf, struct section *sec, +int arch_decode_instruction(const struct elf *elf, const struct section *sec, unsigned long offset, unsigned int maxlen, unsigned int *len, enum insn_type *type, unsigned long *immediate,
tools/objtool/arch/x86/decode.c +3 -3
@@ -27,7 +27,7 @@ static unsigned char op_to_cfi_reg[][2] = { {CFI_DI, CFI_R15}, }; -static int is_x86_64 (struct elf *elf) +static int is_x86_64 (const struct elf *elf) {switch (elf->ehdr.e_machine) {case EM_X86_64 :
@@ -77,7 +77,7 @@ unsigned long arch_jump_destination(struct instruction *insn)return insn->offset + insn->len + insn->immediate; } -int arch_decode_instruction(struct elf *elf, struct section *sec, +int arch_decode_instruction(const struct elf *elf, const struct section *sec, unsigned long offset, unsigned int maxlen, unsigned int *len, enum insn_type *type, unsigned long *immediate,
@@ -98,7 +98,7 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,insn_get_length (&insn);if (!insn_complete (&insn)) { -WARN_FUNC ("can't decode instruction" , sec, offset); +WARN ("can't decode instruction at %s:0x%lx" , sec->name, offset);return -1 ; }
Signature · 1 line
2.20.1
Peter Zijlstra
maintainerRe: [PATCH 2/3] objtool: Rename elf_read() to elf_open_read()
In reply to Ingo MolnarQuoted 13 lines · Ingo Molnar
'struct elf *' handling is an open/close paradigm, make sure the naming matches that:
elf_open_read() elf_write() elf_close()tools/objtool/check.c +0
-0
tools/objtool/check.c int check(const char *_objname, bool orc) · line 2617
Quoted 5 lines
tools/objtool/check.c +1
-1@@ -2614,7 +2614,7 @@ int check(const char *_objname, bool orc) objname = _objname; - file.elf =elf_read (objname, orc ?O_RDWR :O_RDONLY ); + file.elf =elf_open_read (objname, orc ?O_RDWR :O_RDONLY );
Note that I have a patch pending that makes that unconditionally O_RDWR, which sort of seems to suggest elf_open() might be the better name.
Re: [PATCH 2/3] objtool: Rename elf_read() to elf_open_read()
Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · Ingo Molnar
'struct elf *' handling is an open/close paradigm, make sure the naming matches that:
elf_open_read() elf_write() elf_close()tools/objtool/check.c +0
-0@@ -2614,7 +2614,7 @@ int check(const char *_objname, bool orc)
Quoted 7 lines
tools/objtool/check.c int check(const char *_objname, bool orc) · line 2617
Quoted 4 lines
tools/objtool/check.c +1
-1objname = _objname; - file.elf =elf_read (objname, orc ?O_RDWR :O_RDONLY ); + file.elf =elf_open_read (objname, orc ?O_RDWR :O_RDONLY );Note that I have a patch pending that makes that unconditionally
O_RDWR, which sort of seems to suggestelf_open()might be the better name.
Ok, done!
Thanks,
Ingo
Re: [PATCH 2/3] objtool: Rename elf_read() to elf_open_read()
Quoted 20 lines · Ingo Molnar
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Ingo Molnar
'struct elf *' handling is an open/close paradigm, make sure the naming matches that:
elf_open_read() elf_write() elf_close()tools/objtool/check.c +0
-0@@ -2614,7 +2614,7 @@ int check(const char *_objname, bool orc) objname = _objname;
Quoted 7 lines
Quoted 5 lines
tools/objtool/check.c int check(const char *_objname, bool orc) · line 2617
Quoted 2 lines
tools/objtool/check.c +1
-1- file.elf =elf_read (objname, orc ?O_RDWR :O_RDONLY ); + file.elf =elf_open_read (objname, orc ?O_RDWR :O_RDONLY );Note that I have a patch pending that makes that unconditionally
O_RDWR, which sort of seems to suggestelf_open()might be the better name.Ok, done!
It might be a better name but there could be a problem with it -- see 8e144797f1a67c52e386161863da4614a23ad913
"objtool: Rename elf_open() to prevent conflict with libelf from elftoolchain"
Unless I'm forgetting something I think that'd still be an issue.
Cheers,
-Matt
Re: [PATCH 2/3] objtool: Rename elf_read() to elf_open_read()
Quoted 27 lines · Matt Helsley
Quoted 26 lines · Ingo Molnar
Quoted 22 lines · Peter Zijlstra
Quoted 13 lines · Ingo Molnar
'struct elf *' handling is an open/close paradigm, make sure the naming matches that:
elf_open_read() elf_write() elf_close()tools/objtool/check.c +0
-0tools/objtool/check.c int check(const char *_objname, bool orc) · line 2617
Quoted 5 lines
tools/objtool/check.c +1
-1@@ -2614,7 +2614,7 @@ int check(const char *_objname, bool orc) objname = _objname; - file.elf =elf_read (objname, orc ?O_RDWR :O_RDONLY ); + file.elf =elf_open_read (objname, orc ?O_RDWR :O_RDONLY );Note that I have a patch pending that makes that unconditionally
O_RDWR, which sort of seems to suggestelf_open()might be the better name.
Quoted 7 lines
Quoted 1 line
Ok, done!
It might be a better name but there could be a problem with it -- see
8e144797f1a67c52e386161863da4614a23ad913
"objtool: Renameelf_open()to prevent conflict with libelf from elftoolchain"Unless I'm forgetting something I think that'd still be an issue.
Indeed, that's a good point - I'll move back to elf_open_read()! :-)
Thanks,
Ingo
Quoted 10 lines · Ingo Molnar
Constify some of the decoding primitives, to ascertain and enforce that
arch_decode_instructions()is a read-only consumer of the various ELF data structures. (Which it is.)This is in preparation to parallelize the most expensive parts of objtool, but makes sense independently as well.
These bits can also be found at:
git
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gitWIP.core/objtool
Signature · 1 line
Josh
[tip: objtool/core] objtool: Constify arch_decode_instruction()
In reply to Ingo MolnarThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 0c98be8118221a8d3de572740f29dd02ed9686a5 Gitweb: https://git.kernel.org/tip/0c98be8118221a8d3de572740f29dd02ed9686a5 Author: Ingo Molnar <[email protected]> AuthorDate: Wed, 22 Apr 2020 12:32:05 +02:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 23 Apr 2020 08:34:18 +02:00
objtool: Constify arch_decode_instruction()
Mostly straightforward constification, except that WARN_FUNC() needs a writable pointer while we have read-only pointers, so deflect this to WARN().
--- tools/objtool/arch.h | 2 +- tools/objtool/arch/x86/decode.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
tools/objtool/arch.h +1 -1
@@ -72,7 +72,7 @@ struct instruction;void arch_initial_func_cfi_state (struct cfi_init_state *state); -int arch_decode_instruction(struct elf *elf, struct section *sec, +int arch_decode_instruction(const struct elf *elf, const struct section *sec, unsigned long offset, unsigned int maxlen, unsigned int *len, enum insn_type *type, unsigned long *immediate,
tools/objtool/arch/x86/decode.c +3 -3
@@ -27,7 +27,7 @@ static unsigned char op_to_cfi_reg[][2] = { {CFI_DI, CFI_R15}, }; -static int is_x86_64 (struct elf *elf) +static int is_x86_64 (const struct elf *elf) {switch (elf->ehdr.e_machine) {case EM_X86_64 :
@@ -77,7 +77,7 @@ unsigned long arch_jump_destination(struct instruction *insn)return insn->offset + insn->len + insn->immediate; } -int arch_decode_instruction(struct elf *elf, struct section *sec, +int arch_decode_instruction(const struct elf *elf, const struct section *sec, unsigned long offset, unsigned int maxlen, unsigned int *len, enum insn_type *type, unsigned long *immediate,
@@ -98,7 +98,7 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,insn_get_length (&insn);if (!insn_complete (&insn)) { -WARN_FUNC ("can't decode instruction" , sec, offset); +WARN ("can't decode instruction at %s:0x%lx" , sec->name, offset);return -1 ; }
[tip: objtool/core] objtool: Rename elf_read() to elf_open_read()
In reply to Ingo MolnarThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: bc359ff2f6f3e8a9df38c39017e269bc442357c7 Gitweb: https://git.kernel.org/tip/bc359ff2f6f3e8a9df38c39017e269bc442357c7 Author: Ingo Molnar <[email protected]> AuthorDate: Wed, 22 Apr 2020 12:32:04 +02:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 23 Apr 2020 08:34:18 +02:00
objtool: Rename elf_read() to elf_open_read()
'struct elf *' handling is an open/close paradigm, make sure the naming matches that:
elf_open_read()
elf_write()
elf_close()
--- tools/objtool/check.c | 2 +- tools/objtool/elf.c | 2 +- tools/objtool/elf.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
tools/objtool/check.c +1 -1
@@ -2614,7 +2614,7 @@ int check(const char *_objname, bool orc) objname = _objname; - file.elf =elf_read (objname, orc ?O_RDWR :O_RDONLY ); + file.elf =elf_open_read (objname, orc ?O_RDWR :O_RDONLY );if (!file.elf)return 1 ;
tools/objtool/elf.c +1 -1
@@ -542,7 +542,7 @@ static int read_relas(struct elf *elf)return 0 ; } -struct elf *elf_read (const char *name,int flags) +struct elf *elf_open_read (const char *name,int flags) {struct elf *elf;Elf_Cmd cmd;
tools/objtool/elf.h +1 -1
@@ -113,7 +113,7 @@ static inline u32 rela_hash(struct rela *rela)return sec_offset_hash (rela->sec, rela->offset); } -struct elf *elf_read (const char *name,int flags); +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);
[tip: objtool/core] objtool: Constify 'struct elf *' parameters
In reply to Ingo MolnarThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 894e48cada64ec384873fad4fe1b0d0c7de31a29 Gitweb: https://git.kernel.org/tip/894e48cada64ec384873fad4fe1b0d0c7de31a29 Author: Ingo Molnar <[email protected]> AuthorDate: Wed, 22 Apr 2020 12:32:03 +02:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 23 Apr 2020 08:34:18 +02:00
objtool: Constify 'struct elf *' parameters
In preparation to parallelize certain parts of objtool, map out which uses of various data structures are read-only vs. read-write.
As a first step constify 'struct elf' pointer passing, most of the secondary uses of it in find_symbol_*() methods are read-only.
Also, while at it, better group the 'struct elf' handling methods in elf.h.
--- tools/objtool/elf.c | 10 +++++----- tools/objtool/elf.h | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-)
tools/objtool/elf.c +5 -5
@@ -127,7 +127,7 @@ static int symbol_by_offset(const void *key, const struct rb_node *node)return 0 ; } -struct section *find_section_by_name (struct elf *elf,const char *name) +struct section *find_section_by_name (const struct elf *elf,const char *name) {struct section *sec;
@@ -217,7 +217,7 @@ struct symbol *find_func_containing(struct section *sec, unsigned long offset)return NULL ; } -struct symbol *find_symbol_by_name (struct elf *elf,const char *name) +struct symbol *find_symbol_by_name (const struct elf *elf,const char *name) {struct symbol *sym;
@@ -228,7 +228,7 @@ struct symbol *find_symbol_by_name(struct elf *elf, const char *name)return NULL ; } -struct rela *find_rela_by_dest_range (struct elf *elf,struct section *sec, +struct rela *find_rela_by_dest_range (const struct elf *elf,struct section *sec,unsigned long offset,unsigned int len) {struct rela *rela, *r =NULL ;
@@ -257,7 +257,7 @@ struct rela *find_rela_by_dest_range(struct elf *elf, struct section *sec,return NULL ; } -struct rela *find_rela_by_dest (struct elf *elf,struct section *sec,unsigned long offset) +struct rela *find_rela_by_dest (const struct elf *elf,struct section *sec,unsigned long offset) {return find_rela_by_dest_range (elf, sec, offset,1 ); }
@@ -769,7 +769,7 @@ int elf_rebuild_rela_section(struct section *sec)return 0 ; } -int elf_write (struct elf *elf) +int elf_write (const struct elf *elf) {struct section *sec;Elf_Scn *s;
tools/objtool/elf.h +10 -10
@@ -114,22 +114,22 @@ static inline u32 rela_hash(struct rela *rela) }struct elf *elf_read (const char *name,int flags); -struct section *find_section_by_name (struct elf *elf,const char *name); +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); +int elf_write (const struct elf *elf); +void elf_close (struct elf *elf); + +struct section *find_section_by_name (const struct elf *elf,const char *name);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 (struct elf *elf,const char *name); +struct symbol *find_symbol_by_name (const struct elf *elf,const char *name);struct symbol *find_symbol_containing (struct section *sec,unsigned long offset); -struct rela *find_rela_by_dest (struct elf *elf,struct section *sec,unsigned long offset); -struct rela *find_rela_by_dest_range (struct elf *elf,struct section *sec, +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,unsigned long offset,unsigned int len);struct symbol *find_func_containing (struct section *sec,unsigned long offset); -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);int elf_rebuild_rela_section (struct section *sec); -int elf_write (struct elf *elf); -void elf_close (struct elf *elf); -void elf_add_rela (struct elf *elf,struct rela *rela);#define for_each_sec (file, sec) \ list_for_each_entry(sec, &file->elf->sections, list)