Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list functions present in object file?

Tags:

I am trying to understand the compiling and linking process in C/C++. I know that the source files are first converted into object files by the compiler. The linker then generated libraries or executables from the object files.

I am trying to first read the information in object files. Here is the program I have written for experimentation.

func.h

#include <iostream>

void beautifulprint(char *str);

func.cpp

#include "stdafx.h"
#include "func.h"

using namespace std;

void beautifulprint(char *str) {
    cout << "*** " << str << " ***" << endl;
}

TestApp.cpp

#include "stdafx.h"
#include "func.h"

int _tmain(int argc, _TCHAR* argv[])
{
    beautifulprint("Hello, world!");

    return 0;
}

After building the project in VS 2010 I get func.obj. I assumed that somewhere in func.obj there should be a reference for beautifulprint function. I ran the below for both debug and release versions of func.obj

dumpbin /HEADERS func.obj > funchead.txt

Below is the output.

Debug version (Full output not included as it is very large)

...

SECTION HEADER #41
   .text name
       0 physical address
       0 virtual address
      78 size of raw data
    5B94 file pointer to raw data (00005B94 to 00005C0B)
    5C0C file pointer to relocation table
       0 file pointer to line numbers
       A number of relocations
       0 number of line numbers
60501020 flags
         Code
         COMDAT; sym= "void __cdecl beautifulprint(char *)" (?beautifulprint@@YAXPAD@Z)
         16 byte align
         Execute Read

SECTION HEADER #42
.debug$S name
       0 physical address
       0 virtual address
      E8 size of raw data
    5C70 file pointer to raw data (00005C70 to 00005D57)
    5D58 file pointer to relocation table
       0 file pointer to line numbers
       7 number of relocations
       0 number of line numbers
42101040 flags
         Initialized Data
         COMDAT (no symbol)
         Discardable
         1 byte align
         Read Only

...

Release version (Complete output!)

Microsoft (R) COFF/PE Dumper Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file func.obj

File Type: ANONYMOUS OBJECT

ANON OBJECT HEADER VALUES
               1 version
             14C machine (x86)
        50213733 time date stamp Tue Aug 07 16:41:39 2012
                 ClassID: {0CB3FE38-D9A5-4DAB-AC9B-D6B6222653C2}
             2D1 size
               0 flags

And here are my questions. As expected, func.obj Debug version has a reference to beautifulprint. But to my astonishment there is no reference to beautifulprint in Release version? How would the linker know that this function is present in the object file.

Also why does the debug obj version have functions from iostream? Shouldn't these be present in some standard library instead of this obj?

Edit: I directly opened the release func.obj in VS 2010 in Hex format. I searched for beautifulprint and its present on the right hand side (ASCII) column. It means the reference is present. Why does dumpbin not show it? I need some tool to view it in human readable format.

like image 763
Cracker Avatar asked Aug 07 '12 16:08

Cracker


People also ask

How do I find the symbol in an .O file?

If so, you need to use the respective nm or objdump commmand. For example, if you have used XXX-YYY-gcc to compile the .o file, you need to use XXX-YYY-nm or XXX-YYY-objdump to process the files.

How can I see the code of an object in Linux?

Check out the binutils package. In it you'll find objdump - display information from object files. Then try running objdump -d xyz.o .


2 Answers

Dump the symbols instead. All .obj files have a symbol table. It will show you those symbols defined internally, and those that need resolution. The IO symbols you see may be UNDEF symbols, but the symbol table should make it more clear.

DUMPBIN /SYMBOLS func.obj

Keep in mind that /SYMBOLS is not available when the object is compiled with /GL (whole program optimization). Object modules created with /GL (as well as libraries) are created with a format that is not guaranteed to be compatible from one compiler version to the next.

Whole Program Optimization means that the optimizer can optimize across all modules, as opposed to just within each module. Functions can become "inline" and other tricks performed that presumably are not very COFF compatible. It is recommended that deliverable libraries not have the /GL option set unless you are supplying libraries for all supported compiler versions.

like image 97
Les Avatar answered Oct 04 '22 09:10

Les


I used objdump (in gnu c, windows platform).

    C:\...obj>objdump -t winsock001.o
         
    winsock001.o:     file format pe-x86-64
    SYMBOL TABLE:
    [  0](sec -2)(fl 0x00)(ty   0)(scl 103) (nx 1) 0x0000000000000000 winsock001.c
__main
__imp_WSAStartup
__imp_socket
__imp_htons
__imp_inet_addr
__imp_connect
__imp_closesocket
__imp_WSACleanup
__imp_WSAGetLastError
printf
puts
like image 37
Quark66 Avatar answered Oct 04 '22 09:10

Quark66