Skip to content

Commit

Permalink
add uplt.o [#9]
Browse files Browse the repository at this point in the history
This is a user part of crt0 used for dynamic linking.
  • Loading branch information
stsp committed Oct 1, 2024
1 parent 3efed78 commit 8bd4dfa
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 21 deletions.
6 changes: 3 additions & 3 deletions dj64.mk
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ endif
endif

ifneq ($(AS_OBJECTS),)
XLDFLAGS = -melf_i386
XLDFLAGS = -melf_i386 -static
ifeq ($(DJ64STATIC),1)
XLDFLAGS += $(shell pkg-config --static --libs dj64static) -static
XLDFLAGS += $(shell pkg-config --static --libs dj64static)
DJ64_XLDFLAGS += -f 0x4000
else
XLDFLAGS += -shared -z notext
XLDFLAGS += $(shell pkg-config --variable=crt0 dj64)
endif
$(XELF): $(AS_OBJECTS) $(PLT_O)
$(XLD) $^ $(XLDFLAGS) -o $@
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
6 changes: 5 additions & 1 deletion include/libc/stubinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
#define STUBINFO_PAYLOAD2_NAME 0x74
#define STUBINFO_MEM_BASE 0x88
#define STUBINFO_FLAGS 0x8C
#define STUBINFO_END 0x90
#define STUBINFO_UENTRY_EIP 0x90
#define STUBINFO_UENTRY_CS 0x94
#define STUBINFO_END 0x98
#ifndef __ASSEMBLER__
#include <stdint.h>
typedef struct {
Expand Down Expand Up @@ -57,6 +59,8 @@ typedef struct {
char payload2_name[20];
uint32_t mem_base;
uint32_t flags;
uint32_t uentry_eip;
uint32_t uentry_cs;
} _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
2 changes: 2 additions & 0 deletions src/djdev64/stub/stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ int djstub_main(int argc, char *argv[], char *envp[], unsigned psp_sel,
handle = ops->read_headers(ifile);
if (!handle)
exit(EXIT_FAILURE);
stubinfo.uentry_cs = clnt_entry.selector;
stubinfo.uentry_eip = 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);
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
17 changes: 2 additions & 15 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,13 +74,7 @@ err_str: .asciz "dj64: load failure\r\n$"
ret
.endm

#include "plt.inc"

.global dj64_plt_call
dj64_plt_call:
movl $AUX_USER, %ebx
call_plt
ret
.include "plt.inc"

.global plt_init
plt_init:
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
19 changes: 19 additions & 0 deletions src/libc/dj64/uplt.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <libc/asmdefs.h>
.include "plt_defs.inc"

.bss

__plt_call: .quad 0
__plt_handle: .long 0

.text

.global _start
_start:
movl 4(%esp), %eax
movl %eax, __plt_call
movl 8(%esp), %eax
movl %eax, __plt_call + 4
movl 12(%esp), %eax
movl %eax, __plt_handle
ret

0 comments on commit 8bd4dfa

Please sign in to comment.