Worksheetslinking
Total questions: 96
Worksheet time: 48mins
What is the first stage in the compilation process?
Compiler
Preprocessor
Assembler
Linker
Refer to the diagram of the Compilation Process. Which stage produces assembly code?
Preprocessor
Compiler
Assembler
Linker
Refer to the diagram of the Compilation Process. Fill in the blank: The ________ combines object code and libraries to produce executable code.
Linker
Compiler
Assembler
Preprocessor
Refer to the diagram of the Compilation Process. True or False: The Assembler produces object code.
True
False
What is the final output of the compilation process?
Executable code
Source code
Object code
Assembly code
Given the following C code from main.c and sum.c: main.c: int sum(int *a, int n); int array[2] = {1, 2}; int main() { int val = sum(array, 2); return val; } sum.c: int sum(int *a, int n) { int i, s = 0; for (i = 0; i < n; i++) { s += a[i]; } return s; } What is the value returned by the main function?
3
1
2
0
Given the following C code from main.c and sum.c: main.c: int sum(int *a, int n); int array[2] = {1, 2}; int main() { int val = sum(array, 2); return val; } sum.c: int sum(int *a, int n) { int i, s = 0; for (i = 0; i < n; i++) { s += a[i]; } return s; } What does the function sum do?
Multiplies all elements of the array
Sums all elements of the array
Returns the first element of the array
Returns the last element of the array
The purpose of the for loop in the sum function is:
to iterate through each element and add them to the total sum
to multiply all elements together
to print each element in the list
to reverse the order of elements
What is the command used to compile and link the source files main.c and sum.c into an executable named prog?
gcc -Og -o prog main.c sum.c
gcc -o main.c sum.c prog
gcc -Og -o sum.c main.c prog
gcc -Og -o main.c prog sum.c
Fill in the blank: The files main.c and sum.c are called ______ files in the static linking process.
source
object
header
library
The linker (ld) combines separately compiled object files into a fully linked executable object file.
True
False
Which files contain code and data for all functions defined in main.c and sum.c after linking?
main.o
sum.o
prog
main.c
Why is modularity important when using linkers?
It allows programs to be written as a collection of smaller source files
It makes programs monolithic
It prevents building libraries
It increases code duplication
Fill in the blank: Linkers allow you to build libraries of common functions, such as the ________ library and the standard C library.
Math
Graphics
Network
Audio
Why is separate compilation considered efficient in the context of linkers?
A) It allows changing one source file, compiling, and then relinking without recompiling other source files.
B) It requires recompiling all source files every time.
C) It does not support relinking.
D) It increases compilation time.
Fill in the blank: In the context of linkers, common functions can be aggregated into a single file called _________.
libraries
headers
objects
modules
Executable files and running memory images contain only code for the functions they actually use.
True
False
What is the first step that linkers perform?
Symbol resolution
Code optimization
Memory allocation
Error checking
Programs define and reference ______ (global variables and functions).
symbols
numbers
arrays
pointers
Where are symbol definitions stored in an object file?
In the code section
In the symbol table
In the data segment
In the stack
The symbol table is an array of structs, and each entry includes the name, size, and location of the symbol.
True
False
During the symbol resolution step, the linker associates each symbol reference with exactly one ______.
symbol definition
object file
memory address
function call
What do linkers do in Step 2: Relocation? Fill in the blank: Linkers merge separate code and data sections into ______ sections.
single
multiple
temporary
external
What do linkers do in Step 2: Relocation? Fill in the blank: Linkers relocate symbols from their relative locations in the .o files to their final ______ memory locations in the executable.
absolute
virtual
temporary
dynamic
What do linkers do in Step 2: Relocation? Fill in the blank: Linkers update all references to these symbols to reflect their new ______.
positions
colors
owners
values
Which kind of object file contains code and data in a form that can be combined with other relocatable object files to form executable object file?
Relocatable object file (.o file)
Executable object file (a.out file)
Shared object file (.so file)
Which kind of object file contains code and data in a form that can be copied directly into memory and then executed?
Relocatable object file (.o file)
Executable object file (a.out file)
Shared object file (.so file)
Fill in the blank: The shared object file (.so file) is a special type of relocatable object file that can be loaded into memory and linked dynamically, at either load time or run-time. It is called ________ by Windows.
Dynamic Link Libraries (DLLs)
Executable and Linkable Format (ELF)
Static Object Files (SOF)
Portable Executable (PE)
What is the standard binary format for object files?
A) ELF
B) PDF
C) JPEG
D) TXT
Which of the following is NOT a type of object file unified under ELF?
Relocatable object files (.o)
Executable object files (a.out)
Shared object files (.so)
Text object files (.txt)
Fill in the blank: The generic name for the format described is ________ binaries.
ELF
PE
Mach-O
COFF
Which of the following is a type of object file supported by ELF?
Relocatable object files (.o)
Executable object files (a.out)
Shared object files (.so)
All of the above
What does the ELF header contain?
Code
Word size, byte ordering, file type, machine type
Initialized global variables
Uninitialized global variables
Which section contains code in an ELF object file?
.data section
.bss section
.text section
.rodata section
The .rodata section contains _________.
Read only data: jump tables, ...
Executable code: main functions, ...
Stack data: local variables, ...
Heap data: dynamically allocated memory, ...
Which section is used for initialized global variables?
.bss section
.data section
.text section
.rodata section
The .bss section contains uninitialized global variables.
True
False
What does 'bss' stand for in the ELF object file format?
Block Started by Symbol
Binary Section Segment
Base Symbol Section
Buffer Storage Segment
Which section has a header but occupies no space in the ELF object file format?
.bss section
.text section
.data section
.rodata section
Which section in the ELF Object File Format contains the symbol table, procedure and static variable names, and section names and locations?
.symtab section
.rel.text section
.data section
.debug section
What is the purpose of the .rel.text section in the ELF Object File Format?
Contains symbol table
Relocation info for .text section
Info for symbolic debugging
Offsets and sizes of each section
The .rel.data section provides relocation info for which section?
.text section
.data section
.debug section
.symtab section
Which section in the ELF Object File Format provides info for symbolic debugging?
.debug section
.text section
.data section
.bss section
The Section header table in the ELF Object File Format provides ______ and sizes of each section.
offsets
names
addresses
permissions
The .rel.text section contains addresses of instructions that need to be modified in the executable.
True
False
Which section header table is required for executables in the ELF Object File Format?
Segment header table
Symbol table
String table
Relocation table
What are global symbols in the context of linker symbols?
Symbols defined by module m that can be referenced by other modules.
Symbols defined and referenced exclusively by module m.
Global symbols that are referenced by module m but defined by some other module.
Local program variables.
Fill in the blank: External symbols are ______ that are referenced by module m but defined by some other module.
Global symbols
Local symbols
Static symbols
Private symbols
Local linker symbols are local program variables.
True
False
Which attribute is used to define local symbols in C functions and global variables? Choose the correct option.
static
global
external
public
In the file main.c, which variable is defined as a global variable?
val
array
sum
main
In the file sum.c, which variable is returned by the function sum?
i
s
a
n
Refer to the code snippets and annotations in the image titled 'Step 1: Symbol Resolution'. The linker knows nothing of ______ in main.c.
val
func
main
int
Refer to the code snippets and annotations in the image titled 'Step 1: Symbol Resolution'. The variable 'array' is referenced as a global in main.c.
True
False
Local non-static C variables vs. local static C variables: Which of the following statements is true?
Local non-static C variables are stored in .bss
Local static C variables are stored on the stack
Local non-static C variables are stored on the stack
Local static C variables are stored in either .bss or .data
Local static C variables are stored in either ______ or ______.
.bss, .data
.text, .heap
.stack, .heap
.rodata, .stack
Local non-static C variables vs. local static C variables: Consider the following code: int f() { static int x = 0; return x; } int g() { static int x = 1; return x; } Where does the compiler allocate space for each definition of x?
.stack
.bss or .data
.heap
.text
Local non-static C variables vs. local static C variables: The compiler creates local symbols in the symbol table with unique names for each static local variable definition.
True
False
Program symbols are either strong or weak. Which of the following describes a strong symbol?
A) Procedures and initialized globals
B) Uninitialized globals
C) Local variables
D) Constants
Fill in the blank: Weak symbols are __________ globals.
uninitialized
constant
volatile
private
In the code 'int foo=5;' in p1.c, is 'foo' a strong or weak symbol?
Strong
Weak
Linker's Symbol Rules: According to Rule 1, what happens if multiple strong symbols are defined?
Each item can be defined multiple times
Each item can be defined only once
Linker error
Both B and C
Linker's Symbol Rules: According to Rule 2, given a strong symbol and multiple weak symbols, which symbol is chosen?
The weak symbol
The strong symbol
Both symbols
None
Linker's Symbol Rules: Fill in the blank for Rule 3: If there are multiple weak symbols, pick an ________ one. (Can override this with gcc -fno-common)
arbitrary
unique
default
strong
Given the following code snippets: int x; p1() {} p1() {} What is the linker behavior?
The linker reports a multiple definition error for p1().
The linker ignores the duplicate function definitions.
The linker merges both function definitions into one.
The linker reports an undefined reference to p1().
Given the following code snippets: int x; p1() {} int x; p2() {} What is the linker behavior?
The linker will report a multiple definition error for 'x'.
The linker will successfully link without errors.
The linker will ignore the second definition of 'x'.
The linker will merge both definitions of 'x'.
The linker behavior is:
Combining object files into a single executable
Translating high-level code to machine code
Debugging the source code
Editing the source code
The linker behavior is:
Combining object files into a single executable
Translating high-level code to machine code
Debugging the source code
Editing the source code
Given the following code snippets: int x=7; p1() {} int x; p2() {} What is the linker behavior?
The linker will report a multiple definition error for 'x'.
The linker will successfully link the code without errors.
The linker will ignore the second declaration of 'x'.
The linker will treat both 'x' declarations as separate variables.
Nightmare scenario: What happens when two identical weak structs are compiled by different compilers with different alignment rules?
The structs may have different memory layouts, causing compatibility issues.
The structs will always be compatible regardless of alignment rules.
The compilers will automatically resolve alignment differences.
The structs will be optimized for speed in both compilers.
Should you avoid using global variables if you can?
Yes
No
What keyword should you use if you can when defining a global variable?
static
extern
global
What should you do if you define a global variable? Fill in the blank.
initialize
delete
ignore
rename
What keyword should you use if you reference an external global variable?
static
extern
global
What is the main purpose of the relocation step in the creation of an executable object file?
To combine multiple object files into a single executable
To compile source code into object files
To debug the program
To run the program
In the context of object files, what does the '.text' section typically contain?
System data
Executable code
Debug information
Symbol table
What is stored in the '.data' section of the main.o file?
main()
int array[2]={1,2}
sum()
swap()
Refer to the diagram explaining 'Step 2: Relocation'. Fill in the blank: The sum.o file contains the function ______ in its .text section.
sum()
main()
printf()
multiply()
Refer to the diagram explaining 'Step 2: Relocation'. Which of the following sections are present in the final executable object file?
main()
Headers
swap()
System code
More system code
Which struct in ELF contains the fields 'Elf32_Addr r_offset;' and 'Elf32_Word r_info;'?
Elf32_Rel
Elf32_Rela
Elf64_Rel
Elf64_Rela
Fill in the blank: The struct Elf32_Rela contains the additional field ______ compared to Elf32_Rel.
Elf32_Sword r_addend;
Elf32_Word r_offset;
Elf32_Word r_info;
Elf32_Word r_type;
Which data type is used for the 'r_info' field in the Elf64_Rel struct?
Elf32_Word
Elf64_Xword
Elf32_Sword
Elf64_Addr
Fill in the blank: The struct Elf64_Rela contains the fields Elf64_Addr r_offset, Elf64_Xword r_info, and ______.
Elf64_Sxword r_addend;
Elf64_Word r_size;
Elf64_Addr r_base;
Elf64_Half r_type;
Fill in the blank: The relocation algorithm handles two types of references: ________ and absolute references.
PC-relative
Stack-relative
Base-relative
Index-relative
Which of the following is used to relocate an absolute reference?
A) *refptr = (unsigned) (ADDR(r.symbol) + r.addend - refaddr);
B) *refptr = (unsigned) (ADDR(r.symbol) + r.addend);
C) *refptr = (unsigned) (ADDR(r.symbol) - r.addend);
D) *refptr = (unsigned) (ADDR(r.symbol) + r.offset);
Consider the following C code: int array[2] = {1, 2}; int main() { int val = sum(array, 2); return val; } Which function is called in the main function?
add()
sum()
main()
return()
Refer to the assembly output below for the main function:
0000000000000000
A) To relocate the address of the 'sum' function
B) To relocate the address of the 'array' variable
C) To relocate the value of %esi
D) To relocate the return value
Fill in the blank: In the assembly output, the instruction 'callq 13
sum()
printf()
scanf()
main()
Using PC-relative addressing for sum(): 0x4004e8 = 0x4004e3 + _____. Fill in the blank with the correct value.
0x5
0x3
0xA
0x7
In the given assembly code, which instruction is used to call the sum() function?
mov
callq
add
jmp
What is the hexadecimal address of the sum() function as shown in the relocated .text section?
0x4004d0
0x4004e3
0x4004e8
0x4004fd
Which section of the Executable Object File contains the program's instructions?
.data section
.text section
.bss section
.symtab
Fill in the blank: The ______ section in the Executable Object File contains uninitialized data.
.bss section
.data section
.text section
.init section
Which segment in the memory layout is created by malloc at run-time?
User stack
Run-time heap
Read-only code segment
Memory-mapped region for shared libraries
Fill in the blank: The ______ segment in the memory layout is loaded from the executable file and contains .data and .bss sections.
Read/write data segment
Stack segment
Text segment
Heap segment
The .rodata section in the Executable Object File contains read-only data.
True
False
