OSDN Git Service

* path.cc (conv_path_list): Take cygwin_conv_path_t as third parameter.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / dllfixdbg
1 #!/usr/bin/perl
2 # Copyright 2006, 2007 Red Hat, Inc.
3 #
4 # This file is part of Cygwin.
5 #
6 # This software is a copyrighted work licensed under the terms of the
7 # Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
8 # details.
9 #
10 use integer;
11 use strict;
12 sub xit($@);
13 my $strip = $ARGV[0] eq '-s';
14 shift if $strip;
15 my $objdump = shift;
16 my @objcopy = ((shift));
17 my $dll = shift;
18 my $dbg = shift;
19 xit 0, @objcopy, '-R', '.gnu_debuglink_overlay', '--only-keep-debug', $dll, $dbg;
20 xit 0, @objcopy, '-g', '--add-gnu-debuglink=' . $dbg, $dll;
21 open(OBJDUMP, '-|', "$objdump --headers $dll");
22 my %section;
23 while (<OBJDUMP>) {
24     my ($idx, $name, $size, $vma, $lma, $fileoff, $algn) = /^\s*(\d+)\s+(\.\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*$/;
25     if ($name eq '.gnu_debuglink') {
26         push(@objcopy, '--set-section-flag', '.gnu_debuglink=contents,readonly,debug,noload');
27         $idx = $section{'.gnu_debuglink'}{-idx} if defined($section{'.gnu_debuglink'}{-idx});
28     } elsif ($name eq '.gnu_debuglink_overlay') {
29         push (@objcopy, '-R', '.gnu_debuglink_overlay');
30         $section{'.gnu_debuglink'}{-idx} = $idx;
31         next;
32     }
33     defined($idx) and
34       $section{$name} = {-idx=>int($idx), -size=>hex($size), -vma=>hex($vma), -lma=>hex($lma), -fileoff=>hex($fileoff),
35           -algn=>0x00001000};
36 }
37 close OBJDUMP;
38 my $vma;
39 for my $k (sort {$section{$a}{-idx} <=> $section{$b}{-idx}} keys %section) {
40     if ($strip && $k =~ /\.(?:stab|debug)/o) {
41         push(@objcopy, '-R', $k);
42         next;
43     }
44     if (!defined($vma)) {
45         $vma = $section{$k}{-vma};
46     }
47     if ($vma != $section{$k}{-vma}) {
48         my $newvma = align($vma, $section{$k}{-algn});
49         if ($newvma != $vma) {
50             printf STDERR "$0: ERROR $k VMA 0x%08x != 0x%08x\n", $vma, $newvma;
51             exit 1;
52         }
53         push(@objcopy, '--change-section-address', sprintf "$k=0x%08x", $vma);
54     }
55     $vma = align($vma + $section{$k}{-size}, $section{$k}{-algn});
56 }
57
58 warn "$0: ERROR final VMA (" . sprintf("0x%08x", $vma) . ") not on 64K boundary\n" if $vma != align($vma, 64 * 1024);
59 push(@objcopy, $dll, @ARGV);
60 xit 1, @objcopy;
61 sub align {
62     my $n = $_[0];
63     my $align = $_[1] - 1;
64     return ($n + $align) & ~$align;
65 }
66
67 sub xit($@) {
68     my $execit = shift;
69     print "+ @_\n";
70     if ($execit) {
71         exec @_ or die "$0: couldn't exec $_[0] - $!\n";
72     } else {
73         system @_ and die "$0: couldn't exec $_[0] - $!\n";
74     }
75 }