X-Git-Url: https://git.hcoop.net/bpt/emacs.git/blobdiff_plain/3b95c869b1b2ad7d92e1c5df73edebf83b6b2c5c..b6e11efd50d48abb8077c5b055688a08d51e80aa:/src/unexsunos4.c diff --git a/src/unexsunos4.c b/src/unexsunos4.c index 48934f2144..76480db9b3 100644 --- a/src/unexsunos4.c +++ b/src/unexsunos4.c @@ -1,3 +1,23 @@ +/* Unexec for Sunos 4 using shared libraries. + Copyright (C) 1990, 1994, 1999 Free Software Foundation, Inc. + +This file is part of GNU Emacs. + +GNU Emacs is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Emacs is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Emacs; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + /* Contributed by Viktor Dukhovni. */ /* * Unexec for Berkeley a.out format + SUNOS shared libraries @@ -23,6 +43,7 @@ * is somewhat abused here) is loaded first! * */ + #ifdef emacs #include #endif @@ -35,6 +56,18 @@ #include #include +#if defined (SUNOS4) || defined (__FreeBSD__) || defined (__NetBSD__) +#define UNDO_RELOCATION +#endif + +#ifdef UNDO_RELOCATION +#include +#endif + +#ifdef HAVE_UNISTD_H +#include +#endif + /* NetBSD needs this bit, but SunOS does not have it. */ #ifndef MAP_FILE #define MAP_FILE 0 @@ -59,7 +92,7 @@ #endif extern char *getenv (); -static unsigned Brk; +static unsigned brk_value; static struct exec nhdr; static int rd_only_len; static long cookie; @@ -74,10 +107,6 @@ unexec (new_name, a_name, bndry, bss_start, entry) struct exec ohdr; /* Allocate on the stack, not needed in the next life */ struct stat stat; -#ifdef emacs - fprintf (stderr, "Used %d bytes of Pure Storage\n", pureptr); -#endif - if ((fd = open (a_name, O_RDONLY)) < 0) { fprintf (stderr, "%s: open: ", a_name); @@ -116,7 +145,8 @@ unexec (new_name, a_name, bndry, bss_start, entry) */ cookie = time (0); - Brk = sbrk (0); /* Save the break, it is reset to &_end (by ld.so?) */ + /* Save the break, it is reset to &_end (by ld.so?). */ + brk_value = (unsigned) sbrk (0); /* * Round up data start to a page boundary (Lose if not a 2 power!) @@ -134,15 +164,13 @@ unexec (new_name, a_name, bndry, bss_start, entry) initialized = 1; #endif - /* - * Handle new data and bss sizes and optional new entry point. - * No one actually uses bss_start and entry, but tradition compels - * one to support them. - * Could complain if bss_start > Brk, but the caller is *supposed* to know - * what she is doing. - */ - nhdr.a_data = (bss_start ? bss_start : Brk) - N_DATADDR (nhdr); - nhdr.a_bss = bss_start ? Brk - bss_start : 0; + /* Handle new data and bss sizes and optional new entry point. + No one actually uses bss_start and entry, but tradition compels + one to support them. + Could complain if bss_start > brk_value, + but the caller is *supposed* to know what she is doing. */ + nhdr.a_data = (bss_start ? bss_start : brk_value) - N_DATADDR (nhdr); + nhdr.a_bss = bss_start ? brk_value - bss_start : 0; if (entry) nhdr.a_entry = entry; @@ -177,6 +205,71 @@ unexec (new_name, a_name, bndry, bss_start, entry) lseek (new, N_TRELOFF (nhdr), L_SET); write (new, old + N_TRELOFF (ohdr), stat.st_size - N_TRELOFF (ohdr)); + /* Some other BSD systems use this file. + We don't know whether this change is right for them. */ +#ifdef UNDO_RELOCATION + /* Undo the relocations done at startup by ld.so. + It will do these relocations again when we start the dumped Emacs. + Doing them twice gives incorrect results. */ + { + unsigned long daddr = N_DATADDR (ohdr); + unsigned long rel, erel; +#ifdef SUNOS4 +#ifdef SUNOS4_SHARED_LIBRARIES + extern struct link_dynamic _DYNAMIC; + + /* SunOS4.x's ld_rel is relative to N_TXTADDR. */ + if (!ohdr.a_dynamic) + /* This was statically linked. */ + rel = erel = 0; + else if (_DYNAMIC.ld_version < 2) + { + rel = _DYNAMIC.ld_un.ld_1->ld_rel + N_TXTADDR (ohdr); + erel = _DYNAMIC.ld_un.ld_1->ld_hash + N_TXTADDR (ohdr); + } + else + { + rel = _DYNAMIC.ld_un.ld_2->ld_rel + N_TXTADDR (ohdr); + erel = _DYNAMIC.ld_un.ld_2->ld_hash + N_TXTADDR (ohdr); + } +#else /* not SUNOS4_SHARED_LIBRARIES */ + rel = erel = 0; +#endif /* not SUNOS4_SHARED_LIBRARIES */ +#ifdef sparc +#define REL_INFO_TYPE struct reloc_info_sparc +#else +#define REL_INFO_TYPE struct relocation_info +#endif /* sparc */ +#define REL_TARGET_ADDRESS(r) (((REL_INFO_TYPE *)(r))->r_address) +#endif /* SUNOS4 */ +#if defined (__FreeBSD__) || defined (__NetBSD__) + extern struct _dynamic _DYNAMIC; + + /* FreeBSD's LD_REL is a virtual address itself. */ + rel = LD_REL (&_DYNAMIC); + erel = rel + LD_RELSZ (&_DYNAMIC); +#define REL_INFO_TYPE struct relocation_info +#define REL_TARGET_ADDRESS(r) (((REL_INFO_TYPE *)(r))->r_address) +#endif + + for (; rel < erel; rel += sizeof (REL_INFO_TYPE)) + { + /* This is the virtual address where ld.so will do relocation. */ + unsigned long target = REL_TARGET_ADDRESS (rel); + /* This is the offset in the data segment. */ + unsigned long segoffset = target - daddr; + + /* If it is located below data_start, we have to do nothing here, + because the old data has been already written to the location. */ + if (target < (unsigned long)&data_start) + continue; + + lseek (new, N_DATOFF (nhdr) + segoffset, L_SET); + write (new, old + N_DATOFF (ohdr) + segoffset, sizeof (unsigned long)); + } + } +#endif /* UNDO_RELOCATION */ + fchmod (new, 0755); } @@ -192,7 +285,7 @@ run_time_remap (progname) return; /* Restore the break */ - brk (Brk); + brk ((char *) brk_value); /* If nothing to remap: we are done! */ if (rd_only_len == 0) @@ -239,20 +332,20 @@ run_time_remap (progname) } } -is_it (path) - char *path; +is_it (filename) + char *filename; { int fd; - long paths_cookie; + long filenames_cookie; struct exec hdr; /* * Open an executable and check for a valid header! - * Can't bcmp() the header with what we had, it may have been stripped! + * Can't bcmp the header with what we had, it may have been stripped! * so we may save looking at non executables with the same name, mostly * directories. */ - fd = open (path, O_RDONLY); + fd = open (filename, O_RDONLY); if (fd != -1) { if (read (fd, &hdr, sizeof (hdr)) == sizeof (hdr) @@ -261,8 +354,8 @@ is_it (path) { /* compare cookies */ lseek (fd, N_DATOFF (hdr) + (int)&cookie - N_DATADDR (hdr), L_SET); - read (fd, &paths_cookie, sizeof (paths_cookie)); - if (paths_cookie == cookie) + read (fd, &filenames_cookie, sizeof (filenames_cookie)); + if (filenames_cookie == cookie) { /* Eureka */ /* @@ -271,7 +364,7 @@ is_it (path) * should the shared library decide to indirect through * addresses in the data segment not part of __DYNAMIC */ - mmap (data_start, rd_only_len, PROT_READ | PROT_EXEC, + mmap ((char *) data_start, rd_only_len, PROT_READ | PROT_EXEC, MAP_FILE | MAP_SHARED | MAP_FIXED, fd, N_DATOFF (hdr) + data_start - N_DATADDR (hdr)); close (fd);