Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 4 additions & 95 deletions kpatch-build/create-diff-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,97 +479,6 @@ static bool is_special_static(struct symbol *sym)
return false;
}

static bool has_digit_tail(char *tail)
{
if (*tail != '.')
return false;

while (isdigit(*++tail))
;

if (!*tail)
return true;

return false;
}

/*
* Hack for __UNIQUE_ID(). The following should match:
*
* __UNIQUE_ID_ddebug1131.186
* __UNIQUE_ID_ddebug1132.187
*/
static int __kpatch_unique_id_strcmp(char *s1, char *s2)
{
/* match '__UNIQUE_ID_ddebug' */
while (*s1 == *s2) {
if (!*s1)
return 0;
s1++;
s2++;
}

/* skip digits before '.' or EOL */
while (isdigit(*s1))
s1++;
while (isdigit(*s2))
s2++;

if ((!*s1 || has_digit_tail(s1)) &&
(!*s2 || has_digit_tail(s2)))
return 0;

return 1;
}

/*
* This is like strcmp, but for gcc-mangled symbols. It skips the comparison
* of any substring which consists of '.' followed by any number of digits.
*/
static int kpatch_mangled_strcmp(char *s1, char *s2)
{
/*
* ELF string sections aren't mangled, though they look that way. Just
* compare them normally.
*/
if (strstr(s1, ".str1."))
return strcmp(s1, s2);

if (!strncmp(s1, "__UNIQUE_ID_", 12))
return __kpatch_unique_id_strcmp(s1, s2);

/*
* With LLVM, dynamic debug unique id looks like
* <func_name>.__UNIQUE_ID_ddebug<number>.
*
* Use __kpatch_unique_id_strcmp to compare them.
*/
if (strstr(s1, "__UNIQUE_ID_ddebug"))
return __kpatch_unique_id_strcmp(s1, s2);

while (*s1 == *s2) {
if (!*s1)
return 0;
if (*s1 == '.' && isdigit(s1[1])) {
if (!isdigit(s2[1]))
return 1;
while (isdigit(*++s1))
;
while (isdigit(*++s2))
;
} else {
s1++;
s2++;
}
}

if ((!*s1 && has_digit_tail(s2)) ||
(!*s2 && has_digit_tail(s1)))
return 0;

return 1;
}

