imgtools/buildrom/tools/efficient_rom_paging.pm
changeset 0 044383f39525
child 590 360bd6b35136
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 #
       
     2 # Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 # efficent_rom_paging.pm
       
    16 # changes the paging/unpaged configuration of binaries a generated
       
    17 # OBY file
       
    18 # use
       
    19 # externaltool=efficient_rom_paging
       
    20 # in oby file to enable
       
    21 # ## TODO
       
    22 # ## keyword alias isn't handled
       
    23 #
       
    24 
       
    25 package efficient_rom_paging;
       
    26 use strict;
       
    27 
       
    28 our @EXPORT=qw(
       
    29     efficient_rom_paging_info
       
    30     efficient_rom_paging_single
       
    31     efficient_rom_paging_multiple
       
    32 );
       
    33 require Exporter;
       
    34 our @ISA = qw(Exporter);
       
    35 our @EXPORT_OK = qw();
       
    36 
       
    37 
       
    38 my @moved_entry; # Holds all entries that has been moved without their dependencies
       
    39 
       
    40 my %seen; # Holds all dependencies to entries in moved_entry
       
    41 use File::Basename;
       
    42 
       
    43 # routine to provide information about the tool
       
    44 sub efficient_rom_paging_info ()
       
    45 {
       
    46     my %toolinfo;
       
    47     $toolinfo{'name'} = "efficient_rom_paging";
       
    48     $toolinfo{'invocation'} = "InvocationPoint2.5";
       
    49     $toolinfo{'multiple'} = \&efficient_rom_paging_multiple;
       
    50     $toolinfo{'single'} = \&efficient_rom_paging_single;
       
    51     return \%toolinfo;
       
    52 }
       
    53 
       
    54 # routine to handle multiple invocation
       
    55 sub efficient_rom_paging_multiple
       
    56 {
       
    57     my ($line) = @_;
       
    58     my @args=split /[=\s]/, $line;
       
    59     return "REM efficient_rom_paging.pm";
       
    60 }
       
    61 
       
    62 
       
    63 #
       
    64 # Hash of all executables and their paged attribute
       
    65 #
       
    66 my %executables;
       
    67 
       
    68 
       
    69 sub is_oby_statement
       
    70 {
       
    71 	my ($li) = @_;
       
    72 	if ($li =~ /\s*data\s*=/) { return 1;}
       
    73 	if ($li =~ /\s*file\s*=/) { return 1;}
       
    74 	if ($li =~ /\s*dll\s*=/) { return 1;}
       
    75 	if ($li =~ /\s*secondary\s*=/) { return 1;}
       
    76 
       
    77 	return 0;
       
    78 }
       
    79 
       
    80 # Scan OBY file and move ROM_IMAGE[1] paged executables to ROM_IMAGE[0] part
       
    81 sub efficient_rom_paging_single
       
    82 {
       
    83 	my ($oby) = @_;
       
    84 	
       
    85 	print "efficient_rom_paging.pm: Making paging more efficient.\n";
       
    86 	
       
    87 	my $rofs_start = 0;
       
    88 	#Find ROFS partition
       
    89 	foreach my $line (@$oby)
       
    90 	{
       
    91 		if ($line =~ /ROM_IMAGE\[1\]/i)
       
    92 		{
       
    93 			last;
       
    94 		}
       
    95 		$rofs_start++;
       
    96 	}
       
    97 
       
    98 	my @rom_core_partition = @$oby[0 .. $rofs_start-1];
       
    99 	my @rofs_partition = @$oby[$rofs_start .. $#$oby];
       
   100 	
       
   101 	if (is_pagingoverride_nopaged($oby))
       
   102 	{
       
   103 		move_all_paged_and_default_nonexecutables_to_core(\@rom_core_partition, \@rofs_partition);
       
   104 		move_all_aliases_to_core(\@rom_core_partition, \@rofs_partition);
       
   105 		@$oby = (@rom_core_partition, @rofs_partition);
       
   106 		print "\n";
       
   107 		return;
       
   108 	}
       
   109 
       
   110 	setup_pageable_attribute_array(\@rofs_partition);
       
   111 
       
   112 	if (is_pagingoverride_defaultpaged($oby))
       
   113 	{
       
   114 		move_all_default_executables_to_core(\@rom_core_partition, \@rofs_partition);
       
   115 	}
       
   116 
       
   117 	move_all_paged_and_default_nonexecutables_to_core(\@rom_core_partition, \@rofs_partition);
       
   118 
       
   119 	if (is_pagingoverride_alwayspage($oby))
       
   120 	{
       
   121 		move_all_executables_to_core(\@rom_core_partition, \@rofs_partition);
       
   122 		move_all_aliases_to_core(\@rom_core_partition, \@rofs_partition);
       
   123 		@$oby = (@rom_core_partition, @rofs_partition);
       
   124 		print "\n";
       
   125 		return;
       
   126 	}
       
   127 	
       
   128 	move_all_paged_executables_to_core(\@rom_core_partition, \@rofs_partition);
       
   129 	move_all_dependencies_to_core(\@rom_core_partition, \@rofs_partition, $oby);
       
   130 	move_all_aliases_to_core(\@rom_core_partition, \@rofs_partition);
       
   131 	move_all_renames_to_core(\@rom_core_partition, \@rofs_partition);
       
   132 	move_all_patchdata_to_core(\@rom_core_partition, \@rofs_partition);
       
   133 	
       
   134 	@$oby = (@rom_core_partition, @rofs_partition);
       
   135 	print "\n";
       
   136 }
       
   137 
       
   138 sub is_pagingoverride_nopaged
       
   139 {
       
   140 	my ($oby) = @_;
       
   141 	
       
   142 	for my $line (@$oby)
       
   143 	{
       
   144 		if ($line =~ /pagingoverride\s+nopaging/i)
       
   145 		{
       
   146 			return 1;
       
   147 		}
       
   148 	}
       
   149 	return 0;
       
   150 }
       
   151 
       
   152 sub is_pagingoverride_defaultpaged
       
   153 {
       
   154 	my ($oby) = @_;
       
   155 	
       
   156 	for my $line (@$oby)
       
   157 	{
       
   158 		if ($line =~ /pagingoverride\s+defaultpaged/i)
       
   159 		{
       
   160 			return 1;
       
   161 		}
       
   162 	}
       
   163 	return 0;
       
   164 }
       
   165 
       
   166 sub is_pagingoverride_alwayspage
       
   167 {
       
   168 	my ($oby) = @_;
       
   169 	
       
   170 	for my $line (@$oby)
       
   171 	{
       
   172 		if ($line =~ /pagingoverride\s+alwayspage/i)
       
   173 		{
       
   174 			return 1;
       
   175 		}
       
   176 	}
       
   177 	return 0;
       
   178 }
       
   179 
       
   180 
       
   181 
       
   182 sub setup_pageable_attribute_array
       
   183 {
       
   184 	my ($rofs_partition) = @_;
       
   185 	my $counter = 0;
       
   186 	for my $line (@$rofs_partition)
       
   187 	{
       
   188 		if (is_oby_statement($line))
       
   189 		{
       
   190 			my $executable;
       
   191 			$line =~ /file=(\S+)/i;
       
   192 			$executable = $1;
       
   193 
       
   194 			open DUMP, "ELFTRAN -dump h $executable |" or die "Can't execute ELFTRAN\n";
       
   195 			while (my $line=<DUMP>)
       
   196 			{
       
   197 				if ($line =~ /pageability : (\S+)/i)
       
   198 				{
       
   199 					$executables{$executable} = $1;
       
   200 					print "." if (($counter++ % 10) == 0);
       
   201 				}
       
   202 			}
       
   203 			
       
   204 			close DUMP;
       
   205 		}
       
   206 	}
       
   207 }
       
   208 
       
   209 
       
   210 sub move_all_paged_executables_to_core
       
   211 {
       
   212 	my ($rom_core, $rofs) = @_;
       
   213 	
       
   214 	my @rofs_execs = grep {$_ =~ /file\s*=/i} @$rofs;
       
   215 
       
   216 	for my $line (@rofs_execs)
       
   217 	{
       
   218 		my $executable;
       
   219 		$line =~ /file=(\S+) /;
       
   220 		$executable = $1;
       
   221 
       
   222 		if ($line =~ /\s+paged$/i || ($line !~ /\s+unpaged$/i && lc($executables{$executable}) eq 'paged'))
       
   223 		{
       
   224 
       
   225 			push @$rom_core, $line; # Add line to rom core.
       
   226 			# Save the executables in a list. Will be used as a cache when dependencies are searched for.
       
   227 			push @moved_entry, $executable; 
       
   228 			@$rofs = grep {$_ ne $line} @$rofs; # Remove line from rofs partition
       
   229 		}
       
   230 	}
       
   231 }
       
   232 
       
   233 sub move_all_paged_nonexecutables_to_core
       
   234 {
       
   235 	my ($rom_core_partition, $rofs_partition) = @_;
       
   236 	
       
   237 	my @rofs_data = grep {$_ =~ /data\s*=\s*\S+\s+\S*\s+paged/i} @$rofs_partition;
       
   238 	
       
   239 	for my $line (@rofs_data)
       
   240 	{
       
   241 		push @$rom_core_partition, $line;
       
   242 		@$rofs_partition = grep {$_ ne $line} @$rofs_partition;
       
   243 	}
       
   244 }
       
   245 
       
   246 sub move_all_paged_and_default_nonexecutables_to_core
       
   247 {
       
   248 	my ($rom_core_partition, $rofs_partition) = @_;
       
   249 	
       
   250 	move_all_paged_nonexecutables_to_core($rom_core_partition, $rofs_partition);
       
   251 
       
   252 	my @rofs_data = grep {$_ =~ /data=\s*/} @$rofs_partition;
       
   253 	@rofs_data = grep {$_ !~ /unpaged/ } @rofs_data;
       
   254 	
       
   255 	for my $line (@rofs_data)
       
   256 	{
       
   257 		push @$rom_core_partition, $line;
       
   258 		@$rofs_partition = grep {$_ ne $line} @$rofs_partition;
       
   259 	}
       
   260 }
       
   261 
       
   262 sub move_all_default_executables_to_core
       
   263 {
       
   264 	my ($rom_core_partition, $rofs_partition) = @_;
       
   265 	
       
   266 	my @rofs_execs = grep {$_ =~ /file\s*=/i} @$rofs_partition;
       
   267 	
       
   268 	# If they have been set to unpaged in the oby file, they shouldn't be moved over.
       
   269 	for my $executable (sort keys %executables)
       
   270 	{
       
   271 		if ($executables{$executable} =~ /default/i)
       
   272 		{
       
   273 			for my $line (@rofs_execs)
       
   274 			{
       
   275 				if ($line !~ /unpaged\s*$/i)
       
   276 				{
       
   277 					my $rofs_executable = $line;
       
   278 					$rofs_executable =~ /file\s*=\s*(\S+)/i;
       
   279 					$rofs_executable = $1;
       
   280 					
       
   281 					if ($rofs_executable eq $executable)
       
   282 					{
       
   283 						push @moved_entry, $executable; 
       
   284 						push @$rom_core_partition, $line;
       
   285 						@$rofs_partition = grep {$_ ne $line} @$rofs_partition;
       
   286 						last;
       
   287 					}
       
   288 				}
       
   289 			}
       
   290 		}
       
   291 	}
       
   292 }
       
   293 
       
   294 sub move_all_executables_to_core
       
   295 {
       
   296 	my ($rom_core_partition, $rofs_partition) = @_;
       
   297 	
       
   298 	my @all_executables = grep {$_ =~ /file\s*=/i} @$rofs_partition;
       
   299 	
       
   300 	for my $line (@all_executables)
       
   301 	{
       
   302 		push @$rom_core_partition, $line;
       
   303 		push @moved_entry, $line;
       
   304 		@$rofs_partition = grep {$_ ne $line} @$rofs_partition;
       
   305 	}
       
   306 }
       
   307 
       
   308 sub move_all_aliases_to_core
       
   309 {
       
   310 	my ($rom_core_partition, $rofs_partition) = @_;
       
   311 	
       
   312 	my @all_aliases = grep {$_ =~ /^alias\s+\S+\s+\S+/i} @$rofs_partition;
       
   313 	
       
   314 	for my $line (@all_aliases)
       
   315 	{
       
   316 		$line =~ /^alias\s+(\S+)\s+\S+/i;
       
   317 		my $alias = $1;
       
   318 		$alias = basename($alias);
       
   319 
       
   320 		my @file_stmt = grep {$_ =~ /^\s*(extension|device|file|data)(\[\S+\])?\s*=\s*\S+\s+\S*($alias)(\")?[\s+|\r](\s+\S+)*\s*$/i} @$rom_core_partition;
       
   321  		if(scalar @file_stmt)
       
   322 		{
       
   323 			push @$rom_core_partition, $line;
       
   324 			@$rofs_partition = grep {$_ ne $line} @$rofs_partition;
       
   325 		}
       
   326 	}
       
   327 }
       
   328 
       
   329 sub move_all_renames_to_core
       
   330 {
       
   331 	my ($rom_core_partition, $rofs_partition) = @_;
       
   332 	
       
   333 	my @all_renames = grep {$_ =~ /^rename\s+\S+\s+\S+/i} @$rofs_partition;
       
   334 	
       
   335 	for my $line (@all_renames)
       
   336 	{
       
   337 		$line =~ /^rename\s+(\S+)\s+\S+/i;
       
   338 		my $rename = $1;
       
   339 		$rename = basename($rename);
       
   340 
       
   341 		for (my $i=0; $i < scalar @$rom_core_partition; $i++)
       
   342 		{
       
   343 			if ($rename =~ $$rom_core_partition[$i])
       
   344 			{
       
   345 				splice(@$rom_core_partition, $i+1, 0, $line);
       
   346 				@$rofs_partition = grep {$_ ne $line} @$rofs_partition;
       
   347 				last;
       
   348 			}
       
   349 		}
       
   350 	}
       
   351 }
       
   352 
       
   353 sub move_all_patchdata_to_core
       
   354 {
       
   355 	my ($rom_core_partition, $rofs_partition) = @_;
       
   356 	
       
   357 	my @all_patchdata = grep {$_ =~ /^patchdata\s+\S+\s+\S+\s+/i} @$rofs_partition;
       
   358 	
       
   359 	for my $line (@all_patchdata)
       
   360 	{
       
   361 		$line =~ /^patchdata\s+(\S+)\s+\S+\s+/i;
       
   362 		my $patchdata = $1;
       
   363 		$patchdata = basename($patchdata);
       
   364 
       
   365 		if (grep($patchdata, @$rom_core_partition))
       
   366 		{
       
   367 			push @$rom_core_partition, $line;
       
   368 			@$rofs_partition = grep {$_ ne $line} @$rofs_partition;
       
   369 		}
       
   370 	}
       
   371 }
       
   372 
       
   373 sub move_all_dependencies_to_core
       
   374 {
       
   375 	my ($rom_core_partition, $rofs_partition, $oby) = @_;
       
   376 	my $counter = 0;
       
   377 	
       
   378 	for my $d (@moved_entry) 
       
   379 	{
       
   380 		if (!exists $seen{$d})
       
   381 		{
       
   382 			$seen{$d}=();
       
   383 			listcomp("", $d, $oby);
       
   384 			$counter++;
       
   385 			print "." if (($counter % 10) == 0);
       
   386 		}
       
   387 	}
       
   388 	
       
   389 	# delete all dependencies that have already been moved to core rom.		
       
   390 	for my $exec (@moved_entry)
       
   391 	{
       
   392 		if (exists $seen{$exec})
       
   393 		{
       
   394 			delete $seen{$exec};
       
   395 		}
       
   396 	}
       
   397 	# move all dependencies to rom core
       
   398 	my @rofs_execs = grep {$_ =~/file\s*=/i} @$rofs_partition;
       
   399 	
       
   400 	for my $exec (keys %seen)
       
   401 	{
       
   402 		for my $line (@rofs_execs)
       
   403 		{
       
   404 			my $rofs_exec = $line;
       
   405 			$rofs_exec =~ /file\s*=\s*\S+\s+(\S+)/i;
       
   406 			$rofs_exec = $1;
       
   407 			$rofs_exec =~ s/"//g;
       
   408 			
       
   409 			if (basename(lc($exec)) eq basename(lc($rofs_exec)))
       
   410 			{
       
   411 				push @$rom_core_partition, $line;
       
   412 				push @moved_entry, $line;
       
   413 				@$rofs_partition = grep {$_ ne $line} @$rofs_partition;
       
   414 				last;
       
   415 			}
       
   416 		}
       
   417 	}
       
   418 }
       
   419 
       
   420 # for each exe, list the dependencies
       
   421 
       
   422 sub listcomp
       
   423 {
       
   424 	my ($deps, $comp, $oby) = @_;
       
   425 	# find dependencies of comp
       
   426 	my @ar=getdeps($comp, $oby);
       
   427 	# recurse over new dependencies
       
   428 	foreach my $d (@ar)
       
   429 	{
       
   430 		$d=lc($d);
       
   431 		if (!exists $seen{$d})
       
   432 		{
       
   433  			# recurse
       
   434 			$seen{$d}=();
       
   435 			listcomp($deps, $d, $oby);
       
   436 		}
       
   437 	}
       
   438 }
       
   439 
       
   440 sub getdeps
       
   441 {
       
   442 	my ($comp, $oby) = @_;
       
   443 	my @list=();
       
   444 	my $hw_base_name = basename($comp);
       
   445 	for my $line (@$oby)
       
   446 	{
       
   447 		if (is_oby_statement($line))
       
   448 		{
       
   449 			if ($line =~ /\\$hw_base_name/i)
       
   450 			{
       
   451 				$line =~ /\s*=\s*(\S+)\s*/;
       
   452 				$comp = $1;
       
   453 				last;
       
   454 			}
       
   455 		}
       
   456 	}
       
   457 	
       
   458 	open DUMP, "ELFTRAN -dump i $comp |" or die "Can't execute ELFTRAN\n";
       
   459 	while (my $line=<DUMP>)
       
   460 	{
       
   461 		if ($line =~ /imports from (\S+)/i)
       
   462 		{
       
   463 			my $d = $1;
       
   464 			$d =~ s/\{\S{8}\}//; # remove {00000000}
       
   465 			$d =~ s/\[\S{8}\]//; # remove [00000000]
       
   466 
       
   467 			push @list, "$d";
       
   468 		}
       
   469 	}
       
   470 	close DUMP;
       
   471 	return @list;
       
   472 }
       
   473 
       
   474 1;