2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
29 /* Symtypetab sections. */
31 /* Symtypetab emission flags. */
33 #define CTF_SYMTYPETAB_EMIT_FUNCTION 0x1
34 #define CTF_SYMTYPETAB_EMIT_PAD 0x2
35 #define CTF_SYMTYPETAB_FORCE_INDEXED 0x4
37 /* Properties of symtypetab emission, shared by symtypetab section
38 sizing and symtypetab emission itself. */
40 typedef struct emit_symtypetab_state
42 /* True if linker-reported symbols are being filtered out. symfp is set if
43 this is true: otherwise, indexing is forced and the symflags indicate as
47 /* True if symbols are being sorted. */
50 /* Flags for symtypetab emission. */
53 /* The dict to which the linker has reported symbols. */
56 /* The maximum number of objects seen. */
59 /* The maximum number of func info entris seen. */
61 } emit_symtypetab_state_t
;
63 /* Determine if a symbol is "skippable" and should never appear in the
64 symtypetab sections. */
67 ctf_symtab_skippable (ctf_link_sym_t
*sym
)
69 /* Never skip symbols whose name is not yet known. */
70 if (sym
->st_nameidx_set
)
73 return (sym
->st_name
== NULL
|| sym
->st_name
[0] == 0
74 || sym
->st_shndx
== SHN_UNDEF
75 || strcmp (sym
->st_name
, "_START_") == 0
76 || strcmp (sym
->st_name
, "_END_") == 0
77 || (sym
->st_type
== STT_OBJECT
&& sym
->st_shndx
== SHN_EXTABS
78 && sym
->st_value
== 0));
81 /* Get the number of symbols in a symbol hash, the count of symbols, the maximum
82 seen, the eventual size, without any padding elements, of the func/data and
83 (if generated) index sections, and the size of accumulated padding elements.
84 The linker-reported set of symbols is found in SYMFP: it may be NULL if
85 symbol filtering is not desired, in which case CTF_SYMTYPETAB_FORCE_INDEXED
86 will always be set in the flags.
88 Also figure out if any symbols need to be moved to the variable section, and
89 add them (if not already present). */
91 _libctf_nonnull_ ((1,3,4,5,6,7,8))
93 symtypetab_density (ctf_dict_t
*fp
, ctf_dict_t
*symfp
, ctf_dynhash_t
*symhash
,
94 size_t *count
, size_t *max
, size_t *unpadsize
,
95 size_t *padsize
, size_t *idxsize
, int flags
)
100 ctf_dynhash_t
*linker_known
= NULL
;
110 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
112 /* Make a dynhash citing only symbols reported by the linker of the
113 appropriate type, then traverse all potential-symbols we know the types
114 of, removing them from linker_known as we go. Once this is done, the
115 only symbols remaining in linker_known are symbols we don't know the
116 types of: we must emit pads for those symbols that are below the
117 maximum symbol we will emit (any beyond that are simply skipped).
119 If there are none, this symtypetab will be empty: just report that. */
121 if (!symfp
->ctf_dynsyms
)
124 if ((linker_known
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
125 NULL
, NULL
)) == NULL
)
126 return (ctf_set_errno (fp
, ENOMEM
));
128 while ((err
= ctf_dynhash_cnext (symfp
->ctf_dynsyms
, &i
,
129 &name
, &ctf_sym
)) == 0)
131 ctf_link_sym_t
*sym
= (ctf_link_sym_t
*) ctf_sym
;
133 if (((flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
134 && sym
->st_type
!= STT_FUNC
)
135 || (!(flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
136 && sym
->st_type
!= STT_OBJECT
))
139 if (ctf_symtab_skippable (sym
))
142 /* This should only be true briefly before all the names are
143 finalized, long before we get this far. */
144 if (!ctf_assert (fp
, !sym
->st_nameidx_set
))
145 return -1; /* errno is set for us. */
147 if (ctf_dynhash_cinsert (linker_known
, name
, ctf_sym
) < 0)
149 ctf_dynhash_destroy (linker_known
);
150 return (ctf_set_errno (fp
, ENOMEM
));
153 if (err
!= ECTF_NEXT_END
)
155 ctf_err_warn (fp
, 0, err
, _("iterating over linker-known symbols during "
157 ctf_dynhash_destroy (linker_known
);
158 return (ctf_set_errno (fp
, err
));
162 while ((err
= ctf_dynhash_cnext (symhash
, &i
, &name
, NULL
)) == 0)
166 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
168 /* Linker did not report symbol in symtab. Remove it from the
169 set of known data symbols and continue. */
170 if ((sym
= ctf_dynhash_lookup (symfp
->ctf_dynsyms
, name
)) == NULL
)
172 ctf_dynhash_remove (symhash
, name
);
176 /* We don't remove skippable symbols from the symhash because we don't
177 want them to be migrated into variables. */
178 if (ctf_symtab_skippable (sym
))
181 if ((flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
182 && sym
->st_type
!= STT_FUNC
)
184 ctf_err_warn (fp
, 1, 0, _("symbol %s (%x) added to CTF as a "
185 "function but is of type %x. "
186 "The symbol type lookup tables "
187 "are probably corrupted"),
188 sym
->st_name
, sym
->st_symidx
, sym
->st_type
);
189 ctf_dynhash_remove (symhash
, name
);
192 else if (!(flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
193 && sym
->st_type
!= STT_OBJECT
)
195 ctf_err_warn (fp
, 1, 0, _("symbol %s (%x) added to CTF as a "
196 "data object but is of type %x. "
197 "The symbol type lookup tables "
198 "are probably corrupted"),
199 sym
->st_name
, sym
->st_symidx
, sym
->st_type
);
200 ctf_dynhash_remove (symhash
, name
);
204 ctf_dynhash_remove (linker_known
, name
);
206 *unpadsize
+= sizeof (uint32_t);
209 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
211 if (*max
< sym
->st_symidx
)
212 *max
= sym
->st_symidx
;
217 if (err
!= ECTF_NEXT_END
)
219 ctf_err_warn (fp
, 0, err
, _("iterating over CTF symtypetab during "
221 ctf_dynhash_destroy (linker_known
);
222 return (ctf_set_errno (fp
, err
));
225 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
227 while ((err
= ctf_dynhash_cnext (linker_known
, &i
, NULL
, &ctf_sym
)) == 0)
229 ctf_link_sym_t
*sym
= (ctf_link_sym_t
*) ctf_sym
;
231 if (sym
->st_symidx
> *max
)
234 if (err
!= ECTF_NEXT_END
)
236 ctf_err_warn (fp
, 0, err
, _("iterating over linker-known symbols "
237 "during CTF serialization"));
238 ctf_dynhash_destroy (linker_known
);
239 return (ctf_set_errno (fp
, err
));
243 *idxsize
= *count
* sizeof (uint32_t);
244 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
245 *padsize
= (ctf_dynhash_elements (linker_known
) - beyond_max
) * sizeof (uint32_t);
247 ctf_dynhash_destroy (linker_known
);
251 /* Emit an objt or func symtypetab into DP in a particular order defined by an
252 array of ctf_link_sym_t or symbol names passed in. The index has NIDX
253 elements in it: unindexed output would terminate at symbol OUTMAX and is in
254 any case no larger than SIZE bytes. Some index elements are expected to be
255 skipped: see symtypetab_density. The linker-reported set of symbols (if any)
256 is found in SYMFP. */
258 emit_symtypetab (ctf_dict_t
*fp
, ctf_dict_t
*symfp
, uint32_t *dp
,
259 ctf_link_sym_t
**idx
, const char **nameidx
, uint32_t nidx
,
260 uint32_t outmax
, int size
, int flags
)
264 ctf_dynhash_t
*symhash
;
266 ctf_dprintf ("Emitting table of size %i, outmax %u, %u symtypetab entries, "
267 "flags %i\n", size
, outmax
, nidx
, flags
);
269 /* Empty table? Nothing to do. */
273 if (flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
274 symhash
= fp
->ctf_funchash
;
276 symhash
= fp
->ctf_objthash
;
278 for (i
= 0; i
< nidx
; i
++)
280 const char *sym_name
;
283 /* If we have a linker-reported set of symbols, we may be given that set
284 to work from, or a set of symbol names. In both cases we want to look
285 at the corresponding linker-reported symbol (if any). */
286 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
288 ctf_link_sym_t
*this_link_sym
;
291 this_link_sym
= idx
[i
];
293 this_link_sym
= ctf_dynhash_lookup (symfp
->ctf_dynsyms
, nameidx
[i
]);
295 /* Unreported symbol number. No pad, no nothing. */
299 /* Symbol of the wrong type, or skippable? This symbol is not in this
301 if (((flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
302 && this_link_sym
->st_type
!= STT_FUNC
)
303 || (!(flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
304 && this_link_sym
->st_type
!= STT_OBJECT
))
307 if (ctf_symtab_skippable (this_link_sym
))
310 sym_name
= this_link_sym
->st_name
;
312 /* Linker reports symbol of a different type to the symbol we actually
313 added? Skip the symbol. No pad, since the symbol doesn't actually
314 belong in this table at all. (Warned about in
315 symtypetab_density.) */
316 if ((this_link_sym
->st_type
== STT_FUNC
)
317 && (ctf_dynhash_lookup (fp
->ctf_objthash
, sym_name
)))
320 if ((this_link_sym
->st_type
== STT_OBJECT
)
321 && (ctf_dynhash_lookup (fp
->ctf_funchash
, sym_name
)))
325 sym_name
= nameidx
[i
];
327 /* Symbol in index but no type set? Silently skip and (optionally)
328 pad. (In force-indexed mode, this is also where we track symbols of
329 the wrong type for this round of insertion.) */
330 if ((type
= ctf_dynhash_lookup (symhash
, sym_name
)) == NULL
)
332 if (flags
& CTF_SYMTYPETAB_EMIT_PAD
)
337 if (!ctf_assert (fp
, (((char *) dpp
) - (char *) dp
) < size
))
338 return -1; /* errno is set for us. */
340 *dpp
++ = (ctf_id_t
) (uintptr_t) type
;
342 /* When emitting unindexed output, all later symbols are pads: stop
344 if ((flags
& CTF_SYMTYPETAB_EMIT_PAD
) && idx
[i
]->st_symidx
== outmax
)
351 /* Emit an objt or func symtypetab index into DP in a paticular order defined by
352 an array of symbol names passed in. Stop at NIDX. The linker-reported set
353 of symbols (if any) is found in SYMFP. */
355 emit_symtypetab_index (ctf_dict_t
*fp
, ctf_dict_t
*symfp
, uint32_t *dp
,
356 const char **idx
, uint32_t nidx
, int size
, int flags
)
360 ctf_dynhash_t
*symhash
;
362 ctf_dprintf ("Emitting index of size %i, %u entries reported by linker, "
363 "flags %i\n", size
, nidx
, flags
);
365 /* Empty table? Nothing to do. */
369 if (flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
370 symhash
= fp
->ctf_funchash
;
372 symhash
= fp
->ctf_objthash
;
374 /* Indexes should always be unpadded. */
375 if (!ctf_assert (fp
, !(flags
& CTF_SYMTYPETAB_EMIT_PAD
)))
376 return -1; /* errno is set for us. */
378 for (i
= 0; i
< nidx
; i
++)
380 const char *sym_name
;
383 if (!(flags
& CTF_SYMTYPETAB_FORCE_INDEXED
))
385 ctf_link_sym_t
*this_link_sym
;
387 this_link_sym
= ctf_dynhash_lookup (symfp
->ctf_dynsyms
, idx
[i
]);
389 /* This is an index: unreported symbols should never appear in it. */
390 if (!ctf_assert (fp
, this_link_sym
!= NULL
))
391 return -1; /* errno is set for us. */
393 /* Symbol of the wrong type, or skippable? This symbol is not in this
395 if (((flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
396 && this_link_sym
->st_type
!= STT_FUNC
)
397 || (!(flags
& CTF_SYMTYPETAB_EMIT_FUNCTION
)
398 && this_link_sym
->st_type
!= STT_OBJECT
))
401 if (ctf_symtab_skippable (this_link_sym
))
404 sym_name
= this_link_sym
->st_name
;
406 /* Linker reports symbol of a different type to the symbol we actually
407 added? Skip the symbol. */
408 if ((this_link_sym
->st_type
== STT_FUNC
)
409 && (ctf_dynhash_lookup (fp
->ctf_objthash
, sym_name
)))
412 if ((this_link_sym
->st_type
== STT_OBJECT
)
413 && (ctf_dynhash_lookup (fp
->ctf_funchash
, sym_name
)))
419 /* Symbol in index and reported by linker, but no type set? Silently skip
420 and (optionally) pad. (In force-indexed mode, this is also where we
421 track symbols of the wrong type for this round of insertion.) */
422 if ((type
= ctf_dynhash_lookup (symhash
, sym_name
)) == NULL
)
425 ctf_str_add_ref (fp
, sym_name
, dpp
++);
427 if (!ctf_assert (fp
, (((char *) dpp
) - (char *) dp
) <= size
))
428 return -1; /* errno is set for us. */
434 /* Delete symbols that have been assigned names from the variable section. Must
435 be called from within ctf_serialize, because that is the only place you can
436 safely delete variables without messing up ctf_rollback. */
439 symtypetab_delete_nonstatics (ctf_dict_t
*fp
, ctf_dict_t
*symfp
)
441 ctf_dvdef_t
*dvd
, *nvd
;
444 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
; dvd
= nvd
)
446 nvd
= ctf_list_next (dvd
);
448 if ((((type
= (ctf_id_t
) (uintptr_t)
449 ctf_dynhash_lookup (fp
->ctf_objthash
, dvd
->dvd_name
)) > 0)
450 || (type
= (ctf_id_t
) (uintptr_t)
451 ctf_dynhash_lookup (fp
->ctf_funchash
, dvd
->dvd_name
)) > 0)
452 && ctf_dynhash_lookup (symfp
->ctf_dynsyms
, dvd
->dvd_name
) != NULL
453 && type
== dvd
->dvd_type
)
454 ctf_dvd_delete (fp
, dvd
);
460 /* Figure out the sizes of the symtypetab sections, their indexed state,
463 ctf_symtypetab_sect_sizes (ctf_dict_t
*fp
, emit_symtypetab_state_t
*s
,
464 ctf_header_t
*hdr
, size_t *objt_size
,
465 size_t *func_size
, size_t *objtidx_size
,
466 size_t *funcidx_size
)
468 size_t nfuncs
, nobjts
;
469 size_t objt_unpadsize
, func_unpadsize
, objt_padsize
, func_padsize
;
471 /* If doing a writeout as part of linking, and the link flags request it,
472 filter out reported symbols from the variable section, and filter out all
473 other symbols from the symtypetab sections. (If we are not linking, the
474 symbols are sorted; if we are linking, don't bother sorting if we are not
475 filtering out reported symbols: this is almost certaily an ld -r and only
476 the linker is likely to consume these symtypetabs again. The linker
477 doesn't care what order the symtypetab entries is in, since it only
478 iterates over symbols and does not use the ctf_lookup_by_symbol* API.) */
481 if (fp
->ctf_flags
& LCTF_LINKING
)
483 s
->filter_syms
= !(fp
->ctf_link_flags
& CTF_LINK_NO_FILTER_REPORTED_SYMS
);
488 /* Find the dict to which the linker has reported symbols, if any. */
492 if (!fp
->ctf_dynsyms
&& fp
->ctf_parent
&& fp
->ctf_parent
->ctf_dynsyms
)
493 s
->symfp
= fp
->ctf_parent
;
498 /* If not filtering, keep all potential symbols in an unsorted, indexed
501 s
->symflags
= CTF_SYMTYPETAB_FORCE_INDEXED
;
503 hdr
->cth_flags
|= CTF_F_IDXSORTED
;
505 if (!ctf_assert (fp
, (s
->filter_syms
&& s
->symfp
)
506 || (!s
->filter_syms
&& !s
->symfp
507 && ((s
->symflags
& CTF_SYMTYPETAB_FORCE_INDEXED
) != 0))))
510 /* Work out the sizes of the object and function sections, and work out the
511 number of pad (unassigned) symbols in each, and the overall size of the
514 if (symtypetab_density (fp
, s
->symfp
, fp
->ctf_objthash
, &nobjts
, &s
->maxobjt
,
515 &objt_unpadsize
, &objt_padsize
, objtidx_size
,
517 return -1; /* errno is set for us. */
519 ctf_dprintf ("Object symtypetab: %i objects, max %i, unpadded size %i, "
520 "%i bytes of pads, index size %i\n", (int) nobjts
,
521 (int) s
->maxobjt
, (int) objt_unpadsize
, (int) objt_padsize
,
522 (int) *objtidx_size
);
524 if (symtypetab_density (fp
, s
->symfp
, fp
->ctf_funchash
, &nfuncs
, &s
->maxfunc
,
525 &func_unpadsize
, &func_padsize
, funcidx_size
,
526 s
->symflags
| CTF_SYMTYPETAB_EMIT_FUNCTION
) < 0)
527 return -1; /* errno is set for us. */
529 ctf_dprintf ("Function symtypetab: %i functions, max %i, unpadded size %i, "
530 "%i bytes of pads, index size %i\n", (int) nfuncs
,
531 (int) s
->maxfunc
, (int) func_unpadsize
, (int) func_padsize
,
532 (int) *funcidx_size
);
534 /* It is worth indexing each section if it would save space to do so, due to
535 reducing the number of pads sufficiently. A pad is the same size as a
536 single index entry: but index sections compress relatively poorly compared
537 to constant pads, so it takes a lot of contiguous padding to equal one
538 index section entry. It would be nice to be able to *verify* whether we
539 would save space after compression rather than guessing, but this seems
540 difficult, since it would require complete reserialization. Regardless, if
541 the linker has not reported any symbols (e.g. if this is not a final link
542 but just an ld -r), we must emit things in indexed fashion just as the
545 *objt_size
= objt_unpadsize
;
546 if (!(s
->symflags
& CTF_SYMTYPETAB_FORCE_INDEXED
)
547 && ((objt_padsize
+ objt_unpadsize
) * CTF_INDEX_PAD_THRESHOLD
550 *objt_size
+= objt_padsize
;
554 *func_size
= func_unpadsize
;
555 if (!(s
->symflags
& CTF_SYMTYPETAB_FORCE_INDEXED
)
556 && ((func_padsize
+ func_unpadsize
) * CTF_INDEX_PAD_THRESHOLD
559 *func_size
+= func_padsize
;
563 /* If we are filtering symbols out, those symbols that the linker has not
564 reported have now been removed from the ctf_objthash and ctf_funchash.
565 Delete entries from the variable section that duplicate newly-added
566 symbols. There's no need to migrate new ones in: we do that (if necessary)
567 in ctf_link_deduplicating_variables. */
569 if (s
->filter_syms
&& s
->symfp
->ctf_dynsyms
&&
570 symtypetab_delete_nonstatics (fp
, s
->symfp
) < 0)
577 ctf_emit_symtypetab_sects (ctf_dict_t
*fp
, emit_symtypetab_state_t
*s
,
578 unsigned char **tptr
, size_t objt_size
,
579 size_t func_size
, size_t objtidx_size
,
582 unsigned char *t
= *tptr
;
583 size_t nsymtypes
= 0;
584 const char **sym_name_order
= NULL
;
587 /* Sort the linker's symbols into name order if need be. */
589 if ((objtidx_size
!= 0) || (funcidx_size
!= 0))
591 ctf_next_t
*i
= NULL
;
597 if (s
->symfp
->ctf_dynsyms
)
598 nsymtypes
= ctf_dynhash_elements (s
->symfp
->ctf_dynsyms
);
603 nsymtypes
= ctf_dynhash_elements (fp
->ctf_objthash
)
604 + ctf_dynhash_elements (fp
->ctf_funchash
);
606 if ((sym_name_order
= calloc (nsymtypes
, sizeof (const char *))) == NULL
)
609 walk
= sym_name_order
;
613 if (s
->symfp
->ctf_dynsyms
)
615 while ((err
= ctf_dynhash_next_sorted (s
->symfp
->ctf_dynsyms
, &i
,
617 ctf_dynhash_sort_by_name
,
619 *walk
++ = (const char *) symname
;
620 if (err
!= ECTF_NEXT_END
)
626 ctf_hash_sort_f sort_fun
= NULL
;
628 /* Since we partition the set of symbols back into objt and func,
629 we can sort the two independently without harm. */
631 sort_fun
= ctf_dynhash_sort_by_name
;
633 while ((err
= ctf_dynhash_next_sorted (fp
->ctf_objthash
, &i
, &symname
,
634 NULL
, sort_fun
, NULL
)) == 0)
635 *walk
++ = (const char *) symname
;
636 if (err
!= ECTF_NEXT_END
)
639 while ((err
= ctf_dynhash_next_sorted (fp
->ctf_funchash
, &i
, &symname
,
640 NULL
, sort_fun
, NULL
)) == 0)
641 *walk
++ = (const char *) symname
;
642 if (err
!= ECTF_NEXT_END
)
647 /* Emit the object and function sections, and if necessary their indexes.
648 Emission is done in symtab order if there is no index, and in index
649 (name) order otherwise. */
651 if ((objtidx_size
== 0) && s
->symfp
&& s
->symfp
->ctf_dynsymidx
)
653 ctf_dprintf ("Emitting unindexed objt symtypetab\n");
654 if (emit_symtypetab (fp
, s
->symfp
, (uint32_t *) t
,
655 s
->symfp
->ctf_dynsymidx
, NULL
,
656 s
->symfp
->ctf_dynsymmax
+ 1, s
->maxobjt
,
657 objt_size
, s
->symflags
| CTF_SYMTYPETAB_EMIT_PAD
) < 0)
658 goto err
; /* errno is set for us. */
662 ctf_dprintf ("Emitting indexed objt symtypetab\n");
663 if (emit_symtypetab (fp
, s
->symfp
, (uint32_t *) t
, NULL
,
664 sym_name_order
, nsymtypes
, s
->maxobjt
,
665 objt_size
, s
->symflags
) < 0)
666 goto err
; /* errno is set for us. */
671 if ((funcidx_size
== 0) && s
->symfp
&& s
->symfp
->ctf_dynsymidx
)
673 ctf_dprintf ("Emitting unindexed func symtypetab\n");
674 if (emit_symtypetab (fp
, s
->symfp
, (uint32_t *) t
,
675 s
->symfp
->ctf_dynsymidx
, NULL
,
676 s
->symfp
->ctf_dynsymmax
+ 1, s
->maxfunc
,
677 func_size
, s
->symflags
| CTF_SYMTYPETAB_EMIT_FUNCTION
678 | CTF_SYMTYPETAB_EMIT_PAD
) < 0)
679 goto err
; /* errno is set for us. */
683 ctf_dprintf ("Emitting indexed func symtypetab\n");
684 if (emit_symtypetab (fp
, s
->symfp
, (uint32_t *) t
, NULL
, sym_name_order
,
685 nsymtypes
, s
->maxfunc
, func_size
,
686 s
->symflags
| CTF_SYMTYPETAB_EMIT_FUNCTION
) < 0)
687 goto err
; /* errno is set for us. */
692 if (objtidx_size
> 0)
693 if (emit_symtypetab_index (fp
, s
->symfp
, (uint32_t *) t
, sym_name_order
,
694 nsymtypes
, objtidx_size
, s
->symflags
) < 0)
699 if (funcidx_size
> 0)
700 if (emit_symtypetab_index (fp
, s
->symfp
, (uint32_t *) t
, sym_name_order
,
701 nsymtypes
, funcidx_size
,
702 s
->symflags
| CTF_SYMTYPETAB_EMIT_FUNCTION
) < 0)
706 free (sym_name_order
);
712 ctf_set_errno (fp
, EAGAIN
);
715 ctf_err_warn (fp
, 0, err
, _("error serializing symtypetabs"));
717 free (sym_name_order
);
723 /* Iterate through the dynamic type definition list and compute the
724 size of the CTF type section. */
727 ctf_type_sect_size (ctf_dict_t
*fp
)
732 for (type_size
= 0, dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
733 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
735 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
736 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
737 size_t type_ctt_size
= dtd
->dtd_data
.ctt_size
;
739 /* Shrink ctf_type_t-using types from a ctf_type_t to a ctf_stype_t
742 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
)
744 size_t lsize
= CTF_TYPE_LSIZE (&dtd
->dtd_data
);
746 if (lsize
<= CTF_MAX_SIZE
)
747 type_ctt_size
= lsize
;
750 if (type_ctt_size
!= CTF_LSIZE_SENT
)
751 type_size
+= sizeof (ctf_stype_t
);
753 type_size
+= sizeof (ctf_type_t
);
759 type_size
+= sizeof (uint32_t);
762 type_size
+= sizeof (ctf_array_t
);
765 type_size
+= sizeof (ctf_slice_t
);
768 type_size
+= sizeof (uint32_t) * (vlen
+ (vlen
& 1));
772 if (type_ctt_size
< CTF_LSTRUCT_THRESH
)
773 type_size
+= sizeof (ctf_member_t
) * vlen
;
775 type_size
+= sizeof (ctf_lmember_t
) * vlen
;
778 type_size
+= sizeof (ctf_enum_t
) * vlen
;
786 /* Take a final lap through the dynamic type definition list and copy the
787 appropriate type records to the output buffer, noting down the strings as
791 ctf_emit_type_sect (ctf_dict_t
*fp
, unsigned char **tptr
)
793 unsigned char *t
= *tptr
;
796 for (dtd
= ctf_list_next (&fp
->ctf_dtdefs
);
797 dtd
!= NULL
; dtd
= ctf_list_next (dtd
))
799 uint32_t kind
= LCTF_INFO_KIND (fp
, dtd
->dtd_data
.ctt_info
);
800 uint32_t vlen
= LCTF_INFO_VLEN (fp
, dtd
->dtd_data
.ctt_info
);
801 size_t type_ctt_size
= dtd
->dtd_data
.ctt_size
;
807 /* Shrink ctf_type_t-using types from a ctf_type_t to a ctf_stype_t
810 if (kind
== CTF_K_STRUCT
|| kind
== CTF_K_UNION
)
812 size_t lsize
= CTF_TYPE_LSIZE (&dtd
->dtd_data
);
814 if (lsize
<= CTF_MAX_SIZE
)
815 type_ctt_size
= lsize
;
818 if (type_ctt_size
!= CTF_LSIZE_SENT
)
819 len
= sizeof (ctf_stype_t
);
821 len
= sizeof (ctf_type_t
);
823 memcpy (t
, &dtd
->dtd_data
, len
);
824 copied
= (ctf_stype_t
*) t
; /* name is at the start: constant offset. */
826 && (name
= ctf_strraw (fp
, copied
->ctt_name
)) != NULL
)
828 ctf_str_add_ref (fp
, name
, &copied
->ctt_name
);
829 ctf_str_add_ref (fp
, name
, &dtd
->dtd_data
.ctt_name
);
831 copied
->ctt_size
= type_ctt_size
;
838 memcpy (t
, dtd
->dtd_vlen
, sizeof (uint32_t));
839 t
+= sizeof (uint32_t);
843 memcpy (t
, dtd
->dtd_vlen
, sizeof (struct ctf_slice
));
844 t
+= sizeof (struct ctf_slice
);
848 memcpy (t
, dtd
->dtd_vlen
, sizeof (struct ctf_array
));
849 t
+= sizeof (struct ctf_array
);
853 /* Functions with no args also have no vlen. */
855 memcpy (t
, dtd
->dtd_vlen
, sizeof (uint32_t) * (vlen
+ (vlen
& 1)));
856 t
+= sizeof (uint32_t) * (vlen
+ (vlen
& 1));
859 /* These need to be copied across element by element, depending on
864 ctf_lmember_t
*dtd_vlen
= (ctf_lmember_t
*) dtd
->dtd_vlen
;
865 ctf_lmember_t
*t_lvlen
= (ctf_lmember_t
*) t
;
866 ctf_member_t
*t_vlen
= (ctf_member_t
*) t
;
868 for (i
= 0; i
< vlen
; i
++)
870 const char *name
= ctf_strraw (fp
, dtd_vlen
[i
].ctlm_name
);
872 ctf_str_add_ref (fp
, name
, &dtd_vlen
[i
].ctlm_name
);
874 if (type_ctt_size
< CTF_LSTRUCT_THRESH
)
876 t_vlen
[i
].ctm_name
= dtd_vlen
[i
].ctlm_name
;
877 t_vlen
[i
].ctm_type
= dtd_vlen
[i
].ctlm_type
;
878 t_vlen
[i
].ctm_offset
= CTF_LMEM_OFFSET (&dtd_vlen
[i
]);
879 ctf_str_add_ref (fp
, name
, &t_vlen
[i
].ctm_name
);
883 t_lvlen
[i
] = dtd_vlen
[i
];
884 ctf_str_add_ref (fp
, name
, &t_lvlen
[i
].ctlm_name
);
889 if (type_ctt_size
< CTF_LSTRUCT_THRESH
)
890 t
+= sizeof (ctf_member_t
) * vlen
;
892 t
+= sizeof (ctf_lmember_t
) * vlen
;
897 ctf_enum_t
*dtd_vlen
= (struct ctf_enum
*) dtd
->dtd_vlen
;
898 ctf_enum_t
*t_vlen
= (struct ctf_enum
*) t
;
900 memcpy (t
, dtd
->dtd_vlen
, sizeof (struct ctf_enum
) * vlen
);
901 for (i
= 0; i
< vlen
; i
++)
903 const char *name
= ctf_strraw (fp
, dtd_vlen
[i
].cte_name
);
905 ctf_str_add_ref (fp
, name
, &t_vlen
[i
].cte_name
);
906 ctf_str_add_ref (fp
, name
, &dtd_vlen
[i
].cte_name
);
908 t
+= sizeof (struct ctf_enum
) * vlen
;
918 /* Variable section. */
920 /* Sort a newly-constructed static variable array. */
922 typedef struct ctf_sort_var_arg_cb
926 } ctf_sort_var_arg_cb_t
;
929 ctf_sort_var (const void *one_
, const void *two_
, void *arg_
)
931 const ctf_varent_t
*one
= one_
;
932 const ctf_varent_t
*two
= two_
;
933 ctf_sort_var_arg_cb_t
*arg
= arg_
;
935 return (strcmp (ctf_strraw_explicit (arg
->fp
, one
->ctv_name
, arg
->strtab
),
936 ctf_strraw_explicit (arg
->fp
, two
->ctv_name
, arg
->strtab
)));
939 /* Overall serialization. */
941 /* If the specified CTF dict is writable and has been modified, reload this dict
942 with the updated type definitions, ready for serialization. In order to make
943 this code and the rest of libctf as simple as possible, we perform updates by
944 taking the dynamic type definitions and creating an in-memory CTF dict
945 containing the definitions, and then call ctf_simple_open_internal() on it.
946 We perform one extra trick here for the benefit of callers and to keep our
947 code simple: ctf_simple_open_internal() will return a new ctf_dict_t, but we
948 want to keep the fp constant for the caller, so after
949 ctf_simple_open_internal() returns, we use memcpy to swap the interior of the
950 old and new ctf_dict_t's, and then free the old. */
952 ctf_serialize (ctf_dict_t
*fp
)
954 ctf_dict_t ofp
, *nfp
;
955 ctf_header_t hdr
, *hdrp
;
957 ctf_varent_t
*dvarents
;
958 ctf_strs_writable_t strtab
;
960 int num_missed_str_refs
;
964 size_t buf_size
, type_size
, objt_size
, func_size
;
965 size_t funcidx_size
, objtidx_size
;
967 unsigned char *buf
= NULL
, *newbuf
;
969 emit_symtypetab_state_t symstate
;
970 memset (&symstate
, 0, sizeof (emit_symtypetab_state_t
));
972 if (!(fp
->ctf_flags
& LCTF_RDWR
))
973 return (ctf_set_errno (fp
, ECTF_RDONLY
));
975 /* Update required? */
976 if (!(fp
->ctf_flags
& LCTF_DIRTY
))
979 /* The strtab refs table must be empty at this stage. Any refs already added
980 will be corrupted by any modifications, including reserialization, after
981 strtab finalization is complete. Only this function, and functions it
982 calls, may add refs, and all memory locations (including in the dtds)
983 containing strtab offsets must be traversed as part of serialization, and
986 if (!ctf_assert (fp
, fp
->ctf_str_num_refs
== 0))
987 return -1; /* errno is set for us. */
989 /* Fill in an initial CTF header. We will leave the label, object,
990 and function sections empty and only output a header, type section,
991 and string table. The type section begins at a 4-byte aligned
992 boundary past the CTF header itself (at relative offset zero). The flag
993 indicating a new-style function info section (an array of CTF_K_FUNCTION
994 type IDs in the types section) is flipped on. */
996 memset (&hdr
, 0, sizeof (hdr
));
997 hdr
.cth_magic
= CTF_MAGIC
;
998 hdr
.cth_version
= CTF_VERSION
;
1000 /* This is a new-format func info section, and the symtab and strtab come out
1001 of the dynsym and dynstr these days. */
1002 hdr
.cth_flags
= (CTF_F_NEWFUNCINFO
| CTF_F_DYNSTR
);
1004 if (ctf_symtypetab_sect_sizes (fp
, &symstate
, &hdr
, &objt_size
, &func_size
,
1005 &objtidx_size
, &funcidx_size
) < 0)
1006 return -1; /* errno is set for us. */
1008 for (nvars
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
);
1009 dvd
!= NULL
; dvd
= ctf_list_next (dvd
), nvars
++);
1011 type_size
= ctf_type_sect_size (fp
);
1013 /* Compute the size of the CTF buffer we need, sans only the string table,
1014 then allocate a new buffer and memcpy the finished header to the start of
1015 the buffer. (We will adjust this later with strtab length info.) */
1017 hdr
.cth_lbloff
= hdr
.cth_objtoff
= 0;
1018 hdr
.cth_funcoff
= hdr
.cth_objtoff
+ objt_size
;
1019 hdr
.cth_objtidxoff
= hdr
.cth_funcoff
+ func_size
;
1020 hdr
.cth_funcidxoff
= hdr
.cth_objtidxoff
+ objtidx_size
;
1021 hdr
.cth_varoff
= hdr
.cth_funcidxoff
+ funcidx_size
;
1022 hdr
.cth_typeoff
= hdr
.cth_varoff
+ (nvars
* sizeof (ctf_varent_t
));
1023 hdr
.cth_stroff
= hdr
.cth_typeoff
+ type_size
;
1026 buf_size
= sizeof (ctf_header_t
) + hdr
.cth_stroff
+ hdr
.cth_strlen
;
1028 if ((buf
= malloc (buf_size
)) == NULL
)
1029 return (ctf_set_errno (fp
, EAGAIN
));
1031 memcpy (buf
, &hdr
, sizeof (ctf_header_t
));
1032 t
= (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_objtoff
;
1034 hdrp
= (ctf_header_t
*) buf
;
1035 if ((fp
->ctf_flags
& LCTF_CHILD
) && (fp
->ctf_parname
!= NULL
))
1036 ctf_str_add_ref (fp
, fp
->ctf_parname
, &hdrp
->cth_parname
);
1037 if (fp
->ctf_cuname
!= NULL
)
1038 ctf_str_add_ref (fp
, fp
->ctf_cuname
, &hdrp
->cth_cuname
);
1040 if (ctf_emit_symtypetab_sects (fp
, &symstate
, &t
, objt_size
, func_size
,
1041 objtidx_size
, funcidx_size
) < 0)
1044 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_varoff
);
1046 /* Work over the variable list, translating everything into ctf_varent_t's and
1047 prepping the string table. */
1049 dvarents
= (ctf_varent_t
*) t
;
1050 for (i
= 0, dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
;
1051 dvd
= ctf_list_next (dvd
), i
++)
1053 ctf_varent_t
*var
= &dvarents
[i
];
1055 ctf_str_add_ref (fp
, dvd
->dvd_name
, &var
->ctv_name
);
1056 var
->ctv_type
= (uint32_t) dvd
->dvd_type
;
1058 assert (i
== nvars
);
1060 t
+= sizeof (ctf_varent_t
) * nvars
;
1062 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_typeoff
);
1064 ctf_emit_type_sect (fp
, &t
);
1066 assert (t
== (unsigned char *) buf
+ sizeof (ctf_header_t
) + hdr
.cth_stroff
);
1068 /* Every string added outside serialization by ctf_str_add_pending should
1069 now have been added by ctf_add_ref. */
1070 num_missed_str_refs
= ctf_dynset_elements (fp
->ctf_str_pending_ref
);
1071 if (!ctf_assert (fp
, num_missed_str_refs
== 0))
1072 goto err
; /* errno is set for us. */
1074 /* Construct the final string table and fill out all the string refs with the
1075 final offsets. Then purge the refs list, because we're about to move this
1076 strtab onto the end of the buf, invalidating all the offsets. */
1077 strtab
= ctf_str_write_strtab (fp
);
1078 ctf_str_purge_refs (fp
);
1080 if (strtab
.cts_strs
== NULL
)
1083 /* Now the string table is constructed, we can sort the buffer of
1085 ctf_sort_var_arg_cb_t sort_var_arg
= { fp
, (ctf_strs_t
*) &strtab
};
1086 ctf_qsort_r (dvarents
, nvars
, sizeof (ctf_varent_t
), ctf_sort_var
,
1089 if ((newbuf
= ctf_realloc (fp
, buf
, buf_size
+ strtab
.cts_len
)) == NULL
)
1091 free (strtab
.cts_strs
);
1095 memcpy (buf
+ buf_size
, strtab
.cts_strs
, strtab
.cts_len
);
1096 hdrp
= (ctf_header_t
*) buf
;
1097 hdrp
->cth_strlen
= strtab
.cts_len
;
1098 buf_size
+= hdrp
->cth_strlen
;
1099 free (strtab
.cts_strs
);
1101 /* Finally, we are ready to ctf_simple_open() the new dict. If this is
1102 successful, we then switch nfp and fp and free the old dict. */
1104 if ((nfp
= ctf_simple_open_internal ((char *) buf
, buf_size
, NULL
, 0,
1105 0, NULL
, 0, fp
->ctf_syn_ext_strtab
,
1109 return (ctf_set_errno (fp
, err
));
1112 (void) ctf_setmodel (nfp
, ctf_getmodel (fp
));
1114 nfp
->ctf_parent
= fp
->ctf_parent
;
1115 nfp
->ctf_parent_unreffed
= fp
->ctf_parent_unreffed
;
1116 nfp
->ctf_refcnt
= fp
->ctf_refcnt
;
1117 nfp
->ctf_flags
|= fp
->ctf_flags
& ~LCTF_DIRTY
;
1118 if (nfp
->ctf_dynbase
== NULL
)
1119 nfp
->ctf_dynbase
= buf
; /* Make sure buf is freed on close. */
1120 nfp
->ctf_dthash
= fp
->ctf_dthash
;
1121 nfp
->ctf_dtdefs
= fp
->ctf_dtdefs
;
1122 nfp
->ctf_dvhash
= fp
->ctf_dvhash
;
1123 nfp
->ctf_dvdefs
= fp
->ctf_dvdefs
;
1124 nfp
->ctf_dtoldid
= fp
->ctf_dtoldid
;
1125 nfp
->ctf_add_processing
= fp
->ctf_add_processing
;
1126 nfp
->ctf_snapshots
= fp
->ctf_snapshots
+ 1;
1127 nfp
->ctf_specific
= fp
->ctf_specific
;
1128 nfp
->ctf_nfuncidx
= fp
->ctf_nfuncidx
;
1129 nfp
->ctf_nobjtidx
= fp
->ctf_nobjtidx
;
1130 nfp
->ctf_objthash
= fp
->ctf_objthash
;
1131 nfp
->ctf_funchash
= fp
->ctf_funchash
;
1132 nfp
->ctf_dynsyms
= fp
->ctf_dynsyms
;
1133 nfp
->ctf_ptrtab
= fp
->ctf_ptrtab
;
1134 nfp
->ctf_pptrtab
= fp
->ctf_pptrtab
;
1135 nfp
->ctf_typemax
= fp
->ctf_typemax
;
1136 nfp
->ctf_dynsymidx
= fp
->ctf_dynsymidx
;
1137 nfp
->ctf_dynsymmax
= fp
->ctf_dynsymmax
;
1138 nfp
->ctf_ptrtab_len
= fp
->ctf_ptrtab_len
;
1139 nfp
->ctf_pptrtab_len
= fp
->ctf_pptrtab_len
;
1140 nfp
->ctf_link_inputs
= fp
->ctf_link_inputs
;
1141 nfp
->ctf_link_outputs
= fp
->ctf_link_outputs
;
1142 nfp
->ctf_errs_warnings
= fp
->ctf_errs_warnings
;
1143 nfp
->ctf_funcidx_names
= fp
->ctf_funcidx_names
;
1144 nfp
->ctf_objtidx_names
= fp
->ctf_objtidx_names
;
1145 nfp
->ctf_funcidx_sxlate
= fp
->ctf_funcidx_sxlate
;
1146 nfp
->ctf_objtidx_sxlate
= fp
->ctf_objtidx_sxlate
;
1147 nfp
->ctf_str_prov_offset
= fp
->ctf_str_prov_offset
;
1148 nfp
->ctf_syn_ext_strtab
= fp
->ctf_syn_ext_strtab
;
1149 nfp
->ctf_pptrtab_typemax
= fp
->ctf_pptrtab_typemax
;
1150 nfp
->ctf_in_flight_dynsyms
= fp
->ctf_in_flight_dynsyms
;
1151 nfp
->ctf_link_in_cu_mapping
= fp
->ctf_link_in_cu_mapping
;
1152 nfp
->ctf_link_out_cu_mapping
= fp
->ctf_link_out_cu_mapping
;
1153 nfp
->ctf_link_type_mapping
= fp
->ctf_link_type_mapping
;
1154 nfp
->ctf_link_memb_name_changer
= fp
->ctf_link_memb_name_changer
;
1155 nfp
->ctf_link_memb_name_changer_arg
= fp
->ctf_link_memb_name_changer_arg
;
1156 nfp
->ctf_link_variable_filter
= fp
->ctf_link_variable_filter
;
1157 nfp
->ctf_link_variable_filter_arg
= fp
->ctf_link_variable_filter_arg
;
1158 nfp
->ctf_symsect_little_endian
= fp
->ctf_symsect_little_endian
;
1159 nfp
->ctf_link_flags
= fp
->ctf_link_flags
;
1160 nfp
->ctf_dedup_atoms
= fp
->ctf_dedup_atoms
;
1161 nfp
->ctf_dedup_atoms_alloc
= fp
->ctf_dedup_atoms_alloc
;
1162 memcpy (&nfp
->ctf_dedup
, &fp
->ctf_dedup
, sizeof (fp
->ctf_dedup
));
1164 nfp
->ctf_snapshot_lu
= fp
->ctf_snapshots
;
1166 memcpy (&nfp
->ctf_lookups
, fp
->ctf_lookups
, sizeof (fp
->ctf_lookups
));
1167 nfp
->ctf_structs
= fp
->ctf_structs
;
1168 nfp
->ctf_unions
= fp
->ctf_unions
;
1169 nfp
->ctf_enums
= fp
->ctf_enums
;
1170 nfp
->ctf_names
= fp
->ctf_names
;
1172 fp
->ctf_dthash
= NULL
;
1173 ctf_str_free_atoms (nfp
);
1174 nfp
->ctf_str_atoms
= fp
->ctf_str_atoms
;
1175 nfp
->ctf_prov_strtab
= fp
->ctf_prov_strtab
;
1176 nfp
->ctf_str_pending_ref
= fp
->ctf_str_pending_ref
;
1177 fp
->ctf_str_atoms
= NULL
;
1178 fp
->ctf_prov_strtab
= NULL
;
1179 fp
->ctf_str_pending_ref
= NULL
;
1180 memset (&fp
->ctf_dtdefs
, 0, sizeof (ctf_list_t
));
1181 memset (&fp
->ctf_errs_warnings
, 0, sizeof (ctf_list_t
));
1182 fp
->ctf_add_processing
= NULL
;
1183 fp
->ctf_ptrtab
= NULL
;
1184 fp
->ctf_pptrtab
= NULL
;
1185 fp
->ctf_funcidx_names
= NULL
;
1186 fp
->ctf_objtidx_names
= NULL
;
1187 fp
->ctf_funcidx_sxlate
= NULL
;
1188 fp
->ctf_objtidx_sxlate
= NULL
;
1189 fp
->ctf_objthash
= NULL
;
1190 fp
->ctf_funchash
= NULL
;
1191 fp
->ctf_dynsyms
= NULL
;
1192 fp
->ctf_dynsymidx
= NULL
;
1193 fp
->ctf_link_inputs
= NULL
;
1194 fp
->ctf_link_outputs
= NULL
;
1195 fp
->ctf_syn_ext_strtab
= NULL
;
1196 fp
->ctf_link_in_cu_mapping
= NULL
;
1197 fp
->ctf_link_out_cu_mapping
= NULL
;
1198 fp
->ctf_link_type_mapping
= NULL
;
1199 fp
->ctf_dedup_atoms
= NULL
;
1200 fp
->ctf_dedup_atoms_alloc
= NULL
;
1201 fp
->ctf_parent_unreffed
= 1;
1203 fp
->ctf_dvhash
= NULL
;
1204 memset (&fp
->ctf_dvdefs
, 0, sizeof (ctf_list_t
));
1205 memset (fp
->ctf_lookups
, 0, sizeof (fp
->ctf_lookups
));
1206 memset (&fp
->ctf_in_flight_dynsyms
, 0, sizeof (fp
->ctf_in_flight_dynsyms
));
1207 memset (&fp
->ctf_dedup
, 0, sizeof (fp
->ctf_dedup
));
1208 fp
->ctf_structs
.ctn_writable
= NULL
;
1209 fp
->ctf_unions
.ctn_writable
= NULL
;
1210 fp
->ctf_enums
.ctn_writable
= NULL
;
1211 fp
->ctf_names
.ctn_writable
= NULL
;
1213 memcpy (&ofp
, fp
, sizeof (ctf_dict_t
));
1214 memcpy (fp
, nfp
, sizeof (ctf_dict_t
));
1215 memcpy (nfp
, &ofp
, sizeof (ctf_dict_t
));
1217 nfp
->ctf_refcnt
= 1; /* Force nfp to be freed. */
1218 ctf_dict_close (nfp
);
1224 return (ctf_set_errno (fp
, EAGAIN
));
1227 return -1; /* errno is set for us. */
1232 /* Write the compressed CTF data stream to the specified gzFile descriptor. The
1233 whole stream is compressed, and cannot be read by CTF opening functions in
1234 this library until it is decompressed. (The functions below this one leave
1235 the header uncompressed, and the CTF opening functions work on them without
1236 manual decompression.)
1238 No support for (testing-only) endian-flipping. */
1240 ctf_gzwrite (ctf_dict_t
*fp
, gzFile fd
)
1242 const unsigned char *buf
;
1246 resid
= sizeof (ctf_header_t
);
1247 buf
= (unsigned char *) fp
->ctf_header
;
1250 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
1251 return (ctf_set_errno (fp
, errno
));
1256 resid
= fp
->ctf_size
;
1260 if ((len
= gzwrite (fd
, buf
, resid
)) <= 0)
1261 return (ctf_set_errno (fp
, errno
));
1269 /* Optionally compress the specified CTF data stream and return it as a new
1270 dynamically-allocated string. Possibly write it with reversed
1273 ctf_write_mem (ctf_dict_t
*fp
, size_t *size
, size_t threshold
)
1278 unsigned char *flipped
, *src
;
1279 ssize_t header_len
= sizeof (ctf_header_t
);
1280 ssize_t compress_len
;
1285 flip_endian
= getenv ("LIBCTF_WRITE_FOREIGN_ENDIAN") != NULL
;
1286 uncompressed
= (fp
->ctf_size
< threshold
);
1288 if (ctf_serialize (fp
) < 0)
1289 return NULL
; /* errno is set for us. */
1291 compress_len
= compressBound (fp
->ctf_size
);
1292 if (fp
->ctf_size
< threshold
)
1293 compress_len
= fp
->ctf_size
;
1294 if ((buf
= malloc (compress_len
1295 + sizeof (struct ctf_header
))) == NULL
)
1297 ctf_set_errno (fp
, ENOMEM
);
1298 ctf_err_warn (fp
, 0, 0, _("ctf_write_mem: cannot allocate %li bytes"),
1299 (unsigned long) (compress_len
+ sizeof (struct ctf_header
)));
1303 hp
= (ctf_header_t
*) buf
;
1304 memcpy (hp
, fp
->ctf_header
, header_len
);
1305 bp
= buf
+ sizeof (struct ctf_header
);
1306 *size
= sizeof (struct ctf_header
);
1309 hp
->cth_flags
&= ~CTF_F_COMPRESS
;
1311 hp
->cth_flags
|= CTF_F_COMPRESS
;
1318 if ((flipped
= malloc (fp
->ctf_size
)) == NULL
)
1320 ctf_set_errno (fp
, ENOMEM
);
1321 ctf_err_warn (fp
, 0, 0, _("ctf_write_mem: cannot allocate %li bytes"),
1322 (unsigned long) (fp
->ctf_size
+ sizeof (struct ctf_header
)));
1325 ctf_flip_header (hp
);
1326 memcpy (flipped
, fp
->ctf_buf
, fp
->ctf_size
);
1327 if (ctf_flip (fp
, fp
->ctf_header
, flipped
, 1) < 0)
1331 return NULL
; /* errno is set for us. */
1338 memcpy (bp
, src
, fp
->ctf_size
);
1339 *size
+= fp
->ctf_size
;
1343 if ((rc
= compress (bp
, (uLongf
*) &compress_len
,
1344 src
, fp
->ctf_size
)) != Z_OK
)
1346 ctf_set_errno (fp
, ECTF_COMPRESS
);
1347 ctf_err_warn (fp
, 0, 0, _("zlib deflate err: %s"), zError (rc
));
1351 *size
+= compress_len
;
1359 /* Compress the specified CTF data stream and write it to the specified file
1362 ctf_compress_write (ctf_dict_t
*fp
, int fd
)
1371 if ((buf
= ctf_write_mem (fp
, &tmp
, 0)) == NULL
)
1372 return -1; /* errno is set for us. */
1379 if ((len
= write (fd
, bp
, buf_len
)) < 0)
1381 err
= ctf_set_errno (fp
, errno
);
1382 ctf_err_warn (fp
, 0, 0, _("ctf_compress_write: error writing"));
1394 /* Write the uncompressed CTF data stream to the specified file descriptor. */
1396 ctf_write (ctf_dict_t
*fp
, int fd
)
1405 if ((buf
= ctf_write_mem (fp
, &tmp
, (size_t) -1)) == NULL
)
1406 return -1; /* errno is set for us. */
1413 if ((len
= write (fd
, bp
, buf_len
)) < 0)
1415 err
= ctf_set_errno (fp
, errno
);
1416 ctf_err_warn (fp
, 0, 0, _("ctf_compress_write: error writing"));