Menu ▾ ▴

#4244 ASlink: Fix BSD AR symbol indexing and malformed archive handling

open
nobody
Z80
5
1 day ago
2026-11-04
No

Summary

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.

Issues identified

1. Incorrect BSD symbol name indexing

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.

2. BSD archive symbol table name mismatch

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.

3. Missing bounds validation in archive symbol tables

Both the indexed and non-indexed AR library readers previously trusted several values obtained from archive metadata without sufficient validation.

Affected fields include:

  • BSD ranlib table size and record count.
  • BSD string table size.
  • Symbol name offsets and string termination.
  • Archive member offsets.
  • GNU AR symbol table counts and symbol strings.

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.

Reproduction and regression testing

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.

Valid BSD archive regression

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.

Malformed archive regression

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.

Validation results

  • Indexed linker compilation: PASS.
  • Non-indexed linker compilation: PASS.
  • Valid BSD archive regression: PASS.
  • Previously crashing malformed archive cases: rejected without observed access violations.
  • Final regression script: exit code 0.
  • Patch application check: exit code 0.

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.

Attachment

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.

1 Attachments

Discussion

  • zeynelsagcan

    zeynelsagcan - 2026-11-04

    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 .rel files and ASlink. This led me to inspect the linker sources more closely, including the AR library reader in sdas/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
  • Philipp Klaus Krause

    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.

     
    • zeynelsagcan

      zeynelsagcan - 6 days ago

      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:

      • ASxxxx 6.10 contains linksrc/lklibr.c, but does not contain a corresponding linksrc/lkar.c.
      • I searched all C source and header files under the ASxxxx 6.10 linksrc directory for ARMAG, __.SYMDEF, buildlibraryindex_ar, ranlib, !<arch>, and ar_hdr. No matches were found.
      • In contrast, SDCC r16998 contains sdas/linksrc/lkar.c, which implements GNU/BSD AR archive handling, including buildlibraryindex_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.c patch 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.c and does not modify the common ASxxxx-derived linker components.

       
      • Maarten Brock

        Maarten Brock - 1 day ago

        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.

         

Log in to post a comment.