The ASlink AR library reader (sdas/linksrc/lkar.c) contains several issues affecting BSD archive symbol indexing and malformed archive handling.
These issues can cause incorrect symbol resolution, undefined global symbols, duplicate symbol warnings, and access violations when processing malformed archive metadata.
I prepared a patch against SDCC source revision r16998 that corrects the BSD symbol indexing issues and adds bounds validation to the AR archive reader.
In buildlibraryindex_ar(), the BSD symbol name is calculated using its offset in the string table:
sym = ps + sgetl(po);
However, the calculated pointer is discarded by the subsequent statement:
sym = strdup(ps);
This causes multiple symbols to be indexed using the first string table entry instead of their actual names.
The fix preserves the calculated symbol address and duplicates the correct string.
The BSD symbol table name __.SYMDEF is not consistently recognized because the expected name contains trailing spaces, while get_member_name() removes them.
Consequently, a valid BSD archive may not be indexed correctly.
The patch corrects this handling while retaining support for __.SYMDEF SORTED.
Both the indexed and non-indexed AR library readers previously trusted several values obtained from archive metadata without sufficient validation.
Affected fields include:
Malformed values can result in out-of-bounds accesses or invalid memory references.
The patch adds validation before accessing the corresponding memory regions and improves error handling for invalid archive metadata.
Testing was performed on Windows using MinGW-w64 GCC and SDCC Z80 assembler/linker components.
Two linker configurations were built from the same SDCC r16998 sources:
INDEXLIB enabled — in-memory library indexing.INDEXLIB disabled — direct archive symbol searching.A BSD archive containing two Z80 object modules was constructed with symbol records ordered A → B → A.
The archive contained three symbols:
__s — module A._b_func — module B._f — module A.With the original indexed linker, the BSD SORTED archive produced incorrect duplicate-symbol warnings and failed to resolve _b_func.
The patched indexed linker successfully resolved all three symbols and generated the expected Intel HEX output.
The BSD UNSORTED archive also linked successfully with the patched indexed linker.
The indexed and non-indexed configurations were compared against direct .rel linkage. All eight expected regression outcomes passed.
Additional BSD archives were generated with deliberately invalid metadata.
Two cases previously demonstrated access violations (0xC0000005):
Excessive ranlib record count
The malformed ranlib table caused access violations in the original tested configurations. The patched indexed and non-indexed linkers now reject the archive with a controlled error.
Invalid archive member offset
The original indexed linker crashed when encountering an invalid member offset. The patched indexed linker now reports:
?ASlink-Error-Bad BSD member offset
and exits without an access violation.
Additional malformed cases covering symbol offsets, unterminated strings, oversized tables, and empty indexes were included in the final regression checks.
The patch was generated against SDCC r16998 and verified using:
patch --dry-run -p1 -i lkar_r16998_final.patch
Result:
checking file sdas/linksrc/lkar.c
The original source tree was left unchanged.
lkar_r16998_final.patch`
The patch modifies sdas/linksrc/lkar.c and includes the BSD symbol indexing fixes, archive metadata bounds checks, and related error-handling improvements.
Additional context — how this investigation started
This investigation originated while examining SDCC issue [#3113], which concerns Z80 assembler handling of complex symbol expressions and relocation.
During that work, I tested different SDCC revisions and examined the interaction between the assembler-generated
.relfiles and ASlink. This led me to inspect the linker sources more closely, including the AR library reader insdas/linksrc/lkar.c.While reviewing that code, I noticed several potentially unsafe pointer operations and archive metadata accesses. I investigated them separately and developed targeted regression tests using GNU and BSD AR archives.
Those tests revealed the symbol indexing errors and malformed archive crashes described in this ticket.
To clarify, this patch is not intended to fix issue [#3113]. The two issues are technically independent. Issue #3113 provided the starting point for the source inspection, while the defects reported here were independently reproduced and addressed.
I opened #4244 separately to keep the AR library reader fixes and their regression results isolated from the original assembler issue.
Related
Bugs: #3113
Last edit: Maarten Brock 1 day ago
Changes in the linker, which is derived from the asxxxx one. How do they compare to current upstream asxxxx 6.10? Does upstream asxxxx 6.10 have a fix that we could use? If the issue exists in upstream asxxxx 6.10, I suggest to submit the fix upstream.
Thank you for the suggestion, Philipp.
I downloaded the official ASxxxx 6.10 source archive (
asxs6p10.zip) and compared its linker sources with SDCC r16998.Findings:
linksrc/lklibr.c, but does not contain a correspondinglinksrc/lkar.c.linksrcdirectory forARMAG,__.SYMDEF,buildlibraryindex_ar,ranlib,!<arch>, andar_hdr. No matches were found.sdas/linksrc/lkar.c, which implements GNU/BSD AR archive handling, includingbuildlibraryindex_ar().Based on this source comparison, the AR archive reader affected by issue #4244 appears to be an SDCC-specific extension rather than functionality inherited from current upstream ASxxxx 6.10.
Therefore, ASxxxx 6.10 does not appear to contain an equivalent implementation or an existing fix that could be reused. Submitting this particular
lkar.cpatch to ASxxxx upstream would not address an existing component there.I believe the proposed fix should be reviewed and maintained within SDCC, as it targets SDCC's own GNU/BSD AR archive support.
The patch remains limited to
sdas/linksrc/lkar.cand does not modify the common ASxxxx-derived linker components.AFAIK lkar.c and lkar.h do not come from ASxxxx, but were created by Borut in [r5336]. He probably kept the copyright from Alan Baldwin because it contains code that originates in some other ASxxxx source file. The files belong to SDCC, but could be upstreamed if Alan is willing to accept them.
The same goes for ranlib.