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

lld: pointer remains blank in custom section #62209

Closed
erenon opened this issue Apr 18, 2023 · 2 comments
Closed

lld: pointer remains blank in custom section #62209

erenon opened this issue Apr 18, 2023 · 2 comments
Labels
lld:ELF question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@erenon
Copy link

erenon commented Apr 18, 2023

My application stores a pointer to a string in a custom ELF section. LLD leaves the pointer 0, while other linkers set it correctly.
Minimal repro:

#include <cstdint>
#include <cstdio>

int main() {

  __attribute__((section(".custom"), used, retain))
  static constexpr const char* x[] = {
    __FILE__
  };
  const std::uint64_t id = std::uint64_t(&x);
  printf("%lu\n", id);
}

combined with:

app: app.cpp
	$(CXX) $< -o $@ -fuse-ld=$(LD)

test: app
	readelf -x .custom $<

Test cases:

$ CXX=$(which clang++) LD=bfd make test -B    # ok
$ CXX=$(which clang++) LD=gold make test -B  # ok
$ CXX=$(which clang++) LD=lld make test -B     # BROKEN
$ CXX=$(which g++) LD=bfd make test -B    # ok
$ CXX=$(which g++) LD=gold make test -B  # ok
$ CXX=$(which g++) LD=lld make test -B     # BROKEN

Example good output:

$ readelf -x .custom app

Hex dump of section '.custom':
  0x00002020 44070000 00000000                   D.......

Example bad output:

$ readelf -x .custom app

Hex dump of section '.custom':
  0x000039e8 00000000 00000000                   ........

Versions:

$ clang++ -v
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Selected multilib: .;@m64

$ ld.lld -v
Ubuntu LLD 14.0.0 (compatible with GNU linkers)
@llvmbot
Copy link
Member

llvmbot commented Apr 18, 2023

@llvm/issue-subscribers-lld-elf

@MaskRay
Copy link
Member

MaskRay commented Apr 18, 2023

ld.lld's behavior is different from GNU ld and gold, but the behavior is still correct (it improves text section compressing).
.custom has a dynamic relocation. After rtld applies the relocation, the location in .custom will point to the string literal. See https://maskray.me/blog/2020-12-19-lld-and-gnu-linker-incompatibilities --apply-dynamic-relocs

@MaskRay MaskRay closed this as not planned Won't fix, can't repro, duplicate, stale Apr 18, 2023
@EugeneZelenko EugeneZelenko added the question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! label Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lld:ELF question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

4 participants