cybersecurity · intermediate · ~15 min · safe pentest lab

Count global symbols in an ELF .symtab buffer

Strided binary walk with a bitwise nibble extraction.

Challenge

Count the global symbols in an ELF .symtab buffer — a one-glance signal of how stripped a binary is, and the first thing a reverse-engineer looks at.

Task

Implement int count_global_symbols(const uint8_t *buf, size_t n) that returns how many symbol-table entries have GLOBAL binding.

Input

  • buf, n: a fixture byte buffer shaped like a .symtab, and its length, baked into the harness. Each Elf64_Sym entry is 24 bytes; the binding is the top 4 bits of st_info, which sits at offset 4 within each entry. Binding 1 = GLOBAL.

Output

Returns int:

  • the count of entries with (st_info >> 4) == 1 on success;
  • 0 if buf == NULL and n == 0 (empty);
  • -1 if buf == NULL and n > 0 (corrupt);
  • -1 if n % 24 != 0 (corrupt).

Example

3 entries, two with binding nibble 1   ->   2
NULL, n = 0                            ->   0
n = 25 (not a multiple of 24)          ->   -1

Edge cases

  • An empty table (NULL/0) returns 0, not -1.
  • n must be a multiple of 24.

Rules

  • No struct cast — read buf[i*24 + 4] directly (alignment-safe).
  • The binding is the top nibble; do not confuse it with the type (low nibble).

Why this matters

The symbol table is the first place a reverse-engineer looks. A counter of global symbols is a one-glance signal of how stripped a binary is.

Input format

A const byte buffer + its length in bytes.

Output format

Non-negative count, or -1 on corruption / invalid input.

Constraints

No alignment-unsafe casts. n must be a multiple of 24.

Starter code

#include <stdint.h>
#include <stddef.h>
int count_global_symbols(const uint8_t *buf, size_t n) {
    /* TODO */
    (void)buf; (void)n;
    return -1;
}

Common mistakes

Confusing binding and type. Forgetting the n % 24 check. Casting (Elf64_Sym *)buf and tripping on alignment.

Edge cases to handle

Empty input. Single entry. All entries WEAK.

Complexity

O(n) per byte; O(n/24) per entry.

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.