7b4ac7e1 |
1 | /* Program to stuff files into a specially prepared space in kdb. |
4187119d |
2 | Copyright (C) 1986, 1989 Free Software Foundation, Inc. |
3 | |
4 | This file is part of GDB. |
5 | |
6 | GDB is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
8 | the Free Software Foundation; either version 1, or (at your option) |
9 | any later version. |
10 | |
11 | GDB is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU General Public License |
17 | along with GDB; see the file COPYING. If not, write to |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
7b4ac7e1 |
19 | |
20 | /* Written 13-Mar-86 by David Bridgham. */ |
21 | |
22 | #include <stdio.h> |
23 | #include <a.out.h> |
24 | #include <sys/types.h> |
25 | #include <sys/stat.h> |
26 | #include <sys/file.h> |
27 | |
28 | extern char *sys_errlist[]; |
29 | extern int errno; |
30 | |
31 | main (argc, argv) |
32 | int argc; |
33 | char *argv[]; |
34 | { |
35 | register char *cp; |
36 | char *outfile; |
37 | register int i; |
38 | int offset; |
39 | int out_fd, in_fd; |
40 | struct stat stat_buf; |
41 | int size, pad; |
42 | char buf[1024]; |
43 | static char zeros[4] = {0}; |
44 | |
45 | if (argc < 4) |
46 | err("Not enough arguments\nUsage: %s -o kdb file1 file2 ...\n", |
47 | argv[0]); |
48 | |
49 | outfile = 0; |
50 | for (i = 1; i < argc; i++) |
51 | { |
52 | if (strcmp (argv[i], "-o") == 0) |
53 | outfile = argv[++i]; |
54 | } |
55 | if (outfile == 0) |
56 | err("Output file not specified\n"); |
57 | |
58 | offset = get_offset (outfile, "_heap"); |
59 | |
60 | out_fd = open (outfile, O_WRONLY); |
61 | if (out_fd < 0) |
62 | err ("Error opening %s for write: %s\n", outfile, sys_errlist[errno]); |
63 | if (lseek (out_fd, offset, 0) < 0) |
64 | err ("Error seeking to heap in %s: %s\n", outfile, sys_errlist[errno]); |
65 | |
66 | /* For each file listed on the command line, write it into the |
67 | * 'heap' of the output file. Make sure to skip the arguments |
68 | * that name the output file. */ |
69 | for (i = 1; i < argc; i++) |
70 | { |
71 | if (strcmp (argv[i], "-o") == 0) |
72 | continue; |
73 | if ((in_fd = open (argv[i], O_RDONLY)) < 0) |
74 | err ("Error opening %s for read: %s\n", argv[i], sys_errlist[errno]); |
75 | if (fstat (in_fd, &stat_buf) < 0) |
76 | err ("Error stat'ing %s: %s\n", argv[i], sys_errlist[errno]); |
77 | size = strlen (argv[i]); |
78 | pad = 4 - (size & 3); |
79 | size += pad + stat_buf.st_size + sizeof (int); |
80 | write (out_fd, &size, sizeof (int)); |
81 | write (out_fd, argv[i], strlen (argv[i])); |
82 | write (out_fd, zeros, pad); |
83 | while ((size = read (in_fd, buf, sizeof (buf))) > 0) |
84 | write (out_fd, buf, size); |
85 | close (in_fd); |
86 | } |
87 | size = 0; |
88 | write (out_fd, &size, sizeof (int)); |
89 | close (out_fd); |
90 | return (0); |
91 | } |
92 | |
93 | /* Read symbol table from file and returns the offset into the file |
94 | * where symbol sym_name is located. If error, print message and |
95 | * exit. */ |
96 | get_offset (file, sym_name) |
97 | char *file; |
98 | char *sym_name; |
99 | { |
100 | int f; |
101 | struct exec file_hdr; |
102 | struct nlist *symbol_table; |
103 | int size; |
104 | char *strings; |
105 | |
106 | f = open (file, O_RDONLY); |
107 | if (f < 0) |
108 | err ("Error opening %s: %s\n", file, sys_errlist[errno]); |
109 | if (read (f, &file_hdr, sizeof (file_hdr)) < 0) |
110 | err ("Error reading exec structure: %s\n", sys_errlist[errno]); |
111 | if (N_BADMAG (file_hdr)) |
112 | err ("File %s not an a.out file\n", file); |
113 | |
114 | /* read in symbol table */ |
115 | if ((symbol_table = (struct nlist *)malloc (file_hdr.a_syms)) == 0) |
116 | err ("Couldn't allocate space for symbol table\n"); |
117 | if (lseek (f, N_SYMOFF (file_hdr), 0) == -1) |
118 | err ("lseek error: %s\n", sys_errlist[errno]); |
119 | if (read (f, symbol_table, file_hdr.a_syms) == -1) |
120 | err ("Error reading symbol table from %s: %s\n", file, sys_errlist[errno]); |
121 | |
122 | /* read in string table */ |
123 | if (read (f, &size, 4) == -1) |
124 | err ("reading string table size: %s\n", sys_errlist[errno]); |
125 | if ((strings = (char *)malloc (size)) == 0) |
126 | err ("Couldn't allocate memory for string table\n"); |
127 | if (read (f, strings, size - 4) == -1) |
128 | err ("reading string table: %s\n", sys_errlist[errno]); |
129 | |
130 | /* Find the core address at which the first byte of kdb text segment |
131 | should be loaded into core when kdb is run. */ |
132 | origin = find_symbol ("_etext", symbol_table, file_hdr.a_syms, strings) |
133 | - file_hdr.a_text; |
134 | /* Find the core address at which the heap will appear. */ |
135 | coreaddr = find_symbol (sym_name, symbol_table, file_hdr.a_syms, strings); |
136 | /* Return address in file of the heap data space. */ |
137 | return (N_TXTOFF (file_hdr) + core_addr - origin); |
138 | } |
139 | |
140 | find_symbol (sym_name, symbol_table, length, strings) |
141 | char *sym_name; |
142 | struct nlist *symbol_table; |
143 | int length; |
144 | char *strings; |
145 | { |
146 | register struct nlist *sym; |
147 | |
148 | /* Find symbol in question */ |
149 | for (sym = symbol_table; |
150 | sym != (struct nlist *)((char *)symbol_table + length); |
151 | sym++) |
152 | { |
153 | if ((sym->n_type & N_TYPE) != N_DATA) continue; |
154 | if (sym->n_un.n_strx == 0) continue; |
155 | if (strcmp (sym_name, strings + sym->n_un.n_strx - 4) == 0) |
156 | return sym->n_value; |
157 | } |
158 | err ("Data symbol %s not found in %s\n", sym_name, file); |
159 | } |
160 | |
161 | err (msg, a1, a2, a3) |
162 | char *msg; |
163 | int a1, a2, a3; |
164 | { |
165 | fprintf (stderr, msg, a1, a2, a3); |
166 | exit (-1); |
167 | } |