Good afternoon folks!
I am trying to compile this c source file while performing some CLANG frontend action with a CLANG plugin.
My source code (test.c) includes a header file (section_insertion.h). This header file includes stdio.h because I use some size_t variables inside some structs
test.c
section_insertion.h
Part 1: Building with base CLANG/LLVM.
My base CLANG/LLVM version is the following:
Then, I define the env variables:
needed for the CMake script
Then, building it using the base compiler warns me about not finding the ClangConfig.cmake file
Using the find command doesn't retrieve any file
Part 2: Building with pkg llvm11 version
Installing llvm11 via pkg and checking the installed version
Now, I can find the .cmake file, So, I set the path to it and build.
Part 3: Compile the clang plugin
Part 4: Compile the source code using the clang plugin
Compiling the source code using Clang/LLVM with the plugin, throws me four errors. HOWEVER, it generates the output file into /tmp/test.c
Using the -D __GNUCLIKE_BUILTIN_VARARGS flag, allows me to supress two errors but there are two left.
The error is in /usr/include/sys/_types.h. The relevant lines on that header are:
Although the output file is generated at /tmp/test.c, I am looking to solve those errors.
Does anybody know what I am doing wrong?
I also built the same plugin and test in Ubuntu and no errors were generated.
Thanks for reading and for the help in advance.
EDIT 1: Added test.c code
EDIT 2: Added section_insertion.h code
I am trying to compile this c source file while performing some CLANG frontend action with a CLANG plugin.
My source code (test.c) includes a header file (section_insertion.h). This header file includes stdio.h because I use some size_t variables inside some structs
test.c
C:
// Filename: test.c
#include "section_insertion.h"
void PAYLOAD();
int main(int argc, char *argv[]) {
return 0;
}
section_insertion.h
C:
//Filename: section_insertion.h
#include <stdio.h>
typedef struct {
int num_a;
int num_b;
char caracter;
size_t int_size;
} Payload_A;
Part 1: Building with base CLANG/LLVM.
My base CLANG/LLVM version is the following:
Bash:
$ clang -v
FreeBSD clang version 11.0.1 (git@github.com:llvm/llvm-project.git llvmorg-11.0.1-0-g43ff75f2c3fe)
Target: x86_64-unknown-freebsd13.0
Thread model: posix
InstalledDir: /usr/bin
Then, I define the env variables:
Bash:
$ export Clang_DIR=/usr/local/llvm11
$ export Clang_DIR=/usr/bin/clang
needed for the CMake script
Bash:
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.4.3)
project(clang-tutor-rename-plugin)
#===============================================================================
# 1. LOAD LLVM CONFIGURATION
#===============================================================================
# Set this to a valid LLVM installation dir
set(CT_Clang_INSTALL_DIR "" CACHE PATH "LLVM installation directory")
# Add the location of ClangConfig.cmake to CMake search paths (so that
# find_package can locate it)
list(APPEND CMAKE_PREFIX_PATH "${CT_Clang_INSTALL_DIR}/lib/cmake/clang/")
find_package(Clang REQUIRED CONFIG)
# HelloWorld includes headers from LLVM and Clang - update the include paths
# accordingly
include_directories(SYSTEM "${LLVM_INCLUDE_DIRS};${CLANG_INCLUDE_DIRS}")
#===============================================================================
# 2. LLVM-TUTOR BUILD CONFIGURATION
#===============================================================================
# Use the same C++ standard as LLVM does
set(CMAKE_CXX_STANDARD 14 CACHE STRING "")
# LLVM is normally built without RTTI. Be consistent with that.
if(NOT LLVM_ENABLE_RTTI)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rttic -shared")
endif()
#===============================================================================
# 3. ADD THE TARGET
#===============================================================================
add_library(RenameMacro SHARED RenameMacro.cpp)
# Allow undefined symbols in shared objects on Darwin (this is the default
# behaviour on Linux)
target_link_libraries(RenameMacro
"$<$<PLATFORM_ID:Darwin>:-undefined dynamic_lookup>")
set(CMAKE_VERBOSE_MAKEFILE on)
Then, building it using the base compiler warns me about not finding the ClangConfig.cmake file
Bash:
$ cmake -DCT_Clang_INSTALL_DIR=$Clang_DIR $CLANG_TUTOR_DIR
-- The C compiler identification is Clang 11.0.1
-- The CXX compiler identification is Clang 11.0.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:14 (find_package):
Could not find a package configuration file provided by "Clang" with any of
the following names:
ClangConfig.cmake
clang-config.cmake
Add the installation prefix of "Clang" to CMAKE_PREFIX_PATH or set
"Clang_DIR" to a directory containing one of the above files. If "Clang"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
Using the find command doesn't retrieve any file
Bash:
find -name ClangConfig.cmake
Part 2: Building with pkg llvm11 version
Installing llvm11 via pkg and checking the installed version
Bash:
$ pkg install llvm11
$ clang11 -v
clang version 11.0.1
Target: x86_64-portbld-freebsd13.0
Thread model: posix
InstalledDir: /usr/local/llvm11/bin
Now, I can find the .cmake file, So, I set the path to it and build.
Bash:
$ find -name ClangConfig.cmake
/usr/local/llvm11/lib/cmake/clang/ClangConfig.cmake
$ export Clang_DIR=/usr/local/llvm11
$ cmake -DCT_Clang_INSTALL_DIR=$Clang_DIR $CLANG_TUTOR_DIR
-- Configuring done
-- Generating done
-- Build files have been written to: /home/freebsd/pluginClang/build
Part 3: Compile the clang plugin
Bash:
$ make
make
/usr/local/bin/cmake -S/home/freebsd/pluginClang -B/home/freebsd/pluginClang/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /home/freebsd/pluginClang/build/CMakeFiles /home/freebsd/pluginClang/build//CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make -f CMakeFiles/RenameMacro.dir/build.make CMakeFiles/RenameMacro.dir/depend
cd /home/freebsd/pluginClang/build && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /home/freebsd/pluginClang /home/freebsd/pluginClang /home/freebsd/pluginClang/build /home/freebsd/pluginClang/build /home/freebsd/pluginClang/build/CMakeFiles/RenameMacro.dir/DependInfo.cmake --color=
make -f CMakeFiles/RenameMacro.dir/build.make CMakeFiles/RenameMacro.dir/build
[ 50%] Building CXX object CMakeFiles/RenameMacro.dir/RenameMacro.cpp.o
/usr/bin/c++ -DRenameMacro_EXPORTS -isystem /usr/local/llvm11/include -fPIC -MD -MT CMakeFiles/RenameMacro.dir/RenameMacro.cpp.o -MF CMakeFiles/RenameMacro.dir/RenameMacro.cpp.o.d -o CMakeFiles/RenameMacro.dir/RenameMacro.cpp.o -c /home/freebsd/pluginClang/RenameMacro.cpp
[100%] Linking CXX shared library libRenameMacro.so
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/RenameMacro.dir/link.txt --verbose=1
/usr/bin/c++ -fPIC -shared -Wl,-soname,libRenameMacro.so -o libRenameMacro.so CMakeFiles/RenameMacro.dir/RenameMacro.cpp.o
[100%] Built target RenameMacro
/usr/local/bin/cmake -E cmake_progress_start /home/freebsd/pluginClang/build/CMakeFiles 0
Part 4: Compile the source code using the clang plugin
Bash:
$ ~/pluginClang/build $ ls
CMakeCache.txt Makefile libRenameMacro.so test.c
CMakeFiles cmake_install.cmake section_insertion.h
Bash:
$ clang11 cc1 -load ./libRenameMacro.so -plugin -rename-plugin test.c -v
clang -cc1 version 11.0.1 based upon LLVM 11.0.1 default target x86_64-portbld-freebsd13.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/llvm11/lib/clang/11.0.1/include
/usr/include
End of search list.
In file included from test.c:1:
In file included from ./section_insertion.h:4:
In file included from /usr/include/stdio.h:43:
/usr/include/sys/_types.h:107:24: error: expected ';' at end of declaration list
long long __max_align1 __aligned(_Alignof(long long));
^
/usr/include/sys/_types.h:109:26: error: expected ';' at end of declaration list
long double __max_align2 __aligned(_Alignof(long double));
^
/usr/include/sys/_types.h:135:2: error: "No support for your compiler for stdargs"
#error "No support for your compiler for stdargs"
^
In file included from test.c:1:
In file included from ./section_insertion.h:4:
/usr/include/stdio.h:77:9: error: unknown type name '__va_list'
typedef __va_list va_list;
^
encontro func
Output file created - /tmp/test.c
4 errors generated.
Using the -D __GNUCLIKE_BUILTIN_VARARGS flag, allows me to supress two errors but there are two left.
Bash:
$ clang11 -cc1 -load ./libRenameMacro.so -plugin -rename-plugin test.c -D __GNUCLIKE_BUILTIN_VARARGS -v
clang -cc1 version 11.0.1 based upon LLVM 11.0.1 default target x86_64-portbld-freebsd13.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/llvm11/lib/clang/11.0.1/include
/usr/include
End of search list.
In file included from test.c:1:
In file included from ./section_insertion.h:4:
In file included from /usr/include/stdio.h:43:
/usr/include/sys/_types.h:107:24: error: expected ';' at end of declaration list
long long __max_align1 __aligned(_Alignof(long long));
^
/usr/include/sys/_types.h:109:26: error: expected ';' at end of declaration list
long double __max_align2 __aligned(_Alignof(long double));
^
encontro func
Output file created - /tmp/test.c
2 errors generated.
The error is in /usr/include/sys/_types.h. The relevant lines on that header are:
C:
$ cat /usr/include/sys/_types.h
typedef struct {
long long __max_align1 __aligned(_Alignof(long long));
#ifndef _STANDALONE
long double __max_align2 __aligned(_Alignof(long double));
#endif
} __max_align_t;
Although the output file is generated at /tmp/test.c, I am looking to solve those errors.
Does anybody know what I am doing wrong?
I also built the same plugin and test in Ubuntu and no errors were generated.
Thanks for reading and for the help in advance.
EDIT 1: Added test.c code
EDIT 2: Added section_insertion.h code