Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembler warning with gcc warning when placing data in .text

Tags:

c

gcc

assembly

When I compile

__attribute__((section(".text"))) const int x = 42;
int main(){ return x; }

with gcc (works with tinycc and clang), I get

Warning: ignoring changed section attributes for .text.

What is the cause of the warning and how can it be eliminated while still keeping the (always readonly) data in .text?

like image 726
PSkocik Avatar asked Dec 31 '22 13:12

PSkocik


2 Answers

Apparently:

__attribute__((section(".text#"))) const int x = 42;

Ref: https://gcc.gnu.org/ml/gcc-help/2010-09/msg00088.html where the answerer explains:

__attribute__ ((section(".text"))) will make gcc emits ,"aw",@progbits after the .text to alter the section attributes. If you use: __attribute__ ((section(".text#"))) (notice the extra '#') this suffix will be commented in the assembly and the warning will disappear

In that case the variable concerned was not const, so it was especially ill-advised (as stated by another responder) to the same question. In your case it is a const, so the access is "a" rather than "aw" - still probably ill-advised however.

like image 86
Clifford Avatar answered Jan 03 '23 04:01

Clifford


Apparently gcc emits a .section .text,"a",@progbits directive instead of just .section .text. I don't see any way to avoid it. However the default linker script usually merges all sections named .text.* so you can do something like __attribute__((section(".text.consts"))) and in the final binary it will be in the .text section.

@Clifford found a hackish workaround which involves putting a # after the section name so that the assembler considers the rest of the line a comment.

like image 26
Jester Avatar answered Jan 03 '23 04:01

Jester