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