This is an archive of the discontinued LLVM Phabricator instance.

[libclang] Fall back to getMainExecutable when dladdr fails
ClosedPublic

Authored by aykevl on May 2 2022, 6:30 PM.

Details

Summary

musl-libc doesn't support dladdr in statically linked binaries:

Are you using static or dynamic linking? If static, dladdr is just a
stub that always fails. It could be implemented to work under some
conditions, but it would be highly dependent on what options you
compile the binary with, since by default static binaries do not
contain the bloat that would be needed to perform introspection.

Source: https://www.openwall.com/lists/musl/2013/01/15/25 (in response to a bug report).

Libclang unfortunately uses dladdr to find the ResourcesPath so will fail if it is linked statically on Alpine Linux. This patch fixes this issue by falling back to getMainExecutable if dladdr returns an error.

Reference: https://github.com/llvm/llvm-project/issues/40641#issuecomment-981011427

Diff Detail

Event Timeline

aykevl created this revision.May 2 2022, 6:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2022, 6:30 PM
aykevl requested review of this revision.May 2 2022, 6:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2022, 6:30 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

I think the file is linked into libclang.so.

The resource directory is relative to`libclang.so` but may not be relative to the main executable... So getMainExecutable will be incorrect.

clang/tools/libclang/CIndexer.cpp
142

Diagnostics generally are not capitalized: https://llvm.org/docs/CodingStandards.html#error-and-warning-messages

You can ignore the numerous anti-examples (I know, lots of inconsistency)...

aykevl added a comment.EditedMay 11 2022, 9:46 AM

This file is also linked into libclang.a.

When linking libclang.a on Alpine Linux, dladdr is not available and the only fallback I can think of is getMainExecutable.

I'm open to other solutions. The code as-is essentially compiles to a segmentation fault, which is bad.

MaskRay accepted this revision.May 11 2022, 10:12 AM
This revision is now accepted and ready to land.May 11 2022, 10:12 AM
This revision was landed with ongoing or failed builds.May 29 2022, 4:41 AM
This revision was automatically updated to reflect the committed changes.