symbols: assorted adjustments
cover symbols: assorted adjustments
The main goal was what patch 5 does, but on the way there various other things became noticeable, and some preparation was necessary, too.
1: add minimal self-test
2: don't over-align generated data
3: sanitize a few variable's types
4: split symbols_num_syms
5: arrange to know where functions end
6: centralize and re-arrange $(all_symbols) calculation
Jan
PATCH 1/6 symbols: add minimal self-test
... before making changes to the involved logic.
---
With this FAST_SYMBOL_LOOKUP may make sense to permit enabling even when LIVEPATCH=n. Thoughts? (In this case "symbols: centralize and re- arrange $(all_symbols) calculation" would want pulling ahead.)
xen/common/symbols.c +35 -0
@@ -260,6 +260,41 @@ unsigned long symbols_lookup_by_name(conreturn 0 ; } +#ifdef CONFIG_SELF_TESTS + +static void __init test_lookup (unsigned long addr,const char *expected) +{ +char buf[KSYM_NAME_LEN +1 ]; +const char *name, *symname; +unsigned long size, offs; + + name =symbols_lookup (addr, &size, &offs, buf); +if ( !name ) +panic ("%s: address not found\n" , expected); +if ( offs ) +panic ("%s: non-zero offset (%#lx) unexpected\n" , expected, offs); + +/* Cope with static symbols, where varying file names/paths may be used. */ + symname =strchr (name,'#' ); + symname = symname ? symname +1 : name; +if (strcmp (symname, expected) ) +panic ("%s: unexpected symbol name: '%s'\n" , expected, symname); + + offs =symbols_lookup_by_name (name); +if ( offs != addr ) +panic ("%s: address %#lx unexpected; wanted %#lx\n" , + expected, offs, addr); +} + +static void __init __constructor test_symbols(void) +{ +/* Be sure to only try this for cf_check functions. */ +test_lookup ((unsigned long )dump_execstate,"dump_execstate" ); +test_lookup ((unsigned long )test_symbols, __func__); +} + +#endif /* CONFIG_SELF_TESTS */ +/* * Local variables: * mode: C
PATCH 2/6 symbols: don't over-align generated data
In reply to Jan Beulichx86 is one of the few architectures where .align has the same meaning as .balign; most other architectures (Arm, PPC, and RISC-V in particular) give it the same meaning as .p2align. Aligning every one of these item to 256 bytes (on all 64-bit architectures except x86-64) is clearly too much.
---
Even uniformly aligning to 4 (x86, Arm32) or 8 bytes is too much imo, when some of the items require only 1- or 2-byte alignment.
Is there a reason only x86 defines SYMBOLS_ORIGIN, to halve the address table in size? (Arm32 and other possible 32-bit ports of course have no need for doing so, but for 64-bit ones that can make quite a bit of a difference.)
xen/tools/symbols.c +2 -2
@@ -304,10 +304,10 @@ static void write_src(void)printf ("#include <xen/config.h>\n" );printf ("#if BITS_PER_LONG == 64 && !defined(SYMBOLS_ORIGIN)\n" );printf ("#define PTR .quad\n" ); -printf ("#define ALGN .align 8\n" ); +printf ("#define ALGN .balign 8\n" );printf ("#else\n" );printf ("#define PTR .long\n" ); -printf ("#define ALGN .align 4\n" ); +printf ("#define ALGN .balign 4\n" );printf ("#endif\n" );printf ("\t.section .rodata, \"a\"\n" );
PATCH 3/6 symbols: sanitize a few variable's types
In reply to Jan BeulichParameter and return types of symbols_expand_symbol() make clear that xensyms_read()'s next_offset doesn't need to be 64-bit.
xensyms_read()'s first parameter type makes clear that the function's next_symbols doesn't need to be 64-bit.
symbols_num_syms'es type makes clear that iteration locals in symbols_lookup() don't need to be unsigned long (i.e. 64-bit on 64-bit architectures).
xen/common/symbols.c +2 -2
@@ -108,7 +108,7 @@ const char *symbols_lookup(unsigned longunsigned long *offset, char *namebuf) { - unsigned long i, low, high, mid; + unsigned int i, low, high, mid;unsigned long symbol_end =0 ;const struct virtual_region *region;
@@ -179,7 +179,7 @@ int xensyms_read(uint32_t *symnum, char* from previous read. This can help us avoid the extra call to * get_symbol_offset(). */ -static uint64_t next_symbol, next_offset; +static unsigned int next_symbol, next_offset;static DEFINE_SPINLOCK (symbols_mutex);if ( *symnum > symbols_num_syms )
PATCH 4/6 symbols: split symbols_num_syms
In reply to Jan BeulichIn preparation for inserting address entries into symbols_addresses[] / symbols_offsets[] without enlarging symbols_sorted_offsets[], split symbols_num_syms into symbols_num_addrs (counting entries in the former plus symbols_names[] as well as, less directly, symbols_markers[]) and symbols_num_names (counting entries in the latter).
While doing the adjustment move declarations to a new private symbols.h, to be used by both symbols.c and symbols-dummy.c. Replace u8/u16 while doing so.
xen/common/symbols.c +6 -22
@@ -10,7 +10,6 @@* compression (see tools/symbols.c for a more complete description) */ -#include <xen/symbols.h> #include <xen/kernel.h> #include <xen/init.h> #include <xen/lib.h>
@@ -21,22 +20,7 @@#include <xen/guest_access.h> #include <xen/errno.h> -#ifdef SYMBOLS_ORIGIN -extern const unsigned int symbols_offsets[]; -#define symbols_address (n) (SYMBOLS_ORIGIN + symbols_offsets[n]) -#else -extern const unsigned long symbols_addresses[]; -#define symbols_address (n) symbols_addresses[n] -#endif -extern const unsigned int symbols_num_syms; -extern const u8 symbols_names[]; - -extern const struct symbol_offset symbols_sorted_offsets[]; - -extern const u8 symbols_token_table[]; -extern const u16 symbols_token_index[]; - -extern const unsigned int symbols_markers[]; +#include "symbols.h" /* expand a compressed symbol data into the resulting uncompressed string, given the offset to where the symbol is in the compressed stream */
@@ -124,7 +108,7 @@ const char *symbols_lookup(unsigned long/* do a binary search on the sorted symbols_addresses array */ low =0 ; - high = symbols_num_syms; + high = symbols_num_addrs;while (high-low >1 ) { mid = (low + high) /2 ;
@@ -141,7 +125,7 @@ const char *symbols_lookup(unsigned longsymbols_expand_symbol (get_symbol_offset (low), namebuf);/* Search for next non-aliased symbol */ -for (i = low +1 ; i < symbols_num_syms; i++) { +for (i = low +1 ; i < symbols_num_addrs; i++) {if (symbols_address (i) >symbols_address (low)) { symbol_end =symbols_address (i);break ;
@@ -182,9 +166,9 @@ int xensyms_read(uint32_t *symnum, charstatic unsigned int next_symbol, next_offset;static DEFINE_SPINLOCK (symbols_mutex); -if ( *symnum > symbols_num_syms ) +if ( *symnum > symbols_num_addrs )return -ERANGE ; -if ( *symnum == symbols_num_syms ) +if ( *symnum == symbols_num_addrs ) {/* No more symbols */ name[0 ] ='\0' ;
@@ -227,7 +211,7 @@ unsigned long symbols_lookup_by_name(con#ifdef CONFIG_FAST_SYMBOL_LOOKUP low =0 ; - high = symbols_num_syms; + high = symbols_num_names;while ( low < high ) {unsigned long mid = low + ((high - low) /2 );
xen/common/symbols.h new file +31 -0
@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include <xen/stdint.h> +#include <xen/symbols.h> + +#ifdef SYMBOLS_ORIGIN +extern const unsigned int symbols_offsets[]; +#define symbols_address (n) (SYMBOLS_ORIGIN + symbols_offsets[n]) +#else +extern const unsigned long symbols_addresses[]; +#define symbols_address (n) symbols_addresses[n] +#endif +extern const unsigned int symbols_num_addrs; +extern const unsigned char symbols_names[]; + +extern const unsigned int symbols_num_names; +extern const struct symbol_offset symbols_sorted_offsets[]; + +extern const uint8_t symbols_token_table[]; +extern const uint16_t symbols_token_index[]; + +extern const unsigned int symbols_markers[]; + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/
xen/common/symbols-dummy.c +6 -6
@@ -3,22 +3,22 @@* link of the hypervisor image. */ -#include <xen/types.h> -#include <xen/symbols.h> +#include "symbols.h" #ifdef SYMBOLS_ORIGIN const unsigned int symbols_offsets[1 ];#else const unsigned long symbols_addresses[1 ];#endif -const unsigned int symbols_num_syms; -const u8 symbols_names[1 ]; +const unsigned int symbols_num_addrs; +const unsigned char symbols_names[1 ];#ifdef CONFIG_FAST_SYMBOL_LOOKUP +const unsigned int symbols_num_names;const struct symbol_offset symbols_sorted_offsets[1 ];#endif -const u8 symbols_token_table[1 ]; -const u16 symbols_token_index[1 ]; +const uint8_t symbols_token_table[1 ]; +const uint16_t symbols_token_index[1 ];const unsigned int symbols_markers[1 ];
xen/tools/symbols.c +5 -1
@@ -323,7 +323,7 @@ static void write_src(void) }printf ("\n" ); -output_label ("symbols_num_syms" ); +output_label ("symbols_num_addrs" );printf ("\t.long\t%d\n" , table_cnt);printf ("\n" );
@@ -373,6 +373,10 @@ static void write_src(void)return ; } +output_label ("symbols_num_names" ); +printf ("\t.long\t%d\n" , table_cnt); +printf ("\n" ); +/* Sorted by original symbol names and type. */ qsort (table, table_cnt,sizeof (*table), compare_name_orig);
PATCH 6/6 symbols: centralize and re-arrange $(all_symbols) calculation
In reply to Jan BeulichFor one there's no need for each architecture to have the same logic. Move to the root Makefile, also to calculate just once.
And then re-arrange to permit FAST_SYMBOL_LOOKUP to be independent of LIVEPATCH, which may be useful in (at least) debugging.
---
Likely syms-warn-dup-y wants to follow suit; it doesn't even have an Arm counterpart right now.
xen/Makefile +6 -1
@@ -460,6 +460,10 @@ ALL_OBJS-$(CONFIG_CRYPTO) += crypto/buil ALL_LIBS-y := lib/lib.a +all-symbols-y := +all-symbols-$(CONFIG_LIVEPATCH) += --all-symbols +all-symbols-$(CONFIG_FAST_SYMBOL_LOOKUP) += --sort-by-name + include $(srctree)/arch/$(SRCARCH)/arch.mk # define new variables to avoid the ones defined in Config.mk
@@ -612,7 +616,8 @@ $(TARGET): outputmakefile asm-generic FO $(Q)$(MAKE) $(build)=include all $(Q)$(MAKE) $(build)=arch/$(SRCARCH) include $(Q)$(MAKE) $(build)=. arch/$(SRCARCH)/include/asm/asm-offsets.h - $(Q)$(MAKE) $(build)=. MKRELOC=$(MKRELOC) 'ALL_OBJS=$(ALL_OBJS-y)' 'ALL_LIBS=$(ALL_LIBS-y)' $@ + $(Q)$(MAKE) $(build)=. MKRELOC=$(MKRELOC) 'ALL_OBJS=$(ALL_OBJS-y)' \ + 'ALL_LIBS=$(ALL_LIBS-y)' 'all_symbols=$(all-symbols-y)' $@ SUBDIRS = xsm arch common crypto drivers lib test define all_sources
xen/arch/arm/Makefile +0 -9
@@ -81,15 +81,6 @@ ifneq ($(CONFIG_DTB_FILE),"") obj-y += dtb.o endif -ifdef CONFIG_LIVEPATCH -all_symbols = --all-symbols -ifdef CONFIG_FAST_SYMBOL_LOOKUP -all_symbols = --all-symbols --sort-by-name -endif -else -all_symbols = -endif - $(TARGET): $(TARGET)-syms $(OBJCOPY) -O binary -S $< $@ ifeq ($(CONFIG_ARM_64),y)
xen/arch/x86/Makefile +0 -9
@@ -111,15 +111,6 @@ notes_phdrs = --notes endif endif -ifdef CONFIG_LIVEPATCH -all_symbols = --all-symbols -ifdef CONFIG_FAST_SYMBOL_LOOKUP -all_symbols = --all-symbols --sort-by-name -endif -else -all_symbols = -endif - syms-warn-dup-y := --warn-dup syms-warn-dup-$(CONFIG_SUPPRESS_DUPLICATE_SYMBOL_WARNINGS) := syms-warn-dup-$(CONFIG_ENFORCE_UNIQUE_SYMBOLS) := --error-dup
PATCH 5/6 symbols: arrange to know where functions end
In reply to Jan BeulichWhen determining the symbol for a given address (e.g. for the %pS logging format specifier), so far the size of a symbol (function) was assumed to be everything until the next symbol. There may be gaps though, which would better be recognizable in output (often suggesting something odd is going on).
Insert "fake" end symbols in the address table, accompanied by zero- length type/name entries (to keep lookup reasonably close to how it was).
Note however that this, with present GNU binutils, won't work for xen.efi: The linker loses function sizes (they're not part of a normal symbol table entry), and hence nm has no way of reporting them.
The address table growth is quite significant on x86 release builds (due to functions being aligned to 16-byte boundaries), though: Its size almost doubles.
---
Note: Style-wise this is a horrible mix. I'm trying to match styles with
what's used in the respective functions.
Older GNU ld retains section symbols, which nm then also lists. Should we perhaps strip those as we read in nm's output? They don't provide any useful extra information, as our linker scripts add section start symbols anyway. (For the purposes here, luckily such section symbols are at least emitted without size.)
xen/common/symbols.c +36 -5
@@ -116,6 +116,13 @@ const char *symbols_lookup(unsigned longelse high = mid; } +/* If we hit an END symbol, move to the previous (real) one. */ +if (!symbols_names[get_symbol_offset (low)]) { +ASSERT (low); + symbol_end =symbols_address (low); + --low; + } +/* search for the first aliased symbol. Aliased symbols are symbols with the same address */ while (low &&symbols_address (low -1 ) ==symbols_address (low))
@@ -124,11 +131,13 @@ const char *symbols_lookup(unsigned long/* Grab name */ symbols_expand_symbol (get_symbol_offset (low), namebuf); -/* Search for next non-aliased symbol */ -for (i = low +1 ; i < symbols_num_addrs; i++) { -if (symbols_address (i) >symbols_address (low)) { - symbol_end =symbols_address (i); -break ; +if (!symbol_end) { +/* Search for next non-aliased symbol */ +for (i = low +1 ; i < symbols_num_addrs; i++) { +if (symbols_address (i) >symbols_address (low)) { + symbol_end =symbols_address (i); +break ; + } } }
@@ -170,6 +179,7 @@ int xensyms_read(uint32_t *symnum, charreturn -ERANGE ;if ( *symnum == symbols_num_addrs ) { + no_symbol:/* No more symbols */ name[0 ] ='\0' ;return 0 ;
@@ -183,10 +193,31 @@ int xensyms_read(uint32_t *symnum, char/* Non-sequential access */ next_offset =get_symbol_offset (*symnum); +/* +* If we're at an END symbol, skip to the next (real) one. This can +* happen if the caller ignores the *symnum output from an earlier +* iteration (Linux'es /proc/xen/xensyms handling does as of 6.14-rc). +*/ +if ( !symbols_names[next_offset] ) + { + ++next_offset; +if ( ++*symnum == symbols_num_addrs ) + goto no_symbol; + } + *type =symbols_get_symbol_type (next_offset); next_offset =symbols_expand_symbol (next_offset, name); *address =symbols_address (*symnum); +/* If next one is an END symbol, skip it. */ +if ( !symbols_names[next_offset] ) + { + ++next_offset; +/* Make sure not to increment past symbols_num_addrs below. */ +if ( *symnum +1 < symbols_num_addrs ) + ++*symnum; + } + next_symbol = ++*symnum;spin_unlock (&symbols_mutex);
xen/tools/symbols.c +46 -12
117 lines not on this page · raw message
Re: [PATCH 1/6] symbols: add minimal self-test
In reply to Jan BeulichQuoted 33 lines · Jan Beulich
... before making changes to the involved logic.
Signed-off-by Jan Beulich <[email protected]>---
With thisFAST_SYMBOL_LOOKUPmay make sense to permit enabling even when LIVEPATCH=n. Thoughts? (In this case "symbols: centralize and re- arrange $(all_symbols) calculation" would want pulling ahead.)xen/common/symbols.c +19
-0@@ -260,6 +260,41 @@ unsigned long symbols_lookup_by_name(conreturn 0 ; } +#ifdef CONFIG_SELF_TESTS + +static void __init test_lookup (unsigned long addr,const char *expected) +{ +char buf[KSYM_NAME_LEN +1 ]; +const char *name, *symname; +unsigned long size, offs; + + name =symbols_lookup (addr, &size, &offs, buf); +if ( !name ) +panic ("%s: address not found\n" , expected); +if ( offs ) +panic ("%s: non-zero offset (%#lx) unexpected\n" , expected, offs); + +/* Cope with static symbols, where varying file names/paths may be used. */ + symname =strchr (name,'#' ); + symname = symname ? symname +1 : name; +if (strcmp (symname, expected) ) +panic ("%s: unexpected symbol name: '%s'\n" , expected, symname);
xen/common/symbols.c unsigned long symbols_lookup_by_name(con · line 291
Quoted 10 lines
xen/common/symbols.c +10
-0+ + offs =symbols_lookup_by_name (name); +if ( offs != addr ) +panic ("%s: address %#lx unexpected; wanted %#lx\n" , + expected, offs, addr); +} + +static void __init __constructor test_symbols(void) +{ +/* Be sure to only try this for cf_check functions. */
I'm very happy to see the take-up of SELF_TESTs. Although I probably ought to tie it into a Kconfig option to make the errors non-fatal, which I've been meaning to do for a bit.
One question though. cf_check is an x86-ism, even if it leaks out into common code.
I think you mean "functions emitted into the final image"? If so, I don't think this is relevant then, because ...
xen/common/symbols.c unsigned long symbols_lookup_by_name(con · line 293
Quoted 2 lines
xen/common/symbols.c +2
-0+test_lookup ((unsigned long )dump_execstate,"dump_execstate" ); +test_lookup ((unsigned long )test_symbols, __func__);
... taking the function address here forces it to be emitted even if it would otherwise have been inlined.
~Andrew
Re: [PATCH 1/6] symbols: add minimal self-test
Quoted 52 lines · Andrew Cooper
Quoted 33 lines · Jan Beulich
... before making changes to the involved logic.
Signed-off-by Jan Beulich <[email protected]>---
With thisFAST_SYMBOL_LOOKUPmay make sense to permit enabling even when LIVEPATCH=n. Thoughts? (In this case "symbols: centralize and re- arrange $(all_symbols) calculation" would want pulling ahead.)xen/common/symbols.c +19
-0@@ -260,6 +260,41 @@ unsigned long symbols_lookup_by_name(conreturn 0 ; } +#ifdef CONFIG_SELF_TESTS + +static void __init test_lookup (unsigned long addr,const char *expected) +{ +char buf[KSYM_NAME_LEN +1 ]; +const char *name, *symname; +unsigned long size, offs; + + name =symbols_lookup (addr, &size, &offs, buf); +if ( !name ) +panic ("%s: address not found\n" , expected); +if ( offs ) +panic ("%s: non-zero offset (%#lx) unexpected\n" , expected, offs); + +/* Cope with static symbols, where varying file names/paths may be used. */ + symname =strchr (name,'#' ); + symname = symname ? symname +1 : name; +if (strcmp (symname, expected) ) +panic ("%s: unexpected symbol name: '%s'\n" , expected, symname);xen/common/symbols.c unsigned long symbols_lookup_by_name(con · line 291
Quoted 10 lines
xen/common/symbols.c +10
-0+ + offs =symbols_lookup_by_name (name); +if ( offs != addr ) +panic ("%s: address %#lx unexpected; wanted %#lx\n" , + expected, offs, addr); +} + +static void __init __constructor test_symbols(void) +{ +/* Be sure to only try this for cf_check functions. */ I'm very happy to see the take-up of SELF_TESTs. Although I probably ought to tie it into a
Kconfigoption to make the errors non-fatal, which I've been meaning to do for a bit.One question though.
cf_checkis an x86-ism, even if it leaks out into common code.
Quoted 8 lines
I think you mean "functions emitted into the final image"? If so, I don't think this is relevant then, because ...
xen/common/symbols.c unsigned long symbols_lookup_by_name(con · line 293
Quoted 2 lines
xen/common/symbols.c +2
-0+test_lookup ((unsigned long )dump_execstate,"dump_execstate" ); +test_lookup ((unsigned long )test_symbols, __func__);... taking the function address here forces it to be emitted even if it would otherwise have been inlined.
No, I really mean cf_check. If we took the address of a non-cf_check function, the special gcc13 build's checking would trigger, aiui.
Jan
Re: [PATCH 1/6] symbols: add minimal self-test
Quoted 55 lines · Jan Beulich
Quoted 54 lines · Andrew Cooper
Quoted 33 lines · Jan Beulich
... before making changes to the involved logic.
Signed-off-by Jan Beulich <[email protected]>---
With thisFAST_SYMBOL_LOOKUPmay make sense to permit enabling even when LIVEPATCH=n. Thoughts? (In this case "symbols: centralize and re- arrange $(all_symbols) calculation" would want pulling ahead.)xen/common/symbols.c +19
-0@@ -260,6 +260,41 @@ unsigned long symbols_lookup_by_name(conreturn 0 ; } +#ifdef CONFIG_SELF_TESTS + +static void __init test_lookup (unsigned long addr,const char *expected) +{ +char buf[KSYM_NAME_LEN +1 ]; +const char *name, *symname; +unsigned long size, offs; + + name =symbols_lookup (addr, &size, &offs, buf); +if ( !name ) +panic ("%s: address not found\n" , expected); +if ( offs ) +panic ("%s: non-zero offset (%#lx) unexpected\n" , expected, offs); + +/* Cope with static symbols, where varying file names/paths may be used. */ + symname =strchr (name,'#' ); + symname = symname ? symname +1 : name; +if (strcmp (symname, expected) ) +panic ("%s: unexpected symbol name: '%s'\n" , expected, symname);xen/common/symbols.c unsigned long symbols_lookup_by_name(con · line 291
Quoted 10 lines
xen/common/symbols.c +10
-0+ + offs =symbols_lookup_by_name (name); +if ( offs != addr ) +panic ("%s: address %#lx unexpected; wanted %#lx\n" , + expected, offs, addr); +} + +static void __init __constructor test_symbols(void) +{ +/* Be sure to only try this for cf_check functions. */ I'm very happy to see the take-up of SELF_TESTs. Although I probably ought to tie it into a
Kconfigoption to make the errors non-fatal, which I've been meaning to do for a bit.One question though.
cf_checkis an x86-ism, even if it leaks out into common code.I think you mean "functions emitted into the final image"? If so, I don't think this is relevant then, because ...
Quoted 6 lines
Quoted 4 lines
xen/common/symbols.c unsigned long symbols_lookup_by_name(con · line 293
Quoted 2 lines
xen/common/symbols.c +2
-0+test_lookup ((unsigned long )dump_execstate,"dump_execstate" ); +test_lookup ((unsigned long )test_symbols, __func__);... taking the function address here forces it to be emitted even if it would otherwise have been inlined.
No, I really mean
cf_check. If we took the address of a non-cf_check function, the special gcc13 build's checking would trigger, aiui.
It's GCC-11 sadly. cf_check is part of the function type, and triggers when a function type check would be relevant. Just casing to an integer won't trigger it, I don't think.
~Andrew
Re: [PATCH 1/6] symbols: add minimal self-test
Quoted 58 lines · Andrew Cooper
Quoted 57 lines · Jan Beulich
Quoted 56 lines · Andrew Cooper
Quoted 33 lines · Jan Beulich
... before making changes to the involved logic.
Signed-off-by Jan Beulich <[email protected]>---
With thisFAST_SYMBOL_LOOKUPmay make sense to permit enabling even when LIVEPATCH=n. Thoughts? (In this case "symbols: centralize and re- arrange $(all_symbols) calculation" would want pulling ahead.)xen/common/symbols.c +19
-0@@ -260,6 +260,41 @@ unsigned long symbols_lookup_by_name(conreturn 0 ; } +#ifdef CONFIG_SELF_TESTS + +static void __init test_lookup (unsigned long addr,const char *expected) +{ +char buf[KSYM_NAME_LEN +1 ]; +const char *name, *symname; +unsigned long size, offs; + + name =symbols_lookup (addr, &size, &offs, buf); +if ( !name ) +panic ("%s: address not found\n" , expected); +if ( offs ) +panic ("%s: non-zero offset (%#lx) unexpected\n" , expected, offs); + +/* Cope with static symbols, where varying file names/paths may be used. */ + symname =strchr (name,'#' ); + symname = symname ? symname +1 : name; +if (strcmp (symname, expected) ) +panic ("%s: unexpected symbol name: '%s'\n" , expected, symname);xen/common/symbols.c unsigned long symbols_lookup_by_name(con · line 291
Quoted 10 lines
xen/common/symbols.c +10
-0+ + offs =symbols_lookup_by_name (name); +if ( offs != addr ) +panic ("%s: address %#lx unexpected; wanted %#lx\n" , + expected, offs, addr); +} + +static void __init __constructor test_symbols(void) +{ +/* Be sure to only try this for cf_check functions. */ I'm very happy to see the take-up of SELF_TESTs. Although I probably ought to tie it into a
Kconfigoption to make the errors non-fatal, which I've been meaning to do for a bit.One question though.
cf_checkis an x86-ism, even if it leaks out into common code.I think you mean "functions emitted into the final image"? If so, I don't think this is relevant then, because ...
Quoted 2 lines
xen/common/symbols.c +2
-0+test_lookup ((unsigned long )dump_execstate,"dump_execstate" ); +test_lookup ((unsigned long )test_symbols, __func__);
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
... taking the function address here forces it to be emitted even if it would otherwise have been inlined.
No, I really mean
cf_check. If we took the address of a non-cf_check function, the special gcc13 build's checking would trigger, aiui.It's GCC-11 sadly.
cf_checkis part of the function type, and triggers when a function type check would be relevant. Just casing to an integer won't trigger it, I don't think.
Is there a way to double check? I'd be happy to drop that comment (and use some other, maybe less random function), but I don't have a compiler available that includes that patch.
Jan
Re: [PATCH 2/6] symbols: don't over-align generated data
In reply to Jan BeulichQuoted 7 lines · Jan Beulich
x86 is one of the few architectures where .align has the same meaning as .balign; most other architectures (Arm, PPC, and RISC-V in particular) give it the same meaning as .p2align. Aligning every one of these item to 256 bytes (on all 64-bit architectures except x86-64) is clearly too much.
Signed-off-by Jan Beulich <[email protected]>
Quoted 3 lines
---
Even uniformly aligning to 4 (x86, Arm32) or 8 bytes is too much imo, when some of the items require only 1- or 2-byte alignment.
It matters about the largest item, not the smallest.
Quoted 5 lines
Is there a reason only x86 defines
SYMBOLS_ORIGIN, to halve the address table in size? (Arm32 and other possible 32-bit ports of course have no need for doing so, but for 64-bit ones that can make quite a bit of a difference.)
I think the likely answer is that noone really understands how the symbol generation works, and didn't know that setting SYMBOLS_ORIGIN would be relevant.
I had a nasty interaction with the symbol code for the IDT-gen work, and it took a while to even get this intermediate file out.
~Andrew
Re: [PATCH 2/6] symbols: don't over-align generated data
Quoted 10 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
x86 is one of the few architectures where .align has the same meaning as .balign; most other architectures (Arm, PPC, and RISC-V in particular) give it the same meaning as .p2align. Aligning every one of these item to 256 bytes (on all 64-bit architectures except x86-64) is clearly too much.
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Andrew Cooper <[email protected]>
Thanks.
Quoted 5 lines
Quoted 3 lines
---
Even uniformly aligning to 4 (x86, Arm32) or 8 bytes is too much imo, when some of the items require only 1- or 2-byte alignment.It matters about the largest item, not the smallest.
The labels we generate are all followed by uniform-granularity data. Labels starting arrays of bytes or shorts are nevertheless 4- or 8-byte aligned.
Quoted 8 lines
Quoted 4 lines
Is there a reason only x86 defines
SYMBOLS_ORIGIN, to halve the address table in size? (Arm32 and other possible 32-bit ports of course have no need for doing so, but for 64-bit ones that can make quite a bit of a difference.)I think the likely answer is that noone really understands how the symbol generation works, and didn't know that setting
SYMBOLS_ORIGINwould be relevant.
Hmm, I didn't consider this might be the reason, but you may well be right.
Jan
Re: [PATCH 3/6] symbols: sanitize a few variable's types
In reply to Jan BeulichQuoted 11 lines · Jan Beulich
Parameter and return types of
symbols_expand_symbol()make clear that xensyms_read()'snext_offsetdoesn't need to be 64-bit.xensyms_read()'s first parameter type makes clear that the function's
next_symbolsdoesn't need to be 64-bit.symbols_num_syms'es type makes clear that iteration locals in
symbols_lookup()don't need to be unsigned long (i.e. 64-bit on 64-bit architectures).Signed-off-by Jan Beulich <[email protected]>
Re: [PATCH 4/6] symbols: split symbols_num_syms
In reply to Jan BeulichQuoted 11 lines · Jan Beulich
In preparation for inserting address entries into symbols_addresses[] / symbols_offsets[] without enlarging symbols_sorted_offsets[], split
symbols_num_symsintosymbols_num_addrs(counting entries in the former plus symbols_names[] as well as, less directly, symbols_markers[]) andsymbols_num_names(counting entries in the latter).While doing the adjustment move declarations to a new private
symbols.h, to be used by bothsymbols.candsymbols-dummy.c. Replace u8/u16 while doing so.Signed-off-by Jan Beulich <[email protected]>
Re: [PATCH 5/6] symbols: arrange to know where functions end
In reply to Jan BeulichQuoted 5 lines · Jan Beulich
When determining the symbol for a given address (e.g. for the %pS logging format specifier), so far the size of a symbol (function) was assumed to be everything until the next symbol. There may be gaps though, which would better be recognizable in output (often suggesting something odd is going on).
Do you have an example %pS for this new case?
Quoted 7 lines
Insert "fake" end symbols in the address table, accompanied by zero- length type/name entries (to keep lookup reasonably close to how it was).
Note however that this, with present GNU binutils, won't work for xen.efi: The linker loses function sizes (they're not part of a normal symbol table entry), and hence nm has no way of reporting them.
By "present GNU binutils", does this mean that you've got a fix in mind (or in progress), or that it's an open problem to be solved?
Quoted 3 lines
The address table growth is quite significant on x86 release builds (due to functions being aligned to 16-byte boundaries), though: Its size almost doubles.
Why does the function alignment affect the growth?
Quoted 11 lines
Requested-by Andrew Cooper <[email protected]>Signed-off-by Jan Beulich <[email protected]>---
Note: Style-wise this is a horrible mix. I'm trying to match styles with
what's used in the respective functions.Older GNU ld retains section symbols, which nm then also lists. Should we perhaps strip those as we read in nm's output? They don't provide any useful extra information, as our linker scripts add section start symbols anyway. (For the purposes here, luckily such section symbols are at least emitted without size.)
Will symbols_lookup() ever produce these? If not, it might be better to ignore the problem.
Taking extra logic to work around a benign issue in older toolchains isn't necessarily ideal.
~Andrew
Re: [PATCH 5/6] symbols: arrange to know where functions end
Quoted 8 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
When determining the symbol for a given address (e.g. for the %pS logging format specifier), so far the size of a symbol (function) was assumed to be everything until the next symbol. There may be gaps though, which would better be recognizable in output (often suggesting something odd is going on).
Do you have an example %pS for this new case?
I haven't encountered one yet, and I wasn't particularly trying to make up one.
Quoted 10 lines
Quoted 7 lines
Insert "fake" end symbols in the address table, accompanied by zero- length type/name entries (to keep lookup reasonably close to how it was).
Note however that this, with present GNU binutils, won't work for xen.efi: The linker loses function sizes (they're not part of a normal symbol table entry), and hence nm has no way of reporting them.
By "present GNU binutils", does this mean that you've got a fix in mind (or in progress), or that it's an open problem to be solved?
The latter; I can't even tell yet whether this is legitimate to be arranged for in a PE executable's symbol table.
Quoted 5 lines
Quoted 3 lines
The address table growth is quite significant on x86 release builds (due to functions being aligned to 16-byte boundaries), though: Its size almost doubles.
Why does the function alignment affect the growth?
I only insert fake end symbols when the following symbol doesn't match the prior one's end. Hence with minimal alignment (and thus no gaps) there wouldn't be any "end" symbols at all.
Quoted 12 lines
Quoted 11 lines
Requested-by Andrew Cooper <[email protected]>Signed-off-by Jan Beulich <[email protected]>---
Note: Style-wise this is a horrible mix. I'm trying to match styles with
what's used in the respective functions.Older GNU ld retains section symbols, which nm then also lists. Should we perhaps strip those as we read in nm's output? They don't provide any useful extra information, as our linker scripts add section start symbols anyway. (For the purposes here, luckily such section symbols are at least emitted without size.)
Quoted 5 lines
Will
symbols_lookup()ever produce these? If not, it might be better to ignore the problem.Taking extra logic to work around a benign issue in older toolchains isn't necessarily ideal.
Afaict it's unpredictable from Xen's pov. All depends on the order of entries after we sorted the table by address. The only criteria the tool's compare_value() applies for multiple symbols at the same address is to prefer global over local. As long as the first symbol in a section is global, we wouldn't see section symbols as lookup result.
Jan
Re: [PATCH 6/6] symbols: centralize and re-arrange $(all_symbols) calculation
In reply to Jan BeulichQuoted 10 lines · Jan Beulich
For one there's no need for each architecture to have the same logic. Move to the root
Makefile, also to calculate just once.And then re-arrange to permit
FAST_SYMBOL_LOOKUPto be independent of LIVEPATCH, which may be useful in (at least) debugging.Signed-off-by Jan Beulich <[email protected]>---
Likely syms-warn-dup-y wants to follow suit; it doesn't even have an Arm counterpart right now.
Recently, I thought the same about --orphan-handling={warn,error} too. We need to up it to error, and enforce it consistently.
There's actually a lot of $(TARGET)-syms which ought to be less copy&paste. I'll submit my cleanup so far, which doesn't interact here I don't think, but is also incomplete.
xen/Makefile ALL_OBJS-$(CONFIG_CRYPTO) += crypto/buil · line 466
Quoted 10 lines
xen/Makefile +4
-0@@ -460,6 +460,10 @@ ALL_OBJS-$(CONFIG_CRYPTO) += crypto/buil ALL_LIBS-y := lib/lib.a +all-symbols-y := +all-symbols-$(CONFIG_LIVEPATCH) += --all-symbols +all-symbols-$(CONFIG_FAST_SYMBOL_LOOKUP) += --sort-by-name +
I presume this works, so it's after we've processed Kconfig, but is there really nowhere better for it to live?
If we're moving others, this is going to turn into a lot, and it's specific to one final stage.
~Andrew
Re: [PATCH 5/6] symbols: arrange to know where functions end
In reply to Jan BeulichQuoted 15 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
When determining the symbol for a given address (e.g. for the %pS logging format specifier), so far the size of a symbol (function) was assumed to be everything until the next symbol. There may be gaps though, which would better be recognizable in output (often suggesting something odd is going on).
Do you have an example %pS for this new case?
I haven't encountered one yet, and I wasn't particularly trying to make up one.
Quoted 4 lines
Quoted 4 lines
Insert "fake" end symbols in the address table, accompanied by zero- length type/name entries (to keep lookup reasonably close to how it was).
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Note however that this, with present GNU binutils, won't work for xen.efi: The linker loses function sizes (they're not part of a normal symbol table entry), and hence nm has no way of reporting them.
By "present GNU binutils", does this mean that you've got a fix in mind (or in progress), or that it's an open problem to be solved?
The latter; I can't even tell yet whether this is legitimate to be arranged for in a PE executable's symbol table.
In which case, I'd suggest using the phrase "open problem" to make it clear that there's no fix.
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines
Requested-by Andrew Cooper <[email protected]>Signed-off-by Jan Beulich <[email protected]>---
Note: Style-wise this is a horrible mix. I'm trying to match styles with
what's used in the respective functions.Quoted 5 lines
Older GNU ld retains section symbols, which nm then also lists. Should we perhaps strip those as we read in nm's output? They don't provide any useful extra information, as our linker scripts add section start symbols anyway. (For the purposes here, luckily such section symbols are at least emitted without size.)
Will
symbols_lookup()ever produce these? If not, it might be better to ignore the problem.
Quoted 7 lines
Quoted 2 lines
Taking extra logic to work around a benign issue in older toolchains isn't necessarily ideal.
Afaict it's unpredictable from Xen's pov. All depends on the order of entries after we sorted the table by address. The only criteria the tool's
compare_value()applies for multiple symbols at the same address is to prefer global over local. As long as the first symbol in a section is global, we wouldn't see section symbols as lookup result.
Hmm, thinking about it, the global-ness does cause problems.
e.g. we get _stextentry()+x rather than restore_all_guest()+x, and RAG is more likely than some to show up in a backtrace.
So maybe we should strip section symbols, even the explicit linker ones, from the symbol table. I can't offhand think of a case where we want to look up a symbol by address and get back a section name.
(Feel free to leave this as a todo. I wasn't intending to scope creep like this, but it would be a nice to have.)
~Andrew
Re: [PATCH 6/6] symbols: centralize and re-arrange $(all_symbols) calculation
In reply to Andrew CooperQuoted 12 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
For one there's no need for each architecture to have the same logic. Move to the root
Makefile, also to calculate just once.And then re-arrange to permit
FAST_SYMBOL_LOOKUPto be independent of LIVEPATCH, which may be useful in (at least) debugging.Signed-off-by Jan Beulich <[email protected]>---
Likely syms-warn-dup-y wants to follow suit; it doesn't even have an Arm counterpart right now.
Quoted 5 lines
Recently, I thought the same about --orphan-handling={warn,error} too. We need to up it to error, and enforce it consistently.
There's actually a lot of $(TARGET)-syms which ought to be less copy&paste.
Indeed. Iirc like me I think you indicated you'd like to also break up those multi-step rules, to properly work through dependencies instead.
Quoted 9 lines
I'll submit my cleanup so far, which doesn't interact here I don't think, but is also incomplete.
Quoted 6 lines
xen/Makefile +0
-0@@ -460,6 +460,10 @@ ALL_OBJS-$(CONFIG_CRYPTO) += crypto/buil ALL_LIBS-y := lib/lib.a
Quoted 7 lines
xen/Makefile ALL_OBJS-$(CONFIG_CRYPTO) += crypto/buil · line 466
Quoted 4 lines
xen/Makefile +4
-0+all-symbols-y := +all-symbols-$(CONFIG_LIVEPATCH) += --all-symbols +all-symbols-$(CONFIG_FAST_SYMBOL_LOOKUP) += --sort-by-name +I presume this works, so it's after we've processed
Kconfig, but is there really nowhere better for it to live?
What do you mean by "better"? If we're to (slowly) centralize the linking, this is where all of that would move. xen/Makefile is processed just once (twice if .config changed), and ...
Quoted 2 lines
If we're moving others, this is going to turn into a lot, and it's specific to one final stage.
... applicable in exactly that one case. Or are you suggesting to introduce a new helper makefile where just the linking settings and (eventually) logic live? That would be a bigger piece of work, which I'm afraid I'm not up to right now.
For now it seemed quite natural to place all-symbols next to ALL_OBJS and ALL_LIBS.
Jan
Re: [PATCH 5/6] symbols: arrange to know where functions end
In reply to Andrew CooperQuoted 19 lines · Andrew Cooper
Quoted 18 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
When determining the symbol for a given address (e.g. for the %pS logging format specifier), so far the size of a symbol (function) was assumed to be everything until the next symbol. There may be gaps though, which would better be recognizable in output (often suggesting something odd is going on).
Do you have an example %pS for this new case?
I haven't encountered one yet, and I wasn't particularly trying to make up one.
Quoted 7 lines
Quoted 7 lines
Insert "fake" end symbols in the address table, accompanied by zero- length type/name entries (to keep lookup reasonably close to how it was).
Note however that this, with present GNU binutils, won't work for xen.efi: The linker loses function sizes (they're not part of a normal symbol table entry), and hence nm has no way of reporting them.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
By "present GNU binutils", does this mean that you've got a fix in mind (or in progress), or that it's an open problem to be solved?
The latter; I can't even tell yet whether this is legitimate to be arranged for in a PE executable's symbol table.
In which case, I'd suggest using the phrase "open problem" to make it clear that there's no fix.
I'd like to leave it as is; right here it's not overly important what state the binutils side is. Furthermore, by the time this goes in the binutils side may have changed state already (e.g. from "open problem" to "fix in progress").
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Older GNU ld retains section symbols, which nm then also lists. Should we perhaps strip those as we read in nm's output? They don't provide any useful extra information, as our linker scripts add section start symbols anyway. (For the purposes here, luckily such section symbols are at least emitted without size.)
Will
symbols_lookup()ever produce these? If not, it might be better to ignore the problem.Quoted 7 lines
Quoted 2 lines
Taking extra logic to work around a benign issue in older toolchains isn't necessarily ideal.
Afaict it's unpredictable from Xen's pov. All depends on the order of entries after we sorted the table by address. The only criteria the tool's
compare_value()applies for multiple symbols at the same address is to prefer global over local. As long as the first symbol in a section is global, we wouldn't see section symbols as lookup result.
Quoted 7 lines
Hmm, thinking about it, the global-ness does cause problems.
e.g. we get _stextentry()+x rather than restore_all_guest()+x, and RAG is more likely than some to show up in a backtrace.
So maybe we should strip section symbols, even the explicit linker ones, from the symbol table.
So one thing we could do is to prefer FUNC/OBJECT symbols over NOTYPE ones, and only use global-ness as a last resort criteria.
But "prefer" != "strip" in any event.
Stripping section symbols is reasonably easy for ELF, as rather than being NOTYPE they have no type at all. Stripping section start symbols, otoh, can only be done by name, and hence we'd need to maintain a list of them in the symbols tool. Not overly nice, but doable of course.
An intrusive - to the symbol table - alternative may be to simply strip all NOTYPE symbols. Yet that would take as a prereq marking quite a few more as FUNC or OBJECT.
Quoted 2 lines
I can't offhand think of a case where we want to look up a symbol by address and get back a section name.
We also need to keep in mind the opposite (lookup by name) for livepatch. I for one have no idea how (un)likely it might be for there to be a need to lookup a section symbol (then we'd be in trouble with newer binutils) or a section start symbol.
Quoted 2 lines
(Feel free to leave this as a todo. I wasn't intending to scope creep like this, but it would be a nice to have.)
If we can agree on what behavior we want, I can see about adding further patches to the series.
Jan