2 # SPDX-License-Identifier: GPL-2.0
4 # Generates a linker script that specifies the correct initcall order.
6 # Copyright (C) 2019 Google LLC
12 use POSIX ":sys_wait_h";
14 my $nm = $ENV{'NM'} || die "$0: ERROR: NM not set?";
15 my $objtree = $ENV{'objtree'} || '.';
17 ## currently active child processes
18 my $jobs = {}; # child process pid -> file handle
19 ## results from child processes
20 my $results = {}; # object index -> [ { level, secname }, ... ]
22 ## reads _NPROCESSORS_ONLN to determine the maximum number of processes to
24 sub get_online_processors {
25 open(my $fh, "getconf _NPROCESSORS_ONLN 2>/dev/null |")
26 or die "$0: ERROR: failed to execute getconf: $!";
30 if (!($procs =~ /^\d+$/)) {
37 ## writes results to the parent process
38 ## format: <file index> <initcall level> <base initcall section name>
40 my ($index, $initcalls) = @_;
42 # sort by the counter value to ensure the order of initcalls within
43 # each object file is correct
44 foreach my $counter (sort { $a <=> $b } keys(%{$initcalls})) {
45 my $level = $initcalls->{$counter}->{'level'};
47 # section name for the initcall function
48 my $secname = $initcalls->{$counter}->{'module'} . '__' .
50 $initcalls->{$counter}->{'line'} . '_' .
51 $initcalls->{$counter}->{'function'};
53 print "$index $level $secname\n";
57 ## reads a result line from a child process and adds it to the $results array
61 # each child prints out a full line w/ autoflush and exits after the
62 # last line, so even if buffered I/O blocks here, it shouldn't block
66 if (!defined($data)) {
72 my ($index, $level, $secname) = $data =~
73 /^(\d+)\ ([^\ ]+)\ (.*)$/;
75 if (!defined($index) ||
78 die "$0: ERROR: child process returned invalid data: $data\n";
83 if (!exists($results->{$index})) {
84 $results->{$index} = [];
87 push (@{$results->{$index}}, {
95 ## finds initcalls from an object file or all object files in an archive, and
96 ## writes results back to the parent process
98 my ($index, $file) = @_;
100 die "$0: ERROR: file $file doesn't exist?" if (! -f $file);
102 open(my $fh, "\"$nm\" --defined-only \"$file\" 2>/dev/null |")
103 or die "$0: ERROR: failed to execute \"$nm\": $!";
110 # check for the start of a new object file (if processing an
112 my ($path)= $_ =~ /^(.+)\:$/;
114 if (defined($path)) {
115 write_results($index, $initcalls);
120 # look for an initcall
121 my ($module, $counter, $line, $symbol) = $_ =~
122 /[a-z]\s+__initcall__(\S*)__(\d+)_(\d+)_(.*)$/;
124 if (!defined($module)) {
128 if (!defined($counter) ||
134 # parse initcall level
135 my ($function, $level) = $symbol =~
136 /^(.*)((early|rootfs|con|[0-9])s?)$/;
138 die "$0: ERROR: invalid initcall name $symbol in $file($path)"
139 if (!defined($function) || !defined($level));
141 $initcalls->{$counter} = {
144 'function' => $function,
150 write_results($index, $initcalls);
153 ## waits for any child process to complete, reads the results, and adds them to
154 ## the $results array for later processing
155 sub wait_for_results {
160 # unblock children that may have a full write buffer
161 foreach my $fh ($select->can_read(0)) {
165 # check for children that have exited, read the remaining data
166 # from them, and clean up
167 $pid = waitpid(-1, WNOHANG);
169 if (!exists($jobs->{$pid})) {
173 my $fh = $jobs->{$pid};
174 $select->remove($fh);
176 while (read_results($fh)) {
181 delete($jobs->{$pid});
186 ## forks a child to process each file passed in the command line and collects
190 my $njobs = $ENV{'PARALLELISM'} || get_online_processors();
191 my $select = IO::Select->new();
193 while (my $file = shift(@ARGV)) {
194 # fork a child process and read it's stdout
195 my $pid = open(my $fh, '-|');
197 if (!defined($pid)) {
198 die "$0: ERROR: failed to fork: $!";
200 # save the child process pid and the file handle
204 # in the child process
205 STDOUT->autoflush(1);
206 find_initcalls($index, "$objtree/$file");
212 # limit the number of children to $njobs
213 if (scalar(keys(%{$jobs})) >= $njobs) {
214 wait_for_results($select);
218 # wait for the remaining children to complete
219 while (scalar(keys(%{$jobs})) > 0) {
220 wait_for_results($select);
224 sub generate_initcall_lds() {
227 my $sections = {}; # level -> [ secname, ...]
229 # sort results to retain link order and split to sections per
231 foreach my $index (sort { $a <=> $b } keys(%{$results})) {
232 foreach my $result (@{$results->{$index}}) {
233 my $level = $result->{'level'};
235 if (!exists($sections->{$level})) {
236 $sections->{$level} = [];
239 push(@{$sections->{$level}}, $result->{'secname'});
243 die "$0: ERROR: no initcalls?" if (!keys(%{$sections}));
245 # print out a linker script that defines the order of initcalls for
247 print "SECTIONS {\n";
249 foreach my $level (sort(keys(%{$sections}))) {
252 if ($level eq 'con') {
253 $section = '.con_initcall.init';
255 $section = ".initcall${level}.init";
258 print "\t${section} : {\n";
260 foreach my $secname (@{$sections->{$level}}) {
261 print "\t\t*(${section}..${secname}) ;\n";
270 generate_initcall_lds();