Page 1 of 1

Problems with compiling using [OS Specific toolchain]

Posted: Wed Sep 27, 2017 12:57 pm
by dborowiec10
Hi,

For the past 2 weeks I've been trying to set up the OS Specific Toolchain.
All goes well until I try to compile a userspace C file with it.

I've created a pretty automated build process for the toolchain
Here are the steps that are performed by my bash scripts:
1. Download Sources:
- GCC 6.3.0, Binutils 2.27, Newlib 2.5.0, Autoconf 2.64 & 2.65 + Automake 1.11 & 1.11.6
2. Untar them
3. Build Generic elf target toolchain from GCC and Binutils (this works perfectly fine and compiles my kernel correctly)
4. Build automake and autoconf as 3 isolated combinations
- standalone autoconf2.64
- autoconf2.65 + automake1.11 (installing into the same ../bin/ directory)
- autoconf2.64 + automake1.11.6 (as above)
5. Prepare all required things for Newlib (crt0.s, crti.s, crtn.s, syscall definitions etc.)
6. Patch Newlib using awk and sed
7. Run autoconf and automake as documented in the "Porting Newlib" tutorial on the wiki
8. Build Newlib (works fine)
9. Patch Binutils using sed and awk
10. Build Binutils as documented in the "OS Specific Toolchain" tutorial (builds without any errors)
11. Patch GCC using sed and awk
12. Build GCC (same as for Binutils) (also reports no errors during the build process)

All of this seems to work perfectly fine until I actually try to use these tools

I should probably also add that I've tried the above with multiple versions of Binutils and GCC without any luck.
(GCC 7.1.0, Binutils 2.28 and more...)

The problem:
Im trying to compile a very simple test.c file using my toolchain like this

Code: Select all

damian@damian-Lenovo-Y50-70:~/Desktop/DBOS/apps$ ../build_tools/toolchain/host/bin/i686-dbos-gcc -v -o test  test.c
Using built-in specs.
COLLECT_GCC=../build_tools/toolchain/host/bin/i686-dbos-gcc
COLLECT_LTO_WRAPPER=/home/damian/Desktop/DBOS/build_tools/toolchain/host/libexec/gcc/i686-dbos/6.3.0/lto-wrapper
Target: i686-dbos
Configured with: /home/damian/Desktop/DBOS/build_tools/sources/host_gcc-6.3.0/configure --target=i686-dbos --prefix=/home/damian/Desktop/DBOS/build_tools/toolchain/host --with-sysroot=/home/damian/Desktop/DBOS/sysroot --enable-languages=c,c++
Thread model: single
gcc version 6.3.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-o' 'test' '-fPIE' '-mtune=generic' '-march=pentiumpro'
 /home/damian/Desktop/DBOS/build_tools/toolchain/host/libexec/gcc/i686-dbos/6.3.0/cc1 -quiet -v test.c -quiet -dumpbase test.c -mtune=generic -march=pentiumpro -auxbase test -version -fPIE -o /tmp/ccZHbSxc.s
GNU C11 (GCC) version 6.3.0 (i686-dbos)
	compiled by GNU C version 6.3.0 20170406, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/home/damian/Desktop/DBOS/sysroot/usr/local/include"
ignoring nonexistent directory "/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/include
 /home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/include-fixed
 /home/damian/Desktop/DBOS/sysroot/usr/include
End of search list.
GNU C11 (GCC) version 6.3.0 (i686-dbos)
	compiled by GNU C version 6.3.0 20170406, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 24d62e71f8cbce4777182df57909d083
COLLECT_GCC_OPTIONS='-v' '-o' 'test' '-fPIE' '-mtune=generic' '-march=pentiumpro'
 /home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/bin/as -v -o /tmp/cc6gHlt4.o /tmp/ccZHbSxc.s
