stuff.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Program to stuff files into a specially prepared space in kdb.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. /* Written 13-Mar-86 by David Bridgham. */
  18. #include <stdio.h>
  19. #include <a.out.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <sys/file.h>
  23. extern char *sys_errlist[];
  24. extern int errno;
  25. main (argc, argv)
  26. int argc;
  27. char *argv[];
  28. {
  29. register char *cp;
  30. char *outfile;
  31. register int i;
  32. int offset;
  33. int out_fd, in_fd;
  34. struct stat stat_buf;
  35. int size, pad;
  36. char buf[1024];
  37. static char zeros[4] = {0};
  38. if (argc < 4)
  39. err("Not enough arguments\nUsage: %s -o kdb file1 file2 ...\n",
  40. argv[0]);
  41. outfile = 0;
  42. for (i = 1; i < argc; i++)
  43. {
  44. if (strcmp (argv[i], "-o") == 0)
  45. outfile = argv[++i];
  46. }
  47. if (outfile == 0)
  48. err("Output file not specified\n");
  49. offset = get_offset (outfile, "_heap");
  50. out_fd = open (outfile, O_WRONLY);
  51. if (out_fd < 0)
  52. err ("Error opening %s for write: %s\n", outfile, sys_errlist[errno]);
  53. if (lseek (out_fd, offset, 0) < 0)
  54. err ("Error seeking to heap in %s: %s\n", outfile, sys_errlist[errno]);
  55. /* For each file listed on the command line, write it into the
  56. * 'heap' of the output file. Make sure to skip the arguments
  57. * that name the output file. */
  58. for (i = 1; i < argc; i++)
  59. {
  60. if (strcmp (argv[i], "-o") == 0)
  61. continue;
  62. if ((in_fd = open (argv[i], O_RDONLY)) < 0)
  63. err ("Error opening %s for read: %s\n", argv[i], sys_errlist[errno]);
  64. if (fstat (in_fd, &stat_buf) < 0)
  65. err ("Error stat'ing %s: %s\n", argv[i], sys_errlist[errno]);
  66. size = strlen (argv[i]);
  67. pad = 4 - (size & 3);
  68. size += pad + stat_buf.st_size + sizeof (int);
  69. write (out_fd, &size, sizeof (int));
  70. write (out_fd, argv[i], strlen (argv[i]));
  71. write (out_fd, zeros, pad);
  72. while ((size = read (in_fd, buf, sizeof (buf))) > 0)
  73. write (out_fd, buf, size);
  74. close (in_fd);
  75. }
  76. size = 0;
  77. write (out_fd, &size, sizeof (int));
  78. close (out_fd);
  79. return (0);
  80. }
  81. /* Read symbol table from file and returns the offset into the file
  82. * where symbol sym_name is located. If error, print message and
  83. * exit. */
  84. get_offset (file, sym_name)
  85. char *file;
  86. char *sym_name;
  87. {
  88. int f;
  89. struct exec file_hdr;
  90. struct nlist *symbol_table;
  91. int size;
  92. char *strings;
  93. f = open (file, O_RDONLY);
  94. if (f < 0)
  95. err ("Error opening %s: %s\n", file, sys_errlist[errno]);
  96. if (read (f, &file_hdr, sizeof (file_hdr)) < 0)
  97. err ("Error reading exec structure: %s\n", sys_errlist[errno]);
  98. if (N_BADMAG (file_hdr))
  99. err ("File %s not an a.out file\n", file);
  100. /* read in symbol table */
  101. if ((symbol_table = (struct nlist *)malloc (file_hdr.a_syms)) == 0)
  102. err ("Couldn't allocate space for symbol table\n");
  103. if (lseek (f, N_SYMOFF (file_hdr), 0) == -1)
  104. err ("lseek error: %s\n", sys_errlist[errno]);
  105. if (read (f, symbol_table, file_hdr.a_syms) == -1)
  106. err ("Error reading symbol table from %s: %s\n", file, sys_errlist[errno]);
  107. /* read in string table */
  108. if (read (f, &size, 4) == -1)
  109. err ("reading string table size: %s\n", sys_errlist[errno]);
  110. if ((strings = (char *)malloc (size)) == 0)
  111. err ("Couldn't allocate memory for string table\n");
  112. if (read (f, strings, size - 4) == -1)
  113. err ("reading string table: %s\n", sys_errlist[errno]);
  114. /* Find the core address at which the first byte of kdb text segment
  115. should be loaded into core when kdb is run. */
  116. origin = find_symbol ("_etext", symbol_table, file_hdr.a_syms, strings)
  117. - file_hdr.a_text;
  118. /* Find the core address at which the heap will appear. */
  119. coreaddr = find_symbol (sym_name, symbol_table, file_hdr.a_syms, strings);
  120. /* Return address in file of the heap data space. */
  121. return (N_TXTOFF (file_hdr) + core_addr - origin);
  122. }
  123. find_symbol (sym_name, symbol_table, length, strings)
  124. char *sym_name;
  125. struct nlist *symbol_table;
  126. int length;
  127. char *strings;
  128. {
  129. register struct nlist *sym;
  130. /* Find symbol in question */
  131. for (sym = symbol_table;
  132. sym != (struct nlist *)((char *)symbol_table + length);
  133. sym++)
  134. {
  135. if ((sym->n_type & N_TYPE) != N_DATA) continue;
  136. if (sym->n_un.n_strx == 0) continue;
  137. if (strcmp (sym_name, strings + sym->n_un.n_strx - 4) == 0)
  138. return sym->n_value;
  139. }
  140. err ("Data symbol %s not found in %s\n", sym_name, file);
  141. }
  142. err (msg, a1, a2, a3)
  143. char *msg;
  144. int a1, a2, a3;
  145. {
  146. fprintf (stderr, msg, a1, a2, a3);
  147. exit (-1);
  148. }