aboutsummaryrefslogtreecommitdiff
path: root/libctf/ctf-create.c
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2019-07-06 17:36:21 +0100
committerNick Alcock <nick.alcock@oracle.com>2019-10-03 17:04:55 +0100
commitfd55eae84d8b94b497c6043e5aa6e111e7f5a225 (patch)
treebb17a5265cdaf3b0ede0e38ba1437597ea0433e7 /libctf/ctf-create.c
parent083114f8ba2baa16a48638ecbf41e333eedb738b (diff)
downloadgdb-fd55eae84d8b94b497c6043e5aa6e111e7f5a225.zip
gdb-fd55eae84d8b94b497c6043e5aa6e111e7f5a225.tar.gz
gdb-fd55eae84d8b94b497c6043e5aa6e111e7f5a225.tar.bz2
libctf: allow the header to change between versions
libctf supports dynamic upgrading of the type table as file format versions change, but before now has not supported changes to the CTF header. Doing this is complicated by the baroque storage method used: the CTF header is kept prepended to the rest of the CTF data, just as when read from the file, and written out from there, and is endian-flipped in place. This makes accessing it needlessly hard and makes it almost impossible to make the header larger if we add fields. The general storage machinery around the malloced ctf pointer (the 'ctf_base') is also overcomplicated: the pointer is sometimes malloced locally and sometimes assigned from a parameter, so freeing it requires checking to see if that parameter was used, needlessly coupling ctf_bufopen and ctf_file_close together. So split the header out into a new ctf_file_t.ctf_header, which is written out explicitly: squeeze it out of the CTF buffer whenever we reallocate it, and use ctf_file_t.ctf_buf to skip past the header when we do not need to reallocate (when no upgrading or endian-flipping is required). We now track whether the CTF base can be freed explicitly via a new ctf_dynbase pointer which is non-NULL only when freeing is possible. With all this done, we can upgrade the header on the fly and add new fields as desired, via a new upgrade_header function in ctf-open. As with other forms of upgrading, libctf upgrades older headers automatically to the latest supported version at open time. For a first use of this field, we add a new string field cth_cuname, and a corresponding setter/getter pair ctf_cuname_set and ctf_cuname: this is used by debuggers to determine whether a CTF section's types relate to a single compilation unit, or to all compilation units in the program. (Types with ambiguous definitions in different CUs have only one of these types placed in the top-level shared .ctf container: the rest are placed in much smaller per-CU containers, which have the shared container as their parent. Since CTF must be useful in the absence of DWARF, we store the names of the relevant CUs ourselves, so the debugger can look them up.) v5: fix tabdamage. include/ * ctf-api.h (ctf_cuname): New function. (ctf_cuname_set): Likewise. * ctf.h: Improve comment around upgrading, no longer implying that v2 is the target of upgrades (it is v3 now). (ctf_header_v2_t): New, old-format header for backward compatibility. (ctf_header_t): Add cth_cuname: this is the first of several header changes in format v3. libctf/ * ctf-impl.h (ctf_file_t): New fields ctf_header, ctf_dynbase, ctf_cuname, ctf_dyncuname: ctf_base and ctf_buf are no longer const. * ctf-open.c (ctf_set_base): Preserve the gap between ctf_buf and ctf_base: do not assume that it is always sizeof (ctf_header_t). Print out ctf_cuname: only print out ctf_parname if set. (ctf_free_base): Removed, ctf_base is no longer freed: free ctf_dynbase instead. (ctf_set_version): Fix spacing. (upgrade_header): New, in-place header upgrading. (upgrade_types): Rename to... (upgrade_types_v1): ... this. Free ctf_dynbase, not ctf_base. No longer track old and new headers separately. No longer allow for header sizes explicitly: squeeze the headers out on upgrade (they are preserved in fp->ctf_header). Set ctf_dynbase, ctf_base and ctf_buf explicitly. Use ctf_free, not ctf_free_base. (upgrade_types): New, also handle ctf_parmax updating. (flip_header): Flip ctf_cuname. (flip_types): Flip BUF explicitly rather than deriving BUF from BASE. (ctf_bufopen): Store the header in fp->ctf_header. Correct minimum required alignment of objtoff and funcoff. No longer store it in the ctf_buf unless that buf is derived unmodified from the input. Set ctf_dynbase where ctf_base is dynamically allocated. Drop locals that duplicate fields in ctf_file: move allocation of ctf_file further up instead. Call upgrade_header as needed. Move version-specific ctf_parmax initialization into upgrade_types. More concise error handling. (ctf_file_close): No longer test for null pointers before freeing. Free ctf_dyncuname, ctf_dynbase, and ctf_header. Do not call ctf_free_base. (ctf_cuname): New. (ctf_cuname_set): New. * ctf-create.c (ctf_update): Populate ctf_cuname. (ctf_gzwrite): Write out the header explicitly. Remove obsolescent comment. (ctf_write): Likewise. (ctf_compress_write): Get the header from ctf_header, not ctf_base. Fix the compression length: fp->ctf_size never counted the CTF header. Simplify the compress call accordingly.
Diffstat (limited to 'libctf/ctf-create.c')
-rw-r--r--libctf/ctf-create.c52
1 files changed, 38 insertions, 14 deletions
diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c
index 24ea114..cef2351 100644
--- a/libctf/ctf-create.c
+++ b/libctf/ctf-create.c
@@ -294,6 +294,8 @@ ctf_update (ctf_file_t *fp)
hdrp = (ctf_header_t *) buf;
if ((fp->ctf_flags & LCTF_CHILD) && (fp->ctf_parname != NULL))
ctf_str_add_ref (fp, fp->ctf_parname, &hdrp->cth_parname);
+ if (fp->ctf_cuname != NULL)
+ ctf_str_add_ref (fp, fp->ctf_cuname, &hdrp->cth_cuname);
/* Work over the variable list, translating everything into ctf_varent_t's and
prepping the string table. */
@@ -443,7 +445,8 @@ ctf_update (ctf_file_t *fp)
nfp->ctf_refcnt = fp->ctf_refcnt;
nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
- nfp->ctf_data.cts_data = NULL; /* Force ctf_free() on close. */
+ if (nfp->ctf_dynbase == NULL)
+ nfp->ctf_dynbase = buf; /* Make sure buf is freed on close. */
nfp->ctf_dthash = fp->ctf_dthash;
nfp->ctf_dtdefs = fp->ctf_dtdefs;
nfp->ctf_dtbyname = fp->ctf_dtbyname;
@@ -1919,15 +1922,26 @@ ctf_add_type (ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
return dst_type;
}
-/* Write the compressed CTF data stream to the specified gzFile descriptor.
- This is useful for saving the results of dynamic CTF containers. */
+/* Write the compressed CTF data stream to the specified gzFile descriptor. */
int
ctf_gzwrite (ctf_file_t *fp, gzFile fd)
{
- const unsigned char *buf = fp->ctf_base;
- ssize_t resid = fp->ctf_size;
+ const unsigned char *buf;
+ ssize_t resid;
ssize_t len;
+ resid = sizeof (ctf_header_t);
+ buf = (unsigned char *) fp->ctf_header;
+ while (resid != 0)
+ {
+ if ((len = gzwrite (fd, buf, resid)) <= 0)
+ return (ctf_set_errno (fp, errno));
+ resid -= len;
+ buf += len;
+ }
+
+ resid = fp->ctf_size;
+ buf = fp->ctf_buf;
while (resid != 0)
{
if ((len = gzwrite (fd, buf, resid)) <= 0)
@@ -1950,12 +1964,12 @@ ctf_compress_write (ctf_file_t *fp, int fd)
ctf_header_t *hp = &h;
ssize_t header_len = sizeof (ctf_header_t);
ssize_t compress_len;
- size_t max_compress_len = compressBound (fp->ctf_size - header_len);
+ size_t max_compress_len = compressBound (fp->ctf_size);
ssize_t len;
int rc;
int err = 0;
- memcpy (hp, fp->ctf_base, header_len);
+ memcpy (hp, fp->ctf_header, header_len);
hp->cth_flags |= CTF_F_COMPRESS;
if ((buf = ctf_alloc (max_compress_len)) == NULL)
@@ -1963,8 +1977,7 @@ ctf_compress_write (ctf_file_t *fp, int fd)
compress_len = max_compress_len;
if ((rc = compress (buf, (uLongf *) &compress_len,
- fp->ctf_base + header_len,
- fp->ctf_size - header_len)) != Z_OK)
+ fp->ctf_buf, fp->ctf_size)) != Z_OK)
{
ctf_dprintf ("zlib deflate err: %s\n", zError (rc));
err = ctf_set_errno (fp, ECTF_COMPRESS);
@@ -2000,18 +2013,29 @@ ret:
return err;
}
-/* Write the uncompressed CTF data stream to the specified file descriptor.
- This is useful for saving the results of dynamic CTF containers. */
+/* Write the uncompressed CTF data stream to the specified file descriptor. */
int
ctf_write (ctf_file_t *fp, int fd)
{
- const unsigned char *buf = fp->ctf_base;
- ssize_t resid = fp->ctf_size;
+ const unsigned char *buf;
+ ssize_t resid;
ssize_t len;
+ resid = sizeof (ctf_header_t);
+ buf = (unsigned char *) fp->ctf_header;
+ while (resid != 0)
+ {
+ if ((len = write (fd, buf, resid)) <= 0)
+ return (ctf_set_errno (fp, errno));
+ resid -= len;
+ buf += len;
+ }
+
+ resid = fp->ctf_size;
+ buf = fp->ctf_buf;
while (resid != 0)
{
- if ((len = write (fd, buf, resid)) < 0)
+ if ((len = write (fd, buf, resid)) <= 0)
return (ctf_set_errno (fp, errno));
resid -= len;
buf += len;