Skip to content

Commit

Permalink
separate out the loader [#146]
Browse files Browse the repository at this point in the history
This splits the build system even more.
  • Loading branch information
stsp committed Nov 23, 2023
1 parent c46818a commit 44c4947
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 160 deletions.
19 changes: 7 additions & 12 deletions fdpp/clang.mak
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,19 @@
LD ?= ld
CROSS_LD ?= x86_64-linux-gnu-ld
CCACHE ?= $(shell which ccache 2>/dev/null)
CC = $(CCACHE) clang++
CC = $(CCACHE) clang
CXX = $(CCACHE) clang++
CLANG_VER := $(shell $(CC) -v 2>&1 | head -n 1 | \
sed -E 's/.+ version ([^.]+)\.[^.]+\.[^ ]+.*/\1/')
FLEX = $(shell which flex 2>/dev/null)
ifneq ($(FLEX),)
LEX = $(FLEX)
endif

# Override builtin CXX.
# The assignment below is ignored if CXX was set via cmd line.
CXX=
ifeq ($(CXX),)
CL = $(CC)
else
CL = $(CXX)
endif
CC_FOR_BUILD = $(CCACHE) clang
CPP = $(CC_FOR_BUILD) -E
CC_LD = $(CL)
CC_LD = $(CXX)
CL = $(CC)
NASM ?= nasm-segelf
PKG_CONFIG ?= pkg-config

Expand Down Expand Up @@ -64,9 +58,10 @@ ifeq ($(USE_UBSAN),1)
DBGFLAGS += -fsanitize=undefined -fno-sanitize=alignment,function,vptr
endif

CFLAGS = $(TARGETOPT) $(CPPFLAGS) $(WFLAGS) $(DBGFLAGS) $(TARGETOPT_XTRA)
CXXFLAGS = $(TARGETOPT) $(CPPFLAGS) $(WFLAGS) $(DBGFLAGS) $(TARGETOPT_XTRA)
CFLAGS = -Wall $(DBGFLAGS)
CLCFLAGS = -c -fpic $(WCFLAGS) $(DBGFLAGS) -xc++
LDFLAGS = -shared -Wl,-Bsymbolic -Wl,--build-id=sha1
LDFLAGS = -shared -Wl,--build-id=sha1

ifeq ($(XFAT),32)
CPPFLAGS += -DWITHFAT32
Expand Down
1 change: 1 addition & 0 deletions fdpp/configure
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ if [ ! -f configure ]; then

do_sub thunk_gen
do_sub kernel
do_sub loader
fi
2 changes: 1 addition & 1 deletion fdpp/fdpp.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ fdpplibdir=${libdir}/fdpp
Name: fdpp
Description: 64-bit DOS core
Version: @VERSION@
Libs: -L${fdpplibdir} -lfdpp
Libs: -L${fdpplibdir} -lfdpp -lfdldr
File renamed without changes.
2 changes: 2 additions & 0 deletions fdpp/elf_priv.h → fdpp/loader/elf_priv.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <stdint.h>

void *elf_open(const char *name);
void elf_reloc(void *arg, uint16_t seg);
void elf_close(void *arg);
Expand Down
134 changes: 134 additions & 0 deletions fdpp/loader/loader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* FDPP - freedos port to modern C++
* Copyright (C) 2018-2023 Stas Sergeev (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/>.
*/

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "elf_priv.h"
#include "loader.h"

#define __S(x) #x
#define _S(x) __S(x)
const char *FdppKernelDir(void)
{
return _S(FDPPKRNLDIR);
}

const char *FdppKernelMapName(void)
{
return _S(KRNL_MAP_NAME);
}

struct krnl_hndl {
void *elf;
const void *start;
unsigned load_off;
};

void *FdppKernelLoad(const char *dname, int *len, struct fdpp_bss_list **bss,
uint32_t *_start)
{
struct fdpp_bss_list *bl;
void *start, *end;
void *bstart, *bend;
void *ibstart, *ibend;
char *kname;
void *handle;
struct krnl_hndl *h;
int i, s;

asprintf(&kname, "%s/%s", dname, _S(KRNL_ELFNAME));
handle = elf_open(kname);
if (!handle) {
fprintf(stderr, "failed to open %s\n", kname);
free(kname);
return NULL;
}
free(kname);
start = elf_getsym(handle, "_start");
s = elf_getsymoff(handle, "_start");
if (s == -1)
goto err_close;
*_start = s;
end = elf_getsym(handle, "__InitTextEnd");
if (!end)
goto err_close;
bstart = elf_getsym(handle, "__bss_start");
if (!bstart)
goto err_close;
bend = elf_getsym(handle, "__bss_end");
if (!bend)
goto err_close;
ibstart = elf_getsym(handle, "__ibss_start");
if (!ibstart)
goto err_close;
ibend = elf_getsym(handle, "__ibss_end");
if (!ibend)
goto err_close;
*len = (uintptr_t)end - (uintptr_t)start;

bl = (struct fdpp_bss_list *)malloc(sizeof(*bl) +
sizeof(struct fdpp_bss_ent) * 2);
i = 0;
if (bend != bstart) {
bl->ent[i].off = (uintptr_t)bstart - (uintptr_t)start;
bl->ent[i].len = (uintptr_t)bend - (uintptr_t)bstart;
i++;
}
if (ibend != ibstart) {
bl->ent[i].off = (uintptr_t)ibstart - (uintptr_t)start;
bl->ent[i].len = (uintptr_t)ibend - (uintptr_t)ibstart;
i++;
}
bl->num = i;
*bss = bl;

h = (struct krnl_hndl *)malloc(sizeof(*h));
h->elf = handle;
h->start = start;
h->load_off = s;
return h;

err_close:
elf_close(handle);
return NULL;
}

const void *FdppKernelReloc(void *handle, uint16_t seg, uint16_t *r_seg,
reloc_hook_t hook)
{
struct krnl_hndl *h = (struct krnl_hndl *)handle;

assert(!(h->load_off & 0xf));
seg -= h->load_off >> 4;
elf_reloc(h->elf, seg);

hook(seg, elf_getsymoff, h->elf);

*r_seg = seg;
return h->start;
}

void FdppKernelFree(void *handle)
{
struct krnl_hndl *h = (struct krnl_hndl *)handle;

elf_close(h->elf);
free(h);
}
43 changes: 43 additions & 0 deletions fdpp/loader/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
T = ..
-include config.mak
-include $(T)/local.mak
srcdir ?= $(CURDIR)

include $(srcdir)/$(T)/clang.mak
include $(srcdir)/$(T)/defs.mak

TOP = $(abspath $(srcdir)/$(T)/..)
HDR = $(TOP)/hdr
PPINC = $(TOP)/include/fdpp
AVER = $(shell echo FDPP_API_VER | cpp -include $(PPINC)/thunks.h - | tail -n 1)
BVER = $(shell echo BPRM_VER | cpp -include $(PPINC)/bprm.h - | tail -n 1)
TARGET=fdppkrnl.$(AVER).$(BVER)
FDPPDEVL = libfdldr.so
FDPPLIB = $(FDPPDEVL).$(AVER).$(BVER)
LDFLAGS += -Wl,-soname=$(FDPPLIB) -L..
SOURCES = elf.c loader.c
HEADERS = elf_priv.h
OBJECTS = $(SOURCES:.c=.o)

CFLAGS += -I . -I $(PPINC) \
-DFDPPKRNLDIR=$(DATADIR)/fdpp \
-DKRNL_ELFNAME=$(TARGET).elf -DKRNL_MAP_NAME=$(TARGET).map \
-fpic
LIBELF := $(shell $(PKG_CONFIG) --libs libelf)
ifeq ($(LIBELF),)
$(error libelf not installed)
endif
LIBS += $(LIBELF)

ALL = $(T)/$(FDPPLIB)

all: $(ALL)

$(T)/$(FDPPLIB): $(OBJECTS)
$(CL) -o $@ $^ $(LDFLAGS) $(LIBS)

$(OBJECTS): %.o: $(srcdir)/%.c $(addprefix $(srcdir)/,$(HEADERS)) $(srcdir)/makefile
$(CC) $(CFLAGS) -c -o $@ $<

clean:
$(RM) *.o
39 changes: 22 additions & 17 deletions fdpp/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ AVER = $(shell echo FDPP_API_VER | cpp -include $(PPINC)/thunks.h - | tail -n 1)
BVER = $(shell echo BPRM_VER | cpp -include $(PPINC)/bprm.h - | tail -n 1)
FDPPDEVL = libfdpp.so
FDPPLIB = $(FDPPDEVL).$(AVER).$(BVER)
LDFLAGS += -Wl,-soname=$(FDPPLIB)
LDRDEVL = libfdldr.so
LDRLIB = $(LDRDEVL).$(AVER).$(BVER)
LDFLAGS += -Wl,-soname=$(FDPPLIB) -Wl,-Bsymbolic
TARGET=fdppkrnl.$(AVER).$(BVER)
RELVER = 7
MAJ = 1
Expand All @@ -27,21 +29,21 @@ PKG = fdpp-$(VERSION)
TAR = $(PKG).tar
TGZ = $(TAR).gz
FD_EXT_H = $(TOP)/include/fdpp
EXT_H = $(FD_EXT_H)/thunks.h $(FD_EXT_H)/bprm.h $(FD_EXT_H)/memtype.h
EXT_H = $(FD_EXT_H)/thunks.h $(FD_EXT_H)/bprm.h $(FD_EXT_H)/memtype.h \
$(FD_EXT_H)/loader.h
GEN_EXT = fdpp.pc
TFLAGS = -a 2 -p 2

GIT_REV := $(shell $(srcdir)/git-rev.sh)
GIT_DESCRIBE := $(shell git describe)
.LOW_RESOLUTION_TIME: $(GIT_REV)

CPPFLAGS += -I . -I $(FD_EXT_H) -I $(SRC) -I $(srcdir) \
-DFDPPKRNLDIR=$(DATADIR)/fdpp \
-DKRNL_ELFNAME=$(TARGET).elf -DKRNL_MAP_NAME=$(TARGET).map
CPPFLAGS += -I . -I $(FD_EXT_H) -I $(SRC) -I $(srcdir)

# *Explicit Rules*

ALL = $(FDPPLIB) $(FDPPDEVL) $(TARGET).elf $(TARGET).map $(GEN_EXT)
ALL = $(FDPPLIB) $(FDPPDEVL) $(LDRLIB) $(LDRDEVL) $(TARGET).elf $(TARGET).map \
$(GEN_EXT)

all: $(ALL)

Expand All @@ -58,12 +60,16 @@ else
endif
$(MAKE) -C kernel

$(LDRLIB):
$(MAKE) -C loader

clean:
$(MAKE) -C kernel clean
$(MAKE) -C loader clean
+cd thunk_gen && $(MAKE) srcdir=$(abspath $(srcdir))/thunk_gen clean
-$(RM) .tstamp *.map $(TARGET).elf *.inc \
*.o $(GEN_CC) $(FDPPDEVL) $(FDPPLIB) *.tmp $(GEN_HEADERS) \
$(GEN_HEADERS_FD) $(GEN_ASMS) \
$(LDRDEVL) $(LDRLIB) $(GEN_HEADERS_FD) \
*.pc *.so *.elf

HDRS = $(wildcard $(HDR)/*.h) $(wildcard $(SRC)/*.h)
Expand All @@ -73,7 +79,6 @@ _PPHDRS = $(PLPHDRS) dosobj.h farhlp.hpp thunks_priv.h smalloc.h \
PPHDRS = $(addprefix $(srcdir)/,$(_PPHDRS))
GEN_HEADERS = thunk_calls.h thunk_asms.h plt_asmc.h plt_asmp.h
GEN_HEADERS_FD = glob_asmdefs.h
GEN_ASMS = plt.asm cdata.asm
# dont change file order in GEN_TMP as it matches the gen script
GEN_TMP = thunk_calls.tmp thunk_asms.tmp plt.inc plt_asmc.h plt_asmp.h
GEN_CC = $(CFILES:.c=.cc)
Expand Down Expand Up @@ -112,33 +117,28 @@ CFILES = blockio.c \
prf.c \
share.c

FDPP_CFILES = smalloc.c farhlp_sta.c elf.c
FDPP_CFILES = smalloc.c farhlp_sta.c
FDPP_CCFILES = thunks.cc thunks_c.cc thunks_a.cc thunks_p.cc dosobj.cc
CPPFILES = objhlp.cpp ctors.cpp farhlp.cpp objtrace.cpp
LIBELF := $(shell $(PKG_CONFIG) --libs libelf)
ifeq ($(LIBELF),)
$(error libelf not installed)
endif
LIBS += $(LIBELF)

OBJECTS = $(CFILES:.c=.o)
FDPP_COBJS = $(FDPP_CFILES:.c=.o)
FDPP_CCOBJS = $(FDPP_CCFILES:.cc=.o)
FDPP_CPPOBJS = $(CPPFILES:.cpp=.o)

$(OBJECTS): %.o: %.cc $(HEADERS) $(GEN_HEADERS_FD) $(EXT_H)
$(CC) $(CFLAGS) -o $@ $<
$(CC) $(CXXFLAGS) -o $@ $<

$(FDPP_COBJS): %.o: $(srcdir)/%.c $(PPHDRS) $(srcdir)/makefile
$(CC) $(CLCFLAGS) -I . -o $@ $<

thunks.o: $(EXT_H) $(GIT_REV)
thunks_a.o: $(SRC)/glob_asm.h $(srcdir)/glob_tmpl.h plt_asmc.h plt_asmp.h
$(FDPP_CCOBJS): %.o: $(srcdir)/%.cc $(GEN_HEADERS) $(PPHDRS) $(srcdir)/makefile
$(CC) $(CFLAGS) -o $@ $<
$(CC) $(CXXFLAGS) -o $@ $<

$(FDPP_CPPOBJS): %.o: $(srcdir)/%.cpp $(PPHDRS) $(srcdir)/makefile
$(CC) $(CFLAGS) -o $@ $<
$(CC) $(CXXFLAGS) -o $@ $<

$(GEN_CC): %.cc: $(SRC)/%.c makefile
$(srcdir)/parsers/mkfar.sh $< >$@
Expand All @@ -150,6 +150,9 @@ $(FDPPLIB): $(OBJECTS) $(FDPP_COBJS) $(FDPP_CCOBJS) $(FDPP_CPPOBJS)
$(FDPPDEVL): $(FDPPLIB)
ln -sf $< $@

$(LDRDEVL): $(LDRLIB)
ln -sf $< $@

_pos = $(if $(findstring $1,$2),$(call _pos,$1,\
$(wordlist 2,$(words $2),$2),x $3),$3)
pos = $(words $(call _pos,$1,$2))
Expand Down Expand Up @@ -183,6 +186,8 @@ install: $(ALL)
$(INSTALL) -d $(DESTDIR)$(LIBDIR)/fdpp
$(INSTALL) $(FDPPLIB) $(DESTDIR)$(LIBDIR)/fdpp
cp -fP $(FDPPDEVL) $(DESTDIR)$(LIBDIR)/fdpp
$(INSTALL) $(LDRLIB) $(DESTDIR)$(LIBDIR)/fdpp
cp -fP $(LDRDEVL) $(DESTDIR)$(LIBDIR)/fdpp
$(INSTALL) -d $(DESTDIR)$(DATADIR)/fdpp
$(INSTALL) -m 0644 $(TARGET).elf $(DESTDIR)$(DATADIR)/fdpp
$(INSTALL) -m 0644 $(TARGET).map $(DESTDIR)$(DATADIR)/fdpp
Expand Down
Loading

0 comments on commit 44c4947

Please sign in to comment.