Skip to content
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

Port to FreeBSD #86

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
09a6d81
Ignore local gzip build
vrza Jun 4, 2022
65683f9
Shell script portability improvements
vrza Jun 4, 2022
731619f
Portable detection of ncurses linker lib flags
vrza Jun 4, 2022
8892e2a
More portable Makefile
vrza Jun 4, 2022
dbd71a8
$(RM) might not be set
vrza Jun 4, 2022
dc94021
Remove GNU-specific $(sort) and |
vrza Jun 4, 2022
0db1e6f
Helper shell script to install keymaps
vrza Jun 4, 2022
98e7eac
More Makefile compatibility
vrza Jun 4, 2022
d85e398
Use more portable strndup instad of strndupa
vrza Jun 4, 2022
91a047f
Use malloc instead of GNU-specific alloca
vrza Jun 4, 2022
3ce58b8
Remove non-portable sighandler_t
vrza Jun 5, 2022
8a040b3
Better ncurses linker flags detection
vrza Jun 5, 2022
296be70
Use malloc instead of alloca
vrza Jun 5, 2022
be48ef6
More portable install(1) invocation
vrza Jun 5, 2022
f5e373d
More portable install(1) invocation
vrza Jun 5, 2022
a075613
Fix missing include
vrza Jun 5, 2022
d8914be
Consistent install invocation
vrza Jun 5, 2022
8683a84
Include malloc through stdlib.h
vrza Jun 7, 2022
2ade82f
Build GNU as with binutils
vrza Jun 8, 2022
da75cce
Drop non-portable nproc
vrza Jun 8, 2022
e62f142
Add locally built as to gitignore
vrza Jun 8, 2022
5d88b62
Port to FreeBSD
vrza Jun 8, 2022
98c3ace
Find GNU make in PATH
vrza Jun 8, 2022
0777099
Remove commented out code
vrza Jun 8, 2022
1404255
Fix install(1) invocation
vrza Jun 8, 2022
0d1ee00
Merge branch 'main' into openbsd-1
vrza Jun 8, 2022
1faceb1
Fix typo
vrza Jun 8, 2022
3fcb192
Undefine init_showme on FreeBSD
vrza Jun 8, 2022
2116cb3
Shell script to detect OS-specific symbol list file
vrza Jun 8, 2022
76e1872
Use kebab case for script name
vrza Jun 8, 2022
2c8f2d4
Fix build on NetBSD
vrza Jun 8, 2022
aac3764
Localize fwrite on Linux
vrza Jun 8, 2022
8c8a21e
Symbol undefine list for OpenBSD
vrza Jun 9, 2022
067bea0
Add environ to OpenBSD undefine list
vrza Jun 9, 2022
522c490
Workaround for OpenBSD
vrza Jun 10, 2022
a44ff9c
Revert some malloc changes
vrza Jun 10, 2022
6b9e561
Rebuild list undefine.openbsd
vrza Jun 10, 2022
546186d
Quote command substitution
vrza Jun 12, 2022
587d6bd
Merge updates from main
vrza Jun 12, 2022
0e0e810
Merge branch 'main' into openbsd-1
vrza Jun 12, 2022
5a0db70
Include alloca from stdlib.h for portability
vrza Jun 13, 2022
e644cf5
Don't use non-portable -v
vrza Jun 17, 2022
9d413c8
Don't use non-portable -v
vrza Jun 17, 2022
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
11 changes: 8 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
orig
*.IMG
real-ld
objcopy
objdump
*.o
*.sw?
coffsyrup
123
ld
binutils-*
as
objcopy
objdump
ld
gzip-*
gzip
gunzip
zcat
drawtest
*.a
keymap/keymap
Expand Down
112 changes: 65 additions & 47 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,74 +1,79 @@
platform != uname -s
undefine != ./detect-os-symbol-list-file.sh undefine
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems okay, but maybe we can just do undefine undefine-$(shell uname).lst? I dunno if that is portable though!

Copy link
Contributor Author

@vrza vrza Jun 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK syntax-wise that should be portable between GNU and BSD make , but on systems for which you don't have an explicit list file, the build will error out with file not found. The shell script provides a default if it doesn't have explicit rules about the host system.