static bool rela_equal(struct rela *rela1, struct rela *rela2)
{
struct rela *rela_toc1, *rela_toc2;
Expand Down Expand Up @@ -1399,7 +1308,8 @@ static bool kpatch_is_normal_static_local(struct symbol *sym)
if (sym->type != STT_OBJECT || sym->bind != STB_LOCAL)
return false;

if (!strncmp(sym->name, ".L", 2))
if (!strncmp(sym->name, ".L", 2) ||
!strncmp(sym->name, ".compoundliteral", 16))
return false;

if (!strchr(sym->name, '.'))
Expand Down Expand Up @@ -3890,7 +3800,7 @@ static void kpatch_create_intermediate_sections(struct kpatch_elf *kelf,
ALLOC_LINK(rela2, &ksym_sec->rela->relas);
rela2->sym = strsym;
rela2->type = absolute_rela_type(kelf);
rela2->addend = offset_of_string(&kelf->strings, rela->sym->name);
rela2->addend = offset_of_string(&kelf->strings, symbol.name);
rela2->offset = (unsigned int)(index * sizeof(*ksyms) + \
offsetof(struct kpatch_symbol, name));

Expand Down Expand Up @@ -4169,14 +4079,13 @@ static void kpatch_create_ftrace_callsite_sections(struct kpatch_elf *kelf)
ERROR("malloc");
memcpy(newdata, sym->sec->data->d_buf, sym->sec->data->d_size);
sym->sec->data->d_buf = newdata;
insn = newdata;
insn = newdata + insn_offset;

/*
* Replace the NOP with a call to fentry. The fentry
* rela symbol is already there, just need to change
* the relocation type accordingly.
*/
insn = sym->sec->data->d_buf;
if (insn[0] != 0xf)
ERROR("%s: unexpected instruction at the start of the function", sym->name);
insn[0] = 0xe8;
Expand Down
8 changes: 6 additions & 2 deletions kpatch-build/kpatch-build
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ SKIPCLEANUP=0
SKIPCOMPILERCHECK=0
ARCH_KCFLAGS=""
DEBUG_KCFLAGS=""
USER_KCFLAGS="${KCFLAGS:-}"
declare -a PATCH_LIST
APPLIED_PATCHES=0
OOT_MODULE=
Expand Down Expand Up @@ -1343,7 +1344,7 @@ if [[ "$ARCH" = "loongarch64" ]]; then
fi

export KCFLAGS="-I$DATADIR/patch -ffunction-sections -fdata-sections \
$ARCH_KCFLAGS $DEBUG_KCFLAGS"
$ARCH_KCFLAGS $DEBUG_KCFLAGS${USER_KCFLAGS:+ $USER_KCFLAGS}"

echo "Reading special section data"
find_special_section_data
Expand All @@ -1361,6 +1362,9 @@ declare -a MAKEVARS
if [[ -n "$CONFIG_CC_IS_CLANG" ]]; then
MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${CLANG}")
MAKEVARS+=("HOSTCC=clang")
if [[ -n "$CONFIG_LD_IS_LLD" && -n "$CONFIG_AS_IS_LLVM" ]] && command -v llvm-objcopy &>/dev/null; then
MAKEVARS+=("LLVM=${LLVM:-1}")
fi
else
MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${GCC}")
fi
Expand Down Expand Up @@ -1549,7 +1553,7 @@ do
done
echo

export KCFLAGS="-I$DATADIR/patch $ARCH_KCFLAGS"
export KCFLAGS="-I$DATADIR/patch $ARCH_KCFLAGS${USER_KCFLAGS:+ $USER_KCFLAGS}"
if [[ "$USE_KLP" -eq 0 ]]; then
export KCPPFLAGS="-D__KPATCH_MODULE__"
fi
Expand Down
93 changes: 93 additions & 0 deletions kpatch-build/kpatch-elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
Expand Down Expand Up @@ -769,6 +770,98 @@ bool is_ubsan_sec(const char *name) {
return false;
}

static bool has_digit_tail(char *tail)
{
if (*tail != '.')
return false;

while (isdigit((unsigned char)*++tail))
;

if (!*tail)
return true;

return false;
}

/*
* Hack for __UNIQUE_ID(). The following should match:
*
* __UNIQUE_ID_ddebug1131.186
* __UNIQUE_ID_ddebug1132.187
*/
static int __kpatch_unique_id_strcmp(char *s1, char *s2)
{
/* match '__UNIQUE_ID_ddebug' */
while (*s1 == *s2) {
if (!*s1)
return 0;
s1++;
s2++;
}

/* skip digits before '.' or EOL */
while (isdigit((unsigned char)*s1))
s1++;
while (isdigit((unsigned char)*s2))
s2++;

if ((!*s1 || has_digit_tail(s1)) &&
(!*s2 || has_digit_tail(s2)))
return 0;

return 1;
}

/*
* This is like strcmp, but for gcc/clang-mangled symbols. It skips the
* comparison of any substring which consists of '.' followed by any number of
* digits.
*/
int kpatch_mangled_strcmp(char *s1, char *s2)
{
/*
* ELF string sections aren't mangled, though they look that way. Just
* compare them normally.
*/
if (strstr(s1, ".str1."))
return strcmp(s1, s2);

if (!strncmp(s1, "__UNIQUE_ID_", 12))
return __kpatch_unique_id_strcmp(s1, s2);

/*
* With LLVM, dynamic debug unique id looks like
* <func_name>.__UNIQUE_ID_ddebug<number>.
*
* Use __kpatch_unique_id_strcmp to compare them.
*/
if (strstr(s1, "__UNIQUE_ID_ddebug"))
return __kpatch_unique_id_strcmp(s1, s2);

while (*s1 == *s2) {
if (!*s1)
return 0;
if (*s1 == '.' && isdigit((unsigned char)s1[1])) {
if (!isdigit((unsigned char)s2[1]))
return 1;
while (isdigit((unsigned char)*++s1))
;
while (isdigit((unsigned char)*++s2))
;
} else {
s1++;
s2++;
}
}

if ((!*s1 && has_digit_tail(s2)) ||
(!*s2 && has_digit_tail(s1)))
return 0;

return 1;
}

void print_strtab(char *buf, size_t size)
{
size_t i;
Expand Down
1 change: 1 addition & 0 deletions kpatch-build/kpatch-elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ bool is_file_sym(struct symbol *sym);
bool is_local_func_sym(struct symbol *sym);
bool is_local_sym(struct symbol *sym);
bool is_ubsan_sec(const char *name);
int kpatch_mangled_strcmp(char *s1, char *s2);

void print_strtab(char *buf, size_t size);
void kpatch_create_shstrtab(struct kpatch_elf *kelf);
Expand Down
Loading
Loading