![]() |
Baseer 0.2.0
Baseer is an advanced binary analysis tool designed to provide deep insights into any file.
|
Implementation of ELF parsing and display utilities. More...
#include "bx_elf_utils.h"
Functions | |
void | print_section_header_legend (void) |
Print legends for ELF section header types and flags with color highlighting. | |
void | print_program_header_legend (void) |
Print legends for ELF program header types and flags with color highlighting. | |
const char * | elf_machine_to_str (unsigned int machine) |
Convert ELF e_machine value to human-readable string. | |
const char * | sh_type_to_str (unsigned int sh_type) |
Converts an ELF section header type to a human-readable string with color. | |
const char * | elf_type_to_str (unsigned int type) |
Converts an ELF file type to a human-readable string. | |
const char * | type_p_to_str (unsigned int p_type) |
Converts a program header type (p_type) to a human-readable string with color. | |
void | display_byte (const unsigned char *byte) |
Display a single byte in hexadecimal with color coding. | |
void | display_byte_char (const unsigned char *byte) |
Display a single byte as a printable character with color coding. | |
int | is_in_list (const char *word, const char *list[]) |
Check if a word exists in a null-terminated string list. | |
int | is_number (const char *word) |
Check if a string represents a numeric value. | |
const char * | get_color (const char *word) |
Get the ANSI color code for a given assembly token. | |
void | print_highlight_asm (const char *line) |
Print an assembly instruction line with syntax highlighting. | |
void | format_sh_flags (uint64_t sh_flags, char *buf, size_t size) |
Format ELF section header flags into a colored string. | |
void | print_symbols_32bit (bparser *parser, Elf32_Ehdr *elf, Elf32_Shdr *shdrs, Elf32_Shdr *symtab, Elf32_Shdr *strtab) |
Print all symbols from a 32-bit ELF file (with colors). | |
void | print_symbols_64bit (bparser *parser, Elf64_Ehdr *elf, Elf64_Shdr *shdrs, Elf64_Shdr *symtab, Elf64_Shdr *strtab) |
Print all symbols from a 64-bit ELF file (with colors). | |
void | print_symbols_with_disasm_32bit (bparser *parser, Elf32_Ehdr *elf, Elf32_Shdr *shdrs, Elf32_Shdr *symtab, Elf32_Shdr *strtab) |
Print ELF32 symbols along with disassembly for functions. | |
void | print_symbols_with_disasm_64bit (bparser *parser, Elf64_Ehdr *elf, Elf64_Shdr *shdrs, Elf64_Shdr *symtab, Elf64_Shdr *strtab) |
Print ELF64 symbols along with disassembly for functions. | |
void | print_hex_header (unsigned long long offset) |
Print the hex dump header row. | |
void | print_section_header_metadata_32bit (unsigned int id, const char *name, const char *type_str, const char *flags, Elf32_Shdr *shdrs) |
Print metadata of a 32-bit ELF section header. | |
void | print_section_header_metadata_64bit (unsigned int id, const char *name, const char *type_str, const char *flags, Elf64_Shdr *shdrs) |
Print metadata of a 64-bit ELF section header. | |
void | print_program_header_metadata_32bit (unsigned int id, const char *type_str, const char *flags, Elf32_Phdr *phdr) |
Print metadata of a 32-bit ELF program header (segment). | |
void | print_program_header_metadata_64bit (unsigned int id, const char *type_str, const char *flags, Elf64_Phdr *phdr) |
Print metadata of a 64-bit ELF program header (segment). | |
void | print_body_bytes (unsigned char *ptr, size_t size, unsigned long long offset, int disasm, unsigned char bit_type) |
Print a block of bytes in hex and ASCII format, optionally disassembling executable instructions. | |
void | print_disasm (unsigned char *ptr, size_t size, unsigned long long offset, unsigned char bit_type) |
Disassemble a block of machine code and print the assembly with highlighting. | |
void | format_p_flags (uint32_t p_flags, char *buf, size_t size) |
Format ELF program header flags into a colored string. |
Implementation of ELF parsing and display utilities.
This file provides functions for:
It supports both 32-bit and 64-bit ELF binaries and uses ANSI color codes to enhance terminal output readability.
void display_byte | ( | const unsigned char * | byte | ) |
Display a single byte in hexadecimal with color coding.
This function prints the value of a byte in hex format (two digits) and applies a color depending on its meaning:
byte | Pointer to the byte to display. |
void display_byte_char | ( | const unsigned char * | byte | ) |
Display a single byte as a printable character with color coding.
This function prints the ASCII representation of a byte. Non-printable bytes are shown as a '.' character. The output is color-coded based on the byte value:
Printable ASCII characters (32-126) are displayed as-is, while non-printable bytes are represented with '.'.
byte | Pointer to the byte to display. |
const char * elf_machine_to_str | ( | unsigned int | machine | ) |
Convert ELF e_machine value to human-readable string.
This function takes an ELF machine type (e_machine) from the ELF header and returns a descriptive string representing the target architecture.
machine | The e_machine field from the ELF header. |
Example:
const char * elf_type_to_str | ( | unsigned int | type | ) |
Converts an ELF file type to a human-readable string.
This function maps ELF file type constants (ET_*) to descriptive strings. Examples include relocatable files, executables, shared objects, and core files.
type | The ELF file type (e.g., ET_REL, ET_EXEC, ET_DYN, etc.). |
void format_p_flags | ( | uint32_t | p_flags, |
char * | buf, | ||
size_t | size ) |
Format ELF program header flags into a colored string.
This function converts the p_flags field of an ELF program header into a human-readable string with color highlighting for readability:
p_flags | The flags from the ELF program header (p_flags field of Elf32_Phdr or Elf64_Phdr). |
buf | Output buffer to store the formatted string. Must be preallocated. |
size | Size of the output buffer. |
Example usage:
void format_sh_flags | ( | uint64_t | sh_flags, |
char * | buf, | ||
size_t | size ) |
Format ELF section header flags into a colored string.
sh_flags | Section header flags (bitmask). |
buf | Buffer to write formatted flags into. |
size | Size of the buffer. |
const char * get_color | ( | const char * | word | ) |
Get the ANSI color code for a given assembly token.
This function determines the color that should be used to print a single word/token from an assembly instruction or line. It checks the token against several categories such as numbers, registers, opcodes, instruction types, and invalid/unknown tokens.
The color codes are defined by macros like COLOR_RED, COLOR_GREEN, etc.
word | A null-terminated string representing the token to check. |
int is_in_list | ( | const char * | word, |
const char * | list[] ) |
Check if a word exists in a null-terminated string list.
This function searches for an exact match of the given word in the provided list of strings. The list must be null-terminated.
word | The null-terminated string to search for. |
list | A null-terminated array of strings. |
int is_number | ( | const char * | word | ) |
Check if a string represents a numeric value.
This function determines whether the given string word is a number. It supports:
word | A null-terminated string to check. |
void print_body_bytes | ( | unsigned char * | ptr, |
size_t | size, | ||
unsigned long long | offset, | ||
int | disasm, | ||
unsigned char | bit_type ) |
Print a block of bytes in hex and ASCII format, optionally disassembling executable instructions.
This function prints the content of a memory block in a traditional hex dump format, showing the offset, hexadecimal bytes, and ASCII representation. If the block contains executable instructions (SHF_EXECINSTR flag is set), it also disassembles the instructions using the Udis86 library and highlights the assembly.
ptr | Pointer to the memory block to print. |
size | Number of bytes to print from the memory block. |
offset | Starting offset to display in the hex dump. |
disasm | If non-zero and contains SHF_EXECINSTR, the function disassembles the bytes. |
bit_type | ELF class: ELFCLASS32 for 32-bit, ELFCLASS64 for 64-bit. |
Example output:
void print_disasm | ( | unsigned char * | ptr, |
size_t | size, | ||
unsigned long long | offset, | ||
unsigned char | bit_type ) |
Disassemble a block of machine code and print the assembly with highlighting.
This function uses the Udis86 library to disassemble a memory block and prints each instruction in Intel syntax. The output includes offsets and color highlighting for opcodes, registers, and addresses.
ptr | Pointer to the memory block containing machine code. |
size | Number of bytes to disassemble. |
offset | Starting address to display in the disassembly output. |
bit_type | ELF class: ELFCLASS32 for 32-bit instructions, ELFCLASS64 for 64-bit instructions. |
Example output:
void print_hex_header | ( | unsigned long long | offset | ) |
Print the hex dump header row.
This function prints the header line used in a hex dump, showing both the hexadecimal column labels (00–0F) and the ASCII column labels. It also highlights the header with green color using ANSI escape codes.
Example output (simplified):
offset | Starting offset for the hex dump (not currently used in the header itself, but included for consistency). |
void print_highlight_asm | ( | const char * | line | ) |
Print an assembly instruction line with syntax highlighting.
This function scans through a single line of assembly code and applies syntax highlighting (using ANSI color codes) to recognized tokens such as opcodes, registers, numbers, etc. Highlighting is determined by the helper function get_color().
Tokens are split based on whitespace and punctuation characters, but punctuation itself (commas, brackets, colons, etc.) is preserved and printed without coloring.
Example:
line | A null-terminated string containing the assembly instruction line to highlight. If NULL, the function returns immediately. |
void print_program_header_legend | ( | void | ) |
Print legends for ELF program header types and flags with color highlighting.
This function prints two tables to the console:
Each entry is highlighted using ANSI color codes for better readability.
Example output (truncated):
void print_program_header_metadata_32bit | ( | unsigned int | id, |
const char * | type_str, | ||
const char * | flags, | ||
Elf32_Phdr * | phdr ) |
Print metadata of a 32-bit ELF program header (segment).
This function prints detailed information about a single ELF32 program segment, including type, flags, offset, virtual/physical addresses, file/memory size, and alignment. It uses ANSI color codes for visual distinction.
id | The index of the program header in the program header table. |
type_str | A string representing the segment type (e.g., "PT_LOAD"). |
flags | A string representing the segment flags (e.g., "R E" for readable + executable). |
phdr | Pointer to the array of ELF32 program headers (Elf32_Phdr[]). |
Example output:
void print_program_header_metadata_64bit | ( | unsigned int | id, |
const char * | type_str, | ||
const char * | flags, | ||
Elf64_Phdr * | phdr ) |
Print metadata of a 64-bit ELF program header (segment).
This function prints detailed information about a single ELF64 program segment, including type, flags, offset, virtual/physical addresses, file/memory size, and alignment. It uses ANSI color codes for visual distinction.
id | The index of the program header in the program header table. |
type_str | A string representing the segment type (e.g., "PT_LOAD"). |
flags | A string representing the segment flags (e.g., "R E" for readable + executable). |
phdr | Pointer to the array of ELF64 program headers (Elf64_Phdr[]). |
Example output:
void print_section_header_legend | ( | void | ) |
Print legends for ELF section header types and flags with color highlighting.
This function prints two tables to the console:
Each entry is highlighted using ANSI color codes for better readability.
Example output (truncated):
void print_section_header_metadata_32bit | ( | unsigned int | id, |
const char * | name, | ||
const char * | type_str, | ||
const char * | flags, | ||
Elf32_Shdr * | shdrs ) |
Print metadata of a 32-bit ELF section header.
This function prints detailed information about a single ELF32 section, including type, flags, address, offset, size, link, info, alignment, and entry size. It uses ANSI color codes for visual distinction.
id | The index of the section in the section header table. |
name | The name of the section. |
type_str | A string representing the section type (e.g., "SHT_PROGBITS"). |
flags | A string representing the section flags (e.g., "AX" for alloc+execute). |
shdrs | Pointer to the array of ELF32 section headers (Elf32_Shdr[]). |
Example output:
void print_section_header_metadata_64bit | ( | unsigned int | id, |
const char * | name, | ||
const char * | type_str, | ||
const char * | flags, | ||
Elf64_Shdr * | shdrs ) |
Print metadata of a 64-bit ELF section header.
This function prints detailed information about a single ELF64 section, including type, flags, address, offset, size, link, info, alignment, and entry size. It uses ANSI color codes for visual distinction.
id | The index of the section in the section header table. |
name | The name of the section. |
type_str | A string representing the section type (e.g., "SHT_PROGBITS"). |
flags | A string representing the section flags (e.g., "AX" for alloc+execute). |
shdrs | Pointer to the array of ELF64 section headers (Elf64_Shdr[]). |
Example output:
void print_symbols_32bit | ( | bparser * | parser, |
Elf32_Ehdr * | elf, | ||
Elf32_Shdr * | shdrs, | ||
Elf32_Shdr * | symtab, | ||
Elf32_Shdr * | strtab ) |
Print all symbols from a 32-bit ELF file (with colors).
This function iterates over the ELF32 symbol table (symtab) and its associated string table (strtab) to display information about each symbol. It prints a nicely formatted and colorized table that includes:
parser | Pointer to the binary parser context (holds loaded ELF data in memory). |
elf | Pointer to the ELF32 header structure. |
shdrs | Pointer to the array of section headers in the ELF file. |
symtab | Pointer to the section header for the symbol table (.symtab). |
strtab | Pointer to the section header for the associated string table (.strtab). |
void print_symbols_64bit | ( | bparser * | parser, |
Elf64_Ehdr * | elf, | ||
Elf64_Shdr * | shdrs, | ||
Elf64_Shdr * | symtab, | ||
Elf64_Shdr * | strtab ) |
Print all symbols from a 64-bit ELF file (with colors).
This function iterates over the ELF64 symbol table (symtab) and its associated string table (strtab) to display information about each symbol. It prints a formatted and colorized table including:
parser | Pointer to the binary parser context (holds loaded ELF data in memory). |
elf | Pointer to the ELF64 header structure. |
shdrs | Pointer to the array of section headers in the ELF file. |
symtab | Pointer to the section header for the symbol table (.symtab). |
strtab | Pointer to the section header for the associated string table (.strtab). |
void print_symbols_with_disasm_32bit | ( | bparser * | parser, |
Elf32_Ehdr * | elf, | ||
Elf32_Shdr * | shdrs, | ||
Elf32_Shdr * | symtab, | ||
Elf32_Shdr * | strtab ) |
Print ELF32 symbols along with disassembly for functions.
This function iterates over the symbol table of a 32-bit ELF file, printing information about each symbol and disassembling function symbols. It highlights the type of each symbol using ANSI color codes.
parser | Pointer to a bparser structure containing the loaded ELF data. |
elf | Pointer to the ELF32 file header (Elf32_Ehdr). |
shdrs | Pointer to the section header array (Elf32_Shdr[]). |
symtab | Pointer to the section header of the symbol table (Elf32_Shdr). |
strtab | Pointer to the section header of the associated string table (Elf32_Shdr). |
Example output:
void print_symbols_with_disasm_64bit | ( | bparser * | parser, |
Elf64_Ehdr * | elf, | ||
Elf64_Shdr * | shdrs, | ||
Elf64_Shdr * | symtab, | ||
Elf64_Shdr * | strtab ) |
Print ELF64 symbols along with disassembly for functions.
This function iterates over the symbol table of a 64-bit ELF file, printing information about each symbol and disassembling function symbols. It highlights the type of each symbol using ANSI color codes.
parser | Pointer to a bparser structure containing the loaded ELF data. |
elf | Pointer to the ELF64 file header (Elf64_Ehdr). |
shdrs | Pointer to the section header array (Elf64_Shdr[]). |
symtab | Pointer to the section header of the symbol table (Elf64_Shdr). |
strtab | Pointer to the section header of the associated string table (Elf64_Shdr). |
Example output:
const char * sh_type_to_str | ( | unsigned int | sh_type | ) |
Converts an ELF section header type to a human-readable string with color.
This function maps ELF section header type constants (SHT_*) to descriptive strings. ANSI color codes are added for colored output in terminal.
sh_type | The section header type (e.g., SHT_PROGBITS, SHT_SYMTAB, ...). |
const char * type_p_to_str | ( | unsigned int | p_type | ) |
Converts a program header type (p_type) to a human-readable string with color.
This function maps ELF program header type constants (PT_*) to descriptive strings, optionally including ANSI color codes for terminal highlighting.
p_type | The program header type (e.g., PT_LOAD, PT_DYNAMIC, PT_INTERP, etc.). |