redefine != ./detect-os-symbol-list-file.sh redefine
localize != ./detect-os-symbol-list-file.sh localize
globalize != ./detect-os-symbol-list-file.sh globalize
OBJCOPY_FLAGS = --wildcard --localize-symbols=$(localize) --globalize-symbols=$(globalize) --redefine-syms=$(redefine)
OBJCOPY_FILES = $(localize) $(globalize) $(redefine) $(undefine)
OBJCOPY != if [ -x ./objcopy ]; then echo ./objcopy; else echo objcopy; fi
AS != if [ -x ./as ]; then echo ./as; else echo as; fi
BFD_INP_TARGET = coff-i386
BFD_OUT_TARGET = coff-i386
OBJCOPY_FLAGS = --wildcard --localize-symbols=localize.lst --globalize-symbols=globalize.lst --redefine-syms=redefine.lst
OBJCOPY_FILES = localize.lst globalize.lst redefine.lst undefine.lst
CFLAGS = -W -Wall -m32 -ggdb3 -O0 -fno-stack-protector
CPPFLAGS = -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64 -D_GNU_SOURCE -I ttydraw -Wno-unused-parameter
ASFLAGS = --32
LDFLAGS = $(CFLAGS) -lc -B. -Wl,-b,$(BFD_OUT_TARGET) -no-pie
LDLIBS = -lncurses -ltinfo -lm
PATH := .:$(PATH)
prefix = /usr/local

LINUX_LDFLAGS = -lc
GENERIC_LDFLAGS = $(CFLAGS) -B. -Wl,-b,$(BFD_OUT_TARGET) -no-pie
LDFLAGS != if [ "$(platform)" = "Linux" ]; then echo $(LINUX_LDFLAGS) $(GENERIC_LDFLAGS); else echo $(GENERIC_LDFLAGS); fi
NCURSES_LIBS != ./ncurses-config.sh
LDLIBS = $(NCURSES_LIBS) -lm
OBJECT_FILES = 123.o dl_init.o main.o wrappers.o patch.o filemap.o filename.o graphics.o draw.o ttydraw/ttydraw.a atfuncs/atfuncs.a forceplt.o
# The list of terminals we generate keymaps for by default.
KEYMAPS = xterm rxvt-unicode
KEYMAPS := $(KEYMAPS) $(patsubst %,%-256color,$(KEYMAPS))
KEYMAPS += screen.xterm-256color vt100 vt320 $(TERM)

define BFD_TARGET_ERROR
Your version of binutils was compiled without coff-i386 target support.
You can try running ./binutils.sh to build a version that does support it.
endef

export BFD_TARGET_ERROR
KEYMAPS = xterm rxvt-unicode xterm-256color rxvt-unicode-256color screen screen.xterm-256color vt100 vt320 $(TERM)
prefix = /usr/local

.PHONY: clean check distclean install uninstall

all: check 123 keymaps
@size 123

check:
@objdump --info | egrep -q '^coff-i386$$' || (echo "$$BFD_TARGET_ERROR"; false)
./check-binutils-coff.sh

coffsyrup:
$(CC) -o $@ coffsyrup.c

orig/123.o:
@echo You need to run the extract.sh script to get the 1-2-3 files.
@false

123.o: orig/123.o $(OBJCOPY_FILES) | coffsyrup
objcopy -I $(BFD_INP_TARGET) -O $(BFD_OUT_TARGET) $(OBJCOPY_FLAGS) $< $@
coffsyrup $@ $(@:.o=.tmp.o) $$(cat undefine.lst)
123.o: coffsyrup orig/123.o $(OBJCOPY_FILES)
$(OBJCOPY) -I $(BFD_INP_TARGET) -O $(BFD_OUT_TARGET) $(OBJCOPY_FLAGS) orig/123.o $@
./coffsyrup $@ $(@:.o=.tmp.o) $$(cat $(undefine))
mv $(@:.o=.tmp.o) $@

dl_init.o: orig/dl_init.o
objcopy -I $(BFD_INP_TARGET) -O $(BFD_OUT_TARGET) $(OBJCOPY_FLAGS) $< $@
$(OBJCOPY) -I $(BFD_INP_TARGET) -O $(BFD_OUT_TARGET) $(OBJCOPY_FLAGS) orig/dl_init.o $@

forceplt.o: forceplt.s
$(AS) $(ASFLAGS) -o $@ forceplt.s

ttydraw/ttydraw.a:
$(MAKE) -C ttydraw

atfuncs/atfuncs.a:
$(MAKE) -C atfuncs

