Author Topic: Relocatable code for .SO and .DLL libraries (Mixed C/C++/ASM)  (Read 4916 times)

Offline kanito73

  • Jr. Member
  • *
  • Posts: 10
Relocatable code for .SO and .DLL libraries (Mixed C/C++/ASM)
« on: February 17, 2019, 04:19:44 AM »
Hello

I am developing a C++ library that requires some external assembly functions to be included.

Currently, the C/C++ functions are being declared this way (generic format, not the exact code):

-------------------------------------------------------------------
#if defined _WIN32
   #define DLL_ENTITY __declspec(dllexport)
#endif

#if defined _WIN32
   DLL_ENTITY
#endif
int Function (int argument);
-------------------------------------------------------------------

and compile it (using a Makefile) with GCC using the -fPIC flag to create relocatable code that can be used from the programs linked to my library. For example (one command output of my Makefile):

   g++ -I`pwd`/.. -Wall -fPIC -march=x86-64 -mtune=generic -g -c sort.cpp

and in Windows I create and configure a project with executable format DLL option in Visual Studio, then it does all the job.

Okay, my assembly functions look like this:

-------------------------------------------------------------------
global function
global _function
function:
_function:
      ENTER reserved_bytes,nest_level
      ; ...get possible parameters...
      ; ...do something...
      LEAVE
      ret
-------------------------------------------------------------------

Well, according to the NASM manual, for the Windows DLL libraries I must add something like:

   export function

My doubts are these:

1) For Linux it does not mention nothing about 'export', I guess it is the same way as the C/C++ function/class prototypes that do not require any special treatment, they are declared in the same way as in standalone programs. Is it right? Just use 'export' for Windows and nothing for Linux?

2) What about the relocatable code generation? For Windows, the 'export' keyword makes it relocatable or JUST EXPORTABLE? And for Linux, do I need to use some flag equivalent to -fPIC or I must create the relocatable code by using BASED addressing? For example:

      add WORD[BP+myNumber],10h

         instead of

      add WORD[myNumber],10h

but in this case, how can I find the base address of the function to set BP (EBP/RBP) to it?


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Relocatable code for .SO and .DLL libraries (Mixed C/C++/ASM)
« Reply #1 on: February 17, 2019, 11:56:58 PM »
As I understand it, a .dll can be relocated. An .so can not, and must  be position independent code. If "fPIC" works, you're probably okay. I am not at all sure of this!

Best,
Frank


Offline sonictk

  • Jr. Member
  • *
  • Posts: 5
Re: Relocatable code for .SO and .DLL libraries (Mixed C/C++/ASM)
« Reply #2 on: February 18, 2019, 01:45:14 AM »
Quote
Well, according to the NASM manual, for the Windows DLL libraries I must add something like:

   export function

Are you talking about in your assembly code, or to the linker? The linker `/export` flag just telling the linker to make sure that your symbols are exported and available to other programs. This is because on Windows, symbols are _not_ available to interrogating processes by default, whereas on Linux they _are_. I don't think it has anything to do with relocatable code/PIC at all; all code in Windows x64 ISA is PIC when compiled via MSVC.

On Linux, while you CAN manually rebase your code, chances are you probably shouldn't, especially on x64 architecture, since you're probably forcing the loader to jump through extra hoops (since your .so would never load at its preferred base address anyway). See: https://blogs.msdn.microsoft.com/oldnewthing/20170120-00/?p=95225

Again, take this with a grain of salt, since I'm not an expert on the topic.
« Last Edit: February 18, 2019, 01:47:52 AM by sonictk »