This lab is going to cover Chapter 7.
Sort of.
Externals
- Download some software.
- Let’s take a look.
- Why is there a fetchandadd.h?
- Look at the Makefile .
- Make it.
- Run it.
- That’s not quite right. Why?
- Would a FORTRAN
COMMON
be useful here? - What about a C
extern
? - Let’s try out the nm
program.
- nm fauser.o
- nm fetchandadd.o
- Where does
accumulator
appear in these nm listings? - Put the keyword
extern
at the beginning of theaccumulator
declarator in fauser.c. - Make it.
- Run it.
- !
- Retry nm on those two object files.
- Now also add the
extern
to theaccumulator
declaration in fetchandadd.c. - make it.
- !!!!
- Re-retry nm on those two object files.
In both
accumulator
is listed asU
, for undefined. - Remove the
extern
in fetchandadd.c , the one withFetchAndAdd
. - make and nm.
Now the
accumulator
in fetchandadd.c is listed asC
, for common. Sounds like FORTRAN. - Initialize the
accumulator
in fetchandadd.c to 0. - make and nm.
Now the
accumulator
in fetchandadd.c is listed asB
for uninitialized which is odd. However, all unitiailized data in the BSS is really initialized to zero. - Initialize the
accumulator
in fetchandadd.c to 235. - make and nm.
Now the
accumulator
in fetchandadd.c is listed asD
for initialized which is expected. - Three next to-last changes:
- Move both
accumulator
declarations back inside the functions,main
andFetchAndAdd
. - Remove the
extern
insidemain
. make
and try it out.
- Move both
- Ugh. It always adds to 235.
- Two last changes:
- Put the keyword
static
in front of the declaration ofaccumulator
inside ofFetchAndAdd
. make
and try it out.
- Put the keyword
- Now
accumulator
retains its value between calls. - Finally, try to determine the connection between the
static
of 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
struct
definitions. - 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
?
- What is the type of
- 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