From owner-freebsd-hackers@FreeBSD.ORG Thu Sep 25 18:02:10 2008 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 00A54106568E for ; Thu, 25 Sep 2008 18:02:10 +0000 (UTC) (envelope-from nparhar@gmail.com) Received: from ey-out-2122.google.com (ey-out-2122.google.com [74.125.78.27]) by mx1.freebsd.org (Postfix) with ESMTP id 7B0F08FC0A for ; Thu, 25 Sep 2008 18:02:09 +0000 (UTC) (envelope-from nparhar@gmail.com) Received: by ey-out-2122.google.com with SMTP id 6so162195eyi.7 for ; Thu, 25 Sep 2008 11:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=t9JJs1i2x1BAx4ZZMvOB/iQCdykF2GBrmRqCqkYl3KA=; b=E4dHfYxiEwqK9fr9CyfcDSm0teL0R1Vr8e3/ulW6/Y4YyhwrZZBy203Jdn07bN+C77 dQ9beN83zWmbN5V41dcG6tHXi80G3abPFoZMApA/mD54OmV/ixqoLOGkzplfpvUXpHIO 4cXexZ57aXiQNU5trP3/8cX993j/369ZaHh8o= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=sDOOZrn0DJKlFcsW03WawZggMcyiu3sV3NiFXXeiWJSF8UlLHBm7lwI8vYY89UQuoJ K+fF1mX4XTug+aqEXMfs8zPmtTkgLsmZEpXlPytx2wos2ts4sXu4hFim2swW1slwvmM2 eb8hJ7YA0g8goUYeOzGaADtQSizfKGfl2vIM4= Received: by 10.187.251.11 with SMTP id d11mr38901fas.104.1222365727903; Thu, 25 Sep 2008 11:02:07 -0700 (PDT) Received: by 10.187.182.13 with HTTP; Thu, 25 Sep 2008 11:02:07 -0700 (PDT) Message-ID: Date: Thu, 25 Sep 2008 11:02:07 -0700 From: "Navdeep Parhar" To: freebsd-hackers@freebsd.org In-Reply-To: <200809171628.52406.jhb@freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <200809171131.30819.jhb@freebsd.org> <200809171628.52406.jhb@freebsd.org> Subject: Re: kgdb's add-kld broken on amd64 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 25 Sep 2008 18:02:10 -0000 > Well, this means gdb can't handle loading .o's, though I guess that is to be > expected. :( Even if I fix add-kld there's probably no way I can easily fix > the sharedlibrary stuff w/o ripping gdb itself up a bunch. > > -- > John Baldwin > I thought I'd leave this patch here on the list in case anyone finds it helpful. This patch works both when "add-kld ..." is explicitly called, and when kgdb autodetects KLDs and reads in their symbols automatically. The changes to kld.c are from what John B. posted in this thread earlier. The additional changes to exec.c mimic what the kernel does in link_elf_obj.c for amd64 modules. I used "info files" to verify that the segment reloc info was correct. Regards, Navdeep diff -r 3292f0cda869 contrib/gdb/gdb/exec.c --- a/contrib/gdb/gdb/exec.c Wed Sep 24 17:32:58 2008 -0700 +++ b/contrib/gdb/gdb/exec.c Wed Sep 24 19:54:31 2008 -0700 @@ -348,6 +348,18 @@ (*table_pp)->bfd = abfd; (*table_pp)->the_bfd_section = asect; (*table_pp)->addr = bfd_section_vma (abfd, asect); + if ((*table_pp)->addr == 0 && asect->index > 0) { + /* + * KLDs on amd64 go down this code path. Adjust the addr as is done in + * kern/link_elf_obj.c. We need the previous section's endaddr in order + * to do that. + */ + struct section_table *p = (*table_pp) - 1; + + (*table_pp)->addr = align_power(p->endaddr, + bfd_section_alignment(abfd, asect)); + } + (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); (*table_pp)++; } diff -r 3292f0cda869 gnu/usr.bin/gdb/kgdb/kld.c --- a/gnu/usr.bin/gdb/kgdb/kld.c Wed Sep 24 17:32:58 2008 -0700 +++ b/gnu/usr.bin/gdb/kgdb/kld.c Wed Sep 24 19:54:31 2008 -0700 @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -196,39 +197,14 @@ return (0); } -struct add_section_info { - struct section_addr_info *section_addrs; - int sect_index; - CORE_ADDR base_addr; -}; - -static void -add_section (bfd *bfd, asection *sect, void *arg) -{ - struct add_section_info *asi = arg; - CORE_ADDR address; - char *name; - - /* Ignore non-resident sections. */ - if ((bfd_get_section_flags(bfd, sect) & (SEC_ALLOC | SEC_LOAD)) == 0) - return; - - name = xstrdup(bfd_get_section_name(bfd, sect)); - make_cleanup(xfree, name); - address = asi->base_addr + bfd_get_section_vma(bfd, sect); - asi->section_addrs->other[asi->sect_index].name = name; - asi->section_addrs->other[asi->sect_index].addr = address; - asi->section_addrs->other[asi->sect_index].sectindex = sect->index; - printf_unfiltered("\t%s_addr = %s\n", name, local_hex_string(address)); - asi->sect_index++; -} - static void load_kld (char *path, CORE_ADDR base_addr, int from_tty) { - struct add_section_info asi; struct cleanup *cleanup; + struct section_addr_info *sap; + struct section_table *sections = NULL, *sections_end = NULL, *s; bfd *bfd; + int i; /* Open the kld. */ bfd = bfd_openr(path, gnutarget); @@ -244,19 +220,30 @@ if (bfd_get_section_by_name (bfd, ".text") == NULL) error("\"%s\": can't find text section", path); + /* Build a section table from the bfd and relocate the sections. */ + if (build_section_table(bfd, §ions, §ions_end)) + error("\"%s\": can't find file sections", path); + cleanup = make_cleanup(xfree, sections); + for (s = sections; s < sections_end; s++) { + s->addr += base_addr; + s->endaddr += base_addr; + } + + /* Build a section addr info to pass to symbol_file_add(). */ + sap = build_section_addr_info_from_section_table (sections, + sections_end); + cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info, + sap); + printf_unfiltered("add symbol table from file \"%s\" at\n", path); - - /* Build a section table for symbol_file_add() from the bfd sections. */ - asi.section_addrs = alloc_section_addr_info(bfd_count_sections(bfd)); - cleanup = make_cleanup(xfree, asi.section_addrs); - asi.sect_index = 0; - asi.base_addr = base_addr; - bfd_map_over_sections(bfd, add_section, &asi); + for (i = 0; i < sap->num_sections; i++) + printf_unfiltered("\t%s_addr = %s\n", sap->other[i].name, + local_hex_string(sap->other[i].addr)); if (from_tty && (!query("%s", ""))) error("Not confirmed."); - symbol_file_add(path, from_tty, asi.section_addrs, 0, OBJF_USERLOADED); + symbol_file_add(path, from_tty, sap, 0, OBJF_USERLOADED); do_cleanups(cleanup); }