Posts Tagged ‘read dsp ic encrypted eeprom’
Read DSP Source Code: Segments Before Instructions
DSP toolchains do not produce flat binaries. They produce object files with sections, each of which has a load address, a run address, possibly an alignment requirement, and a section type that determines what the loader does with it. The final flash image is a concatenation produced by a linker script, and the linker script is not in the image.
This matters because DSP programs frequently relocate themselves. Code that executes from fast internal RAM may be stored in slower external memory and copied at boot. Data marked as uninitialised occupies no space in the image but must be zeroed at startup. A reader who treats the image as a contiguous block of instructions will decode the copy routine as if it were the application, and the result will be plausible and wrong.
The sections that matter
| Section type | Load address | Run address | Contents |
|---|---|---|---|
| Text | In the image | Same as load, usually | Instructions |
| Initialised data | In the image | Possibly different, if in RAM | Constants and tables |
| Uninitialised data | Absent from the image | RAM | Nothing; zeroed at boot |
| Overlay or far text | In the image | Copied to another region | Instructions that run relocated |
| Initialisation table | In the image | Read by the startup code | Copy and zero descriptors |
The fourth row is the one that most often defeats a naive read. Overlay sections exist so that a large program can run in a small fast memory, with different overlays swapped in. The instructions are all present in the image, but only one overlay is resident at a time, and the code at a given run address changes during execution.
The initialisation table is the map
The startup code reads a table of descriptors that say what to copy where and what to zero. That table is the single most useful structure in the image, because it encodes the linker’s decisions.
| Field in a descriptor | Meaning |
|---|---|
| Source address | Where the initialised bytes are in the load image |
| Destination address | Where they must be at run time |
| Length | How many bytes to copy |
| Zero flag, or a separate entry | Whether the region is zeroed rather than copied |
Finding the table is a pattern-matching exercise: a sequence of records with regular stride, source addresses inside the image, destination addresses in RAM, and lengths that match the sizes of the data sections.
| Observation | Conclusion |
|---|---|
| Source addresses within the image span | The table describes copies from flash |
| Destinations in a contiguous RAM range | The run-time data layout |
| Lengths that sum to the data section size | Consistency check on the table |
| No table at all | The program runs from load address; image is flat |
The fourth row describes the simple case, and confirming it is valuable: if there is no initialisation table, the image really is flat and instruction decoding can begin immediately.
Why load and run addresses differ
The distinction exists because memory is not uniform. Fast memory is small and expensive, slow memory is large and cheap, and the linker’s job is to place things where they perform while keeping the image in non-volatile storage.
| Memory | Typical role | Consequence |
|---|---|---|
| External flash | Holds the load image | Slow, non-volatile, large |
| Internal RAM | Holds run-time code and data | Fast, volatile, small |
| Internal flash | Holds both, on some parts | Requires no copy |
| External RAM | Holds large buffers | Slower than internal, still faster than flash |
On a system where code runs from internal RAM, the address in the image is not the address the instruction executes at, and every absolute address inside the code refers to the run address. Decoding with the load address produces a disassembly full of references to the wrong locations.
Recovering the layout
The workable sequence is bottom-up and does not skip the table.
| Step | Output | Check |
|---|---|---|
| Identify the entry point | Reset vector target | The first instruction is plausible |
| Locate the initialisation table | Copy descriptors | Descriptors are self-consistent |
| Reconstruct the memory map | Regions with attributes | Regions do not overlap |
| Apply relocations mentally | Run-time addresses | Absolute references land in known regions |
| Decode instructions | Disassembly | Control flow stays within regions |
The fourth step is the one that turns a disassembly into something readable. An instruction that loads from an address inside a known data region is a table access; the same instruction with an address in an unknown region is either a bug or a sign that a region was missed.
Overlays and the address that means two things
An overlay section is the most confusing structure in a DSP image and the one most likely to be misread as a bug. The same run-time address holds different instructions at different moments, because the program copies a new overlay in when it needs it. In the image, all the overlays are present, at distinct load addresses. In execution, only one is visible at a time.
| Observation in the image | Meaning |
|---|---|
| Several code blocks whose destinations are identical | Overlays sharing a run address |
| Copy routine called with a constant argument | Overlay selection |
| Code at a run address that does not match its callers | The image shows a different overlay than the call expects |
| A table of load addresses near the copy routine | The overlay directory |
The third row is what makes a static analysis disagree with the running system, and it explains a class of bug reports that reach this work: a disassembly shows a function doing something the device demonstrably does not do, because the disassembly is looking at the wrong overlay.
The instruction-level work that follows
Once the layout is known, DSP instruction decoding is a specialised but tractable discipline. The features that make DSPs fast — delayed branches, parallel issue, circular addressing — are exactly the features that make a naive decoder wrong.
| Feature | Decoding requirement |
|---|---|
| Delayed branch | Instructions after the branch belong to the previous block |
| Parallel packet | Operations in one packet share issue constraints |
| Circular addressing | Effective address depends on a register-held buffer length |
| Bit-reversed addressing | Index order is not linear |
| Multiple accumulators | Data flow spans several destinations |
These are covered in the context of mixed-device work in reversing DSP and CPLD programs, and the data-space half of the image is the subject of reading DSP chip program. Where the same board carries a logic device whose decode defines the memory map, reading DSP and CPLD dump information covers the reconciliation.
Services at this boundary
Work covers entry point identification, initialisation table recovery, memory map reconstruction with load and run addresses, overlay identification, and disassembly that respects the pipeline and addressing modes. It does not cover recovering the original linker script, and it does not cover producing C without the section layout being established first.
The practical rule is that segments come before instructions. A disassembly produced without a memory map is a hypothesis, and it is usually a wrong one.