SEE TEXT ONLY FreeBSD Toolchain

As with other BSD distributions, FreeBSD has the concept of a base system, an integrated kernel, and core userland, which are developed, tested, and released together by a common team. The userland includes the lan- guage runtime, the build toolchain, basic system utilities, and some third- party libraries and applications. The toolchain includes the and , which translate source code into executable objects, and related utilities for inspecting or modifying those objects.

In addition to this base system, there’s a third-party software ecosystem man- aged through the FreeBSD ports tree and binary packages. Over 26,000 third- party software packages exist in the ports tree. For most of its history FreeBSD relied on the full GNU toolchain suite, includ- ing the GNU Compiler Collection (GCC) compiler and GNU binutils. They served the FreeBSD Project well from 1993 until 2007, when the GNU project migrated to releasing its software under the General Public License, version 3 (GPLv3). In contrast to version 2 of the license, GPLv3 places a number of new restrictions on software users, and FreeBSD developers and users found these objectionable. As a result, the GNU toolchain in FreeBSD was not updated to a new upstream version and quickly became outdated. BY Beginning shortly after that transition, a number of developers in the FreeBSD ED Project started updating components of the FreeBSD toolchain to modern, copy- free alternatives. Some aspects of the toolchain are architecture-dependent, and MASTE in general the tools described below apply to FreeBSD’s Tier-1 architectures. Compiler The LLVM project is a collection of modular and reusable components used to build and other toolchain utilities. It began as a research project at the University of Illinois and has since grown to become a production-quality tool set used in proprietary and open-source software and academic settings. is the C and C++ compiler included in the LLVM suite.

30 FreeBSD Journal We’ve experimented with Clang in FreeBSD (ABIs), other linker optimizations, debugging fea- since 2009, starting with a snapshot from the tures, and much faster link times. upstream project’s Subversion repository. When As with early experiments with Clang, LLD will first added, it was made available as an option to first be added to FreeBSD as an option and will facilitate testing and development, but it was not not be installed as the system linker enabled as the default compiler for some time. (/usr/bin/ld). It will be available by adding When FreeBSD 10 released in January 2014, it -fuse-ld=lld to the compiler’s command included Clang as the system compiler (that is, line arguments (for example, via the CFLAGS installed as /usr/bin/cc) for the 32- and 64- variable). bit x86 architectures. It was later made the We’ve included LLD 3.9 alongside the Clang default for the 32-bit FreeBSD ARM port. The 64- 3.9 update and expect it to be available in bit ARM port began with Clang as the system FreeBSD-CURRENT in October or November compiler. 2016. It is expected to be added to FreeBSD 11.1 Clang includes some support for the MIPS and as well. PowerPC architectures, but is not yet capable of replacing the system compiler there; those archi- Binary Utilities tectures continue to use GCC. The toolchain includes a number of small binary FreeBSD 11 includes Clang 3.8.0, and work is utilities for inspecting or modifying object files, underway in FreeBSD-CURRENT to update to binaries, and libraries. These are tools that report Clang 3.9.0. information about an object, binary, or library, or transform an object’s contents or format. These Linker tools were historically provided by GNU binutils. The linker takes as input individual object files The ELF Tool Chain Project maintains BSD- produced by a compiler or assembler and links licensed implementations of essential compilation them into an executable binary or library. To date tools and libraries for working with ELF object we’ve used the GNU linker in FreeBSD, most files and binaries, and DWARF debugging infor- recently updated to version 2.17.50 in February mation. It began as part of the FreeBSD Project, 2011. This linker lacks link-time optimizations and but became a standalone project in order to facil- support for certain debugging features. itate collaboration with the wider open-source GNU ld in the base system also lacks support community. for AArch64 (arm64) and RISC-V, two CPU archi- For FreeBSD 11 we have migrated to ELF Tool tectures recently added to FreeBSD. Building Chain’s implementation of addr2line, FreeBSD for these currently requires the linker be c++filt, objcopy, , readelf, size, installed from the ports tree or as a package, in , and . The libelf and which case it will be used automatically. libdwarf libraries also come from ELF Tool Chain. The LLVM family of projects includes a linker: ELF Tool Chain also includes a version of LLD. It is a high-performance linker with support FreeBSD’s , brandelf, and elfdump utilities. for ELF, COFF, and Mach-O. Where possible, it We will migrate to those in the future if they gain maintains command-line and functional compati- compelling new features or improvements. bility with existing linkers such as GNU ld, but LLD’s authors are not constrained by strict com- Runtime Libraries patibility where it hampers performance or A number of libraries are required to provide run- desired functionality. time support for the toolchain. The compiler-rt Work began on LLD’s ELF support in July 2015 library comes from the LLVM project and includes and has progressed very quickly; it currently self- several components. Low-level target-specific hosts on several of LLVM’s supported architec- hooks required by GCC or Clang’s code genera- tures and includes almost all of the functionality tion are known as “builtins.” These are routines required to build the FreeBSD base system. that perform operations that the compiler will not In comparison with GNU ld, in FreeBSD LLD emit directly into the output object code. will provide AArch64 and eventually RISC-V sup- Compiler-rt includes sanitizer runtimes that port, full-program Link-Time Optimization (LTO), provide Clang’s runtime support for identifying support for new Application Binary Interfaces

