Home Learning Paths ECU Lab Assessments Interview Preparation Arena Pricing Log In Sign Up

Reading a Linker Map File

Textlinker_map.txt
# Excerpt from a GCC linker .map file for Aurix TC397
# Generated by: gcc -Wl,-Map=output.map

# Memory regions (from linker script)
Memory Configuration

Name             Origin             Length             Attributes
PFLASH           0x0080000000       0x00800000         xr    # 8 MB flash
LMU_RAM          0x0070000000       0x00100000         xrw   # 1 MB shared RAM
DSPR0            0x0050000000       0x00040000         xrw   # 256 kB core-local

# Section layout: .text (code) in PFlash
.text            0x0080000100  0x0001A234
 .text.Can_Init  0x0080000100      0x0084  build/can.o
 .text.Adc_Read  0x0080000184      0x0030  build/adc.o
 .text.App_Main  0x00800001B4      0x01C0  build/app.o

# .bss (uninitialised data) in LMU_RAM
.bss             0x0070001000  0x00002800
 .bss.g_canBuf   0x0070001000      0x0400  build/can.o
 g_canBuf        0x0070001000
 .bss.g_adcData  0x0070001400      0x0100  build/adc.o

# Key: look for unusually large symbols; check for unexpected sections

# Useful grep patterns:
# grep -E '^\s+0x[0-9a-f]+\s+0x[0-9a-f]{4,}' output.map  # large allocations
# grep 'fill' output.map                                    # padding/fill gaps

ELF Section Analysis with nm and objdump

Bashelf_analysis.sh
#!/bin/bash
# Analyse ELF file for memory usage and symbol sizes

ELF="build/app.elf"

# 1. Section sizes: .text (flash), .data (flash+RAM), .bss (RAM only)
echo "=== Section sizes ==="
arm-none-eabi-size $ELF
# Output: text   data    bss    dec    hex
#         98304   1024   4096  103424  193C0

# 2. Top 20 largest symbols by size
echo "=== Top 20 largest symbols ==="
arm-none-eabi-nm --print-size --size-sort --radix=d $ELF | tail -20
# T = text (code); D = data; B = bss; R = rodata (flash const)

# 3. Find all global (non-static) variables — potential MISRA Rule 8.7 violations
echo "=== External symbols (check for unnecessary globals) ==="
arm-none-eabi-nm $ELF | grep ' [BD] ' | sort

# 4. Check for sections not in linker script (unplaced sections → may go to default region)
echo "=== All sections ==="
arm-none-eabi-objdump -h $ELF | grep -E '^\s+[0-9]'

# 5. Disassemble a specific function
echo "=== Disassemble Can_Init ==="
arm-none-eabi-objdump -d --source $ELF | grep -A 30 ":

Exercise 1: Identify Struct Padding Waste

Cpadding_exercise.c
/* Exercise: calculate sizeof each struct; find the one with most padding */
/* Then reorder to minimise padding */
#include 
#include 

/* Struct A: original order — calculate sizeof */
typedef struct {
    uint8_t  flags;       /* 1 byte */
    uint32_t timestamp;   /* 4 bytes — but needs 4-byte alignment */
    uint8_t  channel;     /* 1 byte */
    uint16_t value;       /* 2 bytes — needs 2-byte alignment */
    uint8_t  checksum;    /* 1 byte */
} EventRecord_A_t;
/* sizeof = ? (answer: 1 + 3pad + 4 + 1 + 1pad + 2 + 1 + 3pad = 16) */

/* Struct B: reordered for minimal padding */
typedef struct {
    uint32_t timestamp;   /* 4 bytes, offset 0 */
    uint16_t value;       /* 2 bytes, offset 4 */
    uint8_t  flags;       /* 1 byte,  offset 6 */
    uint8_t  channel;     /* 1 byte,  offset 7 */
    uint8_t  checksum;    /* 1 byte,  offset 8 */
    /* 3 bytes padding to align to 4 */
} EventRecord_B_t;
/* sizeof = ? (answer: 4 + 2 + 1 + 1 + 1 + 3pad = 12) — saved 4 bytes */

_Static_assert(sizeof(EventRecord_A_t) == 16u, "A should be 16");
_Static_assert(sizeof(EventRecord_B_t) == 12u, "B should be 12");
/* Saving 4 bytes × 1024 event log entries = 4 kB RAM saved */

Exercise 2: Stack Usage Analysis

Bashstack_analysis.sh
#!/bin/bash
# Analyse stack usage per function using GCC -fstack-usage flag
# Compile with: gcc -fstack-usage source.c

