This lab is going to cover Chapter 7.
Sort of.
Externals
- Download some software
- Let’s take a look. It’s the famous Fetch-and-Add.
- Why is there a fetchandadd.h?
- It’s a prototype.
- Look at the Makefile , and make it.
- Run it. That’s not quite right. Why?
- Would a FORTRAN
COMMONbe useful here? - What about a C
extern?
- Would a FORTRAN
- Let’s try out the nm
program.
- nm fauser.o
- nm fetchandadd.o
- Look at all those symbol types!
- Where and how does
accumulatorappear in these nm listings?
- Put the keyword
externat the beginning of theaccumulatordeclarator in fauser.c.- Make it.
- Run it!
- Retry nm on those two object files.
- Now also add the
externto theaccumulatordeclaration in fetchandadd.c.- make it.
- !!!!
- Re-retry nm on those two object
files. In both
accumulatoris listed asU, for undefined.
- Remove the
externin fetchandadd.c , the one withFetchAndAdd.- make and nm.
- Now the
accumulatorin fetchandadd.o is listed asC, for common. Sounds like FORTRAN.
- Initialize the
accumulatorin fetchandadd.c to 0.- make and
nm.
Now the
accumulatorin fetchandadd.c is listed asBfor uninitialized which is odd. However, all unitiailized data in the BSS is really initialized to zero.
- make and
nm.
Now the
- Initialize the
accumulatorin fetchandadd.c to 235.- make and
nm. Now the
accumulatorin fetchandadd.c is listed asDfor initialized which is expected.
- make and
nm. Now the
- Three next to-last changes:
- Move both
accumulatordeclarations back inside the functions,mainandFetchAndAdd. - Remove the
externinsidemain. makeand try it out.- Ugh. It always adds to 235.
- Move both
- Two last changes:
- Put the keyword
staticin front of the declaration ofaccumulatorinside ofFetchAndAdd. makeand try it out.- Now
accumulatorretains its value between calls.
- Put the keyword
- Finally, try to determine the connection between the
staticof Java and C.
Memory
Libraries and object forms
- Unix tools from IBM Developer Works
- Download some more software.
It’s a CSCI343 homework from ancient times.
- Look at the btree.h include file.
Give some attention to the
structdefinitions. - Look at the C code in btmisc.c.
Notice how it uses structures in expressions like
i < p->head.numUsed. - What is the type of
p->head.numUsed? - What is the address of
p->head.numUsed?
- Look at the btree.h include file.
Give some attention to the
- Type gcc -S btmisc.c
- Look at btmisc.s
- For N from 0 to 3, do the following to get four files:
(Use the up-arrow)
- gcc -ON -S btmisc.c; mv btmisc.s btmisc.s-ON
- Also, to get the debugging optimization, try one more:
- gcc -Og -S btmisc.c; mv btmisc.s btmisc.s-Og
- Let’s compare the assembly files.
- Notice the sizes.
- Try to find
p->head.numUsed. The offset is 9.
- Let’s examine the Makefile.
- It uses an static library!
- make
- Look at the created files.
- Run nm on some of object (.o) files.
- Try out some of the many options of
objdump on some object files.
- --info
- --file-headers
- --section-headers
- --disassemble
- --debugging
- Use ar to list and extract members of the archive. Look at the ar manual page for ideas.
- readelf -all bupdate | more
- Resolve to master the GNU Binutils.
Dynamic library
Have you heard of DLL Hell?
- Where are the libraries — /usr/lib/x86_64-linux-gnu
- Shared library tutorial
- My experience
- A longer tutorial
A very busy program
BusyBox: Bet it’s in your router and your workstation.
- busybox
- busybox ls -l /etc
- which busybox
- ln -s /bin/busybox ls
- ls -l ls
- which ls
- ./ls -l /etc