Skip to content

[Clang] C++ Modules: Setting section attribute on variable in module causes miscompilation #144312

Open
@Gurah

Description

@Gurah

Setting the output section of a variable by attribute in a module interface causes miscompilation.
Code works fine if not using c++ modules or if removing section attribute.

Example
test.ccm:

export module test;

int foo  __attribute__((section(".data.foo"))) = 2;

export int get_foo()
{
	return foo;
}

export void set_foo(int val)
{
	foo = val;
}

main.cc:

import test;

int main()
{
	set_foo(5);
	return get_foo();
}

Compile and run:
clang++ -std=c++23 -O1 -c test.ccm -fmodule-output=test.pcm
clang++ -std=c++23 -O1 -c main.cc -fmodule-file=test=test.pcm
clang++ test.o main.o -o test
./test
echo $?

Expected result:
5

Actual result:
2

Clang version
clang++ --version
clang version 19.1.7
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions