From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 129C03858D20; Fri, 4 Feb 2022 17:30:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 129C03858D20 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: dllfixdbg: improve readability X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/master X-Git-Oldrev: 4574c6037853789b69b7cce9a4264448eca24f08 X-Git-Newrev: 820c7367859b6b206f5ccefe1dfe967578696563 Message-Id: <20220204173009.129C03858D20@sourceware.org> Date: Fri, 4 Feb 2022 17:30:09 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Feb 2022 17:30:09 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D820c7367859= b6b206f5ccefe1dfe967578696563 commit 820c7367859b6b206f5ccefe1dfe967578696563 Author: Corinna Vinschen Date: Fri Feb 4 18:25:07 2022 +0100 Cygwin: dllfixdbg: improve readability =20 - formatting - use array pointer as argument rather than variable arguments - syntactical fixes - add comments - drop unnecessary recomputation of all section VMAs. =20 Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/dllfixdbg | 124 +++++++++++++++++++++++++-------------------= ---- 1 file changed, 64 insertions(+), 60 deletions(-) diff --git a/winsup/cygwin/dllfixdbg b/winsup/cygwin/dllfixdbg index 87adec954..669ea92dd 100755 --- a/winsup/cygwin/dllfixdbg +++ b/winsup/cygwin/dllfixdbg @@ -8,69 +8,73 @@ # use integer; use strict; -sub xit($@); -my $strip =3D $ARGV[0] eq '-s'; -shift if $strip; -my $objdump =3D shift; -my @objcopy =3D ((shift)); -my $pre_dll =3D shift; -my $dbg_dll =3D shift; -my $new_dll =3D shift; -my $verbose =3D shift; -xit 0, @objcopy, '-R', '.gnu_debuglink_overlay', '--add-gnu-debuglink=3D/d= ev/null', '--only-keep-debug', $pre_dll, $dbg_dll; -xit 0, @objcopy, '-g', '--keep-section=3D.gnu_debuglink_overlay', '--add-g= nu-debuglink=3D' . $dbg_dll, $pre_dll, $new_dll; -open(OBJDUMP, '-|', "$objdump --headers $new_dll"); -my %section; -while () { - my ($idx, $name, $size, $vma, $lma, $fileoff, $algn) =3D /^\s*(\d+)\s+= (\.\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*$/; - if ($name eq '.gnu_debuglink') { - push(@objcopy, '--set-section-flag', '.gnu_debuglink=3Dcontents,readonly,= debug,noload'); - $idx =3D $section{'.gnu_debuglink'}{-idx} if defined($section{'.gnu_debug= link'}{-idx}); - } elsif ($name eq '.gnu_debuglink_overlay') { - push (@objcopy, '-R', '.gnu_debuglink_overlay'); - $section{'.gnu_debuglink'}{-idx} =3D $idx; - next; - } - defined($idx) and - $section{$name} =3D {-idx=3D>int($idx), -size=3D>hex($size), -vma=3D= >hex($vma), -lma=3D>hex($lma), -fileoff=3D>hex($fileoff), - -algn=3D>0x00001000}; -} -close OBJDUMP; -my $vma; -for my $k (sort {$section{$a}{-idx} <=3D> $section{$b}{-idx}} keys %sectio= n) { - if ($strip && $k =3D~ /\.(?:stab|debug)/o) { - push(@objcopy, '-R', $k); - next; - } - if (!defined($vma)) { - $vma =3D $section{$k}{-vma}; - } - if ($vma !=3D $section{$k}{-vma}) { - my $newvma =3D align($vma, $section{$k}{-algn}); - if ($newvma !=3D $vma) { - printf STDERR "$0: ERROR $k VMA 0x%08x !=3D 0x%08x\n", $vma, $newvma; - exit 1; - } - push(@objcopy, '--change-section-address', sprintf "$k=3D0x%08x", $vma); + +sub xit { + my ( $execit, $cmdline, $verbose ) =3D @_; + print "+ ", join (' ', @$cmdline), "\n" if ($verbose); + if ($execit) { + exec @{$cmdline} or die "$0: couldn't exec @{$cmdline}[0] - $!\n"; + } else { + system @{$cmdline} and die "$0: couldn't exec @{$cmdline}[0] - $!\n"; } - $vma =3D align($vma + $section{$k}{-size}, $section{$k}{-algn}); } =20 -warn "$0: ERROR final VMA (" . sprintf("0x%08x", $vma) . ") not on 64K bou= ndary\n" if $vma !=3D align($vma, 64 * 1024); -push(@objcopy, $new_dll, @ARGV); -xit 1, @objcopy; -sub align { - my $n =3D $_[0]; - my $align =3D $_[1] - 1; - return ($n + $align) & ~$align; -} +my $objdump =3D shift; # path to objdump +my $objcopy =3D shift; # path to objcopy +my $pre_dll =3D shift; # name of input DLL, usually cygwin0.dll +my $dbg_dll =3D shift; # name of debug section file, usually cygwin1.dbg +my $new_dll =3D shift; # name of stripped DLL, usually new-cygwin1.dll +my $verbose =3D shift; # verbose flag, taken from Makefile's $(V) =20 -sub xit($@) { - my $execit =3D shift; - print "+ @_\n" if ($verbose); - if ($execit) { - exec @_ or die "$0: couldn't exec $_[0] - $!\n"; - } else { - system @_ and die "$0: couldn't exec $_[0] - $!\n"; +# Create cygwin1.dbg file +my @create_dbgfile =3D ( $objcopy, + '-R', + '.gnu_debuglink_overlay', + '--add-gnu-debuglink=3D/dev/null', + '--only-keep-debug', + $pre_dll, + $dbg_dll ); +&xit (0, \@create_dbgfile, $verbose); +# Create stripped new-cygwin1.dll +# The .gnu_debuglink section gets appended +my @create_stripped_dll =3D ( $objcopy, + '-g', + '--keep-section=3D.gnu_debuglink_overlay', + '--add-gnu-debuglink=3D' . $dbg_dll, + $pre_dll, + $new_dll ); +&xit (0, \@create_stripped_dll, $verbose); +# Fixup .gnu_debuglink section +my @fixup_debuglink =3D ( $objcopy ); +open (OBJDUMP, '-|', "$objdump --headers $new_dll"); +my $overlay_vma; +while () { + # Fetch line and split + my ($idx, $name, $size, $vma, $lma, $fileoff, $algn) + =3D /^\s*(\d+)\s+(\.\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*$= /; + # skip section flag lines + next if !defined ($idx); + # If we see a .gnu_debuglink_overlay section, use its section vma + # as vma of the .gnu_debuglink section. + if ($name eq '.gnu_debuglink_overlay') { + $overlay_vma =3D $vma; + # remove .gnu_debuglink_overlay section + push (@fixup_debuglink, + '-R', + '.gnu_debuglink_overlay'); + # move .gnu_debuglink to .gnu_debuglink_overlay section slot + push (@fixup_debuglink, + '--change-section-address', + '.gnu_debuglink=3D0x' . $overlay_vma); + # fix up section flags + push (@fixup_debuglink, + '--set-section-flag', + '.gnu_debuglink=3Dcontents,readonly,debug,noload'); } } +close OBJDUMP; +# If we didn't find a .gnu_debuglink_overlay section, bail out +defined ($overlay_vma) or die "$0: no .gnu_debuglink_overlay section!\n"; + +push (@fixup_debuglink, $new_dll); +&xit (1, \@fixup_debuglink, $verbose);