Skip to content

Commit

Permalink
Remove runtime dependency on gcc stdarg headers and make
Browse files Browse the repository at this point in the history
Remove the runtime dependency on gcc's stdarg.h. Do this by packaging
the (non-GPL) version shipped with clang. This allows the bulk of
kbuild_helper to be removed, at the expense of hardcoding the kernel
include paths. If in the future the kernel make system changes again to
require different -I paths, we'll have to think this through again.

Signed-off-by: Brenden Blanco <[email protected]>
  • Loading branch information
Brenden Blanco committed Jan 4, 2016
1 parent 6ae08b6 commit 78db178
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 74 deletions.
2 changes: 1 addition & 1 deletion SPECS/bcc.el6.spec
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ make install/strip DESTDIR=%{buildroot}

%package -n libbcc
Summary: Shared Library for BPF Compiler Collection (BCC)
Requires: make, gcc
%description -n libbcc
Shared Library for BPF Compiler Collection (BCC)

Expand All @@ -109,6 +108,7 @@ Python bindings for BPF Compiler Collection (BCC)
%files -n libbcc
/usr/lib64/*
/usr/share/bcc/include/*
/usr/share/bcc/lib/*
/usr/include/bcc/*

%files -n libbcc-examples
Expand Down
2 changes: 1 addition & 1 deletion SPECS/bcc.el6.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ make install/strip DESTDIR=%{buildroot}

%package -n libbcc
Summary: Shared Library for BPF Compiler Collection (BCC)
Requires: make, gcc
%description -n libbcc
Shared Library for BPF Compiler Collection (BCC)

Expand All @@ -109,6 +108,7 @@ Python bindings for BPF Compiler Collection (BCC)
%files -n libbcc
/usr/lib64/*
/usr/share/bcc/include/*
/usr/share/bcc/lib/*
/usr/include/bcc/*

%files -n libbcc-examples
Expand Down
2 changes: 1 addition & 1 deletion SPECS/bcc.el7.spec
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ make install/strip DESTDIR=%{buildroot}

%package -n libbcc
Summary: Shared Library for BPF Compiler Collection (BCC)
Requires: make, gcc
%description -n libbcc
Shared Library for BPF Compiler Collection (BCC)

Expand All @@ -78,6 +77,7 @@ Python bindings for BPF Compiler Collection (BCC)
%files -n libbcc
/usr/lib64/*
/usr/share/bcc/include/*
/usr/share/bcc/lib/*
/usr/include/bcc/*

%files -n libbcc-examples
Expand Down
2 changes: 1 addition & 1 deletion SPECS/bcc.el7.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ make install/strip DESTDIR=%{buildroot}

%package -n libbcc
Summary: Shared Library for BPF Compiler Collection (BCC)
Requires: make, gcc
%description -n libbcc
Shared Library for BPF Compiler Collection (BCC)

Expand All @@ -78,6 +77,7 @@ Python bindings for BPF Compiler Collection (BCC)
%files -n libbcc
/usr/lib64/*
/usr/share/bcc/include/*
/usr/share/bcc/lib/*
/usr/include/bcc/*

%files -n libbcc-examples
Expand Down
2 changes: 1 addition & 1 deletion SPECS/bcc.f22.spec
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ make install/strip DESTDIR=%{buildroot}

%package -n libbcc
Summary: Shared Library for BPF Compiler Collection (BCC)
Requires: make, gcc
%description -n libbcc
Shared Library for BPF Compiler Collection (BCC)

Expand All @@ -78,6 +77,7 @@ Python bindings for BPF Compiler Collection (BCC)
%files -n libbcc
/usr/lib64/*
/usr/share/bcc/include/*
/usr/share/bcc/lib/*
/usr/include/bcc/*

%files -n libbcc-examples
Expand Down
2 changes: 1 addition & 1 deletion SPECS/bcc.f22.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ make install/strip DESTDIR=%{buildroot}

%package -n libbcc
Summary: Shared Library for BPF Compiler Collection (BCC)
Requires: make, gcc
%description -n libbcc
Shared Library for BPF Compiler Collection (BCC)

Expand All @@ -78,6 +77,7 @@ Python bindings for BPF Compiler Collection (BCC)
%files -n libbcc
/usr/lib64/*
/usr/share/bcc/include/*
/usr/share/bcc/lib/*
/usr/include/bcc/*

%files -n libbcc-examples
Expand Down
2 changes: 1 addition & 1 deletion SPECS/bcc.spec
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ make install/strip DESTDIR=%{buildroot}

%package -n libbcc
Summary: Shared Library for BPF Compiler Collection (BCC)
Requires: gcc, make
%description -n libbcc
Shared Library for BPF Compiler Collection (BCC)

Expand Down Expand Up @@ -73,6 +72,7 @@ Command line tools for BPF Compiler Collection (BCC)
%files -n libbcc
/usr/lib64/*
/usr/share/bcc/include/*
/usr/share/bcc/lib/*
/usr/include/bcc/*

%files -n libbcc-examples
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Homepage: https://github.com/iovisor/bcc

Package: libbcc
Architecture: amd64
Depends: libc6, libstdc++6, make, gcc
Depends: libc6, libstdc++6
Description: Shared Library for BPF Compiler Collection (BCC)
Shared Library for BPF Compiler Collection to control BPF programs
from userspace.
Expand Down
1 change: 1 addition & 0 deletions debian/libbcc.install
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
usr/include/bcc/*
usr/lib/x86_64-linux-gnu/libbcc*
usr/share/bcc/include/*
usr/share/bcc/lib/*
3 changes: 3 additions & 0 deletions src/cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,8 @@ install(DIRECTORY compat/linux/ COMPONENT libbcc
FILES_MATCHING PATTERN "*.h")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libbcc.pc COMPONENT libbcc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
install(DIRECTORY clang COMPONENT libbcc
DESTINATION share/bcc/lib
FILES_MATCHING PATTERN "*.h")

add_subdirectory(frontends)
52 changes: 52 additions & 0 deletions src/cc/clang/include/stdarg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*===---- stdarg.h - Variable argument handling ----------------------------===
*
* Copyright (c) 2008 Eli Friedman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*===-----------------------------------------------------------------------===
*/

