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