|
|
1.1 ! root 1: /* Program to stuff files into a specially prepared space in kdb. ! 2: Copyright (C) 1986 Free Software Foundation, Inc. ! 3: ! 4: GDB is distributed in the hope that it will be useful, but WITHOUT ANY ! 5: WARRANTY. No author or distributor accepts responsibility to anyone ! 6: for the consequences of using it or for whether it serves any ! 7: particular purpose or works at all, unless he says so in writing. ! 8: Refer to the GDB General Public License for full details. ! 9: ! 10: Everyone is granted permission to copy, modify and redistribute GDB, ! 11: but only under the conditions described in the GDB General Public ! 12: License. A copy of this license is supposed to have been given to you ! 13: along with GDB so you can know your rights and responsibilities. It ! 14: should be in a file named COPYING. Among other things, the copyright ! 15: notice and this notice must be preserved on all copies. ! 16: ! 17: In other words, go ahead and share GDB, but don't try to stop ! 18: anyone else from sharing it farther. Help stamp out software hoarding! ! 19: */ ! 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 <sys/stat.h> ! 27: #include <sys/file.h> ! 28: ! 29: extern char *sys_errlist[]; ! 30: extern int errno; ! 31: ! 32: main (argc, argv) ! 33: int argc; ! 34: char *argv[]; ! 35: { ! 36: register char *cp; ! 37: char *outfile; ! 38: register int i; ! 39: int offset; ! 40: int out_fd, in_fd; ! 41: struct stat stat_buf; ! 42: int size, pad; ! 43: char buf[1024]; ! 44: static char zeros[4] = {0}; ! 45: ! 46: if (argc < 4) ! 47: err("Not enough arguments\nUsage: %s -o kdb file1 file2 ...\n", ! 48: argv[0]); ! 49: ! 50: outfile = 0; ! 51: for (i = 1; i < argc; i++) ! 52: { ! 53: if (strcmp (argv[i], "-o") == 0) ! 54: outfile = argv[++i]; ! 55: } ! 56: if (outfile == 0) ! 57: err("Output file not specified\n"); ! 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, sys_errlist[errno]); ! 64: if (lseek (out_fd, offset, 0) < 0) ! 65: err ("Error seeking to heap in %s: %s\n", outfile, sys_errlist[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 (strcmp (argv[i], "-o") == 0) ! 73: continue; ! 74: if ((in_fd = open (argv[i], O_RDONLY)) < 0) ! 75: err ("Error opening %s for read: %s\n", argv[i], sys_errlist[errno]); ! 76: if (fstat (in_fd, &stat_buf) < 0) ! 77: err ("Error stat'ing %s: %s\n", argv[i], sys_errlist[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, sys_errlist[errno]); ! 110: if (read (f, &file_hdr, sizeof (file_hdr)) < 0) ! 111: err ("Error reading exec structure: %s\n", sys_errlist[errno]); ! 112: if (N_BADMAG (file_hdr)) ! 113: err ("File %s not an a.out file\n", file); ! 114: ! 115: /* read in symbol table */ ! 116: if ((symbol_table = (struct nlist *)malloc (file_hdr.a_syms)) == 0) ! 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", sys_errlist[errno]); ! 120: if (read (f, symbol_table, file_hdr.a_syms) == -1) ! 121: err ("Error reading symbol table from %s: %s\n", file, sys_errlist[errno]); ! 122: ! 123: /* read in string table */ ! 124: if (read (f, &size, 4) == -1) ! 125: err ("reading string table size: %s\n", sys_errlist[errno]); ! 126: if ((strings = (char *)malloc (size)) == 0) ! 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", sys_errlist[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: ! 141: find_symbol (sym_name, symbol_table, length, strings) ! 142: char *sym_name; ! 143: struct nlist *symbol_table; ! 144: int length; ! 145: char *strings; ! 146: { ! 147: register struct nlist *sym; ! 148: ! 149: /* Find symbol in question */ ! 150: for (sym = symbol_table; ! 151: sym != (struct nlist *)((char *)symbol_table + length); ! 152: sym++) ! 153: { ! 154: if ((sym->n_type & N_TYPE) != N_DATA) continue; ! 155: if (sym->n_un.n_strx == 0) continue; ! 156: if (strcmp (sym_name, strings + sym->n_un.n_strx - 4) == 0) ! 157: return sym->n_value; ! 158: } ! 159: err ("Data symbol %s not found in %s\n", sym_name, file); ! 160: } ! 161: ! 162: err (msg, a1, a2, a3) ! 163: char *msg; ! 164: int a1, a2, a3; ! 165: { ! 166: fprintf (stderr, msg, a1, a2, a3); ! 167: exit (-1); ! 168: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.