flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Does fasm support inlining of C "static inline" fu

Author
Thread Post new topic Reply to topic
Libby



Joined: 27 Jul 2016
Posts: 3
Libby 27 Jul 2016, 15:01
Hello! im wondering does fasm support the inlining of "static inline" functions from C libs? i mean i know it can call them ok, but in all my tests they were just that - a call to a single procedure, not _inlined_. So I don't know if i'm just doing something wrong or if fasm simply doesn't support it?

Ive built the .o file with gcc and tried a few different keyword variations and compiler flags such as -std=c99 without luck.

Here's my little test file, inlined.c file:
Code:
#include "inlined.h"
extern unsigned long int add3(unsigned long int dword1);    
and inlined.h:
Code:
static inline unsigned long int add3(unsigned long int dword1) {
  return dword1 + 3;
}    
which i turn into an object file with: gcc -c -std=c99
Post 27 Jul 2016, 15:01
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 27 Jul 2016, 16:25
I think function inline is compiler thing, it will just do some "tricks" and incorporate "add3" to your code, without calling it.

The only way I can think of inlining functions in FASM is by using macros. Or repeat each code you need, manually.
Post 27 Jul 2016, 16:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 27 Jul 2016, 20:42
fasm can create an object file for linking with other objects. That's probably about as close as you will get with having some external code inlined with any C compiler. For inlining to work the compiler has to know the code, and it can't know anything from just the object file.
Post 27 Jul 2016, 20:42
View user's profile Send private message Visit poster's website Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 435
Location: Australia
redsock 27 Jul 2016, 23:19
I'll chime in here, despite your test inlined.{c,h} not really making a lot of sense to me.

I think what you are really looking for is this: http://gcc.gnu.org/wiki/LinkTimeOptimization. Way back in the olden days, the MIPSPro compiler team did a fantastic job with inter-procedural optimizations, and now most compilers do similarly. The gcc options -fwhole-program and -flto are the meaty bits.

This is to say that I can create a fasm-generated .o file with exported public symbols, and include them in a C/C++ project. If I then use link time optimization, gcc will do its best to optimize even fasm-produced code samples. Whether it eliminates calls and chooses inlining or not is very much compiler-specific.

_________________
2 Ton Digital - https://2ton.com.au/
Post 27 Jul 2016, 23:19
View user's profile Send private message Reply with quote
Libby



Joined: 27 Jul 2016
Posts: 3
Libby 28 Jul 2016, 06:45
thankyou very much everyone for feedback so far! Smile

redsock, I want to go the other way though - I dont want to create a fasm .o and call from gcc ... I want to call my gcc-created .o from fasm, and hopefully have fasm inline the calls that i've declared to be "static inline" in the C source Smile
Post 28 Jul 2016, 06:45
View user's profile Send private message Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 257
Trinitek 28 Jul 2016, 08:19
Oh, I had a hard time understanding your posts. You want GCC to export functions that don't have the stack setup/cleanup/return so you can use them in FASM as if they were FASM macros.
Post 28 Jul 2016, 08:19
View user's profile Send private message Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 435
Location: Australia
redsock 28 Jul 2016, 21:44
Libby wrote:
redsock, I want to go the other way though - I dont want to create a fasm .o and call from gcc ... I want to call my gcc-created .o from fasm, and hopefully have fasm inline the calls that i've declared to be "static inline" in the C source Smile
The effects of "static inline" don't really allow you to accomplish what you want, and fasm doesn't have anything to do with it.

To demonstrate this effect, I too created a source file:
Code:
#include <cstdio>
#include <cstdlib>

static inline unsigned long int add3(unsigned long int dword1) {
        return dword1 + 3;
}

void export_function() {
        printf("%ld\n", add3(rand()));
}    
If I compile this with optimization flags, the compiler rightfully inlines the add3 function and most importantly does not export it. (And this is the crux of your problem). No amount of external language/fasm/etc trickery will undo this:
Code:
Disassembly of section .text:

0000000000000000 <_Z15export_functionv>:
   0:   48 83 ec 08             sub    rsp,0x8
   4:   e8 00 00 00 00          call   9 <_Z15export_functionv+0x9>
                        5: R_X86_64_PC32        rand-0x4
   9:   48 63 f0                movsxd rsi,eax
   c:   48 83 c6 03             add    rsi,0x3
  10:   bf 00 00 00 00          mov    edi,0x0
                        11: R_X86_64_32 .rodata.str1.1
  15:   b8 00 00 00 00          mov    eax,0x0
  1a:   e8 00 00 00 00          call   1f <_Z15export_functionv+0x1f>
                        1b: R_X86_64_PC32       printf-0x4
  1f:   48 83 c4 08             add    rsp,0x8
  23:   c3                      ret    
    
That was compiled with g++ -O -c, and the only exported symbol is the export_function as it should be. If I remove the optimizing flag -O, then it ignores the "static inline" keyword and indeed exports the add3 function:
Code:
Disassembly of section .text:

0000000000000000 <_ZL4add3m>:
   0:   55                      push   rbp
   1:   48 89 e5                mov    rbp,rsp
   4:   48 89 7d f8             mov    QWORD PTR [rbp-0x8],rdi
   8:   48 8b 45 f8             mov    rax,QWORD PTR [rbp-0x8]
   c:   48 83 c0 03             add    rax,0x3
  10:   5d                      pop    rbp
  11:   c3                      ret    

0000000000000012 <_Z15export_functionv>:
  12:   55                      push   rbp
  13:   48 89 e5                mov    rbp,rsp
  16:   e8 00 00 00 00          call   1b <_Z15export_functionv+0x9>
                        17: R_X86_64_PC32       rand-0x4
  1b:   48 98                   cdqe   
  1d:   48 89 c7                mov    rdi,rax
  20:   e8 db ff ff ff          call   0 <_ZL4add3m>
  25:   48 89 c6                mov    rsi,rax
  28:   bf 00 00 00 00          mov    edi,0x0
                        29: R_X86_64_32 .rodata
  2d:   b8 00 00 00 00          mov    eax,0x0
  32:   e8 00 00 00 00          call   37 <_Z15export_functionv+0x25>
                        33: R_X86_64_PC32       printf-0x4
  37:   5d                      pop    rbp
  38:   c3                      ret    
    
So you can see, any "static inline" optimized code built in C/C++ is simply not callable from any outside linked environment and fasm is no exception.

The only way fasm (or any external lang) could detect your "static inline" keywords is if it were also a C/C++ compiler, and that doesn't make any sense IMO.

The above objdump code hopefully highlights the issue with what you are after.

_________________
2 Ton Digital - https://2ton.com.au/
Post 28 Jul 2016, 21:44
View user's profile Send private message Reply with quote
Libby



Joined: 27 Jul 2016
Posts: 3
Libby 29 Jul 2016, 07:35
well that's disappointing, but understandable. Thankyou very much for the detailed explanation, very much appreciated!!! Smile
Post 29 Jul 2016, 07:35
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.