]>
Commit | Line | Data |
---|---|---|
c4029823 | 1 | /* Copyright (C) 1991, 1996 Free Software Foundation, Inc. |
28f540f4 RM |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2, or (at your option) | |
7 | any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with the GNU C Library; see the file COPYING. If not, write to | |
16 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
17 | ||
28f540f4 RM |
18 | #include <errno.h> |
19 | #include <a.out.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | ||
24 | ||
25 | /* Search the executable FILE for symbols matching those in NL, | |
26 | which is terminated by an element with a NULL `n_un.n_name' member, | |
27 | and fill in the elements of NL. */ | |
28 | int | |
c4029823 UD |
29 | nlist (file, nl) |
30 | const char *file; | |
31 | struct nlist *nl; | |
28f540f4 RM |
32 | { |
33 | FILE *f; | |
34 | struct exec header; | |
35 | size_t nsymbols; | |
36 | struct nlist *symbols; | |
37 | unsigned long int string_table_size; | |
38 | char *string_table; | |
39 | register size_t i; | |
40 | ||
41 | if (nl == NULL) | |
42 | { | |
c4029823 | 43 | __set_errno (EINVAL); |
28f540f4 RM |
44 | return -1; |
45 | } | |
46 | ||
c4029823 | 47 | f = fopen (file, "r"); |
28f540f4 RM |
48 | if (f == NULL) |
49 | return -1; | |
50 | ||
c4029823 | 51 | if (fread ((void *) &header, sizeof (header), 1, f) != 1) |
28f540f4 RM |
52 | goto lose; |
53 | ||
c4029823 | 54 | if (fseek (f, N_SYMOFF (header), SEEK_SET) != 0) |
28f540f4 RM |
55 | goto lose; |
56 | ||
c4029823 UD |
57 | symbols = (struct nlist *) __alloca (header.a_syms); |
58 | nsymbols = header.a_syms / sizeof (symbols[0]); | |
28f540f4 | 59 | |
c4029823 | 60 | if (fread ((void *) symbols, sizeof (symbols[0]), nsymbols, f) != nsymbols) |
28f540f4 RM |
61 | goto lose; |
62 | ||
c4029823 UD |
63 | if (fread ((void *) &string_table_size, sizeof (string_table_size), 1, f) |
64 | != 1) | |
28f540f4 | 65 | goto lose; |
c4029823 | 66 | string_table_size -= sizeof (string_table_size); |
28f540f4 | 67 | |
c4029823 UD |
68 | string_table = (char *) __alloca (string_table_size); |
69 | if (fread ((void *) string_table, string_table_size, 1, f) != 1) | |
28f540f4 RM |
70 | goto lose; |
71 | ||
72 | for (i = 0; i < nsymbols; ++i) | |
73 | { | |
74 | register struct nlist *nlp; | |
75 | for (nlp = nl; nlp->n_un.n_name != NULL; ++nlp) | |
c4029823 UD |
76 | if (!strcmp (nlp->n_un.n_name, |
77 | &string_table[symbols[i].n_un.n_strx - | |
78 | sizeof (string_table_size)])) | |
28f540f4 | 79 | { |
c4029823 | 80 | char *const name = nlp->n_un.n_name; |
28f540f4 RM |
81 | *nlp = symbols[i]; |
82 | nlp->n_un.n_name = name; | |
83 | } | |
84 | } | |
85 | ||
c4029823 | 86 | (void) fclose (f); |
28f540f4 RM |
87 | return 0; |
88 | ||
89 | lose:; | |
c4029823 | 90 | (void) fclose (f); |
28f540f4 RM |
91 | return -1; |
92 | } |