Clean up and make code (more) readable.

This commit is contained in:
Poul-Henning Kamp 1995-10-29 09:49:21 +00:00
parent 77103ea36a
commit 05fc2de514
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=11912
2 changed files with 98 additions and 113 deletions

View File

@ -1,7 +1,7 @@
# from: @(#)Makefile 5.6 (Berkeley) 5/22/91
# $Id: Makefile,v 1.20 1995/10/20 20:05:15 phk Exp $
# $Id: Makefile,v 1.21 1995/10/22 18:36:47 bde Exp $
CFLAGS+= -DLIBC_SCCS -fno-omit-frame-pointer
CFLAGS+= -DUGLY_LOCALE_HACK -DLIBC_SCCS -fno-omit-frame-pointer
OBJS= crt0.o c++rt0.o gcrt0.o scrt0.o sgcrt0.o
CLEANFILES+= a.out
MAN3+= dlopen.3

View File

@ -30,100 +30,108 @@
* $Id: crt0.c,v 1.18 1995/09/27 23:13:33 nate Exp $
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "%W% (Erasmus) %G%";
#endif /* LIBC_SCCS and not lint */
extern void exit();
int _callmain();
#include <sys/param.h>
#ifdef UGLY_LOCALE_HACK
#include <locale.h>
#endif
#include <stdlib.h>
extern void _startup_setlocale __P((int, const char *));
#ifdef DYNAMIC
#include <sys/types.h>
#include <sys/syscall.h>
#include <a.out.h>
#include <string.h>
#ifndef N_GETMAGIC
#define N_GETMAGIC(x) ((x).a_magic)
#endif
#ifndef N_BSSADDR
#define N_BSSADDR(x) (N_DATADDR(x)+(x).a_data)
#endif
#include <sys/mman.h>
#ifdef sun
#define MAP_COPY MAP_PRIVATE
#define MAP_FILE 0
#define MAP_ANON 0
#else
#ifdef BSD
#if BSD>=199306 && !defined(MAP_FILE)
#define MAP_FILE 0
#endif /* BSD>=199306 */
#endif /* BSD */
#endif /* sun */
#include <link.h>
extern struct _dynamic _DYNAMIC;
static struct ld_entry *ld_entry;
static void __do_dynamic_link ();
#ifdef DEBUG
static char *_getenv();
static int _strncmp();
/* !!!
* This is gross, ld.so is a ZMAGIC a.out, but has `sizeof(hdr)' for
* an entry point and not at PAGSIZ as the N_*ADDR macros assume.
*/
#undef N_DATADDR
#define N_DATADDR(x) ((x).a_text)
#undef N_BSSADDR
#define N_BSSADDR(x) ((x).a_text + (x).a_data)
#ifndef N_GETMAGIC
#define N_GETMAGIC(x) ((x).a_magic)
#endif /* N_GETMAGIC */
#ifndef MAP_PRIVATE
#define MAP_PRIVATE MAP_COPY
#endif /* MAP_PRIVATE */
#ifndef MAP_FILE
#define MAP_FILE 0
#endif /* MAP_FILE */
#ifndef MAP_ANON
#define MAP_ANON 0
#endif /* MAP_ANON */
#ifdef DEBUG
/*
* We need these two because we are going to call them before the ld.so is
* finished (as a matter of fact before we know if it exists !) so we must
* provide these versions for them
*/
static char *_getenv();
static int _strncmp();
#endif /* DEBUG */
#ifdef sun
#define LDSO "/usr/lib/ld.so"
#endif
#ifdef BSD
#define LDSO "/usr/libexec/ld.so"
#endif
#ifndef LDSO
#define LDSO "/usr/libexec/ld.so"
#endif /* LDSO */
extern struct _dynamic _DYNAMIC;
static struct ld_entry *ld_entry;
static void __do_dynamic_link ();
#endif /* DYNAMIC */
static char *_strrchr();
#ifdef UGLY_LOCALE_HACK
extern void _startup_setlocale __P((int, const char *));
#endif
int _callmain();
int errno;
static char empty[1];
char *__progname = empty;
char **environ;
#ifdef BSD
extern unsigned char etext;
extern unsigned char eprol asm ("eprol");
extern start() asm("start");
extern mcount() asm ("mcount");
extern int main(int argc, char **argv, char **envp);
int __syscall(int syscall,...);
#ifdef MCRT0
void monstartup(void *low, void *high);
#endif /* MCRT0 */
int errno;
static char empty[1];
char *__progname = empty;
#endif
/*
* We need these system calls, but can't use library stubs
* We need these system calls, but can't use library stubs because the are
* not accessible until we have done the ld.so stunt.
*/
#define _exit(v) __syscall(SYS_exit, (int)(v))
#define open(name, f, m) __syscall(SYS_open, (char *)(name), (int)(f), (int)(m))
#define close(fd) __syscall(SYS_close, (int)(fd))
#define read(fd, s, n) __syscall(SYS_read, (int)(fd), (void *)(s), (size_t)(n))
#define write(fd, s, n) __syscall(SYS_write, (int)(fd), (void *)(s), (size_t)(n))
#define dup(fd) __syscall(SYS_dup, (int)(fd))
#define dup2(fd, fdnew) __syscall(SYS_dup2, (int)(fd), (int)(fdnew))
#ifdef sun
#define mmap(addr, len, prot, flags, fd, off) \
__syscall(SYS_mmap, (addr), (len), (prot), _MAP_NEW|(flags), (fd), (off))
#else
#define mmap(addr, len, prot, flags, fd, off) \
__syscall(SYS_mmap, (caddr_t)(addr), (size_t)(len), (int)(prot), (int)(flags), (int)(fd), (long)0L, (off_t)(off))
#endif
#define _PUTNMSG(str, len) write(2, (str), (len))
#define _exit(v) \
__syscall(SYS_exit, (int)(v))
#define _open(name, f, m) \
__syscall(SYS_open, (char *)(name), (int)(f), (int)(m))
#define _read(fd, s, n) \
__syscall(SYS_read, (int)(fd), (void *)(s), (size_t)(n))
#define _write(fd, s, n) \
__syscall(SYS_write, (int)(fd), (void *)(s), (size_t)(n))
#define _mmap(addr, len, prot, flags, fd, off) \
(caddr_t) __syscall(SYS_mmap, (caddr_t)(addr), (size_t)(len), \
(int)(prot), (int)(flags), (int)(fd), (long)0L, (off_t)(off))
#define _PUTNMSG(str, len) _write(2, (str), (len))
#define _PUTMSG(str) _PUTNMSG((str), sizeof (str) - 1)
#define _FATAL(str) ( _PUTMSG(str), _exit(1) )
int
start()
{
struct kframe {
@ -156,22 +164,20 @@ start()
--targv;
environ = targv;
if (argv[0])
if ((__progname = _strrchr(argv[0], '/')) == NULL)
__progname = argv[0];
else
++__progname;
if (argv[0]) {
register char *s;
__progname = argv[0];
for (s=__progname; *s != '\0'; s++)
if (*s == '/')
__progname = s+1;
}
#ifdef DYNAMIC
/* ld(1) convention: if DYNAMIC = 0 then statically linked */
#ifdef stupid_gcc
if (&_DYNAMIC)
__do_dynamic_link();
#else
/* sometimes GCC is too smart/stupid for its own good */
x = (caddr_t)&_DYNAMIC;
if (x)
__do_dynamic_link();
#endif
#endif /* DYNAMIC */
asm("eprol:");
@ -181,8 +187,10 @@ asm("eprol:");
monstartup(&eprol, &etext);
#endif /* MCRT0 */
#ifdef UGLY_LOCALE_HACK
if (getenv("ENABLE_STARTUP_LOCALE") != NULL)
_startup_setlocale(LC_ALL, "");
#endif /* UGLY_LOCALE_HACK */
asm ("__callmain:"); /* Defined for the benefit of debuggers */
exit(main(kfp->kargc, argv, environ));
@ -204,13 +212,15 @@ __do_dynamic_link ()
#endif
ldso = LDSO;
crt.crt_ldfd = open(ldso, 0, 0);
crt.crt_ldfd = _open(ldso, 0, 0);
if (crt.crt_ldfd == -1) {
_FATAL("No ld.so\n");
_PUTMSG("Couldn't open ");
_PUTMSG(LDSO);
_FATAL(".\n");
}
/* Read LDSO exec header */
if (read(crt.crt_ldfd, &hdr, sizeof hdr) < sizeof hdr) {
if (_read(crt.crt_ldfd, &hdr, sizeof hdr) < sizeof hdr) {
_FATAL("Failure reading ld.so\n");
}
if ((N_GETMAGIC_NET(hdr) != ZMAGIC) && (N_GETMAGIC(hdr) != QMAGIC)) {
@ -221,39 +231,29 @@ __do_dynamic_link ()
crt.crt_dzfd = -1;
/* Map in ld.so */
crt.crt_ba = mmap(0, hdr.a_text,
crt.crt_ba = (int)_mmap(0, hdr.a_text,
PROT_READ|PROT_EXEC,
MAP_FILE|MAP_COPY,
MAP_FILE|MAP_PRIVATE,
crt.crt_ldfd, N_TXTOFF(hdr));
if (crt.crt_ba == -1) {
_FATAL("Cannot map ld.so\n");
_FATAL("Cannot map ld.so (text)\n");
}
#ifdef BSD
/* !!!
* This is gross, ld.so is a ZMAGIC a.out, but has `sizeof(hdr)' for
* an entry point and not at PAGSIZ as the N_*ADDR macros assume.
*/
#undef N_DATADDR
#undef N_BSSADDR
#define N_DATADDR(x) ((x).a_text)
#define N_BSSADDR(x) ((x).a_text + (x).a_data)
#endif
/* Map in data segment of ld.so writable */
if (mmap(crt.crt_ba+N_DATADDR(hdr), hdr.a_data,
if ((int)_mmap((caddr_t)(crt.crt_ba+N_DATADDR(hdr)), hdr.a_data,
PROT_READ|PROT_WRITE,
MAP_FIXED|MAP_FILE|MAP_COPY,
MAP_FIXED|MAP_FILE|MAP_PRIVATE,
crt.crt_ldfd, N_DATOFF(hdr)) == -1) {
_FATAL("Cannot map ld.so\n");
_FATAL("Cannot map ld.so (data)\n");
}
/* Map bss segment of ld.so zero */
if (hdr.a_bss && mmap(crt.crt_ba+N_BSSADDR(hdr), hdr.a_bss,
if (hdr.a_bss && (int)_mmap((caddr_t)(crt.crt_ba+N_BSSADDR(hdr)),
hdr.a_bss,
PROT_READ|PROT_WRITE,
MAP_FIXED|MAP_ANON|MAP_COPY,
MAP_FIXED|MAP_ANON|MAP_PRIVATE,
crt.crt_dzfd, 0) == -1) {
_FATAL("Cannot map ld.so\n");
_FATAL("Cannot map ld.so (bss)\n");
}
crt.crt_dp = &_DYNAMIC;
@ -336,8 +336,8 @@ dlerror()
/*
* Support routines
*/
#ifdef DEBUG
#ifdef DEBUG
static int
_strncmp(s1, s2, n)
register char *s1, *s2;
@ -390,21 +390,6 @@ _getenv(name)
#endif /* DYNAMIC */
static char *
_strrchr(p, ch)
register char *p, ch;
{
register char *save;
for (save = NULL;; ++p) {
if (*p == ch)
save = (char *)p;
if (!*p)
return(save);
}
/* NOTREACHED */
}
#ifdef MCRT0
asm (" .text");
asm ("_eprol:");