From nobody Tue Feb 10 01:16:09 2026 Received: from out28-121.mail.aliyun.com (out28-121.mail.aliyun.com [115.124.28.121]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2D1333A71D for ; Mon, 26 Jan 2026 13:35:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.28.121 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769434548; cv=none; b=jRMgmoRnsNhbtorM4RuxiVkGZL9BMnQaasEnUZHAGiNmaHFN8J/qu/RP1CkE0oFpFymEXOCLXtjpJgIrrPmsWm8tXwpe5LEaiAgCdNuAaOdjgvaSmsIjc/mMOjA+230ExyMPdX/8I19xikKdBEvPCZMiMjt7mIwDhH8gOoHEz8M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769434548; c=relaxed/simple; bh=Q9gMrlCKh8ZWb5c0rnpgOpDh4XRPjnnqoTkvA7zhZSQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Fi5D1QzHQDINTi1W788+/i0wKyk1xbBIAq8Q1HfYT6HB4iP9LJKaEXWU2tecUyKIhXIGOfLxpF/Mg2fT7fCenzQ8fWIca60BaWOthO5vTAPRd0XMIeFROql75B6IQ25Qt0B/0OqbH/fSPy2uG1DHULNRClYafiEordyUtDfAs5A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com; spf=pass smtp.mailfrom=antgroup.com; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b=AFDZFGNS; arc=none smtp.client-ip=115.124.28.121 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=antgroup.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b="AFDZFGNS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=antgroup.com; s=default; t=1769434535; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=mHmek4NXFcLFi3eVAlu6YebZS00+zaM7yiTeiwMD7sU=; b=AFDZFGNShqUJP46zjhyfRwxyKuLiM1so1WfZmynj1e2fJO6nI9js0aNxZAiSt/q1dCIsQeZQpCRoy83ww3YCsjeBqIbM/9EUjDY5OVcVAkPHyXs9yJhnHXvTW2pXhaR9JGXxts3T+WFdWiHVW6BnZKaN9lu+tXgmIYdcCzNoQME= Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.gG9Vi2L_1769434534 cluster:ay29) by smtp.aliyun-inc.com; Mon, 26 Jan 2026 21:35:34 +0800 From: Hou Wenlong To: linux-kernel@vger.kernel.org Cc: Lai Jiangshan , Hou Wenlong , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Ard Biesheuvel , Brian Gerst , Uros Bizjak Subject: [RFC PATCH 2/5] x86/relocs: Insert relocations into input file Date: Mon, 26 Jan 2026 21:33:52 +0800 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a command line option to insert relocations into a reserved section named ".data.reloc" section of the input file. This allows for relocation of the uncompressed kernel and is same as the implementation in MIPS. Signed-off-by: Hou Wenlong --- arch/x86/tools/relocs.c | 56 ++++++++++++++++++++++++++++++---- arch/x86/tools/relocs.h | 1 + arch/x86/tools/relocs_common.c | 13 ++++++-- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c index 945d78c368f7..4d0f2bd6c35e 100644 --- a/arch/x86/tools/relocs.c +++ b/arch/x86/tools/relocs.c @@ -735,6 +735,17 @@ static void walk_relocs(int (*process)(struct section = *sec, Elf_Rel *rel, } } =20 +static struct section *sec_lookup(const char *name) +{ + int i; + + for (i =3D 0; i < shnum; i++) + if (!strcmp(sec_name(i), name)) + return &secs[i]; + + return NULL; +} + #if ELF_BITS =3D=3D 64 =20 static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, @@ -950,7 +961,7 @@ static int write32_as_text(uint32_t v, FILE *f) return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1; } =20 -static void emit_relocs(void) +static void emit_relocs(FILE *f) { int i; int (*write_reloc)(uint32_t, FILE *) =3D write32; @@ -1001,21 +1012,54 @@ static void emit_relocs(void) for (i =3D 0; i < relocs32.count; i++) write_reloc(relocs32.offset[i], stdout); } else { + FILE *outf =3D stdout; + + if (keep_relocs) { + struct section *sec_reloc; + uint32_t size_needed; + unsigned long offset; + + sec_reloc =3D sec_lookup(".data.reloc"); + if (!sec_reloc) + die("Could not find relocation data section\n"); + + size_needed =3D (1 + relocs32.count) * sizeof(uint32_t); +#if ELF_BITS =3D=3D 64 + size_needed +=3D (1 + relocs64.count) * sizeof(uint32_t); +#endif + if (size_needed > sec_reloc->shdr.sh_size) + die("Relocations overflow available space!\n" \ + "Please adjust CONFIG_RELOCATION_TABLE_SIZE " \ + "to at least 0x%08x\n", (size_needed + 0x1000) & ~0xFFF); + + /* + * Place the relocations at the end of section to ensure + * compatibility with backward traversal during handling + * relocations. + */ + offset =3D sec_reloc->shdr.sh_offset + sec_reloc->shdr.sh_size - + size_needed; + if (fseek(f, offset, SEEK_SET) < 0) + die("Seek to %ld failed: %s\n", offset, strerror(errno)); + + outf =3D f; + } + #if ELF_BITS =3D=3D 64 /* Print a stop */ - write_reloc(0, stdout); + write_reloc(0, outf); =20 /* Now print each relocation */ for (i =3D 0; i < relocs64.count; i++) - write_reloc(relocs64.offset[i], stdout); + write_reloc(relocs64.offset[i], outf); #endif =20 /* Print a stop */ - write_reloc(0, stdout); + write_reloc(0, outf); =20 /* Now print each relocation */ for (i =3D 0; i < relocs32.count; i++) - write_reloc(relocs32.offset[i], stdout); + write_reloc(relocs32.offset[i], outf); } } =20 @@ -1073,5 +1117,5 @@ void process(FILE *fp) return; } =20 - emit_relocs(); + emit_relocs(fp); } diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h index 5ade07ac2754..05a6e80e815a 100644 --- a/arch/x86/tools/relocs.h +++ b/arch/x86/tools/relocs.h @@ -35,6 +35,7 @@ extern int show_absolute_relocs; extern int show_reloc_info; extern int as_text; extern int use_real_mode; +extern int keep_relocs; =20 void process_32(FILE *fp); void process_64(FILE *fp); diff --git a/arch/x86/tools/relocs_common.c b/arch/x86/tools/relocs_common.c index ee8a9fafcb11..66a9a83b7395 100644 --- a/arch/x86/tools/relocs_common.c +++ b/arch/x86/tools/relocs_common.c @@ -2,7 +2,7 @@ #include "relocs.h" =20 int show_absolute_syms, show_absolute_relocs, show_reloc_info; -int as_text, use_real_mode; +int as_text, use_real_mode, keep_relocs; =20 void die(char *fmt, ...) { @@ -15,7 +15,7 @@ void die(char *fmt, ...) =20 static void usage(void) { - die("relocs [--abs-syms|--abs-relocs|--reloc-info|--text|--realmode]" \ + die("relocs [--abs-syms|--abs-relocs|--reloc-info|--text|--realmode|--kee= p]" \ " vmlinux\n"); } =20 @@ -55,6 +55,10 @@ int main(int argc, char **argv) use_real_mode =3D 1; continue; } + if (strcmp(arg, "--keep") =3D=3D 0) { + keep_relocs =3D 1; + continue; + } } else if (!fname) { fname =3D arg; @@ -65,7 +69,10 @@ int main(int argc, char **argv) if (!fname) { usage(); } - fp =3D fopen(fname, "r"); + if (keep_relocs) + fp =3D fopen(fname, "r+"); + else + fp =3D fopen(fname, "r"); if (!fp) { die("Cannot open %s: %s\n", fname, strerror(errno)); } --=20 2.31.1