Skip to contentSkip to conversation

linux-user: Support gdb 'qOffsets' query for ELF

PATCH Applied Josh Kunz via Qemu-devel 4 participants 8 messages Aug 17, 2019 – Sep 10, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Support gdb 'qOffsets' query for ELF

This is needed to support debugging PIE ELF binaries running under QEMU user mode. Currently, code_offset and data_offset remain unset for all ELF binaries, so GDB is unable to correctly locate the position of the binary's text and data.

The fields code_offset, and data_offset were originally added way back in 2006 to support debugging of bFMT executables (978efd6aac6), and support was just never added for ELF. Since non-PIE binaries are loaded at exactly the address specified in the binary, GDB does not need to relocate any symbols, so the buggy behavior is not normally observed.

---
 linux-user/elfload.c | 2 ++
 1 file changed, 2 insertions(+)
linux-user/elfload.c +2 -0
index 3365e192eb..ceac035208 100644
@@ -2380,6 +2380,8 @@ static void load_elf_image(const char *image_name, int image_fd,
     }
 
     info->load_bias = load_bias;
+    info->code_offset = load_bias;
+    info->data_offset = load_bias;
     info->load_addr = load_addr;
     info->entry = ehdr->e_entry + load_bias;
     info->start_code = -1;
Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#1 lore raw

+cc: [email protected], I typoed the email on the first go.

Quoted 35 lines · Josh Kunz

This is needed to support debugging PIE ELF binaries running under QEMU user mode. Currently, code_offset and data_offset remain unset for all ELF binaries, so GDB is unable to correctly locate the position of the binary's text and data.

The fields code_offset, and data_offset were originally added way back in 2006 to support debugging of bFMT executables (978efd6aac6), and support was just never added for ELF. Since non-PIE binaries are loaded at exactly the address specified in the binary, GDB does not need to relocate any symbols, so the buggy behavior is not normally observed.

---
 linux-user/elfload.c | 2 ++
 1 file changed, 2 insertions(+)
linux-user/elfload.c +2 -0
index 3365e192eb..ceac035208 100644
@@ -2380,6 +2380,8 @@ static void load_elf_image(const char *image_name,
 int image_fd,
     }
 
     info->load_bias = load_bias;
+    info->code_offset = load_bias;
+    info->data_offset = load_bias;
     info->load_addr = load_addr;
     info->entry = ehdr->e_entry + load_bias;
     info->start_code = -1;

--
2.23.0.rc1.153.gdeed80330f-goog

#2 lore raw
In reply to Josh Kunz via Qemu-devel
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Subject: [Qemu-devel] [PATCH] linux-user: Support gdb 'qOffsets' query for ELF
Message-id: [email protected]

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Submodule 'capstone' (https://git.qemu.org/git/capstone.git) registered for path 'capstone'
Submodule 'dtc' (https://git.qemu.org/git/dtc.git) registered for path 'dtc'
Submodule 'roms/QemuMacDrivers' (https://git.qemu.org/git/QemuMacDrivers.git) registered for path 'roms/QemuMacDrivers' Submodule 'roms/SLOF' (https://git.qemu.org/git/SLOF.git) registered for path 'roms/SLOF'
Submodule 'roms/edk2' (https://git.qemu.org/git/edk2.git) registered for path 'roms/edk2'
Submodule 'roms/ipxe' (https://git.qemu.org/git/ipxe.git) registered for path 'roms/ipxe'
Submodule 'roms/openbios' (https://git.qemu.org/git/openbios.git) registered for path 'roms/openbios'
Submodule 'roms/openhackware' (https://git.qemu.org/git/openhackware.git) registered for path 'roms/openhackware'
Submodule 'roms/opensbi' (https://git.qemu.org/git/opensbi.git) registered for path 'roms/opensbi'
Submodule 'roms/qemu-palcode' (https://git.qemu.org/git/qemu-palcode.git) registered for path 'roms/qemu-palcode'
Submodule 'roms/seabios' (https://git.qemu.org/git/seabios.git/) registered for path 'roms/seabios'
Submodule 'roms/seabios-hppa' (https://git.qemu.org/git/seabios-hppa.git) registered for path 'roms/seabios-hppa'
Submodule 'roms/sgabios' (https://git.qemu.org/git/sgabios.git) registered for path 'roms/sgabios'
Submodule 'roms/skiboot' (https://git.qemu.org/git/skiboot.git) registered for path 'roms/skiboot'
Submodule 'roms/u-boot' (https://git.qemu.org/git/u-boot.git) registered for path 'roms/u-boot'
Submodule 'roms/u-boot-sam460ex' (https://git.qemu.org/git/u-boot-sam460ex.git) registered for path 'roms/u-boot-sam460ex' Submodule 'slirp' (https://git.qemu.org/git/libslirp.git) registered for path 'slirp'
Submodule 'tests/fp/berkeley-softfloat-3' (https://git.qemu.org/git/berkeley-softfloat-3.git) registered for path 'tests/fp/berkeley-softfloat-3' Submodule 'tests/fp/berkeley-testfloat-3' (https://git.qemu.org/git/berkeley-testfloat-3.git) registered for path 'tests/fp/berkeley-testfloat-3' Submodule 'ui/keycodemapdb' (https://git.qemu.org/git/keycodemapdb.git) registered for path 'ui/keycodemapdb'
Cloning into 'capstone'...
Submodule path 'capstone': checked out '22ead3e0bfdb87516656453336160e0a37b066bf'
Cloning into 'dtc'...
Submodule path 'dtc': checked out '88f18909db731a627456f26d779445f84e449536'
Cloning into 'roms/QemuMacDrivers'...
Submodule path 'roms/QemuMacDrivers': checked out '90c488d5f4a407342247b9ea869df1c2d9c8e266'
Cloning into 'roms/SLOF'...
Submodule path 'roms/SLOF': checked out 'ba1ab360eebe6338bb8d7d83a9220ccf7e213af3'
Cloning into 'roms/edk2'...
Submodule path 'roms/edk2': checked out '20d2e5a125e34fc8501026613a71549b2a1a3e54'
Submodule 'SoftFloat' (https://github.com/ucb-bar/berkeley-softfloat-3.git) registered for path 'ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3' Submodule 'CryptoPkg/Library/OpensslLib/openssl' (https://github.com/openssl/openssl) registered for path 'CryptoPkg/Library/OpensslLib/openssl' Cloning into 'ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3'...
Submodule path 'roms/edk2/ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3': checked out 'b64af41c3276f97f0e181920400ee056b9c88037' Cloning into 'CryptoPkg/Library/OpensslLib/openssl'...
Submodule path 'roms/edk2/CryptoPkg/Library/OpensslLib/openssl': checked out '50eaac9f3337667259de725451f201e784599687' Submodule 'boringssl' (https://boringssl.googlesource.com/boringssl) registered for path 'boringssl'
Submodule 'krb5' (https://github.com/krb5/krb5) registered for path 'krb5'
Submodule 'pyca.cryptography' (https://github.com/pyca/cryptography.git) registered for path 'pyca-cryptography'
Cloning into 'boringssl'...
Submodule path 'roms/edk2/CryptoPkg/Library/OpensslLib/openssl/boringssl': checked out '2070f8ad9151dc8f3a73bffaa146b5e6937a583f' Cloning into 'krb5'...
Submodule path 'roms/edk2/CryptoPkg/Library/OpensslLib/openssl/krb5': checked out 'b9ad6c49505c96a088326b62a52568e3484f2168' Cloning into 'pyca-cryptography'...
Submodule path 'roms/edk2/CryptoPkg/Library/OpensslLib/openssl/pyca-cryptography': checked out '09403100de2f6f1cdd0d484dcb8e620f1c335c8f' Cloning into 'roms/ipxe'...
Submodule path 'roms/ipxe': checked out 'de4565cbe76ea9f7913a01f331be3ee901bb6e17'
Cloning into 'roms/openbios'...
Submodule path 'roms/openbios': checked out 'c79e0ecb84f4f1ee3f73f521622e264edd1bf174'
Cloning into 'roms/openhackware'...
Submodule path 'roms/openhackware': checked out 'c559da7c8eec5e45ef1f67978827af6f0b9546f5'
Cloning into 'roms/opensbi'...
Submodule path 'roms/opensbi': checked out 'ce228ee0919deb9957192d723eecc8aaae2697c6'
Cloning into 'roms/qemu-palcode'...
Submodule path 'roms/qemu-palcode': checked out 'bf0e13698872450164fa7040da36a95d2d4b326f'
Cloning into 'roms/seabios'...
Submodule path 'roms/seabios': checked out 'a5cab58e9a3fb6e168aba919c5669bea406573b4'
Cloning into 'roms/seabios-hppa'...
Submodule path 'roms/seabios-hppa': checked out '0f4fe84658165e96ce35870fd19fc634e182e77b'
Cloning into 'roms/sgabios'...
Submodule path 'roms/sgabios': checked out 'cbaee52287e5f32373181cff50a00b6c4ac9015a'
Cloning into 'roms/skiboot'...
Submodule path 'roms/skiboot': checked out '261ca8e779e5138869a45f174caa49be6a274501'
Cloning into 'roms/u-boot'...
Submodule path 'roms/u-boot': checked out 'd3689267f92c5956e09cc7d1baa4700141662bff'
Cloning into 'roms/u-boot-sam460ex'...
Submodule path 'roms/u-boot-sam460ex': checked out '60b3916f33e617a815973c5a6df77055b2e3a588'
Cloning into 'slirp'...
Submodule path 'slirp': checked out '126c04acbabd7ad32c2b018fe10dfac2a3bc1210'
Cloning into 'tests/fp/berkeley-softfloat-3'...
Submodule path 'tests/fp/berkeley-softfloat-3': checked out 'b64af41c3276f97f0e181920400ee056b9c88037'
Cloning into 'tests/fp/berkeley-testfloat-3'...
Submodule path 'tests/fp/berkeley-testfloat-3': checked out '5a59dcec19327396a011a17fd924aed4fec416b3'
Cloning into 'ui/keycodemapdb'...
Submodule path 'ui/keycodemapdb': checked out '6b3d716e2b6472eb7189d3220552280ef3d832ce'
Switched to a new branch 'test'
6e270bf linux-user: Support gdb 'qOffsets' query for ELF

=== OUTPUT BEGIN ===
ERROR: Author email address is mangled by the mailing list #2:
Author: Josh Kunz via Qemu-devel <[email protected]>

total: 1 errors, 0 warnings, 8 lines checked

Commit 6e270bf43044 (linux-user: Support gdb 'qOffsets' query for ELF) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#3 lore raw
In reply to Josh Kunz via Qemu-devel

Le 17/08/2019 à 01:34, Josh Kunz via Qemu-devel a écrit :

Quoted 11 lines

This is needed to support debugging PIE ELF binaries running under QEMU user mode. Currently, code_offset and data_offset remain unset for all ELF binaries, so GDB is unable to correctly locate the position of the binary's text and data.

The fields code_offset, and data_offset were originally added way back in 2006 to support debugging of bFMT executables (978efd6aac6), and support was just never added for ELF. Since non-PIE binaries are loaded at exactly the address specified in the binary, GDB does not need to relocate any symbols, so the buggy behavior is not normally observed.

Quoted 5 lines
---
 linux-user/elfload.c | 2 ++
 1 file changed, 2 insertions(+)

As it seems they are text and data segment offsets, why it's not based on info->start_code and info->start_data?

Thanks,
Laurent

#4 lore raw

The Data and Code flags in qOffsets are actually section offsets rather than segment offsets. GDB relocates the symbols in those sections relative to their location in the binary. So we have to use load_bias.

See here for a more detailed description:
https://sourceware.org/gdb/onlinedocs/gdb/General-Query-Packets.html#General-Query-Packets

Quoted 24 lines · Laurent Vivier
Quoted 11 lines · Josh Kunz via Qemu-devel

This is needed to support debugging PIE ELF binaries running under QEMU user mode. Currently, code_offset and data_offset remain unset for all ELF binaries, so GDB is unable to correctly locate the position of the binary's text and data.

The fields code_offset, and data_offset were originally added way back in 2006 to support debugging of bFMT executables (978efd6aac6), and support was just never added for ELF. Since non-PIE binaries are loaded at exactly the address specified in the binary, GDB does not need to relocate any symbols, so the buggy behavior is not normally observed.

Quoted 5 lines
---
 linux-user/elfload.c | 2 ++
 1 file changed, 2 insertions(+)

As it seems they are text and data segment offsets, why it's not based on info->start_code and info->start_data?

Thanks,
Laurent

Reviewed-by
#5 lore raw

Le 03/09/2019 à 21:19, Josh Kunz a écrit :

Quoted 7 lines

The Data and Code flags in qOffsets are actually section offsets rather than segment offsets. GDB relocates the symbols in those sections relative to their location in the binary. So we have to use load_bias.

See here for a more detailed
description: https://sourceware.org/gdb/onlinedocs/gdb/General-Query-Packets.html#General-Query-Packets

Thank you for the details.

#6 lore raw
In reply to Josh Kunz via Qemu-devel

Cc'ing Alex.

Quoted 6 lines · Josh Kunz via Qemu-devel

The Data and Code flags in qOffsets are actually section offsets rather than segment offsets. GDB relocates the symbols in those sections relative to their location in the binary. So we have to use load_bias.

See here for a more detailed description:
https://sourceware.org/gdb/onlinedocs/gdb/General-Query-Packets.html#General-Query-Packets

Maybe we can amend a reference to
http://sourceware.org/gdb/onlinedocs/gdb/General-Query-Packets.html#index-qOffsets-packet
in the commit.

Quoted 26 lines · Laurent Vivier
Quoted 25 lines · Josh Kunz via Qemu-devel
Quoted 11 lines

This is needed to support debugging PIE ELF binaries running under QEMU user mode. Currently, code_offset and data_offset remain unset for all ELF binaries, so GDB is unable to correctly locate the position of the binary's text and data.

The fields code_offset, and data_offset were originally added way back in 2006 to support debugging of bFMT executables (978efd6aac6), and support was just never added for ELF. Since non-PIE binaries are loaded at exactly the address specified in the binary, GDB does not need to relocate any symbols, so the buggy behavior is not normally observed.

Quoted 5 lines
---
 linux-user/elfload.c | 2 ++
 1 file changed, 2 insertions(+)

As it seems they are text and data segment offsets, why it's not based on info->start_code and info->start_data?

Thanks,
Laurent

#7 lore raw
In reply to Josh Kunz via Qemu-devel

Le 17/08/2019 à 01:34, Josh Kunz via Qemu-devel a écrit :

Quoted 23 lines

This is needed to support debugging PIE ELF binaries running under QEMU user mode. Currently, code_offset and data_offset remain unset for all ELF binaries, so GDB is unable to correctly locate the position of the binary's text and data.

The fields code_offset, and data_offset were originally added way back in 2006 to support debugging of bFMT executables (978efd6aac6), and support was just never added for ELF. Since non-PIE binaries are loaded at exactly the address specified in the binary, GDB does not need to relocate any symbols, so the buggy behavior is not normally observed.

---
 linux-user/elfload.c | 2 ++
 1 file changed, 2 insertions(+)
linux-user/elfload.c +0 -0
index 3365e192eb..ceac035208 100644

linux-user/elfload.c static void load_elf_image(const char *image_name, int image_fd, · line 2387

Quoted 8 lines
linux-user/elfload.c +2 -0
@@ -2380,6 +2380,8 @@ static void load_elf_image(const char *image_name, int image_fd,
     }
 
     info->load_bias = load_bias;
+    info->code_offset = load_bias;
+    info->data_offset = load_bias;
     info->load_addr = load_addr;
     info->entry = ehdr->e_entry + load_bias;
     info->start_code = -1;

Applied to my linux-user branch.

Thanks,
Laurent