GNU assembler version 2.27 (i686-dbos) using BFD version (GNU Binutils) 2.27
COMPILER_PATH=/home/damian/Desktop/DBOS/build_tools/toolchain/host/libexec/gcc/i686-dbos/6.3.0/:/home/damian/Desktop/DBOS/build_tools/toolchain/host/libexec/gcc/i686-dbos/6.3.0/:/home/damian/Desktop/DBOS/build_tools/toolchain/host/libexec/gcc/i686-dbos/:/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/:/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/:/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/bin/
LIBRARY_PATH=/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/:/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/lib/:/home/damian/Desktop/DBOS/sysroot/lib/:/home/damian/Desktop/DBOS/sysroot/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'test' '-fPIE' '-mtune=generic' '-march=pentiumpro'
 /home/damian/Desktop/DBOS/build_tools/toolchain/host/libexec/gcc/i686-dbos/6.3.0/collect2 -plugin /home/damian/Desktop/DBOS/build_tools/toolchain/host/libexec/gcc/i686-dbos/6.3.0/liblto_plugin.so -plugin-opt=/home/damian/Desktop/DBOS/build_tools/toolchain/host/libexec/gcc/i686-dbos/6.3.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccOBMjpW.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc --sysroot=/home/damian/Desktop/DBOS/sysroot -o test /home/damian/Desktop/DBOS/sysroot/usr/lib/crt0.o /home/damian/Desktop/DBOS/sysroot/usr/lib/crti.o -L/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0 -L/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/lib -L/home/damian/Desktop/DBOS/sysroot/lib -L/home/damian/Desktop/DBOS/sysroot/usr/lib /tmp/cc6gHlt4.o -lgcc -lc -lgcc /home/damian/Desktop/DBOS/sysroot/usr/lib/crtn.o
