ctf-serialize.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /* CTF dict creation.
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of libctf.
  4. libctf is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <ctf-impl.h>
  16. #include <assert.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <zlib.h>
  20. #include <elf.h>
  21. #include "elf-bfd.h"
  22. /* Symtypetab sections. */
  23. /* Symtypetab emission flags. */
  24. #define CTF_SYMTYPETAB_EMIT_FUNCTION 0x1
  25. #define CTF_SYMTYPETAB_EMIT_PAD 0x2
  26. #define CTF_SYMTYPETAB_FORCE_INDEXED 0x4
  27. /* Properties of symtypetab emission, shared by symtypetab section
  28. sizing and symtypetab emission itself. */
  29. typedef struct emit_symtypetab_state
  30. {
  31. /* True if linker-reported symbols are being filtered out. symfp is set if
  32. this is true: otherwise, indexing is forced and the symflags indicate as
  33. much. */
  34. int filter_syms;
  35. /* True if symbols are being sorted. */
  36. int sort_syms;
  37. /* Flags for symtypetab emission. */
  38. int symflags;
  39. /* The dict to which the linker has reported symbols. */
  40. ctf_dict_t *symfp;
  41. /* The maximum number of objects seen. */
  42. size_t maxobjt;
  43. /* The maximum number of func info entris seen. */
  44. size_t maxfunc;
  45. } emit_symtypetab_state_t;
  46. /* Determine if a symbol is "skippable" and should never appear in the
  47. symtypetab sections. */
  48. int
  49. ctf_symtab_skippable (ctf_link_sym_t *sym)
  50. {
  51. /* Never skip symbols whose name is not yet known. */
  52. if (sym->st_nameidx_set)
  53. return 0;
  54. return (sym->st_name == NULL || sym->st_name[0] == 0
  55. || sym->st_shndx == SHN_UNDEF
  56. || strcmp (sym->st_name, "_START_") == 0
  57. || strcmp (sym->st_name, "_END_") == 0
  58. || (sym->st_type == STT_OBJECT && sym->st_shndx == SHN_EXTABS
  59. && sym->st_value == 0));
  60. }
  61. /* Get the number of symbols in a symbol hash, the count of symbols, the maximum
  62. seen, the eventual size, without any padding elements, of the func/data and
  63. (if generated) index sections, and the size of accumulated padding elements.
  64. The linker-reported set of symbols is found in SYMFP: it may be NULL if
  65. symbol filtering is not desired, in which case CTF_SYMTYPETAB_FORCE_INDEXED
  66. will always be set in the flags.
  67. Also figure out if any symbols need to be moved to the variable section, and
  68. add them (if not already present). */
  69. _libctf_nonnull_ ((1,3,4,5,6,7,8))
  70. static int
  71. symtypetab_density (ctf_dict_t *fp, ctf_dict_t *symfp, ctf_dynhash_t *symhash,
  72. size_t *count, size_t *max, size_t *unpadsize,
  73. size_t *padsize, size_t *idxsize, int flags)
  74. {
  75. ctf_next_t *i = NULL;
  76. const void *name;
  77. const void *ctf_sym;
  78. ctf_dynhash_t *linker_known = NULL;
  79. int err;
  80. int beyond_max = 0;
  81. *count = 0;
  82. *max = 0;
  83. *unpadsize = 0;
  84. *idxsize = 0;
  85. *padsize = 0;
  86. if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
  87. {
  88. /* Make a dynhash citing only symbols reported by the linker of the
  89. appropriate type, then traverse all potential-symbols we know the types
  90. of, removing them from linker_known as we go. Once this is done, the
  91. only symbols remaining in linker_known are symbols we don't know the
  92. types of: we must emit pads for those symbols that are below the
  93. maximum symbol we will emit (any beyond that are simply skipped).
  94. If there are none, this symtypetab will be empty: just report that. */
  95. if (!symfp->ctf_dynsyms)
  96. return 0;
  97. if ((linker_known = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
  98. NULL, NULL)) == NULL)
  99. return (ctf_set_errno (fp, ENOMEM));
  100. while ((err = ctf_dynhash_cnext (symfp->ctf_dynsyms, &i,
  101. &name, &ctf_sym)) == 0)
  102. {
  103. ctf_link_sym_t *sym = (ctf_link_sym_t *) ctf_sym;
  104. if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
  105. && sym->st_type != STT_FUNC)
  106. || (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
  107. && sym->st_type != STT_OBJECT))
  108. continue;
  109. if (ctf_symtab_skippable (sym))
  110. continue;
  111. /* This should only be true briefly before all the names are
  112. finalized, long before we get this far. */
  113. if (!ctf_assert (fp, !sym->st_nameidx_set))
  114. return -1; /* errno is set for us. */
  115. if (ctf_dynhash_cinsert (linker_known, name, ctf_sym) < 0)
  116. {
  117. ctf_dynhash_destroy (linker_known);
  118. return (ctf_set_errno (fp, ENOMEM));
  119. }
  120. }
  121. if (err != ECTF_NEXT_END)
  122. {
  123. ctf_err_warn (fp, 0, err, _("iterating over linker-known symbols during "
  124. "serialization"));
  125. ctf_dynhash_destroy (linker_known);
  126. return (ctf_set_errno (fp, err));
  127. }
  128. }
  129. while ((err = ctf_dynhash_cnext (symhash, &i, &name, NULL)) == 0)
  130. {
  131. ctf_link_sym_t *sym;
  132. if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
  133. {
  134. /* Linker did not report symbol in symtab. Remove it from the
  135. set of known data symbols and continue. */
  136. if ((sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, name)) == NULL)
  137. {
  138. ctf_dynhash_remove (symhash, name);
  139. continue;
  140. }
  141. /* We don't remove skippable symbols from the symhash because we don't
  142. want them to be migrated into variables. */
  143. if (ctf_symtab_skippable (sym))
  144. continue;
  145. if ((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
  146. && sym->st_type != STT_FUNC)
  147. {
  148. ctf_err_warn (fp, 1, 0, _("symbol %s (%x) added to CTF as a "
  149. "function but is of type %x. "
  150. "The symbol type lookup tables "
  151. "are probably corrupted"),
  152. sym->st_name, sym->st_symidx, sym->st_type);
  153. ctf_dynhash_remove (symhash, name);
  154. continue;
  155. }
  156. else if (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
  157. && sym->st_type != STT_OBJECT)
  158. {
  159. ctf_err_warn (fp, 1, 0, _("symbol %s (%x) added to CTF as a "
  160. "data object but is of type %x. "
  161. "The symbol type lookup tables "
  162. "are probably corrupted"),
  163. sym->st_name, sym->st_symidx, sym->st_type);
  164. ctf_dynhash_remove (symhash, name);
  165. continue;
  166. }
  167. ctf_dynhash_remove (linker_known, name);
  168. }
  169. *unpadsize += sizeof (uint32_t);
  170. (*count)++;
  171. if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
  172. {
  173. if (*max < sym->st_symidx)
  174. *max = sym->st_symidx;
  175. }
  176. else
  177. (*max)++;
  178. }
  179. if (err != ECTF_NEXT_END)
  180. {
  181. ctf_err_warn (fp, 0, err, _("iterating over CTF symtypetab during "
  182. "serialization"));
  183. ctf_dynhash_destroy (linker_known);
  184. return (ctf_set_errno (fp, err));
  185. }
  186. if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
  187. {
  188. while ((err = ctf_dynhash_cnext (linker_known, &i, NULL, &ctf_sym)) == 0)
  189. {
  190. ctf_link_sym_t *sym = (ctf_link_sym_t *) ctf_sym;
  191. if (sym->st_symidx > *max)
  192. beyond_max++;
  193. }
  194. if (err != ECTF_NEXT_END)
  195. {
  196. ctf_err_warn (fp, 0, err, _("iterating over linker-known symbols "
  197. "during CTF serialization"));
  198. ctf_dynhash_destroy (linker_known);
  199. return (ctf_set_errno (fp, err));
  200. }
  201. }
  202. *idxsize = *count * sizeof (uint32_t);
  203. if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
  204. *padsize = (ctf_dynhash_elements (linker_known) - beyond_max) * sizeof (uint32_t);
  205. ctf_dynhash_destroy (linker_known);
  206. return 0;
  207. }
  208. /* Emit an objt or func symtypetab into DP in a particular order defined by an
  209. array of ctf_link_sym_t or symbol names passed in. The index has NIDX
  210. elements in it: unindexed output would terminate at symbol OUTMAX and is in
  211. any case no larger than SIZE bytes. Some index elements are expected to be
  212. skipped: see symtypetab_density. The linker-reported set of symbols (if any)
  213. is found in SYMFP. */
  214. static int
  215. emit_symtypetab (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp,
  216. ctf_link_sym_t **idx, const char **nameidx, uint32_t nidx,
  217. uint32_t outmax, int size, int flags)
  218. {
  219. uint32_t i;
  220. uint32_t *dpp = dp;
  221. ctf_dynhash_t *symhash;
  222. ctf_dprintf ("Emitting table of size %i, outmax %u, %u symtypetab entries, "
  223. "flags %i\n", size, outmax, nidx, flags);
  224. /* Empty table? Nothing to do. */
  225. if (size == 0)
  226. return 0;
  227. if (flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
  228. symhash = fp->ctf_funchash;
  229. else
  230. symhash = fp->ctf_objthash;
  231. for (i = 0; i < nidx; i++)
  232. {
  233. const char *sym_name;
  234. void *type;
  235. /* If we have a linker-reported set of symbols, we may be given that set
  236. to work from, or a set of symbol names. In both cases we want to look
  237. at the corresponding linker-reported symbol (if any). */
  238. if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
  239. {
  240. ctf_link_sym_t *this_link_sym;
  241. if (idx)
  242. this_link_sym = idx[i];
  243. else
  244. this_link_sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, nameidx[i]);
  245. /* Unreported symbol number. No pad, no nothing. */
  246. if (!this_link_sym)
  247. continue;
  248. /* Symbol of the wrong type, or skippable? This symbol is not in this
  249. table. */
  250. if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
  251. && this_link_sym->st_type != STT_FUNC)
  252. || (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
  253. && this_link_sym->st_type != STT_OBJECT))
  254. continue;
  255. if (ctf_symtab_skippable (this_link_sym))
  256. continue;
  257. sym_name = this_link_sym->st_name;
  258. /* Linker reports symbol of a different type to the symbol we actually
  259. added? Skip the symbol. No pad, since the symbol doesn't actually
  260. belong in this table at all. (Warned about in
  261. symtypetab_density.) */
  262. if ((this_link_sym->st_type == STT_FUNC)
  263. && (ctf_dynhash_lookup (fp->ctf_objthash, sym_name)))
  264. continue;
  265. if ((this_link_sym->st_type == STT_OBJECT)
  266. && (ctf_dynhash_lookup (fp->ctf_funchash, sym_name)))
  267. continue;
  268. }
  269. else
  270. sym_name = nameidx[i];
  271. /* Symbol in index but no type set? Silently skip and (optionally)
  272. pad. (In force-indexed mode, this is also where we track symbols of
  273. the wrong type for this round of insertion.) */
  274. if ((type = ctf_dynhash_lookup (symhash, sym_name)) == NULL)
  275. {
  276. if (flags & CTF_SYMTYPETAB_EMIT_PAD)
  277. *dpp++ = 0;
  278. continue;
  279. }
  280. if (!ctf_assert (fp, (((char *) dpp) - (char *) dp) < size))
  281. return -1; /* errno is set for us. */
  282. *dpp++ = (ctf_id_t) (uintptr_t) type;
  283. /* When emitting unindexed output, all later symbols are pads: stop
  284. early. */
  285. if ((flags & CTF_SYMTYPETAB_EMIT_PAD) && idx[i]->st_symidx == outmax)
  286. break;
  287. }
  288. return 0;
  289. }
  290. /* Emit an objt or func symtypetab index into DP in a paticular order defined by
  291. an array of symbol names passed in. Stop at NIDX. The linker-reported set
  292. of symbols (if any) is found in SYMFP. */
  293. static int
  294. emit_symtypetab_index (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp,
  295. const char **idx, uint32_t nidx, int size, int flags)
  296. {
  297. uint32_t i;
  298. uint32_t *dpp = dp;
  299. ctf_dynhash_t *symhash;
  300. ctf_dprintf ("Emitting index of size %i, %u entries reported by linker, "
  301. "flags %i\n", size, nidx, flags);
  302. /* Empty table? Nothing to do. */
  303. if (size == 0)
  304. return 0;
  305. if (flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
  306. symhash = fp->ctf_funchash;
  307. else
  308. symhash = fp->ctf_objthash;
  309. /* Indexes should always be unpadded. */
  310. if (!ctf_assert (fp, !(flags & CTF_SYMTYPETAB_EMIT_PAD)))
  311. return -1; /* errno is set for us. */
  312. for (i = 0; i < nidx; i++)
  313. {
  314. const char *sym_name;
  315. void *type;
  316. if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
  317. {
  318. ctf_link_sym_t *this_link_sym;
  319. this_link_sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, idx[i]);
  320. /* This is an index: unreported symbols should never appear in it. */
  321. if (!ctf_assert (fp, this_link_sym != NULL))
  322. return -1; /* errno is set for us. */
  323. /* Symbol of the wrong type, or skippable? This symbol is not in this
  324. table. */
  325. if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
  326. && this_link_sym->st_type != STT_FUNC)
  327. || (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
  328. && this_link_sym->st_type != STT_OBJECT))
  329. continue;
  330. if (ctf_symtab_skippable (this_link_sym))
  331. continue;
  332. sym_name = this_link_sym->st_name;
  333. /* Linker reports symbol of a different type to the symbol we actually
  334. added? Skip the symbol. */
  335. if ((this_link_sym->st_type == STT_FUNC)
  336. && (ctf_dynhash_lookup (fp->ctf_objthash, sym_name)))
  337. continue;
  338. if ((this_link_sym->st_type == STT_OBJECT)
  339. && (ctf_dynhash_lookup (fp->ctf_funchash, sym_name)))
  340. continue;
  341. }
  342. else
  343. sym_name = idx[i];
  344. /* Symbol in index and reported by linker, but no type set? Silently skip
  345. and (optionally) pad. (In force-indexed mode, this is also where we
  346. track symbols of the wrong type for this round of insertion.) */
  347. if ((type = ctf_dynhash_lookup (symhash, sym_name)) == NULL)
  348. continue;
  349. ctf_str_add_ref (fp, sym_name, dpp++);
  350. if (!ctf_assert (fp, (((char *) dpp) - (char *) dp) <= size))
  351. return -1; /* errno is set for us. */
  352. }
  353. return 0;
  354. }
  355. /* Delete symbols that have been assigned names from the variable section. Must
  356. be called from within ctf_serialize, because that is the only place you can
  357. safely delete variables without messing up ctf_rollback. */
  358. static int
  359. symtypetab_delete_nonstatics (ctf_dict_t *fp, ctf_dict_t *symfp)
  360. {
  361. ctf_dvdef_t *dvd, *nvd;
  362. ctf_id_t type;
  363. for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd)
  364. {
  365. nvd = ctf_list_next (dvd);
  366. if ((((type = (ctf_id_t) (uintptr_t)
  367. ctf_dynhash_lookup (fp->ctf_objthash, dvd->dvd_name)) > 0)
  368. || (type = (ctf_id_t) (uintptr_t)
  369. ctf_dynhash_lookup (fp->ctf_funchash, dvd->dvd_name)) > 0)
  370. && ctf_dynhash_lookup (symfp->ctf_dynsyms, dvd->dvd_name) != NULL
  371. && type == dvd->dvd_type)
  372. ctf_dvd_delete (fp, dvd);
  373. }
  374. return 0;
  375. }
  376. /* Figure out the sizes of the symtypetab sections, their indexed state,
  377. etc. */
  378. static int
  379. ctf_symtypetab_sect_sizes (ctf_dict_t *fp, emit_symtypetab_state_t *s,
  380. ctf_header_t *hdr, size_t *objt_size,
  381. size_t *func_size, size_t *objtidx_size,
  382. size_t *funcidx_size)
  383. {
  384. size_t nfuncs, nobjts;
  385. size_t objt_unpadsize, func_unpadsize, objt_padsize, func_padsize;
  386. /* If doing a writeout as part of linking, and the link flags request it,
  387. filter out reported symbols from the variable section, and filter out all
  388. other symbols from the symtypetab sections. (If we are not linking, the
  389. symbols are sorted; if we are linking, don't bother sorting if we are not
  390. filtering out reported symbols: this is almost certaily an ld -r and only
  391. the linker is likely to consume these symtypetabs again. The linker
  392. doesn't care what order the symtypetab entries is in, since it only
  393. iterates over symbols and does not use the ctf_lookup_by_symbol* API.) */
  394. s->sort_syms = 1;
  395. if (fp->ctf_flags & LCTF_LINKING)
  396. {
  397. s->filter_syms = !(fp->ctf_link_flags & CTF_LINK_NO_FILTER_REPORTED_SYMS);
  398. if (!s->filter_syms)
  399. s->sort_syms = 0;
  400. }
  401. /* Find the dict to which the linker has reported symbols, if any. */
  402. if (s->filter_syms)
  403. {
  404. if (!fp->ctf_dynsyms && fp->ctf_parent && fp->ctf_parent->ctf_dynsyms)
  405. s->symfp = fp->ctf_parent;
  406. else
  407. s->symfp = fp;
  408. }
  409. /* If not filtering, keep all potential symbols in an unsorted, indexed
  410. dict. */
  411. if (!s->filter_syms)
  412. s->symflags = CTF_SYMTYPETAB_FORCE_INDEXED;
  413. else
  414. hdr->cth_flags |= CTF_F_IDXSORTED;
  415. if (!ctf_assert (fp, (s->filter_syms && s->symfp)
  416. || (!s->filter_syms && !s->symfp
  417. && ((s->symflags & CTF_SYMTYPETAB_FORCE_INDEXED) != 0))))
  418. return -1;
  419. /* Work out the sizes of the object and function sections, and work out the
  420. number of pad (unassigned) symbols in each, and the overall size of the
  421. sections. */
  422. if (symtypetab_density (fp, s->symfp, fp->ctf_objthash, &nobjts, &s->maxobjt,
  423. &objt_unpadsize, &objt_padsize, objtidx_size,
  424. s->symflags) < 0)
  425. return -1; /* errno is set for us. */
  426. ctf_dprintf ("Object symtypetab: %i objects, max %i, unpadded size %i, "
  427. "%i bytes of pads, index size %i\n", (int) nobjts,
  428. (int) s->maxobjt, (int) objt_unpadsize, (int) objt_padsize,
  429. (int) *objtidx_size);
  430. if (symtypetab_density (fp, s->symfp, fp->ctf_funchash, &nfuncs, &s->maxfunc,
  431. &func_unpadsize, &func_padsize, funcidx_size,
  432. s->symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0)
  433. return -1; /* errno is set for us. */
  434. ctf_dprintf ("Function symtypetab: %i functions, max %i, unpadded size %i, "
  435. "%i bytes of pads, index size %i\n", (int) nfuncs,
  436. (int) s->maxfunc, (int) func_unpadsize, (int) func_padsize,
  437. (int) *funcidx_size);
  438. /* It is worth indexing each section if it would save space to do so, due to
  439. reducing the number of pads sufficiently. A pad is the same size as a
  440. single index entry: but index sections compress relatively poorly compared
  441. to constant pads, so it takes a lot of contiguous padding to equal one
  442. index section entry. It would be nice to be able to *verify* whether we
  443. would save space after compression rather than guessing, but this seems
  444. difficult, since it would require complete reserialization. Regardless, if
  445. the linker has not reported any symbols (e.g. if this is not a final link
  446. but just an ld -r), we must emit things in indexed fashion just as the
  447. compiler does. */
  448. *objt_size = objt_unpadsize;
  449. if (!(s->symflags & CTF_SYMTYPETAB_FORCE_INDEXED)
  450. && ((objt_padsize + objt_unpadsize) * CTF_INDEX_PAD_THRESHOLD
  451. > objt_padsize))
  452. {
  453. *objt_size += objt_padsize;
  454. *objtidx_size = 0;
  455. }
  456. *func_size = func_unpadsize;
  457. if (!(s->symflags & CTF_SYMTYPETAB_FORCE_INDEXED)
  458. && ((func_padsize + func_unpadsize) * CTF_INDEX_PAD_THRESHOLD
  459. > func_padsize))
  460. {
  461. *func_size += func_padsize;
  462. *funcidx_size = 0;
  463. }
  464. /* If we are filtering symbols out, those symbols that the linker has not
  465. reported have now been removed from the ctf_objthash and ctf_funchash.
  466. Delete entries from the variable section that duplicate newly-added
  467. symbols. There's no need to migrate new ones in: we do that (if necessary)
  468. in ctf_link_deduplicating_variables. */
  469. if (s->filter_syms && s->symfp->ctf_dynsyms &&
  470. symtypetab_delete_nonstatics (fp, s->symfp) < 0)
  471. return -1;
  472. return 0;
  473. }
  474. static int
  475. ctf_emit_symtypetab_sects (ctf_dict_t *fp, emit_symtypetab_state_t *s,
  476. unsigned char **tptr, size_t objt_size,
  477. size_t func_size, size_t objtidx_size,
  478. size_t funcidx_size)
  479. {
  480. unsigned char *t = *tptr;
  481. size_t nsymtypes = 0;
  482. const char **sym_name_order = NULL;
  483. int err;
  484. /* Sort the linker's symbols into name order if need be. */
  485. if ((objtidx_size != 0) || (funcidx_size != 0))
  486. {
  487. ctf_next_t *i = NULL;
  488. void *symname;
  489. const char **walk;
  490. if (s->filter_syms)
  491. {
  492. if (s->symfp->ctf_dynsyms)
  493. nsymtypes = ctf_dynhash_elements (s->symfp->ctf_dynsyms);
  494. else
  495. nsymtypes = 0;
  496. }
  497. else
  498. nsymtypes = ctf_dynhash_elements (fp->ctf_objthash)
  499. + ctf_dynhash_elements (fp->ctf_funchash);
  500. if ((sym_name_order = calloc (nsymtypes, sizeof (const char *))) == NULL)
  501. goto oom;
  502. walk = sym_name_order;
  503. if (s->filter_syms)
  504. {
  505. if (s->symfp->ctf_dynsyms)
  506. {
  507. while ((err = ctf_dynhash_next_sorted (s->symfp->ctf_dynsyms, &i,
  508. &symname, NULL,
  509. ctf_dynhash_sort_by_name,
  510. NULL)) == 0)
  511. *walk++ = (const char *) symname;
  512. if (err != ECTF_NEXT_END)
  513. goto symerr;
  514. }
  515. }
  516. else
  517. {
  518. ctf_hash_sort_f sort_fun = NULL;
  519. /* Since we partition the set of symbols back into objt and func,
  520. we can sort the two independently without harm. */
  521. if (s->sort_syms)
  522. sort_fun = ctf_dynhash_sort_by_name;
  523. while ((err = ctf_dynhash_next_sorted (fp->ctf_objthash, &i, &symname,
  524. NULL, sort_fun, NULL)) == 0)
  525. *walk++ = (const char *) symname;
  526. if (err != ECTF_NEXT_END)
  527. goto symerr;
  528. while ((err = ctf_dynhash_next_sorted (fp->ctf_funchash, &i, &symname,
  529. NULL, sort_fun, NULL)) == 0)
  530. *walk++ = (const char *) symname;
  531. if (err != ECTF_NEXT_END)
  532. goto symerr;
  533. }
  534. }
  535. /* Emit the object and function sections, and if necessary their indexes.
  536. Emission is done in symtab order if there is no index, and in index
  537. (name) order otherwise. */
  538. if ((objtidx_size == 0) && s->symfp && s->symfp->ctf_dynsymidx)
  539. {
  540. ctf_dprintf ("Emitting unindexed objt symtypetab\n");
  541. if (emit_symtypetab (fp, s->symfp, (uint32_t *) t,
  542. s->symfp->ctf_dynsymidx, NULL,
  543. s->symfp->ctf_dynsymmax + 1, s->maxobjt,
  544. objt_size, s->symflags | CTF_SYMTYPETAB_EMIT_PAD) < 0)
  545. goto err; /* errno is set for us. */
  546. }
  547. else
  548. {
  549. ctf_dprintf ("Emitting indexed objt symtypetab\n");
  550. if (emit_symtypetab (fp, s->symfp, (uint32_t *) t, NULL,
  551. sym_name_order, nsymtypes, s->maxobjt,
  552. objt_size, s->symflags) < 0)
  553. goto err; /* errno is set for us. */
  554. }
  555. t += objt_size;
  556. if ((funcidx_size == 0) && s->symfp && s->symfp->ctf_dynsymidx)
  557. {
  558. ctf_dprintf ("Emitting unindexed func symtypetab\n");
  559. if (emit_symtypetab (fp, s->symfp, (uint32_t *) t,
  560. s->symfp->ctf_dynsymidx, NULL,
  561. s->symfp->ctf_dynsymmax + 1, s->maxfunc,
  562. func_size, s->symflags | CTF_SYMTYPETAB_EMIT_FUNCTION
  563. | CTF_SYMTYPETAB_EMIT_PAD) < 0)
  564. goto err; /* errno is set for us. */
  565. }
  566. else
  567. {
  568. ctf_dprintf ("Emitting indexed func symtypetab\n");
  569. if (emit_symtypetab (fp, s->symfp, (uint32_t *) t, NULL, sym_name_order,
  570. nsymtypes, s->maxfunc, func_size,
  571. s->symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0)
  572. goto err; /* errno is set for us. */
  573. }
  574. t += func_size;
  575. if (objtidx_size > 0)
  576. if (emit_symtypetab_index (fp, s->symfp, (uint32_t *) t, sym_name_order,
  577. nsymtypes, objtidx_size, s->symflags) < 0)
  578. goto err;
  579. t += objtidx_size;
  580. if (funcidx_size > 0)
  581. if (emit_symtypetab_index (fp, s->symfp, (uint32_t *) t, sym_name_order,
  582. nsymtypes, funcidx_size,
  583. s->symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0)
  584. goto err;
  585. t += funcidx_size;
  586. free (sym_name_order);
  587. *tptr = t;
  588. return 0;
  589. oom:
  590. ctf_set_errno (fp, EAGAIN);
  591. goto err;
  592. symerr:
  593. ctf_err_warn (fp, 0, err, _("error serializing symtypetabs"));
  594. err:
  595. free (sym_name_order);
  596. return -1;
  597. }
  598. /* Type section. */
  599. /* Iterate through the dynamic type definition list and compute the
  600. size of the CTF type section. */
  601. static size_t
  602. ctf_type_sect_size (ctf_dict_t *fp)
  603. {
  604. ctf_dtdef_t *dtd;
  605. size_t type_size;
  606. for (type_size = 0, dtd = ctf_list_next (&fp->ctf_dtdefs);
  607. dtd != NULL; dtd = ctf_list_next (dtd))
  608. {
  609. uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
  610. uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
  611. size_t type_ctt_size = dtd->dtd_data.ctt_size;
  612. /* Shrink ctf_type_t-using types from a ctf_type_t to a ctf_stype_t
  613. if possible. */
  614. if (kind == CTF_K_STRUCT || kind == CTF_K_UNION)
  615. {
  616. size_t lsize = CTF_TYPE_LSIZE (&dtd->dtd_data);
  617. if (lsize <= CTF_MAX_SIZE)
  618. type_ctt_size = lsize;
  619. }
  620. if (type_ctt_size != CTF_LSIZE_SENT)
  621. type_size += sizeof (ctf_stype_t);
  622. else
  623. type_size += sizeof (ctf_type_t);
  624. switch (kind)
  625. {
  626. case CTF_K_INTEGER:
  627. case CTF_K_FLOAT:
  628. type_size += sizeof (uint32_t);
  629. break;
  630. case CTF_K_ARRAY:
  631. type_size += sizeof (ctf_array_t);
  632. break;
  633. case CTF_K_SLICE:
  634. type_size += sizeof (ctf_slice_t);
  635. break;
  636. case CTF_K_FUNCTION:
  637. type_size += sizeof (uint32_t) * (vlen + (vlen & 1));
  638. break;
  639. case CTF_K_STRUCT:
  640. case CTF_K_UNION:
  641. if (type_ctt_size < CTF_LSTRUCT_THRESH)
  642. type_size += sizeof (ctf_member_t) * vlen;
  643. else
  644. type_size += sizeof (ctf_lmember_t) * vlen;
  645. break;
  646. case CTF_K_ENUM:
  647. type_size += sizeof (ctf_enum_t) * vlen;
  648. break;
  649. }
  650. }
  651. return type_size;
  652. }
  653. /* Take a final lap through the dynamic type definition list and copy the
  654. appropriate type records to the output buffer, noting down the strings as
  655. we go. */
  656. static void
  657. ctf_emit_type_sect (ctf_dict_t *fp, unsigned char **tptr)
  658. {
  659. unsigned char *t = *tptr;
  660. ctf_dtdef_t *dtd;
  661. for (dtd = ctf_list_next (&fp->ctf_dtdefs);
  662. dtd != NULL; dtd = ctf_list_next (dtd))
  663. {
  664. uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
  665. uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
  666. size_t type_ctt_size = dtd->dtd_data.ctt_size;
  667. size_t len;
  668. ctf_stype_t *copied;
  669. const char *name;
  670. size_t i;
  671. /* Shrink ctf_type_t-using types from a ctf_type_t to a ctf_stype_t
  672. if possible. */
  673. if (kind == CTF_K_STRUCT || kind == CTF_K_UNION)
  674. {
  675. size_t lsize = CTF_TYPE_LSIZE (&dtd->dtd_data);
  676. if (lsize <= CTF_MAX_SIZE)
  677. type_ctt_size = lsize;
  678. }
  679. if (type_ctt_size != CTF_LSIZE_SENT)
  680. len = sizeof (ctf_stype_t);
  681. else
  682. len = sizeof (ctf_type_t);
  683. memcpy (t, &dtd->dtd_data, len);
  684. copied = (ctf_stype_t *) t; /* name is at the start: constant offset. */
  685. if (copied->ctt_name
  686. && (name = ctf_strraw (fp, copied->ctt_name)) != NULL)
  687. {
  688. ctf_str_add_ref (fp, name, &copied->ctt_name);
  689. ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name);
  690. }
  691. copied->ctt_size = type_ctt_size;
  692. t += len;
  693. switch (kind)
  694. {
  695. case CTF_K_INTEGER:
  696. case CTF_K_FLOAT:
  697. memcpy (t, dtd->dtd_vlen, sizeof (uint32_t));
  698. t += sizeof (uint32_t);
  699. break;
  700. case CTF_K_SLICE:
  701. memcpy (t, dtd->dtd_vlen, sizeof (struct ctf_slice));
  702. t += sizeof (struct ctf_slice);
  703. break;
  704. case CTF_K_ARRAY:
  705. memcpy (t, dtd->dtd_vlen, sizeof (struct ctf_array));
  706. t += sizeof (struct ctf_array);
  707. break;
  708. case CTF_K_FUNCTION:
  709. /* Functions with no args also have no vlen. */
  710. if (dtd->dtd_vlen)
  711. memcpy (t, dtd->dtd_vlen, sizeof (uint32_t) * (vlen + (vlen & 1)));
  712. t += sizeof (uint32_t) * (vlen + (vlen & 1));
  713. break;
  714. /* These need to be copied across element by element, depending on
  715. their ctt_size. */
  716. case CTF_K_STRUCT:
  717. case CTF_K_UNION:
  718. {
  719. ctf_lmember_t *dtd_vlen = (ctf_lmember_t *) dtd->dtd_vlen;
  720. ctf_lmember_t *t_lvlen = (ctf_lmember_t *) t;
  721. ctf_member_t *t_vlen = (ctf_member_t *) t;
  722. for (i = 0; i < vlen; i++)
  723. {
  724. const char *name = ctf_strraw (fp, dtd_vlen[i].ctlm_name);
  725. ctf_str_add_ref (fp, name, &dtd_vlen[i].ctlm_name);
  726. if (type_ctt_size < CTF_LSTRUCT_THRESH)
  727. {
  728. t_vlen[i].ctm_name = dtd_vlen[i].ctlm_name;
  729. t_vlen[i].ctm_type = dtd_vlen[i].ctlm_type;
  730. t_vlen[i].ctm_offset = CTF_LMEM_OFFSET (&dtd_vlen[i]);
  731. ctf_str_add_ref (fp, name, &t_vlen[i].ctm_name);
  732. }
  733. else
  734. {
  735. t_lvlen[i] = dtd_vlen[i];
  736. ctf_str_add_ref (fp, name, &t_lvlen[i].ctlm_name);
  737. }
  738. }
  739. }
  740. if (type_ctt_size < CTF_LSTRUCT_THRESH)
  741. t += sizeof (ctf_member_t) * vlen;
  742. else
  743. t += sizeof (ctf_lmember_t) * vlen;
  744. break;
  745. case CTF_K_ENUM:
  746. {
  747. ctf_enum_t *dtd_vlen = (struct ctf_enum *) dtd->dtd_vlen;
  748. ctf_enum_t *t_vlen = (struct ctf_enum *) t;
  749. memcpy (t, dtd->dtd_vlen, sizeof (struct ctf_enum) * vlen);
  750. for (i = 0; i < vlen; i++)
  751. {
  752. const char *name = ctf_strraw (fp, dtd_vlen[i].cte_name);
  753. ctf_str_add_ref (fp, name, &t_vlen[i].cte_name);
  754. ctf_str_add_ref (fp, name, &dtd_vlen[i].cte_name);
  755. }
  756. t += sizeof (struct ctf_enum) * vlen;
  757. break;
  758. }
  759. }
  760. }
  761. *tptr = t;
  762. }
  763. /* Variable section. */
  764. /* Sort a newly-constructed static variable array. */
  765. typedef struct ctf_sort_var_arg_cb
  766. {
  767. ctf_dict_t *fp;
  768. ctf_strs_t *strtab;
  769. } ctf_sort_var_arg_cb_t;
  770. static int
  771. ctf_sort_var (const void *one_, const void *two_, void *arg_)
  772. {
  773. const ctf_varent_t *one = one_;
  774. const ctf_varent_t *two = two_;
  775. ctf_sort_var_arg_cb_t *arg = arg_;
  776. return (strcmp (ctf_strraw_explicit (arg->fp, one->ctv_name, arg->strtab),
  777. ctf_strraw_explicit (arg->fp, two->ctv_name, arg->strtab)));
  778. }
  779. /* Overall serialization. */
  780. /* If the specified CTF dict is writable and has been modified, reload this dict
  781. with the updated type definitions, ready for serialization. In order to make
  782. this code and the rest of libctf as simple as possible, we perform updates by
  783. taking the dynamic type definitions and creating an in-memory CTF dict
  784. containing the definitions, and then call ctf_simple_open_internal() on it.
  785. We perform one extra trick here for the benefit of callers and to keep our
  786. code simple: ctf_simple_open_internal() will return a new ctf_dict_t, but we
  787. want to keep the fp constant for the caller, so after
  788. ctf_simple_open_internal() returns, we use memcpy to swap the interior of the
  789. old and new ctf_dict_t's, and then free the old. */
  790. int
  791. ctf_serialize (ctf_dict_t *fp)
  792. {
  793. ctf_dict_t ofp, *nfp;
  794. ctf_header_t hdr, *hdrp;
  795. ctf_dvdef_t *dvd;
  796. ctf_varent_t *dvarents;
  797. ctf_strs_writable_t strtab;
  798. int err;
  799. int num_missed_str_refs;
  800. unsigned char *t;
  801. unsigned long i;
  802. size_t buf_size, type_size, objt_size, func_size;
  803. size_t funcidx_size, objtidx_size;
  804. size_t nvars;
  805. unsigned char *buf = NULL, *newbuf;
  806. emit_symtypetab_state_t symstate;
  807. memset (&symstate, 0, sizeof (emit_symtypetab_state_t));
  808. if (!(fp->ctf_flags & LCTF_RDWR))
  809. return (ctf_set_errno (fp, ECTF_RDONLY));
  810. /* Update required? */
  811. if (!(fp->ctf_flags & LCTF_DIRTY))
  812. return 0;
  813. /* The strtab refs table must be empty at this stage. Any refs already added
  814. will be corrupted by any modifications, including reserialization, after
  815. strtab finalization is complete. Only this function, and functions it
  816. calls, may add refs, and all memory locations (including in the dtds)
  817. containing strtab offsets must be traversed as part of serialization, and
  818. refs added. */
  819. if (!ctf_assert (fp, fp->ctf_str_num_refs == 0))
  820. return -1; /* errno is set for us. */
  821. /* Fill in an initial CTF header. We will leave the label, object,
  822. and function sections empty and only output a header, type section,
  823. and string table. The type section begins at a 4-byte aligned
  824. boundary past the CTF header itself (at relative offset zero). The flag
  825. indicating a new-style function info section (an array of CTF_K_FUNCTION
  826. type IDs in the types section) is flipped on. */
  827. memset (&hdr, 0, sizeof (hdr));
  828. hdr.cth_magic = CTF_MAGIC;
  829. hdr.cth_version = CTF_VERSION;
  830. /* This is a new-format func info section, and the symtab and strtab come out
  831. of the dynsym and dynstr these days. */
  832. hdr.cth_flags = (CTF_F_NEWFUNCINFO | CTF_F_DYNSTR);
  833. if (ctf_symtypetab_sect_sizes (fp, &symstate, &hdr, &objt_size, &func_size,
  834. &objtidx_size, &funcidx_size) < 0)
  835. return -1; /* errno is set for us. */
  836. for (nvars = 0, dvd = ctf_list_next (&fp->ctf_dvdefs);
  837. dvd != NULL; dvd = ctf_list_next (dvd), nvars++);
  838. type_size = ctf_type_sect_size (fp);
  839. /* Compute the size of the CTF buffer we need, sans only the string table,
  840. then allocate a new buffer and memcpy the finished header to the start of
  841. the buffer. (We will adjust this later with strtab length info.) */
  842. hdr.cth_lbloff = hdr.cth_objtoff = 0;
  843. hdr.cth_funcoff = hdr.cth_objtoff + objt_size;
  844. hdr.cth_objtidxoff = hdr.cth_funcoff + func_size;
  845. hdr.cth_funcidxoff = hdr.cth_objtidxoff + objtidx_size;
  846. hdr.cth_varoff = hdr.cth_funcidxoff + funcidx_size;
  847. hdr.cth_typeoff = hdr.cth_varoff + (nvars * sizeof (ctf_varent_t));
  848. hdr.cth_stroff = hdr.cth_typeoff + type_size;
  849. hdr.cth_strlen = 0;
  850. buf_size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
  851. if ((buf = malloc (buf_size)) == NULL)
  852. return (ctf_set_errno (fp, EAGAIN));
  853. memcpy (buf, &hdr, sizeof (ctf_header_t));
  854. t = (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_objtoff;
  855. hdrp = (ctf_header_t *) buf;
  856. if ((fp->ctf_flags & LCTF_CHILD) && (fp->ctf_parname != NULL))
  857. ctf_str_add_ref (fp, fp->ctf_parname, &hdrp->cth_parname);
  858. if (fp->ctf_cuname != NULL)
  859. ctf_str_add_ref (fp, fp->ctf_cuname, &hdrp->cth_cuname);
  860. if (ctf_emit_symtypetab_sects (fp, &symstate, &t, objt_size, func_size,
  861. objtidx_size, funcidx_size) < 0)
  862. goto err;
  863. assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_varoff);
  864. /* Work over the variable list, translating everything into ctf_varent_t's and
  865. prepping the string table. */
  866. dvarents = (ctf_varent_t *) t;
  867. for (i = 0, dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL;
  868. dvd = ctf_list_next (dvd), i++)
  869. {
  870. ctf_varent_t *var = &dvarents[i];
  871. ctf_str_add_ref (fp, dvd->dvd_name, &var->ctv_name);
  872. var->ctv_type = (uint32_t) dvd->dvd_type;
  873. }
  874. assert (i == nvars);
  875. t += sizeof (ctf_varent_t) * nvars;
  876. assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_typeoff);
  877. ctf_emit_type_sect (fp, &t);
  878. assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_stroff);
  879. /* Every string added outside serialization by ctf_str_add_pending should
  880. now have been added by ctf_add_ref. */
  881. num_missed_str_refs = ctf_dynset_elements (fp->ctf_str_pending_ref);
  882. if (!ctf_assert (fp, num_missed_str_refs == 0))
  883. goto err; /* errno is set for us. */
  884. /* Construct the final string table and fill out all the string refs with the
  885. final offsets. Then purge the refs list, because we're about to move this
  886. strtab onto the end of the buf, invalidating all the offsets. */
  887. strtab = ctf_str_write_strtab (fp);
  888. ctf_str_purge_refs (fp);
  889. if (strtab.cts_strs == NULL)
  890. goto oom;
  891. /* Now the string table is constructed, we can sort the buffer of
  892. ctf_varent_t's. */
  893. ctf_sort_var_arg_cb_t sort_var_arg = { fp, (ctf_strs_t *) &strtab };
  894. ctf_qsort_r (dvarents, nvars, sizeof (ctf_varent_t), ctf_sort_var,
  895. &sort_var_arg);
  896. if ((newbuf = ctf_realloc (fp, buf, buf_size + strtab.cts_len)) == NULL)
  897. {
  898. free (strtab.cts_strs);
  899. goto oom;
  900. }
  901. buf = newbuf;
  902. memcpy (buf + buf_size, strtab.cts_strs, strtab.cts_len);
  903. hdrp = (ctf_header_t *) buf;
  904. hdrp->cth_strlen = strtab.cts_len;
  905. buf_size += hdrp->cth_strlen;
  906. free (strtab.cts_strs);
  907. /* Finally, we are ready to ctf_simple_open() the new dict. If this is
  908. successful, we then switch nfp and fp and free the old dict. */
  909. if ((nfp = ctf_simple_open_internal ((char *) buf, buf_size, NULL, 0,
  910. 0, NULL, 0, fp->ctf_syn_ext_strtab,
  911. 1, &err)) == NULL)
  912. {
  913. free (buf);
  914. return (ctf_set_errno (fp, err));
  915. }
  916. (void) ctf_setmodel (nfp, ctf_getmodel (fp));
  917. nfp->ctf_parent = fp->ctf_parent;
  918. nfp->ctf_parent_unreffed = fp->ctf_parent_unreffed;
  919. nfp->ctf_refcnt = fp->ctf_refcnt;
  920. nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
  921. if (nfp->ctf_dynbase == NULL)
  922. nfp->ctf_dynbase = buf; /* Make sure buf is freed on close. */
  923. nfp->ctf_dthash = fp->ctf_dthash;
  924. nfp->ctf_dtdefs = fp->ctf_dtdefs;
  925. nfp->ctf_dvhash = fp->ctf_dvhash;
  926. nfp->ctf_dvdefs = fp->ctf_dvdefs;
  927. nfp->ctf_dtoldid = fp->ctf_dtoldid;
  928. nfp->ctf_add_processing = fp->ctf_add_processing;
  929. nfp->ctf_snapshots = fp->ctf_snapshots + 1;
  930. nfp->ctf_specific = fp->ctf_specific;
  931. nfp->ctf_nfuncidx = fp->ctf_nfuncidx;
  932. nfp->ctf_nobjtidx = fp->ctf_nobjtidx;
  933. nfp->ctf_objthash = fp->ctf_objthash;
  934. nfp->ctf_funchash = fp->ctf_funchash;
  935. nfp->ctf_dynsyms = fp->ctf_dynsyms;
  936. nfp->ctf_ptrtab = fp->ctf_ptrtab;
  937. nfp->ctf_pptrtab = fp->ctf_pptrtab;
  938. nfp->ctf_typemax = fp->ctf_typemax;
  939. nfp->ctf_dynsymidx = fp->ctf_dynsymidx;
  940. nfp->ctf_dynsymmax = fp->ctf_dynsymmax;
  941. nfp->ctf_ptrtab_len = fp->ctf_ptrtab_len;
  942. nfp->ctf_pptrtab_len = fp->ctf_pptrtab_len;
  943. nfp->ctf_link_inputs = fp->ctf_link_inputs;
  944. nfp->ctf_link_outputs = fp->ctf_link_outputs;
  945. nfp->ctf_errs_warnings = fp->ctf_errs_warnings;
  946. nfp->ctf_funcidx_names = fp->ctf_funcidx_names;
  947. nfp->ctf_objtidx_names = fp->ctf_objtidx_names;
  948. nfp->ctf_funcidx_sxlate = fp->ctf_funcidx_sxlate;
  949. nfp->ctf_objtidx_sxlate = fp->ctf_objtidx_sxlate;
  950. nfp->ctf_str_prov_offset = fp->ctf_str_prov_offset;
  951. nfp->ctf_syn_ext_strtab = fp->ctf_syn_ext_strtab;
  952. nfp->ctf_pptrtab_typemax = fp->ctf_pptrtab_typemax;
  953. nfp->ctf_in_flight_dynsyms = fp->ctf_in_flight_dynsyms;
  954. nfp->ctf_link_in_cu_mapping = fp->ctf_link_in_cu_mapping;
  955. nfp->ctf_link_out_cu_mapping = fp->ctf_link_out_cu_mapping;
  956. nfp->ctf_link_type_mapping = fp->ctf_link_type_mapping;
  957. nfp->ctf_link_memb_name_changer = fp->ctf_link_memb_name_changer;
  958. nfp->ctf_link_memb_name_changer_arg = fp->ctf_link_memb_name_changer_arg;
  959. nfp->ctf_link_variable_filter = fp->ctf_link_variable_filter;
  960. nfp->ctf_link_variable_filter_arg = fp->ctf_link_variable_filter_arg;
  961. nfp->ctf_symsect_little_endian = fp->ctf_symsect_little_endian;
  962. nfp->ctf_link_flags = fp->ctf_link_flags;
  963. nfp->ctf_dedup_atoms = fp->ctf_dedup_atoms;
  964. nfp->ctf_dedup_atoms_alloc = fp->ctf_dedup_atoms_alloc;
  965. memcpy (&nfp->ctf_dedup, &fp->ctf_dedup, sizeof (fp->ctf_dedup));
  966. nfp->ctf_snapshot_lu = fp->ctf_snapshots;
  967. memcpy (&nfp->ctf_lookups, fp->ctf_lookups, sizeof (fp->ctf_lookups));
  968. nfp->ctf_structs = fp->ctf_structs;
  969. nfp->ctf_unions = fp->ctf_unions;
  970. nfp->ctf_enums = fp->ctf_enums;
  971. nfp->ctf_names = fp->ctf_names;
  972. fp->ctf_dthash = NULL;
  973. ctf_str_free_atoms (nfp);
  974. nfp->ctf_str_atoms = fp->ctf_str_atoms;
  975. nfp->ctf_prov_strtab = fp->ctf_prov_strtab;
  976. nfp->ctf_str_pending_ref = fp->ctf_str_pending_ref;
  977. fp->ctf_str_atoms = NULL;
  978. fp->ctf_prov_strtab = NULL;
  979. fp->ctf_str_pending_ref = NULL;
  980. memset (&fp->ctf_dtdefs, 0, sizeof (ctf_list_t));
  981. memset (&fp->ctf_errs_warnings, 0, sizeof (ctf_list_t));
  982. fp->ctf_add_processing = NULL;
  983. fp->ctf_ptrtab = NULL;
  984. fp->ctf_pptrtab = NULL;
  985. fp->ctf_funcidx_names = NULL;
  986. fp->ctf_objtidx_names = NULL;
  987. fp->ctf_funcidx_sxlate = NULL;
  988. fp->ctf_objtidx_sxlate = NULL;
  989. fp->ctf_objthash = NULL;
  990. fp->ctf_funchash = NULL;
  991. fp->ctf_dynsyms = NULL;
  992. fp->ctf_dynsymidx = NULL;
  993. fp->ctf_link_inputs = NULL;
  994. fp->ctf_link_outputs = NULL;
  995. fp->ctf_syn_ext_strtab = NULL;
  996. fp->ctf_link_in_cu_mapping = NULL;
  997. fp->ctf_link_out_cu_mapping = NULL;
  998. fp->ctf_link_type_mapping = NULL;
  999. fp->ctf_dedup_atoms = NULL;
  1000. fp->ctf_dedup_atoms_alloc = NULL;
  1001. fp->ctf_parent_unreffed = 1;
  1002. fp->ctf_dvhash = NULL;
  1003. memset (&fp->ctf_dvdefs, 0, sizeof (ctf_list_t));
  1004. memset (fp->ctf_lookups, 0, sizeof (fp->ctf_lookups));
  1005. memset (&fp->ctf_in_flight_dynsyms, 0, sizeof (fp->ctf_in_flight_dynsyms));
  1006. memset (&fp->ctf_dedup, 0, sizeof (fp->ctf_dedup));
  1007. fp->ctf_structs.ctn_writable = NULL;
  1008. fp->ctf_unions.ctn_writable = NULL;
  1009. fp->ctf_enums.ctn_writable = NULL;
  1010. fp->ctf_names.ctn_writable = NULL;
  1011. memcpy (&ofp, fp, sizeof (ctf_dict_t));
  1012. memcpy (fp, nfp, sizeof (ctf_dict_t));
  1013. memcpy (nfp, &ofp, sizeof (ctf_dict_t));
  1014. nfp->ctf_refcnt = 1; /* Force nfp to be freed. */
  1015. ctf_dict_close (nfp);
  1016. return 0;
  1017. oom:
  1018. free (buf);
  1019. return (ctf_set_errno (fp, EAGAIN));
  1020. err:
  1021. free (buf);
  1022. return -1; /* errno is set for us. */
  1023. }
  1024. /* File writing. */
  1025. /* Write the compressed CTF data stream to the specified gzFile descriptor. The
  1026. whole stream is compressed, and cannot be read by CTF opening functions in
  1027. this library until it is decompressed. (The functions below this one leave
  1028. the header uncompressed, and the CTF opening functions work on them without
  1029. manual decompression.)
  1030. No support for (testing-only) endian-flipping. */
  1031. int
  1032. ctf_gzwrite (ctf_dict_t *fp, gzFile fd)
  1033. {
  1034. const unsigned char *buf;
  1035. ssize_t resid;
  1036. ssize_t len;
  1037. resid = sizeof (ctf_header_t);
  1038. buf = (unsigned char *) fp->ctf_header;
  1039. while (resid != 0)
  1040. {
  1041. if ((len = gzwrite (fd, buf, resid)) <= 0)
  1042. return (ctf_set_errno (fp, errno));
  1043. resid -= len;
  1044. buf += len;
  1045. }
  1046. resid = fp->ctf_size;
  1047. buf = fp->ctf_buf;
  1048. while (resid != 0)
  1049. {
  1050. if ((len = gzwrite (fd, buf, resid)) <= 0)
  1051. return (ctf_set_errno (fp, errno));
  1052. resid -= len;
  1053. buf += len;
  1054. }
  1055. return 0;
  1056. }
  1057. /* Optionally compress the specified CTF data stream and return it as a new
  1058. dynamically-allocated string. Possibly write it with reversed
  1059. endianness. */
  1060. unsigned char *
  1061. ctf_write_mem (ctf_dict_t *fp, size_t *size, size_t threshold)
  1062. {
  1063. unsigned char *buf;
  1064. unsigned char *bp;
  1065. ctf_header_t *hp;
  1066. unsigned char *flipped, *src;
  1067. ssize_t header_len = sizeof (ctf_header_t);
  1068. ssize_t compress_len;
  1069. int flip_endian;
  1070. int uncompressed;
  1071. int rc;
  1072. flip_endian = getenv ("LIBCTF_WRITE_FOREIGN_ENDIAN") != NULL;
  1073. uncompressed = (fp->ctf_size < threshold);
  1074. if (ctf_serialize (fp) < 0)
  1075. return NULL; /* errno is set for us. */
  1076. compress_len = compressBound (fp->ctf_size);
  1077. if (fp->ctf_size < threshold)
  1078. compress_len = fp->ctf_size;
  1079. if ((buf = malloc (compress_len
  1080. + sizeof (struct ctf_header))) == NULL)
  1081. {
  1082. ctf_set_errno (fp, ENOMEM);
  1083. ctf_err_warn (fp, 0, 0, _("ctf_write_mem: cannot allocate %li bytes"),
  1084. (unsigned long) (compress_len + sizeof (struct ctf_header)));
  1085. return NULL;
  1086. }
  1087. hp = (ctf_header_t *) buf;
  1088. memcpy (hp, fp->ctf_header, header_len);
  1089. bp = buf + sizeof (struct ctf_header);
  1090. *size = sizeof (struct ctf_header);
  1091. if (uncompressed)
  1092. hp->cth_flags &= ~CTF_F_COMPRESS;
  1093. else
  1094. hp->cth_flags |= CTF_F_COMPRESS;
  1095. src = fp->ctf_buf;
  1096. flipped = NULL;
  1097. if (flip_endian)
  1098. {
  1099. if ((flipped = malloc (fp->ctf_size)) == NULL)
  1100. {
  1101. ctf_set_errno (fp, ENOMEM);
  1102. ctf_err_warn (fp, 0, 0, _("ctf_write_mem: cannot allocate %li bytes"),
  1103. (unsigned long) fp->ctf_size + sizeof (struct ctf_header));
  1104. return NULL;
  1105. }
  1106. ctf_flip_header (hp);
  1107. memcpy (flipped, fp->ctf_buf, fp->ctf_size);
  1108. if (ctf_flip (fp, fp->ctf_header, flipped, 1) < 0)
  1109. {
  1110. free (buf);
  1111. free (flipped);
  1112. return NULL; /* errno is set for us. */
  1113. }
  1114. src = flipped;
  1115. }
  1116. if (uncompressed)
  1117. {
  1118. memcpy (bp, src, fp->ctf_size);
  1119. *size += fp->ctf_size;
  1120. }
  1121. else
  1122. {
  1123. if ((rc = compress (bp, (uLongf *) &compress_len,
  1124. src, fp->ctf_size)) != Z_OK)
  1125. {
  1126. ctf_set_errno (fp, ECTF_COMPRESS);
  1127. ctf_err_warn (fp, 0, 0, _("zlib deflate err: %s"), zError (rc));
  1128. free (buf);
  1129. return NULL;
  1130. }
  1131. *size += compress_len;
  1132. }
  1133. free (flipped);
  1134. return buf;
  1135. }
  1136. /* Compress the specified CTF data stream and write it to the specified file
  1137. descriptor. */
  1138. int
  1139. ctf_compress_write (ctf_dict_t *fp, int fd)
  1140. {
  1141. unsigned char *buf;
  1142. unsigned char *bp;
  1143. size_t tmp;
  1144. ssize_t buf_len;
  1145. ssize_t len;
  1146. int err = 0;
  1147. if ((buf = ctf_write_mem (fp, &tmp, 0)) == NULL)
  1148. return -1; /* errno is set for us. */
  1149. buf_len = tmp;
  1150. bp = buf;
  1151. while (buf_len > 0)
  1152. {
  1153. if ((len = write (fd, bp, buf_len)) < 0)
  1154. {
  1155. err = ctf_set_errno (fp, errno);
  1156. ctf_err_warn (fp, 0, 0, _("ctf_compress_write: error writing"));
  1157. goto ret;
  1158. }
  1159. buf_len -= len;
  1160. bp += len;
  1161. }
  1162. ret:
  1163. free (buf);
  1164. return err;
  1165. }
  1166. /* Write the uncompressed CTF data stream to the specified file descriptor. */
  1167. int
  1168. ctf_write (ctf_dict_t *fp, int fd)
  1169. {
  1170. unsigned char *buf;
  1171. unsigned char *bp;
  1172. size_t tmp;
  1173. ssize_t buf_len;
  1174. ssize_t len;
  1175. int err = 0;
  1176. if ((buf = ctf_write_mem (fp, &tmp, (size_t) -1)) == NULL)
  1177. return -1; /* errno is set for us. */
  1178. buf_len = tmp;
  1179. bp = buf;
  1180. while (buf_len > 0)
  1181. {
  1182. if ((len = write (fd, bp, buf_len)) < 0)
  1183. {
  1184. err = ctf_set_errno (fp, errno);
  1185. ctf_err_warn (fp, 0, 0, _("ctf_compress_write: error writing"));
  1186. goto ret;
  1187. }
  1188. buf_len -= len;
  1189. bp += len;
  1190. }
  1191. ret:
  1192. free (buf);
  1193. return err;
  1194. }