| 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 <elf.h> |
| 29 | #include <bfd.h> |
| 30 | #include "swap.h" |
| 31 | #include "ctf-endian.h" |
| 32 | |
| 33 | #include "elf-bfd.h" |
| 34 | |
| 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). */ |
| 39 | |
| 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, |
| 44 | int *errp) |
| 45 | { |
| 46 | struct ctf_archive_internal *arci; |
| 47 | |
| 48 | if ((arci = calloc (1, sizeof (struct ctf_archive_internal))) == NULL) |
| 49 | { |
| 50 | if (is_archive) |
| 51 | ctf_arc_close_internal (arc); |
| 52 | else |
| 53 | ctf_file_close (fp); |
| 54 | return (ctf_set_open_errno (errp, errno)); |
| 55 | } |
| 56 | arci->ctfi_is_archive = is_archive; |
| 57 | if (is_archive) |
| 58 | arci->ctfi_archive = arc; |
| 59 | else |
| 60 | arci->ctfi_file = fp; |
| 61 | if (symsect) |
| 62 | memcpy (&arci->ctfi_symsect, symsect, sizeof (struct ctf_sect)); |
| 63 | if (strsect) |
| 64 | memcpy (&arci->ctfi_strsect, strsect, sizeof (struct ctf_sect)); |
| 65 | |
| 66 | return arci; |
| 67 | } |
| 68 | |
| 69 | /* Free the BFD bits of a CTF file on ctf_arc_close(). */ |
| 70 | |
| 71 | static void |
| 72 | ctf_bfdclose (struct ctf_archive_internal *arci) |
| 73 | { |
| 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())); |
| 77 | } |
| 78 | |
| 79 | /* Open a CTF file given the specified BFD. */ |
| 80 | |
| 81 | ctf_archive_t * |
| 82 | ctf_bfdopen (struct bfd *abfd, int *errp) |
| 83 | { |
| 84 | ctf_archive_t *arc; |
| 85 | asection *ctf_asect; |
| 86 | bfd_byte *contents; |
| 87 | ctf_sect_t ctfsect; |
| 88 | |
| 89 | libctf_init_debug(); |
| 90 | |
| 91 | if ((ctf_asect = bfd_get_section_by_name (abfd, _CTF_SECTION)) == NULL) |
| 92 | { |
| 93 | return (ctf_set_open_errno (errp, ECTF_NOCTFDATA)); |
| 94 | } |
| 95 | |
| 96 | if (!bfd_malloc_and_get_section (abfd, ctf_asect, &contents)) |
| 97 | { |
| 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)); |
| 101 | } |
| 102 | |
| 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; |
| 107 | |
| 108 | if ((arc = ctf_bfdopen_ctfsect (abfd, &ctfsect, errp)) != NULL) |
| 109 | { |
| 110 | arc->ctfi_data = (void *) ctfsect.cts_data; |
| 111 | return arc; |
| 112 | } |
| 113 | |
| 114 | free (contents); |
| 115 | return NULL; /* errno is set for us. */ |
| 116 | } |
| 117 | |
| 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 |
| 120 | later. */ |
| 121 | |
| 122 | ctf_archive_t * |
| 123 | ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_, |
| 124 | const ctf_sect_t *ctfsect, int *errp) |
| 125 | { |
| 126 | struct ctf_archive *arc = NULL; |
| 127 | ctf_archive_t *arci; |
| 128 | ctf_file_t *fp = NULL; |
| 129 | ctf_sect_t *symsectp = NULL; |
| 130 | ctf_sect_t *strsectp = NULL; |
| 131 | const char *bfderrstr = NULL; |
| 132 | int is_archive; |
| 133 | |
| 134 | #ifdef HAVE_BFD_ELF |
| 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; |
| 140 | bfd_byte *symtab; |
| 141 | const char *strtab = NULL; |
| 142 | /* TODO: handle SYMTAB_SHNDX. */ |
| 143 | |
| 144 | if ((symtab = malloc (symhdr->sh_size)) == NULL) |
| 145 | { |
| 146 | bfderrstr = "Cannot malloc symbol table"; |
| 147 | goto err; |
| 148 | } |
| 149 | |
| 150 | isymbuf = bfd_elf_get_elf_syms (abfd, symhdr, symcount, 0, |
| 151 | NULL, symtab, NULL); |
| 152 | free (isymbuf); |
| 153 | if (isymbuf == NULL) |
| 154 | { |
| 155 | bfderrstr = "Cannot read symbol table"; |
| 156 | goto err_free_sym; |
| 157 | } |
| 158 | |
| 159 | if (elf_elfsections (abfd) != NULL |
| 160 | && symhdr->sh_link < elf_numsections (abfd)) |
| 161 | { |
| 162 | strhdr = elf_elfsections (abfd)[symhdr->sh_link]; |
| 163 | if (strhdr->contents == NULL) |
| 164 | { |
| 165 | if ((strtab = bfd_elf_get_str_section (abfd, symhdr->sh_link)) == NULL) |
| 166 | { |
| 167 | bfderrstr = "Cannot read string table"; |
| 168 | goto err_free_sym; |
| 169 | } |
| 170 | } |
| 171 | else |
| 172 | strtab = (const char *) strhdr->contents; |
| 173 | } |
| 174 | |
| 175 | if (strtab) |
| 176 | { |
| 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. */ |
| 180 | |
| 181 | strsect.cts_data = strtab; |
| 182 | strsect.cts_name = ".strtab"; |
| 183 | strsect.cts_size = strhdr->sh_size; |
| 184 | strsectp = &strsect; |
| 185 | |
| 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; |
| 191 | symsectp = &symsect; |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | if (ctfsect->cts_size > sizeof (uint64_t) && |
| 196 | ((*(uint64_t *) ctfsect->cts_data) == CTFA_MAGIC)) |
| 197 | { |
| 198 | is_archive = 1; |
| 199 | if ((arc = ctf_arc_bufopen ((void *) ctfsect->cts_data, |
| 200 | ctfsect->cts_size, errp)) == NULL) |
| 201 | goto err_free_str; |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | is_archive = 0; |
| 206 | if ((fp = ctf_bufopen (ctfsect, symsectp, strsectp, errp)) == NULL) |
| 207 | { |
| 208 | ctf_dprintf ("ctf_internal_open(): cannot open CTF: %s\n", |
| 209 | ctf_errmsg (*errp)); |
| 210 | goto err_free_str; |
| 211 | } |
| 212 | } |
| 213 | arci = ctf_new_archive_internal (is_archive, arc, fp, symsectp, strsectp, |
| 214 | errp); |
| 215 | |
| 216 | if (arci) |
| 217 | return arci; |
| 218 | err_free_str: ; |
| 219 | #ifdef HAVE_BFD_ELF |
| 220 | err_free_sym: |
| 221 | free (symtab); |
| 222 | #endif |
| 223 | err: _libctf_unused_; |
| 224 | if (bfderrstr) |
| 225 | { |
| 226 | ctf_dprintf ("ctf_bfdopen(): %s: %s\n", bfderrstr, |
| 227 | bfd_errmsg (bfd_get_error())); |
| 228 | ctf_set_open_errno (errp, ECTF_FMT); |
| 229 | } |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 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. */ |
| 238 | |
| 239 | ctf_archive_t * |
| 240 | ctf_fdopen (int fd, const char *filename, const char *target, int *errp) |
| 241 | { |
| 242 | ctf_archive_t *arci; |
| 243 | bfd *abfd; |
| 244 | int nfd; |
| 245 | |
| 246 | struct stat st; |
| 247 | ssize_t nbytes; |
| 248 | |
| 249 | ctf_preamble_t ctfhdr; |
| 250 | uint64_t arc_magic; |
| 251 | |
| 252 | memset (&ctfhdr, 0, sizeof (ctfhdr)); |
| 253 | |
| 254 | libctf_init_debug(); |
| 255 | |
| 256 | if (fstat (fd, &st) == -1) |
| 257 | return (ctf_set_open_errno (errp, errno)); |
| 258 | |
| 259 | if ((nbytes = ctf_pread (fd, &ctfhdr, sizeof (ctfhdr), 0)) <= 0) |
| 260 | return (ctf_set_open_errno (errp, nbytes < 0 ? errno : ECTF_FMT)); |
| 261 | |
| 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 |
| 264 | CTF. */ |
| 265 | |
| 266 | if ((size_t) nbytes >= sizeof (ctf_preamble_t) |
| 267 | && (ctfhdr.ctp_magic == CTF_MAGIC |
| 268 | || ctfhdr.ctp_magic == bswap_16 (CTF_MAGIC))) |
| 269 | { |
| 270 | ctf_file_t *fp = NULL; |
| 271 | void *data; |
| 272 | |
| 273 | if ((data = ctf_mmap (st.st_size, 0, fd)) == NULL) |
| 274 | return (ctf_set_open_errno (errp, errno)); |
| 275 | |
| 276 | if ((fp = ctf_simple_open (data, (size_t) st.st_size, NULL, 0, 0, |
| 277 | NULL, 0, errp)) == NULL) |
| 278 | { |
| 279 | ctf_munmap (data, (size_t) st.st_size); |
| 280 | return NULL; /* errno is set for us. */ |
| 281 | } |
| 282 | |
| 283 | fp->ctf_data_mmapped = data; |
| 284 | fp->ctf_data_mmapped_len = (size_t) st.st_size; |
| 285 | |
| 286 | return ctf_new_archive_internal (0, NULL, fp, NULL, NULL, errp); |
| 287 | } |
| 288 | |
| 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)); |
| 291 | |
| 292 | if ((size_t) nbytes >= sizeof (uint64_t) && le64toh (arc_magic) == CTFA_MAGIC) |
| 293 | { |
| 294 | struct ctf_archive *arc; |
| 295 | |
| 296 | if ((arc = ctf_arc_open_internal (filename, errp)) == NULL) |
| 297 | return NULL; /* errno is set for us. */ |
| 298 | |
| 299 | return ctf_new_archive_internal (1, arc, NULL, NULL, NULL, errp); |
| 300 | } |
| 301 | |
| 302 | /* Attempt to open the file with BFD. We must dup the fd first, since bfd |
| 303 | takes ownership of the passed fd. */ |
| 304 | |
| 305 | if ((nfd = dup (fd)) < 0) |
| 306 | return (ctf_set_open_errno (errp, errno)); |
| 307 | |
| 308 | if ((abfd = bfd_fdopenr (filename, target, nfd)) == NULL) |
| 309 | { |
| 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)); |
| 314 | } |
| 315 | bfd_set_cacheable (abfd, 1); |
| 316 | |
| 317 | if (!bfd_check_format (abfd, bfd_object)) |
| 318 | { |
| 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)); |
| 324 | else |
| 325 | return (ctf_set_open_errno (errp, ECTF_FMT)); |
| 326 | } |
| 327 | |
| 328 | if ((arci = ctf_bfdopen (abfd, errp)) == NULL) |
| 329 | { |
| 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. */ |
| 333 | } |
| 334 | arci->ctfi_bfd_close = ctf_bfdclose; |
| 335 | arci->ctfi_abfd = abfd; |
| 336 | |
| 337 | return arci; |
| 338 | } |
| 339 | |
| 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. */ |
| 343 | |
| 344 | ctf_archive_t * |
| 345 | ctf_open (const char *filename, const char *target, int *errp) |
| 346 | { |
| 347 | ctf_archive_t *arc; |
| 348 | int fd; |
| 349 | |
| 350 | if ((fd = open (filename, O_RDONLY)) == -1) |
| 351 | { |
| 352 | if (errp != NULL) |
| 353 | *errp = errno; |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | arc = ctf_fdopen (fd, filename, target, errp); |
| 358 | (void) close (fd); |
| 359 | return arc; |
| 360 | } |
| 361 | |
| 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. */ |
| 365 | ctf_archive_t * |
| 366 | ctf_arc_open (const char *filename, int *errp) |
| 367 | { |
| 368 | return ctf_open (filename, NULL, errp); |
| 369 | } |