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: 2fa536d107 ("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>
This commit is contained in:
Ilya Leoshkevich 2024-10-23 22:27:41 +02:00 committed by Richard Henderson
parent c20d09ebee
commit 6e9dcfb906
2 changed files with 87 additions and 51 deletions

View file

@ -131,23 +131,6 @@ int main(int argc, char **argv)
}
fclose(inf);
/*
* Write out the vdso image now, before we make local changes.
*/
fprintf(outf,
"/* Automatically generated from linux-user/gen-vdso.c. */\n"
"\n"
"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");
/*
* Identify which elf flavor we're processing.
* The first 16 bytes of the file are e_ident.
@ -179,14 +162,17 @@ int main(int argc, char **argv)
* Output relocation addresses as we go.
*/
fprintf(outf, "static const unsigned %s_relocs[] = {\n", prefix);
fprintf(outf,
"/* Automatically generated by linux-user/gen-vdso.c. */\n"
"\n"
"static const unsigned %s_relocs[] = {\n", prefix);
switch (buf[EI_CLASS]) {
case ELFCLASS32:
elf32_process(outf, buf, need_bswap);
elf32_process(outf, buf, total_len, need_bswap);
break;
case ELFCLASS64:
elf64_process(outf, buf, need_bswap);
elf64_process(outf, buf, total_len, need_bswap);
break;
default:
fprintf(stderr, "%s: invalid elf EI_CLASS (%u)\n",
@ -196,6 +182,20 @@ int main(int argc, char **argv)
fprintf(outf, "};\n\n"); /* end vdso_relocs. */
/*
* 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");
fprintf(outf, "static const VdsoImageInfo %s_image_info = {\n", prefix);
fprintf(outf, " .image = %s_image,\n", prefix);
fprintf(outf, " .relocs = %s_relocs,\n", prefix);