public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Fangrui Song <maskray@google.com>
To: Florian Weimer <fweimer@redhat.com>
Cc: Fangrui Song via Libc-alpha <libc-alpha@sourceware.org>
Subject: Re: [PATCH] elf: Remove fallback to the start of DT_STRTAB for dladdr
Date: Mon, 2 May 2022 00:04:15 -0700	[thread overview]
Message-ID: <20220502070415.kv62bjtdhhcyefjn@google.com> (raw)
In-Reply-To: <87sfps8lt6.fsf@oldenburg.str.redhat.com>

On 2022-05-02, Florian Weimer wrote:
>* Fangrui Song via Libc-alpha:
>
>> When neither DT_HASH nor DT_GNU_HASH is present, the code scans
>> [DT_SYMTAB, DT_STRTAB). However, there is no guarantee that .dynstr
>> immediately follows .dynsym (e.g. lld typically places .gnu.version
>> after .dynsym).
>
>The code is compatible with lld because it always generates a hash
>table.  Maybe it was added to support old binaries without a hash table.
>So we would have to check if such binaries exist from the early
>libc.so.6 days (or check if binutils every generated ELF binaries
>without a hash table).  The glibc comment isn't clear if this was added
>because it was required at the time, or just because it seemed a good
>idea.
>
>I couldn't find any binaries with DT_SYMTAB, but without DT_HASH or
>DT_GNU_HASH in my collection, but doesn't mean that they don't exist.
>
>Thanks,
>Florian

With a linker script .hash and .gnu.hash can be removed.
But such an object behaves as if it has no symbol: symbol search will fail.
It makes sense for dladdr to not return a symbol for it.

% bmake
cc -pipe -g   -fuse-ld=bfd -g -fpic -shared -Wl,--version-script=b.ver b.c -o b.so
cc -pipe -g   -fuse-ld=bfd -g a.c -Wl,--no-as-needed -fno-pie -no-pie -Wl,-rpath=/tmp/d b.so -ldl -o a
% ./a
42

% cat b.lds   # GNU ld doesn't have this yet: https://sourceware.org/bugzilla/show_bug.cgi?id=26404
OVERWRITE_SECTIONS {
   /DISCARD/ : { *(.hash) *(.gnu.hash) }
}
% clang -fpic -fuse-ld=lld -shared b.c -Wl,b.lds -o b.so
% ./a
./a: symbol lookup error: ./a: undefined symbol: var

---

GNU ld doesn't seem to allow discarding both .gnu.hash and .hash:
  /DISCARD/ : { *(.hash) *(.gnu.hash) *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }

/usr/bin/ld.bfd: could not find section .hash

  reply	other threads:[~2022-05-02  7:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-01 21:50 Fangrui Song
2022-05-02  6:56 ` Florian Weimer
2022-05-02  7:04   ` Fangrui Song [this message]
2022-05-02  7:21     ` Florian Weimer
2022-05-02  7:30       ` Fangrui Song
2022-05-02  8:26         ` Florian Weimer
2022-05-02  8:34 ` Florian Weimer
2022-05-02 16:06   ` Fangrui Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220502070415.kv62bjtdhhcyefjn@google.com \
    --to=maskray@google.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).