2 # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (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 $OS = undef; # the operating system family
30 GetOptions('name=s' => \$name,
31 'ordinals=s' => \$ordinals_file,
34 'verbose' => \$verbose)
35 or die "Error in command line arguments\n";
37 die "Please supply arguments\n"
38 unless $name && $ordinals_file && $OS;
40 # When building a "variant" shared library, with a custom SONAME, also customize
41 # all the symbol versions. This produces a shared object that can coexist
42 # without conflict in the same address space as a default build, or an object
43 # with a different variant tag.
45 # For example, with a target definition that includes:
47 # shlib_variant => "-opt",
49 # we build the following objects:
53 # if ($l = readlink) {
54 # printf "%s -> %s\n", $_, $l
59 # libcrypto-opt.so.1.1
60 # libcrypto.so -> libcrypto-opt.so.1.1
62 # libssl.so -> libssl-opt.so.1.1
64 # whose SONAMEs and dependencies are:
68 # readelf -d $l | egrep 'SONAME|NEEDED.*(ssl|crypto)'
71 # 0x000000000000000e (SONAME) Library soname: [libcrypto-opt.so.1.1]
73 # 0x0000000000000001 (NEEDED) Shared library: [libcrypto-opt.so.1.1]
74 # 0x000000000000000e (SONAME) Library soname: [libssl-opt.so.1.1]
76 # We case-fold the variant tag to upper case and replace all non-alnum
77 # characters with "_". This yields the following symbol versions:
79 # $ nm libcrypto.so | grep -w A
80 # 0000000000000000 A OPENSSL_OPT_1_1_0
81 # 0000000000000000 A OPENSSL_OPT_1_1_0a
82 # 0000000000000000 A OPENSSL_OPT_1_1_0c
83 # 0000000000000000 A OPENSSL_OPT_1_1_0d
84 # 0000000000000000 A OPENSSL_OPT_1_1_0f
85 # 0000000000000000 A OPENSSL_OPT_1_1_0g
86 # $ nm libssl.so | grep -w A
87 # 0000000000000000 A OPENSSL_OPT_1_1_0
88 # 0000000000000000 A OPENSSL_OPT_1_1_0d
90 (my $SO_VARIANT = uc($target{"shlib_variant"} // '')) =~ s/\W/_/g;
93 $apiv = sprintf "%x%02x%02x", split(/\./, $config{api})
96 my $libname = $unified_info{sharednames}->{$name} // $name;
99 solaris => { writer => \&writer_linux,
100 sort => sorter_linux(),
101 platforms => { UNIX => 1,
102 EXPORT_VAR_AS_FUNCTION => 0 } },
103 linux => 'solaris', # alias
104 aix => { writer => \&writer_aix,
105 sort => sorter_unix(),
106 platforms => { UNIX => 1,
107 EXPORT_VAR_AS_FUNCTION => 0 } },
108 VMS => { writer => \&writer_VMS,
109 sort => OpenSSL::Ordinals::by_number(),
110 platforms => { VMS => 1,
111 EXPORT_VAR_AS_FUNCTION => 0 } },
112 vms => 'VMS', # alias
113 WINDOWS => { writer => \&writer_windows,
114 sort => OpenSSL::Ordinals::by_name(),
115 platforms => { WIN32 => 1,
117 EXPORT_VAR_AS_FUNCTION => 1 } },
118 windows => 'WINDOWS', # alias
119 WIN32 => 'WINDOWS', # alias
120 win32 => 'WIN32', # alias
121 32 => 'WIN32', # alias
122 NT => 'WIN32', # alias
123 nt => 'WIN32', # alias
124 mingw => 'WINDOWS', # alias
128 die "Unknown operating system family $OS\n"
129 unless exists $OS_data{$OS};
131 } while(ref($OS) eq '');
133 my %disabled_uc = map { my $x = uc $_; $x =~ s|-|_|g; $x => 1 } keys %disabled;
135 my %ordinal_opts = ();
136 $ordinal_opts{sort} = $OS->{sort} if $OS->{sort};
137 $ordinal_opts{filter} =
142 && platform_filter($item)
143 && feature_filter($item);
145 my $ordinals = OpenSSL::Ordinals->new(from => $ordinals_file);
147 my $writer = $OS->{writer};
148 $writer = \&writer_ctest if $ctest;
150 $writer->($ordinals->items(%ordinal_opts));
154 sub platform_filter {
156 my %platforms = ( $item->platforms() );
158 # True if no platforms are defined
159 return 1 if scalar keys %platforms == 0;
161 # For any item platform tag, return the equivalence with the
162 # current platform settings if it exists there, return 0 otherwise
163 # if the item platform tag is true
164 for (keys %platforms) {
165 if (exists $OS->{platforms}->{$_}) {
166 return $platforms{$_} == $OS->{platforms}->{$_};
168 if ($platforms{$_}) {
173 # Found no match? Then it's a go
179 my %platforms = ( $item->platforms() );
180 my @features = ( $item->features() );
182 # True if no features are defined
183 return 1 if scalar @features == 0 && scalar keys %platforms == 0;
186 !( ( grep { $disabled_uc{$_} } @features )
187 || ( grep { $_ eq 'ZLIB' && $disabled_uc{$_} } keys %platforms ) );
190 foreach (@features) {
191 next unless /^DEPRECATEDIN_(\d+)_(\d+)_(\d+)$/;
192 my $symdep = sprintf "%x%02x%02x", $1, $2, $3;
193 $verdict = 0 if $apiv ge $symdep;
201 my $by_name = OpenSSL::Ordinals::by_name();
211 my $verdict = $weight{$item1->type()} <=> $weight{$item2->type()};
213 $verdict = $by_name->($item1, $item2);
220 my $by_version = OpenSSL::Ordinals::by_version();
221 my $by_unix = sorter_unix();
227 my $verdict = $by_version->($item1, $item2);
229 $verdict = $by_unix->($item1, $item2);
236 my $thisversion = '';
237 my $prevversion = '';
240 if ($thisversion && $_->version() ne $thisversion) {
244 $prevversion = " OPENSSL${SO_VARIANT}_$thisversion";
245 $thisversion = ''; # Trigger start of next section
247 unless ($thisversion) {
248 $thisversion = $_->version();
250 OPENSSL${SO_VARIANT}_$thisversion {
254 print ' ', $_->name(), ";\n";
265 print $_->name(),"\n";
272 ; Definition file for the DLL version of the $libname library from OpenSSL
280 print " ",$_->name(),"\n";
285 my @slot_collection = ();
286 my $write_vector_slot_pair =
290 my $slotpair_text = " $slot1, -\n $slot2, -\n"
295 while (++$last_num < $_->number()) {
296 push @slot_collection, [ 'SPARE', 'SPARE' ];
299 FUNCTION => 'PROCEDURE',
305 push @slot_collection, [ "$s=$type", 'SPARE' ];
307 push @slot_collection, [ "$s_uc/$s=$type", "$s=$type" ];
312 IDENTIFICATION=$config{version}
316 # It's uncertain how long aggregated lines the linker can handle,
317 # but it has been observed that at least 1024 characters is ok.
318 # Either way, this means that we need to keep track of the total
319 # line length of each "SYMBOL_VECTOR" statement. Fortunately, we
320 # can have more than one of those...
321 my $symvtextcount = 16; # The length of "SYMBOL_VECTOR=("
322 while (@slot_collection) {
323 my $pair = shift @slot_collection;
325 2 # one space indentation and comma
328 + 3 # two space indentation and comma
332 my $firstcomma = ',';
334 if ($symvtextcount + $pairtextlength > 1024) {
339 $symvtextcount = 16; # The length of "SYMBOL_VECTOR=("
341 if ($symvtextcount == 16) {
345 $firstcomma$pair->[0] -
348 $symvtextcount += $pairtextlength;
354 my ($libvmajor, $libvminor, $libvedit, $libvpatch) =
355 $config{version} =~ /^(\d+)_(\d+)_(\d+)([a-z]{0,2})-.*$/;
356 my $libvpatchnum = 0;
357 for (split '', $libvpatch // '') {
358 $libvpatchnum += ord(lc($_)) - 96;
359 # To compensate because the letter 'z' is always followed by another,
360 # i.e. doesn't add any value on its own
361 $libvpatchnum-- if lc($_) eq 'z';
363 my $match1 = $libvmajor * 100 + $libvminor;
364 my $match2 = $libvedit * 100 + $libvpatchnum;
366 GSMATCH=LEQUAL,$match1,$match2
373 * Test file to check all DEF file symbols are present by trying
374 * to link to all of them. This is *not* intended to be run!
382 if ($_->type() eq 'VARIABLE') {
383 print "\textern int ", $_->name(), '; /* type unknown */ /* ', $_->number(), ' ', $_->version(), " */\n";
385 print "\textern int ", $_->name(), '(); /* type unknown */ /* ', $_->number(), ' ', $_->version(), " */\n";