collect2: fatal error: ld terminated with signal 11 [Segmentation fault], core dumped
compilation terminated.
/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/bin/ld: test: warning: allocated section `.init' not in segment
/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/bin/ld: test: warning: allocated section `.text' not in segment
/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/bin/ld: test: warning: allocated section `.fini' not in segment
/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/bin/ld: test: warning: allocated section `.rodata' not in segment
/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/bin/ld: test: warning: allocated section `.eh_frame' not in segment
/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/bin/ld: test: warning: allocated section `.got.plt' not in segment
/home/damian/Desktop/DBOS/build_tools/toolchain/host/lib/gcc/i686-dbos/6.3.0/../../../../i686-dbos/bin/ld: test: warning: allocated section `.data' not in segment
As you can see GCC is finding my crt0, crti and crtn files perfectly fine.
I can also see in the warnings that it tries to do something with the init and fini sections which are in my crt files.

I've tried searching for anwers on the Forum and Google but I couldn't find anything about "ld terminated with signal 11, segfault, core dumped" and those warnings.

Just to give you some more background, here are the contents of my crt files, syscall definitions and the patches I apply to Newlib, Binutils and GCC:

crt0.s:

Code: Select all

SECTION .text
GLOBAL _start
EXTERN bootstrap_stage_1
EXTERN main
_start:
	push main
	call bootstrap_stage_1
crti.s:

Code: Select all

SECTION .init
GLOBAL _bootstrap_stage_2
_bootstrap_stage_2:
  push ebp

SECTION .fini
GLOBAL _de_bootstrap
_de_bootstrap:
  push ebp
crtn.s:

Code: Select all

SECTION .init
  pop ebp
  ret

SECTION .fini
  pop ebp
  ret
bootstrap.c as part of newlib build:

Code: Select all

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/times.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>

extern char ** environ;

extern void _bootstrap_stage_2;

extern int _exit();

#define int (*main_t)(int argc, char ** argv)

void bootstrap_stage_1(main_t main, int arguments_count, char * arguments_values[]){
  environ = NULL;
  _bootstrap_stage_2();
  int retval = main(arguments_count, arguments_values);
  _exit(retval);
}
Newlib patch:

Code: Select all

#!/bin/bash
WD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
VER_NEWLIB="newlib-2.5.0"
NEWLIB_SOURCEDIR=$WD/../sources/${VER_NEWLIB}/

if [ ! -f $NEWLIB_SOURCEDIR/.patched ]; then
  sed -r 's:(\-plan9\* \\):\-plan9\* \| \-dbos\* \\:' < $NEWLIB_SOURCEDIR/config.sub > $WD/config.sub.tmp
  cp $WD/config.sub.tmp $NEWLIB_SOURCEDIR/config.sub
  rm $WD/config.sub.tmp
  touch $NEWLIB_SOURCEDIR/.patched
fi

if [ ! -f $NEWLIB_SOURCEDIR/newlib/.patched ]; then
  awk '{ print }
  /^case "\$\{host\}\" in$/ {
    print "\ti[3-7]86-*-dbos*)"
    print "\t\t\tsysdir=dbos"
    print "\t\t\t;;"
  }' < $NEWLIB_SOURCEDIR/newlib/configure.host > $WD/configure.host.tmp
  cp $WD/configure.host.tmp $NEWLIB_SOURCEDIR/newlib/configure.host
  rm $WD/configure.host.tmp
  touch $NEWLIB_SOURCEDIR/newlib/.patched
fi

if [ ! -f $NEWLIB_SOURCEDIR/newlib/libc/sys/.patched ]; then
  awk '{ print }
  /case \$\{sys_dir\} in$/ {
      print "\tdbos) AC_CONFIG_SUBDIRS(dbos) ;;"
  }' < $NEWLIB_SOURCEDIR/newlib/libc/sys/configure.in > $WD/configure.in.tmp
  cp $WD/configure.in.tmp $NEWLIB_SOURCEDIR/newlib/libc/sys/configure.in
  rm $WD/configure.in.tmp
  touch $NEWLIB_SOURCEDIR/newlib/libc/sys/.patched
fi
Binutils patch:

Code: Select all

#!/bin/bash
WD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BINUTILS_VER="binutils-2.27"

BINUTILS_SOURCEDIR=$WD/../sources/host_${BINUTILS_VER}

if [ ! -f $BINUTILS_SOURCEDIR/.patched ]; then
  sed -r 's:(\-plan9\* \\):\-plan9\* \| \-dbos\* \\:' < $BINUTILS_SOURCEDIR/config.sub > $WD/config.sub.tmp
  cp $WD/config.sub.tmp $BINUTILS_SOURCEDIR/config.sub
  rm -rf $WD/config.sub.tmp
  touch $BINUTILS_SOURCEDIR/.patched
fi

if [ ! -f $BINUTILS_SOURCEDIR/bfd/.patched ]; then
  awk ' { print }
  /# START OF targmatch.h/{
    print "\ti[3-7]86-*-dbos*)"
    print "\t\ttarg_defvec=i386_elf32_vec"
    print "\t\ttarg_selvec="
    print "\t\t;;"
    print ""
  }
  ' < $BINUTILS_SOURCEDIR/bfd/config.bfd > $WD/config.bfd.tmp
  cp $WD/config.bfd.tmp $BINUTILS_SOURCEDIR/bfd/config.bfd
  rm -rf $WD/config.bfd.tmp
  touch $BINUTILS_SOURCEDIR/bfd/.patched
fi

if [ ! -f $BINUTILS_SOURCEDIR/gas/.patched ]; then
  awk ' { print }
  /case \$\{generic_target\} in$/{
    print "\ti386-*-dbos*)\t\t\tfmt=elf;;"
    print ""
  }
  ' < $BINUTILS_SOURCEDIR/gas/configure.tgt > $WD/configure.tgt.tmp
  cp $WD/configure.tgt.tmp $BINUTILS_SOURCEDIR/gas/configure.tgt
  rm -rf $WD/configure.tgt.tmp
  touch $BINUTILS_SOURCEDIR/gas/.patched
fi

if [ ! -f $BINUTILS_SOURCEDIR/ld/.patched ]; then
  awk ' { print }
  /^case "\$\{targ\}" in$/{
    print "i[3-7]86-*-dbos*)"
    print "\t\t\ttarg_emul=elf_i386_dbos"
    print "\t\t\ttarg_extra_emuls=elf_i386"
    print "\t\t\t;;"
  }
  ' < $BINUTILS_SOURCEDIR/ld/configure.tgt > $WD/configure.tgt.tmp
  cp $WD/configure.tgt.tmp $BINUTILS_SOURCEDIR/ld/configure.tgt
  rm -rf $WD/configure.tgt.tmp
  touch $BINUTILS_SOURCEDIR/ld/.patched
fi

if [ ! -f $BINUTILS_SOURCEDIR/ld/emulparams/elf_i386_dbos.sh ]; then
  touch $BINUTILS_SOURCEDIR/ld/emulparams/elf_i386_dbos.sh
echo 'SCRIPT_NAME=elf
OUTPUT_FORMAT="elf32-i386"
TEXT_START_ADDR=0x08000000
MAXPAGESIZE="CONSTANT (MAXPAGESIZE)"
COMMONPAGESIZE="CONSTANT (COMMONPAGESIZE)"
ARCH=i386
MACHINE=
NOP=0x90909090
GENERATE_SHLIB_SCRIPT=yes
GENERATE_PIE_SCRIPT=yes
NO_SMALL_DATA=yes
SEPARATE_GOTPLT="SIZEOF (.got.plt) >= 12 ? 12 : 0"
IREL_IN_PLT=
' > $BINUTILS_SOURCEDIR/ld/emulparams/elf_i386_dbos.sh
fi

if [ ! -f $BINUTILS_SOURCEDIR/ld/.patched2 ]; then
  awk ' { print }
  /eelf_i386.c \\/{
    print "\teelf_i386_dbos.c \\"
  }
  /eelf_i386.c: \$\(srcdir\)\/emulparams\/elf_i386.sh \\/{
    getline; print $0
    getline; print $0
    print "eelf_i386_dbos.c: $(srcdir)/emulparams/elf_i386_dbos.sh \\"
    print "\t$(ELF_DEPS) $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}"
    print "\t\t${GENSCRIPTS} elf_i386_dbos \"$(tdir_elf_i386_dbos)\""
    print ""
  }
  ' < $BINUTILS_SOURCEDIR/ld/Makefile.am > $WD/Makefile.am.tmp
  cp $WD/Makefile.am.tmp $BINUTILS_SOURCEDIR/ld/Makefile.am
  rm -rf $WD/Makefile.am.tmp
  touch $BINUTILS_SOURCEDIR/ld/.patched2
fi
GCC Patch 1:

Code: Select all

WD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
GCC_VER="gcc-6.3.0"

GCC_SOURCEDIR=$WD/../sources/host_${GCC_VER}

if [ ! -f $GCC_SOURCEDIR/.patched ]; then
  sed -r 's:(\-plan9\* \\):\-plan9\* \| \-dbos\* \\:' < $GCC_SOURCEDIR/config.sub > $WD/config.sub.tmp
  cp $WD/config.sub.tmp $GCC_SOURCEDIR/config.sub
  rm $WD/config.sub.tmp
  touch $GCC_SOURCEDIR/.patched
fi

if [ ! -f $GCC_SOURCEDIR/gcc/.patched ]; then
  awk ' { print }
  /# Common parts for widely ported systems./{
    getline; print $0
    print "*-*-dbos*)"
    print "\tgas=yes"
    print "\tgnu_ld=yes"
    print "\tdefault_use_cxa_atexit=yes"
    print "\t;;"
  }
  /i\[34567\]86\-\*\-dragonfly\*\)/{
    getline; print $0
    getline; print $0
    getline; print $0
    getline; print $0
    getline; print $0
    getline; print $0
    getline; print $0
    print "i[34567]86-*-dbos*)"
    print "\ttm_file=\"${tm_file} i386/unix.h i386/att.h dbxelf.h elfos.h newlib-stdint.h i386/i386elf.h dbos.h\""
    print "\t;;"
  }
  ' < $GCC_SOURCEDIR/gcc/config.gcc > $WD/config.gcc.tmp
  cp $WD/config.gcc.tmp $GCC_SOURCEDIR/gcc/config.gcc
  rm -rf $WD/config.gcc.tmp
  touch $GCC_SOURCEDIR/gcc/.patched
fi

if [ ! -f $GCC_SOURCEDIR/gcc/config/dbos.h ]; then
  touch $GCC_SOURCEDIR/gcc/config/dbos.h
echo '
#undef TARGET_DBOS
#define TARGET_DBOS 1

#define LIB_SPEC "-lc"

#undef STARTFILE_SPEC
#define STARTFILE_SPEC "crt0.o%s crti.o%s"

#undef ENDFILE_SPEC
#define ENDFILE_SPEC "crtn.o%s"

#undef NO_IMPLICIT_EXTERN_C
#define NO_IMPLICIT_EXTERN_C 1
' > $GCC_SOURCEDIR/gcc/config/dbos.h
fi

if [ ! -f $GCC_SOURCEDIR/libstdc++-v3/.patched ]; then
  awk ' { print }
  /\*\-darwin\*\)/{
    getline; print $0
    getline; print $0
    getline; print $0
    getline; print $0
    getline; print $0
    getline; print $0
    getline; print $0
    getline; print $0
    getline; print $0
    print "\t*-dbos*)"
    print "\t\tGLIBCXX_CHECK_COMPILER_FEATURES"
    print "\t\tGLIBCXX_CHECK_LINKER_FEATURES"
    print "\t\tGLIBCXX_CHECK_MATH_SUPPORT"
    print "\t\tGLIBCXX_CHECK_STDLIB_SUPPORT"
    print "\t\t;;"
    print ""
  }
  ' < $GCC_SOURCEDIR/libstdc++-v3/crossconfig.m4 > $WD/crossconfig.m4.tmp
  cp $WD/crossconfig.m4.tmp $GCC_SOURCEDIR/libstdc++-v3/crossconfig.m4
  rm -rf $WD/crossconfig.m4.tmp
  touch $GCC_SOURCEDIR/libstdc++-v3/.patched
fi
At this point, Im running autoconf in libstdc++-v3 dir

GCC patch 2:

Code: Select all

WD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
GCC_VER="gcc-6.3.0"

GCC_SOURCEDIR=$WD/../sources/host_${GCC_VER}

if [ ! -f $GCC_SOURCEDIR/libgcc/.patched ]; then
  awk ' { print }
  /i\[34567\]86\-\*\-dragonfly\*\)/{
    getline; print $0
    getline; print $0
    getline; print $0
    print "i[34567]86-*-dbos*)"
    print "\t;;"
  }
  ' < $GCC_SOURCEDIR/libgcc/config.host > $WD/config.host.tmp
  cp $WD/config.host.tmp $GCC_SOURCEDIR/libgcc/config.host
  rm -rf $WD/config.host.tmp
  touch $GCC_SOURCEDIR/libgcc/.patched
fi

if [ ! -f $GCC_SOURCEDIR/fixincludes/.patched ]; then
  awk ' { print }
  /case \$machine in/{
    print "\t\t*-dbos* | \\"
  }
  ' < $GCC_SOURCEDIR/fixincludes/mkfixinc.sh > $WD/mkfixinc.sh.tmp
  cp $WD/mkfixinc.sh.tmp $GCC_SOURCEDIR/fixincludes/mkfixinc.sh.tmp
  rm -rf $WD/mkfixinc.sh.tmp
  touch $GCC_SOURCEDIR/fixincludes/.patched
fi

Re: Problems with compiling using [OS Specific toolchain]

Posted: Wed Sep 27, 2017 4:11 pm
by Korona
Why are you using sed/awk/bash to patch the source? Using diff + patch or git would increase the readability of your patches significantly (or rather, not using diff makes them completely unreadable).

Did you check that the generated linker script (it's an .x file in ld's destdir) is sane?

Re: Problems with compiling using [OS Specific toolchain]

Posted: Wed Sep 27, 2017 5:14 pm
by dborowiec10
Because they are the tools I know enough to use for this purpose. Plus having them in shell scripts allows me to make quick changes to the sources and then rebuild them by running one command

Btw:
I think I got it to work once I added:

Code: Select all

extra_parts="$extra_parts crt0.s crti.s crtn.s"
line to the i[34567]86-*-dbos section of libgcc/config.host

It wasn't properly including the crt files
Also, my crt files were compiled by yasm... which by default compiles to 16 bit (the section warnings)
Adding:

Code: Select all

BITS 32
to the top of my crt files helped.

On a side note, do you know why newlib would complain about missing reentrant syscalls?
Should they be defined somewhere?

The Tutorial only specifies like 17 of them or so and I haven't noticed any of them to include reentrant ones.

Thanks for the idea with linker script, I was actually wondering which one is used during linking since for userspace you don't really provide one as an arg to ld.

Re: Problems with compiling using [OS Specific toolchain]

Posted: Thu Sep 28, 2017 1:18 am
by davidv1992
From what i can extract from your "patches" on how it changes configure.host, it should be enough to provide the 17 functions listed in the wiki. Newlib then provides wrappers that (sort of) emulate the reentrant versions (see reent directory). Note that there are a number of disadvantages to providing these functions in the way the wiki does, particularly around how you have to handle errno (see newlib documentation). But, given those 17 functions, compilation of newlib and programs using just c library functions (no posix extensions!) should work.

If you're sure that you have those and it is still complaining, what c library function are you calling and what is it missing?

Re: Problems with compiling using [OS Specific toolchain]

Posted: Thu Sep 28, 2017 5:35 am
by dborowiec10
At the moment I got it to not complain about the reentrant versions of syscalls but it now complains about the non-reentrant ones
I have added:

Code: Select all

newlib_cflags=\"${newlib_cflags} -DSIGNAL_PROVIDED -DREENTRANT_SYSCALLS_PROVIDED -DMISSING_SYSCALL_NAMES
to the definition in ../newlib/configure.host

When linking, it now throws this:

Code: Select all

damian@damian-Lenovo-Y50-70:~/Desktop/DBOS/apps$ ../build_tools/toolchain/host/bin/i686-dbos-gcc -o test test.c
/home/damian/Desktop/DBOS/sysroot/usr/lib/crt0.o: In function `_start':
/home/damian/Desktop/DBOS/build_tools/newlib/dbos/crt0.s:(.text+0x6): undefined reference to `bootstrap_stage_1'
/home/damian/Desktop/DBOS/sysroot/usr/lib/libc.a(lib_a-mallocr.o): In function `malloc_extend_top':
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdlib/mallocr.c:2165: undefined reference to `sbrk'
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdlib/mallocr.c:2202: undefined reference to `sbrk'
/home/damian/Desktop/DBOS/sysroot/usr/lib/libc.a(lib_a-stdio.o): In function `__sread':
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdio/stdio.c:48: undefined reference to `read'
/home/damian/Desktop/DBOS/sysroot/usr/lib/libc.a(lib_a-stdio.o): In function `__swrite':
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdio/stdio.c:89: undefined reference to `lseek'
/home/damian/Desktop/DBOS/sysroot/usr/lib/libc.a(lib_a-stdio.o): In function `__sseek':
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdio/stdio.c:117: undefined reference to `lseek'
/home/damian/Desktop/DBOS/sysroot/usr/lib/libc.a(lib_a-stdio.o): In function `__swrite':
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdio/stdio.c:97: undefined reference to `write'
/home/damian/Desktop/DBOS/sysroot/usr/lib/libc.a(lib_a-stdio.o): In function `__sclose':
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdio/stdio.c:135: undefined reference to `close'
/home/damian/Desktop/DBOS/sysroot/usr/lib/libc.a(lib_a-freer.o): In function `_malloc_trim_r':
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdlib/mallocr.c:3331: undefined reference to `sbrk'
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdlib/mallocr.c:3340: undefined reference to `sbrk'
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdlib/mallocr.c:3345: undefined reference to `sbrk'
/home/damian/Desktop/DBOS/sysroot/usr/lib/libc.a(lib_a-makebuf.o): In function `__swhatbuf_r':
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdio/makebuf.c:98: undefined reference to `fstat'
/home/damian/Desktop/DBOS/sysroot/usr/lib/libc.a(lib_a-makebuf.o): In function `__smakebuf_r':
/home/damian/Desktop/DBOS/build_tools/sources/newlib-2.5.0/newlib/libc/stdio/makebuf.c:69: undefined reference to `isatty'
collect2: error: ld returned 1 exit status
Im trying to run:

