By the time you have completed this lab you should be able to
file
command.strings
, nm
, objdump
and readelf
g++ -g -c lab07.cpp
g++ -o lab07 lab07.o
file
command for the three files:
-bash-4.3$ file lab07* lab07: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=bc979079d929c86213775cf197744af9d597b51f, not stripped lab07.cpp: C source, ASCII text lab07.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not strippedELF stands for "Executable and Linking Format" as you learned from the Reader and lecture, LSB means "Linux Standard Base" (a collection of APIs meant to be supported by all Linux systems), and SYSV abbreviates "UNIX System V." What else can you learn from these results? And what do you suppose "not stripped" means?
strings
command can be used to list the printable strings in a binary file. For
example, the relocatable object file (lab07.o) has the two strings created in the source
code ("element " and " is "), plus lots of other printable strings added in the compile step. The
'-d' option means "Only print strings from initialized, loaded data sections in the file." Try it:
-bash-4.3$ strings -d lab07.o element isDepending on how the strings program is configured, the '-d' behavior might be the default. If you want to see all printable strings in a binary file, use '-a' to override the default. Meanwhile, the executable file contains even more strings. Try `strings lab07` to see them if you are curious.
nm
and objdump
The command nm
lists the "names" (symbols) in an object or load module, and
objdump
displays various information about such files. Both commands have
useful options that are worth learning.
-bash-4.3$ nm lab07.o 0000000000000000 D a 0000000000000000 B b U __cxa_atexit U __dso_handle 00000000000000e5 t _GLOBAL__sub_I_a 0000000000000000 T main 00000000000000a7 t _Z41__static_initialization_and_destruction_0ii 0000000000000000 r _ZL1M U _ZNSolsEi U _ZNSolsEPFRSoS_E U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev U _ZSt4cout U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_ 0000000000000028 b _ZStL8__ioinit U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 0000000000000028 d _ZZ4mainE1kThe values on the left side are addresses in hexadecimal, and you can show these values in decimal with the "-td" option. The next column shows the class of each symbol, where T and t are functions, D and d are data (global variables), r is read-only data, B and b are uninitialized data, and U is undefined.
Using objdump
can reveal more details about object files. At least one
option must be specified.
more
to view one screenful at a time:
objdump -S lab07.o | moreTry it now. According to
man
, the "-S" option will "display source
code intermixed with disassembly, if possible. Implies -d" where "-d" means disassemble the code.man objdump
or read the man page at http://sourceware.org/binutils/docs/binutils/objdump.html#objdump
to learn about more options. You might like the "-l" (ELL) option in particular - use
it along with the "-d" option as follows:
objdump -d -l lab07.o | more
nm
command. Do you remember that purpose? The -j option lets you specify
"just" particular sections. Interesting sections include .text, .data, .rodata, and .ctors,
among others.readelf
Although you could use objdump to inspect this load module, the readelf
tool gives
more information about load modules (so called because they are ready to be loaded
into memory for execution). As with objdump
, at least one option must be specified.
readelf
than it does
for objdump
? It means
"Display the sections' header" for readelf
. Use it now to find out the names
of the sections in lab07:
readelf -S lab07
a
stored?M
stored?k
stored?b
handled differently than other variables?i
is handled differently too. How?
By the way, don't just search the object and load modules for answers to these questions. Instead recall what you learned from the reading. Ask if you get stuck.
First be sure you know the answers to all of questions a through f listed at the end of Step 4. Then ...
Get your TA's attention to inspect your work, and to record completion of your in-lab work. |
Now look around the lab to find out if there are other students who might need your help. If yes, first ask them if they would like you to help, and if they say "Yes" then do help guide them through their remaining lab steps - without just giving them the exact solution. When all students are either successful or are being helped by someone else, then you may leave early! But make sure that you were checked off first.
Step 5a. ONLY IF YOU RAN OUT OF TIME TO HAVE THE TA INSPECT YOUR WORK
If you must complete this assignment at CSIL, then submit it with the turnin program - but do NOT turn it in if the TA already checked you off.
First create a text file named lab07.txt, and type answers to each of the six questions at the end of Step 4 above. You MUST have both your name and your partner's name in lab07.txt in order to receive credit.
Bring up a terminal window on CSIL, and cd into the original pilot's ~/cs32/lab07 directory. Then type the following to turn in your text file:
turnin lab07@cs32 lab07.txt
Each pair of students must accomplish the following to earn full credit for this lab:
Prepared by Michael Costanzo.