Можно ли создать переносимый исполняемый файл x86-64 Linux с компилятором ocamlopt gh c и g cc? - PullRequest
1 голос
/ 28 мая 2020

в моем дистрибутиве NixOS x86-64 я написал четыре «привет, мир!» приложения, использующие:

  • компилятор ocamlopt
  • gh c компилятор
  • g cc компилятор
  • в качестве компилятора

мой make-файл:

  ocaml:
    ocamlopt mytest.ml -o ocaml_test

  haskell:
    ghc mytest.hs -o haskell_test

  ansiC:
    gcc mytest.c -o ansiC_test 

  asm:
    as -o asm_test.o mytest.s 
    ld -s -o asm_test asm_test.o

и все четыре работают нормально на собственной машине

, но я не могу выполнить первые три приложения на другом x86-64 Linux (чтобы быть точный - Lubuntu x86-64) компьютер - мои приложения не могут найти системные библиотеки на целевой машине, поэтому система сообщает мне, что файл не существует

, но файл существует, и я могу прочитать его с помощью утилиты readelf:


для g cc созданный исполняемый файл:

$> readelf -l ansiC_test 

Elf file type is EXEC (Executable file)
Entry point 0x401040
There are 11 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x0000000000000268 0x0000000000000268  R      0x8
  INTERP         0x00000000000002a8 0x00000000004002a8 0x00000000004002a8
                 0x0000000000000050 0x0000000000000050  R      0x1
      [Requesting program interpreter: /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x00000000000004f0 0x00000000000004f0  R      0x1000

$> ldd ansiC_test 
  linux-vdso.so.1 (0x00007ffee735f000)
  libc.so.6 => /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libc.so.6 (0x00007f8db6ca8000)
  /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/ld-linux-x86-64.so.2 => /nix/store/xvj2l5llpmcgx99cl37v5xqpazs38195-glibc-multi-2.27/lib/ld-linux-x86-64.so.2 (0x00007f8db6e60000)

для gh c созданный исполняемый файл:

$> readelf -l haskell_test

Elf file type is EXEC (Executable file)
Entry point 0x404c70
There are 11 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x0000000000000268 0x0000000000000268  R      0x8
  INTERP         0x00000000000002a8 0x00000000004002a8 0x00000000004002a8
                 0x0000000000000050 0x0000000000000050  R      0x1
      [Requesting program interpreter: /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000

$> ldd haskell_test 
  linux-vdso.so.1 (0x00007ffe7840a000)
  libm.so.6 => /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libm.so.6 (0x00007f13eb093000)
  libgmp.so.10 => /nix/store/7mrq5v4nnqfkv1ka7a0kdw9mzvqs5w3c-gmp-6.1.2/lib/libgmp.so.10 (0x00007f13eaffd000)
  librt.so.1 => /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/librt.so.1 (0x00007f13eaff3000)
  libdl.so.2 => /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libdl.so.2 (0x00007f13eafee000)
  libffi.so.6 => /nix/store/kbbiky87bsdndh63m7919sxkf3gsh5zq-libffi-3.2.1/lib/libffi.so.6 (0x00007f13eafe1000)
  libpthread.so.0 => /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0 (0x00007f13eafbe000)
  libc.so.6 => /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libc.so.6 (0x00007f13eae08000)
  /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/ld-linux-x86-64.so.2 => /nix/store/xvj2l5llpmcgx99cl37v5xqpazs38195-glibc-multi-2.27/lib/ld-linux-x86-64.so.2 (0x00007f13eb22b000)

для ocamlopt созданный исполняемый файл:

$> readelf -l ocaml_test

Elf file type is EXEC (Executable file)
Entry point 0x410730
There are 11 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x0000000000000268 0x0000000000000268  R      0x8
  INTERP         0x00000000000002a8 0x00000000004002a8 0x00000000004002a8
                 0x0000000000000050 0x0000000000000050  R      0x1
      [Requesting program interpreter: /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x000000000000fb58 0x000000000000fb58  R      0x1000
$> ldd ocaml_test 
  linux-vdso.so.1 (0x00007ffc90657000)
  libm.so.6 => /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libm.so.6 (0x00007f1b01f0b000)
  libdl.so.2 => /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libdl.so.2 (0x00007f1b01f06000)
  libc.so.6 => /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libc.so.6 (0x00007f1b01d50000)
  /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/ld-linux-x86-64.so.2 => /nix/store/xvj2l5llpmcgx99cl37v5xqpazs38195-glibc-multi-2.27/lib/ld-linux-x86-64.so.2 (0x00007f1b020a3000)

, и у меня нет этой проблемы с исполняемым файлом, созданным ассемблером:

$> readelf -l asm_test

Elf file type is EXEC (Executable file)
Entry point 0x401000
There are 4 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x0000000000000140 0x0000000000000140  R      0x1000
  LOAD           0x0000000000001000 0x0000000000401000 0x0000000000401000
                 0x0000000000000022 0x0000000000000022  R E    0x1000
  LOAD           0x0000000000002000 0x0000000000402000 0x0000000000402000
                 0x000000000000000e 0x000000000000000e  RW     0x1000
  NOTE           0x0000000000000120 0x0000000000400120 0x0000000000400120
                 0x0000000000000020 0x0000000000000020  R      0x8

 Section to Segment mapping:
  Segment Sections...
   00     .note.gnu.property
   01     .text
   02     .data
   03     .note.gnu.property


$> ldd asm_test
  not a dynamic executable

, так как вы не видите ссылок на glib c .so.x библиотека


у меня вопрос: можно ли создать такое "не динамическое c исполняемое" приложение f ile с g cc, gh c и окамлоптом? какие опции компилятора? может быть, некоторые библиотеки в папке приложения?

У меня нет доступа к другой машине - я могу предоставить конечному пользователю только архив tar.gz - вот и все. и абсолютно нет надежды, что пользователь установит gh c или ocaml на свой компьютер

какие-либо предложения?

FYI: sr c files


haskell:

module Main where

main = putStrLn "hello world!"

ocaml:

print_string "hello world!" ;;
print_newline () ;;

ansi C:

#include <stdio.h>

int
main ()
{
  puts ("hello world!") ;
}

asm:

.code64
.global _start 

.text 

_start:
          movl    $len, %edx
          movl    $msg, %ecx
          movl    $1, %ebx 
          movl    $4, %eax 
          int     $0x80   

          movl    $0, %ebx       
          movl    $1, %eax      
          int     $0x80        

.data     

         msg: .ascii    "hello, world!\n"
         len = . - msg 

1 Ответ

1 голос
/ 28 мая 2020

Сам не тестировал, но предполагаю, что подойдет комбинация -ccopt ( из ocamlopt) и -static ( из gcc) трюк.

  • из ocamlopt вручную
-ccopt option
    Pass the given option to the C compiler and linker. For instance,-ccopt -Ldir causes the C linker to search for C libraries in directory dir. 
  • из gcc вручную
-static
    On systems that support dynamic linking, this overrides -pie and prevents linking with the shared libraries. On other systems, this option has no effect.

...