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

__attribute__((used)) possibly ignored for static function with inline assembler? #21666

Closed
llvmbot opened this issue Oct 16, 2014 · 8 comments
Closed
Labels
bugzilla Issues migrated from bugzilla clang:codegen

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Oct 16, 2014

Bugzilla Link 21292
Resolution FIXED
Resolved on Dec 22, 2015 08:42
Version trunk
OS Linux
Blocks #21794
Reporter LLVM Bugzilla Contributor
CC @echristo,@rengolin,@stephenhines

Extended Description

I've come across what looks like a bug in the integrated assembler. It looks like attribute((used)) is being ignored.

We're seeing the following link error:
cxa_exception.o(.text.__cxa_end_cleanup+0x2): warning: relocation refers to discarded section

The relocation at .text.__cxa_end_cleanup+0x2 is to:
https://github.com/llvm-mirror/libcxxabi/blob/master/src/cxa_exception.cpp#L314

Removing the static keyword allows it to pass, and removing the attribute results in an undefined reference, so clearly used is having some effect.

Our compilation line (pruned to remove the uninteresting android parts):

clang++ -I external/libcxxabi/include/ -I external/libcxx/include/ -I external/libcxxabi -c -msoft-float -ffunction-sections -fdata-sections -funwind-tables -fstack-protector -fno-short-enums -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=neon -fmessage-length=0 -fvisibility-inlines-hidden -target arm-linux-androideabi -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -frtti -fPIC -std=c++11 -fexceptions -integrated-as external/libcxxabi/src/cxa_exception.cpp

More discussion: https://android-review.googlesource.com/#/c/110170/

Possibly related:
http://llvm.org/bugs/show_bug.cgi?id=19743
http://llvm.org/bugs/show_bug.cgi?id=17480

Our clang is based on r212749, so it's possible that this has been fixed already.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Mar 31, 2015

today I looked into this issue , but it's not reproducible from top of llvm/clang trunk. I can find both __cxa_end_cleanup and __cxa_end_cleanup_impl function bodies from the generated assembly code, and I can build libcxxabi with '-fintegrated-as'. I use scripts from https://github.com/loganchien/libcxx-scripts to cross-compile it for ARM target from a X86 host. If the build step is correct, probably it has been already fixed.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Apr 2, 2015

I just checked and it's still affecting Android (reverting https://android-review.googlesource.com/#/c/110170/ and trying to build libc++ for the generic ARM target is enough to repro).

Will have to do some more digging to figure out what we do differently that is causing this though.

@echristo
Copy link
Contributor

echristo commented Apr 2, 2015

Testcase? This isn't a bug report without one.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Dec 16, 2015

This is caused by the fact that we use -ffunction-sections -fdata-sections and -Wl,--gc-sections in the compiler flags.

__cxa_end_cleanup ends up being thrown out by --gc-sections in the linker.

If I remove -Wl,--gc-sections, the warning goes away.

@echristo
Copy link
Contributor

Well, sure, because you've turned off the thing that it depends on.

Do you have a better testcase here?

@llvmbot
Copy link
Collaborator Author

llvmbot commented Dec 17, 2015

I can reproduce this issue, but the description in the thread is not accurate.

Short Answer

This section flag for .text.__cxa_end_cleanup() is missing.

PATCH: http://reviews.llvm.org/D15613

Long Answer

This problem will only occur when:

  1. Assemble cxa_exception.cpp with integrated assembler (-fintegrated-as)

  2. Use ld.gold as the linker (-fuse-ld=gold)

  3. Enable GC section (-Wl,--gc-sections)

  4. Treat warning as fatal (-Wl,--fatal-warning)

  5. Generate one section per function or data (-ffunction-sections -fdata-sections)

When I was writing cxa_exception.cpp several months ago, I expected that .text.__cxa_end_cleanup_impl section won't be discarded because there is a relocation in .text.__cxa_end_cleanup section which refers to .text.__cxa_end_cleanup_impl section. However, this doesn't work due to two reasons:

  1. In contrast to GNU assembler, the integrated assembler will not set default section flags to .text sections. As a result, SHF_ALLOC section flag is missing from the output of the integrated assembler.

  2. ld.gold will ignore the sections without SHF_ALLOC. Consequently, .text.__cxa_end_cleanup section is not considered as a root set. ld.gold will discard .text.__cxa_end_cleanup_impl section and emit a warning for the relocations referring to the discarded section (which will be treated as an error.)

This is the reason why it only happens when we are compiling with integrated assembler.

A temporary workaround is to specify the section flags explicitly:
http://reviews.llvm.org/D15613

I am working on a long-term solution for lib/MC/MCParser so that integrated assembler can be more compatible with GNU assembler.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Dec 22, 2015

This commit should fix the problem:
http://reviews.llvm.org/rL256241

Change status to RESOLVED.

Feel free to re-open and let me know if the commit didn't fix the problem.

@stephenhines
Copy link
Collaborator

mentioned in issue #21794

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 9, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang:codegen
Projects
None yet
Development

No branches or pull requests

3 participants