xref: /dflybsd-src/contrib/binutils-2.34/libctf/ctf-open-bfd.c (revision b52ef7118d1621abed722c5bbbd542210290ecef)
1*fae548d3Szrj /* Opening CTF files with BFD.
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 <assert.h>
23*fae548d3Szrj #include <sys/types.h>
24*fae548d3Szrj #include <sys/stat.h>
25*fae548d3Szrj #include <errno.h>
26*fae548d3Szrj #include <string.h>
27*fae548d3Szrj #include <fcntl.h>
28*fae548d3Szrj #include <elf.h>
29*fae548d3Szrj #include <bfd.h>
30*fae548d3Szrj #include "swap.h"
31*fae548d3Szrj #include "ctf-endian.h"
32*fae548d3Szrj 
33*fae548d3Szrj #include "elf-bfd.h"
34*fae548d3Szrj 
35*fae548d3Szrj /* Make a new struct ctf_archive_internal wrapper for a ctf_archive or a
36*fae548d3Szrj    ctf_file.  Closes ARC and/or FP on error.  Arrange to free the SYMSECT or
37*fae548d3Szrj    STRSECT, as needed, on close (though the STRSECT interior is bound to the bfd
38*fae548d3Szrj    * and is not actually freed by this machinery).  */
39*fae548d3Szrj 
40*fae548d3Szrj static struct ctf_archive_internal *
ctf_new_archive_internal(int is_archive,struct ctf_archive * arc,ctf_file_t * fp,const ctf_sect_t * symsect,const ctf_sect_t * strsect,int * errp)41*fae548d3Szrj ctf_new_archive_internal (int is_archive, struct ctf_archive *arc,
42*fae548d3Szrj 			  ctf_file_t *fp, const ctf_sect_t *symsect,
43*fae548d3Szrj 			  const ctf_sect_t *strsect,
44*fae548d3Szrj 			  int *errp)
45*fae548d3Szrj {
46*fae548d3Szrj   struct ctf_archive_internal *arci;
47*fae548d3Szrj 
48*fae548d3Szrj   if ((arci = calloc (1, sizeof (struct ctf_archive_internal))) == NULL)
49*fae548d3Szrj     {
50*fae548d3Szrj       if (is_archive)
51*fae548d3Szrj 	ctf_arc_close_internal (arc);
52*fae548d3Szrj       else
53*fae548d3Szrj 	ctf_file_close (fp);
54*fae548d3Szrj       return (ctf_set_open_errno (errp, errno));
55*fae548d3Szrj     }
56*fae548d3Szrj   arci->ctfi_is_archive = is_archive;
57*fae548d3Szrj   if (is_archive)
58*fae548d3Szrj     arci->ctfi_archive = arc;
59*fae548d3Szrj   else
60*fae548d3Szrj     arci->ctfi_file = fp;
61*fae548d3Szrj   if (symsect)
62*fae548d3Szrj      memcpy (&arci->ctfi_symsect, symsect, sizeof (struct ctf_sect));
63*fae548d3Szrj   if (strsect)
64*fae548d3Szrj      memcpy (&arci->ctfi_strsect, strsect, sizeof (struct ctf_sect));
65*fae548d3Szrj 
66*fae548d3Szrj   return arci;
67*fae548d3Szrj }
68*fae548d3Szrj 
69*fae548d3Szrj /* Free the BFD bits of a CTF file on ctf_arc_close().  */
70*fae548d3Szrj 
71*fae548d3Szrj static void
ctf_bfdclose(struct ctf_archive_internal * arci)72*fae548d3Szrj ctf_bfdclose (struct ctf_archive_internal *arci)
73*fae548d3Szrj {
74*fae548d3Szrj   if (arci->ctfi_abfd != NULL)
75*fae548d3Szrj     if (!bfd_close_all_done (arci->ctfi_abfd))
76*fae548d3Szrj       ctf_dprintf ("Cannot close BFD: %s\n", bfd_errmsg (bfd_get_error()));
77*fae548d3Szrj }
78*fae548d3Szrj 
79*fae548d3Szrj /* Open a CTF file given the specified BFD.  */
80*fae548d3Szrj 
81*fae548d3Szrj ctf_archive_t *
ctf_bfdopen(struct bfd * abfd,int * errp)82*fae548d3Szrj ctf_bfdopen (struct bfd *abfd, int *errp)
83*fae548d3Szrj {
84*fae548d3Szrj   ctf_archive_t *arc;
85*fae548d3Szrj   asection *ctf_asect;
86*fae548d3Szrj   bfd_byte *contents;
87*fae548d3Szrj   ctf_sect_t ctfsect;
88*fae548d3Szrj 
89*fae548d3Szrj   libctf_init_debug();
90*fae548d3Szrj 
91*fae548d3Szrj   if ((ctf_asect = bfd_get_section_by_name (abfd, _CTF_SECTION)) == NULL)
92*fae548d3Szrj     {
93*fae548d3Szrj       return (ctf_set_open_errno (errp, ECTF_NOCTFDATA));
94*fae548d3Szrj     }
95*fae548d3Szrj 
96*fae548d3Szrj   if (!bfd_malloc_and_get_section (abfd, ctf_asect, &contents))
97*fae548d3Szrj     {
98*fae548d3Szrj       ctf_dprintf ("ctf_bfdopen(): cannot malloc CTF section: %s\n",
99*fae548d3Szrj 		   bfd_errmsg (bfd_get_error()));
100*fae548d3Szrj       return (ctf_set_open_errno (errp, ECTF_FMT));
101*fae548d3Szrj     }
102*fae548d3Szrj 
103*fae548d3Szrj   ctfsect.cts_name = _CTF_SECTION;
104*fae548d3Szrj   ctfsect.cts_entsize = 1;
105*fae548d3Szrj   ctfsect.cts_size = bfd_section_size (ctf_asect);
106*fae548d3Szrj   ctfsect.cts_data = contents;
107*fae548d3Szrj 
108*fae548d3Szrj   if ((arc = ctf_bfdopen_ctfsect (abfd, &ctfsect, errp)) != NULL)
109*fae548d3Szrj     {
110*fae548d3Szrj       arc->ctfi_data = (void *) ctfsect.cts_data;
111*fae548d3Szrj       return arc;
112*fae548d3Szrj     }
113*fae548d3Szrj 
114*fae548d3Szrj   free (contents);
115*fae548d3Szrj   return NULL;				/* errno is set for us.  */
116*fae548d3Szrj }
117*fae548d3Szrj 
118*fae548d3Szrj /* Open a CTF file given the specified BFD and CTF section (which may contain a
119*fae548d3Szrj    CTF archive or a file).  Takes ownership of the ctfsect, and frees it
120*fae548d3Szrj    later.  */
121*fae548d3Szrj 
122*fae548d3Szrj ctf_archive_t *
ctf_bfdopen_ctfsect(struct bfd * abfd _libctf_unused_,const ctf_sect_t * ctfsect,int * errp)123*fae548d3Szrj ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
124*fae548d3Szrj 		     const ctf_sect_t *ctfsect, int *errp)
125*fae548d3Szrj {
126*fae548d3Szrj   struct ctf_archive *arc = NULL;
127*fae548d3Szrj   ctf_archive_t *arci;
128*fae548d3Szrj   ctf_file_t *fp = NULL;
129*fae548d3Szrj   ctf_sect_t *symsectp = NULL;
130*fae548d3Szrj   ctf_sect_t *strsectp = NULL;
131*fae548d3Szrj   const char *bfderrstr = NULL;
132*fae548d3Szrj   int is_archive;
133*fae548d3Szrj 
134*fae548d3Szrj #ifdef HAVE_BFD_ELF
135*fae548d3Szrj   ctf_sect_t symsect, strsect;
136*fae548d3Szrj   Elf_Internal_Shdr *strhdr;
137*fae548d3Szrj   Elf_Internal_Shdr *symhdr = &elf_symtab_hdr (abfd);
138*fae548d3Szrj   size_t symcount = symhdr->sh_size / symhdr->sh_entsize;
139*fae548d3Szrj   Elf_Internal_Sym *isymbuf;
140*fae548d3Szrj   bfd_byte *symtab;
141*fae548d3Szrj   const char *strtab = NULL;
142*fae548d3Szrj   /* TODO: handle SYMTAB_SHNDX.  */
143*fae548d3Szrj 
144*fae548d3Szrj   if ((symtab = malloc (symhdr->sh_size)) == NULL)
145*fae548d3Szrj     {
146*fae548d3Szrj       bfderrstr = "Cannot malloc symbol table";
147*fae548d3Szrj       goto err;
148*fae548d3Szrj     }
149*fae548d3Szrj 
150*fae548d3Szrj   isymbuf = bfd_elf_get_elf_syms (abfd, symhdr, symcount, 0,
151*fae548d3Szrj 				  NULL, symtab, NULL);
152*fae548d3Szrj   free (isymbuf);
153*fae548d3Szrj   if (isymbuf == NULL)
154*fae548d3Szrj     {
155*fae548d3Szrj       bfderrstr = "Cannot read symbol table";
156*fae548d3Szrj       goto err_free_sym;
157*fae548d3Szrj     }
158*fae548d3Szrj 
159*fae548d3Szrj   if (elf_elfsections (abfd) != NULL
160*fae548d3Szrj       && symhdr->sh_link < elf_numsections (abfd))
161*fae548d3Szrj     {
162*fae548d3Szrj       strhdr = elf_elfsections (abfd)[symhdr->sh_link];
163*fae548d3Szrj       if (strhdr->contents == NULL)
164*fae548d3Szrj 	{
165*fae548d3Szrj 	  if ((strtab = bfd_elf_get_str_section (abfd, symhdr->sh_link)) == NULL)
166*fae548d3Szrj 	    {
167*fae548d3Szrj 	      bfderrstr = "Cannot read string table";
168*fae548d3Szrj 	      goto err_free_sym;
169*fae548d3Szrj 	    }
170*fae548d3Szrj 	}
171*fae548d3Szrj       else
172*fae548d3Szrj 	strtab = (const char *) strhdr->contents;
173*fae548d3Szrj     }
174*fae548d3Szrj 
175*fae548d3Szrj   if (strtab)
176*fae548d3Szrj     {
177*fae548d3Szrj       /* The names here are more or less arbitrary, but there is no point
178*fae548d3Szrj 	 thrashing around digging the name out of the shstrtab given that we don't
179*fae548d3Szrj 	 use it for anything but debugging.  */
180*fae548d3Szrj 
181*fae548d3Szrj       strsect.cts_data = strtab;
182*fae548d3Szrj       strsect.cts_name = ".strtab";
183*fae548d3Szrj       strsect.cts_size = strhdr->sh_size;
184*fae548d3Szrj       strsectp = &strsect;
185*fae548d3Szrj 
186*fae548d3Szrj       assert (symhdr->sh_entsize == get_elf_backend_data (abfd)->s->sizeof_sym);
187*fae548d3Szrj       symsect.cts_name = ".symtab";
188*fae548d3Szrj       symsect.cts_entsize = symhdr->sh_entsize;
189*fae548d3Szrj       symsect.cts_size = symhdr->sh_size;
190*fae548d3Szrj       symsect.cts_data = symtab;
191*fae548d3Szrj       symsectp = &symsect;
192*fae548d3Szrj     }
193*fae548d3Szrj #endif
194*fae548d3Szrj 
195*fae548d3Szrj   if (ctfsect->cts_size > sizeof (uint64_t) &&
196*fae548d3Szrj       ((*(uint64_t *) ctfsect->cts_data) == CTFA_MAGIC))
197*fae548d3Szrj     {
198*fae548d3Szrj       is_archive = 1;
199*fae548d3Szrj       if ((arc = ctf_arc_bufopen ((void *) ctfsect->cts_data,
200*fae548d3Szrj 				  ctfsect->cts_size, errp)) == NULL)
201*fae548d3Szrj 	goto err_free_str;
202*fae548d3Szrj     }
203*fae548d3Szrj   else
204*fae548d3Szrj     {
205*fae548d3Szrj       is_archive = 0;
206*fae548d3Szrj       if ((fp = ctf_bufopen (ctfsect, symsectp, strsectp, errp)) == NULL)
207*fae548d3Szrj 	{
208*fae548d3Szrj 	  ctf_dprintf ("ctf_internal_open(): cannot open CTF: %s\n",
209*fae548d3Szrj 		       ctf_errmsg (*errp));
210*fae548d3Szrj 	  goto err_free_str;
211*fae548d3Szrj 	}
212*fae548d3Szrj     }
213*fae548d3Szrj   arci = ctf_new_archive_internal (is_archive, arc, fp, symsectp, strsectp,
214*fae548d3Szrj 				   errp);
215*fae548d3Szrj 
216*fae548d3Szrj   if (arci)
217*fae548d3Szrj     return arci;
218*fae548d3Szrj  err_free_str: ;
219*fae548d3Szrj #ifdef HAVE_BFD_ELF
220*fae548d3Szrj  err_free_sym:
221*fae548d3Szrj   free (symtab);
222*fae548d3Szrj #endif
223*fae548d3Szrj err: _libctf_unused_;
224*fae548d3Szrj   if (bfderrstr)
225*fae548d3Szrj     {
226*fae548d3Szrj       ctf_dprintf ("ctf_bfdopen(): %s: %s\n", bfderrstr,
227*fae548d3Szrj 		   bfd_errmsg (bfd_get_error()));
228*fae548d3Szrj       ctf_set_open_errno (errp, ECTF_FMT);
229*fae548d3Szrj     }
230*fae548d3Szrj   return NULL;
231*fae548d3Szrj }
232*fae548d3Szrj 
233*fae548d3Szrj /* Open the specified file descriptor and return a pointer to a CTF archive that
234*fae548d3Szrj    contains one or more CTF containers.  The file can be an ELF file, a raw CTF
235*fae548d3Szrj    file, or a CTF archive.  The caller is responsible for closing the file
236*fae548d3Szrj    descriptor when it is no longer needed.  If this is an ELF file, TARGET, if
237*fae548d3Szrj    non-NULL, should be the name of a suitable BFD target.  */
238*fae548d3Szrj 
239*fae548d3Szrj ctf_archive_t *
ctf_fdopen(int fd,const char * filename,const char * target,int * errp)240*fae548d3Szrj ctf_fdopen (int fd, const char *filename, const char *target, int *errp)
241*fae548d3Szrj {
242*fae548d3Szrj   ctf_archive_t *arci;
243*fae548d3Szrj   bfd *abfd;
244*fae548d3Szrj   int nfd;
245*fae548d3Szrj 
246*fae548d3Szrj   struct stat st;
247*fae548d3Szrj   ssize_t nbytes;
248*fae548d3Szrj 
249*fae548d3Szrj   ctf_preamble_t ctfhdr;
250*fae548d3Szrj   uint64_t arc_magic;
251*fae548d3Szrj 
252*fae548d3Szrj   memset (&ctfhdr, 0, sizeof (ctfhdr));
253*fae548d3Szrj 
254*fae548d3Szrj   libctf_init_debug();
255*fae548d3Szrj 
256*fae548d3Szrj   if (fstat (fd, &st) == -1)
257*fae548d3Szrj     return (ctf_set_open_errno (errp, errno));
258*fae548d3Szrj 
259*fae548d3Szrj   if ((nbytes = ctf_pread (fd, &ctfhdr, sizeof (ctfhdr), 0)) <= 0)
260*fae548d3Szrj     return (ctf_set_open_errno (errp, nbytes < 0 ? errno : ECTF_FMT));
261*fae548d3Szrj 
262*fae548d3Szrj   /* If we have read enough bytes to form a CTF header and the magic string
263*fae548d3Szrj      matches, in either endianness, attempt to interpret the file as raw
264*fae548d3Szrj      CTF.  */
265*fae548d3Szrj 
266*fae548d3Szrj   if ((size_t) nbytes >= sizeof (ctf_preamble_t)
267*fae548d3Szrj       && (ctfhdr.ctp_magic == CTF_MAGIC
268*fae548d3Szrj 	  || ctfhdr.ctp_magic == bswap_16 (CTF_MAGIC)))
269*fae548d3Szrj     {
270*fae548d3Szrj       ctf_file_t *fp = NULL;
271*fae548d3Szrj       void *data;
272*fae548d3Szrj 
273*fae548d3Szrj       if ((data = ctf_mmap (st.st_size, 0, fd)) == NULL)
274*fae548d3Szrj 	return (ctf_set_open_errno (errp, errno));
275*fae548d3Szrj 
276*fae548d3Szrj       if ((fp = ctf_simple_open (data, (size_t) st.st_size, NULL, 0, 0,
277*fae548d3Szrj 				 NULL, 0, errp)) == NULL)
278*fae548d3Szrj 	{
279*fae548d3Szrj 	  ctf_munmap (data, (size_t) st.st_size);
280*fae548d3Szrj 	  return NULL;			/* errno is set for us.  */
281*fae548d3Szrj 	}
282*fae548d3Szrj 
283*fae548d3Szrj       fp->ctf_data_mmapped = data;
284*fae548d3Szrj       fp->ctf_data_mmapped_len = (size_t) st.st_size;
285*fae548d3Szrj 
286*fae548d3Szrj       return ctf_new_archive_internal (0, NULL, fp, NULL, NULL, errp);
287*fae548d3Szrj     }
288*fae548d3Szrj 
289*fae548d3Szrj   if ((nbytes = ctf_pread (fd, &arc_magic, sizeof (arc_magic), 0)) <= 0)
290*fae548d3Szrj     return (ctf_set_open_errno (errp, nbytes < 0 ? errno : ECTF_FMT));
291*fae548d3Szrj 
292*fae548d3Szrj   if ((size_t) nbytes >= sizeof (uint64_t) && le64toh (arc_magic) == CTFA_MAGIC)
293*fae548d3Szrj     {
294*fae548d3Szrj       struct ctf_archive *arc;
295*fae548d3Szrj 
296*fae548d3Szrj       if ((arc = ctf_arc_open_internal (filename, errp)) == NULL)
297*fae548d3Szrj 	return NULL;			/* errno is set for us.  */
298*fae548d3Szrj 
299*fae548d3Szrj       return ctf_new_archive_internal (1, arc, NULL, NULL, NULL, errp);
300*fae548d3Szrj     }
301*fae548d3Szrj 
302*fae548d3Szrj   /* Attempt to open the file with BFD.  We must dup the fd first, since bfd
303*fae548d3Szrj      takes ownership of the passed fd.  */
304*fae548d3Szrj 
305*fae548d3Szrj   if ((nfd = dup (fd)) < 0)
306*fae548d3Szrj       return (ctf_set_open_errno (errp, errno));
307*fae548d3Szrj 
308*fae548d3Szrj   if ((abfd = bfd_fdopenr (filename, target, nfd)) == NULL)
309*fae548d3Szrj     {
310*fae548d3Szrj       ctf_dprintf ("Cannot open BFD from %s: %s\n",
311*fae548d3Szrj 		   filename ? filename : "(unknown file)",
312*fae548d3Szrj 		   bfd_errmsg (bfd_get_error()));
313*fae548d3Szrj       return (ctf_set_open_errno (errp, ECTF_FMT));
314*fae548d3Szrj     }
315*fae548d3Szrj   bfd_set_cacheable (abfd, 1);
316*fae548d3Szrj 
317*fae548d3Szrj   if (!bfd_check_format (abfd, bfd_object))
318*fae548d3Szrj     {
319*fae548d3Szrj       ctf_dprintf ("BFD format problem in %s: %s\n",
320*fae548d3Szrj 		   filename ? filename : "(unknown file)",
321*fae548d3Szrj 		   bfd_errmsg (bfd_get_error()));
322*fae548d3Szrj       if (bfd_get_error() == bfd_error_file_ambiguously_recognized)
323*fae548d3Szrj 	return (ctf_set_open_errno (errp, ECTF_BFD_AMBIGUOUS));
324*fae548d3Szrj       else
325*fae548d3Szrj 	return (ctf_set_open_errno (errp, ECTF_FMT));
326*fae548d3Szrj     }
327*fae548d3Szrj 
328*fae548d3Szrj   if ((arci = ctf_bfdopen (abfd, errp)) == NULL)
329*fae548d3Szrj     {
330*fae548d3Szrj       if (!bfd_close_all_done (abfd))
331*fae548d3Szrj 	ctf_dprintf ("Cannot close BFD: %s\n", bfd_errmsg (bfd_get_error()));
332*fae548d3Szrj       return NULL;			/* errno is set for us.  */
333*fae548d3Szrj     }
334*fae548d3Szrj   arci->ctfi_bfd_close = ctf_bfdclose;
335*fae548d3Szrj   arci->ctfi_abfd = abfd;
336*fae548d3Szrj 
337*fae548d3Szrj   return arci;
338*fae548d3Szrj }
339*fae548d3Szrj 
340*fae548d3Szrj /* Open the specified file and return a pointer to a CTF container.  The file
341*fae548d3Szrj    can be either an ELF file or raw CTF file.  This is just a convenient
342*fae548d3Szrj    wrapper around ctf_fdopen() for callers.  */
343*fae548d3Szrj 
344*fae548d3Szrj ctf_archive_t *
ctf_open(const char * filename,const char * target,int * errp)345*fae548d3Szrj ctf_open (const char *filename, const char *target, int *errp)
346*fae548d3Szrj {
347*fae548d3Szrj   ctf_archive_t *arc;
348*fae548d3Szrj   int fd;
349*fae548d3Szrj 
350*fae548d3Szrj   if ((fd = open (filename, O_RDONLY)) == -1)
351*fae548d3Szrj     {
352*fae548d3Szrj       if (errp != NULL)
353*fae548d3Szrj 	*errp = errno;
354*fae548d3Szrj       return NULL;
355*fae548d3Szrj     }
356*fae548d3Szrj 
357*fae548d3Szrj   arc = ctf_fdopen (fd, filename, target, errp);
358*fae548d3Szrj   (void) close (fd);
359*fae548d3Szrj   return arc;
360*fae548d3Szrj }
361*fae548d3Szrj 
362*fae548d3Szrj /* Public entry point: open a CTF archive, or CTF file.  Returns the archive, or
363*fae548d3Szrj    NULL and an error in *err.  Despite the fact that this uses CTF archives, it
364*fae548d3Szrj    must be in this file to avoid dragging in BFD into non-BFD-using programs.  */
365*fae548d3Szrj ctf_archive_t *
ctf_arc_open(const char * filename,int * errp)366*fae548d3Szrj ctf_arc_open (const char *filename, int *errp)
367*fae548d3Szrj {
368*fae548d3Szrj   return ctf_open (filename, NULL, errp);
369*fae548d3Szrj }
370