The function is misnamed; it really is looking for a GNU Build ID note, not a Xen note as the name suggests.
It also has an awkward API to use. For a GNU Build ID, the content is always at +16 from the start, so really doesn't need to be returned by pointer, nor is it necessary be doing additional checks on the returned pointers being valid. Switching to is_gnu_build_id() allows xen_build_init() to be simplified, returning after the first successful find rather than passing an rc down the function to skip later checks. No functional change. Signed-off-by: Andrew Cooper <[email protected]> --- CC: Ross Lagerwall <[email protected]> CC: Anthony PERARD <[email protected]> CC: Michal Orzel <[email protected]> CC: Jan Beulich <[email protected]> CC: Julien Grall <[email protected]> CC: Roger Pau Monné <[email protected]> CC: Stefano Stabellini <[email protected]> The 16 probably ought to be a define somewhere, but I see no obvious candidates in the ELF spec. --- xen/common/livepatch.c | 13 +++++-------- xen/common/version.c | 41 +++++++-------------------------------- xen/include/xen/version.h | 2 -- 3 files changed, 12 insertions(+), 44 deletions(-) diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index 7515a040ad46..9960db589b76 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -453,22 +453,19 @@ static int parse_buildid(const struct livepatch_elf_sec *sec, struct livepatch_build_id *id) { const Elf_Note *n; - int rc; + unsigned int len; /* Presence of the sections is ensured by check_special_sections(). */ ASSERT(sec); n = sec->addr; + len = sec->sec->sh_size; - if ( sec->sec->sh_size <= sizeof(*n) ) + if ( !is_gnu_build_id(n, len) ) return -EINVAL; - rc = xen_build_id_check(n, sec->sec->sh_size, &id->p, &id->len); - if ( rc ) - return rc; - - if ( !id->len || !id->p ) - return -EINVAL; + id->p = n + NT_GNU_HDR_SIZE; + id->len = len - NT_GNU_HDR_SIZE; return 0; } diff --git a/xen/common/version.c b/xen/common/version.c index 7eb8a0221d82..63a5261a2279 100644 --- a/xen/common/version.c +++ b/xen/common/version.c @@ -145,36 +145,6 @@ bool is_gnu_build_id(const Elf_Note *n, unsigned int len) return true; } -int xen_build_id_check(const Elf_Note *n, unsigned int n_sz, - const void **p, unsigned int *len) -{ - /* Check if we really have a build-id. */ - ASSERT(n_sz > sizeof(*n)); - - if ( NT_GNU_BUILD_ID != n->type ) - return -ENODATA; - - if ( n->namesz + n->descsz < n->namesz ) - return -EINVAL; - - if ( n->namesz < 4 /* GNU\0 */) - return -EINVAL; - - if ( n->namesz + n->descsz > n_sz - sizeof(*n) ) - return -EINVAL; - - /* Sanity check, name should be "GNU" for ld-generated build-id. */ - if ( strncmp(ELFNOTE_NAME(n), "GNU", n->namesz) != 0 ) - return -ENODATA; - - if ( len ) - *len = n->descsz; - if ( p ) - *p = ELFNOTE_DESC(n); - - return 0; -} - struct pe_external_debug_directory { uint32_t characteristics; @@ -201,7 +171,6 @@ void __init xen_build_init(void) { const Elf_Note *n = __note_gnu_build_id_start; unsigned int sz; - int rc; /* --build-id invoked with wrong parameters. */ if ( __note_gnu_build_id_end <= &n[0] ) @@ -213,14 +182,18 @@ void __init xen_build_init(void) sz = (uintptr_t)__note_gnu_build_id_end - (uintptr_t)n; - rc = xen_build_id_check(n, sz, &xen_build_id, &xen_build_id_len); + if ( is_gnu_build_id(n, sz) ) + { + xen_build_id = n + NT_GNU_HDR_SIZE; + xen_build_id_len = sz - NT_GNU_HDR_SIZE; + return; + } #ifdef CONFIG_X86 /* * xen.efi built with a new enough toolchain will have a CodeView record, * not an ELF note. */ - if ( rc ) { const struct pe_external_debug_directory *dir = (const void *)n; @@ -241,7 +214,7 @@ void __init xen_build_init(void) { xen_build_id = info->signature; xen_build_id_len = sizeof(info->signature); - rc = 0; + return; } } } diff --git a/xen/include/xen/version.h b/xen/include/xen/version.h index ee857616f9e4..6e9a24b7fadf 100644 --- a/xen/include/xen/version.h +++ b/xen/include/xen/version.h @@ -25,8 +25,6 @@ extern unsigned int xen_build_id_len; /* 0 -> No build id. */ #ifdef BUILD_ID void xen_build_init(void); -int xen_build_id_check(const Elf_Note *n, unsigned int n_sz, - const void **p, unsigned int *len); bool is_gnu_build_id(const Elf_Note *n, unsigned int len); #else static inline void xen_build_init(void) {}; -- 2.39.5
