diff options
author | Nick Alcock <nick.alcock@oracle.com> | 2019-07-13 20:40:52 +0100 |
---|---|---|
committer | Nick Alcock <nick.alcock@oracle.com> | 2019-10-03 17:04:55 +0100 |
commit | 5537f9b9a3c46c3898e274a52f6bb0eb8d293bb8 (patch) | |
tree | 3d1ac035f2c596d2fe567637f99f7380faab4ca6 /libctf/ctf-create.c | |
parent | d851ecd373a3764581372b10be5b74c9ee98ae08 (diff) | |
download | gdb-5537f9b9a3c46c3898e274a52f6bb0eb8d293bb8.zip gdb-5537f9b9a3c46c3898e274a52f6bb0eb8d293bb8.tar.gz gdb-5537f9b9a3c46c3898e274a52f6bb0eb8d293bb8.tar.bz2 |
libctf: write CTF files to memory, and CTF archives to fds
Before now, we've been able to write CTF files to gzFile descriptors or
fds, and CTF archives to named files only.
Make this a bit less irregular by allowing CTF archives to be written
to fds with the new function ctf_arc_write_fd: also allow CTF
files to be written to a new memory buffer via ctf_write_mem.
(It would be nice to complete things by adding a new function to write
CTF archives to memory, but this is too difficult to do given the short
time the linker is expected to be writing them out: we will transition
to a better format in format v4, though we will always support reading
CTF archives that are stored in .ctf sections.)
include/
* ctf-api.h (ctf_arc_write_fd): New.
(ctf_write_mem): Likewise.
(ctf_gzwrite): Spacing fix.
libctf/
* ctf-archive.c (ctf_arc_write): Split off, and reimplement in terms
of...
(ctf_arc_write_fd): ... this new function.
* ctf-create.c (ctf_write_mem): New.
Diffstat (limited to 'libctf/ctf-create.c')
-rw-r--r-- | libctf/ctf-create.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c index 3e6a319..09cd8d5 100644 --- a/libctf/ctf-create.c +++ b/libctf/ctf-create.c @@ -2029,6 +2029,57 @@ ret: return err; } +/* Optionally compress the specified CTF data stream and return it as a new + dynamically-allocated string. */ +unsigned char * +ctf_write_mem (ctf_file_t *fp, size_t *size, size_t threshold) +{ + unsigned char *buf; + unsigned char *bp; + ctf_header_t *hp; + ssize_t header_len = sizeof (ctf_header_t); + ssize_t compress_len; + size_t max_compress_len = compressBound (fp->ctf_size); + int rc; + + if (fp->ctf_size < threshold) + max_compress_len = fp->ctf_size; + if ((buf = malloc (max_compress_len + + sizeof (struct ctf_header))) == NULL) + { + ctf_set_errno (fp, ENOMEM); + return NULL; + } + + hp = (ctf_header_t *) buf; + memcpy (hp, fp->ctf_header, header_len); + bp = buf + sizeof (struct ctf_header); + *size = sizeof (struct ctf_header); + + compress_len = max_compress_len; + + if (fp->ctf_size < threshold) + { + hp->cth_flags &= ~CTF_F_COMPRESS; + memcpy (bp, fp->ctf_buf, fp->ctf_size); + *size += fp->ctf_size; + } + else + { + hp->cth_flags |= CTF_F_COMPRESS; + if ((rc = compress (bp, (uLongf *) &compress_len, + fp->ctf_buf, fp->ctf_size)) != Z_OK) + { + ctf_dprintf ("zlib deflate err: %s\n", zError (rc)); + ctf_set_errno (fp, ECTF_COMPRESS); + ctf_free (buf); + return NULL; + } + *size += compress_len; + } + return buf; +} + /* Write the uncompressed CTF data stream to the specified file descriptor. */ int ctf_write (ctf_file_t *fp, int fd) |