Skip to content

Exec #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 21, 2024
Merged

Exec #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/go64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef GO64_H
#define GO64_H

void pltcall32(__dpmi_regs *regs, __dpmi_paddr addr);
int elfexec(const char *path, int argc, char **argv);

#endif
2 changes: 2 additions & 0 deletions include/libc/atexit.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct __atexit {

extern struct __atexit *__atexit_ptr;

int atexit2(void (*function)(void *, int), void *arg);

#endif /* !_POSIX_SOURCE */
#endif /* !__STRICT_ANSI__ */
#endif /* !__dj_ENFORCE_ANSI_FREESTANDING */
Expand Down
1 change: 1 addition & 0 deletions include/libc/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void _npxsetup(char *argv0);
void __emu387_exception_handler(void);
void __djgpp_exception_processor(void);
void __djgpp_exception_setup(void);
void __djgpp_signal_setup(void);
void __register_frame_info(const void *begin, const void *object);
void __deregister_frame_info(const void *begin);
void _crt0_init_mcount(void); /* For profiling */
Expand Down
76 changes: 76 additions & 0 deletions src/djdev64/djexec.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* dj64 - 64bit djgpp-compatible tool-chain
* Copyright (C) 2021-2024 @stsp
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <setjmp.h>
#include <dlfcn.h>
#include "djdev64/djdev64.h"

struct exec_info {
/* volatile because of longjmp() vs auto storage */
volatile unsigned short exit_code;
jmp_buf exit_jmp;
};

static void exit_exec(void *handle, int rc);

int djdev64_exec(const char *path, unsigned flags, int argc, char **argv)
{
void *dlobj;
int (*m)(int, char **);
int (*ae2)(void(*)(void*,int),void*);
struct exec_info ei;
int ret = -1, rc;

dlobj = dlopen(path, RTLD_LOCAL | RTLD_NOW | RTLD_DEEPBIND);
if (!dlobj) {
printf("error loading %s: %s\n", path, dlerror());
return -1;
}
m = dlsym(dlobj, "main");
if (!m) {
printf("error: can't find \"main\"\n");
goto out;
}
ae2 = dlsym(dlobj, "atexit2");
if (!ae2) {
printf("error: can't find \"atexit2\"\n");
goto out;
}

rc = ae2(exit_exec, &ei);
if (rc == -1) {
printf("error: atexit2() failed\n");
goto out;
}
if (setjmp(ei.exit_jmp))
ret = ei.exit_code;
else
ret = (unsigned short)m(argc, argv);
ae2(NULL, NULL);
out:
dlclose(dlobj);
return ret;
}

static void exit_exec(void *handle, int rc)
{
struct exec_info *ei = handle;
ei->exit_code = rc;
longjmp(ei->exit_jmp, 1);
}
2 changes: 1 addition & 1 deletion src/djdev64/include/djdev64/dj64init.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

typedef int (dj64cdispatch_t)(int handle, int libid, int fn, unsigned esi,
uint8_t *sp);
#define DJ64_API_VER 11
#define DJ64_API_VER 12
enum { DJ64_PRINT_LOG, DJ64_PRINT_TERMINAL, DJ64_PRINT_SCREEN };

/* pushal */
Expand Down
2 changes: 2 additions & 0 deletions src/djdev64/include/djdev64/djdev64.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ int djdev64_ctrl(int handle, int libid, int fn, unsigned esi,
unsigned char *sp);
void djdev64_close(int handle);

int djdev64_exec(const char *path, unsigned flags, int argc, char **argv);

#endif
2 changes: 1 addition & 1 deletion src/djdev64/makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TOP = ../..
PREFIX ?= /usr/local
PKG_CONFIG ?= pkg-config
SOURCES = djdev64.c elf.c
SOURCES = djdev64.c elf.c djexec.c
OBJECTS = $(SOURCES:.c=.o)
LIBELF := $(shell $(PKG_CONFIG) --silence-errors --libs libelf)
ifeq ($(LIBELF),)
Expand Down
18 changes: 18 additions & 0 deletions src/libc/ansi/stdlib/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct exit_state {
struct __atexit *__atexit_ptr;
void (*__FSEXT_exit_hook)(void);
void (*__fd_properties_cleanup_hook)(void);
void (*atexit2_hook)(void *, int);
void *atexit2_arg;
};

static struct exit_state *est;
Expand All @@ -50,6 +52,9 @@ static void est_post(void)
DJ64_DEFINE_SWAPPABLE_CONTEXT2(exit_state, est, ((struct exit_state){}),
est_pre(), est_post());

#define atexit2_hook est->atexit2_hook
#define atexit2_arg est->atexit2_arg

void
exit(int status)
{
Expand Down Expand Up @@ -85,5 +90,18 @@ exit(int status)

/* in case the program set it this way */
setmode(0, O_TEXT);

__djgpp_signal_setup();
if (atexit2_hook)
atexit2_hook(atexit2_arg, status);
_exit(status);
}

int atexit2(void (*function)(void *, int), void *arg)
{
if (atexit2_hook && function)
return -1;
atexit2_hook = function;
atexit2_arg = arg;
return 0;
}
125 changes: 125 additions & 0 deletions src/libc/compat/go64/elfexec.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* dj64 - 64bit djgpp-compatible tool-chain
* Copyright (C) 2021-2024 @stsp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <sys/segments.h>
#include <io.h>
#include <dpmi.h>
#include <crt0.h>
#include <go64.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
#define _PAGE_MASK (~(PAGE_SIZE-1))
/* to align the pointer to the (next) page boundary */
#define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&_PAGE_MASK)

// https://github.com/vonj/snippets.org/blob/master/strrpbrk.c
static char *strrpbrk(const char *szString, const char *szChars)
{
const char *p;
char *p0, *p1;

for (p = szChars, p0 = p1 = NULL; p && *p; ++p)
{
p1 = strrchr(szString, *p);
if (p1 && p1 > p0)
p0 = p1;
}
return p0;
}

int elfexec(const char *path, int argc, char **argv)
{
int err, fd, len, errn;
const char *p;
unsigned fname;
__dpmi_paddr api;
__dpmi_shminfo shmi;
__dpmi_meminfo dm;
__dpmi_regs regs;
int en_dis = !(_crt0_startup_flags & _CRT0_FLAG_NEARPTR);

err = __dpmi_get_vendor_specific_api_entry_point("DJ64", &api);
if (err) {
fprintf(stderr, "DJ64 support missing\n");
return -1;
}
memset(&regs, 0, sizeof(regs));
regs.d.ebx = 3; // get version
pltcall32(&regs, api);
if (regs.d.eax < 2) {
fprintf(stderr, "unsupported DJ64 version %i\n", regs.d.eax);
return -1;
}
fd = open(path, O_RDONLY | O_BINARY);
if (fd == -1) {
fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno));
return -1;
}
len = filelength(fd);
p = strrpbrk(path, "/\\");
if (!p)
p = path;
fname = malloc32(strlen(p) + 1);
strcpy(DATA_PTR(fname), p);
shmi.size_requested = len;
shmi.name_offset = fname;
shmi.name_selector = _my_ds();
err = __dpmi_allocate_shared_memory(&shmi);
free32(fname);
if (err) {
close(fd);
fprintf(stderr, "Can't allocate shmem for %s\n", p);
return -1;
}
if (en_dis)
__djgpp_nearptr_enable();
err = read(fd, djaddr2ptr(shmi.address), len);
errn = errno;
if (en_dis)
__djgpp_nearptr_disable();
close(fd);
if (err == -1) {
fprintf(stderr, "error reading %s: %s\n", path, strerror(errn));
return -1;
}
if (err != len) {
fprintf(stderr, "read returned %i, need %i\n", err, len);
return -1;
}
dm.handle = shmi.handle;
dm.address = shmi.address;
dm.size = shmi.size;
err = __dpmi_free_physical_address_mapping(&dm);
assert(!err);
memset(&regs, 0, sizeof(regs));
regs.d.ebx = 2; // exec
regs.x.di = shmi.handle & 0xffff;
regs.x.si = shmi.handle >> 16;
pltcall32(&regs, api);
__dpmi_free_shared_memory(shmi.handle);
return regs.d.eax;
}
7 changes: 7 additions & 0 deletions src/libc/compat/go64/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details
TOP=../..

