2002-11-20 22:36:45

by Patrick Mansfield

[permalink] [raw]
Subject: getting text strings into __initdata for char *foo = "data"

Hi -

Is there a way to get char * initialized data to go into the init
data section?

For example, in the following I want init_foobar and its associated
text strings to go into the init data section, not just the pointers
in init_foobar:

[patman@elm3a50 bleed-2.5]$ more initdata.c
#include <linux/init.h>

/*
* This only puts the pointer in .init.data.
*/
char *init_foobar[] __initdata = {
"AAAAtextAAAAtextAAAAtext-morea",
"AAAA",
"AAAA",
};

/*
* Following is OK, but wastes some space (on disk) and needs one extra byte
* for the '\0'. Worse, the second entry below is not '\0' terminated and gets
* no compilation warning :(
*/
char init_foobar2[][31] __initdata = {
"BBBBtextBBBBtextBBBBtext-morea",
"BBBBtextBBBBtextBBBBtext-morebc",
"BBBB",
"BBBB",
};

char *regular_foobar[] = {
"CCCCtextCCCCtextCCCCtext-morea",
"CCCCtextCCCCtextCCCCtext-moreb",
"CCCC",
};


[patman@elm3a50 bleed-2.5]$ objdump -s initdata.o

initdata.o: file format elf32-i386

Contents of section .text:
Contents of section .data:
0000 40000000 20000000 05000000 @... .......
Contents of section .note:
0000 08000000 00000000 01000000 30312e30 ............01.0
0010 31000000 1...
Contents of section .init.data:
0000 00000000 00000000 00000000 42424242 ............BBBB
0010 74657874 42424242 74657874 42424242 textBBBBtextBBBB
0020 74657874 2d6d6f72 65610042 42424274 text-morea.BBBBt
0030 65787442 42424274 65787442 42424274 extBBBBtextBBBBt
0040 6578742d 6d6f7265 62634242 42420000 ext-morebcBBBB..
0050 00000000 00000000 00000000 00000000 ................
0060 00000000 00000000 00424242 42000000 .........BBBB...
0070 00000000 00000000 00000000 00000000 ................
0080 00000000 00000000 ........
Contents of section .rodata.str1.1:
0000 41414141 00434343 4300 AAAA.CCCC.
Contents of section .rodata.str1.32:
0000 41414141 74657874 41414141 74657874 AAAAtextAAAAtext
0010 41414141 74657874 2d6d6f72 65610000 AAAAtext-morea..
0020 43434343 74657874 43434343 74657874 CCCCtextCCCCtext
0030 43434343 74657874 2d6d6f72 65620000 CCCCtext-moreb..
0040 43434343 74657874 43434343 74657874 CCCCtextCCCCtext
0050 43434343 74657874 2d6d6f72 656100 CCCCtext-morea.
Contents of section .comment:
0000 00474343 3a202847 4e552920 322e3936 .GCC: (GNU) 2.96
0010 20323030 30303733 31202852 65642048 20000731 (Red H
0020 6174204c 696e7578 20372e31 20322e39 at Linux 7.1 2.9
0030 362d3938 2900 6-98).


-- Patrick Mansfield


2002-11-20 23:36:16

by Richard B. Johnson

[permalink] [raw]
Subject: Re: getting text strings into __initdata for char *foo = "data"

On Wed, 20 Nov 2002, Patrick Mansfield wrote:

> Hi -
>
> Is there a way to get char * initialized data to go into the init
> data section?
>
> For example, in the following I want init_foobar and its associated
> text strings to go into the init data section, not just the pointers
> in init_foobar:
>

[SNIPPED...]




I use assembler when I need to do things 'C' doesn't want
to do.. like:

.section .init.data
.global foo
.type foo,@object
.size foo,11
foo: .string "1234567890"

This will work for ".sections" like .data, etc,. but the
object file in empty when I use .init.data. This is probably
because gas treats .string specially. I know you can do this
with ".byte". You don't have to make the assembly by hand.

I do something like:

#ifdef THIS_WILL_NEVER_BE_DEFINED
char foo[]="This is the string..."
#else
extern char foo[];
#endif

I write a 'C' program that looks for "THIS_WILL_NEVER_BE_DEFINED"
and writes gas assembly for the strings up to the "#else".

I do this in an embedded system to put many/most/all strings
in NVRAM PROM.


Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
Bush : The Fourth Reich of America


2002-11-21 05:41:48

by Keith Owens

[permalink] [raw]
Subject: Re: getting text strings into __initdata for char *foo = "data"

On Wed, 20 Nov 2002 14:43:43 -0800,
Patrick Mansfield <[email protected]> wrote:
>Is there a way to get char * initialized data to go into the init
>data section?
>
>For example, in the following I want init_foobar and its associated
>text strings to go into the init data section, not just the pointers
>in init_foobar:

>From the kdb common patch, kdb/Makefile.

gen-kdb_cmds.c: kdb_cmds Makefile
$(AWK) 'BEGIN {print "#include <linux/init.h>"} \
/^ *#/{next} \
/^[ \t]*$$/{next} \
{gsub(/"/, "\\\"", $$0); \
print "static __initdata char kdb_cmd" cmds++ "[] = \"" $$0 "\\n\";"} \
END {print "char __initdata *kdb_cmds[] = {"; for (i = 0; i < cmds; ++i) {print " kdb_cmd" i ","}; print(" 0\n};");}' \
kdb_cmds > gen-kdb_cmds.c

Converts kdb_cmds

set LINES 2000
set BTSP 1
bp sys_open

into gen-kdb_cmds.c

#include <linux/init.h>
static __initdata char kdb_cmd0[] = "set LINES 2000\n";
static __initdata char kdb_cmd1[] = "set BTSP 1\n";
static __initdata char kdb_cmd2[] = "bp sys_open\n";
char __initdata *kdb_cmds[] = {
kdb_cmd0,
kdb_cmd1,
kdb_cmd2,
0
};

# objdump -h kdb/gen-kdb_cmds.o

kdb/gen-kdb_cmds.o: file format elf64-ia64-little

Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000000 0000000000000000 0000000000000000 00000040 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .data 00000000 0000000000000000 0000000000000000 00000040 2**0
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000000 0000000000000000 0000000000000000 00000040 2**0
ALLOC
3 .debug_abbrev 000000a8 0000000000000000 0000000000000000 00000040 2**0
CONTENTS, READONLY, DEBUGGING
4 .debug_info 000001c7 0000000000000000 0000000000000000 000000e8 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
5 .debug_line 00000000 0000000000000000 0000000000000000 000002af 2**0
CONTENTS, READONLY, DEBUGGING
6 .data.init 00000050 0000000000000000 0000000000000000 000002b0 2**3
CONTENTS, ALLOC, LOAD, RELOC, DATA
7 .debug_pubnames 0000001f 0000000000000000 0000000000000000 00000300 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
8 .comment 00000029 0000000000000000 0000000000000000 0000031f 2**0
CONTENTS, READONLY

text, data and bss are all 0 size. Everything is in .data.init.