flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > High Level Languages > [c help] called object is not a function or function pointer

Author
Thread Post new topic Reply to topic
kerr



Joined: 24 Feb 2016
Posts: 124
[c help] called object is not a function or function pointer
I use the FASM compiler to extract the binary to the gcc compiler to use, prompt error pointer.
How to do? can you help me ? thank you!

by the way, fasm what debugger to use?



Code:

#include <stdio.h>
#include <stdlib.h>
int main(){
        char buff[]="\xB0\x04\xB3\x01\x59\x66\xBA\x10\x00\xCD\x80\xB0\x01\xB3\x00\xCD\x80\xE8\xEA\xFF\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x21\x0A"
(*(void(*)())buff)();
}
b$ gcc buff.c -o buff
buff.cIn function ‘main’:
buff.c:18:14errorcalled object is not a function or function pointer
  char buff[]="\xB0\x04\xB3\x01\x59\x66\xBA\x10\x00\xCD\x80\xB0\x01\xB3\x00\xCD\x80\xE8\xEA\xFF\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x21\x0A"
              ^




_________________
I hope we will be good friends.
Post 21 May 2016, 05:07
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15233
Location: 1I/ʻOumuamua
Perhaps your google search box is broken? Hehe. I took less than 20 seconds to find an answer. I thought about not posting it but that would be cruel. So since the work is done:

http://stackoverflow.com/questions/28901223/c-casting-char-array-to-an-integer-pointer
Post 21 May 2016, 05:53
View user's profile Send private message Visit poster's website Reply with quote
kerr



Joined: 24 Feb 2016
Posts: 124

revolution wrote:
Perhaps your google search box is broken? Hehe. I took less than 20 seconds to find an answer. I thought about not posting it but that would be cruel. So since the work is done:

http://stackoverflow.com/questions/28901223/c-casting-char-array-to-an-integer-pointer



oh cronies !
you speak is not what i want!

i have read this post!

just not know! where is my program error!

_________________
I hope we will be good friends.
Post 21 May 2016, 06:39
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Oh man, you're just missing a semicolon (at the end of your string). Lol, that's an annoying mistake to spend a lot of time fixing.

Btw, I tested it and it segfaulted. I'm guessing either your code is buggy or the data section might be marked non-executable on my OS (Linux x64).

If all you want is to link to some assembly, there are much easier ways to go about this.

revolution, he's trying to cast to a function pointer, so he can call the array as a function.
Post 21 May 2016, 07:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15233
Location: 1I/ʻOumuamua

Tyler wrote:
Oh man, you're just missing a semicolon (at the end of your string). Lol, that's an annoying mistake to spend a lot of time fixing.

C is so dumb about this IMO. End of line means end of expression, and if you want to extend the line use a backslash. Haha, just my uninformed opinion on C and how it "should" be.

Tyler wrote:
revolution, he's trying to cast to a function pointer, so he can call the array as a function.

Okay, it is all a mystery to me, all those brackets and asterisks just to call some code are mighty confusing.
Post 21 May 2016, 07:19
View user's profile Send private message Visit poster's website Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA

revolution wrote:
C is so dumb about this IMO. End of line means end of expression, and if you want to extend the line use a backslash. Haha, just my uninformed opinion on C and how it "should" be.

I agree, mostly. Python is follows this and is widely regarded as the most aesthetic language there is. (Except if you have unmatched parentheses, then it assumes the expression cross the the line barrier, which is nice since it makes sense.)


revolution wrote:
Okay, it is all a mystery to me, all those brackets and asterisks just to call some code are mighty confusing.

To be fair, it's not really something you should ever need to do. Semantically, OP is creating a character array and trying to "call" the contents of the array as if it were the machine code of a function. Not that I'm a huge fan of C. If I want pretty code, I look to the good functional languages. I've always liked to read Ruby and Scheme. I think I'd love Haskell if only I was smart enough to understand it. Razz
Post 21 May 2016, 07:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15233
Location: 1I/ʻOumuamua

Tyler wrote:
To be fair, it's not really something you should ever need to do. Semantically, OP is creating a character array and trying to "call" the contents of the array as if it were the machine code of a function.

It is a reflection of the desire of the language designers to constrain how the programmer can express their intent. They wanted everyone to do everything the "right" way, and in doing so made it unwieldy to do it any other "wrong" way. But the world is full of unexpected needs and requirements and having such constraints is often awkward and disappointing. And it doesn't help that many C help forums scold people for trying to do something weird, or odd, or non-portable, or something else equally horrendous.
Post 21 May 2016, 07:39
View user's profile Send private message Visit poster's website Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 202
Re: [c help] called object is not a function or function poi

kerr wrote:
I use the FASM compiler to extract the binary to the gcc ompiler to use, prompt error pointer. ....

Code:
  char buff[]="\xB0\x04\xB3\x01\x59\x66\xBA\x10\x00\xCD\x80\xB0\x01\xB3\x00\xCD\x80\xE8\xEA\xFF\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x21\x0A"
  (*(void(*)())buff)();}



Code:

  char buffer[] = "\xB0\x04\xB3\x01\x59\x66\xBA\x10\x00\xCD\x80\xB0\x01\xB3\x00\xCD\x80\xE8\xEA\xFF\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x21\x0A";
