OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 4:17 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Problems with compiling using [OS Specific toolchain]
PostPosted: Wed Sep 27, 2017 12:57 pm 
Offline

Joined: Sun Jul 16, 2017 4:22 am
Posts: 16
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:
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:
SECTION .text
GLOBAL _start
EXTERN bootstrap_stage_1
EXTERN main
_start:
   push main
   call bootstrap_stage_1


crti.s:
Code:
SECTION .init
GLOBAL _bootstrap_stage_2
_bootstrap_stage_2:
  push ebp

SECTION .fini
GLOBAL _de_bootstrap
_de_bootstrap:
  push ebp


crtn.s:
Code:
SECTION .init
  pop ebp
  ret

SECTION .fini
  pop ebp
  ret


bootstrap.c as part of newlib build:
Code:
#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:
#!/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:
#!/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:
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:
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


Top
 Profile  
 
 Post subject: Re: Problems with compiling using [OS Specific toolchain]
PostPosted: Wed Sep 27, 2017 4:11 pm 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
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?

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: Problems with compiling using [OS Specific toolchain]
PostPosted: Wed Sep 27, 2017 5:14 pm 
Offline

Joined: Sun Jul 16, 2017 4:22 am
Posts: 16
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:
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:
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.


Top
 Profile  
 
 Post subject: Re: Problems with compiling using [OS Specific toolchain]
PostPosted: Thu Sep 28, 2017 1:18 am 
Offline
Member
Member

Joined: Thu Jul 05, 2007 8:58 am
Posts: 223
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?


Top
 Profile  
 
 Post subject: Re: Problems with compiling using [OS Specific toolchain]
PostPosted: Thu Sep 28, 2017 5:35 am 
Offline

Joined: Sun Jul 16, 2017 4:22 am
Posts: 16
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:
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:
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:
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:
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:
// 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?


Top
 Profile  
 
 Post subject: Re: Problems with compiling using [OS Specific toolchain]
PostPosted: Thu Sep 28, 2017 10:02 am 
Offline
Member
Member

Joined: Thu Jul 05, 2007 8:58 am
Posts: 223
I spot two definitely problems, and one potential problem:

First of all, using
Code:
-DREENTRANT_SYSCALLS_PROVIDED
is lying when also passing
Code:
-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:
-DREENTRANT_SYSCALLS_PROVIDED
is given, namespace poluting names (like lseek) when
Code:
-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.


Top
 Profile  
 
 Post subject: Re: Problems with compiling using [OS Specific toolchain]
PostPosted: Thu Sep 28, 2017 10:45 am 
Offline

Joined: Sun Jul 16, 2017 4:22 am
Posts: 16
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


Top
 Profile  
 
 Post subject: Re: Problems with compiling using [OS Specific toolchain]
PostPosted: Thu Sep 28, 2017 2:37 pm 
Offline

Joined: Sun Jul 16, 2017 4:22 am
Posts: 16
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:
BITS 32

global _start
_start:
  extern main
  extern bootstrap
  push main
  call bootstrap


"bootstrap" is a function defined in my syscalls.c file

Code:
#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:
/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?


Top
 Profile  
 
 Post subject: Re: Problems with compiling using [OS Specific toolchain]
PostPosted: Fri Sep 29, 2017 12:25 am 
Offline
Member
Member

Joined: Thu Jul 05, 2007 8:58 am
Posts: 223
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.


Top
 Profile  
 
 Post subject: Re: Problems with compiling using [OS Specific toolchain]
PostPosted: Fri Sep 29, 2017 1:14 am 
Offline

Joined: Sun Jul 16, 2017 4:22 am
Posts: 16
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: SemrushBot [Bot] and 80 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group