objtool: Make recordmcount a subcommand
Subsystems (8)
v4 cover objtool: Make recordmcount a subcommand
recordmcount has its own ELF wrapper code and could utilize objtool's ELF code to more-portably handle architecture variations. This series makes recordmcount a subcommand of objtool. It very gradually convert recordmcount to become a subcommand of objtool and then reuses parts of objtool's ELF code. recordmcount maps the file in and collects simple information it needs to append a section to the object file. The only part of the original file it modifies is the address of new section tables -- interestingly enough this resembles RCU in that we don't really trim the old tables so much as unlink them via a critical offset and then rely on future tooling, in this case, to drop the unused bits. Much of the recordmcount ELF code is only reading and walking the data structures to collect the mcount locations it records in a separate area of memory. This means it's safe to mix access to the mapped file with access to the objtool-style linked data structures as we gradually convert it to using only the linked data structures. Once the old ELF code is no longer in use we can drop it and use objtool to take over the task of writing the results without using the RCU-like trick any more.
Testing:
I've been using scripts to test cross compilation and execution of objtool, and mcount on objects built for x86, ppc64le, arm64, s390, and sparc. I used PowerPC as a sample arch for fixing a bug (see Changes) and confirmed it builds a full zImage with defconfig (CONFIG_DYNAMIC_FTRACE=y).
Changes
v4:
Split out recordmcount cleanups and upstreamed.
[ https://lore.kernel.org/lkml/[email protected]/ ]
Split out and iterated on objtool multi-arch support.
[ https://lore.kernel.org/lkml/[email protected]/ ]
Split out expanded relocation support, renamed types, and functions to reflect expanded relocation support, and posted.
[ https://lore.kernel.org/lkml/[email protected]/ ]
This set is based on the patches sent upstream and posted above.
Adapted to renames by Ingo and Peter: s/elf_open/elf_open_read/
Added weak symbols for mcount subcommand
This nicely eliminated the need for the mcount.h header.
Added tools/objtool/Makefile per-arch SUBCMD_ blocks for each
arch recordmcount / mcount supports.
Moved ftrace/mcount/record.h from objtool_dep to recordmcount_dep
This keeps the dependencies better organized.
Fixed Makefile issue reported for PowerPC and a couple other archs
by kbuild test robot. The always-$(BUILD_C_RECORDMCOUNT) line wasn't sufficiently replaced. Added to prepare-objtool target in top level Makefile.
Split up dependencies to be independent of CONFIG_STACK_VALIDATION
and CONFIG_UNWINDER_ORC since these are x86-specific. Now any arch which uses the C version of recordmcount will build objtool if dynamic tracing is enabled.
Added a second rename at the end to be consistent with other
objtool subcommands.
v3:
Rebased on mainline. s/elf_open/elf_read/ in recordmcount.c
v2:
Fix whitespace before line continuation
Add ftrace/mcount/record.h to objtool_dep
Rename the Makefile variable BUILD_C_RECORDMCOUNT to
better reflect its purpose
Similar: rename recordmcount_source => recordmcount_dep
When using objtool we can just depend on the binary rather than the source the binary is built from. This should address Josh's feedback and make the Makefile code a bit clearer
Add a comment to make reading the Makefile a little
easier
Rebased to latest mainline -rc
Matt Helsley (32):
objtool: Prepare to merge recordmcount
objtool: Make recordmcount into mcount subcmd
objtool: recordmcount: Start using objtool's elf wrapper
objtool: recordmcount: Search for __mcount_loc before walking the
sections
objtool: recordmcount: Convert do_func() relhdrs
objtool: mcount: Remove unused fname parameter
objtool: mcount: Use libelf for section header names
objtool: mcount: Walk objtool Elf structs in find_secsym_ndx
objtool: mcount: Use symbol structs to find mcount relocations
objtool: mcount: Walk relocation lists
objtool: mcount: Move get_mcountsym
objtool: mcount: Replace MIPS offset types
objtool: mcount: Move is_fake_mcount()
objtool: mcount: Stop using ehdr in find_section_sym_index
objtool: mcount: Move find_section_sym_index()
objtool: mcount: Restrict using ehdr in append_func()
objtool: mcount: Use objtool ELF to write
objtool: mcount: Move nop_mcount()
objtool: mcount: Move has_rel_mcount() and tot_relsize()
objtool: mcount: Move relocation entry size detection
objtool: mcount: Only keep ELF file size
objtool: mcount: Use ELF header from objtool
objtool: mcount: Remove unused file mapping
objtool: mcount: Reduce usage of _size wrapper
objtool: mcount: Move mcount_adjust out of wrapper
objtool: mcount: Pre-allocate new ELF sections
objtool: mcount: Generic location and relocation table types
objtool: mcount: Move sift_rel_mcount out of wrapper file
objtool: mcount: Remove wrapper for ELF relocation type
objtool: mcount: Remove wrapper double-include trick
objtool: mcount: Remove endian wrappers
objtool: mcount: Rename
Documentation/dontdiff | 2 +-
Documentation/trace/ftrace-design.rst | 4 +-
Documentation/trace/ftrace.rst | 6 +-
Makefile | 24 +-
arch/arm64/include/asm/ftrace.h | 2 +-
arch/x86/include/asm/ftrace.h | 2 +-
kernel/trace/Kconfig | 2 +-
scripts/.gitignore | 1 -
scripts/Makefile | 1 -
scripts/Makefile.build | 22 +-
scripts/recordmcount.c | 663 --------------------
scripts/recordmcount.h | 606 ------------------
scripts/sorttable.h | 2 +-
tools/objtool/.gitignore | 1 +
tools/objtool/Build | 2 +
tools/objtool/Makefile | 37 +-
tools/objtool/builtin-mcount.c | 50 ++
tools/objtool/builtin.h | 2 +
tools/objtool/elf.c | 5 +-
tools/objtool/elf.h | 2 +
tools/objtool/mcount.c | 682 +++++++++++++++++++++
tools/objtool/objtool.c | 1 +
tools/objtool/objtool.h | 1 +
{scripts => tools/objtool}/recordmcount.pl | 0
tools/objtool/weak.c | 5 +
25 files changed, 821 insertions(+), 1304 deletions(-)
delete mode 100644 scripts/recordmcount.c
delete mode 100644 scripts/recordmcount.h
create mode 100644 tools/objtool/builtin-mcount.c
create mode 100644 tools/objtool/mcount.c
rename {scripts => tools/objtool}/recordmcount.pl (100%)base-commit: 2160e6958fc4fa0a70d5f8a2f32760c7ab3bfd6c
Signature · 1 line
2.20.1
RFC PATCH v4 1/32 objtool: Prepare to merge recordmcount
Move recordmcount into the objtool directory. We keep this step separate so changes which turn recordmcount into a subcommand of objtool don't get obscured.
---
Documentation/trace/ftrace-design.rst | 4 ++--
Documentation/trace/ftrace.rst | 2 +-
Makefile | 15 +++++++++------
scripts/.gitignore | 1 -
scripts/Makefile | 1 -
scripts/Makefile.build | 11 ++++++-----
tools/objtool/.gitignore | 1 +
tools/objtool/Build | 2 ++
tools/objtool/Makefile | 13 ++++++++++++-
{scripts => tools/objtool}/recordmcount.c | 0
{scripts => tools/objtool}/recordmcount.h | 0
{scripts => tools/objtool}/recordmcount.pl | 0
12 files changed, 33 insertions(+), 17 deletions(-)
rename {scripts => tools/objtool}/recordmcount.c (100%)
rename {scripts => tools/objtool}/recordmcount.h (100%)
rename {scripts => tools/objtool}/recordmcount.pl (100%)Documentation/trace/ftrace-design.rst +2 -2
@@ -261,7 +261,7 @@ You need very few things to get the syscalls tracing in an arch. HAVE_FTRACE_MCOUNT_RECORD ------------------------- -See scripts/recordmcount.pl for more info. Just fill in the arch-specific +See tools/objtool/recordmcount.pl for more info. Just fill in the arch-specific details for how to locate the addresses of mcount call sites via objdump. This option doesn't make much sense without also implementing dynamic ftrace.
@@ -379,7 +379,7 @@ linux/ftrace.h for the functions:: ftrace_make_call() The rec->ip value is the address of the mcount call site that was collected -by the scripts/recordmcount.pl during build time. +by the tools/objtool/recordmcount.pl during build time. The last function is used to do runtime patching of the active tracer. This will be modifying the assembly code at the location of the ftrace_call symbol
Documentation/trace/ftrace.rst +1 -1
@@ -2685,7 +2685,7 @@ starts of pointing to a simple return. (Enabling FTRACE will include the -pg switch in the compiling of the kernel.) At compile time every C file object is run through the -recordmcount program (located in the scripts directory). This +recordmcount program (located in the tools/objtool directory). This program will parse the ELF headers in the C object to find all the locations in the .text section that call mcount. Starting with gcc version 4.6, the -mfentry has been added for x86, which
Makefile +9 -6
@@ -844,6 +844,7 @@ ifdef CONFIG_DYNAMIC_FTRACE ifdef CONFIG_HAVE_C_RECORDMCOUNT BUILD_C_RECORDMCOUNT := y export BUILD_C_RECORDMCOUNT + objtool_target := tools/objtool FORCE endif endif endif
@@ -1023,10 +1024,10 @@ endif export mod_sign_cmd HOST_LIBELF_LIBS = $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf) +has_libelf := $(call try-run,\ + echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0) ifdef CONFIG_STACK_VALIDATION - has_libelf := $(call try-run,\ - echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0) ifeq ($(has_libelf),1) objtool_target := tools/objtool FORCE else
@@ -1163,13 +1164,15 @@ uapi-asm-generic: PHONY += prepare-objtool prepare-objtool: $(objtool_target) -ifeq ($(SKIP_STACK_VALIDATION),1) -ifdef CONFIG_UNWINDER_ORC +ifneq ($(has_libelf),1) + ifdef CONFIG_UNWINDER_ORC @echo "error: Cannot generate ORC metadata for CONFIG_UNWINDER_ORC=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2 @false -else + else + ifeq ($(SKIP_STACK_VALIDATION),1) @echo "warning: Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2 -endif + endif + endif endif # Generate some files
scripts/.gitignore +0 -1
@@ -2,7 +2,6 @@ bin2c kallsyms unifdef -recordmcount sorttable asn1_compiler extract-cert
scripts/Makefile +0 -1
@@ -5,7 +5,6 @@ always-$(CONFIG_BUILD_BIN2C) += bin2c always-$(CONFIG_KALLSYMS) += kallsyms -always-$(BUILD_C_RECORDMCOUNT) += recordmcount always-$(CONFIG_BUILDTIME_TABLE_SORT) += sorttable always-$(CONFIG_ASN1) += asn1_compiler always-$(CONFIG_MODULE_SIG_FORMAT) += sign-file
scripts/Makefile.build +6 -5
@@ -174,18 +174,19 @@ endif # files, including recordmcount. sub_cmd_record_mcount = \ if [ $(@) != "scripts/mod/empty.o" ]; then \ - $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \ + $(objtree)/tools/objtool/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \ fi; -recordmcount_source := $(srctree)/scripts/recordmcount.c \ - $(srctree)/scripts/recordmcount.h + +recordmcount_source := $(srctree)/tools/objtool/recordmcount.c \ + $(srctree)/tools/objtool/recordmcount.h else -sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \ +sub_cmd_record_mcount = perl $(srctree)/tools/objtool/recordmcount.pl "$(ARCH)" \ "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \ "$(if $(CONFIG_64BIT),64,32)" \ "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \ "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \ "$(if $(part-of-module),1,0)" "$(@)"; -recordmcount_source := $(srctree)/scripts/recordmcount.pl +recordmcount_source := $(srctree)/tools/objtool/recordmcount.pl endif # BUILD_C_RECORDMCOUNT cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \ $(sub_cmd_record_mcount))
tools/objtool/.gitignore +1 -0
@@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only arch/x86/lib/inat-tables.c objtool +recordmcount fixdep
tools/objtool/Build +2 -0
@@ -35,3 +35,5 @@ $(OUTPUT)str_error_r.o: ../lib/str_error_r.c FORCE $(OUTPUT)librbtree.o: ../lib/rbtree.c FORCE $(call rule_mkdir) $(call if_changed_dep,cc_o_c) + +recordmcount-y += recordmcount.o
tools/objtool/Makefile +12 -1
@@ -31,6 +31,12 @@ OBJTOOL_IN := $(OBJTOOL)-in.o LIBELF_FLAGS := $(shell pkg-config libelf --cflags 2>/dev/null) LIBELF_LIBS := $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf) +RECORDMCOUNT := $(OUTPUT)recordmcount +RECORDMCOUNT_IN := $(RECORDMCOUNT)-in.o +ifeq ($(BUILD_C_RECORDMCOUNT),y) +all: $(RECORDMCOUNT) +endif + all: $(OBJTOOL) INCLUDES := -I$(srctree)/tools/include \
@@ -63,15 +69,20 @@ $(OBJTOOL_IN): fixdep FORCE @$(CONFIG_SHELL) ./sync-check.sh @$(MAKE) $(build)=objtool +$(RECORDMCOUNT_IN): fixdep FORCE + @$(MAKE) $(build)=recordmcount + $(OBJTOOL): $(LIBSUBCMD) $(OBJTOOL_IN) $(QUIET_LINK)$(CC) $(OBJTOOL_IN) $(LDFLAGS) -o $@ +$(RECORDMCOUNT): $(RECORDMCOUNT_IN) + $(QUIET_LINK)$(CC) $(RECORDMCOUNT_IN) $(KBUILD_HOSTLDFLAGS) -o $@ $(LIBSUBCMD): fixdep FORCE $(Q)$(MAKE) -C $(SUBCMD_SRCDIR) OUTPUT=$(LIBSUBCMD_OUTPUT) clean: - $(call QUIET_CLEAN, objtool) $(RM) $(OBJTOOL) + $(call QUIET_CLEAN, objtool) $(RM) $(OBJTOOL) $(RECORDMCOUNT) $(Q)find $(OUTPUT) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete $(Q)$(RM) $(OUTPUT)arch/x86/inat-tables.c $(OUTPUT)fixdep
tools/objtool/recordmcount.c renamed from scripts/recordmcount.c +0 -0
tools/objtool/recordmcount.h renamed from scripts/recordmcount.h +0 -0
tools/objtool/recordmcount.pl renamed from scripts/recordmcount.pl +0 -0
Signature · 1 line
2.20.1
RFC PATCH v4 3/32 objtool: recordmcount: Start using objtool's elf wrapper
In reply to Matt HelsleyUse struct elf to grab the file descriptor. We will later move these calls into other functions as we expand the lifetime of the struct elf so that it can be passed to objtool elf.[ch] functions.
This creates the libelf/objtool data structures and gives us two separate ways to walk the ELF file -- the libelf/objtool way and the old recordmcount wrapper way which avoids these extra data structures by using indices, offsets, and pointers into the mmapped ELF file.
Subsequent patches will convert from the old recordmcount accessors to the libelf/objtool accessors.
--- tools/objtool/recordmcount.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
tools/objtool/recordmcount.c +13 -5
@@ -33,6 +33,8 @@#include "objtool.h" +#include "elf.h" +#ifndef EM_AARCH64 #define EM_AARCH64 183#define R_AARCH64_NONE 0
@@ -57,6 +59,8 @@ static void *file_ptr; /* current file pointer location */static void *file_append;/* added to the end of the file */ static size_t file_append_size;/* how much is added to end of file */ +static struct elf *lf; +/* Per-file resource cleanup when multiple files. */ static void file_append_cleanup (void ) {
@@ -73,6 +77,9 @@ static void mmap_cleanup(void)else free (file_map); file_map =NULL ; +if (lf) +elf_close (lf); + lf =NULL ; }/* ulseek, uwrite, ...: Check return value for errors. */
@@ -170,11 +177,12 @@ static void *mmap_file(char const *fname) file_updated =0 ; sb.st_size =0 ; - fd_map =open (fname,O_RDONLY ); -if (fd_map <0 ) { + lf =elf_open_read (fname,O_RDONLY ); +if (!lf) {perror (fname);return NULL ; } + fd_map = lf->fd;if (fstat (fd_map, &sb) <0 ) {perror (fname); goto out;
@@ -194,14 +202,14 @@ static void *mmap_file(char const *fname) }if (read (fd_map, file_map, sb.st_size) != sb.st_size) {perror (fname); -free (file_map); - file_map =NULL ; +mmap_cleanup (); goto out; } }else mmap_failed =0 ; out: -close (fd_map); +elf_close (lf); + lf =NULL ; fd_map =-1 ; file_end = file_map + sb.st_size;
Signature · 1 line
2.20.1
RFC PATCH v4 9/32 objtool: mcount: Use symbol structs to find mcount relocations
In reply to Matt HelsleyRather than open coding symbol name lookups in get_sym_and_*() we rename the structure and use objtool's lookup function to get the symbol name for a relocation. We also change the name of the function to better reflect its purpose.
--- tools/objtool/elf.c | 2 +- tools/objtool/elf.h | 1 + tools/objtool/recordmcount.h | 54 ++++++++++-------------------------- 3 files changed, 17 insertions(+), 40 deletions(-)
tools/objtool/elf.c +1 -1
@@ -149,7 +149,7 @@ struct section *find_section_by_index(const struct elf *elf, unsigned int idx)return NULL ; } -static struct symbol *find_symbol_by_index (struct elf *elf,unsigned int idx) +struct symbol *find_symbol_by_index (const struct elf *elf,unsigned int idx) {struct symbol *sym;
tools/objtool/elf.h +1 -0
@@ -129,6 +129,7 @@ struct section *find_section_by_index(const struct elf *elf, unsigned int idx);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_by_index (const struct elf *elf,unsigned int idx);struct symbol *find_symbol_containing (const struct section *sec,unsigned long offset);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,
tools/objtool/recordmcount.h +15 -39
122 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 10/32 objtool: mcount: Walk relocation lists
In reply to Matt HelsleyRather than walk the section tables using the old recordmcount mapping of the ELF file, walk the section list provided by objtool's ELF code. This removes the last use of of the Elf_r_sym wrapper so we remove that too.
--- tools/objtool/recordmcount.c | 10 +--- tools/objtool/recordmcount.h | 103 +++++++++-------------------------- 2 files changed, 28 insertions(+), 85 deletions(-)
tools/objtool/recordmcount.c +2 -8
@@ -429,9 +429,9 @@ static const unsigned int missing_sym = (unsigned int)-1;#define RECORD_MCOUNT_64 #include "recordmcount.h" -static int arm_is_fake_mcount (Elf32_Rel const *rp) +static int arm_is_fake_mcount (struct reloc const *rp) { -switch (ELF32_R_TYPE (w (rp->r_info))) { +switch (rp->type) {case R_ARM_THM_CALL :case R_ARM_CALL :case R_ARM_PC24 :
@@ -462,11 +462,6 @@ union mips_r_info { } r_mips; }; -static uint64_t MIPS64_r_sym (Elf64_Rel const *rp) -{ -return w (((union mips_r_info ){ .r_info = rp->r_info }).r_mips.r_sym); -} -static void MIPS64_r_info (Elf64_Rel *const rp,unsigned sym,unsigned type) { rp->r_info = ((union mips_r_info){
@@ -605,7 +600,6 @@ static int do_file(char const *const fname) }if (w2 (ghdr->e_machine) ==EM_MIPS ) { reltype =R_MIPS_64 ; - Elf64_r_sym = MIPS64_r_sym; Elf64_r_info = MIPS64_r_info; is_fake_mcount64 = MIPS64_is_fake_mcount; }
tools/objtool/recordmcount.h +26 -77
240 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 11/32 objtool: mcount: Move get_mcountsym
In reply to Matt HelsleyNow that it's been stripped of using the old recordmcount ELF wrapper get_mcountsym() is ready to be promoted out of the double-included wrapper header.
--- tools/objtool/recordmcount.c | 16 ++++++++++++++++ tools/objtool/recordmcount.h | 19 ------------------- 2 files changed, 16 insertions(+), 19 deletions(-)
tools/objtool/recordmcount.c +16 -0
@@ -422,6 +422,22 @@ static int is_mcounted_section_name(char const *const txtname)strcmp (".cpuidle.text" , txtname) ==0 ; } +static unsigned get_mcountsym (struct reloc *reloc) +{ +struct symbol *sym = reloc->sym; +char const *symname = sym->name; +char const *mcount = gpfx =='_' ?"_mcount" :"mcount" ; +char const *fentry ="__fentry__" ; + +if (symname[0 ] =='.' ) + ++symname;/* ppc64 hack */ +if (strcmp (mcount, symname) ==0 || + (altmcount &&strcmp (altmcount, symname) ==0 ) || + (strcmp (fentry, symname) ==0 )) +return GELF_R_INFO (reloc->sym->idx, reloc->type); +return 0 ; +} +static const unsigned int missing_sym = (unsigned int )-1 ;/* 32 bit and 64 bit are very similar */
tools/objtool/recordmcount.h +0 -19
@@ -27,7 +27,6 @@#undef find_section_sym_index#undef has_rel_mcount#undef tot_relsize -#undef get_mcountsym#undef do_func#undef Elf_Addr#undef Elf_Ehdr
@@ -50,7 +49,6 @@# define has_rel_mcount has64_rel_mcount# define tot_relsize tot64_relsize# define do_func do64 -# define get_mcountsym get_mcountsym_64# define is_fake_mcount is_fake_mcount64# define fn_is_fake_mcount fn_is_fake_mcount64# define MIPS_is_fake_mcount MIPS64_is_fake_mcount
@@ -75,7 +73,6 @@# define has_rel_mcount has32_rel_mcount# define tot_relsize tot32_relsize# define do_func do32 -# define get_mcountsym get_mcountsym_32# define is_fake_mcount is_fake_mcount32# define fn_is_fake_mcount fn_is_fake_mcount32# define MIPS_is_fake_mcount MIPS32_is_fake_mcount
@@ -234,22 +231,6 @@ static int append_func(Elf_Ehdr *const ehdr,return elf_write (lf); } -static unsigned get_mcountsym (struct reloc *reloc) -{ -struct symbol *sym = reloc->sym; -char const *symname = sym->name; -char const *mcount = gpfx =='_' ?"_mcount" :"mcount" ; -char const *fentry ="__fentry__" ; - -if (symname[0 ] =='.' ) - ++symname;/* ppc64 hack */ -if (strcmp (mcount, symname) ==0 || - (altmcount &&strcmp (altmcount, symname) ==0 ) || - (strcmp (fentry, symname) ==0 )) -return GELF_R_INFO (reloc->sym->idx, reloc->type); -return 0 ; -} -/* * Look at the relocations in order to find the calls to mcount. * Accumulate the section offsets that are found, and their relocation info,
Signature · 1 line
2.20.1
RFC PATCH v4 14/32 objtool: mcount: Stop using ehdr in find_section_sym_index
In reply to Matt HelsleyWe can use the objtool GElf_Ehdr structure to access ehdr here. This makes the function completely independent of the old recordmcount ELF wrapper. The next step will be to promote it to the C file.
--- tools/objtool/recordmcount.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
tools/objtool/recordmcount.h +6 -7
@@ -188,7 +188,7 @@ static uint_t *sift_rel_mcount(uint_t *mlocp,Elf_Rel **const mrelpp, const struct section * const rels, unsigned const recsym_index, - uint_t const recval, + unsigned long const recval, unsigned const reltype) { uint_t *const mloc0 = mlocp;
@@ -282,9 +282,8 @@ static int nop_mcount(struct section * const rels,*/ static int find_section_sym_index (unsigned const txtndx,char const *const txtname, -uint_t *const recvalp, -unsigned int *sym_index, -Elf_Ehdr const *const ehdr) +unsigned long *const recvalp, +unsigned int *sym_index) {struct symbol *sym;struct section *txts =find_section_by_index (lf, txtndx);
@@ -298,7 +297,7 @@ static int find_section_sym_index(unsigned const txtndx,list_for_each_entry (sym, &txts->symbol_list, list) {if ((sym->bind ==STB_LOCAL ) || (sym->bind ==STB_GLOBAL )) {/* function symbols on ARM have quirks, avoid them */ -if (w2 (ehdr->e_machine) ==EM_ARM +if (lf->ehdr.e_machine ==EM_ARM && sym->type ==STT_FUNC )continue ;
@@ -387,12 +386,12 @@ static int do_func(Elf_Ehdr *const ehdr, txtname =has_rel_mcount (sec);if (txtname &&is_mcounted_section_name (txtname)) { +unsigned long recval =0 ;unsigned int recsym; -uint_t recval =0 ; symsec_sh_link = sec->sh.sh_link; result =find_section_sym_index (sec->sh.sh_info, - txtname, &recval, &recsym, ehdr); + txtname, &recval, &recsym);if (result) goto out;
Signature · 1 line
2.20.1
RFC PATCH v4 15/32 objtool: mcount: Move find_section_sym_index()
In reply to Matt HelsleyThis function is no longer dependent upon the old recordmcount ELF wrapper code -- it doesn't use the wrapper's Elf_* types nor does it call wrapped functions. Move it into the C file.
--- tools/objtool/recordmcount.c | 41 ++++++++++++++++++++++++++++++++ tools/objtool/recordmcount.h | 46 +----------------------------------- 2 files changed, 42 insertions(+), 45 deletions(-)
tools/objtool/recordmcount.c +41 -0
@@ -482,6 +482,47 @@ static int (*is_fake_mcount)(struct reloc const *reloc) = fn_is_fake_mcount;static const unsigned int missing_sym = (unsigned int )-1 ; +/* +* Find a symbol in the given section, to be used as the base for relocating +* the table of offsets of calls to mcount. A local or global symbol suffices, +* but avoid a Weak symbol because it may be overridden; the change in value +* would invalidate the relocations of the offsets of the calls to mcount. +* Often the found symbol will be the unnamed local symbol generated by +* GNU 'as' for the start of each section. For example: +* Num: Value Size Type Bind Vis Ndx Name +* 2: 00000000 0 SECTION LOCAL DEFAULT 1 +*/ +static int find_section_sym_index (unsigned const txtndx, +char const *const txtname, +unsigned long *const recvalp, +unsigned int *sym_index) +{ +struct symbol *sym; +struct section *txts =find_section_by_index (lf, txtndx); + +if (!txts) { +fprintf (stderr,"Cannot find section %u: %s.\n" , + txtndx, txtname); +return missing_sym; + } + +list_for_each_entry (sym, &txts->symbol_list, list) { +if ((sym->bind ==STB_LOCAL ) || (sym->bind ==STB_GLOBAL )) { +/* function symbols on ARM have quirks, avoid them */ +if (lf->ehdr.e_machine ==EM_ARM + && sym->type ==STT_FUNC ) +continue ; + + *recvalp = sym->sym.st_value; + *sym_index = sym->idx; +return 0 ; + } + } +fprintf (stderr,"Cannot find symbol for section %u: %s.\n" , + txtndx, txtname); +return missing_sym; +} +/* 32 bit and 64 bit are very similar */ #include "recordmcount.h" #define RECORD_MCOUNT_64
tools/objtool/recordmcount.h +1 -45
76 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 19/32 objtool: mcount: Move has_rel_mcount() and tot_relsize()
In reply to Matt HelsleyThe has_rel_mcount() and tot_relsize() helpers are no longer dependent on the ELF wrapper so we can move them.
--- tools/objtool/recordmcount.c | 25 +++++++++++++++++++++++++ tools/objtool/recordmcount.h | 33 --------------------------------- 2 files changed, 25 insertions(+), 33 deletions(-)
tools/objtool/recordmcount.c +25 -0
@@ -445,6 +445,31 @@ static int nop_mcount(struct section * const rels,return 0 ; } +static char const *has_rel_mcount (const struct section *const rels) +{ +const struct section *txts; +if (rels->sh.sh_type !=SHT_REL && rels->sh.sh_type !=SHT_RELA ) +return NULL ; + txts =find_section_by_index (lf, rels->sh.sh_info); +if ((txts->sh.sh_type !=SHT_PROGBITS ) || + !(txts->sh.sh_flags &SHF_EXECINSTR )) +return NULL ; +return txts->name; +} + +static unsigned tot_relsize (void ) +{ +const struct section *sec; +unsigned totrelsz =0 ; +char const *txtname; + +list_for_each_entry (sec, &lf->sections, list) { + txtname =has_rel_mcount (sec); +if (txtname &&is_mcounted_section_name (txtname)) + totrelsz += sec->sh.sh_size; + } +return totrelsz; +}/* 32 bit and 64 bit are very similar */ #include "recordmcount.h" #define RECORD_MCOUNT_64
tools/objtool/recordmcount.h +0 -33
@@ -20,8 +20,6 @@#undef append_func#undef mcount_adjust#undef sift_rel_mcount -#undef has_rel_mcount -#undef tot_relsize#undef do_func#undef Elf_Shdr#undef Elf_Rel
@@ -36,8 +34,6 @@#ifdef RECORD_MCOUNT_64 # define append_func append64# define sift_rel_mcount sift64_rel_mcount -# define has_rel_mcount has64_rel_mcount -# define tot_relsize tot64_relsize# define do_func do64# define mcount_adjust mcount_adjust_64# define Elf_Rel Elf64_Rel
@@ -51,8 +47,6 @@#else # define append_func append32# define sift_rel_mcount sift32_rel_mcount -# define has_rel_mcount has32_rel_mcount -# define tot_relsize tot32_relsize# define do_func do32# define mcount_adjust mcount_adjust_32# define Elf_Rel Elf32_Rel
@@ -168,33 +162,6 @@ static uint_t *sift_rel_mcount(uint_t *mlocp,return mlocp; } -static char const *has_rel_mcount (const struct section *const rels) -{ -const struct section *txts; -if (rels->sh.sh_type !=SHT_REL && rels->sh.sh_type !=SHT_RELA ) -return NULL ; - txts =find_section_by_index (lf, rels->sh.sh_info); -if ((txts->sh.sh_type !=SHT_PROGBITS ) || - !(txts->sh.sh_flags &SHF_EXECINSTR )) -return NULL ; -return txts->name; -} - - -static unsigned tot_relsize (void ) -{ -const struct section *sec; -unsigned totrelsz =0 ; -char const *txtname; - -list_for_each_entry (sec, &lf->sections, list) { - txtname =has_rel_mcount (sec); -if (txtname &&is_mcounted_section_name (txtname)) - totrelsz += sec->sh.sh_size; - } -return totrelsz; -} -/* Overall supervision for Elf32 ET_REL file. */ static int do_func (unsigned const reltype)
Signature · 1 line
2.20.1
RFC PATCH v4 25/32 objtool: mcount: Move mcount_adjust out of wrapper
In reply to Matt HelsleyThe mcount_adjust variable defines how many bytes to move back from the relocation address in order to be able to get to the start of the function call instruction(s) needed to turn it into a no-op. The values are very small and signed so we don't need to worry about changing the size of the variable's type inside the wrapper -- we can just use a regular int.
--- tools/objtool/recordmcount.c | 12 +++++++++--- tools/objtool/recordmcount.h | 5 ----- 2 files changed, 9 insertions(+), 8 deletions(-)
tools/objtool/recordmcount.c +9 -3
@@ -385,6 +385,12 @@ static unsigned tot_relsize(unsigned int *rel_entsize) }return totrelsz; } + +/* zero or a small negative offset added to get the start of the call +* instruction +*/ +static int mcount_adjust =0 ; +/* 32 bit and 64 bit are very similar */ #include "recordmcount.h" #define RECORD_MCOUNT_64
@@ -495,7 +501,7 @@ static int do_file(char const *const fname) rel_type_nop =R_386_NONE ; make_nop = make_nop_x86; ideal_nop = ideal_nop5_x86_32; - mcount_adjust_32 =-1 ; + mcount_adjust =-1 ; gpfx =0 ;break ;case EM_ARM :
@@ -524,7 +530,7 @@ static int do_file(char const *const fname) ideal_nop = ideal_nop5_x86_64; reltype =R_X86_64_64 ; rel_type_nop =R_X86_64_NONE ; - mcount_adjust_64 =-1 ; + mcount_adjust =-1 ; gpfx =0 ;break ; }/* end switch */
@@ -556,7 +562,7 @@ static int do_file(char const *const fname) }if (lf->ehdr.e_machine ==EM_S390 ) { reltype =R_390_64 ; - mcount_adjust_64 =-14 ; + mcount_adjust =-14 ; }if (lf->ehdr.e_machine ==EM_MIPS ) { reltype =R_MIPS_64 ;
tools/objtool/recordmcount.h +0 -5
@@ -18,7 +18,6 @@* Copyright 2010 Steven Rostedt <[email protected]>, Red Hat Inc. */ #undef append_func -#undef mcount_adjust#undef sift_rel_mcount#undef do_func#undef Elf_Shdr
@@ -35,7 +34,6 @@# define append_func append64# define sift_rel_mcount sift64_rel_mcount# define do_func do64 -# define mcount_adjust mcount_adjust_64# define Elf_Rel Elf64_Rel# define Elf_Rela Elf64_Rela# define ELF_R_INFO ELF64_R_INFO
@@ -48,7 +46,6 @@# define append_func append32# define sift_rel_mcount sift32_rel_mcount# define do_func do32 -# define mcount_adjust mcount_adjust_32# define Elf_Rel Elf32_Rel# define Elf_Rela Elf32_Rela# define ELF_R_INFO ELF32_R_INFO
@@ -65,8 +62,6 @@ static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type) }static void (*Elf_r_info)(Elf_Rel *const rp,unsigned sym,unsigned type) = fn_ELF_R_INFO; -static int mcount_adjust =0 ; -/* Append the new __mcount_loc and its relocations. */ static int append_func(uint_t const *const mloc0, uint_t const *const mlocp,
Signature · 1 line
2.20.1
RFC PATCH v4 24/32 objtool: mcount: Reduce usage of _size wrapper
In reply to Matt HelsleyUse a new loc_size parameter to append_func() rather than use the wrapper's _size macro directly.
--- tools/objtool/recordmcount.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
tools/objtool/recordmcount.h +5 -4
@@ -72,6 +72,7 @@ static int append_func(uint_t const *const mloc0,uint_t const *const mlocp, Elf_Rel const *const mrel0, Elf_Rel const *const mrelp, + unsigned int const loc_size, unsigned int const rel_entsize, unsigned int const symsec_sh_link) {
@@ -83,14 +84,14 @@ static int append_func(uint_t const *const mloc0,unsigned const old_shnum = lf->ehdr.e_shnum;/* add section: __mcount_loc */ - sec =elf_create_section (lf, mc_name + (sizeof (Elf_Rela) == rel_entsize) +strlen (".rel" ), _size, mlocp - mloc0); + sec =elf_create_section (lf, mc_name + (sizeof (Elf_Rela) == rel_entsize) +strlen (".rel" ), loc_size, mlocp - mloc0);if (!sec)return -1 ;// created sec->sh.sh_size = (void *)mlocp - (void *)mloc0; sec->sh.sh_link =0 ;/* TODO objtool uses this? */ sec->sh.sh_info =0 ;/* TODO objtool uses this? */ - sec->sh.sh_addralign = _size; + sec->sh.sh_addralign = loc_size;// created sec->sh.sh_entsize = _size; // assert sec->data->d_size == (void *)mlocp - (void *)mloc0
@@ -109,7 +110,7 @@ static int append_func(uint_t const *const mloc0, sec->sh.sh_flags =0 ; sec->sh.sh_link =find_section_by_name (lf,".symtab" )->idx; sec->sh.sh_info = old_shnum; - sec->sh.sh_addralign = _size; + sec->sh.sh_addralign = loc_size;// assert sec->data->d_size == (void *)mrelp - (void *)mrel0 memcpy (sec->data->d_buf, mrel0, sec->data->d_size);
@@ -231,7 +232,7 @@ static int do_func(unsigned const reltype) }if (!result && mloc0 != mlocp) result =append_func (mloc0, mlocp, mrel0, mrelp, - rel_entsize, symsec_sh_link); + _size, rel_entsize, symsec_sh_link); out:free (mrel0);free (mloc0);
Signature · 1 line
2.20.1
RFC PATCH v4 22/32 objtool: mcount: Use ELF header from objtool
In reply to Matt HelsleyThe ELF header is the very first structure in an ELF file. Rather than cast it from the file mapping we use the ELF header extracted via objtool's ELF code.
This is the last usage of the open-coded mapping of the ELF file which we will remove in a later step.
--- tools/objtool/recordmcount.c | 37 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 20 deletions(-)
tools/objtool/recordmcount.c +17 -20
@@ -523,21 +523,19 @@ static void MIPS64_r_info(Elf64_Rel *const rp, unsigned sym, unsigned type)static int do_file (char const *const fname) {unsigned int reltype =0 ; -Elf32_Ehdr *ehdr;int rc =-1 ; - ehdr =mmap_file (fname); -if (!ehdr) +if (!mmap_file (fname)) goto out; w = w4nat; w2 = w2nat; w8 = w8nat; -switch (ehdr->e_ident[EI_DATA ]) { +switch (lf->ehdr.e_ident[EI_DATA ]) {static unsigned int const endian =1 ;default :fprintf (stderr,"unrecognized ELF data encoding %d: %s\n" , - ehdr->e_ident[EI_DATA ], fname); + lf->ehdr.e_ident[EI_DATA ], fname); goto out;case ELFDATA2LSB :if (*(unsigned char const *)&endian !=1 ) {
@@ -566,18 +564,18 @@ static int do_file(char const *const fname) push_bl_mcount_thumb = push_bl_mcount_thumb_be;break ; }/* end switch */ -if (memcmp (ELFMAG , ehdr->e_ident,SELFMAG ) !=0 || -w2 (ehdr->e_type) !=ET_REL || - ehdr->e_ident[EI_VERSION ] !=EV_CURRENT ) { +if (memcmp (ELFMAG , lf->ehdr.e_ident,SELFMAG ) !=0 || + lf->ehdr.e_type !=ET_REL || + lf->ehdr.e_ident[EI_VERSION ] !=EV_CURRENT ) {fprintf (stderr,"unrecognized ET_REL file %s\n" , fname); goto out; } gpfx ='_' ; -switch (w2 (ehdr->e_machine)) { +switch (lf->ehdr.e_machine) {default :fprintf (stderr,"unrecognized e_machine %u %s\n" , -w2 (ehdr->e_machine), fname); + lf->ehdr.e_machine, fname); goto out;case EM_386 : reltype =R_386_32 ;
@@ -618,37 +616,36 @@ static int do_file(char const *const fname)break ; }/* end switch */ -switch (ehdr->e_ident[EI_CLASS ]) { +switch (lf->ehdr.e_ident[EI_CLASS ]) {default :fprintf (stderr,"unrecognized ELF class %d %s\n" , - ehdr->e_ident[EI_CLASS ], fname); + lf->ehdr.e_ident[EI_CLASS ], fname); goto out;case ELFCLASS32 : -if (w2 (ehdr->e_ehsize) !=sizeof (Elf32_Ehdr) - ||w2 (ehdr->e_shentsize) !=sizeof (Elf32_Shdr)) { +if (lf->ehdr.e_ehsize !=sizeof (Elf32_Ehdr) + || lf->ehdr.e_shentsize !=sizeof (Elf32_Shdr)) {fprintf (stderr,"unrecognized ET_REL file: %s\n" , fname); goto out; } -if (w2 (ehdr->e_machine) ==EM_MIPS ) { +if (lf->ehdr.e_machine ==EM_MIPS ) { reltype =R_MIPS_32 ; is_fake_mcount = MIPS_is_fake_mcount; } rc =do32 (reltype);break ;case ELFCLASS64 : { -Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr; -if (w2 (ghdr->e_ehsize) !=sizeof (Elf64_Ehdr) - ||w2 (ghdr->e_shentsize) !=sizeof (Elf64_Shdr)) { +if (lf->ehdr.e_ehsize !=sizeof (Elf64_Ehdr) + || lf->ehdr.e_shentsize !=sizeof (Elf64_Shdr)) {fprintf (stderr,"unrecognized ET_REL file: %s\n" , fname); goto out; } -if (w2 (ghdr->e_machine) ==EM_S390 ) { +if (lf->ehdr.e_machine ==EM_S390 ) { reltype =R_390_64 ; mcount_adjust_64 =-14 ; } -if (w2 (ghdr->e_machine) ==EM_MIPS ) { +if (lf->ehdr.e_machine ==EM_MIPS ) { reltype =R_MIPS_64 ; Elf64_r_info = MIPS64_r_info; is_fake_mcount = MIPS_is_fake_mcount;
Signature · 1 line
2.20.1
RFC PATCH v4 20/32 objtool: mcount: Move relocation entry size detection
In reply to Matt HelsleyMove where we detect the size of relocation entries we wish to use into the first loop over the sections. This will allow us to allocate the mcount location and relocation sections before the next loop that collects them.
--- tools/objtool/recordmcount.c | 8 +++++--- tools/objtool/recordmcount.h | 5 ++--- 2 files changed, 7 insertions(+), 6 deletions(-)
tools/objtool/recordmcount.c +5 -3
@@ -457,7 +457,7 @@ static char const *has_rel_mcount(const struct section * const rels)return txts->name; } -static unsigned tot_relsize (void ) +static unsigned tot_relsize (unsigned int *rel_entsize) {const struct section *sec;unsigned totrelsz =0 ;
@@ -465,8 +465,10 @@ static unsigned tot_relsize(void)list_for_each_entry (sec, &lf->sections, list) { txtname =has_rel_mcount (sec); -if (txtname &&is_mcounted_section_name (txtname)) - totrelsz += sec->sh.sh_size; +if (!(txtname &&is_mcounted_section_name (txtname))) +continue ; + totrelsz += sec->sh.sh_size; + *rel_entsize = sec->sh.sh_entsize; }return totrelsz; }
tools/objtool/recordmcount.h +2 -3
@@ -175,7 +175,7 @@ static int do_func(unsigned const reltype)uint_t * mloc0;uint_t * mlocp; -unsigned rel_entsize =0 ; +unsigned int rel_entsize =0 ;unsigned symsec_sh_link =0 ;struct section *sec;
@@ -185,7 +185,7 @@ static int do_func(unsigned const reltype)if (find_section_by_name (lf,"__mcount_loc" ) !=NULL )return 0 ; - totrelsz =tot_relsize (); + totrelsz =tot_relsize (&rel_entsize);if (totrelsz ==0 )return 0 ; mrel0 =umalloc (totrelsz);
@@ -215,7 +215,6 @@ static int do_func(unsigned const reltype)if (result) goto out; - rel_entsize = sec->sh.sh_entsize; mlocp =sift_rel_mcount (mlocp, (void *)mlocp - (void *)mloc0, &mrelp, sec, recsym, (uint_t )recval, reltype);
Signature · 1 line
2.20.1
RFC PATCH v4 16/32 objtool: mcount: Restrict using ehdr in append_func()
In reply to Matt HelsleyUse the ehdr parameter to append_func() to write the ELF file's header but use the objtool ELF header data as the basis for making changes. The makes it clearer when we can switch from using the old recordmcount wrapper to write the ELF file because ehdr will only be passed on to uwrite() calls and those will get replaced later.
--- tools/objtool/recordmcount.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/objtool/recordmcount.h +3 -3
@@ -99,8 +99,8 @@ static int append_func(Elf_Ehdr *const ehdr,char const *mc_name = (sizeof (Elf_Rela) == rel_entsize) ?".rela__mcount_loc" :".rel__mcount_loc" ; -unsigned const old_shnum =w2 (ehdr->e_shnum); -uint_t const old_shoff =_w (ehdr->e_shoff); +unsigned const old_shnum = lf->ehdr.e_shnum; +uint_t const old_shoff = lf->ehdr.e_shoff;uint_t const old_shstr_sh_size =_w (shstr->sh_size);uint_t const old_shstr_sh_offset =_w (shstr->sh_offset);uint_t t =1 +strlen (mc_name) +_w (shstr->sh_size);
@@ -167,7 +167,7 @@ static int append_func(Elf_Ehdr *const ehdr,return -1 ; ehdr->e_shoff =_w (new_e_shoff); - ehdr->e_shnum =w2 (2 +w2 (ehdr->e_shnum));/* {.rel,}__mcount_loc */ + ehdr->e_shnum =w2 (2 + lf->ehdr.e_shnum);/* {.rel,}__mcount_loc */ if (ulseek (0 ,SEEK_SET ) <0 )return -1 ;if (uwrite (ehdr,sizeof (*ehdr)) <0 )
Signature · 1 line
2.20.1
RFC PATCH v4 30/32 objtool: mcount: Remove wrapper double-include trick
In reply to Matt HelsleyWe no longer need to double-include the recordmcount.h wrapper All of the types and functions that rely on them have been converted to using objtool's ELF code. This moves the remaining function to recordmcount.c and changes the function name to something slightly more descriptive while dropping the wrapped naming.
--- tools/objtool/recordmcount.c | 122 ++++++++++++++++++++++++++++-- tools/objtool/recordmcount.h | 142 ----------------------------------- 2 files changed, 116 insertions(+), 148 deletions(-) delete mode 100644 tools/objtool/recordmcount.h
tools/objtool/recordmcount.c +116 -6
@@ -441,10 +441,120 @@ static void sift_rel_mcount(GElf_Addr **mlocpp, *mrelpp = is_rela ? (void *)mrelap : (void *)mrelp; } -/* 32 bit and 64 bit are very similar */ -#include "recordmcount.h" -#define RECORD_MCOUNT_64 -#include "recordmcount.h" +/* Overall supervision for Elf32 ET_REL file. */ +static int do_mcount (unsigned const reltype,size_t rela_size) +{ +/* Upper bound on space: assume all relevant relocs are for mcount. */ +unsigned totrelsz; + +void *mrel0; +void *mrelp; + +GElf_Addr *mloc0; +GElf_Addr *mlocp; +GElf_Sxword r_offset;/* Used in the added relocations */ + +unsigned int rel_entsize =0 ; + +struct section *sec, *mlocs, *mrels; +unsigned int const old_shnum = lf->ehdr.e_shnum; + +int result =-1 ; +char const *mc_name; +bool is_rela; + +if (find_section_by_name (lf,"__mcount_loc" ) !=NULL ) +return 0 ; + + totrelsz =tot_relsize (&rel_entsize); +if (totrelsz ==0 ) +return 0 ; + + mrel0 =malloc (totrelsz); + mrelp = mrel0; +if (!mrel0) +return -1 ; + +/* 2*sizeof(address) <= sizeof(Elf_Rel) */ + mloc0 =malloc (totrelsz>>1 ); + mlocp = mloc0; +if (!mloc0) { +free (mrel0); +return -1 ; + } + + is_rela = (rela_size == rel_entsize); + mc_name = is_rela + ?".rela__mcount_loc" + :".rel__mcount_loc" ; + +/* add section: __mcount_loc */ + mlocs =elf_create_section (lf, mc_name + (is_rela ?1 :0 ) +strlen (".rel" ),sizeof (*mloc0),0 ); +if (!mlocs) + goto out; + + mlocs->sh.sh_link =0 ; + mlocs->sh.sh_info =0 ; + mlocs->sh.sh_addralign =8 ; + mlocs->data->d_buf = mloc0; + mlocs->data->d_type =ELF_T_ADDR ;/* elf_xlatetof() conversion */ + +/* add section .rel[a]__mcount_loc */ + mrels =elf_create_section (lf, mc_name, rel_entsize,0 ); +if (!mrels) + goto out; +/* Like elf_create_rela_section() without the name bits */ + mrels->sh.sh_type = is_rela ?SHT_RELA :SHT_REL ; + mrels->sh.sh_flags =0 ; + mrels->sh.sh_link =find_section_by_name (lf,".symtab" )->idx; + mrels->sh.sh_info = old_shnum; + mrels->sh.sh_addralign =8 ; + mrels->data->d_buf = mrel0; + mrels->data->d_type = is_rela ?ELF_T_RELA :ELF_T_REL ;/* elf_xlatetof() conversion */ + +list_for_each_entry (sec, &lf->sections, list) { +char const *txtname; + + txtname =has_rel_mcount (sec); +if (txtname &&is_mcounted_section_name (txtname)) { +unsigned long recval =0 ; +unsigned int recsym; + +if (find_section_sym_index (sec->sh.sh_info, + txtname, &recval, &recsym)) + goto out; + +sift_rel_mcount (&mlocp, &r_offset, &mrelp, sec, + recsym, recval, reltype, is_rela); + }else if (txtname && (warn_on_notrace_sect || make_nop)) { +/* +* This section is ignored by ftrace, but still +* has mcount calls. Convert them to nops now. +*/ +if (nop_mcount (sec, txtname) <0 ) + goto out; + } + } + +if (mloc0 != mlocp) { +/* Update the section size and Elf_Data size */ + mlocs->sh.sh_size = (void *)mlocp - (void *)mloc0; + mlocs->len = mlocs->sh.sh_size; + mlocs->data->d_size = mlocs->len; + + mrels->sh.sh_size = mrelp - mrel0; + mrels->len = mrels->sh.sh_size; + mrels->data->d_size = mrels->len; + +/* overwrite the ELF file */ + result =elf_write (lf); + }else + result =0 ; +out: +free (mrel0); +free (mloc0); +return result; +}static int arm_is_fake_mcount (struct reloc const *rp) {
@@ -574,7 +684,7 @@ static int do_file(char const *const fname) is_fake_mcount = MIPS_is_fake_mcount; } loc_size =4 ; - rc =do32 (reltype,sizeof (Elf32_Rela)); + rc =do_mcount (reltype,sizeof (Elf32_Rela));break ;case ELFCLASS64 : { if (lf->ehdr.e_ehsize != sizeof(Elf64_Ehdr)
@@ -592,7 +702,7 @@ static int do_file(char const *const fname) is_fake_mcount = MIPS_is_fake_mcount; } loc_size =8 ; - rc =do64 (reltype,sizeof (Elf64_Rela)); + rc =do_mcount (reltype,sizeof (Elf64_Rela));break ; } }/* end switch */
tools/objtool/recordmcount.h deleted +0 -142
142 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 26/32 objtool: mcount: Pre-allocate new ELF sections
In reply to Matt HelsleyRather than allocating the ELF sections after collecting the mcount locations and building the relocation entries, create the empty sections beforehand. This has the benefit of removing the memcpy() and just using the resulting libgelf buffers directly.
--- tools/objtool/recordmcount.h | 122 +++++++++++++++-------------------- 1 file changed, 52 insertions(+), 70 deletions(-)
tools/objtool/recordmcount.h +52 -70
@@ -17,7 +17,6 @@* This conversion to macros was done by: * Copyright 2010 Steven Rostedt <[email protected]>, Red Hat Inc. */ -#undef append_func#undef sift_rel_mcount#undef do_func#undef Elf_Shdr
@@ -31,7 +30,6 @@#undef _size#ifdef RECORD_MCOUNT_64 -# define append_func append64# define sift_rel_mcount sift64_rel_mcount# define do_func do64# define Elf_Rel Elf64_Rel
@@ -43,7 +41,6 @@# define _w w8# define _size 8#else -# define append_func append32# define sift_rel_mcount sift32_rel_mcount# define do_func do32# define Elf_Rel Elf32_Rel
@@ -62,60 +59,6 @@ static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type) }static void (*Elf_r_info)(Elf_Rel *const rp,unsigned sym,unsigned type) = fn_ELF_R_INFO; -/* Append the new __mcount_loc and its relocations. */ -static int append_func (uint_t const *const mloc0, -uint_t const *const mlocp, -Elf_Rel const *const mrel0, -Elf_Rel const *const mrelp, -unsigned int const loc_size, -unsigned int const rel_entsize, -unsigned int const symsec_sh_link) -{ -/* Begin constructing output file */ -struct section *sec; -char const *mc_name = (sizeof (Elf_Rela) == rel_entsize) - ?".rela__mcount_loc" - :".rel__mcount_loc" ; -unsigned const old_shnum = lf->ehdr.e_shnum; - -/* add section: __mcount_loc */ - sec =elf_create_section (lf, mc_name + (sizeof (Elf_Rela) == rel_entsize) +strlen (".rel" ), loc_size, mlocp - mloc0); -if (!sec) -return -1 ; - -// created sec->sh.sh_size = (void *)mlocp - (void *)mloc0; - sec->sh.sh_link =0 ;/* TODO objtool uses this? */ - sec->sh.sh_info =0 ;/* TODO objtool uses this? */ - sec->sh.sh_addralign = loc_size; -// created sec->sh.sh_entsize = _size; - -// assert sec->data->d_size == (void *)mlocp - (void *)mloc0 -memcpy (sec->data->d_buf, mloc0, sec->data->d_size); -/* HACK link in Pre-assembled buffer ? -sec->data->d_buf = mloc0; -sec->data->d_size = sec->sh.sh_size;*/ - -/* add section .rel[a]__mcount_loc */ - sec =elf_create_section (lf, mc_name, rel_entsize, mrelp - mrel0); -if (!sec) -return -1 ; - sec->sh.sh_type = (sizeof (Elf_Rela) == rel_entsize) - ?SHT_RELA - :SHT_REL ; - sec->sh.sh_flags =0 ; - sec->sh.sh_link =find_section_by_name (lf,".symtab" )->idx; - sec->sh.sh_info = old_shnum; - sec->sh.sh_addralign = loc_size; - -// assert sec->data->d_size == (void *)mrelp - (void *)mrel0 -memcpy (sec->data->d_buf, mrel0, sec->data->d_size); -/* HACK link in Pre-assembled buffer ? -sec->data->d_buf = mrel0; -sec->data->d_size = sec->sh.sh_size;*/ - -return elf_write (lf); -} -/* * Look at the relocations in order to find the calls to mcount. * Accumulate the section offsets that are found, and their relocation info,
@@ -172,11 +115,13 @@ static int do_func(unsigned const reltype)uint_t * mlocp;unsigned int rel_entsize =0 ; -unsigned symsec_sh_link =0 ; -struct section *sec; +struct section *sec, *mlocs, *mrels; +unsigned int const old_shnum = lf->ehdr.e_shnum; -int result =0 ; +int result =-1 ; +char const *mc_name; +bool is_rela;if (find_section_by_name (lf,"__mcount_loc" ) !=NULL )return 0 ;
@@ -184,6 +129,7 @@ static int do_func(unsigned const reltype) totrelsz =tot_relsize (&rel_entsize);if (totrelsz ==0 )return 0 ; + mrel0 =malloc (totrelsz); mrelp = mrel0;if (!mrel0)
@@ -197,6 +143,32 @@ static int do_func(unsigned const reltype)return -1 ; } + is_rela = (sizeof (Elf_Rela) == rel_entsize); + mc_name = is_rela + ?".rela__mcount_loc" + :".rel__mcount_loc" ; + +/* add section: __mcount_loc */ + mlocs =elf_create_section (lf, mc_name + (is_rela ?1 :0 ) +strlen (".rel" ), _size,0 ); +if (!mlocs) + goto out; + + mlocs->sh.sh_link =0 ; + mlocs->sh.sh_info =0 ; + mlocs->sh.sh_addralign = _size; + +/* add section .rel[a]__mcount_loc */ + mrels =elf_create_section (lf, mc_name, rel_entsize,0 ); +if (!mrels) + goto out; + mrels->sh.sh_type = is_rela + ?SHT_RELA + :SHT_REL ; + mrels->sh.sh_flags =0 ; + mrels->sh.sh_link =find_section_by_name (lf,".symtab" )->idx; + mrels->sh.sh_info = old_shnum; + mrels->sh.sh_addralign = _size; +list_for_each_entry (sec, &lf->sections, list) {char const *txtname;
@@ -205,10 +177,8 @@ static int do_func(unsigned const reltype)unsigned long recval =0 ;unsigned int recsym; - symsec_sh_link = sec->sh.sh_link; - result =find_section_sym_index (sec->sh.sh_info, - txtname, &recval, &recsym); -if (result) +if (find_section_sym_index (sec->sh.sh_info, + txtname, &recval, &recsym)) goto out; mlocp = sift_rel_mcount(mlocp,
@@ -219,15 +189,27 @@ static int do_func(unsigned const reltype)* This section is ignored by ftrace, but still * has mcount calls. Convert them to nops now. */ -if (nop_mcount (sec, txtname) <0 ) { - result =-1 ; +if (nop_mcount (sec, txtname) <0 ) goto out; - } } } -if (!result && mloc0 != mlocp) - result =append_func (mloc0, mlocp, mrel0, mrelp, - _size, rel_entsize, symsec_sh_link); + +if (mloc0 != mlocp) { +/* Update the section sizes */ + mlocs->sh.sh_size = (void *)mlocp - (void *)mloc0; + mlocs->len = mlocs->sh.sh_size; + mlocs->data->d_size = mlocs->len; + mlocs->data->d_buf = mloc0; + + mrels->sh.sh_size = (void *)mrelp - (void *)mrel0; + mrels->len = mrels->sh.sh_size; + mrels->data->d_size = mrels->len; + mrels->data->d_buf = mrel0; + +/* overwrite the ELF file */ + result =elf_write (lf); + }else + result =0 ; out:free (mrel0);free (mloc0);
Signature · 1 line
2.20.1
RFC PATCH v4 21/32 objtool: mcount: Only keep ELF file size
In reply to Matt HelsleySince we're no longer writing to the ELF file mapping and we're not appending to it we don't need to keep more information from the stat structure. At the same time we can give the smaller global variable a better name.
--- tools/objtool/recordmcount.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
tools/objtool/recordmcount.c +6 -3
@@ -48,17 +48,17 @@static int fd_map;/* File descriptor for file being modified. */ static int mmap_failed;/* Boolean flag. */ static char gpfx;/* prefix for global symbol name (sometimes '_') */ -static struct stat sb;/* Remember .st_size, etc. */ static const char *altmcount;/* alternate mcount symbol name */ extern int warn_on_notrace_sect;/* warn when section has mcount not being recorded */ static void *file_map;/* pointer of the mapped file */ +static size_t file_map_size;/* original ELF file size */ static struct elf *lf;static void mmap_cleanup (void ) {if (!mmap_failed) -munmap (file_map, sb.st_size); +munmap (file_map, file_map_size);else free (file_map); file_map =NULL ;
@@ -93,11 +93,13 @@ static void * umalloc(size_t size)*/ static void *mmap_file (char const *fname) { +struct stat sb; +/* Avoid problems if early cleanup() */ fd_map =-1 ; mmap_failed =1 ; file_map =NULL ; - sb.st_size =0 ; + file_map_size =0 ; lf =elf_open_read (fname,O_RDWR );if (!lf) {
@@ -129,6 +131,7 @@ static void *mmap_file(char const *fname) } }else mmap_failed =0 ; + file_map_size = sb.st_size; out: fd_map =-1 ;
Signature · 1 line
2.20.1
RFC PATCH v4 23/32 objtool: mcount: Remove unused file mapping
In reply to Matt HelsleyThe ELF data is now accessed completely through objtool's ELF code. We can remove the mapping of the original ELF file and propagate elf_open_read(), elf_close(), and malloc() up in place of mmap_file(), mmap_cleanup(), and umalloc() respectively. This also eliminates the last use of the umalloc() wrapper, reduces the number of global variables, and limits the use of globals to:
The struct elf for the file we're working on. This saves passing it to nearly every function as a parameter.
Variables set depending on the ELF file endian, wordsize, and arch so that the appropriate relocation structures, offset sizes, architecture quirks, and nop encodings will be used.
One command-line option
Note that we're still using the recordmcount wrapper to change variable sizes and structure definitions we use to build the mcount relocation data and call instruction offsets.
--- tools/objtool/recordmcount.c | 99 +++--------------------------------- tools/objtool/recordmcount.h | 4 +- 2 files changed, 9 insertions(+), 94 deletions(-)
tools/objtool/recordmcount.c +7 -92
@@ -41,104 +41,14 @@#define R_AARCH64_ABS64 257#endif -#define R_ARM_PC24 1#define R_ARM_THM_CALL 10 -#define R_ARM_CALL 28 -static int fd_map;/* File descriptor for file being modified. */ -static int mmap_failed;/* Boolean flag. */ static char gpfx;/* prefix for global symbol name (sometimes '_') */ static const char *altmcount;/* alternate mcount symbol name */ extern int warn_on_notrace_sect;/* warn when section has mcount not being recorded */ -static void *file_map;/* pointer of the mapped file */ -static size_t file_map_size;/* original ELF file size */ static struct elf *lf; -static void mmap_cleanup (void ) -{ -if (!mmap_failed) -munmap (file_map, file_map_size); -else -free (file_map); - file_map =NULL ; -if (lf) -elf_close (lf); - lf =NULL ; -} - -static void *umalloc (size_t size) -{ -void *const addr =malloc (size); -if (addr ==0 ) { -fprintf (stderr,"malloc failed: %zu bytes\n" , size); -mmap_cleanup (); -return NULL ; - } -return addr; -} - -/* -* Get the whole file as a programming convenience in order to avoid -* malloc+lseek+read+free of many pieces. If successful, then mmap -* avoids copying unused pieces; else just read the whole file. -* Open for both read and write; new info will be appended to the file. -* Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr -* do not propagate to the file until an explicit overwrite at the last. -* This preserves most aspects of consistency (all except .st_size) -* for simultaneous readers of the file while we are appending to it. -* However, multiple writers still are bad. We choose not to use -* locking because it is expensive and the use case of kernel build -* makes multiple writers unlikely. -*/ -static void *mmap_file (char const *fname) -{ -struct stat sb; - -/* Avoid problems if early cleanup() */ - fd_map =-1 ; - mmap_failed =1 ; - file_map =NULL ; - file_map_size =0 ; - - lf =elf_open_read (fname,O_RDWR ); -if (!lf) { -perror (fname); -return NULL ; - } - fd_map = lf->fd; -if (fstat (fd_map, &sb) <0 ) { -perror (fname); - goto out; - } -if (!S_ISREG (sb.st_mode)) { -fprintf (stderr,"not a regular file: %s\n" , fname); - goto out; - } - file_map =mmap (0 , sb.st_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , - fd_map,0 ); -if (file_map ==MAP_FAILED ) { - mmap_failed =1 ; - file_map =umalloc (sb.st_size); -if (!file_map) { -perror (fname); - goto out; - } -if (read (fd_map, file_map, sb.st_size) != sb.st_size) { -perror (fname); -mmap_cleanup (); - goto out; - } - }else - mmap_failed =0 ; - file_map_size = sb.st_size; -out: - fd_map =-1 ; - -return file_map; -} - -static unsigned char ideal_nop5_x86_64[5 ] = {0x0f ,0x1f ,0x44 ,0x00 ,0x00 };static unsigned char ideal_nop5_x86_32[5 ] = {0x3e ,0x8d ,0x74 ,0x26 ,0x00 };static unsigned char *ideal_nop;
@@ -525,8 +435,11 @@ static int do_file(char const *const fname)unsigned int reltype =0 ;int rc =-1 ; -if (!mmap_file (fname)) + lf =elf_open_read (fname,O_RDWR ); +if (!lf) { +perror (fname); goto out; + } w = w4nat; w2 = w2nat;
@@ -656,7 +569,9 @@ static int do_file(char const *const fname) }/* end switch */ out: -mmap_cleanup (); +if (lf) +elf_close (lf); + lf =NULL ;return rc; }
tools/objtool/recordmcount.h +2 -2
@@ -188,13 +188,13 @@ static int do_func(unsigned const reltype) totrelsz =tot_relsize (&rel_entsize);if (totrelsz ==0 )return 0 ; - mrel0 =umalloc (totrelsz); + mrel0 =malloc (totrelsz); mrelp = mrel0;if (!mrel0)return -1 ;/* 2*sizeof(address) <= sizeof(Elf_Rel) */ - mloc0 =umalloc (totrelsz>>1 ); + mloc0 =malloc (totrelsz>>1 ); mlocp = mloc0;if (!mloc0) {free (mrel0);
Signature · 1 line
2.20.1
RFC PATCH v4 18/32 objtool: mcount: Move nop_mcount()
In reply to Matt HelsleyThe nop_mcount() function overwrites mcount calls that should be ignored with no-ops. This operation varies by architecture and wordsize so we retain the function pointers used to implement the fundamental operation while nop_mcount() itself is responsible for walking the relocations, determining if they should be turned into no-ops, then calling the arch-specific code. Since none of these use the recordmcount ELF wrappers anymore we can move it out of the wrapper.
--- tools/objtool/recordmcount.c | 47 +++++++++++++++++++++++++++++++++ tools/objtool/recordmcount.h | 50 ------------------------------------ 2 files changed, 47 insertions(+), 50 deletions(-)
tools/objtool/recordmcount.c +47 -0
@@ -398,6 +398,53 @@ static int find_section_sym_index(unsigned const txtndx,return missing_sym; } +/* +* Read the relocation table again, but this time its called on sections +* that are not going to be traced. The mcount calls here will be converted +* into nops. +*/ +static int nop_mcount (struct section *const rels, +const char *const txtname) +{ +struct reloc *reloc; +struct section *txts =find_section_by_index (lf, rels->sh.sh_info); +unsigned mcountsym =0 ; +int once =0 ; + +list_for_each_entry (reloc, &rels->reloc_list, list) { +int ret =-1 ; + +if (!mcountsym) + mcountsym =get_mcountsym (reloc); + +if (mcountsym ==GELF_R_INFO (reloc->sym->idx, reloc->type) && !is_fake_mcount (reloc)) { +if (make_nop) { + ret =make_nop (txts, reloc->offset); +if (ret <0 ) +return -1 ; + } +if (warn_on_notrace_sect && !once) { +printf ("Section %s has mcount callers being ignored\n" , + txtname); + once =1 ; +/* just warn? */ +if (!make_nop) +return 0 ; + } + } + +/* +* If we successfully removed the mcount, mark the relocation +* as a nop (don't do anything with it). +*/ +if (!ret) { + reloc->type = rel_type_nop; + rels->changed = true; + } + } +return 0 ; +} +/* 32 bit and 64 bit are very similar */ #include "recordmcount.h" #define RECORD_MCOUNT_64
tools/objtool/recordmcount.h +0 -50
74 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 17/32 objtool: mcount: Use objtool ELF to write
In reply to Matt HelsleyRather than modify the pseudo-mapping of the ELF file directly, which is the recordmcount way of operating, use the objtool section list and generic ELF functions to modify the ELF file.
This eliminates a bunch of code -- the ulseek() and uwrite() functions -- because it's used to patch the ELF data. Instead we rely on objtool's ELF code to handle updating the ELF file. This means a bunch of the odd bits in append_func() also go away since they did things like update the ELF header, add to the section table, and append the new section names to the string table -- all handled by objtool's ELF code.
One unusual part, with respect to objtool is the way we handle writing nops. Objtool is not designed to modify the an ELF text section directly (or at least I could not find and example to base this work on). So we break layering to access the "data" of the text section via the section's "data buffer". This is still cleaner -- we can now pass in the section struct and offset as separate parameters.
Note that this patch does not move the associated parts out of the wrapper file. We postpone that cleanup for later so that it's easier to see the changes to the functions rather than obscuring them with the move.
--- tools/objtool/recordmcount.c | 161 ++++------------------------------- tools/objtool/recordmcount.h | 132 +++++++++------------------- 2 files changed, 55 insertions(+), 238 deletions(-)
tools/objtool/recordmcount.c +16 -145
@@ -52,24 +52,9 @@ static struct stat sb; /* Remember .st_size, etc. */static const char *altmcount;/* alternate mcount symbol name */ extern int warn_on_notrace_sect;/* warn when section has mcount not being recorded */ static void *file_map;/* pointer of the mapped file */ -static void *file_end;/* pointer to the end of the mapped file */ -static int file_updated;/* flag to state file was changed */ -static void *file_ptr;/* current file pointer location */ - -static void *file_append;/* added to the end of the file */ -static size_t file_append_size;/* how much is added to end of file */ static struct elf *lf; -/* Per-file resource cleanup when multiple files. */ -static void file_append_cleanup (void ) -{ -free (file_append); - file_append =NULL ; - file_append_size =0 ; - file_updated =0 ; -} -static void mmap_cleanup (void ) {if (!mmap_failed)
@@ -82,72 +67,11 @@ static void mmap_cleanup(void) lf =NULL ; } -/* ulseek, uwrite, ...: Check return value for errors. */ - -static off_t ulseek (off_t const offset,int const whence) -{ -switch (whence) { -case SEEK_SET : - file_ptr = file_map + offset; -break ; -case SEEK_CUR : - file_ptr += offset; -break ; -case SEEK_END : - file_ptr = file_map + (sb.st_size - offset); -break ; - } -if (file_ptr < file_map) { -fprintf (stderr,"lseek: seek before file\n" ); -return -1 ; - } -return file_ptr - file_map; -} - -static ssize_t uwrite (void const *const buf,size_t const count) -{ -size_t cnt = count; -off_t idx =0 ; - - file_updated =1 ; - -if (file_ptr + count >= file_end) { -off_t aoffset = (file_ptr + count) - file_end; - -if (aoffset > file_append_size) { - file_append =realloc (file_append, aoffset); - file_append_size = aoffset; - } -if (!file_append) { -perror ("write" ); -file_append_cleanup (); -mmap_cleanup (); -return -1 ; - } -if (file_ptr < file_end) { - cnt = file_end - file_ptr; - }else { - cnt =0 ; - idx = aoffset - count; - } - } - -if (cnt) -memcpy (file_ptr, buf, cnt); - -if (cnt < count) -memcpy (file_append + idx, buf + cnt, count - cnt); - - file_ptr += count; -return count; -} -static void *umalloc (size_t size) {void *const addr =malloc (size);if (addr ==0 ) {fprintf (stderr,"malloc failed: %zu bytes\n" , size); -file_append_cleanup ();mmap_cleanup ();return NULL ; }
@@ -173,8 +97,6 @@ static void *mmap_file(char const *fname) fd_map =-1 ; mmap_failed =1 ; file_map =NULL ; - file_ptr =NULL ; - file_updated =0 ; sb.st_size =0 ; lf =elf_open_read (fname,O_RDWR );
@@ -210,8 +132,6 @@ static void *mmap_file(char const *fname) out: fd_map =-1 ; - file_end = file_map + sb.st_size; -return file_map; }
@@ -222,12 +142,16 @@ static unsigned char *ideal_nop;static char rel_type_nop; -static int (*make_nop)(void *map,size_t const offset); +static int (*make_nop)(struct section *,size_t const offset); -static int make_nop_x86 (void *map,size_t const offset) +static int make_nop_x86 (struct section *txts,size_t const offset) {uint32_t *ptr;unsigned char *op; +void *map = txts->data->d_buf; + +if (offset <1 ) +return -1 ;/* Confirm we have 0xe8 0x0 0x0 0x0 0x0 */ ptr = map + offset;
@@ -239,10 +163,7 @@ static int make_nop_x86(void *map, size_t const offset)return -1 ;/* convert to nop */ -if (ulseek (offset -1 ,SEEK_SET ) <0 ) -return -1 ; -if (uwrite (ideal_nop,5 ) <0 ) -return -1 ; +memcpy (op, ideal_nop,5 );return 0 ; }
@@ -266,12 +187,13 @@ static unsigned char push_bl_mcount_thumb_le[6] = { 0x00, 0xb5, 0xff, 0xf7, 0xfestatic unsigned char push_bl_mcount_thumb_be[6 ] = {0xb5 ,0x00 ,0xf7 ,0xff ,0xff ,0xfe };/* push {lr}, bl */ static unsigned char *push_bl_mcount_thumb; -static int make_nop_arm (void *map,size_t const offset) +static int make_nop_arm (struct section *txts,size_t const offset) {char *ptr;int cnt =1 ;int nop_size;size_t off = offset; +void *map = txts->data->d_buf; ptr = map + offset;if (memcmp (ptr, bl_mcount_arm,4 ) ==0 ) {
@@ -290,21 +212,19 @@ static int make_nop_arm(void *map, size_t const offset)return -1 ;/* Convert to nop */ -if (ulseek (off,SEEK_SET ) <0 ) -return -1 ; -do { -if (uwrite (ideal_nop, nop_size) <0 ) -return -1 ; +memcpy (map + off, ideal_nop, nop_size); + off += nop_size; }while (--cnt >0 );return 0 ; }static unsigned char ideal_nop4_arm64[4 ] = {0x1f ,0x20 ,0x03 ,0xd5 }; -static int make_nop_arm64 (void *map,size_t const offset) +static int make_nop_arm64 (struct section *txts,size_t const offset) {uint32_t *ptr; +void *map = txts->data->d_buf; ptr = map + offset;/* bl <_mcount> is 0x94000000 before relocation */
@@ -312,52 +232,7 @@ static int make_nop_arm64(void *map, size_t const offset)return -1 ;/* Convert to nop */ -if (ulseek (offset,SEEK_SET ) <0 ) -return -1 ; -if (uwrite (ideal_nop,4 ) <0 ) -return -1 ; -return 0 ; -} - -static int write_file (const char *fname) -{ -char tmp_file[strlen (fname) +4 ]; -size_t n; - -if (!file_updated) -return 0 ; - -sprintf (tmp_file,"%s.rc" , fname); - -/* -* After reading the entire file into memory, delete it -* and write it back, to prevent weird side effects of modifying -* an object file in place. -*/ - fd_map =open (tmp_file,O_WRONLY |O_TRUNC |O_CREAT , sb.st_mode); -if (fd_map <0 ) { -perror (fname); -return -1 ; - } - n =write (fd_map, file_map, sb.st_size); -if (n != sb.st_size) { -perror ("write" ); -close (fd_map); -return -1 ; - } -if (file_append_size) { - n =write (fd_map, file_append, file_append_size); -if (n != file_append_size) { -perror ("write" ); -close (fd_map); -return -1 ; - } - } -close (fd_map); -if (rename (tmp_file, fname) <0 ) { -perror (fname); -return -1 ; - } +memcpy (map + offset, ideal_nop,4 );return 0 ; }
@@ -682,8 +557,7 @@ static int do_file(char const *const fname) reltype =R_MIPS_32 ; is_fake_mcount = MIPS_is_fake_mcount; } -if (do32 (ehdr, reltype) <0 ) - goto out; + rc =do32 (reltype);break ;case ELFCLASS64 : {Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
@@ -702,15 +576,12 @@ static int do_file(char const *const fname) Elf64_r_info = MIPS64_r_info; is_fake_mcount = MIPS_is_fake_mcount; } -if (do64 (ghdr, reltype) <0 ) - goto out; + rc =do64 (reltype);break ; } }/* end switch */ - rc =write_file (fname); out: -file_append_cleanup ();mmap_cleanup ();return rc; }
tools/objtool/recordmcount.h +39 -93
223 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 8/32 objtool: mcount: Walk objtool Elf structs in find_secsym_ndx
In reply to Matt HelsleyRather than using indices into raw ELF32/64 tables mapped for the wrapper, use the objtool functions to find the a suitable symbol in the given text section.
Also take advantage of this commit to rename the function to something that reads more easily.
--- tools/objtool/recordmcount.c | 2 ++ tools/objtool/recordmcount.h | 42 ++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 23 deletions(-)
tools/objtool/recordmcount.c +2 -0
@@ -422,6 +422,8 @@ static int is_mcounted_section_name(char const *const txtname)strcmp (".cpuidle.text" , txtname) ==0 ; } +static const unsigned int missing_sym = (unsigned int )-1 ; +/* 32 bit and 64 bit are very similar */ #include "recordmcount.h" #define RECORD_MCOUNT_64
tools/objtool/recordmcount.h +19 -23
88 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 12/32 objtool: mcount: Replace MIPS offset types
In reply to Matt HelsleyReplace MIPS is_fake_mcount code using Elf_Addr with unsigned long for the offsets. This is consistent with the way that objtool more generally treats offsets and removes the last use of the Elf_Addr wrapper.
--- tools/objtool/recordmcount.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
tools/objtool/recordmcount.h +3 -6
@@ -28,7 +28,6 @@#undef has_rel_mcount#undef tot_relsize#undef do_func -#undef Elf_Addr#undef Elf_Ehdr#undef Elf_Shdr#undef Elf_Rel
@@ -53,7 +52,6 @@# define fn_is_fake_mcount fn_is_fake_mcount64# define MIPS_is_fake_mcount MIPS64_is_fake_mcount# define mcount_adjust mcount_adjust_64 -# define Elf_Addr Elf64_Addr# define Elf_Ehdr Elf64_Ehdr# define Elf_Shdr Elf64_Shdr# define Elf_Rel Elf64_Rel
@@ -77,7 +75,6 @@# define fn_is_fake_mcount fn_is_fake_mcount32# define MIPS_is_fake_mcount MIPS32_is_fake_mcount# define mcount_adjust mcount_adjust_32 -# define Elf_Addr Elf32_Addr# define Elf_Ehdr Elf32_Ehdr# define Elf_Shdr Elf32_Shdr# define Elf_Rel Elf32_Rel
@@ -129,11 +126,11 @@ static int mcount_adjust = 0;static int MIPS_is_fake_mcount (struct reloc const *reloc) { -static Elf_Addr old_r_offset = ~(Elf_Addr )0 ; -Elf_Addr current_r_offset = reloc->offset; +static unsigned long old_r_offset = ~0UL ; +unsigned long current_r_offset = reloc->offset;int is_fake; - is_fake = (old_r_offset != ~(Elf_Addr )0 ) && + is_fake = (old_r_offset != ~0UL ) && (current_r_offset - old_r_offset ==MIPS_FAKEMCOUNT_OFFSET ); old_r_offset = current_r_offset;
Signature · 1 line
2.20.1
RFC PATCH v4 13/32 objtool: mcount: Move is_fake_mcount()
In reply to Matt HelsleyPromote the now-bit-independent is_fake_mcount() out of the old recordmcount ELF wrapper.
--- tools/objtool/recordmcount.c | 48 +++++++++++++++++++++++++++++++--- tools/objtool/recordmcount.h | 50 ------------------------------------ 2 files changed, 45 insertions(+), 53 deletions(-)
tools/objtool/recordmcount.c +45 -3
@@ -438,6 +438,48 @@ static unsigned get_mcountsym(struct reloc *reloc)return 0 ; } +/* +* MIPS mcount long call has 2 _mcount symbols, only the position of the 1st +* _mcount symbol is needed for dynamic function tracer, with it, to disable +* tracing(ftrace_make_nop), the instruction in the position is replaced with +* the "b label" instruction, to enable tracing(ftrace_make_call), replace the +* instruction back. So, here, we set the 2nd one as fake and filter it. +* +* c: 3c030000 lui v1,0x0 <--> b label +* c: R_MIPS_HI16 _mcount +* c: R_MIPS_NONE *ABS* +* c: R_MIPS_NONE *ABS* +* 10: 64630000 daddiu v1,v1,0 +* 10: R_MIPS_LO16 _mcount +* 10: R_MIPS_NONE *ABS* +* 10: R_MIPS_NONE *ABS* +* 14: 03e0082d move at,ra +* 18: 0060f809 jalr v1 +* label: +*/ +#define MIPS_FAKEMCOUNT_OFFSET 4 + +static int MIPS_is_fake_mcount (struct reloc const *reloc) +{ +static unsigned long old_r_offset = ~0UL ; +unsigned long current_r_offset = reloc->offset; +int is_fake; + + is_fake = (old_r_offset != ~0UL ) && + (current_r_offset - old_r_offset ==MIPS_FAKEMCOUNT_OFFSET ); + old_r_offset = current_r_offset; + +return is_fake; +} + +/* Functions and pointers that do_file() may override for specific e_machine. */ +static int fn_is_fake_mcount (struct reloc const *reloc) +{ +return 0 ; +} + +static int (*is_fake_mcount)(struct reloc const *reloc) = fn_is_fake_mcount; +static const unsigned int missing_sym = (unsigned int )-1 ;/* 32 bit and 64 bit are very similar */
@@ -557,7 +599,7 @@ static int do_file(char const *const fname) altmcount ="__gnu_mcount_nc" ; make_nop = make_nop_arm; rel_type_nop =R_ARM_NONE ; - is_fake_mcount32 = arm_is_fake_mcount; + is_fake_mcount = arm_is_fake_mcount; gpfx =0 ;break ;case EM_AARCH64 :
@@ -597,7 +639,7 @@ static int do_file(char const *const fname) }if (w2 (ehdr->e_machine) ==EM_MIPS ) { reltype =R_MIPS_32 ; - is_fake_mcount32 = MIPS32_is_fake_mcount; + is_fake_mcount = MIPS_is_fake_mcount; }if (do32 (ehdr, reltype) <0 ) goto out;
@@ -617,7 +659,7 @@ static int do_file(char const *const fname)if (w2 (ghdr->e_machine) ==EM_MIPS ) { reltype =R_MIPS_64 ; Elf64_r_info = MIPS64_r_info; - is_fake_mcount64 = MIPS64_is_fake_mcount; + is_fake_mcount = MIPS_is_fake_mcount; }if (do64 (ghdr, reltype) <0 ) goto out;
tools/objtool/recordmcount.h +0 -50
80 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 7/32 objtool: mcount: Use libelf for section header names
In reply to Matt HelsleyRather than passing in the string table contents as a parameter, pass in the section index of the string table and rely on libelf string table accessor functions to look up section names.
Note that modifying the string table with libelf will come later so append_func() is unchanged.
--- tools/objtool/elf.c | 3 +- tools/objtool/elf.h | 1 + tools/objtool/recordmcount.c | 2 +- tools/objtool/recordmcount.h | 89 ++++++++++++++---------------------- 4 files changed, 37 insertions(+), 58 deletions(-)
tools/objtool/elf.c +1 -2
@@ -138,8 +138,7 @@ struct section *find_section_by_name(const struct elf *elf, const char *name)return NULL ; } -static struct section *find_section_by_index (struct elf *elf, -unsigned int idx) +struct section *find_section_by_index (const struct elf *elf,unsigned int idx) {struct section *sec;
tools/objtool/elf.h +1 -0
@@ -125,6 +125,7 @@ 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 section *find_section_by_index (const struct elf *elf,unsigned int idx);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);
tools/objtool/recordmcount.c +1 -1
@@ -177,7 +177,7 @@ static void *mmap_file(char const *fname) file_updated =0 ; sb.st_size =0 ; - lf =elf_open_read (fname,O_RDONLY ); + lf =elf_open_read (fname,O_RDWR );if (!lf) {perror (fname);return NULL ;
tools/objtool/recordmcount.h +34 -55
228 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 6/32 objtool: mcount: Remove unused fname parameter
In reply to Matt HelsleyThe name of the object file being processed is unused in the wrapper's leaf functions so we no longer need to pass it as a parameter.
--- tools/objtool/recordmcount.c | 4 ++-- tools/objtool/recordmcount.h | 19 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-)
tools/objtool/recordmcount.c +2 -2
@@ -586,7 +586,7 @@ static int do_file(char const *const fname) reltype =R_MIPS_32 ; is_fake_mcount32 = MIPS32_is_fake_mcount; } -if (do32 (ehdr, fname, reltype) <0 ) +if (do32 (ehdr, reltype) <0 ) goto out;break ;case ELFCLASS64 : {
@@ -607,7 +607,7 @@ static int do_file(char const *const fname) Elf64_r_info = MIPS64_r_info; is_fake_mcount64 = MIPS64_is_fake_mcount; } -if (do64 (ghdr, fname, reltype) <0 ) +if (do64 (ghdr, reltype) <0 ) goto out;break ; }
tools/objtool/recordmcount.h +8 -11
@@ -466,8 +466,7 @@ static int find_secsym_ndx(unsigned const txtndx,/* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */ static char const *__has_rel_mcount (GElf_Shdr const *const relhdr,/* reltype */ Elf_Shdr const *const shdr0, -char const *const shstrtab, -char const *const fname) +char const *const shstrtab) {/* .sh_info depends on .sh_type == SHT_REL[,A] */ Elf_Shdr const *const txthdr = &shdr0[relhdr->sh_info];
@@ -481,25 +480,23 @@ static char const * __has_rel_mcount(GElf_Shdr const *const relhdr, /* reltype *static char const *has_rel_mcount (GElf_Shdr const *const relhdr,Elf_Shdr const *const shdr0, -char const *const shstrtab, -char const *const fname) +char const *const shstrtab) {if (relhdr->sh_type !=SHT_REL && relhdr->sh_type !=SHT_RELA )return NULL ; -return __has_rel_mcount (relhdr, shdr0, shstrtab, fname); +return __has_rel_mcount (relhdr, shdr0, shstrtab); }static unsigned tot_relsize (Elf_Shdr const *const shdr0, -const char *const shstrtab, -const char *const fname) +const char *const shstrtab) {struct section *sec;unsigned totrelsz =0 ;char const *txtname;list_for_each_entry (sec, &lf->sections, list) { - txtname =has_rel_mcount (&sec->sh, shdr0, shstrtab, fname); + txtname =has_rel_mcount (&sec->sh, shdr0, shstrtab);if (txtname &&is_mcounted_section_name (txtname)) totrelsz += sec->sh.sh_size; }
@@ -508,7 +505,7 @@ static unsigned tot_relsize(Elf_Shdr const *const shdr0,/* Overall supervision for Elf32 ET_REL file. */ -static int do_func (Elf_Ehdr *const ehdr,char const *const fname, +static int do_func (Elf_Ehdr *const ehdr,unsigned const reltype) { Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
@@ -538,7 +535,7 @@ static int do_func(Elf_Ehdr *const ehdr, char const *const fname,if (find_section_by_name (lf,"__mcount_loc" ) !=NULL )return 0 ; - totrelsz =tot_relsize (shdr0, shstrtab, fname); + totrelsz =tot_relsize (shdr0, shstrtab);if (totrelsz ==0 )return 0 ; mrel0 =umalloc (totrelsz);
@@ -559,7 +556,7 @@ static int do_func(Elf_Ehdr *const ehdr, char const *const fname, relhdr = &sec->sh; txtname =has_rel_mcount (relhdr, shdr0, - shstrtab, fname); + shstrtab);if (txtname &&is_mcounted_section_name (txtname)) {unsigned int recsym;uint_t recval =0 ;
Signature · 1 line
2.20.1
RFC PATCH v4 5/32 objtool: recordmcount: Convert do_func() relhdrs
In reply to Matt HelsleyUse objtool's ELF data structures to visit the relocation sections in the top-level ELF file walking function, do_func(). This means we can pass pointers to the relocation header structures into nested functions and avoid the indexing patterns for them.
These conversions don't use libelf/objtool to modify the ELF file -- it only changes the way we walk the ELF sections and do lookups to find the relocations.
--- tools/objtool/recordmcount.h | 62 +++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 30 deletions(-)
tools/objtool/recordmcount.h +32 -30
@@ -285,7 +285,7 @@ static unsigned get_mcountsym(Elf_Sym const *const sym0,return mcountsym; } -static void get_sym_str_and_relp(Elf_Shdr const *const relhdr, +static void get_sym_str_and_relp(GElf_Shdr const *const relhdr, Elf_Ehdr const *const ehdr, Elf_Sym const **sym0, char const **str0,
@@ -293,10 +293,10 @@ static void get_sym_str_and_relp(Elf_Shdr const *const relhdr, {Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w (ehdr->e_shoff) + (void *)ehdr); -unsigned const symsec_sh_link =w (relhdr->sh_link); +unsigned const symsec_sh_link = relhdr->sh_link;Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];Elf_Shdr const *const strsec = &shdr0[w (symsec->sh_link)]; -Elf_Rel const *const rel0 = (Elf_Rel const *)(_w (relhdr->sh_offset) +Elf_Rel const *const rel0 = (Elf_Rel const *)(relhdr->sh_offset + (void *)ehdr); *sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
@@ -316,9 +316,9 @@ static void get_sym_str_and_relp(Elf_Shdr const *const relhdr,static uint_t *sift_rel_mcount (uint_t *mlocp,unsigned const offbase,Elf_Rel **const mrelpp, -Elf_Shdr const *const relhdr, +GElf_Shdr const *const relhdr,Elf_Ehdr const *const ehdr, -unsigned const recsym, +unsigned const recsym_index,uint_t const recval,unsigned const reltype) {
@@ -327,8 +327,8 @@ static uint_t *sift_rel_mcount(uint_t *mlocp,Elf_Sym const *sym0;char const *str0;Elf_Rel const *relp; -unsigned rel_entsize =_w (relhdr->sh_entsize); -unsigned const nrel =_w (relhdr->sh_size) / rel_entsize; +unsigned int rel_entsize = relhdr->sh_entsize; +unsigned const nrel = relhdr->sh_size / rel_entsize;unsigned mcountsym =0 ;unsigned t;
@@ -344,7 +344,7 @@ static uint_t *sift_rel_mcount(uint_t *mlocp,_w (_w (relp->r_offset) - recval + mcount_adjust); mrelp->r_offset =_w (offbase + ((void *)mlocp - (void *)mloc0)); -Elf_r_info (mrelp, recsym, reltype); +Elf_r_info (mrelp, recsym_index, reltype);if (rel_entsize ==sizeof (Elf_Rela)) { ((Elf_Rela *)mrelp)->r_addend = addend; *mlocp++ =0 ;
@@ -364,7 +364,7 @@ static uint_t *sift_rel_mcount(uint_t *mlocp,* that are not going to be traced. The mcount calls here will be converted * into nops. */ -static int nop_mcount (Elf_Shdr const *const relhdr, +static int nop_mcount (GElf_Shdr const *const relhdr,Elf_Ehdr const *const ehdr,const char *const txtname) {
@@ -373,9 +373,9 @@ static int nop_mcount(Elf_Shdr const *const relhdr,Elf_Sym const *sym0;char const *str0;Elf_Rel const *relp; -Elf_Shdr const *const shdr = &shdr0[w (relhdr->sh_info)]; -unsigned rel_entsize =_w (relhdr->sh_entsize); -unsigned const nrel =_w (relhdr->sh_size) / rel_entsize; +Elf_Shdr const *const shdr = &shdr0[relhdr->sh_info]; +unsigned int rel_entsize = relhdr->sh_entsize; +unsigned const nrel = relhdr->sh_size / rel_entsize;unsigned mcountsym =0 ;unsigned t;int once =0 ;
@@ -464,13 +464,13 @@ static int find_secsym_ndx(unsigned const txtndx, }/* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */ -static char const *__has_rel_mcount (Elf_Shdr const *const relhdr,/* reltype */ +static char const *__has_rel_mcount (GElf_Shdr const *const relhdr,/* reltype */ Elf_Shdr const *const shdr0,char const *const shstrtab,char const *const fname) {/* .sh_info depends on .sh_type == SHT_REL[,A] */ -Elf_Shdr const *const txthdr = &shdr0[w (relhdr->sh_info)]; +Elf_Shdr const *const txthdr = &shdr0[relhdr->sh_info];char const *const txtname = &shstrtab[w (txthdr->sh_name)]; if (w(txthdr->sh_type) != SHT_PROGBITS ||
@@ -479,30 +479,29 @@ static char const * __has_rel_mcount(Elf_Shdr const *const relhdr, /* reltype */return txtname; } -static char const *has_rel_mcount (Elf_Shdr const *const relhdr, +static char const *has_rel_mcount (GElf_Shdr const *const relhdr,Elf_Shdr const *const shdr0,char const *const shstrtab,char const *const fname) { -if (w (relhdr->sh_type) !=SHT_REL &&w (relhdr->sh_type) !=SHT_RELA ) +if (relhdr->sh_type !=SHT_REL && relhdr->sh_type !=SHT_RELA )return NULL ;return __has_rel_mcount (relhdr, shdr0, shstrtab, fname); }static unsigned tot_relsize (Elf_Shdr const *const shdr0, -unsigned nhdr,const char *const shstrtab,const char *const fname) { +struct section *sec;unsigned totrelsz =0 ; -Elf_Shdr const *shdrp = shdr0;char const *txtname; -for (; nhdr; --nhdr, ++shdrp) { - txtname =has_rel_mcount (shdrp, shdr0, shstrtab, fname); +list_for_each_entry (sec, &lf->sections, list) { + txtname =has_rel_mcount (&sec->sh, shdr0, shstrtab, fname);if (txtname &&is_mcounted_section_name (txtname)) - totrelsz +=_w (shdrp->sh_size); + totrelsz += sec->sh.sh_size; }return totrelsz; }
@@ -514,13 +513,11 @@ static int do_func(Elf_Ehdr *const ehdr, char const *const fname, {Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w (ehdr->e_shoff) + (void *)ehdr); -unsigned const nhdr =w2 (ehdr->e_shnum);Elf_Shdr *const shstr = &shdr0[w2 (ehdr->e_shstrndx)];char const *const shstrtab = (char const *)(_w (shstr->sh_offset) + (void *)ehdr); -Elf_Shdr const *relhdr; -unsigned k; +GElf_Shdr const *relhdr;/* Upper bound on space: assume all relevant relocs are for mcount. */ unsigned totrelsz;
@@ -534,12 +531,14 @@ static int do_func(Elf_Ehdr *const ehdr, char const *const fname,unsigned rel_entsize =0 ;unsigned symsec_sh_link =0 ; +struct section *sec; +int result =0 ;if (find_section_by_name (lf,"__mcount_loc" ) !=NULL )return 0 ; - totrelsz =tot_relsize (shdr0, nhdr, shstrtab, fname); + totrelsz =tot_relsize (shdr0, shstrtab, fname);if (totrelsz ==0 )return 0 ; mrel0 =umalloc (totrelsz);
@@ -555,22 +554,25 @@ static int do_func(Elf_Ehdr *const ehdr, char const *const fname,return -1 ; } -for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) { -char const *const txtname =has_rel_mcount (relhdr, shdr0, +list_for_each_entry (sec, &lf->sections, list) { +char const *txtname; + + relhdr = &sec->sh; + txtname =has_rel_mcount (relhdr, shdr0, shstrtab, fname);if (txtname &&is_mcounted_section_name (txtname)) {unsigned int recsym;uint_t recval =0 ; - symsec_sh_link =w (relhdr->sh_link); - result =find_secsym_ndx (w (relhdr->sh_info), txtname, + symsec_sh_link = relhdr->sh_link; + result =find_secsym_ndx (relhdr->sh_info, txtname, &recval, &recsym, &shdr0[symsec_sh_link], ehdr);if (result) goto out; - rel_entsize =_w (relhdr->sh_entsize); + rel_entsize = relhdr->sh_entsize; mlocp =sift_rel_mcount (mlocp, (void *)mlocp - (void *)mloc0, &mrelp, relhdr, ehdr, recsym, recval, reltype);
Signature · 1 line
2.20.1
RFC PATCH v4 4/32 objtool: recordmcount: Search for __mcount_loc before walking the sections
In reply to Matt Helsleyrecordmcount iterates over the sections in the order they're listed in the ELF file and checks whether the section name indicates it's of interest. Objtool's elf code works differently
-- it scans the elf file and builds up data structures
representing the headers, sections, etc. and then supplies functions to search these structures. Both walk the elf file in order, however objtool uses more memory to enable faster searches it needs for other tools such as the reliable backtrace support offered by the ORC unwinder.
Rather than walk the section table a second time in the recordmcount code, we use objtool's elf code to search for the section recordmcount is interested in. This also simplifies flow and means we can easily check for already-processed object files before we do any of the more complex things recordmcount does.
This also allows us to remove the already_has_rel_mcount string pointer trick.
--- tools/objtool/recordmcount.c | 4 ---- tools/objtool/recordmcount.h | 17 +++-------------- 2 files changed, 3 insertions(+), 18 deletions(-)
tools/objtool/recordmcount.c +0 -4
@@ -208,8 +208,6 @@ static void *mmap_file(char const *fname) }else mmap_failed =0 ; out: -elf_close (lf); - lf =NULL ; fd_map =-1 ; file_end = file_map + sb.st_size;
@@ -424,8 +422,6 @@ static int is_mcounted_section_name(char const *const txtname)strcmp (".cpuidle.text" , txtname) ==0 ; } -static char const *already_has_rel_mcount ="success" ;/* our work here is done! */ -/* 32 bit and 64 bit are very similar */ #include "recordmcount.h" #define RECORD_MCOUNT_64
tools/objtool/recordmcount.h +3 -14
@@ -473,11 +473,6 @@ static char const * __has_rel_mcount(Elf_Shdr const *const relhdr, /* reltype */Elf_Shdr const *const txthdr = &shdr0[w (relhdr->sh_info)];char const *const txtname = &shstrtab[w (txthdr->sh_name)]; -if (strcmp ("__mcount_loc" , txtname) ==0 ) { -fprintf (stderr,"warning: __mcount_loc already exists: %s\n" , - fname); -return already_has_rel_mcount; - }if (w (txthdr->sh_type) !=SHT_PROGBITS || !(_w (txthdr->sh_flags) &SHF_EXECINSTR ))return NULL ;
@@ -506,10 +501,6 @@ static unsigned tot_relsize(Elf_Shdr const *const shdr0,for (; nhdr; --nhdr, ++shdrp) { txtname =has_rel_mcount (shdrp, shdr0, shstrtab, fname); -if (txtname == already_has_rel_mcount) { - totrelsz =0 ; -break ; - }if (txtname &&is_mcounted_section_name (txtname)) totrelsz +=_w (shdrp->sh_size); }
@@ -545,6 +536,9 @@ static int do_func(Elf_Ehdr *const ehdr, char const *const fname,int result =0 ; +if (find_section_by_name (lf,"__mcount_loc" ) !=NULL ) +return 0 ; + totrelsz =tot_relsize (shdr0, nhdr, shstrtab, fname);if (totrelsz ==0 )return 0 ;
@@ -564,11 +558,6 @@ static int do_func(Elf_Ehdr *const ehdr, char const *const fname,for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {char const *const txtname =has_rel_mcount (relhdr, shdr0, shstrtab, fname); -if (txtname == already_has_rel_mcount) { - result =0 ; - file_updated =0 ; - goto out;/* Nothing to be done; don't append! */ - }if (txtname &&is_mcounted_section_name (txtname)) {unsigned int recsym;uint_t recval =0 ;
Signature · 1 line
2.20.1
RFC PATCH v4 2/32 objtool: Make recordmcount into mcount subcmd
In reply to Matt HelsleyRather than a standalone executable merge recordmcount as a sub command of objtool. This is a small step towards cleaning up recordmcount and eventually sharing ELF code with objtool.
For the initial step all that's required is a bit of Makefile changes and invoking the former main() function from recordmcount.c because the subcommand code uses similar function arguments as main when dispatching.
objtool ignores some object files that tracing does not, specifically those with OBJECT_FILES_NON_STANDARD Makefile variables. For this reason we keep the recordmcount_dep separate from the objtool_dep. When using objtool mcount we can also, like the other objtool invocations, just depend on the binary rather than the source the binary is built from.
Subsequent patches will gradually convert recordmcount to use more and more of libelf/objtool's ELF accessor code. This will both clean up recordmcount to be more easily readable and remove recordmcount's crude accessor wrapping code.
--- Documentation/dontdiff | 2 +- Documentation/trace/ftrace.rst | 6 ++-- Makefile | 9 ++++-- arch/arm64/include/asm/ftrace.h | 2 +- arch/x86/include/asm/ftrace.h | 2 +- kernel/trace/Kconfig | 2 +- scripts/Makefile.build | 19 +++++++------ scripts/sorttable.h | 2 +- tools/objtool/Build | 4 +-- tools/objtool/Makefile | 48 +++++++++++++++++++++++-------- tools/objtool/builtin-mcount.c | 50 +++++++++++++++++++++++++++++++++ tools/objtool/builtin.h | 2 ++ tools/objtool/objtool.c | 1 + tools/objtool/objtool.h | 1 + tools/objtool/recordmcount.c | 36 +++++++----------------- tools/objtool/weak.c | 5 ++++ 16 files changed, 131 insertions(+), 60 deletions(-) create mode 100644 tools/objtool/builtin-mcount.c
Documentation/dontdiff +1 -1
@@ -211,7 +211,7 @@ r420_reg_safe.h r600_reg_safe.h randomize_layout_hash.h randomize_layout_seed.h -recordmcount +objtool relocs rlim_names.h rn50_reg_safe.h
Documentation/trace/ftrace.rst +3 -3
@@ -2684,8 +2684,8 @@ every kernel function, produced by the -pg switch in gcc), starts of pointing to a simple return. (Enabling FTRACE will include the -pg switch in the compiling of the kernel.) -At compile time every C file object is run through the -recordmcount program (located in the tools/objtool directory). This +At compile time every C file object is run through objtool's +mcount subcommand (located in the tools/objtool directory). This program will parse the ELF headers in the C object to find all the locations in the .text section that call mcount. Starting with gcc version 4.6, the -mfentry has been added for x86, which
@@ -2699,7 +2699,7 @@ can be traced. A section called "__mcount_loc" is created that holds references to all the mcount/fentry call sites in the .text section. -The recordmcount program re-links this section back into the +Running "objtool mcount" re-links this section back into the original object. The final linking stage of the kernel will add all these references into a single table.
Makefile +6 -3
@@ -842,12 +842,12 @@ KBUILD_CFLAGS += $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING) KBUILD_AFLAGS += $(CC_FLAGS_USING) ifdef CONFIG_DYNAMIC_FTRACE ifdef CONFIG_HAVE_C_RECORDMCOUNT - BUILD_C_RECORDMCOUNT := y - export BUILD_C_RECORDMCOUNT + USE_OBJTOOL_MCOUNT := y + export USE_OBJTOOL_MCOUNT objtool_target := tools/objtool FORCE endif endif -endif +endif # CONFIG_FUNCTION_TRACER # We trigger additional mismatches with less inlining ifdef CONFIG_DEBUG_SECTION_MISMATCH
@@ -1168,6 +1168,9 @@ ifneq ($(has_libelf),1) ifdef CONFIG_UNWINDER_ORC @echo "error: Cannot generate ORC metadata for CONFIG_UNWINDER_ORC=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2 @false + else ifdef USE_OBJTOOL_MCOUNT + @echo "error: Cannot generate tracing metadata for CONFIG_DYNAMIC_FTRACE, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2 + @false else ifeq ($(SKIP_STACK_VALIDATION),1) @echo "warning: Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2
arch/arm64/include/asm/ftrace.h +1 -1
@@ -62,7 +62,7 @@ static inline unsigned long ftrace_call_adjust(unsigned long addr)return addr +AARCH64_INSN_SIZE ;/* * addr is the address of the mcount call instruction. -* recordmcount does the necessary offset calculation. +* "objtool mcount" does the necessary offset calculation. */ return addr; }
arch/x86/include/asm/ftrace.h +1 -1
@@ -23,7 +23,7 @@ static inline unsigned long ftrace_call_adjust(unsigned long addr) {/* * addr is the address of the mcount call instruction. -* recordmcount does the necessary offset calculation. +* "objtool mcount" does the necessary offset calculation. */ return addr; }
kernel/trace/Kconfig +1 -1
@@ -59,7 +59,7 @@ config HAVE_NOP_MCOUNT config HAVE_C_RECORDMCOUNT bool help - C version of recordmcount available? + C version of objtool mcount available? config TRACER_MAX_TRACE bool
scripts/Makefile.build +10 -9
@@ -163,22 +163,23 @@ endif ifdef CONFIG_FTRACE_MCOUNT_RECORD ifndef CC_USING_RECORD_MCOUNT -# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl -ifdef BUILD_C_RECORDMCOUNT +# The compiler does not support generation of the __mcount_loc section. +# Generate it manually with "objtool mcount record" or recordmcount.pl +ifdef USE_OBJTOOL_MCOUNT ifeq ("$(origin RECORDMCOUNT_WARN)", "command line") RECORDMCOUNT_FLAGS = -w endif # Due to recursion, we must skip empty.o. # The empty.o file is created in the make process in order to determine # the target endianness and word size. It is made before all other C -# files, including recordmcount. +# files, including objtool. sub_cmd_record_mcount = \ if [ $(@) != "scripts/mod/empty.o" ]; then \ - $(objtree)/tools/objtool/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \ + $(objtree)/tools/objtool/objtool mcount record $(RECORDMCOUNT_FLAGS) "$(@)"; \ fi; -recordmcount_source := $(srctree)/tools/objtool/recordmcount.c \ - $(srctree)/tools/objtool/recordmcount.h +recordmcount_dep = $(objtree)/tools/objtool/objtool \ + $(wildcard include/config/ftrace/mcount/record.h) else sub_cmd_record_mcount = perl $(srctree)/tools/objtool/recordmcount.pl "$(ARCH)" \ "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
@@ -186,8 +187,8 @@ sub_cmd_record_mcount = perl $(srctree)/tools/objtool/recordmcount.pl "$(ARCH)" "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \ "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \ "$(if $(part-of-module),1,0)" "$(@)"; -recordmcount_source := $(srctree)/tools/objtool/recordmcount.pl -endif # BUILD_C_RECORDMCOUNT +recordmcount_dep = $(srctree)/tools/objtool/recordmcount.pl +endif # USE_OBJTOOL_MCOUNT cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \ $(sub_cmd_record_mcount)) endif # CC_USING_RECORD_MCOUNT
@@ -263,7 +264,7 @@ cmd_undef_syms = echo endif # Built-in and composite module parts -$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE +$(obj)/%.o: $(src)/%.c $(recordmcount_dep) $(objtool_dep) FORCE $(call cmd,force_checksrc) $(call if_changed_rule,cc_o_c)
scripts/sorttable.h +1 -1
@@ -11,7 +11,7 @@* Some of code was taken out of arch/x86/kernel/unwind_orc.c, written by: * Copyright (C) 2017 Josh Poimboeuf <[email protected]> * -* Some of this code was taken out of recordmcount.h written by: +* Some of this code was taken out of objtool mcount written by: * * Copyright 2009 John F. Reiser <[email protected]>. All rights reserved. * Copyright 2010 Steven Rostedt <[email protected]>, Red Hat Inc.
tools/objtool/Build +2 -2
@@ -7,9 +7,11 @@ objtool-$(SUBCMD_CHECK) += special.o objtool-$(SUBCMD_ORC) += check.o objtool-$(SUBCMD_ORC) += orc_gen.o objtool-$(SUBCMD_ORC) += orc_dump.o +objtool-$(SUBCMD_MCOUNT) += recordmcount.o objtool-y += builtin-check.o objtool-y += builtin-orc.o +objtool-y += builtin-mcount.o objtool-y += elf.o objtool-y += objtool.o
@@ -35,5 +37,3 @@ $(OUTPUT)str_error_r.o: ../lib/str_error_r.c FORCE $(OUTPUT)librbtree.o: ../lib/rbtree.c FORCE $(call rule_mkdir) $(call if_changed_dep,cc_o_c) - -recordmcount-y += recordmcount.o
tools/objtool/Makefile +36 -12
81 lines not on this page · raw message
tools/objtool/builtin-mcount.c new file +50 -0
50 lines not on this page · raw message
tools/objtool/builtin.h +2 -0
@@ -12,5 +12,7 @@ extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats,extern int cmd_check (int argc,const char **argv);extern int cmd_orc (int argc,const char **argv); +extern bool is_cmd_mcount_available (void ); +extern int cmd_mcount (int argc,const char **argv);#endif /* _BUILTIN_H */
tools/objtool/objtool.c +1 -0
@@ -35,6 +35,7 @@ static const char objtool_usage_string[] =static struct cmd_struct objtool_cmds[] = { {"check" , cmd_check,"Perform stack metadata validation on an object file" }, {"orc" , cmd_orc,"Generate in-place ORC unwind tables for an object file" }, + {"mcount" , cmd_mcount,"Construct a table of locations of calls to mcount. Useful for ftrace." }, };bool help;
tools/objtool/objtool.h +1 -0
@@ -21,6 +21,7 @@ struct objtool_file {int check (const char *objname,bool orc);int orc_dump (const char *objname); +int record_mcount (int argc,const char **argv);int create_orc (struct objtool_file *file);int create_orc_sections (struct objtool_file *file);
tools/objtool/recordmcount.c +10 -26
@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0-only /* -* recordmcount.c: construct a table of the locations of calls to 'mcount' -* so that ftrace can find them quickly. +* Construct a table of the locations of calls to 'mcount' so that ftrace +* can find them quickly. * Copyright 2009 John F. Reiser <[email protected]>. All rights reserved. * * Restructured to fit Linux format, as well as other updates:
@@ -24,7 +24,6 @@#include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> -#include <getopt.h> #include <elf.h> #include <fcntl.h> #include <stdio.h>
@@ -32,6 +31,8 @@#include <string.h> #include <unistd.h> +#include "objtool.h" +#ifndef EM_AARCH64 #define EM_AARCH64 183#define R_AARCH64_NONE 0
@@ -47,7 +48,7 @@ static int mmap_failed; /* Boolean flag. */static char gpfx;/* prefix for global symbol name (sometimes '_') */ static struct stat sb;/* Remember .st_size, etc. */ static const char *altmcount;/* alternate mcount symbol name */ -static int warn_on_notrace_sect;/* warn when section has mcount not being recorded */ +extern int warn_on_notrace_sect;/* warn when section has mcount not being recorded */ static void *file_map;/* pointer of the mapped file */ static void *file_end;/* pointer to the end of the mapped file */ static int file_updated;/* flag to state file was changed */
@@ -488,7 +489,7 @@ static int do_file(char const *const fname) goto out;case ELFDATA2LSB :if (*(unsigned char const *)&endian !=1 ) { -/* main() is big endian, file.o is little endian. */ +/* objtool is big endian, file.o is little endian. */ w = w4rev; w2 = w2rev; w8 = w8rev;
@@ -501,7 +502,7 @@ static int do_file(char const *const fname)break ;case ELFDATA2MSB :if (*(unsigned char const *)&endian !=0 ) { -/* main() is little endian, file.o is big endian. */ +/* objtool is little endian, file.o is big endian. */ w = w4rev; w2 = w2rev; w8 = w8rev;
@@ -615,33 +616,16 @@ static int do_file(char const *const fname)return rc; } -int main (int argc,char *argv[]) +int record_mcount (int argc,const char **argv) {const char ftrace[] ="/ftrace.o" ;int ftrace_size =sizeof (ftrace) -1 ;int n_error =0 ;/* gcc-4.3.0 false positive complaint */ -int c;int i; -while ((c =getopt (argc, argv,"w" )) >=0 ) { -switch (c) { -case 'w' : - warn_on_notrace_sect =1 ; -break ; -default : -fprintf (stderr,"usage: recordmcount [-w] file.o...\n" ); -return 0 ; - } - } - -if ((argc - optind) <1 ) { -fprintf (stderr,"usage: recordmcount [-w] file.o...\n" ); -return 0 ; - } -/* Process each file in turn, allowing deep failure. */ -for (i = optind; i < argc; i++) { -char *file = argv[i]; +for (i =0 ; i < argc; i++) { +const char *file = argv[i];int len;/*
tools/objtool/weak.c +5 -0
@@ -29,6 +29,11 @@ int __weak orc_dump(const char *_objname)UNSUPPORTED ("orc" ); } +int __weak record_mcount (int argc,const char **argv) +{ +UNSUPPORTED ("mcount" ); +} +int __weak create_orc (struct objtool_file *file) {UNSUPPORTED ("orc" );
Signature · 1 line
2.20.1
RFC PATCH v4 27/32 objtool: mcount: Generic location and relocation table types
In reply to Matt HelsleyRather than building the exact ELF section data we need and avoiding libelf's conversion step, use more GElf types and then libelf's elfxx_xlatetof() functions to convert the mcount locations (GElf_Addr) and associated relocations.
This converts sift_rel_mcount() so that it doesn't use the recordmcount wrapper. The next patch will move it out of the wrapper.
--- tools/objtool/recordmcount.c | 44 +++---------- tools/objtool/recordmcount.h | 120 ++++++++++++++--------------------- 2 files changed, 59 insertions(+), 105 deletions(-)
tools/objtool/recordmcount.c +10 -34
@@ -210,7 +210,7 @@ static int is_mcounted_section_name(char const *const txtname)strcmp (".cpuidle.text" , txtname) ==0 ; } -static unsigned get_mcountsym (struct reloc *reloc) +static unsigned int get_mcount_sym_info (struct reloc *reloc) {struct symbol *sym = reloc->sym;char const *symname = sym->name;
@@ -321,16 +321,16 @@ static int nop_mcount(struct section * const rels, {struct reloc *reloc;struct section *txts =find_section_by_index (lf, rels->sh.sh_info); -unsigned mcountsym =0 ; +unsigned int mcount_sym_info =0 ;int once =0 ;list_for_each_entry (reloc, &rels->reloc_list, list) {int ret =-1 ; -if (!mcountsym) - mcountsym =get_mcountsym (reloc); +if (!mcount_sym_info) + mcount_sym_info =get_mcount_sym_info (reloc); -if (mcountsym ==GELF_R_INFO (reloc->sym->idx, reloc->type) && !is_fake_mcount (reloc)) { +if (mcount_sym_info ==GELF_R_INFO (reloc->sym->idx, reloc->type) && !is_fake_mcount (reloc)) {if (make_nop) { ret =make_nop (txts, reloc->offset);if (ret <0 )
@@ -391,6 +391,9 @@ static unsigned tot_relsize(unsigned int *rel_entsize)*/ static int mcount_adjust =0 ; +/* Size of an entry in __mcount_loc; 4 or 8 */ +static size_t loc_size; +/* 32 bit and 64 bit are very similar */ #include "recordmcount.h" #define RECORD_MCOUNT_64
@@ -408,34 +411,6 @@ static int arm_is_fake_mcount(struct reloc const *rp)return 1 ; } -/* 64-bit EM_MIPS has weird ELF64_Rela.r_info. -* http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf -* We interpret Table 29 Relocation Operation (Elf64_Rel, Elf64_Rela) [p.40] -* to imply the order of the members; the spec does not say so. -* typedef unsigned char Elf64_Byte; -* fails on MIPS64 because their <elf.h> already has it! -*/ - -typedef uint8_t myElf64_Byte ;/* Type for a 8-bit quantity. */ - -union mips_r_info { -Elf64_Xword r_info; -struct { -Elf64_Word r_sym;/* Symbol index. */ -myElf64_Byte r_ssym;/* Special symbol. */ -myElf64_Byte r_type3;/* Third relocation. */ -myElf64_Byte r_type2;/* Second relocation. */ -myElf64_Byte r_type;/* First relocation. */ - } r_mips; -}; - -static void MIPS64_r_info (Elf64_Rel *const rp,unsigned sym,unsigned type) -{ - rp->r_info = ((union mips_r_info ){ - .r_mips = { .r_sym =w (sym), .r_type = type } - }).r_info; -} -static int do_file (char const *const fname) {unsigned int reltype =0 ;
@@ -551,6 +526,7 @@ static int do_file(char const *const fname) reltype =R_MIPS_32 ; is_fake_mcount = MIPS_is_fake_mcount; } + loc_size =4 ; rc =do32 (reltype);break ;case ELFCLASS64 : {
@@ -566,9 +542,9 @@ static int do_file(char const *const fname) }if (lf->ehdr.e_machine ==EM_MIPS ) { reltype =R_MIPS_64 ; - Elf64_r_info = MIPS64_r_info; is_fake_mcount = MIPS_is_fake_mcount; } + loc_size =8 ; rc =do64 (reltype);break ; }
tools/objtool/recordmcount.h +49 -71
196 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 28/32 objtool: mcount: Move sift_rel_mcount out of wrapper file
In reply to Matt HelsleyNow that this function no longer uses any of the old recordmcount wrapper code we can move it out of the wrapper too.
--- tools/objtool/recordmcount.c | 47 +++++++++++++++++++++++++++++++++ tools/objtool/recordmcount.h | 50 ------------------------------------ 2 files changed, 47 insertions(+), 50 deletions(-)
tools/objtool/recordmcount.c +47 -0
@@ -394,6 +394,53 @@ static int mcount_adjust = 0;/* Size of an entry in __mcount_loc; 4 or 8 */ static size_t loc_size; +/* +* Look at the relocations in order to find the calls to mcount. +* Accumulate the section offsets that are found, and their relocation info, +* onto the end of the existing arrays. +*/ +static void sift_rel_mcount (GElf_Addr **mlocpp, +GElf_Sxword *r_offsetp, +void **const mrelpp, +const struct section *const rels, +unsigned const recsym_index, +unsigned long const recval, +unsigned const reltype, +bool is_rela) +{ +GElf_Rel *mrelp = *mrelpp; +GElf_Rela *mrelap = *mrelpp; +unsigned int mcount_sym_info =0 ; +struct reloc *reloc; + +list_for_each_entry (reloc, &rels->reloc_list, list) { +unsigned long addend; + +if (!mcount_sym_info) + mcount_sym_info =get_mcount_sym_info (reloc); + +if (mcount_sym_info !=GELF_R_INFO (reloc->sym->idx, reloc->type) ||is_fake_mcount (reloc)) +continue ; + + addend = reloc->offset - recval + mcount_adjust; +if (is_rela) { + mrelap->r_offset = *r_offsetp; + mrelap->r_info =GELF_R_INFO (recsym_index, reltype); + mrelap->r_addend = addend; + mrelap++; + **mlocpp =0 ; + }else { + mrelp->r_offset = *r_offsetp; + mrelp->r_info =GELF_R_INFO (recsym_index, reltype); + mrelp++; + **mlocpp = addend; + } + (*mlocpp)++; + r_offsetp += loc_size; + } + *mrelpp = is_rela ? (void *)mrelap : (void *)mrelp; +} +/* 32 bit and 64 bit are very similar */ #include "recordmcount.h" #define RECORD_MCOUNT_64
tools/objtool/recordmcount.h +0 -50
67 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC PATCH v4 32/32 objtool: mcount: Rename
In reply to Matt HelsleyNow that we've converted recordmcount to a subcommand of objtool rename the .c file in order to follow the convention of the other objtool subcmds.
---
tools/objtool/Build | 2 +-
tools/objtool/{recordmcount.c => mcount.c} | 0
2 files changed, 1 insertion(+), 1 deletion(-)
rename tools/objtool/{recordmcount.c => mcount.c} (100%)tools/objtool/Build +1 -1
@@ -7,7 +7,7 @@ objtool-$(SUBCMD_CHECK) += special.o objtool-$(SUBCMD_ORC) += check.o objtool-$(SUBCMD_ORC) += orc_gen.o objtool-$(SUBCMD_ORC) += orc_dump.o -objtool-$(SUBCMD_MCOUNT) += recordmcount.o +objtool-$(SUBCMD_MCOUNT) += mcount.o objtool-y += builtin-check.o objtool-y += builtin-orc.o
tools/objtool/mcount.c renamed from tools/objtool/recordmcount.c +0 -0
Signature · 1 line
2.20.1
RFC PATCH v4 31/32 objtool: mcount: Remove endian wrappers
In reply to Matt HelsleyNow that they're no longer used we can remove these endian wrappers.
--- tools/objtool/recordmcount.c | 63 ------------------------------------ 1 file changed, 63 deletions(-)
tools/objtool/recordmcount.c +0 -63
@@ -149,53 +149,6 @@ static int make_nop_arm64(struct section *txts, size_t const offset)return 0 ; } -/* w8rev, w8nat, ...: Handle endianness. */ - -static uint64_t w8rev (uint64_t const x) -{ -return ((0xff & (x >> (0 *8 ))) << (7 *8 )) - | ((0xff & (x >> (1 *8 ))) << (6 *8 )) - | ((0xff & (x >> (2 *8 ))) << (5 *8 )) - | ((0xff & (x >> (3 *8 ))) << (4 *8 )) - | ((0xff & (x >> (4 *8 ))) << (3 *8 )) - | ((0xff & (x >> (5 *8 ))) << (2 *8 )) - | ((0xff & (x >> (6 *8 ))) << (1 *8 )) - | ((0xff & (x >> (7 *8 ))) << (0 *8 )); -} - -static uint32_t w4rev (uint32_t const x) -{ -return ((0xff & (x >> (0 *8 ))) << (3 *8 )) - | ((0xff & (x >> (1 *8 ))) << (2 *8 )) - | ((0xff & (x >> (2 *8 ))) << (1 *8 )) - | ((0xff & (x >> (3 *8 ))) << (0 *8 )); -} - -static uint32_t w2rev (uint16_t const x) -{ -return ((0xff & (x >> (0 *8 ))) << (1 *8 )) - | ((0xff & (x >> (1 *8 ))) << (0 *8 )); -} - -static uint64_t w8nat (uint64_t const x) -{ -return x; -} - -static uint32_t w4nat (uint32_t const x) -{ -return x; -} - -static uint32_t w2nat (uint16_t const x) -{ -return x; -} - -static uint64_t (*w8)(uint64_t ); -static uint32_t (*w)(uint32_t ); -static uint32_t (*w2)(uint16_t ); -/* Names of the sections that could contain calls to mcount. */ static int is_mcounted_section_name (char const *const txtname) {
@@ -579,22 +532,12 @@ static int do_file(char const *const fname) goto out; } - w = w4nat; - w2 = w2nat; - w8 = w8nat;switch (lf->ehdr.e_ident[EI_DATA ]) { -static unsigned int const endian =1 ;default :fprintf (stderr,"unrecognized ELF data encoding %d: %s\n" , lf->ehdr.e_ident[EI_DATA ], fname); goto out;case ELFDATA2LSB : -if (*(unsigned char const *)&endian !=1 ) { -/* objtool is big endian, file.o is little endian. */ - w = w4rev; - w2 = w2rev; - w8 = w8rev; - } ideal_nop4_arm = ideal_nop4_arm_le; bl_mcount_arm = bl_mcount_arm_le; push_arm = push_arm_le;
@@ -602,12 +545,6 @@ static int do_file(char const *const fname) push_bl_mcount_thumb = push_bl_mcount_thumb_le;break ;case ELFDATA2MSB : -if (*(unsigned char const *)&endian !=0 ) { -/* objtool is little endian, file.o is big endian. */ - w = w4rev; - w2 = w2rev; - w8 = w8rev; - } ideal_nop4_arm = ideal_nop4_arm_be; bl_mcount_arm = bl_mcount_arm_be; push_arm = push_arm_be;
Signature · 1 line
2.20.1
RFC PATCH v4 29/32 objtool: mcount: Remove wrapper for ELF relocation type
In reply to Matt HelsleyRemove the last use of the Elf_Rela wrapper by passing the size of the relocations we're dealing with as a parameter.
--- tools/objtool/recordmcount.c | 4 ++-- tools/objtool/recordmcount.h | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-)
tools/objtool/recordmcount.c +2 -2
@@ -574,7 +574,7 @@ static int do_file(char const *const fname) is_fake_mcount = MIPS_is_fake_mcount; } loc_size =4 ; - rc =do32 (reltype); + rc =do32 (reltype,sizeof (Elf32_Rela));break ;case ELFCLASS64 : { if (lf->ehdr.e_ehsize != sizeof(Elf64_Ehdr)
@@ -592,7 +592,7 @@ static int do_file(char const *const fname) is_fake_mcount = MIPS_is_fake_mcount; } loc_size =8 ; - rc =do64 (reltype); + rc =do64 (reltype,sizeof (Elf64_Rela));break ; } }/* end switch */
tools/objtool/recordmcount.h +2 -5
@@ -18,19 +18,16 @@* Copyright 2010 Steven Rostedt <[email protected]>, Red Hat Inc. */ #undef do_func -#undef Elf_Rela#ifdef RECORD_MCOUNT_64 # define do_func do64 -# define Elf_Rela Elf64_Rela#else # define do_func do32 -# define Elf_Rela Elf32_Rela#endif /* Overall supervision for Elf32 ET_REL file. */ -static int do_func (unsigned const reltype) +static int do_func (unsigned const reltype,size_t rela_size) {/* Upper bound on space: assume all relevant relocs are for mcount. */ unsigned totrelsz;
@@ -71,7 +68,7 @@ static int do_func(unsigned const reltype)return -1 ; } - is_rela = (sizeof (Elf_Rela) == rel_entsize); + is_rela = (rela_size == rel_entsize); mc_name = is_rela ?".rela__mcount_loc" :".rel__mcount_loc" ;
Signature · 1 line
2.20.1
Re: [RFC][PATCH v4 27/32] objtool: mcount: Generic location and relocation table types
In reply to Matt HelsleyQuoted 19 lines · Matt Helsley
Rather than building the exact ELF section data we need and avoiding libelf's conversion step, use more GElf types and then libelf's
elfxx_xlatetof()functions to convert the mcount locations (GElf_Addr) and associated relocations.This converts
sift_rel_mcount()so that it doesn't use the recordmcount wrapper. The next patch will move it out of the wrapper.Signed-off-by Matt Helsley <[email protected]>--- tools/objtool/recordmcount.c | 44 +++---------- tools/objtool/recordmcount.h | 120 ++++++++++++++--------------------- 2 files changed, 59 insertions(+), 105 deletions(-)tools/objtool/recordmcount.c +0
-0
[...]
Quoted 36 lines
tools/objtool/recordmcount.c +8
-21-static uint_t *sift_rel_mcount (uint_t *mlocp, -unsigned const offbase, -Elf_Rel **const mrelpp, +static void sift_rel_mcount (GElf_Addr **mlocpp, +GElf_Sxword *r_offsetp, +void **const mrelpp,const struct section *const rels,unsigned const recsym_index,unsigned long const recval, -unsigned const reltype) +unsigned const reltype, +bool is_rela) { -uint_t *const mloc0 = mlocp; -Elf_Rel *mrelp = *mrelpp; -unsigned int rel_entsize = rels->sh.sh_entsize; -unsigned mcountsym =0 ; +GElf_Rel *mrelp = *mrelpp; +GElf_Rela *mrelap = *mrelpp; +unsigned int mcount_sym_info =0 ;struct reloc *reloc;list_for_each_entry (reloc, &rels->reloc_list, list) { -if (!mcountsym) - mcountsym =get_mcountsym (reloc); - -if (mcountsym ==GELF_R_INFO (reloc->sym->idx, reloc->type) && !is_fake_mcount (reloc)) { -uint_t const addend = -_w (reloc->offset - recval + mcount_adjust); - mrelp->r_offset =_w (offbase - + ((void *)mlocp - (void *)mloc0)); -Elf_r_info (mrelp, recsym_index, reltype); -if (rel_entsize ==sizeof (Elf_Rela)) { - ((Elf_Rela *)mrelp)->r_addend = addend; - *mlocp++ =0 ; - }else
tools/objtool/recordmcount.c
Quoted 10 lines
tools/objtool/recordmcount.c +7
-3- *mlocp++ = addend; - - mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp); +unsigned long addend; + +if (!mcount_sym_info) + mcount_sym_info =get_mcount_sym_info (reloc); + +if (mcount_sym_info !=GELF_R_INFO (reloc->sym->idx, reloc->type) ||is_fake_mcount (reloc)) +continue ;
Hi Matt,
I was trying out the patch series on ppc64le and found that __mcount_loc and .rela__mcount_loc section pairs do not get generated.
# readelf -S fs/proc/cmdline.o|grep mcount #
Debugged the cause to get_mcountsym()'s return type. It returns reloc type from GELF_R_INFO() and expects Elf64_Xword a.k.a unsigned long to be the return type but get_mcountsym() returns unsigned int on 64-bit.
On power the _mcount is of relocation type R_PPC64_REL24 (info 0x170000000a), using unsigned int truncates the value to 0xa and fails the above check. Using below fix, that converts mcount_sym_info to use unsigned long, generates the __mcount_loc section pairs.
tools/objtool/mcount.c +3 -3
@@ -163,7 +163,7 @@ static int is_mcounted_section_name(char const *const txtname)strcmp (".cpuidle.text" , txtname) ==0 ; } -static unsigned int get_mcount_sym_info (struct reloc *reloc) +static unsigned long get_mcount_sym_info (struct reloc *reloc) {struct symbol *sym = reloc->sym;char const *symname = sym->name;
@@ -274,7 +274,7 @@ static int nop_mcount(struct section * const rels, {struct reloc *reloc;struct section *txts =find_section_by_index (lf, rels->sh.sh_info); -unsigned int mcount_sym_info =0 ; +unsigned long mcount_sym_info =0 ;int once =0 ;list_for_each_entry (reloc, &rels->reloc_list, list) {
@@ -363,7 +363,7 @@ static void sift_rel_mcount(GElf_Addr **mlocpp, {GElf_Rel *mrelp = *mrelpp;GElf_Rela *mrelap = *mrelpp; -unsigned int mcount_sym_info =0 ; +unsigned long mcount_sym_info =0 ;struct reloc *reloc;list_for_each_entry (reloc, &rels->reloc_list, list) {
# readelf -S fs/proc/cmdline.o|grep mcount [31] __mcount_loc PROGBITS 0000000000000000 00022f10 [32] .rela__mcount_loc RELA 0000000000000000 00022f20
Quoted 6 lines
tools/objtool/recordmcount.c +6
-0+ + addend = reloc->offset - recval + mcount_adjust; +if (is_rela) { + mrelap->r_offset = *r_offsetp; + mrelap->r_info =GELF_R_INFO (recsym_index, reltype); + mrelap->r_addend = addend;
tools/objtool/recordmcount.c
Quoted 10 lines
tools/objtool/recordmcount.c +9
-0+ mrelap++; + **mlocpp =0 ; + }else { + mrelp->r_offset = *r_offsetp; + mrelp->r_info =GELF_R_INFO (recsym_index, reltype); + mrelp++; + **mlocpp = addend; } + (*mlocpp)++; + r_offsetp += loc_size;
the offsets generated for rela__mcount_loc section are incorrect:
# readelf -rW fs/proc/meminfo.o
[...]
Relocation section '.rela__mcount_loc' at offset 0x59a48 contains 4 entries:
Offset Info Type Symbol's Value Symbol's Name + Addend
0000000000000000 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + c
00000a059c401f38 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + 64
0000000000000000 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + 7c
0000000000000000 0000000600000026 R_PPC64_ADDR64 0000000000000000 .init.text + cchanging the above line to *r_offsetp += loc_size and initializing r_offset=0 in do_mcount() generates the correct offset:
# readelf -rW fs/proc/meminfo.o
[...]
Relocation section '.rela__mcount_loc' at offset 0x59a48 contains 4 entries:
Offset Info Type Symbol's Value Symbol's Name + Addend
0000000000000000 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + c
0000000000000008 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + 64
0000000000000010 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + 7c
0000000000000018 0000000600000026 R_PPC64_ADDR64 0000000000000000 .init.text + cQuoted 7 lines
tools/objtool/recordmcount.c +1
-2} - *mrelpp = mrelp; -return mlocp; + *mrelpp = is_rela ? (void *)mrelap : (void *)mrelp; }
Signature · 1 line
Kamalesh
Re: [RFC][PATCH v4 01/32] objtool: Prepare to merge recordmcount
In reply to Matt HelsleyHi Matt,
Quoted 71 lines · Matt Helsley
Move recordmcount into the objtool directory. We keep this step separate so changes which turn recordmcount into a subcommand of objtool don't get obscured.
Signed-off-by Matt Helsley <[email protected]>--- Documentation/trace/ftrace-design.rst | 4 ++-- Documentation/trace/ftrace.rst | 2 +- Makefile | 15 +++++++++------ scripts/.gitignore | 1 - scripts/Makefile | 1 - scripts/Makefile.build | 11 ++++++----- tools/objtool/.gitignore | 1 + tools/objtool/Build | 2 ++ tools/objtool/Makefile | 13 ++++++++++++- {scripts => tools/objtool}/recordmcount.c | 0 {scripts => tools/objtool}/recordmcount.h | 0 {scripts => tools/objtool}/recordmcount.pl | 0 12 files changed, 33 insertions(+), 17 deletions(-)rename {scripts =>
tools/objtool}/recordmcount.c(100%) rename {scripts =>tools/objtool}/recordmcount.h(100%) rename {scripts =>tools/objtool}/recordmcount.pl(100%)Documentation/trace/ftrace-design.rst +2
-2@@ -261,7 +261,7 @@ You need very few things to get the syscalls tracing in an arch. HAVE_FTRACE_MCOUNT_RECORD ------------------------- -See scripts/recordmcount.pl for more info. Just fill in the arch-specific +See tools/objtool/recordmcount.pl for more info. Just fill in the arch-specific details for how to locate the addresses of mcount call sites via objdump. This option doesn't make much sense without also implementing dynamic ftrace.@@ -379,7 +379,7 @@ linux/ftrace.h for the functions:: ftrace_make_call() The rec->ip value is the address of the mcount call site that was collected -by the scripts/recordmcount.pl during build time. +by the tools/objtool/recordmcount.pl during build time. The last function is used to do runtime patching of the active tracer. This will be modifying the assembly code at the location of the ftrace_call symbolDocumentation/trace/ftrace.rst +1
-1@@ -2685,7 +2685,7 @@ starts of pointing to a simple return. (Enabling FTRACE will include the -pg switch in the compiling of the kernel.) At compile time every C file object is run through the -recordmcount program (located in the scripts directory). This +recordmcount program (located in the tools/objtool directory). This program will parse the ELF headers in the C object to find all the locations in the .text section that call mcount. Starting with gcc version 4.6, the -mfentry has been added for x86, whichMakefile +1
-0@@ -844,6 +844,7 @@ ifdef CONFIG_DYNAMIC_FTRACE ifdef CONFIG_HAVE_C_RECORDMCOUNT BUILD_C_RECORDMCOUNT := y export BUILD_C_RECORDMCOUNT + objtool_target := tools/objtool FORCE endif endif endif
Makefile endif · line 1029
Quoted 6 lines
Makefile +2
-0@@ -1023,10 +1024,10 @@ endif export mod_sign_cmd HOST_LIBELF_LIBS = $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf) +has_libelf := $(call try-run,\ + echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0)
Maybe there could be some build dependency, e.g. CONFIG_OBJTOOL_SUBCMDS that sets the "objtool_target" and "has_libelf" when selected.
Then the CONFIG_UNWINDER_ORC, RECORD_MCOUNT and STACK_VALIDATION would just had to select that config option.
Quoted 10 lines
Makefile +0
-2ifdef CONFIG_STACK_VALIDATION - has_libelf := $(call try-run,\ - echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0) ifeq ($(has_libelf),1) objtool_target := tools/objtool FORCE else@@ -1163,13 +1164,15 @@ uapi-asm-generic: PHONY += prepare-objtool prepare-objtool: $(objtool_target)
Makefile uapi-asm-generic: · line 1173
Quoted 10 lines
Makefile +4
-3-ifeq ($(SKIP_STACK_VALIDATION),1) -ifdef CONFIG_UNWINDER_ORC +ifneq ($(has_libelf),1) + ifdef CONFIG_UNWINDER_ORC @echo "error: Cannot generate ORC metadata for CONFIG_UNWINDER_ORC=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2 @false -else + else + ifeq ($(SKIP_STACK_VALIDATION),1) @echo "warning: Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2
I think this would be more readable without the else branch:
ifneq ($(has_libelf),1) ifdef <some objtool command config> <warn about unavailability> endif ifdef <another objtool command config> <warn ...> endif <...> endif
Cheers,
Signature · 1 line
Julien Thierry
Re: [RFC][PATCH v4 02/32] objtool: Make recordmcount into mcount subcmd
In reply to Matt HelsleyHi Matt,
Quoted 138 lines · Matt Helsley
Rather than a standalone executable merge recordmcount as a sub command of objtool. This is a small step towards cleaning up recordmcount and eventually sharing ELF code with objtool.
For the initial step all that's required is a bit of
Makefilechanges and invoking the formermain()function fromrecordmcount.cbecause the subcommand code uses similar function arguments as main when dispatching.objtool ignores some object files that tracing does not, specifically those with
OBJECT_FILES_NON_STANDARDMakefilevariables. For this reason we keep therecordmcount_depseparate from theobjtool_dep. When using objtool mcount we can also, like the other objtool invocations, just depend on the binary rather than the source the binary is built from.Subsequent patches will gradually convert recordmcount to use more and more of libelf/objtool's ELF accessor code. This will both clean up recordmcount to be more easily readable and remove recordmcount's crude accessor wrapping code.
Signed-off-by Matt Helsley <[email protected]>--- Documentation/dontdiff | 2 +- Documentation/trace/ftrace.rst | 6 ++-- Makefile | 9 ++++-- arch/arm64/include/asm/ftrace.h | 2 +- arch/x86/include/asm/ftrace.h | 2 +- kernel/trace/Kconfig | 2 +- scripts/Makefile.build | 19 +++++++------ scripts/sorttable.h | 2 +- tools/objtool/Build | 4 +-- tools/objtool/Makefile | 48 +++++++++++++++++++++++-------- tools/objtool/builtin-mcount.c | 50 +++++++++++++++++++++++++++++++++ tools/objtool/builtin.h | 2 ++ tools/objtool/objtool.c | 1 + tools/objtool/objtool.h | 1 + tools/objtool/recordmcount.c | 36 +++++++----------------- tools/objtool/weak.c | 5 ++++ 16 files changed, 131 insertions(+), 60 deletions(-)create mode 100644
tools/objtool/builtin-mcount.cDocumentation/dontdiff +1
-1@@ -211,7 +211,7 @@ r420_reg_safe.h r600_reg_safe.h randomize_layout_hash.h randomize_layout_seed.h -recordmcount +objtool relocs rlim_names.h rn50_reg_safe.hDocumentation/trace/ftrace.rst +3
-3@@ -2684,8 +2684,8 @@ every kernel function, produced by the -pg switch in gcc), starts of pointing to a simple return. (Enabling FTRACE will include the -pg switch in the compiling of the kernel.) -At compile time every C file object is run through the -recordmcount program (located in the tools/objtool directory). This +At compile time every C file object is run through objtool's +mcount subcommand (located in the tools/objtool directory). This program will parse the ELF headers in the C object to find all the locations in the .text section that call mcount. Starting with gcc version 4.6, the -mfentry has been added for x86, which@@ -2699,7 +2699,7 @@ can be traced. A section called "__mcount_loc" is created that holds references to all the mcount/fentry call sites in the .text section. -The recordmcount program re-links this section back into the +Running "objtool mcount" re-links this section back into the original object. The final linking stage of the kernel will add all these references into a single table.Makefile +6
-3@@ -842,12 +842,12 @@ KBUILD_CFLAGS += $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING) KBUILD_AFLAGS += $(CC_FLAGS_USING) ifdef CONFIG_DYNAMIC_FTRACE ifdef CONFIG_HAVE_C_RECORDMCOUNT - BUILD_C_RECORDMCOUNT := y - export BUILD_C_RECORDMCOUNT + USE_OBJTOOL_MCOUNT := y + export USE_OBJTOOL_MCOUNT objtool_target := tools/objtool FORCE endif endif -endif +endif # CONFIG_FUNCTION_TRACER # We trigger additional mismatches with less inlining ifdef CONFIG_DEBUG_SECTION_MISMATCH@@ -1168,6 +1168,9 @@ ifneq ($(has_libelf),1) ifdef CONFIG_UNWINDER_ORC @echo "error: Cannot generate ORC metadata for CONFIG_UNWINDER_ORC=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2 @false + else ifdef USE_OBJTOOL_MCOUNT + @echo "error: Cannot generate tracing metadata for CONFIG_DYNAMIC_FTRACE, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2 + @false else ifeq ($(SKIP_STACK_VALIDATION),1) @echo "warning: Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2arch/arm64/include/asm/ftrace.h +1
-1@@ -62,7 +62,7 @@ static inline unsigned long ftrace_call_adjust(unsigned long addr)return addr +AARCH64_INSN_SIZE ;/* * addr is the address of the mcount call instruction. -* recordmcount does the necessary offset calculation. +* "objtool mcount" does the necessary offset calculation. */ return addr; }arch/x86/include/asm/ftrace.h +1
-1@@ -23,7 +23,7 @@ static inline unsigned long ftrace_call_adjust(unsigned long addr) {/* * addr is the address of the mcount call instruction. -* recordmcount does the necessary offset calculation. +* "objtool mcount" does the necessary offset calculation. */ return addr; }kernel/trace/Kconfig +0
-0
kernel/trace/Kconfig config HAVE_NOP_MCOUNT · line 62
Quoted 5 lines
kernel/trace/Kconfig +1
-1@@ -59,7 +59,7 @@ config HAVE_NOP_MCOUNT config HAVE_C_RECORDMCOUNT bool help - C version of recordmcount available? + C version of objtool mcount available?
The "C version" doesn't make much sense here. "Objtool mcount available?" or "mcount subcommand of objtool available?" perhaps?
Quoted 141 lines
kernel/trace/Kconfig +0
-0config TRACER_MAX_TRACE boolscripts/Makefile.build +10
-9@@ -163,22 +163,23 @@ endif ifdef CONFIG_FTRACE_MCOUNT_RECORD ifndef CC_USING_RECORD_MCOUNT -# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl -ifdef BUILD_C_RECORDMCOUNT +# The compiler does not support generation of the __mcount_loc section. +# Generate it manually with "objtool mcount record" or recordmcount.pl +ifdef USE_OBJTOOL_MCOUNT ifeq ("$(origin RECORDMCOUNT_WARN)", "command line") RECORDMCOUNT_FLAGS = -w endif # Due to recursion, we must skip empty.o. # The empty.o file is created in the make process in order to determine # the target endianness and word size. It is made before all other C -# files, including recordmcount. +# files, including objtool. sub_cmd_record_mcount = \ if [ $(@) != "scripts/mod/empty.o" ]; then \ - $(objtree)/tools/objtool/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \ + $(objtree)/tools/objtool/objtool mcount record $(RECORDMCOUNT_FLAGS) "$(@)"; \ fi; -recordmcount_source := $(srctree)/tools/objtool/recordmcount.c \ - $(srctree)/tools/objtool/recordmcount.h +recordmcount_dep = $(objtree)/tools/objtool/objtool \ + $(wildcard include/config/ftrace/mcount/record.h) else sub_cmd_record_mcount = perl $(srctree)/tools/objtool/recordmcount.pl "$(ARCH)" \ "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \@@ -186,8 +187,8 @@ sub_cmd_record_mcount = perl $(srctree)/tools/objtool/recordmcount.pl "$(ARCH)" "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \ "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \ "$(if $(part-of-module),1,0)" "$(@)"; -recordmcount_source := $(srctree)/tools/objtool/recordmcount.pl -endif # BUILD_C_RECORDMCOUNT +recordmcount_dep = $(srctree)/tools/objtool/recordmcount.pl +endif # USE_OBJTOOL_MCOUNT cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \ $(sub_cmd_record_mcount)) endif # CC_USING_RECORD_MCOUNT@@ -263,7 +264,7 @@ cmd_undef_syms = echo endif # Built-in and composite module parts -$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE +$(obj)/%.o: $(src)/%.c $(recordmcount_dep) $(objtool_dep) FORCE $(call cmd,force_checksrc) $(call if_changed_rule,cc_o_c)scripts/sorttable.h +1
-1@@ -11,7 +11,7 @@* Some of code was taken out of arch/x86/kernel/unwind_orc.c, written by: * Copyright (C) 2017 Josh Poimboeuf <[email protected]> * -* Some of this code was taken out of recordmcount.h written by: +* Some of this code was taken out of objtool mcount written by: * * Copyright 2009 John F. Reiser <[email protected]>. All rights reserved. * Copyright 2010 Steven Rostedt <[email protected]>, Red Hat Inc. tools/objtool/Build +2
-2@@ -7,9 +7,11 @@ objtool-$(SUBCMD_CHECK) += special.o objtool-$(SUBCMD_ORC) += check.o objtool-$(SUBCMD_ORC) += orc_gen.o objtool-$(SUBCMD_ORC) += orc_dump.o +objtool-$(SUBCMD_MCOUNT) += recordmcount.o objtool-y += builtin-check.o objtool-y += builtin-orc.o +objtool-y += builtin-mcount.o objtool-y += elf.o objtool-y += objtool.o@@ -35,5 +37,3 @@ $(OUTPUT)str_error_r.o: ../lib/str_error_r.c FORCE $(OUTPUT)librbtree.o: ../lib/rbtree.c FORCE $(call rule_mkdir) $(call if_changed_dep,cc_o_c) - -recordmcount-y += recordmcount.otools/objtool/Makefile +24
-6@@ -31,12 +31,6 @@ OBJTOOL_IN := $(OBJTOOL)-in.o LIBELF_FLAGS := $(shell pkg-config libelf --cflags 2>/dev/null) LIBELF_LIBS := $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf) -RECORDMCOUNT := $(OUTPUT)recordmcount -RECORDMCOUNT_IN := $(RECORDMCOUNT)-in.o -ifeq ($(BUILD_C_RECORDMCOUNT),y) -all: $(RECORDMCOUNT) -endif - all: $(OBJTOOL) INCLUDES := -I$(srctree)/tools/include \@@ -55,13 +49,47 @@ AWK = awk SUBCMD_CHECK := n SUBCMD_ORC := n +SUBCMD_MCOUNT := n ifeq ($(SRCARCH),x86) SUBCMD_CHECK := y SUBCMD_ORC := y + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),arm) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),arm64) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),ia64) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),mips) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),powerpc) + SUBCMD_MCOUNT := y +endif +
tools/objtool/Makefile AWK = awk · line 89
Quoted 10 lines
tools/objtool/Makefile +10
-0+ifeq ($(SRCARCH),s390) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),sh) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),sparc) + SUBCMD_MCOUNT := y
Is there some arch for which MCOUNT is not supported? If not you could just have MCOUNT default to 'y' and avoid adding all those tests (or maybe reduce the numbers and set to 'n' only for arches not supporting it).
Quoted 90 lines
tools/objtool/Makefile +2
-6endif -export SUBCMD_CHECK SUBCMD_ORC +export SUBCMD_CHECK SUBCMD_ORC SUBCMD_MCOUNT export srctree OUTPUT CFLAGS SRCARCH AWK include $(srctree)/tools/build/Makefile.include@@ -69,20 +97,16 @@ $(OBJTOOL_IN): fixdep FORCE @$(CONFIG_SHELL) ./sync-check.sh @$(MAKE) $(build)=objtool -$(RECORDMCOUNT_IN): fixdep FORCE - @$(MAKE) $(build)=recordmcount $(OBJTOOL): $(LIBSUBCMD) $(OBJTOOL_IN) $(QUIET_LINK)$(CC) $(OBJTOOL_IN) $(LDFLAGS) -o $@ -$(RECORDMCOUNT): $(RECORDMCOUNT_IN) - $(QUIET_LINK)$(CC) $(RECORDMCOUNT_IN) $(KBUILD_HOSTLDFLAGS) -o $@ $(LIBSUBCMD): fixdep FORCE $(Q)$(MAKE) -C $(SUBCMD_SRCDIR) OUTPUT=$(LIBSUBCMD_OUTPUT) clean: - $(call QUIET_CLEAN, objtool) $(RM) $(OBJTOOL) $(RECORDMCOUNT) + $(call QUIET_CLEAN, objtool) $(RM) $(OBJTOOL) $(Q)find $(OUTPUT) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete $(Q)$(RM) $(OUTPUT)arch/x86/inat-tables.c $(OUTPUT)fixdeptools/objtool/builtin-mcount.c new file +50
-0@@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +/* +* objtool mcount: +* +* This command analyzes a .o file and constructs a table of the locations of +* calls to 'mcount' useful to ftrace. We can optionally append this table to +* the object file ("objtool mcount record foo.o") or output it separately +* ("objtool mcount show"). The latter can be used to compare the expected +* callers of mcount to those actually found. +*/ + +#include <string.h> +#include <subcmd/parse-options.h> +#include "builtin.h" +#include "objtool.h" + +static const char *const mcount_usage[] = { +"objtool mcount record [<options>] file.o [file2.o ...]" , +NULL , +}; + +bool warn_on_notrace_sect; + +const static struct option mcount_options[] = { +OPT_BOOLEAN ('w' ,"warn-on-notrace-section" , &warn_on_notrace_sect, +"Emit a warning when a section omitting mcount " +"(possibly due to \"notrace\" marking) is encountered" ), +OPT_END (), +}; + +int cmd_mcount (int argc,const char **argv) +{ + argc--; argv++; +if (argc <=0 ) +usage_with_options (mcount_usage, mcount_options); + +if (!strncmp (argv[0 ],"record" ,6 )) { + argc =parse_options (argc, argv, + mcount_options, mcount_usage,0 ); +if (argc <1 ) +usage_with_options (mcount_usage, mcount_options); + +return record_mcount (argc, argv); + } + +usage_with_options (mcount_usage, mcount_options); + +return 0 ; +}tools/objtool/builtin.h +0
-0
tools/objtool/builtin.h extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, · line 15
Quoted 4 lines
tools/objtool/builtin.h +1
-0@@ -12,5 +12,7 @@ extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats,extern int cmd_check (int argc,const char **argv);extern int cmd_orc (int argc,const char **argv); +extern bool is_cmd_mcount_available (void );
This appears to be unused.
Cheers,
Signature · 1 line
Julien Thierry
Re: [RFC][PATCH v4 01/32] objtool: Prepare to merge recordmcount
In reply to Julien ThierryQuoted 8 lines · Julien Thierry
Hi Matt,
Quoted 5 lines · Matt Helsley
Move recordmcount into the objtool directory. We keep this step separate so changes which turn recordmcount into a subcommand of objtool don't get obscured.
Signed-off-by Matt Helsley <[email protected]>
<snip>
Quoted 21 lines
Quoted 13 lines
Makefile +1
-0@@ -844,6 +844,7 @@ ifdef CONFIG_DYNAMIC_FTRACE ifdef CONFIG_HAVE_C_RECORDMCOUNT BUILD_C_RECORDMCOUNT := y export BUILD_C_RECORDMCOUNT + objtool_target := tools/objtool FORCE endif endif endifMakefile endif · line 1027
Quoted 4 lines
Makefile +2
-0@@ -1023,10 +1024,10 @@ endif export mod_sign_cmd HOST_LIBELF_LIBS = $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf) +has_libelf := $(call try-run,\ + echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0)Maybe there could be some build dependency, e.g.
CONFIG_OBJTOOL_SUBCMDSthat sets the "objtool_target" and "has_libelf" when selected.
Quoted 2 lines
Then the
CONFIG_UNWINDER_ORC,RECORD_MCOUNTandSTACK_VALIDATIONwould just had to select that config option.
That might save a good amount of control flow in the Makefiles.
We could take it one step further and have specific CONFIG_OBJTOOL_<subcmd> which might help us remove the per-architecture control-flow in the multi-arch subcmd support found in tools/objtool/Makefile.
What do folks think of that -- too far?
Quoted 24 lines
Quoted 10 lines
Makefile +0
-2ifdef CONFIG_STACK_VALIDATION - has_libelf := $(call try-run,\ - echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0) ifeq ($(has_libelf),1) objtool_target := tools/objtool FORCE else@@ -1163,13 +1164,15 @@ uapi-asm-generic: PHONY += prepare-objtool prepare-objtool: $(objtool_target)Makefile uapi-asm-generic: · line 1172
Quoted 10 lines
Makefile +4
-3-ifeq ($(SKIP_STACK_VALIDATION),1) -ifdef CONFIG_UNWINDER_ORC +ifneq ($(has_libelf),1) + ifdef CONFIG_UNWINDER_ORC @echo "error: Cannot generate ORC metadata for CONFIG_UNWINDER_ORC=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2 @false -else + else + ifeq ($(SKIP_STACK_VALIDATION),1) @echo "warning: Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2I think this would be more readable without the else branch:
Quoted 3 lines
ifneq ($(has_libelf),1)
ifdef <some objtool command config>
<warn about unavailability>
Note: error not warn
Quoted 6 lines
endif ifdef <another objtool command config> <warn ...> endif <...> endif
I think the next patch, which makes recordmcount a subcmd, makes it a little clearer what the pattern is because it adds another ifdef block in the way you suggest.
As for the else around the SKIP_STACK_VALIDATION check -- it is special in a couple ways -- at least as best I can tell.
It's not a CONFIG_* -- it actually breaks the normal pattern with CONFIG_* in that..
It's about a judgement call that it's OK to merely warn and skip the stack validation rather than produce an error. The other, CONFIG_* blocks produce errors.
These two reasons are why I think it makes sense to keep the logic distinct with the "else".
Cheers,
-Matt Helsley
Re: [RFC][PATCH v4 27/32] objtool: mcount: Generic location and relocation table types
In reply to Kamalesh BabulalQuoted 117 lines · Kamalesh Babulal
Quoted 19 lines · Matt Helsley
Rather than building the exact ELF section data we need and avoiding libelf's conversion step, use more GElf types and then libelf's
elfxx_xlatetof()functions to convert the mcount locations (GElf_Addr) and associated relocations.This converts
sift_rel_mcount()so that it doesn't use the recordmcount wrapper. The next patch will move it out of the wrapper.Signed-off-by Matt Helsley <[email protected]>--- tools/objtool/recordmcount.c | 44 +++---------- tools/objtool/recordmcount.h | 120 ++++++++++++++--------------------- 2 files changed, 59 insertions(+), 105 deletions(-)tools/objtool/recordmcount.c +0
-0[...]
Quoted 36 lines
tools/objtool/recordmcount.c +8
-21-static uint_t *sift_rel_mcount (uint_t *mlocp, -unsigned const offbase, -Elf_Rel **const mrelpp, +static void sift_rel_mcount (GElf_Addr **mlocpp, +GElf_Sxword *r_offsetp, +void **const mrelpp,const struct section *const rels,unsigned const recsym_index,unsigned long const recval, -unsigned const reltype) +unsigned const reltype, +bool is_rela) { -uint_t *const mloc0 = mlocp; -Elf_Rel *mrelp = *mrelpp; -unsigned int rel_entsize = rels->sh.sh_entsize; -unsigned mcountsym =0 ; +GElf_Rel *mrelp = *mrelpp; +GElf_Rela *mrelap = *mrelpp; +unsigned int mcount_sym_info =0 ;struct reloc *reloc;list_for_each_entry (reloc, &rels->reloc_list, list) { -if (!mcountsym) - mcountsym =get_mcountsym (reloc); - -if (mcountsym ==GELF_R_INFO (reloc->sym->idx, reloc->type) && !is_fake_mcount (reloc)) { -uint_t const addend = -_w (reloc->offset - recval + mcount_adjust); - mrelp->r_offset =_w (offbase - + ((void *)mlocp - (void *)mloc0)); -Elf_r_info (mrelp, recsym_index, reltype); -if (rel_entsize ==sizeof (Elf_Rela)) { - ((Elf_Rela *)mrelp)->r_addend = addend; - *mlocp++ =0 ; - }else tools/objtool/recordmcount.c
Quoted 10 lines
tools/objtool/recordmcount.c +7
-3- *mlocp++ = addend; - - mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp); +unsigned long addend; + +if (!mcount_sym_info) + mcount_sym_info =get_mcount_sym_info (reloc); + +if (mcount_sym_info !=GELF_R_INFO (reloc->sym->idx, reloc->type) ||is_fake_mcount (reloc)) +continue ;Hi Matt,
I was trying out the patch series on ppc64le and found that
__mcount_locand .rela__mcount_loc section pairs do not get generated.# readelf -S fs/proc/cmdline.o|grep mcount #Debugged the cause to get_mcountsym()'s return type. It returns reloc type from
GELF_R_INFO()and expectsElf64_Xworda.k.a unsigned long to be the return type butget_mcountsym()returns unsigned int on 64-bit.On power the
_mcountis of relocation typeR_PPC64_REL24(info0x170000000a), using unsigned int truncates the value to0xaand fails the above check. Using below fix, that convertsmcount_sym_infoto use unsigned long, generates the__mcount_locsection pairs.tools/objtool/mcount.c +3
-3@@ -163,7 +163,7 @@ static int is_mcounted_section_name(char const *const txtname)strcmp (".cpuidle.text" , txtname) ==0 ; } -static unsigned int get_mcount_sym_info (struct reloc *reloc) +static unsigned long get_mcount_sym_info (struct reloc *reloc) {struct symbol *sym = reloc->sym;char const *symname = sym->name;@@ -274,7 +274,7 @@ static int nop_mcount(struct section * const rels, {struct reloc *reloc;struct section *txts =find_section_by_index (lf, rels->sh.sh_info); -unsigned int mcount_sym_info =0 ; +unsigned long mcount_sym_info =0 ;int once =0 ;list_for_each_entry (reloc, &rels->reloc_list, list) {@@ -363,7 +363,7 @@ static void sift_rel_mcount(GElf_Addr **mlocpp, {GElf_Rel *mrelp = *mrelpp;GElf_Rela *mrelap = *mrelpp; -unsigned int mcount_sym_info =0 ; +unsigned long mcount_sym_info =0 ;struct reloc *reloc;list_for_each_entry (reloc, &rels->reloc_list, list) {
Quoted 3 lines
# readelf -S fs/proc/cmdline.o|grep mcount [31] __mcount_loc PROGBITS 0000000000000000 00022f10 [32] .rela__mcount_loc RELA 0000000000000000 00022f20
Fixed for next posting.
I've essentially added this as another patch before it moves into recordmcount.c, gets renamed to get_mcount_sym_info(), etc. I did it this way because it only becomes necessary to change the type before moving the function (and eventually its callers) out of the wrapper.
I feel I should credit you as author or at least co-author of the added patch since it's basically a "backported" version of the changes you suggested. I reviewed the process in submitting-patches.rst and propose the commit message:
objtool: mcount: Extend mcountsym size
Before we can move this function out of the wrapper and into wordsize-independent code we need to explicitly size the type returned from get_mcountsym() to preserve the symbol info.
Is that OK with you or do you have another preference?
Quoted 33 lines
Quoted 8 lines
tools/objtool/recordmcount.c +6
-0+ + addend = reloc->offset - recval + mcount_adjust; +if (is_rela) { + mrelap->r_offset = *r_offsetp; + mrelap->r_info =GELF_R_INFO (recsym_index, reltype); + mrelap->r_addend = addend;tools/objtool/recordmcount.c
Quoted 10 lines
tools/objtool/recordmcount.c +9
-0+ mrelap++; + **mlocpp =0 ; + }else { + mrelp->r_offset = *r_offsetp; + mrelp->r_info =GELF_R_INFO (recsym_index, reltype); + mrelp++; + **mlocpp = addend; } + (*mlocpp)++; + r_offsetp += loc_size;the offsets generated for
rela__mcount_locsection are incorrect:# readelf -rW fs/proc/meminfo.o [...] Relocation section '.rela__mcount_loc' at offset 0x59a48 contains 4 entries: Offset Info Type Symbol's Value Symbol's Name + Addend 0000000000000000 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + c 00000a059c401f38 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + 64 0000000000000000 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + 7c 0000000000000000 0000000600000026 R_PPC64_ADDR64 0000000000000000 .init.text + cchanging the above line to
*r_offsetp+=loc_sizeand initializing r_offset=0 indo_mcount()generates the correct offset:
Quoted 8 lines
# readelf -rW fs/proc/meminfo.o
[...]
Relocation section '.rela__mcount_loc' at offset 0x59a48 contains 4 entries:
Offset Info Type Symbol's Value Symbol's Name + Addend
0000000000000000 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + c
0000000000000008 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + 64
0000000000000010 0000000200000026 R_PPC64_ADDR64 0000000000000000 .text + 7c
0000000000000018 0000000600000026 R_PPC64_ADDR64 0000000000000000 .init.text + cFixed for next posting.
Thank you for testing these out and the fixes!
Cheers,
-Matt Helsley
Re: [RFC][PATCH v4 02/32] objtool: Make recordmcount into mcount subcmd
In reply to Julien ThierryQuoted 24 lines · Julien Thierry
Hi Matt,
Quoted 21 lines · Matt Helsley
Rather than a standalone executable merge recordmcount as a sub command of objtool. This is a small step towards cleaning up recordmcount and eventually sharing ELF code with objtool.
For the initial step all that's required is a bit of
Makefilechanges and invoking the formermain()function fromrecordmcount.cbecause the subcommand code uses similar function arguments as main when dispatching.objtool ignores some object files that tracing does not, specifically those with
OBJECT_FILES_NON_STANDARDMakefilevariables. For this reason we keep therecordmcount_depseparate from theobjtool_dep. When using objtool mcount we can also, like the other objtool invocations, just depend on the binary rather than the source the binary is built from.Subsequent patches will gradually convert recordmcount to use more and more of libelf/objtool's ELF accessor code. This will both clean up recordmcount to be more easily readable and remove recordmcount's crude accessor wrapping code.
Signed-off-by Matt Helsley <[email protected]>---
...
Quoted 5 lines
Quoted 5 lines
kernel/trace/Kconfig +0
-0@@ -59,7 +59,7 @@ config HAVE_NOP_MCOUNT
Quoted 8 lines
kernel/trace/Kconfig config HAVE_NOP_MCOUNT · line 62
Quoted 5 lines
kernel/trace/Kconfig +1
-1config HAVE_C_RECORDMCOUNT bool help - C version of recordmcount available? + C version of objtool mcount available?The "C version" doesn't make much sense here. "Objtool mcount available?" or "mcount subcommand of objtool available?" perhaps?
Agreed, "C version" is nonsense at this point.
Looking at the other HAVE_* help messages in that Kconfig suggests:
Arch supports objtool mcount subcommand
So I've changed it to that.
Quoted 51 lines
Quoted 51 lines
tools/objtool/Makefile +30
-6@@ -31,12 +31,6 @@ OBJTOOL_IN := $(OBJTOOL)-in.o LIBELF_FLAGS := $(shell pkg-config libelf --cflags 2>/dev/null) LIBELF_LIBS := $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf) -RECORDMCOUNT := $(OUTPUT)recordmcount -RECORDMCOUNT_IN := $(RECORDMCOUNT)-in.o -ifeq ($(BUILD_C_RECORDMCOUNT),y) -all: $(RECORDMCOUNT) -endif - all: $(OBJTOOL) INCLUDES := -I$(srctree)/tools/include \@@ -55,13 +49,47 @@ AWK = awk SUBCMD_CHECK := n SUBCMD_ORC := n +SUBCMD_MCOUNT := n ifeq ($(SRCARCH),x86) SUBCMD_CHECK := y SUBCMD_ORC := y + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),arm) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),arm64) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),ia64) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),mips) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),powerpc) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),s390) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),sh) + SUBCMD_MCOUNT := y
Quoted 8 lines
tools/objtool/Makefile AWK = awk · line 89
Quoted 4 lines
tools/objtool/Makefile +4
-0+endif + +ifeq ($(SRCARCH),sparc) + SUBCMD_MCOUNT := yIs there some arch for which MCOUNT is not supported? If not you could just have MCOUNT default to 'y' and avoid adding all those tests (or maybe reduce the numbers and set to 'n' only for arches not supporting it).
Yes, there are some which it does not support. For those architectures we keep recordmcount.pl around.
It occured to me that with your suggestion to use more CONFIG_ variables we could eliminate this pattern and replace it with these pseudo-patches:
+++ b/kernel/trace/Kconfig
+config OBJTOOL_SUBCMD_MCOUNT
+ bool
+ depends on HAVE_C_RECORDMCOUNT
+ select OBJTOOL_SUBCMDS
+ help
+ Record mcount call locations using objtool
and then change the Makefiles to use the CONFIG_ variables rather than have one ifeq block per arch:
+++ b/tools/objtool/Makefile
+SUBCMD_MCOUNT := $(CONFIG_OBJTOOL_SUBCMD_MCOUNT)
Does this seem like a good use of CONFIG_ variables or is it going too far?
I haven't changed to this pattern just yet -- I'm hoping you and Josh or Peter might weigh in with your preferences.
Quoted 4 lines
Quoted 4 lines
tools/objtool/Makefile +1
-1endif -export SUBCMD_CHECK SUBCMD_ORC +export SUBCMD_CHECK SUBCMD_ORC SUBCMD_MCOUNT
...
Quoted 10 lines
tools/objtool/builtin.h extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, · line 14
Quoted 8 lines
tools/objtool/builtin.h +1
-0@@ -12,5 +12,7 @@ extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats,extern int cmd_check (int argc,const char **argv);extern int cmd_orc (int argc,const char **argv); +extern bool is_cmd_mcount_available (void );This appears to be unused.
Indeed, removed.
Thanks!
Cheers,
-Matt Helsley
Steven Rostedt
maintainerRe: [RFC][PATCH v4 02/32] objtool: Make recordmcount into mcount subcmd
Quoted 22 lines · On Tue, 9 Jun 2020 11:39:51 -0700 Matt Helsley <[email protected]> wrote:
Quoted 6 lines
tools/objtool/Makefile AWK = awk · line 89
Quoted 2 lines
tools/objtool/Makefile +2
-0+ifeq ($(SRCARCH),sparc) + SUBCMD_MCOUNT := yIs there some arch for which MCOUNT is not supported? If not you could just have MCOUNT default to 'y' and avoid adding all those tests (or maybe reduce the numbers and set to 'n' only for arches not supporting it).
Yes, there are some which it does not support. For those architectures we keep recordmcount.pl around.
It occured to me that with your suggestion to use more
CONFIG_variables we could eliminate this pattern and replace it with these pseudo-patches:+++ b/kernel/trace/Kconfig
(quoted diff) +6
-0+config OBJTOOL_SUBCMD_MCOUNT + bool + depends on HAVE_C_RECORDMCOUNT + select OBJTOOL_SUBCMDS + help + Record mcount call locations using objtool
Quoted 6 lines
and then change the Makefiles to use the
CONFIG_variables rather than have one ifeq block per arch:+++ b/tools/objtool/Makefile
+SUBCMD_MCOUNT := $(CONFIG_OBJTOOL_SUBCMD_MCOUNT)
If you can make this work, this is definitely the way to go.
-- Steve
Quoted 6 lines
Does this seem like a good use of
CONFIG_variables or is it going too far?I haven't changed to this pattern just yet -- I'm hoping you and Josh or Peter might weigh in with your preferences.
Re: [RFC][PATCH v4 02/32] objtool: Make recordmcount into mcount subcmd
Quoted 25 lines · Steven Rostedt
Quoted 22 lines · On Tue, 9 Jun 2020 11:39:51 -0700 Matt Helsley <[email protected]> wrote:
Quoted 6 lines
tools/objtool/Makefile AWK = awk · line 89
Quoted 2 lines
tools/objtool/Makefile +2
-0+ifeq ($(SRCARCH),sparc) + SUBCMD_MCOUNT := yIs there some arch for which MCOUNT is not supported? If not you could just have MCOUNT default to 'y' and avoid adding all those tests (or maybe reduce the numbers and set to 'n' only for arches not supporting it).
Yes, there are some which it does not support. For those architectures we keep recordmcount.pl around.
It occured to me that with your suggestion to use more
CONFIG_variables we could eliminate this pattern and replace it with these pseudo-patches:+++ b/kernel/trace/Kconfig
(quoted diff) +6
-0+config OBJTOOL_SUBCMD_MCOUNT + bool + depends on HAVE_C_RECORDMCOUNT + select OBJTOOL_SUBCMDS + help + Record mcount call locations using objtool
Quoted 8 lines
Quoted 6 lines
and then change the Makefiles to use the
CONFIG_variables rather than have one ifeq block per arch:+++ b/tools/objtool/Makefile
+SUBCMD_MCOUNT := $(CONFIG_OBJTOOL_SUBCMD_MCOUNT)
If you can make this work, this is definitely the way to go.
I think I can so I'll give it a go!
Cheers,
-Matt Helsley
Re: [RFC][PATCH v4 02/32] objtool: Make recordmcount into mcount subcmd
In reply to Matt HelsleyQuoted 40 lines · Matt Helsley
Quoted 24 lines · Julien Thierry
Hi Matt,
Quoted 21 lines · Matt Helsley
Rather than a standalone executable merge recordmcount as a sub command of objtool. This is a small step towards cleaning up recordmcount and eventually sharing ELF code with objtool.
For the initial step all that's required is a bit of
Makefilechanges and invoking the formermain()function fromrecordmcount.cbecause the subcommand code uses similar function arguments as main when dispatching.objtool ignores some object files that tracing does not, specifically those with
OBJECT_FILES_NON_STANDARDMakefilevariables. For this reason we keep therecordmcount_depseparate from theobjtool_dep. When using objtool mcount we can also, like the other objtool invocations, just depend on the binary rather than the source the binary is built from.Subsequent patches will gradually convert recordmcount to use more and more of libelf/objtool's ELF accessor code. This will both clean up recordmcount to be more easily readable and remove recordmcount's crude accessor wrapping code.
Signed-off-by Matt Helsley <[email protected]>---
...
Quoted 5 lines
Quoted 5 lines
kernel/trace/Kconfig +0
-0@@ -59,7 +59,7 @@ config HAVE_NOP_MCOUNTQuoted 8 lines
kernel/trace/Kconfig config HAVE_NOP_MCOUNT · line 62
Quoted 5 lines
kernel/trace/Kconfig +1
-1config HAVE_C_RECORDMCOUNT bool help - C version of recordmcount available? + C version of objtool mcount available?The "C version" doesn't make much sense here. "Objtool mcount available?" or "mcount subcommand of objtool available?" perhaps?
Quoted 8 lines
Agreed, "C version" is nonsense at this point.
Looking at the other HAVE_* help messages in that
Kconfigsuggests:Arch supports objtool mcount subcommand
So I've changed it to that.
Yes, that seems good.
Quoted 78 lines
Quoted 51 lines
Quoted 51 lines
tools/objtool/Makefile +30
-6@@ -31,12 +31,6 @@ OBJTOOL_IN := $(OBJTOOL)-in.o LIBELF_FLAGS := $(shell pkg-config libelf --cflags 2>/dev/null) LIBELF_LIBS := $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf) -RECORDMCOUNT := $(OUTPUT)recordmcount -RECORDMCOUNT_IN := $(RECORDMCOUNT)-in.o -ifeq ($(BUILD_C_RECORDMCOUNT),y) -all: $(RECORDMCOUNT) -endif - all: $(OBJTOOL) INCLUDES := -I$(srctree)/tools/include \@@ -55,13 +49,47 @@ AWK = awk SUBCMD_CHECK := n SUBCMD_ORC := n +SUBCMD_MCOUNT := n ifeq ($(SRCARCH),x86) SUBCMD_CHECK := y SUBCMD_ORC := y + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),arm) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),arm64) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),ia64) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),mips) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),powerpc) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),s390) + SUBCMD_MCOUNT := y +endif + +ifeq ($(SRCARCH),sh) + SUBCMD_MCOUNT := yQuoted 8 lines
tools/objtool/Makefile AWK = awk · line 89
Quoted 4 lines
tools/objtool/Makefile +4
-0+endif + +ifeq ($(SRCARCH),sparc) + SUBCMD_MCOUNT := yIs there some arch for which MCOUNT is not supported? If not you could just have MCOUNT default to 'y' and avoid adding all those tests (or maybe reduce the numbers and set to 'n' only for arches not supporting it).
Yes, there are some which it does not support. For those architectures we keep recordmcount.pl around.
It occured to me that with your suggestion to use more
CONFIG_variables we could eliminate this pattern and replace it with these pseudo-patches:+++ b/kernel/trace/Kconfig
(quoted diff) +6
-0+config OBJTOOL_SUBCMD_MCOUNT + bool + depends on HAVE_C_RECORDMCOUNT + select OBJTOOL_SUBCMDS + help + Record mcount call locations using objtooland then change the Makefiles to use the
CONFIG_variables rather than have one ifeq block per arch:
Quoted 7 lines
+++ b/tools/objtool/Makefile
+SUBCMD_MCOUNT := $(CONFIG_OBJTOOL_SUBCMD_MCOUNT)
Does this seem like a good use of
CONFIG_variables or is it going too far?
Definitely seems like a good idea to me! Will be a nice improvement.
Cheers,
Signature · 1 line
Julien Thierry
Re: [RFC][PATCH v4 01/32] objtool: Prepare to merge recordmcount
In reply to Matt HelsleyQuoted 33 lines · Matt Helsley
Quoted 8 lines · Julien Thierry
Hi Matt,
Quoted 5 lines · Matt Helsley
Move recordmcount into the objtool directory. We keep this step separate so changes which turn recordmcount into a subcommand of objtool don't get obscured.
Signed-off-by Matt Helsley <[email protected]><snip>
Quoted 21 lines
Quoted 13 lines
Makefile +1
-0@@ -844,6 +844,7 @@ ifdef CONFIG_DYNAMIC_FTRACE ifdef CONFIG_HAVE_C_RECORDMCOUNT BUILD_C_RECORDMCOUNT := y export BUILD_C_RECORDMCOUNT + objtool_target := tools/objtool FORCE endif endif endifMakefile endif · line 1027
Quoted 4 lines
Makefile +2
-0@@ -1023,10 +1024,10 @@ endif export mod_sign_cmd HOST_LIBELF_LIBS = $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf) +has_libelf := $(call try-run,\ + echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0)Maybe there could be some build dependency, e.g.
CONFIG_OBJTOOL_SUBCMDSthat sets the "objtool_target" and "has_libelf" when selected.
Quoted 10 lines
Quoted 2 lines
Then the
CONFIG_UNWINDER_ORC,RECORD_MCOUNTandSTACK_VALIDATIONwould just had to select that config option.That might save a good amount of control flow in the Makefiles.
We could take it one step further and have specific CONFIG_OBJTOOL_<subcmd> which might help us remove the per-architecture control-flow in the multi-arch subcmd support found in
tools/objtool/Makefile.Quoted 1 line
What do folks think of that -- too far?
I wasn't completely sure I understood before I saw your reply on the next patch. I don't think it's too far, it's cleaner! The current way was good enough to deal with only two x86 specific objtool subcommands.
Since you're adding another one and it is likely that more will be added in the future, I think it's worth having something a bit more structured :) .
Quoted 24 lines
Quoted 24 lines
Quoted 10 lines
Makefile +0
-2ifdef CONFIG_STACK_VALIDATION - has_libelf := $(call try-run,\ - echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0) ifeq ($(has_libelf),1) objtool_target := tools/objtool FORCE else@@ -1163,13 +1164,15 @@ uapi-asm-generic: PHONY += prepare-objtool prepare-objtool: $(objtool_target)Makefile uapi-asm-generic: · line 1172
Quoted 10 lines
Makefile +4
-3-ifeq ($(SKIP_STACK_VALIDATION),1) -ifdef CONFIG_UNWINDER_ORC +ifneq ($(has_libelf),1) + ifdef CONFIG_UNWINDER_ORC @echo "error: Cannot generate ORC metadata for CONFIG_UNWINDER_ORC=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2 @false -else + else + ifeq ($(SKIP_STACK_VALIDATION),1) @echo "warning: Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2I think this would be more readable without the else branch:
Quoted 6 lines
Quoted 3 lines
ifneq ($(has_libelf),1)
ifdef <some objtool command config>
<warn about unavailability>Note: error not warn
Good point. But since those are errors, you don't need the "else" :)
Quoted 11 lines
Quoted 6 lines
endif ifdef <another objtool command config> <warn ...> endif <...> endifI think the next patch, which makes recordmcount a subcmd, makes it a little clearer what the pattern is because it adds another ifdef block in the way you suggest.
Quoted 6 lines
As for the else around the
SKIP_STACK_VALIDATIONcheck -- it is special in a couple ways -- at least as best I can tell.It's not a CONFIG_* -- it actually breaks the normal pattern with CONFIG_* in that..
Yes but $(SKIP_STACK_VALIDATION) == 1 is actually just CONFIG_STACK_VALIDATION && ($(has_libelf) != 1). And since you are already in the ifneq ($(has_libelf),1) branch, checking $(SKIP_STACK_VALIDATION) == 1 is the same as CONFIG_STACK_VALIDATION being defined.
Quoted 4 lines
It's about a judgement call that it's OK to merely warn and skip the stack validation rather than produce an error. The other, CONFIG_* blocks produce errors.
To me this is minor, we could also imagine another command CONFIG_ performing another action than warning or error when libelf is not available.
Anyway, this was just a suggestion, I don't want to insist to much on this.
Cheers,
Signature · 1 line
Julien Thierry
Re: [RFC][PATCH v4 27/32] objtool: mcount: Generic location and relocation table types
In reply to Matt HelsleyOn 6/9/20 11:42 PM, Matt Helsley wrote:
[...]
Quoted 69 lines
Quoted 47 lines
Hi Matt,
I was trying out the patch series on ppc64le and found that
__mcount_locand .rela__mcount_loc section pairs do not get generated.# readelf -S fs/proc/cmdline.o|grep mcount #Debugged the cause to get_mcountsym()'s return type. It returns reloc type from
GELF_R_INFO()and expectsElf64_Xworda.k.a unsigned long to be the return type butget_mcountsym()returns unsigned int on 64-bit.On power the
_mcountis of relocation typeR_PPC64_REL24(info0x170000000a), using unsigned int truncates the value to0xaand fails the above check. Using below fix, that convertsmcount_sym_infoto use unsigned long, generates the__mcount_locsection pairs.tools/objtool/mcount.c +3
-3@@ -163,7 +163,7 @@ static int is_mcounted_section_name(char const *const txtname)strcmp (".cpuidle.text" , txtname) ==0 ; } -static unsigned int get_mcount_sym_info (struct reloc *reloc) +static unsigned long get_mcount_sym_info (struct reloc *reloc) {struct symbol *sym = reloc->sym;char const *symname = sym->name;@@ -274,7 +274,7 @@ static int nop_mcount(struct section * const rels, {struct reloc *reloc;struct section *txts =find_section_by_index (lf, rels->sh.sh_info); -unsigned int mcount_sym_info =0 ; +unsigned long mcount_sym_info =0 ;int once =0 ;list_for_each_entry (reloc, &rels->reloc_list, list) {@@ -363,7 +363,7 @@ static void sift_rel_mcount(GElf_Addr **mlocpp, {GElf_Rel *mrelp = *mrelpp;GElf_Rela *mrelap = *mrelpp; -unsigned int mcount_sym_info =0 ; +unsigned long mcount_sym_info =0 ;struct reloc *reloc;list_for_each_entry (reloc, &rels->reloc_list, list) {Quoted 3 lines
# readelf -S fs/proc/cmdline.o|grep mcount [31] __mcount_loc PROGBITS 0000000000000000 00022f10 [32] .rela__mcount_loc RELA 0000000000000000 00022f20Fixed for next posting.
I've essentially added this as another patch before it moves into
recordmcount.c, gets renamed toget_mcount_sym_info(), etc. I did it this way because it only becomes necessary to change the type before moving the function (and eventually its callers) out of the wrapper.I feel I should credit you as author or at least co-author of the added patch since it's basically a "backported" version of the changes you suggested. I reviewed the process in
submitting-patches.rstand propose the commit message:objtool: mcount: Extend mcountsym size
Before we can move this function out of the wrapper and into wordsize-independent code we need to explicitly size the type returned from
get_mcountsym()to preserve the symbol info.
Quoted 5 lines
Reported-by Kamalesh Babulal <[email protected]>Signed-off-by Kamalesh Babulal <[email protected]>Signed-off-by Matt Helsley <[email protected]>Is that OK with you or do you have another preference?
Thanks, it works for me.
Signature · 1 line
Kamalesh
Peter Zijlstra
maintainerRe: [RFC][PATCH v4 18/32] objtool: mcount: Move nop_mcount()
In reply to Matt HelsleyQuoted 5 lines · Matt Helsley
tools/objtool/recordmcount.c +5
-0+static int nop_mcount (struct section *const rels, +const char *const txtname) +{ +struct reloc *reloc; +struct section *txts =find_section_by_index (lf, rels->sh.sh_info);
tools/objtool/recordmcount.c static int find_section_sym_index(unsigned const txtndx, · line 420
Quoted 10 lines
tools/objtool/recordmcount.c +10
-0+unsigned mcountsym =0 ; +int once =0 ; + +list_for_each_entry (reloc, &rels->reloc_list, list) { +int ret =-1 ; + +if (!mcountsym) + mcountsym =get_mcountsym (reloc); + +if (mcountsym ==GELF_R_INFO (reloc->sym->idx, reloc->type) && !is_fake_mcount (reloc)) {
This makes no sense to me; why not have mcountsym be a 'struct symbol *' and have get_mcountsym() return one of those.
if (reloc->sym == mcountsym && ... )
is much nicer, no?
Quoted 12 lines
tools/objtool/recordmcount.c +12
-0+if (make_nop) { + ret =make_nop (txts, reloc->offset); +if (ret <0 ) +return -1 ; + } +if (warn_on_notrace_sect && !once) { +printf ("Section %s has mcount callers being ignored\n" , + txtname); + once =1 ; +/* just warn? */ +if (!make_nop) +return 0 ;
tools/objtool/recordmcount.c static int find_section_sym_index(unsigned const txtndx, · line 442
Quoted 10 lines
tools/objtool/recordmcount.c +10
-0+ } + } + +/* +* If we successfully removed the mcount, mark the relocation +* as a nop (don't do anything with it). +*/ +if (!ret) { + reloc->type = rel_type_nop; + rels->changed = true;
I have an elf_write_rela(), I'll make sure to Cc you.
Quoted 4 lines
tools/objtool/recordmcount.c +4
-0+ } + } +return 0 ; +}
Peter Zijlstra
maintainerRe: [RFC][PATCH v4 18/32] objtool: mcount: Move nop_mcount()
Quoted 20 lines · Peter Zijlstra
Quoted 5 lines · Matt Helsley
(quoted diff) +5
-0+static int nop_mcount(struct section * const rels, + const char *const txtname) +{ + struct reloc *reloc; + struct section *txts = find_section_by_index(lf, rels->sh.sh_info);Review
Quoted 10 lines
(quoted diff) +10
-0+ unsigned mcountsym = 0; + int once = 0; + + list_for_each_entry(reloc, &rels->reloc_list, list) { + int ret = -1; + + if (!mcountsym) + mcountsym = get_mcountsym(reloc); + + if (mcountsym == GELF_R_INFO(reloc->sym->idx, reloc->type) && !is_fake_mcount(reloc)) {This makes no sense to me; why not have mcountsym be a 'struct symbol *' and have
get_mcountsym()return one of those.
Quoted 3 lines
if (reloc->sym == mcountsym && ... )is much nicer, no?
On top of that, I suppose we can do something like the below.
Then you can simply write:
if (reloc->sym->class == SYM_MCOUNT && ..)
---
kernel/locking/Makefile +1 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 # Any varying coverage in these files is non-deterministic # and is generally not a function of system call inputs. -KCOV_INSTRUMENT := n +# KCOV_INSTRUMENT := n obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o
tools/objtool/elf.c +24 -0
@@ -341,6 +341,24 @@ static int read_sections(struct elf *elf)return 0 ; } +static bool is_mcount_symbol (const char *name) +{ +if (name[0 ] =='.' ) + name++; + +if (name[0 ] =='_' ) + name++; + +return !strcmp (name,"mcount" ,6 ) || + !strcmp (name,"_fentry__" ) || + !strcmp (name,"_gnu_mcount_nc" ); +} + +static bool is_kcov_symbol (const char *name) +{ +return !strncmp (name,"__sanitizer_cov_" ,16 ); +} +static int read_symbols (struct elf *elf) {struct section *symtab, *symtab_shndx, *sec;
@@ -410,6 +428,12 @@ static int read_symbols(struct elf *elf) }else sym->sec =find_section_by_index (elf,0 ); + +if (is_mcount_symbol (sym->name)) + sym->class =SYM_MCOUNT ; +else if (is_kcov_symbol (sym->name)) + sym->class =SYM_KCOV ; + sym->offset = sym->sym.st_value; sym->len = sym->sym.st_size;
tools/objtool/elf.h +7 -0
@@ -42,6 +42,12 @@ struct section {bool changed, text, rodata, noinstr; }; +enum symbol_class { +SYM_REGULAR =0 , +SYM_MCOUNT , +SYM_KCOV , +}; +struct symbol {struct list_head list;struct rb_node node;
@@ -55,6 +61,7 @@ struct symbol {unsigned long offset;unsigned int len;struct symbol *pfunc, *cfunc, *alias; +enum symbol_class class;bool uaccess_safe; };
Re: [RFC][PATCH v4 18/32] objtool: mcount: Move nop_mcount()
In reply to Peter ZijlstraQuoted 20 lines · Peter Zijlstra
Quoted 5 lines · Matt Helsley
tools/objtool/recordmcount.c +5
-0+static int nop_mcount (struct section *const rels, +const char *const txtname) +{ +struct reloc *reloc; +struct section *txts =find_section_by_index (lf, rels->sh.sh_info);tools/objtool/recordmcount.c static int find_section_sym_index(unsigned const txtndx, · line 420
Quoted 10 lines
tools/objtool/recordmcount.c +10
-0+unsigned mcountsym =0 ; +int once =0 ; + +list_for_each_entry (reloc, &rels->reloc_list, list) { +int ret =-1 ; + +if (!mcountsym) + mcountsym =get_mcountsym (reloc); + +if (mcountsym ==GELF_R_INFO (reloc->sym->idx, reloc->type) && !is_fake_mcount (reloc)) {This makes no sense to me; why not have mcountsym be a 'struct symbol *' and have
get_mcountsym()return one of those.
Quoted 3 lines
if (reloc->sym == mcountsym && ... )is much nicer, no?
Indeed! I'll change it from returning an unsigned long to struct symbol * before I move it out of the wrapper code.
Quoted 17 lines
Quoted 17 lines
tools/objtool/recordmcount.c +16
-0+if (make_nop) { + ret =make_nop (txts, reloc->offset); +if (ret <0 ) +return -1 ; + } +if (warn_on_notrace_sect && !once) { +printf ("Section %s has mcount callers being ignored\n" , + txtname); + once =1 ; +/* just warn? */ +if (!make_nop) +return 0 ; + } + } + +/*
Quoted 8 lines
tools/objtool/recordmcount.c static int find_section_sym_index(unsigned const txtndx, · line 442
Quoted 6 lines
tools/objtool/recordmcount.c +6
-0+* If we successfully removed the mcount, mark the relocation +* as a nop (don't do anything with it). +*/ +if (!ret) { + reloc->type = rel_type_nop; + rels->changed = true;I have an
elf_write_rela(), I'll make sure to Cc you.
Thanks! I might also make use of your patch to rewrite instructions. We need a way to turn certain prologue instructions into nops. Would it be more widely useful to move that functionality out of mcount and into the objtool ELF/per-arch code or do you think it's better inside the mcount subcommand code?
Cheers,
-Matt
Re: [RFC][PATCH v4 18/32] objtool: mcount: Move nop_mcount()
In reply to Peter ZijlstraQuoted 21 lines · Peter Zijlstra
Quoted 20 lines · Peter Zijlstra
Quoted 5 lines · Matt Helsley
(quoted diff) +5
-0+static int nop_mcount(struct section * const rels, + const char *const txtname) +{ + struct reloc *reloc; + struct section *txts = find_section_by_index(lf, rels->sh.sh_info);Review
Quoted 10 lines
(quoted diff) +10
-0+ unsigned mcountsym = 0; + int once = 0; + + list_for_each_entry(reloc, &rels->reloc_list, list) { + int ret = -1; + + if (!mcountsym) + mcountsym = get_mcountsym(reloc); + + if (mcountsym == GELF_R_INFO(reloc->sym->idx, reloc->type) && !is_fake_mcount(reloc)) {This makes no sense to me; why not have mcountsym be a 'struct symbol *' and have
get_mcountsym()return one of those.
Quoted 3 lines
Quoted 3 lines
if (reloc->sym == mcountsym && ... )is much nicer, no?
(this is already incorporated in my unposted revisions but...)
Quoted 6 lines
On top of that, I suppose we can do something like the below.
Then you can simply write:
if (reloc->sym->class == SYM_MCOUNT && ..)
This looks like a good way to move towards a "single pass" through the ELF data for mcount.
What order do you want to see this patch go in? Before this series (i.e. perhaps just a kcov SYM_ class to start)? Early or late in this series? After?
Right now I'm thinking of putting this on the end of my series because I'm focusing on converting recordmcount in the series and this isn't strictly necessary but is definitely nicer.
Quoted 23 lines
---
kernel/locking/Makefile +1
-1@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 # Any varying coverage in these files is non-deterministic # and is generally not a function of system call inputs. -KCOV_INSTRUMENT := n +# KCOV_INSTRUMENT := n obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.otools/objtool/elf.c +0
-0@@ -341,6 +341,24 @@ static int read_sections(struct elf *elf)return 0 ; }
tools/objtool/elf.c static int read_sections(struct elf *elf) · line 352
Quoted 10 lines
tools/objtool/elf.c +9
-0+static bool is_mcount_symbol (const char *name) +{ +if (name[0 ] =='.' ) + name++; + +if (name[0 ] =='_' ) + name++; + + return !strcmp(name, "mcount", 6) ||
Looks like you intended this to be a strncmp() but I don't see a reason to use strncmp(). Am I missing something?
tools/objtool/elf.c static int read_sections(struct elf *elf) · line 355
Quoted 3 lines
tools/objtool/elf.c +3
-0+ !strcmp (name,"_fentry__" ) || + !strcmp (name,"_gnu_mcount_nc" ); +}
This mashes all of the arch-specific mcount name checks together. I don't see a problem with that because I doubt there will be a collision with other functions. Just to be careful I looked through the Clang and GCC sources, though I only dug through the history of Clang's output -- GCC's history with respect to mcount symbol names across architectures is much harder to trace so I only looked at the current sources.
<snip> (the rest looks good)
Cheers,
-Matt Helsley
Peter Zijlstra
maintainerRe: [RFC][PATCH v4 18/32] objtool: mcount: Move nop_mcount()
Quoted 11 lines · Matt Helsley
Quoted 5 lines · Peter Zijlstra
On top of that, I suppose we can do something like the below.
Then you can simply write:
if (reloc->sym->class == SYM_MCOUNT && ..)This looks like a good way to move towards a "single pass" through the ELF data for mcount.
Quoted 6 lines
What order do you want to see this patch go in? Before this series (i.e. perhaps just a kcov
SYM_class to start)? Early or late in this series? After?Right now I'm thinking of putting this on the end of my series because I'm focusing on converting recordmcount in the series and this isn't strictly necessary but is definitely nicer.
No particular thoughts about where, so at the end would be fine.
Quoted 23 lines
Quoted 23 lines
---
kernel/locking/Makefile +1
-1@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 # Any varying coverage in these files is non-deterministic # and is generally not a function of system call inputs. -KCOV_INSTRUMENT := n +# KCOV_INSTRUMENT := n obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.otools/objtool/elf.c +0
-0@@ -341,6 +341,24 @@ static int read_sections(struct elf *elf)return 0 ; }
Quoted 12 lines
tools/objtool/elf.c static int read_sections(struct elf *elf) · line 352
Quoted 9 lines
tools/objtool/elf.c +9
-0+static bool is_mcount_symbol (const char *name) +{ +if (name[0 ] =='.' ) + name++; + +if (name[0 ] =='_' ) + name++; + + return !strcmp(name, "mcount", 6) ||Looks like you intended this to be a
strncmp()but I don't see a reason to usestrncmp(). Am I missing something?
typing hard :-)
Quoted 7 lines
tools/objtool/elf.c static int read_sections(struct elf *elf) · line 355
Quoted 3 lines
tools/objtool/elf.c +3
-0+ !strcmp (name,"_fentry__" ) || + !strcmp (name,"_gnu_mcount_nc" ); +}This mashes all of the arch-specific mcount name checks together. I don't see a problem with that because I doubt there will be a collision with other functions.
This, I assumed it would just work.
Quoted 4 lines
Just to be careful I looked through the Clang and
GCC sources, though I only dug through the history of Clang's output -- GCC's history with respect to mcount symbol names across architectures is much harder to trace so I only looked at the current sources.
Fair enough; thanks for the due-dilligence.