From: Roger Pau Monne Date: Tue, 28 Nov 2023 09:21:52 +0000 (+0100) Subject: livepatch-build-tools: account for section alignment when calculating function size X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=0ed8ef88dc300750696a64e89efa3b82502f6dc7;p=livepatch-build-tools.git livepatch-build-tools: account for section alignment when calculating function size Forcing function section alignment at the compiler level ensures that enough space is present in the text section so that at least a jump can be encoded in the old function body to switch to the newly loaded code payload. Modify create-diff-object to account for any section alignment when calculating the size of the old function. When used with a suitable -falign-function compiler parameter on the Xen hypervisor build this ensures that all functions have enough space to accommodate for any control flow instructions plus a jump. Note that while the rounding could be applied to all (text) symbols, it's not required, as the size is only relevant for functions that are live patched, and hence only do the rounding when calculating the old (previous) function size. Signed-off-by: Roger Pau Monné Reviewed-by: Ross Lagerwall --- diff --git a/common.h b/common.h index 0d3e5f1..5ff9ef6 100644 --- a/common.h +++ b/common.h @@ -37,6 +37,8 @@ extern char *childobj; list_add_tail(&(_new)->list, (_list)); \ } +#define ROUNDUP(x, a) (((x) + (a) - 1) & ~((a) - 1)) + enum loglevel { DEBUG, NORMAL diff --git a/create-diff-object.c b/create-diff-object.c index d0e14e3..fed360a 100644 --- a/create-diff-object.c +++ b/create-diff-object.c @@ -2015,6 +2015,11 @@ static void livepatch_create_patches_sections(struct kpatch_elf *kelf, ERROR("lookup_global_symbol %s", sym->name); } + + /* Take into account section alignment for padding. */ + result.size = ROUNDUP(result.size, + sym->sec->sh.sh_addralign); + log_debug("lookup for %s @ 0x%016lx len %lu\n", sym->name, result.value, result.size);