1 /* $NetBSD: elf32.c,v 1.7 2008/04/28 20:24:16 martin Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1996 Christopher G. Demetriou
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed for the
47 * NetBSD Project. See http://www.NetBSD.org/ for
48 * information about NetBSD.
49 * 4. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
66 #include <sys/cdefs.h>
68 __RCSID("$NetBSD: elf32.c,v 1.7 2008/04/28 20:24:16 martin Exp $");
75 #include <sys/param.h>
76 #include <sys/exec_elf.h>
77 #include <sys/queue.h>
79 #include <dev/lockstat.h>
100 NAME(loadsym
)(int fd
)
102 Elf_Shdr symhdr
, strhdr
;
109 * Read the ELF header and make sure it's OK.
111 if (pread(fd
, &ehdr
, sizeof(ehdr
), 0) != sizeof(ehdr
))
114 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
115 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
)
118 switch (ehdr
.e_machine
) {
119 ELFDEFNNAME(MACHDEP_ID_CASES
)
125 * Find the symbol table header, and make sure the binary isn't
129 for (i
= 0; i
< ehdr
.e_shnum
; i
++, off
+= sizeof(symhdr
)) {
130 sz
= pread(fd
, &symhdr
, sizeof(symhdr
), off
);
131 if (sz
!= sizeof(symhdr
))
132 err(EXIT_FAILURE
, "pread (section headers)");
133 if (symhdr
.sh_type
== SHT_SYMTAB
)
136 if (i
== ehdr
.e_shnum
|| symhdr
.sh_offset
== 0)
137 err(EXIT_FAILURE
, "namelist is stripped");
140 * Pull in the string table header, and then read in both the symbol
141 * table and string table proper.
143 * XXX We can't use mmap(), as /dev/ksyms doesn't support mmap yet.
145 off
= ehdr
.e_shoff
+ symhdr
.sh_link
* sizeof(symhdr
);
146 if (pread(fd
, &strhdr
, sizeof(strhdr
), off
) != sizeof(strhdr
))
147 err(EXIT_FAILURE
, "pread");
149 if ((symp
= malloc(symhdr
.sh_size
)) == NULL
)
150 err(EXIT_FAILURE
, "malloc (symbol table)");
151 sz
= pread(fd
, symp
, symhdr
.sh_size
, symhdr
.sh_offset
);
152 if (sz
!= symhdr
.sh_size
)
153 err(EXIT_FAILURE
, "pread (symbol table)");
155 if ((strp
= malloc(strhdr
.sh_size
)) == NULL
)
156 err(EXIT_FAILURE
, "malloc (string table)");
157 sz
= pread(fd
, strp
, strhdr
.sh_size
, strhdr
.sh_offset
);
158 if (sz
!= strhdr
.sh_size
)
159 err(EXIT_FAILURE
, "pread (string table)");
161 nsyms
= (int)(symhdr
.sh_size
/ sizeof(Elf_Sym
));
167 NAME(findsym
)(findsym_t find
, char *name
, uintptr_t *start
, uintptr_t *end
)
169 static int lastptr
[FIND_MAX
];
190 for (i
= lastptr
[find
];;) {
192 for (i
= 0; i
< nsyms
; i
++) {
197 if (ELF_ST_TYPE(symp
[i
].st_info
) != st
)
199 if (strcmp(&strp
[symp
[i
].st_name
], name
) != 0)
201 *start
= (uintptr_t)symp
[i
].st_value
;
202 *end
= *start
+ (uintptr_t)symp
[i
].st_size
;
207 if (ELF_ST_TYPE(symp
[i
].st_info
) != st
)
209 sa
= (uintptr_t)symp
[i
].st_value
;
210 ea
= sa
+ (uintptr_t)symp
[i
].st_size
- 1;
211 if (*start
< sa
|| *start
> ea
)
213 off
= (int)(*start
- sa
);
220 strlcpy(name
, &strp
[symp
[i
].st_name
],
223 snprintf(name
, NAME_SIZE
, "%s+%x",
224 &strp
[symp
[i
].st_name
], off
);
234 if (i
== lastptr
[find
])