2 # Copyright 2018-2024 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
9 # Generate a linker version script suitable for the given platform
10 # from a given ordinals file.
17 use lib "$FindBin::Bin/perl";
19 use OpenSSL::Ordinals;
24 use File::Spec::Functions;
25 use lib catdir($config{sourcedir}, 'Configurations');
28 my $name = undef; # internal library/module name
29 my $ordinals_file = undef; # the ordinals file to use
30 my $version = undef; # the version to use for the library
31 my $OS = undef; # the operating system family
32 my $type = 'lib'; # either lib or dso
37 # For VMS, some modules may have case insensitive names
38 my $case_insensitive = 0;
40 GetOptions('name=s' => \$name,
41 'ordinals=s' => \$ordinals_file,
42 'version=s' => \$version,
46 'verbose' => \$verbose,
48 'case-insensitive' => \$case_insensitive)
49 or die "Error in command line arguments\n";
51 die "Please supply arguments\n"
52 unless $name && $ordinals_file && $OS;
53 die "--type argument must be equal to 'lib' or 'dso'"
54 if $type ne 'lib' && $type ne 'dso';
56 # When building a "variant" shared library, with a custom SONAME, also customize
57 # all the symbol versions. This produces a shared object that can coexist
58 # without conflict in the same address space as a default build, or an object
59 # with a different variant tag.
61 # For example, with a target definition that includes:
63 # shlib_variant => "-opt",
65 # we build the following objects:
69 # if ($l = readlink) {
70 # printf "%s -> %s\n", $_, $l
75 # libcrypto-opt.so.1.1
76 # libcrypto.so -> libcrypto-opt.so.1.1
78 # libssl.so -> libssl-opt.so.1.1
80 # whose SONAMEs and dependencies are:
84 # readelf -d $l | egrep 'SONAME|NEEDED.*(ssl|crypto)'
87 # 0x000000000000000e (SONAME) Library soname: [libcrypto-opt.so.1.1]
89 # 0x0000000000000001 (NEEDED) Shared library: [libcrypto-opt.so.1.1]
90 # 0x000000000000000e (SONAME) Library soname: [libssl-opt.so.1.1]
92 # We case-fold the variant tag to uppercase and replace all non-alnum
93 # characters with "_". This yields the following symbol versions:
95 # $ nm libcrypto.so | grep -w A
96 # 0000000000000000 A OPENSSL_OPT_1_1_0
97 # 0000000000000000 A OPENSSL_OPT_1_1_0a
98 # 0000000000000000 A OPENSSL_OPT_1_1_0c
99 # 0000000000000000 A OPENSSL_OPT_1_1_0d
100 # 0000000000000000 A OPENSSL_OPT_1_1_0f
101 # 0000000000000000 A OPENSSL_OPT_1_1_0g
102 # $ nm libssl.so | grep -w A
103 # 0000000000000000 A OPENSSL_OPT_1_1_0
104 # 0000000000000000 A OPENSSL_OPT_1_1_0d
106 (my $SO_VARIANT = uc($target{"shlib_variant"} // '')) =~ s/\W/_/g;
108 my $libname = $type eq 'lib' ? platform->sharedname($name) : platform->dsoname($name);
111 solaris => { writer => \&writer_linux,
112 sort => sorter_linux(),
113 platforms => { UNIX => 1 } },
114 "solaris-gcc" => 'solaris', # alias
115 linux => 'solaris', # alias
116 "bsd-gcc" => 'solaris', # alias
117 aix => { writer => \&writer_aix,
118 sort => sorter_unix(),
119 platforms => { UNIX => 1 } },
120 "aix-solib" => 'aix', # alias
121 VMS => { writer => \&writer_VMS,
122 sort => OpenSSL::Ordinals::by_number(),
123 platforms => { VMS => 1 } },
124 vms => 'VMS', # alias
125 WINDOWS => { writer => \&writer_windows,
126 sort => OpenSSL::Ordinals::by_name(),
127 platforms => { WIN32 => 1,
129 windows => 'WINDOWS', # alias
130 WIN32 => 'WINDOWS', # alias
131 win32 => 'WIN32', # alias
132 32 => 'WIN32', # alias
133 NT => 'WIN32', # alias
134 nt => 'WIN32', # alias
135 mingw => 'WINDOWS', # alias
136 nonstop => { writer => \&writer_nonstop,
137 sort => OpenSSL::Ordinals::by_name(),
138 platforms => { TANDEM => 1 } },
142 die "Unknown operating system family $OS\n"
143 unless exists $OS_data{$OS};
145 } while(ref($OS) eq '');
147 my %disabled_uc = map { my $x = uc $_; $x =~ s|-|_|g; $x => 1 } keys %disabled;
149 my %ordinal_opts = ();
150 $ordinal_opts{sort} = $OS->{sort} if $OS->{sort};
151 $ordinal_opts{filter} =
156 && platform_filter($item)
157 && feature_filter($item);
159 my $ordinals = OpenSSL::Ordinals->new(from => $ordinals_file);
161 my $writer = $OS->{writer};
162 $writer = \&writer_ctest if $ctest;
164 $writer->($ordinals->items(%ordinal_opts));
168 sub platform_filter {
170 my %platforms = ( $item->platforms() );
172 # True if no platforms are defined
173 return 1 if scalar keys %platforms == 0;
175 # For any item platform tag, return the equivalence with the
176 # current platform settings if it exists there, return 0 otherwise
177 # if the item platform tag is true
178 for (keys %platforms) {
179 if (exists $OS->{platforms}->{$_}) {
180 return $platforms{$_} == $OS->{platforms}->{$_};
182 if ($platforms{$_}) {
187 # Found no match? Then it's a go
193 my @features = ( $item->features() );
195 # True if no features are defined
196 return 1 if scalar @features == 0;
198 my $verdict = ! grep { $disabled_uc{$_} } @features;
200 if ($disabled{deprecated}) {
201 foreach (@features) {
202 next unless /^DEPRECATEDIN_(\d+)_(\d+)(?:_(\d+))?$/;
203 my $symdep = $1 * 10000 + $2 * 100 + ($3 // 0);
204 $verdict = 0 if $config{api} >= $symdep;
205 print STDERR "DEBUG: \$symdep = $symdep, \$verdict = $verdict\n"
206 if $debug && $1 == 0;
214 my $by_name = OpenSSL::Ordinals::by_name();
224 my $verdict = $weight{$item1->type()} <=> $weight{$item2->type()};
226 $verdict = $by_name->($item1, $item2);
233 my $by_version = OpenSSL::Ordinals::by_version();
234 my $by_unix = sorter_unix();
240 my $verdict = $by_version->($item1, $item2);
242 $verdict = $by_unix->($item1, $item2);
249 my $thisversion = '';
250 my $currversion_s = '';
251 my $prevversion_s = '';
255 if ($thisversion && $_->version() ne $thisversion) {
256 die "$ordinals_file: It doesn't make sense to have both versioned ",
257 "and unversioned symbols"
258 if $thisversion eq '*';
262 $prevversion_s = " OPENSSL${SO_VARIANT}_$thisversion";
263 $thisversion = ''; # Trigger start of next section
265 unless ($thisversion) {
267 $thisversion = $_->version();
269 $currversion_s = "OPENSSL${SO_VARIANT}_$thisversion "
270 if $thisversion ne '*';
276 print ' ', $_->name(), ";\n";
287 print $_->name(),"\n";
293 print "-export ",$_->name(),"\n";
300 ; Definition file for the DLL version of the $libname library from OpenSSL
308 print " ",$_->name();
309 if (platform->can('export2internal')) {
310 print "=". platform->export2internal($_->name());
316 sub collect_VMS_mixedcase {
317 return [ 'SPARE', 'SPARE' ] unless @_;
323 return [ "$s=$type", 'SPARE' ] if $s_uc eq $s;
324 return [ "$s_uc/$s=$type", "$s=$type" ];
327 sub collect_VMS_uppercase {
328 return [ 'SPARE' ] unless @_;
334 return [ "$s_uc=$type" ];
338 my @slot_collection = ();
340 $case_insensitive ? \&collect_VMS_uppercase : \&collect_VMS_mixedcase;
344 my $this_num = $_->number();
345 $this_num = $last_num + 1 if $this_num =~ m|^\?|;
347 while (++$last_num < $this_num) {
348 push @slot_collection, $collector->(); # Just occupy a slot
351 FUNCTION => 'PROCEDURE',
354 push @slot_collection, $collector->($_->name(), $type);
357 print <<"_____" if defined $version;
358 IDENTIFICATION=$version
360 print <<"_____" unless $case_insensitive;
366 # It's uncertain how long aggregated lines the linker can handle,
367 # but it has been observed that at least 1024 characters is ok.
368 # Either way, this means that we need to keep track of the total
369 # line length of each "SYMBOL_VECTOR" statement. Fortunately, we
370 # can have more than one of those...
371 my $symvtextcount = 16; # The length of "SYMBOL_VECTOR=("
372 while (@slot_collection) {
373 my $set = shift @slot_collection;
374 my $settextlength = 0;
377 + 3 # two space indentation and comma
382 $settextlength--; # only one space indentation on the first one
383 my $firstcomma = ',';
385 if ($symvtextcount + $settextlength > 1024) {
390 $symvtextcount = 16; # The length of "SYMBOL_VECTOR=("
392 if ($symvtextcount == 16) {
396 my $indent = ' '.$firstcomma;
401 $symvtextcount += length($indent) + length($_) + 1;
409 if (defined $version) {
410 $version =~ /^(\d+)\.(\d+)\.(\d+)/;
412 my $libvminor = $2 * 100 + $3;
414 GSMATCH=LEQUAL,$libvmajor,$libvminor
422 * Test file to check all DEF file symbols are present by trying
423 * to link to all of them. This is *not* intended to be run!
432 my $this_num = $_->number();
433 $this_num = $last_num + 1 if $this_num =~ m|^\?|;
435 if ($_->type() eq 'VARIABLE') {
436 print "\textern int ", $_->name(), '; /* type unknown */ /* ',
437 $this_num, ' ', $_->version(), " */\n";
439 print "\textern int ", $_->name(), '(); /* type unknown */ /* ',
440 $this_num, ' ', $_->version(), " */\n";
443 $last_num = $this_num;