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