2021-06-18 00:30:09 +03:00
|
|
|
/*
|
|
|
|
* Post-process a vdso elf image for inclusion into qemu.
|
|
|
|
*
|
|
|
|
* Copyright 2023 Linaro, Ltd.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <endian.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "elf.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define bswap_(p) _Generic(*(p), \
|
|
|
|
uint16_t: __builtin_bswap16, \
|
|
|
|
uint32_t: __builtin_bswap32, \
|
|
|
|
uint64_t: __builtin_bswap64, \
|
|
|
|
int16_t: __builtin_bswap16, \
|
|
|
|
int32_t: __builtin_bswap32, \
|
|
|
|
int64_t: __builtin_bswap64)
|
|
|
|
#define bswaps(p) (*(p) = bswap_(p)(*(p)))
|
|
|
|
|
|
|
|
static void output_reloc(FILE *outf, void *buf, void *loc)
|
|
|
|
{
|
|
|
|
fprintf(outf, " 0x%08tx,\n", loc - buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *sigreturn_sym;
|
|
|
|
static const char *rt_sigreturn_sym;
|
|
|
|
|
|
|
|
static unsigned sigreturn_addr;
|
|
|
|
static unsigned rt_sigreturn_addr;
|
|
|
|
|
|
|
|
#define N 32
|
|
|
|
#define elfN(x) elf32_##x
|
|
|
|
#define ElfN(x) Elf32_##x
|
|
|
|
#include "gen-vdso-elfn.c.inc"
|
|
|
|
#undef N
|
|
|
|
#undef elfN
|
|
|
|
#undef ElfN
|
|
|
|
|
|
|
|
#define N 64
|
|
|
|
#define elfN(x) elf64_##x
|
|
|
|
#define ElfN(x) Elf64_##x
|
|
|
|
#include "gen-vdso-elfn.c.inc"
|
|
|
|
#undef N
|
|
|
|
#undef elfN
|
|
|
|
#undef ElfN
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
FILE *inf, *outf;
|
|
|
|
long total_len;
|
|
|
|
const char *prefix = "vdso";
|
|
|
|
const char *inf_name;
|
|
|
|
const char *outf_name = NULL;
|
|
|
|
unsigned char *buf;
|
|
|
|
bool need_bswap;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int opt = getopt(argc, argv, "o:p:r:s:");
|
|
|
|
if (opt < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (opt) {
|
|
|
|
case 'o':
|
|
|
|
outf_name = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
prefix = optarg;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rt_sigreturn_sym = optarg;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sigreturn_sym = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage:
|
|
|
|
fprintf(stderr, "usage: [-p prefix] [-r rt-sigreturn-name] "
|
|
|
|
"[-s sigreturn-name] -o output-file input-file\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind >= argc || outf_name == NULL) {
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
inf_name = argv[optind];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the input and output files.
|
|
|
|
*/
|
|
|
|
inf = fopen(inf_name, "rb");
|
|
|
|
if (inf == NULL) {
|
|
|
|
goto perror_inf;
|
|
|
|
}
|
|
|
|
outf = fopen(outf_name, "w");
|
|
|
|
if (outf == NULL) {
|
|
|
|
goto perror_outf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the input file into a buffer.
|
|
|
|
* We expect the vdso to be small, on the order of one page,
|
|
|
|
* therefore we do not expect a partial read.
|
|
|
|
*/
|
|
|
|
fseek(inf, 0, SEEK_END);
|
|
|
|
total_len = ftell(inf);
|
|
|
|
fseek(inf, 0, SEEK_SET);
|
|
|
|
|
|
|
|
buf = malloc(total_len);
|
|
|
|
if (buf == NULL) {
|
|
|
|
goto perror_inf;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (fread(buf, 1, total_len, inf) != total_len) {
|
|
|
|
if (errno) {
|
|
|
|
goto perror_inf;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s: incomplete read\n", inf_name);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
fclose(inf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Identify which elf flavor we're processing.
|
|
|
|
* The first 16 bytes of the file are e_ident.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (buf[EI_MAG0] != ELFMAG0 || buf[EI_MAG1] != ELFMAG1 ||
|
|
|
|
buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3) {
|
|
|
|
fprintf(stderr, "%s: not an elf file\n", inf_name);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
switch (buf[EI_DATA]) {
|
|
|
|
case ELFDATA2LSB:
|
|
|
|
need_bswap = BYTE_ORDER != LITTLE_ENDIAN;
|
|
|
|
break;
|
|
|
|
case ELFDATA2MSB:
|
|
|
|
need_bswap = BYTE_ORDER != BIG_ENDIAN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: invalid elf EI_DATA (%u)\n",
|
|
|
|
inf_name, buf[EI_DATA]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to relocate the VDSO image. The one built into the kernel
|
|
|
|
* is built for a fixed address. The one we built for QEMU is not,
|
|
|
|
* since that requires close control of the guest address space.
|
|
|
|
*
|
|
|
|
* Output relocation addresses as we go.
|
|
|
|
*/
|
|
|
|
|
linux-user: Fix GDB complaining about system-supplied DSO string table index
When debugging qemu-user processes using gdbstub, the following warning
appears every time:
warning: BFD: warning: system-supplied DSO at 0x7f8253cc3000 has a corrupt string table index
The reason is that QEMU does not map the VDSO's section headers. The
VDSO's ELF header's e_shoff points to zeros, which GDB fails to parse.
The difference with the kernel's VDSO is that the latter is mapped as a
blob, ignoring program headers - which also don't cover the section
table. QEMU, on the other hand, loads it as an ELF file.
There appears to be no way to place section headers inside a section,
and, therefore, no way to refer to them from a linker script. Also, ld
hardcodes section headers to be non-loadable, see
_bfd_elf_assign_file_positions_for_non_load(). In theory ld could be
enhanced by implementing an "SHDRS" keyword in addition to the existing
"FILEHDR" and "PHDRS".
There are multiple ways to resolve the issue:
- Copy VDSO as a blob in load_elf_vdso(). This would require creating
specialized loader logic, that duplicates parts of load_elf_image().
- Fix up VDSO's PHDR size in load_elf_vdso(). This would require either
duplicating the parsing logic, or adding an ugly parameter to
load_elf_image().
- Fix up VDSO's PHDR size in gen-vdso. This is the simplest solution,
so do it.
There are two tricky parts:
- Byte-swaps need to be done either on local copies, or in-place and
then reverted in the end. To preserve the existing code structure, do
the former for Sym and Dyn, and the latter for Ehdr, Phdr, and Shdr.
- There must be no .bss, which is already the case - but having an
explicit check is helpful to ensure correctness.
To verify this change, I diffed the on-disk and the loaded VDSOs; the
result does not show anything unusual, except for what seems to be an
existing oversight (which should probably be fixed separately):
│ Symbol table '.dynsym' contains 8 entries:
│ Num: Value Size Type Bind Vis Ndx Name
│ - 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
│ - 6: 0000000000000000 0 OBJECT GLOBAL DEFAULT ABS LINUX_2.6.29
│ + 0: 00007f61075bf000 0 NOTYPE LOCAL DEFAULT UND
│ + 6: 00007f61075bf000 0 OBJECT GLOBAL DEFAULT ABS LINUX_2.6.29
Fixes: 2fa536d10797 ("linux-user: Add gen-vdso tool")
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-ID: <20241023202850.55211-1-iii@linux.ibm.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
2024-10-23 23:27:41 +03:00
|
|
|
fprintf(outf,
|
|
|
|
"/* Automatically generated by linux-user/gen-vdso.c. */\n"
|
|
|
|
"\n"
|
|
|
|
"static const unsigned %s_relocs[] = {\n", prefix);
|
2021-06-18 00:30:09 +03:00
|
|
|
|
|
|
|
switch (buf[EI_CLASS]) {
|
|
|
|
case ELFCLASS32:
|
linux-user: Fix GDB complaining about system-supplied DSO string table index
When debugging qemu-user processes using gdbstub, the following warning
appears every time:
warning: BFD: warning: system-supplied DSO at 0x7f8253cc3000 has a corrupt string table index
The reason is that QEMU does not map the VDSO's section headers. The
VDSO's ELF header's e_shoff points to zeros, which GDB fails to parse.
The difference with the kernel's VDSO is that the latter is mapped as a
blob, ignoring program headers - which also don't cover the section
table. QEMU, on the other hand, loads it as an ELF file.
There appears to be no way to place section headers inside a section,
and, therefore, no way to refer to them from a linker script. Also, ld
hardcodes section headers to be non-loadable, see
_bfd_elf_assign_file_positions_for_non_load(). In theory ld could be
enhanced by implementing an "SHDRS" keyword in addition to the existing
"FILEHDR" and "PHDRS".
There are multiple ways to resolve the issue:
- Copy VDSO as a blob in load_elf_vdso(). This would require creating
specialized loader logic, that duplicates parts of load_elf_image().
- Fix up VDSO's PHDR size in load_elf_vdso(). This would require either
duplicating the parsing logic, or adding an ugly parameter to
load_elf_image().
- Fix up VDSO's PHDR size in gen-vdso. This is the simplest solution,
so do it.
There are two tricky parts:
- Byte-swaps need to be done either on local copies, or in-place and
then reverted in the end. To preserve the existing code structure, do
the former for Sym and Dyn, and the latter for Ehdr, Phdr, and Shdr.
- There must be no .bss, which is already the case - but having an
explicit check is helpful to ensure correctness.
To verify this change, I diffed the on-disk and the loaded VDSOs; the
result does not show anything unusual, except for what seems to be an
existing oversight (which should probably be fixed separately):
│ Symbol table '.dynsym' contains 8 entries:
│ Num: Value Size Type Bind Vis Ndx Name
│ - 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
│ - 6: 0000000000000000 0 OBJECT GLOBAL DEFAULT ABS LINUX_2.6.29
│ + 0: 00007f61075bf000 0 NOTYPE LOCAL DEFAULT UND
│ + 6: 00007f61075bf000 0 OBJECT GLOBAL DEFAULT ABS LINUX_2.6.29
Fixes: 2fa536d10797 ("linux-user: Add gen-vdso tool")
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-ID: <20241023202850.55211-1-iii@linux.ibm.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
2024-10-23 23:27:41 +03:00
|
|
|
elf32_process(outf, buf, total_len, need_bswap);
|
2021-06-18 00:30:09 +03:00
|
|
|
break;
|
|
|
|
case ELFCLASS64:
|
linux-user: Fix GDB complaining about system-supplied DSO string table index
When debugging qemu-user processes using gdbstub, the following warning
appears every time:
warning: BFD: warning: system-supplied DSO at 0x7f8253cc3000 has a corrupt string table index
The reason is that QEMU does not map the VDSO's section headers. The
VDSO's ELF header's e_shoff points to zeros, which GDB fails to parse.
The difference with the kernel's VDSO is that the latter is mapped as a
blob, ignoring program headers - which also don't cover the section
table. QEMU, on the other hand, loads it as an ELF file.
There appears to be no way to place section headers inside a section,
and, therefore, no way to refer to them from a linker script. Also, ld
hardcodes section headers to be non-loadable, see
_bfd_elf_assign_file_positions_for_non_load(). In theory ld could be
enhanced by implementing an "SHDRS" keyword in addition to the existing
"FILEHDR" and "PHDRS".
There are multiple ways to resolve the issue:
- Copy VDSO as a blob in load_elf_vdso(). This would require creating
specialized loader logic, that duplicates parts of load_elf_image().
- Fix up VDSO's PHDR size in load_elf_vdso(). This would require either
duplicating the parsing logic, or adding an ugly parameter to
load_elf_image().
- Fix up VDSO's PHDR size in gen-vdso. This is the simplest solution,
so do it.
There are two tricky parts:
- Byte-swaps need to be done either on local copies, or in-place and
then reverted in the end. To preserve the existing code structure, do
the former for Sym and Dyn, and the latter for Ehdr, Phdr, and Shdr.
- There must be no .bss, which is already the case - but having an
explicit check is helpful to ensure correctness.
To verify this change, I diffed the on-disk and the loaded VDSOs; the
result does not show anything unusual, except for what seems to be an
existing oversight (which should probably be fixed separately):
│ Symbol table '.dynsym' contains 8 entries:
│ Num: Value Size Type Bind Vis Ndx Name
│ - 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
│ - 6: 0000000000000000 0 OBJECT GLOBAL DEFAULT ABS LINUX_2.6.29
│ + 0: 00007f61075bf000 0 NOTYPE LOCAL DEFAULT UND
│ + 6: 00007f61075bf000 0 OBJECT GLOBAL DEFAULT ABS LINUX_2.6.29
Fixes: 2fa536d10797 ("linux-user: Add gen-vdso tool")
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-ID: <20241023202850.55211-1-iii@linux.ibm.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
2024-10-23 23:27:41 +03:00
|
|
|
elf64_process(outf, buf, total_len, need_bswap);
|
2021-06-18 00:30:09 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: invalid elf EI_CLASS (%u)\n",
|
|
|
|
inf_name, buf[EI_CLASS]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(outf, "};\n\n"); /* end vdso_relocs. */
|
|
|
|
|
linux-user: Fix GDB complaining about system-supplied DSO string table index
When debugging qemu-user processes using gdbstub, the following warning
appears every time:
warning: BFD: warning: system-supplied DSO at 0x7f8253cc3000 has a corrupt string table index
The reason is that QEMU does not map the VDSO's section headers. The
VDSO's ELF header's e_shoff points to zeros, which GDB fails to parse.
The difference with the kernel's VDSO is that the latter is mapped as a
blob, ignoring program headers - which also don't cover the section
table. QEMU, on the other hand, loads it as an ELF file.
There appears to be no way to place section headers inside a section,
and, therefore, no way to refer to them from a linker script. Also, ld
hardcodes section headers to be non-loadable, see
_bfd_elf_assign_file_positions_for_non_load(). In theory ld could be
enhanced by implementing an "SHDRS" keyword in addition to the existing
"FILEHDR" and "PHDRS".
There are multiple ways to resolve the issue:
- Copy VDSO as a blob in load_elf_vdso(). This would require creating
specialized loader logic, that duplicates parts of load_elf_image().
- Fix up VDSO's PHDR size in load_elf_vdso(). This would require either
duplicating the parsing logic, or adding an ugly parameter to
load_elf_image().
- Fix up VDSO's PHDR size in gen-vdso. This is the simplest solution,
so do it.
There are two tricky parts:
- Byte-swaps need to be done either on local copies, or in-place and
then reverted in the end. To preserve the existing code structure, do
the former for Sym and Dyn, and the latter for Ehdr, Phdr, and Shdr.
- There must be no .bss, which is already the case - but having an
explicit check is helpful to ensure correctness.
To verify this change, I diffed the on-disk and the loaded VDSOs; the
result does not show anything unusual, except for what seems to be an
existing oversight (which should probably be fixed separately):
│ Symbol table '.dynsym' contains 8 entries:
│ Num: Value Size Type Bind Vis Ndx Name
│ - 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
│ - 6: 0000000000000000 0 OBJECT GLOBAL DEFAULT ABS LINUX_2.6.29
│ + 0: 00007f61075bf000 0 NOTYPE LOCAL DEFAULT UND
│ + 6: 00007f61075bf000 0 OBJECT GLOBAL DEFAULT ABS LINUX_2.6.29
Fixes: 2fa536d10797 ("linux-user: Add gen-vdso tool")
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-ID: <20241023202850.55211-1-iii@linux.ibm.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
2024-10-23 23:27:41 +03:00
|
|
|
/*
|
|
|
|
* Write out the vdso image now, after we made local changes.
|
|
|
|
*/
|
|
|
|
fprintf(outf,
|
|
|
|
"static const uint8_t %s_image[] = {",
|
|
|
|
prefix);
|
|
|
|
for (long i = 0; i < total_len; ++i) {
|
|
|
|
if (i % 12 == 0) {
|
|
|
|
fputs("\n ", outf);
|
|
|
|
}
|
|
|
|
fprintf(outf, " 0x%02x,", buf[i]);
|
|
|
|
}
|
|
|
|
fprintf(outf, "\n};\n\n");
|
|
|
|
|
2021-06-18 00:30:09 +03:00
|
|
|
fprintf(outf, "static const VdsoImageInfo %s_image_info = {\n", prefix);
|
|
|
|
fprintf(outf, " .image = %s_image,\n", prefix);
|
|
|
|
fprintf(outf, " .relocs = %s_relocs,\n", prefix);
|
|
|
|
fprintf(outf, " .image_size = sizeof(%s_image),\n", prefix);
|
|
|
|
fprintf(outf, " .reloc_count = ARRAY_SIZE(%s_relocs),\n", prefix);
|
|
|
|
fprintf(outf, " .sigreturn_ofs = 0x%x,\n", sigreturn_addr);
|
|
|
|
fprintf(outf, " .rt_sigreturn_ofs = 0x%x,\n", rt_sigreturn_addr);
|
|
|
|
fprintf(outf, "};\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Everything should have gone well.
|
|
|
|
*/
|
|
|
|
if (fclose(outf)) {
|
|
|
|
goto perror_outf;
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
perror_inf:
|
|
|
|
perror(inf_name);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
perror_outf:
|
|
|
|
perror(outf_name);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|