RE: [RFC-v5] Fix .text section offset for windows DLL (was Calling __stdcall functions in the inferior)

Pierre Muller
2012-12-13T16:54:58Z

> -----Message d'origine-----
> De : gdb-patches-owner@sourceware.org [mailto:gdb-patches-
> owner@sourceware.org] De la part de Pedro Alves
> Envoyé : jeudi 13 décembre 2012 17:43
> À : Pedro Alves
> Cc : Pierre Muller; 'GDB Patches'
> Objet : Re: [RFC-v5] Fix .text section offset for windows DLL (was Calling
> __stdcall functions in the inferior)
>
> Oh, the problem was really there, but I got confused pointing
> at where it is :-). It's in pe_text_section_offset itself:
>
> /* Get the rva and size of the export section. */
> for (i = 0; i < nsections; i++)
> {
> char sname[8];
> unsigned long secptr1 = secptr + 40 * i;
> unsigned long vaddr = pe_get32 (abfd, secptr1 + 12);
>
> bfd_seek (abfd, (file_ptr) secptr1, SEEK_SET);
> bfd_bread (sname, (bfd_size_type) 8, abfd);
> if (strcmp (sname, ".text") == 0)
> return vaddr;
> }
>
> So sname should be 9 bytes, and be null terminated after the bfd_bread here
> too.
You are right once more...
Strange, because I really thought I copied it from the other
function...

> Want to give it a go?

OK, we were probably lucky because
most DLL's have their .text section as first section, so that we never get to
8 char long names...

ChangeLog entry:
2012-12-13 Pierre Muller <muller@sourceware.org>

* coff-pe-read.c (pe_text_section_offset): Increase size of sname
local variable and zero terminate it to avoid possible problems
in strcmp.

Index: coff-pe-read.c
===================================================================
RCS file: /cvs/src/src/gdb/coff-pe-read.c,v
retrieving revision 1.21
diff -u -r1.21 coff-pe-read.c
--- coff-pe-read.c 13 Dec 2012 15:11:36 -0000 1.21
+++ coff-pe-read.c 13 Dec 2012 16:49:13 -0000
@@ -672,12 +672,13 @@ pe_text_section_offset (struct bfd *abfd
/* Get the rva and size of the export section. */
for (i = 0; i < nsections; i++)
{
- char sname[8];
+ char sname[SCNNMLEN + 1];
unsigned long secptr1 = secptr + 40 * i;
unsigned long vaddr = pe_get32 (abfd, secptr1 + 12);

bfd_seek (abfd, (file_ptr) secptr1, SEEK_SET);
- bfd_bread (sname, (bfd_size_type) 8, abfd);
+ bfd_bread (sname, (bfd_size_type) SCNNMLEN, abfd);
+ sname[SCNNMLEN] = '\0';
if (strcmp (sname, ".text") == 0)
return vaddr;
}