LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-24-2005, 08:02 PM   #1
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Blog Entries: 1

Rep: Reputation: 15
Extracting Functions from a Shared Library


What Linux Command can I use for extracting the functions of a shared library?

The libraries I'm trying to extract have a .so extention (obviously) and are of the ELF File format



Thanks, I would really appreciate it!
 
Old 06-24-2005, 08:59 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
What exactly do you mean by "extract"?

Do you mean getting the source code? If so, then you can't. The library contains compiled versions of the functions. Compilation is a lossy process meaning that you can't get back the exact source code from the machine code. You'll have to download the source code for you library if you want to examine the functions it uses.

If you mean pulling the functions into your own program so your program does not require the library anymore, that's called "static linking". You'll need to things:

1. You need to have a ".a" version of the library you want to statically link against. A ".so" won't work.

2. When you compile your program, you need to pass a special command line argument to specify static linking. For gcc it's "-static". If you use a different compiler, you'll have to check its documentation.

When statically linked, the program is self-contained, meaning the program can be run on any system that supports the executable format it's in.
 
Old 06-24-2005, 09:24 PM   #3
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Blog Entries: 1

Original Poster
Rep: Reputation: 15
I suppose my hopes are ruined them

I was actually very interested in extracting the underlying source code of the C Programming Language. I was hoping that by "extracting" the functions from the C Libraries, I could actually see the Assembly Language Code that made up those functions. If the libraries are in Machine Code, then I suppose there is no way to "see" the source code.

On the other hand, Assembly Language and Machine Code are the same, the only difference is that Assembly Language is a Symbolic Representation of Machine Code (so instead of working in actual Binaries, we use English ). If there was some sort of program out there that could "rebuild" the Assembly Source code from this Machine Code... then I might have a hope



Regardless, what I was REALLY looking for was the underlying source code for the C Programming Language- does anyone know if it is available to the public?

Thanks, I would REALLY REALLY REALLLLLLLLLY APPRECIATE IT

Btw: Thanks Anyways Dark_Helmet, you reply was really informative in itself and I really appreciate your help
 
Old 06-24-2005, 09:42 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Well, there are "decompilers" out there, but I have no experience with them. You have to understand that if you find one, and if you can get it to work on a library, then any code it spits out will be almost unintelligible. Specifically, variable names will be seemingly random.. Granted, in assembly there aren't really variables; just registers and memory locations. I wish I could point you to one, but I don't know of any.

Now, you say you want to look at the underlying code for C. That's still open to interpretation. Do you want to see the code for a C compiler? Since we were talking about libraries, I assume that you'd be interested in the standard C library. The compiler and the standard C library are two entirely different beasts. The compiler is responsible for parsing your code, optimizing it, and eventually turning it into machine code. The standard C library is created by the compiler, and provides the tools for other programs to use. Either way, you can look at the C code for both.

GCC page on freshmeat: http://freshmeat.net/projects/gcc/
GNU C library (standard C functions) on freshmeat: http://freshmeat.net/projects/glibc/

Both of those pages provide links to download tarballs of source code.

One thing I need to mention: you have to understand that there is no golden standard of "underlying source code for the C Programming Language". The C programming language is a specification only. It says what a compiler must support, or defines what functions must be present in a standard library. The specification does not say how that stuff is implemented. The code for gcc is one way of doing it. A borland C compiler is another. Both adhere to the C specification, but how the compilers work under the hood can vary significantly. Similarly, the GNU C library is one implementation of the standard C library. There are other syandard C libraries that provide the same functions, but those functions can (and probably are) written very differently. There is no single implementation that everyone follows.

EDIT: One other thing to mention. The gcc compiler offers a command line to produce assembly code. In other words, the compiler stops short of actually spitting out machine code. I know the option exists, but I've only seen it referenced by other people; I don't know what it is off the top of my head. You'll have to consult the documentation of gcc to find out.

Last edited by Dark_Helmet; 06-24-2005 at 09:45 PM.
 
Old 06-25-2005, 10:36 AM   #5
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Such a Coincidence, I thought of just that last night- making the compiler stop short of producing Assembly Code



Well... since the glibc library is only an implementation of the Standard C Specification, I guess that means two things for me:

1) There is no actual "Standard C Library" issued by ISO- people must produce it themselves based on the specification

and

2) Since people make it themselves... and since GNU has been working on it... then the code has to exist

This is great! Except, being a noob and all (and at my grandma's house, without any of my notes or references... not to mention Linux too) I have absolutely no idea how to get my hands on it. I briefly skimmed at the sites you gave me and found a download for the library. Problem is I have a feeling that is the same machine-code library I have on my computer...

Like I said, I'm looking for the source. I will keep on searching for it, keeping my focus on the GNU site.

And thanks again for the help! You've been a VERY helpful person





And again, if anyone knows the link for the underlying source code of the GNU Implementation of the C Library, then please let me know



Thanks Alot!!!
 
Old 06-25-2005, 11:12 AM   #6
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 69
Quote:
Originally posted by TGWDNGHN
1) There is no actual "Standard C Library" issued by ISO- people must produce it themselves based on the specification
I don't have a copy of the C library standard, but I do have a copy of the C++ standard (BS ISO/IEC 14882:2003). I would assume that the C standard is very similar, but for C.

