[SERVER-32425] How to build MongoDB as a static executable Created: 20/Dec/17  Updated: 27/Oct/23  Resolved: 12/Jan/18

Status: Closed
Project: Core Server
Component/s: Build
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: Praveen Arkeri Assignee: Andrew Morrow (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

I wanted to know if its possible to build 'mongod' statically. I am using following command and I see that build is success full but binary is still dynamically linked..

scons --link-model=static -j 44 CCFLAGS=" -march=armv8-a+crc -static" --disable-warnings-as-errors

Any inputs on what compile options I should use to get statically linked executable?



 Comments   
Comment by Andrew Morrow (Inactive) [ 12/Jan/18 ]

Hi -

I'm closing out this ticket since I haven't heard back from you. Feel free to update this ticket if you have any further questions.

Comment by Andrew Morrow (Inactive) [ 04/Jan/18 ]

No idea about the relocation problems in libresolve.a: that looks like a platform issue that we can't really control. The warning about dlopen is pointing out that, as I stated above, dlopen is going to be weird in a fully static build. Perhaps it is not a problem unless you actually call dlopen? If you want that warning to not break the build, you can build with --disable-warnings-as-errors. You will still need to figure out the libresolv problem though.

In general, static linking is deprecated on linux, and we make no effort to test or support fully static builds, so you are fighting an uphill battle here. You may well need to prune parts of the codebase out (shared_library_posix.cpp, whatever depends on libresolv) to make it work, sort of.

Comment by Praveen Arkeri [ 04/Jan/18 ]

@AndrewM
Motivation to have static binary is that we can use one binary to analyze performance on various platforms (silicon, instruction set simulators, cpu models, etc...). At times a window of performance critical code can be extracted and anlaysed for better understanding.

I added dependency of library 'dl' in top level Sconstruct file which resolved previous errors but few new ones have popped up now:
Change:

 if env.TargetOSIs('linux'):
       env.Append( LIBS=['m',"resolv", "dl"] )

Errors:

build/opt/mongo/platform/shared_library_posix.o: In function `mongo::SharedLibrary::create(boost::filesystem::path const&)':
src/mongo/platform/shared_library_posix.cpp:56: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/5/../../../aarch64-linux-gnu/libresolv.a(res_query.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against external symbol `__stack_chk_guard@@GLIBC_2.17' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/5/../../../aarch64-linux-gnu/libresolv.a(res_query.o)(.text+0x58): unresolvable R_AARCH64_ADR_PREL_PG_HI21 relocation against symbol `__stack_chk_guard@@GLIBC_2.17'
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

Comment by Andrew Morrow (Inactive) [ 21/Dec/17 ]

Well, I think you fixed the first one by saying -static-libgcc. However, the other issue looks more serious. Can you build with VERBOSE=1, and see if the dl library is on the link line for mongod? If not, you could try adding LIBS="dl" to the SCons invocation. However, I'm not entirely sure what it means to use the libdl libraries from what is supposed to be a fully static build.

It may be that this just isn't a supported configuration. So, let me ask: why do you want to build a fully static version of mongod? If you build it on a given linux distro, the dynamic build you get by default only links to system C ABI libraries, so should be moveable to any other ABI compatible version of that distro. Why is a static build needed?

Comment by Praveen Arkeri [ 21/Dec/17 ]

Hi Andrew,

Thanks for your inputs, I removed -static flag from CCFLAGS and added it to LINKFLAGS but I see more errors.
Below you can see various options which I tried with LINKFLAGS and their respective error messages:

LINKFLAGS=" -Wl,-static"

  • cannot find -lgcc_s

LINKFLAGS=" -Wl,-static -static-libgcc"

  • undefined reference to `dl_iterate_phdr

LINKFLAGS="-Wl,-static -static-libgcc -static-libstdc++"

  • build/opt/mongo/util/stacktrace_posix.o: In function `mongo::printStackTrace(std::ostream&)':
    • mongo/src/mongo/util/stacktrace_posix.cpp:187: undefined reference to `dladdr'
  • build/opt/mongo/platform/shared_library_posix.o: In function `mongo::SharedLibrary::~SharedLibrary()':
    • mongo/src/mongo/platform/shared_library_posix.cpp:46: undefined reference to `dlclose'

Am I missing any other flags?

Comment by Andrew Morrow (Inactive) [ 20/Dec/17 ]

I'm assuming that you are finding that the executable is still linked to system dynamic libraries? Given the -link-model=static I wouldn't expect to see any shared libraries of mongodb code involved. I think the issue may be that you are passing -static to the compiler via CCFLAGS but not to the linker, where it is actually required, so you end up depending on libc.so, for instance. Can you try removing -static from CCFLAGS and moving it to LINKFLAGS, like scons --link-model=static -j 44 CCFLAGS=" -march=armv8-a+crc" --disable-warnings-as-errors LINKFLAGS="-Wl,-static".

Please be aware that fully statically linked executables aren't necessarily supported - things like nsswitch and pam may not be fully supported by a link to the static C runtime.

Generated at Thu Feb 08 04:30:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.