#ifndef __STDARG_H
#define __STDARG_H

#ifndef _VA_LIST
typedef __builtin_va_list va_list;
#define _VA_LIST
#endif
#define va_start(ap, param) __builtin_va_start(ap, param)
#define va_end(ap) __builtin_va_end(ap)
#define va_arg(ap, type) __builtin_va_arg(ap, type)

/* GCC always defines __va_copy, but does not define va_copy unless in c99 mode
* or -ansi is not specified, since it was not part of C90.
*/
#define __va_copy(d,s) __builtin_va_copy(d,s)

#if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L || !defined(__STRICT_ANSI__)
#define va_copy(dest, src) __builtin_va_copy(dest, src)
#endif

/* Hack required to make standard headers work, at least on Ubuntu */
#ifndef __GNUC_VA_LIST
#define __GNUC_VA_LIST 1
#endif
typedef __builtin_va_list __gnuc_va_list;

#endif /* __STDARG_H */
104 changes: 44 additions & 60 deletions src/cc/frontends/clang/kbuild_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,70 +23,54 @@ using std::string;
using std::vector;

KBuildHelper::KBuildHelper() {
char *home = ::getenv("HOME");
if (home)
cache_dir_ = string(home) + "/.cache/bcc";
else
cache_dir_ = "/var/run/bcc";
}

// Makefile helper for kbuild_flags
int KBuildHelper::learn_flags(const string &tmpdir, const char *uname_release, const char *cachefile) {
{
// Create a kbuild file to generate the flags
string makefile = tmpdir + "/Makefile";
FILEPtr mf(::fopen(makefile.c_str(), "w"));
if (!mf)
return -1;
fprintf(&*mf, "obj-y := dummy.o\n");
fprintf(&*mf, "CACHEDIR=$(dir %s)\n", cachefile);
fprintf(&*mf, "$(CACHEDIR):\n");
fprintf(&*mf, "\t@mkdir -p $(CACHEDIR)\n");
fprintf(&*mf, "$(obj)/%%.o: $(src)/%%.c $(CACHEDIR)\n");
fprintf(&*mf, "\t@echo -n \"$(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) "
"-D__KERNEL__ -Wno-unused-value -Wno-pointer-sign \" > %s\n", cachefile);
}
{
string cfile = tmpdir + "/dummy.c";
FILEPtr cf(::fopen(cfile.c_str(), "w"));
if (!cf)
return -1;
}
string cmd = "make CROSS_COMPILE= -s";
cmd += " -C " KERNEL_MODULES_DIR "/" + string(uname_release) + "/build";
cmd += " M=" + tmpdir + " dummy.o";
int rc = ::system(cmd.c_str());
if (rc < 0) {
::perror("system");
return -1;
}
return ::open(cachefile, O_RDONLY);
}

