2 #############################################################################
4 ## Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
5 ## Contact: http://www.qt-project.org/
7 ## This file is part of the utilities of the Qt Toolkit.
9 ## $QT_BEGIN_LICENSE:LGPL$
10 ## GNU Lesser General Public License Usage
11 ## This file may be used under the terms of the GNU Lesser General Public
12 ## License version 2.1 as published by the Free Software Foundation and
13 ## appearing in the file LICENSE.LGPL included in the packaging of this
14 ## file. Please review the following information to ensure the GNU Lesser
15 ## General Public License version 2.1 requirements will be met:
16 ## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ## In addition, as a special exception, Nokia gives you certain additional
19 ## rights. These rights are described in the Nokia Qt LGPL Exception
20 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ## GNU General Public License Usage
23 ## Alternatively, this file may be used under the terms of the GNU General
24 ## Public License version 3.0 as published by the Free Software Foundation
25 ## and appearing in the file LICENSE.GPL included in the packaging of this
26 ## file. Please review the following information to ensure the GNU General
27 ## Public License version 3.0 requirements will be met:
28 ## http://www.gnu.org/copyleft/gpl.html.
31 ## Alternatively, this file may be used in accordance with the terms and
32 ## conditions contained in a signed written agreement between you and Nokia.
41 #############################################################################
43 # A script to get around some shortcomings in elf2e32, namely:
44 # - Returning 0 even when there are errors.
45 # - Excluding symbols from the dso file even when they are present in the ELF file.
46 # - Including symbols in the the dso file even when they are not present in the ELF file.
47 # - Overwriting the old dso file even when there are no changes (increases build time).
58 @definput = split('=', $_);
59 } elsif (/^--defoutput/o) {
60 @defoutput = split('=', $_);
62 @dso = split('=', $_);
63 } elsif (/^--tmpdso/o) {
64 @tmpdso = split('=', $_);
71 @definput = () if (!@definput || ! -e $definput[1]);
73 if (@dso && !@tmpdso || !@dso && @tmpdso) {
74 print("--dso and --tmpdso must be used together.\n");
78 my $buildingLibrary = (@defoutput && @dso) ? 1 : 0;
84 # For debugging. Make it nonzero to give verbose output.
88 return if (!$debugScript);
90 my ($msg, $file) = @_;
91 my $content = "$msg, $file:\n";
93 if (!open($defFileFd, "< $file")) {
94 print("Warning: Could not open $file (for debug analysis)\n");
97 while (<$defFileFd>) {
101 push(@usedDefFiles, $content);
103 sub printRecordedDefFiles {
104 return if (!$debugScript);
106 foreach (@usedDefFiles) {
111 sub missingSymbolMismatch
113 my $missingSymbolSum = $_[0];
115 printRecordedDefFiles;
117 print("Bug in the native elf2e32 tool: Number of missing symbols does not\n");
118 print("match number of removed symbols in the output DEF file.\n\n");
120 print("Original elf2e32 output:\n");
121 print(" $missingSymbolSum Frozen Export\(s\) missing from the ELF file\n\n");
123 print("However $defoutput[1] contains more missing entries than that.\n\n");
125 print("This needs to be fixed manually in the DEF file.\n");
130 print("PATH: $ENV{PATH}\n");
131 print("EPOCROOT: $ENV{EPOCROOT}\n");
135 if (++$runCount > 2) {
136 printRecordedDefFiles if ($debugScript);
137 print("Internal error in $0, link succeeded, but exports may be wrong.\n");
142 my $elf2e32Cmd = "elf2e32 @args"
143 . " " . join("=", @definput)
144 . " " . join("=", @defoutput)
145 . " " . join("=", @tmpdso);
146 open($elf2e32Pipe, "$elf2e32Cmd 2>&1 |") or die ("Could not run elf2e32");
149 my $foundBrokenSymbols = 0;
150 my $missingSymbolSum = 0;
151 my $missingSymbolCount = 0;
153 while (<$elf2e32Pipe>) {
157 } elsif (/symbol ([a-z0-9_]+) absent in the DEF file, but present in the ELF file/io) {
158 $fixupSymbols{$1} = 1;
159 $foundBrokenSymbols = 1;
160 } elsif (/([0-9]+) Frozen Export\(s\) missing from the ELF file/io) {
161 $missingSymbolSum = $1;
162 $foundBrokenSymbols = 1;
168 recordDefFile("Run no $runCount, elf2e32 DEF file input", "$definput[1]");
169 recordDefFile("Run no $runCount, elf2e32 DEF file output", "$defoutput[1]");
177 if ($buildingLibrary && $runCount == 1) {
181 my $savedNewDefFileLine = "";
183 open($origDefFile, "< $definput[1]") or die("Could not open $definput[1]");
185 open($newDefFile, "< $defoutput[1]") or die("Could not open $defoutput[1]");
186 open($tmpDefFile, "> $defoutput[1].tmp") or die("Could not open $defoutput[1].tmp");
187 print($tmpDefFile "EXPORTS\n") or die("Could not write to temporary DEF file: $!");
188 $fixupFile = "$defoutput[1].tmp";
203 # Read from original def file, and skip non-symbol lines
205 $origDefLine = <$origDefFile>;
206 if (defined($origDefLine)) {
207 $origDefLine =~ s/[\n\r]//;
208 if ($origDefLine =~ /([a-z0-9_]+) +\@ *([0-9]+) (.*)/i) {
220 if ($savedNewDefFileLine) {
221 # This happens if the new def file was missing an entry.
222 $newDefLine = $savedNewDefFileLine;
223 $newDefLine =~ /([a-z0-9_]+) +\@ *([0-9]+) (.*)/i or die("$0: Shouldn't happen");
228 # Read from new def file, and skip non-symbol lines
230 $newDefLine = <$newDefFile>;
231 if (defined($newDefLine)) {
232 $newDefLine =~ s/[\n\r]//;
233 if ($newDefLine =~ /([a-z0-9_]+) +\@ *([0-9]+) (.*)/i) {
244 $savedNewDefFileLine = "";
245 last if (!defined($origDefLine) && !defined($newDefLine));
247 if (defined($origOrdinal) && (!defined($newOrdinal) || $origOrdinal != $newOrdinal)) {
248 # If the symbol is missing from the new def file, use the original symbol.
249 $savedNewDefFileLine = $newDefLine;
250 $defLine = $origDefLine;
252 $ordinal = $origOrdinal;
253 $extraData = $origExtraData;
255 $defLine = $newDefLine;
257 $ordinal = $newOrdinal;
258 if ($newExtraData =~ /ABSENT/) {
259 # Special case to keep "DATA [0-9]+" data in absent entries.
260 $extraData = $origExtraData;
262 $extraData = $newExtraData;
265 if (exists($fixupSymbols{$sym})) {
266 # Fix symbols that have returned after first being marked ABSENT.
267 $extraData =~ s/ ABSENT//;
268 } elsif ($defLine =~ s/; MISSING://) {
269 # Auto-absent symbols.
270 $extraData .= " ABSENT";
271 if (++$missingSymbolCount > $missingSymbolSum) {
272 missingSymbolMismatch($missingSymbolSum);
275 print($tmpDefFile "\t$sym \@ $ordinal $extraData\n") or die("Could not write to temporary DEF file: $!");
277 print($tmpDefFile "\n") or die("Could not write to temporary DEF file: $!");
278 close($origDefFile) if ($definput[1]);
282 $definput[1] = "$defoutput[1].tmp";
285 if (!$foundBrokenSymbols || $errors) {
289 print("Rerunning elf2e32 due to DEF file / ELF file mismatch\n");
293 unlink($defoutput[1]);
294 move($fixupFile, $defoutput[1]);
297 exit $returnCode if ($returnCode != 0);
299 if ($buildingLibrary) {
300 my $differenceFound = 0;
307 open($dsoFile, "< $dso[1]") or die("Could not open $dso[1]");
308 open($tmpdsoFile, "< $tmpdso[1]") or die("Could not open $tmpdso[1]");
310 binmode($tmpdsoFile);
311 while(read($dsoFile, $dsoBuf, 4096) && read($tmpdsoFile, $tmpdsoBuf, 4096)) {
312 if ($dsoBuf ne $tmpdsoBuf) {
313 $differenceFound = 1;
319 $differenceFound = 1;
322 if ($differenceFound) {
323 copy($tmpdso[1], $dso[1]) or die("Could not copy $tmpdso[1] to $dso[1]: $!");