200_Dpkg_Shlibs.t 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # -*- mode: cperl;-*-
  2. use Test::More tests => 62;
  3. use IO::String;
  4. use strict;
  5. use warnings;
  6. use_ok('Dpkg::Shlibs');
  7. my @tmp;
  8. my @save_paths = @Dpkg::Shlibs::librarypaths;
  9. @Dpkg::Shlibs::librarypaths = ();
  10. my $srcdir = $ENV{srcdir} || '.';
  11. $srcdir .= '/t/200_Dpkg_Shlibs';
  12. Dpkg::Shlibs::parse_ldso_conf("t.tmp/ld.so.conf");
  13. use Data::Dumper;
  14. is_deeply([qw(/nonexistant32 /nonexistant/lib64
  15. /usr/local/lib /nonexistant/lib128 )],
  16. \@Dpkg::Shlibs::librarypaths, "parsed library paths");
  17. use_ok('Dpkg::Shlibs::Objdump');
  18. my $obj = Dpkg::Shlibs::Objdump::Object->new;
  19. open my $objdump, '<', "$srcdir/objdump.dbd-pg"
  20. or die "$srcdir/objdump.dbd-pg: $!";
  21. $obj->_parse($objdump);
  22. close $objdump;
  23. ok(!$obj->is_public_library(), 'Pg.so is not a public library');
  24. ok(!$obj->is_executable(), 'Pg.so is not an executable');
  25. open $objdump, '<', "$srcdir/objdump.ls"
  26. or die "$srcdir/objdump.ls: $!";
  27. $obj->reset();
  28. $obj->_parse($objdump);
  29. close $objdump;
  30. ok(!$obj->is_public_library(), 'ls is not a public library');
  31. ok($obj->is_executable(), 'ls is an executable');
  32. my $sym = $obj->get_symbol('optarg@GLIBC_2.0');
  33. ok($sym, 'optarg@GLIBC_2.0 exists');
  34. ok(!$sym->{'defined'}, 'R_*_COPY relocations are taken into account');
  35. # Non-regression test for #506139
  36. $sym = $obj->get_symbol('singlespace');
  37. ok($sym, 'version less symbol separated by a single space are correctly parsed');
  38. open $objdump, '<', "$srcdir/objdump.libc6-2.6"
  39. or die "$srcdir/objdump.libc6-2.6: $!";
  40. $obj->reset();
  41. $obj->_parse($objdump);
  42. close $objdump;
  43. ok($obj->is_public_library(), 'libc6 is a public library');
  44. ok(!$obj->is_executable(), 'libc6 is not an executable');
  45. is($obj->{SONAME}, 'libc.so.6', 'SONAME');
  46. is($obj->{HASH}, '0x13d99c', 'HASH');
  47. is($obj->{GNU_HASH}, '0x194', 'GNU_HASH');
  48. is($obj->{format}, 'elf32-i386', 'format');
  49. is_deeply($obj->{flags}, { DYNAMIC => 1, HAS_SYMS => 1, D_PAGED => 1 }, 'flags');
  50. is_deeply($obj->{NEEDED}, [ 'ld-linux.so.2' ], 'NEEDED');
  51. is_deeply([ $obj->get_needed_libraries ], [ 'ld-linux.so.2' ], 'NEEDED');
  52. $sym = $obj->get_symbol('_sys_nerr@GLIBC_2.3');
  53. is_deeply( $sym, { name => '_sys_nerr', version => 'GLIBC_2.3',
  54. soname => 'libc.so.6', objid => 'libc.so.6',
  55. section => '.rodata', dynamic => 1,
  56. debug => '', type => 'O', weak => '',
  57. local => '', global => 1, visibility => '',
  58. hidden => 1, defined => 1 }, 'Symbol' );
  59. $sym = $obj->get_symbol('_IO_stdin_used');
  60. is_deeply( $sym, { name => '_IO_stdin_used', version => '',
  61. soname => 'libc.so.6', objid => 'libc.so.6',
  62. section => '*UND*', dynamic => 1,
  63. debug => '', type => ' ', weak => 1,
  64. local => '', global => '', visibility => '',
  65. hidden => '', defined => '' }, 'Symbol 2' );
  66. my @syms = $obj->get_exported_dynamic_symbols;
  67. is( scalar @syms, 2231, 'defined && dynamic' );
  68. @syms = $obj->get_undefined_dynamic_symbols;
  69. is( scalar @syms, 9, 'undefined && dynamic' );
  70. my $obj_old = Dpkg::Shlibs::Objdump::Object->new;
  71. open $objdump, '<', "$srcdir/objdump.libc6-2.3"
  72. or die "$srcdir/objdump.libc6-2.3: $!";
  73. $obj_old->_parse($objdump);
  74. close $objdump;
  75. use_ok('Dpkg::Shlibs::SymbolFile');
  76. use_ok('Dpkg::Shlibs::Symbol');
  77. my $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$srcdir/symbol_file.tmp");
  78. my $sym_file_dup = Dpkg::Shlibs::SymbolFile->new(file => "$srcdir/symbol_file.tmp");
  79. my $sym_file_old = Dpkg::Shlibs::SymbolFile->new(file => "$srcdir/symbol_file.tmp");
  80. $sym_file->merge_symbols($obj_old, "2.3.6.ds1-13");
  81. $sym_file_old->merge_symbols($obj_old, "2.3.6.ds1-13");
  82. ok( $sym_file->has_object('libc.so.6'), 'SONAME in sym file' );
  83. $sym_file->merge_symbols($obj, "2.6-1");
  84. ok( $sym_file->get_new_symbols($sym_file_old), 'has new symbols' );
  85. ok( $sym_file_old->get_lost_symbols($sym_file), 'has lost symbols' );
  86. is( $sym_file_old->lookup_symbol('__bss_start@Base', ['libc.so.6']),
  87. undef, 'internal symbols are blacklisted');
  88. $sym = $sym_file->lookup_symbol('_errno@GLIBC_2.0', ['libc.so.6'], 1);
  89. isa_ok($sym, 'Dpkg::Shlibs::Symbol');
  90. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => '_errno@GLIBC_2.0',
  91. 'minver' => '2.3.6.ds1-13', 'dep_id' => 0,
  92. 'deprecated' => '2.6-1', 'depends' => '',
  93. 'soname' => 'libc.so.6' ), 'deprecated symbol');
  94. # Wildcard test
  95. $sym = Dpkg::Shlibs::Symbol->new(symbol => '*@GLIBC_PRIVATE', minver => '2.3.6.wildcard');
  96. $sym_file_old->add_symbol('libc.so.6', $sym);
  97. $sym_file_old->merge_symbols($obj, "2.6-1");
  98. $sym = $sym_file_old->lookup_symbol('__nss_services_lookup@GLIBC_PRIVATE', ['libc.so.6']);
  99. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => '__nss_services_lookup@GLIBC_PRIVATE',
  100. 'minver' => '2.3.6.wildcard', 'dep_id' => 0,
  101. 'deprecated' => 0, 'depends' => '',
  102. 'soname' => 'libc.so.6' ), 'wildcarded symbol');
  103. # Save -> Load test
  104. use File::Temp;
  105. my $save_file = new File::Temp;
  106. $sym_file->save($save_file->filename);
  107. $sym_file_dup->load($save_file->filename);
  108. $sym_file_dup->{file} = "$srcdir/symbol_file.tmp";
  109. is_deeply($sym_file_dup, $sym_file, 'save -> load' );
  110. # Test include mechanism of SymbolFile
  111. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$srcdir/symbols.include-1");
  112. $sym = $sym_file->lookup_symbol('symbol_before@Base', ['libfake.so.1']);
  113. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol_before@Base',
  114. 'minver' => '0.9', 'dep_id' => 0, 'deprecated' => 0,
  115. 'depends' => 'libfake1 #MINVER#', 'soname' => 'libfake.so.1' ),
  116. 'symbol before include not lost');
  117. $sym = $sym_file->lookup_symbol('symbol_after@Base', ['libfake.so.1']);
  118. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol_after@Base',
  119. 'minver' => '1.1', 'dep_id' => 0, 'deprecated' => 0,
  120. 'depends' => 'libfake1 #MINVER#', 'soname' => 'libfake.so.1' ),
  121. 'symbol after include not lost');
  122. $sym = $sym_file->lookup_symbol('symbol1_fake1@Base', ['libfake.so.1']);
  123. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol1_fake1@Base',
  124. 'minver' => '1.0', 'dep_id' => 0, 'deprecated' => 0,
  125. 'depends' => 'libfake1 #MINVER#', 'soname' => 'libfake.so.1' ),
  126. 'overrides order with #include');
  127. $sym = $sym_file->lookup_symbol('symbol3_fake1@Base', ['libfake.so.1']);
  128. is_deeply($sym, Dpkg::Shlibs::Symbol->new ( 'symbol' => 'symbol3_fake1@Base',
  129. 'minver' => '0', 'dep_id' => 0, 'deprecated' => 0,
  130. 'depends' => 'libfake1 #MINVER#', 'soname' => 'libfake.so.1' ),
  131. 'overrides order with #include');
  132. is($sym_file->get_smallest_version('libfake.so.1'), "0",
  133. 'get_smallest_version with null version');
  134. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$srcdir/symbols.include-2");
  135. $sym = $sym_file->lookup_symbol('symbol1_fake2@Base', ['libfake.so.1']);
  136. is_deeply($sym, Dpkg::Shlibs::Symbol->new ( 'symbol' => 'symbol1_fake2@Base',
  137. 'minver' => '1.0', 'dep_id' => 1, 'deprecated' => 0,
  138. 'depends' => 'libvirtualfake', 'soname' => 'libfake.so.1' ),
  139. 'overrides order with circular #include');
  140. is($sym_file->get_smallest_version('libfake.so.1'), "1.0",
  141. 'get_smallest_version');
  142. # Check dump output
  143. my $io = IO::String->new();
  144. $sym_file->dump($io, package => "libfake1");
  145. is(${$io->string_ref()},
  146. 'libfake.so.1 libfake1 #MINVER#
  147. | libvirtualfake
  148. * Build-Depends-Package: libfake-dev
  149. symbol1_fake2@Base 1.0 1
  150. symbol2_fake2@Base 1.0
  151. symbol3_fake2@Base 1.1
  152. ', "Dump of $srcdir/symbols.include-2");
  153. # Check parsing of objdump output on ia64 (local symbols
  154. # without versions and with visibility attribute)
  155. $obj = Dpkg::Shlibs::Objdump::Object->new;
  156. open $objdump, '<', "$srcdir/objdump.glib-ia64"
  157. or die "$srcdir/objdump.glib-ia64: $!";
  158. $obj->_parse($objdump);
  159. close $objdump;
  160. $sym = $obj->get_symbol('IA__g_free');
  161. is_deeply( $sym, { name => 'IA__g_free', version => '',
  162. soname => 'libglib-2.0.so.0', objid => 'libglib-2.0.so.0',
  163. section => '.text', dynamic => 1,
  164. debug => '', type => 'F', weak => '',
  165. local => 1, global => '', visibility => 'hidden',
  166. hidden => '', defined => 1 },
  167. 'symbol with visibility without version' );
  168. ####### Test symbol tagging support ######
  169. # Parsing/dumping
  170. # Template mode
  171. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$srcdir/symbols.tags.in", arch => 'amd64');
  172. $sym_file->save($save_file->filename, template_mode => 1);
  173. $sym_file_dup = Dpkg::Shlibs::SymbolFile->new(file => $save_file, arch => 'amd64');
  174. $sym_file_dup->{file} = "$srcdir/symbols.tags.in";
  175. is_deeply($sym_file_dup, $sym_file, 'template save -> load' );
  176. is( system("diff -u '$srcdir/symbols.tags.in' '" . $save_file->filename . "' >&2"), 0, "symbols.tags.in template dumped identical" );
  177. # Dumping in non-template mode (amd64) (test for arch, subst tags)
  178. $io = IO::String->new();
  179. $sym_file->dump($io);
  180. is(${$io->string_ref()},
  181. 'libsymboltags.so.1 libsymboltags1 #MINVER#
  182. | libsymboltags1 (>= 1.1)
  183. symbol11_optional@Base 1.1 1
  184. symbol21_amd64@Base 2.1
  185. symbol31_randomtag@Base 3.1
  186. symbol51_untagged@Base 5.1
  187. ', "template vs. non-template on amd64" );
  188. # Dumping in non-template mode (i386) (test for arch, subst tags)
  189. $io = IO::String->new();
  190. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$srcdir/symbols.tags.in", arch => 'i386');
  191. $sym_file_dup = Dpkg::Shlibs::SymbolFile->new(file => "$srcdir/symbols.tags.in", arch => 'i386');
  192. $sym_file->dump($io);
  193. is(${$io->string_ref()},
  194. 'libsymboltags.so.1 libsymboltags1 #MINVER#
  195. | libsymboltags1 (>= 1.1)
  196. symbol11_optional@Base 1.1 1
  197. symbol22_i386@Base 2.2
  198. symbol31_randomtag@Base 3.1
  199. symbol41_i386_and_optional@Base 4.1
  200. symbol51_untagged@Base 5.1
  201. ', "template vs. non-template on i386" );
  202. ok (defined $sym_file->{objects}{'libsymboltags.so.1'}{syms}{'symbol21_amd64@Base'},
  203. "syms keys are symbol names without quotes");
  204. # Preload objdumps
  205. my $tags_obj_i386 = Dpkg::Shlibs::Objdump::Object->new();
  206. open $objdump, '<', "$srcdir/objdump.tags-i386" or die "$srcdir/objdump.tags-i386: $!";
  207. $tags_obj_i386->_parse($objdump);
  208. close $objdump;
  209. $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
  210. is_deeply($sym_file, $sym_file_dup, "is objdump.tags-i386 and symbols.tags.in in sync");
  211. my $tags_obj_amd64 = Dpkg::Shlibs::Objdump::Object->new();
  212. open $objdump, '<', "$srcdir/objdump.tags-amd64" or die "$srcdir/objdump.tags-amd64: $!";
  213. $tags_obj_amd64->_parse($objdump);
  214. close $objdump;
  215. # Merge/get_{new,lost} tests for optional tag:
  216. # - disappeared
  217. my $symbol11 = $tags_obj_i386->get_symbol('symbol11_optional@Base');
  218. delete $tags_obj_i386->{dynsyms}{'symbol11_optional@Base'};
  219. $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
  220. $sym = $sym_file->lookup_symbol('symbol11_optional@Base', ['libsymboltags.so.1'], 1);
  221. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol11_optional@Base', 'symbol_templ' => 'symbol11_optional@Base',
  222. 'minver' => '1.1', 'dep_id' => 1, 'deprecated' => '100.MISSING',
  223. 'depends' => 'libsymboltags1 (>= 1.1)', 'soname' => 'libsymboltags.so.1',
  224. 'tags' => { 'optional' => undef }, 'tagorder' => [ 'optional' ] ),
  225. 'disappered optional symbol gets deprecated');
  226. $sym_file->merge_symbols($tags_obj_i386, '101.MISSING');
  227. $sym = $sym_file->lookup_symbol('symbol11_optional@Base', ['libsymboltags.so.1'], 1);
  228. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol11_optional@Base', 'symbol_templ' => 'symbol11_optional@Base',
  229. 'minver' => '1.1', 'dep_id' => 1, 'deprecated' => '101.MISSING',
  230. 'depends' => 'libsymboltags1 (>= 1.1)', 'soname' => 'libsymboltags.so.1',
  231. 'tags' => { 'optional' => undef }, 'tagorder' => [ 'optional' ] ),
  232. 'deprecated text of MISSING optional symbol gets rebumped each merge');
  233. is( scalar($sym_file->get_lost_symbols($sym_file_dup)), 0, "missing optional symbol is not LOST");
  234. # - reappeared (undeprecate, minver should be 1.1, not 100.MISSED)
  235. $tags_obj_i386->add_dynamic_symbol($symbol11);
  236. $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
  237. $sym = $sym_file->lookup_symbol('symbol11_optional@Base', ['libsymboltags.so.1']);
  238. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol11_optional@Base', 'symbol_templ' => 'symbol11_optional@Base',
  239. 'minver' => '1.1', 'dep_id' => 1, 'deprecated' => 0,
  240. 'depends' => 'libsymboltags1 (>= 1.1)', 'soname' => 'libsymboltags.so.1',
  241. 'tags' => { 'optional' => undef }, 'tagorder' => [ 'optional' ] ),
  242. 'reappered optional symbol gets undeprecated + minver');
  243. is( scalar($sym_file->get_lost_symbols($sym_file_dup) +
  244. $sym_file->get_new_symbols($sym_file_dup)), 0, "reappeared optional symbol: neither NEW nor LOST");
  245. # Merge/get_{new,lost} tests for arch tag:
  246. # - arch specific appears on wrong arch: 'arch' tag should be removed
  247. my $symbol21 = $tags_obj_amd64->get_symbol('symbol21_amd64@Base');
  248. $tags_obj_i386->add_dynamic_symbol($symbol21);
  249. $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
  250. $sym = $sym_file->lookup_symbol('symbol21_amd64@Base', ['libsymboltags.so.1']);
  251. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol21_amd64@Base', 'symbol_templ' => 'symbol21_amd64@Base',
  252. 'symbol_quoted' => "'", 'minver' => '2.1', 'dep_id' => 0, 'deprecated' => 0,
  253. 'depends' => 'libsymboltags1 #MINVER#', 'soname' => 'libsymboltags.so.1' ),
  254. 'symbol appears on foreign arch, arch tag should be removed');
  255. @tmp = map { $_->get_symbolname() } $sym_file->get_new_symbols($sym_file_dup);
  256. is_deeply( \@tmp, [ 'symbol21_amd64@Base' ], "symbol from foreign arch is NEW");
  257. is( $sym->get_symbolspec(1), ' symbol21_amd64@Base 2.1', 'no tags => no quotes in the symbol name' );
  258. # - arch specific symbol disappears
  259. delete $tags_obj_i386->{dynsyms}{'symbol22_i386@Base'};
  260. delete $tags_obj_i386->{dynsyms}{'symbol41_i386_and_optional@Base'};
  261. $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
  262. $sym = $sym_file->lookup_symbol('symbol22_i386@Base', ['libsymboltags.so.1'], 1);
  263. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol22_i386@Base', 'symbol_templ' => 'symbol22_i386@Base',
  264. 'minver' => '2.2', 'dep_id' => 0, 'deprecated' => '100.MISSING',
  265. 'depends' => 'libsymboltags1 #MINVER#', 'soname' => 'libsymboltags.so.1',
  266. 'tags' => { 'arch' => '!amd64 !ia64 !alpha' }, 'tagorder' => [ 'arch' ] ),
  267. 'disappeared arch specific symbol gets deprecated');
  268. $sym = $sym_file->lookup_symbol('symbol41_i386_and_optional@Base', ['libsymboltags.so.1'], 1);
  269. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol41_i386_and_optional@Base',
  270. 'symbol_templ' => 'symbol41_i386_and_optional@Base', 'symbol_quoted' => '"',
  271. 'minver' => '4.1', 'dep_id' => 0, 'deprecated' => '100.MISSING',
  272. 'depends' => 'libsymboltags1 #MINVER#', 'soname' => 'libsymboltags.so.1',
  273. 'tags' => { 'arch' => 'i386', 'optional' => 'reason' }, 'tagorder' => [ 'arch', 'optional' ] ),
  274. 'disappeared optional arch specific symbol gets deprecated');
  275. @tmp = map { $_->get_symbolname() } $sym_file->get_lost_symbols($sym_file_dup);
  276. is_deeply( \@tmp, [ 'symbol22_i386@Base' ], "missing arch specific is LOST, but optional arch specific isn't");
  277. # Tests for tagged #includes
  278. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$srcdir/symbols.include-3", arch => 'i386');
  279. $sym = $sym_file->lookup_symbol('symbol2_fake1@Base', ['libsymboltags.so.2']);
  280. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol2_fake1@Base',
  281. 'minver' => '1.0', 'depends' => 'libsymboltags2', 'soname' => 'libsymboltags.so.2',
  282. 'tags' => { 'optional' => undef, 'random tag' => 'random value' },
  283. 'tagorder' => [ 'optional', 'random tag' ] ),
  284. 'symbols from #included file inherits tags');
  285. $sym = $sym_file->lookup_symbol('symbol41_i386_and_optional@Base', ['libsymboltags.so.1']);
  286. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol41_i386_and_optional@Base',
  287. 'symbol_templ' => 'symbol41_i386_and_optional@Base', symbol_quoted => '"',
  288. 'minver' => '4.1', 'depends' => 'libsymboltags1 #MINVER#', 'soname' => 'libsymboltags.so.1',
  289. 'tags' => { 'optional' => 'reason', 't' => 'v', 'arch' => 'i386' },
  290. 'tagorder' => [ 'optional', 't', 'arch' ] ),
  291. 'symbols in #included file can override tag values');
  292. $sym = $sym_file->lookup_symbol('symbol51_untagged@Base', ['libsymboltags.so.1']);
  293. is_deeply($sym, Dpkg::Shlibs::Symbol->new( 'symbol' => 'symbol51_untagged@Base',
  294. 'minver' => '5.1', 'depends' => 'libsymboltags1 #MINVER#', 'soname' => 'libsymboltags.so.1',
  295. 'tags' => { 'optional' => 'from parent', 't' => 'v' },
  296. 'tagorder' => [ 'optional', 't' ] ),
  297. 'symbols are properly cloned when #including');