// read the flags from cache or learn
int KBuildHelper::get_flags(const char *uname_release, vector<string> *cflags) {
char cachefile[256];
snprintf(cachefile, sizeof(cachefile), "%s/%s.flags", cache_dir_.c_str(), uname_release);
int cachefd = ::open(cachefile, O_RDONLY);
if (cachefd < 0) {
TmpDir tmpdir;
if (!tmpdir.ok())
return -1;
cachefd = learn_flags(tmpdir.str(), uname_release, cachefile);
if (cachefd < 0)
return -1;
}
FILEPtr f(::fdopen(cachefd, "r"));
size_t len = 0;
char *line = NULL;
ssize_t nread;
while ((nread = getdelim(&line, &len, ' ', &*f)) >= 0) {
if (nread == 0 || (nread == 1 && line[0] == ' ')) continue;
if (line[nread - 1] == ' ')
--nread;
cflags->push_back(string(line, nread));
int KBuildHelper::get_flags(const char *uname_machine, vector<string> *cflags) {
//uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/sun4u/sparc64/ -e s/arm.*/arm/
// -e s/sa110/arm/ -e s/s390x/s390/ -e s/parisc64/parisc/
// -e s/ppc.*/powerpc/ -e s/mips.*/mips/ -e s/sh[234].*/sh/
// -e s/aarch64.*/arm64/

string arch = uname_machine;
if (!strncmp(uname_machine, "x86_64", 6)) {
arch = "x86";
} else if (uname_machine[0] == 'i' && !strncmp(&uname_machine[2], "86", 2)) {
arch = "x86";
} else if (!strncmp(uname_machine, "arm", 3)) {
arch = "arm";
} else if (!strncmp(uname_machine, "sa110", 5)) {
arch = "arm";
} else if (!strncmp(uname_machine, "s390x", 5)) {
arch = "s390";
} else if (!strncmp(uname_machine, "parisc64", 8)) {
arch = "parisc";
} else if (!strncmp(uname_machine, "ppc", 3)) {
arch = "powerpc";
} else if (!strncmp(uname_machine, "mips", 4)) {
arch = "mips";
} else if (!strncmp(uname_machine, "sh", 2)) {
arch = "sh";
} else if (!strncmp(uname_machine, "aarch64", 7)) {
arch = "arm64";
}
free(line);

cflags->push_back("-nostdinc");
cflags->push_back("-isystem");
cflags->push_back(BCC_INSTALL_PREFIX "/share/bcc/lib/clang/include");
cflags->push_back("-I./arch/"+arch+"/include");
cflags->push_back("-Iarch/"+arch+"/include/generated/uapi");
cflags->push_back("-Iarch/"+arch+"/include/generated");
cflags->push_back("-Iinclude");
cflags->push_back("-I./arch/"+arch+"/include/uapi");
cflags->push_back("-Iarch/"+arch+"/include/generated/uapi");
cflags->push_back("-I./include/uapi");
cflags->push_back("-Iinclude/generated/uapi");
cflags->push_back("-include");
cflags->push_back("./include/linux/kconfig.h");
cflags->push_back("-D__KERNEL__");
cflags->push_back("-Wno-unused-value");
cflags->push_back("-Wno-pointer-sign");
return 0;
}

Expand Down
6 changes: 1 addition & 5 deletions src/cc/frontends/clang/kbuild_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,9 @@ class TmpDir {
// Note: Depending on environment, different cache locations may be desired. In
// case we eventually support non-root user programs, cache in $HOME.
class KBuildHelper {
private:
int learn_flags(const std::string &tmpdir, const char *uname_release, const char *cachefile);
public:
KBuildHelper();
int get_flags(const char *uname_release, std::vector<std::string> *cflags);
private:
std::string cache_dir_;
int get_flags(const char *uname_machine, std::vector<std::string> *cflags);
};

} // namespace ebpf
3 changes: 2 additions & 1 deletion src/cc/frontends/clang/loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ int ClangLoader::parse(unique_ptr<llvm::Module> *mod, unique_ptr<vector<TableDes

vector<const char *> flags_cstr({"-O0", "-emit-llvm", "-I", dstack.cwd(),
"-Wno-deprecated-declarations",
"-Wno-gnu-variable-sized-type-not-at-end",
"-x", "c", "-c", abs_file.c_str()});

KBuildHelper kbuild_helper;
vector<string> kflags;
if (kbuild_helper.get_flags(un.release, &kflags))
if (kbuild_helper.get_flags(un.machine, &kflags))
return -1;
kflags.push_back("-include");
kflags.push_back(BCC_INSTALL_PREFIX "/share/bcc/include/bcc/helpers.h");
Expand Down

0 comments on commit 78db178

Please sign in to comment.