Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/windows-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ jobs:

- name: Test
# Disable parallel test runs on GitHub runners due to resource constraints
run: ctest --preset=${{ matrix.preset }} --parallel 1
run: ctest --preset=${{ matrix.preset }} --parallel 1
7 changes: 7 additions & 0 deletions systemc-components/common/include/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,14 @@ class loader : public sc_core::sc_module
{
if (elf_version(EV_CURRENT) == EV_NONE) SCP_FATAL("elf_reader") << "failed to read libelf version";

#ifdef _WIN32
// On Windows, open() defaults to text mode which can corrupt binary files (e.g. .so, .skel.so) during ELF loading.
// O_BINARY forces raw binary reads to prevent this.
m_fd = open(filename(), O_RDONLY | O_BINARY, 0);
#else
m_fd = open(filename(), O_RDONLY, 0);
#endif

if (m_fd < 0) SCP_FATAL("elf_reader") << "cannot open elf file " << filename();

Elf* elf = elf_begin(m_fd, ELF_C_READ, nullptr);
Expand Down
8 changes: 8 additions & 0 deletions tests/components/loader/loader-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ TEST_BENCH(LoaderTest, SimpleReadELFFile)

ASSERT_EQ(m_initiator.do_read(0x0000, data), tlm::TLM_OK_RESPONSE);
ASSERT_EQ(data, 0xaf);

/* CRLF bytes at offset 0x0004-0x0005: without O_BINARY on Windows, 0x0D 0x0A
* is collapsed to 0x0A by the CRT text-mode translation, corrupting the data. */
ASSERT_EQ(m_initiator.do_read(0x0004, data), tlm::TLM_OK_RESPONSE);
ASSERT_EQ(data, 0x0d);

ASSERT_EQ(m_initiator.do_read(0x0005, data), tlm::TLM_OK_RESPONSE);
ASSERT_EQ(data, 0x0a);
}

TEST_BENCH(LoaderTest, SimpleReadData)
Expand Down
1 change: 1 addition & 0 deletions tests/components/loader/src/loader-test.asm
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.section .data
.word 0xdeadbeaf
.byte 0x0D, 0x0A /* CRLF: collapsed to 0x0A in Windows text mode without O_BINARY */
2 changes: 1 addition & 1 deletion tests/components/loader/src/loader-test.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
¯¾­Þ
¯¾­Þ
Binary file modified tests/components/loader/src/loader-test.elf
Binary file not shown.
Loading