1 /* Opening CTF files with BFD.
2 Copyright (C) 2019 Free Software Foundation, Inc.
4 This file is part of libctf.
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
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.
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/>. */
23 #include <sys/types.h>
31 #include "ctf-endian.h"
35 /* Make a new struct ctf_archive_internal wrapper for a ctf_archive or a
36 ctf_file. Closes ARC and/or FP on error. Arrange to free the SYMSECT or
37 STRSECT, as needed, on close (though the STRSECT interior is bound to the bfd
38 * and is not actually freed by this machinery). */
40 static struct ctf_archive_internal
*
41 ctf_new_archive_internal (int is_archive
, struct ctf_archive
*arc
,
42 ctf_file_t
*fp
, const ctf_sect_t
*symsect
,
43 const ctf_sect_t
*strsect
,
46 struct ctf_archive_internal
*arci
;
48 if ((arci
= calloc (1, sizeof (struct ctf_archive_internal
))) == NULL
)
51 ctf_arc_close_internal (arc
);
54 return (ctf_set_open_errno (errp
, errno
));
56 arci
->ctfi_is_archive
= is_archive
;
58 arci
->ctfi_archive
= arc
;
62 memcpy (&arci
->ctfi_symsect
, symsect
, sizeof (struct ctf_sect
));
64 memcpy (&arci
->ctfi_strsect
, strsect
, sizeof (struct ctf_sect
));
69 /* Free the BFD bits of a CTF file on ctf_arc_close(). */
72 ctf_bfdclose (struct ctf_archive_internal
*arci
)
74 if (arci
->ctfi_abfd
!= NULL
)
75 if (!bfd_close_all_done (arci
->ctfi_abfd
))
76 ctf_dprintf ("Cannot close BFD: %s\n", bfd_errmsg (bfd_get_error()));
79 /* Open a CTF file given the specified BFD. */
82 ctf_bfdopen (struct bfd
*abfd
, int *errp
)
91 if ((ctf_asect
= bfd_get_section_by_name (abfd
, _CTF_SECTION
)) == NULL
)
93 return (ctf_set_open_errno (errp
, ECTF_NOCTFDATA
));
96 if (!bfd_malloc_and_get_section (abfd
, ctf_asect
, &contents
))
98 ctf_dprintf ("ctf_bfdopen(): cannot malloc CTF section: %s\n",
99 bfd_errmsg (bfd_get_error()));
100 return (ctf_set_open_errno (errp
, ECTF_FMT
));
103 ctfsect
.cts_name
= _CTF_SECTION
;
104 ctfsect
.cts_entsize
= 1;
105 ctfsect
.cts_size
= bfd_section_size (ctf_asect
);
106 ctfsect
.cts_data
= contents
;
108 if ((arc
= ctf_bfdopen_ctfsect (abfd
, &ctfsect
, errp
)) != NULL
)
110 arc
->ctfi_data
= (void *) ctfsect
.cts_data
;
115 return NULL
; /* errno is set for us. */
118 /* Open a CTF file given the specified BFD and CTF section (which may contain a
119 CTF archive or a file). Takes ownership of the ctfsect, and frees it
123 ctf_bfdopen_ctfsect (struct bfd
*abfd _libctf_unused_
,
124 const ctf_sect_t
*ctfsect
, int *errp
)
126 struct ctf_archive
*arc
= NULL
;
128 ctf_file_t
*fp
= NULL
;
129 ctf_sect_t
*symsectp
= NULL
;
130 ctf_sect_t
*strsectp
= NULL
;
131 const char *bfderrstr
= NULL
;
135 ctf_sect_t symsect
, strsect
;
136 Elf_Internal_Shdr
*strhdr
;
137 Elf_Internal_Shdr
*symhdr
= &elf_symtab_hdr (abfd
);
138 size_t symcount
= symhdr
->sh_size
/ symhdr
->sh_entsize
;
139 Elf_Internal_Sym
*isymbuf
;
141 const char *strtab
= NULL
;
142 /* TODO: handle SYMTAB_SHNDX. */
144 if ((symtab
= malloc (symhdr
->sh_size
)) == NULL
)
146 bfderrstr
= "Cannot malloc symbol table";
150 isymbuf
= bfd_elf_get_elf_syms (abfd
, symhdr
, symcount
, 0,
155 bfderrstr
= "Cannot read symbol table";
159 if (elf_elfsections (abfd
) != NULL
160 && symhdr
->sh_link
< elf_numsections (abfd
))
162 strhdr
= elf_elfsections (abfd
)[symhdr
->sh_link
];
163 if (strhdr
->contents
== NULL
)
165 if ((strtab
= bfd_elf_get_str_section (abfd
, symhdr
->sh_link
)) == NULL
)
167 bfderrstr
= "Cannot read string table";
172 strtab
= (const char *) strhdr
->contents
;
177 /* The names here are more or less arbitrary, but there is no point
178 thrashing around digging the name out of the shstrtab given that we don't
179 use it for anything but debugging. */
181 strsect
.cts_data
= strtab
;
182 strsect
.cts_name
= ".strtab";
183 strsect
.cts_size
= strhdr
->sh_size
;
186 assert (symhdr
->sh_entsize
== get_elf_backend_data (abfd
)->s
->sizeof_sym
);
187 symsect
.cts_name
= ".symtab";
188 symsect
.cts_entsize
= symhdr
->sh_entsize
;
189 symsect
.cts_size
= symhdr
->sh_size
;
190 symsect
.cts_data
= symtab
;
195 if (ctfsect
->cts_size
> sizeof (uint64_t) &&
196 ((*(uint64_t *) ctfsect
->cts_data
) == CTFA_MAGIC
))
199 if ((arc
= ctf_arc_bufopen ((void *) ctfsect
->cts_data
,
200 ctfsect
->cts_size
, errp
)) == NULL
)
206 if ((fp
= ctf_bufopen (ctfsect
, symsectp
, strsectp
, errp
)) == NULL
)
208 ctf_dprintf ("ctf_internal_open(): cannot open CTF: %s\n",
213 arci
= ctf_new_archive_internal (is_archive
, arc
, fp
, symsectp
, strsectp
,
223 err
: _libctf_unused_
;
226 ctf_dprintf ("ctf_bfdopen(): %s: %s\n", bfderrstr
,
227 bfd_errmsg (bfd_get_error()));
228 ctf_set_open_errno (errp
, ECTF_FMT
);
233 /* Open the specified file descriptor and return a pointer to a CTF archive that
234 contains one or more CTF containers. The file can be an ELF file, a raw CTF
235 file, or a CTF archive. The caller is responsible for closing the file
236 descriptor when it is no longer needed. If this is an ELF file, TARGET, if
237 non-NULL, should be the name of a suitable BFD target. */
240 ctf_fdopen (int fd
, const char *filename
, const char *target
, int *errp
)
249 ctf_preamble_t ctfhdr
;
252 memset (&ctfhdr
, 0, sizeof (ctfhdr
));
256 if (fstat (fd
, &st
) == -1)
257 return (ctf_set_open_errno (errp
, errno
));
259 if ((nbytes
= ctf_pread (fd
, &ctfhdr
, sizeof (ctfhdr
), 0)) <= 0)
260 return (ctf_set_open_errno (errp
, nbytes
< 0 ? errno
: ECTF_FMT
));
262 /* If we have read enough bytes to form a CTF header and the magic string
263 matches, in either endianness, attempt to interpret the file as raw
266 if ((size_t) nbytes
>= sizeof (ctf_preamble_t
)
267 && (ctfhdr
.ctp_magic
== CTF_MAGIC
268 || ctfhdr
.ctp_magic
== bswap_16 (CTF_MAGIC
)))
270 ctf_file_t
*fp
= NULL
;
273 if ((data
= ctf_mmap (st
.st_size
, 0, fd
)) == NULL
)
274 return (ctf_set_open_errno (errp
, errno
));
276 if ((fp
= ctf_simple_open (data
, (size_t) st
.st_size
, NULL
, 0, 0,
277 NULL
, 0, errp
)) == NULL
)
279 ctf_munmap (data
, (size_t) st
.st_size
);
280 return NULL
; /* errno is set for us. */
283 fp
->ctf_data_mmapped
= data
;
284 fp
->ctf_data_mmapped_len
= (size_t) st
.st_size
;
286 return ctf_new_archive_internal (0, NULL
, fp
, NULL
, NULL
, errp
);
289 if ((nbytes
= ctf_pread (fd
, &arc_magic
, sizeof (arc_magic
), 0)) <= 0)
290 return (ctf_set_open_errno (errp
, nbytes
< 0 ? errno
: ECTF_FMT
));
292 if ((size_t) nbytes
>= sizeof (uint64_t) && le64toh (arc_magic
) == CTFA_MAGIC
)
294 struct ctf_archive
*arc
;
296 if ((arc
= ctf_arc_open_internal (filename
, errp
)) == NULL
)
297 return NULL
; /* errno is set for us. */
299 return ctf_new_archive_internal (1, arc
, NULL
, NULL
, NULL
, errp
);
302 /* Attempt to open the file with BFD. We must dup the fd first, since bfd
303 takes ownership of the passed fd. */
305 if ((nfd
= dup (fd
)) < 0)
306 return (ctf_set_open_errno (errp
, errno
));
308 if ((abfd
= bfd_fdopenr (filename
, target
, nfd
)) == NULL
)
310 ctf_dprintf ("Cannot open BFD from %s: %s\n",
311 filename
? filename
: "(unknown file)",
312 bfd_errmsg (bfd_get_error()));
313 return (ctf_set_open_errno (errp
, ECTF_FMT
));
315 bfd_set_cacheable (abfd
, 1);
317 if (!bfd_check_format (abfd
, bfd_object
))
319 ctf_dprintf ("BFD format problem in %s: %s\n",
320 filename
? filename
: "(unknown file)",
321 bfd_errmsg (bfd_get_error()));
322 if (bfd_get_error() == bfd_error_file_ambiguously_recognized
)
323 return (ctf_set_open_errno (errp
, ECTF_BFD_AMBIGUOUS
));
325 return (ctf_set_open_errno (errp
, ECTF_FMT
));
328 if ((arci
= ctf_bfdopen (abfd
, errp
)) == NULL
)
330 if (!bfd_close_all_done (abfd
))
331 ctf_dprintf ("Cannot close BFD: %s\n", bfd_errmsg (bfd_get_error()));
332 return NULL
; /* errno is set for us. */
334 arci
->ctfi_bfd_close
= ctf_bfdclose
;
335 arci
->ctfi_abfd
= abfd
;
340 /* Open the specified file and return a pointer to a CTF container. The file
341 can be either an ELF file or raw CTF file. This is just a convenient
342 wrapper around ctf_fdopen() for callers. */
345 ctf_open (const char *filename
, const char *target
, int *errp
)
350 if ((fd
= open (filename
, O_RDONLY
)) == -1)
357 arc
= ctf_fdopen (fd
, filename
, target
, errp
);
362 /* Public entry point: open a CTF archive, or CTF file. Returns the archive, or
363 NULL and an error in *err. Despite the fact that this uses CTF archives, it
364 must be in this file to avoid dragging in BFD into non-BFD-using programs. */
366 ctf_arc_open (const char *filename
, int *errp
)
368 return ctf_open (filename
, NULL
, errp
);