3

Linuxに実行可能ファイルがあります-exe

この実行可能ファイルには、コード全体で使用されるいくつかの関数が含まれています。

  • sendMsg
  • debugPrint

.so次に、実行可能ファイルに追加機能を提供するを動的にロードしたいと考えています。

この共有ライブラリには、 と のヘッダーが含まれsendMsgていdebugPrintます。

この共有ライブラリを で読み込みdlopen()、 で API を作成しますdlsym()

ただし、読み込み時にすべてのシンボルを解決するためdlopen()に使用します。RTLD_NOW

sendMsgシンボルが見つからないと言って失敗します。

sendMsg.cがコンパイルされているため、このシンボルは実行可能ファイルに含まれている必要があります。

ただし、実行可能ファイルはmakeプロセスによって削除されます。そのdlopenため、シンボルを見つけることができないのは理にかなっています。

どうすればこの状況を解決できますか?

  • 共有関数を静的ライブラリに構築し、その静的ライブラリをexeと の両方にリンクできます.so。これにより、コードサイズが増加します:(
  • のストリッピングを削除しexeて、シンボルを見つけることができます
  • 私が知らないコンパイル時のリンクマジックを実行して.so、シンボルがどこにあるかを知っていますexe
4

1 に答える 1

5

man ld:

   -E
   --export-dynamic
   --no-export-dynamic
       When  creating  a  dynamically  linked  executable, using the -E option or the --export-dynamic option causes the linker to add all symbols to the dynamic symbol table.  The
       dynamic symbol table is the set of symbols which are visible from dynamic objects at run time.

       If you do not use either of these options (or use the --no-export-dynamic option to restore the default behavior), the dynamic symbol table will normally contain only  those
       symbols which are referenced by some dynamic object mentioned in the link.

       If  you  use "dlopen" to load a dynamic object which needs to refer back to the symbols defined by the program, rather than some other dynamic object, then you will probably
       need to use this option when linking the program itself.

       You can also use the dynamic list to control what symbols should be added to  the  dynamic  symbol  table  if  the  output  format  supports  it.   See  the  description  of
       --dynamic-list.

       Note  that  this  option  is  specific  to  ELF  targeted  ports.   PE  targets  support  a  similar function to export all symbols from a DLL or EXE; see the description of
       --export-all-symbols below.

オプションを gcc/g++ に渡すこともでき-rdynamicます (コメントに記載されているとおり)。makeスクリプトのセットアップ方法によっては、これが便利です

于 2011-05-25T09:01:27.850 に答える