Giter Club home page Giter Club logo

Comments (4)

sayon avatar sayon commented on August 22, 2024

Thank you for this issue! This is indeed an error.

The right sequence should be:

$ nasm -f elf64 -o main.o main.asm
$ nasm -f elf64 -o libso.o libso.asm
$ ld -shared -o libso.so libso.o 
$ ld -o main main.o -d libso.so --dynamic-linker=/lib64/ld-linux-x86-64.so.2

The --dynamic-linker should be provided on the final stage when the executable file is produced, not when creating the shared library itself.

If you compare readelf output on the object file you've created with the new one you will see a difference:

$ # Without dynamic linker
$ ld -o main main.o -d libso.so                                                                                                                                                $ ./main                                                                                                                                                                       bash: no such file or directory: ./main
$ readelf -l main                                                                                                                                                             
Elf file type is EXEC (Executable file)
Entry point 0x400270
There are 5 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x0000000000000118 0x0000000000000118  R E    8
  INTERP         0x0000000000000158 0x0000000000400158 0x0000000000400158
                 0x000000000000000f 0x000000000000000f  R      1
      [Requesting program interpreter: /lib/ld64.so.1]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x0000000000000288 0x0000000000000288  R E    200000
  LOAD           0x0000000000000288 0x0000000000600288 0x0000000000600288
                 0x0000000000000130 0x0000000000000130  RW     200000
  DYNAMIC        0x0000000000000288 0x0000000000600288 0x0000000000600288
                 0x0000000000000110 0x0000000000000110  RW     8

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .hash .dynsym .dynstr .rela.plt .plt .text 
   03     .dynamic .got.plt 
   04     .dynamic 


$ # Now with dynamic linker
$ ld -o main main.o -d libso.so --dynamic-linker=/lib64/ld-linux-x86-64.so.2                                                                                                   
$ ./main                                                                                                                                                                       
Shared object                                                                                                                                                                                                     
$ readelf -l main                                                                                                                                                              sayon@antheus

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

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x0000000000000118 0x0000000000000118  R E    8
  INTERP         0x0000000000000158 0x0000000000400158 0x0000000000400158
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x0000000000000298 0x0000000000000298  R E    200000
  LOAD           0x0000000000000298 0x0000000000600298 0x0000000000600298
                 0x0000000000000130 0x0000000000000130  RW     200000
  DYNAMIC        0x0000000000000298 0x0000000000600298 0x0000000000600298
                 0x0000000000000110 0x0000000000000110  RW     8

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .hash .dynsym .dynstr .rela.plt .plt .text 
   03     .dynamic .got.plt 
   04     .dynamic 

Without -dynamic-linker you are going to silently get a "default" dynamic loader, whose path is hardcoded in ld:

      [Requesting program interpreter: /lib/ld64.so.1]

Most of us do not have a dynamic loader stored with such a name in /lib. That is why the explicit -dynamic-linker option is mandatory in order to obtain a working executable. It changes the relevant field in the resulting ELF program header so that it stores a correct path to the dynamic loader:

      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]

GCC resolves the loader path for you before calling linker internally.

You will read more on that in Chapter 15 which is entirely dedicated to writing dynamic libraries in C and assembly.

Again thank you and I am going to correct that in my next commit.

from low-level-programming.

sayon avatar sayon commented on August 22, 2024

Fixed in #13

from low-level-programming.

yeah-boi avatar yeah-boi commented on August 22, 2024

Thanks for the solusion and explanation.

The same correction needs to be done for the make files also. I'll create a pull request for that.

from low-level-programming.

yeah-boi avatar yeah-boi commented on August 22, 2024

Makefiles fixed in pull request #14.

from low-level-programming.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.