generate_initcall_order.pl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Generates a linker script that specifies the correct initcall order.
  5. #
  6. # Copyright (C) 2019 Google LLC
  7. use strict;
  8. use warnings;
  9. use IO::Handle;
  10. my $nm = $ENV{'LLVM_NM'} || "llvm-nm";
  11. my $ar = $ENV{'AR'} || "llvm-ar";
  12. my $objtree = $ENV{'objtree'} || ".";
  13. ## list of all object files to process, in link order
  14. my @objects;
  15. ## currently active child processes
  16. my $jobs = {}; # child process pid -> file handle
  17. ## results from child processes
  18. my $results = {}; # object index -> { level, function }
  19. ## reads _NPROCESSORS_ONLN to determine the number of processes to start
  20. sub get_online_processors {
  21. open(my $fh, "getconf _NPROCESSORS_ONLN 2>/dev/null |")
  22. or die "$0: failed to execute getconf: $!";
  23. my $procs = <$fh>;
  24. close($fh);
  25. if (!($procs =~ /^\d+$/)) {
  26. return 1;
  27. }
  28. return int($procs);
  29. }
  30. ## finds initcalls defined in an object file, parses level and function name,
  31. ## and prints it out to the parent process
  32. sub find_initcalls {
  33. my ($object) = @_;
  34. die "$0: object file $object doesn't exist?" if (! -f $object);
  35. open(my $fh, "\"$nm\" --just-symbol-name --defined-only \"$object\" 2>/dev/null |")
  36. or die "$0: failed to execute \"$nm\": $!";
  37. my $initcalls = {};
  38. while (<$fh>) {
  39. chomp;
  40. my ($counter, $line, $symbol) = $_ =~ /^__initcall_(\d+)_(\d+)_(.*)$/;
  41. if (!defined($counter) || !defined($line) || !defined($symbol)) {
  42. next;
  43. }
  44. my ($function, $level) = $symbol =~
  45. /^(.*)((early|rootfs|con|security|[0-9])s?)$/;
  46. die "$0: duplicate initcall counter value in object $object: $_"
  47. if exists($initcalls->{$counter});
  48. $initcalls->{$counter} = {
  49. 'level' => $level,
  50. 'line' => $line,
  51. 'function' => $function
  52. };
  53. }
  54. close($fh);
  55. # sort initcalls in each object file numerically by the counter value
  56. # to ensure they are in the order they were defined
  57. foreach my $counter (sort { $a <=> $b } keys(%{$initcalls})) {
  58. print $initcalls->{$counter}->{"level"} . " " .
  59. $counter . " " .
  60. $initcalls->{$counter}->{"line"} . " " .
  61. $initcalls->{$counter}->{"function"} . "\n";
  62. }
  63. }
  64. ## waits for any child process to complete, reads the results, and adds them to
  65. ## the $results array for later processing
  66. sub wait_for_results {
  67. my $pid = wait();
  68. if ($pid > 0) {
  69. my $fh = $jobs->{$pid};
  70. # the child process prints out results in the following format:
  71. # line 1: <object file index>
  72. # line 2..n: <level> <counter> <line> <function>
  73. my $index = <$fh>;
  74. chomp($index);
  75. if (!($index =~ /^\d+$/)) {
  76. die "$0: child $pid returned an invalid index: $index";
  77. }
  78. $index = int($index);
  79. while (<$fh>) {
  80. chomp;
  81. my ($level, $counter, $line, $function) = $_ =~
  82. /^([^\ ]+)\ (\d+)\ (\d+)\ (.*)$/;
  83. if (!defined($level) ||
  84. !defined($counter) ||
  85. !defined($line) ||
  86. !defined($function)) {
  87. die "$0: child $pid returned invalid data";
  88. }
  89. if (!exists($results->{$index})) {
  90. $results->{$index} = [];
  91. }
  92. push (@{$results->{$index}}, {
  93. 'level' => $level,
  94. 'counter' => $counter,
  95. 'line' => $line,
  96. 'function' => $function
  97. });
  98. }
  99. close($fh);
  100. delete($jobs->{$pid});
  101. }
  102. }
  103. ## launches child processes to find initcalls from the object files, waits for
  104. ## each process to complete and collects the results
  105. sub process_objects {
  106. my $index = 0; # link order index of the object file
  107. my $njobs = get_online_processors();
  108. while (scalar(@objects) > 0) {
  109. my $object = shift(@objects);
  110. # fork a child process and read it's stdout
  111. my $pid = open(my $fh, '-|');
  112. if (!defined($pid)) {
  113. die "$0: failed to fork: $!";
  114. } elsif ($pid) {
  115. # save the child process pid and the file handle
  116. $jobs->{$pid} = $fh;
  117. } else {
  118. STDOUT->autoflush(1);
  119. print "$index\n";
  120. find_initcalls("$objtree/$object");
  121. exit;
  122. }
  123. $index++;
  124. # if we reached the maximum number of processes, wait for one
  125. # to complete before launching new ones
  126. if (scalar(keys(%{$jobs})) >= $njobs && scalar(@objects) > 0) {
  127. wait_for_results();
  128. }
  129. }
  130. # wait for the remaining children to complete
  131. while (scalar(keys(%{$jobs})) > 0) {
  132. wait_for_results();
  133. }
  134. }
  135. ## gets a list of actual object files from thin archives, and adds them to
  136. ## @objects in link order
  137. sub find_objects {
  138. while (my $file = shift(@ARGV)) {
  139. my $pid = open (my $fh, "\"$ar\" t \"$file\" 2>/dev/null |")
  140. or die "$0: failed to execute $ar: $!";
  141. my @output;
  142. while (<$fh>) {
  143. chomp;
  144. push(@output, $_);
  145. }
  146. close($fh);
  147. # if $ar failed, assume we have an object file
  148. if ($? != 0) {
  149. push(@objects, $file);
  150. next;
  151. }
  152. # if $ar succeeded, read the list of object files
  153. foreach (@output) {
  154. push(@objects, $_);
  155. }
  156. }
  157. }
  158. ## START
  159. find_objects();
  160. process_objects();
  161. ## process results and add them to $sections in the correct order
  162. my $sections = {};
  163. foreach my $index (sort { $a <=> $b } keys(%{$results})) {
  164. foreach my $result (@{$results->{$index}}) {
  165. my $level = $result->{'level'};
  166. if (!exists($sections->{$level})) {
  167. $sections->{$level} = [];
  168. }
  169. my $fsname = $result->{'counter'} . '_' .
  170. $result->{'line'} . '_' .
  171. $result->{'function'};
  172. push(@{$sections->{$level}}, $fsname);
  173. }
  174. }
  175. if (!keys(%{$sections})) {
  176. exit(0); # no initcalls...?
  177. }
  178. ## print out a linker script that defines the order of initcalls for each
  179. ## level
  180. print "SECTIONS {\n";
  181. foreach my $level (sort(keys(%{$sections}))) {
  182. my $section;
  183. if ($level eq 'con') {
  184. $section = '.con_initcall.init';
  185. } elsif ($level eq 'security') {
  186. $section = '.security_initcall.init';
  187. } else {
  188. $section = ".initcall${level}.init";
  189. }
  190. print "\t${section} : {\n";
  191. foreach my $fsname (@{$sections->{$level}}) {
  192. print "\t\t*(${section}..${fsname}) ;\n"
  193. }
  194. print "\t}\n";
  195. }
  196. print "}\n";