Liste des Groupes | Revenir à cl c |
David Brown <david.brown@hesbynett.no> writes:On 01/08/2024 22:42, Bart wrote:>char text[]="this is a test";>
But this can be changed without making the program self-modifying.
"this is a test" is a string literal, and is typically part of the
program's image. (There are some C implementations that do things
differently, like storing such initialisation data in a compressed format.)
>
The array "char text[]", however, is a normal variable of type array of
char. It is most definitely not part of the program image - it is in
ram (statically allocated or on the stack, depending on the context) and
is initialised by copying the characters from the string literal (prior
to main(), or at each entry to its scope if it is a local variable).
Linux (ELF):
>
A file-scope static declaration of char text[] will emit the string
literal into the .data section and that data section will be loaded
into memory by the ELF loader. There is no copy made at runtime
before main().
>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
>
char text1[] = "This is a test of a static-scope string";
int
main(int argc, const char **argv)
{
char text2[] = "This is a test of a function-scope string";
>[36 lines deleted]
fprintf(stdout, "%p %s\n", &text1, text1);
fprintf(stdout, "%s\n", text2);
>
return 0;
}
>
$ /tmp/a
0x601060 This is a test of a static-scope string
This is a test of a function-scope string
>
$ objdump -p /tmp/a
>
/tmp/a: file format elf64-x86-64
>
Program Header:
PHDR off 0x0000000000000040 vaddr 0x0000000000400040 paddr 0x0000000000400040 align 2**3
filesz 0x00000000000001f8 memsz 0x00000000000001f8 flags r-x
INTERP off 0x0000000000000238 vaddr 0x0000000000400238 paddr 0x0000000000400238 align 2**0
filesz 0x000000000000001c memsz 0x000000000000001c flags r--
LOAD off 0x0000000000000000 vaddr 0x0000000000400000 paddr 0x0000000000400000 align 2**21
filesz 0x00000000000007dc memsz 0x00000000000007dc flags r-x
LOAD off 0x0000000000000e10 vaddr 0x0000000000600e10 paddr 0x0000000000600e10 align 2**21
filesz 0x0000000000000278 memsz 0x0000000000000290 flags rw-
>
.data section:
>
0000e00: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0001050: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0001060: 5468 6973 2069 7320 6120 7465 7374 206f This is a test o
0001070: 6620 6120 7374 6174 6963 2d73 636f 7065 f a static-scope
0001080: 2073 7472 696e 6700 4743 433a 2028 474e string.GCC: (GN
>
$ printf "0x%x\n" $(( 0x601060 - 0x0000000000600e10 ))
0x250
Les messages affichés proviennent d'usenet.