rui314 / 8cc

A Small C Compiler
MIT License
6.12k stars 742 forks source link

`make test` fails with msg about -fPIC #86

Open popfalushi opened 7 years ago

popfalushi commented 7 years ago

Hello. This may be my local issue, however I have vanilla xubuntu 16.04.1 VM (installed today) so it can be widespread. After cloning repo and make && make test I get the following error: ... ./8cc -w -o test/line.o -c test/line.c ./8cc -w -o test/testmain.o -c test/testmain.c cc -o test/line.bin test/line.o test/testmain.o /usr/bin/ld: test/testmain.o: relocation R_X86_64_PC32 against symbol 'printf@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: Bad value collect2: error: ld returned 1 exit status Makefile:20: recipe for target 'test/line.bin' failed make: *** [test/line.bin] Error 1 rm test/line.o test/testmain.o I've spent several hours on this but haven't found a solution. However in C/C++ I am total newb right now. Maybe it will be helpful to update makefile somehow to solve this problem or add some note to README.MD about the issue.

Happy new year!

rdebath commented 7 years ago

I just tried this on a Xenial chroot and it's worked fine.

The message you're getting is contradictory, it states that you can't build a shared library without using -fpic (which is true on x64) but that command doesn't build a library, it builds an executable which doesn't need -fpic ... normally.

The dependencies of the build are minimal but there are lots of extras that interfere with the (generally) stable tools. Off the top of my head that may be things like "apparmour", "selinux", "ccache" and there are others.

So to start we need version numbers of all the tools cc --version,cc -v, python --version, update-alternatives --display cc and so forth.

Also was this the iso you used ?

4094e1c353742754064c59200ea5d5db0651e948 *xubuntu-16.04.1-desktop-amd64.iso

Did you apt-get update; apt-get dist-upgrade after installing the iso?

BTW: I built the chroot (as root) like this:

# debootstrap xenial xenial
# DIR="`pwd`"/xenial

# mount --bind /dev $DIR/dev
# mount --bind /sys $DIR/sys
# mount --bind /proc $DIR/proc
# mount --bind /run $DIR/run

# chroot xenial
# apt-get update
# apt-get install build-essential git python
# git clone https://github.com/rui314/8cc.git
# cd 8cc
# make && make test

And my (WORKING) versions are:

root@ubuntu:/# update-alternatives --display  cc
cc - auto mode
  link best version is /usr/bin/gcc
  link currently points to /usr/bin/gcc
  link cc is /usr/bin/cc
  slave cc.1.gz is /usr/share/man/man1/cc.1.gz
/usr/bin/gcc - priority 20
  slave cc.1.gz: /usr/share/man/man1/gcc.1.gz
root@ubuntu:/# python --version
Python 2.7.11+
root@ubuntu:/# cc --version
cc (Ubuntu 5.3.1-14ubuntu2) 5.3.1 20160413
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
root@ubuntu:/# cc -v
Using built-in specs.
COLLECT_GCC=cc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.3.1-14ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --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 --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2)
popfalushi commented 7 years ago

While gathering info I discovered that I actually used 16.10, not 16.04.1. I've installed 16.04.1 in another VM and everything worked there. Just in case, my info from 16.10:

$ cc --version
cc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005

$ cc -v
Using built-in specs.
COLLECT_GCC=cc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 6.2.0-5ubuntu12' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --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 --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 6.2.0 20161005 (Ubuntu 6.2.0-5ubuntu12) 

$ python --version
Python 2.7.12+

$ update-alternatives --display cc
cc - auto mode
  link best version is /usr/bin/gcc
  link currently points to /usr/bin/gcc
  link cc is /usr/bin/cc
  slave cc.1.gz is /usr/share/man/man1/cc.1.gz
/usr/bin/clang - priority 10
/usr/bin/gcc - priority 20
  slave cc.1.gz: /usr/share/man/man1/gcc.1.gz

$ uname -a
Linux xubu 4.8.0-22-generic #24-Ubuntu SMP Sat Oct 8 09:15:00 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.10
DISTRIB_CODENAME=yakkety
DISTRIB_DESCRIPTION="Ubuntu 16.10"

One big difference from your configuration is this: gcc version 6.2.0 20161005, in 16.04.1 I have gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4). Problem can be considered as solved for now, but be ready for possible surprises in the future (if this was not a bug in toolchain).

Thanks very much!

rdebath commented 7 years ago

Okay, I just did the same to yakkety, looks like the "workaround" is export LDFLAGS=-no-pie

It's the updated ASLR, you know the one that was shown to be (mostly) ineffective even on 64bit systems. There are a couple of distributions where it makes a small difference; Ubuntu is not one of them.

rofl0r commented 7 years ago

@rdebath "updated ASLR" ? is ubuntu 16.10 exporting some weird default CFLAGS, or does it have some hardcoded into their gcc config ?

rdebath commented 7 years ago

On the GCC configure line popfalushi posted there was the tag --enable-default-pie I expect that's what did it.

With normal shared libraries the libraries are PIC code; with that flag set an executable using shared libraries is also PIC. ASLR randomises the position of everything that's PIC, the linker tries to make sure nothing gets screwed by it.

duanev commented 6 years ago

Workaround confirmed for Arch:

$ make clean
$ LDFLAGS=-no-pie make test

passes with flying colors.

$ cc -v
Using built-in specs.
COLLECT_GCC=cc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp
Thread model: posix
gcc version 7.2.1 20171128 (GCC) 

$ python --version
Python 3.6.3

$ ld -V
GNU ld (GNU Binutils) 2.29.1
  Supported emulations:
   elf_x86_64
   elf32_x86_64
   elf_i386
   elf_iamcu
   i386linux
   elf_l1om
   elf_k1om

$ uname -a
Linux djv-ldev 4.14.5-1-ARCH #1 SMP PREEMPT Sun Dec 10 14:50:30 UTC 2017 x86_64 GNU/Linux

$ file test/align.bin 
test/align.bin: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=428f3c827e5ed8faf68729d43f019b0a33edb121, with debug_info, not stripped