LF OS
Hobby operating system for amd64 with high ambitions
Loading...
Searching...
No Matches
elf.c File Reference

(7c4d7d1 userspace/term: change eyecandy inclusion to use objcopy instead of xxd)

#include "elf.h"
#include "log.h"
#include "string.h"
#include "mm.h"
#include "vm.h"
+ Include dependency graph for elf.c:

Go to the source code of this file.

Data Structures

struct  Symbol
 
struct  SymbolData
 

Functions

ptr_t load_elf (ptr_t start, struct vm_table *context, ptr_t *data_start, ptr_t *data_end)
 
elf_section_header_telf_section_by_name (const char *name, const void *elf)
 
void * elf_load_symbols (ptr_t elf, allocator_t *alloc)
 
bool elf_symbolize (void *symbol_data, ptr_t addr, size_t *symbol_size, char *symbol)
 

Data Structure Documentation

◆ Symbol

struct Symbol

Definition at line 89 of file elf.c.

+ Collaboration diagram for Symbol:
Data Fields
uint64_t address
uint32_t name

◆ SymbolData

struct SymbolData

Definition at line 94 of file elf.c.

+ Collaboration diagram for SymbolData:
Data Fields
allocator_t * alloc
uint64_t numSymbols
char * symbolNames
struct Symbol symbols[0]

Function Documentation

◆ elf_load_symbols()

void * elf_load_symbols ( ptr_t  elf,
allocator_t alloc 
)

Load symbols of the provided ELF file.

Parameters
elfELF file already loaded into memory
allocAllocator to use for dynamically allocating memory
Returns
Pointer to a opaque data structure containing the symbols, use elf_symbolize to retrieve the symbol for a given address

Definition at line 101 of file elf.c.

References elf_symbol::addr, Symbol::address, allocator::alloc, alloc, elf_section_by_name(), elf_section_header::entrySize, header, logw, memcpy(), Symbol::name, elf_symbol::name, SymbolData::numSymbols, elf_section_header::offset, and elf_section_header::size.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ elf_section_by_name()

elf_section_header_t * elf_section_by_name ( const char *  name,
const void *  elf 
)

Return section header of ELF file by name

Parameters
nameName of the section to return
elfLoaded ELF image in memory
Returns
Pointer to elf_section_header_t if found, NULL otherwise

Definition at line 71 of file elf.c.

References name, elf_section_header::name, elf_section_header::offset, elf_file_header::sectionHeaderCount, elf_file_header::sectionHeaderEntrySize, elf_file_header::sectionHeaderOffset, elf_file_header::sectionHeaderSectionNameIndex, and strcmp().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ elf_symbolize()

bool elf_symbolize ( void *  symbols,
ptr_t  addr,
size_t symbol_length,
char *  symbol 
)

Get the symbol name (and offset) for a given address from the data loaded with elf_load_symbols.

Parameters
symbolsOpaque data structure returned by elf_load_symbols
addrAddress to symbolize
symbol_lengthInput for size of the buffer where to write symbol, output actual size of either the data written into the buffer or the size needed for the buffer
symbolOutput buffer where to write the symbol
Returns
true if a symbol was found and the output buffer had enough space, false if no symbol was found or output buffer was too small. Output buffer to small can be recognized by symbol_length being set to a non-zero value.

Definition at line 133 of file elf.c.

References addr, Symbol::address, ksnprintf(), Symbol::name, SymbolData::numSymbols, strlen(), strncpy(), SymbolData::symbolNames, and SymbolData::symbols.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ load_elf()

ptr_t load_elf ( ptr_t  elf,
struct vm_table context,
ptr_t data_start,
ptr_t data_end 
)

Parse ELF file, map program segments to context and return proper location for stack.

Parameters
[in]elfMemory chunk where the ELF file is loaded.
[in]contextVM context for mapping of program segments.
[out]data_startStart of the data section
[out]data_endEnd of the data section
Returns
Entrypoint of the image

Definition at line 7 of file elf.c.

References ALLOCATOR_REGION_DIRECT_MAPPING, data_end, ELF_MAGIC, fileLength, elf_program_header::fileLength, header, logf, memcpy(), elf_program_header::memLength, memset(), mm_alloc_pages(), offset, elf_program_header::offset, elf_program_header::type, elf_program_header::vaddr, and vm_context_map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: