Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Program to stuff files into a specially prepared space in kdb. |
2 | Copyright (C) 1986, 1989, 1991 Free Software Foundation, Inc. | |
3 | ||
c5aa993b | 4 | This file is part of GDB. |
c906108c | 5 | |
c5aa993b JM |
6 | This program 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 2 of the License, or | |
9 | (at your option) any later version. | |
c906108c | 10 | |
c5aa993b JM |
11 | This program 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. | |
c906108c | 15 | |
c5aa993b JM |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
20 | |
21 | /* Written 13-Mar-86 by David Bridgham. */ | |
22 | ||
23 | #include <stdio.h> | |
24 | #include <a.out.h> | |
25 | #include <sys/types.h> | |
26 | #include "gdb_stat.h" | |
27 | #include <sys/file.h> | |
28 | #include <varargs.h> | |
29 | ||
30 | main (argc, argv) | |
31 | int argc; | |
32 | char *argv[]; | |
33 | { | |
34 | register char *cp; | |
35 | char *outfile; | |
36 | register int i; | |
37 | int offset; | |
38 | int out_fd, in_fd; | |
39 | struct stat stat_buf; | |
40 | int size, pad; | |
41 | char buf[1024]; | |
c5aa993b JM |
42 | static char zeros[4] = |
43 | {0}; | |
c906108c SS |
44 | |
45 | if (argc < 4) | |
c5aa993b JM |
46 | err ("Not enough arguments\nUsage: %s -o kdb file1 file2 ...\n", |
47 | argv[0]); | |
c906108c SS |
48 | |
49 | outfile = 0; | |
50 | for (i = 1; i < argc; i++) | |
51 | { | |
52 | if (STREQ (argv[i], "-o")) | |
53 | outfile = argv[++i]; | |
54 | } | |
55 | if (outfile == 0) | |
c5aa993b | 56 | err ("Output file not specified\n"); |
c906108c SS |
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, strerror (errno)); | |
63 | if (lseek (out_fd, offset, 0) < 0) | |
64 | err ("Error seeking to heap in %s: %s\n", outfile, strerror (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 (STREQ (argv[i], "-o")) | |
72 | continue; | |
73 | if ((in_fd = open (argv[i], O_RDONLY)) < 0) | |
74 | err ("Error opening %s for read: %s\n", argv[i], | |
75 | strerror (errno)); | |
76 | if (fstat (in_fd, &stat_buf) < 0) | |
77 | err ("Error stat'ing %s: %s\n", argv[i], strerror (errno)); | |
78 | size = strlen (argv[i]); | |
79 | pad = 4 - (size & 3); | |
80 | size += pad + stat_buf.st_size + sizeof (int); | |
81 | write (out_fd, &size, sizeof (int)); | |
82 | write (out_fd, argv[i], strlen (argv[i])); | |
83 | write (out_fd, zeros, pad); | |
84 | while ((size = read (in_fd, buf, sizeof (buf))) > 0) | |
85 | write (out_fd, buf, size); | |
86 | close (in_fd); | |
87 | } | |
88 | size = 0; | |
89 | write (out_fd, &size, sizeof (int)); | |
90 | close (out_fd); | |
91 | return (0); | |
92 | } | |
93 | ||
94 | /* Read symbol table from file and returns the offset into the file | |
95 | * where symbol sym_name is located. If error, print message and | |
96 | * exit. */ | |
97 | get_offset (file, sym_name) | |
98 | char *file; | |
99 | char *sym_name; | |
100 | { | |
101 | int f; | |
102 | struct exec file_hdr; | |
103 | struct nlist *symbol_table; | |
104 | int size; | |
105 | char *strings; | |
106 | ||
107 | f = open (file, O_RDONLY); | |
108 | if (f < 0) | |
109 | err ("Error opening %s: %s\n", file, strerror (errno)); | |
110 | if (read (f, &file_hdr, sizeof (file_hdr)) < 0) | |
111 | err ("Error reading exec structure: %s\n", strerror (errno)); | |
112 | if (N_BADMAG (file_hdr)) | |
113 | err ("File %s not an a.out file\n", file); | |
114 | ||
115 | /* read in symbol table */ | |
c5aa993b | 116 | if ((symbol_table = (struct nlist *) malloc (file_hdr.a_syms)) == 0) |
c906108c SS |
117 | err ("Couldn't allocate space for symbol table\n"); |
118 | if (lseek (f, N_SYMOFF (file_hdr), 0) == -1) | |
119 | err ("lseek error: %s\n", strerror (errno)); | |
120 | if (read (f, symbol_table, file_hdr.a_syms) == -1) | |
121 | err ("Error reading symbol table from %s: %s\n", file, | |
122 | strerror (errno)); | |
123 | ||
124 | /* read in string table */ | |
125 | if (read (f, &size, 4) == -1) | |
126 | err ("reading string table size: %s\n", strerror (errno)); | |
c5aa993b | 127 | if ((strings = (char *) malloc (size)) == 0) |
c906108c SS |
128 | err ("Couldn't allocate memory for string table\n"); |
129 | if (read (f, strings, size - 4) == -1) | |
130 | err ("reading string table: %s\n", strerror (errno)); | |
131 | ||
132 | /* Find the core address at which the first byte of kdb text segment | |
133 | should be loaded into core when kdb is run. */ | |
134 | origin = find_symbol ("_etext", symbol_table, file_hdr.a_syms, strings) | |
135 | - file_hdr.a_text; | |
136 | /* Find the core address at which the heap will appear. */ | |
137 | coreaddr = find_symbol (sym_name, symbol_table, file_hdr.a_syms, strings); | |
138 | /* Return address in file of the heap data space. */ | |
139 | return (N_TXTOFF (file_hdr) + core_addr - origin); | |
140 | } | |
141 | ||
142 | find_symbol (sym_name, symbol_table, length, strings) | |
143 | char *sym_name; | |
144 | struct nlist *symbol_table; | |
145 | int length; | |
146 | char *strings; | |
147 | { | |
148 | register struct nlist *sym; | |
149 | ||
150 | /* Find symbol in question */ | |
151 | for (sym = symbol_table; | |
c5aa993b | 152 | sym != (struct nlist *) ((char *) symbol_table + length); |
c906108c | 153 | sym++) |
c5aa993b JM |
154 | { |
155 | if ((sym->n_type & N_TYPE) != N_DATA) | |
156 | continue; | |
157 | if (sym->n_un.n_strx == 0) | |
158 | continue; | |
159 | if (STREQ (sym_name, strings + sym->n_un.n_strx - 4)) | |
160 | return sym->n_value; | |
161 | } | |
162 | err ("Data symbol %s not found in %s\n", sym_name, file); | |
c906108c SS |
163 | } |
164 | ||
165 | /* VARARGS */ | |
166 | void | |
167 | err (va_alist) | |
168 | va_dcl | |
169 | { | |
170 | va_list args; | |
171 | char *string; | |
172 | ||
173 | va_start (args); | |
174 | string = va_arg (args, char *); | |
175 | vfprintf (gdb_stderr, string, args); | |
176 | va_end (args); | |
177 | exit (-1); | |
178 | } |