bin/123: 123.o dl_init.o main.o wrappers.o patch.o filemap.o graphics.o draw.o filename.o | ttydraw/ttydraw.a atfuncs/atfuncs.a forceplt.o
bin/123: $(OBJECT_FILES)
@mkdir -p $(@D)
$(CC) forceplt.o $(CFLAGS) $(LDFLAGS) $^ -Wl,--whole-archive,ttydraw/ttydraw.a,atfuncs/atfuncs.a,--no-whole-archive -o $@ $(LDLIBS)
$(CC) forceplt.o $(CFLAGS) $(LDFLAGS) $(OBJECT_FILES) -Wl,--whole-archive,ttydraw/ttydraw.a,atfuncs/atfuncs.a,--no-whole-archive -o $@ $(LDLIBS)

123: bin/123
@ln -fs $^ $@
@ln -fs bin/123 $@

keymap/keymap:
$(MAKE) -C keymap

# This generates the keymaps in a seperate directory based on the first letter.
$(sort $(KEYMAPS)): keymap/keymap
mkdir -p share/lotus/keymaps/$(shell printf "%c" $@)
keymap/keymap $@ > share/lotus/keymaps/$(shell printf "%c" $@)/$@
$(KEYMAPS): keymap/keymap
./install-keymap.sh $@

keymaps: $(KEYMAPS)

clean:
$(RM) *.o coffsyrup 123
$(RM) vgcore.* core.* core
$(RM) -r bin share/lotus/keymaps
rm -f *.o coffsyrup 123
rm -f vgcore.* core.* core
rm -rf bin share/lotus/keymaps
$(MAKE) -C ttydraw clean
$(MAKE) -C atfuncs clean
$(MAKE) -C keymap clean
Expand All @@ -79,21 +84,34 @@ distclean: clean
./extract.sh clean

install: all
install -Dm 755 "bin/123" "$(prefix)/bin/123"
install -Dm 644 "share/man/man1/123.1" "$(prefix)/share/man/man1/123.1"
install -Dm 644 "share/lotus/etc/l123set.cf" "$(prefix)/share/lotus/etc/l123set.cf"
find "share/lotus/keymaps" -type f -exec install -Dm 644 {} "$(prefix)/{}" \;
install -Dm 644 "share/lotus/123.v10/sysV386/lotus.bcf" "$(prefix)/share/lotus/123.v10/sysV386/lotus.bcf"
install -Dm 644 "share/lotus/123.v10/sysV386/lib/wyse50-lts123" "$(prefix)/share/lotus/123.v10/sysV386/lib/wyse50-lts123"
find "share/lotus/123.v10/cbd" -type f -exec install -Dm 644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/fonts" -type f -exec install -Dm 644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/hlp" -type f -exec install -Dm 644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/keymaps" -type f -exec install -Dm 644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/pbd" -type f -exec install -Dm 644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/ri" -type f -exec install -Dm 644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/smpfiles" -type f -exec install -Dm 644 {} "$(prefix)/{}" \;
install -m 0755 -d "$(prefix)/bin"
install -m 0755 "bin/123" "$(prefix)/bin/123"
install -m 0755 -d "$(prefix)/share/man/man1"
install -m 0644 "share/man/man1/123.1" "$(prefix)/share/man/man1/123.1"
install -m 0755 -d "$(prefix)/share/lotus/etc"
install -m 0644 "share/lotus/etc/l123set.cf" "$(prefix)/share/lotus/etc/l123set.cf"
find "share/lotus/keymaps" -type d -exec install -m 0755 -d "$(prefix)/{}" \;
find "share/lotus/keymaps" -type f -exec install -m 0644 {} "$(prefix)/{}" \;
install -m 0755 -d "$(prefix)/share/lotus/123.v10/sysV386"
install -m 0644 "share/lotus/123.v10/sysV386/lotus.bcf" "$(prefix)/share/lotus/123.v10/sysV386/lotus.bcf"
install -m 0755 -d "$(prefix)/share/lotus/123.v10/sysV386/lib"
install -m 0644 "share/lotus/123.v10/sysV386/lib/wyse50-lts123" "$(prefix)/share/lotus/123.v10/sysV386/lib/wyse50-lts123"
find "share/lotus/123.v10/cbd" -type d -exec install -m 0755 -d "$(prefix)/{}" \;
find "share/lotus/123.v10/cbd" -type f -exec install -m 0644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/fonts" -type d -exec install -m 0755 -d "$(prefix)/{}" \;
find "share/lotus/123.v10/fonts" -type f -exec install -m 0644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/hlp" -type d -exec install -m 0755 -d "$(prefix)/{}" \;
find "share/lotus/123.v10/hlp" -type f -exec install -m 0644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/keymaps" -type d -exec install -m 0755 -d "$(prefix)/{}" \;
find "share/lotus/123.v10/keymaps" -type f -exec install -m 0644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/pbd" -type d -exec install -m 0755 -d "$(prefix)/{}" \;
find "share/lotus/123.v10/pbd" -type f -exec install -m 0644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/ri" -type d -exec install -m 0755 -d "$(prefix)/{}" \;
find "share/lotus/123.v10/ri" -type f -exec install -m 0644 {} "$(prefix)/{}" \;
find "share/lotus/123.v10/smpfiles" -type d -exec install -m 0755 -d "$(prefix)/{}" \;
find "share/lotus/123.v10/smpfiles" -type f -exec install -m 0644 {} "$(prefix)/{}" \;

