X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=libctf%2Fctf-open.c;h=8fc854ae6704f9f386db6bc5fbed3327964b08fa;hb=refs%2Fheads%2Fsandbox%2Fvbarinov%2Fupstream233;hp=8c6294a89becaf22519bad13e92bcdcf2c40125d;hpb=9402cc593f4aa54677203efa9a92c4f28d3033eb;p=external%2Fbinutils.git diff --git a/libctf/ctf-open.c b/libctf/ctf-open.c index 8c6294a..8fc854a 100644 --- a/libctf/ctf-open.c +++ b/libctf/ctf-open.c @@ -150,8 +150,7 @@ get_vbytes_common (unsigned short kind, ssize_t size _libctf_unused_, case CTF_K_FLOAT: return (sizeof (uint32_t)); case CTF_K_SLICE: - return (offsetof (ctf_slice_t, cts_bits) + - sizeof (((ctf_slice_t *)0)->cts_bits)); + return (sizeof (ctf_slice_t)); case CTF_K_ENUM: return (sizeof (ctf_enum_t) * vlen); case CTF_K_FORWARD: @@ -345,24 +344,17 @@ ctf_set_base (ctf_file_t *fp, const ctf_header_t *hp, void *base) /* Free a ctf_base pointer: the pointer passed, or (if NULL) fp->ctf_base. */ static void -ctf_free_base (ctf_file_t *fp, unsigned char *ctf_base, size_t ctf_size) +ctf_free_base (ctf_file_t *fp, unsigned char *ctf_base) { unsigned char *base; - size_t size; if (ctf_base) - { base = ctf_base; - size = ctf_size; - } else - { base = (unsigned char *) fp->ctf_base; - size = fp->ctf_size; - } if (base != fp->ctf_data.cts_data && base != NULL) - ctf_data_free (base, size); + ctf_free (base); } /* Set the version of the CTF file. */ @@ -392,7 +384,6 @@ upgrade_types (ctf_file_t *fp, ctf_header_t *cth) const ctf_type_v1_t *tbuf; const ctf_type_v1_t *tend; unsigned char *ctf_base, *old_ctf_base = (unsigned char *) fp->ctf_base; - size_t old_ctf_size = fp->ctf_size; ctf_type_t *t2buf; ssize_t increase = 0, size, increment, v2increment, vbytes, v2bytes; @@ -439,7 +430,7 @@ upgrade_types (ctf_file_t *fp, ctf_header_t *cth) version number unchanged, so that LCTF_INFO_* still works on the as-yet-untranslated type info. */ - if ((ctf_base = ctf_data_alloc (fp->ctf_size + increase)) == NULL) + if ((ctf_base = ctf_alloc (fp->ctf_size + increase)) == NULL) return ECTF_ZALLOC; memcpy (ctf_base, fp->ctf_base, sizeof (ctf_header_t) + cth->cth_typeoff); @@ -503,7 +494,7 @@ upgrade_types (ctf_file_t *fp, ctf_header_t *cth) case CTF_K_UNION: case CTF_K_ENUM: case CTF_K_UNKNOWN: - if (size <= CTF_MAX_SIZE) + if ((size_t) size <= CTF_MAX_SIZE) t2p->ctt_size = size; else { @@ -608,7 +599,7 @@ upgrade_types (ctf_file_t *fp, ctf_header_t *cth) assert ((size_t) t2p - (size_t) fp->ctf_buf == new_cth->cth_stroff); ctf_set_version (fp, (ctf_header_t *) ctf_base, CTF_VERSION_1_UPGRADED_3); - ctf_free_base (fp, old_ctf_base, old_ctf_size); + ctf_free_base (fp, old_ctf_base); memcpy (cth, new_cth, sizeof (ctf_header_t)); return 0; @@ -864,6 +855,10 @@ init_types (ctf_file_t *fp, ctf_header_t *cth) if (err != 0 && err != ECTF_STRTAB) return err; break; + default: + ctf_dprintf ("unhandled CTF kind in endianness conversion -- %x\n", + kind); + return ECTF_CORRUPT; } *xp = (uint32_t) ((uintptr_t) tp - (uintptr_t) fp->ctf_buf); @@ -1170,10 +1165,7 @@ ctf_file_t *ctf_simple_open (const char *ctfsect, size_t ctfsect_size, ctf_sect_t *strsectp = NULL; skeleton.cts_name = _CTF_SECTION; - skeleton.cts_type = SHT_PROGBITS; - skeleton.cts_flags = 0; skeleton.cts_entsize = 1; - skeleton.cts_offset = 0; if (ctfsect) { @@ -1215,7 +1207,7 @@ ctf_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, const ctf_preamble_t *pp; ctf_header_t hp; ctf_file_t *fp; - void *buf, *base; + void *base; size_t size, hdrsz; int foreign_endian = 0; int err; @@ -1286,6 +1278,9 @@ ctf_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, if (foreign_endian) flip_header (&hp); + ctf_dprintf ("header offsets: %x/%x/%x/%x/%x/%x/%x\n", + hp.cth_lbloff, hp.cth_objtoff, hp.cth_funcoff, hp.cth_varoff, + hp.cth_typeoff, hp.cth_stroff, hp.cth_strlen); hdrsz = sizeof (ctf_header_t); size = hp.cth_stroff + hp.cth_strlen; @@ -1317,11 +1312,13 @@ ctf_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, if (hp.cth_flags & CTF_F_COMPRESS) { - size_t srclen, dstlen; + size_t srclen; + uLongf dstlen; const void *src; int rc = Z_OK; + void *buf; - if ((base = ctf_data_alloc (size + hdrsz)) == NULL) + if ((base = ctf_alloc (size + hdrsz)) == NULL) return (ctf_set_open_errno (errp, ECTF_ZALLOC)); memcpy (base, ctfsect->cts_data, hdrsz); @@ -1335,29 +1332,33 @@ ctf_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, if ((rc = uncompress (buf, &dstlen, src, srclen)) != Z_OK) { ctf_dprintf ("zlib inflate err: %s\n", zError (rc)); - ctf_data_free (base, size + hdrsz); + free (base); return (ctf_set_open_errno (errp, ECTF_DECOMPRESS)); } - if (dstlen != size) + if ((size_t) dstlen != size) { ctf_dprintf ("zlib inflate short -- got %lu of %lu " "bytes\n", (unsigned long) dstlen, (unsigned long) size); - ctf_data_free (base, size + hdrsz); + free (base); return (ctf_set_open_errno (errp, ECTF_CORRUPT)); } } else if (foreign_endian) { - if ((base = ctf_data_alloc (size + hdrsz)) == NULL) + if ((base = ctf_alloc (size + hdrsz)) == NULL) return (ctf_set_open_errno (errp, ECTF_ZALLOC)); + memcpy (base, ctfsect->cts_data, size + hdrsz); } else - { - base = (void *) ctfsect->cts_data; - buf = (unsigned char *) base + hdrsz; - } + base = (void *) ctfsect->cts_data; + + /* Flip the endianness of the copy of the header in the section, to avoid + ending up with a partially-endian-flipped file. */ + + if (foreign_endian) + flip_header ((ctf_header_t *) base); /* Once we have uncompressed and validated the CTF data buffer, we can proceed with allocating a ctf_file_t and initializing it. @@ -1372,6 +1373,7 @@ ctf_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, memset (fp, 0, sizeof (ctf_file_t)); ctf_set_version (fp, &hp, hp.cth_version); + ctf_str_create_atoms (fp); if (_libctf_unlikely_ (hp.cth_version < CTF_VERSION_2)) fp->ctf_parmax = CTF_MAX_PTYPE_V1; @@ -1427,14 +1429,6 @@ ctf_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, goto bad; } - /* The ctf region may have been reallocated by init_types(), but now - that is done, it will not move again, so we can protect it, as long - as it didn't come from the ctfsect, which might have been allocated - with malloc(). */ - - if (fp->ctf_base != (void *) ctfsect->cts_data) - ctf_data_protect ((void *) fp->ctf_base, fp->ctf_size); - /* If we have a symbol table section, allocate and initialize the symtab translation table, pointed to by ctf_sxlate. */ @@ -1535,6 +1529,7 @@ ctf_file_close (ctf_file_t *fp) ctf_dvd_delete (fp, dvd); } ctf_dynhash_destroy (fp->ctf_dvhash); + ctf_str_free_atoms (fp); ctf_free (fp->ctf_tmp_typeslice); @@ -1553,7 +1548,7 @@ ctf_file_close (ctf_file_t *fp) else if (fp->ctf_data_mmapped) ctf_munmap (fp->ctf_data_mmapped, fp->ctf_data_mmapped_len); - ctf_free_base (fp, NULL, 0); + ctf_free_base (fp, NULL); if (fp->ctf_sxlate != NULL) ctf_free (fp->ctf_sxlate); @@ -1572,6 +1567,14 @@ ctf_file_close (ctf_file_t *fp) ctf_free (fp); } +/* The converse of ctf_open(). ctf_open() disguises whatever it opens as an + archive, so closing one is just like closing an archive. */ +void +ctf_close (ctf_archive_t *arc) +{ + ctf_arc_close (arc); +} + /* Get the CTF archive from which this ctf_file_t is derived. */ ctf_archive_t * ctf_get_arc (const ctf_file_t *fp) @@ -1670,12 +1673,15 @@ ctf_getmodel (ctf_file_t *fp) return fp->ctf_dmodel->ctd_code; } +/* The caller can hang an arbitrary pointer off each ctf_file_t using this + function. */ void ctf_setspecific (ctf_file_t *fp, void *data) { fp->ctf_specific = data; } +/* Retrieve the arbitrary pointer again. */ void * ctf_getspecific (ctf_file_t *fp) {