It literally defines the C++ language, right down to what each keyword does, what the variable scoping rules are, and so on (although some things, like the size of an int, or attempting to read past the end of an array, is left to the implementation to decide). It also defines the standard libraries in the form of “this include file contains these macros and these functions” and describes the effect of each function, including its order of complexity.

Quote:
2) Since people make it themselves... and since GNU has been working on it... then the code has to exist
It would be hard to use C if the library code didn't exist.

Quote:
And again, if anyone knows the link for the underlying source code of the GNU Implementation of the C Library, then please let me know
The GNU implementation of the C compiler is available as part of the GNU Compiler Collection (GCC), which supports many different languages on many different architectures. The source code for GCC version 3.4.4 (latest stable version as I write this, excluding 4.0.0 because this has some issues) is available in a file called /pub/gcc/releases/gcc-3.4.4/gcc-3.4.4.tar.bz2 which you can find on any GCC mirror (http://gcc.gnu.org/mirrors.html). You can also just download the core and C modules if you like. Alternatively, take a look at 4.0.0 because it works slightly differently, especially with optimisation.

The GNU implementation of the C library is in a package called libc, which you can also find on GNU mirrors. Try searching for it on www.freshmeat.net.

I don't advise you to start by reading the source code. It's huge and without some background knowledge you won't understand how it all fits together. Take a look at the documentation on http://gcc.gnu.org/ first!
 
Old 06-25-2005, 11:23 AM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
The links I gave do give you access to the source code. On both pages there are links to "Tar/Gz" or "Tar/Bz2". Those are the source code tarballs. Specifically, the link on the page for glibc takes you here: ftp://ftp.gnu.org/gnu/glibc/

That page is a list of files. All of the *.tar.gz or *.tar.bz2 files are source code tarballs. I downloaded and extracted one just now to make sure.

If you're not familiar with tar, you extract a file like so:

For *.tar.gz:
tar xvzf <filename>

For *.tar.bz2
tar xvjf <filename>

After that, you'll have lots of subdirectories that organize the code into logical groups. For instance, there's a "strings" subdirectory that will contain the source code for string handling functions.

I would agree with rjlee though. If you want to look directly at the source code, be prepared for a steep learning curve.

Last edited by Dark_Helmet; 06-25-2005 at 11:27 AM.
 
Old 06-25-2005, 01:19 PM   #8
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Thank you so much! Since I'm not using my own computer (once again, granny's house) I didn't really have much time to even look at the site in any detail, but I did see the files for download. When I get home I'll make good note of this



And I am sure it can be a steep learning curve, but I'm a fast learner and I'm actually very eager to start learning assembly. Now I am by no means trying to be a Braggart, but as a 15 year old taking Calculus next year I think I am more than qualified to deal with the math and logic Assembly requires. I know this is stupid (and emberessing) but I work in binary everyday just trying to solve everyday applications. I'm familiar with the fact that I'll have to deal with the data in the registers, having to jump back and forth between them and having to check their status, as well as saving and restoring their states, but I'm not concerned.


I was planning to write generic, bullet-proof functions in Assembly and Use them C for the "Think Low, Write High" approach of programming. As for the C Library (and much of the language itself) I need to see a few things... things I've always wondered and questioned

Well anyways, thank you two so much! Sorry for being such a , but I had no choice . This will help encourage me to pursue my studies in the Linux and Programming Field. I usually lose my mind when I have to sense of direction, so this will help orient me to reach me goal.


I will take note of your assistance, and maybe I can repay both of you back some day


Thanks Again!

(PS: I think that answers my questions for now... but I may return !)
 
Old 06-25-2005, 02:15 PM   #9
thandermax
Member
 
Registered: Jun 2005
Distribution: OpenSUSE 10.3 , 11.2 , Solaris 10, Ubuntu 9.10
Posts: 84

Rep: Reputation: 16
Good disassembler with source hightliting

Check out the following windows based disassembler "IDA Pro 4.5" (Interractive DisAssembler Pro)

from www.datarescue.com


Run It with WINE and it works.

It can detect library functions (almost all), support numerous executable file format such as ELF,PE,DOS, NE,COFF even 6811,6502 microcontrollers !

It's FLIRT engine can detect the compiler used to compile it and functions such as printf,scanf,even GTK, Qt ,Java, etc.

It's syntex highliting feature is very good ,suppose if any interrupt is issued it will detect the what sub function called, port number ,dma etc.


Gonna check it

But sorry it's not free, but you can get a FREE workable demo with plenty of features
 
Old 06-25-2005, 02:48 PM   #10
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 69
Re: Good disassembler with source hightliting

Quote:
Originally posted by thandermax
Check out the following windows based disassembler "IDA Pro 4.5" (Interractive DisAssembler Pro)

from www.datarescue.com
For alternatives, see ftp://ftp.mirrorservice.org/sites/so...-3.4.4.tar.bz2

There are quite a few disassemblers out there, but be aware that they can only translate object code into assembler or into some semblance of source code; what you get out is usually very different to the original source (although it will compile to a functionally equivalent version, probably with different timing specifics).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
djgpp c++ cross compiler cannot read library functions written in C back2morrie Linux - General 0 05-31-2005 03:43 AM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 05:20 AM
Exported functions from C shared libraries tim_l Programming 4 05-25-2004 10:06 AM
volume management library functions on Linux vtluu Red Hat 0 04-09-2004 03:05 AM
Hide local functions within a library generated by 'ar' relzok Linux - Newbie 0 03-31-2004 04:51 PM


All times are GMT -5. The time now is 05:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration