From owner-freebsd-hackers@freebsd.org Wed Jan 4 19:55:57 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A6469C9F86B for ; Wed, 4 Jan 2017 19:55:57 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4FBD31979 for ; Wed, 4 Jan 2017 19:55:57 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id v04JtpWK007112 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 4 Jan 2017 21:55:52 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua v04JtpWK007112 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id v04JtpeP007111; Wed, 4 Jan 2017 21:55:51 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 4 Jan 2017 21:55:51 +0200 From: Konstantin Belousov To: Derrick McKee Cc: freebsd-hackers@freebsd.org Subject: Re: Using non-standard libc Message-ID: <20170104195551.GA2533@kib.kiev.ua> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.7.2 (2016-11-26) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on tom.home X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Jan 2017 19:55:57 -0000 On Wed, Jan 04, 2017 at 06:05:03PM +0000, Derrick McKee wrote: > Hi, > > I am conducting research into memory safety, and I have developed a custom > LLVM pass. I have successfully built libc using my pass. However, when I > try to compile a helloworld program using my built libc, I get a whole > bunch of undefined reference errors: > > /path/to/custom/clang -static -nostdlib -L/usr/src/lib/libc -lc > -I/usr/src/include hello.c > > /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to > 0000000000400160 > /usr/src/lib/libc/libc.a(getenv.o): In function `__clean_env': > /usr/src/lib/libc/stdlib/getenv.c:(.text+0x168): undefined reference to > `environ' > /usr/src/lib/libc/stdlib/getenv.c:(.text+0x171): undefined reference to > `environ' > /usr/src/lib/libc/libc.a(getenv.o): In function `getenv': > /usr/src/lib/libc/stdlib/getenv.c:(.text+0x1f6): undefined reference to > `environ' > /usr/src/lib/libc/libc.a(getenv.o): In function `__merge_environ': > /usr/src/lib/libc/stdlib/getenv.c:(.text+0x42d): undefined reference to > `environ' > /usr/src/lib/libc/stdlib/getenv.c:(.text+0x459): undefined reference to > `environ' > /usr/src/lib/libc/libc.a(getenv.o):/usr/src/lib/libc/stdlib/getenv.c:(.text+0x524): > more undefined references to `environ' follow > /usr/src/lib/libc/libc.a(getprogname.o): In function `getprogname': > /usr/src/lib/libc/gen/getprogname.c:(.text+0x7): undefined reference to > `__progname' > /usr/src/lib/libc/libc.a(auxv.o): In function `init_aux_vector_once': > /usr/src/lib/libc/gen/auxv.c:(.text+0x27): undefined reference to `environ' > /usr/src/lib/libc/libc.a(exec.o): In function `execl': > /usr/src/lib/libc/gen/exec.c:(.text+0x154): undefined reference to `environ' > /usr/src/lib/libc/libc.a(exec.o): In function `execlp': > /usr/src/lib/libc/gen/exec.c:(.text+0x484): undefined reference to `environ' > /usr/src/lib/libc/libc.a(exec.o): In function `execvp': > /usr/src/lib/libc/gen/exec.c:(.text+0x4e3): undefined reference to `environ' > /usr/src/lib/libc/libc.a(exec.o): In function `execv': > /usr/src/lib/libc/gen/exec.c:(.text+0x537): undefined reference to `environ' > /usr/src/lib/libc/libc.a(exec.o):/usr/src/lib/libc/gen/exec.c:(.text+0x5b7): > more undefined references to `environ' follow > clang-4.0: error: linker command failed with exit code 1 (use -v to see > invocation) > > Any idea of what I am missing? Thanks. You are missing the so-called crt files which ensure that the resulting binary follows ABI etc. Easier way for you to see what is going on is to compile trivial program in the verbose cc driver mode: cc -v -o hello hello.c You will see what are the actual tools invocation, and in particular, how the actual linking is performed. Then you could replace system libc reference with your own lib by invoking ld manually.