From patchwork Fri Mar 28 13:37:52 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 333 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx22.g.dreamhost.com (caibbdcaabij.dreamhost.com [208.113.200.189]) by wilcox.dreamhost.com (Postfix) with ESMTP id D45C636041E for ; Fri, 28 Mar 2014 06:38:02 -0700 (PDT) Received: by homiemail-mx22.g.dreamhost.com (Postfix, from userid 14314964) id 812114475CA7; Fri, 28 Mar 2014 06:38:02 -0700 (PDT) X-Original-To: gdb@patchwork.siddhesh.in Delivered-To: x14314964@homiemail-mx22.g.dreamhost.com Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by homiemail-mx22.g.dreamhost.com (Postfix) with ESMTPS id 5DB904475C06 for ; Fri, 28 Mar 2014 06:38:02 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:date:from:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; q=dns; s=default; b=kT1VFyW8u+UwYatdYvPWY174c0Z1Yk3fcrFAytNp6A+ Gg+Vd6icITJWuFKHiUKlEk89tN/ICO277cZM3Vjr1DohRy4SzeLhLLAzPYRNmQNe oPJ3Svj9gLRTF+r3A8BtytoUWH10BkT0lskaDEdV3SGka4mRoBCHaVI5jNVEMtTc = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:date:from:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; s=default; bh=tahM0xPmXyCgsRt5BDZkFRTbtqs=; b=PSnExk0hBG7YQ46GI P4XONg2GwvVvIlLOTu8LQFbTec56UymN7qukqMPM4wUzdC+qvGaKpBcKFOgY2JUr dA1dycz7Fp5hM8tU/pEBcp/4dn2btmrPni8CszcyGwETGsPk/Kv2PJsRAZMIlJ61 OBw6akvksUlKxWA/gxC8BI7cMI= Received: (qmail 10418 invoked by alias); 28 Mar 2014 13:38:00 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 10395 invoked by uid 89); 28 Mar 2014 13:37:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 28 Mar 2014 13:37:59 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s2SDbts9011900 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 28 Mar 2014 09:37:55 -0400 Received: from [127.0.0.1] (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s2SDbqFW010203; Fri, 28 Mar 2014 09:37:53 -0400 Message-ID: <53357B30.6040006@redhat.com> Date: Fri, 28 Mar 2014 13:37:52 +0000 From: Pedro Alves User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130625 Thunderbird/17.0.7 MIME-Version: 1.0 To: "Metzger, Markus T" , Mark Wielaard , Cary Coutant , Doug Evans , gdb-patches@sourceware.org, binutils@sourceware.org Subject: Re: vdso handling References: <20140313130322.GA3384@bubble.grove.modra.org> <5321C7C8.6000707@redhat.com> <5321C8FA.40708@gmail.com> <5321CE1A.20509@redhat.com> <20140313235347.GD3384@bubble.grove.modra.org> <20140318230939.GA9145@bubble.grove.modra.org> <5329879C.6070805@redhat.com> <20140320013305.GA13347@bubble.grove.modra.org> <532C5F60.80700@redhat.com> <20140328061321.GU18201@bubble.grove.modra.org> In-Reply-To: <20140328061321.GU18201@bubble.grove.modra.org> X-DH-Original-To: gdb@patchwork.siddhesh.in On 03/28/2014 06:13 AM, Alan Modra wrote: > On Fri, Mar 21, 2014 at 03:48:48PM +0000, Pedro Alves wrote: >> I just tried pointing add-symbol-file-from-memory at an already >> mapped DSO's elf header, but it doesn't work as is unfortunately: >> >> (gdb) info shared curses >> 0x000000324d006d20 0x000000324d01df58 Yes /lib64/libncurses.so.5 >> (gdb) x /4b 0x000000324d000000 >> 0x324d000000: 127 69 76 70 >> (gdb) add-symbol-file-from-memory 0x000000324d000000 >> Failed to read a valid object file image from memory. >> >> I single stepped a little through >> bfd_elf_bfd_from_remote_memory - something goes wrong with the >> reading of the load segment contents, probably something wrong >> with the address computations. > > readelf -a --wide on my x86_64 libncurses.so.5 shows > > [snip] > Start of section headers: 132144 (bytes into file) > [snip] > [25] .shstrtab STRTAB 0000000000000000 02034c 0000de 00 0 0 1 > [snip] > LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x01efe4 0x01efe4 R E 0x200000 > LOAD 0x01fd50 0x000000000021fd50 0x000000000021fd50 0x0005e4 0x000770 RW 0x200000 > > So .shstrtab and the section headers might have been loaded by the > second PT_LOAD header, *but* the second PT_LOAD has a bss area. > Anything past 0x220334 will be cleared out by ld.so. No chance of > getting at section headers then, and this will be true for most > in-memory images. Indeed. > bfd_from_remote_memory should take note of p_memsz.. Hmm, and there > are quite a few other issues there too, most notably that p_align > on x86_64 these days tends to be *much* larger than the page size used > by ld.so. Hmm. Indeed. With current mainline, and with your patch as is, the command still fails for me. In fact, it turns out exactly related to p_align vs page size. $ cat /proc/30669/maps | grep ncurses 324d000000-324d023000 r-xp 00000000 fd:01 315662 /usr/lib64/libncurses.so.5.9 324d023000-324d222000 ---p 00023000 fd:01 315662 /usr/lib64/libncurses.so.5.9 324d222000-324d223000 r--p 00022000 fd:01 315662 /usr/lib64/libncurses.so.5.9 324d223000-324d224000 rw-p 00023000 fd:01 315662 /usr/lib64/libncurses.so.5.9 So when trying to read the second PT_LOAD with p_vmaddr 324d222cf8 and p_vmaddr+p_filesz 324d2236b4, (the 3rd and 4th region above), we'd end up reading from 324d200000 to 324d2236b4: (top-gdb) p /x loadbase + vaddr $5 = 0x324d200000 (top-gdb) p /x end $6 = 0x236b4 (top-gdb) p /x loadbase + vaddr + end $8 = 0x324d2236b4 which fails as it hits the (324d023000-324d222000) region, which has no permissions. This patch on top of yours makes things work for me: --- bfd/elfcode.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bfd/elfcode.h b/bfd/elfcode.h index 31f67a8..974c8b4 100644 --- a/bfd/elfcode.h +++ b/bfd/elfcode.h @@ -1622,6 +1622,7 @@ NAME(_bfd_elf,bfd_from_remote_memory) bfd_vma shdr_end; bfd_vma loadbase; bfd_boolean loadbase_set; + bfd_vma page_size; /* Read in the ELF header in external format. */ err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr, sizeof x_ehdr); @@ -1693,6 +1694,7 @@ NAME(_bfd_elf,bfd_from_remote_memory) } i_phdrs = (Elf_Internal_Phdr *) &x_phdrs[i_ehdr.e_phnum]; + page_size = get_elf_backend_data (templ)->minpagesize; high_offset = 0; last_phdr = NULL; loadbase = 0; @@ -1753,7 +1755,6 @@ NAME(_bfd_elf,bfd_from_remote_memory) high_offset = shdr_end; else { - bfd_vma page_size = get_elf_backend_data (templ)->minpagesize; bfd_vma segment_end = last_phdr->p_offset + last_phdr->p_filesz; /* Assume we loaded full pages, allowing us to sometimes see @@ -1781,15 +1782,14 @@ NAME(_bfd_elf,bfd_from_remote_memory) if (i_phdrs[i].p_type == PT_LOAD) { bfd_vma start = i_phdrs[i].p_offset; - bfd_vma end = start + i_phdrs[i].p_filesz; bfd_vma vaddr = i_phdrs[i].p_vaddr; + bfd_vma end = start + i_phdrs[i].p_filesz; - if (i_phdrs[i].p_align > 1) - { - start &= -i_phdrs[i].p_align; - end = (end + i_phdrs[i].p_align - 1) & -i_phdrs[i].p_align; - vaddr &= -i_phdrs[i].p_align; - } + /* Assume we loaded full pages, allowing us to sometimes see + section headers. */ + start &= -page_size; + vaddr &= -page_size; + end = (end + page_size - 1) & -page_size; if (end > high_offset) end = high_offset; err = target_read_memory (loadbase + vaddr,