Code: Select all

printf("hello world")
in the test.c that im compiling against newlib (including <stdio.h>)

I am properly defining those syscalls in the syscalls.c file and then referencing it in the Makefile.am like so:

Code: Select all

AUTOMAKE_OPTIONS = cygnus
INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS)
AM_CCASFLAGS = $(INCLUDES)

noinst_LIBRARIES = lib.a

lib_a_SOURCES = syscalls.c
lib_a_CCASFLAGS = $(AM_CCASFLAGS)
lib_a_CFLAGS = $(AM_CFLAGS)

all-local: crt0.o crti.o crtn.o

ACLOCAL_AMFLAGS = -I ../../..
CONFIG_STATUS_DEPENDENCIES = $(newlib_basedir)/configure.host
Here is an example for the srbk definition in my syscalls.c

Code: Select all

// crti.o and crtn.o
extern void _fini;

extern char ** environ;

extern void _init;


#define SYS1_DEC(function, param_1) int function##_syscall(param_1)

#define SYS1_DEF(function, syscall_id, ARG1) \
  int function##_syscall(ARG1 param_1) { \
    int ret_val; \
    __asm__ __volatile__("int $0x81" \
                         : "=a" (ret_val) \
                         : "0" (syscall_id), "b" ((int)param_1) \
    ); \
    return ret_val; \
  }