int main(){
  void (*buff)(); // define a function pointer variable
  buff = (void *)buffer; // assign the (cast) address of the buffer to it
  buff(); // call the buffer contents as a function
}


Compiled with 03 (maximum optimization)

Code:
  jmp buffer

Of course this particular code in the buffer won't actually work as a function.
To be perfectly correct buff should return an int and main should end with

Code:
  return buff();

and be defined as

Code:
  int(*buff)();


O3 doesn't like the local buffer array and compiles the code as if it didn't exist, whereas when compiled with
optimizations off run time code is used to populate the array. Probably better to define the array as global as
I've done here. Also the stackoverflow example that revolution quoted is simpler as it doesn't involve the
assignment (which O3 ignores anyway).

Code:
return ((int (*)(void))buffer)();


_________________
Mike Gonta
look and see - many look but few see

http://mikegonta.com


Last edited by Mike Gonta on 24 May 2016, 16:08; edited 1 time in total
Post 21 May 2016, 22:57
View user's profile Send private message Reply with quote
kerr



Joined: 24 Feb 2016
Posts: 124

Tyler wrote:
Oh man, you're just missing a semicolon (at the end of your string). Lol, that's an annoying mistake to spend a lot of time fixing.

Btw, I tested it and it segfaulted. I'm guessing either your code is buggy or the data section might be marked non-executable on my OS (Linux x64).

If all you want is to link to some assembly, there are much easier ways to go about this.

revolution, he's trying to cast to a function pointer, so he can call the array as a function.




Yeah
I found

complie program file running to error
Program received signal SIGSEGV, Segmentation fault.
0xbffff30b in ?? ()
i want use gdb debug fasm complie file Feel powerless!gdb can not debug to fasm complie program file. this is my major headache things.

_________________
I hope we will be good friends.
Post 24 May 2016, 04:04
View user's profile Send private message Reply with quote
kerr



Joined: 24 Feb 2016
Posts: 124
Re: [c help] called object is not a function or function poi

Mike Gonta wrote:

kerr wrote:
I use the FASM compiler to extract the binary to the gcc ompiler to use, prompt error pointer. ....

Code:
  char buff[]="\xB0\x04\xB3\x01\x59\x66\xBA\x10\x00\xCD\x80\xB0\x01\xB3\x00\xCD\x80\xE8\xEA\xFF\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x21\x0A"
  (*(void(*)())buff)();}



Code:

  char buffer[] = "\xB0\x04\xB3\x01\x59\x66\xBA\x10\x00\xCD\x80\xB0\x01\xB3\x00\xCD\x80\xE8\xEA\xFF\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x21\x0A";
int main(){
  void (*buff)(); // define a function pointer variable
  buff = (void *)buffer; // assign the (cast) address of the buffer to it
  buff(); // call the buffer contents as a function
}


Compiled with 03 (maximum optimization)

Code:
  jmp buffer

Of course this particular code in the buffer won't actually work as a function.
To be perfectly correct buff should return an int and main should end with

Code:
  return buff();

and be defined as

Code:
  int(*buff)();





thank you
I'm compile still wrong.
The same with my ah.

_________________
I hope we will be good friends.
Post 24 May 2016, 04:11
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 616
If the intention is to hide asm in C then please ignore everything below
----
It's easier (and more flexible i.e. msvc 64 or other architectures) to split the asm and C parts:

my.c

Code:
int main()
{
        my_func();

        return 0;
}

my.fasm

Code:
        format ELF64

        public my_func

my_func:

        ret

Shell:

Code:
rm -v *.exe
rm -v *.o

fasm my.fasm my.fasm.o

gcc -c my.c -o my.c.o

gcc my.c.o my.fasm.o -o my.exe

For gdb simply 'set disassembly-flavor intel' and use 'layout asm' or 'layout split'
Post 24 May 2016, 17:52
View user's profile Send private message Reply with quote
kerr



Joined: 24 Feb 2016
Posts: 124

cod3b453 wrote:
If the intention is to hide asm in C then please ignore everything below
----
It's easier (and more flexible i.e. msvc 64 or other architectures) to split the asm and C parts:

my.c

Code:
int main()
{
        my_func();

        return 0;
}

my.fasm

Code:
        format ELF64

        public my_func

my_func:

        ret

Shell:

Code:
rm -v *.exe
rm -v *.o

fasm my.fasm my.fasm.o

gcc -c my.c -o my.c.o

gcc my.c.o my.fasm.o -o my.exe

For gdb simply 'set disassembly-flavor intel' and use 'layout asm' or 'layout split'



Oh no, You speak This topic.

_________________
I hope we will be good friends.
Post 24 May 2016, 22:58
View user's profile Send private message Reply with quote
kerr



Joined: 24 Feb 2016
Posts: 124
The problem is solved.

Code:

 char *buff="\xB0\x04\xB3\x01\x59\x66\xBA\x10\x00\xCD\x80\xB0\x01\xB3\x00\xCD\x80\xE8\xEA\xFF\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x21\x0A"
  (*(void(*)())buff)();}

this is ok!





thank you !
bingo
Post 25 May 2016, 07:20
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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.