1 1.2 mrg /* $NetBSD: bin_bfd.c,v 1.2 2017/07/25 06:43:03 mrg Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (c) 1996, 2002 Christopher G. Demetriou 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 3. The name of the author may not be used to endorse or promote products 16 1.1 christos * derived from this software without specific prior written permission. 17 1.1 christos * 18 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 1.1 christos * 29 1.1 christos * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>> 30 1.1 christos */ 31 1.1 christos 32 1.1 christos #if HAVE_NBTOOL_CONFIG_H 33 1.1 christos #include "nbtool_config.h" 34 1.1 christos #endif 35 1.1 christos 36 1.1 christos #include <sys/cdefs.h> 37 1.2 mrg __RCSID("$NetBSD: bin_bfd.c,v 1.2 2017/07/25 06:43:03 mrg Exp $"); 38 1.1 christos 39 1.1 christos #include <stdio.h> 40 1.1 christos #include <string.h> 41 1.1 christos #include <stdlib.h> 42 1.1 christos #include <err.h> 43 1.1 christos #include <bfd.h> 44 1.1 christos #include "bin.h" 45 1.1 christos 46 1.1 christos void * 47 1.1 christos bin_open(int kfd, const char *kfile, const char *bfdname) 48 1.1 christos { 49 1.1 christos bfd *abfd; 50 1.1 christos bfd_init(); 51 1.1 christos if ((abfd = bfd_fdopenr(kfile, bfdname, kfd)) == NULL) { 52 1.1 christos bfd_perror("open"); 53 1.1 christos exit(1); 54 1.1 christos } 55 1.1 christos if (!bfd_check_format(abfd, bfd_object)) { 56 1.1 christos bfd_perror("check format"); 57 1.1 christos exit(1); 58 1.1 christos } 59 1.1 christos return abfd; 60 1.1 christos } 61 1.1 christos 62 1.1 christos int 63 1.1 christos bin_find_md_root(void *bin, const char *mappedkfile, off_t size, 64 1.1 christos unsigned long text_start, 65 1.1 christos const char *root_name, const char *size_name, size_t *md_root_offset, 66 1.1 christos size_t *md_root_size_offset, uint32_t *md_root_size, int verbose) 67 1.1 christos { 68 1.1 christos bfd *abfd = bin; 69 1.1 christos long i; 70 1.1 christos long storage_needed; 71 1.1 christos long number_of_symbols; 72 1.1 christos asymbol **symbol_table = NULL; 73 1.1 christos struct symbols { 74 1.1 christos const char *name; 75 1.1 christos size_t offset; 76 1.1 christos } *s, symbols[3]; 77 1.1 christos 78 1.2 mrg symbols[0].offset = 0; 79 1.2 mrg symbols[1].offset = 0; 80 1.1 christos symbols[0].name = root_name; 81 1.1 christos symbols[1].name = size_name; 82 1.1 christos symbols[2].name = NULL; 83 1.1 christos 84 1.1 christos storage_needed = bfd_get_symtab_upper_bound(abfd); 85 1.1 christos if (storage_needed <= 0) { 86 1.1 christos warnx("bfd storage needed error"); 87 1.1 christos return 1; 88 1.1 christos } 89 1.1 christos 90 1.1 christos symbol_table = malloc(storage_needed); 91 1.1 christos if (symbol_table == NULL) { 92 1.1 christos warn("symbol table"); 93 1.1 christos return 1; 94 1.1 christos } 95 1.1 christos 96 1.1 christos number_of_symbols = bfd_canonicalize_symtab(abfd, symbol_table); 97 1.1 christos if (number_of_symbols <= 0) { 98 1.1 christos warnx("can't canonicalize symbol table"); 99 1.1 christos free(symbol_table); 100 1.1 christos return 1; 101 1.1 christos } 102 1.1 christos 103 1.1 christos for (i = 0; i < number_of_symbols; i++) { 104 1.1 christos for (s = symbols; s->name != NULL; s++) { 105 1.1 christos const char *sym = symbol_table[i]->name; 106 1.1 christos 107 1.1 christos /* 108 1.1 christos * match symbol prefix '_' or ''. 109 1.1 christos */ 110 1.1 christos if (!strcmp(s->name, sym) || 111 1.1 christos !strcmp(s->name + 1, sym)) { 112 1.1 christos s->offset = 113 1.1 christos (size_t)(symbol_table[i]->section->filepos 114 1.1 christos + symbol_table[i]->value); 115 1.1 christos } 116 1.1 christos } 117 1.1 christos } 118 1.1 christos 119 1.1 christos free(symbol_table); 120 1.1 christos 121 1.1 christos for (s = symbols; s->name != NULL; s++) { 122 1.1 christos if (s->offset == 0) { 123 1.1 christos warnx("missing offset for `%s'", s->name); 124 1.1 christos return 1; 125 1.1 christos } 126 1.1 christos } 127 1.1 christos 128 1.1 christos *md_root_offset = symbols[0].offset; 129 1.1 christos *md_root_size_offset = symbols[1].offset; 130 1.1 christos *md_root_size = bfd_get_32(abfd, &mappedkfile[*md_root_size_offset]); 131 1.1 christos 132 1.1 christos return 0; 133 1.1 christos } 134 1.1 christos 135 1.1 christos void 136 1.1 christos bin_put_32(void *bin, off_t size, char *buf) 137 1.1 christos { 138 1.1 christos bfd_put_32((struct bfd *)bin, size, buf); 139 1.1 christos } 140 1.1 christos 141 1.1 christos void 142 1.1 christos bin_close(void *bin) 143 1.1 christos { 144 1.1 christos bfd_close_all_done((struct bfd *)bin); 145 1.1 christos } 146 1.1 christos 147 1.1 christos const char ** 148 1.1 christos bin_supported_targets(void) 149 1.1 christos { 150 1.1 christos return bfd_target_list(); 151 1.1 christos } 152