From owner-freebsd-amd64@FreeBSD.ORG Tue Jun 22 09:21:32 2010 Return-Path: Delivered-To: freebsd-amd64@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 4AE0E106566B; Tue, 22 Jun 2010 09:21:32 +0000 (UTC) (envelope-from avg@freebsd.org) Received: from citadel.icyb.net.ua (citadel.icyb.net.ua [212.40.38.140]) by mx1.freebsd.org (Postfix) with ESMTP id C28E88FC16; Tue, 22 Jun 2010 09:21:29 +0000 (UTC) Received: from porto.topspin.kiev.ua (porto-e.starpoint.kiev.ua [212.40.38.100]) by citadel.icyb.net.ua (8.8.8p3/ICyb-2.3exp) with ESMTP id MAA17232; Tue, 22 Jun 2010 12:21:28 +0300 (EEST) (envelope-from avg@freebsd.org) Received: from localhost.topspin.kiev.ua ([127.0.0.1]) by porto.topspin.kiev.ua with esmtp (Exim 4.34 (FreeBSD)) id 1OQzfc-000PcO-8F; Tue, 22 Jun 2010 12:21:28 +0300 Message-ID: <4C208096.3030602@freebsd.org> Date: Tue, 22 Jun 2010 12:21:26 +0300 From: Andriy Gapon User-Agent: Thunderbird 2.0.0.24 (X11/20100603) MIME-Version: 1.0 To: Navdeep Parhar , John Baldwin References: <4C1F798C.7010204@freebsd.org> <201006211143.26459.jhb@freebsd.org> <4C1F8BDD.9010408@freebsd.org> <201006211610.45811.jhb@freebsd.org> <20100621204435.GA98177@hub.freebsd.org> <4C1FDAF9.3080808@freebsd.org> In-Reply-To: <4C1FDAF9.3080808@freebsd.org> X-Enigmail-Version: 0.96.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Mailman-Approved-At: Tue, 22 Jun 2010 11:08:30 +0000 Cc: freebsd-hackers@freebsd.org, freebsd-amd64@freebsd.org Subject: Re: amd64 kernel modules: mapping sections to addresses X-BeenThere: freebsd-amd64@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Porting FreeBSD to the AMD64 platform List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Jun 2010 09:21:32 -0000 on 22/06/2010 00:34 Andriy Gapon said the following: > gdb change - I'd rather do it via kld_current_sos, > kld_relocate_section_addresses. I'd like to avoid changing common gdb code for > a variety of reasons. I came up with the following patch. EXEC_P and DYNAMIC flags are bfd library equivalents of ET_EXEC and ET_DYN elf flags. Absence of both of these flags means ET_REL, which is a type of our amd64 kernel modules. The code all resides in kld.c and acts only kernel modules that are either auto-loaded via kld_current_sos or explicitly added with add-kld. I used a static variable in kld_relocate_section_addresses because that function is called on each section sequentially, so current offset can not be stored on stack. The offset is reset to module's load address when see that we are called with first section. Alternative is to glimpse at previous section's end address as you did. So the code depends on sections being passed in forward sequential order. How does this look? Could you please test it? diff --git a/gnu/usr.bin/gdb/kgdb/kld.c b/gnu/usr.bin/gdb/kgdb/kld.c index 716a67c..d5ba20a 100644 --- a/gnu/usr.bin/gdb/kgdb/kld.c +++ b/gnu/usr.bin/gdb/kgdb/kld.c @@ -198,12 +198,32 @@ find_kld_address (char *arg, CORE_ADDR *address) } static void +adjust_section_address (struct section_table *sec, CORE_ADDR *curr_base) +{ + struct bfd_section *asect = sec->the_bfd_section; + bfd *abfd = sec->bfd; + + if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) { + sec->addr += *curr_base; + sec->endaddr += *curr_base; + return; + } + + *curr_base = align_power(*curr_base, + bfd_get_section_alignment(abfd, asect)); + sec->addr = *curr_base; + sec->endaddr = sec->addr + bfd_section_size(abfd, asect); + *curr_base = sec->endaddr; +} + +static void load_kld (char *path, CORE_ADDR base_addr, int from_tty) { struct section_addr_info *sap; struct section_table *sections = NULL, *sections_end = NULL, *s; struct cleanup *cleanup; bfd *bfd; + CORE_ADDR curr_addr; int i; /* Open the kld. */ @@ -224,10 +244,9 @@ load_kld (char *path, CORE_ADDR base_addr, int from_tty) 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; - } + curr_addr = base_addr; + for (s = sections; s < sections_end; s++) + adjust_section_address(s, &curr_addr); /* Build a section addr info to pass to symbol_file_add(). */ sap = build_section_addr_info_from_section_table (sections, @@ -284,9 +303,12 @@ kgdb_add_kld_cmd (char *arg, int from_tty) static void kld_relocate_section_addresses (struct so_list *so, struct section_table *sec) { + static CORE_ADDR curr_addr; + + if (sec == so->sections) + curr_addr = so->lm_info->base_address; - sec->addr += so->lm_info->base_address; - sec->endaddr += so->lm_info->base_address; + adjust_section_address(sec, &curr_addr); } static void -- Andriy Gapon