From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26918 invoked by alias); 26 Feb 2003 18:40:11 -0000 Mailing-List: contact gdb-patches-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sources.redhat.com Received: (qmail 26910 invoked from network); 26 Feb 2003 18:40:10 -0000 Received: from unknown (HELO main.gmane.org) (80.91.224.249) by 172.16.49.205 with SMTP; 26 Feb 2003 18:40:10 -0000 Received: from list by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 18o6TB-0008My-00 for ; Wed, 26 Feb 2003 19:39:53 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: gdb-patches@sources.redhat.com Received: from news by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 18o6TA-0008Mo-00 for ; Wed, 26 Feb 2003 19:39:52 +0100 From: "Raoul Gough" Subject: RFC: coffread.c relocation fixes Date: Wed, 26 Feb 2003 18:40:00 -0000 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_01AE_01C2DDC6.A30370D0" X-Complaints-To: usenet@main.gmane.org X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-SW-Source: 2003-02/txt/msg00725.txt.bz2 This is a multi-part message in MIME format. ------=_NextPart_000_01AE_01C2DDC6.A30370D0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-length: 791 When a shared object can't be loaded at its preferred image base, it gets relocated. There's a convoluted switch statement in coff_symtab_read that handles relocation of the symbols, and I believe it handles a couple of cases incorrectly: o Static symbols (with c_sclass of C_STAT) never get relocated. I think this is wrong, probably an oversight where the C_STAT cases fall through to the C_EXT case and then don't get handled. o Absolute symbols (with c_secnum of N_ABS) *do* get relocated, so things like __minor_os_version__ get adjusted by the relocation offset. Curiously, there is a fixme comment in the code not to do this. The attached exmple shows the problem in action and the attached diffs fix both problems. Do these changes seem sensible to others? Regards, Raoul Gough. ------=_NextPart_000_01AE_01C2DDC6.A30370D0 Content-Type: text/plain; name="ChangeLog_entry.txt" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ChangeLog_entry.txt" Content-length: 182 2003-02-26 Raoul Gough * coffread.c(coff_symtab_read): DO relocate static symbols from PE files, DON'T relocate absolute symbols (and DO use mst_abs). ------=_NextPart_000_01AE_01C2DDC6.A30370D0 Content-Type: text/plain; name="coffread.c.diff.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="coffread.c.diff.txt" Content-length: 2051 Index: src/gdb/coffread.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvs/src/src/gdb/coffread.c,v retrieving revision 1.37 diff -c -p -r1.37 coffread.c *** src/gdb/coffread.c 25 Feb 2003 21:36:17 -0000 1.37 --- src/gdb/coffread.c 26 Feb 2003 18:37:26 -0000 *************** coff_symtab_read (long symtab_offset, un *** 869,875 **** print_address_symbolic work right without the (now gone) "set fast-symbolic-addr off" kludge. */ =20=20 - /* FIXME: should use mst_abs, and not relocate, if absolute. */ enum minimal_symbol_type ms_type; int sec; =20=20 --- 869,874 ---- *************** coff_symtab_read (long symtab_offset, un *** 891,902 **** || cs->c_sclass =3D=3D C_THUMBEXT ? mst_bss : mst_file_bss; } else { sec =3D cs_to_section (cs, objfile); tmpaddr =3D cs->c_value; ! if (cs->c_sclass =3D=3D C_EXT || cs->c_sclass =3D=3D C_THUMBEXTFUNC ! || cs->c_sclass =3D=3D C_THUMBEXT) tmpaddr +=3D ANOFFSET (objfile->section_offsets, sec); =20=20 if (sec =3D=3D SECT_OFF_TEXT (objfile)) --- 890,913 ---- || cs->c_sclass =3D=3D C_THUMBEXT ? mst_bss : mst_file_bss; } + else if (cs->c_secnum =3D=3D N_ABS) + { + /* Use the correct minimal symbol type (and don't + relocate) for absolute vaules. */ + ms_type =3D mst_abs; + sec =3D cs_to_section (cs, objfile); + tmpaddr =3D cs->c_value; + } else { sec =3D cs_to_section (cs, objfile); tmpaddr =3D cs->c_value; !=20 ! /* Statics in a PE file also get relocated */ ! if (cs->c_sclass =3D=3D C_EXT ! || cs->c_sclass =3D=3D C_THUMBEXTFUNC ! || cs->c_sclass =3D=3D C_THUMBEXT ! || (pe_file && (cs->c_sclass =3D=3D C_STAT))) tmpaddr +=3D ANOFFSET (objfile->section_offsets, sec); =20=20 if (sec =3D=3D SECT_OFF_TEXT (objfile)) ------=_NextPart_000_01AE_01C2DDC6.A30370D0 Content-Type: text/plain; name="example3.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="example3.txt" Content-length: 2713 $ cat dlleg.c __attribute((__dllexport__)) void fn () { } static int local; $ cat dlleg2.c __attribute((__dllexport__)) void fn2 () { } static int local; $ cat dllegmain.c __attribute((__dllimport__)) void fn (); __attribute((__dllimport__)) void fn2 (); int main () { fn(); fn2(); return 0; } $ gcc -g -c dlleg.c $ gcc -g -c dlleg2.c $ gcc -g -c dllegmain.c $ # $ # Force clash by omitting -Wl,--enable-auto-image-base $ # $ gcc -shared -o dlleg.dll dlleg.o $ gcc -shared -o dlleg2.dll dlleg2.o $ gcc -o dllegmain dllegmain.o dlleg.dll dlleg2.dll $ gdb dllegmain GNU gdb 2003-02-26-cvs Copyright 2003 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain condition= s. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i686-pc-cygwin"... (gdb) run Starting program: /cygdrive/f/Users/Raoul/gdb/dllegmain.exe=20 Program exited with code 01. (gdb) # (gdb) # DLL info shows that dlleg and dlleg2 were loaded at different (gdb) # addresses, so one got relocated: (gdb) # (gdb) info dll DLL Name Load Address /usr/bin/cygwin1.dll 61001000 /cygdrive/f/WINNT/system32/kernel32.dll 77e81000 /cygdrive/f/Users/Raoul/gdb/dlleg2.dll 10001000 /cygdrive/f/Users/Raoul/gdb/dlleg.dll 00331000 /cygdrive/f/WINNT/system32/advapi32.dll 77db1000 /cygdrive/f/WINNT/system32/rpcrt4.dll 77d41000 /cygdrive/f/WINNT/System32/psapi.dll 690a1000 (gdb) # (gdb) # Dump internal symbol tables (gdb) # (gdb) maint print msymbols reloc.dump (gdb) quit $ # $ # There should be two distinct static_int vars (one $ # relocated). Unpatches gdb doesn't get this right: $ # $ grep static_int reloc.dump [96] d 0x10005020 static_int [115] d 0x10005020 static_int $ # $ # ... and absolute values shouldn't be relocated but are: $ # $ grep os_version reloc.dump [ 3] T 0x0 _minor_os_version__ [ 7] T 0x4 _major_os_version__ [ 3] T 0x0 _minor_os_version__ [ 7] T 0x4 _major_os_version__ [127] T 0xf0330000 _minor_os_version__ [131] T 0xf0330004 _major_os_version__ $ # $ # with the patch, static vars are relocated correctly: $ # $ grep static_int reloc2.dump [96] d 0x10005020 static_int [95] d 0x335020 static_int $ # $ # ... and absolute variables are untouched (note also the "A" $ # type for the minimal symbols instead of "T") $ # $ grep os_version reloc2.dump [ 3] A 0x0 _minor_os_version__ [ 7] A 0x4 _major_os_version__ [ 3] A 0x0 _minor_os_version__ [ 7] A 0x4 _major_os_version__ [ 3] A 0x0 _minor_os_version__ [ 7] A 0x4 _major_os_version__ ------=_NextPart_000_01AE_01C2DDC6.A30370D0--