blob: 75c0ed9f830f730b6ae9347eb4f094d125965a99 [file] [log] [blame]
Dan Liewd3c33112016-06-02 05:48:02 +00001//===- FuzzerExtFunctionsWeak.cpp - Interface to external functions -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// Implementation for Linux. This relies on the linker's support for weak
10// symbols. We don't use this approach on Apple platforms because it requires
11// clients of LibFuzzer to pass ``-U _<symbol_name>`` to the linker to allow
12// weak symbols to be undefined. That is a complication we don't want to expose
13// to clients right now.
14//===----------------------------------------------------------------------===//
15#include "FuzzerInternal.h"
16#if LIBFUZZER_LINUX
17
18#include "FuzzerExtFunctions.h"
19
20extern "C" {
21// Declare these symbols as weak to allow them to be optionally defined.
22#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
23 __attribute__((weak)) RETURN_TYPE NAME FUNC_SIG
24
25#include "FuzzerExtFunctions.def"
26
27#undef EXT_FUNC
28}
29
30using namespace fuzzer;
31
32static void CheckFnPtr(void *FnPtr, const char *FnName, bool WarnIfMissing) {
33 if (FnPtr == nullptr && WarnIfMissing) {
34 Printf("WARNING: Failed to find function \"%s\".\n", FnName);
35 }
36}
37
38namespace fuzzer {
39
40ExternalFunctions::ExternalFunctions() {
41#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
42 this->NAME = ::NAME; \
43 CheckFnPtr((void *)::NAME, #NAME, WARN);
44
45#include "FuzzerExtFunctions.def"
46
47#undef EXT_FUNC
48}
49} // namespace fuzzer
50#endif // LIBFUZZER_LINUX