Symbol table of a C program

Hi,
is there any command to see symbol table info.
will it show where its allocating memory for varibales golbals & locals and code.(i mean the segments).

i read there is a section called read only data segment and this is where initialized data such as strings stores.

i have wriiten the following program and its giving segmentation for strings but not others.

any idea whats the proper reason.

char *p="test";
int g=10;
int main()
{
g=20;// no segmentation
printf("%d",g);
getchar();
        *p='T';// generates segmentation
printf("%s",p);
return 0;
}

thanks in advnace
:slight_smile:

Hi.

You can use: objdump or nm.

---------- Post updated at 08:25 AM ---------- Previous update was at 08:16 AM ----------

The problem of that code is that 'p' symbol is stored in a not writable data segment because it is defined as a constant.

If you compile that code, for example, with:

g++ -fwritable-strings test.cpp   -o test

'p' symbol is stored in a writable data segment and the program will be ok, but this option might be removed in a future release of G++.

The line

*p='T';

has 2 problems:

  1. You're trying to assign a character value (which is a value between 0 and 255), not a pointer
  2. You're trying set the target of the pointer to another pointer, not redefine the original pointer

This line should work:

p="T";

Remember: single quotes = character, double quotes = pointer to NULL-terminated character array ("string")

I think

*p = 'T'

it 's the same that:

p[0] = 'T'

so, it's ok.

The final string will be: "Test", the first character will be uppercase. The difference is the options compilation.

how to use the commands , i used

[User@telnet spark]$ objdump -t te.o
te.o: file format elf32-i386
SYMBOL TABLE:
00000000 l df *ABS* 00000000 te.c
00000000 l d .text 00000000
00000000 l d .data 00000000
00000000 l d .bss 00000000
00000000 l d .rodata 00000000
00000000 l d .comment 00000000
00000000 g O .data 00000004 p
00000000 g F .text 00000035 main
00000000 *UND* 00000000 printf

when i use objdump its giving the list of options that can be used with it.
i tried all but dint understand much.

i want the info in following format:

 
symbolname section address default val 
g                rwdata    ----      0

and so on

if any global variables are present it should give U ( undefined/ defined later)

Try with:

objdump -s <exec>

Segments: data and rodata

You can see the difference of the compilation with and without -fwritable-strings option.

If you want to get the executable symbols table, you can use:

nm -C -f sysv <exec>

MrUser, your code is problematic. Consider the following 2 statements:

char p[] = "test";
char *p = "test";

In the first statement, a mutable (modifiable or non-constant) string p is declared as an array of character and initialized with the string "test". The size of array p is 5 bytes including a null terminator. The result of *p = 'T' is defined and works as expected.

In the second statement an immutable (AKA constant) string litteral is declared and the base address of the memory where the compiler stores the string litteral is assigned to the pointer variable p. In this case, the result of *p = 'T' is undefined as you found out.

When you want a mutable string, i.e. a string which can be modified, use initialization instead of assignment.

Try using nm or odump for that.

If you use gdb to get the address of the string literal you will find that it is in the address range of the text segment which is readonly.

A string literal cannot be modified since it is stored in the text segment which is shared and readonly.