Skip to content

Dyn #10

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 2, 2024
Merged

Dyn #10

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
2 changes: 2 additions & 0 deletions debian/dj64-dev-static.install
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
usr/i386-pc-dj64/lib/libc.a
usr/i386-pc-dj64/lib64/*.a
usr/share/pkgconfig/dj64_s.pc
usr/share/pkgconfig/dj64static.pc
3 changes: 1 addition & 2 deletions debian/dj64-dev.install
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
usr/i386-pc-dj64/include
usr/i386-pc-dj64/lib/libc.a
usr/i386-pc-dj64/lib/*.o
usr/i386-pc-dj64/lib64/*.so
usr/i386-pc-dj64/bin/*
usr/i386-pc-dj64/share/*
usr/bin/*
usr/share/pkgconfig/dj64.pc
usr/share/pkgconfig/dj64static.pc
usr/share/man/man1/*
usr/share/doc/dj64/*
11 changes: 9 additions & 2 deletions dj64.mk
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,18 @@ endif
endif

ifneq ($(AS_OBJECTS),)
XLDFLAGS = $(shell pkg-config --static --libs dj64static) -melf_i386 -static
XLDFLAGS = -melf_i386 -static
ifeq ($(DJ64STATIC),1)
XLDFLAGS += $(shell pkg-config --static --libs dj64static)
DJ64_XLDFLAGS += -f 0x4000
else
XLDFLAGS += $(shell pkg-config --variable=crt0 dj64) \
--section-start=.note.gnu.property=0x08148000 -section-start=.text=0x08149000
endif
$(XELF): $(AS_OBJECTS) $(PLT_O)
$(XLD) $^ $(XLDFLAGS) -o $@
$(XSTRIP) $@
DJ64_XLDFLAGS += -l $(XELF) -f 0x4000
DJ64_XLDFLAGS += -l $(XELF)
else
ifeq ($(DJ64STATIC),1)
DJ64_XLDFLAGS += -l $(shell pkg-config --variable=crt0 dj64_s) -f 0x4000
Expand Down
1 change: 1 addition & 0 deletions dj64.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ Version: @VERSION@
prefix=@PREFIX@
makeinc=${prefix}/i386-pc-dj64/share/dj64.mk
cppflags=-I${prefix}/i386-pc-dj64/include -DDJ64
crt0=${prefix}/i386-pc-dj64/lib/uplt.o
Cflags: -fpic
Libs: -L${prefix}/i386-pc-dj64/lib64 -ldj64 -shared -Wl,-Bsymbolic
5 changes: 3 additions & 2 deletions dj64.spec.rpkg
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ This package contains tools and headers for building dj64-enabled programs.
%files dj64-devel
%defattr(-,root,root)
%{_prefix}/i386-pc-dj64/include
%{_prefix}/i386-pc-dj64/lib/libc.a
%{_prefix}/i386-pc-dj64/lib/*.o
%{_prefix}/i386-pc-dj64/lib64/*.so
%{_prefix}/i386-pc-dj64/bin/*
%{_prefix}/i386-pc-dj64/share/*
%{_bindir}/*
%{_datadir}/pkgconfig/dj64.pc
%{_datadir}/pkgconfig/dj64static.pc
%{_mandir}/man1/*
%{_docdir}/dj64
%end
Expand All @@ -100,8 +99,10 @@ May be needed on non-glibc systems.

%files dj64-devel-static
%defattr(-,root,root)
%{_prefix}/i386-pc-dj64/lib/libc.a
%{_prefix}/i386-pc-dj64/lib64/*.a
%{_datadir}/pkgconfig/dj64_s.pc
%{_datadir}/pkgconfig/dj64static.pc
%end

%package djdev64
Expand Down
4 changes: 3 additions & 1 deletion include/libc/stubinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
#define STUBINFO_PAYLOAD2_NAME 0x74
#define STUBINFO_MEM_BASE 0x88
#define STUBINFO_FLAGS 0x8C
#define STUBINFO_END 0x90
#define STUBINFO_UENTRY 0x90
#define STUBINFO_END 0x94
#ifndef __ASSEMBLER__
#include <stdint.h>
typedef struct {
Expand Down Expand Up @@ -57,6 +58,7 @@ typedef struct {
char payload2_name[20];
uint32_t mem_base;
uint32_t flags;
uint32_t uentry;
} _GO32_StubInfo;

_Static_assert(sizeof(_GO32_StubInfo) == STUBINFO_END, "size mismatch");
Expand Down
2 changes: 2 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ INSTALL ?= install
VERSION = 0.1
DJLIBC = $(TOP)/lib/libc.a
DJCRT0 = $(TOP)/lib/crt0.elf
DJUCRT0 = $(TOP)/lib/uplt.o
DJ64LIB = $(TOP)/lib/libdj64.so.*.*
DJ64DEVL = $(TOP)/lib/libdj64.so
DJ64LIBS = $(TOP)/lib/libdj64_s.a
Expand Down Expand Up @@ -66,6 +67,7 @@ install_dj64:
$(INSTALL) -d $(DESTDIR)$(PREFIX)/i386-pc-dj64/lib
$(INSTALL) -m 0644 $(DJLIBC) $(DESTDIR)$(PREFIX)/i386-pc-dj64/lib
$(INSTALL) -m 0644 $(DJCRT0) $(DESTDIR)$(PREFIX)/i386-pc-dj64/lib
$(INSTALL) -m 0644 $(DJUCRT0) $(DESTDIR)$(PREFIX)/i386-pc-dj64/lib
$(INSTALL) -d $(DESTDIR)$(PREFIX)/i386-pc-dj64/lib64
$(INSTALL) $(DJ64LIB) $(DESTDIR)$(PREFIX)/i386-pc-dj64/lib64
cp -fP $(DJ64DEVL) $(DESTDIR)$(PREFIX)/i386-pc-dj64/lib64
Expand Down
47 changes: 41 additions & 6 deletions src/djdev64/stub/stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
#define STFLG1_NO32PL 0x80 // no 32bit payload
#define STFLG2_C32PL 0x40 // have core 32bit payload

#define MB (1024 * 1024)
#define VA_SZ (2*MB)

typedef struct
{
uint32_t offset32;
Expand Down Expand Up @@ -162,6 +165,7 @@ int djstub_main(int argc, char *argv[], char *envp[], unsigned psp_sel,
char buf[BUF_SIZE];
int done = 0;
int dyn = 0;
int pl32 = 0;
uint32_t va;
uint32_t va_size;
uint32_t mem_lin;
Expand Down Expand Up @@ -217,17 +221,21 @@ int djstub_main(int argc, char *argv[], char *envp[], unsigned psp_sel,

if (!(buf[FLG2_OFF] & STFLG2_C32PL)) {
dyn++;
noffset = offs;
moff = 4;
pfile = open(CRT0, O_RDONLY | O_CLOEXEC);
ops = &elf_ops;
} else {
pfile = ifile;
}
if (buf[FLG1_OFF] & STFLG1_NO32PL) {
noffset = offs;
moff = 4;
done = 1;
} else {
pl32++;
coffset = offs;
memcpy(&coffsize, &buf[0x1c], sizeof(coffsize));
if (coffsize)
noffset = coffset + coffsize;
pfile = ifile;
}
memcpy(&nsize, &buf[0x20 - moff], sizeof(nsize));
if (nsize)
Expand Down Expand Up @@ -303,7 +311,13 @@ int djstub_main(int argc, char *argv[], char *envp[], unsigned psp_sel,
clnt_entry.offset32 = ops->get_entry(handle);
stub_debug("va 0x%x va_size 0x%x entry 0x%x\n",
va, va_size, clnt_entry.offset32);
stubinfo.initial_size = max(va_size, 0x10000);
if (va_size > MB)
exit(EXIT_FAILURE);
/* if we load 2 payloads, use larger estimate */
if (dyn && pl32)
stubinfo.initial_size = VA_SZ;
else
stubinfo.initial_size = max(va_size, 0x10000);
info.size = PAGE_ALIGN(stubinfo.initial_size);
/* allocate mem */
__dpmi_allocate_memory(&info);
Expand All @@ -312,11 +326,32 @@ int djstub_main(int argc, char *argv[], char *envp[], unsigned psp_sel,
mem_base = mem_lin - va;
stubinfo.mem_base = mem_base;
stub_debug("mem_lin 0x%x mem_base 0x%x\n", mem_lin, mem_base);
ops->read_sections(handle, lin2ptr(mem_base), pfile, coffset);
ops->read_sections(handle, lin2ptr(mem_base), pfile, dyn ? 0 : coffset);
ops->close(handle);
if (dyn)
close(pfile);
unregister_dosops();
if (dyn && pl32) {
uint32_t va2;
uint32_t va_size2;

/* dyn loaded, now pl32 */
register_dosops(dosops);
handle = ops->read_headers(ifile);
if (!handle)
exit(EXIT_FAILURE);
stubinfo.uentry = ops->get_entry(handle);
va2 = ops->get_va(handle);
va_size2 = ops->get_length(handle);
stub_debug("va 0x%x va_size 0x%x\n", va2, va_size2);
if (va_size2 > MB)
exit(EXIT_FAILURE);
if (va2 < va + va_size || va2 + va_size2 - va > VA_SZ)
exit(EXIT_FAILURE);
ops->read_sections(handle, lin2ptr(mem_base), ifile, coffset);
ops->close(handle);
unregister_dosops();
}

/* set base */
__dpmi_set_segment_base_address(clnt_entry.selector, mem_base);
Expand Down Expand Up @@ -353,7 +388,7 @@ int djstub_main(int argc, char *argv[], char *envp[], unsigned psp_sel,
dosops->_dos_seek(ifile, noffset, SEEK_SET);
if (nsize > 0)
stub_debug("Found payload of size %i at 0x%x\n", nsize, noffset);
stubinfo.stubinfo_ver |= 3;
stubinfo.stubinfo_ver |= 4;

unregister_dpmiops();

Expand Down
9 changes: 7 additions & 2 deletions src/libc/dj64/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ thunks_a.o: asm_incs.h plt_asmc.h asym_incs.h
thunks_c.o: thunk_incs.h thunk_calls.h
thunks_p.o: thunk_incs.h thunk_asms.h

plt.o: plt.S plt.inc
plt.o: plt.S plt.inc plt_defs.inc
uplt.o: uplt.S plt_defs.inc

ALL_OBJS = $(addprefix ../,$(file < $(TOP)/makefile.rf))
$(LIB)/$(LIBN): $(TOP)/makefile.rf $(ALL_OBJS) \
Expand All @@ -82,10 +83,14 @@ $(LIB)/crt0.elf: $(LIB)/libc.a
$(CROSS_ASSTRIP) --strip-debug $@
chmod -x $@

$(LIB)/uplt.o: uplt.o
cp $< $@

$(LIB)/libdj64.so: $(LIB)/$(LIBN) | $(LIB)
ln -sf $(LIBN) $@

final: $(LIB)/libc.a $(LIB)/libdj64.so $(LIB)/$(LIBS) $(LIB)/crt0.elf | $(LIB)
final: $(LIB)/libc.a $(LIB)/libdj64.so $(LIB)/$(LIBS) $(LIB)/crt0.elf \
$(LIB)/uplt.o | $(LIB)

clean::
$(RM) rm *.a *.tmp *.inc asm_*.h thunk_*.h plt_*.h asym_incs.h
Expand Down
28 changes: 14 additions & 14 deletions src/libc/dj64/plt.S
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <libc/asmdefs.h>
#include "stubinfo.h"
#include "plt.h"
.include "plt_defs.inc"

.bss

Expand All @@ -29,8 +30,6 @@ __plt_call: .quad 0
__plt_ctrl: .quad 0
.global __plt_handle
__plt_handle: .long 0
AUX_CORE = 0
AUX_USER = 1

sel: .word 0
SHM_REQ_LEN = 0
Expand Down Expand Up @@ -60,12 +59,6 @@ err_str: .asciz "dj64: load failure\r\n$"

.text

.macro call_plt
movl __plt_handle, %eax
movl $0, %esi // reserved
lcalll *__plt_call
.endm

.macro asmcfunc_n nm,num
.global _\nm
_\nm:
Expand All @@ -81,12 +74,14 @@ err_str: .asciz "dj64: load failure\r\n$"
ret
.endm

#include "plt.inc"
.include "plt.inc"

.global dj64_plt_call
dj64_plt_call:
movl $AUX_USER, %ebx
call_plt
uplt_init:
pushl __plt_handle
pushl __plt_call + 4
pushl __plt_call
call *%fs:STUBINFO_UENTRY
addl $12, %esp
ret

.global plt_init
Expand Down Expand Up @@ -190,7 +185,11 @@ plt_init:
movl %edi, __plt_call
movl %eax, __plt_ctrl + 4
movl %esi, __plt_ctrl

movl %fs:STUBINFO_FLAGS, %ecx
testw $0x4080, %cx
jnz 111f
call uplt_init
111:
movl %fs:STUBINFO_SELF_SIZE, %ecx
orl %ecx, %ecx
jz 3f
Expand Down Expand Up @@ -245,6 +244,7 @@ plt_init:
movl $1, %eax
int $0x31
/* call manager */
movl %fs:STUBINFO_FLAGS, %eax
movl maddr, %ebx
movl %fs:STUBINFO_SELF_SIZE, %ecx
movl %fs:STUBINFO_MEM_BASE, %edx
Expand Down
16 changes: 16 additions & 0 deletions src/libc/dj64/plt_defs.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
AUX_CORE = 0
AUX_USER = 1

.macro call_plt
movl __plt_handle, %eax
movl $0, %esi # reserved
lcalll *__plt_call
.endm

.text

.global dj64_plt_call
dj64_plt_call:
movl $AUX_USER, %ebx
call_plt
ret
50 changes: 32 additions & 18 deletions src/libc/dj64/thunks.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ static int dj64_ctrl(int handle, int libid, int fn, unsigned esi, uint8_t *sp)
switch (fn) {
case DL_SET_SYMTAB: {
struct udisp *u = &udisps[handle];
uint32_t flags = regs->eax;
int have_core = (flags & 0x4000);
uint32_t addr = regs->ebx;
uint32_t size = regs->ecx;
uint32_t mem_base = regs->edx;
Expand All @@ -271,34 +273,46 @@ static int dj64_ctrl(int handle, int libid, int fn, unsigned esi, uint8_t *sp)
char *elf = (char *)djaddr2ptr(addr);
djlogprintf("data %p(%s)\n", elf, elf);
eh = u->eops->open(elf, size);
} else {
eh = u->eops->open_dyn();
if (!eh)
return -1;
if (have_core) {
ret = process_athunks(&u->core_at, mem_base, u->eops, eh);
if (ret)
goto err;
ret = process_pthunks(&u->core_pt, u->eops, eh);
if (ret)
goto err;
}
if (u->at) {
ret = process_athunks(u->at, mem_base, u->eops, eh);
if (ret)
goto err;
}
if (u->pt) {
ret = process_pthunks(u->pt, u->eops, eh);
if (ret)
goto err;
}
u->eops->close(eh);
}
if (!eh)
return -1;
ret = process_athunks(&u->core_at, mem_base, u->eops, eh);
if (ret)
goto err;
if (u->at) {
ret = process_athunks(u->at, mem_base, u->eops, eh);
if (!have_core) {
eh = u->eops->open_dyn();
if (!eh)
return -1;
ret = process_athunks(&u->core_at, mem_base, u->eops, eh);
if (ret)
goto err;
}
ret = process_pthunks(&u->core_pt, u->eops, eh);
if (ret)
goto err;
if (u->pt) {
ret = process_pthunks(u->pt, u->eops, eh);
ret = process_pthunks(&u->core_pt, u->eops, eh);
if (ret)
goto err;
u->eops->close(eh);
}
u->eops->close(eh);

do_early_init(handle);
return ret;
return 0;
err:
u->eops->close(eh);
return ret;
break;
}
}
return -1;
Expand Down
Loading