diff --git a/contrib/gdb/gdb/exec.c b/contrib/gdb/gdb/exec.c --- a/contrib/gdb/gdb/exec.c +++ b/contrib/gdb/gdb/exec.c @@ -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 --git a/sys/kern/link_elf_obj.c b/sys/kern/link_elf_obj.c --- a/sys/kern/link_elf_obj.c +++ b/sys/kern/link_elf_obj.c @@ -680,10 +680,12 @@ switch (shdr[i].sh_type) { case SHT_PROGBITS: case SHT_NOBITS: - alignmask = shdr[i].sh_addralign - 1; - mapsize += alignmask; - mapsize &= ~alignmask; - mapsize += shdr[i].sh_size; + if (shdr[i].sh_size) { + alignmask = shdr[i].sh_addralign - 1; + mapsize += alignmask; + mapsize &= ~alignmask; + mapsize += shdr[i].sh_size; + } break; } } @@ -740,9 +742,11 @@ switch (shdr[i].sh_type) { case SHT_PROGBITS: case SHT_NOBITS: - alignmask = shdr[i].sh_addralign - 1; - mapbase += alignmask; - mapbase &= ~alignmask; + if (shdr[i].sh_size) { + alignmask = shdr[i].sh_addralign - 1; + mapbase += alignmask; + mapbase &= ~alignmask; + } if (ef->shstrtab && shdr[i].sh_name != 0) ef->progtab[pb].name = ef->shstrtab + shdr[i].sh_name;