// ----------------------- sbrk
SYS1_DEC(sbrk, int);
SYS1_DEF(sbrk, 5, int);
caddr_t sbrk(int incr){
 return -1;
}

#define int (*main_t)(int argc, char ** argv)

void bootstrap_stage_1(main_t main, int arguments_count, char * arguments_values[]){
  environ = NULL;
  _init();
  int retval = main(arguments_count, arguments_values);
  _exit(retval);
}
All seems to be following the tutorial yet it still complains.
Any ideas what it might be?

Re: Problems with compiling using [OS Specific toolchain]

Posted: Thu Sep 28, 2017 10:02 am
by davidv1992
I spot two definitely problems, and one potential problem:

First of all, using

Code: Select all

-DREENTRANT_SYSCALLS_PROVIDED
is lying when also passing

Code: Select all

-DMISSING_SYSCALL_NAMES
. Really, you should either use neither, or one of those flags, which switches between syscalls as reentrant functions (_lseek_r function name) when

Code: Select all

-DREENTRANT_SYSCALLS_PROVIDED
is given, namespace poluting names (like lseek) when

Code: Select all

-DMISSING_SYSCALL_NAMES
is given, and namespace clean variants (like _lseek) when neither is provided. See the newlib documentation and source files for how this all functions.

Second, are you sure your syscalls.c file is compiled?

