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