ctf-open.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  1. /* Opening CTF files.
  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 <stddef.h>
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include <elf.h>
  20. #include "swap.h"
  21. #include <bfd.h>
  22. #include <zlib.h>
  23. static const ctf_dmodel_t _libctf_models[] = {
  24. {"ILP32", CTF_MODEL_ILP32, 4, 1, 2, 4, 4},
  25. {"LP64", CTF_MODEL_LP64, 8, 1, 2, 4, 8},
  26. {NULL, 0, 0, 0, 0, 0, 0}
  27. };
  28. const char _CTF_SECTION[] = ".ctf";
  29. const char _CTF_NULLSTR[] = "";
  30. /* Version-sensitive accessors. */
  31. static uint32_t
  32. get_kind_v1 (uint32_t info)
  33. {
  34. return (CTF_V1_INFO_KIND (info));
  35. }
  36. static uint32_t
  37. get_root_v1 (uint32_t info)
  38. {
  39. return (CTF_V1_INFO_ISROOT (info));
  40. }
  41. static uint32_t
  42. get_vlen_v1 (uint32_t info)
  43. {
  44. return (CTF_V1_INFO_VLEN (info));
  45. }
  46. static uint32_t
  47. get_kind_v2 (uint32_t info)
  48. {
  49. return (CTF_V2_INFO_KIND (info));
  50. }
  51. static uint32_t
  52. get_root_v2 (uint32_t info)
  53. {
  54. return (CTF_V2_INFO_ISROOT (info));
  55. }
  56. static uint32_t
  57. get_vlen_v2 (uint32_t info)
  58. {
  59. return (CTF_V2_INFO_VLEN (info));
  60. }
  61. static inline ssize_t
  62. get_ctt_size_common (const ctf_dict_t *fp _libctf_unused_,
  63. const ctf_type_t *tp _libctf_unused_,
  64. ssize_t *sizep, ssize_t *incrementp, size_t lsize,
  65. size_t csize, size_t ctf_type_size,
  66. size_t ctf_stype_size, size_t ctf_lsize_sent)
  67. {
  68. ssize_t size, increment;
  69. if (csize == ctf_lsize_sent)
  70. {
  71. size = lsize;
  72. increment = ctf_type_size;
  73. }
  74. else
  75. {
  76. size = csize;
  77. increment = ctf_stype_size;
  78. }
  79. if (sizep)
  80. *sizep = size;
  81. if (incrementp)
  82. *incrementp = increment;
  83. return size;
  84. }
  85. static ssize_t
  86. get_ctt_size_v1 (const ctf_dict_t *fp, const ctf_type_t *tp,
  87. ssize_t *sizep, ssize_t *incrementp)
  88. {
  89. ctf_type_v1_t *t1p = (ctf_type_v1_t *) tp;
  90. return (get_ctt_size_common (fp, tp, sizep, incrementp,
  91. CTF_TYPE_LSIZE (t1p), t1p->ctt_size,
  92. sizeof (ctf_type_v1_t), sizeof (ctf_stype_v1_t),
  93. CTF_LSIZE_SENT_V1));
  94. }
  95. /* Return the size that a v1 will be once it is converted to v2. */
  96. static ssize_t
  97. get_ctt_size_v2_unconverted (const ctf_dict_t *fp, const ctf_type_t *tp,
  98. ssize_t *sizep, ssize_t *incrementp)
  99. {
  100. ctf_type_v1_t *t1p = (ctf_type_v1_t *) tp;
  101. return (get_ctt_size_common (fp, tp, sizep, incrementp,
  102. CTF_TYPE_LSIZE (t1p), t1p->ctt_size,
  103. sizeof (ctf_type_t), sizeof (ctf_stype_t),
  104. CTF_LSIZE_SENT));
  105. }
  106. static ssize_t
  107. get_ctt_size_v2 (const ctf_dict_t *fp, const ctf_type_t *tp,
  108. ssize_t *sizep, ssize_t *incrementp)
  109. {
  110. return (get_ctt_size_common (fp, tp, sizep, incrementp,
  111. CTF_TYPE_LSIZE (tp), tp->ctt_size,
  112. sizeof (ctf_type_t), sizeof (ctf_stype_t),
  113. CTF_LSIZE_SENT));
  114. }
  115. static ssize_t
  116. get_vbytes_common (ctf_dict_t *fp, unsigned short kind,
  117. ssize_t size _libctf_unused_, size_t vlen)
  118. {
  119. switch (kind)
  120. {
  121. case CTF_K_INTEGER:
  122. case CTF_K_FLOAT:
  123. return (sizeof (uint32_t));
  124. case CTF_K_SLICE:
  125. return (sizeof (ctf_slice_t));
  126. case CTF_K_ENUM:
  127. return (sizeof (ctf_enum_t) * vlen);
  128. case CTF_K_FORWARD:
  129. case CTF_K_UNKNOWN:
  130. case CTF_K_POINTER:
  131. case CTF_K_TYPEDEF:
  132. case CTF_K_VOLATILE:
  133. case CTF_K_CONST:
  134. case CTF_K_RESTRICT:
  135. return 0;
  136. default:
  137. ctf_set_errno (fp, ECTF_CORRUPT);
  138. ctf_err_warn (fp, 0, 0, _("detected invalid CTF kind: %x"), kind);
  139. return -1;
  140. }
  141. }
  142. static ssize_t
  143. get_vbytes_v1 (ctf_dict_t *fp, unsigned short kind, ssize_t size, size_t vlen)
  144. {
  145. switch (kind)
  146. {
  147. case CTF_K_ARRAY:
  148. return (sizeof (ctf_array_v1_t));
  149. case CTF_K_FUNCTION:
  150. return (sizeof (unsigned short) * (vlen + (vlen & 1)));
  151. case CTF_K_STRUCT:
  152. case CTF_K_UNION:
  153. if (size < CTF_LSTRUCT_THRESH_V1)
  154. return (sizeof (ctf_member_v1_t) * vlen);
  155. else
  156. return (sizeof (ctf_lmember_v1_t) * vlen);
  157. }
  158. return (get_vbytes_common (fp, kind, size, vlen));
  159. }
  160. static ssize_t
  161. get_vbytes_v2 (ctf_dict_t *fp, unsigned short kind, ssize_t size, size_t vlen)
  162. {
  163. switch (kind)
  164. {
  165. case CTF_K_ARRAY:
  166. return (sizeof (ctf_array_t));
  167. case CTF_K_FUNCTION:
  168. return (sizeof (uint32_t) * (vlen + (vlen & 1)));
  169. case CTF_K_STRUCT:
  170. case CTF_K_UNION:
  171. if (size < CTF_LSTRUCT_THRESH)
  172. return (sizeof (ctf_member_t) * vlen);
  173. else
  174. return (sizeof (ctf_lmember_t) * vlen);
  175. }
  176. return (get_vbytes_common (fp, kind, size, vlen));
  177. }
  178. static const ctf_dictops_t ctf_dictops[] = {
  179. {NULL, NULL, NULL, NULL, NULL},
  180. /* CTF_VERSION_1 */
  181. {get_kind_v1, get_root_v1, get_vlen_v1, get_ctt_size_v1, get_vbytes_v1},
  182. /* CTF_VERSION_1_UPGRADED_3 */
  183. {get_kind_v2, get_root_v2, get_vlen_v2, get_ctt_size_v2, get_vbytes_v2},
  184. /* CTF_VERSION_2 */
  185. {get_kind_v2, get_root_v2, get_vlen_v2, get_ctt_size_v2, get_vbytes_v2},
  186. /* CTF_VERSION_3, identical to 2: only new type kinds */
  187. {get_kind_v2, get_root_v2, get_vlen_v2, get_ctt_size_v2, get_vbytes_v2},
  188. };
  189. /* Initialize the symtab translation table as appropriate for its indexing
  190. state. For unindexed symtypetabs, fill each entry with the offset of the CTF
  191. type or function data corresponding to each STT_FUNC or STT_OBJECT entry in
  192. the symbol table. For indexed symtypetabs, do nothing: the needed
  193. initialization for indexed lookups may be quite expensive, so it is done only
  194. as needed, when lookups happen. (In particular, the majority of indexed
  195. symtypetabs come from the compiler, and all the linker does is iteration over
  196. all entries, which doesn't need this initialization.)
  197. The SP symbol table section may be NULL if there is no symtab.
  198. If init_symtab works on one call, it cannot fail on future calls to the same
  199. fp: ctf_symsect_endianness relies on this. */
  200. static int
  201. init_symtab (ctf_dict_t *fp, const ctf_header_t *hp, const ctf_sect_t *sp)
  202. {
  203. const unsigned char *symp;
  204. int skip_func_info = 0;
  205. int i;
  206. uint32_t *xp = fp->ctf_sxlate;
  207. uint32_t *xend = PTR_ADD (xp, fp->ctf_nsyms);
  208. uint32_t objtoff = hp->cth_objtoff;
  209. uint32_t funcoff = hp->cth_funcoff;
  210. /* If the CTF_F_NEWFUNCINFO flag is not set, pretend the func info section
  211. is empty: this compiler is too old to emit a function info section we
  212. understand. */
  213. if (!(hp->cth_flags & CTF_F_NEWFUNCINFO))
  214. skip_func_info = 1;
  215. if (hp->cth_objtidxoff < hp->cth_funcidxoff)
  216. fp->ctf_objtidx_names = (uint32_t *) (fp->ctf_buf + hp->cth_objtidxoff);
  217. if (hp->cth_funcidxoff < hp->cth_varoff && !skip_func_info)
  218. fp->ctf_funcidx_names = (uint32_t *) (fp->ctf_buf + hp->cth_funcidxoff);
  219. /* Don't bother doing the rest if everything is indexed, or if we don't have a
  220. symbol table: we will never use it. */
  221. if ((fp->ctf_objtidx_names && fp->ctf_funcidx_names) || !sp || !sp->cts_data)
  222. return 0;
  223. /* The CTF data object and function type sections are ordered to match the
  224. relative order of the respective symbol types in the symtab, unless there
  225. is an index section, in which case the order is arbitrary and the index
  226. gives the mapping. If no type information is available for a symbol table
  227. entry, a pad is inserted in the CTF section. As a further optimization,
  228. anonymous or undefined symbols are omitted from the CTF data. If an
  229. index is available for function symbols but not object symbols, or vice
  230. versa, we populate the xslate table for the unindexed symbols only. */
  231. for (i = 0, symp = sp->cts_data; xp < xend; xp++, symp += sp->cts_entsize,
  232. i++)
  233. {
  234. ctf_link_sym_t sym;
  235. switch (sp->cts_entsize)
  236. {
  237. case sizeof (Elf64_Sym):
  238. {
  239. const Elf64_Sym *symp64 = (Elf64_Sym *) (uintptr_t) symp;
  240. ctf_elf64_to_link_sym (fp, &sym, symp64, i);
  241. }
  242. break;
  243. case sizeof (Elf32_Sym):
  244. {
  245. const Elf32_Sym *symp32 = (Elf32_Sym *) (uintptr_t) symp;
  246. ctf_elf32_to_link_sym (fp, &sym, symp32, i);
  247. }
  248. break;
  249. default:
  250. return ECTF_SYMTAB;
  251. }
  252. /* This call may be led astray if our idea of the symtab's endianness is
  253. wrong, but when this is fixed by a call to ctf_symsect_endianness,
  254. init_symtab will be called again with the right endianness in
  255. force. */
  256. if (ctf_symtab_skippable (&sym))
  257. {
  258. *xp = -1u;
  259. continue;
  260. }
  261. switch (sym.st_type)
  262. {
  263. case STT_OBJECT:
  264. if (fp->ctf_objtidx_names || objtoff >= hp->cth_funcoff)
  265. {
  266. *xp = -1u;
  267. break;
  268. }
  269. *xp = objtoff;
  270. objtoff += sizeof (uint32_t);
  271. break;
  272. case STT_FUNC:
  273. if (fp->ctf_funcidx_names || funcoff >= hp->cth_objtidxoff
  274. || skip_func_info)
  275. {
  276. *xp = -1u;
  277. break;
  278. }
  279. *xp = funcoff;
  280. funcoff += sizeof (uint32_t);
  281. break;
  282. default:
  283. *xp = -1u;
  284. break;
  285. }
  286. }
  287. ctf_dprintf ("loaded %lu symtab entries\n", fp->ctf_nsyms);
  288. return 0;
  289. }
  290. /* Reset the CTF base pointer and derive the buf pointer from it, initializing
  291. everything in the ctf_dict that depends on the base or buf pointers.
  292. The original gap between the buf and base pointers, if any -- the original,
  293. unconverted CTF header -- is kept, but its contents are not specified and are
  294. never used. */
  295. static void
  296. ctf_set_base (ctf_dict_t *fp, const ctf_header_t *hp, unsigned char *base)
  297. {
  298. fp->ctf_buf = base + (fp->ctf_buf - fp->ctf_base);
  299. fp->ctf_base = base;
  300. fp->ctf_vars = (ctf_varent_t *) ((const char *) fp->ctf_buf +
  301. hp->cth_varoff);
  302. fp->ctf_nvars = (hp->cth_typeoff - hp->cth_varoff) / sizeof (ctf_varent_t);
  303. fp->ctf_str[CTF_STRTAB_0].cts_strs = (const char *) fp->ctf_buf
  304. + hp->cth_stroff;
  305. fp->ctf_str[CTF_STRTAB_0].cts_len = hp->cth_strlen;
  306. /* If we have a parent dict name and label, store the relocated string
  307. pointers in the CTF dict for easy access later. */
  308. /* Note: before conversion, these will be set to values that will be
  309. immediately invalidated by the conversion process, but the conversion
  310. process will call ctf_set_base() again to fix things up. */
  311. if (hp->cth_parlabel != 0)
  312. fp->ctf_parlabel = ctf_strptr (fp, hp->cth_parlabel);
  313. if (hp->cth_parname != 0)
  314. fp->ctf_parname = ctf_strptr (fp, hp->cth_parname);
  315. if (hp->cth_cuname != 0)
  316. fp->ctf_cuname = ctf_strptr (fp, hp->cth_cuname);
  317. if (fp->ctf_cuname)
  318. ctf_dprintf ("ctf_set_base: CU name %s\n", fp->ctf_cuname);
  319. if (fp->ctf_parname)
  320. ctf_dprintf ("ctf_set_base: parent name %s (label %s)\n",
  321. fp->ctf_parname,
  322. fp->ctf_parlabel ? fp->ctf_parlabel : "<NULL>");
  323. }
  324. /* Set the version of the CTF file. */
  325. /* When this is reset, LCTF_* changes behaviour, but there is no guarantee that
  326. the variable data list associated with each type has been upgraded: the
  327. caller must ensure this has been done in advance. */
  328. static void
  329. ctf_set_version (ctf_dict_t *fp, ctf_header_t *cth, int ctf_version)
  330. {
  331. fp->ctf_version = ctf_version;
  332. cth->cth_version = ctf_version;
  333. fp->ctf_dictops = &ctf_dictops[ctf_version];
  334. }
  335. /* Upgrade the header to CTF_VERSION_3. The upgrade is done in-place. */
  336. static void
  337. upgrade_header (ctf_header_t *hp)
  338. {
  339. ctf_header_v2_t *oldhp = (ctf_header_v2_t *) hp;
  340. hp->cth_strlen = oldhp->cth_strlen;
  341. hp->cth_stroff = oldhp->cth_stroff;
  342. hp->cth_typeoff = oldhp->cth_typeoff;
  343. hp->cth_varoff = oldhp->cth_varoff;
  344. hp->cth_funcidxoff = hp->cth_varoff; /* No index sections. */
  345. hp->cth_objtidxoff = hp->cth_funcidxoff;
  346. hp->cth_funcoff = oldhp->cth_funcoff;
  347. hp->cth_objtoff = oldhp->cth_objtoff;
  348. hp->cth_lbloff = oldhp->cth_lbloff;
  349. hp->cth_cuname = 0; /* No CU name. */
  350. }
  351. /* Upgrade the type table to CTF_VERSION_3 (really CTF_VERSION_1_UPGRADED_3)
  352. from CTF_VERSION_1.
  353. The upgrade is not done in-place: the ctf_base is moved. ctf_strptr() must
  354. not be called before reallocation is complete.
  355. Sections not checked here due to nonexistence or nonpopulated state in older
  356. formats: objtidx, funcidx.
  357. Type kinds not checked here due to nonexistence in older formats:
  358. CTF_K_SLICE. */
  359. static int
  360. upgrade_types_v1 (ctf_dict_t *fp, ctf_header_t *cth)
  361. {
  362. const ctf_type_v1_t *tbuf;
  363. const ctf_type_v1_t *tend;
  364. unsigned char *ctf_base, *old_ctf_base = (unsigned char *) fp->ctf_dynbase;
  365. ctf_type_t *t2buf;
  366. ssize_t increase = 0, size, increment, v2increment, vbytes, v2bytes;
  367. const ctf_type_v1_t *tp;
  368. ctf_type_t *t2p;
  369. tbuf = (ctf_type_v1_t *) (fp->ctf_buf + cth->cth_typeoff);
  370. tend = (ctf_type_v1_t *) (fp->ctf_buf + cth->cth_stroff);
  371. /* Much like init_types(), this is a two-pass process.
  372. First, figure out the new type-section size needed. (It is possible,
  373. in theory, for it to be less than the old size, but this is very
  374. unlikely. It cannot be so small that cth_typeoff ends up of negative
  375. size. We validate this with an assertion below.)
  376. We must cater not only for changes in vlen and types sizes but also
  377. for changes in 'increment', which happen because v2 places some types
  378. into ctf_stype_t where v1 would be forced to use the larger non-stype. */
  379. for (tp = tbuf; tp < tend;
  380. tp = (ctf_type_v1_t *) ((uintptr_t) tp + increment + vbytes))
  381. {
  382. unsigned short kind = CTF_V1_INFO_KIND (tp->ctt_info);
  383. unsigned long vlen = CTF_V1_INFO_VLEN (tp->ctt_info);
  384. size = get_ctt_size_v1 (fp, (const ctf_type_t *) tp, NULL, &increment);
  385. vbytes = get_vbytes_v1 (fp, kind, size, vlen);
  386. get_ctt_size_v2_unconverted (fp, (const ctf_type_t *) tp, NULL,
  387. &v2increment);
  388. v2bytes = get_vbytes_v2 (fp, kind, size, vlen);
  389. if ((vbytes < 0) || (size < 0))
  390. return ECTF_CORRUPT;
  391. increase += v2increment - increment; /* May be negative. */
  392. increase += v2bytes - vbytes;
  393. }
  394. /* Allocate enough room for the new buffer, then copy everything but the type
  395. section into place, and reset the base accordingly. Leave the version
  396. number unchanged, so that LCTF_INFO_* still works on the
  397. as-yet-untranslated type info. */
  398. if ((ctf_base = malloc (fp->ctf_size + increase)) == NULL)
  399. return ECTF_ZALLOC;
  400. /* Start at ctf_buf, not ctf_base, to squeeze out the original header: we
  401. never use it and it is unconverted. */
  402. memcpy (ctf_base, fp->ctf_buf, cth->cth_typeoff);
  403. memcpy (ctf_base + cth->cth_stroff + increase,
  404. fp->ctf_buf + cth->cth_stroff, cth->cth_strlen);
  405. memset (ctf_base + cth->cth_typeoff, 0, cth->cth_stroff - cth->cth_typeoff
  406. + increase);
  407. cth->cth_stroff += increase;
  408. fp->ctf_size += increase;
  409. assert (cth->cth_stroff >= cth->cth_typeoff);
  410. fp->ctf_base = ctf_base;
  411. fp->ctf_buf = ctf_base;
  412. fp->ctf_dynbase = ctf_base;
  413. ctf_set_base (fp, cth, ctf_base);
  414. t2buf = (ctf_type_t *) (fp->ctf_buf + cth->cth_typeoff);
  415. /* Iterate through all the types again, upgrading them.
  416. Everything that hasn't changed can just be outright memcpy()ed.
  417. Things that have changed need field-by-field consideration. */
  418. for (tp = tbuf, t2p = t2buf; tp < tend;
  419. tp = (ctf_type_v1_t *) ((uintptr_t) tp + increment + vbytes),
  420. t2p = (ctf_type_t *) ((uintptr_t) t2p + v2increment + v2bytes))
  421. {
  422. unsigned short kind = CTF_V1_INFO_KIND (tp->ctt_info);
  423. int isroot = CTF_V1_INFO_ISROOT (tp->ctt_info);
  424. unsigned long vlen = CTF_V1_INFO_VLEN (tp->ctt_info);
  425. ssize_t v2size;
  426. void *vdata, *v2data;
  427. size = get_ctt_size_v1 (fp, (const ctf_type_t *) tp, NULL, &increment);
  428. vbytes = get_vbytes_v1 (fp, kind, size, vlen);
  429. t2p->ctt_name = tp->ctt_name;
  430. t2p->ctt_info = CTF_TYPE_INFO (kind, isroot, vlen);
  431. switch (kind)
  432. {
  433. case CTF_K_FUNCTION:
  434. case CTF_K_FORWARD:
  435. case CTF_K_TYPEDEF:
  436. case CTF_K_POINTER:
  437. case CTF_K_VOLATILE:
  438. case CTF_K_CONST:
  439. case CTF_K_RESTRICT:
  440. t2p->ctt_type = tp->ctt_type;
  441. break;
  442. case CTF_K_INTEGER:
  443. case CTF_K_FLOAT:
  444. case CTF_K_ARRAY:
  445. case CTF_K_STRUCT:
  446. case CTF_K_UNION:
  447. case CTF_K_ENUM:
  448. case CTF_K_UNKNOWN:
  449. if ((size_t) size <= CTF_MAX_SIZE)
  450. t2p->ctt_size = size;
  451. else
  452. {
  453. t2p->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
  454. t2p->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
  455. }
  456. break;
  457. }
  458. v2size = get_ctt_size_v2 (fp, t2p, NULL, &v2increment);
  459. v2bytes = get_vbytes_v2 (fp, kind, v2size, vlen);
  460. /* Catch out-of-sync get_ctt_size_*(). The count goes wrong if
  461. these are not identical (and having them different makes no
  462. sense semantically). */
  463. assert (size == v2size);
  464. /* Now the varlen info. */
  465. vdata = (void *) ((uintptr_t) tp + increment);
  466. v2data = (void *) ((uintptr_t) t2p + v2increment);
  467. switch (kind)
  468. {
  469. case CTF_K_ARRAY:
  470. {
  471. const ctf_array_v1_t *ap = (const ctf_array_v1_t *) vdata;
  472. ctf_array_t *a2p = (ctf_array_t *) v2data;
  473. a2p->cta_contents = ap->cta_contents;
  474. a2p->cta_index = ap->cta_index;
  475. a2p->cta_nelems = ap->cta_nelems;
  476. break;
  477. }
  478. case CTF_K_STRUCT:
  479. case CTF_K_UNION:
  480. {
  481. ctf_member_t tmp;
  482. const ctf_member_v1_t *m1 = (const ctf_member_v1_t *) vdata;
  483. const ctf_lmember_v1_t *lm1 = (const ctf_lmember_v1_t *) m1;
  484. ctf_member_t *m2 = (ctf_member_t *) v2data;
  485. ctf_lmember_t *lm2 = (ctf_lmember_t *) m2;
  486. unsigned long i;
  487. /* We walk all four pointers forward, but only reference the two
  488. that are valid for the given size, to avoid quadruplicating all
  489. the code. */
  490. for (i = vlen; i != 0; i--, m1++, lm1++, m2++, lm2++)
  491. {
  492. size_t offset;
  493. if (size < CTF_LSTRUCT_THRESH_V1)
  494. {
  495. offset = m1->ctm_offset;
  496. tmp.ctm_name = m1->ctm_name;
  497. tmp.ctm_type = m1->ctm_type;
  498. }
  499. else
  500. {
  501. offset = CTF_LMEM_OFFSET (lm1);
  502. tmp.ctm_name = lm1->ctlm_name;
  503. tmp.ctm_type = lm1->ctlm_type;
  504. }
  505. if (size < CTF_LSTRUCT_THRESH)
  506. {
  507. m2->ctm_name = tmp.ctm_name;
  508. m2->ctm_type = tmp.ctm_type;
  509. m2->ctm_offset = offset;
  510. }
  511. else
  512. {
  513. lm2->ctlm_name = tmp.ctm_name;
  514. lm2->ctlm_type = tmp.ctm_type;
  515. lm2->ctlm_offsethi = CTF_OFFSET_TO_LMEMHI (offset);
  516. lm2->ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO (offset);
  517. }
  518. }
  519. break;
  520. }
  521. case CTF_K_FUNCTION:
  522. {
  523. unsigned long i;
  524. unsigned short *a1 = (unsigned short *) vdata;
  525. uint32_t *a2 = (uint32_t *) v2data;
  526. for (i = vlen; i != 0; i--, a1++, a2++)
  527. *a2 = *a1;
  528. }
  529. /* FALLTHRU */
  530. default:
  531. /* Catch out-of-sync get_vbytes_*(). */
  532. assert (vbytes == v2bytes);
  533. memcpy (v2data, vdata, vbytes);
  534. }
  535. }
  536. /* Verify that the entire region was converted. If not, we are either
  537. converting too much, or too little (leading to a buffer overrun either here
  538. or at read time, in init_types().) */
  539. assert ((size_t) t2p - (size_t) fp->ctf_buf == cth->cth_stroff);
  540. ctf_set_version (fp, cth, CTF_VERSION_1_UPGRADED_3);
  541. free (old_ctf_base);
  542. return 0;
  543. }
  544. /* Upgrade from any earlier version. */
  545. static int
  546. upgrade_types (ctf_dict_t *fp, ctf_header_t *cth)
  547. {
  548. switch (cth->cth_version)
  549. {
  550. /* v1 requires a full pass and reformatting. */
  551. case CTF_VERSION_1:
  552. upgrade_types_v1 (fp, cth);
  553. /* FALLTHRU */
  554. /* Already-converted v1 is just like later versions except that its
  555. parent/child boundary is unchanged (and much lower). */
  556. case CTF_VERSION_1_UPGRADED_3:
  557. fp->ctf_parmax = CTF_MAX_PTYPE_V1;
  558. /* v2 is just the same as v3 except for new types and sections:
  559. no upgrading required. */
  560. case CTF_VERSION_2: ;
  561. /* FALLTHRU */
  562. }
  563. return 0;
  564. }
  565. /* Initialize the type ID translation table with the byte offset of each type,
  566. and initialize the hash tables of each named type. Upgrade the type table to
  567. the latest supported representation in the process, if needed, and if this
  568. recension of libctf supports upgrading. */
  569. static int
  570. init_types (ctf_dict_t *fp, ctf_header_t *cth)
  571. {
  572. const ctf_type_t *tbuf;
  573. const ctf_type_t *tend;
  574. unsigned long pop[CTF_K_MAX + 1] = { 0 };
  575. const ctf_type_t *tp;
  576. uint32_t id;
  577. uint32_t *xp;
  578. /* We determine whether the dict is a child or a parent based on the value of
  579. cth_parname. */
  580. int child = cth->cth_parname != 0;
  581. int nlstructs = 0, nlunions = 0;
  582. int err;
  583. assert (!(fp->ctf_flags & LCTF_RDWR));
  584. if (_libctf_unlikely_ (fp->ctf_version == CTF_VERSION_1))
  585. {
  586. int err;
  587. if ((err = upgrade_types (fp, cth)) != 0)
  588. return err; /* Upgrade failed. */
  589. }
  590. tbuf = (ctf_type_t *) (fp->ctf_buf + cth->cth_typeoff);
  591. tend = (ctf_type_t *) (fp->ctf_buf + cth->cth_stroff);
  592. /* We make two passes through the entire type section. In this first
  593. pass, we count the number of each type and the total number of types. */
  594. for (tp = tbuf; tp < tend; fp->ctf_typemax++)
  595. {
  596. unsigned short kind = LCTF_INFO_KIND (fp, tp->ctt_info);
  597. unsigned long vlen = LCTF_INFO_VLEN (fp, tp->ctt_info);
  598. ssize_t size, increment, vbytes;
  599. (void) ctf_get_ctt_size (fp, tp, &size, &increment);
  600. vbytes = LCTF_VBYTES (fp, kind, size, vlen);
  601. if (vbytes < 0)
  602. return ECTF_CORRUPT;
  603. /* For forward declarations, ctt_type is the CTF_K_* kind for the tag,
  604. so bump that population count too. */
  605. if (kind == CTF_K_FORWARD)
  606. pop[tp->ctt_type]++;
  607. tp = (ctf_type_t *) ((uintptr_t) tp + increment + vbytes);
  608. pop[kind]++;
  609. }
  610. if (child)
  611. {
  612. ctf_dprintf ("CTF dict %p is a child\n", (void *) fp);
  613. fp->ctf_flags |= LCTF_CHILD;
  614. }
  615. else
  616. ctf_dprintf ("CTF dict %p is a parent\n", (void *) fp);
  617. /* Now that we've counted up the number of each type, we can allocate
  618. the hash tables, type translation table, and pointer table. */
  619. if ((fp->ctf_structs.ctn_readonly
  620. = ctf_hash_create (pop[CTF_K_STRUCT], ctf_hash_string,
  621. ctf_hash_eq_string)) == NULL)
  622. return ENOMEM;
  623. if ((fp->ctf_unions.ctn_readonly
  624. = ctf_hash_create (pop[CTF_K_UNION], ctf_hash_string,
  625. ctf_hash_eq_string)) == NULL)
  626. return ENOMEM;
  627. if ((fp->ctf_enums.ctn_readonly
  628. = ctf_hash_create (pop[CTF_K_ENUM], ctf_hash_string,
  629. ctf_hash_eq_string)) == NULL)
  630. return ENOMEM;
  631. if ((fp->ctf_names.ctn_readonly
  632. = ctf_hash_create (pop[CTF_K_UNKNOWN] +
  633. pop[CTF_K_INTEGER] +
  634. pop[CTF_K_FLOAT] +
  635. pop[CTF_K_FUNCTION] +
  636. pop[CTF_K_TYPEDEF] +
  637. pop[CTF_K_POINTER] +
  638. pop[CTF_K_VOLATILE] +
  639. pop[CTF_K_CONST] +
  640. pop[CTF_K_RESTRICT],
  641. ctf_hash_string,
  642. ctf_hash_eq_string)) == NULL)
  643. return ENOMEM;
  644. fp->ctf_txlate = malloc (sizeof (uint32_t) * (fp->ctf_typemax + 1));
  645. fp->ctf_ptrtab_len = fp->ctf_typemax + 1;
  646. fp->ctf_ptrtab = malloc (sizeof (uint32_t) * fp->ctf_ptrtab_len);
  647. if (fp->ctf_txlate == NULL || fp->ctf_ptrtab == NULL)
  648. return ENOMEM; /* Memory allocation failed. */
  649. xp = fp->ctf_txlate;
  650. *xp++ = 0; /* Type id 0 is used as a sentinel value. */
  651. memset (fp->ctf_txlate, 0, sizeof (uint32_t) * (fp->ctf_typemax + 1));
  652. memset (fp->ctf_ptrtab, 0, sizeof (uint32_t) * (fp->ctf_typemax + 1));
  653. /* In the second pass through the types, we fill in each entry of the
  654. type and pointer tables and add names to the appropriate hashes. */
  655. for (id = 1, tp = tbuf; tp < tend; xp++, id++)
  656. {
  657. unsigned short kind = LCTF_INFO_KIND (fp, tp->ctt_info);
  658. unsigned short isroot = LCTF_INFO_ISROOT (fp, tp->ctt_info);
  659. unsigned long vlen = LCTF_INFO_VLEN (fp, tp->ctt_info);
  660. ssize_t size, increment, vbytes;
  661. const char *name;
  662. (void) ctf_get_ctt_size (fp, tp, &size, &increment);
  663. name = ctf_strptr (fp, tp->ctt_name);
  664. /* Cannot fail: shielded by call in loop above. */
  665. vbytes = LCTF_VBYTES (fp, kind, size, vlen);
  666. switch (kind)
  667. {
  668. case CTF_K_UNKNOWN:
  669. case CTF_K_INTEGER:
  670. case CTF_K_FLOAT:
  671. /* Names are reused by bit-fields, which are differentiated by their
  672. encodings, and so typically we'd record only the first instance of
  673. a given intrinsic. However, we replace an existing type with a
  674. root-visible version so that we can be sure to find it when
  675. checking for conflicting definitions in ctf_add_type(). */
  676. if (((ctf_hash_lookup_type (fp->ctf_names.ctn_readonly,
  677. fp, name)) == 0)
  678. || isroot)
  679. {
  680. err = ctf_hash_define_type (fp->ctf_names.ctn_readonly, fp,
  681. LCTF_INDEX_TO_TYPE (fp, id, child),
  682. tp->ctt_name);
  683. if (err != 0)
  684. return err;
  685. }
  686. break;
  687. /* These kinds have no name, so do not need interning into any
  688. hashtables. */
  689. case CTF_K_ARRAY:
  690. case CTF_K_SLICE:
  691. break;
  692. case CTF_K_FUNCTION:
  693. if (!isroot)
  694. break;
  695. err = ctf_hash_insert_type (fp->ctf_names.ctn_readonly, fp,
  696. LCTF_INDEX_TO_TYPE (fp, id, child),
  697. tp->ctt_name);
  698. if (err != 0)
  699. return err;
  700. break;
  701. case CTF_K_STRUCT:
  702. if (size >= CTF_LSTRUCT_THRESH)
  703. nlstructs++;
  704. if (!isroot)
  705. break;
  706. err = ctf_hash_define_type (fp->ctf_structs.ctn_readonly, fp,
  707. LCTF_INDEX_TO_TYPE (fp, id, child),
  708. tp->ctt_name);
  709. if (err != 0)
  710. return err;
  711. break;
  712. case CTF_K_UNION:
  713. if (size >= CTF_LSTRUCT_THRESH)
  714. nlunions++;
  715. if (!isroot)
  716. break;
  717. err = ctf_hash_define_type (fp->ctf_unions.ctn_readonly, fp,
  718. LCTF_INDEX_TO_TYPE (fp, id, child),
  719. tp->ctt_name);
  720. if (err != 0)
  721. return err;
  722. break;
  723. case CTF_K_ENUM:
  724. if (!isroot)
  725. break;
  726. err = ctf_hash_define_type (fp->ctf_enums.ctn_readonly, fp,
  727. LCTF_INDEX_TO_TYPE (fp, id, child),
  728. tp->ctt_name);
  729. if (err != 0)
  730. return err;
  731. break;
  732. case CTF_K_TYPEDEF:
  733. if (!isroot)
  734. break;
  735. err = ctf_hash_insert_type (fp->ctf_names.ctn_readonly, fp,
  736. LCTF_INDEX_TO_TYPE (fp, id, child),
  737. tp->ctt_name);
  738. if (err != 0)
  739. return err;
  740. break;
  741. case CTF_K_FORWARD:
  742. {
  743. ctf_names_t *np = ctf_name_table (fp, tp->ctt_type);
  744. if (!isroot)
  745. break;
  746. /* Only insert forward tags into the given hash if the type or tag
  747. name is not already present. */
  748. if (ctf_hash_lookup_type (np->ctn_readonly, fp, name) == 0)
  749. {
  750. err = ctf_hash_insert_type (np->ctn_readonly, fp,
  751. LCTF_INDEX_TO_TYPE (fp, id, child),
  752. tp->ctt_name);
  753. if (err != 0)
  754. return err;
  755. }
  756. break;
  757. }
  758. case CTF_K_POINTER:
  759. /* If the type referenced by the pointer is in this CTF dict, then
  760. store the index of the pointer type in fp->ctf_ptrtab[ index of
  761. referenced type ]. */
  762. if (LCTF_TYPE_ISCHILD (fp, tp->ctt_type) == child
  763. && LCTF_TYPE_TO_INDEX (fp, tp->ctt_type) <= fp->ctf_typemax)
  764. fp->ctf_ptrtab[LCTF_TYPE_TO_INDEX (fp, tp->ctt_type)] = id;
  765. /*FALLTHRU*/
  766. case CTF_K_VOLATILE:
  767. case CTF_K_CONST:
  768. case CTF_K_RESTRICT:
  769. if (!isroot)
  770. break;
  771. err = ctf_hash_insert_type (fp->ctf_names.ctn_readonly, fp,
  772. LCTF_INDEX_TO_TYPE (fp, id, child),
  773. tp->ctt_name);
  774. if (err != 0)
  775. return err;
  776. break;
  777. default:
  778. ctf_err_warn (fp, 0, ECTF_CORRUPT,
  779. _("init_types(): unhandled CTF kind: %x"), kind);
  780. return ECTF_CORRUPT;
  781. }
  782. *xp = (uint32_t) ((uintptr_t) tp - (uintptr_t) fp->ctf_buf);
  783. tp = (ctf_type_t *) ((uintptr_t) tp + increment + vbytes);
  784. }
  785. ctf_dprintf ("%lu total types processed\n", fp->ctf_typemax);
  786. ctf_dprintf ("%u enum names hashed\n",
  787. ctf_hash_size (fp->ctf_enums.ctn_readonly));
  788. ctf_dprintf ("%u struct names hashed (%d long)\n",
  789. ctf_hash_size (fp->ctf_structs.ctn_readonly), nlstructs);
  790. ctf_dprintf ("%u union names hashed (%d long)\n",
  791. ctf_hash_size (fp->ctf_unions.ctn_readonly), nlunions);
  792. ctf_dprintf ("%u base type names hashed\n",
  793. ctf_hash_size (fp->ctf_names.ctn_readonly));
  794. return 0;
  795. }
  796. /* Endianness-flipping routines.
  797. We flip everything, mindlessly, even 1-byte entities, so that future
  798. expansions do not require changes to this code. */
  799. /* Flip the endianness of the CTF header. */
  800. void
  801. ctf_flip_header (ctf_header_t *cth)
  802. {
  803. swap_thing (cth->cth_preamble.ctp_magic);
  804. swap_thing (cth->cth_preamble.ctp_version);
  805. swap_thing (cth->cth_preamble.ctp_flags);
  806. swap_thing (cth->cth_parlabel);
  807. swap_thing (cth->cth_parname);
  808. swap_thing (cth->cth_cuname);
  809. swap_thing (cth->cth_objtoff);
  810. swap_thing (cth->cth_funcoff);
  811. swap_thing (cth->cth_objtidxoff);
  812. swap_thing (cth->cth_funcidxoff);
  813. swap_thing (cth->cth_varoff);
  814. swap_thing (cth->cth_typeoff);
  815. swap_thing (cth->cth_stroff);
  816. swap_thing (cth->cth_strlen);
  817. }
  818. /* Flip the endianness of the label section, an array of ctf_lblent_t. */
  819. static void
  820. flip_lbls (void *start, size_t len)
  821. {
  822. ctf_lblent_t *lbl = start;
  823. ssize_t i;
  824. for (i = len / sizeof (struct ctf_lblent); i > 0; lbl++, i--)
  825. {
  826. swap_thing (lbl->ctl_label);
  827. swap_thing (lbl->ctl_type);
  828. }
  829. }
  830. /* Flip the endianness of the data-object or function sections or their indexes,
  831. all arrays of uint32_t. */
  832. static void
  833. flip_objts (void *start, size_t len)
  834. {
  835. uint32_t *obj = start;
  836. ssize_t i;
  837. for (i = len / sizeof (uint32_t); i > 0; obj++, i--)
  838. swap_thing (*obj);
  839. }
  840. /* Flip the endianness of the variable section, an array of ctf_varent_t. */
  841. static void
  842. flip_vars (void *start, size_t len)
  843. {
  844. ctf_varent_t *var = start;
  845. ssize_t i;
  846. for (i = len / sizeof (struct ctf_varent); i > 0; var++, i--)
  847. {
  848. swap_thing (var->ctv_name);
  849. swap_thing (var->ctv_type);
  850. }
  851. }
  852. /* Flip the endianness of the type section, a tagged array of ctf_type or
  853. ctf_stype followed by variable data. */
  854. static int
  855. flip_types (ctf_dict_t *fp, void *start, size_t len, int to_foreign)
  856. {
  857. ctf_type_t *t = start;
  858. while ((uintptr_t) t < ((uintptr_t) start) + len)
  859. {
  860. uint32_t kind;
  861. size_t size;
  862. uint32_t vlen;
  863. size_t vbytes;
  864. if (to_foreign)
  865. {
  866. kind = CTF_V2_INFO_KIND (t->ctt_info);
  867. size = t->ctt_size;
  868. vlen = CTF_V2_INFO_VLEN (t->ctt_info);
  869. vbytes = get_vbytes_v2 (fp, kind, size, vlen);
  870. }
  871. swap_thing (t->ctt_name);
  872. swap_thing (t->ctt_info);
  873. swap_thing (t->ctt_size);
  874. if (!to_foreign)
  875. {
  876. kind = CTF_V2_INFO_KIND (t->ctt_info);
  877. size = t->ctt_size;
  878. vlen = CTF_V2_INFO_VLEN (t->ctt_info);
  879. vbytes = get_vbytes_v2 (fp, kind, size, vlen);
  880. }
  881. if (_libctf_unlikely_ (size == CTF_LSIZE_SENT))
  882. {
  883. if (to_foreign)
  884. size = CTF_TYPE_LSIZE (t);
  885. swap_thing (t->ctt_lsizehi);
  886. swap_thing (t->ctt_lsizelo);
  887. if (!to_foreign)
  888. size = CTF_TYPE_LSIZE (t);
  889. t = (ctf_type_t *) ((uintptr_t) t + sizeof (ctf_type_t));
  890. }
  891. else
  892. t = (ctf_type_t *) ((uintptr_t) t + sizeof (ctf_stype_t));
  893. switch (kind)
  894. {
  895. case CTF_K_FORWARD:
  896. case CTF_K_UNKNOWN:
  897. case CTF_K_POINTER:
  898. case CTF_K_TYPEDEF:
  899. case CTF_K_VOLATILE:
  900. case CTF_K_CONST:
  901. case CTF_K_RESTRICT:
  902. /* These types have no vlen data to swap. */
  903. assert (vbytes == 0);
  904. break;
  905. case CTF_K_INTEGER:
  906. case CTF_K_FLOAT:
  907. {
  908. /* These types have a single uint32_t. */
  909. uint32_t *item = (uint32_t *) t;
  910. swap_thing (*item);
  911. break;
  912. }
  913. case CTF_K_FUNCTION:
  914. {
  915. /* This type has a bunch of uint32_ts. */
  916. uint32_t *item = (uint32_t *) t;
  917. ssize_t i;
  918. for (i = vlen; i > 0; item++, i--)
  919. swap_thing (*item);
  920. break;
  921. }
  922. case CTF_K_ARRAY:
  923. {
  924. /* This has a single ctf_array_t. */
  925. ctf_array_t *a = (ctf_array_t *) t;
  926. assert (vbytes == sizeof (ctf_array_t));
  927. swap_thing (a->cta_contents);
  928. swap_thing (a->cta_index);
  929. swap_thing (a->cta_nelems);
  930. break;
  931. }
  932. case CTF_K_SLICE:
  933. {
  934. /* This has a single ctf_slice_t. */
  935. ctf_slice_t *s = (ctf_slice_t *) t;
  936. assert (vbytes == sizeof (ctf_slice_t));
  937. swap_thing (s->cts_type);
  938. swap_thing (s->cts_offset);
  939. swap_thing (s->cts_bits);
  940. break;
  941. }
  942. case CTF_K_STRUCT:
  943. case CTF_K_UNION:
  944. {
  945. /* This has an array of ctf_member or ctf_lmember, depending on
  946. size. We could consider it to be a simple array of uint32_t,
  947. but for safety's sake in case these structures ever acquire
  948. non-uint32_t members, do it member by member. */
  949. if (_libctf_unlikely_ (size >= CTF_LSTRUCT_THRESH))
  950. {
  951. ctf_lmember_t *lm = (ctf_lmember_t *) t;
  952. ssize_t i;
  953. for (i = vlen; i > 0; i--, lm++)
  954. {
  955. swap_thing (lm->ctlm_name);
  956. swap_thing (lm->ctlm_offsethi);
  957. swap_thing (lm->ctlm_type);
  958. swap_thing (lm->ctlm_offsetlo);
  959. }
  960. }
  961. else
  962. {
  963. ctf_member_t *m = (ctf_member_t *) t;
  964. ssize_t i;
  965. for (i = vlen; i > 0; i--, m++)
  966. {
  967. swap_thing (m->ctm_name);
  968. swap_thing (m->ctm_offset);
  969. swap_thing (m->ctm_type);
  970. }
  971. }
  972. break;
  973. }
  974. case CTF_K_ENUM:
  975. {
  976. /* This has an array of ctf_enum_t. */
  977. ctf_enum_t *item = (ctf_enum_t *) t;
  978. ssize_t i;
  979. for (i = vlen; i > 0; item++, i--)
  980. {
  981. swap_thing (item->cte_name);
  982. swap_thing (item->cte_value);
  983. }
  984. break;
  985. }
  986. default:
  987. ctf_err_warn (fp, 0, ECTF_CORRUPT,
  988. _("unhandled CTF kind in endianness conversion: %x"),
  989. kind);
  990. return ECTF_CORRUPT;
  991. }
  992. t = (ctf_type_t *) ((uintptr_t) t + vbytes);
  993. }
  994. return 0;
  995. }
  996. /* Flip the endianness of BUF, given the offsets in the (already endian-
  997. converted) CTH. If TO_FOREIGN is set, flip to foreign-endianness; if not,
  998. flip away.
  999. All of this stuff happens before the header is fully initialized, so the
  1000. LCTF_*() macros cannot be used yet. Since we do not try to endian-convert v1
  1001. data, this is no real loss. */
  1002. int
  1003. ctf_flip (ctf_dict_t *fp, ctf_header_t *cth, unsigned char *buf,
  1004. int to_foreign)
  1005. {
  1006. ctf_dprintf("flipping endianness\n");
  1007. flip_lbls (buf + cth->cth_lbloff, cth->cth_objtoff - cth->cth_lbloff);
  1008. flip_objts (buf + cth->cth_objtoff, cth->cth_funcoff - cth->cth_objtoff);
  1009. flip_objts (buf + cth->cth_funcoff, cth->cth_objtidxoff - cth->cth_funcoff);
  1010. flip_objts (buf + cth->cth_objtidxoff, cth->cth_funcidxoff - cth->cth_objtidxoff);
  1011. flip_objts (buf + cth->cth_funcidxoff, cth->cth_varoff - cth->cth_funcidxoff);
  1012. flip_vars (buf + cth->cth_varoff, cth->cth_typeoff - cth->cth_varoff);
  1013. return flip_types (fp, buf + cth->cth_typeoff,
  1014. cth->cth_stroff - cth->cth_typeoff, to_foreign);
  1015. }
  1016. /* Set up the ctl hashes in a ctf_dict_t. Called by both writable and
  1017. non-writable dictionary initialization. */
  1018. void ctf_set_ctl_hashes (ctf_dict_t *fp)
  1019. {
  1020. /* Initialize the ctf_lookup_by_name top-level dictionary. We keep an
  1021. array of type name prefixes and the corresponding ctf_hash to use. */
  1022. fp->ctf_lookups[0].ctl_prefix = "struct";
  1023. fp->ctf_lookups[0].ctl_len = strlen (fp->ctf_lookups[0].ctl_prefix);
  1024. fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
  1025. fp->ctf_lookups[1].ctl_prefix = "union";
  1026. fp->ctf_lookups[1].ctl_len = strlen (fp->ctf_lookups[1].ctl_prefix);
  1027. fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
  1028. fp->ctf_lookups[2].ctl_prefix = "enum";
  1029. fp->ctf_lookups[2].ctl_len = strlen (fp->ctf_lookups[2].ctl_prefix);
  1030. fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
  1031. fp->ctf_lookups[3].ctl_prefix = _CTF_NULLSTR;
  1032. fp->ctf_lookups[3].ctl_len = strlen (fp->ctf_lookups[3].ctl_prefix);
  1033. fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
  1034. fp->ctf_lookups[4].ctl_prefix = NULL;
  1035. fp->ctf_lookups[4].ctl_len = 0;
  1036. fp->ctf_lookups[4].ctl_hash = NULL;
  1037. }
  1038. /* Open a CTF file, mocking up a suitable ctf_sect. */
  1039. ctf_dict_t *ctf_simple_open (const char *ctfsect, size_t ctfsect_size,
  1040. const char *symsect, size_t symsect_size,
  1041. size_t symsect_entsize,
  1042. const char *strsect, size_t strsect_size,
  1043. int *errp)
  1044. {
  1045. return ctf_simple_open_internal (ctfsect, ctfsect_size, symsect, symsect_size,
  1046. symsect_entsize, strsect, strsect_size, NULL,
  1047. 0, errp);
  1048. }
  1049. /* Open a CTF file, mocking up a suitable ctf_sect and overriding the external
  1050. strtab with a synthetic one. */
  1051. ctf_dict_t *ctf_simple_open_internal (const char *ctfsect, size_t ctfsect_size,
  1052. const char *symsect, size_t symsect_size,
  1053. size_t symsect_entsize,
  1054. const char *strsect, size_t strsect_size,
  1055. ctf_dynhash_t *syn_strtab, int writable,
  1056. int *errp)
  1057. {
  1058. ctf_sect_t skeleton;
  1059. ctf_sect_t ctf_sect, sym_sect, str_sect;
  1060. ctf_sect_t *ctfsectp = NULL;
  1061. ctf_sect_t *symsectp = NULL;
  1062. ctf_sect_t *strsectp = NULL;
  1063. skeleton.cts_name = _CTF_SECTION;
  1064. skeleton.cts_entsize = 1;
  1065. if (ctfsect)
  1066. {
  1067. memcpy (&ctf_sect, &skeleton, sizeof (struct ctf_sect));
  1068. ctf_sect.cts_data = ctfsect;
  1069. ctf_sect.cts_size = ctfsect_size;
  1070. ctfsectp = &ctf_sect;
  1071. }
  1072. if (symsect)
  1073. {
  1074. memcpy (&sym_sect, &skeleton, sizeof (struct ctf_sect));
  1075. sym_sect.cts_data = symsect;
  1076. sym_sect.cts_size = symsect_size;
  1077. sym_sect.cts_entsize = symsect_entsize;
  1078. symsectp = &sym_sect;
  1079. }
  1080. if (strsect)
  1081. {
  1082. memcpy (&str_sect, &skeleton, sizeof (struct ctf_sect));
  1083. str_sect.cts_data = strsect;
  1084. str_sect.cts_size = strsect_size;
  1085. strsectp = &str_sect;
  1086. }
  1087. return ctf_bufopen_internal (ctfsectp, symsectp, strsectp, syn_strtab,
  1088. writable, errp);
  1089. }
  1090. /* Decode the specified CTF buffer and optional symbol table, and create a new
  1091. CTF dict representing the symbolic debugging information. This code can
  1092. be used directly by the debugger, or it can be used as the engine for
  1093. ctf_fdopen() or ctf_open(), below. */
  1094. ctf_dict_t *
  1095. ctf_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect,
  1096. const ctf_sect_t *strsect, int *errp)
  1097. {
  1098. return ctf_bufopen_internal (ctfsect, symsect, strsect, NULL, 0, errp);
  1099. }
  1100. /* Like ctf_bufopen, but overriding the external strtab with a synthetic one. */
  1101. ctf_dict_t *
  1102. ctf_bufopen_internal (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect,
  1103. const ctf_sect_t *strsect, ctf_dynhash_t *syn_strtab,
  1104. int writable, int *errp)
  1105. {
  1106. const ctf_preamble_t *pp;
  1107. size_t hdrsz = sizeof (ctf_header_t);
  1108. ctf_header_t *hp;
  1109. ctf_dict_t *fp;
  1110. int foreign_endian = 0;
  1111. int err;
  1112. libctf_init_debug();
  1113. if ((ctfsect == NULL) || ((symsect != NULL) &&
  1114. ((strsect == NULL) && syn_strtab == NULL)))
  1115. return (ctf_set_open_errno (errp, EINVAL));
  1116. if (symsect != NULL && symsect->cts_entsize != sizeof (Elf32_Sym) &&
  1117. symsect->cts_entsize != sizeof (Elf64_Sym))
  1118. return (ctf_set_open_errno (errp, ECTF_SYMTAB));
  1119. if (symsect != NULL && symsect->cts_data == NULL)
  1120. return (ctf_set_open_errno (errp, ECTF_SYMBAD));
  1121. if (strsect != NULL && strsect->cts_data == NULL)
  1122. return (ctf_set_open_errno (errp, ECTF_STRBAD));
  1123. if (ctfsect->cts_size < sizeof (ctf_preamble_t))
  1124. return (ctf_set_open_errno (errp, ECTF_NOCTFBUF));
  1125. pp = (const ctf_preamble_t *) ctfsect->cts_data;
  1126. ctf_dprintf ("ctf_bufopen: magic=0x%x version=%u\n",
  1127. pp->ctp_magic, pp->ctp_version);
  1128. /* Validate each part of the CTF header.
  1129. First, we validate the preamble (common to all versions). At that point,
  1130. we know the endianness and specific header version, and can validate the
  1131. version-specific parts including section offsets and alignments.
  1132. We specifically do not support foreign-endian old versions. */
  1133. if (_libctf_unlikely_ (pp->ctp_magic != CTF_MAGIC))
  1134. {
  1135. if (pp->ctp_magic == bswap_16 (CTF_MAGIC))
  1136. {
  1137. if (pp->ctp_version != CTF_VERSION_3)
  1138. return (ctf_set_open_errno (errp, ECTF_CTFVERS));
  1139. foreign_endian = 1;
  1140. }
  1141. else
  1142. return (ctf_set_open_errno (errp, ECTF_NOCTFBUF));
  1143. }
  1144. if (_libctf_unlikely_ ((pp->ctp_version < CTF_VERSION_1)
  1145. || (pp->ctp_version > CTF_VERSION_3)))
  1146. return (ctf_set_open_errno (errp, ECTF_CTFVERS));
  1147. if ((symsect != NULL) && (pp->ctp_version < CTF_VERSION_2))
  1148. {
  1149. /* The symtab can contain function entries which contain embedded ctf
  1150. info. We do not support dynamically upgrading such entries (none
  1151. should exist in any case, since dwarf2ctf does not create them). */
  1152. ctf_err_warn (NULL, 0, ECTF_NOTSUP, _("ctf_bufopen: CTF version %d "
  1153. "symsect not supported"),
  1154. pp->ctp_version);
  1155. return (ctf_set_open_errno (errp, ECTF_NOTSUP));
  1156. }
  1157. if (pp->ctp_version < CTF_VERSION_3)
  1158. hdrsz = sizeof (ctf_header_v2_t);
  1159. if (_libctf_unlikely_ (pp->ctp_flags > CTF_F_MAX))
  1160. {
  1161. ctf_err_warn (NULL, 0, ECTF_FLAGS, _("ctf_bufopen: invalid header "
  1162. "flags: %x"),
  1163. (unsigned int) pp->ctp_flags);
  1164. return (ctf_set_open_errno (errp, ECTF_FLAGS));
  1165. }
  1166. if (ctfsect->cts_size < hdrsz)
  1167. return (ctf_set_open_errno (errp, ECTF_NOCTFBUF));
  1168. if ((fp = malloc (sizeof (ctf_dict_t))) == NULL)
  1169. return (ctf_set_open_errno (errp, ENOMEM));
  1170. memset (fp, 0, sizeof (ctf_dict_t));
  1171. if (writable)
  1172. fp->ctf_flags |= LCTF_RDWR;
  1173. if ((fp->ctf_header = malloc (sizeof (struct ctf_header))) == NULL)
  1174. {
  1175. free (fp);
  1176. return (ctf_set_open_errno (errp, ENOMEM));
  1177. }
  1178. hp = fp->ctf_header;
  1179. memcpy (hp, ctfsect->cts_data, hdrsz);
  1180. if (pp->ctp_version < CTF_VERSION_3)
  1181. upgrade_header (hp);
  1182. if (foreign_endian)
  1183. ctf_flip_header (hp);
  1184. fp->ctf_openflags = hp->cth_flags;
  1185. fp->ctf_size = hp->cth_stroff + hp->cth_strlen;
  1186. ctf_dprintf ("ctf_bufopen: uncompressed size=%lu\n",
  1187. (unsigned long) fp->ctf_size);
  1188. if (hp->cth_lbloff > fp->ctf_size || hp->cth_objtoff > fp->ctf_size
  1189. || hp->cth_funcoff > fp->ctf_size || hp->cth_objtidxoff > fp->ctf_size
  1190. || hp->cth_funcidxoff > fp->ctf_size || hp->cth_typeoff > fp->ctf_size
  1191. || hp->cth_stroff > fp->ctf_size)
  1192. {
  1193. ctf_err_warn (NULL, 0, ECTF_CORRUPT, _("header offset exceeds CTF size"));
  1194. return (ctf_set_open_errno (errp, ECTF_CORRUPT));
  1195. }
  1196. if (hp->cth_lbloff > hp->cth_objtoff
  1197. || hp->cth_objtoff > hp->cth_funcoff
  1198. || hp->cth_funcoff > hp->cth_typeoff
  1199. || hp->cth_funcoff > hp->cth_objtidxoff
  1200. || hp->cth_objtidxoff > hp->cth_funcidxoff
  1201. || hp->cth_funcidxoff > hp->cth_varoff
  1202. || hp->cth_varoff > hp->cth_typeoff || hp->cth_typeoff > hp->cth_stroff)
  1203. {
  1204. ctf_err_warn (NULL, 0, ECTF_CORRUPT, _("overlapping CTF sections"));
  1205. return (ctf_set_open_errno (errp, ECTF_CORRUPT));
  1206. }
  1207. if ((hp->cth_lbloff & 3) || (hp->cth_objtoff & 2)
  1208. || (hp->cth_funcoff & 2) || (hp->cth_objtidxoff & 2)
  1209. || (hp->cth_funcidxoff & 2) || (hp->cth_varoff & 3)
  1210. || (hp->cth_typeoff & 3))
  1211. {
  1212. ctf_err_warn (NULL, 0, ECTF_CORRUPT,
  1213. _("CTF sections not properly aligned"));
  1214. return (ctf_set_open_errno (errp, ECTF_CORRUPT));
  1215. }
  1216. /* This invariant will be lifted in v4, but for now it is true. */
  1217. if ((hp->cth_funcidxoff - hp->cth_objtidxoff != 0) &&
  1218. (hp->cth_funcidxoff - hp->cth_objtidxoff
  1219. != hp->cth_funcoff - hp->cth_objtoff))
  1220. {
  1221. ctf_err_warn (NULL, 0, ECTF_CORRUPT,
  1222. _("Object index section is neither empty nor the "
  1223. "same length as the object section: %u versus %u "
  1224. "bytes"), hp->cth_funcoff - hp->cth_objtoff,
  1225. hp->cth_funcidxoff - hp->cth_objtidxoff);
  1226. return (ctf_set_open_errno (errp, ECTF_CORRUPT));
  1227. }
  1228. if ((hp->cth_varoff - hp->cth_funcidxoff != 0) &&
  1229. (hp->cth_varoff - hp->cth_funcidxoff
  1230. != hp->cth_objtidxoff - hp->cth_funcoff) &&
  1231. (hp->cth_flags & CTF_F_NEWFUNCINFO))
  1232. {
  1233. ctf_err_warn (NULL, 0, ECTF_CORRUPT,
  1234. _("Function index section is neither empty nor the "
  1235. "same length as the function section: %u versus %u "
  1236. "bytes"), hp->cth_objtidxoff - hp->cth_funcoff,
  1237. hp->cth_varoff - hp->cth_funcidxoff);
  1238. return (ctf_set_open_errno (errp, ECTF_CORRUPT));
  1239. }
  1240. /* Once everything is determined to be valid, attempt to decompress the CTF
  1241. data buffer if it is compressed, or copy it into new storage if it is not
  1242. compressed but needs endian-flipping. Otherwise we just put the data
  1243. section's buffer pointer into ctf_buf, below. */
  1244. /* Note: if this is a v1 buffer, it will be reallocated and expanded by
  1245. init_types(). */
  1246. if (hp->cth_flags & CTF_F_COMPRESS)
  1247. {
  1248. size_t srclen;
  1249. uLongf dstlen;
  1250. const void *src;
  1251. int rc = Z_OK;
  1252. /* We are allocating this ourselves, so we can drop the ctf header
  1253. copy in favour of ctf->ctf_header. */
  1254. if ((fp->ctf_base = malloc (fp->ctf_size)) == NULL)
  1255. {
  1256. err = ECTF_ZALLOC;
  1257. goto bad;
  1258. }
  1259. fp->ctf_dynbase = fp->ctf_base;
  1260. hp->cth_flags &= ~CTF_F_COMPRESS;
  1261. src = (unsigned char *) ctfsect->cts_data + hdrsz;
  1262. srclen = ctfsect->cts_size - hdrsz;
  1263. dstlen = fp->ctf_size;
  1264. fp->ctf_buf = fp->ctf_base;
  1265. if ((rc = uncompress (fp->ctf_base, &dstlen, src, srclen)) != Z_OK)
  1266. {
  1267. ctf_err_warn (NULL, 0, ECTF_DECOMPRESS, _("zlib inflate err: %s"),
  1268. zError (rc));
  1269. err = ECTF_DECOMPRESS;
  1270. goto bad;
  1271. }
  1272. if ((size_t) dstlen != fp->ctf_size)
  1273. {
  1274. ctf_err_warn (NULL, 0, ECTF_CORRUPT,
  1275. _("zlib inflate short: got %lu of %lu bytes"),
  1276. (unsigned long) dstlen, (unsigned long) fp->ctf_size);
  1277. err = ECTF_CORRUPT;
  1278. goto bad;
  1279. }
  1280. }
  1281. else
  1282. {
  1283. if (_libctf_unlikely_ (ctfsect->cts_size < hdrsz + fp->ctf_size))
  1284. {
  1285. ctf_err_warn (NULL, 0, ECTF_CORRUPT,
  1286. _("%lu byte long CTF dictionary overruns %lu byte long CTF section"),
  1287. (unsigned long) ctfsect->cts_size,
  1288. (unsigned long) (hdrsz + fp->ctf_size));
  1289. err = ECTF_CORRUPT;
  1290. goto bad;
  1291. }
  1292. if (foreign_endian)
  1293. {
  1294. if ((fp->ctf_base = malloc (fp->ctf_size)) == NULL)
  1295. {
  1296. err = ECTF_ZALLOC;
  1297. goto bad;
  1298. }
  1299. fp->ctf_dynbase = fp->ctf_base;
  1300. memcpy (fp->ctf_base, ((unsigned char *) ctfsect->cts_data) + hdrsz,
  1301. fp->ctf_size);
  1302. fp->ctf_buf = fp->ctf_base;
  1303. }
  1304. else
  1305. {
  1306. /* We are just using the section passed in -- but its header may
  1307. be an old version. Point ctf_buf past the old header, and
  1308. never touch it again. */
  1309. fp->ctf_base = (unsigned char *) ctfsect->cts_data;
  1310. fp->ctf_dynbase = NULL;
  1311. fp->ctf_buf = fp->ctf_base + hdrsz;
  1312. }
  1313. }
  1314. /* Once we have uncompressed and validated the CTF data buffer, we can
  1315. proceed with initializing the ctf_dict_t we allocated above.
  1316. Nothing that depends on buf or base should be set directly in this function
  1317. before the init_types() call, because it may be reallocated during
  1318. transparent upgrade if this recension of libctf is so configured: see
  1319. ctf_set_base(). */
  1320. ctf_set_version (fp, hp, hp->cth_version);
  1321. if (ctf_str_create_atoms (fp) < 0)
  1322. {
  1323. err = ENOMEM;
  1324. goto bad;
  1325. }
  1326. fp->ctf_parmax = CTF_MAX_PTYPE;
  1327. memcpy (&fp->ctf_data, ctfsect, sizeof (ctf_sect_t));
  1328. if (symsect != NULL)
  1329. {
  1330. memcpy (&fp->ctf_symtab, symsect, sizeof (ctf_sect_t));
  1331. memcpy (&fp->ctf_strtab, strsect, sizeof (ctf_sect_t));
  1332. }
  1333. if (fp->ctf_data.cts_name != NULL)
  1334. if ((fp->ctf_data.cts_name = strdup (fp->ctf_data.cts_name)) == NULL)
  1335. {
  1336. err = ENOMEM;
  1337. goto bad;
  1338. }
  1339. if (fp->ctf_symtab.cts_name != NULL)
  1340. if ((fp->ctf_symtab.cts_name = strdup (fp->ctf_symtab.cts_name)) == NULL)
  1341. {
  1342. err = ENOMEM;
  1343. goto bad;
  1344. }
  1345. if (fp->ctf_strtab.cts_name != NULL)
  1346. if ((fp->ctf_strtab.cts_name = strdup (fp->ctf_strtab.cts_name)) == NULL)
  1347. {
  1348. err = ENOMEM;
  1349. goto bad;
  1350. }
  1351. if (fp->ctf_data.cts_name == NULL)
  1352. fp->ctf_data.cts_name = _CTF_NULLSTR;
  1353. if (fp->ctf_symtab.cts_name == NULL)
  1354. fp->ctf_symtab.cts_name = _CTF_NULLSTR;
  1355. if (fp->ctf_strtab.cts_name == NULL)
  1356. fp->ctf_strtab.cts_name = _CTF_NULLSTR;
  1357. if (strsect != NULL)
  1358. {
  1359. fp->ctf_str[CTF_STRTAB_1].cts_strs = strsect->cts_data;
  1360. fp->ctf_str[CTF_STRTAB_1].cts_len = strsect->cts_size;
  1361. }
  1362. fp->ctf_syn_ext_strtab = syn_strtab;
  1363. if (foreign_endian &&
  1364. (err = ctf_flip (fp, hp, fp->ctf_buf, 0)) != 0)
  1365. {
  1366. /* We can be certain that ctf_flip() will have endian-flipped everything
  1367. other than the types table when we return. In particular the header
  1368. is fine, so set it, to allow freeing to use the usual code path. */
  1369. ctf_set_base (fp, hp, fp->ctf_base);
  1370. goto bad;
  1371. }
  1372. ctf_set_base (fp, hp, fp->ctf_base);
  1373. /* No need to do anything else for dynamic dicts: they do not support symbol
  1374. lookups, and the type table is maintained in the dthashes. */
  1375. if (fp->ctf_flags & LCTF_RDWR)
  1376. {
  1377. fp->ctf_refcnt = 1;
  1378. return fp;
  1379. }
  1380. if ((err = init_types (fp, hp)) != 0)
  1381. goto bad;
  1382. /* Allocate and initialize the symtab translation table, pointed to by
  1383. ctf_sxlate, and the corresponding index sections. This table may be too
  1384. large for the actual size of the object and function info sections: if so,
  1385. ctf_nsyms will be adjusted and the excess will never be used. It's
  1386. possible to do indexed symbol lookups even without a symbol table, so check
  1387. even in that case. Initially, we assume the symtab is native-endian: if it
  1388. isn't, the caller will inform us later by calling ctf_symsect_endianness. */
  1389. #ifdef WORDS_BIGENDIAN
  1390. fp->ctf_symsect_little_endian = 0;
  1391. #else
  1392. fp->ctf_symsect_little_endian = 1;
  1393. #endif
  1394. if (symsect != NULL)
  1395. {
  1396. fp->ctf_nsyms = symsect->cts_size / symsect->cts_entsize;
  1397. fp->ctf_sxlate = malloc (fp->ctf_nsyms * sizeof (uint32_t));
  1398. if (fp->ctf_sxlate == NULL)
  1399. {
  1400. err = ENOMEM;
  1401. goto bad;
  1402. }
  1403. }
  1404. if ((err = init_symtab (fp, hp, symsect)) != 0)
  1405. goto bad;
  1406. ctf_set_ctl_hashes (fp);
  1407. if (symsect != NULL)
  1408. {
  1409. if (symsect->cts_entsize == sizeof (Elf64_Sym))
  1410. (void) ctf_setmodel (fp, CTF_MODEL_LP64);
  1411. else
  1412. (void) ctf_setmodel (fp, CTF_MODEL_ILP32);
  1413. }
  1414. else
  1415. (void) ctf_setmodel (fp, CTF_MODEL_NATIVE);
  1416. fp->ctf_refcnt = 1;
  1417. return fp;
  1418. bad:
  1419. ctf_set_open_errno (errp, err);
  1420. ctf_err_warn_to_open (fp);
  1421. ctf_dict_close (fp);
  1422. return NULL;
  1423. }
  1424. /* Bump the refcount on the specified CTF dict, to allow export of ctf_dict_t's
  1425. from iterators that open and close the ctf_dict_t around the loop. (This
  1426. does not extend their lifetime beyond that of the ctf_archive_t in which they
  1427. are contained.) */
  1428. void
  1429. ctf_ref (ctf_dict_t *fp)
  1430. {
  1431. fp->ctf_refcnt++;
  1432. }
  1433. /* Close the specified CTF dict and free associated data structures. Note that
  1434. ctf_dict_close() is a reference counted operation: if the specified file is
  1435. the parent of other active dict, its reference count will be greater than one
  1436. and it will be freed later when no active children exist. */
  1437. void
  1438. ctf_dict_close (ctf_dict_t *fp)
  1439. {
  1440. ctf_dtdef_t *dtd, *ntd;
  1441. ctf_dvdef_t *dvd, *nvd;
  1442. ctf_in_flight_dynsym_t *did, *nid;
  1443. ctf_err_warning_t *err, *nerr;
  1444. if (fp == NULL)
  1445. return; /* Allow ctf_dict_close(NULL) to simplify caller code. */
  1446. ctf_dprintf ("ctf_dict_close(%p) refcnt=%u\n", (void *) fp, fp->ctf_refcnt);
  1447. if (fp->ctf_refcnt > 1)
  1448. {
  1449. fp->ctf_refcnt--;
  1450. return;
  1451. }
  1452. /* It is possible to recurse back in here, notably if dicts in the
  1453. ctf_link_inputs or ctf_link_outputs cite this dict as a parent without
  1454. using ctf_import_unref. Do nothing in that case. */
  1455. if (fp->ctf_refcnt == 0)
  1456. return;
  1457. fp->ctf_refcnt--;
  1458. free (fp->ctf_dyncuname);
  1459. free (fp->ctf_dynparname);
  1460. if (fp->ctf_parent && !fp->ctf_parent_unreffed)
  1461. ctf_dict_close (fp->ctf_parent);
  1462. for (dtd = ctf_list_next (&fp->ctf_dtdefs); dtd != NULL; dtd = ntd)
  1463. {
  1464. ntd = ctf_list_next (dtd);
  1465. ctf_dtd_delete (fp, dtd);
  1466. }
  1467. ctf_dynhash_destroy (fp->ctf_dthash);
  1468. if (fp->ctf_flags & LCTF_RDWR)
  1469. {
  1470. ctf_dynhash_destroy (fp->ctf_structs.ctn_writable);
  1471. ctf_dynhash_destroy (fp->ctf_unions.ctn_writable);
  1472. ctf_dynhash_destroy (fp->ctf_enums.ctn_writable);
  1473. ctf_dynhash_destroy (fp->ctf_names.ctn_writable);
  1474. }
  1475. else
  1476. {
  1477. ctf_hash_destroy (fp->ctf_structs.ctn_readonly);
  1478. ctf_hash_destroy (fp->ctf_unions.ctn_readonly);
  1479. ctf_hash_destroy (fp->ctf_enums.ctn_readonly);
  1480. ctf_hash_destroy (fp->ctf_names.ctn_readonly);
  1481. }
  1482. for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd)
  1483. {
  1484. nvd = ctf_list_next (dvd);
  1485. ctf_dvd_delete (fp, dvd);
  1486. }
  1487. ctf_dynhash_destroy (fp->ctf_dvhash);
  1488. ctf_dynhash_destroy (fp->ctf_symhash);
  1489. free (fp->ctf_funcidx_sxlate);
  1490. free (fp->ctf_objtidx_sxlate);
  1491. ctf_dynhash_destroy (fp->ctf_objthash);
  1492. ctf_dynhash_destroy (fp->ctf_funchash);
  1493. free (fp->ctf_dynsymidx);
  1494. ctf_dynhash_destroy (fp->ctf_dynsyms);
  1495. for (did = ctf_list_next (&fp->ctf_in_flight_dynsyms); did != NULL; did = nid)
  1496. {
  1497. nid = ctf_list_next (did);
  1498. ctf_list_delete (&fp->ctf_in_flight_dynsyms, did);
  1499. free (did);
  1500. }
  1501. ctf_str_free_atoms (fp);
  1502. free (fp->ctf_tmp_typeslice);
  1503. if (fp->ctf_data.cts_name != _CTF_NULLSTR)
  1504. free ((char *) fp->ctf_data.cts_name);
  1505. if (fp->ctf_symtab.cts_name != _CTF_NULLSTR)
  1506. free ((char *) fp->ctf_symtab.cts_name);
  1507. if (fp->ctf_strtab.cts_name != _CTF_NULLSTR)
  1508. free ((char *) fp->ctf_strtab.cts_name);
  1509. else if (fp->ctf_data_mmapped)
  1510. ctf_munmap (fp->ctf_data_mmapped, fp->ctf_data_mmapped_len);
  1511. free (fp->ctf_dynbase);
  1512. ctf_dynhash_destroy (fp->ctf_syn_ext_strtab);
  1513. ctf_dynhash_destroy (fp->ctf_link_inputs);
  1514. ctf_dynhash_destroy (fp->ctf_link_outputs);
  1515. ctf_dynhash_destroy (fp->ctf_link_type_mapping);
  1516. ctf_dynhash_destroy (fp->ctf_link_in_cu_mapping);
  1517. ctf_dynhash_destroy (fp->ctf_link_out_cu_mapping);
  1518. ctf_dynhash_destroy (fp->ctf_add_processing);
  1519. ctf_dedup_fini (fp, NULL, 0);
  1520. ctf_dynset_destroy (fp->ctf_dedup_atoms_alloc);
  1521. for (err = ctf_list_next (&fp->ctf_errs_warnings); err != NULL; err = nerr)
  1522. {
  1523. nerr = ctf_list_next (err);
  1524. ctf_list_delete (&fp->ctf_errs_warnings, err);
  1525. free (err->cew_text);
  1526. free (err);
  1527. }
  1528. free (fp->ctf_sxlate);
  1529. free (fp->ctf_txlate);
  1530. free (fp->ctf_ptrtab);
  1531. free (fp->ctf_pptrtab);
  1532. free (fp->ctf_header);
  1533. free (fp);
  1534. }
  1535. /* Backward compatibility. */
  1536. void
  1537. ctf_file_close (ctf_file_t *fp)
  1538. {
  1539. ctf_dict_close (fp);
  1540. }
  1541. /* The converse of ctf_open(). ctf_open() disguises whatever it opens as an
  1542. archive, so closing one is just like closing an archive. */
  1543. void
  1544. ctf_close (ctf_archive_t *arc)
  1545. {
  1546. ctf_arc_close (arc);
  1547. }
  1548. /* Get the CTF archive from which this ctf_dict_t is derived. */
  1549. ctf_archive_t *
  1550. ctf_get_arc (const ctf_dict_t *fp)
  1551. {
  1552. return fp->ctf_archive;
  1553. }
  1554. /* Return the ctfsect out of the core ctf_impl. Useful for freeing the
  1555. ctfsect's data * after ctf_dict_close(), which is why we return the actual
  1556. structure, not a pointer to it, since that is likely to become a pointer to
  1557. freed data before the return value is used under the expected use case of
  1558. ctf_getsect()/ ctf_dict_close()/free(). */
  1559. ctf_sect_t
  1560. ctf_getdatasect (const ctf_dict_t *fp)
  1561. {
  1562. return fp->ctf_data;
  1563. }
  1564. ctf_sect_t
  1565. ctf_getsymsect (const ctf_dict_t *fp)
  1566. {
  1567. return fp->ctf_symtab;
  1568. }
  1569. ctf_sect_t
  1570. ctf_getstrsect (const ctf_dict_t *fp)
  1571. {
  1572. return fp->ctf_strtab;
  1573. }
  1574. /* Set the endianness of the symbol table attached to FP. */
  1575. void
  1576. ctf_symsect_endianness (ctf_dict_t *fp, int little_endian)
  1577. {
  1578. int old_endianness = fp->ctf_symsect_little_endian;
  1579. fp->ctf_symsect_little_endian = !!little_endian;
  1580. /* If we already have a symtab translation table, we need to repopulate it if
  1581. our idea of the endianness has changed. */
  1582. if (old_endianness != fp->ctf_symsect_little_endian
  1583. && fp->ctf_sxlate != NULL && fp->ctf_symtab.cts_data != NULL)
  1584. assert (init_symtab (fp, fp->ctf_header, &fp->ctf_symtab) == 0);
  1585. }
  1586. /* Return the CTF handle for the parent CTF dict, if one exists. Otherwise
  1587. return NULL to indicate this dict has no imported parent. */
  1588. ctf_dict_t *
  1589. ctf_parent_dict (ctf_dict_t *fp)
  1590. {
  1591. return fp->ctf_parent;
  1592. }
  1593. /* Backward compatibility. */
  1594. ctf_dict_t *
  1595. ctf_parent_file (ctf_dict_t *fp)
  1596. {
  1597. return ctf_parent_dict (fp);
  1598. }
  1599. /* Return the name of the parent CTF dict, if one exists, or NULL otherwise. */
  1600. const char *
  1601. ctf_parent_name (ctf_dict_t *fp)
  1602. {
  1603. return fp->ctf_parname;
  1604. }
  1605. /* Set the parent name. It is an error to call this routine without calling
  1606. ctf_import() at some point. */
  1607. int
  1608. ctf_parent_name_set (ctf_dict_t *fp, const char *name)
  1609. {
  1610. if (fp->ctf_dynparname != NULL)
  1611. free (fp->ctf_dynparname);
  1612. if ((fp->ctf_dynparname = strdup (name)) == NULL)
  1613. return (ctf_set_errno (fp, ENOMEM));
  1614. fp->ctf_parname = fp->ctf_dynparname;
  1615. return 0;
  1616. }
  1617. /* Return the name of the compilation unit this CTF file applies to. Usually
  1618. non-NULL only for non-parent dicts. */
  1619. const char *
  1620. ctf_cuname (ctf_dict_t *fp)
  1621. {
  1622. return fp->ctf_cuname;
  1623. }
  1624. /* Set the compilation unit name. */
  1625. int
  1626. ctf_cuname_set (ctf_dict_t *fp, const char *name)
  1627. {
  1628. if (fp->ctf_dyncuname != NULL)
  1629. free (fp->ctf_dyncuname);
  1630. if ((fp->ctf_dyncuname = strdup (name)) == NULL)
  1631. return (ctf_set_errno (fp, ENOMEM));
  1632. fp->ctf_cuname = fp->ctf_dyncuname;
  1633. return 0;
  1634. }
  1635. /* Import the types from the specified parent dict by storing a pointer to it in
  1636. ctf_parent and incrementing its reference count. Only one parent is allowed:
  1637. if a parent already exists, it is replaced by the new parent. The pptrtab
  1638. is wiped, and will be refreshed by the next ctf_lookup_by_name call. */
  1639. int
  1640. ctf_import (ctf_dict_t *fp, ctf_dict_t *pfp)
  1641. {
  1642. if (fp == NULL || fp == pfp || (pfp != NULL && pfp->ctf_refcnt == 0))
  1643. return (ctf_set_errno (fp, EINVAL));
  1644. if (pfp != NULL && pfp->ctf_dmodel != fp->ctf_dmodel)
  1645. return (ctf_set_errno (fp, ECTF_DMODEL));
  1646. if (fp->ctf_parent && !fp->ctf_parent_unreffed)
  1647. ctf_dict_close (fp->ctf_parent);
  1648. fp->ctf_parent = NULL;
  1649. free (fp->ctf_pptrtab);
  1650. fp->ctf_pptrtab = NULL;
  1651. fp->ctf_pptrtab_len = 0;
  1652. fp->ctf_pptrtab_typemax = 0;
  1653. if (pfp != NULL)
  1654. {
  1655. int err;
  1656. if (fp->ctf_parname == NULL)
  1657. if ((err = ctf_parent_name_set (fp, "PARENT")) < 0)
  1658. return err;
  1659. fp->ctf_flags |= LCTF_CHILD;
  1660. pfp->ctf_refcnt++;
  1661. fp->ctf_parent_unreffed = 0;
  1662. }
  1663. fp->ctf_parent = pfp;
  1664. return 0;
  1665. }
  1666. /* Like ctf_import, but does not increment the refcount on the imported parent
  1667. or close it at any point: as a result it can go away at any time and the
  1668. caller must do all freeing itself. Used internally to avoid refcount
  1669. loops. */
  1670. int
  1671. ctf_import_unref (ctf_dict_t *fp, ctf_dict_t *pfp)
  1672. {
  1673. if (fp == NULL || fp == pfp || (pfp != NULL && pfp->ctf_refcnt == 0))
  1674. return (ctf_set_errno (fp, EINVAL));
  1675. if (pfp != NULL && pfp->ctf_dmodel != fp->ctf_dmodel)
  1676. return (ctf_set_errno (fp, ECTF_DMODEL));
  1677. if (fp->ctf_parent && !fp->ctf_parent_unreffed)
  1678. ctf_dict_close (fp->ctf_parent);
  1679. fp->ctf_parent = NULL;
  1680. free (fp->ctf_pptrtab);
  1681. fp->ctf_pptrtab = NULL;
  1682. fp->ctf_pptrtab_len = 0;
  1683. fp->ctf_pptrtab_typemax = 0;
  1684. if (pfp != NULL)
  1685. {
  1686. int err;
  1687. if (fp->ctf_parname == NULL)
  1688. if ((err = ctf_parent_name_set (fp, "PARENT")) < 0)
  1689. return err;
  1690. fp->ctf_flags |= LCTF_CHILD;
  1691. fp->ctf_parent_unreffed = 1;
  1692. }
  1693. fp->ctf_parent = pfp;
  1694. return 0;
  1695. }
  1696. /* Set the data model constant for the CTF dict. */
  1697. int
  1698. ctf_setmodel (ctf_dict_t *fp, int model)
  1699. {
  1700. const ctf_dmodel_t *dp;
  1701. for (dp = _libctf_models; dp->ctd_name != NULL; dp++)
  1702. {
  1703. if (dp->ctd_code == model)
  1704. {
  1705. fp->ctf_dmodel = dp;
  1706. return 0;
  1707. }
  1708. }
  1709. return (ctf_set_errno (fp, EINVAL));
  1710. }
  1711. /* Return the data model constant for the CTF dict. */
  1712. int
  1713. ctf_getmodel (ctf_dict_t *fp)
  1714. {
  1715. return fp->ctf_dmodel->ctd_code;
  1716. }
  1717. /* The caller can hang an arbitrary pointer off each ctf_dict_t using this
  1718. function. */
  1719. void
  1720. ctf_setspecific (ctf_dict_t *fp, void *data)
  1721. {
  1722. fp->ctf_specific = data;
  1723. }
  1724. /* Retrieve the arbitrary pointer again. */
  1725. void *
  1726. ctf_getspecific (ctf_dict_t *fp)
  1727. {
  1728. return fp->ctf_specific;
  1729. }