Third, your sbrk always returns -1. This is valid, but will result in undesirable behaviour as sbrk needs to function for malloc to function, which is required for the stdio functions.

Re: Problems with compiling using [OS Specific toolchain]

Posted: Thu Sep 28, 2017 10:45 am
by dborowiec10
Thank you for pointing out those flags.

As to srbk, this is a stub for now as Im just beginning to work on userspace and I wanted to supply something so newlib is not complaining about missing syscalls (I think srbk is one of the compulsory ones to even build newlib).

Thank you all for help. Im pretty certain (knowing my luck) something will crap out again soon as I have zero experience with it so I might need more help from the pro's :D

Re: Problems with compiling using [OS Specific toolchain]

Posted: Thu Sep 28, 2017 2:37 pm
by dborowiec10
Im still experiencing an issue with newlib...
I can't believe I cannot port a C library with a tutorial at hand...

So...
In my crt0.s file, I am referencing an external function like this.

Code: Select all

BITS 32

global _start
_start:
  extern main
  extern bootstrap
  push main
  call bootstrap
"bootstrap" is a function defined in my syscalls.c file

Code: Select all

#define int (*main_t)(int argc, char ** argv)

void bootstrap(main_t main, int arguments_count, char * arguments_values[]){
  environ = NULL;
  _init();
  int retval = main(arguments_count, arguments_values);
  _exit(retval);
}
Looking at the configuration, it should be included in the library once it is built.
However...

When compiling a test program, gcc spits out this:

Code: Select all

/home/damian/Desktop/DBOS/build_tools/newlib/dbos/crt0.s: (.text+0x6): undefined reference to 'bootstrap'
Can anyone point me in the right direction with this?

Re: Problems with compiling using [OS Specific toolchain]

Posted: Fri Sep 29, 2017 12:25 am
by davidv1992
For solving this you probably need to start digging around in newlib's build system. My best guess is that it is, for some reason, not building syscalls.o, but you should be able to verify this in a rather easily from the build directories. If that is indeed so, then the next step would be figuring out why it isnt being build. The easy one to fix would be having forgotten to run autotools stuff on some part of the build tree, but if that is not it, you just need to dig.

Re: Problems with compiling using [OS Specific toolchain]

Posted: Fri Sep 29, 2017 1:14 am
by dborowiec10
syscalls.o are being built correctly. In the output of the compiler with flag -v I can see libc.a is linked. Also my test file calls printf from stdio which would complain if syscalls werent defined. Its just that one single function.

Btw: when it comes to crt0.s where does newlib place it after the build of itself. So far i had to assemble it with yasm and then copy it to the sysroot manually to avoid linker errors saying it cannot be found. Assuming im referencing it in Makefile.am and configure in and then running autoconf and autoreconf, I would have thought it will be built by newlib's build process and placed together with libc.a libm.a and libg.a in the sysroot/usr/lib but that does not happen. Any ideas?