/*
* undump - resurrect a core file into a running program.
*
* for UNIX System V on a 3Bx
* that uses the Common Object File Format
*
* Author:
* Lou Salkind
* New York University
* Tue Mar 3 13:18:25 EST 1987
*
* Adapted from:
* Spencer Thomas's undump and the file unexec.c in GNU emacs
*/
copy_syms(afp, nfp)
register FILE *afp, *nfp;
{
char page[BUFSIZ];
register int n;
register int nsyms;
struct syment symentry;
AUXENT auxentry;
/* if there are line numbers, copy them */
if (lnnoptr) {
if (fseek(afp, lnnoptr, 0) == -1L)
Perror("ln fseek");
copy(afp, nfp, symlocptr - lnnoptr);
}
/* now write the symbol table */
if (fseek(nfp, fh.f_symptr, 0) == -1L)
Perror("fh fseek");
for (nsyms = 0; nsyms < fh.f_nsyms; nsyms++) {
if (fread(&symentry, SYMESZ, 1, afp) != 1)
Perror("sym fread");
if (fwrite(&symentry, SYMESZ, 1, nfp) != 1)
Perror("sym fwrite");
/*
* adjust relative offsets of line numbers for
* function definitions
*/
if (symentry.n_numaux) {
if (fread(&auxentry, AUXESZ, 1, afp) != 1)
Perror("aux fread");
nsyms++;
if (ISFCN (symentry.n_type))
auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
if (fwrite(&auxentry, AUXESZ, 1, nfp) != 1)
Perror("aux fwrite");
}
}
/* finally write the string table, if any */
while ((n = fread(page, 1, sizeof page, afp)) > 0) {
if (fwrite(page, 1, n, nfp) != n)
Perror("sym write");
}
if (n < 0)
Perror("sym read");
}
/*
* After succesfully building the new a.out, mark it executable
*/
mark_x(name)
char *name;
{
struct stat sbuf;
int um;
um = umask(777);
umask(um);
if (stat(name, &sbuf) == -1)
{
perror ("Can't stat new a.out");
fprintf(stderr, "Setting protection to %o\n", 0777 & ~um);
sbuf.st_mode = 0777;
}
sbuf.st_mode |= 0111 & ~um;
if (chmod(name, sbuf.st_mode) == -1)
perror("Couldn't change mode of new a.out to executable");
}
copy(a, b, size)
register FILE *a, *b;
long size;
{
char buf[BUFSIZ];
register int i, n;
while (size > 0) {
i = size;
if (i > sizeof buf)
i = sizeof buf;
if ((n = fread(buf, 1, i, a)) <= 0)
Perror("copy read");
if (fwrite(buf, 1, n, b) != n)
Perror("copy write");
size -= n;
}
}