CSCI 341: Lab 1
The
Elf
Format
Due:
11:59
PM
on
Monday,
February 22nd
This homework is meant to get you familiarized with the concept of symbols and how they relate to compiling and linking executables.
The skeleton code for this assignment is available at this link. You must use GitHub classroom to write your code and keep a commit log on GitHub. Remember: git gives you warnings and errors for good reason, if it complains at the command line when you run a command, don’t just assume it’s completed correctly!
Now that you have the skeleton code, you can start coding. You should commit early and often, and push to your remote repository whenever is convenient to back up your work!
Programming environment
Your readelf output will vary depending on your compiler. I recommend doing all programming for this class on its dedicated server, cs341.cs.oberlin.edu, to ensure you get the results listed here. You can log in with your CS username and password.
The Programming Part!
This part will give you a quick introduction to
using readelf
to better
understand the linking process.
In this assignment, you must fill
hw1.c
with code which
will:
- cause your full name to be printed on the first line of output when the program is run.
- cause
gcc -c -Wall hw1.c
andgcc -Wall -o hw1 *.c
to issue zero warnings (and zero errors). - cause Type, Bind, and Ndx of each Symbol
to be the same as in the
example
below. E.g.
important
must have TypeFUNC
, BindLOCAL
, and Ndx1
. Size needs to be the same for Objects, but not for functions. Everything that should match is bolded below.
Running readelf -sW hw1.o
on the
solution results in:
Num: | Value | Size | Type | Bind | Vis | Ndx | Name |
---|---|---|---|---|---|---|---|
0: | 0000000000000000 | 0 | NOTYPE | LOCAL | DEFAULT | UND | |
1: | 0000000000000000 | 0 | FILE | LOCAL | DEFAULT | ABS | hw1.c |
2: | 0000000000000000 | 0 | SECTION | LOCAL | DEFAULT | 1 | |
3: | 0000000000000000 | 0 | SECTION | LOCAL | DEFAULT | 3 | |
4: | 0000000000000000 | 0 | SECTION | LOCAL | DEFAULT | 4 | |
5: | 0000000000000000 | 0 | SECTION | LOCAL | DEFAULT | 5 | |
6: | 0000000000000000 | 0 | SECTION | LOCAL | DEFAULT | 7 | |
7: | 0000000000000008 | 4 | OBJECT | LOCAL | DEFAULT | 3 | order |
8: | 0000000000000000 | 4 | OBJECT | LOCAL | DEFAULT | 4 | isnt |
9: | 0000000000000000 | 58 | FUNC | LOCAL | DEFAULT | 1 | important |
10: | 0000000000000010 | 8 | OBJECT | LOCAL | DEFAULT | 3 | for_these |
11: | 0000000000000008 | 8 | OBJECT | LOCAL | DEFAULT | 4 | symbols |
12: | 0000000000000000 | 0 | SECTION | LOCAL | DEFAULT | 11 | |
13: | 0000000000000000 | 0 | SECTION | LOCAL | DEFAULT | 12 | |
14: | 0000000000000000 | 0 | SECTION | LOCAL | DEFAULT | 10 | |
15: | 0000000000000020 | 100 | OBJECT | GLOBAL | DEFAULT | COM | but_the |
16: | 0000000000000000 | 8 | OBJECT | GLOBAL | DEFAULT | 3 | types |
17: | 0000000000000000 | 8 | OBJECT | GLOBAL | DEFAULT | 5 | and_the |
18: | 0000000000000000 | 15 | OBJECT | GLOBAL | DEFAULT | 7 | scopes |
19: | 000000000000003a | 32 | FUNC | GLOBAL | DEFAULT | 1 | matter |
20: | 000000000000005a | 44 | FUNC | GLOBAL | DEFAULT | 1 | main |
21: | 0000000000000000 | 0 | NOTYPE | GLOBAL | DEFAULT | UND | _GLOBAL_OFFSET_TABLE_ |
22: | 0000000000000000 | 0 | NOTYPE | GLOBAL | DEFAULT | UND | puts |
As a further hint, here’s the mapping between the Section Index and the Section Names:
Section
Headers:
[Nr] Name
[ 0]
[ 1] .text
[ 2] .rela.text
[ 3] .data
[ 4] .bss
[ 5] .data.rel.local
[ 6] .rela.data.rel.local
[ 7] .rodata
[ 8] .comment
[ 9] .note.GNU-stack
[10] .eh_frame
[11] .rela.eh_frame
[12] .symtab
[13] .strtab
[14] .shstrtab
Hints:
- Section names will correspond to different numbers depending on the compiler you use and will also change as you add symbols to different functions. Use readelf -S to check which sections correspond to which section number. Make sure you are working on the cs341 server
- Order for the symbols does not matter. Take a close look at Chapter 7 and think about what properties of a variable put it in a different section, or would give it a different Binding or Type. Everything you need to know to solve this puzzle is in the book.
Template
There is a very bare bones skeleton and a
Makefile
provided for this
assignment. The skeleton file is worth two points
because it compiles with no
warnings and no errors.
How to Submit The Lab
You will need to submit to Gradescope by using github. Instructions on how to do this are in Worksheet 3. Grading will be done automatically using Gradescope. Submitting to GitHub is not sufficient - your code must be submitted to Gradescope. If you have issues with the autograder, please contact me.Due Date
This assignment is due 02/22/2021 11:59 PM. See the syllabus for the late turnin policy. This assignment is worth just as much as every other homework, but is easier than all the rest (in our opinion) so getting as much credit on it as possible is important!
Helpful Documents
Chapter 7 in the book
Lab written by C. Taylor, C. Kanich