AS_SRC += pltcall32.S
SRC += elfexec.c

include $(TOP)/../makefile.inc
20 changes: 20 additions & 0 deletions src/libc/compat/go64/pltcall32.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <libc/asmdefs.h>

FUNC(_pltcall32)
ENTER
pushal
movl ARG1, %ebx
.irpc i,76543210
pushl \i*4(%ebx)
.endr
movl %ebp, 8(%esp) // keep ebp untouched
popal
movl __plt_handle, %eax
lcalll *ARG2
pushal
movl ARG1, %ebx
.irpc i,01234567
popl \i*4(%ebx)
.endr
popal
LEAVE
13 changes: 11 additions & 2 deletions src/libc/go32/dpmiexcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,16 @@ __djgpp_set_sigquit_key(int new_key)
return set_signal_key(SIGQUIT, new_key);
}


void
__djgpp_signal_setup(void)
{
int i;

for (i = 0; i < SIGMAX; i++)
signal_list[i] = (SignalHandler)SIG_DFL;
}

void
__djgpp_exception_setup(void)
{
Expand All @@ -543,8 +553,7 @@ __djgpp_exception_setup(void)
__djgpp_set_sigquit_key(DEFAULT_SIGQUIT_98);
}

for (i = 0; i < SIGMAX; i++)
signal_list[i] = (SignalHandler)SIG_DFL;
__djgpp_signal_setup();

/* app_DS only used when converting HW interrupts to exceptions */
__djgpp_app_DS = _my_ds();
Expand Down