Sept/Oct 2016 31 erroneous software behaviour. AddressSanitizer Tracing (FBT) probes can have typed arguments (asan) detects out-of-bounds access to heap, when the corresponding executable or libraries stack, and globals, use-after-free and related are compiled with CTF. The FreeBSD base system errors, and double and invalid free. It is available build infrastructure includes support for compil- by adding -fsanitize=address to the com- ing with CTF with a setting in the piler’s command line. /etc/src.conf file. The UndefinedBehaviourSanitizer (ubsan) FreeBSD 11 also adds support for Statically catches undefined behaviour; that is, an opera- Defined Tracing (STD) probes in userland. A .d tion for which the language does not have speci- file containing probe definitions can be included fied behaviour. Examples include overflowing in the list of source files, and the build infrastruc- integer operations (such as addition and bit shift- ture will automatically generate a header contain- ing), division by zero, and using uninitialized vari- ing probe macros that can be referenced in ables. The -fsanitize=undefined com- source files. mand line argument enables ubsan. Clang includes other sanitizers in the upstream Future Work repository, including ThreadSanitizer for detecting Work is underway on a number of additional data races in multithreaded applications and toolchain components which will become avail- MemorySanitizer for detecting uninitialized reads. able in FreeBSD in the future. LLVM provides a Work is underway to these available with code coverage tool -cov, which can operate the system compiler in a later FreeBSD version. in a mode similar to GNU gcov, and can operate For C++ runtime support we use a combina- with Clang’s instrumentation-based profiling. tion of PathScale’s libcxxrt for low-level LLVM also provides an OpenMP runtime (libomp), Application Binary Interface (ABI) support. This an Application Programming Interface (API) for includes Run-Time Type Information (RTTI), excep- shared memory multiprocessing. tion handling, and -safe initializers. The With LLD as the linker, two GNU binutils tools C++ standard library is LLVM’s libc++. remain: the assembler (as) and . There is currently no candidate to replace as. However, it is actually not required to build the FreeBSD base A replacement debugger also comes from the system on Tier-1 architectures: Clang’s integrated LLVM project: LLDB. As with the rest of LLVM, assembler is used to build assembly source files. LLDB is built as a set of reusable components, GNU as could simply be removed, or a GNU as- and builds on other parts of LLVM and Clang. compatible driver could be built from the inte- LLDB has access to a full Clang compiler, which it grated assembler. Objdump is also not required uses as the expression parser. This provides high by the base system build and could be removed. fidelity in interpreting the user’s expressions: LLDB LLVM provides an llvm-objdump that is currently is capable of parsing any expression valid in the limited in functionality, but will likely be a viable original source code being debugged. replacement in due course. LLDB is included in FreeBSD 11 for the 32- and Ongoing efforts in the FreeBSD community 64-bit ARM architectures, and 64-bit Intel and in the upstream Clang/LLVM project are (amd64). attempting to bring the Clang, LLD, and LLDB In addition to LLDB in the base system, the suite to the lower-tier FreeBSD architectures that GNU GDB debugger is available by building from are still using GCC today. • the ports tree or installing the package. It has been updated and includes improved threading ED MASTE manages project development for support and kernel debugging. GDB 7.11.1 is the FreeBSD Foundation and works in an engi- available with a simple pkg install gdb neering support role with the University of command. Cambridge Computer Laboratory. He is also a member of the elected FreeBSD Core Team. DTrace Aside from FreeBSD and ELF Tool Chain, he is a FreeBSD 11 includes support for using contributor to a number of other open-source Compressed Type Format (CTF) data in userland. projects, including LLVM, QEMU, and Open This means that userland Function Boundary vSwitch. He lives in Kitchener, Canada, with his wife, Anna, and sons, Pieter and Daniel.

32 FreeBSD Journal THROUGH DECEMBER 2016 BY DRU LAVIGNE Eventsvents Calendaralendar The following BSD-related conferences will take place in November and December 2016. More information about these events, as well as local user group meetings, can be found at www.bsdevents.org.

Meet BSD California • Nov 10 – 12 • Berkeley, CA https://meetbsd.com • The 5th biennial MeetBSD conference will be held at UC Berkeley. This conference provides a mix of formal presentations, unconference activities, and opportunities to network with other BSD users and developers. The BSDA certification exam will be available at this event. Registration is required to attend this conference.

LISA 16 • Dec 4 – 9 • Boston, MA https://www.usenix.org/conference/lisa16/ • The 30th Large Installation System Administration conference will be held at the Sheraton Boston. There will be a FreeBSD booth in the expo area. Registration is required to attend the conference and a nominal fee is required to attend the expo.

Iridium TThankhank yyou!ou! TheThe FreesBSDFreesBSD FoundationFoundation wouldwould lilikeke ttoo acknowledgeacknowledge thethe ffollowingollowing ccompaniesompanies fforor theirtheir ccontinuedontinued ssupportupport ooff thethe PProject.roject. BecauseBecause ooff ggenerousenerous ddonationsonations ssuchuch aass tthesehese wwee aarere ableable ttoo ccontinueontinue momovingving tthehe PProjectroject fforward.orward.

Silver TM

AArere yoyouu a ffanan of FFreeBSD?reeBSD? HHelpelp usus givegive backback toto thethe ProjectProject andand donatedonate today!today! Please check out the full list of generous community ffreebsdfoundation.org/donate/reebsdfoundation.org/donate/ investors at freebsdfoSept/Octundation.org/ d2016onors 33