From: "Mathieu Lacage" <mathieu.lacage@gmail.com>
To: gdb@sourceware.org
Subject: Re: how to make gdb happy with my linkmap
Date: Mon, 12 Jan 2009 15:08:00 -0000 [thread overview]
Message-ID: <74fef6df0901120708u3dab58eo462d8dae3e9d9ccb@mail.gmail.com> (raw)
In-Reply-To: <74fef6df0901050658g6c279a3ah321c7c6e30630475@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2219 bytes --]
Just in case it helps anyone, since I did not really have time to
investigate the gdb issues, I came up with the attached elf editor
which replaces the content of PT_INTERP. Eventually, I will have to
deal with gdb but, not now :)
Mathieu
On Mon, Jan 5, 2009 at 3:58 PM, Mathieu Lacage <mathieu.lacage@gmail.com> wrote:
>> Does it work any better with ld.so? I doubt it - GDB doesn't do
>
> It does not seem to work any better with ld-linux.so.2. I tried to
> look into this a bit more but I did not make much progress. Here is
> what I get:
>
> mathieu@mathieu-boulot:~/code/elf-loader$ gdb ./ldso
> GNU gdb 6.8-debian
> Copyright (C) 2008 Free Software Foundation, Inc.
> License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
> This is free software: you are free to change and redistribute it.
> There is NO WARRANTY, to the extent permitted by law. Type "show copying"
> and "show warranty" for details.
> This GDB was configured as "i486-linux-gnu"...
> (gdb) b stage1
> Breakpoint 1 at 0x944: file ldso.c, line 304.
> (gdb) r ./hello
> Starting program: /home/mathieu/code/elf-loader/ldso ./hello
> hello
>
> Program exited normally.
> (gdb)
>
> What I find weird is:
> 1) mathieu@mathieu-boulot:~/code/elf-loader$ readelf -s ./ldso |grep stage1
> 225: 00000932 135 FUNC GLOBAL HIDDEN 6 stage1
> mathieu@mathieu-boulot:~/code/elf-loader$ readelf -l ./ldso
>
> Elf file type is DYN (Shared object file)
> Entry point 0x932
> There are 6 program headers, starting at offset 52
> [...]
>
> i.e., stage1 is located at offset 0x932, and not 0x944 so, I can't
> figure out where the 0x944 displayed by gdb is coming from. I also
> find it surprising that gdb is actually trying to set a breakpoint at
> address 0x944: this is a pie binary so, gdb should know that the
> address will be known only once the program is run....
>
> To summarize, 2 questions:
> 1) what do I need to do to make gdb _not_ attempt to really set the
> breakpoint before the program is run ?
>
> 2) why is gdb using 0x944 and not 0x932 ?
>
> I am shooting a bit in the dark here, obviously.
>
> Mathieu
> --
> Mathieu Lacage <mathieu.lacage@gmail.com>
>
--
Mathieu Lacage <mathieu.lacage@gmail.com>
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: elfedit.c --]
[-- Type: text/x-csrc; name=elfedit.c, Size: 1758 bytes --]
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <elf.h>
#include <link.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
const char *filename = argv[1];
ElfW(Ehdr) header;
int fd = open (filename, O_RDWR);
ssize_t bytes_read = read (fd, &header, sizeof (header));
if (bytes_read != sizeof (header))
{
return -1;
}
ElfW(Phdr) *ph = malloc (header.e_phnum * header.e_phentsize);
if (ph == 0)
{
return -2;
}
if (lseek (fd, header.e_phoff, SEEK_SET) == -1)
{
return -3;
}
if (read (fd, ph, header.e_phnum * header.e_phentsize) != header.e_phnum * header.e_phentsize)
{
return -4;
}
int i;
for (i = 0; i < header.e_phnum; i++)
{
if (ph[i].p_type == PT_INTERP)
{
if (strlen (argv[2]) + 1> ph[i].p_filesz)
{
return -5;
}
if (lseek (fd, ph[i].p_offset, SEEK_SET) == -1)
{
return -6;
}
char *interp = malloc (ph[i].p_filesz);
memset (interp, 0, ph[i].p_filesz);
memcpy (interp, argv[2], strlen (argv[2]));
if (write (fd, argv[2], ph[i].p_filesz) != ph[i].p_filesz)
{
return -7;
}
if (lseek (fd, header.e_phoff + ((long)&ph[i].p_filesz - (long)ph), SEEK_SET) == -1)
{
return -8;
}
ElfW(Xword) filesz = strlen (argv[2])+1;
if (write (fd, &filesz, sizeof(filesz)) != sizeof(filesz))
{
return -9;
}
if (lseek (fd, header.e_phoff + ((long)&ph[i].p_memsz-(long)ph), SEEK_SET) == -1)
{
return -10;
}
ElfW(Xword) memsz = strlen (argv[2])+1;
if (write (fd, &memsz, sizeof(memsz)) != sizeof(memsz))
{
return -11;
}
return 0;
}
}
return 0;
}
prev parent reply other threads:[~2009-01-12 15:08 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-12-18 21:42 Mathieu Lacage
2008-12-18 21:49 ` Daniel Jacobowitz
2008-12-18 22:03 ` Mathieu Lacage
2008-12-18 22:10 ` Daniel Jacobowitz
2008-12-26 10:40 ` Mathieu Lacage
2008-12-26 12:54 ` Daniel Jacobowitz
2009-01-05 14:58 ` Mathieu Lacage
2009-01-05 17:13 ` Daniel Jacobowitz
2009-01-07 16:46 ` Doug Evans
2009-01-12 15:08 ` Mathieu Lacage [this message]
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=74fef6df0901120708u3dab58eo462d8dae3e9d9ccb@mail.gmail.com \
--to=mathieu.lacage@gmail.com \
--cc=gdb@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