How to find the main function of my program (...)?

Currently, I am transferring a project with several hundred code files and dependencies to several third-party libraries in Mac Os. I finally got to the point that the program compiles without warnings or errors, but it does not seem to perform my own main function.

Instead, it seems to perform some other basic function, which seems to belong to a third party. This function writes some diagnostic data to the console and displays them later:

(gdb) continue Current language: auto; currently c++ // // This is an automatically generated file. // Do not edit. // const unsigned short expTable[] = { 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, ... 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, }; Debugger stopped. Program exited with status value:0. 

I cannot use the debugger to find out where this main function is located, because although the stack trace seems valid, gdb does not show me the correct line number and file name for each stack entry (see this unresolved question ).

The search took several minutes, but yielded no results.

My project uses SDL among other libraries, but I reward SDL_Main () and the main problems and built my project on top of the perfectly working SDL project template. Therefore, I am absolutely sure that my main function is valid.

Do you have any ideas what might be wrong? I currently have no ideas on how to find and remove the main scam function.

Thanks,

Adrian

EDIT: As I just found out, I made a mistake when searching for files with the string "This is automatically generated." I just found dozens of files with one line, all of which belong to FreeImage, one of the third-party libraries that I use. So the problem seems to be related to FreeImage, but I don’t yet know how to do this, since I compiled Freeimage as a library with the MacOs make file attached and included only the library. I will try to rebuild a newer version of FreeImage and see it if this fixes my problem.

+6
c ++ gcc unix gdb
source share
10 answers

Could this be an initializer for a static object that does not work before calling main ()?

+8
source share

Do you have some basic binary format? Try using nm on it. (this should not be possible, since ld will not be associated with duplicates, but go to the dynamic libraries and find _main there)

 nm a.out | grep -5 _main 

This should give 5 lines before and after any _main found in binary a.out

If you have several, look at the surrounding characters for clues about what parts they are in ...

The next step is to do the same for each dynamic library used. For a list of dynamic libraries to use, use otool

 otool -L a.out 
+3
source share

I'm not sure how to find another, but you can specify your own entry point explicitly and make the other unoccupied. You can use the GNU linker ld -e option to set your entry point.

-e record

- record = record

Use a record as an explicit character to start your execution and not the default. If there is no character named entry, the linker will try to parse the record as a number and use this as the address of the record (the number will be interpreted in base 10; you can use leading 0x for base 16 or leading 0 for base 8).

For future readers, if you have this problem in Windows. Equivalent Linker / ENTRY Option .

+2
source share

Try to run the executable through nm ? You may be able to give you some advice. I would not think that you can associate a program with more than one globally visible function called main() , I’m not sure how this will happen.

+1
source share

Browse through the header files that you included and see if there is a definition that reassigns main to another. This is an old trick to first call the main library function to do some tweaking. Typically, it will eventually call your main function by accessing an overridden value.

+1
source share

Quick Hack:

 readelf -s -w my_bin_file > temp.txt 

Open temp.txt, find the main one (with FUNC in one column) Come up until you find the first FILE column - this is the file with the associated main.

edit: This only works with GNU Unix accessories and friends. OS X uses the Mach-O format, not ELF.

+1
source share

I know that in C you might have another entry point, called before the main function, which might be an idea. The code usually looks like this:

 void __attribute__ ((constructor)) my_main(void); 

Perhaps you can find something similar in your code.

In C, there are also various ways to catch the main function and call it after the "real" main. Some thread libraries have hacks to “prepare” the environment, scheduler, and the like.

This is not very useful, but it may explain why your main one is not called at all.

Hope this helps!

0
source share

One more note.

WxWidgets also define their own main

From here

As with all programs, a “core” function must exist. Under wxWidgets, main is implemented using this macro, which creates an instance of the application and runs the program.

IMPLEMENT_APP(MyApp)

0
source share

It looks like you may have a file called b44ExpLogTable.cpp compiled into your binary or some third party library. It looks like this little program is designed to create an exp () table, but it is somehow imported into your project (s)

See this and this in FreeImage sources

0
source share

Create a map file. Most programs do not really start with the core. The map file from GCC should tell you the address __start or __executable_start, with which you can break through and go to find out what will lead to the exit of your program.

0
source share

All Articles