1#! /bin/sh 2# Copyright (C) 2013 Red Hat, Inc. 3# This file is part of elfutils. 4# 5# This file is free software; you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation; either version 3 of the License, or 8# (at your option) any later version. 9# 10# elfutils is distributed in the hope that it will be useful, but 11# WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18. $srcdir/test-subr.sh 19 20# Tests readelf -s and readelf --elf-section -s 21# See also run-dwflsyms.sh 22# 23# - bar.c 24# 25# static int b1 = 1; 26# int b2 = 1; 27# 28# static int 29# foo (int a) 30# { 31# return a + b2; 32# } 33# 34# int bar (int b) 35# { 36# return b - foo (b - b1); 37# } 38# 39# - foo.c 40# 41# extern int bar (int b); 42# extern int b2; 43# 44# int 45# main (int argc, char ** argv) 46# { 47# return bar (argc + b2); 48# } 49# 50# gcc -pie -g -c foo.c 51# gcc -pie -g -c bar.c 52# gcc -pie -g -o baz foo.o bar.o 53# 54# - testfilebaztab (dynsym + symtab) 55# cp baz testfilebaztab 56# 57# - testfilebazdbg (dynsym + .debug file) 58# eu-strip --remove-comment -f testfilebazdbg.debug baz 59# cp baz testfilebazdbg 60# 61#- testfilebazdyn (dynsym only) 62# objcopy --remove-section=.gnu_debuglink baz testfilebazdyn 63# 64# - testfilebazmdb (dynsym + gnu_debugdata + .debug) 65# This is how rpmbuild does it: 66# nm -D baz --format=posix --defined-only | awk '{ print $1 }' | sort > dynsyms 67# nm baz.debug --format=posix --defined-only | awk '{ if ($2 == "T" || $2 == "t") print $1 }' | sort > funcsyms 68# comm -13 dynsyms funcsyms > keep_symbols 69# objcopy -S --remove-section .gdb_index --remove-section .comment --keep-symbols=keep_symbols baz.debug mini_debuginfo 70# rm -f mini_debuginfo.xz 71# xz mini_debuginfo 72# objcopy --add-section .gnu_debugdata=mini_debuginfo.xz baz 73# cp baz testfilebazmdb 74# 75# - testfilebazmin (dynsym + gnu_debugdata) 76# objcopy --remove-section=.gnu_debuglink baz testfilebazmin 77# 78# 79# Special auxiliary only, can happen with static binaries. 80# - start.c 81# 82# extern int main (int argc, char ** argv); 83# void _start (void) { for (;;) main (1, 0); } 84# 85# gcc -g -c start.c 86# gcc -static -nostdlib -o bas foo.o bar.o start.o 87# 88# eu-strip --remove-comment -f bas.debug bas 89# nm bas.debug --format=posix --defined-only | awk '{ if ($2 == "T" || $2 == "t") print $1 }' | sort > funcsyms 90# objcopy -S --remove-section .gdb_index --remove-section .comment --keep-symbols=funcsyms bas.debug mini_debuginfo 91# rm -f mini_debuginfo.xz 92# xz mini_debuginfo 93# objcopy --add-section .gnu_debugdata=mini_debuginfo.xz bas 94# rm bas.debug 95# mv bas testfilebasmin 96# 97# 98# Make sure that find_aux_sym doesn't corrupt relocations, avoiding a kernel 99# heuristic that forces ET_EXEC->ET_DYN. NB: ld.gold doesn't seem to produce 100# the mismatched load addrs between the main file and the mini_debuginfo, so 101# this is forcing ld.bfd. 102# 103# gcc -g -o bax foo.c bar.c -fuse-ld=bfd 104# eu-strip --remove-comment -f bax.debug bax 105# nm -D bax --format=posix --defined-only | awk '{ print $1 }' | sort > dynsyms 106# nm bax.debug --format=posix --defined-only | awk '{ if ($2 == "T" || $2 == "t") print $1 }' | sort > funcsyms 107# comm -13 dynsyms funcsyms > keep_symbols 108# objcopy -S --remove-section .gdb_index --remove-section .comment --keep-symbols=keep_symbols bax.debug mini_debuginfo 109# rm -f mini_debuginfo.xz 110# xz mini_debuginfo 111# objcopy --add-section .gnu_debugdata=mini_debuginfo.xz bax 112# objcopy --remove-section=.gnu_debuglink bax testfilebaxmin 113 114 115testfiles testfilebaztab 116testfiles testfilebazdbg testfilebazdbg.debug 117testfiles testfilebazdyn 118testfiles testfilebazmdb 119testfiles testfilebazmin 120testfiles testfilebasmin 121testfiles testfilebaxmin 122 123tempfiles testfile.dynsym.in testfile.symtab.in testfile.minsym.in 124 125cat > testfile.dynsym.in <<\EOF 126 127Symbol table [ 5] '.dynsym' contains 14 entries: 128 2 local symbols String table: [ 6] '.dynstr' 129 Num: Value Size Type Bind Vis Ndx Name 130 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UNDEF 131 1: 0000000000000238 0 SECTION LOCAL DEFAULT 1 132 2: 0000000000000000 0 NOTYPE WEAK DEFAULT UNDEF _ITM_deregisterTMCloneTable 133 3: 0000000000000000 0 FUNC GLOBAL DEFAULT UNDEF __libc_start_main@GLIBC_2.2.5 (2) 134 4: 0000000000000000 0 NOTYPE WEAK DEFAULT UNDEF __gmon_start__ 135 5: 0000000000000000 0 NOTYPE WEAK DEFAULT UNDEF _Jv_RegisterClasses 136 6: 0000000000000000 0 NOTYPE WEAK DEFAULT UNDEF _ITM_registerTMCloneTable 137 7: 0000000000000000 0 FUNC WEAK DEFAULT UNDEF __cxa_finalize@GLIBC_2.2.5 (2) 138 8: 000000000020103c 0 NOTYPE GLOBAL DEFAULT 25 _edata 139 9: 0000000000201040 0 NOTYPE GLOBAL DEFAULT 26 _end 140 10: 0000000000000860 137 FUNC GLOBAL DEFAULT 13 __libc_csu_init 141 11: 000000000020103c 0 NOTYPE GLOBAL DEFAULT 26 __bss_start 142 12: 00000000000007f0 35 FUNC GLOBAL DEFAULT 13 main 143 13: 00000000000008f0 2 FUNC GLOBAL DEFAULT 13 __libc_csu_fini 144EOF 145 146cat > testfile.symtab.in <<\EOF 147 148Symbol table [34] '.symtab' contains 76 entries: 149 54 local symbols String table: [35] '.strtab' 150 Num: Value Size Type Bind Vis Ndx Name 151 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UNDEF 152 1: 0000000000000238 0 SECTION LOCAL DEFAULT 1 153 2: 0000000000000254 0 SECTION LOCAL DEFAULT 2 154 3: 0000000000000274 0 SECTION LOCAL DEFAULT 3 155 4: 0000000000000298 0 SECTION LOCAL DEFAULT 4 156 5: 00000000000002d8 0 SECTION LOCAL DEFAULT 5 157 6: 0000000000000428 0 SECTION LOCAL DEFAULT 6 158 7: 00000000000004f2 0 SECTION LOCAL DEFAULT 7 159 8: 0000000000000510 0 SECTION LOCAL DEFAULT 8 160 9: 0000000000000530 0 SECTION LOCAL DEFAULT 9 161 10: 0000000000000638 0 SECTION LOCAL DEFAULT 10 162 11: 0000000000000680 0 SECTION LOCAL DEFAULT 11 163 12: 00000000000006a0 0 SECTION LOCAL DEFAULT 12 164 13: 00000000000006e0 0 SECTION LOCAL DEFAULT 13 165 14: 00000000000008f4 0 SECTION LOCAL DEFAULT 14 166 15: 0000000000000900 0 SECTION LOCAL DEFAULT 15 167 16: 0000000000000904 0 SECTION LOCAL DEFAULT 16 168 17: 0000000000000948 0 SECTION LOCAL DEFAULT 17 169 18: 0000000000200dd0 0 SECTION LOCAL DEFAULT 18 170 19: 0000000000200dd8 0 SECTION LOCAL DEFAULT 19 171 20: 0000000000200de0 0 SECTION LOCAL DEFAULT 20 172 21: 0000000000200de8 0 SECTION LOCAL DEFAULT 21 173 22: 0000000000200df0 0 SECTION LOCAL DEFAULT 22 174 23: 0000000000200fc0 0 SECTION LOCAL DEFAULT 23 175 24: 0000000000201000 0 SECTION LOCAL DEFAULT 24 176 25: 0000000000201030 0 SECTION LOCAL DEFAULT 25 177 26: 000000000020103c 0 SECTION LOCAL DEFAULT 26 178 27: 0000000000000000 0 SECTION LOCAL DEFAULT 27 179 28: 0000000000000000 0 SECTION LOCAL DEFAULT 28 180 29: 0000000000000000 0 SECTION LOCAL DEFAULT 29 181 30: 0000000000000000 0 SECTION LOCAL DEFAULT 30 182 31: 0000000000000000 0 SECTION LOCAL DEFAULT 31 183 32: 0000000000000000 0 SECTION LOCAL DEFAULT 32 184 33: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c 185 34: 0000000000200de0 0 OBJECT LOCAL DEFAULT 20 __JCR_LIST__ 186 35: 0000000000000710 0 FUNC LOCAL DEFAULT 13 deregister_tm_clones 187 36: 0000000000000740 0 FUNC LOCAL DEFAULT 13 register_tm_clones 188 37: 0000000000000780 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux 189 38: 000000000020103c 1 OBJECT LOCAL DEFAULT 26 completed.6137 190 39: 0000000000200dd8 0 OBJECT LOCAL DEFAULT 19 __do_global_dtors_aux_fini_array_entry 191 40: 00000000000007c0 0 FUNC LOCAL DEFAULT 13 frame_dummy 192 41: 0000000000200dd0 0 OBJECT LOCAL DEFAULT 18 __frame_dummy_init_array_entry 193 42: 0000000000000000 0 FILE LOCAL DEFAULT ABS foo.c 194 43: 0000000000000000 0 FILE LOCAL DEFAULT ABS bar.c 195 44: 0000000000201034 4 OBJECT LOCAL DEFAULT 25 b1 196 45: 0000000000000814 20 FUNC LOCAL DEFAULT 13 foo 197 46: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c 198 47: 0000000000000a58 0 OBJECT LOCAL DEFAULT 17 __FRAME_END__ 199 48: 0000000000200de0 0 OBJECT LOCAL DEFAULT 20 __JCR_END__ 200 49: 0000000000000000 0 FILE LOCAL DEFAULT ABS 201 50: 0000000000200dd8 0 NOTYPE LOCAL DEFAULT 18 __init_array_end 202 51: 0000000000200df0 0 OBJECT LOCAL DEFAULT 22 _DYNAMIC 203 52: 0000000000200dd0 0 NOTYPE LOCAL DEFAULT 18 __init_array_start 204 53: 0000000000201000 0 OBJECT LOCAL DEFAULT 24 _GLOBAL_OFFSET_TABLE_ 205 54: 00000000000008f0 2 FUNC GLOBAL DEFAULT 13 __libc_csu_fini 206 55: 0000000000000000 0 NOTYPE WEAK DEFAULT UNDEF _ITM_deregisterTMCloneTable 207 56: 0000000000201030 0 NOTYPE WEAK DEFAULT 25 data_start 208 57: 000000000020103c 0 NOTYPE GLOBAL DEFAULT 25 _edata 209 58: 0000000000000828 44 FUNC GLOBAL DEFAULT 13 bar 210 59: 00000000000008f4 0 FUNC GLOBAL DEFAULT 14 _fini 211 60: 0000000000000000 0 FUNC GLOBAL DEFAULT UNDEF __libc_start_main@@GLIBC_2.2.5 212 61: 0000000000201030 0 NOTYPE GLOBAL DEFAULT 25 __data_start 213 62: 0000000000000000 0 NOTYPE WEAK DEFAULT UNDEF __gmon_start__ 214 63: 0000000000200de8 0 OBJECT GLOBAL HIDDEN 21 __dso_handle 215 64: 0000000000000900 4 OBJECT GLOBAL DEFAULT 15 _IO_stdin_used 216 65: 0000000000201038 4 OBJECT GLOBAL DEFAULT 25 b2 217 66: 0000000000000860 137 FUNC GLOBAL DEFAULT 13 __libc_csu_init 218 67: 0000000000201040 0 NOTYPE GLOBAL DEFAULT 26 _end 219 68: 00000000000006e0 0 FUNC GLOBAL DEFAULT 13 _start 220 69: 000000000020103c 0 NOTYPE GLOBAL DEFAULT 26 __bss_start 221 70: 00000000000007f0 35 FUNC GLOBAL DEFAULT 13 main 222 71: 0000000000000000 0 NOTYPE WEAK DEFAULT UNDEF _Jv_RegisterClasses 223 72: 0000000000201040 0 OBJECT GLOBAL HIDDEN 25 __TMC_END__ 224 73: 0000000000000000 0 NOTYPE WEAK DEFAULT UNDEF _ITM_registerTMCloneTable 225 74: 0000000000000000 0 FUNC WEAK DEFAULT UNDEF __cxa_finalize@@GLIBC_2.2.5 226 75: 0000000000000680 0 FUNC GLOBAL DEFAULT 11 _init 227EOF 228 229cat > testfile.minsym.in <<\EOF 230 231Symbol table [28] '.symtab' contains 40 entries: 232 36 local symbols String table: [29] '.strtab' 233 Num: Value Size Type Bind Vis Ndx Name 234 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UNDEF 235 1: 0000000000000710 0 FUNC LOCAL DEFAULT 13 deregister_tm_clones 236 2: 0000000000000740 0 FUNC LOCAL DEFAULT 13 register_tm_clones 237 3: 0000000000000780 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux 238 4: 0000000000200dd8 0 OBJECT LOCAL DEFAULT 19 __do_global_dtors_aux_fini_array_entry 239 5: 00000000000007c0 0 FUNC LOCAL DEFAULT 13 frame_dummy 240 6: 0000000000200dd0 0 OBJECT LOCAL DEFAULT 18 __frame_dummy_init_array_entry 241 7: 0000000000000814 20 FUNC LOCAL DEFAULT 13 foo 242 8: 0000000000200dd8 0 NOTYPE LOCAL DEFAULT 18 __init_array_end 243 9: 0000000000200dd0 0 NOTYPE LOCAL DEFAULT 18 __init_array_start 244 10: 0000000000000238 0 SECTION LOCAL DEFAULT 1 245 11: 0000000000000254 0 SECTION LOCAL DEFAULT 2 246 12: 0000000000000274 0 SECTION LOCAL DEFAULT 3 247 13: 0000000000000298 0 SECTION LOCAL DEFAULT 4 248 14: 00000000000002d8 0 SECTION LOCAL DEFAULT 5 249 15: 0000000000000428 0 SECTION LOCAL DEFAULT 6 250 16: 00000000000004f2 0 SECTION LOCAL DEFAULT 7 251 17: 0000000000000510 0 SECTION LOCAL DEFAULT 8 252 18: 0000000000000530 0 SECTION LOCAL DEFAULT 9 253 19: 0000000000000638 0 SECTION LOCAL DEFAULT 10 254 20: 0000000000000680 0 SECTION LOCAL DEFAULT 11 255 21: 00000000000006a0 0 SECTION LOCAL DEFAULT 12 256 22: 00000000000006e0 0 SECTION LOCAL DEFAULT 13 257 23: 00000000000008f4 0 SECTION LOCAL DEFAULT 14 258 24: 0000000000000900 0 SECTION LOCAL DEFAULT 15 259 25: 0000000000000904 0 SECTION LOCAL DEFAULT 16 260 26: 0000000000000948 0 SECTION LOCAL DEFAULT 17 261 27: 0000000000200dd0 0 SECTION LOCAL DEFAULT 18 262 28: 0000000000200dd8 0 SECTION LOCAL DEFAULT 19 263 29: 0000000000200de0 0 SECTION LOCAL DEFAULT 20 264 30: 0000000000200de8 0 SECTION LOCAL DEFAULT 21 265 31: 0000000000200df0 0 SECTION LOCAL DEFAULT 22 266 32: 0000000000200fc0 0 SECTION LOCAL DEFAULT 23 267 33: 0000000000201000 0 SECTION LOCAL DEFAULT 24 268 34: 0000000000201030 0 SECTION LOCAL DEFAULT 25 269 35: 000000000020103c 0 SECTION LOCAL DEFAULT 26 270 36: 0000000000000828 44 FUNC GLOBAL DEFAULT 13 bar 271 37: 00000000000008f4 0 FUNC GLOBAL DEFAULT 14 _fini 272 38: 00000000000006e0 0 FUNC GLOBAL DEFAULT 13 _start 273 39: 0000000000000680 0 FUNC GLOBAL DEFAULT 11 _init 274EOF 275 276cat testfile.dynsym.in testfile.symtab.in \ 277 | testrun_compare ${abs_top_builddir}/src/readelf -s testfilebaztab 278 279cat testfile.dynsym.in \ 280 | testrun_compare ${abs_top_builddir}/src/readelf -s testfilebazdbg 281 282cat testfile.symtab.in \ 283 | testrun_compare ${abs_top_builddir}/src/readelf -s testfilebazdbg.debug 284 285cat testfile.dynsym.in \ 286 | testrun_compare ${abs_top_builddir}/src/readelf -s testfilebazdyn 287 288cat testfile.dynsym.in \ 289 | testrun_compare ${abs_top_builddir}/src/readelf -s testfilebazmdb 290 291cat testfile.minsym.in \ 292 | testrun_compare ${abs_top_builddir}/src/readelf --elf-section -s testfilebazmdb 293 294cat testfile.dynsym.in \ 295 | testrun_compare ${abs_top_builddir}/src/readelf -s testfilebazmin 296 297cat testfile.minsym.in \ 298 | testrun_compare ${abs_top_builddir}/src/readelf --elf-section -s testfilebazmin 299 300testrun_compare ${abs_top_builddir}/src/readelf -s testfilebasmin <<EOF 301EOF 302 303testrun_compare ${abs_top_builddir}/src/readelf --elf-section -s testfilebasmin <<\EOF 304 305Symbol table [ 6] '.symtab' contains 9 entries: 306 6 local symbols String table: [ 7] '.strtab' 307 Num: Value Size Type Bind Vis Ndx Name 308 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UNDEF 309 1: 0000000000400168 18 FUNC LOCAL DEFAULT 2 foo 310 2: 0000000000400120 0 SECTION LOCAL DEFAULT 1 311 3: 0000000000400144 0 SECTION LOCAL DEFAULT 2 312 4: 00000000004001c0 0 SECTION LOCAL DEFAULT 3 313 5: 0000000000600258 0 SECTION LOCAL DEFAULT 4 314 6: 00000000004001a8 21 FUNC GLOBAL DEFAULT 2 _start 315 7: 0000000000400144 33 FUNC GLOBAL DEFAULT 2 main 316 8: 000000000040017a 44 FUNC GLOBAL DEFAULT 2 bar 317EOF 318 319testrun_compare ${abs_top_builddir}/src/readelf -s testfilebaxmin <<EOF 320 321Symbol table [ 5] '.dynsym' contains 3 entries: 322 1 local symbol String table: [ 6] '.dynstr' 323 Num: Value Size Type Bind Vis Ndx Name 324 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UNDEF 325 1: 0000000000000000 0 FUNC GLOBAL DEFAULT UNDEF __libc_start_main@GLIBC_2.2.5 (2) 326 2: 0000000000000000 0 NOTYPE WEAK DEFAULT UNDEF __gmon_start__ 327EOF 328 329testrun_compare ${abs_top_builddir}/src/readelf --elf-section -s testfilebaxmin <<\EOF 330 331Symbol table [27] '.symtab' contains 42 entries: 332 35 local symbols String table: [28] '.strtab' 333 Num: Value Size Type Bind Vis Ndx Name 334 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UNDEF 335 1: 0000000000400430 0 FUNC LOCAL DEFAULT 13 deregister_tm_clones 336 2: 0000000000400460 0 FUNC LOCAL DEFAULT 13 register_tm_clones 337 3: 00000000004004a0 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux 338 4: 0000000000600e18 0 OBJECT LOCAL DEFAULT 19 __do_global_dtors_aux_fini_array_entry 339 5: 00000000004004c0 0 FUNC LOCAL DEFAULT 13 frame_dummy 340 6: 0000000000600e10 0 OBJECT LOCAL DEFAULT 18 __frame_dummy_init_array_entry 341 7: 00000000004004f0 20 FUNC LOCAL DEFAULT 13 foo 342 8: 0000000000600e18 0 NOTYPE LOCAL DEFAULT 18 __init_array_end 343 9: 0000000000600e10 0 NOTYPE LOCAL DEFAULT 18 __init_array_start 344 10: 0000000000400238 0 SECTION LOCAL DEFAULT 1 345 11: 0000000000400254 0 SECTION LOCAL DEFAULT 2 346 12: 0000000000400274 0 SECTION LOCAL DEFAULT 3 347 13: 0000000000400298 0 SECTION LOCAL DEFAULT 4 348 14: 00000000004002b8 0 SECTION LOCAL DEFAULT 5 349 15: 0000000000400300 0 SECTION LOCAL DEFAULT 6 350 16: 0000000000400338 0 SECTION LOCAL DEFAULT 7 351 17: 0000000000400340 0 SECTION LOCAL DEFAULT 8 352 18: 0000000000400360 0 SECTION LOCAL DEFAULT 9 353 19: 0000000000400378 0 SECTION LOCAL DEFAULT 10 354 20: 00000000004003a8 0 SECTION LOCAL DEFAULT 11 355 21: 00000000004003d0 0 SECTION LOCAL DEFAULT 12 356 22: 0000000000400400 0 SECTION LOCAL DEFAULT 13 357 23: 00000000004005c4 0 SECTION LOCAL DEFAULT 14 358 24: 00000000004005d0 0 SECTION LOCAL DEFAULT 15 359 25: 00000000004005e0 0 SECTION LOCAL DEFAULT 16 360 26: 0000000000400628 0 SECTION LOCAL DEFAULT 17 361 27: 0000000000600e10 0 SECTION LOCAL DEFAULT 18 362 28: 0000000000600e18 0 SECTION LOCAL DEFAULT 19 363 29: 0000000000600e20 0 SECTION LOCAL DEFAULT 20 364 30: 0000000000600e28 0 SECTION LOCAL DEFAULT 21 365 31: 0000000000600ff8 0 SECTION LOCAL DEFAULT 22 366 32: 0000000000601000 0 SECTION LOCAL DEFAULT 23 367 33: 0000000000601028 0 SECTION LOCAL DEFAULT 24 368 34: 0000000000601034 0 SECTION LOCAL DEFAULT 25 369 35: 00000000004005c0 2 FUNC GLOBAL DEFAULT 13 __libc_csu_fini 370 36: 0000000000400504 40 FUNC GLOBAL DEFAULT 13 bar 371 37: 00000000004005c4 0 FUNC GLOBAL DEFAULT 14 _fini 372 38: 0000000000400550 101 FUNC GLOBAL DEFAULT 13 __libc_csu_init 373 39: 0000000000400400 0 FUNC GLOBAL DEFAULT 13 _start 374 40: 000000000040052c 35 FUNC GLOBAL DEFAULT 13 main 375 41: 00000000004003a8 0 FUNC GLOBAL DEFAULT 11 _init 376EOF 377 378exit 0 379