CodeBlanks Logo Icon CodeBlanks

Summary - Linker

Combine object files, relocation and ELF to write a symbol viewer.

Summary - Symbol viewer

In this lesson you'll combine object files, relocation and ELF to write a symbol viewer. The program defines three functions, prints each one's name, gives the total count, and finally calls one to show its result.

Each function is a symbol: the linker records them in the symbol table. Printing the function names is like listing the symbol table contents. The call shows symbol resolution and relocation logic at run time. This exercise merges the linker topics in one program.

Practice Write a symbol viewer

Define three functions: int add(int a, int b) { return a + b; }, int sub(int a, int b) { return a - b; }, int mul(int a, int b) { return a * b; }. Write int main(void); print each function's name as printf("Sembol: add\n") (for all three), then call printf("Toplam: 3\n") and printf("Çağrı: %d\n", add(40, 2)).

Starter code

main.c
#include <stdio.h>

// üç fonksiyon tanımla: add, sub, mul
// her birinin adını yazdır
// toplam sayıyı yazdır
// birini çağır ve sonucu yazdır

Sample solution

main.c
#include <stdio.h>

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }

int main(void) {
    printf("Sembol: add\n");
    printf("Sembol: sub\n");
    printf("Sembol: mul\n");
    printf("Toplam: 3\n");
    printf("Çağrı: %d\n", add(40, 2));
    return 0;
}

Expected output

Sembol: add
Sembol: sub
Sembol: mul
Toplam: 3
Çağrı: 42

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does a symbol viewer show?

  • Only error codes
  • Only memory addresses
  • Only compiler versions
  • Function and variable names Correct answer

Explanation: A symbol viewer lists names from the symbol table.

2 Complete the function call

Question
int add(int a, int b) { return a + b; }
int result = add(40, ____);
Answer
int add(int a, int b) { return a + b; }
int result = add(40, 2);

Explanation: answer: 2. add(40, 2) gives 42.

3 Match concept to role

Term Meaning
Object file Relocatable
Symbol Name and address
Relocation Address patching
ELF File format

All lessons in Expert