The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading ELF file Symbol table of C++ program vinod_chitrali High Level Programming 2 08-02-2009 10:54 AM
Reading ELF file Symbol table of C++ program vinod_chitrali Linux 0 08-01-2009 02:15 PM
Check the record count in table (table in oracle) kamineni Shell Programming and Scripting 1 12-15-2008 08:31 AM
how to view symbol table in unix saravanan_nitt High Level Programming 1 02-14-2008 01:02 AM
no symbol table Dom_Cyrus High Level Programming 2 01-31-2006 09:15 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 2 Weeks Ago
MrUser's Avatar
MrUser MrUser is offline
Registered User
  
 

Join Date: Oct 2009
Location: Earth
Posts: 23
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.

Code:
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
  #2 (permalink)  
Old 2 Weeks Ago
pogdorica's Avatar
pogdorica pogdorica is offline
Registered User
  
 

Join Date: Oct 2009
Location: Madrid
Posts: 53
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:
Code:
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++.
  #3 (permalink)  
Old 2 Weeks Ago
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,839
The line
Code:
*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:
Code:
p="T";
Remember: single quotes = character, double quotes = pointer to NULL-terminated character array ("string")
  #4 (permalink)  
Old 2 Weeks Ago
pogdorica's Avatar
pogdorica pogdorica is offline
Registered User
  
 

Join Date: Oct 2009
Location: Madrid
Posts: 53
I think
Code:
*p = 'T'
it 's the same that:
Code:
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.
  #5 (permalink)  
Old 2 Weeks Ago
MrUser's Avatar
MrUser MrUser is offline
Registered User
  
 

Join Date: Oct 2009
Location: Earth
Posts: 23
Quote:
You can use: objdump or nm.

how to use the commands , i used

Code:
[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:

Code:
 
symbolname section address default val 
g                rwdata    ----      0
and so on

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

Last edited by pludi; 2 Weeks Ago at 04:24 AM.. Reason: code tags added
  #6 (permalink)  
Old 2 Weeks Ago
pogdorica's Avatar
pogdorica pogdorica is offline
Registered User
  
 

Join Date: Oct 2009
Location: Madrid
Posts: 53
Try with:
Code:
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:

Code:
nm -C -f sysv <exec>

Last edited by pogdorica; 2 Weeks Ago at 04:27 AM..
  #7 (permalink)  
Old 2 Weeks Ago
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
MrUser, your code is problematic. Consider the following 2 statements:
Code:
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.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0