Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[x86] why llvm keep rodata.const section #133684

Open
LukeSTM opened this issue Mar 31, 2025 · 3 comments
Open

[x86] why llvm keep rodata.const section #133684

LukeSTM opened this issue Mar 31, 2025 · 3 comments
Labels
backend:X86 question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@LukeSTM
Copy link

LukeSTM commented Mar 31, 2025

https://godbolt.org/z/r8ha9o9nf

compile option: -O2 -ffunction-sections -fdata-sections -mno-sse

#include <stdio.h>
#include <string.h>                                                                                                                                             
 
struct ss_t {
	int s_id;
	char msg[];
};

struct ss_t *mst_1;

void do_some_test(void)
{
	struct ss_t *mst_2;
	char buf[100];
	int fill_num=4;
	char tmp_str[40]="test stringssssssssss!";
	memcpy(buf,&fill_num,sizeof(int));
	memcpy(buf+sizeof(int), tmp_str, strlen(tmp_str) + 1); 
	mst_1=(struct ss_t *)buf;
 
	if (mst_1->s_id==4)
		printf("mst_1->s_id=%d,mst_1->msg=%s\n",mst_1->s_id,mst_1->msg);
}

Image

sorry, but i want know, why clang keep .rodata..L__const.do_some_test.tmp_str in sections. Constant could be expanded into movabsq instructions, and what would use .rodata..L__const.do_some_test.tmp_str? Dose it should be removed ?

Image

@LukeSTM LukeSTM changed the title [x86] why llvm keep rodata.const section when compile with -fdata-sections [x86] why llvm keep rodata.const section Mar 31, 2025
@LukeSTM
Copy link
Author

LukeSTM commented Mar 31, 2025

CC @MaskRay

@frederick-vs-ja frederick-vs-ja added question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! backend:X86 and removed new issue labels Mar 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 31, 2025

@llvm/issue-subscribers-backend-x86

Author: Luke (LukeSTM)

https://godbolt.org/z/r8ha9o9nf

compile option: -O2 -ffunction-sections -fdata-sections -mno-sse

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;                                                                                                                                             
 
struct ss_t {
	int s_id;
	char msg[];
};

struct ss_t *mst_1;

void do_some_test(void)
{
	struct ss_t *mst_2;
	char buf[100];
	int fill_num=4;
	char tmp_str[40]="test stringssssssssss!";
	memcpy(buf,&amp;fill_num,sizeof(int));
	memcpy(buf+sizeof(int), tmp_str, strlen(tmp_str) + 1); 
	mst_1=(struct ss_t *)buf;
 
	if (mst_1-&gt;s_id==4)
		printf("mst_1-&gt;s_id=%d,mst_1-&gt;msg=%s\n",mst_1-&gt;s_id,mst_1-&gt;msg);
}

Image

sorry, but i want know, why clang keep .rodata..L__const.do_some_test.tmp_str in sections. Constant could be expanded into movabsq instructions, and what would use .rodata..L__const.do_some_test.tmp_str? Dose it should be removed ?

Image

@efriedma-quic
Copy link
Collaborator

I briefly looked; I think what's happening is that the accesses to the string are getting removed, but it happens too late for the relevant passes to remove the global. I'd expect the linker to eliminate the string at link time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

4 participants