DEFINE_NOTE puts the data of a note after the packed 20-byte header.
When the data is 8-byte aligned, as lwpstatus_t, pstatus_t, psinfo_t
and auxv_t are on x86_64, the compiler adds 4 bytes of padding in
between, and write_note wrote them to the file along with the header.
The contents of each of these notes were shifted by 4 bytes and the
last 4 bytes were cut off. gdb then reads the same id for every thread
and fails with an internal error in get_thread_regcache.
Write the header and the data separately. On i386 there is no padding,
so the output is the same there.
Also advance the offset after a partial write (the old loop wrote every
chunk at the same offset), write nothing when the offset is already
past the core limit instead of computing a negative size, and return
EIO when io_write writes nothing instead of looping forever.
---
v2: return EIO when io_write writes nothing, instead of looping
(Alperen Erkan).
About the core limits: nothing passes one today. The crash server is
dump_core's only caller and always passes (off_t) -1 ("XXX should get
core limit in RPC"); gcore goes through the crash server too. So I
tested write_bytes and write_note on their own: the code from this
patch, copied into a small program with a fake io_write, writing a note
with a 20-byte header and 24 bytes of 8-byte-aligned data. With the
limit inside the header, at the header/data boundary, inside the data,
at the end of the note, at its start and before it, with io_write
writing all or only 3 or 7 bytes per call, exactly the bytes below the
limit get written, and the offset ends at or past the limit, which is
what makes dump_core stop. With an io_write that keeps writing 0
bytes, v1 loops and v2 returns EIO.
Rerun in QEMU on Guix System x86_64 (Hurd 6290b4c plus v2): the crash
server writes the same notes as with v1, apart from the pids, times and
other values that change between runs. The gdb side is on gdb-patches:
https://sourceware.org/pipermail/gdb-patches/2026-November/232199.html
exec/elfcore.c | 29 ++++++++++++++++++++---------
1 file changed, 20 insertions(+), 9 deletions(-)
diff --git a/exec/elfcore.c b/exec/elfcore.c
index 4b2afdf1..262842ce 100644
--- a/exec/elfcore.c
+++ b/exec/elfcore.c
@@ -276,7 +276,7 @@ dump_core (task_t task, file_t file, off_t corelimit,
#define DEFINE_NOTE(typename) struct { struct note_header hdr; typename data; }
#define WRITE_NOTE(type, var) ({ \
(var).hdr = NOTE_HEADER ((type), sizeof (var).data); \
- write_note (&(var).hdr); \
+ write_note (&(var).hdr, &(var).data);
\
})
struct note_header
{
@@ -285,26 +285,37 @@ dump_core (task_t task, file_t file, off_t corelimit,
} __attribute__ ((packed));
#define NOTE_HEADER(type, size) \
((struct note_header) { { sizeof "CORE", (size), (type) }, "CORE" })
- inline error_t write_note (struct note_header *hdr)
+ /* Write SIZE bytes from DATA at OFFSET, within CORELIMIT, and advance
+ OFFSET. */
+ inline error_t write_bytes (const void *data, size_t size)
{
- error_t err = 0;
- char *data = (char *) hdr;
- size_t size = sizeof *hdr + hdr->note.n_descsz;
+ const char *p = data;
if (corelimit >= 0 && offset + size > corelimit)
- size = corelimit - offset;
+ size = corelimit > offset ? corelimit - offset : 0;
while (size > 0)
{
- err = io_write (file, data, size, offset, &wrote);
+ error_t err = io_write (file, p, size, offset, &wrote);
if (err)
return err;
if (wrote > size)
return EGRATUITOUS;
- data += wrote;
+ if (wrote == 0)
+ return EIO;
+ p += wrote;
+ offset += wrote;
size -= wrote;
}
- offset = (offset + wrote + 3) &~ 3; /* Pad it to word alignment. */
return 0;
}
+ /* DEFINE_NOTE can have padding between the header and the data (4 bytes
+ on x86_64), which must not go into the file. */
+ inline error_t write_note (struct note_header *hdr, const void *desc)
+ {
+ error_t err = write_bytes (hdr, sizeof *hdr)
+ ?: write_bytes (desc, hdr->note.n_descsz);
+ offset = (offset + 3) &~ 3; /* Pad it to word alignment. */
+ return err;
+ }
struct vm_region_list
{
--
2.56.0