mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Pablo Galindo Salgado <pablogsal@gmail.com>
To: musl@lists.openwall.com
Subject: [musl] Why the entries in the dynamic section are not always relocated?
Date: Sun, 8 May 2022 08:48:29 +0100	[thread overview]
Message-ID: <CAFjbc8H+ZpF2cOs8cdpurTo=dm6W5dJsAYbVurtkn0ZRa-13Kw@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1906 bytes --]

Hi,

I have noticed that when using dl_iterate_phdr in muslc to inspect some
entries in the dynamic section
of different shared libraries loaded into a program, the pointers are not
always relocated (they
are offset instead of full addresses). For example, consider this program:

#include <elf.h>
#include <link.h>
#include <iostream>

static int
phdrs_callback(dl_phdr_info* info, size_t size, void* data) noexcept
{
    for (auto phdr = info->dlpi_phdr, end = phdr + info->dlpi_phnum; phdr
!= end; ++phdr) {
        if (phdr->p_type != PT_DYNAMIC) {
            continue;
        }
        const auto* dynamic_section = reinterpret_cast<const
ElfW(Dyn)*>(phdr->p_vaddr + info->dlpi_addr);

        for (; dynamic_section->d_tag != DT_NULL; ++dynamic_section) {
            if (dynamic_section->d_tag == DT_JMPREL) {
                std::cerr << "Address of JMPREL for lib " <<
info->dlpi_name <<" is: " << (void*)dynamic_section->d_un.d_ptr <<
std::endl;
            }
        }
    }
    return 0;
}

int main() {
    dl_iterate_phdr(&phdrs_callback, NULL);
}

In most glibc systems (for instance  ubuntu:20.04), this prints regular
addressed that have been relocated:

Address of JMPREL for lib  is: 0x557bed97a8e8
Address of JMPREL for lib /usr/lib/libc.so.6 is: 0x7f8e0f08cc18

but in muslc systems (like Alpine linux, for example using
the python:3.10-alpine container) this prints offsets
instead of full relocated addresses:

Address of JMPREL for lib ./a.out is: 0x7f8
Address of JMPREL for lib /usr/lib/libstdc++.so.6 is: 0xa94e0
Address of JMPREL for lib /lib/ld-musl-x86_64.so.1 is: 0x145c0
Address of JMPREL for lib /usr/lib/libgcc_s.so.1 is: 0x22e8

Why is this happening? How can one programmatically know when the linker is
going to place here offsets or full
relocated addresses? In which situation does this happen?

Thanks in advance for the help!

Kind regards,
Pablo Galindo Salgado

[-- Attachment #2: Type: text/html, Size: 4723 bytes --]

             reply	other threads:[~2022-05-08 10:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-08  7:48 Pablo Galindo Salgado [this message]
2022-05-08 11:39 ` Markus Wichmann
2022-05-08 13:54   ` Rich Felker
2022-05-08 14:23     ` Pablo Galindo Salgado
2022-05-08 19:38       ` Szabolcs Nagy

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='CAFjbc8H+ZpF2cOs8cdpurTo=dm6W5dJsAYbVurtkn0ZRa-13Kw@mail.gmail.com' \
    --to=pablogsal@gmail.com \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).