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