Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faq: adds section about linkers errors when using linkers incompatibl… #163

Merged

Conversation

hbobenicio
Copy link
Contributor

Issue #, if available: #151

Description of changes:

Adds an entry to the FAQ & Troubleshooting section about linking errors when using a linker incompatible with the IR used by the compiler, when ENABLE_LTO=ON (which is the default option).

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@hbobenicio
Copy link
Contributor Author

hbobenicio commented Aug 29, 2022

Please, feel free to modify and/or rephrase anything as desired. This is just an initial suggestion trying to help people getting linking errors like the one described in the related issue.

@hbobenicio hbobenicio force-pushed the faq-troubleshooting-lto-linking-error branch from bd9c85b to b58d862 Compare August 29, 2022 15:52
@bmoffatt
Copy link
Collaborator

LGTM! Thanks for the contribution!

@bmoffatt bmoffatt merged commit 9e2c1a3 into awslabs:master Aug 30, 2022
Copy link
Contributor

@marcomagdy marcomagdy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hbobenicio did you mix compilers when you encountered this error?
I've compiled with clang and gcc with LTO turned on and never had a problem.
Mixing compilers is not a supported use-case and generally very bad practice.

@@ -214,6 +214,8 @@ curl_easy_setopt(curl_handle, CURLOPT_CAINFO, "/etc/pki/tls/certs/ca-bundle.crt"
- You can try hitting the non-TLS version of the endpoint if available. (Not Recommended).
1. **No known conversion between `std::string` and `Aws::String`**
- Either turn off custom memory management in the AWS C++ SDK or build it as a static library (`-DBUILD_SHARED_LIBS=OFF`)
1. **I'm getting wierd linking errors like `/usr/bin/ld: /home/<...>bin/lib/libaws-lambda-runtime.a: error adding symbols: file format not recognized clang: error: linker command failed with exit code 1 (use -v to see invocation)`**
Copy link
Contributor

@marcomagdy marcomagdy Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hbobenicio can you fix this typo wierd -> weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops :)
Nice catch!

@@ -214,6 +214,8 @@ curl_easy_setopt(curl_handle, CURLOPT_CAINFO, "/etc/pki/tls/certs/ca-bundle.crt"
- You can try hitting the non-TLS version of the endpoint if available. (Not Recommended).
1. **No known conversion between `std::string` and `Aws::String`**
- Either turn off custom memory management in the AWS C++ SDK or build it as a static library (`-DBUILD_SHARED_LIBS=OFF`)
1. **I'm getting wierd linking errors like `/usr/bin/ld: /home/<...>bin/lib/libaws-lambda-runtime.a: error adding symbols: file format not recognized clang: error: linker command failed with exit code 1 (use -v to see invocation)`**
- By default this project enables _link-time optimization (LTO)_ (configurable with `ENABLE_LTO` _cmake_ option). This tells compilers to delay optimizations until the linking phase. For some compilers (like _clang_) this means that each compilation unit should be compiled down only to intermediate representations (_e.g._ _clang_ uses _LLVM IR_) instead of directly to native object files. As a side-effect, library archives (like _aws-lambda-runtime_) would also contain only _IR_ code. This may result in linker errors like the above if you use a linker incompatible with the _IR_ code generated by your compiler (e.g. _GNU ld_ linker doesn't support _LLVM IR_). In order for the linking step to succeed with _LTO_ you must either use an appropriate linker with something like `-DCMAKE_CXX_FLAGS="-fuse-ld=lld"` (_GNU ld_ for _gcc_ and _lld_ for _clang_ are known to work) or disable _LTO_ (`-DENABLE_LTO=OFF`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this means that each compilation unit should be compiled down only to intermediate representations (e.g. clang uses LLVM IR) instead of directly to native object files.

Moreover, I'd rather avoid making such claims on how the linker/compiler works because:

  • I'm not an expert so I really don't know.
  • If that information gets outdated in the future, we don't end up with stale or wrong claims.

I think it's better to suggest turning off LTO and keep it succinct. That's assuming the problem exists, which I'm not yet sure is that case.

Copy link
Contributor Author

@hbobenicio hbobenicio Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agreeded. You're right. I'm gonna try to rephrase that tomorrow. This became too assertive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!
Also, it would be nice if you can provide me with steps on triggering this problem. I can't reproduce it.

Copy link
Contributor Author

@hbobenicio hbobenicio Sep 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Working on it

Copy link
Contributor Author

@hbobenicio hbobenicio Sep 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The dockerfile is on the issue #151

@hbobenicio
Copy link
Contributor Author

hbobenicio commented Aug 30, 2022

@hbobenicio did you mix compilers when you encountered this error?

Not actually.
What I have configured here in my environment is clang as my default compiler (CC and CXX env vars also set). But I didn't changed the default linker (I didn't set symlinks to /usr/bin/ld or set LD env var).
So the default linker for my environment is still GNU's ld linker.

I've compiled with clang and gcc with LTO turned on and never had a problem.

Yeah... I got this compiling with gcc/LTO/ld and clang/LTO/lld too.
The problem is with clang/LTO/ld which doesn't seem to work.
Because clang with LTO outputs LLVM IR bitcode and GNU ld doesn't know how to deal with it.
So probably you were linking with lld when you were using clang I would guess (or gold or ld64 which are also supported)

Mixing compilers is not a supported use-case and generally very bad practice.

Sure. C ABI's is actually pretty standard and portable, but sometimes it can get really messy.
It's always much better to use just a single compiler for your whole build pipeline and actually I'm using just one too :)
The problem is with clang's LTO linker support:

@marcomagdy
Copy link
Contributor

But I didn't changed the default linker (I didn't set symlinks to /usr/bin/ld or set LD env var).
So the default linker for my environment is still GNU's ld linker.

I don't recall having to change the linker when I compiled with clang and gcc. I'm still skeptical. Maybe @bmoffatt can try to reproduce it. Unfortunately, I don't have access to a Linux box/container right now.

However, seeing this:

/usr/bin/ld: /home/<...>bin/lib/libaws-lambda-runtime.a:

tells me that the runtime is built as a static-library, so no linking step was involved. The linking happened later in your application. And either your toolchain is somehow tangled up, or if it is indeed a problem that @bmoffatt can reproduce then we should turn off LTO when -DBUILD_SHARED_LIBS=OFF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants