2001-03-16 Orjan Friberg <orjanf@axis.com>
[deliverable/binutils-gdb.git] / gdb / stuff.c
CommitLineData
c906108c 1/* Program to stuff files into a specially prepared space in kdb.
b6ba6518
KB
2 Copyright 1986, 1989, 1991, 1992, 1995, 2000
3 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22/* Written 13-Mar-86 by David Bridgham. */
23
24#include <stdio.h>
25#include <a.out.h>
26#include <sys/types.h>
27#include "gdb_stat.h"
28#include <sys/file.h>
29#include <varargs.h>
30
31main (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];
c5aa993b
JM
43 static char zeros[4] =
44 {0};
c906108c
SS
45
46 if (argc < 4)
c5aa993b
JM
47 err ("Not enough arguments\nUsage: %s -o kdb file1 file2 ...\n",
48 argv[0]);
c906108c
SS
49
50 outfile = 0;
51 for (i = 1; i < argc; i++)
52 {
53 if (STREQ (argv[i], "-o"))
54 outfile = argv[++i];
55 }
56 if (outfile == 0)
c5aa993b 57 err ("Output file not specified\n");
c906108c
SS
58
59 offset = get_offset (outfile, "_heap");
60
61 out_fd = open (outfile, O_WRONLY);
62 if (out_fd < 0)
63 err ("Error opening %s for write: %s\n", outfile, strerror (errno));
64 if (lseek (out_fd, offset, 0) < 0)
65 err ("Error seeking to heap in %s: %s\n", outfile, strerror (errno));
66
67 /* For each file listed on the command line, write it into the
68 * 'heap' of the output file. Make sure to skip the arguments
69 * that name the output file. */
70 for (i = 1; i < argc; i++)
71 {
72 if (STREQ (argv[i], "-o"))
73 continue;
74 if ((in_fd = open (argv[i], O_RDONLY)) < 0)
75 err ("Error opening %s for read: %s\n", argv[i],
76 strerror (errno));
77 if (fstat (in_fd, &stat_buf) < 0)
78 err ("Error stat'ing %s: %s\n", argv[i], strerror (errno));
79 size = strlen (argv[i]);
80 pad = 4 - (size & 3);
81 size += pad + stat_buf.st_size + sizeof (int);
82 write (out_fd, &size, sizeof (int));
83 write (out_fd, argv[i], strlen (argv[i]));
84 write (out_fd, zeros, pad);
85 while ((size = read (in_fd, buf, sizeof (buf))) > 0)
86 write (out_fd, buf, size);
87 close (in_fd);
88 }
89 size = 0;
90 write (out_fd, &size, sizeof (int));
91 close (out_fd);
92 return (0);
93}
94
95/* Read symbol table from file and returns the offset into the file
96 * where symbol sym_name is located. If error, print message and
97 * exit. */
fba45db2 98get_offset (char *file, char *sym_name)
c906108c
SS
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, strerror (errno));
109 if (read (f, &file_hdr, sizeof (file_hdr)) < 0)
110 err ("Error reading exec structure: %s\n", strerror (errno));
111 if (N_BADMAG (file_hdr))
112 err ("File %s not an a.out file\n", file);
113
114 /* read in symbol table */
c5aa993b 115 if ((symbol_table = (struct nlist *) malloc (file_hdr.a_syms)) == 0)
c906108c
SS
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", strerror (errno));
119 if (read (f, symbol_table, file_hdr.a_syms) == -1)
120 err ("Error reading symbol table from %s: %s\n", file,
121 strerror (errno));
122
123 /* read in string table */
124 if (read (f, &size, 4) == -1)
125 err ("reading string table size: %s\n", strerror (errno));
c5aa993b 126 if ((strings = (char *) malloc (size)) == 0)
c906108c
SS
127 err ("Couldn't allocate memory for string table\n");
128 if (read (f, strings, size - 4) == -1)
129 err ("reading string table: %s\n", strerror (errno));
130
131 /* Find the core address at which the first byte of kdb text segment
132 should be loaded into core when kdb is run. */
133 origin = find_symbol ("_etext", symbol_table, file_hdr.a_syms, strings)
134 - file_hdr.a_text;
135 /* Find the core address at which the heap will appear. */
136 coreaddr = find_symbol (sym_name, symbol_table, file_hdr.a_syms, strings);
137 /* Return address in file of the heap data space. */
138 return (N_TXTOFF (file_hdr) + core_addr - origin);
139}
140
fba45db2
KB
141find_symbol (char *sym_name, struct nlist *symbol_table, int length,
142 char *strings)
c906108c
SS
143{
144 register struct nlist *sym;
145
146 /* Find symbol in question */
147 for (sym = symbol_table;
c5aa993b 148 sym != (struct nlist *) ((char *) symbol_table + length);
c906108c 149 sym++)
c5aa993b
JM
150 {
151 if ((sym->n_type & N_TYPE) != N_DATA)
152 continue;
153 if (sym->n_un.n_strx == 0)
154 continue;
155 if (STREQ (sym_name, strings + sym->n_un.n_strx - 4))
156 return sym->n_value;
157 }
158 err ("Data symbol %s not found in %s\n", sym_name, file);
c906108c
SS
159}
160
161/* VARARGS */
162void
163err (va_alist)
164 va_dcl
165{
166 va_list args;
167 char *string;
168
169 va_start (args);
170 string = va_arg (args, char *);
171 vfprintf (gdb_stderr, string, args);
172 va_end (args);
173 exit (-1);
174}
This page took 0.135 seconds and 4 git commands to generate.