flat assembler
Message board for the users of flat assembler.

Index > Main > Combining gcc generated .obj with FASM generated .obj

Author
Thread Post new topic Reply to topic
kamac



Joined: 06 Dec 2013
Posts: 12
kamac
Hi.

I'm trying to merge C++ code with FASM code.

For this purpose, I have following C++ code:
Code:
#include <sstream>
#include <string>

const char* strToInt(int i) {
        std::stringstream out;
        out << i;
        return out.str().c_str();
}    


And the following call to it in ASM:

Code:
format MS COFF
extrn '__imp__printf' as printf:DWORD
extrn '__imp__ExitProcess@4' as ExitProcess:DWORD
extrn strToInt

section '.text' code
dbgTxt: db "Number: %d",0
_start:
    call main
main:
    push ebp
    mov ebp,esp
    push esi
    push edi

    push 78
    call strToInt
    add esp,4
    push eax
    push dbgTxt
    call [printf]
    add esp,8

    call [ExitProcess]
    pop edi
    pop esi
    mov esp,ebp
    pop ebp
    ret    


I compile the C++ code with:
Code:
g++ -c cppFile.cpp    

Which produces me a cppFile.o

Now, I compile ASM:
Code:
FASM testApp.ASM testApp.OBJ    



Then, I link it with ld.exe:
Code:
ld.exe testApp.OBJ cppFile.OBJ libkernel32.a libmsvcrt.a libuser32.a libstdc++.a    


And this is where the problem is. Linking throws me:
Code:
testApp.OBJ:(.text+0x3b): undefined reference to `intToStr'
cppFile.OBJ:cppFile.cpp:(.text+0x9e): undefined reference to `_Unwind_Re
sume'
libstdc++.a(eh_personality.o):(.text$_ZL28read_encoded_value_with_basehjPKhPj+0x
c1): undefined reference to `abort'
libstdc++.a(eh_personality.o):(.text$_ZL15get_ttype_entryP16lsda_header_infom+0x
61): undefined reference to `abort'
ld.exe: libstdc++.a(eh_personality.o): bad reloc address 0x61 in section `.text$
_ZL15get_ttype_entryP16lsda_header_infom'
ld.exe: final link failed: Invalid operation    


I'm using mingw. Any thoughts?
Post 07 Dec 2013, 18:07
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17664
Location: In your JS exploiting you and your system
revolution
At a minimum you will have to define strToInt, printf and ExitProcess using extrn.
Post 07 Dec 2013, 18:13
View user's profile Send private message Visit poster's website Reply with quote
kamac



Joined: 06 Dec 2013
Posts: 12
kamac
But it's there already.

Code:
extrn '__imp__printf' as printf:DWORD 
extrn '__imp__ExitProcess@4' as ExitProcess:DWORD 
extrn strToInt
    


From what I read, it could be due to mixing of SJLJ and DW2.. But that's all I know.
Post 07 Dec 2013, 18:19
View user's profile Send private message Reply with quote
eax



Joined: 02 Dec 2013
Posts: 1
eax
great question, i was wondering about foreign functions with FASM myself.
Post 07 Dec 2013, 19:04
View user's profile Send private message Reply with quote
dancho



Joined: 06 Mar 2011
Posts: 74
dancho
how to mix C++(VS) and fasm(obj) :

1.) in VS create new console project
2.) copy this code
Code:
#include <iostream>
#include <conio.h>
using namespace std;

// proto for the extern func
extern "C" unsigned int AddFunc(unsigned int x, unsigned int y);

int main(void)
{
        
    cout << "10 + 20 = " << AddFunc(10,20) << endl;
        _getch();

    return 0;
}
    

3.) open fasmw copy and save as AddFunc.asm , compile it
Code:
format MS COFF
include 'win32axp.inc'

public  AddFunc as '_AddFunc'

section '.code' code readable executable
                
                proc AddFunc c p1,p2
                        
                        mov eax,[p1]
                        add eax,[p2]
                        
                        ret
                endp            
                
                
section '.data' data readable writeable
                nop
    

4.) copy AddFunc.obj into main VS directory
5.) open project properties and under linker there is an input item,
subitem additional dependencies , write AddFunc.obj here
6.) compile and run
Post 08 Dec 2013, 19:55
View user's profile Send private message Reply with quote
kamac



Joined: 06 Dec 2013
Posts: 12
kamac
Though, I needed it the other way around Wink (combine c/c++ .obj files with ASM's .obj files and thus link them together with something like ld)
Post 09 Dec 2013, 16:16
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Given that this is C++ you need the proper compiler "hash".

Comment out the C++ code, declare as extern and make a call to it in C++:
Code:
extern const char* strToInt(int i);
//{
//      return NULL;
//(void)i;
//}
// ...
strToInt(0);    
The compiler will complain:
Code:
Error   1       error LNK2001: unresolved external symbol "char const * __cdecl strToInt(int)" (?strToInt@@YAPEBDH@Z)   main.obj        1       1       aluminium
    
You then take the "?strToInt@@YAPEBDH@Z" and use it for your FASM extrn:
Code:
extrn '?strToInt@@YAPEBDH@Z' as strToInt:DWORD    
Uncomment the strToInt in C++ and remove the extern and dummy call. It should now link.
Post 09 Dec 2013, 18:06
View user's profile Send private message Reply with quote
dancho



Joined: 06 Mar 2011
Posts: 74
dancho
kamac wrote:
Though, I needed it the other way around Wink (combine c/c++ .obj files with ASM's .obj files and thus link them together with something like ld)


ahh yes , sry , just ignore my post then...
Post 09 Dec 2013, 18:28
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.