# 1. Build with stack usage reporting
arm-none-eabi-gcc -O2 -fstack-usage -c app.c -o app.o
# Generates: app.su file with per-function stack frame sizes

# 2. Parse .su files: format is "file:line:col function_name frame_size type"
# type: static (known at compile time), dynamic (variable-length arrays), unbounded
echo "=== Functions with largest stack frames ==="
sort -k3 -rn *.su | head -20
# Example output:
# app.c:45:12 ParseCanMessage 128 static
# can.c:22:8  Can_Receive      64 static
# app.c:88:4  ProcessSensor    256 dynamic  <-- WARNING: VLA or alloca

# 3. Generate call-graph stack analysis with GCC -Wstack-usage
arm-none-eabi-gcc -Wstack-usage=256 app.c  # warn if frame > 256 bytes

# 4. Total stack depth: requires call-graph traversal
# Tool: arm-none-eabi-gcc -fdump-rtl-expand + custom analysis script
# Commercial: LDRA, Polyspace (for certified WCSS calculation)

Summary

Map file analysis and arm-none-eabi-nm/objdump are the primary tools for understanding where code and data actually end up in flash and RAM. The most common findings: unexpectedly large BSS sections (global arrays that should be local), struct padding wasting RAM (fix by reordering fields), and uninitialised globals that should be stack-allocated. Stack usage analysis with -fstack-usage is essential for demonstrating stack safety — ASIL-B/C/D require a demonstrated stack usage ceiling with a defined safety margin.

🔬 Deep Dive — Core Concepts Expanded

This section builds on the foundational concepts covered above with additional technical depth, edge cases, and configuration nuances that separate competent engineers from experts. When working on production ECU projects, the details covered here are the ones most commonly responsible for integration delays and late-phase defects.

Key principles to reinforce:

  • Configuration over coding: In AUTOSAR and automotive middleware environments, correctness is largely determined by ARXML configuration, not application code. A correctly implemented algorithm can produce wrong results due to a single misconfigured parameter.
  • Traceability as a first-class concern: Every configuration decision should be traceable to a requirement, safety goal, or architecture decision. Undocumented configuration choices are a common source of regression defects when ECUs are updated.
  • Cross-module dependencies: In tightly integrated automotive software stacks, changing one module's configuration often requires corresponding updates in dependent modules. Always perform a dependency impact analysis before submitting configuration changes.

🏭 How This Topic Appears in Production Projects

  • Project integration phase: The concepts covered in this lesson are most commonly encountered during ECU integration testing — when multiple software components from different teams are combined for the first time. Issues that were invisible in unit tests frequently surface at this stage.
  • Supplier/OEM interface: This is a topic that frequently appears in technical discussions between Tier-1 ECU suppliers and OEM system integrators. Engineers who can speak fluently about these details earn credibility and are often brought into critical design review meetings.
  • Automotive tool ecosystem: Vector CANoe/CANalyzer, dSPACE tools, and ETAS INCA are the standard tools used to validate and measure the correct behaviour of the systems described in this lesson. Familiarity with these tools alongside the conceptual knowledge dramatically accelerates debugging in real projects.

⚠️ Common Mistakes and How to Avoid Them

  1. Assuming default configuration is correct: Automotive software tools ship with default configurations that are designed to compile and link, not to meet project-specific requirements. Every configuration parameter needs to be consciously set. 'It compiled' is not the same as 'it is correctly configured'.
  2. Skipping documentation of configuration rationale: In a 3-year ECU project with team turnover, undocumented configuration choices become tribal knowledge that disappears when engineers leave. Document why a parameter is set to a specific value, not just what it is set to.
  3. Testing only the happy path: Automotive ECUs must behave correctly under fault conditions, voltage variations, and communication errors. Always test the error handling paths as rigorously as the nominal operation. Many production escapes originate in untested error branches.
  4. Version mismatches between teams: In a multi-team project, the BSW team, SWC team, and system integration team may use different versions of the same ARXML file. Version management of all ARXML files in a shared repository is mandatory, not optional.

📊 Industry Note

Engineers who master both the theoretical concepts and the practical toolchain skills covered in this course are among the most sought-after professionals in the automotive software industry. The combination of AUTOSAR standards knowledge, safety engineering understanding, and hands-on configuration experience commands premium salaries at OEMs and Tier-1 suppliers globally.

← PreviousPreprocessor, Macros & Conditional CompilationNext →Memory-Mapped I/O & Register Access