lpsantil / rt0

A minimal C runtime for Linux i386 & x86_64
BSD 2-Clause "Simplified" License
579 stars 30 forks source link

Does not work in ubuntu 18.04 #3

Open Squantor opened 5 years ago

Squantor commented 5 years ago

I am trying to run the rt0 build in ubuntu 18.04 but I am getting the following error:

$ make 
make: *** No rule to make target '/usr/include//asm/unistd_64.h', needed by 'include/rt0/sys64.h'.  Stop.

After digging into the Makefile I come to the conclusion that there is something wrong on this line:

OS = $(shell cat /etc/os-release | grep "rhel\|fedora\|centos" && echo "rhel" || cat /etc/os-release | grep "debian\|ubuntu" && echo "debian" || uname)

As when I execute make showconfig the line for OS looks like this:

OS=ID=ubuntu ID_LIKE=debian HOME_URL=https://www.ubuntu.com/ SUPPORT_URL=https://help.ubuntu.com/ BUG_REPORT_URL=https://bugs.launchpad.net/ubuntu/ PRIVACY_POLICY_URL=https://www.ubuntu.com/legal/terms-and-policies/privacy-policy debian

Instead of the expected "debian", after staring at the line in the Makefile for a while, I can understand what is happening, grep searches for the strings "debian" or "ubuntu" and will echo debian when this matches. But this does not happen correctly. Maybe somebody better versed in unix commandline magic would be a better candidate to solve this problem :-).

tkchia commented 5 years ago

Hello @Squantor , hello @lpsantil ,

The grep commands need a -q --- we want grep to only return an exit status code, and not echo the matching lines in /etc/os-release to standard output.

OS = $(shell cat /etc/os-release | grep -q "rhel\|fedora\|centos" && echo "rhel" \
    || cat /etc/os-release | grep -q "debian\|ubuntu" && echo "debian" \
    || uname)

I may submit a pull request for this soon.

Thank you!

Squantor commented 5 years ago

@tkchia the -q with grep commands fixed it indeed. @tkchia @lpsantil But after further digging I found two additional problems after setting OS just to "debian": The generation of the systemcall header file is not correct, this occurs in line:

$(SYSINC): /usr/include/$(UNISTD_PATH)/asm/unistd_$(MSIZE).h
    echo -e "\n#define __SYSCALL(x,y)\n" >> $@
    grep __NR_ $< | sed -e s/__NR_/SYS_/g >> $@
ifeq ($(WITH_FAST_SYSCALL), 1)
    echo -e "\n#define __RT0_WITH_FASTER_SYSCALL__ 1\n" >> $@
endif
    echo -e "\n#undef __SYSCALL\n" >> $@

At the beginning and end of the file some spurious "-e" are added. Fixed this manually. At compilation I get the following warnings:

cc -Os -Wall -std=gnu99 -pedantic -nostdlib -fomit-frame-pointer -fno-stack-protector -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-unroll-loops -fmerge-all-constants -fno-ident -mfpmath=sse -mfancy-math-387 -ffunction-sections -fdata-sections -Wl,--gc-sections -flto -MMD -MP -Iinclude -c t/_end.c -o t/_end.o
ld -Llib -lrt0 -s -static -nostdlib -z norelro --hash-style=sysv --build-id=none --gc-sections -flto t/_end.o -o t/_end.exe
ld: lib/librt0.a(00_start.o): plugin needed to handle lto object
ld: t/_end.o: plugin needed to handle lto object
ld: warning: cannot find entry symbol _start; not setting start address

And the binaries (weird that they have the extension .exe) produce:

$ ./test.exe 
bash: ./test.exe: cannot execute binary file: Exec format error

running file:

$ file t/test.exe 
t/test.exe: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), corrupted program header size, stripped

Strange that the program header size is corrupted? As we get errors on lto, I removed the option from the CFLAGS, not LDFLAGS. Compiling and linking has no errors and the executables work. Here are my GCC settings, it is just the standard gcc for xubuntu:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.3.0-27ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04) 
lpsantil commented 5 years ago

I'll setup a couple VMs and take a look at this tonight.