2 # Copyright 2018 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 my $name = undef; # internal library/module name
25 my $ordinals_file = undef; # the ordinals file to use
26 my $version = undef; # the version to use for the library
27 my $OS = undef; # the operating system family
31 # For VMS, some modules may have case insensitive names
32 my $case_insensitive = 0;
34 GetOptions('name=s' => \$name,
35 'ordinals=s' => \$ordinals_file,
36 'version=s' => \$version,
39 'verbose' => \$verbose,
41 'case-insensitive' => \$case_insensitive)
42 or die "Error in command line arguments\n";
44 die "Please supply arguments\n"
45 unless $name && $ordinals_file && $OS;
47 # When building a "variant" shared library, with a custom SONAME, also customize
48 # all the symbol versions. This produces a shared object that can coexist
49 # without conflict in the same address space as a default build, or an object
50 # with a different variant tag.
52 # For example, with a target definition that includes:
54 # shlib_variant => "-opt",
56 # we build the following objects:
60 # if ($l = readlink) {
61 # printf "%s -> %s\n", $_, $l
66 # libcrypto-opt.so.1.1
67 # libcrypto.so -> libcrypto-opt.so.1.1
69 # libssl.so -> libssl-opt.so.1.1
71 # whose SONAMEs and dependencies are:
75 # readelf -d $l | egrep 'SONAME|NEEDED.*(ssl|crypto)'
78 # 0x000000000000000e (SONAME) Library soname: [libcrypto-opt.so.1.1]
80 # 0x0000000000000001 (NEEDED) Shared library: [libcrypto-opt.so.1.1]
81 # 0x000000000000000e (SONAME) Library soname: [libssl-opt.so.1.1]
83 # We case-fold the variant tag to upper case and replace all non-alnum
84 # characters with "_". This yields the following symbol versions:
86 # $ nm libcrypto.so | grep -w A
87 # 0000000000000000 A OPENSSL_OPT_1_1_0
88 # 0000000000000000 A OPENSSL_OPT_1_1_0a
89 # 0000000000000000 A OPENSSL_OPT_1_1_0c
90 # 0000000000000000 A OPENSSL_OPT_1_1_0d
91 # 0000000000000000 A OPENSSL_OPT_1_1_0f
92 # 0000000000000000 A OPENSSL_OPT_1_1_0g
93 # $ nm libssl.so | grep -w A
94 # 0000000000000000 A OPENSSL_OPT_1_1_0
95 # 0000000000000000 A OPENSSL_OPT_1_1_0d
97 (my $SO_VARIANT = uc($target{"shlib_variant"} // '')) =~ s/\W/_/g;
100 $apiv = sprintf "%x%02x%02x", split(/\./, $config{api})
103 my $libname = $unified_info{sharednames}->{$name} // $name;
106 solaris => { writer => \&writer_linux,
107 sort => sorter_linux(),
108 platforms => { UNIX => 1,
109 EXPORT_VAR_AS_FUNCTION => 0 } },
110 linux => 'solaris', # alias
111 "bsd-gcc" => 'solaris', # alias
112 aix => { writer => \&writer_aix,
113 sort => sorter_unix(),
114 platforms => { UNIX => 1,
115 EXPORT_VAR_AS_FUNCTION => 0 } },
116 VMS => { writer => \&writer_VMS,
117 sort => OpenSSL::Ordinals::by_number(),
118 platforms => { VMS => 1,
119 EXPORT_VAR_AS_FUNCTION => 0 } },
120 vms => 'VMS', # alias
121 WINDOWS => { writer => \&writer_windows,
122 sort => OpenSSL::Ordinals::by_name(),
123 platforms => { WIN32 => 1,
125 EXPORT_VAR_AS_FUNCTION => 1 } },
126 windows => 'WINDOWS', # alias
127 WIN32 => 'WINDOWS', # alias
128 win32 => 'WIN32', # alias
129 32 => 'WIN32', # alias
130 NT => 'WIN32', # alias
131 nt => 'WIN32', # alias
132 mingw => 'WINDOWS', # alias
136 die "Unknown operating system family $OS\n"
137 unless exists $OS_data{$OS};
139 } while(ref($OS) eq '');
141 my %disabled_uc = map { my $x = uc $_; $x =~ s|-|_|g; $x => 1 } keys %disabled;
143 my %ordinal_opts = ();
144 $ordinal_opts{sort} = $OS->{sort} if $OS->{sort};
145 $ordinal_opts{filter} =
150 && platform_filter($item)
151 && feature_filter($item);
153 my $ordinals = OpenSSL::Ordinals->new(from => $ordinals_file);
155 my $writer = $OS->{writer};
156 $writer = \&writer_ctest if $ctest;
158 $writer->($ordinals->items(%ordinal_opts));
162 sub platform_filter {
164 my %platforms = ( $item->platforms() );
166 # True if no platforms are defined
167 return 1 if scalar keys %platforms == 0;
169 # For any item platform tag, return the equivalence with the
170 # current platform settings if it exists there, return 0 otherwise
171 # if the item platform tag is true
172 for (keys %platforms) {
173 if (exists $OS->{platforms}->{$_}) {
174 return $platforms{$_} == $OS->{platforms}->{$_};
176 if ($platforms{$_}) {
181 # Found no match? Then it's a go
187 my @features = ( $item->features() );
189 # True if no features are defined
190 return 1 if scalar @features == 0;
192 my $verdict = ! grep { $disabled_uc{$_} } @features;
195 foreach (@features) {
196 next unless /^DEPRECATEDIN_(\d+)(?:_(\d+)_(\d+))?$/;
197 my $symdep = sprintf "%x%02x%02x", $1, ($2 // 0), ($3 // 0);
198 $verdict = 0 if $apiv ge $symdep;
206 my $by_name = OpenSSL::Ordinals::by_name();
216 my $verdict = $weight{$item1->type()} <=> $weight{$item2->type()};
218 $verdict = $by_name->($item1, $item2);
225 my $by_version = OpenSSL::Ordinals::by_version();
226 my $by_unix = sorter_unix();
232 my $verdict = $by_version->($item1, $item2);
234 $verdict = $by_unix->($item1, $item2);
241 my $thisversion = '';
242 my $currversion_s = '';
243 my $prevversion_s = '';
247 if ($thisversion && $_->version() ne $thisversion) {
248 die "$ordinals_file: It doesn't make sense to have both versioned ",
249 "and unversioned symbols"
250 if $thisversion eq '*';
254 $prevversion_s = " OPENSSL${SO_VARIANT}_$thisversion";
255 $thisversion = ''; # Trigger start of next section
257 unless ($thisversion) {
259 $thisversion = $_->version();
261 $currversion_s = "OPENSSL${SO_VARIANT}_$thisversion "
262 if $thisversion ne '*';
268 print ' ', $_->name(), ";\n";
279 print $_->name(),"\n";
286 ; Definition file for the DLL version of the $libname library from OpenSSL
294 print " ",$_->name(),"\n";
298 sub collect_VMS_mixedcase {
299 return [ 'SPARE', 'SPARE' ] unless @_;
305 return [ "$s=$type", 'SPARE' ] if $s_uc eq $s;
306 return [ "$s_uc/$s=$type", "$s=$type" ];
309 sub collect_VMS_uppercase {
310 return [ 'SPARE' ] unless @_;
316 return [ "$s_uc=$type" ];
320 my @slot_collection = ();
322 $case_insensitive ? \&collect_VMS_uppercase : \&collect_VMS_mixedcase;
326 while (++$last_num < $_->number()) {
327 push @slot_collection, $collector->(); # Just occupy a slot
330 FUNCTION => 'PROCEDURE',
333 push @slot_collection, $collector->($_->name(), $type);
336 print <<"_____" if defined $version;
337 IDENTIFICATION=$version
339 print <<"_____" unless $case_insensitive;
345 # It's uncertain how long aggregated lines the linker can handle,
346 # but it has been observed that at least 1024 characters is ok.
347 # Either way, this means that we need to keep track of the total
348 # line length of each "SYMBOL_VECTOR" statement. Fortunately, we
349 # can have more than one of those...
350 my $symvtextcount = 16; # The length of "SYMBOL_VECTOR=("
351 while (@slot_collection) {
352 my $set = shift @slot_collection;
353 my $settextlength = 0;
356 + 3 # two space indentation and comma
361 $settextlength--; # only one space indentation on the first one
362 my $firstcomma = ',';
364 if ($symvtextcount + $settextlength > 1024) {
369 $symvtextcount = 16; # The length of "SYMBOL_VECTOR=("
371 if ($symvtextcount == 16) {
375 my $indent = ' '.$firstcomma;
380 $symvtextcount += length($indent) + length($_) + 1;
388 if (defined $version) {
389 $version =~ /^(\d+)\.(\d+)\.(\d+)/;
391 my $libvminor = $2 * 100 + $3;
393 GSMATCH=LEQUAL,$libvmajor,$libvminor
401 * Test file to check all DEF file symbols are present by trying
402 * to link to all of them. This is *not* intended to be run!
410 if ($_->type() eq 'VARIABLE') {
411 print "\textern int ", $_->name(), '; /* type unknown */ /* ', $_->number(), ' ', $_->version(), " */\n";
413 print "\textern int ", $_->name(), '(); /* type unknown */ /* ', $_->number(), ' ', $_->version(), " */\n";