# martinribelotta/elfloader architecture

> Architecture diagram of the GitHub repository martinribelotta/elfloader, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/martinribelotta/elfloader
- Repository: https://github.com/martinribelotta/elfloader

## Overview

##### And Label Main Components
- Core ELF Loader Module:
- Main ELF loading engine (loader.c/loader.h).
- Exported API functions: load_elf, jumpTo, get_func, get_obj, and unload_elf.
- Configuration & Abstraction Layer:
- File handling macros (LOADER_OPEN_FOR_RD, LOADER_READ, LOADER_SEEK_FROM_START, etc.).
- Memory management macros (LOADER_ALIGN_ALLOC, LOADER_FREE, etc.).
- Code execution macro (LOADER_JUMP_TO) and debugging utilities (DBG, ERR, MSG).
- ARM Support Module:
- ARM layer which includes the CMSIS directory, startup files (startup_ARMCM4.S), and the fault handling (fault.c).
- This module is critical as it underpins proper initialization and execution in the ARM embedded context.
- Application/Demo Modules:
- These modules serve as usage examples that interact with the ELF Loader Core.
- Linker Scripts & Build Configuration:
- Although not a runtime module, include a section for ldscripts (gcc.ld, mem.ld, etc.) which influence memory layout and linking.
- Mention the Makefiles present in various folders as part of the build process.

##### Relationships and Data Flow
- Interactions from the Loader Core to the Configuration & Abstraction Layer:
- That file I/O and memory allocation functions are decoupled via macros, abstracting the hardware-specific details.
- Dependency of the Loader Core on the ARM Support Module:
- Link the Loader Core with the ARM-specific startup and CMSIS code that ensure proper execution in a bare-metal environment.
- Optionally, denote that the linker scripts guide how different sections (such as .sdram_data, .sdram_rodata, .sdram_bss) are handled during the load process.

##### Architectural Patterns and Design Principles
- Modular Design:
- The core loader functionality is separated from configuration, hardware, and demo applications.
- Abstraction of Low-level Operations:
- Through macros and configuration definitions, file I/O, memory management, and code jumping are abstracted to work with various environments.
- Separation of Concerns:
- Distinct modules for execution (core loader), hardware initialization (ARM CMSIS, startup), and demonstration/testing (applications) are clearly shown.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Core Modules"
        loaderCore["ELF Loader Core\n(load_elf, jumpTo, get_func, get_obj, unload_elf)"]:::core
    end

    subgraph "Configuration & Abstraction"
        configAb["Configuration & Abstraction Layer\n(LOADER_OPEN_FOR_RD, LOADER_READ, LOADER_SEEK_FROM_START, LOADER_ALIGN_ALLOC, LOADER_FREE, LOADER_JUMP_TO, DBG, ERR, MSG)"]:::config
    end

    subgraph "ARM Support Module"
        armStartup["ARM Startup & Fault Handling\n(startup_ARMCM4.S, fault.c)"]:::arm
        arnCMSIS["CMSIS Libraries"]:::arm
    end

    subgraph "Application/Demo Modules"
        appC["C Application Example"]:::app
        appCpp["C++ Application Example"]:::app
        hostDemo["Host-based Loader Testing"]:::app
    end

    subgraph "Linker Scripts & Build Config"
        linkerNode["Linker Scripts & Build Files\n(gcc.ld, mem.ld, Makefiles)"]:::linker
    end

    appC -->|"calls_API"| loaderCore
    appCpp -->|"calls_API"| loaderCore
    hostDemo -->|"calls_API"| loaderCore

    loaderCore -->|"uses_macros"| configAb
    loaderCore -->|"depends_on"| armStartup
    loaderCore -->|"initializes_via"| arnCMSIS
    loaderCore -->|"linked_by"| linkerNode

    classDef core fill:#90CAF9,stroke:#1E88E5,stroke-width:2px;
    classDef config fill:#A5D6A7,stroke:#43A047,stroke-width:2px;
    classDef arm fill:#EF9A9A,stroke:#E53935,stroke-width:2px;
    classDef app fill:#FFF59D,stroke:#FBC02D,stroke-width:2px;
    classDef linker fill:#CE93D8,stroke:#8E24AA,stroke-width:2px;

    click loaderCore "https://github.com/martinribelotta/elfloader/blob/master/loader.c"
    click configAb "https://github.com/martinribelotta/elfloader/blob/master/host/loader_config.h"
    click armStartup "https://github.com/martinribelotta/elfloader/blob/master/arm/startup_ARMCM4.S"
    click arnCMSIS "https://github.com/martinribelotta/elfloader/tree/master/arm/CMSIS"
    click appC "https://github.com/martinribelotta/elfloader/blob/master/app/main.c"
    click appCpp "https://github.com/martinribelotta/elfloader/blob/master/app-cpp/main.cpp"
    click hostDemo "https://github.com/martinribelotta/elfloader/blob/master/host/main.c"
    click linkerNode "https://github.com/martinribelotta/elfloader/blob/master/ldscripts/gcc.ld"
```
