0

I'm contributing to one open-source project named AMX Mod X. It uses really simple, custom-made build system (called AMBuild). Project is built using Clang on Linux.

I tried to enable LTO for it, just by passing compile-time and link-time arguments.

As a link time arguments I added

-O2 -flto=thin -fuse-ld=lld -Wl,-z,notext

And as a compile-time arguments.

-O2 -flto=thin -fPIC

I selected this configuration after careful googling. It seems I'm very close to achieving complete LTO. Only one module fails to build, it links against pcre.

Generated (by build system) linking command is like this.

clang-4.0 _public_sdk_amxxmodule.o module.o CRegEx.o utils.o -m32 -O2 -flto=thin -fuse-ld=lld -Wl,-z,notext -ldl -lm -lgcc_eh ../../../support/versionlib/version/libversion.a /home/travis/build/alliedmodders/amxmodx/modules/regex/lib_linux/libpcre.a -shared -o regex_amxx_i386.so

And linking errors are many, but they are very similar. This is one of them.

/usr/lib/llvm-4.0/bin/ld.lld: error: pcre_compile.c:(function add_to_class): can't create dynamic relocation R_386_32 against symbol '_pcre_ucd_stage1' defined in /home/travis/build/alliedmodders/amxmodx/modules/regex/lib_linux/libpcre.a(libpcre_la-pcre_ucd.o)

I read that LLD and bfd have different defaults and -Wl,-z,notext should work, but it didn't have any effect.

libpcre.a is just a binary file bundled with repository. AMBuild isn't building pcre from sources. This is my PR, you can find there failed travis build and read logs more carefully. Don't be afraid against custom build system, all executed commands are printed to the terminal in the most verbose form. It shouldn't be problem.

How to enable LTO when linking against pcre? I don't really care about other compilers (gcc, icc, etc..) or other operating systems.

UPD: LTO stands for Link Time Optimization

Inline
  • 2,566
  • 1
  • 16
  • 32
  • Based on the command line snippets, you are mixing and matching `-m32` and native or `-m64`. All projects need to be built with the same `CFLAGS` and `CXXFLAGS`. Some flags are less important than others. But flags like `-fPIC`, `-m32` and `-flto` are important and should be used consistently. Regarding `pcre_compile.c`, it looks like it is missing `-fPIC` based on the snippets. (Also, PCRE uses both `CFLAGS` and `CXXFLAGS`. Be sure to set both, and not just `CFLAGS`). – jww Feb 09 '19 at 13:12
  • Ditto jww said. LTO (and WPO) really, really depend on all the flags being set the same. (Including the -DWAZZIT defines.) Things can go sour fast if they're not the same. Yes, some flags are irrelevant, but it's best to have all the flags exactly the same, and in the same order. It will save you headache and anguish. – Eljay Feb 09 '19 at 13:35

0 Answers0