uninstall:
$(RM) "$(prefix)/bin/123"
$(RM) "$(prefix)/share/man/man1/123.1"
$(RM) -r "$(prefix)/share/lotus"
rm -f "$(prefix)/bin/123"
rm -f "$(prefix)/share/man/man1/123.1"
rm -rf "$(prefix)/share/lotus"
2 changes: 1 addition & 1 deletion atfuncs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LDLIBS=
all: atfuncs.a

atfuncs.a: date.o
$(AR) r $@ $^
$(AR) r $@ date.o

clean:
rm -f *.a *.o
6 changes: 3 additions & 3 deletions atfuncs/date.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <stdint.h>
#include <stddef.h>

#include "lottypes.h"
#include "lotfuncs.h"
#include "lotdefs.h"
#include "../lottypes.h"
#include "../lotfuncs.h"
#include "../lotdefs.h"

int16_t at_date()
{
Expand Down
52 changes: 26 additions & 26 deletions binutils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,55 @@ BINUTILS_DL="https://ftp.gnu.org/gnu/binutils"
BINUTILS_XZ="binutils-2.38.tar.xz"
BINUTILS_DIR="$(basename $BINUTILS_XZ .tar.xz)"
BINUTILS_URL="$BINUTILS_DL/$BINUTILS_XZ"
ORIG_DIR="$(pwd)"
ORIGIN_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"

# Dont continue on error.
set -e
# Make sure we have GNU make
. "$ORIGIN_DIR/detect-gnu-make.sh"

# Help text.
if [ "$1" = '-h' ]; then
echo >&2 "Usage: $0 [clean]"
echo >&2
echo >&2 'Specify "clean" to remove binutils, otherwise it will be downloaded and built.'
exit 1
echo >&2 "Usage: $0 [clean]"
echo >&2
echo >&2 'Specify "clean" to remove binutils, otherwise it will be downloaded and built.'
exit 1
fi

# Optional cleanup if requested.
if [ "$1" = 'clean' ]; then
rm -fv objcopy objdump ld "$BINUTILS_XZ"
rm -rfv "$BINUTILS_DIR"
exit
rm -f "$ORIGIN_DIR/objcopy" "$ORIGIN_DIR/objdump" "$ORIGIN_DIR/ld" "$ORIGIN_DIR/as"
rm -rf "$BINUTILS_DIR"
rm -f "$ORIGIN_DIR/$BINUTILS_XZ"
exit
fi

# Download binutils.
if [ ! -f "$BINUTILS_XZ" ]; then
wget "$BINUTILS_URL"
wget "$BINUTILS_URL"
fi

# Extract binutils.
if [ ! -d "$BINUTILS_DIR" ]; then
tar xf "$BINUTILS_XZ"
xzcat "$BINUTILS_XZ" | tar xf -
fi

# Compile binutils.
if [ ! -x "$BINUTILS_DIR/binutils/objcopy" ]; then
cd "$BINUTILS_DIR"
./configure --enable-targets=i386-pc-elf32 \
--disable-gas \
--disable-libctf \
--disable-plugins \
--disable-gprof \
--enable-compressed-debug-sections=none

make all-ld -j$(nproc) MAKEINFO=true
cd "$BINUTILS_DIR" && \
./configure --enable-targets=i386-pc-elf32 \
--disable-libctf \
--disable-plugins \
--disable-gprof \
--enable-compressed-debug-sections=none && \
"$gnu_make" all-ld all-gas MAKEINFO=true
cd "$ORIGIN_DIR"
fi

# Copy compiled binaries to working directory.
copy() {
test ! -x "$2" && cp -v "$1" "$2"
test ! -x "$2" && cp "$1" "$2"
}

cd "$ORIG_DIR"
copy "$BINUTILS_DIR/binutils/objcopy" objcopy
copy "$BINUTILS_DIR/binutils/objdump" objdump
copy "$BINUTILS_DIR/ld/ld-new" ld
copy "$BINUTILS_DIR/binutils/objcopy" "$ORIGIN_DIR/objcopy"
copy "$BINUTILS_DIR/binutils/objdump" "$ORIGIN_DIR/objdump"
copy "$BINUTILS_DIR/ld/ld-new" "$ORIGIN_DIR/ld"
copy "$BINUTILS_DIR/gas/as-new" "$ORIGIN_DIR/as"
9 changes: 9 additions & 0 deletions check-binutils-coff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

BASE="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
PATH="${BASE}:$PATH"

BFD_TARGET_ERROR="Your version of binutils was compiled without coff-i386 target support.
You can try running ./binutils.sh to build a version that does support it."

objdump --info | grep -q '^coff-i386$' || (echo "$BFD_TARGET_ERROR"; false)
30 changes: 22 additions & 8 deletions coffsyrup.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ int main(int argc, char **argv)

// Read in each symbol.
for (int i = 0; i < hdr.f_nsyms; i++) {
const char *symname;
char *symname;
int symnamedup = 0;

if (fread(&symtab[i], sizeof *symtab, 1, infile) != 1) {
err(EXIT_FAILURE, "Failed to read in a symbol.");
Expand All @@ -171,9 +172,12 @@ int main(int argc, char **argv)
}

// Figure out the symbol name.
symname = symtab[i].e.e.e_zeroes
? strndupa(symtab[i].e.e_name, sizeof symtab[i].e.e_name)
: (strtab + symtab[i].e.e.e_offset);
if (symtab[i].e.e.e_zeroes) {
symname = strndup(symtab[i].e.e_name, sizeof symtab[i].e.e_name);
symnamedup = 1;
} else {
symname = strtab + symtab[i].e.e.e_offset;
}

// See if we are supposed to be adjusting this symbol.
for (int check = 3; check < argc; check++) {
Expand Down Expand Up @@ -202,6 +206,9 @@ int main(int argc, char **argv)
}
}

// free symname if it was duplicated on heap
if (symnamedup) free(symname);

// XXX: Note the ++i, this must be last in the loop!!!
if (symtab[i].e_numaux) {
// Read in any auxiliary entry.
Expand Down Expand Up @@ -258,7 +265,8 @@ int main(int argc, char **argv)
relocs[i] = calloc(scn[i].s_nreloc + nmkrelocs, sizeof(RELOC));

for (int r = 0; r < scn[i].s_nreloc; r++) {
const char *symname;
char *symname;
int symnamedup = 0;
RELOC *rel = &relocs[i][r];

if (fread(rel, sizeof *rel, 1, infile) != 1) {
Expand All @@ -282,9 +290,12 @@ int main(int argc, char **argv)
}

// Figure out the symbol name.
symname = symtab[rel->r_symndx].e.e.e_zeroes
? strndupa(symtab[rel->r_symndx].e.e_name, sizeof symtab[rel->r_symndx].e.e_name)
: (strtab + symtab[rel->r_symndx].e.e.e_offset);
if (symtab[rel->r_symndx].e.e.e_zeroes) {
symname = strndup(symtab[rel->r_symndx].e.e_name, sizeof symtab[rel->r_symndx].e.e_name);
symnamedup = 1;
} else {
symname = (strtab + symtab[rel->r_symndx].e.e.e_offset);
}

// See if we are supposed to be adjusting this symbol.
for (int check = 3; check < argc; check++) {
Expand Down Expand Up @@ -313,6 +324,9 @@ int main(int argc, char **argv)
}
}
}

// free symname if it was duplicated on heap
if (symnamedup) free(symname);
}

for (int mkr = 0; mkr < nmkrelocs; mkr++) {
Expand Down
9 changes: 9 additions & 0 deletions detect-gnu-make.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
gnu_make="$(command -v make 2>/dev/null)"
if command -v gmake >/dev/null 2>&1; then
gnu_make="$(command -v gmake)"
fi
if [ -z "$gnu_make" -o "$("$gnu_make" --version 2>/dev/null | head -n 1 | cut -c-3)" != "GNU" ]; then
echo >&2 "Could not find GNU make in PATH"
exit 1
fi
Loading